1 ! Test default initialization of global variables (static init)
2 ! RUN: bbc %s -o - | FileCheck %s
5 real, target
:: ziel(100)
15 ! Test type that combines all kinds of components with and without
16 ! default initialization.
18 ! Simple type component with default init
20 ! Simple type component without default init
23 ! Pointer component with a default initial target
24 real, pointer :: x(:) => ziel
25 ! Pointer component with no init
27 ! Pointer component with null init
28 real, pointer :: z(:) => NULL()
30 ! Character component with init
31 character(10) c1
= "hello"
32 ! Character component without init
35 ! Component with a derived type that has default init
37 ! Component with a derived type that has no default init.
38 type(tno_init
) :: sometno_init
39 ! Component whose type default init is overridden by
40 ! default init for the component.
41 type(t0
) :: somet0_2
= t0(33)
42 ! Array component with a derived type that has default init
43 type(t0
) :: somet0_array
46 ! Test type that extends type with default init.
47 type, extends(t0
) :: textendst0
51 ! Test type with default init in equivalences
62 real, pointer :: mv(:)
64 ! Test scalar with default init
66 ! CHECK-LABEL: fir.global @_QMtinitEat0 : !fir.type<_QMtinitTt0{k:i32}> {
67 ! CHECK: %[[VAL_0:.*]] = arith.constant 66 : i32
68 ! CHECK: %[[VAL_1:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
69 ! CHECK: %[[VAL_2:.*]] = fir.insert_value %[[VAL_1]], %[[VAL_0]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
70 ! CHECK: fir.has_value %[[VAL_2]] : !fir.type<_QMtinitTt0{k:i32}>
72 ! Test array with default init
74 ! CHECK-LABEL: @_QMtinitEbt0 : !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>> {
75 ! CHECK: %[[VAL_3:.*]] = arith.constant 66 : i32
76 ! CHECK: %[[VAL_4:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
77 ! CHECK: %[[VAL_5:.*]] = fir.insert_value %[[VAL_4]], %[[VAL_3]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
78 ! CHECK: %[[VAL_6:.*]] = fir.undefined !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
79 ! CHECK: %[[VAL_7:.*]] = fir.insert_on_range %[[VAL_6]], %[[VAL_5]] from (0) to (99) : (!fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
80 ! CHECK: fir.has_value %[[VAL_7]] : !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
82 ! Test default init overridden by explicit init
83 type(t0
) :: ct0
= t0(42)
84 ! CHECK-LABEL: fir.global @_QMtinitEct0 : !fir.type<_QMtinitTt0{k:i32}> {
85 ! CHECK: %[[VAL_8:.*]] = arith.constant 42 : i32
86 ! CHECK: %[[VAL_9:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
87 ! CHECK: %[[VAL_10:.*]] = fir.insert_value %[[VAL_9]], %[[VAL_8]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
88 ! CHECK: fir.has_value %[[VAL_10]] : !fir.type<_QMtinitTt0{k:i32}>
90 ! Test a non trivial derived type mixing all sorts of default initialization
92 ! CHECK-LABEL: @_QMtinitEdt1 : !fir.type<_QMtinitTt1{{.*}}> {
93 ! CHECK-DAG: %[[VAL_11:.*]] = arith.constant 42 : i32
94 ! CHECK-DAG: %[[VAL_12:.*]] = arith.constant 100 : index
95 ! CHECK-DAG: %[[VAL_13:.*]] = arith.constant 0 : index
96 ! CHECK-DAG: %[[VAL_14:.*]] = arith.constant 33 : i32
97 ! CHECK-DAG: %[[VAL_15:.*]] = arith.constant 66 : i32
98 ! CHECK: %[[VAL_16:.*]] = fir.undefined !fir.type<_QMtinitTt1{{.*}}>
99 ! CHECK: %[[VAL_17:.*]] = fir.insert_value %[[VAL_16]], %[[VAL_11]], ["i", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, i32) -> !fir.type<_QMtinitTt1{{.*}}>
100 ! CHECK: %[[VAL_18:.*]] = fir.undefined i32
101 ! CHECK: %[[VAL_19:.*]] = fir.insert_value %[[VAL_17]], %[[VAL_18]], ["j", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, i32) -> !fir.type<_QMtinitTt1{{.*}}>
102 ! CHECK: %[[VAL_20:.*]] = fir.address_of(@_QMtinitEziel) : !fir.ref<!fir.array<100xf32>>
103 ! CHECK: %[[VAL_21:.*]] = fir.shape %[[VAL_12]] : (index) -> !fir.shape<1>
104 ! CHECK: %[[VAL_22:.*]] = fir.embox %[[VAL_20]](%[[VAL_21]]) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.box<!fir.array<100xf32>>
105 ! CHECK: %[[VAL_22_B:.*]] = fir.rebox %[[VAL_22]] : (!fir.box<!fir.array<100xf32>>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
106 ! CHECK: %[[VAL_23:.*]] = fir.insert_value %[[VAL_19]], %[[VAL_22_B]], ["x", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
107 ! CHECK: %[[VAL_24:.*]] = fir.zero_bits !fir.ptr<!fir.array<?xf32>>
108 ! CHECK: %[[VAL_25:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1>
109 ! CHECK: %[[VAL_26:.*]] = fir.embox %[[VAL_24]](%[[VAL_25]]) : (!fir.ptr<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
110 ! CHECK: %[[VAL_27:.*]] = fir.insert_value %[[VAL_23]], %[[VAL_26]], ["y", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
111 ! CHECK: %[[VAL_28:.*]] = fir.insert_value %[[VAL_27]], %[[VAL_26]], ["z", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
112 ! CHECK: %[[VAL_29:.*]] = fir.string_lit "hello "(10) : !fir.char<1,10>
113 ! CHECK: %[[VAL_30:.*]] = fir.insert_value %[[VAL_28]], %[[VAL_29]], ["c1", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.char<1,10>) -> !fir.type<_QMtinitTt1{{.*}}>
114 ! CHECK: %[[VAL_31:.*]] = fir.undefined !fir.char<1,10>
115 ! CHECK: %[[VAL_32:.*]] = fir.insert_value %[[VAL_30]], %[[VAL_31]], ["c2", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.char<1,10>) -> !fir.type<_QMtinitTt1{{.*}}>
116 ! CHECK: %[[VAL_33:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
117 ! CHECK: %[[VAL_34:.*]] = fir.insert_value %[[VAL_33]], %[[VAL_15]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
118 ! CHECK: %[[VAL_35:.*]] = fir.insert_value %[[VAL_32]], %[[VAL_34]], ["somet0", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
119 ! CHECK: %[[VAL_36:.*]] = fir.undefined !fir.type<_QMtinitTtno_init{k:i32}>
120 ! CHECK: %[[VAL_37:.*]] = fir.insert_value %[[VAL_35]], %[[VAL_36]], ["sometno_init", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTtno_init{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
121 ! CHECK: %[[VAL_38:.*]] = fir.insert_value %[[VAL_33]], %[[VAL_14]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
122 ! CHECK: %[[VAL_39:.*]] = fir.insert_value %[[VAL_37]], %[[VAL_38]], ["somet0_2", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
123 ! CHECK: %[[VAL_40:.*]] = fir.insert_value %[[VAL_39]], %[[VAL_34]], ["somet0_array", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
124 ! CHECK: fir.has_value %[[VAL_40]] : !fir.type<_QMtinitTt1{{.*}}>
126 ! Test a type extending other type with a default init
127 type(textendst0
) :: etextendst0
128 ! CHECK-LABEL: @_QMtinitEetextendst0 : !fir.type<_QMtinitTtextendst0{k:i32,l:i32}> {
129 ! CHECK: %[[VAL_42:.*]] = arith.constant 66 : i32
130 ! CHECK: %[[VAL_43:.*]] = fir.undefined !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
131 ! CHECK: %[[VAL_44:.*]] = fir.insert_value %[[VAL_43]], %[[VAL_42]], ["k", !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>] : (!fir.type<_QMtinitTtextendst0{k:i32,l:i32}>, i32) -> !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
132 ! CHECK: %[[VAL_45:.*]] = fir.undefined i32
133 ! CHECK: %[[VAL_46:.*]] = fir.insert_value %[[VAL_44]], %[[VAL_45]], ["l", !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>] : (!fir.type<_QMtinitTtextendst0{k:i32,l:i32}>, i32) -> !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
134 ! CHECK: fir.has_value %[[VAL_46]] : !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
136 type(tv
) :: withmold
= tv(null(mv
))
137 ! CHECK-LABEL: fir.global @_QMtinitEwithmold
138 ! CHECK: %[[C0:.*]] = arith.constant 0 : index
139 ! CHECK: %[[UNDEF:.*]] = fir.undefined !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
140 ! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.ptr<!fir.array<?xf32>>
141 ! CHECK: %[[SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
142 ! CHECK: %[[ZEROBOX:.*]] = fir.embox %[[ZERO]](%[[SHAPE]]) : (!fir.ptr<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
143 ! CHECK: %[[RET:.*]] = fir.insert_value %[[UNDEF]], %[[ZEROBOX]], ["v", !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>] : (!fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
144 ! CHECK: fir.has_value %[[RET]] : !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
149 ! Test that default initialization is also applied to saved variables
152 type(t0
), save :: savedt0
153 ! CHECK-LABEL: fir.global internal @_QFsavedEsavedt0 : !fir.type<_QMtinitTt0{k:i32}> {
154 ! CHECK: %[[VAL_47:.*]] = arith.constant 66 : i32
155 ! CHECK: %[[VAL_48:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
156 ! CHECK: %[[VAL_49:.*]] = fir.insert_value %[[VAL_48]], %[[VAL_47]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
157 ! CHECK: fir.has_value %[[VAL_49]] : !fir.type<_QMtinitTt0{k:i32}>
160 ! Test default initialization in equivalences
163 type(tseq
), save :: somet
165 equivalence (somet
, i
)
166 ! CHECK-LABEL: fir.global internal @_QFeqvEi : !fir.array<2xi32> {
167 ! CHECK-DAG: %[[VAL_50:.*]] = arith.constant 2 : i32
168 ! CHECK-DAG: %[[VAL_51:.*]] = arith.constant 3 : i32
169 ! CHECK: %[[VAL_52:.*]] = fir.undefined !fir.array<2xi32>
170 ! CHECK: %[[VAL_53:.*]] = fir.insert_value %[[VAL_52]], %[[VAL_50]], [0 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
171 ! CHECK: %[[VAL_54:.*]] = fir.insert_value %[[VAL_53]], %[[VAL_51]], [1 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
172 ! CHECK: fir.has_value %[[VAL_54]] : !fir.array<2xi32>
175 subroutine eqv_explicit_init()
177 type(tseq
), save :: somet
178 integer :: i(2) = [4, 5]
179 equivalence (somet
, i
)
180 ! CHECK-LABEL: fir.global internal @_QFeqv_explicit_initEi : !fir.array<2xi32> {
181 ! CHECK-DAG: %[[VAL_57:.*]] = arith.constant 4 : i32
182 ! CHECK-DAG: %[[VAL_58:.*]] = arith.constant 5 : i32
183 ! CHECK: %[[VAL_59:.*]] = fir.undefined !fir.array<2xi32>
184 ! CHECK: %[[VAL_60:.*]] = fir.insert_value %[[VAL_59]], %[[VAL_57]], [0 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
185 ! CHECK: %[[VAL_61:.*]] = fir.insert_value %[[VAL_60]], %[[VAL_58]], [1 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
186 ! CHECK: fir.has_value %[[VAL_61]] : !fir.array<2xi32>
189 subroutine eqv_same_default_init()
191 type(tseq
), save :: somet1(2), somet2
192 equivalence (somet1(1), somet2
)
193 ! CHECK-LABEL: fir.global internal @_QFeqv_same_default_initEsomet1 : !fir.array<2xi64> {
194 ! CHECK: %[[VAL_62:.*]] = arith.constant 12884901890 : i64
195 ! CHECK: %[[VAL_63:.*]] = fir.undefined !fir.array<2xi64>
196 ! CHECK: %[[VAL_64:.*]] = fir.insert_on_range %[[VAL_63]], %[[VAL_62]] from (0) to (1) : (!fir.array<2xi64>, i64) -> !fir.array<2xi64>
197 ! CHECK: fir.has_value %[[VAL_64]] : !fir.array<2xi64>
200 subroutine eqv_full_overlaps_with_explicit_init()
202 type(tseq
), save :: somet
203 integer, save :: link(4)
204 integer :: i(2) = [5, 6]
205 integer :: j(2) = [7, 8]
206 ! Equivalence: somet fully covered by explicit init.
207 ! i(1)=5 | i(2)=6 | - | -
208 ! - | somet%i | somet%j |
209 ! - | - | j(1)=7 | j(2)=8
210 equivalence (i
, link(1))
211 equivalence (somet
, link(2))
212 equivalence (j
, link(3))
213 ! CHECK-LABEL: fir.global internal @_QFeqv_full_overlaps_with_explicit_initEi : !fir.array<4xi32> {
214 ! CHECK-DAG: %[[VAL_73:.*]] = arith.constant 5 : i32
215 ! CHECK-DAG: %[[VAL_74:.*]] = arith.constant 6 : i32
216 ! CHECK-DAG: %[[VAL_75:.*]] = arith.constant 7 : i32
217 ! CHECK-DAG: %[[VAL_76:.*]] = arith.constant 8 : i32
218 ! CHECK-DAG: %[[VAL_77:.*]] = fir.undefined !fir.array<4xi32>
219 ! CHECK-DAG: %[[VAL_78:.*]] = fir.insert_value %[[VAL_77]], %[[VAL_73]], [0 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
220 ! CHECK-DAG: %[[VAL_79:.*]] = fir.insert_value %[[VAL_78]], %[[VAL_74]], [1 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
221 ! CHECK-DAG: %[[VAL_80:.*]] = fir.insert_value %[[VAL_79]], %[[VAL_75]], [2 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
222 ! CHECK-DAG: %[[VAL_81:.*]] = fir.insert_value %[[VAL_80]], %[[VAL_76]], [3 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
223 ! CHECK-DAG: fir.has_value %[[VAL_81]] : !fir.array<4xi32>
226 subroutine eqv_partial_overlaps_with_explicit_init()
228 type(tseq
), save :: somet
229 integer, save :: link(4)
230 integer :: i(2) = [5, 6]
232 ! `somet` is only partially covered by explicit init, somet%j default
233 ! init value should be used in the equiv storage init to match nvfortran,
234 ! ifort, and nagfor behavior (gfortran refuses this code). 19.5.3.4 point
235 ! 10 specifies that explicit initialization overrides default initialization.
236 ! i(1)=5 | i(2)=6 | - | -
237 ! - | somet%i | somet%j |
239 equivalence (i
, link(1))
240 equivalence (somet
, link(2))
241 equivalence (j
, link(4))
242 ! CHECK-LABEL: fir.global internal @_QFeqv_partial_overlaps_with_explicit_initEi : !fir.array<4xi32>
243 ! CHECK-DAG: %[[VAL_82:.*]] = arith.constant 5 : i32
244 ! CHECK-DAG: %[[VAL_83:.*]] = arith.constant 6 : i32
245 ! CHECK-DAG: %[[VAL_84:.*]] = arith.constant 3 : i32
246 ! CHECK-DAG: %[[VAL_85:.*]] = arith.constant 7 : i32
247 ! CHECK: %[[VAL_86:.*]] = fir.undefined !fir.array<4xi32>
248 ! CHECK: %[[VAL_87:.*]] = fir.insert_value %[[VAL_86]], %[[VAL_82]], [0 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
249 ! CHECK: %[[VAL_88:.*]] = fir.insert_value %[[VAL_87]], %[[VAL_83]], [1 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
250 ! CHECK: %[[VAL_89:.*]] = fir.insert_value %[[VAL_88]], %[[VAL_84]], [2 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
251 ! CHECK: %[[VAL_90:.*]] = fir.insert_value %[[VAL_89]], %[[VAL_85]], [3 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
252 ! CHECK: fir.has_value %[[VAL_90]] : !fir.array<4xi32>