Debian Codegraph @

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

raw
1 ! PR 101304
2 ! { dg-do run }
3 ! { dg-additional-sources "contiguous-2-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.
29
30 ! ftest1 and ftest2 both negate the elements of their input array;
31 ! this allows testing that modifications to the array contents get
32 ! propagated back to the base array.
33
34 module m
35
36 contains
37
38 subroutine ftest1 (a, first, last, step) bind (c)
39 use iso_c_binding
40 integer(C_INT), contiguous :: a(:)
41 integer(C_INT), value :: first, last, step
42 integer :: i, ival
43
44 ! Sanity checking that we got a contiguous array. The direct call
45 ! to is_contiguous might be optimized away, but the indirect one
46 ! in check_contiguous shouldn't be.
47 ! FIXME: is this correct? "the Fortran processor will handle the
48 ! difference in contiguity" may not mean that it's required to make
49 ! the array contiguous, just that it can access it correctly?
50 if (.not. is_contiguous (a)) stop 301
51 call check_contiguous (a)
52
53 ! Sanity checking that we got the right input array contents.
54 ! print *, 'a on entry to ftest1'
55 ! do i = lbound(a, 1), ubound(a, 1)
56 ! print *, 'a(', i, ') = ', a(i)
57 ! end do
58 ival = first
59 do i = lbound(a, 1), ubound(a, 1)
60 if (a (i) .ne. ival) then
61 print *, 'a(', i, ') = ', a(i), ' expected ', ival
62 stop 302
63 end if
64 a(i) = - a(i)
65 ival = ival + step
66 end do
67 end subroutine
68
69 subroutine ftest2 (a, first, last, step) bind (c)
70 use iso_c_binding
71
72 integer(C_INT), contiguous :: a(..)
73 integer(C_INT), value :: first, last, step
74
75 select rank (a)
76 rank (1)
77 call ftest1 (a(:), first, last, step)
78 rank default
79 stop 303
80 end select
81 end subroutine
82
83 subroutine check_contiguous (a)
84 use iso_c_binding
85 integer(C_INT) :: a(..)
86 if (.not. is_contiguous (a)) stop 304
87 end subroutine
88
89 end module
90
91
92 program testit
93 use iso_c_binding
94 use m
95 implicit none
96
97 interface
98 subroutine ctest1 (a) bind (c)
99 use iso_c_binding
100 integer(C_INT) :: a(:)
101 end subroutine
102 subroutine ctest2 (a) bind (c)
103 use iso_c_binding
104 integer(C_INT) :: a(..)
105 end subroutine
106 end interface
107
108 integer(C_INT) :: aa(32)
109 integer :: i
110
111 ! assumed-shape, called from Fortran
112 do i = 1, 32
113 aa(i) = i
114 end do
115 call ftest1 (aa(4:12:2), 4, 12, 2)
116 do i = 1, 32
117 if (i .ge. 4 .and. i .le. 12 .and. mod (i-4,2) .eq. 0) then
118 if (aa (i) .ne. -i) stop 101
119 else
120 if (aa (i) .ne. i) stop 102
121 end if
122 end do
123
124 ! assumed-shape, called from C code which will use the C interface
125 ! to create a non-contiguous array section and pass it to ftest1.
126 do i = 1, 32
127 aa(i) = i
128 end do
129 call ctest1 (aa)
130
131 ! assumed-rank, called from Fortran
132 do i = 1, 32
133 aa(i) = i
134 end do
135 call ftest2 (aa(7:19:3), 7, 19, 3)
136 do i = 1, 32
137 if (i .ge. 7 .and. i .le. 19 .and. mod (i-7,3) .eq. 0) then
138 if (aa (i) .ne. -i) stop 201
139 else
140 if (aa (i) .ne. i) stop 202
141 end if
142 end do
143
144 ! assumed-rank, called from C code which will use the C interface
145 ! to create a non-contiguous array section and pass it to ftest2.
146 do i = 1, 32
147 aa(i) = i
148 end do
149 call ctest2 (aa)
150
151 end program
152