]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix spurious error on iterated component association with large index type
authorEric Botcazou <ebotcazou@adacore.com>
Wed, 30 Oct 2024 10:22:12 +0000 (11:22 +0100)
committerEric Botcazou <ebotcazou@adacore.com>
Tue, 12 Nov 2024 17:50:19 +0000 (18:50 +0100)
This is only for the Ada 2022 form of the iterated component association.

gcc/ada/ChangeLog:

PR ada/117328
* exp_aggr.adb (Two_Pass_Aggregate_Expansion): Use a type sized
from the index type to compute the length.  Simplify and remove
useless calls to New_Copy_Tree for this computation.

gcc/ada/exp_aggr.adb

index a20ca7bb9389ca7fd66c4a0ad438525afc0c5e99..26b279701e9f3d1c225421fe90fc821a2366739a 100644 (file)
@@ -5705,6 +5705,9 @@ package body Exp_Aggr is
          Index_Id   : constant Entity_Id := Make_Temporary (Loc, 'I', N);
          Index_Type : constant Entity_Id := Etype (First_Index (Etype (N)));
          Size_Id    : constant Entity_Id := Make_Temporary (Loc, 'I', N);
+         Size_Type  : constant Entity_Id :=
+                        Integer_Type_For
+                          (Esize (Index_Type), Is_Unsigned_Type (Index_Type));
          TmpE       : constant Entity_Id := Make_Temporary (Loc, 'A', N);
 
          Assoc    : Node_Id := First (Component_Associations (N));
@@ -5720,7 +5723,7 @@ package body Exp_Aggr is
          Size_Expr_Code := New_List (
            Make_Object_Declaration (Loc,
              Defining_Identifier => Size_Id,
-             Object_Definition   => New_Occurrence_Of (Standard_Integer, Loc),
+             Object_Definition   => New_Occurrence_Of (Size_Type, Loc),
              Expression          => Make_Integer_Literal (Loc, 0)));
 
          --  First pass: execute the iterators to count the number of elements
@@ -5747,32 +5750,35 @@ package body Exp_Aggr is
 
          Insert_Actions (N, Size_Expr_Code);
 
-         --  Build a constrained subtype with the calculated length
-         --  and declare the proper bounded aggregate object.
+         --  Build a constrained subtype with the bounds deduced from
+         --  the size computed above and declare the aggregate object.
          --  The index type is some discrete type, so the bounds of the
-         --  constructed array are computed as T'Val (T'Pos (ineger bound));
+         --  constrained subtype are computed as T'Val (integer bounds).
 
          declare
+            --  Pos_Lo := Index_Type'Pos (Index_Type'First)
+
             Pos_Lo : constant Node_Id :=
               Make_Attribute_Reference (Loc,
-                Prefix => New_Occurrence_Of (Index_Type, Loc),
+                Prefix         => New_Occurrence_Of (Index_Type, Loc),
                 Attribute_Name => Name_Pos,
-                Expressions => New_List (
+                Expressions    => New_List (
                   Make_Attribute_Reference (Loc,
                     Prefix => New_Occurrence_Of (Index_Type, Loc),
                     Attribute_Name => Name_First)));
 
+            --  Corresponding index value, i.e. Index_Type'First
+
             Aggr_Lo : constant Node_Id :=
                Make_Attribute_Reference (Loc,
-                 Prefix => New_Occurrence_Of (Index_Type, Loc),
-                 Attribute_Name => Name_Val,
-                 Expressions => New_List (New_Copy_Tree (Pos_Lo)));
+                 Prefix         => New_Occurrence_Of (Index_Type, Loc),
+                 Attribute_Name => Name_First);
 
-            --  Hi = Index_type'Pos (Lo + Size -1).
+            --  Pos_Hi := Pos_Lo + Size - 1
 
             Pos_Hi : constant Node_Id :=
                Make_Op_Add (Loc,
-                 Left_Opnd => New_Copy_Tree (Pos_Lo),
+                 Left_Opnd  => Pos_Lo,
                  Right_Opnd =>
                    Make_Op_Subtract (Loc,
                      Left_Opnd  => New_Occurrence_Of (Size_Id, Loc),
@@ -5784,7 +5790,7 @@ package body Exp_Aggr is
                Make_Attribute_Reference (Loc,
                  Prefix => New_Occurrence_Of (Index_Type, Loc),
                  Attribute_Name => Name_Val,
-                 Expressions => New_List (New_Copy_Tree (Pos_Hi)));
+                 Expressions    => New_List (Pos_Hi));
 
             SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
             SubD : constant Node_Id :=
@@ -5807,6 +5813,7 @@ package body Exp_Aggr is
                  Make_Object_Declaration (Loc,
                    Defining_Identifier => TmpE,
                    Object_Definition   => New_Occurrence_Of (SubE, Loc));
+
          begin
             Insert_Actions (N, New_List (SubD, TmpD));
          end;