Skip to content

Commit

Permalink
pr #2396. Added support for adding the target attribute to the origin…
Browse files Browse the repository at this point in the history
…al type so that the pointers can point to it. More tests are needed.
  • Loading branch information
rupertford committed Dec 7, 2023
1 parent 181e878 commit 22bae99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
39 changes: 39 additions & 0 deletions src/psyclone/psyir/frontend/fparser2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,45 @@ def _type_construct_handler(self, node, parent):
currentparent.addchild(elsebody)
self.process_nodes(parent=elsebody, nodes=stmts[default_idx])

# Ensure that the type selector variable declaration has the
# pointer or a target attribute. The difficulty is that it
# will be in an UnknownFortranType
type_selector_name = selector
symbol_table = stmt.scope.symbol_table
symbol = symbol_table.lookup(type_selector_name)
datatype = symbol.datatype
if not isinstance(datatype, UnknownFortranType):
raise InternalError(
f"Expected the datatype of the select type selector to be an "
f"UnknownFortranType, but found '{type(datatype).__name__}'.")
# Re-create the datatype as an fparser ast
dummy_code = (
f"subroutine dummy()\n"
f" {datatype.declaration}\n"
f"end subroutine\n")
parser = ParserFactory().create(std="f2008")
reader = FortranStringReader(dummy_code)
fp2_ast = parser(reader)
type_decl_stmt = fp2_ast.children[0].children[1].children[0]
attr_spec_list = type_decl_stmt.children[1]
found = False
if attr_spec_list:
for attr_spec in attr_spec_list.children:
attr_spec_str = attr_spec.string
if attr_spec_str.upper() in ["TARGET", "POINTER"]:
found = True
break
if not found:
# TARGET needs to be added as an attribute
if attr_spec_list:
attr_spec_list.append(Fortran2003.Attr_Spec("TARGET"))
else:
attr_spec_list = Fortran2003.Attr_Spec_List("TARGET")
type_decl_stmt.items = (
type_decl_stmt.items[0], attr_spec_list,
type_decl_stmt.items[2])
attr_spec_list.parent = type_decl_stmt
datatype._declaration = str(type_decl_stmt)
return stmt

def _case_construct_handler(self, node, parent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ def test_class(fortran_reader, fortran_writer, tmpdir):
" END SELECT\n"
"end subroutine\n"
"end module\n")
expected = (
expected1 = "CLASS(*), TARGET :: type"
expected2 = (
" character(256) :: type_string\n\n"
" type(type2), pointer :: ptr_type2\n\n"
" INTEGER, pointer :: ptr_INTEGER\n\n"
Expand Down Expand Up @@ -284,9 +285,8 @@ def test_class(fortran_reader, fortran_writer, tmpdir):
" end if\n")
psyir = fortran_reader.psyir_from_source(code)
result = fortran_writer(psyir)
print(result)
exit(1)
assert expected in result
assert expected1 in result
assert expected2 in result
if_blocks = psyir.walk(IfBlock)
assert "was_class_type" in if_blocks[0].annotations
assert "was_class_type" in if_blocks[2].annotations
Expand Down

0 comments on commit 22bae99

Please sign in to comment.