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
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Environment handling *)
open Cmi_format
open Config
open Misc
open Asttypes
open Longident
open Path
open Types
open Btype
let add_delayed_check_forward = ref (fun _ -> assert false)
let value_declarations : ((string * Location.t), (unit -> unit)) Hashtbl.t =
Hashtbl.create 16
(* This table is used to usage of value declarations. A declaration is
identified with its name and location. The callback attached to a
declaration is called whenever the value is used explicitly
(lookup_value) or implicitly (inclusion test between signatures,
cf Includemod.value_descriptions). *)
let type_declarations = Hashtbl.create 16
let module_declarations = Hashtbl.create 16
type constructor_usage = Positive | Pattern | Privatize
type constructor_usages =
{
mutable cu_positive: bool;
mutable cu_pattern: bool;
mutable cu_privatize: bool;
}
let add_constructor_usage cu = function
| Positive -> cu.cu_positive <- true
| Pattern -> cu.cu_pattern <- true
| Privatize -> cu.cu_privatize <- true
let constructor_usages () =
{cu_positive = false; cu_pattern = false; cu_privatize = false}
let used_constructors :
(string * Location.t * string, (constructor_usage -> unit)) Hashtbl.t
= Hashtbl.create 16
let prefixed_sg = Hashtbl.create 113
type error =
| Illegal_renaming of string * string * string
| Inconsistent_import of string * string * string
| Need_recursive_types of string * string
| Depend_on_unsafe_string_unit of string * string
| Missing_module of Location.t * Path.t * Path.t
| Illegal_value_name of Location.t * string
exception Error of error
let error err = raise (Error err)
module EnvLazy : sig
type ('a,'b) t
type log
val force : ('a -> 'b) -> ('a,'b) t -> 'b
val create : 'a -> ('a,'b) t
val get_arg : ('a,'b) t -> 'a option
(* [force_logged log f t] is equivalent to [force f t] but if [f] returns [None] then
[t] is recorded in [log]. [backtrack log] will then reset all the recorded [t]s back
to their original state. *)
val log : unit -> log
val force_logged : log -> ('a -> 'b option) -> ('a,'b option) t -> 'b option
val backtrack : log -> unit
end = struct
type ('a,'b) t = ('a,'b) eval ref
and ('a,'b) eval =
| Done of 'b
| Raise of exn
| Thunk of 'a
type undo =
| Nil
| Cons : ('a, 'b) t * 'a * undo -> undo
type log = undo ref
let force f x =
match !x with
| Done x -> x
| Raise e -> raise e
| Thunk e ->
match f e with
| y ->
x := Done y;
y
| exception e ->
x := Raise e;
raise e
let get_arg x =
match !x with Thunk a -> Some a | _ -> None
let create x =
ref (Thunk x)
let log () =
ref Nil
let force_logged log f x =
match !x with
| Done x -> x
| Raise e -> raise e
| Thunk e ->
match f e with
| None ->
x := Done None;
log := Cons(x, e, !log);
None
| Some _ as y ->
x := Done y;
y
| exception e ->
x := Raise e;
raise e
let backtrack log =
let rec loop = function
| Nil -> ()
| Cons(x, e, rest) ->
x := Thunk e;
loop rest
in
loop !log
end
module PathMap = Map.Make(Path)
type summary =
Env_empty
| Env_value of summary * Ident.t * value_description
| Env_type of summary * Ident.t * type_declaration
| Env_extension of summary * Ident.t * extension_constructor
| Env_module of summary * Ident.t * module_declaration
| Env_modtype of summary * Ident.t * modtype_declaration
| Env_class of summary * Ident.t * class_declaration
| Env_cltype of summary * Ident.t * class_type_declaration
| Env_open of summary * Path.t
| Env_functor_arg of summary * Ident.t
| Env_constraints of summary * type_declaration PathMap.t
| Env_copy_types of summary * string list
module TycompTbl =
struct
(** This module is used to store components of types (i.e. labels
and contructors). We keep a representation of each nested
"open" and the set of local bindings between each of them. *)
type 'a t = {
current: 'a Ident.tbl;
(** Local bindings since the last open. *)
opened: 'a opened option;
(** Symbolic representation of the last (innermost) open, if any. *)
}
and 'a opened = {
components: (string, 'a list) Tbl.t;
(** Components from the opened module. We keep a list of
bindings for each name, as in comp_labels and
comp_constrs. *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: 'a t;
(** The table before opening the module. *)
}
let empty = { current = Ident.empty; opened = None }
let add id x tbl =
{tbl with current = Ident.add id x tbl.current}
let add_open slot wrap components next =
let using =
match slot with
| None -> None
| Some f -> Some (fun s x -> f s (wrap x))
in
{
current = Ident.empty;
opened = Some {using; components; next};
}
let rec find_same id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.opened with
| Some {next; _} -> find_same id next
| None -> raise exn
end
let nothing = fun () -> ()
let mk_callback rest name desc = function
| None -> nothing
| Some f ->
(fun () ->
match rest with
| [] -> f name None
| (hidden, _) :: _ -> f name (Some (desc, hidden))
)
let rec find_all name tbl =
List.map (fun (_id, desc) -> desc, nothing)
(Ident.find_all name tbl.current) @
match tbl.opened with
| None -> []
| Some {using; next; components} ->
let rest = find_all name next in
match Tbl.find_str name components with
| exception Not_found -> rest
| opened ->
List.map
(fun desc -> desc, mk_callback rest name desc using)
opened
@ rest
let rec fold_name f tbl acc =
let acc = Ident.fold_name (fun _id d -> f d) tbl.current acc in
match tbl.opened with
| Some {using = _; next; components} ->
acc
|> Tbl.fold
(fun _name -> List.fold_right (fun desc -> f desc))
components
|> fold_name f next
| None ->
acc
let rec local_keys tbl acc =
let acc = Ident.fold_all (fun k _ accu -> k::accu) tbl.current acc in
match tbl.opened with
| Some o -> local_keys o.next acc
| None -> acc
let diff_keys is_local tbl1 tbl2 =
let keys2 = local_keys tbl2 [] in
List.filter
(fun id ->
is_local (find_same id tbl2) &&
try ignore (find_same id tbl1); false
with Not_found -> true)
keys2
end
module IdTbl =
struct
(** This module is used to store all kinds of components except
(labels and constructors) in environments. We keep a
representation of each nested "open" and the set of local
bindings between each of them. *)
type 'a t = {
current: 'a Ident.tbl;
(** Local bindings since the last open *)
opened: 'a opened option;
(** Symbolic representation of the last (innermost) open, if any. *)
}
and 'a opened = {
root: Path.t;
(** The path of the opened module, to be prefixed in front of
its local names to produce a valid path in the current
environment. *)
components: (string, 'a * int) Tbl.t;
(** Components from the opened module. *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: 'a t;
(** The table before opening the module. *)
}
let empty = { current = Ident.empty; opened = None }
let add id x tbl =
{tbl with current = Ident.add id x tbl.current}
let add_open slot wrap root components next =
let using =
match slot with
| None -> None
| Some f -> Some (fun s x -> f s (wrap x))
in
{
current = Ident.empty;
opened = Some {using; root; components; next};
}
let rec find_same id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.opened with
| Some {next; _} -> find_same id next
| None -> raise exn
end
let rec find_name mark name tbl =
try
let (id, desc) = Ident.find_name name tbl.current in
Pident id, desc
with Not_found as exn ->
begin match tbl.opened with
| Some {using; root; next; components} ->
begin try
let (descr, pos) = Tbl.find_str name components in
let res = Pdot (root, name, pos), descr in
if mark then begin match using with
| None -> ()
| Some f ->
begin try f name (Some (snd (find_name false name next), snd res))
with Not_found -> f name None
end
end;
res
with Not_found ->
find_name mark name next
end
| None ->
raise exn
end
let find_name name tbl = find_name true name tbl
let rec update name f tbl =
try
let (id, desc) = Ident.find_name name tbl.current in
let new_desc = f desc in
{tbl with current = Ident.add id new_desc tbl.current}
with Not_found ->
begin match tbl.opened with
| Some {root; using; next; components} ->
begin try
let (desc, pos) = Tbl.find_str name components in
let new_desc = f desc in
let components = Tbl.add name (new_desc, pos) components in
{tbl with opened = Some {root; using; next; components}}
with Not_found ->
let next = update name f next in
{tbl with opened = Some {root; using; next; components}}
end
| None ->
tbl
end
let rec find_all name tbl =
List.map (fun (id, desc) -> Pident id, desc) (Ident.find_all name tbl.current) @
match tbl.opened with
| None -> []
| Some {root; using = _; next; components} ->
try
let (desc, pos) = Tbl.find_str name components in
(Pdot (root, name, pos), desc) :: find_all name next
with Not_found ->
find_all name next
let rec fold_name f tbl acc =
let acc = Ident.fold_name (fun id d -> f (Ident.name id) (Pident id, d)) tbl.current acc in
match tbl.opened with
| Some {root; using = _; next; components} ->
acc
|> Tbl.fold
(fun name (desc, pos) -> f name (Pdot (root, name, pos), desc))
components
|> fold_name f next
| None ->
acc
let rec local_keys tbl acc =
let acc = Ident.fold_all (fun k _ accu -> k::accu) tbl.current acc in
match tbl.opened with
| Some o -> local_keys o.next acc
| None -> acc
let rec iter f tbl =
Ident.iter (fun id desc -> f id (Pident id, desc)) tbl.current;
match tbl.opened with
| Some {root; using = _; next; components} ->
Tbl.iter
(fun s (x, pos) -> f (Ident.hide (Ident.create s) (* ??? *)) (Pdot (root, s, pos), x))
components;
iter f next
| None -> ()
let diff_keys tbl1 tbl2 =
let keys2 = local_keys tbl2 [] in
List.filter
(fun id ->
try ignore (find_same id tbl1); false
with Not_found -> true)
keys2
end
type type_descriptions =
constructor_description list * label_description list
let in_signature_flag = 0x01
let implicit_coercion_flag = 0x02
type t = {
values: value_description IdTbl.t;
constrs: constructor_description TycompTbl.t;
labels: label_description TycompTbl.t;
types: (type_declaration * type_descriptions) IdTbl.t;
modules: (Subst.t * module_declaration, module_declaration) EnvLazy.t IdTbl.t;
modtypes: modtype_declaration IdTbl.t;
components: module_components IdTbl.t;
classes: class_declaration IdTbl.t;
cltypes: class_type_declaration IdTbl.t;
functor_args: unit Ident.tbl;
summary: summary;
local_constraints: type_declaration PathMap.t;
gadt_instances: (int * TypeSet.t ref) list;
flags: int;
}
and module_components =
{
deprecated: string option;
loc: Location.t;
comps:
(t * Subst.t * Path.t * Types.module_type, module_components_repr option)
EnvLazy.t;
}
and module_components_repr =
Structure_comps of structure_components
| Functor_comps of functor_components
and 'a comp_tbl = (string, ('a * int)) Tbl.t
and structure_components = {
mutable comp_values: value_description comp_tbl;
mutable comp_constrs: (string, constructor_description list) Tbl.t;
mutable comp_labels: (string, label_description list) Tbl.t;
mutable comp_types: (type_declaration * type_descriptions) comp_tbl;
mutable comp_modules:
(Subst.t * module_declaration, module_declaration) EnvLazy.t comp_tbl;
mutable comp_modtypes: modtype_declaration comp_tbl;
mutable comp_components: module_components comp_tbl;
mutable comp_classes: class_declaration comp_tbl;
mutable comp_cltypes: class_type_declaration comp_tbl;
}
and functor_components = {
fcomp_param: Ident.t; (* Formal parameter *)
fcomp_arg: module_type option; (* Argument signature *)
fcomp_res: module_type; (* Result signature *)
fcomp_cache: (Path.t, module_components) Hashtbl.t; (* For memoization *)
fcomp_subst_cache: (Path.t, module_type) Hashtbl.t
}
let copy_local ~from env =
{ env with
local_constraints = from.local_constraints;
gadt_instances = from.gadt_instances;
flags = from.flags }
let same_constr = ref (fun _ _ _ -> assert false)
(* Helper to decide whether to report an identifier shadowing
by some 'open'. For labels and constructors, we do not report
if the two elements are from the same re-exported declaration.
Later, one could also interpret some attributes on value and
type declarations to silence the shadowing warnings. *)
let check_shadowing env = function
| `Constructor (Some (c1, c2))
when not (!same_constr env c1.cstr_res c2.cstr_res) ->
Some "constructor"
| `Label (Some (l1, l2))
when not (!same_constr env l1.lbl_res l2.lbl_res) ->
Some "label"
| `Value (Some _) -> Some "value"
| `Type (Some _) -> Some "type"
| `Module (Some _) | `Component (Some _) -> Some "module"
| `Module_type (Some _) -> Some "module type"
| `Class (Some _) -> Some "class"
| `Class_type (Some _) -> Some "class type"
| `Constructor _ | `Label _
| `Value None | `Type None | `Module None | `Module_type None
| `Class None | `Class_type None | `Component None ->
None
let subst_modtype_maker (subst, md) =
if subst == Subst.identity then md
else {md with md_type = Subst.modtype subst md.md_type}
let empty = {
values = IdTbl.empty; constrs = TycompTbl.empty;
labels = TycompTbl.empty; types = IdTbl.empty;
modules = IdTbl.empty; modtypes = IdTbl.empty;
components = IdTbl.empty; classes = IdTbl.empty;
cltypes = IdTbl.empty;
summary = Env_empty; local_constraints = PathMap.empty; gadt_instances = [];
flags = 0;
functor_args = Ident.empty;
}
let in_signature b env =
let flags =
if b then env.flags lor in_signature_flag
else env.flags land (lnot in_signature_flag)
in
{env with flags}
let implicit_coercion env =
{env with flags = env.flags lor implicit_coercion_flag}
let is_in_signature env = env.flags land in_signature_flag <> 0
let is_implicit_coercion env = env.flags land implicit_coercion_flag <> 0
let is_ident = function
Pident _ -> true
| Pdot _ | Papply _ -> false
let is_local_ext = function
| {cstr_tag = Cstr_extension(p, _)} -> is_ident p
| _ -> false
let diff env1 env2 =
IdTbl.diff_keys env1.values env2.values @
TycompTbl.diff_keys is_local_ext env1.constrs env2.constrs @
IdTbl.diff_keys env1.modules env2.modules @
IdTbl.diff_keys env1.classes env2.classes
type can_load_cmis =
| Can_load_cmis
| Cannot_load_cmis of EnvLazy.log
let can_load_cmis = ref Can_load_cmis
let without_cmis f x =
let log = EnvLazy.log () in
let res =
Misc.(protect_refs
[R (can_load_cmis, Cannot_load_cmis log)]
(fun () -> f x))
in
EnvLazy.backtrack log;
res
(* Forward declarations *)
let components_of_module' =
ref ((fun ~deprecated:_ ~loc:_ _env _sub _path _mty -> assert false) :
deprecated:string option -> loc:Location.t -> t -> Subst.t ->
Path.t -> module_type ->
module_components)
let components_of_module_maker' =
ref ((fun (_env, _sub, _path, _mty) -> assert false) :
t * Subst.t * Path.t * module_type -> module_components_repr option)
let components_of_functor_appl' =
ref ((fun _f _env _p1 _p2 -> assert false) :
functor_components -> t -> Path.t -> Path.t -> module_components)
let check_modtype_inclusion =
(* to be filled with Includemod.check_modtype_inclusion *)
ref ((fun ~loc:_ _env _mty1 _path1 _mty2 -> assert false) :
loc:Location.t -> t -> module_type -> Path.t -> module_type -> unit)
let strengthen =
(* to be filled with Mtype.strengthen *)
ref ((fun ~aliasable:_ _env _mty _path -> assert false) :
aliasable:bool -> t -> module_type -> Path.t -> module_type)
let md md_type =
{md_type; md_attributes=[]; md_loc=Location.none}
let get_components_opt c =
match !can_load_cmis with
| Can_load_cmis ->
EnvLazy.force !components_of_module_maker' c.comps
| Cannot_load_cmis log ->
EnvLazy.force_logged log !components_of_module_maker' c.comps
let empty_structure =
Structure_comps {
comp_values = Tbl.empty;
comp_constrs = Tbl.empty;
comp_labels = Tbl.empty;
comp_types = Tbl.empty;
comp_modules = Tbl.empty; comp_modtypes = Tbl.empty;
comp_components = Tbl.empty; comp_classes = Tbl.empty;
comp_cltypes = Tbl.empty }
let get_components c =
match get_components_opt c with
| None -> empty_structure
| Some c -> c
(* The name of the compilation unit currently compiled.
"" if outside a compilation unit. *)
let current_unit = ref ""
(* Persistent structure descriptions *)
type pers_struct =
{ ps_name: string;
ps_sig: signature Lazy.t;
ps_comps: module_components;
ps_crcs: (string * Digest.t option) list;
ps_filename: string;
ps_flags: pers_flags list }
let persistent_structures =
(Hashtbl.create 17 : (string, pers_struct option) Hashtbl.t)
(* Consistency between persistent structures *)
let crc_units = Consistbl.create()
module StringSet =
Set.Make(struct type t = string let compare = String.compare end)
let imported_units = ref StringSet.empty
let add_import s =
imported_units := StringSet.add s !imported_units
let imported_opaque_units = ref StringSet.empty
let add_imported_opaque s =
imported_opaque_units := StringSet.add s !imported_opaque_units
let clear_imports () =
Consistbl.clear crc_units;
imported_units := StringSet.empty;
imported_opaque_units := StringSet.empty
let check_consistency ps =
try
List.iter
(fun (name, crco) ->
match crco with
None -> ()
| Some crc ->
add_import name;
Consistbl.check crc_units name crc ps.ps_filename)
ps.ps_crcs;
with Consistbl.Inconsistency(name, source, auth) ->
error (Inconsistent_import(name, auth, source))
(* Reading persistent structures from .cmi files *)
let save_pers_struct crc ps =
let modname = ps.ps_name in
Hashtbl.add persistent_structures modname (Some ps);
List.iter
(function
| Rectypes -> ()
| Deprecated _ -> ()
| Unsafe_string -> ()
| Opaque -> add_imported_opaque modname)
ps.ps_flags;
Consistbl.set crc_units modname crc ps.ps_filename;
add_import modname
module Persistent_signature = struct
type t =
{ filename : string;
cmi : Cmi_format.cmi_infos }
let load = ref (fun ~unit_name ->
match find_in_path_uncap !load_path (unit_name ^ ".cmi") with
| filename -> Some { filename; cmi = read_cmi filename }
| exception Not_found -> None)
end
let acknowledge_pers_struct check modname
{ Persistent_signature.filename; cmi } =
let name = cmi.cmi_name in
let sign = cmi.cmi_sign in
let crcs = cmi.cmi_crcs in
let flags = cmi.cmi_flags in
let deprecated =
List.fold_left (fun acc -> function Deprecated s -> Some s | _ -> acc) None
flags
in
let comps =
!components_of_module' ~deprecated ~loc:Location.none
empty Subst.identity
(Pident(Ident.create_persistent name))
(Mty_signature sign)
in
let ps = { ps_name = name;
ps_sig = lazy (Subst.signature Subst.identity sign);
ps_comps = comps;
ps_crcs = crcs;
ps_filename = filename;
ps_flags = flags;
} in
if ps.ps_name <> modname then
error (Illegal_renaming(modname, ps.ps_name, filename));
List.iter
(function
| Rectypes ->
if not !Clflags.recursive_types then
error (Need_recursive_types(ps.ps_name, !current_unit))
| Unsafe_string ->
if Config.safe_string then
error (Depend_on_unsafe_string_unit (ps.ps_name, !current_unit));
| Deprecated _ -> ()
| Opaque -> add_imported_opaque modname)
ps.ps_flags;
if check then check_consistency ps;
Hashtbl.add persistent_structures modname (Some ps);
ps
let read_pers_struct check modname filename =
add_import modname;
let cmi = read_cmi filename in
acknowledge_pers_struct check modname
{ Persistent_signature.filename; cmi }
let find_pers_struct check name =
if name = "*predef*" then raise Not_found;
match Hashtbl.find persistent_structures name with
| Some ps -> ps
| None -> raise Not_found
| exception Not_found ->
match !can_load_cmis with
| Cannot_load_cmis _ -> raise Not_found
| Can_load_cmis ->
let ps =
match !Persistent_signature.load ~unit_name:name with
| Some ps -> ps
| None ->
Hashtbl.add persistent_structures name None;
raise Not_found
in
add_import name;
acknowledge_pers_struct check name ps
(* Emits a warning if there is no valid cmi for name *)
let check_pers_struct name =
try
ignore (find_pers_struct false name)
with
| Not_found ->
let warn = Warnings.No_cmi_file(name, None) in
Location.prerr_warning Location.none warn
| Cmi_format.Error err ->
let msg = Format.asprintf "%a" Cmi_format.report_error err in
let warn = Warnings.No_cmi_file(name, Some msg) in
Location.prerr_warning Location.none warn
| Error err ->
let msg =
match err with
| Illegal_renaming(name, ps_name, filename) ->
Format.asprintf
" %a@ contains the compiled interface for @ \
%s when %s was expected"
Location.print_filename filename ps_name name
| Inconsistent_import _ -> assert false
| Need_recursive_types(name, _) ->
Format.sprintf
"%s uses recursive types"
name
| Depend_on_unsafe_string_unit (name, _) ->
Printf.sprintf "%s uses -unsafe-string"
name
| Missing_module _ -> assert false
| Illegal_value_name _ -> assert false
in
let warn = Warnings.No_cmi_file(name, Some msg) in
Location.prerr_warning Location.none warn
let read_pers_struct modname filename =
read_pers_struct true modname filename
let find_pers_struct name =
find_pers_struct true name
let check_pers_struct name =
if not (Hashtbl.mem persistent_structures name) then begin
(* PR#6843: record the weak dependency ([add_import]) regardless of
whether the check suceeds, to help make builds more
deterministic. *)
add_import name;
if (Warnings.is_active (Warnings.No_cmi_file("", None))) then
!add_delayed_check_forward
(fun () -> check_pers_struct name)
end
let reset_cache () =
current_unit := "";
Hashtbl.clear persistent_structures;
clear_imports ();
Hashtbl.clear value_declarations;
Hashtbl.clear type_declarations;
Hashtbl.clear module_declarations;
Hashtbl.clear used_constructors;
Hashtbl.clear prefixed_sg
let reset_cache_toplevel () =
(* Delete 'missing cmi' entries from the cache. *)
let l =
Hashtbl.fold
(fun name r acc -> if r = None then name :: acc else acc)
persistent_structures []
in
List.iter (Hashtbl.remove persistent_structures) l;
Hashtbl.clear value_declarations;
Hashtbl.clear type_declarations;
Hashtbl.clear module_declarations;
Hashtbl.clear used_constructors;
Hashtbl.clear prefixed_sg
let set_unit_name name =
current_unit := name
let get_unit_name () =
!current_unit
(* Lookup by identifier *)
let rec find_module_descr path env =
match path with
Pident id ->
begin try
IdTbl.find_same id env.components
with Not_found ->
if Ident.persistent id && not (Ident.name id = !current_unit)
then (find_pers_struct (Ident.name id)).ps_comps
else raise Not_found
end
| Pdot(p, s, _pos) ->
begin match get_components (find_module_descr p env) with
Structure_comps c ->
let (descr, _pos) = Tbl.find_str s c.comp_components in
descr
| Functor_comps _ ->
raise Not_found
end
| Papply(p1, p2) ->
begin match get_components (find_module_descr p1 env) with
Functor_comps f ->
!components_of_functor_appl' f env p1 p2
| Structure_comps _ ->
raise Not_found
end
let find proj1 proj2 path env =
match path with
Pident id ->
IdTbl.find_same id (proj1 env)
| Pdot(p, s, _pos) ->
begin match get_components (find_module_descr p env) with
Structure_comps c ->
let (data, _pos) = Tbl.find_str s (proj2 c) in data
| Functor_comps _ ->
raise Not_found
end
| Papply _ ->
raise Not_found
let find_value =
find (fun env -> env.values) (fun sc -> sc.comp_values)
and find_type_full =
find (fun env -> env.types) (fun sc -> sc.comp_types)
and find_modtype =
find (fun env -> env.modtypes) (fun sc -> sc.comp_modtypes)
and find_class =
find (fun env -> env.classes) (fun sc -> sc.comp_classes)
and find_cltype =
find (fun env -> env.cltypes) (fun sc -> sc.comp_cltypes)
let type_of_cstr path = function
| {cstr_inlined = Some d; _} ->
(d, ([], List.map snd (Datarepr.labels_of_type path d)))
| _ ->
assert false
let find_type_full path env =
match Path.constructor_typath path with
| Regular p ->
(try (PathMap.find p env.local_constraints, ([], []))
with Not_found -> find_type_full p env)
| Cstr (ty_path, s) ->
let (_, (cstrs, _)) =
try find_type_full ty_path env
with Not_found -> assert false
in
let cstr =
try List.find (fun cstr -> cstr.cstr_name = s) cstrs
with Not_found -> assert false
in
type_of_cstr path cstr
| LocalExt id ->
let cstr =
try TycompTbl.find_same id env.constrs
with Not_found -> assert false
in
type_of_cstr path cstr
| Ext (mod_path, s) ->
let comps =
try find_module_descr mod_path env
with Not_found -> assert false
in
let comps =
match get_components comps with
| Structure_comps c -> c
| Functor_comps _ -> assert false
in
let exts =
List.filter
(function {cstr_tag=Cstr_extension _} -> true | _ -> false)
(try Tbl.find_str s comps.comp_constrs
with Not_found -> assert false)
in
match exts with
| [cstr] -> type_of_cstr path cstr
| _ -> assert false
let find_type p env =
fst (find_type_full p env)
let find_type_descrs p env =
snd (find_type_full p env)
let find_module ~alias path env =
match path with
Pident id ->
begin try
let data = IdTbl.find_same id env.modules in
EnvLazy.force subst_modtype_maker data
with Not_found ->
if Ident.persistent id && not (Ident.name id = !current_unit) then
let ps = find_pers_struct (Ident.name id) in
md (Mty_signature(Lazy.force ps.ps_sig))
else raise Not_found
end
| Pdot(p, s, _pos) ->
begin match get_components (find_module_descr p env) with
Structure_comps c ->
let (data, _pos) = Tbl.find_str s c.comp_modules in
EnvLazy.force subst_modtype_maker data
| Functor_comps _ ->
raise Not_found
end
| Papply(p1, p2) ->
let desc1 = find_module_descr p1 env in
begin match get_components desc1 with
Functor_comps f ->
md begin match f.fcomp_res with
| Mty_alias _ as mty -> mty
| mty ->
if alias then mty else
try
Hashtbl.find f.fcomp_subst_cache p2
with Not_found ->
let mty =
Subst.modtype
(Subst.add_module f.fcomp_param p2 Subst.identity)
f.fcomp_res in
Hashtbl.add f.fcomp_subst_cache p2 mty;
mty
end
| Structure_comps _ ->
raise Not_found
end
let required_globals = ref []
let reset_required_globals () = required_globals := []
let get_required_globals () = !required_globals
let add_required_global id =
if Ident.global id && not !Clflags.transparent_modules
&& not (List.exists (Ident.same id) !required_globals)
then required_globals := id :: !required_globals
let rec normalize_path lax env path =
let path =
match path with
Pdot(p, s, pos) ->
Pdot(normalize_path lax env p, s, pos)
| Papply(p1, p2) ->
Papply(normalize_path lax env p1, normalize_path true env p2)
| _ -> path
in
try match find_module ~alias:true path env with
{md_type=Mty_alias(_, path1)} ->
let path' = normalize_path lax env path1 in
if lax || !Clflags.transparent_modules then path' else
let id = Path.head path in
if Ident.global id && not (Ident.same id (Path.head path'))
then add_required_global id;
path'
| _ -> path
with Not_found when lax
|| (match path with Pident id -> not (Ident.persistent id) | _ -> true) ->
path
let normalize_path oloc env path =
try normalize_path (oloc = None) env path
with Not_found ->
match oloc with None -> assert false
| Some loc ->
raise (Error(Missing_module(loc, path, normalize_path true env path)))
let normalize_path_prefix oloc env path =
match path with
Pdot(p, s, pos) ->
Pdot(normalize_path oloc env p, s, pos)
| Pident _ ->
path
| Papply _ ->
assert false
let find_module = find_module ~alias:false
(* Find the manifest type associated to a type when appropriate:
- the type should be public or should have a private row,
- the type should have an associated manifest type. *)
let find_type_expansion path env =
let decl = find_type path env in
match decl.type_manifest with
| Some body when decl.type_private = Public
|| decl.type_kind <> Type_abstract
|| Btype.has_constr_row body ->
(decl.type_params, body, may_map snd decl.type_newtype_level)
(* The manifest type of Private abstract data types without
private row are still considered unknown to the type system.
Hence, this case is caught by the following clause that also handles
purely abstract data types without manifest type definition. *)
| _ -> raise Not_found
(* Find the manifest type information associated to a type, i.e.
the necessary information for the compiler's type-based optimisations.
In particular, the manifest type associated to a private abstract type
is revealed for the sake of compiler's type-based optimisations. *)
let find_type_expansion_opt path env =
let decl = find_type path env in
match decl.type_manifest with
(* The manifest type of Private abstract data types can still get
an approximation using their manifest type. *)
| Some body -> (decl.type_params, body, may_map snd decl.type_newtype_level)
| _ -> raise Not_found
let find_modtype_expansion path env =
match (find_modtype path env).mtd_type with
| None -> raise Not_found
| Some mty -> mty
let rec is_functor_arg path env =
match path with
Pident id ->
begin try Ident.find_same id env.functor_args; true
with Not_found -> false
end
| Pdot (p, _s, _) -> is_functor_arg p env
| Papply _ -> true
(* Lookup by name *)
exception Recmodule
let report_deprecated ?loc p deprecated =
match loc, deprecated with
| Some loc, Some txt ->
let txt = if txt = "" then "" else "\n" ^ txt in
Location.deprecated loc (Printf.sprintf "module %s%s" (Path.name p) txt)
| _ -> ()
let mark_module_used env name loc =
if not (is_implicit_coercion env) then
try Hashtbl.find module_declarations (name, loc) ()
with Not_found -> ()
let rec lookup_module_descr_aux ?loc lid env =
match lid with
Lident s ->
begin try
IdTbl.find_name s env.components
with Not_found ->
if s = !current_unit then raise Not_found;
let ps = find_pers_struct s in
(Pident(Ident.create_persistent s), ps.ps_comps)
end
| Ldot(l, s) ->
let (p, descr) = lookup_module_descr ?loc l env in
begin match get_components descr with
Structure_comps c ->
let (descr, pos) = Tbl.find_str s c.comp_components in
(Pdot(p, s, pos), descr)
| Functor_comps _ ->
raise Not_found
end
| Lapply(l1, l2) ->
let (p1, desc1) = lookup_module_descr ?loc l1 env in
let p2 = lookup_module ~load:true ?loc l2 env in
let {md_type=mty2} = find_module p2 env in
begin match get_components desc1 with
Functor_comps f ->
let loc = match loc with Some l -> l | None -> Location.none in
Misc.may (!check_modtype_inclusion ~loc env mty2 p2) f.fcomp_arg;
(Papply(p1, p2), !components_of_functor_appl' f env p1 p2)
| Structure_comps _ ->
raise Not_found
end
and lookup_module_descr ?loc lid env =
let (p, comps) as res = lookup_module_descr_aux ?loc lid env in
mark_module_used env (Path.last p) comps.loc;
(*
Format.printf "USE module %s at %a@." (Path.last p)
Location.print comps.loc;
*)
report_deprecated ?loc p comps.deprecated;
res
and lookup_module ~load ?loc lid env : Path.t =
match lid with
Lident s ->
begin try
let (p, data) = IdTbl.find_name s env.modules in
let {md_loc; md_attributes; md_type} =
EnvLazy.force subst_modtype_maker data
in
mark_module_used env s md_loc;
begin match md_type with
| Mty_ident (Path.Pident id) when Ident.name id = "#recmod#" ->
(* see #5965 *)
raise Recmodule
| _ -> ()
end;
report_deprecated ?loc p
(Builtin_attributes.deprecated_of_attrs md_attributes);
p
with Not_found ->
if s = !current_unit then raise Not_found;
let p = Pident(Ident.create_persistent s) in
if !Clflags.transparent_modules && not load then check_pers_struct s
else begin
let ps = find_pers_struct s in
report_deprecated ?loc p ps.ps_comps.deprecated
end;
p
end
| Ldot(l, s) ->
let (p, descr) = lookup_module_descr ?loc l env in
begin match get_components descr with
Structure_comps c ->
let (_data, pos) = Tbl.find_str s c.comp_modules in
let (comps, _) = Tbl.find_str s c.comp_components in
mark_module_used env s comps.loc;
let p = Pdot(p, s, pos) in
report_deprecated ?loc p comps.deprecated;
p
| Functor_comps _ ->
raise Not_found
end
| Lapply(l1, l2) ->
let (p1, desc1) = lookup_module_descr ?loc l1 env in
let p2 = lookup_module ~load:true ?loc l2 env in
let {md_type=mty2} = find_module p2 env in
let p = Papply(p1, p2) in
begin match get_components desc1 with
Functor_comps f ->
let loc = match loc with Some l -> l | None -> Location.none in
Misc.may (!check_modtype_inclusion ~loc env mty2 p2) f.fcomp_arg;
p
| Structure_comps _ ->
raise Not_found
end
let lookup proj1 proj2 ?loc lid env =
match lid with
Lident s ->
IdTbl.find_name s (proj1 env)
| Ldot(l, s) ->
let (p, desc) = lookup_module_descr ?loc l env in
begin match get_components desc with
Structure_comps c ->
let (data, pos) = Tbl.find_str s (proj2 c) in
(Pdot(p, s, pos), data)
| Functor_comps _ ->
raise Not_found
end
| Lapply _ ->
raise Not_found
let lookup_all_simple proj1 proj2 shadow ?loc lid env =
match lid with
Lident s ->
let xl = TycompTbl.find_all s (proj1 env) in
let rec do_shadow =
function
| [] -> []
| ((x, f) :: xs) ->
(x, f) ::
(do_shadow (List.filter (fun (y, _) -> not (shadow x y)) xs))
in
do_shadow xl
| Ldot(l, s) ->
let (_p, desc) = lookup_module_descr ?loc l env in
begin match get_components desc with
Structure_comps c ->
let comps =
try Tbl.find_str s (proj2 c) with Not_found -> []
in
List.map
(fun data -> (data, (fun () -> ())))
comps
| Functor_comps _ ->
raise Not_found
end
| Lapply _ ->
raise Not_found
let has_local_constraints env = not (PathMap.is_empty env.local_constraints)
let cstr_shadow cstr1 cstr2 =
match cstr1.cstr_tag, cstr2.cstr_tag with
| Cstr_extension _, Cstr_extension _ -> true
| _ -> false
let lbl_shadow _lbl1 _lbl2 = false
let lookup_value =
lookup (fun env -> env.values) (fun sc -> sc.comp_values)
let lookup_all_constructors =
lookup_all_simple (fun env -> env.constrs) (fun sc -> sc.comp_constrs)
cstr_shadow
let lookup_all_labels =
lookup_all_simple (fun env -> env.labels) (fun sc -> sc.comp_labels)
lbl_shadow
let lookup_type =
lookup (fun env -> env.types) (fun sc -> sc.comp_types)
let lookup_modtype =
lookup (fun env -> env.modtypes) (fun sc -> sc.comp_modtypes)
let lookup_class =
lookup (fun env -> env.classes) (fun sc -> sc.comp_classes)
let lookup_cltype =
lookup (fun env -> env.cltypes) (fun sc -> sc.comp_cltypes)
let copy_types l env =
let f desc = {desc with val_type = Subst.type_expr Subst.identity desc.val_type} in
let values = List.fold_left (fun env s -> IdTbl.update s f env) env.values l in
{env with values; summary = Env_copy_types (env.summary, l)}
let mark_value_used env name vd =
if not (is_implicit_coercion env) then
try Hashtbl.find value_declarations (name, vd.val_loc) ()
with Not_found -> ()
let mark_type_used env name vd =
if not (is_implicit_coercion env) then
try Hashtbl.find type_declarations (name, vd.type_loc) ()
with Not_found -> ()
let mark_constructor_used usage env name vd constr =
if not (is_implicit_coercion env) then
try Hashtbl.find used_constructors (name, vd.type_loc, constr) usage
with Not_found -> ()
let mark_extension_used usage env ext name =
if not (is_implicit_coercion env) then
let ty_name = Path.last ext.ext_type_path in
try Hashtbl.find used_constructors (ty_name, ext.ext_loc, name) usage
with Not_found -> ()
let set_value_used_callback name vd callback =
let key = (name, vd.val_loc) in
try
let old = Hashtbl.find value_declarations key in
Hashtbl.replace value_declarations key (fun () -> old (); callback ())
(* this is to support cases like:
let x = let x = 1 in x in x
where the two declarations have the same location
(e.g. resulting from Camlp4 expansion of grammar entries) *)
with Not_found ->
Hashtbl.add value_declarations key callback
let set_type_used_callback name td callback =
let loc = td.type_loc in
if loc.Location.loc_ghost then ()
else let key = (name, loc) in
let old =
try Hashtbl.find type_declarations key
with Not_found -> assert false
in
Hashtbl.replace type_declarations key (fun () -> callback old)
let lookup_value ?loc lid env =
let (_, desc) as r = lookup_value ?loc lid env in
mark_value_used env (Longident.last lid) desc;
r
let lookup_type ?loc lid env =
let (path, (decl, _)) = lookup_type ?loc lid env in
mark_type_used env (Longident.last lid) decl;
path
let mark_type_path env path =
try
let decl = find_type path env in
mark_type_used env (Path.last path) decl
with Not_found -> ()
let ty_path t =
match repr t with
| {desc=Tconstr(path, _, _)} -> path
| _ -> assert false
let lookup_constructor ?loc lid env =
match lookup_all_constructors ?loc lid env with
[] -> raise Not_found
| (desc, use) :: _ ->
mark_type_path env (ty_path desc.cstr_res);
use ();
desc
let is_lident = function
Lident _ -> true
| _ -> false
let lookup_all_constructors ?loc lid env =
try
let cstrs = lookup_all_constructors ?loc lid env in
let wrap_use desc use () =
mark_type_path env (ty_path desc.cstr_res);
use ()
in
List.map (fun (cstr, use) -> (cstr, wrap_use cstr use)) cstrs
with
Not_found when is_lident lid -> []
let mark_constructor usage env name desc =
if not (is_implicit_coercion env)
then match desc.cstr_tag with
| Cstr_extension _ ->
begin
let ty_path = ty_path desc.cstr_res in
let ty_name = Path.last ty_path in
try Hashtbl.find used_constructors (ty_name, desc.cstr_loc, name) usage
with Not_found -> ()
end
| _ ->
let ty_path = ty_path desc.cstr_res in
let ty_decl = try find_type ty_path env with Not_found -> assert false in
let ty_name = Path.last ty_path in
mark_constructor_used usage env ty_name ty_decl name
let lookup_label ?loc lid env =
match lookup_all_labels ?loc lid env with
[] -> raise Not_found
| (desc, use) :: _ ->
mark_type_path env (ty_path desc.lbl_res);
use ();
desc
let lookup_all_labels ?loc lid env =
try
let lbls = lookup_all_labels ?loc lid env in
let wrap_use desc use () =
mark_type_path env (ty_path desc.lbl_res);
use ()
in
List.map (fun (lbl, use) -> (lbl, wrap_use lbl use)) lbls
with
Not_found when is_lident lid -> []
let lookup_class ?loc lid env =
let (_, desc) as r = lookup_class ?loc lid env in
(* special support for Typeclass.unbound_class *)
if Path.name desc.cty_path = "" then ignore (lookup_type ?loc lid env)
else mark_type_path env desc.cty_path;
r
let lookup_cltype ?loc lid env =
let (_, desc) as r = lookup_cltype ?loc lid env in
if Path.name desc.clty_path = "" then ignore (lookup_type ?loc lid env)
else mark_type_path env desc.clty_path;
mark_type_path env desc.clty_path;
r
(* Iter on an environment (ignoring the body of functors and
not yet evaluated structures) *)
type iter_cont = unit -> unit
let iter_env_cont = ref []
let rec scrape_alias_for_visit env mty =
match mty with
| Mty_alias(_, Pident id)
when Ident.persistent id
&& not (Hashtbl.mem persistent_structures (Ident.name id)) -> false
| Mty_alias(_, path) -> (* PR#6600: find_module may raise Not_found *)
begin try scrape_alias_for_visit env (find_module path env).md_type
with Not_found -> false
end
| _ -> true
let iter_env proj1 proj2 f env () =
IdTbl.iter (fun id x -> f (Pident id) x) (proj1 env);
let rec iter_components path path' mcomps =
let cont () =
let visit =
match EnvLazy.get_arg mcomps.comps with
| None -> true
| Some (env, _sub, _path, mty) -> scrape_alias_for_visit env mty
in
if not visit then () else
match get_components mcomps with
Structure_comps comps ->
Tbl.iter
(fun s (d, n) -> f (Pdot (path, s, n)) (Pdot (path', s, n), d))
(proj2 comps);
Tbl.iter
(fun s (c, n) ->
iter_components (Pdot (path, s, n)) (Pdot (path', s, n)) c)
comps.comp_components
| Functor_comps _ -> ()
in iter_env_cont := (path, cont) :: !iter_env_cont
in
Hashtbl.iter
(fun s pso ->
match pso with None -> ()
| Some ps ->
let id = Pident (Ident.create_persistent s) in
iter_components id id ps.ps_comps)
persistent_structures;
IdTbl.iter
(fun id (path, comps) -> iter_components (Pident id) path comps)
env.components
let run_iter_cont l =
iter_env_cont := [];
List.iter (fun c -> c ()) l;
let cont = List.rev !iter_env_cont in
iter_env_cont := [];
cont
let iter_types f = iter_env (fun env -> env.types) (fun sc -> sc.comp_types) f
let same_types env1 env2 =
env1.types == env2.types && env1.components == env2.components
let used_persistent () =
let r = ref Concr.empty in
Hashtbl.iter (fun s pso -> if pso != None then r := Concr.add s !r)
persistent_structures;
!r
let find_all_comps proj s (p,mcomps) =
match get_components mcomps with
Functor_comps _ -> []
| Structure_comps comps ->
try let (c,n) = Tbl.find_str s (proj comps) in [Pdot(p,s,n), c]
with Not_found -> []
let rec find_shadowed_comps path env =
match path with
Pident id ->
IdTbl.find_all (Ident.name id) env.components
| Pdot (p, s, _) ->
let l = find_shadowed_comps p env in
let l' =
List.map (find_all_comps (fun comps -> comps.comp_components) s) l in
List.flatten l'
| Papply _ -> []
let find_shadowed proj1 proj2 path env =
match path with
Pident id ->
IdTbl.find_all (Ident.name id) (proj1 env)
| Pdot (p, s, _) ->
let l = find_shadowed_comps p env in
let l' = List.map (find_all_comps proj2 s) l in
List.flatten l'
| Papply _ -> []
let find_shadowed_types path env =
List.map fst
(find_shadowed
(fun env -> env.types) (fun comps -> comps.comp_types) path env)
(* GADT instance tracking *)
let add_gadt_instance_level lv env =
{env with
gadt_instances = (lv, ref TypeSet.empty) :: env.gadt_instances}
let is_Tlink = function {desc = Tlink _} -> true | _ -> false
let gadt_instance_level env t =
let rec find_instance = function
[] -> None
| (lv, r) :: rem ->
if TypeSet.exists is_Tlink !r then
(* Should we use set_typeset ? *)
r := TypeSet.fold (fun ty -> TypeSet.add (repr ty)) !r TypeSet.empty;
if TypeSet.mem t !r then Some lv else find_instance rem
in find_instance env.gadt_instances
let add_gadt_instances env lv tl =
let r =
try List.assoc lv env.gadt_instances with Not_found -> assert false in
(* Format.eprintf "Added";
List.iter (fun ty -> Format.eprintf "@ %a" !Btype.print_raw ty) tl;
Format.eprintf "@."; *)
set_typeset r (List.fold_right TypeSet.add tl !r)
(* Only use this after expand_head! *)
let add_gadt_instance_chain env lv t =
let r =
try List.assoc lv env.gadt_instances with Not_found -> assert false in
let rec add_instance t =
let t = repr t in
if not (TypeSet.mem t !r) then begin
(* Format.eprintf "@ %a" !Btype.print_raw t; *)
set_typeset r (TypeSet.add t !r);
match t.desc with
Tconstr (p, _, memo) ->
may add_instance (find_expans Private p !memo)
| _ -> ()
end
in
(* Format.eprintf "Added chain"; *)
add_instance t
(* Format.eprintf "@." *)
(* Expand manifest module type names at the top of the given module type *)
let rec scrape_alias env ?path mty =
match mty, path with
Mty_ident p, _ ->
begin try
scrape_alias env (find_modtype_expansion p env) ?path
with Not_found ->
mty
end
| Mty_alias(_, path), _ ->
begin try
scrape_alias env (find_module path env).md_type ~path
with Not_found ->
(*Location.prerr_warning Location.none
(Warnings.No_cmi_file (Path.name path));*)
mty
end
| mty, Some path ->
!strengthen ~aliasable:true env mty path
| _ -> mty
let scrape_alias env mty = scrape_alias env mty
(* Given a signature and a root path, prefix all idents in the signature
by the root path and build the corresponding substitution. *)
let rec prefix_idents root pos sub = function
[] -> ([], sub)
| Sig_value(id, decl) :: rem ->
let p = Pdot(root, Ident.name id, pos) in
let nextpos = match decl.val_kind with Val_prim _ -> pos | _ -> pos+1 in
let (pl, final_sub) = prefix_idents root nextpos sub rem in
(p::pl, final_sub)
| Sig_type(id, _, _) :: rem ->
let p = Pdot(root, Ident.name id, nopos) in
let (pl, final_sub) =
prefix_idents root pos (Subst.add_type id p sub) rem in
(p::pl, final_sub)
| Sig_typext(id, _, _) :: rem ->
let p = Pdot(root, Ident.name id, pos) in
(* we extend the substitution in case of an inlined record *)
let (pl, final_sub) =
prefix_idents root (pos+1) (Subst.add_type id p sub) rem in
(p::pl, final_sub)
| Sig_module(id, _, _) :: rem ->
let p = Pdot(root, Ident.name id, pos) in
let (pl, final_sub) =
prefix_idents root (pos+1) (Subst.add_module id p sub) rem in
(p::pl, final_sub)
| Sig_modtype(id, _) :: rem ->
let p = Pdot(root, Ident.name id, nopos) in
let (pl, final_sub) =
prefix_idents root pos
(Subst.add_modtype id (Mty_ident p) sub) rem in
(p::pl, final_sub)
| Sig_class(id, _, _) :: rem ->
(* pretend this is a type, cf. PR#6650 *)
let p = Pdot(root, Ident.name id, pos) in
let (pl, final_sub) =
prefix_idents root (pos + 1) (Subst.add_type id p sub) rem in
(p::pl, final_sub)
| Sig_class_type(id, _, _) :: rem ->
let p = Pdot(root, Ident.name id, nopos) in
let (pl, final_sub) =
prefix_idents root pos (Subst.add_type id p sub) rem in
(p::pl, final_sub)
let prefix_idents root sub sg =
if sub = Subst.identity then
let sgs =
try
Hashtbl.find prefixed_sg root
with Not_found ->
let sgs = ref [] in
Hashtbl.add prefixed_sg root sgs;
sgs
in
try
List.assq sg !sgs
with Not_found ->
let r = prefix_idents root 0 sub sg in
sgs := (sg, r) :: !sgs;
r
else
prefix_idents root 0 sub sg
(* Compute structure descriptions *)
let add_to_tbl id decl tbl =
let decls =
try Tbl.find_str id tbl with Not_found -> [] in
Tbl.add id (decl :: decls) tbl
let rec components_of_module ~deprecated ~loc env sub path mty =
{
deprecated;
loc;
comps = EnvLazy.create (env, sub, path, mty)
}
and components_of_module_maker (env, sub, path, mty) =
match scrape_alias env mty with
Mty_signature sg ->
let c =
{ comp_values = Tbl.empty;
comp_constrs = Tbl.empty;
comp_labels = Tbl.empty; comp_types = Tbl.empty;
comp_modules = Tbl.empty; comp_modtypes = Tbl.empty;
comp_components = Tbl.empty; comp_classes = Tbl.empty;
comp_cltypes = Tbl.empty } in
let pl, sub = prefix_idents path sub sg in
let env = ref env in
let pos = ref 0 in
List.iter2 (fun item path ->
match item with
Sig_value(id, decl) ->
let decl' = Subst.value_description sub decl in
c.comp_values <-
Tbl.add (Ident.name id) (decl', !pos) c.comp_values;
begin match decl.val_kind with
Val_prim _ -> () | _ -> incr pos
end
| Sig_type(id, decl, _) ->
let decl' = Subst.type_declaration sub decl in
let constructors =
List.map snd (Datarepr.constructors_of_type path decl') in
let labels =
List.map snd (Datarepr.labels_of_type path decl') in
c.comp_types <-
Tbl.add (Ident.name id)
((decl', (constructors, labels)), nopos)
c.comp_types;
List.iter
(fun descr ->
c.comp_constrs <-
add_to_tbl descr.cstr_name descr c.comp_constrs)
constructors;
List.iter
(fun descr ->
c.comp_labels <-
add_to_tbl descr.lbl_name descr c.comp_labels)
labels;
env := store_type_infos id decl !env
| Sig_typext(id, ext, _) ->
let ext' = Subst.extension_constructor sub ext in
let descr = Datarepr.extension_descr path ext' in
c.comp_constrs <-
add_to_tbl (Ident.name id) descr c.comp_constrs;
incr pos
| Sig_module(id, md, _) ->
let md' = EnvLazy.create (sub, md) in
c.comp_modules <-
Tbl.add (Ident.name id) (md', !pos) c.comp_modules;
let deprecated =
Builtin_attributes.deprecated_of_attrs md.md_attributes
in
let comps =
components_of_module ~deprecated ~loc:md.md_loc !env sub path
md.md_type
in
c.comp_components <-
Tbl.add (Ident.name id) (comps, !pos) c.comp_components;
env := store_module ~check:false id md !env;
incr pos
| Sig_modtype(id, decl) ->
let decl' = Subst.modtype_declaration sub decl in
c.comp_modtypes <-
Tbl.add (Ident.name id) (decl', nopos) c.comp_modtypes;
env := store_modtype id decl !env
| Sig_class(id, decl, _) ->
let decl' = Subst.class_declaration sub decl in
c.comp_classes <-
Tbl.add (Ident.name id) (decl', !pos) c.comp_classes;
incr pos
| Sig_class_type(id, decl, _) ->
let decl' = Subst.cltype_declaration sub decl in
c.comp_cltypes <-
Tbl.add (Ident.name id) (decl', !pos) c.comp_cltypes)
sg pl;
Some (Structure_comps c)
| Mty_functor(param, ty_arg, ty_res) ->
Some (Functor_comps {
fcomp_param = param;
(* fcomp_arg and fcomp_res must be prefixed eagerly, because
they are interpreted in the outer environment *)
fcomp_arg = may_map (Subst.modtype sub) ty_arg;
fcomp_res = Subst.modtype sub ty_res;
fcomp_cache = Hashtbl.create 17;
fcomp_subst_cache = Hashtbl.create 17 })
| Mty_ident _
| Mty_alias _ -> None
(* Insertion of bindings by identifier + path *)
and check_usage loc id warn tbl =
if not loc.Location.loc_ghost && Warnings.is_active (warn "") then begin
let name = Ident.name id in
let key = (name, loc) in
if Hashtbl.mem tbl key then ()
else let used = ref false in
Hashtbl.add tbl key (fun () -> used := true);
if not (name = "" || name.[0] = '_' || name.[0] = '#')
then
!add_delayed_check_forward
(fun () -> if not !used then Location.prerr_warning loc (warn name))
end;
and check_value_name name loc =
(* Note: we could also check here general validity of the
identifier, to protect against bad identifiers forged by -pp or
-ppx preprocessors. *)
if String.length name > 0 && (name.[0] = '#') then
for i = 1 to String.length name - 1 do
if name.[i] = '#' then
raise (Error(Illegal_value_name(loc, name)))
done
and store_value ?check id decl env =
check_value_name (Ident.name id) decl.val_loc;
may (fun f -> check_usage decl.val_loc id f value_declarations) check;
{ env with
values = IdTbl.add id decl env.values;
summary = Env_value(env.summary, id, decl) }
and store_type ~check id info env =
let loc = info.type_loc in
if check then
check_usage loc id (fun s -> Warnings.Unused_type_declaration s)
type_declarations;
let path = Pident id in
let constructors = Datarepr.constructors_of_type path info in
let labels = Datarepr.labels_of_type path info in
let descrs = (List.map snd constructors, List.map snd labels) in
if check && not loc.Location.loc_ghost &&
Warnings.is_active (Warnings.Unused_constructor ("", false, false))
then begin
let ty = Ident.name id in
List.iter
begin fun (_, {cstr_name = c; _}) ->
let k = (ty, loc, c) in
if not (Hashtbl.mem used_constructors k) then
let used = constructor_usages () in
Hashtbl.add used_constructors k (add_constructor_usage used);
if not (ty = "" || ty.[0] = '_')
then !add_delayed_check_forward
(fun () ->
if not (is_in_signature env) && not used.cu_positive then
Location.prerr_warning loc
(Warnings.Unused_constructor
(c, used.cu_pattern, used.cu_privatize)))
end
constructors
end;
{ env with
constrs =
List.fold_right
(fun (id, descr) constrs -> TycompTbl.add id descr constrs)
constructors
env.constrs;
labels =
List.fold_right
(fun (id, descr) labels -> TycompTbl.add id descr labels)
labels
env.labels;
types =
IdTbl.add id (info, descrs) env.types;
summary = Env_type(env.summary, id, info) }
and store_type_infos id info env =
(* Simplified version of store_type that doesn't compute and store
constructor and label infos, but simply record the arity and
manifest-ness of the type. Used in components_of_module to
keep track of type abbreviations (e.g. type t = float) in the
computation of label representations. *)
{ env with
types = IdTbl.add id (info,([],[]))
env.types;
summary = Env_type(env.summary, id, info) }
and store_extension ~check id ext env =
let loc = ext.ext_loc in
if check && not loc.Location.loc_ghost &&
Warnings.is_active (Warnings.Unused_extension ("", false, false, false))
then begin
let is_exception = Path.same ext.ext_type_path Predef.path_exn in
let ty = Path.last ext.ext_type_path in
let n = Ident.name id in
let k = (ty, loc, n) in
if not (Hashtbl.mem used_constructors k) then begin
let used = constructor_usages () in
Hashtbl.add used_constructors k (add_constructor_usage used);
!add_delayed_check_forward
(fun () ->
if not (is_in_signature env) && not used.cu_positive then
Location.prerr_warning loc
(Warnings.Unused_extension
(n, is_exception, used.cu_pattern, used.cu_privatize)
)
)
end;
end;
{ env with
constrs = TycompTbl.add id
(Datarepr.extension_descr (Pident id) ext)
env.constrs;
summary = Env_extension(env.summary, id, ext) }
and store_module ~check id md env =
let loc = md.md_loc in
if check then
check_usage loc id (fun s -> Warnings.Unused_module s)
module_declarations;
let deprecated = Builtin_attributes.deprecated_of_attrs md.md_attributes in
{ env with
modules = IdTbl.add id (EnvLazy.create (Subst.identity, md)) env.modules;
components =
IdTbl.add id
(components_of_module ~deprecated ~loc:md.md_loc
env Subst.identity (Pident id) md.md_type)
env.components;
summary = Env_module(env.summary, id, md) }
and store_modtype id info env =
{ env with
modtypes = IdTbl.add id info env.modtypes;
summary = Env_modtype(env.summary, id, info) }
and store_class id desc env =
{ env with
classes = IdTbl.add id desc env.classes;
summary = Env_class(env.summary, id, desc) }
and store_cltype id desc env =
{ env with
cltypes = IdTbl.add id desc env.cltypes;
summary = Env_cltype(env.summary, id, desc) }
(* Compute the components of a functor application in a path. *)
let components_of_functor_appl f env p1 p2 =
try
Hashtbl.find f.fcomp_cache p2
with Not_found ->
let p = Papply(p1, p2) in
let sub = Subst.add_module f.fcomp_param p2 Subst.identity in
let mty = Subst.modtype sub f.fcomp_res in
let comps = components_of_module ~deprecated:None ~loc:Location.none
(*???*)
env Subst.identity p mty in
Hashtbl.add f.fcomp_cache p2 comps;
comps
(* Define forward functions *)
let _ =
components_of_module' := components_of_module;
components_of_functor_appl' := components_of_functor_appl;
components_of_module_maker' := components_of_module_maker
(* Insertion of bindings by identifier *)
let add_functor_arg id env =
{env with
functor_args = Ident.add id () env.functor_args;
summary = Env_functor_arg (env.summary, id)}
let add_value ?check id desc env =
store_value ?check id desc env
let add_type ~check id info env =
store_type ~check id info env
and add_extension ~check id ext env =
store_extension ~check id ext env
and add_module_declaration ?(arg=false) ~check id md env =
let env = store_module ~check id md env in
if arg then add_functor_arg id env else env
and add_modtype id info env =
store_modtype id info env
and add_class id ty env =
store_class id ty env
and add_cltype id ty env =
store_cltype id ty env
let add_module ?arg id mty env =
add_module_declaration ~check:false ?arg id (md mty) env
let add_local_type path info env =
{ env with
local_constraints = PathMap.add path info env.local_constraints }
let add_local_constraint path info elv env =
match info with
{type_manifest = Some _; type_newtype_level = Some (lv, _)} ->
(* elv is the expansion level, lv is the definition level *)
let info = {info with type_newtype_level = Some (lv, elv)} in
add_local_type path info env
| _ -> assert false
(* Insertion of bindings by name *)
let enter store_fun name data env =
let id = Ident.create name in (id, store_fun id data env)
let enter_value ?check = enter (store_value ?check)
and enter_type = enter (store_type ~check:true)
and enter_extension = enter (store_extension ~check:true)
and enter_module_declaration ?arg id md env =
add_module_declaration ?arg ~check:true id md env
(* let (id, env) = enter store_module name md env in
(id, add_functor_arg ?arg id env) *)
and enter_modtype = enter store_modtype
and enter_class = enter store_class
and enter_cltype = enter store_cltype
let enter_module ?arg s mty env =
let id = Ident.create s in
(id, enter_module_declaration ?arg id (md mty) env)
(* Insertion of all components of a signature *)
let add_item comp env =
match comp with
Sig_value(id, decl) -> add_value id decl env
| Sig_type(id, decl, _) -> add_type ~check:false id decl env
| Sig_typext(id, ext, _) -> add_extension ~check:false id ext env
| Sig_module(id, md, _) -> add_module_declaration ~check:false id md env
| Sig_modtype(id, decl) -> add_modtype id decl env
| Sig_class(id, decl, _) -> add_class id decl env
| Sig_class_type(id, decl, _) -> add_cltype id decl env
let rec add_signature sg env =
match sg with
[] -> env
| comp :: rem -> add_signature rem (add_item comp env)
(* Open a signature path *)
let add_components slot root env0 comps =
let add_l w comps env0 =
TycompTbl.add_open slot w comps env0
in
let add w comps env0 = IdTbl.add_open slot w root comps env0 in
let constrs =
add_l (fun x -> `Constructor x) comps.comp_constrs env0.constrs
in
let labels =
add_l (fun x -> `Label x) comps.comp_labels env0.labels
in
let values =
add (fun x -> `Value x) comps.comp_values env0.values
in
let types =
add (fun x -> `Type x) comps.comp_types env0.types
in
let modtypes =
add (fun x -> `Module_type x) comps.comp_modtypes env0.modtypes
in
let classes =
add (fun x -> `Class x) comps.comp_classes env0.classes
in
let cltypes =
add (fun x -> `Class_type x) comps.comp_cltypes env0.cltypes
in
let components =
add (fun x -> `Component x) comps.comp_components env0.components
in
let modules =
add (fun x -> `Module x) comps.comp_modules env0.modules
in
{ env0 with
summary = Env_open(env0.summary, root);
constrs;
labels;
values;
types;
modtypes;
classes;
cltypes;
components;
modules;
}
let open_signature slot root env0 =
match get_components (find_module_descr root env0) with
| Functor_comps _ -> None
| Structure_comps comps -> Some (add_components slot root env0 comps)
(* Open a signature from a file *)
let open_pers_signature name env =
match open_signature None (Pident(Ident.create_persistent name)) env with
| Some env -> env
| None -> assert false (* a compilation unit cannot refer to a functor *)
let open_signature ?(loc = Location.none) ?(toplevel = false) ovf root env =
if not toplevel && ovf = Asttypes.Fresh && not loc.Location.loc_ghost
&& (Warnings.is_active (Warnings.Unused_open "")
|| Warnings.is_active (Warnings.Open_shadow_identifier ("", ""))
|| Warnings.is_active (Warnings.Open_shadow_label_constructor ("","")))
then begin
let used = ref false in
!add_delayed_check_forward
(fun () ->
if not !used then
Location.prerr_warning loc (Warnings.Unused_open (Path.name root))
);
let shadowed = ref [] in
let slot s b =
begin match check_shadowing env b with
| Some kind when not (List.mem (kind, s) !shadowed) ->
shadowed := (kind, s) :: !shadowed;
let w =
match kind with
| "label" | "constructor" ->
Warnings.Open_shadow_label_constructor (kind, s)
| _ -> Warnings.Open_shadow_identifier (kind, s)
in
Location.prerr_warning loc w
| _ -> ()
end;
used := true
in
open_signature (Some slot) root env
end
else open_signature None root env
(* Read a signature from a file *)
let read_signature modname filename =
let ps = read_pers_struct modname filename in
Lazy.force ps.ps_sig
(* Return the CRC of the interface of the given compilation unit *)
let crc_of_unit name =
let ps = find_pers_struct name in
let crco =
try
List.assoc name ps.ps_crcs
with Not_found ->
assert false
in
match crco with
None -> assert false
| Some crc -> crc
(* Return the list of imported interfaces with their CRCs *)
let imports () =
Consistbl.extract (StringSet.elements !imported_units) crc_units
(* Returns true if [s] is an opaque imported module *)
let is_imported_opaque s =
StringSet.mem s !imported_opaque_units
(* Save a signature to a file *)
let save_signature_with_imports ~deprecated sg modname filename imports =
(*prerr_endline filename;
List.iter (fun (name, crc) -> prerr_endline name) imports;*)
Btype.cleanup_abbrev ();
Subst.reset_for_saving ();
let sg = Subst.signature (Subst.for_saving Subst.identity) sg in
let flags =
List.concat [
if !Clflags.recursive_types then [Cmi_format.Rectypes] else [];
if !Clflags.opaque then [Cmi_format.Opaque] else [];
(if !Clflags.unsafe_string then [Cmi_format.Unsafe_string] else []);
(match deprecated with Some s -> [Deprecated s] | None -> []);
]
in
let oc = open_out_bin filename in
try
let cmi = {
cmi_name = modname;
cmi_sign = sg;
cmi_crcs = imports;
cmi_flags = flags;
} in
let crc = output_cmi filename oc cmi in
close_out oc;
(* Enter signature in persistent table so that imported_unit()
will also return its crc *)
let comps =
components_of_module ~deprecated ~loc:Location.none
empty Subst.identity
(Pident(Ident.create_persistent modname)) (Mty_signature sg) in
let ps =
{ ps_name = modname;
ps_sig = lazy (Subst.signature Subst.identity sg);
ps_comps = comps;
ps_crcs = (cmi.cmi_name, Some crc) :: imports;
ps_filename = filename;
ps_flags = cmi.cmi_flags;
} in
save_pers_struct crc ps;
cmi
with exn ->
close_out oc;
remove_file filename;
raise exn
let save_signature ~deprecated sg modname filename =
save_signature_with_imports ~deprecated sg modname filename (imports())
(* Folding on environments *)
let find_all proj1 proj2 f lid env acc =
match lid with
| None ->
IdTbl.fold_name
(fun name (p, data) acc -> f name p data acc)
(proj1 env) acc
| Some l ->
let p, desc = lookup_module_descr l env in
begin match get_components desc with
Structure_comps c ->
Tbl.fold
(fun s (data, pos) acc -> f s (Pdot (p, s, pos)) data acc)
(proj2 c) acc
| Functor_comps _ ->
acc
end
let find_all_simple_list proj1 proj2 f lid env acc =
match lid with
| None ->
TycompTbl.fold_name
(fun data acc -> f data acc)
(proj1 env) acc
| Some l ->
let (_p, desc) = lookup_module_descr l env in
begin match get_components desc with
Structure_comps c ->
Tbl.fold
(fun _s comps acc ->
match comps with
[] -> acc
| data :: _ ->
f data acc)
(proj2 c) acc
| Functor_comps _ ->
acc
end
let fold_modules f lid env acc =
match lid with
| None ->
let acc =
IdTbl.fold_name
(fun name (p, data) acc ->
let data = EnvLazy.force subst_modtype_maker data in
f name p data acc
)
env.modules
acc
in
Hashtbl.fold
(fun name ps acc ->
match ps with
None -> acc
| Some ps ->
f name (Pident(Ident.create_persistent name))
(md (Mty_signature (Lazy.force ps.ps_sig))) acc)
persistent_structures
acc
| Some l ->
let p, desc = lookup_module_descr l env in
begin match get_components desc with
Structure_comps c ->
Tbl.fold
(fun s (data, pos) acc ->
f s (Pdot (p, s, pos))
(EnvLazy.force subst_modtype_maker data) acc)
c.comp_modules
acc
| Functor_comps _ ->
acc
end
let fold_values f =
find_all (fun env -> env.values) (fun sc -> sc.comp_values) f
and fold_constructors f =
find_all_simple_list (fun env -> env.constrs) (fun sc -> sc.comp_constrs) f
and fold_labels f =
find_all_simple_list (fun env -> env.labels) (fun sc -> sc.comp_labels) f
and fold_types f =
find_all (fun env -> env.types) (fun sc -> sc.comp_types) f
and fold_modtypes f =
find_all (fun env -> env.modtypes) (fun sc -> sc.comp_modtypes) f
and fold_classs f =
find_all (fun env -> env.classes) (fun sc -> sc.comp_classes) f
and fold_cltypes f =
find_all (fun env -> env.cltypes) (fun sc -> sc.comp_cltypes) f
(* Make the initial environment *)
let (initial_safe_string, initial_unsafe_string) =
Predef.build_initial_env
(add_type ~check:false)
(add_extension ~check:false)
empty
(* Return the environment summary *)
let summary env =
if PathMap.is_empty env.local_constraints then env.summary
else Env_constraints (env.summary, env.local_constraints)
let last_env = ref empty
let last_reduced_env = ref empty
let keep_only_summary env =
if !last_env == env then !last_reduced_env
else begin
let new_env =
{
empty with
summary = env.summary;
local_constraints = env.local_constraints;
flags = env.flags;
}
in
last_env := env;
last_reduced_env := new_env;
new_env
end
let env_of_only_summary env_from_summary env =
let new_env = env_from_summary env.summary Subst.identity in
{ new_env with
local_constraints = env.local_constraints;
flags = env.flags;
}
(* Error report *)
open Format
let report_error ppf = function
| Illegal_renaming(modname, ps_name, filename) -> fprintf ppf
"Wrong file naming: %a@ contains the compiled interface for @ \
%s when %s was expected"
Location.print_filename filename ps_name modname
| Inconsistent_import(name, source1, source2) -> fprintf ppf
"@[<hov>The files %a@ and %a@ \
make inconsistent assumptions@ over interface %s@]"
Location.print_filename source1 Location.print_filename source2 name
| Need_recursive_types(import, export) ->
fprintf ppf
"@[<hov>Unit %s imports from %s, which uses recursive types.@ %s@]"
export import "The compilation flag -rectypes is required"
| Depend_on_unsafe_string_unit(import, export) ->
fprintf ppf
"@[<hov>Unit %s imports from %s, compiled with -unsafe-string.@ %s@]"
export import "This compiler has been configured in strict \
-safe-string mode"
| Missing_module(_, path1, path2) ->
fprintf ppf "@[@[<hov>";
if Path.same path1 path2 then
fprintf ppf "Internal path@ %s@ is dangling." (Path.name path1)
else
fprintf ppf "Internal path@ %s@ expands to@ %s@ which is dangling."
(Path.name path1) (Path.name path2);
fprintf ppf "@]@ @[%s@ %s@ %s.@]@]"
"The compiled interface for module" (Ident.name (Path.head path2))
"was not found"
| Illegal_value_name(_loc, name) ->
fprintf ppf "'%s' is not a valid value identifier."
name
let () =
Location.register_error_of_exn
(function
| Error (Missing_module (loc, _, _)
| Illegal_value_name (loc, _)
as err) when loc <> Location.none ->
Some (Location.error_of_printer loc report_error err)
| Error err -> Some (Location.error_of_printer_file report_error err)
| _ -> None
)
|