aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/gfortran.dg/select_type_4.f90
blob: 7e12d935447835a95bc8b02b6e4dc9fb10b0115e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
! { dg-do run }
!
! Contributed by by Richard Maine
! http://coding.derkeiler.com/Archive/Fortran/comp.lang.fortran/2006-10/msg00104.html
!
module poly_list 

  !--  Polymorphic lists using type extension. 

  implicit none 

  type, public :: node_type 
    private 
    class(node_type), pointer :: next => null() 
  end type node_type 

  type, public :: list_type 
    private 
    class(node_type), pointer :: head => null(), tail => null() 
  end type list_type 

contains 

  subroutine append_node (list, new_node) 

    !-- Append a node to a list. 
    !-- Caller is responsible for allocating the node. 

    !---------- interface. 

    type(list_type), intent(inout) :: list 
    class(node_type), target :: new_node 

    !---------- executable code. 

    if (.not.associated(list%head)) list%head => new_node 
    if (associated(list%tail)) list%tail%next => new_node 
    list%tail => new_node 
    return 
  end subroutine append_node 

  function first_node (list) 

    !-- Get the first node of a list. 

    !---------- interface. 

    type(list_type), intent(in) :: list 
    class(node_type), pointer :: first_node 

    !---------- executable code. 

    first_node => list%head 
    return 
  end function first_node 

  function next_node (node) 

    !-- Step to the next node of a list. 

    !---------- interface. 

    class(node_type), target :: node 
    class(node_type), pointer :: next_node 

    !---------- executable code. 

    next_node => node%next 
    return 
  end function next_node 

  subroutine destroy_list (list) 

    !-- Delete (and deallocate) all the nodes of a list. 

    !---------- interface. 
    type(list_type), intent(inout) :: list 

    !---------- local. 
    class(node_type), pointer :: node, next 

    !---------- executable code. 

    node => list%head 
    do while (associated(node)) 
      next => node%next 
      deallocate(node) 
      node => next 
    end do 
    nullify(list%head, list%tail) 
    return 
  end subroutine destroy_list 

end module poly_list 

program main 

  use poly_list 

  implicit none 
  integer :: cnt

  type, extends(node_type) :: real_node_type 
    real :: x 
  end type real_node_type 

  type, extends(node_type) :: integer_node_type 
    integer :: i 
  end type integer_node_type 

  type, extends(node_type) :: character_node_type 
    character(1) :: c 
  end type character_node_type 

  type(list_type) :: list 
  class(node_type), pointer :: node 
  type(integer_node_type), pointer :: integer_node 
  type(real_node_type), pointer :: real_node 
  type(character_node_type), pointer :: character_node 

  !---------- executable code. 

  !----- Build the list. 

  allocate(real_node) 
  real_node%x = 1.23 
  call append_node(list, real_node) 

  allocate(integer_node) 
  integer_node%i = 42 
  call append_node(list, integer_node) 

  allocate(node) 
  call append_node(list, node) 

  allocate(character_node) 
  character_node%c = "z" 
  call append_node(list, character_node) 

  allocate(real_node) 
  real_node%x = 4.56 
  call append_node(list, real_node) 

  !----- Retrieve from it. 

  node => first_node(list) 

  cnt = 0
  do while (associated(node)) 
    cnt = cnt + 1
    select type (node) 
      type is (real_node_type) 
        write (*,*) node%x
        if (.not.(     (cnt == 1 .and. node%x == 1.23)   &
                  .or. (cnt == 5 .and. node%x == 4.56))) then
          call abort()
        end if
      type is (integer_node_type) 
        write (*,*) node%i
        if (cnt /= 2 .or. node%i /= 42) call abort()
      type is (node_type) 
        write (*,*) "Node with no data."
        if (cnt /= 3) call abort()
      class default 
        Write (*,*) "Some other node type."
        if (cnt /= 4) call abort()
    end select 

    node => next_node(node) 
  end do 
  if (cnt /= 5) call abort()
  call destroy_list(list) 
  stop 
end program main