[F03] EXTENDS_TYPE_OF, CLASS IS and libgfortran
Janus Weil
janus@gcc.gnu.org
Tue Nov 3 11:28:00 GMT 2009
Hi all,
since we have the newly-structured OOP implementation in the
fortran-dev branch now (cf. http://gcc.gnu.org/wiki/OOP), I'm starting
to think about EXTENDS_TYPE_OF (A,B) and CLASS IS.
Regarding EXTENDS_TYPE_OF: This is an F03 intrinsic function, which
takes two arguments A and B, both of a derived type or class, and
tells you whether the (dynamic) type of the first argument is an
extension of the (dynamic) type of the second.
My idea to implement this would be to add a new function to
libgfortran, let's call it IS_EXTENSION_OF, which would take two vtabs
as arguments, and would basically do the run-time work for
EXTENDS_TYPE_OF. Depending on the type of arguments which
EXTENDS_TYPE_OF gets (type/class), one could pass the corresponding
vtabs to IS_EXTENSION_OF. In addition one could also use
IS_EXTENSION_OF in the implementation of CLASS IS, calling it once for
each CLASS IS case in a SELECT TYPE block.
However, since my experience with libgfortran is practically zero, I
will need some help with this. What I would do is probably to add a
new file 'extends_type_of.c' to libgfortran/intrinsics/, whose content
could look something like this:
typedef struct vtype
{
int hash;
int size;
vtype *extends;
}
vtype;
bool
is_extension_of (struct vtype *v1, struct vtype *v2)
{
while (v1)
{
if (v1->hash == v2->hash) return true;
v1 = v1->extends;
}
return false;
}
This code is only schematic, I haven't checked if it compiles, but it
should give you a rough idea of how the function should work.
Now, first technical question: How do I correctly tell the libgfortran
Makefile to pick up this file, and add the function to the library?
In the Fortran front end, I have already placed some hooks for
EXTENDS_TYPE_OF, and would just need to replace the call to
EXTENDS_TYPE_OF by a call to IS_EXTENSION_OF with the right arguments:
If I have a CLASS variable c, I just use c->$vptr. If it is a TYPE
variable, I can get its vtab at compile time.
Second question: What do you think of my plan in general? Does this
sound reasonable? Does it have any drawbacks? Is there a better way to
do it? Any kind of comment or suggestion is welcome ...
Cheers,
Janus
More information about the Fortran
mailing list