Skip to content

Commit ce1c282

Browse files
committed
Added the ability to change READ access to TYPE_INFO
1 parent 45fffca commit ce1c282

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/psyclone/core/access_sequence.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ def change_read_to_write(self):
9090
"which does not have 'READ' access.")
9191
self._access_type = AccessType.WRITE
9292

93+
def change_read_to_type_info(self):
94+
'''This changes the access mode from READ to TYPE_INFO.
95+
96+
:raises InternalError: if the variable does not have READ acccess.
97+
'''
98+
if self._access_type != AccessType.READ:
99+
raise InternalError("Trying to change variable to 'TYPE_INFO' "
100+
"which does not have 'READ' access.")
101+
self._access_type = AccessType.TYPE_INFO
102+
93103
@property
94104
def component_indices(self):
95105
'''
@@ -310,6 +320,35 @@ def add_access(
310320
'''
311321
self.append(AccessInfo(access_type, node, component_indices))
312322

323+
def change_read_to_type_info(self):
324+
'''This function is used to change a READ into a TYPEINFO.
325+
326+
:raises InternalError: if there is an access that is not READ or
327+
INQUIRY or there is > 1 READ accesses.
328+
'''
329+
read_access = None
330+
for acc in self:
331+
332+
if acc.access_type == AccessType.READ:
333+
if read_access:
334+
raise InternalError(
335+
f"Trying to change variable '{self._signature}' to "
336+
f"'TYPE_INFO' but it has more than one 'READ' access."
337+
)
338+
read_access = acc
339+
340+
elif acc.access_type not in AccessType.non_data_accesses():
341+
raise InternalError(
342+
f"Variable '{self._signature}' has a '{acc.access_type}' "
343+
f"access. change_read_to_type_info() expects only "
344+
f"inquiry accesses and a single 'READ' access.")
345+
346+
if not read_access:
347+
raise InternalError(
348+
f"Trying to change variable '{self._signature}' to "
349+
f"'TYPE_INFO' but it does not have a 'READ' access.")
350+
read_access.change_read_to_type_info()
351+
313352
def change_read_to_write(self):
314353
'''This function is only used when analysing an assignment statement.
315354
The LHS has first all variables identified, which will be READ.

src/psyclone/tests/core/access_sequence_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def test_access_info():
6262
access_info.change_read_to_write()
6363
assert "Trying to change variable to 'WRITE' which does not have "\
6464
"'READ' access." in str(err.value)
65+
access_info2 = AccessInfo(AccessType.READ, Node())
66+
assert str(access_info2) == "READ"
67+
access_info2.change_read_to_type_info()
68+
assert str(access_info2) == "TYPE_INFO"
69+
assert access_info2.access_type == AccessType.TYPE_INFO
70+
with pytest.raises(InternalError) as err:
71+
access_info2.change_read_to_type_info()
72+
assert ("Trying to change variable to 'TYPE_INFO' which does not have "
73+
"'READ' access." in str(err.value))
6574

6675
# Test setter and getter:
6776
component_indices = ComponentIndices([["i"]])
@@ -200,6 +209,42 @@ def test_variable_access_sequence():
200209
assert not accesses.has_data_access()
201210

202211

212+
def test_variable_access_sequence_read_to_type_info():
213+
'''
214+
Test the read_to_type_info functionality of AccessSequence
215+
'''
216+
accesses = AccessSequence(Signature("var_name"))
217+
accesses.add_access(AccessType.INQUIRY, Node(), component_indices=None)
218+
accesses.add_access(AccessType.READ, Node(), component_indices=None)
219+
accesses.change_read_to_type_info()
220+
assert not accesses.is_read()
221+
assert not accesses.is_written()
222+
assert not accesses.has_data_access()
223+
224+
with pytest.raises(InternalError) as err:
225+
accesses.change_read_to_type_info()
226+
assert ("Trying to change variable 'var_name' to "
227+
"'TYPE_INFO' but it does not have a 'READ' access."
228+
in str(err.value))
229+
230+
accesses.add_access(AccessType.WRITE, Node(), component_indices=None)
231+
with pytest.raises(InternalError) as err:
232+
accesses.change_read_to_type_info()
233+
assert ("Variable 'var_name' has a 'WRITE' access. "
234+
"change_read_to_type_info() "
235+
"expects only inquiry accesses and a single 'READ' access."
236+
in str(err.value))
237+
238+
accesses = AccessSequence(Signature("var_name"))
239+
accesses.add_access(AccessType.READ, Node(), component_indices=None)
240+
accesses.add_access(AccessType.READ, Node(), component_indices=None)
241+
242+
with pytest.raises(InternalError) as err:
243+
accesses.change_read_to_type_info()
244+
assert ("Trying to change variable 'var_name' to "
245+
"'TYPE_INFO' but it has more than one 'READ' access."
246+
in str(err.value))
247+
203248
def test_variable_access_sequence_has_indices(fortran_reader):
204249
'''Test that the AccessSequence class handles indices as expected.
205250

0 commit comments

Comments
 (0)