cpp_bindgen icon indicating copy to clipboard operation
cpp_bindgen copied to clipboard

Strong type checking in Fortran

Open angainor opened this issue 6 years ago • 5 comments

Currently, when returning a complex type to Fortran cpp_bindgen always returns bindgen_handle * as type(c_ptr) in Fortran. As a result, the actual type of the Fortran object is unknown and the user is allowed to pass the object to 'wrong' underlying C functions.

This could be improved by returning a named derived type to fortran, and requiring that type in the generated Fortran bindings. For example, consider the following generated Fortran binding:

! This file is generated!
module ghex2
implicit none
  interface

    type(c_ptr) function add(arg0, arg1) bind(c)
      use iso_c_binding
      type(c_ptr), value :: arg0
      integer(c_int), value :: arg1
    end function

  end interface
contains
end

A binding with strong Fortran type checking could look like

! This file is generated!
module ghex2
implicit none

  type, bind(c) :: vector_type
     type(c_ptr) :: obj = c_null_ptr
  end type vector_type

  interface

    type(vector_type) function add(arg0, arg1) bind(c)
      use iso_c_binding
      type(vector_type), value :: arg0
      integer(c_int), value :: arg1
    end function

  end interface
contains
end

This would make the fortran interfaces compile-time type safe.

Being able to use meaningful type names instead of a generic bindgen_handle would be very useful. That is, in the above case the programmer should be able to specify the vector_type name somewhere in the C++ code, e.g., using a dedicated macro, or generic C++ capabilities.

angainor avatar Aug 19 '19 08:08 angainor

This sound like a very nice feature. Contributions are welcome! @mbianco Let us know if you think someone from GridTools should work on this.

havogt avatar Aug 19 '19 08:08 havogt

Is it something urgent for @marcin? I think this should not take too long time to implement, and may improve also cosmo, doesn't it?

mbianco avatar Aug 19 '19 12:08 mbianco

@mbianco I am currently using manually implemented bindings, so no rush. It is more like a useful thing to have, IMO.

angainor avatar Aug 19 '19 13:08 angainor

I think this looks like an interesting feature. I would estimate 2 days (1 day prototyping + implementation, 1 day other work like integration in GT).

lukasm91 avatar Aug 19 '19 13:08 lukasm91

I would suggest to develop this as a side thing, when someone needs a break from the main tasks

mbianco avatar Aug 19 '19 14:08 mbianco