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:
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 layed 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.
A note on interfacing to a C “varargs” function:
varargsallows 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
printf, create a C function
printfithat takes two arguments, a pointer to a string and an int, and calls
printf. Then in the Ada program, use pragma
Importto interface to
varargsfunction 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
varargsC function with the same two arguments.
C_Plus_Plus (or CPP)
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.
Stdcallcalling sequence will be used, as defined by the NT API. Nevertheless, to ease building cross-platform bindings this convention will be handled as a
Ccalling convention on non-Windows platforms.
GNAT additionally provides a useful pragma
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 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.