1// RUN: %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s -std=c++11 | FileCheck %s
2
3// FIXME: The padding in all these objects should be zero-initialized.
4namespace StructUnion {
5  struct A {
6    int n;
7    double d;
8    union U {
9      constexpr U(int x) : x(x) {}
10      constexpr U(const char *y) : y(y) {}
11      int x;
12      const char *y;
13    } u;
14
15    constexpr A(int n, double d, int x) : n(n), d(d), u(x) {}
16    constexpr A(int n, double d, const char *y) : n(n), d(d), u(y) {}
17  };
18
19  // CHECK: @_ZN11StructUnion1aE = constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } }
20  extern constexpr A a(1, 2.0, 3);
21
22  // CHECK: @_ZN11StructUnion1bE = constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { i8* getelementptr inbounds ([6 x i8]* @{{.*}}, i32 0, i32 0) } }
23  extern constexpr A b(4, 5, "hello");
24
25  struct B {
26    int n;
27  };
28
29  // CHECK: @_ZN11StructUnion1cE = global {{.*}} zeroinitializer
30  // CHECK: @_ZN11StructUnion2c2E = global {{.*}} zeroinitializer
31  B c;
32  B c2 = B();
33
34  // CHECK: @_ZN11StructUnion1dE = global {{.*}} zeroinitializer
35  B d[10];
36
37  struct C {
38    constexpr C() : c(0) {}
39    int c;
40  };
41
42  // CHECK: @_ZN11StructUnion1eE = global {{.*}} zeroinitializer
43  C e[10];
44
45  struct D {
46    constexpr D() : d(5) {}
47    int d;
48  };
49
50  // CHECK: @_ZN11StructUnion1fE = global {{.*}} { i32 5 }
51  D f;
52}
53
54namespace BaseClass {
55  template<typename T, unsigned> struct X : T {};
56  struct C { char c = 1; };
57  template<unsigned... Ns> struct Cs : X<C,Ns>... {};
58  struct N { int n = 3; };
59  struct D { double d = 4.0; };
60
61  template<typename ...Ts>
62  struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; };
63
64  using Test1 = Test<N, C, Cs<1,2>, D, X<C,1>>;
65  // CHECK: @_ZN9BaseClass2t1E = constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8
66  extern constexpr Test1 t1 = Test1();
67
68  struct DN : D, N {};
69  struct DND : DN, X<D,0> {};
70  struct DNN : DN, X<N,0> {};
71  // CHECK: @_ZN9BaseClass3dndE = constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 }
72  extern constexpr DND dnd = DND();
73  // Note, N subobject is laid out in DN subobject's tail padding.
74  // CHECK: @_ZN9BaseClass3dnnE = constant {{.*}} { double 4.000000e+00, i32 3, i32 3 }
75  extern constexpr DNN dnn = DNN();
76
77  struct E {};
78  struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};
79  // CHECK: @_ZN9BaseClass2t2E = constant {{.*}} undef
80  extern constexpr Test2 t2 = Test2();
81
82  struct __attribute((packed)) PackedD { double y = 2; };
83  struct Test3 : C, PackedD { constexpr Test3() {} };
84  // CHECK: @_ZN9BaseClass2t3E = constant <{ i8, double }> <{ i8 1, double 2.000000e+00 }>
85  extern constexpr Test3 t3 = Test3();
86}
87
88namespace Array {
89  // CHECK: @_ZN5Array3arrE = constant [2 x i32] [i32 4, i32 0]
90  extern constexpr int arr[2] = { 4 };
91
92  // CHECK: @_ZN5Array1cE = constant [6 x [4 x i8]] [{{.*}} c"foo\00", [4 x i8] c"a\00\00\00", [4 x i8] c"bar\00", [4 x i8] c"xyz\00", [4 x i8] c"b\00\00\00", [4 x i8] c"123\00"]
93  extern constexpr char c[6][4] = { "foo", "a", { "bar" }, { 'x', 'y', 'z' }, { "b" }, '1', '2', '3' };
94
95  // CHECK: @_ZN5Array2ucE = constant [4 x i8] c"foo\00"
96  extern constexpr unsigned char uc[] = { "foo" };
97
98  struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; };
99  // CHECK: @_ZN5Array5ctorsE = constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }]
100  extern const C ctors[3];
101  constexpr C ctors[3];
102
103  // CHECK: @_ZN5Array1dE = constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] }
104  struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 };
105
106  struct E {
107    char c[4];
108    char d[4];
109    constexpr E() : c("foo"), d("x") {}
110  };
111  // CHECK: @_ZN5Array1eE = constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }
112  extern constexpr E e = E();
113}
114
115namespace MemberPtr {
116  struct B1 {
117    int a, b;
118    virtual void f();
119    void g();
120  };
121  struct B2 {
122    int c, d;
123    virtual void h();
124    void i();
125  };
126  struct C : B1 {
127    int e;
128    virtual void j();
129    void k();
130  };
131  struct D : C, B2 {
132    int z;
133    virtual void l();
134    void m();
135  };
136
137  // CHECK: @_ZN9MemberPtr2daE = constant i64 8
138  // CHECK: @_ZN9MemberPtr2dbE = constant i64 12
139  // CHECK: @_ZN9MemberPtr2dcE = constant i64 32
140  // CHECK: @_ZN9MemberPtr2ddE = constant i64 36
141  // CHECK: @_ZN9MemberPtr2deE = constant i64 16
142  // CHECK: @_ZN9MemberPtr2dzE = constant i64 40
143  extern constexpr int (D::*da) = &B1::a;
144  extern constexpr int (D::*db) = &C::b;
145  extern constexpr int (D::*dc) = &B2::c;
146  extern constexpr int (D::*dd) = &D::d;
147  extern constexpr int (D::*de) = &C::e;
148  extern constexpr int (D::*dz) = &D::z;
149
150  // CHECK: @_ZN9MemberPtr2baE = constant i64 8
151  // CHECK: @_ZN9MemberPtr2bbE = constant i64 12
152  // CHECK: @_ZN9MemberPtr2bcE = constant i64 8
153  // CHECK: @_ZN9MemberPtr2bdE = constant i64 12
154  // CHECK: @_ZN9MemberPtr2beE = constant i64 16
155  // CHECK: @_ZN9MemberPtr3b1zE = constant i64 40
156  // CHECK: @_ZN9MemberPtr3b2zE = constant i64 16
157  extern constexpr int (B1::*ba) = (int(B1::*))&B1::a;
158  extern constexpr int (B1::*bb) = (int(B1::*))&C::b;
159  extern constexpr int (B2::*bc) = (int(B2::*))&B2::c;
160  extern constexpr int (B2::*bd) = (int(B2::*))&D::d;
161  extern constexpr int (B1::*be) = (int(B1::*))&C::e;
162  extern constexpr int (B1::*b1z) = (int(B1::*))&D::z;
163  extern constexpr int (B2::*b2z) = (int(B2::*))&D::z;
164
165  // CHECK: @_ZN9MemberPtr2dfE = constant {{.*}} { i64 1, i64 0 }
166  // CHECK: @_ZN9MemberPtr2dgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
167  // CHECK: @_ZN9MemberPtr2dhE = constant {{.*}} { i64 1, i64 24 }
168  // CHECK: @_ZN9MemberPtr2diE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 }
169  // CHECK: @_ZN9MemberPtr2djE = constant {{.*}} { i64 9, i64 0 }
170  // CHECK: @_ZN9MemberPtr2dkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
171  // CHECK: @_ZN9MemberPtr2dlE = constant {{.*}} { i64 17, i64 0 }
172  // CHECK: @_ZN9MemberPtr2dmE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
173  extern constexpr void (D::*df)() = &C::f;
174  extern constexpr void (D::*dg)() = &B1::g;
175  extern constexpr void (D::*dh)() = &B2::h;
176  extern constexpr void (D::*di)() = &D::i;
177  extern constexpr void (D::*dj)() = &C::j;
178  extern constexpr void (D::*dk)() = &C::k;
179  extern constexpr void (D::*dl)() = &D::l;
180  extern constexpr void (D::*dm)() = &D::m;
181
182  // CHECK: @_ZN9MemberPtr2bfE = constant {{.*}} { i64 1, i64 0 }
183  // CHECK: @_ZN9MemberPtr2bgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
184  // CHECK: @_ZN9MemberPtr2bhE = constant {{.*}} { i64 1, i64 0 }
185  // CHECK: @_ZN9MemberPtr2biE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 }
186  // CHECK: @_ZN9MemberPtr2bjE = constant {{.*}} { i64 9, i64 0 }
187  // CHECK: @_ZN9MemberPtr2bkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
188  // CHECK: @_ZN9MemberPtr3b1lE = constant {{.*}} { i64 17, i64 0 }
189  // CHECK: @_ZN9MemberPtr3b1mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
190  // CHECK: @_ZN9MemberPtr3b2lE = constant {{.*}} { i64 17, i64 -24 }
191  // CHECK: @_ZN9MemberPtr3b2mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 }
192  extern constexpr void (B1::*bf)()  = (void(B1::*)())&C::f;
193  extern constexpr void (B1::*bg)()  = (void(B1::*)())&B1::g;
194  extern constexpr void (B2::*bh)()  = (void(B2::*)())&B2::h;
195  extern constexpr void (B2::*bi)()  = (void(B2::*)())&D::i;
196  extern constexpr void (B1::*bj)()  = (void(B1::*)())&C::j;
197  extern constexpr void (B1::*bk)()  = (void(B1::*)())&C::k;
198  extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l;
199  extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m;
200  extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l;
201  extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m;
202}
203
204namespace LiteralReference {
205  struct Lit {
206    constexpr Lit() : n(5) {}
207    int n;
208  };
209  // FIXME: This should have static initialization, but we do not implement
210  // that yet. For now, just check that we don't set the (pointer) value of
211  // the reference to 5!
212  //
213  // CHECK: @_ZN16LiteralReference3litE = global {{.*}} null
214  const Lit &lit = Lit();
215}
216
217namespace NonLiteralConstexpr {
218  constexpr int factorial(int n) {
219    return n ? factorial(n-1) * n : 1;
220  }
221  extern void f(int *p);
222
223  struct NonTrivialDtor {
224    constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}
225    ~NonTrivialDtor() {
226      f(p);
227    }
228
229    int n;
230    int *p;
231  };
232  static_assert(!__is_literal(NonTrivialDtor), "");
233  // CHECK: @_ZN19NonLiteralConstexpr3ntdE = global {{.*}} { i32 120, i32* getelementptr
234  NonTrivialDtor ntd;
235
236  struct VolatileMember {
237    constexpr VolatileMember() : n(5) {}
238    volatile int n;
239  };
240  static_assert(!__is_literal(VolatileMember), "");
241  // CHECK: @_ZN19NonLiteralConstexpr2vmE = global {{.*}} { i32 5 }
242  VolatileMember vm;
243
244  struct Both {
245    constexpr Both() : n(10) {}
246    ~Both();
247    volatile int n;
248  };
249  // CHECK: @_ZN19NonLiteralConstexpr1bE = global {{.*}} { i32 10 }
250  Both b;
251
252  void StaticVars() {
253    // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, i32* getelementptr {{.*}}
254    // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd =
255    static NonTrivialDtor ntd;
256    // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 }
257    // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm =
258    static VolatileMember vm;
259    // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 }
260    // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b =
261    static Both b;
262  }
263}
264
265// PR12067
266namespace VirtualMembers {
267  struct A {
268    constexpr A(double d) : d(d) {}
269    virtual void f();
270    double d;
271  };
272  struct B : A {
273    constexpr B() : A(2.0), c{'h', 'e', 'l', 'l', 'o'} {}
274    constexpr B(int n) : A(n), c{'w', 'o', 'r', 'l', 'd'} {}
275    virtual void g();
276    char c[5];
277  };
278  struct C {
279    constexpr C() : n(64) {}
280    int n;
281  };
282  struct D : C, A, B {
283    constexpr D() : A(1.0), B(), s(5) {}
284    short s;
285  };
286  struct E : D, B {
287    constexpr E() : B(3), c{'b','y','e'} {}
288    char c[3];
289  };
290
291  // CHECK: @_ZN14VirtualMembers1eE = global { i8**, double, i32, i8**, double, [5 x i8], i16, i8**, double, [5 x i8], [3 x i8] } { i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 2), double 1.000000e+00, i32 64, i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 5), double 2.000000e+00, [5 x i8] c"hello", i16 5, i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 9), double 3.000000e+00, [5 x i8] c"world", [3 x i8] c"bye" }
292  E e;
293
294  struct nsMemoryImpl {
295    virtual void f();
296  };
297  // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global { i8** } { i8** getelementptr inbounds ([3 x i8*]* @_ZTVN14VirtualMembers12nsMemoryImplE, i64 0, i64 2) }
298  static nsMemoryImpl sGlobalMemory;
299}
300
301// Constant initialization tests go before this point,
302// dynamic initialization tests go after.
303
304// We must emit a constant initializer for NonLiteralConstexpr::ntd, but also
305// emit an initializer to register its destructor.
306// CHECK: define {{.*}}cxx_global_var_init{{.*}}
307// CHECK-NOT: NonLiteralConstexpr
308// CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev {{.*}} @_ZN19NonLiteralConstexpr3ntdE
309// CHECK-NEXT: ret void
310
311// We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.
312// CHECK-NOT: NonLiteralConstexpr2vm
313
314// We must emit a constant initializer for NonLiteralConstexpr::b, but also
315// emit an initializer to register its destructor.
316// CHECK: define {{.*}}cxx_global_var_init{{.*}}
317// CHECK-NOT: NonLiteralConstexpr
318// CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr4BothD1Ev {{.*}} @_ZN19NonLiteralConstexpr1bE
319// CHECK-NEXT: ret void
320
321// CHECK: define {{.*}}NonLiteralConstexpr10StaticVars
322// CHECK-NOT: }
323// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev
324// CHECK-NOT: }
325// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev
326
327namespace CrossFuncLabelDiff {
328  // Make sure we refuse to constant-fold the variable b.
329  constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); }
330  void test() { static long b = (long)&&lbl - a(false); lbl: return; }
331  // CHECK: sub nsw i64 ptrtoint (i8* blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64),
332  // CHECK: store i64 {{.*}}, i64* @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8
333}
334
335// PR12012
336namespace VirtualBase {
337  struct B {};
338  struct D : virtual B {};
339  D d;
340  // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev
341
342  template<typename T> struct X : T {
343    constexpr X() : T() {}
344  };
345  X<D> x;
346  // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev
347}
348
349// PR12145
350namespace Unreferenced {
351  int n;
352  constexpr int *p = &n;
353  // We must not emit a load of 'p' here, since it's not odr-used.
354  int q = *p;
355  // CHECK-NOT: _ZN12Unreferenced1pE
356  // CHECK: = load i32* @_ZN12Unreferenced1nE
357  // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN12Unreferenced1qE
358  // CHECK-NOT: _ZN12Unreferenced1pE
359
360  // Technically, we are not required to substitute variables of reference types
361  // initialized by constant expressions, because the special case for odr-use
362  // of variables in [basic.def.odr]p2 only applies to objects. But we do so
363  // anyway.
364
365  constexpr int &r = n;
366  // CHECK-NOT: _ZN12Unreferenced1rE
367  int s = r;
368
369  const int t = 1;
370  const int &rt = t;
371  int f(int);
372  int u = f(rt);
373  // CHECK: call i32 @_ZN12Unreferenced1fEi(i32 1)
374}
375
376namespace InitFromConst {
377  template<typename T> void consume(T);
378
379  const bool b = true;
380  const int n = 5;
381  constexpr double d = 4.3;
382
383  struct S { int n = 7; S *p = 0; };
384  constexpr S s = S();
385  const S &r = s;
386  constexpr const S *p = &r;
387  constexpr int S::*mp = &S::n;
388  constexpr int a[3] = { 1, 4, 9 };
389
390  void test() {
391    // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 zeroext true)
392    consume(b);
393
394    // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 5)
395    consume(n);
396
397    // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double 4.300000e+00)
398    consume(d);
399
400    // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
401    consume<const S&>(s);
402
403    // FIXME CHECK-NOT: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
404    // There's no lvalue-to-rvalue conversion here, so 'r' is odr-used, and
405    // we're permitted to emit a load of it. This seems likely to be a defect
406    // in the standard. If we start emitting a direct reference to 's', update
407    // this test.
408    consume<const S&>(r);
409
410    // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
411    consume(p);
412
413    // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)
414    consume(mp);
415
416    // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(i32* getelementptr inbounds ([3 x i32]* @_ZN13InitFromConstL1aE, i32 0, i32 0))
417    consume(a);
418  }
419}
420
421namespace Null {
422  decltype(nullptr) null();
423  // CHECK: call {{.*}} @_ZN4Null4nullEv(
424  int *p = null();
425  struct S {};
426  // CHECK: call {{.*}} @_ZN4Null4nullEv(
427  int S::*q = null();
428}
429