gfortran: OOP features [Fortran 2003]
The upcoming 4.5 release of gfortran will contain some experimental support for polymorphic CLASS entities and object oriented features. The purpose of this page is to document which features have been implemented so far, what is still missing and which problems can be expected.
Implemented features
The following features are available on gfortran 4.5 trunk:
- basic CLASS declarations
- polymorphic pointer assignments
- polymorphic dummy arguments
- polymorphic allocation
- SELECT TYPE statements
- intrinsic functions SAME_TYPE_AS and EXTENDS_TYPE_OF
- dynamic dispatch, polymorphic type-bound procedures
Unimplemented features
- unlimited polymorphism: CLASS(*)
- Improved handling of TBPs to make polymorphism work between libraries/programs and complicated inheritance patterns
Generic procedures with same name as a type ("constructor", PR39427)
FINALizing subroutines ("destructor")
- ASSOCIATE construct
- allocatable character length
"fortran-dev" branch
The fortran-dev branch tracks the trunk and contains additional fixes.
Fixed for fortran-dev but not in the trunk/in 4.5
PR41829: Runtime error with dynamic dispatching
Open Problems
There are still several problematic areas. While e.g. scalar CLASS variables work mostly fine, there are a couple of issues with CLASS arrays:
PR41539: Calling function which takes CLASS: Rank comparison does not work
PR41600: SELECT TYPE with associate-name => exp: Arrays not supported
PR41951: ICE in gfc_match_varspec, at fortran/primary.c:1815
Furthermore, the implementation of type-bound procedures is not perfect yet. Although they often work, there are many cases where polymorphic TBP calls fail at runtime:
PR41829: Runtime error with dynamic dispatching (fixed in the fortran-dev branch)
PR42385: poylmorphic operators do not work
Apart from these main problems, there are several minor bugs:
PR41580: SAME_TYPE_AS and EXTENDS_TYPE_OF - add compile-time simplifcation
PR41599: SELECT TYPE with associate-name => exp: Use (sometimes) a restricted pointer
PR41753: segfault with -O2 using methods as actual arguments
PR42051: ICE on array-valued function with CLASS formal argument
PR42207: Compile-time errors on typed allocation and pointer function result
PR42353: Bogus Error: Name 'vtype$...' at (1) is an ambiguous reference ...
Internal Representation
The current implementation is such that each class(t) variable consists of a class container (named '.class.t') with the following fields:
$data: a pointer to a struct of the declared type t
$vptr: a pointer to a vtab entity
For each derived type t we then construct a vtab entity (called vtab$t), which is of a type vtype$t and currently contains three fields:
$hash: the unique hash identifier for this type
$size: the storage size of the type t
$extends: a pointer to the vtab of the parent type (or NULL if the type is not an extension)
For a correct handling of polymorphic type-bound procedures, the vtypes will have to be extended to contain procedure pointer components for the methods.
Fixed Bugs (29)
PR41556: Errors in applying operator/assignment to an abstract type
PR41579: Nesting of SELECT TYPE
PR41581: Allocation of a CLASS with SOURCE=<class> does not work
PR41582: Allocation of abstract types requires a type spec or a SOURCE
PR41583: TYPE IS rejected because of way vtable index is implemented
PR41585: Reject CLASS(T) as component of TYPE T
PR41586: Allocatable _scalar_ TYPE/CLASS leak memory
PR41587: ICE with ALLOCATABLE CLASS components
PR41608: ICE with CLASS and invalid code
PR41618: accepts-invalid with CLASS pointer component
PR41629: gimplification error on valid code
PR41631: Support CLASS IS () in SELECT TYPE
PR41648: Type-bound procedures refused
PR41656: Unresolved GENERIC
PR41685: ICE: verify_flow_info failed
PR41706: Calling one TBP as an actual argument of another TBP
PR41714: ALLOCATE SOURCE= does not properly copy the value from SOURCE
PR41719: invalid: Intrinsic assignment involving polymorphic variables
PR41766: SELECT TYPE selector as actual argument with INTENT(INOUT)
PR41781: bogus undefined label error with SELECT TYPE
PR41784: ICE in load_derived_extensions
PR41800: ICE in fold_convert_loc, at fold-const.c:2789
PR41873: Bogus Error: ABSTRACT INTERFACE must not be referenced...
PR41937: Error in allocating derived type allocatable components
PR42053: SELECT TYPE: reject duplicate CLASS IS blocks
PR42144: deferred TBPs do not work
PR42167: SELECT TYPE with function return value
PR42257: Compiler segmentation fault due missing public statement
PR42335: ICE on CLASS IS (bad_identifier)