]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: handle initialization of derived type parameter arrays from scalar
authorHarald Anlauf <anlauf@gmx.de>
Sun, 10 Oct 2021 18:11:43 +0000 (20:11 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Sun, 10 Oct 2021 18:11:43 +0000 (20:11 +0200)
gcc/fortran/ChangeLog:

PR fortran/99348
PR fortran/102521
* decl.c (add_init_expr_to_sym): Extend initialization of
parameter arrays from scalars to handle derived types.

gcc/testsuite/ChangeLog:

PR fortran/99348
PR fortran/102521
* gfortran.dg/parameter_array_init_8.f90: New test.

gcc/fortran/decl.c
gcc/testsuite/gfortran.dg/parameter_array_init_8.f90 [new file with mode: 0644]

index b3c65b7175ba68f16b7a89cb689522336dd5d526..d6a22d13451fe784ace3739af9332f811f1b16fc 100644 (file)
@@ -2228,12 +2228,16 @@ add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
          gfc_expr *array;
          int n;
          if (sym->attr.flavor == FL_PARAMETER
-               && init->expr_type == EXPR_CONSTANT
-               && spec_size (sym->as, &size)
-               && mpz_cmp_si (size, 0) > 0)
+             && gfc_is_constant_expr (init)
+             && (init->expr_type == EXPR_CONSTANT
+                 || init->expr_type == EXPR_STRUCTURE)
+             && spec_size (sym->as, &size)
+             && mpz_cmp_si (size, 0) > 0)
            {
              array = gfc_get_array_expr (init->ts.type, init->ts.kind,
                                          &init->where);
+             if (init->ts.type == BT_DERIVED)
+               array->ts.u.derived = init->ts.u.derived;
              for (n = 0; n < (int)mpz_get_si (size); n++)
                gfc_constructor_append_expr (&array->value.constructor,
                                             n == 0
diff --git a/gcc/testsuite/gfortran.dg/parameter_array_init_8.f90 b/gcc/testsuite/gfortran.dg/parameter_array_init_8.f90
new file mode 100644 (file)
index 0000000..2e5f769
--- /dev/null
@@ -0,0 +1,25 @@
+! { dg-do run }
+! PR fortran/99348
+! PR fortran/102521
+! Check simplifications for initialization of DT parameter arrays
+
+program p
+  type t
+     integer :: n
+  end type
+  type(t), parameter :: a(4)   = t(1)
+  type(t), parameter :: d(*)   = a
+  type(t), parameter :: b(2,2) = reshape(d, [2,2])
+  integer, parameter :: nn     = b(2,2)% n
+  type u
+     character(3) :: c
+  end type
+  type(u),      parameter :: x(2,3) = u('ab')
+  type(u),      parameter :: y(*,*) = transpose (x)
+  character(*), parameter :: c      = y(3,2)% c
+  integer,      parameter :: lc     = c% len
+  integer,      parameter :: lyc    = len (y(3,2)% c)
+! integer,      parameter :: lxc    = x(1,1)% c% len    ! fails (pr101735?)
+  if (nn /= 1) stop 1
+  if (lc /= 3 .or. lyc /= 3 .or. c /= "ab ") stop 2
+end