Skip to content

Commit

Permalink
Merge branch 'topic/1034' into 'master'
Browse files Browse the repository at this point in the history
Fixed lower bounds for array types and subtypes (gnatX)

Closes #1034

See merge request eng/libadalang/libadalang!1456
  • Loading branch information
thvnx committed Dec 11, 2023
2 parents 2864709 + 808c6ef commit 709e248
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 13 deletions.
13 changes: 9 additions & 4 deletions ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9180,11 +9180,15 @@ def index_type(dim=Int):
@langkit_property()
def xref_equation():
return Entity.types.logic_all(
lambda typ: typ.subtype_indication.sub_equation
lambda typ:
typ.subtype_name.xref_no_overloading
& If(typ.lower_bound.is_null,
LogicTrue(),
typ.lower_bound.sub_equation)
)

is_static = Property(Entity.types.all(
lambda t: t.subtype_indication.is_static_subtype
lambda t: t.subtype_name.is_static_subtype
))


Expand Down Expand Up @@ -23219,11 +23223,12 @@ class UnconstrainedArrayIndex(AdaNode):
List of unconstrained array indexes.
"""

subtype_indication = Field(type=SubtypeIndication)
subtype_name = Field(type=Name)
lower_bound = Field(type=Expr)

@langkit_property(dynamic_vars=[origin])
def designated_type():
return Entity.subtype_indication.designated_type
return Entity.subtype_name.name_designated_type


class AbstractStateDecl(BasicDecl):
Expand Down
8 changes: 6 additions & 2 deletions ada/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def end_named_block():
),

unconstrained_index=UnconstrainedArrayIndex(
A.subtype_indication, "range", "<>"
A.subtype_name, "range", Opt(A.simple_expr, ".."), "<>"
),

array_type_def=ArrayTypeDef(
Expand Down Expand Up @@ -1426,7 +1426,11 @@ def end_named_block():
),

discrete_range=Or(
BinOp(A.simple_expr, Op.alt_double_dot(".."), A.simple_expr),
BinOp(
A.simple_expr,
Op.alt_double_dot(".."),
Or(A.simple_expr, A.box_expr)
),
Predicate(A.name, T.Name.is_range_attribute)
),

Expand Down
40 changes: 40 additions & 0 deletions testsuite/tests/name_resolution/fixed_lower_bound/test.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Check that the following experimental GNAT feature:
-- Fixed lower bounds for array types and subtypes
-- is correctly supported.

-- https://github.com/AdaCore/ada-spark-rfcs/blob/master/prototyped/rfc-fixed-lower-bound.rst
pragma Extensions_Allowed (On);

procedure Test is
type A is array (Integer range 1 .. <>) of Integer;
pragma Test_Block;

type String_1 is array (Positive range 1 .. <>) of Character;
pragma Test_Block;

subtype Fixed_String is String (1 .. <>);
pragma Test_Statement;

subtype Fixed_String_2 is String (Integer range 1 .. <>);
pragma Test_Statement;

type Int_Matrix_1 is array (Positive range 1 .. <>,
Positive range <>) of Integer;
pragma Test_Block;
type Int_Matrix_2 is array (Positive range <>,
Positive range 1 .. <>) of Integer;
pragma Test_Block;
type Int_Matrix_3 is array (Positive range 1 .. <>,
Positive range 1 .. <>) of Integer;
pragma Test_Block;

type Matrix is array (Positive range <>, Positive range <>) of Integer;
pragma Test_Block;

subtype SMatrix is Matrix (2 .. <>, 2 .. <>);
pragma Test_Statement;
subtype SMatrix2 is Matrix (Integer range 2 .. <>, Integer range 2 .. <>);
pragma Test_Statement;
begin
null;
end Test;
245 changes: 245 additions & 0 deletions testsuite/tests/name_resolution/fixed_lower_bound/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
Analyzing test.adb
##################

Resolving xrefs for node <ArrayTypeDef test.adb:9:14-9:54>
**********************************************************

Expr: <Id "Integer" test.adb:9:21-9:28>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <Int test.adb:9:35-9:36>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Integer" test.adb:9:47-9:54>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None

Resolving xrefs for node <ArrayTypeDef test.adb:12:21-12:64>
************************************************************

Expr: <Id "Positive" test.adb:12:28-12:36>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Int test.adb:12:43-12:44>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Character" test.adb:12:55-12:64>
references: <DefiningName "Character" __standard:21:8-21:17>
type: None
expected type: None

Resolving xrefs for node <SubtypeDecl ["Fixed_String"] test.adb:15:4-15:45>
***************************************************************************

Expr: <Id "String" test.adb:15:28-15:34>
references: <DefiningName "String" __standard:105:8-105:14>
type: None
expected type: None
Expr: <BinOp test.adb:15:36-15:43>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:15:36-15:37>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:15:38-15:40>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:15:41-15:43>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <SubtypeDecl ["Fixed_String_2"] test.adb:18:4-18:61>
*****************************************************************************

Expr: <Id "String" test.adb:18:30-18:36>
references: <DefiningName "String" __standard:105:8-105:14>
type: None
expected type: None
Expr: <Id "Integer" test.adb:18:38-18:45>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <BinOp test.adb:18:52-18:59>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:18:52-18:53>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:18:54-18:56>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:18:57-18:59>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <ArrayTypeDef test.adb:21:25-22:61>
************************************************************

Expr: <Id "Positive" test.adb:21:32-21:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Int test.adb:21:47-21:48>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Positive" test.adb:22:32-22:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Id "Integer" test.adb:22:54-22:61>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None

Resolving xrefs for node <ArrayTypeDef test.adb:24:25-25:66>
************************************************************

Expr: <Id "Positive" test.adb:24:32-24:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Id "Positive" test.adb:25:32-25:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Int test.adb:25:47-25:48>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Integer" test.adb:25:59-25:66>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None

Resolving xrefs for node <ArrayTypeDef test.adb:27:25-28:66>
************************************************************

Expr: <Id "Positive" test.adb:27:32-27:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Int test.adb:27:47-27:48>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Positive" test.adb:28:32-28:40>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Int test.adb:28:47-28:48>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: None
Expr: <Id "Integer" test.adb:28:59-28:66>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None

Resolving xrefs for node <ArrayTypeDef test.adb:31:19-31:74>
************************************************************

Expr: <Id "Positive" test.adb:31:26-31:34>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Id "Positive" test.adb:31:45-31:53>
references: <DefiningName "Positive" __standard:6:11-6:19>
type: None
expected type: None
Expr: <Id "Integer" test.adb:31:67-31:74>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None

Resolving xrefs for node <SubtypeDecl ["SMatrix"] test.adb:34:4-34:49>
**********************************************************************

Expr: <Id "Matrix" test.adb:34:23-34:29>
references: <DefiningName "Matrix" test.adb:31:9-31:15>
type: None
expected type: None
Expr: <BinOp test.adb:34:31-34:38>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:34:31-34:32>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:34:33-34:35>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:34:36-34:38>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <BinOp test.adb:34:40-34:47>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:34:40-34:41>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:34:42-34:44>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:34:45-34:47>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <SubtypeDecl ["SMatrix2"] test.adb:36:4-36:78>
***********************************************************************

Expr: <Id "Matrix" test.adb:36:24-36:30>
references: <DefiningName "Matrix" test.adb:31:9-31:15>
type: None
expected type: None
Expr: <Id "Integer" test.adb:36:32-36:39>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <BinOp test.adb:36:46-36:53>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:36:46-36:47>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:36:48-36:50>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:36:51-36:53>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Id "Integer" test.adb:36:55-36:62>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <BinOp test.adb:36:69-36:76>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Int test.adb:36:69-36:70>
references: None
type: <ConcreteTypeDecl ["Universal_Int_Type_"] __standard:116:3-116:45>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpDoubleDot ".." test.adb:36:71-36:73>
references: None
type: None
expected type: None
Expr: <BoxExpr test.adb:36:74-36:76>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>


Done.
2 changes: 2 additions & 0 deletions testsuite/tests/name_resolution/fixed_lower_bound/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: name-resolution
input_sources: [test.adb]
10 changes: 3 additions & 7 deletions testsuite/tests/parser/full_type_decl_16/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ ConcreteTypeDecl[1:1-1:49]
| | |f_types:
| | | UnconstrainedArrayIndexList[1:20-1:36]
| | | | UnconstrainedArrayIndex[1:20-1:36]
| | | | |f_subtype_indication:
| | | | | SubtypeIndication[1:20-1:27]
| | | | | |f_has_not_null:
| | | | | | NotNullAbsent[1:20-1:20]
| | | | | |f_name:
| | | | | | Id[1:20-1:27]: Integer
| | | | | |f_constraint: <null>
| | | | |f_subtype_name:
| | | | | Id[1:20-1:27]: Integer
| | | | |f_lower_bound: <null>
| |f_component_type:
| | ComponentDef[1:41-1:48]
| | |f_has_aliased:
Expand Down
6 changes: 6 additions & 0 deletions user_manual/changes/libadalang/1034.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: new-feature
title: Fixed lower bounds for array types and subtypes
description: |
This change adds support for the GNAT experimental feature allowing fixed
lower bounds for array types and subtypes.
date: 2023-11-07

0 comments on commit 709e248

Please sign in to comment.