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