Debian Codegraph @

clang/test/AST/Interp/records.cpp

raw
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{}; // ref-note {{in call to 'S{}.~S()'}} \
338 // expected-note {{in call to '&S{}->~S()'}}
339 return 12;
340 }
341 static_assert(f() == 12); // both-error {{not an integral constant expression}} \
342 // both-note {{in call to 'f()'}}
343
344
345 #endif
346 }
347
348 #if __cplusplus >= 201703L
349 namespace BaseInit {
350 class _A {public: int a;};
351 class _B : public _A {};
352 class _C : public _B {};
353
354 constexpr _C c{12};
355 constexpr const _B &b = c;
356 static_assert(b.a == 12);
357
358 class A {public: int a;};
359 class B : public A {};
360 class C : public A {};
361 class D : public B, public C {};
362
363 // This initializes D::B::A::a and not D::C::A::a.
364 constexpr D d{12};
365 static_assert(d.B::a == 12);
366 static_assert(d.C::a == 0);
367 };
368 #endif
369
370 namespace MI {
371 class A {
372 public:
373 int a;
374 constexpr A(int a) : a(a) {}
375 };
376
377 class B {
378 public:
379 int b;
380 constexpr B(int b) : b(b) {}
381 };
382
383 class C : public A, public B {
384 public:
385 constexpr C() : A(10), B(20) {}
386 };
387 constexpr C c = {};
388 static_assert(c.a == 10, "");
389 static_assert(c.b == 20, "");
390
391 constexpr const A *aPointer = &c;
392 constexpr const B *bPointer = &c;
393
394 class D : private A, private B {
395 public:
396 constexpr D() : A(20), B(30) {}
397 constexpr int getA() const { return a; }
398 constexpr int getB() const { return b; }
399 };
400 constexpr D d = {};
401 static_assert(d.getA() == 20, "");
402 static_assert(d.getB() == 30, "");
403 };
404
405 namespace DeriveFailures {
406 #if __cplusplus < 202002L
407 struct Base { // both-note {{declared here}} \
408 // ref-note {{declared here}}
409 int Val;
410 };
411
412 struct Derived : Base {
413 int OtherVal;
414
415 constexpr Derived(int i) : OtherVal(i) {} // ref-error {{never produces a constant expression}} \
416 // both-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}} \
417 // ref-note {{non-constexpr constructor 'Base' cannot be used in a constant expression}}
418 };
419
420 constexpr Derived D(12); // both-error {{must be initialized by a constant expression}} \
421 // both-note {{in call to 'Derived(12)'}} \
422 // both-note {{declared here}}
423
424 static_assert(D.Val == 0, ""); // both-error {{not an integral constant expression}} \
425 // both-note {{initializer of 'D' is not a constant expression}}
426 #endif
427
428 struct AnotherBase {
429 int Val;
430 constexpr AnotherBase(int i) : Val(12 / i) {} // both-note {{division by zero}}
431 };
432
433 struct AnotherDerived : AnotherBase {
434 constexpr AnotherDerived(int i) : AnotherBase(i) {}
435 };
436 constexpr AnotherBase Derp(0); // both-error {{must be initialized by a constant expression}} \
437 // both-note {{in call to 'AnotherBase(0)'}}
438
439 struct YetAnotherBase {
440 int Val;
441 constexpr YetAnotherBase(int i) : Val(i) {}
442 };
443
444 struct YetAnotherDerived : YetAnotherBase {
445 using YetAnotherBase::YetAnotherBase; // both-note {{declared here}}
446 int OtherVal;
447
448 constexpr bool doit() const { return Val == OtherVal; }
449 };
450
451 constexpr YetAnotherDerived Oops(0); // both-error {{must be initialized by a constant expression}} \
452 // both-note {{constructor inherited from base class 'YetAnotherBase' cannot be used in a constant expression}}
453 };
454
455 namespace EmptyCtor {
456 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
457 constexpr piecewise_construct_t piecewise_construct =
458 piecewise_construct_t();
459 };
460
461 namespace ConditionalInit {
462 struct S { int a; };
463
464 constexpr S getS(bool b) {
465 return b ? S{12} : S{13};
466 }
467
468 static_assert(getS(true).a == 12, "");
469 static_assert(getS(false).a == 13, "");
470 };
471 namespace DeclRefs {
472 struct A{ int m; const int &f = m; };
473
474 constexpr A a{10};
475 static_assert(a.m == 10, "");
476 static_assert(a.f == 10, "");
477
478 class Foo {
479 public:
480 int z = 1337;
481 constexpr int a() const {
482 A b{this->z};
483
484 return b.f;
485 }
486 };
487 constexpr Foo f;
488 static_assert(f.a() == 1337, "");
489
490
491 struct B {
492 A a = A{100};
493 };
494 constexpr B b;
495 static_assert(b.a.m == 100, "");
496 static_assert(b.a.f == 100, "");
497
498 constexpr B b2{};
499 static_assert(b2.a.m == 100, "");
500 static_assert(b2.a.f == 100, "");
501 static_assert(b2.a.f == 101, ""); // both-error {{failed}} \
502 // both-note {{evaluates to '100 == 101'}}
503 }
504
505 namespace PointerArith {
506 struct A {};
507 struct B : A { int n; };
508
509 B b = {};
510 constexpr A *a1 = &b;
511 constexpr B *b1 = &b + 1;
512 constexpr B *b2 = &b + 0;
513
514 constexpr A *a2 = &b + 1; // both-error {{must be initialized by a constant expression}} \
515 // both-note {{cannot access base class of pointer past the end of object}}
516 constexpr const int *pn = &(&b + 1)->n; // both-error {{must be initialized by a constant expression}} \
517 // both-note {{cannot access field of pointer past the end of object}}
518 }
519
520 #if __cplusplus >= 202002L
521 namespace VirtualCalls {
522 namespace Obvious {
523
524 class A {
525 public:
526 constexpr A(){}
527 constexpr virtual int foo() {
528 return 3;
529 }
530 };
531 class B : public A {
532 public:
533 constexpr int foo() override {
534 return 6;
535 }
536 };
537
538 constexpr int getFooB(bool b) {
539 A *a;
540 A myA;
541 B myB;
542
543 if (b)
544 a = &myA;
545 else
546 a = &myB;
547
548 return a->foo();
549 }
550 static_assert(getFooB(true) == 3, "");
551 static_assert(getFooB(false) == 6, "");
552 }
553
554 namespace MultipleBases {
555 class A {
556 public:
557 constexpr virtual int getInt() const { return 10; }
558 };
559 class B {
560 public:
561 };
562 class C : public A, public B {
563 public:
564 constexpr int getInt() const override { return 20; }
565 };
566
567 constexpr int callGetInt(const A& a) { return a.getInt(); }
568 static_assert(callGetInt(C()) == 20, "");
569 static_assert(callGetInt(A()) == 10, "");
570 }
571
572 namespace Destructors {
573 class Base {
574 public:
575 int i;
576 constexpr Base(int &i) : i(i) {i++;}
577 constexpr virtual ~Base() {i--;}
578 };
579
580 class Derived : public Base {
581 public:
582 constexpr Derived(int &i) : Base(i) {}
583 constexpr virtual ~Derived() {i--;}
584 };
585
586 constexpr int test() {
587 int i = 0;
588 Derived d(i);
589 return i;
590 }
591 static_assert(test() == 1);
592
593 struct S {
594 constexpr S() {}
595 constexpr ~S() { // both-error {{never produces a constant expression}}
596 int i = 1 / 0; // both-warning {{division by zero}} \
597 // both-note 2{{division by zero}}
598 }
599 };
600 constexpr int testS() {
601 S{}; // ref-note {{in call to 'S{}.~S()'}} \
602 // expected-note {{in call to '&S{}->~S()'}}
603 return 1;
604 }
605 static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
606 // both-note {{in call to 'testS()'}}
607 }
608
609 namespace BaseToDerived {
610 namespace A {
611 struct A {};
612 struct B : A { int n; };
613 struct C : B {};
614 C c = {};
615 constexpr C *pb = (C*)((A*)&c + 1); // both-error {{must be initialized by a constant expression}} \
616 // both-note {{cannot access derived class of pointer past the end of object}}
617 }
618 namespace B {
619 struct A {};
620 struct Z {};
621 struct B : Z, A {
622 int n;
623 constexpr B() : n(10) {}
624 };
625 struct C : B {
626 constexpr C() : B() {}
627 };
628
629 constexpr C c = {};
630 constexpr const A *pa = &c;
631 constexpr const C *cp = (C*)pa;
632 constexpr const B *cb = (B*)cp;
633
634 static_assert(cb->n == 10);
635 static_assert(cp->n == 10);
636 }
637
638 namespace C {
639 struct Base { int *a; };
640 struct Base2 : Base { int f[12]; };
641
642 struct Middle1 { int b[3]; };
643 struct Middle2 : Base2 { char c; };
644 struct Middle3 : Middle2 { char g[3]; };
645 struct Middle4 { int f[3]; };
646 struct Middle5 : Middle4, Middle3 { char g2[3]; };
647
648 struct NotQuiteDerived : Middle1, Middle5 { bool d; };
649 struct Derived : NotQuiteDerived { int e; };
650
651 constexpr NotQuiteDerived NQD1 = {};
652
653 constexpr Middle5 *M4 = (Middle5*)((Base2*)&NQD1);
654 static_assert(M4->a == nullptr);
655 static_assert(M4->g2[0] == 0);
656 }
657 }
658
659
660 namespace VirtualDtors {
661 class A {
662 public:
663 unsigned &v;
664 constexpr A(unsigned &v) : v(v) {}
665 constexpr virtual ~A() {
666 v |= (1 << 0);
667 }
668 };
669 class B : public A {
670 public:
671 constexpr B(unsigned &v) : A(v) {}
672 constexpr virtual ~B() {
673 v |= (1 << 1);
674 }
675 };
676 class C : public B {
677 public:
678 constexpr C(unsigned &v) : B(v) {}
679 constexpr virtual ~C() {
680 v |= (1 << 2);
681 }
682 };
683
684 constexpr bool foo() {
685 unsigned a = 0;
686 {
687 C c(a);
688 }
689 return ((a & (1 << 0)) && (a & (1 << 1)) && (a & (1 << 2)));
690 }
691
692 static_assert(foo());
693 };
694
695 namespace QualifiedCalls {
696 class A {
697 public:
698 constexpr virtual int foo() const {
699 return 5;
700 }
701 };
702 class B : public A {};
703 class C : public B {
704 public:
705 constexpr int foo() const override {
706 return B::foo(); // B doesn't have a foo(), so this should call A::foo().
707 }
708 constexpr int foo2() const {
709 return this->A::foo();
710 }
711 };
712 constexpr C c;
713 static_assert(c.foo() == 5);
714 static_assert(c.foo2() == 5);
715
716
717 struct S {
718 int _c = 0;
719 virtual constexpr int foo() const { return 1; }
720 };
721
722 struct SS : S {
723 int a;
724 constexpr SS() {
725 a = S::foo();
726 }
727 constexpr int foo() const override {
728 return S::foo();
729 }
730 };
731
732 constexpr SS ss;
733 static_assert(ss.a == 1);
734 }
735
736 namespace CtorDtor {
737 struct Base {
738 int i = 0;
739 int j = 0;
740
741 constexpr Base() : i(func()) {
742 j = func();
743 }
744 constexpr Base(int i) : i(i), j(i) {}
745
746 constexpr virtual int func() const { return 1; }
747 };
748
749 struct Derived : Base {
750 constexpr Derived() {}
751 constexpr Derived(int i) : Base(i) {}
752 constexpr int func() const override { return 2; }
753 };
754
755 struct Derived2 : Derived {
756 constexpr Derived2() : Derived(func()) {} // ref-note {{subexpression not valid in a constant expression}}
757 constexpr int func() const override { return 3; }
758 };
759
760 constexpr Base B;
761 static_assert(B.i == 1 && B.j == 1, "");
762
763 constexpr Derived D;
764 static_assert(D.i == 1, ""); // expected-error {{static assertion failed}} \
765 // expected-note {{2 == 1}}
766 static_assert(D.j == 1, ""); // expected-error {{static assertion failed}} \
767 // expected-note {{2 == 1}}
768
769 constexpr Derived2 D2; // ref-error {{must be initialized by a constant expression}} \
770 // ref-note {{in call to 'Derived2()'}} \
771 // ref-note 2{{declared here}}
772 static_assert(D2.i == 3, ""); // ref-error {{not an integral constant expression}} \
773 // ref-note {{initializer of 'D2' is not a constant expression}}
774 static_assert(D2.j == 3, ""); // ref-error {{not an integral constant expression}} \
775 // ref-note {{initializer of 'D2' is not a constant expression}}
776
777 }
778
779 namespace VirtualFunctionPointers {
780 struct S {
781 virtual constexpr int func() const { return 1; }
782 };
783
784 struct Middle : S {
785 constexpr Middle(int i) : i(i) {}
786 int i;
787 };
788
789 struct Other {
790 constexpr Other(int k) : k(k) {}
791 int k;
792 };
793
794 struct S2 : Middle, Other {
795 int j;
796 constexpr S2(int i, int j, int k) : Middle(i), Other(k), j(j) {}
797 virtual constexpr int func() const { return i + j + k + S::func(); }
798 };
799
800 constexpr S s;
801 constexpr decltype(&S::func) foo = &S::func;
802 constexpr int value = (s.*foo)();
803 static_assert(value == 1);
804
805
806 constexpr S2 s2(1, 2, 3);
807 static_assert(s2.i == 1);
808 static_assert(s2.j == 2);
809 static_assert(s2.k == 3);
810
811 constexpr int value2 = s2.func();
812 constexpr int value3 = (s2.*foo)();
813 static_assert(value3 == 7);
814
815 constexpr int dynamicDispatch(const S &s) {
816 constexpr decltype(&S::func) SFunc = &S::func;
817
818 return (s.*SFunc)();
819 }
820
821 static_assert(dynamicDispatch(s) == 1);
822 static_assert(dynamicDispatch(s2) == 7);
823 };
824
825 };
826 #endif
827
828 #if __cplusplus < 202002L
829 namespace VirtualFromBase {
830 struct S1 {
831 virtual int f() const;
832 };
833 struct S2 {
834 virtual int f();
835 };
836 template <typename T> struct X : T {
837 constexpr X() {}
838 double d = 0.0;
839 constexpr int f() { return sizeof(T); }
840 };
841
842 // Non-virtual f(), OK.
843 constexpr X<X<S1>> xxs1;
844 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
845 static_assert(p->f() == sizeof(S1), "");
846
847 // Virtual f(), not OK.
848 constexpr X<X<S2>> xxs2;
849 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
850 static_assert(q->f() == sizeof(X<S2>), ""); // both-error {{not an integral constant expression}} \
851 // both-note {{cannot evaluate call to virtual function}}
852 }
853 #endif
854
855 namespace CompositeDefaultArgs {
856 struct Foo {
857 int a;
858 int b;
859 constexpr Foo() : a(12), b(13) {}
860 };
861
862 class Bar {
863 public:
864 bool B = false;
865
866 constexpr int someFunc(Foo F = Foo()) {
867 this->B = true;
868 return 5;
869 }
870 };
871
872 constexpr bool testMe() {
873 Bar B;
874 B.someFunc();
875 return B.B;
876 }
877 static_assert(testMe(), "");
878 }
879
880 constexpr bool BPand(BoolPair bp) {
881 return bp.first && bp.second;
882 }
883 static_assert(BPand(BoolPair{true, false}) == false, "");
884
885 namespace TemporaryObjectExpr {
886 struct F {
887 int a;
888 constexpr F() : a(12) {}
889 };
890 constexpr int foo(F f) {
891 return 0;
892 }
893 static_assert(foo(F()) == 0, "");
894 }
895
896 namespace ZeroInit {
897 struct F {
898 int a;
899 };
900
901 namespace Simple {
902 struct A {
903 char a;
904 bool b;
905 int c[4];
906 float d;
907 };
908 constexpr int foo(A x) {
909 return x.a + static_cast<int>(x.b) + x.c[0] + x.c[3] + static_cast<int>(x.d);
910 }
911 static_assert(foo(A()) == 0, "");
912 }
913
914 namespace Inheritance {
915 struct F2 : F {
916 float f;
917 };
918
919 constexpr int foo(F2 f) {
920 return (int)f.f + f.a;
921 }
922 static_assert(foo(F2()) == 0, "");
923 }
924
925 namespace BitFields {
926 struct F {
927 unsigned a : 6;
928 };
929 constexpr int foo(F f) {
930 return f.a;
931 }
932 static_assert(foo(F()) == 0, "");
933 }
934
935 namespace Nested {
936 struct F2 {
937 float f;
938 char c;
939 };
940
941 struct F {
942 F2 f2;
943 int i;
944 };
945
946 constexpr int foo(F f) {
947 return f.i + f.f2.f + f.f2.c;
948 }
949 static_assert(foo(F()) == 0, "");
950 }
951
952 namespace CompositeArrays {
953 struct F2 {
954 float f;
955 char c;
956 };
957
958 struct F {
959 F2 f2[2];
960 int i;
961 };
962
963 constexpr int foo(F f) {
964 return f.i + f.f2[0].f + f.f2[0].c + f.f2[1].f + f.f2[1].c;
965 }
966 static_assert(foo(F()) == 0, "");
967 }
968
969 /// FIXME: This needs support for unions on the new interpreter.
970 /// We diagnose an uninitialized object in c++14.
971 #if __cplusplus > 201402L
972 namespace Unions {
973 struct F {
974 union {
975 int a;
976 char c[4];
977 float f;
978 } U;
979 int i;
980 };
981
982 constexpr int foo(F f) {
983 return f.i + f.U.f; // ref-note {{read of member 'f' of union with active member 'a'}}
984 }
985 static_assert(foo(F()) == 0, ""); // ref-error {{not an integral constant expression}} \
986 // ref-note {{in call to}}
987 }
988 #endif
989
990 #if __cplusplus >= 202002L
991 namespace Failure {
992 struct S {
993 int a;
994 F f{12};
995 };
996 constexpr int foo(S x) {
997 return x.a;
998 }
999 static_assert(foo(S()) == 0, "");
1000 };
1001 #endif
1002 }
1003
1004 #if __cplusplus >= 202002L
1005 namespace ParenInit {
1006 struct A {
1007 int a;
1008 };
1009
1010 struct B : A {
1011 int b;
1012 };
1013
1014 constexpr B b(A(1),2);
1015
1016
1017 struct O {
1018 int &&j;
1019 };
1020
1021 /// Not constexpr!
1022 O o1(0);
1023 constinit O o2(0); // both-error {{variable does not have a constant initializer}} \
1024 // both-note {{required by 'constinit' specifier}} \
1025 // both-note {{reference to temporary is not a constant expression}} \
1026 // both-note {{temporary created here}}
1027
1028
1029 /// Initializing an array.
1030 constexpr void bar(int i, int j) {
1031 int arr[4](i, j);
1032 }
1033 }
1034 #endif
1035
1036 namespace DelegatingConstructors {
1037 struct S {
1038 int a;
1039 constexpr S() : S(10) {}
1040 constexpr S(int a) : a(a) {}
1041 };
1042 constexpr S s = {};
1043 static_assert(s.a == 10, "");
1044
1045 struct B {
1046 int a;
1047 int b;
1048
1049 constexpr B(int a) : a(a), b(a + 2) {}
1050 };
1051 struct A : B {
1052 constexpr A() : B(10) {};
1053 };
1054 constexpr A d4 = {};
1055 static_assert(d4.a == 10, "");
1056 static_assert(d4.b == 12, "");
1057 }
1058
1059 namespace AccessOnNullptr {
1060 struct F {
1061 int a;
1062 };
1063
1064 constexpr int a() { // both-error {{never produces a constant expression}}
1065 F *f = nullptr;
1066
1067 f->a = 0; // both-note 2{{cannot access field of null pointer}}
1068 return f->a;
1069 }
1070 static_assert(a() == 0, ""); // both-error {{not an integral constant expression}} \
1071 // both-note {{in call to 'a()'}}
1072
1073 constexpr int a2() { // both-error {{never produces a constant expression}}
1074 F *f = nullptr;
1075
1076
1077 const int *a = &(f->a); // both-note 2{{cannot access field of null pointer}}
1078 return f->a;
1079 }
1080 static_assert(a2() == 0, ""); // both-error {{not an integral constant expression}} \
1081 // both-note {{in call to 'a2()'}}
1082 }
1083
1084 namespace IndirectFieldInit {
1085 #if __cplusplus >= 202002L
1086 /// Primitive.
1087 struct Nested1 {
1088 struct {
1089 int first;
1090 };
1091 int x;
1092 constexpr Nested1(int x) : first(12), x() { x = 4; }
1093 constexpr Nested1() : Nested1(42) {}
1094 };
1095 constexpr Nested1 N1{};
1096 static_assert(N1.first == 12, "");
1097
1098 /// Composite.
1099 struct Nested2 {
1100 struct First { int x = 42; };
1101 struct {
1102 First first;
1103 };
1104 int x;
1105 constexpr Nested2(int x) : first(12), x() { x = 4; }
1106 constexpr Nested2() : Nested2(42) {}
1107 };
1108 constexpr Nested2 N2{};
1109 static_assert(N2.first.x == 12, "");
1110
1111 /// Bitfield.
1112 struct Nested3 {
1113 struct {
1114 unsigned first : 2;
1115 };
1116 int x;
1117 constexpr Nested3(int x) : first(3), x() { x = 4; }
1118 constexpr Nested3() : Nested3(42) {}
1119 };
1120
1121 constexpr Nested3 N3{};
1122 static_assert(N3.first == 3, "");
1123
1124 /// Test that we get the offset right if the
1125 /// record has a base.
1126 struct Nested4Base {
1127 int a;
1128 int b;
1129 char c;
1130 };
1131 struct Nested4 : Nested4Base{
1132 struct {
1133 int first;
1134 };
1135 int x;
1136 constexpr Nested4(int x) : first(123), x() { a = 1; b = 2; c = 3; x = 4; }
1137 constexpr Nested4() : Nested4(42) {}
1138 };
1139 constexpr Nested4 N4{};
1140 static_assert(N4.first == 123, "");
1141
1142 struct S {
1143 struct {
1144 int x, y;
1145 };
1146
1147 constexpr S(int x_, int y_) : x(x_), y(y_) {}
1148 };
1149
1150 constexpr S s(1, 2);
1151 static_assert(s.x == 1 && s.y == 2);
1152
1153 struct S2 {
1154 int a;
1155 struct {
1156 int b;
1157 struct {
1158 int x, y;
1159 };
1160 };
1161
1162 constexpr S2(int x_, int y_) : a(3), b(4), x(x_), y(y_) {}
1163 };
1164
1165 constexpr S2 s2(1, 2);
1166 static_assert(s2.x == 1 && s2.y == 2 && s2.a == 3 && s2.b == 4);
1167
1168 #endif
1169 }
1170
1171 namespace InheritedConstructor {
1172 namespace PR47555 {
1173 struct A {
1174 int c;
1175 int d;
1176 constexpr A(int c, int d) : c(c), d(d){}
1177 };
1178 struct B : A { using A::A; };
1179
1180 constexpr B b = {13, 1};
1181 static_assert(b.c == 13, "");
1182 static_assert(b.d == 1, "");
1183 }
1184
1185 namespace PR47555_2 {
1186 struct A {
1187 int c;
1188 int d;
1189 double e;
1190 constexpr A(int c, int &d, double e) : c(c), d(++d), e(e){}
1191 };
1192 struct B : A { using A::A; };
1193
1194 constexpr int f() {
1195 int a = 10;
1196 B b = {10, a, 40.0};
1197 return a;
1198 }
1199 static_assert(f() == 11, "");
1200 }
1201
1202 namespace AaronsTest {
1203 struct T {
1204 constexpr T(float) {}
1205 };
1206
1207 struct Base {
1208 constexpr Base(T t = 1.0f) {}
1209 constexpr Base(float) {}
1210 };
1211
1212 struct FirstMiddle : Base {
1213 using Base::Base;
1214 constexpr FirstMiddle() : Base(2.0f) {}
1215 };
1216
1217 struct SecondMiddle : Base {
1218 constexpr SecondMiddle() : Base(3.0f) {}
1219 constexpr SecondMiddle(T t) : Base(t) {}
1220 };
1221
1222 struct S : FirstMiddle, SecondMiddle {
1223 using FirstMiddle::FirstMiddle;
1224 constexpr S(int i) : S(4.0f) {}
1225 };
1226
1227 constexpr S s(1);
1228 }
1229 }
1230
1231 namespace InvalidCtorInitializer {
1232 struct X {
1233 int Y;
1234 constexpr X()
1235 : Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
1236 };
1237 // no crash on evaluating the constexpr ctor.
1238 constexpr int Z = X().Y; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}}
1239 }
1240
1241 extern int f(); // both-note {{here}}
1242 struct HasNonConstExprMemInit {
1243 int x = f(); // both-note {{non-constexpr function}}
1244 constexpr HasNonConstExprMemInit() {} // both-error {{never produces a constant expression}}
1245 };
1246
1247 namespace {
1248 template <class Tp, Tp v>
1249 struct integral_constant {
1250 static const Tp value = v;
1251 };
1252
1253 template <class Tp, Tp v>
1254 const Tp integral_constant<Tp, v>::value;
1255
1256 typedef integral_constant<bool, true> true_type;
1257 typedef integral_constant<bool, false> false_type;
1258
1259 /// This might look innocent, but we get an evaluateAsInitializer call for the
1260 /// static bool member before evaluating the first static_assert, but we do NOT
1261 /// get such a call for the second one. So the second one needs to lazily visit
1262 /// the data member itself.
1263 static_assert(true_type::value, "");
1264 static_assert(true_type::value, "");
1265 }
1266
1267 #if __cplusplus >= 202002L
1268 namespace {
1269 /// Used to crash because the CXXDefaultInitExpr is of compound type.
1270 struct A {
1271 int &x;
1272 constexpr ~A() { --x; }
1273 };
1274 struct B {
1275 int &x;
1276 const A &a = A{x};
1277 };
1278 constexpr int a() {
1279 int x = 1;
1280 int f = B{x}.x;
1281 B{x}; // both-warning {{expression result unused}}
1282
1283 return 1;
1284 }
1285 }
1286 #endif
1287
1288 namespace pr18633 {
1289 struct A1 {
1290 static const int sz;
1291 static const int sz2;
1292 };
1293 const int A1::sz2 = 11;
1294 template<typename T>
1295 void func () {
1296 int arr[A1::sz];
1297 // both-warning@-1 {{variable length arrays in C++ are a Clang extension}}
1298 // both-note@-2 {{initializer of 'sz' is unknown}}
1299 // both-note@-9 {{declared here}}
1300 }
1301 template<typename T>
1302 void func2 () {
1303 int arr[A1::sz2];
1304 }
1305 const int A1::sz = 12;
1306 void func2() {
1307 func<int>();
1308 func2<int>();
1309 }
1310 }
1311
1312 namespace {
1313 struct F {
1314 static constexpr int Z = 12;
1315 };
1316 F f;
1317 static_assert(f.Z == 12, "");
1318 }
1319
1320 namespace UnnamedBitFields {
1321 struct A {
1322 int : 1;
1323 double f;
1324 int : 1;
1325 char c;
1326 };
1327
1328 constexpr A a = (A){1.0, 'a'};
1329 static_assert(a.f == 1.0, "");
1330 static_assert(a.c == 'a', "");
1331 }
1332
1333 namespace VirtualBases {
1334 /// This used to crash.
1335 namespace One {
1336 class A {
1337 protected:
1338 int x;
1339 };
1340 class B : public virtual A {
1341 public:
1342 int getX() { return x; } // both-note {{declared here}}
1343 };
1344
1345 class DV : virtual public B{};
1346
1347 void foo() {
1348 DV b;
1349 int a[b.getX()]; // both-warning {{variable length arrays}} \
1350 // both-note {{non-constexpr function 'getX' cannot be used}}
1351 }
1352 }
1353
1354 namespace Two {
1355 struct U { int n; };
1356 struct A : virtual U { int n; };
1357 struct B : A {};
1358 B a;
1359 static_assert((U*)(A*)(&a) == (U*)(&a), "");
1360
1361 struct C : virtual A {};
1362 struct D : B, C {};
1363 D d;
1364 constexpr B *p = &d;
1365 constexpr C *q = &d;
1366 static_assert((A*)p == (A*)q, ""); // both-error {{failed}}
1367 }
1368
1369 namespace Three {
1370 struct U { int n; };
1371 struct V : U { int n; };
1372 struct A : virtual V { int n; };
1373 struct Aa { int n; };
1374 struct B : virtual A, Aa {};
1375
1376 struct C : virtual A, Aa {};
1377
1378 struct D : B, C {};
1379
1380 D d;
1381
1382 constexpr B *p = &d;
1383 constexpr C *q = &d;
1384
1385 static_assert((void*)p != (void*)q, "");
1386 static_assert((A*)p == (A*)q, "");
1387 static_assert((Aa*)p != (Aa*)q, "");
1388
1389 constexpr V *v = p;
1390 constexpr V *w = q;
1391 constexpr V *x = (A*)p;
1392 static_assert(v == w, "");
1393 static_assert(v == x, "");
1394
1395 static_assert((U*)&d == p, "");
1396 static_assert((U*)&d == q, "");
1397 static_assert((U*)&d == v, "");
1398 static_assert((U*)&d == w, "");
1399 static_assert((U*)&d == x, "");
1400
1401 struct X {};
1402 struct Y1 : virtual X {};
1403 struct Y2 : X {};
1404 struct Z : Y1, Y2 {};
1405 Z z;
1406 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
1407 }
1408 }
1409
1410 namespace ZeroInit {
1411 struct S3 {
1412 S3() = default;
1413 S3(const S3&) = default;
1414 S3(S3&&) = default;
1415 constexpr S3(int n) : n(n) {}
1416 int n;
1417 };
1418 constexpr S3 s3d; // both-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
1419 static_assert(s3d.n == 0, "");
1420
1421 struct P {
1422 int a = 10;
1423 };
1424 static_assert(P().a == 10, "");
1425 }
1426
1427 namespace {
1428 #if __cplusplus >= 202002L
1429 struct C {
1430 template <unsigned N> constexpr C(const char (&)[N]) : n(N) {}
1431 unsigned n;
1432 };
1433 template <C c>
1434 constexpr auto operator""_c() { return c.n; }
1435
1436 constexpr auto waldo = "abc"_c;
1437 static_assert(waldo == 4, "");
1438 #endif
1439 }
1440
1441
1442 namespace TemporaryWithInvalidDestructor {
1443 #if __cplusplus >= 202002L
1444 struct A {
1445 bool a = true;
1446 constexpr ~A() noexcept(false) { // both-error {{never produces a constant expression}}
1447 throw; // both-note 2{{not valid in a constant expression}} \
1448 // both-error {{cannot use 'throw' with exceptions disabled}}
1449 }
1450 };
1451 static_assert(A().a, ""); // both-error {{not an integral constant expression}} \
1452 // both-note {{in call to}}
1453 #endif
1454 }
1455
1456 namespace IgnoredCtorWithZeroInit {
1457 struct S {
1458 int a;
1459 };
1460
1461 bool get_status() {
1462 return (S(), true);
1463 }
1464 }
1465
1466 #if __cplusplus >= 202002L
1467 namespace VirtOperator {
1468 /// This used to crash because it's a virtual CXXOperatorCallExpr.
1469 struct B {
1470 virtual constexpr bool operator==(const B&) const { return true; }
1471 };
1472 struct D : B {
1473 constexpr bool operator==(const B&) const override{ return false; } // both-note {{operator}}
1474 };
1475 constexpr bool cmp_base_derived = D() == D(); // both-warning {{ambiguous}}
1476 }
1477
1478 namespace FloatAPValue {
1479 struct ClassTemplateArg {
1480 int a;
1481 float f;
1482 };
1483 template<ClassTemplateArg A> struct ClassTemplateArgTemplate {
1484 static constexpr const ClassTemplateArg &Arg = A;
1485 };
1486 ClassTemplateArgTemplate<ClassTemplateArg{1, 2.0f}> ClassTemplateArgObj;
1487 template<const ClassTemplateArg&> struct ClassTemplateArgRefTemplate {};
1488 ClassTemplateArgRefTemplate<ClassTemplateArgObj.Arg> ClassTemplateArgRefObj;
1489 }
1490 #endif
1491
1492 namespace LocalWithThisPtrInit {
1493 struct S {
1494 int i;
1495 int *p = &i;
1496 };
1497 constexpr int foo() {
1498 S s{2};
1499 return *s.p;
1500 }
1501 static_assert(foo() == 2, "");
1502 }
1503
1504 namespace OnePastEndAndBack {
1505 struct Base {
1506 constexpr Base() {}
1507 int n = 0;
1508 };
1509
1510 constexpr Base a;
1511 constexpr const Base *c = &a + 1;
1512 constexpr const Base *d = c - 1;
1513 static_assert(d == &a, "");
1514 }
1515
1516 namespace BitSet {
1517 class Bitset {
1518 unsigned Bit = 0;
1519
1520 public:
1521 constexpr Bitset() {
1522 int Init[2] = {1,2};
1523 for (auto I : Init)
1524 set(I);
1525 }
1526 constexpr void set(unsigned I) {
1527 this->Bit++;
1528 this->Bit = 1u << 1;
1529 }
1530 };
1531
1532 struct ArchInfo {
1533 Bitset DefaultExts;
1534 };
1535
1536 constexpr ArchInfo ARMV8A = {
1537 Bitset()
1538 };
1539 }