3.6 Array Types
1
{array}
{array type}
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
2
array_type_definition ::=
unconstrained_array_definition |
constrained_array_definition
3
unconstrained_array_definition ::=
array(
index_subtype_definition {,
index_subtype_definition})
of component_definition
4
index_subtype_definition ::= subtype_mark range <>
5
constrained_array_definition ::=
array (
discrete_subtype_definition {,
discrete_subtype_definition})
of component_definition
6
discrete_subtype_definition ::= discrete_subtype_indication |
range
7/2
Name Resolution Rules
8
{expected type (discrete_subtype_definition
range) [partial]} 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
9
{index subtype}
Each
index_subtype_definition
or
discrete_subtype_definition in an
array_type_definition
defines an
index subtype;
{index
type} its type (the
index type)
shall be discrete.
9.a
Discussion: {
index (of an array)}
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.
10
{component subtype}
The subtype defined by the
subtype_indication
of a
component_definition (the
component
subtype) shall be a definite subtype.
10.a
Ramification: This applies to all uses
of component_definition, including in record_type_definitions
and protected_definitions.
11/2
This paragraph was
deleted.{
AI-00363-01}
Within the definition of a nonlimited composite
type (or a limited composite type that later in its immediate scope becomes
nonlimited — see 7.3.1 and 7.5),
if a component_definition contains the reserved
word aliased and the type of the component is discriminated, then
the nominal subtype of the component shall be constrained.
11.a/2
This paragraph
was deleted.Reason: If we allowed
the subtype to be unconstrained, then the discriminants might change
because of an assignment to the containing (nonlimited) object, thus
causing a potential violation of an access subtype constraint of an access
value designating the aliased component.
11.b/2
This paragraph
was deleted.Note that the rule elsewhere
defining all aliased discriminated objects to be constrained does not
help — that rule prevents assignments to the component itself from
doing any harm, but not assignments to the containing object.
11.c/2
This
paragraph was deleted.We allow this
for components within limited types since assignment to the enclosing
object is not a problem. Furthermore, it is important to be able to use
a default expression for a discriminant in arrays of limited components,
since that is the only way to give the components different values for
their discriminants. For example:
11.d/2
This paragraph
was deleted.protected type Counter_Type(Initial_Value : Integer := 1) is
procedure Get_Next(Next_Value : out Integer);
-- Returns the next value on each call, bumping Count
-- before returning.
private
Count : Integer := Initial_Value;
end Counter_Type;
protected body Counter_Type is ...
11.e/2
This paragraph
was deleted.function Next_Id(Counter : access Counter_Type) return Integer is
Result : Integer;
begin
Counter.Get_Next(Result);
return Result;
end Next_Id;
11.f/2
This paragraph
was deleted.C : aliased Counter_Type;
task type T(Who_Am_I : Integer := Next_Id(C'Access));
task body T is ...
11.g/2
This paragraph
was deleted.Task_Array : array(1..100) of aliased T;
-- Array of task elements, each with its own unique ID.
-- We specify "aliased" so we can use Task_Array(I)'Access.
-- This is safe because Task_Array is of a limited type,
-- so there is no way an assignment to it could change
-- the discriminants of one of its components.
11.h/2
This paragraph
was deleted.Ramification: Note
that this rule applies to array components and record components, but
not to protected type components (since they are always limited).
Static Semantics
12
{dimensionality (of
an array)} {one-dimensional
array} {multi-dimensional
array} 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.
13
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;
{index
range} this range of values is called
the
index range.
{bounds (of an
array)} The
bounds of an array
are the bounds of its index ranges.
{length
(of a dimension of an array)} 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).
{length
(of a one-dimensional array)} The
length
of a one-dimensional array is the length of its only dimension.
14
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)].
15
{constrained (subtype)}
{unconstrained (subtype)}
An
unconstrained_array_definition
defines an array type with an unconstrained first subtype. Each
index_subtype_definition
defines the corresponding index subtype to be the subtype denoted by
the
subtype_mark. [
{box
(compound delimiter) [partial]} The compound
delimiter <> (called a
box) of an
index_subtype_definition
stands for an undefined range (different objects of the type need not
have the same bounds).]
16
{constrained (subtype)}
{unconstrained (subtype)}
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.
{constraint
(of a first array subtype) [partial]} The
constraint of the first subtype consists of the bounds of the
index ranges.
16.a
Discussion: Although there is no namable
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.
17
The discrete subtype
defined by a discrete_subtype_definition is
either that defined by the subtype_indication,
or a subtype determined by the range as follows:
18
- 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;
{implicit subtype conversion (bounds
of a range) [partial]}
18.a
Reason: This ensures that indexing over
the discrete subtype can be performed with regular Integers, rather than
only universal_integers.
18.b
Discussion: We considered doing this
by simply creating a “preference” for Integer when resolving
the
range. {
Beaujolais effect [partial]}
However, this can introduce
Beaujolais effects
when the
simple_expressions involve calls
on functions visible due to
use clauses.
19
- Otherwise, the discrete_subtype_definition
defines a subtype of the type of the range,
with the bounds given by the range.
20
{nominal subtype
(of a component) [partial]} The
component_definition
of an
array_type_definition defines the nominal
subtype of the components. If the reserved word
aliased appears
in the
component_definition, then each component
of the array is aliased (see
3.10).
20.a/2
This paragraph
was deleted.Ramification: {
AI-00363-01}
In this case, the nominal subtype cannot be an
unconstrained discriminated subtype. See 3.8.
Dynamic Semantics
21
{elaboration (array_type_definition)
[partial]} The elaboration of an
array_type_definition
creates the array type and its first subtype, and consists of the elaboration
of any
discrete_subtype_definitions and the
component_definition.
22/2
{
8652/0002}
{
AI-00171-01}
{
AI-00230-01}
{elaboration (discrete_subtype_definition)
[partial]} The elaboration of a
discrete_subtype_definition
that does not contain any per-object expressions
creates the discrete subtype, and consists of the elaboration of the
subtype_indication or the evaluation of the
range.
The elaboration
of a discrete_subtype_definition that contains
one or more per-object expressions is defined in 3.8.
{elaboration (component_definition)
[partial]} The elaboration of a
component_definition
in an
array_type_definition consists of the
elaboration of the
subtype_indication or access_definition. The elaboration
of any
discrete_subtype_definitions and the
elaboration of the
component_definition are
performed in an arbitrary order.
23
43 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.
24
44 Each elaboration of an array_type_definition
creates a distinct array type. A consequence of this is that each object
whose object_declaration contains an array_type_definition
is of its own unique type.
Examples
25
Examples of type
declarations with unconstrained array definitions:
26
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
27
Examples of type declarations
with constrained array definitions:
28
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;
29
Examples of object
declarations with array type definitions:
30/2
{
AI-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
30.a
{
extensions to Ada 83}
The
syntax rule for
component_definition is modified
to allow the reserved word
aliased.
30.b
The syntax rules for unconstrained_array_definition
and constrained_array_definition are modified
to use component_definition (instead of component_subtype_indication).
The effect of this change is to allow the reserved word aliased
before the component subtype_indication.
30.c
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
30.d
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.
30.e
The syntax for index_constraint
and discrete_range have been moved to their
own subclause, since they are no longer used here.
30.f
The syntax rule for component_definition
(formerly component_subtype_definition) is
moved here from RM83-3.7.
Extensions to Ada 95
30.g/2
30.h/2
{
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
30.i/2
{
8652/0002}
{
AI95-00171-01}
Corrigendum: Added wording to allow the
elaboration of per-object constraints for constrained arrays.