[COMMITTED] ada: Fix resolution of mod operator of System.Storage_Elements

Message ID 20230523080845.1874226-1-poulhies@adacore.com
State Unresolved
Headers
Series [COMMITTED] ada: Fix resolution of mod operator of System.Storage_Elements |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Marc Poulhiès May 23, 2023, 8:08 a.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

This operator is special because the left operand is of Address type while
the right operand and the result are of Storage_Offset type (but we raise
Constraint_Error if the right operand is not positive) and it needs to be
resolved to the type of the left operand to implement the correct semantics.

gcc/ada/

	* exp_ch4.adb (Expand_N_Op_Mod): Adjust the detection of the special
	operator of System.Storage_Elements.  Do not rewrite it into a rem.
	* sem_res.adb (Resolve_Intrinsic_Operator): Use the base type of the
	left operand for the special mod operator of System.Storage_Elements

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

---
 gcc/ada/exp_ch4.adb | 11 +++++++----
 gcc/ada/sem_res.adb | 23 +++++++++++++++++++----
 2 files changed, 26 insertions(+), 8 deletions(-)
  

Patch

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index c974a9e8d44..c7727904df2 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -9561,9 +9561,10 @@  package body Exp_Ch4 is
       DDC   : constant Boolean    := Do_Division_Check (N);
 
       Is_Stoele_Mod : constant Boolean :=
-        Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
-          and then Nkind (Left_Opnd (N)) = N_Unchecked_Type_Conversion
-          and then Is_RTE (Etype (Expression (Left_Opnd (N))), RE_Address);
+        Is_RTE (Typ, RE_Address)
+          and then Nkind (Right_Opnd (N)) = N_Unchecked_Type_Conversion
+          and then
+            Is_RTE (Etype (Expression (Right_Opnd (N))), RE_Storage_Offset);
       --  True if this is the special mod operator of System.Storage_Elements
 
       Left  : Node_Id;
@@ -9633,6 +9634,7 @@  package body Exp_Ch4 is
         and then ((Llo >= 0 and then Rlo >= 0)
                      or else
                   (Lhi <= 0 and then Rhi <= 0))
+        and then not Is_Stoele_Mod
       then
          Rewrite (N,
            Make_Op_Rem (Sloc (N),
@@ -9683,7 +9685,8 @@  package body Exp_Ch4 is
               Make_Raise_Constraint_Error (Loc,
                 Condition =>
                   Make_Op_Le (Loc,
-                    Left_Opnd  => Duplicate_Subexpr_No_Checks (Right),
+                    Left_Opnd  =>
+                      Duplicate_Subexpr_No_Checks (Expression (Right)),
                     Right_Opnd => Make_Integer_Literal (Loc, 0)),
                 Reason => CE_Overflow_Check_Failed));
             return;
diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb
index a99bed00118..f1d9a97452a 100644
--- a/gcc/ada/sem_res.adb
+++ b/gcc/ada/sem_res.adb
@@ -9710,10 +9710,19 @@  package body Sem_Res is
    --------------------------------
 
    procedure Resolve_Intrinsic_Operator  (N : Node_Id; Typ : Entity_Id) is
-      Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
-      Op   : Entity_Id;
-      Arg1 : Node_Id;
-      Arg2 : Node_Id;
+      Is_Stoele_Mod : constant Boolean :=
+        Nkind (N) = N_Op_Mod
+          and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
+          and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
+      --  True if this is the special mod operator of System.Storage_Elements,
+      --  which needs to be resolved to the type of the left operand in order
+      --  to implement the correct semantics.
+
+      Btyp : constant Entity_Id :=
+        (if Is_Stoele_Mod
+          then Implementation_Base_Type (Etype (Left_Opnd (N)))
+          else Implementation_Base_Type (Typ));
+      --  The base type to be used for the operator
 
       function Convert_Operand (Opnd : Node_Id) return Node_Id;
       --  If the operand is a literal, it cannot be the expression in a
@@ -9742,6 +9751,12 @@  package body Sem_Res is
          return Res;
       end Convert_Operand;
 
+      --  Local variables
+
+      Arg1 : Node_Id;
+      Arg2 : Node_Id;
+      Op   : Entity_Id;
+
    --  Start of processing for Resolve_Intrinsic_Operator
 
    begin