[Fortran, Patch, Teams, 1/5] Unify handling of STAT= and ERRMSG= optional arguments [PR87939]

2025-04-10 Thread Andre Vehreschild
Hi all,

this patch unifies handling of STAT= and ERRMSG= for some users of these
optional arguments. The first introduction of the arguments seems to stem from
SYNC, which choose the rule name sync_stat in the Fortran grammar. Therefore I
named the structure and participating routines the same. The patch allows for
easy parsing, resolving and translating these arguments. There is also an
implementation on how to make use of this for functions like MOVE_ALLOC(). The
following patches make use sync_stat in the teams statements.

Regtested ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From b329c2d35cbc4a5ecf0445811f1236ef3c9e9611 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild 
Date: Fri, 14 Mar 2025 14:20:18 +0100
Subject: [PATCH 1/6] Fortran: Unify handling of STAT= and ERRMSG= optional
 arguments [PR87939]

In preparing F2018 Teams handling improvements, unify handling of STAT=
and ERRMSG= optional arguments.  Handling of stat and errmsg in most
teams statements is corrected in the next patch.

Implement stat and errmsg for move_alloc () to comply with F2018.

	PR fortran/87939

gcc/fortran/ChangeLog:

	* check.cc (gfc_check_move_alloc): Add stat and errmsg to
	move_alloc.
	* dump-parse-tree.cc (show_sync_stat): New helper function.
	(show_code_node): Use show_sync_stat to print stat and errmsg.
	* gfortran.h (struct sync_stat): New struct to unify stat and
	errmsg handling.
	* intrinsic.cc (add_subroutines): Correct signature of
	move_alloc.
	* intrinsic.h (gfc_check_move_alloc): Correct signature of
	check_move_alloc.
	* match.cc (match_named_arg): Match an optional argument to a
	statement.
	(match_stat_errmsg): Match a stat= or errmsg= named argument.
	(gfc_match_critical): Use match_stat_errmsg to match the named
	arguments.
	(gfc_match_sync_team): Same.
	* resolve.cc (resolve_team_argument): Resolve an expr to have
	type TEAM_TYPE from iso_fortran_env.
	(resolve_scalar_variable_as_arg): Resolve an argument as a
	scalar type.
	(resolve_sync_stat): Resolve stat and errmsg expressions.
	(resolve_sync_team): Resolve a sync team statement using
	sync_stat helper.
	(resolve_end_team): Same.
	(resolve_critical): Same.
	* trans-decl.cc (gfc_build_builtin_function_decls): Correct
	sync_team signature.
	* trans-intrinsic.cc (conv_intrinsic_move_alloc): Store stat
	an errmsg optional arguments in helper struct and use helper
	to translate.
	* trans-stmt.cc (trans_exit): Implement DRY pattern for
	generating an _exit().
	(gfc_trans_sync_stat): Translate stat and errmsg contents.
	(gfc_trans_end_team): Use helper to translate stat and errmsg.
	(gfc_trans_sync_team): Same.
	(gfc_trans_critical): Same.
	* trans-stmt.h (gfc_trans_sync_stat): New function.
	* trans.cc (gfc_deallocate_with_status): Parameterize check at
	runtime to allow unallocated (co-)array when freeing a
	structure.
	(gfc_deallocate_scalar_with_status): Same and also add errmsg.
	* trans.h (gfc_deallocate_with_status): Signature changes.
	(gfc_deallocate_scalar_with_status): Same.

libgfortran/ChangeLog:

	* caf/single.c (_gfortran_caf_lock): Correct stat value, if
	lock is already locked by current image.
	(_gfortran_caf_unlock): Correct stat value, if lock is not
	locked.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray_critical_2.f90: New test.
	* gfortran.dg/coarray_critical_3.f90: New test.
	* gfortran.dg/team_sync_1.f90: New test.
	* gfortran.dg/move_alloc_11.f90: New test.
---
 gcc/fortran/check.cc  |  12 +-
 gcc/fortran/dump-parse-tree.cc|  23 ++-
 gcc/fortran/gfortran.h|   9 +
 gcc/fortran/intrinsic.cc  |  10 +-
 gcc/fortran/intrinsic.h   |   3 +-
 gcc/fortran/match.cc  | 121 ++--
 gcc/fortran/resolve.cc|  52 -
 gcc/fortran/trans-decl.cc |   8 +-
 gcc/fortran/trans-intrinsic.cc|  61 --
 gcc/fortran/trans-stmt.cc | 186 --
 gcc/fortran/trans-stmt.h  |   1 +
 gcc/fortran/trans.cc  |  46 +++--
 gcc/fortran/trans.h   |  11 +-
 .../gfortran.dg/coarray_critical_2.f90|  30 +++
 .../gfortran.dg/coarray_critical_3.f90|  32 +++
 gcc/testsuite/gfortran.dg/move_alloc_11.f90   |  23 +++
 gcc/testsuite/gfortran.dg/team_sync_1.f90 |  24 +++
 libgfortran/caf/single.c  |   8 +-
 18 files changed, 532 insertions(+), 128 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_critical_2.f90
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_critical_3.f90
 create mode 100644 gcc/testsuite/gfortran.dg/move_alloc_11.f90
 create mode 100644 gcc/testsuite/gfortran.dg/team_sync_1.f90

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index 9c66c25e059..00342787a51 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.

[Fortran, Patch, Team, 3/5] Update get_team, team_number and image_status to F2018 [PR88154, PR88960, PR97210, PR103001]

2025-04-10 Thread Andre Vehreschild
Hi all,

attached patch reworks GET_TEAM(), TEAM_NUMBER() and IMAGE_STATUS() to adhere
to the Fortran 2018 as much as possible and to play nicely with the previous
patch of the TEAM statements.

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From ace6832bd6afaa766a43e33ff932714200f1fd09 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild 
Date: Mon, 7 Apr 2025 15:12:09 +0200
Subject: [PATCH 3/6] Fortran: Update get_team, team_number and image_status to
 F2018 [PR88154, PR88960, PR97210, PR103001]

Add functions get_team() and team_number() to comply with F2018
standard.

Update image_status() to comply with F2018 standard.

	PR fortran/88154
	PR fortran/88960
	PR fortran/97210
	PR fortran/103001

gcc/fortran/ChangeLog:

	* check.cc (team_type_check): Check a type for being team_type
	from the iso_fortran_env module.
	(gfc_check_image_status): Use team_type check.
	(gfc_check_get_team): Check for level argument.
	(gfc_check_team_number): Use team_type check.
	* expr.cc (gfc_check_assign): Add treatment for returning
	team_type in caf-single mode.
	* gfortran.texi: Add/Update documentation for get_team and
	team_number API functions.
	* intrinsic.cc (add_functions): Update get_team signature.
	* intrinsic.h (gfc_resolve_get_team): Add prototype.
	* intrinsic.texi: Add/Update documentation for get_team and
	team_number Fortran functions.
	* iresolve.cc (gfc_resolve_get_team): Resolve return type to be
	of type team_type.
	* iso-fortran-env.def: Update STAT_LOCK constants.  They have
	nothing to do with files.  Add level constants for get_team.
	* libgfortran.h: Add level and unlock_stat constants.
	* simplify.cc (gfc_simplify_get_team): Simply to correct return
	type team_type.
	* trans-decl.cc (gfc_build_builtin_function_decls): Update
	get_team and image_status API prototypes to correct signatures.
	* trans-intrinsic.cc (conv_intrinsic_image_status): Translate
	second parameter correctly.
	(conv_intrinsic_team_number): Translate optional single team
	argument correctly.
	(gfc_conv_intrinsic_function): Add translation of get_team.

libgfortran/ChangeLog:

	* caf/libcaf.h: Add constants for get_team's level argument and
	update stat values for failed images.
	(_gfortran_caf_team_number): Add prototype.
	(_gfortran_caf_get_team): Same.
	* caf/single.c (_gfortran_caf_team_number): Get the given team's
	team number.
	(_gfortran_caf_get_team): Get the current team or the team given
	by level when the argument is present.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/image_status_1.f08: Correct check for
	team_type.
	* gfortran.dg/pr102458.f90: Adapt to multiple errors.
	* gfortran.dg/coarray/get_team_1.f90: New test.
	* gfortran.dg/team_get_1.f90: New test.
	* gfortran.dg/team_number_1.f90: Correct Fortran syntax.
---
 gcc/fortran/check.cc  |  65 +-
 gcc/fortran/expr.cc   |   8 +-
 gcc/fortran/gfortran.texi |  50 
 gcc/fortran/intrinsic.cc  |   8 +-
 gcc/fortran/intrinsic.h   |   2 +-
 gcc/fortran/intrinsic.texi| 112 ++
 gcc/fortran/iresolve.cc   |  22 +++-
 gcc/fortran/iso-fortran-env.def   |  22 ++--
 gcc/fortran/libgfortran.h |  10 +-
 gcc/fortran/simplify.cc   |   6 +-
 gcc/fortran/trans-decl.cc |  14 +--
 gcc/fortran/trans-intrinsic.cc|  21 +---
 .../gfortran.dg/coarray/get_team_1.f90|  29 +
 .../gfortran.dg/coarray/image_status_1.f08|   2 +-
 gcc/testsuite/gfortran.dg/pr102458.f90|   2 +-
 gcc/testsuite/gfortran.dg/team_get_1.f90  |  27 +
 gcc/testsuite/gfortran.dg/team_number_1.f90   |   6 +-
 libgfortran/caf/libcaf.h  |  13 +-
 libgfortran/caf/single.c  |  26 
 19 files changed, 367 insertions(+), 78 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/get_team_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/team_get_1.f90

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index 00342787a51..a1c3de3e80d 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -1809,6 +1809,23 @@ gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
   return gfc_check_atomic (atom, 1, value, 0, stat, 2);
 }

+bool
+team_type_check (gfc_expr *e, int n)
+{
+  if (e->ts.type != BT_DERIVED || !e->ts.u.derived
+  || e->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
+  || e->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
+{
+  gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
+		 "% from the intrinsic module "
+		 "%",
+		 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
+		 &e->where);
+  return false;
+}
+
+  return true;
+}

 bool
 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
@@ -1

[Fortran, Patch, Teams, 0/5] Improve on Fortran 2018 teams support

2025-04-10 Thread Andre Vehreschild
Hi all,

I again have a series of patches. This time to improve the teams support in
gfortran.

1/5: Improves/Unifies handling of STAT= and ERRMSG= handling, which is part of
all TEAM statements. I wanted to prevent repeating myself over and over so I
factored this out (DRY principle). Because the standard's rule name for this is
sync_stat the structure to store the information in gfc_code is named like that.

2/5: Rework (FORM|CHANGE|END|SYNC) TEAM and CRITICAL to use sync_stat and
adhere to F2018 standard as much as possible. Because CHANGE TEAM has kind of
an association list (but for coarrays only), I choose to factor out that
parsing and other preparations from ASSOCIATE. Added support to caf_single for
testing.

3/5: Update/Implement get_team()/team_number() and image_status() parsing and
also add testcases as well as support in caf_single.

4/5: Update this_image() parsing and treatment as well as adding testcases and
support in caf_single.

5/5: Update image_index() and num_images() support also in caf_single.

All patches together have been bootstrapped and regtested ok on
x86_64-pc-linux-gnu.

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de


[Fortran, Patch, Team, 4/5] Add team-support to this_image [PR87326]

2025-04-10 Thread Andre Vehreschild
Hi all,

attached patch reworks the implementation of THIS_IMAGE() to adhere as much as
possible to the Fortran 2018 standard.

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From c6da27813d7a7622cae2663af7e25d21e7a34661 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild 
Date: Tue, 1 Apr 2025 12:17:43 +0200
Subject: [PATCH 4/6] Fortran: Add team-support to this_image [PR87326]

This_image() no longer has a distance formal argument, but a team one.
The source of the distance argument could not be identified, i.e.
whether it came from a TS or standard draft.  To implement only the
standard it is removed.  Besides being defined, it was not used anyway.

	PR fortran/87326

gcc/fortran/ChangeLog:

	* check.cc (gfc_check_this_image): Check the three different
	parameter lists possible for this_image and sort them correctly.
	* gfortran.texi: Update documentation on this_image's API.
	* intrinsic.cc (add_functions): Update this_image's signature.
	(check_specific): Add specific check for this_image.
	* intrinsic.h (gfc_check_this_image): Change to flexible
	argument list.
	* intrinsic.texi: Update documentation on this_image().
	* iresolve.cc (gfc_resolve_this_image): Resolve the different
	arguments.
	* simplify.cc (gfc_simplify_this_image): Simplify the simplify
	routine.
	* trans-decl.cc (gfc_build_builtin_function_decls): Update
	signature of this_image.
	* trans-expr.cc (gfc_caf_get_image_index): Use correct signature
	of this_image.
	* trans-intrinsic.cc (trans_this_image): Adapt to correct
	signature.

libgfortran/ChangeLog:

	* caf/libcaf.h (_gfortran_caf_this_image): Correct prototype.
	* caf/single.c (struct caf_single_team): Add new_index of image.
	(_gfortran_caf_this_image): Return the image index in the given team.
	(_gfortran_caf_form_team): Set new_index in team structure.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray_10.f90: Update error messages.
	* gfortran.dg/coarray_lib_this_image_1.f90: Same.
	* gfortran.dg/coarray_lib_this_image_2.f90: Same.
	* gfortran.dg/coarray_this_image_1.f90: Add more tests and
	remove incorrect ones.
	* gfortran.dg/coarray_this_image_2.f90: Test more features.
	* gfortran.dg/coarray_this_image_3.f90: New test.
---
 gcc/fortran/check.cc  | 122 --
 gcc/fortran/gfortran.texi |  16 ++-
 gcc/fortran/intrinsic.cc  |  12 +-
 gcc/fortran/intrinsic.h   |   2 +-
 gcc/fortran/intrinsic.texi|  30 ++---
 gcc/fortran/iresolve.cc   |  23 +++-
 gcc/fortran/simplify.cc   |   7 +-
 gcc/fortran/trans-decl.cc |   6 +-
 gcc/fortran/trans-expr.cc |   6 +-
 gcc/fortran/trans-intrinsic.cc|  39 +++---
 gcc/testsuite/gfortran.dg/coarray_10.f90  |   2 +-
 .../gfortran.dg/coarray_lib_this_image_1.f90  |   2 +-
 .../gfortran.dg/coarray_lib_this_image_2.f90  |   2 +-
 .../gfortran.dg/coarray_this_image_1.f90  |  49 ++-
 .../gfortran.dg/coarray_this_image_2.f90  |  52 +++-
 .../gfortran.dg/coarray_this_image_3.f90  |  34 +
 libgfortran/caf/libcaf.h  |   2 +-
 libgfortran/caf/single.c  |  13 +-
 18 files changed, 290 insertions(+), 129 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_this_image_3.f90

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index a1c3de3e80d..c27f653d3b0 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -6665,75 +6665,115 @@ gfc_check_team_number (gfc_expr *team)


 bool
-gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
+gfc_check_this_image (gfc_actual_arglist *args)
 {
+  gfc_expr *coarray, *dim, *team, *cur;
+
+  coarray = dim = team = NULL;
+
   if (flag_coarray == GFC_FCOARRAY_NONE)
 {
   gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
   return false;
 }

-  if (coarray == NULL && dim == NULL && distance == NULL)
+  /* Shortcut when no arguments are given.  */
+  if (!args->expr && !args->next->expr && !args->next->next->expr)
 return true;

-  if (dim != NULL && coarray == NULL)
-{
-  gfc_error ("DIM argument without COARRAY argument not allowed for "
-		 "THIS_IMAGE intrinsic at %L", &dim->where);
-  return false;
-}
+  cur = args->expr;

-  if (distance && (coarray || dim))
+  if (cur)
 {
-  gfc_error ("The DISTANCE argument may not be specified together with the "
-		 "COARRAY or DIM argument in intrinsic at %L",
-		 &distance->where);
-  return false;
+  gfc_push_suppress_errors ();
+  if (coarray_check (cur, 0))
+	coarray = cur;
+  else if (scalar_check (cur, 2) && team_type_check (cur, 2))
+	team = cur;
+  else
+	{
+	  gfc_pop_suppress_errors ();
+	  gfc_error ("First argument of % intrinsic at %L must be "
+		 "a coarray "
+		 "va

Re: GSoC[Fortran Runtime argument check ] Draft of Proposal and some doubts about the needs

2025-04-10 Thread Gwen Fu
Thank you for your detailed explanation of  "dummy parameter"  !

>It is still unclear to me what you are trying to accomplish.
>Implicit typying and implicit interfaces are a compile-time
>thing.
...
>An -fcheck=implicit-type option that generates a runtime
>error that does not make sense to me unless the aim is
>to slow down the compile code.  The above code would be
>transformed into something like (ignoring passing convention).
>float
>foo(float x)
>{
>  if (runtime_option & fcheck_implicit_type)
 >runtime_error("'foo' has an implicit type");

>  if (runtime_option & fcheck_implicit_type)
>runtime_error("'x' has an implicit type");

>  return (x * x);
>}

Here  is the description of the GSoC project :
*Fortran – run-time argument checking.* – In particular older Fortran code,
which does not use modules, but also code which uses implicit-size or
explicit-size arrays is prone to argument mismatches. The goal of this item
is to *add an optional run-time test* which works by storing the
argument-type/size data before the call in a global variable – and check
against it in the callee. (A pointer to the called function is stored
alongside to permit calls from uninstrumented code to instrumented code.)
This project would/could be mentored by Tobias Burnus. Required skills
include C/C++; some knowledge of Fortran helps, but is not needed. *Difficulty
medium, size: 175 hours (medium)*.

"older Fortran code "may indicate the version Fortran 77
I try to regard "an optional run-time test" as a compile option like what
you said "-fcheck=implicit-type" . Is there any other understanding?
(Maybe , the test is "check_implicit_type" , the code you showed to me
above))

Although the optimized code will reduce the code running efficiency, I feel
that this project should have its place!😄😄


[Fortran, Patch, Teams, 5/5] Add teams support in image_index and num_images for F2018

2025-04-10 Thread Andre Vehreschild
Hi all,

attached patch reworks the NUM_IMAGES() implementation to adhere to the Fortran
2018 standard.

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From 1d0262dc068f4c6018d669a88387dbb7baaff39a Mon Sep 17 00:00:00 2001
From: Andre Vehreschild 
Date: Thu, 3 Apr 2025 10:11:50 +0200
Subject: [PATCH 5/6] Fortran: Add teams support in image_index and num_images
 for F2018

This more or less completes the set of functions that are affected by
teams.

gcc/fortran/ChangeLog:

	* check.cc (gfc_check_image_index): Check for team or
	team_number correctnes.
	(gfc_check_num_images): Same.
	* gfortran.texi: Update documentation on num_images' API
	function.
	* intrinsic.cc (add_functions): Update signature of image_index
	and num_images.  Both can take either a team handle or number.
	* intrinsic.h (gfc_check_num_images): Update signature to take
	either team or team_number.
	(gfc_check_image_index): Can take coarray, subscripts and team
	or team number now.
	(gfc_simplify_image_index): Same.
	(gfc_simplify_num_images): Same.
	(gfc_resolve_image_index): Same.
	* intrinsic.texi: Update documentation of num_images() Fortran
	function.
	* iresolve.cc (gfc_resolve_image_index): Update signature.
	* simplify.cc (gfc_simplify_num_images): Update signature and
	remove undocumented failed argument.
	(gfc_simplify_image_index): Add team or team number argument.
	* trans-intrinsic.cc (conv_stat_and_team): Because being
	optional teams need to be a pointer to the opaque pointer.
	(conv_caf_sendget): Correct call; was two arguments short.
	(trans_image_index): Support team or team_number.
	(trans_num_images): Same.
	(conv_intrinsic_cobound): Adapt to changed signature of
	num_images in call.
	* trans-stmt.cc (gfc_trans_sync): Same.

libgfortran/ChangeLog:

	* caf/libcaf.h (_gfortran_caf_num_images): Correct prototype.
	* caf/single.c (_gfortran_caf_num_images): Default
	implementation.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray_49.f90: Adapt to changed error message.
	* gfortran.dg/coarray_collectives_12.f90: Adapt to changed
	function signature of num_images.
	* gfortran.dg/coarray_collectives_16.f90: Same.
	* gfortran.dg/coarray_lib_this_image_1.f90: Same.
	* gfortran.dg/coarray_lib_this_image_2.f90: Same.
	* gfortran.dg/coarray_this_image_1.f90: Adapt tests for
	num_images.
	* gfortran.dg/coarray_this_image_2.f90: Same.
	* gfortran.dg/coarray_this_image_3.f90: Same.
	* gfortran.dg/num_images_1.f90: Check that deprecated syntax is
	no longer supported.
---
 gcc/fortran/check.cc  | 63 --
 gcc/fortran/gfortran.texi | 26 
 gcc/fortran/intrinsic.cc  | 55 
 gcc/fortran/intrinsic.h   | 10 +--
 gcc/fortran/intrinsic.texi| 45 ++---
 gcc/fortran/iresolve.cc   |  3 +-
 gcc/fortran/simplify.cc   | 12 +---
 gcc/fortran/trans-intrinsic.cc| 66 +++
 gcc/fortran/trans-stmt.cc |  3 +-
 gcc/testsuite/gfortran.dg/coarray_49.f90  |  2 +-
 .../gfortran.dg/coarray_collectives_12.f90|  2 +-
 .../gfortran.dg/coarray_collectives_16.f90|  2 +-
 .../gfortran.dg/coarray_lib_this_image_1.f90  |  2 +-
 .../gfortran.dg/coarray_lib_this_image_2.f90  |  2 +-
 .../gfortran.dg/coarray_this_image_1.f90  | 14 ++--
 .../gfortran.dg/coarray_this_image_2.f90  | 16 ++---
 .../gfortran.dg/coarray_this_image_3.f90  | 15 ++---
 gcc/testsuite/gfortran.dg/num_images_1.f90|  2 +-
 libgfortran/caf/libcaf.h  |  2 +-
 libgfortran/caf/single.c  |  4 +-
 20 files changed, 171 insertions(+), 175 deletions(-)

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index c27f653d3b0..356e0d7f678 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -6565,7 +6565,8 @@ gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)


 bool
-gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
+gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub,
+		   gfc_expr *team_or_team_number)
 {
   mpz_t nelems;

@@ -6585,12 +6586,8 @@ gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
   return false;
 }

-  if (sub->ts.type != BT_INTEGER)
-{
-  gfc_error ("Type of %s argument of IMAGE_INDEX at %L shall be INTEGER",
-		 gfc_current_intrinsic_arg[1]->name, &sub->where);
-  return false;
-}
+  if (!type_check (sub, 1, BT_INTEGER))
+return false;

   if (gfc_array_size (sub, &nelems))
 {
@@ -6605,12 +6602,23 @@ gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
   mpz_clear (nelems);
 }

+  if (team_or_team_number)
+{
+  if (!type_check2 (team_or_team_number, 2, BT_DERIVED, BT_INTEGER)
+	  || !scalar_check (team_or_team_number, 2))
+	return false;
+
+  /* Check team is of team_type.  */
+  if (team_

[Fortran, Patch, Team, 2/5] Improve F2018 TEAM handling [PR87326, PR87556, PR88254, PR103896]

2025-04-10 Thread Andre Vehreschild
Hi all,

attached patch reworks (FORM|CHANGE|END|SYNC) TEAM to implement the Fortran
2018 as much as possible. This work has been done in sync with enhancing the
OpenCoarrays library. Because CHANGE TEAM has an association list very similar
to ASSOCIATE, the common code has been factored out to implement the DRY
principle.

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From 64c951fd2f64f5d4407076532fd57e8370254826 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild 
Date: Mon, 7 Apr 2025 09:36:24 +0200
Subject: [PATCH 2/6] Fortran: Improve F2018 TEAM handling [PR87326, PR87556,
 PR88254, PR103896]

Improve the implementation of F2018 TEAM handling routines. Add
runtime-functions to caf_single to allow testing.

	PR fortran/87326
	PR fortran/87556
	PR fortran/88254
	PR fortran/103796

gcc/fortran/ChangeLog:

	* coarray.cc (split_expr_at_caf_ref): Treat polymorphic types
	correctly.  Ensure resolve of expression after coindex.
	(create_allocated_callback): Fix parameter of allocated function
	for coarrays.
	(coindexed_expr_callback): Improve detection of coarrays in
	allocated function.
	* decl.cc (gfc_match_end): Add team block matching.
	* dump-parse-tree.cc (show_code_node): Dump change team block as
	such.
	* frontend-passes.cc (gfc_code_walker): Recognice team block.
	* gfortran.texi: Add documentation for team api functions.
	* intrinsic.texi: Add documentation about team_type in
	iso_fortran_env module.
	* iso-fortran-env.def (team_type): Use helper to get pointer
	kind.
	* match.cc (gfc_match_associate): Factor out matching of
	association list, because it is used in change team as well.
	(check_coarray_assoc): Ensure, that the association is to a
	coarray.
	(match_association_list): Match a list of association either in
	associate or in change team.
	(gfc_match_form_team): Match form team correctly include
	new_index.
	(gfc_match_change_team): Match change team with association
	list.
	(gfc_match_end_team): Match end team including stat and errmsg.
	(gfc_match_return): Prevent return from team block.
	* parse.cc (decode_statement): Sort team block.
	(next_statement): Same.
	(check_statement_label): Same.
	(accept_statement): Same.
	(verify_st_order): Same.
	(parse_associate): Renamed to move_associates_to_block...
	(move_associates_to_block): ... to enable reuse for change team.
	(parse_change_team): Parse it as block.
	(parse_executable): Same.
	* parse.h (enum gfc_compile_state): Add team block as compiler
	state.
	* resolve.cc (resolve_scalar_argument): New function to resolve
	an argument to a statement as a scalar.
	(resolve_form_team): Resolve its members.
	(resolve_change_team): Same.
	(resolve_branch): Prevent branch from jumping out of team block.
	(check_team): Removed.
	* trans-decl.cc (gfc_build_builtin_function_decls): Add stat and
	errmsg to team API functions and update their arguments.
	* trans-expr.cc (gfc_trans_subcomponent_assign): Also null the
	token when moving memory or an allocated() will not detect a
	free.
	* trans-intrinsic.cc (gfc_conv_intrinsic_caf_is_present_remote):
	Adapt to signature change no longer a pointer-pointer.
	* trans-stmt.cc (gfc_trans_form_team): Translate a form team
	including new_index.
	(gfc_trans_change_team): Translate a change team as a block.

libgfortran/ChangeLog:

	* caf/libcaf.h: Remove commented block.
	(_gfortran_caf_form_team): Allow for all relevant arguments.
	(_gfortran_caf_change_team): Same.
	(_gfortran_caf_end_team): Same.
	(_gfortran_caf_sync_team): Same.
	* caf/single.c (struct caf_single_team): Team handling
	structures.
	(_gfortran_caf_init): Initialize initial team.
	(free_team_list): Free all teams and the memory they hold.
	(_gfortran_caf_finalize): Free initial and sibling teams.
	(_gfortran_caf_register): Add memory registered to current team.
	(_gfortran_caf_deregister): Unregister memory from current team.
	(_gfortran_caf_is_present_on_remote): Check token's memptr for
	llocation.  May have been deallocated by an end team.
	(_gfortran_caf_form_team): Push a new team stub to the list.
	(_gfortran_caf_change_team): Push a formed team on top of the
	ctive teams stack.
	(_gfortran_caf_end_team): End the active team, free all memory
	allocated during its livespan.
	(_gfortran_caf_sync_team): Take stat and errmsg into account.

gcc/testsuite/ChangeLog:

	* gfortran.dg/team_change_2.f90: New test.
	* gfortran.dg/team_change_3.f90: New test.
	* gfortran.dg/team_end_2.f90: New test.
	* gfortran.dg/team_end_3.f90: New test.
	* gfortran.dg/team_form_2.f90: New test.
	* gfortran.dg/team_form_3.f90: New test.
	* gfortran.dg/team_sync_2.f90: New test.
---
 gcc/fortran/coarray.cc  |  12 +-
 gcc/fortran/decl.cc |  20 +-
 gcc/fortran/dump-parse-tree.cc  |  30 ++-
 gcc/fortran/frontend-passes.cc  |   1 +
 gcc/fortran/gfortran.texi   | 146 +-
 gcc/fortran/intrinsic.texi