Debian Codegraph @

source_2012/05.mss

raw
1 @Part(05, Root="ada.mss")
2
3 @Comment{$Date: 2016/02/09 04:55:40 $}
4 @LabeledSection{Statements}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/05.mss,v $}
7 @Comment{$Revision: 1.65 $}
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 @Redundant[A @nt<statement> is either simple or compound.
45 A @nt<simple_statement> encloses
46 no other @nt<statement>. A @nt<compound_statement> can enclose
47 @nt<simple_statement>s and other @nt<compound_statement>s.]
48 @end{Intro}
49
50 @begin{Syntax}
51 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0179-1]}
52 @Syn{lhs=<sequence_of_statements>,rhs="@Syn2{statement} {@Syn2{statement}}@Chg{Version=[3],New=[ {@Syn2{label}}],Old=[]}"}
53
54
55 @Syn{lhs=<statement>,rhs="
56 {@Syn2{label}} @Syn2{simple_statement} | {@Syn2{label}} @Syn2{compound_statement}"}
57
58 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
59 @Syn{tabs=[P31], lhs=<simple_statement>,rhs="@Syn2{null_statement}
60 | @Syn2{assignment_statement}@\| @Syn2{exit_statement}
61 | @Syn2{goto_statement}@\| @Syn2{procedure_call_statement}
62 | @Chg{Version=[2],New=[@Syn2{simple_return_statement}],Old=[@Syn2{return_statement}]}@\| @Syn2{entry_call_statement}
63 | @Syn2{requeue_statement}@\| @Syn2{delay_statement}
64 | @Syn2{abort_statement}@\| @Syn2{raise_statement}
65 | @Syn2{code_statement}"}
66
67 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
68 @Syn{tabs=[P31], lhs=<compound_statement>,rhs="
69 @Syn2{if_statement}@\| @Syn2{case_statement}
70 | @Syn2{loop_statement}@\| @Syn2{block_statement}@Chg{Version=[2],New=[
71 | @Syn2{extended_return_statement}],Old=[]}
72 | @Syn2{accept_statement}@\| @Syn2{select_statement}"}
73
74 @Syn{lhs=<null_statement>,rhs="@key{null};"}
75
76 @Syn{lhs=<label>,rhs="<<@SynI{label_}@Syn2{statement_identifier}>>"}
77
78 @Syn{lhs=<statement_identifier>,rhs="@Syn2{direct_name}"}
79
80 @begin(SyntaxText)
81 The @nt<direct_name> of a @nt<statement_identifier> shall
82 be an @nt<identifier> (not an @nt<operator_symbol>).
83 @end(SyntaxText)
84 @end{Syntax}
85
86 @begin{Resolution}
87 The @nt<direct_name> of a @nt<statement_identifier> shall resolve to
88 denote its corresponding implicit declaration (see below).
89 @end{Resolution}
90
91 @begin{Legality}
92 Distinct @nt{identifier}s shall be used for all
93 @nt<statement_identifier>s that
94 appear in the same body, including
95 inner @nt{block_statement}s
96 but excluding inner program units.
97 @end{Legality}
98
99 @begin{StaticSem}
100 For each @nt<statement_identifier>,
101 there is an implicit declaration (with the specified @nt<identifier>)
102 at the end of the @nt{declarative_part} of the
103 innermost @nt{block_statement} or body that
104 encloses the @nt{statement_identifier}.
105 The implicit declarations occur in the same order as the
106 @nt<statement_identifier>s occur in the source text.
107 If a usage name denotes such an implicit declaration, the entity it
108 denotes is the @nt<label>, @nt<loop_statement>,
109 or @nt<block_statement> with the given @nt<statement_identifier>.
110 @begin{Reason}
111 We talk in terms of individual @nt<statement_identifier>s here
112 rather than in terms of the corresponding statements, since
113 a given @nt{statement} may have multiple @nt<statement_identifier>s.
114
115 A @nt{block_statement} that has no
116 explicit @nt{declarative_part} has an implicit empty
117 @nt{declarative_part},
118 so this rule can safely
119 refer to the @nt{declarative_part} of a @nt<block_statement>.
120
121 The scope of a declaration starts at the place of the declaration
122 itself (see @RefSecNum{Scope of Declarations}).
123 In the case of a label, loop, or block name, it
124 follows from this rule that the scope of the implicit declaration
125 starts before the first explicit occurrence of the corresponding
126 name, since this occurrence is either in a statement label, a
127 @nt{loop_statement}, a @nt{block_statement}, or a
128 @nt{goto_statement}. An implicit
129 declaration in a @nt{block_statement} may hide a declaration given in an
130 outer program unit or @nt{block_statement} (according to the usual rules
131 of hiding explained in @RefSecNum{Visibility}).
132
133 The syntax rule for @nt{label} uses @nt{statement_identifier} which
134 is a @nt<direct_name> (not a @nt{defining_identifier}),
135 because labels are implicitly declared. The same applies to loop and
136 block names.
137 In other words, the @nt{label} itself is not the defining occurrence;
138 the implicit declaration is.
139
140 @Leading@;We cannot consider the @nt{label} to be a defining occurrence.
141 An example that can tell the difference is this:
142 @begin{example}
143 @key[declare]
144 --@RI{ Label Foo is implicitly declared here.}
145 @key[begin]
146 @key[for] Foo @key[in] ... @key[loop]
147 ...
148 <<Foo>> --@RI{ Illegal.}
149 ...
150 @key[end] @key[loop];
151 @key[end];
152 @end{example}
153
154 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
155 The label in this example is hidden from itself by the loop parameter
156 with the same name;
157 the example is illegal.
158 We considered creating a new syntactic category name, separate from
159 @nt{direct_name} and @nt{selector_name}, for use in the case of statement
160 labels.
161 However, that would confuse the rules in @Chg{Version=[3],New=[Clause],Old=[Section]}
162 8, so we didn't do it.
163 @end{Reason}
164
165 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0179-1]}
166 @ChgAdded{Version=[3],Text=[If one or more @nt{label}s end a
167 @nt{sequence_of_statements}, an implicit @nt{null_statement}
168 follows the @nt{label}s before any following constructs.]}
169
170 @begin{Reason}
171 @ChgRef{Version=[3],Kind=[Added]}
172 @ChgAdded{Version=[3],Text=[The semantics of a @nt{goto_statement} is
173 defined in terms of the statement having (following) that label. Thus
174 we ensure that every label has a following statement, which might be
175 implicit.]}
176 @end{Reason}
177 @end{StaticSem}
178
179 @begin{RunTime}
180 @PDefn2{Term=[execution], Sec=(null_statement)}
181 The execution of a @nt{null_statement} has no effect.
182
183 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]}
184 @Defn{transfer of control}
185 A @i{transfer of control} is
186 the run-time action of an @nt{exit_statement},
187 @Chg{Version=[2],New=[return statement],Old=[@nt{return_statement}]},
188 @nt{goto_statement},
189 or @nt{requeue_statement},
190 selection of a @nt{terminate_alternative},
191 raising of an exception,
192 or an abort,
193 which causes
194 the next action performed to be one other than what would normally be
195 expected from the other rules of the language.
196 @Redundant[As explained in
197 @RefSecNum{Completion and Finalization},
198 a transfer of control can cause the execution of constructs to be
199 completed and then left,
200 which may trigger finalization.]
201
202 @PDefn2{Term=[execution], Sec=(sequence_of_statements)}
203 The execution of a @nt{sequence_of_statements} consists of the execution
204 of the individual @nt{statement}s in succession
205 until the @ntf{sequence_} is completed.
206 @begin{Ramification}
207 It could be completed by reaching the end of it,
208 or by a transfer of control.
209 @end{Ramification}
210 @end{RunTime}
211
212 @begin{Notes}
213 A @nt<statement_identifier> that appears immediately within
214 the declarative region of a
215 named @nt<loop_statement> or an @nt<accept_statement> is nevertheless
216 implicitly declared immediately within the declarative region
217 of the innermost enclosing body or @nt<block_statement>;
218 in other words, the expanded name for a named statement is
219 not affected by whether the statement occurs inside or outside
220 a named loop or an @nt<accept_statement> @em only nesting
221 within @nt<block_statement>s is relevant to the form of its
222 expanded name.
223 @begin{Discussion}
224 @Leading@keepnext@;Each comment in the following example gives the
225 expanded name associated with an entity declared in the task body:
226 @begin{Example}
227 @key(task body) Compute @key(is)
228 Sum : Integer := 0; --@RI[ Compute.Sum]
229 @key(begin)
230 Outer: --@RI[ Compute.Outer]
231 @key(for) I @key(in) 1..10 @key(loop) --@RI[ Compute.Outer.I]
232 Blk: --@RI[ Compute.Blk]
233 @key(declare)
234 Sum : Integer := 0; --@RI[ Compute.Blk.Sum]
235 @key(begin)
236 @key(accept) Ent(I : out Integer; J : in Integer) @key(do)
237 --@RI[ Compute.Ent.I, Compute.Ent.J]
238 Compute.Ent.I := Compute.Outer.I;
239 Inner: --@RI[ Compute.Blk.Inner]
240 @key(for) J @key(in) 1..10 @key(loop)
241 --@RI[ Compute.Blk.Inner.J]
242 Sum := Sum + Compute.Blk.Inner.J * Compute.Ent.J;
243 @key(end loop) Inner;
244 @key(end) Ent;
245 Compute.Sum := Compute.Sum + Compute.Blk.Sum;
246 @key(end) Blk;
247 @key(end loop) Outer;
248 Record_Result(Sum);
249 @key(end) Compute;
250 @end{Example}
251 @end{Discussion}
252 @end{Notes}
253
254 @begin{Examples}
255 @Leading@keepnext@i{Examples of labeled statements:}
256 @begin{Example}
257 <<Here>> <<Ici>> <<Aqui>> <<Hier>> @key[null];
258
259 <<After>> X := 1;
260 @end{Example}
261 @end{Examples}
262
263 @begin{Extend83}
264 @Defn{extensions to Ada 83}
265 The @nt{requeue_statement} is new.
266 @end{Extend83}
267
268 @begin{DiffWord83}
269 We define the syntactic category @nt<statement_identifier> to simplify
270 the description. It is used for labels, loop names, and block names.
271 We define the entity associated with the implicit declarations
272 of statement names.
273
274 Completion includes completion caused by a transfer of control,
275 although RM83-5.1(6) did not take this view.
276 @end{DiffWord83}
277
278 @begin{Extend95}
279 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
280 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
281 The @nt{extended_return_statement} is new (@nt{simple_return_statement}
282 is merely renamed).]}
283 @end{Extend95}
284
285 @begin{Extend2005}
286 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI95-0179-1]}
287 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
288 A @nt{label} can end a @nt{sequence_of_statements},
289 eliminating the requirement for having an explicit @key[null]; statement
290 after an ending label (a common use).]}
291 @end{Extend2005}
292
293
294 @LabeledClause{Assignment Statements}
295
296 @begin{Intro}
297 @Redundant[An @nt{assignment_statement}
298 replaces the current value of
299 a variable with the result of evaluating an
300 @nt<expression>.]
301 @end{Intro}
302
303 @begin{Syntax}
304 @Syn{lhs=<assignment_statement>,rhs="
305 @SynI{variable_}@Syn2{name} := @Syn2{expression};"}
306 @end{Syntax}
307
308 @begin{Intro}
309 The execution of an @nt<assignment_statement> includes
310 the evaluation of the @nt<expression> and the @i(assignment)
311 of the value of the @nt<expression> into the @i(target).
312 @RootDefn{assignment operation}
313 @IndexSee{Term=[assign], See=(assignment operation)}
314 @Redundant[An assignment operation (as opposed to
315 an @nt<assignment_@!statement>) is performed in other contexts
316 as well, including object initialization and by-copy parameter
317 passing.]
318 @Defn2{Term=[target], Sec=(of an assignment operation)}
319 @Defn2{Term=[target], Sec=(of an @nt{assignment_statement})}
320 The @i{target} of an assignment operation
321 is the view of the object to which a value is being assigned;
322 the target of an @nt{assignment_@!statement} is the variable denoted by
323 the @SynI{variable_}@nt{name}.
324 @begin{Discussion}
325 Don't confuse this notion of the @lquotes@;target@rquotes@; of an assignment
326 with the notion of the @lquotes@;target object@rquotes@; of an entry call or requeue.
327
328 Don't confuse the term @lquotes@;assignment operation@rquotes@; with the
329 @nt{assignment_statement}.
330 The assignment operation is just one part of the execution of an
331 @nt{assignment_statement}.
332 The assignment operation is also a part of the execution of various
333 other constructs; see @RefSec{Completion and Finalization} for a complete
334 list.
335 Note that when we say, @lquotes@;such-and-such is assigned to so-and-so@rquotes@;,
336 we mean that the assignment operation is being applied, and that
337 so-and-so is the target of the assignment operation.
338 @end{Discussion}
339 @end{Intro}
340
341 @begin{Resolution}
342 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
343 @PDefn2{Term=[expected type],
344 Sec=(assignment_statement variable_name)}
345 The @i(variable_)@nt<name> of an @nt<assignment_statement>
346 is expected to be of any @Chg{Version=[2],New=[],Old=[nonlimited ]}type.
347 @PDefn2{Term=[expected type],
348 Sec=(assignment_statement expression)}
349 The expected type for the @nt<expression> is
350 the type of the target.
351 @begin{ImplNote}
352 @Leading@keepnext@;An @nt<assignment_statement> as a whole is a "complete context,"
353 so if the @i{variable_}@nt<name> of an @nt<assignment_statement> is
354 overloaded, the @nt<expression> can be used to help disambiguate it.
355 For example:
356 @begin{Example}
357 @key[type] P1 @key[is access] R1;
358 @key[type] P2 @key[is access] R2;
359
360 @key[function] F return P1;
361 @key[function] F return P2;
362
363 X : R1;
364 @key[begin]
365 F.all := X; --@RI[ Right hand side helps resolve left hand side]
366 @end{Example}
367 @end{ImplNote}
368 @end{Resolution}
369
370 @begin{Legality}
371 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
372 The target @Redundant[denoted by the
373 @i(variable_)@nt<name>] shall be a variable@Chg{Version=[2],New=[ of a
374 nonlimited type],Old=[]}.
375
376 If the target is of a tagged class-wide type @i(T)'Class, then
377 the @nt<expression> shall either be dynamically tagged,
378 or of type @i(T) and tag-indeterminate
379 (see @RefSecNum{Dispatching Operations of Tagged Types}).
380 @begin{Reason}
381 This is consistent with the general rule that a single
382 dispatching operation shall not have both dynamically tagged and
383 statically tagged operands. Note that for an object
384 initialization (as opposed to the @nt{assignment_statement}),
385 a statically tagged initialization expression is permitted,
386 since there is no chance for confusion (or Tag_Check failure).
387 Also, in an object initialization, tag-indeterminate expressions
388 of any type covered by @i(T)'Class would be allowed, but with
389 an @nt{assignment_statement}, that might not work if the tag of the target
390 was for a type that didn't have one of the dispatching operations
391 in the tag-indeterminate expression.
392 @end{Reason}
393 @end{Legality}
394
395 @begin{RunTime}
396 @PDefn2{Term=[execution], Sec=(assignment_statement)}
397 For the execution of an @nt{assignment_statement},
398 the @i(variable_)@nt<name> and the @nt<expression>
399 are first evaluated in an arbitrary order.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
400 @begin{Ramification}
401 Other rules of the language may require that the
402 bounds of the variable be determined prior to evaluating
403 the @nt<expression>, but that does not necessarily require
404 evaluation of the @i(variable_)@nt<name>, as pointed out by the ACID.
405 @end{Ramification}
406
407 @Leading@keepnext@;When the type of the target is class-wide:
408 @begin(itemize)
409 @PDefn2{Term=[controlling tag value], Sec=(for the @nt{expression} in an @nt{assignment_statement})}
410 If the @nt<expression> is tag-indeterminate
411 (see @RefSecNum{Dispatching Operations of Tagged Types}), then the controlling
412 tag value for the @nt<expression> is the tag of the target;
413 @begin{Ramification}
414 See @RefSec(Dispatching Operations of Tagged Types).
415 @end{Ramification}
416
417 @IndexCheck{Tag_Check}
418 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
419 Otherwise @Redundant[(the @nt<expression> is dynamically tagged)],
420 a check is made that the tag of
421 the value of the @nt<expression>
422 is the same as that of the target;
423 if this check fails, Constraint_Error is raised.
424 @end(itemize)
425
426 The value of the @nt<expression> is converted to the subtype of the
427 target. @Redundant[The conversion might raise an exception
428 (see @RefSecNum{Type Conversions}).]
429 @PDefn2{Term=[implicit subtype conversion],Sec=(assignment_statement)}
430 @begin{Ramification}
431 @RefSec(Type Conversions) defines what actions
432 and checks are associated with subtype conversion.
433 For non-array subtypes, it is just a constraint
434 check presuming the types match.
435 For array subtypes, it checks the lengths and slides if the
436 target is constrained.
437 @lquotes@;Sliding@rquotes@; means the array doesn't have to have the same bounds,
438 so long as it is the same length.
439 @end{Ramification}
440
441 In cases involving controlled types, the target is finalized,
442 and an anonymous object might be used as an intermediate in the assignment,
443 as described in @RefSec{Completion and Finalization}.
444 @Defn{assignment operation}
445 @Defn2{Term=[assignment operation],
446 Sec=(during execution of an @nt{assignment_statement})}
447 In any case,
448 the converted value of the @nt<expression> is then @i(assigned) to the target,
449 which consists of the following two steps:
450 @begin{Honest}
451 @RefSecNum{Completion and Finalization} actually says that
452 finalization happens always, but unless controlled types are involved,
453 this finalization during an @nt{assignment_statement} does
454 nothing.
455 @end{Honest}
456 @begin(itemize)
457 The value of the target becomes the converted value.
458
459 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
460 If any part of the target is controlled, its value
461 is adjusted as explained in @Chg{Version=[3],New=[subclause],Old=[clause]}
462 @RefSecNum{Assignment and Finalization}.
463 @PDefn2{Term=[adjustment], Sec=(as part of assignment)}
464 @begin{Ramification}
465 If any parts of the object are controlled,
466 abort is deferred during the assignment operation itself,
467 but not during the rest of the execution of an
468 @nt<assignment_statement>.
469 @end{Ramification}
470 @end(itemize)
471
472 @end{RunTime}
473
474 @begin{Notes}
475 The tag of an object never changes;
476 in particular, an
477 @nt{assignment_statement}
478 does not change the tag of the target.
479
480 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00363-01]}
481 @ChgDeleted{Version=[2],Text=[The values of the discriminants of an object
482 designated by an access value cannot be changed (not even by assigning a
483 complete value to the object itself) since such objects are always constrained;
484 however, subcomponents of such objects may be unconstrained.]}
485 @begin{Ramification}
486 The implicit subtype conversion described above for
487 @nt{assignment_statement}s
488 is performed only for the value of the right-hand side
489 expression as a whole; it is not performed for subcomponents of the
490 value.
491
492 The determination of the type of the variable of an
493 @nt{assignment_statement} may require consideration of the expression
494 if the variable
495 name can be interpreted as the name of a variable designated by the
496 access value returned by a function call, and similarly, as a
497 component or slice of such a variable
498 (see @RefSec{The Context of Overload Resolution}).
499 @end{Ramification}
500 @end{Notes}
501
502 @begin{Examples}
503 @Leading@keepnext@i{Examples of assignment statements:}
504 @begin{Example}
505 Value := Max_Value - 1;
506 Shade := Blue;
507
508 Next_Frame(F)(M, N) := 2.5; --@RI{ see @RefSecNum{Indexed Components}}
509 U := Dot_Product(V, W); --@RI{ see @RefSecNum{Subprogram Bodies}}
510
511 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0056-1]}
512 Writer := (Status => Open, Unit => Printer, Line_Count => 60); --@RI{ see @RefSecNum{Variant Parts and Discrete Choices}}
513 @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}}
514 @end{Example}
515
516 @begin{WideAbove}
517 @Leading@keepnext@i{Examples involving scalar subtype conversions:}
518 @end{WideAbove}
519 @begin{Example}
520 I, J : Integer @key[range] 1 .. 10 := 5;
521 K : Integer @key[range] 1 .. 20 := 15;
522 ...
523
524 I := J; --@RI{ identical ranges}
525 K := J; --@RI{ compatible ranges}
526 J := K; --@RI{ will raise Constraint_Error if K > 10}
527 @end{Example}
528
529 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
530 @begin{WideAbove}
531 @leading@keepnext@i{Examples involving array subtype conversions:}
532 @end{WideAbove}
533 @begin{Example}
534 A : String(1 .. 31);
535 B : String(3 .. 33);
536 ...
537
538 A := B; --@RI{ same number of components}
539
540 A(1 .. 9) := "tar sauce";
541 A(4 .. 12) := A(1 .. 9); --@RI{ A(1 .. 12) = "tartar sauce"}
542 @end{Example}
543 @end{Examples}
544
545 @begin{Notes}
546 @i{Notes on the examples:}
547 @nt{Assignment_statement}s are allowed even in the case of overlapping
548 slices of the same array,
549 because the @SynI{variable_}@nt{name} and @nt{expression}
550 are both evaluated before copying the value into the variable.
551 In the above example, an
552 implementation yielding A(1 .. 12) = "tartartartar" would be
553 incorrect.
554 @end{Notes}
555
556 @begin{Extend83}
557 @Defn{extensions to Ada 83}
558 We now allow user-defined finalization and value adjustment actions
559 as part of @nt{assignment_statement}s
560 (see @RefSec{Assignment and Finalization}).
561 @end{Extend83}
562
563 @begin{DiffWord83}
564 The special case of array assignment is subsumed by the concept
565 of a subtype conversion, which is applied for all kinds of types,
566 not just arrays. For arrays it provides @lquotes@;sliding@rquotes@;. For numeric
567 types it provides conversion of a value of a universal type to
568 the specific type of the target. For other types,
569 it generally has no run-time effect, other than a constraint
570 check.
571
572 We now cover in a general way in @RefSecNum{Operations of Discriminated Types}
573 the erroneous execution possible due to
574 changing the value of a discriminant when
575 the variable in an @nt<assignment_statement> is a subcomponent
576 that depends on discriminants.
577 @end{DiffWord83}
578
579 @begin{Incompatible95}
580 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
581 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
582 The change of the limited check from a resolution rule to
583 a legality rule is not quite upward compatible. For example]}
584 @begin{Example}
585 @ChgRef{Version=[2],Kind=[AddedNormal]}
586 @ChgAdded{Version=[2],Text=[@key{type} AccNonLim @key{is} @key{access} NonLim;
587 @key{function} Foo (Arg : in Integer) @key{return} AccNonLim;
588 @key{type} AccLim @key{is} @key{access} Lim;
589 @key{function} Foo (Arg : in Integer) @key{return} AccLim;
590 Foo(2).@key{all} := Foo(1).@key{all};]}
591 @end{Example}
592 @ChgRef{Version=[2],Kind=[AddedNormal]}
593 @ChgAdded{Version=[2],Text=[where NonLim is a nonlimited type and Lim is a
594 limited type. The assignment is legal in Ada 95 (only the first Foo would be
595 considered), and is ambiguous in Ada 2005. We made the change because we want
596 limited types to be as similar to nonlimited types as possible. Limited
597 expressions are now allowed in all other contexts (with a similar
598 incompatibility), and it would be odd if assignments had different resolution
599 rules (which would eliminate ambiguities in some cases). Moreover, examples
600 like this one are rare, as they depend on assigning into overloaded function
601 calls.]}
602 @end{Incompatible95}
603
604
605 @LabeledClause{If Statements}
606
607 @begin{Intro}
608 @Redundant[An @nt{if_statement} selects for execution at most one of
609 the enclosed @ntf{sequences_of_statements}, depending on the (truth)
610 value of one or more corresponding @nt{condition}s.]
611 @end{Intro}
612
613 @begin{Syntax}
614 @Syn{lhs=<if_statement>,rhs="
615 @key{if} @Syn2{condition} @key{then}
616 @Syn2{sequence_of_statements}
617 {@key{elsif} @Syn2{condition} @key{then}
618 @Syn2{sequence_of_statements}}
619 [@key{else}
620 @Syn2{sequence_of_statements}]
621 @key{end} @key{if};"}
622
623
624 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0147-1]}
625 @DeletedSyn{Version=[3],LHS=<@Chg{Version=[3],New=[],Old=[condition]}>,
626 RHS="@Chg{Version=[3],New=[],Old=[@SynI{boolean_}@Syn2{expression}]}"}
627 @end{Syntax}
628
629 @begin{Resolution}
630 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0147-1]}
631 @ChgDeleted{Version=[3],Text=[@PDefn2{Term=[expected type], Sec=(condition)}
632 A @nt{condition} is expected to be of any boolean type.]}
633 @end{Resolution}
634 @begin{NotIso}
635 @ChgAdded{Version=[3],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 3 and 4
636 were deleted.>}]}@Comment{This message should be
637 deleted if the paragraphs are ever renumbered.}
638 @end{NotIso}
639
640 @begin{RunTime}
641 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
642 @PDefn2{Term=[execution], Sec=(if_statement)}
643 For the execution of an @nt{if_statement}, the @nt{condition} specified
644 after @key{if}, and any @nt{condition}s specified after @key{elsif}, are
645 evaluated in succession (treating a final @key{else} as @key{elsif} True
646 @key{then}), until one evaluates to True or
647 all @nt{condition}s are evaluated and
648 yield False.
649 If a @nt{condition} evaluates to True, then the
650 corresponding @nt{sequence_of_statements} is executed;
651 otherwise@Chg{Version=[3],New=[,],Old=[]}
652 none of them is executed.
653 @begin{Ramification}
654 The part about all evaluating to False can't happen if
655 there is an @key{else}, since that is herein considered equivalent to
656 @key{elsif} True @key{then}.
657 @end{Ramification}
658 @end{RunTime}
659
660 @begin{Examples}
661 @Leading@keepnext@i{Examples of if statements:}
662 @begin{Example}
663 @key[if] Month = December @key[and] Day = 31 @key[then]
664 Month := January;
665 Day := 1;
666 Year := Year + 1;
667 @key[end] @key[if];
668
669 @key[if] Line_Too_Short @key[then]
670 @key[raise] Layout_Error;
671 @key[elsif] Line_Full @key[then]
672 New_Line;
673 Put(Item);
674 @key[else]
675 Put(Item);
676 @key[end] @key[if];
677
678 @key[if] My_Car.Owner.Vehicle /= My_Car @key[then] --@RI{ see @RefSecNum{Incomplete Type Declarations}}
679 Report ("Incorrect data");
680 @key[end] @key[if];
681 @end{Example}
682 @end{Examples}
683
684 @begin{Diffword2005}
685 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0147-1]}
686 @ChgAdded{Version=[3],Text=[Moved definition of @nt{condition} to
687 @RefSecNum{Conditional Expressions} in order to eliminate a forward reference.]}
688 @end{Diffword2005}
689
690
691 @LabeledClause{Case Statements}
692
693 @begin{Intro}
694 @Redundant[A @nt{case_statement} selects for execution one of a
695 number of alternative @ntf{sequences_of_statements}; the chosen
696 alternative is defined by the value of an expression.]
697 @end{Intro}
698
699 @begin{Syntax}
700 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
701 @Syn{lhs=<case_statement>,rhs="
702 @key{case} @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@Syn2{expression} @key{is}
703 @Syn2{case_statement_alternative}
704 {@Syn2{case_statement_alternative}}
705 @key{end} @key{case};"}
706
707
708 @Syn{lhs=<case_statement_alternative>,rhs="
709 @key{when} @Syn2{discrete_choice_list} =>
710 @Syn2{sequence_of_statements}"}
711 @end{Syntax}
712
713 @begin{Resolution}
714 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
715 @Chg{Version=[3],New=[@PDefn2{Term=[expected type], Sec=(case_statement selecting_expression)}
716 @PDefn2{Term=[expected type], Sec=(selecting_expression case_statement)}],Old=[@PDefn2{Term=[expected type], Sec=(case expression)}]}
717 The @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
718 expected to be of any discrete type.
719 @PDefn2{Term=[expected type],
720 Sec=(case_statement_alternative discrete_choice)}
721 The expected type for each @nt{discrete_choice} is the type of the
722 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}.
723 @end{Resolution}
724
725 @begin{Legality}
726 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
727 The @Chg{Version=[3],New=[@nt{choice_expression}s,
728 @nt{subtype_indication}s,],Old=[@nt{expression}s]} and
729 @Chg{Version=[3],New=[@nt{range}s],Old=[@nt{discrete_range}s]} given as
730 @nt{discrete_choice}s of a @nt{case_statement} shall be static.
731 @Redundant[A @nt{discrete_choice} @key(others), if present,
732 shall appear alone and in the last @nt{discrete_choice_list}.]
733
734 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1],ARef=[AI05-0240-1]}
735 The possible values of the
736 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} shall be
737 covered @Chg{Version=[3],New=[(see @RefSecNum{Variant Parts and Discrete Choices}) ],
738 Old=[]}as follows:
739 @begin{Discussion}
740 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0240-1]}
741 @ChgAdded{Version=[3],Text=[The meaning of "covered" here and in the
742 following rules is that of the term "cover a value" that is defined in
743 @RefSecNum{Variant Parts and Discrete Choices}.]}
744 @end{Discussion}
745 @begin{itemize}
746 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0003-1],ARef=[AI05-0153-3],ARef=[AI05-0188-1],ARef=[AI05-0262-1]}
747 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
748 If the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is a @nt{name}
749 @Redundant[(including a
750 @nt<type_conversion>@Chg{Version=[3],New=[, @nt{qualified_expression},],Old=[]}
751 or @Chg{Version=[3],New=[],Old=[a ]}@nt<function_call>)] having
752 a static and constrained nominal subtype,@Chg{Version=[3],New=[],Old=[ or
753 is a @nt{qualified_expression} whose
754 @nt{subtype_mark} denotes a static and constrained
755 scalar subtype,]}
756 then each non-@key{others} @nt{discrete_choice} shall cover only values in
757 that subtype@Chg{Version=[3],New=[ that satisfy its
758 @Chg{Version=[4],New=[predicates],Old=[predicate]} (see
759 @RefSecNum{Subtype Predicates})],Old=[]},
760 and each value of that subtype@Chg{Version=[3],New=[ that satisfies its
761 @Chg{Version=[4],New=[predicates],Old=[predicate]}],Old=[]} shall be
762 covered by some @nt{discrete_choice}
763 @Redundant[(either explicitly or by @key(others))].
764 @begin{Ramification}
765 Although not official @nt<name>s of objects, a value conversion
766 still has a defined nominal subtype, namely its target subtype.
767 See @RefSecNum{Type Conversions}.
768 @end{Ramification}
769
770 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
771 If the type of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
772 @i(root_integer), @i(universal_integer), or a descendant of a
773 formal scalar type,
774 then the @nt{case_statement} shall have an @key{others}
775 @nt{discrete_choice}.
776 @begin{Reason}
777 This is because the base range is
778 implementation defined for @i(root_integer) and @i(universal_integer),
779 and not known statically in the case of a formal scalar type.
780 @end{Reason}
781
782 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
783 Otherwise, each value of the base range of the type of the
784 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} shall
785 be covered
786 @Redundant[(either explicitly or by @key(others))].
787 @end{itemize}
788
789 Two distinct @nt{discrete_choice}s of a
790 @nt{case_statement} shall not cover the same value.
791 @begin{Ramification}
792 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
793 The goal of these coverage rules is
794 that any possible value of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} of a
795 @nt{case_statement} should be covered by exactly one
796 @nt{discrete_choice} of the
797 @nt{case_statement}, and that this should be checked at compile time.
798 The goal is achieved in most cases, but there are two minor
799 loopholes:
800 @begin{Itemize}
801 If the expression reads an object with an invalid representation
802 (e.g. an uninitialized object),
803 then the value can be outside the covered range.
804 This can happen for static constrained subtypes, as well as nonstatic or
805 unconstrained subtypes.
806 It cannot, however, happen if the @nt{case_statement} has the
807 @nt{discrete_choice} @key{others}, because @key{others} covers all values,
808 even those outside the subtype.
809
810 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
811 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
812 If the compiler chooses to represent the value of an expression of an
813 unconstrained subtype in a way that includes values outside the bounds of the
814 subtype, then those values can be outside the covered range.
815 For example, if X: Integer := Integer'Last;, and the case @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression} is
816 X+1, then the implementation might choose to produce the correct value, which
817 is outside the bounds of Integer.
818 (It might raise Constraint_Error instead.)
819 This case can only happen for nongeneric subtypes that are either
820 unconstrained or
821 non@Chg{Version=[2],New=[],Old=[-]}@ChgNote{Make spelling consistent}static
822 (or both).
823 It can only happen if there is no @key{others} @nt{discrete_choice}.
824 @end{Itemize}
825
826 In the uninitialized variable case, the value might be anything; hence, any
827 alternative can be chosen, or Constraint_Error can be raised. (We intend to
828 prevent, however, jumping to random memory locations and the like.)
829 In the out-of-range case, the behavior is more sensible: if there is an
830 @key{others}, then the implementation may choose to raise Constraint_Error
831 on the evaluation of the @nt{expression} (as usual), or it may choose
832 to correctly evaluate the @nt{expression} and therefore choose the
833 @key{others} alternative.
834 Otherwise (no @key{others}), Constraint_Error is raised either way @em on
835 the @nt{expression} evaluation, or for the @nt{case_statement} itself.
836
837 For an enumeration type with a discontiguous set of internal codes
838 (see @RefSecNum{Enumeration Representation Clauses}),
839 the only way to get values in between the proper values
840 is via an object with an invalid representation;
841 there is no @lquotes@;out-of-range@rquotes@; situation that can produce them.
842 @end{Ramification}
843 @end{Legality}
844
845 @begin{RunTime}
846 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
847 @PDefn2{Term=[execution], Sec=(case_statement)}
848 For the execution of a @nt{case_statement} the
849 @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}
850 is first evaluated.
851
852 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0188-1]}
853 If the value of the @Chg{Version=[3],New=[@SynI{selecting_}],Old=[]}@nt{expression}
854 is covered by the @nt{discrete_@!choice_@!list} of some
855 @nt{case_@!statement_@!alternative}, then the
856 @nt{sequence_of_@!statements} of the @ntf{_alternative} is executed.
857
858 @IndexCheck{Overflow_Check}
859 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
860 Otherwise (the value is not covered by any
861 @nt{discrete_choice_list},
862 perhaps due to being outside the base range),
863 Constraint_Error is raised.
864 @begin{Ramification}
865
866 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0005-1]}
867 In this case, the value @Chg{Version=[5],New=[fails to satisfy its
868 (static) predicate (possible when the predicate is disabled),],Old=[]}
869 is outside the base range of its type,
870 or is an invalid representation.
871
872 @end{Ramification}
873 @end{RunTime}
874
875 @begin{Notes}
876 The execution of a @nt{case_statement} chooses one and only one
877 alternative.
878 Qualification of the expression of a @nt{case_statement} by a static
879 subtype can often be used to limit the number of choices that need be
880 given explicitly.
881 @end{Notes}
882
883 @begin{Examples}
884 @Leading@keepnext@i{Examples of case statements:}
885 @begin{Example}
886 @tabclear()@tabset(P22)
887 @key[case] Sensor @key[is]
888 @key[when] Elevation@\=> Record_Elevation(Sensor_Value);
889 @key[when] Azimuth@\=> Record_Azimuth (Sensor_Value);
890 @key[when] Distance@\=> Record_Distance (Sensor_Value);
891 @key[when] @key[others]@\=> @key[null];
892 @key[end] @key[case];
893
894 @tabclear()@tabset(P22)
895 @key[case] Today @key[is]
896 @key[when] Mon@\=> Compute_Initial_Balance;
897 @key[when] Fri@\=> Compute_Closing_Balance;
898 @key[when] Tue .. Thu@\=> Generate_Report(Today);
899 @key[when] Sat .. Sun@\=> @key[null];
900 @key[end] @key[case];
901
902 @tabclear()@tabset(P16)
903 @key[case] Bin_Number(Count) @key[is]
904 @key[when] 1@\=> Update_Bin(1);
905 @key[when] 2@\=> Update_Bin(2);
906 @key[when] 3 | 4@\=>
907 Empty_Bin(1);
908 Empty_Bin(2);
909 @key[when] @key[others]@\=> @key[raise] Error;
910 @key[end] @key[case];
911 @end{Example}
912 @end{Examples}
913
914 @begin{Incompatible83}
915 @ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020}
916 @Chg{New=[@Defn{incompatibilities with Ada 83}
917 In Ada 95, @nt{function_call}s and @nt{type_conversion}s are @nt{name}s, whereas
918 in Ada 83, they were @nt{expression}s. Therefore, if the @nt{expression} of a
919 @nt{case_statement} is a @nt{function_call} or @nt{type_conversion}, and the
920 result subtype is static, it is illegal to specify a choice outside the bounds
921 of the subtype. For this case in Ada 83 choices only are required to be in the
922 base range of the type.],Old=[]}
923
924 @ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020}
925 @Chg{New=[In addition, the rule about which choices must be covered is
926 unchanged in Ada 95. Therefore, for a @nt{case_statement} whose @nt{expression}
927 is a @nt{function_call} or @nt{type_conversion}, Ada 83 required covering all
928 choices in the base range, while Ada 95 only requires covering choices in the
929 bounds of the subtype. If the @nt{case_statement} does not include an @key{others}
930 @nt{discrete_choice}, then a legal Ada 83 @nt{case_statement} will be illegal
931 in Ada 95 if the bounds of the subtype are different than the bounds of the
932 base type.],Old=[]}
933 @end{Incompatible83}
934
935 @begin{Extend83}
936 @Defn{extensions to Ada 83}
937 In Ada 83, the @nt{expression} in a @nt{case_statement} is not allowed to
938 be of a generic formal type.
939 This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice}
940 is required instead.
941
942 In Ada 95, a function call is the name of an object;
943 this was not true in Ada 83 (see @RefSec{Names}).
944 This change makes the following @nt{case_statement} legal:
945 @begin{Example}
946 @key[subtype] S @key[is] Integer @key[range] 1..2;
947 @key[function] F @key[return] S;
948 @key[case] F @key[is]
949 @key[when] 1 => ...;
950 @key[when] 2 => ...;
951 --@RI{ No @key{others} needed.}
952 @key[end] @key[case];
953 @end{Example}
954
955 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
956 Note that the result subtype given in a function
957 @nt{renaming_declaration} is ignored;
958 for a @nt{case_statement} whose expression calls a such a function, the
959 full coverage rules are checked using the result subtype of the original
960 function.
961 Note that predefined operators such as "+" have an unconstrained result
962 subtype (see @RefSecNum{Logical Operators and Short-circuit Control Forms}).
963 Note that generic formal functions do not have static result subtypes.
964 Note that the result subtype of an inherited subprogram need not
965 correspond to any @Chg{Version=[3],New=[nameable],Old=[namable]} subtype;
966 there is still a perfectly good result subtype, though.
967 @end{Extend83}
968
969 @begin{DiffWord83}
970 Ada 83 forgot to say what happens for @lquotes@;legally@rquotes@; out-of-bounds values.
971
972 We take advantage of rules and terms (e.g. @i(cover a value))
973 defined for @nt{discrete_choice}s and @nt{discrete_choice_list}s
974 in @RefSec{Variant Parts and Discrete Choices}.
975
976 In the @ResolutionName for the case expression,
977 we no longer need RM83-5.4(3)'s @lquotes@;which must be determinable
978 independently of the context in which the expression occurs, but
979 using the fact that the expression must be of a discrete type,@rquotes@;
980 because the @nt{expression} is now a complete context.
981 See @RefSec{The Context of Overload Resolution}.
982
983 Since @nt<type_conversion>s are now defined as @nt<name>s,
984 their coverage rule is now covered under the general rule
985 for @nt<name>s, rather than being separated out along with
986 @nt<qualified_expression>s.
987 @end{DiffWord83}
988
989
990 @begin{DiffWord2005}
991 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0003-1]}
992 @ChgAdded{Version=[3],Text=[Rewording to reflect that
993 a @nt{qualified_expression} is now a @nt{name}.]}
994
995 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0153-3]}
996 @ChgAdded{Version=[3],Text=[Revised for changes to @nt{discrete_choice}s
997 made to allow static predicates (see @RefSecNum{Subtype Predicates}) as
998 case choices (see @RefSecNum{Variant Parts and Discrete Choices}).]}
999
1000 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
1001 @ChgAdded{Version=[3],Text=[Added the @SynI{selecting_} prefix to
1002 make this wording consistent with @nt{case_expression}, and to clarify
1003 which @nt{expression} is being talked about in the wording.]}
1004 @end{Diffword2005}
1005
1006 @begin{DiffWord2012}
1007 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
1008 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of
1009 case coverage to use the new term "satisfies the predicates"
1010 (see @RefSecNum{Subtype Predicates}).]}
1011 @end{Diffword2012}
1012
1013
1014 @LabeledClause{Loop Statements}
1015
1016 @begin{Intro}
1017 @Redundant[A @nt{loop_statement} includes a
1018 @nt{sequence_of_statements} that is to be executed repeatedly,
1019 zero or more times.]
1020 @end{Intro}
1021
1022 @begin{Syntax}
1023 @Syn{lhs=<loop_statement>,rhs="
1024 [@SynI{loop_}@Syn2{statement_identifier}:]
1025 [@Syn2{iteration_scheme}] @key{loop}
1026 @Syn2{sequence_of_statements}
1027 @key{end} @key{loop} [@SynI{loop_}@Syn2{identifier}];"}
1028
1029
1030 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0139-2]}
1031 @Syn{lhs=<iteration_scheme>,rhs="@key{while} @Syn2{condition}
1032 | @key{for} @Syn2{loop_parameter_specification}@Chg{Version=[3],New=[
1033 | @key{for} @Syn2{iterator_specification}],Old=[]}"}
1034
1035 @Syn{lhs=<loop_parameter_specification>,rhs="
1036 @Syn2{defining_identifier} @key{in} [@key{reverse}] @Syn2{discrete_subtype_definition}"}
1037
1038 @begin(SyntaxText)
1039 If a @nt{loop_statement} has a @SynI{loop_}@nt{statement_identifier},
1040 then the @nt<identifier> shall be repeated after the @key{end loop};
1041 otherwise, there shall not be an @nt<identifier> after the @key{end loop}.
1042 @end(SyntaxText)
1043 @end{Syntax}
1044
1045 @begin{StaticSem}
1046 @Defn{loop parameter}
1047 A @nt{loop_parameter_specification} declares a @i{loop parameter},
1048 which is an object whose
1049 subtype is that defined by the @nt{discrete_subtype_definition}.
1050 @IndexSeeAlso{Term=[parameter],See=[loop parameter]}
1051 @end{StaticSem}
1052
1053 @begin{RunTime}
1054 @PDefn2{Term=[execution], Sec=(loop_statement)}
1055 For the execution of a @nt{loop_statement},
1056 the @nt{sequence_of_statements} is executed repeatedly,
1057 zero or more times,
1058 until the @nt{loop_statement} is complete.
1059 The @nt{loop_statement} is complete when a transfer
1060 of control occurs that transfers control out of the loop, or, in the
1061 case of an @nt{iteration_scheme}, as specified below.
1062
1063 @PDefn2{Term=[execution],
1064 Sec=(loop_statement with a while iteration_scheme)}
1065 For the execution of a @nt{loop_statement} with a @key{while}
1066 @nt{iteration_scheme}, the condition is evaluated before each
1067 execution of the @nt{sequence_of_@!statements}; if the value of the
1068 @nt{condition} is True, the @nt{sequence_of_@!statements} is executed;
1069 if False, the execution of the @nt{loop_@!statement} is complete.
1070
1071 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0139-2],ARef=[AI05-0262-1]}
1072 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
1073 @PDefn2{Term=[execution],
1074 Sec=(loop_statement with a for iteration_scheme)}
1075 @PDefn2{Term=[elaboration], Sec=(loop_parameter_specification)}
1076 For the execution of a @nt{loop_statement} with
1077 @Chg{Version=[3],New=[the],Old=[a @key{for}]}
1078 @nt{iteration_scheme}@Chg{Version=[3],New=[ being @key[for]
1079 @nt{loop_@!parameter_@!specification}],Old=[]},
1080 the @nt{loop_@!parameter_@!specification} is first elaborated. This
1081 elaboration creates the loop parameter and elaborates the
1082 @nt{discrete_@!subtype_@!definition}.
1083 If the @nt{discrete_@!subtype_@!definition} defines a subtype
1084 with a null range,
1085 the execution of the
1086 @nt{loop_statement} is complete. Otherwise, the
1087 @nt{sequence_of_@!statements} is executed once for each value of the
1088 discrete subtype defined by the
1089 @nt{discrete_@!subtype_@!definition}
1090 @Chg{Version=[3],New=[that satisfies the
1091 @Chg{Version=[4],New=[predicates],Old=[predicate]} of the subtype ],Old=[]}(or
1092 until the loop is left as a consequence of a transfer of control).
1093 @Defn2{Term=[assignment operation], Sec=(during execution of a @key{for} loop)}
1094 Prior to each such iteration,
1095 the corresponding value of the discrete subtype is assigned to the
1096 loop parameter. These values are assigned in increasing order unless
1097 the reserved word @key{reverse} is present, in which case the values
1098 are assigned in decreasing order.
1099 @begin{Ramification}
1100 The order of creating the loop parameter and evaluating the
1101 @nt{discrete_subtype_definition} doesn't matter,
1102 since the creation of the loop parameter has no side effects (other
1103 than possibly raising Storage_Error, but anything can do that).
1104
1105 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
1106 @ChgAdded{Version=[3],Text=[The predicate (if any) necessarily has to be a
1107 static predicate as a dynamic predicate is explicitly disallowed @em
1108 see @RefSecNum{Subtype Predicates}.]}
1109
1110 @end{Ramification}
1111
1112 @begin{Reason}
1113 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
1114 @ChgAdded{Version=[3],Type=[Leading],Text=[If there is a predicate, the loop
1115 still visits the values in the order of the underlying base type; the order of
1116 the values in the predicate is irrelevant. This is the case so that the
1117 following loops have the same sequence of calls and parameters on procedure
1118 Call for any subtype S:]}
1119 @begin{Example}
1120 @ChgAdded{Version=[3],Text=[@key[for] I @key[in] S @key[loop]
1121 Call (I);
1122 @key[end loop];]}
1123
1124 @ChgAdded{Version=[3],Text=[@key[for] I @key[in] S'Base @key[loop]
1125 @key[if] I @key[in] S @key[then]
1126 Call (I);
1127 @key[end if];
1128 @key[end loop];]}
1129 @end{Example}
1130 @end{Reason}
1131
1132 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0262-1]}
1133 @ChgAdded{Version=[3],Text=[@Redundant[For details about the execution of a
1134 @nt{loop_statement} with the @nt{iteration_scheme} being @key[for]
1135 @nt{iterator_specification}, see @RefSecNum{Generalized Loop Iteration}.]]}
1136
1137 @end{RunTime}
1138
1139 @begin{Notes}
1140 A loop parameter is a constant;
1141 it cannot be updated within the
1142 @nt{sequence_of_statements} of the loop
1143 (see @RefSecNum{Objects and Named Numbers}).
1144
1145 An @nt{object_declaration} should not be given for a loop parameter,
1146 since the loop parameter is automatically declared by
1147 the @nt{loop_parameter_specification}.
1148 The scope of a loop parameter extends from the
1149 @nt{loop_parameter_specification} to the end of the
1150 @nt{loop_statement}, and the visibility
1151 rules are such that a loop parameter is only visible within the
1152 @nt{sequence_of_statements} of the loop.
1153 @begin{ImplNote}
1154 An implementation could give a warning if a variable is hidden by a
1155 @nt{loop_parameter_specification}.
1156 @end{ImplNote}
1157
1158 The @nt<discrete_subtype_definition> of a for loop is elaborated
1159 just once. Use of the
1160 reserved word @key[reverse] does not alter the discrete subtype defined,
1161 so that the following @nt{iteration_scheme}s are not equivalent; the
1162 first has a null range.
1163 @begin{Example}
1164 @key[for] J @key[in] @key[reverse] 1 .. 0
1165 @key[for] J @key[in] 0 .. 1
1166 @end{Example}
1167 @begin{Ramification}
1168 If a @nt{loop_parameter_specification} has a static discrete range,
1169 the subtype of the loop parameter is static.
1170 @end{Ramification}
1171 @end{Notes}
1172
1173 @begin{Examples}
1174 @Leading@keepnext@i{Example of a loop statement without an iteration scheme:}
1175 @begin{Example}
1176 @key[loop]
1177 Get(Current_Character);
1178 @key[exit] @key[when] Current_Character = '*';
1179 @key[end] @key[loop];
1180 @end{Example}
1181
1182 @begin{WideAbove}
1183 @leading@keepnext@i{Example of a loop statement with a @key[while] iteration scheme:}
1184 @end{WideAbove}
1185 @begin{Example}
1186 @key[while] Bid(N).Price < Cut_Off.Price @key[loop]
1187 Record_Bid(Bid(N).Price);
1188 N := N + 1;
1189 @key[end] @key[loop];
1190 @end{Example}
1191
1192 @begin{WideAbove}
1193 @leading@keepnext@i{Example of a loop statement with a @key[for] iteration scheme:}
1194 @end{WideAbove}
1195 @begin{Example}
1196 @key[for] J @key[in] Buffer'Range @key[loop] --@RI{ works even with a null range}
1197 @key[if] Buffer(J) /= Space @key[then]
1198 Put(Buffer(J));
1199 @key[end] @key[if];
1200 @key[end] @key[loop];
1201 @end{Example}
1202
1203 @begin{WideAbove}
1204 @leading@keepnext@i{Example of a loop statement with a name:}
1205 @end{WideAbove}
1206 @begin{Example}
1207 Summation:
1208 @key[while] Next /= Head @key[loop] --@RI{ see @RefSecNum{Incomplete Type Declarations}}
1209 Sum := Sum + Next.Value;
1210 Next := Next.Succ;
1211 @key[end] @key[loop] Summation;
1212 @end{Example}
1213 @end{Examples}
1214
1215 @begin{DiffWord83}
1216 The constant-ness of loop parameters is specified in
1217 @RefSec{Objects and Named Numbers}.
1218 @end{DiffWord83}
1219
1220 @begin{DiffWord2005}
1221 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0262-1],ARef=[AI05-0299-1]}
1222 @ChgAdded{Version=[3],Text=[Generalized @nt{iterator_specification}s are
1223 allowed in @key[for] loops; these are documented as an extension in the
1224 appropriate subclause.]}
1225 @end{DiffWord2005}
1226
1227 @begin{DiffWord2012}
1228 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
1229 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of
1230 loop execution to use the new term "satisfies the predicates"
1231 (see @RefSecNum{Subtype Predicates}).]}
1232 @end{Diffword2012}
1233
1234
1235 @LabeledAddedSubClause{Version=[3],Name=[User-Defined Iterator Types]}
1236
1237 @begin{StaticSem}
1238
1239 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1240 @ChgAdded{Version=[3],Type=[Leading],Keepnext=[T],Text=[The following
1241 language-defined generic library package exists:]}
1242 @begin{Example}
1243 @ChgRef{Version=[3],Kind=[AddedNormal]}
1244 @ChgAdded{Version=[3],Text=[@ChildUnit{Parent=[Ada],Child=[Iterator_Interfaces]}@key[generic]
1245 @key[type] Cursor;
1246 @key[with function] Has_Element (Position : Cursor) @key[return] Boolean;
1247 @key[package] Ada.Iterator_Interfaces @key[is]
1248 @key[pragma] Pure (Iterator_Interfaces);]}
1249
1250 @ChgRef{Version=[3],Kind=[AddedNormal]}
1251 @ChgAdded{Version=[3],Text=[ @key[type] @AdaTypeDefn{Forward_Iterator} @key[is limited interface];
1252 @key[function] @AdaSubDefn{First} (Object : Forward_Iterator) @key[return] Cursor @key[is abstract];
1253 @key[function] @AdaSubDefn{Next} (Object : Forward_Iterator; Position : Cursor)
1254 @key[return] Cursor @key[is abstract];]}
1255
1256 @ChgRef{Version=[3],Kind=[AddedNormal]}
1257 @ChgAdded{Version=[3],Text=[ @key[type] @AdaTypeDefn{Reversible_Iterator} @key[is limited interface and] Forward_Iterator;
1258 @key[function] @AdaSubDefn{Last} (Object : Reversible_Iterator) @key[return] Cursor @key[is abstract];
1259 @key[function] @AdaSubDefn{Previous} (Object : Reversible_Iterator; Position : Cursor)
1260 @key[return] Cursor @key[is abstract];]}
1261
1262 @ChgRef{Version=[3],Kind=[AddedNormal]}
1263 @ChgAdded{Version=[3],Text=[@key[end] Ada.Iterator_Interfaces;]}
1264 @end{Example}
1265
1266 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1267 @ChgAdded{Version=[3],Text=[An @i<iterator type> is a type descended from
1268 the Forward_Iterator interface from some instance of
1269 Ada.Iterator_Interfaces.@Defn{iterator type}
1270 A @i<reversible iterator type> is a type descended from the Reversible_Iterator
1271 interface from some instance of Ada.Iterator_Interfaces.@Defn{reversible iterator type}
1272 An @i<iterator object> is an object of an iterator type.@Defn{iterator object}
1273 A @i<reversible iterator object> is an object
1274 of a reversible iterator type.@Defn{reversible iterator object}
1275 The formal subtype Cursor from the associated
1276 instance of Ada.Iterator_Interfaces is the @i<iteration cursor subtype> for the
1277 iterator type.@Defn{iteration cursor subtype}]}
1278
1279 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1280 @ChgAdded{Version=[3],Type=[Leading],Text=[The following type-related
1281 operational aspects may be specified for an indexable container type @i<T> (see
1282 @RefSecNum{User-Defined Indexing}):]}
1283
1284 @begin{Description}
1285 @ChgRef{Version=[3],Kind=[AddedNormal]}
1286 @ChgAdded{Version=[3],Text=[Default_Iterator@\This aspect is specified
1287 by a @nt{name} that denotes exactly one function declared immediately within
1288 the same declaration list in which @i<T> is declared, whose first parameter
1289 is of type @i<T> or @i<T>'Class or an access parameter whose designated type
1290 is type @i<T> or @i<T>'Class, whose other parameters, if any, have default
1291 expressions, and whose result type is an iterator type. This function is the
1292 @i<default iterator function> for @i<T>.@Defn{default iterator function}
1293 Its result subtype is the @i<default iterator subtype> for
1294 @i<T>.@Defn{default iterator subtype} The iteration cursor subtype for
1295 the default iterator subtype is the @i<default cursor subtype>
1296 for @i<T>.@Defn{default cursor subtype}@AspectDefn{Default_Iterator}]}
1297
1298 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Default_Iterator],
1299 Text=[@ChgAdded{Version=[3],Text=[Default iterator to be used in @key[for]
1300 loops.]}]}
1301
1302 @ChgRef{Version=[3],Kind=[AddedNormal]}
1303 @ChgAdded{Version=[3],Text=[Iterator_Element@\This aspect is specified by a
1304 @nt{name} that denotes a subtype. This is the @i<default element subtype>
1305 for @i<T>.@Defn{default element subtype}@AspectDefn{Iterator_Element}]}
1306
1307 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Iterator_Element],
1308 Text=[@ChgAdded{Version=[3],Text=[Element type to be used for user-defined
1309 iterators.]}]}
1310 @end{Description}
1311
1312 @ChgRef{Version=[3],Kind=[AddedNormal]}
1313 @ChgAdded{Version=[3],Text=[These aspects are inherited by descendants of
1314 type @i<T> (including @i<T>'Class).]}
1315
1316 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1317 @ChgAdded{Version=[3],Text=[An @i<iterable container type> is an indexable container type
1318 with specified Default_Iterator and Iterator_Element aspects.@Defn{iterable container type}
1319 A @i<reversible iterable container type> is an iterable container type with the default iterator type
1320 being a reversible iterator type.@Defn{reversible iterable container type}
1321 An @i<iterable container object> is an object of an iterable container type.@Defn{iterable container object}
1322 A @i<reversible iterable container object> is an object of a reversible iterable container
1323 type.@Defn{reversible iterable container object}]}
1324
1325 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Iterable container type>,
1326 Text=<@ChgAdded{Version=[3],Text=[An iterable container type is one that has
1327 user-defined behavior for iteration, via the Default_Iterator and
1328 Iterator_Element aspects.]}>}
1329
1330 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0138-1]}
1331 @ChgAdded{Version=[4],Text=[The Default_Iterator and Iterator_Element aspects
1332 are nonoverridable (see @RefSecNum{Aspect Specifications}).]}
1333
1334 @begin{Reason}
1335 @ChgRef{Version=[4],Kind=[AddedNormal]}
1336 @ChgAdded{Version=[4],Text=[This ensures that all descendants of an
1337 iterable container type have aspects with the same properties. This prevents
1338 generic contract problems with formal derived types.]}
1339 @end{Reason}
1340 @end{StaticSem}
1341
1342 @begin{Legality}
1343
1344 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1345 @ChgAdded{Version=[3],Type=[Leading],Text=[The Constant_Indexing aspect (if any)
1346 of an iterable container type @i<T> shall denote exactly one function with the following
1347 properties:]}
1348
1349 @begin{Itemize}
1350
1351 @ChgRef{Version=[3],Kind=[AddedNormal]}
1352 @ChgAdded{Version=[3],Text=[the result type of the function is covered by the
1353 default element type of @i<T> or is a reference type (see
1354 @RefSecNum{User-Defined References}) with an access discriminant designating
1355 a type covered by the default element type of @i<T>;]}
1356
1357 @ChgRef{Version=[3],Kind=[AddedNormal]}
1358 @ChgAdded{Version=[3],Text=[the type of the second parameter of the function
1359 covers the default cursor type for @i<T>;]}
1360
1361 @ChgRef{Version=[3],Kind=[AddedNormal]}
1362 @ChgAdded{Version=[3],Text=[if there are more than two parameters, the
1363 additional parameters all have default expressions.]}
1364
1365 @end{Itemize}
1366
1367 @ChgRef{Version=[3],Kind=[AddedNormal]}
1368 @ChgAdded{Version=[3],Text=[This function (if any) is the
1369 @i<default constant indexing function> for @i<T>.@Defn{default constant indexing function}]}
1370
1371 @begin{Ramification}
1372 @ChgRef{Version=[3],Kind=[AddedNormal]}
1373 @ChgAdded{Version=[3],Text=[This does not mean that Constant_Indexing has to
1374 designate only one subprogram, only that there is only one routine that meets
1375 all of these properties. There can be other routines designated by
1376 Constant_Indexing, but they cannot have the profile described above. For
1377 instance, map containers have a version of Constant_Indexing that takes a
1378 key instead of a cursor; this is allowed.]}
1379 @end{Ramification}
1380
1381 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1382 @ChgAdded{Version=[3],Type=[Leading],Text=[The Variable_Indexing aspect (if any)
1383 of an iterable container type @i<T> shall denote exactly one function with the following
1384 properties:]}
1385
1386 @begin{Itemize}
1387 @ChgRef{Version=[3],Kind=[AddedNormal]}
1388 @ChgAdded{Version=[3],Text=[the result type of the function is a reference
1389 type (see @RefSecNum{User-Defined References}) with an access discriminant
1390 designating a type covered by the default element type of @i<T>;]}
1391
1392 @ChgRef{Version=[3],Kind=[AddedNormal]}
1393 @ChgAdded{Version=[3],Text=[the type of the second parameter of the function
1394 covers the default cursor type for @i<T>;]}
1395
1396 @ChgRef{Version=[3],Kind=[AddedNormal]}
1397 @ChgAdded{Version=[3],Text=[if there are more than two parameters, the
1398 additional parameters all have default expressions.]}
1399
1400 @end{Itemize}
1401
1402 @ChgRef{Version=[3],Kind=[AddedNormal]}
1403 @ChgAdded{Version=[3],Text=[This function (if any) is the
1404 @i<default variable indexing function> for @i<T>.@Defn{default variable indexing function}]}
1405
1406 @end{Legality}
1407
1408 @begin{Extend2005}
1409 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1410 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}User-defined
1411 iterator types are new in Ada 2012.]}
1412 @end{Extend2005}
1413
1414 @begin{Incompatible2012}
1415 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0138-1]}
1416 @ChgAdded{Version=[4],Text=[@Defn{incompatibilities with Ada 2012}@b<Corrigendum:>
1417 Defined Default_Iterator and Iterator_Element to be nonoveridable, which
1418 makes redefinitions and hiding of these aspects illegal. It's possible that
1419 some program could violate one of these new restrictions, but in most cases
1420 this can easily be worked around by using overriding rather than
1421 redefinition.]}
1422 @end{Incompatible2012}
1423
1424
1425
1426 @LabeledAddedSubClause{Version=[3],Name=[Generalized Loop Iteration]}
1427
1428 @begin{Intro}
1429 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1430 @ChgAdded{Version=[3],Text=[Generalized forms of loop iteration are provided by
1431 an @nt{iterator_specification}.]}
1432 @end{Intro}
1433
1434 @begin{Syntax}
1435 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1436 @AddedSyn{Version=[3],lhs=<@Chg{Version=[3],New=<iterator_specification>,Old=<>}>,
1437 rhs="@Chg{Version=[3],New=<
1438 @Syn2{defining_identifier} @key[in] [@key{reverse}] @SynI{iterator_}@Syn2{name}
1439 | @Syn2{defining_identifier} [: @Syn2{subtype_indication}] @key[of] [@key{reverse}] @SynI{iterable_}@Syn2{name}>,Old=<>}"}
1440 @end{Syntax}
1441
1442 @begin{Resolution}
1443 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1444 @ChgAdded{Version=[3],Text=[For the first form of @nt{iterator_specification},
1445 called a @i<generalized iterator>,@Defn{generalized iterator}@Defn2{Term=[iterator],Sec=[generalized]}
1446 the expected type for the @SynI<iterator_>@nt{name} is any iterator
1447 type.@PDefn2{Term=[expected type],Sec=[@SynI<iterator_>@nt{name}]}
1448 For the second form of @nt{iterator_specification},
1449 the expected type for the @SynI<iterable_>@nt{name} is any array or iterable
1450 container type.@PDefn2{Term=[expected type],Sec=[@SynI<iterable_>@nt{name}]}
1451 If the @SynI<iterable_>@nt{name} denotes an array object, the
1452 @nt{iterator_specification} is called an @i<array
1453 component iterator>;@Defn{array component iterator}@Defn2{Term=[iterator],Sec=[array component]}
1454 otherwise it is called a
1455 @i<container element iterator>.@Defn{container element iterator}@Defn2{Term=[iterator],Sec=[container element]}]}
1456 @end{Resolution}
1457
1458 @begin{Legality}
1459 @ChgToGlossary{Version=[3],Kind=[Added],Term=<Iterator>,
1460 Text=<@ChgAdded{Version=[3],Text=[An iterator is a construct that is used to
1461 loop over the elements of an array or container. Iterators may be user defined,
1462 and may perform arbitrary computations to access elements from a container.]}>}
1463
1464 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1465 @ChgAdded{Version=[3],Text=[If the reserved word @key[reverse] appears,
1466 the @nt{iterator_specification} is a @i<reverse iterator>;@Defn{reverse iterator}@Defn2{Term=[iterator],Sec=[reverse]}
1467 otherwise it is a @i<forward iterator>.@Defn{forward iterator}@Defn2{Term=[iterator],Sec=[forward]}
1468 In a reverse generalized iterator, the
1469 @SynI<iterator_>@nt{name} shall be of a reversible iterator type.
1470 In a reverse container element iterator, the default iterator type for the type
1471 of the @SynI<iterable_>@nt{name} shall be a reversible iterator type.]}
1472
1473 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1474 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0151-1]}
1475 @ChgAdded{Version=[3],Text=[The @Chg{Version=[4],New=[subtype defined
1476 by],Old=[type of]} the @nt{subtype_indication}, if any, of an array component
1477 iterator shall @Chg{Version=[4],New=[statically match],Old=[cover]} the
1478 component @Chg{Version=[4],New=[subtype],Old=[type]} of the type of the
1479 @SynI<iterable_>@nt{name}. The @Chg{Version=[4],New=[subtype defined
1480 by],Old=[type of]} the @nt{subtype_indication}, if any, of a container element
1481 iterator shall @Chg{Version=[4],New=[statically match],Old=[cover]} the default
1482 element @Chg{Version=[4],New=[subtype],Old=[type]} for the type of the
1483 @SynI<iterable_>@nt{name}.]}
1484
1485 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1486 @ChgAdded{Version=[3],Text=[In a container element iterator whose
1487 @SynI<iterable_>@nt{name} has type @i<T>, if the @SynI<iterable_>@nt{name}
1488 denotes a constant or the Variable_Indexing aspect is not specified for @i<T>,
1489 then the Constant_Indexing aspect shall be specified for @i<T>.]}
1490
1491 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0047-1]}
1492 @ChgAdded{Version=[4],Text=[The @SynI<iterator_>@nt{name} or
1493 @SynI<iterable_>@nt{name} of an @nt{iterator_specification} shall
1494 not denote a subcomponent that depends on discriminants of an object
1495 whose nominal subtype is unconstrained, unless the object is known
1496 to be constrained.]}
1497
1498 @begin{Reason}
1499 @ChgRef{Version=[4],Kind=[AddedNormal]}
1500 @ChgAdded{Version=[4],Text=[This is the same rule that applies to
1501 renames; it serves the same purpose of preventing the object from
1502 disappearing while the iterator is still using it.]}
1503 @end{Reason}
1504
1505 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0120-1]}
1506 @ChgAdded{Version=[4],Text=[A container element iterator is illegal if the
1507 call of the default iterator function that creates the loop iterator
1508 (see below) is illegal.]}
1509
1510 @begin{Ramification}
1511 @ChgRef{Version=[4],Kind=[AddedNormal]}
1512 @ChgAdded{Version=[4],Text=[This can happen if the parameter to the default
1513 iterator function is @key[in out] and the @SynI<iterable_>@nt{name} is a
1514 constant. The wording applies to any reason that the call would be illegal,
1515 as it's possible that one of the default parameters would be illegal, or
1516 that some accessibility check would fail.]}
1517 @end{Ramification}
1518
1519 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0120-1]}
1520 @ChgAdded{Version=[4],Text=[A generalized iterator is illegal if the iteration
1521 cursor subtype of the @SynI<iterator_>@nt{name} is a limited type at the point
1522 of the generalized iterator. A container element iterator is illegal if the
1523 default cursor subtype of the type of the @SynI<iterable_>@nt{name} is a limited
1524 type at the point of the container element iterator.]}
1525
1526 @begin{Reason}
1527 @ChgRef{Version=[4],Kind=[AddedNormal]}
1528 @ChgAdded{Version=[4],Text=[If the cursor type is limited, the assignment to
1529 the loop parameter for a generalized iterator would be illegal. The same is
1530 true for a container element iterator. We have to say "at the point of the
1531 iterator" as the limitedness of a type can change due to visibility.]}
1532 @end{Reason}
1533
1534 @end{Legality}
1535
1536 @begin{StaticSem}
1537
1538 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0269-1],ARef=[AI05-0292-1]}
1539 @ChgAdded{Version=[3],Text=[An @nt{iterator_specification} declares a
1540 @i<loop parameter>.@Defn{loop parameter}
1541 In a generalized iterator, the nominal subtype of the loop parameter is
1542 the iteration cursor subtype. In an array component iterator or a
1543 container element iterator, if a @nt{subtype_indication} is present, it
1544 determines the nominal subtype of the loop parameter. In an array
1545 component iterator, if a @nt{subtype_indication} is not present, the
1546 nominal subtype of the loop parameter is the component subtype of the
1547 type of the @SynI{iterable_}@nt{name}. In a container element iterator, if a
1548 @nt{subtype_indication} is not present, the nominal subtype of the loop
1549 parameter is the default element subtype for the type of the
1550 @SynI{iterable_}@nt{name}.]}
1551
1552 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1553 @ChgAdded{Version=[3],Text=[In a generalized iterator, the loop parameter
1554 is a constant. In an array component iterator, the loop parameter
1555 is a constant if the @SynI<iterable_>@nt{name} denotes a constant; otherwise
1556 it denotes a variable. In a container element iterator, the loop parameter
1557 is a constant if the @SynI{iterable_}@nt{name} denotes a constant, or if
1558 the Variable_Indexing aspect is not specified for the type of the
1559 @SynI{iterable_}@nt{name}; otherwise it is a variable.]}
1560
1561 @begin{Ramification}
1562 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0093-1]}
1563 @ChgAdded{Version=[4],Text=[The loop parameter of a generalized iterator has
1564 the same accessibility as the loop statement. This means that the loop
1565 parameter object is finalized when the loop statement is left. (It also may be
1566 finalized as part of assigning a new value to the loop parameter.) For array
1567 component iterators and container element iterators, the loop parameter
1568 directly denotes an element of the array or container and has the
1569 accessibility of the associated array or container.]}
1570 @end{Ramification}
1571
1572 @end{StaticSem}
1573
1574 @begin{Runtime}
1575
1576 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1577 @ChgAdded{Version=[3],Text=[For the execution of a @nt{loop_statement} with
1578 an @nt{iterator_specification}, the @nt{iterator_specification} is
1579 first elaborated. This elaboration elaborates the @nt{subtype_indication},
1580 if any.]}
1581
1582 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1583 @ChgAdded{Version=[3],Text=[For a generalized iterator, the loop parameter is
1584 created, the @SynI{iterator_}@nt{name} is evaluated, and the denoted iterator
1585 object becomes the @i<loop iterator>.@Defn{loop iterator} In a forward
1586 generalized iterator, the operation First of the iterator type is called on the
1587 loop iterator, to produce the initial value for the loop parameter. If the
1588 result of calling Has_Element on the initial value is False, then the execution
1589 of the @nt{loop_statement} is complete. Otherwise, the
1590 @nt{sequence_of_statements} is executed and then the Next operation of the
1591 iterator type is called with the loop iterator and the current value of the loop
1592 parameter to produce the next value to be assigned to the loop parameter. This
1593 repeats until the result of calling Has_Element on the loop parameter is False,
1594 or the loop is left as a consequence of a transfer of control. For a reverse
1595 generalized iterator, the operations Last and Previous are called rather than
1596 First and Next.]}
1597
1598 @begin{Ramification}
1599 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0093-1]}
1600 @ChgAdded{Version=[4],Text=[The loop parameter of a generalized iterator is a
1601 variable of which the user only has a constant view. It follows the normal
1602 rules for a variable of its nominal subtype. In particular, if the nominal
1603 subtype is indefinite, the variable is constrained by its initial value.
1604 Similarly, if the nominal subtype is class-wide, the variable (like all
1605 variables) has the tag of the initial value. Constraint_Error may be raised by
1606 a subsequent iteration if Next or Previous return an object with a different
1607 tag or constraint.]}
1608 @end{Ramification}
1609
1610 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1611 @ChgAdded{Version=[3],Text=[For an array component iterator, the
1612 @SynI<iterable_>@nt{name} is evaluated and
1613 the denoted array object becomes the @i<array for the
1614 loop>.@Defn{array for a loop} If the array for the loop is a null array,
1615 then the execution of the @nt{loop_statement} is complete. Otherwise, the
1616 @nt{sequence_of_statements} is executed with the loop parameter denoting
1617 each component of the array for the loop, using a @i<canonical> order
1618 of components,@Defn{canonical order of array components}
1619 which is last dimension varying fastest (unless the
1620 array has convention Fortran, in which case it is first dimension
1621 varying fastest). For a
1622 forward array component iterator, the iteration starts with the
1623 component whose index values are each the first in their index range,
1624 and continues in the canonical order. For a reverse array component
1625 iterator, the iteration starts with the component whose index values
1626 are each the last in their index range, and continues in the reverse
1627 of the canonical order. The loop iteration proceeds until the
1628 @nt{sequence_of_statements} has been executed for each component of the
1629 array for the loop, or until the loop is left as a consequence of a
1630 transfer of control.]}
1631
1632 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1633 @ChgAdded{Version=[3],Text=[For a container element iterator, the @SynI<iterable_>@nt{name} is evaluated
1634 and the denoted iterable container object becomes the @i<iterable
1635 container object for the loop>.@Defn{iterable container object for a loop}
1636 The default iterator function for the type of
1637 the iterable container object for the loop is called on the iterable container object
1638 and the result is the @i<loop iterator>.@Defn2{Term=[loop iterator],Sec=[container element iterator]}
1639 An object of the default cursor subtype is created (the @i<loop cursor>).@Defn{loop cursor}]}
1640
1641 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2],ARef=[AI05-0292-1]}
1642 @ChgAdded{Version=[3],Text=[For a forward container element iterator, the
1643 operation First of the iterator type is called on the loop iterator, to produce
1644 the initial value for the loop cursor. If the result of calling Has_Element on
1645 the initial value is False, then the execution of the @nt{loop_statement} is
1646 complete. Otherwise, the @nt{sequence_of_statements} is executed with the loop
1647 parameter denoting an indexing (see @RefSecNum{User-Defined Indexing}) into the
1648 iterable container object for the loop, with the only parameter to the indexing being the
1649 current value of the loop cursor; then the Next operation of the iterator type
1650 is called with the loop iterator and the loop cursor to produce the next value
1651 to be assigned to the loop cursor. This repeats until the result of calling
1652 Has_Element on the loop cursor is False, or until the loop is left as a
1653 consequence of a transfer of control. For a reverse container element iterator,
1654 the operations Last and Previous are called rather than First and Next. If the
1655 loop parameter is a constant (see above), then the indexing uses the default
1656 constant indexing function for the type of the iterable container object for the
1657 loop; otherwise it uses the default variable indexing function.]}
1658
1659 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
1660 @ChgAdded{Version=[4],Text=[Any exception propagated by the execution of a
1661 generalized iterator or container element iterator is propagated by the
1662 immediately enclosing loop statement.]}
1663
1664 @begin{Ramification}
1665 @ChgRef{Version=[4],Kind=[AddedNormal]}
1666 @ChgAdded{Version=[4],Text=[This text covers exceptions raised by called
1667 functions that make up the execution of the iterator as well as
1668 exceptions raised by the assignment to the loop parameter or cursor.]}
1669 @end{Ramification}
1670 @end{Runtime}
1671
1672 @begin{Examples}
1673 @begin{Example}
1674 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0269-1]}
1675 @ChgAdded{Version=[3],Text=[-- @Examcom{Array component iterator example:}
1676 @key[for] Element @key[of] Board @key[loop] -- @Examcom{See @RefSecNum{Index Constraints and Discrete Ranges}.}
1677 Element := Element * 2.0; -- @Examcom{Double each element of Board, a two-dimensional array.}
1678 @key[end loop];]}
1679 @end{Example}
1680
1681 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0268-1]}
1682 @ChgAdded{Version=[3],Text=[For examples of use of generalized iterators,
1683 see @RefSecNum{Example of Container Use} and the corresponding container
1684 packages in @RefSecNum{The Generic Package Containers.Vectors} and
1685 @RefSecNum{The Generic Package Containers.Doubly_Linked_Lists}.]}
1686 @end{Examples}
1687
1688
1689 @begin{Extend2005}
1690 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0139-2]}
1691 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}Generalized forms
1692 of loop iteration are new.]}
1693 @end{Extend2005}
1694
1695 @begin{Incompatible2012}
1696 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0047-1]}
1697 @ChgAdded{Version=[4],Text=[@Defn{incompatibilities with Ada
1698 2012}@b<Corrigendum:> Added a rule to ensure that the object being iterated
1699 cannot be a component that could disappear before the loop completes. This
1700 could be incompatible by making a loop that was legal (and worked correctly,
1701 so long as the enclosing object is not modified during the loop) from the
1702 original Ada 2012 illegal in corrected Ada 2012. Such loops should be pretty
1703 rare, especially as these iterator forms are new to Ada 2012.]}
1704
1705 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
1706 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added rules to reject loops
1707 if the call to the default iterator function for a container element
1708 iterator is illegal, or if the cursor type of an iterator is limited.
1709 These are formally incompatible with original Ada 2012, but as it's unlikely
1710 that any Ada 2012 compiler ever allowed the illegal usages in an expansion
1711 of a loop (it's much more likely that they would have just caused an internal
1712 error in the compiler), this should have no effect in practice.]}
1713
1714 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0151-1]}
1715 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added a requirement that the
1716 given subtype statically match the subtype of the element or component for
1717 a component element iterator or array component iterator. Original Ada 2012
1718 text allowed any type that covered the subtype of the element or component,
1719 but that led to questions of what the meaning was if they are different.
1720 In this case, the element is essentially a renaming of the container element,
1721 and it doesn't make sense for the constraints to be different. Ignoring
1722 explicitly defined constraints in renames is a mistake that we don't want
1723 to continue, thus we require static matching. This means that some programs
1724 might be illegal, but those programs were misleading at best, and
1725 potentially would raise unexpected exceptions because the element values
1726 might have been invalid or abnormal with respect to the declared constraint.]}
1727 @end{Incompatible2012}
1728
1729 @begin{DiffWord2012}
1730 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0120-1]}
1731 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Added wording to specify that
1732 a loop propagates any exceptions propagated by the execution of an iterator.
1733 Since that's what naturally would happen from a macro-style expansion of the
1734 parts of an iterator, and no other interpretation makes sense given the way
1735 the rest of Ada works, we consider it so unlikely that any Ada 2012
1736 implementation ever did anything else that we don't document this as a
1737 possible inconsistency.]}
1738 @end{DiffWord2012}
1739
1740
1741 @RMNewPageVer{Version=[0]}@Comment{For printed version of Ada 95}
1742 @RMNewPageVer{Version=[1]}@Comment{For printed version of Ada 95 + TC1 RM}
1743 @RMNewPageVer{Version=[2]}@Comment{For printed version of Ada 2005 RM}
1744 @LabeledClause{Block Statements}
1745
1746 @begin{Intro}
1747 @Redundant[A @nt{block_statement} encloses a
1748 @nt{handled_sequence_of_statements}
1749 optionally preceded by a @nt{declarative_part}.]
1750 @end{Intro}
1751
1752 @begin{Syntax}
1753 @Syn{lhs=<block_statement>,rhs="
1754 [@SynI{block_}@Syn2{statement_identifier}:]
1755 [@key{declare}
1756 @Syn2{declarative_part}]
1757 @key{begin}
1758 @Syn2{handled_sequence_of_statements}
1759 @key{end} [@SynI{block_}@Syn2{identifier}];"}
1760
1761 @begin(SyntaxText)
1762 If a @nt{block_statement} has a @SynI{block_}@nt{statement_identifier},
1763 then the @nt<identifier> shall be repeated after the @key{end};
1764 otherwise, there shall not be an @nt<identifier> after the @key{end}.
1765 @end(SyntaxText)
1766 @end{Syntax}
1767
1768 @begin{StaticSem}
1769 A @nt{block_statement} that has no
1770 explicit @nt{declarative_part} has an implicit empty
1771 @nt{declarative_part}.
1772 @begin{Ramification}
1773 Thus, other rules can always
1774 refer to the @nt{declarative_part} of a @nt<block_statement>.
1775 @end{Ramification}
1776 @end{StaticSem}
1777
1778 @begin{RunTime}
1779 @PDefn2{Term=[execution], Sec=(block_statement)}
1780 The execution of a @nt{block_statement} consists of the elaboration
1781 of its @nt{declarative_part} followed by the execution of
1782 its @nt{handled_sequence_of_statements}.
1783 @end{RunTime}
1784
1785 @begin{Examples}
1786 @Leading@keepnext@i{Example of a block statement with a local variable:}
1787 @begin{Example}
1788 Swap:
1789 @key[declare]
1790 Temp : Integer;
1791 @key[begin]
1792 Temp := V; V := U; U := Temp;
1793 @key[end] Swap;
1794 @end{Example}
1795 @begin{Ramification}
1796 If task objects are declared within a @nt{block_statement} whose execution
1797 is completed, the @nt{block_statement} is not left until all its dependent
1798 tasks are terminated
1799 (see @RefSecNum{Assignment and Finalization}).
1800 This rule applies to completion caused by a transfer of control.
1801
1802 Within a @nt{block_statement}, the block name can be used in expanded
1803 names denoting local entities such as Swap.Temp in the above example
1804 (see @RefSecNum{Selected Components}).
1805 @end{Ramification}
1806 @end{Examples}
1807
1808 @begin{DiffWord83}
1809 The syntax rule for @nt{block_statement} now uses the syntactic category
1810 @nt{handled_sequence_of_statements}.
1811 @end{DiffWord83}
1812
1813 @LabeledClause{Exit Statements}
1814
1815 @begin{Intro}
1816 @Redundant[An @nt{exit_statement} is used to complete the execution
1817 of an enclosing @nt{loop_statement}; the
1818 completion is conditional if the @nt{exit_statement} includes a
1819 @nt{condition}.]
1820 @end{Intro}
1821
1822 @begin{Syntax}
1823 @Syn{lhs=<exit_statement>,rhs="
1824 @key{exit} [@SynI{loop_}@Syn2{name}] [@key{when} @Syn2{condition}];"}
1825 @end{Syntax}
1826
1827 @begin{Resolution}
1828 The @i(loop_)@nt{name}, if any, in an @nt{exit_statement} shall resolve to
1829 denote a @nt{loop_statement}.
1830 @end{Resolution}
1831
1832 @begin{Legality}
1833 @Defn2{Term=[apply], Sec=(to a @nt{loop_statement} by an @nt{exit_statement})}
1834 Each @nt{exit_@!statement} @i{applies to} a
1835 @nt{loop_@!statement}; this is the @nt{loop_@!statement} being exited.
1836 An @nt{exit_@!statement} with a @nt{name} is only allowed within the
1837 @nt{loop_@!statement} denoted by the @nt{name},
1838 and applies to that @nt{loop_@!statement}.
1839 An @nt{exit_@!statement} without a @nt{name} is only allowed within a
1840 @nt{loop_@!statement}, and applies to the innermost enclosing one.
1841 An @nt{exit_@!statement} that applies to a given @nt{loop_@!statement}
1842 shall not appear within a body or @nt{accept_@!statement}, if
1843 this construct is itself enclosed by the given @nt{loop_statement}.
1844 @end{Legality}
1845
1846 @begin{RunTime}
1847 @PDefn2{Term=[execution], Sec=(exit_statement)}
1848 For the execution of an @nt{exit_statement}, the @nt{condition}, if
1849 present, is first evaluated.
1850 If the value of the @nt{condition} is True, or if there is no @nt{condition},
1851 a transfer of control is done to complete the @nt{loop_@!statement}.
1852 If the value of the @nt{condition} is False, no transfer of control takes
1853 place.
1854 @end{RunTime}
1855
1856 @begin{Notes}
1857 Several nested loops can be exited by an @nt{exit_statement} that names
1858 the outer loop.
1859 @end{Notes}
1860
1861 @begin{Examples}
1862 @i{Examples of loops with exit statements:}
1863 @begin{Example}
1864 @key[for] N @key[in] 1 .. Max_Num_Items @key[loop]
1865 Get_New_Item(New_Item);
1866 Merge_Item(New_Item, Storage_File);
1867 @key[exit] @key[when] New_Item = Terminal_Item;
1868 @key[end] @key[loop];
1869
1870 Main_Cycle:
1871 @key[loop]
1872 --@RI{ initial statements}
1873 @key[exit] Main_Cycle @key[when] Found;
1874 --@RI{ final statements}
1875 @key[end] @key[loop] Main_Cycle;
1876 @end{Example}
1877 @end{Examples}
1878
1879 @LabeledClause{Goto Statements}
1880
1881 @begin{Intro}
1882 @Redundant[A @nt{goto_statement} specifies an explicit transfer of control
1883 from this @nt{statement} to a target
1884 statement with a given label.]
1885 @end{Intro}
1886
1887 @begin{Syntax}
1888 @Syn{lhs=<goto_statement>,rhs="@key{goto} @SynI{label_}@Syn2{name};"}
1889 @end{Syntax}
1890
1891 @begin{Resolution}
1892 @Defn2{Term=[target statement], Sec=(of a @nt{goto_statement})}
1893 The @i(label_)@nt{name} shall resolve to denote a @nt<label>;
1894 the @nt{statement} with that @nt{label} is the @i(target statement).
1895 @end{Resolution}
1896
1897 @begin{Legality}
1898 The innermost @nt{sequence_of_statements} that encloses the target
1899 statement shall also enclose the @nt{goto_statement}.
1900 Furthermore, if a @nt{goto_statement} is enclosed by an
1901 @nt{accept_statement} or a body, then the target
1902 statement shall not be outside this enclosing construct.
1903 @begin{Ramification}
1904 The @nt{goto_statement} can be a @nt{statement} of an inner
1905 @ntf{sequence_}.
1906
1907 It follows from the second rule that if the target @nt{statement}
1908 is enclosed by such a construct, then the @nt{goto_statement}
1909 cannot be outside.
1910 @end{Ramification}
1911 @end{Legality}
1912
1913 @begin{RunTime}
1914 @PDefn2{Term=[execution], Sec=(goto_statement)}
1915 The execution of a @nt{goto_statement} transfers control to the
1916 target statement, completing the execution
1917 of any @nt<compound_statement> that encloses the @nt<goto_statement>
1918 but does not enclose the target.
1919 @end{RunTime}
1920
1921 @begin{Notes}
1922 The above rules allow transfer of control to a @nt{statement} of an
1923 enclosing @nt{sequence_of_statements} but not the reverse. Similarly,
1924 they prohibit transfers of control such as between alternatives of a
1925 @nt{case_statement}, @nt{if_statement}, or @nt{select_statement};
1926 between @nt{exception_handler}s; or from an @nt{exception_handler} of
1927 a @nt{handled_sequence_of_statements}
1928 back to its @nt{sequence_of_statements}.
1929 @end{Notes}
1930
1931 @begin{Examples}
1932 @Leading@keepnext@i{Example of a loop containing a goto statement:}
1933 @begin{Example}
1934 <<Sort>>
1935 @key[for] I @key[in] 1 .. N-1 @key[loop]
1936 @key[if] A(I) > A(I+1) @key[then]
1937 Exchange(A(I), A(I+1));
1938 @key[goto] Sort;
1939 @key[end] @key[if];
1940 @key[end] @key[loop];
1941 @end{Example}
1942 @end{Examples}