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

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