| 1 |
open! Base |
| 2 |
open! Ppxlib |
| 3 |
open Ast_builder.Default |
| 4 |
open Helpers |
| 5 |
open Lifted.Monad_infix |
| 6 |
|
| 7 |
|
| 8 |
module Sig_generate_sexp_of = struct |
| 9 |
let type_of_sexp_of ~loc t = |
| 10 |
let loc = { loc with loc_ghost = true } in |
| 11 |
[%type: [%t t] -> Sexplib0.Sexp.t] |
| 12 |
;; |
| 13 |
|
| 14 |
let mk_type td = combinator_type_of_type_declaration td ~f:type_of_sexp_of |
| 15 |
|
| 16 |
let mk_sig ~loc:_ ~path:_ (_rf, tds) = |
| 17 |
List.map tds ~f:(fun td -> |
| 18 |
let loc = td.ptype_loc in |
| 19 |
psig_value |
| 20 |
~loc |
| 21 |
(value_description |
| 22 |
~loc |
| 23 |
~name:(Located.map (( ^ ) "sexp_of_") td.ptype_name) |
| 24 |
~type_:(mk_type td) |
| 25 |
~prim:[])) |
| 26 |
;; |
| 27 |
|
| 28 |
let mk_sig_exn ~loc:_ ~path:_ _te = [] |
| 29 |
end |
| 30 |
|
| 31 |
module Str_generate_sexp_of = struct |
| 32 |
module Types_being_defined = struct |
| 33 |
type t = |
| 34 |
| Nonrec |
| 35 |
| Rec of Set.M(String).t |
| 36 |
|
| 37 |
let to_rec_flag = function |
| 38 |
| Nonrec -> Nonrecursive |
| 39 |
| Rec _ -> Recursive |
| 40 |
;; |
| 41 |
|
| 42 |
let to_values_being_defined = function |
| 43 |
| Nonrec -> Set.empty (module String) |
| 44 |
| Rec types -> Set.map (module String) types ~f:(fun s -> "sexp_of_" ^ s) |
| 45 |
;; |
| 46 |
end |
| 47 |
|
| 48 |
let sexp_of_type_constr ~loc id args = |
| 49 |
type_constr_conv ~loc id ~f:(fun s -> "sexp_of_" ^ s) args |
| 50 |
;; |
| 51 |
|
| 52 |
|
| 53 |
let rec sexp_of_type ~renaming typ : Conversion.t = |
| 54 |
let loc = { typ.ptyp_loc with loc_ghost = true } in |
| 55 |
match Ppxlib_jane.Jane_syntax.Core_type.of_ast typ with |
| 56 |
| Some (Jtyp_tuple alist, (_ : attributes)) -> |
| 57 |
Conversion.of_lambda [ sexp_of_labeled_tuple ~renaming ~loc alist ] |
| 58 |
| Some (Jtyp_layout _, _) | None -> |
| 59 |
(match typ with |
| 60 |
| _ when Option.is_some (Attribute.get Attrs.opaque typ) -> |
| 61 |
Conversion.of_reference_exn [%expr Sexplib0.Sexp_conv.sexp_of_opaque] |
| 62 |
| [%type: _] -> |
| 63 |
Conversion.of_lambda [ ppat_any ~loc --> [%expr Sexplib0.Sexp.Atom "_"] ] |
| 64 |
| [%type: [%t? _] sexp_opaque] -> |
| 65 |
Conversion.of_reference_exn [%expr Sexplib0.Sexp_conv.sexp_of_opaque] |
| 66 |
| { ptyp_desc = Ptyp_tuple tp; _ } -> |
| 67 |
Conversion.of_lambda [ sexp_of_tuple ~renaming (loc, tp) ] |
| 68 |
| { ptyp_desc = Ptyp_var parm; _ } -> |
| 69 |
(match Renaming.binding_kind renaming parm ~loc with |
| 70 |
| Universally_bound fresh -> |
| 71 |
Conversion.of_reference_exn (Fresh_name.expression fresh) |
| 72 |
| Existentially_bound -> sexp_of_type ~renaming [%type: _]) |
| 73 |
| { ptyp_desc = Ptyp_constr (id, args); _ } -> |
| 74 |
Conversion.of_reference_exn |
| 75 |
(sexp_of_type_constr |
| 76 |
~loc |
| 77 |
id |
| 78 |
(List.map args ~f:(fun tp -> |
| 79 |
Conversion.to_expression ~loc (sexp_of_type ~renaming tp)))) |
| 80 |
| { ptyp_desc = Ptyp_arrow (_, _, _); _ } -> |
| 81 |
Conversion.of_lambda |
| 82 |
[ ppat_any ~loc |
| 83 |
--> [%expr Sexplib0.Sexp_conv.sexp_of_fun Sexplib0.Sexp_conv.ignore] |
| 84 |
] |
| 85 |
| { ptyp_desc = Ptyp_variant (row_fields, Closed, _); _ } -> |
| 86 |
sexp_of_variant ~renaming (loc, row_fields) |
| 87 |
| { ptyp_desc = Ptyp_poly (parms, poly_tp); _ } -> |
| 88 |
sexp_of_poly ~renaming parms poly_tp |
| 89 |
| { ptyp_desc = Ptyp_variant (_, Open, _); _ } |
| 90 |
| { ptyp_desc = Ptyp_object (_, _); _ } |
| 91 |
| { ptyp_desc = Ptyp_class (_, _); _ } |
| 92 |
| { ptyp_desc = Ptyp_alias (_, _); _ } |
| 93 |
| { ptyp_desc = Ptyp_package _; _ } |
| 94 |
| { ptyp_desc = Ptyp_extension _; _ } |
| 95 |
| { ptyp_desc = Ptyp_open _; _ } -> |
| 96 |
Location.raise_errorf ~loc "Type unsupported for ppx [sexp_of] conversion") |
| 97 |
|
| 98 |
|
| 99 |
and sexp_of_tuple ~renaming (loc, tps) = |
| 100 |
let fps = List.map ~f:(fun tp -> sexp_of_type ~renaming tp) tps in |
| 101 |
let ({ bindings; arguments; converted } : Conversion.Apply_all.t) = |
| 102 |
Conversion.apply_all ~loc fps |
| 103 |
in |
| 104 |
let in_expr = [%expr Sexplib0.Sexp.List [%e elist ~loc converted]] in |
| 105 |
let expr = pexp_let ~loc Nonrecursive bindings in_expr in |
| 106 |
ppat_tuple ~loc arguments --> expr |
| 107 |
|
| 108 |
|
| 109 |
and sexp_of_labeled_tuple ~renaming ~loc alist = |
| 110 |
assert (Labeled_tuple.is_valid alist); |
| 111 |
let ({ bindings; arguments; converted } : Conversion.Apply_all.t) = |
| 112 |
List.map alist ~f:(fun (_, core_type) -> sexp_of_type ~renaming core_type) |
| 113 |
|> Conversion.apply_all ~loc |
| 114 |
in |
| 115 |
let expr = |
| 116 |
let sexp_exprs = |
| 117 |
|
| 118 |
List.map2_exn alist converted ~f:(fun (label_option, _) expr -> |
| 119 |
[%expr |
| 120 |
List |
| 121 |
[ Atom [%e estring ~loc (Labeled_tuple.atom_of_label label_option)] |
| 122 |
; [%e expr] |
| 123 |
]]) |
| 124 |
in |
| 125 |
[%expr Sexplib0.Sexp.List [%e elist ~loc sexp_exprs]] |
| 126 |
|> pexp_let ~loc Nonrecursive bindings |
| 127 |
in |
| 128 |
let pat = |
| 129 |
( List.map2_exn alist arguments ~f:(fun (label_option, _) arg -> label_option, arg) |
| 130 |
, Closed ) |
| 131 |
|> Ppxlib_jane.Jane_syntax.Labeled_tuples.pat_of ~loc |
| 132 |
in |
| 133 |
pat --> expr |
| 134 |
|
| 135 |
|
| 136 |
and sexp_of_variant ~renaming ((loc, row_fields) : Location.t * row_field list) |
| 137 |
: Conversion.t |
| 138 |
= |
| 139 |
let item row = |
| 140 |
match row.prf_desc with |
| 141 |
| Rtag ({ txt = cnstr; _ }, true, []) -> |
| 142 |
ppat_variant ~loc cnstr None |
| 143 |
--> [%expr Sexplib0.Sexp.Atom [%e estring ~loc cnstr]] |
| 144 |
| Rtag ({ txt = cnstr; _ }, _, [ tp ]) |
| 145 |
when Option.is_some (Attribute.get Attrs.list_poly row) -> |
| 146 |
(match tp with |
| 147 |
| [%type: [%t? tp] list] -> |
| 148 |
let cnv_expr = Conversion.to_expression ~loc (sexp_of_type ~renaming tp) in |
| 149 |
let name = Fresh_name.create "l" ~loc in |
| 150 |
ppat_variant ~loc cnstr (Some (Fresh_name.pattern name)) |
| 151 |
--> [%expr |
| 152 |
Sexplib0.Sexp.List |
| 153 |
(Sexplib0.Sexp.Atom [%e estring ~loc cnstr] |
| 154 |
:: Sexplib0.Sexp_conv.list_map |
| 155 |
[%e cnv_expr] |
| 156 |
[%e Fresh_name.expression name])] |
| 157 |
| _ -> Attrs.invalid_attribute ~loc Attrs.list_poly "_ list") |
| 158 |
| Rtag ({ txt = cnstr; _ }, _, [ [%type: [%t? tp] sexp_list] ]) -> |
| 159 |
let cnv_expr = Conversion.to_expression ~loc (sexp_of_type ~renaming tp) in |
| 160 |
let name = Fresh_name.create "l" ~loc in |
| 161 |
ppat_variant ~loc cnstr (Some (Fresh_name.pattern name)) |
| 162 |
--> [%expr |
| 163 |
Sexplib0.Sexp.List |
| 164 |
(Sexplib0.Sexp.Atom [%e estring ~loc cnstr] |
| 165 |
:: Sexplib0.Sexp_conv.list_map |
| 166 |
[%e cnv_expr] |
| 167 |
[%e Fresh_name.expression name])] |
| 168 |
| Rtag ({ txt = cnstr; _ }, false, [ tp ]) -> |
| 169 |
let cnstr_expr = [%expr Sexplib0.Sexp.Atom [%e estring ~loc cnstr]] in |
| 170 |
let fresh = Fresh_name.create "v" ~loc in |
| 171 |
let cnstr_arg = |
| 172 |
Conversion.apply ~loc (sexp_of_type ~renaming tp) (Fresh_name.expression fresh) |
| 173 |
in |
| 174 |
let expr = [%expr Sexplib0.Sexp.List [%e elist ~loc [ cnstr_expr; cnstr_arg ]]] in |
| 175 |
ppat_variant ~loc cnstr (Some (Fresh_name.pattern fresh)) --> expr |
| 176 |
| Rinherit { ptyp_desc = Ptyp_constr (id, []); _ } -> |
| 177 |
let name = Fresh_name.create "v" ~loc in |
| 178 |
ppat_alias ~loc (ppat_type ~loc id) (Fresh_name.to_string_loc name) |
| 179 |
--> sexp_of_type_constr ~loc id [ Fresh_name.expression name ] |
| 180 |
| Rtag (_, true, [ _ ]) | Rtag (_, _, _ :: _ :: _) -> |
| 181 |
Location.raise_errorf ~loc "unsupported: polymorphic variant intersection type" |
| 182 |
| Rinherit ({ ptyp_desc = Ptyp_constr (id, _ :: _); _ } as typ) -> |
| 183 |
let call = Conversion.to_expression ~loc (sexp_of_type ~renaming typ) in |
| 184 |
let name = Fresh_name.create "v" ~loc in |
| 185 |
ppat_alias ~loc (ppat_type ~loc id) (Fresh_name.to_string_loc name) |
| 186 |
--> [%expr [%e call] [%e Fresh_name.expression name]] |
| 187 |
| Rinherit _ -> |
| 188 |
Location.raise_errorf |
| 189 |
~loc |
| 190 |
"unsupported: polymorphic variant with invalid (non-identifier) inherited type" |
| 191 |
| Rtag (_, false, []) -> |
| 192 |
Location.raise_errorf ~loc "unsupported: polymorphic variant empty type" |
| 193 |
in |
| 194 |
Conversion.of_lambda (List.map ~f:item row_fields) |
| 195 |
|
| 196 |
|
| 197 |
and sexp_of_poly ~renaming parms tp = |
| 198 |
let loc = tp.ptyp_loc in |
| 199 |
let renaming = |
| 200 |
List.fold_left |
| 201 |
parms |
| 202 |
~init:renaming |
| 203 |
~f:(Renaming.add_universally_bound ~prefix:"_of_") |
| 204 |
in |
| 205 |
let bindings = |
| 206 |
let mk_binding parm = |
| 207 |
let name = |
| 208 |
match Renaming.binding_kind renaming parm.txt ~loc:parm.loc with |
| 209 |
| Universally_bound name -> name |
| 210 |
| Existentially_bound -> assert false |
| 211 |
in |
| 212 |
value_binding |
| 213 |
~loc |
| 214 |
~pat:(Fresh_name.pattern name) |
| 215 |
~expr:[%expr Sexplib0.Sexp_conv.sexp_of_opaque] |
| 216 |
in |
| 217 |
List.map ~f:mk_binding parms |
| 218 |
in |
| 219 |
Conversion.bind (sexp_of_type ~renaming tp) bindings |
| 220 |
;; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
let mk_rec_patt loc patt name fresh = |
| 225 |
let p = Loc.make (Longident.Lident name) ~loc, Fresh_name.pattern fresh in |
| 226 |
patt @ [ p ] |
| 227 |
;; |
| 228 |
|
| 229 |
type is_empty_expr = |
| 230 |
| Inspect_value of (location -> expression -> expression) |
| 231 |
| Inspect_sexp of (cnv_expr:expression -> location -> expression -> expression) |
| 232 |
|
| 233 |
let sexp_of_record_field ~renaming ~bnds patt expr name tp ?sexp_of is_empty_expr = |
| 234 |
let loc = tp.ptyp_loc in |
| 235 |
let fresh = Fresh_name.create name ~loc in |
| 236 |
let patt = mk_rec_patt loc patt name fresh in |
| 237 |
let cnv_expr = Conversion.to_expression ~loc (sexp_of_type ~renaming tp) in |
| 238 |
let cnv_expr = |
| 239 |
match sexp_of with |
| 240 |
| None -> cnv_expr |
| 241 |
| Some sexp_of -> [%expr [%e sexp_of] [%e cnv_expr]] |
| 242 |
in |
| 243 |
let bnd = Fresh_name.create "bnd" ~loc in |
| 244 |
let arg = Fresh_name.create "arg" ~loc in |
| 245 |
let expr = |
| 246 |
[%expr |
| 247 |
let [%p Fresh_name.pattern bnds] = |
| 248 |
[%e |
| 249 |
match is_empty_expr with |
| 250 |
| Inspect_value is_empty_expr -> |
| 251 |
[%expr |
| 252 |
if [%e is_empty_expr loc (Fresh_name.expression fresh)] |
| 253 |
then [%e Fresh_name.expression bnds] |
| 254 |
else ( |
| 255 |
let [%p Fresh_name.pattern arg] = |
| 256 |
[%e cnv_expr] [%e Fresh_name.expression fresh] |
| 257 |
in |
| 258 |
let [%p Fresh_name.pattern bnd] = |
| 259 |
Sexplib0.Sexp.List |
| 260 |
[ Sexplib0.Sexp.Atom [%e estring ~loc name] |
| 261 |
; [%e Fresh_name.expression arg] |
| 262 |
] |
| 263 |
in |
| 264 |
([%e Fresh_name.expression bnd] :: [%e Fresh_name.expression bnds] |
| 265 |
: _ Stdlib.List.t))] |
| 266 |
| Inspect_sexp is_empty_expr -> |
| 267 |
[%expr |
| 268 |
let [%p Fresh_name.pattern arg] = |
| 269 |
[%e cnv_expr] [%e Fresh_name.expression fresh] |
| 270 |
in |
| 271 |
if [%e is_empty_expr ~cnv_expr loc (Fresh_name.expression arg)] |
| 272 |
then [%e Fresh_name.expression bnds] |
| 273 |
else ( |
| 274 |
let [%p Fresh_name.pattern bnd] = |
| 275 |
Sexplib0.Sexp.List |
| 276 |
[ Sexplib0.Sexp.Atom [%e estring ~loc name] |
| 277 |
; [%e Fresh_name.expression arg] |
| 278 |
] |
| 279 |
in |
| 280 |
([%e Fresh_name.expression bnd] :: [%e Fresh_name.expression bnds] |
| 281 |
: _ Stdlib.List.t))]] |
| 282 |
in |
| 283 |
[%e expr]] |
| 284 |
in |
| 285 |
patt, expr |
| 286 |
;; |
| 287 |
|
| 288 |
let disallow_type_variables_and_recursive_occurrences |
| 289 |
~types_being_defined |
| 290 |
~loc |
| 291 |
~attr_name |
| 292 |
tp |
| 293 |
= |
| 294 |
let disallow_variables = |
| 295 |
let iter = |
| 296 |
object |
| 297 |
inherit Ast_traverse.iter as super |
| 298 |
|
| 299 |
method! core_type_desc = |
| 300 |
function |
| 301 |
| Ptyp_var v -> |
| 302 |
Location.raise_errorf |
| 303 |
~loc |
| 304 |
"[@%s] was used, but the type of the field contains a type variable: '%s.\n\ |
| 305 |
Comparison is not avaiable for type variables.\n\ |
| 306 |
Consider using [@sexp_drop_if _] or [@sexp_drop_default.sexp] instead." |
| 307 |
attr_name |
| 308 |
v |
| 309 |
| t -> super#core_type_desc t |
| 310 |
end |
| 311 |
in |
| 312 |
iter#core_type |
| 313 |
in |
| 314 |
let disallow_recursive_occurrences = |
| 315 |
match (types_being_defined : Types_being_defined.t) with |
| 316 |
| Nonrec -> fun _ -> () |
| 317 |
| Rec types_being_defined -> |
| 318 |
let iter = |
| 319 |
object |
| 320 |
inherit Ast_traverse.iter as super |
| 321 |
|
| 322 |
method! core_type_desc = |
| 323 |
function |
| 324 |
| Ptyp_constr ({ loc = _; txt = Lident s }, _) as t -> |
| 325 |
if Set.mem types_being_defined s |
| 326 |
then |
| 327 |
Location.raise_errorf |
| 328 |
~loc |
| 329 |
"[@%s] was used, but the type of the field contains a type defined \ |
| 330 |
in the current recursive block: %s.\n\ |
| 331 |
This is not supported.\n\ |
| 332 |
Consider using [@sexp_drop_if _] or [@sexp_drop_default.sexp] \ |
| 333 |
instead." |
| 334 |
attr_name |
| 335 |
s; |
| 336 |
super#core_type_desc t |
| 337 |
| t -> super#core_type_desc t |
| 338 |
end |
| 339 |
in |
| 340 |
iter#core_type |
| 341 |
in |
| 342 |
disallow_variables tp; |
| 343 |
disallow_recursive_occurrences tp |
| 344 |
;; |
| 345 |
|
| 346 |
let sexp_of_default_field |
| 347 |
~types_being_defined |
| 348 |
how |
| 349 |
~renaming |
| 350 |
~bnds |
| 351 |
patt |
| 352 |
expr |
| 353 |
name |
| 354 |
tp |
| 355 |
?sexp_of |
| 356 |
default |
| 357 |
= |
| 358 |
let is_empty = |
| 359 |
let inspect_value equality_f = |
| 360 |
Inspect_value (fun loc expr -> [%expr [%e equality_f loc] [%e default] [%e expr]]) |
| 361 |
in |
| 362 |
match (how : Record_field_attrs.Sexp_of.Drop.t) with |
| 363 |
| Sexp -> |
| 364 |
Inspect_sexp |
| 365 |
(fun ~cnv_expr loc sexp_expr -> |
| 366 |
[%expr Sexplib0.Sexp_conv.( = ) ([%e cnv_expr] [%e default]) [%e sexp_expr]]) |
| 367 |
|> Lifted.return |
| 368 |
| No_arg -> |
| 369 |
inspect_value (fun loc -> |
| 370 |
[%expr |
| 371 |
Sexplib0.Sexp_conv.( = ) [@ocaml.ppwarning |
| 372 |
"[@sexp_drop_default] is deprecated: please use \ |
| 373 |
one of:\n\ |
| 374 |
- [@sexp_drop_default f] and give an explicit \ |
| 375 |
equality function ([f = Poly.(=)] corresponds to \ |
| 376 |
the old behavior)\n\ |
| 377 |
- [@sexp_drop_default.compare] if the type \ |
| 378 |
supports [%compare]\n\ |
| 379 |
- [@sexp_drop_default.equal] if the type \ |
| 380 |
supports [%equal]\n\ |
| 381 |
- [@sexp_drop_default.sexp] if you want to \ |
| 382 |
compare the sexp representations\n"]]) |
| 383 |
|> Lifted.return |
| 384 |
| Func lifted -> lifted >>| fun f -> inspect_value (fun _ -> f) |
| 385 |
| Compare -> |
| 386 |
inspect_value (fun loc -> |
| 387 |
disallow_type_variables_and_recursive_occurrences |
| 388 |
~types_being_defined |
| 389 |
~attr_name:"sexp_drop_default.compare" |
| 390 |
~loc |
| 391 |
tp; |
| 392 |
[%expr [%compare.equal: [%t tp]]]) |
| 393 |
|> Lifted.return |
| 394 |
| Equal -> |
| 395 |
inspect_value (fun loc -> |
| 396 |
disallow_type_variables_and_recursive_occurrences |
| 397 |
~types_being_defined |
| 398 |
~attr_name:"sexp_drop_default.equal" |
| 399 |
~loc |
| 400 |
tp; |
| 401 |
[%expr [%equal: [%t tp]]]) |
| 402 |
|> Lifted.return |
| 403 |
in |
| 404 |
is_empty >>| sexp_of_record_field ~renaming ~bnds patt expr name tp ?sexp_of |
| 405 |
;; |
| 406 |
|
| 407 |
let sexp_of_label_declaration_list ~types_being_defined ~renaming loc flds ~wrap_expr = |
| 408 |
let bnds = Fresh_name.create "bnds" ~loc in |
| 409 |
let list_empty_expr = |
| 410 |
Inspect_value |
| 411 |
(fun loc lst -> |
| 412 |
[%expr |
| 413 |
match [%e lst] with |
| 414 |
| [] -> true |
| 415 |
| _ -> false]) |
| 416 |
in |
| 417 |
let array_empty_expr = |
| 418 |
Inspect_value |
| 419 |
(fun loc arr -> |
| 420 |
[%expr |
| 421 |
match [%e arr] with |
| 422 |
| [||] -> true |
| 423 |
| _ -> false]) |
| 424 |
in |
| 425 |
let coll lifted ld = |
| 426 |
lifted |
| 427 |
>>= fun ((patt : (Longident.t loc * pattern) list), expr) -> |
| 428 |
let name = ld.pld_name.txt in |
| 429 |
let loc = ld.pld_name.loc in |
| 430 |
let fresh = Fresh_name.create name ~loc in |
| 431 |
match Record_field_attrs.Sexp_of.create ~loc ld with |
| 432 |
| Sexp_option tp -> |
| 433 |
let v = Fresh_name.create "v" ~loc in |
| 434 |
let bnd = Fresh_name.create "bnd" ~loc in |
| 435 |
let arg = Fresh_name.create "arg" ~loc in |
| 436 |
let patt = mk_rec_patt loc patt name fresh in |
| 437 |
let vname = Fresh_name.expression v in |
| 438 |
let cnv_expr = Conversion.apply ~loc (sexp_of_type ~renaming tp) vname in |
| 439 |
let expr = |
| 440 |
[%expr |
| 441 |
let [%p Fresh_name.pattern bnds] = |
| 442 |
match [%e Fresh_name.expression fresh] with |
| 443 |
| Stdlib.Option.None -> [%e Fresh_name.expression bnds] |
| 444 |
| Stdlib.Option.Some [%p Fresh_name.pattern v] -> |
| 445 |
let [%p Fresh_name.pattern arg] = [%e cnv_expr] in |
| 446 |
let [%p Fresh_name.pattern bnd] = |
| 447 |
Sexplib0.Sexp.List |
| 448 |
[ Sexplib0.Sexp.Atom [%e estring ~loc name] |
| 449 |
; [%e Fresh_name.expression arg] |
| 450 |
] |
| 451 |
in |
| 452 |
([%e Fresh_name.expression bnd] :: [%e Fresh_name.expression bnds] |
| 453 |
: _ Stdlib.List.t) |
| 454 |
in |
| 455 |
[%e expr]] |
| 456 |
in |
| 457 |
Lifted.return (patt, expr) |
| 458 |
| Sexp_bool -> |
| 459 |
let patt = mk_rec_patt loc patt name fresh in |
| 460 |
let bnd = Fresh_name.create "bnd" ~loc in |
| 461 |
let expr = |
| 462 |
[%expr |
| 463 |
let [%p Fresh_name.pattern bnds] = |
| 464 |
if [%e Fresh_name.expression fresh] |
| 465 |
then ( |
| 466 |
let [%p Fresh_name.pattern bnd] = |
| 467 |
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom [%e estring ~loc name] ] |
| 468 |
in |
| 469 |
([%e Fresh_name.expression bnd] :: [%e Fresh_name.expression bnds] |
| 470 |
: _ Stdlib.List.t)) |
| 471 |
else [%e Fresh_name.expression bnds] |
| 472 |
in |
| 473 |
[%e expr]] |
| 474 |
in |
| 475 |
Lifted.return (patt, expr) |
| 476 |
| Sexp_list tp -> |
| 477 |
sexp_of_record_field |
| 478 |
~renaming |
| 479 |
~bnds |
| 480 |
patt |
| 481 |
expr |
| 482 |
name |
| 483 |
tp |
| 484 |
~sexp_of: |
| 485 |
|
| 486 |
[%expr sexp_of_list] |
| 487 |
list_empty_expr |
| 488 |
|> Lifted.return |
| 489 |
| Sexp_array tp -> |
| 490 |
sexp_of_record_field |
| 491 |
~renaming |
| 492 |
~bnds |
| 493 |
patt |
| 494 |
expr |
| 495 |
name |
| 496 |
tp |
| 497 |
~sexp_of: |
| 498 |
|
| 499 |
[%expr sexp_of_array] |
| 500 |
array_empty_expr |
| 501 |
|> Lifted.return |
| 502 |
| Specific (Drop_default how) -> |
| 503 |
let tp = ld.pld_type in |
| 504 |
(match Attribute.get Attrs.default ld with |
| 505 |
| None -> Location.raise_errorf ~loc "no default to drop" |
| 506 |
| Some { to_lift = default } -> |
| 507 |
Record_field_attrs.lift_default ~loc ld default |
| 508 |
>>= sexp_of_default_field |
| 509 |
~types_being_defined |
| 510 |
how |
| 511 |
~renaming |
| 512 |
~bnds |
| 513 |
patt |
| 514 |
expr |
| 515 |
name |
| 516 |
tp) |
| 517 |
| Specific (Drop_if test) -> |
| 518 |
test |
| 519 |
>>| fun test -> |
| 520 |
let tp = ld.pld_type in |
| 521 |
sexp_of_record_field |
| 522 |
~renaming |
| 523 |
~bnds |
| 524 |
patt |
| 525 |
expr |
| 526 |
name |
| 527 |
tp |
| 528 |
(Inspect_value (fun loc expr -> [%expr [%e test] [%e expr]])) |
| 529 |
| Omit_nil -> |
| 530 |
let tp = ld.pld_type in |
| 531 |
let patt = mk_rec_patt loc patt name fresh in |
| 532 |
let vname = Fresh_name.expression fresh in |
| 533 |
let arg = Fresh_name.create "arg" ~loc in |
| 534 |
let cnv_expr = Conversion.apply ~loc (sexp_of_type ~renaming tp) vname in |
| 535 |
let bnds_expr = |
| 536 |
[%expr |
| 537 |
match [%e cnv_expr] with |
| 538 |
| Sexplib0.Sexp.List [] -> [%e Fresh_name.expression bnds] |
| 539 |
| [%p Fresh_name.pattern arg] -> |
| 540 |
(Sexplib0.Sexp.List |
| 541 |
[ Sexplib0.Sexp.Atom [%e estring ~loc name] |
| 542 |
; [%e Fresh_name.expression arg] |
| 543 |
] |
| 544 |
:: [%e Fresh_name.expression bnds] |
| 545 |
: _ Stdlib.List.t)] |
| 546 |
in |
| 547 |
( patt |
| 548 |
, [%expr |
| 549 |
let [%p Fresh_name.pattern bnds] = [%e bnds_expr] in |
| 550 |
[%e expr]] ) |
| 551 |
|> Lifted.return |
| 552 |
| Specific Keep -> |
| 553 |
let tp = ld.pld_type in |
| 554 |
let patt = mk_rec_patt loc patt name fresh in |
| 555 |
let vname = Fresh_name.expression fresh in |
| 556 |
let arg = Fresh_name.create "arg" ~loc in |
| 557 |
let cnv_expr = Conversion.apply ~loc (sexp_of_type ~renaming tp) vname in |
| 558 |
let bnds_expr = |
| 559 |
[%expr |
| 560 |
let [%p Fresh_name.pattern arg] = [%e cnv_expr] in |
| 561 |
(Sexplib0.Sexp.List |
| 562 |
[ Sexplib0.Sexp.Atom [%e estring ~loc name] |
| 563 |
; [%e Fresh_name.expression arg] |
| 564 |
] |
| 565 |
:: [%e Fresh_name.expression bnds] |
| 566 |
: _ Stdlib.List.t)] |
| 567 |
in |
| 568 |
( patt |
| 569 |
, [%expr |
| 570 |
let [%p Fresh_name.pattern bnds] = [%e bnds_expr] in |
| 571 |
[%e expr]] ) |
| 572 |
|> Lifted.return |
| 573 |
in |
| 574 |
let init_expr = wrap_expr (Fresh_name.expression bnds) in |
| 575 |
List.fold_left ~f:coll ~init:(Lifted.return ([], init_expr)) flds |
| 576 |
>>| fun (patt, expr) -> |
| 577 |
( ppat_record ~loc patt Closed |
| 578 |
, [%expr |
| 579 |
let [%p Fresh_name.pattern bnds] = ([] : _ Stdlib.List.t) in |
| 580 |
[%e expr]] ) |
| 581 |
;; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
let branch_sum |
| 586 |
row |
| 587 |
inline_attr |
| 588 |
~types_being_defined |
| 589 |
renaming |
| 590 |
~loc |
| 591 |
constr_lid |
| 592 |
constr_str |
| 593 |
args |
| 594 |
= |
| 595 |
match args with |
| 596 |
| Pcstr_record lds -> |
| 597 |
let cnstr_expr = [%expr Sexplib0.Sexp.Atom [%e constr_str]] in |
| 598 |
sexp_of_label_declaration_list |
| 599 |
~types_being_defined |
| 600 |
~renaming |
| 601 |
loc |
| 602 |
lds |
| 603 |
~wrap_expr:(fun expr -> [%expr Sexplib0.Sexp.List ([%e cnstr_expr] :: [%e expr])]) |
| 604 |
>>| fun (patt, expr) -> ppat_construct ~loc constr_lid (Some patt) --> expr |
| 605 |
| Pcstr_tuple pcd_args -> |
| 606 |
(match pcd_args with |
| 607 |
| [] -> |
| 608 |
ppat_construct ~loc constr_lid None |
| 609 |
--> [%expr Sexplib0.Sexp.Atom [%e constr_str]] |
| 610 |
|> Lifted.return |
| 611 |
| args -> |
| 612 |
(match args with |
| 613 |
| [ tp ] when Option.is_some (Attribute.get inline_attr row) -> |
| 614 |
(match tp with |
| 615 |
| [%type: [%t? tp] list] -> |
| 616 |
let cnv_expr = Conversion.to_expression ~loc (sexp_of_type ~renaming tp) in |
| 617 |
let name = Fresh_name.create "l" ~loc in |
| 618 |
ppat_construct ~loc constr_lid (Some (Fresh_name.pattern name)) |
| 619 |
--> [%expr |
| 620 |
Sexplib0.Sexp.List |
| 621 |
(Sexplib0.Sexp.Atom [%e constr_str] |
| 622 |
:: Sexplib0.Sexp_conv.list_map |
| 623 |
[%e cnv_expr] |
| 624 |
[%e Fresh_name.expression name])] |
| 625 |
| _ -> Attrs.invalid_attribute ~loc inline_attr "_ list") |
| 626 |
| [ [%type: [%t? tp] sexp_list] ] -> |
| 627 |
let cnv_expr = Conversion.to_expression ~loc (sexp_of_type ~renaming tp) in |
| 628 |
let name = Fresh_name.create "l" ~loc in |
| 629 |
ppat_construct ~loc constr_lid (Some (Fresh_name.pattern name)) |
| 630 |
--> [%expr |
| 631 |
Sexplib0.Sexp.List |
| 632 |
(Sexplib0.Sexp.Atom [%e constr_str] |
| 633 |
:: Sexplib0.Sexp_conv.list_map |
| 634 |
[%e cnv_expr] |
| 635 |
[%e Fresh_name.expression name])] |
| 636 |
| _ -> |
| 637 |
let sexp_of_args = List.map ~f:(sexp_of_type ~renaming) args in |
| 638 |
let cnstr_expr = [%expr Sexplib0.Sexp.Atom [%e constr_str]] in |
| 639 |
let ({ bindings; arguments; converted } : Conversion.Apply_all.t) = |
| 640 |
Conversion.apply_all ~loc sexp_of_args |
| 641 |
in |
| 642 |
let patt = |
| 643 |
match arguments with |
| 644 |
| [ arg ] -> arg |
| 645 |
| _ -> ppat_tuple ~loc arguments |
| 646 |
in |
| 647 |
ppat_construct ~loc constr_lid (Some patt) |
| 648 |
--> pexp_let |
| 649 |
~loc |
| 650 |
Nonrecursive |
| 651 |
bindings |
| 652 |
[%expr Sexplib0.Sexp.List [%e elist ~loc (cnstr_expr :: converted)]]) |
| 653 |
|> Lifted.return) |
| 654 |
;; |
| 655 |
|
| 656 |
let sexp_of_sum ~types_being_defined ~renaming tps cds = |
| 657 |
List.map cds ~f:(fun cd -> |
| 658 |
let renaming = |
| 659 |
Renaming.with_constructor_declaration renaming ~type_parameters:tps cd |
| 660 |
in |
| 661 |
let constr_lid = Located.map lident cd.pcd_name in |
| 662 |
let constr_str = estring ~loc:cd.pcd_name.loc cd.pcd_name.txt in |
| 663 |
branch_sum |
| 664 |
cd |
| 665 |
Attrs.list_variant |
| 666 |
~types_being_defined |
| 667 |
renaming |
| 668 |
~loc:cd.pcd_loc |
| 669 |
constr_lid |
| 670 |
constr_str |
| 671 |
cd.pcd_args) |
| 672 |
|> Lifted.all |
| 673 |
>>| Conversion.of_lambda |
| 674 |
;; |
| 675 |
|
| 676 |
|
| 677 |
let sexp_of_nil loc = Conversion.of_lambda [ ppat_any ~loc --> [%expr assert false] ] |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
let sexp_of_td ~types_being_defined td = |
| 682 |
let td = name_type_params_in_td td in |
| 683 |
let tps = List.map td.ptype_params ~f:get_type_param_name in |
| 684 |
let { ptype_name = { txt = type_name; loc = _ }; ptype_loc = loc; _ } = td in |
| 685 |
let renaming = Renaming.of_type_declaration td ~prefix:"_of_" in |
| 686 |
let body = |
| 687 |
let body = |
| 688 |
match td.ptype_kind with |
| 689 |
| Ptype_variant cds -> |
| 690 |
sexp_of_sum |
| 691 |
~renaming |
| 692 |
~types_being_defined |
| 693 |
(List.map tps ~f:(fun x -> x.txt)) |
| 694 |
cds |
| 695 |
| Ptype_record lds -> |
| 696 |
sexp_of_label_declaration_list |
| 697 |
~renaming |
| 698 |
loc |
| 699 |
lds |
| 700 |
~types_being_defined |
| 701 |
~wrap_expr:(fun expr -> [%expr Sexplib0.Sexp.List [%e expr]]) |
| 702 |
>>| fun (patt, expr) -> Conversion.of_lambda [ patt --> expr ] |
| 703 |
| Ptype_open -> |
| 704 |
Location.raise_errorf ~loc "ppx_sexp_conv: open types not supported" |
| 705 |
| Ptype_abstract -> |
| 706 |
(match td.ptype_manifest with |
| 707 |
| None -> sexp_of_nil loc |
| 708 |
| Some ty -> sexp_of_type ~renaming ty) |
| 709 |
|> Lifted.return |
| 710 |
in |
| 711 |
body |
| 712 |
>>| fun body -> |
| 713 |
let is_private_alias = |
| 714 |
match td.ptype_kind, td.ptype_manifest, td.ptype_private with |
| 715 |
| Ptype_abstract, Some _, Private -> true |
| 716 |
| _ -> false |
| 717 |
in |
| 718 |
if is_private_alias |
| 719 |
then ( |
| 720 |
|
| 721 |
let ty_src = |
| 722 |
core_type_of_type_declaration td |> replace_variables_by_underscores |
| 723 |
in |
| 724 |
let manifest = |
| 725 |
match td.ptype_manifest with |
| 726 |
| Some manifest -> manifest |
| 727 |
| None -> Location.raise_errorf ~loc "sexp_of_td/no-manifest" |
| 728 |
in |
| 729 |
let ty_dst = replace_variables_by_underscores manifest in |
| 730 |
let v = Fresh_name.create "v" ~loc in |
| 731 |
let coercion = |
| 732 |
[%expr ([%e Fresh_name.expression v] : [%t ty_src] :> [%t ty_dst])] |
| 733 |
in |
| 734 |
[%expr fun [%p Fresh_name.pattern v] -> [%e Conversion.apply ~loc body coercion]]) |
| 735 |
else |
| 736 |
|
| 737 |
|
| 738 |
Conversion.to_value_expression |
| 739 |
~loc |
| 740 |
~rec_flag:(Types_being_defined.to_rec_flag types_being_defined) |
| 741 |
~values_being_defined: |
| 742 |
(Types_being_defined.to_values_being_defined types_being_defined) |
| 743 |
body |
| 744 |
in |
| 745 |
let typ = Sig_generate_sexp_of.mk_type td in |
| 746 |
let func_name = "sexp_of_" ^ type_name in |
| 747 |
let body = |
| 748 |
body |
| 749 |
>>| fun body -> |
| 750 |
let patts = |
| 751 |
List.map tps ~f:(fun id -> |
| 752 |
match Renaming.binding_kind renaming id.txt ~loc:id.loc with |
| 753 |
| Universally_bound name -> Fresh_name.pattern name |
| 754 |
| Existentially_bound -> assert false) |
| 755 |
in |
| 756 |
let rec_flag = Types_being_defined.to_rec_flag types_being_defined in |
| 757 |
eta_reduce_if_possible_and_nonrec ~rec_flag (eabstract ~loc patts body) |
| 758 |
in |
| 759 |
let body = Lifted.let_bind_user_expressions ~loc body in |
| 760 |
constrained_function_binding loc td typ ~tps ~func_name body |
| 761 |
;; |
| 762 |
|
| 763 |
let sexp_of_tds ~loc ~path:_ (rec_flag, tds) = |
| 764 |
let rec_flag = really_recursive_respecting_opaque rec_flag tds in |
| 765 |
let (types_being_defined : Types_being_defined.t) = |
| 766 |
match rec_flag with |
| 767 |
| Nonrecursive -> Nonrec |
| 768 |
| Recursive -> |
| 769 |
Rec (Set.of_list (module String) (List.map tds ~f:(fun td -> td.ptype_name.txt))) |
| 770 |
in |
| 771 |
let bindings = List.map tds ~f:(sexp_of_td ~types_being_defined) in |
| 772 |
pstr_value_list ~loc rec_flag bindings |
| 773 |
;; |
| 774 |
|
| 775 |
let sexp_of_exn ~loc:_ ~path ec = |
| 776 |
let renaming = Renaming.without_type () in |
| 777 |
let get_full_cnstr str = path ^ "." ^ str in |
| 778 |
let loc = ec.ptyexn_loc in |
| 779 |
let expr = |
| 780 |
match ec.ptyexn_constructor with |
| 781 |
| { pext_name = cnstr |
| 782 |
; pext_kind = Pext_decl (_, extension_constructor_kind, None) |
| 783 |
; _ |
| 784 |
} -> |
| 785 |
let constr_lid = Located.map lident cnstr in |
| 786 |
branch_sum |
| 787 |
ec |
| 788 |
Attrs.list_exception |
| 789 |
~types_being_defined:Nonrec |
| 790 |
renaming |
| 791 |
~loc |
| 792 |
constr_lid |
| 793 |
(estring ~loc (get_full_cnstr cnstr.txt)) |
| 794 |
extension_constructor_kind |
| 795 |
>>| fun converter -> |
| 796 |
let assert_false = ppat_any ~loc --> [%expr assert false] in |
| 797 |
[%expr |
| 798 |
Sexplib0.Sexp_conv.Exn_converter.add |
| 799 |
[%extension_constructor [%e pexp_construct ~loc constr_lid None]] |
| 800 |
[%e |
| 801 |
Conversion.to_expression |
| 802 |
~loc |
| 803 |
(Conversion.of_lambda [ converter; assert_false ])]] |
| 804 |
| { pext_kind = Pext_decl (_, _, Some _); _ } -> |
| 805 |
Location.raise_errorf ~loc "sexp_of_exn/:" |
| 806 |
| { pext_kind = Pext_rebind _; _ } -> |
| 807 |
Location.raise_errorf ~loc "sexp_of_exn/rebind" |
| 808 |
in |
| 809 |
let expr = Lifted.let_bind_user_expressions ~loc expr in |
| 810 |
[ pstr_value ~loc Nonrecursive [ value_binding ~loc ~pat:[%pat? ()] ~expr ] ] |
| 811 |
;; |
| 812 |
|
| 813 |
let sexp_of_core_type core_type = |
| 814 |
let loc = { core_type.ptyp_loc with loc_ghost = true } in |
| 815 |
sexp_of_type ~renaming:(Renaming.without_type ()) core_type |
| 816 |
|> Conversion.to_value_expression |
| 817 |
~loc |
| 818 |
~rec_flag:Nonrecursive |
| 819 |
~values_being_defined:(Set.empty (module String)) |
| 820 |
|> Merlin_helpers.hide_expression |
| 821 |
;; |
| 822 |
end |