| 1 |
! { dg-do run } |
| 2 |
! |
| 3 |
! PR fortran/124652 - passing a procedure pointer to c_funloc |
| 4 |
! |
| 5 |
! Contributed by Damian Rouson |
| 6 |
|
| 7 |
program proc_ptr_demo |
| 8 |
use iso_c_binding |
| 9 |
implicit none |
| 10 |
|
| 11 |
! Define an interface for the type of procedure we are pointing to |
| 12 |
abstract interface |
| 13 |
function compute_interface(x) result(res) bind(c) |
| 14 |
use iso_c_binding |
| 15 |
real(c_float), intent(in), value :: x |
| 16 |
real(c_float) :: res |
| 17 |
end function compute_interface |
| 18 |
end interface |
| 19 |
|
| 20 |
! Procedure pointers |
| 21 |
procedure(compute_interface), pointer :: original_ptr => null() |
| 22 |
procedure(compute_interface), pointer :: restored_ptr => null() |
| 23 |
type(c_funptr) :: c_address |
| 24 |
real(c_float) :: expect |
| 25 |
|
| 26 |
expect = square_it (5.0) |
| 27 |
! Point to our actual function |
| 28 |
original_ptr => square_it |
| 29 |
if (original_ptr (5.0) /= expect) stop 1 |
| 30 |
|
| 31 |
! Convert pointers "inline" |
| 32 |
c_address = c_funloc (square_it) |
| 33 |
call c_f_procpointer(c_address, restored_ptr) |
| 34 |
if (.not. associated (original_ptr, restored_ptr)) stop 2 |
| 35 |
if (restored_ptr (5.0) /= expect) stop 3 |
| 36 |
|
| 37 |
c_address = c_funloc (original_ptr) |
| 38 |
call c_f_procpointer (c_address, restored_ptr) |
| 39 |
if (.not. associated (original_ptr, restored_ptr)) stop 4 |
| 40 |
if (restored_ptr (5.0) /= expect) stop 5 |
| 41 |
|
| 42 |
! Call contained subroutines to perform the C conversion logic |
| 43 |
call round_trip_conversion_proc (square_it, restored_ptr) |
| 44 |
if (.not. associated (original_ptr, restored_ptr)) stop 6 |
| 45 |
if (restored_ptr (5.0) /= expect) stop 7 |
| 46 |
|
| 47 |
call round_trip_conversion_proc (original_ptr, restored_ptr) |
| 48 |
if (.not. associated (original_ptr, restored_ptr)) stop 8 |
| 49 |
if (restored_ptr (5.0) /= expect) stop 9 |
| 50 |
|
| 51 |
! The following used to fail |
| 52 |
call round_trip_conversion_ptr (square_it, restored_ptr) |
| 53 |
if (.not. associated (original_ptr, restored_ptr)) stop 10 |
| 54 |
if (restored_ptr (5.0) /= expect) stop 11 |
| 55 |
|
| 56 |
call round_trip_conversion_ptr (original_ptr, restored_ptr) |
| 57 |
if (.not. associated (original_ptr, restored_ptr)) stop 12 |
| 58 |
if (restored_ptr (5.0) /= expect) stop 13 |
| 59 |
|
| 60 |
contains |
| 61 |
|
| 62 |
subroutine round_trip_conversion_proc (proc_in, fptr_out) |
| 63 |
procedure(compute_interface) :: proc_in |
| 64 |
procedure(compute_interface), pointer, intent(out) :: fptr_out |
| 65 |
type(c_funptr) :: c_address |
| 66 |
! print *, proc_in(1.0) |
| 67 |
c_address = c_funloc (proc_in) |
| 68 |
call c_f_procpointer (c_address, fptr_out) |
| 69 |
end subroutine round_trip_conversion_proc |
| 70 |
|
| 71 |
subroutine round_trip_conversion_ptr (fptr_in, fptr_out) |
| 72 |
procedure(compute_interface), pointer, intent(in) :: fptr_in |
| 73 |
procedure(compute_interface), pointer, intent(out) :: fptr_out |
| 74 |
type(c_funptr) :: c_address_s |
| 75 |
! print *, fptr_in(2.0) |
| 76 |
c_address_s = c_funloc (fptr_in) |
| 77 |
call c_f_procpointer (c_address_s, fptr_out) |
| 78 |
end subroutine round_trip_conversion_ptr |
| 79 |
|
| 80 |
function square_it (x) result(res) bind(c) |
| 81 |
real(c_float), intent(in), value :: x |
| 82 |
real(c_float) :: res |
| 83 |
res = x * x |
| 84 |
end function square_it |
| 85 |
|
| 86 |
end program |