The ATTRIBUTES properties (also known as options) C, REFERENCE, VALUE, and VARYING all affect the calling convention of routines. You can specify the:
C, REFERENCE, and VARYING properties for an entire routine
VALUE and REFERENCE properties for individual arguments
By default, Fortran passes all data by reference (except the hidden length argument of strings, which is passed by value). If the C property is used, the default changes to passing almost all data except arrays by value. However, in addition to the calling-convention property C, you can specify argument properties VALUE and REFERENCE (to pass arguments by value or by reference), regardless of the calling convention property. Arrays can only be passed by reference.
Different Fortran calling conventions can be specified by declaring the Fortran procedure to have certain attributes. Assume this example:
INTERFACE
SUBROUTINE MY_SUB (I)
!DEC$ ATTRIBUTES C, ALIAS:'My_Sub_' :: MY_SUB ! IA-32 systems
INTEGER I
END SUBROUTINE MY_SUB
END INTERFACE
This code declares a subroutine named MY_SUB
with the C
property and the external name My_Sub_
set with the ALIAS
property.
For another example, the following declaration assumes the subroutine is called with the C calling convention:
SUBROUTINE CALLED_FROM_C
(A)
!DEC$ ATTRIBUTES C :: CALLED_FROM_C
INTEGER A
.
.
.
The following table summarizes the effect of the most common Fortran calling-convention directives:
Calling Conventions for ATTRIBUTES Properties
Argument | Default | C | C, REFERENCE |
---|---|---|---|
Scalar |
Reference |
Value |
Reference |
Scalar [value] |
Value |
Value |
Value |
Scalar [reference] |
Reference |
Reference |
Reference |
String |
Reference, either Len:End or Len:Mixed |
String(1:1) |
Reference, either Len:End or Len:Mixed |
String [value] |
Error |
String(1:1) |
String(1:1) |
String [reference] |
Reference, either No Len or Len:Mixed |
Reference, No Len |
Reference, No Len |
Array |
Reference |
Reference |
Reference |
Array [value] |
Error |
Error |
Error |
Array [reference] |
Reference |
Reference |
Reference |
Derived Type |
Reference |
Value, size dependent |
Reference |
Derived Type [value] |
Value, size dependent |
Value, size dependent |
Value, size dependent |
Derived Type [reference] |
Reference |
Reference |
Reference |
F90 Pointer |
Descriptor |
Descriptor |
Descriptor |
F90 Pointer [value] |
Error |
Error |
Error |
F90 Pointer [reference] |
Descriptor |
Descriptor |
Descriptor |
The procedure name is all lowercase for all the calling conventions.
The terms in the above table mean the following:
[value] |
Argument assigned the VALUE attribute. |
[reference] |
Argument assigned the REFERENCE attribute. |
Value |
The argument value is pushed on the stack. All values are padded to the next 4-byte boundary for IA-32 based systems and to the next 8-byte boundary for Intel® EM64T and Itanium-based systems. |
Reference |
On IA-32 systems, the 4-byte argument address is pushed on the stack.
|
Len:End or Len:Mixed |
For certain string arguments:
|
No Len or Len:Mixed |
For certain string arguments:
|
No Len |
For string arguments, the length of the string is not available to the called procedure. |
String(1:1) |
For string arguments, the first character is converted to INTEGER(4) as in ICHAR(string(1:1)) and pushed on the stack by value. |
Error |
Produces a compiler error. |
Descriptor |
On IA-32 systems, the 4-byte address of the array descriptor. |
Size dependent |
Derived-type arguments specified by value are passed as follows: On IA-32 systems:
On Intel® EM64T and Itanium-based systems:
|
The following table shows another Fortran ATTRIBUTES property that matches another language calling convention:
Other Language Calling Convention | Matching ATTRIBUTES Property |
---|---|
C/C++ cdecl (default) |
C |
The ALIAS property can be used with any other Fortran calling-convention property to preserve mixed-case names. You can also use the DECORATE property in combination with the ALIAS property to specify that the external name specified in ALIAS should have the correct prefix and postfix decorations for the calling mechanism in effect.
For information on naming conventions, see Adjusting Naming Conventions in Mixed-Language Programming Overview.