2.8 Compilation Units

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

A compilation unit is a section of text that can be submitted to the compiler, in order to create one or more modules of a program. Three examples are given below. You have already seen these examples in earlier pages, but here we make clear the relationship between the compilation unit and the associated program unit or units.

In the first example the compilation consists of a context clause followed by the code defining a library procedure body. Thus, the compilation unit is slightly larger than the program unit text.

Compilation Unit (Library Procedure)

Context Clause
with Show_Birthday_Song;
Program Unit
procedure Test_Show_Birthday_Song is

  Given_Name : constant String := "Ada";
  Sur_Name   : constant String := "Lovelace";

begin

  Show_Birthday_Song(Given_Name, Sur_Name);

end Test_Show_Birthday_Song;

In the second example the compilation unit is exactly the same as the program unit text -- a library function that doesn't happen to require any context clauses.

Compilation Unit (Library Function)

Program Unit

function To_Fahrenheit (C : Float) return Float is

  Base  : constant Float := 32.0;
  Ratio : constant Float := 1.8;

begin

  return (Base + Ratio*C):

end To_Fahrenheit;

In the third example there are two compilation units, each containing one part (declaration or body) of a library package. In both cases it so happens that neither of these components require any with clauses to define the context.

Two Compilation Units (Library Package Declaration and Body)

First Part of a Program Unit

package Temperature_Conversions is

  function To_Fahrenheit (C : Float) return Float;
  function To_Celsius(F : Float) return Float;

end Temperature_Conversions;

Second Part of a Program Unit

package body Temperature_Conversions is

  Base   : constant Float := 32.0;
  Ratio  : constant Float :=  1.8;

  function To_Fahrenheit (C : Float) return Float is
  begin
    return Base + Ratio*C;
  end To_Fahrenheit;

  function To_Celsius(F : Float) return Float is
  begin
    return (F - Base)/Ratio;
  end To_Celsius;

end Temperature_Conversions;

Separate, but not independent, compilation -- a key feature of Ada

When separate compilation units are compiled, each compilation takes place within a context defined by withing relationships and other relationships. Thus if unit A withs in unit B, the compilation of A is done with full knowledge of the declaration (external view) of unit B. For example, assume that unit B is a package that exports procedure P and that there are several formal parameters involved in calling P. (We speak of the parameter profile of P.) During the compilation of A the compiler performs a set of checks to see that every call to P includes a set of actual parameters that conform to the parameter profile of P. That is, the types and values of the actual parameters created in unit A must conform to the types and constraints specified for the formal parameters of P, as defined in package B. Thus, the compiler enforces consistency between the separate modules. This is referred to as separate, but not independent, compilation. It is one of the features of Ada that helps developers create large systems composed of many modules that work together in a correct and consistent fashion. Once the various modules have been successfully compiled, it is likely that they will work together in a consistent and correct manner -- with far less need for de-bugging than has been traditionally the case in the experience of many.

The Program Library

When Ada programmers speak of a library or "program library," they refer to a data base of information related to all of the compilation units needed for a particular program. This data base forms the context mentioned above. It is enlarged whenever a new compilation unit is compiled and added to the library.

Other examples of compilation units

The three examples given above do not cover all the possible kinds of compilation units. A very small, two line, compilation unit can be built using a renaming declaration, as in the following example.

with Ada.Text_IO;
package Tio renames Ada.Text_IO;

Another kind of example would be a compilation unit that contains a separately compiled subunit body, a topic covered in a later section.

Related Topics

2.2 Program Units 2.14 Subunits

[ Back to top of pagePrev ] Next ]