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