| 1 |
! { dg-do run } |
| 2 |
! PR fortran/118080 |
| 3 |
! |
| 4 |
! Test passing of scalar derived types (user-defined or ISO_C_BINDING) |
| 5 |
! to dummy argument with OPTIONAL + VALUE attribute |
| 6 |
! |
| 7 |
! Original/initial testcase by Tobias Burnus |
| 8 |
|
| 9 |
module m |
| 10 |
use iso_c_binding |
| 11 |
implicit none(type,external) |
| 12 |
logical is_present |
| 13 |
contains |
| 14 |
subroutine f(x) |
| 15 |
! void f (void * x, logical(kind=1) .x) - 2nd arg = is-present flag |
| 16 |
type(c_ptr), optional, value :: x |
| 17 |
if (present(x) .neqv. is_present) stop 1 |
| 18 |
if (present(x)) then |
| 19 |
block |
| 20 |
integer, pointer :: ptr |
| 21 |
call c_f_pointer(x,ptr) |
| 22 |
if (ptr /= 55) stop 2 |
| 23 |
end block |
| 24 |
endif |
| 25 |
end |
| 26 |
end |
| 27 |
|
| 28 |
module m0 |
| 29 |
use m |
| 30 |
implicit none(type,external) |
| 31 |
contains |
| 32 |
subroutine test_pr118080 |
| 33 |
type(c_ptr) :: a |
| 34 |
integer, target :: x |
| 35 |
a = c_loc(x) |
| 36 |
x = 55 |
| 37 |
|
| 38 |
is_present = .true. |
| 39 |
call f(a) |
| 40 |
|
| 41 |
is_present = .false. |
| 42 |
call f() |
| 43 |
|
| 44 |
! Trying again after the absent call: |
| 45 |
is_present = .true. |
| 46 |
call f(a) |
| 47 |
|
| 48 |
print *, "Passed original test" |
| 49 |
end subroutine test_pr118080 |
| 50 |
end |
| 51 |
|
| 52 |
! Exercise ISO_C_BINDING uses |
| 53 |
module m1 |
| 54 |
use iso_c_binding, only: c_ptr, c_funptr, C_NULL_PTR, C_NULL_FUNPTR |
| 55 |
implicit none |
| 56 |
logical :: is_present = .false. |
| 57 |
integer :: base = 0 |
| 58 |
contains |
| 59 |
subroutine test_c () |
| 60 |
type(c_ptr) :: x = C_NULL_PTR |
| 61 |
type(c_funptr) :: y = C_NULL_FUNPTR |
| 62 |
|
| 63 |
is_present = .true. |
| 64 |
base = 10 |
| 65 |
! Tests with c_ptr: |
| 66 |
call f_c (x) |
| 67 |
call f_c_opt (x) |
| 68 |
call f_c_val (x) |
| 69 |
call f_c_opt_val (x) |
| 70 |
call f_c2_opt (x) |
| 71 |
call f_c2_opt_val (x) |
| 72 |
|
| 73 |
! Tests with c_funptr: |
| 74 |
call g_c (y) |
| 75 |
call g_c_opt (y) |
| 76 |
call g_c_val (y) |
| 77 |
call g_c_opt_val (y) |
| 78 |
call g_c2_opt (y) |
| 79 |
call g_c2_opt_val (y) |
| 80 |
|
| 81 |
! Elemental subroutine calls: |
| 82 |
base = 20 |
| 83 |
call f_c ([x]) |
| 84 |
call f_c_opt ([x]) |
| 85 |
call f_c_val ([x]) |
| 86 |
call f_c_opt_val ([x]) |
| 87 |
call f_c2_opt ([x]) |
| 88 |
call f_c2_opt_val ([x]) |
| 89 |
|
| 90 |
call g_c ([y]) |
| 91 |
call g_c_opt ([y]) |
| 92 |
call g_c_val ([y]) |
| 93 |
call g_c_opt_val ([y]) |
| 94 |
call g_c2_opt ([y]) |
| 95 |
call g_c2_opt_val ([y]) |
| 96 |
|
| 97 |
is_present = .false. |
| 98 |
base = 30 |
| 99 |
call f_c_opt () |
| 100 |
call f_c_opt_val () |
| 101 |
call f_c2_opt () |
| 102 |
call f_c2_opt_val () |
| 103 |
|
| 104 |
call g_c_opt () |
| 105 |
call g_c_opt_val () |
| 106 |
call g_c2_opt () |
| 107 |
call g_c2_opt_val () |
| 108 |
|
| 109 |
print *, "Passed test_c" |
| 110 |
end subroutine test_c |
| 111 |
|
| 112 |
elemental subroutine f_c (x) |
| 113 |
type(c_ptr), intent(in) :: x |
| 114 |
end |
| 115 |
! |
| 116 |
elemental subroutine f_c_val (x) |
| 117 |
type(c_ptr), value :: x |
| 118 |
call f_c (x) |
| 119 |
end |
| 120 |
! |
| 121 |
elemental subroutine f_c_opt (x) |
| 122 |
type(c_ptr), intent(in), optional :: x |
| 123 |
if (present (x) .neqv. is_present) error stop base+1 |
| 124 |
end |
| 125 |
! |
| 126 |
elemental subroutine f_c_opt_val (x) |
| 127 |
type(c_ptr), value, optional :: x |
| 128 |
if (present (x) .neqv. is_present) error stop base+2 |
| 129 |
end |
| 130 |
! |
| 131 |
elemental subroutine f_c2_opt_val (x) |
| 132 |
type(c_ptr), value, optional :: x |
| 133 |
if (present (x) .neqv. is_present) error stop base+3 |
| 134 |
call f_c_opt (x) |
| 135 |
call f_c_opt_val (x) |
| 136 |
if (present (x)) call f_c (x) |
| 137 |
if (present (x)) call f_c_val (x) |
| 138 |
end |
| 139 |
! |
| 140 |
elemental subroutine f_c2_opt (x) |
| 141 |
type(c_ptr), intent(in), optional :: x |
| 142 |
if (present (x) .neqv. is_present) error stop base+4 |
| 143 |
call f_c_opt_val (x) |
| 144 |
call f_c2_opt_val (x) |
| 145 |
end |
| 146 |
|
| 147 |
elemental subroutine g_c (x) |
| 148 |
type(c_funptr), intent(in) :: x |
| 149 |
end |
| 150 |
! |
| 151 |
elemental subroutine g_c_val (x) |
| 152 |
type(c_funptr), value :: x |
| 153 |
call g_c (x) |
| 154 |
end |
| 155 |
! |
| 156 |
elemental subroutine g_c_opt (x) |
| 157 |
type(c_funptr), intent(in), optional :: x |
| 158 |
if (present (x) .neqv. is_present) error stop base+6 |
| 159 |
end |
| 160 |
! |
| 161 |
elemental subroutine g_c_opt_val (x) |
| 162 |
type(c_funptr), value, optional :: x |
| 163 |
if (present (x) .neqv. is_present) error stop base+7 |
| 164 |
end |
| 165 |
! |
| 166 |
elemental subroutine g_c2_opt_val (x) |
| 167 |
type(c_funptr), value, optional :: x |
| 168 |
if (present (x) .neqv. is_present) error stop base+8 |
| 169 |
call g_c_opt (x) |
| 170 |
call g_c_opt_val (x) |
| 171 |
if (present (x)) call g_c (x) |
| 172 |
if (present (x)) call g_c_val (x) |
| 173 |
end |
| 174 |
! |
| 175 |
elemental subroutine g_c2_opt (x) |
| 176 |
type(c_funptr), intent(in), optional :: x |
| 177 |
if (present (x) .neqv. is_present) error stop base+9 |
| 178 |
call g_c_opt_val (x) |
| 179 |
call g_c2_opt_val (x) |
| 180 |
end |
| 181 |
! |
| 182 |
end |
| 183 |
|
| 184 |
! Exercise simple user-defined types |
| 185 |
module m2 |
| 186 |
implicit none |
| 187 |
|
| 188 |
type t1 |
| 189 |
character(42) :: c = "" |
| 190 |
logical :: l = .false. |
| 191 |
end type t1 |
| 192 |
|
| 193 |
type, bind(c) :: t2 |
| 194 |
real :: r(8) = 0. |
| 195 |
complex :: c(4) = 0. |
| 196 |
integer :: i = 0 |
| 197 |
end type t2 |
| 198 |
|
| 199 |
logical :: is_present = .false. |
| 200 |
integer :: base = 0 |
| 201 |
contains |
| 202 |
subroutine test_t () |
| 203 |
type(t1) :: x |
| 204 |
type(t2) :: y |
| 205 |
|
| 206 |
x% c = "foo" |
| 207 |
|
| 208 |
is_present = .true. |
| 209 |
base = 50 |
| 210 |
! Tests with t1: |
| 211 |
call f_c (x) |
| 212 |
call f_c_opt (x) |
| 213 |
call f_c_val (x) |
| 214 |
call f_c_opt_val (x) |
| 215 |
call f_c2_opt (x) |
| 216 |
call f_c2_opt_val (x) |
| 217 |
|
| 218 |
! Tests with t2: |
| 219 |
call g_c (y) |
| 220 |
call g_c_opt (y) |
| 221 |
call g_c_val (y) |
| 222 |
call g_c_opt_val (y) |
| 223 |
call g_c2_opt (y) |
| 224 |
call g_c2_opt_val (y) |
| 225 |
|
| 226 |
! Elemental subroutine calls: |
| 227 |
base = 60 |
| 228 |
call f_c ([x]) |
| 229 |
call f_c_opt ([x]) |
| 230 |
call f_c_val ([x]) |
| 231 |
call f_c_opt_val ([x]) |
| 232 |
call f_c2_opt ([x]) |
| 233 |
call f_c2_opt_val ([x]) |
| 234 |
|
| 235 |
call g_c ([y]) |
| 236 |
call g_c_opt ([y]) |
| 237 |
call g_c_val ([y]) |
| 238 |
call g_c_opt_val ([y]) |
| 239 |
call g_c2_opt ([y]) |
| 240 |
call g_c2_opt_val ([y]) |
| 241 |
|
| 242 |
is_present = .false. |
| 243 |
base = 70 |
| 244 |
call f_c_opt () |
| 245 |
call f_c_opt_val () |
| 246 |
call f_c2_opt () |
| 247 |
call f_c2_opt_val () |
| 248 |
|
| 249 |
call g_c_opt () |
| 250 |
call g_c_opt_val () |
| 251 |
call g_c2_opt () |
| 252 |
call g_c2_opt_val () |
| 253 |
|
| 254 |
print *, "Passed test_t" |
| 255 |
end subroutine test_t |
| 256 |
|
| 257 |
elemental subroutine f_c (x) |
| 258 |
type(t1), intent(in) :: x |
| 259 |
if (x% c /= "foo") error stop base |
| 260 |
end |
| 261 |
! |
| 262 |
elemental subroutine f_c_val (x) |
| 263 |
type(t1), value :: x |
| 264 |
if (x% c /= "foo") error stop base+5 |
| 265 |
call f_c (x) |
| 266 |
end |
| 267 |
! |
| 268 |
elemental subroutine f_c_opt (x) |
| 269 |
type(t1), intent(in), optional :: x |
| 270 |
if (present (x) .neqv. is_present) error stop base+1 |
| 271 |
end |
| 272 |
! |
| 273 |
elemental subroutine f_c_opt_val (x) |
| 274 |
type(t1), value, optional :: x |
| 275 |
if (present (x) .neqv. is_present) error stop base+2 |
| 276 |
end |
| 277 |
! |
| 278 |
elemental subroutine f_c2_opt_val (x) |
| 279 |
type(t1), value, optional :: x |
| 280 |
if (present (x) .neqv. is_present) error stop base+3 |
| 281 |
call f_c_opt (x) |
| 282 |
call f_c_opt_val (x) |
| 283 |
if (present (x)) call f_c (x) |
| 284 |
if (present (x)) call f_c_val (x) |
| 285 |
end |
| 286 |
! |
| 287 |
elemental subroutine f_c2_opt (x) |
| 288 |
type(t1), intent(in), optional :: x |
| 289 |
if (present (x) .neqv. is_present) error stop base+4 |
| 290 |
call f_c_opt_val (x) |
| 291 |
call f_c2_opt_val (x) |
| 292 |
end |
| 293 |
|
| 294 |
elemental subroutine g_c (x) |
| 295 |
type(t2), intent(in) :: x |
| 296 |
end |
| 297 |
! |
| 298 |
elemental subroutine g_c_val (x) |
| 299 |
type(t2), value :: x |
| 300 |
call g_c (x) |
| 301 |
end |
| 302 |
! |
| 303 |
elemental subroutine g_c_opt (x) |
| 304 |
type(t2), intent(in), optional :: x |
| 305 |
if (present (x) .neqv. is_present) error stop base+6 |
| 306 |
end |
| 307 |
! |
| 308 |
elemental subroutine g_c_opt_val (x) |
| 309 |
type(t2), value, optional :: x |
| 310 |
if (present (x) .neqv. is_present) error stop base+7 |
| 311 |
end |
| 312 |
! |
| 313 |
elemental subroutine g_c2_opt_val (x) |
| 314 |
type(t2), value, optional :: x |
| 315 |
if (present (x) .neqv. is_present) error stop base+8 |
| 316 |
call g_c_opt (x) |
| 317 |
call g_c_opt_val (x) |
| 318 |
if (present (x)) call g_c (x) |
| 319 |
if (present (x)) call g_c_val (x) |
| 320 |
end |
| 321 |
! |
| 322 |
elemental subroutine g_c2_opt (x) |
| 323 |
type(t2), intent(in), optional :: x |
| 324 |
if (present (x) .neqv. is_present) error stop base+9 |
| 325 |
call g_c_opt_val (x) |
| 326 |
call g_c2_opt_val (x) |
| 327 |
end |
| 328 |
end |
| 329 |
|
| 330 |
program pr118080 |
| 331 |
use m0 |
| 332 |
use m1 |
| 333 |
use m2 |
| 334 |
implicit none |
| 335 |
call test_pr118080 () |
| 336 |
call test_c() |
| 337 |
call test_t() |
| 338 |
end |