Skip to content

Commit

Permalink
Merge branch 'topic/local_decl_without_block' into 'master'
Browse files Browse the repository at this point in the history
Implement "local declarations without block" -gnatX feature

Closes #1031

See merge request eng/libadalang/libadalang!1461
  • Loading branch information
raph-amiard committed Dec 1, 2023
2 parents 7b20fde + 533a83c commit 2802a1d
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 5 deletions.
13 changes: 12 additions & 1 deletion ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12547,7 +12547,7 @@ class GenericFormal(BaseFormalParamDecl):
Enclosing declaration for a generic formal. The real declaration is
accessible via the ``decl`` field.
"""
decl = Field(T.BasicDecl)
decl = Field(type=T.BasicDecl)
aspects = NullField()

defining_names = Property(Entity.decl.defining_names)
Expand Down Expand Up @@ -22113,6 +22113,17 @@ class NamedStmt(CompositeStmt):
xref_equation = Property(LogicTrue())


class SimpleDeclStmt(SimpleStmt):
"""
Statement wrapping a simple object declaration.
"""
decl = Field(type=T.ObjectDecl)

@langkit_property()
def xref_equation():
return LogicTrue()


@abstract
class BaseLoopStmt(CompositeStmt):
"""
Expand Down
10 changes: 6 additions & 4 deletions ada/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ def end_named_block():
A.protected_type_decl,
A.subtype_decl,
A.object_decl,
A.single_task_decl,
A.protected_decl,
A.number_decl,
A.package_decl,
A.aspect_clause,
A.use_clause,
Expand All @@ -530,9 +533,6 @@ def end_named_block():
object_decl=Or(
A.sub_object_decl,
A.no_type_object_renaming_decl,
A.single_task_decl,
A.protected_decl,
A.number_decl
),

sub_object_decl=ObjectDecl(
Expand Down Expand Up @@ -1098,10 +1098,12 @@ def end_named_block():

simple_stmt=Or(A.null_stmt, A.assignment_stmt,
A.goto_stmt, A.exit_stmt,
A.return_stmt, A.requeue_stmt,
A.return_stmt, A.requeue_stmt, A.simple_decl_stmt,
A.call_stmt, A.abort_stmt, A.delay_stmt,
A.raise_stmt, A.terminate_alternative, A.pragma),

simple_decl_stmt=SimpleDeclStmt(A.object_decl),

null_stmt=NullStmt(L.Null, sc()),

assignment_stmt=AssignStmt(A.name, ":=", Cut(), A.expr, sc()),
Expand Down
37 changes: 37 additions & 0 deletions testsuite/tests/name_resolution/stmt_object_decl/test.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
with Ada.Text_IO; use Ada.Text_IO;

procedure Test is
Outer : Integer := 12;
begin
A : Integer := 12;
B : Integer := A;

-- Basic test: A & B should be visible
Put_Line (Integer'Image (A + B));
pragma Test_Statement;

-- Sequential visibility test: This should fail because C is not declared
-- yet.
Put_Line (Integer'Image (A + B + C));
pragma Test_Statement;

-- Renamings test: this should work
C : Integer renames B;
Put_Line (Integer'Image (A + B + C));
pragma Test_Statement;

-- FQN test
Test.Outer := A;
pragma Test_Statement;

Outer : Integer := Test.Outer;

-- Hiding test: should point to redefinition above
Outer := A;
pragma Test_Statement;

-- FQN + hiding test: should point to redefinition above too
Test.Outer := A;
pragma Test_Statement;

end Test;
159 changes: 159 additions & 0 deletions testsuite/tests/name_resolution/stmt_object_decl/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
Analyzing test.adb
##################

Resolving xrefs for node <CallStmt test.adb:10:4-10:37>
*******************************************************

Expr: <CallExpr test.adb:10:4-10:36>
references: <DefiningName "Put_Line" a-textio.ads:565:14-565:22>
type: None
expected type: None
Expr: <Id "Put_Line" test.adb:10:4-10:12>
references: <DefiningName "Put_Line" a-textio.ads:565:14-565:22>
type: None
expected type: None
Expr: <CallExpr test.adb:10:14-10:35>
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 test.adb:10:14-10:27>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: None
expected type: None
Expr: <Id "Integer" test.adb:10:14-10:21>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <Id "Image" test.adb:10:22-10:27>
references: None
type: None
expected type: None
Expr: <BinOp test.adb:10:29-10:34>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Id "A" test.adb:10:29-10:30>
references: <DefiningName "A" test.adb:6:4-6:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpPlus "+" test.adb:10:31-10:32>
references: None
type: None
expected type: None
Expr: <Id "B" test.adb:10:33-10:34>
references: <DefiningName "B" test.adb:7:4-7:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <CallStmt test.adb:15:4-15:41>
*******************************************************

Resolution failed for node <CallStmt test.adb:15:4-15:41>

Resolving xrefs for node <CallStmt test.adb:20:4-20:41>
*******************************************************

Expr: <CallExpr test.adb:20:4-20:40>
references: <DefiningName "Put_Line" a-textio.ads:565:14-565:22>
type: None
expected type: None
Expr: <Id "Put_Line" test.adb:20:4-20:12>
references: <DefiningName "Put_Line" a-textio.ads:565:14-565:22>
type: None
expected type: None
Expr: <CallExpr test.adb:20:14-20:39>
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 test.adb:20:14-20:27>
references: <SyntheticDefiningName "image" __standard:4:3-4:54>
type: None
expected type: None
Expr: <Id "Integer" test.adb:20:14-20:21>
references: <DefiningName "Integer" __standard:4:8-4:15>
type: None
expected type: None
Expr: <Id "Image" test.adb:20:22-20:27>
references: None
type: None
expected type: None
Expr: <BinOp test.adb:20:29-20:38>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <BinOp test.adb:20:29-20:34>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Id "A" test.adb:20:29-20:30>
references: <DefiningName "A" test.adb:6:4-6:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpPlus "+" test.adb:20:31-20:32>
references: None
type: None
expected type: None
Expr: <Id "B" test.adb:20:33-20:34>
references: <DefiningName "B" test.adb:7:4-7:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <OpPlus "+" test.adb:20:35-20:36>
references: None
type: None
expected type: None
Expr: <Id "C" test.adb:20:37-20:38>
references: <DefiningName "C" test.adb:19:4-19:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <AssignStmt test.adb:24:4-24:20>
*********************************************************

Expr: <DottedName test.adb:24:4-24:14>
references: <DefiningName "Outer" test.adb:4:4-4:9>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: None
Expr: <Id "Test" test.adb:24:4-24:8>
references: <DefiningName "Test" test.adb:3:11-3:15>
type: None
expected type: None
Expr: <Id "Outer" test.adb:24:9-24:14>
references: <DefiningName "Outer" test.adb:4:4-4:9>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: None
Expr: <Id "A" test.adb:24:18-24:19>
references: <DefiningName "A" test.adb:6:4-6:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <AssignStmt test.adb:30:4-30:15>
*********************************************************

Expr: <Id "Outer" test.adb:30:4-30:9>
references: <DefiningName "Outer" test.adb:27:4-27:9>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: None
Expr: <Id "A" test.adb:30:13-30:14>
references: <DefiningName "A" test.adb:6:4-6:5>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>

Resolving xrefs for node <AssignStmt test.adb:34:4-34:20>
*********************************************************

Expr: <DottedName test.adb:34:4-34:14>
references: <DefiningName "Outer" test.adb:27:4-27:9>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: None
Expr: <Id "Test" test.adb:34:4-34:8>
references: <DefiningName "Test" test.adb:3:11-3:15>
type: None
expected type: None
Expr: <Id "Outer" test.adb:34:9-34:14>
references: <DefiningName "Outer" test.adb:27:4-27:9>
type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
expected type: None
Expr: <Id "A" test.adb:34:18-34:19>
references: <DefiningName "A" test.adb:6:4-6:5>
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/stmt_object_decl/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: name-resolution
input_sources: [test.adb]

0 comments on commit 2802a1d

Please sign in to comment.