2424(* Note: some of the functions here should go to Ast_mapper instead,
2525 which would encapsulate the "binary AST" protocol. *)
2626
27- let write_ast ( type a ) ( kind : a Ml_binary.kind ) fn ( ast : a ) =
27+ let write_ast fn ( ast0 : Ml_binary.ast0 ) =
2828 let oc = open_out_bin fn in
29- output_string oc (Ml_binary. magic_of_kind kind );
29+ output_string oc (Ml_binary. magic_of_ast0 ast0 );
3030 output_value oc (! Location. input_name : string );
31- output_value oc (ast : a );
31+ (match ast0 with
32+ | Ml_binary. Impl ast -> output_value oc (ast : Parsetree0.structure )
33+ | Ml_binary. Intf ast -> output_value oc (ast : Parsetree0.signature ));
3234 close_out oc
3335
3436let temp_ppx_file () =
@@ -53,25 +55,29 @@ let apply_rewriter kind fn_in ppx =
5355 fn_out
5456
5557(* This is a fatal error, no need to protect it *)
56- let read_ast (type a ) (kind : a Ml_binary.kind ) fn : a =
58+ let read_ast (type a ) (kind : a Ml_binary.kind ) fn : Ml_binary.ast0 =
5759 let ic = open_in_bin fn in
5860 let magic = Ml_binary. magic_of_kind kind in
5961 let buffer = really_input_string ic (String. length magic) in
6062 assert (buffer = magic);
6163 (* already checked by apply_rewriter *)
6264 Location. set_input_name @@ (input_value ic : string );
63- let ast = (input_value ic : a ) in
65+ let ast0 =
66+ match kind with
67+ | Ml_binary. Ml -> Ml_binary. Impl (input_value ic : Parsetree0.structure )
68+ | Ml_binary. Mli -> Ml_binary. Intf (input_value ic : Parsetree0.signature )
69+ in
6470 close_in ic;
65-
66- ast
71+ ast0
6772
6873(* * [ppxs] are a stack,
6974 [-ppx1 -ppx2 -ppx3]
7075 are stored as [-ppx3; -ppx2; -ppx1]
7176 [fold_right] happens to process the first one *)
7277let rewrite kind ppxs ast =
7378 let fn_in = temp_ppx_file () in
74- write_ast kind fn_in ast;
79+ let ast0 = Ml_binary. to_ast0 kind ast in
80+ write_ast fn_in ast0;
7581 let temp_files =
7682 List. fold_right
7783 (fun ppx fns ->
@@ -93,7 +99,7 @@ let apply_rewriters_str ?(restore = true) ~tool_name ast =
9399 | ppxs ->
94100 ast
95101 |> Ast_mapper. add_ppx_context_str ~tool_name
96- |> rewrite Ml ppxs
102+ |> rewrite Ml ppxs |> Ml_binary. ast0_to_structure
97103 |> Ast_mapper. drop_ppx_context_str ~restore
98104
99105let apply_rewriters_sig ?(restore = true ) ~tool_name ast =
@@ -102,7 +108,7 @@ let apply_rewriters_sig ?(restore = true) ~tool_name ast =
102108 | ppxs ->
103109 ast
104110 |> Ast_mapper. add_ppx_context_sig ~tool_name
105- |> rewrite Mli ppxs
111+ |> rewrite Mli ppxs |> Ml_binary. ast0_to_signature
106112 |> Ast_mapper. drop_ppx_context_sig ~restore
107113
108114let apply_rewriters ?restore ~tool_name (type a ) (kind : a Ml_binary.kind )
0 commit comments