[Home] [Prev] [Next] [Index]

4.6 Type Conversions

4.6 Type Conversions

1
[Explicit type conversions, both value conversions and view conversions, are allowed between closely related types as defined below.  This clause 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

2
type_conversion ::=
    subtype_mark(expression)
  | subtype_mark(name)

3
The 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.

4
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 clause. Two types are convertible if each is convertible to the other.

4.a
Ramification: Note that "convertible" is defined in terms of legality of the conversion.  Whether the conversion would raise an exception at run time is irrelevant to this definition.

5
A type_conversion whose operand is the name of an object is called a view conversion if its target type is tagged, or if it appears as an actual parameter of mode out or in out; other type_conversions are called value conversions.

5.a
Ramification: A view conversion to a tagged type can appear in any context that requires an object 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

6
The operand of a type_conversion is expected to be of any type.

6.a
Discussion:  This replaces the "must be determinable" wording of Ada 83. This is equivalent to (but hopefully more intuitive than) saying that the operand of a type_conversion is a "complete context."

7
The operand of a view conversion is interpreted only as a name; the operand of a value conversion is interpreted as an expression.

7.a
Reason: This formally resolves the syntactic ambiguity between the two forms of type_conversion, not that it really matters.

Legality Rules

8
If the target type is a numeric type, then the operand type shall be a numeric type.

9
If the target type is an array type, then the operand type shall be an array type.  Further:

10 ·
The types shall have the same dimensionality;

11 ·
Corresponding index types shall be convertible; and

12 ·
The component subtypes shall statically match.

13
If the target type is a general access type, then the operand type shall be an access-to-object type.  Further:

13.a
Discussion:  The Legality Rules and Dynamic Semantics are worded so that a 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 type_conversion accepts a null value, whereas the attribute_reference would raise Constraint_Error.

14 ·
If the target type is an access-to-variable type, then the operand type shall be an access-to-variable type;

14.a
Ramification: If the target type is an access-to-constant type, then the operand type can be access-to-constant or access-to-variable.

15 ·
If the target designated type is tagged, then the operand designated type shall be convertible to the target designated type;

16 ·
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 target designated subtype shall be discriminated and unconstrained; and

16.a
Reason: These rules are designed to ensure that aliased array objects only need "dope" if their nominal subtype is unconstrained, but they can always have dope if required by the run-time model (since no sliding is permitted as part of access type conversion). By contrast, aliased discriminated objects will always need their discriminants stored with them, even if nominally constrained. (Here, we are assuming an implementation that represents an access value as a single pointer.)

17 ·
The accessibility level of the operand type shall not be statically deeper than that of the target type. In addition to the places where Legality Rules normally apply (see 12.3), this rule applies also in the private part of an instance of a generic unit.

17.a
Ramification: The access parameter case is handled by a run-time check. Run-time checks are also done in instance bodies.

18
If the target type is an access-to-subprogram type, then the operand type shall be an access-to-subprogram type.  Further:

19 ·
The designated profiles shall be subtype-conformant.

20 ·
The accessibility level of the operand type shall not be statically deeper than that of the target type. In addition to the places where Legality Rules normally apply (see 12.3), this rule applies also in the private part of an instance of a generic unit. If the operand type is declared within a generic body, the target type shall be declared within the generic body.

20.a
Reason: The reason it is illegal to convert from an access-to-subprogram type declared in a generic body to one declared outside that body is that in an implementation that shares generic bodies, procedures declared inside the generic need to have a different calling convention - they need an extra parameter pointing to the data declared in the current instance. For procedures declared in the spec, that's OK, because the compiler can know about them at compile time of the instantiation.

21
If the target type is not included in any of the above four cases, there shall be a type that is an ancestor of both the target type and the operand type. Further, if the target type is tagged, then either:

22 ·
The operand type shall be covered by or descended from the target type; or

22.a
Ramification: This is a conversion toward the root, which is always safe.

23 ·
The operand type shall be a class-wide type that covers the target type.

23.a
Ramification: This is a conversion of a class-wide type toward the leaves, which requires a tag check.  See Dynamic Semantics.

23.b
These two rules imply that a conversion from a parent type to a type extension is not permitted, as this would require specifying the values for additional components, in general, and changing the tag.  An 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 the parent type is permitted; such a conversion just verifies that the operand's tag is a descendant of the target.

24
In a view conversion for an untagged type, the target type shall be convertible (back) to the operand type.

24.a
Reason: Untagged view conversions appear only as [in] out parameters. Hence, the reverse conversion must be legal as well. The forward conversion must be legal even if an out parameter, because actual parameters of an access type are always copied in anyway.

Static Semantics

25
A 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.

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

27
The nominal subtype of a type_conversion is its target subtype.

Dynamic Semantics

28
For 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:

29 ·
Numeric Type Conversion

30 ·   
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.
31 ·   
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.
32 ·   
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).
32.a
Discussion:  An integer type might have more bits of precision than a real type, so on conversion (of a large integer), some precision might be lost.

33 ·   
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).
33.a
Discussion:  This was implementation defined in Ada 83. There seems no reason to preserve the nonportability in Ada 9X.  Round-away-from-zero is the conventional definition of rounding, and standard Fortran and COBOL both specify rounding away from zero, so for interoperability, it seems important to pick this.  This is also the most easily "undone" by hand. Round-to-nearest-even is an alternative, but that is quite complicated if not supported by the hardware.  In any case, this operation is not expected to be part of an inner loop, so predictability and portability are judged most important.  We anticipate that a floating point attribute function Unbiased_Rounding will be provided for those applications that require round-to-nearest-even. "Deterministic" rounding is required for static conversions to integer as well. See 4.9.

34 ·
Enumeration Type Conversion

35 ·   
The result is the value of the target type with the same position number as that of the operand value.
36 ·
Array Type Conversion

37 ·   
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.
38 ·   
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.
38.a
Discussion:  Only nonnull index ranges are checked, per AI-00313.

39 ·   
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).
39.a
Ramification: This applies whether or not the component is initialized.

40 ·
Composite (Non-Array) Type Conversion

41 ·   
The value of each nondiscriminant component of the result is that of the matching component of the operand value.
41.a
Ramification: This applies whether or not the component is initialized.

42 ·   
[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.
42.a
Ramification: This check is certain to succeed if the operand type is itself covered by or descended from the target type.

42.b
Proof: The fact that a type_conversion preserves the tag is stated officially in , "Tagged Types and Type Extensions"

43 ·   
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.
44 ·   
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 the derived_type_definition.
44.a
Ramification: It is a ramification of the rules for the discriminants of derived types that each discriminant of the result is covered either by this paragraph or the previous one.  See 3.7.

45 ·   
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.
47 ·
Access Type Conversion

48 ·   
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.
48.a
Ramification: This check is needed for operands that are access parameters and in instance bodies.

48.b
Note that this check can never fail for the implicit conversion to the anonymous type of an access parameter that is done when calling a subprogram with an access parameter.

49 ·   
If the target type is an anonymous access type, a check is made that the value of the operand is not null; if the target is not an anonymous access type, then the result is null if the operand value is null.
49.a
Ramification: A conversion to an anonymous access type happens implicitly as part of initializing an access discriminant or access parameter.

49.b
Reason: As explained in 3.10, "Access Types", it is important that a value of an anonymous access type can never be null.

50 ·   
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.
50.a
Ramification: The checks are certain to succeed if the target and operand designated subtypes statically match.

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

51.a
Ramification: The above check is a Range_Check for scalar subtypes, a Discriminant_Check or Index_Check for access subtypes, and a Discriminant_Check for discriminated subtypes. The Length_Check for an array conversion is performed as part of the conversion to the target type.

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

53
The properties of this new view are as follows:

54 ·
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 operand type is a descendant of the target type, and has discriminants that were not inherited from the target type;

55 ·
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 might raise Constraint_Error);

56 ·
Reading the value of the view yields the result of converting the value of the operand object to the target subtype (which might raise Constraint_Error), except if the object is of an access type and the view conversion is passed as an out parameter; in this latter case, the value of the operand object is used to initialize the formal parameter without checking against any constraint of the target subtype (see 6.4.1).

56.a
Reason: This ensures that even an out parameter of an access type is initialized reasonably.

57
If an Accessibility_Check fails, Program_Error is raised. Any other check associated with a conversion raises Constraint_Error if it fails.

58
Conversion to a type is the same as conversion to an unconstrained subtype of the type.

58.a
Reason: This definition is needed because the semantics of various constructs involves converting to a type, whereas an explicit 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.

58.b
Ramification: A conversion to a scalar type, or, equivalently, to an unconstrained scalar subtype, can raise Constraint_Error if the value is outside the base range of the type.

NOTES

59 20
In addition to explicit type_conversions, 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.

60
Even when the expected and actual types are the same, implicit subtype conversions are performed to adjust the array bounds (if any) of an operand to match the desired target subtype, or to raise Constraint_Error if the (possibly adjusted) value does not satisfy the constraints of the target subtype.

61 21
A ramification of the overload resolution rules is that the operand of an (explicit) type_conversion cannot be the literal null, 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.

62 22
The constraint of the target subtype has no effect for a 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

63
Examples of numeric type conversion:

64
Real(2*J)      --  value is converted to floating point
Integer(1.6)   --  value is 2
Integer(-0.4)  --  value is 0

65
Example of conversion between derived types:

66
type A_Form is new B_Form;

67
X : A_Form;
Y : B_Form;

68
X := A_Form(Y);
Y := B_Form(X);  --  the reverse conversion

69
Examples of conversions between array types:

70
type Sequence is array (Integer range <>) of Integer;
subtype Dozen is Sequence(1 .. 12);
Ledger : array(1 .. 100) of Integer;

71
Sequence(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

71.a
A character_literal is not allowed as the operand of a type_conversion, since there are now two character types in package Standard.

71.b
The component subtypes have to statically match in an array conversion, rather than being checked for matching constraints at run time.

71.c
Because sliding of array bounds is now provided for operations where it was not in Ada 83, programs that used to raise Constraint_Error might now continue executing and produce a reasonable result. This is likely to fix more bugs than it creates.

Extensions to Ada 83

71.d
A 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.

71.e
We no longer require type-mark conformance between a parameter of the form of a type conversion, and the corresponding formal parameter.  This had caused some problems for inherited subprograms (since there isn't really a type-mark for converted formals), as well as for renamings, formal subprograms, etc.  See AI-245, AI-318, AI-547.

71.f
We now specify "deterministic" rounding from real to integer types when the value of the operand is exactly between two integers (rounding is away from zero in this case).

71.g
"Sliding" of array bounds (which is part of conversion to an array subtype) is performed in more cases in Ada 9X than in Ada 83. Sliding is not performed on the operand of a membership test, nor on the operand of a 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

71.h
We no longer explicitly list the kinds of things that are not allowed as the operand of a type_conversion, except in a NOTE.

71.i
The rules in this clause subsume the rules for "parameters of the form of a type conversion," and have been generalized to cover the use of a type conversion as a name.



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-1998, Rational Software Corporation.   All rights reserved.