Interoperate with Arguments Using C Descriptors

These are facilities for interoperable procedure interfaces that specify arguments that are assumed-shape arrays, have assumed character length, or have the ALLOCATABLE, POINTER, or OPTIONAL attributes.

Assumed-Rank

An assumed-rank object is a variable whose rank is assumed from its actual argument. This facilitates interoperability with C functions that can accept arguments of arbitrary rank. The intrinsic function, RANK, can be used to obtain the rank of an assumed-rank variable.

A procedure must have an explicit interface if it has an argument that is assumed-rank.

The SHAPE, SIZE, and UBOUND intrinsic functions are not defined for an assumed-rank array that is associated with an assumed-size array except when DIM is specified for either SHAPE or UBOUND, and DIM is not equal to the rank of the array.

An assumed-rank argument may correspond to an actual argument of any rank. If the actual argument has rank zero, the argument has rank zero; the shape is a zero-sized array and the LBOUND and UBOUND intrinsic functions, with no DIM argument, return zero-sized arrays. If the actual argument has rank greater than zero, the rank and extents of the argument are assumed from the actual argument, including no final extent for an assumed-size array. If the actual argument is an array and the argument has the ALLOCATABLE or POINTER attribute, the bounds of the argument are assumed from the actual argument.

Assumed-Type

An assumed-type object is a variable declared as TYPE(*). This simplifies interoperability with C formal parameters of type (void *).

An explicit interface is required for a procedure that has an argument that is assumed-type because an assumed-type argument is polymorphic.

An assumed-type argument must not correspond to an actual argument that is of derived type with type parameters, type-bound procedures, or final subroutines.

CFI_cdesc

A C descriptor is a C structure of type CFI_cdesc that is defined in the file ISO_Fortran_binding.h.

Restrictions for BIND(C)

If BIND(C) is specified for a procedure, each argument must be an interoperable procedure or a variable that is interoperable, assumed shape, assumed rank, assumed type, of assumed character length, or has the ALLOCATABLE or POINTER attribute. If BIND(C) is specified for a function, the function result must be an interoperable scalar variable.

An argument of a procedure that is BIND(C) must not have both the OPTIONAL and VALUE attributes.

A variable that is an argument of a procedure that is BIND(C) must be of interoperable type or assumed type.

A coarray shall not be an argument of a BIND(C) procedure.

The ALLOCATABLE or POINTER attribute must not be specified for a default-initialized argument of a BIND(C) procedure.

Further Requirements

Variables with the ASYNCHRONOUS attribute can be used for asynchronous communications between Fortran and C procedures.

When a Fortran procedure that has an INTENT (OUT) allocatable argument is invoked by a C function, and the actual argument in the C function is the address of a C descriptor that describes an allocated allocatable variable, the variable is deallocated on entry to the Fortran procedure. When a C function is invoked from a Fortran procedure via an interface with an INTENT (OUT) allocatable argument, and the actual argument in the reference to the C function is an allocated allocatable variable, the variable is deallocated on invocation (before execution of the C function begins).

ISO_Fortran_binding.h

The types, macros, and functions declared in ISO_Fortran_binding.h can be used by a C function to interpret C descriptors and allocate and deallocate objects represented by C descriptors. These provide a means to specify a C prototype that interoperates with a Fortran interface that has an allocatable, assumed character length, assumed-rank, assumed-shape, or data pointer argument.

The ISO_Fortran_binding.h is a C header file that contains these definitions:

Restrictions on C Functions Interoperating with Fortran Procedures

Any C function inter-operating with Fortran procedures must meet these restrictions:

See Also