Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strong type checking in Fortran #39

Open
angainor opened this issue Aug 19, 2019 · 5 comments
Open

Strong type checking in Fortran #39

angainor opened this issue Aug 19, 2019 · 5 comments

Comments

@angainor
Copy link

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.

@havogt
Copy link
Contributor

havogt commented Aug 19, 2019

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

@mbianco
Copy link
Contributor

mbianco commented Aug 19, 2019

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?

@angainor
Copy link
Author

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

@lukasm91
Copy link
Contributor

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).

@mbianco
Copy link
Contributor

mbianco commented Aug 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants