6.4 Subprogram Calls
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
A subprogram call is either a procedure_call_statement
or a function_call
; [it invokes the execution of the subprogram_body
. The call specifies the association of the actual parameters, if any, with formal parameters of the subprogram.]
Syntax
2procedure_call_statement
::=
procedure_name
;
| procedure_prefix
actual_parameter_part
;
3function_call
::=
function_name
| function_prefix
actual_parameter_part
function_call
s. See 6.6. actual_parameter_part
::=
(parameter_association
{, parameter_association
})
5parameter_association
::=
[formal_parameter_selector_name
=>] explicit_actual_parameter
6explicit_actual_parameter
::=
expression
| variable_name
7/5A parameter_association
is named or positional according to whether or not the formal_parameter_selector_name
is specified. For the parameter_association
s of a single actual_parameter_part
or iterator_actual_parameter_part
, any positional associations shall precede any named associations. Named associations are not allowed if the prefix
in a subprogram call is an attribute_reference
.
Name Resolution Rules
8/2The name
or prefix
given in a procedure_call_statement
shall resolve to denote a callable entity that is a procedure, or an entry renamed as (viewed as) a procedure. The name
or prefix
given in a function_call
shall resolve to denote a callable entity that is a function. The name
or prefix
shall not resolve to denote an abstract subprogram unless it is also a dispatching subprogram. [When there is an actual_parameter_part
, the prefix
can be an implicit_dereference
of an access-to-subprogram value.]
A subprogram call shall contain at most one association for each formal parameter. Each formal parameter without an association shall have a default_expression
(in the profile of the view denoted by the name
or prefix
). [This rule is an overloading rule (see 8.6).]
Static Semantics
9.1/5If the name
or prefix
of a subprogram call denotes a prefixed view (see 4.1.3), the subprogram call is equivalent to a call on the underlying subprogram, with the first actual parameter being provided by the prefix
of the prefixed view (or the Access attribute of this prefix
if the first formal parameter is an access parameter), and the remaining actual parameters given by the actual_parameter_part
, if any.
Dynamic Semantics
10/2For the execution of a subprogram call, the name
or prefix
of the call is evaluated, and each parameter_association
is evaluated (see 6.4.1). If a default_expression
is used, an implicit parameter_association
is assumed for this rule. These evaluations are done in an arbitrary order. The subprogram_body
is then executed, or a call on an entry or protected subprogram is performed (see 3.9.2). Finally, if the subprogram completes normally, then after it is left, any necessary assigning back of formal to actual parameters occurs (see 6.4.1).
subprogram_body
that is executed by the above rule is the one for the subprogram being called. For an enumeration literal, implicitly declared (but noninherited) subprogram, null procedure, or an attribute that is a subprogram, an implicit body is assumed. For a dispatching call, 3.9.2, “Dispatching Operations of Tagged Types” defines which subprogram_body
is executed.
The exception Program_Error is raised at the point of a function_call
if the function completes normally without executing a return statement.
A function_call
denotes a constant, as defined in 6.5; the nominal subtype of the constant is given by the nominal subtype of the function result.
Examples
13Examples of procedure calls:
Traverse_Tree; -- see 6.1
Print_Header(128, Title, True); -- see 6.1
15Switch(From => X, To => Next); -- see 6.1
Print_Header(128, Header => Title, Center => True); -- see 6.1
Print_Header(Header => Title, Center => True, Pages => 128); -- see 6.1
16Examples of function calls:
Dot_Product(U, V) -- see 6.1 and 6.3
Clock -- see 9.6
F.all -- presuming F is of an access-to-subprogram type — see 3.10
18Examples of procedures with default expressions:
procedure Activate(Process : in Process_Name;
After : in Process_Name := No_Process;
Wait : in Duration := 0.0;
Prior : in Boolean := False);
20/3procedure Pair(Left, Right : in Person_Name := new Person(M)); -- see 3.10.1
21Examples of their calls:
Activate(X);
Activate(X, After => Y);
Activate(X, Wait => 60.0, Prior => True);
Activate(X, Y, 10.0, False);
23/3Pair;
Pair(Left => new Person(F), Right => new Person(M));
default_expression
is used for two or more parameters in a multiple parameter_specification
, the default_expression
is evaluated once for each omitted parameter. Hence in the above examples, the two calls of Pair are equivalent. Examples
25Examples of overloaded subprograms:
procedure Put(X : in Integer);
procedure Put(X : in String);
27procedure Set(Tint : in Color);
procedure Set(Signal : in Light);
28Examples of their calls:
Put(28);
Put("no possible ambiguity here");
30Set(Tint => Red);
Set(Signal => Red);
Set(Color'(Red));
31/5-- Set(Red) would be ambiguous since Red can
-- denote a value either of type Color or of type Light
Wording Changes from Ada 83
type_conversion
s allows us to use normal type_conversion
s instead.Extensions to Ada 95
Wording Changes from Ada 95
function_call
to use the nominal subtype wording of 6.1, to take into account null_exclusion
s and access result types.Wording Changes from Ada 2012
prefix
). 6.4.1 Parameter Associations
1[ A parameter association defines the association between an actual parameter and a formal parameter.]
Language Design Principles
Name Resolution Rules
2/3The formal_parameter_selector_name
of a named parameter_association
shall resolve to denote a parameter_specification
of the view being called; this is the formal parameter of the association. The formal parameter for a positional parameter_association
is the parameter with the corresponding position in the formal part of the view being called.
The actual parameter is either the explicit_actual_parameter
given in a parameter_association
for a given formal parameter, or the corresponding default_expression
if no parameter_association
is given for the formal parameter. The expected type for an actual parameter is the type of the corresponding formal parameter.
default_expression
is the one of the corresponding formal parameter in the profile of the view denoted by the name
or prefix
of the call. If the mode is in, the actual is interpreted as an expression
; otherwise, the actual is interpreted only as a name
, if possible.
explicit_actual_parameter
. This matters as an expression
that is a name
is evaluated and represents a value while a name
by itself can be an object; if the mode is not in, we want the parameter to interpreted as an object. Note that we don't actually require that the actual be a name
if the mode is not in; we do that below.Legality Rules
5If the mode is in out or out, the actual shall be a name
that denotes a variable.
type_conversion
whose argument is the name
of a variable”, because a type_conversion
is now a name
, and a view conversion of a variable is a variable while any other conversion (which should not be legal here) is a constant. name
is not an overload resolution rule, since we don't want the difference between expression
and name
to be used to resolve overloading. For example: procedure Print(X : in Integer; Y : in Boolean := True);
procedure Print(Z : in out Integer);
. . .
Print(3); -- Ambiguous!
name
(“3” is an expression
, not a name
). This requirement is a legality rule, so overload resolution fails before it is considered, meaning that the call is ambiguous. If the mode is out, the actual parameter is a view conversion, and the type of the formal parameter is a scalar type , then
- neither the target type nor the operand type has the Default_Value aspect specified; or
- both the target type and the operand type shall have the Default_Value aspect specified, and there shall exist a type (other than a root numeric type) that is an ancestor of both the target type and the operand 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.
If the formal parameter is an explicitly aliased parameter, the type of the actual parameter shall be tagged or the actual parameter shall be an aliased view of an object. Further, if the formal parameter subtype F is untagged:
- the subtype F shall statically match the nominal subtype of the actual object; or
- the subtype F shall be unconstrained, discriminated in its full view, and unconstrained in any partial view.
aggregate
s for in parameters) do not need to be aliased. This matches the behavior of unaliased formal parameters of tagged types, which allow 'Access to be taken of the formal parameter regardless of the form of the actual parameter. 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.
In a function call, the accessibility level of the actual object for each explicitly aliased parameter shall not be statically deeper than the accessibility level of the master of the call (see 3.10.2).
allocator
; in almost all other cases this check will always pass. Two name
s are known to denote the same object if:
- both
name
s statically denote the same stand-alone object or parameter; or 6.7/3 - both
name
s areselected_component
s, theirprefix
es are known to denote the same object, and theirselector_name
s denote the same component; or 6.8/3 - both
name
s are dereferences (implicit or explicit) and the dereferencedname
s are known to denote the same object; or 6.9/3 - both
name
s areindexed_component
s, theirprefix
es are known to denote the same object, and each of the pairs of corresponding index values are either both static expressions with the same static value or bothname
s that are known to denote the same object; or 6.10/3 - both
name
s areslice
s, theirprefix
es are known to denote the same object, and the twoslice
s have statically matching index constraints; or 6.11/3 - one of the two
name
s statically denotes a renaming declaration whose renamed object_name
is known to denote the same object as the other, theprefix
of any dereference within the renamed object_name
is not a variable, and anyexpression
within the renamed object_name
contains no references to variables nor calls on nonstatic functions.
C : Character renames S(1);
prefix
es of dereferences and in (index) expression
s of the renamed object in order to avoid problems from later changes to those parts of renamed names. Consider:type Ref is access Some_Type;
Ptr : Ref := new Some_Type'(...);
X : Some_Type renames Ptr.all;
begin
Ptr := new Some_Type'(...);
P (Func_With_Out_Params (Ptr.all), X);
Global : Tagged_Type;
6.m/3procedure Foo (Param : in Tagged_Type := Global) is
X : Element renames Some_Global_Array (Param.C);
begin
Global.C := Global.C + 1;
Swap (X, Some_Global_Array (Param.C));
name
s or prefix
es are known to denote the same object is determined statically. If the name contains some dynamic portion other than a dereference, indexed_component
, or slice
, it is not "known to denote the same object".S : String(1..10);
ONE : constant Natural := 1;
R : Character renames S(1);
Two name
s are known to refer to the same object if
- The two
name
s are known to denote the same object; or 6.14/3 - One of the
name
s is aselected_component
,indexed_component
, orslice
and itsprefix
is known to refer to the same object as the othername
; or 6.15/3 - One of the two
name
s statically denotes a renaming declaration whose renamed object_name
is known to refer to the same object as the othername
.
If a call C has two or more parameters of mode in out or out that are of an elementary type, then the call is legal only if:
- For each
name
N denoting an object of an elementary type that is passed as a parameter of mode in out or out to the call C, there is no othername
among the other parameters of mode in out or out to C that is known to denote the same object.
If a construct C has two or more direct constituents that are name
s or expression
s whose evaluation may occur in an arbitrary order, at least one of which contains a function call with an in out or out parameter, then the construct is legal only if:
name
s or expression
s (such as checks or finalization). - For each name N that is passed as a parameter of mode in out or out to some inner function call C2 (not including the construct C itself), there is no other
name
anywhere within a direct constituent of the construct C other than the one containing C2, that is known to refer to the same object.
name
s or expression
s. Such dependence is usually a bug, and in any case, is not portable to another implementation (or even another optimization setting).For the purposes of checking this rule:
- For an array
aggregate
, anexpression
associated with adiscrete_choice_list
that has two or more discrete choices, or that has a nonstatic range, is considered as two or more separate occurrences of theexpression
; 6.22/3 - For a record
aggregate
:
- The
expression
of arecord_component_association
is considered to occur once for each associated component; and 6.24/3 - The
default_expression
for eachrecord_component_association
with <> for which the associated component has adefault_expression
is considered part of theaggregate
;
- For a call, any
default_expression
evaluated as part of the call is considered part of the call.
Dynamic Semantics
7For the evaluation of a parameter_association
:
- The actual parameter is first evaluated.
- For an access parameter, the
access_definition
is elaborated, which creates the anonymous access type. 10 - For a parameter [(of any mode)] that is passed by reference (see 6.2), a view conversion of the actual parameter to the nominal subtype of the formal parameter is evaluated, and the formal parameter denotes that conversion.
- For an in or in out parameter that is passed by copy (see 6.2), the formal parameter object is created, and the value of the actual parameter is converted to the nominal subtype of the formal parameter and assigned to the formal.
- For an out parameter that is passed by copy, the formal parameter object is created, and:
- For an access type, the formal parameter is initialized from the value of the actual, without checking whether the value satisfies any constraints, predicates, or null exclusions, but including any[ dynamic] accessibility checks associated with a conversion to the type of the formal parameter.
- For a scalar type that has the Default_Value aspect specified, the formal parameter is initialized from the value of the actual, without checking that the value satisfies any constraint or any predicate.
-
-
-
- For a composite type with discriminants or that has implicit initial values for any subcomponents (see 3.3.1), the behavior is as for an in out parameter passed by copy[, except that no predicate check is performed].
default_expression
, among other things. - For any other type, the formal parameter is uninitialized. If composite, a view conversion of the actual parameter to the nominal subtype of the formal is evaluated [(which can raise Constraint_Error)], and the actual subtype of the formal is that of the view conversion. If elementary, the actual subtype of the formal is given by its nominal subtype.
- Furthermore, if the type is a scalar type, and the actual parameter is a view conversion, then Program_Error is raised if either the target or the operand type has the Default_Value aspect specified, unless they both have the Default_Value aspect specified, and there is a type (other than a root numeric type) that is an ancestor of both the target type and the operand type.
- In a function call, for each explicitly aliased parameter, a check is made that the accessibility level of the master of the actual object is not deeper than that of the master of the call (see 3.10.2).
A formal parameter of mode in out or out with discriminants is constrained if either its nominal subtype or the actual parameter is constrained.
After normal completion and leaving of a subprogram, for each in out or out parameter that is passed by copy, the value of the formal parameter is converted to the subtype of the variable given as the actual parameter and assigned to it. These conversions and assignments occur in an arbitrary order.
subprogram_body
. subprogram_body
, the execution of the subprogram_body
does not dynamically enclose them, so it can't handle the exceptions. name
given as the explicit_actual_parameter
. If this variable_name
is a type_conversion
, then the rules in 4.6 for assigning to a view conversion apply. That is, if X is of subtype S1, and the actual is S2(X), the above-mentioned conversion will convert to S2, and the one mentioned in 4.6 will convert to S1. Erroneous Execution
18/3If the nominal subtype of a formal parameter with discriminants is constrained or indefinite, and the parameter is passed by reference, then the execution of the call is erroneous if the value of any discriminant of the actual is changed while the formal parameter exists (that is, before leaving the corresponding callable construct).
Implementation Permissions
19/5If the actual parameter in a parameter_association
with mode out is a view conversion between two access types that do not share a common ancestor type, the implementation may pass in the null value of the type of the formal parameter instead of the value of the actual parameter. It is implementation-defined under what circumstances the implementation passes in the null value.