]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Errors on legal container aggregates with iterated_element_associations
authorGary Dismukes <dismukes@adacore.com>
Mon, 15 Jul 2024 23:57:43 +0000 (23:57 +0000)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 2 Aug 2024 07:08:09 +0000 (09:08 +0200)
The compiler rejects various cases of container aggregates with
iterated_element_associations that include a loop_parameter_subtype_indication
or that include the "reverse" keyword. The fixes are in the parser, for
naccepting the syntax for these cases, as well as for properly accounting
for reverse iterators in the analyzer and expander.

gcc/ada/

* exp_aggr.adb
(Expand_Container_Aggregate.Expand_Iterated_Component): Set the
Reverse_Present flag when creating the loop's iteration_scheme.
* gen_il-gen-gen_nodes.adb: Add flag Reverse_Present to
N_Iterated_Component_Association nodes.
* par-ch3.adb (P_Constraint_Op): Remove testing for and ignoring
of Tok_In following a constraint. It's allowed for "in" to follow
a constraint of loop_parameter_subtype_indication of an
iterator_specification, so it shouldn't be ignored.
* par-ch4.adb (P_Iterated_Component_Association): Account for
"reverse" following the "in" in an iterated_component_association,
and set the Reverse_Present flag on the
N_Iterated_Component_Association node. Add handling for a ":"
following the identifier in an iterator_specification of an
iterated_element_association, sharing the code with the "of" case
(which backs up to the identifier at the beginning of the
iterator_specification). Fix incorrect trailing comment following
the call to Scan.
(Build_Iterated_Element_Association): Set the Reverse_Present flag
on an N_Loop_Parameter_Specification node of an
N_Iterated_Element_Association.
* par-ch5.adb (P_Iterator_Specification): Remove error-recovery
and error code that reports "subtype indication is only legal on
an element iterator", as that error can no longer be emitted (and
was formerly only reported on one fixedbugs test).
* sem_aggr.adb
(Resolve_Container_Aggregate.Resolve_Iterated_Association): When
creating an N_Iterator_Specification for an
N_Iterated_Component_Association, set the Reverse_Present flag of
the N_Iterated_Specification from the flag on the latter.
* sinfo.ads: Add comments for the Reverse_Present flag, which is
now allowed on nodes of kind N_Iterated_Component_Association.

gcc/ada/exp_aggr.adb
gcc/ada/gen_il-gen-gen_nodes.adb
gcc/ada/par-ch3.adb
gcc/ada/par-ch4.adb
gcc/ada/par-ch5.adb
gcc/ada/sem_aggr.adb
gcc/ada/sinfo.ads

index ed0dad1444b3c12a133d330dfe002081e0c92dab..25af78a4da7baa910a619de307d26a971e36d292 100644 (file)
@@ -7019,6 +7019,7 @@ package body Exp_Aggr is
                    Loop_Parameter_Specification =>
                      Make_Loop_Parameter_Specification (Loc,
                        Defining_Identifier => Loop_Id,
+                       Reverse_Present => Reverse_Present (Comp),
                        Discrete_Subtype_Definition => L_Range));
             end if;
          end if;
index 7224556accd78767bb95a4f3d6b843e0cfd10112..327ff376d12a760379df4c3e22b9441a63a3f5ac 100644 (file)
@@ -1489,6 +1489,7 @@ begin -- Gen_IL.Gen.Gen_Nodes
         Sy (Iterator_Specification, Node_Id, Default_Empty),
         Sy (Expression, Node_Id, Default_Empty),
         Sy (Discrete_Choices, List_Id),
+        Sy (Reverse_Present, Flag),
         Sy (Box_Present, Flag),
         Sm (Loop_Actions, List_Id)));
 
index 01dd45c4f235e5fe2defd2a5a9a2ae1cedc6b2f9..a5f4319debf6a14236ba8cd6e16856282e2531c1 100644 (file)
@@ -1196,10 +1196,6 @@ package body Ch3 is
       elsif Token = Tok_Left_Paren then
          return P_Index_Or_Discriminant_Constraint;
 
-      elsif Token = Tok_In then
-         Ignore (Tok_In);
-         return P_Constraint_Opt;
-
       --  One more possibility is e.g. 1 .. 10 (i.e. missing RANGE keyword)
 
       elsif Token = Tok_Identifier      or else
index 9c9b0d70207c92910b76bd3a2a5e8843d51fe2bf..8b491c2cfd79b441b4dfcdfba88ad62e421b92c2 100644 (file)
@@ -3584,6 +3584,7 @@ package body Ch4 is
       Iter_Spec  : Node_Id;
       Loop_Spec  : Node_Id;
       State      : Saved_Scan_State;
+      In_Reverse : Boolean := False;
 
       procedure Build_Iterated_Element_Association;
       --  If the iterator includes a key expression or a filter, it is
@@ -3602,6 +3603,8 @@ package body Ch4 is
            New_Node (N_Loop_Parameter_Specification, Prev_Token_Ptr);
          Set_Defining_Identifier (Loop_Spec, Id);
 
+         Set_Reverse_Present (Loop_Spec, In_Reverse);
+
          Choice := First (Discrete_Choices (Assoc_Node));
          Assoc_Node :=
            New_Node (N_Iterated_Element_Association, Prev_Token_Ptr);
@@ -3644,6 +3647,13 @@ package body Ch4 is
          when Tok_In =>
             Set_Defining_Identifier (Assoc_Node, Id);
             T_In;
+
+            if Token = Tok_Reverse then
+               Scan; -- past REVERSE
+               Set_Reverse_Present (Assoc_Node, True);
+               In_Reverse := True;
+            end if;
+
             Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
 
             --  The iterator may include a filter
@@ -3673,7 +3683,7 @@ package body Ch4 is
             TF_Arrow;
             Set_Expression (Assoc_Node, P_Expression);
 
-         when Tok_Of =>
+         when Tok_Colon | Tok_Of =>
             Restore_Scan_State (State);
             Scan;  -- past OF
             Iter_Spec := P_Iterator_Specification (Id);
index 6de9ef0089e95d75992c0551ac573530c55b8b4b..a245fa1c08b38a6a12b8a4cfbd84781affb39a77 100644 (file)
@@ -1836,18 +1836,6 @@ package body Ch5 is
       elsif Token = Tok_In then
          Scan;  --  past IN
 
-      elsif Prev_Token = Tok_In
-        and then Present (Subtype_Indication (Node1))
-      then
-         --  Simplest recovery is to transform it into an element iterator.
-         --  Error message on 'in" has already been emitted when parsing the
-         --  optional constraint.
-
-         Set_Of_Present (Node1);
-         Error_Msg_N
-           ("subtype indication is only legal on an element iterator",
-            Subtype_Indication (Node1));
-
       else
          return Error;
       end if;
index 5f7c7321f4f658d647602da3c1c9814b9654f785..565f2cb8a794f3fa7b992c7e543c50e1fbce7459 100644 (file)
@@ -3818,7 +3818,7 @@ package body Sem_Aggr is
                       Defining_Identifier =>
                         Relocate_Node (Defining_Identifier (Comp)),
                       Name                => Copy,
-                      Reverse_Present     => False,
+                      Reverse_Present     => Reverse_Present (Comp),
                       Iterator_Filter     => Empty,
                       Subtype_Indication  => Empty);
                begin
index 742527fcedb95fa3ebba093e159c57f83a6d2de6..beecc2c253ffc866471b33340ccfb8643480f1b0 100644 (file)
@@ -4203,6 +4203,11 @@ package Sinfo is
 
       --  At most one of (Defining_Identifier, Iterator_Specification)
       --  is present at a time, in which case the other one is empty.
+      --  The Reverse_Present flag is present for cases where semantic analysis
+      --  later changes the association to have an N_Iterator_Specification
+      --  rather than a Defining_Identifier (due to the "discrete choice"
+      --  being resolved as an iterator name), and needs to set that flag on
+      --  the N_Iterator_Specification node.
 
       --  N_Iterated_Component_Association
       --  Sloc points to FOR
@@ -4210,6 +4215,7 @@ package Sinfo is
       --  Iterator_Specification
       --  Expression
       --  Discrete_Choices
+      --  Reverse_Present
       --  Loop_Actions
       --  Box_Present