Re: [Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541]

2021-10-11 Thread Harald Anlauf via Gcc-patches

Hi Tobias,

Am 01.10.21 um 02:43 schrieb Tobias Burnus:

Hi all,

this patch fixes a bunch of issues with CLASS.

  * * *


besides my previous minor remarks I do not have further comments.
I played around a little but did not find any (new) obstacles.


OK for mainline?


OK from my side.

Thanks for the patch!

Harald


Tobias

-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201,
80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer:
Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München;
Registergericht München, HRB 106955





PING**2 - (Re: [Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541])

2021-10-11 Thread Tobias Burnus

PING**2

On 06.10.21 12:24, Tobias Burnus wrote:

Early ping for this patch.

I do note that Harald browsed the patch (thanks!) and had two remarks,
cf. https://gcc.gnu.org/pipermail/gcc-patches/2021-October/581256.html
(I intent to fix the two nits – as mentioned in the reply to Harald's
email.)

(I still plan to review Harald's pending patch soon, unless someone
beats me:
https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580810.html )


Jerry was faster – thanks for that review!

Tobias


On 01.10.21 02:43, Tobias Burnus wrote:

Hi all,

this patch fixes a bunch of issues with CLASS.

 * * *

Side remark: I disliked the way CLASS is represented when it was
introduced;
when writing the testcase for this PR and kept fixing the testcase
fallout,
I started to hate it!
I am sure that there are more issues – but I tried hard not too look
closer
at surrounding code to avoid hitting more issues.
(If you look for a project, I think if you put attributes on separate
lines,
like a separate "POINTER :: var" line, you have a high chance to hit the
error.)

 * * *

What I found rather puzzling is that the 'optional' argument could be
either
on sym->attr.optional or on CLASS_DATA (sym)->attr.optional. I think one
occurs for 'foo2' and the other for 'foo4' - not that I understand
why it
differs.

I think it is otherwise straight forward. Regarding the original issue:

In order to detect an assumed-size argument to an assumed-rank array,
the last dimension has 'ubound = -1' to indicate an assume-size array;
for those size(x, dim=rank(x)-1) == -1 and size(x) < 0

However, when the dummy argument (and hence: actual argument) is either
a pointer or an allocatable, the bound is passed as is (in particular,
"-1" is a valid ubound and size(x) >= 0). – However, if the actual
argument is unallocated/not associated, rank(var) still is supposed to
work - hence, it has to be set.

The last two items did work before - but not for CLASS -> CLASS.
Additionally,
the ubound = -1 had an issue for CLASS -> TYPE as the code assumed that
expr->ref is the whole array ("var(full-array-ref)") but for CLASS the
expr->ref is a component and only expr->ref->next is the array ref.
("var%_data(full-array-ref)").

OK for mainline?

Tobias


-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955


Re: [Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541]

2021-10-11 Thread Tobias Burnus

Hi Harald,

On 10.10.21 21:27, Harald Anlauf via Fortran wrote:

just some random remarks from initially browsing your patch.

Thanks for browsing the patch :-)

- leftover from debugging?

Yes.

- code that could be shortened/made slightly more readable:
...
Is there a reason to not use strcmp (comp->name, "_data") == 0?


Just (pre-mature) optimization. I think the latter is clearer; I will
change it.

Tobias


-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955


Re: [Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541]

2021-10-10 Thread Harald Anlauf via Gcc-patches

Hi Tobias,

just some random remarks from initially browsing your patch.

- leftover from debugging?

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 1c24556c299..8a82e55d1f9 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -1,3 +1,4 @@
+#pragma GCC optimize(0)

- code that could be shortened/made slightly more readable:

@@ -2723,7 +2728,13 @@ gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
else
  {
codimension = comp->attr.codimension;
-   pointer = comp->attr.pointer;
+   if (expr->ts.type == BT_CLASS
+   && comp->name[0] == '_' && comp->name[1] == 'd'
+   && comp->name[2] == 'a' && comp->name[3] == 't'
+   && comp->name[4] == 'a' && comp->name[5] == '\0')

Is there a reason to not use strcmp (comp->name, "_data") == 0?

Cheers,
Harald

Am 01.10.21 um 02:43 schrieb Tobias Burnus:

Hi all,

this patch fixes a bunch of issues with CLASS.

  * * *

Side remark: I disliked the way CLASS is represented when it was
introduced;
when writing the testcase for this PR and kept fixing the testcase fallout,
I started to hate it!
I am sure that there are more issues – but I tried hard not too look closer
at surrounding code to avoid hitting more issues.
(If you look for a project, I think if you put attributes on separate
lines,
like a separate "POINTER :: var" line, you have a high chance to hit the
error.)

  * * *

What I found rather puzzling is that the 'optional' argument could be
either
on sym->attr.optional or on CLASS_DATA (sym)->attr.optional. I think one
occurs for 'foo2' and the other for 'foo4' - not that I understand why it
differs.

I think it is otherwise straight forward. Regarding the original issue:

In order to detect an assumed-size argument to an assumed-rank array,
the last dimension has 'ubound = -1' to indicate an assume-size array;
for those size(x, dim=rank(x)-1) == -1 and size(x) < 0

However, when the dummy argument (and hence: actual argument) is either
a pointer or an allocatable, the bound is passed as is (in particular,
"-1" is a valid ubound and size(x) >= 0). – However, if the actual
argument is unallocated/not associated, rank(var) still is supposed to
work - hence, it has to be set.

The last two items did work before - but not for CLASS -> CLASS.
Additionally,
the ubound = -1 had an issue for CLASS -> TYPE as the code assumed that
expr->ref is the whole array ("var(full-array-ref)") but for CLASS the
expr->ref is a component and only expr->ref->next is the array ref.
("var%_data(full-array-ref)").

OK for mainline?

Tobias

-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201,
80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer:
Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München;
Registergericht München, HRB 106955




PING - (Re: [Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541])

2021-10-06 Thread Tobias Burnus

Early ping for this patch.

(I still plan to review Harald's pending patch soon, unless someone
beats me:
https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580810.html )

On 01.10.21 02:43, Tobias Burnus wrote:

Hi all,

this patch fixes a bunch of issues with CLASS.

 * * *

Side remark: I disliked the way CLASS is represented when it was
introduced;
when writing the testcase for this PR and kept fixing the testcase
fallout,
I started to hate it!
I am sure that there are more issues – but I tried hard not too look
closer
at surrounding code to avoid hitting more issues.
(If you look for a project, I think if you put attributes on separate
lines,
like a separate "POINTER :: var" line, you have a high chance to hit the
error.)

 * * *

What I found rather puzzling is that the 'optional' argument could be
either
on sym->attr.optional or on CLASS_DATA (sym)->attr.optional. I think one
occurs for 'foo2' and the other for 'foo4' - not that I understand why it
differs.

I think it is otherwise straight forward. Regarding the original issue:

In order to detect an assumed-size argument to an assumed-rank array,
the last dimension has 'ubound = -1' to indicate an assume-size array;
for those size(x, dim=rank(x)-1) == -1 and size(x) < 0

However, when the dummy argument (and hence: actual argument) is either
a pointer or an allocatable, the bound is passed as is (in particular,
"-1" is a valid ubound and size(x) >= 0). – However, if the actual
argument is unallocated/not associated, rank(var) still is supposed to
work - hence, it has to be set.

The last two items did work before - but not for CLASS -> CLASS.
Additionally,
the ubound = -1 had an issue for CLASS -> TYPE as the code assumed that
expr->ref is the whole array ("var(full-array-ref)") but for CLASS the
expr->ref is a component and only expr->ref->next is the array ref.
("var%_data(full-array-ref)").

OK for mainline?

Tobias


-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955


[Patch] Fortran: Various CLASS + assumed-rank fixed [PR102541]

2021-09-30 Thread Tobias Burnus

Hi all,

this patch fixes a bunch of issues with CLASS.

 * * *

Side remark: I disliked the way CLASS is represented when it was introduced;
when writing the testcase for this PR and kept fixing the testcase fallout,
I started to hate it!
I am sure that there are more issues – but I tried hard not too look closer
at surrounding code to avoid hitting more issues.
(If you look for a project, I think if you put attributes on separate lines,
like a separate "POINTER :: var" line, you have a high chance to hit the
error.)

 * * *

What I found rather puzzling is that the 'optional' argument could be either
on sym->attr.optional or on CLASS_DATA (sym)->attr.optional. I think one
occurs for 'foo2' and the other for 'foo4' - not that I understand why it
differs.

I think it is otherwise straight forward. Regarding the original issue:

In order to detect an assumed-size argument to an assumed-rank array,
the last dimension has 'ubound = -1' to indicate an assume-size array;
for those size(x, dim=rank(x)-1) == -1 and size(x) < 0

However, when the dummy argument (and hence: actual argument) is either
a pointer or an allocatable, the bound is passed as is (in particular,
"-1" is a valid ubound and size(x) >= 0). – However, if the actual
argument is unallocated/not associated, rank(var) still is supposed to
work - hence, it has to be set.

The last two items did work before - but not for CLASS -> CLASS. Additionally,
the ubound = -1 had an issue for CLASS -> TYPE as the code assumed that
expr->ref is the whole array ("var(full-array-ref)") but for CLASS the
expr->ref is a component and only expr->ref->next is the array ref.
("var%_data(full-array-ref)").

OK for mainline?

Tobias

-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955
Fortran: Various CLASS + assumed-rank fixed [PR102541]

Starting point was PR102541, were a previous patch caused an invalid
e->ref access for class. When testing, it turned out that for
CLASS to CLASS the code was never executed - additionally, issues
appeared for optional and a bogus error for -fcheck=all. In particular:

There were a bunch of issues related to optional CLASS, can have the
'attr.dummy' set in CLASS_DATA (sym) - but sometimes also in 'sym'!?!
Additionally, gfc_variable_attr could return pointer = 1 for nonpointers
when the expr is no longer "var" but "var%_data".

	PR fortran/102541

gcc/fortran/ChangeLog:

	* check.c (gfc_check_present): Handle optional CLASS.
	* interface.c (gfc_compare_actual_formal): Likewise.
	* trans-array.c (gfc_trans_g77_array): Likewise.
	* trans-decl.c (gfc_build_dummy_array_decl): Likewise.
	* trans-types.c (gfc_sym_type): Likewise.
	* primary.c (gfc_variable_attr): Fixes for dummy and
	pointer when 'class%_data' is passed.
	* trans-expr.c (set_dtype_for_unallocated, gfc_conv_procedure_call):
	For assumed-rank dummy, fix setting rank for dealloc/notassoc actual
	and setting ubound to -1 for assumed-size actuals.

gcc/testsuite/ChangeLog:

	* gfortran.dg/assumed_rank_24.f90: New test.

 gcc/fortran/check.c   |   4 +-
 gcc/fortran/interface.c   |   9 +-
 gcc/fortran/primary.c |  20 +++-
 gcc/fortran/trans-array.c |   4 +-
 gcc/fortran/trans-decl.c  |   3 +-
 gcc/fortran/trans-expr.c  |  81 ---
 gcc/fortran/trans-types.c |   3 +-
 gcc/testsuite/gfortran.dg/assumed_rank_24.f90 | 137 ++
 8 files changed, 213 insertions(+), 48 deletions(-)

diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c
index f31ad68053b..677209ee95e 100644
--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -4530,7 +4530,9 @@ gfc_check_present (gfc_expr *a)
   return false;
 }
 
-  if (!sym->attr.optional)
+  /* For CLASS, the optional attribute might be set at either location. */
+  if ((sym->ts.type != BT_CLASS || !CLASS_DATA (sym)->attr.optional)
+  && !sym->attr.optional)
 {
   gfc_error ("%qs argument of %qs intrinsic at %L must be of "
 		 "an OPTIONAL dummy variable",
diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c
index a2fea0e97b8..34a0fddffe2 100644
--- a/gcc/fortran/interface.c
+++ b/gcc/fortran/interface.c
@@ -3546,8 +3546,13 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
 		   "at %L", where);
 	  return false;
 	}
-  if (!f->sym->attr.optional
-	  || (in_statement_function && f->sym->attr.optional))
+  /* For CLASS, the optional attribute might be set at either location. */
+  if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
+	   && !f->sym->attr.optional)
+	  || (in_statement_function
+	  && (f->sym->attr.optional
+		  || (f->sym->ts.type == BT_CLASS
+