[Fortran-experiments] Bind(c) first experiences

Tobias Burnus burnus@net-b.de
Wed Dec 20 09:55:00 GMT 2006


Hi,

I played a bit around with bind(c). In general it works rather well and
contains a lot of nice diagnostics which other compilers don't have such as:
  real, BIND(C) :: pnter
Warning: Variable "pnter" at (1) may not be a C interoperable kind but
it is bind(c)

Since I missed to say it before: Thanks a lot for your Bind(C) patch,
Chris! And thanks to FX, who submitted it to the branch!
How great job was done, one sees by realizing that only few compilers
have implemented bind(c) to date, even though it is (relatively) often
requested.


However, I found three minor things which don't work:
-------------
MODULE test
  USE, INTRINSIC :: ISO_C_BINDING
  TYPE(C_FUNPTR) , BIND(C) :: pnter
END MODULE test
-------------

First, (decl.c: match_attr_spec, gfc_match_bind_c) the way attributes
are matched does not allow for the space in " ,bind(c)".

Secondly, something goes wrong here:
"Object 'pnter' at (1) must have the SAVE attribute for default
initialization of a component"

This check is in resolve.c's resolve_fl_variable. Why have c_ptr and
c_funptr default initializations?

Found it: symbol.c, gen_c_interop_ptr_syms:
         /* initialize the value of the c_address field to NULL */
         tmp_comp->initializer = gfc_int_expr(0);
(there are two such initializations in symbol.c)

I think it does not belong there. If one wants to set it to NULL, one
should do so explicitely:
  TYPE(C_FUNPTR), SAVE , BIND(C) :: pnter = C_NULL_FUNPTR

Thirdly,
   type(c_funptr), pointer :: pnter
is accepted, whereas NAG f95 gives me an error:
   Error: test.f90, line 4: BIND(C) variable PNTER is a POINTER
(I think NAG's error message is a bit misleading.)

I think this is prohibited; see F2003, 15.2.2 / 15.2.3:
"C_PTR and C_FUNPTR shall be derived types with private components."
"C1505  (R429)  Each component of a derived type with the BIND attribute
shall be a nonpointer, nonallocatable data component with interoperable
type and type parameters."

 * * *

I tried to compile CP2k including the liblint_interface, which uses
Bind(c). If I remove the default initialization in symbol.c and comment
out the line with C_F_PROCPOINTER in libint_interface.F, then it
compiles. (c_f_procpointer is not yet implemented.)

I think, I need to find some real-world code, which already uses
Bind(c), but does not use procedure pointers to do some real testing.

Tobias



More information about the Fortran mailing list