Debian Codegraph @

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

raw
1 ! { dg-do run }
2 !
3 ! This program checks that passing allocatable and pointer scalars 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
12 integer, parameter :: imagic=-1, jmagic=42
13 end module
14
15 program testit
16 use iso_c_binding
17 use mm
18 implicit none
19
20 type(m), allocatable :: a
21 type(m), target :: t
22 type(m), pointer :: p
23
24 p => NULL()
25
26 call testc (a, t, p)
27 call testf (a, t, p)
28
29 contains
30
31 ! C binding version
32
33 subroutine checkc (a, t, p, initp) bind (c)
34 use iso_c_binding
35 use mm
36 type(m), allocatable :: a
37 type(m), target :: t
38 type(m), pointer :: p
39 logical, value :: initp
40
41 if (initp) then
42 if (.not. allocated (a)) stop 101
43 if (a%i .ne. imagic) stop 102
44 if (a%j .ne. jmagic) stop 103
45 if (.not. associated (p)) stop 104
46 if (.not. associated (p, t)) stop 105
47 if (p%i .ne. imagic) stop 106
48 if (p%j .ne. jmagic) stop 107
49 else
50 if (allocated (a)) stop 108
51 if (associated (p)) stop 109
52 end if
53
54 if (rank (a) .ne. 0) stop 110
55 if (rank (t) .ne. 0) stop 111
56 if (rank (p) .ne. 0) stop 112
57
58 end subroutine
59
60 ! Fortran binding version
61 subroutine checkf (a, t, p, initp)
62 use iso_c_binding
63 use mm
64 type(m), allocatable :: a
65 type(m), target :: t
66 type(m), pointer :: p
67 logical, value :: initp
68
69 if (initp) then
70 if (.not. allocated (a)) stop 201
71 if (a%i .ne. imagic) stop 202
72 if (a%j .ne. jmagic) stop 203
73 if (.not. associated (p)) stop 204
74 if (.not. associated (p, t)) stop 205
75 if (p%i .ne. imagic) stop 206
76 if (p%j .ne. jmagic) stop 207
77 else
78 if (allocated (a)) stop 208
79 if (associated (p)) stop 209
80 end if
81
82 if (rank (a) .ne. 0) stop 210
83 if (rank (t) .ne. 0) stop 211
84 if (rank (p) .ne. 0) stop 212
85
86 end subroutine
87
88 ! C binding version
89 subroutine testc (a, t, p) bind (c)
90 use iso_c_binding
91 use mm
92 type(m), allocatable :: a
93 type(m), target :: t
94 type(m), pointer :: p
95
96 ! Call both the C and Fortran binding check functions
97 call checkc (a, t, p, .false.)
98 call checkf (a, t, p, .false.)
99
100 ! Allocate/associate and check again.
101 allocate (a)
102 a%i = imagic
103 a%j = jmagic
104 p => t
105 t%i = imagic
106 t%j = jmagic
107 call checkc (a, t, p, .true.)
108 call checkf (a, t, p, .true.)
109
110 ! Reset and check a third time.
111 deallocate (a)
112 p => NULL ()
113 call checkc (a, t, p, .false.)
114 call checkf (a, t, p, .false.)
115
116 end subroutine
117
118 ! Fortran binding version
119 subroutine testf (a, t, p)
120 use iso_c_binding
121 use mm
122 type(m), allocatable :: a
123 type(m), target :: t
124 type(m), pointer :: p
125
126 ! Call both the C and Fortran binding check functions
127 call checkc (a, t, p, .false.)
128 call checkf (a, t, p, .false.)
129
130 ! Allocate/associate and check again.
131 allocate (a)
132 a%i = imagic
133 a%j = jmagic
134 p => t
135 t%i = imagic
136 t%j = jmagic
137 call checkc (a, t, p, .true.)
138 call checkf (a, t, p, .true.)
139
140 ! Reset and check a third time.
141 deallocate (a)
142 p => NULL ()
143 call checkc (a, t, p, .false.)
144 call checkf (a, t, p, .false.)
145
146 end subroutine
147
148 end program