2003-04-01 17:32:09 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Damien Doligez, projet Moscova, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2003 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Recording and dumping (partial) type information *)
|
|
|
|
|
|
|
|
(*
|
|
|
|
We record all types in a list as they are created.
|
|
|
|
This means we can dump type information even if type inference fails,
|
|
|
|
which is extremely important, since type information is most
|
|
|
|
interesting in case of errors.
|
|
|
|
*)
|
|
|
|
|
2007-05-16 01:21:41 -07:00
|
|
|
open Annot;;
|
2003-04-01 17:32:09 -08:00
|
|
|
open Format;;
|
|
|
|
open Lexing;;
|
|
|
|
open Location;;
|
2003-04-01 22:57:15 -08:00
|
|
|
open Typedtree;;
|
2003-04-01 17:32:09 -08:00
|
|
|
|
2007-05-16 01:21:41 -07:00
|
|
|
type annotation =
|
|
|
|
| Ti_pat of pattern
|
2003-04-01 22:57:15 -08:00
|
|
|
| Ti_expr of expression
|
|
|
|
| Ti_class of class_expr
|
|
|
|
| Ti_mod of module_expr
|
2007-05-16 01:21:41 -07:00
|
|
|
| An_call of Location.t * Annot.call
|
|
|
|
| An_ident of Location.t * Annot.ident
|
2003-04-01 22:57:15 -08:00
|
|
|
;;
|
|
|
|
|
|
|
|
let get_location ti =
|
|
|
|
match ti with
|
|
|
|
Ti_pat p -> p.pat_loc
|
|
|
|
| Ti_expr e -> e.exp_loc
|
|
|
|
| Ti_class c -> c.cl_loc
|
|
|
|
| Ti_mod m -> m.mod_loc
|
2007-05-16 01:21:41 -07:00
|
|
|
| An_call (l, k) -> l
|
|
|
|
| An_ident (l, k) -> l
|
2003-04-01 22:57:15 -08:00
|
|
|
;;
|
|
|
|
|
2007-05-16 01:21:41 -07:00
|
|
|
let annotations = ref ([] : annotation list);;
|
2003-07-23 09:52:41 -07:00
|
|
|
let phrases = ref ([] : Location.t list);;
|
2003-04-01 17:32:09 -08:00
|
|
|
|
2003-04-01 22:57:15 -08:00
|
|
|
let record ti =
|
2007-05-16 01:21:41 -07:00
|
|
|
if !Clflags.annotations && not (get_location ti).Location.loc_ghost then
|
|
|
|
annotations := ti :: !annotations
|
2003-04-01 17:32:09 -08:00
|
|
|
;;
|
|
|
|
|
2003-07-23 09:52:41 -07:00
|
|
|
let record_phrase loc =
|
2007-05-16 01:21:41 -07:00
|
|
|
if !Clflags.annotations then phrases := loc :: !phrases;
|
2003-07-23 09:52:41 -07:00
|
|
|
;;
|
|
|
|
|
2003-07-25 11:00:40 -07:00
|
|
|
(* comparison order:
|
|
|
|
the intervals are sorted by order of increasing upper bound
|
|
|
|
same upper bound -> sorted by decreasing lower bound
|
|
|
|
*)
|
|
|
|
let cmp_loc_inner_first loc1 loc2 =
|
|
|
|
match compare loc1.loc_end.pos_cnum loc2.loc_end.pos_cnum with
|
|
|
|
| 0 -> compare loc2.loc_start.pos_cnum loc1.loc_start.pos_cnum
|
2003-04-01 17:32:09 -08:00
|
|
|
| x -> x
|
|
|
|
;;
|
2003-07-25 11:00:40 -07:00
|
|
|
let cmp_ti_inner_first ti1 ti2 =
|
|
|
|
cmp_loc_inner_first (get_location ti1) (get_location ti2)
|
|
|
|
;;
|
2003-04-01 17:32:09 -08:00
|
|
|
|
|
|
|
let print_position pp pos =
|
2007-05-16 01:21:41 -07:00
|
|
|
if pos = dummy_pos then
|
|
|
|
fprintf pp "--"
|
|
|
|
else
|
|
|
|
fprintf pp "%S %d %d %d" pos.pos_fname pos.pos_lnum pos.pos_bol
|
|
|
|
pos.pos_cnum;
|
|
|
|
;;
|
|
|
|
|
|
|
|
let print_location pp loc =
|
|
|
|
print_position pp loc.loc_start;
|
|
|
|
fprintf pp " ";
|
|
|
|
print_position pp loc.loc_end;
|
2003-04-01 17:32:09 -08:00
|
|
|
;;
|
|
|
|
|
2003-07-23 09:52:41 -07:00
|
|
|
let sort_filter_phrases () =
|
2003-07-25 11:00:40 -07:00
|
|
|
let ph = List.sort (fun x y -> cmp_loc_inner_first y x) !phrases in
|
2003-07-23 09:52:41 -07:00
|
|
|
let rec loop accu cur l =
|
|
|
|
match l with
|
|
|
|
| [] -> accu
|
|
|
|
| loc :: t ->
|
2003-07-25 11:00:40 -07:00
|
|
|
if cur.loc_start.pos_cnum <= loc.loc_start.pos_cnum
|
|
|
|
&& cur.loc_end.pos_cnum >= loc.loc_end.pos_cnum
|
2003-07-23 09:52:41 -07:00
|
|
|
then loop accu cur t
|
|
|
|
else loop (loc :: accu) loc t
|
|
|
|
in
|
|
|
|
phrases := loop [] Location.none ph;
|
|
|
|
;;
|
|
|
|
|
|
|
|
let rec printtyp_reset_maybe loc =
|
|
|
|
match !phrases with
|
2003-07-25 11:00:40 -07:00
|
|
|
| cur :: t when cur.loc_start.pos_cnum <= loc.loc_start.pos_cnum ->
|
|
|
|
Printtyp.reset ();
|
|
|
|
phrases := t;
|
|
|
|
printtyp_reset_maybe loc;
|
|
|
|
| _ -> ()
|
2003-07-23 09:52:41 -07:00
|
|
|
;;
|
|
|
|
|
2007-05-16 01:21:41 -07:00
|
|
|
let call_kind_string k =
|
|
|
|
match k with
|
|
|
|
| Tail -> "tail"
|
|
|
|
| Stack -> "stack"
|
|
|
|
| Inline -> "inline"
|
|
|
|
;;
|
|
|
|
|
|
|
|
let print_ident_annot pp k =
|
|
|
|
match k with
|
|
|
|
| Idef l -> fprintf pp "def %a@." print_location l;
|
|
|
|
| Iref_internal l -> fprintf pp "internal_ref %a@." print_location l;
|
|
|
|
| Iref_external s -> fprintf pp "external_ref %s@." s;
|
|
|
|
;;
|
2003-07-23 09:52:41 -07:00
|
|
|
|
2003-06-12 05:52:17 -07:00
|
|
|
(* The format of the annotation file is documented in emacs/caml-types.el. *)
|
|
|
|
|
2007-05-16 01:21:41 -07:00
|
|
|
let print_info pp prev_loc ti =
|
2003-04-01 22:57:15 -08:00
|
|
|
match ti with
|
2007-05-16 01:21:41 -07:00
|
|
|
| Ti_class _ | Ti_mod _ -> prev_loc
|
2003-04-01 22:57:15 -08:00
|
|
|
| Ti_pat {pat_loc = loc; pat_type = typ}
|
|
|
|
| Ti_expr {exp_loc = loc; exp_type = typ} ->
|
2007-05-16 01:21:41 -07:00
|
|
|
if loc <> prev_loc then fprintf pp "%a@." print_location loc;
|
|
|
|
fprintf pp "type(@. ";
|
2003-07-23 09:52:41 -07:00
|
|
|
printtyp_reset_maybe loc;
|
|
|
|
Printtyp.mark_loops typ;
|
2006-04-16 16:28:22 -07:00
|
|
|
Printtyp.type_sch pp typ;
|
2003-04-01 22:57:15 -08:00
|
|
|
fprintf pp "@.)@.";
|
2007-05-16 01:21:41 -07:00
|
|
|
loc
|
|
|
|
| An_call (loc, k) ->
|
|
|
|
if loc <> prev_loc then fprintf pp "%a@." print_location loc;
|
|
|
|
fprintf pp "call(@. %s@.)@." (call_kind_string k);
|
|
|
|
loc
|
|
|
|
| An_ident (loc, k) ->
|
|
|
|
if loc <> prev_loc then fprintf pp "%a@." print_location loc;
|
|
|
|
fprintf pp "ident(@. ";
|
|
|
|
print_ident_annot pp k;
|
|
|
|
fprintf pp ")@.";
|
|
|
|
loc
|
2003-04-01 17:32:09 -08:00
|
|
|
;;
|
|
|
|
|
2003-04-01 22:57:15 -08:00
|
|
|
let get_info () =
|
2007-05-16 01:21:41 -07:00
|
|
|
let info = List.fast_sort cmp_ti_inner_first !annotations in
|
|
|
|
annotations := [];
|
2003-04-01 22:57:15 -08:00
|
|
|
info
|
2003-07-23 09:52:41 -07:00
|
|
|
;;
|
2003-04-01 22:57:15 -08:00
|
|
|
|
|
|
|
let dump filename =
|
2007-05-16 01:21:41 -07:00
|
|
|
if !Clflags.annotations then begin
|
2003-07-25 11:00:40 -07:00
|
|
|
let info = get_info () in
|
2003-04-01 17:32:09 -08:00
|
|
|
let pp = formatter_of_out_channel (open_out filename) in
|
2003-07-23 09:52:41 -07:00
|
|
|
sort_filter_phrases ();
|
2007-05-16 01:21:41 -07:00
|
|
|
ignore (List.fold_left (print_info pp) Location.none info);
|
2003-07-23 09:52:41 -07:00
|
|
|
phrases := [];
|
2003-07-25 11:00:40 -07:00
|
|
|
end else begin
|
2007-05-16 01:21:41 -07:00
|
|
|
annotations := [];
|
2003-04-01 17:32:09 -08:00
|
|
|
end;
|
|
|
|
;;
|