Debian Codegraph @

clang/test/AST/ByteCode/builtin-bit-cast.cpp

raw
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
5
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
10
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
18
19 typedef decltype(nullptr) nullptr_t;
20 typedef __INTPTR_TYPE__ intptr_t;
21
22 static_assert(sizeof(int) == 4);
23 static_assert(sizeof(long long) == 8);
24
25
26 constexpr bool test_bad_bool = __builtin_bit_cast(bool, (char)0xff); // both-error {{must be initialized by a constant expression}} \
27 // both-note {{value 255 cannot be represented in type 'bool'}}
28
29 template <class To, class From>
30 constexpr To bit_cast(const From &from) {
31 static_assert(sizeof(To) == sizeof(From));
32 return __builtin_bit_cast(To, from);
33 }
34
35 template <class Intermediate, class Init>
36 constexpr bool check_round_trip(const Init &init) {
37 return bit_cast<Init>(bit_cast<Intermediate>(init)) == init;
38 }
39
40 template <class Intermediate, class Init>
41 constexpr Init round_trip(const Init &init) {
42 return bit_cast<Init>(bit_cast<Intermediate>(init));
43 }
44
45
46 namespace Discarding {
47 struct S { int a; };
48 constexpr int f = (__builtin_bit_cast(int, 2), 0);
49 constexpr int f2 = (__builtin_bit_cast(S, 2), 0);
50 }
51
52 namespace std {
53 enum byte : unsigned char {};
54 } // namespace std
55
56 using uint8_t = unsigned char;
57
58 template<int N>
59 struct bytes {
60 using size_t = unsigned int;
61 unsigned char d[N];
62
63 constexpr unsigned char &operator[](size_t index) {
64 if (index < N)
65 return d[index];
66 }
67 };
68
69
70 template <int N, typename T = unsigned char, int Pad = 0>
71 struct bits {
72 T : Pad;
73 T bits : N;
74
75 constexpr bool operator==(const T& rhs) const {
76 return bits == rhs;
77 }
78 };
79
80 template <int N, typename T, int P>
81 constexpr bool operator==(const struct bits<N, T, P>& lhs, const struct bits<N, T, P>& rhs) {
82 return lhs.bits == rhs.bits;
83 }
84
85 #ifdef __SIZEOF_INT128__
86 static_assert(check_round_trip<__int128_t>((__int128_t)34));
87 static_assert(check_round_trip<__int128_t>((__int128_t)-34));
88
89 constexpr unsigned char OneBit[] = {
90 0x1, 0x0, 0x0, 0x0,
91 0x0, 0x0, 0x0, 0x0,
92 0x0, 0x0, 0x0, 0x0,
93 0x0, 0x0, 0x0, 0x0,
94 };
95 constexpr __int128_t One = 1;
96 constexpr __int128_t Expected = One << 120;
97 static_assert(__builtin_bit_cast(__int128_t, OneBit) == (LITTLE_END ? 1 : Expected));
98
99 #endif
100
101 static_assert(check_round_trip<double>(17.0));
102
103
104 namespace simple {
105 constexpr int A = __builtin_bit_cast(int, 10);
106 static_assert(A == 10);
107
108 static_assert(__builtin_bit_cast(unsigned, 1.0F) == 1065353216);
109
110 struct Bytes {
111 char a, b, c, d;
112 };
113 constexpr unsigned B = __builtin_bit_cast(unsigned, Bytes{10, 12, 13, 14});
114 static_assert(B == (LITTLE_END ? 235736074 : 168561934));
115
116
117 constexpr unsigned C = __builtin_bit_cast(unsigned, (_BitInt(32))12);
118 static_assert(C == 12);
119
120 struct BitInts {
121 _BitInt(16) a;
122 _BitInt(16) b;
123 };
124 constexpr unsigned D = __builtin_bit_cast(unsigned, BitInts{12, 13});
125 static_assert(D == (LITTLE_END ? 851980 : 786445));
126
127
128
129 static_assert(__builtin_bit_cast(char, true) == 1);
130
131 static_assert(check_round_trip<unsigned>((int)-1));
132 static_assert(check_round_trip<unsigned>((int)0x12345678));
133 static_assert(check_round_trip<unsigned>((int)0x87654321));
134 static_assert(check_round_trip<unsigned>((int)0x0C05FEFE));
135 static_assert(round_trip<float>((int)0x0C05FEFE));
136
137 static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // both-error {{not an integral constant expression}} \
138 // both-note {{indeterminate value can only initialize an object}}
139
140 constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[sizeof(nullptr)], nullptr), 0);
141 constexpr unsigned char NPData[sizeof(nullptr)] = {1,2,3,4};
142 constexpr nullptr_t NP = __builtin_bit_cast(nullptr_t, NPData);
143 static_assert(NP == nullptr);
144 }
145
146 namespace Fail {
147 constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \
148 // both-note {{division by zero}} \
149 // both-note {{declared here}}
150 constexpr int b = __builtin_bit_cast(int, a); // both-error {{must be initialized by a constant expression}} \
151 // both-note {{initializer of 'a' is not a constant expression}}
152 }
153
154 namespace ToPtr {
155 struct S {
156 const int *p = nullptr;
157 };
158 struct P {
159 const int *p; // both-note {{invalid type 'const int *' is a member of 'ToPtr::P'}}
160 };
161 constexpr P p = __builtin_bit_cast(P, S{}); // both-error {{must be initialized by a constant expression}} \
162 // both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
163 }
164
165 namespace Invalid {
166 struct S {
167 int a;
168 };
169 constexpr S s = S{1/0}; // both-error {{must be initialized by a constant expression}} \
170 // both-note {{division by zero}} \
171 // both-note {{declared here}}
172 constexpr S s2 = __builtin_bit_cast(S, s); // both-error {{must be initialized by a constant expression}} \
173 // both-note {{initializer of 's' is not a constant expression}}
174 }
175
176 namespace NullPtr {
177 constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
178 static_assert(N == nullptr);
179 static_assert(__builtin_bit_cast(nullptr_t, (_BitInt(sizeof(void*) * 8))12) == __builtin_bit_cast(nullptr_t, (unsigned _BitInt(sizeof(void*) * 8))0));
180 static_assert(__builtin_bit_cast(nullptr_t, nullptr) == nullptr);
181 }
182
183 namespace bitint {
184 constexpr _BitInt(sizeof(int) * 8) BI = ~0;
185 constexpr unsigned int I = __builtin_bit_cast(unsigned int, BI);
186 static_assert(I == ~0u, "");
187
188 constexpr _BitInt(sizeof(int) * 8) IB = __builtin_bit_cast(_BitInt(sizeof(int) * 8), I); // ref-error {{must be initialized by a constant expression}} \
189 // ref-note {{constexpr bit cast involving type '_BitInt(32)' is not yet supported}} \
190 // ref-note {{declared here}}
191 static_assert(IB == ~0u, ""); // ref-error {{not an integral constant expression}} \
192 // ref-note {{initializer of 'IB' is not a constant expression}}
193 }
194
195 namespace Classes {
196 class A {
197 public:
198 char a[2];
199 };
200 class B : public A {
201 public:
202 char b[2];
203 };
204 static_assert(__builtin_bit_cast(int, B{{0, 0},{0, 0}}) == 0);
205 static_assert(__builtin_bit_cast(int, B{{13, 0},{0, 0}}) == (LITTLE_END ? 13 : 218103808));
206 static_assert(__builtin_bit_cast(int, B{{13, 7},{12, 20}}) == (LITTLE_END ? 336332557 : 218565652));
207
208 class Ref {
209 public:
210 const int &a;
211 constexpr Ref(const int &a) : a(a) {}
212 };
213 constexpr int I = 12;
214
215 typedef __INTPTR_TYPE__ intptr_t;
216 static_assert(__builtin_bit_cast(intptr_t, Ref{I}) == 0); // both-error {{not an integral constant expression}} \
217 // both-note {{bit_cast from a type with a reference member is not allowed in a constant expression}}
218
219 class C : public A {
220 public:
221 constexpr C() : A{1,2} {}
222 virtual constexpr int get() {
223 return 4;
224 }
225 };
226 static_assert(__builtin_bit_cast(_BitInt(sizeof(C) * 8), C()) == 0); // both-error {{source type must be trivially copyable}}
227
228
229 class D : virtual A {};
230 static_assert(__builtin_bit_cast(_BitInt(sizeof(D) * 8), D()) == 0); // both-error {{source type must be trivially copyable}}
231
232 class F {
233 public:
234 char f[2];
235 };
236
237 class E : public A, public F {
238 public:
239 constexpr E() : A{1,2}, F{3,4}, e{5,6,7,8} {}
240 char e[4];
241 };
242 static_assert(__builtin_bit_cast(long long, E()) == (LITTLE_END ? 578437695752307201 : 72623859790382856));
243 }
244
245 struct int_splicer {
246 unsigned x;
247 unsigned y;
248
249 constexpr int_splicer() : x(1), y(2) {}
250 constexpr int_splicer(unsigned x, unsigned y) : x(x), y(y) {}
251
252 constexpr bool operator==(const int_splicer &other) const {
253 return other.x == x && other.y == y;
254 }
255 };
256
257 constexpr int_splicer splice(0x0C05FEFE, 0xCAFEBABE);
258
259 #if 1
260 static_assert(bit_cast<unsigned long long>(splice) == (LITTLE_END
261 ? 0xCAFEBABE0C05FEFE
262 : 0x0C05FEFECAFEBABE));
263
264 constexpr int_splicer IS = bit_cast<int_splicer>(0xCAFEBABE0C05FEFE);
265 static_assert(bit_cast<int_splicer>(0xCAFEBABE0C05FEFE).x == (LITTLE_END
266 ? 0x0C05FEFE
267 : 0xCAFEBABE));
268
269 static_assert(check_round_trip<unsigned long long>(splice));
270 static_assert(check_round_trip<long long>(splice));
271 #endif
272
273
274 namespace Overread {
275 /// This used to crash becaus we were reading all elements of the
276 /// source array even though we should only be reading 1.
277 constexpr int a[] = {2,3, 4, 5};
278 constexpr int b = __builtin_bit_cast(int, *(a + 1));
279 static_assert(b == 3);
280
281 struct S {
282 int a;
283 };
284 constexpr S ss[] = {{1},{2}};
285 constexpr int c = __builtin_bit_cast(int, *(ss + 1));
286 static_assert(c == 2);
287 }
288
289
290 /// ---------------------------------------------------------------------------
291 /// From here on, it's things copied from test/SemaCXX/constexpr-builtin-bit.cast.cpp
292
293 void test_int() {
294 static_assert(round_trip<unsigned>((int)-1));
295 static_assert(round_trip<unsigned>((int)0x12345678));
296 static_assert(round_trip<unsigned>((int)0x87654321));
297 static_assert(round_trip<unsigned>((int)0x0C05FEFE));
298 }
299
300 void test_array() {
301 constexpr unsigned char input[] = {0xCA, 0xFE, 0xBA, 0xBE};
302 constexpr unsigned expected = LITTLE_END ? 0xBEBAFECA : 0xCAFEBABE;
303 static_assert(bit_cast<unsigned>(input) == expected);
304
305 /// Same things but with a composite array.
306 struct US { unsigned char I; };
307 constexpr US input2[] = {{0xCA}, {0xFE}, {0xBA}, {0xBE}};
308 static_assert(bit_cast<unsigned>(input2) == expected);
309 }
310
311 void test_record() {
312 struct int_splicer {
313 unsigned x;
314 unsigned y;
315
316 constexpr bool operator==(const int_splicer &other) const {
317 return other.x == x && other.y == y;
318 }
319 };
320
321 constexpr int_splicer splice{0x0C05FEFE, 0xCAFEBABE};
322
323 static_assert(bit_cast<unsigned long long>(splice) == (LITTLE_END
324 ? 0xCAFEBABE0C05FEFE
325 : 0x0C05FEFECAFEBABE));
326
327 static_assert(bit_cast<int_splicer>(0xCAFEBABE0C05FEFE).x == (LITTLE_END
328 ? 0x0C05FEFE
329 : 0xCAFEBABE));
330
331 static_assert(check_round_trip<unsigned long long>(splice));
332 static_assert(check_round_trip<long long>(splice));
333
334 struct base2 {
335 };
336
337 struct base3 {
338 unsigned z;
339 };
340
341 struct bases : int_splicer, base2, base3 {
342 unsigned doublez;
343 };
344
345 struct tuple4 {
346 unsigned x, y, z, doublez;
347
348 bool operator==(tuple4 const &other) const = default;
349 constexpr bool operator==(bases const &other) const {
350 return x == other.x && y == other.y &&
351 z == other.z && doublez == other.doublez;
352 }
353 };
354 constexpr bases b = {{1, 2}, {}, {3}, 4};
355 constexpr tuple4 t4 = bit_cast<tuple4>(b);
356 static_assert(t4 == tuple4{1, 2, 3, 4});
357 static_assert(check_round_trip<tuple4>(b));
358
359 constexpr auto b2 = bit_cast<bases>(t4);
360 static_assert(t4 == b2);
361 }
362
363 void test_partially_initialized() {
364 struct pad {
365 signed char x;
366 int y;
367 };
368
369 struct no_pad {
370 signed char x;
371 signed char p1, p2, p3;
372 int y;
373 };
374
375 static_assert(sizeof(pad) == sizeof(no_pad));
376
377 #if 0
378 constexpr pad pir{4, 4};
379 constexpr int piw = bit_cast<no_pad>(pir).x; // both-error {{constexpr variable 'piw' must be initialized by a constant expression}} \
380 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
381
382
383 constexpr no_pad bad = bit_cast<no_pad>(pir); // both-error {{constexpr variable 'bad' must be initialized by a constant expression}} \
384 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
385 // constexpr pad fine = bit_cast<pad>(no_pad{1, 2, 3, 4, 5});
386 // static_assert(fine.x == 1 && fine.y == 5);
387 #endif
388 }
389
390
391 void bad_types() {
392 union X {
393 int x;
394 };
395 static_assert(__builtin_bit_cast(int, X{0}) == 0); // both-error {{not an integral constant expression}} \
396 // both-note {{bit_cast from a union type is not allowed in a constant expression}}
397
398 struct G {
399 int g;
400 };
401 // both-error@+2 {{constexpr variable 'g' must be initialized by a constant expression}}
402 // both-note@+1 {{bit_cast from a union type is not allowed in a constant expression}}
403 constexpr G g = __builtin_bit_cast(G, X{0});
404 // both-error@+2 {{constexpr variable 'x' must be initialized by a constant expression}}
405 // both-note@+1 {{bit_cast to a union type is not allowed in a constant expression}}
406 constexpr X x = __builtin_bit_cast(X, G{0});
407
408 struct has_pointer {
409 int *ptr; // both-note 2{{invalid type 'int *' is a member of 'has_pointer'}}
410 };
411
412 constexpr intptr_t ptr = __builtin_bit_cast(intptr_t, has_pointer{0}); // both-error {{constexpr variable 'ptr' must be initialized by a constant expression}} \
413 // both-note {{bit_cast from a pointer type is not allowed in a constant expression}}
414
415 // both-error@+2 {{constexpr variable 'hptr' must be initialized by a constant expression}}
416 // both-note@+1 {{bit_cast to a pointer type is not allowed in a constant expression}}
417 constexpr has_pointer hptr = __builtin_bit_cast(has_pointer, (intptr_t)0);
418 }
419
420 void test_array_fill() {
421 constexpr unsigned char a[4] = {1, 2};
422 constexpr unsigned int i = bit_cast<unsigned int>(a);
423 static_assert(i == (LITTLE_END ? 0x00000201 : 0x01020000));
424 }
425
426 struct vol_mem {
427 volatile int x;
428 };
429
430 // both-error@+2 {{constexpr variable 'run_vol_mem' must be initialized by a constant expression}}
431 // both-note@+1 {{non-literal type 'vol_mem' cannot be used in a constant expression}}
432 constexpr int run_vol_mem = __builtin_bit_cast(int, vol_mem{43});
433
434 struct mem_ptr {
435 int vol_mem::*x; // both-note{{invalid type 'int vol_mem::*' is a member of 'mem_ptr'}}
436 };
437
438 // both-error@+2 {{constexpr variable 'run_mem_ptr' must be initialized by a constant expression}}
439 // both-note@+1 {{bit_cast from a member pointer type is not allowed in a constant expression}}
440 constexpr _BitInt(sizeof(mem_ptr) * 8) run_mem_ptr = __builtin_bit_cast(_BitInt(sizeof(mem_ptr) * 8), mem_ptr{nullptr});
441
442 constexpr int global_int = 0;
443
444 struct ref_mem {
445 const int &rm;
446 };
447 // both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
448 // both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
449 constexpr intptr_t run_ref_mem = __builtin_bit_cast(intptr_t, ref_mem{global_int});
450
451 namespace test_vector {
452
453 typedef unsigned uint2 __attribute__((vector_size(2 * sizeof(unsigned))));
454 typedef char byte8 __attribute__((vector_size(sizeof(unsigned long long))));
455
456 constexpr uint2 test_vector = { 0x0C05FEFE, 0xCAFEBABE };
457
458 static_assert(bit_cast<unsigned long long>(test_vector) == (LITTLE_END
459 ? 0xCAFEBABE0C05FEFE
460 : 0x0C05FEFECAFEBABE), "");
461 static_assert(check_round_trip<uint2>(0xCAFEBABE0C05FEFEULL), "");
462 static_assert(check_round_trip<byte8>(0xCAFEBABE0C05FEFEULL), "");
463
464 #if 0
465 // expected-error@+2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
466 // 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}}
467 constexpr unsigned short bad_bool9_to_short = __builtin_bit_cast(unsigned short, bool9{1,1,0,1,0,1,0,1,0});
468 // expected-error@+2 {{constexpr variable 'bad_short_to_bool9' must be initialized by a constant expression}}
469 // 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}}
470 constexpr bool9 bad_short_to_bool9 = __builtin_bit_cast(bool9, static_cast<unsigned short>(0));
471 // expected-error@+2 {{constexpr variable 'bad_int_to_bool17' must be initialized by a constant expression}}
472 // 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}}
473 constexpr bool17 bad_int_to_bool17 = __builtin_bit_cast(bool17, 0x0001CAFEU);
474 #endif
475 }
476
477 namespace test_complex {
478 constexpr _Complex unsigned test_int_complex = { 0x0C05FEFE, 0xCAFEBABE };
479 static_assert(round_trip<_Complex unsigned>(0xCAFEBABE0C05FEFEULL), "");
480 static_assert(bit_cast<unsigned long long>(test_int_complex) == (LITTLE_END
481 ? 0xCAFEBABE0C05FEFE
482 : 0x0C05FEFECAFEBABE), "");
483 static_assert(sizeof(double) == 2 * sizeof(float));
484 struct TwoFloats { float A; float B; };
485 constexpr _Complex float test_float_complex = {1.0f, 2.0f};
486 constexpr TwoFloats TF = __builtin_bit_cast(TwoFloats, test_float_complex);
487 static_assert(TF.A == 1.0f && TF.B == 2.0f);
488
489 constexpr double D = __builtin_bit_cast(double, test_float_complex);
490 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)}}
491 }
492
493
494 namespace OversizedBitField {
495 #if defined(_WIN32)
496 /// This is an error (not just a warning) on Windows and the field ends up with a size of 1 instead of 4.
497 #else
498 typedef unsigned __INT16_TYPE__ uint16_t;
499 typedef unsigned __INT32_TYPE__ uint32_t;
500 struct S {
501 uint16_t a : 20; // both-warning {{exceeds the width of its type}}
502 };
503
504 static_assert(sizeof(S) == 4);
505 static_assert(__builtin_bit_cast(S, (uint32_t)32).a == (LITTLE_END ? 32 : 0)); // ref-error {{not an integral constant expression}} \
506 // ref-note {{constexpr bit_cast involving bit-field is not yet supported}}
507 #endif
508 }
509
510 namespace Discarded {
511 enum my_byte : unsigned char {};
512 struct pad {
513 char a;
514 int b;
515 };
516 constexpr int bad_my_byte = (__builtin_bit_cast(my_byte[8], pad{1, 2}), 0); // both-error {{must be initialized by a constant expression}} \
517 // both-note {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte';}}
518 }
519
520 typedef bool bool9 __attribute__((ext_vector_type(9)));
521 // both-error@+2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
522 // both-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}}
523 constexpr unsigned short bad_bool9_to_short = __builtin_bit_cast(unsigned short, bool9{1,1,0,1,0,1,0,1,0});
524
525 // both-warning@+2 {{returning reference to local temporary object}}
526 // both-note@+1 {{temporary created here}}
527 constexpr const intptr_t &returns_local() { return 0L; }
528
529 // both-error@+2 {{constexpr variable 'test_nullptr_bad' must be initialized by a constant expression}}
530 // both-note@+1 {{read of temporary whose lifetime has ended}}
531 constexpr nullptr_t test_nullptr_bad = __builtin_bit_cast(nullptr_t, returns_local());
532
533 #ifdef __SIZEOF_INT128__
534 namespace VectorCast {
535 typedef unsigned X __attribute__ ((vector_size (64)));
536 typedef unsigned __int128 Y __attribute__ ((vector_size (64)));
537 constexpr int test() {
538 X x = {0};
539 Y y = x;
540
541 X x2 = y;
542
543 return 0;
544 }
545 static_assert(test() == 0);
546
547 typedef int X2 __attribute__ ((vector_size (64)));
548 typedef __int128 Y2 __attribute__ ((vector_size (64)));
549 constexpr int test2() {
550 X2 x = {0};
551 Y2 y = x;
552
553 X2 x2 = y;
554
555 return 0;
556 }
557 static_assert(test2() == 0);
558
559 /// On s390x, S is only 8 bytes.
560 #if !defined(__s390x__)
561 struct S {
562 unsigned __int128 a : 3;
563 };
564 constexpr S s = __builtin_bit_cast(S, (__int128)12); // ref-error {{must be initialized by a constant expression}} \
565 // ref-note {{constexpr bit_cast involving bit-field is not yet supported}} \
566 // ref-note {{declared here}}
567 #if LITTLE_END
568 static_assert(s.a == 4); // ref-error {{not an integral constant expression}} \
569 // ref-note {{initializer of 's' is not a constant expression}}
570 #else
571 static_assert(s.a == 0); // ref-error {{not an integral constant expression}} \
572 // ref-note {{initializer of 's' is not a constant expression}}
573 #endif
574 #endif
575 }
576 #endif
577
578 namespace ToPrimPtrs {
579 struct S { int foo () { return 0; } };
580 auto ptr = __builtin_bit_cast(int *, ((__INTPTR_TYPE__) 0));
581 auto nptr = __builtin_bit_cast(nullptr_t, ((__INTPTR_TYPE__)0));
582
583 constexpr auto cptr = __builtin_bit_cast(int *, ((__INTPTR_TYPE__) 0)); // both-error {{must be initialized by a constant expression}} \
584 // both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
585 constexpr auto cnptr = __builtin_bit_cast(nullptr_t, ((__INTPTR_TYPE__)0));
586
587 #if !defined(_WIN32)
588 auto memptr = __builtin_bit_cast(int S::*, ((__INTPTR_TYPE__) 0));
589 constexpr auto cmemptr = __builtin_bit_cast(int S::*, ((__INTPTR_TYPE__) 0)); // both-error {{must be initialized by a constant expression}} \
590 // both-note {{bit_cast to a member pointer type is not allowed in a constant expression}}
591 #endif
592 }