This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Fwd: C++/Fortran Interop for Assumed Shape Arrays


Bob Fischer wrote:
It would be useful if one could declare assumed shape arrays in Fortran, and then pass them to C/C++.  The GNU Fortran documentation claims this is not normally possible in Fortran 2003.  However, I have developed (and use daily) a standards-compliant way to do it.

Well, you do not directly use the array descriptor of Fortran – you convert it into your own array descriptor which you pass around. That's neat and quite useful but still something different.


"The function c_loc_double(x) is required because c_loc() does not seem to work directly on elements of arrays (at least not in GNU's gfortran)."

That bug has finally be fixed this spring for GCC 4.9 – together with lifting many other restrictions. For instance, Fortran 2008 or TS29113 (I forgot which one) now permits to take the C_LOC / C_FUNLOC of noninteroperable variables/procedures.


"The deltas() array contains the address of the second element in each dimension. True skip offsets for each dimension can be computed by spec%deltas(i) - spec%base. This is not done because Fortran does not have pointer arithmetic."


Well, in Fortran you can do pointer arithmetics - but with more effort: You have to convert the pointer to an of type integer(c_ptrdiff) or (c_intptr) with TRANSFER. Then you can add/subtract - and convert it back. That works relatively well, even if it is done in a round-about fashion and uses "processor-defined" behaviour.


Please feel free to add this to the gfortran manual, improve it, etc. This would be an amendment to section 7.1.4 of the manual at:
http://gcc.gnu.org/onlinedocs/gfortran/Interoperable-Subroutines-and-Functions.html

Well, I think it would better fit to http://gcc.gnu.org/onlinedocs/gfortran/Further-Interoperability-of-Fortran-with-C.html, which already refers to Chasm.

Still, I think TS29113 is more convenient. One just includes on the C (C++, ...) side one header file:
http://gcc.gnu.org/viewcvs/gcc/branches/fortran-dev/libgfortran/ISO_Fortran_binding.h.tmpl?&view=markup#l1

And one has then full access to Fortran's array descriptor, e.g.
http://gcc.gnu.org/viewcvs/gcc/branches/fortran-dev/gcc/testsuite/gfortran.dg/iso-ts-29113_1_c.c?view=markup#l1 http://gcc.gnu.org/viewcvs/gcc/branches/fortran-dev/gcc/testsuite/gfortran.dg/iso-ts-29113_2_c.c?view=markup#l1 http://gcc.gnu.org/viewcvs/gcc/branches/fortran-dev/gcc/testsuite/gfortran.dg/iso-ts-29113_3_c.c?view=markup#l1


(Links refer to the Fortran-dev branch, which is used to define a new array descriptor; it already works quite well, but has several known issues. Hence, it shouldn't be used in production code.)

See here for description of what I've done:
http://jerseybiker.blogspot.com/2013/10/passing-assumed-shape-arrays-between.html


Does anyone know how to contact the people involved with the relevant ISO spec?
        ISO/IEC TS 29113:2012

I would start by visiting http://j3-fortran.org/.

Curious question: Why do you want to contact the committee, which developed TS29113? Do you miss some functionality? (Which ones?) I think they do plan to add some features for the next Fortran standard. (Schedule: ftp://ftp.nag.co.uk/sc22wg5/N1951-N2000/N1979.txt )

Tobias


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]