[COMMITTED,20/26] ada: Errors on legal container aggregates with iterated_element_associations

Message ID 20240802071210.413366-20-poulhies@adacore.com
State Committed
Commit 32c4a7278fd182af126c4f37228e467a3e4db568
Headers
Series [COMMITTED,01/26] ada: Fix detection of suspicious loop patterns |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Marc Poulhiès Aug. 2, 2024, 7:11 a.m. UTC
  From: Gary Dismukes <dismukes@adacore.com>

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.

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

---
 gcc/ada/exp_aggr.adb             |  1 +
 gcc/ada/gen_il-gen-gen_nodes.adb |  1 +
 gcc/ada/par-ch3.adb              |  4 ----
 gcc/ada/par-ch4.adb              | 12 +++++++++++-
 gcc/ada/par-ch5.adb              | 12 ------------
 gcc/ada/sem_aggr.adb             |  2 +-
 gcc/ada/sinfo.ads                |  6 ++++++
 7 files changed, 20 insertions(+), 18 deletions(-)
  

Patch

diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb
index ed0dad1444b..25af78a4da7 100644
--- a/gcc/ada/exp_aggr.adb
+++ b/gcc/ada/exp_aggr.adb
@@ -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;
diff --git a/gcc/ada/gen_il-gen-gen_nodes.adb b/gcc/ada/gen_il-gen-gen_nodes.adb
index 7224556accd..327ff376d12 100644
--- a/gcc/ada/gen_il-gen-gen_nodes.adb
+++ b/gcc/ada/gen_il-gen-gen_nodes.adb
@@ -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)));
 
diff --git a/gcc/ada/par-ch3.adb b/gcc/ada/par-ch3.adb
index 01dd45c4f23..a5f4319debf 100644
--- a/gcc/ada/par-ch3.adb
+++ b/gcc/ada/par-ch3.adb
@@ -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
diff --git a/gcc/ada/par-ch4.adb b/gcc/ada/par-ch4.adb
index 9c9b0d70207..8b491c2cfd7 100644
--- a/gcc/ada/par-ch4.adb
+++ b/gcc/ada/par-ch4.adb
@@ -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);
diff --git a/gcc/ada/par-ch5.adb b/gcc/ada/par-ch5.adb
index 6de9ef0089e..a245fa1c08b 100644
--- a/gcc/ada/par-ch5.adb
+++ b/gcc/ada/par-ch5.adb
@@ -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;
diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb
index 5f7c7321f4f..565f2cb8a79 100644
--- a/gcc/ada/sem_aggr.adb
+++ b/gcc/ada/sem_aggr.adb
@@ -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
diff --git a/gcc/ada/sinfo.ads b/gcc/ada/sinfo.ads
index 742527fcedb..beecc2c253f 100644
--- a/gcc/ada/sinfo.ads
+++ b/gcc/ada/sinfo.ads
@@ -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