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
|
|
|
(* *)
|
1996-05-16 07:16:34 -07:00
|
|
|
(* Xavier Leroy and Jerome Vouillon, projet Cristal, INRIA Rocquencourt*)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
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
|
|
|
(* Printing functions *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
open Misc
|
|
|
|
open Ctype
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-05-04 03:15:53 -07:00
|
|
|
open Longident
|
|
|
|
open Path
|
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1997-03-24 12:11:22 -08:00
|
|
|
open Btype
|
2001-08-04 03:08:19 -07:00
|
|
|
open Outcometree
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Print a long identifier *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec longident ppf = function
|
2012-11-07 02:50:14 -08:00
|
|
|
| Lident s -> pp_print_string ppf s
|
2000-03-06 14:12:09 -08:00
|
|
|
| Ldot(p, s) -> fprintf ppf "%a.%s" longident p s
|
|
|
|
| Lapply(p1, p2) -> fprintf ppf "%a(%a)" longident p1 longident p2
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Print an identifier *)
|
|
|
|
|
2010-04-30 00:11:27 -07:00
|
|
|
let unique_names = ref Ident.empty
|
|
|
|
|
|
|
|
let ident_name id =
|
|
|
|
try Ident.find_same id !unique_names with Not_found -> Ident.name id
|
|
|
|
|
|
|
|
let add_unique id =
|
|
|
|
try ignore (Ident.find_same id !unique_names)
|
|
|
|
with Not_found ->
|
|
|
|
unique_names := Ident.add id (Ident.unique_toplevel_name id) !unique_names
|
|
|
|
|
2012-11-07 02:50:14 -08:00
|
|
|
let ident ppf id = pp_print_string ppf (ident_name id)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Print a path *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let ident_pervasive = Ident.create_persistent "Pervasives"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-04 03:08:19 -07:00
|
|
|
let rec tree_of_path = function
|
|
|
|
| Pident id ->
|
2010-04-30 00:11:27 -07:00
|
|
|
Oide_ident (ident_name id)
|
2001-08-04 03:08:19 -07:00
|
|
|
| Pdot(Pident id, s, pos) when Ident.same id ident_pervasive ->
|
|
|
|
Oide_ident s
|
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
Oide_dot (tree_of_path p, s)
|
|
|
|
| Papply(p1, p2) ->
|
|
|
|
Oide_apply (tree_of_path p1, tree_of_path p2)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec path ppf = function
|
|
|
|
| Pident id ->
|
|
|
|
ident ppf id
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(Pident id, s, pos) when Ident.same id ident_pervasive ->
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_print_string ppf s
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(p, s, pos) ->
|
2012-11-07 02:50:14 -08:00
|
|
|
path ppf p;
|
|
|
|
pp_print_char ppf '.';
|
|
|
|
pp_print_string ppf s
|
1995-08-23 04:55:54 -07:00
|
|
|
| Papply(p1, p2) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "%a(%a)" path p1 path p2
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-06-12 01:55:49 -07:00
|
|
|
(* Print a recursive annotation *)
|
|
|
|
|
|
|
|
let tree_of_rec = function
|
|
|
|
| Trec_not -> Orec_not
|
|
|
|
| Trec_first -> Orec_first
|
|
|
|
| Trec_next -> Orec_next
|
|
|
|
|
2003-11-06 17:07:32 -08:00
|
|
|
(* Print a raw type expression, with sharing *)
|
|
|
|
|
|
|
|
let raw_list pr ppf = function
|
|
|
|
[] -> fprintf ppf "[]"
|
|
|
|
| a :: l ->
|
|
|
|
fprintf ppf "@[<1>[%a%t]@]" pr a
|
|
|
|
(fun ppf -> List.iter (fun x -> fprintf ppf ";@,%a" pr x) l)
|
|
|
|
|
|
|
|
let rec safe_kind_repr v = function
|
|
|
|
Fvar {contents=Some k} ->
|
|
|
|
if List.memq k v then "Fvar loop" else
|
|
|
|
safe_kind_repr (k::v) k
|
|
|
|
| Fvar _ -> "Fvar None"
|
|
|
|
| Fpresent -> "Fpresent"
|
|
|
|
| Fabsent -> "Fabsent"
|
|
|
|
|
|
|
|
let rec safe_commu_repr v = function
|
|
|
|
Cok -> "Cok"
|
|
|
|
| Cunknown -> "Cunknown"
|
|
|
|
| Clink r ->
|
|
|
|
if List.memq r v then "Clink loop" else
|
|
|
|
safe_commu_repr (r::v) !r
|
|
|
|
|
|
|
|
let rec safe_repr v = function
|
|
|
|
{desc = Tlink t} when not (List.memq t v) ->
|
|
|
|
safe_repr (t::v) t
|
|
|
|
| t -> t
|
|
|
|
|
|
|
|
let rec list_of_memo = function
|
|
|
|
Mnil -> []
|
2008-07-18 19:13:09 -07:00
|
|
|
| Mcons (priv, p, t1, t2, rem) -> p :: list_of_memo rem
|
2003-11-06 17:07:32 -08:00
|
|
|
| Mlink rem -> list_of_memo !rem
|
|
|
|
|
2011-09-22 02:05:42 -07:00
|
|
|
let print_name ppf = function
|
|
|
|
None -> fprintf ppf "None"
|
|
|
|
| Some name -> fprintf ppf "\"%s\"" name
|
|
|
|
|
2003-11-06 17:07:32 -08:00
|
|
|
let visited = ref []
|
|
|
|
let rec raw_type ppf ty =
|
|
|
|
let ty = safe_repr [] ty in
|
|
|
|
if List.memq ty !visited then fprintf ppf "{id=%d}" ty.id else begin
|
|
|
|
visited := ty :: !visited;
|
|
|
|
fprintf ppf "@[<1>{id=%d;level=%d;desc=@,%a}@]" ty.id ty.level
|
|
|
|
raw_type_desc ty.desc
|
|
|
|
end
|
|
|
|
and raw_type_list tl = raw_list raw_type tl
|
|
|
|
and raw_type_desc ppf = function
|
2011-09-22 02:05:42 -07:00
|
|
|
Tvar name -> fprintf ppf "Tvar %a" print_name name
|
2003-11-06 17:07:32 -08:00
|
|
|
| Tarrow(l,t1,t2,c) ->
|
|
|
|
fprintf ppf "@[<hov1>Tarrow(%s,@,%a,@,%a,@,%s)@]"
|
|
|
|
l raw_type t1 raw_type t2
|
|
|
|
(safe_commu_repr [] c)
|
|
|
|
| Ttuple tl ->
|
|
|
|
fprintf ppf "@[<1>Ttuple@,%a@]" raw_type_list tl
|
|
|
|
| Tconstr (p, tl, abbrev) ->
|
|
|
|
fprintf ppf "@[<hov1>Tconstr(@,%a,@,%a,@,%a)@]" path p
|
|
|
|
raw_type_list tl
|
2006-01-04 08:55:50 -08:00
|
|
|
(raw_list path) (list_of_memo !abbrev)
|
2003-11-06 17:07:32 -08:00
|
|
|
| Tobject (t, nm) ->
|
|
|
|
fprintf ppf "@[<hov1>Tobject(@,%a,@,@[<1>ref%t@])@]" raw_type t
|
|
|
|
(fun ppf ->
|
|
|
|
match !nm with None -> fprintf ppf " None"
|
|
|
|
| Some(p,tl) ->
|
|
|
|
fprintf ppf "(Some(@,%a,@,%a))" path p raw_type_list tl)
|
|
|
|
| Tfield (f, k, t1, t2) ->
|
2005-08-13 13:59:37 -07:00
|
|
|
fprintf ppf "@[<hov1>Tfield(@,%s,@,%s,@,%a,@;<0 -1>%a)@]" f
|
2003-11-06 17:07:32 -08:00
|
|
|
(safe_kind_repr [] k)
|
|
|
|
raw_type t1 raw_type t2
|
2006-04-16 16:28:22 -07:00
|
|
|
| Tnil -> fprintf ppf "Tnil"
|
2003-11-06 17:07:32 -08:00
|
|
|
| Tlink t -> fprintf ppf "@[<1>Tlink@,%a@]" raw_type t
|
|
|
|
| Tsubst t -> fprintf ppf "@[<1>Tsubst@,%a@]" raw_type t
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tunivar name -> fprintf ppf "Tunivar %a" print_name name
|
2003-11-06 17:07:32 -08:00
|
|
|
| Tpoly (t, tl) ->
|
|
|
|
fprintf ppf "@[<hov1>Tpoly(@,%a,@,%a)@]"
|
|
|
|
raw_type t
|
|
|
|
raw_type_list tl
|
|
|
|
| Tvariant row ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[<hov1>{@[%s@,%a;@]@ @[%s@,%a;@]@ %s%b;@ %s%b;@ @[<1>%s%t@]}@]"
|
|
|
|
"row_fields="
|
|
|
|
(raw_list (fun ppf (l, f) ->
|
|
|
|
fprintf ppf "@[%s,@ %a@]" l raw_field f))
|
|
|
|
row.row_fields
|
|
|
|
"row_more=" raw_type row.row_more
|
|
|
|
"row_closed=" row.row_closed
|
|
|
|
"row_fixed=" row.row_fixed
|
|
|
|
"row_name="
|
|
|
|
(fun ppf ->
|
|
|
|
match row.row_name with None -> fprintf ppf "None"
|
|
|
|
| Some(p,tl) ->
|
|
|
|
fprintf ppf "Some(@,%a,@,%a)" path p raw_type_list tl)
|
2009-10-26 03:53:16 -07:00
|
|
|
| Tpackage (p, _, tl) ->
|
|
|
|
fprintf ppf "@[<hov1>Tpackage(@,%a@,%a)@]" path p
|
|
|
|
raw_type_list tl
|
2003-11-06 17:07:32 -08:00
|
|
|
|
|
|
|
and raw_field ppf = function
|
|
|
|
Rpresent None -> fprintf ppf "Rpresent None"
|
|
|
|
| Rpresent (Some t) -> fprintf ppf "@[<1>Rpresent(Some@,%a)@]" raw_type t
|
|
|
|
| Reither (c,tl,m,e) ->
|
|
|
|
fprintf ppf "@[<hov1>Reither(%b,@,%a,@,%b,@,@[<1>ref%t@])@]" c
|
|
|
|
raw_type_list tl m
|
|
|
|
(fun ppf ->
|
|
|
|
match !e with None -> fprintf ppf " None"
|
|
|
|
| Some f -> fprintf ppf "@,@[<1>(%a)@]" raw_field f)
|
|
|
|
| Rabsent -> fprintf ppf "Rabsent"
|
|
|
|
|
|
|
|
let raw_type_expr ppf t =
|
|
|
|
visited := [];
|
|
|
|
raw_type ppf t;
|
|
|
|
visited := []
|
|
|
|
|
2011-07-28 18:39:09 -07:00
|
|
|
let () = Btype.print_raw := raw_type_expr
|
|
|
|
|
2012-01-21 19:15:14 -08:00
|
|
|
(* Normalize paths *)
|
|
|
|
|
2012-12-10 02:39:07 -08:00
|
|
|
type param_subst = Id | Nth of int | Map of int list
|
|
|
|
|
|
|
|
let compose l1 = function
|
|
|
|
| Id -> Map l1
|
|
|
|
| Map l2 -> Map (List.map (List.nth l1) l2)
|
|
|
|
| Nth n -> Nth (List.nth l1 n)
|
|
|
|
|
|
|
|
let apply_subst s1 tyl =
|
|
|
|
match s1 with
|
|
|
|
Nth n1 -> [List.nth tyl n1]
|
|
|
|
| Map l1 -> List.map (List.nth tyl) l1
|
|
|
|
| Id -> tyl
|
|
|
|
|
2012-01-21 19:15:14 -08:00
|
|
|
let printing_env = ref Env.empty
|
2012-04-10 22:50:08 -07:00
|
|
|
let printing_map = ref (Lazy.lazy_from_val Tbl.empty)
|
2012-01-21 19:15:14 -08:00
|
|
|
|
|
|
|
let same_type t t' = repr t == repr t'
|
|
|
|
|
2012-12-10 02:39:07 -08:00
|
|
|
let rec index l x =
|
|
|
|
match l with
|
|
|
|
[] -> raise Not_found
|
|
|
|
| a :: l -> if x == a then 0 else 1 + index l x
|
|
|
|
|
|
|
|
let rec uniq = function
|
|
|
|
[] -> true
|
|
|
|
| a :: l -> not (List.memq a l) && uniq l
|
|
|
|
|
|
|
|
let rec normalize_type_path ?(cache=false) env p =
|
2012-04-02 20:44:30 -07:00
|
|
|
try
|
2012-04-10 22:50:08 -07:00
|
|
|
let desc = Env.find_type p env in
|
2012-12-10 02:39:07 -08:00
|
|
|
if desc.type_private = Private || desc.type_newtype_level <> None then
|
|
|
|
(p, Id)
|
|
|
|
else match desc.type_manifest with
|
2012-01-21 19:15:14 -08:00
|
|
|
Some ty ->
|
2012-12-10 02:39:07 -08:00
|
|
|
let params = List.map repr desc.type_params in
|
2012-01-21 19:15:14 -08:00
|
|
|
begin match repr ty with
|
|
|
|
{desc = Tconstr (p1, tyl, _)} ->
|
2012-12-10 02:39:07 -08:00
|
|
|
let tyl = List.map repr tyl in
|
2012-12-16 19:18:31 -08:00
|
|
|
if List.length params = List.length tyl
|
2012-12-10 02:39:07 -08:00
|
|
|
&& List.for_all2 (==) params tyl
|
|
|
|
then normalize_type_path ~cache env p1
|
|
|
|
else if cache || List.length params <= List.length tyl
|
|
|
|
|| not (uniq tyl) then (p, Id)
|
|
|
|
else
|
|
|
|
let l1 = List.map (index params) tyl in
|
|
|
|
let (p2, s2) = normalize_type_path ~cache env p1 in
|
|
|
|
(p2, compose l1 s2)
|
|
|
|
| ty ->
|
|
|
|
(p, Nth (index params ty))
|
2012-01-21 19:15:14 -08:00
|
|
|
end
|
2012-12-10 02:39:07 -08:00
|
|
|
| None -> (p, Id)
|
2012-01-21 19:15:14 -08:00
|
|
|
with
|
2012-12-10 02:39:07 -08:00
|
|
|
Not_found -> (p, Id)
|
2012-04-10 22:50:08 -07:00
|
|
|
|
2012-04-18 23:23:14 -07:00
|
|
|
let rec path_size = function
|
2012-04-10 22:50:08 -07:00
|
|
|
Pident id ->
|
2012-04-18 23:23:14 -07:00
|
|
|
(let s = Ident.name id in if s <> "" && s.[0] = '_' then 10 else 1),
|
|
|
|
-Ident.binding_time id
|
|
|
|
| Pdot (p, _, _) ->
|
|
|
|
let (l, b) = path_size p in (1+l, b)
|
|
|
|
| Papply (p1, p2) ->
|
|
|
|
let (l, b) = path_size p1 in
|
|
|
|
(l + fst (path_size p2), b)
|
2012-04-10 22:50:08 -07:00
|
|
|
|
|
|
|
let set_printing_env env =
|
|
|
|
if not !Clflags.real_paths && env != !printing_env then begin
|
|
|
|
(* printf "Reset printing_map@."; *)
|
|
|
|
printing_env := env;
|
|
|
|
printing_map := lazy begin
|
|
|
|
(* printf "Recompute printing_map.@."; *)
|
|
|
|
let map = ref Tbl.empty in
|
|
|
|
Env.iter_types
|
|
|
|
(fun p (p', decl) ->
|
2012-12-10 02:39:07 -08:00
|
|
|
let (p1, s1) = normalize_type_path env p' ~cache:true in
|
|
|
|
if s1 = Id then
|
2012-04-10 22:50:08 -07:00
|
|
|
try
|
|
|
|
let p2 = Tbl.find p1 !map in
|
2012-04-18 23:23:14 -07:00
|
|
|
if path_size p < path_size p2 then raise Not_found
|
2012-04-10 22:50:08 -07:00
|
|
|
with Not_found ->
|
|
|
|
(* printf "%a --> %a@." path p1 path p; *)
|
|
|
|
map := Tbl.add p1 p !map)
|
|
|
|
env;
|
|
|
|
!map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let wrap_printing_env env f =
|
|
|
|
if env == !printing_env then f () else
|
|
|
|
begin
|
|
|
|
set_printing_env env;
|
|
|
|
try_finally f (fun () -> set_printing_env Env.empty)
|
|
|
|
end
|
2012-04-02 20:44:30 -07:00
|
|
|
|
2012-04-10 22:50:08 -07:00
|
|
|
let best_type_path p =
|
2012-04-18 23:23:14 -07:00
|
|
|
if !Clflags.real_paths || !printing_env == Env.empty
|
2012-12-10 02:39:07 -08:00
|
|
|
then (p, Id)
|
|
|
|
else
|
|
|
|
let (p', s) = normalize_type_path !printing_env p in
|
|
|
|
(try Tbl.find p' (Lazy.force !printing_map) with Not_found -> p'),
|
|
|
|
s
|
2012-01-21 19:15:14 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print a type expression *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let names = ref ([] : (type_expr * string) list)
|
|
|
|
let name_counter = ref 0
|
2011-09-22 02:05:42 -07:00
|
|
|
let named_vars = ref ([] : string list)
|
|
|
|
|
|
|
|
let reset_names () = names := []; name_counter := 0; named_vars := []
|
|
|
|
let add_named_var ty =
|
|
|
|
match ty.desc with
|
|
|
|
Tvar (Some name) | Tunivar (Some name) ->
|
|
|
|
if List.mem name !named_vars then () else
|
|
|
|
named_vars := name :: !named_vars
|
|
|
|
| _ -> ()
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2011-09-22 02:05:42 -07:00
|
|
|
let rec new_name () =
|
1996-04-22 04:15:41 -07:00
|
|
|
let name =
|
|
|
|
if !name_counter < 26
|
2006-04-16 16:28:22 -07:00
|
|
|
then String.make 1 (Char.chr(97 + !name_counter))
|
1996-04-22 04:15:41 -07:00
|
|
|
else String.make 1 (Char.chr(97 + !name_counter mod 26)) ^
|
2000-03-06 14:12:09 -08:00
|
|
|
string_of_int(!name_counter / 26) in
|
|
|
|
incr name_counter;
|
2011-09-22 02:05:42 -07:00
|
|
|
if List.mem name !named_vars
|
|
|
|
|| List.exists (fun (_, name') -> name = name') !names
|
|
|
|
then new_name ()
|
|
|
|
else name
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let name_of_type t =
|
2011-09-22 02:05:42 -07:00
|
|
|
(* We've already been through repr at this stage, so t is our representative
|
|
|
|
of the union-find class. *)
|
1996-04-22 04:15:41 -07:00
|
|
|
try List.assq t !names with Not_found ->
|
2011-09-22 02:05:42 -07:00
|
|
|
let name =
|
|
|
|
match t.desc with
|
|
|
|
Tvar (Some name) | Tunivar (Some name) ->
|
|
|
|
(* Some part of the type we've already printed has assigned another
|
|
|
|
* unification variable to that name. We want to keep the name, so try
|
|
|
|
* adding a number until we find a name that's not taken. *)
|
|
|
|
let current_name = ref name in
|
|
|
|
let i = ref 0 in
|
|
|
|
while List.exists (fun (_, name') -> !current_name = name') !names do
|
|
|
|
current_name := name ^ (string_of_int !i);
|
|
|
|
i := !i + 1;
|
|
|
|
done;
|
|
|
|
!current_name
|
|
|
|
| _ ->
|
|
|
|
(* No name available, create a new one *)
|
|
|
|
new_name ()
|
|
|
|
in
|
2011-12-27 18:22:38 -08:00
|
|
|
(* Exception for type declarations *)
|
|
|
|
if name <> "_" then names := (t, name) :: !names;
|
1996-04-22 04:15:41 -07:00
|
|
|
name
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let check_name_of_type t = ignore(name_of_type t)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2011-09-22 02:05:42 -07:00
|
|
|
let remove_names tyl =
|
|
|
|
let tyl = List.map repr tyl in
|
|
|
|
names := List.filter (fun (ty,_) -> not (List.memq ty tyl)) !names
|
|
|
|
|
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let non_gen_mark sch ty =
|
2011-09-22 02:05:42 -07:00
|
|
|
if sch && is_Tvar ty && ty.level <> generic_level then "_" else ""
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let print_name_of_type sch ppf t =
|
|
|
|
fprintf ppf "'%s%s" (non_gen_mark sch t) (name_of_type t)
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let visited_objects = ref ([] : type_expr list)
|
|
|
|
let aliased = ref ([] : type_expr list)
|
|
|
|
let delayed = ref ([] : type_expr list)
|
|
|
|
|
2003-06-25 00:52:27 -07:00
|
|
|
let add_delayed t =
|
2005-03-10 00:19:22 -08:00
|
|
|
if not (List.memq t !delayed) then delayed := t :: !delayed
|
2003-06-25 00:52:27 -07:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let is_aliased ty = List.memq (proxy ty) !aliased
|
|
|
|
let add_alias ty =
|
|
|
|
let px = proxy ty in
|
2011-09-22 02:05:42 -07:00
|
|
|
if not (is_aliased px) then begin
|
|
|
|
aliased := px :: !aliased;
|
|
|
|
add_named_var px
|
|
|
|
end
|
|
|
|
|
2005-08-13 13:59:37 -07:00
|
|
|
let aliasable ty =
|
2012-12-10 02:39:07 -08:00
|
|
|
match ty.desc with
|
|
|
|
Tvar _ | Tunivar _ | Tpoly _ -> false
|
|
|
|
| Tconstr (p, _, _) ->
|
|
|
|
(match best_type_path p with (_, Nth _) -> false | _ -> true)
|
|
|
|
| _ -> true
|
2002-04-18 00:27:47 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let namable_row row =
|
2001-09-25 02:54:18 -07:00
|
|
|
row.row_name <> None &&
|
1999-11-30 08:07:38 -08:00
|
|
|
List.for_all
|
2000-03-06 14:12:09 -08:00
|
|
|
(fun (_, f) ->
|
|
|
|
match row_field_repr f with
|
2001-09-25 02:54:18 -07:00
|
|
|
| Reither(c, l, _, _) ->
|
|
|
|
row.row_closed && if c then l = [] else List.length l = 1
|
2000-03-06 14:12:09 -08:00
|
|
|
| _ -> true)
|
1999-11-30 08:07:38 -08:00
|
|
|
row.row_fields
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let rec mark_loops_rec visited ty =
|
|
|
|
let ty = repr ty in
|
1999-11-30 08:07:38 -08:00
|
|
|
let px = proxy ty in
|
2005-08-13 13:59:37 -07:00
|
|
|
if List.memq px visited && aliasable ty then add_alias px else
|
2000-02-24 19:33:54 -08:00
|
|
|
let visited = px :: visited in
|
1997-02-20 12:39:02 -08:00
|
|
|
match ty.desc with
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _ -> add_named_var ty
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow(_, ty1, ty2, _) ->
|
1997-02-20 12:39:02 -08:00
|
|
|
mark_loops_rec visited ty1; mark_loops_rec visited ty2
|
2000-03-06 14:12:09 -08:00
|
|
|
| Ttuple tyl -> List.iter (mark_loops_rec visited) tyl
|
2012-12-10 02:39:07 -08:00
|
|
|
| Tconstr(p, tyl, _) ->
|
|
|
|
let (p', s) = best_type_path p in
|
|
|
|
List.iter (mark_loops_rec visited) (apply_subst s tyl)
|
|
|
|
| Tpackage (_, _, tyl) ->
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter (mark_loops_rec visited) tyl
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
if List.memq px !visited_objects then add_alias px else
|
|
|
|
begin
|
2003-06-25 00:52:27 -07:00
|
|
|
let row = row_repr row in
|
1999-11-30 08:07:38 -08:00
|
|
|
if not (static_row row) then
|
|
|
|
visited_objects := px :: !visited_objects;
|
|
|
|
match row.row_name with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Some(p, tyl) when namable_row row ->
|
1999-11-30 08:07:38 -08:00
|
|
|
List.iter (mark_loops_rec visited) tyl
|
|
|
|
| _ ->
|
2008-01-11 08:13:18 -08:00
|
|
|
iter_row (mark_loops_rec visited) row
|
2000-03-06 14:12:09 -08:00
|
|
|
end
|
|
|
|
| Tobject (fi, nm) ->
|
|
|
|
if List.memq px !visited_objects then add_alias px else
|
|
|
|
begin
|
1997-02-20 12:39:02 -08:00
|
|
|
if opened_object ty then
|
1999-11-30 08:07:38 -08:00
|
|
|
visited_objects := px :: !visited_objects;
|
1997-02-20 12:39:02 -08:00
|
|
|
begin match !nm with
|
2000-03-06 14:12:09 -08:00
|
|
|
| None ->
|
2002-04-18 00:27:47 -07:00
|
|
|
let fields, _ = flatten_fields fi in
|
|
|
|
List.iter
|
|
|
|
(fun (_, kind, ty) ->
|
|
|
|
if field_kind_repr kind = Fpresent then
|
|
|
|
mark_loops_rec visited ty)
|
|
|
|
fields
|
1997-02-20 12:39:02 -08:00
|
|
|
| Some (_, l) ->
|
2002-04-18 00:27:47 -07:00
|
|
|
List.iter (mark_loops_rec visited) (List.tl l)
|
1997-02-20 12:39:02 -08:00
|
|
|
end
|
|
|
|
end
|
1997-05-11 14:48:21 -07:00
|
|
|
| Tfield(_, kind, ty1, ty2) when field_kind_repr kind = Fpresent ->
|
1997-02-20 12:39:02 -08:00
|
|
|
mark_loops_rec visited ty1; mark_loops_rec visited ty2
|
1997-11-24 04:05:55 -08:00
|
|
|
| Tfield(_, _, _, ty2) ->
|
|
|
|
mark_loops_rec visited ty2
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tnil -> ()
|
|
|
|
| Tsubst ty -> mark_loops_rec visited ty
|
|
|
|
| Tlink _ -> fatal_error "Printtyp.mark_loops_rec (2)"
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tpoly (ty, tyl) ->
|
2002-07-23 07:12:03 -07:00
|
|
|
List.iter (fun t -> add_alias t) tyl;
|
|
|
|
mark_loops_rec visited ty
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tunivar _ -> add_named_var ty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-02-24 02:18:25 -08:00
|
|
|
let mark_loops ty =
|
|
|
|
normalize_type Env.empty ty;
|
2000-03-06 14:12:09 -08:00
|
|
|
mark_loops_rec [] ty;;
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
let reset_loop_marks () =
|
2002-04-18 00:27:47 -07:00
|
|
|
visited_objects := []; aliased := []; delayed := []
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
let reset () =
|
2010-04-30 00:11:27 -07:00
|
|
|
unique_names := Ident.empty; reset_names (); reset_loop_marks ()
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let reset_and_mark_loops ty =
|
2002-04-18 00:27:47 -07:00
|
|
|
reset (); mark_loops ty
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
let reset_and_mark_loops_list tyl =
|
2002-04-18 00:27:47 -07:00
|
|
|
reset (); List.iter mark_loops tyl
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
(* Disabled in classic mode when printing an unification error *)
|
1999-11-30 08:07:38 -08:00
|
|
|
let print_labels = ref true
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_label ppf l =
|
|
|
|
if !print_labels && l <> "" || is_optional l then fprintf ppf "%s:" l
|
1999-11-30 08:07:38 -08:00
|
|
|
|
2001-08-04 03:08:19 -07:00
|
|
|
let rec tree_of_typexp sch ty =
|
1996-04-22 04:15:41 -07:00
|
|
|
let ty = repr ty in
|
1999-11-30 08:07:38 -08:00
|
|
|
let px = proxy ty in
|
2002-04-18 00:27:47 -07:00
|
|
|
if List.mem_assq px !names && not (List.memq px !delayed) then
|
2002-11-07 00:45:57 -08:00
|
|
|
let mark = is_non_gen sch ty in
|
2001-08-04 03:08:19 -07:00
|
|
|
Otyp_var (mark, name_of_type px) else
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2001-08-04 03:08:19 -07:00
|
|
|
let pr_typ () =
|
2002-04-18 00:27:47 -07:00
|
|
|
match ty.desc with
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _ ->
|
2001-08-04 03:08:19 -07:00
|
|
|
Otyp_var (is_non_gen sch ty, name_of_type ty)
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow(l, ty1, ty2, _) ->
|
2001-08-04 03:08:19 -07:00
|
|
|
let pr_arrow l ty1 ty2 =
|
|
|
|
let lab =
|
|
|
|
if !print_labels && l <> "" || is_optional l then l else ""
|
|
|
|
in
|
|
|
|
let t1 =
|
|
|
|
if is_optional l then
|
|
|
|
match (repr ty1).desc with
|
|
|
|
| Tconstr(path, [ty], _)
|
|
|
|
when Path.same path Predef.path_option ->
|
|
|
|
tree_of_typexp sch ty
|
|
|
|
| _ -> Otyp_stuff "<hidden>"
|
|
|
|
else tree_of_typexp sch ty1 in
|
|
|
|
Otyp_arrow (lab, t1, tree_of_typexp sch ty2) in
|
|
|
|
pr_arrow l ty1 ty2
|
1997-02-20 12:39:02 -08:00
|
|
|
| Ttuple tyl ->
|
2001-08-04 03:08:19 -07:00
|
|
|
Otyp_tuple (tree_of_typlist sch tyl)
|
1997-02-20 12:39:02 -08:00
|
|
|
| Tconstr(p, tyl, abbrev) ->
|
2012-12-10 02:39:07 -08:00
|
|
|
begin match best_type_path p with
|
|
|
|
(_, Nth n) -> tree_of_typexp sch (List.nth tyl n)
|
|
|
|
| (p', s) ->
|
|
|
|
let tyl' = apply_subst s tyl in
|
|
|
|
Otyp_constr (tree_of_path p', tree_of_typlist sch tyl')
|
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
let fields =
|
|
|
|
if row.row_closed then
|
2000-03-06 14:12:09 -08:00
|
|
|
List.filter (fun (_, f) -> row_field_repr f <> Rabsent)
|
1999-11-30 08:07:38 -08:00
|
|
|
row.row_fields
|
2000-03-06 14:12:09 -08:00
|
|
|
else row.row_fields in
|
1999-11-30 08:07:38 -08:00
|
|
|
let present =
|
|
|
|
List.filter
|
2000-03-06 14:12:09 -08:00
|
|
|
(fun (_, f) ->
|
|
|
|
match row_field_repr f with
|
|
|
|
| Rpresent _ -> true
|
|
|
|
| _ -> false)
|
1999-11-30 08:07:38 -08:00
|
|
|
fields in
|
|
|
|
let all_present = List.length present = List.length fields in
|
|
|
|
begin match row.row_name with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Some(p, tyl) when namable_row row ->
|
2012-12-10 02:39:07 -08:00
|
|
|
let (p', s) = best_type_path p in
|
|
|
|
assert (s = Id);
|
2012-04-10 22:50:08 -07:00
|
|
|
let id = tree_of_path p' in
|
2001-08-04 03:08:19 -07:00
|
|
|
let args = tree_of_typlist sch tyl in
|
2001-09-25 02:54:18 -07:00
|
|
|
if row.row_closed && all_present then
|
2001-08-04 03:08:19 -07:00
|
|
|
Otyp_constr (id, args)
|
|
|
|
else
|
|
|
|
let non_gen = is_non_gen sch px in
|
2001-09-25 02:54:18 -07:00
|
|
|
let tags =
|
|
|
|
if all_present then None else Some (List.map fst present) in
|
2012-01-21 19:15:14 -08:00
|
|
|
Otyp_variant (non_gen, Ovar_name(id, args),
|
2001-09-25 02:54:18 -07:00
|
|
|
row.row_closed, tags)
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
2001-08-04 03:08:19 -07:00
|
|
|
let non_gen =
|
2001-09-25 02:54:18 -07:00
|
|
|
not (row.row_closed && all_present) && is_non_gen sch px in
|
|
|
|
let fields = List.map (tree_of_row_field sch) fields in
|
2001-08-04 03:08:19 -07:00
|
|
|
let tags =
|
2001-09-25 02:54:18 -07:00
|
|
|
if all_present then None else Some (List.map fst present) in
|
|
|
|
Otyp_variant (non_gen, Ovar_fields fields, row.row_closed, tags)
|
1999-11-30 08:07:38 -08:00
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
| Tobject (fi, nm) ->
|
2011-12-14 18:25:01 -08:00
|
|
|
tree_of_typobject sch fi !nm
|
|
|
|
| Tnil | Tfield _ ->
|
|
|
|
tree_of_typobject sch ty None
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst ty ->
|
2001-08-04 03:08:19 -07:00
|
|
|
tree_of_typexp sch ty
|
2011-12-14 18:25:01 -08:00
|
|
|
| Tlink _ ->
|
2001-08-04 03:08:19 -07:00
|
|
|
fatal_error "Printtyp.tree_of_typexp"
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tpoly (ty, []) ->
|
2002-07-23 07:12:03 -07:00
|
|
|
tree_of_typexp sch ty
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tpoly (ty, tyl) ->
|
2011-09-22 02:05:42 -07:00
|
|
|
(*let print_names () =
|
|
|
|
List.iter (fun (_, name) -> prerr_string (name ^ " ")) !names;
|
|
|
|
prerr_string "; " in *)
|
2002-04-18 00:27:47 -07:00
|
|
|
let tyl = List.map repr tyl in
|
2003-06-25 00:52:27 -07:00
|
|
|
if tyl = [] then tree_of_typexp sch ty else begin
|
2005-03-10 00:19:22 -08:00
|
|
|
let old_delayed = !delayed in
|
2011-09-22 02:05:42 -07:00
|
|
|
(* Make the names delayed, so that the real type is
|
|
|
|
printed once when used as proxy *)
|
2003-06-25 00:52:27 -07:00
|
|
|
List.iter add_delayed tyl;
|
|
|
|
let tl = List.map name_of_type tyl in
|
2005-03-10 00:19:22 -08:00
|
|
|
let tr = Otyp_poly (tl, tree_of_typexp sch ty) in
|
2011-09-22 02:05:42 -07:00
|
|
|
(* Forget names when we leave scope *)
|
|
|
|
remove_names tyl;
|
2005-03-10 00:19:22 -08:00
|
|
|
delayed := old_delayed; tr
|
2003-06-25 00:52:27 -07:00
|
|
|
end
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tunivar _ ->
|
2002-04-18 00:27:47 -07:00
|
|
|
Otyp_var (false, name_of_type ty)
|
2009-10-26 03:53:16 -07:00
|
|
|
| Tpackage (p, n, tyl) ->
|
2012-05-31 01:07:31 -07:00
|
|
|
let n =
|
|
|
|
List.map (fun li -> String.concat "." (Longident.flatten li)) n in
|
2009-10-26 03:53:16 -07:00
|
|
|
Otyp_module (Path.name p, n, tree_of_typlist sch tyl)
|
2002-04-18 00:27:47 -07:00
|
|
|
in
|
|
|
|
if List.memq px !delayed then delayed := List.filter ((!=) px) !delayed;
|
2005-08-13 13:59:37 -07:00
|
|
|
if is_aliased px && aliasable ty then begin
|
2000-03-06 14:12:09 -08:00
|
|
|
check_name_of_type px;
|
2001-08-04 03:08:19 -07:00
|
|
|
Otyp_alias (pr_typ (), name_of_type px) end
|
|
|
|
else pr_typ ()
|
|
|
|
|
|
|
|
and tree_of_row_field sch (l, f) =
|
|
|
|
match row_field_repr f with
|
|
|
|
| Rpresent None | Reither(true, [], _, _) -> (l, false, [])
|
|
|
|
| Rpresent(Some ty) -> (l, false, [tree_of_typexp sch ty])
|
|
|
|
| Reither(c, tyl, _, _) ->
|
|
|
|
if c (* contradiction: un constructeur constant qui a un argument *)
|
|
|
|
then (l, true, tree_of_typlist sch tyl)
|
|
|
|
else (l, false, tree_of_typlist sch tyl)
|
|
|
|
| Rabsent -> (l, false, [] (* une erreur, en fait *))
|
|
|
|
|
2004-12-09 04:40:53 -08:00
|
|
|
and tree_of_typlist sch tyl =
|
|
|
|
List.map (tree_of_typexp sch) tyl
|
2001-08-04 03:08:19 -07:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
and tree_of_typobject sch fi nm =
|
2011-12-14 18:25:01 -08:00
|
|
|
begin match nm with
|
2000-03-06 14:12:09 -08:00
|
|
|
| None ->
|
2001-08-04 03:08:19 -07:00
|
|
|
let pr_fields fi =
|
2000-03-06 14:12:09 -08:00
|
|
|
let (fields, rest) = flatten_fields fi in
|
|
|
|
let present_fields =
|
|
|
|
List.fold_right
|
|
|
|
(fun (n, k, t) l ->
|
|
|
|
match field_kind_repr k with
|
|
|
|
| Fpresent -> (n, t) :: l
|
|
|
|
| _ -> l)
|
|
|
|
fields [] in
|
|
|
|
let sorted_fields =
|
2011-06-15 02:26:30 -07:00
|
|
|
List.sort (fun (n, _) (n', _) -> compare n n') present_fields in
|
2001-08-04 03:08:19 -07:00
|
|
|
tree_of_typfields sch rest sorted_fields in
|
|
|
|
let (fields, rest) = pr_fields fi in
|
|
|
|
Otyp_object (fields, rest)
|
2002-04-18 00:27:47 -07:00
|
|
|
| Some (p, ty :: tyl) ->
|
|
|
|
let non_gen = is_non_gen sch (repr ty) in
|
2001-08-04 03:08:19 -07:00
|
|
|
let args = tree_of_typlist sch tyl in
|
2012-12-10 02:39:07 -08:00
|
|
|
let (p', s) = best_type_path p in
|
|
|
|
assert (s = Id);
|
2012-04-10 22:50:08 -07:00
|
|
|
Otyp_class (non_gen, tree_of_path p', args)
|
1997-02-20 12:39:02 -08:00
|
|
|
| _ ->
|
2001-08-04 03:08:19 -07:00
|
|
|
fatal_error "Printtyp.tree_of_typobject"
|
1997-02-20 12:39:02 -08:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2001-08-04 03:08:19 -07:00
|
|
|
and is_non_gen sch ty =
|
2011-09-22 02:05:42 -07:00
|
|
|
sch && is_Tvar ty && ty.level <> generic_level
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2001-08-04 03:08:19 -07:00
|
|
|
and tree_of_typfields sch rest = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| [] ->
|
2001-08-04 03:08:19 -07:00
|
|
|
let rest =
|
|
|
|
match rest.desc with
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _ | Tunivar _ -> Some (is_non_gen sch rest)
|
2005-03-22 19:08:37 -08:00
|
|
|
| Tconstr _ -> Some false
|
2001-08-04 03:08:19 -07:00
|
|
|
| Tnil -> None
|
|
|
|
| _ -> fatal_error "typfields (1)"
|
|
|
|
in
|
|
|
|
([], rest)
|
|
|
|
| (s, t) :: l ->
|
|
|
|
let field = (s, tree_of_typexp sch t) in
|
|
|
|
let (fields, rest) = tree_of_typfields sch rest l in
|
|
|
|
(field :: fields, rest)
|
|
|
|
|
|
|
|
let typexp sch prio ppf ty =
|
2002-02-13 03:09:19 -08:00
|
|
|
!Oprint.out_type ppf (tree_of_typexp sch ty)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let type_expr ppf ty = typexp false 0 ppf ty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
and type_sch ppf ty = typexp true 0 ppf ty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
and type_scheme ppf ty = reset_and_mark_loops ty; typexp true 0 ppf ty
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-01 07:09:52 -07:00
|
|
|
(* Maxence *)
|
2006-04-16 16:28:22 -07:00
|
|
|
let type_scheme_max ?(b_reset_names=true) ppf ty =
|
2001-10-01 07:09:52 -07:00
|
|
|
if b_reset_names then reset_names () ;
|
|
|
|
typexp true 0 ppf ty
|
|
|
|
(* Fin Maxence *)
|
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let tree_of_type_scheme ty = reset_and_mark_loops ty; tree_of_typexp true ty
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print one type declaration *)
|
|
|
|
|
2001-08-07 01:03:04 -07:00
|
|
|
let tree_of_constraints params =
|
|
|
|
List.fold_right
|
|
|
|
(fun ty list ->
|
|
|
|
let ty' = unalias ty in
|
2002-05-29 23:24:45 -07:00
|
|
|
if proxy ty != proxy ty' then
|
2001-09-28 19:40:59 -07:00
|
|
|
let tr = tree_of_typexp true ty in
|
|
|
|
(tr, tree_of_typexp true ty') :: list
|
2001-08-07 01:03:04 -07:00
|
|
|
else list)
|
|
|
|
params []
|
|
|
|
|
2000-05-24 20:09:28 -07:00
|
|
|
let filter_params tyl =
|
|
|
|
let params =
|
|
|
|
List.fold_left
|
|
|
|
(fun tyl ty ->
|
|
|
|
let ty = repr ty in
|
|
|
|
if List.memq ty tyl then Btype.newgenty (Tsubst ty) :: tyl
|
|
|
|
else ty :: tyl)
|
|
|
|
[] tyl
|
|
|
|
in List.rev params
|
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let string_of_mutable = function
|
|
|
|
| Immutable -> ""
|
|
|
|
| Mutable -> "mutable "
|
|
|
|
|
|
|
|
let rec tree_of_type_decl id decl =
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
reset();
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2000-05-24 20:09:28 -07:00
|
|
|
let params = filter_params decl.type_params in
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
List.iter add_alias params;
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter mark_loops params;
|
2000-06-27 22:47:42 -07:00
|
|
|
List.iter check_name_of_type (List.map proxy params);
|
2003-06-30 01:04:42 -07:00
|
|
|
let ty_manifest =
|
|
|
|
match decl.type_manifest with
|
|
|
|
| None -> None
|
|
|
|
| Some ty ->
|
|
|
|
let ty =
|
|
|
|
(* Special hack to hide variant name *)
|
|
|
|
match repr ty with {desc=Tvariant row} ->
|
|
|
|
let row = row_repr row in
|
|
|
|
begin match row.row_name with
|
|
|
|
Some (Pident id', _) when Ident.same id id' ->
|
|
|
|
newgenty (Tvariant {row with row_name = None})
|
|
|
|
| _ -> ty
|
|
|
|
end
|
|
|
|
| _ -> ty
|
|
|
|
in
|
|
|
|
mark_loops ty;
|
|
|
|
Some ty
|
2006-04-16 16:28:22 -07:00
|
|
|
in
|
2003-07-02 02:14:35 -07:00
|
|
|
begin match decl.type_kind with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Type_abstract -> ()
|
2007-10-09 03:29:37 -07:00
|
|
|
| Type_variant cstrs ->
|
2012-05-30 07:52:37 -07:00
|
|
|
List.iter
|
2012-07-30 11:04:46 -07:00
|
|
|
(fun (_, args,ret_type_opt) ->
|
|
|
|
List.iter mark_loops args;
|
|
|
|
may mark_loops ret_type_opt)
|
|
|
|
cstrs
|
2007-10-09 03:29:37 -07:00
|
|
|
| Type_record(l, rep) ->
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter (fun (_, _, ty) -> mark_loops ty) l
|
2003-07-02 02:14:35 -07:00
|
|
|
end;
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let type_param =
|
|
|
|
function
|
|
|
|
| Otyp_var (_, id) -> id
|
|
|
|
| _ -> "?"
|
2000-09-07 03:57:32 -07:00
|
|
|
in
|
2001-08-06 05:28:50 -07:00
|
|
|
let type_defined decl =
|
2005-03-22 19:08:37 -08:00
|
|
|
let abstr =
|
|
|
|
match decl.type_kind with
|
|
|
|
Type_abstract ->
|
2010-04-26 05:54:11 -07:00
|
|
|
decl.type_manifest = None || decl.type_private = Private
|
2010-11-07 22:59:46 -08:00
|
|
|
| Type_record _ ->
|
2007-10-09 03:29:37 -07:00
|
|
|
decl.type_private = Private
|
2010-11-07 22:59:46 -08:00
|
|
|
| Type_variant tll ->
|
|
|
|
decl.type_private = Private ||
|
|
|
|
List.exists (fun (_,_,ret) -> ret <> None) tll
|
2005-03-22 19:08:37 -08:00
|
|
|
in
|
|
|
|
let vari =
|
|
|
|
List.map2
|
|
|
|
(fun ty (co,cn,ct) ->
|
2011-09-22 02:05:42 -07:00
|
|
|
if abstr || not (is_Tvar (repr ty)) then (co,cn) else (true,true))
|
2004-12-09 04:40:53 -08:00
|
|
|
decl.type_params decl.type_variance
|
2005-03-22 19:08:37 -08:00
|
|
|
in
|
|
|
|
(Ident.name id,
|
|
|
|
List.map2 (fun ty cocn -> type_param (tree_of_typexp false ty), cocn)
|
|
|
|
params vari)
|
2000-09-07 03:57:32 -07:00
|
|
|
in
|
2003-06-30 01:04:42 -07:00
|
|
|
let tree_of_manifest ty1 =
|
|
|
|
match ty_manifest with
|
2001-08-06 05:28:50 -07:00
|
|
|
| None -> ty1
|
|
|
|
| Some ty -> Otyp_manifest (tree_of_typexp false ty, ty1)
|
|
|
|
in
|
|
|
|
let (name, args) = type_defined decl in
|
|
|
|
let constraints = tree_of_constraints params in
|
2005-03-22 19:08:37 -08:00
|
|
|
let ty, priv =
|
2003-07-02 02:14:35 -07:00
|
|
|
match decl.type_kind with
|
2001-08-06 05:28:50 -07:00
|
|
|
| Type_abstract ->
|
2003-06-30 01:04:42 -07:00
|
|
|
begin match ty_manifest with
|
2005-03-22 19:08:37 -08:00
|
|
|
| None -> (Otyp_abstract, Public)
|
|
|
|
| Some ty ->
|
2007-10-17 20:38:28 -07:00
|
|
|
tree_of_typexp false ty, decl.type_private
|
2001-08-06 05:28:50 -07:00
|
|
|
end
|
2007-10-09 03:29:37 -07:00
|
|
|
| Type_variant cstrs ->
|
|
|
|
tree_of_manifest (Otyp_sum (List.map tree_of_constructor cstrs)),
|
|
|
|
decl.type_private
|
|
|
|
| Type_record(lbls, rep) ->
|
|
|
|
tree_of_manifest (Otyp_record (List.map tree_of_label lbls)),
|
|
|
|
decl.type_private
|
2001-08-06 05:28:50 -07:00
|
|
|
in
|
2005-03-22 19:08:37 -08:00
|
|
|
(name, args, ty, priv, constraints)
|
2000-03-08 08:48:10 -08:00
|
|
|
|
2011-09-26 23:37:06 -07:00
|
|
|
and tree_of_constructor (name, args, ret_type_opt) =
|
2012-05-30 07:52:37 -07:00
|
|
|
let name = Ident.name name in
|
2011-09-26 23:37:06 -07:00
|
|
|
if ret_type_opt = None then (name, tree_of_typlist false args, None) else
|
|
|
|
let nm = !names in
|
|
|
|
names := [];
|
|
|
|
let ret = may_map (tree_of_typexp false) ret_type_opt in
|
|
|
|
let args = tree_of_typlist false args in
|
|
|
|
names := nm;
|
|
|
|
(name, args, ret)
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2010-09-12 22:28:30 -07:00
|
|
|
|
|
|
|
and tree_of_constructor_ret =
|
|
|
|
function
|
|
|
|
| None -> None
|
2010-10-18 22:24:36 -07:00
|
|
|
| Some ret_type -> Some (tree_of_typexp false ret_type)
|
2000-03-08 08:48:10 -08:00
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
and tree_of_label (name, mut, arg) =
|
2012-05-30 07:52:37 -07:00
|
|
|
(Ident.name name, mut = Mutable, tree_of_typexp false arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-06-12 01:55:49 -07:00
|
|
|
let tree_of_type_declaration id decl rs =
|
|
|
|
Osig_type (tree_of_type_decl id decl, tree_of_rec rs)
|
2001-08-07 05:12:33 -07:00
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let type_declaration id ppf decl =
|
2004-06-12 01:55:49 -07:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_type_declaration id decl Trec_first)
|
1998-04-14 07:48:08 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print an exception declaration *)
|
|
|
|
|
2001-08-07 05:12:33 -07:00
|
|
|
let tree_of_exception_declaration id decl =
|
2012-03-06 11:03:17 -08:00
|
|
|
reset_and_mark_loops_list decl.exn_args;
|
|
|
|
let tyl = tree_of_typlist false decl.exn_args in
|
2001-08-07 05:12:33 -07:00
|
|
|
Osig_exception (Ident.name id, tyl)
|
|
|
|
|
|
|
|
let exception_declaration id ppf decl =
|
2002-02-13 03:09:19 -08:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_exception_declaration id decl)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-05-05 03:05:18 -07:00
|
|
|
(* Print a value declaration *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let tree_of_value_description id decl =
|
|
|
|
let id = Ident.name id in
|
|
|
|
let ty = tree_of_type_scheme decl.val_type in
|
|
|
|
let prims =
|
|
|
|
match decl.val_kind with
|
|
|
|
| Val_prim p -> Primitive.description_list p
|
|
|
|
| _ -> []
|
|
|
|
in
|
|
|
|
Osig_value (id, ty, prims)
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
let value_description id ppf decl =
|
2002-02-13 03:09:19 -08:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_value_description id decl)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Print a class type *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let class_var sch ppf l (m, t) =
|
|
|
|
fprintf ppf
|
|
|
|
"@ @[<2>val %s%s :@ %a@]" (string_of_mutable m) l (typexp sch 0) t
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2004-01-26 23:06:54 -08:00
|
|
|
let method_type (_, kind, ty) =
|
|
|
|
match field_kind_repr kind, repr ty with
|
2011-09-22 02:05:42 -07:00
|
|
|
Fpresent, {desc=Tpoly(ty, tyl)} -> (ty, tyl)
|
|
|
|
| _ , ty -> (ty, [])
|
2002-06-18 03:47:33 -07:00
|
|
|
|
2001-09-28 19:40:59 -07:00
|
|
|
let tree_of_metho sch concrete csil (lab, kind, ty) =
|
2003-11-25 01:20:45 -08:00
|
|
|
if lab <> dummy_method then begin
|
2004-01-26 23:06:54 -08:00
|
|
|
let kind = field_kind_repr kind in
|
|
|
|
let priv = kind <> Fpresent in
|
2001-08-07 01:03:04 -07:00
|
|
|
let virt = not (Concr.mem lab concrete) in
|
2011-09-22 02:05:42 -07:00
|
|
|
let (ty, tyl) = method_type (lab, kind, ty) in
|
|
|
|
let tty = tree_of_typexp sch ty in
|
|
|
|
remove_names tyl;
|
|
|
|
Ocsg_method (lab, priv, virt, tty) :: csil
|
2001-08-07 01:03:04 -07:00
|
|
|
end
|
|
|
|
else csil
|
|
|
|
|
2001-11-19 01:49:56 -08:00
|
|
|
let rec prepare_class_type params = function
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_constr (p, tyl, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = Ctype.self_type cty in
|
2003-04-22 03:46:57 -07:00
|
|
|
if List.memq (proxy sty) !visited_objects
|
2011-09-22 02:05:42 -07:00
|
|
|
|| not (List.for_all is_Tvar params)
|
2002-06-09 19:39:35 -07:00
|
|
|
|| List.exists (deep_occur sty) tyl
|
|
|
|
then prepare_class_type params cty
|
|
|
|
else List.iter mark_loops tyl
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = repr sign.cty_self in
|
|
|
|
(* Self may have a name *)
|
2003-04-22 03:46:57 -07:00
|
|
|
let px = proxy sty in
|
|
|
|
if List.memq px !visited_objects then add_alias sty
|
|
|
|
else visited_objects := px :: !visited_objects;
|
1998-11-24 13:55:05 -08:00
|
|
|
let (fields, _) =
|
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self)
|
|
|
|
in
|
2011-09-22 02:05:42 -07:00
|
|
|
List.iter (fun met -> mark_loops (fst (method_type met))) fields;
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.iter (fun _ (_, _, ty) -> mark_loops ty) sign.cty_vars
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_fun (_, ty, cty) ->
|
1998-11-29 10:02:58 -08:00
|
|
|
mark_loops ty;
|
2001-11-19 01:49:56 -08:00
|
|
|
prepare_class_type params cty
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2001-08-07 01:03:04 -07:00
|
|
|
let rec tree_of_class_type sch params =
|
|
|
|
function
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_constr (p', tyl, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = Ctype.self_type cty in
|
2003-04-22 03:46:57 -07:00
|
|
|
if List.memq (proxy sty) !visited_objects
|
2011-09-22 02:05:42 -07:00
|
|
|
|| not (List.for_all is_Tvar params)
|
2001-11-19 01:49:56 -08:00
|
|
|
then
|
2001-08-07 01:03:04 -07:00
|
|
|
tree_of_class_type sch params cty
|
2000-03-06 14:12:09 -08:00
|
|
|
else
|
2001-08-07 01:03:04 -07:00
|
|
|
Octy_constr (tree_of_path p', tree_of_typlist true tyl)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = repr sign.cty_self in
|
2001-08-07 01:03:04 -07:00
|
|
|
let self_ty =
|
2002-04-18 00:27:47 -07:00
|
|
|
if is_aliased sty then
|
|
|
|
Some (Otyp_var (false, name_of_type (proxy sty)))
|
2001-08-07 01:03:04 -07:00
|
|
|
else None
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (fields, _) =
|
2001-08-07 01:03:04 -07:00
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self)
|
|
|
|
in
|
|
|
|
let csil = [] in
|
|
|
|
let csil =
|
2002-01-10 00:15:44 -08:00
|
|
|
List.fold_left
|
|
|
|
(fun csil (ty1, ty2) -> Ocsg_constraint (ty1, ty2) :: csil)
|
|
|
|
csil (tree_of_constraints params)
|
2001-08-07 01:03:04 -07:00
|
|
|
in
|
2001-09-28 19:40:59 -07:00
|
|
|
let all_vars =
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.fold (fun l (m, v, t) all -> (l, m, v, t) :: all) sign.cty_vars []
|
|
|
|
in
|
2005-08-15 17:48:56 -07:00
|
|
|
(* Consequence of PR#3607: order of Map.fold has changed! *)
|
|
|
|
let all_vars = List.rev all_vars in
|
2001-08-07 01:03:04 -07:00
|
|
|
let csil =
|
2001-09-28 19:40:59 -07:00
|
|
|
List.fold_left
|
2006-04-04 19:28:13 -07:00
|
|
|
(fun csil (l, m, v, t) ->
|
|
|
|
Ocsg_value (l, m = Mutable, v = Virtual, tree_of_typexp sch t)
|
|
|
|
:: csil)
|
2001-09-28 19:40:59 -07:00
|
|
|
csil all_vars
|
2001-08-07 01:03:04 -07:00
|
|
|
in
|
|
|
|
let csil =
|
2002-01-10 00:15:44 -08:00
|
|
|
List.fold_left (tree_of_metho sch sign.cty_concr) csil fields
|
2001-08-07 01:03:04 -07:00
|
|
|
in
|
2001-09-28 19:40:59 -07:00
|
|
|
Octy_signature (self_ty, List.rev csil)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_fun (l, ty, cty) ->
|
2001-08-07 01:03:04 -07:00
|
|
|
let lab = if !print_labels && l <> "" || is_optional l then l else "" in
|
2000-03-06 14:12:09 -08:00
|
|
|
let ty =
|
|
|
|
if is_optional l then
|
|
|
|
match (repr ty).desc with
|
2000-06-12 00:07:01 -07:00
|
|
|
| Tconstr(path, [ty], _) when Path.same path Predef.path_option -> ty
|
|
|
|
| _ -> newconstr (Path.Pident(Ident.create "<hidden>")) []
|
2000-03-06 14:12:09 -08:00
|
|
|
else ty in
|
2001-09-28 19:40:59 -07:00
|
|
|
let tr = tree_of_typexp sch ty in
|
|
|
|
Octy_fun (lab, tr, tree_of_class_type sch params cty)
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
let class_type ppf cty =
|
1998-06-24 12:22:26 -07:00
|
|
|
reset ();
|
2001-11-19 01:49:56 -08:00
|
|
|
prepare_class_type [] cty;
|
2002-02-13 03:09:19 -08:00
|
|
|
!Oprint.out_class_type ppf (tree_of_class_type false [] cty)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2004-12-09 04:51:55 -08:00
|
|
|
let tree_of_class_param param variance =
|
|
|
|
(match tree_of_typexp true param with
|
|
|
|
Otyp_var (_, s) -> s
|
|
|
|
| _ -> "?"),
|
2011-09-22 02:05:42 -07:00
|
|
|
if is_Tvar (repr param) then (true, true) else variance
|
2004-12-09 04:51:55 -08:00
|
|
|
|
2004-12-09 04:40:53 -08:00
|
|
|
let tree_of_class_params params =
|
|
|
|
let tyl = tree_of_typlist true params in
|
|
|
|
List.map (function Otyp_var (_, s) -> s | _ -> "?") tyl
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2004-06-12 01:55:49 -07:00
|
|
|
let tree_of_class_declaration id cl rs =
|
2000-05-24 20:09:28 -07:00
|
|
|
let params = filter_params cl.cty_params in
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
reset ();
|
2002-04-18 00:27:47 -07:00
|
|
|
List.iter add_alias params;
|
2001-11-19 01:49:56 -08:00
|
|
|
prepare_class_type params cl.cty_type;
|
2011-07-20 02:17:07 -07:00
|
|
|
let sty = Ctype.self_type cl.cty_type in
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter mark_loops params;
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-06-27 22:47:42 -07:00
|
|
|
List.iter check_name_of_type (List.map proxy params);
|
2002-04-18 00:27:47 -07:00
|
|
|
if is_aliased sty then check_name_of_type (proxy sty);
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2001-08-07 01:03:04 -07:00
|
|
|
let vir_flag = cl.cty_new = None in
|
|
|
|
Osig_class
|
2004-12-09 04:40:53 -08:00
|
|
|
(vir_flag, Ident.name id,
|
2004-12-09 04:51:55 -08:00
|
|
|
List.map2 tree_of_class_param params cl.cty_variance,
|
2004-06-12 01:55:49 -07:00
|
|
|
tree_of_class_type true params cl.cty_type,
|
|
|
|
tree_of_rec rs)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2001-08-07 01:03:04 -07:00
|
|
|
let class_declaration id ppf cl =
|
2004-06-12 01:55:49 -07:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_class_declaration id cl Trec_first)
|
2001-08-07 01:03:04 -07:00
|
|
|
|
2004-06-12 01:55:49 -07:00
|
|
|
let tree_of_cltype_declaration id cl rs =
|
1998-06-24 12:22:26 -07:00
|
|
|
let params = List.map repr cl.clty_params in
|
|
|
|
|
|
|
|
reset ();
|
2002-05-29 23:24:45 -07:00
|
|
|
List.iter add_alias params;
|
2001-11-19 01:49:56 -08:00
|
|
|
prepare_class_type params cl.clty_type;
|
2011-07-20 02:17:07 -07:00
|
|
|
let sty = Ctype.self_type cl.clty_type in
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter mark_loops params;
|
|
|
|
|
2000-06-27 22:47:42 -07:00
|
|
|
List.iter check_name_of_type (List.map proxy params);
|
2002-04-18 00:27:47 -07:00
|
|
|
if is_aliased sty then check_name_of_type (proxy sty);
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let sign = Ctype.signature_of_class_type cl.clty_type in
|
2000-03-06 14:12:09 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let virt =
|
|
|
|
let (fields, _) =
|
2000-03-06 14:12:09 -08:00
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self) in
|
1998-06-24 12:22:26 -07:00
|
|
|
List.exists
|
|
|
|
(fun (lab, _, ty) ->
|
2003-11-25 01:20:45 -08:00
|
|
|
not (lab = dummy_method || Concr.mem lab sign.cty_concr))
|
2006-04-04 19:28:13 -07:00
|
|
|
fields
|
|
|
|
|| Vars.fold (fun _ (_,vr,_) b -> vr = Virtual || b) sign.cty_vars false
|
|
|
|
in
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2001-08-07 01:03:04 -07:00
|
|
|
Osig_class_type
|
2004-12-09 04:40:53 -08:00
|
|
|
(virt, Ident.name id,
|
2004-12-09 04:51:55 -08:00
|
|
|
List.map2 tree_of_class_param params cl.clty_variance,
|
2004-06-12 01:55:49 -07:00
|
|
|
tree_of_class_type true params cl.clty_type,
|
|
|
|
tree_of_rec rs)
|
2001-08-07 01:03:04 -07:00
|
|
|
|
|
|
|
let cltype_declaration id ppf cl =
|
2004-06-12 01:55:49 -07:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_cltype_declaration id cl Trec_first)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print a module type *)
|
|
|
|
|
2012-01-21 22:42:56 -08:00
|
|
|
let wrap_env fenv ftree arg =
|
2012-01-21 21:56:11 -08:00
|
|
|
let env = !printing_env in
|
2012-04-10 22:50:08 -07:00
|
|
|
set_printing_env (fenv env);
|
2012-01-21 22:42:56 -08:00
|
|
|
let tree = ftree arg in
|
2012-04-10 22:50:08 -07:00
|
|
|
set_printing_env env;
|
2012-01-21 22:42:56 -08:00
|
|
|
tree
|
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let filter_rem_sig item rem =
|
2012-04-18 23:23:14 -07:00
|
|
|
match item, rem with
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_class _, ctydecl :: tydecl1 :: tydecl2 :: rem ->
|
2012-04-18 23:23:14 -07:00
|
|
|
([ctydecl; tydecl1; tydecl2], rem)
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_class_type _, tydecl1 :: tydecl2 :: rem ->
|
2012-04-18 23:23:14 -07:00
|
|
|
([tydecl1; tydecl2], rem)
|
|
|
|
| _ ->
|
|
|
|
([], rem)
|
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let rec tree_of_modtype = function
|
2012-05-30 07:52:37 -07:00
|
|
|
| Mty_ident p ->
|
2001-08-06 05:28:50 -07:00
|
|
|
Omty_ident (tree_of_path p)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Mty_signature sg ->
|
2001-08-06 05:28:50 -07:00
|
|
|
Omty_signature (tree_of_signature sg)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Mty_functor(param, ty_arg, ty_res) ->
|
2001-08-06 05:28:50 -07:00
|
|
|
Omty_functor
|
2012-04-18 23:23:14 -07:00
|
|
|
(Ident.name param, tree_of_modtype ty_arg,
|
|
|
|
wrap_env (Env.add_module param ty_arg) tree_of_modtype ty_res)
|
2012-01-21 22:42:56 -08:00
|
|
|
|
|
|
|
and tree_of_signature sg =
|
2012-04-18 23:23:14 -07:00
|
|
|
wrap_env (fun env -> env) tree_of_signature_rec sg
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2012-01-21 22:42:56 -08:00
|
|
|
and tree_of_signature_rec = function
|
2012-04-18 23:23:14 -07:00
|
|
|
[] -> []
|
|
|
|
| item :: rem ->
|
|
|
|
let (sg, rem) = filter_rem_sig item rem in
|
|
|
|
let trees =
|
|
|
|
match item with
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_value(id, decl) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[tree_of_value_description id decl]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_type(id, _, _) when is_row_name (Ident.name id) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_type(id, decl, rs) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[Osig_type(tree_of_type_decl id decl, tree_of_rec rs)]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_exception(id, decl) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[tree_of_exception_declaration id decl]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_module(id, mty, rs) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[Osig_module (Ident.name id, tree_of_modtype mty, tree_of_rec rs)]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_modtype(id, decl) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[tree_of_modtype_declaration id decl]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_class(id, decl, rs) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[tree_of_class_declaration id decl rs]
|
2012-08-21 00:10:35 -07:00
|
|
|
| Sig_class_type(id, decl, rs) ->
|
2012-04-18 23:23:14 -07:00
|
|
|
[tree_of_cltype_declaration id decl rs]
|
|
|
|
in
|
|
|
|
set_printing_env (Env.add_signature (item :: sg) !printing_env);
|
|
|
|
trees @ tree_of_signature_rec rem
|
2001-08-06 05:28:50 -07:00
|
|
|
|
|
|
|
and tree_of_modtype_declaration id decl =
|
|
|
|
let mty =
|
|
|
|
match decl with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Modtype_abstract -> Omty_abstract
|
|
|
|
| Modtype_manifest mty -> tree_of_modtype mty
|
2001-08-06 05:28:50 -07:00
|
|
|
in
|
|
|
|
Osig_modtype (Ident.name id, mty)
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
let tree_of_module id mty rs =
|
2004-06-12 01:55:49 -07:00
|
|
|
Osig_module (Ident.name id, tree_of_modtype mty, tree_of_rec rs)
|
2001-08-07 05:12:33 -07:00
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let modtype ppf mty = !Oprint.out_module_type ppf (tree_of_modtype mty)
|
2001-08-06 05:28:50 -07:00
|
|
|
let modtype_declaration id ppf decl =
|
2002-02-13 03:09:19 -08:00
|
|
|
!Oprint.out_sig_item ppf (tree_of_modtype_declaration id decl)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-02-16 10:29:28 -08:00
|
|
|
(* Print a signature body (used by -i when compiling a .ml) *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-15 16:37:59 -07:00
|
|
|
let print_signature ppf tree =
|
2002-02-13 03:09:19 -08:00
|
|
|
fprintf ppf "@[<v>%a@]" !Oprint.out_signature tree
|
2001-08-15 16:37:59 -07:00
|
|
|
|
2001-08-06 05:28:50 -07:00
|
|
|
let signature ppf sg =
|
2002-02-13 03:09:19 -08:00
|
|
|
fprintf ppf "%a" print_signature (tree_of_signature sg)
|
1996-05-20 09:43:29 -07:00
|
|
|
|
|
|
|
(* Print an unification error *)
|
|
|
|
|
2012-01-21 19:15:14 -08:00
|
|
|
let same_path t t' =
|
|
|
|
let t = repr t and t' = repr t' in
|
|
|
|
t == t' ||
|
|
|
|
match t.desc, t'.desc with
|
|
|
|
Tconstr(p,tl,_), Tconstr(p',tl',_) ->
|
2012-12-10 02:39:07 -08:00
|
|
|
let (p1, s1) = best_type_path p and (p2, s2) = best_type_path p' in
|
|
|
|
begin match s1, s2 with
|
|
|
|
Nth n1, Nth n2 when n1 = n2 -> true
|
|
|
|
| (Id | Map _), (Id | Map _) when Path.same p1 p2 ->
|
|
|
|
let tl = apply_subst s1 tl and tl' = apply_subst s2 tl' in
|
|
|
|
List.length tl = List.length tl' &&
|
|
|
|
List.for_all2 same_type tl tl'
|
|
|
|
| _ -> false
|
|
|
|
end
|
2012-01-21 19:15:14 -08:00
|
|
|
| _ ->
|
|
|
|
false
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let type_expansion t ppf t' =
|
2012-01-21 19:15:14 -08:00
|
|
|
if same_path t t' then type_expr ppf t else
|
2006-04-16 16:28:22 -07:00
|
|
|
let t' = if proxy t == proxy t' then unalias t' else t' in
|
2002-06-12 02:52:08 -07:00
|
|
|
fprintf ppf "@[<2>%a@ =@ %a@]" type_expr t type_expr t'
|
1996-05-20 09:43:29 -07:00
|
|
|
|
2012-11-10 19:46:59 -08:00
|
|
|
let type_path_expansion tp ppf tp' =
|
|
|
|
if Path.same tp tp' then path ppf tp else
|
|
|
|
fprintf ppf "@[<2>%a@ =@ %a@]" path tp path tp'
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec trace fst txt ppf = function
|
|
|
|
| (t1, t1') :: (t2, t2') :: rem ->
|
|
|
|
if not fst then fprintf ppf "@,";
|
|
|
|
fprintf ppf "@[Type@;<1 2>%a@ %s@;<1 2>%a@] %a"
|
|
|
|
(type_expansion t1) t1' txt (type_expansion t2) t2'
|
|
|
|
(trace false txt) rem
|
|
|
|
| _ -> ()
|
1996-05-20 09:43:29 -07:00
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let rec filter_trace keep_last = function
|
2011-11-24 01:02:48 -08:00
|
|
|
| (_, t1') :: (_, t2') :: [] when is_Tvar t1' || is_Tvar t2' ->
|
|
|
|
[]
|
2000-03-06 14:12:09 -08:00
|
|
|
| (t1, t1') :: (t2, t2') :: rem ->
|
2013-01-29 06:21:12 -08:00
|
|
|
let rem' = filter_trace keep_last rem in
|
|
|
|
if is_constr_row t1' || is_constr_row t2'
|
|
|
|
|| same_path t1 t1' && same_path t2 t2' && not (keep_last && rem' = [])
|
1998-06-24 12:22:26 -07:00
|
|
|
then rem'
|
2000-03-06 14:12:09 -08:00
|
|
|
else (t1, t1') :: (t2, t2') :: rem'
|
|
|
|
| _ -> []
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2012-11-10 19:46:59 -08:00
|
|
|
let rec type_path_list ppf = function
|
|
|
|
| [tp, tp'] -> type_path_expansion tp ppf tp'
|
|
|
|
| (tp, tp') :: rem ->
|
|
|
|
fprintf ppf "%a@;<2 0>%a"
|
|
|
|
(type_path_expansion tp) tp'
|
|
|
|
type_path_list rem
|
|
|
|
| [] -> ()
|
|
|
|
|
2001-09-25 02:54:18 -07:00
|
|
|
(* Hide variant name and var, to force printing the expanded type *)
|
1999-11-30 08:07:38 -08:00
|
|
|
let hide_variant_name t =
|
|
|
|
match repr t with
|
2000-03-06 14:12:09 -08:00
|
|
|
| {desc = Tvariant row} as t when (row_repr row).row_name <> None ->
|
2001-09-25 02:54:18 -07:00
|
|
|
newty2 t.level
|
|
|
|
(Tvariant {(row_repr row) with row_name = None;
|
2011-09-22 02:05:42 -07:00
|
|
|
row_more = newvar2 (row_more row).level})
|
2000-03-06 14:12:09 -08:00
|
|
|
| _ -> t
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
let prepare_expansion (t, t') =
|
|
|
|
let t' = hide_variant_name t' in
|
2012-01-21 19:15:14 -08:00
|
|
|
mark_loops t;
|
|
|
|
if not (same_path t t') then mark_loops t';
|
1999-11-30 08:07:38 -08:00
|
|
|
(t, t')
|
|
|
|
|
2004-04-27 00:37:30 -07:00
|
|
|
let may_prepare_expansion compact (t, t') =
|
|
|
|
match (repr t').desc with
|
|
|
|
Tvariant _ | Tobject _ when compact ->
|
|
|
|
mark_loops t; (t, t)
|
|
|
|
| _ -> prepare_expansion (t, t')
|
|
|
|
|
2002-06-18 03:47:33 -07:00
|
|
|
let print_tags ppf fields =
|
|
|
|
match fields with [] -> ()
|
|
|
|
| (t, _) :: fields ->
|
|
|
|
fprintf ppf "`%s" t;
|
|
|
|
List.iter (fun (t, _) -> fprintf ppf ",@ `%s" t) fields
|
|
|
|
|
2004-04-27 00:37:30 -07:00
|
|
|
let has_explanation unif t3 t4 =
|
|
|
|
match t3.desc, t4.desc with
|
2011-12-14 18:25:01 -08:00
|
|
|
Tfield _, (Tnil|Tconstr _) | (Tnil|Tconstr _), Tfield _
|
2013-01-29 06:21:12 -08:00
|
|
|
| Tnil, Tconstr _ | Tconstr _, Tnil
|
2011-11-24 01:02:48 -08:00
|
|
|
| _, Tvar _ | Tvar _, _
|
2004-04-27 00:37:30 -07:00
|
|
|
| Tvariant _, Tvariant _ -> true
|
2011-12-14 18:25:01 -08:00
|
|
|
| Tfield (l,_,_,{desc=Tnil}), Tfield (l',_,_,{desc=Tnil}) -> l = l'
|
2004-04-27 00:37:30 -07:00
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let rec mismatch unif = function
|
|
|
|
(_, t) :: (_, t') :: rem ->
|
|
|
|
begin match mismatch unif rem with
|
|
|
|
Some _ as m -> m
|
|
|
|
| None ->
|
|
|
|
if has_explanation unif t t' then Some(t,t') else None
|
|
|
|
end
|
|
|
|
| [] -> None
|
|
|
|
| _ -> assert false
|
|
|
|
|
2001-08-07 23:58:58 -07:00
|
|
|
let explanation unif t3 t4 ppf =
|
|
|
|
match t3.desc, t4.desc with
|
2012-05-24 22:11:55 -07:00
|
|
|
| Ttuple [], Tvar _ | Tvar _, Ttuple [] ->
|
2001-08-07 23:58:58 -07:00
|
|
|
fprintf ppf "@,Self type cannot escape its class"
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tconstr (p, tl, _), Tvar _
|
2011-11-24 01:02:48 -08:00
|
|
|
when unif && t4.level < Path.binding_time p ->
|
2001-08-07 23:58:58 -07:00
|
|
|
fprintf ppf
|
|
|
|
"@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
|
|
|
|
path p
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _, Tconstr (p, tl, _)
|
2011-11-24 01:02:48 -08:00
|
|
|
when unif && t3.level < Path.binding_time p ->
|
2001-08-07 23:58:58 -07:00
|
|
|
fprintf ppf
|
|
|
|
"@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
|
|
|
|
path p
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _, Tunivar _ | Tunivar _, Tvar _ ->
|
2002-06-18 03:47:33 -07:00
|
|
|
fprintf ppf "@,The universal variable %a would escape its scope"
|
2011-09-22 02:05:42 -07:00
|
|
|
type_expr (if is_Tunivar t3 then t3 else t4)
|
2011-11-24 01:02:48 -08:00
|
|
|
| Tvar _, _ | _, Tvar _ ->
|
|
|
|
let t, t' = if is_Tvar t3 then (t3, t4) else (t4, t3) in
|
|
|
|
if occur_in Env.empty t t' then
|
|
|
|
fprintf ppf "@,@[<hov>The type variable %a occurs inside@ %a@]"
|
|
|
|
type_expr t type_expr t'
|
|
|
|
else
|
|
|
|
fprintf ppf "@,@[<hov>This instance of %a is ambiguous:@ %s@]"
|
|
|
|
type_expr t'
|
|
|
|
"it would escape the scope of its equation"
|
2003-11-25 01:20:45 -08:00
|
|
|
| Tfield (lab, _, _, _), _
|
|
|
|
| _, Tfield (lab, _, _, _) when lab = dummy_method ->
|
2001-08-07 23:58:58 -07:00
|
|
|
fprintf ppf
|
|
|
|
"@,Self type cannot be unified with a closed object type"
|
2011-12-14 18:25:01 -08:00
|
|
|
| Tfield (l,_,_,{desc=Tnil}), Tfield (l',_,_,{desc=Tnil}) when l = l' ->
|
2004-04-27 00:37:30 -07:00
|
|
|
fprintf ppf "@,Types for method %s are incompatible" l
|
2011-12-14 18:25:01 -08:00
|
|
|
| (Tnil|Tconstr _), Tfield (l, _, _, _) ->
|
2001-08-07 23:58:58 -07:00
|
|
|
fprintf ppf
|
2007-02-16 03:18:54 -08:00
|
|
|
"@,@[The first object type has no method %s@]" l
|
2011-12-14 18:25:01 -08:00
|
|
|
| Tfield (l, _, _, _), (Tnil|Tconstr _) ->
|
2007-02-16 03:18:54 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@,@[The second object type has no method %s@]" l
|
2013-01-29 06:21:12 -08:00
|
|
|
| Tnil, Tconstr _ | Tconstr _, Tnil ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[The %s object type has an abstract row, it cannot be closed@]"
|
|
|
|
(if t4.desc = Tnil then "first" else "second")
|
2002-06-18 03:47:33 -07:00
|
|
|
| Tvariant row1, Tvariant row2 ->
|
|
|
|
let row1 = row_repr row1 and row2 = row_repr row2 in
|
|
|
|
begin match
|
2007-02-16 03:18:54 -08:00
|
|
|
row1.row_fields, row1.row_closed, row2.row_fields, row2.row_closed with
|
2002-06-18 03:47:33 -07:00
|
|
|
| [], true, [], true ->
|
|
|
|
fprintf ppf "@,These two variant types have no intersection"
|
|
|
|
| [], true, fields, _ ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[The first variant type does not allow tag(s)@ @[<hov>%a@]@]"
|
|
|
|
print_tags fields
|
|
|
|
| fields, _, [], true ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[The second variant type does not allow tag(s)@ @[<hov>%a@]@]"
|
|
|
|
print_tags fields
|
2004-04-27 00:37:30 -07:00
|
|
|
| [l1,_], true, [l2,_], true when l1 = l2 ->
|
|
|
|
fprintf ppf "@,Types for tag `%s are incompatible" l1
|
2002-06-18 03:47:33 -07:00
|
|
|
| _ -> ()
|
|
|
|
end
|
2001-08-07 23:58:58 -07:00
|
|
|
| _ -> ()
|
|
|
|
|
2004-04-27 00:37:30 -07:00
|
|
|
let explanation unif mis ppf =
|
|
|
|
match mis with
|
|
|
|
None -> ()
|
|
|
|
| Some (t3, t4) -> explanation unif t3 t4 ppf
|
|
|
|
|
2010-04-30 00:11:27 -07:00
|
|
|
let ident_same_name id1 id2 =
|
|
|
|
if Ident.equal id1 id2 && not (Ident.same id1 id2) then begin
|
|
|
|
add_unique id1; add_unique id2
|
|
|
|
end
|
|
|
|
|
|
|
|
let rec path_same_name p1 p2 =
|
|
|
|
match p1, p2 with
|
|
|
|
Pident id1, Pident id2 -> ident_same_name id1 id2
|
|
|
|
| Pdot (p1, s1, _), Pdot (p2, s2, _) when s1 = s2 -> path_same_name p1 p2
|
|
|
|
| Papply (p1, p1'), Papply (p2, p2') ->
|
|
|
|
path_same_name p1 p2; path_same_name p1' p2'
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
let type_same_name t1 t2 =
|
|
|
|
match (repr t1).desc, (repr t2).desc with
|
2012-01-21 19:15:14 -08:00
|
|
|
Tconstr (p1, _, _), Tconstr (p2, _, _) ->
|
2012-12-10 02:39:07 -08:00
|
|
|
path_same_name (fst (best_type_path p1)) (fst (best_type_path p2))
|
2010-04-30 00:11:27 -07:00
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
let rec trace_same_names = function
|
|
|
|
(t1, t1') :: (t2, t2') :: rem ->
|
|
|
|
type_same_name t1 t2; type_same_name t1' t2'; trace_same_names rem
|
|
|
|
| _ -> ()
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let unification_error unif tr txt1 ppf txt2 =
|
1996-05-20 09:43:29 -07:00
|
|
|
reset ();
|
2010-04-30 00:11:27 -07:00
|
|
|
trace_same_names tr;
|
1999-11-30 08:07:38 -08:00
|
|
|
let tr = List.map (fun (t, t') -> (t, hide_variant_name t')) tr in
|
2004-04-27 00:37:30 -07:00
|
|
|
let mis = mismatch unif tr in
|
1998-06-24 12:22:26 -07:00
|
|
|
match tr with
|
2000-03-06 14:12:09 -08:00
|
|
|
| [] | _ :: [] -> assert false
|
|
|
|
| t1 :: t2 :: tr ->
|
1999-11-30 08:07:38 -08:00
|
|
|
try
|
2013-01-29 06:21:12 -08:00
|
|
|
let tr = filter_trace (mis = None) tr in
|
2004-04-27 00:37:30 -07:00
|
|
|
let t1, t1' = may_prepare_expansion (tr = []) t1
|
|
|
|
and t2, t2' = may_prepare_expansion (tr = []) t2 in
|
|
|
|
print_labels := not !Clflags.classic;
|
1999-11-30 08:07:38 -08:00
|
|
|
let tr = List.map prepare_expansion tr in
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[<v>\
|
|
|
|
@[%t@;<1 2>%a@ \
|
|
|
|
%t@;<1 2>%a\
|
|
|
|
@]%a%t\
|
|
|
|
@]"
|
|
|
|
txt1 (type_expansion t1) t1'
|
|
|
|
txt2 (type_expansion t2) t2'
|
|
|
|
(trace false "is not compatible with type") tr
|
2004-04-27 00:37:30 -07:00
|
|
|
(explanation unif mis);
|
1999-11-30 08:07:38 -08:00
|
|
|
print_labels := true
|
|
|
|
with exn ->
|
|
|
|
print_labels := true;
|
|
|
|
raise exn
|
1998-08-15 06:50:50 -07:00
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let report_unification_error ppf env ?(unif=true)
|
2012-01-22 23:59:45 -08:00
|
|
|
tr txt1 txt2 =
|
|
|
|
wrap_printing_env env (fun () -> unification_error unif tr txt1 ppf txt2)
|
2012-01-21 19:15:14 -08:00
|
|
|
;;
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let trace fst keep_last txt ppf tr =
|
1999-11-30 08:07:38 -08:00
|
|
|
print_labels := not !Clflags.classic;
|
2010-04-30 00:11:27 -07:00
|
|
|
trace_same_names tr;
|
2000-08-10 20:19:51 -07:00
|
|
|
try match tr with
|
2001-08-07 23:58:58 -07:00
|
|
|
t1 :: t2 :: tr' ->
|
2013-01-29 06:21:12 -08:00
|
|
|
if fst then trace fst txt ppf (t1 :: t2 :: filter_trace keep_last tr')
|
|
|
|
else trace fst txt ppf (filter_trace keep_last tr);
|
2000-08-10 20:19:51 -07:00
|
|
|
print_labels := true
|
|
|
|
| _ -> ()
|
1999-11-30 08:07:38 -08:00
|
|
|
with exn ->
|
|
|
|
print_labels := true;
|
|
|
|
raise exn
|
2000-03-06 14:12:09 -08:00
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let report_subtyping_error ppf env tr1 txt1 tr2 =
|
|
|
|
wrap_printing_env env (fun () ->
|
2012-01-22 23:59:45 -08:00
|
|
|
reset ();
|
|
|
|
let tr1 = List.map prepare_expansion tr1
|
|
|
|
and tr2 = List.map prepare_expansion tr2 in
|
2013-01-29 06:21:12 -08:00
|
|
|
fprintf ppf "@[<v>%a" (trace true (tr2 = []) txt1) tr1;
|
|
|
|
if tr2 = [] then fprintf ppf "@]" else
|
2012-01-22 23:59:45 -08:00
|
|
|
let mis = mismatch true tr2 in
|
2013-01-29 06:21:12 -08:00
|
|
|
fprintf ppf "%a%t@]"
|
|
|
|
(trace false (mis = None) "is not compatible with type") tr2
|
|
|
|
(explanation true mis))
|
|
|
|
|
|
|
|
let report_ambiguous_type_error ppf env (tp0, tp0') tpl txt1 txt2 txt3 =
|
|
|
|
wrap_printing_env env (fun () ->
|
|
|
|
reset ();
|
|
|
|
List.iter
|
|
|
|
(fun (tp, tp') -> path_same_name tp0 tp; path_same_name tp0' tp')
|
|
|
|
tpl;
|
|
|
|
match tpl with
|
|
|
|
[] -> assert false
|
|
|
|
| [tp, tp'] ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[%t@;<1 2>%a@ \
|
|
|
|
%t@;<1 2>%a\
|
|
|
|
@]"
|
|
|
|
txt1 (type_path_expansion tp) tp'
|
|
|
|
txt3 (type_path_expansion tp0) tp0'
|
|
|
|
| _ ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[%t@;<1 2>@[<hv>%a@]\
|
|
|
|
@ %t@;<1 2>%a\
|
|
|
|
@]"
|
|
|
|
txt2 type_path_list tpl
|
|
|
|
txt3 (type_path_expansion tp0) tp0')
|