Skip to content

Commit

Permalink
#2642 Refactor, codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienRemy committed Dec 11, 2024
1 parent 6f18710 commit 9419a05
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 40 deletions.
46 changes: 7 additions & 39 deletions src/psyclone/psyGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,46 +1765,14 @@ def _rename_psyir(self, suffix):
partial_datatype=sym.datatype.partial_datatype)
# pylint: enable=protected-access
# Or the DataTypeSymbol is a StructureType, in which case we
# go through its procedure components.
# replace the "code" procedure component initial value.
elif isinstance(sym.datatype, StructureType):
for procedure_name, procedure_component \
in sym.datatype.procedure_components.items():
# Either the procedure component is of
# UnsupportedFortranType, in which case we replace in its
# whole declaration.
if isinstance(procedure_component.datatype,
UnsupportedFortranType):
new_declaration = \
procedure_component.datatype.declaration.replace(
orig_kern_name, new_kern_name)
new_procedure_component = StructureType.ComponentType(
procedure_component.name,
UnsupportedFortranType(new_declaration,
procedure_component.
datatype.partial_datatype),
procedure_component.visibility,
procedure_component.initial_value)
sym.datatype.procedure_components[procedure_name] = \
new_procedure_component
# Or the procedure component has an initial value that is
# a Reference to the original kernel name, in which case
# we replace it with a Reference to the new kernel name.
elif (procedure_component.initial_value is not None
and (procedure_component.initial_value.name.lower()
== orig_kern_name.lower())):
new_kernel_symbol = container_table.lookup(
new_kern_name)
new_procedure_component = \
StructureType.ComponentType(procedure_component
.name,
procedure_component
.datatype,
procedure_component
.visibility,
Reference(
new_kernel_symbol))
sym.datatype.procedure_components[procedure_name] = \
new_procedure_component
new_kernel_symbol = container_table.lookup(new_kern_name)
new_initial_value = Reference(new_kernel_symbol)
sym.datatype.replace_procedure_component_initial_value(
orig_kern_name,
new_initial_value
)

@property
def modified(self):
Expand Down
70 changes: 69 additions & 1 deletion src/psyclone/psyir/symbols/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ def lookup_component(self, name):
return self._components[name]

def add_procedure_component(self, name, datatype, visibility,
initial_value):
initial_value=None):
'''
Create a procedure component with the supplied attributes and add it to
this StructureType.
Expand Down Expand Up @@ -1147,6 +1147,74 @@ def lookup_procedure_component(self, name):
'''
return self._procedure_components[name]

def replace_procedure_component_initial_value(self, old_value_name,
new_value):
'''
Replace the initial value of the procedure component with
"old_value_name" as initial value with the supplied new value.
:param str old_value_name: the name of the initial value to replace.
:param new_value: the new initial value for the procedure component.
:type new_value: :py:class:`psyclone.psyir.nodes.Reference`
:raises TypeError: if the name is not a string.
:raises TypeError: if the new value is not a Reference to a
RoutineSymbol.
:returns: None
'''
# pylint: disable=import-outside-toplevel
# These imports must be placed here to avoid circular dependencies.
from psyclone.psyir.symbols import RoutineSymbol
from psyclone.psyir.nodes import Reference

if not isinstance(old_value_name, str):
raise TypeError(
"The name of the procedure component to replace must be a "
f"string but got '{type(old_value_name).__name__}'.")
if not isinstance(new_value, Reference):
raise TypeError(
"The new value for the procedure component must be a "
f"Reference but got '{type(new_value).__name__}'.")
if not isinstance(new_value.symbol, RoutineSymbol):
raise TypeError(
"The new value for the procedure component must be a "
f"Reference to a RoutineSymbol but got a Reference to a "
f"'{type(new_value.symbol).__name__}'.")

for name, procedure_component in self._procedure_components.items():
if isinstance(procedure_component.datatype,
UnsupportedFortranType):
# Either the procedure component is of UnsupportedFortranType,
# in which case we replace in its whole string declaration.
if old_value_name.lower() not in procedure_component.datatype\
.declaration.lower():
continue
new_declaration = procedure_component.datatype\
.declaration.replace(old_value_name, new_value.name)
new_datatype = UnsupportedFortranType(
new_declaration,
procedure_component.datatype.partial_datatype)
self._procedure_components[name] = self.ComponentType(
name,
new_datatype,
procedure_component.visibility,
new_value)
return

# Or it is enough to replace the initial value.
if (not procedure_component.initial_value
or procedure_component.initial_value.name.lower()
!= old_value_name.lower()):
continue
self._procedure_components[name] = self.ComponentType(
name,
procedure_component.datatype,
procedure_component.visibility,
new_value)
return

def __eq__(self, other):
'''
:param Any other: the object to check equality to.
Expand Down
5 changes: 5 additions & 0 deletions src/psyclone/tests/psyGen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ def test_codedkern__rename_psyir_supported_procedure_datatype():
Reference(routine_symbol))
testkern_type.datatype.procedure_components["code"] \
= new_procedure_component
# Check that the procedure component has been replaced
assert (testkern_type.datatype.procedure_components["code"]
.initial_value is not None)
assert (testkern_type.datatype.procedure_components["code"]
.initial_value.name.lower() == "testkern_code")
# Rename
kern._rename_psyir(suffix="_new2")
assert (testkern_type.datatype.procedure_components["code"]
Expand Down
47 changes: 47 additions & 0 deletions src/psyclone/tests/psyir/symbols/datatype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,50 @@ def test_structuretype_extends():
structure_type.extends = "invalid"
assert ("The type that a StructureType extends must be a "
"DataTypeSymbol but got 'str'." in str(err.value))


def test_replace_procedure_component_initial_value():
'''Test the replace_procedure_component_initial_value method of
StructureType.'''
stype = StructureType()
with pytest.raises(TypeError) as excinfo:
stype.replace_procedure_component_initial_value(123, None)
assert ("The name of the procedure component to replace must be a string"
in str(excinfo.value))

stype = StructureType()
with pytest.raises(TypeError) as excinfo:
stype.replace_procedure_component_initial_value("old_value", 123)
assert ("The new value for the procedure component must be a Reference"
in str(excinfo.value))

stype = StructureType()
symbol = Symbol("dummy")
ref = Reference(symbol)
with pytest.raises(TypeError) as excinfo:
stype.replace_procedure_component_initial_value("old_value", ref)
assert ("The new value for the procedure component must be a Reference to "
"a RoutineSymbol" in str(excinfo.value))

# UnsupportedFortranType
stype = StructureType()
unsupported_type = UnsupportedFortranType("procedure, code => routine",
None)
stype.add_procedure_component("proc", unsupported_type,
Symbol.Visibility.PUBLIC)
new_routine_symbol = RoutineSymbol("new_routine")
new_ref = Reference(new_routine_symbol)
stype.replace_procedure_component_initial_value("routine", new_ref)
assert (stype.lookup_procedure_component("proc").datatype.declaration
== "procedure, code => new_routine")

# Other datatype
stype = StructureType()
routine_symbol = RoutineSymbol("routine")
old_ref = Reference(routine_symbol)
stype.add_procedure_component("proc", UnresolvedType(),
Symbol.Visibility.PUBLIC, old_ref)
new_routine_symbol = RoutineSymbol("new_routine")
new_ref = Reference(new_routine_symbol)
stype.replace_procedure_component_initial_value("routine", new_ref)
assert stype.lookup_procedure_component("proc").initial_value == new_ref

0 comments on commit 9419a05

Please sign in to comment.