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

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