public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* typespec in forall and implied-do
@ 2022-11-16  1:13 Steve Kargl
  2022-11-16  2:31 ` Steve Kargl
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Steve Kargl @ 2022-11-16  1:13 UTC (permalink / raw)
  To: fortran

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.

Consider,

   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
            real x(n)
            x = [(sqrt(real(p)), integer :: p = 1, n)]
            print '(*(F8.2,1X))', x
         end subroutine bar

   end program foo

This patch allows the above to compile and execute.
It has only had some light testing, and I do not know
if nested forall and implied-do loops do work.  Feel
free to commit as I cannot.

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 (;;)

^ permalink raw reply	[flat|nested] 17+ messages in thread
* typespec in forall and implied-do
@ 2022-11-20 21:28 Harald Anlauf
  2022-11-20 23:31 ` Steve Kargl
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Harald Anlauf @ 2022-11-20 21:28 UTC (permalink / raw)
  To: fortran, sgk

Steve,

for unknown reasons I cannot reply to your mail on gmane,
so trying directly via mailing list.

I tried your patch, and it works on the supplied testcases.

However, there is a scoping issue for the declaration of the
index variable, as can be seen by the following variation:

program foo
  use iso_fortran_env, only : k => real_kinds
  implicit none
  integer, parameter :: n = size(k)
  integer(8) :: i
!!$  integer, parameter :: &
!!$       &  p(n) = [(precision(real(1.,k(i))), integer :: i = 1, n)]
  integer, parameter :: &
       &  q(n) = [(kind(i), integer(2) :: i = 1, n)]
  integer, parameter :: &
       &  r(n) = [(storage_size(i), integer(1) :: i = 1, n)]
!!$  print *, p
  print *, q
  print *, r
end program foo

After your patch, gfortran prints:

           8           8           8           8
          64          64          64          64

This suggests that the integer kind is taken from the host decl,
which is kind=8, and not the local one (2 or 1).

Crayftn (which chokes on your original testcase):

 3*2
 3*8

This is what I expect.

Intel doesn't accept storage_size() here, which is a bug.
Commenting the uses of array r, I then get:

           2           2           2

At least this agrees with Cray.

Can you have another look at this?

Thanks so far for you patch!

Harald


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-11-27 19:33 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-16  1:13 typespec in forall and implied-do Steve Kargl
2022-11-16  2:31 ` Steve Kargl
2022-11-16 20:24   ` Steve Kargl
2022-11-16 18:08 ` Steve Kargl
2022-11-16 18:20 ` Steve Kargl
2022-11-16 21:30 ` Steve Kargl
2022-11-17  0:32   ` Steve Kargl
2022-11-17  0:47     ` Steve Kargl
2022-11-17  4:15       ` Steve Kargl
2022-11-17 18:48 ` Steve Kargl
2022-11-20 21:28 Harald Anlauf
2022-11-20 23:31 ` Steve Kargl
     [not found]   ` <d2efcc09-f5be-904e-fb70-f75fdabbee1f@orange.fr>
     [not found]     ` <Y3vHlojilLVU8qC2@troutmask.apl.washington.edu>
2022-11-27 19:17       ` Mikael Morin
2022-11-27 19:33         ` Mikael Morin
2022-11-20 23:33 ` Steve Kargl
2022-11-22 21:15 ` Harald Anlauf
2022-11-22 21:59   ` Steve Kargl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).