5.5 Loop Statements
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[A loop_statement
includes a sequence_of_statements
that is to be executed repeatedly, zero or more times with the iterations running sequentially or concurrently with one another.]
Syntax
2loop_statement
::=
[loop_statement_identifier
:]
[iteration_scheme
] loop
sequence_of_statements
end loop [loop_identifier
];
3/5iteration_scheme
::=
while condition
| for loop_parameter_specification
| for iterator_specification
| [parallel [aspect_specification
]]
for procedural_iterator
| parallel [(chunk_specification
)] [aspect_specification
]
for loop_parameter_specification
| parallel [(chunk_specification
)] [aspect_specification
]
for iterator_specification
3.1/5chunk_specification
::=
integer_simple_expression
| defining_identifier
in discrete_subtype_definition
4/5loop_parameter_specification
::=
defining_identifier
in [reverse] discrete_subtype_definition
[iterator_filter
]
4.1/5iterator_filter
::=
when condition
5If a loop_statement
has a loop_statement_identifier
, then the identifier
shall be repeated after the end loop; otherwise, there shall not be an identifier
after the end loop.
An iteration_scheme
that begins with the reserved word parallel shall not have the reserved word reverse in its loop_parameter_specification
.
Name Resolution Rules
5.2/5In a chunk_specification
that is an integer_simple_expression
, the integer_simple_expression
is expected to be of any integer type.
Static Semantics
6/5A loop_parameter_specification
declares a loop parameter, which is an object whose subtype (and nominal subtype) is that defined by the discrete_subtype_definition
.
In a chunk_specification
that has a discrete_subtype_definition
, the chunk_specification
declares a chunk parameter object whose subtype (and nominal subtype) is that defined by the discrete_subtype_definition
.
Dynamic Semantics
6.2/5The filter of an iterator construct (a loop_parameter_specification
, iterator_specification
, or procedural_iterator
) is defined to be satisfied when there is no iterator_filter
for the iterator construct, or when the condition
of the iterator_filter
evaluates to True for a given iteration of the iterator construct.
If a sequence_of_statements
of a loop_statement
with an iterator construct is said to be conditionally executed, then the statement
s are executed only when the filter of the iterator construct is satisfied.
The loop iterators loop_parameter_specification
and iterator_specification
can also be used in contexts other than loop_statement
s (for example, see 4.3.5 and 4.5.8). In such a context, the iterator conditionally produces values in the order specified for the associated construct below or in 5.5.2. The values produced are the values given to the loop parameter when the filter of the iterator construct is satisfied for that value. [No value is produced when the condition
of an iterator_filter
evaluates to False.]
For the execution of a loop_statement
, the sequence_of_statements
is executed zero or more times, until the loop_statement
is complete. The loop_statement
is complete when a transfer of control occurs that transfers control out of the loop, or, in the case of an iteration_scheme
, as specified below.
For the execution of a loop_statement
with a while iteration_scheme
, the condition is evaluated before each execution of the sequence_of_statements
; if the value of the condition
is True, the sequence_of_statements
is executed; if False, the execution of the loop_statement
is complete.
If the reserved word parallel is present in the iteration_scheme
of a loop_statement
(a parallel loop), the iterations are partitioned into one or more chunks, each with its own separate logical thread of control (see clause 9). If a chunk_specification
is present in a parallel loop, it is elaborated first, and the result of the elaboration determines the maximum number of chunks used for the parallel loop. If the chunk_specification
is an integer_simple_expression
, the elaboration evaluates the expression, and the value of the expression determines the maximum number of chunks. If a discrete_subtype_definition
is present, the elaboration elaborates the discrete_subtype_definition
, which defines the subtype of the chunk parameter, and the number of values in this subtype determines the maximum number of chunks. After elaborating the chunk_specification
, a check is made that the determined maximum number of chunks is greater than zero. If this check fails, Program_Error is raised.
For the execution of a loop_statement
that has an iteration_scheme
including a loop_parameter_specification
, after elaborating the chunk_specification
and aspect_specification
, if any, the loop_parameter_specification
is elaborated. This elaborates the discrete_subtype_definition
, which defines the subtype of the loop parameter. If the discrete_subtype_definition
defines a subtype with a null range, the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is conditionally executed once for each value of the discrete subtype defined by the discrete_subtype_definition
that satisfies the predicates of the subtype (or until the loop is left as a consequence of a transfer of control). Prior to each such iteration, the corresponding value of the discrete subtype is assigned to the loop parameter associated with the given iteration. If the loop is a parallel loop, each chunk has its own logical thread of control with its own copy of the loop parameter; otherwise (a sequential loop), a single logical thread of control performs the loop, and there is a single copy of the loop parameter. Each logical thread of control handles a distinct subrange of the values of the subtype of the loop parameter such that all values are covered with no overlaps. Within each logical thread of control, the values are assigned to the loop parameter in increasing order unless the reserved word reverse is present, in which case the values are assigned in decreasing order. In the absence of a transfer of control, the associated parallel construct of a loop_parameter_specification
is complete when all of its logical threads of control are complete.
discrete_subtype_definition
. for I in S loop
Call (I);
end loop;
10.efor I in S'Base loop
if I in S then
Call (I);
end if;
end loop;
If a chunk_specification
with a discrete_subtype_definition
is present, then the logical thread of control associated with a given chunk has its own copy of the chunk parameter initialized with a distinct value from the discrete subtype defined by the discrete_subtype_definition
. The values of the chunk parameters are assigned such that they increase with increasing values of the ranges covered by the corresponding loop parameters.
Whether or not a chunk_specification
is present in a parallel loop, the total number of iterations of the loop represents an upper bound on the number of logical threads of control devoted to the loop.
[For details about the execution of a loop_statement
with the iteration_scheme
including an iterator_specification
, see 5.5.2. For details relating to a procedural_iterator
, see 5.5.3.]
loop_parameter_specification
is a constant; it cannot be updated within the sequence_of_statements
of the loop (see 3.3).object_declaration
is expected for a loop parameter, since the loop parameter is automatically declared by the loop_parameter_specification
. The scope of a loop parameter extends from the loop_parameter_specification
to the end of the loop_statement
, and the visibility rules are such that a loop parameter is only visible within the sequence_of_statements
of the loop. loop_parameter_specification
. discrete_subtype_definition
of a for loop is elaborated just once. Use of the reserved word reverse does not alter the discrete subtype defined, so that the following iteration_scheme
s are not equivalent; the first has a null range. for J in reverse 1 .. 0
for J in 0 .. 1
loop_parameter_specification
has a static discrete range, the subtype of the loop parameter is static. Examples
18Example of a loop statement without an iteration scheme:
loop
Get(Current_Character);
exit when Current_Character = '*';
end loop;
Example of a loop statement with a while iteration scheme:
while Bid(N).Price < Cut_Off.Price loop
Record_Bid(Bid(N).Price);
N := N + 1;
end loop;
Example of a loop statement with a for iteration scheme:
for J in Buffer'Range loop -- works even with a null range
if Buffer(J) /= Space then
Put(Buffer(J));
end if;
end loop;
Example of a loop statement with a name:
Summation:
while Next /= Head loop -- see 3.10.1
Sum := Sum + Next.Value;
Next := Next.Succ;
end loop Summation;
26/5Example of a simple parallel loop:
-- see 3.6
parallel
for I in Grid'Range(1) loop
Grid(I, 1) := (for all J in Grid'Range(2) => Grid(I,J) = True);
end loop;
28/5Example of a parallel loop with a chunk specification:
declare
subtype Chunk_Number is Natural range 1 .. 8;
30/5Partial_Sum,
Partial_Max : array (Chunk_Number) of Natural := (others => 0);
Partial_Min : array (Chunk_Number) of Natural :=
(others => Natural'Last);
31/5begin
parallel (Chunk in Chunk_Number)
for I in Grid'Range(1) loop
declare
True_Count : constant Natural :=
[for J in Grid'Range(2) =>
(if Grid (I, J) then 1 else 0)]'Reduce("+",0);
begin
Partial_Sum (Chunk) := @ + True_Count;
Partial_Min (Chunk) := Natural'Min(@, True_Count);
Partial_Max (Chunk) := Natural'Max(@, True_Count);
end;
end loop;
32/5Put_Line
("Total=" & Partial_Sum'Reduce("+", 0)'Image &
", Min=" & Partial_Min'Reduce(Natural'Min, Natural'Last)'Image &
", Max=" & Partial_Max'Reduce(Natural'Max, 0)'Image);
end;
33/5For an example of an iterator_filter
, see 4.5.8.
Wording Changes from Ada 83
Wording Changes from Ada 2005
iterator_specification
s are allowed in for loops; these are documented as an extension in the appropriate subclause. Extensions to Ada 2012
iterator_filter
is now allowed on loop_parameter_specification
s. This is mainly for consistency with aggregate and reduction iterators, where it eliminates the need for temporary objects. Wording Changes from Ada 2012
5.5.1 User-Defined Iterator Types
Static Semantics
1/3The following language-defined generic library package exists:
generic
type Cursor;
with function Has_Element (Position : Cursor) return Boolean;
package Ada.Iterator_Interfaces
with Pure, Nonblocking => False is
3/3type Forward_Iterator is limited interface;
function First (Object : Forward_Iterator) return Cursor is abstract;
function Next (Object : Forward_Iterator; Position : Cursor)
return Cursor is abstract;
4/3type Reversible_Iterator is limited interface and Forward_Iterator;
function Last (Object : Reversible_Iterator) return Cursor is abstract;
function Previous (Object : Reversible_Iterator; Position : Cursor)
return Cursor is abstract;
4.1/5type Parallel_Iterator is limited interface and Forward_Iterator;
4.2/5subtype Chunk_Index is Positive;
4.3/5function Is_Split (Object : Parallel_Iterator)
return Boolean is abstract;
4.4/5procedure Split_Into_Chunks (Object : in out Parallel_Iterator;
Max_Chunks : in Chunk_Index) is abstract
with Pre'Class => not Object.Is_Split or else raise Program_Error,
Post'Class => Object.Is_Split and then
Object.Chunk_Count <= Max_Chunks;
4.5/5function Chunk_Count (Object : Parallel_Iterator)
return Chunk_Index is abstract
with Pre'Class => Object.Is_Split or else raise Program_Error;
4.6/5function First (Object : Parallel_Iterator;
Chunk : Chunk_Index) return Cursor is abstract
with Pre'Class => (Object.Is_Split and then
Chunk <= Object.Chunk_Count)
or else raise Program_Error;
4.7/5function Next (Object : Parallel_Iterator;
Position : Cursor;
Chunk : Chunk_Index) return Cursor is abstract
with Pre'Class => (Object.Is_Split and then
Chunk <= Object.Chunk_Count)
or else raise Program_Error;
4.8/5type Parallel_Reversible_Iterator is limited interface
and Parallel_Iterator and Reversible_Iterator;
5/3end Ada.Iterator_Interfaces;
An iterator type is a type descended from the Forward_Iterator interface from some instance of Ada.Iterator_Interfaces. A reversible iterator type is a type descended from the Reversible_Iterator interface from some instance of Ada.Iterator_Interfaces. A parallel iterator type is a type descended from the Parallel_Iterator interface from some instance of Ada.Iterator_Interfaces. A type descended from the Parallel_Reversible_Iterator interface from some instance of Ada.Iterator_Interfaces is both a parallel iterator type and a reversible iterator type. An iterator object is an object of an iterator type. A reversible iterator object is an object of a reversible iterator type. A parallel iterator object is an object of a parallel iterator type. The formal subtype Cursor from the associated instance of Ada.Iterator_Interfaces is the iteration cursor subtype for the iterator type.
The following type-related operational aspects may be specified for an indexable container type T (see 4.1.6):
Default_Iterator
- This aspect is specified by a
name
that denotes exactly one function declared immediately within the same declaration list in which T, or the declaration completed by T, is declared, whose first parameter is of type T or T'Class or an access parameter whose designated type is type T or T'Class, whose other parameters, if any, have default expressions, and whose result type is an iterator type. This function is the default iterator function for T. Its result subtype is the default iterator subtype for T. The iteration cursor subtype for the default iterator subtype is the default cursor subtype for T. This aspect is inherited by descendants of type T (including T'Class).
Iterator_Element
- This aspect is specified by a
name
that denotes a subtype. This is the default element subtype for T. This aspect is inherited by descendants of type T (including T'Class).
Iterator_View
- This aspect is specified by a
name
that denotes a type T2 with the following properties:
- T2 is declared in the same compilation unit as T;
- T2 is an iterable container type;
- T2 has a single discriminant which is an access discriminant designating T; and
- The default iterator subtypes for T and T2 statically match.
- This aspect is never inherited[, even by T'Class].
This paragraph was deleted.
An iterable container type is an indexable container type with specified Default_Iterator and Iterator_Element aspects. A reversible iterable container type is an iterable container type with the default iterator type being a reversible iterator type. A parallel iterable container type is an iterable container type with the default iterator type being a parallel iterator type. An iterable container object is an object of an iterable container type. A reversible iterable container object is an object of a reversible iterable container type. A parallel iterable container object is an object of a parallel iterable container type.
The Default_Iterator and Iterator_Element aspects are nonoverridable (see 13.1.1).
Legality Rules
13/3The Constant_Indexing aspect (if any) of an iterable container type T shall denote exactly one function with the following properties:
- the result type of the function is covered by the default element type of T or is a reference type (see 4.1.5) with an access discriminant designating a type covered by the default element type of T;
- the type of the second parameter of the function covers the default cursor type for T;
- if there are more than two parameters, the additional parameters all have default expressions.
This function (if any) is the default constant indexing function for T.
The Variable_Indexing aspect (if any) of an iterable container type T shall denote exactly one function with the following properties:
- the result type of the function is a reference type (see 4.1.5) with an access discriminant designating a type covered by the default element type of T;
- the type of the second parameter of the function covers the default cursor type for T;
- if there are more than two parameters, the additional parameters all have default expressions.
This function (if any) is the default variable indexing function for T.
Erroneous Execution
23/5A call on the First or Next operation on a given Parallel_Iterator object with a given Chunk value, which does not propagate an exception, should return a Cursor value that either yields False when passed to Has_Element, or that identifies an element distinct from any Cursor value returned by a call on a First or Next operation on the same Parallel_Iterator object with a different Chunk value. If the First or Next operations with a Chunk parameter behave in any other manner, execution is erroneous.
Extensions to Ada 2005
Incompatibilities With Ada 2012
Extensions to Ada 2012
5.5.2 Generalized Loop Iteration
1/3Generalized forms of loop iteration are provided by an iterator_specification
.
Syntax
2/5iterator_specification
::=
defining_identifier
[: loop_parameter_subtype_indication
] in [reverse] iterator_name
[iterator_filter
]
| defining_identifier
[: loop_parameter_subtype_indication
] of [reverse] iterable_name
[iterator_filter
]
2.1/5loop_parameter_subtype_indication
::=
subtype_indication
| access_definition
2.2/5If an iterator_specification
is for a parallel construct, the reserved word reverse shall not appear in the iterator_specification
.
Name Resolution Rules
3/3For the first form of iterator_specification
, called a generalized iterator, the expected type for the iterator_name
is any iterator type. For the second form of iterator_specification
, the expected type for the iterable_name
is any array or iterable container type. If the iterable_name
denotes an array object, the iterator_specification
is called an array component iterator; otherwise it is called a container element iterator.
Legality Rules
45/5If the reserved word reverse appears, the iterator_specification
is a reverse iterator. If the iterator_specification
is for a parallel construct, the iterator_specification
is a parallel iterator. Otherwise, it is a forward iterator. Forward and reverse iterators are collectively called sequential iterators. In a reverse generalized iterator, the iterator_name
shall be of a reversible iterator type. In a parallel generalized iterator, the iterator_name
shall be of a parallel iterator type. In a reverse container element iterator, the default iterator type for the type of the iterable_name
shall be a reversible iterator type. In a parallel container element iterator, the default iterator type for the type of the iterable_name
shall be of a parallel iterator type.
The subtype defined by the loop_parameter_subtype_indication
, if any, of a generalized iterator shall statically match the iteration cursor subtype. The subtype defined by the loop_parameter_subtype_indication
, if any, of an array component iterator shall statically match the component subtype of the type of the iterable_name
. The subtype defined by the loop_parameter_subtype_indication
, if any, of a container element iterator shall statically match the default element subtype for the type of the iterable_name
.
In a container element iterator whose iterable_name
has type T, if the iterable_name
denotes a constant or the Variable_Indexing aspect is not specified for T, then the Constant_Indexing aspect shall be specified for T.
The iterator_name
or iterable_name
of an iterator_specification
shall not denote a subcomponent that depends on discriminants of an object whose nominal subtype is unconstrained, unless the object is known to be constrained.
A container element iterator is illegal if the call of the default iterator function that creates the loop iterator (see below) is illegal.
name
is a constant. The wording applies to any reason that the call would be illegal, as it's possible that one of the default parameters would be illegal, or that some accessibility check would fail. A generalized iterator is illegal if the iteration cursor subtype of the iterator_name
is a limited type at the point of the generalized iterator. A container element iterator is illegal if the default cursor subtype of the type of the iterable_name
is a limited type at the point of the container element iterator.
Static Semantics
8/5An iterator_specification
declares a loop parameter. In a generalized iterator, an array component iterator, or a container element iterator, if a loop_parameter_subtype_indication
is present, it determines the nominal subtype of the loop parameter. In a generalized iterator, if a loop_parameter_subtype_indication
is not present, the nominal subtype of the loop parameter is the iteration cursor subtype. In an array component iterator, if a loop_parameter_subtype_indication
is not present, the nominal subtype of the loop parameter is the component subtype of the type of the iterable_name
. In a container element iterator, if a loop_parameter_subtype_indication
is not present, the nominal subtype of the loop parameter is the default element subtype for the type of the iterable_name
.
In a generalized iterator, the loop parameter is a constant. In an array component iterator, the loop parameter is a constant if the iterable_name
denotes a constant; otherwise it denotes a variable. In a container element iterator, the loop parameter is a constant if the iterable_name
denotes a constant, or if the Variable_Indexing aspect is not specified for the type of the iterable_name
; otherwise it is a variable.
sequence_of_statements
; the accessibility of the loop parameter is that of the block statement. Dynamic Semantics
10/3For the execution of a loop_statement
with an iterator_specification
, the iterator_specification
is first elaborated. This elaboration elaborates the subtype_indication
, if any.
For a sequential generalized iterator, the loop parameter is created, the iterator_name
is evaluated, and the denoted iterator object becomes the loop iterator. In a forward generalized iterator, the operation First of the iterator type is called on the loop iterator, to produce the initial value for the loop parameter. If the result of calling Has_Element on the initial value is False, then the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is conditionally executed and then the Next operation of the iterator type is called with the loop iterator and the current value of the loop parameter to produce the next value to be assigned to the loop parameter. This repeats until the result of calling Has_Element on the loop parameter is False, or the loop is left as a consequence of a transfer of control. For a reverse generalized iterator, the operations Last and Previous are called rather than First and Next.
For a parallel generalized iterator, the chunk_specification
, if any, of the associated parallel construct, is first elaborated, to determine the maximum number of chunks (see 5.5), and then the operation Split_Into_Chunks of the iterator type is called, with the determined maximum passed as the Max_Chunks parameter, specifying the upper bound for the number of loop parameter objects (and the number of logical threads of control) to be associated with the iterator. In the absence of a chunk_specification
, the maximum number of chunks is determined in an implementation-defined manner.
chunk_specification
.Upon return from Split_Into_Chunks, the actual number of chunks for the loop is determined by calling the Chunk_Count operation of the iterator, at which point one logical thread of control is initiated for each chunk, with an associated chunk index in the range from one to the actual number of chunks.
Within each logical thread of control, a loop parameter is created. If a chunk_specification
with a discrete_subtype_definition
is present in the associated parallel construct, then a chunk parameter is created and initialized with a value from the discrete subtype defined by the discrete_subtype_definition
, so that the order of the chosen chunk parameter values correspond to the order of the chunk indices associated with the logical threads of control. The operation First of the iterator type that has a Chunk parameter is called on the loop iterator, with Chunk initialized from the corresponding chunk index, to produce the initial value for the loop parameter. If the result of calling Has_Element on this initial value is False, then the execution of the logical thread of control is complete. Otherwise, the sequence_of_statements
is conditionally executed, and then the Next operation of the iterator type that has a Chunk parameter is called with the loop iterator, the current value of the loop parameter, and the corresponding chunk index, to produce the next value to be assigned to the loop parameter. This repeats until the result of calling Has_Element on the loop parameter is False, or the associated parallel construct is left as a consequence of a transfer of control.
In the absence of a transfer of control, the associated parallel construct of a parallel generalized iterator is complete when all of its logical threads of control are complete.
For an array component iterator, the chunk_specification
of the associated parallel construct, if any, is first elaborated to determine the maximum number of chunks (see 5.5), and then the iterable_name
is evaluated and the denoted array object becomes the array for the loop. If the array for the loop is a null array, then the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is conditionally executed with the loop parameter denoting each component of the array for the loop, using a canonical order of components, which is last dimension varying fastest (unless the array has convention Fortran, in which case it is first dimension varying fastest). For a forward array component iterator, the iteration starts with the component whose index values are each the first in their index range, and continues in the canonical order. For a reverse array component iterator, the iteration starts with the component whose index values are each the last in their index range, and continues in the reverse of the canonical order. For a parallel array component iterator, the iteration is broken up into contiguous chunks of the canonical order, such that all components are covered with no overlaps; each chunk has its own logical thread of control with its own loop parameter and iteration within each chunk is in the canonical order. The number of chunks is implementation defined, but is limited in the presence of a chunk_specification
to the determined maximum. The loop iteration proceeds until the sequence_of_statements
has been conditionally executed for each component of the array for the loop, or until the loop is left as a consequence of a transfer of control.
If a chunk_specification
with a discrete_subtype_definition
is present in the associated parallel construct, then the logical thread of control associated with a given chunk has a chunk parameter initialized with a distinct value from the discrete subtype defined by the discrete_subtype_definition
. The values of the chunk parameters are assigned such that they increase in the canonical order of the starting array components for the chunks.
For a container element iterator, the chunk_specification
of the associated parallel construct, if any, is first elaborated to determine the maximum number of chunks (see 5.5), and then the iterable_name
is evaluated. If the container type has Iterator_View specified, an object of the Iterator_View type is created with the discriminant referencing the iterable container object denoted by the iterable_name
. This is the iterable container object for the loop. Otherwise, the iterable container object denoted by the iterable_name
becomes the iterable container object for the loop . The default iterator function for the type of the iterable container object for the loop is called on the iterable container object and the result is the loop iterator. For a sequential container element iterator, an object of the default cursor subtype is created (the loop cursor). For a parallel container element iterator, each chunk of iterations will have its own loop cursor, again of the default cursor subtype.
A container element iterator then proceeds as described above for a generalized iterator, except that each reference to a loop parameter is replaced by a reference to the corresponding loop cursor. For a container element iterator, the loop parameter for each iteration instead denotes an indexing (see 4.1.6) into the iterable container object for the loop, with the only parameter to the indexing being the value of the loop cursor for the given iteration . If the loop parameter is a constant (see above), then the indexing uses the default constant indexing function for the type of the iterable container object for the loop; otherwise it uses the default variable indexing function.
Any exception propagated by the execution of a generalized iterator or container element iterator is propagated by the immediately enclosing loop statement.
Examples
16/5Example of a parallel generalized loop over an array:
parallel
for Element of Board loop -- See 3.6.1.
Element := Element * 2.0; -- Double each element of Board, a two-dimensional array.
end loop;
18/5For examples of use of generalized iterators, see A.18.33 and the corresponding container packages in A.18.2 and A.18.3.
Extensions to Ada 2005
Incompatibilities With Ada 2012
Extensions to Ada 2012
subtype_indication
on a generalized iterator, and anonymous access types on all forms of iterator. We introduced a new syntax non-terminal, loop_parameter_subtype_indication
to simplfy the wording.iterator_filter
is now allowed on iterator_specification
s. This is mainly for consistency with aggregate and reduction iterators, where it eliminates the need for temporary objects. Wording Changes from Ada 2012
5.5.3 Procedural Iterators
1/5A procedural_iterator
invokes a user-defined procedure, passing in the body of the enclosing loop_statement
as a parameter of an anonymous access-to-procedure type, to allow the loop body to be executed repeatedly as part of the invocation of the user-defined procedure.
Syntax
2/5procedural_iterator
::=
iterator_parameter_specification
of iterator_procedure_call
[iterator_filter
]
3/5iterator_parameter_specification
::=
formal_part
| (defining_identifier
{, defining_identifier
})
4/5iterator_procedure_call
::=
procedure_name
| procedure_prefix
iterator_actual_parameter_part
5/5iterator_actual_parameter_part
::=
(iterator_parameter_association
{, iterator_parameter_association
})
6/5iterator_parameter_association
::=
parameter_association
| parameter_association_with_box
7/5parameter_association_with_box
::=
[ formal_parameter_selector_name
=> ] <>
8/5At most one iterator_parameter_association
within an iterator_actual_parameter_part
shall be a parameter_association_with_box
.
Name Resolution Rules
9/5The name
or prefix
given in an iterator_procedure_call
shall resolve to denote a callable entity C (the iterating procedure) that is a procedure, or an entry renamed as (viewed as) a procedure. [When there is an iterator_actual_parameter_part
, the prefix
can be an implicit_dereference
of an access-to-subprogram value.]
An iterator_procedure_call
without a parameter_association_with_box
is equivalent to one with an iterator_actual_parameter_part
with an additional parameter_association_with_box
at the end, with the formal_parameter_selector_name
identifying the last formal parameter of the callable entity denoted by the name
or prefix
.
An iterator_procedure_call
shall contain at most one iterator_parameter_association
for each formal parameter of the callable entity C. Each formal parameter without an iterator_parameter_association
shall have a default_expression
(in the profile of the view of C denoted by the name
or prefix
).
The formal parameter of the callable entity C associated with the parameter_association_with_box
shall be of an anonymous access-to-procedure type A.
Legality Rules
13/5The anonymous access-to-procedure type A shall have at least one formal parameter in its parameter profile. If the iterator_parameter_specification
is a formal_part
, then this formal_part
shall be mode conformant with that of A. If the iterator_parameter_specification
is a list of defining_identifier
s, the number of formal parameters of A shall be the same as the length of this list.
[If the name
or prefix
given in an iterator_procedure_call
denotes an abstract subprogram, the subprogram shall be a dispatching subprogram.]
Static Semantics
15/5A loop_statement
with an iteration_scheme
that has a procedural_iterator
is equivalent to a local declaration of a procedure P followed by a procedure_call_statement
that is formed from the iterator_procedure_call
by replacing the <> of the parameter_association_with_box
with P'Access. The formal_part
of the locally declared procedure P is formed from the formal_part
of the anonymous access-to-procedure type A, by replacing the identifier
of each formal parameter of this formal_part
with the identifier
of the corresponding formal parameter or element of the list of defining_identifier
s given in the iterator_parameter_specification
. The body of P consists of the conditionally executed sequence_of_statements
. The procedure P is called the loop body procedure.
procedure P ... is
begin
if iterator_filter then
sequence_of_statements
end if;
end P;
In a procedural iterator, the Parallel_Calls aspect (see 9.10.1) of the loop body procedure is True if the reserved word parallel occurs in the corresponding loop statement, and False otherwise.
The following aspects may be specified for a callable entity S that has exactly one formal parameter of an anonymous access-to-subprogram type:
Allows_Exit
- The Allows_Exit aspect is of type Boolean. The specified value shall be static. The Allows_Exit aspect of an inherited primitive subprogram is True if Allows_Exit is True either for the corresponding subprogram of the progenitor type or for any other inherited subprogram that it overrides. If not specified or inherited as True, the Allows_Exit aspect of a callable entity is False. For an entry, only a confirming specification of False is permitted for the Allows_Exit aspect.
- Specifying the Allows_Exit aspect to be True for a subprogram indicates that the subprogram allows exit, meaning that it is prepared to be completed by arbitrary transfers of control from the loop body procedure[, including propagation of exceptions. A subprogram for which Allows_Exit is True should use finalization as appropriate rather than exception handling to recover resources and make any necessary final updates to data structures].
procedural_iterator
. In particular, exception handlers in S, even when others handlers, will not be executed when a transfer of control occurs. The mechanism that the implementation uses to implement such transfers of control needs to avoid triggering exception handlers. Parallel_Iterator
- The Parallel_Iterator aspect is of type Boolean. The specified value shall be static. The Parallel_Iterator aspect of an inherited primitive subprogram is True if Parallel_Iterator is True either for the corresponding subprogram of the progenitor type or for any other inherited subprogram that it overrides. If not specified or inherited as True, the Parallel_Iterator aspect of a callable entity is False.
- Specifying the Parallel_Iterator aspect to be True for a callable entity indicates that the entity is allowed to invoke the loop body procedure from multiple distinct logical threads of control. The Parallel_Iterator aspect for a subprogram shall be statically False if the subprogram allows exit.
Legality Rules
22/5If a callable entity overrides an inherited dispatching subprogram that allows exit, the overriding callable entity also shall allow exit. If a callable entity overrides an inherited dispatching subprogram that has a True Parallel_Iterator aspect, the overriding callable entity also shall have a True Parallel_Iterator aspect.
A loop_statement
with a procedural_iterator
as its iteration_scheme
shall begin with the reserved word parallel if and only if the callable entity identified in the iterator_procedure_call
has a Parallel_iterator aspect of True.
If the actual parameter of an anonymous access-to-subprogram type, passed in an explicit call of a subprogram for which the Parallel_Iterator aspect is True, is of the form P'Access, the designated subprogram P shall have a Parallel_Calls aspect True (see 9.10.1).
The sequence_of_statements
of a loop_statement
with a procedural_iterator
as its iteration_scheme
shall contain an exit_statement
, return statement, goto_statement
, or requeue_statement
that leaves the loop only if the callable entity associated with the procedural_iterator
allows exit.
The sequence_of_statements
of a loop_statement
with a procedural_iterator
as its iteration_scheme
shall not contain an accept_statement
whose entry_declaration
occurs outside the loop_statement
.
accept_statement
is not allowed in a procedure (see 9.5.2), it has to be directly in a task_body
. Since the loop body here is implemented as a procedure, we can't allow accept_statement
s there, either, even if the loop itself is directly in a task_body
. accept_statement
is part of another construct, for instance, a select_statement
. Dynamic Semantics
27/5[For the execution of a loop_statement
with an iteration_scheme
that has a procedural_iterator
, the procedure denoted by the name
or prefix
of the iterator_procedure_call
(the iterating procedure) is invoked, passing an access value designating the loop body procedure as a parameter. The iterating procedure then calls the loop body procedure zero or more times and returns, whereupon the loop_statement
is complete. If the parallel reserved word is present, the iterating procedure is allowed to invoke the loop body procedure from multiple distinct logical threads of control.] The aspect_specification
, if any, is elaborated prior to the invocation of the iterating procedure.
Bounded (Run-Time) Errors
28/5If the callable entity identified in the iterator_procedure_call
allows exit, then it is a bounded error for a call of the loop body procedure to be performed from within an abort-deferred operation (see 9.8), unless the entire loop_statement
was within the same abort-deferred operation. If detected, Program_Error is raised at the point of the call; otherwise, a transfer of control from the sequence_of_statements
of the loop_statement
will not necessarily terminate the loop_statement
, and the loop body procedure can be called again.
If a loop_statement
with the procedural_iterator
as its iteration_scheme
(see 5.5) does not begin with the reserved word parallel, it is a bounded error if the loop body procedure is invoked from a different logical thread of control than the one that initiates the loop_statement
. If detected, Program_Error is raised; otherwise, conflicts associated with concurrent executions of the loop body procedure can occur without being detected by the applicable conflict check policy (see 9.10.1). Furthermore, propagating an exception or making an attempt to exit in the presence of multiple threads of control will not necessarily terminate the loop_statement
, deadlock can occur, or the loop body procedure can be called again.
Examples
30/5Example of iterating over a map from My_Key_Type to My_Element_Type (see A.18.4):
for (C : Cursor) of My_Map.Iterate loop
Put_Line (My_Key_Type'Image (Key (C)) & " => " &
My_Element_Type'Image (Element (C)));
end loop;
32/5-- The above is equivalent to:
33/5declare
procedure P (C : Cursor) is
begin
Put_Line (My_Key_Type'Image (Key (c)) & " => " &
My_Element_Type'Image (Element (C)));
end P;
begin
My_Map.Iterate (P'Access);
end;
34/5Example of iterating over the environment variables (see A.17):
for (Name, Val) of Ada.Environment_Variables.Iterate(<>) loop
-- "(<>)" is optional because it is the last parameter
Put_Line (Name & " => " & Val);
end loop;
36/5-- The above is equivalent to:
37/5declare
procedure P (Name : String; Val : String) is
begin
Put_Line (Name & " => " & Val);
end P;
begin
Ada.Environment_Variables.Iterate (P'Access);
end;