6.5 Return Statements
Syntax
Name Resolution Rules
Legality Rules
A return statement shall be within
a callable construct, and it
applies to the innermost callable
construct or
extended_return_statement
that contains it. A return statement shall not be within a body that
is within the construct to which the return statement applies.
For any return statement
that applies to a function body:
If the result subtype of the function is limited,
then the
expression
of the return statement (if any) shall be an
aggregate,
a function call (or equivalent use of an operator), or a
qualified_expression
or parenthesized expression whose operand is one of these.
If the result subtype of the function is class-wide,
the accessibility level of the type of the
expression
of the return statement shall not be statically deeper than that of the
master that elaborated the function body. If the result subtype has one
or more unconstrained access discriminants, the accessibility level of
the anonymous access type of each access discriminant, as determined
by the
expression
of the
simple_return_statement
or the
return_subtype_indication,
shall not be statically deeper than that of the master that elaborated
the function body.
Static Semantics
Dynamic Semantics
For the execution of an
extended_return_statement,
the
subtype_indication
or
access_definition
is elaborated. This creates the nominal subtype of the return object.
If there is an
expression,
it is evaluated and converted to the nominal subtype (which might raise
Constraint_Error — see
4.6);
the return object is created and the converted value is assigned to the
return object. Otherwise, the return object is created and initialized
by default as for a stand-alone object of its nominal subtype (see
3.3.1).
If the nominal subtype is indefinite, the return object is constrained
by its initial value.
For the execution of a
simple_return_statement,
the
expression
(if any) is first evaluated, converted to the result subtype, and then
is assigned to the anonymous
return object.
If the return object has any parts that are tasks,
the activation of those tasks does not occur until after the function
returns (see
9.2).
If the result type of a function is a specific tagged
type, the tag of the return object is that of the result type. If the
result type is class-wide, the tag of the return object is that of the
value of the expression. A check is made that the accessibility level
of the type identified by the tag of the result is not deeper than that
of the master that elaborated the function body. If this check fails,
Program_Error is raised.
Paragraphs 9 through
20 were deleted.
If the result subtype of a function has one or more
unconstrained access discriminants, a check is made that the accessibility
level of the anonymous access type of each access discriminant, as determined
by the
expression
or the
return_subtype_indication
of the function, is not deeper than that of the master that elaborated
the function body. If this check fails, Program_Error is raised.
In the case of a function, the
function_call
denotes a constant view of the return object.
Implementation Permissions
If the result subtype of a function is unconstrained,
and a call on the function is used to provide the initial value of an
object with a constrained nominal subtype, Constraint_Error may be raised
at the point of the call (after abandoning the execution of the function
body) if, while elaborating the
return_subtype_indication
or evaluating the
expression
of a return statement that applies to the function body, it is determined
that the value of the result will violate the constraint of the subtype
of this object.
Examples
Examples of return
statements:
return Key_Value(Last_Index); -- in a function body
return Node : Cell
do --
in a function body, see 3.10.1 for Cell
Node.Value := Result;
Node.Succ := Next_Node;
end return;