Source file types.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
open Allowance
open Asttypes
module Rigid_name = struct
type unknown_id = Shape.Uid.t
type t =
| Atom of
{ constr : Path.t;
arg_index : int
}
| KAtom of Path.t
| Param of int
| Unknown of unknown_id
let compare a b =
if a == b
then 0
else
match a, b with
| Atom a1, Atom a2 ->
let h = Path.compare a1.constr a2.constr in
if h != 0 then h else Int.compare a1.arg_index a2.arg_index
| KAtom p1, KAtom p2 -> Path.compare p1 p2
| Param x, Param y -> Int.compare x y
| Atom _, _ -> -1
| _, Atom _ -> 1
| KAtom _, _ -> -1
| _, KAtom _ -> 1
| Unknown x, Unknown y -> Shape.Uid.compare x y
| Unknown _, _ -> 1
| _, Unknown _ -> -1
let to_string = function
| Atom { constr; arg_index } ->
let constr_s = Format_doc.asprintf "%a" Path.print constr in
Printf.sprintf "%s.%d" constr_s arg_index
| KAtom path ->
let path_s = Format_doc.asprintf "%a" Path.print path in
Printf.sprintf "katom[%s]" path_s
| Param i -> Printf.sprintf "param[%d]" i
| Unknown id ->
Format.asprintf "unknown[%a]" Shape.Uid.print id
let atomic constr arg_index = Atom { constr; arg_index }
let katom path = KAtom path
let param i = Param i
let unknown uid = Unknown uid
end
module Ldd = struct
module Name = Rigid_name
include (Ldd.Make (Rigid_name) :
Ldd_intf.S with module Name := Rigid_name)
end
type constructor_ikind =
{ base : Ldd.node;
coeffs : Ldd.node array;
}
type constructor_ikind_entry =
| Constructor_ikind of constructor_ikind
| No_constructor_ikind of string
type type_ikind = constructor_ikind_entry
let ikinds_todo (message : string) : type_ikind =
if !Clflags.ikinds_debug then
Format.eprintf "[ikinds-todo] %s@." message;
No_constructor_ikind message
type atomic =
| Nonatomic
| Atomic
type mutability =
| Immutable
| Mutable of
{ mode : Mode.Value.Comonadic.lr
; atomic : atomic
}
let is_mutable = function
| Immutable -> false
| Mutable _ -> true
let is_atomic = function
| Immutable -> false
| Mutable { atomic = Atomic; mode = _ } -> true
| Mutable { atomic = Nonatomic; mode = _ } -> false
(** Takes [m0] which is the parameter of [let mutable], returns the
mode of new values in future writes. *)
let mutable_mode m0 : _ Mode.Value.t =
{ comonadic = m0
; monadic = Mode.Value.Monadic.(min |> allow_left |> allow_right)
}
type mod_bounds =
{ crossing : Mode.Crossing.t;
externality: Jkind_axis.Externality.t;
}
module With_bounds_type_info = struct
type t = {relevant_axes : Jkind_axis.Axis_set.t } [@@unboxed]
let join { relevant_axes = axes1 } { relevant_axes = axes2 } =
{ relevant_axes = Jkind_axis.Axis_set.union axes1 axes2 }
end
type transient_expr =
{ mutable desc: type_desc;
mutable level: int;
mutable scope: scope_field;
id: int }
and scope_field = int
and type_expr = transient_expr
and type_desc =
| Tvar of { name : string option; jkind : jkind_lr }
| Tarrow of arrow_desc * type_expr * type_expr * commutable
| Ttuple of (string option * type_expr) list
| Tunboxed_tuple of (string option * type_expr) list
| Tconstr of Path.t * type_expr list * abbrev_memo ref
| Tobject of type_expr * (Path.t * type_expr list) option ref
| Tfield of string * field_kind * type_expr * type_expr
| Tquote of type_expr
| Tsplice of type_expr
| Tquote_eval of type_expr
| Tnil
| Tlink of type_expr
| Tsubst of type_expr * type_expr option
| Tvariant of row_desc
| Tunivar of { name : string option; jkind : jkind_lr }
| Tpoly of type_expr * type_expr list
| Trepr of type_expr * Jkind_types.Sort.univar list
| Tpackage of Path.t * (Longident.t * type_expr) list
| Tof_kind of jkind_lr
and arg_label =
| Nolabel
| Labelled of string
| Optional of string
| Position of string
and arrow_desc =
arg_label * Mode.Alloc.lr * Mode.Alloc.lr
and row_desc =
{ row_fields: (label * row_field) list;
row_more: type_expr;
row_closed: bool;
row_fixed: fixed_explanation option;
row_name: (Path.t * type_expr list) option }
and fixed_explanation =
| Univar of type_expr
| Fixed_private
| Reified of Path.t
| Rigid
| Fixed_existential
and row_field = [`some] row_field_gen
and _ row_field_gen =
RFpresent : type_expr option -> [> `some] row_field_gen
| RFeither :
{ no_arg: bool;
arg_type: type_expr list;
matched: bool;
ext: [`some | `none] row_field_gen ref} -> [> `some] row_field_gen
| RFabsent : [> `some] row_field_gen
| RFnone : [> `none] row_field_gen
and abbrev_memo =
Mnil
| Mcons of private_flag * Path.t * type_expr * type_expr * abbrev_memo
| Mlink of abbrev_memo ref
and any = [`some | `none | `var]
and field_kind = [`some|`var] field_kind_gen
and _ field_kind_gen =
FKvar : {mutable field_kind: any field_kind_gen} -> [> `var] field_kind_gen
| FKprivate : [> `none] field_kind_gen
| FKpublic : [> `some] field_kind_gen
| FKabsent : [> `some] field_kind_gen
and commutable = [`some|`var] commutable_gen
and _ commutable_gen =
Cok : [> `some] commutable_gen
| Cunknown : [> `none] commutable_gen
| Cvar : {mutable commu: any commutable_gen} -> [> `var] commutable_gen
and jkind_history =
| Interact of
{ reason : Jkind_intf.History.interact_reason;
jkind1 : jkind_desc_packed;
history1 : jkind_history;
jkind2 : jkind_desc_packed;
history2 : jkind_history
}
| Creation of Jkind_intf.History.creation_reason
and with_bounds_types
and 'd with_bounds =
| No_with_bounds : ('l * 'r) with_bounds
| With_bounds : with_bounds_types -> ('l * Allowance.disallowed) with_bounds
and 'layout jkind_base =
| Layout of 'layout
| Kconstr of Path.t
and ('layout, 'd) base_and_axes =
{ base : 'layout jkind_base;
mod_bounds : mod_bounds;
with_bounds : 'd with_bounds
}
constraint 'd = 'l * 'r
and 'd jkind_const_desc = (Jkind_types.Layout.Const.t, 'd) base_and_axes
constraint 'd = 'l * 'r
and jkind_const_desc_lr = (allowed * allowed) jkind_const_desc
and 'd jkind_desc = (Jkind_types.Sort.t Jkind_types.Layout.t, 'd) base_and_axes
constraint 'd = 'l * 'r
and jkind_desc_packed = Pack_jkind_desc : ('l * 'r) jkind_desc -> jkind_desc_packed
and 'd jkind_quality =
| Best : ('l * disallowed) jkind_quality
| Not_best : ('l * 'r) jkind_quality
and 'd jkind =
{ jkind : 'd jkind_desc;
annotation : Parsetree.jkind_annotation option;
history : jkind_history;
has_warned : bool;
ran_out_of_fuel_during_normalize : bool;
quality : 'd jkind_quality;
}
constraint 'd = 'l * 'r
and jkind_l = (allowed * disallowed) jkind
and jkind_r = (disallowed * allowed) jkind
and jkind_lr = (allowed * allowed) jkind
and jkind_packed = Pack_jkind : ('l * 'r) jkind -> jkind_packed
and jkind_declaration =
{
jkind_manifest : jkind_const_desc_lr option;
jkind_attributes : Parsetree.attributes;
jkind_uid : Shape.Uid.t;
jkind_loc : Location.t
}
module TransientTypeOps = struct
type t = type_expr
let compare t1 t2 = t1.id - t2.id
let hash t = t.id
let equal t1 t2 = t1 == t2
end
module TransientTypeHash = Hashtbl.Make(TransientTypeOps)
module Uid = Shape.Uid
module MethSet = Misc.Stdlib.String.Set
module VarSet = Misc.Stdlib.String.Set
module Meths = Misc.Stdlib.String.Map
module Vars = Misc.Stdlib.String.Map
type value_kind =
Val_reg of Jkind_types.Sort.t
| Val_mut of Mode.Value.Comonadic.lr * Jkind_types.Sort.t
| Val_prim of Primitive.description
| Val_ivar of mutable_flag * string
| Val_self of
class_signature * self_meths * Ident.t Vars.t * string
| Val_anc of class_signature * Ident.t Meths.t * string
and self_meths =
| Self_concrete of Ident.t Meths.t
| Self_virtual of Ident.t Meths.t ref
and class_signature =
{ csig_self: type_expr;
mutable csig_self_row: type_expr;
mutable csig_vars: (mutable_flag * virtual_flag * type_expr) Vars.t;
mutable csig_meths: (method_privacy * virtual_flag * type_expr) Meths.t; }
and method_privacy =
| Mpublic
| Mprivate of field_kind
module Variance = struct
type t = int
type f = May_pos | May_neg | May_weak | Inj | Pos | Neg | Inv
let single = function
| May_pos -> 1
| May_neg -> 2 + 4
| May_weak -> 4
| Inj -> 8
| Pos -> 16 + 8 + 1
| Neg -> 32 + 8 + 4 + 2
| Inv -> 63
let union v1 v2 = v1 lor v2
let inter v1 v2 = v1 land v2
let subset v1 v2 = (v1 land v2 = v1)
let eq (v1 : t) v2 = (v1 = v2)
let set x v = union v (single x)
let set_if b x v = if b then set x v else v
let mem x = subset (single x)
let null = 0
let unknown = 7
let full = single Inv
let covariant = single Pos
let swap f1 f2 v v' =
set_if (mem f2 v) f1 (set_if (mem f1 v) f2 v')
let conjugate v =
let v' = inter v (union (single Inj) (single May_weak)) in
swap Pos Neg v (swap May_pos May_neg v v')
let compose v1 v2 =
if mem Inv v1 && mem Inj v2 then full else
let mp =
mem May_pos v1 && mem May_pos v2 || mem May_neg v1 && mem May_neg v2
and mn =
mem May_pos v1 && mem May_neg v2 || mem May_neg v1 && mem May_pos v2
and mw = mem May_weak v1 && v2 <> null || v1 <> null && mem May_weak v2
and inj = mem Inj v1 && mem Inj v2
and pos = mem Pos v1 && mem Pos v2 || mem Neg v1 && mem Neg v2
and neg = mem Pos v1 && mem Neg v2 || mem Neg v1 && mem Pos v2 in
List.fold_left (fun v (b,f) -> set_if b f v) null
[mp, May_pos; mn, May_neg; mw, May_weak; inj, Inj; pos, Pos; neg, Neg]
let strengthen v =
if mem May_neg v then v else v land (full - single May_weak)
let get_upper v = (mem May_pos v, mem May_neg v)
let get_lower v = (mem Pos v, mem Neg v, mem Inj v)
let unknown_signature ~injective ~arity =
let v = if injective then set Inj unknown else unknown in
Misc.replicate_list v arity
end
module Separability = struct
type t = Ind | Sep | Deepsep
type signature = t list
let eq (m1 : t) m2 = (m1 = m2)
let rank = function
| Ind -> 0
| Sep -> 1
| Deepsep -> 2
let compare m1 m2 = compare (rank m1) (rank m2)
let max m1 m2 = if rank m1 >= rank m2 then m1 else m2
let print ppf = function
| Ind -> Format.fprintf ppf "Ind"
| Sep -> Format.fprintf ppf "Sep"
| Deepsep -> Format.fprintf ppf "Deepsep"
let print_signature ppf modes =
let pp_sep ppf () = Format.fprintf ppf ",@," in
Format.fprintf ppf "@[(%a)@]"
(Format.pp_print_list ~pp_sep print) modes
let default_signature ~arity =
let default_mode = if Config.flat_float_array then Deepsep else Ind in
Misc.replicate_list default_mode arity
end
type type_declaration =
{ type_params: type_expr list;
type_arity: int;
type_kind: type_decl_kind;
type_jkind: jkind_l;
type_ikind: constructor_ikind_entry;
type_private: private_flag;
type_manifest: type_expr option;
type_variance: Variance.t list;
type_separability: Separability.t list;
type_is_newtype: bool;
type_expansion_scope: int;
type_loc: Location.t;
type_attributes: Parsetree.attributes;
type_unboxed_default: bool;
type_uid: Uid.t;
type_unboxed_version : type_declaration option;
}
and type_decl_kind =
(label_declaration, label_declaration, constructor_declaration) type_kind
and unsafe_mode_crossing =
{ unsafe_mod_bounds : Mode.Crossing.t
; unsafe_with_bounds : (allowed * disallowed) with_bounds
}
and ('lbl, 'lbl_flat, 'cstr) type_kind =
Type_abstract of type_origin
| Type_record of 'lbl list * record_representation * unsafe_mode_crossing option
| Type_record_unboxed_product of
'lbl_flat list *
record_unboxed_product_representation *
unsafe_mode_crossing option
| Type_variant of 'cstr list * variant_representation * unsafe_mode_crossing option
| Type_open
and tag = Ordinary of {src_index: int;
runtime_tag: int}
| Extension of Path.t
| Null
and type_origin =
Definition
| Rec_check_regularity
| Existential of string
and mixed_block_element =
| Scannable of Jkind_types.Scannable_axes.t
| Float_boxed
| Float64
| Float32
| Bits8
| Bits16
| Untagged_immediate
| Bits32
| Bits64
| Vec128
| Vec256
| Vec512
| Word
| Product of mixed_product_shape
| Void
and mixed_product_shape = mixed_block_element array
and module_representation = Jkind_types.Sort.t array
and record_representation =
| Record_unboxed
| Record_inlined of tag * constructor_representation * variant_representation
| Record_boxed
| Record_float
| Record_ufloat
| Record_mixed of mixed_product_shape
| Record_dummy of { represent_as_float_array : bool }
and record_unboxed_product_representation =
| Record_unboxed_product
and variant_representation =
| Variant_unboxed
| Variant_boxed of (constructor_representation *
Jkind_types.Sort.Const.t array) array
| Variant_extensible
| Variant_with_null
and constructor_representation =
| Constructor_uniform_value
| Constructor_mixed of mixed_product_shape
and label_declaration =
{
ld_id: Ident.t;
ld_mutable: mutability;
ld_modalities: Mode.Modality.Const.t;
ld_type: type_expr;
ld_sort: Jkind_types.Sort.Const.t;
ld_loc: Location.t;
ld_attributes: Parsetree.attributes;
ld_uid: Uid.t;
}
and constructor_declaration =
{
cd_id: Ident.t;
cd_args: constructor_arguments;
cd_res: type_expr option;
cd_loc: Location.t;
cd_attributes: Parsetree.attributes;
cd_uid: Uid.t;
}
and constructor_argument =
{
ca_modalities: Mode.Modality.Const.t;
ca_type: type_expr;
ca_sort: Jkind_types.Sort.Const.t;
ca_loc: Location.t;
}
and constructor_arguments =
| Cstr_tuple of constructor_argument list
| Cstr_record of label_declaration list
type extension_constructor =
{ ext_type_path: Path.t;
ext_type_params: type_expr list;
ext_args: constructor_arguments;
ext_shape: constructor_representation;
ext_constant: bool;
ext_ret_type: type_expr option;
ext_private: private_flag;
ext_loc: Location.t;
ext_attributes: Parsetree.attributes;
ext_uid: Uid.t;
}
and type_transparence =
Type_public
| Type_new
| Type_private
let tys_of_constr_args = function
| Cstr_tuple tl -> List.map (fun ca -> ca.ca_type) tl
| Cstr_record lbls -> List.map (fun l -> l.ld_type) lbls
type class_type =
Cty_constr of Path.t * type_expr list * class_type
| Cty_signature of class_signature
| Cty_arrow of arg_label * type_expr * class_type
type class_declaration =
{ cty_params: type_expr list;
mutable cty_type: class_type;
cty_path: Path.t;
cty_new: type_expr option;
cty_variance: Variance.t list;
cty_loc: Location.t;
cty_attributes: Parsetree.attributes;
cty_uid: Uid.t;
}
type class_type_declaration =
{ clty_params: type_expr list;
clty_type: class_type;
clty_path: Path.t;
clty_hash_type: type_declaration;
clty_variance: Variance.t list;
clty_loc: Location.t;
clty_attributes: Parsetree.attributes;
clty_uid: Uid.t;
}
type visibility =
| Exported
| Hidden
type rec_status =
Trec_not
| Trec_first
| Trec_next
type ext_status =
Text_first
| Text_next
| Text_exception
type module_presence =
| Mp_present
| Mp_absent
module Aliasability = struct
type t = Not_aliasable | Aliasable
let aliasable b = if b then Aliasable else Not_aliasable
let is_aliasable = function
| Aliasable -> true
| Not_aliasable -> false
end
module type Wrap = sig
type 'a t
end
module Lpoly = struct
type state =
| Pending of Location.t
| Determined of Jkind_types.Sort.var list
type t = state ref
let get_exn t = match !t with
| Pending _ -> Misc.fatal_error "layout is pending generalization"
| Determined l -> l
let is_empty_exn t = List.is_empty @@ get_exn t
let determined l = ref (Determined l)
let pending ~loc = ref (Pending loc)
let generalize ~on_determined ~on_to_generalize t =
match !t with
| Pending loc -> t := Determined (on_to_generalize loc)
| Determined _ -> on_determined ()
end
module type Wrapped = sig
type 'a wrapped
type value_description =
{ val_type: type_expr wrapped;
val_modalities : Mode.Modality.t;
val_kind: value_kind;
val_lpoly: Lpoly.t wrapped;
val_loc: Location.t;
val_zero_alloc: Zero_alloc.t;
val_attributes: Parsetree.attributes;
val_uid: Uid.t;
}
type module_type =
Mty_ident of Path.t
| Mty_signature of signature
| Mty_functor of functor_parameter * module_type * Mode.Alloc.lr
| Mty_alias of Path.t
| Mty_strengthen of module_type * Path.t * Aliasability.t
| Mty_for_hole
and functor_parameter =
| Unit
| Named of Ident.t option * module_type * Mode.Alloc.lr
and signature = signature_item list wrapped
and persistent_signature = signature * Mode.Staticity.Const.t
and signature_item =
Sig_value of Ident.t * value_description * visibility
| Sig_type of Ident.t * type_declaration * rec_status * visibility
| Sig_typext of Ident.t * extension_constructor * ext_status * visibility
| Sig_module of
Ident.t * module_presence * module_declaration * rec_status * visibility
| Sig_modtype of Ident.t * modtype_declaration * visibility
| Sig_class of Ident.t * class_declaration * rec_status * visibility
| Sig_class_type of Ident.t * class_type_declaration * rec_status * visibility
| Sig_jkind of Ident.t * jkind_declaration * visibility
and module_declaration =
{
md_type: module_type;
md_modalities: Mode.Modality.t;
md_attributes: Parsetree.attributes;
md_loc: Location.t;
md_uid: Uid.t;
}
and modtype_declaration =
{
mtd_type: module_type option;
mtd_attributes: Parsetree.attributes;
mtd_loc: Location.t;
mtd_uid: Uid.t;
}
val sort_of_signature_item :
signature_item -> Jkind_types.Sort.t option
end
module Make_wrapped(Wrap : Wrap) = struct
module rec M : Wrapped with type 'a wrapped = 'a Wrap.t = M
include M
let sort_of_signature_item = function
| Sig_value(_, decl, _) ->
begin match decl.val_kind with
| Val_reg sort -> Some sort
| Val_ivar _ ->
Some Jkind_types.Sort.(of_const Const.for_instance_var)
| Val_self _ | Val_anc _ ->
Some Jkind_types.Sort.(of_const Const.for_object)
| Val_prim _ -> None
| Val_mut _ ->
Misc.fatal_error "Mutable variable found at the structure level"
end
| Sig_typext _ ->
Some Jkind_types.Sort.(of_const Const.for_type_extension)
| Sig_module(_, pres, _, _, _) ->
begin match pres with
| Mp_present ->
Some Jkind_types.Sort.(of_const Const.for_module)
| Mp_absent -> None
end
| Sig_class _ ->
Some Jkind_types.Sort.(of_const Const.for_class)
| Sig_type _ | Sig_modtype _ | Sig_class_type _ | Sig_jkind _ -> None
end
module Map_wrapped(From : Wrapped)(To : Wrapped) = struct
open From
type mapper =
{
map_signature: mapper -> signature -> To.signature;
map_type_expr: mapper -> type_expr wrapped -> type_expr To.wrapped;
map_value_description:
mapper -> value_description -> To.value_description;
}
let signature m = m.map_signature m
let rec module_type m = function
| Mty_ident p -> To.Mty_ident p
| Mty_alias p -> To.Mty_alias p
| Mty_functor (parm,mty,mm) ->
To.Mty_functor (functor_parameter m parm, module_type m mty, mm)
| Mty_signature sg -> To.Mty_signature (signature m sg)
| Mty_for_hole -> To.Mty_for_hole
| Mty_strengthen (mty,p,aliasable) ->
To.Mty_strengthen (module_type m mty, p, aliasable)
and functor_parameter m = function
| Unit -> To.Unit
| Named (id,mty,mm) -> To.Named (id, module_type m mty,mm)
let value_description m vd = m.map_value_description m vd
let module_declaration m {md_type; md_modalities; md_attributes;
md_loc; md_uid} =
To.{
md_type = module_type m md_type;
md_modalities;
md_attributes;
md_loc;
md_uid;
}
let modtype_declaration m {mtd_type; mtd_attributes; mtd_loc; mtd_uid} =
To.{
mtd_type = Option.map (module_type m) mtd_type;
mtd_attributes;
mtd_loc;
mtd_uid;
}
let signature_item m = function
| Sig_value (id,vd,vis) ->
To.Sig_value (id, value_description m vd, vis)
| Sig_type (id,td,rs,vis) ->
To.Sig_type (id,td,rs,vis)
| Sig_module (id,pres,md,rs,vis) ->
To.Sig_module (id, pres, module_declaration m md, rs, vis)
| Sig_modtype (id,mtd,vis) ->
To.Sig_modtype (id, modtype_declaration m mtd, vis)
| Sig_typext (id,ec,es,vis) ->
To.Sig_typext (id,ec,es,vis)
| Sig_class (id,cd,rs,vis) ->
To.Sig_class (id,cd,rs,vis)
| Sig_class_type (id,ctd,rs,vis) ->
To.Sig_class_type (id,ctd,rs,vis)
| Sig_jkind (id,jkd,vis) ->
To.Sig_jkind (id,jkd,vis)
end
include Make_wrapped(struct type 'a t = 'a end)
type constructor_description =
{ cstr_name: string;
cstr_res: type_expr;
cstr_existentials: type_expr list;
cstr_args: constructor_argument list;
cstr_arity: int;
cstr_tag: tag;
cstr_repr: variant_representation;
cstr_shape: constructor_representation;
cstr_constant: bool;
cstr_consts: int;
cstr_nonconsts: int;
cstr_generalized: bool;
cstr_private: private_flag;
cstr_loc: Location.t;
cstr_attributes: Parsetree.attributes;
cstr_inlined: type_declaration option;
cstr_uid: Uid.t;
}
let equal_tag t1 t2 =
match (t1, t2) with
| Ordinary {src_index=i1}, Ordinary {src_index=i2} ->
i2 = i1
| Extension path1, Extension path2 -> Path.same path1 path2
| Null, Null -> true
| (Ordinary _ | Extension _ | Null), _ -> false
let compare_tag t1 t2 =
match (t1, t2) with
| Ordinary {src_index=i1}, Ordinary {src_index=i2} ->
Int.compare i1 i2
| Extension path1, Extension path2 -> Path.compare path1 path2
| Null, Null -> 0
| Ordinary _, (Extension _ | Null) -> -1
| (Extension _ | Null), Ordinary _ -> 1
| Extension _, Null -> -1
| Null, Extension _ -> 1
let rec equal_mixed_block_element_up_to_scannable_axes e1 e2 =
match e1, e2 with
| Scannable _, Scannable _
| Float64, Float64 | Float32, Float32 | Float_boxed, Float_boxed
| Word, Word | Untagged_immediate, Untagged_immediate
| Bits8, Bits8 | Bits16, Bits16
| Bits32, Bits32 | Bits64, Bits64
| Vec128, Vec128 | Vec256, Vec256 | Vec512, Vec512
| Void, Void
-> true
| Product es1, Product es2
-> Misc.Stdlib.Array.equal
equal_mixed_block_element_up_to_scannable_axes es1 es2
| ( Scannable _ | Float64 | Float32 | Float_boxed | Word | Untagged_immediate
| Bits8 | Bits16 | Bits32 | Bits64 | Vec128 | Vec256 | Vec512
| Product _ | Void ), _
-> false
let rec compare_mixed_block_element e1 e2 =
match e1, e2 with
| Scannable sa1, Scannable sa2 -> (
match Jkind_types.Scannable_axes.less_or_equal sa1 sa2 with
| Less -> -1
| Equal -> 0
| Not_le -> 1)
| Float_boxed, Float_boxed
| Float64, Float64 | Float32, Float32
| Word, Word | Untagged_immediate, Untagged_immediate
| Bits8, Bits8 | Bits16, Bits16 | Bits32, Bits32 | Bits64, Bits64
| Vec128, Vec128 | Vec256, Vec256 | Vec512, Vec512
| Void, Void
-> 0
| Product es1, Product es2
-> Misc.Stdlib.Array.compare compare_mixed_block_element es1 es2
| Scannable _, _ -> -1
| _, Scannable _ -> 1
| Float_boxed, _ -> -1
| _, Float_boxed -> 1
| Float64, _ -> -1
| _, Float64 -> 1
| Float32, _ -> -1
| _, Float32 -> 1
| Word, _ -> -1
| _, Word -> 1
| Untagged_immediate, _ -> -1
| _, Untagged_immediate -> 1
| Bits8, _ -> -1
| _, Bits8 -> 1
| Bits16, _ -> -1
| _, Bits16 -> 1
| Bits32, _ -> -1
| _, Bits32 -> 1
| Bits64, _ -> -1
| _, Bits64 -> 1
| Vec128, _ -> -1
| _, Vec128 -> 1
| Vec256, _ -> -1
| _, Vec256 -> 1
| Vec512, _ -> -1
| _, Vec512 -> 1
| Void, _ -> -1
| _, Void -> 1
let equal_mixed_product_shape_up_to_scannable_axes r1 r2 = r1 == r2 ||
Misc.Stdlib.Array.equal equal_mixed_block_element_up_to_scannable_axes r1 r2
let equal_constructor_representation_up_to_scannable_axes r1 r2 = r1 == r2 ||
match r1, r2 with
| Constructor_uniform_value, Constructor_uniform_value -> true
| Constructor_mixed mx1, Constructor_mixed mx2 ->
equal_mixed_product_shape_up_to_scannable_axes mx1 mx2
| (Constructor_mixed _ | Constructor_uniform_value), _ -> false
let equal_variant_representation_up_to_scannable_axes r1 r2 = r1 == r2 ||
match r1, r2 with
| Variant_unboxed, Variant_unboxed ->
true
| Variant_boxed cstrs_and_sorts1, Variant_boxed cstrs_and_sorts2 ->
Misc.Stdlib.Array.equal (fun (cstr1, sorts1) (cstr2, sorts2) ->
equal_constructor_representation_up_to_scannable_axes cstr1 cstr2
&& Misc.Stdlib.Array.equal Jkind_types.Sort.Const.equal
sorts1 sorts2)
cstrs_and_sorts1
cstrs_and_sorts2
| Variant_extensible, Variant_extensible ->
true
| Variant_with_null, Variant_with_null -> true
| (Variant_unboxed | Variant_boxed _ | Variant_extensible | Variant_with_null), _ ->
false
let equal_record_representation_up_to_scannable_axes r1 r2 = match r1, r2 with
| Record_unboxed, Record_unboxed ->
true
| Record_inlined (tag1, cr1, vr1), Record_inlined (tag2, cr2, vr2) ->
ignore (cr1 : constructor_representation);
ignore (cr2 : constructor_representation);
equal_tag tag1 tag2 &&
equal_variant_representation_up_to_scannable_axes vr1 vr2
| Record_boxed, Record_boxed ->
true
| Record_float, Record_float ->
true
| Record_ufloat, Record_ufloat ->
true
| Record_mixed mx1, Record_mixed mx2 ->
equal_mixed_product_shape_up_to_scannable_axes mx1 mx2
| Record_dummy { represent_as_float_array = a },
Record_dummy { represent_as_float_array = b } ->
Bool.equal a b
| (Record_unboxed | Record_inlined _ | Record_boxed | Record_float
| Record_ufloat | Record_mixed _ | Record_dummy _), _ ->
false
let equal_record_unboxed_product_representation_up_to_scannable_axes r1 r2 =
match r1, r2 with
| Record_unboxed_product, Record_unboxed_product -> true
let may_equal_constr c1 c2 =
c1.cstr_arity = c2.cstr_arity
&& (match c1.cstr_tag,c2.cstr_tag with
| Extension _, Extension _ ->
true
| tag1, tag2 ->
equal_tag tag1 tag2)
type 'a gen_label_description =
{ lbl_name: string;
lbl_res: type_expr;
lbl_arg: type_expr;
lbl_mut: mutability;
lbl_modalities: Mode.Modality.Const.t;
lbl_sort: Jkind_types.Sort.Const.t;
lbl_pos: int;
lbl_all: 'a gen_label_description array;
lbl_repres: 'a;
lbl_private: private_flag;
lbl_loc: Location.t;
lbl_attributes: Parsetree.attributes;
lbl_uid: Uid.t;
}
type label_description = record_representation gen_label_description
type unboxed_label_description =
record_unboxed_product_representation gen_label_description
type _ record_form =
| Legacy : record_representation record_form
| Unboxed_product : record_unboxed_product_representation record_form
type record_form_packed =
| P : _ record_form -> record_form_packed
let record_form_to_string (type rep) (record_form : rep record_form) =
match record_form with
| Legacy -> "record"
| Unboxed_product -> "unboxed record"
let rec mixed_block_element_of_const_sort (sort : Jkind_types.Sort.Const.t) =
match sort with
| Base Scannable -> Scannable Jkind_types.Scannable_axes.max
| Base Bits8 -> Bits8
| Base Bits16 -> Bits16
| Base Bits32 -> Bits32
| Base Bits64 -> Bits64
| Base Float32 -> Float32
| Base Float64 -> Float64
| Base Untagged_immediate -> Untagged_immediate
| Base Vec128 -> Vec128
| Base Vec256 -> Vec256
| Base Vec512 -> Vec512
| Base Word -> Word
| Product sorts ->
Product (Array.map mixed_block_element_of_const_sort (Array.of_list sorts))
| Base Void -> Void
| Univar _ -> Misc.fatal_error "mixed_block_element_of_const_sort: Univar"
| Genvar _ -> Misc.fatal_error "mixed_block_element_of_const_sort: Genvar"
let find_unboxed_type decl =
match decl.type_kind with
Type_record ([{ld_type = arg; ld_modalities = ms; _}], Record_unboxed, _)
| Type_record
([{ld_type = arg; ld_modalities = ms; _ }], Record_inlined (_, _, Variant_unboxed), _)
| Type_record_unboxed_product
([{ld_type = arg; ld_modalities = ms; _ }], Record_unboxed_product, _)
| Type_variant ([{cd_args = Cstr_tuple [{ca_type = arg; ca_modalities = ms; _}]; _}], Variant_unboxed, _)
| Type_variant ([{cd_args = Cstr_record [{ld_type = arg; ld_modalities = ms; _}]; _}], Variant_unboxed, _) ->
Some (arg, ms)
| Type_record (_, ( Record_inlined _ | Record_unboxed
| Record_boxed | Record_float | Record_ufloat
| Record_mixed _ | Record_dummy _), _)
| Type_record_unboxed_product (_, Record_unboxed_product, _)
| Type_variant (_, ( Variant_boxed _ | Variant_unboxed
| Variant_extensible | Variant_with_null), _)
| Type_abstract _ | Type_open ->
None
let item_visibility = function
| Sig_value (_, _, vis)
| Sig_type (_, _, _, vis)
| Sig_typext (_, _, _, vis)
| Sig_module (_, _, _, _, vis)
| Sig_modtype (_, _, vis)
| Sig_class (_, _, _, vis)
| Sig_class_type (_, _, _, vis)
| Sig_jkind (_, _, vis) -> vis
let rec bound_value_identifiers = function
[] -> []
| Sig_value(id, {val_kind = Val_reg _}, _) :: rem ->
id :: bound_value_identifiers rem
| Sig_typext(id, _, _, _) :: rem -> id :: bound_value_identifiers rem
| Sig_module(id, Mp_present, _, _, _) :: rem ->
id :: bound_value_identifiers rem
| Sig_class(id, _, _, _) :: rem -> id :: bound_value_identifiers rem
| _ :: rem -> bound_value_identifiers rem
let signature_item_id = function
| Sig_value (id, _, _)
| Sig_type (id, _, _, _)
| Sig_typext (id, _, _, _)
| Sig_module (id, _, _, _, _)
| Sig_modtype (id, _, _)
| Sig_class (id, _, _, _)
| Sig_class_type (id, _, _, _)
| Sig_jkind (id, _, _)
-> id
let signature_item_representation sg =
match sort_of_signature_item sg with
| None -> None
| Some sort -> Some (signature_item_id sg, sort)
let bound_value_identifiers_and_sorts sigs =
List.filter_map signature_item_representation sigs
let rec mixed_block_element_to_string = function
| Scannable _ -> "Scannable"
| Float_boxed -> "Float_boxed"
| Float32 -> "Float32"
| Float64 -> "Float64"
| Bits8 -> "Bits8"
| Bits16 -> "Bits16"
| Bits32 -> "Bits32"
| Bits64 -> "Bits64"
| Vec128 -> "Vec128"
| Vec256 -> "Vec256"
| Vec512 -> "Vec512"
| Word -> "Word"
| Untagged_immediate -> "Untagged_immediate"
| Product es ->
"Product ["
^ (String.concat ", "
(Array.to_list (Array.map mixed_block_element_to_string es)))
^ "]"
| Void -> "Void"
let mixed_block_element_to_lowercase_string = function
| Scannable _ -> "scannable"
| Float_boxed -> "float"
| Float32 -> "float32"
| Float64 -> "float64"
| Bits8 -> "bits8"
| Bits16 -> "bits16"
| Bits32 -> "bits32"
| Bits64 -> "bits64"
| Vec128 -> "vec128"
| Vec256 -> "vec256"
| Vec512 -> "vec512"
| Word -> "word"
| Untagged_immediate -> "untagged_immediate"
| Product es ->
"product ["
^ (String.concat ", "
(Array.to_list (Array.map mixed_block_element_to_string es)))
^ "]"
| Void -> "void"
type change =
Ctype : type_expr * type_desc -> change
| Ccompress : type_expr * type_desc * type_desc -> change
| Clevel : type_expr * int -> change
| Cscope : type_expr * int -> change
| Cname :
(Path.t * type_expr list) option ref * (Path.t * type_expr list) option -> change
| Crow : [`none|`some] row_field_gen ref -> change
| Ckind : [`var] field_kind_gen -> change
| Ccommu : [`var] commutable_gen -> change
| Cuniv : type_expr option ref * type_expr option -> change
| Cmodes : Mode.changes -> change
| Cfun of (unit -> unit)
| Csort : Jkind_types.Sort.change -> change
| Czero_alloc : Zero_alloc.change -> change
type changes =
Change of change * changes ref
| Unchanged
| Invalid
open Local_store
let trail = s_table ref Unchanged
let log_change ch =
let r' = ref Unchanged in
!trail := Change (ch, r');
trail := r'
let () =
Mode.set_append_changes (fun changes -> log_change (Cmodes !changes));
Jkind_types.Sort.set_change_log (fun change -> log_change (Csort change));
Zero_alloc.set_change_log (fun change -> log_change (Czero_alloc change))
type field_kind_view =
Fprivate
| Fpublic
| Fabsent
let rec field_kind_internal_repr : field_kind -> field_kind = function
| FKvar {field_kind = FKvar _ | FKpublic | FKabsent as fk} ->
field_kind_internal_repr fk
| kind -> kind
let field_kind_repr fk =
match field_kind_internal_repr fk with
| FKvar _ -> Fprivate
| FKpublic -> Fpublic
| FKabsent -> Fabsent
let field_public = FKpublic
let field_absent = FKabsent
let field_private () = FKvar {field_kind=FKprivate}
let rec is_commu_ok : type a. a commutable_gen -> bool = function
| Cvar {commu} -> is_commu_ok commu
| Cunknown -> false
| Cok -> true
let commu_ok = Cok
let commu_var () = Cvar {commu=Cunknown}
let rec repr_link (t : type_expr) d : type_expr -> type_expr =
function
{desc = Tlink t' as d'} ->
repr_link t d' t'
| {desc = Tfield (_, k, _, t') as d'}
when field_kind_internal_repr k = FKabsent ->
repr_link t d' t'
| t' ->
log_change (Ccompress (t, t.desc, d));
t.desc <- d;
t'
let repr_link1 t = function
{desc = Tlink t' as d'} ->
repr_link t d' t'
| {desc = Tfield (_, k, _, t') as d'}
when field_kind_internal_repr k = FKabsent ->
repr_link t d' t'
| t' -> t'
let repr t =
match t.desc with
Tlink t' ->
repr_link1 t t'
| Tfield (_, k, _, t') when field_kind_internal_repr k = FKabsent ->
repr_link1 t t'
| _ -> t
let scope_mask = (1 lsl 27) - 1
let marks_mask = (-1) lxor scope_mask
let () = assert (Ident.highest_scope land marks_mask = 0)
type type_mark =
| Mark of {mark: int; mutable marked: type_expr list}
| Hash of {visited: unit TransientTypeHash.t}
let type_marks =
List.init (Sys.int_size - 27) (fun x -> 1 lsl (x + 27))
let available_marks = Local_store.s_ref type_marks
let with_type_mark f =
match !available_marks with
| mark :: rem as old ->
available_marks := rem;
let mk = Mark {mark; marked = []} in
Misc.try_finally (fun () -> f mk) ~always: begin fun () ->
available_marks := old;
match mk with
| Mark {marked} ->
List.iter
(fun ty -> ty.scope <- ty.scope land ((-1) lxor mark))
marked
| Hash _ -> ()
end
| [] ->
f (Hash {visited = TransientTypeHash.create 1})
let get_desc t = (repr t).desc
let get_level t = (repr t).level
let get_scope t = (repr t).scope land scope_mask
let get_id t = (repr t).id
let not_marked_node mark t =
match mark with
| Mark {mark} -> (repr t).scope land mark = 0
| Hash {visited} -> not (TransientTypeHash.mem visited (repr t))
module Transient_expr = struct
let create desc ~level ~scope ~id = {desc; level; scope; id}
let set_desc ty d = ty.desc <- d
let set_stub_desc ty d =
(match ty.desc with
| Tvar {name = None; _} -> ()
| _ -> assert false);
ty.desc <- d
let set_level ty lv = ty.level <- lv
let set_var_jkind ty jkind' =
match ty.desc with
| Tvar { name; _ } ->
set_desc ty (Tvar { name; jkind = jkind' })
| _ -> Misc.fatal_error "set_var_jkind called on non-var"
let get_scope ty = ty.scope land scope_mask
let get_marks ty = ty.scope lsr 27
let set_scope ty sc =
if (sc land marks_mask <> 0) then
invalid_arg "Types.Transient_expr.set_scope";
ty.scope <- (ty.scope land marks_mask) lor sc
let try_mark_node mark ty =
match mark with
| Mark ({mark} as mk) ->
(ty.scope land mark = 0) &&
(ty.scope <- ty.scope lor mark; mk.marked <- ty :: mk.marked; true)
| Hash {visited} ->
not (TransientTypeHash.mem visited ty) &&
(TransientTypeHash.add visited ty (); true)
let coerce ty = ty
let repr = repr
let type_expr ty = ty
end
let try_mark_node mark t = Transient_expr.try_mark_node mark (repr t)
let eq_type t1 t2 = t1 == t2 || repr t1 == repr t2
let compare_type t1 t2 = compare (get_id t1) (get_id t2)
let best_effort_compare_type_expr te1 te2 =
let max_depth = 50 in
let rank_by_id ty =
-ty.id
in
let rec aux depth te1 te2 =
if te1 == te2 || repr te1 == repr te2 then 0
else if depth >= max_depth
then (rank_by_id te1) - (rank_by_id te2)
else
let rank ty =
match get_desc ty with
| Tvar _
| Tunivar _
| Tobject (_, _)
| Tfield (_, _, _, _)
| Tnil
| Tvariant _
| Tpackage (_, _)
| Tarrow (_, _, _, _)
| Tquote _
| Tsplice _
| Tquote_eval _
| Tsubst (_, _)
-> rank_by_id ty
| Ttuple _ -> 2
| Tunboxed_tuple _ -> 3
| Tconstr (_, _, _) -> 5
| Tpoly (_, _) -> 6
| Tof_kind _ -> 7
| Trepr (_, _) -> 8
| Tlink _ -> Misc.fatal_error "Tlink encountered in With_bounds_types"
in
match get_desc te1, get_desc te2 with
| Ttuple elts1, Ttuple elts2
| Tunboxed_tuple elts1, Tunboxed_tuple elts2 ->
List.compare
(fun (l1, te1) (l2, te2) ->
let l = Option.compare String.compare l1 l2 in
if l = 0 then aux (depth + 1) te1 te2 else l
)
elts1
elts2
| Tconstr (p1, args1, _), Tconstr (p2, args2, _) ->
let p = Path.compare p1 p2 in
if p = 0
then List.compare (aux (depth + 1)) args1 args2
else p
| Tpoly (t1, ts1), Tpoly (t2, ts2) ->
List.compare (aux (depth + 1)) (t1 :: ts1) (t2 :: ts2)
| Trepr (t1, sort_vars1), Trepr (t2, sort_vars2) ->
(match List.combine sort_vars1 sort_vars2 with
| exception Invalid_argument _ ->
Int.compare (List.length sort_vars1) (List.length sort_vars2)
| pairs ->
Jkind_types.Sort.enter_repr pairs (fun () -> aux (depth + 1) t1 t2))
| _, _ -> rank te1 - rank te2
in
aux 0 te1 te2
module With_bounds_types : sig
type t = with_bounds_types
type info := With_bounds_type_info.t
val empty : t
val is_empty : t -> bool
val to_seq : t -> (type_expr * info) Seq.t
val of_list : (type_expr * info) list -> t
val of_seq : (type_expr * info) Seq.t -> t
val singleton : type_expr -> info -> t
val map : (info -> info) -> t -> t
val map_with_key : (type_expr -> info -> type_expr * info) -> t -> t
val merge
: (type_expr -> info option -> info option -> info option) ->
t -> t -> t
val update : type_expr -> (info option -> info option) -> t -> t
val find_opt : type_expr -> t -> info option
val for_all : (type_expr -> info -> bool) -> t -> bool
val exists : (type_expr -> info -> bool) -> t -> bool
end = struct
module M = Map.Make(struct
type t = type_expr
let compare = best_effort_compare_type_expr
end)
include M
type map = With_bounds_type_info.t M.t
type t = with_bounds_types
let of_map : map -> with_bounds_types = Obj.magic
let to_map : with_bounds_types -> map = Obj.magic
let empty = empty |> of_map
let is_empty t = t |> to_map |> is_empty
let to_seq t = t |> to_map |> to_seq
let of_seq s = of_seq s |> of_map
let of_list l = l |> List.to_seq |> of_seq
let singleton ty i = add ty i (to_map empty) |> of_map
let map f t = map f (to_map t) |> of_map
let merge f t1 t2 = merge f (to_map t1) (to_map t2) |> of_map
let update te f t = update te f (to_map t) |> of_map
let find_opt te t = find_opt te (to_map t)
let for_all f t = for_all f (to_map t)
let exists f t = exists f (to_map t)
let map_with_key f t =
fold (fun key value acc ->
let key, value = f key value in
M.add key value acc) (to_map t) M.empty |> of_map
end
let equal_unsafe_mode_crossing
~type_equal
{ unsafe_mod_bounds = mc1; unsafe_with_bounds = wb2 }
umc2 =
Misc.Le_result.equal ~le:Mode.Crossing.le mc1 umc2.unsafe_mod_bounds
&& (match wb2, umc2.unsafe_with_bounds with
| No_with_bounds, No_with_bounds -> true
| No_with_bounds, With_bounds _ | With_bounds _, No_with_bounds -> false
| With_bounds wb1, With_bounds wb2 ->
With_bounds_types.for_all
(fun ty1 _info ->
With_bounds_types.exists
(fun ty2 _info -> type_equal ty1 ty2)
wb2)
wb1
&& With_bounds_types.for_all
(fun ty2 _info ->
With_bounds_types.exists
(fun ty1 _info -> type_equal ty1 ty2)
wb1)
wb2)
let create_row ~fields ~more ~closed ~fixed ~name =
{ row_fields=fields; row_more=more;
row_closed=closed; row_fixed=fixed; row_name=name }
let rec row_fields row =
match get_desc row.row_more with
| Tvariant row' ->
row.row_fields @ row_fields row'
| _ ->
row.row_fields
let rec row_repr_no_fields row =
match get_desc row.row_more with
| Tvariant row' -> row_repr_no_fields row'
| _ -> row
let row_more row = (row_repr_no_fields row).row_more
let row_closed row = (row_repr_no_fields row).row_closed
let row_fixed row = (row_repr_no_fields row).row_fixed
let row_name row = (row_repr_no_fields row).row_name
let rec get_row_field tag row =
let rec find = function
| (tag',f) :: fields ->
if tag = tag' then f else find fields
| [] ->
match get_desc row.row_more with
| Tvariant row' -> get_row_field tag row'
| _ -> RFabsent
in find row.row_fields
let set_row_name row row_name =
let row_fields = row_fields row in
let row = row_repr_no_fields row in
{row with row_fields; row_name}
type row_desc_repr =
Row of { fields: (label * row_field) list;
more:type_expr;
closed:bool;
fixed:fixed_explanation option;
name:(Path.t * type_expr list) option }
let row_repr row =
let fields = row_fields row in
let row = row_repr_no_fields row in
Row { fields;
more = row.row_more;
closed = row.row_closed;
fixed = row.row_fixed;
name = row.row_name }
type row_field_view =
Rpresent of type_expr option
| Reither of bool * type_expr list * bool
| Rabsent
let rec row_field_repr_aux tl : row_field -> row_field = function
| RFeither ({ext = {contents = RFnone}} as r) ->
RFeither {r with arg_type = tl@r.arg_type}
| RFeither {arg_type;
ext = {contents = RFeither _ | RFpresent _ | RFabsent as rf}} ->
row_field_repr_aux (tl@arg_type) rf
| RFpresent (Some _) when tl <> [] ->
RFpresent (Some (List.hd tl))
| RFpresent _ as rf -> rf
| RFabsent -> RFabsent
let row_field_repr fi =
match row_field_repr_aux [] fi with
| RFeither {no_arg; arg_type; matched} -> Reither (no_arg, arg_type, matched)
| RFpresent t -> Rpresent t
| RFabsent -> Rabsent
let rec row_field_ext (fi : row_field) =
match fi with
| RFeither {ext = {contents = RFnone} as ext} -> ext
| RFeither {ext = {contents = RFeither _ | RFpresent _ | RFabsent as rf}} ->
row_field_ext rf
| _ -> Misc.fatal_error "Types.row_field_ext "
let rf_present oty = RFpresent oty
let rf_absent = RFabsent
let rf_either ?use_ext_of ~no_arg arg_type ~matched =
let ext =
match use_ext_of with
Some rf -> row_field_ext rf
| None -> ref RFnone
in
RFeither {no_arg; arg_type; matched; ext}
let rf_either_of = function
| None ->
RFeither {no_arg=true; arg_type=[]; matched=false; ext=ref RFnone}
| Some ty ->
RFeither {no_arg=false; arg_type=[ty]; matched=false; ext=ref RFnone}
let eq_row_field_ext rf1 rf2 =
row_field_ext rf1 == row_field_ext rf2
let changed_row_field_exts l f =
let exts = List.map row_field_ext l in
f ();
List.exists (fun r -> !r <> RFnone) exts
let match_row_field ~present ~absent ~either (f : row_field) =
match f with
| RFabsent -> absent ()
| RFpresent t -> present t
| RFeither {no_arg; arg_type; matched; ext} ->
let e : row_field option =
match !ext with
| RFnone -> None
| RFeither _ | RFpresent _ | RFabsent as e -> Some e
in
either no_arg arg_type matched e
let new_id = Local_store.s_ref (-1)
let create_expr = Transient_expr.create
let newty3 ~level ~scope desc =
incr new_id;
create_expr desc ~level ~scope ~id:!new_id
let newty2 ~level desc =
newty3 ~level ~scope:Ident.lowest_scope desc
let undo_change = function
Ctype (ty, desc) -> Transient_expr.set_desc ty desc
| Ccompress (ty, desc, _) -> Transient_expr.set_desc ty desc
| Clevel (ty, level) -> Transient_expr.set_level ty level
| Cscope (ty, scope) -> Transient_expr.set_scope ty scope
| Cname (r, v) -> r := v
| Crow r -> r := RFnone
| Ckind (FKvar r) -> r.field_kind <- FKprivate
| Ccommu (Cvar r) -> r.commu <- Cunknown
| Cuniv (r, v) -> r := v
| Cmodes c -> Mode.undo_changes c
| Cfun f -> f ()
| Csort change -> Jkind_types.Sort.undo_change change
| Czero_alloc c -> Zero_alloc.undo_change c
type snapshot = changes ref * int
let last_snapshot = Local_store.s_ref 0
let linked_variables = s_ref 0
let log_type ty =
if ty.id <= !last_snapshot then log_change (Ctype (ty, ty.desc))
let link_type ty ty' =
let ty = repr ty in
let ty' = repr ty' in
if ty == ty' then () else begin
log_type ty;
let desc = ty.desc in
(match desc with
| Tvar _ -> incr linked_variables
| _ -> ());
Transient_expr.set_desc ty (Tlink ty');
match desc, ty'.desc with
Tvar { name }, Tvar { name = name'; jkind = jkind' } ->
begin match name, name' with
| Some _, None ->
log_type ty';
Transient_expr.set_desc ty' (Tvar { name; jkind = jkind' })
| None, Some _ -> ()
| Some _, Some _ ->
if ty.level < ty'.level then begin
log_type ty';
Transient_expr.set_desc ty' (Tvar { name; jkind = jkind' })
end
| None, None -> ()
end
| _ -> ()
end
let set_type_desc ty td =
let ty = repr ty in
if td != ty.desc then begin
log_type ty;
Transient_expr.set_desc ty td
end
let set_level ty level =
let ty = repr ty in
if level <> ty.level then begin
if ty.id <= !last_snapshot then log_change (Clevel (ty, ty.level));
Transient_expr.set_level ty level
end
let set_scope ty scope =
let ty = repr ty in
let prev_scope = ty.scope land scope_mask in
if scope <> prev_scope then begin
if ty.id <= !last_snapshot then log_change (Cscope (ty, prev_scope));
Transient_expr.set_scope ty scope
end
let set_var_jkind ty jkind =
let ty = repr ty in
log_type ty;
Transient_expr.set_var_jkind ty jkind
let set_univar rty ty =
log_change (Cuniv (rty, !rty)); rty := Some ty
let set_name nm v =
log_change (Cname (nm, !nm)); nm := v
let rec link_row_field_ext ~(inside : row_field) (v : row_field) =
match inside with
| RFeither {ext = {contents = RFnone} as e} ->
let RFeither _ | RFpresent _ | RFabsent as v = v in
log_change (Crow e); e := v
| RFeither {ext = {contents = RFeither _ | RFpresent _ | RFabsent as rf}} ->
link_row_field_ext ~inside:rf v
| _ -> invalid_arg "Types.link_row_field_ext"
let rec link_kind ~(inside : field_kind) (k : field_kind) =
match inside with
| FKvar ({field_kind = FKprivate} as rk) as inside ->
let FKvar _ | FKpublic | FKabsent as k = field_kind_internal_repr k in
if k != inside then begin
log_change (Ckind inside);
rk.field_kind <- k
end
| FKvar {field_kind = FKvar _ | FKpublic | FKabsent as inside} ->
link_kind ~inside k
| _ -> invalid_arg "Types.link_kind"
let rec commu_repr : commutable -> commutable = function
| Cvar {commu = Cvar _ | Cok as commu} -> commu_repr commu
| c -> c
let rec link_commu ~(inside : commutable) (c : commutable) =
match inside with
| Cvar ({commu = Cunknown} as rc) as inside ->
let Cvar _ | Cok as c = commu_repr c in
if c != inside then begin
log_change (Ccommu inside);
rc.commu <- c
end
| Cvar {commu = Cvar _ | Cok as inside} ->
link_commu ~inside c
| _ -> invalid_arg "Types.link_commu"
let set_commu_ok c = link_commu ~inside:c Cok
let snapshot () =
let old = !last_snapshot in
last_snapshot := !new_id;
(!trail, old)
let rec rev_log accu = function
Unchanged -> accu
| Invalid -> assert false
| Change (ch, next) ->
let d = !next in
next := Invalid;
rev_log (ch::accu) d
let backtrack ~cleanup_abbrev (changes, old) =
match !changes with
Unchanged -> last_snapshot := old
| Invalid -> failwith "Types.backtrack"
| Change _ as change ->
cleanup_abbrev ();
let backlog = rev_log [] change in
List.iter undo_change backlog;
changes := Unchanged;
last_snapshot := old;
trail := changes
let undo_first_change_after (changes, _) =
match !changes with
| Change (ch, _) ->
undo_change ch
| _ -> ()
let rec rev_compress_log log r =
match !r with
Unchanged | Invalid ->
log
| Change (Ccompress _, next) ->
rev_compress_log (r::log) next
| Change (_, next) ->
rev_compress_log log next
let undo_compress (changes, _old) =
match !changes with
Unchanged
| Invalid -> ()
| Change _ ->
let log = rev_compress_log [] changes in
List.iter
(fun r -> match !r with
Change (Ccompress (ty, desc, d), next) when ty.desc == d ->
Transient_expr.set_desc ty desc; r := !next
| _ -> ())
log
let class_mode =
let hint : _ Mode.Hint.const = Legacy Class in
Mode.Value.(of_const ~hint_monadic:hint ~hint_comonadic:hint Const.legacy)
let toplevel_mode =
let hint : _ Mode.Hint.const = Legacy Toplevel in
Mode.Value.(of_const ~hint_monadic:hint ~hint_comonadic:hint Const.legacy)
let linked_variables () = !linked_variables
let is_valid (changes, _old) =
match !changes with
| Invalid -> false
| _ -> true
let on_backtrack f =
log_change (Cfun f)