Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How does SELECT TYPE work?

Jugoslav_Dujic
Valued Contributor II
552 Views
I'm playing a bit with F2003 features (by the way, when will they be fully documented?) and I came to the following example:
[fortran]    module m_ext

type:: base
integer:: b = Z'BBBBBBBB'
end type base

type, extends(base):: ext
integer:: e= Z'EEEEEEEE'
end type ext

end module m_ext
!=====================================
program PolyDummy
use m_ext
implicit none

! Variables
type(ext):: ex
! Body of PolyDummy
call PolyBase(ex%base)
call PolyBase(ex)
end program
!=====================================
subroutine PolyBase(bs)
use m_ext
class(base):: bs
select type(bs)
class is (base)
print*, "Base"
class is (ext)
print*, "Ext"
end select
end subroutine PolyBase
[/fortran]
And surely, it outputs
[bash]Base
Ext[/bash]

But I'm wondering just how does this work, i.e. how is the information about type passed from the program to the subroutine? I don't see any hidden arguments, I don't see any "guard bytes" around type definition. I'm not particularly good with assembly to reverse-engineer the machine code of PolyBase (or at least, it's easier to ask).

Note that there is no explicit interface for PolyBase. Is it required? I suppose yes, but I purposefully omitted it to see if it would work without it. And Lord, it does.

I'm interested because I plan to use (or abuse :) ) polymorphic (CLASS) arguments in INTERFACEs to C functions. Those C functions need not deduce the passed type, but they shouldn't crash because Fortran passes some extra information in an unexpected place.

0 Kudos
1 Reply
Steven_L_Intel1
Employee
552 Views

This program should not have worked. It only does if you have generated interface checking enabled and that is a bug. I will report it. Issue ID is DPD200151279.

An explicit interface is required when a dummy argument is polymorphic. The compiler passes a descriptior with type information that is then examined by SELECT TYPE.

I will mention that in the next major release (call it 12.0), we're going to require that you recompile sources that use CLASS as we have to significantly change the descriptor layout in order to accomodate finalizers and more.

The F2003 features will be documented in 12.0.

0 Kudos
Reply