Skip to content

Commit ab467f9

Browse files
committed
#3473 Add Loop.variable_reference property
1 parent 7381c5e commit ab467f9

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/psyclone/psyir/nodes/loop.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
''' This module contains the Loop node implementation.'''
4141

42-
from typing import Union
42+
from typing import Union, Optional
4343

4444
from psyclone.core import VariablesAccessMap
4545
from psyclone.psyir.nodes.datanode import DataNode
@@ -274,22 +274,28 @@ def _check_completeness(self):
274274
f" '{', '.join([str(child) for child in self.children])}'.")
275275

276276
@property
277-
def variable(self):
277+
def variable_reference(self) -> Reference:
278+
'''
279+
:returns: the control variable reference for this loop.
280+
'''
281+
self._check_completeness()
282+
return self._children[0]
283+
284+
@property
285+
def variable(self) -> Optional[DataSymbol]:
278286
'''
279287
:returns: the control variable for this loop.
280-
:rtype: :py:class:`psyclone.psyir.symbols.DataSymbol`
281288
'''
282289
if len(self._children) < 1:
283290
return None
284291
return self._children[0].symbol
285292

286293
@variable.setter
287-
def variable(self, var):
294+
def variable(self, var: DataSymbol):
288295
'''
289296
Setter for the variable associated with this loop.
290297
291298
:param var: the control variable reference.
292-
:type var: :py:class:`psyclone.psyir.symbols.DataSymbol`
293299
294300
'''
295301
self._check_variable(var)

src/psyclone/tests/psyir/nodes/loop_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def test_loop_navigation_properties():
126126
loop.addchild(Literal("1", ScalarType.integer_single_type()))
127127

128128
# If it's not fully complete, it still returns an error
129+
with pytest.raises(InternalError) as err:
130+
_ = loop.variable_reference
131+
assert error_str in str(err.value)
129132
with pytest.raises(InternalError) as err:
130133
_ = loop.start_expr
131134
assert error_str in str(err.value)
@@ -143,6 +146,7 @@ def test_loop_navigation_properties():
143146
loop.addchild(Schedule(parent=loop))
144147
loop.loop_body.addchild(Return(parent=loop.loop_body))
145148

149+
assert loop.variable_reference.name == "i"
146150
assert loop.start_expr.value == "0"
147151
assert loop.stop_expr.value == "2"
148152
assert loop.step_expr.value == "1"
@@ -274,7 +278,7 @@ def test_loop_create(fortran_writer):
274278
Reference(DataSymbol("i", ScalarType.real_single_type())))
275279
loop = Loop.create(DataSymbol("i", ScalarType.integer_single_type()),
276280
start, stop, step, [child_node])
277-
ref = loop.children[0]
281+
ref = loop.variable_reference
278282
schedule = loop.loop_body
279283
assert isinstance(schedule, Schedule)
280284
check_links(loop, [ref, start, stop, step, schedule])

src/psyclone/tests/psyir/nodes/node_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ def test_replace_with_error1():
13621362
# The first child of a loop is the loop start value which should
13631363
# be a DataNode.
13641364
with pytest.raises(GenerationError) as info:
1365-
loop.children[0].replace_with(new_node)
1365+
loop.variable_reference.replace_with(new_node)
13661366
assert ("Item 'Assignment' can't be child 0 of 'Loop'. The valid "
13671367
"format is: 'Reference, DataNode, DataNode, DataNode, Schedule'"
13681368
in str(info.value))

src/psyclone/tests/psyir/nodes/reference_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ def test_reference_next_accesses(fortran_reader):
232232
assert b.next_accesses()[0] == b
233233

234234
# Check the next_access of the loop variable
235-
assert loop.children[0].next_accesses() == [loop.loop_body.children[0].rhs]
235+
assert (loop.variable_reference.next_accesses() ==
236+
[loop.loop_body.children[0].rhs])
236237

237238
# Check that a loop accessing a variable before
238239
# the reference doesn't result in a false positive.

0 commit comments

Comments
 (0)