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