Clang Project

clang_source_code/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp
1// RUN: %clang_cc1 -std=c++11 -triple x86_64-none-linux-gnu -fmerge-all-constants -emit-llvm -o - %s | FileCheck -check-prefixes=X86,CHECK %s
2// RUN: %clang_cc1 -std=c++11 -triple amdgcn-amd-amdhsa -DNO_TLS -fmerge-all-constants -emit-llvm -o - %s | FileCheck -check-prefixes=AMDGCN,CHECK %s
3
4namespace std {
5  typedef decltype(sizeof(int)) size_t;
6
7  // libc++'s implementation
8  template <class _E>
9  class initializer_list
10  {
11    const _E* __begin_;
12    size_t    __size_;
13
14    initializer_list(const _E* __b, size_t __s)
15      : __begin_(__b),
16        __size_(__s)
17    {}
18
19  public:
20    typedef _E        value_type;
21    typedef const _E& reference;
22    typedef const _E& const_reference;
23    typedef size_t    size_type;
24
25    typedef const _E* iterator;
26    typedef const _E* const_iterator;
27
28    initializer_list() : __begin_(nullptr), __size_(0) {}
29
30    size_t    size()  const {return __size_;}
31    const _E* begin() const {return __begin_;}
32    const _E* end()   const {return __begin_ + __size_;}
33  };
34}
35
36struct destroyme1 {
37  ~destroyme1();
38};
39struct destroyme2 {
40  ~destroyme2();
41};
42struct witharg1 {
43  witharg1(const destroyme1&);
44  ~witharg1();
45};
46struct wantslist1 {
47  wantslist1(std::initializer_list<destroyme1>);
48  ~wantslist1();
49};
50// X86: @_ZGR15globalInitList1_ = internal constant [3 x i32] [i32 1, i32 2, i32 3]
51// X86: @globalInitList1 = global %{{[^ ]+}} { i32* getelementptr inbounds ([3 x i32], [3 x i32]* @_ZGR15globalInitList1_, i32 0, i32 0), i{{32|64}} 3 }
52// AMDGCN: @_ZGR15globalInitList1_ = internal addrspace(1) constant [3 x i32] [i32 1, i32 2, i32 3]
53// AMDGCN: @globalInitList1 = addrspace(1) global %{{[^ ]+}} { i32* addrspacecast (i32 addrspace(1)* getelementptr inbounds ([3 x i32], [3 x i32] addrspace(1)* @_ZGR15globalInitList1_, i32 0, i32 0) to i32*), i{{32|64}} 3 }
54std::initializer_list<int> globalInitList1 = {1, 2, 3};
55
56#ifndef NO_TLS
57namespace thread_local_global_array {
58// FIXME: We should be able to constant-evaluate this even though the
59// initializer is not a constant expression (pointers to thread_local
60// objects aren't really a problem).
61//
62// X86: @_ZN25thread_local_global_array1xE = thread_local global
63// X86: @_ZGRN25thread_local_global_array1xE_ = internal thread_local constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
64std::initializer_list<int> thread_local x = {1, 2, 3, 4};
65}
66#endif
67
68// X86: @globalInitList2 = global %{{[^ ]+}} zeroinitializer
69// X86: @_ZGR15globalInitList2_ = internal global [2 x %[[WITHARG:[^ ]*]]] zeroinitializer
70// AMDGCN: @globalInitList2 = addrspace(1) global %{{[^ ]+}} zeroinitializer
71// AMDGCN: @_ZGR15globalInitList2_ = internal addrspace(1) global [2 x %[[WITHARG:[^ ]*]]] zeroinitializer
72
73// X86: @_ZN15partly_constant1kE = global i32 0, align 4
74// X86: @_ZN15partly_constant2ilE = global {{.*}} null, align 8
75// X86: @[[PARTLY_CONSTANT_OUTER:_ZGRN15partly_constant2ilE_]] = internal global {{.*}} zeroinitializer, align 8
76// X86: @[[PARTLY_CONSTANT_INNER:_ZGRN15partly_constant2ilE0_]] = internal global [3 x {{.*}}] zeroinitializer, align 8
77// X86: @[[PARTLY_CONSTANT_FIRST:_ZGRN15partly_constant2ilE1_]] = internal constant [3 x i32] [i32 1, i32 2, i32 3], align 4
78// X86: @[[PARTLY_CONSTANT_SECOND:_ZGRN15partly_constant2ilE2_]] = internal global [2 x i32] zeroinitializer, align 4
79// X86: @[[PARTLY_CONSTANT_THIRD:_ZGRN15partly_constant2ilE3_]] = internal constant [4 x i32] [i32 5, i32 6, i32 7, i32 8], align 4
80// AMDGCN: @_ZN15partly_constant1kE = addrspace(1) global i32 0, align 4
81// AMDGCN: @_ZN15partly_constant2ilE = addrspace(4) global {{.*}} null, align 8
82// AMDGCN: @[[PARTLY_CONSTANT_OUTER:_ZGRN15partly_constant2ilE_]] = internal addrspace(4) global {{.*}} zeroinitializer, align 8
83// AMDGCN: @[[PARTLY_CONSTANT_INNER:_ZGRN15partly_constant2ilE0_]] = internal addrspace(4) global [3 x {{.*}}] zeroinitializer, align 8
84// AMDGCN: @[[PARTLY_CONSTANT_FIRST:_ZGRN15partly_constant2ilE1_]] = internal addrspace(4) constant [3 x i32] [i32 1, i32 2, i32 3], align 4
85// AMDGCN: @[[PARTLY_CONSTANT_SECOND:_ZGRN15partly_constant2ilE2_]] = internal addrspace(4) global [2 x i32] zeroinitializer, align 4
86// AMDGCN: @[[PARTLY_CONSTANT_THIRD:_ZGRN15partly_constant2ilE3_]] = internal addrspace(4) constant [4 x i32] [i32 5, i32 6, i32 7, i32 8], align 4
87
88// X86: @[[REFTMP1:.*]] = private constant [2 x i32] [i32 42, i32 43], align 4
89// X86: @[[REFTMP2:.*]] = private constant [3 x %{{.*}}] [%{{.*}} { i32 1 }, %{{.*}} { i32 2 }, %{{.*}} { i32 3 }], align 4
90// AMDGCN: @[[REFTMP1:.*]] = private addrspace(4) constant [2 x i32] [i32 42, i32 43], align 4
91// AMDGCN: @[[REFTMP2:.*]] = private addrspace(4) constant [3 x %{{.*}}] [%{{.*}} { i32 1 }, %{{.*}} { i32 2 }, %{{.*}} { i32 3 }], align 4
92
93// CHECK: appending global
94
95// thread_local initializer:
96// X86-LABEL: define internal void @__cxx_global_var_init
97// X86: store i32* getelementptr inbounds ([4 x i32], [4 x i32]* @_ZGRN25thread_local_global_array1xE_, i64 0, i64 0),
98// X86:       i32** getelementptr inbounds ({{.*}}, {{.*}}* @_ZN25thread_local_global_array1xE, i32 0, i32 0), align 8
99// X86: store i64 4, i64* getelementptr inbounds ({{.*}}, {{.*}}* @_ZN25thread_local_global_array1xE, i32 0, i32 1), align 8
100
101// CHECK-LABEL: define internal void @__cxx_global_var_init
102// X86: call void @_ZN8witharg1C1ERK10destroyme1(%[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* @_ZGR15globalInitList2_, i{{32|64}} 0, i{{32|64}} 0
103// X86: call void @_ZN8witharg1C1ERK10destroyme1(%[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* @_ZGR15globalInitList2_, i{{32|64}} 0, i{{32|64}} 1
104// AMDGCN: call void @_ZN8witharg1C1ERK10destroyme1(%[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}), i{{32|64}} 0, i{{32|64}} 0
105// AMDGCN: call void @_ZN8witharg1C1ERK10destroyme1(%[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}), i{{32|64}} 0, i{{32|64}} 1
106// CHECK: call i32 @__cxa_atexit
107// X86: store %[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* @_ZGR15globalInitList2_, i64 0, i64 0),
108// X86:       %[[WITHARG]]** getelementptr inbounds (%{{.*}}, %{{.*}}* @globalInitList2, i32 0, i32 0), align 8
109// X86: store i64 2, i64* getelementptr inbounds (%{{.*}}, %{{.*}}* @globalInitList2, i32 0, i32 1), align 8
110// AMDGCN: store %[[WITHARG]]* getelementptr inbounds ([2 x %[[WITHARG]]], [2 x %[[WITHARG]]]* addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}), i64 0, i64 0),
111// AMDGCN:       %[[WITHARG]]** getelementptr inbounds (%{{.*}}, %{{.*}}* addrspacecast ({{[^@]+}} @globalInitList2 {{[^)]+}}), i32 0, i32 0), align 8
112// AMDGCN: store i64 2, i64* getelementptr inbounds (%{{.*}}, %{{.*}}* addrspacecast ({{[^@]+}} @globalInitList2 {{[^)]+}}), i32 0, i32 1), align 8
113// CHECK: call void @_ZN10destroyme1D1Ev
114// CHECK-NEXT: call void @_ZN10destroyme1D1Ev
115// CHECK-NEXT: ret void
116std::initializer_list<witharg1> globalInitList2 = {
117  witharg1(destroyme1()), witharg1(destroyme1())
118};
119
120void fn1(int i) {
121  // CHECK-LABEL: define void @_Z3fn1i
122  // temporary array
123  // X86: [[array:%[^ ]+]] = alloca [3 x i32]
124  // AMDGCN: [[alloca:%[^ ]+]] = alloca [3 x i32], align 4, addrspace(5)
125  // AMDGCN: [[array:%[^ ]+]] = addrspacecast [3 x i32] addrspace(5)* [[alloca]] to [3 x i32]*
126  // CHECK: getelementptr inbounds [3 x i32], [3 x i32]* [[array]], i{{32|64}} 0
127  // CHECK-NEXT: store i32 1, i32*
128  // CHECK-NEXT: getelementptr
129  // CHECK-NEXT: store
130  // CHECK-NEXT: getelementptr
131  // CHECK-NEXT: load
132  // CHECK-NEXT: store
133  // init the list
134  // CHECK-NEXT: getelementptr
135  // CHECK-NEXT: getelementptr inbounds [3 x i32], [3 x i32]*
136  // CHECK-NEXT: store i32*
137  // CHECK-NEXT: getelementptr
138  // CHECK-NEXT: store i{{32|64}} 3
139  std::initializer_list<int> intlist{1, 2, i};
140}
141
142void fn2() {
143  // CHECK-LABEL: define void @_Z3fn2v
144  void target(std::initializer_list<destroyme1>);
145  // objects should be destroyed before dm2, after call returns
146  // CHECK: call void @_Z6targetSt16initializer_listI10destroyme1E
147  target({ destroyme1(), destroyme1() });
148  // CHECK: call void @_ZN10destroyme1D1Ev
149  destroyme2 dm2;
150  // CHECK: call void @_ZN10destroyme2D1Ev
151}
152
153void fn3() {
154  // CHECK-LABEL: define void @_Z3fn3v
155  // objects should be destroyed after dm2
156  auto list = { destroyme1(), destroyme1() };
157  destroyme2 dm2;
158  // CHECK: call void @_ZN10destroyme2D1Ev
159  // CHECK: call void @_ZN10destroyme1D1Ev
160}
161
162void fn4() {
163  // CHECK-LABEL: define void @_Z3fn4v
164  void target(std::initializer_list<witharg1>);
165  // objects should be destroyed before dm2, after call returns
166  // CHECK: call void @_ZN8witharg1C1ERK10destroyme1
167  // CHECK: call void @_Z6targetSt16initializer_listI8witharg1E
168  target({ witharg1(destroyme1()), witharg1(destroyme1()) });
169  // CHECK: call void @_ZN8witharg1D1Ev
170  // CHECK: call void @_ZN10destroyme1D1Ev
171  destroyme2 dm2;
172  // CHECK: call void @_ZN10destroyme2D1Ev
173}
174
175void fn5() {
176  // CHECK-LABEL: define void @_Z3fn5v
177  // temps should be destroyed before dm2
178  // objects should be destroyed after dm2
179  // CHECK: call void @_ZN8witharg1C1ERK10destroyme1
180  auto list = { witharg1(destroyme1()), witharg1(destroyme1()) };
181  // CHECK: call void @_ZN10destroyme1D1Ev
182  destroyme2 dm2;
183  // CHECK: call void @_ZN10destroyme2D1Ev
184  // CHECK: call void @_ZN8witharg1D1Ev
185}
186
187void fn6() {
188  // CHECK-LABEL: define void @_Z3fn6v
189  void target(const wantslist1&);
190  // objects should be destroyed before dm2, after call returns
191  // CHECK: call void @_ZN10wantslist1C1ESt16initializer_listI10destroyme1E
192  // CHECK: call void @_Z6targetRK10wantslist1
193  target({ destroyme1(), destroyme1() });
194  // CHECK: call void @_ZN10wantslist1D1Ev
195  // CHECK: call void @_ZN10destroyme1D1Ev
196  destroyme2 dm2;
197  // CHECK: call void @_ZN10destroyme2D1Ev
198}
199void fn7() {
200  // CHECK-LABEL: define void @_Z3fn7v
201  // temps should be destroyed before dm2
202  // object should be destroyed after dm2
203  // CHECK: call void @_ZN10wantslist1C1ESt16initializer_listI10destroyme1E
204  wantslist1 wl = { destroyme1(), destroyme1() };
205  // CHECK: call void @_ZN10destroyme1D1Ev
206  destroyme2 dm2;
207  // CHECK: call void @_ZN10destroyme2D1Ev
208  // CHECK: call void @_ZN10wantslist1D1Ev
209}
210
211void fn8() {
212  // CHECK-LABEL: define void @_Z3fn8v
213  void target(std::initializer_list<std::initializer_list<destroyme1>>);
214  // objects should be destroyed before dm2, after call returns
215  // CHECK: call void @_Z6targetSt16initializer_listIS_I10destroyme1EE
216  std::initializer_list<destroyme1> inner;
217  target({ inner, { destroyme1() } });
218  // CHECK: call void @_ZN10destroyme1D1Ev
219  // Only one destroy loop, since only one inner init list is directly inited.
220  // CHECK-NOT: call void @_ZN10destroyme1D1Ev
221  destroyme2 dm2;
222  // CHECK: call void @_ZN10destroyme2D1Ev
223}
224
225void fn9() {
226  // CHECK-LABEL: define void @_Z3fn9v
227  // objects should be destroyed after dm2
228  std::initializer_list<destroyme1> inner;
229  std::initializer_list<std::initializer_list<destroyme1>> list =
230      { inner, { destroyme1() } };
231  destroyme2 dm2;
232  // CHECK: call void @_ZN10destroyme2D1Ev
233  // CHECK: call void @_ZN10destroyme1D1Ev
234  // Only one destroy loop, since only one inner init list is directly inited.
235  // CHECK-NOT: call void @_ZN10destroyme1D1Ev
236  // CHECK: ret void
237}
238
239void fn10(int i) {
240  // CHECK-LABEL: define void @_Z4fn10i
241  // CHECK: alloca [3 x i32]
242  // CHECK: call i8* @_Znw{{[jm]}}
243  // CHECK: store i32 %
244  // CHECK: store i32 2
245  // CHECK: store i32 3
246  // CHECK: store i32*
247  (void) new std::initializer_list<int> {i, 2, 3};
248}
249
250void fn11() {
251  // CHECK-LABEL: define void @_Z4fn11v
252  (void) new std::initializer_list<destroyme1> {destroyme1(), destroyme1()};
253  // CHECK: call void @_ZN10destroyme1D1Ev
254  destroyme2 dm2;
255  // CHECK: call void @_ZN10destroyme2D1Ev
256}
257
258namespace PR12178 {
259  struct string {
260    string(int);
261    ~string();
262  };
263
264  struct pair {
265    string a;
266    int b;
267  };
268
269  struct map {
270    map(std::initializer_list<pair>);
271  };
272
273  map m{ {1, 2}, {3, 4} };
274}
275
276namespace rdar13325066 {
277  struct X { ~X(); };
278
279  // CHECK-LABEL: define void @_ZN12rdar133250664loopERNS_1XES1_
280  void loop(X &x1, X &x2) {
281    // CHECK: br label
282    // CHECK: br i1
283    // CHECK: br label
284    // CHECK: call void @_ZN12rdar133250661XD1Ev
285    // CHECK: br label
286    // CHECK: br label
287    // CHECK: call void @_ZN12rdar133250661XD1Ev
288    // CHECK: br i1
289    // CHECK: br label
290    // CHECK: ret void
291    for (X x : { x1, x2 }) { }
292  }
293}
294
295namespace dtors {
296  struct S {
297    S();
298    ~S();
299  };
300  void z();
301
302  // CHECK-LABEL: define void @_ZN5dtors1fEv(
303  void f() {
304    // CHECK: call void @_ZN5dtors1SC1Ev(
305    // CHECK: call void @_ZN5dtors1SC1Ev(
306    std::initializer_list<S>{ S(), S() };
307
308    // Destruction loop for underlying array.
309    // CHECK: br label
310    // CHECK: call void @_ZN5dtors1SD1Ev(
311    // CHECK: br i1
312
313    // CHECK: call void @_ZN5dtors1zEv(
314    z();
315
316    // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
317  }
318
319  // CHECK-LABEL: define void @_ZN5dtors1gEv(
320  void g() {
321    // CHECK: call void @_ZN5dtors1SC1Ev(
322    // CHECK: call void @_ZN5dtors1SC1Ev(
323    auto x = std::initializer_list<S>{ S(), S() };
324
325    // Destruction loop for underlying array.
326    // CHECK: br label
327    // CHECK: call void @_ZN5dtors1SD1Ev(
328    // CHECK: br i1
329
330    // CHECK: call void @_ZN5dtors1zEv(
331    z();
332
333    // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
334  }
335
336  // CHECK-LABEL: define void @_ZN5dtors1hEv(
337  void h() {
338    // CHECK: call void @_ZN5dtors1SC1Ev(
339    // CHECK: call void @_ZN5dtors1SC1Ev(
340    std::initializer_list<S> x = { S(), S() };
341
342    // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
343
344    // CHECK: call void @_ZN5dtors1zEv(
345    z();
346
347    // Destruction loop for underlying array.
348    // CHECK: br label
349    // CHECK: call void @_ZN5dtors1SD1Ev(
350    // CHECK: br i1
351  }
352}
353
354namespace partly_constant {
355  int k;
356  std::initializer_list<std::initializer_list<int>> &&il = { { 1, 2, 3 }, { 4, k }, { 5, 6, 7, 8 } };
357  // First init list.
358  // CHECK-NOT: @[[PARTLY_CONSTANT_FIRST]],
359  // CHECK: store i32* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_FIRST]]{{.*}}, i64 0, i64 0),
360  // CHECK:       i32** getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 0, i32 0)
361  // CHECK: store i64 3, i64* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 0, i32 1)
362  // CHECK-NOT: @[[PARTLY_CONSTANT_FIRST]],
363  //
364  // Second init list array (non-constant).
365  // CHECK: store i32 4, i32* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, i64 0, i64 0)
366  // CHECK: load i32, i32* {{.*}}@_ZN15partly_constant1kE
367  // CHECK: store i32 {{.*}}, i32* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, i64 0, i64 1)
368  //
369  // Second init list.
370  // CHECK: store i32* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, i64 0, i64 0),
371  // CHECK:       i32** getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 1, i32 0)
372  // CHECK: store i64 2, i64* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 1, i32 1)
373  //
374  // Third init list.
375  // CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
376  // CHECK: store i32* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_THIRD]]{{.*}}, i64 0, i64 0),
377  // CHECK:       i32** getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 2, i32 0)
378  // CHECK: store i64 4, i64* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 2, i32 1)
379  // CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
380  //
381  // Outer init list.
382  // CHECK: store {{.*}}* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 0, i64 0),
383  // CHECK:       {{.*}}** getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}, i32 0, i32 0)
384  // CHECK: store i64 3, i64* getelementptr inbounds ({{.*}}, {{.*}}* {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}, i32 0, i32 1)
385  //
386  // 'il' reference.
387  // CHECK: store {{.*}}* {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}, {{.*}}** {{.*}}@_ZN15partly_constant2ilE{{.*}}, align 8
388}
389namespace nested {
390  struct A { A(); ~A(); };
391  struct B { const A &a; ~B(); };
392  struct C { std::initializer_list<B> b; ~C(); };
393  void f();
394  // CHECK-LABEL: define void @_ZN6nested1gEv(
395  void g() {
396    // CHECK: call void @_ZN6nested1AC1Ev(
397    // CHECK-NOT: call
398    // CHECK: call void @_ZN6nested1AC1Ev(
399    // CHECK-NOT: call
400    const C &c { { { A() }, { A() } } };
401
402    // CHECK: call void @_ZN6nested1fEv(
403    // CHECK-NOT: call
404    f();
405
406    // CHECK: call void @_ZN6nested1CD1Ev(
407    // CHECK-NOT: call
408
409    // Destroy B[2] array.
410    // FIXME: This isn't technically correct: reverse construction order would
411    // destroy the second B then the second A then the first B then the first A.
412    // CHECK: call void @_ZN6nested1BD1Ev(
413    // CHECK-NOT: call
414    // CHECK: br
415
416    // CHECK-NOT: call
417    // CHECK: call void @_ZN6nested1AD1Ev(
418    // CHECK-NOT: call
419    // CHECK: call void @_ZN6nested1AD1Ev(
420    // CHECK-NOT: call
421    // CHECK: }
422  }
423}
424
425namespace DR1070 {
426  struct A {
427    A(std::initializer_list<int>);
428  };
429  struct B {
430    int i;
431    A a;
432  };
433  B b = {1};
434  struct C {
435    std::initializer_list<int> a;
436    B b;
437    std::initializer_list<double> c;
438  };
439  C c = {};
440}
441
442namespace ArrayOfInitList {
443  struct S {
444    S(std::initializer_list<int>);
445  };
446  S x[1] = {};
447}
448
449namespace PR20445 {
450  struct vector { vector(std::initializer_list<int>); };
451  struct MyClass { explicit MyClass(const vector &v); };
452  template<int x> void f() { new MyClass({42, 43}); }
453  template void f<0>();
454  // CHECK-LABEL: define {{.*}} @_ZN7PR204451fILi0EEEvv(
455  // CHECK: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* {{.*}}@[[REFTMP1]]{{.*}}, i64 0, i64 0)
456  // CHECK: call void @_ZN7PR204456vectorC1ESt16initializer_listIiE(
457  // CHECK: call void @_ZN7PR204457MyClassC1ERKNS_6vectorE(
458}
459
460namespace ConstExpr {
461  class C {
462    int x;
463  public:
464    constexpr C(int x) : x(x) {}
465  };
466  void f(std::initializer_list<C>);
467  void g() {
468    // CHECK-LABEL: _ZN9ConstExpr1gEv
469    // CHECK: store %"class.ConstExpr::C"* getelementptr inbounds ([3 x %"class.ConstExpr::C"], [3 x %"class.ConstExpr::C"]* {{.*}}@[[REFTMP2]]{{.*}}, i64 0, i64 0)
470    // CHECK: call void @_ZN9ConstExpr1fESt16initializer_listINS_1CEE
471    f({C(1), C(2), C(3)});
472  }
473}
474
475namespace B19773010 {
476  template <class T1, class T2> struct pair {
477    T1 first;
478    T2 second;
479    constexpr pair() : first(), second() {}
480    constexpr pair(T1 a, T2 b) : first(a), second(b) {}
481  };
482
483  enum E { ENUM_CONSTANT };
484  struct testcase {
485    testcase(std::initializer_list<pair<const char *, E>>);
486  };
487  void f1() {
488    // CHECK-LABEL: @_ZN9B197730102f1Ev
489    testcase a{{"", ENUM_CONSTANT}};
490    // X86: store %"struct.B19773010::pair"* getelementptr inbounds ([1 x %"struct.B19773010::pair"], [1 x %"struct.B19773010::pair"]* bitcast ([1 x { i8*, i32 }]* @.ref.tmp{{.*}} to [1 x %"struct.B19773010::pair"]*), i64 0, i64 0), %"struct.B19773010::pair"** %{{.*}}, align 8
491    // AMDGCN: store %"struct.B19773010::pair"* getelementptr inbounds ([1 x %"struct.B19773010::pair"], [1 x %"struct.B19773010::pair"]* addrspacecast{{.*}} bitcast ([1 x { i8*, i32 }] addrspace(4)* @.ref.tmp{{.*}} to [1 x %"struct.B19773010::pair"] addrspace(4)*){{.*}}, i64 0, i64 0), %"struct.B19773010::pair"** %{{.*}}, align 8
492  }
493  void f2() {
494    // CHECK-LABEL: @_ZN9B197730102f2Ev
495    // X86: store %"struct.B19773010::pair"* getelementptr inbounds ([1 x %"struct.B19773010::pair"], [1 x %"struct.B19773010::pair"]* bitcast ([1 x { i8*, i32 }]* @_ZGRZN9B197730102f2EvE1p_ to [1 x %"struct.B19773010::pair"]*), i64 0, i64 0), %"struct.B19773010::pair"** getelementptr inbounds ([2 x %"class.std::initializer_list.10"], [2 x %"class.std::initializer_list.10"]* @_ZZN9B197730102f2EvE1p, i64 0, i64 1, i32 0), align 16
496    // AMDGCN: store %"struct.B19773010::pair"* getelementptr inbounds ([1 x %"struct.B19773010::pair"], [1 x %"struct.B19773010::pair"]* addrspacecast{{.*}} bitcast ([1 x { i8*, i32 }] addrspace(1)* @_ZGRZN9B197730102f2EvE1p_ to [1 x %"struct.B19773010::pair"] addrspace(1)*){{.*}}, i64 0, i64 0), %"struct.B19773010::pair"** getelementptr inbounds ([2 x %"class.std::initializer_list.10"], [2 x %"class.std::initializer_list.10"]* addrspacecast{{.*}}@_ZZN9B197730102f2EvE1p{{.*}}, i64 0, i64 1, i32 0), align 8
497    static std::initializer_list<pair<const char *, E>> a, p[2] =
498        {a, {{"", ENUM_CONSTANT}}};
499  }
500
501  void PR22940_helper(const pair<void*, int>&) { }
502  void PR22940() {
503    // CHECK-LABEL: @_ZN9B197730107PR22940Ev
504    // CHECK: call {{.*}} @_ZN9B197730104pairIPviEC{{.}}Ev(
505    // CHECK: call {{.*}} @_ZN9B1977301014PR22940_helperERKNS_4pairIPviEE(
506    PR22940_helper(pair<void*, int>());
507  }
508}
509