GNAT GPL User's Guide: The GNAT Compilation Model

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 2. The GNAT Compilation Model

> 2.1 Source Representation |  |
> 2.2 Foreign Language Representation |  |
> 2.3 File Naming Rules |  |
> 2.4 Using Other File Names |  |
> 2.5 Alternative File Naming Schemes |  |
> 2.6 Generating Object Files |  |
> 2.7 Source Dependencies |  |
> 2.8 The Ada Library Information Files |  |
> 2.9 Binding an Ada Program |  |
> 2.10 Mixed Language Programming |  |
> 2.11 Building Mixed Ada and C++ Programs |  |
> 2.12 Comparison between GNAT and C/C++ Compilation Models |  |
> 2.13 Comparison between GNAT and Conventional Ada Library Models |  |

This chapter describes the compilation model used by GNAT. Although similar to that used by other languages, such as C and C++, this model is substantially different from the traditional Ada compilation models, which are based on a library. The model is initially described without reference to the library-based model. If you have not previously used an Ada compiler, you need only read the first part of this chapter. The last section describes and discusses the differences between the GNAT model and the traditional Ada compiler models. If you have used other Ada compilers, this section will help you to understand those differences, and the advantages of the GNAT model.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.1 Source Representation

Ada source programs are represented in standard text files, using Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar 7-bit ASCII set, plus additional characters used for representing foreign languages (see section 2.2 Foreign Language Representation for support of non-USA character sets). The format effector characters are represented using their standard ASCII encodings, as follows:

`VT`
  Vertical tab, `16#0B#`

`HT`
  Horizontal tab, `16#09#`

`CR`
  Carriage return, `16#0D#`

`LF`
  Line feed, `16#0A#`

`FF`
  Form feed, `16#0C#`

Source files are in standard text file format. In addition, GNAT will recognize a wide variety of stream formats, in which the end of physical lines is marked by any of the following sequences: `LF`, `CR`, `CR-LF`, or `LF-CR`. This is useful in accommodating files that are imported from other operating systems.

The end of a source file is normally represented by the physical end of file. However, the control character `16#1A#` (`SUB`) is also recognized as signalling the end of the source file. Again, this is provided for compatibility with other operating systems where this code is used to represent the end of file.

Each file contains a single Ada compilation unit, including any pragmas associated with the unit. For example, this means you must place a package declaration (a package spec) and the corresponding body in separate files. An Ada compilation (which is a sequence of compilation units) is represented using a sequence of files. Similarly, you will place each subunit or child unit in a separate file.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.2 Foreign Language Representation

GNAT supports the standard character sets defined in Ada as well as several other non-standard character sets for use in localized versions of the compiler (see section 3.2.10 Character Set Control).

> 2.2.1 Latin-1 |  |
> 2.2.2 Other 8-Bit Codes |  |
> 2.2.3 Wide_Character Encodings |  |
> 2.2.4 Wide_Wide_Character Encodings |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.2.1 Latin-1

The basic character set is Latin-1. This character set is defined by ISO standard 8859, part 1. The lower half (character codes `16#00#` ... `16#7F#)` is identical to standard ASCII coding, but the upper half is used to represent additional characters. These include extended letters used by European languages, such as French accents, the vowels with umlauts used in German, and the extra letter A-ring used in Swedish.

For a complete list of Latin-1 codes and their encodings, see the source file of library unit `Ada.Characters.Latin_1` in file ``a-chlat1.ads'`. You may use any of these extended characters freely in character or string literals. In addition, the extended characters that represent letters can be used in identifiers.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.2.2 Other 8-Bit Codes

GNAT also supports several other 8-bit coding schemes:

ISO 8859-2 (Latin-2)
  Latin-2 letters allowed in identifiers, with uppercase and lowercase equivalence.

ISO 8859-3 (Latin-3)
  Latin-3 letters allowed in identifiers, with uppercase and lowercase equivalence.

ISO 8859-4 (Latin-4)
  Latin-4 letters allowed in identifiers, with uppercase and lowercase equivalence.

ISO 8859-5 (Cyrillic)
  ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and lowercase equivalence.

ISO 8859-15 (Latin-9)
  ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and lowercase equivalence

IBM PC (code page 437)
  This code page is the normal default for PCs in the U.S. It corresponds to the original IBM PC character set. This set has some, but not all, of the extended Latin-1 letters, but these letters do not have the same encoding as Latin-1. In this mode, these letters are allowed in identifiers with uppercase and lowercase equivalence.

IBM PC (code page 850)
  This code page is a modification of 437 extended to include all the Latin-1 letters, but still not with the usual Latin-1 encoding. In this mode, all these letters are allowed in identifiers with uppercase and lowercase equivalence.

Full Upper 8-bit
  Any character in the range 80-FF allowed in identifiers, and all are considered distinct. In other words, there are no uppercase and lowercase equivalences in this range. This is useful in conjunction with certain encoding schemes used for some foreign character sets (e.g., the typical method of representing Chinese characters on the PC).

No Upper-Half
  No upper-half characters in the range 80-FF are allowed in identifiers. This gives Ada 83 compatibility for identifier names.

For precise data on the encodings permitted, and the uppercase and lowercase equivalences that are recognized, see the file ``csets.adb'` in the GNAT compiler sources. You will need to obtain a full source release of GNAT to obtain this file.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.2.3 Wide_Character Encodings

GNAT allows wide character codes to appear in character and string literals, and also optionally in identifiers, by means of the following possible encoding schemes:

Hex Coding
  In this encoding, a wide character is represented by the following five character sequence:

      ESC a b c d

  Where `a`, `b`, `c`, `d` are the four hexadecimal characters (using uppercase letters) of the wide character code. For example, ESC A345 is used to represent the wide character with code `16#A345#`. This scheme is compatible with use of the full Wide_Character set.

Upper-Half Coding
  The wide character with encoding `16#abcd#` where the upper bit is on (in other words, "a" is in the range 8-F) is represented as two bytes, `16#ab#` and `16#cd#`. The second byte cannot be a format control character, but is not required to be in the upper half. This method can be also used for shift-JIS or EUC, where the internal coding matches the external coding.

Shift JIS Coding
  A wide character is represented by a two-character sequence, `16#ab#` and `16#cd#`, with the restrictions described for upper-half encoding as described above. The internal character code is the corresponding JIS character according to the standard algorithm for Shift-JIS conversion. Only characters defined in the JIS code set table can be used with this encoding method.

EUC Coding
  A wide character is represented by a two-character sequence `16#ab#` and `16#cd#`, with both characters being in the upper half. The internal character code is the corresponding JIS character according to the EUC encoding algorithm. Only characters defined in the JIS code set table can be used with this encoding method.

UTF-8 Coding
  A wide character is represented using UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO 10646-1/Am.2. Depending on the character value, the representation is a one, two, or three byte sequence:

      16#0000#-16#007f#: 2#0xxxxxxx#
      16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
      16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#

  where the xxx bits correspond to the left-padded bits of the 16-bit character value. Note that all lower half ASCII characters are represented as ASCII bytes and all upper half characters and other wide characters are represented as sequences of upper-half (The full UTF-8 scheme allows for encoding 31-bit characters as 6-byte sequences, and in the following section on wide wide characters, the use of these sequences is documented).

Brackets Coding
  In this encoding, a wide character is represented by the following eight character sequence:

      [ " a b c d " ]

  Where `a`, `b`, `c`, `d` are the four hexadecimal characters (using uppercase letters) of the wide character code. For example, ["A345"] is used to represent the wide character with code `16#A345#`. It is also possible (though not required) to use the Brackets coding for upper half characters. For example, the code `16#A3#` can be represented as `["A3"]`.

  This scheme is compatible with use of the full Wide_Character set, and is also the method used for wide character encoding in some standard ACATS (Ada Conformity Assessment Test Suite) test suite distributions.

Note: Some of these coding schemes do not permit the full use of the Ada character set. For example, neither Shift JIS, nor EUC allow the use of the upper half of the Latin-1 set.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.2.4 Wide_Wide_Character Encodings

GNAT allows wide wide character codes to appear in character and string literals, and also optionally in identifiers, by means of the following possible encoding schemes:

UTF-8 Coding
  A wide character is represented using UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO 10646-1/Am.2. Depending on the character value, the representation of character codes with values greater than 16#FFFF# is a is a four, five, or six byte sequence:

      16#01_0000#-16#10_FFFF#:     11110xxx 10xxxxxx 10xxxxxx
                                   10xxxxxx
      16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
                                   10xxxxxx 10xxxxxx
      16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
                                   10xxxxxx 10xxxxxx 10xxxxxx

  where the xxx bits correspond to the left-padded bits of the 32-bit character value.

Brackets Coding
  In this encoding, a wide wide character is represented by the following ten or twelve byte character sequence:

      [ " a b c d e f " ]
      [ " a b c d e f g h " ]

  Where `a-h` are the six or eight hexadecimal characters (using uppercase letters) of the wide wide character code. For example, ["1F4567"] is used to represent the wide wide character with code `16#001F_4567#`.

  This scheme is compatible with use of the full Wide_Wide_Character set, and is also the method used for wide wide character encoding in some standard ACATS (Ada Conformity Assessment Test Suite) test suite distributions.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.3 File Naming Rules

The default file name is determined by the name of the unit that the file contains. The name is formed by taking the full expanded name of the unit and replacing the separating dots with hyphens and using lowercase for all letters.

An exception arises if the file name generated by the above rules starts with one of the characters ``a'`, ``g'`, ``i'`, or ``s'`, and the second character is a minus. In this case, the character tilde is used in place of the minus. The reason for this special rule is to avoid clashes with the standard names for child units of the packages System, Ada, Interfaces, and GNAT, which use the prefixes ``s-'`, ``a-'`, ``i-'`, and ``g-'`, respectively.

The file extension is ``.ads'` for a spec and ``.adb'` for a body. The following list shows some examples of these rules.

``main.ads'`
  Main (spec)
``main.adb'`
  Main (body)
``arith_functions.ads'`
  Arith_Functions (package spec)
``arith_functions.adb'`
  Arith_Functions (package body)
``func-spec.ads'`
  Func.Spec (child package spec)
``func-spec.adb'`
  Func.Spec (child package body)
``main-sub.adb'`
  Sub (subunit of Main)
``a~bad.adb'`
  A.Bad (child package body)

Following these rules can result in excessively long file names if corresponding unit names are long (for example, if child units or subunits are heavily nested). An option is available to shorten such long file names (called file name "krunching"). This may be particularly useful when programs being developed with GNAT are to be used on operating systems with limited file name lengths. See section 17.2 Using `gnatkr`.

Of course, no file shortening algorithm can guarantee uniqueness over all possible unit names; if file name krunching is used, it is your responsibility to ensure no name clashes occur. Alternatively you can specify the exact file names that you want used, as described in the next section. Finally, if your Ada programs are migrating from a compiler with a different naming convention, you can use the gnatchop utility to produce source files that follow the GNAT naming conventions. (For details see section 8. Renaming Files with `gnatchop`.)

Note: in the case of `Windows NT/XP` or `OpenVMS` operating systems, case is not significant. So for example on `Windows XP` if the canonical name is `main-sub.adb`, you can use the file name `Main-Sub.adb` instead. However, case is significant for other operating systems, so for example, if you want to use other than canonically cased file names on a Unix system, you need to follow the procedures described in the next section.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.4 Using Other File Names

In the previous section, we have described the default rules used by GNAT to determine the file name in which a given unit resides. It is often convenient to follow these default rules, and if you follow them, the compiler knows without being explicitly told where to find all the files it needs.

However, in some cases, particularly when a program is imported from another Ada compiler environment, it may be more convenient for the programmer to specify which file names contain which units. GNAT allows arbitrary file names to be used by means of the Source_File_Name pragma. The form of this pragma is as shown in the following examples:

    pragma Source_File_Name (My_Utilities.Stacks,
      Spec_File_Name => "myutilst_a.ada");
    pragma Source_File_name (My_Utilities.Stacks,
      Body_File_Name => "myutilst.ada");

As shown in this example, the first argument for the pragma is the unit name (in this example a child unit). The second argument has the form of a named association. The identifier indicates whether the file name is for a spec or a body; the file name itself is given by a string literal.

The source file name pragma is a configuration pragma, which means that normally it will be placed in the ``gnat.adc'` file used to hold configuration pragmas that apply to a complete compilation environment. For more details on how the ``gnat.adc'` file is created and used see 9.1 Handling of Configuration Pragmas.

GNAT allows completely arbitrary file names to be specified using the source file name pragma. However, if the file name specified has an extension other than ``.ads'` or ``.adb'` it is necessary to use a special syntax when compiling the file. The name in this case must be preceded by the special sequence ``-x'` followed by a space and the name of the language, here `ada`, as in:

    $ gcc -c -x ada peculiar_file_name.sim

`gnatmake` handles non-standard file names in the usual manner (the non-standard file name for the main program is simply used as the argument to gnatmake). Note that if the extension is also non-standard, then it must be included in the `gnatmake` command, it may not be omitted.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.5 Alternative File Naming Schemes

In the previous section, we described the use of the `Source_File_Name` pragma to allow arbitrary names to be assigned to individual source files. However, this approach requires one pragma for each file, and especially in large systems can result in very long ``gnat.adc'` files, and also create a maintenance problem.

GNAT also provides a facility for specifying systematic file naming schemes other than the standard default naming scheme previously described. An alternative scheme for naming is specified by the use of `Source_File_Name` pragmas having the following format:

    pragma Source_File_Name (
       Spec_File_Name  => FILE_NAME_PATTERN
     [,Casing          => CASING_SPEC]
     [,Dot_Replacement => STRING_LITERAL]);

    pragma Source_File_Name (
       Body_File_Name  => FILE_NAME_PATTERN
     [,Casing          => CASING_SPEC]
     [,Dot_Replacement => STRING_LITERAL]);

    pragma Source_File_Name (
       Subunit_File_Name  => FILE_NAME_PATTERN
     [,Casing             => CASING_SPEC]
     [,Dot_Replacement    => STRING_LITERAL]);

    FILE_NAME_PATTERN ::= STRING_LITERAL
    CASING_SPEC ::= Lowercase | Uppercase | Mixedcase

The `FILE_NAME_PATTERN` string shows how the file name is constructed. It contains a single asterisk character, and the unit name is substituted systematically for this asterisk. The optional parameter `Casing` indicates whether the unit name is to be all upper-case letters, all lower-case letters, or mixed-case. If no `Casing` parameter is used, then the default is all lower-case.

The optional `Dot_Replacement` string is used to replace any periods that occur in subunit or child unit names. If no `Dot_Replacement` argument is used then separating dots appear unchanged in the resulting file name. Although the above syntax indicates that the `Casing` argument must appear before the `Dot_Replacement` argument, but it is also permissible to write these arguments in the opposite order.

As indicated, it is possible to specify different naming schemes for bodies, specs, and subunits. Quite often the rule for subunits is the same as the rule for bodies, in which case, there is no need to give a separate `Subunit_File_Name` rule, and in this case the `Body_File_name` rule is used for subunits as well.

The separate rule for subunits can also be used to implement the rather unusual case of a compilation environment (e.g. a single directory) which contains a subunit and a child unit with the same unit name. Although both units cannot appear in the same partition, the Ada Reference Manual allows (but does not require) the possibility of the two units coexisting in the same environment.

The file name translation works in the following steps:

- If there is a specific `Source_File_Name` pragma for the given unit, then this is always used, and any general pattern rules are ignored.

- If there is a pattern type `Source_File_Name` pragma that applies to the unit, then the resulting file name will be used if the file exists. If more than one pattern matches, the latest one will be tried first, and the first attempt resulting in a reference to a file that exists will be used.

- If no pattern type `Source_File_Name` pragma that applies to the unit for which the corresponding file exists, then the standard GNAT default naming rules are used.

As an example of the use of this mechanism, consider a commonly used scheme in which file names are all lower case, with separating periods copied unchanged to the resulting file name, and specs end with ``.1.ada'`, and bodies end with ``.2.ada'`. GNAT will follow this scheme if the following two pragmas appear:

    pragma Source_File_Name
      (Spec_File_Name => "*.1.ada");
    pragma Source_File_Name
      (Body_File_Name => "*.2.ada");

The default GNAT scheme is actually implemented by providing the following default pragmas internally:

    pragma Source_File_Name
      (Spec_File_Name => "*.ads", Dot_Replacement => "-");
    pragma Source_File_Name
      (Body_File_Name => "*.adb", Dot_Replacement => "-");

Our final example implements a scheme typically used with one of the Ada 83 compilers, where the separator character for subunits was "__" (two underscores), specs were identified by adding ``_.ADA'`, bodies by adding ``.ADA'`, and subunits by adding ``.SEP'`. All file names were upper case. Child units were not present of course since this was an Ada 83 compiler, but it seems reasonable to extend this scheme to use the same double underscore separator for child units.

    pragma Source_File_Name
      (Spec_File_Name => "*_.ADA",
       Dot_Replacement => "__",
       Casing = Uppercase);
    pragma Source_File_Name
      (Body_File_Name => "*.ADA",
       Dot_Replacement => "__",
       Casing = Uppercase);
    pragma Source_File_Name
      (Subunit_File_Name => "*.SEP",
       Dot_Replacement => "__",
       Casing = Uppercase);

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.6 Generating Object Files

An Ada program consists of a set of source files, and the first step in compiling the program is to generate the corresponding object files. These are generated by compiling a subset of these source files. The files you need to compile are the following:

- If a package spec has no body, compile the package spec to produce the object file for the package.

- If a package has both a spec and a body, compile the body to produce the object file for the package. The source file for the package spec need not be compiled in this case because there is only one object file, which contains the code for both the spec and body of the package.

- For a subprogram, compile the subprogram body to produce the object file for the subprogram. The spec, if one is present, is as usual in a separate file, and need not be compiled.

- In the case of subunits, only compile the parent unit. A single object file is generated for the entire subunit tree, which includes all the subunits.

- Compile child units independently of their parent units (though, of course, the spec of all the ancestor unit must be present in order to compile a child unit).

- Compile generic units in the same manner as any other units. The object files in this case are small dummy files that contain at most the flag used for elaboration checking. This is because GNAT always handles generic instantiation by means of macro expansion. However, it is still necessary to compile generic units, for dependency checking and elaboration purposes.

The preceding rules describe the set of files that must be compiled to generate the object files for a program. Each object file has the same name as the corresponding source file, except that the extension is ``.o'` as usual.

You may wish to compile other files for the purpose of checking their syntactic and semantic correctness. For example, in the case where a package has a separate spec and body, you would not normally compile the spec. However, it is convenient in practice to compile the spec to make sure it is error-free before compiling clients of this spec, because such compilations will fail if there is an error in the spec.

GNAT provides an option for compiling such files purely for the purposes of checking correctness; such compilations are not required as part of the process of building a program. To compile a file in this checking mode, use the ``-gnatc'` switch.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.7 Source Dependencies

A given object file clearly depends on the source file which is compiled to produce it. Here we are using depends in the sense of a typical `make` utility; in other words, an object file depends on a source file if changes to the source file require the object file to be recompiled. In addition to this basic dependency, a given object may depend on additional source files as follows:

- If a file being compiled `with`'s a unit X, the object file depends on the file containing the spec of unit X. This includes files that are `with`'ed implicitly either because they are parents of `with`'ed child units or they are run-time units required by the language constructs used in a particular unit.

- If a file being compiled instantiates a library level generic unit, the object file depends on both the spec and body files for this generic unit.

- If a file being compiled instantiates a generic unit defined within a package, the object file depends on the body file for the package as well as the spec file.

- If a file being compiled contains a call to a subprogram for which pragma `Inline` applies and inlining is activated with the ``-gnatn'` switch, the object file depends on the file containing the body of this subprogram as well as on the file containing the spec. Note that for inlining to actually occur as a result of the use of this switch, it is necessary to compile in optimizing mode.

  The use of ``-gnatN'` activates inlining optimization that is performed by the front end of the compiler. This inlining does not require that the code generation be optimized. Like ``-gnatn'`, the use of this switch generates additional dependencies.

  When using a gcc-based back end (in practice this means using any version of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of ``-gnatN'` is deprecated, and the use of ``-gnatn'` is preferred. Historically front end inlining was more extensive than the gcc back end inlining, but that is no longer the case.

- If an object file ``O'` depends on the proper body of a subunit through inlining or instantiation, it depends on the parent unit of the subunit. This means that any modification of the parent unit or one of its subunits affects the compilation of ``O'`.

- The object file for a parent unit depends on all its subunit body files.

- The previous two rules meant that for purposes of computing dependencies and recompilation, a body and all its subunits are treated as an indivisible whole.

  These rules are applied transitively: if unit `A` `with`'s unit `B`, whose elaboration calls an inlined procedure in package `C`, the object file for unit `A` will depend on the body of `C`, in file ``c.adb'`.

  The set of dependent files described by these rules includes all the files on which the unit is semantically dependent, as dictated by the Ada language standard. However, it is a superset of what the standard describes, because it includes generic, inline, and subunit dependencies.

  An object file must be recreated by recompiling the corresponding source file if any of the source files on which it depends are modified. For example, if the `make` utility is used to control compilation, the rule for an Ada object file must mention all the source files on which the object file depends, according to the above definition. The determination of the necessary recompilations is done automatically when one uses `gnatmake`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.8 The Ada Library Information Files

Each compilation actually generates two output files. The first of these is the normal object file that has a ``.o'` extension. The second is a text file containing full dependency information. It has the same name as the source file, but an ``.ali'` extension. This file is known as the Ada Library Information (``ALI'`) file. The following information is contained in the ``ALI'` file.

- Version information (indicates which version of GNAT was used to compile the unit(s) in question)

- Main program information (including priority and time slice settings, as well as the wide character encoding used during compilation).

- List of arguments used in the `gcc` command for the compilation

- Attributes of the unit, including configuration pragmas used, an indication of whether the compilation was successful, exception model used etc.

- A list of relevant restrictions applying to the unit (used for consistency) checking.

- Categorization information (e.g. use of pragma `Pure`).

- Information on all `with`'ed units, including presence of `Elaborate` or `Elaborate_All` pragmas.

- Information from any `Linker_Options` pragmas used in the unit

- Information on the use of `Body_Version` or `Version` attributes in the unit.

- Dependency information. This is a list of files, together with time stamp and checksum information. These are files on which the unit depends in the sense that recompilation is required if any of these units are modified.

- Cross-reference data. Contains information on all entities referenced in the unit. Used by tools like `gnatxref` and `gnatfind` to provide cross-reference information.

For a full detailed description of the format of the ``ALI'` file, see the source of the body of unit `Lib.Writ`, contained in file ``lib-writ.adb'` in the GNAT compiler sources.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.9 Binding an Ada Program

When using languages such as C and C++, once the source files have been compiled the only remaining step in building an executable program is linking the object modules together. This means that it is possible to link an inconsistent version of a program, in which two units have included different versions of the same header.

The rules of Ada do not permit such an inconsistent program to be built. For example, if two clients have different versions of the same package, it is illegal to build a program containing these two clients. These rules are enforced by the GNAT binder, which also determines an elaboration order consistent with the Ada rules.

The GNAT binder is run after all the object files for a program have been created. It is given the name of the main program unit, and from this it determines the set of units required by the program, by reading the corresponding ALI files. It generates error messages if the program is inconsistent or if no valid order of elaboration exists.

If no errors are detected, the binder produces a main program, in Ada by default, that contains calls to the elaboration procedures of those compilation unit that require them, followed by a call to the main program. This Ada program is compiled to generate the object file for the main program. The name of the Ada file is ``b~xxx.adb'` (with the corresponding spec ``b~xxx.ads'`) where xxx is the name of the main program unit.

Finally, the linker is used to build the resulting executable program, using the object from the main program from the bind step as well as the object files for the Ada units of the program.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.10 Mixed Language Programming

This section describes how to develop a mixed-language program, specifically one that comprises units in both Ada and C.

> 2.10.1 Interfacing to C |  |
> 2.10.2 Calling Conventions |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.10.1 Interfacing to C

Interfacing Ada with a foreign language such as C involves using compiler directives to import and/or export entity definitions in each language--using `extern` statements in C, for instance, and the `Import`, `Export`, and `Convention` pragmas in Ada. A full treatment of these topics is provided in Appendix B, section 1 of the Ada Reference Manual.

There are two ways to build a program using GNAT that contains some Ada sources and some foreign language sources, depending on whether or not the main subprogram is written in Ada. Here is a source example with the main subprogram in Ada:

    /* file1.c */
    #include <stdio.h>

    void print_num (int num)
    {
      printf ("num is %d.\n", num);
      return;
    }

    /* file2.c */

    /* num_from_Ada is declared in my_main.adb */
    extern int num_from_Ada;

    int get_num (void)
    {
      return num_from_Ada;
    }

    --  my_main.adb
    procedure My_Main is

       --  Declare then export an Integer entity called num_from_Ada
       My_Num : Integer := 10;
       pragma Export (C, My_Num, "num_from_Ada");

       --  Declare an Ada function spec for Get_Num, then use
       --  C function get_num for the implementation.
       function Get_Num return Integer;
       pragma Import (C, Get_Num, "get_num");

       --  Declare an Ada procedure spec for Print_Num, then use
       --  C function print_num for the implementation.
       procedure Print_Num (Num : Integer);
       pragma Import (C, Print_Num, "print_num");

    begin
       Print_Num (Get_Num);
    end My_Main;

1. To build this example, first compile the foreign language files to generate object files:

       gcc -c file1.c
       gcc -c file2.c

2. Then, compile the Ada units to produce a set of object files and ALI files:

       gnatmake -c my_main.adb

3. Run the Ada binder on the Ada main program:

       gnatbind my_main.ali

4. Link the Ada main program, the Ada objects and the other language objects:

       gnatlink my_main.ali file1.o file2.o

The last three steps can be grouped in a single command:

    gnatmake my_main.adb -largs file1.o file2.o

If the main program is in a language other than Ada, then you may have more than one entry point into the Ada subsystem. You must use a special binder option to generate callable routines that initialize and finalize the Ada units (see section 4.2.6 Binding with Non-Ada Main Programs). Calls to the initialization and finalization routines must be inserted in the main program, or some other appropriate point in the code. The call to initialize the Ada units must occur before the first Ada subprogram is called, and the call to finalize the Ada units must occur after the last Ada subprogram returns. The binder will place the initialization and finalization subprograms into the ``b~xxx.adb'` file where they can be accessed by your C sources. To illustrate, we have the following example:

    /* main.c */
    extern void adainit (void);
    extern void adafinal (void);
    extern int add (int, int);
    extern int sub (int, int);

    int main (int argc, char *argv[])
    {
      int a = 21, b = 7;

      adainit();

      /* Should print "21 + 7 = 28" */
      printf ("%d + %d = %d\n", a, b, add (a, b));
      /* Should print "21 - 7 = 14" */
      printf ("%d - %d = %d\n", a, b, sub (a, b));

      adafinal();
    }

    --  unit1.ads
    package Unit1 is
       function Add (A, B : Integer) return Integer;
       pragma Export (C, Add, "add");
    end Unit1;

    --  unit1.adb
    package body Unit1 is
       function Add (A, B : Integer) return Integer is
       begin
          return A + B;
       end Add;
    end Unit1;

    --  unit2.ads
    package Unit2 is
       function Sub (A, B : Integer) return Integer;
       pragma Export (C, Sub, "sub");
    end Unit2;

    --  unit2.adb
    package body Unit2 is
       function Sub (A, B : Integer) return Integer is
       begin
          return A - B;
       end Sub;
    end Unit2;

1. The build procedure for this application is similar to the last example's. First, compile the foreign language files to generate object files:

       gcc -c main.c

2. Next, compile the Ada units to produce a set of object files and ALI files:

       gnatmake -c unit1.adb
       gnatmake -c unit2.adb

3. Run the Ada binder on every generated ALI file. Make sure to use the ``-n'` option to specify a foreign main program:

       gnatbind -n unit1.ali unit2.ali

4. Link the Ada main program, the Ada objects and the foreign language objects. You need only list the last ALI file here:

       gnatlink unit2.ali main.o -o exec_file

   This procedure yields a binary executable called ``exec_file'`.

Depending on the circumstances (for example when your non-Ada main object does not provide symbol `main`), you may also need to instruct the GNAT linker not to include the standard startup objects by passing the ``-nostartfiles'` switch to `gnatlink`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.10.2 Calling Conventions

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`
  This indicates that the standard Ada calling sequence will be used and all Ada data items may be passed without any limitations in the case where GNAT is used to generate both the caller and callee. It is also possible to mix GNAT generated code and code generated by another Ada compiler. In this case, the data types should be restricted to simple cases, including primitive types. Whether complex data types can be passed depends on the situation. Probably it is safe to pass simple arrays, such as arrays of integers or floats. Records may or may not work, depending on whether both compilers lay them out identically. Complex structures involving variant records, access parameters, tasks, or protected types, are unlikely to be able to be passed.

  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`
  Specifies assembler as the convention. In practice this has the same effect as convention Ada (but is not equivalent in the sense of being considered the same convention).

`Asm`
  Equivalent to Assembler.

`COBOL`
  Data will be passed according to the conventions described in section B.4 of the Ada Reference Manual.

`C`
  Data will be passed according to the conventions described in section B.3 of the Ada Reference Manual.

  A note on interfacing to a C "varargs" function:

  - In C, `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`.

  - It may work on some platforms to directly interface to a `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`
  Equivalent to C.

`External`
  Equivalent to C.

`C_Plus_Plus (or CPP)`
  This stands for C++. For most purposes this is identical to C. See the separate description of the specialized GNAT pragmas relating to C++ interfacing for further details.

`Fortran`
  Data will be passed according to the conventions described in section B.5 of the Ada Reference Manual.

`Intrinsic`
  This applies to an intrinsic operation, as defined in the Ada Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram, this means that the body of the subprogram is provided by the compiler itself, usually by means of an efficient code sequence, and that the user does not supply an explicit body for it. In an application program, the pragma may be applied to the following sets of names:

  - Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic. The corresponding subprogram declaration must have two formal parameters. The first one must be a signed integer type or a modular type with a binary modulus, and the second parameter must be of type Natural. The return type must be the same as the type of the first argument. The size of this type can only be 8, 16, 32, or 64.

  - Binary arithmetic operators: "+", "-", "*", "/" The corresponding operator declaration must have parameters and result type that have the same root numeric type (for example, all three are long_float types). This simplifies the definition of operations that use type checking to perform dimensional checks:

        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.

  - General subprogram entities, to bind an Ada subprogram declaration to a compiler builtin by name with back-ends where such interfaces are available. A typical example is the set of "__builtin" functions exposed by the GCC back-end, as in the following example:

           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`
  This is relevant only to Windows XP/2000/NT implementations of GNAT, and specifies that the `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`
  This is equivalent to `Stdcall`.

`Win32`
  This is equivalent to `Stdcall`.

`Stubbed`
  This is a special convention that indicates that the compiler should provide a stub body that raises `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.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.11 Building Mixed Ada and C++ Programs

A programmer inexperienced with mixed-language development may find that building an application containing both Ada and C++ code can be a challenge. This section gives a few hints that should make this task easier. The first section addresses the differences between interfacing with C and interfacing with C++. The second section looks into the delicate problem of linking the complete application from its Ada and C++ parts. The last section gives some hints on how the GNAT run-time library can be adapted in order to allow inter-language dispatching with a new C++ compiler.

> 2.11.1 Interfacing to C++ |  |
> 2.11.2 Linking a Mixed C++ & Ada Program |  |
> 2.11.3 A Simple Example |  |
> 2.11.4 Interfacing with C++ constructors |  |
> 2.11.5 Interfacing with C++ at the Class Level |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.11.1 Interfacing to C++

GNAT supports interfacing with the G++ compiler (or any C++ compiler generating code that is compatible with the G++ Application Binary Interface --see http://www.codesourcery.com/archives/cxx-abi).

Interfacing can be done at 3 levels: simple data, subprograms, and classes. In the first two cases, GNAT offers a specific `Convention C_Plus_Plus` (or `CPP`) that behaves exactly like `Convention C`. Usually, C++ mangles the names of subprograms. To generate proper mangled names automatically, see 29. Generating Ada Bindings for C and C++ headers). This problem can also be addressed manually in two ways:

- by modifying the C++ code in order to force a C convention using the `extern "C"` syntax.

- by figuring out the mangled name (using e.g. `nm`) and using it as the Link_Name argument of the pragma import.

Interfacing at the class level can be achieved by using the GNAT specific pragmas such as `CPP_Constructor`. See section `Interfacing to C++' in GNAT Reference Manual, for additional information.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.11.2 Linking a Mixed C++ & Ada Program

Usually the linker of the C++ development system must be used to link mixed applications because most C++ systems will resolve elaboration issues (such as calling constructors on global class instances) transparently during the link phase. GNAT has been adapted to ease the use of a foreign linker for the last phase. Three cases can be considered:

1. Using GNAT and G++ (GNU C++ compiler) from the same GCC installation: The C++ linker can simply be called by using the C++ specific driver called `g++`.

   Note that if the C++ code uses inline functions, you will need to compile your C++ code with the `-fkeep-inline-functions` switch in order to provide an existing function implementation that the Ada code can link with.

       $ g++ -c -fkeep-inline-functions file1.C
       $ g++ -c -fkeep-inline-functions file2.C
       $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++

2. Using GNAT and G++ from two different GCC installations: If both compilers are on the `PATH`, the previous method may be used. It is important to note that environment variables such as `C_INCLUDE_PATH`, `GCC_EXEC_PREFIX`, `BINUTILS_ROOT`, and `GCC_ROOT` will affect both compilers at the same time and may make one of the two compilers operate improperly if set during invocation of the wrong compiler. It is also very important that the linker uses the proper ``libgcc.a'` GCC library -- that is, the one from the C++ compiler installation. The implicit link command as suggested in the `gnatmake` command from the former example can be replaced by an explicit link command with the full-verbosity option in order to verify which library is used:

       $ gnatbind ada_unit
       $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++

   If there is a problem due to interfering environment variables, it can be worked around by using an intermediate script. The following example shows the proper script to use when GNAT has not been installed at its default location and g++ has been installed at its default location:

       $ cat ./my_script
       #!/bin/sh
       unset BINUTILS_ROOT
       unset GCC_ROOT
       c++ $*
       $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script

3. Using a non-GNU C++ compiler: The commands previously described can be used to insure that the C++ linker is used. Nonetheless, you need to add a few more parameters to the link command line, depending on the exception mechanism used.

   If the `setjmp/longjmp` exception mechanism is used, only the paths to the libgcc libraries are required:

       $ cat ./my_script
       #!/bin/sh
       CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
       $ gnatlink ada_unit file1.o file2.o --LINK=./my_script

   Where CC is the name of the non-GNU C++ compiler.

   If the `zero cost` exception mechanism is used, and the platform supports automatic registration of exception tables (e.g. Solaris), paths to more objects are required:

       $ cat ./my_script
       #!/bin/sh
       CC `gcc -print-file-name=crtbegin.o` $* \
       `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
       `gcc -print-file-name=crtend.o`
       $ gnatlink ada_unit file1.o file2.o --LINK=./my_script

   If the `zero cost` exception mechanism is used, and the platform doesn't support automatic registration of exception tables (e.g. HP-UX or AIX), the simple approach described above will not work and a pre-linking phase using GNAT will be necessary.

Another alternative is to use the `gprbuild` multi-language builder which has a large knowledge base and knows how to link Ada and C++ code together automatically in most cases.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.11.3 A Simple Example

The following example, provided as part of the GNAT examples, shows how to achieve procedural interfacing between Ada and C++ in both directions. The C++ class A has two methods. The first method is exported to Ada by the means of an extern C wrapper function. The second method calls an Ada subprogram. On the Ada side, The C++ calls are modelled by a limited record with a layout comparable to the C++ class. The Ada subprogram, in turn, calls the C++ method. So, starting from the C++ main program, the process passes back and forth between the two languages.

Here are the compilation commands:

    $ gnatmake -c simple_cpp_interface
    $ g++ -c cpp_main.C
    $ g++ -c ex7.C
    $ gnatbind -n simple_cpp_interface
    $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++
          -lstdc++ ex7.o cpp_main.o

Here are the corresponding sources:

    //cpp_main.C

    #include "ex7.h"

    extern "C" {
      void adainit (void);
      void adafinal (void);
      void method1 (A *t);
    }

    void method1 (A *t)
    {
      t->method1 ();
    }

    int main ()
    {
      A obj;
      adainit ();
      obj.method2 (3030);
      adafinal ();
    }

    //ex7.h

    class Origin {
     public:
      int o_value;
    };
    class A : public Origin {
     public:
      void method1 (void);
      void method2 (int v);
      A();
      int   a_value;
    };

    //ex7.C

    #include "ex7.h"
    #include <stdio.h>

    extern "C" { void ada_method2 (A *t, int v);}

    void A::method1 (void)
    {
      a_value = 2020;
      printf ("in A::method1, a_value = %d \n",a_value);

    }

    void A::method2 (int v)
    {
       ada_method2 (this, v);
       printf ("in A::method2, a_value = %d \n",a_value);

    }

    A::A(void)
    {
       a_value = 1010;
      printf ("in A::A, a_value = %d \n",a_value);
    }

    -- Ada sources
    package body Simple_Cpp_Interface is

       procedure Ada_Method2 (This : in out A; V : Integer) is
       begin
          Method1 (This);
          This.A_Value := V;
       end Ada_Method2;

    end Simple_Cpp_Interface;

    with System;
    package Simple_Cpp_Interface is
       type A is limited
          record
             Vptr    : System.Address;
             O_Value : Integer;
             A_Value : Integer;
          end record;
       pragma Convention (C, A);

       procedure Method1 (This : in out A);
       pragma Import (C, Method1);

       procedure Ada_Method2 (This : in out A; V : Integer);
       pragma Export (C, Ada_Method2);

    end Simple_Cpp_Interface;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.11.4 Interfacing with C++ constructors

In order to interface with C++ constructors GNAT provides the `pragma CPP_Constructor` (See section `Interfacing to C++' in GNAT Reference Manual, for additional information). In this section we present some common uses of C++ constructors in mixed-languages programs in GNAT.

Let us assume that we need to interface with the following C++ class:

    class Root {
    public:
      int  a_value;
      int  b_value;
      virtual int Get_Value ();
      Root();              // Default constructor
      Root(int v);         // 1st non-default constructor
      Root(int v, int w);  // 2nd non-default constructor
    };

For this purpose we can write the following package spec (further information on how to build this spec is available in 2.11.5 Interfacing with C++ at the Class Level and 29. Generating Ada Bindings for C and C++ headers).

    with Interfaces.C; use Interfaces.C;
    package Pkg_Root is
      type Root is tagged limited record
         A_Value : int;
         B_Value : int;
      end record;
      pragma Import (CPP, Root);

      function Get_Value (Obj : Root) return int;
      pragma Import (CPP, Get_Value);

      function Constructor return Root;
      pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");

      function Constructor (v : Integer) return Root;
      pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");

      function Constructor (v, w : Integer) return Root;
      pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
    end Pkg_Root;

On the Ada side the constructor is represented by a function (whose name is arbitrary) that returns the classwide type corresponding to the imported C++ class. Although the constructor is described as a function, it is typically a procedure with an extra implicit argument (the object being initialized) at the implementation level. GNAT issues the appropriate call, whatever it is, to get the object properly initialized.

Constructors can only appear in the following contexts:

- On the right side of an initialization of an object of type T.
- On the right side of an initialization of a record component of type T.
- In an Ada 2005 limited aggregate.
- In an Ada 2005 nested limited aggregate.
- In an Ada 2005 limited aggregate that initializes an object built in place by an extended return statement.

In a declaration of an object whose type is a class imported from C++, either the default C++ constructor is implicitly called by GNAT, or else the required C++ constructor must be explicitly called in the expression that initializes the object. For example:

      Obj1 : Root;
      Obj2 : Root := Constructor;
      Obj3 : Root := Constructor (v => 10);
      Obj4 : Root := Constructor (30, 40);

The first two declarations are equivalent: in both cases the default C++ constructor is invoked (in the former case the call to the constructor is implicit, and in the latter case the call is explicit in the object declaration). `Obj3` is initialized by the C++ non-default constructor that takes an integer argument, and `Obj4` is initialized by the non-default C++ constructor that takes two integers.

Let us derive the imported C++ class in the Ada side. For example:

      type DT is new Root with record
         C_Value : Natural := 2009;
      end record;

In this case the components DT inherited from the C++ side must be initialized by a C++ constructor, and the additional Ada components of type DT are initialized by GNAT. The initialization of such an object is done either by default, or by means of a function returning an aggregate of type DT, or by means of an extension aggregate.

      Obj5 : DT;
      Obj6 : DT := Function_Returning_DT (50);
      Obj7 : DT := (Constructor (30,40) with C_Value => 50);

The declaration of `Obj5` invokes the default constructors: the C++ default constructor of the parent type takes care of the initialization of the components inherited from Root, and GNAT takes care of the default initialization of the additional Ada components of type DT (that is, `C_Value` is initialized to value 2009). The order of invocation of the constructors is consistent with the order of elaboration required by Ada and C++. That is, the constructor of the parent type is always called before the constructor of the derived type.

Let us now consider a record that has components whose type is imported from C++. For example:

      type Rec1 is limited record
         Data1 : Root := Constructor (10);
         Value : Natural := 1000;
      end record;

      type Rec2 (D : Integer := 20) is limited record
         Rec   : Rec1;
         Data2 : Root := Constructor (D, 30);
      end record;

The initialization of an object of type `Rec2` will call the non-default C++ constructors specified for the imported components. For example:

      Obj8 : Rec2 (40);

Using Ada 2005 we can use limited aggregates to initialize an object invoking C++ constructors that differ from those specified in the type declarations. For example:

      Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
                              others => <>),
                      others => <>);

The above declaration uses an Ada 2005 limited aggregate to initialize `Obj9`, and the C++ constructor that has two integer arguments is invoked to initialize the `Data1` component instead of the constructor specified in the declaration of type `Rec1`. In Ada 2005 the box in the aggregate indicates that unspecified components are initialized using the expression (if any) available in the component declaration. That is, in this case discriminant `D` is initialized to value `20`, `Value` is initialized to value 1000, and the non-default C++ constructor that handles two integers takes care of initializing component `Data2` with values `20,30`.

In Ada 2005 we can use the extended return statement to build the Ada equivalent to C++ non-default constructors. For example:

      function Constructor (V : Integer) return Rec2 is
      begin
         return Obj : Rec2 := (Rec => (Data1  => Constructor (V, 20),
                                       others => <>),
                               others => <>) do
            --  Further actions required for construction of
            --  objects of type Rec2
            ...
         end record;
      end Constructor;

In this example the extended return statement construct is used to build in place the returned object whose components are initialized by means of a limited aggregate. Any further action associated with the constructor can be placed inside the construct.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.11.5 Interfacing with C++ at the Class Level

In this section we demonstrate the GNAT features for interfacing with C++ by means of an example making use of Ada 2005 abstract interface types. This example consists of a classification of animals; classes have been used to model our main classification of animals, and interfaces provide support for the management of secondary classifications. We first demonstrate a case in which the types and constructors are defined on the C++ side and imported from the Ada side, and latter the reverse case.

The root of our derivation will be the `Animal` class, with a single private attribute (the `Age` of the animal) and two public primitives to set and get the value of this attribute.

    class Animal {
     public:
       virtual void Set_Age (int New_Age);
       virtual int Age ();
     private:
       int Age_Count;
    };

Abstract interface types are defined in C++ by means of classes with pure virtual functions and no data members. In our example we will use two interfaces that provide support for the common management of `Carnivore` and `Domestic` animals:

    class Carnivore {
    public:
       virtual int Number_Of_Teeth () = 0;
    };

    class Domestic {
    public:
       virtual void Set_Owner (char* Name) = 0;
    };

Using these declarations, we can now say that a `Dog` is an animal that is both Carnivore and Domestic, that is:

    class Dog : Animal, Carnivore, Domestic {
     public:
       virtual int  Number_Of_Teeth ();
       virtual void Set_Owner (char* Name);

       Dog(); // Constructor
     private:
       int  Tooth_Count;
       char *Owner;
    };

In the following examples we will assume that the previous declarations are located in a file named `animals.h`. The following package demonstrates how to import these C++ declarations from the Ada side:

    with Interfaces.C.Strings; use Interfaces.C.Strings;
    package Animals is
      type Carnivore is interface;
      pragma Convention (C_Plus_Plus, Carnivore);
      function Number_Of_Teeth (X : Carnivore)
         return Natural is abstract;

      type Domestic is interface;
      pragma Convention (C_Plus_Plus, Set_Owner);
      procedure Set_Owner
        (X    : in out Domestic;
         Name : Chars_Ptr) is abstract;

      type Animal is tagged record
        Age : Natural := 0;
      end record;
      pragma Import (C_Plus_Plus, Animal);

      procedure Set_Age (X : in out Animal; Age : Integer);
      pragma Import (C_Plus_Plus, Set_Age);

      function Age (X : Animal) return Integer;
      pragma Import (C_Plus_Plus, Age);

      type Dog is new Animal and Carnivore and Domestic with record
        Tooth_Count : Natural;
        Owner       : String (1 .. 30);
      end record;
      pragma Import (C_Plus_Plus, Dog);

      function Number_Of_Teeth (A : Dog) return Integer;
      pragma Import (C_Plus_Plus, Number_Of_Teeth);

      procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
      pragma Import (C_Plus_Plus, Set_Owner);

      function New_Dog return Dog;
      pragma CPP_Constructor (New_Dog);
      pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
    end Animals;

Thanks to the compatibility between GNAT run-time structures and the C++ ABI, interfacing with these C++ classes is easy. The only requirement is that all the primitives and components must be declared exactly in the same order in the two languages.

Regarding the abstract interfaces, we must indicate to the GNAT compiler by means of a `pragma Convention (C_Plus_Plus)`, the convention used to pass the arguments to the called primitives will be the same as for C++. For the imported classes we use `pragma Import` with convention `C_Plus_Plus` to indicate that they have been defined on the C++ side; this is required because the dispatch table associated with these tagged types will be built in the C++ side and therefore will not contain the predefined Ada primitives which Ada would otherwise expect.

As the reader can see there is no need to indicate the C++ mangled names associated with each subprogram because it is assumed that all the calls to these primitives will be dispatching calls. The only exception is the constructor, which must be registered with the compiler by means of `pragma CPP_Constructor` and needs to provide its associated C++ mangled name because the Ada compiler generates direct calls to it.

With the above packages we can now declare objects of type Dog on the Ada side and dispatch calls to the corresponding subprograms on the C++ side. We can also extend the tagged type Dog with further fields and primitives, and override some of its C++ primitives on the Ada side. For example, here we have a type derivation defined on the Ada side that inherits all the dispatching primitives of the ancestor from the C++ side.

    with Animals; use Animals;
    package Vaccinated_Animals is
      type Vaccinated_Dog is new Dog with null record;
      function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
    end Vaccinated_Animals;

It is important to note that, because of the ABI compatibility, the programmer does not need to add any further information to indicate either the object layout or the dispatch table entry associated with each dispatching operation.

Now let us define all the types and constructors on the Ada side and export them to C++, using the same hierarchy of our previous example:

    with Interfaces.C.Strings;
    use Interfaces.C.Strings;
    package Animals is
      type Carnivore is interface;
      pragma Convention (C_Plus_Plus, Carnivore);
      function Number_Of_Teeth (X : Carnivore)
         return Natural is abstract;

      type Domestic is interface;
      pragma Convention (C_Plus_Plus, Set_Owner);
      procedure Set_Owner
        (X    : in out Domestic;
         Name : Chars_Ptr) is abstract;

      type Animal is tagged record
        Age : Natural := 0;
      end record;
      pragma Convention (C_Plus_Plus, Animal);

      procedure Set_Age (X : in out Animal; Age : Integer);
      pragma Export (C_Plus_Plus, Set_Age);

      function Age (X : Animal) return Integer;
      pragma Export (C_Plus_Plus, Age);

      type Dog is new Animal and Carnivore and Domestic with record
        Tooth_Count : Natural;
        Owner       : String (1 .. 30);
      end record;
      pragma Convention (C_Plus_Plus, Dog);

      function Number_Of_Teeth (A : Dog) return Integer;
      pragma Export (C_Plus_Plus, Number_Of_Teeth);

      procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
      pragma Export (C_Plus_Plus, Set_Owner);

      function New_Dog return Dog'Class;
      pragma Export (C_Plus_Plus, New_Dog);
    end Animals;

Compared with our previous example the only difference is the use of `pragma Export` to indicate to the GNAT compiler that the primitives will be available to C++. Thanks to the ABI compatibility, on the C++ side there is nothing else to be done; as explained above, the only requirement is that all the primitives and components are declared in exactly the same order.

For completeness, let us see a brief C++ main program that uses the declarations available in `animals.h` (presented in our first example) to import and use the declarations from the Ada side, properly initializing and finalizing the Ada run-time system along the way:

    #include "animals.h"
    #include <iostream>
    using namespace std;

    void Check_Carnivore (Carnivore *obj) {...}
    void Check_Domestic (Domestic *obj)   {...}
    void Check_Animal (Animal *obj)       {...}
    void Check_Dog (Dog *obj)             {...}

    extern "C" {
      void adainit (void);
      void adafinal (void);
      Dog* new_dog ();
    }

    void test ()
    {
      Dog *obj = new_dog();  // Ada constructor
      Check_Carnivore (obj); // Check secondary DT
      Check_Domestic (obj);  // Check secondary DT
      Check_Animal (obj);    // Check primary DT
      Check_Dog (obj);       // Check primary DT
    }

    int main ()
    {
      adainit ();  test();  adafinal ();
      return 0;
    }

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.12 Comparison between GNAT and C/C++ Compilation Models

The GNAT model of compilation is close to the C and C++ models. You can think of Ada specs as corresponding to header files in C. As in C, you don't need to compile specs; they are compiled when they are used. The Ada `with` is similar in effect to the `#include` of a C header.

One notable difference is that, in Ada, you may compile specs separately to check them for semantic and syntactic accuracy. This is not always possible with C headers because they are fragments of programs that have less specific syntactic or semantic rules.

The other major difference is the requirement for running the binder, which performs two important functions. First, it checks for consistency. In C or C++, the only defense against assembling inconsistent programs lies outside the compiler, in a makefile, for example. The binder satisfies the Ada requirement that it be impossible to construct an inconsistent program when the compiler is used in normal mode.

The other important function of the binder is to deal with elaboration issues. There are also elaboration issues in C++ that are handled automatically. This automatic handling has the advantage of being simpler to use, but the C++ programmer has no control over elaboration. Where `gnatbind` might complain there was no valid order of elaboration, a C++ compiler would simply construct a program that malfunctioned at run time.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.13 Comparison between GNAT and Conventional Ada Library Models

This section is intended for Ada programmers who have used an Ada compiler implementing the traditional Ada library model, as described in the Ada Reference Manual.

In GNAT, there is no "library" in the normal sense. Instead, the set of source files themselves acts as the library. Compiling Ada programs does not generate any centralized information, but rather an object file and a ALI file, which are of interest only to the binder and linker. In a traditional system, the compiler reads information not only from the source file being compiled, but also from the centralized library. This means that the effect of a compilation depends on what has been previously compiled. In particular:

- When a unit is `with`'ed, the unit seen by the compiler corresponds to the version of the unit most recently compiled into the library.

- Inlining is effective only if the necessary body has already been compiled into the library.

- Compiling a unit may obsolete other units in the library.

In GNAT, compiling one unit never affects the compilation of any other units because the compiler reads only source files. Only changes to source files can affect the results of a compilation. In particular:

- When a unit is `with`'ed, the unit seen by the compiler corresponds to the source version of the unit that is currently accessible to the compiler.

- Inlining requires the appropriate source files for the package or subprogram bodies to be available to the compiler. Inlining is always effective, independent of the order in which units are complied.

- Compiling a unit never affects any other compilations. The editing of sources may cause previous compilations to be out of date if they depended on the source file being modified.

The most important result of these differences is that order of compilation is never significant in GNAT. There is no situation in which one is required to do one compilation before another. What shows up as order of compilation requirements in the traditional Ada library becomes, in GNAT, simple source dependencies; in other words, there is only a set of rules saying what source files must be present when a file is compiled.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by GNAT Mailserver on April, 17 2014 using texi2html