diff options
author | Gary Dismukes <dismukes@adacore.com> | 2024-08-12 22:50:57 +0000 |
---|---|---|
committer | Marc Poulhiès <dkm@gcc.gnu.org> | 2024-08-29 15:06:26 +0200 |
commit | a383d7bb732a38649a46696a2c4386d825054050 (patch) | |
tree | 158d33247b142dff30dccef73539cffb0d8770d2 /gcc | |
parent | 567e36ccbae11857413ab6cd7d50856173d16405 (diff) | |
download | gcc-a383d7bb732a38649a46696a2c4386d825054050.zip gcc-a383d7bb732a38649a46696a2c4386d825054050.tar.gz gcc-a383d7bb732a38649a46696a2c4386d825054050.tar.bz2 |
ada: Proper handling for iterator associations in array aggregates
The compiler was flagging type-mismatch errors on iterated component
associations in array aggregates of form "for C in <iterator_name>",
improperly requiring the type of the iterator to be the array index
type. The parser can't distinguish whether the association is one
involving an actual discrete choice vs. an iterator specification,
and creates an N_Iterated_Component_Association with a Defining_Identifer
and Discrete_Choices, and the analysis phase has to disambiguate this,
determining whether to create an N_Iterator_Specification node for
the association. A related change is to revise the similar code for
iterated associations of container aggregates, to allow forms of
iterator objects other than just function calls.
gcc/ada/
* sem_aggr.adb (Resolve_Array_Aggregate): Add loop over associations to locate
N_Iterated_Component_Associations that do not have an Iterator_Specification,
and if their Discrete_Choices list consists of a single choice, analyze it and
if it's the name of an iterator object, then create an Iterator_Specification
and associate it with the iterated component association.
(Resolve_Iterated_Association): Replace test for function call with test of
Is_Object_Reference, to handle other forms of iterator objects in container
aggregates.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ada/sem_aggr.adb | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb index 087e324..8319ff5 100644 --- a/gcc/ada/sem_aggr.adb +++ b/gcc/ada/sem_aggr.adb @@ -2542,6 +2542,66 @@ package body Sem_Aggr is null; elsif Present (Component_Associations (N)) then + Assoc := First (Component_Associations (N)); + + -- Loop over associations to identify any iterated associations that + -- need to be converted from the form with a Defining_Identifer and + -- Discrete_Choices list to the form with an Iterator_Specification. + + if Nkind (Assoc) = N_Iterated_Component_Association then + while Present (Assoc) loop + if Nkind (Assoc) = N_Iterated_Component_Association + and then No (Iterator_Specification (Assoc)) + then + declare + Choice : constant Node_Id := + First (Discrete_Choices (Assoc)); + Copy : Node_Id; + begin + + -- A copy of Choice is made before it's analyzed, + -- to preserve prefixed calls in their original form, + -- because otherwise the analysis of Choice can transform + -- such calls to normal form, and the later analysis of + -- the iterator_specification created below may trigger + -- an error on the call (in the case where the function + -- is not directly visible). + + Copy := Copy_Separate_Tree (Choice); + + -- This is an association with a Defining_Identifier and + -- Discrete_Choice_List, but if the latter has a single + -- choice denoting an object (including a function call) + -- of an iterator type, then it's a stand-in for an + -- Iterator_Specification, and so we transform the + -- association accordingly. + + if No (Next (Choice)) then + Analyze (Choice); + + if Is_Object_Reference (Choice) + and then Is_Iterator (Etype (Choice)) + then + Set_Iterator_Specification + (Assoc, + Make_Iterator_Specification (Sloc (N), + Defining_Identifier => + Relocate_Node (Defining_Identifier (Assoc)), + Name => Copy, + Reverse_Present => Reverse_Present (Assoc), + Iterator_Filter => Empty, + Subtype_Indication => Empty)); + + Set_Defining_Identifier (Assoc, Empty); + Set_Discrete_Choices (Assoc, No_List); + end if; + end if; + end; + end if; + + Next (Assoc); + end loop; + end if; -- Verify that all or none of the component associations -- include an iterator specification. @@ -3814,7 +3874,7 @@ package body Sem_Aggr is then null; - elsif Nkind (Choice) = N_Function_Call then + elsif Is_Object_Reference (Choice) then declare I_Spec : constant Node_Id := Make_Iterator_Specification (Sloc (N), |