This patch ensures that the front-end generates constraint checks for
some operations that previously depended on gigi for the corresponding
check. The patch also resets the Do_Range_Check flag so that it never
appears in the tree presented to gigi.

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

2018-09-26  Ed Schonberg  <schonb...@adacore.com>

gcc/ada/

        * checks.adb (Apply_Type_Conversion_Checks): Do not generate a
        range check on the expression of the conversion if it involves a
        fixed-point type, as such conversions are handled specially
        during expansion.
        * exp_ch4.adb (Expand_N_Type_Conversion): In a conversion from
        Fixed to Integer, use the base type of the expression to ensure
        that the caller will generate the proper constraint check when
        needed.
--- gcc/ada/checks.adb
+++ gcc/ada/checks.adb
@@ -3550,8 +3550,21 @@ package body Checks is
                   Apply_Float_Conversion_Check (Expr, Target_Type);
 
                else
-                  Apply_Scalar_Range_Check
-                    (Expr, Target_Type, Fixed_Int => Conv_OK);
+                  --  Conversions involving fixed-point types are expanded
+                  --  separately, and do not need a Range_Check flag, except
+                  --  in SPARK_Mode, where the explicit constraint check will
+                  --  not be generated.
+
+                  if SPARK_Mode = On
+                    or else (not Is_Fixed_Point_Type (Expr_Type)
+                              and then not Is_Fixed_Point_Type (Target_Type))
+                  then
+                     Apply_Scalar_Range_Check
+                       (Expr, Target_Type, Fixed_Int => Conv_OK);
+
+                  else
+                     Set_Do_Range_Check (Expression (N), False);
+                  end if;
 
                   --  If the target type has predicates, we need to indicate
                   --  the need for a check, even if Determine_Range finds that

--- gcc/ada/exp_ch4.adb
+++ gcc/ada/exp_ch4.adb
@@ -11694,6 +11694,11 @@ package body Exp_Ch4 is
             elsif Is_Integer_Type (Etype (N)) then
                Expand_Convert_Fixed_To_Integer (N);
 
+               --  The result of the conversion might need a range check,
+               --   so do not assume that the result is in bounds.
+
+               Set_Etype (N, Base_Type (Target_Type));
+
             else
                pragma Assert (Is_Floating_Point_Type (Etype (N)));
                Expand_Convert_Fixed_To_Float (N);

Reply via email to