annotate gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03 @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ! { dg-do run }
kono
parents:
diff changeset
2 !
kono
parents:
diff changeset
3 ! Basic tests of functionality of unlimited polymorphism
kono
parents:
diff changeset
4 !
kono
parents:
diff changeset
5 ! Contributed by Paul Thomas <pault@gcc.gnu.org>
kono
parents:
diff changeset
6 !
kono
parents:
diff changeset
7 MODULE m
kono
parents:
diff changeset
8 TYPE :: a
kono
parents:
diff changeset
9 integer :: i
kono
parents:
diff changeset
10 END TYPE
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 contains
kono
parents:
diff changeset
13 subroutine bar (arg, res)
kono
parents:
diff changeset
14 class(*) :: arg
kono
parents:
diff changeset
15 character(100) :: res
kono
parents:
diff changeset
16 select type (w => arg)
kono
parents:
diff changeset
17 type is (a)
kono
parents:
diff changeset
18 write (res, '(a, I4)') "type(a)", w%i
kono
parents:
diff changeset
19 type is (integer)
kono
parents:
diff changeset
20 write (res, '(a, I4)') "integer", w
kono
parents:
diff changeset
21 type is (real(4))
kono
parents:
diff changeset
22 write (res, '(a, F4.1)') "real4", w
kono
parents:
diff changeset
23 type is (real(8))
kono
parents:
diff changeset
24 write (res, '(a, F4.1)') "real8", w
kono
parents:
diff changeset
25 type is (character(*, kind = 4))
kono
parents:
diff changeset
26 call abort
kono
parents:
diff changeset
27 type is (character(*))
kono
parents:
diff changeset
28 write (res, '(a, I2, a, a)') "char(", LEN(w), ")", trim(w)
kono
parents:
diff changeset
29 end select
kono
parents:
diff changeset
30 end subroutine
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 subroutine foo (arg, res)
kono
parents:
diff changeset
33 class(*) :: arg (:)
kono
parents:
diff changeset
34 character(100) :: res
kono
parents:
diff changeset
35 select type (w => arg)
kono
parents:
diff changeset
36 type is (a)
kono
parents:
diff changeset
37 write (res,'(a, 10I4)') "type(a) array", w%i
kono
parents:
diff changeset
38 type is (integer)
kono
parents:
diff changeset
39 write (res,'(a, 10I4)') "integer array", w
kono
parents:
diff changeset
40 type is (real)
kono
parents:
diff changeset
41 write (res,'(a, 10F4.1)') "real array", w
kono
parents:
diff changeset
42 type is (character(*))
kono
parents:
diff changeset
43 write (res, '(a5, I2, a, I2, a1, 2(a))') &
kono
parents:
diff changeset
44 "char(",len(w),",", size(w,1),") array ", w
kono
parents:
diff changeset
45 end select
kono
parents:
diff changeset
46 end subroutine
kono
parents:
diff changeset
47 END MODULE
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 USE m
kono
parents:
diff changeset
51 TYPE(a), target :: obj1 = a(99)
kono
parents:
diff changeset
52 TYPE(a), target :: obj2(3) = a(999)
kono
parents:
diff changeset
53 integer, target :: obj3 = 999
kono
parents:
diff changeset
54 real(4), target :: obj4(4) = [(real(i), i = 1, 4)]
kono
parents:
diff changeset
55 integer, target :: obj5(3) = [(i*99, i = 1, 3)]
kono
parents:
diff changeset
56 class(*), pointer :: u1
kono
parents:
diff changeset
57 class(*), pointer :: u2(:)
kono
parents:
diff changeset
58 class(*), allocatable :: u3
kono
parents:
diff changeset
59 class(*), allocatable :: u4(:)
kono
parents:
diff changeset
60 type(a), pointer :: aptr(:)
kono
parents:
diff changeset
61 character(8) :: sun = "sunshine"
kono
parents:
diff changeset
62 character(100) :: res
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 ! NULL without MOLD used to cause segfault
kono
parents:
diff changeset
65 u2 => NULL()
kono
parents:
diff changeset
66 u2 => NULL(aptr)
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 ! Test pointing to derived types.
kono
parents:
diff changeset
69 u1 => obj1
kono
parents:
diff changeset
70 if (SAME_TYPE_AS (obj1, u1) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
71 u2 => obj2
kono
parents:
diff changeset
72 call bar (u1, res)
kono
parents:
diff changeset
73 if (trim (res) .ne. "type(a) 99") call abort
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 call foo (u2, res)
kono
parents:
diff changeset
76 if (trim (res) .ne. "type(a) array 999 999 999") call abort
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 if (SAME_TYPE_AS (obj1, u1) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 ! Check allocate with an array SOURCE.
kono
parents:
diff changeset
81 allocate (u2(5), source = [(a(i), i = 1,5)])
kono
parents:
diff changeset
82 if (SAME_TYPE_AS (u1, a(2)) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
83 call foo (u2, res)
kono
parents:
diff changeset
84 if (trim (res) .ne. "type(a) array 1 2 3 4 5") call abort
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 deallocate (u2)
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 ! Point to intrinsic targets.
kono
parents:
diff changeset
89 u1 => obj3
kono
parents:
diff changeset
90 call bar (u1, res)
kono
parents:
diff changeset
91 if (trim (res) .ne. "integer 999") call abort
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 u2 => obj4
kono
parents:
diff changeset
94 call foo (u2, res)
kono
parents:
diff changeset
95 if (trim (res) .ne. "real array 1.0 2.0 3.0 4.0") call abort
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 u2 => obj5
kono
parents:
diff changeset
98 call foo (u2, res)
kono
parents:
diff changeset
99 if (trim (res) .ne. "integer array 99 198 297") call abort
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 ! Test allocate with source.
kono
parents:
diff changeset
102 allocate (u1, source = sun)
kono
parents:
diff changeset
103 call bar (u1, res)
kono
parents:
diff changeset
104 if (trim (res) .ne. "char( 8)sunshine") call abort
kono
parents:
diff changeset
105 deallocate (u1)
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 allocate (u2(3), source = [7,8,9])
kono
parents:
diff changeset
108 call foo (u2, res)
kono
parents:
diff changeset
109 if (trim (res) .ne. "integer array 7 8 9") call abort
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 deallocate (u2)
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 if (EXTENDS_TYPE_OF (obj1, u2) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
114 if (EXTENDS_TYPE_OF (u2, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 allocate (u2(3), source = [5.0,6.0,7.0])
kono
parents:
diff changeset
117 call foo (u2, res)
kono
parents:
diff changeset
118 if (trim (res) .ne. "real array 5.0 6.0 7.0") call abort
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 if (EXTENDS_TYPE_OF (obj1, u2) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
121 if (EXTENDS_TYPE_OF (u2, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
122 deallocate (u2)
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 ! Check allocate with a MOLD tag.
kono
parents:
diff changeset
125 allocate (u2(3), mold = 8.0)
kono
parents:
diff changeset
126 call foo (u2, res)
kono
parents:
diff changeset
127 if (res(1:10) .ne. "real array") call abort
kono
parents:
diff changeset
128 deallocate (u2)
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 ! Test passing an intrinsic type to a CLASS(*) formal.
kono
parents:
diff changeset
131 call bar(1, res)
kono
parents:
diff changeset
132 if (trim (res) .ne. "integer 1") call abort
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 call bar(2.0, res)
kono
parents:
diff changeset
135 if (trim (res) .ne. "real4 2.0") call abort
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 call bar(2d0, res)
kono
parents:
diff changeset
138 if (trim (res) .ne. "real8 2.0") call abort
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 call bar(a(3), res)
kono
parents:
diff changeset
141 if (trim (res) .ne. "type(a) 3") call abort
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 call bar(sun, res)
kono
parents:
diff changeset
144 if (trim (res) .ne. "char( 8)sunshine") call abort
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 call bar (obj3, res)
kono
parents:
diff changeset
147 if (trim (res) .ne. "integer 999") call abort
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 call foo([4,5], res)
kono
parents:
diff changeset
150 if (trim (res) .ne. "integer array 4 5") call abort
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 call foo([6.0,7.0], res)
kono
parents:
diff changeset
153 if (trim (res) .ne. "real array 6.0 7.0") call abort
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 call foo([a(8),a(9)], res)
kono
parents:
diff changeset
156 if (trim (res) .ne. "type(a) array 8 9") call abort
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 call foo([sun, " & rain"], res)
kono
parents:
diff changeset
159 if (trim (res) .ne. "char( 8, 2)sunshine & rain") call abort
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 call foo([sun//" never happens", " & rain always happens"], res)
kono
parents:
diff changeset
162 if (trim (res) .ne. "char(22, 2)sunshine never happens & rain always happens") call abort
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 call foo (obj4, res)
kono
parents:
diff changeset
165 if (trim (res) .ne. "real array 1.0 2.0 3.0 4.0") call abort
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 call foo (obj5, res)
kono
parents:
diff changeset
168 if (trim (res) .ne. "integer array 99 198 297") call abort
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 ! Allocatable entities
kono
parents:
diff changeset
171 if (EXTENDS_TYPE_OF (obj1, u3) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
172 if (EXTENDS_TYPE_OF (u3, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
173 if (EXTENDS_TYPE_OF (obj1, u4) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
174 if (EXTENDS_TYPE_OF (u4, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 allocate (u3, source = 2.4)
kono
parents:
diff changeset
177 call bar (u3, res)
kono
parents:
diff changeset
178 if (trim (res) .ne. "real4 2.4") call abort
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 allocate (u4(2), source = [a(88), a(99)])
kono
parents:
diff changeset
181 call foo (u4, res)
kono
parents:
diff changeset
182 if (trim (res) .ne. "type(a) array 88 99") call abort
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 if (EXTENDS_TYPE_OF (obj1, u3) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
185 if (EXTENDS_TYPE_OF (u3, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 deallocate (u3)
kono
parents:
diff changeset
188 if (EXTENDS_TYPE_OF (obj1, u3) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
189 if (EXTENDS_TYPE_OF (u3, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 if (EXTENDS_TYPE_OF (obj1, u4) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
192 if (EXTENDS_TYPE_OF (u4, obj1) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
193 deallocate (u4)
kono
parents:
diff changeset
194 if (EXTENDS_TYPE_OF (obj1, u4) .neqv. .TRUE.) call abort
kono
parents:
diff changeset
195 if (EXTENDS_TYPE_OF (u4, obj1) .neqv. .FALSE.) call abort
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 ! Check assumed rank calls
kono
parents:
diff changeset
199 call foobar (u3, 0)
kono
parents:
diff changeset
200 call foobar (u4, 1)
kono
parents:
diff changeset
201 contains
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 subroutine foobar (arg, ranki)
kono
parents:
diff changeset
204 class(*) :: arg (..)
kono
parents:
diff changeset
205 integer :: ranki
kono
parents:
diff changeset
206 integer i
kono
parents:
diff changeset
207 i = rank (arg)
kono
parents:
diff changeset
208 if (i .ne. ranki) call abort
kono
parents:
diff changeset
209 end subroutine
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 END