From 6df364d720c66a9153b3d1d9405124d149cddfa5 Mon Sep 17 00:00:00 2001 From: Erik Edelmann Date: Sat, 19 Aug 2006 21:05:59 +0000 Subject: [PATCH] re PR fortran/25217 (Derived type dummy argument having intent(out) attribute) fortran/ 2006-08-19 Erik Edelmann PR fortran/25217 * resolve.c (resolve_fl_variable): Set a default initializer for derived types with INTENT(OUT) even if 'flag' is true. * trans-expr.c (gfc_conv_function_call): Insert code to reinitialize INTENT(OUT) arguments of derived type with default initializers. testsuite/ 2006-08-19 Erik Edelmann PR fortran/25217 * gfortran.dg/derived_init_2.f90: New. From-SVN: r116261 --- gcc/fortran/ChangeLog | 9 +++++ gcc/fortran/resolve.c | 4 +-- gcc/fortran/trans-expr.c | 10 ++++++ gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gfortran.dg/derived_init_2.f90 | 38 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/derived_init_2.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index aacdc11889d..c922b8d94d8 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,12 @@ +2006-08-19 Erik Edelmann + + PR fortran/25217 + * resolve.c (resolve_fl_variable): Set a default initializer for + derived types with INTENT(OUT) even if 'flag' is true. + * trans-expr.c (gfc_conv_function_call): Insert code to + reinitialize INTENT(OUT) arguments of derived type with default + initializers. + 2006-08-15 Jerry DeLisle PR fortran/25828 diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index cb45a2b2a22..5c9786b7410 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -5232,8 +5232,8 @@ resolve_fl_variable (gfc_symbol *sym, int mp_flag) } /* Assign default initializer. */ - if (sym->ts.type == BT_DERIVED && !(sym->value || flag) - && !sym->attr.pointer) + if (sym->ts.type == BT_DERIVED && !sym->value && !sym->attr.pointer + && !sym->attr.allocatable && (!flag || sym->attr.intent == INTENT_OUT)) sym->value = gfc_default_initializer (&sym->ts); return SUCCESS; diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index d536dcde626..4225b6930c3 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -2014,6 +2014,16 @@ gfc_conv_function_call (gfc_se * se, gfc_symbol * sym, gfc_add_block_to_block (&se->pre, &parmse.pre); gfc_add_block_to_block (&post, &parmse.post); + /* If an INTENT(OUT) dummy of derived type has a default + initializer, it must be (re)initialized here. */ + if (fsym && fsym->attr.intent == INTENT_OUT && fsym->ts.type == BT_DERIVED + && fsym->value) + { + gcc_assert (!fsym->attr.allocatable); + tmp = gfc_trans_assignment (e, fsym->value); + gfc_add_expr_to_block (&se->pre, tmp); + } + /* Character strings are passed as two parameters, a length and a pointer. */ if (parmse.string_length != NULL_TREE) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index cd8b9ab2819..6f8ae4dd29b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2006-08-19 Erik Edelmann + + PR fortran/25217 + * gfortran.dg/derived_init_2.f90: New. + 2006-08-17 J"orn Rennecke * gcc.c-torture/execute/pr28289.c: New test. diff --git a/gcc/testsuite/gfortran.dg/derived_init_2.f90 b/gcc/testsuite/gfortran.dg/derived_init_2.f90 new file mode 100644 index 00000000000..381f13afbbc --- /dev/null +++ b/gcc/testsuite/gfortran.dg/derived_init_2.f90 @@ -0,0 +1,38 @@ +! { dg-do run } +! PR 25217: INTENT(OUT) dummies of derived type with default initializers shall +! be (re)initialized upon procedure entry, unless they are ALLOCATABLE. +program main + + implicit none + + type :: drv + integer :: a(3) = [ 1, 2, 3 ] + character(3) :: s = "abc" + real, pointer :: p => null() + end type drv + type(drv) :: aa + type(drv), allocatable :: ab(:) + real, target :: x + + aa%a = [ 4, 5, 6] + aa%s = "def" + aa%p => x + call sub(aa) + + call sub2(ab) + +contains + + subroutine sub(fa) + type(drv), intent(out) :: fa + + if (any(fa%a /= [ 1, 2, 3 ])) call abort() + if (fa%s /= "abc") call abort() + if (associated(fa%p)) call abort() + end subroutine sub + + subroutine sub2(fa) + type(drv), allocatable, intent(out) :: fa(:) + end subroutine sub2 + +end program main