-
A package_body shall be the completion of a previous package_declaration
or generic_package_declaration. A library package_declaration or library
generic_package_declaration shall not have a body unless it requires a
body; pragma Elaborate_Body can be used to require a
library_unit_declaration to have a body, See section 10.2.1 Elaboration Control, if it would not
otherwise require one.
Static Semantics
-
In any package_body without statements there is an implicit
null_statement. For any package_declaration without an explicit
completion, there is an implicit package_body containing a single
null_statement. For a noninstance, nonlibrary package, this body occurs
at the end of the declarative_part of the innermost enclosing program
unit or block_statement; if there are several such packages, the order
of the implicit package_bodies is unspecified. (For an instance, the
implicit package_body occurs at the place of the instantiation,
See section 12.3 Generic Instantiation. For a library package, the place is partially determined by
the elaboration dependences, See section 10 Program Structure and Compilation Issues.
Dynamic Semantics
-
For the elaboration of a nongeneric package_body, its declarative_part
is first elaborated, and its handled_sequence_of_statements is then
executed.
NOTES
-
(3) A variable declared in the body of a package is only visible within
this body and, consequently, its value can only be changed within the
package_body. In the absence of local tasks, the value of such a
variable remains unchanged between calls issued from outside the package
to subprograms declared in the visible part. The properties of such a
variable are similar to those of a "static" variable of C.
-
(4) The elaboration of the body of a subprogram explicitly declared in
the visible part of a package is caused by the elaboration of the body
of the package. Hence a call of such a subprogram by an outside program
unit raises the exception Program_Error if the call takes place before
the elaboration of the package_body, See section 3.11 Declarative Parts.
Examples
-
Example of a package body, See section 7.1 Package Specifications and Declarations
-
package body Rational_Numbers is
-
procedure Same_Denominator (X,Y : in out Rational) is
begin
-- reduces X and Y to the same denominator:
...
end Same_Denominator;
-
function "="(X,Y : Rational) return Boolean is
U : Rational := X;
V : Rational := Y;
begin
Same_Denominator (U,V);
return U.Numerator = V.Numerator;
end "=";
-
function "/" (X,Y : Integer) return Rational is
begin
if Y > 0 then
return (Numerator => X, Denominator => Y);
else
return (Numerator => -X, Denominator => -Y);
end if;
end "/";
-
function "+" (X,Y : Rational) return Rational is ... end "+";
function "-" (X,Y : Rational) return Rational is ... end "-";
function "*" (X,Y : Rational) return Rational is ... end "*";
function "/" (X,Y : Rational) return Rational is ... end "/";
-
end Rational_Numbers;