1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Environment handling *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
open Config
|
|
|
|
open Misc
|
|
|
|
open Asttypes
|
|
|
|
open Longident
|
|
|
|
open Path
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
type error =
|
1995-11-03 05:23:03 -08:00
|
|
|
Not_an_interface of string
|
1995-05-04 03:15:53 -07:00
|
|
|
| Corrupted_interface of string
|
|
|
|
| Illegal_renaming of string * string
|
1997-05-15 06:30:02 -07:00
|
|
|
| Inconsistent_import of string * string * string
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Error of error
|
|
|
|
|
1996-11-29 10:36:42 -08:00
|
|
|
type summary =
|
|
|
|
Env_empty
|
|
|
|
| Env_value of summary * Ident.t * value_description
|
|
|
|
| Env_type of summary * Ident.t * type_declaration
|
|
|
|
| Env_exception of summary * Ident.t * exception_declaration
|
|
|
|
| Env_module of summary * Ident.t * module_type
|
|
|
|
| Env_modtype of summary * Ident.t * modtype_declaration
|
|
|
|
| Env_class of summary * Ident.t * class_type
|
|
|
|
| Env_open of summary * Path.t
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
type t = {
|
|
|
|
values: (Path.t * value_description) Ident.tbl;
|
|
|
|
constrs: constructor_description Ident.tbl;
|
|
|
|
labels: label_description Ident.tbl;
|
|
|
|
types: (Path.t * type_declaration) Ident.tbl;
|
|
|
|
modules: (Path.t * module_type) Ident.tbl;
|
|
|
|
modtypes: (Path.t * modtype_declaration) Ident.tbl;
|
1996-04-22 04:15:41 -07:00
|
|
|
components: (Path.t * module_components) Ident.tbl;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes: (Path.t * class_type) Ident.tbl;
|
|
|
|
summary: summary
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1995-08-23 04:55:54 -07:00
|
|
|
and module_components =
|
|
|
|
Structure_comps of structure_components
|
|
|
|
| Functor_comps of functor_components
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
and structure_components = {
|
|
|
|
mutable comp_values: (string, (value_description * int)) Tbl.t;
|
|
|
|
mutable comp_constrs: (string, (constructor_description * int)) Tbl.t;
|
|
|
|
mutable comp_labels: (string, (label_description * int)) Tbl.t;
|
|
|
|
mutable comp_types: (string, (type_declaration * int)) Tbl.t;
|
|
|
|
mutable comp_modules: (string, (module_type * int)) Tbl.t;
|
|
|
|
mutable comp_modtypes: (string, (modtype_declaration * int)) Tbl.t;
|
1996-04-22 04:15:41 -07:00
|
|
|
mutable comp_components: (string, (module_components * int)) Tbl.t;
|
|
|
|
mutable comp_classes: (string, (class_type * int)) Tbl.t
|
1995-08-23 04:55:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
and functor_components = {
|
|
|
|
fcomp_param: Ident.t;
|
|
|
|
fcomp_arg: module_type;
|
|
|
|
fcomp_res: module_type;
|
|
|
|
fcomp_env: t
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let empty = {
|
|
|
|
values = Ident.empty; constrs = Ident.empty;
|
|
|
|
labels = Ident.empty; types = Ident.empty;
|
|
|
|
modules = Ident.empty; modtypes = Ident.empty;
|
1996-11-29 10:36:42 -08:00
|
|
|
components = Ident.empty; classes = Ident.empty;
|
1997-03-24 12:12:16 -08:00
|
|
|
summary = Env_empty }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Persistent structure descriptions *)
|
|
|
|
|
|
|
|
type pers_struct =
|
|
|
|
{ ps_name: string;
|
|
|
|
ps_sig: signature;
|
1997-04-01 12:53:02 -08:00
|
|
|
ps_comps: module_components;
|
1997-05-15 06:30:02 -07:00
|
|
|
ps_crcs: (string * Digest.t) list }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let persistent_structures =
|
1996-04-22 04:15:41 -07:00
|
|
|
(Hashtbl.create 17 : (string, pers_struct) Hashtbl.t)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let read_pers_struct modname filename =
|
|
|
|
let ic = open_in_bin filename in
|
|
|
|
try
|
|
|
|
let buffer = String.create (String.length cmi_magic_number) in
|
|
|
|
really_input ic buffer 0 (String.length cmi_magic_number);
|
1995-07-02 09:50:08 -07:00
|
|
|
if buffer <> cmi_magic_number then begin
|
|
|
|
close_in ic;
|
|
|
|
raise(Error(Not_an_interface filename))
|
|
|
|
end;
|
1997-04-01 12:53:02 -08:00
|
|
|
let (name, sign, comps) = input_value ic in
|
1997-05-15 06:30:02 -07:00
|
|
|
let crcs = input_value ic in
|
|
|
|
close_in ic;
|
1997-04-01 12:53:02 -08:00
|
|
|
let ps = { ps_name = name;
|
|
|
|
ps_sig = sign;
|
|
|
|
ps_comps = comps;
|
1997-05-15 06:30:02 -07:00
|
|
|
ps_crcs = crcs } in
|
1995-05-04 03:15:53 -07:00
|
|
|
if ps.ps_name <> modname then
|
1996-02-20 03:01:45 -08:00
|
|
|
raise(Error(Illegal_renaming(ps.ps_name, filename)));
|
1997-05-15 06:30:02 -07:00
|
|
|
Hashtbl.add persistent_structures modname ps;
|
1997-04-01 12:53:02 -08:00
|
|
|
ps
|
1995-05-04 03:15:53 -07:00
|
|
|
with End_of_file | Failure _ ->
|
1995-07-02 09:50:08 -07:00
|
|
|
close_in ic;
|
1995-05-04 03:15:53 -07:00
|
|
|
raise(Error(Corrupted_interface(filename)))
|
|
|
|
|
|
|
|
let find_pers_struct name =
|
|
|
|
try
|
|
|
|
Hashtbl.find persistent_structures name
|
|
|
|
with Not_found ->
|
1997-05-15 06:30:02 -07:00
|
|
|
read_pers_struct name
|
|
|
|
(find_in_path !load_path (String.uncapitalize name ^ ".cmi"))
|
1997-04-01 12:53:02 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let reset_cache() =
|
1997-04-01 12:53:02 -08:00
|
|
|
Hashtbl.clear persistent_structures
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-08-23 04:55:54 -07:00
|
|
|
(* Forward declarations *)
|
|
|
|
|
|
|
|
let components_of_functor_appl =
|
|
|
|
ref ((fun f p1 p2 -> fatal_error "Env.components_of_functor_appl") :
|
|
|
|
functor_components -> Path.t -> Path.t -> module_components)
|
|
|
|
|
|
|
|
let check_modtype_inclusion =
|
|
|
|
(* to be filled with includemod.check_modtype_inclusion *)
|
|
|
|
ref ((fun env mty1 mty2 -> fatal_error "Env.include_modtypes") :
|
|
|
|
t -> module_type -> module_type -> unit)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Lookup by identifier *)
|
|
|
|
|
|
|
|
let rec find_module_descr path env =
|
|
|
|
match path with
|
|
|
|
Pident id ->
|
|
|
|
begin try
|
|
|
|
let (p, desc) = Ident.find_same id env.components
|
1997-05-15 06:30:02 -07:00
|
|
|
in desc
|
1995-05-04 03:15:53 -07:00
|
|
|
with Not_found ->
|
1997-05-15 06:30:02 -07:00
|
|
|
if Ident.persistent id
|
|
|
|
then (find_pers_struct (Ident.name id)).ps_comps
|
1995-05-04 03:15:53 -07:00
|
|
|
else raise Not_found
|
|
|
|
end
|
|
|
|
| Pdot(p, s, pos) ->
|
1995-08-23 04:55:54 -07:00
|
|
|
begin match find_module_descr p env with
|
1997-05-19 08:42:21 -07:00
|
|
|
Structure_comps c ->
|
|
|
|
let (descr, pos) = Tbl.find s c.comp_components in
|
1997-05-15 06:30:02 -07:00
|
|
|
descr
|
|
|
|
| Functor_comps f ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
begin match find_module_descr p1 env with
|
1997-05-19 08:42:21 -07:00
|
|
|
Functor_comps f ->
|
1997-05-15 06:30:02 -07:00
|
|
|
!components_of_functor_appl f p1 p2
|
|
|
|
| Structure_comps c ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-05-15 06:30:02 -07:00
|
|
|
let find proj1 proj2 path env =
|
1995-11-03 05:23:03 -08:00
|
|
|
match path with
|
|
|
|
Pident id ->
|
|
|
|
let (p, data) = Ident.find_same id (proj1 env)
|
1997-05-15 06:30:02 -07:00
|
|
|
in data
|
1995-11-03 05:23:03 -08:00
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
begin match find_module_descr p env with
|
1997-05-15 06:30:02 -07:00
|
|
|
Structure_comps c ->
|
|
|
|
let (data, pos) = Tbl.find s (proj2 c) in data
|
|
|
|
| Functor_comps f ->
|
1995-11-03 05:23:03 -08:00
|
|
|
raise Not_found
|
|
|
|
end
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
raise Not_found
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-04-01 12:53:02 -08:00
|
|
|
let find_value =
|
1997-05-15 06:30:02 -07:00
|
|
|
find (fun env -> env.values) (fun sc -> sc.comp_values)
|
1997-04-01 12:53:02 -08:00
|
|
|
and find_type =
|
|
|
|
find (fun env -> env.types) (fun sc -> sc.comp_types)
|
1997-05-15 06:30:02 -07:00
|
|
|
and find_modtype =
|
1997-04-01 12:53:02 -08:00
|
|
|
find (fun env -> env.modtypes) (fun sc -> sc.comp_modtypes)
|
1997-05-15 06:30:02 -07:00
|
|
|
and find_class =
|
|
|
|
find (fun env -> env.classes) (fun sc -> sc.comp_classes)
|
|
|
|
|
|
|
|
let find_type_expansion path env =
|
|
|
|
let decl = find_type path env in
|
|
|
|
match decl.type_manifest with
|
|
|
|
None -> raise Not_found
|
|
|
|
| Some body -> (decl.type_params, body)
|
|
|
|
|
|
|
|
let find_modtype_expansion path env =
|
|
|
|
match find_modtype path env with
|
|
|
|
Tmodtype_abstract -> raise Not_found
|
|
|
|
| Tmodtype_manifest mty -> mty
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-11-29 10:36:42 -08:00
|
|
|
let find_module path env =
|
|
|
|
match path with
|
|
|
|
Pident id ->
|
|
|
|
begin try
|
|
|
|
let (p, data) = Ident.find_same id env.modules
|
|
|
|
in data
|
|
|
|
with Not_found ->
|
1997-04-01 12:53:02 -08:00
|
|
|
if Ident.persistent id then
|
|
|
|
let ps = find_pers_struct (Ident.name id) in
|
1997-05-15 06:30:02 -07:00
|
|
|
Tmty_signature(ps.ps_sig)
|
1996-11-29 10:36:42 -08:00
|
|
|
else raise Not_found
|
|
|
|
end
|
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
begin match find_module_descr p env with
|
1997-05-15 06:30:02 -07:00
|
|
|
Structure_comps c ->
|
1996-11-29 10:36:42 -08:00
|
|
|
let (data, pos) = Tbl.find s c.comp_modules in data
|
1997-05-15 06:30:02 -07:00
|
|
|
| Functor_comps f ->
|
1996-11-29 10:36:42 -08:00
|
|
|
raise Not_found
|
|
|
|
end
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
raise Not_found (* not right *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Lookup by name *)
|
|
|
|
|
|
|
|
let rec lookup_module_descr lid env =
|
|
|
|
match lid with
|
|
|
|
Lident s ->
|
|
|
|
begin try
|
|
|
|
Ident.find_name s env.components
|
|
|
|
with Not_found ->
|
1997-04-01 12:53:02 -08:00
|
|
|
let ps = find_pers_struct s in
|
|
|
|
(Pident(Ident.create_persistent s), ps.ps_comps)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
1995-08-23 04:55:54 -07:00
|
|
|
| Ldot(l, s) ->
|
|
|
|
let (p, descr) = lookup_module_descr l env in
|
|
|
|
begin match descr with
|
1997-05-19 08:42:21 -07:00
|
|
|
Structure_comps c ->
|
|
|
|
let (descr, pos) = Tbl.find s c.comp_components in
|
1995-08-23 04:55:54 -07:00
|
|
|
(Pdot(p, s, pos), descr)
|
|
|
|
| Functor_comps f ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Lapply(l1, l2) ->
|
|
|
|
let (p1, desc1) = lookup_module_descr l1 env in
|
|
|
|
let (p2, mty2) = lookup_module l2 env in
|
|
|
|
begin match desc1 with
|
1997-05-19 08:42:21 -07:00
|
|
|
Functor_comps f ->
|
1995-08-23 04:55:54 -07:00
|
|
|
!check_modtype_inclusion env mty2 f.fcomp_arg;
|
|
|
|
(Papply(p1, p2), !components_of_functor_appl f p1 p2)
|
|
|
|
| Structure_comps c ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
and lookup_module lid env =
|
|
|
|
match lid with
|
|
|
|
Lident s ->
|
|
|
|
begin try
|
|
|
|
Ident.find_name s env.modules
|
|
|
|
with Not_found ->
|
1997-04-01 12:53:02 -08:00
|
|
|
let ps = find_pers_struct s in
|
|
|
|
(Pident(Ident.create_persistent s), Tmty_signature ps.ps_sig)
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Ldot(l, s) ->
|
|
|
|
let (p, descr) = lookup_module_descr l env in
|
|
|
|
begin match descr with
|
1997-05-19 08:42:21 -07:00
|
|
|
Structure_comps c ->
|
1995-08-23 04:55:54 -07:00
|
|
|
let (data, pos) = Tbl.find s c.comp_modules in
|
|
|
|
(Pdot(p, s, pos), data)
|
|
|
|
| Functor_comps f ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Lapply(l1, l2) ->
|
|
|
|
let (p1, desc1) = lookup_module_descr l1 env in
|
|
|
|
let (p2, mty2) = lookup_module l2 env in
|
|
|
|
let p = Papply(p1, p2) in
|
|
|
|
begin match desc1 with
|
1997-05-19 08:42:21 -07:00
|
|
|
Functor_comps f ->
|
1995-08-23 04:55:54 -07:00
|
|
|
!check_modtype_inclusion env mty2 f.fcomp_arg;
|
|
|
|
(p, Subst.modtype (Subst.add_module f.fcomp_param p2 Subst.identity)
|
1997-05-19 08:42:21 -07:00
|
|
|
f.fcomp_res)
|
1995-08-23 04:55:54 -07:00
|
|
|
| Structure_comps c ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lookup proj1 proj2 lid env =
|
|
|
|
match lid with
|
|
|
|
Lident s ->
|
|
|
|
Ident.find_name s (proj1 env)
|
1995-08-23 04:55:54 -07:00
|
|
|
| Ldot(l, s) ->
|
|
|
|
begin match lookup_module_descr l env with
|
1997-05-19 08:42:21 -07:00
|
|
|
(p, Structure_comps c) ->
|
|
|
|
let (data, pos) = Tbl.find s (proj2 c) in
|
1995-08-23 04:55:54 -07:00
|
|
|
(Pdot(p, s, pos), data)
|
|
|
|
| (p, Functor_comps f) ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Lapply(l1, l2) ->
|
|
|
|
raise Not_found
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lookup_simple proj1 proj2 lid env =
|
|
|
|
match lid with
|
|
|
|
Lident s ->
|
|
|
|
Ident.find_name s (proj1 env)
|
1995-08-23 04:55:54 -07:00
|
|
|
| Ldot(l, s) ->
|
|
|
|
begin match lookup_module_descr l env with
|
1997-05-19 08:42:21 -07:00
|
|
|
(p, Structure_comps c) ->
|
|
|
|
let (data, pos) = Tbl.find s (proj2 c) in
|
1995-08-23 04:55:54 -07:00
|
|
|
data
|
|
|
|
| (p, Functor_comps f) ->
|
1997-05-19 08:42:21 -07:00
|
|
|
raise Not_found
|
1995-08-23 04:55:54 -07:00
|
|
|
end
|
|
|
|
| Lapply(l1, l2) ->
|
|
|
|
raise Not_found
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lookup_value =
|
|
|
|
lookup (fun env -> env.values) (fun sc -> sc.comp_values)
|
|
|
|
and lookup_constructor =
|
|
|
|
lookup_simple (fun env -> env.constrs) (fun sc -> sc.comp_constrs)
|
|
|
|
and lookup_label =
|
|
|
|
lookup_simple (fun env -> env.labels) (fun sc -> sc.comp_labels)
|
|
|
|
and lookup_type =
|
|
|
|
lookup (fun env -> env.types) (fun sc -> sc.comp_types)
|
|
|
|
and lookup_modtype =
|
|
|
|
lookup (fun env -> env.modtypes) (fun sc -> sc.comp_modtypes)
|
1996-04-22 04:15:41 -07:00
|
|
|
and lookup_class =
|
|
|
|
lookup (fun env -> env.classes) (fun sc -> sc.comp_classes)
|
1997-04-01 12:53:02 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Scrape a module type *)
|
|
|
|
|
|
|
|
let rec scrape_modtype mty env =
|
|
|
|
match mty with
|
|
|
|
Tmty_ident path ->
|
1997-04-01 12:53:02 -08:00
|
|
|
begin try
|
|
|
|
scrape_modtype (find_modtype_expansion path env) env
|
|
|
|
with Not_found ->
|
|
|
|
mty
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| _ -> mty
|
|
|
|
|
|
|
|
(* Compute constructor descriptions *)
|
|
|
|
|
|
|
|
let constructors_of_type ty_path decl =
|
|
|
|
match decl.type_kind with
|
|
|
|
Type_variant cstrs ->
|
1996-04-22 04:15:41 -07:00
|
|
|
Datarepr.constructor_descrs
|
1997-03-24 12:12:16 -08:00
|
|
|
(Btype.newgenty (Tconstr(ty_path, decl.type_params, ref Mnil)))
|
1996-04-22 04:15:41 -07:00
|
|
|
cstrs
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ -> []
|
|
|
|
|
|
|
|
(* Compute label descriptions *)
|
|
|
|
|
|
|
|
let labels_of_type ty_path decl =
|
|
|
|
match decl.type_kind with
|
|
|
|
Type_record labels ->
|
1996-04-22 04:15:41 -07:00
|
|
|
Datarepr.label_descrs
|
1997-03-24 12:12:16 -08:00
|
|
|
(Btype.newgenty (Tconstr(ty_path, decl.type_params, ref Mnil)))
|
1996-04-22 04:15:41 -07:00
|
|
|
labels
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ -> []
|
|
|
|
|
|
|
|
(* 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)
|
|
|
|
| Tsig_value(id, decl) :: rem ->
|
|
|
|
let p = Pdot(root, Ident.name id, pos) in
|
1996-04-22 04:15:41 -07:00
|
|
|
let nextpos = match decl.val_kind with Val_prim _ -> pos | _ -> pos+1 in
|
1995-10-23 09:59:41 -07:00
|
|
|
let (pl, final_sub) = prefix_idents root nextpos sub rem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(p::pl, final_sub)
|
|
|
|
| Tsig_type(id, decl) :: 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)
|
|
|
|
| Tsig_exception(id, decl) :: rem ->
|
|
|
|
let p = Pdot(root, Ident.name id, pos) in
|
|
|
|
let (pl, final_sub) = prefix_idents root (pos+1) sub rem in
|
|
|
|
(p::pl, final_sub)
|
|
|
|
| Tsig_module(id, mty) :: 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)
|
|
|
|
| Tsig_modtype(id, decl) :: rem ->
|
|
|
|
let p = Pdot(root, Ident.name id, nopos) in
|
|
|
|
let (pl, final_sub) =
|
|
|
|
prefix_idents root pos
|
|
|
|
(Subst.add_modtype id (Tmty_ident p) sub) rem in
|
|
|
|
(p::pl, final_sub)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tsig_class(id, decl) :: rem ->
|
|
|
|
let p = Pdot(root, Ident.name id, pos) in
|
|
|
|
let (pl, final_sub) =
|
|
|
|
prefix_idents root (pos + 1) sub rem
|
|
|
|
in
|
|
|
|
(p::pl, final_sub)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Compute structure descriptions *)
|
|
|
|
|
1995-09-23 02:08:18 -07:00
|
|
|
let rec components_of_module env sub path mty =
|
1995-08-23 04:55:54 -07:00
|
|
|
match scrape_modtype mty env with
|
1995-05-04 03:15:53 -07:00
|
|
|
Tmty_signature sg ->
|
1995-08-23 04:55:54 -07:00
|
|
|
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;
|
1996-04-22 04:15:41 -07:00
|
|
|
comp_components = Tbl.empty; comp_classes = Tbl.empty } in
|
1995-09-23 02:08:18 -07:00
|
|
|
let (pl, sub) = prefix_idents path 0 sub sg in
|
1995-05-04 03:15:53 -07:00
|
|
|
let env = ref env in
|
|
|
|
let pos = ref 0 in
|
|
|
|
List.iter2 (fun item path ->
|
|
|
|
match item with
|
|
|
|
Tsig_value(id, decl) ->
|
|
|
|
let decl' = Subst.value_description sub decl in
|
|
|
|
c.comp_values <-
|
|
|
|
Tbl.add (Ident.name id) (decl', !pos) c.comp_values;
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match decl.val_kind with
|
|
|
|
Val_prim _ -> () | _ -> incr pos
|
1995-10-23 09:59:41 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tsig_type(id, decl) ->
|
|
|
|
let decl' = Subst.type_declaration sub decl in
|
|
|
|
c.comp_types <-
|
|
|
|
Tbl.add (Ident.name id) (decl', nopos) c.comp_types;
|
|
|
|
List.iter
|
|
|
|
(fun (name, descr) ->
|
|
|
|
c.comp_constrs <- Tbl.add name (descr, nopos) c.comp_constrs)
|
|
|
|
(constructors_of_type path decl');
|
|
|
|
List.iter
|
|
|
|
(fun (name, descr) ->
|
|
|
|
c.comp_labels <- Tbl.add name (descr, nopos) c.comp_labels)
|
|
|
|
(labels_of_type path decl')
|
|
|
|
| Tsig_exception(id, decl) ->
|
|
|
|
let decl' = Subst.exception_declaration sub decl in
|
1995-07-25 04:40:07 -07:00
|
|
|
let cstr = Datarepr.exception_descr path decl' in
|
1995-05-04 03:15:53 -07:00
|
|
|
c.comp_constrs <-
|
|
|
|
Tbl.add (Ident.name id) (cstr, !pos) c.comp_constrs;
|
|
|
|
incr pos
|
|
|
|
| Tsig_module(id, mty) ->
|
|
|
|
let mty' = Subst.modtype sub mty in
|
|
|
|
c.comp_modules <-
|
|
|
|
Tbl.add (Ident.name id) (mty', !pos) c.comp_modules;
|
1995-09-23 02:08:18 -07:00
|
|
|
let comps = components_of_module !env sub path mty in
|
1995-05-04 03:15:53 -07:00
|
|
|
c.comp_components <-
|
|
|
|
Tbl.add (Ident.name id) (comps, !pos) c.comp_components;
|
|
|
|
env := store_components id path comps !env;
|
|
|
|
incr pos
|
|
|
|
| Tsig_modtype(id, decl) ->
|
|
|
|
let decl' = Subst.modtype_declaration sub decl in
|
|
|
|
c.comp_modtypes <-
|
|
|
|
Tbl.add (Ident.name id) (decl', nopos) c.comp_modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
env := store_modtype id path decl' !env
|
1997-05-19 08:42:21 -07:00
|
|
|
| Tsig_class(id, decl) ->
|
|
|
|
let decl' = Subst.class_type sub decl in
|
1996-04-22 04:15:41 -07:00
|
|
|
c.comp_classes <-
|
|
|
|
Tbl.add (Ident.name id) (decl', !pos) c.comp_classes;
|
1997-05-19 08:42:21 -07:00
|
|
|
incr pos)
|
1995-08-23 04:55:54 -07:00
|
|
|
sg pl;
|
1997-05-19 08:42:21 -07:00
|
|
|
Structure_comps c
|
1995-08-23 04:55:54 -07:00
|
|
|
| Tmty_functor(param, ty_arg, ty_res) ->
|
1997-05-19 08:42:21 -07:00
|
|
|
Functor_comps {
|
|
|
|
fcomp_param = param;
|
|
|
|
fcomp_arg = Subst.modtype sub ty_arg;
|
|
|
|
fcomp_res = Subst.modtype sub ty_res;
|
|
|
|
fcomp_env = env }
|
1995-08-23 04:55:54 -07:00
|
|
|
| Tmty_ident p ->
|
1997-05-19 08:42:21 -07:00
|
|
|
Structure_comps {
|
|
|
|
comp_values = Tbl.empty; comp_constrs = Tbl.empty;
|
1995-08-23 04:55:54 -07:00
|
|
|
comp_labels = Tbl.empty; comp_types = Tbl.empty;
|
|
|
|
comp_modules = Tbl.empty; comp_modtypes = Tbl.empty;
|
1996-04-22 04:15:41 -07:00
|
|
|
comp_components = Tbl.empty; comp_classes = Tbl.empty }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Insertion of bindings by identifier + path *)
|
|
|
|
|
|
|
|
and store_value id path decl env =
|
|
|
|
{ values = Ident.add id (path, decl) env.values;
|
|
|
|
constrs = env.constrs;
|
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = env.modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
components = env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = Env_value(env.summary, id, decl) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and store_type id path info env =
|
|
|
|
{ values = env.values;
|
|
|
|
constrs =
|
|
|
|
List.fold_right
|
|
|
|
(fun (name, descr) constrs ->
|
1996-04-22 04:15:41 -07:00
|
|
|
Ident.add (Ident.create name) descr constrs)
|
1995-05-04 03:15:53 -07:00
|
|
|
(constructors_of_type path info)
|
|
|
|
env.constrs;
|
|
|
|
labels =
|
|
|
|
List.fold_right
|
|
|
|
(fun (name, descr) labels ->
|
1996-04-22 04:15:41 -07:00
|
|
|
Ident.add (Ident.create name) descr labels)
|
1995-05-04 03:15:53 -07:00
|
|
|
(labels_of_type path info)
|
|
|
|
env.labels;
|
|
|
|
types = Ident.add id (path, info) env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = env.modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
components = env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = Env_type(env.summary, id, info) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and store_exception id path decl env =
|
|
|
|
{ values = env.values;
|
1995-07-25 04:40:07 -07:00
|
|
|
constrs = Ident.add id (Datarepr.exception_descr path decl) env.constrs;
|
1995-05-04 03:15:53 -07:00
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = env.modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
components = env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = Env_exception(env.summary, id, decl) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and store_module id path mty env =
|
|
|
|
{ values = env.values;
|
|
|
|
constrs = env.constrs;
|
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = Ident.add id (path, mty) env.modules;
|
|
|
|
modtypes = env.modtypes;
|
1995-09-23 02:08:18 -07:00
|
|
|
components =
|
|
|
|
Ident.add id (path, components_of_module env Subst.identity path mty)
|
1996-04-22 04:15:41 -07:00
|
|
|
env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = Env_module(env.summary, id, mty) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and store_modtype id path info env =
|
|
|
|
{ values = env.values;
|
|
|
|
constrs = env.constrs;
|
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = Ident.add id (path, info) env.modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
components = env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = Env_modtype(env.summary, id, info) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and store_components id path comps env =
|
|
|
|
{ values = env.values;
|
|
|
|
constrs = env.constrs;
|
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = env.modtypes;
|
1996-04-22 04:15:41 -07:00
|
|
|
components = Ident.add id (path, comps) env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = env.classes;
|
|
|
|
summary = env.summary }
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
and store_class id path desc env =
|
|
|
|
{ values = env.values;
|
|
|
|
constrs = env.constrs;
|
|
|
|
labels = env.labels;
|
|
|
|
types = env.types;
|
|
|
|
modules = env.modules;
|
|
|
|
modtypes = env.modtypes;
|
|
|
|
components = env.components;
|
1996-11-29 10:36:42 -08:00
|
|
|
classes = Ident.add id (path, desc) env.classes;
|
|
|
|
summary = Env_class(env.summary, id, desc) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-08-23 04:55:54 -07:00
|
|
|
(* Memoized function to compute the components of a functor application
|
|
|
|
in a path. *)
|
|
|
|
|
|
|
|
let funappl_memo =
|
1996-04-22 04:15:41 -07:00
|
|
|
(Hashtbl.create 17 : (Path.t, module_components) Hashtbl.t)
|
1995-08-23 04:55:54 -07:00
|
|
|
|
|
|
|
let _ =
|
|
|
|
components_of_functor_appl :=
|
|
|
|
(fun f p1 p2 ->
|
|
|
|
let p = Papply(p1, p2) in
|
|
|
|
try
|
|
|
|
Hashtbl.find funappl_memo p
|
|
|
|
with Not_found ->
|
|
|
|
let mty =
|
|
|
|
Subst.modtype (Subst.add_module f.fcomp_param p2 Subst.identity)
|
|
|
|
f.fcomp_res in
|
1995-09-23 02:08:18 -07:00
|
|
|
let comps = components_of_module f.fcomp_env Subst.identity p mty in
|
1995-08-23 04:55:54 -07:00
|
|
|
Hashtbl.add funappl_memo p comps;
|
|
|
|
comps)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Insertion of bindings by identifier *)
|
|
|
|
|
|
|
|
let add_value id desc env =
|
|
|
|
store_value id (Pident id) desc env
|
|
|
|
|
|
|
|
and add_type id info env =
|
|
|
|
store_type id (Pident id) info env
|
|
|
|
|
|
|
|
and add_exception id decl env =
|
|
|
|
store_exception id (Pident id) decl env
|
|
|
|
|
|
|
|
and add_module id mty env =
|
|
|
|
store_module id (Pident id) mty env
|
|
|
|
|
|
|
|
and add_modtype id info env =
|
|
|
|
store_modtype id (Pident id) info env
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
and add_class id ty env =
|
|
|
|
store_class id (Pident id) ty env
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Insertion of bindings by name *)
|
|
|
|
|
|
|
|
let enter store_fun name data env =
|
1996-04-22 04:15:41 -07:00
|
|
|
let id = Ident.create name in (id, store_fun id (Pident id) data env)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let enter_value = enter store_value
|
|
|
|
and enter_type = enter store_type
|
|
|
|
and enter_exception = enter store_exception
|
|
|
|
and enter_module = enter store_module
|
|
|
|
and enter_modtype = enter store_modtype
|
1996-04-22 04:15:41 -07:00
|
|
|
and enter_class = enter store_class
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Insertion of all components of a signature *)
|
|
|
|
|
1996-07-25 06:18:53 -07:00
|
|
|
let add_item comp env =
|
1995-05-04 03:15:53 -07:00
|
|
|
match comp with
|
|
|
|
Tsig_value(id, decl) -> add_value id decl env
|
|
|
|
| Tsig_type(id, decl) -> add_type id decl env
|
|
|
|
| Tsig_exception(id, decl) -> add_exception id decl env
|
|
|
|
| Tsig_module(id, mty) -> add_module id mty env
|
|
|
|
| Tsig_modtype(id, decl) -> add_modtype id decl env
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tsig_class(id, decl) -> add_class id decl env
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-07-25 06:18:53 -07:00
|
|
|
let rec add_signature sg env =
|
|
|
|
match sg with
|
|
|
|
[] -> env
|
|
|
|
| comp :: rem -> add_signature rem (add_item comp env)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Open a signature path *)
|
|
|
|
|
|
|
|
let open_signature root sg env =
|
|
|
|
(* First build the paths and substitution *)
|
|
|
|
let (pl, sub) = prefix_idents root 0 Subst.identity sg in
|
|
|
|
(* Then enter the components in the environment after substitution *)
|
1996-11-29 10:36:42 -08:00
|
|
|
let newenv =
|
|
|
|
List.fold_left2
|
|
|
|
(fun env item p ->
|
|
|
|
match item with
|
|
|
|
Tsig_value(id, decl) ->
|
|
|
|
store_value (Ident.hide id) p
|
|
|
|
(Subst.value_description sub decl) env
|
|
|
|
| Tsig_type(id, decl) ->
|
|
|
|
store_type (Ident.hide id) p
|
|
|
|
(Subst.type_declaration sub decl) env
|
|
|
|
| Tsig_exception(id, decl) ->
|
|
|
|
store_exception (Ident.hide id) p
|
|
|
|
(Subst.exception_declaration sub decl) env
|
|
|
|
| Tsig_module(id, mty) ->
|
|
|
|
store_module (Ident.hide id) p (Subst.modtype sub mty) env
|
|
|
|
| Tsig_modtype(id, decl) ->
|
|
|
|
store_modtype (Ident.hide id) p
|
|
|
|
(Subst.modtype_declaration sub decl) env
|
|
|
|
| Tsig_class(id, decl) ->
|
|
|
|
store_class (Ident.hide id) p
|
|
|
|
(Subst.class_type sub decl) env)
|
|
|
|
env sg pl in
|
|
|
|
{ values = newenv.values;
|
|
|
|
constrs = newenv.constrs;
|
|
|
|
labels = newenv.labels;
|
|
|
|
types = newenv.types;
|
|
|
|
modules = newenv.modules;
|
|
|
|
modtypes = newenv.modtypes;
|
|
|
|
components = newenv.components;
|
|
|
|
classes = newenv.classes;
|
|
|
|
summary = Env_open(env.summary, root) }
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Open a signature from a file *)
|
|
|
|
|
|
|
|
let open_pers_signature name env =
|
|
|
|
let ps = find_pers_struct name in
|
1996-04-22 04:15:41 -07:00
|
|
|
open_signature (Pident(Ident.create_persistent name)) ps.ps_sig env
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Read a signature from a file *)
|
|
|
|
|
|
|
|
let read_signature modname filename =
|
1997-05-15 06:30:02 -07:00
|
|
|
let ps = read_pers_struct modname filename in ps.ps_sig
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-05-15 06:30:02 -07:00
|
|
|
(* Return the list of imported interfaces with their CRCs *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-05-15 06:30:02 -07:00
|
|
|
let imported_units() =
|
|
|
|
let imported_units =
|
|
|
|
ref ([] : (string * Digest.t) list) in
|
|
|
|
let units_xref =
|
|
|
|
(Hashtbl.create 13 : (string, Digest.t * string) Hashtbl.t) in
|
|
|
|
let add_unit source (name, crc) =
|
|
|
|
try
|
|
|
|
let (oldcrc, oldsource) = Hashtbl.find units_xref name in
|
|
|
|
if oldcrc <> crc then
|
|
|
|
raise(Error(Inconsistent_import(name, oldsource, source)))
|
|
|
|
with Not_found ->
|
|
|
|
Hashtbl.add units_xref name (crc, source);
|
|
|
|
imported_units := (name, crc) :: !imported_units in
|
|
|
|
Hashtbl.iter
|
|
|
|
(fun name ps -> List.iter (add_unit ps.ps_name) ps.ps_crcs)
|
|
|
|
persistent_structures;
|
|
|
|
!imported_units
|
|
|
|
|
|
|
|
(* Save a signature to a file *)
|
1997-04-01 12:53:02 -08:00
|
|
|
|
1995-07-02 09:50:08 -07:00
|
|
|
let save_signature sg modname filename =
|
1997-03-24 12:12:16 -08:00
|
|
|
Btype.cleanup_abbrev ();
|
1995-05-04 03:15:53 -07:00
|
|
|
let oc = open_out_bin filename in
|
|
|
|
output_string oc cmi_magic_number;
|
1997-05-15 06:30:02 -07:00
|
|
|
let comps =
|
|
|
|
components_of_module empty Subst.identity
|
|
|
|
(Pident(Ident.create_persistent modname)) (Tmty_signature sg) in
|
|
|
|
output_value oc (modname, sg, comps);
|
1995-07-02 09:50:08 -07:00
|
|
|
flush oc;
|
1995-10-09 06:37:11 -07:00
|
|
|
let crc = Digest.file filename in
|
1997-05-15 06:30:02 -07:00
|
|
|
let crcs = (modname, crc) :: imported_units() in
|
|
|
|
output_value oc crcs;
|
1995-07-02 09:50:08 -07:00
|
|
|
close_out oc;
|
1997-05-15 06:30:02 -07:00
|
|
|
(* Enter signature in persistent table so that imported_unit()
|
|
|
|
will also return its crc *)
|
|
|
|
let ps =
|
|
|
|
{ ps_name = modname; ps_sig = sg; ps_comps = comps; ps_crcs = crcs } in
|
|
|
|
Hashtbl.add persistent_structures modname ps
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Make the initial environment *)
|
|
|
|
|
|
|
|
let initial = Predef.build_initial_env add_type add_exception empty
|
|
|
|
|
1996-11-29 10:36:42 -08:00
|
|
|
(* Return the environment summary *)
|
|
|
|
|
|
|
|
let summary env = env.summary
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
let report_error = function
|
1995-11-03 05:23:03 -08:00
|
|
|
Not_an_interface filename ->
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string filename; print_space();
|
|
|
|
print_string "is not a compiled interface."
|
|
|
|
| Corrupted_interface filename ->
|
|
|
|
print_string "Corrupted compiled interface"; print_space();
|
|
|
|
print_string filename
|
|
|
|
| Illegal_renaming(modname, filename) ->
|
1996-02-20 03:01:45 -08:00
|
|
|
print_string "Wrong file naming:"; print_space();
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string filename; print_space();
|
|
|
|
print_string "contains the compiled interface for"; print_space();
|
|
|
|
print_string modname
|
1997-05-15 06:30:02 -07:00
|
|
|
| Inconsistent_import(name, source1, source2) ->
|
|
|
|
open_hvbox 0;
|
|
|
|
print_string "The compiled interfaces for "; print_string source1;
|
|
|
|
print_string " and "; print_string source2; print_space();
|
|
|
|
print_string "make inconsistent assumptions over interface ";
|
|
|
|
print_string name;
|
|
|
|
close_box()
|