package cconv-ppx

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ppx_deriving_cconv.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# 1 "ppx_deriving_cconv.cppo.ml"
(* Largely inspired from ppx_deriving_yojson *)

open Longident
open Location
open Asttypes
open Parsetree
module AH = Ast_helper
module AC = Ast_convenience

let deriver = "cconv"
let raise_errorf = Ppx_deriving.raise_errorf

let encode_prefix = `Prefix "encode"
let decode_prefix = `Prefix "decode"

let argn = Printf.sprintf "arg%d"

let attr_encoder attrs =
  Ppx_deriving.attr ~deriver "encoder" attrs |>
  Ppx_deriving.Arg.(get_attr ~deriver expr)

let attr_decoder attrs =
  Ppx_deriving.attr ~deriver "decoder" attrs |>
  Ppx_deriving.Arg.(get_attr ~deriver expr)

let attr_ignore attrs =
  Ppx_deriving.attr ~deriver "ignore" attrs |>
  Ppx_deriving.Arg.(get_flag ~deriver )

let attr_default attrs =
  Ppx_deriving.attr ~deriver "default" attrs |>
  Ppx_deriving.Arg.(get_attr ~deriver expr)

let attr_string name default attrs =
  match Ppx_deriving.attr ~deriver name attrs |>
        Ppx_deriving.Arg.(get_attr ~deriver string) with
  | Some x -> x
  | None   -> default

let attr_key  = attr_string "key"

(* fold right, with index of element *)
let fold_right_i f l acc =
  let rec fold' f acc i l = match l with
  | [] -> acc
  | x::tail ->
      let acc = fold' f acc (i+1) tail in
      f i x acc
  in
  fold' f acc 0 l


# 59 "ppx_deriving_cconv.cppo.ml"
let extract_pcd_args_tuple_values ~loc pcd_args =
  match pcd_args with
  | Pcstr_tuple l -> l
  | Pcstr_record _ ->
    (* When calling this method, the constructors have been checked
       already during pattern matching, but handle it just in case *)
    raise_errorf ~loc "%s cannot be derived for record variants" deriver

let contains_record_variant constrs =
  let is_record_variant constr =
    match constr.pcd_args with
    | Pcstr_record _ -> true
    | Pcstr_tuple _ -> false in
  List.exists is_record_variant constrs


# 76 "ppx_deriving_cconv.cppo.ml"
(* generate a [typ CConv.Encode.encoder] for the given [typ].
  @param self an option contains the type being defined, and a reference
    indicating whether a self-reference was used *)
let encode_of_typ ~self typ =
  let rec encode_of_typ typ = match attr_encoder typ.ptyp_attributes with
    | None -> encode_of_typ_rec typ
    | Some e -> e
  and encode_of_typ_rec typ = match typ with
  | [%type: unit]            -> [%expr CConv.Encode.unit]
  | [%type: int]             -> [%expr CConv.Encode.int]
  | [%type: float]           -> [%expr CConv.Encode.float]
  | [%type: bool]            -> [%expr CConv.Encode.bool]
  | [%type: string]          -> [%expr CConv.Encode.string]
  | [%type: bytes]           -> [%expr CConv.Encode.(map Bytes.to_string string)]
  | [%type: char]            -> [%expr CConv.Encode.char]
  | [%type: [%t? typ] ref]   -> [%expr CConv.Encode.(map (!) [%e encode_of_typ typ])]
  | [%type: [%t? typ] list]  -> [%expr CConv.Encode.(list [%e encode_of_typ typ])]
  | [%type: int32] | [%type: Int32.t] -> [%expr CConv.Encode.int32]
  | [%type: int64] | [%type: Int64.t] -> [%expr CConv.Encode.int64]
  | [%type: nativeint] | [%type: Nativeint.t] -> [%expr CConv.Encode.nativeint]
  | [%type: [%t? typ] array] ->
      [%expr CConv.Encode.(array [%e encode_of_typ typ])]
  | [%type: [%t? typ] option] ->
      [%expr CConv.Encode.(option [%e encode_of_typ typ])]
  | { ptyp_desc = Ptyp_constr ({ txt = lid }, args) } ->
      begin match self, lid with
      | Some (name, used), Lident liname when liname=name ->
          (* typ is actually a recursive reference to the type
            being defined. Use a "self" variables that will be bound
            with [CConv.Encode.record_fix] or [CConv.Encode.sum_fix] *)
          used := true;
          AC.evar "self"
      | _ ->
          AC.app
            (AH.Exp.ident (mknoloc (Ppx_deriving.mangle_lid encode_prefix lid)))
            (List.map encode_of_typ args)
      end
  | { ptyp_desc = Ptyp_tuple typs } ->
      (* encode tuple, by destructuring it *)
      [%expr
        CConv.Encode.tuple
          {CConv.Encode.tuple_emit=
             fun into [%p AC.ptuple (List.mapi (fun i _ -> AC.pvar (argn i)) typs)] ->
               [%e fold_right_i
                     (fun i typ acc ->
                        [%expr
                          [%e encode_of_typ typ].CConv.Encode.emit into
                            [%e AC.evar (argn i)] ::
                          [%e acc]
                        ]
                     ) typs [%expr []]
               ]
          }
      ]
  | { ptyp_desc = Ptyp_variant (fields, _, _); ptyp_loc } ->
      raise_errorf ~loc:ptyp_loc "%s cannot be derived for poly variants" deriver
  | { ptyp_desc = Ptyp_var name } ->
      [%expr ([%e AC.evar ("poly_"^name)] : 'a CConv.Encode.encoder)]
  | { ptyp_desc = Ptyp_alias (typ, name) } -> encode_of_typ typ
  | { ptyp_loc } ->
      raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s"
                   deriver (Ppx_deriving.string_of_core_type typ)
  in
  encode_of_typ typ

(* make an encoder from a type declaration *)
let encode_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
  let encoder =
    match type_decl.ptype_kind, type_decl.ptype_manifest with
    | Ptype_abstract, Some manifest -> encode_of_typ ~self:None manifest
    | Ptype_variant constrs, _ when contains_record_variant constrs ->
      raise_errorf ~loc "%s cannot be derived for record variants" deriver
    | Ptype_variant constrs, _ ->
        let self_used = ref false in
        let self = Some (type_decl.ptype_name.txt, self_used) in
        (* pattern matching *)
        let cases = List.map
          (fun { pcd_name = { txt = name' }; pcd_args; pcd_attributes } ->
            let pcd_args = extract_pcd_args_tuple_values ~loc pcd_args in
            (* first, encode arguments *)
            let args = fold_right_i
              (fun i typ acc ->
                let encoder = encode_of_typ ~self typ in
                [%expr
                  [%e encoder].CConv.Encode.emit into
                  [%e AC.evar (argn i)] ::
                  [%e acc]
                ]
              ) pcd_args [%expr []]
            in
            (* result is name,arguments *)
            let result = AC.tuple [AC.str name'; args] in
            (* the pattern case itself *)
            AH.Exp.case
              (AC.pconstr name' (List.mapi (fun i _ -> AC.pvar (argn i)) pcd_args))
              result
          ) constrs
        in
        let f = AH.Exp.function_ cases in
        let f = [%expr {CConv.Encode.sum_emit=fun into -> [%e f]}] in
        if !self_used
        then [%expr CConv.Encode.sum_fix (fun self -> [%e f])]
        else [%expr CConv.Encode.sum [%e f]]
    | Ptype_record labels, _ ->
        let self_used = ref false in
        let self = Some (type_decl.ptype_name.txt, self_used) in
        (* build the function  record->hlist  (here, its body). The record
            is named "r". *)
        let destruct = fold_right_i
          (fun i field tail ->
            if attr_ignore field.pld_attributes
            then tail (* do not encode *)
            else
              let encoder = encode_of_typ ~self field.pld_type in
              let field_name = attr_key field.pld_name.txt field.pld_attributes in
              [%expr
                ( [%e AC.str field_name],
                  [%e encoder].CConv.Encode.emit into
                    [%e AH.Exp.field [%expr r] (AC.lid field.pld_name.txt)]
                ) :: [%e tail]
              ]
          ) labels [%expr []]
        in
        let destruct = [%expr {CConv.Encode.record_emit=fun into r -> [%e destruct]}] in
        if !self_used
        then [%expr CConv.Encode.record_fix (fun self -> [%e destruct])]
        else [%expr CConv.Encode.record [%e destruct]]
    | Ptype_abstract, None ->
        raise_errorf ~loc "%s cannot be derived for fully abstract types" deriver
    | Ptype_open, _        ->
        raise_errorf ~loc "%s cannot be derived for open types" deriver
  in
  let polymorphize = Ppx_deriving.poly_fun_of_type_decl type_decl in
  [AH.Vb.mk
    (AC.pvar (Ppx_deriving.mangle_type_decl encode_prefix type_decl))
    (polymorphize [%expr ([%e encoder] : _ CConv.Encode.encoder)])]

(* signature of the generated encoder *)
let encode_sig_of_type ~options ~path type_decl =
  let typ = Ppx_deriving.core_type_of_type_decl type_decl in
  let polymorphize_enc =
    Ppx_deriving.poly_arrow_of_type_decl
      (fun var -> [%type: [%t var] CConv.Encode.encoder])
      type_decl
  in
  [AH.Sig.value
    (AH.Val.mk (mknoloc (Ppx_deriving.mangle_type_decl encode_prefix type_decl))
    (polymorphize_enc  [%type: [%t typ] CConv.Encode.encoder]))
  ]

(* generate a [typ CConv.Decode.decoder] for the given [typ].
  @param self an option contains the type being defined, and a reference
    indicating whether a self-reference was used *)
let decode_of_typ ~self typ =
  let rec decode_of_typ typ = match attr_decoder typ.ptyp_attributes with
    | None -> decode_of_typ_rec typ
    | Some d -> d
  and decode_of_typ_rec typ = match typ with
  | [%type: unit]            -> [%expr CConv.Decode.unit]
  | [%type: int]             -> [%expr CConv.Decode.int]
  | [%type: float]           -> [%expr CConv.Decode.float]
  | [%type: bool]            -> [%expr CConv.Decode.bool]
  | [%type: string]          -> [%expr CConv.Decode.string]
  | [%type: bytes]           -> [%expr CConv.Decode.(map Bytes.to_string string)]
  | [%type: char]            -> [%expr CConv.Decode.char]
  | [%type: [%t? typ] ref]   -> [%expr CConv.Decode.(map (!) [%e decode_of_typ typ])]
  | [%type: [%t? typ] list]  -> [%expr CConv.Decode.(list [%e decode_of_typ typ])]
  | [%type: int32] | [%type: Int32.t] -> [%expr CConv.Decode.int32]
  | [%type: int64] | [%type: Int64.t] -> [%expr CConv.Decode.int64]
  | [%type: nativeint] | [%type: Nativeint.t] -> [%expr CConv.Decode.nativeint]
  | [%type: [%t? typ] array] ->
      [%expr CConv.Decode.(array [%e decode_of_typ typ])]
  | [%type: [%t? typ] option] ->
      [%expr CConv.Decode.(option [%e decode_of_typ typ])]
  | { ptyp_desc = Ptyp_constr ({ txt = lid }, args) } ->
      begin match self, lid with
      | Some (name, used), Lident liname when liname=name ->
          (* typ is actually a recursive reference to the type
            being defined. Use a "self" variables that will be bound
            with [CConv.Decode.record_fix] or [CConv.Decode.sum_fix] *)
          used := true;
          AC.evar "self"
      | _ ->
          AC.app
            (AH.Exp.ident (mknoloc (Ppx_deriving.mangle_lid decode_prefix lid)))
            (List.map decode_of_typ args)
      end
  | { ptyp_desc = Ptyp_tuple typs } ->
      (* decode tuple, matching on the list *)
      [%expr CConv.Decode.(tuple {tuple_accept=fun src args ->
        match args with
        | [%p   (* didn't find how to build pattern [v1; v2; ...; vn] *)
            fold_right_i
            (fun i ty pat -> [%pat? [%p AC.pvar (argn i)] :: [%p pat]])
            typs [%pat? []]
          ] ->
          [%e AC.tuple (List.mapi
            (fun i ty ->
              [%expr CConv.Decode.apply src
                [%e decode_of_typ ty]
                [%e AC.evar (argn i)]
              ]
            ) typs
          )]
        | _ ->
          CConv.report_error "expected %d-ary tuple"
            [%e AC.int (List.length typs)]
      })]
  | { ptyp_desc = Ptyp_variant (fields, _, _); ptyp_loc } ->
      raise_errorf ~loc:ptyp_loc "%s cannot be derived for poly variants" deriver
  | { ptyp_desc = Ptyp_var name } ->
      [%expr ([%e AC.evar ("poly_"^name)] : 'a CConv.Decode.decoder)]
  | { ptyp_desc = Ptyp_alias (typ, name) } -> decode_of_typ typ
  | { ptyp_loc } ->
      raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s"
                   deriver (Ppx_deriving.string_of_core_type typ)
  in
  decode_of_typ typ

(* make an decoder from a type declaration *)
let decode_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
  let decoder =
    match type_decl.ptype_kind, type_decl.ptype_manifest with
    | Ptype_abstract, Some manifest -> decode_of_typ ~self:None manifest
    | Ptype_variant constrs, _ when contains_record_variant constrs ->
      raise_errorf ~loc "%s cannot be derived for record variants" deriver
    | Ptype_variant constrs, _ ->
        let self_used = ref false in
        let self = Some (type_decl.ptype_name.txt, self_used) in
        (* generate pattern matching cases *)
        let cases = List.map
          (fun { pcd_name = { txt = name' }; pcd_args; pcd_attributes } ->
            let pcd_args = extract_pcd_args_tuple_values ~loc pcd_args in
            AH.Exp.case
              [%pat?
                ([%p AC.pstr name'],
                 [%p AC.plist (List.mapi (fun i ty -> AC.pvar (argn i)) pcd_args)]
                )
              ]
              (AC.constr name'
                (List.mapi
                  (fun i ty ->
                    let decoder = match attr_decoder pcd_attributes with
                      | None -> decode_of_typ ~self ty
                      | Some d -> d
                    in
                    [%expr CConv.Decode.apply src
                      [%e decoder]
                      [%e AC.evar (argn i)]
                    ]
                  ) pcd_args
                )
              )
          ) constrs
        and last_case = AH.Exp.case
          (AH.Pat.any ()) [%expr CConv.report_error "expected sum"]
        in
        let sum_decoder = [%expr {CConv.Decode.sum_accept=fun src name args ->
          [%e AH.Exp.match_
            [%expr (name,args)]
            (cases @ [last_case])
          ]
        }] in
        if !self_used
        then [%expr CConv.Decode.sum_fix (fun self -> [%e sum_decoder]) ]
        else [%expr CConv.Decode.sum [%e sum_decoder]]
    | Ptype_record labels, _ ->
        let self_used = ref false in
        let self = Some (type_decl.ptype_name.txt, self_used) in
        (* build a list of
            let field = record_get "field" (decode field) src args in ... *)
        let bindings = fold_right_i
          (fun i field tail ->
            let decoder = match attr_decoder field.pld_attributes with
              | None -> decode_of_typ ~self field.pld_type
              | Some d -> d in
            let field_name = attr_key field.pld_name.txt field.pld_attributes in
            let body_expr = match attr_default field.pld_attributes with
              | Some default ->
                [%expr
                  match CConv.Decode.record_get_opt [%e AC.str field_name] [%e decoder] src args with
                  | Some v -> v
                  | None -> [%e default]]
              | None -> [%expr CConv.Decode.record_get [%e AC.str field_name] [%e decoder] src args]
            in
            [%expr let [%p AC.pvar field.pld_name.txt] = [%e body_expr] in
              [%e tail]]
          ) labels
          (AC.record (* build the record *)
            (List.map
              (fun field ->
                let name = field.pld_name.txt in
                name, AC.evar name
              ) labels
            )
          )
        in
        let record_decoder = [%expr
          {CConv.Decode.record_accept=fun src args -> [%e bindings] }
        ] in
        if !self_used
        then [%expr CConv.Decode.record_fix (fun self -> [%e record_decoder])]
        else [%expr CConv.Decode.record [%e record_decoder]]
    | Ptype_abstract, None ->
        raise_errorf ~loc "%s cannot be derived for fully abstract types" deriver
    | Ptype_open, _        ->
        raise_errorf ~loc "%s cannot be derived for open types" deriver
  in
  let polymorphize = Ppx_deriving.poly_fun_of_type_decl type_decl in
  [AH.Vb.mk
    (AC.pvar (Ppx_deriving.mangle_type_decl decode_prefix type_decl))
    (polymorphize [%expr ([%e decoder] : _ CConv.Decode.decoder)])]

(* signature of the generated encoder *)
let decode_sig_of_type ~options ~path type_decl =
  let typ = Ppx_deriving.core_type_of_type_decl type_decl in
  let polymorphize_enc =
    Ppx_deriving.poly_arrow_of_type_decl
      (fun var -> [%type: [%t var] CConv.Decode.decoder])
      type_decl
  in
  [AH.Sig.value
    (AH.Val.mk (mknoloc (Ppx_deriving.mangle_type_decl decode_prefix type_decl))
    (polymorphize_enc  [%type: [%t typ] CConv.Decode.decoder]))
  ]

let str_of_type ~options ~path type_decl =
  encode_of_type ~options ~path type_decl @
  decode_of_type ~options ~path type_decl

let sig_of_type ~options ~path type_decl =
  encode_sig_of_type ~options ~path type_decl @
  decode_sig_of_type ~options ~path type_decl

let () =
  let open Ppx_deriving in
  register (create "cconv"
    ~type_decl_str:(fun ~options ~path type_decls ->
      let recu = if List.length type_decls > 1 then Recursive else Nonrecursive in
      [AH.Str.value recu
        (List.concat (List.map (str_of_type ~options ~path) type_decls))]
    )
    ~type_decl_sig: (fun ~options ~path type_decls ->
      List.concat (List.map (sig_of_type ~options ~path) type_decls)
    )
    ()
  );
  register (create "encode"
    ~core_type:(encode_of_typ ~self:None)
    ~type_decl_str: (fun ~options ~path type_decls ->
      let recu = if List.length type_decls > 1 then Recursive else Nonrecursive in
      [AH.Str.value recu
        (List.concat (List.map (encode_of_type ~options ~path) type_decls))]
    )
    ~type_decl_sig: (fun ~options ~path type_decls ->
      List.concat (List.map (encode_sig_of_type ~options ~path) type_decls)
    )
    ()
  );
  register (create "decode"
    ~core_type:(fun typ -> (decode_of_typ ~self:None typ))
    ~type_decl_str: (fun ~options ~path type_decls ->
      let recu = if List.length type_decls > 1 then Recursive else Nonrecursive in
      [AH.Str.value recu
        (List.concat (List.map (decode_of_type ~options ~path) type_decls))]
    )
    ~type_decl_sig: (fun ~options ~path type_decls ->
      List.concat (List.map (decode_sig_of_type ~options ~path) type_decls)
    )
    ()
  );
  ()
OCaml

Innovation. Community. Security.