Skip to content

Commit c5fa210

Browse files
author
Paul Thomas
committed
Fortran: Generic processing of assumed rank objects (f202y) [PR116733]
2024-10-23 Paul Thomas <[email protected]> gcc/fortran PR fortran/116733 * array.cc : White space corrections. * expr.cc (gfc_check_pointer_assign): Permit assumed rank target with -std=f202y. Add constraints that the data pointer object must have rank remapping specified and the that the data target be contiguous. * gfortran.h : Add a gfc_array_ref field 'ar' to the structure 'gfc_association_list'. * interface.cc (gfc_compare_actual_formal): If -Wsurprising is set, emit a warning if an assumed size array is passed to an assumed rank dummy. * intrinsic.cc (do_ts29113_check): Permit an assumed rank arg. for reshape if -std=f202y and the argument is contiguous. * invoke.texi : Introduce -std=f202y. Whitespace errors. * lang.opt : Accept -std=f202y. * libgfortran.h : Define GFC_STD_F202Y. * match.cc (gfc_match_associate): If -std=f202y an assumed rank selector is allowed if it is contiguous and the associate name has rank remapping specified. * options.cc (gfc_init_options): -std=f202y is equivalent to -std=f2023 with experimental f202y features. White space issues * parse.cc (parse_associate): If the selector is assumed rank, use the 'ar' field of the association list to build an array specification. * primary.cc (gfc_match_varspec): Do not resolve the assumed rank selector of a class associate name at this stage to avoid the rank change. * resolve.cc (find_array_spec): If an array_ref dimension is -1 reset it with the rank in the object's array_spec. (gfc_expression_rank): Do not check dimen types for an assumed rank variable expression. (resolve_variable): Do not emit the assumed rank context error if the context is pointer assignment and the variable is a target. (resolve_assoc_var): Resolve the bounds and check for missing bounds in the rank remap of an associate name with an assumed rank selector. Do not correct the rank of an associate name with an assumed rank selector. (resolve_symbol): Allow the reference to an assumed rank object if -std-f202y is enabled and the current operation is EXEC_BLOCK. * st.cc (gfc_free_association_list): Free bounds expressions of the 'ar' field, if present. * trans-array.cc (gfc_conv_ss_startstride): If -std=f202y and bounds checking activated, do not apply the assertion. * trans-expr.cc (gfc_trans_pointer_assignment): An assumed rank target has its offset set to zero. * trans-stmt.cc (trans_associate_var): If the selector is assumed rank, call gfc_trans_pointer_assignment using the 'ar' field in the association list as the array reference for expr1. The data target, expr2, is a copy of the selector expression. gcc/testsuite/ PR fortran/116733 * gfortran.dg/associate_3.f03: Change error message. * gfortran.dg/f202y/f202y.exp: Enable tests of f202y features. * gfortran.dg/f202y/generic_assumed_rank_1.f90: New test. * gfortran.dg/f202y/generic_assumed_rank_2.f90: New test. * gfortran.dg/f202y/generic_assumed_rank_3.f90: New test.
1 parent 2ac01a4 commit c5fa210

22 files changed

+581
-65
lines changed

gcc/fortran/array.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
869869
{
870870
int i;
871871
symbol_attribute *attr;
872-
872+
873873
if (as == NULL)
874874
return true;
875875

@@ -878,7 +878,7 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
878878
attr = &sym->attr;
879879
if (gfc_submodule_procedure(attr))
880880
return true;
881-
881+
882882
if (as->rank
883883
&& !gfc_add_dimension (&sym->attr, sym->name, error_loc))
884884
return false;
@@ -2457,7 +2457,7 @@ gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
24572457
mpz_set_ui (stride, 1);
24582458
else
24592459
{
2460-
stride_expr = gfc_copy_expr(ar->stride[dimen]);
2460+
stride_expr = gfc_copy_expr(ar->stride[dimen]);
24612461

24622462
if (!gfc_simplify_expr (stride_expr, 1)
24632463
|| stride_expr->expr_type != EXPR_CONSTANT

gcc/fortran/expr.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4371,9 +4371,18 @@ gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
43714371
return false;
43724372
}
43734373

4374+
/* An assumed rank target is an experimental F202y feature. */
4375+
if (rvalue->rank == -1 && !(gfc_option.allow_std & GFC_STD_F202Y))
4376+
{
4377+
gfc_error ("The assumed rank target at %L is an experimental F202y "
4378+
"feature. Use option -std=f202y to enable",
4379+
&rvalue->where);
4380+
return false;
4381+
}
4382+
43744383
/* The target must be either rank one or it must be simply contiguous
43754384
and F2008 must be allowed. */
4376-
if (rvalue->rank != 1)
4385+
if (rvalue->rank != 1 && rvalue->rank != -1)
43774386
{
43784387
if (!gfc_is_simply_contiguous (rvalue, true, false))
43794388
{
@@ -4386,6 +4395,21 @@ gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
43864395
return false;
43874396
}
43884397
}
4398+
else if (rvalue->rank == -1)
4399+
{
4400+
gfc_error ("The data-target at %L is an assumed rank object and so the "
4401+
"data-pointer-object %s must have a bounds remapping list "
4402+
"(list of lbound:ubound for each dimension)",
4403+
&rvalue->where, lvalue->symtree->name);
4404+
return false;
4405+
}
4406+
4407+
if (rvalue->rank == -1 && !gfc_is_simply_contiguous (rvalue, true, false))
4408+
{
4409+
gfc_error ("The assumed rank data-target at %L must be contiguous",
4410+
&rvalue->where);
4411+
return false;
4412+
}
43894413

43904414
/* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
43914415
if (rvalue->expr_type == EXPR_NULL)

gcc/fortran/gfortran.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,8 @@ typedef struct gfc_association_list
30343034

30353035
gfc_expr *target;
30363036

3037+
gfc_array_ref *ar;
3038+
30373039
/* Used for inferring the derived type of an associate name, whose selector
30383040
is a sibling derived type function that has not yet been parsed. */
30393041
gfc_symbol *derived_types;

gcc/fortran/interface.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,16 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
33373337
goto match;
33383338
}
33393339

3340+
if (warn_surprising
3341+
&& a->expr->expr_type == EXPR_VARIABLE
3342+
&& a->expr->symtree->n.sym->as
3343+
&& a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
3344+
&& f->sym->as
3345+
&& f->sym->as->type == AS_ASSUMED_RANK)
3346+
gfc_warning (0, "The assumed-size dummy %qs is being passed at %L to "
3347+
"an assumed-rank dummy %qs", a->expr->symtree->name,
3348+
&a->expr->where, f->sym->name);
3349+
33403350
if (a->expr->expr_type == EXPR_NULL
33413351
&& a->expr->ts.type == BT_UNKNOWN
33423352
&& f->sym->ts.type == BT_CHARACTER

gcc/fortran/intrinsic.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,15 @@ do_ts29113_check (gfc_intrinsic_sym *specific, gfc_actual_arglist *arg)
293293
&a->expr->where, gfc_current_intrinsic);
294294
ok = false;
295295
}
296-
else if (a->expr->rank == -1 && !specific->inquiry)
296+
else if (a->expr->rank == -1
297+
&& !(specific->inquiry
298+
|| (specific->id == GFC_ISYM_RESHAPE
299+
&& (gfc_option.allow_std & GFC_STD_F202Y))))
297300
{
298301
gfc_error ("Assumed-rank argument at %L is only permitted as actual "
299-
"argument to intrinsic inquiry functions",
300-
&a->expr->where);
302+
"argument to intrinsic inquiry functions or to RESHAPE. "
303+
"The latter is an experimental F202y feature. Use "
304+
"-std=f202y to enable", &a->expr->where);
301305
ok = false;
302306
}
303307
else if (a->expr->rank == -1 && arg != a)
@@ -307,6 +311,13 @@ do_ts29113_check (gfc_intrinsic_sym *specific, gfc_actual_arglist *arg)
307311
&a->expr->where, gfc_current_intrinsic);
308312
ok = false;
309313
}
314+
else if (a->expr->rank == -1 && specific->id == GFC_ISYM_RESHAPE
315+
&& !gfc_is_simply_contiguous (a->expr, true, false))
316+
{
317+
gfc_error ("Assumed rank argument to the RESHAPE intrinsic at %L "
318+
"must be contiguous", &a->expr->where);
319+
ok = false;
320+
}
310321
}
311322

312323
return ok;

gcc/fortran/invoke.texi

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@c Copyright (C) 2004-2024 Free Software Foundation, Inc.
2-
@c This is part of the GNU Fortran manual.
2+
@c This is part of the GNU Fortran manual.
33
@c For copying conditions, see the file gfortran.texi.
44

55
@ignore
@@ -139,7 +139,7 @@ by type. Explanations are in the following sections.
139139
-H -P
140140
-U@var{macro} -cpp -dD -dI -dM -dN -dU -fworking-directory
141141
-imultilib @var{dir}
142-
-iprefix @var{file} -iquote -isysroot @var{dir} -isystem @var{dir} -nocpp
142+
-iprefix @var{file} -iquote -isysroot @var{dir} -isystem @var{dir} -nocpp
143143
-nostdinc
144144
-undef
145145
}
@@ -312,7 +312,7 @@ JIAND, etc...). For a complete list of intrinsics see the full documentation.
312312
Obsolete flag. The purpose of this option was to
313313
enable legacy math intrinsics such as COTAN and degree-valued trigonometric
314314
functions (e.g. TAND, ATAND, etc...) for compatability with older code. This
315-
option is no longer operable. The trigonometric functions are now either
315+
option is no longer operable. The trigonometric functions are now either
316316
part of Fortran 2023 or GNU extensions.
317317

318318
@opindex fdec-static
@@ -341,7 +341,7 @@ following the final comma.
341341
@cindex symbol names
342342
@cindex character set
343343
@item -fdollar-ok
344-
Allow @samp{$} as a valid non-first character in a symbol name. Symbols
344+
Allow @samp{$} as a valid non-first character in a symbol name. Symbols
345345
that start with @samp{$} are rejected since it is unclear which rules to
346346
apply to implicit typing as different vendors implement different rules.
347347
Using @samp{$} in @code{IMPLICIT} statements is also rejected.
@@ -606,7 +606,10 @@ beyond the relevant language standard, and warnings are given for the
606606
Fortran 77 features that are permitted but obsolescent in later
607607
standards. The deprecated option @samp{-std=f2008ts} acts as an alias for
608608
@samp{-std=f2018}. It is only present for backwards compatibility with
609-
earlier gfortran versions and should not be used any more.
609+
earlier gfortran versions and should not be used any more. @samp{-std=f202y}
610+
acts as an alias for @samp{-std=f2023} and enables proposed features for
611+
testing Fortran 202y. As the Fortran 202y standard develops, implementation
612+
might change or the experimental new features might be removed.
610613

611614
@opindex ftest-forall-temp
612615
@item -ftest-forall-temp
@@ -718,7 +721,7 @@ Like @option{-dD}, but emit only the macro names, not their expansions.
718721
@cindex debugging, preprocessor
719722
@item -dU
720723
Like @option{dD} except that only macros that are expanded, or whose
721-
definedness is tested in preprocessor directives, are output; the
724+
definedness is tested in preprocessor directives, are output; the
722725
output is delayed until the use or test of the macro; and @code{'#undef'}
723726
directives are also output for macros tested but undefined at the time.
724727

@@ -908,7 +911,7 @@ with a @option{-D} option.
908911
Errors are diagnostic messages that report that the GNU Fortran compiler
909912
cannot compile the relevant piece of source code. The compiler will
910913
continue to process the program in an attempt to report further errors
911-
to aid in debugging, but will not produce any compiled output.
914+
to aid in debugging, but will not produce any compiled output.
912915

913916
Warnings are diagnostic messages that report constructions which
914917
are not inherently erroneous but which are risky or suggest there is
@@ -1027,7 +1030,7 @@ avoid such temporaries.
10271030
@opindex Wc-binding-type
10281031
@cindex warning, C binding type
10291032
@item -Wc-binding-type
1030-
Warn if the a variable might not be C interoperable. In particular, warn if
1033+
Warn if the a variable might not be C interoperable. In particular, warn if
10311034
the variable has been declared using an intrinsic type with default kind
10321035
instead of using a kind parameter defined for C interoperability in the
10331036
intrinsic @code{ISO_C_Binding} module. This option is implied by
@@ -1050,7 +1053,7 @@ error.
10501053
@cindex warnings, conversion
10511054
@cindex conversion
10521055
@item -Wconversion
1053-
Warn about implicit conversions that are likely to change the value of
1056+
Warn about implicit conversions that are likely to change the value of
10541057
the expression after conversion. Implied by @option{-Wall}.
10551058

10561059
@opindex Wconversion-extra
@@ -1191,7 +1194,7 @@ the desired intrinsic/procedure. This option is implied by @option{-Wall}.
11911194
@cindex warnings, use statements
11921195
@cindex intrinsic
11931196
@item -Wuse-without-only
1194-
Warn if a @code{USE} statement has no @code{ONLY} qualifier and
1197+
Warn if a @code{USE} statement has no @code{ONLY} qualifier and
11951198
thus implicitly imports all public entities of the used module.
11961199

11971200
@opindex Wunused-dummy-argument
@@ -1436,8 +1439,8 @@ they are not in the default location expected by the compiler.
14361439
@cindex options, linking
14371440
@cindex linking, static
14381441

1439-
These options come into play when the compiler links object files into an
1440-
executable output file. They are meaningless if the compiler is not doing
1442+
These options come into play when the compiler links object files into an
1443+
executable output file. They are meaningless if the compiler is not doing
14411444
a link step.
14421445

14431446
@table @gcctabopt
@@ -1609,7 +1612,7 @@ referenced in it. Does not affect common blocks. (Some Fortran compilers
16091612
provide this option under the name @option{-static} or @option{-save}.)
16101613
The default, which is @option{-fautomatic}, uses the stack for local
16111614
variables smaller than the value given by @option{-fmax-stack-var-size}.
1612-
Use the option @option{-frecursive} to use no static memory.
1615+
Use the option @option{-frecursive} to use no static memory.
16131616

16141617
Local variables or arrays having an explicit @code{SAVE} attribute are
16151618
silently ignored unless the @option{-pedantic} option is added.
@@ -1880,7 +1883,7 @@ Deprecated alias for @option{-fcheck=array-temps}.
18801883

18811884
@opindex fmax-array-constructor
18821885
@item -fmax-array-constructor=@var{n}
1883-
This option can be used to increase the upper limit permitted in
1886+
This option can be used to increase the upper limit permitted in
18841887
array constructors. The code below requires this option to expand
18851888
the array at compile time.
18861889

gcc/fortran/lang.opt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
; the terms of the GNU General Public License as published by the Free
88
; Software Foundation; either version 3, or (at your option) any later
99
; version.
10-
;
10+
;
1111
; GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1212
; WARRANTY; without even the implied warranty of MERCHANTABILITY or
1313
; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1414
; for more details.
15-
;
15+
;
1616
; You should have received a copy of the GNU General Public License
1717
; along with GCC; see the file COPYING3. If not see
1818
; <http://www.gnu.org/licenses/>.
@@ -930,6 +930,10 @@ std=f2023
930930
Fortran
931931
Conform to the ISO Fortran 2023 standard.
932932

933+
std=f202y
934+
Fortran
935+
Enable experimental Fortran 202y features.
936+
933937
std=f95
934938
Fortran
935939
Conform to the ISO Fortran 95 standard.

gcc/fortran/libgfortran.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see
2323
Nevertheless, some features available in F2018 are prohibited in F2023.
2424
Please remember to keep those definitions in sync with
2525
gfortran.texi. */
26+
#define GFC_STD_F202Y (1<<14) /* Enable proposed F202y features. */
2627
#define GFC_STD_UNSIGNED (1<<14) /* Not really a standard, but
2728
better for error handling. */
2829
#define GFC_STD_F2023_DEL (1<<13) /* Prohibited in F2023. */

gcc/fortran/match.cc

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,29 @@ gfc_match_associate (void)
19251925
gfc_association_list* a;
19261926

19271927
/* Match the next association. */
1928-
if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1928+
if (gfc_match (" %n ", newAssoc->name) != MATCH_YES)
1929+
{
1930+
gfc_error ("Expected associate name at %C");
1931+
goto assocListError;
1932+
}
1933+
1934+
/* Required for an assumed rank target. */
1935+
if (gfc_peek_char () == '(')
1936+
{
1937+
newAssoc->ar = gfc_get_array_ref ();
1938+
if (gfc_match_array_ref (newAssoc->ar, NULL, 0, 0) != MATCH_YES)
1939+
{
1940+
gfc_error ("Bad bounds remapping list at %C");
1941+
goto assocListError;
1942+
}
1943+
}
1944+
1945+
if (newAssoc->ar && !(gfc_option.allow_std & GFC_STD_F202Y))
1946+
gfc_error_now ("The bounds remapping list at %C is an experimental "
1947+
"F202y feature. Use std=f202y to enable");
1948+
1949+
/* Match the next association. */
1950+
if (gfc_match (" =>", newAssoc->name) != MATCH_YES)
19291951
{
19301952
gfc_error ("Expected association at %C");
19311953
goto assocListError;
@@ -1969,6 +1991,35 @@ gfc_match_associate (void)
19691991
goto assocListError;
19701992
}
19711993

1994+
if (newAssoc->target->expr_type == EXPR_VARIABLE
1995+
&& newAssoc->target->symtree->n.sym->as
1996+
&& newAssoc->target->symtree->n.sym->as->type == AS_ASSUMED_RANK)
1997+
{
1998+
bool bounds_remapping_list = true;
1999+
if (!newAssoc->ar)
2000+
bounds_remapping_list = false;
2001+
else
2002+
for (int dim = 0; dim < newAssoc->ar->dimen; dim++)
2003+
if (!newAssoc->ar->start[dim] || !newAssoc->ar->end[dim]
2004+
|| newAssoc->ar->stride[dim] != NULL)
2005+
bounds_remapping_list = false;
2006+
2007+
if (!bounds_remapping_list)
2008+
{
2009+
gfc_error ("The associate name %s with an assumed rank "
2010+
"target at %L must have a bounds remapping list "
2011+
"(list of lbound:ubound for each dimension)",
2012+
newAssoc->name, &newAssoc->target->where);
2013+
goto assocListError;
2014+
}
2015+
2016+
if (!newAssoc->target->symtree->n.sym->attr.contiguous)
2017+
{
2018+
gfc_error ("The assumed rank target at %C must be contiguous");
2019+
goto assocListError;
2020+
}
2021+
}
2022+
19722023
/* The `variable' field is left blank for now; because the target is not
19732024
yet resolved, we can't use gfc_has_vector_subscript to determine it
19742025
for now. This is set during resolution. */

0 commit comments

Comments
 (0)