Interoperability of assumed-type arguments (TS 29113)
To facilitate the interoperability with formal
parameters of type
void* in C procedures, assumed-type
arguments are introduced in Fortran. Assumed-type arguments are entities
that are declared with the TYPE(*) specifier.
They are unlimited polymorphic, and their dynamic types and type parameters
are assumed from the corresponding actual arguments. See the following
example:! example1f.f
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INTEGER(C_SHORT) :: i_short
INTEGER(C_INT) :: i_int
INTEGER(C_LONG) :: i_long
INTERFACE
SUBROUTINE c_func(a, flag) BIND(C)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
TYPE(*) :: a
INTEGER(C_INT), VALUE :: flag
END SUBROUTINE c_func
END INTERFACE
i_short = 2
i_int = 4
i_long = 4
CALL c_func(i_short, 1)
CALL c_func(i_int, 2)
CALL c_func(i_long, 3)
END
// example1c.c
#include <stdio.h>
void c_func(void* a, int flag) {
if(flag == 1)
printf("Type is c_short %hd\n",*(short*)a);
else if(flag == 2)
printf("Type is c_int %d\n",*(int*)a);
else if(flag == 3)
printf("Type is c_long %ld\n",*(long*)a);
return;
}The output of the example is as follows:Type is c_short 2
Type is c_int 4
Type is c_long 4
In this example, function c_func is defined
in the C program, and it is called by a Fortran procedure. Variable a of
type void* is a formal parameter of function c_func in
the C program. The corresponding dummy argument of a in
the Fortran procedure interface is specified by the TYPE(*) specifier,
so the dummy argument is of an assumed type. The dynamic type of variable a is
determined by the actual argument when function c_func is
called. As shown, function c_func is called three
times with the actual parameters i_short, i_int,
and i_long passed in, so the type of variable a is
deduced to c_short, c_int, and c_long accordingly.Note: Fortran
procedures cannot determine the actual types of their assumed-type
arguments.
Restrictions:
- An assumed-type argument can neither be an explicit-shaped array
nor have any of the following attributes:
- ALLOCATABLE
- INTENT(OUT)
- POINTER
- VALUE
- An assumed-type variable name cannot be used in a designator or
an expression except in the following cases:
- As an actual argument that corresponds to a dummy argument of an assumed type
- As an actual argument that corresponds to the first dummy argument
of any of the following intrinsic procedures:
C_LOCIS_CONTIGUOUSLBOUNDPRESENTRANKSHAPESIZEUBOUND
- An assumed-type actual argument that corresponds to an assumed-rank dummy argument must be of assumed-shape or assumed-rank.
- An assumed-type dummy argument cannot correspond to an actual argument of a derived type that has type parameters, type-bound procedures, or final subroutines.