Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Closes #2642, initial step towards #2643) Implement extends(type), procedure in derived type, class in declarations. #2644

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a527543
Add `type, extends` and `procedure` support in `StructureType`.
JulienRemy Jun 25, 2024
b95abdc
Add support for `class` keyword in declarations.
JulienRemy Jul 1, 2024
349bd2b
Cleanup.
JulienRemy Jul 2, 2024
f3bb061
#2642 Implement `extends`, `procedure` in derived types, `class` keyw…
JulienRemy Jul 2, 2024
db3d952
#2642 Add missing `class` keyword in Fortran backend.
JulienRemy Jul 15, 2024
6d0bb74
#2642 Fix `replace` calls for #2643 workaround.
JulienRemy Jul 15, 2024
a449c31
Merge branch 'master' into 2642_implement_extends_procedure_and_class
Nov 28, 2024
16ed2e4
#2642 Fix flake8 formatting and some tests.
Nov 28, 2024
e182c18
#2642 Some codecov progress, some tests still missing.
Nov 28, 2024
ff993ea
Merge branch 'master' into 2642_implement_extends_procedure_and_class
Nov 28, 2024
8c789b7
#2642 More tests.
Nov 29, 2024
4247a00
Merge branch 'master' into 2642_implement_extends_procedure_and_class
Nov 29, 2024
1cd37e4
#2642 Edit dev doc
Nov 29, 2024
0719559
#2642 Cleanup
Nov 29, 2024
6698427
#2642 More codecov
Nov 29, 2024
cc6fb67
#2642 Flake8
Nov 29, 2024
c8f74cb
#2642 Remove nonsensical test. Edit psyGen w.r.t. procedure support.
Dec 2, 2024
c35d98b
#2642 Avoid useless extends DataTypeSymbol visibility and procedure d…
Dec 2, 2024
0923123
Merge branch 'master' into 2642_implement_extends_procedure_and_class
Dec 2, 2024
18679fa
#2642 Add test about unkown parent type not being declared in the mod…
Dec 2, 2024
706bffc
#2642 Add psyGen tests and cleanup.
Dec 2, 2024
b72336b
#2642 Edits w.r.t. Andy's review
Dec 11, 2024
809ebf2
#2642 codecov
Dec 11, 2024
6f18710
#2642 flake8...
Dec 11, 2024
9419a05
#2642 Refactor, codecov
Dec 11, 2024
421e546
Merge branch 'master' into 2642_implement_extends_procedure_and_class
arporter Dec 18, 2024
a862a21
#2642 Fix parent type to UnresolvedInterface and add to symbol table.
Dec 18, 2024
cbc5857
#2642 Edits wrt Andy's second review (all except `class(*)`)
Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#2642 Some codecov progress, some tests still missing.
  • Loading branch information
JulienRemy committed Nov 28, 2024
commit e182c187027839d6a776d9f3e25f421566a436d9
26 changes: 10 additions & 16 deletions src/psyclone/domain/gocean/kernel/psyir.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,22 @@ def create_from_psyir(symbol):

datatype = symbol.datatype

if not isinstance(datatype, StructureType):
raise InternalError(
f"Expected kernel metadata to be stored in the PSyIR as "
f"a StructureType, but found "
f"{type(datatype).__name__}.")

# TODO #2643: This is a temporary solution using FortranWriter
# to allow the current metadata extraction to work with StructureType,
# instead of relying on UnsupportedFortranType.
# This will be removed when the metadata is extracted from the PSyIR
# itself.
if isinstance(datatype, StructureType):
type_declaration = FortranWriter().gen_typedecl(symbol)
type_declaration = type_declaration.replace(", public", "")
type_declaration = type_declaration.replace(", private", "")
return GOceanKernelMetadata.create_from_fortran_string(
type_declaration)

if not isinstance(datatype, UnsupportedFortranType):
raise InternalError(
f"Expected kernel metadata to be stored in the PSyIR as "
f"an UnsupportedFortranType, but found "
f"{type(datatype).__name__}.")

# In an UnsupportedFortranType, the declaration is stored as a
# string, so use create_from_fortran_string()
type_declaration = FortranWriter().gen_typedecl(symbol)
type_declaration = type_declaration.replace(", public", "")
type_declaration = type_declaration.replace(", private", "")
return GOceanKernelMetadata.create_from_fortran_string(
arporter marked this conversation as resolved.
Show resolved Hide resolved
datatype.declaration)
type_declaration)

@staticmethod
def create_from_fortran_string(fortran_string):
Expand Down
9 changes: 2 additions & 7 deletions src/psyclone/psyir/backend/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ def gen_proceduredecl(self, symbol, include_visibility=True):
:returns: the Fortran procedure declaration as a string.
:rtype: str

:raises VisitorError: if the symbol is not a RoutineSymbol.
:raises VisitorError: if the symbol is not a RoutineSymbol or a
StructureType.ComponentType.
:raises InternalError: if the visibility is to be included but is
neither PUBLIC nor PRIVATE.
'''
Expand Down Expand Up @@ -747,12 +748,6 @@ def gen_typedecl(self, symbol, include_visibility=True):
f"Fortran backend cannot generate code for symbol "
f"'{symbol.name}' of type '{type(symbol.datatype).__name__}'")

if isinstance(symbol.datatype, UnresolvedType):
raise VisitorError(
f"Local Symbol '{symbol.name}' is of UnresolvedType and "
f"therefore no declaration can be created for it. Should it "
f"have an ImportInterface?")

if not isinstance(symbol.datatype, StructureType):
raise VisitorError(
f"gen_typedecl expects a DataTypeSymbol with a StructureType "
Expand Down
10 changes: 0 additions & 10 deletions src/psyclone/psyir/frontend/fparser2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,18 +2028,8 @@ def _process_derived_type_decln(self, parent, decl, visibility_map):
# We support derived-type definitions with a CONTAINS section.
arporter marked this conversation as resolved.
Show resolved Hide resolved
contains_blocks = walk(decl, Fortran2003.Type_Bound_Procedure_Part)
if contains_blocks:
# First check there is only one CONTAINS section.
if len(contains_blocks) > 1:
raise NotImplementedError(
"Derived-type definition contains multiple CONTAINS "
"statements.")
# Get it.
contains = contains_blocks[0]
# Check that it's indeed a CONTAINS statement.
if walk(contains, Fortran2003.Contains_Stmt) is None:
raise NotImplementedError(
"Derived-type definition contains a procedure "
"without a CONTAINS statement.")
# Get all procedures in the CONTAINS section.
procedures = walk(contains, Fortran2003.Specific_Binding)
if procedures is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def test_goceankernelmetadata_create1(fortran_reader):
symbol._datatype = REAL_TYPE
with pytest.raises(InternalError) as info:
_ = GOceanKernelMetadata.create_from_psyir(symbol)
assert ("Expected kernel metadata to be stored in the PSyIR as an "
"UnsupportedFortranType, but found ScalarType." in str(info.value))
assert ("Expected kernel metadata to be stored in the PSyIR as a "
"StructureType, but found ScalarType." in str(info.value))


# create_from_fortran_string
Expand Down
8 changes: 6 additions & 2 deletions src/psyclone/tests/psyir/backend/fortran_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ def test_gen_typedecl_validation(fortran_writer, monkeypatch):
tsymbol = DataTypeSymbol("my_type", UnresolvedType())
with pytest.raises(VisitorError) as err:
fortran_writer.gen_typedecl(tsymbol)
assert ("Local Symbol 'my_type' is of UnresolvedType and therefore no "
"declaration can be created for it." in str(err.value))
assert ("gen_typedecl expects a DataTypeSymbol with a StructureType "
"as its datatype but got: 'UnresolvedType'" in str(err.value))


def test_gen_typedecl_unsupported_fortran_type(fortran_writer):
Expand Down Expand Up @@ -760,6 +760,10 @@ def test_fw_gen_vardecl_visibility(fortran_writer):
"end type var\n")


def test_fw_gen_proceduredecl(fortran_writer):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test be part of fortran_gen_decls_test.py rather than in this file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of gen_vardecl, gen_typedecl, etc. tests are in this file, so I did the same. But indeed, they should probably all be moved there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we're gradually splitting things up but since moving and editing things at the same time can be hard to review, it's slow going. However, as this is a new test, please could you put it in fortran_gen_decls_test.py.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pass


def test_gen_default_access_stmt(fortran_writer):
'''
Tests for the gen_default_access_stmt method of FortranWriter.
Expand Down
10 changes: 10 additions & 0 deletions src/psyclone/tests/psyir/symbols/data_type_symbol_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def test_create_datatypesymbol_wrong_datatype():
sym.datatype = "integer"
assert ("datatype of a DataTypeSymbol must be specified using a "
"DataType but got: 'str'" in str(err.value))


def test_create_datatypesymbol_wrong_is_class():
''' Check that attempting to specify the is_class attribute of a
DataTypeSymbol with an invalid type results in the expected error. '''
sym = DataTypeSymbol("my_type", UnresolvedType())
with pytest.raises(TypeError) as err:
sym.is_class = "integer"
assert ("The is_class attribute of a DataTypeSymbol must be a bool "
"but got: 'str'" in str(err.value))


def test_datatypesymbol_copy():
Expand Down
Loading