This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: [patch, fortran] PR45516 - [F08] allocatable components of recursive type
- From: Dominique d'Humières <dominiq at lps dot ens dot fr>
- To: Paul Richard Thomas <paul dot richard dot thomas at gmail dot com>
- Cc: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>, Andre Vehreschild <vehre at gmx dot de>
- Date: Mon, 24 Oct 2016 23:18:12 +0200
- Subject: Re: [patch, fortran] PR45516 - [F08] allocatable components of recursive type
- Authentication-results: sourceware.org; auth=none
- References: <CAGkQGi+0T6kQf0F4d9ALUx0bF6LFH5EXuRhazdZi7YR-JA18Jw@mail.gmail.com>
Dear Paul,
The patch works as expected. The following test now compiles but segfault at run time:
program test_move_alloc
type :: linked_list
type(linked_list), allocatable :: link
integer :: value
end type linked_list
type(linked_list) :: test
test % value = 1
write(*, "('Base value = ', I0)") test % value
allocate(test % link)
write(*, "('Allocated first link = ', L1)") allocated(test % link)
test % link % value = 2
write(*, "('Original first link value = ', I0)") test % link % value
allocate(test % link % link)
write(*, "('Allocated second link = ', L1)") allocated(test % link % link)
test % link % link % value = 3
write(*, "('Original second link value = ', I0)") test % link % value
write(*, "('Allocated third link = ', L1)") allocated(test % link % link % link)
call move_alloc(test % link, test % link % link)
write(*, "('New base value = ', I0)") test % value
write(*, "('Allocated new first link = ', L1)") allocated(test % link)
write(*, "('New first link value = ', I0)") test % link % value
write(*, "('Allocated new second link = ', L1)") allocated(test % link % link)
end program test_move_alloc
Base value = 1
Allocated first link = T
Original first link value = 2
Allocated second link = T
Original second link value = 2
Allocated third link = F
New base value = 1
Allocated new first link = F
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Dominique
> Le 24 oct. 2016 à 14:42, Paul Richard Thomas <paul.richard.thomas@gmail.com> a écrit :
>
> Dear All,
>
> Please find attached the patch for allocatable components of recursive
> type. The patch is pretty straightforward in that for the main part
> they are treated exactly as their pointer equivalents. The exception
> to this is the automatic deallocation of allocatable components. I
> tried to use the vtable finalization wrapper to do this but found it
> impossible to prevent the compilation going into an infinite loop on
> trying to build the automatic deallocation code. I therefore added a
> new field to the vtable that points to a component that does nothing
> more than deallocate the component. This function takes a rank 1
> array, which can be done safely for automatic deallocation of an
> allocatable component.
>
> The testcases indicate some of the possibilities for these components,
> which provide the benefit of automatic garbage collection. As the
> comment in the fourth testcase says, array components are fiendishly
> difficult to use and, I suspect, will find very little application.
>
> Bootstraps and regtests on FC21/x86_64 - OK for trunk?
>
> Paul
>
> 2016-10-24 Paul Thomas <pault@gcc.gnu.org>
>
> PR fortran/45516
> * class.c (gfc_find_derived_vtab): Detect recursive allocatable
> derived type components. If present, add '_deallocate' field to
> the vtable and build the '__deallocate' function.
> * decl.c (build_struct): Allow recursive allocatable derived
> type components for -std=f2008 or more.
> (gfc_match_data_decl): Accept these derived types.
> * expr.c (gfc_has_default_initializer): Ditto.
> * resolve.c (resolve_component): Make sure that the vtable is
> built for these derived types.
> * trans-array.c(structure_alloc_comps) : Use the '__deallocate'
> function for the automatic deallocation of these types.
> * trans-expr.c : Generate the deallocate accessor.
> * trans.h : Add its prototype.
> * trans-types.c (gfc_get_derived_type): Treat the recursive
> allocatable components in the same way as the corresponding
> pointer components.
>
> 2016-10-24 Paul Thomas <pault@gcc.gnu.org>
>
> PR fortran/45516
> * gfortran.dg/class_2.f03: Set -std=f2003.
> * gfortran.dg/finalize_21.f90: Modify tree-dump.
> * gfortran.dg/recursive_alloc_comp_1.f08: New test.
> * gfortran.dg/recursive_alloc_comp_2.f08: New test.
> * gfortran.dg/recursive_alloc_comp_3.f08: New test.
> * gfortran.dg/recursive_alloc_comp_4.f08: New test.
> <submit.diff>