[J3] dumb question about module use not inheriting into interfaces

Thomas König tk at tkoenig.net
Sun Aug 28 07:43:28 UTC 2022


On 27.08.22 23:48, Vipul Parekh via J3 wrote:
 > On Fri, Aug 26, 2022 at 10:14 PM Gary Klimowicz <gak at dancingbear.dev 
<mailto:gak at dancingbear.dev>> wrote:
 >
 >     ..
 >     I might be confused by your suggestion, Vipul, but I don't see how
 >     anything like this is realistically possible with current compiler
 >     technology.
 >
 >
 > Gary,
 >
 > Consider the following in a file named foo.f90:
 > module m
 >     use, intrinsic :: iso_c_binding, only : c_int, c_char, c_float
 >     type, bind(C) :: t
 >        integer(c_int) :: x
 >     end type
 > contains
 >     subroutine foo( a ) bind(C, name="foo")
 >        real(kind=c_float) :: a(*)
 >     end subroutine
 >     subroutine bar( b ) bind(C, name="bar")
 >        integer(kind=c_int), value :: b
 >     end subroutine
 >     subroutine baz( c ) bind(C, name="baz")
 >        character(kind=c_char, len=1) :: c(*)
 >     end subroutine
 >     subroutine qux( d ) bind(C, name="qux")
 >        type(t) :: d
 >     end subroutine
 > end module
 >
 > And a practitioner does 'gfortran -fc-prototypes -fsyntax-only 
foo.f90 > foo.h'.  She then gets (in addition to some fluff) in foo.h:
 > typedef struct t {
 >      int x;
 > } t;
 > void bar (int b);
 > void baz (char *c);
 > void foo (float *a);
 > void qux (t *d);
 >
 > And can think "hmm.. the reverse i.e., a C prototype to Fortran 
interface can be achieved too and why not in situ"!

I considered writing something this, the pointer / array ambiguity
of C got in the way.  What should the Fortran translation of

   void foo (float *a);

be?  It could be either

   subroutine foo (a) bind(c)
     use, intrinsic :: iso_c_binding
     real(kind=c_float) :: a
   end subroutine foo

or

   subroutine foo(a) bind(c)
     use, intrinsic :: iso_c_binding
     real (kind=c_float), dimension(*) :: a
   end subroutine foo

One would have to look at the C source code to determine if pointer
arithmetic is done on a  to determine this.

Best regards

     Thomas





More information about the J3 mailing list