Debian Codegraph @

gcc/testsuite/gfortran.dg/c-interop/contiguous-3.f90

raw
1 ! PR 101304
2 ! { dg-do run }
3 ! { dg-additional-sources "contiguous-3-c.c dump-descriptors.c" }
4 ! { dg-additional-options "-g" }
5 !
6 ! TS 29113
7 ! 8.7 In an invocation of an interoperable procedure whose Fortran
8 ! interface has an assumed-shape or assumed-rank dummy argument with the
9 ! CONTIGUOUS attribute, the associated effective argument may be an
10 ! array that is not contiguous or the address of a C descriptor for such
11 ! an array. If the procedure is invoked from Fortran or the procedure is
12 ! a Fortran procedure, the Fortran processor will handle the difference
13 ! in contiguity. If the procedure is invoked from C and the procedure is
14 ! a C procedure, the C code within the procedure shall be prepared to
15 ! handle the situation of receiving a discontiguous argument.
16 !
17 ! The wording is different in the 2018 standard, but the intent is more
18 ! or less the same:
19 !
20 ! When an interoperable Fortran procedure that is invoked from C has a
21 ! dummy argument with the CONTIGUOUS attribute or that is an assumed-length
22 ! CHARACTER explicit-shape or assumed-size array, and the actual argument
23 ! is the address of a C descriptor for a discontiguous object, the Fortran
24 ! processor shall handle the difference in contiguity.
25 !
26 ! This program tests the cases where a Fortran procedure with C binding and
27 ! a dummy array argument with the contiguous attribute is invoked from
28 ! both C or Fortran. It is similar to contiguous-2.f90 but here the array
29 ! sections are created in Fortran even in the called-from-C case, rather
30 ! than by calling CFI_section.
31
32 ! ftest1 and ftest2 both negate the elements of their input array;
33 ! this allows testing that modifications to the array contents get
34 ! propagated back to the base array.
35
36 module m
37
38 contains
39
40 subroutine ftest1 (a, first, last, step) bind (c)
41 use iso_c_binding
42 integer(C_INT), contiguous :: a(:)
43 integer(C_INT), value :: first, last, step
44 integer :: i, ival
45
46 ! Sanity checking that we got a contiguous array. The direct call
47 ! to is_contiguous might be optimized away, but the indirect one
48 ! in check_contiguous shouldn't be.
49 ! FIXME: is this correct? "the Fortran processor will handle the
50 ! difference in contiguity" may not mean that it's required to make
51 ! the array contiguous, just that it can access it correctly?
52 if (.not. is_contiguous (a)) stop 301
53 call check_contiguous (a)
54
55 ! Sanity checking that we got the right input array contents.
56 ! print *, 'a on entry to ftest1'
57 ! do i = lbound(a, 1), ubound(a, 1)
58 ! print *, 'a(', i, ') = ', a(i)
59 ! end do
60 ival = first
61 do i = lbound(a, 1), ubound(a, 1)
62 if (a (i) .ne. ival) then
63 print *, 'a(', i, ') = ', a(i), ' expected ', ival
64 stop 302
65 end if
66 a(i) = - a(i)
67 ival = ival + step
68 end do
69 end subroutine
70
71 subroutine ftest2 (a, first, last, step) bind (c)
72 use iso_c_binding
73
74 integer(C_INT), contiguous :: a(..)
75 integer(C_INT), value :: first, last, step
76
77 select rank (a)
78 rank (1)
79 call ftest1 (a(:), first, last, step)
80 rank default
81 stop 303
82 end select
83 end subroutine
84
85 subroutine check_contiguous (a)
86 use iso_c_binding
87 integer(C_INT) :: a(..)
88 if (.not. is_contiguous (a)) stop 304
89 end subroutine
90
91 end module
92
93
94 program testit
95 use iso_c_binding
96 use m
97 implicit none
98
99 ! Note ctest1 and ctest2 do not have the contiguous attribute on a.
100 interface
101 subroutine ctest1 (a, first, last, step) bind (c)
102 use iso_c_binding
103 integer(C_INT) :: a(:)
104 integer(C_INT), value :: first, last, step
105 end subroutine
106 subroutine ctest2 (a, first, last, step) bind (c)
107 use iso_c_binding
108 integer(C_INT) :: a(..)
109 integer(C_INT), value :: first, last, step
110 end subroutine
111 end interface
112
113 integer(C_INT) :: aa(32)
114 integer :: i
115
116 ! assumed-shape, called from Fortran
117 do i = 1, 32
118 aa(i) = i
119 end do
120 call ftest1 (aa(4:12:2), 4, 12, 2)
121 do i = 1, 32
122 if (i .ge. 4 .and. i .le. 12 .and. mod (i-4,2) .eq. 0) then
123 if (aa (i) .ne. -i) stop 101
124 else
125 if (aa (i) .ne. i) stop 102
126 end if
127 end do
128
129 ! assumed-shape, called indirectly from C code, using an array
130 ! section created in Fortran instead of by CFI_section
131 do i = 1, 32
132 aa(i) = i
133 end do
134 call ctest1 (aa(5:13:2), 5, 13, 2)
135 do i = 1, 32
136 if (i .ge. 5 .and. i .le. 13 .and. mod (i-5,2) .eq. 0) then
137 if (aa (i) .ne. -i) stop 103
138 else
139 if (aa (i) .ne. i) stop 104
140 end if
141 end do
142
143 ! assumed-rank, called from Fortran
144 do i = 1, 32
145 aa(i) = i
146 end do
147 call ftest2 (aa(7:19:3), 7, 19, 3)
148 do i = 1, 32
149 if (i .ge. 7 .and. i .le. 19 .and. mod (i-7,3) .eq. 0) then
150 if (aa (i) .ne. -i) stop 201
151 else
152 if (aa (i) .ne. i) stop 202
153 end if
154 end do
155
156 ! assumed-rank, called indirectly from C code, using an array
157 ! section created in Fortran instead of by CFI_section
158 do i = 1, 32
159 aa(i) = i
160 end do
161 call ctest2 (aa(8:20:3), 8, 20, 3)
162 do i = 1, 32
163 if (i .ge. 8 .and. i .le. 20 .and. mod (i-8,3) .eq. 0) then
164 if (aa (i) .ne. -i) stop 203
165 else
166 if (aa (i) .ne. i) stop 204
167 end if
168 end do
169
170 end program
171