Section 2.4 - Compilation Units

Now that we know about program units, packages, and the difference between declarations and bodies, we can talk about compilation units.

A compilation unit contains either the declaration or the body of a program unit, preceded by the necessary context clause (`with' or `use' clauses). Thus a compilation unit can be a package declaration, a package body, a subprogram declaration, or a subprogram body along with its context clause.

An Ada compiler only compiles collections of one or more compilation units. That's why it's important to understand compilation units - to compile something, it must be a part of a legal compilation unit.

Here's a simplified BNF syntax for a compilation unit. The full definition is in section 10.1 of the RM:

  1. compilation_unit ::= context_clause library_item
  2. context_clause ::= {context_item}
  3. context_item ::= with_clause | use_clause
  4. with_clause ::= "with" library_unit_name { "," library_unit_name} ";"
  5. use_clause ::= "use" library_unit_name { "," library_unit_name} ";"
  6. library_item ::= package_declaration | package_body | subprogram_declaration | subprogram_body

Note that compilation units start with "with" and "use" clauses, followed by a program unit declaration or body. We've already seen three compilation units - the simplified package declaration for Text_IO and two versions of the "Hello" program (in two different forms). Let's go back and look at the "Hello2" program to see that it really is a valid compilation unit:

-- Print a simple message to demonstrate a trivial Ada program.
with Ada.Text_IO;
use Ada.Text_IO;  -- use clause - automatically search Ada.Text_IO.
procedure Hello2 is
begin
 Put_Line("Hello, world!"); -- Note: No longer has "Ada.Text_IO" in front.
end Hello2;

Ada compilers will only compile a compilation_unit, and according to the BNF definition above a compilation_unit is a context_clause followed by a library_item. The BNF also says that a context_clause is a set of zero or more context_items, and a context_item is either a "with" clause or "use" clause. Since Hello2 starts off with a "with" clause and a "use" clause, we have two context_items that make up the context_clause. I haven't shown the definition of a library_item, but it turns out that the rest of Hello2 is a valid library_item, so we have a valid compilation unit.

Although most Ada compilers permit multiple compilation units in a single file, it's usually better to put separate compilation units in separate files. One Ada compiler (GNAT) requires different compilation units to be in different files.

Informally, when people say `show me X's package declaration' they really mean `show me the compilation unit that includes the package declaration of package X'.


Quiz:

Which of the following cannot be part of a compilation unit after the context clause?

  1. A package declaration
  2. A procedure declaration
  3. A procedure body
  4. A type definition

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 2 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s2s4.htm".