3.3 Objects and Named Numbers
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[Objects are created at run time and contain a value of a given type. An object can be created and initialized as part of elaborating a declaration, evaluating an allocator
, aggregate
, or function_call
, or passing a parameter by copy. Prior to reclaiming the storage for an object, it is finalized if necessary (see 7.6.1).]
Static Semantics
2All of the following are objects:
object_declaration
or by an allocator
. A formal parameter is (a view of) an object. A subcomponent of an object is an object.- the entity declared by an
object_declaration
; 5 - a formal parameter of a subprogram, entry, or generic subprogram;
- a generic formal object;
- a loop parameter;
- the index parameter of an
iterated_component_association
; 7.2/5 - the chunk parameter of a
chunk_specification
; 8 - a choice parameter of an
exception_handler
; 9 - an entry index of an
entry_body
; 10 - the result of dereferencing an access-to-object value (see 4.1);
- the return object of a function;
- the result of evaluating an
aggregate
; 12.1/5 - a value conversion or
qualified_expression
whose operand denotes an object; 13 - a component, slice, or view conversion of another object.
An object is either a constant object or a variable object. Similarly, a view of an object is either a constant or a variable. All views of a constant elementary object are constant. All views of a constant composite object are constant, except for parts that are of controlled or immutably limited types; variable views of those parts and their subcomponents may exist. In this sense, objects of controlled and immutably limited types are inherently mutable. A constant view of an object cannot be used to modify its value. The terms constant and variable by themselves refer to constant and variable views of objects.
A constant object is known to have no variable views if it does not have a part that is immutably limited, or of a controlled type, private type, or private extension.
The value of an object is read when the value of any part of the object is evaluated, or when the value of an enclosing object is evaluated. The value of a variable is updated when an assignment is performed to any part of the variable, or when an assignment is performed to an enclosing object.
Whether a view of an object is constant or variable is determined by the definition of the view. The following (and no others) represent variables :
- an object declared by an
object_declaration
without the reserved word constant;
object_declaration
, not some random word constant. Thus, X : access constant T;
- a formal parameter of mode in out or out ;
- a generic formal object of mode in out;
- a non-discriminant component of a variable ;
- a
slice
of a variable; 19.2/5 - a loop parameter that is specified to be a variable for a generalized loop (see 5.5.2);
- a view conversion of a variable ;
- a dereference of an access-to-variable value;
- the return object declared by an
extended_return_statement
without the reserved word constant; 22/5 - the current instance of a type other than a protected type[, if the current instance is an object and not a value (see 8.6)] ;
- This paragraph was deleted.
- the current instance of a protected unit except within the body of a protected function of that protected unit, or within a function declared immediately within the body of the protected unit;
- an
attribute_reference
where the attribute is defined to denote a variable (for example, the Storage_Pool attribute – see 13.11) .
- the result of evaluating a
function_call
, anaggregate
, a value conversion, aqualified_expression
, aconditional_expression
, araise_expression
, or a parenthesized expression; 23.c/5 - a
target_name
of anassignment_statement
(see 5.2.1); 23.d/5 - the index parameter of an
iterated_component_association
; 23.e/5 - a choice parameter or entry index;
- a chunk parameter of a
chunk_specification
.
At the place where a view of an object is defined, a nominal subtype is associated with the view. The nominal type of a view is the type of the nominal subtype of the view. The object's actual subtype (that is, its subtype) can be more restrictive than the nominal subtype of the view; it always is more restrictive if the nominal subtype is an indefinite subtype. A subtype is an indefinite subtype if it is an unconstrained array subtype, or if it has unknown discriminants or unconstrained discriminants without defaults (see 3.7); otherwise, the subtype is a definite subtype [(all elementary subtypes are definite subtypes)]. [A class-wide subtype is defined to have unknown discriminants, and is therefore an indefinite subtype. An indefinite subtype does not by itself provide enough information to create an object; an additional constraint
or explicit initialization expression
is necessary (see 3.3.1). A component cannot have an indefinite nominal subtype.]
A view of a composite object is known to be constrained if:
- its nominal subtype is constrained and not an untagged partial view, and it is neither a value conversion nor a
qualified_expression
; or 25.3/3 - its nominal subtype is indefinite; or
- its type is immutably limited (see 7.5); or
- it is part of a stand-alone constant (including a generic formal object of mode in); or
- it is part of a formal parameter of mode in; or
- it is part of the object denoted by a
function_call
oraggregate
; or 25.8/5 - it is a value conversion or
qualified_expression
where the operand denotes a view of a composite object that is known to be constrained; or 25.9/5 - it is part of a constant return object of an
extended_return_statement
; or 25.10/5 - it is a dereference of a pool-specific access type, and there is no ancestor of its type that has a constrained partial view.
For the purposes of determining within a generic body whether an object is known to be constrained:
- if a subtype is a descendant of an untagged generic formal private or derived type, and the subtype is not an unconstrained array subtype, it is not considered indefinite and is considered to have a constrained partial view;
- if a subtype is a descendant of a formal access type, it is not considered pool-specific.
A named number provides a name for a numeric value known at compile time. It is declared by a number_declaration
.
Extensions to Ada 83
function_call
is also syntactically a name
(see 4.1), the result of a function_call
can be renamed, thereby allowing repeated use of the result without calling the function again. Wording Changes from Ada 83
object_declaration
.object_declaration
and number_declaration
down into their respective subclauses, to keep the syntax close to the description of the associated semantics.object_declaration
s, and it seems better to have the list of the kinds of constants juxtaposed with the kinds of objects.Wording Changes from Ada 95
Extensions to Ada 2005
Wording Changes from Ada 2005
qualified_expression
is defined to be a constant view and is defined to be an object if the operand of the qualified_expression
is an object. These definitions, combined with some grammar changes, allow qualified_expression
s to be used in more places. See 4.1 for details. Incompatibilities With Ada 2012
qualified_expression
s is always used to determine whether the property exists. As the rules are ored together, a value conversion or qualified_expression
with a constrained nominal subtype would have always met the requirements in Ada 2012, regardless of the operand. This change will mean that some conversions or qualifications (mostly of variables) will no longer be considered “known to be constrained” and therefore 'Access and renaming of such prefix
es will now be illegal. This is necessary to meet the design goal that subsequent execution cannot cause a renaming or 'Access to cause erroneous execution. Extensions to Ada 2012
qualified_expression
of an object that is known to be constrained is now also known to be constrained. This allows qualification to be used to disambiguate a function call used as a prefix in a renaming_declaration
without making the renaming_declaration
illegal.Wording Changes from Ada 2012
3.3.1 Object Declarations
1/3An object_declaration
declares a stand-alone object with a given nominal subtype and, optionally, an explicit initial value given by an initialization expression. For an array, access, task, or protected object, the object_declaration
may include the definition of the (anonymous) type of the object.
Syntax
2/3object_declaration
::=
defining_identifier_list
: [aliased] [constant] subtype_indication
[:= expression
]
[aspect_specification
];
| defining_identifier_list
: [aliased] [constant] access_definition
[:= expression
]
[aspect_specification
];
| defining_identifier_list
: [aliased] [constant] array_type_definition
[:= expression
]
[aspect_specification
];
| single_task_declaration
| single_protected_declaration
3defining_identifier_list
::=
defining_identifier
{, defining_identifier
}
Name Resolution Rules
4For an object_declaration
with an expression
following the compound delimiter :=, the type expected for the expression
is that of the object. This expression
is called the initialization expression.
Legality Rules
5/2An object_declaration
without the reserved word constant declares a variable object. If it has a subtype_indication
or an array_type_definition
that defines an indefinite subtype, then there shall be an initialization expression.
Static Semantics
6/3An object_declaration
with the reserved word constant declares a constant object. If it has an initialization expression, then it is called a full constant declaration. Otherwise, it is called a deferred constant declaration. The rules for deferred constant declarations are given in subclause 7.4. The rules for full constant declarations are given in this subclause.
Any declaration that includes a defining_identifier_list
with more than one defining_identifier
is equivalent to a series of declarations each containing one defining_identifier
from the list, with the rest of the text of the declaration copied for each declaration in the series, in the same order as the list. The remainder of this Reference Manual relies on this equivalence; explanations are given for declarations with a single defining_identifier
.
The subtype_indication
, access_definition
, or full type definition of an object_declaration
defines the nominal subtype of the object. The object_declaration
declares an object of the type of the nominal subtype.
A component of an object is said to require late initialization if:
- it has an access discriminant value constrained by a per-object expression; or
- it has an initialization expression that includes a name denoting an access discriminant; or
- it has an initialization expression that includes a reference to the current instance of the type either by name or implicitly as the target object of a call.
Dynamic Semantics
9/2If a composite object declared by an object_declaration
has an unconstrained nominal subtype, then if this subtype is indefinite or the object is constant the actual subtype of this object is constrained. The constraint is determined by the bounds or discriminants (if any) of its initial value; the object is said to be constrained by its initial value. When not constrained by its initial value, the actual and nominal subtypes of the object are the same. If its actual subtype is constrained, the object is called a constrained object.
For an object_declaration
without an initialization expression, any initial values for the object or its subcomponents are determined by the implicit initial values defined for its nominal subtype, as follows:
- The implicit initial value for an access subtype is the null value of the access type.
- The implicit initial value for a scalar subtype that has the Default_Value aspect specified is the value of that aspect converted to the nominal subtype (which can raise Constraint_Error — see 4.6, “Type Conversions”);
aspect_specification
is not relevant — if the full type for a private type has the Default_Value aspect specified, partial views of the type also have this implicit initial value. - The implicit initial (and only) value for each discriminant of a constrained discriminated subtype is defined by the subtype.
- For a (definite) composite subtype, the implicit initial value of each component with a
default_expression
is obtained by evaluation of this expression and conversion to the component's nominal subtype (which can raise Constraint_Error), unless the component is a discriminant of a constrained subtype (the previous case), or is in an excludedvariant
(see 3.8.1). For each component that does not have adefault_expression
, if the composite subtype has the Default_Component_Value aspect specified, the implicit initial value is the value of that aspect converted to the component's nominal subtype; otherwise, any implicit initial values are those determined by the component's nominal subtype. 14 - For a protected or task subtype, there is an implicit component (an entry queue) corresponding to each entry, with its implicit initial value being an empty queue.
The elaboration of an object_declaration
proceeds in the following sequence of steps:
a)
- The
subtype_indication
,access_definition
,array_type_definition
,single_task_declaration
, orsingle_protected_declaration
is first elaborated. This creates the nominal subtype (and the anonymous type in the last four cases). 17/5
b)- If the
object_declaration
includes an initialization expression, the (explicit) initial value is obtained by evaluating the expression and converting it to the nominal subtype (which can raise Constraint_Error — see 4.6). 18/2
c)- {8652/0002} The object is created, and, if there is not an initialization expression, the object is initialized by default. When an object is initialized by default, any per-object constraints (see 3.8) are elaborated and any implicit initial values for the object or for its subcomponents are obtained as determined by the nominal subtype. Any initial values (whether explicit or implicit) are assigned to the object or to the corresponding subcomponents. As described in 5.2 and 7.6, Initialize and Adjust procedures can be called.
type_declaration
, and the values of the per-object expressions evaluated at the point of the creation of the object.access_definition
is elaborated at this time (see 3.7). - This paragraph was deleted.
For the third step above, evaluations and assignments are performed in an arbitrary order subject to the following restrictions:
- Assignment to any part of the object is preceded by the evaluation of the value that is to be assigned.
- The evaluation of a
default_expression
that includes the name of a discriminant is preceded by the assignment to that discriminant.
subtype_indication
s. - The evaluation of the
default_expression
for any component that depends on a discriminant is preceded by the assignment to that discriminant.
- The assignments to any components, including implicit components, not requiring late initialization precede the initial value evaluations for any components requiring late initialization; if two components both require late initialization, then assignments to parts of the component occurring earlier in the order of the component declarations precede the initial value evaluations of the component occurring later.
type T (D : Natural) is
limited record
C1 : T1 (T'Access);
C2 : Natural := F (D);
C3 : String (1 .. D) := (others => ' ');
end record;
[There is no implicit initial value defined for a scalar subtype unless the Default_Value aspect has been specified for the type.] In the absence of an explicit initialization or the specification of the Default_Value aspect, a newly created scalar object can have a value that does not belong to its subtype (see 13.9.1 and H.1).
object_declaration
produce two different initial values. However, any particular uninitialized object is default-initialized to a single value (or invalid representation). Thus, multiple reads of such an uninitialized object will produce the same value each time (if the implementation chooses not to detect the error). object_declaration
. Similar definitions apply to “stand-alone constant” and “stand-alone variable”. A subcomponent of an object is not a stand-alone object, nor is an object that is created by an allocator
. An object declared by a loop_parameter_specification
, iterator_specification
, iterated_component_association
, chunk_specification
, parameter_specification
, entry_index_specification
, choice_parameter_specification
, extended_return_statement
, or a formal_object_declaration
of mode in out is not considered a stand-alone object.Examples
25Example of a multiple object declaration:
-- the multiple object declaration
27/2John, Paul : not null Person_Name := new Person(Sex => M); -- see 3.10.1
28-- is equivalent to the two single object declarations in the order given
29/2John : not null Person_Name := new Person(Sex => M);
Paul : not null Person_Name := new Person(Sex => M);
30Examples of variable declarations:
Count, Sum : Integer;
Size : Integer range 0 .. 10_000 := 0;
Sorted : Boolean := False;
Color_Table : array(1 .. Max) of Color;
Option : Bit_Vector(1 .. 10) := (others => True); -- see 3.6
Hello : aliased String := "Hi, world.";
θ, φ : Float range -π .. +π;
32Examples of constant declarations:
Limit : constant Integer := 10_000;
Low_Limit : constant Integer := Limit/10;
Tolerance : constant Real := Dispersion(1.15);
A_String : constant String := "A";
Hello_Msg : constant access String := Hello'Access; -- see 3.10.2
Extensions to Ada 83
object_declaration
is modified to allow the aliased reserved word.object_declaration
can be constrained by its initial value; that is, a variable of a nominally unconstrained array subtype, or discriminated type without defaults, can be declared so long as it has an explicit initial value. In Ada 83, this was permitted for constants, and for variables created by allocators, but not for variables declared by object_declaration
s. This is particularly important for tagged class-wide types, since there is no way to constrain them explicitly, and so an initial value is the only way to provide a constraint. It is also important for generic formal private types with unknown discriminants.unconstrained_array_definition
in an object_declaration
. This allows an object of an anonymous array type to have its bounds determined by its initial value. This is for uniformity: If one can write “X: constant array(Integer range 1..10) of Integer := ...;” then it makes sense to also allow “X: constant array(Integer range <>) of Integer := ...;”. (Note that if anonymous array types are ever sensible, a common situation is for a table implemented as an array. Tables are often constant, and for constants, there's usually no point in forcing the user to count the number of elements in the value.) Wording Changes from Ada 83
object_declaration
s into this subclause.object_declaration
as constants declared without an initialization expression. Inconsistencies With Ada 95
Extensions to Ada 95
expression
has to be built-in-place (see 7.5).Wording Changes from Ada 95
Extensions to Ada 2005
Wording Changes from Ada 2005
Inconsistencies With Ada 2012
3.3.2 Number Declarations
1A number_declaration
declares a named number.
Syntax
2number_declaration
::=
defining_identifier_list
: constant := static_expression
;
Name Resolution Rules
3The static_expression
given for a number_declaration
is expected to be of any numeric type.
A name
that denotes a number_declaration
is interpreted as a value of a universal type, unless the expected type for the name
is a non-numeric type with an Integer_Literal or Real_Literal aspect, in which case it is interpreted to be of its expected type.
Legality Rules
4/3The static_expression
given for a number declaration shall be a static expression, as defined by subclause 4.9.
Static Semantics
5The named number denotes a value of type universal_integer if the type of the static_expression
is an integer type. The named number denotes a value of type universal_real if the type of the static_expression
is a real type.
The value denoted by the named number is the value of the static_expression
, converted to the corresponding universal type.
Dynamic Semantics
7The elaboration of a number_declaration
has no effect.
expression
was evaluated at compile time. Examples
8Examples of number declarations:
Two_Pi : constant := 2.0*Ada.Numerics.Pi; -- a real number (see A.5)
10/2Max : constant := 500; -- an integer number
Max_Line_Size : constant := Max/6; -- the integer 83
Power_16 : constant := 2**16; -- the integer 65_536
One, Un, Eins : constant := 1; -- three different names for 1
Extensions to Ada 83
Wording Changes from Ada 83
object_declaration
. However, since there is no expression to be evaluated and no object to be created, it seems simpler to say that the elaboration has no effect.