Currently, the size needed for allocation is not taken into account with CLASS. The example below should print 42 -4.0 but prints 512 0.0000000 Expected: a) The warning "Warning: Dynamic size allocation at (1) not supported yet, using size of declared type" is turned into an error as interim measure (cf. ifort: "Feature not yet implemented: SOURCE=polymorphic_expression") b) The dynamic-size implementation is implemented. Example: type t end type t type,extends(t) :: t2 integer :: i = 54 real :: r = 384.02 end type t2 class(t), allocatable :: m1, m2 allocate(t2 :: m2) select type(m2) type is (t2) m2%i = 42 m2%r = -4.0 end select allocate(m1, source=m2) select type(m1) type is (t2) print *, m1%i, m1%r ! shall print "42 -4.0" end select end
See also PR 41582.
To prevent wrong-code, SOURCE=<class> is now rejected (cf. PR 41582). For the run-time version: One might also need to check whether SOURCE is allocated - at least with some checking option.
Mine. The plan: Add a '$size' field to the class container, which will be used at runtime to determine the size of the memory block to be allocated.
Subject: Bug 41581 Author: janus Date: Tue Oct 13 16:12:24 2009 New Revision: 152715 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=152715 Log: 2009-10-13 Janus Weil <janus@gcc.gnu.org> PR fortran/41581 * decl.c (encapsulate_class_symbol): Add new component '$size'. * resolve.c (resolve_allocate_expr): Move CLASS handling to gfc_trans_allocate. (resolve_class_assign): Replaced by gfc_trans_class_assign. (resolve_code): Remove calls to resolve_class_assign. * trans.c (gfc_trans_code): Use new function gfc_trans_class_assign. * trans-expr.c (get_proc_ptr_comp): Fix a memory leak. (gfc_conv_procedure_call): For CLASS dummies, set the $size component. (gfc_trans_class_assign): New function, replacing resolve_class_assign. * trans-stmt.h (gfc_trans_class_assign): New prototype. * trans-stmt.c (gfc_trans_allocate): Use correct size when allocating CLASS variables. Do proper initialization. Move some code here from resolve_allocate_expr. 2009-10-13 Janus Weil <janus@gcc.gnu.org> PR fortran/41581 * gfortran.dg/class_allocate_2.f03: Modified. * gfortran.dg/class_allocate_3.f03: New test case. Added: trunk/gcc/testsuite/gfortran.dg/class_allocate_3.f03 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/decl.c trunk/gcc/fortran/resolve.c trunk/gcc/fortran/trans-expr.c trunk/gcc/fortran/trans-stmt.c trunk/gcc/fortran/trans-stmt.h trunk/gcc/fortran/trans.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gfortran.dg/class_allocate_2.f03
Fixed with r152715. Closing.