This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] PR fortran/68053 -- Reduce initialization expression to constant value


On Sun, Nov 08, 2015 at 02:35:58PM -0800, Steve Kargl wrote:
> The attached patch has been built and regression tested
> on i386-*-freebsd and x86_64-*-freebsd.  If an array
> index in an initialization expression is an array element
> from an array named constant, the array index needs to be
> reduced.  This patch causes the reduction to occur.
> OK to commit?
> 
> 2015-11-08  Steven g. Kargl  <kargl@gcc.gnu.org>
> 
> 	PR fortran/68053
> 	* decl.c (add_init_expr_to_sym):  Try to reduce initialization expression
> 	before testing for a constant value.
> 
> 2015-11-08  Steven g. Kargl  <kargl@gcc.gnu.org>
> 
> 	PR fortran/68053
> 	* gfortran.dg/pr68053.f90: New test.

Now with the patch attached!

-- 
Steve
Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 229970)
+++ gcc/fortran/decl.c	(working copy)
@@ -1529,26 +1529,34 @@ add_init_expr_to_sym (const char *name, 
 	  for (dim = 0; dim < sym->as->rank; ++dim)
 	    {
 	      int k;
-	      gfc_expr* lower;
-	      gfc_expr* e;
+	      gfc_expr *e, *lower;
 
 	      lower = sym->as->lower[dim];
-	      if (lower->expr_type != EXPR_CONSTANT)
+
+	      /* If the lower bound is an array element from another 
+		 parameterized array, then it is marked with EXPR_VARIABLE and
+		 is an initialization expression.  Try to reduce it.  */
+	      if (lower->expr_type == EXPR_VARIABLE)
+		gfc_reduce_init_expr (lower);
+
+	      if (lower->expr_type == EXPR_CONSTANT)
+		{
+		  /* All dimensions must be without upper bound.  */
+		  gcc_assert (!sym->as->upper[dim]);
+
+		  k = lower->ts.kind;
+		  e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
+		  mpz_add (e->value.integer, lower->value.integer,
+			   init->shape[dim]);
+		  mpz_sub_ui (e->value.integer, e->value.integer, 1);
+		  sym->as->upper[dim] = e;
+		}
+	      else
 		{
 		  gfc_error ("Non-constant lower bound in implied-shape"
 			     " declaration at %L", &lower->where);
 		  return false;
 		}
-
-	      /* All dimensions must be without upper bound.  */
-	      gcc_assert (!sym->as->upper[dim]);
-
-	      k = lower->ts.kind;
-	      e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
-	      mpz_add (e->value.integer,
-		       lower->value.integer, init->shape[dim]);
-	      mpz_sub_ui (e->value.integer, e->value.integer, 1);
-	      sym->as->upper[dim] = e;
 	    }
 
 	  sym->as->type = AS_EXPLICIT;
Index: gcc/testsuite/gfortran.dg/pr68053.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr68053.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/pr68053.f90	(working copy)
@@ -0,0 +1,10 @@
+! { dg-do run }
+! PR fortran/68053
+! Original code contributed by Gerhard Steinmetz
+! <gerhard dot steinmetx dot fortran at t-online dot de>
+program p
+   integer, parameter :: n(3) = [1,2,3]
+   integer, parameter :: x(1) = 7
+   integer, parameter :: z(n(2):*) = x
+   if (lbound(z,1) /= 2) call abort
+end

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]