Source file ppx_hardcaml_zero.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
open Base
open Ppxlib
open Ppxlib.Ast_builder.Default
let deriver = "hardcaml"
let raise_errorf = Location.raise_errorf
type options_t =
{ rtlprefix : expression option
; rtlsuffix : expression option
; rtlmangle : expression option
; ast : bool
}
let parse_rtlmangle expr ~loc =
match expr with
| [%expr true] -> Some [%expr "_"]
| [%expr false] -> None
| e -> Some e
;;
module Attribute : sig
type t
val find : t -> label_declaration -> expression option
val exists : t
val bits : t
val length : t
val rtlmangle : t
val rtlname : t
val rtlprefix : t
val rtlsuffix : t
val doc : t
end = struct
type t = (label_declaration, expression) Attribute.t
let find t label_declaration = Attribute.get t label_declaration
let create name =
Attribute.declare
name
Label_declaration
Ast_pattern.(pstr (pstr_eval __ nil ^:: nil))
(fun x -> x)
;;
let exists = create "exists"
let bits = create "bits"
let length = create "length"
let rtlmangle = create "rtlmangle"
let rtlname = create "rtlname"
let rtlprefix = create "rtlprefix"
let rtlsuffix = create "rtlsuffix"
let doc = create "hardcaml.ocaml.doc"
end
let get_bits ~loc label_declaration =
match Attribute.(find bits) label_declaration with
| Some expr -> expr
| None -> pexp_constant ~loc (Pconst_integer ("1", None))
;;
let get_length ~loc label_declaration =
match Attribute.(find length) label_declaration with
| Some expr -> expr
| None -> raise_errorf ~loc "[%s] length attribute must be set" deriver
;;
let field_name ~loc txt = pexp_constant ~loc (Pconst_string (txt, loc, None))
let get_rtlname ~loc txt label_declaration =
match Attribute.(find rtlname) label_declaration with
| Some expr -> expr
| None -> field_name ~loc txt
;;
let get_rtlprefix ~loc:_ opts label_declaration =
match Attribute.(find rtlprefix) label_declaration with
| Some expr -> Some expr
| None -> opts.rtlprefix
;;
let get_rtlsuffix ~loc:_ opts label_declaration =
match Attribute.(find rtlsuffix) label_declaration with
| Some expr -> Some expr
| None -> opts.rtlsuffix
;;
let get_rtlmangle ~loc opts label_declaration =
match Attribute.(find rtlmangle) label_declaration with
| Some expr -> parse_rtlmangle expr ~loc
| None -> opts.rtlmangle
;;
let get_doc ~loc label_declaration =
match Attribute.(find doc) label_declaration with
| Some expr ->
(match expr.pexp_desc with
| Pexp_constant (Pconst_string (str, _, _)) -> Some str
| _ -> raise_errorf ~loc "[%s] doc atttribute must be a string" deriver)
| None -> None
;;
let mk_rtlident ~loc name prefix suffix =
match prefix, suffix with
| None, None -> [%expr [%e name]]
| Some pre, None -> [%expr Ppx_hardcaml_runtime.concat [ [%e pre]; [%e name] ]]
| None, Some suf -> [%expr Ppx_hardcaml_runtime.concat [ [%e name]; [%e suf] ]]
| Some pre, Some suf ->
[%expr Ppx_hardcaml_runtime.concat [ [%e pre]; [%e name]; [%e suf] ]] [@metaloc loc]
;;
let mangle_name ~loc name mangle =
match mangle with
| Some separator ->
[%expr Ppx_hardcaml_runtime.concat [ [%e name]; [%e separator]; _n ]]
| None -> [%expr _n]
;;
let expand_array_init ~loc vname label_declaration =
let nbits = get_bits ~loc label_declaration in
let length = get_length ~loc label_declaration in
[%expr
Ppx_hardcaml_runtime.Array.init [%e length] ~f:(fun _i ->
( Ppx_hardcaml_runtime.concat [ [%e vname]; Ppx_hardcaml_runtime.Int.to_string _i ]
, [%e nbits] ))]
;;
let expand_array_init_str ~loc vname mapid mid label_declaration =
let length = get_length ~loc label_declaration in
[%expr
Ppx_hardcaml_runtime.Array.init [%e length] ~f:(fun _i ->
[%e mapid] [%e pexp_ident ~loc mid] ~f:(fun (_n, _b) -> [%e vname], _b))]
;;
let expand_port_names_and_widths_label_array
var
loc
label_declaration
name
prefix
suffix
mangle
= function
| Ptyp_var v when String.equal v var ->
let rtlident = mk_rtlident ~loc name prefix suffix in
expand_array_init ~loc rtlident label_declaration
| Ptyp_constr ({ txt = Ldot (mname, _); loc }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mid = { txt = Ldot (mname, "port_names_and_widths"); loc } in
let mangled =
[%expr
Ppx_hardcaml_runtime.concat
[ [%e mangle_name ~loc name mangle]; Ppx_hardcaml_runtime.Int.to_string _i ]]
in
let rtlident = mk_rtlident ~loc mangled prefix suffix in
let mapid = pexp_ident ~loc (Located.mk ~loc (Ldot (mname, "map"))) in
expand_array_init_str ~loc rtlident mapid mid label_declaration
| _ ->
raise_errorf
~loc
"[%s] expand_port_names_and_widths_label_array: only supports abstract record \
labels"
deriver
;;
let expand_port_names_and_widths_label_list
var
loc
label_declaration
name
prefix
suffix
mangle
desc
=
let ainit =
expand_port_names_and_widths_label_array
var
loc
label_declaration
name
prefix
suffix
mangle
desc
in
[%expr Ppx_hardcaml_runtime.Array.to_list [%e ainit]]
;;
let expand_port_names_and_widths_expresion
opts
var
({ pld_name = { txt; loc; _ }; _ } as label_declaration)
ptyp_desc
=
let rtlname = get_rtlname ~loc txt label_declaration
and rtlprefix = get_rtlprefix ~loc opts label_declaration
and rtlsuffix = get_rtlsuffix ~loc opts label_declaration
and rtlmangle = get_rtlmangle ~loc opts label_declaration in
match ptyp_desc with
| Ptyp_var v when String.equal v var ->
let nbits = get_bits ~loc label_declaration
and rtlident = mk_rtlident ~loc rtlname rtlprefix rtlsuffix in
pexp_tuple ~loc [ rtlident; nbits ]
| Ptyp_constr ({ txt = Ldot (mname, _); loc }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mid = { txt = Ldot (mname, "port_names_and_widths"); loc } in
let mangled = mangle_name ~loc rtlname rtlmangle in
let rtlident = mk_rtlident ~loc mangled rtlprefix rtlsuffix in
let mapid = pexp_ident ~loc (Located.mk ~loc (Ldot (mname, "map"))) in
[%expr [%e mapid] [%e pexp_ident ~loc mid] ~f:(fun (_n, _b) -> [%e rtlident], _b)]
| Ptyp_constr ({ txt = Lident "list"; _ }, [ { ptyp_desc; _ } ]) ->
expand_port_names_and_widths_label_list
var
loc
label_declaration
rtlname
rtlprefix
rtlsuffix
rtlmangle
ptyp_desc
| Ptyp_constr ({ txt = Lident "array"; _ }, [ { ptyp_desc; _ } ]) ->
expand_port_names_and_widths_label_array
var
loc
label_declaration
rtlname
rtlprefix
rtlsuffix
rtlmangle
ptyp_desc
| _ ->
raise_errorf
~loc
"[%s] expand_port_names_and_widths_label: only supports abstract record labels"
deriver
;;
let expand_port_names_and_widths_label
opts
var
({ pld_name = { txt; loc }; _ } as label_declaration)
=
let expand_inner_expression ptyp_desc =
expand_port_names_and_widths_expresion opts var label_declaration ptyp_desc
in
let expr =
match label_declaration.pld_type.ptyp_desc with
| Ptyp_constr ({ txt = Lident "option"; _ }, [ { ptyp_desc; _ } ]) ->
let exists =
match Attribute.(find exists) label_declaration with
| Some exists -> exists
| None -> raise_errorf ~loc "[%s] exists attribute must be in [option]" deriver
in
[%expr if [%e exists] then Some [%e expand_inner_expression ptyp_desc] else None]
| ptyp_desc -> expand_inner_expression ptyp_desc
in
Located.mk ~loc (Lident txt), expr
;;
let mkfield var memb =
let loc = Location.none in
pexp_field
~loc
(pexp_ident ~loc (Located.mk ~loc (Lident var)))
(Located.mk ~loc (Lident memb))
;;
module Iter_or_map = struct
type t =
| Iter
| Map
let name = function
| Iter -> "iter"
| Map -> "map"
;;
let name2 = function
| Iter -> "iter2"
| Map -> "map2"
;;
let option_map t loc =
match t with
| Iter -> [%expr Base.Option.iter]
| Map -> [%expr Base.Option.map]
;;
let option_map2_exn t loc =
match t with
| Iter -> [%expr Ppx_hardcaml_runtime.option_iter2_exn]
| Map -> [%expr Ppx_hardcaml_runtime.option_map2_exn]
;;
let array_map t loc =
match t with
| Iter -> [%expr Ppx_hardcaml_runtime.Array.iter]
| Map -> [%expr Ppx_hardcaml_runtime.Array.map]
;;
let array_init t loc =
match t with
| Iter -> [%expr Ppx_hardcaml_runtime.Array.for_]
| Map -> [%expr Ppx_hardcaml_runtime.Array.init]
;;
let list_map t loc =
match t with
| Iter -> [%expr Ppx_hardcaml_runtime.List.iter]
| Map -> [%expr Ppx_hardcaml_runtime.List.map]
;;
let list_map2_exn t loc =
match t with
| Iter -> [%expr Ppx_hardcaml_runtime.List.iter2_exn]
| Map -> [%expr Ppx_hardcaml_runtime.List.map2_exn]
;;
end
let expand_map_label_list iter_or_map var loc ident = function
| Ptyp_var v when String.equal v var ->
[%expr [%e Iter_or_map.list_map iter_or_map loc] [%e ident] ~f]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name iter_or_map)))
in
[%expr
[%e Iter_or_map.list_map iter_or_map loc] [%e ident] ~f:(fun _e -> [%e mapid] _e ~f)]
| _ ->
raise_errorf
~loc
"[%s] expand_map_label_list: only supports abstract record labels"
deriver
;;
let expand_map_label_array iter_or_map var loc ident = function
| Ptyp_var v when String.equal v var ->
[%expr [%e Iter_or_map.array_map iter_or_map loc] [%e ident] ~f]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name iter_or_map)))
in
[%expr
[%e Iter_or_map.array_map iter_or_map loc] [%e ident] ~f:(fun _e ->
[%e mapid] _e ~f)]
| _ ->
raise_errorf
~loc
"[%s] expand_map_label_list: only supports abstract record labels"
deriver
;;
module Record_field = struct
type t =
{ label_declaration : label_declaration
; expression : expression
}
let expression t = t.expression
let label_text t = t.label_declaration.pld_name.txt
end
let expand_map_label_expression (iter_or_map : Iter_or_map.t) var loc ptyp_desc ident =
match ptyp_desc with
| Ptyp_var v when String.equal v var -> [%expr f [%e ident]]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name iter_or_map)))
in
[%expr [%e mapid] [%e ident] ~f]
| Ptyp_constr ({ txt = Lident "list"; _ }, [ { ptyp_desc; _ } ]) ->
expand_map_label_list iter_or_map var loc ident ptyp_desc
| Ptyp_constr ({ txt = Lident "array"; _ }, [ { ptyp_desc; _ } ]) ->
expand_map_label_array iter_or_map var loc ident ptyp_desc
| _ ->
raise_errorf
~loc
"[%s] expand_map_label: only supports abstract record labels"
deriver
;;
let expand_map_label iter_or_map var ({ pld_name = { txt; loc }; _ } as label_declaration)
: Record_field.t
=
let expand_inner_expression ptyp_desc x =
expand_map_label_expression iter_or_map var loc ptyp_desc x
in
let ident = mkfield "x" txt in
let expression =
match label_declaration.pld_type.ptyp_desc with
| Ptyp_constr ({ txt = Lident "option"; _ }, [ { ptyp_desc; _ } ]) ->
[%expr
[%e Iter_or_map.option_map iter_or_map loc]
~f:(fun x -> [%e expand_inner_expression ptyp_desc [%expr x]])
[%e ident]]
| ptyp_desc -> expand_inner_expression ptyp_desc ident
in
{ label_declaration; expression }
;;
let expand_map2_label_list iter_or_map var loc ident0 ident1 = function
| Ptyp_var v when String.equal v var ->
[%expr [%e Iter_or_map.list_map2_exn iter_or_map loc] [%e ident0] [%e ident1] ~f]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name2 iter_or_map)))
in
[%expr
[%e Iter_or_map.list_map2_exn iter_or_map loc]
[%e ident0]
[%e ident1]
~f:(fun _e0 _e1 -> [%e mapid] _e0 _e1 ~f)]
| _ ->
raise_errorf
~loc
"[%s] expand_map_label_list: only supports abstract record labels"
deriver
;;
let expand_map2_label_array iter_or_map var loc ident0 ident1 = function
| Ptyp_var v when String.equal v var ->
[%expr
[%e Iter_or_map.array_init iter_or_map loc]
(Array.length [%e ident0])
~f:(fun _i ->
f
(Ppx_hardcaml_runtime.Array.get [%e ident0] _i)
(Ppx_hardcaml_runtime.Array.get [%e ident1] _i))]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name2 iter_or_map)))
in
[%expr
[%e Iter_or_map.array_init iter_or_map loc]
(Array.length [%e ident0])
~f:(fun _i ->
[%e mapid]
(Ppx_hardcaml_runtime.Array.get [%e ident0] _i)
(Ppx_hardcaml_runtime.Array.get [%e ident1] _i)
~f)]
| _ ->
raise_errorf
~loc
"[%s] expand_map_label_list: only supports abstract record labels"
deriver
;;
let expand_map2_label_expression
(iter_or_map : Iter_or_map.t)
var
loc
(ptyp_desc : Ppxlib.core_type_desc)
ident0
ident1
=
match ptyp_desc with
| Ptyp_var v when String.equal v var -> [%expr f [%e ident0] [%e ident1]]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let mapid =
pexp_ident ~loc (Located.mk ~loc (Ldot (mname, Iter_or_map.name2 iter_or_map)))
in
[%expr [%e mapid] [%e ident0] [%e ident1] ~f]
| Ptyp_constr ({ txt = Lident "list"; _ }, [ { ptyp_desc; _ } ]) ->
expand_map2_label_list iter_or_map var loc ident0 ident1 ptyp_desc
| Ptyp_constr ({ txt = Lident "array"; _ }, [ { ptyp_desc; _ } ]) ->
expand_map2_label_array iter_or_map var loc ident0 ident1 ptyp_desc
| _ ->
raise_errorf
~loc
"[%s] expand_map2_label: only supports abstract record labels"
deriver
;;
let expand_map2_label
iter_or_map
var
({ pld_name = { txt; loc }; _ } as label_declaration)
: Record_field.t
=
let expand_inner_expression ptyp_desc x0 x1 =
expand_map2_label_expression iter_or_map var loc ptyp_desc x0 x1
in
let ident0 = mkfield "x0" txt in
let ident1 = mkfield "x1" txt in
let expression =
match label_declaration.pld_type.ptyp_desc with
| Ptyp_constr ({ txt = Lident "option"; _ }, [ { ptyp_desc; _ } ]) ->
[%expr
[%e Iter_or_map.option_map2_exn iter_or_map loc]
~f:(fun x0 x1 -> [%e expand_inner_expression ptyp_desc [%expr x0] [%expr x1]])
[%e ident0]
[%e ident1]]
| ptyp_desc -> expand_inner_expression ptyp_desc ident0 ident1
in
{ label_declaration; expression }
;;
let expand_to_list_label_list var loc ident = function
| Ptyp_var v when String.equal v var -> ident
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
let to_list_id = pexp_ident ~loc (Located.mk ~loc (Ldot (mname, "to_list"))) in
[%expr
Ppx_hardcaml_runtime.List.concat
(Ppx_hardcaml_runtime.List.map [%e ident] ~f:(fun _e -> [%e to_list_id] _e))]
| _ ->
raise_errorf
~loc
"[%s] expand_map_label_list: only supports abstract record labels"
deriver
;;
let expand_to_list_label_array var loc ident desc =
expand_to_list_label_list
var
loc
[%expr Ppx_hardcaml_runtime.Array.to_list [%e ident]]
desc
;;
let expand_to_list_label_expression var loc ptyp_desc ident =
match ptyp_desc with
| Ptyp_var v when String.equal v var -> [%expr [ [%e ident] ]]
| Ptyp_constr ({ txt = Lident "option"; _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var -> [%expr Base.Option.to_list [%e ident]]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var ->
pexp_apply
~loc
(pexp_ident ~loc (Located.mk ~loc (Ldot (mname, "to_list"))))
[ Nolabel, ident ]
| Ptyp_constr ({ txt = Lident "list"; _ }, [ { ptyp_desc; _ } ]) ->
expand_to_list_label_list var loc ident ptyp_desc
| Ptyp_constr ({ txt = Lident "array"; _ }, [ { ptyp_desc; _ } ]) ->
expand_to_list_label_array var loc ident ptyp_desc
| _ ->
raise_errorf
~loc
"[%s] expand_to_list_label: only supports abstract record labels"
deriver
;;
let expand_to_list_label var ({ pld_name = { txt; loc }; _ } as label_declaration) =
let ident = mkfield "x" txt in
let expand_inner_expression ptyp_desc x =
expand_to_list_label_expression var loc ptyp_desc x
in
match label_declaration.pld_type.ptyp_desc with
| Ptyp_constr ({ txt = Lident "option"; _ }, [ { ptyp_desc; _ } ]) ->
[%expr
match [%e ident] with
| None -> []
| Some x -> [%e expand_inner_expression ptyp_desc [%expr x]]]
| ptyp_desc -> expand_inner_expression ptyp_desc ident
;;
let build_expr_list labels =
let loc = Location.none in
List.fold_right
labels
~f:(fun expr acc ->
pexp_construct
~loc
(Located.mk ~loc (Lident "::"))
(Some (pexp_tuple ~loc [ expr; acc ])))
~init:(pexp_construct ~loc (Located.mk ~loc (Lident "[]")) None)
;;
let expand_ast_label
opts
var
({ pld_name = { txt; loc; _ }; pld_type; _ } as label_declaration)
=
let expand_expr ptyp_desc =
let rtlname = get_rtlname ~loc txt label_declaration
and rtlprefix = get_rtlprefix ~loc opts label_declaration
and rtlsuffix =
get_rtlsuffix ~loc opts label_declaration
in
let signal () =
let rtlident = mk_rtlident ~loc rtlname rtlprefix rtlsuffix in
let bits = get_bits ~loc label_declaration in
[%expr Signal { bits = [%e bits]; rtlname = [%e rtlident] }]
in
let module_ mname =
let ast = pexp_ident ~loc (Located.mk ~loc (Ldot (mname, "ast"))) in
let mname =
let mname = Longident.flatten_exn mname |> String.concat ~sep:"." in
pexp_constant ~loc (Pconst_string (mname, loc, None))
in
[%expr Module { name = [%e mname]; ast = [%e ast] }]
in
let sequence kind =
let length = get_length ~loc label_declaration in
[%expr Some { kind = [%e kind]; length = [%e length] }]
in
let type_, sequence =
match ptyp_desc with
| Ptyp_var v when String.equal v var -> signal (), [%expr None]
| Ptyp_constr ({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var -> module_ mname, [%expr None]
| Ptyp_constr ({ txt = Lident "list"; _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var -> signal (), sequence [%expr List]
| Ptyp_constr
( { txt = Lident "list"; _ }
, [ { ptyp_desc =
Ptyp_constr
({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
; _
}
] )
when String.equal v var -> module_ mname, sequence [%expr List]
| Ptyp_constr ({ txt = Lident "array"; _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
when String.equal v var -> signal (), sequence [%expr Array]
| Ptyp_constr
( { txt = Lident "array"; _ }
, [ { ptyp_desc =
Ptyp_constr
({ txt = Ldot (mname, _); _ }, [ { ptyp_desc = Ptyp_var v; _ } ])
; _
}
] )
when String.equal v var -> module_ mname, sequence [%expr Array]
| _ ->
raise_errorf
~loc
"[%s] expand_doc_label: only supports abstract record labels"
deriver
in
let field_name = field_name ~loc txt in
let doc =
match get_doc ~loc label_declaration with
| None -> [%expr None]
| Some doc -> [%expr Some [%e pexp_constant ~loc (Pconst_string (doc, loc, None))]]
in
[%expr
{ Ppx_hardcaml_runtime.Interface.Ast.Field.name = [%e field_name]
; type_ = [%e type_]
; sequence = [%e sequence]
; doc = [%e doc]
}]
in
expand_expr pld_type.ptyp_desc
;;
let pexp_sequenceN ~loc exprs =
match List.rev exprs with
| [] -> [%expr ()]
| [ e ] -> e
| last :: es -> List.fold es ~init:last ~f:(fun ac e -> pexp_sequence ~loc e ac)
;;
let record_fields (iter_or_map : Iter_or_map.t) ~loc fields =
match iter_or_map with
| Iter -> pexp_sequenceN ~loc (List.map fields ~f:Record_field.expression)
| Map ->
pexp_let
~loc
Nonrecursive
(List.map fields ~f:(fun record_field ->
value_binding
~loc
~pat:(pvar ~loc (Record_field.label_text record_field))
~expr:record_field.expression))
(pexp_record
~loc
(List.map fields ~f:(fun record_field ->
let id = Located.mk ~loc (Lident (Record_field.label_text record_field)) in
id, pexp_ident ~loc id))
None)
;;
let str_of_type ~options ({ ptype_loc = loc; _ } as type_decl) =
match type_decl.ptype_kind, type_decl.ptype_params with
| Ptype_record labels, [ ({ ptyp_desc = Ptyp_var var; _ }, _) ] ->
let str_port_names_and_widths_labels =
List.map labels ~f:(expand_port_names_and_widths_label options var)
in
let str_port_names_and_widths =
pexp_record ~loc str_port_names_and_widths_labels None
in
let str_map iter_or_map =
let fields = List.map labels ~f:(expand_map_label iter_or_map var) in
[%expr fun x ~f -> [%e record_fields iter_or_map ~loc fields]]
in
let str_map2 iter_or_map =
let fields = List.map labels ~f:(expand_map2_label iter_or_map var) in
[%expr fun x0 x1 ~f -> [%e record_fields iter_or_map ~loc fields]]
in
let str_to_list_labels = List.map labels ~f:(expand_to_list_label var) in
let str_to_list_args = build_expr_list str_to_list_labels in
let str_to_list =
[%expr fun x -> Ppx_hardcaml_runtime.List.concat [%e str_to_list_args]]
in
let str_ast_labels () = List.map labels ~f:(expand_ast_label options var) in
let str_ast () = build_expr_list (str_ast_labels ()) in
[ pstr_value
~loc
Nonrecursive
([ value_binding
~loc
~pat:(pvar ~loc "port_names_and_widths")
~expr:str_port_names_and_widths
; value_binding ~loc ~pat:(pvar ~loc "iter") ~expr:(str_map Iter)
; value_binding ~loc ~pat:(pvar ~loc "iter2") ~expr:(str_map2 Iter)
; value_binding ~loc ~pat:(pvar ~loc "map") ~expr:(str_map Map)
; value_binding ~loc ~pat:(pvar ~loc "map2") ~expr:(str_map2 Map)
; value_binding ~loc ~pat:(pvar ~loc "to_list") ~expr:str_to_list
]
@
if options.ast
then [ value_binding ~loc ~pat:(pvar ~loc "ast") ~expr:(str_ast ()) ]
else [])
; [%stri
include Ppx_hardcaml_runtime.Interface.Make (struct
type nonrec 'a t = 'a t
let sexp_of_t = sexp_of_t
let port_names_and_widths = port_names_and_widths
let iter = iter
let iter2 = iter2
let map = map
let map2 = map2
let to_list = to_list
end)]
]
| _ -> raise_errorf ~loc "[%s] str_of_type: only supports record types" deriver
;;
let sig_of_type ~ast ({ ptype_loc = loc; _ } as type_decl) =
match type_decl.ptype_kind, type_decl.ptype_params with
| Ptype_record _, [ ({ ptyp_desc = Ptyp_var _; _ }, _) ] ->
let intf = [%sigi: include Ppx_hardcaml_runtime.Interface.S with type 'a t := 'a t] in
if ast
then [ intf; [%sigi: val ast : Ppx_hardcaml_runtime.Interface.Ast.t] ]
else [ intf ]
| _, _ -> raise_errorf ~loc "[%s] sig_of_type: only supports record types" deriver
;;
let declare_let_binding_extension ~name ~generate_naming_function =
let pattern =
Ast_pattern.(single_expr_payload (pexp_let __' __ __))
in
Extension.declare_with_path_arg
name
Expression
pattern
(fun ~loc ~path:_ ~arg recursive_flag bindings rhs ->
(match recursive_flag.txt with
| Recursive ->
Location.raise_errorf ~loc:recursive_flag.loc "[let rec] not supported."
| Nonrecursive -> ());
let bindings =
List.map bindings ~f:(fun { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ->
let loc = { pvb_loc with loc_ghost = true } in
match pvb_pat.ppat_desc with
| Ppat_var { txt; loc = _ } ->
{ pvb_pat
; pvb_expr =
[%expr [%e generate_naming_function ~arg ~loc ~name:txt] [%e pvb_expr]]
; pvb_attributes
; pvb_loc
}
| _ ->
Location.raise_errorf
~loc:pvb_pat.ppat_loc
"This form of let binding is not currently supported")
in
pexp_let ~loc Nonrecursive bindings rhs)
;;
let hardcaml_name () =
declare_let_binding_extension
~name:"hw"
~generate_naming_function:(fun ~arg ~loc ~name ->
match arg with
| None ->
[%expr
fun signal_to_name ->
Hardcaml.Scope.naming scope signal_to_name [%e estring ~loc name]]
| Some { loc = _; txt = module_of_type_of_expression_being_named } ->
let apply_names =
String.concat
~sep:"."
(Longident.flatten_exn module_of_type_of_expression_being_named
@ [ "apply_names" ])
in
[%expr
fun intf_to_name ->
let (_ : _) =
[%e evar ~loc apply_names]
~prefix:[%e estring ~loc (name ^ "$")]
~naming_op:(Hardcaml.Scope.naming scope)
intf_to_name
in
intf_to_name])
;;
let hardcaml_name_var () =
declare_let_binding_extension
~name:"hw_var"
~generate_naming_function:(fun ~arg ~loc ~name ->
match arg with
| None ->
[%expr
fun (variable_to_name : Hardcaml.Always.Variable.t) ->
let (_ : Hardcaml.Signal.t) =
Hardcaml.Scope.naming scope variable_to_name.value [%e estring ~loc name]
in
variable_to_name]
| Some _ ->
Location.raise_errorf
~loc
"[hw_var] does not take a module argument. It is only used with plain \
[Variable.t]s - use [let%%hw.Your_type_here.Of_always] instead")
;;
let () =
let get_bool_option ~loc option = Option.bind option ~f:(parse_rtlmangle ~loc) in
let hardcaml_internal =
Deriving.add
"hardcaml_internal"
~str_type_decl:
(Deriving.Generator.make
Deriving.Args.(
empty
+> arg "rtlprefix" Ast_pattern.__
+> arg "rtlsuffix" Ast_pattern.__
+> arg "rtlmangle" Ast_pattern.__
+> flag "ast")
(fun ~loc ~path:_ (_, type_declarations) rtlprefix rtlsuffix rtlmangle ast ->
let options =
{ rtlprefix; rtlsuffix; rtlmangle = get_bool_option ~loc rtlmangle; ast }
in
List.concat_map type_declarations ~f:(fun decl -> str_of_type ~options decl)))
~sig_type_decl:
(Deriving.Generator.make
Deriving.Args.(empty +> flag "ast")
(fun ~loc:_ ~path:_ (_, type_declarations) ast ->
List.concat_map type_declarations ~f:(sig_of_type ~ast)))
in
Deriving.add_alias deriver [ hardcaml_internal; Ppx_sexp_conv.sexp_of ]
|> Deriving.ignore;
Driver.register_transformation
"hardcaml_naming"
~rules:
[ Context_free.Rule.extension (hardcaml_name ())
; Context_free.Rule.extension (hardcaml_name_var ())
]
;;