[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / AST / ByteCode / builtin-bit-cast.cpp
blob60e8c3a615c5e63e542928ff8a7c3daa5e8db21d
1 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only %s
2 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s
3 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple powerpc64le-unknown-unknown -mabi=ieeelongdouble %s
4 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple powerpc64-unknown-unknown -mabi=ieeelongdouble %s
6 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter %s
7 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu -fexperimental-new-constant-interpreter %s
8 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter -triple powerpc64le-unknown-unknown -mabi=ieeelongdouble %s
9 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter -triple powerpc64-unknown-unknown -mabi=ieeelongdouble %s
11 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
12 # define LITTLE_END 1
13 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
14 # define LITTLE_END 0
15 #else
16 # error "huh?"
17 #endif
19 typedef decltype(nullptr) nullptr_t;
20 typedef __INTPTR_TYPE__ intptr_t;
22 static_assert(sizeof(int) == 4);
23 static_assert(sizeof(long long) == 8);
25 template <class To, class From>
26 constexpr To bit_cast(const From &from) {
27 static_assert(sizeof(To) == sizeof(From));
28 return __builtin_bit_cast(To, from);
31 template <class Intermediate, class Init>
32 constexpr bool check_round_trip(const Init &init) {
33 return bit_cast<Init>(bit_cast<Intermediate>(init)) == init;
36 template <class Intermediate, class Init>
37 constexpr Init round_trip(const Init &init) {
38 return bit_cast<Init>(bit_cast<Intermediate>(init));
42 namespace Discarding {
43 struct S { int a; };
44 constexpr int f = (__builtin_bit_cast(int, 2), 0);
45 constexpr int f2 = (__builtin_bit_cast(S, 2), 0);
48 namespace std {
49 enum byte : unsigned char {};
50 } // namespace std
52 using uint8_t = unsigned char;
54 template<int N>
55 struct bytes {
56 using size_t = unsigned int;
57 unsigned char d[N];
59 constexpr unsigned char &operator[](size_t index) {
60 if (index < N)
61 return d[index];
66 template <int N, typename T = unsigned char, int Pad = 0>
67 struct bits {
68 T : Pad;
69 T bits : N;
71 constexpr bool operator==(const T& rhs) const {
72 return bits == rhs;
76 template <int N, typename T, int P>
77 constexpr bool operator==(const struct bits<N, T, P>& lhs, const struct bits<N, T, P>& rhs) {
78 return lhs.bits == rhs.bits;
81 #ifdef __SIZEOF_INT128__
82 static_assert(check_round_trip<__int128_t>((__int128_t)34));
83 static_assert(check_round_trip<__int128_t>((__int128_t)-34));
85 constexpr unsigned char OneBit[] = {
86 0x1, 0x0, 0x0, 0x0,
87 0x0, 0x0, 0x0, 0x0,
88 0x0, 0x0, 0x0, 0x0,
89 0x0, 0x0, 0x0, 0x0,
91 constexpr __int128_t One = 1;
92 constexpr __int128_t Expected = One << 120;
93 static_assert(__builtin_bit_cast(__int128_t, OneBit) == (LITTLE_END ? 1 : Expected));
95 #endif
97 static_assert(check_round_trip<double>(17.0));
100 namespace simple {
101 constexpr int A = __builtin_bit_cast(int, 10);
102 static_assert(A == 10);
104 static_assert(__builtin_bit_cast(unsigned, 1.0F) == 1065353216);
106 struct Bytes {
107 char a, b, c, d;
109 constexpr unsigned B = __builtin_bit_cast(unsigned, Bytes{10, 12, 13, 14});
110 static_assert(B == (LITTLE_END ? 235736074 : 168561934));
113 constexpr unsigned C = __builtin_bit_cast(unsigned, (_BitInt(32))12);
114 static_assert(C == 12);
116 struct BitInts {
117 _BitInt(16) a;
118 _BitInt(16) b;
120 constexpr unsigned D = __builtin_bit_cast(unsigned, BitInts{12, 13});
121 static_assert(D == (LITTLE_END ? 851980 : 786445));
125 static_assert(__builtin_bit_cast(char, true) == 1);
127 static_assert(check_round_trip<unsigned>((int)-1));
128 static_assert(check_round_trip<unsigned>((int)0x12345678));
129 static_assert(check_round_trip<unsigned>((int)0x87654321));
130 static_assert(check_round_trip<unsigned>((int)0x0C05FEFE));
131 static_assert(round_trip<float>((int)0x0C05FEFE));
134 /// This works in GCC and in the bytecode interpreter, but the current interpreter
135 /// diagnoses it.
136 static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \
137 // ref-note {{indeterminate value can only initialize an object}}
140 namespace Fail {
141 constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \
142 // both-note {{division by zero}} \
143 // both-note {{declared here}}
144 constexpr int b = __builtin_bit_cast(int, a); // both-error {{must be initialized by a constant expression}} \
145 // both-note {{initializer of 'a' is not a constant expression}}
148 namespace NullPtr {
149 constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
150 static_assert(N == nullptr);
151 static_assert(__builtin_bit_cast(nullptr_t, (_BitInt(sizeof(void*) * 8))12) == __builtin_bit_cast(nullptr_t, (unsigned _BitInt(sizeof(void*) * 8))0));
152 static_assert(__builtin_bit_cast(nullptr_t, nullptr) == nullptr);
155 namespace bitint {
156 constexpr _BitInt(sizeof(int) * 8) BI = ~0;
157 constexpr unsigned int I = __builtin_bit_cast(unsigned int, BI);
158 static_assert(I == ~0u, "");
160 constexpr _BitInt(sizeof(int) * 8) IB = __builtin_bit_cast(_BitInt(sizeof(int) * 8), I); // ref-error {{must be initialized by a constant expression}} \
161 // ref-note {{constexpr bit cast involving type '_BitInt(32)' is not yet supported}} \
162 // ref-note {{declared here}}
163 static_assert(IB == ~0u, ""); // ref-error {{not an integral constant expression}} \
164 // ref-note {{initializer of 'IB' is not a constant expression}}
167 namespace BitFields {
168 struct BitFields {
169 unsigned a : 2;
170 unsigned b : 30;
173 constexpr unsigned A = __builtin_bit_cast(unsigned, BitFields{3, 16}); // ref-error {{must be initialized by a constant expression}} \
174 // ref-note {{not yet supported}} \
175 // ref-note {{declared here}}
176 static_assert(A == (LITTLE_END ? 67 : 3221225488)); // ref-error {{not an integral constant expression}} \
177 // ref-note {{initializer of 'A'}}
180 void bitfield_indeterminate() {
181 struct BF { unsigned char z : 2; };
182 enum byte : unsigned char {};
184 constexpr BF bf = {0x3};
185 /// Requires bitcasts to composite types.
186 // static_assert(bit_cast<bits<2>>(bf).bits == bf.z);
187 // static_assert(bit_cast<unsigned char>(bf));
189 #if 0
190 // static_assert(__builtin_bit_cast(byte, bf));
192 struct M {
193 // expected-note@+1 {{subobject declared here}}
194 unsigned char mem[sizeof(BF)];
196 // expected-error@+2 {{initialized by a constant expression}}
197 // expected-note@+1 {{not initialized}}
198 constexpr M m = bit_cast<M>(bf);
200 constexpr auto f = []() constexpr {
201 // bits<24, unsigned int, LITTLE_END ? 0 : 8> B = {0xc0ffee};
202 constexpr struct { unsigned short b1; unsigned char b0; } B = {0xc0ff, 0xee};
203 return bit_cast<bytes<4>>(B);
206 static_assert(f()[0] + f()[1] + f()[2] == 0xc0 + 0xff + 0xee);
208 // expected-error@+2 {{initialized by a constant expression}}
209 // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
210 constexpr auto _bad = f()[3];
213 struct B {
214 unsigned short s0 : 8;
215 unsigned short s1 : 8;
216 std::byte b0 : 4;
217 std::byte b1 : 4;
218 std::byte b2 : 4;
220 constexpr auto g = [f]() constexpr {
221 return bit_cast<B>(f());
223 static_assert(g().s0 + g().s1 + g().b0 + g().b1 == 0xc0 + 0xff + 0xe + 0xe);
225 // expected-error@+2 {{initialized by a constant expression}}
226 // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
227 constexpr auto _bad = g().b2;
229 #endif
233 namespace Classes {
234 class A {
235 public:
236 char a[2];
238 class B : public A {
239 public:
240 char b[2];
242 static_assert(__builtin_bit_cast(int, B{{0, 0},{0, 0}}) == 0);
243 static_assert(__builtin_bit_cast(int, B{{13, 0},{0, 0}}) == (LITTLE_END ? 13 : 218103808));
244 static_assert(__builtin_bit_cast(int, B{{13, 7},{12, 20}}) == (LITTLE_END ? 336332557 : 218565652));
246 class Ref {
247 public:
248 const int &a;
249 constexpr Ref(const int &a) : a(a) {}
251 constexpr int I = 12;
253 typedef __INTPTR_TYPE__ intptr_t;
254 static_assert(__builtin_bit_cast(intptr_t, Ref{I}) == 0); // both-error {{not an integral constant expression}} \
255 // both-note {{bit_cast from a type with a reference member is not allowed in a constant expression}}
257 class C : public A {
258 public:
259 constexpr C() : A{1,2} {}
260 virtual constexpr int get() {
261 return 4;
264 static_assert(__builtin_bit_cast(_BitInt(sizeof(C) * 8), C()) == 0); // both-error {{source type must be trivially copyable}}
267 class D : virtual A {};
268 static_assert(__builtin_bit_cast(_BitInt(sizeof(D) * 8), D()) == 0); // both-error {{source type must be trivially copyable}}
270 class F {
271 public:
272 char f[2];
275 class E : public A, public F {
276 public:
277 constexpr E() : A{1,2}, F{3,4}, e{5,6,7,8} {}
278 char e[4];
280 static_assert(__builtin_bit_cast(long long, E()) == (LITTLE_END ? 578437695752307201 : 72623859790382856));
283 struct int_splicer {
284 unsigned x;
285 unsigned y;
287 constexpr int_splicer() : x(1), y(2) {}
288 constexpr int_splicer(unsigned x, unsigned y) : x(x), y(y) {}
290 constexpr bool operator==(const int_splicer &other) const {
291 return other.x == x && other.y == y;
295 constexpr int_splicer splice(0x0C05FEFE, 0xCAFEBABE);
297 #if 1
298 static_assert(bit_cast<unsigned long long>(splice) == (LITTLE_END
299 ? 0xCAFEBABE0C05FEFE
300 : 0x0C05FEFECAFEBABE));
302 constexpr int_splicer IS = bit_cast<int_splicer>(0xCAFEBABE0C05FEFE);
303 static_assert(bit_cast<int_splicer>(0xCAFEBABE0C05FEFE).x == (LITTLE_END
304 ? 0x0C05FEFE
305 : 0xCAFEBABE));
307 static_assert(check_round_trip<unsigned long long>(splice));
308 static_assert(check_round_trip<long long>(splice));
309 #endif
313 /// ---------------------------------------------------------------------------
314 /// From here on, it's things copied from test/SemaCXX/constexpr-builtin-bit.cast.cpp
316 void test_int() {
317 static_assert(round_trip<unsigned>((int)-1));
318 static_assert(round_trip<unsigned>((int)0x12345678));
319 static_assert(round_trip<unsigned>((int)0x87654321));
320 static_assert(round_trip<unsigned>((int)0x0C05FEFE));
323 void test_array() {
324 constexpr unsigned char input[] = {0xCA, 0xFE, 0xBA, 0xBE};
325 constexpr unsigned expected = LITTLE_END ? 0xBEBAFECA : 0xCAFEBABE;
326 static_assert(bit_cast<unsigned>(input) == expected);
328 /// Same things but with a composite array.
329 struct US { unsigned char I; };
330 constexpr US input2[] = {{0xCA}, {0xFE}, {0xBA}, {0xBE}};
331 static_assert(bit_cast<unsigned>(input2) == expected);
334 void test_record() {
335 struct int_splicer {
336 unsigned x;
337 unsigned y;
339 constexpr bool operator==(const int_splicer &other) const {
340 return other.x == x && other.y == y;
344 constexpr int_splicer splice{0x0C05FEFE, 0xCAFEBABE};
346 static_assert(bit_cast<unsigned long long>(splice) == (LITTLE_END
347 ? 0xCAFEBABE0C05FEFE
348 : 0x0C05FEFECAFEBABE));
350 static_assert(bit_cast<int_splicer>(0xCAFEBABE0C05FEFE).x == (LITTLE_END
351 ? 0x0C05FEFE
352 : 0xCAFEBABE));
354 static_assert(check_round_trip<unsigned long long>(splice));
355 static_assert(check_round_trip<long long>(splice));
357 struct base2 {
360 struct base3 {
361 unsigned z;
364 struct bases : int_splicer, base2, base3 {
365 unsigned doublez;
368 struct tuple4 {
369 unsigned x, y, z, doublez;
371 bool operator==(tuple4 const &other) const = default;
372 constexpr bool operator==(bases const &other) const {
373 return x == other.x && y == other.y &&
374 z == other.z && doublez == other.doublez;
377 constexpr bases b = {{1, 2}, {}, {3}, 4};
378 constexpr tuple4 t4 = bit_cast<tuple4>(b);
379 static_assert(t4 == tuple4{1, 2, 3, 4});
380 static_assert(check_round_trip<tuple4>(b));
382 /// FIXME: We need to initialize the base pointers in the pointer we're bitcasting to.
383 // constexpr auto b2 = bit_cast<bases>(t4);
384 // static_assert(t4 == b2);
387 void test_partially_initialized() {
388 struct pad {
389 signed char x;
390 int y;
393 struct no_pad {
394 signed char x;
395 signed char p1, p2, p3;
396 int y;
399 static_assert(sizeof(pad) == sizeof(no_pad));
401 #if 0
402 constexpr pad pir{4, 4};
403 constexpr int piw = bit_cast<no_pad>(pir).x; // both-error {{constexpr variable 'piw' must be initialized by a constant expression}} \
404 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
407 constexpr no_pad bad = bit_cast<no_pad>(pir); // both-error {{constexpr variable 'bad' must be initialized by a constant expression}} \
408 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
409 // constexpr pad fine = bit_cast<pad>(no_pad{1, 2, 3, 4, 5});
410 // static_assert(fine.x == 1 && fine.y == 5);
411 #endif
415 void bad_types() {
416 union X {
417 int x;
419 static_assert(__builtin_bit_cast(int, X{0}) == 0); // both-error {{not an integral constant expression}} \
420 // both-note {{bit_cast from a union type is not allowed in a constant expression}}
421 #if 1
423 struct G {
424 int g;
426 // both-error@+2 {{constexpr variable 'g' must be initialized by a constant expression}}
427 // both-note@+1 {{bit_cast from a union type is not allowed in a constant expression}}
428 constexpr G g = __builtin_bit_cast(G, X{0});
429 // both-error@+2 {{constexpr variable 'x' must be initialized by a constant expression}}
430 // both-note@+1 {{bit_cast to a union type is not allowed in a constant expression}}
431 constexpr X x = __builtin_bit_cast(X, G{0});
432 #endif
433 struct has_pointer {
434 int *ptr; // both-note {{invalid type 'int *' is a member of 'has_pointer'}}
437 constexpr intptr_t ptr = __builtin_bit_cast(intptr_t, has_pointer{0}); // both-error {{constexpr variable 'ptr' must be initialized by a constant expression}} \
438 // both-note {{bit_cast from a pointer type is not allowed in a constant expression}}
440 #if 0
441 // expected-error@+2 {{constexpr variable 'hptr' must be initialized by a constant expression}}
442 // expected-note@+1 {{bit_cast to a pointer type is not allowed in a constant expression}}
443 constexpr has_pointer hptr = __builtin_bit_cast(has_pointer, 0ul);
444 #endif
447 void test_array_fill() {
448 constexpr unsigned char a[4] = {1, 2};
449 constexpr unsigned int i = bit_cast<unsigned int>(a);
450 static_assert(i == (LITTLE_END ? 0x00000201 : 0x01020000));
453 struct vol_mem {
454 volatile int x;
457 // both-error@+2 {{constexpr variable 'run_vol_mem' must be initialized by a constant expression}}
458 // both-note@+1 {{non-literal type 'vol_mem' cannot be used in a constant expression}}
459 constexpr int run_vol_mem = __builtin_bit_cast(int, vol_mem{43});
461 struct mem_ptr {
462 int vol_mem::*x; // both-note{{invalid type 'int vol_mem::*' is a member of 'mem_ptr'}}
465 // both-error@+2 {{constexpr variable 'run_mem_ptr' must be initialized by a constant expression}}
466 // both-note@+1 {{bit_cast from a member pointer type is not allowed in a constant expression}}
467 constexpr _BitInt(sizeof(mem_ptr) * 8) run_mem_ptr = __builtin_bit_cast(_BitInt(sizeof(mem_ptr) * 8), mem_ptr{nullptr});
469 constexpr int global_int = 0;
471 struct ref_mem {
472 const int &rm;
474 // both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
475 // both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
476 constexpr intptr_t run_ref_mem = __builtin_bit_cast(intptr_t, ref_mem{global_int});
478 namespace test_vector {
480 typedef unsigned uint2 __attribute__((vector_size(2 * sizeof(unsigned))));
481 typedef char byte8 __attribute__((vector_size(sizeof(unsigned long long))));
483 constexpr uint2 test_vector = { 0x0C05FEFE, 0xCAFEBABE };
485 static_assert(bit_cast<unsigned long long>(test_vector) == (LITTLE_END
486 ? 0xCAFEBABE0C05FEFE
487 : 0x0C05FEFECAFEBABE), "");
488 static_assert(check_round_trip<uint2>(0xCAFEBABE0C05FEFEULL), "");
489 static_assert(check_round_trip<byte8>(0xCAFEBABE0C05FEFEULL), "");
491 typedef bool bool8 __attribute__((ext_vector_type(8)));
492 typedef bool bool9 __attribute__((ext_vector_type(9)));
493 typedef bool bool16 __attribute__((ext_vector_type(16)));
494 typedef bool bool17 __attribute__((ext_vector_type(17)));
495 typedef bool bool32 __attribute__((ext_vector_type(32)));
496 typedef bool bool128 __attribute__((ext_vector_type(128)));
498 static_assert(bit_cast<unsigned char>(bool8{1,0,1,0,1,0,1,0}) == (LITTLE_END ? 0x55 : 0xAA), "");
499 constexpr bool8 b8 = __builtin_bit_cast(bool8, 0x55); // both-error {{'__builtin_bit_cast' source type 'int' does not match destination type 'bool8' (vector of 8 'bool' values) (4 vs 1 bytes)}}
500 #if 0
501 static_assert(check_round_trip<bool8>(static_cast<unsigned char>(0)), "");
502 static_assert(check_round_trip<bool8>(static_cast<unsigned char>(1)), "");
503 static_assert(check_round_trip<bool8>(static_cast<unsigned char>(0x55)), "");
505 static_assert(bit_cast<unsigned short>(bool16{1,1,1,1,1,0,0,0, 1,1,1,1,0,1,0,0}) == (LITTLE_END ? 0x2F1F : 0xF8F4), "");
507 static_assert(check_round_trip<bool16>(static_cast<short>(0xCAFE)), "");
508 static_assert(check_round_trip<bool32>(static_cast<int>(0xCAFEBABE)), "");
509 static_assert(check_round_trip<bool128>(static_cast<__int128_t>(0xCAFEBABE0C05FEFEULL)), "");
510 #endif
512 #if 0
513 // expected-error@+2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
514 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(9)))' (vector of 9 'bool' values) is not allowed in a constant expression; element size 1 * element count 9 is not a multiple of the byte size 8}}
515 constexpr unsigned short bad_bool9_to_short = __builtin_bit_cast(unsigned short, bool9{1,1,0,1,0,1,0,1,0});
516 // expected-error@+2 {{constexpr variable 'bad_short_to_bool9' must be initialized by a constant expression}}
517 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(9)))' (vector of 9 'bool' values) is not allowed in a constant expression; element size 1 * element count 9 is not a multiple of the byte size 8}}
518 constexpr bool9 bad_short_to_bool9 = __builtin_bit_cast(bool9, static_cast<unsigned short>(0));
519 // expected-error@+2 {{constexpr variable 'bad_int_to_bool17' must be initialized by a constant expression}}
520 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(17)))' (vector of 17 'bool' values) is not allowed in a constant expression; element size 1 * element count 17 is not a multiple of the byte size 8}}
521 constexpr bool17 bad_int_to_bool17 = __builtin_bit_cast(bool17, 0x0001CAFEU);
522 #endif
525 namespace test_complex {
526 constexpr _Complex unsigned test_int_complex = { 0x0C05FEFE, 0xCAFEBABE };
527 static_assert(round_trip<_Complex unsigned>(0xCAFEBABE0C05FEFEULL), "");
528 static_assert(bit_cast<unsigned long long>(test_int_complex) == (LITTLE_END
529 ? 0xCAFEBABE0C05FEFE
530 : 0x0C05FEFECAFEBABE), "");
531 static_assert(sizeof(double) == 2 * sizeof(float));
532 struct TwoFloats { float A; float B; };
533 constexpr _Complex float test_float_complex = {1.0f, 2.0f};
534 constexpr TwoFloats TF = __builtin_bit_cast(TwoFloats, test_float_complex);
535 static_assert(TF.A == 1.0f && TF.B == 2.0f);
537 constexpr double D = __builtin_bit_cast(double, test_float_complex);
538 constexpr int M = __builtin_bit_cast(int, test_int_complex); // both-error {{size of '__builtin_bit_cast' source type 'const _Complex unsigned int' does not match destination type 'int' (8 vs 4 bytes)}}