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

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