Blame SOURCES/gcc11-fortran-fdec-old-init.patch

237f7c
From 8bcc0f85ed1718c0dd9033ad4a34df181aabaffe Mon Sep 17 00:00:00 2001
237f7c
From: Mark Eggleston <markeggleston@gcc.gnu.org>
237f7c
Date: Fri, 22 Jan 2021 13:11:06 +0000
237f7c
Subject: [PATCH 05/10] Allow old-style initializers in derived types
237f7c
237f7c
This allows simple declarations in derived types and structures, such as:
237f7c
    LOGICAL*1      NIL      /0/
237f7c
Only single value expressions are allowed at the moment.
237f7c
237f7c
Use -fdec-old-init to enable. Also enabled by -fdec.
237f7c
---
237f7c
 gcc/fortran/decl.c                            | 27 +++++++++++++++----
237f7c
 gcc/fortran/lang.opt                          |  4 +++
237f7c
 gcc/fortran/options.c                         |  1 +
237f7c
 ...ec_derived_types_initialised_old_style_1.f | 25 +++++++++++++++++
237f7c
 ...ec_derived_types_initialised_old_style_2.f | 25 +++++++++++++++++
237f7c
 ...ec_derived_types_initialised_old_style_3.f | 26 ++++++++++++++++++
237f7c
 6 files changed, 103 insertions(+), 5 deletions(-)
237f7c
 create mode 100644 gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_1.f
237f7c
 create mode 100644 gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_2.f
237f7c
 create mode 100644 gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_3.f
237f7c
237f7c
diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
237f7c
index 723915822f3..5c8c1b7981b 100644
237f7c
--- a/gcc/fortran/decl.c
237f7c
+++ b/gcc/fortran/decl.c
237f7c
@@ -2827,12 +2827,29 @@ variable_decl (int elem)
237f7c
          but not components of derived types.  */
237f7c
       else if (gfc_current_state () == COMP_DERIVED)
237f7c
 	{
237f7c
-	  gfc_error ("Invalid old style initialization for derived type "
237f7c
-		     "component at %C");
237f7c
-	  m = MATCH_ERROR;
237f7c
-	  goto cleanup;
237f7c
+	  if (flag_dec_old_init)
237f7c
+	    {
237f7c
+	      /* Attempt to match an old-style initializer which is a simple
237f7c
+		 integer or character expression; this will not work with
237f7c
+		 multiple values. */
237f7c
+	      m = gfc_match_init_expr (&initializer);
237f7c
+	      if (m == MATCH_ERROR)
237f7c
+		goto cleanup;
237f7c
+	      else if (m == MATCH_YES)
237f7c
+		{
237f7c
+		  m = gfc_match ("/");
237f7c
+		  if (m != MATCH_YES)
237f7c
+		    goto cleanup;
237f7c
+		}
237f7c
+	    }
237f7c
+	  else
237f7c
+	    {
237f7c
+	      gfc_error ("Invalid old style initialization for derived type "
237f7c
+			 "component at %C");
237f7c
+	      m = MATCH_ERROR;
237f7c
+	      goto cleanup;
237f7c
+	    }
237f7c
 	}
237f7c
-
237f7c
       /* For structure components, read the initializer as a special
237f7c
          expression and let the rest of this function apply the initializer
237f7c
          as usual.  */
237f7c
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
237f7c
index d527c106bd6..25cc948699b 100644
237f7c
--- a/gcc/fortran/lang.opt
237f7c
+++ b/gcc/fortran/lang.opt
237f7c
@@ -493,6 +493,10 @@ fdec-non-integer-index
237f7c
 Fortran Var(flag_dec_non_integer_index)
237f7c
 Enable support for non-integer substring indexes.
237f7c
 
237f7c
+fdec-old-init
237f7c
+Fortran Var(flag_dec_old_init)
237f7c
+Enable support for old style initializers in derived types.
237f7c
+
237f7c
 fdec-structure
237f7c
 Fortran Var(flag_dec_structure)
237f7c
 Enable support for DEC STRUCTURE/RECORD.
237f7c
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
237f7c
index 9a042f64881..d6bd36c3a8a 100644
237f7c
--- a/gcc/fortran/options.c
237f7c
+++ b/gcc/fortran/options.c
237f7c
@@ -79,6 +79,7 @@ set_dec_flags (int value)
237f7c
   SET_BITFLAG (flag_dec_char_conversions, value, value);
237f7c
   SET_BITFLAG (flag_dec_duplicates, value, value);
237f7c
   SET_BITFLAG (flag_dec_non_integer_index, value, value);
237f7c
+  SET_BITFLAG (flag_dec_old_init, value, value);
237f7c
 }
237f7c
 
237f7c
 /* Finalize DEC flags.  */
237f7c
diff --git a/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_1.f b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_1.f
237f7c
new file mode 100644
237f7c
index 00000000000..eac4f9bfcf1
237f7c
--- /dev/null
237f7c
+++ b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_1.f
237f7c
@@ -0,0 +1,25 @@
237f7c
+! { dg-do run }
237f7c
+! { dg-options "-fdec" }
237f7c
+!
237f7c
+! Test old style initializers in derived types
237f7c
+!
237f7c
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
237f7c
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
237f7c
+!
237f7c
+        PROGRAM spec_in_var
237f7c
+          TYPE STRUCT1
237f7c
+            INTEGER*4      ID       /8/
237f7c
+            INTEGER*4      TYPE     /5/
237f7c
+            INTEGER*8      DEFVAL   /0/
237f7c
+            CHARACTER*(5)  NAME     /'tests'/
237f7c
+            LOGICAL*1      NIL      /0/
237f7c
+          END TYPE STRUCT1
237f7c
+
237f7c
+          TYPE (STRUCT1) SINST
237f7c
+
237f7c
+          IF(SINST%ID.NE.8) STOP 1
237f7c
+          IF(SINST%TYPE.NE.5) STOP 2
237f7c
+          IF(SINST%DEFVAL.NE.0) STOP 3
237f7c
+          IF(SINST%NAME.NE.'tests') STOP 4
237f7c
+          IF(SINST%NIL) STOP 5
237f7c
+        END
237f7c
diff --git a/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_2.f b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_2.f
237f7c
new file mode 100644
237f7c
index 00000000000..d904c8b2974
237f7c
--- /dev/null
237f7c
+++ b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_2.f
237f7c
@@ -0,0 +1,25 @@
237f7c
+! { dg-do run }
237f7c
+! { dg-options "-std=legacy -fdec-old-init" }
237f7c
+!
237f7c
+! Test old style initializers in derived types
237f7c
+!
237f7c
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
237f7c
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
237f7c
+!
237f7c
+        PROGRAM spec_in_var
237f7c
+          TYPE STRUCT1
237f7c
+            INTEGER*4      ID       /8/
237f7c
+            INTEGER*4      TYPE     /5/
237f7c
+            INTEGER*8      DEFVAL   /0/
237f7c
+            CHARACTER*(5)  NAME     /'tests'/
237f7c
+            LOGICAL*1      NIL      /0/
237f7c
+          END TYPE STRUCT1
237f7c
+
237f7c
+          TYPE (STRUCT1) SINST
237f7c
+
237f7c
+          IF(SINST%ID.NE.8) STOP 1
237f7c
+          IF(SINST%TYPE.NE.5) STOP 2
237f7c
+          IF(SINST%DEFVAL.NE.0) STOP 3
237f7c
+          IF(SINST%NAME.NE.'tests') STOP 4
237f7c
+          IF(SINST%NIL) STOP 5
237f7c
+        END
237f7c
diff --git a/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_3.f b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_3.f
237f7c
new file mode 100644
237f7c
index 00000000000..58c2b4b66cf
237f7c
--- /dev/null
237f7c
+++ b/gcc/testsuite/gfortran.dg/dec_derived_types_initialised_old_style_3.f
237f7c
@@ -0,0 +1,26 @@
237f7c
+! { dg-do compile }
237f7c
+! { dg-options "-std=legacy -fdec -fno-dec-old-init" }
237f7c
+!
237f7c
+! Test old style initializers in derived types
237f7c
+!
237f7c
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
237f7c
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
237f7c
+!
237f7c
+
237f7c
+        PROGRAM spec_in_var
237f7c
+          TYPE STRUCT1
237f7c
+            INTEGER*4      ID       /8/ ! { dg-error "Invalid old style initialization" }
237f7c
+            INTEGER*4      TYPE     /5/ ! { dg-error "Invalid old style initialization" }
237f7c
+            INTEGER*8      DEFVAL   /0/ ! { dg-error "Invalid old style initialization" }
237f7c
+            CHARACTER*(5)  NAME     /'tests'/ ! { dg-error "Invalid old style initialization" }
237f7c
+            LOGICAL*1      NIL      /0/ ! { dg-error "Invalid old style initialization" }
237f7c
+          END TYPE STRUCT1
237f7c
+
237f7c
+          TYPE (STRUCT1) SINST
237f7c
+
237f7c
+          IF(SINST%ID.NE.8) STOP 1 ! { dg-error "'id' at \\(1\\) is not a member" }
237f7c
+          IF(SINST%TYPE.NE.5) STOP 2 ! { dg-error "'type' at \\(1\\) is not a member" }
237f7c
+          IF(SINST%DEFVAL.NE.0) STOP 3  ! { dg-error "'defval' at \\(1\\) is not a member" }
237f7c
+          IF(SINST%NAME.NE.'tests') STOP 4 ! { dg-error "'name' at \\(1\\) is not a member" }
237f7c
+          IF(SINST%NIL) STOP 5 ! { dg-error "'nil' at \\(1\\) is not a member" }
237f7c
+        END
237f7c
-- 
237f7c
2.27.0
237f7c