https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101308

            Bug ID: 101308
           Summary: Bind(C): gfortran does not create C descriptors for
                    scalar pointer/allocatable arguments
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sandra at gcc dot gnu.org
  Target Milestone: ---

gfortran is completely failing to create C descriptors for scalar arguments to
bind(c) functions that have the POINTER or ALLOCATABLE attribute.  E.g. for
this trivial test case

subroutine f ()
  use iso_c_binding

  interface
    subroutine ctest (pp) bind (c)
      use iso_c_binding
      integer(C_INT), pointer :: pp
    end subroutine
  end interface

  integer(C_INT), pointer :: p
  p => NULL ()

  call ctest (p)
end subroutine

-fdump-tree-original produces this output:

__attribute__((fn spec (". ")))
void f ()
{
  integer(kind=4) * p;

  p = 0B;
  ctest (&p);
}

showing that it is passing a raw pointer by reference instead.

The 2018 Fortran standard is clear that pointers/allocatables are passed as
pointers to C descriptors; it's item (5) in section 18.3.6.

This bug is causing many failures in my WIP TS29113 testsuite, posted at

https://gcc.gnu.org/pipermail/gcc-patches/2021-July/574115.html

interoperability/fc-descriptor-3.f90
interoperability/fc-out-descriptor-3.f90
interoperability/typecodes-scalar.f90
interoperability/typecodes-scalar-ext.f90
procedures/allocatable-dummy.f90

Is it an accident that the related tests passing descriptors from C to Fortran
functions for pointer/allocatable scalars appear to work?  The called Fortran
function expects a pointer to a pointer to the effective argument, and the C
function is passing a pointer to a descriptor structure whose first element
happens to be a pointer to the effective argument.

Reply via email to