GNAT follows standard calling sequence conventions and will thus interface to any other language that also follows these conventions. The following Convention identifiers are recognized by GNAT:
Ada
Note that in the case of GNAT running on a platform that supports HP Ada 83, a higher degree of compatibility can be guaranteed, and in particular records are laid out in an identical manner in the two compilers. Note also that if output from two different compilers is mixed, the program is responsible for dealing with elaboration issues. Probably the safest approach is to write the main program in the version of Ada other than GNAT, so that it takes care of its own elaboration requirements, and then call the GNAT-generated adainit procedure to ensure elaboration of the GNAT components. Consult the documentation of the other Ada compiler for further details on elaboration.
However, it is not possible to mix the tasking run time of GNAT and HP Ada 83, All the tasking operations must either be entirely within GNAT compiled sections of the program, or entirely within HP Ada 83 compiled sections of the program.
Assembler
Asm
COBOL
C
A note on interfacing to a C “varargs” function:
varargs
allows a function to take a variable number of
arguments. There is no direct equivalent in this to Ada. One
approach that can be used is to create a C wrapper for each
different profile and then interface to this C wrapper. For
example, to print an int
value using printf
,
create a C function printfi
that takes two arguments, a
pointer to a string and an int, and calls printf
.
Then in the Ada program, use pragma Import
to
interface to printfi
.
varargs
function by providing a specific Ada profile
for a particular call. However, this does not work on
all platforms, since there is no guarantee that the
calling sequence for a two argument normal C function
is the same as for calling a varargs
C function with
the same two arguments.
Default
External
C_Plus_Plus (or CPP)
Fortran
Intrinsic
type Distance is new Long_Float; type Time is new Long_Float; type Velocity is new Long_Float; function "/" (D : Distance; T : Time) return Velocity; pragma Import (Intrinsic, "/");
This common idiom is often programmed with a generic definition and an explicit body. The pragma makes it simpler to introduce such declarations. It incurs no overhead in compilation time or code size, because it is implemented as a single machine instruction.
function builtin_sqrt (F : Float) return Float; pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
Most of the GCC builtins are accessible this way, and as for other import conventions (e.g. C), it is the user's responsibility to ensure that the Ada subprogram profile matches the underlying builtin expectations.
Stdcall
Stdcall
calling sequence will be used,
as defined by the NT API. Nevertheless, to ease building
cross-platform bindings this convention will be handled as a C
calling
convention on non-Windows platforms.
DLL
Stdcall
.
Win32
Stdcall
.
Stubbed
Program_Error
.
GNAT additionally provides a useful pragma Convention_Identifier
that can be used to parameterize conventions and allow additional synonyms
to be specified. For example if you have legacy code in which the convention
identifier Fortran77 was used for Fortran, you can use the configuration
pragma:
pragma Convention_Identifier (Fortran77, Fortran);
And from now on the identifier Fortran77 may be used as a convention
identifier (for example in an Import
pragma) with the same
meaning as Fortran.