diff mbox series

[COMMITTED,34/35] ada: Reset scope of top level object declaration during unnesting

Message ID 20240516092606.41242-34-poulhies@adacore.com
State New
Headers show
Series [COMMITTED,01/35] ada: Fix docs and comments about pragmas for Boolean-valued aspects | expand

Commit Message

Marc Poulhiès May 16, 2024, 9:26 a.m. UTC
When unnesting, the compiler gathers elaboration code and wraps it with
a new dedicated procedure. While doing so, it resets the scopes of
entities that are wrapped to point to this new procedure. This change
also resets the scopes of N_Object_Declaration and
N_Object_Renaming_Declaration nodes only if an elaboration procedure
is needed.

gcc/ada/

	* exp_ch7.adb (Reset_Scopes_To_Block_Elab_Proc): also reset scope
	for object declarations.

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

---
 gcc/ada/exp_ch7.adb | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb
index 6d76572f405..f9738e115f9 100644
--- a/gcc/ada/exp_ch7.adb
+++ b/gcc/ada/exp_ch7.adb
@@ -3646,9 +3646,10 @@  package body Exp_Ch7 is
       --  unnesting actions, which depend on proper setting of the Scope links
       --  to determine the nesting level of each subprogram.
 
-      -----------------------
-      --  Find_Local_Scope --
-      -----------------------
+      --------------------------------------
+      --  Reset_Scopes_To_Block_Elab_Proc --
+      --------------------------------------
+      Maybe_Reset_Scopes_For_Decl : constant Elist_Id := New_Elmt_List;
 
       procedure Reset_Scopes_To_Block_Elab_Proc (L : List_Id) is
          Id   : Entity_Id;
@@ -3707,7 +3708,8 @@  package body Exp_Ch7 is
                      Next (Node);
                   end loop;
 
-               --  Reset the Scope of a subprogram occurring at the top level
+               --  Reset the Scope of a subprogram and object declaration
+               --  occurring at the top level
 
                when N_Subprogram_Body =>
                   Id := Defining_Entity (Stat);
@@ -3715,12 +3717,33 @@  package body Exp_Ch7 is
                   Set_Block_Elab_Proc;
                   Set_Scope (Id, Block_Elab_Proc);
 
+               when N_Object_Declaration
+                 | N_Object_Renaming_Declaration =>
+                  Id := Defining_Entity (Stat);
+                  if No (Block_Elab_Proc) then
+                     Append_Elmt (Id, Maybe_Reset_Scopes_For_Decl);
+                  else
+                     Set_Scope (Id, Block_Elab_Proc);
+                  end if;
+
                when others =>
                   null;
             end case;
 
             Next (Stat);
          end loop;
+
+         --  If we are creating an Elab procedure, move all the gathered
+         --  declarations in its scope.
+
+         if Present (Block_Elab_Proc) then
+            while not Is_Empty_Elmt_List (Maybe_Reset_Scopes_For_Decl) loop
+               Set_Scope
+                 (Elists.Node
+                   (Last_Elmt (Maybe_Reset_Scopes_For_Decl)), Block_Elab_Proc);
+               Remove_Last_Elmt (Maybe_Reset_Scopes_For_Decl);
+            end loop;
+         end if;
       end Reset_Scopes_To_Block_Elab_Proc;
 
       --  Local variables