def modify_psyir_tree(): ''' Apply modifications to the PSyIR tree created in create.py :returns: a modified PSyIR tree. :rtype: :py:class:`psyclone.psyir.nodes.Container` ''' file_container = create_psyir_tree() container = file_container.children[0] subroutine = container.children[0] # Rename one of the subroutine local symbols. tmp_symbol = subroutine.symbol_table.lookup("psyir_tmp") subroutine.symbol_table.rename_symbol(tmp_symbol, "new_variable") # The type of a symbol might be unknown symbol = Symbol("unused") container.symbol_table.add(symbol) # later its type could be determined. However, we don't want to # replace the existing symbol instance with a new instance as it # may have references, which could then lead to inconsistencies. Therefore # we support the specialise method, which transforms the existing # node type to a subclass of type without changing the memory # location of the instance. Note, any additional subclass properties would # have to be added manually. symbol.specialise(RoutineSymbol) # In some cases we may want to replace one node with another. This # can be simply done using a node's replace_with method. assignment = subroutine.children[2] assignment_rhs = assignment.rhs assignment_rhs.replace_with(Reference(tmp_symbol)) return file_container
def test_symbol_specialise(): '''Test the Symbol.specialise() method.''' # pylint: disable = unidiomatic-typecheck asym = Symbol("a") assert type(asym) is Symbol assert str(asym) == "a" asym.specialise(RoutineSymbol) assert type(asym) is RoutineSymbol assert str(asym) == "a : RoutineSymbol"