| 1 |
! PR 92621 (?) |
| 2 |
! { dg-do run } |
| 3 |
! { dg-additional-sources "cf-out-descriptor-3-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 scalar. |
| 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 :: imagic=-1, jmagic=42 |
| 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 |
|
| 36 |
if (allocated (a)) stop 101 |
| 37 |
allocate (a) |
| 38 |
a%i = imagic |
| 39 |
a%j = jmagic |
| 40 |
|
| 41 |
if (allocated (aa)) stop 102 |
| 42 |
allocate (aa) |
| 43 |
aa%i = imagic |
| 44 |
aa%j = jmagic |
| 45 |
|
| 46 |
! association status of p is undefined on entry |
| 47 |
allocate (p) |
| 48 |
p%i = imagic |
| 49 |
p%j = jmagic |
| 50 |
end subroutine |
| 51 |
|
| 52 |
subroutine ftest () bind (c, name="ftest") |
| 53 |
use iso_c_binding |
| 54 |
use mm |
| 55 |
type(m), allocatable :: a, aa |
| 56 |
type(m), pointer :: p |
| 57 |
|
| 58 |
interface |
| 59 |
subroutine frob (a, aa, p) bind (c, name="frob") |
| 60 |
use iso_c_binding |
| 61 |
use mm |
| 62 |
type(m), intent(out), allocatable :: a, aa |
| 63 |
type(m), intent(out), pointer :: p |
| 64 |
end subroutine |
| 65 |
end interface |
| 66 |
|
| 67 |
p => NULL () |
| 68 |
allocate (aa) |
| 69 |
aa%i = 0 |
| 70 |
aa%j = 0 |
| 71 |
call frob (a, aa, p) |
| 72 |
|
| 73 |
if (.not. allocated (a)) stop 201 |
| 74 |
if (a%i .ne. imagic) stop 202 |
| 75 |
if (a%j .ne. jmagic) stop 203 |
| 76 |
|
| 77 |
if (.not. allocated (aa)) stop 204 |
| 78 |
if (a%i .ne. imagic) stop 205 |
| 79 |
if (a%j .ne. jmagic) stop 206 |
| 80 |
|
| 81 |
if (.not. associated (p)) stop 207 |
| 82 |
if (p%i .ne. imagic) stop 208 |
| 83 |
if (p%j .ne. jmagic) stop 209 |
| 84 |
|
| 85 |
end subroutine |
| 86 |
|
| 87 |
program testit |
| 88 |
use iso_c_binding |
| 89 |
use mm |
| 90 |
implicit none |
| 91 |
|
| 92 |
interface |
| 93 |
subroutine frob (a, aa, p) bind (c, name="frob") |
| 94 |
use iso_c_binding |
| 95 |
use mm |
| 96 |
type(m), intent(out), allocatable :: a, aa |
| 97 |
type(m), intent(out), pointer :: p |
| 98 |
end subroutine |
| 99 |
subroutine ftest () bind (c, name="ftest") |
| 100 |
use iso_c_binding |
| 101 |
use mm |
| 102 |
end subroutine |
| 103 |
subroutine ctest (ii, jj) bind (c, name="ctest") |
| 104 |
use iso_c_binding |
| 105 |
use mm |
| 106 |
integer(C_INT), value :: ii, jj |
| 107 |
end subroutine |
| 108 |
end interface |
| 109 |
|
| 110 |
type(m), allocatable :: a, aa |
| 111 |
type(m), pointer :: p |
| 112 |
|
| 113 |
p => NULL () |
| 114 |
allocate (aa) |
| 115 |
aa%i = 0 |
| 116 |
aa%j = 0 |
| 117 |
call frob (a, aa, p) |
| 118 |
|
| 119 |
if (.not. allocated (a)) stop 201 |
| 120 |
if (a%i .ne. imagic) stop 202 |
| 121 |
if (a%j .ne. jmagic) stop 203 |
| 122 |
|
| 123 |
if (.not. allocated (aa)) stop 204 |
| 124 |
if (a%i .ne. imagic) stop 205 |
| 125 |
if (a%j .ne. jmagic) stop 206 |
| 126 |
|
| 127 |
if (.not. associated (p)) stop 207 |
| 128 |
if (p%i .ne. imagic) stop 208 |
| 129 |
if (p%j .ne. jmagic) stop 209 |
| 130 |
|
| 131 |
call ftest |
| 132 |
call ctest (imagic, jmagic) |
| 133 |
|
| 134 |
end program |