Contents   Index   Search   Previous   Next

13.9 Unchecked Type Conversions

   [{unchecked type conversion} {type conversion (unchecked)} {conversion (unchecked)} {type_conversion: See also unchecked type conversion} {cast: See unchecked type conversion} An unchecked type conversion can be achieved by a call to an instance of the generic function Unchecked_Conversion.]

Static Semantics

   The following language-defined generic library function exists:
   type Source(<>) is limited private;
   type Target(<>) is limited private;
function Ada.Unchecked_Conversion(S : Source) return Target;
pragma Convention(Intrinsic, Ada.Unchecked_Conversion);
pragma Pure(Ada.Unchecked_Conversion);
Reason: The pragma Convention implies that the attribute Access is not allowed for instances of Unchecked_Conversion.

Dynamic Semantics

   The size of the formal parameter S in an instance of Unchecked_Conversion is that of its subtype. [This is the actual subtype passed to Source, except when the actual is an unconstrained composite subtype, in which case the subtype is constrained by the bounds or discriminants of the value of the actual expression passed to S.]
   If all of the following are true, the effect of an unchecked conversion is to return the value of an object of the target subtype whose representation is the same as that of the source object S:
Ramification: Note that there is no requirement that the Sizes be known at compile time.
    Otherwise, the effect is implementation defined; in particular, the result can be abnormal (see 13.9.1).
Implementation defined: The effect of unchecked conversion.
Ramification: Whenever unchecked conversions are used, it is the programmer's responsibility to ensure that these conversions maintain the properties that are guaranteed by the language for objects of the target type. This requires the user to understand the underlying run-time model of the implementation. The execution of a program that violates these properties by means of unchecked conversions is erroneous.
An instance of Unchecked_Conversion can be applied to an object of a private type, assuming the implementation allows it.

Implementation Permissions

    An implementation may return the result of an unchecked conversion by reference, if the Source type is not a by-copy type. [In this case, the result of the unchecked conversion represents simply a different (read-only) view of the operand of the conversion.]
Ramification: In other words, the result object of a call on an instance of Unchecked_Conversion can occupy the same storage as the formal parameter S.
    An implementation may place restrictions on Unchecked_Conversion.
Ramification: For example, an instantiation of Unchecked_Conversion for types for which unchecked conversion doesn't make sense may be disallowed.

Implementation Advice

    The Size of an array object should not include its bounds; hence, the bounds should not be part of the converted data.
Ramification: On the other hand, we have no advice to offer about discriminants and tag fields.
    The implementation should not generate unnecessary run-time checks to ensure that the representation of S is a representation of the target type. It should take advantage of the permission to return by reference when possible. Restrictions on unchecked conversions should be avoided unless required by the target environment.
Implementation Note: As an example of an unnecessary run-time check, consider a record type with gaps between components. The compiler might assume that such gaps are always zero bits. If a value is produced that does not obey that assumption, then the program might misbehave. The implementation should not generate extra code to check for zero bits (except, perhaps, in a special error-checking mode).
    {recommended level of support (unchecked conversion) [partial]} The recommended level of support for unchecked conversions is:

Contents   Index   Search   Previous   Next   Legal