Debian Codegraph @

gcc/testsuite/gfortran.dg/c-interop/allocatable-dummy.f90

raw
1 ! PR 101308
2 ! PR 92621(?)
3 ! { dg-do run }
4 ! { dg-additional-sources "allocatable-dummy-c.c dump-descriptors.c" }
5 ! { dg-additional-options "-g" }
6 !
7 ! TS 29113
8 ! 6.3 Argument association
9 !
10 ! When a Fortran procedure that has an INTENT(OUT) allocatable dummy
11 ! argument is invoked by a C function, and the actual argument in the C
12 ! function is the address of a C descriptor that describes an allocated
13 ! allocatable variable, the variable is deallocated on entry to the
14 ! Fortran procedure.
15
16 ! When a C function is invoked from a Fortran procedure via an interface
17 ! with an INTENT(OUT) allocatable dummy argument, and the actual
18 ! argument in the reference to the C function is an allocated
19 ! allocatable variable, the variable is deallocated on invocation
20 ! (before execution of the C function begins).
21
22 module m
23 use iso_c_binding
24
25 type, bind (c) :: t
26 real(C_FLOAT) :: xyz(3)
27 integer(C_INT) :: id
28 end type
29
30 interface
31 subroutine testit_c (a, x, y, z) bind (c)
32 use iso_c_binding
33 import :: t
34 type (t), allocatable, intent(out) :: a
35 real(C_FLOAT), value, intent(in) :: x, y, z
36 end subroutine
37 end interface
38
39 contains
40
41 subroutine testit_f (a, x, y, z)
42 type (t), allocatable, intent(out) :: a
43 real(C_FLOAT), value, intent(in) :: x, y, z
44 if (allocated (a)) stop 201
45 allocate (a)
46 a%id = 69
47 a%xyz(1) = x
48 a%xyz(2) = y
49 a%xyz(3) = z
50 end subroutine
51
52 subroutine testit_f_bind_c (a, x, y, z) bind (c)
53 type (t), allocatable, intent(out) :: a
54 real(C_FLOAT), value, intent(in) :: x, y, z
55 if (allocated (a)) stop 301
56 allocate (a)
57 a%id = -1
58 a%xyz(1) = x
59 a%xyz(2) = y
60 a%xyz(3) = z
61 end subroutine
62
63 end module
64
65 program test
66 use iso_c_binding
67 use m
68
69 type (t), allocatable :: b
70
71 if (allocated (b)) stop 401
72
73 ! Try the regular Fortran test routine.
74 allocate (b)
75 call testit_f (b, 1.0, 2.0, 3.0)
76 if (.not. allocated (b)) stop 402
77 deallocate (b)
78 if (allocated (b)) stop 403
79
80 ! Try the test routine written in Fortran with C binding.
81 allocate (b)
82 call testit_f_bind_c (b, 1.0, 2.0, 3.0)
83 if (.not. allocated (b)) stop 404
84 deallocate (b)
85 if (allocated (b)) stop 405
86
87 ! Try the test routine written in C. This calls testit_f_bind_c
88 ! before returning, so make sure that's what we've got when returning.
89 allocate (b)
90 call testit_c (b, -1.0, -2.0, -3.0)
91 if (.not. allocated (b)) stop 406
92 if (b%id .ne. -1) stop 407
93 if (b%xyz(1) .ne. -1.0) stop 408
94 if (b%xyz(2) .ne. -2.0) stop 408
95 if (b%xyz(3) .ne. -3.0) stop 408
96 deallocate (b)
97
98 end program