annotate gcc/testsuite/gfortran.dg/unlimited_polymorphic_24.f03 @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
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 ! Copyright 2015 NVIDIA Corporation
kono
parents:
diff changeset
4 !
kono
parents:
diff changeset
5 ! Test case for unlimited polymorphism that is derived from the article
kono
parents:
diff changeset
6 ! by Mark Leair, in the 'PGInsider':
kono
parents:
diff changeset
7 ! https://www.pgroup.com/lit/articles/insider/v3n2a2.htm
kono
parents:
diff changeset
8 ! Note that 'addValue' has been removed from the generic 'add' because
kono
parents:
diff changeset
9 ! gfortran asserts that this is ambiguous. See
kono
parents:
diff changeset
10 ! https://gcc.gnu.org/ml/fortran/2015-03/msg00002.html for a discussion.
kono
parents:
diff changeset
11 !
kono
parents:
diff changeset
12 module link_mod
kono
parents:
diff changeset
13 private
kono
parents:
diff changeset
14 public :: link, output, index
kono
parents:
diff changeset
15 character(6) :: output (14)
kono
parents:
diff changeset
16 integer :: index = 0
kono
parents:
diff changeset
17 type link
kono
parents:
diff changeset
18 private
kono
parents:
diff changeset
19 class(*), pointer :: value => null() ! value stored in link
kono
parents:
diff changeset
20 type(link), pointer :: next => null()! next link in list
kono
parents:
diff changeset
21 contains
kono
parents:
diff changeset
22 procedure :: getValue ! return value pointer
kono
parents:
diff changeset
23 procedure :: printLinks ! print linked list starting with this link
kono
parents:
diff changeset
24 procedure :: nextLink ! return next pointer
kono
parents:
diff changeset
25 procedure :: setNextLink ! set next pointer
kono
parents:
diff changeset
26 end type link
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 interface link
kono
parents:
diff changeset
29 procedure constructor ! construct/initialize a link
kono
parents:
diff changeset
30 end interface
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 contains
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 function nextLink(this)
kono
parents:
diff changeset
35 class(link) :: this
kono
parents:
diff changeset
36 class(link), pointer :: nextLink
kono
parents:
diff changeset
37 nextLink => this%next
kono
parents:
diff changeset
38 end function nextLink
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 subroutine setNextLink(this,next)
kono
parents:
diff changeset
41 class(link) :: this
kono
parents:
diff changeset
42 class(link), pointer :: next
kono
parents:
diff changeset
43 this%next => next
kono
parents:
diff changeset
44 end subroutine setNextLink
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 function getValue(this)
kono
parents:
diff changeset
47 class(link) :: this
kono
parents:
diff changeset
48 class(*), pointer :: getValue
kono
parents:
diff changeset
49 getValue => this%value
kono
parents:
diff changeset
50 end function getValue
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 subroutine printLink(this)
kono
parents:
diff changeset
53 class(link) :: this
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 index = index + 1
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 select type(v => this%value)
kono
parents:
diff changeset
58 type is (integer)
kono
parents:
diff changeset
59 write (output(index), '(i6)') v
kono
parents:
diff changeset
60 type is (character(*))
kono
parents:
diff changeset
61 write (output(index), '(a6)') v
kono
parents:
diff changeset
62 type is (real)
kono
parents:
diff changeset
63 write (output(index), '(f6.2)') v
kono
parents:
diff changeset
64 class default
kono
parents:
diff changeset
65 stop 'printLink: unexepected type for link'
kono
parents:
diff changeset
66 end select
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 end subroutine printLink
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 subroutine printLinks(this)
kono
parents:
diff changeset
71 class(link) :: this
kono
parents:
diff changeset
72 class(link), pointer :: curr
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 call printLink(this)
kono
parents:
diff changeset
75 curr => this%next
kono
parents:
diff changeset
76 do while(associated(curr))
kono
parents:
diff changeset
77 call printLink(curr)
kono
parents:
diff changeset
78 curr => curr%next
kono
parents:
diff changeset
79 end do
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 end subroutine
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 function constructor(value, next)
kono
parents:
diff changeset
84 class(link),pointer :: constructor
kono
parents:
diff changeset
85 class(*) :: value
kono
parents:
diff changeset
86 class(link), pointer :: next
kono
parents:
diff changeset
87 allocate(constructor)
kono
parents:
diff changeset
88 constructor%next => next
kono
parents:
diff changeset
89 allocate(constructor%value, source=value)
kono
parents:
diff changeset
90 end function constructor
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 end module link_mod
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 module list_mod
kono
parents:
diff changeset
95 use link_mod
kono
parents:
diff changeset
96 private
kono
parents:
diff changeset
97 public :: list
kono
parents:
diff changeset
98 type list
kono
parents:
diff changeset
99 private
kono
parents:
diff changeset
100 class(link),pointer :: firstLink => null() ! first link in list
kono
parents:
diff changeset
101 class(link),pointer :: lastLink => null() ! last link in list
kono
parents:
diff changeset
102 contains
kono
parents:
diff changeset
103 procedure :: printValues ! print linked list
kono
parents:
diff changeset
104 procedure :: addInteger ! add integer to linked list
kono
parents:
diff changeset
105 procedure :: addChar ! add character to linked list
kono
parents:
diff changeset
106 procedure :: addReal ! add real to linked list
kono
parents:
diff changeset
107 procedure :: addValue ! add class(*) to linked list
kono
parents:
diff changeset
108 procedure :: firstValue ! return value associated with firstLink
kono
parents:
diff changeset
109 procedure :: isEmpty ! return true if list is empty
kono
parents:
diff changeset
110 generic :: add => addInteger, addChar, addReal
kono
parents:
diff changeset
111 end type list
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 contains
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 subroutine printValues(this)
kono
parents:
diff changeset
116 class(list) :: this
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 if (.not.this%isEmpty()) then
kono
parents:
diff changeset
119 call this%firstLink%printLinks()
kono
parents:
diff changeset
120 endif
kono
parents:
diff changeset
121 end subroutine printValues
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 subroutine addValue(this, value)
kono
parents:
diff changeset
124 class(list) :: this
kono
parents:
diff changeset
125 class(*) :: value
kono
parents:
diff changeset
126 class(link), pointer :: newLink
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 if (.not. associated(this%firstLink)) then
kono
parents:
diff changeset
129 this%firstLink => link(value, this%firstLink)
kono
parents:
diff changeset
130 this%lastLink => this%firstLink
kono
parents:
diff changeset
131 else
kono
parents:
diff changeset
132 newLink => link(value, this%lastLink%nextLink())
kono
parents:
diff changeset
133 call this%lastLink%setNextLink(newLink)
kono
parents:
diff changeset
134 this%lastLink => newLink
kono
parents:
diff changeset
135 end if
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 end subroutine addValue
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 subroutine addInteger(this, value)
kono
parents:
diff changeset
140 class(list) :: this
kono
parents:
diff changeset
141 integer value
kono
parents:
diff changeset
142 class(*), allocatable :: v
kono
parents:
diff changeset
143 allocate(v,source=value)
kono
parents:
diff changeset
144 call this%addValue(v)
kono
parents:
diff changeset
145 end subroutine addInteger
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 subroutine addChar(this, value)
kono
parents:
diff changeset
148 class(list) :: this
kono
parents:
diff changeset
149 character(*) :: value
kono
parents:
diff changeset
150 class(*), allocatable :: v
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 allocate(v,source=value)
kono
parents:
diff changeset
153 call this%addValue(v)
kono
parents:
diff changeset
154 end subroutine addChar
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 subroutine addReal(this, value)
kono
parents:
diff changeset
157 class(list) :: this
kono
parents:
diff changeset
158 real value
kono
parents:
diff changeset
159 class(*), allocatable :: v
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 allocate(v,source=value)
kono
parents:
diff changeset
162 call this%addValue(v)
kono
parents:
diff changeset
163 end subroutine addReal
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 function firstValue(this)
kono
parents:
diff changeset
166 class(list) :: this
kono
parents:
diff changeset
167 class(*), pointer :: firstValue
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 firstValue => this%firstLink%getValue()
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 end function firstValue
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 function isEmpty(this)
kono
parents:
diff changeset
174 class(list) :: this
kono
parents:
diff changeset
175 logical isEmpty
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 if (associated(this%firstLink)) then
kono
parents:
diff changeset
178 isEmpty = .false.
kono
parents:
diff changeset
179 else
kono
parents:
diff changeset
180 isEmpty = .true.
kono
parents:
diff changeset
181 endif
kono
parents:
diff changeset
182 end function isEmpty
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 end module list_mod
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 program main
kono
parents:
diff changeset
187 use link_mod, only : output
kono
parents:
diff changeset
188 use list_mod
kono
parents:
diff changeset
189 implicit none
kono
parents:
diff changeset
190 integer i, j
kono
parents:
diff changeset
191 type(list) :: my_list
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 do i=1, 10
kono
parents:
diff changeset
194 call my_list%add(i)
kono
parents:
diff changeset
195 enddo
kono
parents:
diff changeset
196 call my_list%add(1.23)
kono
parents:
diff changeset
197 call my_list%add('A')
kono
parents:
diff changeset
198 call my_list%add('BC')
kono
parents:
diff changeset
199 call my_list%add('DEF')
kono
parents:
diff changeset
200 call my_list%printvalues()
kono
parents:
diff changeset
201 do i = 1, 14
kono
parents:
diff changeset
202 select case (i)
kono
parents:
diff changeset
203 case (1:10)
kono
parents:
diff changeset
204 read (output(i), '(i6)') j
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 if (j .ne. i) STOP 1
111
kono
parents:
diff changeset
206 case (11)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
207 if (output(i) .ne. " 1.23") STOP 2
111
kono
parents:
diff changeset
208 case (12)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
209 if (output(i) .ne. " A") STOP 3
111
kono
parents:
diff changeset
210 case (13)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
211 if (output(i) .ne. " BC") STOP 4
111
kono
parents:
diff changeset
212 case (14)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
213 if (output(i) .ne. " DEF") STOP 5
111
kono
parents:
diff changeset
214 end select
kono
parents:
diff changeset
215 end do
kono
parents:
diff changeset
216 end program main