Skip to content

Commit

Permalink
Merge branch 'topic/1337' into 'master'
Browse files Browse the repository at this point in the history
Make `inst_params` parameters instance-aware.

Closes #1339 and #1337

See merge request eng/libadalang/libadalang!1599
  • Loading branch information
Roldak committed Apr 3, 2024
2 parents d733b00 + c367bdd commit e019dff
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 178 deletions.
46 changes: 10 additions & 36 deletions ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -12469,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(
Expand Down Expand Up @@ -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)
)


Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -12993,43 +12992,19 @@ 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,
symbol=Entity.defining_name.name_symbol,
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
Expand Down Expand Up @@ -17429,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,
Expand Down
2 changes: 1 addition & 1 deletion testsuite/ada/nameres.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions testsuite/tests/navigation/generic_formal_object/test.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
In instantiation of <GenericPackageInstantiation ["P_1"] test.adb:19:4-19:48>:
- X.p_referenced_decl() => <AnonymousExprDecl Integer = Var>
- p_get_formal() => <DefiningName "X" test.adb:6:7-6:8>
- p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:19:4] |>
- Y.p_referenced_decl() => <AnonymousExprDecl Integer = Value>
- p_get_formal() => <DefiningName "Y" test.adb:7:7-7:8>
- p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:19:4] |>

In instantiation of <GenericPackageInstantiation ["P_2"] test.adb:20:4-20:45>:
- X.p_referenced_decl() => <AnonymousExprDecl Integer = Var>
- p_get_formal() => <DefiningName "X" test.adb:6:7-6:8>
- p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:20:4] |>
- Y.p_referenced_decl() => <AnonymousExprDecl Integer = 13>
- p_get_formal() => <DefiningName "Y" test.adb:7:7-7:8>
- p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:20:4] |>

Done
8 changes: 4 additions & 4 deletions testsuite/tests/navigation/generic_formal_object_2/test.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
In instantiation of <GenericSubpInstantiation ["P_1"] test.adb:15:4-15:52>:
- X.p_referenced_decl() => <AnonymousExprDecl Integer = Var>
- p_get_formal() => <DefiningName "X" test.adb:6:7-6:8>
- p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:15:4] |>
- Y.p_referenced_decl() => <AnonymousExprDecl Integer = Value>
- p_get_formal() => <DefiningName "Y" test.adb:7:7-7:8>
- p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:15:4] |>

In instantiation of <GenericSubpInstantiation ["P_2"] test.adb:16:4-16:49>:
- X.p_referenced_decl() => <AnonymousExprDecl Integer = Var>
- p_get_formal() => <DefiningName "X" test.adb:6:7-6:8>
- p_get_formal() => <| DefiningName "X" test.adb:6:7-6:8 [test.adb:16:4] |>
- Y.p_referenced_decl() => <AnonymousExprDecl Integer = 13>
- p_get_formal() => <DefiningName "Y" test.adb:7:7-7:8>
- p_get_formal() => <| DefiningName "Y" test.adb:7:7-7:8 [test.adb:16:4] |>

Done
Loading

0 comments on commit e019dff

Please sign in to comment.