[Patch, Fortran] PR50269 - C_LOC fixes

Tobias Burnus burnus@net-b.de
Fri Aug 17 14:25:00 GMT 2012


*ping* and some background.

On 08/15/2012 12:09 AM, Tobias Burnus wrote:
> The main purpose of this patch is to allow elements of assumed-shape 
> arrays (which are scalars) and assumed-rank arrays with C_LOC.

F2003 only allowed [nonzero-sized] arrays as arguments to C_LOC if they 
were interoperable or allocatable [and allocated] (plus had the target 
attribute, and interoperable type and interoperable type parameter (i.e. 
character(len=1) - interop DT couldn't have a type parameter).

Those arrays are all contiguous - thus taking regarding c_loc(array) == 
c_loc(first_element) makes sense.

F2008 allows any contiguous array, i.e. also assumed-shape and pointer 
deferred-shape arrays. Note: That's "contiguous" and not "simply 
contiguous" and thus not compile-time checkable. (Only some special 
cases which aren't contiguous could be checked, e.g. a(::2) iff the 
array is known to have more than 2 elements.)

With TS29113, an non-interoperable array is also allowed if it is 
"nonpolymorphic variable with no length type parameters." (With the 
usual TARGET, noncoindexed, contiguous requirements).


And for scalars, already F2003 allowed most of them.


The patch now covers a small but important part of the existing bugs, 
namely two rejects-valid issues by:

* C_LOC is no longer rejecting assumed-rank arrays. (interface.c; an 
oversight).

* C_LOC is now accepting scalars, e.g. C_LOC(assumed_rank(1)). Before, 
expr->symtree->n.sym->attr.dimension was tested, but using expr->rank is 
much simpler and more correct.

By fixing the latter, one runs in segfault as for arrays, the old is 
assumed that expr->symtree->n.sym->as is not NULL, but that fails for 
scalar%array. (There is an existing test case for it.)

The patch now simply uses the last array ref. A more though patch would 
be to use the last array ref which isn't AR_ELEMENT, but as 
array1(:)%array2(1) is rejected, using the array spec of array2 instead 
of array1 shouldn't matter. However, I can change that.


There are two applications for this patch:
a) c_loc(assumed_rank(1)) which has been asked for a couple of times and 
I have already seen real-world code
b) c_loc(assumed_rank) which will be used by Open MPI's MPI 3 
implementation at https://bitbucket.org/jsquyres/mpi3-fortran/changesets


While the other issues should be fixed as well, I think a proper patch 
(with -std=f2003, -std=f2008, -std=f2008ts diagnostic) and fixing some 
contiguity checks is quite some effort. Thus, I'd prefer to start with 
this simple patch and defer the rest to a later point.

(I was also thinking about combining it with an implementation of 
"gfc_simply_noncontiguous()" which compile-time checks whether an 
expression is noncontiguous [possibly with some -W... warning for cases 
which are likely noncontiguous, e.g. ptr => a(::2), which is only valid 
if size(a) <= 2]. But also with the implementation of the IS_CONTIGUOUS 
F2008 intrinsics, and moving the C_Binding intrinsics to intrinsics.c, 
using the same mechanism as for C_SIZEOF; that will hopefully also fix 
some issues one sees with the c binding module.)

Tobias



More information about the Fortran mailing list