Elaboration Code (GNAT User’s Guide for Native Platforms)

### 9.1 Elaboration Code ¶

Ada defines the term ‘execution’ as the process by which a construct achieves its run-time effect. This process is also referred to as ‘elaboration’ for declarations and ‘evaluation’ for expressions.

The execution model in Ada allows for certain sections of an Ada program to be executed prior to execution of the program itself, primarily with the intent of initializing data. These sections are referred to as ‘elaboration code’. Elaboration code is executed as follows:

- All partitions of an Ada program are executed in parallel with one another, possibly in a separate address space and possibly on a separate computer.
- The execution of a partition involves running the environment task for that partition.
- The environment task executes all elaboration code (if available) for all units within that partition. This code is said to be executed at ‘elaboration time’.
- The environment task executes the Ada program (if available) for that partition.

In addition to the Ada terminology, this appendix defines the following terms:

- ‘Invocation’

  The act of calling a subprogram, instantiating a generic, or activating a task.

- ‘Scenario’

  A construct that is elaborated or invoked by elaboration code is referred to as an ‘elaboration scenario’ or simply a ‘scenario’. GNAT recognizes the following scenarios:

  - `'Access` of entries, operators, and subprograms
  - Activation of tasks
  - Calls to entries, operators, and subprograms
  - Instantiations of generic templates
- ‘Target’

  A construct elaborated by a scenario is referred to as an ‘elaboration target’ or simply a ‘target’. GNAT recognizes the following targets:

  - For `'Access` of entries, operators, and subprograms, the target is the entry, operator, or subprogram being aliased.
  - For activation of tasks, the target is the task body
  - For calls to entries, operators, and subprograms, the target is the entry, operator, or subprogram being invoked.
  - For instantiations of generic templates, the target is the generic template being instantiated.

Elaboration code may appear in two distinct contexts:

- ‘Library level’

  A scenario appears at the library level when it is encapsulated by a package [body] compilation unit, ignoring any other package [body] declarations in between.

      with Server;
      package Client is
         procedure Proc;

         package Nested is
            Val : ... := Server.Func;
         end Nested;
      end Client;

  In the example above, the call to `Server.Func` is an elaboration scenario because it appears at the library level of package `Client`. Note that the declaration of package `Nested` is ignored according to the definition given above. As a result, the call to `Server.Func` will be invoked when the spec of unit `Client` is elaborated.

- ‘Package body statements’

  A scenario appears within the statement sequence of a package body when it is bounded by the region starting from the `begin` keyword of the package body and ending at the `end` keyword of the package body.

      package body Client is
         procedure Proc is
         begin
            ...
         end Proc;
      begin
         Proc;
      end Client;

  In the example above, the call to `Proc` is an elaboration scenario because it appears within the statement sequence of package body `Client`. As a result, the call to `Proc` will be invoked when the body of `Client` is elaborated.