Bug 41581 - [OOP] Allocation of a CLASS with SOURCE=<class> does not work
[OOP] Allocation of a CLASS with SOURCE=<class> does not work
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.5.0
: P3 normal
: ---
Assigned To: janus
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2009-10-05 12:52 UTC by Tobias Burnus
Modified: 2009-10-13 16:14 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-10-11 19:07:13


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2009-10-05 12:52:36 UTC
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
Comment 1 Tobias Burnus 2009-10-05 12:53:20 UTC
See also PR 41582.
Comment 2 Tobias Burnus 2009-10-09 20:46:24 UTC
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.
Comment 3 janus 2009-10-11 19:07:13 UTC
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.
Comment 4 janus 2009-10-13 16:12:50 UTC
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

Comment 5 janus 2009-10-13 16:14:47 UTC
Fixed with r152715. Closing.