Skip to content

Commit

Permalink
Merge branch 'topic/1050' into 'master'
Browse files Browse the repository at this point in the history
Support key_expression in iterated association of container aggregates

Closes #1050 and #1062

See merge request eng/libadalang/libadalang!1385
  • Loading branch information
thvnx committed Oct 2, 2023
2 parents 8e1f689 + 10befdf commit aa5e31e
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 38 deletions.
10 changes: 9 additions & 1 deletion ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16002,6 +16002,7 @@ class IteratedAssoc(BasicAssoc):
Iterated association (Ada 2020, :rmlink:`4.3.3`).
"""
spec = Field(type=T.ForLoopSpec)
key_expr = Field(type=T.Expr)
r_expr = Field(type=T.Expr)

expr = Property(Entity.r_expr)
Expand Down Expand Up @@ -16074,7 +16075,14 @@ def xref_equation():
# .. Then we want to match the component type
Entity.expr.sub_equation
& Bind(Entity.expr.expected_type_var, comp_type)
& Entity.expr.matches_expected_type,
& Entity.expr.matches_expected_type
# .. As well as the key expression if it exists
& Entity.key_expr.then(
lambda ke: ke.sub_equation
& Bind(ke.expected_type_var, index_type)
& ke.matches_expected_type,
default_val=LogicTrue()
),

# .. Else we're on an intermediate dimension of a
# multidimensional array: do nothing.
Expand Down
5 changes: 3 additions & 2 deletions ada/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,9 @@ def end_named_block():

iterated_assoc=IteratedAssoc(
"for", Cut(),
A.for_loop_param_spec, "=>",
A.expr
A.for_loop_param_spec,
Opt("use", A.expr),
"=>", A.expr
),

aggregate_assoc=Or(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ begin
-- !!! Make GNAT crash.

-- The above could have been written using an explicit key_expression:
-- M:= [for Key of Keys use Key => Integer'Image (Key)];
-- This example is not supported by LAL ([use key_expression] not parsed)
-- TODO: fix LAL, see libadalang#1050.
M:= [for Key of Keys use Key => Integer'Image (Key)];
pragma Test_Statement;

-- Example aggregates using Vector_Type

Expand Down
103 changes: 71 additions & 32 deletions testsuite/tests/name_resolution/container_aggregate/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -533,122 +533,161 @@ Expr: <Str ""beige"" ai12_0212.adb:144:31-144:38>
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>

Resolving xrefs for node <AssignStmt ai12_0212.adb:166:4-166:12>
Resolving xrefs for node <AssignStmt ai12_0212.adb:159:4-159:57>
****************************************************************

Expr: <Id "V" ai12_0212.adb:166:4-166:5>
Expr: <Id "M" ai12_0212.adb:159:4-159:5>
references: <DefiningName "M" ai12_0212.adb:102:4-102:5>
type: <ConcreteTypeDecl ["Map_Type"] ai12_0212.adb:24:7-26:55>
expected type: None
Expr: <BracketAggregate ai12_0212.adb:159:8-159:56>
type: <ConcreteTypeDecl ["Map_Type"] ai12_0212.adb:24:7-26:55>
expected type: <ConcreteTypeDecl ["Map_Type"] ai12_0212.adb:24:7-26:55>
Expr: <Id "Keys" ai12_0212.adb:159:20-159:24>
references: <DefiningName "Keys" ai12_0212.adb:116:4-116:8>
type: <AnonymousTypeDecl ["None"] ai12_0212.adb:116:20-116:56>
expected type: None
Expr: <Id "Key" ai12_0212.adb:159:29-159:32>
references: <DefiningName "Key" ai12_0212.adb:159:13-159:16>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <CallExpr ai12_0212.adb:159:36-159:55>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <AttributeRef ai12_0212.adb:159:36-159:49>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: None
expected type: None
Expr: <Id "Integer" ai12_0212.adb:159:36-159:43>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <Id "Image" ai12_0212.adb:159:44-159:49>
references: None
type: None
expected type: None
Expr: <Id "Key" ai12_0212.adb:159:51-159:54>
references: <DefiningName "Key" ai12_0212.adb:159:13-159:16>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <AssignStmt ai12_0212.adb:165:4-165:12>
****************************************************************

Expr: <Id "V" ai12_0212.adb:165:4-165:5>
references: <DefiningName "V" ai12_0212.adb:103:4-103:5>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: None
Expr: <BracketAggregate ai12_0212.adb:166:9-166:11>
Expr: <BracketAggregate ai12_0212.adb:165:9-165:11>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>

Resolving xrefs for node <AssignStmt ai12_0212.adb:170:4-170:24>
Resolving xrefs for node <AssignStmt ai12_0212.adb:169:4-169:24>
****************************************************************

Expr: <Id "V" ai12_0212.adb:170:4-170:5>
Expr: <Id "V" ai12_0212.adb:169:4-169:5>
references: <DefiningName "V" ai12_0212.adb:103:4-103:5>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: None
Expr: <BracketAggregate ai12_0212.adb:170:9-170:23>
Expr: <BracketAggregate ai12_0212.adb:169:9-169:23>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
Expr: <Str ""abc"" ai12_0212.adb:170:10-170:15>
Expr: <Str ""abc"" ai12_0212.adb:169:10-169:15>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <Str ""def"" ai12_0212.adb:170:17-170:22>
Expr: <Str ""def"" ai12_0212.adb:169:17-169:22>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>

Resolving xrefs for node <AssignStmt ai12_0212.adb:174:4-174:57>
Resolving xrefs for node <AssignStmt ai12_0212.adb:173:4-173:57>
****************************************************************

Expr: <Id "V" ai12_0212.adb:174:4-174:5>
Expr: <Id "V" ai12_0212.adb:173:4-173:5>
references: <DefiningName "V" ai12_0212.adb:103:4-103:5>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: None
Expr: <BracketAggregate ai12_0212.adb:174:9-174:56>
Expr: <BracketAggregate ai12_0212.adb:173:9-173:56>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
Expr: <Int ai12_0212.adb:174:10-174:11>
Expr: <Int ai12_0212.adb:173:10-173:11>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <Str ""this"" ai12_0212.adb:174:15-174:21>
Expr: <Str ""this"" ai12_0212.adb:173:15-173:21>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <Int ai12_0212.adb:174:23-174:24>
Expr: <Int ai12_0212.adb:173:23-173:24>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <Str ""is"" ai12_0212.adb:174:28-174:32>
Expr: <Str ""is"" ai12_0212.adb:173:28-173:32>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <Int ai12_0212.adb:174:34-174:35>
Expr: <Int ai12_0212.adb:173:34-173:35>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <Str ""a"" ai12_0212.adb:174:39-174:42>
Expr: <Str ""a"" ai12_0212.adb:173:39-173:42>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <Int ai12_0212.adb:174:44-174:45>
Expr: <Int ai12_0212.adb:173:44-173:45>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <Str ""test"" ai12_0212.adb:174:49-174:55>
Expr: <Str ""test"" ai12_0212.adb:173:49-173:55>
references: None
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>

Resolving xrefs for node <AssignStmt ai12_0212.adb:178:4-178:48>
Resolving xrefs for node <AssignStmt ai12_0212.adb:177:4-177:48>
****************************************************************

Expr: <Id "V" ai12_0212.adb:178:4-178:5>
Expr: <Id "V" ai12_0212.adb:177:4-177:5>
references: <DefiningName "V" ai12_0212.adb:103:4-103:5>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: None
Expr: <BracketAggregate ai12_0212.adb:178:9-178:47>
Expr: <BracketAggregate ai12_0212.adb:177:9-177:47>
type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
expected type: <ConcreteTypeDecl ["Vector_Type"] ai12_0212.adb:33:7-37:63>
Expr: <BinOp ai12_0212.adb:178:19-178:25>
Expr: <BinOp ai12_0212.adb:177:19-177:25>
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <Int ai12_0212.adb:178:19-178:20>
Expr: <Int ai12_0212.adb:177:19-177:20>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <OpDoubleDot ".." ai12_0212.adb:178:21-178:23>
Expr: <OpDoubleDot ".." ai12_0212.adb:177:21-177:23>
references: None
type: None
expected type: None
Expr: <Int ai12_0212.adb:178:24-178:25>
Expr: <Int ai12_0212.adb:177:24-177:25>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
Expr: <CallExpr ai12_0212.adb:178:29-178:46>
Expr: <CallExpr ai12_0212.adb:177:29-177:46>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
expected type: <ConcreteTypeDecl ["String"] __standard:105:3-105:57>
Expr: <AttributeRef ai12_0212.adb:178:29-178:42>
Expr: <AttributeRef ai12_0212.adb:177:29-177:42>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: None
expected type: None
Expr: <Id "Integer" ai12_0212.adb:178:29-178:36>
Expr: <Id "Integer" ai12_0212.adb:177:29-177:36>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <Id "Image" ai12_0212.adb:178:37-178:42>
Expr: <Id "Image" ai12_0212.adb:177:37-177:42>
references: None
type: None
expected type: None
Expr: <Id "I" ai12_0212.adb:178:44-178:45>
references: <DefiningName "I" ai12_0212.adb:178:14-178:15>
Expr: <Id "I" ai12_0212.adb:177:44-177:45>
references: <DefiningName "I" ai12_0212.adb:177:14-177:15>
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Expand Down
1 change: 1 addition & 0 deletions testsuite/tests/parser/iterated_assoc_key_expr/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
for Key of Keys use Key => Integer'Image (Key)
35 changes: 35 additions & 0 deletions testsuite/tests/parser/iterated_assoc_key_expr/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
IteratedAssoc[1:1-1:47]
|f_spec:
| ForLoopSpec[1:5-1:16]
| |f_var_decl:
| | ForLoopVarDecl[1:5-1:8]
| | |f_id:
| | | DefiningName[1:5-1:8]
| | | |f_name:
| | | | Id[1:5-1:8]: Key
| | |f_id_type: <null>
| |f_loop_type:
| | IterTypeOf[1:9-1:11]
| |f_has_reverse:
| | ReverseAbsent[1:11-1:11]
| |f_iter_expr:
| | Id[1:12-1:16]: Keys
| |f_iter_filter: <null>
|f_key_expr:
| Id[1:21-1:24]: Key
|f_r_expr:
| CallExpr[1:28-1:47]
| |f_name:
| | AttributeRef[1:28-1:41]
| | |f_prefix:
| | | Id[1:28-1:35]: Integer
| | |f_attribute:
| | | Id[1:36-1:41]: Image
| | |f_args:
| | | AssocList[1:41-1:41]: <empty list>
| |f_suffix:
| | AssocList[1:43-1:46]
| | | ParamAssoc[1:43-1:46]
| | | |f_designator: <null>
| | | |f_r_expr:
| | | | Id[1:43-1:46]: Key
2 changes: 2 additions & 0 deletions testsuite/tests/parser/iterated_assoc_key_expr/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: parser
rule: iterated_assoc
1 change: 1 addition & 0 deletions testsuite/tests/parser/reduce_attr_1/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ReduceAttributeRef[1:1-1:38]
| | | | |f_right:
| | | | | Id[1:16-1:17]: N
| | | |f_iter_filter: <null>
| | |f_key_expr: <null>
| | |f_r_expr:
| | | Id[1:21-1:22]: J
|f_attribute:
Expand Down
8 changes: 8 additions & 0 deletions user_manual/changes/libadalang/1050.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: new-feature
short_title: Support key expression in aggregates
title: Add support for key expression in container aggregates
description: |
Libadalang now handles key expression used in the iterated element association
of container aggregates. This change adds both parser and name resolution
support. Container aggregate is an Ada 2022 feature, see ARM 4.3.5.
date: 2023-08-11

0 comments on commit aa5e31e

Please sign in to comment.