Ada Conformity Assessment Authority |
Home |
Conformity Assessment | Test Suite |
ARG | Ada Standard |

Certain expressions of a scalar or string type are
defined to be static. Similarly, certain discrete ranges are defined
to be static, and certain scalar and string subtypes are defined to be
static subtypes. [ *Static* means determinable
at compile time, using the declared properties or values of the program
entities.]

For an expression to be static, it has to be
calculable at compile time.

Only scalar and string expressions are static.

To be static, an expression cannot have any
nonscalar, nonstring subexpressions (though it can have nonscalar constituent
names). A
static scalar expression cannot have any nonscalar subexpressions. There
is one exception — a membership test for a string subtype can be
static, and the result is scalar, even though a subexpression is nonscalar.

The rules for evaluating static expressions
are designed to maximize portability of static calculations.

a string_literal
of a static string subtype;

a name
that denotes the declaration of a named number or a static constant;

a function_call
whose *function_*name
or *function_*prefix
statically denotes a static function, and whose actual parameters, if
any (whether given explicitly or by default), are all static expressions;

an attribute_reference
that denotes a scalar value, and whose prefix
denotes a static scalar subtype;

An implementation may define the staticness
and other properties of implementation-defined attributes.

{*AI12-0368-1*}
an attribute_reference
whose prefix
statically names denotes
a statically constrained array object or array subtype, and whose attribute_designator
is First, Last, or Length, with an optional dimension;

{*AI12-0064-2*}
an attribute_reference
whose prefix
denotes a non-generic entity that is not declared in a generic unit,
and whose attribute_designator
is Nonblocking;

{*AI12-0201-1*}
a type_conversion
whose subtype_mark
denotes a static [(scalar or string)] scalar
subtype, and whose operand is a static expression;

a qualified_expression
whose subtype_mark
denotes a static [(scalar or string)] subtype, and whose operand is a
static expression;

{*AI05-0158-1*}
{*AI05-0269-1*}
{*AI12-0039-1*}
a membership test whose *tested_*simple_expression simple_expression
is a static expression, and whose membership_choice_list
consists only of membership_choices
that are either static *choice_*simple_expressions choice_expressions,
static ranges,
or subtype_marks
that denote a static [(scalar or string)] subtype;

a short-circuit control form both of whose relations
are static expressions;

{*AI05-0147-1*}
{*AI05-0188-1*}
a conditional_expression
all of whose conditions,
*selecting_*expressions,
and *dependent_*expressions
are static expressions;

{*AI12-0368-1*}
a declare_expression
whose *body_*expression
is static and each of whose declarations, if any, is either the declaration
of a static constant or is an object_renaming_declaration
with an *object_*name
that statically names the renamed object;

a static expression enclosed in parentheses.

It is a direct_name,
expanded name, or character_literal,
and it denotes a declaration other than a renaming_declaration;
or

It is an attribute_reference
whose prefix
statically denotes some entity; or

{*AI12-0322-1*}
It is a target_name
(see 5.2.1) in an assignment_statement
whose *variable_*name
statically denotes some entity; or

It denotes a renaming_declaration
with a name
that statically denotes the renamed entity.

{*AI12-0368-1*}
A name
*statically names* an object if it:

{*AI12-0368-1*}
statically denotes the declaration of an object
[(possibly through one or more renames)];

{*AI12-0368-1*}
{*AI12-0373-1*}
is a selected_component
whose prefix statically names an object, there is no implicit dereference
of the prefix, and the selector_name
does not denote a component_declaration
occurring within a variant_part;
or

{*AI12-0368-1*}
is an indexed_component
whose prefix statically names an object, there is no implicit dereference
of the prefix, the object is statically constrained, and the index expressions
of the object are static and have values that are within the range of
the index constraint.

{*AI12-0368-1*}
For an entity other than an object, a name
statically names an entity if the name
statically denotes the entity.

a predefined operator whose parameter and result
types are all scalar types none of which are descendants of formal scalar
types;

{*AI12-0201-1*}
a predefined relational operator whose parameters
are of a string type that is not a descendant of a formal array type;

{*AI12-0201-1*}
a predefined concatenation operator whose result type is a string type that is not a descendant of a formal array type;

an enumeration literal;

{*AI12-0075-1*}
a static expression function (see 6.8);

a language-defined attribute that is a function,
if the prefix
denotes a static scalar subtype, and if the parameter and result types
are scalar.

In any case, a generic formal subprogram is not a
static function.

{*AI12-0201-1*}
A *static constant* is a constant view declared
by a full constant declaration or an object_renaming_declaration
with a static nominal subtype, having a value defined by a static scalar
expression or by a static string expression whose
value has a length not exceeding the maximum length of a string_literal
in the implementation.

A *static range* is a range
whose bounds are static expressions, [or a range_attribute_reference
that is equivalent to such a range.]
A *static **discrete_range*
is one that is a static range or is a subtype_indication
that defines a static scalar subtype. The base range of a scalar type
is a static range, unless the type is a descendant of a formal scalar
type.

{*AI95-00263-01*}
{*AI05-0153-3*}
A *static subtype* is either a *static scalar
subtype* or a *static string subtype*. A
static scalar subtype is an unconstrained scalar subtype whose type is
not a descendant of a formal type, or a constrained scalar subtype formed
by imposing a compatible static constraint on a static scalar subtype.
A static string subtype is an unconstrained string
subtype whose index subtype and component subtype are static, or a constrained
string subtype formed by imposing a compatible static constraint on a
static string subtype. In any case, the subtype of a generic formal object
of mode **in out**, and the result subtype of a generic formal function,
are not static. Also, a subtype is not static if any Dynamic_Predicate
specifications apply to it.

F :

--

X : Integer **range** 1..20;

**procedure** I **is** **new** G(F => X); --* OK.*

The case_statement
is illegal, because the subtype of F is not static, so the choices have
to cover all values of Integer, not just those in the range 1..10. A
similar issue arises for generic formal functions, now that function
calls are object names.

A null constraint is always static;

An index constraint is static
if each discrete_range
is static, and each index subtype of the corresponding array type is
static;

A discriminant constraint is
static if each expression
of the constraint is static, and the subtype of each discriminant is
static.

{*AI95-00311-01*}
In any case, the constraint of the first subtype of a scalar formal type
is neither static nor null.

A subtype is *statically constrained*
if it is constrained, and its constraint is static. An object is *statically
constrained* if its nominal subtype is statically constrained, or
if it is a static string constant.

{*AI05-0147-1*}
the right operand of a static short-circuit control form whose value
is determined by its left operand; or

{*AI05-0147-1*}
{*AI05-0188-1*}
a *dependent_*expression
of an if_expression
whose associated condition
is static and equals False; or

{*AI05-0147-1*}
{*AI05-0188-1*}
a condition
or *dependent_*expression
of an if_expression
where the condition
corresponding to at least one preceding *dependent_*expression
of the if_expression
is static and equals True; or

(**if** N = 0 **then** Min **elsif** 10_000/N > Min **then** 10_000/N **else** Min)

legal if N and Min are static and N = 0.

{*AI05-0188-1*}
{*AI05-0269-1*}
a *dependent_*expression
of a case_expression
whose *selecting_*expression
is static and whose value is not covered by the corresponding discrete_choice_list;
or

{*AI05-0158-1*}
{*AI12-0039-1*}
a *choice_*simple_expression choice_expression
(or a simple_expression
of a range
that occurs as a membership_choice
of a membership_choice_list)
of a static membership test that is preceded in the enclosing membership_choice_list
by another item whose individual membership test (see 4.5.2)
statically yields True.

{*AI05-0147-1*}
A static expression is evaluated at compile time except when it is statically
unevaluated. The compile-time evaluation of a static expression is performed
exactly, without performing Overflow_Checks. For a static expression
that is evaluated:

{*AI05-0262-1*}
The expression is illegal if its evaluation fails a language-defined
check other than Overflow_Check. For the purposes of this evaluation,
the assertion policy is assumed to be Check.

{*AI95-00269-01*}
If the expression is not part of a larger static expression and the expression
is expected to be of a single specific type, then its value shall be
within the base range of its expected type. Otherwise, the value may
be arbitrarily large or small.

{*AI95-00269-01*}
If the expression is of type *universal_real* and its expected type
is a decimal fixed point type, then its value shall be a multiple of
the *small* of the decimal type. This restriction does not apply
if the expected type is a descendant of a formal scalar type (or a corresponding
actual type in an instance).

{*AI95-00269-01*}
In addition to the places where Legality Rules normally
apply (see 12.3), the above restrictions also
apply in the private part of an instance of a generic unit.

Short-circuit
control forms are a special case:

N: **constant** := 0.0;

X:**constant** Boolean := (N = 0.0) **or** **else** (1.0/N > 0.5); --* Static.*

X:

The declaration of X is legal, since the divide-by-zero
part of the expression is not evaluated. X is a static constant equal
to True.

{*AI12-0075-1*}
The preceding “statically unevaluated”
rule allows

X : **constant** := (**if** True **then** 37 **else** (1 / 0));

but
does not allow

(

X :

because
evaluation of a function call includes evaluation of all of its actual
parameters.

{*AI95-00268-01*}
{*AI95-00269-01*}
For a real static expression that is not part of a larger static expression,
and whose expected type is not a descendant of a formal type, the implementation
shall round or truncate the value (according to the Machine_Rounds attribute
of the expected type) to the nearest machine number of the expected type;
if the value is exactly half-way between two machine numbers, the rounding
performed is implementation-defined. If the expected type is a descendant
of a formal type, or if the static expression appears in the body of
an instance of a generic unit and the corresponding expression is nonstatic
in the corresponding generic body, then no special rounding or truncating
is required — normal accuracy rules apply (see Annex
G).

When the expected type is a descendant of a
formal floating point type, extended precision (beyond that of the machine
numbers) can be retained when evaluating a static expression, to ease
code sharing for generic instantiations. For similar reasons, normal
(nondeterministic) rounding or truncating rules apply for descendants
of a formal fixed point type.

{*AI95-00269-01*}
There is no requirement for exact evaluation or special rounding in an
instance body (unless the expression is static in the generic body).
This eliminates a potential contract issue where the exact value of a
static expression depends on the actual parameters (which could then
affect the legality of other code).

{*AI95-00100-01*}
Note that the only machine numbers of a fixed point type are the multiples
of the small, so a static conversion to a fixed-point type, or division
by an integer, must do truncation to a multiple of small. It is not correct
for the implementation to do all static calculations in infinite precision.

{*AI95-00268-01*}
For a real static expression that is not part of a larger static expression,
and whose expected type is not a descendant of a formal type, the rounding
should be the same as the default rounding for the target system.

NOTES

29 An expression can be static even if
it occurs in a context where staticness is not required.

X : Float := Float'(1.0E+400) + 1.0 - Float'(1.0E+400);

The expression is static, which means that the
value of X must be exactly 1.0, independent of the accuracy or range
of the run-time floating point implementation.

The following kinds of expressions are never
static: explicit_dereference,
indexed_component,
slice, **null**,
aggregate,
allocator.

30 A static (or run-time) type_conversion
from a real type to an integer type performs rounding. If the operand
value is exactly half-way between two integers, the rounding is performed
away from zero.

1 + 1 *-- 2*

**abs**(-10)*3 *-- 30*

Kilo : **constant** := 1000;

Mega :**constant** := Kilo*Kilo; *-- 1_000_000*

Long :**constant** := Float'Digits*2;

Mega :

Long :

Half_Pi : **constant** := Pi/2; *-- see 3.3.2*

Deg_To_Rad :**constant** := Half_Pi/90;

Rad_To_Deg :**constant** := 1.0/Deg_To_Rad; *-- equivalent to 1.0/((3.14159_26536/2)/90)*

Deg_To_Rad :

Rad_To_Deg :

The rules for static expressions
and static subtypes are generalized to allow more kinds of compile-time-known
expressions to be used where compile-time-known values are required,
as follows:

Membership tests and short-circuit control
forms may appear in a static expression.

The bounds and length of statically constrained
array objects or subtypes are static.

The Range attribute of a statically constrained
array subtype or object gives a static range.

A type_conversion
is static if the subtype_mark
denotes a static scalar subtype and the operand is a static expression.

All numeric literals are now static, even
if the expected type is a formal scalar type. This is useful in case_statements
and variant_parts,
which both now allow a value of a formal scalar type to control the selection,
to ease conversion of a package into a generic package. Similarly, named
array aggregates are also permitted for array types with an index type
that is a formal scalar type.

The rules for the evaluation of static expressions
are revised to require exact evaluation at compile time, and force a
machine number result when crossing from the static realm to the dynamic
realm, to enhance portability and predictability. Exact evaluation is
not required for descendants of a formal scalar type, to simplify generic
code sharing and to avoid generic contract model problems.

Static expressions
are legal even if an intermediate in the expression goes outside the
base range of the type. Therefore, the following will succeed in Ada
95, whereas it might raise an exception in Ada 83:

I : Short_Int := -32_768;

This might raise an exception in Ada 83 because
"32_768" is out of range, even though "–32_768"
is not. In Ada 95, this will always succeed.

Certain expressions involving string operations
(in particular concatenation and membership tests) are considered static
in Ada 95.

The reason for this change is to simplify the
rule requiring compile-time-known string expressions as the link name
in an interfacing pragma, and to simplify the preelaborability rules.

An Ada 83 program that uses
an out-of-range static value is illegal in Ada 95, unless the expression
is part of a larger static expression, or the expression is not evaluated
due to being on the right-hand side of a short-circuit control form.

{*AI05-0299-1*}
This subclause (and 4.5.5, “Multiplying
Operators”) subsumes the RM83 section on Universal Expressions.

The existence of static string expressions necessitated
changing the definition of static subtype to include string subtypes.
Most occurrences of "static subtype" have been changed to "static
scalar subtype", in order to preserve the effect of the Ada 83 rules.
This has the added benefit of clarifying the difference between "static
subtype" and "statically constrained subtype", which has
been a source of confusion. In cases where we allow static string subtypes,
we explicitly use phrases like "static string subtype" or "static
(scalar or string) subtype", in order to clarify the meaning for
those who have gotten used to the Ada 83 terminology.

In Ada 83, an
expression was considered nonstatic if it raised an exception. Thus,
for example:

Bad: **constant** := 1/0; --* Illegal!*

was illegal because 1/0 was not static. In Ada
95, the above example is still illegal, but for a different reason: 1/0
is static, but there's a separate rule forbidding the exception raising.

{*AI95-00268-01*}
**Amendment Correction:** Rounding of static real
expressions is implementation-defined in Ada 2005, while it was specified
as away from zero in (original) Ada 95. This could make subtle differences
in programs. However, the original Ada 95 rule required rounding that
(probably) differed from the target processor, thus creating anomalies
where the value of a static expression was required to be different than
the same expression evaluated at run time.

{*AI95-00263-01*}
{*AI95-00268-01*}
The Ada 95 wording that defined static subtypes unintentionally failed
to exclude formal derived types that happen to be scalar (these aren't
formal scalar types); and had a parenthetical remark excluding formal
string types - but that was neither necessary nor parenthetical (it didn't
follow from other wording). This issue also applies to the rounding rules
for real static expressions.

{*AI95-00269-01*}
Ada 95 didn't clearly define the bounds of a value of a static expression
for universal types and for "any integer/float/fixed type".
We also make it clear that we do not intend exact evaluation of static
expressions in an instance body if the expressions aren't static in the
generic body.

{*AI95-00311-01*}
We clarify that the first subtype of a scalar formal type has a nonstatic,
nonnull constraint.

{*AI05-0147-1*}
{*AI05-0188-1*}
Added wording to define staticness and the lack of evaluation for if_expressions
and case_expressions.
These are new and defined elsewhere.

{*AI05-0153-3*}
Added wording to prevent subtypes that have dynamic predicates (see 3.2.4)
from being static.

{*AI05-0158-1*}
Revised wording for membership tests to allow for the new possibilities
allowed by the membership_choice_list.

{*AI12-0201-1*}
Added a missing exclusion for
concatenations of a string type descended from a formal array type. This
could potentially make some expression non-static; but as that could
only matter in a context where a static string is required (such as the
Link_Name aspect), it is quite unlikely.

{*AI12-0201-1*}
Expressions involving string
relational operators or string type conversions now can be static.

{*AI12-0064-2*}
Defined the staticness of the Nonblocking attribute
(see 9.5).

{*AI12-0075-1*}
Expression functions can be static if declared
correctly; this is documented as an extension in 6.8.

{*AI12-0249-1*}
A numeric_literal
can be non-static if they are defined by an Integer_Literal or Real_Literal
aspect (see 4.2.1).

{*AI12-0322-1*}
Clarified that a target name symbol can statically
denote an entity if the associated *variable_*name
statically denotes an entity. This is necessary so that target names
participate in the anti-order-dependence checks of 6.4.1.

{*AI12-0368-1*}
{*AI12-0373-1*}
Added wording to define staticness for declare_expressions.
Also moved “statically names” definition here and used it
in array attribute prefix wording.

Ada 2005 and 2012 Editions sponsored in part by **Ada-Europe**