From bc5ac512e3416d9e2cbd386bf4a7ef12dc5298a8 Mon Sep 17 00:00:00 2001 From: Romain Beguet Date: Wed, 27 Mar 2024 17:06:36 +0100 Subject: [PATCH 1/2] Return a more precise type for the `designated_generic_decl` property. --- ada/ast.py | 11 +++++------ testsuite/ada/nameres.adb | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ada/ast.py b/ada/ast.py index 969c98c49..18c82e798 100644 --- a/ada/ast.py +++ b/ada/ast.py @@ -12286,9 +12286,9 @@ def instantiation_bindings_internal(): ) designated_generic_decl = AbstractProperty( - type=T.BasicDecl.entity, public=True, doc=""" + type=T.GenericDecl.entity, public=True, doc=""" Return the generic decl entity designated by this instantiation, - containing the generic context. This is equivalent to the expanded + including instantiation information. This is equivalent to the expanded generic unit in GNAT. """ ) @@ -12374,8 +12374,7 @@ def parent_instantiation_env(): Entity.is_any_formal, LogicTrue(), - Entity.designated_generic_decl.cast_or_raise(T.GenericDecl) - ._.formal_part.match_param_list( + Entity.designated_generic_decl._.formal_part.match_param_list( Entity.generic_inst_params, False ).filter( lambda pm: Not(pm.actual.assoc.expr.is_a(BoxExpr)) @@ -12577,7 +12576,7 @@ def designated_subp(): ) designated_generic_decl = Property( - Entity.designated_subp.parent.cast(T.BasicDecl) + Entity.designated_subp.parent.cast_or_raise(T.GenericDecl) ) @@ -12622,7 +12621,7 @@ def designated_package(): ) designated_generic_decl = Property( - Entity.designated_package.parent.cast(T.BasicDecl) + Entity.designated_package.parent.cast_or_raise(T.GenericDecl) ) @langkit_property(return_type=LexicalEnv, dynamic_vars=[origin]) diff --git a/testsuite/ada/nameres.adb b/testsuite/ada/nameres.adb index efcc43250..3fb845885 100644 --- a/testsuite/ada/nameres.adb +++ b/testsuite/ada/nameres.adb @@ -1112,7 +1112,7 @@ procedure Nameres is if Args.Traverse_Generics.Get then if Node.Kind in Ada_Generic_Instantiation then declare - Generic_Decl : constant Basic_Decl := + Generic_Decl : constant Libadalang.Analysis.Generic_Decl := Node.As_Generic_Instantiation.P_Designated_Generic_Decl; Generic_Body : constant Body_Node := Generic_Decl.P_Body_Part_For_Decl; From c367bddc905b891bdef7c0b460a8d858494c7100 Mon Sep 17 00:00:00 2001 From: Romain Beguet Date: Wed, 27 Mar 2024 17:31:44 +0100 Subject: [PATCH 2/2] Make ``inst_params`` parameters instance-aware. Also update the zip_with_params implementation to behave similarly. --- ada/ast.py | 35 +-- .../navigation/generic_formal_object/test.out | 8 +- .../generic_formal_object_2/test.out | 8 +- .../tests/properties/inst_params/test.out | 248 +++++++++--------- .../test.adb | 12 + .../test.out | 8 + .../test.yaml | 2 + .../zip_with_params_box_expr/test.out | 18 +- 8 files changed, 168 insertions(+), 171 deletions(-) create mode 100644 testsuite/tests/properties/inst_params_unique_identifying_name/test.adb create mode 100644 testsuite/tests/properties/inst_params_unique_identifying_name/test.out create mode 100644 testsuite/tests/properties/inst_params_unique_identifying_name/test.yaml diff --git a/ada/ast.py b/ada/ast.py index 18c82e798..e6bea27cc 100644 --- a/ada/ast.py +++ b/ada/ast.py @@ -12468,7 +12468,7 @@ def inst_params(): """ ap = Var(Entity.generic_inst_params) - return Entity.nonbound_generic_decl._.formal_part.decls.mapcat( + return Entity.designated_generic_decl._.formal_part.decls.mapcat( # Unpack generic formals with their default expressions lambda d: d.match( lambda t=GenericFormalPackage: ParamActual.new( @@ -12992,6 +12992,8 @@ def designated_subprogram_from(inst=T.GenericInstantiation.entity): Return the first visible subprogram that can match this formal subp in the context of an instantiation. This is used to find the matching subprogram in an instantiation for a formal subp with a box expr. + This property assumes that ``Entity`` is already in the context of the + given instantiation. """ subps = Var(Self.env_get( env=inst.node_env, @@ -12999,36 +13001,10 @@ def designated_subprogram_from(inst=T.GenericInstantiation.entity): from_node=inst.node )) - # Create a subp spec for the formal subprogram in the context of the - # instantiation, e.g. for the function ``Foo`` in ``G_Inst`` in the - # following code:: - # - # generic - # type T is private; - # function Foo (Self: T) return T; - # package G is ... - # - # package G_Inst is new G (Integer); - # - # return ``function Foo (Self : Integer) return Integer``. - actual_spec = Var(SubpSpec.entity.new( - - node=Self.subp_spec, - info=T.entity_info.new( - md=Entity.info.md, - rebindings=Entity.info.rebindings - # Append the given generic instantiation - .append_rebinding( - Self.parent.node_env, inst.instantiation_env - ), - from_rebound=Entity.info.from_rebound - ) - )) - # Search for the first subprogram that matches the instantiated profile found = Var(origin.bind(inst.origin_node, subps.find( lambda subp: subp.cast(BasicDecl).subp_spec_or_null.then( - lambda spec: actual_spec.match_signature( + lambda spec: Entity.subp_spec.match_signature( other=spec, # Names must already match due to the env_get call @@ -17428,8 +17404,7 @@ def zip_with_params(): lambda e=T.CallExpr: e.called_subp_spec._.abstract_formal_params, lambda i=T.GenericInstantiation: - i.generic_entity_name.referenced_decl.cast(T.GenericDecl) - ._.formal_part.abstract_formal_params, + i.designated_generic_decl._.formal_part.abstract_formal_params, lambda c=T.CompositeConstraint: c.subtype._.discriminants_list, diff --git a/testsuite/tests/navigation/generic_formal_object/test.out b/testsuite/tests/navigation/generic_formal_object/test.out index 64166d0a8..efb0e31a7 100644 --- a/testsuite/tests/navigation/generic_formal_object/test.out +++ b/testsuite/tests/navigation/generic_formal_object/test.out @@ -1,13 +1,13 @@ In instantiation of : - X.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:19:4] |> - Y.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:19:4] |> In instantiation of : - X.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:20:4] |> - Y.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:20:4] |> Done diff --git a/testsuite/tests/navigation/generic_formal_object_2/test.out b/testsuite/tests/navigation/generic_formal_object_2/test.out index 621f15430..927977fe3 100644 --- a/testsuite/tests/navigation/generic_formal_object_2/test.out +++ b/testsuite/tests/navigation/generic_formal_object_2/test.out @@ -1,13 +1,13 @@ In instantiation of : - X.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:15:4] |> - Y.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:15:4] |> In instantiation of : - X.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:16:4] |> - Y.p_referenced_decl() => - - p_get_formal() => + - p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:16:4] |> Done diff --git a/testsuite/tests/properties/inst_params/test.out b/testsuite/tests/properties/inst_params/test.out index 364f00436..891156dca 100644 --- a/testsuite/tests/properties/inst_params/test.out +++ b/testsuite/tests/properties/inst_params/test.out @@ -2,307 +2,307 @@ Working on node =========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - "" gen_cmp.adb:7:21-7:24> actual=>] +Result: [ actual=>, + actual=>, + actual=>, + "" gen_cmp.adb:7:21-7:24 [gen_cmp.adb:26:4] |> actual=>] Working on node =========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - "" gen_cmp.adb:7:21-7:24> actual=>] +Result: [ actual=>, + actual=>, + actual=>, + "" gen_cmp.adb:7:21-7:24 [gen_cmp.adb:32:4] |> actual=>] Working on node ====================================================================================== Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ================================================================================ Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================= Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=<| Int geni.adb:19:22-19:23 [geni.adb:60:4] |>>, + actual=<| Int geni.adb:20:22-20:23 [geni.adb:60:4] |>>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=<| Int geni.adb:20:22-20:23 [geni.adb:62:4] |>>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=<| Int geni.adb:26:25-26:26 [geni.adb:73:4] |>>, + actual=<| Int geni.adb:26:25-26:26 [geni.adb:73:4] |>>] Working on node ======================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=<| Int geni.adb:26:25-26:26 [geni.adb:75:4] |>>] Working on node ========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=<| Int geni.adb:80:22-80:23 [geni.adb:89:4] |>>, + actual=<| Int geni.adb:81:25-81:26 [geni.adb:89:4] |>>, + actual=<| Int geni.adb:81:25-81:26 [geni.adb:89:4] |>>, + actual=>] Working on node ========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=<| Int geni.adb:80:22-80:23 [geni.adb:91:4] |>>, + actual=<| Int geni.adb:81:25-81:26 [geni.adb:91:4] |>>, + actual=<| Int geni.adb:81:25-81:26 [geni.adb:91:4] |>>, + actual=>] Working on node ========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ========================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=>, + actual=>, + actual=>] Working on node ========================================================================= Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================= Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node =============================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node =============================================================================== Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node ========================================================================= Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================ Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================ Eval 'node.p_inst_params' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================ Eval 'node.p_inst_params' -Result: [ actual=>, - actual=>, - actual=>] +Result: [ actual=>, + actual=>, + actual=<| DottedName what.adb:13:45-13:52 [what.adb:19:4] |>>] diff --git a/testsuite/tests/properties/inst_params_unique_identifying_name/test.adb b/testsuite/tests/properties/inst_params_unique_identifying_name/test.adb new file mode 100644 index 000000000..7b5557870 --- /dev/null +++ b/testsuite/tests/properties/inst_params_unique_identifying_name/test.adb @@ -0,0 +1,12 @@ +procedure Test is + generic + X : Integer; + package Gen is + end Gen; + + package Inst is new Gen (0); + --% params = node.p_inst_params + --% params[0].param.p_unique_identifying_name +begin + null; +end Test; diff --git a/testsuite/tests/properties/inst_params_unique_identifying_name/test.out b/testsuite/tests/properties/inst_params_unique_identifying_name/test.out new file mode 100644 index 000000000..37893965d --- /dev/null +++ b/testsuite/tests/properties/inst_params_unique_identifying_name/test.out @@ -0,0 +1,8 @@ +Working on node +======================================================================== + +Set 'params' to 'node.p_inst_params' +Result: [ actual=>] + +Eval 'params[0].param.p_unique_identifying_name' +Result: 'test().inst.x' diff --git a/testsuite/tests/properties/inst_params_unique_identifying_name/test.yaml b/testsuite/tests/properties/inst_params_unique_identifying_name/test.yaml new file mode 100644 index 000000000..35ad4d5c4 --- /dev/null +++ b/testsuite/tests/properties/inst_params_unique_identifying_name/test.yaml @@ -0,0 +1,2 @@ +driver: inline-playground +input_sources: [test.adb] diff --git a/testsuite/tests/properties/zip_with_params_box_expr/test.out b/testsuite/tests/properties/zip_with_params_box_expr/test.out index 63ec2386d..c0246b0d0 100644 --- a/testsuite/tests/properties/zip_with_params_box_expr/test.out +++ b/testsuite/tests/properties/zip_with_params_box_expr/test.out @@ -2,37 +2,37 @@ Working on node ========================================================================= Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================= Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node =============================================================================== Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node =============================================================================== Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>, - actual=>] +Result: [ actual=>, + actual=>] Working on node ========================================================================= Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>] +Result: [ actual=>] Working on node ============================================================================ Eval 'node.f_params.p_zip_with_params()' -Result: [ actual=>] +Result: [ actual=>]