c7b8388
From 786869fd62813e80da9b6545a295d53c36275c19 Mon Sep 17 00:00:00 2001
c7b8388
From: Mark Eggleston <markeggleston@gcc.gnu.org>
c7b8388
Date: Fri, 22 Jan 2021 13:12:14 +0000
c7b8388
Subject: [PATCH 06/10] Allow string length and kind to be specified on a per
c7b8388
 variable basis.
c7b8388
c7b8388
This allows kind/length to be mixed with array specification in
c7b8388
declarations.
c7b8388
c7b8388
e.g.
c7b8388
c7b8388
      INTEGER*4 x*2, y*8
c7b8388
      CHARACTER names*20(10)
c7b8388
      REAL v(100)*8, vv*4(50)
c7b8388
c7b8388
The per-variable size overrides the kind or length specified for the type.
c7b8388
c7b8388
Use -fdec-override-kind to enable. Also enabled by -fdec.
c7b8388
c7b8388
Note: this feature is a merger of two previously separate features.
c7b8388
c7b8388
Now accepts named constants as kind parameters:
c7b8388
c7b8388
      INTEGER A
c7b8388
      PARAMETER (A=2)
c7b8388
      INTEGER B*(A)
c7b8388
c7b8388
Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
c7b8388
Now rejects invalid kind parameters and prints error messages:
c7b8388
c7b8388
      INTEGER X*3
c7b8388
c7b8388
caused an internal compiler error.
c7b8388
c7b8388
Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
---
595f09c
 gcc/fortran/decl.cc                           | 156 ++++++++++++++----
c7b8388
 gcc/fortran/lang.opt                          |   4 +
595f09c
 gcc/fortran/options.cc                        |   1 +
c7b8388
 .../dec_mixed_char_array_declaration_1.f      |  13 ++
c7b8388
 .../dec_mixed_char_array_declaration_2.f      |  13 ++
c7b8388
 .../dec_mixed_char_array_declaration_3.f      |  13 ++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_1.f      |  31 ++++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_2.f      |  31 ++++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_3.f      |  31 ++++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_4.f      |  14 ++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_5.f      |  19 +++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_6.f      |  19 +++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_7.f      |  15 ++
c7b8388
 .../gfortran.dg/dec_spec_in_variable_8.f      |  14 ++
c7b8388
 14 files changed, 340 insertions(+), 34 deletions(-)
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
c7b8388
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
c7b8388
595f09c
diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
c7b8388
index 5c8c1b7981b..f7dc9d8263d 100644
595f09c
--- a/gcc/fortran/decl.cc
595f09c
+++ b/gcc/fortran/decl.cc
c7b8388
@@ -1213,6 +1213,54 @@ syntax:
c7b8388
   return MATCH_ERROR;
c7b8388
 }
c7b8388
 
c7b8388
+/* This matches the nonstandard kind given after a variable name, like:
c7b8388
+   INTEGER x*2, y*4
c7b8388
+   The per-variable kind will override any kind given in the type
c7b8388
+   declaration.
c7b8388
+*/
c7b8388
+
c7b8388
+static match
c7b8388
+match_per_symbol_kind (int *length)
c7b8388
+{
c7b8388
+  match m;
c7b8388
+  gfc_expr *expr = NULL;
c7b8388
+
c7b8388
+  m = gfc_match_char ('*');
c7b8388
+  if (m != MATCH_YES)
c7b8388
+    return m;
c7b8388
+
c7b8388
+  m = gfc_match_small_literal_int (length, NULL);
c7b8388
+  if (m == MATCH_YES || m == MATCH_ERROR)
c7b8388
+    return m;
c7b8388
+
c7b8388
+  if (gfc_match_char ('(') == MATCH_NO)
c7b8388
+    return MATCH_ERROR;
c7b8388
+
c7b8388
+  m = gfc_match_expr (&expr;;
c7b8388
+  if (m == MATCH_YES)
c7b8388
+    {
c7b8388
+      m = MATCH_ERROR; // Assume error
c7b8388
+      if (gfc_expr_check_typed (expr, gfc_current_ns, false))
c7b8388
+	{
c7b8388
+	  if ((expr->expr_type == EXPR_CONSTANT)
c7b8388
+	      && (expr->ts.type == BT_INTEGER))
c7b8388
+	    {
c7b8388
+	      *length = mpz_get_si(expr->value.integer);
c7b8388
+	      m = MATCH_YES;
c7b8388
+	    }
c7b8388
+	}
c7b8388
+
c7b8388
+	if (m == MATCH_YES)
c7b8388
+	  {
c7b8388
+	    if (gfc_match_char (')') == MATCH_NO)
c7b8388
+	       m = MATCH_ERROR;
c7b8388
+  }
c7b8388
+     }
c7b8388
+
c7b8388
+  if (expr != NULL)
c7b8388
+     gfc_free_expr (expr);
c7b8388
+  return m;
c7b8388
+}
c7b8388
 
c7b8388
 /* Special subroutine for finding a symbol.  Check if the name is found
c7b8388
    in the current name space.  If not, and we're compiling a function or
c7b8388
@@ -2443,6 +2491,35 @@ check_function_name (char *name)
c7b8388
 }
c7b8388
 
c7b8388
 
c7b8388
+static match
c7b8388
+match_character_length_clause (gfc_charlen **cl, bool *cl_deferred, int elem)
c7b8388
+{
c7b8388
+  gfc_expr* char_len;
c7b8388
+  char_len = NULL;
c7b8388
+
c7b8388
+  match m = match_char_length (&char_len, cl_deferred, false);
c7b8388
+  if (m == MATCH_YES)
c7b8388
+    {
c7b8388
+      *cl = gfc_new_charlen (gfc_current_ns, NULL);
c7b8388
+      (*cl)->length = char_len;
c7b8388
+    }
c7b8388
+  else if (m == MATCH_NO)
c7b8388
+    {
c7b8388
+      if (elem > 1
c7b8388
+	  && (current_ts.u.cl->length == NULL
c7b8388
+	      || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
c7b8388
+	{
c7b8388
+	  *cl = gfc_new_charlen (gfc_current_ns, NULL);
c7b8388
+	  (*cl)->length = gfc_copy_expr (current_ts.u.cl->length);
c7b8388
+	}
c7b8388
+      else
c7b8388
+      *cl = current_ts.u.cl;
c7b8388
+
c7b8388
+      *cl_deferred = current_ts.deferred;
c7b8388
+    }
c7b8388
+  return m;
c7b8388
+}
c7b8388
+
c7b8388
 /* Match a variable name with an optional initializer.  When this
c7b8388
    subroutine is called, a variable is expected to be parsed next.
c7b8388
    Depending on what is happening at the moment, updates either the
c7b8388
@@ -2453,7 +2530,7 @@ variable_decl (int elem)
c7b8388
 {
c7b8388
   char name[GFC_MAX_SYMBOL_LEN + 1];
c7b8388
   static unsigned int fill_id = 0;
c7b8388
-  gfc_expr *initializer, *char_len;
c7b8388
+  gfc_expr *initializer;
c7b8388
   gfc_array_spec *as;
c7b8388
   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
c7b8388
   gfc_charlen *cl;
c7b8388
@@ -2462,11 +2539,15 @@ variable_decl (int elem)
c7b8388
   match m;
c7b8388
   bool t;
c7b8388
   gfc_symbol *sym;
c7b8388
+  match cl_match;
c7b8388
+  match kind_match;
c7b8388
+  int overridden_kind;
c7b8388
   char c;
c7b8388
 
c7b8388
   initializer = NULL;
c7b8388
   as = NULL;
c7b8388
   cp_as = NULL;
c7b8388
+  kind_match = MATCH_NO;
c7b8388
 
c7b8388
   /* When we get here, we've just matched a list of attributes and
c7b8388
      maybe a type and a double colon.  The next thing we expect to see
c7b8388
@@ -2519,6 +2600,28 @@ variable_decl (int elem)
c7b8388
 
c7b8388
   var_locus = gfc_current_locus;
c7b8388
 
c7b8388
+
c7b8388
+  cl = NULL;
c7b8388
+  cl_deferred = false;
c7b8388
+  cl_match = MATCH_NO;
c7b8388
+
c7b8388
+  /* Check for a character length clause before an array clause */
c7b8388
+  if (flag_dec_override_kind)
c7b8388
+    {
c7b8388
+      if (current_ts.type == BT_CHARACTER)
c7b8388
+	{
c7b8388
+	  cl_match = match_character_length_clause (&cl, &cl_deferred, elem);
c7b8388
+	  if (cl_match == MATCH_ERROR)
c7b8388
+	    goto cleanup;
c7b8388
+	}
c7b8388
+      else
c7b8388
+	{
c7b8388
+	  kind_match = match_per_symbol_kind (&overridden_kind);
c7b8388
+	  if (kind_match == MATCH_ERROR)
c7b8388
+	    goto cleanup;
c7b8388
+	}
c7b8388
+    }
c7b8388
+
c7b8388
   /* Now we could see the optional array spec. or character length.  */
c7b8388
   m = gfc_match_array_spec (&as, true, true);
c7b8388
   if (m == MATCH_ERROR)
c7b8388
@@ -2667,40 +2770,12 @@ variable_decl (int elem)
c7b8388
 	}
c7b8388
     }
c7b8388
 
c7b8388
-  char_len = NULL;
c7b8388
-  cl = NULL;
c7b8388
-  cl_deferred = false;
c7b8388
-
c7b8388
-  if (current_ts.type == BT_CHARACTER)
c7b8388
+  /* Second chance for a character length clause */
c7b8388
+  if (cl_match == MATCH_NO && current_ts.type == BT_CHARACTER)
c7b8388
     {
c7b8388
-      switch (match_char_length (&char_len, &cl_deferred, false))
c7b8388
-	{
c7b8388
-	case MATCH_YES:
c7b8388
-	  cl = gfc_new_charlen (gfc_current_ns, NULL);
c7b8388
-
c7b8388
-	  cl->length = char_len;
c7b8388
-	  break;
c7b8388
-
c7b8388
-	/* Non-constant lengths need to be copied after the first
c7b8388
-	   element.  Also copy assumed lengths.  */
c7b8388
-	case MATCH_NO:
c7b8388
-	  if (elem > 1
c7b8388
-	      && (current_ts.u.cl->length == NULL
c7b8388
-		  || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
c7b8388
-	    {
c7b8388
-	      cl = gfc_new_charlen (gfc_current_ns, NULL);
c7b8388
-	      cl->length = gfc_copy_expr (current_ts.u.cl->length);
c7b8388
-	    }
c7b8388
-	  else
c7b8388
-	    cl = current_ts.u.cl;
c7b8388
-
c7b8388
-	  cl_deferred = current_ts.deferred;
c7b8388
-
c7b8388
-	  break;
c7b8388
-
c7b8388
-	case MATCH_ERROR:
c7b8388
-	  goto cleanup;
c7b8388
-	}
c7b8388
+      m = match_character_length_clause (&cl, &cl_deferred, elem);
c7b8388
+      if (m == MATCH_ERROR)
c7b8388
+	goto cleanup;
c7b8388
     }
c7b8388
 
c7b8388
   /* The dummy arguments and result of the abreviated form of MODULE
c7b8388
@@ -2802,6 +2877,19 @@ variable_decl (int elem)
c7b8388
       goto cleanup;
c7b8388
     }
c7b8388
 
c7b8388
+  if (kind_match == MATCH_YES)
c7b8388
+    {
c7b8388
+      gfc_find_symbol (name, gfc_current_ns, 1, &sym);
c7b8388
+      /* sym *must* be found at this point */
c7b8388
+      sym->ts.kind = overridden_kind;
c7b8388
+      if (gfc_validate_kind (sym->ts.type, sym->ts.kind, true) < 0)
c7b8388
+	{
c7b8388
+	  gfc_error ("Kind %d not supported for type %s at %C",
c7b8388
+		     sym->ts.kind, gfc_basic_typename (sym->ts.type));
c7b8388
+	  return MATCH_ERROR;
c7b8388
+	}
c7b8388
+    }
c7b8388
+
c7b8388
   if (!check_function_name (name))
c7b8388
     {
c7b8388
       m = MATCH_ERROR;
c7b8388
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
c7b8388
index 25cc948699b..4a269ebb22d 100644
c7b8388
--- a/gcc/fortran/lang.opt
c7b8388
+++ b/gcc/fortran/lang.opt
91b3540
@@ -502,6 +502,10 @@ fdec-math
91b3540
 Fortran Var(flag_dec_math)
91b3540
 Enable legacy math intrinsics for compatibility.
c7b8388
 
c7b8388
+fdec-override-kind
c7b8388
+Fortran Var(flag_dec_override_kind)
c7b8388
+Enable support for per variable kind specification.
c7b8388
+
91b3540
 fdec-structure
91b3540
 Fortran Var(flag_dec_structure)
91b3540
 Enable support for DEC STRUCTURE/RECORD.
595f09c
diff --git a/gcc/fortran/options.cc b/gcc/fortran/options.cc
c7b8388
index d6bd36c3a8a..edbab483b36 100644
595f09c
--- a/gcc/fortran/options.cc
595f09c
+++ b/gcc/fortran/options.cc
91b3540
@@ -78,6 +78,7 @@ set_dec_flags (int value)
91b3540
   SET_BITFLAG (flag_dec_blank_format_item, value, value);
91b3540
   SET_BITFLAG (flag_dec_char_conversions, value, value);
c7b8388
   SET_BITFLAG (flag_dec_duplicates, value, value);
c7b8388
+  SET_BITFLAG (flag_dec_override_kind, value, value);
c7b8388
 }
c7b8388
 
c7b8388
 /* Finalize DEC flags.  */
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..706ea4112a4
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
c7b8388
@@ -0,0 +1,13 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec" }
c7b8388
+!
c7b8388
+! Test character declaration with mixed string length and array specification
c7b8388
+!
c7b8388
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
c7b8388
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+       PROGRAM character_declaration
c7b8388
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/
c7b8388
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
c7b8388
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1
c7b8388
+        END
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..26d2acf01de
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
c7b8388
@@ -0,0 +1,13 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec-override-kind" }
c7b8388
+!
c7b8388
+! Test character declaration with mixed string length and array specification
c7b8388
+!
c7b8388
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
c7b8388
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        PROGRAM character_declaration
c7b8388
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/
c7b8388
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
c7b8388
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1
c7b8388
+        END
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..76e4f0bdb93
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
c7b8388
@@ -0,0 +1,13 @@
c7b8388
+! { dg-do compile }
c7b8388
+! { dg-options "-fdec-override-kind -fno-dec-override-kind" }
c7b8388
+!
c7b8388
+! Test character declaration with mixed string length and array specification
c7b8388
+!
c7b8388
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
c7b8388
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        PROGRAM character_declaration
c7b8388
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/ ! { dg-error "Syntax error" }
c7b8388
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
c7b8388
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1 ! { dg-error " Operands of comparison operator" }
c7b8388
+        END
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..edd0f5874b7
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
c7b8388
@@ -0,0 +1,31 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer*8  ai*1, bi*4, ci
c7b8388
+          real*4 ar*4, br*8, cr
c7b8388
+
c7b8388
+          ai = 1
c7b8388
+          ar = 1.0
c7b8388
+          bi = 2
c7b8388
+          br = 2.0
c7b8388
+          ci = 3
c7b8388
+          cr = 3.0
c7b8388
+
c7b8388
+          if (ai .ne. 1) stop 1
c7b8388
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
c7b8388
+          if (bi .ne. 2) stop 3
c7b8388
+          if (abs(br - 2.0) > 1.0D-6) stop 4
c7b8388
+          if (ci .ne. 3) stop 5
c7b8388
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
c7b8388
+          if (kind(ai) .ne. 1) stop 7
c7b8388
+          if (kind(ar) .ne. 4) stop 8
c7b8388
+          if (kind(bi) .ne. 4) stop 9
c7b8388
+          if (kind(br) .ne. 8) stop 10
c7b8388
+          if (kind(ci) .ne. 8) stop 11
c7b8388
+          if (kind(cr) .ne. 4) stop 12
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..bfaba584dbb
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
c7b8388
@@ -0,0 +1,31 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec-override-kind" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer*8  ai*1, bi*4, ci
c7b8388
+          real*4 ar*4, br*8, cr
c7b8388
+
c7b8388
+          ai = 1
c7b8388
+          ar = 1.0
c7b8388
+          bi = 2
c7b8388
+          br = 2.0
c7b8388
+          ci = 3
c7b8388
+          cr = 3.0
c7b8388
+
c7b8388
+          if (ai .ne. 1) stop 1
c7b8388
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
c7b8388
+          if (bi .ne. 2) stop 3
c7b8388
+          if (abs(br - 2.0) > 1.0D-6) stop 4
c7b8388
+          if (ci .ne. 3) stop 5
c7b8388
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
c7b8388
+          if (kind(ai) .ne. 1) stop 7
c7b8388
+          if (kind(ar) .ne. 4) stop 8
c7b8388
+          if (kind(bi) .ne. 4) stop 9
c7b8388
+          if (kind(br) .ne. 8) stop 10
c7b8388
+          if (kind(ci) .ne. 8) stop 11
c7b8388
+          if (kind(cr) .ne. 4) stop 12
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..5ff434e7466
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
c7b8388
@@ -0,0 +1,31 @@
c7b8388
+! { dg-do compile }
c7b8388
+! { dg-options "-fdec -fno-dec-override-kind" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer*8  ai*1, bi*4, ci ! { dg-error "Syntax error" }
c7b8388
+          real*4 ar*4, br*8, cr ! { dg-error "Syntax error" }
c7b8388
+
c7b8388
+          ai = 1
c7b8388
+          ar = 1.0
c7b8388
+          bi = 2
c7b8388
+          br = 2.0
c7b8388
+          ci = 3
c7b8388
+          cr = 3.0
c7b8388
+
c7b8388
+          if (ai .ne. 1) stop 1
c7b8388
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
c7b8388
+          if (bi .ne. 2) stop 3
c7b8388
+          if (abs(br - 2.0) > 1.0D-6) stop 4
c7b8388
+          if (ci .ne. 3) stop 5
c7b8388
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
c7b8388
+          if (kind(ai) .ne. 1) stop 7
c7b8388
+          if (kind(ar) .ne. 4) stop 8
c7b8388
+          if (kind(bi) .ne. 4) stop 9
c7b8388
+          if (kind(br) .ne. 8) stop 10
c7b8388
+          if (kind(ci) .ne. 8) stop 11
c7b8388
+          if (kind(cr) .ne. 4) stop 12
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..c01980e8b9d
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
c7b8388
@@ -0,0 +1,14 @@
c7b8388
+! { dg-do compile }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type. The per variable
c7b8388
+! kind specification is not enabled so these should fail
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer a
c7b8388
+          parameter(a=2)
c7b8388
+          integer b*(a) ! { dg-error "Syntax error" }
c7b8388
+          real c*(8)    ! { dg-error "Syntax error" }
c7b8388
+          logical d*1_1 ! { dg-error "Syntax error" }
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..e2f39da3f4f
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
c7b8388
@@ -0,0 +1,19 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec-override-kind" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer a
c7b8388
+          parameter(a=2)
c7b8388
+          integer b*(a)
c7b8388
+          real c*(8)
c7b8388
+          logical d*(1_1)
c7b8388
+          character e*(a)
c7b8388
+          if (kind(b).ne.2) stop 1
c7b8388
+          if (kind(c).ne.8) stop 2
c7b8388
+          if (kind(d).ne.1) stop 3
c7b8388
+          if (len(e).ne.2) stop 4
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..569747874e3
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
c7b8388
@@ -0,0 +1,19 @@
c7b8388
+! { dg-do run }
c7b8388
+! { dg-options "-fdec" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer a
c7b8388
+          parameter(a=2)
c7b8388
+          integer b*(a)
c7b8388
+          real c*(8)
c7b8388
+          logical d*(1_1)
c7b8388
+          character e*(a)
c7b8388
+          if (kind(b).ne.2) stop 1
c7b8388
+          if (kind(c).ne.8) stop 2
c7b8388
+          if (kind(d).ne.1) stop 3
c7b8388
+          if (len(e).ne.2) stop 4
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..b975bfd15c5
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
c7b8388
@@ -0,0 +1,15 @@
c7b8388
+! { dg-do compile }
c7b8388
+! { dg-options "-fdec -fno-dec-override-kind" }
c7b8388
+!
c7b8388
+! Test kind specification in variable not in type as the per variable
c7b8388
+! kind specification is not enables these should fail
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer a
c7b8388
+          parameter(a=2)
c7b8388
+          integer b*(a) ! { dg-error "Syntax error" }
c7b8388
+          real c*(8)    ! { dg-error "Syntax error" }
c7b8388
+          logical d*1_1 ! { dg-error "Syntax error" }
c7b8388
+        end
c7b8388
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
c7b8388
new file mode 100644
c7b8388
index 00000000000..85732e0bd85
c7b8388
--- /dev/null
c7b8388
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
c7b8388
@@ -0,0 +1,14 @@
c7b8388
+! { dg-do compile }
c7b8388
+! { dg-options "-fdec" }
c7b8388
+!
c7b8388
+! Check that invalid kind values are rejected.
c7b8388
+!
c7b8388
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
c7b8388
+!
c7b8388
+        program spec_in_var
c7b8388
+          integer a
c7b8388
+          parameter(a=3)
c7b8388
+          integer b*(a) ! { dg-error "Kind 3 not supported" }
c7b8388
+          real c*(78)   ! { dg-error "Kind 78 not supported" }
c7b8388
+          logical d*(*) ! { dg-error "Invalid character" }
c7b8388
+        end
c7b8388
-- 
c7b8388
2.27.0
c7b8388