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 *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Substitutions *)
|
|
|
|
|
1995-08-23 04:55:54 -07:00
|
|
|
open Misc
|
1995-05-04 03:15:53 -07:00
|
|
|
open Path
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1997-03-24 12:12:16 -08:00
|
|
|
open Btype
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
type t =
|
1996-05-16 07:16:34 -07:00
|
|
|
{ types: (Ident.t, Path.t) Tbl.t;
|
|
|
|
modules: (Ident.t, Path.t) Tbl.t;
|
2002-04-03 19:49:58 -08:00
|
|
|
modtypes: (Ident.t, module_type) Tbl.t;
|
|
|
|
for_saving: bool }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let identity =
|
2002-04-03 19:49:58 -08:00
|
|
|
{ types = Tbl.empty; modules = Tbl.empty; modtypes = Tbl.empty;
|
|
|
|
for_saving = false }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-04-03 19:49:58 -08:00
|
|
|
let add_type id p s = { s with types = Tbl.add id p s.types }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-04-03 19:49:58 -08:00
|
|
|
let add_module id p s = { s with modules = Tbl.add id p s.modules }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-04-03 19:49:58 -08:00
|
|
|
let add_modtype id ty s = { s with modtypes = Tbl.add id ty s.modtypes }
|
|
|
|
|
|
|
|
let for_saving s = { s with for_saving = true }
|
1996-05-16 07:16:34 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec module_path s = function
|
|
|
|
Pident id as p ->
|
1996-05-16 07:16:34 -07:00
|
|
|
begin try Tbl.find id s.modules with Not_found -> p end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(p, n, pos) ->
|
|
|
|
Pdot(module_path s p, n, pos)
|
1995-08-23 04:55:54 -07:00
|
|
|
| Papply(p1, p2) ->
|
|
|
|
Papply(module_path s p1, module_path s p2)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2009-10-26 03:53:16 -07:00
|
|
|
let rec modtype_path s = function
|
|
|
|
Pident id as p ->
|
|
|
|
begin try
|
|
|
|
match Tbl.find id s.modtypes with
|
|
|
|
| Tmty_ident p -> p
|
|
|
|
| _ -> fatal_error "Subst.modtype_path"
|
|
|
|
with Not_found -> p end
|
|
|
|
| Pdot(p, n, pos) ->
|
|
|
|
Pdot(module_path s p, n, pos)
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
fatal_error "Subst.modtype_path"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let type_path s = function
|
|
|
|
Pident id as p ->
|
1996-05-16 07:16:34 -07:00
|
|
|
begin try Tbl.find id s.types with Not_found -> p end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(p, n, pos) ->
|
|
|
|
Pdot(module_path s p, n, pos)
|
1995-08-23 04:55:54 -07:00
|
|
|
| Papply(p1, p2) ->
|
|
|
|
fatal_error "Subst.type_path"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-04-03 19:49:58 -08:00
|
|
|
(* Special type ids for saved signatures *)
|
|
|
|
|
|
|
|
let new_id = ref (-1)
|
2002-04-04 14:19:02 -08:00
|
|
|
let reset_for_saving () = new_id := -1
|
2002-04-03 19:49:58 -08:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let newpersty desc =
|
|
|
|
decr new_id; { desc = desc; level = generic_level; id = !new_id }
|
2002-04-03 19:49:58 -08:00
|
|
|
|
1997-03-09 11:08:14 -08:00
|
|
|
(* Similar to [Ctype.nondep_type_rec]. *)
|
|
|
|
let rec typexp s ty =
|
1996-04-22 04:15:41 -07:00
|
|
|
let ty = repr ty in
|
1999-11-30 08:07:38 -08:00
|
|
|
match ty.desc with
|
2002-04-18 00:27:47 -07:00
|
|
|
Tvar | Tunivar ->
|
2002-09-10 23:09:26 -07:00
|
|
|
if s.for_saving || ty.id < 0 then
|
|
|
|
let ty' =
|
|
|
|
if s.for_saving then newpersty ty.desc else newty2 ty.level ty.desc
|
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
save_desc ty ty.desc; ty.desc <- Tsubst ty'; ty'
|
2002-04-03 19:49:58 -08:00
|
|
|
else ty
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst ty ->
|
|
|
|
ty
|
2001-11-22 05:22:55 -08:00
|
|
|
(* cannot do it, since it would omit subsitution
|
|
|
|
| Tvariant row when not (static_row row) ->
|
|
|
|
ty
|
|
|
|
*)
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
1997-03-09 11:08:14 -08:00
|
|
|
let desc = ty.desc in
|
1997-03-24 12:12:16 -08:00
|
|
|
save_desc ty desc;
|
2002-04-03 19:49:58 -08:00
|
|
|
(* Make a stub *)
|
2002-04-18 00:27:47 -07:00
|
|
|
let ty' = if s.for_saving then newpersty Tvar else newgenvar () in
|
1999-11-30 08:07:38 -08:00
|
|
|
ty.desc <- Tsubst ty';
|
1997-03-09 11:08:14 -08:00
|
|
|
ty'.desc <-
|
|
|
|
begin match desc with
|
|
|
|
| Tconstr(p, tl, abbrev) ->
|
|
|
|
Tconstr(type_path s p, List.map (typexp s) tl, ref Mnil)
|
2009-10-26 03:53:16 -07:00
|
|
|
| Tpackage(p, n, tl) ->
|
|
|
|
Tpackage(modtype_path s p, n, List.map (typexp s) tl)
|
1997-03-09 11:08:14 -08:00
|
|
|
| Tobject (t1, name) ->
|
|
|
|
Tobject (typexp s t1,
|
|
|
|
ref (match !name with
|
|
|
|
None -> None
|
|
|
|
| Some (p, tl) ->
|
|
|
|
Some (type_path s p, List.map (typexp s) tl)))
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
let more = repr row.row_more in
|
|
|
|
(* We must substitute in a subtle way *)
|
2003-06-25 00:52:27 -07:00
|
|
|
(* Tsubst takes a tuple containing the row var and the variant *)
|
1999-11-30 08:07:38 -08:00
|
|
|
begin match more.desc with
|
2003-06-25 00:52:27 -07:00
|
|
|
Tsubst {desc = Ttuple [_;ty2]} ->
|
1999-11-30 08:07:38 -08:00
|
|
|
(* This variant type has been already copied *)
|
|
|
|
ty.desc <- Tsubst ty2; (* avoid Tlink in the new type *)
|
|
|
|
Tlink ty2
|
|
|
|
| _ ->
|
2003-07-22 03:58:39 -07:00
|
|
|
let dup =
|
2005-03-22 19:08:37 -08:00
|
|
|
s.for_saving || more.level = generic_level || static_row row ||
|
|
|
|
match more.desc with Tconstr _ -> true | _ -> false in
|
2002-04-18 00:27:47 -07:00
|
|
|
(* Various cases for the row variable *)
|
|
|
|
let more' =
|
2005-03-22 19:08:37 -08:00
|
|
|
match more.desc with
|
|
|
|
Tsubst ty -> ty
|
|
|
|
| Tconstr _ -> typexp s more
|
|
|
|
| Tunivar | Tvar ->
|
2002-09-10 23:09:26 -07:00
|
|
|
save_desc more more.desc;
|
2002-04-18 00:27:47 -07:00
|
|
|
if s.for_saving then newpersty more.desc else
|
2003-07-22 03:58:39 -07:00
|
|
|
if dup && more.desc <> Tunivar then newgenvar () else more
|
2005-03-22 19:08:37 -08:00
|
|
|
| _ -> assert false
|
2002-04-18 00:27:47 -07:00
|
|
|
in
|
2001-11-22 02:41:29 -08:00
|
|
|
(* Register new type first for recursion *)
|
2003-06-25 00:52:27 -07:00
|
|
|
more.desc <- Tsubst(newgenty(Ttuple[more';ty']));
|
2001-11-22 02:41:29 -08:00
|
|
|
(* Return a new copy *)
|
2002-04-18 00:27:47 -07:00
|
|
|
let row =
|
2003-07-22 03:58:39 -07:00
|
|
|
copy_row (typexp s) true row (not dup) more' in
|
2001-11-22 05:22:55 -08:00
|
|
|
match row.row_name with
|
|
|
|
Some (p, tl) ->
|
|
|
|
Tvariant {row with row_name = Some (type_path s p, tl)}
|
|
|
|
| None ->
|
|
|
|
Tvariant row
|
1999-11-30 08:07:38 -08:00
|
|
|
end
|
2006-01-04 08:55:50 -08:00
|
|
|
| Tfield(label, kind, t1, t2) when field_kind_repr kind = Fabsent ->
|
|
|
|
Tlink (typexp s t2)
|
2001-11-22 02:41:29 -08:00
|
|
|
| _ -> copy_type_desc (typexp s) desc
|
1997-03-09 11:08:14 -08:00
|
|
|
end;
|
|
|
|
ty'
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-13 13:11:19 -08:00
|
|
|
(*
|
1997-03-18 13:05:49 -08:00
|
|
|
Always make a copy of the type. If this is not done, type levels
|
|
|
|
might not be correct.
|
1997-03-13 13:11:19 -08:00
|
|
|
*)
|
1996-04-22 04:15:41 -07:00
|
|
|
let type_expr s ty =
|
1997-03-09 11:08:14 -08:00
|
|
|
let ty' = typexp s ty in
|
|
|
|
cleanup_types ();
|
|
|
|
ty'
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let type_declaration s decl =
|
1997-03-09 11:08:14 -08:00
|
|
|
let decl =
|
|
|
|
{ type_params = List.map (typexp s) decl.type_params;
|
|
|
|
type_arity = decl.type_arity;
|
|
|
|
type_kind =
|
2003-07-02 02:14:35 -07:00
|
|
|
begin match decl.type_kind with
|
|
|
|
Type_abstract -> Type_abstract
|
2007-10-09 03:29:37 -07:00
|
|
|
| Type_variant cstrs ->
|
1997-03-09 11:08:14 -08:00
|
|
|
Type_variant(
|
|
|
|
List.map (fun (n, args) -> (n, List.map (typexp s) args))
|
2007-10-09 03:29:37 -07:00
|
|
|
cstrs)
|
|
|
|
| Type_record(lbls, rep) ->
|
1997-03-09 11:08:14 -08:00
|
|
|
Type_record(
|
|
|
|
List.map (fun (n, mut, arg) -> (n, mut, typexp s arg))
|
2000-03-21 06:43:25 -08:00
|
|
|
lbls,
|
2007-10-09 03:29:37 -07:00
|
|
|
rep)
|
1997-03-09 11:08:14 -08:00
|
|
|
end;
|
|
|
|
type_manifest =
|
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> None
|
|
|
|
| Some ty -> Some(typexp s ty)
|
2000-09-06 03:21:07 -07:00
|
|
|
end;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private = decl.type_private;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_variance = decl.type_variance;
|
1997-03-09 11:08:14 -08:00
|
|
|
}
|
|
|
|
in
|
|
|
|
cleanup_types ();
|
|
|
|
decl
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let class_signature s sign =
|
|
|
|
{ cty_self = typexp s sign.cty_self;
|
2006-04-04 19:28:13 -07:00
|
|
|
cty_vars =
|
|
|
|
Vars.map (function (m, v, t) -> (m, v, typexp s t)) sign.cty_vars;
|
2004-05-18 06:28:00 -07:00
|
|
|
cty_concr = sign.cty_concr;
|
|
|
|
cty_inher =
|
|
|
|
List.map (fun (p, tl) -> (type_path s p, List.map (typexp s) tl))
|
|
|
|
sign.cty_inher
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let rec class_type s =
|
|
|
|
function
|
|
|
|
Tcty_constr (p, tyl, cty) ->
|
|
|
|
Tcty_constr (type_path s p, List.map (typexp s) tyl, class_type s cty)
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
Tcty_signature (class_signature s sign)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l, ty, cty) ->
|
|
|
|
Tcty_fun (l, typexp s ty, class_type s cty)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let class_declaration s decl =
|
1996-04-22 04:15:41 -07:00
|
|
|
let decl =
|
1997-03-09 11:08:14 -08:00
|
|
|
{ cty_params = List.map (typexp s) decl.cty_params;
|
2004-12-09 04:40:53 -08:00
|
|
|
cty_variance = decl.cty_variance;
|
1998-06-24 12:22:26 -07:00
|
|
|
cty_type = class_type s decl.cty_type;
|
|
|
|
cty_path = type_path s decl.cty_path;
|
1996-04-22 04:15:41 -07:00
|
|
|
cty_new =
|
|
|
|
begin match decl.cty_new with
|
1997-05-19 08:42:21 -07:00
|
|
|
None -> None
|
1997-03-09 11:08:14 -08:00
|
|
|
| Some ty -> Some (typexp s ty)
|
1997-05-19 08:42:21 -07:00
|
|
|
end }
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2002-04-04 14:19:02 -08:00
|
|
|
(* Do not clean up if saving: next is cltype_declaration *)
|
|
|
|
if not s.for_saving then cleanup_types ();
|
1997-03-09 11:08:14 -08:00
|
|
|
decl
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let cltype_declaration s decl =
|
|
|
|
let decl =
|
|
|
|
{ clty_params = List.map (typexp s) decl.clty_params;
|
2004-12-09 04:40:53 -08:00
|
|
|
clty_variance = decl.clty_variance;
|
1998-06-24 12:22:26 -07:00
|
|
|
clty_type = class_type s decl.clty_type;
|
|
|
|
clty_path = type_path s decl.clty_path }
|
|
|
|
in
|
2003-06-25 00:52:27 -07:00
|
|
|
(* Do clean up even if saving: type_declaration may be recursive *)
|
|
|
|
cleanup_types ();
|
1998-06-24 12:22:26 -07:00
|
|
|
decl
|
|
|
|
|
|
|
|
let class_type s cty =
|
|
|
|
let cty = class_type s cty in
|
|
|
|
cleanup_types ();
|
|
|
|
cty
|
|
|
|
|
1997-03-09 11:08:14 -08:00
|
|
|
let value_description s descr =
|
|
|
|
{ val_type = type_expr s descr.val_type;
|
|
|
|
val_kind = descr.val_kind }
|
|
|
|
|
|
|
|
let exception_declaration s tyl =
|
|
|
|
List.map (type_expr s) tyl
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-07-27 05:40:43 -07:00
|
|
|
let rec rename_bound_idents s idents = function
|
|
|
|
[] -> (List.rev idents, s)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_type(id, d, _) :: sg ->
|
2000-07-27 05:40:43 -07:00
|
|
|
let id' = Ident.rename id in
|
|
|
|
rename_bound_idents (add_type id (Pident id') s) (id' :: idents) sg
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_module(id, mty, _) :: sg ->
|
2000-07-27 05:40:43 -07:00
|
|
|
let id' = Ident.rename id in
|
|
|
|
rename_bound_idents (add_module id (Pident id') s) (id' :: idents) sg
|
|
|
|
| Tsig_modtype(id, d) :: sg ->
|
|
|
|
let id' = Ident.rename id in
|
|
|
|
rename_bound_idents (add_modtype id (Tmty_ident(Pident id')) s)
|
|
|
|
(id' :: idents) sg
|
2010-01-22 04:48:24 -08:00
|
|
|
| (Tsig_value(id, _) | Tsig_exception(id, _) |
|
2004-06-12 01:55:49 -07:00
|
|
|
Tsig_class(id, _, _) | Tsig_cltype(id, _, _)) :: sg ->
|
2001-07-05 05:23:52 -07:00
|
|
|
let id' = Ident.rename id in
|
|
|
|
rename_bound_idents s (id' :: idents) sg
|
2000-07-27 05:40:43 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec modtype s = function
|
|
|
|
Tmty_ident p as mty ->
|
|
|
|
begin match p with
|
|
|
|
Pident id ->
|
1996-05-16 07:16:34 -07:00
|
|
|
begin try Tbl.find id s.modtypes with Not_found -> mty end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(p, n, pos) ->
|
|
|
|
Tmty_ident(Pdot(module_path s p, n, pos))
|
1995-08-23 04:55:54 -07:00
|
|
|
| Papply(p1, p2) ->
|
|
|
|
fatal_error "Subst.modtype"
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| Tmty_signature sg ->
|
|
|
|
Tmty_signature(signature s sg)
|
|
|
|
| Tmty_functor(id, arg, res) ->
|
2000-05-15 23:28:21 -07:00
|
|
|
let id' = Ident.rename id in
|
|
|
|
Tmty_functor(id', modtype s arg,
|
|
|
|
modtype (add_module id (Pident id') s) res)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-07-27 05:40:43 -07:00
|
|
|
and signature s sg =
|
|
|
|
(* Components of signature may be mutually recursive (e.g. type declarations
|
|
|
|
or class and type declarations), so first build global renaming
|
|
|
|
substitution... *)
|
|
|
|
let (new_idents, s') = rename_bound_idents s [] sg in
|
|
|
|
(* ... then apply it to each signature component in turn *)
|
2001-07-05 05:23:52 -07:00
|
|
|
List.map2 (signature_component s') sg new_idents
|
2000-07-27 05:40:43 -07:00
|
|
|
|
2001-07-05 05:23:52 -07:00
|
|
|
and signature_component s comp newid =
|
|
|
|
match comp with
|
|
|
|
Tsig_value(id, d) ->
|
|
|
|
Tsig_value(newid, value_description s d)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_type(id, d, rs) ->
|
|
|
|
Tsig_type(newid, type_declaration s d, rs)
|
2001-07-05 05:23:52 -07:00
|
|
|
| Tsig_exception(id, d) ->
|
|
|
|
Tsig_exception(newid, exception_declaration s d)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_module(id, mty, rs) ->
|
|
|
|
Tsig_module(newid, modtype s mty, rs)
|
2001-07-05 05:23:52 -07:00
|
|
|
| Tsig_modtype(id, d) ->
|
|
|
|
Tsig_modtype(newid, modtype_declaration s d)
|
2004-06-12 01:55:49 -07:00
|
|
|
| Tsig_class(id, d, rs) ->
|
|
|
|
Tsig_class(newid, class_declaration s d, rs)
|
|
|
|
| Tsig_cltype(id, d, rs) ->
|
|
|
|
Tsig_cltype(newid, cltype_declaration s d, rs)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and modtype_declaration s = function
|
|
|
|
Tmodtype_abstract -> Tmodtype_abstract
|
|
|
|
| Tmodtype_manifest mty -> Tmodtype_manifest(modtype s mty)
|
2009-05-20 04:52:42 -07:00
|
|
|
|
2010-04-20 07:11:28 -07:00
|
|
|
(* For every binding k |-> d of m1, add k |-> f d to m2
|
|
|
|
and return resulting merged map. *)
|
|
|
|
|
|
|
|
let merge_tbls f m1 m2 =
|
|
|
|
Tbl.fold (fun k d accu -> Tbl.add k (f d) accu) m1 m2
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
(* Composition of substitutions:
|
2009-05-20 04:52:42 -07:00
|
|
|
apply (compose s1 s2) x = apply s2 (apply s1 x) *)
|
|
|
|
|
|
|
|
let compose s1 s2 =
|
2010-04-20 07:11:28 -07:00
|
|
|
{ types = merge_tbls (type_path s2) s1.types s2.types;
|
|
|
|
modules = merge_tbls (module_path s2) s1.modules s2.modules;
|
|
|
|
modtypes = merge_tbls (modtype s2) s1.modtypes s2.modtypes;
|
2009-05-20 04:52:42 -07:00
|
|
|
for_saving = false }
|