1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
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 *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
open Misc
|
1995-09-28 03:42:38 -07:00
|
|
|
open Longident
|
1995-05-04 03:15:53 -07:00
|
|
|
open Path
|
2006-04-04 19:28:13 -07:00
|
|
|
open Asttypes
|
1995-05-04 03:15:53 -07:00
|
|
|
open Parsetree
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type error =
|
2010-05-18 10:18:24 -07:00
|
|
|
Cannot_apply of module_type
|
1995-05-04 03:15:53 -07:00
|
|
|
| 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
|
2004-04-09 06:32:28 -07:00
|
|
|
| Implementation_is_required of string
|
2004-06-13 05:48:01 -07:00
|
|
|
| Interface_not_compiled of string
|
2009-10-26 03:53:16 -07:00
|
|
|
| Not_allowed_in_functor_body
|
2010-04-17 07:45:12 -07:00
|
|
|
| With_need_typeconstr
|
2010-10-21 16:59:33 -07:00
|
|
|
| Not_a_packed_module of type_expr
|
|
|
|
| Incomplete_packed_module of type_expr
|
2011-12-14 02:26:15 -08:00
|
|
|
| Scoping_pack of Longident.t * type_expr
|
2013-02-28 08:51:59 -08:00
|
|
|
| Extension of string
|
2013-03-05 08:50:05 -08:00
|
|
|
| Recursive_module_require_explicit_type
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-01-22 23:59:45 -08:00
|
|
|
exception Error of Location.t * Env.t * error
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
open Typedtree
|
|
|
|
|
|
|
|
let fst3 (x,_,_) = x
|
|
|
|
|
|
|
|
let rec path_concat head p =
|
|
|
|
match p with
|
|
|
|
Pident tail -> Pdot (Pident head, Ident.name tail, 0)
|
|
|
|
| Pdot (pre, s, pos) -> Pdot (path_concat head pre, s, pos)
|
|
|
|
| Papply _ -> assert false
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Extract a signature from a module type *)
|
|
|
|
|
|
|
|
let extract_sig env loc mty =
|
|
|
|
match Mtype.scrape env mty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_signature sg -> sg
|
2012-01-22 23:59:45 -08:00
|
|
|
| _ -> raise(Error(loc, env, Signature_expected))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let extract_sig_open env loc mty =
|
|
|
|
match Mtype.scrape env mty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_signature sg -> sg
|
2012-01-22 23:59:45 -08:00
|
|
|
| _ -> raise(Error(loc, env, Structure_expected mty))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2009-11-01 13:52:29 -08:00
|
|
|
(* Compute the environment after opening a module *)
|
|
|
|
|
2013-05-16 06:34:53 -07:00
|
|
|
let type_open ?toplevel ovf env loc lid =
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, mty) = Typetexp.find_module env loc lid.txt in
|
2009-11-01 13:52:29 -08:00
|
|
|
let sg = extract_sig_open env loc mty in
|
2013-05-16 06:34:53 -07:00
|
|
|
path, Env.open_signature ~loc ?toplevel ovf path sg env
|
2009-11-01 13:52:29 -08:00
|
|
|
|
2003-04-01 22:57:15 -08:00
|
|
|
(* Record a module type *)
|
|
|
|
let rm node =
|
|
|
|
Stypes.record (Stypes.Ti_mod node);
|
|
|
|
node
|
|
|
|
|
2010-04-02 05:53:33 -07:00
|
|
|
(* Forward declaration, to be filled in by type_module_type_of *)
|
2012-05-31 01:07:31 -07:00
|
|
|
let type_module_type_of_fwd :
|
|
|
|
(Env.t -> Parsetree.module_expr ->
|
|
|
|
Typedtree.module_expr * Types.module_type) ref
|
2010-04-02 05:53:33 -07:00
|
|
|
= ref (fun env m -> assert false)
|
|
|
|
|
1995-09-28 03:42:38 -07:00
|
|
|
(* Merge one "with" constraint in a signature *)
|
|
|
|
|
2005-03-22 19:08:37 -08:00
|
|
|
let rec add_rec_types env = function
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_type(id, decl, Trec_next) :: rem ->
|
2005-03-22 19:08:37 -08:00
|
|
|
add_rec_types (Env.add_type id decl env) rem
|
|
|
|
| _ -> env
|
|
|
|
|
2013-05-03 07:40:11 -07:00
|
|
|
let check_type_decl env loc id row_id newdecl decl rs rem =
|
2005-03-22 19:08:37 -08:00
|
|
|
let env = Env.add_type id newdecl env in
|
|
|
|
let env =
|
|
|
|
match row_id with None -> env | Some id -> Env.add_type id newdecl env in
|
|
|
|
let env = if rs = Trec_not then env else add_rec_types env rem in
|
2013-05-03 07:40:11 -07:00
|
|
|
Includemod.type_declarations env id newdecl decl;
|
|
|
|
Typedecl.check_coherence env loc id newdecl
|
2005-03-22 19:08:37 -08:00
|
|
|
|
2010-04-17 07:45:12 -07:00
|
|
|
let rec make_params n = function
|
|
|
|
[] -> []
|
|
|
|
| _ :: l -> ("a" ^ string_of_int n) :: make_params (n+1) l
|
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
let make_next_first rs rem =
|
|
|
|
if rs = Trec_first then
|
|
|
|
match rem with
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_type (id, decl, Trec_next) :: rem ->
|
|
|
|
Sig_type (id, decl, Trec_first) :: rem
|
|
|
|
| Sig_module (id, mty, Trec_next) :: rem ->
|
|
|
|
Sig_module (id, mty, Trec_first) :: rem
|
2011-07-20 02:17:07 -07:00
|
|
|
| _ -> rem
|
|
|
|
else rem
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let sig_item desc typ env loc = {
|
|
|
|
Typedtree.sig_desc = desc; sig_loc = loc; sig_env = env
|
|
|
|
}
|
|
|
|
|
2013-05-03 06:38:30 -07:00
|
|
|
let make p n i =
|
|
|
|
let open Variance in
|
|
|
|
set May_pos p (set May_neg n (set May_weak n (set Inj i null)))
|
|
|
|
|
2013-04-16 03:47:45 -07:00
|
|
|
let merge_constraint initial_env loc sg constr =
|
|
|
|
let lid =
|
|
|
|
match constr with
|
|
|
|
| Pwith_type (lid, _) | Pwith_module (lid, _) -> lid
|
|
|
|
| Pwith_typesubst {ptype_name=s} | Pwith_modsubst (s, _) ->
|
|
|
|
{loc = s.loc; txt=Lident s.txt}
|
|
|
|
in
|
2010-04-17 07:45:12 -07:00
|
|
|
let real_id = ref None in
|
2005-03-22 19:08:37 -08:00
|
|
|
let rec merge env sg namelist row_id =
|
1995-10-01 06:39:43 -07:00
|
|
|
match (sg, namelist, constr) with
|
|
|
|
([], _, _) ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, env, With_no_component lid.txt))
|
2012-05-30 07:52:37 -07:00
|
|
|
| (Sig_type(id, decl, rs) :: rem, [s],
|
2013-04-16 03:47:45 -07:00
|
|
|
Pwith_type (_, ({ptype_kind = Ptype_abstract} as sdecl)))
|
2007-10-09 03:29:37 -07:00
|
|
|
when Ident.name id = s && Typedecl.is_fixed_type sdecl ->
|
2006-09-20 04:14:37 -07:00
|
|
|
let decl_row =
|
|
|
|
{ type_params =
|
|
|
|
List.map (fun _ -> Btype.newgenvar()) sdecl.ptype_params;
|
|
|
|
type_arity = List.length sdecl.ptype_params;
|
|
|
|
type_kind = Type_abstract;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private = Private;
|
2006-09-20 04:14:37 -07:00
|
|
|
type_manifest = None;
|
|
|
|
type_variance =
|
2013-04-02 08:33:35 -07:00
|
|
|
List.map
|
2013-07-16 06:34:30 -07:00
|
|
|
(fun (_, v) ->
|
|
|
|
let (c, n) =
|
|
|
|
match v with
|
|
|
|
| Covariant -> true, false
|
|
|
|
| Contravariant -> false, true
|
|
|
|
| Invariant -> false, false
|
|
|
|
in
|
|
|
|
make (not n) (not c) false
|
|
|
|
)
|
|
|
|
sdecl.ptype_params;
|
2013-05-03 07:40:11 -07:00
|
|
|
type_loc = sdecl.ptype_loc;
|
2012-07-30 11:04:46 -07:00
|
|
|
type_newtype_level = None }
|
2006-09-20 04:14:37 -07:00
|
|
|
and id_row = Ident.create (s^"#row") in
|
|
|
|
let initial_env = Env.add_type id_row decl_row initial_env in
|
2012-05-30 07:52:37 -07:00
|
|
|
let tdecl = Typedecl.transl_with_constraint
|
2011-07-20 02:17:07 -07:00
|
|
|
initial_env id (Some(Pident id_row)) decl sdecl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let newdecl = tdecl.typ_type in
|
2013-05-03 07:40:11 -07:00
|
|
|
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
|
2006-09-20 04:14:37 -07:00
|
|
|
let decl_row = {decl_row with type_params = newdecl.type_params} in
|
2005-03-22 19:08:37 -08:00
|
|
|
let rs' = if rs = Trec_first then Trec_not else rs in
|
2012-05-30 07:52:37 -07:00
|
|
|
(Pident id, lid, Twith_type tdecl),
|
|
|
|
Sig_type(id_row, decl_row, rs') :: Sig_type(id, newdecl, rs) :: rem
|
2013-04-16 03:47:45 -07:00
|
|
|
| (Sig_type(id, decl, rs) :: rem , [s], Pwith_type (_, sdecl))
|
1995-10-01 06:39:43 -07:00
|
|
|
when Ident.name id = s ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let tdecl =
|
2011-07-20 02:17:07 -07:00
|
|
|
Typedecl.transl_with_constraint initial_env id None decl sdecl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let newdecl = tdecl.typ_type in
|
2013-05-03 07:40:11 -07:00
|
|
|
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
|
2012-05-30 07:52:37 -07:00
|
|
|
(Pident id, lid, Twith_type tdecl), Sig_type(id, newdecl, rs) :: rem
|
|
|
|
| (Sig_type(id, decl, rs) :: rem, [s], (Pwith_type _ | Pwith_typesubst _))
|
2005-03-22 19:08:37 -08:00
|
|
|
when Ident.name id = s ^ "#row" ->
|
|
|
|
merge env rem namelist (Some id)
|
2012-05-30 07:52:37 -07:00
|
|
|
| (Sig_type(id, decl, rs) :: rem, [s], Pwith_typesubst sdecl)
|
2010-04-17 07:45:12 -07:00
|
|
|
when Ident.name id = s ->
|
|
|
|
(* Check as for a normal with constraint, but discard definition *)
|
2012-05-30 07:52:37 -07:00
|
|
|
let tdecl =
|
2011-07-20 02:17:07 -07:00
|
|
|
Typedecl.transl_with_constraint initial_env id None decl sdecl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let newdecl = tdecl.typ_type in
|
2013-05-03 07:40:11 -07:00
|
|
|
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
|
2010-04-17 07:45:12 -07:00
|
|
|
real_id := Some id;
|
2012-05-30 07:52:37 -07:00
|
|
|
(Pident id, lid, Twith_typesubst tdecl),
|
2011-07-20 02:17:07 -07:00
|
|
|
make_next_first rs rem
|
2013-04-16 03:47:45 -07:00
|
|
|
| (Sig_module(id, mty, rs) :: rem, [s], Pwith_module (_, lid))
|
1995-10-01 06:39:43 -07:00
|
|
|
when Ident.name id = s ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, mty') = Typetexp.find_module initial_env loc lid.txt in
|
1996-07-25 06:18:53 -07:00
|
|
|
let newmty = Mtype.strengthen env mty' path in
|
1999-02-24 07:21:50 -08:00
|
|
|
ignore(Includemod.modtypes env newmty mty);
|
2012-05-30 07:52:37 -07:00
|
|
|
(Pident id, lid, Twith_module (path, lid)),
|
|
|
|
Sig_module(id, newmty, rs) :: rem
|
2013-04-16 03:47:45 -07:00
|
|
|
| (Sig_module(id, mty, rs) :: rem, [s], Pwith_modsubst (_, lid))
|
2010-04-17 07:45:12 -07:00
|
|
|
when Ident.name id = s ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, mty') = Typetexp.find_module initial_env loc lid.txt in
|
2010-04-17 07:45:12 -07:00
|
|
|
let newmty = Mtype.strengthen env mty' path in
|
|
|
|
ignore(Includemod.modtypes env newmty mty);
|
|
|
|
real_id := Some id;
|
2012-05-30 07:52:37 -07:00
|
|
|
(Pident id, lid, Twith_modsubst (path, lid)),
|
2011-07-20 02:17:07 -07:00
|
|
|
make_next_first rs rem
|
2012-05-30 07:52:37 -07:00
|
|
|
| (Sig_module(id, mty, rs) :: rem, s :: namelist, _)
|
2004-06-12 01:55:49 -07:00
|
|
|
when Ident.name id = s ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let ((path, path_loc, tcstr), newsg) =
|
|
|
|
merge env (extract_sig env loc mty) namelist None in
|
|
|
|
(path_concat id path, lid, tcstr),
|
|
|
|
Sig_module(id, Mty_signature newsg, rs) :: rem
|
1995-10-01 06:39:43 -07:00
|
|
|
| (item :: rem, _, _) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (cstr, items) = merge (Env.add_item item env) rem namelist row_id
|
|
|
|
in
|
|
|
|
cstr, item :: items
|
|
|
|
in
|
1996-07-25 06:18:53 -07:00
|
|
|
try
|
2012-05-30 07:52:37 -07:00
|
|
|
let names = Longident.flatten lid.txt in
|
|
|
|
let (tcstr, sg) = merge initial_env sg names None in
|
|
|
|
let sg =
|
2010-04-17 07:45:12 -07:00
|
|
|
match names, constr with
|
|
|
|
[s], Pwith_typesubst sdecl ->
|
|
|
|
let id =
|
|
|
|
match !real_id with None -> assert false | Some id -> id in
|
|
|
|
let lid =
|
|
|
|
try match sdecl.ptype_manifest with
|
2012-07-20 18:12:51 -07:00
|
|
|
| Some {ptyp_desc = Ptyp_constr (lid, stl)}
|
|
|
|
when List.length stl = List.length sdecl.ptype_params ->
|
2010-04-17 07:45:12 -07:00
|
|
|
let params =
|
|
|
|
List.map
|
|
|
|
(function {ptyp_desc=Ptyp_var s} -> s | _ -> raise Exit)
|
|
|
|
stl in
|
2013-04-02 08:33:35 -07:00
|
|
|
List.iter2 (fun x (ox, _) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
match ox with
|
|
|
|
Some y when x = y.txt -> ()
|
|
|
|
| _ -> raise Exit
|
|
|
|
) params sdecl.ptype_params;
|
2010-04-17 07:45:12 -07:00
|
|
|
lid
|
|
|
|
| _ -> raise Exit
|
2012-01-22 23:59:45 -08:00
|
|
|
with Exit ->
|
|
|
|
raise(Error(sdecl.ptype_loc, initial_env, With_need_typeconstr))
|
2010-04-17 07:45:12 -07:00
|
|
|
in
|
|
|
|
let (path, _) =
|
2012-05-30 07:52:37 -07:00
|
|
|
try Env.lookup_type lid.txt initial_env with Not_found -> assert false
|
2010-04-17 07:45:12 -07:00
|
|
|
in
|
|
|
|
let sub = Subst.add_type id path Subst.identity in
|
|
|
|
Subst.signature sub sg
|
2013-04-16 03:47:45 -07:00
|
|
|
| [s], Pwith_modsubst (_, lid) ->
|
2010-04-17 07:45:12 -07:00
|
|
|
let id =
|
|
|
|
match !real_id with None -> assert false | Some id -> id in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, _) = Typetexp.find_module initial_env loc lid.txt in
|
2010-04-17 07:45:12 -07:00
|
|
|
let sub = Subst.add_module id path Subst.identity in
|
|
|
|
Subst.signature sub sg
|
|
|
|
| _ ->
|
2012-05-30 07:52:37 -07:00
|
|
|
sg
|
|
|
|
in
|
|
|
|
(tcstr, sg)
|
1996-07-25 06:18:53 -07:00
|
|
|
with Includemod.Error explanation ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, initial_env, With_mismatch(lid.txt, explanation)))
|
1995-09-28 03:42:38 -07:00
|
|
|
|
2004-06-12 01:55:49 -07:00
|
|
|
(* Add recursion flags on declarations arising from a mutually recursive
|
|
|
|
block. *)
|
|
|
|
|
|
|
|
let map_rec fn decls rem =
|
|
|
|
match decls with
|
|
|
|
| [] -> rem
|
|
|
|
| d1 :: dl -> fn Trec_first d1 :: map_end (fn Trec_next) dl rem
|
|
|
|
|
2012-01-21 21:56:11 -08:00
|
|
|
let map_rec' = map_rec
|
|
|
|
(*
|
2005-03-22 19:08:37 -08:00
|
|
|
let rec map_rec' fn decls rem =
|
|
|
|
match decls with
|
|
|
|
| (id,_ as d1) :: dl when Btype.is_row_name (Ident.name id) ->
|
|
|
|
fn Trec_not d1 :: map_rec' fn dl rem
|
|
|
|
| _ -> map_rec fn decls rem
|
2012-01-21 21:56:11 -08:00
|
|
|
*)
|
2005-03-22 19:08:37 -08:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let rec map_rec'' fn decls rem =
|
|
|
|
match decls with
|
2013-03-25 11:20:11 -07:00
|
|
|
| d1 :: dl when Btype.is_row_name (Ident.name d1.typ_id) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
fn Trec_not d1 :: map_rec'' fn dl rem
|
|
|
|
| _ -> map_rec fn decls rem
|
|
|
|
|
2003-06-19 08:53:53 -07:00
|
|
|
(* Auxiliary for translating recursively-defined module types.
|
|
|
|
Return a module type that approximates the shape of the given module
|
|
|
|
type AST. Retain only module, type, and module type
|
|
|
|
components of signatures. For types, retain only their arity,
|
|
|
|
making them abstract otherwise. *)
|
|
|
|
|
2008-08-07 01:25:09 -07:00
|
|
|
let rec approx_modtype env smty =
|
|
|
|
match smty.pmty_desc with
|
|
|
|
Pmty_ident lid ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, info) = Typetexp.find_modtype env smty.pmty_loc lid.txt in
|
|
|
|
Mty_ident path
|
2008-08-07 01:25:09 -07:00
|
|
|
| Pmty_signature ssg ->
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_signature(approx_sig env ssg)
|
2008-08-07 01:25:09 -07:00
|
|
|
| Pmty_functor(param, sarg, sres) ->
|
|
|
|
let arg = approx_modtype env sarg in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (id, newenv) = Env.enter_module param.txt arg env in
|
2008-08-07 01:25:09 -07:00
|
|
|
let res = approx_modtype newenv sres in
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_functor(id, arg, res)
|
2008-08-07 01:25:09 -07:00
|
|
|
| Pmty_with(sbody, constraints) ->
|
|
|
|
approx_modtype env sbody
|
2010-04-02 05:53:33 -07:00
|
|
|
| Pmty_typeof smod ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (_, mty) = !type_module_type_of_fwd env smod in
|
|
|
|
mty
|
2013-03-04 06:11:15 -08:00
|
|
|
| Pmty_extension (s, _arg) ->
|
2013-04-19 00:40:57 -07:00
|
|
|
raise (Error (s.loc, env, Extension s.txt))
|
2008-08-07 01:25:09 -07:00
|
|
|
|
|
|
|
and approx_sig env ssg =
|
|
|
|
match ssg with
|
|
|
|
[] -> []
|
|
|
|
| item :: srem ->
|
|
|
|
match item.psig_desc with
|
|
|
|
| Psig_type sdecls ->
|
|
|
|
let decls = Typedecl.approx_type_decl env sdecls in
|
|
|
|
let rem = approx_sig env srem in
|
2012-05-30 07:52:37 -07:00
|
|
|
map_rec' (fun rs (id, info) -> Sig_type(id, info, rs)) decls rem
|
2013-03-04 09:39:07 -08:00
|
|
|
| Psig_module pmd ->
|
|
|
|
let mty = approx_modtype env pmd.pmd_type in
|
|
|
|
let (id, newenv) = Env.enter_module pmd.pmd_name.txt mty env in
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_module(id, mty, Trec_not) :: approx_sig newenv srem
|
2008-08-07 01:25:09 -07:00
|
|
|
| Psig_recmodule sdecls ->
|
|
|
|
let decls =
|
|
|
|
List.map
|
2013-03-04 09:39:07 -08:00
|
|
|
(fun pmd ->
|
|
|
|
(Ident.create pmd.pmd_name.txt, approx_modtype env pmd.pmd_type))
|
2008-08-07 01:25:09 -07:00
|
|
|
sdecls in
|
|
|
|
let newenv =
|
|
|
|
List.fold_left (fun env (id, mty) -> Env.add_module id mty env)
|
|
|
|
env decls in
|
2012-05-30 07:52:37 -07:00
|
|
|
map_rec (fun rs (id, mty) -> Sig_module(id, mty, rs)) decls
|
2008-08-07 01:25:09 -07:00
|
|
|
(approx_sig newenv srem)
|
2013-03-06 04:14:02 -08:00
|
|
|
| Psig_modtype d ->
|
|
|
|
let info = approx_modtype_info env d.pmtd_type in
|
|
|
|
let (id, newenv) = Env.enter_modtype d.pmtd_name.txt info env in
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_modtype(id, info) :: approx_sig newenv srem
|
2013-07-16 06:34:30 -07:00
|
|
|
| Psig_open (ovf, lid, _attrs) ->
|
2013-05-16 06:34:53 -07:00
|
|
|
let (path, mty) = type_open ovf env item.psig_loc lid in
|
2012-05-30 07:52:37 -07:00
|
|
|
approx_sig mty srem
|
2013-03-04 08:36:32 -08:00
|
|
|
| Psig_include (smty, _attrs) ->
|
2008-08-07 01:25:09 -07:00
|
|
|
let mty = approx_modtype env smty in
|
|
|
|
let sg = Subst.signature Subst.identity
|
|
|
|
(extract_sig env smty.pmty_loc mty) in
|
|
|
|
let newenv = Env.add_signature sg env in
|
|
|
|
sg @ approx_sig newenv srem
|
|
|
|
| Psig_class sdecls | Psig_class_type sdecls ->
|
|
|
|
let decls = Typeclass.approx_class_declarations env sdecls in
|
|
|
|
let rem = approx_sig env srem in
|
|
|
|
List.flatten
|
|
|
|
(map_rec
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun rs (i1, _, d1, i2, d2, i3, d3, _) ->
|
|
|
|
[Sig_class_type(i1, d1, rs);
|
|
|
|
Sig_type(i2, d2, rs);
|
|
|
|
Sig_type(i3, d3, rs)])
|
2008-08-07 01:25:09 -07:00
|
|
|
decls [rem])
|
|
|
|
| _ ->
|
|
|
|
approx_sig env srem
|
|
|
|
|
|
|
|
and approx_modtype_info env sinfo =
|
|
|
|
match sinfo with
|
2013-03-06 04:14:02 -08:00
|
|
|
None ->
|
2012-05-30 07:52:37 -07:00
|
|
|
Modtype_abstract
|
2013-03-06 04:14:02 -08:00
|
|
|
| Some smty ->
|
2012-05-30 07:52:37 -07:00
|
|
|
Modtype_manifest(approx_modtype env smty)
|
2003-06-19 08:53:53 -07:00
|
|
|
|
2003-07-01 06:05:43 -07:00
|
|
|
(* Additional validity checks on type definitions arising from
|
|
|
|
recursive modules *)
|
|
|
|
|
|
|
|
let check_recmod_typedecls env sdecls decls =
|
2012-05-30 07:52:37 -07:00
|
|
|
let recmod_ids = List.map fst3 decls in
|
2003-07-01 06:05:43 -07:00
|
|
|
List.iter2
|
2013-03-04 09:39:07 -08:00
|
|
|
(fun pmd (id, _, mty) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let mty = mty.mty_type in
|
2003-07-01 06:05:43 -07:00
|
|
|
List.iter
|
|
|
|
(fun path ->
|
2013-03-04 09:39:07 -08:00
|
|
|
Typedecl.check_recmod_typedecl env pmd.pmd_type.pmty_loc recmod_ids
|
2003-07-01 06:05:43 -07:00
|
|
|
path (Env.find_type path env))
|
|
|
|
(Mtype.type_paths env (Pident id) mty))
|
|
|
|
sdecls decls
|
|
|
|
|
1999-03-02 06:49:38 -08:00
|
|
|
(* Auxiliaries for checking uniqueness of names in signatures and structures *)
|
|
|
|
|
2013-03-19 00:22:12 -07:00
|
|
|
module StringSet =
|
|
|
|
Set.Make(struct type t = string let compare (x:t) y = compare x y end)
|
1999-03-02 06:49:38 -08:00
|
|
|
|
|
|
|
let check cl loc set_ref name =
|
|
|
|
if StringSet.mem name !set_ref
|
2012-01-22 23:59:45 -08:00
|
|
|
then raise(Error(loc, Env.empty, Repeated_name(cl, name)))
|
1999-03-02 06:49:38 -08:00
|
|
|
else set_ref := StringSet.add name !set_ref
|
|
|
|
|
|
|
|
let check_sig_item type_names module_names modtype_names loc = function
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_type(id, _, _) ->
|
1999-03-02 06:49:38 -08:00
|
|
|
check "type" loc type_names (Ident.name id)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_module(id, _, _) ->
|
1999-03-02 06:49:38 -08:00
|
|
|
check "module" loc module_names (Ident.name id)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_modtype(id, _) ->
|
1999-03-02 06:49:38 -08:00
|
|
|
check "module type" loc modtype_names (Ident.name id)
|
|
|
|
| _ -> ()
|
|
|
|
|
2013-06-16 19:03:36 -07:00
|
|
|
let rec remove_duplicates val_ids exn_ids = function
|
2010-04-17 07:45:12 -07:00
|
|
|
[] -> []
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_value (id, _) :: rem
|
2013-09-04 08:12:37 -07:00
|
|
|
when List.exists (Ident.equal id) val_ids ->
|
|
|
|
remove_duplicates val_ids exn_ids rem
|
2013-06-16 19:03:36 -07:00
|
|
|
| Sig_exception(id, _) :: rem
|
2013-09-04 08:12:37 -07:00
|
|
|
when List.exists (Ident.equal id) exn_ids ->
|
|
|
|
remove_duplicates val_ids exn_ids rem
|
2013-06-16 19:03:36 -07:00
|
|
|
| f :: rem -> f :: remove_duplicates val_ids exn_ids rem
|
2010-04-17 07:45:12 -07:00
|
|
|
|
|
|
|
let rec get_values = function
|
|
|
|
[] -> []
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_value (id, _) :: rem -> id :: get_values rem
|
2010-04-17 07:45:12 -07:00
|
|
|
| f :: rem -> get_values rem
|
|
|
|
|
2013-06-16 19:03:36 -07:00
|
|
|
let rec get_exceptions = function
|
|
|
|
[] -> []
|
|
|
|
| Sig_exception (id, _) :: rem -> id :: get_exceptions rem
|
|
|
|
| f :: rem -> get_exceptions rem
|
|
|
|
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Check and translate a module type expression *)
|
|
|
|
|
2009-10-26 03:53:16 -07:00
|
|
|
let transl_modtype_longident loc env lid =
|
2010-05-18 10:18:24 -07:00
|
|
|
let (path, info) = Typetexp.find_modtype env loc lid in
|
|
|
|
path
|
2009-10-26 03:53:16 -07:00
|
|
|
|
2013-03-25 07:16:07 -07:00
|
|
|
let mkmty desc typ env loc attrs =
|
2012-05-30 07:52:37 -07:00
|
|
|
let mty = {
|
|
|
|
mty_desc = desc;
|
|
|
|
mty_type = typ;
|
|
|
|
mty_loc = loc;
|
|
|
|
mty_env = env;
|
2013-03-25 07:16:07 -07:00
|
|
|
mty_attributes = attrs;
|
2012-05-30 07:52:37 -07:00
|
|
|
} in
|
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_module_type mty);
|
|
|
|
mty
|
|
|
|
|
|
|
|
let mksig desc env loc =
|
|
|
|
let sg = { sig_desc = desc; sig_loc = loc; sig_env = env } in
|
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_signature_item sg);
|
|
|
|
sg
|
|
|
|
|
|
|
|
(* let signature sg = List.map (fun item -> item.sig_type) sg *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec transl_modtype env smty =
|
2012-05-30 07:52:37 -07:00
|
|
|
let loc = smty.pmty_loc in
|
1995-05-04 03:15:53 -07:00
|
|
|
match smty.pmty_desc with
|
|
|
|
Pmty_ident lid ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let path = transl_modtype_longident loc env lid.txt in
|
|
|
|
mkmty (Tmty_ident (path, lid)) (Mty_ident path) env loc
|
2013-03-25 07:16:07 -07:00
|
|
|
smty.pmty_attributes
|
1995-08-28 04:23:33 -07:00
|
|
|
| Pmty_signature ssg ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let sg = transl_signature env ssg in
|
|
|
|
mkmty (Tmty_signature sg) (Mty_signature sg.sig_type) env loc
|
2013-03-25 07:16:07 -07:00
|
|
|
smty.pmty_attributes
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmty_functor(param, sarg, sres) ->
|
|
|
|
let arg = transl_modtype env sarg in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (id, newenv) = Env.enter_module param.txt arg.mty_type env in
|
1995-05-04 03:15:53 -07:00
|
|
|
let res = transl_modtype newenv sres in
|
2012-05-30 07:52:37 -07:00
|
|
|
mkmty (Tmty_functor (id, param, arg, res))
|
|
|
|
(Mty_functor(id, arg.mty_type, res.mty_type)) env loc
|
2013-03-25 07:16:07 -07:00
|
|
|
smty.pmty_attributes
|
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
|
2012-05-30 07:52:37 -07:00
|
|
|
let init_sg = extract_sig env sbody.pmty_loc body.mty_type in
|
|
|
|
let (tcstrs, final_sg) =
|
1995-09-28 03:42:38 -07:00
|
|
|
List.fold_left
|
2013-04-16 03:47:45 -07:00
|
|
|
(fun (tcstrs,sg) sdecl ->
|
|
|
|
let (tcstr, sg) = merge_constraint env smty.pmty_loc sg sdecl
|
2012-05-30 07:52:37 -07:00
|
|
|
in
|
|
|
|
(tcstr :: tcstrs, sg)
|
|
|
|
)
|
|
|
|
([],init_sg) constraints in
|
|
|
|
mkmty (Tmty_with ( body, tcstrs))
|
2013-03-25 07:16:07 -07:00
|
|
|
(Mtype.freshen (Mty_signature final_sg)) env loc
|
|
|
|
smty.pmty_attributes
|
2010-04-02 05:53:33 -07:00
|
|
|
| Pmty_typeof smod ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let tmty, mty = !type_module_type_of_fwd env smod in
|
2013-03-25 07:16:07 -07:00
|
|
|
mkmty (Tmty_typeof tmty) mty env loc smty.pmty_attributes
|
2013-03-04 06:11:15 -08:00
|
|
|
| Pmty_extension (s, _arg) ->
|
2013-04-19 00:40:57 -07:00
|
|
|
raise (Error (s.loc, env, Extension s.txt))
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2006-09-20 04:14:37 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
and transl_signature env sg =
|
1999-03-02 06:49:38 -08:00
|
|
|
let type_names = ref StringSet.empty
|
|
|
|
and module_names = ref StringSet.empty
|
|
|
|
and modtype_names = ref StringSet.empty in
|
|
|
|
let rec transl_sig env sg =
|
|
|
|
Ctype.init_def(Ident.current_time());
|
|
|
|
match sg with
|
2012-05-30 07:52:37 -07:00
|
|
|
[] -> [], [], env
|
1999-03-02 06:49:38 -08:00
|
|
|
| item :: srem ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let loc = item.psig_loc in
|
1999-03-02 06:49:38 -08:00
|
|
|
match item.psig_desc with
|
2013-03-06 04:00:18 -08:00
|
|
|
| Psig_value sdesc ->
|
2013-03-25 11:04:40 -07:00
|
|
|
let (tdesc, newenv) = Typedecl.transl_value_decl env item.psig_loc sdesc in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem,rem, final_env) = transl_sig newenv srem in
|
2013-03-25 11:04:40 -07:00
|
|
|
mksig (Tsig_value tdesc) env loc :: trem,
|
|
|
|
(if List.exists (Ident.equal tdesc.val_id) (get_values rem) then rem
|
|
|
|
else Sig_value(tdesc.val_id, tdesc.val_val) :: rem),
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
1999-03-02 06:49:38 -08:00
|
|
|
| Psig_type sdecls ->
|
|
|
|
List.iter
|
2013-03-06 03:47:59 -08:00
|
|
|
(fun decl ->
|
|
|
|
check "type" item.psig_loc type_names decl.ptype_name.txt)
|
1999-03-02 06:49:38 -08:00
|
|
|
sdecls;
|
|
|
|
let (decls, newenv) = Typedecl.transl_type_decl env sdecls in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
|
|
|
mksig (Tsig_type decls) env loc :: trem,
|
2013-03-25 11:20:11 -07:00
|
|
|
map_rec'' (fun rs td ->
|
|
|
|
Sig_type(td.typ_id, td.typ_type, rs)) decls rem,
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
2013-03-05 04:37:17 -08:00
|
|
|
| Psig_exception sarg ->
|
2013-03-25 11:42:45 -07:00
|
|
|
let (arg, decl, newenv) = Typedecl.transl_exception env sarg in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2013-07-16 06:34:30 -07:00
|
|
|
let id = arg.cd_id in
|
2013-03-25 11:42:45 -07:00
|
|
|
mksig (Tsig_exception arg) env loc :: trem,
|
2013-06-16 19:03:36 -07:00
|
|
|
(if List.exists (Ident.equal id) (get_exceptions rem) then rem
|
2013-07-16 06:34:30 -07:00
|
|
|
else Sig_exception(id, decl) :: rem),
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
2013-03-04 09:39:07 -08:00
|
|
|
| Psig_module pmd ->
|
|
|
|
check "module" item.psig_loc module_names pmd.pmd_name.txt;
|
|
|
|
let tmty = transl_modtype env pmd.pmd_type in
|
2012-05-30 07:52:37 -07:00
|
|
|
let mty = tmty.mty_type in
|
2013-03-04 09:39:07 -08:00
|
|
|
let (id, newenv) = Env.enter_module pmd.pmd_name.txt mty env in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2013-03-25 10:47:28 -07:00
|
|
|
mksig (Tsig_module {md_id=id; md_name=pmd.pmd_name; md_type=tmty; md_attributes=pmd.pmd_attributes}) env loc :: trem,
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_module(id, mty, Trec_not) :: rem,
|
|
|
|
final_env
|
2003-06-19 08:53:53 -07:00
|
|
|
| Psig_recmodule sdecls ->
|
|
|
|
List.iter
|
2013-03-04 09:39:07 -08:00
|
|
|
(fun pmd ->
|
|
|
|
check "module" item.psig_loc module_names pmd.pmd_name.txt)
|
2003-06-19 08:53:53 -07:00
|
|
|
sdecls;
|
|
|
|
let (decls, newenv) =
|
|
|
|
transl_recmodule_modtypes item.psig_loc env sdecls in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
|
|
|
mksig (Tsig_recmodule decls) env loc :: trem,
|
2013-03-25 10:47:28 -07:00
|
|
|
map_rec (fun rs md -> Sig_module(md.md_id, md.md_type.mty_type, rs))
|
2012-05-31 01:07:31 -07:00
|
|
|
decls rem,
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
2013-04-18 06:14:53 -07:00
|
|
|
| Psig_modtype pmtd ->
|
|
|
|
let newenv, mtd, sg =
|
|
|
|
transl_modtype_decl modtype_names env item.psig_loc pmtd
|
|
|
|
in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2013-04-18 06:14:53 -07:00
|
|
|
mksig (Tsig_modtype mtd) env loc :: trem,
|
|
|
|
sg :: rem,
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
2013-07-16 06:34:30 -07:00
|
|
|
| Psig_open (ovf, lid, attrs) ->
|
2013-05-16 06:34:53 -07:00
|
|
|
let (path, newenv) = type_open ovf env item.psig_loc lid in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2013-07-16 06:34:30 -07:00
|
|
|
mksig (Tsig_open (ovf, path,lid,attrs)) env loc :: trem,
|
2013-05-16 06:34:53 -07:00
|
|
|
rem, final_env
|
2013-03-25 07:16:07 -07:00
|
|
|
| Psig_include (smty, attrs) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let tmty = transl_modtype env smty in
|
|
|
|
let mty = tmty.mty_type in
|
2000-12-01 01:35:00 -08:00
|
|
|
let sg = Subst.signature Subst.identity
|
|
|
|
(extract_sig env smty.pmty_loc mty) in
|
1999-03-02 06:49:38 -08:00
|
|
|
List.iter
|
|
|
|
(check_sig_item type_names module_names modtype_names
|
|
|
|
item.psig_loc)
|
|
|
|
sg;
|
|
|
|
let newenv = Env.add_signature sg env in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2013-03-25 07:16:07 -07:00
|
|
|
mksig (Tsig_include (tmty, sg, attrs)) env loc :: trem,
|
2013-06-16 19:03:36 -07:00
|
|
|
remove_duplicates (get_values rem) (get_exceptions rem) sg @ rem,
|
|
|
|
final_env
|
1999-03-02 06:49:38 -08:00
|
|
|
| Psig_class cl ->
|
|
|
|
List.iter
|
|
|
|
(fun {pci_name = name} ->
|
2012-05-30 07:52:37 -07:00
|
|
|
check "type" item.psig_loc type_names name.txt )
|
1999-03-02 06:49:38 -08:00
|
|
|
cl;
|
|
|
|
let (classes, newenv) = Typeclass.class_descriptions env cl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem, rem, final_env) = transl_sig newenv srem in
|
2012-05-31 01:07:31 -07:00
|
|
|
mksig (Tsig_class
|
|
|
|
(List.map2
|
|
|
|
(fun pcl tcl ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let (_, _, _, _, _, _, _, _, _, _, _, tcl) = tcl in
|
2012-05-31 01:07:31 -07:00
|
|
|
tcl)
|
2012-07-30 11:04:46 -07:00
|
|
|
cl classes)) env loc
|
2012-05-31 01:07:31 -07:00
|
|
|
:: trem,
|
|
|
|
List.flatten
|
|
|
|
(map_rec
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun rs (i, _, d, i', d', i'', d'', i''', d''', _, _, _) ->
|
2012-05-31 01:07:31 -07:00
|
|
|
[Sig_class(i, d, rs);
|
|
|
|
Sig_class_type(i', d', rs);
|
|
|
|
Sig_type(i'', d'', rs);
|
|
|
|
Sig_type(i''', d''', rs)])
|
|
|
|
classes [rem]),
|
2012-05-30 07:52:37 -07:00
|
|
|
final_env
|
1999-03-02 06:49:38 -08:00
|
|
|
| Psig_class_type cl ->
|
|
|
|
List.iter
|
|
|
|
(fun {pci_name = name} ->
|
2012-05-30 07:52:37 -07:00
|
|
|
check "type" item.psig_loc type_names name.txt)
|
1999-03-02 06:49:38 -08:00
|
|
|
cl;
|
|
|
|
let (classes, newenv) = Typeclass.class_type_declarations env cl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (trem,rem, final_env) = transl_sig newenv srem in
|
|
|
|
mksig (Tsig_class_type (List.map2 (fun pcl tcl ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let (_, _, _, _, _, _, _, tcl) = tcl in
|
|
|
|
tcl
|
|
|
|
) cl classes)) env loc :: trem,
|
1999-03-02 06:49:38 -08:00
|
|
|
List.flatten
|
2004-06-12 01:55:49 -07:00
|
|
|
(map_rec
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun rs (i, _, d, i', d', i'', d'', _) ->
|
|
|
|
[Sig_class_type(i, d, rs);
|
|
|
|
Sig_type(i', d', rs);
|
|
|
|
Sig_type(i'', d'', rs)])
|
|
|
|
classes [rem]),
|
|
|
|
final_env
|
2013-03-25 07:16:07 -07:00
|
|
|
| Psig_attribute x ->
|
|
|
|
let (trem,rem, final_env) = transl_sig env srem in
|
|
|
|
mksig (Tsig_attribute x) env loc :: trem, rem, final_env
|
2013-03-05 03:46:25 -08:00
|
|
|
| Psig_extension ((s, _), _) ->
|
2013-04-19 00:40:57 -07:00
|
|
|
raise (Error (s.loc, env, Extension s.txt))
|
2012-05-30 07:52:37 -07:00
|
|
|
in
|
|
|
|
let previous_saved_types = Cmt_format.get_saved_types () in
|
|
|
|
let (trem, rem, final_env) = transl_sig (Env.in_signature env) sg in
|
|
|
|
let sg = { sig_items = trem; sig_type = rem; sig_final_env = final_env } in
|
2012-05-31 01:07:31 -07:00
|
|
|
Cmt_format.set_saved_types
|
|
|
|
((Cmt_format.Partial_signature sg) :: previous_saved_types);
|
2012-05-30 07:52:37 -07:00
|
|
|
sg
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2013-04-18 06:14:53 -07:00
|
|
|
and transl_modtype_decl modtype_names env loc
|
|
|
|
{pmtd_name; pmtd_type; pmtd_attributes} =
|
|
|
|
check "module type" loc modtype_names pmtd_name.txt;
|
|
|
|
let (tinfo, info) = transl_modtype_info env pmtd_type in
|
|
|
|
let (id, newenv) = Env.enter_modtype pmtd_name.txt info env in
|
|
|
|
let mtd =
|
|
|
|
{
|
|
|
|
mtd_id=id;
|
|
|
|
mtd_name=pmtd_name;
|
|
|
|
mtd_type=tinfo;
|
|
|
|
mtd_attributes=pmtd_attributes;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
newenv, mtd, Sig_modtype(id, info)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
and transl_modtype_info env sinfo =
|
|
|
|
match sinfo with
|
2013-03-06 04:14:02 -08:00
|
|
|
None ->
|
2013-03-25 10:47:28 -07:00
|
|
|
None, Modtype_abstract
|
2013-03-06 04:14:02 -08:00
|
|
|
| Some smty ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let tmty = transl_modtype env smty in
|
2013-03-25 10:47:28 -07:00
|
|
|
Some tmty, Modtype_manifest tmty.mty_type
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-06-19 08:53:53 -07:00
|
|
|
and transl_recmodule_modtypes loc env sdecls =
|
|
|
|
let make_env curr =
|
|
|
|
List.fold_left
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun env (id, _, mty) -> Env.add_module id mty env)
|
|
|
|
env curr in
|
|
|
|
let make_env2 curr =
|
|
|
|
List.fold_left
|
|
|
|
(fun env (id, _, mty) -> Env.add_module id mty.mty_type env)
|
2003-06-19 08:53:53 -07:00
|
|
|
env curr in
|
|
|
|
let transition env_c curr =
|
|
|
|
List.map2
|
2013-03-04 09:39:07 -08:00
|
|
|
(fun pmd (id, id_loc, mty) -> (id, id_loc, transl_modtype env_c pmd.pmd_type))
|
2003-06-19 08:53:53 -07:00
|
|
|
sdecls curr in
|
2013-07-16 06:34:30 -07:00
|
|
|
let ids = List.map (fun x -> Ident.create x.pmd_name.txt) sdecls in
|
2013-04-29 08:39:00 -07:00
|
|
|
let approx_env =
|
|
|
|
(*
|
|
|
|
cf #5965
|
|
|
|
We use a dummy module type in order to detect a reference to one
|
|
|
|
of the module being defined during the call to approx_modtype.
|
|
|
|
It will be detected in Env.lookup_module.
|
|
|
|
*)
|
|
|
|
List.fold_left
|
|
|
|
(fun env id ->
|
|
|
|
let dummy = Mty_ident (Path.Pident (Ident.create "#recmod#")) in
|
|
|
|
Env.add_module id dummy env
|
|
|
|
)
|
|
|
|
env ids
|
|
|
|
in
|
2003-06-19 08:53:53 -07:00
|
|
|
let init =
|
2013-04-29 08:39:00 -07:00
|
|
|
List.map2
|
2013-07-16 06:34:30 -07:00
|
|
|
(fun id pmd ->
|
|
|
|
(id, pmd.pmd_name, approx_modtype approx_env pmd.pmd_type))
|
2013-04-29 08:39:00 -07:00
|
|
|
ids sdecls
|
|
|
|
in
|
2008-01-11 08:13:18 -08:00
|
|
|
let env0 = make_env init in
|
|
|
|
let dcl1 = transition env0 init in
|
2012-05-30 07:52:37 -07:00
|
|
|
let env1 = make_env2 dcl1 in
|
2008-08-07 01:39:14 -07:00
|
|
|
check_recmod_typedecls env1 sdecls dcl1;
|
2008-01-11 08:13:18 -08:00
|
|
|
let dcl2 = transition env1 dcl1 in
|
|
|
|
(*
|
|
|
|
List.iter
|
|
|
|
(fun (id, mty) ->
|
|
|
|
Format.printf "%a: %a@." Printtyp.ident id Printtyp.modtype mty)
|
|
|
|
dcl2;
|
|
|
|
*)
|
2012-05-30 07:52:37 -07:00
|
|
|
let env2 = make_env2 dcl2 in
|
2008-08-07 01:25:09 -07:00
|
|
|
check_recmod_typedecls env2 sdecls dcl2;
|
2013-03-25 10:47:28 -07:00
|
|
|
let dcl2 =
|
|
|
|
List.map2
|
|
|
|
(fun pmd (id, id_loc, mty) ->
|
|
|
|
{md_id=id; md_name=id_loc; md_type=mty; md_attributes=pmd.pmd_attributes})
|
|
|
|
sdecls dcl2
|
|
|
|
in
|
2008-01-11 08:13:18 -08:00
|
|
|
(dcl2, env2)
|
2003-06-19 08:53:53 -07:00
|
|
|
|
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
|
2012-05-30 07:52:37 -07:00
|
|
|
Tmod_ident (p,_) -> p
|
2009-07-15 07:06:37 -07:00
|
|
|
| Tmod_apply(funct, arg, coercion) when !Clflags.applicative_functors ->
|
1995-08-23 04:55:54 -07:00
|
|
|
Papply(path_of_module funct, path_of_module arg)
|
|
|
|
| _ -> raise Not_a_path
|
|
|
|
|
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
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_ident p -> true
|
|
|
|
| Mty_signature sg -> List.for_all closed_signature_item sg
|
|
|
|
| Mty_functor(id, param, body) -> closed_modtype body
|
1996-05-22 09:22:33 -07:00
|
|
|
|
|
|
|
and closed_signature_item = function
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_value(id, desc) -> Ctype.closed_schema desc.val_type
|
|
|
|
| Sig_module(id, mty, _) -> closed_modtype mty
|
1996-05-22 09:22:33 -07:00
|
|
|
| _ -> true
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let check_nongen_scheme env str =
|
|
|
|
match str.str_desc with
|
2013-06-03 08:14:19 -07:00
|
|
|
Tstr_value(rec_flag, pat_exp_list) ->
|
1996-05-22 09:22:33 -07:00
|
|
|
List.iter
|
2013-06-03 08:14:19 -07:00
|
|
|
(fun {vb_expr=exp} ->
|
1997-03-18 13:06:28 -08:00
|
|
|
if not (Ctype.closed_schema exp.exp_type) then
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(exp.exp_loc, env, Non_generalizable exp.exp_type)))
|
1996-05-22 09:22:33 -07:00
|
|
|
pat_exp_list
|
2013-03-26 01:09:26 -07:00
|
|
|
| Tstr_module {mb_expr=md;_} ->
|
1996-05-22 09:22:33 -07:00
|
|
|
if not (closed_modtype md.mod_type) then
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(md.mod_loc, env, Non_generalizable_module md.mod_type))
|
1996-05-22 09:22:33 -07:00
|
|
|
| _ -> ()
|
|
|
|
|
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
|
|
|
|
2003-06-19 08:53:53 -07:00
|
|
|
(* Helpers for typing recursive modules *)
|
|
|
|
|
|
|
|
let anchor_submodule name anchor =
|
|
|
|
match anchor with None -> None | Some p -> Some(Pdot(p, name, nopos))
|
|
|
|
let anchor_recmodule id anchor =
|
|
|
|
Some (Pident id)
|
|
|
|
|
|
|
|
let enrich_type_decls anchor decls oldenv newenv =
|
|
|
|
match anchor with
|
|
|
|
None -> newenv
|
|
|
|
| Some p ->
|
|
|
|
List.fold_left
|
2013-03-25 11:20:11 -07:00
|
|
|
(fun e info ->
|
|
|
|
let id = info.typ_id in
|
2003-06-19 08:53:53 -07:00
|
|
|
let info' =
|
2012-05-31 01:07:31 -07:00
|
|
|
Mtype.enrich_typedecl oldenv (Pdot(p, Ident.name id, nopos))
|
|
|
|
info.typ_type
|
2003-06-19 08:53:53 -07:00
|
|
|
in
|
|
|
|
Env.add_type id info' e)
|
|
|
|
oldenv decls
|
|
|
|
|
|
|
|
let enrich_module_type anchor name mty env =
|
|
|
|
match anchor with
|
|
|
|
None -> mty
|
|
|
|
| Some p -> Mtype.enrich_modtype env (Pdot(p, name, nopos)) mty
|
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
let check_recmodule_inclusion env bindings =
|
|
|
|
(* PR#4450, PR#4470: consider
|
|
|
|
module rec X : DECL = MOD where MOD has inferred type ACTUAL
|
|
|
|
The "natural" typing condition
|
|
|
|
E, X: ACTUAL |- ACTUAL <: DECL
|
2008-12-03 10:09:09 -08:00
|
|
|
leads to circularities through manifest types.
|
2008-01-11 08:13:18 -08:00
|
|
|
Instead, we "unroll away" the potential circularities a finite number
|
|
|
|
of times. The (weaker) condition we implement is:
|
|
|
|
E, X: DECL,
|
|
|
|
X1: ACTUAL,
|
|
|
|
X2: ACTUAL{X <- X1}/X1
|
|
|
|
...
|
|
|
|
Xn: ACTUAL{X <- X(n-1)}/X(n-1)
|
|
|
|
|- ACTUAL{X <- Xn}/Xn <: DECL{X <- Xn}
|
|
|
|
so that manifest types rooted at X(n+1) are expanded in terms of X(n),
|
|
|
|
avoiding circularities. The strengthenings ensure that
|
|
|
|
Xn.t = X(n-1).t = ... = X2.t = X1.t.
|
|
|
|
N can be chosen arbitrarily; larger values of N result in more
|
|
|
|
recursive definitions being accepted. A good choice appears to be
|
|
|
|
the number of mutually recursive declarations. *)
|
|
|
|
|
|
|
|
let subst_and_strengthen env s id mty =
|
|
|
|
Mtype.strengthen env (Subst.modtype s mty)
|
|
|
|
(Subst.module_path s (Pident id)) in
|
|
|
|
|
|
|
|
let rec check_incl first_time n env s =
|
|
|
|
if n > 0 then begin
|
|
|
|
(* Generate fresh names Y_i for the rec. bound module idents X_i *)
|
|
|
|
let bindings1 =
|
|
|
|
List.map
|
2013-03-26 01:09:26 -07:00
|
|
|
(fun (id, _, mty_decl, modl, mty_actual, _attrs) ->
|
2008-01-11 08:13:18 -08:00
|
|
|
(id, Ident.rename id, mty_actual))
|
|
|
|
bindings in
|
|
|
|
(* Enter the Y_i in the environment with their actual types substituted
|
|
|
|
by the input substitution s *)
|
|
|
|
let env' =
|
|
|
|
List.fold_left
|
|
|
|
(fun env (id, id', mty_actual) ->
|
|
|
|
let mty_actual' =
|
|
|
|
if first_time
|
|
|
|
then mty_actual
|
|
|
|
else subst_and_strengthen env s id mty_actual in
|
|
|
|
Env.add_module id' mty_actual' env)
|
|
|
|
env bindings1 in
|
|
|
|
(* Build the output substitution Y_i <- X_i *)
|
|
|
|
let s' =
|
|
|
|
List.fold_left
|
|
|
|
(fun s (id, id', mty_actual) ->
|
|
|
|
Subst.add_module id (Pident id') s)
|
|
|
|
Subst.identity bindings1 in
|
|
|
|
(* Recurse with env' and s' *)
|
|
|
|
check_incl false (n-1) env' s'
|
|
|
|
end else begin
|
|
|
|
(* Base case: check inclusion of s(mty_actual) in s(mty_decl)
|
|
|
|
and insert coercion if needed *)
|
2013-03-26 01:09:26 -07:00
|
|
|
let check_inclusion (id, id_loc, mty_decl, modl, mty_actual, attrs) =
|
2012-05-30 07:52:37 -07:00
|
|
|
let mty_decl' = Subst.modtype s mty_decl.mty_type
|
2008-01-11 08:13:18 -08:00
|
|
|
and mty_actual' = subst_and_strengthen env s id mty_actual in
|
|
|
|
let coercion =
|
|
|
|
try
|
|
|
|
Includemod.modtypes env mty_actual' mty_decl'
|
|
|
|
with Includemod.Error msg ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(modl.mod_loc, env, Not_included msg)) in
|
2008-01-11 08:13:18 -08:00
|
|
|
let modl' =
|
2012-05-30 07:52:37 -07:00
|
|
|
{ mod_desc = Tmod_constraint(modl, mty_decl.mty_type,
|
|
|
|
Tmodtype_explicit mty_decl, coercion);
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_type = mty_decl.mty_type;
|
|
|
|
mod_env = env;
|
|
|
|
mod_loc = modl.mod_loc;
|
|
|
|
mod_attributes = [];
|
|
|
|
} in
|
2013-03-26 01:09:26 -07:00
|
|
|
{
|
|
|
|
mb_id = id;
|
|
|
|
mb_name = id_loc;
|
|
|
|
mb_expr = modl';
|
|
|
|
mb_attributes = attrs;
|
|
|
|
}
|
|
|
|
in
|
2008-01-11 08:13:18 -08:00
|
|
|
List.map check_inclusion bindings
|
|
|
|
end
|
|
|
|
in check_incl true (List.length bindings) env Subst.identity
|
|
|
|
|
2010-10-21 16:59:33 -07:00
|
|
|
(* Helper for unpack *)
|
|
|
|
|
2011-12-14 02:26:15 -08:00
|
|
|
let rec package_constraints env loc mty constrs =
|
|
|
|
if constrs = [] then mty
|
|
|
|
else let sg = extract_sig env loc mty in
|
|
|
|
let sg' =
|
|
|
|
List.map
|
|
|
|
(function
|
2012-05-31 01:07:31 -07:00
|
|
|
| Sig_type (id, ({type_params=[]} as td), rs)
|
|
|
|
when List.mem_assoc [Ident.name id] constrs ->
|
2011-12-14 02:26:15 -08:00
|
|
|
let ty = List.assoc [Ident.name id] constrs in
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_type (id, {td with type_manifest = Some ty}, rs)
|
|
|
|
| Sig_module (id, mty, rs) ->
|
2011-12-14 02:26:15 -08:00
|
|
|
let rec aux = function
|
2012-05-31 01:07:31 -07:00
|
|
|
| (m :: ((_ :: _) as l), t) :: rest when m = Ident.name id ->
|
|
|
|
(l, t) :: aux rest
|
2011-12-14 02:26:15 -08:00
|
|
|
| _ :: rest -> aux rest
|
|
|
|
| [] -> []
|
|
|
|
in
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_module (id, package_constraints env loc mty (aux constrs), rs)
|
2011-12-14 02:26:15 -08:00
|
|
|
| item -> item
|
|
|
|
)
|
|
|
|
sg
|
|
|
|
in
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_signature sg'
|
2011-12-14 02:26:15 -08:00
|
|
|
|
2010-10-21 16:59:33 -07:00
|
|
|
let modtype_of_package env loc p nl tl =
|
|
|
|
try match Env.find_modtype p env with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Modtype_manifest mty when nl <> [] ->
|
2012-05-31 01:07:31 -07:00
|
|
|
package_constraints env loc mty
|
|
|
|
(List.combine (List.map Longident.flatten nl) tl)
|
2010-10-21 16:59:33 -07:00
|
|
|
| _ ->
|
2012-05-30 07:52:37 -07:00
|
|
|
if nl = [] then Mty_ident p
|
2012-01-22 23:59:45 -08:00
|
|
|
else raise(Error(loc, env, Signature_expected))
|
2010-10-21 16:59:33 -07:00
|
|
|
with Not_found ->
|
2013-01-29 06:21:12 -08:00
|
|
|
let error = Typetexp.Unbound_modtype (Ctype.lid_of_path p) in
|
|
|
|
raise(Typetexp.Error(loc, env, error))
|
2010-10-21 16:59:33 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let wrap_constraint env arg mty explicit =
|
2010-10-21 16:59:33 -07:00
|
|
|
let coercion =
|
|
|
|
try
|
|
|
|
Includemod.modtypes env arg.mod_type mty
|
|
|
|
with Includemod.Error msg ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(arg.mod_loc, env, Not_included msg)) in
|
2012-05-30 07:52:37 -07:00
|
|
|
{ mod_desc = Tmod_constraint(arg, mty, explicit, coercion);
|
2010-10-21 16:59:33 -07:00
|
|
|
mod_type = mty;
|
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = [];
|
2010-10-21 16:59:33 -07:00
|
|
|
mod_loc = arg.mod_loc }
|
|
|
|
|
1995-05-05 03:05:18 -07:00
|
|
|
(* Type a module value expression *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2010-08-02 07:37:22 -07:00
|
|
|
let rec type_module sttn funct_body anchor env smod =
|
1995-05-04 03:15:53 -07:00
|
|
|
match smod.pmod_desc with
|
|
|
|
Pmod_ident lid ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, mty) = Typetexp.find_module env smod.pmod_loc lid.txt in
|
|
|
|
rm { mod_desc = Tmod_ident (path, lid);
|
2010-08-02 07:37:22 -07:00
|
|
|
mod_type = if sttn then Mtype.strengthen env mty path else mty;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_loc = smod.pmod_loc }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmod_structure sstr ->
|
2010-08-02 07:37:22 -07:00
|
|
|
let (str, sg, finalenv) =
|
|
|
|
type_structure funct_body anchor env sstr smod.pmod_loc in
|
2003-04-01 22:57:15 -08:00
|
|
|
rm { mod_desc = Tmod_structure str;
|
2012-05-30 07:52:37 -07:00
|
|
|
mod_type = Mty_signature sg;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_loc = smod.pmod_loc }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmod_functor(name, smty, sbody) ->
|
|
|
|
let mty = transl_modtype env smty in
|
2012-05-30 07:52:37 -07:00
|
|
|
let (id, newenv) = Env.enter_module name.txt mty.mty_type env in
|
2010-08-02 07:37:22 -07:00
|
|
|
let body = type_module sttn true None newenv sbody in
|
2012-05-30 07:52:37 -07:00
|
|
|
rm { mod_desc = Tmod_functor(id, name, mty, body);
|
|
|
|
mod_type = Mty_functor(id, mty.mty_type, body.mod_type);
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_loc = smod.pmod_loc }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmod_apply(sfunct, sarg) ->
|
2010-08-02 07:37:22 -07:00
|
|
|
let arg = type_module true funct_body None env sarg in
|
|
|
|
let path = try Some (path_of_module arg) with Not_a_path -> None in
|
|
|
|
let funct =
|
|
|
|
type_module (sttn && path <> None) funct_body None env sfunct in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin match Mtype.scrape env funct.mod_type with
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_functor(param, mty_param, mty_res) as mty_functor ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let coercion =
|
|
|
|
try
|
|
|
|
Includemod.modtypes env arg.mod_type mty_param
|
|
|
|
with Includemod.Error msg ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(sarg.pmod_loc, env, Not_included msg)) in
|
1995-05-04 03:15:53 -07:00
|
|
|
let mty_appl =
|
2010-08-02 07:37:22 -07:00
|
|
|
match path with
|
|
|
|
Some path ->
|
|
|
|
Subst.modtype (Subst.add_module param path Subst.identity)
|
|
|
|
mty_res
|
|
|
|
| None ->
|
|
|
|
try
|
|
|
|
Mtype.nondep_supertype
|
|
|
|
(Env.add_module param arg.mod_type env) param mty_res
|
|
|
|
with Not_found ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(smod.pmod_loc, env,
|
2010-08-02 07:37:22 -07:00
|
|
|
Cannot_eliminate_dependency mty_functor))
|
|
|
|
in
|
2003-04-01 22:57:15 -08:00
|
|
|
rm { mod_desc = Tmod_apply(funct, arg, coercion);
|
|
|
|
mod_type = mty_appl;
|
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2003-04-01 22:57:15 -08:00
|
|
|
mod_loc = smod.pmod_loc }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(sfunct.pmod_loc, env, Cannot_apply funct.mod_type))
|
2006-09-20 04:14:37 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pmod_constraint(sarg, smty) ->
|
2010-08-02 07:37:22 -07:00
|
|
|
let arg = type_module true funct_body anchor env sarg in
|
1995-05-04 03:15:53 -07:00
|
|
|
let mty = transl_modtype env smty in
|
2012-05-31 01:07:31 -07:00
|
|
|
rm {(wrap_constraint env arg mty.mty_type (Tmodtype_explicit mty)) with
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_loc = smod.pmod_loc;
|
|
|
|
mod_attributes = smod.pmod_attributes;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2010-10-21 16:59:33 -07:00
|
|
|
| Pmod_unpack sexp ->
|
2010-08-02 07:37:22 -07:00
|
|
|
if funct_body then
|
2012-01-22 23:59:45 -08:00
|
|
|
raise (Error (smod.pmod_loc, env, Not_allowed_in_functor_body));
|
2010-10-21 16:59:33 -07:00
|
|
|
if !Clflags.principal then Ctype.begin_def ();
|
|
|
|
let exp = Typecore.type_exp env sexp in
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
Ctype.end_def ();
|
|
|
|
Ctype.generalize_structure exp.exp_type
|
|
|
|
end;
|
|
|
|
let mty =
|
|
|
|
match Ctype.expand_head env exp.exp_type with
|
|
|
|
{desc = Tpackage (p, nl, tl)} ->
|
|
|
|
if List.exists (fun t -> Ctype.free_variables t <> []) tl then
|
2012-01-22 23:59:45 -08:00
|
|
|
raise (Error (smod.pmod_loc, env,
|
2010-10-21 16:59:33 -07:00
|
|
|
Incomplete_packed_module exp.exp_type));
|
|
|
|
if !Clflags.principal &&
|
|
|
|
not (Typecore.generalizable (Btype.generic_level-1) exp.exp_type)
|
|
|
|
then
|
|
|
|
Location.prerr_warning smod.pmod_loc
|
|
|
|
(Warnings.Not_principal "this module unpacking");
|
|
|
|
modtype_of_package env smod.pmod_loc p nl tl
|
2011-09-22 02:05:42 -07:00
|
|
|
| {desc = Tvar _} ->
|
2010-10-21 16:59:33 -07:00
|
|
|
raise (Typecore.Error
|
2012-01-21 19:15:14 -08:00
|
|
|
(smod.pmod_loc, env, Typecore.Cannot_infer_signature))
|
2010-10-21 16:59:33 -07:00
|
|
|
| _ ->
|
2012-01-22 23:59:45 -08:00
|
|
|
raise (Error(smod.pmod_loc, env, Not_a_packed_module exp.exp_type))
|
2010-10-21 16:59:33 -07:00
|
|
|
in
|
2009-10-26 03:53:16 -07:00
|
|
|
rm { mod_desc = Tmod_unpack(exp, mty);
|
|
|
|
mod_type = mty;
|
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2009-10-26 03:53:16 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
2013-03-04 04:54:57 -08:00
|
|
|
| Pmod_extension (s, _arg) ->
|
2013-04-19 00:40:57 -07:00
|
|
|
raise (Error (s.loc, env, Extension s.txt))
|
2009-10-26 03:53:16 -07:00
|
|
|
|
2012-06-01 07:05:49 -07:00
|
|
|
and type_structure ?(toplevel = false) funct_body anchor env sstr scope =
|
2000-12-01 01:35:00 -08:00
|
|
|
let type_names = ref StringSet.empty
|
|
|
|
and module_names = ref StringSet.empty
|
|
|
|
and modtype_names = ref StringSet.empty in
|
|
|
|
let rec type_struct env sstr =
|
2012-11-08 05:47:41 -08:00
|
|
|
let previous_saved_types = Cmt_format.get_saved_types () in
|
2000-12-01 01:35:00 -08:00
|
|
|
Ctype.init_def(Ident.current_time());
|
|
|
|
match sstr with
|
|
|
|
[] ->
|
|
|
|
([], [], env)
|
2012-05-30 07:52:37 -07:00
|
|
|
| pstr :: srem ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let loc = pstr.pstr_loc in
|
2012-11-08 05:47:41 -08:00
|
|
|
let mk desc =
|
|
|
|
let str = { str_desc = desc; str_loc = loc; str_env = env } in
|
2013-09-04 08:12:37 -07:00
|
|
|
Cmt_format.set_saved_types (Cmt_format.Partial_structure_item str
|
|
|
|
:: previous_saved_types);
|
2012-11-08 05:47:41 -08:00
|
|
|
str
|
|
|
|
in
|
2012-07-30 11:04:46 -07:00
|
|
|
match pstr.pstr_desc with
|
2013-04-11 07:07:32 -07:00
|
|
|
| Pstr_eval (sexpr, attrs) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let expr = Typecore.type_expression env sexpr in
|
2013-04-11 07:07:32 -07:00
|
|
|
let item = mk (Tstr_eval (expr, attrs)) in
|
2012-07-30 11:04:46 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct env srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem, sig_rem, final_env)
|
2013-06-03 08:14:19 -07:00
|
|
|
| Pstr_value(rec_flag, sdefs) ->
|
2007-05-16 01:21:41 -07:00
|
|
|
let scope =
|
|
|
|
match rec_flag with
|
|
|
|
| Recursive -> Some (Annot.Idef {scope with
|
|
|
|
Location.loc_start = loc.Location.loc_start})
|
|
|
|
| Nonrecursive ->
|
|
|
|
let start = match srem with
|
2008-07-29 08:42:44 -07:00
|
|
|
| [] -> loc.Location.loc_end
|
2007-05-16 01:21:41 -07:00
|
|
|
| {pstr_loc = loc2} :: _ -> loc2.Location.loc_start
|
|
|
|
in Some (Annot.Idef {scope with Location.loc_start = start})
|
|
|
|
in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (defs, newenv) =
|
2007-05-16 01:21:41 -07:00
|
|
|
Typecore.type_binding env rec_flag sdefs scope in
|
2013-06-03 08:14:19 -07:00
|
|
|
let item = mk (Tstr_value(rec_flag, defs)) in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
|
|
|
let bound_idents = let_bound_idents defs in
|
2011-12-21 07:40:54 -08:00
|
|
|
(* Note: Env.find_value does not trigger the value_used event. Values
|
|
|
|
will be marked as being used during the signature inclusion test. *)
|
2000-12-01 01:35:00 -08:00
|
|
|
let make_sig_value id =
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_value(id, Env.find_value (Pident id) newenv) in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
map_end make_sig_value bound_idents sig_rem,
|
|
|
|
final_env)
|
2013-03-06 04:00:18 -08:00
|
|
|
| Pstr_primitive sdesc ->
|
2013-03-25 11:04:40 -07:00
|
|
|
let (desc, newenv) = Typedecl.transl_value_decl env loc sdesc in
|
|
|
|
let item = mk (Tstr_primitive desc) in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2013-03-25 11:04:40 -07:00
|
|
|
(item :: str_rem, Sig_value(desc.val_id, desc.val_val) :: sig_rem, final_env)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pstr_type sdecls ->
|
2000-12-01 01:35:00 -08:00
|
|
|
List.iter
|
2013-03-06 03:47:59 -08:00
|
|
|
(fun decl -> check "type" loc type_names decl.ptype_name.txt)
|
2000-12-01 01:35:00 -08:00
|
|
|
sdecls;
|
|
|
|
let (decls, newenv) = Typedecl.transl_type_decl env sdecls in
|
2012-11-08 05:47:41 -08:00
|
|
|
let item = mk (Tstr_type decls) in
|
2006-09-20 04:14:37 -07:00
|
|
|
let newenv' =
|
2003-06-19 08:53:53 -07:00
|
|
|
enrich_type_decls anchor decls env newenv in
|
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv' srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2013-03-25 11:20:11 -07:00
|
|
|
map_rec'' (fun rs info -> Sig_type(info.typ_id, info.typ_type, rs))
|
2012-05-31 01:07:31 -07:00
|
|
|
decls sig_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
final_env)
|
2013-03-05 04:37:17 -08:00
|
|
|
| Pstr_exception sarg ->
|
2013-03-25 11:42:45 -07:00
|
|
|
let (arg, decl, newenv) = Typedecl.transl_exception env sarg in
|
|
|
|
let item = mk (Tstr_exception arg) in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2013-03-25 11:42:45 -07:00
|
|
|
Sig_exception(arg.cd_id, decl) :: sig_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
final_env)
|
2013-03-25 07:16:07 -07:00
|
|
|
| Pstr_exn_rebind(name, longid, attrs) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, arg) = Typedecl.transl_exn_rebind env loc longid.txt in
|
|
|
|
let (id, newenv) = Env.enter_exception name.txt arg env in
|
2013-03-25 07:16:07 -07:00
|
|
|
let item = mk (Tstr_exn_rebind(id, name, path, longid, attrs)) in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_exception(id, arg) :: sig_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
final_env)
|
2013-03-26 01:09:26 -07:00
|
|
|
| Pstr_module {pmb_name = name; pmb_expr = smodl; pmb_attributes = attrs} ->
|
2012-05-30 07:52:37 -07:00
|
|
|
check "module" loc module_names name.txt;
|
2010-08-02 07:37:22 -07:00
|
|
|
let modl =
|
2012-05-30 07:52:37 -07:00
|
|
|
type_module true funct_body (anchor_submodule name.txt anchor) env
|
2010-08-02 07:37:22 -07:00
|
|
|
smodl in
|
2012-05-30 07:52:37 -07:00
|
|
|
let mty = enrich_module_type anchor name.txt modl.mod_type env in
|
|
|
|
let (id, newenv) = Env.enter_module name.txt mty env in
|
2013-03-26 01:09:26 -07:00
|
|
|
let item = mk
|
|
|
|
(Tstr_module
|
|
|
|
{
|
|
|
|
mb_id=id;
|
|
|
|
mb_name=name;
|
|
|
|
mb_expr=modl;
|
|
|
|
mb_attributes=attrs;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_module(id, modl.mod_type, Trec_not) :: sig_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
final_env)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pstr_recmodule sbind ->
|
2013-03-05 08:50:05 -08:00
|
|
|
let sbind =
|
|
|
|
List.map
|
|
|
|
(function
|
2013-03-25 10:47:28 -07:00
|
|
|
| {pmb_name = name; pmb_expr = {pmod_desc=Pmod_constraint(expr, typ)}; pmb_attributes = attrs} ->
|
|
|
|
name, typ, expr, attrs
|
2013-03-05 08:50:05 -08:00
|
|
|
| mb ->
|
|
|
|
raise (Error (mb.pmb_expr.pmod_loc, env, Recursive_module_require_explicit_type))
|
|
|
|
)
|
|
|
|
sbind
|
|
|
|
in
|
2003-06-19 08:53:53 -07:00
|
|
|
List.iter
|
2013-03-25 10:47:28 -07:00
|
|
|
(fun (name, _, _, _) -> check "module" loc module_names name.txt)
|
2003-06-19 08:53:53 -07:00
|
|
|
sbind;
|
|
|
|
let (decls, newenv) =
|
|
|
|
transl_recmodule_modtypes loc env
|
2013-03-25 10:47:28 -07:00
|
|
|
(List.map (fun (name, smty, smodl, attrs) -> {pmd_name=name; pmd_type=smty; pmd_attributes=attrs}) sbind) in
|
2008-01-11 08:13:18 -08:00
|
|
|
let bindings1 =
|
|
|
|
List.map2
|
2013-03-26 01:09:26 -07:00
|
|
|
(fun {md_id=id; md_type=mty} (name, _, smodl, attrs) ->
|
2008-01-11 08:13:18 -08:00
|
|
|
let modl =
|
2010-08-02 07:37:22 -07:00
|
|
|
type_module true funct_body (anchor_recmodule id anchor) newenv
|
|
|
|
smodl in
|
2008-01-11 08:13:18 -08:00
|
|
|
let mty' =
|
2010-08-02 07:37:22 -07:00
|
|
|
enrich_module_type anchor (Ident.name id) modl.mod_type newenv
|
|
|
|
in
|
2013-03-26 01:09:26 -07:00
|
|
|
(id, name, mty, modl, mty', attrs))
|
2008-01-11 08:13:18 -08:00
|
|
|
decls sbind in
|
|
|
|
let bindings2 =
|
|
|
|
check_recmodule_inclusion newenv bindings1 in
|
2012-11-08 05:47:41 -08:00
|
|
|
let item = mk (Tstr_recmodule bindings2) in
|
2003-06-19 08:53:53 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2013-03-26 01:09:26 -07:00
|
|
|
map_rec (fun rs mb -> Sig_module(mb.mb_id, mb.mb_expr.mod_type, rs))
|
2008-01-11 08:13:18 -08:00
|
|
|
bindings2 sig_rem,
|
2003-06-19 08:53:53 -07:00
|
|
|
final_env)
|
2013-04-18 06:14:53 -07:00
|
|
|
| Pstr_modtype pmtd ->
|
|
|
|
(* check that it is non-abstract *)
|
|
|
|
let newenv, mtd, sg =
|
|
|
|
transl_modtype_decl modtype_names env loc pmtd
|
2013-03-26 01:21:29 -07:00
|
|
|
in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2013-04-18 06:14:53 -07:00
|
|
|
mk (Tstr_modtype mtd) :: str_rem,
|
|
|
|
sg :: sig_rem,
|
|
|
|
final_env
|
2013-07-16 06:34:30 -07:00
|
|
|
| Pstr_open (ovf, lid, attrs) ->
|
2013-05-16 06:34:53 -07:00
|
|
|
let (path, newenv) = type_open ovf ~toplevel env loc lid in
|
2013-07-16 06:34:30 -07:00
|
|
|
let item = mk (Tstr_open (ovf, path, lid, attrs)) in
|
2012-07-30 11:04:46 -07:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct newenv srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem, sig_rem, final_env)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pstr_class cl ->
|
2001-08-13 02:35:49 -07:00
|
|
|
List.iter
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun {pci_name = name} -> check "type" loc type_names name.txt)
|
2001-08-13 02:35:49 -07:00
|
|
|
cl;
|
2000-12-01 01:35:00 -08:00
|
|
|
let (classes, new_env) = Typeclass.class_declarations env cl in
|
2012-11-08 05:47:41 -08:00
|
|
|
let item =
|
|
|
|
mk
|
|
|
|
(Tstr_class
|
|
|
|
(List.map (fun (i, _, d, _,_,_,_,_,_, s, m, c) ->
|
|
|
|
let vf = if d.cty_new = None then Virtual else Concrete in
|
|
|
|
(* (i, s, m, c, vf) *) (c, m, vf)) classes))
|
2012-05-30 07:52:37 -07:00
|
|
|
(* TODO: check with Jacques why this is here
|
|
|
|
Tstr_class_type
|
|
|
|
(List.map (fun (_,_, i, d, _,_,_,_,_,_,c) -> (i, c)) classes) ::
|
2000-12-01 01:35:00 -08:00
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_,_,_,_, i, d, _,_,_,_,_) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
|
|
|
(List.map (fun (_,_,_,_,_,_, i, d, _,_,_) -> (i, d)) classes) ::
|
2012-05-30 07:52:37 -07:00
|
|
|
*)
|
2012-11-08 05:47:41 -08:00
|
|
|
in
|
|
|
|
let (str_rem, sig_rem, final_env) = type_struct new_env srem in
|
|
|
|
(item :: str_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
List.flatten
|
2004-06-12 01:55:49 -07:00
|
|
|
(map_rec
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun rs (i, _, d, i', d', i'', d'', i''', d''', _, _, _) ->
|
2012-11-08 05:47:41 -08:00
|
|
|
[Sig_class(i, d, rs);
|
|
|
|
Sig_class_type(i', d', rs);
|
|
|
|
Sig_type(i'', d'', rs);
|
|
|
|
Sig_type(i''', d''', rs)])
|
2000-12-01 01:35:00 -08:00
|
|
|
classes [sig_rem]),
|
|
|
|
final_env)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pstr_class_type cl ->
|
2001-08-13 02:35:49 -07:00
|
|
|
List.iter
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun {pci_name = name} -> check "type" loc type_names name.txt)
|
2001-08-13 02:35:49 -07:00
|
|
|
cl;
|
2000-12-01 01:35:00 -08:00
|
|
|
let (classes, new_env) = Typeclass.class_type_declarations env cl in
|
2012-11-08 05:47:41 -08:00
|
|
|
let item =
|
|
|
|
mk
|
|
|
|
(Tstr_class_type
|
|
|
|
(List.map (fun (i, i_loc, d, _, _, _, _, c) ->
|
|
|
|
(i, i_loc, c)) classes))
|
2012-05-30 07:52:37 -07:00
|
|
|
(* TODO: check with Jacques why this is here
|
|
|
|
Tstr_type
|
2000-12-01 01:35:00 -08:00
|
|
|
(List.map (fun (_, _, i, d, _, _) -> (i, d)) classes) ::
|
|
|
|
Tstr_type
|
2012-05-30 07:52:37 -07:00
|
|
|
(List.map (fun (_, _, _, _, i, d) -> (i, d)) classes) :: *)
|
2012-11-08 05:47:41 -08:00
|
|
|
in
|
|
|
|
let (str_rem, sig_rem, final_env) = type_struct new_env srem in
|
|
|
|
(item :: str_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
List.flatten
|
2004-06-12 01:55:49 -07:00
|
|
|
(map_rec
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun rs (i, _, d, i', d', i'', d'', _) ->
|
|
|
|
[Sig_class_type(i, d, rs);
|
|
|
|
Sig_type(i', d', rs);
|
|
|
|
Sig_type(i'', d'', rs)])
|
2000-12-01 01:35:00 -08:00
|
|
|
classes [sig_rem]),
|
|
|
|
final_env)
|
2013-03-25 07:16:07 -07:00
|
|
|
| Pstr_include (smodl, attrs) ->
|
2010-08-02 07:37:22 -07:00
|
|
|
let modl = type_module true funct_body None env smodl in
|
2000-12-01 01:35:00 -08:00
|
|
|
(* Rename all identifiers bound by this signature to avoid clashes *)
|
|
|
|
let sg = Subst.signature Subst.identity
|
|
|
|
(extract_sig_open env smodl.pmod_loc modl.mod_type) in
|
|
|
|
List.iter
|
|
|
|
(check_sig_item type_names module_names modtype_names loc) sg;
|
|
|
|
let new_env = Env.add_signature sg env in
|
2013-07-22 10:03:39 -07:00
|
|
|
let item = mk (Tstr_include (modl, sg, attrs)) in
|
2000-12-01 01:35:00 -08:00
|
|
|
let (str_rem, sig_rem, final_env) = type_struct new_env srem in
|
2012-11-08 05:47:41 -08:00
|
|
|
(item :: str_rem,
|
2000-12-01 01:35:00 -08:00
|
|
|
sg @ sig_rem,
|
|
|
|
final_env)
|
2013-03-05 03:46:25 -08:00
|
|
|
| Pstr_extension ((s, _), _) ->
|
2013-04-19 00:40:57 -07:00
|
|
|
raise (Error (s.loc, env, Extension s.txt))
|
2013-03-25 07:16:07 -07:00
|
|
|
| Pstr_attribute x ->
|
|
|
|
let (str_rem, sig_rem, final_env) = type_struct env srem in
|
|
|
|
mk (Tstr_attribute x) :: str_rem, sig_rem, final_env
|
2003-07-23 09:52:41 -07:00
|
|
|
in
|
2012-05-31 01:07:31 -07:00
|
|
|
if !Clflags.annotations then
|
|
|
|
(* moved to genannot *)
|
|
|
|
List.iter (function {pstr_loc = l} -> Stypes.record_phrase l) sstr;
|
2012-05-30 07:52:37 -07:00
|
|
|
let previous_saved_types = Cmt_format.get_saved_types () in
|
|
|
|
let (items, sg, final_env) = type_struct env sstr in
|
|
|
|
let str = { str_items = items; str_type = sg; str_final_env = final_env } in
|
2012-05-31 01:07:31 -07:00
|
|
|
Cmt_format.set_saved_types
|
|
|
|
(Cmt_format.Partial_structure str :: previous_saved_types);
|
2012-05-30 07:52:37 -07:00
|
|
|
str, sg, final_env
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
let type_toplevel_phrase env s =
|
|
|
|
type_structure ~toplevel:true false None env s Location.none
|
2010-08-02 07:37:22 -07:00
|
|
|
let type_module = type_module true false None
|
2009-10-26 03:53:16 -07:00
|
|
|
let type_structure = type_structure false None
|
2003-06-19 08:53:53 -07:00
|
|
|
|
2000-02-24 02:18:25 -08:00
|
|
|
(* Normalize types in a signature *)
|
|
|
|
|
|
|
|
let rec normalize_modtype env = function
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_ident p -> ()
|
|
|
|
| Mty_signature sg -> normalize_signature env sg
|
|
|
|
| Mty_functor(id, param, body) -> normalize_modtype env body
|
2000-02-24 02:18:25 -08:00
|
|
|
|
|
|
|
and normalize_signature env = List.iter (normalize_signature_item env)
|
|
|
|
|
|
|
|
and normalize_signature_item env = function
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_value(id, desc) -> Ctype.normalize_type env desc.val_type
|
|
|
|
| Sig_module(id, mty, _) -> normalize_modtype env mty
|
2000-02-24 02:18:25 -08:00
|
|
|
| _ -> ()
|
|
|
|
|
2005-08-08 02:41:52 -07:00
|
|
|
(* Simplify multiple specifications of a value or an exception in a signature.
|
|
|
|
(Other signature components, e.g. types, modules, etc, are checked for
|
|
|
|
name uniqueness.) If multiple specifications with the same name,
|
|
|
|
keep only the last (rightmost) one. *)
|
|
|
|
|
|
|
|
let rec simplify_modtype mty =
|
|
|
|
match mty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_ident path -> mty
|
|
|
|
| Mty_functor(id, arg, res) -> Mty_functor(id, arg, simplify_modtype res)
|
|
|
|
| Mty_signature sg -> Mty_signature(simplify_signature sg)
|
2005-08-08 02:41:52 -07:00
|
|
|
|
|
|
|
and simplify_signature sg =
|
|
|
|
let rec simplif val_names exn_names res = function
|
|
|
|
[] -> res
|
2012-05-30 07:52:37 -07:00
|
|
|
| (Sig_value(id, descr) as component) :: sg ->
|
2005-08-08 02:41:52 -07:00
|
|
|
let name = Ident.name id in
|
|
|
|
simplif (StringSet.add name val_names) exn_names
|
|
|
|
(if StringSet.mem name val_names then res else component :: res)
|
|
|
|
sg
|
2012-05-30 07:52:37 -07:00
|
|
|
| (Sig_exception(id, decl) as component) :: sg ->
|
2005-08-08 02:41:52 -07:00
|
|
|
let name = Ident.name id in
|
|
|
|
simplif val_names (StringSet.add name exn_names)
|
|
|
|
(if StringSet.mem name exn_names then res else component :: res)
|
|
|
|
sg
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_module(id, mty, rs) :: sg ->
|
2005-08-08 02:41:52 -07:00
|
|
|
simplif val_names exn_names
|
2012-05-30 07:52:37 -07:00
|
|
|
(Sig_module(id, simplify_modtype mty, rs) :: res) sg
|
2005-08-08 02:41:52 -07:00
|
|
|
| component :: sg ->
|
|
|
|
simplif val_names exn_names (component :: res) sg
|
|
|
|
in
|
|
|
|
simplif StringSet.empty StringSet.empty [] (List.rev sg)
|
|
|
|
|
2010-04-27 21:54:57 -07:00
|
|
|
(* Extract the module type of a module expression *)
|
|
|
|
|
|
|
|
let type_module_type_of env smod =
|
2012-05-30 07:52:37 -07:00
|
|
|
let tmty =
|
2010-04-28 00:53:19 -07:00
|
|
|
match smod.pmod_desc with
|
|
|
|
| Pmod_ident lid -> (* turn off strengthening in this case *)
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, mty) = Typetexp.find_module env smod.pmod_loc lid.txt in
|
|
|
|
rm { mod_desc = Tmod_ident (path, lid);
|
|
|
|
mod_type = mty;
|
|
|
|
mod_env = env;
|
2013-03-25 10:47:28 -07:00
|
|
|
mod_attributes = smod.pmod_attributes;
|
2012-05-30 07:52:37 -07:00
|
|
|
mod_loc = smod.pmod_loc }
|
|
|
|
| _ -> type_module env smod in
|
|
|
|
let mty = tmty.mod_type in
|
2010-04-29 22:12:44 -07:00
|
|
|
(* PR#5037: clean up inferred signature to remove duplicate specs *)
|
|
|
|
let mty = simplify_modtype mty in
|
2010-04-28 00:53:19 -07:00
|
|
|
(* PR#5036: must not contain non-generalized type variables *)
|
|
|
|
if not (closed_modtype mty) then
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(smod.pmod_loc, env, Non_generalizable_module mty));
|
2012-05-30 07:52:37 -07:00
|
|
|
tmty, mty
|
2010-04-27 21:54:57 -07:00
|
|
|
|
2010-10-21 16:59:33 -07:00
|
|
|
(* For Typecore *)
|
|
|
|
|
|
|
|
let rec get_manifest_types = function
|
|
|
|
[] -> []
|
2012-05-30 07:52:37 -07:00
|
|
|
| Sig_type (id, {type_params=[]; type_manifest=Some ty}, _) :: rem ->
|
2010-10-21 16:59:33 -07:00
|
|
|
(Ident.name id, ty) :: get_manifest_types rem
|
|
|
|
| _ :: rem -> get_manifest_types rem
|
|
|
|
|
|
|
|
let type_package env m p nl tl =
|
2011-08-19 19:51:34 -07:00
|
|
|
(* Same as Pexp_letmodule *)
|
|
|
|
(* remember original level *)
|
|
|
|
let lv = Ctype.get_current_level () in
|
|
|
|
Ctype.begin_def ();
|
|
|
|
Ident.set_current_time lv;
|
|
|
|
let context = Typetexp.narrow () in
|
2010-10-21 16:59:33 -07:00
|
|
|
let modl = type_module env m in
|
2011-08-19 19:51:34 -07:00
|
|
|
Ctype.init_def(Ident.current_time());
|
|
|
|
Typetexp.widen context;
|
|
|
|
let (mp, env) =
|
|
|
|
match modl.mod_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
Tmod_ident (mp,_) -> (mp, env)
|
2011-08-19 19:51:34 -07:00
|
|
|
| _ ->
|
|
|
|
let (id, new_env) = Env.enter_module "%M" modl.mod_type env in
|
|
|
|
(Pident id, new_env)
|
2010-10-21 16:59:33 -07:00
|
|
|
in
|
2011-12-14 02:26:15 -08:00
|
|
|
let rec mkpath mp = function
|
|
|
|
| Lident name -> Pdot(mp, name, nopos)
|
|
|
|
| Ldot (m, name) -> Pdot(mkpath mp m, name, nopos)
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
2011-08-19 19:51:34 -07:00
|
|
|
let tl' =
|
2011-12-14 02:26:15 -08:00
|
|
|
List.map (fun name -> Ctype.newconstr (mkpath mp name) []) nl in
|
2011-08-19 19:51:34 -07:00
|
|
|
(* go back to original level *)
|
|
|
|
Ctype.end_def ();
|
2012-05-31 01:07:31 -07:00
|
|
|
if nl = [] then
|
|
|
|
(wrap_constraint env modl (Mty_ident p) Tmodtype_implicit, [])
|
|
|
|
else let mty = modtype_of_package env modl.mod_loc p nl tl' in
|
2011-08-19 19:51:34 -07:00
|
|
|
List.iter2
|
|
|
|
(fun n ty ->
|
|
|
|
try Ctype.unify env ty (Ctype.newvar ())
|
2012-01-22 23:59:45 -08:00
|
|
|
with Ctype.Unify _ ->
|
|
|
|
raise (Error(m.pmod_loc, env, Scoping_pack (n,ty))))
|
2011-08-19 19:51:34 -07:00
|
|
|
nl tl';
|
2012-05-30 07:52:37 -07:00
|
|
|
(wrap_constraint env modl mty Tmodtype_implicit, tl')
|
2010-10-21 16:59:33 -07:00
|
|
|
|
2010-04-27 21:54:57 -07:00
|
|
|
(* Fill in the forward declarations *)
|
|
|
|
let () =
|
|
|
|
Typecore.type_module := type_module;
|
|
|
|
Typetexp.transl_modtype_longident := transl_modtype_longident;
|
|
|
|
Typetexp.transl_modtype := transl_modtype;
|
|
|
|
Typecore.type_open := type_open;
|
2010-10-21 16:59:33 -07:00
|
|
|
Typecore.type_package := type_package;
|
2010-04-27 21:54:57 -07:00
|
|
|
type_module_type_of_fwd := type_module_type_of
|
|
|
|
|
2003-03-10 08:57:04 -08:00
|
|
|
(* Typecheck an implementation file *)
|
|
|
|
|
2004-06-13 05:48:01 -07:00
|
|
|
let type_implementation sourcefile outputprefix modulename initial_env ast =
|
2012-05-30 07:52:37 -07:00
|
|
|
Cmt_format.set_saved_types [];
|
|
|
|
try
|
2003-03-10 08:57:04 -08:00
|
|
|
Typecore.reset_delayed_checks ();
|
2012-11-11 02:59:43 -08:00
|
|
|
let (str, sg, finalenv) =
|
|
|
|
type_structure initial_env ast (Location.in_file sourcefile) in
|
2007-10-08 07:19:34 -07:00
|
|
|
let simple_sg = simplify_signature sg in
|
2003-06-23 07:00:49 -07:00
|
|
|
if !Clflags.print_types then begin
|
2012-01-22 23:59:45 -08:00
|
|
|
Printtyp.wrap_printing_env initial_env
|
|
|
|
(fun () -> fprintf std_formatter "%a@." Printtyp.signature simple_sg);
|
2007-10-08 07:19:34 -07:00
|
|
|
(str, Tcoerce_none) (* result is ignored by Compile.implementation *)
|
2003-06-23 07:00:49 -07:00
|
|
|
end else begin
|
2007-10-08 07:19:34 -07:00
|
|
|
let sourceintf =
|
|
|
|
Misc.chop_extension_if_any sourcefile ^ !Config.interface_suffix in
|
|
|
|
if Sys.file_exists sourceintf then begin
|
|
|
|
let intf_file =
|
|
|
|
try
|
|
|
|
find_in_path_uncap !Config.load_path (modulename ^ ".cmi")
|
|
|
|
with Not_found ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise(Error(Location.in_file sourcefile, Env.empty,
|
2012-11-11 02:59:43 -08:00
|
|
|
Interface_not_compiled sourceintf)) in
|
2007-10-08 07:19:34 -07:00
|
|
|
let dclsig = Env.read_signature modulename intf_file in
|
|
|
|
let coercion = Includemod.compunit sourcefile sg intf_file dclsig in
|
2011-12-21 07:40:54 -08:00
|
|
|
Typecore.force_delayed_checks ();
|
|
|
|
(* It is important to run these checks after the inclusion test above,
|
|
|
|
so that value declarations which are not used internally but exported
|
|
|
|
are not reported as being unused. *)
|
2012-05-31 01:07:31 -07:00
|
|
|
Cmt_format.save_cmt (outputprefix ^ ".cmt") modulename
|
|
|
|
(Cmt_format.Implementation str) (Some sourcefile) initial_env None;
|
2007-10-08 07:19:34 -07:00
|
|
|
(str, coercion)
|
|
|
|
end else begin
|
2012-05-30 07:52:37 -07:00
|
|
|
check_nongen_schemes finalenv str.str_items;
|
2008-10-08 06:09:39 -07:00
|
|
|
normalize_signature finalenv simple_sg;
|
2007-10-08 07:19:34 -07:00
|
|
|
let coercion =
|
|
|
|
Includemod.compunit sourcefile sg
|
|
|
|
"(inferred signature)" simple_sg in
|
2011-12-21 07:40:54 -08:00
|
|
|
Typecore.force_delayed_checks ();
|
|
|
|
(* See comment above. Here the target signature contains all
|
|
|
|
the value being exported. We can still capture unused
|
|
|
|
declarations like "let x = true;; let x = 1;;", because in this
|
|
|
|
case, the inferred signature contains only the last declaration. *)
|
2012-05-30 07:52:37 -07:00
|
|
|
if not !Clflags.dont_write_files then begin
|
2012-05-31 01:07:31 -07:00
|
|
|
let sg =
|
|
|
|
Env.save_signature simple_sg modulename (outputprefix ^ ".cmi") in
|
|
|
|
Cmt_format.save_cmt (outputprefix ^ ".cmt") modulename
|
|
|
|
(Cmt_format.Implementation str)
|
2012-05-30 07:52:37 -07:00
|
|
|
(Some sourcefile) initial_env (Some sg);
|
|
|
|
end;
|
2007-10-08 07:19:34 -07:00
|
|
|
(str, coercion)
|
|
|
|
end
|
2012-05-30 07:52:37 -07:00
|
|
|
end
|
|
|
|
with e ->
|
|
|
|
Cmt_format.save_cmt (outputprefix ^ ".cmt") modulename
|
2012-05-31 01:07:31 -07:00
|
|
|
(Cmt_format.Partial_implementation
|
|
|
|
(Array.of_list (Cmt_format.get_saved_types ())))
|
2012-05-30 07:52:37 -07:00
|
|
|
(Some sourcefile) initial_env None;
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
let save_signature modname tsg outputprefix source_file initial_env cmi =
|
|
|
|
Cmt_format.save_cmt (outputprefix ^ ".cmti") modname
|
|
|
|
(Cmt_format.Interface tsg) (Some source_file) initial_env (Some cmi)
|
2003-03-10 08:57:04 -08:00
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
(* "Packaging" of several compilation units into one unit
|
|
|
|
having them as sub-modules. *)
|
|
|
|
|
|
|
|
let rec package_signatures subst = function
|
|
|
|
[] -> []
|
|
|
|
| (name, sg) :: rem ->
|
|
|
|
let sg' = Subst.signature subst sg in
|
|
|
|
let oldid = Ident.create_persistent name
|
|
|
|
and newid = Ident.create name in
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_module(newid, Mty_signature sg', Trec_not) ::
|
2002-02-08 08:55:44 -08:00
|
|
|
package_signatures (Subst.add_module oldid (Pident newid) subst) rem
|
|
|
|
|
|
|
|
let package_units objfiles cmifile modulename =
|
|
|
|
(* Read the signatures of the units *)
|
|
|
|
let units =
|
|
|
|
List.map
|
|
|
|
(fun f ->
|
2007-02-23 05:44:51 -08:00
|
|
|
let pref = chop_extensions f in
|
2002-02-08 08:55:44 -08:00
|
|
|
let modname = String.capitalize(Filename.basename pref) in
|
2004-04-09 06:32:28 -07:00
|
|
|
let sg = Env.read_signature modname (pref ^ ".cmi") in
|
|
|
|
if Filename.check_suffix f ".cmi" &&
|
|
|
|
not(Mtype.no_code_needed_sig Env.initial sg)
|
2012-01-22 23:59:45 -08:00
|
|
|
then raise(Error(Location.none, Env.empty,
|
|
|
|
Implementation_is_required f));
|
2002-02-08 08:55:44 -08:00
|
|
|
(modname, Env.read_signature modname (pref ^ ".cmi")))
|
|
|
|
objfiles in
|
|
|
|
(* Compute signature of packaged unit *)
|
2003-08-20 07:35:14 -07:00
|
|
|
Ident.reinit();
|
2002-02-08 08:55:44 -08:00
|
|
|
let sg = package_signatures Subst.identity units in
|
2003-03-06 07:59:55 -08:00
|
|
|
(* See if explicit interface is provided *)
|
2012-05-30 07:52:37 -07:00
|
|
|
let prefix = chop_extension_if_any cmifile in
|
|
|
|
let mlifile = prefix ^ !Config.interface_suffix in
|
2003-03-06 07:59:55 -08:00
|
|
|
if Sys.file_exists mlifile then begin
|
2005-03-24 09:20:54 -08:00
|
|
|
if not (Sys.file_exists cmifile) then begin
|
2012-01-22 23:59:45 -08:00
|
|
|
raise(Error(Location.in_file mlifile, Env.empty,
|
|
|
|
Interface_not_compiled mlifile))
|
2005-03-24 09:20:54 -08:00
|
|
|
end;
|
2003-03-06 07:59:55 -08:00
|
|
|
let dclsig = Env.read_signature modulename cmifile in
|
2012-05-30 07:52:37 -07:00
|
|
|
Cmt_format.save_cmt (prefix ^ ".cmt") modulename
|
|
|
|
(Cmt_format.Packed (sg, objfiles)) None Env.initial None ;
|
2006-09-20 04:14:37 -07:00
|
|
|
Includemod.compunit "(obtained by packing)" sg mlifile dclsig
|
2003-03-06 07:59:55 -08:00
|
|
|
end else begin
|
|
|
|
(* Determine imports *)
|
|
|
|
let unit_names = List.map fst units in
|
|
|
|
let imports =
|
|
|
|
List.filter
|
|
|
|
(fun (name, crc) -> not (List.mem name unit_names))
|
|
|
|
(Env.imported_units()) in
|
|
|
|
(* Write packaged signature *)
|
2012-05-30 07:52:37 -07:00
|
|
|
if not !Clflags.dont_write_files then begin
|
2012-05-31 01:07:31 -07:00
|
|
|
let sg =
|
|
|
|
Env.save_signature_with_imports sg modulename
|
|
|
|
(prefix ^ ".cmi") imports in
|
2012-05-30 07:52:37 -07:00
|
|
|
Cmt_format.save_cmt (prefix ^ ".cmt") modulename
|
|
|
|
(Cmt_format.Packed (sg, objfiles)) None Env.initial (Some sg)
|
|
|
|
end;
|
2003-03-06 07:59:55 -08:00
|
|
|
Tcoerce_none
|
|
|
|
end
|
2002-02-08 08:55:44 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Printtyp
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_error ppf = function
|
2010-05-18 10:18:24 -07:00
|
|
|
Cannot_apply mty ->
|
2000-03-21 07:16:48 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[This module is not a functor; it has type@ %a@]" modtype mty
|
|
|
|
| Not_included errs ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[<v>Signature mismatch:@ %a@]" Includemod.report_error errs
|
|
|
|
| Cannot_eliminate_dependency mty ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[This functor has type@ %a@ \
|
|
|
|
The parameter cannot be eliminated in the result type.@ \
|
|
|
|
Please bind the argument to a module identifier.@]" modtype mty
|
2000-03-06 14:12:09 -08:00
|
|
|
| Signature_expected -> fprintf ppf "This module type is not a signature"
|
2000-03-21 07:16:48 -08:00
|
|
|
| Structure_expected mty ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[This module is not a structure; it has type@ %a" modtype mty
|
|
|
|
| With_no_component lid ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The signature constrained by `with' has no component named %a@]"
|
|
|
|
longident lid
|
|
|
|
| With_mismatch(lid, explanation) ->
|
|
|
|
fprintf ppf
|
2001-07-23 08:35:49 -07:00
|
|
|
"@[<v>\
|
2000-03-21 07:16:48 -08:00
|
|
|
@[In this `with' constraint, the new definition of %a@ \
|
|
|
|
does not match its original definition@ \
|
|
|
|
in the constrained signature:@]@ \
|
|
|
|
%a@]"
|
|
|
|
longident lid Includemod.report_error explanation
|
|
|
|
| Repeated_name(kind, name) ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[Multiple definition of the %s name %s.@ \
|
|
|
|
Names must be unique in a given structure or signature.@]" kind name
|
|
|
|
| Non_generalizable typ ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The type of this expression,@ %a,@ \
|
|
|
|
contains type variables that cannot be generalized@]" type_scheme typ
|
|
|
|
| Non_generalizable_class (id, desc) ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The type of this class,@ %a,@ \
|
|
|
|
contains type variables that cannot be generalized@]"
|
|
|
|
(class_declaration id) desc
|
|
|
|
| Non_generalizable_module mty ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The type of this module,@ %a,@ \
|
|
|
|
contains type variables that cannot be generalized@]" modtype mty
|
2004-04-09 06:32:28 -07:00
|
|
|
| Implementation_is_required intf_name ->
|
|
|
|
fprintf ppf
|
2012-03-07 09:37:05 -08:00
|
|
|
"@[The interface %a@ declares values, not just types.@ \
|
|
|
|
An implementation must be provided.@]"
|
|
|
|
Location.print_filename intf_name
|
2004-06-13 05:48:01 -07:00
|
|
|
| Interface_not_compiled intf_name ->
|
|
|
|
fprintf ppf
|
2012-03-07 09:37:05 -08:00
|
|
|
"@[Could not find the .cmi file for interface@ %a.@]"
|
|
|
|
Location.print_filename intf_name
|
2009-10-26 03:53:16 -07:00
|
|
|
| Not_allowed_in_functor_body ->
|
|
|
|
fprintf ppf
|
|
|
|
"This kind of expression is not allowed within the body of a functor."
|
2010-04-17 07:45:12 -07:00
|
|
|
| With_need_typeconstr ->
|
|
|
|
fprintf ppf
|
|
|
|
"Only type constructors with identical parameters can be substituted."
|
2010-10-21 16:59:33 -07:00
|
|
|
| Not_a_packed_module ty ->
|
|
|
|
fprintf ppf
|
|
|
|
"This expression is not a packed module. It has type@ %a"
|
|
|
|
type_expr ty
|
|
|
|
| Incomplete_packed_module ty ->
|
|
|
|
fprintf ppf
|
|
|
|
"The type of this packed module contains variables:@ %a"
|
|
|
|
type_expr ty
|
2011-12-14 02:26:15 -08:00
|
|
|
| Scoping_pack (lid, ty) ->
|
2011-08-19 19:51:34 -07:00
|
|
|
fprintf ppf
|
2011-12-14 02:26:15 -08:00
|
|
|
"The type %a in this module cannot be exported.@ " longident lid;
|
2011-08-19 19:51:34 -07:00
|
|
|
fprintf ppf
|
|
|
|
"Its type contains local dependencies:@ %a" type_expr ty
|
2013-02-28 08:51:59 -08:00
|
|
|
| Extension s ->
|
|
|
|
fprintf ppf "Uninterpreted extension '%s'." s
|
2013-03-05 08:50:05 -08:00
|
|
|
| Recursive_module_require_explicit_type ->
|
|
|
|
fprintf ppf "Recursive modules require an explicit module type."
|
2012-01-22 23:59:45 -08:00
|
|
|
|
|
|
|
let report_error env ppf err =
|
|
|
|
Printtyp.wrap_printing_env env (fun () -> report_error ppf err)
|