Blame SOURCES/gcc12-fortran-fdec-duplicates.patch

31df50
From 23b1fcb104c666429451ffaf936f8da5fcd3d43a Mon Sep 17 00:00:00 2001
31df50
From: Mark Eggleston <markeggleston@gcc.gnu.org>
31df50
Date: Fri, 22 Jan 2021 12:29:47 +0000
31df50
Subject: [PATCH 01/10] Allow duplicate declarations.
31df50
31df50
Enabled by -fdec-duplicates and -fdec.
31df50
31df50
Some fixes by Jim MacArthur <jim.macarthur@codethink.co.uk>
31df50
Addition of -fdec-duplicates by Mark Eggleston <mark.eggleston@codethink.com>
31df50
---
31df50
 gcc/fortran/lang.opt                          |  4 ++++
31df50
 gcc/fortran/options.cc                        |  1 +
31df50
 gcc/fortran/symbol.cc                         | 21 +++++++++++++++++--
31df50
 .../gfortran.dg/duplicate_type_4.f90          | 13 ++++++++++++
31df50
 .../gfortran.dg/duplicate_type_5.f90          | 13 ++++++++++++
31df50
 .../gfortran.dg/duplicate_type_6.f90          | 13 ++++++++++++
31df50
 .../gfortran.dg/duplicate_type_7.f90          | 13 ++++++++++++
31df50
 .../gfortran.dg/duplicate_type_8.f90          | 12 +++++++++++
31df50
 .../gfortran.dg/duplicate_type_9.f90          | 12 +++++++++++
31df50
 9 files changed, 100 insertions(+), 2 deletions(-)
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_4.f90
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_5.f90
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_6.f90
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_7.f90
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_8.f90
31df50
 create mode 100644 gcc/testsuite/gfortran.dg/duplicate_type_9.f90
31df50
31df50
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
31df50
index 2b1977c523b..52bd522051e 100644
31df50
--- a/gcc/fortran/lang.opt
31df50
+++ b/gcc/fortran/lang.opt
31df50
@@ -469,6 +469,10 @@ Fortran Var(flag_dec_char_conversions)
31df50
 Enable the use of character literals in assignments and data statements
31df50
 for non-character variables.
31df50
 
31df50
+fdec-duplicates
31df50
+Fortran Var(flag_dec_duplicates)
31df50
+Allow varibles to be duplicated in the type specification matches.
31df50
+
31df50
 fdec-include
31df50
 Fortran Var(flag_dec_include)
31df50
 Enable legacy parsing of INCLUDE as statement.
31df50
diff --git a/gcc/fortran/options.cc b/gcc/fortran/options.cc
31df50
index 3a0b98bf1ec..f19ba87f8a0 100644
31df50
--- a/gcc/fortran/options.cc
31df50
+++ b/gcc/fortran/options.cc
31df50
@@ -77,6 +77,7 @@ set_dec_flags (int value)
31df50
   SET_BITFLAG (flag_dec_format_defaults, value, value);
31df50
   SET_BITFLAG (flag_dec_blank_format_item, value, value);
31df50
   SET_BITFLAG (flag_dec_char_conversions, value, value);
31df50
+  SET_BITFLAG (flag_dec_duplicates, value, value);
31df50
 }
31df50
 
31df50
 /* Finalize DEC flags.  */
31df50
diff --git a/gcc/fortran/symbol.cc b/gcc/fortran/symbol.cc
31df50
index 3b988d1be22..9843175cc2a 100644
31df50
--- a/gcc/fortran/symbol.cc
31df50
+++ b/gcc/fortran/symbol.cc
31df50
@@ -1995,6 +1995,8 @@ gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
31df50
   if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
31df50
     type = sym->ns->proc_name->ts.type;
31df50
 
31df50
+  flavor = sym->attr.flavor;
31df50
+
31df50
   if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
31df50
       && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
31df50
 	   && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
31df50
@@ -2007,6 +2009,23 @@ gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
31df50
       else if (sym->attr.function && sym->attr.result)
31df50
 	gfc_error ("Symbol %qs at %L already has basic type of %s",
31df50
 		   sym->ns->proc_name->name, where, gfc_basic_typename (type));
31df50
+      else if (flag_dec_duplicates)
31df50
+	{
31df50
+	  /* Ignore temporaries and class/procedure names */
31df50
+	  if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS
31df50
+	      || sym->ts.type == BT_PROCEDURE)
31df50
+	    return false;
31df50
+
31df50
+	  if (gfc_compare_types (&sym->ts, ts)
31df50
+	      && (flavor == FL_UNKNOWN || flavor == FL_VARIABLE
31df50
+	      || flavor == FL_PROCEDURE))
31df50
+	    {
31df50
+	      return gfc_notify_std (GFC_STD_LEGACY,
31df50
+				     "Symbol '%qs' at %L already has "
31df50
+				     "basic type of %s", sym->name, where,
31df50
+				     gfc_basic_typename (type));
31df50
+	    }
31df50
+	}
31df50
       else
31df50
 	gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
31df50
 		   where, gfc_basic_typename (type));
31df50
@@ -2020,8 +2039,6 @@ gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
31df50
       return false;
31df50
     }
31df50
 
31df50
-  flavor = sym->attr.flavor;
31df50
-
31df50
   if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
31df50
       || flavor == FL_LABEL
31df50
       || (flavor == FL_PROCEDURE && sym->attr.subroutine)
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_4.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_4.f90
31df50
new file mode 100644
31df50
index 00000000000..cdd29ea8846
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_4.f90
31df50
@@ -0,0 +1,13 @@
31df50
+! { dg-do compile }
31df50
+! { dg-options "-std=f95" }
31df50
+
31df50
+! PR fortran/30239
31df50
+! Check for errors when a symbol gets declared a type twice, even if it
31df50
+! is the same.
31df50
+
31df50
+INTEGER FUNCTION foo ()
31df50
+  IMPLICIT NONE
31df50
+  INTEGER :: x
31df50
+  INTEGER :: x ! { dg-error "basic type of" }
31df50
+  x = 42
31df50
+END FUNCTION foo
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_5.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_5.f90
31df50
new file mode 100644
31df50
index 00000000000..00f931809aa
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_5.f90
31df50
@@ -0,0 +1,13 @@
31df50
+! { dg-do run }
31df50
+! { dg-options "-fdec" }
31df50
+!
31df50
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
31df50
+!
31df50
+
31df50
+program test
31df50
+  implicit none
31df50
+  integer :: x
31df50
+  integer :: x
31df50
+  x = 42
31df50
+  if (x /= 42) stop 1
31df50
+end program test
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_6.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_6.f90
31df50
new file mode 100644
31df50
index 00000000000..f0df27e323c
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_6.f90
31df50
@@ -0,0 +1,13 @@
31df50
+! { dg-do run }
31df50
+! { dg-options "-std=legacy -fdec-duplicates" }
31df50
+!
31df50
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
31df50
+!
31df50
+
31df50
+program test
31df50
+  implicit none
31df50
+  integer :: x
31df50
+  integer :: x
31df50
+  x = 42
31df50
+  if (x /= 42) stop 1
31df50
+end program test
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_7.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_7.f90
31df50
new file mode 100644
31df50
index 00000000000..f32472ff586
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_7.f90
31df50
@@ -0,0 +1,13 @@
31df50
+! { dg-do run }
31df50
+! { dg-options "-fdec-duplicates" }
31df50
+!
31df50
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
31df50
+!
31df50
+
31df50
+program test
31df50
+  implicit none
31df50
+  integer :: x
31df50
+  integer :: x! { dg-warning "Legacy Extension" }
31df50
+  x = 42
31df50
+  if (x /= 42) stop 1
31df50
+end program test
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_8.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_8.f90
31df50
new file mode 100644
31df50
index 00000000000..23c94add179
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_8.f90
31df50
@@ -0,0 +1,12 @@
31df50
+! { dg-do compile }
31df50
+! { dg-options "-fdec -fno-dec-duplicates" }
31df50
+!
31df50
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
31df50
+!
31df50
+
31df50
+integer function foo ()
31df50
+  implicit none
31df50
+  integer :: x
31df50
+  integer :: x ! { dg-error "basic type of" }
31df50
+  x = 42
31df50
+end function foo
31df50
diff --git a/gcc/testsuite/gfortran.dg/duplicate_type_9.f90 b/gcc/testsuite/gfortran.dg/duplicate_type_9.f90
31df50
new file mode 100644
31df50
index 00000000000..d5edee4d8ee
31df50
--- /dev/null
31df50
+++ b/gcc/testsuite/gfortran.dg/duplicate_type_9.f90
31df50
@@ -0,0 +1,12 @@
31df50
+! { dg-do compile }
31df50
+! { dg-options "-fdec-duplicates -fno-dec-duplicates" }
31df50
+!
31df50
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
31df50
+!
31df50
+
31df50
+integer function foo ()
31df50
+  implicit none
31df50
+  integer :: x
31df50
+  integer :: x ! { dg-error "basic type of" }
31df50
+  x = 42
31df50
+end function foo
31df50
-- 
31df50
2.27.0
31df50