aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorEric Botcazou <ebotcazou@adacore.com>2022-12-10 00:51:10 +0100
committerMarc Poulhiès <poulhies@adacore.com>2023-01-03 10:29:54 +0100
commitde77a81b2c974520183e6c2f205be54844f3d42e (patch)
treef2f542240a495e9d8495ada7fddc0e17ab16e7be /gcc
parentd3f50f75aa3e5054b28074c029d36c1cbce2d0cb (diff)
downloadgcc-de77a81b2c974520183e6c2f205be54844f3d42e.zip
gcc-de77a81b2c974520183e6c2f205be54844f3d42e.tar.gz
gcc-de77a81b2c974520183e6c2f205be54844f3d42e.tar.bz2
ada: Fix premature finalization of return temporary
Various parts of the expander and the code generator must have a consistent view on which temporaries generated for return statements must be finalized because they are regular temporaries, and which ones must not be since they are allocated on the return stack directly. The Is_Related_To_Func_Return predicate is used for this purpose and needs to be tested consistently. gcc/ada/ * exp_ch6.adb (Expand_Simple_Function_Return): Make sure that a captured function call also verifies Is_Related_To_Func_Return. Do not generate an actual subtype for special return objects. * exp_util.ads (Is_Related_To_Func_Return): Add commentary.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ada/exp_ch6.adb16
-rw-r--r--gcc/ada/exp_util.ads4
2 files changed, 18 insertions, 2 deletions
diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb
index d90ee41..db1fd1d 100644
--- a/gcc/ada/exp_ch6.adb
+++ b/gcc/ada/exp_ch6.adb
@@ -6435,12 +6435,21 @@ package body Exp_Ch6 is
-- The result type of the function
Utyp : constant Entity_Id := Underlying_Type (R_Type);
+ -- The underlying result type of the function
Exp : Node_Id := Expression (N);
pragma Assert (Present (Exp));
Exp_Is_Function_Call : constant Boolean :=
- Nkind (Exp) = N_Function_Call or else Is_Captured_Function_Call (Exp);
+ Nkind (Exp) = N_Function_Call
+ or else
+ (Is_Captured_Function_Call (Exp)
+ and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
+ -- If the expression is a captured function call, then we need to make
+ -- sure that the object doing the capture is properly recognized by the
+ -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
+ -- that needs finalization, Requires_Cleanup_Actions would return true
+ -- because of it and Build_Finalizer would finalize it prematurely.
Exp_Typ : constant Entity_Id := Etype (Exp);
-- The type of the expression (not necessarily the same as R_Type)
@@ -6624,7 +6633,8 @@ package body Exp_Ch6 is
-- size. We create an actual subtype for this purpose. However we
-- need not do it if the expression is a function call since this
-- will be done in the called function and doing it here too would
- -- cause a temporary with maximum size to be created.
+ -- cause a temporary with maximum size to be created. Likewise for
+ -- a special return object, since there is no copy in this case.
declare
Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
@@ -6633,6 +6643,8 @@ package body Exp_Ch6 is
begin
if not Exp_Is_Function_Call
+ and then not (Is_Entity_Name (Exp)
+ and then Is_Special_Return_Object (Entity (Exp)))
and then Has_Defaulted_Discriminants (Ubt)
and then not Is_Constrained (Ubt)
and then not Has_Unchecked_Union (Ubt)
diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads
index 0d09d25..b770d02 100644
--- a/gcc/ada/exp_util.ads
+++ b/gcc/ada/exp_util.ads
@@ -825,6 +825,10 @@ package Exp_Util is
-- Determine whether object Id is related to an expanded return statement.
-- The case concerned is "return Id.all;".
+ -- This is effectively used to determine which temporaries generated for
+ -- return statements must be finalized because they are regular temporaries
+ -- and which ones must not be since they are allocated on the return stack.
+
-- WARNING: There is a matching C declaration of this subprogram in fe.h
function Is_Renamed_Object (N : Node_Id) return Boolean;