To: J3 10-142 From: Nick Maclaren/Bill Long Subject: C Interoperability Objectives Date: 2010 February 17 References: N1761.txt, N1808-1.pdf 1. Introduction --------------- The purpose of TR 29113 is: C interoperability in the Fortran Standard provides for the sharing of some forms of data between Fortran and C. However, it does not address procedures that have data pointer, allocatable, assumed- shape, or optional dummy arguments. This work item aims to develop ways and means, using facilities provided by the companion Fortran processor, to allow C functions to handle such Fortran dummy arguments directly. This has been extended by the addition of assumed-type and assumed-rank arguments. Constraints on those are mentioned later. The TR does not have further extensibility as an objective, except as explicitly mentioned. The facilities explicitly described in the purpose and added later are considered separately. 2 Requirements -------------- 2.1 Assumed-type ---------------- Requirement 1: A dummy argument declared assumed-type should be compatible with an actual argument of an arbitrary interoperable type, or of assumed-type. It is not required that the interface for the procedure specify bind(c). Constraint 1: Such dummy arguments will be usable for anything other than passing on as an actual argument only in a procedure which can access them with the type of the ultimate argument that is not assumed-type. There is no intent to provide such a mechanism in Fortran. An explicit interface is required for a procedure with an assumed-type dummy argument. Question: Should the TR be exended to allow interoperability of assumed-length CHARACTER arguments? 2.2 Assumed-rank ---------------- Requirement 2: A dummy argument declared assumed-rank should be compatible with an actual argument of an arbitrary rank, or of assumed-rank. It is not required that the interface for the procedure specify bind(c). Constraint 2: Such arguments will be usable only after having been passed to a procedure which can access them with the correct rank. The interface of a procedure called with an actual argument that is assumed-rank shall be explicit. An explicit interface is required for a procedure with an assumed-rank dummy argument. 2.3 Optional Arguments ---------------------- Requirement 3: Interfaces that specify OPTIONAL dummy arguments should be interoperable. Constraint 3: OPTIONAL VALUE dummy arguments are not required to be interoperable. 2.4 Assumed-shape Arrays ------------------------ Requirement 4: (a) An interface with bind(c) may include assumed-shape dummy arguments. The called procedure should be able to access the shape and elements of the dummy argument. (b) A C function should have a mechanism to create an array that it can use as an actual argument corresponding to an assumed-shape dummy of a Fortran procedure. The same mechanism should allow an existing block of memory in the C function, including a array that was passed into the C function, or memory accessed as a global object, to be passed to an assumed-shape dummy in Fortran. c) In the scope of a C function, the lower bounds of an assumed-shape dummy argument are assumed to be zero. In the scope of a Fortran procedure, the lower bounds of an assumed-shape dummy are assumed from its declaration. Constraint 4: The memory that is associated with a Fortran assumed-shape dummy argument should be memory that is accessible in Fortran using normal variable definitions or references. Array strides of zero and overlapping elements are not supported. 2.5 Allocatable Arrays ---------------------- Requirements 5: a) The requirements and constraints that apply to assumed-shape arrays also apply to ALLOCATABLE ones (including converting them to assumed-shape), excluding the intepretation of lower bounds of an assumed-shape array. b) A mechanism should be provided for a C function to allocate and deallocate arrays passed from Fortran. c) A mechanism should be provided for a C function to create ALLOCATABLE arrays and pass them to from Fortran. Such an array should be allocatable or deallocatable using the Fortran ALLOCATE and DEALLOCATE statements. Constraint 5: INTENT(OUT) should not be allowed for an allocatable dummy argument in a bind(c) interface. (Since C would not reliably deallocate the array on entry.) 2.6 Pointer Arrays ------------------ Requirements 6: a) all of the requirements and constraints that apply to ALLOCATABLE arrays also apply to POINTER ones. b) C should be able to allocate memory for POINTER arrays, starting from C pointers, and pass them to Fortran as such. Constraint 6: Neither Fortran nor C may deallocate pointers allocated by the other. 3 Goals ------- These are not directly part of the requirements, but might affect the implementation model. a) Execution efficiency is important, which definitely means that data remapping should not be needed, but it is unclear beyond that. b) Minimize the work required to modify the Fortran compiler. c) Reasonably implementable if the Fortran and C descriptors are the same.