From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) by sourceware.org (Postfix) with ESMTPS id 8A8A13853D4A for ; Thu, 17 Nov 2022 18:48:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8A8A13853D4A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=troutmask.apl.washington.edu Authentication-Results: sourceware.org; spf=none smtp.mailfrom=troutmask.apl.washington.edu Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.16.1/8.16.1) with ESMTPS id 2AHImuIU010116 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO) for ; Thu, 17 Nov 2022 10:48:57 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.16.1/8.16.1/Submit) id 2AHImu6f010115 for fortran@gcc.gnu.org; Thu, 17 Nov 2022 10:48:56 -0800 (PST) (envelope-from sgk) Date: Thu, 17 Nov 2022 10:48:56 -0800 From: Steve Kargl To: Steve Kargl via Fortran Subject: Re: typespec in forall and implied-do Message-ID: Reply-To: sgk@troutmask.apl.washington.edu References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, Nov 15, 2022 at 05:13:19PM -0800, Steve Kargl via Fortran wrote: > F2008 introduced the inclusion of a typespec in a forall > statement, and thn F2018 a typespec was allowed in an > implied-do. There may even be a few bug reports. New patch and two test cases (don't know how add testcases under git). Fixes pr78219 for forall. I thought, but cannot find, there is a PR about implied-do. * fortran/decl.cc: Place current_attr in global namespace. Needed ... * fortran/expr.cc (gfc_reduce_init_expr): ... here. Handle an implied-do loop in an initialization expression whre a type-spec has been given. * fortran/match.cc (gfc_match_iterator): Match optional type-spec in implied-do. * fortran/match.cc (match_forall_header): Match optional type-spec in forall-control-header. diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 0f9b2ced4c2..068eb6c4113 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -52,7 +52,7 @@ static int old_char_selector; static gfc_typespec current_ts; -static symbol_attribute current_attr; +symbol_attribute current_attr; static gfc_array_spec *current_as; static int colon_seen; static int attr_seen; diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index 69d0b57c688..899c76f8cde 100644 --- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -3162,12 +3162,34 @@ gfc_check_init_expr (gfc_expr *e) bool gfc_reduce_init_expr (gfc_expr *expr) { + extern symbol_attribute current_attr; bool t; gfc_init_expr_flag = true; + + /* This block is need to reduce an initialization expression with an + implied-do loop where a type-spec is include, e.g., + + integer, parameter :: & + & p(n) = [(precision(real(1.,k(i))), integer :: i = 1, n)] */ + if (expr + && expr->expr_type == EXPR_ARRAY + && expr->ts.type == BT_UNKNOWN + && current_attr.flavor == FL_PARAMETER + && gfc_current_ns->seen_implicit_none == 1) + { + gfc_simplify_expr (expr, 1); + gfc_resolve_expr (expr); + if (!gfc_check_constructor_type (expr)) + return false; + if (!gfc_expand_constructor (expr, true)) + return false; + } + t = gfc_resolve_expr (expr); if (t) t = gfc_check_init_expr (expr); + gfc_init_expr_flag = false; if (!t || !expr) diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc index 8b8b6e79c8b..3fd2a80caad 100644 --- a/gcc/fortran/match.cc +++ b/gcc/fortran/match.cc @@ -968,9 +968,39 @@ gfc_match_iterator (gfc_iterator *iter, int init_flag) gfc_expr *var, *e1, *e2, *e3; locus start; match m; + gfc_typespec ts; + bool seen_ts; e1 = e2 = e3 = NULL; + /* Match an optional "integer ::" type-spec. */ + start = gfc_current_locus; + seen_ts = false; + gfc_clear_ts (&ts); + m = gfc_match_type_spec (&ts); + if (m == MATCH_YES) + { + seen_ts = (gfc_match (" ::") == MATCH_YES); + + if (seen_ts) + { + if (!gfc_notify_std (GFC_STD_F2018, "Optional type-spec " + "included in implied-do loop at %C")) + goto cleanup; + + if (ts.type != BT_INTEGER) + { + gfc_error ("Type in type-spec at %C shall be INTEGER"); + goto cleanup; + } + } + } + else if (m == MATCH_ERROR) + goto cleanup; + + if (!seen_ts) + gfc_current_locus = start; + /* Match the start of an iterator without affecting the symbol table. */ start = gfc_current_locus; @@ -984,6 +1014,14 @@ gfc_match_iterator (gfc_iterator *iter, int init_flag) if (m != MATCH_YES) return MATCH_NO; + if (seen_ts && var->ts.type == BT_UNKNOWN) + { + var->ts.type = ts.type; + var->ts.kind = ts.kind; + var->symtree->n.sym->ts.type = ts.type; + var->symtree->n.sym->ts.kind = ts.kind; + } + if (var->symtree->n.sym->attr.dimension) { gfc_error ("Loop variable at %C cannot be an array"); @@ -2396,6 +2434,9 @@ match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask) gfc_forall_iterator *head, *tail, *new_iter; gfc_expr *msk; match m; + locus start; + gfc_typespec ts; + bool seen_ts; gfc_gobble_whitespace (); @@ -2405,12 +2446,48 @@ match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask) if (gfc_match_char ('(') != MATCH_YES) return MATCH_NO; + /* Match an optional "integer ::" type-spec. */ + start = gfc_current_locus; + seen_ts = false; + gfc_clear_ts (&ts); + m = gfc_match_type_spec (&ts); + if (m == MATCH_YES) + { + seen_ts = (gfc_match (" ::") == MATCH_YES); + + if (seen_ts) + { + if (!gfc_notify_std (GFC_STD_F2008, "Optional type-spec " + "included in FORALL at %C")) + goto cleanup; + + if (ts.type != BT_INTEGER) + { + gfc_error ("Type in type-spec at %C shall be INTEGER"); + goto cleanup; + } + } + } + else if (m == MATCH_ERROR) + goto cleanup; + + if (!seen_ts) + gfc_current_locus = start; + m = match_forall_iterator (&new_iter); if (m == MATCH_ERROR) goto cleanup; if (m == MATCH_NO) goto syntax; + if (seen_ts && new_iter->var->ts.type == BT_UNKNOWN) + { + new_iter->var->ts.type = ts.type; + new_iter->var->ts.kind = ts.kind; + new_iter->var->symtree->n.sym->ts.type = ts.type; + new_iter->var->symtree->n.sym->ts.kind = ts.kind; + } + head = tail = new_iter; for (;;) % cat ~/gcc/gccx/gcc/testsuite/gfortran.dg/implied_do_index.f90 ! { dg-do run } program foo use iso_fortran_env, only : k => real_kinds implicit none integer, parameter :: n = size(k) integer, parameter :: & & p(n) = [(precision(real(1.,k(i))), integer :: i = 1, n)] if (p(1) /= 6 .or. p(2) /= 15) stop 1 end program foo % cat ~/gcc/gccx/gcc/testsuite/gfortran.dg/forall_index.f90 ! { dg-do run } ! program foo implicit none integer, parameter :: n = 9 integer a(n,n), b(n), j b = [(k, integer :: k = 1, n)] if (any(b /= [1, 2, 3, 4, 5, 6, 7, 8, 9])) stop 1 a = 0 forall (integer :: i = 1:n) a(i,i) = b(i) do j = 1, n if (a(j,j) /= b(j)) stop j end do call bar contains subroutine bar character(len=*), parameter :: & & out = " 1.00 1.41 1.73 2.00 2.24 2.45 2.65 2.83 3.00" character(len=80) str real x(n) x = [(sqrt(real(p)), integer :: p = 1, n)] write(str,'(*(F5.2,1X))') x if (trim(str) /= out) stop 42 end subroutine bar end program foo -- Steve