Debian Codegraph @

gcc/testsuite/gfortran.dg/c-interop/ff-descriptor-4.f90

raw
1 ! { dg-do run }
2 !
3 ! This program checks that passing allocatable and pointer arrays to
4 ! and from Fortran functions with C binding works.
5
6 module mm
7 use iso_c_binding
8 type, bind (c) :: m
9 integer(C_INT) :: i, j
10 end type
11 end module
12
13 program testit
14 use iso_c_binding
15 use mm
16 implicit none
17
18 type(m), allocatable :: a(:)
19 type(m), target :: t(3,10)
20 type(m), pointer :: p(:,:)
21
22 p => NULL()
23
24 call testc (a, t, p)
25 call testf (a, t, p)
26
27 contains
28
29 ! C binding version
30
31 subroutine checkc (a, t, p, initp) bind (c)
32 use iso_c_binding
33 use mm
34 type(m), allocatable :: a(:)
35 type(m), target :: t(3,10)
36 type(m), pointer :: p(:,:)
37 logical, value :: initp
38 integer :: i, j
39
40 if (rank (a) .ne. 1) stop 101
41 if (rank (t) .ne. 2) stop 102
42 if (rank (p) .ne. 2) stop 103
43
44 if (initp) then
45 if (.not. allocated (a)) stop 104
46 if (.not. associated (p)) stop 105
47 if (.not. associated (p, t)) stop 106
48 if (size (a, 1) .ne. 5) stop 107
49 if (size (p, 1) .ne. 3) stop 108
50 if (size (p, 2) .ne. 10) stop 109
51 else
52 if (allocated (a)) stop 121
53 if (associated (p)) stop 122
54 end if
55
56 end subroutine
57
58 ! Fortran binding version
59 subroutine checkf (a, t, p, initp)
60 use iso_c_binding
61 use mm
62 type(m), allocatable :: a(:)
63 type(m), target :: t(3,10)
64 type(m), pointer :: p(:,:)
65 logical, value :: initp
66 integer :: i, j
67
68 if (rank (a) .ne. 1) stop 201
69 if (rank (t) .ne. 2) stop 202
70 if (rank (p) .ne. 2) stop 203
71
72 if (initp) then
73 if (.not. allocated (a)) stop 204
74 if (.not. associated (p)) stop 205
75 if (.not. associated (p, t)) stop 206
76 if (size (a, 1) .ne. 5) stop 207
77 if (size (p, 1) .ne. 3) stop 208
78 if (size (p, 2) .ne. 10) stop 209
79 else
80 if (allocated (a)) stop 221
81 if (associated (p)) stop 222
82 end if
83
84 end subroutine
85
86 ! C binding version
87 subroutine allocatec (a, t, p) bind (c)
88 use iso_c_binding
89 use mm
90 type(m), allocatable :: a(:)
91 type(m), target :: t(3,10)
92 type(m), pointer :: p(:,:)
93
94 allocate (a(10:20))
95 p => t
96 end subroutine
97
98 ! Fortran binding version
99 subroutine allocatef (a, t, p) bind (c)
100 use iso_c_binding
101 use mm
102 type(m), allocatable :: a(:)
103 type(m), target :: t(3,10)
104 type(m), pointer :: p(:,:)
105
106 allocate (a(5:15))
107 p => t
108 end subroutine
109
110 ! C binding version
111 subroutine testc (a, t, p) bind (c)
112 use iso_c_binding
113 use mm
114 type(m), allocatable :: a(:)
115 type(m), target :: t(3,10)
116 type(m), pointer :: p(:,:)
117
118 ! Call both the C and Fortran binding check functions
119 call checkc (a, t, p, .false.)
120 call checkf (a, t, p, .false.)
121
122 ! Allocate/associate and check again.
123 allocate (a(5))
124 p => t
125 call checkc (a, t, p, .true.)
126 call checkf (a, t, p, .true.)
127
128 ! Reset and check a third time.
129 deallocate (a)
130 p => NULL ()
131 call checkc (a, t, p, .false.)
132 call checkf (a, t, p, .false.)
133
134 ! Allocate/associate inside a function with Fortran binding.
135 call allocatef (a, t, p)
136 if (.not. allocated (a)) stop 301
137 if (.not. associated (p)) stop 302
138 if (lbound (a, 1) .ne. 5) stop 303
139 if (ubound (a, 1) .ne. 15) stop 304
140 deallocate (a)
141 p => NULL ()
142
143 ! Allocate/associate inside a function with C binding.
144 call allocatec (a, t, p)
145 if (.not. allocated (a)) stop 311
146 if (.not. associated (p)) stop 312
147 if (lbound (a, 1) .ne. 10) stop 313
148 if (ubound (a, 1) .ne. 20) stop 314
149 deallocate (a)
150 p => NULL ()
151
152 end subroutine
153
154 ! Fortran binding version
155 subroutine testf (a, t, p)
156 use iso_c_binding
157 use mm
158 type(m), allocatable :: a(:)
159 type(m), target :: t(3,10)
160 type(m), pointer :: p(:,:)
161
162 ! Call both the C and Fortran binding check functions
163 call checkc (a, t, p, .false.)
164 call checkf (a, t, p, .false.)
165
166 ! Allocate/associate and check again.
167 allocate (a(5))
168 p => t
169 call checkc (a, t, p, .true.)
170 call checkf (a, t, p, .true.)
171
172 ! Reset and check a third time.
173 deallocate (a)
174 p => NULL ()
175 call checkc (a, t, p, .false.)
176 call checkf (a, t, p, .false.)
177
178 ! Allocate/associate inside a function with Fortran binding.
179 call allocatef (a, t, p)
180 if (.not. allocated (a)) stop 401
181 if (.not. associated (p)) stop 402
182 if (lbound (a, 1) .ne. 5) stop 403
183 if (ubound (a, 1) .ne. 15) stop 404
184 deallocate (a)
185 p => NULL ()
186
187 ! Allocate/associate inside a function with C binding.
188 call allocatec (a, t, p)
189 if (.not. allocated (a)) stop 411
190 if (.not. associated (p)) stop 412
191 if (lbound (a, 1) .ne. 10) stop 413
192 if (ubound (a, 1) .ne. 20) stop 414
193 deallocate (a)
194 p => NULL ()
195
196 end subroutine
197
198 end program