https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96255

--- Comment #7 from kargl at gcc dot gnu.org ---
(In reply to Steve Kargl from comment #6)
> On Tue, Jul 21, 2020 at 07:44:16PM +0000, jvdelisle at charter dot net wrote:
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96255
> > 
> > --- Comment #5 from jvdelisle at charter dot net ---
> > (In reply to kargl from comment #2)
> > > This issue depends on the fix for FORALL.  In gfc_match_do in the 
> > > concurrent
> > > section, one gets to 
> > > 
> > >       m = match_forall_header (&head, &mask);
> > > 
> > > to match the control portion of the statement.
> > 
> > Although we need to support forall, it is interesting that the standards
> > committe i going to deprecate it, if they have not done so already. Not
> > encouraged to be used for sure. Also they will be adding features to DO
> > CONCUURENT which look useful to me.
> > 
> 
> do current (JUNK HERE)
> 
> 
> forall (JUNK HERE)
> 
> The JUNK HERE is parsed by the same code; namely, match_forall_header().
> 
> So, if one fixes do current, then one fixes forall.
> 
> PS: J3 has an interesting discussion that suggests that do current
> is also broken.

IMHO, J3 has done no one any good with adding type specs
to FORALL and DO CONCURRENT.  There are three situations with 
FORALL (and by extension DO CONCURRENT).

1) The control variable is declared in the outer scope and a type spec
   is not used.  This is what gfortran currently assumes.

   subroutine sub1
     implicit none
     integer i, index(10), a(10), b(10)
     b = (/ (i, i = 1, 10) /)
     index = (/ b(1:10:2), b(2:10:2) /)
     forall (i = 1:10)
       a(index(i)) = b(i)
     end forall
     print '(10I3)', a
     print '(10I3)', b
     print '(10I3)', index
   end subroutine sub1

2) The control variable appears only in the FORALL construct and a type
   spec is used. Here, we can simply set the type spec of the control
   variable.

   subroutine sub2
     implicit none
     integer i, index(10), a(10), b(10)
     b = (/ (i, i = 1, 10) /)
     index = (/ b(1:10:2), b(2:10:2) /)
     forall (integer(2) :: j = 1:10)
       a(index(j)) = b(j)
     end forall
     print '(10I3)', a
     print '(10I3)', b
     print '(10I3)', index
  end subroutine sub2

3) The control variable is declared in the outer scope and a type spec
   is specified in the FORALL construct.  The control variable is a
   statement entity (meaning FORALL should have its own namespace, but
   it does not have one).  This now shadows the variable in the outer
   scope. If the kind type parameters are the same, we're ok because we
   can ignore the type spec.  If the kind type parameters are different,
   gfortran needs to create a shadow variable.

   subroutine sub3
     implicit none
     integer i, index(10), a(10), b(10)     ! i declared here.
     b = (/ (i, i = 1, 10) /)
     index = (/ b(1:10:2), b(2:10:2) /)
     forall (integer(2) :: i = 1:10)        ! i re-declared here.
       a(index(i)) = b(i)
     end forall
     print '(10I3)', a
     print '(10I3)', b
     print '(10I3)', index
  end subroutine sub3

The following diff permits the above cases with one caveat.  In case 3),
when gfortran parses the body of the FORALL construct, the 'i' in
'index(i)' and 'b(i)' is the 'i' from the outer scope.  It is not the
newly created shadow variable '_i'.  AFAICT, we need to walk the gfc_code
list in resolve.c:(gfc_resolve_forall) and update the 'i' to the 
shadow variable '_i'

Someone interesting in completing the patch can start here:

Index: gcc/fortran/match.c
===================================================================
--- gcc/fortran/match.c (revision 280157)
+++ gcc/fortran/match.c (working copy)
@@ -2381,7 +2381,10 @@ cleanup:
 }


-/* Match the header of a FORALL statement.  */
+/* Match the header of a FORALL statement.  In F2008 and F2018, the form of
+   the header is 
+   ([ type-spec :: ] concurrent-control-list [, scalar-mask-expr ] )
+   where type-spec is INTEGER.  */

 static match
 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
@@ -2389,6 +2392,9 @@ match_forall_header (gfc_forall_iterator **phead, gfc_
   gfc_forall_iterator *head, *tail, *new_iter;
   gfc_expr *msk;
   match m;
+  gfc_typespec ts;
+  bool seen_ts = false;
+  locus loc;

   gfc_gobble_whitespace ();

@@ -2398,12 +2404,74 @@ match_forall_header (gfc_forall_iterator **phead, gfc_
   if (gfc_match_char ('(') != MATCH_YES)
     return MATCH_NO;

+  /* Check for an optional type-spec.  */
+  gfc_clear_ts (&ts);
+  loc = gfc_current_locus;
+  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, "FORALL or DO CONCURRENT "
+                              "construct includes type specification "
+                              "at %L", &loc))
+           goto cleanup;
+
+         if (ts.type != BT_INTEGER)
+           {
+             gfc_error ("Type-spec at %L must be an INTEGER type", &loc);
+             goto cleanup;
+           }
+       }
+    }
+  else if (m == MATCH_ERROR)
+    goto syntax;
+
   m = match_forall_iterator (&new_iter);
   if (m == MATCH_ERROR)
     goto cleanup;
   if (m == MATCH_NO)
     goto syntax;

+  if (seen_ts)
+    {
+      char *name;
+      gfc_expr *v;
+      gfc_symtree *st;
+
+      /* If the control variable does not have a type and type spec, then
+        update the type spec in both the variable and symtree.  Otherwise,
+        create a new private symbol.  */
+      v = new_iter->var;
+      if (v->ts.type == BT_UNKNOWN)
+       {
+         v->ts.type = v->symtree->n.sym->ts.type = BT_INTEGER;
+         v->ts.kind = v->symtree->n.sym->ts.kind = ts.kind;
+       }
+      else if (v->ts.kind != ts.kind)
+       {
+         name = (char *) alloca (strlen (v->symtree->name) + 2);
+         strcpy (name, "_");
+         strcat (name, v->symtree->name);
+         if (gfc_get_sym_tree (name, NULL, &st, false) != 0)
+           gfc_internal_error ("whoops");
+
+         v = gfc_get_expr ();
+         v->where = gfc_current_locus;
+         v->expr_type = EXPR_VARIABLE;
+         v->ts.type = st->n.sym->ts.type = ts.type;
+         v->ts.kind = st->n.sym->ts.kind = ts.kind;
+         st->n.sym->forall_index = true;
+         v->symtree = st;
+         gfc_replace_expr (new_iter->var, v);
+       }
+      gfc_convert_type (new_iter->start, &ts, 1);
+      gfc_convert_type (new_iter->end, &ts, 1);
+      gfc_convert_type (new_iter->stride, &ts, 1);
+    }
+
   head = tail = new_iter;

   for (;;)
@@ -2417,6 +2485,42 @@ match_forall_header (gfc_forall_iterator **phead, gfc_

       if (m == MATCH_YES)
        {
+         if (seen_ts)
+           {
+             char *name;
+             gfc_expr *v;
+             gfc_symtree *st;
+
+             v = new_iter->var;
+             if (v->ts.type == BT_UNKNOWN)
+               {
+                 v->ts.type = v->symtree->n.sym->ts.type = BT_INTEGER;
+                 v->ts.kind = v->symtree->n.sym->ts.kind = ts.kind;
+               }
+             else if (v->ts.kind != ts.kind)
+               {
+                 name = (char *) alloca (strlen (v->symtree->name) + 2);
+                 strcpy (name, "_");
+                 strcat (name, v->symtree->name);
+                 if (gfc_get_sym_tree (name, NULL, &st, false) != 0)
+                   gfc_internal_error ("whoops");
+
+                 v = gfc_get_expr ();
+                 v->expr_type = EXPR_VARIABLE;
+                 v->ts.type = ts.type;
+                 v->ts.kind = ts.kind;
+                 v->where = gfc_current_locus;
+                 st->n.sym->ts.type = ts.type;
+                 st->n.sym->ts.kind = ts.kind;
+                 st->n.sym->forall_index = true;
+                 v->symtree = st;
+                 gfc_replace_expr (new_iter->var, v);
+               }
+             gfc_convert_type (new_iter->start, &ts, 1);
+             gfc_convert_type (new_iter->end, &ts, 1);
+             gfc_convert_type (new_iter->stride, &ts, 1);
+           }
+
          tail->next = new_iter;
          tail = new_iter;
          continue;

Reply via email to