| 1 |
! { dg-do run } |
| 2 |
! { dg-additional-sources PR100911.c } |
| 3 |
! |
| 4 |
! Test the fix for PR100911 |
| 5 |
! |
| 6 |
|
| 7 |
module isof_m |
| 8 |
|
| 9 |
use, intrinsic :: iso_c_binding, only: & |
| 10 |
c_signed_char, c_int16_t |
| 11 |
|
| 12 |
implicit none |
| 13 |
|
| 14 |
private |
| 15 |
|
| 16 |
public :: & |
| 17 |
CFI_type_cptr |
| 18 |
|
| 19 |
public :: & |
| 20 |
check_tk_as, & |
| 21 |
check_tk_ar |
| 22 |
|
| 23 |
public :: & |
| 24 |
cfi_encode_type |
| 25 |
|
| 26 |
integer, parameter :: CFI_type_t = c_int16_t |
| 27 |
|
| 28 |
integer(kind=c_int16_t), parameter :: CFI_type_mask = int(z"FF", kind=c_int16_t) |
| 29 |
integer(kind=c_int16_t), parameter :: CFI_type_kind_shift = 8_c_int16_t |
| 30 |
|
| 31 |
! Intrinsic types. Their kind number defines their storage size. */ |
| 32 |
integer(kind=c_signed_char), parameter :: CFI_type_cptr = 7 |
| 33 |
|
| 34 |
interface |
| 35 |
subroutine check_tk_as(a, t, k, e, n) & |
| 36 |
bind(c, name="check_tk") |
| 37 |
use, intrinsic :: iso_c_binding, only: & |
| 38 |
c_int16_t, c_signed_char, c_size_t |
| 39 |
implicit none |
| 40 |
type(*), intent(in) :: a(:) |
| 41 |
integer(c_int16_t), value, intent(in) :: t |
| 42 |
integer(c_signed_char), value, intent(in) :: k |
| 43 |
integer(c_size_t), value, intent(in) :: e |
| 44 |
integer(c_size_t), value, intent(in) :: n |
| 45 |
end subroutine check_tk_as |
| 46 |
subroutine check_tk_ar(a, t, k, e, n) & |
| 47 |
bind(c, name="check_tk") |
| 48 |
use, intrinsic :: iso_c_binding, only: & |
| 49 |
c_int16_t, c_signed_char, c_size_t |
| 50 |
implicit none |
| 51 |
type(*), intent(in) :: a(..) |
| 52 |
integer(c_int16_t), value, intent(in) :: t |
| 53 |
integer(c_signed_char), value, intent(in) :: k |
| 54 |
integer(c_size_t), value, intent(in) :: e |
| 55 |
integer(c_size_t), value, intent(in) :: n |
| 56 |
end subroutine check_tk_ar |
| 57 |
end interface |
| 58 |
|
| 59 |
contains |
| 60 |
|
| 61 |
elemental function cfi_encode_type(type, kind) result(itype) |
| 62 |
integer(kind=c_signed_char), intent(in) :: type |
| 63 |
integer(kind=c_signed_char), intent(in) :: kind |
| 64 |
|
| 65 |
integer(kind=c_int16_t) :: itype, ikind |
| 66 |
|
| 67 |
itype = int(type, kind=c_int16_t) |
| 68 |
itype = iand(itype, CFI_type_mask) |
| 69 |
ikind = int(kind, kind=c_int16_t) |
| 70 |
ikind = iand(ikind, CFI_type_mask) |
| 71 |
ikind = shiftl(ikind, CFI_type_kind_shift) |
| 72 |
itype = ior(ikind, itype) |
| 73 |
return |
| 74 |
end function cfi_encode_type |
| 75 |
|
| 76 |
end module isof_m |
| 77 |
|
| 78 |
module iso_check_m |
| 79 |
|
| 80 |
use, intrinsic :: iso_c_binding, only: & |
| 81 |
c_signed_char, c_int16_t, c_size_t |
| 82 |
|
| 83 |
use, intrinsic :: iso_c_binding, only: & |
| 84 |
c_int, c_ptr, c_loc, c_associated |
| 85 |
|
| 86 |
use, intrinsic :: iso_c_binding, only: & |
| 87 |
c_ptr |
| 88 |
|
| 89 |
use :: isof_m, only: & |
| 90 |
CFI_type_cptr |
| 91 |
|
| 92 |
use :: isof_m, only: & |
| 93 |
check_tk_as, & |
| 94 |
check_tk_ar |
| 95 |
|
| 96 |
use :: isof_m, only: & |
| 97 |
cfi_encode_type |
| 98 |
|
| 99 |
implicit none |
| 100 |
|
| 101 |
integer :: i |
| 102 |
integer(kind=c_size_t), parameter :: b = 8 |
| 103 |
integer, parameter :: n = 11 |
| 104 |
|
| 105 |
type, bind(c) :: c_foo_t |
| 106 |
integer(kind=c_int) :: a |
| 107 |
end type c_foo_t |
| 108 |
|
| 109 |
type(c_foo_t), parameter :: ref_c_foo_t(*) = [(c_foo_t(a=i), i=1,n)] |
| 110 |
|
| 111 |
type(c_foo_t), protected, target :: target_c_foo_t(n) |
| 112 |
|
| 113 |
|
| 114 |
contains |
| 115 |
|
| 116 |
subroutine check_c_ptr() |
| 117 |
type(c_ptr) :: p(n) |
| 118 |
integer :: i |
| 119 |
! |
| 120 |
target_c_foo_t = ref_c_foo_t |
| 121 |
p = [(c_loc(target_c_foo_t(i)), i=1,n)] |
| 122 |
call f_check_c_ptr_as(p) |
| 123 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 1 |
| 124 |
do i = 1, n |
| 125 |
if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 2 |
| 126 |
end do |
| 127 |
target_c_foo_t = ref_c_foo_t |
| 128 |
p = [(c_loc(target_c_foo_t(i)), i=1,n)] |
| 129 |
call c_check_c_ptr_as(p) |
| 130 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 3 |
| 131 |
do i = 1, n |
| 132 |
if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 4 |
| 133 |
end do |
| 134 |
target_c_foo_t = ref_c_foo_t |
| 135 |
p = [(c_loc(target_c_foo_t(i)), i=1,n)] |
| 136 |
call f_check_c_ptr_ar(p) |
| 137 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 5 |
| 138 |
do i = 1, n |
| 139 |
if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 6 |
| 140 |
end do |
| 141 |
target_c_foo_t = ref_c_foo_t |
| 142 |
p = [(c_loc(target_c_foo_t(i)), i=1,n)] |
| 143 |
call c_check_c_ptr_ar(p) |
| 144 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 7 |
| 145 |
do i = 1, n |
| 146 |
if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 8 |
| 147 |
end do |
| 148 |
return |
| 149 |
end subroutine check_c_ptr |
| 150 |
|
| 151 |
subroutine f_check_c_ptr_as(a) |
| 152 |
type(c_ptr), intent(in) :: a(:) |
| 153 |
! |
| 154 |
integer(kind=c_int16_t) :: t |
| 155 |
integer(kind=c_signed_char) :: k |
| 156 |
integer(kind=c_size_t) :: e |
| 157 |
! |
| 158 |
k = 0 |
| 159 |
e = storage_size(a)/b |
| 160 |
t = cfi_encode_type(CFI_type_cptr, k) |
| 161 |
! Assumes 64-bit target. |
| 162 |
! if(e/=8) stop 9 |
| 163 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 10 |
| 164 |
do i = 1, n |
| 165 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 11 |
| 166 |
end do |
| 167 |
call check_tk_as(a, t, k, e, 1_c_size_t) |
| 168 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 12 |
| 169 |
do i = 1, n |
| 170 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 13 |
| 171 |
end do |
| 172 |
return |
| 173 |
end subroutine f_check_c_ptr_as |
| 174 |
|
| 175 |
subroutine c_check_c_ptr_as(a) bind(c) |
| 176 |
type(c_ptr), intent(in) :: a(:) |
| 177 |
integer(kind=c_int16_t) :: t |
| 178 |
integer(kind=c_signed_char) :: k |
| 179 |
integer(kind=c_size_t) :: e |
| 180 |
! |
| 181 |
k = 0 |
| 182 |
e = storage_size(a)/b |
| 183 |
t = cfi_encode_type(CFI_type_cptr, k) |
| 184 |
! Assumes 64-bit target. |
| 185 |
! if(e/=8) stop 14 |
| 186 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 15 |
| 187 |
do i = 1, n |
| 188 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 16 |
| 189 |
end do |
| 190 |
call check_tk_as(a, t, k, e, 1_c_size_t) |
| 191 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 17 |
| 192 |
do i = 1, n |
| 193 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 18 |
| 194 |
end do |
| 195 |
return |
| 196 |
end subroutine c_check_c_ptr_as |
| 197 |
|
| 198 |
subroutine f_check_c_ptr_ar(a) |
| 199 |
type(c_ptr), intent(in) :: a(..) |
| 200 |
! |
| 201 |
integer(kind=c_int16_t) :: t |
| 202 |
integer(kind=c_signed_char) :: k |
| 203 |
integer(kind=c_size_t) :: e |
| 204 |
! |
| 205 |
k = 0 |
| 206 |
e = storage_size(a)/b |
| 207 |
t = cfi_encode_type(CFI_type_cptr, k) |
| 208 |
! Assumes 64-bit target. |
| 209 |
! if(e/=8) stop 19 |
| 210 |
select rank(a) |
| 211 |
rank(1) |
| 212 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 20 |
| 213 |
do i = 1, n |
| 214 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 21 |
| 215 |
end do |
| 216 |
rank default |
| 217 |
stop 22 |
| 218 |
end select |
| 219 |
call check_tk_ar(a, t, k, e, 1_c_size_t) |
| 220 |
select rank(a) |
| 221 |
rank(1) |
| 222 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 23 |
| 223 |
do i = 1, n |
| 224 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 24 |
| 225 |
end do |
| 226 |
rank default |
| 227 |
stop 25 |
| 228 |
end select |
| 229 |
return |
| 230 |
end subroutine f_check_c_ptr_ar |
| 231 |
|
| 232 |
subroutine c_check_c_ptr_ar(a) bind(c) |
| 233 |
type(c_ptr), intent(in) :: a(..) |
| 234 |
integer(kind=c_int16_t) :: t |
| 235 |
integer(kind=c_signed_char) :: k |
| 236 |
integer(kind=c_size_t) :: e |
| 237 |
! |
| 238 |
k = 0 |
| 239 |
e = storage_size(a)/b |
| 240 |
t = cfi_encode_type(CFI_type_cptr, k) |
| 241 |
! Assumes 64-bit target. |
| 242 |
! if(e/=8) stop 26 |
| 243 |
select rank(a) |
| 244 |
rank(1) |
| 245 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 27 |
| 246 |
do i = 1, n |
| 247 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 28 |
| 248 |
end do |
| 249 |
rank default |
| 250 |
stop 29 |
| 251 |
end select |
| 252 |
call check_tk_ar(a, t, k, e, 1_c_size_t) |
| 253 |
select rank(a) |
| 254 |
rank(1) |
| 255 |
if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 30 |
| 256 |
do i = 1, n |
| 257 |
if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 31 |
| 258 |
end do |
| 259 |
rank default |
| 260 |
stop 32 |
| 261 |
end select |
| 262 |
return |
| 263 |
end subroutine c_check_c_ptr_ar |
| 264 |
|
| 265 |
end module iso_check_m |
| 266 |
|
| 267 |
program main_p |
| 268 |
|
| 269 |
use :: iso_check_m, only: & |
| 270 |
check_c_ptr |
| 271 |
|
| 272 |
implicit none |
| 273 |
|
| 274 |
call check_c_ptr() |
| 275 |
stop |
| 276 |
|
| 277 |
end program main_p |
| 278 |
|
| 279 |
!! Local Variables: |
| 280 |
!! mode: f90 |
| 281 |
!! End: |
| 282 |
|