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. This one handles the example of an implied-do loop in an initialization expression (see patch for expr.cc). diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index 69d0b57c688..90bd8d7251d 100644 --- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -3165,9 +3165,20 @@ gfc_reduce_init_expr (gfc_expr *expr) bool t; gfc_init_expr_flag = true; + + if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_UNKNOWN) + { + gfc_simplify_expr (expr, 1); + 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 (;;) -- Steve