ocaml-ctypes icon indicating copy to clipboard operation
ocaml-ctypes copied to clipboard

Cstubs.Types.TYPE.const consumption

Open dsheets opened this issue 10 years ago • 2 comments
trafficstars

What functions can consume Cstubs.Types.TYPE.const and return something useful?

Only Cstubs.Types.TYPE.enum as far as I can tell.

What if you have constants that were not in an enum? For instance, a group of macros does not appear to have a way to be mapped into variant constructors. Have a missed something?

dsheets avatar Jul 04 '15 13:07 dsheets

What if you have constants that were not in an enum? For instance, a group of macros does not appear to have a way to be mapped into variant constructors. Have a missed something?

I think for the moment you have to process the results of the macros outside the functor:

module Type_bindings(T: Cstubs.Types.TYPE) = struct
   let macro_a = constant "MACRO_A" int64
   and macro_b = constant "MACRO_B" int64
   and macro_c = constant "MACRO_C" int64
end

[...]
(* in some other file *)
module Bindings = Type_bindings(Generated_type_stubs)

(* Bindings.macro_{a|b|c} are now int64 values, which you can map to
   variant contructors etc. *)

More generally, const is currently pretty limited and could perhaps be relocated to the more general Ctypes_types.TYPE signature, since there are quite a few other situations where it could be used to retrieve and use compile-time information:

  • The array constructor could have the type

    val array : int const -> 'a typ -> 'a carray typ
    

    so that we could construct arrays with lengths that are known at compile-time:

    #define TABLE_ENTRIES 10
    
    struct table {
      int id;
      struct entry *entries[TABLE_ENTRIES];
    };
    
  • Similarly, the abstract constructor could take its size and alignment arguments as const values.

  • The sizeof and alignment functions could return int const so that they could be computed in generated C code and used to construct other static elements

We could also make const an applicative, which should make it possible to move various computations back inside the bindings functor.

yallop avatar Jul 06 '15 09:07 yallop

I need a constant_opt (or macro_opt?) function for macros that may not be defined which would introduce an #ifdef and return None if the macro is missing.

dsheets avatar Jul 26 '15 15:07 dsheets