gfortrans handling of type bound procedures of polymorphic types
Tobias Burnus
burnus@net-b.de
Mon Apr 8 20:10:00 GMT 2013
Hi all,
Tilo Schwarz wrote:
> since I currently "explore" the Fortran OOP features I was wondering,
> how gfortran handles type bound procedures of polymorphic types. So
> the one question is (just out of curiosity): Are the polymorphic
> features of Fortran handled by the same mechanisms which are used for
> C++? So if I define a polymorphic type and attach a procedure which is
> overridden in a derived type - does this look like the equivalent C++
> (virtual function) definition at some point in the compiler (midend?)
Yes and no. The way Fortran (the language) implements it is surely
influenced by how C++ does it, though I believe it was more influenced
by other languages.
In terms of the compiler, the same applies: The general ideas are the
same, but it differs in the details. The main difference to C++ is that
Fortran does not allow multi-inheritance, which makes life much easier.
There is quite some special handling for multi-inheritance in the GCC
middle end.
There are other differences between C++ and Fortran. For instance, C++
allows static polymorphic variables such that the user's constructor
must be run before the start of the program. Fortran only allows
polymorphic allocatables and pointers. On the other hand, Fortran's
support for arrays is quite different and more powerful than C++'s -
which means that the destructor handling (finalizing subroutines) is
different. Another reason for implementation differences is that no-one
of the Fortran developers knows the internals of the C++ front end.
In case of gfortran, a polymorphic variable contains of two things: The
actual data and a link to a virtual table. In this virtual table, one
finds the default initialization, a copy (assignment) procedure, a
wrapper function which handles component deallocation and calls the
finalizing subroutine, and all type-bound procedures. The generic
procedures and user-defined operators can be translated at compile time
to the type-bound procedure.
Note: Finalization subroutines aren't yet supported; a draft patch which
adds basic support for it (and enabled code which is already in GCC 4.9)
is at https://userpage.physik.fu-berlin.de/~tburnus/final/
See also http://gcc.gnu.org/wiki/OOP - including "internal representation".
> Does this already happen in the frontend? I was wondering, because
> with -fdump-tree-... some terms (like "vptr" etc.) sound like the
> corresponding C++ mechanisms.
Yes, it happens exclusively in the front end. Actually, the virtual
tables are generated in the gfortran abstract syntax tree (AST) as
normal derived-types with procedure-pointer components. Thus, they do
not use any special features of the middle end. Martin actually once
checked whether some special handling (e.g. for inlining or
devirtualization) is required - but that's not the case. (That's
different to C++ where the multi-inheritance cause problems, which the
ME takes special care of.) At some point gfortran should set the proper
DWARF debugging symbols for OOP, which are currently lacking and might
(or might not) require some tiny adjustments to the middle end.
Tobias
More information about the Fortran
mailing list