Skip to main content

3.6 Array Types

danger

This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue

1

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_markrange<>
5

constrained_array_definition ::=
array (discrete_subtype_definition {, discrete_subtype_definition}) of component_definition

6

discrete_subtype_definition ::= discrete_subtype_indication | range

7/2

component_definition ::=
[aliased] subtype_indication
| [aliased] access_definition

Name Resolution Rules

8

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

Each index_subtype_definition or discrete_subtype_definition in an array_type_definition defines an index subtype; its type (the index type) shall be discrete.

9.a
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.

10

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.

Static Semantics

12

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; 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.

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/5

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. [ The compound delimiter <> (called a box) of an index_subtype_definition stands for an undefined range (different objects of the type can have different bounds).]

16

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.

16.a/3
discussion

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.

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;
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. However, this can introduce Beaujolais effects when the simple_expressions involve calls on functions visible due to use clauses.

19 20

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).

Dynamic Semantics

21

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} 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. 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.

Static Semantics

22.1/3

For an array type with a scalar component type, the following language-defined representation aspect may be specified with an aspect_specification (see 13.1.1):

22.2/3

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.
22.a/3
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.

22.b/3

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).

22.c/3

Aspect Description for Default_Component_Value: Default value for the components of an array-of-scalar subtype.

22.3/5

If a derived type inherits a boolean Default_Component_Value aspect, the aspect may be specified to have any value for the derived type.

22.d/3
reason

This overrides the 13.1.1 rule that says that a boolean aspect with a value True cannot be changed.

Name Resolution Rules

22.4/3

The expected type for the expression specified for the Default_Component_Value aspect is the component type of the array type defined by the full_type_declaration on which it appears.

23

NOTE 1 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

NOTE 2 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

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

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

Array components can have an anonymous access type.

30.h/2

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} Corrigendum: Added wording to allow the elaboration of per-object constraints for constrained arrays.

Extensions to Ada 2005

30.j/3

The new aspect Default_Component_Value allows defining implicit initial values (see 3.3.1) for arrays of scalar types.

3.6.1 Index Constraints and Discrete Ranges

1

An index_constraint determines the range of possible values for every index of an array subtype, and thereby the corresponding array bounds.

Syntax

2

index_constraint ::= (discrete_range {, discrete_range})

3

discrete_range ::= discrete_subtype_indication | range

Name Resolution Rules

4

The type of a discrete_range is the type of the subtype defined by the subtype_indication, or the type of the range. For an index_constraint, each discrete_range shall resolve to be of the type of the corresponding index.

4.a
discussion

In Ada 95, index_constraints only appear in a subtype_indication; they no longer appear in constrained_array_definitions.

Legality Rules

5

An index_constraint shall appear only in a subtype_indication whose subtype_mark denotes either an unconstrained array subtype, or an unconstrained access subtype whose designated subtype is an unconstrained array subtype; in either case, the index_constraint shall provide a discrete_range for each index of the array type.

Static Semantics

6

A discrete_range defines a range whose bounds are given by the range, or by the range of the subtype defined by the subtype_indication.

Dynamic Semantics

7

An index_constraint is compatible with an unconstrained array subtype if and only if the index range defined by each discrete_range is compatible (see 3.5) with the corresponding index subtype. If any of the discrete_ranges defines a null range, any array thus constrained is a null array, having no components. An array value satisfies an index_constraint if at each index position the array value and the index_constraint have the same index bounds.

7.a
ramification

There is no need to define compatibility with a constrained array subtype, because one is not allowed to constrain it again.

8

The elaboration of an index_constraint consists of the evaluation of the discrete_range(s), in an arbitrary order. The evaluation of a discrete_range consists of the elaboration of the subtype_indication or the evaluation of the range.

9

NOTE 1 The elaboration of a subtype_indication consisting of a subtype_mark followed by an index_constraint checks the compatibility of the index_constraint with the subtype_mark (see 3.2.2).

10

NOTE 2 Even if an array value does not satisfy the index constraint of an array subtype, Constraint_Error is not raised on conversion to the array subtype, so long as the length of each dimension of the array value and the array subtype match. See 4.6.

Examples

11

Examples of array declarations including an index constraint:

12/5

Board : Matrix(1 .. 8, 1 .. 8); -- see 3.6 Rectangle : Matrix(1 .. 20, 1 .. 30); Inverse : Matrix(1 .. N, 1 .. N); -- N can be nonstatic 13/5

Filter : Bit_Vector(0 .. 31); -- see 3.6

14

Example of array declaration with a constrained array subtype:

15

My_Schedule : Schedule; -- all arrays of type Schedule have the same bounds

16

Example of record type with a component that is an array:

17

type Var_Line(Length : Natural) is record Image : String(1 .. Length); end record; 18 Null_Line : Var_Line(0); -- Null_Line.Image is a null array

Extensions to Ada 83

18.a

We allow the declaration of a variable with a nominally unconstrained array subtype, so long as it has an initialization expression to determine its bounds.

Wording Changes from Ada 83

18.b

We have moved the syntax for index_constraint and discrete_range here since they are no longer used in constrained_array_definitions. We therefore also no longer have to describe the (special) semantics of index_constraints and discrete_ranges that appear in constrained_array_definitions.

18.c

The rules given in RM83-3.6.1(5,7-10), which define the bounds of an array object, are redundant with rules given elsewhere, and so are not repeated here. RM83-3.6.1(6), which requires that the (nominal) subtype of an array variable be constrained, no longer applies, so long as the variable is explicitly initialized.

3.6.2 Operations of Array Types

Legality Rules

1

[The argument N used in the attribute_designators for the N-th dimension of an array shall be a static expression of some integer type.] The value of N shall be positive (nonzero) and no greater than the dimensionality of the array.

Static Semantics

2/1

{8652/0006} The following attributes are defined for a prefix A that is of an array type [(after any implicit dereference)], or denotes a constrained array subtype:

2.a
ramification

These attributes are not defined if A is a subtype-mark for an access-to-array subtype. They are defined (by implicit dereference) for access-to-array values.

3

A'First
A'First denotes the lower bound of the first index range; its type is the corresponding index type.
4

A'First(N)
A'First(N) denotes the lower bound of the N-th index range; its type is the corresponding index type.
5

A'Last
A'Last denotes the upper bound of the first index range; its type is the corresponding index type.
6

A'Last(N)
A'Last(N) denotes the upper bound of the N-th index range; its type is the corresponding index type.
7

A'Range
A'Range is equivalent to the range A'First .. A'Last, except that the prefix A is only evaluated once.
8

A'Range(N)
A'Range(N) is equivalent to the range A'First(N) .. A'Last(N), except that the prefix A is only evaluated once.
9

A'Length
A'Length denotes the number of values of the first index range (zero for a null range); its type is universal_integer.
10

A'Length(N)
A'Length(N) denotes the number of values of the N-th index range (zero for a null range); its type is universal_integer.

Implementation Advice

11/3

An implementation should normally represent multidimensional arrays in row-major order, consistent with the notation used for multidimensional array aggregates (see 4.3.3). However, if convention Fortran is specified for a multidimensional array type, then column-major order should be used instead (see B.5, “Interfacing with Fortran”).

11.a/2
implementation advice

Multidimensional arrays should be represented in row-major order, unless the array has convention Fortran.

12

NOTE 1 The attribute_references A'First and A'First(1) denote the same value. A similar relation exists for the attribute_references A'Last, A'Range, and A'Length. The following relation is satisfied (except for a null array) by the above attributes if the index type is an integer type:

13

A'Length(N) = A'Last(N) - A'First(N) + 1

14

NOTE 2 An array type is limited if its component type is limited (see 7.5).

15

NOTE 3 The predefined operations of an array type include the membership tests, qualification, and explicit conversion. If the array type is not limited, they also include assignment and the predefined equality operators. For a one-dimensional array type, they include the predefined concatenation operators (if nonlimited) and, if the component type is discrete, the predefined relational operators; if the component type is boolean, the predefined logical operators are also included.

16/2

NOTE 4 A component of an array can be named with an indexed_component. A value of an array type can be specified with an array_aggregate. For a one-dimensional array type, a slice of the array can be named; also, string literals are defined if the component type is a character type.

Examples

17/5

Examples (using arrays declared in the examples of 3.6.1):

18

-- Filter'First = 0 Filter'Last = 31 Filter'Length = 32 -- Rectangle'Last(1) = 20 Rectangle'Last(2) = 30

3.6.3 String Types

Static Semantics

1

A one-dimensional array type whose component type is a character type is called a string type.

2/2

[There are three predefined string types, String, Wide_String, and Wide_Wide_String, each indexed by values of the predefined subtype Positive; these are declared in the visible part of package Standard:]

3

[subtype Positive is Integer range 1 .. Integer'Last; 4/2

type String is array(Positive range <>) of Character; type Wide_String is array(Positive range <>) of Wide_Character; type Wide_Wide_String is array(Positive range <>) of Wide_Wide_Character; ]

5

NOTE String literals (see 2.6 and 4.2) are defined for all string types. The concatenation operator & is predefined for string types, as for all nonlimited one-dimensional array types. The ordering operators <, <=, >, and >= are predefined for string types, as for all one-dimensional discrete array types; these ordering operators correspond to lexicographic order (see 4.5.2).

Examples

6

Examples of string objects:

7

Stars : String(1 .. 120) := (1 .. 120 => '*' ); Question : constant String := "How many characters?"; -- Question'First = 1, Question'Last = 20 -- Question'Length = 20 (the number of characters) 8 Ask_Twice : String := Question & Question; -- constrained to (1..40) Ninety_Six : constant Roman := "XCVI"; -- see 3.5.2 and 3.6

Inconsistencies With Ada 83

8.a

The declaration of Wide_String in Standard hides a use-visible declaration with the same defining_identifier. In rare cases, this might result in an inconsistency between Ada 83 and Ada 95.

Incompatibilities With Ada 83

8.b

Because both String and Wide_String are always directly visible, an expression like

8.c

"a" < "bc"

8.d

is now ambiguous, whereas in Ada 83 both string literals could be resolved to type String.

Extensions to Ada 83

8.e

The type Wide_String is new (though it was approved by ARG for Ada 83 compilers as well).

Wording Changes from Ada 83

8.f

We define the term string type as a natural analogy to the term character type.

Inconsistencies With Ada 95

8.g/2

The declaration of Wide_Wide_String in Standard hides a use-visible declaration with the same defining_identifier. In the (very) unlikely event that an Ada 95 program had depended on such a use-visible declaration, and the program remains legal after the substitution of Standard.Wide_Wide_String, the meaning of the program will be different.

Extensions to Ada 95

8.h/2

The type Wide_Wide_String is new.