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
|
|
|
(* *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
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
|
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
|
|
|
|
| Lident s -> fprintf ppf "%s" s
|
|
|
|
| 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 *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let ident ppf id = fprintf ppf "%s" (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
|
|
|
|
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 ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "%s" s
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pdot(p, s, pos) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "%a.%s" path p 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
|
|
|
|
|
|
|
(* Print a type expression *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let names = ref ([] : (type_expr * string) list)
|
|
|
|
let name_counter = ref 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let reset_names () = names := []; name_counter := 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let new_name () =
|
|
|
|
let name =
|
|
|
|
if !name_counter < 26
|
|
|
|
then String.make 1 (Char.chr(97 + !name_counter))
|
|
|
|
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;
|
|
|
|
name
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let name_of_type t =
|
|
|
|
try List.assq t !names with Not_found ->
|
|
|
|
let name = new_name () in
|
|
|
|
names := (t, name) :: !names;
|
|
|
|
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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_name_of_type ppf t = fprintf ppf "%s" (name_of_type t)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let visited_objects = ref ([] : type_expr list)
|
|
|
|
let aliased = ref ([] : type_expr list)
|
|
|
|
|
2000-05-10 19:22:54 -07:00
|
|
|
let is_aliased ty = List.memq ty !aliased
|
|
|
|
let add_alias ty =
|
|
|
|
if not (is_aliased ty) then aliased := ty :: !aliased
|
2000-03-06 14:12:09 -08:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let proxy ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvariant row -> Btype.row_more row
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> ty
|
|
|
|
|
|
|
|
let namable_row row =
|
2000-02-24 02:18:25 -08:00
|
|
|
row.row_name <> None && row.row_closed &&
|
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
|
|
|
|
| Reither(c, l, _) -> if c then l = [] else List.length l = 1
|
|
|
|
| _ -> 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
|
2000-03-06 14:12:09 -08:00
|
|
|
if List.memq px visited 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
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvar -> ()
|
1999-11-30 08:07:38 -08: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
|
|
|
|
| Tconstr(_, 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 ->
|
1999-11-30 08:07:38 -08:00
|
|
|
let row = row_repr row in
|
2000-03-06 14:12:09 -08:00
|
|
|
if List.memq px !visited_objects then add_alias px else
|
|
|
|
begin
|
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
|
|
|
|
| _ ->
|
2000-05-11 20:12:19 -07:00
|
|
|
iter_row (mark_loops_rec visited) {row with row_bound = []}
|
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 ->
|
1997-02-20 12:39:02 -08:00
|
|
|
mark_loops_rec visited fi
|
|
|
|
| Some (_, l) ->
|
|
|
|
List.iter (mark_loops_rec visited) l
|
|
|
|
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)"
|
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 () =
|
|
|
|
visited_objects := []; aliased := []
|
|
|
|
|
|
|
|
let reset () =
|
|
|
|
reset_names (); reset_loop_marks ()
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let reset_and_mark_loops ty =
|
|
|
|
reset (); mark_loops ty;;
|
|
|
|
|
|
|
|
let reset_and_mark_loops_list tyl =
|
|
|
|
reset (); List.iter mark_loops tyl;;
|
|
|
|
|
|
|
|
(* 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
|
|
|
|
2000-03-28 22:09:03 -08:00
|
|
|
let rec print_list_init pr sep ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| a :: l -> sep (); pr ppf a; print_list_init pr sep ppf l;;
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec print_list pr sep ppf = function
|
|
|
|
| [] -> ()
|
2000-03-28 22:09:03 -08:00
|
|
|
| [a] -> pr ppf a
|
|
|
|
| a :: l -> pr ppf a; sep (); print_list pr sep ppf l;;
|
1999-11-30 08:07:38 -08:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec typexp sch prio0 ppf 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
|
2000-03-06 14:12:09 -08:00
|
|
|
if List.mem_assq px !names then
|
2000-05-14 17:52:09 -07:00
|
|
|
let mark = if ty.desc = Tvar then non_gen_mark sch px else "" in
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "'%s%a" mark print_name_of_type px else
|
|
|
|
|
|
|
|
let pr_typ ppf prio =
|
|
|
|
(match ty.desc with
|
|
|
|
| Tvar ->
|
|
|
|
fprintf ppf "'%s%a" (non_gen_mark sch ty) print_name_of_type ty
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tarrow(l, ty1, ty2) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let pr_arrow l ty1 ppf ty2 =
|
|
|
|
print_label ppf l;
|
|
|
|
if is_optional l then
|
|
|
|
match (repr ty1).desc with
|
2000-06-12 00:07:01 -07:00
|
|
|
| Tconstr(path, [ty], _) when Path.same path Predef.path_option ->
|
2000-03-06 14:12:09 -08:00
|
|
|
typexp sch 2 ppf ty
|
2000-06-12 00:07:01 -07:00
|
|
|
| _ -> fprintf ppf "<hidden>"
|
2000-03-06 14:12:09 -08:00
|
|
|
else typexp sch 2 ppf ty1;
|
|
|
|
fprintf ppf " ->@ %a" (typexp sch 1) ty2 in
|
|
|
|
if prio >= 2
|
|
|
|
then fprintf ppf "@[<1>(%a)@]" (pr_arrow l ty1) ty2
|
|
|
|
else fprintf ppf "@[<0>%a@]" (pr_arrow l ty1) ty2
|
1997-02-20 12:39:02 -08:00
|
|
|
| Ttuple tyl ->
|
2000-03-06 14:12:09 -08:00
|
|
|
if prio >= 3
|
|
|
|
then fprintf ppf "@[<1>(%a)@]" (typlist sch 3 " *") tyl
|
|
|
|
else fprintf ppf "@[<0>%a@]" (typlist sch 3 " *") tyl
|
1997-02-20 12:39:02 -08:00
|
|
|
| Tconstr(p, tyl, abbrev) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[%a%a@]" (typargs sch) tyl path p
|
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
|
2000-03-14 23:43:33 -08:00
|
|
|
let pr_present =
|
2000-03-14 23:55:24 -08:00
|
|
|
print_list (fun ppf (s, _) -> fprintf ppf "`%s" s)
|
2000-06-14 03:01:44 -07:00
|
|
|
(fun () -> fprintf ppf "@ ")
|
2000-03-14 23:43:33 -08:00
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
begin match row.row_name with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Some(p, tyl) when namable_row row ->
|
|
|
|
let sharp_mark =
|
|
|
|
if not all_present then non_gen_mark sch px ^ "#" else "" in
|
|
|
|
let print_present ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| l ->
|
2000-03-14 23:43:33 -08:00
|
|
|
if not all_present then
|
2000-03-14 23:55:24 -08:00
|
|
|
fprintf ppf "@[<hov>[>%a]@]" pr_present l in
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[%a%s%a%a@]"
|
|
|
|
(typargs sch) tyl sharp_mark path p print_present present
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let gen_mark =
|
|
|
|
if not (row.row_closed && all_present)
|
|
|
|
then non_gen_mark sch px
|
|
|
|
else "" in
|
|
|
|
let close_mark =
|
2000-06-11 07:34:10 -07:00
|
|
|
if row.row_closed then
|
|
|
|
if all_present then " " else "< "
|
|
|
|
else "> " in
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_present ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| l ->
|
2000-03-14 23:43:33 -08:00
|
|
|
if not all_present then
|
2000-04-01 05:00:35 -08:00
|
|
|
fprintf ppf "@;<1 -2>> @[<hov>%a@]" pr_present l in
|
2000-03-08 08:48:10 -08:00
|
|
|
let print_fields =
|
2000-04-01 05:00:35 -08:00
|
|
|
print_list (row_field sch) (fun () -> fprintf ppf "@;<1 -2>| ")
|
|
|
|
in
|
2000-03-08 08:48:10 -08:00
|
|
|
|
2000-06-11 07:34:10 -07:00
|
|
|
fprintf ppf "%s[%s@[<hv>@[<hv>%a@]%a]@]"
|
|
|
|
gen_mark close_mark print_fields fields
|
2000-03-08 08:48:10 -08:00
|
|
|
print_present present
|
1999-11-30 08:07:38 -08:00
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
| Tobject (fi, nm) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
typobject sch ty fi ppf nm
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst ty ->
|
2000-03-06 14:12:09 -08:00
|
|
|
typexp sch prio ppf ty
|
2000-05-24 20:09:28 -07:00
|
|
|
| Tlink _ | Tnil | Tfield _ ->
|
1997-02-20 12:39:02 -08:00
|
|
|
fatal_error "Printtyp.typexp"
|
2000-03-06 14:12:09 -08:00
|
|
|
) in
|
2000-05-10 19:22:54 -07:00
|
|
|
if is_aliased px then begin
|
2000-03-06 14:12:09 -08:00
|
|
|
check_name_of_type px;
|
|
|
|
if prio0 >= 1
|
2000-03-22 23:38:13 -08:00
|
|
|
then fprintf ppf "@[<1>(%a as '%a)@]" pr_typ 0 print_name_of_type px
|
|
|
|
else fprintf ppf "@[%a as '%a@]" pr_typ prio0 print_name_of_type px end
|
2000-03-06 14:12:09 -08:00
|
|
|
else pr_typ ppf prio0
|
|
|
|
|
|
|
|
and row_field sch ppf (l, f) =
|
|
|
|
let pr_field ppf f =
|
|
|
|
match row_field_repr f with
|
|
|
|
| Rpresent None | Reither(true, [], _) -> ()
|
2000-04-01 05:00:35 -08:00
|
|
|
| Rpresent(Some ty) -> fprintf ppf " of@ %a" (typexp sch 0) ty
|
2000-03-06 14:12:09 -08:00
|
|
|
| Reither(c, tyl,_) ->
|
2000-04-01 05:00:35 -08:00
|
|
|
if c (* contradiction: un constructeur constant qui a un argument *)
|
|
|
|
then fprintf ppf " of@ &@ %a" (typlist sch 0 " &") tyl
|
|
|
|
else fprintf ppf " of@ %a" (typlist sch 0 " &") tyl
|
|
|
|
| Rabsent -> fprintf ppf "@ []" (* une erreur, en fait *) in
|
2000-03-08 08:48:10 -08:00
|
|
|
fprintf ppf "@[<hv 2>`%s%a@]" l pr_field f
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
and typlist sch prio sep ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| [ty] -> typexp sch prio ppf ty
|
|
|
|
| ty :: tyl ->
|
|
|
|
fprintf ppf "%a%s@ %a"
|
|
|
|
(typexp sch prio) ty sep (typlist sch prio sep) tyl
|
|
|
|
|
|
|
|
and typargs sch ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| [ty1] -> fprintf ppf "%a@ " (typexp sch 3) ty1
|
|
|
|
| tyl -> fprintf ppf "@[<1>(%a)@]@ " (typlist sch 0 ",") tyl
|
|
|
|
|
|
|
|
and typobject sch ty fi ppf nm =
|
1997-02-20 12:39:02 -08:00
|
|
|
begin match !nm with
|
2000-03-06 14:12:09 -08:00
|
|
|
| None ->
|
|
|
|
let pr_fields ppf fi =
|
|
|
|
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 =
|
|
|
|
Sort.list (fun (n, _) (n', _) -> n <= n') present_fields in
|
|
|
|
typfields sch rest ppf sorted_fields in
|
|
|
|
fprintf ppf "@[<2>< %a >@]" pr_fields fi
|
|
|
|
| Some (p, {desc = Tvar} :: tyl) ->
|
|
|
|
fprintf ppf "@[%a%s#%a@]" (typargs sch) tyl (non_gen_mark sch ty) path p
|
1997-02-20 12:39:02 -08:00
|
|
|
| _ ->
|
|
|
|
fatal_error "Printtyp.typobject"
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
and non_gen_mark sch ty =
|
|
|
|
if sch && ty.level <> generic_level then "_" else ""
|
|
|
|
|
|
|
|
and typfields sch rest ppf = function
|
|
|
|
| [] ->
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match rest.desc with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvar -> fprintf ppf "%s.." (non_gen_mark sch rest)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tnil -> ()
|
2000-03-06 14:12:09 -08:00
|
|
|
| _ -> fatal_error "typfields (1)"
|
1996-04-22 04:15:41 -07:00
|
|
|
end
|
|
|
|
| [(s, t)] ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "%s : %a" s (typexp sch 0) t;
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match rest.desc with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvar -> fprintf ppf ";@ "
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tnil -> ()
|
2000-03-06 14:12:09 -08:00
|
|
|
| _ -> fatal_error "typfields (2)"
|
1996-04-22 04:15:41 -07:00
|
|
|
end;
|
2000-03-06 14:12:09 -08:00
|
|
|
typfields sch rest ppf []
|
|
|
|
| (s, t) :: l ->
|
|
|
|
fprintf ppf "%s : %a;@ %a" s (typexp sch 0) t (typfields sch rest) l
|
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
|
|
|
|
|
|
|
(* Print one type declaration *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let constrain ppf ty =
|
1997-02-20 12:39:02 -08:00
|
|
|
let ty' = unalias ty in
|
2000-03-06 14:12:09 -08:00
|
|
|
if ty != ty'
|
|
|
|
then fprintf ppf "@ @[<2>constraint %a =@ %a@]" type_sch ty type_sch ty'
|
|
|
|
|
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
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec type_decl kwd id ppf 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
|
|
|
|
|
|
|
aliased := params @ !aliased;
|
|
|
|
List.iter mark_loops params;
|
2000-06-27 22:47:42 -07:00
|
|
|
List.iter check_name_of_type (List.map proxy params);
|
1997-02-20 12:39:02 -08:00
|
|
|
begin match decl.type_manifest with
|
2000-03-06 14:12:09 -08:00
|
|
|
| None -> ()
|
1997-02-20 12:39:02 -08:00
|
|
|
| Some ty -> mark_loops ty
|
|
|
|
end;
|
|
|
|
begin match decl.type_kind with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Type_abstract -> ()
|
1997-02-20 12:39:02 -08:00
|
|
|
| Type_variant [] -> ()
|
|
|
|
| Type_variant cstrs ->
|
|
|
|
List.iter (fun (_, args) -> List.iter mark_loops args) cstrs
|
2000-03-21 06:43:25 -08:00
|
|
|
| Type_record(l, rep) ->
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter (fun (_, _, ty) -> mark_loops ty) l
|
|
|
|
end;
|
|
|
|
|
2000-03-08 08:48:10 -08:00
|
|
|
let print_constraints ppf params =
|
|
|
|
List.iter (constrain ppf) params in
|
|
|
|
|
|
|
|
let print_manifest ppf decl =
|
|
|
|
match decl.type_manifest with
|
|
|
|
| None -> ()
|
|
|
|
| Some ty -> fprintf ppf " =@ %a" type_expr ty in
|
|
|
|
|
|
|
|
let print_name_args ppf decl =
|
|
|
|
fprintf ppf "%s%a%a"
|
|
|
|
kwd type_expr (Btype.newgenty (Tconstr(Pident id, params, ref Mnil)))
|
|
|
|
print_manifest decl in
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
begin match decl.type_kind with
|
2000-03-08 08:48:10 -08:00
|
|
|
| Type_abstract ->
|
2000-03-14 23:43:33 -08:00
|
|
|
fprintf ppf "@[<2>@[<hv 2>%a@]%a@]"
|
2000-03-08 08:48:10 -08:00
|
|
|
print_name_args decl print_constraints params
|
1995-09-26 13:23:29 -07:00
|
|
|
| Type_variant [] -> ()
|
|
|
|
(* A fatal error actually, except when printing type exn... *)
|
1996-11-08 05:38:34 -08:00
|
|
|
| Type_variant cstrs ->
|
2000-03-12 21:22:23 -08:00
|
|
|
fprintf ppf "@[<2>@[<hv 2>%a =@;<1 2>%a@]%a@]"
|
2000-03-08 08:48:10 -08:00
|
|
|
print_name_args decl
|
2000-03-12 21:22:23 -08:00
|
|
|
(print_list constructor (fun () -> fprintf ppf "@ | ")) cstrs
|
2000-03-08 08:48:10 -08:00
|
|
|
print_constraints params
|
2000-03-21 06:43:25 -08:00
|
|
|
| Type_record(lbls, rep) ->
|
2000-03-14 23:43:33 -08:00
|
|
|
fprintf ppf "@[<2>@[<hv 2>%a = {%a@;<1 -2>}@]@ %a@]"
|
2000-03-14 23:55:24 -08:00
|
|
|
print_name_args decl
|
2000-03-28 22:09:03 -08:00
|
|
|
(print_list_init label (fun () -> fprintf ppf "@ ")) lbls
|
2000-03-08 08:48:10 -08:00
|
|
|
print_constraints params
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
and constructor ppf (name, args) =
|
1995-05-04 03:15:53 -07:00
|
|
|
match args with
|
2000-03-07 05:11:14 -08:00
|
|
|
| [] -> fprintf ppf "%s" name
|
2000-03-08 08:48:10 -08:00
|
|
|
| _ -> fprintf ppf "@[<2>%s of@ %a@]" name (typlist false 3 " *") args
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
and label ppf (name, mut, arg) =
|
2000-03-08 08:48:10 -08:00
|
|
|
fprintf ppf "@[<2>%s%s :@ %a@];" (string_of_mutable mut) name type_expr arg
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
and string_of_mutable = function
|
|
|
|
| Immutable -> ""
|
|
|
|
| Mutable -> "mutable "
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-04-14 07:48:08 -07:00
|
|
|
let type_declaration id decl = type_decl "type " id decl
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print an exception declaration *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let exception_declaration id ppf decl =
|
|
|
|
fprintf ppf "exception %a" constructor (Ident.name 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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let value_ident ppf id =
|
1999-12-01 01:31:32 -08:00
|
|
|
let name = Ident.name id in
|
2000-03-06 14:12:09 -08:00
|
|
|
if List.mem name
|
|
|
|
["or"; "mod"; "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr"]
|
|
|
|
then fprintf ppf "( %s )" name
|
1999-12-01 01:31:32 -08:00
|
|
|
else match name.[0] with
|
2000-03-06 14:12:09 -08:00
|
|
|
| 'a' .. 'z' | '\223' .. '\246' | '\248' .. '\255' | '_' -> ident ppf id
|
|
|
|
| _ -> fprintf ppf "( %s )" name
|
|
|
|
|
|
|
|
let value_description id ppf decl =
|
|
|
|
let kwd = if decl.val_kind = Val_reg then "val " else "external " in
|
|
|
|
let pr_val ppf =
|
|
|
|
match decl.val_kind with
|
|
|
|
| Val_prim p ->
|
2000-03-13 08:49:01 -08:00
|
|
|
fprintf ppf "@ = %a" Primitive.print_description p
|
2000-03-06 14:12:09 -08:00
|
|
|
| _ -> () in
|
|
|
|
fprintf ppf "@[<2>%s%a :@ %a%t@]"
|
|
|
|
kwd value_ident id type_scheme decl.val_type pr_val
|
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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let metho sch concrete ppf (lab, kind, ty) =
|
1998-06-24 12:22:26 -07:00
|
|
|
if lab <> "*dummy method*" then begin
|
2000-03-06 14:12:09 -08:00
|
|
|
let priv =
|
|
|
|
match field_kind_repr kind with
|
|
|
|
| Fvar _ (* {contents = None} *) -> "private "
|
|
|
|
| _ (* Fpresent *) -> "" in
|
|
|
|
let virt =
|
|
|
|
if Concr.mem lab concrete then "" else "virtual " in
|
|
|
|
fprintf ppf "@ @[<2>method %s%s%s :@ %a@]" priv virt lab (typexp sch 0) ty
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec prepare_class_type = function
|
|
|
|
| Tcty_constr (p, tyl, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = Ctype.self_type cty in
|
1998-11-29 10:02:58 -08:00
|
|
|
begin try
|
|
|
|
if List.memq sty !visited_objects then raise (Unify []);
|
|
|
|
List.iter (occur Env.empty sty) tyl;
|
|
|
|
List.iter mark_loops tyl
|
|
|
|
with Unify _ ->
|
|
|
|
prepare_class_type cty
|
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tcty_signature sign ->
|
|
|
|
let sty = repr sign.cty_self in
|
|
|
|
(* Self may have a name *)
|
2000-05-10 19:22:54 -07:00
|
|
|
if List.memq sty !visited_objects then add_alias sty
|
|
|
|
else visited_objects := sty :: !visited_objects;
|
1998-11-24 13:55:05 -08:00
|
|
|
let (fields, _) =
|
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self)
|
|
|
|
in
|
|
|
|
List.iter (fun (_, _, ty) -> mark_loops ty) fields;
|
1998-06-24 12:22:26 -07:00
|
|
|
Vars.iter (fun _ (_, ty) -> mark_loops ty) sign.cty_vars
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) ->
|
1998-11-29 10:02:58 -08:00
|
|
|
mark_loops ty;
|
|
|
|
prepare_class_type cty
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec perform_class_type sch params ppf = function
|
|
|
|
| Tcty_constr (p', tyl, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = Ctype.self_type cty in
|
1998-11-29 10:02:58 -08:00
|
|
|
if List.memq sty !visited_objects then
|
2000-03-06 14:12:09 -08:00
|
|
|
perform_class_type sch params ppf cty
|
|
|
|
else
|
|
|
|
let pr_tyl ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| tyl -> fprintf ppf "@[<1>[%a]@]@ " (typlist true 0 ",") tyl in
|
|
|
|
fprintf ppf "@[%a%a@]" pr_tyl tyl path p'
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tcty_signature sign ->
|
|
|
|
let sty = repr sign.cty_self in
|
2000-03-06 14:12:09 -08:00
|
|
|
let pr_param ppf sty =
|
2000-05-10 19:22:54 -07:00
|
|
|
if is_aliased sty then
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@ @[('%a)@]" print_name_of_type sty in
|
|
|
|
|
|
|
|
fprintf ppf "@[<hv 2>@[<2>object%a@]%a"
|
|
|
|
pr_param sty
|
|
|
|
(fun ppf l -> List.iter (constrain ppf) l) params;
|
|
|
|
Vars.iter (class_var sch ppf) sign.cty_vars;
|
1998-06-24 12:22:26 -07:00
|
|
|
let (fields, _) =
|
2000-03-06 14:12:09 -08:00
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self) in
|
|
|
|
List.iter (metho sch sign.cty_concr ppf) fields;
|
|
|
|
fprintf ppf "@;<1 -2>end@]"
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l, ty, cty) ->
|
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
|
|
|
|
fprintf ppf "@[%a%a ->@ %a@]"
|
|
|
|
print_label l (typexp sch 2) ty (perform_class_type sch params) cty
|
|
|
|
|
|
|
|
let class_type ppf cty =
|
1998-06-24 12:22:26 -07:00
|
|
|
reset ();
|
1998-11-29 10:02:58 -08:00
|
|
|
prepare_class_type cty;
|
2000-03-06 14:12:09 -08:00
|
|
|
perform_class_type false [] ppf cty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let class_params ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| params -> fprintf ppf "@[<1>[%a]@]@ " (typlist true 0 ",") params
|
|
|
|
|
|
|
|
let class_declaration id ppf cl =
|
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 ();
|
1997-02-20 12:39:02 -08:00
|
|
|
aliased := params @ !aliased;
|
1998-11-29 10:02:58 -08:00
|
|
|
prepare_class_type cl.cty_type;
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = 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);
|
2000-05-10 19:22:54 -07:00
|
|
|
if is_aliased sty then check_name_of_type sty;
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let vir_mark = if cl.cty_new = None then " virtual" else "" in
|
|
|
|
fprintf ppf "@[<2>class%s@ %a%a@ :@ %a@]" vir_mark
|
|
|
|
class_params params ident id (perform_class_type true params) cl.cty_type
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let cltype_declaration id ppf cl =
|
1998-06-24 12:22:26 -07:00
|
|
|
let params = List.map repr cl.clty_params in
|
|
|
|
|
|
|
|
reset ();
|
|
|
|
aliased := params @ !aliased;
|
1998-11-29 10:02:58 -08:00
|
|
|
prepare_class_type cl.clty_type;
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = self_type cl.clty_type in
|
|
|
|
List.iter mark_loops params;
|
|
|
|
|
2000-06-27 22:47:42 -07:00
|
|
|
List.iter check_name_of_type (List.map proxy params);
|
2000-05-10 19:22:54 -07:00
|
|
|
if is_aliased sty then check_name_of_type 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) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
not (lab = "*dummy method*" || Concr.mem lab sign.cty_concr))
|
|
|
|
fields in
|
|
|
|
|
|
|
|
let vir_mark = if virt then " virtual" else "" in
|
|
|
|
fprintf ppf "@[<2>class type%s@ %a%a@ =@ %a@]"
|
|
|
|
vir_mark class_params params
|
|
|
|
ident id
|
|
|
|
(perform_class_type true params) cl.clty_type
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Print a module type *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec modtype ppf = function
|
|
|
|
| Tmty_ident p ->
|
|
|
|
path ppf p
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tmty_signature sg ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[<hv 2>sig%a@;<1 -2>end@]" (signature_body true) sg
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tmty_functor(param, ty_arg, ty_res) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[<2>functor@ (%a : %a) ->@ %a@]"
|
|
|
|
ident param modtype ty_arg modtype ty_res
|
|
|
|
|
|
|
|
and signature_body spc ppf = function
|
|
|
|
| [] -> ()
|
1996-05-16 07:16:34 -07:00
|
|
|
| item :: rem ->
|
2000-03-07 05:11:14 -08:00
|
|
|
if spc then fprintf ppf "@ ";
|
1996-05-16 07:16:34 -07:00
|
|
|
let cont =
|
|
|
|
match item with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tsig_value(id, decl) ->
|
|
|
|
value_description id ppf decl; rem
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tsig_type(id, decl) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
type_declaration id ppf decl;
|
1998-04-14 07:48:08 -07:00
|
|
|
let rec more_type_declarations = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tsig_type(id, decl) :: rem ->
|
|
|
|
fprintf ppf "@ %a" (type_decl "and " id) decl;
|
1998-04-14 07:48:08 -07:00
|
|
|
more_type_declarations rem
|
|
|
|
| rem -> rem in
|
|
|
|
more_type_declarations rem
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tsig_exception(id, decl) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
exception_declaration id ppf decl; rem
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tsig_module(id, mty) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[<2>module %a :@ %a@]" ident id modtype mty; rem
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tsig_modtype(id, decl) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
modtype_declaration id ppf decl; rem
|
1996-05-16 07:16:34 -07:00
|
|
|
| Tsig_class(id, decl) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
class_declaration id ppf decl;
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match rem with
|
2000-03-06 14:12:09 -08:00
|
|
|
| ctydecl :: tydecl1 :: tydecl2 :: rem -> rem
|
|
|
|
| _ -> []
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
|
|
|
| Tsig_cltype(id, decl) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
cltype_declaration id ppf decl;
|
2000-03-09 08:53:08 -08:00
|
|
|
begin match rem with
|
|
|
|
| tydecl1 :: tydecl2 :: rem -> rem
|
|
|
|
| _ -> []
|
|
|
|
end
|
2000-03-12 21:22:23 -08:00
|
|
|
in signature_body true ppf cont
|
2000-03-06 14:12:09 -08:00
|
|
|
|
|
|
|
and modtype_declaration id ppf decl =
|
|
|
|
let pr_decl ppf = function
|
|
|
|
| Tmodtype_abstract -> ()
|
|
|
|
| Tmodtype_manifest mty -> fprintf ppf " =@ %a" modtype mty in
|
2000-03-08 08:48:10 -08:00
|
|
|
fprintf ppf "@[<2>module type %a%a@]" ident id pr_decl 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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let signature ppf sg = fprintf ppf "@[<v>%a@]" (signature_body false) sg
|
1996-05-20 09:43:29 -07:00
|
|
|
|
|
|
|
(* Print an unification error *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let type_expansion t ppf t' =
|
|
|
|
if t == t' then type_expr ppf t
|
|
|
|
else fprintf ppf "@[<2>%a@ =@ %a@]" type_expr t type_expr t'
|
1996-05-20 09:43:29 -07:00
|
|
|
|
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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec mismatch = function
|
|
|
|
| [(_, t); (_, t')] -> (t, t')
|
|
|
|
| _ :: _ :: rem -> mismatch rem
|
|
|
|
| _ -> assert false
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec filter_trace = function
|
|
|
|
| (t1, t1') :: (t2, t2') :: rem ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let rem' = filter_trace rem in
|
2000-03-06 14:12:09 -08:00
|
|
|
if t1 == t1' && t2 == t2'
|
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
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(* Hide variant name, to force printing the expanded type *)
|
|
|
|
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 ->
|
1999-11-30 08:07:38 -08:00
|
|
|
newty2 t.level (Tvariant {(row_repr row) with row_name = None})
|
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
|
|
|
|
mark_loops t; if t != t' then mark_loops t';
|
|
|
|
(t, t')
|
|
|
|
|
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 ();
|
1999-11-30 08:07:38 -08:00
|
|
|
let tr = List.map (fun (t, t') -> (t, hide_variant_name t')) tr in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (t3, t4) = mismatch tr in
|
|
|
|
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
|
|
|
|
let t1, t1' = prepare_expansion t1
|
|
|
|
and t2, t2' = prepare_expansion t2 in
|
|
|
|
print_labels := not !Clflags.classic;
|
1998-06-24 12:22:26 -07:00
|
|
|
let tr = filter_trace tr in
|
1999-11-30 08:07:38 -08:00
|
|
|
let tr = List.map prepare_expansion tr in
|
2000-03-06 14:12:09 -08:00
|
|
|
let explanation ppf =
|
|
|
|
match t3.desc, t4.desc with
|
|
|
|
| Tfield _, Tvar | Tvar, Tfield _ ->
|
|
|
|
fprintf ppf "@,Self type cannot escape its class"
|
|
|
|
| Tconstr (p, _, _), Tvar
|
|
|
|
when unif && t4.level < Path.binding_time p ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
|
|
|
|
path p
|
|
|
|
| Tvar, Tconstr (p, _, _)
|
|
|
|
when unif && t3.level < Path.binding_time p ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
|
|
|
|
path p
|
|
|
|
| Tfield ("*dummy method*", _, _, _), _
|
|
|
|
| _, Tfield ("*dummy method*", _, _, _) ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,Self type cannot be unified with a closed object type"
|
|
|
|
| Tfield (l, _, _, _), _ ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[Only the first object type has a method %s@]" l
|
|
|
|
| _, Tfield (l, _, _, _) ->
|
|
|
|
fprintf ppf
|
|
|
|
"@,@[Only the second object type has a method %s@]" l
|
|
|
|
| _ -> () in
|
|
|
|
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
|
|
|
|
explanation;
|
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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_unification_error ppf tr txt1 txt2 =
|
|
|
|
unification_error true tr txt1 ppf txt2;;
|
|
|
|
|
|
|
|
let trace fst txt ppf tr =
|
1999-11-30 08:07:38 -08:00
|
|
|
print_labels := not !Clflags.classic;
|
2000-08-10 20:19:51 -07:00
|
|
|
try match tr with
|
|
|
|
t1 :: t2 :: tr ->
|
|
|
|
trace fst txt ppf (t1 :: t2 :: filter_trace tr);
|
|
|
|
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
|
|
|
|