Annotated Ada Reference Manual (Ada 202y Draft 1)Legal Information
Contents   Index   References   Search   Previous   Next 

B.5 Interfacing with Fortran

1/3
{AI05-0229-1} The facilities relevant to interfacing with the Fortran language are the package Interfaces.Fortran and support for specifying the Convention aspect with convention_identifier Fortran.
2
The package Interfaces.Fortran defines Ada types whose representations are identical to the default representations of the Fortran intrinsic types Integer, Real, Double Precision, Complex, Logical, and Character in a supported Fortran implementation. These Ada types can therefore be used to pass objects between Ada and Fortran programs.

Static Semantics

3
The library package Interfaces.Fortran has the following declaration: 
4/5
{AI12-0414-1} with Ada.Numerics.Generic_Complex_Types;  -- see G.1.1
pragma Elaborate_All(Ada.Numerics.Generic_Complex_Types);
package Interfaces.Fortran
   with Pure is
5
   type Fortran_Integer is range implementation-defined;
6
   type Real             is digits implementation-defined;
   type Double_Precision is digits implementation-defined;
7
   type Logical is new Boolean;
8
   package Single_Precision_Complex_Types is
      new Ada.Numerics.Generic_Complex_Types (Real);
9
   type Complex is new Single_Precision_Complex_Types.Complex;
10
   subtype Imaginary is Single_Precision_Complex_Types.Imaginary;
   i : Imaginary renames Single_Precision_Complex_Types.i;
   j : Imaginary renames Single_Precision_Complex_Types.j;
10.1/5
{AI12-0058-1}    package Double_Precision_Complex_Types is
      new Ada.Numerics.Generic_Complex_Types (Double_Precision);
10.2/5
{AI12-0058-1}    type Double_Complex is new Double_Precision_Complex_Types.Complex;
10.3/5
{AI12-0058-1}    subtype Double_Imaginary is Double_Precision_Complex_Types.Imaginary;
11
   type Character_Set is implementation-defined character type;
12/3
{AI05-0229-1}    type Fortran_Character is array (Positive range <>) of Character_Set
      with Pack;
13
   function To_Fortran (Item : in Character) return Character_Set;
   function To_Ada (Item : in Character_Set) return Character;
14
   function To_Fortran (Item : in String) return Fortran_Character;
   function To_Ada     (Item : in Fortran_Character) return String;
15
   procedure To_Fortran (Item       : in String;
                         Target     : out Fortran_Character;
                         Last       : out Natural);
16
   procedure To_Ada (Item     : in Fortran_Character;
                     Target   : out String;
                     Last     : out Natural);
17
end Interfaces.Fortran;
17.a.1/1
Implementation defined: The types Fortran_Integer, Real, Double_Precision, and Character_Set in Interfaces.Fortran.
17.a/5
Ramification: {AI12-0058-1} The means by which the Complex and Double_Complex types are provided in Interfaces.Fortran creates a dependence of Interfaces.Fortran on Numerics.Generic_Complex_Types (see G.1.1). This dependence is intentional and unavoidable, if the Fortran-compatible Complex and Double_Complex types are to be useful in Ada code without duplicating facilities defined elsewhere. 
18/5
{AI12-0058-1} The types Fortran_Integer, Real, Double_Precision, Logical, Complex, Double_Complex, Character_Set, and Fortran_Character are Fortran-compatible.
19
The To_Fortran and To_Ada functions map between the Ada type Character and the Fortran type Character_Set, and also between the Ada type String and the Fortran type Fortran_Character. The To_Fortran and To_Ada procedures have analogous effects to the string conversion subprograms found in Interfaces.COBOL. 

Implementation Requirements

20/3
{AI05-0229-1} An implementation shall support specifying aspect Convention with a Fortran convention_identifier for a Fortran-eligible type (see B.1). 

Implementation Permissions

21/5
{AI12-0058-1} {AI12-0263-1} {AI12-0450-1} An implementation may add additional declarations to the Fortran interface packages. For example, declarations are permitted for the character types corresponding to Fortran character kinds 'ascii' and 'iso_10646', which in turn correspond to ISO/IEC 646:1991 and to UCS-4 as specified in ISO/IEC 10646:2020. 
21.a.1/5
Reason: {AI12-0058-1} Fortran compilers are required to recognize 'ascii' and 'iso_10646' as arguments to the SELECTED_CHAR_KIND intrinsic function, but are not required to support those kinds. 
21.a/5
Discussion: {AI12-0058-1} Implementations may add auxiliary declarations as needed to assist in the declarations of additional Fortran-compatible types. For example, if a wide character type is defined to match a Fortran 90 wide character type (accessible in Fortran 90 with the Kind attribute), then an auxiliary character set may be declared to serve as its component type. 

Implementation Advice

22
An Ada implementation should support the following interface correspondences between Ada and Fortran: 
23
An Ada procedure corresponds to a Fortran subroutine.
24
An Ada function corresponds to a Fortran function.
25
An Ada parameter of an elementary, array, or record type T is passed as a TF argument to a Fortran procedure, where TF is the Fortran type corresponding to the Ada type T, and where the INTENT attribute of the corresponding dummy argument matches the Ada formal parameter mode; the Fortran implementation's parameter passing conventions are used. For elementary types, a local copy is used if necessary to ensure by-copy semantics.
26
An Ada parameter of an access-to-subprogram type is passed as a reference to a Fortran procedure whose interface corresponds to the designated subprogram's specification. 
26.a/2
Implementation Advice: If Fortran interfacing is supported, the interface correspondences between Ada and Fortran should be supported.
27
NOTE 1   An object of a Fortran-compatible record type, declared in a library package or subprogram, can correspond to a Fortran common block; the type also corresponds to a Fortran “derived type”.
28/5
NOTE 2   {AI12-0224-1} For Fortran facilities not addressed by this subclause, consider using the Fortran to C interoperability features defined in ISO/IEC 1594-1:2018 along with the C interfacing features defined in B.3.

Examples

29
Example of Interfaces.Fortran:
30
with Interfaces.Fortran;
use Interfaces.Fortran;
procedure Ada_Application is
31/5
{AI05-0229-1} {AI12-0178-1}    type Fortran_Matrix is 
      array (Fortran_Integer range <>,
             Fortran_Integer range <>) of Double_Precision
      with Convention => Fortran;                  -- stored in Fortran's
                                                   -- column-major order
   procedure Invert (Rank : in Fortran_Integer; X : in out Fortran_Matrix)
      with Import => True, Convention => Fortran; -- a Fortran subroutine
32
   Rank      : constant Fortran_Integer := 100;
   My_Matrix : Fortran_Matrix (1 .. Rank, 1 .. Rank);
33/5
{AI12-0058-1}    Precision: constant := 6;
   type Standard_Deviation is digits Precision
      with Convention => Fortran;
   Deviation : Standard_Deviation;
      -- Declarations to match the following Fortran declarations:
      --   integer, parameter :: precision = selected_real_kind(p=6)
      --   real(precision) :: deviation
34
begin
35
   ...
   My_Matrix := ...;
   ...
   Invert (Rank, My_Matrix);
   ...
36/5
{AI12-0058-1}    Deviation := ...;
   ...
37
end Ada_Application;

Wording Changes from Ada 2012

37.a/5
{AI12-0058-1} Correction: The package Double_Precision_Complex_Types and associated types are added to package Interfaces.Fortran. In unusual circumstances, this could cause an incompatibility; we don't document it as an incompatibility as implementations are allowed to add declarations to this package, so that risk of an incompatibility is present for any move from one version of an implementation to another (not to mention to another implementation). As such, the language-defined additions make no change in the risk of incompatibility. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe