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

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