Blame SOURCES/0013-Allow-per-variable-kind-specification.patch

840d93
From 72d3915eadd1121d8b2f0be04fafc17e9232be81 Mon Sep 17 00:00:00 2001
840d93
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
840d93
Date: Thu, 5 Nov 2015 18:57:53 +0000
840d93
Subject: [PATCH 13/23] Allow per-variable kind specification.
840d93
840d93
      INTEGER*4 x*2, y*8
840d93
840d93
The per-variable sizes override the kind specified in the type.
840d93
At the moment, you can follow this with an array specification, so
840d93
INTEGER x*2(10) is OK, but not the other way round.
840d93
840d93
This feature is enabled by the `-std=extra-legacy` compiler flag.
840d93
---
840d93
840d93
    0013-Allow-per-variable-kind-specification.patch
840d93
840d93
    Allow per-variable kind specification.
840d93
    
840d93
          INTEGER*4 x*2, y*8
840d93
    
840d93
    The per-variable sizes override the kind specified in the type.
840d93
    At the moment, you can follow this with an array specification, so
840d93
    INTEGER x*2(10) is OK, but not the other way round.
840d93
    
840d93
    This feature is enabled by the `-std=extra-legacy` compiler flag.
840d93
    
840d93
    Test written by: Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
840d93
840d93
diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
840d93
index 3ad9c2c8b40..faa08d9c4bb 100644
840d93
--- a/gcc/fortran/decl.c
840d93
+++ b/gcc/fortran/decl.c
840d93
@@ -1019,6 +1019,24 @@ syntax:
840d93
   return MATCH_ERROR;
840d93
 }
840d93
 
840d93
+/* This matches the nonstandard kind given after a variable name, like:
840d93
+   INTEGER x*2, y*4
840d93
+   The per-variable kind will override any kind given in the type
840d93
+   declaration.
840d93
+*/
840d93
+
840d93
+static match
840d93
+match_per_symbol_kind (int *length)
840d93
+{
840d93
+  match m;
840d93
+
840d93
+  m = gfc_match_char ('*');
840d93
+  if (m != MATCH_YES)
840d93
+    return m;
840d93
+
840d93
+  m = gfc_match_small_literal_int (length, NULL);
840d93
+  return m;
840d93
+}
840d93
 
840d93
 /* Special subroutine for finding a symbol.  Check if the name is found
840d93
    in the current name space.  If not, and we're compiling a function or
840d93
@@ -2193,10 +2211,13 @@ variable_decl (int elem)
840d93
   bool t;
840d93
   gfc_symbol *sym;
840d93
   match cl_match;
840d93
+  match kind_match;
840d93
+  int overridden_kind;
840d93
 
840d93
   initializer = NULL;
840d93
   as = NULL;
840d93
   cp_as = NULL;
840d93
+  kind_match = MATCH_NO;
840d93
 
840d93
   /* When we get here, we've just matched a list of attributes and
840d93
      maybe a type and a double colon.  The next thing we expect to see
840d93
@@ -2213,12 +2234,20 @@ variable_decl (int elem)
840d93
   cl_match = MATCH_NO;
840d93
 
840d93
   /* Check for a character length clause before an array clause */
840d93
-  if ((gfc_option.allow_std & GFC_STD_EXTRA_LEGACY)
840d93
-      && current_ts.type == BT_CHARACTER)
840d93
+  if (gfc_option.allow_std & GFC_STD_EXTRA_LEGACY)
840d93
     {
840d93
-      cl_match = match_character_length_clause (&cl, &cl_deferred, elem);
840d93
-      if (cl_match == MATCH_ERROR)
840d93
-	goto cleanup;
840d93
+      if (current_ts.type == BT_CHARACTER)
840d93
+	{
840d93
+	  cl_match = match_character_length_clause (&cl, &cl_deferred, elem);
840d93
+	  if (cl_match == MATCH_ERROR)
840d93
+	    goto cleanup;
840d93
+	}
840d93
+      else
840d93
+	{
840d93
+	  kind_match = match_per_symbol_kind (&overridden_kind);
840d93
+	  if (kind_match == MATCH_ERROR)
840d93
+	    goto cleanup;
840d93
+	}
840d93
     }
840d93
 
840d93
   /* Now we could see the optional array spec. or character length.  */
840d93
@@ -2412,6 +2441,13 @@ variable_decl (int elem)
840d93
       goto cleanup;
840d93
     }
840d93
 
840d93
+  if (kind_match == MATCH_YES)
840d93
+    {
840d93
+      gfc_find_symbol (name, gfc_current_ns, 1, &sym);
840d93
+      /* sym *must* be found at this point */
840d93
+      sym->ts.kind = overridden_kind;
840d93
+    }
840d93
+
840d93
   if (!check_function_name (name))
840d93
     {
840d93
       m = MATCH_ERROR;
840d93
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable.f
840d93
new file mode 100644
840d93
index 00000000000..0341a176aca
840d93
--- /dev/null
840d93
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable.f
840d93
@@ -0,0 +1,12 @@
840d93
+! { dg-do compile }
840d93
+! { dg-options "-std=extra-legacy" }
840d93
+!
840d93
+! Test kind specification in variable not in type
840d93
+!
840d93
+        PROGRAM spec_in_var
840d93
+          INTEGER  ai*1/1/
840d93
+          REAL ar*4/1.0/
840d93
+
840d93
+          if(ai.NE.1) STOP 1
840d93
+          if(abs(ar - 1.0) > 1.0D-6) STOP 2
840d93
+        END