Coming from C++, I know that non-virtual member functions are tied to their classes through name-mangling. I'm trying to learn modern Fortran and my question is how are Fortran type-bound procedures linked to their types?
For example, consider the derived type
module shape_mod
type shape
integer :: color
logical :: filled
contains
procedure :: isFilled
end type shape
contains
logical function isFilled(this)
class(shape) :: this
isFilled = this%filled
end function isFilled
end module
My question is about the internals of procedure dispatch. Does the memory layout of an instance shp
of type shape
hold a pointer to a function or vtable that needs to be de-referenced when calling shp%isFilled()
, as happens with virtual member functions in C++? Or is the name of the type-bound procedure shp%isFilled()
mangled into something like _Z5shape_isFilled(shp)
, as happens with non-virtual member functions in C++?
I know that Fortran uses name mangling for procedures in modules see wiki for example.
There is no need for (additional) name mangling here at all.
The procedures you write are completely ordinary normal module procedures. They just have a polymorphic dummy argument. You do not have to call them as type-bound, you can use them in a normal way.
What makes them type-bound is the creation of the binding in the type declaration.
that means there will be a virtual table and a pointer to the procedure in the virtual table and so on and it will point the right type-bound procedure for the right actual type.
But the procedures are completely ordinary ones. At least conceptually, I did not study the compiler, I am describing the standard concept.
In your case you can call
and it will work perfectly fine, there is nothing special about the procedure itself.
Note that when you are extending the type
you have to use a different name for the procedure. Hence no name mangling is necessary, you already have a procedure with a different name
Again, you can call it as a type bound procedure:
but you can also call it directly (non-virtually)
and you can even call the parent one directly
The reason why C++ needs name mangling is because it uses the same name fir the virtual function in each type. It does not have these normal procedures that have different names as in Fortran. Because if that, C++ needs name-mangling.
Consider this example:
In gfortran, the result contains these symbols:
their meaning is the following:
the subroutine, you can call them directly this way (non-virtually).
the intrinsic assignments of the two types.
the default constructors of the two types
the virtual table functions, one per type (not per procedure, it is not name-mangling of the procedures).
When you add more procedures, https://godbolt.org/z/3q4KWf you can see how the virtual tables contains pointers to them: