From 6b5ac6bb0dec86807d778979a7cbc476d727d71a Mon Sep 17 00:00:00 2001 From: Romain Beguet Date: Mon, 27 Nov 2023 12:03:59 +0100 Subject: [PATCH] Synthetize object for self references on all types. --- ada/ast.py | 22 ++++++++----------- .../name_resolution/type_predicates/test.out | 2 +- .../type_predicates_2/test.adb | 18 +++++++++++++++ .../type_predicates_2/test.out | 17 ++++++++++++++ .../type_predicates_2/test.yaml | 2 ++ 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 testsuite/tests/name_resolution/type_predicates_2/test.adb create mode 100644 testsuite/tests/name_resolution/type_predicates_2/test.out create mode 100644 testsuite/tests/name_resolution/type_predicates_2/test.yaml diff --git a/ada/ast.py b/ada/ast.py index c893b268c..596b53844 100644 --- a/ada/ast.py +++ b/ada/ast.py @@ -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 diff --git a/testsuite/tests/name_resolution/type_predicates/test.out b/testsuite/tests/name_resolution/type_predicates/test.out index 922cb341b..e27fb2e54 100644 --- a/testsuite/tests/name_resolution/type_predicates/test.out +++ b/testsuite/tests/name_resolution/type_predicates/test.out @@ -530,7 +530,7 @@ Expr: expected type: Expr: references: - type: None + type: expected type: None Expr: references: None diff --git a/testsuite/tests/name_resolution/type_predicates_2/test.adb b/testsuite/tests/name_resolution/type_predicates_2/test.adb new file mode 100644 index 000000000..a297378c8 --- /dev/null +++ b/testsuite/tests/name_resolution/type_predicates_2/test.adb @@ -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; diff --git a/testsuite/tests/name_resolution/type_predicates_2/test.out b/testsuite/tests/name_resolution/type_predicates_2/test.out new file mode 100644 index 000000000..b54cef314 --- /dev/null +++ b/testsuite/tests/name_resolution/type_predicates_2/test.out @@ -0,0 +1,17 @@ +Working on node +============================================== + +Eval 'node.p_referenced_decl()' +Result: + +Working on node +============================================== + +Eval 'node.p_referenced_decl()' +Result: + +Working on node +================================================ + +Eval 'node.p_referenced_decl()' +Result: diff --git a/testsuite/tests/name_resolution/type_predicates_2/test.yaml b/testsuite/tests/name_resolution/type_predicates_2/test.yaml new file mode 100644 index 000000000..35ad4d5c4 --- /dev/null +++ b/testsuite/tests/name_resolution/type_predicates_2/test.yaml @@ -0,0 +1,2 @@ +driver: inline-playground +input_sources: [test.adb]