blob: c6250c6d96686bc0b42dd0d41cd1e001f5359190 [file] [log] [blame]
Eli Friedman5930a4c2012-01-05 23:59:40 +00001// RUN: %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s -std=c++11 | FileCheck %s
2
Richard Smith2d6a5672012-01-14 04:30:29 +00003// 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
Richard Smithe15c7122012-02-17 04:54:50 +000019 // CHECK: @_ZN11StructUnion1aE = constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } }
Richard Smith2d6a5672012-01-14 04:30:29 +000020 extern constexpr A a(1, 2.0, 3);
21
Richard Smithe15c7122012-02-17 04:54:50 +000022 // CHECK: @_ZN11StructUnion1bE = constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { i8* getelementptr inbounds ([6 x i8]* @{{.*}}, i32 0, i32 0) } }
Richard Smith2d6a5672012-01-14 04:30:29 +000023 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>>;
Richard Smithe15c7122012-02-17 04:54:50 +000065 // CHECK: @_ZN9BaseClass2t1E = constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8
Richard Smith2d6a5672012-01-14 04:30:29 +000066 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> {};
Richard Smithe15c7122012-02-17 04:54:50 +000071 // CHECK: @_ZN9BaseClass3dndE = constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 }
Richard Smith2d6a5672012-01-14 04:30:29 +000072 extern constexpr DND dnd = DND();
73 // Note, N subobject is laid out in DN subobject's tail padding.
Richard Smithe15c7122012-02-17 04:54:50 +000074 // CHECK: @_ZN9BaseClass3dnnE = constant {{.*}} { double 4.000000e+00, i32 3, i32 3 }
Richard Smith2d6a5672012-01-14 04:30:29 +000075 extern constexpr DNN dnn = DNN();
76
77 struct E {};
78 struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};
Richard Smithe15c7122012-02-17 04:54:50 +000079 // CHECK: @_ZN9BaseClass2t2E = constant {{.*}} undef
Richard Smith2d6a5672012-01-14 04:30:29 +000080 extern constexpr Test2 t2 = Test2();
Eli Friedman5fe61c62012-03-30 03:55:31 +000081
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();
Richard Smith2d6a5672012-01-14 04:30:29 +000086}
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 struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; };
Richard Smithe15c7122012-02-17 04:54:50 +000096 // CHECK: @_ZN5Array5ctorsE = constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }]
Richard Smith2d6a5672012-01-14 04:30:29 +000097 extern const C ctors[3];
98 constexpr C ctors[3];
99
100 // CHECK: @_ZN5Array1dE = constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] }
101 struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 };
Richard Smithf3908f22012-02-17 03:35:37 +0000102
103 struct E {
104 char c[4];
105 char d[4];
106 constexpr E() : c("foo"), d("x") {}
107 };
Richard Smithe15c7122012-02-17 04:54:50 +0000108 // CHECK: @_ZN5Array1eE = constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }
Richard Smithf3908f22012-02-17 03:35:37 +0000109 extern constexpr E e = E();
Richard Smith2d6a5672012-01-14 04:30:29 +0000110}
111
112namespace MemberPtr {
113 struct B1 {
114 int a, b;
115 virtual void f();
116 void g();
117 };
118 struct B2 {
119 int c, d;
120 virtual void h();
121 void i();
122 };
123 struct C : B1 {
124 int e;
125 virtual void j();
126 void k();
127 };
128 struct D : C, B2 {
129 int z;
130 virtual void l();
131 void m();
132 };
133
134 // CHECK: @_ZN9MemberPtr2daE = constant i64 8
135 // CHECK: @_ZN9MemberPtr2dbE = constant i64 12
136 // CHECK: @_ZN9MemberPtr2dcE = constant i64 32
137 // CHECK: @_ZN9MemberPtr2ddE = constant i64 36
138 // CHECK: @_ZN9MemberPtr2deE = constant i64 16
139 // CHECK: @_ZN9MemberPtr2dzE = constant i64 40
140 extern constexpr int (D::*da) = &B1::a;
141 extern constexpr int (D::*db) = &C::b;
142 extern constexpr int (D::*dc) = &B2::c;
143 extern constexpr int (D::*dd) = &D::d;
144 extern constexpr int (D::*de) = &C::e;
145 extern constexpr int (D::*dz) = &D::z;
146
147 // CHECK: @_ZN9MemberPtr2baE = constant i64 8
148 // CHECK: @_ZN9MemberPtr2bbE = constant i64 12
149 // CHECK: @_ZN9MemberPtr2bcE = constant i64 8
150 // CHECK: @_ZN9MemberPtr2bdE = constant i64 12
151 // CHECK: @_ZN9MemberPtr2beE = constant i64 16
152 // CHECK: @_ZN9MemberPtr3b1zE = constant i64 40
153 // CHECK: @_ZN9MemberPtr3b2zE = constant i64 16
154 extern constexpr int (B1::*ba) = (int(B1::*))&B1::a;
155 extern constexpr int (B1::*bb) = (int(B1::*))&C::b;
156 extern constexpr int (B2::*bc) = (int(B2::*))&B2::c;
157 extern constexpr int (B2::*bd) = (int(B2::*))&D::d;
158 extern constexpr int (B1::*be) = (int(B1::*))&C::e;
159 extern constexpr int (B1::*b1z) = (int(B1::*))&D::z;
160 extern constexpr int (B2::*b2z) = (int(B2::*))&D::z;
161
162 // CHECK: @_ZN9MemberPtr2dfE = constant {{.*}} { i64 1, i64 0 }
163 // CHECK: @_ZN9MemberPtr2dgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
164 // CHECK: @_ZN9MemberPtr2dhE = constant {{.*}} { i64 1, i64 24 }
165 // CHECK: @_ZN9MemberPtr2diE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 }
166 // CHECK: @_ZN9MemberPtr2djE = constant {{.*}} { i64 9, i64 0 }
167 // CHECK: @_ZN9MemberPtr2dkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
168 // CHECK: @_ZN9MemberPtr2dlE = constant {{.*}} { i64 17, i64 0 }
169 // CHECK: @_ZN9MemberPtr2dmE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
170 extern constexpr void (D::*df)() = &C::f;
171 extern constexpr void (D::*dg)() = &B1::g;
172 extern constexpr void (D::*dh)() = &B2::h;
173 extern constexpr void (D::*di)() = &D::i;
174 extern constexpr void (D::*dj)() = &C::j;
175 extern constexpr void (D::*dk)() = &C::k;
176 extern constexpr void (D::*dl)() = &D::l;
177 extern constexpr void (D::*dm)() = &D::m;
178
179 // CHECK: @_ZN9MemberPtr2bfE = constant {{.*}} { i64 1, i64 0 }
180 // CHECK: @_ZN9MemberPtr2bgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
181 // CHECK: @_ZN9MemberPtr2bhE = constant {{.*}} { i64 1, i64 0 }
182 // CHECK: @_ZN9MemberPtr2biE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 }
183 // CHECK: @_ZN9MemberPtr2bjE = constant {{.*}} { i64 9, i64 0 }
184 // CHECK: @_ZN9MemberPtr2bkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
185 // CHECK: @_ZN9MemberPtr3b1lE = constant {{.*}} { i64 17, i64 0 }
186 // CHECK: @_ZN9MemberPtr3b1mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
187 // CHECK: @_ZN9MemberPtr3b2lE = constant {{.*}} { i64 17, i64 -24 }
188 // CHECK: @_ZN9MemberPtr3b2mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 }
189 extern constexpr void (B1::*bf)() = (void(B1::*)())&C::f;
190 extern constexpr void (B1::*bg)() = (void(B1::*)())&B1::g;
191 extern constexpr void (B2::*bh)() = (void(B2::*)())&B2::h;
192 extern constexpr void (B2::*bi)() = (void(B2::*)())&D::i;
193 extern constexpr void (B1::*bj)() = (void(B1::*)())&C::j;
194 extern constexpr void (B1::*bk)() = (void(B1::*)())&C::k;
195 extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l;
196 extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m;
197 extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l;
198 extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m;
199}
200
Richard Smith7ca48502012-02-13 22:16:19 +0000201namespace LiteralReference {
202 struct Lit {
203 constexpr Lit() : n(5) {}
204 int n;
205 };
206 // FIXME: This should have static initialization, but we do not implement
207 // that yet. For now, just check that we don't set the (pointer) value of
208 // the reference to 5!
209 //
210 // CHECK: @_ZN16LiteralReference3litE = global {{.*}} null
211 const Lit &lit = Lit();
212}
213
214namespace NonLiteralConstexpr {
215 constexpr int factorial(int n) {
216 return n ? factorial(n-1) * n : 1;
217 }
218 extern void f(int *p);
219
220 struct NonTrivialDtor {
221 constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}
222 ~NonTrivialDtor() {
223 f(p);
224 }
225
226 int n;
227 int *p;
228 };
229 static_assert(!__is_literal(NonTrivialDtor), "");
230 // CHECK: @_ZN19NonLiteralConstexpr3ntdE = global {{.*}} { i32 120, i32* getelementptr
231 NonTrivialDtor ntd;
232
233 struct VolatileMember {
234 constexpr VolatileMember() : n(5) {}
235 volatile int n;
236 };
237 static_assert(!__is_literal(VolatileMember), "");
238 // CHECK: @_ZN19NonLiteralConstexpr2vmE = global {{.*}} { i32 5 }
239 VolatileMember vm;
240
241 struct Both {
242 constexpr Both() : n(10) {}
243 ~Both();
244 volatile int n;
245 };
246 // CHECK: @_ZN19NonLiteralConstexpr1bE = global {{.*}} { i32 10 }
247 Both b;
248
249 void StaticVars() {
250 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, i32* getelementptr {{.*}}
251 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd =
252 static NonTrivialDtor ntd;
253 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 }
254 // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm =
255 static VolatileMember vm;
256 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 }
257 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b =
258 static Both b;
259 }
260}
261
Richard Smithc22adbd2012-02-23 08:33:23 +0000262// PR12067
263namespace VirtualMembers {
264 struct A {
265 constexpr A(double d) : d(d) {}
266 virtual void f();
267 double d;
268 };
269 struct B : A {
270 constexpr B() : A(2.0), c{'h', 'e', 'l', 'l', 'o'} {}
271 constexpr B(int n) : A(n), c{'w', 'o', 'r', 'l', 'd'} {}
272 virtual void g();
273 char c[5];
274 };
275 struct C {
276 constexpr C() : n(64) {}
277 int n;
278 };
279 struct D : C, A, B {
280 constexpr D() : A(1.0), B(), s(5) {}
281 short s;
282 };
283 struct E : D, B {
284 constexpr E() : B(3), c{'b','y','e'} {}
285 char c[3];
286 };
287
288 // 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" }
289 E e;
290
291 struct nsMemoryImpl {
292 virtual void f();
293 };
294 // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global { i8** } { i8** getelementptr inbounds ([3 x i8*]* @_ZTVN14VirtualMembers12nsMemoryImplE, i64 0, i64 2) }
295 static nsMemoryImpl sGlobalMemory;
296}
297
Richard Smith2d6a5672012-01-14 04:30:29 +0000298// Constant initialization tests go before this point,
299// dynamic initialization tests go after.
300
Richard Smith7ca48502012-02-13 22:16:19 +0000301// We must emit a constant initializer for NonLiteralConstexpr::ntd, but also
302// emit an initializer to register its destructor.
303// CHECK: define {{.*}}cxx_global_var_init{{.*}}
304// CHECK-NOT: NonLiteralConstexpr
305// CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev {{.*}} @_ZN19NonLiteralConstexpr3ntdE
306// CHECK-NEXT: ret void
307
308// We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.
309// CHECK-NOT: NonLiteralConstexpr2vm
310
311// We must emit a constant initializer for NonLiteralConstexpr::b, but also
312// emit an initializer to register its destructor.
313// CHECK: define {{.*}}cxx_global_var_init{{.*}}
314// CHECK-NOT: NonLiteralConstexpr
315// CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr4BothD1Ev {{.*}} @_ZN19NonLiteralConstexpr1bE
316// CHECK-NEXT: ret void
317
318// CHECK: define {{.*}}NonLiteralConstexpr10StaticVars
319// CHECK-NOT: }
320// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev
321// CHECK-NOT: }
322// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev
323
Eli Friedman5930a4c2012-01-05 23:59:40 +0000324namespace CrossFuncLabelDiff {
325 // Make sure we refuse to constant-fold the variable b.
Richard Smith745f5142012-01-27 01:14:48 +0000326 constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); }
327 void test() { static long b = (long)&&lbl - a(false); lbl: return; }
Eli Friedman5930a4c2012-01-05 23:59:40 +0000328 // CHECK: sub nsw i64 ptrtoint (i8* blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64),
329 // CHECK: store i64 {{.*}}, i64* @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8
330}
Richard Smithce582fe2012-02-17 00:44:16 +0000331
332// PR12012
333namespace VirtualBase {
334 struct B {};
335 struct D : virtual B {};
336 D d;
337 // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev
338
339 template<typename T> struct X : T {
340 constexpr X() : T() {}
341 };
342 X<D> x;
343 // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev
344}
Richard Smitha3ca41f2012-03-02 23:27:11 +0000345
346// PR12145
347namespace Unreferenced {
348 int n;
349 constexpr int *p = &n;
350 // We must not emit a load of 'p' here, since it's not odr-used.
351 int q = *p;
352 // CHECK-NOT: _ZN12Unreferenced1pE
353 // CHECK: = load i32* @_ZN12Unreferenced1nE
354 // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN12Unreferenced1qE
355 // CHECK-NOT: _ZN12Unreferenced1pE
356
357 // Technically, we are not required to substitute variables of reference types
358 // initialized by constant expressions, because the special case for odr-use
359 // of variables in [basic.def.odr]p2 only applies to objects. But we do so
360 // anyway.
361
362 constexpr int &r = n;
363 // CHECK-NOT: _ZN12Unreferenced1rE
364 int s = r;
365
366 const int t = 1;
367 const int &rt = t;
368 int f(int);
369 int u = f(rt);
370 // CHECK: call i32 @_ZN12Unreferenced1fEi(i32 1)
371}
372
373namespace InitFromConst {
374 template<typename T> void consume(T);
375
376 const bool b = true;
377 const int n = 5;
Richard Smith946e2722012-03-07 01:58:44 +0000378 constexpr double d = 4.3;
Richard Smitha3ca41f2012-03-02 23:27:11 +0000379
380 struct S { int n = 7; S *p = 0; };
381 constexpr S s = S();
382 const S &r = s;
383 constexpr const S *p = &r;
384 constexpr int S::*mp = &S::n;
385 constexpr int a[3] = { 1, 4, 9 };
386
387 void test() {
388 // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 zeroext true)
389 consume(b);
390
391 // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 5)
392 consume(n);
393
394 // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double 4.300000e+00)
395 consume(d);
396
397 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
398 consume<const S&>(s);
399
400 // FIXME CHECK-NOT: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
401 // There's no lvalue-to-rvalue conversion here, so 'r' is odr-used, and
402 // we're permitted to emit a load of it. This seems likely to be a defect
403 // in the standard. If we start emitting a direct reference to 's', update
404 // this test.
405 consume<const S&>(r);
406
407 // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
408 consume(p);
409
410 // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)
411 consume(mp);
412
413 // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(i32* getelementptr inbounds ([3 x i32]* @_ZN13InitFromConstL1aE, i32 0, i32 0))
414 consume(a);
415 }
416}