1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* 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
|
|
|
|
|
|
|
type t =
|
1996-05-16 07:16:34 -07:00
|
|
|
{ types: (Ident.t, Path.t) Tbl.t;
|
|
|
|
modules: (Ident.t, Path.t) Tbl.t;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes: (Ident.t, module_type) Tbl.t }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let identity =
|
1997-03-24 12:12:16 -08:00
|
|
|
{ types = Tbl.empty; modules = Tbl.empty; modtypes = Tbl.empty }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let add_type id p s =
|
1996-05-16 07:16:34 -07:00
|
|
|
{ types = Tbl.add id p s.types;
|
1995-05-04 03:15:53 -07:00
|
|
|
modules = s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = s.modtypes }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let add_module id p s =
|
|
|
|
{ types = s.types;
|
1996-05-16 07:16:34 -07:00
|
|
|
modules = Tbl.add id p s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = s.modtypes }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let add_modtype id ty s =
|
|
|
|
{ types = s.types;
|
|
|
|
modules = s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = Tbl.add id ty s.modtypes }
|
1996-05-16 07:16:34 -07:00
|
|
|
|
|
|
|
let remove_type id s =
|
|
|
|
{ types = Tbl.remove id s.types;
|
|
|
|
modules = s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = s.modtypes }
|
1996-05-16 07:16:34 -07:00
|
|
|
|
|
|
|
let remove_module id s =
|
|
|
|
{ types = s.types;
|
|
|
|
modules = Tbl.remove id s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = s.modtypes }
|
1996-05-16 07:16:34 -07:00
|
|
|
|
|
|
|
let remove_modtype id s =
|
|
|
|
{ types = s.types;
|
|
|
|
modules = s.modules;
|
1997-03-24 12:12:16 -08:00
|
|
|
modtypes = Tbl.remove id s.modtypes }
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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
|
1997-03-24 12:12:16 -08:00
|
|
|
if (ty.desc = Tvar) || (ty.level < lowest_level) then
|
1997-03-09 11:08:14 -08:00
|
|
|
ty
|
1997-03-24 12:12:16 -08:00
|
|
|
else begin
|
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;
|
|
|
|
let ty' = newmarkedgenvar () in (* Stub *)
|
1997-03-09 11:08:14 -08:00
|
|
|
ty.desc <- Tlink ty';
|
|
|
|
ty'.desc <-
|
|
|
|
begin match desc with
|
|
|
|
Tvar | Tlink _ ->
|
|
|
|
fatal_error "Subst.typexp"
|
|
|
|
| Tarrow(t1, t2) ->
|
|
|
|
Tarrow(typexp s t1, typexp s t2)
|
|
|
|
| Ttuple tl ->
|
|
|
|
Ttuple(List.map (typexp s) tl)
|
|
|
|
| Tconstr(p, tl, abbrev) ->
|
|
|
|
Tconstr(type_path s p, List.map (typexp s) tl, ref Mnil)
|
|
|
|
| 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)))
|
1997-05-11 14:48:21 -07:00
|
|
|
| Tfield(label, kind, t1, t2) ->
|
|
|
|
begin match field_kind_repr kind with
|
|
|
|
Fpresent ->
|
|
|
|
Tfield(label, Fpresent, typexp s t1, typexp s t2)
|
|
|
|
| _ ->
|
|
|
|
Tlink(typexp s t2)
|
|
|
|
end
|
1997-03-09 11:08:14 -08:00
|
|
|
| Tnil ->
|
|
|
|
Tnil
|
|
|
|
end;
|
|
|
|
ty'
|
|
|
|
end
|
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 ();
|
|
|
|
unmark_type ty';
|
|
|
|
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 =
|
|
|
|
begin match decl.type_kind with
|
|
|
|
Type_abstract -> Type_abstract
|
|
|
|
| Type_variant cstrs ->
|
|
|
|
Type_variant(
|
|
|
|
List.map (fun (n, args) -> (n, List.map (typexp s) args))
|
|
|
|
cstrs)
|
|
|
|
| Type_record lbls ->
|
|
|
|
Type_record(
|
|
|
|
List.map (fun (n, mut, arg) -> (n, mut, typexp s arg))
|
|
|
|
lbls)
|
|
|
|
end;
|
|
|
|
type_manifest =
|
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> None
|
|
|
|
| Some ty -> Some(typexp s ty)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
in
|
|
|
|
cleanup_types ();
|
|
|
|
List.iter unmark_type decl.type_params;
|
|
|
|
begin match decl.type_kind with
|
|
|
|
Type_abstract -> ()
|
|
|
|
| Type_variant cstrs ->
|
|
|
|
List.iter (fun (c, tl) -> List.iter unmark_type tl) cstrs
|
|
|
|
| Type_record lbls ->
|
|
|
|
List.iter (fun (c, mut, t) -> unmark_type t) lbls
|
|
|
|
end;
|
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> unmark_type ty
|
|
|
|
end;
|
|
|
|
decl
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let class_type s decl =
|
|
|
|
let decl =
|
1997-03-09 11:08:14 -08:00
|
|
|
{ cty_params = List.map (typexp s) decl.cty_params;
|
|
|
|
cty_args = List.map (typexp s) decl.cty_args;
|
1997-05-11 14:48:21 -07:00
|
|
|
cty_vars = Vars.map (function (m, t) -> (m, typexp s t)) decl.cty_vars;
|
|
|
|
cty_meths = Meths.map (typexp s) decl.cty_meths;
|
1997-03-09 11:08:14 -08:00
|
|
|
cty_self = typexp s decl.cty_self;
|
1996-04-22 04:15:41 -07:00
|
|
|
cty_concr = decl.cty_concr;
|
|
|
|
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
|
1997-03-09 11:08:14 -08:00
|
|
|
cleanup_types ();
|
|
|
|
List.iter unmark_type decl.cty_params;
|
|
|
|
List.iter unmark_type decl.cty_args;
|
|
|
|
Vars.iter (fun l (m, t) -> unmark_type t) decl.cty_vars;
|
1997-05-11 14:48:21 -07:00
|
|
|
Meths.iter (fun l t -> unmark_type t) decl.cty_meths;
|
1997-03-09 11:08:14 -08:00
|
|
|
unmark_type decl.cty_self;
|
|
|
|
begin match decl.cty_new with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> unmark_type ty
|
|
|
|
end;
|
|
|
|
decl
|
|
|
|
|
|
|
|
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
|
|
|
|
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) ->
|
1996-05-16 07:16:34 -07:00
|
|
|
Tmty_functor(id, modtype s arg, modtype (remove_module id s) res)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-05-16 07:16:34 -07:00
|
|
|
and signature s = function
|
|
|
|
[] -> []
|
|
|
|
| Tsig_value(id, d) :: sg ->
|
|
|
|
Tsig_value(id, value_description s d) :: signature s sg
|
|
|
|
| Tsig_type(id, d) :: sg ->
|
|
|
|
Tsig_type(id, type_declaration s d) :: signature (remove_type id s) sg
|
|
|
|
| Tsig_exception(id, d) :: sg ->
|
|
|
|
Tsig_exception(id, exception_declaration s d) :: signature s sg
|
|
|
|
| Tsig_module(id, mty) :: sg ->
|
|
|
|
Tsig_module(id, modtype s mty) :: signature (remove_module id s) sg
|
|
|
|
| Tsig_modtype(id, d) :: sg ->
|
|
|
|
Tsig_modtype(id, modtype_declaration s d) ::
|
|
|
|
signature (remove_modtype id s) sg
|
|
|
|
| Tsig_class(id, d) :: sg ->
|
|
|
|
Tsig_class(id, class_type s d) :: signature s sg
|
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)
|