* Re: [PATCH] PR fortran/68053 -- Reduce initialization expression to constant value
2015-11-08 22:36 [PATCH] PR fortran/68053 -- Reduce initialization expression to constant value Steve Kargl
@ 2015-11-08 22:37 ` Steve Kargl
2015-11-09 5:38 ` Paul Richard Thomas
0 siblings, 1 reply; 3+ messages in thread
From: Steve Kargl @ 2015-11-08 22:37 UTC (permalink / raw)
To: fortran, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 716 bytes --]
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
[-- Attachment #2: pr68053.diff --]
[-- Type: text/x-diff, Size: 2287 bytes --]
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
^ permalink raw reply [flat|nested] 3+ messages in thread