[Patch] Fortran: Fix Bind(C) char-len check, add ptr-contiguous check

Tobias Burnus tobias@codesourcery.com
Fri Aug 20 17:24:52 GMT 2021

The following is about interoperability (BIND(C)) only.

* The patch adds a missing check for pointer + contiguous.
(Rejected to avoid copy-in issues? Or checking issues?)

* And it corrects an issue regarding len > 1 characters. While

  subroutine foo(x)
     character(len=2) :: x(*)

is valid Fortran code (the argument can be "abce" or ['a','b','c','d'] or ...)
– and would work also with bind(C) as the len=2 does not need to be passed
as hidden argument as len is a constant.
However, it is not valid nonetheless.

OK? Comments?


PS: Referenced locations in the standard (F2018):

C1554 If proc-language-binding-spec is specified for a procedure,
each of its dummy arguments shall be an interoperable procedure (18.3.6)
or a variable that is interoperable (18.3.4, 18.3.5), assumed-shape,
assumed-rank, assumed-type, of type CHARACTER with assumed length,
or that has the ALLOCATABLE or POINTER attribute.

18.3.1: "... If the type is character, the length type parameter is
interoperable if and only if its value is one. ..."

"18.3.4 Interoperability of scalar variables":
"... A named scalar Fortran variable is interoperable ... if it
is of type character12its length is not assumed or declared by
an expression that is not a constant expression."

18.3.5: Likewise but for arrays.

18.3.6 "... Fortran procedure interface is interoperable with a C function prototype ..."
"(5) any dummy argument without the VALUE attribute corresponds
      to a formal parameter of the prototype that is of a pointer type, and either
      • the dummy argument is interoperable with an entity of the referenced type ..."
(Remark: those are passed as byte stream)
      "• the dummy argument is a nonallocatable nonpointer variable of type
         CHARACTER with assumed character length and the formal parameter is
         a pointer to CFI_cdesc_t,
       • the dummy argument is allocatable, assumed-shape, assumed-rank, or
         a pointer without the CONTIGUOUS attribute, and the formal parameter
         is a pointer to CFI_cdesc_t, or
(Remark: those two use an array descriptor, also for explicit-size/assumed-size
arrays or for scalars.)
       • the dummy argument is assumed-type ..."

Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bind-c-char-check.diff
Type: text/x-patch
Size: 26029 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20210820/067726d8/attachment-0001.bin>

More information about the Gcc-patches mailing list