xref: /llvm-project/clang/test/AST/ByteCode/records.cpp (revision 6972788bf3d330b7a6136e2ddd840782882b8dd0)
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify=expected,both %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s
3 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -triple i686 -verify=expected,both %s
4 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,both %s
5 // RUN: %clang_cc1 -verify=ref,both -std=c++14 %s
6 // RUN: %clang_cc1 -verify=ref,both -std=c++17 %s
7 // RUN: %clang_cc1 -verify=ref,both -std=c++17 -triple i686 %s
8 // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
9 
10 /// Used to crash.
11 struct Empty {};
12 constexpr Empty e = {Empty()};
13 
14 struct BoolPair {
15   bool first;
16   bool second;
17 };
18 
19 struct Ints {
20   int a = 20;
21   int b = 30;
22   bool c = true;
23   BoolPair bp = {true, false};
24   int numbers[3] = {1,2,3};
25 
26   static const int five = 5;
27   static constexpr int getFive() {
28     return five;
29   }
30 
31   constexpr int getTen() const {
32     return 10;
33   }
34 };
35 
36 static_assert(Ints::getFive() == 5, "");
37 
38 constexpr Ints ints;
39 static_assert(ints.a == 20, "");
40 static_assert(ints.b == 30, "");
41 static_assert(ints.c, "");
42 static_assert(ints.getTen() == 10, "");
43 static_assert(ints.numbers[0] == 1, "");
44 static_assert(ints.numbers[1] == 2, "");
45 static_assert(ints.numbers[2] == 3, "");
46 
47 constexpr const BoolPair &BP = ints.bp;
48 static_assert(BP.first, "");
49 static_assert(!BP.second, "");
50 static_assert(ints.bp.first, "");
51 static_assert(!ints.bp.second, "");
52 
53 
54 constexpr Ints ints2{-20, -30, false};
55 static_assert(ints2.a == -20, "");
56 static_assert(ints2.b == -30, "");
57 static_assert(!ints2.c, "");
58 
59 constexpr Ints getInts() {
60   return {64, 128, true};
61 }
62 constexpr Ints ints3 = getInts();
63 static_assert(ints3.a == 64, "");
64 static_assert(ints3.b == 128, "");
65 static_assert(ints3.c, "");
66 
67 constexpr Ints ints4 = {
68   .a = 40 * 50,
69   .b = 0,
70   .c = (ints.a > 0),
71 
72 };
73 static_assert(ints4.a == (40 * 50), "");
74 static_assert(ints4.b == 0, "");
75 static_assert(ints4.c, "");
76 static_assert(ints4.numbers[0] == 1, "");
77 static_assert(ints4.numbers[1] == 2, "");
78 static_assert(ints4.numbers[2] == 3, "");
79 
80 constexpr Ints ints5 = ints4;
81 static_assert(ints5.a == (40 * 50), "");
82 static_assert(ints5.b == 0, "");
83 static_assert(ints5.c, "");
84 static_assert(ints5.numbers[0] == 1, "");
85 static_assert(ints5.numbers[1] == 2, "");
86 static_assert(ints5.numbers[2] == 3, "");
87 
88 
89 struct Ints2 {
90   int a = 10;
91   int b;
92 };
93 constexpr Ints2 ints22; // both-error {{without a user-provided default constructor}}
94 
95 constexpr Ints2 I2 = Ints2{12, 25};
96 static_assert(I2.a == 12, "");
97 static_assert(I2.b == 25, "");
98 
99 class C {
100   public:
101     int a;
102     int b;
103 
104   constexpr C() : a(100), b(200) {}
105 
106   constexpr C get() const {
107     return *this;
108   }
109 };
110 
111 constexpr C c;
112 static_assert(c.a == 100, "");
113 static_assert(c.b == 200, "");
114 
115 constexpr C c2 = C().get();
116 static_assert(c2.a == 100, "");
117 static_assert(c2.b == 200, "");
118 
119 
120 /// A global, composite temporary variable.
121 constexpr const C &c3 = C().get();
122 
123 /// Same, but with a bitfield.
124 class D {
125 public:
126   unsigned a : 4;
127   constexpr D() : a(15) {}
128   constexpr D get() const {
129     return *this;
130   }
131 };
132 constexpr const D &d4 = D().get();
133 
134 constexpr int getB() {
135   C c;
136   int &j = c.b;
137 
138   j = j * 2;
139 
140   return c.b;
141 }
142 static_assert(getB() == 400, "");
143 
144 constexpr int getA(const C &c) {
145   return c.a;
146 }
147 static_assert(getA(c) == 100, "");
148 
149 constexpr const C* getPointer() {
150   return &c;
151 }
152 static_assert(getPointer()->a == 100, "");
153 
154 constexpr C RVOAndParams(const C *c) {
155   return C();
156 }
157 constexpr C RVOAndParamsResult = RVOAndParams(&c);
158 
159 /// Parameter and return value have different types.
160 constexpr C RVOAndParams(int a) {
161   return C();
162 }
163 constexpr C RVOAndParamsResult2 = RVOAndParams(12);
164 
165 class Bar { // both-note {{definition of 'Bar' is not complete}}
166 public:
167   constexpr Bar(){}
168   constexpr Bar b; // both-error {{cannot be constexpr}} \
169                    // both-error {{has incomplete type 'const Bar'}}
170 };
171 constexpr Bar B; // both-error {{must be initialized by a constant expression}}
172 constexpr Bar *pb = nullptr;
173 
174 constexpr int locals() {
175   C c;
176   c.a = 10;
177 
178   // Assignment, not an initializer.
179   c = C();
180   c.a = 10;
181 
182 
183   // Assignment, not an initializer.
184   c = RVOAndParams(&c);
185 
186   return c.a;
187 }
188 static_assert(locals() == 100, "");
189 
190 namespace thisPointer {
191   struct S {
192     constexpr int get12() { return 12; }
193   };
194 
195   constexpr int foo() { // both-error {{never produces a constant expression}}
196     S *s = nullptr;
197     return s->get12(); // both-note 2{{member call on dereferenced null pointer}}
198 
199   }
200   static_assert(foo() == 12, ""); // both-error {{not an integral constant expression}} \
201                                   // both-note {{in call to 'foo()'}}
202 };
203 
204 struct FourBoolPairs {
205   BoolPair v[4] = {
206     {false, false},
207     {false,  true},
208     {true,  false},
209     {true,  true },
210   };
211 };
212 // Init
213 constexpr FourBoolPairs LT;
214 // Copy ctor
215 constexpr FourBoolPairs LT2 = LT;
216 static_assert(LT2.v[0].first == false, "");
217 static_assert(LT2.v[0].second == false, "");
218 static_assert(LT2.v[2].first == true, "");
219 static_assert(LT2.v[2].second == false, "");
220 
221 class Base {
222 public:
223   int i;
224   constexpr Base() : i(10) {}
225   constexpr Base(int i) : i(i) {}
226 };
227 
228 class A : public Base {
229 public:
230   constexpr A() : Base(100) {}
231   constexpr A(int a) : Base(a) {}
232 };
233 constexpr A a{};
234 static_assert(a.i == 100, "");
235 constexpr A a2{12};
236 static_assert(a2.i == 12, "");
237 static_assert(a2.i == 200, ""); // both-error {{static assertion failed}} \
238                                 // both-note {{evaluates to '12 == 200'}}
239 
240 
241 struct S {
242   int a = 0;
243   constexpr int get5() const { return 5; }
244   constexpr void fo() const {
245     this; // both-warning {{expression result unused}}
246     this->a; // both-warning {{expression result unused}}
247     get5();
248     getInts();
249   }
250 
251   constexpr int m() const {
252     fo();
253     return 1;
254   }
255 };
256 constexpr S s;
257 static_assert(s.m() == 1, "");
258 
259 namespace InitializerTemporaries {
260   class Bar {
261   private:
262     int a;
263 
264   public:
265     constexpr Bar() : a(10) {}
266     constexpr int getA() const { return a; }
267   };
268 
269   class Foo {
270   public:
271     int a;
272 
273     constexpr Foo() : a(Bar().getA()) {}
274   };
275   constexpr Foo F;
276   static_assert(F.a == 10, "");
277 
278 
279   /// Needs constexpr destructors.
280 #if __cplusplus >= 202002L
281   /// Does
282   ///    Arr[Pos] = Value;
283   ///    ++Pos;
284   /// in its destructor.
285   class BitSetter {
286   private:
287     int *Arr;
288     int &Pos;
289     int Value;
290 
291   public:
292     constexpr BitSetter(int *Arr, int &Pos, int Value) :
293       Arr(Arr), Pos(Pos), Value(Value) {}
294 
295     constexpr int getValue() const { return 0; }
296     constexpr ~BitSetter() {
297       Arr[Pos] = Value;
298       ++Pos;
299     }
300   };
301 
302   class Test {
303     int a, b, c;
304   public:
305     constexpr Test(int *Arr, int &Pos) :
306       a(BitSetter(Arr, Pos, 1).getValue()),
307       b(BitSetter(Arr, Pos, 2).getValue()),
308       c(BitSetter(Arr, Pos, 3).getValue())
309     {}
310   };
311 
312 
313   constexpr int T(int Index) {
314     int Arr[] = {0, 0, 0};
315     int Pos = 0;
316 
317     {
318       Test(Arr, Pos);
319       // End of scope, should destroy Test.
320     }
321 
322     return Arr[Index];
323   }
324   static_assert(T(0) == 1);
325   static_assert(T(1) == 2);
326   static_assert(T(2) == 3);
327 
328   // Invalid destructor.
329   struct S {
330     constexpr S() {}
331     constexpr ~S() noexcept(false) { throw 12; } // both-error {{cannot use 'throw'}} \
332                                                  // both-error {{never produces a constant expression}} \
333                                                  // both-note 2{{subexpression not valid}}
334   };
335 
336   constexpr int f() {
337     S{}; // both-note {{in call to 'S{}.~S()'}}
338     return 12;
339   }
340   static_assert(f() == 12); // both-error {{not an integral constant expression}} \
341                             // both-note {{in call to 'f()'}}
342 
343 
344 #endif
345 }
346 
347 #if __cplusplus >= 201703L
348 namespace BaseInit {
349   class _A {public: int a;};
350   class _B : public _A {};
351   class _C : public _B {};
352 
353   constexpr _C c{12};
354   constexpr const _B &b = c;
355   static_assert(b.a == 12);
356 
357   class A {public: int a;};
358   class B : public A {};
359   class C : public A {};
360   class D : public B, public C {};
361 
362   // This initializes D::B::A::a and not D::C::A::a.
363   constexpr D d{12};
364   static_assert(d.B::a == 12);
365   static_assert(d.C::a == 0);
366 };
367 #endif
368 
369 namespace MI {
370   class A {
371   public:
372     int a;
373     constexpr A(int a) : a(a) {}
374   };
375 
376   class B {
377   public:
378     int b;
379     constexpr B(int b) : b(b) {}
380   };
381 
382   class C : public A, public B {
383   public:
384     constexpr C() : A(10), B(20) {}
385   };
386   constexpr C c = {};
387   static_assert(c.a == 10, "");
388   static_assert(c.b == 20, "");
389 
390   constexpr const A *aPointer = &c;
391   constexpr const B *bPointer = &c;
392 
393   class D : private A, private B {
394     public:
395     constexpr D() : A(20), B(30) {}
396     constexpr int getA() const { return a; }
397     constexpr int getB() const { return b; }
398   };
399   constexpr D d = {};
400   static_assert(d.getA() == 20, "");
401   static_assert(d.getB() == 30, "");
402 };
403 
404 namespace DeriveFailures {
405 #if __cplusplus < 202002L
406   struct Base { // both-note {{declared here}} \
407                 // ref-note {{declared here}}
408     int Val;
409   };
410 
411   struct Derived : Base {
412     int OtherVal;
413 
414     constexpr Derived(int i) : OtherVal(i) {} // ref-error {{never produces a constant expression}} \
415                                               // both-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}} \
416                                               // ref-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}}
417   };
418 
419   constexpr Derived D(12); // both-error {{must be initialized by a constant expression}} \
420                            // both-note {{in call to 'Derived(12)'}} \
421                            // both-note {{declared here}}
422 
423   static_assert(D.Val == 0, ""); // both-error {{not an integral constant expression}} \
424                                  // both-note {{initializer of 'D' is not a constant expression}}
425 #endif
426 
427   struct AnotherBase {
428     int Val;
429     constexpr AnotherBase(int i) : Val(12 / i) {} // both-note {{division by zero}}
430   };
431 
432   struct AnotherDerived : AnotherBase {
433     constexpr AnotherDerived(int i) : AnotherBase(i) {}
434   };
435   constexpr AnotherBase Derp(0); // both-error {{must be initialized by a constant expression}} \
436                                  // both-note {{in call to 'AnotherBase(0)'}}
437 
438   struct YetAnotherBase {
439     int Val;
440     constexpr YetAnotherBase(int i) : Val(i) {}
441   };
442 
443   struct YetAnotherDerived : YetAnotherBase {
444     using YetAnotherBase::YetAnotherBase; // both-note {{declared here}}
445     int OtherVal;
446 
447     constexpr bool doit() const { return Val == OtherVal; }
448   };
449 
450   constexpr YetAnotherDerived Oops(0); // both-error {{must be initialized by a constant expression}} \
451                                        // both-note {{constructor inherited from base class 'YetAnotherBase' cannot be used in a constant expression}}
452 };
453 
454 namespace EmptyCtor {
455   struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
456   constexpr piecewise_construct_t piecewise_construct =
457     piecewise_construct_t();
458 };
459 
460 namespace ConditionalInit {
461   struct S { int a; };
462 
463   constexpr S getS(bool b) {
464     return b ? S{12} : S{13};
465   }
466 
467   static_assert(getS(true).a == 12, "");
468   static_assert(getS(false).a == 13, "");
469 };
470 namespace DeclRefs {
471   struct A{ int m; const int &f = m; };
472 
473   constexpr A a{10};
474   static_assert(a.m == 10, "");
475   static_assert(a.f == 10, "");
476 
477   class Foo {
478   public:
479     int z = 1337;
480     constexpr int a() const {
481       A b{this->z};
482 
483       return b.f;
484     }
485   };
486   constexpr Foo f;
487   static_assert(f.a() == 1337, "");
488 
489 
490   struct B {
491     A a = A{100};
492   };
493   constexpr B b;
494   static_assert(b.a.m == 100, "");
495   static_assert(b.a.f == 100, "");
496 
497   constexpr B b2{};
498   static_assert(b2.a.m == 100, "");
499   static_assert(b2.a.f == 100, "");
500   static_assert(b2.a.f == 101, ""); // both-error {{failed}} \
501                                     // both-note {{evaluates to '100 == 101'}}
502 }
503 
504 namespace PointerArith {
505   struct A {};
506   struct B : A { int n; };
507 
508   B b = {};
509   constexpr A *a1 = &b;
510   constexpr B *b1 = &b + 1;
511   constexpr B *b2 = &b + 0;
512 
513   constexpr A *a2 = &b + 1; // both-error {{must be initialized by a constant expression}} \
514                             // both-note {{cannot access base class of pointer past the end of object}}
515   constexpr const int *pn = &(&b + 1)->n; // both-error {{must be initialized by a constant expression}} \
516                                           // both-note {{cannot access field of pointer past the end of object}}
517 }
518 
519 #if __cplusplus >= 202002L
520 namespace VirtualCalls {
521 namespace Obvious {
522 
523   class A {
524   public:
525     constexpr A(){}
526     constexpr virtual int foo() {
527       return 3;
528     }
529   };
530   class B : public A {
531   public:
532     constexpr int foo() override {
533       return 6;
534     }
535   };
536 
537   constexpr int getFooB(bool b) {
538     A *a;
539     A myA;
540     B myB;
541 
542     if (b)
543       a = &myA;
544     else
545       a = &myB;
546 
547     return a->foo();
548   }
549   static_assert(getFooB(true) == 3, "");
550   static_assert(getFooB(false) == 6, "");
551 }
552 
553 namespace MultipleBases {
554   class A {
555   public:
556     constexpr virtual int getInt() const { return 10; }
557   };
558   class B {
559   public:
560   };
561   class C : public A, public B {
562   public:
563     constexpr int getInt() const override { return 20; }
564   };
565 
566   constexpr int callGetInt(const A& a) { return a.getInt(); }
567   static_assert(callGetInt(C()) == 20, "");
568   static_assert(callGetInt(A()) == 10, "");
569 }
570 
571 namespace Destructors {
572   class Base {
573   public:
574     int i;
575     constexpr Base(int &i) : i(i) {i++;}
576     constexpr virtual ~Base() {i--;}
577   };
578 
579   class Derived : public Base {
580   public:
581     constexpr Derived(int &i) : Base(i) {}
582     constexpr virtual ~Derived() {i--;}
583   };
584 
585   constexpr int test() {
586     int i = 0;
587     Derived d(i);
588     return i;
589   }
590   static_assert(test() == 1);
591 
592   struct S {
593     constexpr S() {}
594     constexpr ~S() { // both-error {{never produces a constant expression}}
595       int i = 1 / 0; // both-warning {{division by zero}} \
596                      // both-note 2{{division by zero}}
597     }
598   };
599   constexpr int testS() {
600     S{}; // both-note {{in call to 'S{}.~S()'}}
601     return 1;
602   }
603   static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
604                                // both-note {{in call to 'testS()'}}
605 }
606 
607 namespace BaseToDerived {
608 namespace A {
609   struct A {};
610   struct B : A { int n; };
611   struct C : B {};
612   C c = {};
613   constexpr C *pb = (C*)((A*)&c + 1); // both-error {{must be initialized by a constant expression}} \
614                                       // both-note {{cannot access derived class of pointer past the end of object}}
615 }
616 namespace B {
617   struct A {};
618   struct Z {};
619   struct B : Z, A {
620     int n;
621    constexpr B() : n(10) {}
622   };
623   struct C : B {
624    constexpr C() : B() {}
625   };
626 
627   constexpr C c = {};
628   constexpr const A *pa = &c;
629   constexpr const C *cp = (C*)pa;
630   constexpr const B *cb = (B*)cp;
631 
632   static_assert(cb->n == 10);
633   static_assert(cp->n == 10);
634 }
635 
636 namespace C {
637   struct Base { int *a; };
638   struct Base2 : Base { int f[12]; };
639 
640   struct Middle1 { int b[3]; };
641   struct Middle2 : Base2 { char c; };
642   struct Middle3 : Middle2 { char g[3]; };
643   struct Middle4 { int f[3]; };
644   struct Middle5 : Middle4, Middle3 { char g2[3]; };
645 
646   struct NotQuiteDerived : Middle1, Middle5 { bool d; };
647   struct Derived : NotQuiteDerived { int e; };
648 
649   constexpr NotQuiteDerived NQD1 = {};
650 
651   constexpr Middle5 *M4 = (Middle5*)((Base2*)&NQD1);
652   static_assert(M4->a == nullptr);
653   static_assert(M4->g2[0] == 0);
654 }
655 }
656 
657 
658 namespace VirtualDtors {
659   class A {
660   public:
661     unsigned &v;
662     constexpr A(unsigned &v) : v(v) {}
663     constexpr virtual ~A() {
664       v |= (1 << 0);
665     }
666   };
667   class B : public A {
668   public:
669     constexpr B(unsigned &v) : A(v) {}
670     constexpr virtual ~B() {
671       v |= (1 << 1);
672     }
673   };
674   class C : public B {
675   public:
676     constexpr C(unsigned &v) : B(v) {}
677     constexpr virtual ~C() {
678       v |= (1 << 2);
679     }
680   };
681 
682   constexpr bool foo() {
683     unsigned a = 0;
684     {
685       C c(a);
686     }
687     return ((a & (1 << 0)) && (a & (1 << 1)) && (a & (1 << 2)));
688   }
689 
690   static_assert(foo());
691 };
692 
693 namespace QualifiedCalls {
694   class A {
695       public:
696       constexpr virtual int foo() const {
697           return 5;
698       }
699   };
700   class B : public A {};
701   class C : public B {
702       public:
703       constexpr int foo() const override {
704           return B::foo(); // B doesn't have a foo(), so this should call A::foo().
705       }
706       constexpr int foo2() const {
707         return this->A::foo();
708       }
709   };
710   constexpr C c;
711   static_assert(c.foo() == 5);
712   static_assert(c.foo2() == 5);
713 
714 
715   struct S {
716     int _c = 0;
717     virtual constexpr int foo() const { return 1; }
718   };
719 
720   struct SS : S {
721     int a;
722     constexpr SS() {
723       a = S::foo();
724     }
725     constexpr int foo() const override {
726       return S::foo();
727     }
728   };
729 
730   constexpr SS ss;
731   static_assert(ss.a == 1);
732 }
733 
734 namespace CtorDtor {
735   struct Base {
736     int i = 0;
737     int j = 0;
738 
739     constexpr Base() : i(func()) {
740       j = func();
741     }
742     constexpr Base(int i) : i(i), j(i) {}
743 
744     constexpr virtual int func() const { return 1; }
745   };
746 
747   struct Derived : Base {
748     constexpr Derived() {}
749     constexpr Derived(int i) : Base(i) {}
750     constexpr int func() const override { return 2; }
751   };
752 
753   struct Derived2 : Derived {
754     constexpr Derived2() : Derived(func()) {} // ref-note {{subexpression not valid in a constant expression}}
755     constexpr int func() const override { return 3; }
756   };
757 
758   constexpr Base B;
759   static_assert(B.i == 1 && B.j == 1, "");
760 
761   constexpr Derived D;
762   static_assert(D.i == 1, ""); // expected-error {{static assertion failed}} \
763                                // expected-note {{2 == 1}}
764   static_assert(D.j == 1, ""); // expected-error {{static assertion failed}} \
765                                // expected-note {{2 == 1}}
766 
767   constexpr Derived2 D2; // ref-error {{must be initialized by a constant expression}} \
768                          // ref-note {{in call to 'Derived2()'}} \
769                          // ref-note 2{{declared here}}
770   static_assert(D2.i == 3, ""); // ref-error {{not an integral constant expression}} \
771                                 // ref-note {{initializer of 'D2' is not a constant expression}}
772   static_assert(D2.j == 3, ""); // ref-error {{not an integral constant expression}} \
773                                 // ref-note {{initializer of 'D2' is not a constant expression}}
774 
775 }
776 
777 namespace VirtualFunctionPointers {
778   struct S {
779     virtual constexpr int func() const { return 1; }
780   };
781 
782   struct Middle : S {
783     constexpr Middle(int i) : i(i) {}
784     int i;
785   };
786 
787   struct Other {
788     constexpr Other(int k) : k(k) {}
789     int k;
790   };
791 
792   struct S2 : Middle, Other {
793     int j;
794     constexpr S2(int i, int j, int k) : Middle(i), Other(k), j(j) {}
795     virtual constexpr int func() const { return i + j + k  + S::func(); }
796   };
797 
798   constexpr S s;
799   constexpr decltype(&S::func) foo = &S::func;
800   constexpr int value = (s.*foo)();
801   static_assert(value == 1);
802 
803 
804   constexpr S2 s2(1, 2, 3);
805   static_assert(s2.i == 1);
806   static_assert(s2.j == 2);
807   static_assert(s2.k == 3);
808 
809   constexpr int value2 = s2.func();
810   constexpr int value3 = (s2.*foo)();
811   static_assert(value3 == 7);
812 
813   constexpr int dynamicDispatch(const S &s) {
814     constexpr decltype(&S::func) SFunc = &S::func;
815 
816     return (s.*SFunc)();
817   }
818 
819   static_assert(dynamicDispatch(s) == 1);
820   static_assert(dynamicDispatch(s2) == 7);
821 };
822 
823 };
824 #endif
825 
826 #if __cplusplus < 202002L
827 namespace VirtualFromBase {
828   struct S1 {
829     virtual int f() const;
830   };
831   struct S2 {
832     virtual int f();
833   };
834   template <typename T> struct X : T {
835     constexpr X() {}
836     double d = 0.0;
837     constexpr int f() { return sizeof(T); }
838   };
839 
840   // Non-virtual f(), OK.
841   constexpr X<X<S1>> xxs1;
842   constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
843   static_assert(p->f() == sizeof(S1), "");
844 
845   // Virtual f(), not OK.
846   constexpr X<X<S2>> xxs2;
847   constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
848   static_assert(q->f() == sizeof(X<S2>), ""); // both-error {{not an integral constant expression}} \
849                                               // both-note {{cannot evaluate call to virtual function}}
850 }
851 #endif
852 
853 namespace CompositeDefaultArgs {
854   struct Foo {
855     int a;
856     int b;
857     constexpr Foo() : a(12), b(13) {}
858   };
859 
860   class Bar {
861   public:
862     bool B = false;
863 
864     constexpr int someFunc(Foo F = Foo()) {
865       this->B = true;
866       return 5;
867     }
868   };
869 
870   constexpr bool testMe() {
871     Bar B;
872     B.someFunc();
873     return B.B;
874   }
875   static_assert(testMe(), "");
876 }
877 
878 constexpr bool BPand(BoolPair bp) {
879   return bp.first && bp.second;
880 }
881 static_assert(BPand(BoolPair{true, false}) == false, "");
882 
883 namespace TemporaryObjectExpr {
884   struct F {
885     int a;
886     constexpr F() : a(12) {}
887   };
888   constexpr int foo(F f) {
889     return 0;
890   }
891   static_assert(foo(F()) == 0, "");
892 }
893 
894   namespace ZeroInit {
895   struct F {
896     int a;
897   };
898 
899   namespace Simple {
900     struct A {
901       char a;
902       bool b;
903       int c[4];
904       float d;
905     };
906     constexpr int foo(A x) {
907       return x.a + static_cast<int>(x.b) + x.c[0] + x.c[3] + static_cast<int>(x.d);
908     }
909     static_assert(foo(A()) == 0, "");
910   }
911 
912   namespace Inheritance {
913     struct F2 : F {
914       float f;
915     };
916 
917     constexpr int foo(F2 f) {
918       return (int)f.f + f.a;
919     }
920     static_assert(foo(F2()) == 0, "");
921   }
922 
923   namespace BitFields {
924     struct F {
925       unsigned a : 6;
926     };
927     constexpr int foo(F f) {
928       return f.a;
929     }
930     static_assert(foo(F()) == 0, "");
931   }
932 
933   namespace Nested {
934     struct F2 {
935       float f;
936       char c;
937     };
938 
939     struct F {
940       F2 f2;
941       int i;
942     };
943 
944     constexpr int foo(F f) {
945       return f.i + f.f2.f + f.f2.c;
946     }
947     static_assert(foo(F()) == 0, "");
948   }
949 
950   namespace CompositeArrays {
951     struct F2 {
952       float f;
953       char c;
954     };
955 
956     struct F {
957       F2 f2[2];
958       int i;
959     };
960 
961     constexpr int foo(F f) {
962       return f.i + f.f2[0].f + f.f2[0].c + f.f2[1].f + f.f2[1].c;
963     }
964     static_assert(foo(F()) == 0, "");
965   }
966 
967 #if __cplusplus > 201402L
968   namespace Unions {
969     struct F {
970       union {
971         int a;
972         char c[4];
973         float f;
974       } U;
975       int i;
976     };
977 
978     constexpr int foo(F f) {
979       return f.i + f.U.f; // both-note {{read of member 'f' of union with active member 'a'}}
980     }
981     static_assert(foo(F()) == 0, ""); // both-error {{not an integral constant expression}} \
982                                       // both-note {{in call to}}
983   }
984 #endif
985 
986 #if __cplusplus >= 202002L
987   namespace Failure {
988     struct S {
989       int a;
990       F f{12};
991     };
992     constexpr int foo(S x) {
993       return x.a;
994     }
995     static_assert(foo(S()) == 0, "");
996   };
997 #endif
998 }
999 
1000 #if __cplusplus >= 202002L
1001 namespace ParenInit {
1002   struct A {
1003     int a;
1004   };
1005 
1006   struct B : A {
1007     int b;
1008   };
1009 
1010   constexpr B b(A(1),2);
1011 
1012 
1013   struct O {
1014     int &&j;
1015   };
1016 
1017   /// Not constexpr!
1018   O o1(0); // both-warning {{temporary whose address is used as value of}}
1019   // FIXME: the secondary warning message is bogus, would be nice to suppress it.
1020   constinit O o2(0); // both-error {{variable does not have a constant initializer}} \
1021                      // both-note {{required by 'constinit' specifier}} \
1022                      // both-note {{reference to temporary is not a constant expression}} \
1023                      // both-note {{temporary created here}} \
1024                      // both-warning {{temporary whose address is used as value}}
1025 
1026 
1027   /// Initializing an array.
1028   constexpr void bar(int i, int j) {
1029     int arr[4](i, j);
1030   }
1031 }
1032 #endif
1033 
1034 namespace DelegatingConstructors {
1035   struct S {
1036     int a;
1037     constexpr S() : S(10) {}
1038     constexpr S(int a) : a(a) {}
1039   };
1040   constexpr S s = {};
1041   static_assert(s.a == 10, "");
1042 
1043   struct B {
1044     int a;
1045     int b;
1046 
1047     constexpr B(int a) : a(a), b(a + 2) {}
1048   };
1049   struct A : B {
1050     constexpr A() : B(10) {};
1051   };
1052   constexpr A d4 = {};
1053   static_assert(d4.a == 10, "");
1054   static_assert(d4.b == 12, "");
1055 }
1056 
1057 namespace AccessOnNullptr {
1058   struct F {
1059     int a;
1060   };
1061 
1062   constexpr int a() { // both-error {{never produces a constant expression}}
1063     F *f = nullptr;
1064 
1065     f->a = 0; // both-note 2{{cannot access field of null pointer}}
1066     return f->a;
1067   }
1068   static_assert(a() == 0, ""); // both-error {{not an integral constant expression}} \
1069                                // both-note {{in call to 'a()'}}
1070 
1071   constexpr int a2() { // both-error {{never produces a constant expression}}
1072     F *f = nullptr;
1073 
1074 
1075     const int *a = &(f->a); // both-note 2{{cannot access field of null pointer}}
1076     return f->a;
1077   }
1078   static_assert(a2() == 0, ""); // both-error {{not an integral constant expression}} \
1079                                 // both-note {{in call to 'a2()'}}
1080 }
1081 
1082 namespace IndirectFieldInit {
1083 #if __cplusplus >= 202002L
1084   /// Primitive.
1085   struct Nested1 {
1086     struct {
1087       int first;
1088     };
1089     int x;
1090     constexpr Nested1(int x) : first(12), x() { x = 4; }
1091     constexpr Nested1() : Nested1(42) {}
1092   };
1093   constexpr Nested1 N1{};
1094   static_assert(N1.first == 12, "");
1095 
1096   /// Composite.
1097   struct Nested2 {
1098     struct First { int x = 42; };
1099     struct {
1100       First first;
1101     };
1102     int x;
1103     constexpr Nested2(int x) : first(12), x() { x = 4; }
1104     constexpr Nested2() : Nested2(42) {}
1105   };
1106   constexpr Nested2 N2{};
1107   static_assert(N2.first.x == 12, "");
1108 
1109   /// Bitfield.
1110   struct Nested3 {
1111     struct {
1112       unsigned first : 2;
1113     };
1114     int x;
1115     constexpr Nested3(int x) : first(3), x() { x = 4; }
1116     constexpr Nested3() : Nested3(42) {}
1117   };
1118 
1119   constexpr Nested3 N3{};
1120   static_assert(N3.first == 3, "");
1121 
1122   /// Test that we get the offset right if the
1123   /// record has a base.
1124   struct Nested4Base {
1125     int a;
1126     int b;
1127     char c;
1128   };
1129   struct Nested4 : Nested4Base{
1130     struct {
1131       int first;
1132     };
1133     int x;
1134     constexpr Nested4(int x) : first(123), x() { a = 1; b = 2; c = 3; x = 4; }
1135     constexpr Nested4() : Nested4(42) {}
1136   };
1137   constexpr Nested4 N4{};
1138   static_assert(N4.first == 123, "");
1139 
1140   struct S {
1141     struct {
1142       int x, y;
1143     };
1144 
1145     constexpr S(int x_, int y_) : x(x_), y(y_) {}
1146   };
1147 
1148   constexpr S s(1, 2);
1149   static_assert(s.x == 1 && s.y == 2);
1150 
1151   struct S2 {
1152     int a;
1153     struct {
1154       int b;
1155       struct {
1156         int x, y;
1157       };
1158     };
1159 
1160     constexpr S2(int x_, int y_) : a(3), b(4), x(x_), y(y_) {}
1161   };
1162 
1163   constexpr S2 s2(1, 2);
1164   static_assert(s2.x == 1 && s2.y == 2 && s2.a == 3 && s2.b == 4);
1165 
1166 #endif
1167 }
1168 
1169 namespace InheritedConstructor {
1170   namespace PR47555 {
1171     struct A {
1172       int c;
1173       int d;
1174       constexpr A(int c, int d) : c(c), d(d){}
1175     };
1176     struct B : A { using A::A; };
1177 
1178     constexpr B b = {13, 1};
1179     static_assert(b.c == 13, "");
1180     static_assert(b.d == 1, "");
1181   }
1182 
1183   namespace PR47555_2 {
1184     struct A {
1185       int c;
1186       int d;
1187       double e;
1188       constexpr A(int c, int &d, double e) : c(c), d(++d), e(e){}
1189     };
1190     struct B : A { using A::A; };
1191 
1192     constexpr int f() {
1193       int a = 10;
1194       B b = {10, a, 40.0};
1195       return a;
1196     }
1197     static_assert(f() == 11, "");
1198   }
1199 
1200   namespace AaronsTest {
1201     struct T {
1202       constexpr T(float) {}
1203     };
1204 
1205     struct Base {
1206       constexpr Base(T t = 1.0f) {}
1207       constexpr Base(float) {}
1208     };
1209 
1210     struct FirstMiddle : Base {
1211       using Base::Base;
1212       constexpr FirstMiddle() : Base(2.0f) {}
1213     };
1214 
1215     struct SecondMiddle : Base {
1216       constexpr SecondMiddle() : Base(3.0f) {}
1217       constexpr SecondMiddle(T t) : Base(t) {}
1218     };
1219 
1220     struct S : FirstMiddle, SecondMiddle {
1221       using FirstMiddle::FirstMiddle;
1222       constexpr S(int i) : S(4.0f) {}
1223     };
1224 
1225     constexpr S s(1);
1226   }
1227 }
1228 
1229 namespace InvalidCtorInitializer {
1230   struct X {
1231     int Y;
1232     constexpr X()
1233         : Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
1234   };
1235   // no crash on evaluating the constexpr ctor.
1236   constexpr int Z = X().Y; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}}
1237 }
1238 
1239 extern int f(); // both-note {{here}}
1240 struct HasNonConstExprMemInit {
1241   int x = f(); // both-note {{non-constexpr function}}
1242   constexpr HasNonConstExprMemInit() {} // both-error {{never produces a constant expression}}
1243 };
1244 
1245 namespace {
1246   template <class Tp, Tp v>
1247   struct integral_constant {
1248     static const Tp value = v;
1249   };
1250 
1251   template <class Tp, Tp v>
1252   const Tp integral_constant<Tp, v>::value;
1253 
1254   typedef integral_constant<bool, true> true_type;
1255   typedef integral_constant<bool, false> false_type;
1256 
1257   /// This might look innocent, but we get an evaluateAsInitializer call for the
1258   /// static bool member before evaluating the first static_assert, but we do NOT
1259   /// get such a call for the second one. So the second one needs to lazily visit
1260   /// the data member itself.
1261   static_assert(true_type::value, "");
1262   static_assert(true_type::value, "");
1263 }
1264 
1265 #if __cplusplus >= 202002L
1266 namespace {
1267   /// Used to crash because the CXXDefaultInitExpr is of compound type.
1268   struct A {
1269     int &x;
1270     constexpr ~A() { --x; }
1271   };
1272   struct B {
1273     int &x;
1274     const A &a = A{x};
1275   };
1276   constexpr int a() {
1277     int x = 1;
1278     int f = B{x}.x;
1279     B{x}; // both-warning {{expression result unused}}
1280 
1281     return 1;
1282   }
1283 }
1284 #endif
1285 
1286 namespace pr18633 {
1287   struct A1 {
1288     static const int sz;
1289     static const int sz2;
1290   };
1291   const int A1::sz2 = 11;
1292   template<typename T>
1293   void func () {
1294     int arr[A1::sz];
1295     // both-warning@-1 {{variable length arrays in C++ are a Clang extension}}
1296     // both-note@-2 {{initializer of 'sz' is unknown}}
1297     // both-note@-9 {{declared here}}
1298   }
1299   template<typename T>
1300   void func2 () {
1301     int arr[A1::sz2];
1302   }
1303   const int A1::sz = 12;
1304   void func2() {
1305     func<int>();
1306     func2<int>();
1307   }
1308 }
1309 
1310 namespace {
1311   struct F {
1312     static constexpr int Z = 12;
1313   };
1314   F f;
1315   static_assert(f.Z == 12, "");
1316 }
1317 
1318 namespace UnnamedBitFields {
1319   struct A {
1320     int : 1;
1321     double f;
1322     int : 1;
1323     char c;
1324   };
1325 
1326   constexpr A a = (A){1.0, 'a'};
1327   static_assert(a.f == 1.0, "");
1328   static_assert(a.c == 'a', "");
1329 }
1330 
1331 namespace VirtualBases {
1332   /// This used to crash.
1333   namespace One {
1334     class A {
1335     protected:
1336       int x;
1337     };
1338     class B : public virtual A {
1339     public:
1340       int getX() { return x; } // both-note {{declared here}}
1341     };
1342 
1343     class DV : virtual public B{};
1344 
1345     void foo() {
1346       DV b;
1347       int a[b.getX()]; // both-warning {{variable length arrays}} \
1348                        // both-note {{non-constexpr function 'getX' cannot be used}}
1349     }
1350   }
1351 
1352   namespace Two {
1353     struct U { int n; };
1354     struct A : virtual U { int n; };
1355     struct B : A {};
1356     B a;
1357     static_assert((U*)(A*)(&a) == (U*)(&a), "");
1358 
1359     struct C : virtual A {};
1360     struct D : B, C {};
1361     D d;
1362     constexpr B *p = &d;
1363     constexpr C *q = &d;
1364     static_assert((A*)p == (A*)q, ""); // both-error {{failed}}
1365   }
1366 
1367   namespace Three {
1368     struct U { int n; };
1369     struct V : U { int n; };
1370     struct A : virtual V { int n; };
1371     struct Aa { int n; };
1372     struct B : virtual A, Aa {};
1373 
1374     struct C : virtual A, Aa {};
1375 
1376     struct D : B, C {};
1377 
1378     D d;
1379 
1380     constexpr B *p = &d;
1381     constexpr C *q = &d;
1382 
1383     static_assert((void*)p != (void*)q, "");
1384     static_assert((A*)p == (A*)q, "");
1385     static_assert((Aa*)p != (Aa*)q, "");
1386 
1387     constexpr V *v = p;
1388     constexpr V *w = q;
1389     constexpr V *x = (A*)p;
1390     static_assert(v == w, "");
1391     static_assert(v == x, "");
1392 
1393     static_assert((U*)&d == p, "");
1394     static_assert((U*)&d == q, "");
1395     static_assert((U*)&d == v, "");
1396     static_assert((U*)&d == w, "");
1397     static_assert((U*)&d == x, "");
1398 
1399     struct X {};
1400     struct Y1 : virtual X {};
1401     struct Y2 : X {};
1402     struct Z : Y1, Y2 {};
1403     Z z;
1404     static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
1405   }
1406 }
1407 
1408 namespace ZeroInit {
1409   struct S3 {
1410     S3() = default;
1411     S3(const S3&) = default;
1412     S3(S3&&) = default;
1413     constexpr S3(int n) : n(n) {}
1414     int n;
1415   };
1416   constexpr S3 s3d; // both-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
1417   static_assert(s3d.n == 0, "");
1418 
1419   struct P {
1420     int a = 10;
1421   };
1422   static_assert(P().a == 10, "");
1423 }
1424 
1425 namespace {
1426 #if __cplusplus >= 202002L
1427   struct C {
1428     template <unsigned N> constexpr C(const char (&)[N]) : n(N) {}
1429     unsigned n;
1430   };
1431   template <C c>
1432   constexpr auto operator""_c() { return c.n; }
1433 
1434   constexpr auto waldo = "abc"_c;
1435   static_assert(waldo == 4, "");
1436 #endif
1437 }
1438 
1439 
1440 namespace TemporaryWithInvalidDestructor {
1441 #if __cplusplus >= 202002L
1442   struct A {
1443     bool a = true;
1444     constexpr ~A() noexcept(false) { // both-error {{never produces a constant expression}}
1445       throw; // both-note 2{{not valid in a constant expression}} \
1446              // both-error {{cannot use 'throw' with exceptions disabled}}
1447     }
1448   };
1449   static_assert(A().a, ""); // both-error {{not an integral constant expression}} \
1450                         // both-note {{in call to}}
1451 #endif
1452 }
1453 
1454 namespace IgnoredCtorWithZeroInit {
1455   struct S {
1456     int a;
1457   };
1458 
1459   bool get_status() {
1460     return (S(), true);
1461   }
1462 }
1463 
1464 #if __cplusplus >= 202002L
1465 namespace VirtOperator {
1466   /// This used to crash because it's a virtual CXXOperatorCallExpr.
1467   struct B {
1468     virtual constexpr bool operator==(const B&) const { return true; }
1469   };
1470   struct D : B {
1471     constexpr bool operator==(const B&) const override{ return false; } // both-note {{operator}}
1472   };
1473   constexpr bool cmp_base_derived = D() == D(); // both-warning {{ambiguous}}
1474 }
1475 
1476 namespace FloatAPValue {
1477   struct ClassTemplateArg {
1478     int a;
1479     float f;
1480   };
1481   template<ClassTemplateArg A> struct ClassTemplateArgTemplate {
1482     static constexpr const ClassTemplateArg &Arg = A;
1483   };
1484   ClassTemplateArgTemplate<ClassTemplateArg{1, 2.0f}> ClassTemplateArgObj;
1485   template<const ClassTemplateArg&> struct ClassTemplateArgRefTemplate {};
1486   ClassTemplateArgRefTemplate<ClassTemplateArgObj.Arg> ClassTemplateArgRefObj;
1487 }
1488 #endif
1489 
1490 namespace LocalWithThisPtrInit {
1491   struct S {
1492     int i;
1493     int *p = &i;
1494   };
1495   constexpr int foo() {
1496     S s{2};
1497     return *s.p;
1498   }
1499   static_assert(foo() == 2, "");
1500 }
1501 
1502 namespace OnePastEndAndBack {
1503   struct Base {
1504     constexpr Base() {}
1505     int n = 0;
1506   };
1507 
1508   constexpr Base a;
1509   constexpr const Base *c = &a + 1;
1510   constexpr const Base *d = c - 1;
1511   static_assert(d == &a, "");
1512 }
1513 
1514 namespace BitSet {
1515   class Bitset {
1516     unsigned Bit = 0;
1517 
1518   public:
1519     constexpr Bitset() {
1520       int Init[2] = {1,2};
1521       for (auto I : Init)
1522         set(I);
1523     }
1524     constexpr void set(unsigned I) {
1525       this->Bit++;
1526       this->Bit = 1u << 1;
1527     }
1528   };
1529 
1530   struct ArchInfo {
1531     Bitset DefaultExts;
1532   };
1533 
1534   constexpr ArchInfo ARMV8A = {
1535     Bitset()
1536   };
1537 }
1538 
1539 namespace ArrayInitChain {
1540   struct StringLiteral {
1541     const char *S;
1542   };
1543 
1544   struct CustomOperandVal {
1545     StringLiteral Str;
1546     unsigned Width;
1547     unsigned Mask = Width + 1;
1548   };
1549 
1550   constexpr CustomOperandVal A[] = {
1551     {},
1552     {{"depctr_hold_cnt"},  12,   13},
1553   };
1554   static_assert(A[0].Str.S == nullptr, "");
1555   static_assert(A[0].Width == 0, "");
1556   static_assert(A[0].Mask == 1, "");
1557 
1558   static_assert(A[1].Width == 12, "");
1559   static_assert(A[1].Mask == 13, "");
1560 }
1561 
1562 #if __cplusplus >= 202002L
1563 namespace ctorOverrider {
1564   // Ensure that we pick the right final overrider during construction.
1565   struct A {
1566     virtual constexpr char f() const { return 'A'; }
1567     char a = f();
1568   };
1569 
1570   struct Covariant1 {
1571     A d;
1572   };
1573 
1574   constexpr Covariant1 cb;
1575 }
1576 #endif
1577 
1578 #if __cplusplus >= 202002L
1579 namespace VirtDtor {
1580   struct X { char *p; constexpr ~X() { *p++ = 'X'; } };
1581   struct Y : X { int y; virtual constexpr ~Y() { *p++ = 'Y'; } };
1582   struct Z : Y { int z; constexpr ~Z() override { *p++ = 'Z'; } };
1583 
1584   union VU {
1585     constexpr VU() : z() {}
1586     constexpr ~VU() {}
1587     Z z;
1588   };
1589 
1590   constexpr char virt_dtor(int mode, const char *expected) {
1591     char buff[4] = {};
1592     VU vu;
1593     vu.z.p = buff;
1594 
1595     ((Y&)vu.z).~Y();
1596     return true;
1597   }
1598   static_assert(virt_dtor(0, "ZYX"));
1599 }
1600 
1601 namespace DtorDestroysFieldsAfterSelf {
1602     struct  S {
1603       int a = 10;
1604       constexpr ~S() {
1605         a = 0;
1606       }
1607 
1608     };
1609     struct F {
1610       S s;
1611       int a;
1612       int &b;
1613       constexpr F(int a, int &b) : a(a), b(b) {}
1614       constexpr ~F() {
1615         b += s.a;
1616       }
1617     };
1618 
1619   constexpr int foo() {
1620     int a = 10;
1621     int b = 5;
1622     {
1623       F f(a, b);
1624     }
1625 
1626     return b;
1627   }
1628 
1629   static_assert(foo() == 15);
1630 }
1631 #endif
1632 
1633 namespace ExprWithCleanups {
1634   struct A { A(); ~A(); int get(); };
1635   constexpr int get() {return false ? A().get() : 1;}
1636   static_assert(get() == 1, "");
1637 
1638 
1639   struct S {
1640     int V;
1641     constexpr S(int V) : V(V) {}
1642     constexpr int get() {
1643       return V;
1644     }
1645   };
1646   constexpr int get(bool b) {
1647     S a = b ? S(1) : S(2);
1648 
1649     return a.get();
1650   }
1651   static_assert(get(true) == 1, "");
1652   static_assert(get(false) == 2, "");
1653 
1654 
1655   constexpr auto F = true ? 1i : 2i;
1656   static_assert(F == 1i, "");
1657 }
1658 
1659 namespace NullptrUpcast {
1660   struct A {};
1661   struct B : A { int n; };
1662   constexpr B *nb = nullptr;
1663   constexpr A &ra = *nb; // both-error {{constant expression}} \
1664                          // both-note {{cannot access base class of null pointer}}
1665 }
1666 
1667 namespace NonConst {
1668   template <int I>
1669   struct S {
1670     static constexpr int Size = I;
1671     constexpr int getSize() const { return I; }
1672     explicit S(int a) {}
1673   };
1674 
1675   void func() {
1676     int a,b ;
1677     const S<10> s{a};
1678     static_assert(s.getSize() == 10, "");
1679   }
1680 }
1681 
1682 namespace ExplicitThisInTemporary {
1683   struct B { B *p = this; };
1684   constexpr bool g(B b) { return &b == b.p; }
1685   static_assert(g({}), "");
1686 }
1687 
1688 namespace IgnoredMemberExpr {
1689   class A {
1690   public:
1691     int a;
1692   };
1693   class B : public A {
1694   public:
1695     constexpr int foo() {
1696       a; // both-warning {{expression result unused}}
1697       return 0;
1698     }
1699   };
1700   static_assert(B{}.foo() == 0, "");
1701 }
1702