package ctypes

  1. Overview
  2. Docs
Combinators for binding to C libraries without writing any C

Install

Dune Dependency

Authors

Maintainers

Sources

0.23.0.tar.gz
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a

doc/src/ctypes.stubs/cstubs_generate_ml.ml.html

Source file cstubs_generate_ml.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
(*
 * Copyright (c) 2014 Jeremy Yallop.
 *
 * This file is distributed under the terms of the MIT License.
 * See the file LICENSE for details.
 *)

(* ML stub generation *)

[@@@warning "-9-27"]

open Ctypes_static
open Ctypes_path
open Cstubs_errors

type non_lwt = [ `Sequential | `Unlocked ]
type lwt = [ `Lwt_jobs | `Lwt_preemptive ]
type concurrency_policy = [ non_lwt | lwt ]
type errno_policy = [ `Ignore_errno | `Return_errno ]

type lident = string
type ml_type = [ `Ident of path
               | `Appl of path * ml_type list
               | `Pair of ml_type * ml_type
               | `Fn of ml_type * ml_type ]

type ml_external_type = [ `Prim of ml_type list * ml_type ]

type ml_pat = [ `Var of string
              | `Record of (path * ml_pat) list * [`Etc | `Complete]
              | `As of ml_pat * string
              | `Underscore
              | `Con of path * ml_pat list ]

type ml_exp = [ `Ident of path 
              | `Project of ml_exp * path
              | `MakePtr of ml_exp * ml_exp
              | `MakeFunPtr of ml_exp * ml_exp
              | `MakeStructured of ml_exp * ml_exp
              | `Appl of ml_exp * ml_exp
              | `Tuple of ml_exp list
              | `Seq of ml_exp * ml_exp
              | `Let of ml_pat * ml_exp * ml_exp
              | `Unit
              | `Fun of lident list * ml_exp ]

type attributes = { float: bool; noalloc: bool }

type extern = {
  ident : string;
  typ: ml_external_type;
  primname: string;
  primname_byte: string option;
  attributes: attributes;
}

module Emit_ML : sig 
  type appl_parens = ApplParens | NoApplParens
  val ml_exp : appl_parens -> Format.formatter -> ml_exp -> unit
  val ml_pat : appl_parens -> Format.formatter -> ml_pat -> unit
  val ml_external_type : Format.formatter -> ml_external_type -> unit
  val extern : Format.formatter -> extern -> unit
end =
struct
  let fprintf = Format.fprintf

  (* We (only) need to parenthesize function types in certain contexts 
        * on the lhs of a function type: - -> t
        * as the argument to a single-argument type constructor: - t
  *)
  type arrow_parens = ArrowParens | NoArrowParens

  (* We (only) need to parenthesize application expressions in certain contexts 
        * in a projection expression: -.l
        * in a dereference expression: !@ -
        * as an argument in an application: e -
  *)
  type appl_parens = ApplParens | NoApplParens

  let ident = format_path

  let rec ml_type arrow_parens fmt t =
    match arrow_parens, t with
    | _, `Ident i -> ident fmt i
    | _, `Appl (t, []) -> ident fmt t
    | _, `Appl (t, [t']) ->
      fprintf fmt "@[%a@ %a@]" (ml_type ArrowParens) t' ident t
    | _, `Appl (t, ts) ->
      let nargs = List.length ts in
      fprintf fmt "(";
      List.iteri
        (fun i arg ->
          if i = nargs - 1 then (ml_type NoArrowParens) fmt arg
          else fprintf fmt "%a,@ " (ml_type NoArrowParens) arg
        ) ts;
      fprintf fmt ")@ %a" ident t;
    | ArrowParens, `Fn (t, t') ->
      fprintf fmt "@[(%a@ ->@ %a)@]" (ml_type ArrowParens) t (ml_type NoArrowParens) t'
    | NoArrowParens, `Fn (t, t') ->
      fprintf fmt "@[%a@ ->@]@ %a" (ml_type ArrowParens) t (ml_type NoArrowParens) t'
    | _, `Pair (t, t') ->
      fprintf fmt "@[(%a@ *@ %a)@]" (ml_type NoArrowParens) t (ml_type NoArrowParens) t'

  let ml_external_type fmt (`Prim (args, ret) : ml_external_type) =
    List.iter (fprintf fmt "@[%a@ ->@]@ " (ml_type ArrowParens)) args;
    ml_type ArrowParens fmt ret

  let primname_opt fmt = function
    | None -> ()
    | Some primname -> fprintf fmt "%S@ " primname

  let attrs fmt { float; noalloc } =
    begin 
    (* TODO: float support not yet implemented *)
    (* if float then pp_print_string fmt "\"float\""; *)

    (* TODO: fix this.  The may_allocate function determines whether any of
       the functions in the generated C cause OCaml heap allocations.
       However, it doesn't currently account for callbacks: if we pass a
       handle to an OCaml function into C, calling the function can trigger an
       allocation.  We need some way in the interface of the library for the
       client to indicate whether it is safe to assume that a C function
       cannot call back into OCaml. *)
    (* if noalloc then pp_print_string fmt "\"noalloc\"" *)
    end

  let args fmt xs =
    List.iter (fprintf fmt "%s@ ") xs

  let rec ml_exp appl_parens fmt (e : ml_exp) =
    match appl_parens, e with
    | _, `Unit -> fprintf fmt "()"
    | _, `Ident x -> ident fmt x
    | _, `Project (e, l) -> fprintf fmt "%a.%a" (ml_exp ApplParens) e ident l
    | ApplParens, `Appl (f, p) -> fprintf fmt "@[(%a@;<1 2>%a)@]" (ml_exp NoApplParens) f (ml_exp ApplParens) p
    | NoApplParens, `Appl (f, p) -> fprintf fmt "@[%a@ %a@]" (ml_exp NoApplParens) f (ml_exp ApplParens) p
    | ApplParens, `MakePtr (t, e) ->
      fprintf fmt
        "(@[<hov 2>CI.make_ptr@ %a@ %a)@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | NoApplParens, `MakePtr (t, e) ->
      fprintf fmt
        "@[<hov 2>CI.make_ptr@ %a@ %a@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | ApplParens, `MakeFunPtr (t, e) ->
      fprintf fmt
        "(@[<hov 2>CI.make_fun_ptr@ %a@ %a)@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | NoApplParens, `MakeFunPtr (t, e) ->
      fprintf fmt
        "@[<hov 2>CI.make_fun_ptr@ %a@ %a@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | ApplParens, `MakeStructured (t, e) ->
      fprintf fmt
        "(@[<hov 2>CI.make_structured@ %a@ %a)@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | NoApplParens, `MakeStructured (t, e) ->
      fprintf fmt
        "@[<hov 2>CI.make_structured@ %a@ %a@]" (ml_exp ApplParens) t (ml_exp ApplParens) e
    | _, `Fun (xs, e) ->
      fprintf fmt "(@[<1>fun@ %a->@ %a)@]" args xs (ml_exp NoApplParens) e
    | _, `Tuple es ->
      fprintf fmt "(@[%a)@]" tuple_elements es
    | _, `Seq (e1, e2) ->
      fprintf fmt "(@[%a;@ %a)@]" (ml_exp NoApplParens) e1 (ml_exp NoApplParens) e2
    | ApplParens, `Let (p, e1, e2) ->
      fprintf fmt "(@[let@ %a@ = %a@ in@ %a)@]" (ml_pat NoApplParens) p (ml_exp NoApplParens) e1 (ml_exp NoApplParens) e2
    | NoApplParens, `Let (p, e1, e2) ->
      fprintf fmt "@[let@ %a@ = %a@ in@ %a@]" (ml_pat NoApplParens) p (ml_exp NoApplParens) e1 (ml_exp NoApplParens) e2
  and tuple_elements fmt : ml_exp list -> unit =
    fun xs ->
      let last = List.length xs - 1 in
      List.iteri
        (fun i ->
          if i <> last then fprintf fmt "%a,@ " (ml_exp NoApplParens)
          else fprintf fmt "%a" (ml_exp NoApplParens))
        xs
  and ml_pat appl_parens fmt pat =
    match appl_parens, pat with
    | _, `Var x -> fprintf fmt "%s" x
    | _, `Record (fs, `Etc) -> fprintf fmt "{@[%a_}@]" pat_fields fs
    | _, `Record (fs, `Complete) -> fprintf fmt "{@[%a}@]" pat_fields fs
    | _, `As (p, x) -> fprintf fmt "@[(%a@ as@ %s)@]" (ml_pat NoApplParens) p x
    | _, `Underscore -> fprintf fmt "_"
    | _, `Con (c, []) -> fprintf fmt "%a" format_path c
    | NoApplParens, `Con (c, [p]) ->
      fprintf fmt "@[<2>%a@ @[%a@]@]" format_path c (ml_pat ApplParens) p
    | ApplParens, `Con (c, [p]) ->
      fprintf fmt "(@[<2>%a@ @[%a@])@]" format_path c (ml_pat ApplParens) p
    | ApplParens, `Con (c, ps) ->
      fprintf fmt "(@[<2>%a@ (@[%a)@])@]" format_path c pat_args ps
    | NoApplParens, `Con (c, ps) ->
      fprintf fmt "@[<2>%a@ (@[%a)@]@]" format_path c pat_args ps
  and pat_fields fmt : (path * ml_pat) list -> unit =
    List.iter
      (fun (l, p) ->
        fprintf fmt "@[%a@ =@ %a;@]@ " format_path l (ml_pat NoApplParens) p)
  and pat_args fmt : ml_pat list -> unit =
    fun xs ->
      let last = List.length xs - 1 in
      List.iteri
        (fun i ->
          if i <> last then fprintf fmt "%a,@ " (ml_pat NoApplParens)
          else fprintf fmt "%a" (ml_pat NoApplParens))
        xs

  let extern fmt { ident; typ; primname; primname_byte; attributes } =
    fprintf fmt
      "@[<hov 2>@[external@ %s@]@ @[<h 1>:@ @[%a@]@]@ "
      ident ml_external_type typ;
    fprintf fmt
      "@[=@ @[@[%a@]@[%S@]@ %a@]@]@]@."
      primname_opt primname_byte primname attrs attributes
end

let arity : ml_external_type -> int =
  fun (`Prim (args, _)) -> List.length args

let max_byte_args = 5

let byte_stub_name : string -> ml_external_type -> string option =
  fun name t ->
    let arity = arity t in
    if arity > max_byte_args
    then Some (Printf.sprintf "%s_byte%d" name arity)
    else None

let attributes : type a. a fn -> attributes =
   let open Cstubs_analysis in
   fun fn -> { float = float fn; noalloc = not (may_allocate fn) }

let managed_buffer = `Ident (path_of_string "CI.managed_buffer")
let voidp = `Ident (path_of_string "CI.voidp")
let fatptr = `Appl (path_of_string "CI.fatptr", [`Ident (path_of_string "_")])
let fatfunptr = `Appl (path_of_string "CI.fatfunptr", [`Ident (path_of_string "_")])

(* These functions determine the type that should appear in the extern
   signature *)
let rec ml_typ_of_return_typ : type a. a typ -> ml_type =
  function
  | Void -> `Ident (path_of_string "unit")
  | Primitive p -> `Ident (Cstubs_public_name.ident_of_ml_prim (Ctypes_primitive_types.ml_prim p))
  | Struct _    -> managed_buffer
  | Union _     -> managed_buffer
  | Abstract _  -> managed_buffer
  | Pointer _   -> voidp
  | Funptr _    -> voidp
  | View { ty } -> ml_typ_of_return_typ ty
  | Qualified (_, ty) -> ml_typ_of_return_typ ty
  | Array _    as a -> internal_error
    "Unexpected array type in the return type: %s" (Ctypes.string_of_typ a)
  | Bigarray _ as a -> internal_error
    "Unexpected bigarray type in the return type: %s" (Ctypes.string_of_typ a)
  | OCaml String -> Ctypes_static.unsupported
    "cstubs does not support OCaml strings as return values"
  | OCaml Bytes -> Ctypes_static.unsupported
    "cstubs does not support OCaml bytes values as return values"
  | OCaml FloatArray -> Ctypes_static.unsupported
    "cstubs does not support OCaml float arrays as return values"

let rec ml_typ_of_arg_typ : type a. a typ -> ml_type = function
  | Void -> `Ident (path_of_string "unit")
  | Primitive p -> `Ident (Cstubs_public_name.ident_of_ml_prim (Ctypes_primitive_types.ml_prim p))
  | Pointer _   -> fatptr
  | Funptr _    -> fatfunptr
  | Struct _    -> fatptr
  | Union _     -> fatptr
  | Abstract _  -> fatptr
  | View { ty } -> ml_typ_of_arg_typ ty
  | Qualified (_, ty) -> ml_typ_of_arg_typ ty
  | Array _    as a -> internal_error
    "Unexpected array in an argument type: %s" (Ctypes.string_of_typ a)
  | Bigarray _ as a -> internal_error
    "Unexpected bigarray in an argument type: %s" (Ctypes.string_of_typ a)
  | OCaml String ->
    `Appl (path_of_string "CI.ocaml",
           [`Ident (path_of_string "string")])
  | OCaml Bytes ->
    `Appl (path_of_string "CI.ocaml",
           [`Ident (path_of_string "bytes")])
  | OCaml FloatArray ->
    `Appl (path_of_string "CI.ocaml",
           [`Appl (path_of_string "array",
                   [`Ident (path_of_string "float")])])

type polarity = In | Out

let flip = function
  | In -> Out
  | Out -> In

let ml_typ_of_typ = function
    In -> ml_typ_of_arg_typ
  | Out -> ml_typ_of_return_typ

let lwt_job_type = Ctypes_path.path_of_string "Lwt_unix.job"
let int_type = `Ident (Ctypes_path.path_of_string "Signed.sint")

let rec ml_external_type_of_fn :
  type a. concurrency:concurrency_policy -> errno:errno_policy ->
  a fn -> polarity -> ml_external_type =
  fun ~concurrency ~errno fn polarity -> match fn, concurrency, errno with
    | Returns t, (#non_lwt|`Lwt_preemptive), `Ignore_errno ->
      `Prim ([], ml_typ_of_typ polarity t)
    | Returns t, (#non_lwt|`Lwt_preemptive), `Return_errno ->
      `Prim ([], `Pair (ml_typ_of_typ polarity t, int_type))
    | Returns t, `Lwt_jobs, `Ignore_errno ->
      `Prim ([], `Appl (lwt_job_type, [ml_typ_of_typ polarity t]))
    | Returns t, `Lwt_jobs, `Return_errno ->
      `Prim ([], `Appl (lwt_job_type, [`Pair (ml_typ_of_typ polarity t, int_type)]))
    | Function (f, t), _, _ ->
      let `Prim (l, t) = ml_external_type_of_fn ~concurrency ~errno t polarity in
      `Prim (ml_typ_of_typ (flip polarity) f :: l, t)

let var_counter = ref 0
let fresh_var () =
  incr var_counter;
  Printf.sprintf "x%d" !var_counter

let extern ~concurrency ~errno ~stub_name ~external_name fmt fn =
  let ext =
    let typ = ml_external_type_of_fn ~concurrency ~errno fn Out in
    ({ ident = external_name;
       typ = typ;
       primname = stub_name;
       primname_byte = byte_stub_name stub_name typ;
       attributes = attributes fn; }) in
  Format.fprintf fmt "%a@." Emit_ML.extern ext

let static_con c args =
  `Con (Ctypes_path.path_of_string ("CI." ^ c), args)

let local_con c args =
  `Con (Ctypes_path.path_of_string c, args)

let map_result_id = Ctypes_path.path_of_string "map_result"
let make_ptr = Ctypes_path.path_of_string "CI.make_ptr"
let make_fun_ptr = Ctypes_path.path_of_string "CI.make_fun_ptr"
let make_structured = Ctypes_path.path_of_string "CI.make_structured"

let map_result ~concurrency ~errno f e =
  let map_result f x = `Appl (`Appl (`Ident map_result_id, f), x) in
  match concurrency, errno, f with
    #non_lwt, `Ignore_errno, `MakePtr x ->
    `MakePtr (`Ident (path_of_string x), e)
  | #non_lwt, `Ignore_errno, `MakeFunPtr x ->
    `MakeFunPtr (`Ident (path_of_string x), e)
  | #non_lwt, `Ignore_errno, `MakeStructured x ->
    `MakeStructured (`Ident (path_of_string x), e)
  | #non_lwt, `Ignore_errno, `Appl x ->
    `Appl (`Ident (path_of_string x), e)
  | _, _, `MakePtr x ->
    map_result (`Appl (`Ident make_ptr, `Ident (path_of_string x))) e
  | _, _, `MakeFunPtr x ->
    map_result (`Appl (`Ident make_fun_ptr, `Ident (path_of_string x))) e
  | _, _, `MakeStructured x ->
    map_result (`Appl (`Ident make_structured, `Ident (path_of_string x))) e
  | _, _, `Appl x ->
    map_result (`Ident (path_of_string x)) e 

type pattern_exp_return = ml_pat * ml_exp option * (ml_pat * ml_exp) list

let rec pattern_and_exp_of_typ : type a. concurrency:concurrency_policy -> errno:errno_policy ->
  a typ -> ml_exp -> polarity -> (ml_pat * ml_exp) list -> pattern_exp_return =
  fun ~concurrency ~errno typ e pol binds -> match typ with
  | Void ->
    (static_con "Void" [], None, binds)
  | Primitive p ->
    let id = Cstubs_public_name.constructor_cident_of_prim ~module_name:"CI" p in
    (static_con "Primitive" [`Con (id, [])], None, binds)
  | Pointer _ ->
    begin match pol with
    | In ->
      let pat = static_con "Pointer" [`Underscore] in
      let x = fresh_var () in
      (pat, Some (`Ident (path_of_string x)), binds @ [static_con "CPointer" [`Var x], e])
    | Out ->
      let x = fresh_var () in
      let pat = static_con "Pointer" [`Var x] in
      (pat, Some (map_result ~concurrency ~errno (`MakePtr x) e), binds)
    end
  | Funptr _ ->
    begin match pol with
    | In ->
      let pat = static_con "Funptr" [`Underscore] in
      let x = fresh_var () in
      (pat, Some (`Ident (path_of_string x)), binds @ [static_con "Static_funptr" [`Var x], e])
    | Out ->
      let x = fresh_var () in
      let pat = static_con "Funptr" [`Var x] in
      (pat, Some (map_result ~concurrency ~errno (`MakeFunPtr x) e), binds)
    end
  | Struct _ ->
    begin match pol with
    | In ->
      let pat = static_con "Struct" [`Underscore] in
      let x = fresh_var () in
      (pat, Some (`Ident (path_of_string x)),
       binds @ [static_con "CPointer" [`Var x],
                `Appl (`Ident (path_of_string "Ctypes.addr"), e)])
    | Out ->
      let x = fresh_var () in
      let pat = `As (static_con "Struct" [`Underscore], x) in
      (pat, Some (map_result ~concurrency ~errno (`MakeStructured x) e), binds)
    end
  | Union _ ->
    begin match pol with
    | In ->
      let pat = static_con "Union" [`Underscore] in
      let x = fresh_var () in
      (pat, Some (`Ident (path_of_string x)),
       binds @ [static_con "CPointer" [`Var x],
                `Appl (`Ident (path_of_string "Ctypes.addr"), e)])
    | Out ->
      let x = fresh_var () in
      let pat = `As (static_con "Union" [`Underscore], x) in
      (pat, Some (map_result ~concurrency ~errno (`MakeStructured x) e), binds)
    end
  | View { ty } ->
    begin match pol  with
    | In ->
      let x = fresh_var () in
      let y = fresh_var () in
      let e = `Appl (`Ident (path_of_string x), e) in
      let (p, None, binds), e | (p, Some e, binds), _ =
        pattern_and_exp_of_typ ~concurrency ~errno ty e pol binds, e in
      let pat = static_con "View"
        [`Record ([path_of_string "CI.ty", p;
                   path_of_string "write", `Var x], `Etc)] in
      (pat, Some (`Ident (Ctypes_path.path_of_string y)), (`Var y, e) :: binds)
    | Out ->
      let (p, None, binds), e | (p, Some e, binds), _ =
        pattern_and_exp_of_typ ~concurrency ~errno ty e pol binds, e in
      let x = fresh_var () in
      let pat = static_con "View"
        [`Record ([path_of_string "CI.ty", p;
                   path_of_string "read", `Var x], `Etc)] in
      (pat, Some (map_result ~concurrency ~errno (`Appl x) e), binds)
    end
  | Qualified (_, ty) -> pattern_and_exp_of_typ ~concurrency ~errno ty e pol binds
  | OCaml ty ->
    begin match pol, ty with
    | In, String -> (static_con "OCaml" [static_con "String" []], None, binds)
    | In, Bytes -> (static_con "OCaml" [static_con "Bytes" []], None, binds)
    | In, FloatArray -> (static_con "OCaml" [static_con "FloatArray" []], None, binds)
    | Out, String -> Ctypes_static.unsupported
      "cstubs does not support OCaml strings as return values"
    | Out, Bytes -> Ctypes_static.unsupported
      "cstubs does not support OCaml bytes values as return values"
    | Out, FloatArray -> Ctypes_static.unsupported
      "cstubs does not support OCaml float arrays as return values"
    end
  | Abstract _ as ty -> internal_error
    "Unexpected abstract type encountered during ML code generation: %s"
    (Ctypes.string_of_typ ty)
  | Array _ as ty -> internal_error
    "Unexpected array type encountered during ML code generation: %s"
    (Ctypes.string_of_typ ty)
  | Bigarray _ as ty -> internal_error
    "Unexpected bigarray type encountered during ML code generation: %s"
    (Ctypes.string_of_typ ty)

(* Build a pattern (without variables) that matches the argument *)
let rec pattern_of_typ : type a. a typ -> ml_pat = function
    Void -> static_con "Void" []
  | Primitive p ->
    let id = Cstubs_public_name.constructor_cident_of_prim ~module_name:"CI" p in
    static_con "Primitive" [`Con (id, [])]
  | Pointer _ ->
    static_con "Pointer" [`Underscore]
  | Funptr _ ->
    static_con "Funptr" [`Underscore]
  | Struct _ ->
    static_con "Struct" [`Underscore]
  | Union _ ->
    static_con "Union" [`Underscore]
  | View { ty } ->
    static_con "View"
      [`Record ([path_of_string "CI.ty", pattern_of_typ ty], `Etc)]
  | Qualified (_, ty) ->
    static_con "Qualified" [`Underscore; pattern_of_typ ty]
  | Array (_, _) ->
     static_con "Array" [`Underscore; `Underscore]
  | Bigarray _ ->
     static_con "Bigarray" [`Underscore]
  | OCaml String ->
    Ctypes_static.unsupported
      "cstubs does not support OCaml strings as global values"
  | OCaml Bytes ->
    Ctypes_static.unsupported
      "cstubs does not support OCaml bytes values as global values"
  | OCaml FloatArray ->
    Ctypes_static.unsupported
      "cstubs does not support OCaml float arrays as global values"
  | Abstract _ as ty ->
    internal_error
      "Unexpected abstract type encountered during ML code generation: %s"
      (Ctypes.string_of_typ ty)

type wrapper_state = {
  pat: ml_pat;
  exp: ml_exp;
  args: lident list;
  trivial: bool;
  binds: (ml_pat * ml_exp) list;
}

let lwt_unix_run_job = Ctypes_path.path_of_string "Lwt_unix.run_job"
let lwt_preemptive_detach = Ctypes_path.path_of_string "Lwt_preemptive.detach"

let run_exp ~concurrency exp = match concurrency with
    #non_lwt -> exp
  | `Lwt_jobs -> `Appl (`Ident lwt_unix_run_job, exp)
  | `Lwt_preemptive -> `Appl
                         (`Appl (`Ident lwt_preemptive_detach,
                                 `Fun (["_"], exp)),
                          `Unit)

let let_bind : (ml_pat * ml_exp) list -> ml_exp -> ml_exp =
  fun binds e ->
    ListLabels.fold_left ~init:e binds
      ~f:(fun e' (x, e) -> `Let (x, e, e'))

let rec wrapper_body : type a. concurrency:concurrency_policy -> errno:errno_policy ->
  a fn -> ml_exp -> polarity -> (ml_pat * ml_exp) list -> wrapper_state =
  fun ~concurrency ~errno fn exp pol binds -> match fn with
  | Returns t ->
    let exp = run_exp ~concurrency exp in
    begin match pattern_and_exp_of_typ ~concurrency ~errno t exp (flip pol) binds with
      pat, None, binds -> { exp ; args = []; trivial = true; binds;
                            pat = local_con "Returns" [pat] }
    | pat, Some exp, binds -> { exp; args = []; trivial = false; binds;
                                pat = local_con "Returns" [pat] }
    end
  | Function (f, t) ->
    let x = fresh_var () in
    begin match pattern_and_exp_of_typ ~concurrency ~errno
                  f (`Ident (path_of_string x)) pol binds with
    | fpat, None, binds ->
      let { exp; args; trivial; pat = tpat; binds } =
        wrapper_body ~concurrency ~errno
          t (`Appl (exp, `Ident (path_of_string x))) pol binds  in
      { exp; args = x :: args; trivial; binds;
        pat = local_con "Function" [fpat; tpat] }
    | fpat, Some exp', binds ->
      let { exp; args = xs; trivial; pat = tpat; binds } =
        wrapper_body ~concurrency ~errno
          t (`Appl (exp, exp')) pol binds in
      { exp; args = x :: xs; trivial = false; binds;
        pat = local_con "Function" [fpat; tpat] }
    end

let lwt_bind = Ctypes_path.path_of_string "Lwt.bind"
let lwt_return = Ctypes_path.path_of_string "Lwt.return"
let box_lwt = Ctypes_path.path_of_string "box_lwt"
let use_value = Ctypes_path.path_of_string "CI.use_value"

let return_result : args:lident list -> ml_exp =
  fun ~args ->
    let x = fresh_var () in
    (* fun v -> CI.use_value (x1,x2,....xn); Lwt.return v *)
    `Fun ([x],
          `Seq
            (`Appl (`Ident use_value,
                    `Tuple
                      (ListLabels.map args
                         ~f:(fun x -> `Ident (Ctypes_path.path_of_string x)))),
             `Appl (`Ident lwt_return, `Ident (Ctypes_path.path_of_string x))))

(** Returns the variables bound in a pattern, in no particular order *)
let rec pat_bound_vars : ml_pat -> lident list = function
  | `Var x -> [x]
  | `Record (args, _) -> pats_bound_vars (List.map snd args)
  | `As (p, x) -> x :: pat_bound_vars p
  | `Underscore -> []
  | `Con (_, ps) -> pats_bound_vars ps
and pats_bound_vars : ml_pat list -> lident list =
  fun ps -> List.fold_left (fun xs p -> pat_bound_vars p @ xs) [] ps

let wrapper : type a. concurrency:concurrency_policy -> errno:errno_policy ->
  path -> a fn -> string -> polarity -> ml_pat * ml_exp =
  fun ~concurrency ~errno id fn f pol ->
    let p = wrapper_body ~concurrency ~errno fn (`Ident (path_of_string f)) pol [] in
    match p, concurrency with
      { trivial = true; pat; binds }, #non_lwt ->
      (pat, let_bind binds (run_exp ~concurrency (`Ident id)))
    | { exp; args; pat; binds }, #non_lwt ->
      (pat, `Fun (args, let_bind binds exp))
    | { trivial = true; pat; args; binds }, #lwt ->
      let exp : ml_exp = List.fold_left (fun f p -> `Appl (f, `Ident (path_of_string p))) (`Ident id) args in
      (pat, `Fun (args,
                  let_bind binds
                    (`Appl (`Ident box_lwt,
                            `Appl (`Appl (`Ident lwt_bind,
                                          run_exp ~concurrency exp),
                                   return_result ~args:(args
                                                        @ pats_bound_vars
                                                            (List.map fst binds)))))))
    | { exp; args; pat; binds }, #lwt ->
      (pat, `Fun (args,
                  let_bind binds
                    (`Appl (`Ident box_lwt,
                            `Appl (`Appl (`Ident lwt_bind, exp),
                                   return_result ~args:(args @
                                                          pats_bound_vars
                                                            (List.map fst binds)))))))

let case ~concurrency ~errno ~stub_name ~external_name fmt fn =
  let p, e = wrapper ~concurrency ~errno
      (path_of_string external_name) fn external_name In in
  Format.fprintf fmt "@[<hov 2>@[<h 2>|@ @[@[%a@],@ %S@]@ ->@]@ "
    Emit_ML.(ml_pat NoApplParens) p stub_name;
  Format.fprintf fmt "@[<hov 2>@[%a@]@]@]@." Emit_ML.(ml_exp ApplParens) e

let val_case ~stub_name ~external_name fmt typ =
  let x = fresh_var () in
  let p = `As (pattern_of_typ typ, x) in
  let app = `Appl (`Ident (path_of_string external_name), `Unit) in
  let rhs = `MakePtr (`Ident (path_of_string x), app) in
  Format.fprintf fmt "@[<hov 2>@[<h 2>|@ @[@[%a@],@ %S@]@ ->@]@ "
    Emit_ML.(ml_pat NoApplParens) p stub_name;
  Format.fprintf fmt "@[<hov 2>@[%a@]@]@]@."
    Emit_ML.(ml_exp (ApplParens)) rhs

let constructor_decl : type a. concurrency:concurrency_policy ->
  errno:errno_policy -> string -> a fn -> Format.formatter -> unit =
  fun ~concurrency ~errno name fn fmt ->
    Format.fprintf fmt "@[|@ %s@ : (@[%a@])@ name@]@\n" name
      Emit_ML.ml_external_type (ml_external_type_of_fn ~concurrency ~errno fn In)

let inverse_case ~register_name ~constructor name fmt fn : unit =
  let p, e = wrapper ~concurrency:`Sequential ~errno:`Ignore_errno
      (path_of_string "f") fn "f" Out in
  Format.fprintf fmt "|@[ @[%a, %S@] -> %s %s (%a)@]@\n"
    Emit_ML.(ml_pat NoApplParens) p name register_name constructor
    Emit_ML.(ml_exp ApplParens) 
    e
OCaml

Innovation. Community. Security.