forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ml
3719 lines (3302 loc) · 122 KB
/
env.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* 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 Misc
open Asttypes
open Longident
open Path
open Types
open Local_store
module String = Misc.Stdlib.String
let add_delayed_check_forward = ref (fun _ -> assert false)
type 'a usage_tbl = ('a -> unit) Types.Uid.Tbl.t
(** This table is used to track usage of value declarations.
A declaration is identified by its uid.
The callback attached to a declaration is called whenever the value (or
type, or ...) is used explicitly (lookup_value, ...) or implicitly
(inclusion test between signatures, cf Includemod.value_descriptions, ...).
*)
let value_declarations : unit usage_tbl ref = s_table Types.Uid.Tbl.create 16
let type_declarations : unit usage_tbl ref = s_table Types.Uid.Tbl.create 16
let module_declarations : unit usage_tbl ref = s_table Types.Uid.Tbl.create 16
type constructor_usage = Positive | Pattern | Exported_private | Exported
type constructor_usages =
{
mutable cu_positive: bool;
mutable cu_pattern: bool;
mutable cu_exported_private: bool;
}
let add_constructor_usage cu usage =
match usage with
| Positive -> cu.cu_positive <- true
| Pattern -> cu.cu_pattern <- true
| Exported_private -> cu.cu_exported_private <- true
| Exported ->
cu.cu_positive <- true;
cu.cu_pattern <- true;
cu.cu_exported_private <- true
let constructor_usages () =
{cu_positive = false; cu_pattern = false; cu_exported_private = false}
let constructor_usage_complaint ~rebind priv cu
: Warnings.constructor_usage_warning option =
match priv, rebind with
| Asttypes.Private, _ | _, true ->
if cu.cu_positive || cu.cu_pattern || cu.cu_exported_private then None
else Some Unused
| Asttypes.Public, false -> begin
match cu.cu_positive, cu.cu_pattern, cu.cu_exported_private with
| true, _, _ -> None
| false, false, false -> Some Unused
| false, true, _ -> Some Not_constructed
| false, false, true -> Some Only_exported_private
end
let used_constructors : constructor_usage usage_tbl ref =
s_table Types.Uid.Tbl.create 16
type label_usage =
Projection | Mutation | Construct | Exported_private | Exported
type label_usages =
{
mutable lu_projection: bool;
mutable lu_mutation: bool;
mutable lu_construct: bool;
}
let add_label_usage lu usage =
match usage with
| Projection -> lu.lu_projection <- true;
| Mutation -> lu.lu_mutation <- true
| Construct -> lu.lu_construct <- true
| Exported_private ->
lu.lu_projection <- true
| Exported ->
lu.lu_projection <- true;
lu.lu_mutation <- true;
lu.lu_construct <- true
let is_mutating_label_usage = function
| Mutation -> true
| (Projection | Construct | Exported_private | Exported) -> false
let label_usages () =
{lu_projection = false; lu_mutation = false; lu_construct = false}
let label_usage_complaint priv mut lu
: Warnings.field_usage_warning option =
match priv, mut with
| Asttypes.Private, _ ->
if lu.lu_projection then None
else Some Unused
| Asttypes.Public, Asttypes.Immutable -> begin
match lu.lu_projection, lu.lu_construct with
| true, _ -> None
| false, false -> Some Unused
| false, true -> Some Not_read
end
| Asttypes.Public, Asttypes.Mutable -> begin
match lu.lu_projection, lu.lu_mutation, lu.lu_construct with
| true, true, _ -> None
| false, false, false -> Some Unused
| false, _, _ -> Some Not_read
| true, false, _ -> Some Not_mutated
end
let used_labels : label_usage usage_tbl ref =
s_table Types.Uid.Tbl.create 16
(** Map indexed by the name of module components. *)
module NameMap = String.Map
type value_unbound_reason =
| Val_unbound_instance_variable
| Val_unbound_self
| Val_unbound_ancestor
| Val_unbound_ghost_recursive of Location.t
type module_unbound_reason =
| Mod_unbound_illegal_recursion
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_presence * 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 Path.Map.t
| Env_copy_types of summary
| Env_persistent of summary * Ident.t
| Env_value_unbound of summary * string * value_unbound_reason
| Env_module_unbound of summary * string * module_unbound_reason
let map_summary f = function
Env_empty -> Env_empty
| Env_value (s, id, d) -> Env_value (f s, id, d)
| Env_type (s, id, d) -> Env_type (f s, id, d)
| Env_extension (s, id, d) -> Env_extension (f s, id, d)
| Env_module (s, id, p, d) -> Env_module (f s, id, p, d)
| Env_modtype (s, id, d) -> Env_modtype (f s, id, d)
| Env_class (s, id, d) -> Env_class (f s, id, d)
| Env_cltype (s, id, d) -> Env_cltype (f s, id, d)
| Env_open (s, p) -> Env_open (f s, p)
| Env_functor_arg (s, id) -> Env_functor_arg (f s, id)
| Env_constraints (s, m) -> Env_constraints (f s, m)
| Env_copy_types s -> Env_copy_types (f s)
| Env_persistent (s, id) -> Env_persistent (f s, id)
| Env_value_unbound (s, u, r) -> Env_value_unbound (f s, u, r)
| Env_module_unbound (s, u, r) -> Env_module_unbound (f s, u, r)
type address =
| Aident of Ident.t
| Adot of address * int
module TycompTbl =
struct
(** This module is used to store components of types (i.e. labels
and constructors). We keep a representation of each nested
"open" and the set of local bindings between each of them. *)
type '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: ('a list) NameMap.t;
(** Components from the opened module. We keep a list of
bindings for each name, as in comp_labels and
comp_constrs. *)
root: Path.t;
(** Only used to check removal of open *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: '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; components; root; next};
}
let remove_last_open rt tbl =
match tbl.opened with
| Some {root; next; _} when Path.same rt root ->
{ next with current =
Ident.fold_all Ident.add tbl.current next.current }
| _ ->
assert false
let rec find_same id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.opened with
| Some {next; _} -> find_same id next
| None -> raise exn
end
let nothing = fun () -> ()
let mk_callback rest name desc using =
match using with
| None -> nothing
| Some f ->
(fun () ->
match rest with
| [] -> f name None
| (hidden, _) :: _ -> f name (Some (desc, hidden)))
let rec find_all ~mark name tbl =
List.map (fun (_id, desc) -> desc, nothing)
(Ident.find_all name tbl.current) @
match tbl.opened with
| None -> []
| Some {using; next; components; root = _} ->
let rest = find_all ~mark name next in
let using = if mark then using else None in
match NameMap.find name components with
| exception Not_found -> rest
| opened ->
List.map
(fun desc -> 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; root = _} ->
acc
|> NameMap.fold
(fun _name -> List.fold_right f)
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, 'b) t = {
current: 'a Ident.tbl;
(** Local bindings since the last open *)
layer: ('a, 'b) layer;
(** Symbolic representation of the last (innermost) open, if any. *)
}
and ('a, 'b) layer =
| Open of {
root: Path.t;
(** The path of the opened module, to be prefixed in front of
its local names to produce a valid path in the current
environment. *)
components: 'b NameMap.t;
(** Components from the opened module. *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: ('a, 'b) t;
(** The table before opening the module. *)
}
| Map of {
f: ('a -> 'a);
next: ('a, 'b) t;
}
| Nothing
let empty = { current = Ident.empty; layer = Nothing }
let add id x tbl =
{tbl with current = Ident.add id x tbl.current}
let remove id tbl =
{tbl with current = Ident.remove id tbl.current}
let add_open slot wrap root components next =
let using =
match slot with
| None -> None
| Some f -> Some (fun s x -> f s (wrap x))
in
{
current = Ident.empty;
layer = Open {using; root; components; next};
}
let remove_last_open rt tbl =
match tbl.layer with
| Open {root; next; _} when Path.same rt root ->
{ next with current =
Ident.fold_all Ident.add tbl.current next.current }
| _ ->
assert false
let map f next =
{
current = Ident.empty;
layer = Map {f; next}
}
let rec find_same id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.layer with
| Open {next; _} -> find_same id next
| Map {f; next} -> f (find_same id next)
| Nothing -> raise exn
end
let rec find_name wrap ~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.layer with
| Open {using; root; next; components} ->
begin try
let descr = wrap (NameMap.find name components) in
let res = Pdot (root, name), descr in
if mark then begin match using with
| None -> ()
| Some f -> begin
match find_name wrap ~mark:false name next with
| exception Not_found -> f name None
| _, descr' -> f name (Some (descr', descr))
end
end;
res
with Not_found ->
find_name wrap ~mark name next
end
| Map {f; next} ->
let (p, desc) = find_name wrap ~mark name next in
p, f desc
| Nothing ->
raise exn
end
let rec find_all wrap name tbl =
List.map
(fun (id, desc) -> Pident id, desc)
(Ident.find_all name tbl.current) @
match tbl.layer with
| Nothing -> []
| Open {root; using = _; next; components} ->
begin try
let desc = wrap (NameMap.find name components) in
(Pdot (root, name), desc) :: find_all wrap name next
with Not_found ->
find_all wrap name next
end
| Map {f; next} ->
List.map (fun (p, desc) -> (p, f desc))
(find_all wrap name next)
let rec find_all_idents name tbl () =
let current =
Ident.find_all_seq name tbl.current
|> Seq.map (fun (id, _) -> Some id)
in
let next () =
match tbl.layer with
| Nothing -> Seq.Nil
| Open { next; components; _ } ->
if NameMap.mem name components then
Seq.Cons(None, find_all_idents name next)
else
find_all_idents name next ()
| Map {next; _ } -> find_all_idents name next ()
in
Seq.append current next ()
let rec fold_name wrap f tbl acc =
let acc =
Ident.fold_name
(fun id d -> f (Ident.name id) (Pident id, d))
tbl.current acc
in
match tbl.layer with
| Open {root; using = _; next; components} ->
acc
|> NameMap.fold
(fun name desc -> f name (Pdot (root, name), wrap desc))
components
|> fold_name wrap f next
| Nothing ->
acc
| Map {f=g; next} ->
acc
|> fold_name wrap
(fun name (path, desc) -> f name (path, g desc))
next
let rec local_keys tbl acc =
let acc = Ident.fold_all (fun k _ accu -> k::accu) tbl.current acc in
match tbl.layer with
| Open {next; _ } | Map {next; _} -> local_keys next acc
| Nothing -> acc
let rec iter wrap f tbl =
Ident.iter (fun id desc -> f id (Pident id, desc)) tbl.current;
match tbl.layer with
| Open {root; using = _; next; components} ->
NameMap.iter
(fun s x ->
let root_scope = Path.scope root in
f (Ident.create_scoped ~scope:root_scope s)
(Pdot (root, s), wrap x))
components;
iter wrap f next
| Map {f=g; next} ->
iter wrap (fun id (path, desc) -> f id (path, g desc)) next
| Nothing -> ()
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_descr_kind =
(label_description, constructor_description) type_kind
type type_descriptions = type_descr_kind
let in_signature_flag = 0x01
type t = {
values: (value_entry, value_data) IdTbl.t;
constrs: constructor_data TycompTbl.t;
labels: label_data TycompTbl.t;
types: (type_data, type_data) IdTbl.t;
modules: (module_entry, module_data) IdTbl.t;
modtypes: (modtype_data, modtype_data) IdTbl.t;
classes: (class_data, class_data) IdTbl.t;
cltypes: (cltype_data, cltype_data) IdTbl.t;
functor_args: unit Ident.tbl;
summary: summary;
local_constraints: type_declaration Path.Map.t;
flags: int;
}
and module_components =
{
alerts: alerts;
uid: Uid.t;
comps:
(components_maker,
(module_components_repr, module_components_failure) result)
Lazy_backtrack.t;
}
and components_maker = {
cm_env: t;
cm_prefixing_subst: Subst.t;
cm_path: Path.t;
cm_addr: address_lazy;
cm_mty: Subst.Lazy.modtype;
cm_shape: Shape.t;
}
and module_components_repr =
Structure_comps of structure_components
| Functor_comps of functor_components
and module_components_failure =
| No_components_abstract
| No_components_alias of Path.t
and structure_components = {
mutable comp_values: value_data NameMap.t;
mutable comp_constrs: constructor_data list NameMap.t;
mutable comp_labels: label_data list NameMap.t;
mutable comp_types: type_data NameMap.t;
mutable comp_modules: module_data NameMap.t;
mutable comp_modtypes: modtype_data NameMap.t;
mutable comp_classes: class_data NameMap.t;
mutable comp_cltypes: cltype_data NameMap.t;
}
and functor_components = {
fcomp_arg: functor_parameter;
(* Formal parameter and argument signature *)
fcomp_res: module_type; (* Result signature *)
fcomp_shape: Shape.t;
fcomp_cache: (Path.t, module_components) Hashtbl.t; (* For memoization *)
fcomp_subst_cache: (Path.t, module_type) Hashtbl.t
}
and address_unforced =
| Projection of { parent : address_lazy; pos : int; }
| ModAlias of { env : t; path : Path.t; }
and address_lazy = (address_unforced, address) Lazy_backtrack.t
and value_data =
{ vda_description : value_description;
vda_address : address_lazy;
vda_shape : Shape.t }
and value_entry =
| Val_bound of value_data
| Val_unbound of value_unbound_reason
and constructor_data =
{ cda_description : constructor_description;
cda_address : address_lazy option;
cda_shape: Shape.t; }
and label_data = label_description
and type_data =
{ tda_declaration : type_declaration;
tda_descriptions : type_descriptions;
tda_shape : Shape.t; }
and module_data =
{ mda_declaration : Subst.Lazy.module_decl;
mda_components : module_components;
mda_address : address_lazy;
mda_shape: Shape.t; }
and module_entry =
| Mod_local of module_data
| Mod_persistent
| Mod_unbound of module_unbound_reason
and modtype_data =
{ mtda_declaration : Subst.Lazy.modtype_declaration;
mtda_shape : Shape.t; }
and class_data =
{ clda_declaration : class_declaration;
clda_address : address_lazy;
clda_shape : Shape.t }
and cltype_data =
{ cltda_declaration : class_type_declaration;
cltda_shape : Shape.t }
let empty_structure =
Structure_comps {
comp_values = NameMap.empty;
comp_constrs = NameMap.empty;
comp_labels = NameMap.empty;
comp_types = NameMap.empty;
comp_modules = NameMap.empty; comp_modtypes = NameMap.empty;
comp_classes = NameMap.empty;
comp_cltypes = NameMap.empty }
type unbound_value_hint =
| No_hint
| Missing_rec of Location.t
type lookup_error =
| Unbound_value of Longident.t * unbound_value_hint
| Unbound_type of Longident.t
| Unbound_constructor of Longident.t
| Unbound_label of Longident.t
| Unbound_module of Longident.t
| Unbound_class of Longident.t
| Unbound_modtype of Longident.t
| Unbound_cltype of Longident.t
| Unbound_instance_variable of string
| Not_an_instance_variable of string
| Masked_instance_variable of Longident.t
| Masked_self_variable of Longident.t
| Masked_ancestor_variable of Longident.t
| Structure_used_as_functor of Longident.t
| Abstract_used_as_functor of Longident.t
| Functor_used_as_structure of Longident.t
| Abstract_used_as_structure of Longident.t
| Generative_used_as_applicative of Longident.t
| Illegal_reference_to_recursive_module
| Cannot_scrape_alias of Longident.t * Path.t
type error =
| Missing_module of Location.t * Path.t * Path.t
| Illegal_value_name of Location.t * string
| Lookup_error of Location.t * t * lookup_error
exception Error of error
let error err = raise (Error err)
let lookup_error loc env err =
error (Lookup_error(loc, env, err))
let same_type_declarations e1 e2 =
e1.types == e2.types &&
e1.modules == e2.modules &&
e1.local_constraints == e2.local_constraints
let same_constr = ref (fun _ _ _ -> assert false)
let check_well_formed_module = 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 (cda1, cda2))
when not (!same_constr env
cda1.cda_description.cstr_res
cda2.cda_description.cstr_res) ->
Some "constructor"
| `Label (Some (l1, l2))
when not (!same_constr env l1.lbl_res l2.lbl_res) ->
Some "label"
| `Value (Some (Val_unbound _, _)) -> None
| `Value (Some (_, _)) -> Some "value"
| `Type (Some _) -> Some "type"
| `Module (Some (Mod_unbound _, _)) -> None
| `Module (Some _) | `Component (Some _) ->
Some "module"
| `Module_type (Some _) -> Some "module type"
| `Class (Some _) -> Some "class"
| `Class_type (Some _) -> Some "class type"
| `Constructor _ | `Label _
| `Value None | `Type None | `Module None | `Module_type None
| `Class None | `Class_type None | `Component None ->
None
let empty = {
values = IdTbl.empty; constrs = TycompTbl.empty;
labels = TycompTbl.empty; types = IdTbl.empty;
modules = IdTbl.empty; modtypes = IdTbl.empty;
classes = IdTbl.empty; cltypes = IdTbl.empty;
summary = Env_empty; local_constraints = Path.Map.empty;
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 is_in_signature env = env.flags land in_signature_flag <> 0
let has_local_constraints env =
not (Path.Map.is_empty env.local_constraints)
let is_ext cda =
match cda.cda_description with
| {cstr_tag = Cstr_extension _} -> true
| _ -> false
let is_local_ext cda =
match cda.cda_description with
| {cstr_tag = Cstr_extension(p, _)} -> begin
match p with
| Pident _ -> true
| Pdot _ | Papply _ | Pextra_ty _ -> false
end
| _ -> false
let diff env1 env2 =
IdTbl.diff_keys env1.values env2.values @
TycompTbl.diff_keys is_local_ext env1.constrs env2.constrs @
IdTbl.diff_keys env1.modules env2.modules @
IdTbl.diff_keys env1.classes env2.classes
(* Functions for use in "wrap" parameters in IdTbl *)
let wrap_identity x = x
let wrap_value vda = Val_bound vda
let wrap_module mda = Mod_local mda
(* Forward declarations *)
let components_of_module_maker' =
ref ((fun _ -> assert false) :
components_maker ->
(module_components_repr, module_components_failure) result)
let components_of_functor_appl' =
ref ((fun ~loc:_ ~f_path:_ ~f_comp:_ ~arg:_ _env -> assert false) :
loc:Location.t -> f_path:Path.t -> f_comp:functor_components ->
arg:Path.t -> t -> module_components)
let check_functor_application =
(* to be filled by Includemod *)
ref ((fun ~errors:_ ~loc:_
~lid_whole_app:_ ~f0_path:_ ~args:_
~arg_path:_ ~arg_mty:_ ~param_mty:_
_env
-> assert false) :
errors:bool -> loc:Location.t ->
lid_whole_app:Longident.t ->
f0_path:Path.t -> args:(Path.t * Types.module_type) list ->
arg_path:Path.t -> arg_mty:module_type -> param_mty:module_type ->
t -> unit)
let strengthen =
(* to be filled with Mtype.strengthen *)
ref ((fun ~aliasable:_ _env _mty _path -> assert false) :
aliasable:bool -> t -> Subst.Lazy.modtype ->
Path.t -> Subst.Lazy.modtype)
let md md_type =
{md_type; md_attributes=[]; md_loc=Location.none
;md_uid = Uid.internal_not_actually_unique}
(* Print addresses *)
let rec print_address ppf = function
| Aident id -> Format.fprintf ppf "%s" (Ident.name id)
| Adot(a, pos) -> Format.fprintf ppf "%a.[%i]" print_address a pos
(* The name of the compilation unit currently compiled.
"" if outside a compilation unit. *)
module Current_unit_name : sig
val get : unit -> modname
val set : modname -> unit
val is : modname -> bool
val is_ident : Ident.t -> bool
val is_path : Path.t -> bool
end = struct
let current_unit =
ref ""
let get () =
!current_unit
let set name =
current_unit := name
let is name =
!current_unit = name
let is_ident id =
Ident.persistent id && is (Ident.name id)
let is_path = function
| Pident id -> is_ident id
| Pdot _ | Papply _ | Pextra_ty _ -> false
end
let set_unit_name = Current_unit_name.set
let get_unit_name = Current_unit_name.get
let find_same_module id tbl =
match IdTbl.find_same id tbl with
| x -> x
| exception Not_found
when Ident.persistent id && not (Current_unit_name.is_ident id) ->
Mod_persistent
let find_name_module ~mark name tbl =
match IdTbl.find_name wrap_module ~mark name tbl with
| x -> x
| exception Not_found when not (Current_unit_name.is name) ->
let path = Pident(Ident.create_persistent name) in
path, Mod_persistent
let add_persistent_structure id env =
if not (Ident.persistent id) then invalid_arg "Env.add_persistent_structure";
if Current_unit_name.is_ident id then env
else begin
let material =
(* This addition only observably changes the environment if it shadows a
non-persistent module already in the environment.
(See PR#9345) *)
match
IdTbl.find_name wrap_module ~mark:false (Ident.name id) env.modules
with
| exception Not_found | _, Mod_persistent -> false
| _ -> true
in
let summary =
if material then Env_persistent (env.summary, id)
else env.summary
in
let modules =
(* With [-no-alias-deps], non-material additions should not
affect the environment at all. We should only observe the
existence of a cmi when accessing components of the module.
(See #9991). *)
if material || not !Clflags.transparent_modules then
IdTbl.add id Mod_persistent env.modules
else
env.modules
in
{ env with modules; summary }
end
let components_of_module ~alerts ~uid env ps path addr mty shape =
{
alerts;
uid;
comps = Lazy_backtrack.create {
cm_env = env;
cm_prefixing_subst = ps;
cm_path = path;
cm_addr = addr;
cm_mty = mty;
cm_shape = shape;
}
}
let sign_of_cmi ~freshen { Persistent_env.Persistent_signature.cmi; _ } =
let name = cmi.cmi_name in
let sign = cmi.cmi_sign in
let flags = cmi.cmi_flags in
let id = Ident.create_persistent name in
let path = Pident id in
let alerts =
List.fold_left (fun acc -> function Alerts s -> s | _ -> acc)
Misc.Stdlib.String.Map.empty
flags
in
let md =
{ md_type = Mty_signature sign;
md_loc = Location.none;
md_attributes = [];
md_uid = Uid.of_compilation_unit_id id;
}
in
let mda_address = Lazy_backtrack.create_forced (Aident id) in
let mda_declaration =
Subst.(Lazy.module_decl Make_local identity (Lazy.of_module_decl md))
in
let mda_shape = Shape.for_persistent_unit name in
let mda_components =
let mty = Subst.Lazy.of_modtype (Mty_signature sign) in
let mty =
if freshen then
Subst.Lazy.modtype (Subst.Rescope (Path.scope path))
Subst.identity mty
else mty
in
components_of_module ~alerts ~uid:md.md_uid
empty Subst.identity
path mda_address mty mda_shape
in
{
mda_declaration;
mda_components;
mda_address;
mda_shape;
}
let read_sign_of_cmi = sign_of_cmi ~freshen:true
let save_sign_of_cmi = sign_of_cmi ~freshen:false
let persistent_env : module_data Persistent_env.t ref =
s_table Persistent_env.empty ()
let without_cmis f x =
Persistent_env.without_cmis !persistent_env f x
let imports () = Persistent_env.imports !persistent_env
let import_crcs ~source crcs =
Persistent_env.import_crcs !persistent_env ~source crcs
let read_pers_mod cmi =
Persistent_env.read !persistent_env read_sign_of_cmi cmi
let find_pers_mod name =
Persistent_env.find !persistent_env read_sign_of_cmi name
let check_pers_mod ~loc name =
Persistent_env.check !persistent_env read_sign_of_cmi ~loc name
let crc_of_unit name =
Persistent_env.crc_of_unit !persistent_env read_sign_of_cmi name
let is_imported_opaque modname =
Persistent_env.is_imported_opaque !persistent_env modname
let register_import_as_opaque modname =
Persistent_env.register_import_as_opaque !persistent_env modname
let reset_declaration_caches () =
Types.Uid.Tbl.clear !value_declarations;
Types.Uid.Tbl.clear !type_declarations;
Types.Uid.Tbl.clear !module_declarations;
Types.Uid.Tbl.clear !used_constructors;
Types.Uid.Tbl.clear !used_labels;
()
let reset_cache () =
Current_unit_name.set "";
Persistent_env.clear !persistent_env;
reset_declaration_caches ();
()
let reset_cache_toplevel () =
Persistent_env.clear_missing !persistent_env;
reset_declaration_caches ();
()
(* get_components *)
let get_components_res c =
match Persistent_env.can_load_cmis !persistent_env with
| Persistent_env.Can_load_cmis ->
Lazy_backtrack.force !components_of_module_maker' c.comps
| Persistent_env.Cannot_load_cmis log ->
Lazy_backtrack.force_logged log !components_of_module_maker' c.comps
let get_components c =
match get_components_res c with
| Error _ -> empty_structure
| Ok c -> c
(* Module type of functor application *)
let modtype_of_functor_appl fcomp p1 p2 =
match fcomp.fcomp_res with
| Mty_alias _ as mty -> mty
| mty ->
try
Hashtbl.find fcomp.fcomp_subst_cache p2
with Not_found ->
let scope = Path.scope (Papply(p1, p2)) in
let mty =
let subst =