OOP - GCC Wiki (original) (raw)
gfortran: OOP features [Fortran 2003/2008]
OOP here refers to "Object-Oriented Programming", which was introduced to Fortran in the 2003 standard. The purpose of this page is to document which features have been implemented in gfortran so far, what is still missing and which problems can be expected.
Summary:
- gfortran 4.4: type extension, abstract types, non-polymorphic type-bound procedures
- gfortran 4.5: experimental support for polymorphic entities (CLASS variables)
- gfortran 4.6: almost complete implementation of scalar polymorphism (GSoC 2010 project of Janus Weil)
- gfortran 4.7: polymorphic arrays, constructors, bug fixes and minor enhancements
- gfortran 4.8: unlimited polymorphism (CLASS(*)) and various bug fixes
- gfortran 4.9: finalization & polymorphic deallocation
- gfortran 5: bug-fixing
- gfortran 6: bug-fixing
- gfortran 7: derived-type I/O
- gfortran 8: parametrized derived types
Contents
Implemented features
The following features are available in gfortran 4.5:
- 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 (only partially working)
- allocatable scalars with allocatable components (partially working, fully in 4.6)
These additional features are available in 4.6:
- improved handling of TBPs to make polymorphism work between libraries/programs and complicated inheritance patterns
- ALLOCATE with MOLD
- STORAGE_SIZE intrinsic function
- proper handling of generic TBPs
- ASSOCIATE construct (except some special cases)
- (re)allocate on assignment (non-polymorphic)
- allocatable character length scalar variables (deferred-length type parameter)
These additional features are available in 4.7:
- polymorphic arrays
- generic procedures with same name as a type ("constructor")
- lots of bug fixes
- enhanced diagnostics
- performance improvements
These additional features are available in 4.8:
- unlimited polymorphism: CLASS(*)
- allocatable character length arrays (PR45170)
- bug fixes (especially related to allocatable character length scalar variables)
These additional features are available in 4.9:
- proper polymorphic deallocation
- finalization ("destructor"), note: finalization is currently only done for a subset of the situations in which it should occur
- deferred-length character components
These additional features are available in version 7:
- user-defined derived-type I/O (PR48298)
- Intrinsic assignment to polymorphic variables (F08) (PR43366)
These additional features are available in version 8:
- parametrized derived types
Unimplemented features
Internal Representation
The current implementation is such that each class(t) variable consists of a class container (named __class_ X) with two fields (PR53971):
- _data: a pointer to a struct of the declared type t
- _vptr: a pointer to a vtab entity (see below)
For each derived type t we then construct a vtab entity (called __vtab_ X), which is of a type __vtype_ X and contains the following fields:
- _hash: a unique hash identifier for this type
- _size: the storage size of the type t (in bytes)
- _extends: a pointer to the vtab of the parent type (or NULL if the type is not an extension)
- _def_init: a pointer to a default-initialized variable of type t
- _copy: a procedure pointer to a deep-copy function used for ALLOCATE with polymorphic SOURCE=
- _final: a procedure pointer to a wrapper function with assumed-rank argument, which handles finalization, including the deallocation and finalization of the allocatable components (cf. PR46321 and PR37336)
- a number of procedure pointer components for the implementation of polymorphic type-bound procedure calls
In the naming of class containers, vtabs and vtypes, the X is usually being replaced by a combination of module name and type name. However, if the resulting string is too long, we insert a hashed string instead, which is a hex representation of the type's _hash value.
Object-oriented Fortran codes
Here are some examples of codes that make use of object-oriented features from Fortran 2003 (certain not the most comprehensive list, though) ...
- PSBLAS
- Galacticus
- ForTrilinos
- Ses3d-NT
- PERMIX
- pFUnit
- SiGMA
- MESA
- FOODIE
- Generic Fortran Containers
- WHIZARD
- OFF
- Fortran Astrodynamics Toolkit
Literature
- D. Rouson et al., Scientific Software Design: The Object-Oriented Way (2012)
- Arjen Markus, Modern Fortran in Practice (2012)
- S. Arabas et al., Object-oriented implementations of the MPDATA advection equation solver in C++, Python and Fortran (arXiv, 2013)
Open Problems
[Here is a list of all OOP-related bugs.]
PRs related to CLASS arrays (10):
- Issues listed in the patch submission email for polymorphic arrays
- PR46991: polymorphic assumed-size actual arguments
- PR47506: [Fortran 90+] Assumed-size array checks (polymorphic and component)
- PR51610: Class container does not properly handle POINTER and TARGET
- PR55824: ICE with ALLOCATE and SOURCE= TRANSPOSE/RESHAPE
- PR55825: Bogus rank error with CLASS pointer assignment using structure constructors
- PR56691: Allocatable array: wrong offset when passing to CLASS dummy
- PR56765: compilation errors/ICE with polymorphic array
- PR57284: ICE with find_array_spec for polymorphic arrays
- PR58043: Incorrect behaviour of polymorphic array
- PR58331: Bogus rank checking with explicit-/assumed-size arrays and CLASS
Also the implementation of SELECT TYPE still has a few bugs (6):
- PR41599: SELECT TYPE with associate-name => exp: Use (sometimes) a restricted pointer
- PR52265: TREE dump confusing with nested SELECT TYPE
- PR54322: Wrong TARGET-attribute handling with CLASS IS/TYPE IS
- PR55849: Implement temporary support for SELECT TYPE(name => array ( [vector-subscript] ))
- PR57116: ICE for pointer assignment inside SELECT TYPE
- PR58906: SELECT TYPE with CLASS IS generates ICE
Bugs concerning type-bound generics & operators (6):
- PR41951: Not diagnosing ambiguous operators (TB vs. INTERFACE)
- PR46897: type-bound defined ASSIGNMENT(=) not used for derived type component in intrinsic assign
- PR48786: Generic ambiguity check too strict for polymorphic dummies
- PR53694: GENERIC type-bound procs should be available without part-ref syntax
- PR57696: Defined assignment for components not used when those are ALLOCATABLE
- PR58620: Defined assignment not called for TYPE when the type's extension is used
Miscellaneous (compile-time) bugs (8):
- PR47805: Overridding hidden (private) TPB is rejected
- PR49213: gfortran rejects structure constructor expression
- PR49592: Non-polymorphic ALLOCATE with polymorphic SOURCE= rejected
- PR51961: ALLOCATE with MOLD= rejects if source-expr has a different rank
- PR52227: TARGET attribute mishandled in polymorphic types
- PR53951: Later TARGET statement ignored for CLASS
- PR58557: Issues with CLASS/TYPE functions in array constructors: reject valid, memory leaks, invalid free
- PR58857: CLASS wrongly rejected in BLOCK DATA
Accepts-invalid, ICE-on-invalid and diagnostic bugs (5):
- PR37222: Checks when overriding type-bound procedures are incomplete
- PR50252: Error message on "call x%y" (x not declared) can be more informative
- PR51208: ALLOCATE with SOURCE= or MOLD=: Diagnose if variable occurs twice
- PR54880: ICE in gfc_create_module_variable, at fortran/trans-decl.c:4013
- PR59103: Reject deallocate/intent(out) for polymorphic var in PURE procedures (IR F08/0033)
Runtime problems (wrong code, missed optimization) (9):
- PR46783: TRANSFER with polymorphic MOLD=
- PR47505: Intrinsics which should operate on polymorphic objects (BT_CLASS)
- PR49475: [debugging] Add DWARF info for Fortran's OOP features (extension, member functions)
- PR51284: CLASS and VALUE attribute: No copy to temporary done
- PR53800: Wrong copy-in/copy-out when passing CLASS array to assumed-shape TYPE
- PR54035: TBP wrongly binds to a generic name if the specific name is the same
- PR54618: wrong-code with CLASS(...), INTENT(OUT) -- and OPTIONAL or ALLOCATABLE
- PR57590: wrong code with class variables of different shapes
- PR58644: Missing .data ref in passing a CLASS array as actual argument to a TYPE
Extended features of Fortran 2008 (4):
- PR46371: [Coarray] SELECT TYPE: scalar coarray variable is rejected
- PR52994: [F08] internal compiler error: in gfc_trans_assignment_1, at fortran/trans-expr.c:6881
- PR56496: [F08] ICE with TYPE(*) coarray and SELECT TYPE
- PR57710: [F08] _vptr not set for allocatable CLASS component inside BLOCK
Fixed Bugs
Fixed in 4.5.x (36)
Fixed in 4.6.x (103)
Fixed in 4.7.x (58)
Fixed in 4.8.x (49)
Fixed in 4.9.x (46)
Fixed in 5.x (18)
Fixed in 6.x (8)
Fixed in 7.x (27)
Fixed in 8.x (2)