| 1 |
! PR 92621 (?) |
| 2 |
! { dg-do run } |
| 3 |
! { dg-additional-sources "cf-out-descriptor-4-c.c dump-descriptors.c" } |
| 4 |
! { dg-additional-options "-g" } |
| 5 |
! |
| 6 |
! This program checks that calling a Fortran function with C binding and |
| 7 |
! an intent(out) argument works from both C and Fortran. For this |
| 8 |
! test case the argument is an allocatable or pointer array. |
| 9 |
|
| 10 |
module mm |
| 11 |
use iso_c_binding |
| 12 |
type, bind (c) :: m |
| 13 |
integer(C_INT) :: i, j |
| 14 |
end type |
| 15 |
|
| 16 |
integer, parameter :: imax=5, jmax=10 |
| 17 |
|
| 18 |
end module |
| 19 |
|
| 20 |
! The call chains being tested here are |
| 21 |
! main -> frob |
| 22 |
! main -> ftest -> frob |
| 23 |
! main -> ctest -> frob |
| 24 |
! where everything other than main has C binding. |
| 25 |
|
| 26 |
! frob allocates and initializes its arguments. |
| 27 |
! There are two allocatable dummies so that we can pass both |
| 28 |
! unallocated (a) and allocated (aa). |
| 29 |
|
| 30 |
subroutine frob (a, aa, p) bind (c, name="frob") |
| 31 |
use iso_c_binding |
| 32 |
use mm |
| 33 |
type(m), intent(out), allocatable :: a(:,:), aa(:,:) |
| 34 |
type(m), intent(out), pointer :: p(:,:) |
| 35 |
integer :: i, j |
| 36 |
|
| 37 |
if (allocated (a)) stop 101 |
| 38 |
allocate (a (imax, jmax)) |
| 39 |
do j = 1, jmax |
| 40 |
do i = 1, imax |
| 41 |
a(i,j)%i = i |
| 42 |
a(i,j)%j = j |
| 43 |
end do |
| 44 |
end do |
| 45 |
|
| 46 |
if (allocated (aa)) stop 102 |
| 47 |
allocate (aa (imax, jmax)) |
| 48 |
do j = 1, jmax |
| 49 |
do i = 1, imax |
| 50 |
aa(i,j)%i = i |
| 51 |
aa(i,j)%j = j |
| 52 |
end do |
| 53 |
end do |
| 54 |
|
| 55 |
allocate (p (jmax, imax)) |
| 56 |
do j = 1, jmax |
| 57 |
do i = 1, imax |
| 58 |
p(j,i)%i = i |
| 59 |
p(j,i)%j = j |
| 60 |
end do |
| 61 |
end do |
| 62 |
end subroutine |
| 63 |
|
| 64 |
subroutine ftest () bind (c, name="ftest") |
| 65 |
use iso_c_binding |
| 66 |
use mm |
| 67 |
type(m), allocatable :: a(:,:), aa(:,:) |
| 68 |
type(m), pointer :: p(:,:) |
| 69 |
|
| 70 |
integer :: i, j |
| 71 |
|
| 72 |
interface |
| 73 |
subroutine frob (a, aa, p) bind (c, name="frob") |
| 74 |
use iso_c_binding |
| 75 |
use mm |
| 76 |
type(m), intent(out), allocatable :: a(:,:), aa(:,:) |
| 77 |
type(m), intent(out), pointer :: p(:,:) |
| 78 |
end subroutine |
| 79 |
end interface |
| 80 |
|
| 81 |
p => NULL () |
| 82 |
if (allocated (a) .or. allocated (aa)) stop 200 |
| 83 |
allocate (aa (jmax, imax)) |
| 84 |
do j = 1, jmax |
| 85 |
do i = 1, imax |
| 86 |
aa(j,i)%i = 0 |
| 87 |
aa(j,i)%j = 0 |
| 88 |
end do |
| 89 |
end do |
| 90 |
call frob (a, aa, p) |
| 91 |
|
| 92 |
if (.not. allocated (a)) stop 201 |
| 93 |
if (lbound (a, 1) .ne. 1) stop 202 |
| 94 |
if (lbound (a, 2) .ne. 1) stop 203 |
| 95 |
if (ubound (a, 1) .ne. imax) stop 204 |
| 96 |
if (ubound (a, 2) .ne. jmax) stop 205 |
| 97 |
do j = 1, jmax |
| 98 |
do i = 1, imax |
| 99 |
if (a(i,j)%i .ne. i) stop 206 |
| 100 |
if (a(i,j)%j .ne. j) stop 207 |
| 101 |
end do |
| 102 |
end do |
| 103 |
|
| 104 |
if (.not. allocated (aa)) stop 211 |
| 105 |
if (lbound (aa, 1) .ne. 1) stop 212 |
| 106 |
if (lbound (aa, 2) .ne. 1) stop 213 |
| 107 |
if (ubound (aa, 1) .ne. imax) stop 214 |
| 108 |
if (ubound (aa, 2) .ne. jmax) stop 215 |
| 109 |
do j = 1, jmax |
| 110 |
do i = 1, imax |
| 111 |
if (aa(i,j)%i .ne. i) stop 216 |
| 112 |
if (aa(i,j)%j .ne. j) stop 217 |
| 113 |
end do |
| 114 |
end do |
| 115 |
|
| 116 |
if (.not. associated (p)) stop 221 |
| 117 |
if (lbound (p, 1) .ne. 1) stop 222 |
| 118 |
if (lbound (p, 2) .ne. 1) stop 223 |
| 119 |
if (ubound (p, 1) .ne. jmax) stop 224 |
| 120 |
if (ubound (p, 2) .ne. imax) stop 225 |
| 121 |
do j = 1, jmax |
| 122 |
do i = 1, imax |
| 123 |
if (p(j,i)%i .ne. i) stop 226 |
| 124 |
if (p(j,i)%j .ne. j) stop 227 |
| 125 |
end do |
| 126 |
end do |
| 127 |
|
| 128 |
end subroutine |
| 129 |
|
| 130 |
program testit |
| 131 |
use iso_c_binding |
| 132 |
use mm |
| 133 |
implicit none |
| 134 |
|
| 135 |
interface |
| 136 |
subroutine frob (a, aa, p) bind (c, name="frob") |
| 137 |
use iso_c_binding |
| 138 |
use mm |
| 139 |
type(m), intent(out), allocatable :: a(:,:), aa(:,:) |
| 140 |
type(m), intent(out), pointer :: p(:,:) |
| 141 |
end subroutine |
| 142 |
subroutine ftest () bind (c, name="ftest") |
| 143 |
use iso_c_binding |
| 144 |
use mm |
| 145 |
end subroutine |
| 146 |
subroutine ctest (ii, jj) bind (c, name="ctest") |
| 147 |
use iso_c_binding |
| 148 |
use mm |
| 149 |
integer(C_INT), value :: ii, jj |
| 150 |
end subroutine |
| 151 |
end interface |
| 152 |
|
| 153 |
type(m), allocatable :: a(:,:), aa(:,:) |
| 154 |
type(m), pointer :: p(:,:) |
| 155 |
integer :: i, j |
| 156 |
|
| 157 |
p => NULL () |
| 158 |
if (allocated (a) .or. allocated (aa)) stop 300 |
| 159 |
allocate (aa (jmax, imax)) |
| 160 |
do j = 1, jmax |
| 161 |
do i = 1, imax |
| 162 |
aa(j,i)%i = 0 |
| 163 |
aa(j,i)%j = 0 |
| 164 |
end do |
| 165 |
end do |
| 166 |
call frob (a, aa, p) |
| 167 |
|
| 168 |
if (.not. allocated (a)) stop 301 |
| 169 |
if (lbound (a, 1) .ne. 1) stop 302 |
| 170 |
if (lbound (a, 2) .ne. 1) stop 303 |
| 171 |
if (ubound (a, 1) .ne. imax) stop 304 |
| 172 |
if (ubound (a, 2) .ne. jmax) stop 305 |
| 173 |
do j = 1, jmax |
| 174 |
do i = 1, imax |
| 175 |
if (a(i,j)%i .ne. i) stop 306 |
| 176 |
if (a(i,j)%j .ne. j) stop 307 |
| 177 |
end do |
| 178 |
end do |
| 179 |
|
| 180 |
if (.not. allocated (aa)) stop 311 |
| 181 |
if (lbound (aa, 1) .ne. 1) stop 312 |
| 182 |
if (lbound (aa, 2) .ne. 1) stop 313 |
| 183 |
if (ubound (aa, 1) .ne. imax) stop 314 |
| 184 |
if (ubound (aa, 2) .ne. jmax) stop 315 |
| 185 |
do j = 1, jmax |
| 186 |
do i = 1, imax |
| 187 |
if (aa(i,j)%i .ne. i) stop 316 |
| 188 |
if (aa(i,j)%j .ne. j) stop 317 |
| 189 |
end do |
| 190 |
end do |
| 191 |
|
| 192 |
if (.not. associated (p)) stop 321 |
| 193 |
if (lbound (p, 1) .ne. 1) stop 322 |
| 194 |
if (lbound (p, 2) .ne. 1) stop 323 |
| 195 |
if (ubound (p, 1) .ne. jmax) stop 324 |
| 196 |
if (ubound (p, 2) .ne. imax) stop 325 |
| 197 |
do j = 1, jmax |
| 198 |
do i = 1, imax |
| 199 |
if (p(j,i)%i .ne. i) stop 326 |
| 200 |
if (p(j,i)%j .ne. j) stop 327 |
| 201 |
end do |
| 202 |
end do |
| 203 |
|
| 204 |
call ftest |
| 205 |
call ctest (imax, jmax) |
| 206 |
|
| 207 |
end program |