4.2 Literals
Discussion: An enumeration literal that
is an
identifier
rather than a
character_literal
is not considered a
literal in the above sense, because it involves
no special notation “suited to its kind”. It might more properly
be called an
enumeration_identifier, except
for historical reasons.
Name Resolution Rules
For a
name
that consists of a
character_literal,
either its expected type shall be a single character type, in which case
it is interpreted as a parameterless
function_call
that yields the corresponding value of the character type, or its expected
profile shall correspond to a parameterless function with a character
result type, in which case it is interpreted as the name of the corresponding
parameterless function declared as part of the character type's definition
(see
3.5.1). In either case, the
character_literal
denotes the
enumeration_literal_specification.
{
AI12-0325-1}
{
AI12-0373-1}
{
AI22-0109-1}
The expected type for a
primary
that is a
string_literal
shall be a single string type or
a descendant of
a type with a specified String_Literal aspect (see
4.2.1).
In either case, the
string_literal
is interpreted to be of its expected type. If the expected type of an
integer literal is
a descendant of a type
with a specified Integer_Literal aspect (see
4.2.1),
the literal is interpreted to
be of its expected
type; otherwise it is interpreted to be of type
universal_integer.
If the expected type of a real literal is
a descendant
of a type with a specified Real_Literal aspect (see
4.2.1),
it is interpreted to be of its
expected type; otherwise,
it is interpreted to be of type
universal_real.
Legality Rules
Static Semantics
Dynamic Semantics
{
AI12-0249-1}
If
its expected type is a numeric type, the evaluation of a numeric literal
yields the represented value. [In other cases, the effect of evaluating
a numeric literal is determined by the Integer_Literal or Real_Literal
aspect that applies (see
4.2.1).]
{
AI12-0249-1}
The evaluation of the literal
null yields the null value of the
expected type.
{
AI12-0295-1}
{
AI12-0325-1}
{
AI22-0100-2}
The evaluation of a
string_literal
that is a
primary
and has an expected type that is a string type, yields an array value
containing the value of each character of the sequence of characters
of the
string_literal,
as defined in
2.6. The bounds of this array
value are determined according to the rules for
positional_array_aggregates
without an others choice (see
4.3.3)
,
except that for a null string literal, the upper bound is the predecessor
of the lower bound. [In other cases, the effect of evaluating
a
string_literal
is determined by the String_Literal aspect that applies (see
4.2.1).]
Ramification: {
AI22-0100-2}
Determining the bounds of a string_literal
includes the checks on the computed bounds themselves (such as checking
that the determined range is compatible with the appropriate index subtype).
{
AI12-0295-1}
{
AI12-0325-1}
{
AI22-0100-2}
For the evaluation of a
string_literal
of a string type
T, a check is made that the value of each character
of the
string_literal
belongs to the component subtype of
T.
For
the evaluation of a null string literal of a string type, a check is
made that its lower bound is greater than the lower bound of the base
range of the index type. The exception Constraint_Error
is raised if
this check either
of these checks fails.
Ramification: {
AI12-0005-1}
If no predicates apply to the component subtype, the checks on the characters
need not involve more than two checks altogether, since one need only
check the characters of the string with the lowest and highest position
numbers against the range of the component subtype.
Examples
Examples of literals:
3.14159_26536 -- a real literal
1_345 -- an integer literal
'A' -- a character literal
"Some Text" -- a string literal
Incompatibilities With Ada 83
Because
character_literals
are now treated like other literals, in that they are resolved using
context rather than depending on direct visibility, additional qualification
might be necessary when passing a
character_literal
to an overloaded subprogram.
Extensions to Ada 83
Wording Changes from Ada 83
Name Resolution rules for enumeration literals
that are not
character_literals
are not included anymore, since they are neither syntactically nor semantically
"literals" but are rather names of parameterless functions.
Extensions to Ada 95
{
AI95-00230-01}
{
AI95-00231-01}
Null now has type
universal_access,
which is similar to other literals.
Null can be used with anonymous
access types.
Wording Changes from Ada 2012
{
AI12-0249-1}
{
AI12-0295-1}
The rules in this subclause are adjusted to allow for the possibility
of user-defined literals. These are fully documented in the next subclause.
Extensions to Ada 2022
{
AI22-0109-1}
Descendants of types with specified
Integer_Literal, Real_Literal, and String_Literal aspects can use the
associated literals. This allows literals to be used with class-wide
values, with the appropriate dispatching and tag-interdeterminate call
semantics.
Wording Changes from Ada 2022
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe