| 1 |
! { dg-do run } |
| 2 |
! { dg-additional-sources "cf-out-descriptor-1-c.c dump-descriptors.c" } |
| 3 |
! |
| 4 |
! This program checks that calling a Fortran function with C binding and |
| 5 |
! an intent(out) argument works from both C and Fortran. For this |
| 6 |
! test case the argument is an assumed-shape array. |
| 7 |
|
| 8 |
module mm |
| 9 |
use iso_c_binding |
| 10 |
type, bind (c) :: m |
| 11 |
integer(C_INT) :: i, j |
| 12 |
end type |
| 13 |
|
| 14 |
integer, parameter :: imax=10, jmax=5 |
| 15 |
end module |
| 16 |
|
| 17 |
! frob has regular Fortran binding. It transposes input array argument |
| 18 |
! a into the intent(out) argument b. |
| 19 |
|
| 20 |
subroutine frob (a, b) |
| 21 |
use iso_c_binding |
| 22 |
use mm |
| 23 |
type(m) :: a(:,:) |
| 24 |
type(m), intent(out) :: b(:,:) |
| 25 |
integer :: i, j |
| 26 |
|
| 27 |
if (lbound (a, 1) .ne. lbound (b, 2)) stop 101 |
| 28 |
if (lbound (a, 2) .ne. lbound (b, 1)) stop 102 |
| 29 |
if (ubound (a, 1) .ne. ubound (b, 2)) stop 103 |
| 30 |
if (ubound (a, 2) .ne. ubound (b, 1)) stop 104 |
| 31 |
|
| 32 |
do j = lbound (a, 2), ubound (a, 2) |
| 33 |
do i = lbound (a, 1), ubound (a, 1) |
| 34 |
b(j,i) = a(i,j) |
| 35 |
end do |
| 36 |
end do |
| 37 |
end subroutine |
| 38 |
|
| 39 |
! check also has regular Fortran binding, and two input arguments. |
| 40 |
|
| 41 |
subroutine check (a, b) |
| 42 |
use iso_c_binding |
| 43 |
use mm |
| 44 |
type(m) :: a(:,:), b(:,:) |
| 45 |
integer :: i, j |
| 46 |
|
| 47 |
if (lbound (a, 1) .ne. 1 .or. lbound (b, 2) .ne. 1) stop 101 |
| 48 |
if (lbound (a, 2) .ne. 1 .or. lbound (b, 1) .ne. 1) stop 102 |
| 49 |
if (ubound (a, 1) .ne. ubound (b, 2)) stop 103 |
| 50 |
if (ubound (a, 2) .ne. ubound (b, 1)) stop 104 |
| 51 |
|
| 52 |
do j = 1, ubound (a, 2) |
| 53 |
do i = 1, ubound (a, 1) |
| 54 |
if (b(j,i)%i .ne. a(i,j)%i) stop 105 |
| 55 |
if (b(j,i)%j .ne. a(i,j)%j) stop 106 |
| 56 |
end do |
| 57 |
end do |
| 58 |
end subroutine |
| 59 |
|
| 60 |
! ftest1 has C binding and calls frob. This allows us to test intent(out) |
| 61 |
! arguments passed back from Fortran binding to a Fortran function with C |
| 62 |
! binding. |
| 63 |
|
| 64 |
subroutine ftest1 (a, b) bind (c, name="ftest1") |
| 65 |
use iso_c_binding |
| 66 |
use mm |
| 67 |
type(m) :: a(:,:) |
| 68 |
type(m), intent(out) :: b(:,:) |
| 69 |
|
| 70 |
interface |
| 71 |
subroutine frob (a, b) |
| 72 |
use iso_c_binding |
| 73 |
use mm |
| 74 |
type(m) :: a(:,:) |
| 75 |
type(m), intent(out) :: b(:,:) |
| 76 |
end subroutine |
| 77 |
subroutine check (a, b) |
| 78 |
use iso_c_binding |
| 79 |
use mm |
| 80 |
type(m) :: a(:,:), b(:,:) |
| 81 |
end subroutine |
| 82 |
end interface |
| 83 |
|
| 84 |
call frob (a, b) |
| 85 |
call check (a, b) |
| 86 |
end subroutine |
| 87 |
|
| 88 |
! ftest2 has C binding and calls ftest1. This allows us to test intent(out) |
| 89 |
! arguments passed between two Fortran functions with C binding. |
| 90 |
|
| 91 |
subroutine ftest2 (a, b) bind (c, name="ftest2") |
| 92 |
use iso_c_binding |
| 93 |
use mm |
| 94 |
type(m) :: a(:,:) |
| 95 |
type(m), intent(out) :: b(:,:) |
| 96 |
|
| 97 |
interface |
| 98 |
subroutine ftest1 (a, b) bind (c, name="ftest1") |
| 99 |
use iso_c_binding |
| 100 |
use mm |
| 101 |
type(m) :: a(:,:) |
| 102 |
type(m), intent(out) :: b(:,:) |
| 103 |
end subroutine |
| 104 |
subroutine check (a, b) |
| 105 |
use iso_c_binding |
| 106 |
use mm |
| 107 |
type(m) :: a(:,:), b(:,:) |
| 108 |
end subroutine |
| 109 |
end interface |
| 110 |
|
| 111 |
call ftest1 (a, b) |
| 112 |
call check (a, b) |
| 113 |
end subroutine |
| 114 |
|
| 115 |
! main calls ftest2 directly and also indirectly from a C function ctest. |
| 116 |
! The former allows us to test intent(out) arguments passed back from a |
| 117 |
! Fortran routine with C binding to a regular Fortran routine, and the |
| 118 |
! latter tests passing them back from Fortran to C and C to Fortran. |
| 119 |
|
| 120 |
program testit |
| 121 |
use iso_c_binding |
| 122 |
use mm |
| 123 |
implicit none |
| 124 |
|
| 125 |
interface |
| 126 |
subroutine ftest2 (a, b) bind (c, name="ftest2") |
| 127 |
use iso_c_binding |
| 128 |
use mm |
| 129 |
type(m) :: a(:,:) |
| 130 |
type(m), intent(out) :: b(:,:) |
| 131 |
end subroutine |
| 132 |
subroutine ctest (a, b) bind (c) |
| 133 |
use iso_c_binding |
| 134 |
use mm |
| 135 |
type(m) :: a(:,:) |
| 136 |
type(m), intent(out) :: b(:,:) |
| 137 |
end subroutine |
| 138 |
subroutine check (a, b) |
| 139 |
use iso_c_binding |
| 140 |
use mm |
| 141 |
type(m) :: a(:,:), b(:,:) |
| 142 |
end subroutine |
| 143 |
end interface |
| 144 |
|
| 145 |
type(m) :: aa(imax,jmax), bb(jmax,imax) |
| 146 |
integer :: i, j |
| 147 |
|
| 148 |
! initialize |
| 149 |
do j = 1, jmax |
| 150 |
do i = 1, imax |
| 151 |
aa(i,j)%i = i |
| 152 |
aa(i,j)%j = j |
| 153 |
bb(j,i)%i = -1 |
| 154 |
bb(j,i)%j = -2 |
| 155 |
end do |
| 156 |
end do |
| 157 |
|
| 158 |
call ftest2 (aa, bb) |
| 159 |
call check (aa, bb) |
| 160 |
|
| 161 |
! initialize again |
| 162 |
do j = 1, jmax |
| 163 |
do i = 1, imax |
| 164 |
aa(i,j)%i = i |
| 165 |
aa(i,j)%j = j |
| 166 |
bb(j,i)%i = -1 |
| 167 |
bb(j,i)%j = -2 |
| 168 |
end do |
| 169 |
end do |
| 170 |
|
| 171 |
call ctest (aa, bb) |
| 172 |
call check (aa, bb) |
| 173 |
|
| 174 |
end program |