Debian Codegraph @

source_2020/05.mss

raw
1 @Part(05, Root="ada.mss")
2
3 @Comment{$Date: 2020/06/03 00:09:00 $}
4 @LabeledSection{Statements}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/05.mss,v $}
7 @Comment{$Revision: 1.84 $}
8
9 @begin{Intro}
10 @Redundant[A @nt{statement} defines an action to be performed upon
11 its execution.]
12
13 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
14 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
15 @Redundant[This @Chg{Version=[3],New=[clause],Old=[section]} describes the
16 general rules applicable to all @nt{statement}s.
17 Some @nt{statement}s are discussed in later @Chg{Version=[3],New=[clauses],Old=[sections]}:
18 @nt{Procedure_@!call_@!statement}s and
19 @Chg{Version=[2],New=[return statements],Old=[@nt{return_@!statement}s]} are
20 described in @RefSec{Subprograms}.
21 @nt{Entry_@!call_@!statement}s, @nt{requeue_@!statement}s,
22 @nt{delay_@!statement}s, @nt{accept_@!statement}s,
23 @nt{select_@!statement}s, and @nt{abort_@!statement}s are described in
24 @RefSec{Tasks and Synchronization}.
25 @nt{Raise_@!statement}s are described in @RefSec{Exceptions},
26 and @nt{code_@!statement}s in
27 @RefSecNum{Representation Issues}.
28 The remaining forms of @nt{statement}s are presented in this
29 @Chg{Version=[3],New=[clause],Old=[section]}.]
30 @end{Intro}
31
32 @begin{DiffWord83}
33 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
34 The description of
35 @Chg{Version=[2],New=[return statements],Old=[@nt{return_@!statement}s]}
36 has been moved to
37 @RefSec{Return Statements}, so that it is closer to the
38 description of subprograms.
39 @end{DiffWord83}
40
41 @LabeledClause{Simple and Compound Statements - Sequences of Statements}
42
43 @begin{Intro}
44 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1]}
45 @Redundant[A @nt<statement> is either simple or compound.
46 A @nt<simple_statement> encloses
47 no other @nt<statement>. A @nt<compound_statement> can enclose
48 @nt<simple_statement>s and other @nt<compound_statement>s.]@Chg{Version=[5],New=[
49 A @i<parallel construct>@Defn{parallel construct}@Defn2{Term=[construct],Sec=[parallel]}
50 is a construct that introduces additional
51 logical threads of control (see clause @RefSecNum{Tasks and Synchronization})
52 without creating a new task.
53 Parallel loops (see @RefSecNum{Loop Statements}) and
54 @nt{parallel_block_statement}s (see
55 @RefSecNum{Parallel Block Statements}) are parallel
56 constructs.@IndexSee{Term=[parallel processing],See=(parallel construct)}
57 @IndexSee{Term=[concurrent processing],See=(parallel construct)}],Old=[]}
58
59 @ChgToGlossary{Version=[5],Kind=[Added],Term=<Parallel Construct>,
60 Text=<@ChgAdded{Version=[5],Text=[An executable construct that defines multiple
61 activities of a single task that can proceed in parallel, via the execution of
62 multiple logical threads of control.]}>}
63 @end{Intro}
64
65 @begin{Syntax}
66 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0179-1]}
67 @Syn{lhs=<sequence_of_statements>,rhs="@Syn2{statement} {@Syn2{statement}}@Chg{Version=[3],New=[ {@Syn2{label}}],Old=[]}"}
68
69
70 @Syn{lhs=<statement>,rhs="
71 {@Syn2{label}} @Syn2{simple_statement} | {@Syn2{label}} @Syn2{compound_statement}"}
72
73 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
74 @Syn{tabs=[P31], lhs=<simple_statement>,rhs="@Syn2{null_statement}
75 | @Syn2{assignment_statement}@\| @Syn2{exit_statement}
76 | @Syn2{goto_statement}@\| @Syn2{procedure_call_statement}
77 | @Chg{Version=[2],New=[@Syn2{simple_return_statement}],Old=[@Syn2{return_statement}]}@\| @Syn2{entry_call_statement}
78 | @Syn2{requeue_statement}@\| @Syn2{delay_statement}
79 | @Syn2{abort_statement}@\| @Syn2{raise_statement}
80 | @Syn2{code_statement}"}
81
82 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
83 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1]}
84 @Syn{tabs=[P31], lhs=<compound_statement>,rhs="
85 @Syn2{if_statement}@\| @Syn2{case_statement}
86 | @Syn2{loop_statement}@\| @Syn2{block_statement}@Chg{Version=[2],New=[
87 | @Syn2{extended_return_statement}],Old=[]}@Chg{Version=[5],New=[
88 | @Syn2{parallel_block_statement}],Old=[]}
89 | @Syn2{accept_statement}@\| @Syn2{select_statement}"}
90
91 @Syn{lhs=<null_statement>,rhs="@key{null};"}
92
93 @Syn{lhs=<label>,rhs="<<@SynI{label_}@Syn2{statement_identifier}>>"}
94
95 @Syn{lhs=<statement_identifier>,rhs="@Syn2{direct_name}"}
96
97 @begin(SyntaxText)
98 The @nt<direct_name> of a @nt<statement_identifier> shall
99 be an @nt<identifier> (not an @nt<operator_symbol>).
100 @end(SyntaxText)
101 @end{Syntax}
102
103 @begin{Resolution}
104 The @nt<direct_name> of a @nt<statement_identifier> shall resolve to
105 denote its corresponding implicit declaration (see below).
106 @end{Resolution}
107
108 @begin{Legality}
109 Distinct @nt{identifier}s shall be used for all
110 @nt<statement_identifier>s that
111 appear in the same body, including
112 inner @nt{block_statement}s
113 but excluding inner program units.
114 @end{Legality}
115
116 @begin{StaticSem}
117 For each @nt<statement_identifier>,
118 there is an implicit declaration (with the specified @nt<identifier>)
119 at the end of the @nt{declarative_part} of the
120 innermost @nt{block_statement} or body that
121 encloses the @nt{statement_identifier}.
122 The implicit declarations occur in the same order as the
123 @nt<statement_identifier>s occur in the source text.
124 If a usage name denotes such an implicit declaration, the entity it
125 denotes is the @nt<label>, @nt<loop_statement>,
126 or @nt<block_statement> with the given @nt<statement_identifier>.
127 @begin{Reason}
128 We talk in terms of individual @nt<statement_identifier>s here
129 rather than in terms of the corresponding statements, since
130 a given @nt{statement} may have multiple @nt<statement_identifier>s.
131
132 A @nt{block_statement} that has no
133 explicit @nt{declarative_part} has an implicit empty
134 @nt{declarative_part},
135 so this rule can safely
136 refer to the @nt{declarative_part} of a @nt<block_statement>.
137
138 The scope of a declaration starts at the place of the declaration
139 itself (see @RefSecNum{Scope of Declarations}).
140 In the case of a label, loop, or block name, it
141 follows from this rule that the scope of the implicit declaration
142 starts before the first explicit occurrence of the corresponding
143 name, since this occurrence is either in a statement label, a
144 @nt{loop_statement}, a @nt{block_statement}, or a
145 @nt{goto_statement}. An implicit
146 declaration in a @nt{block_statement} may hide a declaration given in an
147 outer program unit or @nt{block_statement} (according to the usual rules
148 of hiding explained in @RefSecNum{Visibility}).
149
150 The syntax rule for @nt{label} uses @nt{statement_identifier} which
151 is a @nt<direct_name> (not a @nt{defining_identifier}),
152 because labels are implicitly declared. The same applies to loop and
153 block names.
154 In other words, the @nt{label} itself is not the defining occurrence;
155 the implicit declaration is.
156
157 @Leading@;We cannot consider the @nt{label} to be a defining occurrence.
158 An example that can tell the difference is this:
159 @begin{example}
160 @key[declare]
161 --@RI{ Label Foo is implicitly declared here.}
162 @key[begin]
163 @key[for] Foo @key[in] ... @key[loop]
164 ...
165 <<Foo>> --@RI{ Illegal.}
166 ...
167 @key[end] @key[loop];
168 @key[end];
169 @end{example}
170
171 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
172 The label in this example is hidden from itself by the loop parameter
173 with the same name;
174 the example is illegal.
175 We considered creating a new syntactic category name, separate from
176 @nt{direct_name} and @nt{selector_name}, for use in the case of statement
177 labels.
178 However, that would confuse the rules in @Chg{Version=[3],New=[Clause],Old=[Section]}
179 8, so we didn't do it.
180 @end{Reason}
181
182 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0179-1]}
183 @ChgAdded{Version=[3],Text=[If one or more @nt{label}s end a
184 @nt{sequence_of_statements}, an implicit @nt{null_statement}
185 follows the @nt{label}s before any following constructs.]}
186
187 @begin{Reason}
188 @ChgRef{Version=[3],Kind=[Added]}
189 @ChgAdded{Version=[3],Text=[The semantics of a @nt{goto_statement} is
190 defined in terms of the statement having (following) that label. Thus
191 we ensure that every label has a following statement, which might be
192 implicit.]}
193 @end{Reason}
194 @end{StaticSem}
195
196 @begin{RunTime}
197 @PDefn2{Term=[execution], Sec=(null_statement)}
198 The execution of a @nt{null_statement} has no effect.
199
200 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
201 @Defn{transfer of control}
202 A @i{transfer of control} is
203 the run-time action of an @nt{exit_statement},
204 @Chg{Version=[2],New=[return statement],Old=[@nt{return_statement}]},
205 @nt{goto_statement},
206 or @nt{requeue_statement},
207 selection of a @nt{terminate_alternative},
208 raising of an exception,
209 or an abort,
210 which causes
211 the next action performed to be one other than what would normally be
212 expected from the other rules of the language.
213 @Redundant[As explained in
214 @RefSecNum{Completion and Finalization},
215 a transfer of control can cause the execution of constructs to be
216 completed and then left,
217 which may trigger finalization.]
218
219 @PDefn2{Term=[execution], Sec=(sequence_of_statements)}
220 The execution of a @nt{sequence_of_statements} consists of the execution
221 of the individual @nt{statement}s in succession
222 until the @ntf{sequence_} is completed.
223 @begin{Ramification}
224 It could be completed by reaching the end of it,
225 or by a transfer of control.
226 @end{Ramification}
227
228 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
229 @ChgAdded{Version=[5],Text=[Within a parallel construct, if a transfer of
230 control out of the construct is initiated by one of the logical threads of
231 control, an attempt is made to @i<cancel>@Defn2{Term=[cancel],Sec=[a logical
232 thread]}@Defn2{Term=[cancel],Sec=[a parallel construct]} all other logical
233 threads of control initiated by the parallel construct. Once all other logical
234 threads of control of the construct either complete or are canceled, the
235 transfer of control occurs. If two or more logical threads of control of the
236 same construct initiate such a transfer of control concurrently, one of them is
237 chosen arbitrarily and the others are canceled.]}
238
239 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
240 @ChgAdded{Version=[5],Text=[When a logical thread of control is canceled, the
241 cancellation causes it to complete as though it had performed a transfer of
242 control to the point where it would have finished its execution. Such a
243 cancellation is deferred while the logical thread of control is executing within
244 an abort-deferred operation (see
245 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}), and may be
246 deferred further, but not past
247 a point where the logical thread initiates a new nested parallel construct or
248 reaches an exception handler that is outside such an abort-deferred operation.]}
249 @end{RunTime}
250
251 @begin{Bounded}
252 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
253 @ChgAdded{Version=[5],Text=[@PDefn2{Term=(bounded error),Sec=(cause)}
254 During the execution of a parallel construct, it is
255 a bounded error to invoke an operation that is potentially blocking (see
256 @RefSecNum{Intertask Communication}). Program_Error is raised if the error is
257 detected by the implementation; otherwise, the execution of the potentially
258 blocking operation might proceed normally, or it might result in the indefinite
259 blocking of some or all of the logical threads of control making up the current
260 task.@Defn2{Term=[Program_Error],Sec=(raised by detection of a bounded error)}]}
261 @end{Bounded}
262
263 @begin{Notes}
264 A @nt<statement_identifier> that appears immediately within
265 the declarative region of a
266 named @nt<loop_statement> or an @nt<accept_statement> is nevertheless
267 implicitly declared immediately within the declarative region
268 of the innermost enclosing body or @nt<block_statement>;
269 in other words, the expanded name for a named statement is
270 not affected by whether the statement occurs inside or outside
271 a named loop or an @nt<accept_statement> @em only nesting
272 within @nt<block_statement>s is relevant to the form of its
273 expanded name.
274 @begin{Discussion}
275 @Leading@keepnext@;Each comment in the following example gives the
276 expanded name associated with an entity declared in the task body:
277 @begin{Example}
278 @key(task body) Compute @key(is)
279 Sum : Integer := 0; --@RI[ Compute.Sum]
280 @key(begin)
281 Outer: --@RI[ Compute.Outer]
282 @key(for) I @key(in) 1..10 @key(loop) --@RI[ Compute.Outer.I]
283 Blk: --@RI[ Compute.Blk]
284 @key(declare)
285 Sum : Integer := 0; --@RI[ Compute.Blk.Sum]
286 @key(begin)
287 @key(accept) Ent(I : out Integer; J : in Integer) @key(do)
288 --@RI[ Compute.Ent.I, Compute.Ent.J]
289 Compute.Ent.I := Compute.Outer.I;
290 Inner: --@RI[ Compute.Blk.Inner]
291 @key(for) J @key(in) 1..10 @key(loop)
292 --@RI[ Compute.Blk.Inner.J]
293 Sum := Sum + Compute.Blk.Inner.J * Compute.Ent.J;
294 @key(end loop) Inner;
295 @key(end) Ent;
296 Compute.Sum := Compute.Sum + Compute.Blk.Sum;
297 @key(end) Blk;
298 @key(end loop) Outer;
299 Record_Result(Sum);
300 @key(end) Compute;
301 @end{Example}
302 @end{Discussion}
303 @end{Notes}
304
305 @begin{Examples}
306 @Leading@keepnext@i{Examples of labeled statements:}
307 @begin{Example}
308 <<Here>> <<Ici>> <<Aqui>> <<Hier>> @key[null];
309
310 <<After>> X := 1;
311 @end{Example}
312 @end{Examples}
313
314 @begin{Extend83}
315 @Defn{extensions to Ada 83}
316 The @nt{requeue_statement} is new.
317 @end{Extend83}
318
319 @begin{DiffWord83}
320 We define the syntactic category @nt<statement_identifier> to simplify
321 the description. It is used for labels, loop names, and block names.
322 We define the entity associated with the implicit declarations
323 of statement names.
324
325 Completion includes completion caused by a transfer of control,
326 although RM83-5.1(6) did not take this view.
327 @end{DiffWord83}
328
329 @begin{Extend95}
330 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
331 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
332 The @nt{extended_return_statement} is new (@nt{simple_return_statement}
333 is merely renamed).]}
334 @end{Extend95}
335
336 @begin{Extend2005}
337 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0179-1]}
338 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
339 A @nt{label} can end a @nt{sequence_of_statements},
340 eliminating the requirement for having an explicit @key[null]; statement
341 after an ending label (a common use).]}
342 @end{Extend2005}
343
344 @begin{Extend2012}
345 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
346 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
347 The definition of @ldquote@;parallel constructs@rdquote and the
348 @nt{parallel_block_statement} are new.]}
349 @end{Extend2012}
350
351
352 @LabeledClause{Assignment Statements}
353
354 @begin{Intro}
355 @Redundant[An @nt{assignment_statement}
356 replaces the current value of
357 a variable with the result of evaluating an
358 @nt<expression>.]
359 @end{Intro}
360
361 @begin{Syntax}
362 @Syn{lhs=<assignment_statement>,rhs="
363 @SynI{variable_}@Syn2{name} := @Syn2{expression};"}
364 @end{Syntax}
365
366 @begin{Intro}
367 The execution of an @nt<assignment_statement> includes
368 the evaluation of the @nt<expression> and the @i(assignment)
369 of the value of the @nt<expression> into the @i(target).
370 @RootDefn{assignment operation}
371 @IndexSee{Term=[assign], See=(assignment operation)}
372 @Redundant[An assignment operation (as opposed to
373 an @nt<assignment_@!statement>) is performed in other contexts
374 as well, including object initialization and by-copy parameter
375 passing.]
376 @Defn2{Term=[target], Sec=(of an assignment operation)}
377 @Defn2{Term=[target], Sec=(of an @nt{assignment_statement})}
378 The @i{target} of an assignment operation
379 is the view of the object to which a value is being assigned;
380 the target of an @nt{assignment_@!statement} is the variable denoted by
381 the @SynI{variable_}@nt{name}.
382 @begin{Discussion}
383 Don't confuse this notion of the @lquotes@;target@rquotes@; of an assignment
384 with the notion of the @lquotes@;target object@rquotes@; of an entry call or requeue.
385
386 Don't confuse the term @lquotes@;assignment operation@rquotes@; with the
387 @nt{assignment_statement}.
388 The assignment operation is just one part of the execution of an
389 @nt{assignment_statement}.
390 The assignment operation is also a part of the execution of various
391 other constructs; see @RefSec{Completion and Finalization} for a complete
392 list.
393 Note that when we say, @lquotes@;such-and-such is assigned to so-and-so@rquotes@;,
394 we mean that the assignment operation is being applied, and that
395 so-and-so is the target of the assignment operation.
396 @end{Discussion}
397 @end{Intro}
398
399 @begin{Resolution}
400 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
401 @PDefn2{Term=[expected type],
402 Sec=(assignment_statement variable_name)}
403 The @i(variable_)@nt<name> of an @nt<assignment_statement>
404 is expected to be of any @Chg{Version=[2],New=[],Old=[nonlimited ]}type.
405 @PDefn2{Term=[expected type],
406 Sec=(assignment_statement expression)}
407 The expected type for the @nt<expression> is
408 the type of the target.
409 @begin{ImplNote}
410 @Leading@keepnext@;An @nt<assignment_statement> as a whole is a "complete context,"
411 so if the @i{variable_}@nt<name> of an @nt<assignment_statement> is
412 overloaded, the @nt<expression> can be used to help disambiguate it.
413 For example:
414 @begin{Example}
415 @key[type] P1 @key[is access] R1;
416 @key[type] P2 @key[is access] R2;
417
418 @key[function] F return P1;
419 @key[function] F return P2;
420
421 X : R1;
422 @key[begin]
423 F.all := X; --@RI[ Right hand side helps resolve left hand side]
424 @end{Example}
425 @end{ImplNote}
426 @end{Resolution}
427
428 @begin{Legality}
429 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
430 The target @Redundant[denoted by the
431 @i(variable_)@nt<name>] shall be a variable@Chg{Version=[2],New=[ of a
432 nonlimited type],Old=[]}.@Defn2{Term=[variable],Sec=(required)}@Defn2{Term=[object],Sec=(required)}
433
434 If the target is of a tagged class-wide type @i(T)'Class, then
435 the @nt<expression> shall either be dynamically tagged,
436 or of type @i(T) and tag-indeterminate
437 (see @RefSecNum{Dispatching Operations of Tagged Types}).
438 @begin{Reason}
439 This is consistent with the general rule that a single
440 dispatching operation shall not have both dynamically tagged and
441 statically tagged operands. Note that for an object
442 initialization (as opposed to the @nt{assignment_statement}),
443 a statically tagged initialization expression is permitted,
444 since there is no chance for confusion (or Tag_Check failure).
445 Also, in an object initialization, tag-indeterminate expressions
446 of any type covered by @i(T)'Class would be allowed, but with
447 an @nt{assignment_statement}, that might not work if the tag of the target
448 was for a type that didn't have one of the dispatching operations
449 in the tag-indeterminate expression.
450 @end{Reason}
451 @end{Legality}
452
453 @begin{RunTime}
454 @PDefn2{Term=[execution], Sec=(assignment_statement)}
455 For the execution of an @nt{assignment_statement},
456 the @i(variable_)@nt<name> and the @nt<expression>
457 are first evaluated in an arbitrary order.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
458 @begin{Ramification}
459 Other rules of the language may require that the
460 bounds of the variable be determined prior to evaluating
461 the @nt<expression>, but that does not necessarily require
462 evaluation of the @i(variable_)@nt<name>, as pointed out by the ACID.
463 @end{Ramification}
464
465 @Leading@keepnext@;When the type of the target is class-wide:
466 @begin(itemize)
467 @PDefn2{Term=[controlling tag value], Sec=(for the @nt{expression} in an @nt{assignment_statement})}
468 If the @nt<expression> is tag-indeterminate
469 (see @RefSecNum{Dispatching Operations of Tagged Types}), then the controlling
470 tag value for the @nt<expression> is the tag of the target;
471 @begin{Ramification}
472 See @RefSec(Dispatching Operations of Tagged Types).
473 @end{Ramification}
474
475 @IndexCheck{Tag_Check}
476 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of runtime check)}
477 Otherwise @Redundant[(the @nt<expression> is dynamically tagged)],
478 a check is made that the tag of
479 the value of the @nt<expression>
480 is the same as that of the target;
481 if this check fails, Constraint_Error is raised.
482 @end(itemize)
483
484 The value of the @nt<expression> is converted to the subtype of the
485 target. @Redundant[The conversion might raise an exception
486 (see @RefSecNum{Type Conversions}).]
487 @PDefn2{Term=[implicit subtype conversion],Sec=(assignment_statement)}
488 @begin{Ramification}
489 @RefSec(Type Conversions) defines what actions
490 and checks are associated with subtype conversion.
491 For non-array subtypes, it is just a constraint
492 check presuming the types match.
493 For array subtypes, it checks the lengths and slides if the
494 target is constrained.
495 @lquotes@;Sliding@rquotes@; means the array doesn't have to have the same bounds,
496 so long as it is the same length.
497 @end{Ramification}
498
499 In cases involving controlled types, the target is finalized,
500 and an anonymous object might be used as an intermediate in the assignment,
501 as described in @RefSec{Completion and Finalization}.
502 @Defn{assignment operation}
503 @Defn2{Term=[assignment operation],
504 Sec=(during execution of an @nt{assignment_statement})}
505 In any case,
506 the converted value of the @nt<expression> is then @i(assigned) to the target,
507 which consists of the following two steps:
508 @begin{Honest}
509 @RefSecNum{Completion and Finalization} actually says that
510 finalization happens always, but unless controlled types are involved,
511 this finalization during an @nt{assignment_statement} does
512 nothing.
513 @end{Honest}
514 @begin(itemize)
515 The value of the target becomes the converted value.
516
517 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
518 If any part of the target is controlled, its value
519 is adjusted as explained in @Chg{Version=[3],New=[subclause],Old=[clause]}
520 @RefSecNum{Assignment and Finalization}.
521 @PDefn2{Term=[adjustment], Sec=(as part of assignment)}
522 @begin{Ramification}
523 If any parts of the object are controlled,
524 abort is deferred during the assignment operation itself,
525 but not during the rest of the execution of an
526 @nt<assignment_statement>.
527 @end{Ramification}
528 @end(itemize)
529
530 @end{RunTime}
531
532 @begin{Notes}
533 The tag of an object never changes;
534 in particular, an
535 @nt{assignment_statement}
536 does not change the tag of the target.
537
538 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00363-01]}
539 @ChgDeleted{Version=[2],Text=[The values of the discriminants of an object
540 designated by an access value cannot be changed (not even by assigning a
541 complete value to the object itself) since such objects are always constrained;
542 however, subcomponents of such objects may be unconstrained.]}
543 @begin{Ramification}
544 The implicit subtype conversion described above for
545 @nt{assignment_statement}s
546 is performed only for the value of the right-hand side
547 expression as a whole; it is not performed for subcomponents of the
548 value.
549
550 The determination of the type of the variable of an
551 @nt{assignment_statement} may require consideration of the expression
552 if the variable
553 name can be interpreted as the name of a variable designated by the
554 access value returned by a function call, and similarly, as a
555 component or slice of such a variable
556 (see @RefSec{The Context of Overload Resolution}).
557 @end{Ramification}
558 @end{Notes}
559
560 @begin{Examples}
561 @Leading@keepnext@i{Examples of assignment statements:}
562 @begin{Example}
563 Value := Max_Value - 1;
564 Shade := Blue;
565
566 Next_Frame(F)(M, N) := 2.5; --@RI{ see @RefSecNum{Indexed Components}}
567 U := Dot_Product(V, W); --@RI{ see @RefSecNum{Subprogram Bodies}}
568
569 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0056-1]}
570 Writer := (Status => Open, Unit => Printer, Line_Count => 60); --@RI{ see @RefSecNum{Variant Parts and Discrete Choices}}
571 @Chg{Version=[4],New=[Next],Old=[Next_Car]}.@key[all] := (72074, @key[null]@Chg{Version=[4],New=[, Head],Old=[]});@Chg{Version=[4],New=[],Old=[ ]} --@RI{ see @RefSecNum{Incomplete Type Declarations}}
572 @end{Example}
573
574 @begin{WideAbove}
575 @Leading@keepnext@i{Examples involving scalar subtype conversions:}
576 @end{WideAbove}
577 @begin{Example}
578 I, J : Integer @key[range] 1 .. 10 := 5;
579 K : Integer @key[range] 1 .. 20 := 15;
580 ...
581
582 I := J; --@RI{ identical ranges}
583 K := J; --@RI{ compatible ranges}
584 J := K; --@RI{ will raise Constraint_Error if K > 10}
585 @end{Example}
586
587 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
588 @begin{WideAbove}
589 @leading@keepnext@i{Examples involving array subtype conversions:}
590 @end{WideAbove}
591 @begin{Example}
592 A : String(1 .. 31);
593 B : String(3 .. 33);
594 ...
595
596 A := B; --@RI{ same number of components}
597
598 A(1 .. 9) := "tar sauce";
599 A(4 .. 12) := A(1 .. 9); --@RI{ A(1 .. 12) = "tartar sauce"}
600 @end{Example}
601 @end{Examples}
602
603 @begin{Notes}
604 @i{Notes on the examples:}
605 @nt{Assignment_statement}s are allowed even in the case of overlapping
606 slices of the same array,
607 because the @SynI{variable_}@nt{name} and @nt{expression}
608 are both evaluated before copying the value into the variable.
609 In the above example, an
610 implementation yielding A(1 .. 12) = "tartartartar" would be
611 incorrect.
612 @end{Notes}
613
614 @begin{Extend83}
615 @Defn{extensions to Ada 83}
616 We now allow user-defined finalization and value adjustment actions
617 as part of @nt{assignment_statement}s
618 (see @RefSec{Assignment and Finalization}).
619 @end{Extend83}
620
621 @begin{DiffWord83}
622 The special case of array assignment is subsumed by the concept
623 of a subtype conversion, which is applied for all kinds of types,
624 not just arrays. For arrays it provides @lquotes@;sliding@rquotes@;. For numeric
625 types it provides conversion of a value of a universal type to
626 the specific type of the target. For other types,
627 it generally has no run-time effect, other than a constraint
628 check.
629
630 We now cover in a general way in @RefSecNum{Operations of Discriminated Types}
631 the erroneous execution possible due to
632 changing the value of a discriminant when
633 the variable in an @nt<assignment_statement> is a subcomponent
634 that depends on discriminants.
635 @end{DiffWord83}
636
637 @begin{Incompatible95}
638 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
639 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
640 The change of the limited check from a resolution rule to
641 a legality rule is not quite upward compatible. For example]}
642 @begin{Example}
643 @ChgRef{Version=[2],Kind=[AddedNormal]}
644 @ChgAdded{Version=[2],Text=[@key{type} AccNonLim @key{is} @key{access} NonLim;
645 @key{function} Foo (Arg : in Integer) @key{return} AccNonLim;
646 @key{type} AccLim @key{is} @key{access} Lim;
647 @key{function} Foo (Arg : in Integer) @key{return} AccLim;
648 Foo(2).@key{all} := Foo(1).@key{all};]}
649 @end{Example}
650 @ChgRef{Version=[2],Kind=[AddedNormal]}
651 @ChgAdded{Version=[2],Text=[where NonLim is a nonlimited type and Lim is a
652 limited type. The assignment is legal in Ada 95 (only the first Foo would be
653 considered), and is ambiguous in Ada 2005. We made the change because we want
654 limited types to be as similar to nonlimited types as possible. Limited
655 expressions are now allowed in all other contexts (with a similar
656 incompatibility), and it would be odd if assignments had different resolution
657 rules (which would eliminate ambiguities in some cases). Moreover, examples
658 like this one are rare, as they depend on assigning into overloaded function
659 calls.]}
660 @end{Incompatible95}
661
662
663
664 @LabeledAddedSubClause{Version=[5],Name=[Target Name Symbols]}
665
666 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3]}
667 @ChgAdded{Version=[5],Text=[@@, known as the @i<target name> of an assignment
668 statement, provides an abbreviation to avoid repetition of potentially long
669 names in assignment statements.@Defn{target name}]}
670
671 @begin{Syntax}
672 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3]}
673 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<target_name>,Old=<>}>,
674 rhs="@Chg{Version=[5],New=<@@>,Old=<>}"}
675 @end{Syntax}
676
677 @begin{Resolution}
678 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3]}
679 @ChgAdded{Version=[5],Text=[@Redundant[If a @nt{target_name} occurs in an
680 @nt{assignment_statement} @i<A>, the @SynI<variable_>@nt{name} @i<V> of @i<A>
681 is a complete context. The target name is a
682 constant view of @i<V>, having the nominal subtype of @i<V>.]]}
683
684 @begin{TheProof}
685 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3],ARef=[AI12-0322-1]}
686 @ChgAdded{Version=[5],Text=[The complete context rule is formally given in
687 @RefSecNum{The Context of Overload Resolution}. The constant view rule is
688 formally given in @RefSecNum{Objects and Named Numbers}; the nominal subtype
689 is a property taken from the target object as described below
690 in @RunTimeTitle.]}
691 @end{TheProof}
692 @end{Resolution}
693
694 @begin{Legality}
695 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3],ARef=[AI12-0322-1]}
696 @ChgAdded{Version=[5],Text=[A @nt{target_name} shall appear only in the
697 @nt{expression} of an @nt{assignment_statement}.]}
698 @end{Legality}
699
700 @begin{RunTime}
701 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3],ARef=[AI12-0322-1]}
702 @ChgAdded{Version=[5],Text=[For the execution of an @nt{assignment_statement}
703 with one or more @nt{target_name}s appearing in its @nt{expression}, the
704 @SynI{variable_}@nt{name} @i{V} of the @nt{assignment_statement}
705 is evaluated first to determine the object denoted by @i{V}, and then the
706 @nt{expression} of the @nt{assignment_statement} is evaluated with the evaluation of
707 each @nt{target_name} yielding a constant view of the the target whose
708 properties are otherwise identical to those of the view provided by @i{V}. The
709 remainder of the execution of the @nt{assignment_statement} is as given in
710 subclause @RefSecNum{Assignment Statements}.]}
711
712 @begin{Honest}
713 @ChgRef{Version=[5],Kind=[AddedNormal]}
714 @ChgAdded{Version=[5],Text=[The properties here include static properties
715 like whether the @nt{target_name} is aliased and the nominal subtype of
716 the @nt{target_name}. It was too weird to give separate rules for static
717 and dynamic properties that said almost the same thing.]}
718 @end{Honest}
719
720 @begin{Ramification}
721 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0322-1]}
722 @ChgAdded{Version=[5],Text=[Use of a @nt{target_name} can be erroneous if the
723 @SynI{variable_}@nt{name} @i{V} is a discriminant-dependent component, and
724 some other constituent of the @nt{expression} modifies the discriminant
725 governing the component @i<V>. The assignment probably would be erroneous
726 anyway, but the use of a @nt{target_name} eliminates the possibility that
727 a later evaluation of @i<V> raises an exception before any erroneous
728 execution occurs. See @RefSecNum{Operations of Discriminated Types}.]}
729 @end{Ramification}
730 @end{RunTime}
731
732 @begin{Examples}
733 @begin{Example}
734 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3],ARef=[AI12-0379-1]}
735 @ChgAdded{Version=[5],Text=[Board(1, 1) := @@ + 1.0; -- @Examcom<An abbreviation for Board(1, 1) := Board(1, 1) + 1.0;>
736 -- @Examcom<(Board is declared in @RefSecNum{Index Constraints and Discrete Ranges}).>]}
737
738 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3]}
739 @ChgAdded{Version=[5],Text=[My_Complex_Array : @key[array] (1 .. Max) @key[of] Complex; -- @Examcom<See @RefSecNum{Number Declarations}, @RefSecNum{Record Types}.>
740 ...
741 -- @Examcom<Square the element in the Count (see @RefSecNum{Object Declarations}) position:>
742 My_Complex_Array (Count) := (Re => @@.Re**2 - @@.Im**2,
743 Im => 2.0 * @@.Re * @@.Im);
744 -- @Examcom<A target_name can be used multiple times and as a prefix if needed.>]}
745 @end{Example}
746 @end{Examples}
747
748 @begin{Extend2012}
749 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0125-3],ARef=[AI12-0322-1]}
750 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
751 The target name symbol @@ is new.]}
752 @end{Extend2012}
753
754
755 @LabeledClause{If Statements}
756
757 @begin{Intro}
758 @Redundant[An @nt{if_statement} selects for execution at most one of
759 the enclosed @ntf{sequences_of_statements}, depending on the (truth)
760 value of one or more corresponding @nt{condition}s.]
761 @end{Intro}
762
763 @begin{Syntax}
764 @Syn{lhs=<if_statement>,rhs="
765 @key{if} @Syn2{condition} @key{then}
766 @Syn2{sequence_of_statements}
767 {@key{elsif} @Syn2{condition} @key{then}
768 @Syn2{sequence_of_statements}}
769 [@key{else}
770 @Syn2{sequence_of_statements}]
771 @key{end} @key{if};"}
772
773
774 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0147-1]}
775 @DeletedSyn{Version=[3],LHS=<@Chg{Version=[3],New=[],Old=[condition]}>,
776 RHS="@Chg{Version=[3],New=[],Old=[@SynI{boolean_}@Syn2{expression}]}"}
777 @end{Syntax}
778
779 @begin{Resolution}
780 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0147-1]}
781 @ChgDeleted{Version=[3],Text=[@PDefn2{Term=[expected type], Sec=(condition)}
782 A @nt{condition} is expected to be of any boolean type.]}
783 @end{Resolution}
784 @begin{NotIso}
785 @ChgAdded{Version=[3],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 3 and 4
786 were deleted.>}]}@Comment{This message should be
787 deleted if the paragraphs are ever renumbered.}
788 @end{NotIso}
789
790 @begin{RunTime}
791 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
792 @PDefn2{Term=[execution], Sec=(if_statement)}
793 For the execution of an @nt{if_statement}, the @nt{condition} specified
794 after @key{if}, and any @nt{condition}s specified after @key{elsif}, are
795 evaluated in succession (treating a final @key{else} as @key{elsif} True
796 @key{then}), until one evaluates to True or
797 all @nt{condition}s are evaluated and
798 yield False.
799 If a @nt{condition} evaluates to True, then the
800 corresponding @nt{sequence_of_statements} is executed;
801 otherwise@Chg{Version=[3],New=[,],Old=[]}
802 none of them is executed.
803 @begin{Ramification}
804 The part about all evaluating to False can't happen if
805 there is an @key{else}, since that is herein considered equivalent to
806 @key{elsif} True @key{then}.
807 @end{Ramification}
808 @end{RunTime}
809
810 @begin{Examples}
811 @Leading@keepnext@i{Examples of if statements:}
812 @begin{Example}
813 @key[if] Month = December @key[and] Day = 31 @key[then]
814 Month := January;
815 Day := 1;
816 Year := Year + 1;
817 @key[end] @key[if];
818
819 @key[if] Line_Too_Short @key[then]
820 @key[raise] Layout_Error;
821 @key[elsif] Line_Full @key[then]
822 New_Line;
823 Put(Item);
824 @key[else]
825 Put(Item);
826 @key[end] @key[if];
827
828 @key[if] My_Car.Owner.Vehicle /= My_Car @key[then] --@RI{ see @RefSecNum{Incomplete Type Declarations}}
829 Report ("Incorrect data");
830 @key[end] @key[if];
831 @end{Example}
832 @end{Examples}
833
834 @begin{Diffword2005}
835 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0147-1]}
836 @ChgAdded{Version=[3],Text=[Moved definition of @nt{condition} to
837 @RefSecNum{Conditional Expressions} in order to eliminate a forward reference.]}
838 @end{Diffword2005}
839
840
841 @LabeledClause{Case Statements}
842
843 @begin{Intro}
844 @Redundant[A @nt{case_statement} selects for execution one of a
845 number of alternative @ntf{sequences_of_statements}; the chosen
846 alternative is defined by the value of an expression.]
847 @end{Intro}
848
849 @begin{Syntax}
850 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
851 @Syn{lhs=<case_statement>,rhs="
852 @key{case} @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@Syn2{expression} @key{is}
853 @Syn2{case_statement_alternative}
854 {@Syn2{case_statement_alternative}}
855 @key{end case};"}
856
857 @Syn{lhs=<case_statement_alternative>,rhs="
858 @key{when} @Syn2{discrete_choice_list} =>
859 @Syn2{sequence_of_statements}"}
860 @end{Syntax}
861
862 @begin{Resolution}
863 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
864 @Chg{Version=[3],New=[@PDefn2{Term=[expected type], Sec=(case_statement selecting_expression)}
865 @PDefn2{Term=[expected type], Sec=(selecting_expression case_statement)}],Old=[@PDefn2{Term=[expected type], Sec=(case expression)}]}
866 The @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
867 expected to be of any discrete type.
868 @PDefn2{Term=[expected type],
869 Sec=(case_statement_alternative discrete_choice)}
870 The expected type for each @nt{discrete_choice} is the type of the
871 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}.
872 @end{Resolution}
873
874 @begin{Legality}
875 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
876 The @Chg{Version=[3],New=[@nt{choice_expression}s,
877 @nt{subtype_indication}s,],Old=[@nt{expression}s]} and
878 @Chg{Version=[3],New=[@nt{range}s],Old=[@nt{discrete_range}s]} given as
879 @nt{discrete_choice}s of a @nt{case_statement} shall be static.
880 @Redundant[A @nt{discrete_choice} @key(others), if present,
881 shall appear alone and in the last @nt{discrete_choice_list}.]
882
883 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1],ARef=[AI05-0240-1]}
884 The possible values of the
885 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} shall be
886 covered @Chg{Version=[3],New=[(see @RefSecNum{Variant Parts and Discrete Choices}) ],
887 Old=[]}as follows:
888 @begin{Discussion}
889 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0240-1]}
890 @ChgAdded{Version=[3],Text=[The meaning of "covered" here and in the
891 following rules is that of the term "cover a value" that is defined in
892 @RefSecNum{Variant Parts and Discrete Choices}.]}
893 @end{Discussion}
894 @begin{itemize}
895 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0003-1],ARef=[AI05-0153-3],ARef=[AI05-0188-1],ARef=[AI05-0262-1]}
896 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
897 If the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is a @nt{name}
898 @Redundant[(including a
899 @nt<type_conversion>@Chg{Version=[3],New=[, @nt{qualified_expression},],Old=[]}
900 or @Chg{Version=[3],New=[],Old=[a ]}@nt<function_call>)] having
901 a static and constrained nominal subtype,@Chg{Version=[3],New=[],Old=[ or
902 is a @nt{qualified_expression} whose
903 @nt{subtype_mark} denotes a static and constrained
904 scalar subtype,]}
905 then each non-@key{others} @nt{discrete_choice} shall cover only values in
906 that subtype@Chg{Version=[3],New=[ that satisfy its
907 @Chg{Version=[4],New=[predicates],Old=[predicate]} (see
908 @RefSecNum{Subtype Predicates})],Old=[]},
909 and each value of that subtype@Chg{Version=[3],New=[ that satisfies its
910 @Chg{Version=[4],New=[predicates],Old=[predicate]}],Old=[]} shall be
911 covered by some @nt{discrete_choice}
912 @Redundant[(either explicitly or by @key(others))].
913 @begin{Ramification}
914 Although not official @nt<name>s of objects, a value conversion
915 still has a defined nominal subtype, namely its target subtype.
916 See @RefSecNum{Type Conversions}.
917 @end{Ramification}
918
919 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
920 If the type of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
921 @i(root_integer), @i(universal_integer), or a descendant of a
922 formal scalar type,
923 then the @nt{case_statement} shall have an @key{others}
924 @nt{discrete_choice}.
925 @begin{Reason}
926 This is because the base range is
927 implementation defined for @i(root_integer) and @i(universal_integer),
928 and not known statically in the case of a formal scalar type.
929 @end{Reason}
930
931 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
932 Otherwise, each value of the base range of the type of the
933 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} shall
934 be covered
935 @Redundant[(either explicitly or by @key(others))].
936 @end{itemize}
937
938 Two distinct @nt{discrete_choice}s of a
939 @nt{case_statement} shall not cover the same value.
940 @begin{Ramification}
941 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
942 The goal of these coverage rules is
943 that any possible value of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} of a
944 @nt{case_statement} should be covered by exactly one
945 @nt{discrete_choice} of the
946 @nt{case_statement}, and that this should be checked at compile time.
947 The goal is achieved in most cases, but there are two minor
948 loopholes:
949 @begin{Itemize}
950 If the expression reads an object with an invalid representation
951 (e.g. an uninitialized object),
952 then the value can be outside the covered range.
953 This can happen for static constrained subtypes, as well as nonstatic or
954 unconstrained subtypes.
955 It cannot, however, happen if the @nt{case_statement} has the
956 @nt{discrete_choice} @key{others}, because @key{others} covers all values,
957 even those outside the subtype.
958
959 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
960 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
961 If the compiler chooses to represent the value of an expression of an
962 unconstrained subtype in a way that includes values outside the bounds of the
963 subtype, then those values can be outside the covered range.
964 For example, if X: Integer := Integer'Last;, and the case @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
965 X+1, then the implementation might choose to produce the correct value, which
966 is outside the bounds of Integer.
967 (It might raise Constraint_Error instead.)
968 This case can only happen for nongeneric subtypes that are either
969 unconstrained or
970 non@Chg{Version=[2],New=[],Old=[-]}@ChgNote{Make spelling consistent}static
971 (or both).
972 It can only happen if there is no @key{others} @nt{discrete_choice}.
973 @end{Itemize}
974
975 In the uninitialized variable case, the value might be anything; hence, any
976 alternative can be chosen, or Constraint_Error can be raised. (We intend to
977 prevent, however, jumping to random memory locations and the like.)
978 In the out-of-range case, the behavior is more sensible: if there is an
979 @key{others}, then the implementation may choose to raise Constraint_Error
980 on the evaluation of the @nt{expression} (as usual), or it may choose
981 to correctly evaluate the @nt{expression} and therefore choose the
982 @key{others} alternative.
983 Otherwise (no @key{others}), Constraint_Error is raised either way @em on
984 the @nt{expression} evaluation, or for the @nt{case_statement} itself.
985
986 For an enumeration type with a discontiguous set of internal codes
987 (see @RefSecNum{Enumeration Representation Clauses}),
988 the only way to get values in between the proper values
989 is via an object with an invalid representation;
990 there is no @lquotes@;out-of-range@rquotes@; situation that can produce them.
991 @end{Ramification}
992 @end{Legality}
993
994 @begin{RunTime}
995 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
996 @PDefn2{Term=[execution], Sec=(case_statement)}
997 For the execution of a @nt{case_statement}@Chg{Version=[5],New=[,],Old=[]} the
998 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}
999 is first evaluated.
1000
1001 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
1002 If the value of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}
1003 is covered by the @nt{discrete_@!choice_@!list} of some
1004 @nt{case_@!statement_@!alternative}, then the
1005 @nt{sequence_of_@!statements} of the @ntf{_alternative} is executed.
1006
1007 @Defn2{Term=[Constraint_Error],Sec=(raised by case statement)}
1008 Otherwise (the value is not covered by any
1009 @nt{discrete_choice_list},
1010 perhaps due to being outside the base range),
1011 Constraint_Error is raised.
1012 @begin{Ramification}
1013
1014 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0005-1]}
1015 In this case, the value @Chg{Version=[5],New=[fails to satisfy its
1016 (static) predicate (possible when the predicate is disabled),],Old=[]}
1017 is outside the base range of its type,
1018 or is an invalid representation.
1019
1020 @end{Ramification}
1021
1022 @end{RunTime}
1023
1024 @begin{Notes}
1025 The execution of a @nt{case_statement} chooses one and only one
1026 alternative.
1027 Qualification of the expression of a @nt{case_statement} by a static
1028 subtype can often be used to limit the number of choices that need be
1029 given explicitly.
1030 @end{Notes}
1031
1032 @begin{Examples}
1033 @Leading@keepnext@i{Examples of case statements:}
1034 @begin{Example}
1035 @tabclear()@tabset(P22)
1036 @key[case] Sensor @key[is]
1037 @key[when] Elevation@\=> Record_Elevation(Sensor_Value);
1038 @key[when] Azimuth@\=> Record_Azimuth (Sensor_Value);
1039 @key[when] Distance@\=> Record_Distance (Sensor_Value);
1040 @key[when] @key[others]@\=> @key[null];
1041 @key[end] @key[case];
1042
1043 @tabclear()@tabset(P22)
1044 @key[case] Today @key[is]
1045 @key[when] Mon@\=> Compute_Initial_Balance;
1046 @key[when] Fri@\=> Compute_Closing_Balance;
1047 @key[when] Tue .. Thu@\=> Generate_Report(Today);
1048 @key[when] Sat .. Sun@\=> @key[null];
1049 @key[end] @key[case];
1050
1051 @tabclear()@tabset(P16)
1052 @key[case] Bin_Number(Count) @key[is]
1053 @key[when] 1@\=> Update_Bin(1);
1054 @key[when] 2@\=> Update_Bin(2);
1055 @key[when] 3 | 4@\=>
1056 Empty_Bin(1);
1057 Empty_Bin(2);
1058 @key[when] @key[others]@\=> @key[raise] Error;
1059 @key[end] @key[case];
1060 @end{Example}
1061 @end{Examples}
1062
1063 @begin{Incompatible83}
1064 @ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020}
1065 @Chg{New=[@Defn{incompatibilities with Ada 83}
1066 In Ada 95, @nt{function_call}s and @nt{type_conversion}s are @nt{name}s, whereas
1067 in Ada 83, they were @nt{expression}s. Therefore, if the @nt{expression} of a
1068 @nt{case_statement} is a @nt{function_call} or @nt{type_conversion}, and the
1069 result subtype is static, it is illegal to specify a choice outside the bounds
1070 of the subtype. For this case in Ada 83 choices only are required to be in the
1071 base range of the type.],Old=[]}
1072
1073 @ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020}
1074 @Chg{New=[In addition, the rule about which choices must be covered is
1075 unchanged in Ada 95. Therefore, for a @nt{case_statement} whose @nt{expression}
1076 is a @nt{function_call} or @nt{type_conversion}, Ada 83 required covering all
1077 choices in the base range, while Ada 95 only requires covering choices in the
1078 bounds of the subtype. If the @nt{case_statement} does not include an @key{others}
1079 @nt{discrete_choice}, then a legal Ada 83 @nt{case_statement} will be illegal
1080 in Ada 95 if the bounds of the subtype are different than the bounds of the
1081 base type.],Old=[]}
1082 @end{Incompatible83}
1083
1084 @begin{Extend83}
1085 @Defn{extensions to Ada 83}
1086 In Ada 83, the @nt{expression} in a @nt{case_statement} is not allowed to
1087 be of a generic formal type.
1088 This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice}
1089 is required instead.
1090
1091 In Ada 95, a function call is the name of an object;
1092 this was not true in Ada 83 (see @RefSec{Names}).
1093 This change makes the following @nt{case_statement} legal:
1094 @begin{Example}
1095 @key[subtype] S @key[is] Integer @key[range] 1..2;
1096 @key[function] F @key[return] S;
1097 @key[case] F @key[is]
1098 @key[when] 1 => ...;
1099 @key[when] 2 => ...;
1100 --@RI{ No @key{others} needed.}
1101 @key[end] @key[case];
1102 @end{Example}
1103
1104 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
1105 Note that the result subtype given in a function
1106 @nt{renaming_declaration} is ignored;
1107 for a @nt{case_statement} whose expression calls a such a function, the
1108 full coverage rules are checked using the result subtype of the original
1109 function.
1110 Note that predefined operators such as "+" have an unconstrained result
1111 subtype (see @RefSecNum{Logical Operators and Short-circuit Control Forms}).
1112 Note that generic formal functions do not have static result subtypes.
1113 Note that the result subtype of an inherited subprogram need not
1114 correspond to any @Chg{Version=[3],New=[nameable],Old=[namable]} subtype;
1115 there is still a perfectly good result subtype, though.
1116 @end{Extend83}
1117
1118 @begin{DiffWord83}
1119 Ada 83 forgot to say what happens for @lquotes@;legally@rquotes@; out-of-bounds values.
1120
1121 We take advantage of rules and terms (e.g. @i(cover a value))
1122 defined for @nt{discrete_choice}s and @nt{discrete_choice_list}s
1123 in @RefSec{Variant Parts and Discrete Choices}.
1124
1125 In the @ResolutionName for the case expression,
1126 we no longer need RM83-5.4(3)'s @lquotes@;which must be determinable
1127 independently of the context in which the expression occurs, but
1128 using the fact that the expression must be of a discrete type,@rquotes@;
1129 because the @nt{expression} is now a complete context.
1130 See @RefSec{The Context of Overload Resolution}.
1131
1132 Since @nt<type_conversion>s are now defined as @nt<name>s,
1133 their coverage rule is now covered under the general rule
1134 for @nt<name>s, rather than being separated out along with
1135 @nt<qualified_expression>s.
1136 @end{DiffWord83}
1137
1138
1139 @begin{DiffWord2005}
1140 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0003-1]}
1141 @ChgAdded{Version=[3],Text=[Rewording to reflect that
1142 a @nt{qualified_expression} is now a @nt{name}.]}
1143
1144 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0153-3]}
1145 @ChgAdded{Version=[3],Text=[Revised for changes to @nt{discrete_choice}s
1146 made to allow static predicates (see @RefSecNum{Subtype Predicates}) as
1147 case choices (see @RefSecNum{Variant Parts and Discrete Choices}).]}
1148
1149 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
1150 @ChgAdded{Version=[3],Text=[Added the @SynI{selecting_} prefix to
1151 make this wording consistent with @nt{case_expression}, and to clarify
1152 which @nt{expression} is being talked about in the wording.]}
1153 @end{Diffword2005}
1154
1155 @begin{DiffWord2012}
1156 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
1157 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of
1158 case coverage to use the new term "satisfies the predicates"
1159 (see @RefSecNum{Subtype Predicates}).]}
1160 @end{Diffword2012}
1161
1162
1163 @LabeledClause{Loop Statements}
1164
1165 @begin{Intro}
1166 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1]}
1167 @Redundant[A @nt{loop_statement} includes a
1168 @nt{sequence_of_statements} that is to be executed repeatedly,
1169 zero or more times@Chg{Version=[5],New=[ with the iterations
1170 running sequentially or concurrently with one another],Old=[]}.]
1171 @end{Intro}
1172
1173 @begin{Syntax}
1174 @Syn{lhs=<loop_statement>,rhs="
1175 [@SynI{loop_}@Syn2{statement_identifier}:]
1176 [@Syn2{iteration_scheme}] @key{loop}
1177 @Syn2{sequence_of_statements}
1178 @key{end} @key{loop} [@SynI{loop_}@Syn2{identifier}];"}
1179
1180
1181 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0139-2]}
1182 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1],ARef=[AI12-0189-1],ARef=[AI12-0251-1],ARef=[AI12-0266-1],ARef=[AI12-0326-2]}
1183 @Syn{lhs=<iteration_scheme>,rhs="@key{while} @Syn2{condition}
1184 | @key{for} @Syn2{loop_parameter_specification}@Chg{Version=[3],New=[
1185 | @key{for} @Syn2{iterator_specification}],Old=[]}@Chg{Version=[5],New=<
1186 | [@key[parallel]]
1187 @key{for} @Syn2{procedural_iterator}
1188 | @key[parallel] [(@Syn2{chunk_specification})]
1189 @key{for} @Syn2{loop_parameter_specification}
1190 | @key[parallel] [(@Syn2{chunk_specification})]
1191 @key{for} @Syn2{iterator_specification}>,Old=[]}"}
1192
1193 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1]}
1194 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<chunk_specification>,Old=<>}>,
1195 rhs="@Chg{Version=[5],New=<
1196 @SynI{integer_}@Syn2{simple_expression}
1197 | @Syn2{defining_identifier} @key[in] @Syn2{discrete_subtype_definition}>,Old=<>}"}
1198
1199 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0250-1]}
1200 @Syn{lhs=<loop_parameter_specification>,rhs="
1201 @Syn2{defining_identifier} @key{in} [@key{reverse}] @Syn2{discrete_subtype_definition}@Chg{Version=[5],New=<
1202 [@Syn2{iterator_filter}]>,Old=<>}"}
1203
1204 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0250-1]}
1205 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<iterator_filter>,Old=<>}>,
1206 rhs="@Chg{Version=[5],New=<@key[when] @Syn2{condition}>,Old=<>}"}
1207
1208 @begin(SyntaxText)
1209 If a @nt{loop_statement} has a @SynI{loop_}@nt{statement_identifier},
1210 then the @nt<identifier> shall be repeated after the @key{end loop};
1211 otherwise, there shall not be an @nt<identifier> after the @key{end loop}.
1212
1213 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0119-1]}
1214 @ChgAdded{Version=[5],Text=[An @nt{iteration_scheme} that begins with the reserved word
1215 @key[parallel] shall not have the reserved word @key[reverse] in its
1216 @nt{loop_parameter_specification}.]}
1217 @end(SyntaxText)
1218 @end{Syntax}
1219
1220 @begin{Resolution}
1221 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1]}
1222 @ChgAdded{Version=[5],Text=[In a @nt{chunk_specification} that is an
1223 @Syni{integer_}@nt{simple_expression}, the
1224 @Syni{integer_}@nt{simple_expression} is expected to be of any integer type.]}
1225 @end{Resolution}
1226
1227 @begin{StaticSem}
1228 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0061-1]}
1229 @Defn{loop parameter}
1230 A @nt{loop_parameter_specification} declares a @i{loop parameter},
1231 which is an object whose subtype@Chg{Version=[5],New=[ (and nominal subtype)],Old=[]}
1232 is that defined by the @nt{discrete_subtype_definition}.
1233 @IndexSeeAlso{Term=[parameter],See=[loop parameter]}
1234
1235 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1]}
1236 @ChgAdded{Version=[5],Text=[In a @nt{chunk_specification} that has a
1237 @nt{discrete_subtype_definition}, the @nt{chunk_specification} declares a
1238 @i{chunk parameter} object@Defn{chunk parameter} whose subtype (and nominal
1239 subtype) is that defined by the @nt{discrete_subtype_definition}.]}
1240 @end{StaticSem}
1241
1242 @begin{RunTime}
1243 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0250-1],ARef=[AI12-0327-1]}
1244 @ChgAdded{Version=[5],Text=[The @i<filter> of an @i{iterator construct}
1245 @Defn{iterator construct}@Defn2{Term=[filter],Sec={iterator construct}} (a
1246 @nt{loop_parameter_specification}, @nt{iterator_specification}, or
1247 @nt{procedural_iterator}) is defined to be
1248 @i{satisfied}@Defn2{Term=[satisfied],Sec=[filter]} when there is no
1249 @nt{iterator_filter} for the iterator construct, or when the @nt{condition}
1250 of the @nt{iterator_filter} evaluates to True for a given iteration of the
1251 iterator construct.@Defn{iterator filter}]}
1252
1253 @ChgToGlossary{Version=[5],Kind=[Added],Term=<Iterator Filter>,
1254 Text=<@ChgAdded{Version=[5],Text=[A construct that is used to restrict the
1255 elements produced by an iteration to those for which a boolean condition
1256 evaluates to True.]}>}
1257
1258 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0250-1],ARef=[AI12-0327-1]}
1259 @ChgAdded{Version=[5],Text=[If a @nt{sequence_of_statements} of a
1260 @nt{loop_statement} with an iterator construct is said to be
1261 @i{conditionally executed},@Defn{conditionally executed} then the @nt{statement}s
1262 are executed only when the filter of the iterator construct is satisfied.]}
1263
1264 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0250-1],ARef=[AI12-0327-1]}
1265 @ChgAdded{Version=[5],Text=[The loop iterators @nt{loop_parameter_specification}
1266 and @nt{iterator_specification} can also be used in contexts other than
1267 @nt{loop_statement}s (for example, see @RefSecNum{Container Aggregates} and
1268 @RefSecNum{Quantified Expressions}). In such a context,
1269 the iterator @i{conditionally produces}@Defn2{Term=[conditionally produces],
1270 Sec=[iteration]} values in the order specified for
1271 the associated construct below or in @RefSecNum{Generalized Loop Iteration}.
1272 The values produced are the values given to the loop parameter when the filter
1273 of the iterator construct is satisfied for that value. @Redundant[No value is
1274 produced when the @nt{condition} of an @nt{iterator_filter} evaluates to False.]]}
1275
1276 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1]}
1277 @PDefn2{Term=[execution], Sec=(loop_statement)}
1278 For the execution of a @nt{loop_statement},
1279 the @nt{sequence_of_statements} is executed@Chg{Version=[5],New=[],Old=[ repeatedly,]}
1280 zero or more times,
1281 until the @nt{loop_statement} is complete.
1282 The @nt{loop_statement} is complete when a transfer
1283 of control occurs that transfers control out of the loop, or, in the
1284 case of an @nt{iteration_scheme}, as specified below.
1285
1286 @PDefn2{Term=[execution],
1287 Sec=(loop_statement with a while iteration_scheme)}
1288 For the execution of a @nt{loop_statement} with a @key{while}
1289 @nt{iteration_scheme}, the condition is evaluated before each
1290 execution of the @nt{sequence_of_@!statements}; if the value of the
1291 @nt{condition} is True, the @nt{sequence_of_@!statements} is executed;
1292 if False, the execution of the @nt{loop_@!statement} is complete.
1293
1294 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1],ARef=[AI12-0294-1]}
1295 @ChgAdded{Version=[5],Text=[If the reserved word @key[parallel] is present
1296 in the @nt{iteration_scheme} of a @nt{loop_statement} (a
1297 @i{parallel loop}),@Defn{parallel loop}@Defn2{Term=[loop],Sec=[parallel]}
1298 the iterations are partitioned into one or more @Defn{chunk}@i{chunks}, each
1299 with its own separate logical thread of control (see
1300 clause @RefSecNum{Tasks and Synchronization}). If a @nt{chunk_specification}
1301 is present in a parallel loop, it is elaborated first, and the result of the
1302 elaboration determines the maximum number of chunks used for the parallel loop.
1303 @PDefn2{Term=[elaboration], Sec=(chunk_specification)}
1304 If the @nt{chunk_specification}
1305 is an @SynI{integer_}@nt{simple_expression}, the elaboration evaluates the
1306 expression, and the value of the expression determines the maximum number
1307 of chunks. If a @nt{discrete_subtype_definition} is present, the
1308 elaboration elaborates the @nt{discrete_subtype_definition}, which
1309 defines the subtype of the chunk parameter, and the number
1310 of values in this subtype determines the maximum number of chunks. After
1311 elaborating the @nt{chunk_specification}, a check is made that the determined
1312 maximum number of chunks is greater than zero. If
1313 this check fails, Program_Error is raised.@IndexCheck{Program_Error_Check}
1314 @Defn2{Term=[Program_Error],Sec=(raised by failure of runtime check)}]}
1315
1316 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0139-2],ARef=[AI05-0262-1]}
1317 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
1318 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0119-1],ARef=[AI12-0250-1],ARef=[AI12-0251-1],ARef=[AI12-0294-1]}
1319 @Chg{Version=[5],New=[@PDefn2{Term=[execution],
1320 Sec=(loop_statement with a for loop_parameter_specification)}],
1321 Old=[@PDefn2{Term=[execution],
1322 Sec=(loop_statement with a for iteration_scheme)}]}
1323 @PDefn2{Term=[elaboration], Sec=(loop_parameter_specification)}
1324 For the execution of a @nt{loop_statement} @Chg{Version=[5],New=[that has an],
1325 Old=[with @Chg{Version=[3],New=[the],Old=[a @key{for}]}]}
1326 @nt{iteration_scheme}@Chg{Version=[3],New=[ @Chg{Version=[5],New=[including
1327 a],Old=[being @key[for]]} @nt{loop_@!parameter_@!specification}],Old=[]},
1328 @Chg{Version=[5],New=[after
1329 elaborating the @nt{chunk_specification}, if any, ],Old=[]}the
1330 @nt{loop_@!parameter_@!specification} is
1331 @Chg{Version=[5],New=[],Old=[first ]}elaborated. This
1332 @Chg{Version=[5],New=[],Old=[elaboration creates the loop parameter
1333 and ]}elaborates the @nt{discrete_@!subtype_@!definition}@Chg{Version=[5],New=[,
1334 which defines the subtype of the loop parameter],Old=[]}.
1335 If the @nt{discrete_@!subtype_@!definition} defines a subtype
1336 with a null range,
1337 the execution of the
1338 @nt{loop_statement} is complete. Otherwise, the
1339 @nt{sequence_of_@!statements} is@Chg{Version=[5],New=[ conditionally],Old=[]}
1340 executed once for each value of the discrete subtype defined by the
1341 @nt{discrete_@!subtype_@!definition}
1342 @Chg{Version=[3],New=[that satisfies the
1343 @Chg{Version=[4],New=[predicates],Old=[predicate]} of the subtype ],Old=[]}(or
1344 until the loop is left as a consequence of a transfer of control).
1345 @Defn2{Term=[assignment operation], Sec=(during execution of a @key{for} loop)}
1346 Prior to each such iteration,
1347 the corresponding value of the discrete subtype is assigned to the
1348 loop parameter@Chg{Version=[5],New=[ associated with the given iteration.
1349 If the loop is a parallel loop,
1350 each chunk has its own logical thread of control with its own copy of the loop
1351 parameter; otherwise (a @i<sequential loop>)@Defn{sequential loop}@Defn2{Term=[loop],Sec=[sequential]},
1352 a single logical thread of control performs the loop, and there is a single copy
1353 of the loop parameter. Each logical thread of control handles a distinct
1354 subrange of the values of the subtype of the loop parameter such that
1355 all values are covered with no overlaps.
1356 Within each logical thread of control, the],Old=[. These]}
1357 values are assigned @Chg{Version=[5],New=[to the loop parameter ],Old=[]}in
1358 increasing order unless the reserved word @key{reverse} is present, in which
1359 case the values are assigned in decreasing order.
1360 @begin{Honest}
1361 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0294-1]}
1362 @ChgAdded{Version=[5],Text=[This wording does not describe when the loop
1363 parameter
1364 object(s) are created. That creation has no side-effects (other than
1365 possibly raising Storage_Error, but anything can do that), so we
1366 simplified the wording by leaving it out. Each object has to be created
1367 before any iteration that depends on it starts, but we do not (for
1368 instance) require that the objects are all created at once at the start
1369 of the loop, nor that the objects are created after the elaboration of the
1370 @nt{discrete_subtype_definition}.]}
1371 @end{Honest}
1372 @begin{Ramification}
1373 @ChgRef{Version=[5],Kind=[Deleted],ARef=[AI12-0294-1]}
1374 @ChgDeleted{Version=[5],Text=[The order of creating the loop parameter and
1375 evaluating the @nt{discrete_subtype_definition} doesn't matter, since the
1376 creation of the loop parameter has no side effects (other than possibly
1377 raising Storage_Error, but anything can do that).]}
1378
1379 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
1380 @ChgAdded{Version=[3],Text=[The predicate (if any) necessarily has to be a
1381 static predicate as a dynamic predicate is explicitly disallowed @em
1382 see @RefSecNum{Subtype Predicates}.]}
1383 @end{Ramification}
1384
1385 @begin{Reason}
1386 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
1387 @ChgAdded{Version=[3],Type=[Leading],Text=[If there is a predicate, the loop
1388 still visits the values in the order of the underlying base type; the order of
1389 the values in the predicate is irrelevant. This is the case so that the
1390 following loops have the same sequence of calls and parameters on procedure
1391 Call for any subtype S:]}
1392 @begin{Example}
1393 @ChgAdded{Version=[3],Text=[@key[for] I @key[in] S @key[loop]
1394 Call (I);
1395 @key[end loop];]}
1396
1397 @ChgAdded{Version=[3],Text=[@key[for] I @key[in] S'Base @key[loop]
1398 @key[if] I @key[in] S @key[then]
1399 Call (I);
1400 @key[end if];
1401 @key[end loop];]}
1402 @end{Example}
1403 @end{Reason}
1404
1405 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1],ARef=[AI12-0294-1]}
1406 @ChgAdded{Version=[5],Text=[If a @nt{chunk_specification} with a
1407 @nt{discrete_subtype_definition} is present, then the logical thread of control
1408 associated with a given chunk has its own copy of the chunk parameter
1409 initialized with a distinct value from the discrete subtype defined by the
1410 @nt{discrete_subtype_definition}. The values of the chunk parameters are
1411 assigned such that they increase with increasing values of the ranges covered
1412 by the corresponding loop parameters.]}
1413
1414 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0251-1]}
1415 @ChgAdded{Version=[5],Text=[Whether or not a @nt{chunk_specification}
1416 is present in a parallel loop,
1417 the total number of iterations of the loop represents an upper bound
1418 on the number of logical threads of control devoted to the loop.]}
1419
1420 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0262-1]}
1421 @ChgRef{Version=[5],Kind=[RevisedAdded],ARef=[AI12-0250-1],ARef=[AI12-0266-1]}
1422 @ChgAdded{Version=[3],Text=[@Redundant[For details about the execution of a
1423 @nt{loop_statement} with the @nt{iteration_scheme}
1424 @Chg{Version=[5],New=[including an],Old=[being @key[for]]}
1425 @nt{iterator_specification}, see @RefSecNum{Generalized Loop Iteration}.@Chg{Version=[5],New=[
1426 For details relating to a @nt{procedural_iterator}, see
1427 @RefSecNum{Procedural Iterators}.],Old=[]}]]}
1428
1429 @end{RunTime}
1430
1431 @begin{Notes}
1432 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0250-1]}
1433 A loop parameter@Chg{Version=[5],New=[ declared by a
1434 @nt{loop_parameter_specification}],Old=[]} is a constant;
1435 it cannot be updated within the
1436 @nt{sequence_of_statements} of the loop
1437 (see @RefSecNum{Objects and Named Numbers}).
1438
1439 An @nt{object_declaration} should not be given for a loop parameter,
1440 since the loop parameter is automatically declared by
1441 the @nt{loop_parameter_specification}.
1442 The scope of a loop parameter extends from the
1443 @nt{loop_parameter_specification} to the end of the
1444 @nt{loop_statement}, and the visibility
1445 rules are such that a loop parameter is only visible within the
1446 @nt{sequence_of_statements} of the loop.
1447 @begin{ImplNote}
1448 An implementation could give a warning if a variable is hidden by a
1449 @nt{loop_parameter_specification}.
1450 @end{ImplNote}
1451
1452 The @nt<discrete_subtype_definition> of a for loop is elaborated
1453 just once. Use of the
1454 reserved word @key[reverse] does not alter the discrete subtype defined,
1455 so that the following @nt{iteration_scheme}s are not equivalent; the
1456 first has a null range.
1457 @begin{Example}
1458 @key[for] J @key[in] @key[reverse] 1 .. 0
1459 @key[for] J @key[in] 0 .. 1
1460 @end{Example}
1461 @begin{Ramification}
1462 If a @nt{loop_parameter_specification} has a static discrete range,
1463 the subtype of the loop parameter is static.
1464 @end{Ramification}
1465 @end{Notes}
1466
1467 @begin{Examples}
1468 @Leading@keepnext@i{Example of a loop statement without an iteration scheme:}
1469 @begin{Example}
1470 @key[loop]
1471 Get(Current_Character);
1472 @key[exit] @key[when] Current_Character = '*';
1473 @key[end] @key[loop];
1474 @end{Example}
1475
1476 @begin{WideAbove}
1477 @leading@keepnext@i{Example of a loop statement with a @key[while] iteration scheme:}
1478 @end{WideAbove}
1479 @begin{Example}
1480 @key[while] Bid(N).Price < Cut_Off.Price @key[loop]
1481 Record_Bid(Bid(N).Price);
1482 N := N + 1;
1483 @key[end] @key[loop];
1484 @end{Example}
1485
1486 @begin{WideAbove}
1487 @leading@keepnext@i{Example of a loop statement with a @key[for] iteration scheme:}
1488 @end{WideAbove}
1489 @begin{Example}
1490 @key[for] J @key[in] Buffer'Range @key[loop] --@RI{ works even with a null range}
1491 @key[if] Buffer(J) /= Space @key[then]
1492 Put(Buffer(J));
1493 @key[end] @key[if];
1494 @key[end] @key[loop];
1495 @end{Example}
1496
1497 @begin{WideAbove}
1498 @leading@keepnext@i{Example of a loop statement with a name:}
1499 @end{WideAbove}
1500 @begin{Example}
1501 Summation:
1502 @key[while] Next /= Head @key[loop] --@RI{ see @RefSecNum{Incomplete Type Declarations}}
1503 Sum := Sum + Next.Value;
1504 Next := Next.Succ;
1505 @key[end] @key[loop] Summation;
1506 @end{Example}
1507
1508 @begin{WideAbove}
1509 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1],ARef=[AI12-0312-1]}
1510 @ChgAdded{Version=[5],Type=[Leading],KeepNext=[T],
1511 Text=[@i{Example of a simple parallel loop:}]}
1512 @end{WideAbove}
1513 @begin{Example}
1514 @ChgRef{Version=[5],Kind=[AddedNormal]}
1515 @ChgAdded{Version=[5],Text=[-- @ExamCom{see @RefSecNum{Array Types}}
1516 @key[parallel]
1517 @key[for] I @key[in] Grid'Range(1) @key[loop]
1518 Grid(I, 1) := (@key[for all] J @key[in] Grid'Range(2) => Grid(I,J) = True);
1519 @key[end loop];]}
1520 @end{Example}
1521
1522 @begin{WideAbove}
1523 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0312-1]}
1524 @ChgAdded{Version=[5],Type=[Leading],KeepNext=[T],
1525 Text=[@i{Example of a parallel loop with a chunk specification:}]}
1526 @end{WideAbove}
1527 @begin{Example}
1528 @ChgRef{Version=[5],Kind=[AddedNormal]}
1529 @ChgAdded{Version=[5],Text=[@key[declare]
1530 @key[subtype] Chunk_Number @key[is] Natural @key[range] 1 .. 8;]}
1531
1532 @ChgRef{Version=[5],Kind=[AddedNormal]}
1533 @ChgAdded{Version=[5],Text=[ Partial_Sum,
1534 Partial_Max : @key[array] (Chunk_Number) @key[of] Natural := (@key[others] => 0);
1535 Partial_Min : @key[array] (Chunk_Number) @key[of] Natural := (@key[others] => Natural'Last);]}
1536
1537 @ChgRef{Version=[5],Kind=[AddedNormal]}
1538 @ChgAdded{Version=[5],Text={@key[begin]
1539 @key[parallel] (Chunk @key[in] Chunk_Number)
1540 @key[for] I @key[in] Grid'Range(1) @key[loop]
1541 @key[declare]
1542 True_Count : @key[constant] Natural :=
1543 [@key[for] J @key[in] Grid'Range(2) => (@key[if] Grid (I, J) @key[then] 1 @key[else] 0)]'Reduce("+",0);
1544 @key[begin]
1545 Partial_Sum (Chunk) := @@ + True_Count;
1546 Partial_Min (Chunk) := Natural'Min(@@, True_Count);
1547 Partial_Max (Chunk) := Natural'Max(@@, True_Count);
1548 @key[end];
1549 @key[end loop];}}
1550
1551 @ChgRef{Version=[5],Kind=[AddedNormal]}
1552 @ChgAdded{Version=[5],Text=[ Put_Line("Total=" & Partial_Sum'Reduce("+", 0) &
1553 ", Min=" & Partial_Min'Reduce(Natural'Min, Natural'Last) &
1554 ", Max=" & Partial_Max'Reduce(Natural'Max, 0));
1555 @key[end];]}
1556
1557 @begin{WideAbove}
1558 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0312-1]}
1559 @ChgAdded{Version=[5],Text=[@i{For an example of an} @nt{iterator_filter}@i{,
1560 see @RefSecNum{Quantified Expressions}.}]}
1561 @end{WideAbove}
1562 @end{Example}
1563 @end{Examples}
1564
1565 @begin{DiffWord83}
1566 The constant-ness of loop parameters is specified in
1567 @RefSec{Objects and Named Numbers}.
1568 @end{DiffWord83}
1569
1570 @begin{DiffWord2005}
1571 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0262-1],ARef=[AI05-0299-1]}
1572 @ChgAdded{Version=[3],Text=[Generalized @nt{iterator_specification}s are
1573 allowed in @key[for] loops; these are documented as an extension in the
1574 appropriate subclause.]}
1575 @end{DiffWord2005}
1576
1577 @begin{Extend2012}
1578 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1],ARef=[AI12-0251-1],ARef=[AI12-0266-1],ARef=[AI12-0294-1]}
1579 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}Parallel loops
1580 are new.]}
1581
1582 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0250-1]}
1583 @ChgAdded{Version=[5],Text=[An @nt{iterator_filter} is now allowed on
1584 @nt{loop_parameter_specification}s. This is mainly for consistency with
1585 aggregate and reduction iterators, where it eliminates the need for
1586 temporary objects.]}
1587 @end{Extend2012}
1588
1589 @begin{DiffWord2012}
1590 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
1591 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of
1592 loop execution to use the new term "satisfies the predicates"
1593 (see @RefSecNum{Subtype Predicates}).]}
1594
1595 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0061-1]}
1596 @ChgAdded{Version=[5],Text=[Added text so that the nominal subtype of a
1597 loop parameter is clearly defined.]}
1598 @end{Diffword2012}
1599
1600
1601 @LabeledAddedSubClause{Version=[3],Name=[User-Defined Iterator Types]}
1602
1603 @begin{StaticSem}
1604
1605 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1606 @ChgAdded{Version=[3],Type=[Leading],Keepnext=[T],Text=[The following
1607 language-defined generic library package exists:]}
1608 @begin{Example}
1609 @ChgRef{Version=[3],Kind=[AddedNormal]}
1610 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0241-1]}
1611 @ChgAdded{Version=[3],Text=[@ChildUnit{Parent=[Ada],Child=[Iterator_Interfaces]}@key[generic]
1612 @key[type] Cursor;
1613 @key[with function] Has_Element (Position : Cursor) @key[return] Boolean;
1614 @key[package] Ada.Iterator_Interfaces@Chg{Version=[5],New=[],Old=[ @key[is]]}
1615 @Chg{Version=[5],New=[@key[with] Pure, Nonblocking => False @key[is]],Old=[@key[pragma] Pure (Iterator_Interfaces);]}]}
1616
1617 @ChgRef{Version=[3],Kind=[AddedNormal]}
1618 @ChgAdded{Version=[3],Text=[ @key[type] @AdaTypeDefn{Forward_Iterator} @key[is limited interface];
1619 @key[function] @AdaSubDefn{First} (Object : Forward_Iterator) @key[return] Cursor @key[is abstract];
1620 @key[function] @AdaSubDefn{Next} (Object : Forward_Iterator; Position : Cursor)
1621 @key[return] Cursor @key[is abstract];]}
1622
1623 @ChgRef{Version=[3],Kind=[AddedNormal]}
1624 @ChgAdded{Version=[3],Text=[ @key[type] @AdaTypeDefn{Reversible_Iterator} @key[is limited interface and] Forward_Iterator;
1625 @key[function] @AdaSubDefn{Last} (Object : Reversible_Iterator) @key[return] Cursor @key[is abstract];
1626 @key[function] @AdaSubDefn{Previous} (Object : Reversible_Iterator; Position : Cursor)
1627 @key[return] Cursor @key[is abstract];]}
1628
1629 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1630 @ChgAdded{Version=[5],Text=[ @key[type] @AdaTypeDefn{Parallel_Iterator} @key[is limited interface and] Forward_Iterator;]}
1631
1632 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1633 @ChgAdded{Version=[5],Text=[ @key[subtype] @AdaSubtypeDefn{Name=[Chunk_Index],Of=[Positive]} @key[is] Positive;]}
1634
1635 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1636 @ChgAdded{Version=[5],Text=[ @key[function] @AdaSubDefn{Is_Split} (Object : Parallel_Iterator)
1637 @key[return] Boolean @key[is abstract];]}
1638
1639 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1640 @ChgAdded{Version=[5],Text=[ @key[procedure] @AdaSubDefn{Split_Into_Chunks} (Object : @key[in out] Parallel_Iterator;
1641 Max_Chunks : @key[in] Chunk_Index) @key[is abstract]
1642 @key[with] Pre'Class => @key[not] Object.Is_Split @key[or else raise] Program_Error,
1643 Post'Class => Object.Is_Split @key[and then]
1644 Object.Chunk_Count <= Max_Chunks;]}
1645
1646 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1647 @ChgAdded{Version=[5],Text=[ @key[function] @AdaSubDefn{Chunk_Count} (Object : Parallel_Iterator)
1648 @key[return] Chunk_Index @key[is abstract]
1649 @key[with] Pre'Class => Object.Is_Split @key[or else raise] Program_Error;]}
1650
1651 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1652 @ChgAdded{Version=[5],Text=[ @key[function] @AdaSubDefn{First} (Object : Parallel_Iterator;
1653 Chunk : Chunk_Index) @key[return] Cursor @key[is abstract]
1654 @key[with] Pre'Class => (Object.Is_Split @key[and then]
1655 Chunk <= Object.Chunk_Count)
1656 @key[or else raise] Program_Error;]}
1657
1658 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1659 @ChgAdded{Version=[5],Text=[ @key[function] @AdaSubDefn{Next} (Object : Parallel_Iterator;
1660 Position : Cursor;
1661 Chunk : Chunk_Index) @key[return] Cursor @key[is abstract]
1662 @key[with] Pre'Class => (Object.Is_Split @key[and then]
1663 Chunk <= Object.Chunk_Count)
1664 @key[or else raise] Program_Error;]}
1665
1666 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1667 @ChgAdded{Version=[5],Text=[ @key[type] @AdaTypeDefn{Parallel_Reversible_Iterator} @key[is limited interface]
1668 @b[and] Parallel_Iterator @key[and] Reversible_Iterator;]}
1669
1670 @ChgRef{Version=[3],Kind=[AddedNormal]}
1671 @ChgAdded{Version=[3],Text=[@key[end] Ada.Iterator_Interfaces;]}
1672 @end{Example}
1673
1674 @begin{Reason}
1675 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0241-1]}
1676 @ChgAdded{Version=[5],Text=[This package must allow blocking
1677 (Nonblocking => False) for compatibility. The purpose of this package
1678 is to provide a template for overriding user-defined routines; and
1679 such routines can only allow blocking if the root type does so.
1680 Users can still declare their overridding routines nonblocking if they
1681 wish.]}
1682 @end{Reason}
1683
1684 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1685 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0266-1]}
1686 @ChgAdded{Version=[3],Text=[An @i<iterator type> is a type descended from
1687 the Forward_Iterator interface from some instance of
1688 Ada.Iterator_Interfaces.@Defn{iterator type}
1689 A @i<reversible iterator type> is a type descended from the Reversible_Iterator
1690 interface from some instance of Ada.Iterator_Interfaces.@Defn{reversible iterator type}
1691 @Chg{Version=[5],New=[A @i{parallel iterator type}@Defn{parallel iterator type}
1692 is a type descended from the Parallel_Iterator interface from some instance
1693 of Ada.Iterator_Interfaces. A type descended from the
1694 Parallel_Reversible_Iterator interface from some
1695 instance of Ada.Iterator_Interfaces is both a parallel iterator type and a
1696 reversible iterator type. ],Old=[]}An @i<iterator object>
1697 is an object of an iterator type.@Defn{iterator object}
1698 A @i<reversible iterator object> is an object
1699 of a reversible iterator type.@Defn{reversible iterator object}
1700 @Chg{Version=[5],New=[A @i<parallel iterator object> is an object
1701 of a parallel iterator type.@Defn{parallel iterator object} ],Old=[]}The
1702 formal subtype Cursor from the associated
1703 instance of Ada.Iterator_Interfaces is the @i<iteration cursor subtype> for the
1704 iterator type.@Defn{iteration cursor subtype}]}
1705
1706 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1707 @ChgAdded{Version=[3],Type=[Leading],Text=[The following type-related
1708 operational aspects may be specified for an indexable container type @i<T> (see
1709 @RefSecNum{User-Defined Indexing}):]}
1710
1711 @begin{Description}
1712 @ChgRef{Version=[3],Kind=[AddedNormal]}
1713 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0111-1]}
1714 @ChgAdded{Version=[3],Text=[Default_Iterator@\This aspect is specified
1715 by a @nt{name} that denotes exactly one function declared immediately within
1716 the same declaration list in which @i<T> is declared, whose first parameter
1717 is of type @i<T> or @i<T>'Class or an access parameter whose designated type
1718 is type @i<T> or @i<T>'Class, whose other parameters, if any, have default
1719 expressions, and whose result type is an iterator type. This function is the
1720 @i<default iterator function> for @i<T>.@Defn{default iterator function}
1721 Its result subtype is the @i<default iterator subtype> for
1722 @i<T>.@Defn{default iterator subtype} The iteration cursor subtype for
1723 the default iterator subtype is the @i<default cursor subtype>
1724 for
1725 @i<T>.@Defn{default cursor subtype}@AspectDefn{Default_Iterator}@Chg{Version=[5],New=[
1726 This aspect is inherited by descendants
1727 of type @i<T> (including @i<T>'Class).],Old=[]}]}
1728
1729 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Default_Iterator],
1730 Text=[@ChgAdded{Version=[3],Text=[Default iterator to be used in @key[for]
1731 loops.]}]}
1732
1733 @ChgRef{Version=[3],Kind=[AddedNormal]}
1734 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0111-1]}
1735 @ChgAdded{Version=[3],Text=[Iterator_Element@\This aspect is specified by a
1736 @nt{name} that denotes a subtype. This is the @i<default element subtype>
1737 for @i<T>.@Defn{default element subtype}@AspectDefn{Iterator_Element}@Chg{Version=[5],New=[
1738 This aspect is inherited by descendants
1739 of type @i<T> (including @i<T>'Class).],Old=[]}]}
1740
1741 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Iterator_Element],
1742 Text=[@ChgAdded{Version=[3],Text=[Element type to be used for user-defined
1743 iterators.]}]}
1744
1745 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0111-1]}
1746 @ChgAdded{Version=[5],Type=[Leading],Text=[Iterator_View@\This aspect is
1747 specified by a @nt{name} that denotes a type @i<T2> with the
1748 following properties:@AspectDefn{Iterator_View}]}
1749
1750 @begin{Itemize}
1751 @ChgRef{Version=[5],Kind=[Added]}
1752 @ChgAdded{Version=[5],Text=[@i<T2> is declared in the same compilation
1753 unit as @i<T>;]}
1754
1755 @ChgRef{Version=[5],Kind=[Added]}
1756 @ChgAdded{Version=[5],Text=[@i<T2> is an iterable container type;]}
1757
1758 @ChgRef{Version=[5],Kind=[Added]}
1759 @ChgAdded{Version=[5],Text=[@i<T2> has a single discriminant which is an
1760 access discriminant designating @i<T>; and]}
1761
1762 @ChgRef{Version=[5],Kind=[Added]}
1763 @ChgAdded{Version=[5],Text=[The default iterator subtypes for @i<T> and
1764 @i<T2> statically match.]}
1765 @end{Itemize}
1766
1767 @ChgRef{Version=[5],Kind=[Added]}
1768 @ChgAdded{Version=[5],NoPrefix=[T],Text=[This aspect is never
1769 inherited@Redundant[, even by @i<T>'Class].]}
1770
1771 @begin{Reason}
1772 @ChgRef{Version=[5],Kind=[AddedNormal]}
1773 @ChgAdded{Version=[5],Text=[Iterator_View allows specifying an alternative
1774 type to be automatically used by container element iterators; see
1775 @RefSecNum{Generalized Loop Iteration}. This allows setting state for
1776 an iteration only once rather than for each individual reference.]}
1777 @end{Reason}
1778
1779 @begin{Ramification}
1780 @ChgRef{Version=[5],Kind=[AddedNormal]}
1781 @ChgAdded{Version=[5],Text=[Since Iterator_View is not inherited, it does
1782 not apply to @i<T>'Class. Otherwise, the type of the iterator object would
1783 not be known at compile-time (since it necessarily has to be different for
1784 each descendant).]}
1785 @end{Ramification}
1786
1787 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Iterator_View],
1788 Text=[@ChgAdded{Version=[5],Text=[An alternative type to used for container
1789 element iterators.]}]}
1790
1791 @end{Description}
1792
1793 @ChgRef{Version=[3],Kind=[AddedNormal]}
1794 @ChgRef{Version=[5],Kind=[Deleted],ARef=[AI12-0111-1]}
1795 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[],Old=[These aspects are
1796 inherited by descendants of type @i<T> (including @i<T>'Class).]}]}
1797
1798 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1799 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0266-1]}
1800 @ChgAdded{Version=[3],Text=[An @i<iterable container type> is an indexable container type
1801 with specified Default_Iterator and Iterator_Element aspects.@Defn{iterable container type}
1802 A @i<reversible iterable container type> is an iterable container type with the default iterator type
1803 being a reversible iterator type.@Defn{reversible iterable container type}
1804 @Chg{Version=[5],New=[A @i{parallel iterable container type} is an iterable
1805 container type with the default iterator type being a parallel iterator
1806 type.@Defn{parallel iterable container type} ],Old=[]}An
1807 @i<iterable container object> is an object of an iterable container type.@Defn{iterable container object}
1808 A @i<reversible iterable container object> is an object of a reversible iterable container
1809 type.@Defn{reversible iterable container object}@Chg{Version=[5],New=[
1810 A @i<parallel iterable container object> is an object of a
1811 parallel iterable container type.@Defn{parallel iterable container object}],Old=[]}]}
1812
1813 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Iterable container type>,
1814 Text=<@ChgAdded{Version=[3],Text=[An iterable container type is one that has
1815 user-defined behavior for iteration, via the Default_Iterator and
1816 Iterator_Element aspects.]}>}
1817
1818 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0138-1]}
1819 @ChgAdded{Version=[4],Text=[The Default_Iterator and Iterator_Element aspects
1820 are nonoverridable (see @RefSecNum{Aspect Specifications}).]}
1821
1822 @begin{Reason}
1823 @ChgRef{Version=[4],Kind=[AddedNormal]}
1824 @ChgAdded{Version=[4],Text=[This ensures that all descendants of an
1825 iterable container type have aspects with the same properties. This prevents
1826 generic contract problems with formal derived types.]}
1827 @end{Reason}
1828 @end{StaticSem}
1829
1830 @begin{Legality}
1831
1832 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1833 @ChgAdded{Version=[3],Type=[Leading],Text=[The Constant_Indexing aspect (if any)
1834 of an iterable container type @i<T> shall denote exactly one function with the following
1835 properties:]}
1836
1837 @begin{Itemize}
1838
1839 @ChgRef{Version=[3],Kind=[AddedNormal]}
1840 @ChgAdded{Version=[3],Text=[the result type of the function is covered by the
1841 default element type of @i<T> or is a reference type (see
1842 @RefSecNum{User-Defined References}) with an access discriminant designating
1843 a type covered by the default element type of @i<T>;]}
1844
1845 @ChgRef{Version=[3],Kind=[AddedNormal]}
1846 @ChgAdded{Version=[3],Text=[the type of the second parameter of the function
1847 covers the default cursor type for @i<T>;]}
1848
1849 @ChgRef{Version=[3],Kind=[AddedNormal]}
1850 @ChgAdded{Version=[3],Text=[if there are more than two parameters, the
1851 additional parameters all have default expressions.]}
1852
1853 @end{Itemize}
1854
1855 @ChgRef{Version=[3],Kind=[AddedNormal]}
1856 @ChgAdded{Version=[3],Text=[This function (if any) is the
1857 @i<default constant indexing function> for @i<T>.@Defn{default constant indexing function}]}
1858
1859 @begin{Ramification}
1860 @ChgRef{Version=[3],Kind=[AddedNormal]}
1861 @ChgAdded{Version=[3],Text=[This does not mean that Constant_Indexing has to
1862 designate only one subprogram, only that there is only one routine that meets
1863 all of these properties. There can be other routines designated by
1864 Constant_Indexing, but they cannot have the profile described above. For
1865 instance, map containers have a version of Constant_Indexing that takes a
1866 key instead of a cursor; this is allowed.]}
1867 @end{Ramification}
1868
1869 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1870 @ChgAdded{Version=[3],Type=[Leading],Text=[The Variable_Indexing aspect (if any)
1871 of an iterable container type @i<T> shall denote exactly one function with the following
1872 properties:]}
1873
1874 @begin{Itemize}
1875 @ChgRef{Version=[3],Kind=[AddedNormal]}
1876 @ChgAdded{Version=[3],Text=[the result type of the function is a reference
1877 type (see @RefSecNum{User-Defined References}) with an access discriminant
1878 designating a type covered by the default element type of @i<T>;]}
1879
1880 @ChgRef{Version=[3],Kind=[AddedNormal]}
1881 @ChgAdded{Version=[3],Text=[the type of the second parameter of the function
1882 covers the default cursor type for @i<T>;]}
1883
1884 @ChgRef{Version=[3],Kind=[AddedNormal]}
1885 @ChgAdded{Version=[3],Text=[if there are more than two parameters, the
1886 additional parameters all have default expressions.]}
1887
1888 @end{Itemize}
1889
1890 @ChgRef{Version=[3],Kind=[AddedNormal]}
1891 @ChgAdded{Version=[3],Text=[This function (if any) is the
1892 @i<default variable indexing function> for @i<T>.@Defn{default variable indexing function}]}
1893
1894 @end{Legality}
1895
1896 @begin{Extend2005}
1897 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1898 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}User-defined
1899 iterator types are new in Ada 2012.]}
1900 @end{Extend2005}
1901
1902 @begin{Incompatible2012}
1903 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0138-1]}
1904 @ChgAdded{Version=[4],Text=[@Defn{incompatibilities with Ada 2012}@b<Corrigendum:>
1905 Defined Default_Iterator and Iterator_Element to be nonoveridable, which
1906 makes redefinitions and hiding of these aspects illegal. It's possible that
1907 some program could violate one of these new restrictions, but in most cases
1908 this can easily be worked around by using overriding rather than
1909 redefinition.]}
1910
1911 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0266-1]}
1912 @ChgAdded{Version=[5],Text=[Various new types and subprograms are newly added
1913 to Ada.Iterator_Interfaces. If an instance of Ada.Iterator_Interfaces is
1914 referenced in a @nt{use_clause}, and an entity with one of the new names
1915 is defined in some other package that is also referenced in a @nt{use_clause},
1916 the user-defined entity may no longer be use-visible,
1917 resulting in errors. This should be rare and is easily fixed if it does occur.]}
1918 @end{Incompatible2012}
1919
1920 @begin{Extend2012}
1921 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0111-1]}
1922 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}Aspect Iterator_View
1923 is new; it allows container element iterators to set the tampering state
1924 once rather than for each use of the element.]}
1925
1926 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0266-1]}
1927 @ChgAdded{Version=[5],Text=[Parallel iterator
1928 interfaces are new; they allow user-defined parallel loops to be defined.]}
1929 @end{Extend2012}
1930
1931
1932
1933 @LabeledAddedSubClause{Version=[3],Name=[Generalized Loop Iteration]}
1934
1935 @begin{Intro}
1936 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1937 @ChgAdded{Version=[3],Text=[Generalized forms of loop iteration are provided by
1938 an @nt{iterator_specification}.]}
1939 @end{Intro}
1940
1941 @begin{Syntax}
1942 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1943 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0156-1],ARef=[AI12-0250-1]}
1944 @AddedSyn{Version=[3],lhs=<@Chg{Version=[3],New=<iterator_specification>,Old=<>}>,
1945 rhs="@Chg{Version=[3],New=<
1946 @Syn2{defining_identifier} @Chg{Version=[5],New=<[: @Syn2{loop_parameter_subtype_indication}] >,Old=<>}@key[in] [@key{reverse}] @SynI{iterator_}@Syn2{name}@Chg{Version=[5],New=<
1947 [@Syn2{iterator_filter}]>,Old=<>}
1948 | @Syn2{defining_identifier} [: @Chg{Version=[5],New=<@Syn2{loop_parameter_subtype_indication}>,Old=<@Syn2{subtype_indication}>}] @key[of] [@key{reverse}] @SynI{iterable_}@Syn2{name}>,Old=<>}@Chg{Version=[5],New=<
1949 [@Syn2{iterator_filter}]>,Old=<>}"}
1950
1951 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0156-1]}
1952 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<loop_parameter_subtype_indication>,Old=<>}>,
1953 rhs="@Chg{Version=[5],New=<@Syn2{subtype_indication} | @Syn2{access_definition}>,Old=<>}"}
1954
1955 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
1956 @ChgAdded{Version=[5],Text=[If an @nt{iterator_specification} is for a parallel
1957 construct, the reserved word @key[reverse] shall not appear in the
1958 @nt{iterator_specification}.]}
1959 @end{Syntax}
1960
1961 @begin{Resolution}
1962 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1963 @ChgAdded{Version=[3],Text=[For the first form of @nt{iterator_specification},
1964 called a @i<generalized iterator>,@Defn{generalized iterator}@Defn2{Term=[iterator],Sec=[generalized]}
1965 the expected type for the @SynI<iterator_>@nt{name} is any iterator
1966 type.@PDefn2{Term=[expected type],Sec=[@SynI<iterator_>@nt{name}]}
1967 For the second form of @nt{iterator_specification},
1968 the expected type for the @SynI<iterable_>@nt{name} is any array or iterable
1969 container type.@PDefn2{Term=[expected type],Sec=[@SynI<iterable_>@nt{name}]}
1970 If the @SynI<iterable_>@nt{name} denotes an array object, the
1971 @nt{iterator_specification} is called an @i<array
1972 component iterator>;@Defn{array component iterator}@Defn2{Term=[iterator],Sec=[array component]}
1973 otherwise it is called a
1974 @i<container element iterator>.@Defn{container element iterator}@Defn2{Term=[iterator],Sec=[container element]}]}
1975 @end{Resolution}
1976
1977 @begin{Legality}
1978 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Iterator>,
1979 Text=<@ChgAdded{Version=[3],Text=[An iterator is a construct that is used to
1980 loop over the elements of an array or container. Iterators may be user defined,
1981 and may perform arbitrary computations to access elements from a container.]}>}
1982
1983 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1984 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0266-1]}
1985 @ChgAdded{Version=[3],Text=[If the reserved word @key[reverse] appears,
1986 the @nt{iterator_specification} is a
1987 @Defn{reverse iterator}@Defn2{Term=[iterator],Sec=[reverse]}@i<reverse iterator>@Chg{Version=[5],New=[.
1988 If the @nt{iterator_specification} is for a parallel construct, the
1989 @nt{iterator_specification} is a
1990 @i<parallel iterator>.@Defn{parallel iterator}@Defn2{Term=[iterator],Sec=[parallel]}
1991 Otherwise,],Old=[; otherwise]} it is a
1992 @i<forward iterator>.@Defn{forward iterator}@Defn2{Term=[iterator],Sec=[forward]}
1993 @Chg{Version=[5],New=[Forward and reverse iterators are collectively called
1994 @i<sequential>
1995 iterators.@Defn{sequential iterator}@Defn2{Term=[iterator],Sec=[sequential]} ],Old=[]}In a
1996 reverse generalized iterator, the
1997 @SynI<iterator_>@nt{name} shall be of a reversible iterator type.
1998 @Chg{Version=[5],New=[In a parallel generalized iterator, the
1999 @SynI<iterator_>@nt{name} shall be of a parallel iterator type. ],Old=[]}In
2000 a reverse container element iterator, the default iterator type for the type
2001 of the @SynI<iterable_>@nt{name} shall be a reversible iterator
2002 type.@Chg{Version=[5],New=[ In a parallel container element iterator, the
2003 default iterator type for the type of the @SynI<iterable_>@nt{name} shall be
2004 of a parallel iterator type.],Old=[]}]}
2005
2006 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
2007 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0151-1]}
2008 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0156-1],ARef=[AI12-0183-1]}
2009 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[The subtype defined by the
2010 @nt{loop_parameter_subtype_indication}, if any, of a generalized iterator
2011 shall statically match the iteration cursor
2012 subtype. ],Old=[]}The @Chg{Version=[4],New=[subtype defined by],Old=[type of]}
2013 the @Chg{Version=[5],New=[@nt{loop_parameter_subtype_indication}],
2014 Old=[@nt{subtype_indication}]}, if any, of an array component
2015 iterator shall @Chg{Version=[4],New=[statically match],Old=[cover]} the
2016 component @Chg{Version=[4],New=[subtype],Old=[type]} of the type of the
2017 @SynI<iterable_>@nt{name}. The @Chg{Version=[4],New=[subtype defined
2018 by],Old=[type of]} the @Chg{Version=[5],New=[@nt{loop_parameter_subtype_indication}],
2019 Old=[@nt{subtype_indication}]}, if any, of a container element
2020 iterator shall @Chg{Version=[4],New=[statically match],Old=[cover]} the default
2021 element @Chg{Version=[4],New=[subtype],Old=[type]} for the type of the
2022 @SynI<iterable_>@nt{name}.]}
2023
2024 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
2025 @ChgAdded{Version=[3],Text=[In a container element iterator whose
2026 @SynI<iterable_>@nt{name} has type @i<T>, if the @SynI<iterable_>@nt{name}
2027 denotes a constant or the Variable_Indexing aspect is not specified for @i<T>,
2028 then the Constant_Indexing aspect shall be specified for @i<T>.]}
2029
2030 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0047-1]}
2031 @ChgAdded{Version=[4],Text=[The @SynI<iterator_>@nt{name} or
2032 @SynI<iterable_>@nt{name} of an @nt{iterator_specification} shall
2033 not denote a subcomponent that depends on discriminants of an object
2034 whose nominal subtype is unconstrained, unless the object is known
2035 to be constrained.]}
2036
2037 @begin{Reason}
2038 @ChgRef{Version=[4],Kind=[AddedNormal]}
2039 @ChgAdded{Version=[4],Text=[This is the same rule that applies to
2040 renames; it serves the same purpose of preventing the object from
2041 disappearing while the iterator is still using it.]}
2042 @end{Reason}
2043
2044 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0120-1]}
2045 @ChgAdded{Version=[4],Text=[A container element iterator is illegal if the
2046 call of the default iterator function that creates the loop iterator
2047 (see below) is illegal.]}
2048
2049 @begin{Ramification}
2050 @ChgRef{Version=[4],Kind=[AddedNormal]}
2051 @ChgAdded{Version=[4],Text=[This can happen if the parameter to the default
2052 iterator function is @key[in out] and the @SynI<iterable_>@nt{name} is a
2053 constant. The wording applies to any reason that the call would be illegal,
2054 as it's possible that one of the default parameters would be illegal, or
2055 that some accessibility check would fail.]}
2056 @end{Ramification}
2057
2058 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0120-1]}
2059 @ChgAdded{Version=[4],Text=[A generalized iterator is illegal if the iteration
2060 cursor subtype of the @SynI<iterator_>@nt{name} is a limited type at the point
2061 of the generalized iterator. A container element iterator is illegal if the
2062 default cursor subtype of the type of the @SynI<iterable_>@nt{name} is a limited
2063 type at the point of the container element iterator.]}
2064
2065 @begin{Reason}
2066 @ChgRef{Version=[4],Kind=[AddedNormal]}
2067 @ChgAdded{Version=[4],Text=[If the cursor type is limited, the assignment to
2068 the loop parameter for a generalized iterator would be illegal. The same is
2069 true for a container element iterator. We have to say "at the point of the
2070 iterator" as the limitedness of a type can change due to visibility.]}
2071 @end{Reason}
2072
2073 @end{Legality}
2074
2075 @begin{StaticSem}
2076
2077 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0269-1],ARef=[AI05-0292-1]}
2078 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0156-1]}
2079 @ChgAdded{Version=[3],Text=[An @nt{iterator_specification} declares a
2080 @i<loop parameter>.@Defn{loop parameter}
2081 In a generalized iterator, @Chg{Version=[5],New=[],Old=[the nominal subtype of
2082 the loop parameter is the iteration cursor subtype. In ]}an array component
2083 iterator@Chg{Version=[5],New=[,],Old=[]} or a container element iterator, if
2084 a @Chg{Version=[5],New=[@nt{loop_parameter_subtype_indication}],
2085 Old=[@nt{subtype_indication}]} is present, it
2086 determines the nominal subtype of the loop parameter.@Chg{Version=[5],New=[ In
2087 a generalized iterator, if a @nt{loop_parameter_subtype_indication} is not
2088 present, the nominal subtype of the loop parameter is the iteration cursor
2089 subtype.],Old=[]} In an array component iterator, if a
2090 @Chg{Version=[5],New=[@nt{loop_parameter_subtype_indication}],
2091 Old=[@nt{subtype_indication}]} is not present, the
2092 nominal subtype of the loop parameter is the component subtype of the
2093 type of the @SynI{iterable_}@nt{name}. In a container element iterator, if a
2094 @Chg{Version=[5],New=[@nt{loop_parameter_subtype_indication}],
2095 Old=[@nt{subtype_indication}]} is not present, the nominal subtype of the loop
2096 parameter is the default element subtype for the type of the
2097 @SynI{iterable_}@nt{name}.]}
2098
2099 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
2100 @ChgAdded{Version=[3],Text=[In a generalized iterator, the loop parameter
2101 is a constant. In an array component iterator, the loop parameter
2102 is a constant if the @SynI<iterable_>@nt{name} denotes a constant; otherwise
2103 it denotes a variable. In a container element iterator, the loop parameter
2104 is a constant if the @SynI{iterable_}@nt{name} denotes a constant, or if
2105 the Variable_Indexing aspect is not specified for the type of the
2106 @SynI{iterable_}@nt{name}; otherwise it is a variable.]}
2107
2108 @begin{Ramification}
2109 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0093-1]}
2110 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0156-1]}
2111 @ChgAdded{Version=[4],Text=[The loop parameter of a generalized iterator has
2112 the same accessibility as the loop statement. This means that the loop
2113 parameter object is finalized when the loop statement is left. (It also may be
2114 finalized as part of assigning a new value to the loop parameter.) For array
2115 component iterators@Chg{Version=[5],New=[],Old=[and container element
2116 iterators]}, the loop parameter directly denotes an element of the array
2117 @Chg{Version=[5],New=[],Old=[or container ]}and has the accessibility of
2118 the associated array@Chg{Version=[5],New=[],Old=[ or container]}.
2119 @Chg{Version=[5],New=[For container element iterators, the loop parameter
2120 denotes the result of the indexing function call (in the case of a constant
2121 indexing) or a generalized reference thereof (in the case of a variable
2122 indexing). Roughly speaking, the loop parameter has the accessibility level
2123 of a single iteration of the loop. More precisely, the function result (or
2124 the generalized reference thereof) is considered to be renamed in the
2125 declarative part of a notional block statement which immediately encloses
2126 the loop's @nt{sequence_of_statements}; the accessibility of the loop
2127 parameter is that of the block statement.],Old=[]}]}
2128 @end{Ramification}
2129
2130 @end{StaticSem}
2131
2132 @begin{Runtime}
2133
2134 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
2135 @ChgAdded{Version=[3],Text=[For the execution of a @nt{loop_statement} with
2136 an @nt{iterator_specification}, the @nt{iterator_specification} is
2137 first elaborated. This elaboration elaborates the @nt{subtype_indication},
2138 if any.]}
2139
2140 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
2141 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0250-1],ARef=[AI12-0266-1]}
2142 @ChgAdded{Version=[3],Text=[For a @Chg{Version=[5],New=[sequential
2143 ],Old=[]}generalized iterator, the loop parameter is created, the
2144 @SynI{iterator_}@nt{name} is evaluated, and the denoted iterator object becomes
2145 the @i<loop iterator>.@Defn{loop iterator} In a forward generalized iterator,
2146 the operation First of the iterator type is called on the loop iterator, to
2147 produce the initial value for the loop parameter. If the result of calling
2148 Has_Element on the initial value is False, then the execution of the
2149 @nt{loop_statement} is complete. Otherwise, the @nt{sequence_of_statements} is
2150 @Chg{Version=[5],New=[conditionally ],Old=[]}executed and then the
2151 Next operation of the iterator type is called with the
2152 loop iterator and the current value of the loop parameter to produce the next
2153 value to be assigned to the loop parameter. This repeats until the result of
2154 calling Has_Element on the loop parameter is False, or the loop is left as a
2155 consequence of a transfer of control. For a reverse generalized iterator, the
2156 operations Last and Previous are called rather than First and Next.]}
2157
2158 @begin{Ramification}
2159 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0093-1]}
2160 @ChgAdded{Version=[4],Text=[The loop parameter of a generalized iterator is a
2161 variable of which the user only has a constant view. It follows the normal
2162 rules for a variable of its nominal subtype. In particular, if the nominal
2163 subtype is indefinite, the variable is constrained by its initial value.
2164 Similarly, if the nominal subtype is class-wide, the variable (like all
2165 variables) has the tag of the initial value. Constraint_Error may be raised by
2166 a subsequent iteration if Next or Previous return an object with a different
2167 tag or constraint.]}
2168 @end{Ramification}
2169
2170 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
2171 @ChgAdded{Version=[5],Text=[For a parallel generalized iterator, the
2172 @nt{chunk_specification}, if any, of the associated parallel construct, is first
2173 elaborated, to determine the maximum number of chunks (see
2174 @RefSecNum{Loop Statements}), and then the operation Split_Into_Chunks of the
2175 iterator type is called, with the determined maximum passed as the Max_Chunks
2176 parameter, specifying the upper bound for the number of loop parameter objects
2177 (and the number of logical threads of control) to be associated with the
2178 iterator. In the absence of a @nt{chunk_specification}, the maximum number of
2179 chunks is determined in an implementation-defined manner.]}
2180
2181 @ChgImplDef{Version=[5],Kind=[AddedNormal],InitialVersion=[5],
2182 Text=[@ChgAdded{Version=[5],Text=[The maximum number of chunks for a parallel
2183 generalized iterator without a @nt{chunk_specification}.]}]}
2184
2185 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0250-1],ARef=[AI12-0266-1]}
2186 @ChgAdded{Version=[5],Text=[Upon return from Split_Into_Chunks, the actual
2187 number of chunks for the loop is determined by calling the Chunk_Count operation
2188 of the iterator, at which point one logical thread of control is initiated for
2189 each chunk, with an associated chunk index in the range from one to the actual
2190 number of chunks. Within each logical thread of control, a loop parameter is
2191 created. If a @nt{chunk_specification} with a @nt{discrete_subtype_definition}
2192 is present in the associated parallel construct, then a chunk parameter is
2193 created, and initialized with a value from the discrete subtype defined by the
2194 @nt{discrete_subtype_definition}, so that the order of the chosen chunk
2195 parameter values correspond to the order of the chunk indices associated with
2196 the logical threads of control. The operation First of the iterator type having
2197 a Chunk parameter is called on the loop iterator, with Chunk initialized from
2198 the corresponding chunk index, to produce the initial value for the loop
2199 parameter. If the result of calling Has_Element on this initial value is False,
2200 then the execution of the logical thread of control is complete. Otherwise, the
2201 @nt{sequence_of_statements} is conditionally executed and then the Next
2202 operation of the iterator type having a Chunk parameter is called, with the loop
2203 iterator, the current value of the loop parameter, and the corresponding chunk
2204 index, to produce the next value to be assigned to the loop parameter. This
2205 repeats until the result of calling Has_Element on the loop parameter is False,
2206 or the associated parallel construct is left as a consequence of a transfer of
2207 control. In the absence of a transfer of control, the associated parallel
2208 construct of a parallel generalized iterator is complete when all of its logical
2209 threads of control are complete.]}
2210
2211 @begin{Discussion}
2212 @ChgRef{Version=[5],Kind=[Added]}
2213 @ChgAdded{Version=[5],Text=[The Max_Chunks parameter of the Split_Into_Chunks
2214 procedure is an upper bound for the number of chunks to be associated with a
2215 loop. A container implementation may opt for a lower value for the number of
2216 chunks if a more optimal split can be determined. For instance, a tree-based
2217 container might create the split based on the number of branches at the top
2218 levels of the tree.]}
2219 @end{Discussion}
2220
2221 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
2222 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0250-1],ARef=[AI12-0266-1]}
2223 @ChgAdded{Version=[3],Text=[For an array component iterator,
2224 @Chg{Version=[5],New=[the @nt{chunk_specification} of the associated
2225 parallel construct, if any, is first elaborated to determine
2226 the maximum number of chunks (see @RefSecNum{Loop Statements}), and
2227 then ],Old=[]}the @SynI<iterable_>@nt{name} is evaluated and
2228 the denoted array object becomes the @i<array for the
2229 loop>.@Defn{array for a loop} If the array for the loop is a null array,
2230 then the execution of the @nt{loop_statement} is complete. Otherwise, the
2231 @nt{sequence_of_statements} is
2232 @Chg{Version=[5],New=[conditionally ],Old=[]}executed with the loop parameter
2233 denoting each component of the array for the loop, using a @i<canonical> order
2234 of components,@Defn{canonical order of array components}
2235 which is last dimension varying fastest (unless the
2236 array has convention Fortran, in which case it is first dimension
2237 varying fastest). For a
2238 forward array component iterator, the iteration starts with the
2239 component whose index values are each the first in their index range,
2240 and continues in the canonical order. For a reverse array component
2241 iterator, the iteration starts with the component whose index values
2242 are each the last in their index range, and continues in the reverse
2243 of the canonical order.
2244 @Chg{Version=[5],New=[For a parallel array component iterator, the iteration is
2245 broken up into contiguous chunks of the canonical order, such that all
2246 components are covered with no overlaps; each chunk has its own logical thread
2247 of control with its own loop parameter and iteration within each chunk is in the
2248 canonical order. The number of chunks is implementation defined, but is limited
2249 in the presence of a @nt{chunk_specification} to the determined maximum.
2250 ],Old=[]}The loop iteration proceeds until the
2251 @nt{sequence_of_statements} has been
2252 @Chg{Version=[5],New=[conditionally ],Old=[]}executed for each component of the
2253 array for the loop, or until the loop is left as a consequence of a
2254 transfer of control.]}
2255
2256 @ChgImplDef{Version=[5],Kind=[AddedNormal],InitialVersion=[5],
2257 Text=[@ChgAdded{Version=[5],Text=[The number of chunks for an array component
2258 iterator.]}]}
2259
2260 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0266-1]}
2261 @ChgAdded{Version=[5],Text=[If a @nt{chunk_specification} with a
2262 @nt{discrete_subtype_definition} is present
2263 in the associated parallel construct, then the logical thread of
2264 control associated with a given chunk has a chunk parameter
2265 initialized with a distinct value from the discrete subtype defined by
2266 the @nt{discrete_subtype_definition}. The values of the chunk parameters
2267 are assigned such that they increase in the canonical order of the
2268 starting array components for the chunks.]}
2269
2270 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
2271 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0111-1],ARef=[AI12-0266-1]}
2272 @ChgAdded{Version=[3],Text=[For a container element iterator,
2273 @Chg{Version=[5],New=[the @nt{chunk_specification} of the associated
2274 parallel construct, if any, is first elaborated to determine
2275 the maximum number of chunks (see @RefSecNum{Loop Statements}), and
2276 then ],Old=[]}the @SynI<iterable_>@nt{name} is evaluated@Chg{Version=[5],New=[.
2277 If the container type has Iterator_View specified, an object of the
2278 Iterator_View type is created with the discriminant referencing the
2279 iterable container object denoted by the @SynI<iterable_>@nt{name}. This is
2280 the @i<iterable container object for the loop>. Otherwise,],Old=[and]}
2281 the iterable container object
2282 @Chg{Version=[5],New=[denoted by the @SynI<iterable_>@nt{name} becomes the
2283 iterable container object for the loop],Old=[becomes the @i<iterable
2284 container object for the loop>]}.@Defn{iterable container object for a loop}
2285 The default iterator function for the type of
2286 the iterable container object for the loop is called on the iterable container object
2287 and the result is the @i<loop iterator>.@Defn2{Term=[loop iterator],Sec=[container element iterator]}
2288 @Chg{Version=[5],New=[For a sequential container element
2289 iterator, an],Old=[An]} object of the default cursor subtype is created
2290 (the @i<loop cursor>).@Defn{loop cursor}@Chg{Version=[5],New=[ For a parallel
2291 container element iterator, each chunk of iterations
2292 will have its own loop cursor, again of the default cursor subtype.],Old=[]}]}
2293
2294 @begin{Reason}
2295 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0111-1]}
2296 @ChgAdded{Version=[5],Text=[If Iterator_View is specified, we add an extra
2297 object and use that object for this iteration. This allows these iterators
2298 to automatically use the stable view (defined in each of the language-defined
2299 containers) to do the iteration. That eliminates the need to set and clear
2300 the tampering with elements indication each time Reference is called; that
2301 eliminates substantial overhead as finalization is typically used to
2302 implement the tampering reset.]}
2303 @end{Reason}
2304
2305 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
2306 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0266-1]}
2307 @ChgAdded{Version=[3],Text=[@Chg{Version=[5],New=[A],Old=[For a]} container
2308 element iterator@Chg{Version=[5],New=[ then proceeds as described above for a
2309 generalized iterator, except that each reference to a loop parameter
2310 is replaced by a reference to the corresponding loop cursor. For a
2311 container element iterator, the loop parameter for each iteration
2312 instead denotes],Old=[, the
2313 operation First of the iterator type is called on the loop iterator, to produce
2314 the initial value for the loop cursor. If the result of calling Has_Element on
2315 the initial value is False, then the execution of the @nt{loop_statement} is
2316 complete. Otherwise, the @nt{sequence_of_statements} is executed with the loop
2317 parameter denoting]} an indexing (see @RefSecNum{User-Defined Indexing})
2318 into the iterable container
2319 object for the loop, with the only parameter to the indexing being the
2320 @Chg{Version=[5],New=[],Old=[current ]}value of the loop
2321 cursor@Chg{Version=[5],New=[ for the given iteration],Old=[; then the Next
2322 operation of the iterator type is called with the loop iterator and the loop
2323 cursor to produce the next value to be assigned to the loop cursor. This repeats
2324 until the result of calling Has_Element on the loop cursor is False, or until
2325 the loop is left as a consequence of a transfer of control. For a reverse
2326 container element iterator, the operations Last and Previous are called rather
2327 than First and Next]}. If the loop parameter is a constant (see above), then the
2328 indexing uses the default constant indexing function for the type of the
2329 iterable container object for the loop; otherwise it uses the default variable
2330 indexing function.]}
2331
2332
2333
2334 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
2335 @ChgAdded{Version=[4],Text=[Any exception propagated by the execution of a
2336 generalized iterator or container element iterator is propagated by the
2337 immediately enclosing loop statement.]}
2338
2339 @begin{Ramification}
2340 @ChgRef{Version=[4],Kind=[AddedNormal]}
2341 @ChgAdded{Version=[4],Text=[This text covers exceptions raised by called
2342 functions that make up the execution of the iterator as well as
2343 exceptions raised by the assignment to the loop parameter or cursor.]}
2344 @end{Ramification}
2345 @end{Runtime}
2346
2347 @begin{Examples}
2348 @begin{Example}
2349 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0269-1]}
2350 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0266-1]}
2351 @ChgAdded{Version=[3],Text=[-- @Examcom{Array component iterator example:}
2352 @Chg{Version=[5],New=[@key[parallel]
2353 ],Old=[]}@key[for] Element @key[of] Board @key[loop] -- @Examcom{See @RefSecNum{Index Constraints and Discrete Ranges}.}
2354 Element := Element * 2.0; -- @Examcom{Double each element of Board, a two-dimensional array.}
2355 @key[end loop];]}
2356 @end{Example}
2357
2358 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0268-1]}
2359 @ChgAdded{Version=[3],Text=[For examples of use of generalized iterators,
2360 see @RefSecNum{Example of Container Use} and the corresponding container
2361 packages in @RefSecNum{The Generic Package Containers.Vectors} and
2362 @RefSecNum{The Generic Package Containers.Doubly_Linked_Lists}.]}
2363 @end{Examples}
2364
2365
2366 @begin{Extend2005}
2367 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
2368 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}Generalized forms
2369 of loop iteration are new.]}
2370 @end{Extend2005}
2371
2372 @begin{Incompatible2012}
2373 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0047-1]}
2374 @ChgAdded{Version=[4],Text=[@Defn{incompatibilities with Ada
2375 2012}@b<Corrigendum:> Added a rule to ensure that the object being iterated
2376 cannot be a component that could disappear before the loop completes. This
2377 could be incompatible by making a loop that was legal (and worked correctly,
2378 so long as the enclosing object is not modified during the loop) from the
2379 original Ada 2012 illegal in corrected Ada 2012. Such loops should be pretty
2380 rare, especially as these iterator forms are new to Ada 2012.]}
2381
2382 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
2383 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added rules to reject loops
2384 if the call to the default iterator function for a container element
2385 iterator is illegal, or if the cursor type of an iterator is limited.
2386 These are formally incompatible with original Ada 2012, but as it's unlikely
2387 that any Ada 2012 compiler ever allowed the illegal usages in an expansion
2388 of a loop (it's much more likely that they would have just caused an internal
2389 error in the compiler), this should have no effect in practice.]}
2390
2391 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0151-1]}
2392 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added a requirement that the
2393 given subtype statically match the subtype of the element or component for
2394 a component element iterator or array component iterator. Original Ada 2012
2395 text allowed any type that covered the subtype of the element or component,
2396 but that led to questions of what the meaning was if they are different.
2397 In this case, the element is essentially a renaming of the container element,
2398 and it doesn't make sense for the constraints to be different. Ignoring
2399 explicitly defined constraints in renames is a mistake that we don't want
2400 to continue, thus we require static matching. This means that some programs
2401 might be illegal, but those programs were misleading at best, and
2402 potentially would raise unexpected exceptions because the element values
2403 might have been invalid or abnormal with respect to the declared constraint.]}
2404 @end{Incompatible2012}
2405
2406 @begin{Extend2012}
2407 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0156-1]}
2408 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}For consistency, we
2409 now allow a @nt{subtype_indication} on a generalized iterator, and anonymous
2410 access types on all forms of iterator. We introduced a new syntax
2411 non-terminal, @nt{loop_parameter_subtype_indication} to simplfy the wording.]}
2412
2413 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0250-1]}
2414 @ChgAdded{Version=[5],Text=[An @nt{iterator_filter} is now allowed on
2415 @nt{iterator_specification}s. This is mainly for consistency with
2416 aggregate and reduction iterators, where it eliminates the need for
2417 temporary objects.]}
2418 @end{Extend2012}
2419
2420 @begin{DiffWord2012}
2421 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
2422 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added wording to specify that
2423 a loop propagates any exceptions propagated by the execution of an iterator.
2424 Since that's what naturally would happen from a macro-style expansion of the
2425 parts of an iterator, and no other interpretation makes sense given the way
2426 the rest of Ada works, we consider it so unlikely that any Ada 2012
2427 implementation ever did anything else that we don't document this as a
2428 possible inconsistency.]}
2429
2430 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0111-1]}
2431 @ChgAdded{Version=[5],Text=[Added wording to include the use of the iterator
2432 view in a container element iterator.]}
2433
2434 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0266-1]}
2435 @ChgAdded{Version=[5],Text=[Added wording to describe the execution of
2436 parallel iterators.]}
2437 @end{DiffWord2012}
2438
2439
2440 @LabeledAddedSubClause{Version=[5],Name=[Procedural Iterators]}
2441
2442 @begin{Intro}
2443 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2444 @ChgAdded{Version=[5],Text=[A @nt{procedural_iterator} invokes a
2445 user-defined procedure, passing in the body of the enclosing @nt{loop_statement}
2446 as a parameter of an anonymous access-to-procedure type, to allow the loop body
2447 to be executed repeatedly as part of the invocation of the user-defined
2448 procedure.]}
2449 @end{Intro}
2450
2451 @begin{Syntax}
2452 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0250-1]}
2453 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<procedural_iterator>,Old=<>}>,
2454 rhs="@Chg{Version=[5],New=<
2455 @Syn2{iterator_parameter_specification} @key[of] @Syn2{iterator_procedure_call}
2456 [@Syn2{iterator_filter}]>,Old=<>}"}
2457
2458 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0308-1]}
2459 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<iterator_parameter_specification>,Old=<>}>,
2460 rhs="@Chg{Version=[5],New=<
2461 @Syn2{formal_part}
2462 | (@Syn2{defining_identifier}{, @Syn2{defining_identifier}})>,Old=<>}"}
2463
2464 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2465 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<iterator_procedure_call>,Old=<>}>,
2466 rhs="@Chg{Version=[5],New=<
2467 @SynI{procedure_}@Syn2{name}
2468 | @SynI{procedure_}@Syn2{prefix} @Syn2{iterator_actual_parameter_part}>,Old=<>}"}
2469
2470 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2471 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<iterator_actual_parameter_part>,Old=<>}>,
2472 rhs="@Chg{Version=[5],New=<
2473 (@Syn2{iterator_parameter_association} {, @Syn2{iterator_parameter_association}})>,Old=<>}"}
2474
2475 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2476 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<iterator_parameter_association>,Old=<>}>,
2477 rhs="@Chg{Version=[5],New=<
2478 @Syn2{parameter_association}
2479 | @Syn2{parameter_association_with_box}>,Old=<>}"}
2480
2481 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2482 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<parameter_association_with_box>,Old=<>}>,
2483 rhs="@Chg{Version=[5],New={
2484 [ @SynI{formal_parameter_}@Syn2{selector_name} => ] <>},Old=<>}"}
2485
2486 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2487 @ChgAdded{Version=[5],Text=[At most one @nt{iterator_parameter_association} within an
2488 @nt{iterator_actual_parameter_part} shall be a
2489 @nt{parameter_association_with_box}.]}
2490 @end{Syntax}
2491
2492 @begin{Resolution}
2493
2494 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0292-1],ARef=[AI12-0326-2]}
2495 @ChgAdded{Version=[5],Text=[The @nt{name} or @nt{prefix} given in an
2496 @nt{iterator_procedure_call} shall resolve to denote a callable entity @i<C>
2497 (the @i<iterating procedure>@Defn{iterating procedure})
2498 that is a procedure, or an entry renamed as (viewed as) a procedure.
2499 @Redundant[When there is an @nt{iterator_actual_parameter_part}, the @nt{prefix}
2500 can be an @nt{implicit_dereference} of an access-to-subprogram value.]]}
2501
2502 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2503 @ChgAdded{Version=[5],Text=[An @nt{iterator_procedure_call} without a
2504 @nt{parameter_association_with_box} is
2505 equivalent to one with an @nt{iterator_actual_parameter_part} with an
2506 additional @nt{parameter_association_with_box} at the end, with the
2507 @SynI{formal_parameter_}@nt{selector_name} identifying the last formal
2508 parameter of the callable entity denoted by the @nt{name} or @nt{prefix}.]}
2509
2510 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0320-1]}
2511 @ChgAdded{Version=[5],Text=[An @nt{iterator_procedure_call} shall contain at
2512 most one @nt{iterator_parameter_association} for each formal parameter of the
2513 callable entity @i<C>. Each formal parameter without an
2514 @nt{iterator_parameter_association} shall have a
2515 @nt{default_expression} (in the profile of the view of @i<C> denoted by the
2516 @nt{name} or @nt{prefix}).]]}
2517
2518 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2519 @ChgAdded{Version=[5],Text=[The formal parameter of the callable entity @i<C>
2520 associated with the @nt{parameter_association_with_box} shall be of an
2521 anonymous access-to-procedure type @i<A>.]}
2522 @end{Resolution}
2523
2524
2525 @begin{Legality}
2526
2527 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0308-1]}
2528 @ChgAdded{Version=[5],Text=[The anonymous access-to-procedure type @i<A> shall
2529 have at least one formal parameter in its parameter profile. If the
2530 @nt{iterator_parameter_specification} is a @nt{formal_part}, then this
2531 @nt{formal_part} shall be mode conformant with that of @i<A>. If the
2532 @nt{iterator_parameter_specification} is a list of @nt{defining_identifier}s,
2533 the number of formal parameters of @i<A> shall be the same as the length of
2534 this list.]}
2535
2536 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0292-1]}
2537 @ChgAdded{Version=[5],Text=[@Redundant[If the @nt{name} or @nt{prefix} given in an
2538 @nt{iterator_procedure_call} denotes an abstract subprogram, the subprogram
2539 shall be a dispatching subprogram.]]}
2540
2541 @begin{TheProof}
2542 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0320-1]}
2543 @ChgAdded{Version=[5],Text=[This is stated normatively in
2544 @RefSecNum{Abstract Types and Subprograms}.]}
2545 @end{TheProof}
2546
2547 @end{Legality}
2548
2549 @begin{StaticSem}
2550
2551 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0250-1],ARef=[AI12-0308-1],ARef=[AI12-0326-2]}
2552 @ChgAdded{Version=[5],Text=[A @nt{loop_statement} with an @nt{iteration_scheme}
2553 that has a @nt{procedural_iterator} is equivalent to a local declaration of a
2554 procedure P followed by a @nt{procedure_call_statement} that is formed from the
2555 @nt{iterator_procedure_call} by replacing the <> of the
2556 @nt{parameter_association_with_box} with P'Access. The @nt{formal_part} of the
2557 locally declared procedure P is formed from the @nt{formal_part} of the
2558 anonymous access-to-procedure type @i<A>, by replacing the @nt{identifier} of
2559 each formal parameter of this @nt{formal_part} with the @nt{identifier} of the
2560 corresponding formal parameter or element of the list of
2561 @nt{defining_identifier}s given in the @nt{iterator_parameter_specification}.
2562 The body of @i<P> consists of the conditionally executed
2563 @nt{sequence_of_statements}. The procedure P is called the
2564 @i<loop body procedure>.@Defn{loop body procedure}]}
2565
2566 @begin{ImplNote}
2567 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0250-1]}
2568 @ChgAdded{Version=[5],Type=[Leading],Text=[For a @nt{procedural_iterator}
2569 with an @nt{iterator_filter}, the body of the routine would be something like:]}
2570
2571 @begin{Example}
2572 @ChgRef{Version=[5],Kind=[AddedNormal]}
2573 @ChgAdded{Version=[5],Text=[@key{procedure} @i<P> ... @key{is}
2574 @key{begin}
2575 @key{if} @nt{iterator_filter} @key{then}
2576 @nt{sequence_of_statements}
2577 @key{end if};
2578 @key{end} @i<P>;]}
2579 @end{Example}
2580 @end{ImplNote}
2581
2582 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0326-2]}
2583 @ChgAdded{Version=[5],Type=[Leading],Text=[The following aspects may be specified
2584 for a callable entity @i<S> that has at least one formal parameter of an
2585 anonymous access-to-subprogram type:]}
2586
2587 @begin{Description}
2588 @ChgRef{Version=[5],Kind=[AddedNormal]}
2589 @ChgAdded{Version=[5],Text=[Allows_Exit@\The Allows_Exit aspect is of type
2590 Boolean. The specified value shall be static. The Allows_Exit aspect of an
2591 inherited primitive subprogram is True if Allows_Exit is True either for the
2592 corresponding subprogram of the progenitor type or for any other inherited
2593 subprogram that it overrides. If not specified or inherited as True, the
2594 Allows_Exit aspect of a callable entity is False. For an
2595 entry, only a confirming specification of False is permitted for the
2596 Allows_Exit aspect.]}
2597
2598 @begin{Reason}
2599 @ChgRef{Version=[5],Kind=[AddedNormal]}
2600 @ChgAdded{Version=[5],Text=[An entry does not allow exit, because
2601 implementing a transfer of control out of a task or protected entry creates
2602 unnecessarily complex dynamic semantics.]}
2603 @end{Reason}
2604
2605 @ChgRef{Version=[5],Kind=[AddedNormal]}
2606 @ChgAdded{Version=[5],NoPrefix=[T],Text=[Specifying the Allows_Exit aspect
2607 to be True for a subprogram indicates that the
2608 subprogram @i<allows exit>,@Defn2{Term=[subprogram],Sec=[allows exit]}@Defn{allows exit}
2609 meaning that it is prepared to be completed by arbitrary transfers of
2610 control from the loop body procedure@Redundant[, including propagation
2611 of exceptions. A subprogram for which
2612 Allows_Exit is True should use finalization as appropriate rather than
2613 exception handling to recover resources and make any necessary final
2614 updates to data structures].]}
2615
2616 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Allows_Exit],
2617 Text=[@ChgAdded{Version=[5],Text=[An indication of whether a subprogram
2618 will operate correctly for arbitrary transfers of control.]}]}
2619 @begin{Ramification}
2620 @ChgRef{Version=[5],Kind=[AddedNormal]}
2621 @ChgAdded{Version=[5],Text=[A subprogram that does not need cleanup
2622 satisfies the requirements, and thus can specify Allows_Exit as True.
2623 If a subprogram @i<S> allows exit, it cannot expect to get control other
2624 than via finalization if the loop body procedure initiates a transfer of
2625 control as part of a @nt{procedural_iterator}. In particular, exception
2626 handlers in @i<S>, even @key[when others] handlers, will not be executed
2627 when a transfer of control occurs. The mechanism that the implementation
2628 uses to implement such transfers of control needs to avoid triggering
2629 exception handlers.]}
2630 @end{Ramification}
2631
2632 @ChgRef{Version=[5],Kind=[AddedNormal]}
2633 @ChgAdded{Version=[5],Text=[Parallel_Iterator@\The Parallel_Iterator aspect
2634 is of type Boolean. The specified value shall be static. The
2635 Parallel_Iterator aspect of an inherited primitive subprogram is True if
2636 Parallel_Iterator is True either for the corresponding subprogram of the
2637 progenitor type or for any other inherited subprogram that it overrides. If
2638 not specified or inherited as True, the Parallel_Iterator aspect of a
2639 callable entity is False.]}
2640
2641 @ChgRef{Version=[5],Kind=[AddedNormal]}
2642 @ChgAdded{Version=[5],NoPrefix=[T],Text=[Specifying the Parallel_Iterator
2643 aspect to be True for a callable entity indicates that the entity might
2644 invoke the loop body procedure from multiple distinct logical threads of
2645 control. The Parallel_Iterator aspect for a subprogram shall be statically
2646 False if the subprogram allows exit.]}
2647
2648 @ChgAspectDesc{Version=[5],Kind=[AddedNormal],Aspect=[Parallel_Iterator],
2649 Text=[@ChgAdded{Version=[5],Text=[An indication of whether
2650 a subprogram may use multiple threads of control to invoke a
2651 loop body procedure.]}]}
2652
2653 @begin{Reason}
2654 @ChgRef{Version=[5],Kind=[AddedNormal]}
2655 @ChgAdded{Version=[5],Text=[Permitting exit from a parallel procedural
2656 iterator introduces additional semantic and implementation complexity.]}
2657 @end{Reason}
2658
2659 @end{Description}
2660 @end{StaticSem}
2661
2662 @begin{Legality}
2663
2664 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0326-2]}
2665 @ChgAdded{Version=[5],Text=[If a callable entity overrides an inherited
2666 dispatching subprogram that allows exit, the overriding callable entity also
2667 shall allow exit. If a callable entity overrides an inherited dispatching
2668 subprogram that has a True Parallel_Iterator aspect, the overriding callable
2669 entity also shall have a True Parallel_Iterator aspect.]}
2670
2671 @begin{Ramification}
2672 @ChgRef{Version=[5],Kind=[AddedNormal]}
2673 @ChgAdded{Version=[5],Text=[Since an entry never allows exit, attempting
2674 to implement an allows exit subprogram with a task or protected entry
2675 is always illegal. However, the Parallel_Iterator aspect can be
2676 applied to an entry, so a subprogram with the Parallel_Iterator aspect
2677 True can be implemented by an entry.]}
2678 @end{Ramification}
2679
2680 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0326-2]}
2681 @ChgAdded{Version=[5],Text=[A @nt{loop_statement} with a
2682 @nt{procedural_iterator} as its @nt{iteration_scheme}
2683 shall begin with the reserved word @key[parallel] if and only if the callable
2684 entity identified in the @nt{iterator_procedure_call} has a Parallel_iterator
2685 aspect of True.]}
2686
2687 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0326-2]}
2688 @ChgAdded{Version=[5],Text=[The @nt{sequence_of_statements} of a
2689 @nt{loop_statement} with a @nt{procedural_iterator} as its @nt{iteration_scheme}
2690 shall contain an @nt{exit_statement}, return statement, @nt{goto_statement}, or
2691 @nt{requeue_statement} that leaves the loop only if the callable entity
2692 associated with the @nt{procedural_iterator} allows exit.]}
2693
2694 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0294-1]}
2695 @ChgAdded{Version=[5],Text=[The @nt{sequence_of_statements} of a
2696 @nt{loop_statement} with a @nt{procedural_iterator} as its @nt{iteration_scheme}
2697 shall not contain an @nt{accept_statement} whose @nt{entry_declaration} occurs
2698 outside the @nt{loop_statement}.]}
2699
2700 @begin{Reason}
2701 @ChgRef{Version=[5],Kind=[AddedNormal]}
2702 @ChgAdded{Version=[5],Text=[An @nt{accept_statement} is not allowed in a
2703 procedure (see 9.5.2), it has to be directly in a @nt{task_body}. Since the
2704 loop body here is implemented as a procedure, we can't allow
2705 @nt{accept_statement}s there, either, even if the loop itself is directly in
2706 a @nt{task_body}.]}
2707 @end{Reason}
2708 @begin{Ramification}
2709 @ChgRef{Version=[5],Kind=[AddedNormal]}
2710 @ChgAdded{Version=[5],Text=[This includes cases where the
2711 @nt{accept_statement} is part of another construct, for instance, a
2712 @nt{select_statement}.]}
2713 @end{Ramification}
2714
2715 @end{Legality}
2716
2717 @begin{Runtime}
2718 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0326-2]}
2719 @ChgAdded{Version=[5],Text=[@Redundant[For the execution of a
2720 @nt{loop_statement} with an @nt{iteration_scheme} that has a
2721 @nt{procedural_iterator}, the procedure denoted by the @nt{name} or
2722 @nt{prefix} of the @nt{iterator_procedure_call} (the @i<iterating
2723 procedure>)@Defn{iterating procedure} is invoked, passing an access value
2724 designating the loop body procedure as a parameter. The iterating procedure
2725 then calls the loop body procedure zero or more times and returns, whereupon
2726 the @nt{loop_statement} is complete. If the @key[parallel] reserved word is
2727 present, the iterating procedure might invoke the loop body procedure from
2728 multiple distinct logical threads of control.]]}
2729
2730 @begin{TheProof}
2731 @ChgRef{Version=[5],Kind=[AddedNormal]}
2732 @ChgAdded{Version=[5],Text=[The stated dynamic semantics are implied by the
2733 static semantics given above and the bounded errors given below.]}
2734 @end{TheProof}
2735
2736 @end{Runtime}
2737
2738 @begin{Bounded}
2739 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0326-2]}
2740 @ChgAdded{Version=[5],Text=[@PDefn2{Term=(bounded error),Sec=(cause)}
2741 If the callable entity identified in the @nt{iterator_procedure_call} allows
2742 exit, then it is a bounded error for a call of the loop body procedure
2743 to be performed from within an abort-deferred operation (see
2744 @RefSecNum{Abort of a Task - Abort of a Sequence of Statements}),
2745 unless the entire @nt{loop_statement} was within the same abort-deferred
2746 operation. If detected, Program_Error is raised at the point of the
2747 call; otherwise, a transfer of control from the @nt{sequence_of_statements}
2748 of the @nt{loop_statement} might not terminate the @nt{loop_statement}, and the
2749 loop body procedure might be called
2750 again.@Defn2{Term=[Program_Error],Sec=(raised by detection of a bounded error)}]}
2751
2752 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0326-2]}
2753 @ChgAdded{Version=[5],Text=[If a @nt{loop_statement} with the
2754 @nt{procedural_iterator} as its @nt{iteration_scheme}
2755 (see @RefSecNum{Loop Statements}) does not begin with the reserved word
2756 @key[parallel], it is a bounded error if the loop body procedure is invoked
2757 from a different logical thread of control than the one that initiates
2758 the @nt{loop_statement}. If detected, Program_Error is raised; otherwise,
2759 conflicts associated with concurrent executions of the loop body
2760 procedure can occur without being detected by the applicable conflict
2761 check policy (see @RefSecNum{Conflict Check Policies}). Furthermore, propagating
2762 an exception or making an attempt to exit in the presence of multiple threads of
2763 control might not terminate the @nt{loop_statement}, deadlock might occur, or
2764 the loop body procedure might be called
2765 again.@Defn2{Term=[Program_Error],Sec=(raised by detection of a bounded error)}]}
2766
2767 @begin{Discussion}
2768 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0326-2]}
2769 @ChgAdded{Version=[5],Text=[Other Ada rules are still in effect for the
2770 allows exit subprogram @i<A>, of course. For instance, if a transfer of
2771 control causes finalization which raises an exception, Program_Error will be
2772 propagated by @i<A> (rather than the transfer of control). In such a case,
2773 the bounded error above would still apply. Another example is the case where
2774 an unrelated task is waiting on the normal completion of the loop body
2775 procedure call in @i<A>. Such a task might end up waiting forever if a
2776 transfer of control happens (this is a deadlock situation). This case does
2777 not require additional wording, as the same thing would happen if an
2778 exception is propagated from the loop body procedure or if @i<A> executed a
2779 transfer of control (such as a return statement).]}
2780 @end{Discussion}
2781
2782 @end{Bounded}
2783
2784 @begin{Examples}
2785
2786 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1],ARef=[AI12-0379-1]}
2787 @ChgAdded{Version=[5],Type=[Leading],Text=[Example of iterating over a map from
2788 My_Key_Type to My_Element_Type (see @RefSecNum{Maps}):]}
2789
2790 @begin{Example}
2791 @ChgRef{Version=[5],Kind=[AddedNormal]}
2792 @ChgAdded{Version=[5],Text=[@key[for] (C : Cursor) @key[of] My_Map.Iterate @key[loop]
2793 Put_Line (My_Key_Type'Image (Key (C)) & " => " &
2794 My_Element_Type'Image (Element (C)));
2795 @key[end loop];]}
2796
2797 @ChgRef{Version=[5],Kind=[AddedNormal]}
2798 @ChgAdded{Version=[5],Text=[--@Examcom{ The above is equivalent to:}]}
2799
2800 @ChgRef{Version=[5],Kind=[AddedNormal]}
2801 @ChgAdded{Version=[5],Text=[@key[declare]
2802 @key[procedure] P (C : Cursor) @key[is]
2803 @key[begin]
2804 Put_Line (My_Key_Type'Image (Key (c)) & " => " &
2805 My_Element_Type'Image (Element (C)));
2806 @key[end] P;
2807 @key[begin]
2808 My_Map.Iterate (P'Access);
2809 @key[end];]}
2810 @end{Example}
2811
2812 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0189-1]}
2813 @ChgAdded{Version=[5],Type=[Leading],Text=[Example of iterating over the
2814 environment variables (see @RefSecNum{The Package Environment_Variables}):]}
2815
2816 @begin{Example}
2817 @ChgRef{Version=[5],Kind=[AddedNormal]}
2818 @ChgAdded{Version=[5],Text=[@key[for] (Name, Val) @key[of] Ada.Environment_Variables.Iterate(<>) @key[loop]
2819 --@examcom{ "(<>)" is optional because it is the last parameter}
2820 Put_Line (Name & " => " & Val);
2821 @key[end loop];]}
2822
2823 @ChgRef{Version=[5],Kind=[AddedNormal]}
2824 @ChgAdded{Version=[5],Text=[--@Examcom{ The above is equivalent to:}]}
2825
2826 @ChgRef{Version=[5],Kind=[AddedNormal]}
2827 @ChgAdded{Version=[5],Text=[@key[declare]
2828 @key[procedure] P (Name : String; Val : String) @key[is]
2829 @key[begin]
2830 Put_Line (Name & " => " & Val);
2831 @key[end] P;
2832 @key[begin]
2833 Ada.Environment_Variables.Iterate (P'Access);
2834 @key[end];]}
2835 @end{Example}
2836 @end{Examples}
2837
2838 @begin{Extend2012}
2839 @ChgRef{Version=[5],Kind=[AddedNormal],
2840 ARef=[AI12-0189-1],ARef=[AI12-0292-1],ARef=[AI12-0294-1],ARef=[AI12-0326-2]}
2841 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}Procedural
2842 iterators, and the Allows_Exit and Parallel_Iterator aspects are
2843 new in Ada 202x.]}
2844 @end{Extend2012}
2845
2846
2847 @RMNewPageVer{Version=[0]}@Comment{For printed version of Ada 95}
2848 @RMNewPageVer{Version=[1]}@Comment{For printed version of Ada 95 + TC1 RM}
2849 @RMNewPageVer{Version=[2]}@Comment{For printed version of Ada 2005 RM}
2850 @LabeledClause{Block Statements}
2851
2852 @begin{Intro}
2853 @Redundant[A @nt{block_statement} encloses a
2854 @nt{handled_sequence_of_statements}
2855 optionally preceded by a @nt{declarative_part}.]
2856 @end{Intro}
2857
2858 @begin{Syntax}
2859 @Syn{lhs=<block_statement>,rhs="
2860 [@SynI{block_}@Syn2{statement_identifier}:]
2861 [@key{declare}
2862 @Syn2{declarative_part}]
2863 @key{begin}
2864 @Syn2{handled_sequence_of_statements}
2865 @key{end} [@SynI{block_}@Syn2{identifier}];"}
2866
2867 @begin(SyntaxText)
2868 If a @nt{block_statement} has a @SynI{block_}@nt{statement_identifier},
2869 then the @nt<identifier> shall be repeated after the @key{end};
2870 otherwise, there shall not be an @nt<identifier> after the @key{end}.
2871 @end(SyntaxText)
2872 @end{Syntax}
2873
2874 @begin{StaticSem}
2875 A @nt{block_statement} that has no
2876 explicit @nt{declarative_part} has an implicit empty
2877 @nt{declarative_part}.
2878 @begin{Ramification}
2879 Thus, other rules can always
2880 refer to the @nt{declarative_part} of a @nt<block_statement>.
2881 @end{Ramification}
2882 @end{StaticSem}
2883
2884 @begin{RunTime}
2885 @PDefn2{Term=[execution], Sec=(block_statement)}
2886 The execution of a @nt{block_statement} consists of the elaboration
2887 of its @nt{declarative_part} followed by the execution of
2888 its @nt{handled_sequence_of_statements}.
2889 @end{RunTime}
2890
2891 @begin{Examples}
2892 @Leading@keepnext@i{Example of a block statement with a local variable:}
2893 @begin{Example}
2894 Swap:
2895 @key[declare]
2896 Temp : Integer;
2897 @key[begin]
2898 Temp := V; V := U; U := Temp;
2899 @key[end] Swap;
2900 @end{Example}
2901 @begin{Ramification}
2902 If task objects are declared within a @nt{block_statement} whose execution
2903 is completed, the @nt{block_statement} is not left until all its dependent
2904 tasks are terminated
2905 (see @RefSecNum{Assignment and Finalization}).
2906 This rule applies to completion caused by a transfer of control.
2907
2908 Within a @nt{block_statement}, the block name can be used in expanded
2909 names denoting local entities such as Swap.Temp in the above example
2910 (see @RefSecNum{Selected Components}).
2911 @end{Ramification}
2912 @end{Examples}
2913
2914 @begin{DiffWord83}
2915 The syntax rule for @nt{block_statement} now uses the syntactic category
2916 @nt{handled_sequence_of_statements}.
2917 @end{DiffWord83}
2918
2919
2920 @LabeledAddedSubClause{Version=[5], Name=[Parallel Block Statements]}
2921
2922 @begin{Intro}
2923
2924 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2925 @ChgAdded{Version=[5],Text=[@Redundant[A @nt{parallel_block_statement}
2926 comprises two or more @nt{handled_sequence_of_statements} separated by
2927 @key[and] where each represents an independent activity that is intended to
2928 proceed concurrently with the others.]]}
2929
2930 @end{Intro}
2931
2932 @begin{Syntax}
2933
2934 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2935 @AddedSyn{Version=[5],lhs=<@Chg{Version=[5],New=<parallel_block_statement>,Old=<>}>,
2936 rhs="@Chg{Version=[5],New=<
2937 @key[parallel] @key[do]
2938 @Syn2{handled_sequence_of_statements}
2939 @key[and]
2940 @Syn2{handled_sequence_of_statements}
2941 {@key[and]
2942 @Syn2{handled_sequence_of_statements}}
2943 @key[end do];>,Old=<>}"}
2944
2945 @end{Syntax}
2946
2947 @begin{StaticSem}
2948
2949 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2950 @ChgAdded{Version=[5],Text=[
2951 Each @nt{handled_sequence_of_statements} represents a separate logical thread
2952 of control that proceeds independently and concurrently. The
2953 @nt{parallel_block_statement} is complete once every one of the
2954 @nt{handled_sequence_of_statements} has completed, either by reaching the end
2955 of its execution, or due to a transfer of control out of the construct
2956 by one of the @nt{handled_sequence_of_statements}
2957 (see @RefSecNum{Simple and Compound Statements - Sequences of Statements}).]}
2958
2959 @begin{ImplNote}
2960 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2961 @ChgAdded{Version=[5],Text=[Although each @nt{handled_sequence_of_statements}
2962 of a parallel block represents a separate logical thread of control, the
2963 implementation may choose to combine two or more such logical threads
2964 of control into a single physical thread of control to reduce the cost
2965 of creating numerous physical threads of control.]}
2966 @end{ImplNote}
2967 @end{StaticSem}
2968
2969 @begin{Examples}
2970 @begin{Example}
2971 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2972 @ChgAdded{Version=[5],Text=[@key[procedure] Traverse (T : Expr_Ptr) @key[is] --@Examcom{ see @RefSecNum{Tagged Types and Type Extensions}}
2973 @key[begin]
2974 @key[if] T /= @key[null] @key[and then]
2975 T.@key[all] @key[in] Binary_Operation'Class --@Examcom{ see @RefSecNum{Type Extensions}}
2976 @key[then] --@Examcom{ recurse down the binary tree}
2977 @key[parallel do]
2978 Traverse (T.Left);
2979 @key[and]
2980 Traverse (T.Right);
2981 @key[and]
2982 Ada.Text_IO.Put_Line
2983 ("Processing " & Ada.Tags.Expanded_Name (T'Tag));
2984 @key[end do];
2985 @key[end if];
2986 @key[end] Traverse;]}
2987
2988 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
2989 @ChgAdded{Version=[5],Text=[@key[function] Search (S : String; Char : Character) @key[return] Boolean @key[is]
2990 @key[begin]
2991 @key[if] S'Length <= 1000 @key[then]
2992 --@Examcom{ Sequential scan}
2993 @key[return] (@key[for] @key[some] C @key[of] S => C = Char);
2994 @key[else]
2995 --@Examcom{ Parallel divide and conquer}
2996 @key[declare]
2997 Mid : @key[constant] Positive := S'First + S'Length/2 - 1;
2998 @key[begin]
2999 @key[parallel do]
3000 @key[for] C @key[of] S(S'First .. Mid) @key[loop]
3001 @key[if] C = Char @key[then]
3002 @key[return] True; --@Examcom{ Terminates enclosing @key[do]}
3003 @key[end if];
3004 @key[end loop];
3005 @key[and]
3006 @key[for] C @key[of] S(Mid + 1 .. S'Last) @key[loop]
3007 @key[if] C = Char @key[then]
3008 @key[return] True; --@Examcom{ Terminates enclosing @key[do]}
3009 @key[end if];
3010 @key[end loop];
3011 @key[end do];
3012 --@Examcom{ Not found}
3013 @key[return] False;
3014 @key[end];
3015 @key[end if];
3016 @key[end] Search;]}
3017 @end{Example}
3018 @end{Examples}
3019
3020 @begin{Extend2012}
3021 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0119-1]}
3022 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}
3023 The @nt{parallel_block_statement} is new.]}
3024 @end{Extend2012}
3025
3026
3027 @LabeledClause{Exit Statements}
3028
3029 @begin{Intro}
3030 @Redundant[An @nt{exit_statement} is used to complete the execution
3031 of an enclosing @nt{loop_statement}; the
3032 completion is conditional if the @nt{exit_statement} includes a
3033 @nt{condition}.]
3034 @end{Intro}
3035
3036 @begin{Syntax}
3037 @Syn{lhs=<exit_statement>,rhs="
3038 @key{exit} [@SynI{loop_}@Syn2{name}] [@key{when} @Syn2{condition}];"}
3039 @end{Syntax}
3040
3041 @begin{Resolution}
3042 The @i(loop_)@nt{name}, if any, in an @nt{exit_statement} shall resolve to
3043 denote a @nt{loop_statement}.
3044 @end{Resolution}
3045
3046 @begin{Legality}
3047 @Defn2{Term=[apply], Sec=(to a @nt{loop_statement} by an @nt{exit_statement})}
3048 Each @nt{exit_@!statement} @i{applies to} a
3049 @nt{loop_@!statement}; this is the @nt{loop_@!statement} being exited.
3050 An @nt{exit_@!statement} with a @nt{name} is only allowed within the
3051 @nt{loop_@!statement} denoted by the @nt{name},
3052 and applies to that @nt{loop_@!statement}.
3053 An @nt{exit_@!statement} without a @nt{name} is only allowed within a
3054 @nt{loop_@!statement}, and applies to the innermost enclosing one.
3055 An @nt{exit_@!statement} that applies to a given @nt{loop_@!statement}
3056 shall not appear within a body or @nt{accept_@!statement}, if
3057 this construct is itself enclosed by the given @nt{loop_statement}.
3058 @end{Legality}
3059
3060 @begin{RunTime}
3061 @PDefn2{Term=[execution], Sec=(exit_statement)}
3062 For the execution of an @nt{exit_statement}, the @nt{condition}, if
3063 present, is first evaluated.
3064 If the value of the @nt{condition} is True, or if there is no @nt{condition},
3065 a transfer of control is done to complete the @nt{loop_@!statement}.
3066 If the value of the @nt{condition} is False, no transfer of control takes
3067 place.
3068 @end{RunTime}
3069
3070 @begin{Notes}
3071 Several nested loops can be exited by an @nt{exit_statement} that names
3072 the outer loop.
3073 @end{Notes}
3074
3075 @begin{Examples}
3076 @i{Examples of loops with exit statements:}
3077 @begin{Example}
3078 @key[for] N @key[in] 1 .. Max_Num_Items @key[loop]
3079 Get_New_Item(New_Item);
3080 Merge_Item(New_Item, Storage_File);
3081 @key[exit] @key[when] New_Item = Terminal_Item;
3082 @key[end] @key[loop];
3083
3084 Main_Cycle:
3085 @key[loop]
3086 --@RI{ initial statements}
3087 @key[exit] Main_Cycle @key[when] Found;
3088 --@RI{ final statements}
3089 @key[end] @key[loop] Main_Cycle;
3090 @end{Example}
3091 @end{Examples}
3092
3093 @LabeledClause{Goto Statements}
3094
3095 @begin{Intro}
3096 @Redundant[A @nt{goto_statement} specifies an explicit transfer of control
3097 from this @nt{statement} to a target
3098 statement with a given label.]
3099 @end{Intro}
3100
3101 @begin{Syntax}
3102 @Syn{lhs=<goto_statement>,rhs="@key{goto} @SynI{label_}@Syn2{name};"}
3103 @end{Syntax}
3104
3105 @begin{Resolution}
3106 @Defn2{Term=[target statement], Sec=(of a @nt{goto_statement})}
3107 The @i(label_)@nt{name} shall resolve to denote a @nt<label>;
3108 the @nt{statement} with that @nt{label} is the @i(target statement).
3109 @end{Resolution}
3110
3111 @begin{Legality}
3112 The innermost @nt{sequence_of_statements} that encloses the target
3113 statement shall also enclose the @nt{goto_statement}.
3114 Furthermore, if a @nt{goto_statement} is enclosed by an
3115 @nt{accept_statement} or a body, then the target
3116 statement shall not be outside this enclosing construct.
3117 @begin{Ramification}
3118 The @nt{goto_statement} can be a @nt{statement} of an inner
3119 @ntf{sequence_}.
3120
3121 It follows from the second rule that if the target @nt{statement}
3122 is enclosed by such a construct, then the @nt{goto_statement}
3123 cannot be outside.
3124 @end{Ramification}
3125 @end{Legality}
3126
3127 @begin{RunTime}
3128 @PDefn2{Term=[execution], Sec=(goto_statement)}
3129 The execution of a @nt{goto_statement} transfers control to the
3130 target statement, completing the execution
3131 of any @nt<compound_statement> that encloses the @nt<goto_statement>
3132 but does not enclose the target.
3133 @end{RunTime}
3134
3135 @begin{Notes}
3136 The above rules allow transfer of control to a @nt{statement} of an
3137 enclosing @nt{sequence_of_statements} but not the reverse. Similarly,
3138 they prohibit transfers of control such as between alternatives of a
3139 @nt{case_statement}, @nt{if_statement}, or @nt{select_statement};
3140 between @nt{exception_handler}s; or from an @nt{exception_handler} of
3141 a @nt{handled_sequence_of_statements}
3142 back to its @nt{sequence_of_statements}.
3143 @end{Notes}
3144
3145 @begin{Examples}
3146 @Leading@keepnext@i{Example of a loop containing a goto statement:}
3147 @begin{Example}
3148 <<Sort>>
3149 @key[for] I @key[in] 1 .. N-1 @key[loop]
3150 @key[if] A(I) > A(I+1) @key[then]
3151 Exchange(A(I), A(I+1));
3152 @key[goto] Sort;
3153 @key[end] @key[if];
3154 @key[end] @key[loop];
3155 @end{Example}
3156 @end{Examples}