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.]

2

case_statement
::=

**case** expression **is**

case_statement_alternative

{case_statement_alternative}

**end** **case**;

case_statement_alternative

{case_statement_alternative}

3

4

{*expected type (case expression)*
[partial]} The expression
is expected to be of any discrete type. {*expected type
(case_statement_alternative discrete_choice)* [partial]} The
expected type for each discrete_choice
is the type of the expression.

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**)].

7.a

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.

8.a

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.

10.a

10.b

- If the expression reads an object with an invalid representation
(e.g. an uninitialized object), then the value can be outside the covered
range. This can happen for static constrained subtypes, as well as nonstatic
or unconstrained subtypes. It cannot, however, happen if the case_statement
has the discrete_choice
**others**, because**others**covers all values, even those outside the subtype.

10.c

- If the compiler chooses to represent the value of an expression
of an unconstrained subtype in a way that includes values outside the
bounds of the subtype, then those values can be outside the covered range.
For example, if X: Integer := Integer'Last;, and the case expression
is X+1, then the implementation might choose to produce the correct value,
which is outside the bounds of Integer. (It might raise Constraint_Error
instead.) This case can only happen for non-generic subtypes that are
either unconstrained or non-static (or both). It can only happen if there
is no
**others**discrete_choice.

10.d

In the uninitialized variable
case, the value might be anything; hence, any alternative can be chosen,
or Constraint_Error can be raised. (We intend to prevent, however, jumping
to random memory locations and the like.) In the out-of-range case, the
behavior is more sensible: if there is an **others**, then the implementation
may choose to raise Constraint_Error on the evaluation of the expression
(as usual), or it may choose to correctly evaluate the expression
and therefore choose the **others** alternative. Otherwise (no **others**),
Constraint_Error is raised either way -- on the expression
evaluation, or for the case_statement
itself.

10.e

For an enumeration type with a discontiguous
set of internal codes (see 13.4), the only way to
get values in between the proper values is via an object with an invalid representation;
there is no ``out-of-range'' situation that can produce them.

11

{*execution (case_statement)*
[partial]} 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

{*Overflow_Check* [partial]}
{*check, language-defined (Overflow_Check)*}
{*Constraint_Error (raised by failure
of run-time check)*} Otherwise (the value
is not covered by any discrete_choice_list,
perhaps due to being outside the base range), Constraint_Error is raised.

13.a

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.

15

16

17

18

Empty_Bin(1);

Empty_Bin(2);

18.a.1/1

18.a.2/1

18.a

{*extensions to Ada 83*}
In Ada 83, the expression
in a case_statement is not allowed
to be of a generic formal type. This restriction is removed in Ada 95;
an **others** discrete_choice
is required instead.

18.b

In Ada 95, a function call is the name
of an object; this was not true in Ada 83 (see 4.1,
``Names''). This change makes the following case_statement
legal:

18.c

--

18.d

Note that the result subtype given in a
function renaming_declaration is ignored;
for a case_statement whose expression calls
a such a function, the full coverage rules are checked using the result subtype
of the original function. Note that predefined operators such as "+"
have an unconstrained result subtype (see 4.5.1).
Note that generic formal functions do not have static result subtypes. Note
that the result subtype of an inherited subprogram need not correspond to any
namable subtype; there is still a perfectly good result subtype, though.

18.e

Ada 83 forgot to say what happens
for ``legally'' out-of-bounds values.

18.f

We take advantage of rules and terms (e.g.
*cover a value*) defined for discrete_choices
and discrete_choice_lists in 3.8.1,
``Variant Parts and Discrete Choices''.

18.g

In the Name Resolution Rule for the case
expression, we no longer need RM83-5.4(3)'s ``which must be determinable independently
of the context in which the expression occurs, but using the fact that the expression
must be of a discrete type,'' because the expression
is now a complete context. See 8.6, ``The
Context of Overload Resolution''.

18.h

Since type_conversions
are now defined as names, their
coverage rule is now covered under the general rule for names,
rather than being separated out along with qualified_expressions.