3.6 Array Types
An
array
object is a composite object consisting of components which all have
the same subtype. The name for a component of an array uses one or more
index values belonging to specified discrete types. The value of an array
object is a composite value consisting of the values of the components.
Syntax
Name Resolution Rules
For a
discrete_subtype_definition
that is a
range,
the
range
shall resolve to be of some specific discrete type[; which discrete type
shall be determined without using any context other than the bounds of
the
range
itself (plus the preference for
root_integer — see
8.6).]
Legality Rules
Discussion: An
index
is a discrete quantity used to select along a given dimension of an array.
A component is selected by specifying corresponding values for each of
the indices.
Static Semantics
An
array is characterized by the number of indices (the
dimensionality
of the array), the type and position of each index, the lower and upper
bounds for each index, and the subtype of the components. The order of
the indices is significant.
A one-dimensional array has a distinct component
for each possible index value. A multidimensional array has a distinct
component for each possible sequence of index values that can be formed
by selecting one value for each index position (in the given order).
The possible values for a given index are all the values between the
lower and upper bounds, inclusive;
this range of
values is called the
index range.
The
bounds
of an array are the bounds of its index ranges.
The
length of a dimension of an array is the number of values of the
index range of the dimension (zero for a null range).
The
length of a one-dimensional array is the length of its only dimension.
An
array_type_definition
defines an array type and its first subtype. For each object of this
array type, the number of indices, the type and position of each index,
and the subtype of the components are as in the type definition[; the
values of the lower and upper bounds for each index belong to the corresponding
index subtype of its type, except for null arrays (see
3.6.1)].
A
constrained_array_definition
defines an array type with a constrained first subtype. Each
discrete_subtype_definition
defines the corresponding index subtype, as well as the corresponding
index range for the constrained first subtype.
The
constraint of the first subtype consists of the bounds of the
index ranges.
Discussion: {
AI05-0005-1}
Although there is no nameable unconstrained array subtype in this case,
the predefined slicing and concatenation operations can operate on and
yield values that do not necessarily belong to the first array subtype.
This is also true for Ada 83.
If the type of the
range
resolves to
root_integer, then the
discrete_subtype_definition
defines a subtype of the predefined type Integer with bounds given by
a conversion to Integer of the bounds of the
range;
Reason: This ensures that indexing over
the discrete subtype can be performed with regular Integers, rather than
only universal_integers.
Discussion: We considered doing this
by simply creating a “preference” for Integer when resolving
the
range.
However, this can introduce
Beaujolais effects
when the
simple_expressions
involve calls on functions visible due to
use clauses.
Dynamic Semantics
Static Semantics
Default_Component_Value
This aspect shall be specified by a static expression, and that expression
shall be explicit, even if the aspect has a boolean type. Default_Component_Value
shall be specified only on a
full_type_declaration.
Reason: The part about requiring an explicit
expression is to disallow omitting the value for this aspect, which would
otherwise be allowed by the rules of
13.1.1.
This is a representation attribute in order
to disallow specifying it on a derived type that has inherited primitive
subprograms; that is necessary as the sizes of
out parameters
could be different whether or not a Default_Value is specified (see
6.4.1).
Aspect Description for Default_Component_Value:
Default value for the components of an array-of-scalar subtype.
{
AI05-0228-1}
If a derived type with no primitive subprograms inherits a boolean Default_Component_Value
aspect, the aspect may be specified to have any value for the derived
type.
Reason: This overrides the
13.1.1
rule that says that a boolean aspect with a value True cannot be changed.
Name Resolution Rules
49 All components of an array have the
same subtype. In particular, for an array of components that are one-dimensional
arrays, this means that all components have the same bounds and hence
the same length.
Examples
Examples of type
declarations with unconstrained array definitions:
type Vector
is array(Integer
range <>)
of Real;
type Matrix
is array(Integer
range <>, Integer
range <>)
of Real;
type Bit_Vector
is array(Integer
range <>)
of Boolean;
type Roman
is array(Positive
range <>)
of Roman_Digit; --
see 3.5.2
Examples of type
declarations with constrained array definitions:
type Table is array(1 .. 10) of Integer;
type Schedule is array(Day) of Boolean;
type Line is array(1 .. Max_Line_Size) of Character;
Examples of object
declarations with array type definitions:
{
AI95-00433-01}
Grid :
array(1 .. 80, 1 .. 100)
of Boolean;
Mix :
array(Color
range Red .. Green)
of Boolean;
Msg_Table :
constant array(Error_Code)
of access constant String :=
(Too_Big =>
new String'("Result too big"), Too_Small => ...);
Page :
array(Positive
range <>)
of Line := --
an array of arrays
(1 | 50 => Line'(1 | Line'Last => '+',
others => '-'), --
see 4.3.3
2 .. 49 => Line'(1 | Line'Last => '|',
others => ' '));
--
Page is constrained by its initial value to (1..50)
Extensions to Ada 83
A
range
in a
discrete_subtype_definition
may use arbitrary universal expressions for each bound (e.g. –1
.. 3+5), rather than strictly "implicitly convertible" operands.
The subtype defined will still be a subtype of Integer.
Wording Changes from Ada 83
We introduce a new syntactic category,
discrete_subtype_definition,
as distinct from
discrete_range.
These two constructs have the same syntax, but their semantics are quite
different (one defines a subtype, with a preference for Integer subtypes,
while the other just selects a subrange of an existing subtype). We use
this new syntactic category in
for loops and entry families.
The syntax rule for
component_definition
(formerly
component_subtype_definition) is
moved here from RM83-3.7.
Extensions to Ada 95
{
AI95-00363-01}
The prohibition against unconstrained discriminated aliased components
has been lifted. It has been replaced by a prohibition against the actual
troublemakers: general access discriminant constraints (see
3.7.1).
Wording Changes from Ada 95
{
8652/0002}
{
AI95-00171-01}
Corrigendum: Added wording to allow the elaboration of per-object
constraints for constrained arrays.
Extensions to Ada 2005
{
AI05-0228-1}
The new aspect Default_Component_Value allows defining
implicit initial values (see
3.3.1) for arrays
of scalar types.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe