Skip to content

Commit

Permalink
Additional test for De/Reference regarding mappers, add more verbose …
Browse files Browse the repository at this point in the history
…comment for De/Reference methods
  • Loading branch information
MichaelSt98 committed Feb 5, 2024
1 parent 00c3a80 commit a43e407
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
42 changes: 40 additions & 2 deletions loki/expression/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,10 @@ class Reference(pmbl.Expression):
"""
Internal representation of a Reference.
.. warning:: Experimental!
.. warning:: Experimental! Allowing compound
``Reference(Variable(...))`` to appear
with behaviour akin to a symbol itself
for easier processing in mappers.
**C/C++ only**, no corresponding concept in Fortran.
Referencing refers to taking the address of an
Expand All @@ -1482,18 +1485,34 @@ def __init__(self, expression):

@property
def name(self):
"""
Allowing the compound ``Reference(Variable(name))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.name

@property
def type(self):
"""
Allowing the compound ``Reference(Variable(type))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.type

@property
def scope(self):
"""
Allowing the compound ``Reference(Variable(scope))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.scope

@property
def initial(self):
"""
Allowing the compound ``Reference(Variable(initial))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.initial

mapper_method = intern('map_c_reference')
Expand All @@ -1504,7 +1523,10 @@ class Dereference(pmbl.Expression):
"""
Internal representation of a Dereference.
.. warning:: Experimental!
.. warning:: Experimental! Allowing compound
``Dereference(Variable(...))`` to appear
with behaviour akin to a symbol itself
for easier processing in mappers.
**C/C++ only**, no corresponding concept in Fortran.
Dereferencing (a pointer) refers to retrieving the value
Expand All @@ -1521,18 +1543,34 @@ def __init__(self, expression):

@property
def name(self):
"""
Allowing the compound ``Dereference(Variable(name))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.name

@property
def type(self):
"""
Allowing the compound ``Dereference(Variable(type))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.type

@property
def scope(self):
"""
Allowing the compound ``Dereference(Variable(scope))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.scope

@property
def initial(self):
"""
Allowing the compound ``Dereference(Variable(initial))`` to appear
with behaviour akin to a symbol itself for easier processing in mappers.
"""
return self.expression.initial

mapper_method = intern('map_c_dereference')
Expand Down
18 changes: 18 additions & 0 deletions tests/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,21 @@ def test_expression_c_de_reference(frontend):
c_str = cgen(routine).replace(' ', '')
assert '(&var_reference)=1' in c_str
assert '(*var_dereference)=2' in c_str

# now test processing in mappers (by renaming variables being "De/Referenced")
var_reference = routine.variable_map['var_reference']
var_dereference = routine.variable_map['var_dereference']
var_map = {var_reference: var_reference.clone(name='renamed_var_reference'),
var_dereference: var_dereference.clone(name='renamed_var_dereference')}
routine.spec = SubstituteExpressions(var_map).visit(routine.spec)
routine.body = SubstituteExpressions(var_map).visit(routine.body)

f_str = fgen(routine).replace(' ', '')
assert 'renamed_var_reference=1' in f_str
assert 'renamed_var_dereference=2' in f_str
assert '*' not in f_str
assert '&' not in f_str

c_str = cgen(routine).replace(' ', '')
assert '(&renamed_var_reference)=1' in c_str
assert '(*renamed_var_dereference)=2' in c_str

0 comments on commit a43e407

Please sign in to comment.