4.6 Type Conversions
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[Explicit type conversions, both value conversions and view conversions, are allowed between closely related types as defined below. This subclause also defines rules for value and view conversions to a particular subtype of a type, both explicit ones and those implicit in other constructs. ]
Syntax
2type_conversion
::=
subtype_mark
(expression
)
| subtype_mark
(name
)
3The target subtype of a type_conversion
is the subtype denoted by the subtype_mark
. The operand of a type_conversion
is the expression
or name
within the parentheses; its type is the operand type.
One type is convertible to a second type if a type_conversion
with the first type as operand type and the second type as target type is legal according to the rules of this subclause. Two types are convertible if each is convertible to the other.
{8652/0017} A type_conversion
is called a view conversion if both its target type and operand type are tagged, or if it appears in a call as an actual parameter of mode out or in out; other type_conversion
s are called value conversions.
name
, including in an object renaming, the prefix
of a selected_component
, and if the operand is a variable, on the left side of an assignment_statement
. View conversions to other types only occur as actual parameters. Allowing view conversions of untagged types in all contexts seemed to incur an undue implementation burden.Name Resolution Rules
6The operand of a type_conversion
is expected to be of any type.
type_conversion
is a “complete context”. The operand of a view conversion is interpreted only as a name
; the operand of a value conversion is interpreted as an expression
.
type_conversion
. This matters because an expression
that is a name
is evaluated and represents a value while a name
by itself can be an object; we want a view conversion to be an object. Legality Rules
8/2In a view conversion for an untagged type, the target type shall be convertible (back) to the operand type.
Paragraphs 9 through 20 were reorganized and moved below.
If there is a type (other than a root numeric type) that is an ancestor of both the target type and the operand type, or both types are class-wide types, then at least one of the following rules shall apply:
- The target type shall be untagged; or
- The operand type shall be covered by or descended from the target type; or
- The operand type shall be a class-wide type that covers the target type; or
extension_aggregate
has to be used instead, constructing a new value, rather than converting an existing value. However, a conversion from the class-wide type rooted at an ancestor type is permitted; such a conversion just verifies that the operand's tag is a descendant of the target. - The operand and target types shall both be class-wide types and the specific type associated with at least one of them shall be an interface type.
If there is no type (other than a root numeric type) that is the ancestor of both the target type and the operand type, and they are not both class-wide types, one of the following rules shall apply:
- If the target type is a numeric type, then the operand type shall be a numeric type.
- If the target type is an array type, then the operand type shall be an array type. Further:
- The types shall have the same dimensionality;
- Corresponding index types shall be convertible;
- The component subtypes shall statically match;
- If the component types are anonymous access types, then the accessibility level of the operand type shall not be statically deeper than that of the target type;
- Neither the target type nor the operand type shall be limited;
- If the target type of a view conversion has aliased components, then so shall the operand type; and
- The operand type of a view conversion shall not have a tagged, private, or volatile subcomponent.
- If the target type is universal_access, then the operand type shall be an access type.
- If the target type is a general access-to-object type, then the operand type shall be universal_access or an access-to-object type. Further, if the operand type is not universal_access:
type_conversion
T(X) (where T is an access type) is (almost) equivalent to the attribute_reference
X.all'Access, where the result is of type T. The only difference is that the type_conversion
accepts a null value, whereas the attribute_reference
would raise Constraint_Error. - If the target type is an access-to-variable type, then the operand type shall be an access-to-variable type;
- If the target designated type is tagged, then the operand designated type shall be convertible to the target designated type;
- If the target designated type is not tagged, then the designated types shall be the same, and either:
- the designated subtypes shall statically match; or
- the designated type shall be discriminated in its full view and unconstrained in any partial view, and one of the designated subtypes shall be unconstrained;
- The accessibility level of the operand type shall not be statically deeper than that of the target type, unless the target type is an anonymous access type of a stand-alone object. If the target type is that of such a stand-alone object, the accessibility level of the operand type shall not be statically deeper than that of the declaration of the stand-alone object.
- If the target type is a pool-specific access-to-object type, then the operand type shall be universal_access.
- If the target type is an access-to-subprogram type, then the operand type shall be universal_access or an access-to-subprogram type. Further, if the operand type is not universal_access:
- The designated profiles shall be subtype conformant.
- The accessibility level of the operand type shall not be statically deeper than that of the target type. If the operand type is declared within a generic body, the target type shall be declared within the generic body.
- If the target type has a Global aspect other than in out all or Unspecified, then each mode of the Global aspect of the operand type shall identify a subset of the variables identified by the corresponding mode of the target type Global aspect, or by the in out mode of the target type Global aspect.
- If the target type is nonblocking, the operand type shall be nonblocking.
In addition to the places where Legality Rules normally apply (see 12.3), these rules apply also in the private part of an instance of a generic unit.
Static Semantics
25A type_conversion
that is a value conversion denotes the value that is the result of converting the value of the operand to the target subtype.
A type_conversion
that is a view conversion denotes a view of the object denoted by the operand. This view is a variable of the target type if the operand denotes a variable; otherwise, it is a constant of the target type.
The nominal subtype of a type_conversion
is its target subtype.
Dynamic Semantics
28For the evaluation of a type_conversion
that is a value conversion, the operand is evaluated, and then the value of the operand is converted to a corresponding value of the target type, if any. If there is no value of the target type that corresponds to the operand value, Constraint_Error is raised[; this can only happen on conversion to a modular type, and only when the operand value is outside the base range of the modular type.] Additional rules follow:
- Numeric Type Conversion
- If the target and the operand types are both integer types, then the result is the value of the target type that corresponds to the same mathematical integer as the operand.
- If the target type is a decimal fixed point type, then the result is truncated (toward 0) if the value of the operand is not a multiple of the small of the target type.
- If the target type is some other real type, then the result is within the accuracy of the target type (see G.2, “Numeric Performance Requirements”, for implementations that support the Numerics Annex).
- If the target type is an integer type and the operand type is real, the result is rounded to the nearest integer (away from zero if exactly halfway between two integers).
- Enumeration Type Conversion
- The result is the value of the target type with the same position number as that of the operand value.
- Array Type Conversion
- If the target subtype is a constrained array subtype, then a check is made that the length of each dimension of the value of the operand equals the length of the corresponding dimension of the target subtype. The bounds of the result are those of the target subtype.
- If the target subtype is an unconstrained array subtype, then the bounds of the result are obtained by converting each bound of the value of the operand to the corresponding index type of the target type. For each nonnull index range, a check is made that the bounds of the range belong to the corresponding index subtype.
- In either array case, the value of each component of the result is that of the matching component of the operand value (see 4.5.2).
- If the component types of the array types are anonymous access types, then a check is made that the accessibility level of the operand type is not deeper than that of the target type.
- Composite (Non-Array) Type Conversion
- The value of each nondiscriminant component of the result is that of the matching component of the operand value.
- [The tag of the result is that of the operand.] If the operand type is class-wide, a check is made that the tag of the operand identifies a (specific) type that is covered by or descended from the target type.
type_conversion
preserves the tag is stated officially in 3.9, “Tagged Types and Type Extensions” - For each discriminant of the target type that corresponds to a discriminant of the operand type, its value is that of the corresponding discriminant of the operand value; if it corresponds to more than one discriminant of the operand type, a check is made that all these discriminants are equal in the operand value.
- For each discriminant of the target type that corresponds to a discriminant that is specified by the
derived_type_definition
for some ancestor of the operand type (or if class-wide, some ancestor of the specific type identified by the tag of the operand), its value in the result is that specified by thederived_type_definition
.
- For each discriminant of the operand type that corresponds to a discriminant that is specified by the
derived_type_definition
for some ancestor of the target type, a check is made that in the operand value it equals the value specified for it. 46 - For each discriminant of the result, a check is made that its value belongs to its subtype.
- Access Type Conversion
- For an access-to-object type, a check is made that the accessibility level of the operand type is not deeper than that of the target type, unless the target type is an anonymous access type of a stand-alone object. If the target type is that of such a stand-alone object, a check is made that the accessibility level of the operand type is not deeper than that of the declaration of the stand-alone object[; then if the check succeeds, the accessibility level of the target type becomes that of the operand type].
- If the operand value is null, the result of the conversion is the null value of the target type.
- If the operand value is not null, then the result designates the same object (or subprogram) as is designated by the operand value, but viewed as being of the target designated subtype (or profile); any checks associated with evaluating a conversion to the target designated subtype are performed.
After conversion of the value to the target type, if the target subtype is constrained, a check is performed that the value satisfies this constraint. If the target subtype excludes null, then a check is made that the value is not null. If predicate checks are enabled for the target subtype (see 3.2.4), a check is performed that the value satisfies the predicates of the target subtype, unless the conversion is:.
- a view conversion that is the target of an assignment statement and is not referenced with a
target_name
, or an actual parameter of mode out; or 51.2/5 - an implicit subtype conversion of an actual parameter of mode out to the nominal subtype of its formal parameter.
For the evaluation of a view conversion, the operand name
is evaluated, and a new view of the object denoted by the operand is created, whose type is the target type; if the target type is composite, checks are performed as above for a value conversion.
The properties of this new view are as follows:
- {8652/0017} If the target type is composite, the bounds or discriminants (if any) of the view are as defined above for a value conversion; each nondiscriminant component of the view denotes the matching component of the operand object; the subtype of the view is constrained if either the target subtype or the operand object is constrained, or if the target subtype is indefinite, or if the operand type is a descendant of the target type and has discriminants that were not inherited from the target type;
- If the target type is tagged, then an assignment to the view assigns to the corresponding part of the object denoted by the operand; otherwise, an assignment to the view assigns to the object, after converting the assigned value to the subtype of the object (which can raise Constraint_Error);
- Reading the value of the view yields the result of converting the value of the operand object to the target subtype (which can raise Constraint_Error), except if the object is of an elementary type and the view conversion is passed as an out parameter; in this latter case, the value of the operand object may be used to initialize the formal parameter without checking against any constraint of the target subtype (as described more precisely in 6.4.1).
If an Accessibility_Check fails, Program_Error is raised. If a predicate check fails, the effect is as defined in subclause 3.2.4, “Subtype Predicates”. Any other check associated with a conversion raises Constraint_Error if it fails.
Conversion to a type is the same as conversion to an unconstrained subtype of the type.
type_conversion
actually converts to a subtype. For example, the evaluation of a range
is defined to convert the values of the expressions to the type of the range. Evaluation of a value conversion of an object either creates a new anonymous object[ (similar to the object created by the evaluation of an aggregate
or a function call)] or yields a new view of the operand object without creating a new object:
- If the target type is a by-reference type and there is a type that is an ancestor of both the target type and the operand type then no new object is created;
- If the target type is an array type having aliased components and the operand type is an array type having unaliased components, then a new object is created;
- If the target type is an elementary type, then a new object is created;
- Otherwise, it is unspecified whether a new object is created.
If a new object is created, then the initialization of that object is an assignment operation.
type_conversion
s, type conversions are performed implicitly in situations where the expected type and the actual type of a construct differ, as is permitted by the type resolution rules (see 8.6). For example, an integer literal is of the type universal_integer, and is implicitly converted when assigned to a target of some specific integer type. Similarly, an actual parameter of a specific tagged type is implicitly converted when the corresponding formal parameter is of a class-wide type.type_conversion
cannot be an allocator
, an aggregate
, a string_literal
, a character_literal
, or an attribute_reference
for an Access or Unchecked_Access attribute. Similarly, such an expression
enclosed by parentheses is not allowed. A qualified_expression
(see 4.7) can be used instead of such a type_conversion
.type_conversion
of an elementary type passed as an out parameter. Hence, it is recommended that the first subtype be specified as the target to minimize confusion (a similar recommendation applies to renaming and generic formal in out objects). Examples
63Examples of numeric type conversion:
Real(2*J) -- value is converted to floating point
Integer(1.6) -- value is 2
Integer(-0.4) -- value is 0
Example of conversion between derived types:
type A_Form is new B_Form;
67X : A_Form;
Y : B_Form;
68X := A_Form(Y);
Y := B_Form(X); -- the reverse conversion
69Examples of conversions between array types:
type Sequence is array (Integer range <>) of Integer;
subtype Dozen is Sequence(1 .. 12);
Ledger : array(1 .. 100) of Integer;
71Sequence(Ledger) -- bounds are those of Ledger
Sequence(Ledger(31 .. 42)) -- bounds are 31 and 42
Dozen(Ledger(31 .. 42)) -- bounds are those of Dozen
Incompatibilities With Ada 83
character_literal
is not allowed as the operand of a type_conversion
, since there are now two character types in package Standard.Extensions to Ada 83
type_conversion
is considered the name of an object in certain circumstances (such a type_conversion
is called a view conversion). In particular, as in Ada 83, a type_conversion
can appear as an in out or out actual parameter. In addition, if the target type is tagged and the operand is the name
of an object, then so is the type_conversion
, and it can be used as the prefix
to a selected_component
, in an object_renaming_declaration
, etc.qualified_expression
. It wouldn't make sense on a membership test, and we wish to retain a connection between subtype membership and subtype qualification. In general, a subtype membership test returns True if and only if a corresponding subtype qualification succeeds without raising an exception. Other operations that take arrays perform sliding. Wording Changes from Ada 83
type_conversion
, except in a NOTE.name
. Incompatibilities With Ada 95
Extensions to Ada 95
Wording Changes from Ada 95
Inconsistencies With Ada 2005
Wording Changes from Ada 2005
Inconsistencies With Ada 2012
assignment_statement
. Both of these cases seem quite unlikely, as programs (outside of ACATS tests) that depend on the failure of checks are very rare, and the predicate might be checking uninitialized components (making check failure unreliable). Incompatibilities With Ada 2012
Wording Changes from Ada 2012
name
. The requirement could cause unintended consequences in the case where the operand of a type conversion represents an object but is more complex than a simple name
(such as a qualified_expression
or conditional_expression
).