forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.ml
2315 lines (2023 loc) · 72 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 Config
open Misc
open Asttypes
open Longident
open Path
open Types
open Btype
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
| 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 unit
| 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 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: (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
Ext_list.filter keys2
(fun id ->
is_local (find_same id tbl2) &&
try ignore (find_same id tbl1); false
with Not_found -> true)
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
Ext_list.filter keys2
(fun id ->
try ignore (find_same id tbl1); false
with Not_found -> true)
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;
comp_classes: class_declaration comp_tbl; (* warning -69*)
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 clear_imports () =
Consistbl.clear crc_units;
imported_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);
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 _ -> function Deprecated s -> Some s ) 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));
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
| 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 succeeds, to help make builds more
deterministic. *)
add_import name;
if (Warnings.is_active (Warnings.No_cmi_file("", None))) then
Delayed_checks.add_delayed_check
(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 =
Ext_list.filter
(try Tbl.find_str s comps.comp_constrs
with Not_found -> assert false)
(function {cstr_tag=Cstr_extension _} -> true | _ -> 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 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)} ->
normalize_path lax env path1
| _ -> 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