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

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