4.9 Static Expressions and Static Subtypes
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
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.]
Language Design Principles
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.
aspect_definitionfor a Link_Name aspect can contain a concatenation. We don't support static aggregates (even for string types) or non-string static nonscalar types; we're trying to keep it cheap and simple (from the implementer's viewpoint).
A static expression is [a scalar or string expression that is] one of the following:
numeric_literalof a numeric type;
numeric_literalof a numeric type is always a static expression, even if its expected type is not that of a static subtype. However, if its value is explicitly converted to, or qualified by, a nonstatic subtype, the resulting expression is nonstatic. Non-numeric types can have numeric literals if aspect Integer_Literal or Real_Literal is used; these are never static.
string_literalof a static string subtype;
namethat denotes the declaration of a static constant; 5.1/5
namethat denotes a named number, and that is interpreted as a value of a numeric type;
prefixstatically denotes a static function, and whose actual parameters, if any (whether given explicitly or by default), are all static expressions;
attribute_referencethat denotes a scalar value, and whose
prefixdenotes a static scalar subtype;
prefixstatically names a statically constrained array object or array subtype, and whose
attribute_designatoris First, Last, or Length, with an optional dimension; 8.1/5
prefixdenotes a non-generic entity that is not declared in a generic unit, and whose
attribute_designatoris Nonblocking; 9/5
subtype_markdenotes a static [(scalar or string)] subtype, and whose operand is a static expression; 10
subtype_markdenotes a static [(scalar or string)] subtype, and whose operand is a static expression;
- a membership test whose tested_
simple_expressionis a static expression, and whose
membership_choice_listconsists only of
membership_choices that are either static choice_
subtype_marks that denote a static [(scalar or string)] subtype;
- a short-circuit control form both of whose
relations are static expressions; 12.1/3
conditional_expressionall of whose
expressions, and dependent_
expressions are static expressions; 12.2/5
expressionis static and each of whose declarations, if any, is either the declaration of a static constant or is an
object_renaming_declarationwith an object_
namethat statically names the renamed object; 13
- a static expression enclosed in parentheses.
number_declaration, a numeric type definition, certain representation items, and a number of other contexts .
name statically denotes an entity if it denotes the entity and:
- It is a
direct_name, expanded name, or
character_literal, and it denotes a declaration other than a
renaming_declaration; or 16
- It is an
prefixstatically denotes some entity; or 16.1/5
- It is a
target_name(see 5.2.1) in an
namestatically denotes some entity; or 17
- It denotes a
namethat statically denotes the renamed entity.
Selected_components that are not expanded names and
indexed_components do not statically denote things.
name statically names an object if it:
- statically denotes the declaration of an object [(possibly through one or more renames)];
- is a
selected_componentwhose prefix statically names an object, there is no implicit dereference of the prefix, and the
selector_namedoes not denote a
component_declarationoccurring within a
variant_partso that no discriminant checks are needed to evaluate the
selected_component. Note that other kinds of discriminant-dependent components do not need any checks on access (only when they are changed).
- is an
indexed_componentwhose 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.
For an entity other than an object, a
name statically names an entity if the
name statically denotes the entity.
A static function is one of the following:
- a predefined operator whose parameter and result types are all scalar types none of which are descendants of formal scalar types;
- a predefined relational operator whose parameters are of a string type that is not a descendant of a formal array type;
- a predefined concatenation operator whose result type is a string type that is not a descendant of a formal array type;
- a shifting or rotating function associated with a modular type declared in package Interfaces (see B.2);
- an enumeration literal;
- a static expression function (see 6.8);
- a language-defined attribute that is a function, if the
prefixdenotes 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.
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, and which satisfies any constraint or predicate that applies to the nominal subtype .
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.
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.
subtype Int10 is Integer range 1..10; 26.dgeneric F : in out Int10; procedure G; 26.eprocedure G is begin case F is when 1..10 => null; -- Illegal! end case; end G; 26.fX : Integer range 1..20; procedure I is new G(F => X); -- OK.
case_statementis 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.
The different kinds of static constraint are defined as follows:
- A null constraint is always static;
- A scalar constraint is static if it has no
range_constraint, or one with a static range; 30
- An index constraint is static if each
discrete_rangeis static, and each index subtype of the corresponding array type is static; 31
- A discriminant constraint is static if each
expressionof the constraint is static, and the subtype of each discriminant is static.
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.
An expression is statically unevaluated if it is part of:
- the right operand of a static short-circuit control form whose value is determined by its left operand; or
- a dependent_
conditionis static and equals False; or 32.4/3
conditioncorresponding to at least one preceding dependent_
if_expressionis static and equals True; or
expressionis evaluated in a static
if_expressionif there is more than one
conditionthat evaluates to True. The part about
(if N = 0 then Min elsif 10_000/N > Min then 10_000/N else Min)
if_expression" here so there is no confusion for nested
if_expressions; this rule only applies to the
conditions and dependent_
expressions of a single
if_expression. Similar reasoning applies to the "of a
case_expression" of the last bullet.
- a dependent_
expressionis static and whose value is not covered by the corresponding
discrete_choice_list; or 32.6/4
- a choice_
rangethat occurs as a
membership_choice_list) of a static membership test that is preceded in the enclosing
membership_choice_listby another item whose individual membership test (see 4.5.2) statically yields True.
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:
- 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.
- 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.
- 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).
numeric_literalfor a decimal type cannot have “extra” significant digits.
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.
N: constant := 0.0;
X: constant Boolean := (N = 0.0) or else (1.0/N > 0.5); -- Static.
X : constant := (if True then 37 else (1 / 0));
function If_Then_Else (Flag : Boolean; X, Y : Integer) return Integer is (if Flag then X else Y) with Static; -- see 6.8 X : constant := If_Then_Else (True, 37, 1 / 0);
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).
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.
X : Float := Float'(1.0E+400) + 1.0 - Float'(1.0E+400);
type_conversionfrom 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.
Examples of static expressions:
1 + 1 -- 2 abs(-10)*3 -- 30 43Kilo : constant := 1000; Mega : constant := Kilo*Kilo; -- 1_000_000 Long : constant := Float'Digits*2; 44Half_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)
Extensions to Ada 83
- 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.
type_conversionis static if the
subtype_markdenotes a static scalar subtype and the operand is a static expression. 44.f
- All numeric literals are now static, even if the expected type is a formal scalar type. This is useful in
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.
type Short_Int is range -32_768 .. 32_767;
I : Short_Int := -32_768;
Incompatibilities With Ada 83
Wording Changes from Ada 83
Bad: constant := 1/0; -- Illegal!
Inconsistencies With Ada 95
Wording Changes from Ada 95
Wording Changes from Ada 2005
case_expressions. These are new and defined elsewhere.
Incompatibilities With Ada 2012
Extensions to Ada 2012
Wording Changes from Ada 2012
numeric_literalor named number can be non-static if they interpreted using an Integer_Literal or Real_Literal aspect (see 4.2.1).
declare_expressions. Also moved “statically names” definition here and used it in array attribute prefix wording.
4.9.1 Statically Matching Constraints and Subtypes
A constraint statically matches another constraint if:
- both are null constraints;
- both are static and have equal corresponding bounds or discriminant values;
- both are nonstatic and result from the same elaboration of a
subtype_indicationor the same evaluation of a
discrete_subtype_definition; or 1.4/2
- both are nonstatic and come from the same
The Global or Global'Class aspects (see 6.1.2) of two entities statically match if both consist of a single
global_aspect_definition where each is the reserved word null, or each is of the form “
global_designator” with each
global_mode being the same sequence of reserved words and each
global_designator being the same reserved word, or each being a
global_name that statically names the same entity.
A subtype statically matches another subtype of the same type if they have statically matching constraints, all predicate specifications that apply to them come from the same declarations, Nonblocking aspects have the same value, global aspects statically match, Object_Size (see 13.3) has been specified to have a nonconfirming value for either both or neither, and the nonconfirming values, if any, are the same, and, for access subtypes, either both or neither exclude null. Two anonymous access-to-object subtypes statically match if their designated subtypes statically match, and either both or neither exclude null, and either both or neither are access-to-constant. Two anonymous access-to-subprogram subtypes statically match if their designated profiles are subtype conformant, and either both or neither exclude null.
Two ranges of the same type statically match if both result from the same evaluation of a
range, or if both are static and have equal corresponding bounds.
A constraint is statically compatible with a scalar subtype if it statically matches the constraint of the subtype, or if both are static and the constraint is compatible with the subtype. A constraint is statically compatible with an access or composite subtype if it statically matches the constraint of the subtype, or if the subtype is unconstrained.
discriminant_part. See 3.7. Static compatibility is also used in matching generic formal derived types.
Two statically matching subtypes are statically compatible with each other. In addition, a subtype S1 is statically compatible with a subtype S2 if:
- the constraint of S1 is statically compatible with S2, and
- if S2 excludes null, so does S1, and
- all predicate specifications that apply to S2 apply also to S1, or
- both subtypes are static, every value that satisfies the predicates of S1 also satisfies the predicates of S2, and it is not the case that both types each have at least one applicable predicate specification, predicate checks are enabled (see 11.4.2) for S2, and predicate checks are not enabled for S1.