Blame SOURCES/0015-Allow-automatics-in-equivalence.patch

ca6e07
From e6f385f8258148890a097878a618b694be663db6 Mon Sep 17 00:00:00 2001
ca6e07
From: Mark Eggleston <markeggleston@codethink.com>
ca6e07
Date: Tue, 11 Sep 2018 12:50:11 +0100
ca6e07
Subject: [PATCH 15/16] Allow automatics in equivalence
ca6e07
ca6e07
If a variable with an automatic attribute appears in an
ca6e07
equivalence statement the storage should be allocated on
ca6e07
the stack.
ca6e07
ca6e07
Note: most of this patch was provided by Jeff Law <law@redhat.com>.
ca6e07
---
ca6e07
 gcc/fortran/gfortran.h                        |  1 +
ca6e07
 gcc/fortran/symbol.c                          |  4 +-
ca6e07
 gcc/fortran/trans-common.c                    | 75 +++++++++++++++++++++++++--
ca6e07
 gcc/testsuite/gfortran.dg/auto_in_equiv_1.f90 | 36 +++++++++++++
ca6e07
 gcc/testsuite/gfortran.dg/auto_in_equiv_2.f90 | 38 ++++++++++++++
ca6e07
 gcc/testsuite/gfortran.dg/auto_in_equiv_3.f90 | 63 ++++++++++++++++++++++
ca6e07
 6 files changed, 210 insertions(+), 7 deletions(-)
ca6e07
 create mode 100644 gcc/testsuite/gfortran.dg/auto_in_equiv_1.f90
ca6e07
 create mode 100644 gcc/testsuite/gfortran.dg/auto_in_equiv_2.f90
ca6e07
 create mode 100644 gcc/testsuite/gfortran.dg/auto_in_equiv_3.f90
ca6e07
ca6e07
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
ca6e07
index 23d01b10728..eb2a29fea5f 100644
ca6e07
--- a/gcc/fortran/gfortran.h
ca6e07
+++ b/gcc/fortran/gfortran.h
ca6e07
@@ -2993,6 +2993,7 @@ bool gfc_merge_new_implicit (gfc_typespec *);
ca6e07
 void gfc_set_implicit_none (bool, bool, locus *);
ca6e07
 void gfc_check_function_type (gfc_namespace *);
ca6e07
 bool gfc_is_intrinsic_typename (const char *);
ca6e07
+bool check_conflict (symbol_attribute *, const char *, locus *);
ca6e07
 
ca6e07
 gfc_typespec *gfc_get_default_type (const char *, gfc_namespace *);
ca6e07
 bool gfc_set_default_type (gfc_symbol *, int, gfc_namespace *);
ca6e07
diff --git a/gcc/fortran/symbol.c b/gcc/fortran/symbol.c
ca6e07
index 4247b5b60c8..5fdb46c4b32 100644
ca6e07
--- a/gcc/fortran/symbol.c
ca6e07
+++ b/gcc/fortran/symbol.c
ca6e07
@@ -407,7 +407,7 @@ gfc_check_function_type (gfc_namespace *ns)
ca6e07
                                 goto conflict_std;\
ca6e07
                               }
ca6e07
 
ca6e07
-static bool
ca6e07
+bool
ca6e07
 check_conflict (symbol_attribute *attr, const char *name, locus *where)
ca6e07
 {
ca6e07
   static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
ca6e07
@@ -544,7 +544,6 @@ check_conflict (symbol_attribute *attr, const char *name, locus *where)
ca6e07
   conf (allocatable, elemental);
ca6e07
 
ca6e07
   conf (in_common, automatic);
ca6e07
-  conf (in_equivalence, automatic);
ca6e07
   conf (result, automatic);
ca6e07
   conf (use_assoc, automatic);
ca6e07
   conf (dummy, automatic);
ca6e07
@@ -4261,6 +4260,7 @@ save_symbol (gfc_symbol *sym)
ca6e07
     return;
ca6e07
 
ca6e07
   if (sym->attr.in_common
ca6e07
+      || sym->attr.in_equivalence
ca6e07
       || sym->attr.dummy
ca6e07
       || sym->attr.result
ca6e07
       || sym->attr.flavor != FL_VARIABLE)
ca6e07
diff --git a/gcc/fortran/trans-common.c b/gcc/fortran/trans-common.c
ca6e07
index debdbd98ac0..a5fb230bb1b 100644
ca6e07
--- a/gcc/fortran/trans-common.c
ca6e07
+++ b/gcc/fortran/trans-common.c
ca6e07
@@ -339,7 +339,7 @@ build_field (segment_info *h, tree union_type, record_layout_info rli)
ca6e07
 /* Get storage for local equivalence.  */
ca6e07
 
ca6e07
 static tree
ca6e07
-build_equiv_decl (tree union_type, bool is_init, bool is_saved)
ca6e07
+build_equiv_decl (tree union_type, bool is_init, bool is_saved, bool is_auto)
ca6e07
 {
ca6e07
   tree decl;
ca6e07
   char name[18];
ca6e07
@@ -359,8 +359,8 @@ build_equiv_decl (tree union_type, bool is_init, bool is_saved)
ca6e07
   DECL_ARTIFICIAL (decl) = 1;
ca6e07
   DECL_IGNORED_P (decl) = 1;
ca6e07
 
ca6e07
-  if (!gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
ca6e07
-      || is_saved)
ca6e07
+  if (!is_auto && (!gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
ca6e07
+      || is_saved))
ca6e07
     TREE_STATIC (decl) = 1;
ca6e07
 
ca6e07
   TREE_ADDRESSABLE (decl) = 1;
ca6e07
@@ -611,6 +611,7 @@ create_common (gfc_common_head *com, segment_info *head, bool saw_equiv)
ca6e07
   tree decl;
ca6e07
   bool is_init = false;
ca6e07
   bool is_saved = false;
ca6e07
+  bool is_auto = false;
ca6e07
 
ca6e07
   /* Declare the variables inside the common block.
ca6e07
      If the current common block contains any equivalence object, then
ca6e07
@@ -654,6 +655,10 @@ create_common (gfc_common_head *com, segment_info *head, bool saw_equiv)
ca6e07
       /* Has SAVE attribute.  */
ca6e07
       if (s->sym->attr.save)
ca6e07
         is_saved = true;
ca6e07
+
ca6e07
+      /* Has AUTOMATIC attribute.  */
ca6e07
+      if (s->sym->attr.automatic)
ca6e07
+	is_auto = true;
ca6e07
     }
ca6e07
 
ca6e07
   finish_record_layout (rli, true);
ca6e07
@@ -661,7 +666,7 @@ create_common (gfc_common_head *com, segment_info *head, bool saw_equiv)
ca6e07
   if (com)
ca6e07
     decl = build_common_decl (com, union_type, is_init);
ca6e07
   else
ca6e07
-    decl = build_equiv_decl (union_type, is_init, is_saved);
ca6e07
+    decl = build_equiv_decl (union_type, is_init, is_saved, is_auto);
ca6e07
 
ca6e07
   if (is_init)
ca6e07
     {
ca6e07
@@ -948,6 +953,61 @@ add_condition (segment_info *f, gfc_equiv *eq1, gfc_equiv *eq2)
ca6e07
     confirm_condition (f, eq1, n, eq2);
ca6e07
 }
ca6e07
 
ca6e07
+static void
ca6e07
+accumulate_equivalence_attributes (symbol_attribute *dummy_symbol, gfc_equiv *e)
ca6e07
+{
ca6e07
+  symbol_attribute attr = e->expr->symtree->n.sym->attr;
ca6e07
+
ca6e07
+  dummy_symbol->dummy |= attr.dummy;
ca6e07
+  dummy_symbol->pointer |= attr.pointer;
ca6e07
+  dummy_symbol->target |= attr.target;
ca6e07
+  dummy_symbol->external |= attr.external;
ca6e07
+  dummy_symbol->intrinsic |= attr.intrinsic;
ca6e07
+  dummy_symbol->allocatable |= attr.allocatable;
ca6e07
+  dummy_symbol->elemental |= attr.elemental;
ca6e07
+  dummy_symbol->recursive |= attr.recursive;
ca6e07
+  dummy_symbol->in_common |= attr.in_common;
ca6e07
+  dummy_symbol->result |= attr.result;
ca6e07
+  dummy_symbol->in_namelist |= attr.in_namelist;
ca6e07
+  dummy_symbol->optional |= attr.optional;
ca6e07
+  dummy_symbol->entry |= attr.entry;
ca6e07
+  dummy_symbol->function |= attr.function;
ca6e07
+  dummy_symbol->subroutine |= attr.subroutine;
ca6e07
+  dummy_symbol->dimension |= attr.dimension;
ca6e07
+  dummy_symbol->in_equivalence |= attr.in_equivalence;
ca6e07
+  dummy_symbol->use_assoc |= attr.use_assoc;
ca6e07
+  dummy_symbol->cray_pointer |= attr.cray_pointer;
ca6e07
+  dummy_symbol->cray_pointee |= attr.cray_pointee;
ca6e07
+  dummy_symbol->data |= attr.data;
ca6e07
+  dummy_symbol->value |= attr.value;
ca6e07
+  dummy_symbol->volatile_ |= attr.volatile_;
ca6e07
+  dummy_symbol->is_protected |= attr.is_protected;
ca6e07
+  dummy_symbol->is_bind_c |= attr.is_bind_c;
ca6e07
+  dummy_symbol->procedure |= attr.procedure;
ca6e07
+  dummy_symbol->proc_pointer |= attr.proc_pointer;
ca6e07
+  dummy_symbol->abstract |= attr.abstract;
ca6e07
+  dummy_symbol->asynchronous |= attr.asynchronous;
ca6e07
+  dummy_symbol->codimension |= attr.codimension;
ca6e07
+  dummy_symbol->contiguous |= attr.contiguous;
ca6e07
+  dummy_symbol->generic |= attr.generic;
ca6e07
+  dummy_symbol->automatic |= attr.automatic;
ca6e07
+  dummy_symbol->threadprivate |= attr.threadprivate;
ca6e07
+  dummy_symbol->omp_declare_target |= attr.omp_declare_target;
ca6e07
+  dummy_symbol->omp_declare_target_link |= attr.omp_declare_target_link;
ca6e07
+  dummy_symbol->oacc_declare_copyin |= attr.oacc_declare_copyin;
ca6e07
+  dummy_symbol->oacc_declare_create |= attr.oacc_declare_create;
ca6e07
+  dummy_symbol->oacc_declare_deviceptr |= attr.oacc_declare_deviceptr;
ca6e07
+  dummy_symbol->oacc_declare_device_resident
ca6e07
+    |= attr.oacc_declare_device_resident;
ca6e07
+
ca6e07
+  /* Not strictly correct, but probably close enough.  */
ca6e07
+  if (attr.save > dummy_symbol->save)
ca6e07
+    dummy_symbol->save = attr.save;
ca6e07
+  if (attr.intent > dummy_symbol->intent)
ca6e07
+    dummy_symbol->intent = attr.intent;
ca6e07
+  if (attr.access > dummy_symbol->access)
ca6e07
+    dummy_symbol->access = attr.access;
ca6e07
+}
ca6e07
 
ca6e07
 /* Given a segment element, search through the equivalence lists for unused
ca6e07
    conditions that involve the symbol.  Add these rules to the segment.  */
ca6e07
@@ -965,9 +1025,12 @@ find_equivalence (segment_info *n)
ca6e07
       eq = NULL;
ca6e07
 
ca6e07
       /* Search the equivalence list, including the root (first) element
ca6e07
-         for the symbol that owns the segment.  */
ca6e07
+	 for the symbol that owns the segment.  */
ca6e07
+      symbol_attribute dummy_symbol;
ca6e07
+      memset (&dummy_symbol, 0, sizeof (dummy_symbol));
ca6e07
       for (e2 = e1; e2; e2 = e2->eq)
ca6e07
 	{
ca6e07
+	  accumulate_equivalence_attributes (&dummy_symbol, e2);
ca6e07
 	  if (!e2->used && e2->expr->symtree->n.sym == n->sym)
ca6e07
 	    {
ca6e07
 	      eq = e2;
ca6e07
@@ -975,6 +1038,8 @@ find_equivalence (segment_info *n)
ca6e07
 	    }
ca6e07
 	}
ca6e07
 
ca6e07
+      check_conflict (&dummy_symbol, e1->expr->symtree->name, &e1->expr->where);
ca6e07
+
ca6e07
       /* Go to the next root element.  */
ca6e07
       if (eq == NULL)
ca6e07
 	continue;
ca6e07
diff --git a/gcc/testsuite/gfortran.dg/auto_in_equiv_1.f90 b/gcc/testsuite/gfortran.dg/auto_in_equiv_1.f90
ca6e07
new file mode 100644
ca6e07
index 00000000000..61bfd0738c5
ca6e07
--- /dev/null
ca6e07
+++ b/gcc/testsuite/gfortran.dg/auto_in_equiv_1.f90
ca6e07
@@ -0,0 +1,36 @@
ca6e07
+! { dg-compile }
ca6e07
+
ca6e07
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
ca6e07
+program test
ca6e07
+  call suba(0)
ca6e07
+  call subb(0)
ca6e07
+  call suba(1)
ca6e07
+
ca6e07
+contains
ca6e07
+  subroutine suba(option) 
ca6e07
+    integer, intent(in) :: option
ca6e07
+    integer, automatic :: a ! { dg-error "AUTOMATIC at \\(1\\) is a DEC extension" }
ca6e07
+    integer :: b
ca6e07
+    integer :: c
ca6e07
+    equivalence (a, b)
ca6e07
+    if (option.eq.0) then
ca6e07
+      ! initialise a and c
ca6e07
+      a = 9
ca6e07
+      c = 99
ca6e07
+      if (a.ne.b) stop 1
ca6e07
+      if (loc(a).ne.loc(b)) stop 2
ca6e07
+    else
ca6e07
+      ! a should've been overwritten
ca6e07
+      if (a.eq.9) stop 3
ca6e07
+    end if
ca6e07
+  end subroutine suba
ca6e07
+
ca6e07
+  subroutine subb(dummy)
ca6e07
+    integer, intent(in) :: dummy
ca6e07
+    integer, automatic :: x ! { dg-error "AUTOMATIC at \\(1\\) is a DEC extension" }
ca6e07
+    integer :: y
ca6e07
+    x = 77
ca6e07
+    y = 7
ca6e07
+  end subroutine subb
ca6e07
+
ca6e07
+end program test
ca6e07
diff --git a/gcc/testsuite/gfortran.dg/auto_in_equiv_2.f90 b/gcc/testsuite/gfortran.dg/auto_in_equiv_2.f90
ca6e07
new file mode 100644
ca6e07
index 00000000000..406e718604a
ca6e07
--- /dev/null
ca6e07
+++ b/gcc/testsuite/gfortran.dg/auto_in_equiv_2.f90
ca6e07
@@ -0,0 +1,38 @@
ca6e07
+! { dg-run }
ca6e07
+! { dg-options "-fdec-static" }
ca6e07
+
ca6e07
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
ca6e07
+
ca6e07
+program test
ca6e07
+  call suba(0)
ca6e07
+  call subb(0)
ca6e07
+  call suba(1)
ca6e07
+
ca6e07
+contains
ca6e07
+  subroutine suba(option) 
ca6e07
+    integer, intent(in) :: option
ca6e07
+    integer, automatic :: a
ca6e07
+    integer :: b
ca6e07
+    integer :: c
ca6e07
+    equivalence (a, b)
ca6e07
+    if (option.eq.0) then
ca6e07
+      ! initialise a and c
ca6e07
+      a = 9
ca6e07
+      c = 99
ca6e07
+      if (a.ne.b) stop 1
ca6e07
+      if (loc(a).ne.loc(b)) stop 2
ca6e07
+    else
ca6e07
+      ! a should've been overwritten
ca6e07
+      if (a.eq.9) stop 3
ca6e07
+    end if
ca6e07
+  end subroutine suba
ca6e07
+
ca6e07
+  subroutine subb(dummy)
ca6e07
+    integer, intent(in) :: dummy
ca6e07
+    integer, automatic :: x
ca6e07
+    integer :: y
ca6e07
+    x = 77
ca6e07
+    y = 7
ca6e07
+  end subroutine subb
ca6e07
+
ca6e07
+end program test
ca6e07
diff --git a/gcc/testsuite/gfortran.dg/auto_in_equiv_3.f90 b/gcc/testsuite/gfortran.dg/auto_in_equiv_3.f90
ca6e07
new file mode 100644
ca6e07
index 00000000000..c67aa8c6ac1
ca6e07
--- /dev/null
ca6e07
+++ b/gcc/testsuite/gfortran.dg/auto_in_equiv_3.f90
ca6e07
@@ -0,0 +1,63 @@
ca6e07
+! { dg-run }
ca6e07
+! { dg-options "-fdec-static -fno-automatic" }
ca6e07
+
ca6e07
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
ca6e07
+
ca6e07
+! Storage is NOT on the static unless explicitly specified using the
ca6e07
+! DEC extension "automatic". The address of the first local variable
ca6e07
+! is used to determine that storage for the automatic local variable
ca6e07
+! is different to that of a local variable with no attributes. The
ca6e07
+! contents of the local variable in suba should be overwritten by the
ca6e07
+! call to subb. 
ca6e07
+!
ca6e07
+program test
ca6e07
+  integer :: dummy
ca6e07
+  integer, parameter :: address = kind(loc(dummy))
ca6e07
+  integer(address) :: ad1
ca6e07
+  integer(address) :: ad2
ca6e07
+  integer(address) :: ad3
ca6e07
+  logical :: ok
ca6e07
+
ca6e07
+  call suba(0, ad1)
ca6e07
+  call subb(0, ad2)
ca6e07
+  call suba(1, ad1)
ca6e07
+  call subc(0, ad3)
ca6e07
+  ok = (ad1.eq.ad3).and.(ad1.ne.ad2)
ca6e07
+  if (.not.ok) stop 4
ca6e07
+
ca6e07
+contains
ca6e07
+  subroutine suba(option, addr) 
ca6e07
+    integer, intent(in) :: option
ca6e07
+    integer(address), intent(out) :: addr
ca6e07
+    integer, automatic :: a
ca6e07
+    integer :: b
ca6e07
+    equivalence (a, b)
ca6e07
+    addr = loc(a)
ca6e07
+    if (option.eq.0) then
ca6e07
+      ! initialise a and c
ca6e07
+      a = 9
ca6e07
+      if (a.ne.b) stop 1
ca6e07
+      if (loc(a).ne.loc(b)) stop 2
ca6e07
+    else
ca6e07
+      ! a should've been overwritten
ca6e07
+      if (a.eq.9) stop 3
ca6e07
+    end if
ca6e07
+  end subroutine suba
ca6e07
+
ca6e07
+  subroutine subb(dummy, addr)
ca6e07
+    integer, intent(in) :: dummy
ca6e07
+    integer(address), intent(out) :: addr
ca6e07
+    integer :: x
ca6e07
+    addr = loc(x)
ca6e07
+    x = 77
ca6e07
+  end subroutine subb
ca6e07
+
ca6e07
+  subroutine subc(dummy, addr)
ca6e07
+    integer, intent(in) :: dummy
ca6e07
+    integer(address), intent(out) :: addr
ca6e07
+    integer, automatic :: y
ca6e07
+    addr = loc(y)
ca6e07
+    y = 77
ca6e07
+  end subroutine subc
ca6e07
+
ca6e07
+end program test
ca6e07
-- 
ca6e07
2.11.0
ca6e07