Debian Codegraph @

expander/ppx_sexp_message_expander.ml

raw symbols SCIP
1 open Base
2 open Ppxlib
3 open Ast_builder.Default
4
5 let omit_nil_attr =
6 Attribute.declare
7 "sexp_message.sexp.omit_nil"
8 Attribute.Context.core_type
9 Ast_pattern.(pstr nil)
10 ()
11 ;;
12
13 let option_attr =
14 Attribute.declare
15 "sexp_message.sexp.option"
16 Attribute.Context.core_type
17 Ast_pattern.(pstr nil)
18 ()
19 ;;
20
21 let sexp_atom ~loc x = [%expr Ppx_sexp_conv_lib.Sexp.Atom [%e x]]
22 let sexp_list ~loc x = [%expr Ppx_sexp_conv_lib.Sexp.List [%e x]]
23
24 let sexp_inline ~loc l =
25 match l with
26 | [ x ] -> x
27 | _ -> sexp_list ~loc (elist ~loc l)
28 ;;
29
30 (* Same as Ppx_sexp_value.omittable_sexp *)
31 type omittable_sexp =
32 | Present of expression
33 | Optional of Location.t * expression * (expression -> expression)
34 | Omit_nil of Location.t * expression * (expression -> expression)
35 | Absent
36
37 let present_or_omit_nil ~loc ~omit_nil expr =
38 if omit_nil then Omit_nil (loc, expr, Fn.id) else Present expr
39 ;;
40
41 let wrap_sexp_if_present omittable_sexp ~f =
42 match omittable_sexp with
43 | Present e -> Present (f e)
44 | Optional (loc, e, k) -> Optional (loc, e, fun e -> f (k e))
45 | Omit_nil (loc, e, k) -> Omit_nil (loc, e, fun e -> f (k e))
46 | Absent -> Absent
47 ;;
48
49 let sexp_of_constraint ~omit_nil ~loc expr ctyp =
50 let optional ty =
51 let sexp_of = Ppx_sexp_conv_expander.Sexp_of.core_type ty in
52 Optional (loc, expr, fun expr -> eapply ~loc sexp_of [ expr ])
53 in
54 match ctyp with
55 | [%type: [%t? ty] option] when Option.is_some (Attribute.get option_attr ctyp) ->
56 optional ty
57 | [%type: [%t? ty] option] when omit_nil -> optional ty
58 | _ ->
59 let expr =
60 let sexp_of = Ppx_sexp_conv_expander.Sexp_of.core_type ctyp in
61 eapply ~loc sexp_of [ expr ]
62 in
63 let omit_nil_attr =
64 lazy
65 (* this is lazy so using [@omit_nil] inside [%message.omit_nil] is an error (unused
66 attribute) *)
67 (match Attribute.get omit_nil_attr ctyp with
68 | Some () -> true
69 | None -> false)
70 in
71 present_or_omit_nil ~loc expr ~omit_nil:(omit_nil || Lazy.force omit_nil_attr)
72 ;;
73
74 let sexp_of_constant ~loc const =
75 let f typ =
76 eapply
77 ~loc
78 (evar ~loc ("Ppx_sexp_conv_lib.Conv.sexp_of_" ^ typ))
79 [ pexp_constant ~loc const ]
80 in
81 match const with
82 | Pconst_integer _ -> f "int"
83 | Pconst_char _ -> f "char"
84 | Pconst_string _ -> f "string"
85 | Pconst_float _ -> f "float"
86 ;;
87
88 let rewrite_here e =
89 match e.pexp_desc with
90 | Pexp_extension ({ txt = "here"; _ }, PStr []) ->
91 Ppx_here_expander.lift_position_as_string ~loc:e.pexp_loc
92 | _ -> e
93 ;;
94
95 let sexp_of_expr ~omit_nil e =
96 let e = rewrite_here e in
97 let loc = { e.pexp_loc with loc_ghost = true } in
98 match e.pexp_desc with
99 | Pexp_constant (Pconst_string ("", _, _)) -> Absent
100 | Pexp_constant const ->
101 present_or_omit_nil ~loc ~omit_nil:false (sexp_of_constant ~loc const)
102 | Pexp_constraint (expr, ctyp) -> sexp_of_constraint ~omit_nil ~loc expr ctyp
103 | _ ->
104 present_or_omit_nil
105 ~loc
106 ~omit_nil:false
107 [%expr Ppx_sexp_conv_lib.Conv.sexp_of_string [%e e]]
108 ;;
109
110 let sexp_of_labelled_expr ~omit_nil (label, e) =
111 let loc = { e.pexp_loc with loc_ghost = true } in
112 match label, e.pexp_desc with
113 | Nolabel, Pexp_constraint (expr, _) ->
114 let expr_str = Pprintast.string_of_expression expr in
115 let k e = sexp_inline ~loc [ sexp_atom ~loc (estring ~loc expr_str); e ] in
116 wrap_sexp_if_present (sexp_of_expr ~omit_nil e) ~f:k
117 | Nolabel, _ -> sexp_of_expr ~omit_nil e
118 | Labelled "_", _ -> sexp_of_expr ~omit_nil e
119 | Labelled label, _ ->
120 let k e = sexp_inline ~loc [ sexp_atom ~loc (estring ~loc label); e ] in
121 wrap_sexp_if_present (sexp_of_expr ~omit_nil e) ~f:k
122 | Optional _, _ ->
123 (* Could be used to encode sexp_option if that's ever needed. *)
124 Location.raise_errorf ~loc "ppx_sexp_value: optional argument not allowed here"
125 ;;
126
127 (* Wrap up the generated code in a [@cold] function so it doesn't pollute the call site.
128
129 Also, give that cold function a nice name so it's easy for assembly readers to figure
130 out why that function is being called. *)
131 let wrap_in_cold_function ~loc expr =
132 [%expr
133 let[@cold] ppx_sexp_message () = [%e expr] in
134 (* Prevent tail calls so the closure environment is always allocated locally (with
135 Jane Street extensions). The sexp this is generating is still allocated globally,
136 but it's nice to keep the closure allocation cheap where it's easy. *)
137 ppx_sexp_message () [@nontail]]
138 ;;
139
140 let sexp_of_labelled_exprs ~omit_nil ~loc labels_and_exprs =
141 let loc = { loc with loc_ghost = true } in
142 let l = List.map labels_and_exprs ~f:(sexp_of_labelled_expr ~omit_nil) in
143 let res =
144 List.fold_left (List.rev l) ~init:(elist ~loc []) ~f:(fun acc e ->
145 match e with
146 | Absent -> acc
147 | Present e -> [%expr [%e e] :: [%e acc]]
148 | Optional (_, v_opt, k) ->
149 (* We match simultaneously on the head and tail in the generated code to avoid
150 changing their respective typing environments. *)
151 [%expr
152 match [%e v_opt], [%e acc] with
153 | None, tl -> tl
154 | Some v, tl -> [%e k [%expr v]] :: tl]
155 | Omit_nil (_, e, k) ->
156 [%expr
157 match [%e e], [%e acc] with
158 | Ppx_sexp_conv_lib.Sexp.List [], tl -> tl
159 | v, tl -> [%e k [%expr v]] :: tl])
160 in
161 let has_optional_values =
162 List.exists l ~f:(function
163 | (Optional _ | Omit_nil _ : omittable_sexp) -> true
164 | Present _ | Absent -> false)
165 in
166 (* The two branches do the same thing, but when there are no optional values, we can do
167 it at compile-time, which avoids making the generated code ugly. *)
168 let final_expr =
169 if has_optional_values
170 then
171 [%expr
172 match [%e res] with
173 | [ h ] -> h
174 | ([] | _ :: _ :: _) as res -> [%e sexp_list ~loc [%expr res]]]
175 else (
176 match res with
177 | [%expr [ [%e? h] ]] -> h
178 | _ -> sexp_list ~loc res)
179 in
180 wrap_in_cold_function ~loc final_expr
181 ;;
182
183 let expand ~omit_nil ~path:_ e =
184 let loc = e.pexp_loc in
185 let labelled_exprs =
186 match e.pexp_desc with
187 | Pexp_apply (f, args) -> (Nolabel, f) :: args
188 | _ -> [ Nolabel, e ]
189 in
190 sexp_of_labelled_exprs ~omit_nil ~loc labelled_exprs
191 ;;
192
193 let expand_opt ~omit_nil ~loc ~path = function
194 | None ->
195 let loc = { loc with loc_ghost = true } in
196 wrap_in_cold_function ~loc (sexp_list ~loc (elist ~loc []))
197 | Some e -> expand ~omit_nil ~path e
198 ;;