| 1 |
@Part(05, Root="ada.mss") |
| 2 |
|
| 3 |
@Comment{$Date: 2006/10/17 05:29:45 $} |
| 4 |
@LabeledSection{Statements} |
| 5 |
|
| 6 |
@Comment{$Source: e:\\cvsroot/ARM/Source/05.mss,v $} |
| 7 |
@Comment{$Revision: 1.31 $} |
| 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 |
@Redundant[This section describes the general rules applicable to all |
| 15 |
@nt{statement}s. |
| 16 |
Some @nt{statement}s are discussed in later sections: |
| 17 |
@nt{Procedure_@!call_@!statement}s and |
| 18 |
@Chg{Version=[2],New=[return statements],Old=[@nt{return_@!statement}s]} are |
| 19 |
described in @RefSec{Subprograms}. |
| 20 |
@nt{Entry_@!call_@!statement}s, @nt{requeue_@!statement}s, |
| 21 |
@nt{delay_@!statement}s, @nt{accept_@!statement}s, |
| 22 |
@nt{select_@!statement}s, and @nt{abort_@!statement}s are described in |
| 23 |
@RefSec{Tasks and Synchronization}. |
| 24 |
@nt{Raise_@!statement}s are described in @RefSec{Exceptions}, |
| 25 |
and @nt{code_@!statement}s in |
| 26 |
@RefSecNum{Representation Issues}. |
| 27 |
The remaining forms of @nt{statement}s are presented in this |
| 28 |
section.] |
| 29 |
@end{Intro} |
| 30 |
|
| 31 |
@begin{DiffWord83} |
| 32 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]} |
| 33 |
The description of |
| 34 |
@Chg{Version=[2],New=[return statements],Old=[@nt{return_@!statement}s]} |
| 35 |
has been moved to |
| 36 |
@RefSec{Return Statements}, so that it is closer to the |
| 37 |
description of subprograms. |
| 38 |
@end{DiffWord83} |
| 39 |
|
| 40 |
@LabeledClause{Simple and Compound Statements - Sequences of Statements} |
| 41 |
|
| 42 |
@begin{Intro} |
| 43 |
@Redundant[A @nt<statement> is either simple or compound. |
| 44 |
A @nt<simple_statement> encloses |
| 45 |
no other @nt<statement>. A @nt<compound_statement> can enclose |
| 46 |
@nt<simple_statement>s and other @nt<compound_statement>s.] |
| 47 |
@end{Intro} |
| 48 |
|
| 49 |
@begin{Syntax} |
| 50 |
@Syn{lhs=<sequence_of_statements>,rhs="@Syn2{statement} {@Syn2{statement}}"} |
| 51 |
|
| 52 |
|
| 53 |
@Syn{lhs=<statement>,rhs=" |
| 54 |
{@Syn2{label}} @Syn2{simple_statement} | {@Syn2{label}} @Syn2{compound_statement}"} |
| 55 |
|
| 56 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]} |
| 57 |
@Syn{tabs=[P31], lhs=<simple_statement>,rhs="@Syn2{null_statement} |
| 58 |
| @Syn2{assignment_statement}@\| @Syn2{exit_statement} |
| 59 |
| @Syn2{goto_statement}@\| @Syn2{procedure_call_statement} |
| 60 |
| @Chg{Version=[2],New=[@Syn2{simple_return_statement}],Old=[@Syn2{return_statement}]}@\| @Syn2{entry_call_statement} |
| 61 |
| @Syn2{requeue_statement}@\| @Syn2{delay_statement} |
| 62 |
| @Syn2{abort_statement}@\| @Syn2{raise_statement} |
| 63 |
| @Syn2{code_statement}"} |
| 64 |
|
| 65 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]} |
| 66 |
@Syn{tabs=[P31], lhs=<compound_statement>,rhs=" |
| 67 |
@Syn2{if_statement}@\| @Syn2{case_statement} |
| 68 |
| @Syn2{loop_statement}@\| @Syn2{block_statement}@Chg{Version=[2],New=[ |
| 69 |
| @Syn2{extended_return_statement}],Old=[]} |
| 70 |
| @Syn2{accept_statement}@\| @Syn2{select_statement}"} |
| 71 |
|
| 72 |
@Syn{lhs=<null_statement>,rhs="@key{null};"} |
| 73 |
|
| 74 |
@Syn{lhs=<label>,rhs="<<@SynI{label_}@Syn2{statement_identifier}>>"} |
| 75 |
|
| 76 |
@Syn{lhs=<statement_identifier>,rhs="@Syn2{direct_name}"} |
| 77 |
|
| 78 |
@begin(SyntaxText) |
| 79 |
The @nt<direct_name> of a @nt<statement_identifier> shall |
| 80 |
be an @nt<identifier> (not an @nt<operator_symbol>). |
| 81 |
@end(SyntaxText) |
| 82 |
@end{Syntax} |
| 83 |
|
| 84 |
@begin{Resolution} |
| 85 |
The @nt<direct_name> of a @nt<statement_identifier> shall resolve to |
| 86 |
denote its corresponding implicit declaration (see below). |
| 87 |
@end{Resolution} |
| 88 |
|
| 89 |
@begin{Legality} |
| 90 |
Distinct @nt{identifier}s shall be used for all |
| 91 |
@nt<statement_identifier>s that |
| 92 |
appear in the same body, including |
| 93 |
inner @nt{block_statement}s |
| 94 |
but excluding inner program units. |
| 95 |
@end{Legality} |
| 96 |
|
| 97 |
@begin{StaticSem} |
| 98 |
For each @nt<statement_identifier>, |
| 99 |
there is an implicit declaration (with the specified @nt<identifier>) |
| 100 |
at the end of the @nt{declarative_part} of the |
| 101 |
innermost @nt{block_statement} or body that |
| 102 |
encloses the @nt{statement_identifier}. |
| 103 |
The implicit declarations occur in the same order as the |
| 104 |
@nt<statement_identifier>s occur in the source text. |
| 105 |
If a usage name denotes such an implicit declaration, the entity it |
| 106 |
denotes is the @nt<label>, @nt<loop_statement>, |
| 107 |
or @nt<block_statement> with the given @nt<statement_identifier>. |
| 108 |
@begin{Reason} |
| 109 |
We talk in terms of individual @nt<statement_identifier>s here |
| 110 |
rather than in terms of the corresponding statements, since |
| 111 |
a given @nt{statement} may have multiple @nt<statement_identifier>s. |
| 112 |
|
| 113 |
A @nt{block_statement} that has no |
| 114 |
explicit @nt{declarative_part} has an implicit empty |
| 115 |
@nt{declarative_part}, |
| 116 |
so this rule can safely |
| 117 |
refer to the @nt{declarative_part} of a @nt<block_statement>. |
| 118 |
|
| 119 |
The scope of a declaration starts at the place of the declaration |
| 120 |
itself (see @RefSecNum{Scope of Declarations}). |
| 121 |
In the case of a label, loop, or block name, it |
| 122 |
follows from this rule that the scope of the implicit declaration |
| 123 |
starts before the first explicit occurrence of the corresponding |
| 124 |
name, since this occurrence is either in a statement label, a |
| 125 |
@nt{loop_statement}, a @nt{block_statement}, or a |
| 126 |
@nt{goto_statement}. An implicit |
| 127 |
declaration in a @nt{block_statement} may hide a declaration given in an |
| 128 |
outer program unit or @nt{block_statement} (according to the usual rules |
| 129 |
of hiding explained in @RefSecNum{Visibility}). |
| 130 |
|
| 131 |
The syntax rule for @nt{label} uses @nt{statement_identifier} which |
| 132 |
is a @nt<direct_name> (not a @nt{defining_identifier}), |
| 133 |
because labels are implicitly declared. The same applies to loop and |
| 134 |
block names. |
| 135 |
In other words, the @nt{label} itself is not the defining occurrence; |
| 136 |
the implicit declaration is. |
| 137 |
|
| 138 |
@Leading@;We cannot consider the @nt{label} to be a defining occurrence. |
| 139 |
An example that can tell the difference is this: |
| 140 |
@begin{example} |
| 141 |
@key[declare] |
| 142 |
--@RI{ Label Foo is implicitly declared here.} |
| 143 |
@key[begin] |
| 144 |
@key[for] Foo @key[in] ... @key[loop] |
| 145 |
... |
| 146 |
<<Foo>> --@RI{ Illegal.} |
| 147 |
... |
| 148 |
@key[end] @key[loop]; |
| 149 |
@key[end]; |
| 150 |
@end{example} |
| 151 |
|
| 152 |
The label in this example is hidden from itself by the loop parameter |
| 153 |
with the same name; |
| 154 |
the example is illegal. |
| 155 |
We considered creating a new syntactic category name, separate from |
| 156 |
@nt{direct_name} and @nt{selector_name}, for use in the case of statement |
| 157 |
labels. |
| 158 |
However, that would confuse the rules in Section 8, so we didn't do it. |
| 159 |
@end{Reason} |
| 160 |
@end{StaticSem} |
| 161 |
|
| 162 |
@begin{RunTime} |
| 163 |
@PDefn2{Term=[execution], Sec=(null_statement)} |
| 164 |
The execution of a @nt{null_statement} has no effect. |
| 165 |
|
| 166 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00318-02]} |
| 167 |
@Defn{transfer of control} |
| 168 |
A @i{transfer of control} is |
| 169 |
the run-time action of an @nt{exit_statement}, |
| 170 |
@Chg{Version=[2],New=[return statement],Old=[@nt{return_statement}]}, |
| 171 |
@nt{goto_statement}, |
| 172 |
or @nt{requeue_statement}, |
| 173 |
selection of a @nt{terminate_alternative}, |
| 174 |
raising of an exception, |
| 175 |
or an abort, |
| 176 |
which causes |
| 177 |
the next action performed to be one other than what would normally be |
| 178 |
expected from the other rules of the language. |
| 179 |
@Redundant[As explained in |
| 180 |
@RefSecNum{Completion and Finalization}, |
| 181 |
a transfer of control can cause the execution of constructs to be |
| 182 |
completed and then left, |
| 183 |
which may trigger finalization.] |
| 184 |
|
| 185 |
@PDefn2{Term=[execution], Sec=(sequence_of_statements)} |
| 186 |
The execution of a @nt{sequence_of_statements} consists of the execution |
| 187 |
of the individual @nt{statement}s in succession |
| 188 |
until the @ntf{sequence_} is completed. |
| 189 |
@begin{Ramification} |
| 190 |
It could be completed by reaching the end of it, |
| 191 |
or by a transfer of control. |
| 192 |
@end{Ramification} |
| 193 |
@end{RunTime} |
| 194 |
|
| 195 |
@begin{Notes} |
| 196 |
A @nt<statement_identifier> that appears immediately within |
| 197 |
the declarative region of a |
| 198 |
named @nt<loop_statement> or an @nt<accept_statement> is nevertheless |
| 199 |
implicitly declared immediately within the declarative region |
| 200 |
of the innermost enclosing body or @nt<block_statement>; |
| 201 |
in other words, the expanded name for a named statement is |
| 202 |
not affected by whether the statement occurs inside or outside |
| 203 |
a named loop or an @nt<accept_statement> @em only nesting |
| 204 |
within @nt<block_statement>s is relevant to the form of its |
| 205 |
expanded name. |
| 206 |
@begin{Discussion} |
| 207 |
@Leading@keepnext@;Each comment in the following example gives the |
| 208 |
expanded name associated with an entity declared in the task body: |
| 209 |
@begin{Example} |
| 210 |
@key(task body) Compute @key(is) |
| 211 |
Sum : Integer := 0; --@RI[ Compute.Sum] |
| 212 |
@key(begin) |
| 213 |
Outer: --@RI[ Compute.Outer] |
| 214 |
@key(for) I @key(in) 1..10 @key(loop) --@RI[ Compute.Outer.I] |
| 215 |
Blk: --@RI[ Compute.Blk] |
| 216 |
@key(declare) |
| 217 |
Sum : Integer := 0; --@RI[ Compute.Blk.Sum] |
| 218 |
@key(begin) |
| 219 |
@key(accept) Ent(I : out Integer; J : in Integer) @key(do) |
| 220 |
--@RI[ Compute.Ent.I, Compute.Ent.J] |
| 221 |
Compute.Ent.I := Compute.Outer.I; |
| 222 |
Inner: --@RI[ Compute.Blk.Inner] |
| 223 |
@key(for) J @key(in) 1..10 @key(loop) |
| 224 |
--@RI[ Compute.Blk.Inner.J] |
| 225 |
Sum := Sum + Compute.Blk.Inner.J * Compute.Ent.J; |
| 226 |
@key(end loop) Inner; |
| 227 |
@key(end) Ent; |
| 228 |
Compute.Sum := Compute.Sum + Compute.Blk.Sum; |
| 229 |
@key(end) Blk; |
| 230 |
@key(end loop) Outer; |
| 231 |
Record_Result(Sum); |
| 232 |
@key(end) Compute; |
| 233 |
@end{Example} |
| 234 |
@end{Discussion} |
| 235 |
@end{Notes} |
| 236 |
|
| 237 |
@begin{Examples} |
| 238 |
@Leading@keepnext@i{Examples of labeled statements:} |
| 239 |
@begin{Example} |
| 240 |
<<Here>> <<Ici>> <<Aqui>> <<Hier>> @key[null]; |
| 241 |
|
| 242 |
<<After>> X := 1; |
| 243 |
@end{Example} |
| 244 |
@end{Examples} |
| 245 |
|
| 246 |
@begin{Extend83} |
| 247 |
@Defn{extensions to Ada 83} |
| 248 |
The @nt{requeue_statement} is new. |
| 249 |
@end{Extend83} |
| 250 |
|
| 251 |
@begin{DiffWord83} |
| 252 |
We define the syntactic category @nt<statement_identifier> to simplify |
| 253 |
the description. It is used for labels, loop names, and block names. |
| 254 |
We define the entity associated with the implicit declarations |
| 255 |
of statement names. |
| 256 |
|
| 257 |
Completion includes completion caused by a transfer of control, |
| 258 |
although RM83-5.1(6) did not take this view. |
| 259 |
@end{DiffWord83} |
| 260 |
|
| 261 |
@begin{Extend95} |
| 262 |
@ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]} |
| 263 |
@ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} |
| 264 |
The @nt{extended_return_statement} is new (@nt{simple_return_statement} |
| 265 |
is merely renamed).]} |
| 266 |
@end{Extend95} |
| 267 |
|
| 268 |
|
| 269 |
@LabeledClause{Assignment Statements} |
| 270 |
|
| 271 |
@begin{Intro} |
| 272 |
@Redundant[An @nt{assignment_statement} |
| 273 |
replaces the current value of |
| 274 |
a variable with the result of evaluating an |
| 275 |
@nt<expression>.] |
| 276 |
@end{Intro} |
| 277 |
|
| 278 |
@begin{Syntax} |
| 279 |
@Syn{lhs=<assignment_statement>,rhs=" |
| 280 |
@SynI{variable_}@Syn2{name} := @Syn2{expression};"} |
| 281 |
@end{Syntax} |
| 282 |
|
| 283 |
@begin{Intro} |
| 284 |
The execution of an @nt<assignment_statement> includes |
| 285 |
the evaluation of the @nt<expression> and the @i(assignment) |
| 286 |
of the value of the @nt<expression> into the @i(target). |
| 287 |
@RootDefn{assignment operation} |
| 288 |
@IndexSee{Term=[assign], See=(assignment operation)} |
| 289 |
@Redundant[An assignment operation (as opposed to |
| 290 |
an @nt<assignment_@!statement>) is performed in other contexts |
| 291 |
as well, including object initialization and by-copy parameter |
| 292 |
passing.] |
| 293 |
@Defn2{Term=[target], Sec=(of an assignment operation)} |
| 294 |
@Defn2{Term=[target], Sec=(of an @nt{assignment_statement})} |
| 295 |
The @i{target} of an assignment operation |
| 296 |
is the view of the object to which a value is being assigned; |
| 297 |
the target of an @nt{assignment_@!statement} is the variable denoted by |
| 298 |
the @SynI{variable_}@nt{name}. |
| 299 |
@begin{Discussion} |
| 300 |
Don't confuse this notion of the @lquotes@;target@rquotes@; of an assignment |
| 301 |
with the notion of the @lquotes@;target object@rquotes@; of an entry call or requeue. |
| 302 |
|
| 303 |
Don't confuse the term @lquotes@;assignment operation@rquotes@; with the |
| 304 |
@nt{assignment_statement}. |
| 305 |
The assignment operation is just one part of the execution of an |
| 306 |
@nt{assignment_statement}. |
| 307 |
The assignment operation is also a part of the execution of various |
| 308 |
other constructs; see @RefSec{Completion and Finalization} for a complete |
| 309 |
list. |
| 310 |
Note that when we say, @lquotes@;such-and-such is assigned to so-and-so@rquotes@;, |
| 311 |
we mean that the assignment operation is being applied, and that |
| 312 |
so-and-so is the target of the assignment operation. |
| 313 |
@end{Discussion} |
| 314 |
@end{Intro} |
| 315 |
|
| 316 |
@begin{Resolution} |
| 317 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]} |
| 318 |
@PDefn2{Term=[expected type], |
| 319 |
Sec=(assignment_statement variable_name)} |
| 320 |
The @i(variable_)@nt<name> of an @nt<assignment_statement> |
| 321 |
is expected to be of any @Chg{Version=[2],New=[],Old=[nonlimited ]}type. |
| 322 |
@PDefn2{Term=[expected type], |
| 323 |
Sec=(assignment_statement expression)} |
| 324 |
The expected type for the @nt<expression> is |
| 325 |
the type of the target. |
| 326 |
@begin{ImplNote} |
| 327 |
@Leading@keepnext@;An @nt<assignment_statement> as a whole is a "complete context," |
| 328 |
so if the @i{variable_}@nt<name> of an @nt<assignment_statement> is |
| 329 |
overloaded, the @nt<expression> can be used to help disambiguate it. |
| 330 |
For example: |
| 331 |
@begin{Example} |
| 332 |
@key[type] P1 @key[is access] R1; |
| 333 |
@key[type] P2 @key[is access] R2; |
| 334 |
|
| 335 |
@key[function] F return P1; |
| 336 |
@key[function] F return P2; |
| 337 |
|
| 338 |
X : R1; |
| 339 |
@key[begin] |
| 340 |
F.all := X; --@RI[ Right hand side helps resolve left hand side] |
| 341 |
@end{Example} |
| 342 |
@end{ImplNote} |
| 343 |
@end{Resolution} |
| 344 |
|
| 345 |
@begin{Legality} |
| 346 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]} |
| 347 |
The target @Redundant[denoted by the |
| 348 |
@i(variable_)@nt<name>] shall be a variable@Chg{Version=[2],New=[ of a |
| 349 |
nonlimited type],Old=[]}. |
| 350 |
|
| 351 |
If the target is of a tagged class-wide type @i(T)'Class, then |
| 352 |
the @nt<expression> shall either be dynamically tagged, |
| 353 |
or of type @i(T) and tag-indeterminate |
| 354 |
(see @RefSecNum{Dispatching Operations of Tagged Types}). |
| 355 |
@begin{Reason} |
| 356 |
This is consistent with the general rule that a single |
| 357 |
dispatching operation shall not have both dynamically tagged and |
| 358 |
statically tagged operands. Note that for an object |
| 359 |
initialization (as opposed to the @nt{assignment_statement}), |
| 360 |
a statically tagged initialization expression is permitted, |
| 361 |
since there is no chance for confusion (or Tag_Check failure). |
| 362 |
Also, in an object initialization, tag-indeterminate expressions |
| 363 |
of any type covered by @i(T)'Class would be allowed, but with |
| 364 |
an @nt{assignment_statement}, that might not work if the tag of the target |
| 365 |
was for a type that didn't have one of the dispatching operations |
| 366 |
in the tag-indeterminate expression. |
| 367 |
@end{Reason} |
| 368 |
@end{Legality} |
| 369 |
|
| 370 |
@begin{RunTime} |
| 371 |
@PDefn2{Term=[execution], Sec=(assignment_statement)} |
| 372 |
For the execution of an @nt{assignment_statement}, |
| 373 |
the @i(variable_)@nt<name> and the @nt<expression> |
| 374 |
are first evaluated in an arbitrary order. |
| 375 |
@begin{Ramification} |
| 376 |
Other rules of the language may require that the |
| 377 |
bounds of the variable be determined prior to evaluating |
| 378 |
the @nt<expression>, but that does not necessarily require |
| 379 |
evaluation of the @i(variable_)@nt<name>, as pointed out by the ACID. |
| 380 |
@end{Ramification} |
| 381 |
|
| 382 |
@Leading@keepnext@;When the type of the target is class-wide: |
| 383 |
@begin(itemize) |
| 384 |
@PDefn2{Term=[controlling tag value], Sec=(for the @nt{expression} |
| 385 |
in an @nt{assignment_statement})} |
| 386 |
If the @nt<expression> is tag-indeterminate |
| 387 |
(see @RefSecNum{Dispatching Operations of Tagged Types}), then the controlling |
| 388 |
tag value for the @nt<expression> is the tag of the target; |
| 389 |
@begin{Ramification} |
| 390 |
See @RefSec(Dispatching Operations of Tagged Types). |
| 391 |
@end{Ramification} |
| 392 |
|
| 393 |
@IndexCheck{Tag_Check} |
| 394 |
@Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)} |
| 395 |
Otherwise @Redundant[(the @nt<expression> is dynamically tagged)], |
| 396 |
a check is made that the tag of |
| 397 |
the value of the @nt<expression> |
| 398 |
is the same as that of the target; |
| 399 |
if this check fails, Constraint_Error is raised. |
| 400 |
@end(itemize) |
| 401 |
|
| 402 |
The value of the @nt<expression> is converted to the subtype of the |
| 403 |
target. @Redundant[The conversion might raise an exception |
| 404 |
(see @RefSecNum{Type Conversions}).] |
| 405 |
@PDefn2{Term=[implicit subtype conversion],Sec=(assignment_statement)} |
| 406 |
@begin{Ramification} |
| 407 |
@RefSec(Type Conversions) defines what actions |
| 408 |
and checks are associated with subtype conversion. |
| 409 |
For non-array subtypes, it is just a constraint |
| 410 |
check presuming the types match. |
| 411 |
For array subtypes, it checks the lengths and slides if the |
| 412 |
target is constrained. |
| 413 |
@lquotes@;Sliding@rquotes@; means the array doesn't have to have the same bounds, |
| 414 |
so long as it is the same length. |
| 415 |
@end{Ramification} |
| 416 |
|
| 417 |
In cases involving controlled types, the target is finalized, |
| 418 |
and an anonymous object might be used as an intermediate in the assignment, |
| 419 |
as described in @RefSec{Completion and Finalization}. |
| 420 |
@Defn{assignment operation} |
| 421 |
@Defn2{Term=[assignment operation], |
| 422 |
Sec=(during execution of an @nt{assignment_statement})} |
| 423 |
In any case, |
| 424 |
the converted value of the @nt<expression> is then @i(assigned) to the target, |
| 425 |
which consists of the following two steps: |
| 426 |
@begin{Honest} |
| 427 |
@RefSecNum{Completion and Finalization} actually says that |
| 428 |
finalization happens always, but unless controlled types are involved, |
| 429 |
this finalization during an @nt{assignment_statement} does |
| 430 |
nothing. |
| 431 |
@end{Honest} |
| 432 |
@begin(itemize) |
| 433 |
The value of the target becomes the converted value. |
| 434 |
|
| 435 |
If any part of the target is controlled, its value |
| 436 |
is adjusted as explained in |
| 437 |
clause @RefSecNum{User-Defined Assignment and Finalization}. |
| 438 |
@PDefn2{Term=[adjustment], Sec=(as part of assignment)} |
| 439 |
@begin{Ramification} |
| 440 |
If any parts of the object are controlled, |
| 441 |
abort is deferred during the assignment operation itself, |
| 442 |
but not during the rest of the execution of an |
| 443 |
@nt<assignment_statement>. |
| 444 |
@end{Ramification} |
| 445 |
@end(itemize) |
| 446 |
|
| 447 |
@end{RunTime} |
| 448 |
|
| 449 |
@begin{Notes} |
| 450 |
The tag of an object never changes; |
| 451 |
in particular, an |
| 452 |
@nt{assignment_statement} |
| 453 |
does not change the tag of the target. |
| 454 |
|
| 455 |
@ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00363-01]} |
| 456 |
@ChgDeleted{Version=[2],Text=[The values of the discriminants of an object |
| 457 |
designated by an access value cannot be changed (not even by assigning a |
| 458 |
complete value to the object itself) since such objects are always constrained; |
| 459 |
however, subcomponents of such objects may be unconstrained.]} |
| 460 |
@begin{Ramification} |
| 461 |
The implicit subtype conversion described above for |
| 462 |
@nt{assignment_statement}s |
| 463 |
is performed only for the value of the right-hand side |
| 464 |
expression as a whole; it is not performed for subcomponents of the |
| 465 |
value. |
| 466 |
|
| 467 |
The determination of the type of the variable of an |
| 468 |
@nt{assignment_statement} may require consideration of the expression |
| 469 |
if the variable |
| 470 |
name can be interpreted as the name of a variable designated by the |
| 471 |
access value returned by a function call, and similarly, as a |
| 472 |
component or slice of such a variable |
| 473 |
(see @RefSec{The Context of Overload Resolution}). |
| 474 |
@end{Ramification} |
| 475 |
@end{Notes} |
| 476 |
|
| 477 |
@begin{Examples} |
| 478 |
@Leading@keepnext@i{Examples of assignment statements:} |
| 479 |
@begin{Example} |
| 480 |
Value := Max_Value - 1; |
| 481 |
Shade := Blue; |
| 482 |
|
| 483 |
Next_Frame(F)(M, N) := 2.5; --@RI{ see @RefSecNum{Indexed Components}} |
| 484 |
U := Dot_Product(V, W); --@RI{ see @RefSecNum{Subprogram Bodies}} |
| 485 |
|
| 486 |
Writer := (Status => Open, Unit => Printer, Line_Count => 60); --@RI{ see @RefSecNum{Variant Parts and Discrete Choices}} |
| 487 |
Next_Car.@key[all] := (72074, @key[null]); --@RI{ see @RefSecNum{Incomplete Type Declarations}} |
| 488 |
@end{Example} |
| 489 |
|
| 490 |
@begin{Wide} |
| 491 |
@Leading@keepnext@i{Examples involving scalar subtype conversions:} |
| 492 |
@end{Wide} |
| 493 |
@begin{Example} |
| 494 |
I, J : Integer @key[range] 1 .. 10 := 5; |
| 495 |
K : Integer @key[range] 1 .. 20 := 15; |
| 496 |
... |
| 497 |
|
| 498 |
I := J; --@RI{ identical ranges} |
| 499 |
K := J; --@RI{ compatible ranges} |
| 500 |
J := K; --@RI{ will raise Constraint_Error if K > 10} |
| 501 |
@end{Example} |
| 502 |
|
| 503 |
@begin{Wide} |
| 504 |
@leading@keepnext@i{Examples involving array subtype conversions:} |
| 505 |
@end{Wide} |
| 506 |
@begin{Example} |
| 507 |
A : String(1 .. 31); |
| 508 |
B : String(3 .. 33); |
| 509 |
... |
| 510 |
|
| 511 |
A := B; --@RI{ same number of components} |
| 512 |
|
| 513 |
A(1 .. 9) := "tar sauce"; |
| 514 |
A(4 .. 12) := A(1 .. 9); --@RI{ A(1 .. 12) = "tartar sauce"} |
| 515 |
@end{Example} |
| 516 |
@end{Examples} |
| 517 |
|
| 518 |
@begin{Notes} |
| 519 |
@i{Notes on the examples:} |
| 520 |
@nt{Assignment_statement}s are allowed even in the case of overlapping |
| 521 |
slices of the same array, |
| 522 |
because the @SynI{variable_}@nt{name} and @nt{expression} |
| 523 |
are both evaluated before copying the value into the variable. |
| 524 |
In the above example, an |
| 525 |
implementation yielding A(1 .. 12) = "tartartartar" would be |
| 526 |
incorrect. |
| 527 |
@end{Notes} |
| 528 |
|
| 529 |
@begin{Extend83} |
| 530 |
@Defn{extensions to Ada 83} |
| 531 |
We now allow user-defined finalization and value adjustment actions |
| 532 |
as part of @nt{assignment_statement}s |
| 533 |
(see @RefSec{User-Defined Assignment and Finalization}). |
| 534 |
@end{Extend83} |
| 535 |
|
| 536 |
@begin{DiffWord83} |
| 537 |
The special case of array assignment is subsumed by the concept |
| 538 |
of a subtype conversion, which is applied for all kinds of types, |
| 539 |
not just arrays. For arrays it provides @lquotes@;sliding@rquotes@;. For numeric |
| 540 |
types it provides conversion of a value of a universal type to |
| 541 |
the specific type of the target. For other types, |
| 542 |
it generally has no run-time effect, other than a constraint |
| 543 |
check. |
| 544 |
|
| 545 |
We now cover in a general way in @RefSecNum{Operations of Discriminated Types} |
| 546 |
the erroneous execution possible due to |
| 547 |
changing the value of a discriminant when |
| 548 |
the variable in an @nt<assignment_statement> is a subcomponent |
| 549 |
that depends on discriminants. |
| 550 |
@end{DiffWord83} |
| 551 |
|
| 552 |
@begin{Incompatible95} |
| 553 |
@ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]} |
| 554 |
@Chg{Version=[2],New=[@Defn{incompatibilities with Ada 95} |
| 555 |
The change of the limited check from a resolution rule to |
| 556 |
a legality rule is not quite upward compatible. For example],Old=[]}. |
| 557 |
@begin{Example} |
| 558 |
@Chg{Version=[2],New=[@key{type} AccNonLim @key{is} @key{access} NonLim; |
| 559 |
@key{function} Foo (Arg : in Integer) @key{return} AccNonLim; |
| 560 |
@key{type} AccLim @key{is} @key{access} Lim; |
| 561 |
@key{function} Foo (Arg : in Integer) @key{return} AccLim; |
| 562 |
Foo(2).@key{all} := Foo(1).@key{all};],Old=[]}. |
| 563 |
@end{Example} |
| 564 |
@ChgAdded{Version=[2],Text=[where NonLim is a nonlimited type and Lim is a |
| 565 |
limited type. The assignment is legal in Ada 95 (only the first Foo would be |
| 566 |
considered), and is ambiguous in Ada 2005. We made the change because we want |
| 567 |
limited types to be as similar to nonlimited types as possible. Limited |
| 568 |
expressions are now allowed in all other contexts (with a similar |
| 569 |
incompatibility), and it would be odd if assignments had different resolution |
| 570 |
rules (which would eliminate ambiguities in some cases). Moreover, examples |
| 571 |
like this one are rare, as they depend on assigning into overloaded function |
| 572 |
calls.]} |
| 573 |
@end{Incompatible95} |
| 574 |
|
| 575 |
|
| 576 |
@LabeledClause{If Statements} |
| 577 |
|
| 578 |
@begin{Intro} |
| 579 |
@Redundant[An @nt{if_statement} selects for execution at most one of |
| 580 |
the enclosed @ntf{sequences_of_statements}, depending on the (truth) |
| 581 |
value of one or more corresponding @nt{condition}s.] |
| 582 |
@end{Intro} |
| 583 |
|
| 584 |
@begin{Syntax} |
| 585 |
@Syn{lhs=<if_statement>,rhs=" |
| 586 |
@key{if} @Syn2{condition} @key{then} |
| 587 |
@Syn2{sequence_of_statements} |
| 588 |
{@key{elsif} @Syn2{condition} @key{then} |
| 589 |
@Syn2{sequence_of_statements}} |
| 590 |
[@key{else} |
| 591 |
@Syn2{sequence_of_statements}] |
| 592 |
@key{end} @key{if};"} |
| 593 |
|
| 594 |
|
| 595 |
@Syn{lhs=<condition>,rhs="@SynI{boolean_}@Syn2{expression}"} |
| 596 |
@end{Syntax} |
| 597 |
|
| 598 |
@begin{Resolution} |
| 599 |
@PDefn2{Term=[expected type], Sec=(condition)} |
| 600 |
A @nt{condition} is expected to be of any boolean type. |
| 601 |
@end{Resolution} |
| 602 |
|
| 603 |
@begin{RunTime} |
| 604 |
@PDefn2{Term=[execution], Sec=(if_statement)} |
| 605 |
For the execution of an @nt{if_statement}, the @nt{condition} specified |
| 606 |
after @key{if}, and any @nt{condition}s specified after @key{elsif}, are |
| 607 |
evaluated in succession (treating a final @key{else} as @key{elsif} True |
| 608 |
@key{then}), until one evaluates to True or |
| 609 |
all @nt{condition}s are evaluated and |
| 610 |
yield False. |
| 611 |
If a @nt{condition} evaluates to True, then the |
| 612 |
corresponding @nt{sequence_of_statements} is executed; otherwise none of |
| 613 |
them is executed. |
| 614 |
@begin{Ramification} |
| 615 |
The part about all evaluating to False can't happen if |
| 616 |
there is an @key{else}, since that is herein considered equivalent to |
| 617 |
@key{elsif} True @key{then}. |
| 618 |
@end{Ramification} |
| 619 |
@end{RunTime} |
| 620 |
|
| 621 |
@begin{Examples} |
| 622 |
@Leading@keepnext@i{Examples of if statements:} |
| 623 |
@begin{Example} |
| 624 |
@key[if] Month = December @key[and] Day = 31 @key[then] |
| 625 |
Month := January; |
| 626 |
Day := 1; |
| 627 |
Year := Year + 1; |
| 628 |
@key[end] @key[if]; |
| 629 |
|
| 630 |
@key[if] Line_Too_Short @key[then] |
| 631 |
@key[raise] Layout_Error; |
| 632 |
@key[elsif] Line_Full @key[then] |
| 633 |
New_Line; |
| 634 |
Put(Item); |
| 635 |
@key[else] |
| 636 |
Put(Item); |
| 637 |
@key[end] @key[if]; |
| 638 |
|
| 639 |
@key[if] My_Car.Owner.Vehicle /= My_Car @key[then] --@RI{ see @RefSecNum{Incomplete Type Declarations}} |
| 640 |
Report ("Incorrect data"); |
| 641 |
@key[end] @key[if]; |
| 642 |
@end{Example} |
| 643 |
@end{Examples} |
| 644 |
|
| 645 |
@LabeledClause{Case Statements} |
| 646 |
|
| 647 |
@begin{Intro} |
| 648 |
@Redundant[A @nt{case_statement} selects for execution one of a |
| 649 |
number of alternative @ntf{sequences_of_statements}; the chosen |
| 650 |
alternative is defined by the value of an expression.] |
| 651 |
@end{Intro} |
| 652 |
|
| 653 |
@begin{Syntax} |
| 654 |
@Syn{lhs=<case_statement>,rhs=" |
| 655 |
@key{case} @Syn2{expression} @key{is} |
| 656 |
@Syn2{case_statement_alternative} |
| 657 |
{@Syn2{case_statement_alternative}} |
| 658 |
@key{end} @key{case};"} |
| 659 |
|
| 660 |
|
| 661 |
@Syn{lhs=<case_statement_alternative>,rhs=" |
| 662 |
@key{when} @Syn2{discrete_choice_list} => |
| 663 |
@Syn2{sequence_of_statements}"} |
| 664 |
@end{Syntax} |
| 665 |
|
| 666 |
@begin{Resolution} |
| 667 |
@PDefn2{Term=[expected type], Sec=(case expression)} |
| 668 |
The @nt{expression} is expected to be of any discrete type. |
| 669 |
@PDefn2{Term=[expected type], |
| 670 |
Sec=(case_statement_alternative discrete_choice)} |
| 671 |
The expected type for each @nt{discrete_choice} is the type of the |
| 672 |
@nt{expression}. |
| 673 |
@end{Resolution} |
| 674 |
|
| 675 |
@begin{Legality} |
| 676 |
The @nt{expression}s and @nt{discrete_range}s given as |
| 677 |
@nt{discrete_choice}s of a @nt{case_statement} shall be static. |
| 678 |
@Redundant[A @nt{discrete_choice} @key(others), if present, |
| 679 |
shall appear alone and in the last @nt{discrete_choice_list}.] |
| 680 |
|
| 681 |
The possible values of the @nt{expression} shall be covered as follows: |
| 682 |
@begin{itemize} |
| 683 |
If the @nt{expression} is a @nt{name} @Redundant[(including a |
| 684 |
@nt<type_conversion> or a @nt<function_call>)] having |
| 685 |
a static and constrained nominal subtype, or |
| 686 |
is a @nt{qualified_expression} whose |
| 687 |
@nt{subtype_mark} denotes a static and constrained |
| 688 |
scalar subtype, |
| 689 |
then each non-@key{others} @nt{discrete_choice} shall cover only values in |
| 690 |
that subtype, and each value of that subtype shall |
| 691 |
be covered by some @nt{discrete_choice} |
| 692 |
@Redundant[(either explicitly or by @key(others))]. |
| 693 |
@begin{Ramification} |
| 694 |
Although not official @nt<name>s of objects, a value conversion |
| 695 |
still has a defined nominal subtype, namely its target subtype. |
| 696 |
See @RefSecNum{Type Conversions}. |
| 697 |
@end{Ramification} |
| 698 |
|
| 699 |
If the type of the @nt{expression} is |
| 700 |
@i(root_integer), @i(universal_integer), or a descendant of a |
| 701 |
formal scalar type, |
| 702 |
then the @nt{case_statement} shall have an @key{others} |
| 703 |
@nt{discrete_choice}. |
| 704 |
@begin{Reason} |
| 705 |
This is because the base range is |
| 706 |
implementation defined for @i(root_integer) and @i(universal_integer), |
| 707 |
and not known statically in the case of a formal scalar type. |
| 708 |
@end{Reason} |
| 709 |
|
| 710 |
Otherwise, |
| 711 |
each value of the base range of the type of the @nt{expression} shall |
| 712 |
be covered |
| 713 |
@Redundant[(either explicitly or by @key(others))]. |
| 714 |
@end{itemize} |
| 715 |
|
| 716 |
Two distinct @nt{discrete_choice}s of a |
| 717 |
@nt{case_statement} shall not cover the same value. |
| 718 |
@begin{Ramification} |
| 719 |
The goal of these coverage rules is |
| 720 |
that any possible value of the @nt{expression} of a |
| 721 |
@nt{case_statement} should be covered by exactly one |
| 722 |
@nt{discrete_choice} of the |
| 723 |
@nt{case_statement}, and that this should be checked at compile time. |
| 724 |
The goal is achieved in most cases, but there are two minor |
| 725 |
loopholes: |
| 726 |
@begin{Itemize} |
| 727 |
If the expression reads an object with an invalid representation |
| 728 |
(e.g. an uninitialized object), |
| 729 |
then the value can be outside the covered range. |
| 730 |
This can happen for static constrained subtypes, as well as nonstatic or |
| 731 |
unconstrained subtypes. |
| 732 |
It cannot, however, happen if the @nt{case_statement} has the |
| 733 |
@nt{discrete_choice} @key{others}, because @key{others} covers all values, |
| 734 |
even those outside the subtype. |
| 735 |
|
| 736 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]} |
| 737 |
If the compiler chooses to represent the value of an expression of an |
| 738 |
unconstrained subtype in a way that includes values outside the bounds of the |
| 739 |
subtype, then those values can be outside the covered range. |
| 740 |
For example, if X: Integer := Integer'Last;, and the case @nt{expression} is |
| 741 |
X+1, then the implementation might choose to produce the correct value, which |
| 742 |
is outside the bounds of Integer. |
| 743 |
(It might raise Constraint_Error instead.) |
| 744 |
This case can only happen for non-generic subtypes that are either |
| 745 |
unconstrained or |
| 746 |
non@Chg{Version=[2],New=[],Old=[-]}@ChgNote{Make spelling consistent}static |
| 747 |
(or both). |
| 748 |
It can only happen if there is no @key{others} @nt{discrete_choice}. |
| 749 |
@end{Itemize} |
| 750 |
|
| 751 |
In the uninitialized variable case, the value might be anything; hence, any |
| 752 |
alternative can be chosen, or Constraint_Error can be raised. (We intend to |
| 753 |
prevent, however, jumping to random memory locations and the like.) |
| 754 |
In the out-of-range case, the behavior is more sensible: if there is an |
| 755 |
@key{others}, then the implementation may choose to raise Constraint_Error |
| 756 |
on the evaluation of the @nt{expression} (as usual), or it may choose |
| 757 |
to correctly evaluate the @nt{expression} and therefore choose the |
| 758 |
@key{others} alternative. |
| 759 |
Otherwise (no @key{others}), Constraint_Error is raised either way @em on |
| 760 |
the @nt{expression} evaluation, or for the @nt{case_statement} itself. |
| 761 |
|
| 762 |
For an enumeration type with a discontiguous set of internal codes |
| 763 |
(see @RefSecNum{Enumeration Representation Clauses}), |
| 764 |
the only way to get values in between the proper values |
| 765 |
is via an object with an invalid representation; |
| 766 |
there is no @lquotes@;out-of-range@rquotes@; situation that can produce them. |
| 767 |
@end{Ramification} |
| 768 |
@end{Legality} |
| 769 |
|
| 770 |
@begin{RunTime} |
| 771 |
@PDefn2{Term=[execution], Sec=(case_statement)} |
| 772 |
For the execution of a @nt{case_statement} the |
| 773 |
@nt{expression} is first evaluated. |
| 774 |
|
| 775 |
If the value of the @nt{expression} is covered by the |
| 776 |
@nt{discrete_@!choice_@!list} of some |
| 777 |
@nt{case_@!statement_@!alternative}, then the |
| 778 |
@nt{sequence_of_@!statements} of the @ntf{_alternative} is |
| 779 |
executed. |
| 780 |
|
| 781 |
@IndexCheck{Overflow_Check} |
| 782 |
@Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)} |
| 783 |
Otherwise (the value is not covered by any |
| 784 |
@nt{discrete_choice_list}, |
| 785 |
perhaps due to being outside the base range), |
| 786 |
Constraint_Error is raised. |
| 787 |
@begin{Ramification} |
| 788 |
|
| 789 |
In this case, the value is outside the base range of its type, |
| 790 |
or is an invalid representation. |
| 791 |
|
| 792 |
@end{Ramification} |
| 793 |
@end{RunTime} |
| 794 |
|
| 795 |
@begin{Notes} |
| 796 |
The execution of a @nt{case_statement} chooses one and only one |
| 797 |
alternative. |
| 798 |
Qualification of the expression of a @nt{case_statement} by a static |
| 799 |
subtype can often be used to limit the number of choices that need be |
| 800 |
given explicitly. |
| 801 |
@end{Notes} |
| 802 |
|
| 803 |
@begin{Examples} |
| 804 |
@Leading@keepnext@i{Examples of case statements:} |
| 805 |
@begin{Example} |
| 806 |
@tabclear()@tabset(P22) |
| 807 |
@key[case] Sensor @key[is] |
| 808 |
@key[when] Elevation@\=> Record_Elevation(Sensor_Value); |
| 809 |
@key[when] Azimuth@\=> Record_Azimuth (Sensor_Value); |
| 810 |
@key[when] Distance@\=> Record_Distance (Sensor_Value); |
| 811 |
@key[when] @key[others]@\=> @key[null]; |
| 812 |
@key[end] @key[case]; |
| 813 |
|
| 814 |
@tabclear()@tabset(P22) |
| 815 |
@key[case] Today @key[is] |
| 816 |
@key[when] Mon@\=> Compute_Initial_Balance; |
| 817 |
@key[when] Fri@\=> Compute_Closing_Balance; |
| 818 |
@key[when] Tue .. Thu@\=> Generate_Report(Today); |
| 819 |
@key[when] Sat .. Sun@\=> @key[null]; |
| 820 |
@key[end] @key[case]; |
| 821 |
|
| 822 |
@tabclear()@tabset(P16) |
| 823 |
@key[case] Bin_Number(Count) @key[is] |
| 824 |
@key[when] 1@\=> Update_Bin(1); |
| 825 |
@key[when] 2@\=> Update_Bin(2); |
| 826 |
@key[when] 3 | 4@\=> |
| 827 |
Empty_Bin(1); |
| 828 |
Empty_Bin(2); |
| 829 |
@key[when] @key[others]@\=> @key[raise] Error; |
| 830 |
@key[end] @key[case]; |
| 831 |
@end{Example} |
| 832 |
@end{Examples} |
| 833 |
|
| 834 |
@begin{Incompatible83} |
| 835 |
@ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020} |
| 836 |
@Chg{New=[@Defn{incompatibilities with Ada 83} |
| 837 |
In Ada 95, @nt{function_call}s and @nt{type_conversion}s are @nt{name}s, whereas |
| 838 |
in Ada 83, they were @nt{expression}s. Therefore, if the @nt{expression} of a |
| 839 |
@nt{case_statement} is a @nt{function_call} or @nt{type_conversion}, and the |
| 840 |
result subtype is static, it is illegal to specify a choice outside the bounds |
| 841 |
of the subtype. For this case in Ada 83 choices only are required to be in the |
| 842 |
base range of the type.],Old=[]} |
| 843 |
|
| 844 |
@ChgRef{Version=[1],Kind=[Added]}@ChgNote{Presentation AI-00020} |
| 845 |
@Chg{New=[In addition, the rule about which choices must be covered is |
| 846 |
unchanged in Ada 95. Therefore, for a @nt{case_statement} whose @nt{expression} |
| 847 |
is a @nt{function_call} or @nt{type_conversion}, Ada 83 required covering all |
| 848 |
choices in the base range, while Ada 95 only requires covering choices in the |
| 849 |
bounds of the subtype. If the @nt{case_statement} does not include an @key{others} |
| 850 |
@nt{discrete_choice}, then a legal Ada 83 @nt{case_statement} will be illegal |
| 851 |
in Ada 95 if the bounds of the subtype are different than the bounds of the |
| 852 |
base type.],Old=[]} |
| 853 |
@end{Incompatible83} |
| 854 |
|
| 855 |
@begin{Extend83} |
| 856 |
@Defn{extensions to Ada 83} |
| 857 |
In Ada 83, the @nt{expression} in a @nt{case_statement} is not allowed to |
| 858 |
be of a generic formal type. |
| 859 |
This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice} |
| 860 |
is required instead. |
| 861 |
|
| 862 |
In Ada 95, a function call is the name of an object; |
| 863 |
this was not true in Ada 83 (see @RefSec{Names}). |
| 864 |
This change makes the following @nt{case_statement} legal: |
| 865 |
@begin{Example} |
| 866 |
@key[subtype] S @key[is] Integer @key[range] 1..2; |
| 867 |
@key[function] F @key[return] S; |
| 868 |
@key[case] F @key[is] |
| 869 |
@key[when] 1 => ...; |
| 870 |
@key[when] 2 => ...; |
| 871 |
--@RI{ No @key{others} needed.} |
| 872 |
@key[end] @key[case]; |
| 873 |
@end{Example} |
| 874 |
|
| 875 |
Note that the result subtype given in a function |
| 876 |
@nt{renaming_declaration} is ignored; |
| 877 |
for a @nt{case_statement} whose expression calls a such a function, the |
| 878 |
full coverage rules are checked using the result subtype of the original |
| 879 |
function. |
| 880 |
Note that predefined operators such as "+" have an unconstrained result |
| 881 |
subtype (see @RefSecNum{Logical Operators and Short-circuit Control Forms}). |
| 882 |
Note that generic formal functions do not have static result subtypes. |
| 883 |
Note that the result subtype of an inherited subprogram need not |
| 884 |
correspond to any namable subtype; |
| 885 |
there is still a perfectly good result subtype, though. |
| 886 |
@end{Extend83} |
| 887 |
|
| 888 |
@begin{DiffWord83} |
| 889 |
Ada 83 forgot to say what happens for @lquotes@;legally@rquotes@; out-of-bounds values. |
| 890 |
|
| 891 |
We take advantage of rules and terms (e.g. @i(cover a value)) |
| 892 |
defined for @nt{discrete_choice}s and @nt{discrete_choice_list}s |
| 893 |
in @RefSec{Variant Parts and Discrete Choices}. |
| 894 |
|
| 895 |
In the @ResolutionName for the case expression, |
| 896 |
we no longer need RM83-5.4(3)'s @lquotes@;which must be determinable |
| 897 |
independently of the context in which the expression occurs, but |
| 898 |
using the fact that the expression must be of a discrete type,@rquotes@; |
| 899 |
because the @nt{expression} is now a complete context. |
| 900 |
See @RefSec{The Context of Overload Resolution}. |
| 901 |
|
| 902 |
Since @nt<type_conversion>s are now defined as @nt<name>s, |
| 903 |
their coverage rule is now covered under the general rule |
| 904 |
for @nt<name>s, rather than being separated out along with |
| 905 |
@nt<qualified_expression>s. |
| 906 |
@end{DiffWord83} |
| 907 |
|
| 908 |
@LabeledClause{Loop Statements} |
| 909 |
|
| 910 |
@begin{Intro} |
| 911 |
@Redundant[A @nt{loop_statement} includes a |
| 912 |
@nt{sequence_of_statements} that is to be executed repeatedly, |
| 913 |
zero or more times.] |
| 914 |
@end{Intro} |
| 915 |
|
| 916 |
@begin{Syntax} |
| 917 |
@Syn{lhs=<loop_statement>,rhs=" |
| 918 |
[@SynI{loop_}@Syn2{statement_identifier}:] |
| 919 |
[@Syn2{iteration_scheme}] @key{loop} |
| 920 |
@Syn2{sequence_of_statements} |
| 921 |
@key{end} @key{loop} [@SynI{loop_}@Syn2{identifier}];"} |
| 922 |
|
| 923 |
|
| 924 |
@Syn{lhs=<iteration_scheme>,rhs="@key{while} @Syn2{condition} |
| 925 |
| @key{for} @Syn2{loop_parameter_specification}"} |
| 926 |
|
| 927 |
@Syn{lhs=<loop_parameter_specification>,rhs=" |
| 928 |
@Syn2{defining_identifier} @key{in} [@key{reverse}] @Syn2{discrete_subtype_definition}"} |
| 929 |
|
| 930 |
@begin(SyntaxText) |
| 931 |
If a @nt{loop_statement} has a @SynI{loop_}@nt{statement_identifier}, |
| 932 |
then the @nt<identifier> shall be repeated after the @key{end loop}; |
| 933 |
otherwise, there shall not be an @nt<identifier> after the @key{end loop}. |
| 934 |
@end(SyntaxText) |
| 935 |
@end{Syntax} |
| 936 |
|
| 937 |
@begin{StaticSem} |
| 938 |
@Defn{loop parameter} |
| 939 |
A @nt{loop_parameter_specification} declares a @i{loop parameter}, |
| 940 |
which is an object whose |
| 941 |
subtype is that defined by the @nt{discrete_subtype_definition}. |
| 942 |
@IndexSeeAlso{Term=[parameter],See=[loop parameter]} |
| 943 |
@end{StaticSem} |
| 944 |
|
| 945 |
@begin{RunTime} |
| 946 |
@PDefn2{Term=[execution], Sec=(loop_statement)} |
| 947 |
For the execution of a @nt{loop_statement}, |
| 948 |
the @nt{sequence_of_statements} is executed repeatedly, |
| 949 |
zero or more times, |
| 950 |
until the @nt{loop_statement} is complete. |
| 951 |
The @nt{loop_statement} is complete when a transfer |
| 952 |
of control occurs that transfers control out of the loop, or, in the |
| 953 |
case of an @nt{iteration_scheme}, as specified below. |
| 954 |
|
| 955 |
@PDefn2{Term=[execution], |
| 956 |
Sec=(loop_statement with a while iteration_scheme)} |
| 957 |
For the execution of a @nt{loop_statement} with a @key{while} |
| 958 |
@nt{iteration_scheme}, the condition is evaluated before each |
| 959 |
execution of the @nt{sequence_of_@!statements}; if the value of the |
| 960 |
@nt{condition} is True, the @nt{sequence_of_@!statements} is executed; |
| 961 |
if False, the execution of the @nt{loop_@!statement} is complete. |
| 962 |
|
| 963 |
@PDefn2{Term=[execution], |
| 964 |
Sec=(loop_statement with a for iteration_scheme)} |
| 965 |
@PDefn2{Term=[elaboration], Sec=(loop_parameter_specification)} |
| 966 |
For the execution of a @nt{loop_statement} with a @key{for} |
| 967 |
@nt{iteration_scheme}, |
| 968 |
the @nt{loop_@!parameter_@!specification} is first elaborated. This |
| 969 |
elaboration creates the loop parameter and elaborates the |
| 970 |
@nt{discrete_@!subtype_@!definition}. |
| 971 |
If the @nt{discrete_@!subtype_@!definition} defines a subtype |
| 972 |
with a null range, |
| 973 |
the execution of the |
| 974 |
@nt{loop_statement} is complete. Otherwise, the |
| 975 |
@nt{sequence_of_@!statements} is executed once for each value of the |
| 976 |
discrete subtype defined by the |
| 977 |
@nt{discrete_@!subtype_@!definition} (or until the loop is left as a |
| 978 |
consequence of a transfer of control). |
| 979 |
@Defn2{Term=[assignment operation], Sec=(during execution of a @key{for} loop)} |
| 980 |
Prior to each such iteration, |
| 981 |
the corresponding value of the discrete subtype is assigned to the |
| 982 |
loop parameter. These values are assigned in increasing order unless |
| 983 |
the reserved word @key{reverse} is present, in which case the values |
| 984 |
are assigned in decreasing order. |
| 985 |
@begin{Ramification} |
| 986 |
The order of creating the loop parameter and evaluating the |
| 987 |
@nt{discrete_subtype_definition} doesn't matter, |
| 988 |
since the creation of the loop parameter has no side effects (other |
| 989 |
than possibly raising Storage_Error, but anything can do that). |
| 990 |
@end{Ramification} |
| 991 |
@end{RunTime} |
| 992 |
|
| 993 |
@begin{Notes} |
| 994 |
A loop parameter is a constant; |
| 995 |
it cannot be updated within the |
| 996 |
@nt{sequence_of_statements} of the loop |
| 997 |
(see @RefSecNum{Objects and Named Numbers}). |
| 998 |
|
| 999 |
|
| 1000 |
An @nt{object_declaration} should not be given for a loop parameter, |
| 1001 |
since the loop parameter is automatically declared by |
| 1002 |
the @nt{loop_parameter_specification}. |
| 1003 |
The scope of a loop parameter extends from the |
| 1004 |
@nt{loop_parameter_specification} to the end of the |
| 1005 |
@nt{loop_statement}, and the visibility |
| 1006 |
rules are such that a loop parameter is only visible within the |
| 1007 |
@nt{sequence_of_statements} of the loop. |
| 1008 |
@begin{ImplNote} |
| 1009 |
An implementation could give a warning if a variable is hidden by a |
| 1010 |
@nt{loop_parameter_specification}. |
| 1011 |
@end{ImplNote} |
| 1012 |
|
| 1013 |
The @nt<discrete_subtype_definition> of a for loop is elaborated |
| 1014 |
just once. Use of the |
| 1015 |
reserved word @key[reverse] does not alter the discrete subtype defined, |
| 1016 |
so that the following @nt{iteration_scheme}s are not equivalent; the |
| 1017 |
first has a null range. |
| 1018 |
@begin{Example} |
| 1019 |
@key[for] J @key[in] @key[reverse] 1 .. 0 |
| 1020 |
@key[for] J @key[in] 0 .. 1 |
| 1021 |
@end{Example} |
| 1022 |
@begin{Ramification} |
| 1023 |
If a @nt{loop_parameter_specification} has a static discrete range, |
| 1024 |
the subtype of the loop parameter is static. |
| 1025 |
@end{Ramification} |
| 1026 |
@end{Notes} |
| 1027 |
|
| 1028 |
@begin{Examples} |
| 1029 |
@Leading@keepnext@i{Example of a loop statement without an iteration scheme:} |
| 1030 |
@begin{Example} |
| 1031 |
@key[loop] |
| 1032 |
Get(Current_Character); |
| 1033 |
@key[exit] @key[when] Current_Character = '*'; |
| 1034 |
@key[end] @key[loop]; |
| 1035 |
@end{Example} |
| 1036 |
|
| 1037 |
@begin{Wide} |
| 1038 |
@leading@keepnext@i{Example of a loop statement with a @key[while] iteration scheme:} |
| 1039 |
@end{Wide} |
| 1040 |
@begin{Example} |
| 1041 |
@key[while] Bid(N).Price < Cut_Off.Price @key[loop] |
| 1042 |
Record_Bid(Bid(N).Price); |
| 1043 |
N := N + 1; |
| 1044 |
@key[end] @key[loop]; |
| 1045 |
@end{Example} |
| 1046 |
|
| 1047 |
@begin{Wide} |
| 1048 |
@leading@keepnext@i{Example of a loop statement with a @key[for] iteration scheme:} |
| 1049 |
@end{Wide} |
| 1050 |
@begin{Example} |
| 1051 |
@key[for] J @key[in] Buffer'Range @key[loop] --@RI{ works even with a null range} |
| 1052 |
@key[if] Buffer(J) /= Space @key[then] |
| 1053 |
Put(Buffer(J)); |
| 1054 |
@key[end] @key[if]; |
| 1055 |
@key[end] @key[loop]; |
| 1056 |
@end{Example} |
| 1057 |
|
| 1058 |
@begin{Wide} |
| 1059 |
@leading@keepnext@i{Example of a loop statement with a name:} |
| 1060 |
@end{Wide} |
| 1061 |
@begin{Example} |
| 1062 |
Summation: |
| 1063 |
@key[while] Next /= Head @key[loop] --@RI{ see @RefSecNum{Incomplete Type Declarations}} |
| 1064 |
Sum := Sum + Next.Value; |
| 1065 |
Next := Next.Succ; |
| 1066 |
@key[end] @key[loop] Summation; |
| 1067 |
@end{Example} |
| 1068 |
@end{Examples} |
| 1069 |
|
| 1070 |
@begin{DiffWord83} |
| 1071 |
The constant-ness of loop parameters is specified in |
| 1072 |
@RefSec{Objects and Named Numbers}. |
| 1073 |
@end{DiffWord83} |
| 1074 |
|
| 1075 |
@RMNewPage |
| 1076 |
@LabeledClause{Block Statements} |
| 1077 |
|
| 1078 |
@begin{Intro} |
| 1079 |
@Redundant[A @nt{block_statement} encloses a |
| 1080 |
@nt{handled_sequence_of_statements} |
| 1081 |
optionally preceded by a @nt{declarative_part}.] |
| 1082 |
@end{Intro} |
| 1083 |
|
| 1084 |
@begin{Syntax} |
| 1085 |
@Syn{lhs=<block_statement>,rhs=" |
| 1086 |
[@SynI{block_}@Syn2{statement_identifier}:] |
| 1087 |
[@key{declare} |
| 1088 |
@Syn2{declarative_part}] |
| 1089 |
@key{begin} |
| 1090 |
@Syn2{handled_sequence_of_statements} |
| 1091 |
@key{end} [@SynI{block_}@Syn2{identifier}];"} |
| 1092 |
|
| 1093 |
@begin(SyntaxText) |
| 1094 |
If a @nt{block_statement} has a @SynI{block_}@nt{statement_identifier}, |
| 1095 |
then the @nt<identifier> shall be repeated after the @key{end}; |
| 1096 |
otherwise, there shall not be an @nt<identifier> after the @key{end}. |
| 1097 |
@end(SyntaxText) |
| 1098 |
@end{Syntax} |
| 1099 |
|
| 1100 |
@begin{StaticSem} |
| 1101 |
A @nt{block_statement} that has no |
| 1102 |
explicit @nt{declarative_part} has an implicit empty |
| 1103 |
@nt{declarative_part}. |
| 1104 |
@begin{Ramification} |
| 1105 |
Thus, other rules can always |
| 1106 |
refer to the @nt{declarative_part} of a @nt<block_statement>. |
| 1107 |
@end{Ramification} |
| 1108 |
@end{StaticSem} |
| 1109 |
|
| 1110 |
@begin{RunTime} |
| 1111 |
@PDefn2{Term=[execution], Sec=(block_statement)} |
| 1112 |
The execution of a @nt{block_statement} consists of the elaboration |
| 1113 |
of its @nt{declarative_part} followed by the execution of |
| 1114 |
its @nt{handled_sequence_of_statements}. |
| 1115 |
@end{RunTime} |
| 1116 |
|
| 1117 |
@begin{Examples} |
| 1118 |
@Leading@keepnext@i{Example of a block statement with a local variable:} |
| 1119 |
@begin{Example} |
| 1120 |
Swap: |
| 1121 |
@key[declare] |
| 1122 |
Temp : Integer; |
| 1123 |
@key[begin] |
| 1124 |
Temp := V; V := U; U := Temp; |
| 1125 |
@key[end] Swap; |
| 1126 |
@end{Example} |
| 1127 |
@begin{Ramification} |
| 1128 |
If task objects are declared within a @nt{block_statement} whose execution |
| 1129 |
is completed, the @nt{block_statement} is not left until all its dependent |
| 1130 |
tasks are terminated |
| 1131 |
(see @RefSecNum{User-Defined Assignment and Finalization}). |
| 1132 |
This rule applies to completion caused by a transfer of control. |
| 1133 |
|
| 1134 |
Within a @nt{block_statement}, the block name can be used in expanded |
| 1135 |
names denoting local entities such as Swap.Temp in the above example |
| 1136 |
(see @RefSecNum{Selected Components}). |
| 1137 |
@end{Ramification} |
| 1138 |
@end{Examples} |
| 1139 |
|
| 1140 |
@begin{DiffWord83} |
| 1141 |
The syntax rule for @nt{block_statement} now uses the syntactic category |
| 1142 |
@nt{handled_sequence_of_statements}. |
| 1143 |
@end{DiffWord83} |
| 1144 |
|
| 1145 |
@LabeledClause{Exit Statements} |
| 1146 |
|
| 1147 |
@begin{Intro} |
| 1148 |
@Redundant[An @nt{exit_statement} is used to complete the execution |
| 1149 |
of an enclosing @nt{loop_statement}; the |
| 1150 |
completion is conditional if the @nt{exit_statement} includes a |
| 1151 |
@nt{condition}.] |
| 1152 |
@end{Intro} |
| 1153 |
|
| 1154 |
@begin{Syntax} |
| 1155 |
@Syn{lhs=<exit_statement>,rhs=" |
| 1156 |
@key{exit} [@SynI{loop_}@Syn2{name}] [@key{when} @Syn2{condition}];"} |
| 1157 |
@end{Syntax} |
| 1158 |
|
| 1159 |
@begin{Resolution} |
| 1160 |
The @i(loop_)@nt{name}, if any, in an @nt{exit_statement} shall resolve to |
| 1161 |
denote a @nt{loop_statement}. |
| 1162 |
@end{Resolution} |
| 1163 |
|
| 1164 |
@begin{Legality} |
| 1165 |
@Defn2{Term=[apply], Sec=(to a @nt{loop_statement} by an @nt{exit_statement})} |
| 1166 |
Each @nt{exit_@!statement} @i{applies to} a |
| 1167 |
@nt{loop_@!statement}; this is the @nt{loop_@!statement} being exited. |
| 1168 |
An @nt{exit_@!statement} with a @nt{name} is only allowed within the |
| 1169 |
@nt{loop_@!statement} denoted by the @nt{name}, |
| 1170 |
and applies to that @nt{loop_@!statement}. |
| 1171 |
An @nt{exit_@!statement} without a @nt{name} is only allowed within a |
| 1172 |
@nt{loop_@!statement}, and applies to the innermost enclosing one. |
| 1173 |
An @nt{exit_@!statement} that applies to a given @nt{loop_@!statement} |
| 1174 |
shall not appear within a body or @nt{accept_@!statement}, if |
| 1175 |
this construct is itself enclosed by the given @nt{loop_statement}. |
| 1176 |
@end{Legality} |
| 1177 |
|
| 1178 |
@begin{RunTime} |
| 1179 |
@PDefn2{Term=[execution], Sec=(exit_statement)} |
| 1180 |
For the execution of an @nt{exit_statement}, the @nt{condition}, if |
| 1181 |
present, is first evaluated. |
| 1182 |
If the value of the @nt{condition} is True, or if there is no @nt{condition}, |
| 1183 |
a transfer of control is done to complete the @nt{loop_@!statement}. |
| 1184 |
If the value of the @nt{condition} is False, no transfer of control takes |
| 1185 |
place. |
| 1186 |
@end{RunTime} |
| 1187 |
|
| 1188 |
@begin{Notes} |
| 1189 |
Several nested loops can be exited by an @nt{exit_statement} that names |
| 1190 |
the outer loop. |
| 1191 |
@end{Notes} |
| 1192 |
|
| 1193 |
@begin{Examples} |
| 1194 |
@i{Examples of loops with exit statements:} |
| 1195 |
@begin{Example} |
| 1196 |
@key[for] N @key[in] 1 .. Max_Num_Items @key[loop] |
| 1197 |
Get_New_Item(New_Item); |
| 1198 |
Merge_Item(New_Item, Storage_File); |
| 1199 |
@key[exit] @key[when] New_Item = Terminal_Item; |
| 1200 |
@key[end] @key[loop]; |
| 1201 |
|
| 1202 |
Main_Cycle: |
| 1203 |
@key[loop] |
| 1204 |
--@RI{ initial statements} |
| 1205 |
@key[exit] Main_Cycle @key[when] Found; |
| 1206 |
--@RI{ final statements} |
| 1207 |
@key[end] @key[loop] Main_Cycle; |
| 1208 |
@end{Example} |
| 1209 |
@end{Examples} |
| 1210 |
|
| 1211 |
@LabeledClause{Goto Statements} |
| 1212 |
|
| 1213 |
@begin{Intro} |
| 1214 |
@Redundant[A @nt{goto_statement} specifies an explicit transfer of control |
| 1215 |
from this @nt{statement} to a target |
| 1216 |
statement with a given label.] |
| 1217 |
@end{Intro} |
| 1218 |
|
| 1219 |
@begin{Syntax} |
| 1220 |
@Syn{lhs=<goto_statement>,rhs="@key{goto} @SynI{label_}@Syn2{name};"} |
| 1221 |
@end{Syntax} |
| 1222 |
|
| 1223 |
@begin{Resolution} |
| 1224 |
@Defn2{Term=[target statement], Sec=(of a @nt{goto_statement})} |
| 1225 |
The @i(label_)@nt{name} shall resolve to denote a @nt<label>; |
| 1226 |
the @nt{statement} with that @nt{label} is the @i(target statement). |
| 1227 |
@end{Resolution} |
| 1228 |
|
| 1229 |
@begin{Legality} |
| 1230 |
The innermost @nt{sequence_of_statements} that encloses the target |
| 1231 |
statement shall also enclose the @nt{goto_statement}. |
| 1232 |
Furthermore, if a @nt{goto_statement} is enclosed by an |
| 1233 |
@nt{accept_statement} or a body, then the target |
| 1234 |
statement shall not be outside this enclosing construct. |
| 1235 |
@begin{Ramification} |
| 1236 |
The @nt{goto_statement} can be a @nt{statement} of an inner |
| 1237 |
@ntf{sequence_}. |
| 1238 |
|
| 1239 |
It follows from the second rule that if the target @nt{statement} |
| 1240 |
is enclosed by such a construct, then the @nt{goto_statement} |
| 1241 |
cannot be outside. |
| 1242 |
@end{Ramification} |
| 1243 |
@end{Legality} |
| 1244 |
|
| 1245 |
@begin{RunTime} |
| 1246 |
@PDefn2{Term=[execution], Sec=(goto_statement)} |
| 1247 |
The execution of a @nt{goto_statement} transfers control to the |
| 1248 |
target statement, completing the execution |
| 1249 |
of any @nt<compound_statement> that encloses the @nt<goto_statement> |
| 1250 |
but does not enclose the target. |
| 1251 |
@end{RunTime} |
| 1252 |
|
| 1253 |
@begin{Notes} |
| 1254 |
The above rules allow transfer of control to a @nt{statement} of an |
| 1255 |
enclosing @nt{sequence_of_statements} but not the reverse. Similarly, |
| 1256 |
they prohibit transfers of control such as between alternatives of a |
| 1257 |
@nt{case_statement}, @nt{if_statement}, or @nt{select_statement}; |
| 1258 |
between @nt{exception_handler}s; or from an @nt{exception_handler} of |
| 1259 |
a @nt{handled_sequence_of_statements} |
| 1260 |
back to its @nt{sequence_of_statements}. |
| 1261 |
@end{Notes} |
| 1262 |
|
| 1263 |
@begin{Examples} |
| 1264 |
@Leading@keepnext@i{Example of a loop containing a goto statement:} |
| 1265 |
@begin{Example} |
| 1266 |
<<Sort>> |
| 1267 |
@key[for] I @key[in] 1 .. N-1 @key[loop] |
| 1268 |
@key[if] A(I) > A(I+1) @key[then] |
| 1269 |
Exchange(A(I), A(I+1)); |
| 1270 |
@key[goto] Sort; |
| 1271 |
@key[end] @key[if]; |
| 1272 |
@key[end] @key[loop]; |
| 1273 |
@end{Example} |
| 1274 |
@end{Examples} |