Annotated Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

B.3.3 Unchecked Union Types

1/3
{AI95-00216-01} {AI05-0229-1} {AI05-0269-1} [Specifying aspect Unchecked_Union to have the value True defines an interface correspondence between a given discriminated type and some C union. The aspect requires that the associated type shall be given a representation that allocates no space for its discriminant(s).]
Paragraphs 2 through 3 were moved to Annex J, “Obsolescent Features”. 

Static Semantics

3.1/3
 {AI05-0229-1} For a discriminated record type having a variant_part, the following language-defined representation aspect may be specified:
3.2/3
 Unchecked_Union

The type of aspect Unchecked_Union is Boolean. If directly specified, the aspect_definition shall be a static expression. If not specified (including by inheritance), the aspect is False.
3.a/3
Aspect Description for Unchecked_Union: Type is used to interface to a C union type.

Legality Rules

Paragraphs 4 and 5 were deleted. 
6/3
{AI95-00216-01} {AI05-0229-1} A type for which aspect Unchecked_Union is True is called an unchecked union type. A subtype of an unchecked union type is defined to be an unchecked union subtype. An object of an unchecked union type is defined to be an unchecked union object.
7/2
{AI95-00216-01} All component subtypes of an unchecked union type shall be C-compatible.
8/2
{AI95-00216-01} If a component subtype of an unchecked union type is subject to a per-object constraint, then the component subtype shall be an unchecked union subtype.
9/3
{AI95-00216-01} {AI05-0026-1} Any name that denotes a discriminant of an object of an unchecked union type shall occur within the declarative region of the type, and shall not occur within a record_representation_clause.
10/3
{AI95-00216-01} {AI05-0026-1} The type of a component declared in a variant_part of an unchecked union type shall not need finalization. In addition to the places where Legality Rules normally apply (see 12.3), this rule also applies in the private part of an instance of a generic unit. For an unchecked union type declared within the body of a generic unit, or within the body of any of its descendant library units, no part of the type of a component declared in a variant_part of the unchecked union type shall be of a formal private type or formal private extension declared within the formal part of the generic unit.
10.a/3
Reason: {AI05-0026-1} The last part is a classic assume-the-worst rule that avoids dependence on the actuals in a generic body. We did not include this in the definition of “needs finalization” as it has a bad interaction with the use of that term for the No_Nested_Finalization restriction. 
11/2
{AI95-00216-01} The completion of an incomplete or private type declaration having a known_discriminant_part shall not be an unchecked union type.
12/2
{AI95-00216-01} An unchecked union subtype shall only be passed as a generic actual parameter if the corresponding formal type has no known discriminants or is an unchecked union type.
12.a/2
Ramification: This includes formal private types without a known_discriminant_part, formal derived types that do not inherit any discriminants (formal derived types do not have known_discriminant_parts), and formal derived types that are unchecked union types. 

Static Semantics

13/2
{AI95-00216-01} An unchecked union type is eligible for convention C.
14/2
{AI95-00216-01} All objects of an unchecked union type have the same size.
15/2
{AI95-00216-01} Discriminants of objects of an unchecked union type are of size zero.
16/2
{AI95-00216-01} Any check which would require reading a discriminant of an unchecked union object is suppressed (see 11.5). These checks include:
17/2
The check performed when addressing a variant component (i.e., a component that was declared in a variant part) of an unchecked union object that the object has this component (see 4.1.3).
18/2
Any checks associated with a type or subtype conversion of a value of an unchecked union type (see 4.6). This includes, for example, the check associated with the implicit subtype conversion of an assignment statement.
19/2
The subtype membership check associated with the evaluation of a qualified expression (see 4.7) or an uninitialized allocator (see 4.8). 
19.a/2
Discussion: If a suppressed check would have failed, execution is erroneous (see 11.5). An implementation is always allowed to make a suppressed check if it can somehow determine the discriminant value. 

Dynamic Semantics

20/2
{AI95-00216-01} A view of an unchecked union object (including a type conversion or function call) has inferable discriminants if it has a constrained nominal subtype, unless the object is a component of an enclosing unchecked union object that is subject to a per-object constraint and the enclosing object lacks inferable discriminants.
21/2
{AI95-00216-01} An expression of an unchecked union type has inferable discriminants if it is either a name of an object with inferable discriminants or a qualified expression whose subtype_mark denotes a constrained subtype.
22/2
{AI95-00216-01} Program_Error is raised in the following cases:
23/2
Evaluation of the predefined equality operator for an unchecked union type if either of the operands lacks inferable discriminants.
24/2
Evaluation of the predefined equality operator for a type which has a subcomponent of an unchecked union type whose nominal subtype is unconstrained.
25/2
Evaluation of a membership test if the subtype_mark denotes a constrained unchecked union subtype and the expression lacks inferable discriminants.
26/2
Conversion from a derived unchecked union type to an unconstrained non-unchecked-union type if the operand of the conversion lacks inferable discriminants.
27/2
Execution of the default implementation of the Write or Read attribute of an unchecked union type.
28/2
Execution of the default implementation of the Output or Input attribute of an unchecked union type if the type lacks default discriminant values. 
Paragraph 29 was deleted. 
NOTES
30/2
11  {AI95-00216-01} The use of an unchecked union to obtain the effect of an unchecked conversion results in erroneous execution (see 11.5). Execution of the following example is erroneous even if Float'Size = Integer'Size:
31/3
{AI05-0229-1} type T (Flag : Boolean := False) is
   record
       case Flag is
           when False =>
               F1 : Float := 0.0;
           when True =>
               F2 : Integer := 0;
       end case;
    end record
    with Unchecked_Union;
32/2
X : T;
Y : Integer := X.F2; -- erroneous

Extensions to Ada 95

32.a/2
{AI95-00216-01} Pragma Unchecked_Union is new. 

Incompatibilities With Ada 2005

32.b/3
{AI05-0026-1} Correction: The use of discriminants on Unchecked_Union types is now illegal in record_representation_clauses, as it makes no sense to specify a position for something that is not supposed to exist. It is very unlikely that this change will have any impact on existing code. 

Extensions to Ada 2005

32.c/3
{AI05-0229-1} Aspect Unchecked_Union is new; pragma Unchecked_Union is now obsolescent. 

Wording Changes from Ada 2005

32.d/3
{AI05-0026-1} Correction: Revised the rules to use the “needs finalization” definition, and eliminated generic contract issues. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe