2012-05-30 08:25:49 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2012-08-01 05:38:51 -07:00
|
|
|
(* OCaml *)
|
2012-05-30 08:25:49 -07:00
|
|
|
(* *)
|
|
|
|
(* Fabrice Le Fessant, INRIA Saclay *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2012 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. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
2012-11-08 02:19:36 -08:00
|
|
|
|
|
|
|
(* Generate an .annot file from a .cmt file. *)
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2012-11-08 09:21:27 -08:00
|
|
|
open Asttypes
|
2012-05-30 08:25:49 -07:00
|
|
|
open Typedtree
|
2014-12-22 01:55:17 -08:00
|
|
|
open Tast_mapper
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2012-11-08 09:21:27 -08:00
|
|
|
let bind_variables scope =
|
2014-12-22 01:55:17 -08:00
|
|
|
let super = Tast_mapper.default in
|
|
|
|
let pat sub p =
|
|
|
|
begin match p.pat_desc with
|
|
|
|
| Tpat_var (id, _) | Tpat_alias (_, id, _) ->
|
|
|
|
Stypes.record (Stypes.An_ident (p.pat_loc,
|
|
|
|
Ident.name id,
|
|
|
|
Annot.Idef scope))
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
super.pat sub p;
|
|
|
|
in
|
|
|
|
{super with pat}
|
|
|
|
|
|
|
|
let bind_variables scope =
|
|
|
|
let o = bind_variables scope in
|
|
|
|
fun p -> ignore (o.pat o p)
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2012-11-08 09:21:27 -08:00
|
|
|
let bind_bindings scope bindings =
|
|
|
|
let o = bind_variables scope in
|
2014-12-22 01:55:17 -08:00
|
|
|
List.iter (fun x -> o x.vb_pat) bindings
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2012-11-08 09:21:27 -08:00
|
|
|
let bind_cases l =
|
2013-04-15 09:23:22 -07:00
|
|
|
List.iter
|
|
|
|
(fun {c_lhs; c_guard; c_rhs} ->
|
|
|
|
let loc =
|
|
|
|
let open Location in
|
|
|
|
match c_guard with
|
|
|
|
| None -> c_rhs.exp_loc
|
|
|
|
| Some g -> {c_rhs.exp_loc with loc_start=g.exp_loc.loc_start}
|
|
|
|
in
|
2014-12-22 01:55:17 -08:00
|
|
|
bind_variables loc c_lhs
|
|
|
|
)
|
|
|
|
l
|
|
|
|
|
|
|
|
let rec iterator ~scope rebuild_env =
|
|
|
|
let super = Tast_mapper.default in
|
|
|
|
let class_expr sub node =
|
|
|
|
Stypes.record (Stypes.Ti_class node);
|
|
|
|
super.class_expr sub node
|
|
|
|
|
|
|
|
and module_expr _sub node =
|
|
|
|
Stypes.record (Stypes.Ti_mod node);
|
|
|
|
super.module_expr (iterator ~scope:node.mod_loc rebuild_env) node
|
|
|
|
|
|
|
|
and expr sub exp =
|
|
|
|
begin match exp.exp_desc with
|
|
|
|
| Texp_ident (path, _, _) ->
|
|
|
|
let full_name = Path.name ~paren:Oprint.parenthesized_ident path in
|
|
|
|
let env =
|
|
|
|
if rebuild_env then
|
2012-05-30 08:25:49 -07:00
|
|
|
try
|
2014-12-22 01:55:17 -08:00
|
|
|
Env.env_of_only_summary Envaux.env_from_summary exp.exp_env
|
|
|
|
with Envaux.Error err ->
|
|
|
|
Format.eprintf "%a@." Envaux.report_error err;
|
|
|
|
exit 2
|
|
|
|
else
|
|
|
|
exp.exp_env
|
|
|
|
in
|
|
|
|
let annot =
|
|
|
|
try
|
|
|
|
let desc = Env.find_value path env in
|
|
|
|
let dloc = desc.Types.val_loc in
|
|
|
|
if dloc.Location.loc_ghost then Annot.Iref_external
|
|
|
|
else Annot.Iref_internal dloc
|
|
|
|
with Not_found ->
|
|
|
|
Annot.Iref_external
|
|
|
|
in
|
|
|
|
Stypes.record
|
|
|
|
(Stypes.An_ident (exp.exp_loc, full_name , annot))
|
|
|
|
| Texp_let (Recursive, bindings, _) ->
|
|
|
|
bind_bindings exp.exp_loc bindings
|
|
|
|
| Texp_let (Nonrecursive, bindings, body) ->
|
|
|
|
bind_bindings body.exp_loc bindings
|
Revert GPR#305 (exception patterns under or-patterns) from trunk
This week we merged several changes from Thomas Refis, to allow the
use of exception patterns under or-patterns, to write code such as
match foo x with
| None | exception Not_found -> ...
| Some -> ...
Unfortunately, I failed to properly assess the impact of this change,
and in particular to make sure that Luc Maranget had properly reviewed
this code -- any change to the pattern-matching machinery should be
reviewed by Luc.
The problem that I had not foreseen and that he would have immediately
realized is that, while adapting the pattern-matching *compiler* is
relatively easy (Thomas inserted a transformation at the right place
to separate exception patterns from the others and handle them
separately, using the staticraise construct used by the
pattern-matching compiler to avoid duplicating the
right-hand-side branch), adapting the pattern-matching warnings
machinery is both more subtle and easier to overlook (it may fail
silently and nobody notices, unlike wrong code production). This part
of the compiler is subtle and best understood by Luc, but he does not
have the time to do a proper review of those changes in the timeframe
for the 4.03 feature freeze (mid-December).
I believe the right move in this case, implemented in the present
commit, is to revert the change from trunk (this is not a feature that
we must *imperatively* have in 4.03), do a proper job of understanding
the changes, and integrate the change when we are confident it is
ready. I hope to do this in 2016, together with Luc Maranget and
Thomas Refis -- hopefully this would allow Thomas and I to be more
confident when changing the pattern-matching machinery in the future.
Revert "Merge pull request #343 from trefis/pr7083"
This reverts commit 22681b8d2a56b308673b58fba1a06781bfc6d4b6, reversing
changes made to a24e4edf0a37d78abc1046cc453b84625b1521b5.
Revert "Merge pull request #341 from trefis/or-exception"
This reverts commit f8f68bd329375fd61e33781f61deeaeec2733f4b, reversing
changes made to 1534fe8082f6edd68be3fb960606a0e2fa87a116.
Revert "Merge pull request #305 from trefis/or-exception"
This reverts commit cfeda89396c67656d61ee24509278e50cb6e36e6, reversing
changes made to 77cf36cf82e3fb87469138c5da8f4ca9774414ff.
2015-12-12 01:52:33 -08:00
|
|
|
| Texp_match (_, f1, f2, _) ->
|
|
|
|
bind_cases f1;
|
|
|
|
bind_cases f2
|
2014-12-22 01:55:17 -08:00
|
|
|
| Texp_function (_, f, _)
|
|
|
|
| Texp_try (_, f) ->
|
|
|
|
bind_cases f
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
Stypes.record (Stypes.Ti_expr exp);
|
|
|
|
super.expr sub exp
|
|
|
|
|
|
|
|
and pat sub p =
|
|
|
|
Stypes.record (Stypes.Ti_pat p);
|
|
|
|
super.pat sub p
|
|
|
|
in
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2014-12-22 01:55:17 -08:00
|
|
|
let structure_item_rem sub s rem =
|
|
|
|
begin match s with
|
|
|
|
| {str_desc = Tstr_value (rec_flag, bindings); str_loc = loc} ->
|
|
|
|
let open Location in
|
|
|
|
let doit loc_start = bind_bindings {scope with loc_start} bindings in
|
|
|
|
begin match rec_flag, rem with
|
|
|
|
| Recursive, _ -> doit loc.loc_start
|
|
|
|
| Nonrecursive, [] -> doit loc.loc_end
|
|
|
|
| Nonrecursive, {str_loc = loc2} :: _ -> doit loc2.loc_start
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
()
|
|
|
|
end;
|
|
|
|
Stypes.record_phrase s.str_loc;
|
|
|
|
super.structure_item sub s
|
|
|
|
in
|
|
|
|
let structure_item sub s =
|
|
|
|
(* This will be used for Partial_structure_item.
|
|
|
|
We don't have here the location of the "next" item,
|
|
|
|
this will give a slightly different scope for the non-recursive
|
|
|
|
binding case. *)
|
|
|
|
structure_item_rem sub s []
|
|
|
|
and structure sub l =
|
|
|
|
let rec loop = function
|
|
|
|
| str :: rem -> structure_item_rem sub str rem :: loop rem
|
|
|
|
| [] -> []
|
|
|
|
in
|
|
|
|
{l with str_items = loop l.str_items}
|
|
|
|
in
|
|
|
|
{super with class_expr; module_expr; expr; pat; structure_item; structure}
|
2012-05-30 08:25:49 -07:00
|
|
|
|
2012-11-08 09:21:27 -08:00
|
|
|
let binary_part iter x =
|
2014-12-22 01:55:17 -08:00
|
|
|
let app f x = ignore (f iter x) in
|
2012-11-08 06:15:11 -08:00
|
|
|
let open Cmt_format in
|
|
|
|
match x with
|
2014-12-22 01:55:17 -08:00
|
|
|
| Partial_structure x -> app iter.structure x
|
|
|
|
| Partial_structure_item x -> app iter.structure_item x
|
|
|
|
| Partial_expression x -> app iter.expr x
|
|
|
|
| Partial_pattern x -> app iter.pat x
|
|
|
|
| Partial_class_expr x -> app iter.class_expr x
|
|
|
|
| Partial_signature x -> app iter.signature x
|
|
|
|
| Partial_signature_item x -> app iter.signature_item x
|
|
|
|
| Partial_module_type x -> app iter.module_type x
|
2012-11-08 06:15:11 -08:00
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
let gen_annot target_filename filename
|
|
|
|
{Cmt_format.cmt_loadpath; cmt_annots; cmt_use_summaries; _} =
|
2012-11-08 06:15:11 -08:00
|
|
|
let open Cmt_format in
|
2012-11-08 02:19:36 -08:00
|
|
|
Envaux.reset_cache ();
|
|
|
|
Config.load_path := cmt_loadpath;
|
2012-11-08 06:15:11 -08:00
|
|
|
let target_filename =
|
|
|
|
match target_filename with
|
|
|
|
| None -> Some (filename ^ ".annot")
|
|
|
|
| Some "-" -> None
|
2014-05-12 03:41:21 -07:00
|
|
|
| Some _ -> target_filename
|
2012-11-08 06:15:11 -08:00
|
|
|
in
|
2014-12-22 01:55:17 -08:00
|
|
|
let iterator = iterator ~scope:Location.none cmt_use_summaries in
|
2012-11-08 02:19:36 -08:00
|
|
|
match cmt_annots with
|
2012-11-08 06:15:11 -08:00
|
|
|
| Implementation typedtree ->
|
2014-12-22 01:55:17 -08:00
|
|
|
ignore (iterator.structure iterator typedtree);
|
2012-11-08 02:19:36 -08:00
|
|
|
Stypes.dump target_filename
|
2012-11-08 06:15:11 -08:00
|
|
|
| Interface _ ->
|
2013-09-04 08:12:37 -07:00
|
|
|
Printf.eprintf "Cannot generate annotations for interface file\n%!";
|
2012-05-30 08:25:49 -07:00
|
|
|
exit 2
|
2012-11-08 06:15:11 -08:00
|
|
|
| Partial_implementation parts ->
|
2012-11-08 09:21:27 -08:00
|
|
|
Array.iter (binary_part iterator) parts;
|
2012-11-08 06:15:11 -08:00
|
|
|
Stypes.dump target_filename
|
2012-11-08 02:19:36 -08:00
|
|
|
| _ ->
|
2012-05-30 08:25:49 -07:00
|
|
|
Printf.fprintf stderr "File was generated with an error\n%!";
|
|
|
|
exit 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let gen_ml target_filename filename cmt =
|
|
|
|
let (printer, ext) =
|
|
|
|
match cmt.Cmt_format.cmt_annots with
|
|
|
|
| Cmt_format.Implementation typedtree ->
|
2014-08-22 06:45:02 -07:00
|
|
|
(fun ppf -> Pprintast.structure ppf
|
|
|
|
(Untypeast.untype_structure typedtree)),
|
|
|
|
".ml"
|
2012-05-30 08:25:49 -07:00
|
|
|
| Cmt_format.Interface typedtree ->
|
2014-08-22 06:45:02 -07:00
|
|
|
(fun ppf -> Pprintast.signature ppf
|
|
|
|
(Untypeast.untype_signature typedtree)),
|
|
|
|
".mli"
|
2012-05-30 08:25:49 -07:00
|
|
|
| _ ->
|
|
|
|
Printf.fprintf stderr "File was generated with an error\n%!";
|
|
|
|
exit 2
|
|
|
|
in
|
|
|
|
let target_filename = match target_filename with
|
|
|
|
None -> Some (filename ^ ext)
|
|
|
|
| Some "-" -> None
|
2014-05-12 03:41:21 -07:00
|
|
|
| Some _ -> target_filename
|
2012-05-30 08:25:49 -07:00
|
|
|
in
|
|
|
|
let oc = match target_filename with
|
|
|
|
None -> None
|
|
|
|
| Some filename -> Some (open_out filename) in
|
|
|
|
let ppf = match oc with
|
|
|
|
None -> Format.std_formatter
|
|
|
|
| Some oc -> Format.formatter_of_out_channel oc in
|
|
|
|
printer ppf;
|
|
|
|
Format.pp_print_flush ppf ();
|
|
|
|
match oc with
|
|
|
|
None -> flush stdout
|
|
|
|
| Some oc -> close_out oc
|