[Ada] Use Actions field of freeze nodes for subprograms

Message ID 20220517082731.GA1087940@adacore.com
State Committed
Headers
Series [Ada] Use Actions field of freeze nodes for subprograms |

Commit Message

Pierre-Marie de Rodat May 17, 2022, 8:27 a.m. UTC
  This has a couple of advantages: 1) the actions are analyzed with checks
disabled and 2) they are considered elaboration code by Sem_Elab.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* exp_ch13.adb (Expand_N_Freeze_Entity): Delete freeze nodes for
	subprograms only if they have no actions.
	* exp_ch6.adb (Freeze_Subprogram): Put the actions into the Actions
	field of the freeze node instead of inserting them after it.
	* sem_elab.adb (Is_SPARK_Semantic_Target): Fix typo in comment.
	* gcc-interface/trans.cc (process_freeze_entity): Return early for
	freeze nodes of subprograms with Interface_Alias set.
  

Patch

diff --git a/gcc/ada/exp_ch13.adb b/gcc/ada/exp_ch13.adb
--- a/gcc/ada/exp_ch13.adb
+++ b/gcc/ada/exp_ch13.adb
@@ -617,14 +617,12 @@  package body Exp_Ch13 is
       elsif Is_Subprogram (E) then
          Exp_Ch6.Freeze_Subprogram (N);
 
-         --  Ada 2005 (AI-251): Remove the freezing node associated with the
-         --  entities internally used by the frontend to register primitives
-         --  covering abstract interfaces. The call to Freeze_Subprogram has
-         --  already expanded the code that fills the corresponding entry in
-         --  its secondary dispatch table and therefore the code generator
-         --  has nothing else to do with this freezing node.
-
-         Delete := Present (Interface_Alias (E));
+         --  Ada 2005 (AI-251): Remove the freeze nodes associated with the
+         --  entities internally used by the front end to register primitives
+         --  covering abstract interfaces if they have no side effects. For the
+         --  others, gigi must discard them after evaluating the side effects.
+
+         Delete := Present (Interface_Alias (E)) and then No (Actions (N));
       end if;
 
       --  Analyze actions generated by freezing. The init_proc contains source


diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb
--- a/gcc/ada/exp_ch6.adb
+++ b/gcc/ada/exp_ch6.adb
@@ -7856,6 +7856,8 @@  package body Exp_Ch6 is
          declare
             Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
 
+            L : List_Id;
+
          begin
             --  Handle private overridden primitives
 
@@ -7895,8 +7897,17 @@  package body Exp_Ch6 is
                      Register_Predefined_DT_Entry (Subp);
                   end if;
 
-                  Insert_Actions_After (N,
-                    Register_Primitive (Loc, Prim => Subp));
+                  L := Register_Primitive (Loc, Prim => Subp);
+
+                  if Is_Empty_List (L) then
+                     null;
+
+                  elsif No (Actions (N)) then
+                     Set_Actions (N, L);
+
+                  else
+                     Append_List (L, Actions (N));
+                  end if;
                end if;
             end if;
          end;


diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc
--- a/gcc/ada/gcc-interface/trans.cc
+++ b/gcc/ada/gcc-interface/trans.cc
@@ -9045,6 +9045,11 @@  process_freeze_entity (Node_Id gnat_node)
   if (kind == E_Class_Wide_Type)
     return;
 
+  /* Likewise for the entities internally used by the front-end to register
+     primitives covering abstract interfaces, see Expand_N_Freeze_Entity.  */
+  if (Is_Subprogram (gnat_entity) && Present (Interface_Alias (gnat_entity)))
+    return;
+
   /* Check for an old definition if this isn't an object with address clause,
      since the saved GCC tree is the address expression in that case.  */
   gnu_old


diff --git a/gcc/ada/sem_elab.adb b/gcc/ada/sem_elab.adb
--- a/gcc/ada/sem_elab.adb
+++ b/gcc/ada/sem_elab.adb
@@ -1845,7 +1845,7 @@  package body Sem_Elab is
 
       function Is_SPARK_Semantic_Target (Id : Entity_Id) return Boolean;
       pragma Inline (Is_SPARK_Semantic_Target);
-      --  Determine whether arbitrary entity Id nodes a source or internally
+      --  Determine whether arbitrary entity Id denotes a source or internally
       --  generated subprogram which emulates SPARK semantics.
 
       function Is_Subprogram_Inst (Id : Entity_Id) return Boolean;