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

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