[ctypes] [dune] Constant pass to know struct size

I am using the new dune ctypes stanza.
I was wondering if it was possible to have a constant generation pass, before the type generation pass, in order to use the constants to build types.

for example, consider this C code:

#define SIZE 42
struct foo {
  int array [SIZE];
};

I would like to do something like this:

module Types(T : Ctypes.TYPE) = struct
  type foo
  let size = T.constant "SIZE" T.int64_t
  let foo : foo Ctypes.structure T.typ = T.structure "foo"
  ...
  let () = T.seal foo
end

but to use the array field:

let arr = T.field foo "array" (T.array (Int64.of_int size) T.int) (* error *)

seems impossible, as size is still a int64 T.const

Do I have to go back from the fantastic dune ctypes stanza to custom rules, or is there a trick I am missing?

1 Like

This was discussed a bit on Discord but thought I’d post something on the visible web.

The ctypes stanza in dune doesn’t support this outrigh though it might work if you define two libraries that each use ctypes, one where the type_description contains the constants, and then a second library where the type_description references the constants from the first library. Report here on how it goes. :slight_smile:

3 Likes

Did not test this yet, as I am still working on the code, but be sure I will test it as soon as I am done, and come back here to give a verdict! :slight_smile: