[J3] Is BIND (C [ , NAME = scalar-default-char-constant-expr ]) where scalar-default-char-constant-expr has zero length legal?

Vipul Parekh parekhvs at gmail.com
Wed Jan 9 16:02:06 EST 2019


On Wed, Jan 9, 2019 at 11:27 AM Daniel C Chen via J3 <
j3 at mailman.j3-fortran.org> wrote:

> ..
> Based on the wording in the standard that Bill pointed out,
> ..
> 2. If "subroutine foo() bind(c, name='')" is in an interface, Fortran
> cannot call a C routine named 'foo' as well. ..
>

Re: 'If "subroutine foo() bind(c, name='')" is in an interface, Fortran
cannot call a C routine named 'foo' as well.', is it not
processor-dependent again whether Fortran can call a C routine named 'foo'.

Please see the following where on Windows x64 platform, one processor seems
to apply a Fortran procedure label which is "the same as the name of the
procedure using lower case letters" when NAME is specified with zero length
in a BIND statement.  And because that labels happens to be the same as
what the companion processor does for a C function, a coder can manage to
call a C routine from Fortran,  I believe this is not portable.  But I
don't think the Fortran standard disallows it either.

Given this code in C:

#include <stdio.h>
void foo() {
    printf("foo says Hello!\n");
}

and the Fortran code as:

module a
   interface
      subroutine foo() bind(C, name="")
      end subroutine
   end interface
end module

module b
contains
   function foo() result(r)
      integer :: r
      r = 42
   end function
end module

program p
   use a
   use b, bar => foo
   call foo()
   print *, "bar returns ", bar()
end program p

This processor allows linking a program to provide the output:
foo says Hello!
 bar returns  42

My thinking is the above is conforming i.e., a module procedure by itself
is not a global identifier (if I get section 19.2 in 18-007r1 right) unlike
a module name itself.  So module b above should be able to CONTAIN a
procedure named 'foo' regardless of anything in other modules.  Now when
USEd in a scope such as program p with the renaming option, the global
identifiers in that scope are the interface toward subroutine foo() with
proc-lang-binding-spec but no binding label and 'bar'.  The resultant code
on that platform allows invocation of C function via that processor's
convention for the Fortran procedure label and the Fortran function via the
'bar' label.  Does anyone think this is incorrect?

Thanks,
Vipul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190109/db842f6e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graycol.gif
Type: image/gif
Size: 105 bytes
Desc: not available
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190109/db842f6e/attachment.gif>


More information about the J3 mailing list