Skip to content

Commit 37c3a69

Browse files
authored
feat: empty function parameters become unit param (#13)
Directly match out an empty list rather than using empty string "" for label.
1 parent 75887e2 commit 37c3a69

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

bindgen/caml.ml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ let rec core_type_from_ir typ =
3232
| Ir.Prim Char -> Typ.constr (lid "char") []
3333
| Ir.Prim Void -> Typ.constr (lid "unit") []
3434
| Ir.Ptr t -> core_type_from_ir t
35-
| Ir.Func { fn_ret; fn_params } ->
36-
List.fold_left
37-
(fun acc (name, typ) ->
38-
let label = Asttypes.Labelled name in
39-
let typ = core_type_from_ir typ in
40-
Typ.arrow label typ acc)
41-
(core_type_from_ir fn_ret) fn_params
35+
| Ir.Func { fn_ret; fn_params } -> (
36+
match fn_params with
37+
| [] ->
38+
(* If the C function declaration has no parameters we must introduce a `unit` param *)
39+
Typ.arrow Asttypes.Nolabel
40+
(core_type_from_ir (Ir.Prim Void))
41+
(core_type_from_ir fn_ret)
42+
| params ->
43+
List.fold_left
44+
(fun acc (name, typ) ->
45+
let label = Asttypes.Labelled name in
46+
let typ = core_type_from_ir typ in
47+
Typ.arrow label typ acc)
48+
(core_type_from_ir fn_ret) params)
4249

4350
let type_from_ir typ =
4451
match typ with

examples/caml_doggo.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ void caml_eleven_out_of_ten_majestic_af(value caml_pupper) {
3131
CAMLreturn0;
3232
}
3333

34+
void caml_no_input_no_output() {
35+
CAMLparam0();
36+
no_input_no_output();
37+
CAMLreturn0;
38+
}
39+
3440

examples/doggo.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ void eleven_out_of_ten_majestic_af(Doggo* pupper) {
1313
printf("doggo is a %s\n", BreedToString[pupper->breed]);
1414
}
1515

16+
void no_input_no_output(void) {
17+
printf("We are doing nothing (of importance)\n");
18+
}

examples/doggo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ typedef struct Doggo {
1212
} Doggo;
1313

1414
void eleven_out_of_ten_majestic_af(Doggo* pupper);
15+
16+
void no_input_no_output(void);

examples/doggo.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ type nonrec doggo = {
1010
wow: char }
1111
external eleven_out_of_ten_majestic_af :
1212
pupper:doggo -> unit = "caml_eleven_out_of_ten_majestic_af"
13+
external no_input_no_output : unit -> unit = "caml_no_input_no_output"

0 commit comments

Comments
 (0)