[COMMITTED] ada: Another couple of cleanups in the finalization machinery

Message ID 20231121100056.1965191-1-poulhies@adacore.com
State Committed
Commit 5a6dbb34c521e79ced35ce778bc86b39f3d348a7
Headers
Series [COMMITTED] ada: Another couple of cleanups in the finalization machinery |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged

Commit Message

Marc Poulhiès Nov. 21, 2023, 10 a.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

For package specs and bodies that need finalizers, Build_Finalizer is
invoked from the Standard scope so it needs to adjust the scope stack
before creating new objects; this changes it to do so only once.

For other kinds of scopes, it is invoked from Expand_Cleanup_Actions,
which assumes that the correct scope is already on the stack; that's
why Cleanup_Scopes adjusts the scope stack explicitly, but it should
use Pop_Scope instead of End_Scope to do it.

gcc/ada/

	* exp_ch7.adb (Build_Finalizer): For package specs and bodies, push
	and pop the specs onto the scope stack only once.
	* inline.adb (Cleanup_Scopes): Call Pop_Scope instead of End_Scope.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch7.adb | 29 +++++++++++++----------------
 gcc/ada/inline.adb  |  2 +-
 2 files changed, 14 insertions(+), 17 deletions(-)
  

Patch

diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb
index ef3b5c95d64..f8c12b73e9b 100644
--- a/gcc/ada/exp_ch7.adb
+++ b/gcc/ada/exp_ch7.adb
@@ -1575,19 +1575,10 @@  package body Exp_Ch7 is
             Prepend_To (Decls, Counter_Typ_Decl);
 
             --  The counter and its associated type must be manually analyzed
-            --  since N has already been analyzed. Use the scope of the spec
-            --  when inserting in a package.
+            --  since N has already been analyzed.
 
-            if For_Package then
-               Push_Scope (Spec_Id);
-               Analyze (Counter_Typ_Decl);
-               Analyze (Counter_Decl);
-               Pop_Scope;
-
-            else
-               Analyze (Counter_Typ_Decl);
-               Analyze (Counter_Decl);
-            end if;
+            Analyze (Counter_Typ_Decl);
+            Analyze (Counter_Decl);
 
             Jump_Alts := New_List;
          end if;
@@ -1933,12 +1924,8 @@  package body Exp_Ch7 is
                Append_To (Decls, Fin_Body);
             end if;
 
-            --  Push the name of the package
-
-            Push_Scope (Spec_Id);
             Analyze (Fin_Spec);
             Analyze (Fin_Body);
-            Pop_Scope;
 
          --  Non-package case
 
@@ -3419,6 +3406,10 @@  package body Exp_Ch7 is
       --  Step 2: Object [pre]processing
 
       if For_Package then
+         --  For package specs and bodies, we are invoked from the Standard
+         --  scope, so we need to push the specs onto the scope stack first.
+
+         Push_Scope (Spec_Id);
 
          --  Preprocess the visible declarations now in order to obtain the
          --  correct number of controlled object by the time the private
@@ -3496,6 +3487,12 @@  package body Exp_Ch7 is
       if Acts_As_Clean or Has_Ctrl_Objs or Has_Tagged_Types then
          Create_Finalizer;
       end if;
+
+      --  Pop the scope that was pushed above for package specs and bodies
+
+      if For_Package then
+         Pop_Scope;
+      end if;
    end Build_Finalizer;
 
    --------------------------
diff --git a/gcc/ada/inline.adb b/gcc/ada/inline.adb
index 5fff88144b2..1fbbe6d32c2 100644
--- a/gcc/ada/inline.adb
+++ b/gcc/ada/inline.adb
@@ -2908,7 +2908,7 @@  package body Inline is
          else
             Push_Scope (Scop);
             Expand_Cleanup_Actions (Decl);
-            End_Scope;
+            Pop_Scope;
          end if;
 
          Next_Elmt (Elmt);