Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
 
Annotated Ada Reference Manual (Ada 202x Draft 21)Legal Information
Contents   Index   References   Search   Previous   Next 

4.2.1 User-Defined Literals

1/5
{AI12-0249-1} Using one or more of the aspects defined below, a type may be specified to allow the use of one or more kinds of literals as values of the type. 

Static Semantics

2/5
{AI12-0249-1} The following nonoverridable, type-related operational aspects may be specified for any type T:
3/5
{AI12-0249-1} Integer_Literal

This aspect is specified by a function_name that denotes a primitive function of T with one parameter of type String and a result type of T.
3.a/5
Aspect Description for Integer_Literal: Defines a function to implement user-defined integer literals.
4/5
{AI12-0249-1} Real_Literal

This aspect is specified by a function_name that denotes a primitive function of T with one parameter of type String and a result type of T.
4.a/5
Aspect Description for Real_Literal: Defines a function to implement user-defined real literals.
5/5
{AI12-0295-1} String_Literal

This aspect is specified by a function_name that denotes a primitive function of T with one parameter of type Wide_Wide_String and a result type of T.
5.a/5
Aspect Description for String_Literal: Defines a function to implement user-defined string literals.

Legality Rules

6/5
{AI12-0249-1} {AI12-0295-1} {AI12-0325-1} The Integer_Literal or Real_Literal aspect shall not be specified for a type T if the full view of T is a numeric type. The String_Literal aspect shall not be specified for a type T if the full view of T is a string type. In addition to the places where Legality Rules normally apply (see 12.3), these rules also apply in the private part of an instance of a generic unit. 

Dynamic Semantics

7/5
{AI12-0249-1} For the evaluation of an integer literal that has an expected type with a specified Integer_Literal aspect, the value is the result of a call on the function specified by the aspect, with the parameter being a String with lower bound one whose value corresponds to the textual representation of the integer literal. Similarly, the evaluation of a real literal that has an expected type with a specified Real_Literal aspect, the value is the result of a call on the function specified by the aspect, with the parameter being a String with lower bound one whose value corresponds to the textual representation of the real literal.
8/5
{AI12-0295-1} For the evaluation of a string_literal that has an expected type with a specified String_Literal aspect, the value is the result of a call on the function specified by the aspect, with the parameter being a Wide_Wide_String with lower bound one that corresponds to the literal. 

Bounded (Run-Time) Errors

9/5
{AI12-0249-1} {AI12-0325-1} It is a bounded error if the evaluation of a literal that has an expected type with a specified Integer_Literal, Real_Literal, or String_Literal aspect, propagates an exception. Either Program_Error or the exception propagated by the evaluation is raised at the point of use of the value of the literal. If it is recognized prior to run time that evaluation of such a literal will inevitably (if executed) result in such a bounded error, then this may be reported as an error prior to run time.
9.a/5
Implementation Note: {AI12-0249-1} As always, an implementation may apply "as-if" optimizations (those that result in the same external effects in the absence of erroneous execution) to the function calls associated with user-defined literals. In particular, if the function associated with a _Literal aspect has a Global aspect that indicates no references to global variables, then a number of optimizations are available to the implementation: 
9.b/5
The implementation can evaluate a user-defined literal function at compile-time if it has access to the body of the function (for example, if it is inlined), and that body doesn't reference anything evaluated at runtime. If the compile-time evaluation results in an exception, this bounded error allows the compilation unit to be rejected.
9.c/5
Implementations can use existing permissions (see 6.1.2) to avoid evaluating the function associated with a user-defined literal more than once for a particular literal value. This evaluation can be "hoisted" (done once per compilation unit during the elaboration of the unit) if the compiler can prove that the function doesn't depend on any constants or locals with a runtime value not yet elaborated.
9.d/5
If the literal value is not needed by the execution of the program, the function call can be omitted even if it might have side-effects (again, see 6.1.2).

Extensions to Ada 2012

9.e/5
{AI12-0249-1} {AI12-0295-1} {AI12-0325-1} The user-defined literal aspects Integer_Literal, Real_Literal, and String_Literal are new. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe