package ppx_typerep_conv

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

Source file ppx_typerep_conv.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
open Base
open Ppxlib
open Ast_builder.Default

let ( @@ ) a b = a b

module Gen = struct
  let let_in loc list_lid_expr body =
    List.fold_right list_lid_expr ~init:body ~f:(fun (lid, expr) body ->
      [%expr
        let [%p pvar ~loc lid] = [%e expr] in
        [%e body]])
  ;;
end

module Field_case = struct
  type t =
    { label : string
    ; ctyp : core_type
    ; index : int
    ; mutable_flag : Asttypes.mutable_flag
    }
end

module Variant_case = struct
  type t =
    { label : string
    ; ctyp : core_type option
    ; args_labels : string list
    ; poly : bool
    ; arity : int
    ; index : int
    ; arity_index : int
    }

  let patt ~loc t arg =
    let label = t.label in
    if t.poly
    then ppat_variant ~loc label arg
    else ppat_construct ~loc (Located.lident ~loc label) arg
  ;;

  let expr ~loc t arg =
    let label = t.label in
    if t.poly
    then pexp_variant ~loc label arg
    else pexp_construct ~loc (Located.lident ~loc label) arg
  ;;

  let ocaml_repr ~loc { label; poly; arity_index; _ } =
    if poly
    then
      [%expr
        Typerep_lib.Std.Typerep_obj.repr_of_poly_variant [%e pexp_variant ~loc label None]]
    else eint ~loc arity_index
  ;;
end

module Branches = struct
  let fields fields =
    let mapi index ld : Field_case.t =
      { label = ld.pld_name.txt
      ; ctyp = ld.pld_type
      ; index
      ; mutable_flag = ld.pld_mutable
      }
    in
    List.mapi fields ~f:mapi
  ;;

  let row_fields rfs =
    (* duplicates like [ `A | `B | `A ] cause warnings in the generated code (duplicated
       patterns), so we don't have to deal with them. *)
    let no_arg =
      let r = ref (-1) in
      fun () ->
        r := !r + 1;
        !r
    in
    let with_arg =
      let r = ref (-1) in
      fun () ->
        r := !r + 1;
        !r
    in
    let mapi index rf : Variant_case.t =
      match rf.prf_desc with
      | Rtag ({ txt = label; _ }, true, _) | Rtag ({ txt = label; _ }, _, []) ->
        { label
        ; ctyp = None
        ; args_labels = []
        ; poly = true
        ; arity = 0
        ; index
        ; arity_index = no_arg ()
        }
      | Rtag ({ txt = label; _ }, false, ctyp :: _) ->
        { label
        ; ctyp = Some ctyp
        ; args_labels = []
        ; poly = true
        ; arity = 1
        ; index
        ; arity_index = with_arg ()
        }
      | Rinherit ty ->
        Location.raise_errorf ~loc:ty.ptyp_loc "ppx_typerep_conv: unknown type"
    in
    List.mapi rfs ~f:mapi
  ;;

  let constructors cds =
    let no_arg =
      let r = ref (-1) in
      fun () ->
        r := !r + 1;
        !r
    in
    let with_arg =
      let r = ref (-1) in
      fun () ->
        r := !r + 1;
        !r
    in
    let mapi index cd : Variant_case.t =
      if Option.is_some cd.pcd_res
      then Location.raise_errorf ~loc:cd.pcd_loc "ppx_typerep_conv: GADTs not supported";
      let label = cd.pcd_name.txt in
      let loc = cd.pcd_loc in
      match cd.pcd_args with
      | Pcstr_tuple [] ->
        { label
        ; ctyp = None
        ; args_labels = []
        ; poly = false
        ; arity = 0
        ; index
        ; arity_index = no_arg ()
        }
      | Pcstr_tuple args ->
        let arity = List.length args in
        let ctyp = ptyp_tuple ~loc args in
        { label
        ; ctyp = Some ctyp
        ; args_labels = []
        ; poly = false
        ; arity
        ; index
        ; arity_index = with_arg ()
        }
      | Pcstr_record labels ->
        let args = List.map labels ~f:(fun { pld_type; _ } -> pld_type) in
        let args_labels = List.map labels ~f:(fun { pld_name; _ } -> pld_name.txt) in
        let arity = List.length args in
        let ctyp = ptyp_tuple ~loc args in
        { label
        ; ctyp = Some ctyp
        ; args_labels
        ; poly = false
        ; arity
        ; index
        ; arity_index = with_arg ()
        }
    in
    List.mapi cds ~f:mapi
  ;;
end

module Typerep_signature = struct
  let sig_of_typerep_of_t td =
    combinator_type_of_type_declaration td ~f:(fun ~loc ty ->
      [%type: [%t ty] Typerep_lib.Std.Typerep.t])
  ;;

  let sig_of_typename_of_t td =
    combinator_type_of_type_declaration td ~f:(fun ~loc ty ->
      [%type: [%t ty] Typerep_lib.Std.Typename.t])
  ;;

  let sig_of_one_def td =
    let typerep_of = sig_of_typerep_of_t td in
    let typename_of = sig_of_typename_of_t td in
    let loc = td.ptype_loc in
    let type_name = td.ptype_name.txt in
    [ psig_value
        ~loc
        (value_description
           ~loc
           ~name:(Located.mk ~loc ("typerep_of_" ^ type_name))
           ~type_:typerep_of
           ~prim:[])
    ; psig_value
        ~loc
        (value_description
           ~loc
           ~name:(Located.mk ~loc ("typename_of_" ^ type_name))
           ~type_:typename_of
           ~prim:[])
    ]
  ;;

  let sig_generator ~loc ~path:_ (_rec_flag, tds) =
    match
      mk_named_sig
        ~loc
        ~sg_name:"Typerep_lib.Typerepable.S"
        ~handle_polymorphic_variant:true
        tds
    with
    | Some include_infos -> [ psig_include ~loc include_infos ]
    | None -> List.concat_map tds ~f:sig_of_one_def
  ;;

  let gen = Deriving.Generator.make Deriving.Args.empty sig_generator
end

module Typerep_implementation = struct
  module Util : sig
    val typename_field : loc:Location.t -> type_name:string option -> expression
    val arg_of_param : string -> string
    val params_names : params:(core_type * (variance * injectivity)) list -> string list
    val params_patts : loc:Location.t -> params_names:string list -> pattern list

    val type_name_module_definition
      :  loc:Location.t
      -> path:string
      -> type_name:string
      -> params_names:string list
      -> structure

    val with_named
      :  loc:Location.t
      -> type_name:string
      -> params_names:string list
      -> expression
      -> expression

    val typerep_of_t_coerce : type_declaration -> core_type option

    val typerep_abstract
      :  loc:Location.t
      -> path:string
      -> type_name:string
      -> params_names:string list
      -> structure_item

    module Record : sig
      val field_n_ident : fields:Field_case.t list -> int -> string

      val fields
        :  loc:Location.t
        -> typerep_of_type:(core_type -> expression)
        -> fields:Field_case.t list
        -> (int * string * expression) list

      val create : loc:Location.t -> fields:Field_case.t list -> expression
      val has_double_array_tag : loc:Location.t -> fields:Field_case.t list -> expression
    end

    module Variant : sig
      val tag_n_ident : variants:Variant_case.t list -> int -> string

      val tags
        :  loc:Location.t
        -> typerep_of_type:(core_type -> expression)
        -> variants:Variant_case.t list
        -> (int * expression) list

      val value : loc:Location.t -> variants:Variant_case.t list -> expression
      val polymorphic : loc:Location.t -> variants:Variant_case.t list -> expression
    end
  end = struct
    let str_item_type_and_name ~loc ~path ~params_names ~type_name =
      let params =
        List.map params_names ~f:(fun name ->
          ptyp_var ~loc name, (NoVariance, NoInjectivity))
      in
      let td =
        let manifest =
          ptyp_constr
            ~loc
            (Located.lident ~loc type_name)
            (List.map params_names ~f:(ptyp_var ~loc))
        in
        type_declaration
          ~loc
          ~name:(Located.mk ~loc "t")
          ~params
          ~manifest:(Some manifest)
          ~kind:Ptype_abstract
          ~cstrs:[]
          ~private_:Public
      in
      let name_def =
        let full_type_name = Printf.sprintf "%s.%s" path type_name in
        [%stri let name = [%e estring ~loc full_type_name]]
      in
      pmod_structure ~loc [ pstr_type ~loc Nonrecursive [ td ]; name_def ]
    ;;

    let arg_of_param name = "_of_" ^ name
    let name_of_t ~type_name = "name_of_" ^ type_name

    let typename_field ~loc ~type_name =
      match type_name with
      | None -> [%expr Typerep_lib.Std.Typename.create ()]
      | Some type_name ->
        [%expr
          Typerep_lib.Std.Typerep.Named.typename_of_t
            [%e evar ~loc @@ name_of_t ~type_name]]
    ;;

    let params_names ~params = List.map params ~f:(fun x -> (get_type_param_name x).txt)

    let params_patts ~loc ~params_names =
      List.map params_names ~f:(fun s -> pvar ~loc @@ arg_of_param s)
    ;;

    let type_name_module_name ~type_name = "Typename_of_" ^ type_name

    let with_named ~loc ~type_name ~params_names expr =
      let name_t =
        eapply
          ~loc
          (pexp_ident ~loc
           @@ Located.lident ~loc
           @@ type_name_module_name ~type_name
           ^ ".named")
          (List.map params_names ~f:(fun name -> evar ~loc @@ arg_of_param name))
      in
      let name_of_t = name_of_t ~type_name in
      let args = [%expr [%e evar ~loc name_of_t], Some (lazy [%e expr])] in
      [%expr
        let [%p pvar ~loc name_of_t] = [%e name_t] in
        Typerep_lib.Std.Typerep.Named [%e args]]
    ;;

    let typerep_of_t_coerce td =
      match td.ptype_params with
      | [] -> None
      | params ->
        let t =
          combinator_type_of_type_declaration td ~f:(fun ~loc ty ->
            [%type: [%t ty] Typerep_lib.Std.Typerep.t])
        in
        Some (ptyp_poly ~loc:td.ptype_loc (List.map params ~f:get_type_param_name) t)
    ;;

    let type_name_module_definition ~loc ~path ~type_name ~params_names =
      let name = type_name_module_name ~type_name in
      let type_arity = List.length params_names in
      let make =
        pmod_ident ~loc
        @@ Located.lident ~loc
        @@ "Typerep_lib.Std.Make_typename.Make"
        ^ Int.to_string type_arity
      in
      let type_name_struct = str_item_type_and_name ~loc ~path ~params_names ~type_name in
      let type_name_module = pmod_apply ~loc make type_name_struct in
      let module_def =
        pstr_module ~loc
        @@ module_binding ~loc ~name:(Located.mk ~loc (Some name)) ~expr:type_name_module
      in
      let typename_of_t =
        let lid = "typename_of_" ^ type_name in
        pstr_value
          ~loc
          Nonrecursive
          [ value_binding
              ~loc
              ~pat:(pvar ~loc lid)
              ~expr:(evar ~loc (name ^ ".typename_of_t"))
          ]
      in
      [ module_def; typename_of_t ]
    ;;

    let typerep_abstract ~loc ~path ~type_name ~params_names =
      let type_name_struct = str_item_type_and_name ~loc ~path ~params_names ~type_name in
      let type_arity = List.length params_names in
      let make =
        pmod_ident ~loc
        @@ Located.lident ~loc
        @@ "Typerep_lib.Std.Type_abstract.Make"
        ^ Int.to_string type_arity
      in
      pstr_include ~loc @@ include_infos ~loc @@ pmod_apply ~loc make type_name_struct
    ;;

    let field_or_tag_n_ident prefix ~list n =
      if n < 0 || n > List.length list then assert false;
      prefix ^ Int.to_string n
    ;;

    module Record = struct
      let field_n_ident ~fields:list = field_or_tag_n_ident "field" ~list

      let fields ~loc ~typerep_of_type ~fields =
        let map { Field_case.ctyp; label; index; mutable_flag } =
          let rep = typerep_of_type ctyp in
          let is_mutable =
            match mutable_flag with
            | Mutable -> true
            | Immutable -> false
          in
          ( index
          , label
          , [%expr
              Typerep_lib.Std.Typerep.Field.internal_use_only
                { Typerep_lib.Std.Typerep.Field_internal.label = [%e estring ~loc label]
                ; index = [%e eint ~loc index]
                ; is_mutable = [%e ebool ~loc is_mutable]
                ; rep = [%e rep]
                ; tyid = Typerep_lib.Std.Typename.create ()
                ; get =
                    (fun t ->
                      [%e pexp_field ~loc (evar ~loc "t") (Located.lident ~loc label)])
                }] )
        in
        List.map ~f:map fields
      ;;

      let has_double_array_tag ~loc ~fields =
        let fields_binding =
          let map { Field_case.label; _ } =
            (* The value must be a float else this segfaults.  This is tested by the
               unit tests in case this property changes. *)
            ( Located.lident ~loc label
            , [%expr Typerep_lib.Std.Typerep_obj.double_array_value ()] )
          in
          List.map ~f:map fields
        in
        [%expr
          Typerep_lib.Std.Typerep_obj.has_double_array_tag
            [%e pexp_record ~loc fields_binding None]]
      ;;

      let create ~loc ~fields =
        let record =
          (* Calling [get] on the fields from left to right matters, so that iteration
             goes left to right too. *)
          let fields_binding =
            let map { Field_case.label; _ } =
              Located.lident ~loc label, evar ~loc label
            in
            List.map ~f:map fields
          in
          let record = pexp_record ~loc fields_binding None in
          let vbs =
            List.mapi fields ~f:(fun i { Field_case.index; label; _ } ->
              assert (i = index);
              let pat = pvar ~loc label in
              let expr = [%expr get [%e evar ~loc @@ field_n_ident ~fields index]] in
              value_binding ~loc ~pat ~expr)
          in
          pexp_let ~loc Nonrecursive vbs record
        in
        [%expr fun { Typerep_lib.Std.Typerep.Record_internal.get } -> [%e record]]
      ;;
    end

    module Variant = struct
      (* tag_0, tag_1, etc. *)
      let tag_n_ident ~variants:list = field_or_tag_n_ident "tag" ~list

      let polymorphic ~loc ~variants =
        let polymorphic =
          match variants with
          | [] -> true
          | hd :: _ -> hd.Variant_case.poly
        in
        [%expr [%e ebool ~loc polymorphic]]
      ;;

      let tags ~loc ~typerep_of_type ~variants =
        let create ({ Variant_case.arity; args_labels; _ } as variant) =
          if arity = 0
          then
            [%expr
              Typerep_lib.Std.Typerep.Tag_internal.Const
                [%e Variant_case.expr ~loc variant None]]
          else (
            let arg_tuple i = "v" ^ Int.to_string i in
            let patt, expr =
              let patt =
                let f i = pvar ~loc @@ arg_tuple i in
                ppat_tuple ~loc (List.init arity ~f)
              in
              let expr =
                let f i = evar ~loc @@ arg_tuple i in
                let args =
                  match args_labels with
                  | [] -> pexp_tuple ~loc (List.init arity ~f)
                  | _ :: _ as labels ->
                    pexp_record
                      ~loc
                      (List.mapi labels ~f:(fun i label -> Located.lident ~loc label, f i))
                      None
                in
                Variant_case.expr ~loc variant (Some args)
              in
              patt, expr
            in
            [%expr Typerep_lib.Std.Typerep.Tag_internal.Args (fun [%p patt] -> [%e expr])])
        in
        let mapi
          index'
          ({ Variant_case.ctyp; label; arity; args_labels; index; _ } as variant)
          =
          if index <> index' then assert false;
          let rep, tyid =
            match ctyp with
            | Some ctyp ->
              typerep_of_type ctyp, [%expr Typerep_lib.Std.Typename.create ()]
            | None -> [%expr typerep_of_tuple0], [%expr typename_of_tuple0]
          in
          let args_labels = List.map args_labels ~f:(fun x -> estring ~loc x) in
          ( index
          , [%expr
              Typerep_lib.Std.Typerep.Tag.internal_use_only
                { Typerep_lib.Std.Typerep.Tag_internal.label = [%e estring ~loc label]
                ; rep = [%e rep]
                ; arity = [%e eint ~loc arity]
                ; args_labels = [%e elist ~loc args_labels]
                ; index = [%e eint ~loc index]
                ; ocaml_repr = [%e Variant_case.ocaml_repr ~loc variant]
                ; tyid = [%e tyid]
                ; create = [%e create variant]
                }] )
        in
        List.mapi ~f:mapi variants
      ;;

      let value ~loc ~variants =
        let match_cases =
          let arg_tuple i = "v" ^ Int.to_string i in
          let mapi index' ({ Variant_case.arity; index; args_labels; _ } as variant) =
            if index <> index' then assert false;
            let patt, value =
              if arity = 0
              then Variant_case.patt ~loc variant None, [%expr value_tuple0]
              else (
                let patt =
                  let f i = pvar ~loc @@ arg_tuple i in
                  let args =
                    match args_labels with
                    | [] -> ppat_tuple ~loc (List.init arity ~f)
                    | _ :: _ as labels ->
                      ppat_record
                        ~loc
                        (List.mapi labels ~f:(fun i label ->
                           Located.lident ~loc label, f i))
                        Closed
                  in
                  Variant_case.patt ~loc variant (Some args)
                in
                let expr =
                  let f i = evar ~loc @@ arg_tuple i in
                  pexp_tuple ~loc (List.init arity ~f)
                in
                patt, expr)
            in
            let tag = evar ~loc @@ tag_n_ident ~variants index in
            let prod =
              [%expr
                Typerep_lib.Std.Typerep.Variant_internal.Value ([%e tag], [%e value])]
            in
            case ~guard:None ~lhs:patt ~rhs:prod
          in
          List.mapi ~f:mapi variants
        in
        pexp_function_cases ~loc match_cases
      ;;
    end
  end

  let rec typerep_of_type ty =
    let loc = { ty.ptyp_loc with loc_ghost = true } in
    match ty.ptyp_desc with
    | Ptyp_constr (id, params) ->
      type_constr_conv
        ~loc
        id
        ~f:(fun tn -> "typerep_of_" ^ tn)
        (List.map params ~f:typerep_of_type)
    | Ptyp_var parm -> evar ~loc @@ Util.arg_of_param parm
    | Ptyp_variant (row_fields, _, _) ->
      typerep_of_variant loc ~type_name:None (Branches.row_fields row_fields)
    | Ptyp_tuple tuple -> typerep_of_tuple loc tuple
    | _ -> Location.raise_errorf ~loc "ppx_typerep: unknown type"

  and typerep_of_tuple loc tuple =
    let typereps = List.map tuple ~f:typerep_of_type in
    match typereps with
    | [ typerep ] -> typerep
    | _ ->
      let typerep_of_tuple =
        let len = List.length typereps in
        if len < 2 || len > 5
        then
          Location.raise_errorf
            ~loc
            "ppx_type_conv: unsupported tuple arity %d. must be in {2,3,4,5}"
            len
        else evar ~loc @@ "typerep_of_tuple" ^ Int.to_string len
      in
      eapply ~loc typerep_of_tuple typereps

  and typerep_of_record loc ~type_name lds =
    let fields = Branches.fields lds in
    let field_ident i = Util.Record.field_n_ident ~fields i in
    let indexed_fields = Util.Record.fields ~loc ~typerep_of_type ~fields in
    let fields_array =
      let fields =
        List.map
          ~f:(fun (index, _, _) ->
            [%expr
              Typerep_lib.Std.Typerep.Record_internal.Field
                [%e evar ~loc @@ field_ident index]])
          indexed_fields
      in
      pexp_array ~loc fields
    in
    let bindings =
      [ "typename", Util.typename_field ~loc ~type_name:(Some type_name)
      ; "has_double_array_tag", Util.Record.has_double_array_tag ~loc ~fields
      ; "fields", fields_array
      ; "create", Util.Record.create ~loc ~fields
      ]
    in
    let fields_binding =
      let map (name, _) =
        ( Located.lident ~loc ("Typerep_lib.Std.Typerep.Record_internal." ^ name)
        , evar ~loc name )
      in
      List.map ~f:map bindings
    in
    let record =
      let fields =
        [%expr
          Typerep_lib.Std.Typerep.Record.internal_use_only
            [%e pexp_record ~loc fields_binding None]]
      in
      [%expr Typerep_lib.Std.Typerep.Record [%e fields]]
    in
    let record = Gen.let_in loc bindings record in
    let record =
      List.fold_right
        indexed_fields
        ~f:(fun (index, _, expr) acc ->
          [%expr
            let [%p pvar ~loc @@ field_ident index] = [%e expr] in
            [%e acc]])
        ~init:record
    in
    record

  and typerep_of_variant loc ~type_name variants =
    let tags = Util.Variant.tags ~loc ~typerep_of_type ~variants in
    let tag_ident i = Util.Variant.tag_n_ident ~variants i in
    let tags_array =
      let tags =
        List.map
          ~f:(fun (index, _) ->
            [%expr
              Typerep_lib.Std.Typerep.Variant_internal.Tag
                [%e evar ~loc @@ tag_ident index]])
          tags
      in
      pexp_array ~loc tags
    in
    let bindings =
      [ "typename", Util.typename_field ~loc ~type_name
      ; "tags", tags_array
      ; "polymorphic", Util.Variant.polymorphic ~loc ~variants
      ; "value", Util.Variant.value ~loc ~variants
      ]
    in
    let tags_binding =
      let map (name, _) =
        ( Located.lident ~loc ("Typerep_lib.Std.Typerep.Variant_internal." ^ name)
        , evar ~loc name )
      in
      List.map ~f:map bindings
    in
    let variant =
      let tags =
        [%expr
          Typerep_lib.Std.Typerep.Variant.internal_use_only
            [%e pexp_record ~loc tags_binding None]]
      in
      [%expr Typerep_lib.Std.Typerep.Variant [%e tags]]
    in
    let variant = Gen.let_in loc bindings variant in
    let variant =
      List.fold_right
        tags
        ~f:(fun (index, expr) acc ->
          [%expr
            let [%p pvar ~loc @@ tag_ident index] = [%e expr] in
            [%e acc]])
        ~init:variant
    in
    variant
  ;;

  let impl_of_one_def ~loc:_ ~path td =
    let loc = td.ptype_loc in
    let type_name = td.ptype_name.txt in
    let body =
      match td.ptype_kind with
      | Ptype_variant cds ->
        typerep_of_variant loc ~type_name:(Some type_name) (Branches.constructors cds)
      | Ptype_record lds -> typerep_of_record loc ~type_name lds
      | Ptype_open ->
        Location.raise_errorf ~loc "ppx_typerep_conv: open types are not supported"
      | Ptype_abstract ->
        (match td.ptype_manifest with
         | None ->
           Location.raise_errorf
             ~loc
             "typerep cannot be applied on abstract types, except like 'type t \
              [@@@@deriving typerep ~abstract]'"
         | Some ty ->
           (match ty.ptyp_desc with
            | Ptyp_variant (row_fields, _, _) ->
              typerep_of_variant
                loc
                ~type_name:(Some type_name)
                (Branches.row_fields row_fields)
            | _ -> typerep_of_type ty))
    in
    let params = td.ptype_params in
    let params_names = Util.params_names ~params in
    let params_patts = Util.params_patts ~loc ~params_names in
    let body = Util.with_named ~loc ~type_name ~params_names body in
    let arguments =
      List.map2_exn params_names params_patts ~f:(fun name patt ->
        (* Add type annotations to parameters, at least to avoid the unused type warning. *)
        let loc = patt.ppat_loc in
        [%pat?
          ([%p patt] :
            [%t ptyp_constr ~loc (Located.lident ~loc name) []] Typerep_lib.Std.Typerep.t)])
    in
    let body = eabstract ~loc arguments body in
    let body =
      List.fold_right params_names ~init:body ~f:(fun name acc ->
        pexp_newtype ~loc { txt = name; loc } acc)
    in
    let bnd = pvar ~loc @@ "typerep_of_" ^ type_name in
    let bnd =
      match Util.typerep_of_t_coerce td with
      | Some coerce -> ppat_constraint ~loc bnd coerce
      | None -> bnd
    in
    let binding = value_binding ~loc ~pat:bnd ~expr:body in
    Util.type_name_module_definition ~loc ~path ~type_name ~params_names, binding
  ;;

  module List = struct
    include List

    (* to avoid gensym diffs with camlp4 *)
    let map_right_to_left xs ~f = rev xs |> map ~f |> rev
  end

  let with_typerep ~loc ~path (rec_flag, tds) =
    let tds = List.map tds ~f:name_type_params_in_td in
    let rec_flag = really_recursive rec_flag tds in
    let prelude, bindings =
      List.unzip (List.map_right_to_left tds ~f:(impl_of_one_def ~loc ~path))
    in
    List.concat prelude @ [ pstr_value ~loc rec_flag bindings ]
  ;;

  let with_typerep_abstract ~loc:_ ~path (_rec_flag, tds) =
    List.map tds ~f:(fun td ->
      let td = name_type_params_in_td td in
      let loc = td.ptype_loc in
      let type_name = td.ptype_name.txt in
      let params = td.ptype_params in
      let params_names = Util.params_names ~params in
      Util.typerep_abstract ~loc ~path ~type_name ~params_names)
  ;;

  let gen =
    Deriving.Generator.make
      Deriving.Args.(empty +> flag "abstract")
      (fun ~loc ~path x abstract ->
        if abstract then with_typerep_abstract ~loc ~path x else with_typerep ~loc ~path x)
  ;;

  let typerep_of_extension ~loc:_ ~path:_ ctyp = typerep_of_type ctyp
end

let typerep =
  Deriving.add
    "typerep"
    ~sig_type_decl:Typerep_signature.gen
    ~str_type_decl:Typerep_implementation.gen
;;

let () =
  Deriving.add "typerep_of" ~extension:Typerep_implementation.typerep_of_extension
  |> Deriving.ignore
;;