Source file btype.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
open Asttypes
open Types
open Local_store
let print_raw =
ref (fun _ -> assert false : Format.formatter -> type_expr -> unit)
let wrap_repr f ty = f (Transient_expr.repr ty)
let wrap_type_expr f tty = f (Transient_expr.type_expr tty)
module TransientTypeSet = Set.Make(TransientTypeOps)
module TypeSet = struct
include TransientTypeSet
let add = wrap_repr add
let mem = wrap_repr mem
let singleton = wrap_repr singleton
let exists p = TransientTypeSet.exists (wrap_type_expr p)
let elements set =
List.map Transient_expr.type_expr (TransientTypeSet.elements set)
let debug_print ppf t =
Format.(
fprintf ppf "{ %a }"
(pp_print_seq
~pp_sep:(fun ppf () -> fprintf ppf ";@,")
!print_raw)
(to_seq t |> Seq.map Transient_expr.type_expr))
end
module TransientTypeMap = Map.Make(TransientTypeOps)
module TypeMap = struct
include TransientTypeMap
let add ty = wrap_repr add ty
let find ty = wrap_repr find ty
let singleton ty = wrap_repr singleton ty
let fold f = TransientTypeMap.fold (wrap_type_expr f)
end
module TypeHash = struct
include TransientTypeHash
let mem hash = wrap_repr (mem hash)
let add hash = wrap_repr (add hash)
let replace hash = wrap_repr (replace hash)
let remove hash = wrap_repr (remove hash)
let find hash = wrap_repr (find hash)
let find_opt hash = wrap_repr (find_opt hash)
let iter f = TransientTypeHash.iter (wrap_type_expr f)
end
module TransientTypePairs =
Hashtbl.Make (struct
type t = transient_expr * transient_expr
let equal (t1, t1') (t2, t2') = (t1 == t2) && (t1' == t2')
let hash (t, t') = t.id + 93 * t'.id
end)
module TypePairs = struct
module H = TransientTypePairs
open Transient_expr
type t = {
set : unit H.t;
mutable elems : (transient_expr * transient_expr) list;
}
let create n =
{ elems = []; set = H.create n }
let clear t =
t.elems <- [];
H.clear t.set
let repr2 (t1, t2) = (repr t1, repr t2)
let add t p =
let p = repr2 p in
if H.mem t.set p then () else begin
H.add t.set p ();
t.elems <- p :: t.elems
end
let mem t p = H.mem t.set (repr2 p)
let iter f t =
List.rev t.elems
|> List.iter (fun (t1,t2) ->
f (type_expr t1, type_expr t2))
end
let generic_level = Ident.highest_scope
let lowest_level = Ident.lowest_scope
let newgenty desc = newty2 ~level:generic_level desc
let newgenvar ?name jkind = newgenty (Tvar { name; jkind })
let newgenstub ~scope jkind =
newty3 ~level:generic_level ~scope (Tvar { name=None; jkind })
let new_splice_ty t = newty2 ~level:(get_level t) (Tsplice t)
let new_quote_ty t = newty2 ~level:(get_level t) (Tquote t)
let new_quote_eval_ty t = newty2 ~level:(get_level t) (Tquote_eval t)
let is_Tvar ty = match get_desc ty with Tvar _ -> true | _ -> false
let is_Tunivar ty = match get_desc ty with Tunivar _ -> true | _ -> false
let is_Tconstr ty = match get_desc ty with Tconstr _ -> true | _ -> false
let is_Tpoly ty = match get_desc ty with Tpoly _ -> true | _ -> false
let type_kind_is_abstract decl =
match decl.type_kind with Type_abstract _ -> true | _ -> false
let type_origin decl =
match decl.type_kind with
| Type_abstract origin -> origin
| Type_variant _ | Type_record _ | Type_record_unboxed_product _
| Type_open ->
Definition
let dummy_method = "*dummy method*"
let merge_fixed_explanation fixed1 fixed2 =
match fixed1, fixed2 with
| Some Univar _ as x, _ | _, (Some Univar _ as x) -> x
| Some Fixed_private as x, _ | _, (Some Fixed_private as x) -> x
| Some Reified _ as x, _ | _, (Some Reified _ as x) -> x
| Some Rigid as x, _ | _, (Some Rigid as x) -> x
| Some Fixed_existential as x, _ | _, (Some Fixed_existential as x) -> x
| None, None -> None
let fixed_explanation row =
match row_fixed row with
| Some _ as x -> x
| None ->
let ty = row_more row in
match get_desc ty with
| Tvar _ | Tnil -> None
| Tunivar _ -> Some (Univar ty)
| Tconstr (p,_,_) -> Some (Reified p)
| Tof_kind _ -> Some Fixed_existential
| _ -> assert false
let is_fixed row = match row_fixed row with
| None -> false
| Some _ -> true
let has_fixed_explanation row = fixed_explanation row <> None
let static_row row =
row_closed row &&
List.for_all
(fun (_,f) -> match row_field_repr f with Reither _ -> false | _ -> true)
(row_fields row)
let tvariant_not_immediate row =
not (row_closed row)
|| List.exists
(fun (_,field) -> match row_field_repr field with
| Rpresent (Some _) | Reither (false, _, _) -> true
| _ -> false)
(row_fields row)
let hash_variant s =
let accu = ref 0 in
for i = 0 to String.length s - 1 do
accu := 223 * !accu + Char.code s.[i]
done;
accu := !accu land (1 lsl 31 - 1);
if !accu > 0x3FFFFFFF then !accu - (1 lsl 31) else !accu
let proxy ty =
match get_desc ty with
| Tvariant row when not (static_row row) ->
row_more row
| Tobject (ty, _) ->
let rec proxy_obj ty =
match get_desc ty with
Tfield (_, _, _, ty) -> proxy_obj ty
| Tvar _ | Tunivar _ | Tconstr _ -> ty
| Tnil -> ty
| _ -> assert false
in proxy_obj ty
| _ -> ty
let row_of_type t =
match get_desc t with
Tobject(t,_) ->
let rec get_row t =
match get_desc t with
Tfield(_,_,_,t) -> get_row t
| _ -> t
in get_row t
| Tvariant row ->
row_more row
| _ ->
t
let has_constr_row t =
not (is_Tconstr t) && is_Tconstr (row_of_type t)
let is_row_name s =
let l = String.length s in
l > 4 && String.sub s (l-4) 4 = "#row"
let is_constr_row ~allow_ident t =
match get_desc t with
Tconstr (Path.Pident id, _, _) when allow_ident ->
is_row_name (Ident.name id)
| Tconstr (Path.Pdot (_, s), _, _) -> is_row_name s
| _ -> false
let set_static_row_name decl path =
match decl.type_manifest with
None -> ()
| Some ty ->
match get_desc ty with
Tvariant row when static_row row ->
let row =
set_row_name row (Some (path, decl.type_params)) in
set_type_desc ty (Tvariant row)
| _ -> ()
let fold_row f init row =
let result =
List.fold_left
(fun init (_, fi) ->
match row_field_repr fi with
| Rpresent(Some ty) -> f init ty
| Reither(_, tl, _) -> List.fold_left f init tl
| _ -> init)
init
(row_fields row)
in
match get_desc (row_more row) with
| Tvar _ | Tunivar _ | Tsubst _ | Tconstr _ | Tnil
| Tof_kind _ ->
begin match
Option.map (fun (_,l) -> List.fold_left f result l) (row_name row)
with
| None -> result
| Some result -> result
end
| _ -> assert false
let iter_row f row =
fold_row (fun () v -> f v) () row
let fold_type_expr f init ty =
match get_desc ty with
Tvar _ -> init
| Tarrow (_, ty1, ty2, _) ->
let result = f init ty1 in
f result ty2
| Ttuple l -> List.fold_left f init (List.map snd l)
| Tunboxed_tuple l -> List.fold_left f init (List.map snd l)
| Tconstr (_, l, _) -> List.fold_left f init l
| Tobject(ty, {contents = Some (_, p)}) ->
let result = f init ty in
List.fold_left f result p
| Tobject (ty, _) -> f init ty
| Tvariant row ->
let result = fold_row f init row in
f result (row_more row)
| Tquote ty -> f init ty
| Tsplice ty -> f init ty
| Tquote_eval ty -> f init ty
| Tfield (_, _, ty1, ty2) ->
let result = f init ty1 in
f result ty2
| Tnil -> init
| Tlink _
| Tsubst _ -> assert false
| Tunivar _ -> init
| Tpoly (ty, tyl) ->
let result = f init ty in
List.fold_left f result tyl
| Trepr (ty, _sort_vars) ->
f init ty
| Tpackage (_, fl) ->
List.fold_left (fun result (_n, ty) -> f result ty) init fl
| Tof_kind _ -> init
let iter_type_expr f ty =
fold_type_expr (fun () v -> f v) () ty
let rec iter_abbrev f = function
Mnil -> ()
| Mcons(_, _, ty, ty', rem) -> f ty; f ty'; iter_abbrev f rem
| Mlink rem -> iter_abbrev f !rem
let iter_type_expr_cstr_args f = function
| Cstr_tuple tl -> List.iter (fun ca -> f ca.ca_type) tl
| Cstr_record lbls -> List.iter (fun d -> f d.ld_type) lbls
let map_type_expr_cstr_args f = function
| Cstr_tuple tl -> Cstr_tuple (List.map (fun ca -> {ca with ca_type=f ca.ca_type}) tl)
| Cstr_record lbls ->
Cstr_record (List.map (fun d -> {d with ld_type=f d.ld_type}) lbls)
let iter_type_expr_kind f = function
| Type_abstract _ -> ()
| Type_variant (cstrs, _, _) ->
List.iter
(fun cd ->
iter_type_expr_cstr_args f cd.cd_args;
Option.iter f cd.cd_res
)
cstrs
| Type_record(lbls, _, _) ->
List.iter (fun d -> f d.ld_type) lbls
| Type_record_unboxed_product(lbls, _, _) ->
List.iter (fun d -> f d.ld_type) lbls
| Type_open ->
()
let rec mark_type mark ty =
if try_mark_node mark ty then iter_type_expr (mark_type mark) ty
let mark_type_params mark ty =
iter_type_expr (mark_type mark) ty
type 'a type_iterators =
{ it_signature: 'a type_iterators -> signature -> unit;
it_signature_item: 'a type_iterators -> signature_item -> unit;
it_value_description: 'a type_iterators -> value_description -> unit;
it_type_declaration: 'a type_iterators -> type_declaration -> unit;
it_extension_constructor:
'a type_iterators -> extension_constructor -> unit;
it_module_declaration: 'a type_iterators -> module_declaration -> unit;
it_modtype_declaration: 'a type_iterators -> modtype_declaration -> unit;
it_class_declaration: 'a type_iterators -> class_declaration -> unit;
it_class_type_declaration:
'a type_iterators -> class_type_declaration -> unit;
it_jkind_declaration : 'a type_iterators -> jkind_declaration -> unit;
it_functor_param: 'a type_iterators -> functor_parameter -> unit;
it_module_type: 'a type_iterators -> module_type -> unit;
it_class_type: 'a type_iterators -> class_type -> unit;
it_type_kind: 'a type_iterators -> type_decl_kind -> unit;
it_do_type_expr: 'a type_iterators -> 'a;
it_type_expr: 'a type_iterators -> type_expr -> unit;
it_path: Path.t -> unit; }
type type_iterators_full = (type_expr -> unit) type_iterators
type type_iterators_without_type_expr = (unit -> unit) type_iterators
let type_iterators_without_type_expr =
let it_signature it =
List.iter (it.it_signature_item it)
and it_signature_item it = function
Sig_value (_, vd, _) -> it.it_value_description it vd
| Sig_type (_, td, _, _) -> it.it_type_declaration it td
| Sig_typext (_, td, _, _) -> it.it_extension_constructor it td
| Sig_module (_, _, md, _, _) -> it.it_module_declaration it md
| Sig_modtype (_, mtd, _) -> it.it_modtype_declaration it mtd
| Sig_class (_, cd, _, _) -> it.it_class_declaration it cd
| Sig_class_type (_, ctd, _, _) -> it.it_class_type_declaration it ctd
| Sig_jkind (_ , jkd, _) -> it.it_jkind_declaration it jkd
and it_value_description it vd =
it.it_type_expr it vd.val_type
and it_type_declaration it td =
List.iter (it.it_type_expr it) td.type_params;
Option.iter (it.it_type_expr it) td.type_manifest;
Option.iter (it.it_type_declaration it) td.type_unboxed_version;
it.it_type_kind it td.type_kind
and it_extension_constructor it td =
it.it_path td.ext_type_path;
List.iter (it.it_type_expr it) td.ext_type_params;
iter_type_expr_cstr_args (it.it_type_expr it) td.ext_args;
Option.iter (it.it_type_expr it) td.ext_ret_type
and it_module_declaration it md =
it.it_module_type it md.md_type
and it_modtype_declaration it mtd =
Option.iter (it.it_module_type it) mtd.mtd_type
and it_class_declaration it cd =
List.iter (it.it_type_expr it) cd.cty_params;
it.it_class_type it cd.cty_type;
Option.iter (it.it_type_expr it) cd.cty_new;
it.it_path cd.cty_path
and it_class_type_declaration it ctd =
List.iter (it.it_type_expr it) ctd.clty_params;
it.it_class_type it ctd.clty_type;
it.it_path ctd.clty_path
and it_jkind_declaration it jkd =
match jkd.jkind_manifest with
| None -> ()
| Some { base = Kconstr p; mod_bounds = _; with_bounds = No_with_bounds } ->
it.it_path p
| Some { base = Layout _; mod_bounds = _; with_bounds = No_with_bounds } ->
()
and it_functor_param it = function
| Unit -> ()
| Named (_, mt, _) -> it.it_module_type it mt
and it_module_type it = function
Mty_ident p
| Mty_alias p -> it.it_path p
| Mty_for_hole -> ()
| Mty_signature sg -> it.it_signature it sg
| Mty_functor (p, mt, _) ->
it.it_functor_param it p;
it.it_module_type it mt
| Mty_strengthen (mty, p, _) ->
it.it_module_type it mty;
it.it_path p
and it_class_type it = function
Cty_constr (p, tyl, cty) ->
it.it_path p;
List.iter (it.it_type_expr it) tyl;
it.it_class_type it cty
| Cty_signature cs ->
it.it_type_expr it cs.csig_self;
it.it_type_expr it cs.csig_self_row;
Vars.iter (fun _ (_,_,ty) -> it.it_type_expr it ty) cs.csig_vars;
Meths.iter (fun _ (_,_,ty) -> it.it_type_expr it ty) cs.csig_meths
| Cty_arrow (_, ty, cty) ->
it.it_type_expr it ty;
it.it_class_type it cty
and it_type_kind it kind =
iter_type_expr_kind (it.it_type_expr it) kind
and it_path _p = ()
in
{ it_path; it_type_expr = (fun _ _ -> ()); it_do_type_expr = (fun _ _ -> ());
it_type_kind; it_class_type; it_functor_param; it_module_type;
it_signature; it_class_type_declaration; it_class_declaration;
it_jkind_declaration;
it_modtype_declaration; it_module_declaration; it_extension_constructor;
it_type_declaration; it_value_description; it_signature_item; }
let type_iterators mark =
let it_type_expr it ty =
if try_mark_node mark ty then it.it_do_type_expr it ty
and it_do_type_expr it ty =
iter_type_expr (it.it_type_expr it) ty;
match get_desc ty with
Tconstr (p, _, _)
| Tobject (_, {contents=Some (p, _)})
| Tpackage (p, _) ->
it.it_path p
| Tvariant row ->
Option.iter (fun (p,_) -> it.it_path p) (row_name row)
| _ -> ()
in
{type_iterators_without_type_expr with it_type_expr; it_do_type_expr}
let copy_row f fixed row keep more =
let Row {fields = orig_fields; fixed = orig_fixed; closed; name = orig_name} =
row_repr row in
let fields = List.map
(fun (l, fi) -> l,
match row_field_repr fi with
| Rpresent oty -> rf_present (Option.map f oty)
| Reither(c, tl, m) ->
let use_ext_of = if keep then Some fi else None in
let m = if is_fixed row then fixed else m in
let tl = List.map f tl in
rf_either tl ?use_ext_of ~no_arg:c ~matched:m
| Rabsent -> rf_absent)
orig_fields in
let name =
match orig_name with
| None -> None
| Some (path, tl) -> Some (path, List.map f tl) in
let fixed = if fixed then orig_fixed else None in
create_row ~fields ~more ~fixed ~closed ~name
let copy_commu c = if is_commu_ok c then commu_ok else commu_var ()
let instance_jkind (t : jkind_lr) : jkind_lr =
let rec instance_layout (l : Jkind_types.Sort.t Jkind_types.Layout.t)
: Jkind_types.Sort.t Jkind_types.Layout.t =
match l with
| Any _ -> l
| Sort (s, sa) -> Sort (Jkind_types.Sort.instance s, sa)
| Product ts -> Product (List.map instance_layout ts)
in
match t.jkind.base with
| Kconstr _ -> t
| Layout l ->
{ t with jkind = { t.jkind with base = Layout (instance_layout l) } }
let rec copy_type_desc ?(keep_names=false) f = function
Tvar { name; jkind } ->
let jkind = instance_jkind jkind in
if keep_names then Tvar { name; jkind } else Tvar { name=None; jkind }
| Tarrow (p, ty1, ty2, c)-> Tarrow (p, f ty1, f ty2, copy_commu c)
| Ttuple l -> Ttuple (List.map (fun (label, t) -> label, f t) l)
| Tunboxed_tuple l ->
Tunboxed_tuple (List.map (fun (label, t) -> label, f t) l)
| Tconstr (p, l, _) -> Tconstr (p, List.map f l, ref Mnil)
| Tobject(ty, {contents = Some (p, tl)})
-> Tobject (f ty, ref (Some(p, List.map f tl)))
| Tobject (ty, _) -> Tobject (f ty, ref None)
| Tvariant _ -> assert false
| Tquote ty -> Tquote (f ty)
| Tsplice ty -> Tsplice (f ty)
| Tquote_eval ty -> Tquote_eval (f ty)
| Tfield (p, k, ty1, ty2) ->
Tfield (p, field_kind_internal_repr k, f ty1, f ty2)
| Tnil -> Tnil
| Tlink ty -> copy_type_desc f (get_desc ty)
| Tsubst _ -> assert false
| Tunivar _ as ty -> ty
| Tpoly (ty, tyl) ->
let tyl = List.map f tyl in
Tpoly (f ty, tyl)
| Trepr (ty, sort_vars) ->
Trepr (f ty, sort_vars)
| Tpackage (p, fl) -> Tpackage (p, List.map (fun (n, ty) -> (n, f ty)) fl)
| Tof_kind jk -> Tof_kind jk
module For_copy : sig
type copy_scope
val redirect_desc: copy_scope -> type_expr -> type_desc -> unit
val with_scope: (copy_scope -> 'a) -> 'a
end = struct
type copy_scope = {
mutable saved_desc : (transient_expr * type_desc) list;
}
let redirect_desc copy_scope ty desc =
let ty = Transient_expr.repr ty in
copy_scope.saved_desc <- (ty, ty.desc) :: copy_scope.saved_desc;
Transient_expr.set_desc ty desc
let cleanup { saved_desc; _ } =
List.iter (fun (ty, desc) -> Transient_expr.set_desc ty desc) saved_desc
let with_scope f =
let scope = { saved_desc = [] } in
Fun.protect ~finally:(fun () -> cleanup scope) (fun () -> f scope)
end
let lte_public p1 p2 =
match p1, p2 with
| Private, _ | _, Public -> true
| Public, Private -> false
let rec find_expans priv p1 = function
Mnil -> None
| Mcons (priv', p2, _ty0, ty, _)
when lte_public priv priv' && Path.same p1 p2 -> Some ty
| Mcons (_, _, _, _, rem) -> find_expans priv p1 rem
| Mlink {contents = rem} -> find_expans priv p1 rem
let memo = s_ref []
let cleanup_abbrev () =
List.iter (fun abbr -> abbr := Mnil) !memo;
memo := []
let memorize_abbrev mem priv path v v' =
mem := Mcons (priv, path, v, v', !mem);
memo := mem :: !memo
let rec forget_abbrev_rec mem path =
match mem with
Mnil ->
mem
| Mcons (_, path', _, _, rem) when Path.same path path' ->
rem
| Mcons (priv, path', v, v', rem) ->
Mcons (priv, path', v, v', forget_abbrev_rec rem path)
| Mlink mem' ->
mem' := forget_abbrev_rec !mem' path;
raise Exit
let forget_abbrev mem path =
try mem := forget_abbrev_rec !mem path with Exit -> ()
let snapshot = snapshot
let backtrack = backtrack ~cleanup_abbrev
let is_optional_parsetree : Parsetree.arg_label -> bool = function
Optional _ -> true
| _ -> false
let is_optional = function Optional _ -> true | _ -> false
let is_position = function Position _ -> true | _ -> false
let is_omittable = function
Optional _
| Position _ -> true
| Nolabel | Labelled _ -> false
let label_name = function
Nolabel -> ""
| Labelled s
| Optional s
| Position s -> s
let prefixed_label_name = function
Nolabel -> ""
| Labelled s | Position s -> "~" ^ s
| Optional s -> "?" ^ s
let rec hd l = function
| [] -> None
| (l',t as p) :: ls ->
if label_name l' = l then
Some (l', t, hd <> [], List.rev_append hd ls)
else
extract_label_aux (p::hd) l ls
let l ls = extract_label_aux [] l ls
let rec signature_of_class_type =
function
Cty_constr (_, _, cty) -> signature_of_class_type cty
| Cty_signature sign -> sign
| Cty_arrow (_, _, cty) -> signature_of_class_type cty
let rec class_body cty =
match cty with
Cty_constr _ ->
cty
| Cty_signature _ ->
cty
| Cty_arrow (_, _, cty) ->
class_body cty
let rec scrape_class_type =
function
Cty_constr (_, _, cty) -> scrape_class_type cty
| cty -> cty
let rec class_type_arity =
function
Cty_constr (_, _, cty) -> class_type_arity cty
| Cty_signature _ -> 0
| Cty_arrow (_, _, cty) -> 1 + class_type_arity cty
let rec abbreviate_class_type path params cty =
match cty with
Cty_constr (_, _, _) | Cty_signature _ ->
Cty_constr (path, params, cty)
| Cty_arrow (l, ty, cty) ->
Cty_arrow (l, ty, abbreviate_class_type path params cty)
let self_type cty =
(signature_of_class_type cty).csig_self
let self_type_row cty =
(signature_of_class_type cty).csig_self_row
let methods sign =
Meths.fold
(fun name _ l -> name :: l)
sign.csig_meths []
let virtual_methods sign =
Meths.fold
(fun name (_priv, vr, _ty) l ->
match vr with
| Virtual -> name :: l
| Concrete -> l)
sign.csig_meths []
let concrete_methods sign =
Meths.fold
(fun name (_priv, vr, _ty) s ->
match vr with
| Virtual -> s
| Concrete -> MethSet.add name s)
sign.csig_meths MethSet.empty
let public_methods sign =
Meths.fold
(fun name (priv, _vr, _ty) l ->
match priv with
| Mprivate _ -> l
| Mpublic -> name :: l)
sign.csig_meths []
let instance_vars sign =
Vars.fold
(fun name _ l -> name :: l)
sign.csig_vars []
let virtual_instance_vars sign =
Vars.fold
(fun name (_mut, vr, _ty) l ->
match vr with
| Virtual -> name :: l
| Concrete -> l)
sign.csig_vars []
let concrete_instance_vars sign =
Vars.fold
(fun name (_mut, vr, _ty) s ->
match vr with
| Virtual -> s
| Concrete -> VarSet.add name s)
sign.csig_vars VarSet.empty
let method_type label sign =
match Meths.find label sign.csig_meths with
| (_, _, ty) -> ty
| exception Not_found -> assert false
let instance_variable_type label sign =
match Vars.find label sign.csig_vars with
| (_, _, ty) -> ty
| exception Not_found -> assert false
let tpoly_is_mono ty =
match get_desc ty with
| Tpoly(_, []) -> true
| Tpoly(_, _ :: _) -> false
| _ -> assert false
let tpoly_get_poly ty =
match get_desc ty with
| Tpoly(ty, vars) -> (ty, vars)
| _ -> assert false
let tpoly_get_mono ty =
match get_desc ty with
| Tpoly(ty, []) -> ty
| _ -> assert false
let cstr_type_path cstr =
match get_desc cstr.cstr_res with
| Tconstr (p, _, _) -> p
| _ -> assert false
module Jkind0 = struct
open Allowance
module Mod_bounds = struct
module Crossing = Mode.Crossing
module Externality = Jkind_axis.Externality
type t = mod_bounds = {
crossing : Crossing.t;
externality: Externality.t;
}
let crossing t = t.crossing
let[@inline] modal ax t =
t |> crossing |> (Crossing.proj [@inlined hint]) ax
let areality = Crossing.Axis.Comonadic Areality
let linearity = Crossing.Axis.Comonadic Linearity
let uniqueness = Crossing.Axis.Monadic Uniqueness
let portability = Crossing.Axis.Comonadic Portability
let contention = Crossing.Axis.Monadic Contention
let forkable = Crossing.Axis.Comonadic Forkable
let yielding = Crossing.Axis.Comonadic Yielding
let statefulness = Crossing.Axis.Comonadic Statefulness
let visibility = Crossing.Axis.Monadic Visibility
let staticity = Crossing.Axis.Monadic Staticity
let[@inline] externality t = t.externality
let[@inline] create
crossing
~externality =
{
crossing;
externality;
}
let[@inline] set_crossing crossing t = { t with crossing }
let[@inline] set_externality externality t = { t with externality }
let[@inline] set_max_in_set t max_axes =
let open Jkind_axis.Axis_set in
let[@inline] modal ax =
if mem max_axes (Modal ax)
then (Crossing.Per_axis.max [@inlined hint]) ax
else modal ax t
in
if is_empty max_axes then t else
let regionality = modal areality in
let linearity = modal linearity in
let uniqueness = modal uniqueness in
let portability = modal portability in
let contention = modal contention in
let forkable = modal forkable in
let yielding = modal yielding in
let statefulness = modal statefulness in
let visibility = modal visibility in
let staticity = modal staticity in
let externality =
if mem max_axes (Nonmodal Externality)
then Externality.max
else t.externality
in
let monadic =
Crossing.Monadic.create ~uniqueness ~contention ~visibility ~staticity
in
let comonadic =
Crossing.Comonadic.create ~regionality ~linearity ~portability ~yielding
~forkable ~statefulness
in
let crossing : Mode.Crossing.t = { monadic; comonadic } in
{
crossing;
externality;
}
let[@inline] set_min_in_set t min_axes =
let open Jkind_axis.Axis_set in
let modal ax =
if mem min_axes (Modal ax)
then (Crossing.Per_axis.min [@inlined hint]) ax
else modal ax t
in
if is_empty min_axes then t else
let regionality = modal areality in
let linearity = modal linearity in
let uniqueness = modal uniqueness in
let portability = modal portability in
let contention = modal contention in
let forkable = modal forkable in
let yielding = modal yielding in
let statefulness = modal statefulness in
let visibility = modal visibility in
let staticity = modal staticity in
let externality =
if mem min_axes (Nonmodal Externality)
then Externality.min
else t.externality
in
let monadic =
Crossing.Monadic.create ~uniqueness ~contention ~visibility ~staticity
in
let comonadic =
Crossing.Comonadic.create ~regionality ~linearity ~portability ~yielding
~forkable ~statefulness
in
let crossing : Mode.Crossing.t = { monadic; comonadic } in
{
crossing;
externality;
}
let[@inline] is_max_within_set t axes =
let open Jkind_axis.Axis_set in
let modal ax =
not (mem axes (Modal ax)) ||
Crossing.Per_axis.((le [@inlined hint]) ax ((max [@inlined hint]) ax)
(modal ax t))
in
modal areality &&
modal linearity &&
modal uniqueness &&
modal portability &&
modal contention &&
modal forkable &&
modal yielding &&
modal statefulness &&
modal visibility &&
modal staticity &&
(not (mem axes (Nonmodal Externality)) ||
Externality.(le max (externality t)))
let min = create Crossing.min ~externality:Externality.min
let max = create Crossing.max ~externality:Externality.max
let[@inline] is_max m = m = max
let for_arrow =
let crossing =
Crossing.create ~linearity:false ~regionality:false ~uniqueness:true
~portability:false ~contention:true ~forkable:false ~yielding:false
~statefulness:false ~visibility:true ~staticity:false
in
create crossing ~externality:Externality.max
let debug_print ppf
{ crossing;
externality } =
Format.fprintf ppf "@[{ crossing = %a;@ externality = %a }@]"
(Format_doc.compat Crossing.print) crossing
(Format_doc.compat Externality.print) externality
let equal t1 t2 =
Misc.Le_result.equal ~le:Crossing.le (crossing t1) (crossing t2)
&& Externality.equal (externality t1) (externality t2)
let join t1 t2 =
let crossing = Crossing.join (crossing t1) (crossing t2) in
let externality = Externality.join (externality t1) (externality t2) in
create crossing ~externality
let axis t =
let (Crossing.Monadic.Atom.Modality
(Mode.Modality.Monadic.Atom.Join_const value)) = modal axis t
in
value
let axis t =
let (Crossing.Comonadic.Atom.Modality
(Mode.Modality.Comonadic.Atom.Meet_const value)) = modal axis t
in
value
let areality_const t = extract_comonadic areality t
let linearity_const t = extract_comonadic linearity t
let uniqueness_const t = extract_monadic uniqueness t
let portability_const t = extract_comonadic portability t
let contention_const t = extract_monadic contention t
let forkable_const t = extract_comonadic forkable t
let yielding_const t = extract_comonadic yielding t
let statefulness_const t = extract_comonadic statefulness t
let visibility_const t = extract_monadic visibility t
let staticity_const t = extract_monadic staticity t
let to_axis_lattice (t : t) : Axis_lattice.t =
Axis_lattice.create ~areality:(areality_const t)
~linearity:(linearity_const t) ~uniqueness:(uniqueness_const t)
~portability:(portability_const t) ~contention:(contention_const t)
~forkable:(forkable_const t) ~yielding:(yielding_const t)
~statefulness:(statefulness_const t) ~visibility:(visibility_const t)
~staticity:(staticity_const t) ~externality:(externality t)
let of_axis_lattice (x : Axis_lattice.t) : t =
let crossing = Axis_lattice.to_mode_crossing x in
create crossing ~externality:(Axis_lattice.externality x)
let meet t1 t2 =
let crossing = Crossing.meet (crossing t1) (crossing t2) in
let externality = Externality.meet (externality t1) (externality t2) in
create crossing ~externality
let relevant_axes_of_modality ~modality =
Jkind_axis.Axis_set.create ~f:(fun ~axis:(Pack axis) ->
match axis with
| Modal axis ->
let (P axis) = P axis |> Mode.Crossing.Axis.to_modality in
let modality = Mode.Modality.Const.proj axis modality in
not (Mode.Modality.Per_axis.is_constant axis modality)
| Nonmodal Externality -> true)
end
module Quality = struct
include Allowance.Magic_allow_disallow (struct
type (_, _, 'd) sided = 'd jkind_quality constraint 'd = 'l * 'r
let disallow_left :
type l r. (l * r) jkind_quality -> (disallowed * r) jkind_quality =
function
| Not_best -> Not_best
| Best -> Best
let disallow_right :
type l r. (l * r) jkind_quality -> (l * disallowed) jkind_quality =
function
| Not_best -> Not_best
| Best -> Best
let allow_left :
type l r. (allowed * r) jkind_quality -> (l * r) jkind_quality =
function
| Not_best -> Not_best
| Best -> Best
let allow_right :
type l r. (l * allowed) jkind_quality -> (l * r) jkind_quality =
function
| Not_best -> Not_best
end)
let try_allow_r :
type l r. (l * r) jkind_quality -> (l * allowed) jkind_quality option =
function
| Not_best -> Some Not_best
| Best -> None
end
module With_bounds = struct
type 'd t = 'd with_bounds constraint 'd = 'l * 'r
include Magic_allow_disallow (struct
type (_, _, 'd) sided = 'd t constraint 'd = 'l * 'r
let disallow_left : type l r. (l * r) t -> (disallowed * r) t = function
| No_with_bounds -> No_with_bounds
| With_bounds _ as b -> b
let disallow_right : type l r. (l * r) t -> (l * disallowed) t = function
| No_with_bounds -> No_with_bounds
| With_bounds _ as b -> b
let allow_left : type l r. (allowed * r) t -> (l * r) t = function
| No_with_bounds -> No_with_bounds
| With_bounds _ as b -> b
let allow_right : type l r. (l * allowed) t -> (l * r) t = function
| No_with_bounds -> No_with_bounds
end)
let try_allow_l : type l r. (l * r) t -> (allowed * r) t option = function
| No_with_bounds -> Some No_with_bounds
| With_bounds _ as b -> Some b
let try_allow_r : type l r. (l * r) t -> (l * allowed) t option = function
| No_with_bounds -> Some No_with_bounds
| With_bounds _ -> None
let map_type_expr (type l r) f : (l * r) t -> (l * r) t = function
| No_with_bounds -> No_with_bounds
| With_bounds tys ->
With_bounds (With_bounds_types.map_with_key (fun ty ti -> f ty, ti) tys)
let add_bound type_expr type_info tys =
With_bounds_types.update type_expr
(function
| None -> Some type_info
| Some ti -> Some (With_bounds_type_info.join ti type_info))
tys
let add_modality ~modality ~type_expr
(t : (allowed * 'r) t) : (allowed * 'r) t =
let relevant_axes =
Mod_bounds.relevant_axes_of_modality ~modality
in
match t with
| No_with_bounds ->
With_bounds
(With_bounds_types.singleton type_expr
({ relevant_axes } : With_bounds_type_info.t))
| With_bounds tys ->
With_bounds (add_bound type_expr { relevant_axes } tys)
let is_empty (type l r) (t : (l * r) t) : bool =
match t with
| No_with_bounds -> true
| With_bounds tys -> With_bounds_types.is_empty tys
end
module Base_and_axes = struct
module Allow_disallow = Allowance.Magic_allow_disallow (struct
type (_, 'layout, 'd) sided = ('layout, 'd) base_and_axes
let disallow_left t =
{ t with with_bounds = With_bounds.disallow_left t.with_bounds }
let disallow_right t =
{ t with with_bounds = With_bounds.disallow_right t.with_bounds }
let allow_left t =
{ t with with_bounds = With_bounds.allow_left t.with_bounds }
let allow_right t =
{ t with with_bounds = With_bounds.allow_right t.with_bounds }
end)
include Allow_disallow
let map_layout f t =
match t.base with
| Kconstr _ as k -> { t with base = k }
| Layout l -> { t with base = Layout (f l) }
let map_layout_option f t =
match t.base with
| Kconstr _ as k -> Some { t with base = k }
| Layout l -> (
match f l with None -> None | Some l -> Some { t with base = Layout l })
let map_type_expr f t =
{ t with with_bounds = With_bounds.map_type_expr f t.with_bounds }
let try_allow_l :
type l r.
('layout, l * r) base_and_axes ->
('layout, Allowance.allowed * r) base_and_axes option =
fun { base; mod_bounds; with_bounds } ->
match With_bounds.try_allow_l with_bounds with
| None -> None
| Some with_bounds ->
Some { base; mod_bounds = Obj.magic mod_bounds; with_bounds }
let try_allow_r { base; mod_bounds; with_bounds } =
match With_bounds.try_allow_r with_bounds with
| Some with_bounds ->
Some { base; mod_bounds = Obj.magic mod_bounds; with_bounds }
| None -> None
end
module Const = struct
type 'd t = (Jkind_types.Layout.Const.t, 'd) base_and_axes
include Allowance.Magic_allow_disallow (struct
include Base_and_axes.Allow_disallow
type (_, _, 'd) sided = 'd t
end)
let of_path path =
{ base = Kconstr path;
mod_bounds = Mod_bounds.max;
with_bounds = No_with_bounds
}
let max =
{ base = Layout Jkind_types.Layout.Const.max;
mod_bounds = Mod_bounds.max;
with_bounds = No_with_bounds
}
let shallow_no_with_bounds_and_equal t1 t2 =
let open Misc.Stdlib.Monad.Option.Syntax in
let t1_t2 =
let* t1 = Base_and_axes.try_allow_l t1 in
let* t1 = Base_and_axes.try_allow_r t1 in
let* t2 = Base_and_axes.try_allow_l t2 in
let* t2 = Base_and_axes.try_allow_r t2 in
Some (t1, t2)
in
match t1_t2 with
| None -> false
| Some (t1, t2) -> (
match t1.base, t2.base with
| Kconstr p1, Kconstr p2 ->
Path.same p1 p2 &&
Mod_bounds.equal t1.mod_bounds t2.mod_bounds
| Kconstr _, Layout _ | Layout _, Kconstr _ -> false
| Layout l1, Layout l2 ->
Jkind_types.Layout.Const.equal l1 l2 &&
Mod_bounds.equal t1.mod_bounds t2.mod_bounds
)
module Builtin = struct
open Jkind_types
type nonrec t =
{ jkind : (allowed * allowed) t;
name : string
}
let cross_all_except_staticity =
let ax : _ Mode.Crossing.Axis.t = Monadic Staticity in
Mode.Crossing.(set ax (Per_axis.max ax) min)
let mk_jkind ~crossing ~externality (layout : Layout.Const.t) =
let mod_bounds = Mod_bounds.create crossing ~externality in
{ base = Layout layout; mod_bounds; with_bounds = No_with_bounds }
let any =
{ jkind =
mk_jkind (Any Scannable_axes.max) ~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "any"
}
let any_mod_everything =
{ jkind =
mk_jkind (Any Scannable_axes.max)
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "any mod everything"
}
let scannable =
{ jkind =
mk_jkind (Base (Scannable, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "scannable"
}
let value_or_null =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Maybe_null;
separability = Maybe_separable }))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "value_or_null"
}
let value_maybe_null =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Maybe_null; separability = Separable }))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "value_maybe_null"
}
let value_maybe_separable =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Non_null; separability = Maybe_separable }))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "value_maybe_separable"
}
let value_or_null_mod_everything =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Maybe_null;
separability = Maybe_separable }))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "value_or_null mod everything"
}
let value =
{ jkind =
mk_jkind (Base (Scannable, Scannable_axes.value_axes))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.max;
name = "value"
}
let value_mod_everything =
{ jkind =
mk_jkind (Base (Scannable, Scannable_axes.value_axes))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "value mod everything"
}
let immutable_data_mod_bounds =
let open Mod_bounds in
let crossing =
Crossing.create ~regionality:false ~linearity:true ~portability:true
~forkable:true ~yielding:true ~uniqueness:false ~contention:true
~statefulness:true ~visibility:true ~staticity:false
in
create crossing ~externality:Externality.max
let immutable_data =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Non_null; separability = Non_float }));
mod_bounds = immutable_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "immutable_data"
}
let immutable_data_or_null =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Maybe_null; separability = Non_float }));
mod_bounds = immutable_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "immutable_data_or_null"
}
let exn =
let open Mod_bounds in
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Non_null; separability = Non_float }));
mod_bounds =
(let crossing =
Crossing.create ~regionality:false ~linearity:false
~portability:true ~forkable:false ~yielding:false
~uniqueness:false ~contention:true ~statefulness:true
~visibility:true ~staticity:false
in
create crossing ~externality:Externality.max);
with_bounds = No_with_bounds
};
name = "exn"
}
let sync_data_mod_bounds =
let open Mod_bounds in
let crossing =
Crossing.create ~regionality:false ~linearity:true ~portability:true
~forkable:true ~yielding:true ~uniqueness:false ~contention:true
~statefulness:true ~visibility:false ~staticity:false
in
create crossing ~externality:Externality.max
let sync_data =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Non_null; separability = Non_float }));
mod_bounds = sync_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "sync_data"
}
let sync_data_or_null =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Maybe_null; separability = Non_float }));
mod_bounds = sync_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "sync_data_or_null"
}
let mutable_data_mod_bounds =
let open Mod_bounds in
let crossing =
Crossing.create ~regionality:false ~linearity:true ~portability:true
~forkable:true ~yielding:true ~contention:false ~uniqueness:false
~statefulness:true ~visibility:false ~staticity:false
in
create crossing ~externality:Externality.max
let mutable_data =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Non_null; separability = Non_float }));
mod_bounds = mutable_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "mutable_data"
}
let mutable_data_or_null =
{ jkind =
{ base =
Layout
(Base
(Scannable,
{ nullability = Maybe_null; separability = Non_float }));
mod_bounds = mutable_data_mod_bounds;
with_bounds = No_with_bounds
};
name = "mutable_data_or_null"
}
let void =
{ jkind =
mk_jkind (Base (Void, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "void"
}
let void_mod_everything =
{ jkind =
mk_jkind (Base (Void, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "void mod everything"
}
let kind_of_unboxed_unit = void_mod_everything
let immediate =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Non_null;
separability = Non_pointer
}))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "immediate"
}
let immediate_or_null =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Maybe_null; separability = Non_pointer }))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "immediate_or_null"
}
let immediate64 =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Non_null;
separability = Non_pointer64
}))
~crossing:cross_all_except_staticity
~externality:External64;
name = "immediate64"
}
let immediate64_or_null =
{ jkind =
mk_jkind
(Base
(Scannable,
{ nullability = Maybe_null; separability = Non_pointer64 }))
~crossing:cross_all_except_staticity
~externality:External64;
name = "immediate64_or_null"
}
let float64 =
{ jkind =
mk_jkind (Base (Float64, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "float64"
}
let kind_of_unboxed_float =
{ jkind =
mk_jkind (Base (Float64, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "float64 mod everything"
}
let float32 =
{ jkind =
mk_jkind (Base (Float32, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "float32"
}
let kind_of_unboxed_float32 =
{ jkind =
mk_jkind (Base (Float32, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "float32 mod everything"
}
let word =
{ jkind =
mk_jkind (Base (Word, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "word"
}
let kind_of_unboxed_nativeint =
{ jkind =
mk_jkind (Base (Word, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "word mod everything"
}
let untagged_immediate =
{ jkind =
mk_jkind (Base (Untagged_immediate, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "untagged_immediate"
}
let kind_of_untagged_int =
{ jkind =
mk_jkind (Base (Untagged_immediate, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "untagged_immediate mod everything"
}
let bits8 =
{ jkind =
mk_jkind (Base (Bits8, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "bits8"
}
let kind_of_unboxed_int8 =
{ jkind =
mk_jkind (Base (Bits8, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "bits8 mod everything"
}
let kind_of_unboxed_bool = kind_of_unboxed_int8
let bits16 =
{ jkind =
mk_jkind (Base (Bits16, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "bits16"
}
let kind_of_unboxed_int16 =
{ jkind =
mk_jkind (Base (Bits16, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "bits16 mod everything"
}
let bits32 =
{ jkind =
mk_jkind (Base (Bits32, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "bits32"
}
let kind_of_unboxed_int32 =
{ jkind =
mk_jkind (Base (Bits32, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "bits32 mod everything"
}
let bits64 =
{ jkind =
mk_jkind (Base (Bits64, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "bits64"
}
let kind_of_unboxed_int64 =
{ jkind =
mk_jkind (Base (Bits64, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "bits64 mod everything"
}
let kind_of_idx =
{ jkind =
mk_jkind (Base (Bits64, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "bits64 mod everything"
}
let vec128 =
{ jkind =
mk_jkind (Base (Vec128, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "vec128"
}
let vec256 =
{ jkind =
mk_jkind (Base (Vec256, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "vec256"
}
let vec512 =
{ jkind =
mk_jkind (Base (Vec512, Scannable_axes.max))
~crossing:Mode.Crossing.max
~externality:Mod_bounds.Externality.min;
name = "vec512"
}
let kind_of_unboxed_128bit_vectors =
{ jkind =
mk_jkind (Base (Vec128, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "vec128 mod everything"
}
let kind_of_unboxed_256bit_vectors =
{ jkind =
mk_jkind (Base (Vec256, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "vec256 mod everything"
}
let kind_of_unboxed_512bit_vectors =
{ jkind =
mk_jkind (Base (Vec512, Scannable_axes.max))
~crossing:cross_all_except_staticity
~externality:Mod_bounds.Externality.min;
name = "vec512 mod everything"
}
let builtins =
[ any;
value_maybe_null;
value_maybe_separable;
value_or_null;
value;
immutable_data;
immutable_data_or_null;
sync_data;
sync_data_or_null;
mutable_data;
mutable_data_or_null;
void;
immediate;
immediate_or_null;
immediate64;
immediate64_or_null;
untagged_immediate;
float64;
float32;
word;
bits8;
bits16;
bits32;
bits64;
vec128;
vec256;
vec512 ]
let additional_common_jkinds =
[ any_mod_everything;
value_mod_everything;
value_or_null_mod_everything;
void_mod_everything;
kind_of_untagged_int;
kind_of_unboxed_float;
kind_of_unboxed_float32;
kind_of_unboxed_nativeint;
kind_of_unboxed_int8;
kind_of_unboxed_int16;
kind_of_unboxed_int32;
kind_of_unboxed_int64;
kind_of_unboxed_128bit_vectors;
kind_of_unboxed_256bit_vectors;
kind_of_unboxed_512bit_vectors ]
let common_jkinds = builtins @ additional_common_jkinds
let of_attribute : Builtin_attributes.jkind_attribute -> t = function
| Immediate -> immediate
| Immediate64 -> immediate64
end
end
module Jkind_desc = struct
let of_const t = Base_and_axes.map_layout Jkind_types.Layout.of_const t
let max = of_const Const.max
let map_type_expr f t = Base_and_axes.map_type_expr f t
let add_with_bounds ~type_expr ~modality t =
match get_desc type_expr with
| Tarrow (_, _, _, _) ->
{ t with
mod_bounds =
Mod_bounds.join t.mod_bounds
(Mod_bounds.set_min_in_set Mod_bounds.for_arrow
(Jkind_axis.Axis_set.complement
(Mod_bounds.relevant_axes_of_modality ~modality)))
}
| _ ->
{ t with
with_bounds =
With_bounds.add_modality ~type_expr ~modality
t.with_bounds
}
module Builtin = struct
let any = max
let scannable = of_const Const.Builtin.scannable.jkind
let value_or_null = of_const Const.Builtin.value_or_null.jkind
let value = of_const Const.Builtin.value.jkind
let immutable_data = of_const Const.Builtin.immutable_data.jkind
let sync_data = of_const Const.Builtin.sync_data.jkind
let mutable_data = of_const Const.Builtin.mutable_data.jkind
let void = of_const Const.Builtin.void.jkind
let immediate = of_const Const.Builtin.immediate.jkind
let immediate_or_null =
of_const Const.Builtin.immediate_or_null.jkind
end
let product tys_modalities layouts =
let base = Layout (Jkind_types.Layout.product layouts) in
let mod_bounds = Mod_bounds.min in
let with_bounds =
List.fold_right
(fun (type_expr, modality) bounds ->
With_bounds.add_modality ~type_expr ~modality bounds)
tys_modalities No_with_bounds
in
{ base; mod_bounds; with_bounds }
let get_const t =
Base_and_axes.map_layout_option Jkind_types.Layout.get_const t
end
module Violation = struct
module Sub_failure_reason = struct
type t =
| Axis_disagreement of Jkind_axis.Axis.packed
| Layout_disagreement
| With_bounds_on_left
| Constrain_ran_out_of_fuel
end
type violation =
| Not_a_subjkind :
(allowed * 'r1) jkind * ('l * 'r2) jkind * Sub_failure_reason.t list
-> violation
| No_intersection : 'd jkind * ('l * allowed) jkind -> violation
type nonrec t =
{ violation : violation;
missing_cmi : Path.t option
}
end
module Jkind = struct
include Allowance.Magic_allow_disallow (struct
type (_, _, 'd) sided = 'd jkind
let disallow_right t =
{ t with
jkind = Base_and_axes.disallow_right t.jkind;
quality = Quality.disallow_right t.quality
}
let disallow_left t =
{ t with
jkind = Base_and_axes.disallow_left t.jkind;
quality = Quality.disallow_left t.quality
}
let allow_right t =
{ t with
jkind = Base_and_axes.allow_right t.jkind;
quality = Quality.allow_right t.quality
}
let allow_left t =
{ t with
jkind = Base_and_axes.allow_left t.jkind;
quality = Quality.allow_left t.quality
}
end)
let combine_mutability mut1 mut2 =
match mut1, mut2 with
| (Mutable { atomic = Nonatomic; mode = _ } as x), _
| _, (Mutable { atomic = Nonatomic; mode = _ } as x) ->
x
| (Mutable { atomic = Atomic; mode = _ } as x), _
| _, (Mutable { atomic = Atomic; mode = _ } as x) ->
x
| (Immutable as x), Immutable -> x
let try_allow_r t =
let open Misc.Stdlib.Monad.Option.Syntax in
let* jkind = Base_and_axes.try_allow_r t.jkind in
let* quality = Quality.try_allow_r t.quality in
Some { t with jkind; quality }
let fresh_jkind jkind ~annotation ~why =
{ jkind;
annotation;
history = Creation why;
has_warned = false;
ran_out_of_fuel_during_normalize = false;
quality = Not_best
}
|> allow_left |> allow_right
let fresh_jkind_poly jkind ~annotation ~why =
{ jkind;
annotation;
history = Creation why;
has_warned = false;
ran_out_of_fuel_during_normalize = false;
quality = Not_best
}
let mk_annot name =
Some
Parsetree.{
pjka_loc = Location.none;
pjka_desc = Pjk_abbreviation ({ loc = Location.none;
txt = (Lident name) }, [])
}
let mark_best (type l r) (t : (l * r) jkind) =
{ (disallow_right t) with quality = Best }
module Builtin = struct
let any_dummy_jkind =
{ jkind = Jkind_desc.max;
annotation = None;
history = Creation (Any_creation Dummy_jkind);
has_warned = false;
ran_out_of_fuel_during_normalize = false;
quality = Not_best
}
let any ~(why : Jkind_intf.History.any_creation_reason) =
match why with
| Dummy_jkind ->
any_dummy_jkind |> allow_left
|> allow_right
| _ ->
fresh_jkind Jkind_desc.Builtin.any
~annotation:(mk_annot "any") ~why:(Any_creation why)
let value_v1_safety_check =
{ jkind = Jkind_desc.Builtin.value_or_null;
annotation = mk_annot "value";
history = Creation (Value_or_null_creation V1_safety_check);
has_warned = false;
ran_out_of_fuel_during_normalize = false;
quality = Not_best
}
let void ~why =
fresh_jkind Jkind_desc.Builtin.void ~annotation:(mk_annot "void")
~why:(Void_creation why)
|> mark_best
let scannable ~why =
fresh_jkind Jkind_desc.Builtin.scannable
~annotation:(mk_annot "scannable") ~why:(Scannable_creation why)
let value_or_null ~why =
match (why : Jkind_intf.History.value_or_null_creation_reason) with
| V1_safety_check -> value_v1_safety_check |> allow_left |> allow_right
| _ ->
fresh_jkind Jkind_desc.Builtin.value_or_null
~annotation:(mk_annot "value_or_null")
~why:(Value_or_null_creation why)
let value ~(why : Jkind_intf.History.value_creation_reason) =
fresh_jkind Jkind_desc.Builtin.value ~annotation:(mk_annot "value")
~why:(Value_creation why)
let immutable_data ~(why : Jkind_intf.History.value_creation_reason) =
fresh_jkind Jkind_desc.Builtin.immutable_data
~annotation:(mk_annot "immutable_data")
~why:(Value_creation why)
let sync_data ~(why : Jkind_intf.History.value_creation_reason) =
fresh_jkind Jkind_desc.Builtin.sync_data
~annotation:(mk_annot "sync_data") ~why:(Value_creation why)
let mutable_data ~(why : Jkind_intf.History.value_creation_reason) =
fresh_jkind Jkind_desc.Builtin.mutable_data
~annotation:(mk_annot "mutable_data") ~why:(Value_creation why)
let immediate ~why =
fresh_jkind Jkind_desc.Builtin.immediate
~annotation:(mk_annot "immediate") ~why:(Immediate_creation why)
|> mark_best
let immediate_or_null ~why =
fresh_jkind Jkind_desc.Builtin.immediate_or_null
~annotation:(mk_annot "immediate_or_null")
~why:(Immediate_or_null_creation why)
let product ~why tys_modalities layouts =
let desc = Jkind_desc.product tys_modalities layouts in
fresh_jkind_poly desc ~annotation:None ~why:(Product_creation why)
|> mark_best
let product_of_sorts ~why ~level arity =
let layout =
Jkind_types.Layout.product
(List.init arity
(fun _ ->
fst
(Jkind_types.Layout.of_new_sort_var ~level
Jkind_types.Scannable_axes.max)))
in
let desc : _ jkind_desc =
{ base = Layout layout;
mod_bounds = Mod_bounds.max;
with_bounds = No_with_bounds }
in
fresh_jkind_poly desc ~annotation:None ~why:(Product_creation why)
end
let has_with_bounds (type r) (t : (_ * r) jkind) =
match t.jkind.with_bounds with
| No_with_bounds -> false
| With_bounds tys -> not (With_bounds_types.is_empty tys)
let of_const (type l r) ~annotation ~why ~(quality : (l * r) jkind_quality)
~ran_out_of_fuel_during_normalize (c : (l * r) Const.t) =
{ jkind = Base_and_axes.map_layout Jkind_types.Layout.of_const c;
annotation;
history = Creation why;
has_warned = false;
ran_out_of_fuel_during_normalize;
quality
}
let of_builtin ~why Const.Builtin.{ jkind; name } =
jkind
|> Base_and_axes.allow_left
|> Base_and_axes.disallow_right
|> of_const ~annotation:(mk_annot name)
~why
~quality:Best ~ran_out_of_fuel_during_normalize:false
let get_const t = Jkind_desc.get_const t.jkind
let instance = instance_jkind
let map_type_expr f t =
if has_with_bounds t
then { t with jkind = Jkind_desc.map_type_expr f t.jkind }
else t
let add_with_bounds ~modality ~type_expr t =
{ t with
jkind =
Jkind_desc.add_with_bounds ~type_expr ~modality t.jkind
}
let jkind_of_mutability mutability ~why =
(match mutability with
| Immutable -> Builtin.immutable_data
| Mutable { atomic = Atomic; _ } -> Builtin.sync_data
| Mutable { atomic = Nonatomic; _ } -> Builtin.mutable_data)
~why
let all_void_labels lbls =
List.for_all
(fun (lbl : label_declaration) ->
Jkind_types.Sort.Const.(all_void lbl.ld_sort))
lbls
let add_labels_as_with_bounds lbls jkind =
List.fold_right
(fun (lbl : label_declaration) ->
add_with_bounds ~type_expr:lbl.ld_type ~modality:lbl.ld_modalities)
lbls jkind
let for_boxed_record lbls =
if all_void_labels lbls
then Builtin.immediate ~why:Empty_record
else
let base =
lbls
|> List.map (fun (ld : label_declaration) -> ld.ld_mutable)
|> List.fold_left combine_mutability Immutable
|> jkind_of_mutability ~why:Boxed_record
|> mark_best
in
add_labels_as_with_bounds lbls base
let for_non_float ~(why : Jkind_intf.History.value_creation_reason) =
let mod_bounds =
Mod_bounds.create Mode.Crossing.max
~externality:Mod_bounds.Externality.max
in
fresh_jkind
{ base =
Layout
(Sort
(Base Scannable,
{ nullability = Non_null; separability = Non_float }));
mod_bounds;
with_bounds = No_with_bounds
}
~annotation:None ~why:(Value_creation why)
let gadt_payload_subst
~projected_params ~res_args ~payload_tys ~get_free_vars =
let domain, range, seen =
List.fold_left2
(fun ((domain, range, seen) as acc) res_arg projected_param ->
if TypeSet.mem res_arg seen
then
acc
else
match get_desc res_arg with
| Tvar _ ->
res_arg :: domain, projected_param :: range,
TypeSet.add res_arg seen
| _ -> acc)
([], [], TypeSet.empty)
res_args projected_params
in
let orphaned_type_var_set = TypeSet.diff (get_free_vars payload_tys) seen in
let orphaned_type_var_list = TypeSet.elements orphaned_type_var_set in
let mk_type_of_kind ty =
match get_desc ty with
| Tvar { jkind; name = _ } -> newgenty (Tof_kind jkind)
| _ ->
Misc.fatal_error
"post-condition of [free_variable_set_of_list] violated"
in
let type_of_kind_list = List.map mk_type_of_kind orphaned_type_var_list in
List.combine
(orphaned_type_var_list @ domain)
(type_of_kind_list @ range)
let for_boxed_variant ~loc ~decl_params ~type_apply ~get_free_vars cstrs =
let base =
let all_args_void =
List.for_all
(fun cstr ->
match cstr.cd_args with
| Cstr_tuple args ->
List.for_all
(fun arg -> Jkind_types.Sort.Const.(all_void arg.ca_sort)) args
| Cstr_record lbls -> all_void_labels lbls)
cstrs
in
if all_args_void
then (
let has_args =
List.exists
(fun cstr ->
match cstr.cd_args with
| Cstr_tuple (_ :: _) | Cstr_record (_ :: _) -> true
| Cstr_tuple [] | Cstr_record [] -> false)
cstrs
in
if has_args && Language_extension.erasable_extensions_only ()
then
Location.prerr_warning loc
(Warnings.Incompatible_with_upstream
Warnings.Immediate_void_variant);
Builtin.immediate ~why:Enumeration)
else
List.concat_map
(fun cstr ->
match cstr.cd_args with
| Cstr_tuple _ -> [Immutable]
| Cstr_record lbls -> List.map (fun ld -> ld.ld_mutable) lbls)
cstrs
|> List.fold_left combine_mutability Immutable
|> jkind_of_mutability ~why:Boxed_variant
in
let base = mark_best base in
let add_with_bounds_for_cstr jkind_so_far cstr =
let cstr_arg_tys, cstr_arg_modalities =
match cstr.cd_args with
| Cstr_tuple args ->
List.fold_left
(fun (tys, ms) arg -> arg.ca_type :: tys, arg.ca_modalities :: ms)
([], []) args
| Cstr_record lbls ->
List.fold_left
(fun (tys, ms) lbl -> lbl.ld_type :: tys, lbl.ld_modalities :: ms)
([], []) lbls
in
let cstr_arg_tys =
match cstr.cd_res with
| None -> cstr_arg_tys
| Some res ->
let apply_subst domain range tys =
if Misc.Stdlib.List.is_empty domain
then tys
else List.map (fun ty -> type_apply domain ty range) tys
in
let res_args =
match get_desc res with
| Tconstr (_, args, _) -> args
| _ -> Misc.fatal_error "cd_res must be Tconstr"
in
let =
gadt_payload_subst
~projected_params:decl_params
~res_args
~payload_tys:cstr_arg_tys
~get_free_vars
in
let domain, range = List.split extra_substs in
let cstr_arg_tys =
apply_subst
domain
range
cstr_arg_tys
in
cstr_arg_tys
in
List.fold_left2
(fun jkind type_expr modality ->
add_with_bounds ~modality ~type_expr jkind)
jkind_so_far cstr_arg_tys cstr_arg_modalities
in
List.fold_left add_with_bounds_for_cstr base cstrs
let for_float ident =
let crossing =
Mode.Crossing.create ~regionality:false ~linearity:true
~portability:true ~forkable:true ~yielding:true ~uniqueness:false
~contention:true ~statefulness:true ~visibility:true ~staticity:false
in
let mod_bounds =
Mod_bounds.create crossing ~externality:Mod_bounds.Externality.max
in
fresh_jkind
{ base =
Layout
(Sort
(Base Scannable,
{ nullability = Non_null; separability = Separable }));
mod_bounds;
with_bounds = No_with_bounds
}
~annotation:None ~why:(Primitive ident)
|> mark_best
let for_expr =
fresh_jkind
{ base = Layout (Sort (Base Scannable, { nullability = Non_null;
separability = Separable }));
mod_bounds = Mod_bounds.for_arrow;
with_bounds = No_with_bounds
}
~annotation:None ~why:(Value_creation Quoted_expression)
|> mark_best
let for_array_argument =
let mod_bounds =
Mod_bounds.create Mode.Crossing.max
~externality:Mod_bounds.Externality.max
in
fresh_jkind
{ base =
Layout
(Any { nullability = Maybe_null; separability = Separable });
mod_bounds;
with_bounds = No_with_bounds
}
~annotation:None ~why:(Any_creation Array_type_argument)
let for_or_null_argument ident =
let why : Jkind_intf.History.value_creation_reason =
Type_argument
{ parent_path = Path.Pident ident; position = 1; arity = 1 }
in
let mod_bounds =
Mod_bounds.create Mode.Crossing.max
~externality:Mod_bounds.Externality.max
in
fresh_jkind
{ base =
Layout
(Sort
(Base Scannable,
{ nullability = Non_null;
separability = Maybe_separable }));
mod_bounds;
with_bounds = No_with_bounds
}
~annotation:None ~why:(Value_creation why)
let for_variant_with_null_result path param =
let why : Jkind_intf.History.value_or_null_creation_reason =
Type_argument
{ parent_path = path; position = 1; arity = 1 }
in
Builtin.value_or_null ~why
|> add_with_bounds ~modality:Mode.Modality.Const.id ~type_expr:param
|> mark_best
end
include Jkind
end