Skip to content

Commit

Permalink
V713-005: Improve get_aspect for pragma Convention & co.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roldak committed Sep 13, 2022
1 parent 02b2dbf commit 80e1d3f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
22 changes: 21 additions & 1 deletion ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9645,6 +9645,26 @@ def xref_equation():
)
)

@langkit_property(return_type=T.Expr.entity)
def value_expr():
"""
Return the expression representing the "value" of this pragma, which
will be used to fill the ``value`` field of the ``Aspect`` struct
returned by calls to ``get_aspect``. This property doesn't make sense
for all pragmas but tries to give the most reasonable answer, and in
particular tries to match what ``get_aspect`` would return if the
pragma was replaced by its equivalent aspect.
For example, on ``pragma Convention (C, X)``, the returned value is
``C`` because one would write ``X : Integer with Convention => C``.
"""
return If(
Entity.id.name_symbol.any_of(
'Import', 'Export', 'Interface', 'Convention'
),
Entity.args.at(0).assoc_expr,
Entity.args._.at(1)._.assoc_expr
)

@langkit_property(return_type=T.Name.entity.array)
def associated_entity_names():
return Cond(
Expand Down Expand Up @@ -15967,7 +15987,7 @@ def get_aspect_impl(name=Symbol):
"""
return Entity.get_pragma(name).then(
lambda p: Aspect.new(
exists=True, node=p, value=p.args._.at(1)._.assoc_expr
exists=True, node=p, value=p.value_expr
)
)._or(Entity.basic_decl.get_aspect_assoc(name).then(
lambda aa: Aspect.new(exists=True, node=aa, value=aa.expr)
Expand Down
12 changes: 12 additions & 0 deletions testsuite/tests/properties/get_aspect_pragma_convention/test.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
procedure Test is
X : Integer := 0
with Convention => C;
--% node.p_get_aspect("Convention").value

Y : Integer := 0;
--% node.p_get_aspect("Convention").value
pragma Convention (C, Y);
begin
null;
end Test;

11 changes: 11 additions & 0 deletions testsuite/tests/properties/get_aspect_pragma_convention/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Working on node <ObjectDecl ["X"] test.adb:2:4-3:28>
====================================================

Eval 'node.p_get_aspect("Convention").value'
Result: <Id "C" test.adb:3:26-3:27>

Working on node <ObjectDecl ["Y"] test.adb:6:4-6:21>
====================================================

Eval 'node.p_get_aspect("Convention").value'
Result: <Id "C" test.adb:8:23-8:24>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: inline-playground
input_sources: [test.adb]
21 changes: 21 additions & 0 deletions user_manual/changes/V713-005.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type: bugfix
title: Improve ``get_aspect`` for ``pragma Convention``
description: |
Previously, calling ``get_aspect("Convention")`` on the declaration of
``X`` in the snippet below would return an ``Aspect`` struct whose
``value`` field would contain the identifier ``X``:
..code:: ada
X : Integer;
pragma Convention (C, X);
It will now return ``C``, because that's what would have been returned if
one had used the Ada 2012 aspect notation instead:
..code:: ada
X : Integer
with Convention => C;
date: 2022-09-12

0 comments on commit 80e1d3f

Please sign in to comment.