From c901877facf9635149ed69cabd88c871f60931fe Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Thu, 26 May 2022 11:06:01 +0200 Subject: [Ada] Call-initialize all controlled objects in place This changes the compiler to build in place almost all objects that need finalization and are initialized with the result of a function call, thus saving a pair of Adjust/Finalize calls for the anonymous return object. gcc/ada/ * exp_ch3.adb (Expand_N_Object_Declaration): Don't adjust the object if the expression is a function call. : Return true if the object needs finalization and is initialized with the result of a function call returned on the secondary stack. * exp_ch6.adb (Expand_Ctrl_Function_Call): Add Use_Sec_Stack boolean parameter. Early return if the parent is an object declaration and Use_Sec_Stack is false. (Expand_Call_Helper): Adjust call to Expand_Ctrl_Function_Call. * exp_ch7.adb (Find_Last_Init): Be prepared for initialization still present in the object declaration. * sem_ch3.adb (Analyze_Object_Declaration): Call the predicates Needs_Secondary_Stack and Needs_Finalization to guard the renaming optimization. --- gcc/ada/sem_ch3.adb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'gcc/ada/sem_ch3.adb') diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb index 2dbba15..3bbb788 100644 --- a/gcc/ada/sem_ch3.adb +++ b/gcc/ada/sem_ch3.adb @@ -5046,21 +5046,21 @@ package body Sem_Ch3 is end if; -- Another optimization: if the nominal subtype is unconstrained and - -- the expression is a function call that returns an unconstrained - -- type, rewrite the declaration as a renaming of the result of the + -- the expression is a function call that returns on the secondary + -- stack, rewrite the declaration as a renaming of the result of the -- call. The exceptions below are cases where the copy is expected, -- either by the back end (Aliased case) or by the semantics, as for -- initializing controlled types or copying tags for class-wide types. + -- ??? To be moved to Expand_N_Object_Declaration.Rewrite_As_Renaming. if Present (E) and then Nkind (E) = N_Explicit_Dereference and then Nkind (Original_Node (E)) = N_Function_Call and then not Is_Library_Level_Entity (Id) - and then not Is_Constrained (Underlying_Type (T)) and then not Is_Aliased (Id) + and then Needs_Secondary_Stack (T) and then not Is_Class_Wide_Type (T) - and then not Is_Controlled (T) - and then not Has_Controlled_Component (Base_Type (T)) + and then not Needs_Finalization (T) and then Expander_Active then Rewrite (N, -- cgit v1.1