[ Table of Contents ]
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) |
||||
|
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) |
||
|
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) |
||||
|
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 page ]