Skip to content

Commit

Permalink
Synthetize object for self references on all types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roldak committed Nov 27, 2023
1 parent bf827de commit 6b5ac6b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
22 changes: 9 additions & 13 deletions ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8044,19 +8044,15 @@ def predefined_operators():
add_env(),
handle_children(),

# If this `TypeDecl` can have a predicate, add a synthetic object
# declaration into its environement in order to support name resolution
# of self-references that can appear in predicates (see
# `SyntheticObjectDecl`).
add_to_env(If(
# Try to filter which type decls can have predicate to save some
# space in envs.
Self.type_def.is_a(
T.RecordTypeDef, T.DerivedTypeDef, T.TypeAccessDef
),
Entity.synthetic_object_decl_env_assoc,
No(T.env_assoc)
)),
# Add a synthetic object declaration into its environement in order to
# support name resolution of self-references that can appear in
# predicates (see `SyntheticObjectDecl`) or in a records' body.
# TODO: This could be added conditionally to avoid polluting envs with
# unnecessary objects if we can statically detect whether it will be
# useful or not, for example by checking if `Predicate` aspect is
# specified on this type. However, it is not as easy because there can
# `Predicate` pragmas defined *after* this.
add_to_env(Entity.synthetic_object_decl_env_assoc),

# Make sure the reference to the primitives env is created *AFTER* the
# synthetic type predicate object has been added to Self's env: since
Expand Down
2 changes: 1 addition & 1 deletion testsuite/tests/name_resolution/type_predicates/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ Expr: <AttributeRef other.adb:11:29-11:40>
expected type: <ConcreteTypeDecl ["Integer"] __standard:4:3-4:54>
Expr: <Id "Table" other.adb:11:29-11:34>
references: <DefiningName "Table" other.adb:10:9-10:14>
type: None
type: <ConcreteTypeDecl ["Table"] other.adb:10:4-10:45>
expected type: None
Expr: <Id "First" other.adb:11:35-11:40>
references: None
Expand Down
18 changes: 18 additions & 0 deletions testsuite/tests/name_resolution/type_predicates_2/test.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
procedure Test is
type Even is range 1 .. 100
with Dynamic_Predicate => (
Even --% node.p_referenced_decl()
mod 2) = 0;

subtype By_4 is Even
with Dynamic_Predicate => (
By_4 --% node.p_referenced_decl()
mod 4) = 0;

type By_8 is new Even
with Dynamic_Predicate => (
By_8 --% node.p_referenced_decl()
mod 8) = 0;
begin
null;
end Test;
17 changes: 17 additions & 0 deletions testsuite/tests/name_resolution/type_predicates_2/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Working on node <Id "Even" test.adb:4:10-4:14>
==============================================

Eval 'node.p_referenced_decl()'
Result: <SyntheticObjectDecl ["Even"] test.adb:2:4-5:21>

Working on node <Id "By_4" test.adb:9:10-9:14>
==============================================

Eval 'node.p_referenced_decl()'
Result: <SyntheticObjectDecl ["By_4"] test.adb:7:4-10:21>

Working on node <Id "By_8" test.adb:14:10-14:14>
================================================

Eval 'node.p_referenced_decl()'
Result: <SyntheticObjectDecl ["By_8"] test.adb:12:4-15:21>
2 changes: 2 additions & 0 deletions testsuite/tests/name_resolution/type_predicates_2/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: inline-playground
input_sources: [test.adb]

0 comments on commit 6b5ac6b

Please sign in to comment.