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
|
|
|
(* Type-checking of the module language *)
|
|
|
|
|
|
|
|
open Misc
|
1995-09-28 03:42:38 -07:00
|
|
|
open Longident
|
1995-05-04 03:15:53 -07:00
|
|
|
open Path
|
|
|
|
open Parsetree
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Typedtree
|
|
|
|
|
|
|
|
|
|
|
|
type error =
|
|
|
|
Unbound_module of Longident.t
|
|
|
|
| Unbound_modtype of Longident.t
|
|
|
|
| Cannot_apply of module_type
|
|
|
|
| Not_included of Includemod.error list
|
|
|
|
| Cannot_eliminate_dependency of module_type
|
|
|
|
| Signature_expected
|
|
|
|
| Structure_expected of module_type
|
1995-10-01 06:39:43 -07:00
|
|
|
| With_no_component of Longident.t
|
1996-07-25 06:18:53 -07:00
|
|
|
| With_mismatch of Longident.t * Includemod.error list
|
1995-08-28 04:23:33 -07:00
|
|
|
| Repeated_name of string * string
|
1995-09-02 11:55:37 -07:00
|
|
|
| Non_generalizable of type_expr
|
1998-06-24 12:22:26 -07:00
|
|
|
| Non_generalizable_class of Ident.t * class_declaration
|
1996-05-22 09:22:33 -07:00
|
|
|
| Non_generalizable_module of module_type
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Error of Location.t * error
|
|
|
|
|
|
|
|
(* Extract a signature from a module type *)
|
|
|
|
|
|
|
|
let extract_sig env loc mty =
|
|
|
|
match Mtype.scrape env mty with
|
|
|
|
Tmty_signature sg -> sg
|
|
|
|
| _ -> raise(Error(loc, Signature_expected))
|
|
|
|
|
|
|
|
let extract_sig_open env loc mty =
|
|
|
|
match Mtype.scrape env mty with
|
|
|
|
Tmty_signature sg -> sg
|
|
|
|
| _ -> raise(Error(loc, Structure_expected mty))
|
|
|
|
|
1995-10-01 06:39:43 -07:00
|
|
|
(* Lookup the type of a module path *)
|
|
|
|
|
|
|
|
let type_module_path env loc lid =
|
|
|
|
try
|
|
|
|
Env.lookup_module lid env
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(loc, Unbound_module lid))
|
|
|
|
|
1995-09-28 03:42:38 -07:00
|
|
|
(* Merge one "with" constraint in a signature *)
|
|
|
|
|
1996-07-25 06:18:53 -07:00
|
|
|
let merge_constraint initial_env loc sg lid constr =
|
|
|
|
let rec merge env sg namelist =
|
1995-10-01 06:39:43 -07:00
|
|
|
match (sg, namelist, constr) with
|
|
|
|
([], _, _) ->
|
|
|
|
raise(Error(loc, With_no_component lid))
|
|
|
|
| (Tsig_type(id, decl) :: rem, [s], Pwith_type sdecl)
|
|
|
|
when Ident.name id = s ->
|
1996-07-25 06:18:53 -07:00
|
|
|
let newdecl = Typedecl.transl_with_constraint initial_env sdecl in
|
|
|
|
Includemod.type_declarations env id newdecl decl;
|
1995-09-28 03:42:38 -07:00
|
|
|
Tsig_type(id, newdecl) :: rem
|
1995-10-01 06:39:43 -07:00
|
|
|
| (Tsig_module(id, mty) :: rem, [s], Pwith_module lid)
|
|
|
|
when Ident.name id = s ->
|
1996-07-25 06:18:53 -07:00
|
|
|
let (path, mty') = type_module_path initial_env loc lid in
|
|
|
|
let newmty = Mtype.strengthen env mty' path in
|
|
|
|
Includemod.modtypes env newmty mty;
|
|
|
|
Tsig_module(id, newmty) :: rem
|
1995-10-01 06:39:43 -07:00
|
|
|
| (Tsig_module(id, mty) :: rem, s :: namelist, _) when Ident.name id = s ->
|
1996-07-25 06:18:53 -07:00
|
|
|
let newsg = merge env (extract_sig env loc mty) namelist in
|
1995-09-28 03:42:38 -07:00
|
|
|
Tsig_module(id, Tmty_signature newsg) :: rem
|
1995-10-01 06:39:43 -07:00
|
|
|
| (item :: rem, _, _) ->
|
1996-07-25 06:18:53 -07:00
|
|
|
item :: merge (Env.add_item item env) rem namelist in
|
|
|
|
try
|
|
|
|
merge initial_env sg (Longident.flatten lid)
|
|
|
|
with Includemod.Error explanation ->
|
|
|
|
raise(Error(loc, With_mismatch(lid, explanation)))
|
1995-09-28 03:42:38 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Check and translate a module type expression *)
|
|
|
|
|
|
|
|
let rec transl_modtype env smty =
|
|
|
|
match smty.pmty_desc with
|
|
|
|
Pmty_ident lid ->
|
|
|
|
begin try
|
|
|
|
let (path, info) = Env.lookup_modtype lid env in
|
|
|
|
Tmty_ident path
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(smty.pmty_loc, Unbound_modtype lid))
|
|
|
|
end
|
1995-08-28 04:23:33 -07:00
|
|
|
| Pmty_signature ssg ->
|
|
|
|
Tmty_signature(transl_signature env ssg)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmty_functor(param, sarg, sres) ->
|
|
|
|
let arg = transl_modtype env sarg in
|
|
|
|
let (id, newenv) = Env.enter_module param arg env in
|
|
|
|
let res = transl_modtype newenv sres in
|
|
|
|
Tmty_functor(id, arg, res)
|
1995-09-28 03:42:38 -07:00
|
|
|
| Pmty_with(sbody, constraints) ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let body = transl_modtype env sbody in
|
1995-09-28 03:42:38 -07:00
|
|
|
let init_sg = extract_sig env sbody.pmty_loc body in
|
|
|
|
let final_sg =
|
|
|
|
List.fold_left
|
|
|
|
(fun sg (lid, sdecl) ->
|
|
|
|
merge_constraint env smty.pmty_loc sg lid sdecl)
|
|
|
|
init_sg constraints in
|
|
|
|
Tmty_signature final_sg
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and transl_signature env sg =
|
1996-07-18 01:33:23 -07:00
|
|
|
Ctype.init_def(Ident.current_time());
|
1995-05-04 03:15:53 -07:00
|
|
|
match sg with
|
|
|
|
[] -> []
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_value(name, sdesc)} :: srem ->
|
1995-07-25 04:40:07 -07:00
|
|
|
let desc = Typedecl.transl_value_decl env sdesc in
|
1995-05-04 03:15:53 -07:00
|
|
|
let (id, newenv) = Env.enter_value name desc env in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
Tsig_value(id, desc) :: rem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_type sdecls} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (decls, newenv) = Typedecl.transl_type_decl env sdecls in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
map_end (fun (id, info) -> Tsig_type(id, info)) decls rem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_exception(name, sarg)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let arg = Typedecl.transl_exception env sarg in
|
|
|
|
let (id, newenv) = Env.enter_exception name arg env in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
Tsig_exception(id, arg) :: rem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_module(name, smty)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let mty = transl_modtype env smty in
|
|
|
|
let (id, newenv) = Env.enter_module name mty env in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
Tsig_module(id, mty) :: rem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_modtype(name, sinfo)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let info = transl_modtype_info env sinfo in
|
|
|
|
let (id, newenv) = Env.enter_modtype name info env in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
Tsig_modtype(id, info) :: rem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_open lid; psig_loc = loc} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (path, mty) = type_module_path env loc lid in
|
|
|
|
let sg = extract_sig_open env loc mty in
|
|
|
|
let newenv = Env.open_signature path sg env in
|
|
|
|
transl_signature newenv srem
|
1995-10-05 08:22:23 -07:00
|
|
|
| {psig_desc = Psig_include smty} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let mty = transl_modtype env smty in
|
|
|
|
let sg = extract_sig env smty.pmty_loc mty in
|
|
|
|
let newenv = Env.add_signature sg env in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
sg @ rem
|
1996-04-22 04:15:41 -07:00
|
|
|
| {psig_desc = Psig_class cl} :: srem ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let (classes, newenv) = Typeclass.class_descriptions env cl in
|
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
List.flatten
|
|
|
|
(map_end
|
|
|
|
(fun (i, d, i', d', i'', d'', i''', d''', _, _, _) ->
|
|
|
|
[Tsig_class(i, d); Tsig_cltype(i', d');
|
|
|
|
Tsig_type(i'', d''); Tsig_type(i''', d''')])
|
|
|
|
classes [rem])
|
|
|
|
| {psig_desc = Psig_class_type cl} :: srem ->
|
|
|
|
let (classes, newenv) = Typeclass.class_type_declarations env cl in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rem = transl_signature newenv srem in
|
|
|
|
List.flatten
|
|
|
|
(map_end
|
|
|
|
(fun (i, d, i', d', i'', d'') ->
|
1998-06-24 12:22:26 -07:00
|
|
|
[Tsig_cltype(i, d); Tsig_type(i', d'); Tsig_type(i'', d'')])
|
1996-04-22 04:15:41 -07:00
|
|
|
classes [rem])
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and transl_modtype_info env sinfo =
|
|
|
|
match sinfo with
|
|
|
|
Pmodtype_abstract ->
|
|
|
|
Tmodtype_abstract
|
|
|
|
| Pmodtype_manifest smty ->
|
|
|
|
Tmodtype_manifest(transl_modtype env smty)
|
|
|
|
|
1995-08-23 04:55:54 -07:00
|
|
|
(* Try to convert a module expression to a module path. *)
|
|
|
|
|
|
|
|
exception Not_a_path
|
|
|
|
|
|
|
|
let rec path_of_module mexp =
|
|
|
|
match mexp.mod_desc with
|
|
|
|
Tmod_ident p -> p
|
|
|
|
| Tmod_apply(funct, arg, coercion) ->
|
|
|
|
Papply(path_of_module funct, path_of_module arg)
|
|
|
|
| _ -> raise Not_a_path
|
|
|
|
|
1995-08-28 04:23:33 -07:00
|
|
|
(* Check that all type and module identifiers in a structure have
|
|
|
|
distinct names (so that access by named paths is unambiguous). *)
|
|
|
|
|
|
|
|
module StringSet = Set.Make(struct type t = string let compare = compare end)
|
|
|
|
|
|
|
|
let check_unique_names sg =
|
|
|
|
let type_names = ref StringSet.empty
|
1995-10-05 08:22:23 -07:00
|
|
|
and module_names = ref StringSet.empty
|
|
|
|
and modtype_names = ref StringSet.empty in
|
1996-04-22 04:15:41 -07:00
|
|
|
let check cl loc set_ref name =
|
1995-08-28 04:23:33 -07:00
|
|
|
if StringSet.mem name !set_ref
|
1996-04-22 04:15:41 -07:00
|
|
|
then raise(Error(loc, Repeated_name(cl, name)))
|
1995-08-28 04:23:33 -07:00
|
|
|
else set_ref := StringSet.add name !set_ref in
|
1995-10-05 08:22:23 -07:00
|
|
|
let check_item item =
|
|
|
|
match item.pstr_desc with
|
1995-08-28 04:23:33 -07:00
|
|
|
Pstr_eval exp -> ()
|
|
|
|
| Pstr_value(rec_flag, exps) -> ()
|
|
|
|
| Pstr_primitive(name, desc) -> ()
|
|
|
|
| Pstr_type name_decl_list ->
|
|
|
|
List.iter
|
1995-10-05 08:22:23 -07:00
|
|
|
(fun (name, decl) -> check "type" item.pstr_loc type_names name)
|
1995-08-28 04:23:33 -07:00
|
|
|
name_decl_list
|
|
|
|
| Pstr_exception(name, decl) -> ()
|
|
|
|
| Pstr_module(name, smod) ->
|
1995-10-05 08:22:23 -07:00
|
|
|
check "module" item.pstr_loc module_names name
|
|
|
|
| Pstr_modtype(name, decl) ->
|
|
|
|
check "module type" item.pstr_loc modtype_names name
|
1996-04-22 04:15:41 -07:00
|
|
|
| Pstr_open lid -> ()
|
1996-10-25 14:34:25 -07:00
|
|
|
| Pstr_class decl ->
|
|
|
|
List.iter
|
1998-06-24 12:22:26 -07:00
|
|
|
(fun {pci_name = name} ->
|
|
|
|
check "type" item.pstr_loc type_names name)
|
|
|
|
decl
|
|
|
|
| Pstr_class_type decl ->
|
|
|
|
List.iter
|
|
|
|
(fun {pci_name = name} ->
|
1996-10-25 14:34:25 -07:00
|
|
|
check "type" item.pstr_loc type_names name)
|
|
|
|
decl
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
|
|
|
List.iter check_item sg
|
1995-08-28 04:23:33 -07:00
|
|
|
|
1995-09-02 11:55:37 -07:00
|
|
|
(* Check that all core type schemes in a structure are closed *)
|
|
|
|
|
1996-05-22 09:22:33 -07:00
|
|
|
let rec closed_modtype = function
|
|
|
|
Tmty_ident p -> true
|
|
|
|
| Tmty_signature sg -> List.for_all closed_signature_item sg
|
|
|
|
| Tmty_functor(id, param, body) -> closed_modtype body
|
|
|
|
|
|
|
|
and closed_signature_item = function
|
1997-03-18 13:06:28 -08:00
|
|
|
Tsig_value(id, desc) -> Ctype.closed_schema desc.val_type
|
1996-05-22 09:22:33 -07:00
|
|
|
| Tsig_module(id, mty) -> closed_modtype mty
|
|
|
|
| _ -> true
|
|
|
|
|
|
|
|
let check_nongen_scheme env = function
|
|
|
|
Tstr_value(rec_flag, pat_exp_list) ->
|
|
|
|
List.iter
|
|
|
|
(fun (pat, exp) ->
|
1997-03-18 13:06:28 -08:00
|
|
|
if not (Ctype.closed_schema exp.exp_type) then
|
1996-05-22 09:22:33 -07:00
|
|
|
raise(Error(exp.exp_loc, Non_generalizable exp.exp_type)))
|
|
|
|
pat_exp_list
|
|
|
|
| Tstr_module(id, md) ->
|
|
|
|
if not (closed_modtype md.mod_type) then
|
|
|
|
raise(Error(md.mod_loc, Non_generalizable_module md.mod_type))
|
|
|
|
| _ -> ()
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let check_nongen_schemes env str =
|
1996-05-22 09:22:33 -07:00
|
|
|
List.iter (check_nongen_scheme env) str
|
1995-09-02 11:55:37 -07:00
|
|
|
|
1995-05-05 03:05:18 -07:00
|
|
|
(* Type a module value expression *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let rec type_module env smod =
|
|
|
|
match smod.pmod_desc with
|
|
|
|
Pmod_ident lid ->
|
|
|
|
let (path, mty) = type_module_path env smod.pmod_loc lid in
|
|
|
|
{ mod_desc = Tmod_ident path;
|
|
|
|
mod_type = Mtype.strengthen env mty path;
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_env = env;
|
1995-05-04 03:15:53 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
| Pmod_structure sstr ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let (str, sg, finalenv) = type_structure env sstr in
|
1995-05-04 03:15:53 -07:00
|
|
|
{ mod_desc = Tmod_structure str;
|
|
|
|
mod_type = Tmty_signature sg;
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_env = env;
|
1995-05-04 03:15:53 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
| Pmod_functor(name, smty, sbody) ->
|
|
|
|
let mty = transl_modtype env smty in
|
|
|
|
let (id, newenv) = Env.enter_module name mty env in
|
|
|
|
let body = type_module newenv sbody in
|
|
|
|
{ mod_desc = Tmod_functor(id, mty, body);
|
|
|
|
mod_type = Tmty_functor(id, mty, body.mod_type);
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_env = env;
|
1995-05-04 03:15:53 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
| Pmod_apply(sfunct, sarg) ->
|
|
|
|
let funct = type_module env sfunct in
|
|
|
|
let arg = type_module env sarg in
|
|
|
|
begin match Mtype.scrape env funct.mod_type with
|
|
|
|
Tmty_functor(param, mty_param, mty_res) as mty_functor ->
|
|
|
|
let coercion =
|
|
|
|
try
|
|
|
|
Includemod.modtypes env arg.mod_type mty_param
|
|
|
|
with Includemod.Error msg ->
|
|
|
|
raise(Error(sarg.pmod_loc, Not_included msg)) in
|
|
|
|
let mty_appl =
|
1997-05-19 08:42:21 -07:00
|
|
|
try
|
|
|
|
let path = path_of_module arg in
|
1995-08-23 04:55:54 -07:00
|
|
|
Subst.modtype (Subst.add_module param path Subst.identity)
|
1997-05-19 08:42:21 -07:00
|
|
|
mty_res
|
|
|
|
with Not_a_path ->
|
1995-05-04 03:15:53 -07:00
|
|
|
try
|
|
|
|
Mtype.nondep_supertype
|
|
|
|
(Env.add_module param arg.mod_type env) param mty_res
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(smod.pmod_loc,
|
|
|
|
Cannot_eliminate_dependency mty_functor)) in
|
|
|
|
{ mod_desc = Tmod_apply(funct, arg, coercion);
|
|
|
|
mod_type = mty_appl;
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_env = env;
|
1995-05-04 03:15:53 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
| _ ->
|
|
|
|
raise(Error(sfunct.pmod_loc, Cannot_apply funct.mod_type))
|
|
|
|
end
|
|
|
|
| Pmod_constraint(sarg, smty) ->
|
|
|
|
let arg = type_module env sarg in
|
|
|
|
let mty = transl_modtype env smty in
|
|
|
|
let coercion =
|
|
|
|
try
|
|
|
|
Includemod.modtypes env arg.mod_type mty
|
|
|
|
with Includemod.Error msg ->
|
|
|
|
raise(Error(sarg.pmod_loc, Not_included msg)) in
|
|
|
|
{ mod_desc = Tmod_constraint(arg, mty, coercion);
|
|
|
|
mod_type = mty;
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_env = env;
|
1995-05-04 03:15:53 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
|
1995-08-28 04:23:33 -07:00
|
|
|
and type_structure env sstr =
|
|
|
|
check_unique_names sstr;
|
|
|
|
type_struct env sstr
|
|
|
|
|
1996-07-15 09:35:35 -07:00
|
|
|
and type_struct env sstr =
|
|
|
|
Ctype.init_def(Ident.current_time());
|
|
|
|
match sstr with
|
1995-05-04 03:15:53 -07:00
|
|
|
[] ->
|
|
|
|
([], [], env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_eval sexpr} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let expr = Typecore.type_expression env sexpr in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct env srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_eval expr :: str_rem, sig_rem, final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_value(rec_flag, sdefs)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (defs, newenv) =
|
|
|
|
Typecore.type_binding env rec_flag sdefs in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1996-02-18 06:45:54 -08:00
|
|
|
let bound_idents = let_bound_idents defs in
|
1995-05-04 03:15:53 -07:00
|
|
|
let make_sig_value id =
|
|
|
|
Tsig_value(id, Env.find_value (Pident id) newenv) in
|
|
|
|
(Tstr_value(rec_flag, defs) :: str_rem,
|
|
|
|
map_end make_sig_value bound_idents sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_primitive(name, sdesc)} :: srem ->
|
1995-07-25 04:40:07 -07:00
|
|
|
let desc = Typedecl.transl_value_decl env sdesc in
|
1995-05-04 03:15:53 -07:00
|
|
|
let (id, newenv) = Env.enter_value name desc env in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_primitive(id, desc) :: str_rem,
|
|
|
|
Tsig_value(id, desc) :: sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_type sdecls} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (decls, newenv) = Typedecl.transl_type_decl env sdecls in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_type decls :: str_rem,
|
|
|
|
map_end (fun (id, info) -> Tsig_type(id, info)) decls sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_exception(name, sarg)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let arg = Typedecl.transl_exception env sarg in
|
|
|
|
let (id, newenv) = Env.enter_exception name arg env in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_exception(id, arg) :: str_rem,
|
|
|
|
Tsig_exception(id, arg) :: sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_module(name, smodl)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let modl = type_module env smodl in
|
|
|
|
let (id, newenv) = Env.enter_module name modl.mod_type env in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_module(id, modl) :: str_rem,
|
|
|
|
Tsig_module(id, modl.mod_type) :: sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_modtype(name, smty)} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let mty = transl_modtype env smty in
|
|
|
|
let (id, newenv) = Env.enter_modtype name (Tmodtype_manifest mty) env in
|
1995-08-28 04:23:33 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
1995-05-04 03:15:53 -07:00
|
|
|
(Tstr_modtype(id, mty) :: str_rem,
|
|
|
|
Tsig_modtype(id, Tmodtype_manifest mty) :: sig_rem,
|
|
|
|
final_env)
|
1995-10-05 08:22:23 -07:00
|
|
|
| {pstr_desc = Pstr_open lid; pstr_loc = loc} :: srem ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (path, mty) = type_module_path env loc lid in
|
|
|
|
let sg = extract_sig_open env loc mty in
|
1995-08-28 04:23:33 -07:00
|
|
|
type_struct (Env.open_signature path sg env) srem
|
1998-06-24 12:22:26 -07:00
|
|
|
| {pstr_desc = Pstr_class cl} :: srem ->
|
|
|
|
let (classes, new_env) = Typeclass.class_declarations env cl in
|
|
|
|
let (str_rem, sig_rem, final_env) = type_struct new_env srem in
|
|
|
|
(Tstr_class
|
|
|
|
(List.map (fun (i, _,_,_,_,_,_,_, s, m, c) ->
|
|
|
|
(i, s, m, c)) classes) ::
|
|
|
|
Tstr_cltype
|
|
|
|
(List.map (fun (_,_, i, d, _,_,_,_,_,_,_) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_,_,_,_, i, d, _,_,_,_,_) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_,_,_,_,_,_, i, d, _,_,_) -> (i, d)) classes) ::
|
|
|
|
str_rem,
|
|
|
|
List.flatten
|
|
|
|
(map_end
|
|
|
|
(fun (i, d, i', d', i'', d'', i''', d''', _, _, _) ->
|
|
|
|
[Tsig_class(i, d); Tsig_cltype(i', d');
|
|
|
|
Tsig_type(i'', d''); Tsig_type(i''', d''')])
|
|
|
|
classes [sig_rem]),
|
|
|
|
final_env)
|
|
|
|
| {pstr_desc = Pstr_class_type cl} :: srem ->
|
|
|
|
let (classes, new_env) = Typeclass.class_type_declarations env cl in
|
1996-04-22 04:15:41 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct new_env srem in
|
1998-06-24 12:22:26 -07:00
|
|
|
(Tstr_cltype
|
|
|
|
(List.map (fun (i, d, _, _, _, _) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_, _, i, d, _, _) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_, _, _, _, i, d) -> (i, d)) classes) ::
|
|
|
|
str_rem,
|
1996-04-22 04:15:41 -07:00
|
|
|
List.flatten
|
|
|
|
(map_end
|
1998-06-24 12:22:26 -07:00
|
|
|
(fun (i, d, i', d', i'', d'') ->
|
|
|
|
[Tsig_cltype(i, d); Tsig_type(i', d'); Tsig_type(i'', d'')])
|
1996-04-22 04:15:41 -07:00
|
|
|
classes [sig_rem]),
|
|
|
|
final_env)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-02-26 04:54:44 -08:00
|
|
|
(* Fill in the forward declaration *)
|
|
|
|
let _ =
|
|
|
|
Typecore.type_module := type_module
|
|
|
|
|
1998-04-27 08:16:48 -07:00
|
|
|
(* Typecheck an implementation file *)
|
|
|
|
|
|
|
|
let type_implementation sourcefile prefixname modulename initial_env ast =
|
|
|
|
let (str, sg, finalenv) = type_structure initial_env ast in
|
|
|
|
if !Clflags.print_types then (Printtyp.signature sg; print_newline());
|
|
|
|
let coercion =
|
1998-05-27 07:11:25 -07:00
|
|
|
if Sys.file_exists (prefixname ^ !Config.interface_suffix) then begin
|
1998-04-27 08:16:48 -07:00
|
|
|
let intf_file =
|
|
|
|
try find_in_path !Config.load_path (prefixname ^ ".cmi")
|
|
|
|
with Not_found -> prefixname ^ ".cmi" in
|
|
|
|
let dclsig = Env.read_signature modulename intf_file in
|
|
|
|
Includemod.compunit sourcefile sg intf_file dclsig
|
|
|
|
end else begin
|
|
|
|
check_nongen_schemes finalenv str;
|
|
|
|
Env.save_signature sg modulename (prefixname ^ ".cmi");
|
|
|
|
Tcoerce_none
|
|
|
|
end in
|
|
|
|
(str, coercion)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
open Printtyp
|
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Unbound_module lid ->
|
|
|
|
print_string "Unbound module "; longident lid
|
|
|
|
| Unbound_modtype lid ->
|
|
|
|
print_string "Unbound module type "; longident lid
|
|
|
|
| Cannot_apply mty ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string "This module is not a functor; it has type";
|
|
|
|
print_space(); modtype mty;
|
|
|
|
close_box()
|
|
|
|
| Not_included errs ->
|
|
|
|
open_vbox 0;
|
|
|
|
print_string "Signature mismatch:"; print_space();
|
|
|
|
Includemod.report_error errs;
|
|
|
|
close_box()
|
|
|
|
| Cannot_eliminate_dependency mty ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string "This functor has type";
|
|
|
|
print_space(); modtype mty; print_space();
|
|
|
|
print_string "The parameter cannot be eliminated in the result type.";
|
|
|
|
print_space();
|
|
|
|
print_string "Please bind the argument to a module identifier.";
|
|
|
|
close_box()
|
|
|
|
| Signature_expected ->
|
|
|
|
print_string "This module type is not a signature"
|
|
|
|
| Structure_expected mty ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string "This module is not a structure; it has type";
|
|
|
|
print_space(); modtype mty;
|
|
|
|
close_box()
|
1995-10-01 06:39:43 -07:00
|
|
|
| With_no_component lid ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-10-01 06:39:43 -07:00
|
|
|
print_string "The signature constrained by `with' has no component named";
|
1996-07-25 06:18:53 -07:00
|
|
|
print_space(); longident lid;
|
|
|
|
close_box()
|
|
|
|
| With_mismatch(lid, explanation) ->
|
|
|
|
open_vbox 0;
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1996-07-25 06:18:53 -07:00
|
|
|
print_string "In this `with' constraint, the new definition of";
|
|
|
|
print_space(); longident lid; print_space();
|
|
|
|
print_string "does not match its original definition";
|
|
|
|
print_space(); print_string "in the constrained signature:";
|
|
|
|
close_box();
|
|
|
|
print_space();
|
|
|
|
Includemod.report_error explanation;
|
|
|
|
close_box()
|
1995-08-28 04:23:33 -07:00
|
|
|
| Repeated_name(kind, name) ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-08-28 04:23:33 -07:00
|
|
|
print_string "Multiple definition of the "; print_string kind;
|
|
|
|
print_string " name "; print_string name; print_string ".";
|
|
|
|
print_space();
|
|
|
|
print_string "Names must be unique in a given structure.";
|
|
|
|
close_box()
|
1995-09-02 11:55:37 -07:00
|
|
|
| Non_generalizable typ ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-09-02 11:55:37 -07:00
|
|
|
print_string "The type of this expression,"; print_space();
|
|
|
|
type_scheme typ; print_string ","; print_space();
|
1996-04-22 04:15:41 -07:00
|
|
|
print_string "contains type variables that cannot be generalized";
|
|
|
|
close_box()
|
|
|
|
| Non_generalizable_class (id, desc) ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1996-04-22 04:15:41 -07:00
|
|
|
print_string "The type of this class,"; print_space();
|
1998-06-24 12:22:26 -07:00
|
|
|
class_declaration id desc; print_string ","; print_space();
|
1996-04-22 04:15:41 -07:00
|
|
|
print_string "contains type variables that cannot be generalized";
|
|
|
|
close_box()
|
1996-05-22 09:22:33 -07:00
|
|
|
| Non_generalizable_module mty ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1996-05-22 09:22:33 -07:00
|
|
|
print_string "The type of this module,"; print_space();
|
|
|
|
modtype mty; print_string ","; print_space();
|
|
|
|
print_string "contains type variables that cannot be generalized";
|
|
|
|
close_box()
|