| 1 |
! { dg-do run } |
| 2 |
! { dg-additional-sources "cf-out-descriptor-2-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-rank 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 |
! The call chains we'll be testing will be |
| 18 |
! main -> ctest -> ftest1 |
| 19 |
! main -> ftest2 -> ftest1 |
| 20 |
! main -> ftest1 |
| 21 |
! where everything has "c" binding except main. |
| 22 |
|
| 23 |
! ftest1 has C binding and transposes a into b. |
| 24 |
|
| 25 |
subroutine ftest1 (a, b) bind (c, name="ftest1") |
| 26 |
use iso_c_binding |
| 27 |
use mm |
| 28 |
type(m) :: a(..) |
| 29 |
type(m), intent(out) :: b(..) |
| 30 |
|
| 31 |
select rank (a) |
| 32 |
rank (2) |
| 33 |
select rank (b) |
| 34 |
rank (2) |
| 35 |
b = transpose (a) |
| 36 |
rank default |
| 37 |
stop 101 |
| 38 |
end select |
| 39 |
rank default |
| 40 |
stop 102 |
| 41 |
end select |
| 42 |
end subroutine |
| 43 |
|
| 44 |
! ftest2 has C binding and calls ftest1. |
| 45 |
|
| 46 |
subroutine ftest2 (a, b) bind (c, name="ftest2") |
| 47 |
use iso_c_binding |
| 48 |
use mm |
| 49 |
type(m) :: a(..) |
| 50 |
type(m), intent(out) :: b(..) |
| 51 |
|
| 52 |
interface |
| 53 |
subroutine ftest1 (a, b) bind (c, name="ftest1") |
| 54 |
use iso_c_binding |
| 55 |
use mm |
| 56 |
type(m) :: a(..) |
| 57 |
type(m), intent(out) :: b(..) |
| 58 |
end subroutine |
| 59 |
end interface |
| 60 |
|
| 61 |
call ftest1 (a, b) |
| 62 |
if (rank (a) .ne. 2) stop 201 |
| 63 |
if (rank (b) .ne. 2) stop 202 |
| 64 |
end subroutine |
| 65 |
|
| 66 |
! main calls ftest2 directly and also indirectly from a C function ctest. |
| 67 |
! The former allows us to test intent(out) arguments passed back from a |
| 68 |
! Fortran routine with C binding to a regular Fortran routine, and the |
| 69 |
! latter tests passing them back from Fortran to C and C to Fortran. |
| 70 |
|
| 71 |
program testit |
| 72 |
use iso_c_binding |
| 73 |
use mm |
| 74 |
implicit none |
| 75 |
|
| 76 |
interface |
| 77 |
subroutine ftest1 (a, b) bind (c, name="ftest2") |
| 78 |
use iso_c_binding |
| 79 |
use mm |
| 80 |
type(m) :: a(..) |
| 81 |
type(m), intent(out) :: b(..) |
| 82 |
end subroutine |
| 83 |
subroutine ftest2 (a, b) bind (c, name="ftest2") |
| 84 |
use iso_c_binding |
| 85 |
use mm |
| 86 |
type(m) :: a(..) |
| 87 |
type(m), intent(out) :: b(..) |
| 88 |
end subroutine |
| 89 |
subroutine ctest (a, b) bind (c, name="ctest") |
| 90 |
use iso_c_binding |
| 91 |
use mm |
| 92 |
type(m) :: a(..) |
| 93 |
type(m), intent(out) :: b(..) |
| 94 |
end subroutine |
| 95 |
end interface |
| 96 |
|
| 97 |
type(m) :: aa(imax,jmax), bb(jmax,imax) |
| 98 |
integer :: i, j |
| 99 |
|
| 100 |
! initialize |
| 101 |
do j = 1, jmax |
| 102 |
do i = 1, imax |
| 103 |
aa(i,j)%i = i |
| 104 |
aa(i,j)%j = j |
| 105 |
bb(j,i)%i = -1 |
| 106 |
bb(j,i)%j = -2 |
| 107 |
end do |
| 108 |
end do |
| 109 |
|
| 110 |
! frob and check |
| 111 |
call ftest1 (aa, bb) |
| 112 |
do j = 1, jmax |
| 113 |
do i = 1, imax |
| 114 |
if (aa(i,j)%i .ne. bb(j,i)%i) stop 301 |
| 115 |
if (aa(i,j)%j .ne. bb(j,i)%j) stop 302 |
| 116 |
end do |
| 117 |
end do |
| 118 |
|
| 119 |
! initialize again |
| 120 |
do j = 1, jmax |
| 121 |
do i = 1, imax |
| 122 |
aa(i,j)%i = i |
| 123 |
aa(i,j)%j = j |
| 124 |
bb(j,i)%i = -1 |
| 125 |
bb(j,i)%j = -2 |
| 126 |
end do |
| 127 |
end do |
| 128 |
|
| 129 |
! frob and check |
| 130 |
call ftest2 (aa, bb) |
| 131 |
do j = 1, jmax |
| 132 |
do i = 1, imax |
| 133 |
if (aa(i,j)%i .ne. bb(j,i)%i) stop 401 |
| 134 |
if (aa(i,j)%j .ne. bb(j,i)%j) stop 402 |
| 135 |
end do |
| 136 |
end do |
| 137 |
|
| 138 |
! initialize again |
| 139 |
do j = 1, jmax |
| 140 |
do i = 1, imax |
| 141 |
aa(i,j)%i = i |
| 142 |
aa(i,j)%j = j |
| 143 |
bb(j,i)%i = -1 |
| 144 |
bb(j,i)%j = -2 |
| 145 |
end do |
| 146 |
end do |
| 147 |
|
| 148 |
! frob and check |
| 149 |
call ctest (aa, bb) |
| 150 |
do j = 1, jmax |
| 151 |
do i = 1, imax |
| 152 |
if (aa(i,j)%i .ne. bb(j,i)%i) stop 501 |
| 153 |
if (aa(i,j)%j .ne. bb(j,i)%j) stop 502 |
| 154 |
end do |
| 155 |
end do |
| 156 |
|
| 157 |
end program |