Contents   Index   Search   Previous   Next

13.4 Enumeration Representation Clauses

   [An enumeration_representation_clause specifies the internal codes for enumeration literals.]


enumeration_representation_clause ::=
    for first_subtype_local_name use enumeration_aggregate;
enumeration_aggregate ::= array_aggregate

Name Resolution Rules

   {expected type (enumeration_representation_clause expressions) [partial]} The enumeration_aggregate shall be written as a one-dimensional array_aggregate, for which the index subtype is the unconstrained subtype of the enumeration type, and each component expression is expected to be of any integer type.
Ramification: The ``full coverage rules'' for aggregates applies. An others is not allowed -- there is no applicable index constraint in this context.

Legality Rules

   The first_subtype_local_name of an enumeration_representation_clause shall denote an enumeration subtype.
Ramification: As for all type-related representation items, the local_name is required to denote a first subtype.
   The expressions given in the array_aggregate shall be static, and shall specify distinct integer codes for each value of the enumeration type; the associated integer codes shall satisfy the predefined ordering relation of the type.
Reason: Each value of the enumeration type has to be given an internal code, even if the first subtype of the enumeration type is constrained to only a subrange (this is only possible if the enumeration type is a derived type). This ``full coverage'' requirement is important because one may refer to Enum'Base'First and Enum'Base'Last, which need to have defined representations.

Static Semantics

   {aspect of representation (coding) [partial]} {coding (aspect of representation)} An enumeration_representation_clause specifies the coding aspect of representation. {internal code} The coding consists of the internal code for each enumeration literal, that is, the integral value used internally to represent each literal.

Implementation Requirements

   For nonboolean enumeration types, if the coding is not specified for the type, then for each value of the type, the internal code shall be equal to its position number.
Reason: This default representation is already used by all known Ada compilers for nonboolean enumeration types. Therefore, we make it a requirement so users can depend on it, rather than feeling obliged to supply for every enumeration type an enumeration representation clause that is equivalent to this default rule.
Discussion: For boolean types, it is relatively common to use all ones for True, and all zeros for False, since some hardware supports that directly. Of course, for a one-bit Boolean object (like in a packed array), False is presumably zero and True is presumably one (choosing the reverse would be extremely unfriendly!).

Implementation Advice

   {recommended level of support (enumeration_representation_clause) [partial]} The recommended level of support for enumeration_representation_clauses is:
Ramification: The implementation may support numbers outside the above range, such as numbers greater than System.Max_Int. See AI83-00564.
Reason: The benefits of specifying the internal coding of a boolean type do not outweigh the implementation costs. Consider, for example, the implementation of the logical operators on a packed array of booleans with strange internal codes. It's implementable, but not worth it.
11  {8652/0009} Unchecked_Conversion may be used to query the internal codes used for an enumeration type. The attributes of the type, such as Succ, Pred, and Pos, are unaffected by the enumeration_ representation_clause. For example, Pos always returns the position number, not the internal integer code that might have been specified in a enumeration_ representation_clause.
Discussion: Suppose the enumeration type in question is derived:
type T1 is (Red, Green, Blue);
subtype S1 is T1 range Red .. Green;
type S2 is new S1;
for S2 use (Red => 10, Green => 20, Blue => 30);
{8652/0009} The enumeration_ representation_clause has to specify values for all enumerals, even ones that are not in S2 (such as Blue). The Base attribute can be used to get at these values. For example:
for I in S2'Base loop
    ... -- When I equals Blue, the internal code is 30.
end loop;
We considered allowing or requiring ``for S2'Base use ...'' in cases like this, but it didn't seem worth the trouble.


    Example of an enumeration representation clause:
type Mix_Code is (ADD, SUB, MUL, LDA, STA, STZ);
for Mix_Code use
   (ADD => 1, SUB => 2, MUL => 3, LDA => 8, STA => 24, STZ =>33);

Extensions to Ada 83

{extensions to Ada 83} As in other similar contexts, Ada 95 allows expressions of any integer type, not just expressions of type universal_integer, for the component expressions in the enumeration_aggregate. The preference rules for the predefined operators of root_integer eliminate any ambiguity.
For portability, we now require that the default coding for an enumeration type be the ``obvious'' coding using position numbers. This is satisfied by all known implementations.

Contents   Index   Search   Previous   Next   Legal