constant-expression-cxx11.cpp revision a5aa96d01f2f8750a4756ca74c8e172384a54602
1// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 %s
2
3// This version of static_assert just requires a foldable value as the
4// expression, not an ICE.
5// FIXME: Once we implement the C++11 ICE rules, most uses of this here should
6// be converted to static_assert.
7#define static_assert_fold(expr, str) \
8    static_assert(__builtin_constant_p(expr), "not an integral constant expression"); \
9    static_assert(__builtin_constant_p(expr) ? expr : true, str)
10
11namespace StaticAssertFoldTest {
12
13int x;
14static_assert_fold(++x, "test"); // expected-error {{not an integral constant expression}}
15static_assert_fold(false, "test"); // expected-error {{test}}
16
17}
18
19// FIXME: support const T& parameters here.
20//template<typename T> constexpr T id(const T &t) { return t; }
21template<typename T> constexpr T id(T t) { return t; }
22// FIXME: support templates here.
23//template<typename T> constexpr T min(const T &a, const T &b) {
24//  return a < b ? a : b;
25//}
26//template<typename T> constexpr T max(const T &a, const T &b) {
27//  return a < b ? b : a;
28//}
29constexpr int min(const int &a, const int &b) { return a < b ? a : b; }
30constexpr int max(const int &a, const int &b) { return a < b ? b : a; }
31
32struct MemberZero {
33  constexpr int zero() { return 0; }
34};
35
36namespace DerivedToVBaseCast {
37
38  struct U { int n; };
39  struct V : U { int n; };
40  struct A : virtual V { int n; };
41  struct Aa { int n; };
42  struct B : virtual A, Aa {};
43  struct C : virtual A, Aa {};
44  struct D : B, C {};
45
46  D d;
47  constexpr B *p = &d;
48  constexpr C *q = &d;
49  static_assert_fold((void*)p != (void*)q, "");
50  static_assert_fold((A*)p == (A*)q, "");
51  static_assert_fold((Aa*)p != (Aa*)q, "");
52
53  constexpr B &pp = d;
54  constexpr C &qq = d;
55  static_assert_fold((void*)&pp != (void*)&qq, "");
56  static_assert_fold(&(A&)pp == &(A&)qq, "");
57  static_assert_fold(&(Aa&)pp != &(Aa&)qq, "");
58
59  constexpr V *v = p;
60  constexpr V *w = q;
61  constexpr V *x = (A*)p;
62  static_assert_fold(v == w, "");
63  static_assert_fold(v == x, "");
64
65  static_assert_fold((U*)&d == p, "");
66  static_assert_fold((U*)&d == q, "");
67  static_assert_fold((U*)&d == v, "");
68  static_assert_fold((U*)&d == w, "");
69  static_assert_fold((U*)&d == x, "");
70
71  struct X {};
72  struct Y1 : virtual X {};
73  struct Y2 : X {};
74  struct Z : Y1, Y2 {};
75  Z z;
76  static_assert_fold((X*)(Y1*)&z != (X*)(Y2*)&z, "");
77
78}
79
80namespace ConstCast {
81
82constexpr int n1 = 0;
83constexpr int n2 = const_cast<int&>(n1);
84constexpr int *n3 = const_cast<int*>(&n1);
85constexpr int n4 = *const_cast<int*>(&n1);
86constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
87constexpr int **n6 = const_cast<int**>(&n3);
88constexpr int n7 = **n5;
89constexpr int n8 = **n6;
90
91}
92
93namespace TemplateArgumentConversion {
94  template<int n> struct IntParam {};
95
96  using IntParam0 = IntParam<0>;
97  // FIXME: This should be accepted once we implement the new ICE rules.
98  using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}}
99  using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
100}
101
102namespace CaseStatements {
103  void f(int n) {
104    switch (n) {
105    // FIXME: Produce the 'add ()' fixit for this.
106    case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}}
107    // FIXME: This should be accepted once we implement the new ICE rules.
108    case id(1): // expected-error {{not an integer constant expression}}
109      return;
110    }
111  }
112}
113
114extern int &Recurse1;
115int &Recurse2 = Recurse1, &Recurse1 = Recurse2;
116constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}}
117
118namespace MemberEnum {
119  struct WithMemberEnum {
120    enum E { A = 42 };
121  } wme;
122
123  static_assert_fold(wme.A == 42, "");
124}
125
126namespace DefaultArguments {
127
128const int z = int();
129constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
130  return a + b + *c + d;
131}
132const int four = 4;
133constexpr int eight = 8;
134constexpr const int twentyseven = 27;
135static_assert_fold(Sum() == 0, "");
136static_assert_fold(Sum(1) == 1, "");
137static_assert_fold(Sum(1, four) == 5, "");
138static_assert_fold(Sum(1, eight, &twentyseven) == 36, "");
139static_assert_fold(Sum(1, 2, &four, eight) == 15, "");
140
141}
142
143namespace Ellipsis {
144
145// Note, values passed through an ellipsis can't actually be used.
146constexpr int F(int a, ...) { return a; }
147static_assert_fold(F(0) == 0, "");
148static_assert_fold(F(1, 0) == 1, "");
149static_assert_fold(F(2, "test") == 2, "");
150static_assert_fold(F(3, &F) == 3, "");
151int k = 0;
152static_assert_fold(F(4, k) == 3, ""); // expected-error {{constant expression}}
153
154}
155
156namespace Recursion {
157  constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
158  static_assert_fold(fib(11) == 89, "");
159
160  constexpr int gcd_inner(int a, int b) {
161    return b == 0 ? a : gcd_inner(b, a % b);
162  }
163  constexpr int gcd(int a, int b) {
164    return gcd_inner(max(a, b), min(a, b));
165  }
166
167  static_assert_fold(gcd(1749237, 5628959) == 7, "");
168}
169
170namespace FunctionCast {
171  // When folding, we allow functions to be cast to different types. Such
172  // cast functions cannot be called, even if they're constexpr.
173  constexpr int f() { return 1; }
174  typedef double (*DoubleFn)();
175  typedef int (*IntFn)();
176  int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}}
177  int b[(int)IntFn(f)()];    // ok
178}
179
180namespace StaticMemberFunction {
181  struct S {
182    static constexpr int k = 42;
183    static constexpr int f(int n) { return n * k + 2; }
184  } s;
185
186  constexpr int n = s.f(19);
187  static_assert_fold(S::f(19) == 800, "");
188  static_assert_fold(s.f(19) == 800, "");
189  static_assert_fold(n == 800, "");
190
191  constexpr int (*sf1)(int) = &S::f;
192  constexpr int (*sf2)(int) = &s.f;
193  constexpr const int *sk = &s.k;
194}
195
196namespace ParameterScopes {
197
198  const int k = 42;
199  constexpr const int &ObscureTheTruth(const int &a) { return a; }
200  constexpr const int &MaybeReturnJunk(bool b, const int a) {
201    return ObscureTheTruth(b ? a : k);
202  }
203  static_assert_fold(MaybeReturnJunk(false, 0) == 42, ""); // ok
204  constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}}
205
206  constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
207    // If ObscureTheTruth returns a reference to 'a', the result is not a
208    // constant expression even though 'a' is still in scope.
209    return ObscureTheTruth(b ? a : k);
210  }
211  static_assert_fold(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
212  constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}}
213
214  constexpr int InternalReturnJunk(int n) {
215    // FIXME: We should reject this: it never produces a constant expression.
216    return MaybeReturnJunk(true, n);
217  }
218  constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}}
219
220  constexpr int LToR(int &n) { return n; }
221  constexpr int GrabCallersArgument(bool which, int a, int b) {
222    return LToR(which ? b : a);
223  }
224  static_assert_fold(GrabCallersArgument(false, 1, 2) == 1, "");
225  static_assert_fold(GrabCallersArgument(true, 4, 8) == 8, "");
226
227}
228
229namespace Pointers {
230
231  constexpr int f(int n, const int *a, const int *b, const int *c) {
232    return n == 0 ? 0 : *a + f(n-1, b, c, a);
233  }
234
235  const int x = 1, y = 10, z = 100;
236  static_assert_fold(f(23, &x, &y, &z) == 788, "");
237
238  constexpr int g(int n, int a, int b, int c) {
239    return f(n, &a, &b, &c);
240  }
241  static_assert_fold(g(23, x, y, z) == 788, "");
242
243}
244
245namespace FunctionPointers {
246
247  constexpr int Double(int n) { return 2 * n; }
248  constexpr int Triple(int n) { return 3 * n; }
249  constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
250  constexpr int Quadruple(int n) { return Twice(Double, n); }
251  constexpr auto Select(int n) -> int (*)(int) {
252    return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
253  }
254  constexpr int Apply(int (*F)(int), int n) { return F(n); }
255
256  static_assert_fold(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
257
258  constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}}
259
260}
261
262namespace PointerComparison {
263
264int x, y;
265static_assert_fold(&x == &y, "false"); // expected-error {{false}}
266static_assert_fold(&x != &y, "");
267constexpr bool g1 = &x == &y;
268constexpr bool g2 = &x != &y;
269constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
270constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
271constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
272constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
273
274struct S { int x, y; } s;
275static_assert_fold(&s.x == &s.y, "false"); // expected-error {{false}}
276static_assert_fold(&s.x != &s.y, "");
277static_assert_fold(&s.x <= &s.y, "");
278static_assert_fold(&s.x >= &s.y, "false"); // expected-error {{false}}
279static_assert_fold(&s.x < &s.y, "");
280static_assert_fold(&s.x > &s.y, "false"); // expected-error {{false}}
281
282static_assert_fold(0 == &y, "false"); // expected-error {{false}}
283static_assert_fold(0 != &y, "");
284constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
285constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
286constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
287constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
288
289static_assert_fold(&x == 0, "false"); // expected-error {{false}}
290static_assert_fold(&x != 0, "");
291constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
292constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
293constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
294constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
295
296static_assert_fold(&x == &x, "");
297static_assert_fold(&x != &x, "false"); // expected-error {{false}}
298static_assert_fold(&x <= &x, "");
299static_assert_fold(&x >= &x, "");
300static_assert_fold(&x < &x, "false"); // expected-error {{false}}
301static_assert_fold(&x > &x, "false"); // expected-error {{false}}
302
303constexpr S* sptr = &s;
304// FIXME: This is not a constant expression; check we reject this and move this
305// test elsewhere.
306constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr);
307
308extern char externalvar[];
309// FIXME: This is not a constant expression; check we reject this and move this
310// test elsewhere.
311constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL;  // expected-error {{must be initialized by a constant expression}}
312constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
313static_assert_fold(0 != "foo", "");
314
315}
316
317namespace MaterializeTemporary {
318
319constexpr int f(const int &r) { return r; }
320constexpr int n = f(1);
321
322constexpr bool same(const int &a, const int &b) { return &a == &b; }
323constexpr bool sameTemporary(const int &n) { return same(n, n); }
324
325static_assert_fold(n, "");
326static_assert_fold(!same(4, 4), "");
327static_assert_fold(same(n, n), "");
328static_assert_fold(sameTemporary(9), "");
329
330}
331
332constexpr int strcmp_ce(const char *p, const char *q) {
333  return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
334}
335
336namespace StringLiteral {
337
338// FIXME: Refactor this once we support constexpr templates.
339constexpr int MangleChars(const char *p) {
340  return *p + 3 * (*p ? MangleChars(p+1) : 0);
341}
342constexpr int MangleChars(const char16_t *p) {
343  return *p + 3 * (*p ? MangleChars(p+1) : 0);
344}
345constexpr int MangleChars(const char32_t *p) {
346  return *p + 3 * (*p ? MangleChars(p+1) : 0);
347}
348
349static_assert_fold(MangleChars("constexpr!") == 1768383, "");
350static_assert_fold(MangleChars(u"constexpr!") == 1768383, "");
351static_assert_fold(MangleChars(U"constexpr!") == 1768383, "");
352
353constexpr char c0 = "nought index"[0];
354constexpr char c1 = "nice index"[10];
355constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}}
356constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}}
357constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
358
359constexpr const char *p = "test" + 2;
360static_assert_fold(*p == 's', "");
361
362constexpr const char *max_iter(const char *a, const char *b) {
363  return *a < *b ? b : a;
364}
365constexpr const char *max_element(const char *a, const char *b) {
366  return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
367}
368
369constexpr const char *begin(const char (&arr)[45]) { return arr; }
370constexpr const char *end(const char (&arr)[45]) { return arr + 45; }
371
372constexpr char str[] = "the quick brown fox jumped over the lazy dog";
373constexpr const char *max = max_element(begin(str), end(str));
374static_assert_fold(*max == 'z', "");
375static_assert_fold(max == str + 38, "");
376
377static_assert_fold(strcmp_ce("hello world", "hello world") == 0, "");
378static_assert_fold(strcmp_ce("hello world", "hello clang") > 0, "");
379static_assert_fold(strcmp_ce("constexpr", "test") < 0, "");
380static_assert_fold(strcmp_ce("", " ") < 0, "");
381
382}
383
384namespace Array {
385
386// FIXME: Use templates for these once we support constexpr templates.
387constexpr int Sum(const int *begin, const int *end) {
388  return begin == end ? 0 : *begin + Sum(begin+1, end);
389}
390constexpr const int *begin(const int (&xs)[5]) { return xs; }
391constexpr const int *end(const int (&xs)[5]) { return xs + 5; }
392
393constexpr int xs[] = { 1, 2, 3, 4, 5 };
394constexpr int ys[] = { 5, 4, 3, 2, 1 };
395constexpr int sum_xs = Sum(begin(xs), end(xs));
396static_assert_fold(sum_xs == 15, "");
397
398constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
399                       const int *xs, const int *ys, int c) {
400  return n ? F(*xs, *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c;
401}
402constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
403constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
404static_assert_fold(InnerProduct == 35, "");
405
406constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
407constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
408static_assert_fold(DiffProd == 8, "");
409static_assert_fold(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}}
410
411constexpr const int *p = xs + 3;
412constexpr int xs4 = p[1]; // ok
413constexpr int xs5 = p[2]; // expected-error {{constant expression}}
414constexpr int xs0 = p[-3]; // ok
415constexpr int xs_1 = p[-4]; // expected-error {{constant expression}}
416
417constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
418static_assert_fold(zs[0][0][0][0] == 1, "");
419static_assert_fold(zs[1][1][1][1] == 16, "");
420static_assert_fold(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}}
421static_assert_fold((&zs[0][0][0][2])[-1] == 2, "");
422static_assert_fold(**(**(zs + 1) + 1) == 11, "");
423static_assert_fold(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
424
425constexpr int arr[40] = { 1, 2, 3, [8] = 4 };
426constexpr int SumNonzero(const int *p) {
427  return *p + (*p ? SumNonzero(p+1) : 0);
428}
429constexpr int CountZero(const int *p, const int *q) {
430  return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
431}
432static_assert_fold(SumNonzero(arr) == 6, "");
433static_assert_fold(CountZero(arr, arr + 40) == 36, "");
434
435struct ArrayElem {
436  constexpr ArrayElem() : n(0) {}
437  int n;
438  constexpr int f() { return n; }
439};
440struct ArrayRVal {
441  constexpr ArrayRVal() {}
442  ArrayElem elems[10];
443};
444static_assert_fold(ArrayRVal().elems[3].f() == 0, "");
445
446}
447
448namespace DependentValues {
449
450struct I { int n; typedef I V[10]; };
451I::V x, y;
452template<bool B> struct S {
453  int k;
454  void f() {
455    I::V &cells = B ? x : y;
456    I &i = cells[k];
457    switch (i.n) {}
458  }
459};
460
461}
462
463namespace Class {
464
465struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
466constexpr int fn(const A &a) { return a.k; }
467static_assert_fold(fn(A(4,5)) == 9, "");
468
469struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
470struct C {
471  constexpr C(C *this_) : m(42), n(this_->m) {} // ok
472  int m, n;
473};
474struct D {
475  C c;
476  constexpr D() : c(&c) {}
477};
478static_assert_fold(D().c.n == 42, "");
479
480struct E {
481  constexpr E() : p(&p) {}
482  void *p;
483};
484constexpr const E &e1 = E(); // expected-error {{constant expression}}
485// This is a constant expression if we elide the copy constructor call, and
486// is not a constant expression if we don't! But we do, so it is.
487// FIXME: The move constructor is not currently implicitly defined as constexpr.
488// We notice this when evaluating an expression which uses it, but not when
489// checking its initializer.
490constexpr E e2 = E(); // unexpected-error {{constant expression}}
491static_assert_fold(e2.p == &e2.p, ""); // unexpected-error {{constant expression}}
492// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the
493// initializer of this declaration.
494constexpr E e3; // unexpected-error {{constant expression}}
495static_assert_fold(e3.p == &e3.p, "");
496
497extern const class F f;
498struct F {
499  constexpr F() : p(&f.p) {}
500  const void *p;
501};
502constexpr F f = F();
503
504struct G {
505  struct T {
506    constexpr T(T *p) : u1(), u2(p) {}
507    union U1 {
508      constexpr U1() {}
509      int a, b = 42;
510    } u1;
511    union U2 {
512      constexpr U2(T *p) : c(p->u1.b) {}
513      int c, d;
514    } u2;
515  } t;
516  constexpr G() : t(&t) {}
517} constexpr g;
518
519static_assert_fold(g.t.u1.a == 42, ""); // expected-error {{constant expression}}
520static_assert_fold(g.t.u1.b == 42, "");
521static_assert_fold(g.t.u2.c == 42, "");
522static_assert_fold(g.t.u2.d == 42, ""); // expected-error {{constant expression}}
523
524struct S {
525  int a, b;
526  const S *p;
527  double d;
528  const char *q;
529
530  constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
531};
532
533S global(43, &global);
534
535static_assert_fold(S(15, &global).b == 15, "");
536
537constexpr bool CheckS(const S &s) {
538  return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
539}
540static_assert_fold(CheckS(S(27, &global)), "");
541
542struct Arr {
543  char arr[3];
544  constexpr Arr() : arr{'x', 'y', 'z'} {}
545};
546constexpr int hash(Arr &&a) {
547  return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
548}
549constexpr int k = hash(Arr());
550static_assert_fold(k == 0x007a7978, "");
551
552
553struct AggregateInit {
554  const char &c;
555  int n;
556  double d;
557  int arr[5];
558  void *p;
559};
560
561constexpr AggregateInit agg1 = { "hello"[0] };
562
563static_assert_fold(strcmp_ce(&agg1.c, "hello") == 0, "");
564static_assert_fold(agg1.n == 0, "");
565static_assert_fold(agg1.d == 0.0, "");
566static_assert_fold(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}}
567static_assert_fold(agg1.arr[0] == 0, "");
568static_assert_fold(agg1.arr[4] == 0, "");
569static_assert_fold(agg1.arr[5] == 0, ""); // expected-error {{constant expression}}
570static_assert_fold(agg1.p == nullptr, "");
571
572namespace SimpleDerivedClass {
573
574struct B {
575  constexpr B(int n) : a(n) {}
576  int a;
577};
578struct D : B {
579  constexpr D(int n) : B(n) {}
580};
581constexpr D d(3);
582static_assert_fold(d.a == 3, "");
583
584}
585
586struct Bottom { constexpr Bottom() {} };
587struct Base : Bottom {
588  constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
589  int a;
590  const char *b;
591};
592struct Base2 : Bottom {
593  constexpr Base2(const int &r) : r(r) {}
594  int q = 123;
595  // FIXME: When we track the global for which we are computing the initializer,
596  // use a reference here.
597  //const int &r;
598  int r;
599};
600struct Derived : Base, Base2 {
601  constexpr Derived() : Base(76), Base2(a) {}
602  int c = r + b[1];
603};
604
605constexpr bool operator==(const Base &a, const Base &b) {
606  return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
607}
608
609constexpr Base base;
610constexpr Base base2(76);
611constexpr Derived derived;
612static_assert_fold(derived.a == 76, "");
613static_assert_fold(derived.b[2] == 's', "");
614static_assert_fold(derived.c == 76 + 'e', "");
615static_assert_fold(derived.q == 123, "");
616static_assert_fold(derived.r == 76, "");
617static_assert_fold(&derived.r == &derived.a, ""); // expected-error {{}}
618
619static_assert_fold(!(derived == base), "");
620static_assert_fold(derived == base2, "");
621
622constexpr Bottom &bot1 = (Base&)derived;
623constexpr Bottom &bot2 = (Base2&)derived;
624static_assert_fold(&bot1 != &bot2, "");
625
626constexpr Bottom *pb1 = (Base*)&derived;
627constexpr Bottom *pb2 = (Base2*)&derived;
628static_assert_fold(pb1 != pb2, "");
629static_assert_fold(pb1 == &bot1, "");
630static_assert_fold(pb2 == &bot2, "");
631
632constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
633constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
634constexpr Base2 &ok2 = (Base2&)bot2;
635static_assert_fold(&ok2 == &derived, "");
636
637constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
638constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
639constexpr Base2 *pok2 = (Base2*)pb2;
640static_assert_fold(pok2 == &derived, "");
641static_assert_fold(&ok2 == pok2, "");
642static_assert_fold((Base2*)(Derived*)(Base*)pb1 == pok2, "");
643static_assert_fold((Derived*)(Base*)pb1 == (Derived*)pok2, "");
644
645constexpr Base *nullB = 42 - 6 * 7;
646static_assert_fold((Bottom*)nullB == 0, "");
647static_assert_fold((Derived*)nullB == 0, "");
648static_assert_fold((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
649
650}
651
652namespace Temporaries {
653
654struct S {
655  constexpr S() {}
656  constexpr int f();
657};
658struct T : S {
659  constexpr T(int n) : S(), n(n) {}
660  int n;
661};
662constexpr int S::f() {
663  // 'this' must be the postfix-expression in a class member access expression,
664  // so we can't just use
665  //   return static_cast<T*>(this)->n;
666  return this->*(int(S::*))&T::n;
667}
668// The T temporary is implicitly cast to an S subobject, but we can recover the
669// T full-object via a base-to-derived cast, or a derived-to-base-casted member
670// pointer.
671static_assert_fold(T(3).f() == 3, "");
672
673constexpr int f(const S &s) {
674  return static_cast<const T&>(s).n;
675}
676constexpr int n = f(T(5));
677static_assert_fold(f(T(5)) == 5, "");
678
679}
680
681namespace Union {
682
683union U {
684  int a;
685  int b;
686};
687
688constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } };
689static_assert_fold(u[0].a == 0, "");
690static_assert_fold(u[0].b, ""); // expected-error {{constant expression}}
691static_assert_fold(u[1].b == 1, "");
692static_assert_fold((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}}
693static_assert_fold(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}}
694static_assert_fold((&(u[1]) + 1 + 1)->b == 3, "");
695
696}
697
698namespace MemberPointer {
699  struct A {
700    constexpr A(int n) : n(n) {}
701    int n;
702    constexpr int f() { return n + 3; }
703  };
704  constexpr A a(7);
705  static_assert_fold(A(5).*&A::n == 5, "");
706  static_assert_fold((&a)->*&A::n == 7, "");
707  static_assert_fold((A(8).*&A::f)() == 11, "");
708  static_assert_fold(((&a)->*&A::f)() == 10, "");
709
710  struct B : A {
711    constexpr B(int n, int m) : A(n), m(m) {}
712    int m;
713    constexpr int g() { return n + m + 1; }
714  };
715  constexpr B b(9, 13);
716  static_assert_fold(B(4, 11).*&A::n == 4, "");
717  static_assert_fold(B(4, 11).*&B::m == 11, "");
718  static_assert_fold(B(4, 11).*(int(A::*))&B::m == 11, "");
719  static_assert_fold((&b)->*&A::n == 9, "");
720  static_assert_fold((&b)->*&B::m == 13, "");
721  static_assert_fold((&b)->*(int(A::*))&B::m == 13, "");
722  static_assert_fold((B(4, 11).*&A::f)() == 7, "");
723  static_assert_fold((B(4, 11).*&B::g)() == 16, "");
724  static_assert_fold((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
725  static_assert_fold(((&b)->*&A::f)() == 12, "");
726  static_assert_fold(((&b)->*&B::g)() == 23, "");
727  static_assert_fold(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
728
729  struct S {
730    constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
731      m(m), n(n), pf(pf), pn(pn) {}
732    constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
733
734    constexpr int f() { return this->*pn; }
735    virtual int g() const;
736
737    int m, n;
738    int (S::*pf)() const;
739    int S::*pn;
740  };
741
742  constexpr int S::*pm = &S::m;
743  constexpr int S::*pn = &S::n;
744  constexpr int (S::*pf)() const = &S::f;
745  constexpr int (S::*pg)() const = &S::g;
746
747  constexpr S s(2, 5, &S::f, &S::m);
748
749  static_assert_fold((s.*&S::f)() == 2, "");
750  static_assert_fold((s.*s.pf)() == 2, "");
751
752  template<int n> struct T : T<n-1> {};
753  template<> struct T<0> { int n; };
754  template<> struct T<30> : T<29> { int m; };
755
756  T<17> t17;
757  T<30> t30;
758
759  constexpr int (T<10>::*deepn) = &T<0>::n;
760  static_assert_fold(&(t17.*deepn) == &t17.n, "");
761
762  constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
763  constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
764  static_assert_fold(&(t30.*deepm) == &t30.m, "");
765
766  constexpr T<5> *p17_5 = &t17;
767  constexpr T<13> *p17_13 = (T<13>*)p17_5;
768  constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
769  static_assert_fold(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
770  static_assert_fold(&(p17_13->*deepn) == &t17.n, "");
771  constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
772
773  constexpr T<5> *p30_5 = &t30;
774  constexpr T<23> *p30_23 = (T<23>*)p30_5;
775  constexpr T<13> *p30_13 = p30_23;
776  static_assert_fold(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
777  static_assert_fold(&(p30_13->*deepn) == &t30.n, "");
778  static_assert_fold(&(p30_23->*deepn) == &t30.n, "");
779  static_assert_fold(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
780  static_assert_fold(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
781  static_assert_fold(&(p30_23->*deepm) == &t30.m, "");
782}
783
784namespace ArrayBaseDerived {
785
786  struct Base {
787    constexpr Base() {}
788    int n = 0;
789  };
790  struct Derived : Base {
791    constexpr Derived() {}
792    constexpr const int *f() { return &n; }
793  };
794
795  constexpr Derived a[10];
796  constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
797  constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
798  static_assert_fold(pb3 == pd3, "");
799
800  // pb3 does not point to an array element.
801  constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
802  constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
803  constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
804  constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
805  constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
806  constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
807  constexpr Base *pb3a = pb4 - 1;
808
809  // pb4 does not point to a Derived.
810  constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
811  constexpr Derived *pd3a = (Derived*)pb3a;
812  constexpr int pd3n = pd3a->n;
813
814  // pd3a still points to the Derived array.
815  constexpr Derived *pd6 = pd3a + 3;
816  static_assert_fold(pd6 == &a[6], "");
817  constexpr Derived *pd9 = pd6 + 3;
818  constexpr Derived *pd10 = pd6 + 4;
819  constexpr int pd9n = pd9->n; // ok
820  constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}}
821  constexpr int pd0n = pd10[-10].n;
822  constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}}
823
824  constexpr Base *pb9 = pd9;
825  constexpr const int *(Base::*pfb)() const =
826      static_cast<const int *(Base::*)() const>(&Derived::f);
827  static_assert_fold((pb9->*pfb)() == &a[9].n, "");
828}
829
830namespace Complex {
831
832class complex {
833  int re, im;
834public:
835  constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
836  constexpr complex(const complex &o) : re(o.re), im(o.im) {}
837  constexpr complex operator-() const { return complex(-re, -im); }
838  friend constexpr complex operator+(const complex &l, const complex &r) {
839    return complex(l.re + r.re, l.im + r.im);
840  }
841  friend constexpr complex operator-(const complex &l, const complex &r) {
842    return l + -r;
843  }
844  friend constexpr complex operator*(const complex &l, const complex &r) {
845    return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
846  }
847  friend constexpr bool operator==(const complex &l, const complex &r) {
848    return l.re == r.re && l.im == r.im;
849  }
850  constexpr bool operator!=(const complex &r) const {
851    return re != r.re || im != r.im;
852  }
853  constexpr int real() const { return re; }
854  constexpr int imag() const { return im; }
855};
856
857constexpr complex i = complex(0, 1);
858constexpr complex k = (3 + 4*i) * (6 - 4*i);
859static_assert_fold(complex(1,0).real() == 1, "");
860static_assert_fold(complex(1,0).imag() == 0, "");
861static_assert_fold(((complex)1).imag() == 0, "");
862static_assert_fold(k.real() == 34, "");
863static_assert_fold(k.imag() == 12, "");
864static_assert_fold(k - 34 == 12*i, "");
865static_assert_fold((complex)1 == complex(1), "");
866static_assert_fold((complex)1 != complex(0, 1), "");
867static_assert_fold(complex(1) == complex(1), "");
868static_assert_fold(complex(1) != complex(0, 1), "");
869constexpr complex makeComplex(int re, int im) { return complex(re, im); }
870static_assert_fold(makeComplex(1,0) == complex(1), "");
871static_assert_fold(makeComplex(1,0) != complex(0, 1), "");
872
873class complex_wrap : public complex {
874public:
875  constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
876  constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
877};
878
879static_assert_fold((complex_wrap)1 == complex(1), "");
880static_assert_fold((complex)1 != complex_wrap(0, 1), "");
881static_assert_fold(complex(1) == complex_wrap(1), "");
882static_assert_fold(complex_wrap(1) != complex(0, 1), "");
883constexpr complex_wrap makeComplexWrap(int re, int im) {
884  return complex_wrap(re, im);
885}
886static_assert_fold(makeComplexWrap(1,0) == complex(1), "");
887static_assert_fold(makeComplexWrap(1,0) != complex(0, 1), "");
888
889}
890