From: Eric Botcazou <ebotca...@adacore.com>

This node is used in a single place in the front-end: it wraps the newly
built N_Indexed_Component nodes on the left-hand side of assignments
generated to elaborate array aggregates, and its effect is to disable
range checks for the expressions of these nodes.

Most of the code in the front-end does not expect to encounter it at all,
which leads to weird effects when this actually happens after changes are
made to the processing of array aggregates.

This change replaces the node by the Kill_Range_Check flag already present
on N_Unchecked_Type_Conversion, but with a slightly adjusted semantics.

gcc/ada/ChangeLog:

        * exp_aggr.adb (Build_Array_Aggr_Code.Gen_Assign): Do not call
        Checks_Off on the newly built N_Indexed_Component node but instead
        set Kill_Range_Check on it.
        * exp_ch4.ads (Expand_N_Unchecked_Expression): Delete.
        * exp_ch4.adb (Expand_N_Indexed_Component): Remove handling of
        N_Unchecked_Expression.
        (Expand_N_Unchecked_Expression): Delete.
        (Expand_N_Unchecked_Type_Conversion): Propagate the Assignment_OK
        flag and rewrite the node manually.
        * exp_util.adb (Insert_Actions): Remove handling of
        N_Unchecked_Expression.
        (Side_Effect_Free): Likewise.
        * expander.adb (Expand): Likewise.
        * gen_il-gen-gen_nodes.adb (N_Indexed_Component): Add flag
        Kill_Range_Check for the purpose of semantics.
        (N_Unchecked_Expression): Delete.
        * gen_il-internals.ads (Type_Frequency): Remove entry for
        N_Unchecked_Expression.
        * gen_il-types.ads (Opt_Type_Enum): Remove N_Unchecked_Expression.
        * pprint.adb (Expression_Image): Remove handling of
        N_Unchecked_Expression.
        * sem.adb (Analyze): Likewise.
        * sem_ch4.ads (Analyze_Unchecked_Expression): Delete.
        * sem_ch4.adb (Analyze_Unchecked_Expression): Likewise.
        * sem_res.adb (Resolve_Unchecked_Expression): Likewise.
        (Resolve): Remove handling of N_Unchecked_Expression.
        (Resolve_Indexed_Component): Do not call Apply_Scalar_Range_Check
        on the expressions if Kill_Range_Check is set on the node.
        * sem_util.adb (Is_Non_Preelaborable_Construct): Remove handling of
        N_Unchecked_Expression.
        * sinfo.ads (Kill_Range_Check): Document it for N_Indexed_Component.
        (Unchecked Expression): Delete specification.
        * sprint.adb (Sprint_Node_Actual): Remove handling of
        N_Unchecked_Expression.
        * tbuild.ads (Checks_Off): Delete.
        * tbuild.adb (Checks_Off): Likewise.

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

---
 gcc/ada/exp_aggr.adb             |  8 ++++----
 gcc/ada/exp_ch4.adb              | 27 ++++++--------------------
 gcc/ada/exp_ch4.ads              |  1 -
 gcc/ada/exp_util.adb             |  2 --
 gcc/ada/expander.adb             |  3 ---
 gcc/ada/gen_il-gen-gen_nodes.adb |  6 ++----
 gcc/ada/gen_il-internals.ads     |  1 -
 gcc/ada/gen_il-types.ads         |  1 -
 gcc/ada/pprint.adb               |  4 +---
 gcc/ada/sem.adb                  |  3 ---
 gcc/ada/sem_ch4.adb              | 13 -------------
 gcc/ada/sem_ch4.ads              |  1 -
 gcc/ada/sem_res.adb              | 21 +++-----------------
 gcc/ada/sem_util.adb             |  1 -
 gcc/ada/sinfo.ads                | 33 +++++++-------------------------
 gcc/ada/sprint.adb               |  6 ------
 gcc/ada/tbuild.adb               | 11 -----------
 gcc/ada/tbuild.ads               |  6 ------
 18 files changed, 23 insertions(+), 125 deletions(-)

diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb
index 8231e4006b1..d5e238baa59 100644
--- a/gcc/ada/exp_aggr.adb
+++ b/gcc/ada/exp_aggr.adb
@@ -1329,12 +1329,12 @@ package body Exp_Aggr is
          --  If we get here then we are at a bottom-level (sub-)aggregate
 
          Indexed_Comp :=
-           Checks_Off
-             (Make_Indexed_Component (Loc,
-                Prefix      => New_Copy_Tree (Into),
-                Expressions => New_Indexes));
+           Make_Indexed_Component (Loc,
+             Prefix      => New_Copy_Tree (Into),
+             Expressions => New_Indexes);
 
          Set_Assignment_OK (Indexed_Comp);
+         Set_Kill_Range_Check (Indexed_Comp);
 
          --  Ada 2005 (AI-287): In case of default initialized component, Expr
          --  is not present (and therefore we also initialize Expr_Q to empty).
diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index c16e09d9562..9e82b78e3b6 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -7307,10 +7307,7 @@ package body Exp_Ch4 is
 
       begin
          loop
-            if Nkind (Parnt) = N_Unchecked_Expression then
-               null;
-
-            elsif Nkind (Parnt) = N_Object_Renaming_Declaration then
+            if Nkind (Parnt) = N_Object_Renaming_Declaration then
                return;
 
             elsif Nkind (Parnt) in N_Subprogram_Call
@@ -12116,22 +12113,6 @@ package body Exp_Ch4 is
       end if;
    end Expand_N_Type_Conversion;
 
-   -----------------------------------
-   -- Expand_N_Unchecked_Expression --
-   -----------------------------------
-
-   --  Remove the unchecked expression node from the tree. Its job was simply
-   --  to make sure that its constituent expression was handled with checks
-   --  off, and now that is done, we can remove it from the tree, and indeed
-   --  must, since Gigi does not expect to see these nodes.
-
-   procedure Expand_N_Unchecked_Expression (N : Node_Id) is
-      Exp : constant Node_Id := Expression (N);
-   begin
-      Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
-      Rewrite (N, Exp);
-   end Expand_N_Unchecked_Expression;
-
    ----------------------------------------
    -- Expand_N_Unchecked_Type_Conversion --
    ----------------------------------------
@@ -12150,7 +12131,11 @@ package body Exp_Ch4 is
       --  an Assignment_OK indication which must be propagated to the operand.
 
       if Operand_Type = Target_Type then
-         Expand_N_Unchecked_Expression (N);
+         if Assignment_OK (N) then
+            Set_Assignment_OK (Operand);
+         end if;
+
+         Rewrite (N, Operand);
          return;
       end if;
 
diff --git a/gcc/ada/exp_ch4.ads b/gcc/ada/exp_ch4.ads
index 7bc777ea2ec..22ffdc6496d 100644
--- a/gcc/ada/exp_ch4.ads
+++ b/gcc/ada/exp_ch4.ads
@@ -71,7 +71,6 @@ package Exp_Ch4 is
    procedure Expand_N_Selected_Component          (N : Node_Id);
    procedure Expand_N_Slice                       (N : Node_Id);
    procedure Expand_N_Type_Conversion             (N : Node_Id);
-   procedure Expand_N_Unchecked_Expression        (N : Node_Id);
    procedure Expand_N_Unchecked_Type_Conversion   (N : Node_Id);
 
    function Build_Eq_Call
diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index df108918a74..767191060d8 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -8340,7 +8340,6 @@ package body Exp_Util is
                | N_Terminate_Alternative
                | N_Triggering_Alternative
                | N_Type_Conversion
-               | N_Unchecked_Expression
                | N_Unchecked_Type_Conversion
                | N_Unconstrained_Array_Definition
                | N_Unused_At_End
@@ -14259,7 +14258,6 @@ package body Exp_Util is
 
          when N_Qualified_Expression
             | N_Type_Conversion
-            | N_Unchecked_Expression
          =>
             return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
 
diff --git a/gcc/ada/expander.adb b/gcc/ada/expander.adb
index 39b311fc136..05dcbc0ea80 100644
--- a/gcc/ada/expander.adb
+++ b/gcc/ada/expander.adb
@@ -506,9 +506,6 @@ package body Expander is
                when N_Type_Conversion =>
                   Expand_N_Type_Conversion (N);
 
-               when N_Unchecked_Expression =>
-                  Expand_N_Unchecked_Expression (N);
-
                when N_Unchecked_Type_Conversion =>
                   Expand_N_Unchecked_Type_Conversion (N);
 
diff --git a/gcc/ada/gen_il-gen-gen_nodes.adb b/gcc/ada/gen_il-gen-gen_nodes.adb
index e0e0538c5f0..b322f3ca81c 100644
--- a/gcc/ada/gen_il-gen-gen_nodes.adb
+++ b/gcc/ada/gen_il-gen-gen_nodes.adb
@@ -478,7 +478,8 @@ begin -- Gen_IL.Gen.Gen_Nodes
        (Sy (Prefix, Node_Id),
         Sy (Expressions, List_Id, Default_No_List),
         Sm (Atomic_Sync_Required, Flag),
-        Sm (Generalized_Indexing, Node_Id)));
+        Sm (Generalized_Indexing, Node_Id),
+        Sm (Kill_Range_Check, Flag)));
 
    Cc (N_Null, N_Subexpr);
 
@@ -575,9 +576,6 @@ begin -- Gen_IL.Gen.Gen_Nodes
         Sm (Float_Truncate, Flag),
         Sm (Rounded_Result, Flag)));
 
-   Cc (N_Unchecked_Expression, N_Subexpr,
-       (Sy (Expression, Node_Id, Default_Empty)));
-
    Cc (N_Unchecked_Type_Conversion, N_Subexpr,
        (Sy (Subtype_Mark, Node_Id, Default_Empty),
         Sy (Expression, Node_Id, Default_Empty),
diff --git a/gcc/ada/gen_il-internals.ads b/gcc/ada/gen_il-internals.ads
index 31e81c1feb5..46d38d0baaa 100644
--- a/gcc/ada/gen_il-internals.ads
+++ b/gcc/ada/gen_il-internals.ads
@@ -458,7 +458,6 @@ package Gen_IL.Internals is
       E_Generic_Function => 1292, -- (0.000) 41 slots
       E_Enumeration_Type => 1186, -- (0.000) 47 slots
       N_Enumeration_Type_Definition => 1169, -- (0.000) 6 slots
-      N_Unchecked_Expression => 1112, -- (0.000) 7 slots
       N_Op_Or => 1107, -- (0.000) 8 slots
       N_Designator => 1100, -- (0.000) 9 slots
       N_Formal_Discrete_Type_Definition => 1086, -- (0.000) 4 slots
diff --git a/gcc/ada/gen_il-types.ads b/gcc/ada/gen_il-types.ads
index 4a739043faa..66e9b695ce0 100644
--- a/gcc/ada/gen_il-types.ads
+++ b/gcc/ada/gen_il-types.ads
@@ -271,7 +271,6 @@ package Gen_IL.Types is
       N_Slice,
       N_Target_Name,
       N_Type_Conversion,
-      N_Unchecked_Expression,
       N_Unchecked_Type_Conversion,
       N_Subtype_Indication,
       N_Component_Declaration,
diff --git a/gcc/ada/pprint.adb b/gcc/ada/pprint.adb
index fbf0e59239a..b5935fcb0b4 100644
--- a/gcc/ada/pprint.adb
+++ b/gcc/ada/pprint.adb
@@ -424,9 +424,7 @@ package body Pprint is
                   end if;
                end;
 
-            when N_Expression_With_Actions
-               | N_Unchecked_Expression
-            =>
+            when N_Expression_With_Actions =>
                return Expr_Name (Expression (Expr));
 
             when N_Raise_Constraint_Error =>
diff --git a/gcc/ada/sem.adb b/gcc/ada/sem.adb
index cfd04935274..c6d65ea713a 100644
--- a/gcc/ada/sem.adb
+++ b/gcc/ada/sem.adb
@@ -613,9 +613,6 @@ package body Sem is
          when N_Type_Conversion =>
             Analyze_Type_Conversion (N);
 
-         when N_Unchecked_Expression =>
-            Analyze_Unchecked_Expression (N);
-
          when N_Unchecked_Type_Conversion =>
             Analyze_Unchecked_Type_Conversion (N);
 
diff --git a/gcc/ada/sem_ch4.adb b/gcc/ada/sem_ch4.adb
index c1f6622db1e..ad18e824457 100644
--- a/gcc/ada/sem_ch4.adb
+++ b/gcc/ada/sem_ch4.adb
@@ -6488,19 +6488,6 @@ package body Sem_Ch4 is
       Operator_Check (N);
    end Analyze_Unary_Op;
 
-   ----------------------------------
-   -- Analyze_Unchecked_Expression --
-   ----------------------------------
-
-   procedure Analyze_Unchecked_Expression (N : Node_Id) is
-      Expr : constant Node_Id := Expression (N);
-
-   begin
-      Analyze (Expr, Suppress => All_Checks);
-      Set_Etype (N, Etype (Expr));
-      Save_Interps (Expr, N);
-   end Analyze_Unchecked_Expression;
-
    ---------------------------------------
    -- Analyze_Unchecked_Type_Conversion --
    ---------------------------------------
diff --git a/gcc/ada/sem_ch4.ads b/gcc/ada/sem_ch4.ads
index dbe0f9a73da..574613dc38b 100644
--- a/gcc/ada/sem_ch4.ads
+++ b/gcc/ada/sem_ch4.ads
@@ -50,7 +50,6 @@ package Sem_Ch4  is
    procedure Analyze_Slice                     (N : Node_Id);
    procedure Analyze_Type_Conversion           (N : Node_Id);
    procedure Analyze_Unary_Op                  (N : Node_Id);
-   procedure Analyze_Unchecked_Expression      (N : Node_Id);
    procedure Analyze_Unchecked_Type_Conversion (N : Node_Id);
 
    procedure Ambiguous_Operands (N : Node_Id);
diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb
index 658f9eb2b72..d2b019aef17 100644
--- a/gcc/ada/sem_res.adb
+++ b/gcc/ada/sem_res.adb
@@ -242,7 +242,6 @@ package body Sem_Res is
    procedure Resolve_Target_Name               (N : Node_Id; Typ : Entity_Id);
    procedure Resolve_Type_Conversion           (N : Node_Id; Typ : Entity_Id);
    procedure Resolve_Unary_Op                  (N : Node_Id; Typ : Entity_Id);
-   procedure Resolve_Unchecked_Expression      (N : Node_Id; Typ : Entity_Id);
    procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
 
    function Operator_Kind
@@ -3534,9 +3533,6 @@ package body Sem_Res is
             when N_Type_Conversion =>
                Resolve_Type_Conversion           (N, Ctx_Type);
 
-            when N_Unchecked_Expression =>
-               Resolve_Unchecked_Expression      (N, Ctx_Type);
-
             when N_Unchecked_Type_Conversion =>
                Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
          end case;
@@ -9730,7 +9726,9 @@ package body Sem_Res is
             Resolve (Expr, Etype (Index));
             Check_Unset_Reference (Expr);
 
-            Apply_Scalar_Range_Check (Expr, Etype (Index));
+            if not Kill_Range_Check (N) then
+               Apply_Scalar_Range_Check (Expr, Etype (Index));
+            end if;
 
             Next_Index (Index);
             Next (Expr);
@@ -12871,19 +12869,6 @@ package body Sem_Res is
       end;
    end Resolve_Unary_Op;
 
-   ----------------------------------
-   -- Resolve_Unchecked_Expression --
-   ----------------------------------
-
-   procedure Resolve_Unchecked_Expression
-     (N   : Node_Id;
-      Typ : Entity_Id)
-   is
-   begin
-      Resolve (Expression (N), Typ, Suppress => All_Checks);
-      Set_Etype (N, Typ);
-   end Resolve_Unchecked_Expression;
-
    ---------------------------------------
    -- Resolve_Unchecked_Type_Conversion --
    ---------------------------------------
diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 25f164f8736..b1b3891dd65 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -18483,7 +18483,6 @@ package body Sem_Util is
             when N_Allocator
                | N_Qualified_Expression
                | N_Type_Conversion
-               | N_Unchecked_Expression
                | N_Unchecked_Type_Conversion
             =>
                --  Subpool_Handle_Name and Subtype_Mark are left out because
diff --git a/gcc/ada/sinfo.ads b/gcc/ada/sinfo.ads
index 7ed6fad6709..09385e95586 100644
--- a/gcc/ada/sinfo.ads
+++ b/gcc/ada/sinfo.ads
@@ -1909,9 +1909,11 @@ package Sinfo is
    --    for further details.
 
    --  Kill_Range_Check
-   --    Used in an N_Unchecked_Type_Conversion node to indicate that the
-   --    result should not be subjected to range checks. This is used for the
-   --    implementation of Normalize_Scalars.
+   --    Used in N_Indexed_Component to indicate that its expressions should
+   --    not be subjected to range checks and in N_Unchecked_Type_Conversion
+   --    to indicate that the result of the conversion should not be subjected
+   --    to range checks. This is used for the implementation of aggregates and
+   --    Normalize_Scalars respectively.
 
    --  Label_Construct
    --    Used in an N_Implicit_Label_Declaration node. Refers to an N_Label,
@@ -3830,8 +3832,9 @@ package Sinfo is
       --  Sloc contains a copy of the Sloc value of the Prefix
       --  Prefix
       --  Expressions
-      --  Generalized_Indexing
       --  Atomic_Sync_Required
+      --  Generalized_Indexing
+      --  Kill_Range_Check
       --  plus fields for expression
 
       --  Note: if any of the subscripts requires a range check, then the
@@ -8428,28 +8431,6 @@ package Sinfo is
       --  with the N_In node (or a rewriting thereof) corresponding to a
       --  classwide membership test.
 
-      --------------------------
-      -- Unchecked Expression --
-      --------------------------
-
-      --  An unchecked expression is one that must be analyzed and resolved
-      --  with all checks off, regardless of the current setting of scope
-      --  suppress flags.
-
-      --  Sprint syntax: `(expression)
-
-      --  Note: this node is always removed from the tree (and replaced by
-      --  its constituent expression) on completion of analysis, so it only
-      --  appears in intermediate trees, and will never be seen by Gigi.
-
-      --  N_Unchecked_Expression
-      --  Sloc is a copy of the Sloc of the expression
-      --  Expression
-      --  plus fields for expression
-
-      --  Note: in the case where a debug source file is generated, the Sloc
-      --  for this node points to the back quote in the Sprint file output.
-
       -------------------------------
       -- Unchecked Type Conversion --
       -------------------------------
diff --git a/gcc/ada/sprint.adb b/gcc/ada/sprint.adb
index 321fd7f99a9..614bcc17b14 100644
--- a/gcc/ada/sprint.adb
+++ b/gcc/ada/sprint.adb
@@ -3550,12 +3550,6 @@ package body Sprint is
             Sprint_Node (Expression (Node));
             Write_Char (')');
 
-         when N_Unchecked_Expression =>
-            Col_Check (10);
-            Write_Str ("`(");
-            Sprint_Node_Sloc (Expression (Node));
-            Write_Char (')');
-
          when N_Unchecked_Type_Conversion =>
             Sprint_Node (Subtype_Mark (Node));
             Write_Char ('!');
diff --git a/gcc/ada/tbuild.adb b/gcc/ada/tbuild.adb
index b538911e8bc..31154266bcb 100644
--- a/gcc/ada/tbuild.adb
+++ b/gcc/ada/tbuild.adb
@@ -99,17 +99,6 @@ package body Tbuild is
       end if;
    end Add_Unique_Serial_Number;
 
-   ----------------
-   -- Checks_Off --
-   ----------------
-
-   function Checks_Off (N : Node_Id) return Node_Id is
-   begin
-      return
-        Make_Unchecked_Expression (Sloc (N),
-          Expression => N);
-   end Checks_Off;
-
    ----------------
    -- Convert_To --
    ----------------
diff --git a/gcc/ada/tbuild.ads b/gcc/ada/tbuild.ads
index 64296e68c18..e818bcd0021 100644
--- a/gcc/ada/tbuild.ads
+++ b/gcc/ada/tbuild.ads
@@ -34,12 +34,6 @@ with Uintp;          use Uintp;
 
 package Tbuild is
 
-   function Checks_Off (N : Node_Id) return Node_Id;
-   pragma Inline (Checks_Off);
-   --  Returns an N_Unchecked_Expression node whose expression is the given
-   --  argument. The results is a subexpression identical to the argument,
-   --  except that it will be analyzed and resolved with checks off.
-
    function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id;
    --  Returns an expression that is a type conversion of expression Expr to
    --  type Typ. If the type of Expr is Typ, then no conversion is required.
-- 
2.43.0

Reply via email to