3.7.2 Operations of Discriminated Types
[If a discriminated type has
default_expressions
for its discriminants, then unconstrained variables of the type are permitted,
and the discriminants of such a variable can be changed by assignment
to the variable. For a formal parameter of such a type, an attribute
is provided to determine whether the corresponding actual parameter is
constrained or unconstrained.]
Static Semantics
For a
prefix
A that is of a discriminated type [(after any implicit dereference)],
the following attribute is defined:
A'Constrained
{
AI05-0214-1}
Yields the value True if A denotes a constant, a value, a tagged object,
or a constrained variable, and False otherwise.
Implementation Note: {
AI05-0214-1}
This attribute is primarily used on parameters, to determine whether
the discriminants can be changed as part of an assignment. The Constrained
attribute is statically True for
in parameters. For
in out
and
out parameters of a discriminated type, the value of this
attribute needs to be passed as an implicit parameter, in general. However,
if the type is tagged or does not have defaults for its discriminants,
the attribute is statically True, so no implicit parameter is needed.
Parameters of a limited untagged type with defaulted discriminants need
this implicit parameter, unless there are no nonlimited views, because
they might be passed to a subprogram whose body has visibility on a nonlimited
view of the type, and hence might be able to assign to the object and
change its discriminants.
Reason: {
AI05-0214-1}
{
AI12-0005-1}
All tagged objects are known to be constrained (as nonlimited tagged
types cannot have discriminant defaults, and limited tagged objects are
immutably limited), and are always considered constrained by this attribute
to avoid distributed overhead for parameters of limited class-wide types,
as limited tagged objects may technically be unconstrained if they use
defaulted discriminants. Such objects still cannot have their discriminants
changed, as assignment is not supported for them, so there is no use
for this attribute that would justify the overhead of passing it with
all class-wide parameters.
Discussion: {
AI05-0005-1}
{
AI05-0214-1}
If the type of A is a type derived from an untagged partial view of a
tagged type such that it is not a tagged type, then A is not considered
a tagged object, and A'Constrained can return either True or False depending
on the nature of the object.
Erroneous Execution
The execution of a construct
is erroneous if the construct has a constituent that is a
name
denoting a subcomponent that depends on discriminants, and the value
of any of these discriminants is changed by this execution between evaluating
the
name and
the last use (within this execution) of the subcomponent denoted by the
name.
Ramification: This rule applies to
assignment_statements,
calls (except when the discriminant-dependent subcomponent is an
in
parameter passed by copy),
indexed_components,
and
slices.
Ada 83 only covered the first two cases. AI83-00585 pointed out the situation
with the last two cases. The cases of
object_renaming_declarations
and generic formal
in out objects are handled differently, by
disallowing the situation at compile time.
Extensions to Ada 83
For consistency with other
attributes, we are allowing the
prefix
of Constrained to be a value as well as an object of a discriminated
type, and also an implicit dereference. These extensions are not important
capabilities, but there seems no reason to make this attribute different
from other similar attributes. We are curious what most Ada 83 compilers
do with F(1).X'Constrained.
We now handle in a general way the cases of
erroneousness identified by AI83-00585, where the
prefix
of an
indexed_component
or
slice
is discriminant-dependent, and the evaluation of the index or discrete
range changes the value of a discriminant.
Wording Changes from Ada 83
We have moved all discussion of erroneous use
of
names that
denote discriminant-dependent subcomponents to this subclause. In Ada
83, it used to appear separately under
assignment_statements
and subprogram calls.
Wording Changes from Ada 2005
{
AI05-0214-1}
A'Constrained is now defined to return True for any A that is a tagged
object. This doesn't change the result for any A allowed by previous
versions of Ada; the change is necessary to avoid unnecessary overhead
for limited tagged parameters.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe