[J3] Question about C_F_Pointer

Van Snyder van.snyder at sbcglobal.net
Mon May 3 01:07:38 UTC 2021


As currently defined, the first argument of C_F_Pointer has to be a
C_Ptr.

In a Bind(C) procedure, presumably if a dummy argument doesn't have the
VALUE attribute, the corresponding thing hiding under the covers is a
C_Ptr.

Would there be a problem in allowing a dummy argument with TARGET and
without VALUE as the first argument to C_F_Pointer in a Bind(C)
procedure?

You can't just use pointer assignment if what's really there is an
array because you can't specify the bounds during pointer assignment.
It's almost possible using <bounds-remapping-list> in pointer
assignment, but for the requirement that the <data-target> is simply
contiguous, which doesn't include scalars.

Is there a special reason we prohibited non-interoperable scalars that
do not have the VALUE from being dummy arguments in Bind(C) procedures?
If non-interoperable scalars were allowed as dummy arguments that don't
have the VALUE attribute, and as actual arguments corresponding to
dummy arguments that don't have the VALUE attribute, the gymnastics
necessary to pass Fortran objects through C code and thence onward to
Fortran code would be less strenuous. It wouldn't be necessary to put
C_Loc() around the actual argument, it wouldn't be necessary to declare
the dummy argument as C_Ptr with VALUE and TARGET, and it wouldn't be
necessary to copy the dummy argument to a Fortran pointer.

Or maybe I've missed something and I'm unnecessarily doing things the
hard way.

I also don't see a reason why this wouldn't work for assumed-size or
explicit-shape arguments of non-interoperable type.

For example

interface
subroutine foo ( Y, F ) bind(c)
import My_Callback
type(non_interoperable), intent(in) :: Y
procedure(my_callback) :: F
end subroutine foo
end interface

type(non_interoperable) :: X(10,20)
call foo ( x, c_funloc(my_callback) )

subroutine My_Callback ( Y )
type(non_interoperable), intent(in), target :: Y(10,20)

The processor can check that argument association and pointer
assignment are type safe.

This is more reliable, and easier to write and understand than

interface
subroutine foo ( Y, F ) bind(c)
type(c_ptr), value, intent(in) :: Y
type(c_funptr), value, intent(in) :: F
end subroutine foo
end interface


type(non_interoperable) :: X(10,20)
call foo ( c_loc(x), c_funloc(my_callback) ) ! Foo is a C procedure
subroutine My_Callback ( Y ) bind(C)
type(c_ptr), intent(in), target, value :: Y
type(non_interoperable), pointer :: Z(:,:)
call c_f_pointer ( y, z, [10,20] )
I assumed here that C_Funloc(my_callback) instead of simply my_callback
is necessary. Maybe it's not. But if it is necessary, why?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20210502/c8793dea/attachment.htm>


More information about the J3 mailing list