16 Language interfaces

Overview

No matter how good a new language is, there is usually a great deal of investment in existing programs. The designers of Ada realised this and provide a standard mechanism to interface with other language. The facilites provided are optional, and just which languages are supported depend on the compiler vendor. Considerably more support has been built into Ada95. Consult your compiler's reference manual for full details.


Interfacing with foreign languages (Ada83)

Assume you are on a Unix system and you wish to make use of the kill command. You should perform the following.

The first parameter to pragma interface is the language of the called routine, the second the name the routine is known by in Ada.

Another example for a package is

The pragma interface cannot be used with generic subprograms.


Interfacing with foreign languages (Ada95)

Ada95 makes extensive use of the predefined libraries to enable data type translation between Ada and foriegn languages. Together with the pragmas import, export and convention they allow Ada systems to be easily used in a multi-language environment.


Pragma Import

The pragma Import allows the inclusion of foreign language entities within an Ada program, such as variables or procedures/functions. The code below shows an example of the use of pragma import for the Unix function read.

Pragma Import consists of three parameters,
- the language convention (only Ada and Instrinsic must be supported)
- Ada

- Instrinsic

- C

- Fortran

- Cobol

- any other implementation defined value


- the Ada name for the object
- the foreign langauge name for the object, as a string.


The interface packages

The interface package hierachy consists of packages designed to ease the interfacing of Ada with other langauges. The standard suggests interfaces for C, COBOL and Fortran.
The package hierachy is

The packages give sufficient power to deal with most foreign language interfacing issues.

One area in which Ada has problems interfacing to other languages is functions that contain non homogenous variable length parameter lists, such as printf. Such functions are inherently type unsafe, and there is no satisfactory way to handle such situations.

Ada can, however, handle functions where the argument types are homogenous; this is achieved through the use of unconstrained array types.

A simple example would be a C function...

it could be accessed as follows...

A larger and more complex example is given below for the Unix C function execv. The added complication is caused by the necessity of translation from Ada strings to C style character arrays (and is not necessarily as good as it could be. See the LRM for more information on using interfaces.c child family).

The C function is defined as...

you need to declare a few things...

This function could be implemented as follows. Note that the address of the first item in the array is passed, not the address of the array. Arrays declared from unconstrained array types often have a vector which includes extra information such as the lower and upper bounds of the array.


to the index...