Source file deriving.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
open Import
open Ast_builder.Default
let do_insert_unused_warning_attribute = ref false
let keep_w32_impl = ref false
let keep_w32_intf = ref false
let () =
let keep_w32_spec =
Caml.Arg.Symbol
(["impl"; "intf"; "both"],
(function
| "impl" -> keep_w32_impl := true
| "intf" -> keep_w32_intf := true
| "both" ->
keep_w32_impl := true;
keep_w32_intf := true
| _ -> assert false))
in
let conv_w32_spec =
Caml.Arg.Symbol
(["code"; "attribute"],
(function
| "code" -> do_insert_unused_warning_attribute := false
| "attribute" -> do_insert_unused_warning_attribute := true
| _ -> assert false))
in
Driver.add_arg "-deriving-keep-w32"
keep_w32_spec
~doc:" Do not try to disable warning 32 for the generated code";
Driver.add_arg "-deriving-disable-w32-method"
conv_w32_spec
~doc:" How to disable warning 32 for the generated code";
Driver.add_arg "-type-conv-keep-w32"
keep_w32_spec
~doc:" Deprecated, use -deriving-keep-w32";
Driver.add_arg "-type-conv-w32"
conv_w32_spec
~doc:" Deprecated, use -deriving-disable-w32-method"
let keep_w32_impl () = !keep_w32_impl || Driver.pretty ()
let keep_w32_intf () = !keep_w32_intf || Driver.pretty ()
module List = struct
include List
let concat_map xs ~f = concat (map xs ~f)
let rec filter_map l ~f =
match l with
| [] -> []
| x :: l ->
match f x with
| None -> filter_map l ~f
| Some y -> y :: filter_map l ~f
end
module Args = struct
include (Ast_pattern : module type of struct include Ast_pattern end
with type ('a, 'b, 'c) t := ('a, 'b, 'c) Ast_pattern.t)
type 'a param =
{ name : string
; pattern : (expression, 'a) Ast_pattern.Packed.t
; default : 'a
}
let arg name pattern =
{ name
; default = None
; pattern = Ast_pattern.Packed.create pattern (fun x -> Some x)
}
;;
let flag name =
let pattern = pexp_ident (lident (string name)) in
{ name
; default = false
; pattern = Ast_pattern.Packed.create pattern true
}
;;
type (_, _) t =
| Nil : ('m, 'm) t
| Cons : ('m1, 'a -> 'm2) t * 'a param -> ('m1, 'm2) t
let empty = Nil
let ( +> ) a b = Cons (a, b)
let rec names : type a b. (a, b) t -> string list = function
| Nil -> []
| Cons (t, p) -> p.name :: names t
;;
module Instance = struct
type (_, _) instance =
| I_nil : ('m, 'm) instance
| I_cons : ('m1, 'a -> 'm2) instance * 'a -> ('m1, 'm2) instance
let rec create
: type a b. (a, b) t -> (string * expression) list -> (a, b) instance
= fun spec args ->
match spec with
| Nil -> I_nil
| Cons (t, p) ->
let value =
match List.Assoc.find args ~equal:String.equal p.name with
| None -> p.default
| Some expr -> Ast_pattern.Packed.parse p.pattern expr.pexp_loc expr
in
I_cons (create t args, value)
;;
let rec apply : type a b. (a, b) instance -> a -> b = fun t f ->
match t with
| I_nil -> f
| I_cons (t, x) -> apply t f x
;;
end
let apply t args f = Instance.apply (Instance.create t args) f
end
type t = string
let ignore (_ : t) = ()
type parsed_args =
| Args of (string * expression) list
| Unknown_syntax of Location.t * string
module Generator = struct
type deriver = t
type ('a, 'b) t =
| T : { spec : ('c, 'a) Args.t
; gen : ctxt:Expansion_context.Deriver.t -> 'b -> 'c
; arg_names : Set.M(String).t
; attributes : Attribute.packed list
; deps : deriver list
} -> ('a, 'b) t
let deps (T t) = t.deps
module V2 = struct
let make ?(attributes=[]) ?(deps=[]) spec gen =
let arg_names = Set.of_list (module String) (Args.names spec) in
T { spec
; gen
; arg_names
; attributes
; deps
}
;;
let make_noarg ?attributes ?deps gen = make ?attributes ?deps Args.empty gen
end
let make ?attributes ?deps spec gen =
V2.make ?attributes ?deps spec (Expansion_context.Deriver.with_loc_and_path gen)
let make_noarg ?attributes ?deps gen = make ?attributes ?deps Args.empty gen
let merge_accepted_args l =
let rec loop acc = function
| [] -> acc
| T t :: rest -> loop (Set.union acc t.arg_names) rest
in
loop (Set.empty (module String)) l
let check_arguments name generators (args : (string * expression) list) =
List.iter args ~f:(fun (label, e) ->
if String.is_empty label then
Location.raise_errorf ~loc:e.pexp_loc
"Ppxlib.Deriving: generator arguments must be labelled");
Option.iter (List.find_a_dup args ~compare:(fun (a, _) (b, _) -> String.compare a b))
~f:(fun (label, e) ->
Location.raise_errorf ~loc:e.pexp_loc
"Ppxlib.Deriving: argument labelled '%s' appears more than once" label);
let accepted_args = merge_accepted_args generators in
List.iter args ~f:(fun (label, e) ->
if not (Set.mem accepted_args label) then
let spellcheck_msg =
match Spellcheck.spellcheck (Set.to_list accepted_args) label with
| None -> ""
| Some s -> ".\n" ^ s
in
Location.raise_errorf ~loc:e.pexp_loc
"Ppxlib.Deriving: generator '%s' doesn't accept argument '%s'%s"
name label spellcheck_msg);
;;
let apply (T t) ~name:_ ~ctxt x args =
Args.apply t.spec args (t.gen ~ctxt x)
;;
let apply_all ~ctxt entry (name, generators, args) =
check_arguments name.txt generators args;
List.concat_map generators ~f:(fun t -> apply t ~name:name.txt ~ctxt entry args)
;;
let apply_all ~ctxt entry generators =
List.concat_map generators ~f:(apply_all ~ctxt entry)
;;
end
module Deriver = struct
module Actual_deriver = struct
type t =
{ name : string
; str_type_decl : (structure, rec_flag * type_declaration list) Generator.t option
; str_type_ext : (structure, type_extension ) Generator.t option
; str_exception : (structure, extension_constructor ) Generator.t option
; sig_type_decl : (signature, rec_flag * type_declaration list) Generator.t option
; sig_type_ext : (signature, type_extension ) Generator.t option
; sig_exception : (signature, extension_constructor ) Generator.t option
; extension : (loc:Location.t -> path:string -> core_type -> expression) option
}
end
module Alias = struct
type t =
{ str_type_decl : string list
; str_type_ext : string list
; str_exception : string list
; sig_type_decl : string list
; sig_type_ext : string list
; sig_exception : string list
}
end
module Field = struct
type kind = Str | Sig
type ('a, 'b) t =
{ name : string
; kind : kind
; get : Actual_deriver.t -> ('a, 'b) Generator.t option
; get_set : Alias.t -> string list
}
let str_type_decl = { kind = Str; name = "type"
; get = (fun t -> t.str_type_decl)
; get_set = (fun t -> t.str_type_decl) }
let str_type_ext = { kind = Str; name = "type extension"
; get = (fun t -> t.str_type_ext)
; get_set = (fun t -> t.str_type_ext ) }
let str_exception = { kind = Str; name = "exception"
; get = (fun t -> t.str_exception)
; get_set = (fun t -> t.str_exception) }
let sig_type_decl = { kind = Sig; name = "signature type"
; get = (fun t -> t.sig_type_decl)
; get_set = (fun t -> t.sig_type_decl) }
let sig_type_ext = { kind = Sig; name = "signature type extension"
; get = (fun t -> t.sig_type_ext)
; get_set = (fun t -> t.sig_type_ext ) }
let sig_exception = { kind = Sig; name = "signature exception"
; get = (fun t -> t.sig_exception)
; get_set = (fun t -> t.sig_exception) }
end
type t =
| Actual_deriver of Actual_deriver.t
| Alias of Alias.t
type Ppx_derivers.deriver += T of t
let derivers () =
List.filter_map (Ppx_derivers.derivers ()) ~f:(function
| name, T t -> Some (name, t)
| _ -> None)
exception Not_supported of string
let resolve_actual_derivers (field : (_, _) Field.t) name =
let rec loop name collected =
if List.exists collected
~f:(fun (d : Actual_deriver.t) -> String.equal d.name name) then
collected
else
match Ppx_derivers.lookup name with
| Some (T (Actual_deriver drv)) -> drv :: collected
| Some (T (Alias alias)) ->
let set = field.get_set alias in
List.fold_right set ~init:collected ~f:loop
| _ -> raise (Not_supported name)
in
List.rev (loop name [])
let resolve_internal (field : (_, _) Field.t) name =
List.map (resolve_actual_derivers field name) ~f:(fun drv ->
match field.get drv with
| None -> raise (Not_supported name)
| Some g -> (drv.name, g))
;;
let supported_for field =
List.fold_left (derivers ()) ~init:(Set.empty (module String))
~f:(fun acc (name, _) ->
match resolve_internal field name with
| _ -> Set.add acc name
| exception Not_supported _ -> acc)
|> Set.to_list
;;
let not_supported (field : (_, _) Field.t) ?(spellcheck=true) name =
let spellcheck_msg =
if spellcheck then
match Spellcheck.spellcheck (supported_for field) name.txt with
| None -> ""
| Some s -> ".\n" ^ s
else
""
in
Location.raise_errorf ~loc:name.loc
"Ppxlib.Deriving: '%s' is not a supported %s deriving generator%s"
name.txt field.name spellcheck_msg
;;
let resolve field name =
try
resolve_internal field name.txt
with Not_supported name' ->
not_supported field ~spellcheck:(String.equal name.txt name') name
;;
let resolve_all field derivers =
let derivers_and_args =
List.filter_map derivers ~f:(fun (name, args) ->
match Ppx_derivers.lookup name.txt with
| None ->
not_supported field name
| Some (T _) ->
Some
(name,
match args with
| Args l -> l
| Unknown_syntax (loc, msg) ->
Location.raise_errorf ~loc "Ppxlib.Deriving: %s" msg)
| Some _ ->
None)
in
let seen = Hash_set.create (module String) in
List.map derivers_and_args ~f:(fun (name, args) ->
let named_generators = resolve field name in
List.iter named_generators ~f:(fun (actual_deriver_name, gen) ->
if Options.fail_on_duplicate_derivers
&& Hash_set.mem seen actual_deriver_name then
Location.raise_errorf ~loc:name.loc
"Deriver %s appears twice" actual_deriver_name;
List.iter (Generator.deps gen) ~f:(fun dep ->
List.iter (resolve_actual_derivers field dep) ~f:(fun drv ->
let dep_name = drv.name in
if not (Hash_set.mem seen dep_name) then
Location.raise_errorf ~loc:name.loc
"Deriver %s is needed for %s, you need to add it before in the list"
dep_name name.txt));
Hash_set.add seen actual_deriver_name);
(name, List.map named_generators ~f:snd, args))
;;
let add
?str_type_decl
?str_type_ext
?str_exception
?sig_type_decl
?sig_type_ext
?sig_exception
?extension
name
=
let actual_deriver : Actual_deriver.t =
{ name
; str_type_decl
; str_type_ext
; str_exception
; sig_type_decl
; sig_type_ext
; sig_exception
; extension
}
in
Ppx_derivers.register name (T (Actual_deriver actual_deriver));
(match extension with
| None -> ()
| Some f ->
let extension = Extension.declare name Expression Ast_pattern.(ptyp __) f in
Driver.register_transformation ("Ppxlib.Deriving." ^ name)
~rules:[ Context_free.Rule.extension extension ]);
name
;;
let add_alias
name
?str_type_decl
?str_type_ext
?str_exception
?sig_type_decl
?sig_type_ext
?sig_exception
set
=
let alias : Alias.t =
let get = function
| None -> set
| Some set -> set
in
{ str_type_decl = get str_type_decl
; str_type_ext = get str_type_ext
; str_exception = get str_exception
; sig_type_decl = get sig_type_decl
; sig_type_ext = get sig_type_ext
; sig_exception = get sig_exception
}
in
Ppx_derivers.register name (T (Alias alias));
name
;;
end
let add = Deriver.add
let add_alias = Deriver.add_alias
let invalid_with ~loc = Location.raise_errorf ~loc "invalid [@@deriving ] attribute syntax"
let generator_name_of_id loc id =
match Longident.flatten_exn id with
| l -> { loc; txt = String.concat ~sep:"." l }
| exception _ -> invalid_with ~loc:loc
;;
exception Unknown_syntax of Location.t * string
let parse_arguments l =
try
Args (
match l with
| [(Nolabel, e)] -> begin
match e.pexp_desc with
| Pexp_record (fields, None) ->
List.map fields ~f:(fun (id, expr) ->
let name =
match id.txt with
| Lident s -> s
| _ -> Exn.raise_without_backtrace
(Unknown_syntax
(id.loc, "simple identifier expected"))
in
(name, expr))
| _ ->
Exn.raise_without_backtrace
(Unknown_syntax
(e.pexp_loc, "non-optional labelled argument or record expected"))
end
| l ->
List.map l ~f:(fun (label, expr) ->
match label with
| Labelled s ->
(s, expr)
| _ ->
Exn.raise_without_backtrace
(Unknown_syntax
(expr.pexp_loc, "non-optional labelled argument expected"))))
with Unknown_syntax (loc, msg) ->
Unknown_syntax (loc, msg)
let mk_deriving_attr context ~prefix ~suffix =
Attribute.declare
(prefix ^ "deriving" ^ suffix)
context
Ast_pattern.(
let generator_name () =
map' (pexp_ident __) ~f:(fun loc f id -> f (generator_name_of_id loc id))
in
let generator () =
map (generator_name ()) ~f:(fun f x -> f (x, Args [])) |||
pack2 (pexp_apply (generator_name ()) (map1 (many __) ~f:parse_arguments))
in
let generators =
pexp_tuple (many (generator ())) |||
map (generator ()) ~f:(fun f x -> f [x])
in
pstr (pstr_eval generators nil ^:: nil)
)
(fun x -> x)
;;
module Attr = struct
let suffix = ""
let td = mk_deriving_attr ~prefix:"ppxlib." ~suffix Type_declaration
let te = mk_deriving_attr ~prefix:"ppxlib." ~suffix Type_extension
let ec = mk_deriving_attr ~prefix:"ppxlib." ~suffix Extension_constructor
module Expect = struct
let suffix = "_inline"
let td = mk_deriving_attr ~prefix:"ppxlib." ~suffix Type_declaration
let te = mk_deriving_attr ~prefix:"ppxlib." ~suffix Type_extension
let ec = mk_deriving_attr ~prefix:"ppxlib." ~suffix Extension_constructor
end
end
let disable_unused_warning_attribute ~loc =
({ txt = "ocaml.warning"; loc },
PStr [pstr_eval ~loc (estring ~loc "-32") []])
;;
let inline_doc_attr ~loc =
({ txt = "ocaml.doc"; loc },
PStr [pstr_eval ~loc (estring ~loc "@inline") []])
;;
let disable_unused_warning_str ~loc st =
if keep_w32_impl () then
st
else if not !do_insert_unused_warning_attribute then
Ignore_unused_warning.add_dummy_user_for_values#structure st
else
let include_infos =
include_infos ~loc
(pmod_structure ~loc
(pstr_attribute ~loc (disable_unused_warning_attribute ~loc) :: st))
in
[pstr_include ~loc
{include_infos with pincl_attributes = [inline_doc_attr ~loc]}]
;;
let disable_unused_warning_sig ~loc sg =
if keep_w32_intf () then
sg
else
let include_infos =
include_infos ~loc
(pmty_signature ~loc
(psig_attribute ~loc (disable_unused_warning_attribute ~loc) :: sg))
in
[psig_include ~loc
{ include_infos with pincl_attributes = [inline_doc_attr ~loc]}]
;;
let types_used_by_deriving (tds : type_declaration list)
: structure_item list =
if keep_w32_impl () then
[]
else
List.map tds ~f:(fun td ->
let typ = Common.core_type_of_type_declaration td in
let loc = td.ptype_loc in
pstr_value
~loc
Nonrecursive
[value_binding
~loc
~pat:(ppat_any ~loc)
~expr:(pexp_fun
~loc
Nolabel
None
(ppat_constraint ~loc (ppat_any ~loc) typ)
(eunit ~loc))]
)
let merge_generators field l =
List.filter_map l ~f:(fun x -> x)
|> List.concat
|> Deriver.resolve_all field
let expand_str_type_decls ~ctxt rec_flag tds values =
let generators = merge_generators Deriver.Field.str_type_decl values in
let generated =
types_used_by_deriving tds
@ Generator.apply_all ~ctxt (rec_flag, tds) generators;
in
disable_unused_warning_str ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let expand_sig_type_decls ~ctxt rec_flag tds values =
let generators = merge_generators Deriver.Field.sig_type_decl values in
let generated = Generator.apply_all ~ctxt (rec_flag, tds) generators in
disable_unused_warning_sig ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let expand_str_exception ~ctxt ec generators =
let generators = Deriver.resolve_all Deriver.Field.str_exception generators in
let generated = Generator.apply_all ~ctxt ec generators in
disable_unused_warning_str ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let expand_sig_exception ~ctxt ec generators =
let generators = Deriver.resolve_all Deriver.Field.sig_exception generators in
let generated = Generator.apply_all ~ctxt ec generators in
disable_unused_warning_sig ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let expand_str_type_ext ~ctxt te generators =
let generators = Deriver.resolve_all Deriver.Field.str_type_ext generators in
let generated = Generator.apply_all ~ctxt te generators in
disable_unused_warning_str ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let expand_sig_type_ext ~ctxt te generators =
let generators = Deriver.resolve_all Deriver.Field.sig_type_ext generators in
let generated = Generator.apply_all ~ctxt te generators in
disable_unused_warning_sig ~loc:(Expansion_context.Deriver.derived_item_loc ctxt) generated
let () =
Driver.register_transformation "deriving"
~aliases:["type_conv"]
~rules:[ Context_free.Rule.attr_str_type_decl
Attr.td
expand_str_type_decls
; Context_free.Rule.attr_sig_type_decl
Attr.td
expand_sig_type_decls
; Context_free.Rule.attr_str_type_ext
Attr.te
expand_str_type_ext
; Context_free.Rule.attr_sig_type_ext
Attr.te
expand_sig_type_ext
; Context_free.Rule.attr_str_exception
Attr.ec
expand_str_exception
; Context_free.Rule.attr_sig_exception
Attr.ec
expand_sig_exception
; Context_free.Rule.attr_str_type_decl_expect
Attr.Expect.td
expand_str_type_decls
; Context_free.Rule.attr_sig_type_decl_expect
Attr.Expect.td
expand_sig_type_decls
; Context_free.Rule.attr_str_type_ext_expect
Attr.Expect.te
expand_str_type_ext
; Context_free.Rule.attr_sig_type_ext_expect
Attr.Expect.te
expand_sig_type_ext
; Context_free.Rule.attr_str_exception_expect
Attr.Expect.ec
expand_str_exception
; Context_free.Rule.attr_sig_exception_expect
Attr.Expect.ec
expand_sig_exception
]
;;