Blame SOURCES/gcc11-fortran-fdec-non-integer-index.patch

2e9d4a
From 67aef262311d6a746786ee0f59748ccaa7e1e711 Mon Sep 17 00:00:00 2001
2e9d4a
From: Mark Eggleston <markeggleston@gcc.gnu.org>
2e9d4a
Date: Fri, 22 Jan 2021 13:09:54 +0000
2e9d4a
Subject: [PATCH 04/10] Allow non-integer substring indexes
2e9d4a
2e9d4a
Use -fdec-non-integer-index compiler flag to enable. Also enabled by -fdec.
2e9d4a
---
2e9d4a
 gcc/fortran/lang.opt                          |  4 ++++
2e9d4a
 gcc/fortran/options.c                         |  1 +
2e9d4a
 gcc/fortran/resolve.c                         | 20 +++++++++++++++++++
2e9d4a
 .../dec_not_integer_substring_indexes_1.f     | 18 +++++++++++++++++
2e9d4a
 .../dec_not_integer_substring_indexes_2.f     | 18 +++++++++++++++++
2e9d4a
 .../dec_not_integer_substring_indexes_3.f     | 18 +++++++++++++++++
2e9d4a
 6 files changed, 79 insertions(+)
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_1.f
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_2.f
2e9d4a
 create mode 100644 gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_3.f
2e9d4a
2e9d4a
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
2e9d4a
index c4da248f07c..d527c106bd6 100644
2e9d4a
--- a/gcc/fortran/lang.opt
2e9d4a
+++ b/gcc/fortran/lang.opt
2e9d4a
@@ -489,6 +489,10 @@ fdec-math
2e9d4a
 Fortran Var(flag_dec_math)
2e9d4a
 Enable legacy math intrinsics for compatibility.
2e9d4a
 
2e9d4a
+fdec-non-integer-index
2e9d4a
+Fortran Var(flag_dec_non_integer_index)
2e9d4a
+Enable support for non-integer substring indexes.
2e9d4a
+
2e9d4a
 fdec-structure
2e9d4a
 Fortran Var(flag_dec_structure)
2e9d4a
 Enable support for DEC STRUCTURE/RECORD.
2e9d4a
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
2e9d4a
index f19ba87f8a0..9a042f64881 100644
2e9d4a
--- a/gcc/fortran/options.c
2e9d4a
+++ b/gcc/fortran/options.c
2e9d4a
@@ -78,6 +78,7 @@ set_dec_flags (int value)
2e9d4a
   SET_BITFLAG (flag_dec_blank_format_item, value, value);
2e9d4a
   SET_BITFLAG (flag_dec_char_conversions, value, value);
2e9d4a
   SET_BITFLAG (flag_dec_duplicates, value, value);
2e9d4a
+  SET_BITFLAG (flag_dec_non_integer_index, value, value);
2e9d4a
 }
2e9d4a
 
2e9d4a
 /* Finalize DEC flags.  */
2e9d4a
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
2e9d4a
index 4b90cb59902..bc0df0fdb99 100644
2e9d4a
--- a/gcc/fortran/resolve.c
2e9d4a
+++ b/gcc/fortran/resolve.c
2e9d4a
@@ -5131,6 +5131,16 @@ gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
2e9d4a
       if (!gfc_resolve_expr (ref->u.ss.start))
2e9d4a
 	return false;
2e9d4a
 
2e9d4a
+      /* In legacy mode, allow non-integer string indexes by converting */
2e9d4a
+      if (flag_dec_non_integer_index && ref->u.ss.start->ts.type != BT_INTEGER
2e9d4a
+	  && gfc_numeric_ts (&ref->u.ss.start->ts))
2e9d4a
+	{
2e9d4a
+	  gfc_typespec t;
2e9d4a
+	  t.type = BT_INTEGER;
2e9d4a
+	  t.kind = ref->u.ss.start->ts.kind;
2e9d4a
+	  gfc_convert_type_warn (ref->u.ss.start, &t, 2, 1);
2e9d4a
+	}
2e9d4a
+
2e9d4a
       if (ref->u.ss.start->ts.type != BT_INTEGER)
2e9d4a
 	{
2e9d4a
 	  gfc_error ("Substring start index at %L must be of type INTEGER",
2e9d4a
@@ -5160,6 +5170,16 @@ gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
2e9d4a
       if (!gfc_resolve_expr (ref->u.ss.end))
2e9d4a
 	return false;
2e9d4a
 
2e9d4a
+      /* Non-integer string index endings, as for start */
2e9d4a
+      if (flag_dec_non_integer_index && ref->u.ss.end->ts.type != BT_INTEGER
2e9d4a
+	  && gfc_numeric_ts (&ref->u.ss.end->ts))
2e9d4a
+	{
2e9d4a
+	  gfc_typespec t;
2e9d4a
+	  t.type = BT_INTEGER;
2e9d4a
+	  t.kind = ref->u.ss.end->ts.kind;
2e9d4a
+	  gfc_convert_type_warn (ref->u.ss.end, &t, 2, 1);
2e9d4a
+	}
2e9d4a
+
2e9d4a
       if (ref->u.ss.end->ts.type != BT_INTEGER)
2e9d4a
 	{
2e9d4a
 	  gfc_error ("Substring end index at %L must be of type INTEGER",
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_1.f b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_1.f
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..0be28abaa4b
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_1.f
2e9d4a
@@ -0,0 +1,18 @@
2e9d4a
+! { dg-do run }
2e9d4a
+! { dg-options "-fdec" }
2e9d4a
+!
2e9d4a
+! Test not integer substring indexes
2e9d4a
+!
2e9d4a
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
2e9d4a
+!
2e9d4a
+        PROGRAM not_integer_substring_indexes
2e9d4a
+          CHARACTER*5 st/'Tests'/
2e9d4a
+          REAL ir/1.0/
2e9d4a
+          REAL ir2/4.0/
2e9d4a
+
2e9d4a
+          if (st(ir:4).ne.'Test') stop 1
2e9d4a
+          if (st(1:ir2).ne.'Test') stop 2
2e9d4a
+          if (st(1.0:4).ne.'Test') stop 3
2e9d4a
+          if (st(1:4.0).ne.'Test') stop 4
2e9d4a
+          if (st(2.5:4).ne.'est') stop 5
2e9d4a
+        END
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_2.f b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_2.f
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..3cf05296d0c
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_2.f
2e9d4a
@@ -0,0 +1,18 @@
2e9d4a
+! { dg-do run }
2e9d4a
+! { dg-options "-fdec-non-integer-index" }
2e9d4a
+!
2e9d4a
+! Test not integer substring indexes
2e9d4a
+!
2e9d4a
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
2e9d4a
+!
2e9d4a
+        PROGRAM not_integer_substring_indexes
2e9d4a
+          CHARACTER*5 st/'Tests'/
2e9d4a
+          REAL ir/1.0/
2e9d4a
+          REAL ir2/4.0/
2e9d4a
+
2e9d4a
+          if (st(ir:4).ne.'Test') stop 1
2e9d4a
+          if (st(1:ir2).ne.'Test') stop 2
2e9d4a
+          if (st(1.0:4).ne.'Test') stop 3
2e9d4a
+          if (st(1:4.0).ne.'Test') stop 4
2e9d4a
+          if (st(2.5:4).ne.'est') stop 5
2e9d4a
+        END
2e9d4a
diff --git a/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_3.f b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_3.f
2e9d4a
new file mode 100644
2e9d4a
index 00000000000..703de995897
2e9d4a
--- /dev/null
2e9d4a
+++ b/gcc/testsuite/gfortran.dg/dec_not_integer_substring_indexes_3.f
2e9d4a
@@ -0,0 +1,18 @@
2e9d4a
+! { dg-do compile }
2e9d4a
+! { dg-options "-fdec -fno-dec-non-integer-index" }
2e9d4a
+!
2e9d4a
+! Test not integer substring indexes
2e9d4a
+!
2e9d4a
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
2e9d4a
+!
2e9d4a
+        PROGRAM not_integer_substring_indexes
2e9d4a
+          CHARACTER*5 st/'Tests'/
2e9d4a
+          REAL ir/1.0/
2e9d4a
+          REAL ir2/4.0/
2e9d4a
+
2e9d4a
+          if (st(ir:4).ne.'Test') stop 1 ! { dg-error "Substring start index" }
2e9d4a
+          if (st(1:ir2).ne.'Test') stop 2 ! { dg-error "Substring end index" }
2e9d4a
+          if (st(1.0:4).ne.'Test') stop 3 ! { dg-error "Substring start index" }
2e9d4a
+          if (st(1:4.0).ne.'Test') stop 4 ! { dg-error "Substring end index" }
2e9d4a
+          if (st(2.5:4).ne.'est') stop 5 ! { dg-error "Substring start index" }
2e9d4a
+        END
2e9d4a
-- 
2e9d4a
2.27.0
2e9d4a