Debian Codegraph @

gcc/testsuite/gfortran.dg/c-interop/fc-descriptor-7.f90

raw
1 ! PR 101309
2 ! { dg-do run }
3 ! { dg-additional-sources "fc-descriptor-7-c.c dump-descriptors.c" }
4 ! { dg-additional-options "-g" }
5 !
6 ! This program tests passing arrays that may not be contiguous through
7 ! descriptors to C functions as assumed-shape arguments.
8
9 program testit
10 use iso_c_binding
11 implicit none (type, external)
12
13 interface
14 subroutine ctest (a, is_cont) bind (c)
15 use iso_c_binding
16 integer(C_INT) :: a(:,:)
17 logical(C_Bool), value :: is_cont
18 end subroutine
19 subroutine ctest_cont (a, is_cont) bind (c, name="ctest")
20 use iso_c_binding
21 integer(C_INT), contiguous :: a(:,:)
22 logical(C_Bool), value :: is_cont
23 end subroutine
24
25 subroutine ctest_ar (a, is_cont) bind (c, name="ctest")
26 use iso_c_binding
27 integer(C_INT) :: a(..)
28 logical(C_Bool), value :: is_cont
29 end subroutine
30 subroutine ctest_ar_cont (a, is_cont) bind (c, name="ctest")
31 use iso_c_binding
32 integer(C_INT), contiguous :: a(..)
33 logical(C_Bool), value :: is_cont
34 end subroutine
35 end interface
36
37 integer :: i , j
38 integer(C_INT), target :: aa(10,5)
39 integer(C_INT), target :: bb(10,10)
40
41 ! Original array
42 do j = 1, 5
43 do i = 1, 10
44 aa(i,j) = i + 100*j
45 end do
46 end do
47
48 ! Transposed array
49 do j = 2, 10, 2
50 do i = 1, 10
51 bb(j, i) = i + 100*((j-2)/2 + 1)
52 end do
53 end do
54
55 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
56
57 ! Test both calling the C function directly, and via another function
58 ! that takes an assumed-shape/assumed-rank argument.
59
60 call ftest (transpose (aa), is_cont=.true._c_bool) ! Implementation choice: copy in; hence, contiguous
61 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
62
63 call ctest (transpose (aa), is_cont=.false._c_bool) ! Implementation choice: noncontigous / sm inversed
64 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
65 call ctest_cont (transpose (aa), is_cont=.true._c_bool)
66 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
67 call ctest_ar (transpose (aa), is_cont=.false._c_bool) ! Implementation choice: noncontigous / sm inversed
68 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
69 call ctest_ar_cont (transpose (aa), is_cont=.true._c_bool)
70 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
71
72
73 call ftest (bb(2:10:2, :), is_cont=.false._c_bool)
74 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
75
76 call ctest (bb(2:10:2, :), is_cont=.false._c_bool)
77 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
78 call ctest_cont (bb(2:10:2, :), is_cont=.true._c_bool)
79 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
80 call ctest_ar (bb(2:10:2, :), is_cont=.false._c_bool)
81 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
82 call ctest_ar_cont (bb(2:10:2, :), is_cont=.true._c_bool)
83 if (any (transpose (aa) /= bb(2:10:2, :))) error stop 1
84
85 contains
86 subroutine ftest (a, is_cont)
87 use iso_c_binding
88 integer(C_INT) :: a(:,:)
89 logical(c_bool), value, intent(in) :: is_cont
90 if (is_cont .NEQV. is_contiguous (a)) error stop 2
91 if (any (shape (a) /= [5, 10])) error stop 3
92 do j = 1, 5
93 do i = 1, 10
94 if (a(j, i) /= i + 100*j) error stop 4
95 if (a(j, i) /= aa(i,j)) error stop
96 end do
97 end do
98 call ctest (a, is_cont)
99 call ctest_cont (a, is_cont=.true._c_bool)
100 call ctest_ar (a, is_cont)
101 call ctest_ar_cont (a, is_cont=.true._c_bool)
102 end subroutine
103
104 subroutine ftest_ar (a, is_cont)
105 use iso_c_binding
106 integer(C_INT) :: a(..)
107 logical(c_bool), value, intent(in) :: is_cont
108 if (is_cont .NEQV. is_contiguous (a)) error stop 2
109 if (any (shape (a) /= [5, 10])) error stop 3
110 select rank (a)
111 rank(2)
112 do j = 1, 5
113 do i = 1, 10
114 if (a(j, i) /= i + 100*j) error stop 4
115 if (a(j, i) /= aa(i,j)) error stop
116 end do
117 end do
118 call ctest (a, is_cont)
119 call ctest_cont (a, is_cont=.true._c_bool)
120 call ftest_ar_con (a, is_cont=.true._c_bool)
121 end select
122 call ctest_ar (a, is_cont)
123 ! call ctest_ar_cont (a, is_cont=.true._c_bool) ! TODO/FIXME: ICE, cf. PR fortran/102729
124 ! call ftest_ar_con (a, is_cont=.true._c_bool) ! TODO/FIXME: ICE, cf. PR fortran/102729
125 end subroutine
126
127 subroutine ftest_ar_con (a, is_cont)
128 use iso_c_binding
129 integer(C_INT), contiguous :: a(..)
130 logical(c_bool), value, intent(in) :: is_cont
131 if (is_cont .NEQV. is_contiguous (a)) error stop 2
132 if (any (shape (a) /= [5, 10])) error stop 3
133 select rank (a)
134 rank(2)
135 do j = 1, 5
136 do i = 1, 10
137 if (a(j, i) /= i + 100*j) error stop 4
138 if (a(j, i) /= aa(i,j)) error stop
139 end do
140 end do
141 call ctest (a, is_cont)
142 call ctest_cont (a, is_cont=.true._c_bool)
143 end select
144 call ctest_ar (a, is_cont)
145 call ctest_ar_cont (a, is_cont=.true._c_bool)
146 end subroutine
147 end program