http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38536

--- Comment #19 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-01-14 
09:00:20 UTC ---
(In reply to comment #18)
> (I asked on comp.lang.fortran)

Cf.
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/4b65b1cd206ce9fe

Or from the standard (F2008):

"15.2.3.6 C LOC (X)": "Argument. X [...] shall not be a coindexed object. [...]
If it is an array, it shall be contiguous and have nonzero size."


> We just have to check for array sections along the references, and error
> out if there are any.

I was about to suggest to use gfc_simply_contiguous() but I then realized that
-- for a simply contiguous array "array" -- also the following array section 
  array([1,2,3,4])
is contiguous - even though it is not simply contiguous and in the general case
the contiguity is not compile-time checkable. (vector-subscripts are
notoriously difficult to check at compile time)

Similarly, c_loc(comp(1:2)%elem) is valid as long as "elem" is the only item in
the derived type *and* no padding is happening. (As padding often occurs, one
could always reject it.)

And comp(1:1)%elem is always contiguous - but that's also a strange way of
writing it. (Actually, I think that comp(1:1)%elem _is_ simply contiguous -
though I am not sure gfc_simply_contiguous does detect this.)


Personally, I think one should give some diagnostic as soon as the argument is
not simply contiguous -- be it a warning or an error. At least I cannot imagine
any sensible program using non-simply contiguous arrays as argument for C_LOC.

(The term simply contiguous is defined in section 6.5.4.)

 * * *

When you add another check  for C_LOC, can you include one for the following?
You just need to add a "gfc_is_coindexed (expr)".

use iso_c_binding
type t
  integer :: A
end type t
type(t), target, allocatable :: x(:)[:]
type(c_ptr) :: cptr

allocate(x(1)[*])
cptr = c_loc(x(1)%a)    ! OK
cptr = c_loc(x(1)[1]%a) ! Invalid
end

Reply via email to