Contents Index Search Previous Next
13.9.1 Data Validity
1
Certain actions that can potentially lead to erroneous
execution are not directly erroneous, but instead can cause objects to
become abnormal. Subsequent uses of abnormal objects can be erroneous.
2
A scalar object can have an
invalid representation,
which means that the object's representation does not represent any value
of the object's subtype.
{uninitialized variables
[distributed]} The primary cause of invalid
representations is uninitialized variables.
3
Abnormal objects and invalid representations are
explained in this subclause.
Dynamic Semantics
4
{normal
state of an object [distributed]} {abnormal
state of an object [distributed]} When
an object is first created, and any explicit or default initializations
have been performed, the object and all of its parts are in the
normal
state. Subsequent operations generally leave them normal. However, an
object or part of an object can become
abnormal in the following
ways:
5
- {disruption of
an assignment} An assignment to the object
is disrupted due to an abort (see 9.8) or due
to the failure of a language-defined check (see 11.6).
6
- The object is not scalar, and is passed
to an in out or out parameter of an imported procedure
or language-defined input procedure, if after return from the procedure
the representation of the parameter does not represent a value of the
parameter's subtype.
7
{unspecified [partial]}
Whether or not an object actually becomes abnormal
in these cases is not specified. An abnormal object becomes normal again
upon successful completion of an assignment to the object as a whole.
Erroneous Execution
8
{erroneous execution (cause)
[partial]} It is erroneous to evaluate a
primary
that is a
name denoting an abnormal
object, or to evaluate a
prefix
that denotes an abnormal object.
8.a
Ramification: Although
a composite object with no subcomponents of an access type, and with
static constraints all the way down cannot become abnormal, a scalar
subcomponent of such an object can become abnormal.
8.b
The in out or out
parameter case does not apply to scalars; bad scalars are merely invalid
representations, rather than abnormal, in this case.
8.c
Reason: The reason we
allow access objects, and objects containing subcomponents of an access
type, to become abnormal is because the correctness of an access value
cannot necessarily be determined merely by looking at the bits of the
object. The reason we allow scalar objects to become abnormal is that
we wish to allow the compiler to optimize assuming that the value of
a scalar object belongs to the object's subtype, if the compiler can
prove that the object is initialized with a value that belongs to the
subtype. The reason we allow composite objects to become abnormal if
some constraints are nonstatic is that such object might be represented
with implicit levels of indirection; if those are corrupted, then even
assigning into a component of the object, or simply asking for its Address,
might have an unpredictable effect. The same is true if the discriminants
have been destroyed.
Bounded (Run-Time) Errors
9
{invalid
representation} {bounded
error (cause) [partial]} If the representation
of a scalar object does not represent a value of the object's subtype
(perhaps because the object was not initialized), the object is said
to have an
invalid representation. It is a bounded error to evaluate
the value of such an object.
{Program_Error (raised
by failure of run-time check)} {Constraint_Error
(raised by failure of run-time check)} If
the error is detected, either Constraint_Error or Program_Error is raised.
Otherwise, execution continues using the invalid representation. The
rules of the language outside this subclause assume that all objects
have valid representations. The semantics of operations on invalid representations
are as follows:
9.a
Discussion: The AARM
is more explicit about what happens when the value of the case expression
is an invalid representation.
10
- If the representation of the object
represents a value of the object's type, the value of the type is used.
11
- If the representation of the object
does not represent a value of the object's type, the semantics of operations
on such representations is implementation-defined, but does not by itself
lead to erroneous or unpredictable execution, or to other objects becoming
abnormal.
Erroneous Execution
12
{erroneous execution (cause)
[partial]} A call to an imported function
or an instance of Unchecked_Conversion is erroneous if the result is
scalar, and the result object has an invalid representation.
12.a
Ramification: In a typical
implementation, every bit pattern that fits in an object of an integer
subtype will represent a value of the type, if not of the subtype. However,
for an enumeration or floating point type, there are typically bit patterns
that do not represent any value of the type. In such cases, the implementation
ought to define the semantics of operations on the invalid representations
in the obvious manner (assuming the bounded error is not detected): a
given representation should be equal to itself, a representation that
is in between the internal codes of two enumeration literals should behave
accordingly when passed to comparison operators and membership tests,
etc. We considered requiring such sensible behavior, but it resulted
in too much arcane verbiage, and since implementations have little incentive
to behave irrationally, such verbiage is not important to have.
12.b
If a stand-alone scalar object
is initialized to a an in-range value, then the implementation can take
advantage of the fact that any out-of-range value has to be abnormal.
Such an out-of-range value can be produced only by things like unchecked
conversion, input, and disruption of an assignment due to abort or to
failure of a language-defined check. This depends on out-of-range values
being checked before assignment (that is, checks are not optimized away
unless they are proven redundant).
12.c
Consider
the following example:
12.d
type My_Int is range 0..99;
function Safe_Convert is new Unchecked_Conversion(My_Int, Integer);
function Unsafe_Convert is new Unchecked_Conversion(My_Int, Positive);
X : Positive := Safe_Convert(0); -- Raises Constraint_Error.
Y : Positive := Unsafe_Convert(0); -- Erroneous.
12.e
The call to Unsafe_Convert causes
erroneous execution. The call to Safe_Convert is not erroneous. The result
object is an object of subtype Integer containing the value 0. The assignment
to X is required to do a constraint check; the fact that the conversion
is unchecked does not obviate the need for subsequent checks required
by the language rules.
12.f
Implementation Note: If
an implementation wants to have a ``friendly'' mode, it might always
assign an uninitialized scalar a default initial value that is outside
the object's subtype (if there is one), and check for this value on some
or all reads of the object, so as to help detect references to uninitialized
scalars. Alternatively, an implementation might want to provide an ``unsafe''
mode where it presumed even uninitialized scalars were always within
their subtype.
12.g
Ramification: The above
rules imply that it is a bounded error to apply a predefined operator
to an object with a scalar subcomponent having an invalid representation,
since this implies reading the value of each subcomponent. Either Program_Error
or Constraint_Error is raised, or some result is produced, which if composite,
might have a corresponding scalar subcomponent still with an invalid
representation.
12.h
Note that it is not an error
to assign, convert, or pass as a parameter a composite object with an
uninitialized scalar subcomponent. In the other hand, it is a (bounded)
error to apply a predefined operator such as =, <, and xor
to a composite operand with an invalid scalar subcomponent.
13
{erroneous execution (cause)
[partial]} The dereference of an access value
is erroneous if it does not designate an object of an appropriate type
or a subprogram with an appropriate profile, if it designates a nonexistent
object, or if it is an access-to-variable value that designates a constant
object. [Such an access value can exist, for example, because of Unchecked_Deallocation,
Unchecked_Access, or Unchecked_Conversion.]
13.a
Ramification: The above
mentioned Unchecked_... features are not the only causes of such access
values. For example, interfacing to other languages can also cause the
problem.
13.b
One obscure example is if the
Adjust subprogram of a controlled type uses Unchecked_Access to create
an access-to-variable value designating a subcomponent of its controlled
parameter, and saves this access value in a global object. When Adjust
is called during the initialization of a constant object of the type,
the end result will be an access-to-variable value that designates a
constant object.
14
18 Objects can become abnormal
due to other kinds of actions that directly update the object's representation;
such actions are generally considered directly erroneous, however.
Wording Changes from Ada 83
14.a
In order to reduce the amount
of erroneousness, we separate the concept of an undefined value into
objects with invalid representation (scalars only) and abnormal objects.
14.b
Reading an object with an invalid
representation is a bounded error rather than erroneous; reading an abnormal
object is still erroneous. In fact, the only safe thing to do to an abnormal
object is to assign to the object as a whole.
Contents Index Search Previous Next Legal