From 21b0ecb9853071f0642ee1fba7861e69789d0727 Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Mon, 3 Apr 2023 10:53:30 +0200 Subject: ada: Fix wrong finalization for call to BIP function in conditional expression This happens when the call is a dependent expression of the conditional expression, and the conditional expression is either the expression of a simple return statement or the return expression of an expression function. The reason is that the special processing of "tail calls" for BIP functions, i.e. calls that are the expression of simple return statements or the return expression of expression functions, is not applied. This change makes sure that it is applied by distributing the simple return statements enclosing conditional expressions into the dependent expressions of the conditional expressions in almost all cases. As a side effect, this elides a temporary in the nonlimited by-reference case, as well as a pair of calls to Adjust/Finalize in the nonlimited controlled case. gcc/ada/ * exp_ch4.adb (Expand_N_Case_Expression): Distribute simple return statements enclosing the conditional expression into the dependent expressions in almost all cases. (Expand_N_If_Expression): Likewise. (Process_Transient_In_Expression): Adjust to the above distribution. * exp_ch6.adb (Expand_Ctrl_Function_Call): Deal with calls in the dependent expressions of a conditional expression. * sem_ch6.adb (Analyze_Function_Return): Deal with the rewriting of a simple return statement during the resolution of its expression. --- gcc/ada/sem_ch6.adb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'gcc/ada/sem_ch6.adb') diff --git a/gcc/ada/sem_ch6.adb b/gcc/ada/sem_ch6.adb index c58a548..495e8b1 100644 --- a/gcc/ada/sem_ch6.adb +++ b/gcc/ada/sem_ch6.adb @@ -857,6 +857,14 @@ package body Sem_Ch6 is end if; Resolve (Expr, R_Type); + + -- The expansion of the expression may have rewritten the return + -- statement itself, e.g. when it is a conditional expression. + + if Nkind (N) /= N_Simple_Return_Statement then + return; + end if; + Check_Limited_Return (N, Expr, R_Type); Check_Return_Construct_Accessibility (N, Stm_Entity); @@ -952,9 +960,7 @@ package body Sem_Ch6 is -- Defend against previous errors - if Nkind (Expr) = N_Empty - or else No (Etype (Expr)) - then + if Nkind (Expr) = N_Empty or else No (Etype (Expr)) then return; end if; -- cgit v1.1