1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
Section 5: Statements
1 A statement defines an action to be performed upon its execution.
2 This section describes the general rules applicable to all statements.
Some statements are discussed in later sections: Procedure_call_statements and
return_statements are described in 6, ``Subprograms''. Entry_call_statements,
requeue_statements, delay_statements, accept_statements, select_statements,
and abort_statements are described in 9, ``Tasks and Synchronization''. Raise_-
statements are described in 11, ``Exceptions'', and code_statements in 13. The
remaining forms of statements are presented in this section.
5.1 Simple and Compound Statements - Sequences of Statements
1 A statement is either simple or compound. A simple_statement encloses no
other statement. A compound_statement can enclose simple_statements and other
compound_statements.
Syntax
2 sequence_of_statements ::= statement {statement}
3 statement ::=
{label} simple_statement | {label} compound_statement
4 simple_statement ::= null_statement
| assignment_statement | exit_statement
| goto_statement | procedure_call_statement
| return_statement | entry_call_statement
| requeue_statement | delay_statement
| abort_statement | raise_statement
| code_statement
5 compound_statement ::=
if_statement | case_statement
| loop_statement | block_statement
| accept_statement | select_statement
6 null_statement ::= null;
7 label ::= <<label_statement_identifier>>
8 statement_identifier ::= direct_name
9 The direct_name of a statement_identifier shall be an identifier (not an
operator_symbol).
Name Resolution Rules
10 The direct_name of a statement_identifier shall resolve to denote its
corresponding implicit declaration (see below).
Legality Rules
11 Distinct identifiers shall be used for all statement_identifiers that
appear in the same body, including inner block_statements but excluding inner
program units.
Static Semantics
12 For each statement_identifier, there is an implicit declaration (with
the specified identifier) at the end of the declarative_part of the innermost
block_statement or body that encloses the statement_identifier. The implicit
declarations occur in the same order as the statement_identifiers occur in the
source text. If a usage name denotes such an implicit declaration, the entity
it denotes is the label, loop_statement, or block_statement with the given
statement_identifier.
Dynamic Semantics
13 The execution of a null_statement has no effect.
14 A transfer of control is the run-time action of an exit_statement,
return_statement, goto_statement, or requeue_statement, selection of a
terminate_alternative, raising of an exception, or an abort, which causes the
next action performed to be one other than what would normally be expected
from the other rules of the language. As explained in 7.6.1, a transfer of
control can cause the execution of constructs to be completed and then left,
which may trigger finalization.
15 The execution of a sequence_of_statements consists of the execution of
the individual statements in succession until the sequence_ is completed.
NOTES
16 1 A statement_identifier that appears immediately within the
declarative region of a named loop_statement or an accept_statement is
nevertheless implicitly declared immediately within the declarative
region of the innermost enclosing body or block_statement; in other
words, the expanded name for a named statement is not affected by
whether the statement occurs inside or outside a named loop or an
accept_statement - only nesting within block_statements is relevant to
the form of its expanded name.
Examples
17 Examples of labeled statements:
18 <<Here>> <<Ici>> <<Aqui>> <<Hier>> null;
19 <<After>> X := 1;
5.2 Assignment Statements
1 An assignment_statement replaces the current value of a variable with
the result of evaluating an expression.
Syntax
2 assignment_statement ::=
variable_name := expression;
3 The execution of an assignment_statement includes the evaluation of the
expression and the assignment of the value of the expression into the target.
An assignment operation (as opposed to an assignment_statement) is performed
in other contexts as well, including object initialization and by-copy
parameter passing. The target of an assignment operation is the view of the
object to which a value is being assigned; the target of an assignment_-
statement is the variable denoted by the variable_name.
Name Resolution Rules
4 The variable_name of an assignment_statement is expected to be of any
nonlimited type. The expected type for the expression is the type of the
target.
Legality Rules
5 The target denoted by the variable_name shall be a variable.
6 If the target is of a tagged class-wide type T'Class, then the
expression shall either be dynamically tagged, or of type T and
tag-indeterminate (see 3.9.2).
Dynamic Semantics
7 For the execution of an assignment_statement, the variable_name and the
expression are first evaluated in an arbitrary order.
8 When the type of the target is class-wide:
9 If the expression is tag-indeterminate (see 3.9.2), then the controlling
tag value for the expression is the tag of the target;
10 Otherwise (the expression is dynamically tagged), a check is made that
the tag of the value of the expression is the same as that of the
target; if this check fails, Constraint_Error is raised.
11 The value of the expression is converted to the subtype of the target.
The conversion might raise an exception (see 4.6).
12 In cases involving controlled types, the target is finalized, and an
anonymous object might be used as an intermediate in the assignment, as
described in 7.6.1, ``Completion and Finalization''. In any case, the
converted value of the expression is then assigned to the target, which
consists of the following two steps:
13 The value of the target becomes the converted value.
14 If any part of the target is controlled, its value is adjusted as
explained in clause 7.6.
NOTES
15 2 The tag of an object never changes; in particular, an
assignment_statement does not change the tag of the target.
16 3 The values of the discriminants of an object designated by an access
value cannot be changed (not even by assigning a complete value to the
object itself) since such objects are always constrained; however,
subcomponents of such objects may be unconstrained.
Examples
17 Examples of assignment statements:
18 Value := Max_Value - 1;
Shade := Blue;
19 Next_Frame(F)(M, N) := 2.5; -- see 4.1.1
U := Dot_Product(V, W); -- see 6.3
20 Writer := (Status => Open, Unit => Printer, Line_Count => 60); -- see 3.8.1
Next_Car.all := (72074, null); -- see 3.10.1
21 Examples involving scalar subtype conversions:
22 I, J : Integer range 1 .. 10 := 5;
K : Integer range 1 .. 20 := 15;
...
23 I := J; -- identical ranges
K := J; -- compatible ranges
J := K; -- will raise Constraint_Error if K > 10
24 Examples involving array subtype conversions:
25 A : String(1 .. 31);
B : String(3 .. 33);
...
26 A := B; -- same number of components
27 A(1 .. 9) := "tar sauce";
A(4 .. 12) := A(1 .. 9); -- A(1 .. 12) = "tartar sauce"
NOTES
28 4 Notes on the examples: Assignment_statements are allowed even in the
case of overlapping slices of the same array, because the variable_name
and expression are both evaluated before copying the value into the
variable. In the above example, an implementation yielding A(1 .. 12) =
"tartartartar" would be incorrect.
5.3 If Statements
1 An if_statement selects for execution at most one of the enclosed
sequences_of_statements, depending on the (truth) value of one or more
corresponding conditions.
Syntax
2 if_statement ::=
if condition then
sequence_of_statements
{elsif condition then
sequence_of_statements}
[else
sequence_of_statements]
end if;
3 condition ::= boolean_expression
Name Resolution Rules
4 A condition is expected to be of any boolean type.
Dynamic Semantics
5 For the execution of an if_statement, the condition specified after if,
and any conditions specified after elsif, are evaluated in succession
(treating a final else as elsif True then), until one evaluates to True or all
conditions are evaluated and yield False. If a condition evaluates to True,
then the corresponding sequence_of_statements is executed; otherwise none of
them is executed.
Examples
6 Examples of if statements:
7 if Month = December and Day = 31 then
Month := January;
Day := 1;
Year := Year + 1;
end if;
8 if Line_Too_Short then
raise Layout_Error;
elsif Line_Full then
New_Line;
Put(Item);
else
Put(Item);
end if;
9 if My_Car.Owner.Vehicle /= My_Car then -- see 3.10.1
Report ("Incorrect data");
end if;
5.4 Case Statements
1 A case_statement selects for execution one of a number of alternative
sequences_of_statements; the chosen alternative is defined by the value of an
expression.
Syntax
2 case_statement ::=
case expression is
case_statement_alternative
{case_statement_alternative}
end case;
3 case_statement_alternative ::=
when discrete_choice_list =>
sequence_of_statements
Name Resolution Rules
4 The expression is expected to be of any discrete type. The expected type
for each discrete_choice is the type of the expression.
Legality Rules
5 The expressions and discrete_ranges given as discrete_choices of a
case_statement shall be static. A discrete_choice others, if present, shall
appear alone and in the last discrete_choice_list.
6 The possible values of the expression shall be covered as follows:
7 If the expression is a name (including a type_conversion or a
function_call) having a static and constrained nominal subtype, or is a
qualified_expression whose subtype_mark denotes a static and constrained
scalar subtype, then each non-others discrete_choice shall cover only
values in that subtype, and each value of that subtype shall be covered
by some discrete_choice (either explicitly or by others).
8 If the type of the expression is root_integer, universal_integer, or a
descendant of a formal scalar type, then the case_statement shall have
an others discrete_choice.
9 Otherwise, each value of the base range of the type of the expression
shall be covered (either explicitly or by others).
10 Two distinct discrete_choices of a case_statement shall not cover the
same value.
Dynamic Semantics
11 For the execution of a case_statement the expression is first evaluated.
12 If the value of the expression is covered by the discrete_choice_-
list of some case_statement_alternative, then the sequence_of_statements of
the _alternative is executed.
13 Otherwise (the value is not covered by any discrete_choice_list, perhaps
due to being outside the base range), Constraint_Error is raised.
NOTES
14 5 The execution of a case_statement chooses one and only one
alternative. Qualification of the expression of a case_statement by a
static subtype can often be used to limit the number of choices that
need be given explicitly.
Examples
15 Examples of case statements:
16 case Sensor is
when Elevation => Record_Elevation(Sensor_Value);
when Azimuth => Record_Azimuth (Sensor_Value);
when Distance => Record_Distance (Sensor_Value);
when others => null;
end case;
17 case Today is
when Mon => Compute_Initial_Balance;
when Fri => Compute_Closing_Balance;
when Tue .. Thu => Generate_Report(Today);
when Sat .. Sun => null;
end case;
18 case Bin_Number(Count) is
when 1 => Update_Bin(1);
when 2 => Update_Bin(2);
when 3 | 4 =>
Empty_Bin(1);
Empty_Bin(2);
when others => raise Error;
end case;
5.5 Loop Statements
1 A loop_statement includes a sequence_of_statements that is to be
executed repeatedly, zero or more times.
Syntax
2 loop_statement ::=
[loop_statement_identifier:]
[iteration_scheme] loop
sequence_of_statements
end loop [loop_identifier];
3 iteration_scheme ::= while condition
| for loop_parameter_specification
4 loop_parameter_specification ::=
defining_identifier in [reverse] discrete_subtype_definition
5 If a loop_statement has a loop_statement_identifier, then the identifier
shall be repeated after the end loop; otherwise, there shall not be an
identifier after the end loop.
Static Semantics
6 A loop_parameter_specification declares a loop parameter, which is an
object whose subtype is that defined by the discrete_subtype_definition.
Dynamic Semantics
7 For the execution of a loop_statement, the sequence_of_statements is
executed repeatedly, zero or more times, until the loop_statement is complete.
The loop_statement is complete when a transfer of control occurs that
transfers control out of the loop, or, in the case of an iteration_scheme, as
specified below.
8 For the execution of a loop_statement with a while iteration_scheme, the
condition is evaluated before each execution of the sequence_of_statements; if
the value of the condition is True, the sequence_of_statements is executed; if
False, the execution of the loop_statement is complete.
9 For the execution of a loop_statement with a for iteration_scheme, the
loop_parameter_specification is first elaborated. This elaboration creates the
loop parameter and elaborates the discrete_subtype_definition. If the discrete_-
subtype_definition defines a subtype with a null range, the execution of the
loop_statement is complete. Otherwise, the sequence_of_statements is executed
once for each value of the discrete subtype defined by the discrete_subtype_-
definition (or until the loop is left as a consequence of a transfer of
control). Prior to each such iteration, the corresponding value of the
discrete subtype is assigned to the loop parameter. These values are assigned
in increasing order unless the reserved word reverse is present, in which case
the values are assigned in decreasing order.
NOTES
10 6 A loop parameter is a constant; it cannot be updated within the
sequence_of_statements of the loop (see 3.3).
11 7 An object_declaration should not be given for a loop parameter, since
the loop parameter is automatically declared by the
loop_parameter_specification. The scope of a loop parameter extends from
the loop_parameter_specification to the end of the loop_statement, and
the visibility rules are such that a loop parameter is only visible
within the sequence_of_statements of the loop.
12 8 The discrete_subtype_definition of a for loop is elaborated just
once. Use of the reserved word reverse does not alter the discrete
subtype defined, so that the following iteration_schemes are not
equivalent; the first has a null range.
13 for J in reverse 1 .. 0
for J in 0 .. 1
Examples
14 Example of a loop statement without an iteration scheme:
15 loop
Get(Current_Character);
exit when Current_Character = '*';
end loop;
16 Example of a loop statement with a while iteration scheme:
17 while Bid(N).Price < Cut_Off.Price loop
Record_Bid(Bid(N).Price);
N := N + 1;
end loop;
18 Example of a loop statement with a for iteration scheme:
19 for J in Buffer'Range loop -- works even with a null range
if Buffer(J) /= Space then
Put(Buffer(J));
end if;
end loop;
20 Example of a loop statement with a name:
21 Summation:
while Next /= Head loop -- see 3.10.1
Sum := Sum + Next.Value;
Next := Next.Succ;
end loop Summation;
5.6 Block Statements
1 A block_statement encloses a handled_sequence_of_statements optionally
preceded by a declarative_part.
Syntax
2 block_statement ::=
[block_statement_identifier:]
[declare
declarative_part]
begin
handled_sequence_of_statements
end [block_identifier];
3 If a block_statement has a block_statement_identifier, then the
identifier shall be repeated after the end; otherwise, there shall not
be an identifier after the end.
Static Semantics
4 A block_statement that has no explicit declarative_part has an implicit
empty declarative_part.
Dynamic Semantics
5 The execution of a block_statement consists of the elaboration of its
declarative_part followed by the execution of its
handled_sequence_of_statements.
Examples
6 Example of a block statement with a local variable:
7 Swap:
declare
Temp : Integer;
begin
Temp := V; V := U; U := Temp;
end Swap;
5.7 Exit Statements
1 An exit_statement is used to complete the execution of an enclosing
loop_statement; the completion is conditional if the exit_statement includes a
condition.
Syntax
2 exit_statement ::=
exit [loop_name] [when condition];
Name Resolution Rules
3 The loop_name, if any, in an exit_statement shall resolve to denote a
loop_statement.
Legality Rules
4 Each exit_statement applies to a loop_statement; this is the loop_-
statement being exited. An exit_statement with a name is only allowed within
the loop_statement denoted by the name, and applies to that loop_statement. An
exit_statement without a name is only allowed within a loop_statement, and
applies to the innermost enclosing one. An exit_statement that applies to a
given loop_statement shall not appear within a body or accept_statement, if
this construct is itself enclosed by the given loop_statement.
Dynamic Semantics
5 For the execution of an exit_statement, the condition, if present, is
first evaluated. If the value of the condition is True, or if there is no
condition, a transfer of control is done to complete the loop_statement. If
the value of the condition is False, no transfer of control takes place.
NOTES
6 9 Several nested loops can be exited by an exit_statement that names
the outer loop.
Examples
7 Examples of loops with exit statements:
8 for N in 1 .. Max_Num_Items loop
Get_New_Item(New_Item);
Merge_Item(New_Item, Storage_File);
exit when New_Item = Terminal_Item;
end loop;
9 Main_Cycle:
loop
-- initial statements
exit Main_Cycle when Found;
-- final statements
end loop Main_Cycle;
5.8 Goto Statements
1 A goto_statement specifies an explicit transfer of control from this
statement to a target statement with a given label.
Syntax
2 goto_statement ::= goto label_name;
Name Resolution Rules
3 The label_name shall resolve to denote a label; the statement with that
label is the target statement.
Legality Rules
4 The innermost sequence_of_statements that encloses the target statement
shall also enclose the goto_statement. Furthermore, if a goto_statement is
enclosed by an accept_statement or a body, then the target statement shall not
be outside this enclosing construct.
Dynamic Semantics
5 The execution of a goto_statement transfers control to the target
statement, completing the execution of any compound_statement that encloses
the goto_statement but does not enclose the target.
NOTES
6 10 The above rules allow transfer of control to a statement of an
enclosing sequence_of_statements but not the reverse. Similarly, they
prohibit transfers of control such as between alternatives of a
case_statement, if_statement, or select_statement; between
exception_handlers; or from an exception_handler of a
handled_sequence_of_statements back to its sequence_of_statements.
Examples
7 Example of a loop containing a goto statement:
8 <<Sort>>
for I in 1 .. N-1 loop
if A(I) > A(I+1) then
Exchange(A(I), A(I+1));
goto Sort;
end if;
end loop;
|