summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/PR100911.f90
blob: 69f485b59de2e1f664284f32d2bec8d8bfcdbc3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
! { dg-do run }
! { dg-additional-sources PR100911.c }
!
! Test the fix for PR100911
! 

module isof_m

  use, intrinsic :: iso_c_binding, only: &
    c_signed_char, c_int16_t
  
  implicit none

  private
  
  public :: &
    CFI_type_cptr
 
  public ::      &
    check_tk_as, &
    check_tk_ar
  
  public ::          &
    cfi_encode_type
  
  integer, parameter :: CFI_type_t = c_int16_t
  
  integer(kind=c_int16_t), parameter :: CFI_type_mask = int(z"FF", kind=c_int16_t)
  integer(kind=c_int16_t), parameter :: CFI_type_kind_shift = 8_c_int16_t

  ! Intrinsic types. Their kind number defines their storage size. */
  integer(kind=c_signed_char), parameter :: CFI_type_cptr   = 7

  interface
    subroutine check_tk_as(a, t, k, e, n) &
      bind(c, name="check_tk")
      use, intrinsic :: iso_c_binding, only: &
        c_int16_t, c_signed_char, c_size_t
      implicit none
      type(*),                       intent(in) :: a(:)
      integer(c_int16_t),     value, intent(in) :: t
      integer(c_signed_char), value, intent(in) :: k
      integer(c_size_t),      value, intent(in) :: e
      integer(c_size_t),      value, intent(in) :: n
    end subroutine check_tk_as
    subroutine check_tk_ar(a, t, k, e, n) &
      bind(c, name="check_tk")
      use, intrinsic :: iso_c_binding, only: &
        c_int16_t, c_signed_char, c_size_t
      implicit none
      type(*),                       intent(in) :: a(..)
      integer(c_int16_t),     value, intent(in) :: t
      integer(c_signed_char), value, intent(in) :: k
      integer(c_size_t),      value, intent(in) :: e
      integer(c_size_t),      value, intent(in) :: n
    end subroutine check_tk_ar
  end interface

contains

  elemental function cfi_encode_type(type, kind) result(itype)
    integer(kind=c_signed_char), intent(in) :: type
    integer(kind=c_signed_char), intent(in) :: kind

    integer(kind=c_int16_t) :: itype, ikind

    itype = int(type, kind=c_int16_t)
    itype = iand(itype, CFI_type_mask)
    ikind = int(kind, kind=c_int16_t)
    ikind = iand(ikind, CFI_type_mask)
    ikind = shiftl(ikind, CFI_type_kind_shift)
    itype = ior(ikind, itype)
    return
  end function cfi_encode_type
  
end module isof_m

module iso_check_m

  use, intrinsic :: iso_c_binding, only: &
    c_signed_char, c_int16_t, c_size_t

  use, intrinsic :: iso_c_binding, only: &
    c_int, c_ptr, c_loc, c_associated

  use, intrinsic :: iso_c_binding, only: &
    c_ptr

  use :: isof_m, only:  &
    CFI_type_cptr

  use :: isof_m, only: &
    check_tk_as,       &
    check_tk_ar

  use :: isof_m, only: &
    cfi_encode_type
  
  implicit none

  integer                           :: i
  integer(kind=c_size_t), parameter :: b = 8
  integer,                parameter :: n = 11
  
  type, bind(c) :: c_foo_t
    integer(kind=c_int) :: a
  end type c_foo_t
  
  type(c_foo_t), parameter :: ref_c_foo_t(*) = [(c_foo_t(a=i), i=1,n)]
  
  type(c_foo_t), protected, target :: target_c_foo_t(n)

  
contains

  subroutine check_c_ptr()
    type(c_ptr) :: p(n)
    integer :: i
    !
    target_c_foo_t = ref_c_foo_t
    p = [(c_loc(target_c_foo_t(i)), i=1,n)]
    call f_check_c_ptr_as(p)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 1
    do i = 1, n
      if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 2
    end do
    target_c_foo_t = ref_c_foo_t
    p = [(c_loc(target_c_foo_t(i)), i=1,n)]
    call c_check_c_ptr_as(p)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 3
    do i = 1, n
      if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 4
    end do
    target_c_foo_t = ref_c_foo_t
    p = [(c_loc(target_c_foo_t(i)), i=1,n)]
    call f_check_c_ptr_ar(p)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 5
    do i = 1, n
      if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 6
    end do
    target_c_foo_t = ref_c_foo_t
    p = [(c_loc(target_c_foo_t(i)), i=1,n)]
    call c_check_c_ptr_ar(p)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 7
    do i = 1, n
      if(.not.c_associated(p(i), c_loc(target_c_foo_t(i)))) stop 8
    end do
    return
  end subroutine check_c_ptr

  subroutine f_check_c_ptr_as(a)
    type(c_ptr), intent(in) :: a(:)
    !
    integer(kind=c_int16_t)     :: t
    integer(kind=c_signed_char) :: k
    integer(kind=c_size_t)      :: e
    !
    k = 0
    e = storage_size(a)/b
    t = cfi_encode_type(CFI_type_cptr, k)
    ! Assumes 64-bit target.
    ! if(e/=8) stop 9
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 10
    do i = 1, n
      if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 11
    end do
    call check_tk_as(a, t, k, e, 1_c_size_t)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 12
    do i = 1, n
      if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 13
    end do
    return
  end subroutine f_check_c_ptr_as

  subroutine c_check_c_ptr_as(a) bind(c)
    type(c_ptr), intent(in) :: a(:)
    integer(kind=c_int16_t)     :: t
    integer(kind=c_signed_char) :: k
    integer(kind=c_size_t)      :: e
    !
    k = 0
    e = storage_size(a)/b
    t = cfi_encode_type(CFI_type_cptr, k)
    ! Assumes 64-bit target.
    ! if(e/=8) stop 14
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 15
    do i = 1, n
      if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 16
    end do
    call check_tk_as(a, t, k, e, 1_c_size_t)
    if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 17
    do i = 1, n
      if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 18
    end do
    return
  end subroutine c_check_c_ptr_as

  subroutine f_check_c_ptr_ar(a)
    type(c_ptr), intent(in) :: a(..)
    !
    integer(kind=c_int16_t)     :: t
    integer(kind=c_signed_char) :: k
    integer(kind=c_size_t)      :: e
    !
    k = 0
    e = storage_size(a)/b
    t = cfi_encode_type(CFI_type_cptr, k)
    ! Assumes 64-bit target.
    ! if(e/=8) stop 19
    select rank(a)
    rank(1)
      if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 20
      do i = 1, n
        if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 21
      end do
    rank default
      stop 22
    end select
    call check_tk_ar(a, t, k, e, 1_c_size_t)
    select rank(a)
    rank(1)
      if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 23
      do i = 1, n
        if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 24
      end do
    rank default
      stop 25
    end select
    return
  end subroutine f_check_c_ptr_ar

  subroutine c_check_c_ptr_ar(a) bind(c)
    type(c_ptr), intent(in) :: a(..)
    integer(kind=c_int16_t)     :: t
    integer(kind=c_signed_char) :: k
    integer(kind=c_size_t)      :: e
    !
    k = 0
    e = storage_size(a)/b
    t = cfi_encode_type(CFI_type_cptr, k)
    ! Assumes 64-bit target.
    ! if(e/=8) stop 26
    select rank(a)
    rank(1)
      if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 27
      do i = 1, n
        if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 28
      end do
    rank default
      stop 29
    end select
    call check_tk_ar(a, t, k, e, 1_c_size_t)
    select rank(a)
    rank(1)
      if(any(target_c_foo_t(:)%a/=ref_c_foo_t(:)%a)) stop 30
      do i = 1, n
        if(.not.c_associated(a(i), c_loc(target_c_foo_t(i)))) stop 31
      end do
    rank default
      stop 32
    end select
    return
  end subroutine c_check_c_ptr_ar

end module iso_check_m

program main_p
  
  use :: iso_check_m, only: &
    check_c_ptr

  implicit none

  call check_c_ptr()
  stop

end program main_p

!! Local Variables:
!! mode: f90
!! End: