Debian Codegraph @

expander/conversion.ml

raw symbols SCIP
1 open! Base
2 open! Ppxlib
3 open Ast_builder.Default
4 open Helpers
5
6 module Reference = struct
7 type t =
8 { types : type_declaration list
9 ; binds : value_binding list list
10 ; ident : longident_loc
11 ; args : (arg_label * expression) list
12 }
13
14 let bind t binds = { t with binds = binds :: t.binds }
15 let bind_types t types = { t with types = types @ t.types }
16
17 let maybe_apply { types; binds; ident; args } ~loc maybe_arg =
18 let ident = pexp_ident ~loc ident in
19 let args =
20 match maybe_arg with
21 | None -> args
22 | Some arg -> args @ [ Nolabel, arg ]
23 in
24 let expr =
25 match args with
26 | [] -> ident
27 | _ -> pexp_apply ~loc ident args
28 in
29 with_types ~loc ~types (with_let ~loc ~binds expr)
30 ;;
31
32 let apply t ~loc arg = maybe_apply t ~loc (Some arg)
33 let to_expression t ~loc = maybe_apply t ~loc None
34
35 let to_value_expression t ~loc ~rec_flag ~values_being_defined =
36 let may_refer_directly_to ident =
37 match rec_flag with
38 | Nonrecursive -> true
39 | Recursive -> not (Set.mem values_being_defined (Longident.name ident.txt))
40 in
41 match t with
42 | { types = []; binds = []; ident; args = [] } when may_refer_directly_to ident ->
43 pexp_ident ~loc ident
44 | _ -> fresh_lambda ~loc (fun ~arg -> apply t ~loc arg)
45 ;;
46 end
47
48 module Lambda = struct
49 type t =
50 { types : type_declaration list
51 ; binds : value_binding list list
52 ; cases : cases
53 }
54
55 let bind t binds = { t with binds = binds :: t.binds }
56 let bind_types t types = { t with types = types @ t.types }
57
58 (* generic case: use [function] or [match] *)
59 let maybe_apply_generic ~loc ~types ~binds maybe_arg cases =
60 let expr =
61 match maybe_arg with
62 | None -> pexp_function_cases ~loc cases
63 | Some arg -> pexp_match ~loc arg cases
64 in
65 with_types ~loc ~types (with_let ~loc ~binds expr)
66 ;;
67
68 (* zero cases: synthesize an "impossible" case, i.e. [| _ -> .] *)
69 let maybe_apply_impossible ~loc ~types ~binds maybe_arg =
70 [ case ~lhs:(ppat_any ~loc) ~guard:None ~rhs:(pexp_unreachable ~loc) ]
71 |> maybe_apply_generic ~loc ~binds ~types maybe_arg
72 ;;
73
74 (* one case without guard: use [fun] or [let] *)
75 let maybe_apply_simple ~loc ~types ~binds maybe_arg pat body =
76 let expr =
77 match maybe_arg with
78 | None -> pexp_fun ~loc Nolabel None pat body
79 | Some arg -> pexp_let ~loc Nonrecursive [ value_binding ~loc ~pat ~expr:arg ] body
80 in
81 with_types ~loc ~types (with_let ~loc ~binds expr)
82 ;;
83
84 (* shared special-casing logic for [apply] and [to_expression] *)
85 let maybe_apply t ~loc maybe_arg =
86 match t with
87 | { types; binds; cases = [] } -> maybe_apply_impossible ~loc ~types ~binds maybe_arg
88 | { types; binds; cases = [ { pc_lhs; pc_guard = None; pc_rhs } ] } ->
89 maybe_apply_simple ~loc ~types ~binds maybe_arg pc_lhs pc_rhs
90 | { types; binds; cases } -> maybe_apply_generic ~loc ~types ~binds maybe_arg cases
91 ;;
92
93 let apply t ~loc arg = maybe_apply t ~loc (Some arg)
94 let to_expression t ~loc = maybe_apply t ~loc None
95
96 let to_value_expression t ~loc =
97 match t with
98 | { types = []; binds = []; cases = _ } ->
99 (* lambdas without [let] are already values *)
100 let expr = to_expression t ~loc in
101 assert (is_value_expression expr);
102 expr
103 | _ -> fresh_lambda ~loc (fun ~arg -> apply t ~loc arg)
104 ;;
105 end
106
107 type t =
108 | Reference of Reference.t
109 | Lambda of Lambda.t
110
111 let of_lambda cases = Lambda { types = []; binds = []; cases }
112
113 let of_reference_exn expr =
114 match expr.pexp_desc with
115 | Pexp_ident ident -> Reference { types = []; binds = []; ident; args = [] }
116 | Pexp_apply ({ pexp_desc = Pexp_ident ident; _ }, args) ->
117 Reference { types = []; binds = []; ident; args }
118 | _ ->
119 Location.raise_errorf
120 ~loc:expr.pexp_loc
121 "ppx_sexp_conv: internal error.\n\
122 [Conversion.of_reference_exn] expected an identifier possibly applied to arguments.\n\
123 Instead, got:\n\
124 %s"
125 (Pprintast.string_of_expression expr)
126 ;;
127
128 let to_expression t ~loc =
129 match t with
130 | Reference reference -> Reference.to_expression ~loc reference
131 | Lambda lambda -> Lambda.to_expression ~loc lambda
132 ;;
133
134 let to_value_expression t ~loc ~rec_flag ~values_being_defined =
135 match t with
136 | Reference reference ->
137 Reference.to_value_expression ~loc ~rec_flag ~values_being_defined reference
138 | Lambda lambda -> Lambda.to_value_expression ~loc lambda
139 ;;
140
141 let apply t ~loc e =
142 match t with
143 | Reference reference -> Reference.apply ~loc reference e
144 | Lambda lambda -> Lambda.apply ~loc lambda e
145 ;;
146
147 let bind t binds =
148 match t with
149 | Reference reference -> Reference (Reference.bind reference binds)
150 | Lambda lambda -> Lambda (Lambda.bind lambda binds)
151 ;;
152
153 let bind_types t types =
154 match t with
155 | Reference reference -> Reference (Reference.bind_types reference types)
156 | Lambda lambda -> Lambda (Lambda.bind_types lambda types)
157 ;;
158
159 module Apply_all = struct
160 type t =
161 { bindings : value_binding list
162 ; arguments : pattern list
163 ; converted : expression list
164 }
165 end
166
167 let gen_symbols list ~prefix =
168 List.mapi list ~f:(fun i _ -> gen_symbol ~prefix:(prefix ^ Int.to_string i) ())
169 ;;
170
171 let apply_all ts ~loc =
172 let arguments_names = gen_symbols ts ~prefix:"arg" in
173 let converted_names = gen_symbols ts ~prefix:"res" in
174 let bindings =
175 List.map3_exn ts arguments_names converted_names ~f:(fun t arg conv ->
176 let expr = apply ~loc t (evar ~loc arg) in
177 value_binding ~loc ~pat:(pvar ~loc conv) ~expr)
178 in
179 ({ bindings
180 ; arguments = List.map arguments_names ~f:(pvar ~loc)
181 ; converted = List.map converted_names ~f:(evar ~loc)
182 }
183 : Apply_all.t)
184 ;;