(j3.2006) (SC22WG5.4521) [ukfortran] Callback functions with polymorphicarguments
Malcolm Cohen
malcolm
Wed Aug 31 21:02:09 EDT 2011
No, the code is not conforming.
The call to FOO passes a
subroutine callback1(data)
class(bar) :: data(:) ! (C)
actual argument, but FOO expects
subroutine callback(data)
class(*), dimension(:) :: data ! (A)
Note that the characteristics of the actual argument differ from those of the
dummy argument: the actual has an argument of CLASS(BAR) whereas the dummy has
an argument of CLASS(*).
This is a violation of 12.5.2.9 paragraph 1 sentence 1.
This is not a constraint and not required to be diagnosed. Since the program is
not Fortran, it is not very surprising that compilers differ. (NAG only detects
the error at runtime, not compile time, which is a shame - I'll have to fix
that!)
Changing the actual argument to
subroutine callback1(data)
class(*) :: data(:) ! (C)
integer :: i
select type(data)
class is (bar)
do i=1,size(data)
data(i)%i = i
end do
class default
stop 'WRONG CALLBACK'
end select
end subroutine
fixes the problem.
-----Original Message-----
From: Bader, Reinhold
Date: ?? 23?9?1? 4:41
To: j3 at j3-fortran.org ; WG5
Cc: Rolf Rabenseifner (rabenseifner at hlrs.de)
Subject: [ukfortran] (SC22WG5.4520) Callback functions with polymorphicarguments
Hello all,
consider the following module:
module mod_callback_up_01
implicit none
abstract interface
subroutine callback(data)
class(*), dimension(:) :: data ! (A)
end subroutine
end interface
contains
subroutine foo(data, proc)
class(*), dimension(:) :: data ! (B)
procedure(callback) :: proc
call proc(data)
end subroutine
end module
and then the following code which uses this module:
module mod_impl
implicit none
type :: bar
integer :: i
end type
contains
subroutine callback1(data)
class(bar) :: data(:) ! (C)
integer :: i
do i=1,size(data)
data(i)%i = i
end do
end subroutine
end module
program prog
use mod_callback_up_01
use mod_impl
integer, parameter :: dim=5
type(bar) :: x(dim)
call foo(x, callback1)
end program
Questions:
(1) Is the above code standard conforming?
(2) Assume that at (C), the declaration is replaced by
type(bar) :: data(:)
Is the resulting code standard conforming?
(3) Assume that at (A) and (B), class(*) is replaced by
class(base), where base is some known type, and
bar is an extension of base.
Is the resulting code standard conforming?
(4) Apply both assumptions in (2) and (3) - is the
resulting code then standard conforming?
The background for this is the issue of handling callbacks in MPI-3.
For backward compatibility it is considered desirable to have
abstract interfaces for callbacks which use arguments declared
as TYPE(*), DIMENSION(*). The dummy arguments of the callbacks
of course will have some definite type in existing code. (The
programmer may still need to add BIND(C), but this is considered
sufficiently low-intrusive). Depending on the answers to above questions,
something may need to be done about this in the TR.
(For the above code, different compilers give widely varying results
with respect to both compilation and run time behaviour).
Regards
Reinhold
_______________________________________________
ukfortran mailing list
http://lists.accu.org/mailman/listinfo/ukfortran
--
................................Malcolm Cohen, Nihon NAG, Tokyo.
More information about the J3
mailing list