1// RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2
3namespace StaticAssertFoldTest {
4
5int x;
6static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7static_assert(false, "test"); // expected-error {{test}}
8
9}
10
11typedef decltype(sizeof(char)) size_t;
12
13template<typename T> constexpr T id(const T &t) { return t; }
14template<typename T> constexpr T min(const T &a, const T &b) {
15  return a < b ? a : b;
16}
17template<typename T> constexpr T max(const T &a, const T &b) {
18  return a < b ? b : a;
19}
20template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
21template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
22
23struct MemberZero {
24  constexpr int zero() const { return 0; }
25};
26
27namespace DerivedToVBaseCast {
28
29  struct U { int n; };
30  struct V : U { int n; };
31  struct A : virtual V { int n; };
32  struct Aa { int n; };
33  struct B : virtual A, Aa {};
34  struct C : virtual A, Aa {};
35  struct D : B, C {};
36
37  D d;
38  constexpr B *p = &d;
39  constexpr C *q = &d;
40
41  static_assert((void*)p != (void*)q, "");
42  static_assert((A*)p == (A*)q, "");
43  static_assert((Aa*)p != (Aa*)q, "");
44
45  constexpr B &pp = d;
46  constexpr C &qq = d;
47  static_assert((void*)&pp != (void*)&qq, "");
48  static_assert(&(A&)pp == &(A&)qq, "");
49  static_assert(&(Aa&)pp != &(Aa&)qq, "");
50
51  constexpr V *v = p;
52  constexpr V *w = q;
53  constexpr V *x = (A*)p;
54  static_assert(v == w, "");
55  static_assert(v == x, "");
56
57  static_assert((U*)&d == p, "");
58  static_assert((U*)&d == q, "");
59  static_assert((U*)&d == v, "");
60  static_assert((U*)&d == w, "");
61  static_assert((U*)&d == x, "");
62
63  struct X {};
64  struct Y1 : virtual X {};
65  struct Y2 : X {};
66  struct Z : Y1, Y2 {};
67  Z z;
68  static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
69}
70
71namespace ConstCast {
72
73constexpr int n1 = 0;
74constexpr int n2 = const_cast<int&>(n1);
75constexpr int *n3 = const_cast<int*>(&n1);
76constexpr int n4 = *const_cast<int*>(&n1);
77constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
78constexpr int **n6 = const_cast<int**>(&n3);
79constexpr int n7 = **n5;
80constexpr int n8 = **n6;
81
82// const_cast from prvalue to xvalue.
83struct A { int n; };
84constexpr int n9 = (const_cast<A&&>(A{123})).n;
85static_assert(n9 == 123, "");
86
87}
88
89namespace TemplateArgumentConversion {
90  template<int n> struct IntParam {};
91
92  using IntParam0 = IntParam<0>;
93  using IntParam0 = IntParam<id(0)>;
94  using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
95}
96
97namespace CaseStatements {
98  void f(int n) {
99    switch (n) {
100    case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
101    case id(0): // expected-error {{duplicate case value '0'}}
102      return;
103    }
104  }
105}
106
107extern int &Recurse1;
108int &Recurse2 = Recurse1; // expected-note {{declared here}}
109int &Recurse1 = Recurse2;
110constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
111
112extern const int RecurseA;
113const int RecurseB = RecurseA; // expected-note {{declared here}}
114const int RecurseA = 10;
115constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
116
117namespace MemberEnum {
118  struct WithMemberEnum {
119    enum E { A = 42 };
120  } wme;
121
122  static_assert(wme.A == 42, "");
123}
124
125namespace DefaultArguments {
126
127const int z = int();
128constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
129  return a + b + *c + d;
130}
131const int four = 4;
132constexpr int eight = 8;
133constexpr const int twentyseven = 27;
134static_assert(Sum() == 0, "");
135static_assert(Sum(1) == 1, "");
136static_assert(Sum(1, four) == 5, "");
137static_assert(Sum(1, eight, &twentyseven) == 36, "");
138static_assert(Sum(1, 2, &four, eight) == 15, "");
139
140}
141
142namespace Ellipsis {
143
144// Note, values passed through an ellipsis can't actually be used.
145constexpr int F(int a, ...) { return a; }
146static_assert(F(0) == 0, "");
147static_assert(F(1, 0) == 1, "");
148static_assert(F(2, "test") == 2, "");
149static_assert(F(3, &F) == 3, "");
150int k = 0; // expected-note {{here}}
151static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
152
153}
154
155namespace Recursion {
156  constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
157  static_assert(fib(11) == 89, "");
158
159  constexpr int gcd_inner(int a, int b) {
160    return b == 0 ? a : gcd_inner(b, a % b);
161  }
162  constexpr int gcd(int a, int b) {
163    return gcd_inner(max(a, b), min(a, b));
164  }
165
166  static_assert(gcd(1749237, 5628959) == 7, "");
167}
168
169namespace FunctionCast {
170  // When folding, we allow functions to be cast to different types. Such
171  // cast functions cannot be called, even if they're constexpr.
172  constexpr int f() { return 1; }
173  typedef double (*DoubleFn)();
174  typedef int (*IntFn)();
175  int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
176  int b[(int)IntFn(f)()];    // ok
177}
178
179namespace StaticMemberFunction {
180  struct S {
181    static constexpr int k = 42;
182    static constexpr int f(int n) { return n * k + 2; }
183  } s;
184
185  constexpr int n = s.f(19);
186  static_assert(S::f(19) == 800, "");
187  static_assert(s.f(19) == 800, "");
188  static_assert(n == 800, "");
189
190  constexpr int (*sf1)(int) = &S::f;
191  constexpr int (*sf2)(int) = &s.f;
192  constexpr const int *sk = &s.k;
193}
194
195namespace ParameterScopes {
196
197  const int k = 42;
198  constexpr const int &ObscureTheTruth(const int &a) { return a; }
199  constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
200    return ObscureTheTruth(b ? a : k);
201  }
202  static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
203  constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
204
205  constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
206    return ObscureTheTruth(b ? a : k);
207  }
208  static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
209  constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
210
211  constexpr int InternalReturnJunk(int n) {
212    return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}}
213  }
214  constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
215
216  constexpr int LToR(int &n) { return n; }
217  constexpr int GrabCallersArgument(bool which, int a, int b) {
218    return LToR(which ? b : a);
219  }
220  static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
221  static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
222
223}
224
225namespace Pointers {
226
227  constexpr int f(int n, const int *a, const int *b, const int *c) {
228    return n == 0 ? 0 : *a + f(n-1, b, c, a);
229  }
230
231  const int x = 1, y = 10, z = 100;
232  static_assert(f(23, &x, &y, &z) == 788, "");
233
234  constexpr int g(int n, int a, int b, int c) {
235    return f(n, &a, &b, &c);
236  }
237  static_assert(g(23, x, y, z) == 788, "");
238
239}
240
241namespace FunctionPointers {
242
243  constexpr int Double(int n) { return 2 * n; }
244  constexpr int Triple(int n) { return 3 * n; }
245  constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
246  constexpr int Quadruple(int n) { return Twice(Double, n); }
247  constexpr auto Select(int n) -> int (*)(int) {
248    return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
249  }
250  constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
251
252  static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
253
254  constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
255
256}
257
258namespace PointerComparison {
259
260int x, y;
261static_assert(&x == &y, "false"); // expected-error {{false}}
262static_assert(&x != &y, "");
263constexpr bool g1 = &x == &y;
264constexpr bool g2 = &x != &y;
265constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
266constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
267constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
268constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
269
270struct S { int x, y; } s;
271static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
272static_assert(&s.x != &s.y, "");
273static_assert(&s.x <= &s.y, "");
274static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
275static_assert(&s.x < &s.y, "");
276static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
277
278static_assert(0 == &y, "false"); // expected-error {{false}}
279static_assert(0 != &y, "");
280constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
281constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
282constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
283constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
284
285static_assert(&x == 0, "false"); // expected-error {{false}}
286static_assert(&x != 0, "");
287constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
288constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
289constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
290constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
291
292static_assert(&x == &x, "");
293static_assert(&x != &x, "false"); // expected-error {{false}}
294static_assert(&x <= &x, "");
295static_assert(&x >= &x, "");
296static_assert(&x < &x, "false"); // expected-error {{false}}
297static_assert(&x > &x, "false"); // expected-error {{false}}
298
299constexpr S* sptr = &s;
300constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
301
302struct U {};
303struct Str {
304  int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
305    expected-warning {{not an integral constant expression}} \
306    expected-note {{dynamic_cast is not allowed in a constant expression}}
307  int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
308    expected-warning {{not an integral constant expression}} \
309    expected-note {{reinterpret_cast is not allowed in a constant expression}}
310  int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
311    expected-warning {{not an integral constant expression}} \
312    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
313  int d : (S*)(42) == (S*)(42); // \
314    expected-warning {{not an integral constant expression}} \
315    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
316  int e : (Str*)(sptr) == (Str*)(sptr); // \
317    expected-warning {{not an integral constant expression}} \
318    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
319  int f : &(U&)(*sptr) == &(U&)(*sptr); // \
320    expected-warning {{not an integral constant expression}} \
321    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
322  int g : (S*)(void*)(sptr) == sptr; // \
323    expected-warning {{not an integral constant expression}} \
324    expected-note {{cast from 'void *' is not allowed in a constant expression}}
325};
326
327extern char externalvar[];
328constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
329constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
330static_assert(0 != "foo", "");
331
332}
333
334namespace MaterializeTemporary {
335
336constexpr int f(const int &r) { return r; }
337constexpr int n = f(1);
338
339constexpr bool same(const int &a, const int &b) { return &a == &b; }
340constexpr bool sameTemporary(const int &n) { return same(n, n); }
341
342static_assert(n, "");
343static_assert(!same(4, 4), "");
344static_assert(same(n, n), "");
345static_assert(sameTemporary(9), "");
346
347struct A { int &&r; };
348struct B { A &&a1; A &&a2; };
349
350constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
351static_assert(&b1.a1 != &b1.a2, "");
352static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
353
354constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
355static_assert(&b1 != &b2, "");
356static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
357
358constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
359void foo() {
360  constexpr static B b1 { { 1 }, { 2 } }; // ok
361  constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
362  constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
363}
364
365constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}}
366static_assert(&b4 != &b2, "");
367
368// Proposed DR: copy-elision doesn't trigger lifetime extension.
369constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
370
371namespace NestedNonStatic {
372  // Proposed DR: for a reference constant expression to refer to a static
373  // storage duration temporary, that temporary must itself be initialized
374  // by a constant expression (a core constant expression is not enough).
375  struct A { int &&r; };
376  struct B { A &&a; };
377  constexpr B a = { A{0} }; // ok
378  constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
379}
380
381namespace FakeInitList {
382  struct init_list_3_ints { const int (&x)[3]; };
383  struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
384  constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
385}
386
387}
388
389constexpr int strcmp_ce(const char *p, const char *q) {
390  return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
391}
392
393namespace StringLiteral {
394
395template<typename Char>
396constexpr int MangleChars(const Char *p) {
397  return *p + 3 * (*p ? MangleChars(p+1) : 0);
398}
399
400static_assert(MangleChars("constexpr!") == 1768383, "");
401static_assert(MangleChars(u8"constexpr!") == 1768383, "");
402static_assert(MangleChars(L"constexpr!") == 1768383, "");
403static_assert(MangleChars(u"constexpr!") == 1768383, "");
404static_assert(MangleChars(U"constexpr!") == 1768383, "");
405
406constexpr char c0 = "nought index"[0];
407constexpr char c1 = "nice index"[10];
408constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}}
409constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
410constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}}
411
412constexpr const char *p = "test" + 2;
413static_assert(*p == 's', "");
414
415constexpr const char *max_iter(const char *a, const char *b) {
416  return *a < *b ? b : a;
417}
418constexpr const char *max_element(const char *a, const char *b) {
419  return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
420}
421
422constexpr char str[] = "the quick brown fox jumped over the lazy dog";
423constexpr const char *max = max_element(begin(str), end(str));
424static_assert(*max == 'z', "");
425static_assert(max == str + 38, "");
426
427static_assert(strcmp_ce("hello world", "hello world") == 0, "");
428static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
429static_assert(strcmp_ce("constexpr", "test") < 0, "");
430static_assert(strcmp_ce("", " ") < 0, "");
431
432struct S {
433  int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
434};
435
436struct T {
437  char c[6];
438  constexpr T() : c{"foo"} {}
439};
440constexpr T t;
441
442static_assert(t.c[0] == 'f', "");
443static_assert(t.c[1] == 'o', "");
444static_assert(t.c[2] == 'o', "");
445static_assert(t.c[3] == 0, "");
446static_assert(t.c[4] == 0, "");
447static_assert(t.c[5] == 0, "");
448static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
449
450struct U {
451  wchar_t chars[6];
452  int n;
453} constexpr u = { { L"test" }, 0 };
454static_assert(u.chars[2] == L's', "");
455
456struct V {
457  char c[4];
458  constexpr V() : c("hi!") {}
459};
460static_assert(V().c[1] == "i"[0], "");
461
462namespace Parens {
463  constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
464                          d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
465  static_assert(a[0] == 'f', "");
466  static_assert(b[1] == 'o', "");
467  static_assert(c[2] == 'o', "");
468  static_assert(d[0] == 'f', "");
469  static_assert(e[1] == 'o', "");
470  static_assert(f[2] == 'o', "");
471  static_assert(f[5] == 0, "");
472  static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
473}
474
475}
476
477namespace Array {
478
479template<typename Iter>
480constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
481  return begin == end ? 0 : *begin + Sum(begin+1, end);
482}
483
484constexpr int xs[] = { 1, 2, 3, 4, 5 };
485constexpr int ys[] = { 5, 4, 3, 2, 1 };
486constexpr int sum_xs = Sum(begin(xs), end(xs));
487static_assert(sum_xs == 15, "");
488
489constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
490                       const int *xs, const int *ys, int c) {
491  return n ? F(
492               *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
493               *ys,
494               ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
495      expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
496      expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
497           : c;
498}
499constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
500constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
501static_assert(InnerProduct == 35, "");
502
503constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
504constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
505static_assert(DiffProd == 8, "");
506static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
507      expected-error {{constant expression}} \
508      expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
509
510constexpr const int *p = xs + 3;
511constexpr int xs4 = p[1]; // ok
512constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
513constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
514constexpr int xs0 = p[-3]; // ok
515constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
516
517constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
518static_assert(zs[0][0][0][0] == 1, "");
519static_assert(zs[1][1][1][1] == 16, "");
520static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
521static_assert((&zs[0][0][0][2])[-1] == 2, "");
522static_assert(**(**(zs + 1) + 1) == 11, "");
523static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
524static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
525constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
526
527constexpr int fail(const int &p) {
528  return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
529}
530static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
531expected-error {{static_assert expression is not an integral constant expression}} \
532expected-note {{in call to 'fail(zs[1][0][1][0])'}}
533
534constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
535constexpr int SumNonzero(const int *p) {
536  return *p + (*p ? SumNonzero(p+1) : 0);
537}
538constexpr int CountZero(const int *p, const int *q) {
539  return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
540}
541static_assert(SumNonzero(arr) == 6, "");
542static_assert(CountZero(arr, arr + 40) == 36, "");
543
544struct ArrayElem {
545  constexpr ArrayElem() : n(0) {}
546  int n;
547  constexpr int f() const { return n; }
548};
549struct ArrayRVal {
550  constexpr ArrayRVal() {}
551  ArrayElem elems[10];
552};
553static_assert(ArrayRVal().elems[3].f() == 0, "");
554
555constexpr int selfref[2][2][2] = {
556  selfref[1][1][1] + 1, selfref[0][0][0] + 1,
557  selfref[1][0][1] + 1, selfref[0][1][0] + 1,
558  selfref[1][0][0] + 1, selfref[0][1][1] + 1 };
559static_assert(selfref[0][0][0] == 1, "");
560static_assert(selfref[0][0][1] == 2, "");
561static_assert(selfref[0][1][0] == 1, "");
562static_assert(selfref[0][1][1] == 2, "");
563static_assert(selfref[1][0][0] == 1, "");
564static_assert(selfref[1][0][1] == 3, "");
565static_assert(selfref[1][1][0] == 0, "");
566static_assert(selfref[1][1][1] == 0, "");
567
568struct TrivialDefCtor { int n; };
569typedef TrivialDefCtor TDCArray[2][2];
570static_assert(TDCArray{}[1][1].n == 0, "");
571
572struct NonAggregateTDC : TrivialDefCtor {};
573typedef NonAggregateTDC NATDCArray[2][2];
574static_assert(NATDCArray{}[1][1].n == 0, "");
575
576}
577
578namespace DependentValues {
579
580struct I { int n; typedef I V[10]; };
581I::V x, y;
582int g();
583template<bool B, typename T> struct S : T {
584  int k;
585  void f() {
586    I::V &cells = B ? x : y;
587    I &i = cells[k];
588    switch (i.n) {}
589
590    // FIXME: We should be able to diagnose this.
591    constexpr int n = g();
592
593    constexpr int m = this->g(); // ok, could be constexpr
594  }
595};
596
597}
598
599namespace Class {
600
601struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
602constexpr int fn(const A &a) { return a.k; }
603static_assert(fn(A(4,5)) == 9, "");
604
605struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
606struct C {
607  constexpr C(C *this_) : m(42), n(this_->m) {} // ok
608  int m, n;
609};
610struct D {
611  C c;
612  constexpr D() : c(&c) {}
613};
614static_assert(D().c.n == 42, "");
615
616struct E {
617  constexpr E() : p(&p) {}
618  void *p;
619};
620constexpr const E &e1 = E();
621// This is a constant expression if we elide the copy constructor call, and
622// is not a constant expression if we don't! But we do, so it is.
623constexpr E e2 = E();
624static_assert(e2.p == &e2.p, "");
625constexpr E e3;
626static_assert(e3.p == &e3.p, "");
627
628extern const class F f;
629struct F {
630  constexpr F() : p(&f.p) {}
631  const void *p;
632};
633constexpr F f;
634
635struct G {
636  struct T {
637    constexpr T(T *p) : u1(), u2(p) {}
638    union U1 {
639      constexpr U1() {}
640      int a, b = 42;
641    } u1;
642    union U2 {
643      constexpr U2(T *p) : c(p->u1.b) {}
644      int c, d;
645    } u2;
646  } t;
647  constexpr G() : t(&t) {}
648} constexpr g;
649
650static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
651static_assert(g.t.u1.b == 42, "");
652static_assert(g.t.u2.c == 42, "");
653static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
654
655struct S {
656  int a, b;
657  const S *p;
658  double d;
659  const char *q;
660
661  constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
662};
663
664S global(43, &global);
665
666static_assert(S(15, &global).b == 15, "");
667
668constexpr bool CheckS(const S &s) {
669  return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
670}
671static_assert(CheckS(S(27, &global)), "");
672
673struct Arr {
674  char arr[3];
675  constexpr Arr() : arr{'x', 'y', 'z'} {}
676};
677constexpr int hash(Arr &&a) {
678  return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
679}
680constexpr int k = hash(Arr());
681static_assert(k == 0x007a7978, "");
682
683
684struct AggregateInit {
685  const char &c;
686  int n;
687  double d;
688  int arr[5];
689  void *p;
690};
691
692constexpr AggregateInit agg1 = { "hello"[0] };
693
694static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
695static_assert(agg1.n == 0, "");
696static_assert(agg1.d == 0.0, "");
697static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
698static_assert(agg1.arr[0] == 0, "");
699static_assert(agg1.arr[4] == 0, "");
700static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
701static_assert(agg1.p == nullptr, "");
702
703static constexpr const unsigned char uc[] = { "foo" };
704static_assert(uc[0] == 'f', "");
705static_assert(uc[3] == 0, "");
706
707namespace SimpleDerivedClass {
708
709struct B {
710  constexpr B(int n) : a(n) {}
711  int a;
712};
713struct D : B {
714  constexpr D(int n) : B(n) {}
715};
716constexpr D d(3);
717static_assert(d.a == 3, "");
718
719}
720
721struct Bottom { constexpr Bottom() {} };
722struct Base : Bottom {
723  constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
724  int a;
725  const char *b;
726};
727struct Base2 : Bottom {
728  constexpr Base2(const int &r) : r(r) {}
729  int q = 123;
730  const int &r;
731};
732struct Derived : Base, Base2 {
733  constexpr Derived() : Base(76), Base2(a) {}
734  int c = r + b[1];
735};
736
737constexpr bool operator==(const Base &a, const Base &b) {
738  return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
739}
740
741constexpr Base base;
742constexpr Base base2(76);
743constexpr Derived derived;
744static_assert(derived.a == 76, "");
745static_assert(derived.b[2] == 's', "");
746static_assert(derived.c == 76 + 'e', "");
747static_assert(derived.q == 123, "");
748static_assert(derived.r == 76, "");
749static_assert(&derived.r == &derived.a, "");
750
751static_assert(!(derived == base), "");
752static_assert(derived == base2, "");
753
754constexpr Bottom &bot1 = (Base&)derived;
755constexpr Bottom &bot2 = (Base2&)derived;
756static_assert(&bot1 != &bot2, "");
757
758constexpr Bottom *pb1 = (Base*)&derived;
759constexpr Bottom *pb2 = (Base2*)&derived;
760static_assert(&pb1 != &pb2, "");
761static_assert(pb1 == &bot1, "");
762static_assert(pb2 == &bot2, "");
763
764constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
765constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
766constexpr Base2 &ok2 = (Base2&)bot2;
767static_assert(&ok2 == &derived, "");
768
769constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
770constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
771constexpr Base2 *pok2 = (Base2*)pb2;
772static_assert(pok2 == &derived, "");
773static_assert(&ok2 == pok2, "");
774static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
775static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
776
777// Core issue 903: we do not perform constant evaluation when checking for a
778// null pointer in C++11. Just check for an integer literal with value 0.
779constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}}
780constexpr Base *nullB1 = 0;
781static_assert((Bottom*)nullB == 0, "");
782static_assert((Derived*)nullB == 0, "");
783static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
784Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}}
785Base *nullB3 = (0);
786Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}}
787Base *nullB5 = ((0ULL));
788Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}}
789enum Null { kNull };
790Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}}
791static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
792
793
794
795namespace ConversionOperators {
796
797struct T {
798  constexpr T(int n) : k(5*n - 3) {}
799  constexpr operator int() const { return k; }
800  int k;
801};
802
803struct S {
804  constexpr S(int n) : k(2*n + 1) {}
805  constexpr operator int() const { return k; }
806  constexpr operator T() const { return T(k); }
807  int k;
808};
809
810constexpr bool check(T a, T b) { return a == b.k; }
811
812static_assert(S(5) == 11, "");
813static_assert(check(S(5), 11), "");
814
815namespace PR14171 {
816
817struct X {
818  constexpr (operator int)() const { return 0; }
819};
820static_assert(X() == 0, "");
821
822}
823
824}
825
826struct This {
827  constexpr int f() const { return 0; }
828  static constexpr int g() { return 0; }
829  void h() {
830    constexpr int x = f(); // expected-error {{must be initialized by a constant}}
831    // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
832    constexpr int y = this->f(); // expected-error {{must be initialized by a constant}}
833    // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
834    constexpr int z = g();
835    static_assert(z == 0, "");
836  }
837};
838
839}
840
841namespace Temporaries {
842
843struct S {
844  constexpr S() {}
845  constexpr int f() const;
846  constexpr int g() const;
847};
848struct T : S {
849  constexpr T(int n) : S(), n(n) {}
850  int n;
851};
852constexpr int S::f() const {
853  return static_cast<const T*>(this)->n; // expected-note {{cannot cast}}
854}
855constexpr int S::g() const {
856  // FIXME: Better diagnostic for this.
857  return this->*(int(S::*))&T::n; // expected-note {{subexpression}}
858}
859// The T temporary is implicitly cast to an S subobject, but we can recover the
860// T full-object via a base-to-derived cast, or a derived-to-base-casted member
861// pointer.
862static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}}
863static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}}
864static_assert(T(3).f() == 3, "");
865static_assert(T(4).g() == 4, "");
866
867constexpr int f(const S &s) {
868  return static_cast<const T&>(s).n;
869}
870constexpr int n = f(T(5));
871static_assert(f(T(5)) == 5, "");
872
873constexpr bool b(int n) { return &n; }
874static_assert(b(0), "");
875
876struct NonLiteral {
877  NonLiteral();
878  int f();
879};
880constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'Temporaries::NonLiteral'}}
881
882}
883
884namespace Union {
885
886union U {
887  int a;
888  int b;
889};
890
891constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
892static_assert(u[0].a == 0, "");
893static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
894static_assert(u[1].b == 1, "");
895static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
896static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
897static_assert((&(u[1]) + 1 + 1)->b == 3, "");
898
899constexpr U v = {};
900static_assert(v.a == 0, "");
901
902union Empty {};
903constexpr Empty e = {};
904
905// Make sure we handle trivial copy constructors for unions.
906constexpr U x = {42};
907constexpr U y = x;
908static_assert(y.a == 42, "");
909static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
910
911}
912
913namespace MemberPointer {
914  struct A {
915    constexpr A(int n) : n(n) {}
916    int n;
917    constexpr int f() const { return n + 3; }
918  };
919  constexpr A a(7);
920  static_assert(A(5).*&A::n == 5, "");
921  static_assert((&a)->*&A::n == 7, "");
922  static_assert((A(8).*&A::f)() == 11, "");
923  static_assert(((&a)->*&A::f)() == 10, "");
924
925  struct B : A {
926    constexpr B(int n, int m) : A(n), m(m) {}
927    int m;
928    constexpr int g() const { return n + m + 1; }
929  };
930  constexpr B b(9, 13);
931  static_assert(B(4, 11).*&A::n == 4, "");
932  static_assert(B(4, 11).*&B::m == 11, "");
933  static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
934  static_assert((&b)->*&A::n == 9, "");
935  static_assert((&b)->*&B::m == 13, "");
936  static_assert((&b)->*(int(A::*))&B::m == 13, "");
937  static_assert((B(4, 11).*&A::f)() == 7, "");
938  static_assert((B(4, 11).*&B::g)() == 16, "");
939  static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
940  static_assert(((&b)->*&A::f)() == 12, "");
941  static_assert(((&b)->*&B::g)() == 23, "");
942  static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
943
944  struct S {
945    constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
946      m(m), n(n), pf(pf), pn(pn) {}
947    constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
948
949    constexpr int f() const { return this->*pn; }
950    virtual int g() const;
951
952    int m, n;
953    int (S::*pf)() const;
954    int S::*pn;
955  };
956
957  constexpr int S::*pm = &S::m;
958  constexpr int S::*pn = &S::n;
959  constexpr int (S::*pf)() const = &S::f;
960  constexpr int (S::*pg)() const = &S::g;
961
962  constexpr S s(2, 5, &S::f, &S::m);
963
964  static_assert((s.*&S::f)() == 2, "");
965  static_assert((s.*s.pf)() == 2, "");
966
967  static_assert(pf == &S::f, "");
968  static_assert(pf == s.*&S::pf, "");
969  static_assert(pm == &S::m, "");
970  static_assert(pm != pn, "");
971  static_assert(s.pn != pn, "");
972  static_assert(s.pn == pm, "");
973  static_assert(pg != nullptr, "");
974  static_assert(pf != nullptr, "");
975  static_assert((int S::*)nullptr == nullptr, "");
976  static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
977  static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
978
979  template<int n> struct T : T<n-1> {};
980  template<> struct T<0> { int n; };
981  template<> struct T<30> : T<29> { int m; };
982
983  T<17> t17;
984  T<30> t30;
985
986  constexpr int (T<10>::*deepn) = &T<0>::n;
987  static_assert(&(t17.*deepn) == &t17.n, "");
988  static_assert(deepn == &T<2>::n, "");
989
990  constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
991  constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
992  static_assert(&(t30.*deepm) == &t30.m, "");
993  static_assert(deepm == &T<50>::m, "");
994  static_assert(deepm != deepn, "");
995
996  constexpr T<5> *p17_5 = &t17;
997  constexpr T<13> *p17_13 = (T<13>*)p17_5;
998  constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}}
999  static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
1000  static_assert(&(p17_13->*deepn) == &t17.n, "");
1001  constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
1002
1003  constexpr T<5> *p30_5 = &t30;
1004  constexpr T<23> *p30_23 = (T<23>*)p30_5;
1005  constexpr T<13> *p30_13 = p30_23;
1006  static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
1007  static_assert(&(p30_13->*deepn) == &t30.n, "");
1008  static_assert(&(p30_23->*deepn) == &t30.n, "");
1009  static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
1010  static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
1011  static_assert(&(p30_23->*deepm) == &t30.m, "");
1012
1013  struct Base { int n; };
1014  template<int N> struct Mid : Base {};
1015  struct Derived : Mid<0>, Mid<1> {};
1016  static_assert(&Mid<0>::n == &Mid<1>::n, "");
1017  static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n !=
1018                (int Derived::*)(int Mid<1>::*)&Mid<1>::n, "");
1019  static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, "");
1020}
1021
1022namespace ArrayBaseDerived {
1023
1024  struct Base {
1025    constexpr Base() {}
1026    int n = 0;
1027  };
1028  struct Derived : Base {
1029    constexpr Derived() {}
1030    constexpr const int *f() const { return &n; }
1031  };
1032
1033  constexpr Derived a[10];
1034  constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
1035  constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
1036  static_assert(pb3 == pd3, "");
1037
1038  // pb3 does not point to an array element.
1039  constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
1040  constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1041  constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1042  constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1043  constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1044  constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1045  constexpr Base *pb3a = pb4 - 1;
1046
1047  // pb4 does not point to a Derived.
1048  constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1049  constexpr Derived *pd3a = (Derived*)pb3a;
1050  constexpr int pd3n = pd3a->n;
1051
1052  // pd3a still points to the Derived array.
1053  constexpr Derived *pd6 = pd3a + 3;
1054  static_assert(pd6 == &a[6], "");
1055  constexpr Derived *pd9 = pd6 + 3;
1056  constexpr Derived *pd10 = pd6 + 4;
1057  constexpr int pd9n = pd9->n; // ok
1058  constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1059  constexpr int pd0n = pd10[-10].n;
1060  constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1061
1062  constexpr Base *pb9 = pd9;
1063  constexpr const int *(Base::*pfb)() const =
1064      static_cast<const int *(Base::*)() const>(&Derived::f);
1065  static_assert((pb9->*pfb)() == &a[9].n, "");
1066}
1067
1068namespace Complex {
1069
1070class complex {
1071  int re, im;
1072public:
1073  constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
1074  constexpr complex(const complex &o) : re(o.re), im(o.im) {}
1075  constexpr complex operator-() const { return complex(-re, -im); }
1076  friend constexpr complex operator+(const complex &l, const complex &r) {
1077    return complex(l.re + r.re, l.im + r.im);
1078  }
1079  friend constexpr complex operator-(const complex &l, const complex &r) {
1080    return l + -r;
1081  }
1082  friend constexpr complex operator*(const complex &l, const complex &r) {
1083    return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
1084  }
1085  friend constexpr bool operator==(const complex &l, const complex &r) {
1086    return l.re == r.re && l.im == r.im;
1087  }
1088  constexpr bool operator!=(const complex &r) const {
1089    return re != r.re || im != r.im;
1090  }
1091  constexpr int real() const { return re; }
1092  constexpr int imag() const { return im; }
1093};
1094
1095constexpr complex i = complex(0, 1);
1096constexpr complex k = (3 + 4*i) * (6 - 4*i);
1097static_assert(complex(1,0).real() == 1, "");
1098static_assert(complex(1,0).imag() == 0, "");
1099static_assert(((complex)1).imag() == 0, "");
1100static_assert(k.real() == 34, "");
1101static_assert(k.imag() == 12, "");
1102static_assert(k - 34 == 12*i, "");
1103static_assert((complex)1 == complex(1), "");
1104static_assert((complex)1 != complex(0, 1), "");
1105static_assert(complex(1) == complex(1), "");
1106static_assert(complex(1) != complex(0, 1), "");
1107constexpr complex makeComplex(int re, int im) { return complex(re, im); }
1108static_assert(makeComplex(1,0) == complex(1), "");
1109static_assert(makeComplex(1,0) != complex(0, 1), "");
1110
1111class complex_wrap : public complex {
1112public:
1113  constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
1114  constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
1115};
1116
1117static_assert((complex_wrap)1 == complex(1), "");
1118static_assert((complex)1 != complex_wrap(0, 1), "");
1119static_assert(complex(1) == complex_wrap(1), "");
1120static_assert(complex_wrap(1) != complex(0, 1), "");
1121constexpr complex_wrap makeComplexWrap(int re, int im) {
1122  return complex_wrap(re, im);
1123}
1124static_assert(makeComplexWrap(1,0) == complex(1), "");
1125static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1126
1127}
1128
1129namespace PR11595 {
1130  struct A { constexpr bool operator==(int x) const { return true; } };
1131  struct B { B(); A& x; };
1132  static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1133
1134  constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}}
1135    return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1136  }
1137}
1138
1139namespace ExprWithCleanups {
1140  struct A { A(); ~A(); int get(); };
1141  constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
1142  constexpr int n = get(false);
1143}
1144
1145namespace Volatile {
1146
1147volatile constexpr int n1 = 0; // expected-note {{here}}
1148volatile const int n2 = 0; // expected-note {{here}}
1149int n3 = 37; // expected-note {{declared here}}
1150
1151constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1152constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1153constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1154constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1155
1156struct T { int n; };
1157const T t = { 42 }; // expected-note {{declared here}}
1158
1159constexpr int f(volatile int &&r) {
1160  return r; // expected-note {{read of volatile-qualified type 'volatile int'}}
1161}
1162constexpr int g(volatile int &&r) {
1163  return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1164}
1165struct S {
1166  int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1167  int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1168  int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1169  int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
1170};
1171
1172}
1173
1174namespace ExternConstexpr {
1175  extern constexpr int n = 0;
1176  extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
1177  void f() {
1178    extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1179    constexpr int j = 0;
1180    constexpr int k; // expected-error {{default initialization of an object of const type}}
1181  }
1182}
1183
1184namespace ComplexConstexpr {
1185  constexpr _Complex float test1 = {};
1186  constexpr _Complex float test2 = {1};
1187  constexpr _Complex double test3 = {1,2};
1188  constexpr _Complex int test4 = {4};
1189  constexpr _Complex int test5 = 4;
1190  constexpr _Complex int test6 = {5,6};
1191  typedef _Complex float fcomplex;
1192  constexpr fcomplex test7 = fcomplex();
1193
1194  constexpr const double &t2r = __real test3;
1195  constexpr const double &t2i = __imag test3;
1196  static_assert(&t2r + 1 == &t2i, "");
1197  static_assert(t2r == 1.0, "");
1198  static_assert(t2i == 2.0, "");
1199  constexpr const double *t2p = &t2r;
1200  static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1201  static_assert(t2p[0] == 1.0, "");
1202  static_assert(t2p[1] == 2.0, "");
1203  static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1204  static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1205  constexpr _Complex float *p = 0;
1206  constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1207  constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1208  constexpr const _Complex double *q = &test3 + 1;
1209  constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1210  constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1211
1212  static_assert(__real test6 == 5, "");
1213  static_assert(__imag test6 == 6, "");
1214  static_assert(&__imag test6 == &__real test6 + 1, "");
1215}
1216
1217// _Atomic(T) is exactly like T for the purposes of constant expression
1218// evaluation..
1219namespace Atomic {
1220  constexpr _Atomic int n = 3;
1221
1222  struct S { _Atomic(double) d; };
1223  constexpr S s = { 0.5 };
1224  constexpr double d1 = s.d;
1225  constexpr double d2 = n;
1226  constexpr _Atomic double d3 = n;
1227
1228  constexpr _Atomic(int) n2 = d3;
1229  static_assert(d1 == 0.5, "");
1230  static_assert(d3 == 3.0, "");
1231
1232  namespace PR16056 {
1233    struct TestVar {
1234      _Atomic(int) value;
1235      constexpr TestVar(int value) : value(value) {}
1236    };
1237    constexpr TestVar testVar{-1};
1238    static_assert(testVar.value == -1, "");
1239  }
1240}
1241
1242namespace InstantiateCaseStmt {
1243  template<int x> constexpr int f() { return x; }
1244  template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
1245  int gg(int c) { return g<4>(c); }
1246}
1247
1248namespace ConvertedConstantExpr {
1249  extern int &m;
1250  extern int &n;
1251
1252  constexpr int k = 4;
1253  int &m = const_cast<int&>(k);
1254
1255  // If we have nothing more interesting to say, ensure we don't produce a
1256  // useless note and instead just point to the non-constant subexpression.
1257  enum class E {
1258    em = m,
1259    en = n, // expected-error {{not a constant expression}}
1260    eo = (m +
1261          n // expected-error {{not a constant expression}}
1262          ),
1263    eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1264  };
1265}
1266
1267namespace IndirectField {
1268  struct S {
1269    struct { // expected-warning {{GNU extension}}
1270      union { // expected-warning {{declared in an anonymous struct}}
1271        struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1272          int a;
1273          int b;
1274        };
1275        int c;
1276      };
1277      int d;
1278    };
1279    union {
1280      int e;
1281      int f;
1282    };
1283    constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {}
1284    constexpr S(int c, int d, int f) : c(c), d(d), f(f) {}
1285  };
1286
1287  constexpr S s1(1, 2, 3, 4);
1288  constexpr S s2(5, 6, 7);
1289
1290  // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1291  // member is active and which is requested.
1292  static_assert(s1.a == 1, "");
1293  static_assert(s1.b == 2, "");
1294  static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1295  static_assert(s1.d == 3, "");
1296  static_assert(s1.e == 4, "");
1297  static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1298
1299  static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1300  static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1301  static_assert(s2.c == 5, "");
1302  static_assert(s2.d == 6, "");
1303  static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1304  static_assert(s2.f == 7, "");
1305}
1306
1307// DR1405: don't allow reading mutable members in constant expressions.
1308namespace MutableMembers {
1309  struct MM {
1310    mutable int n; // expected-note 3{{declared here}}
1311  } constexpr mm = { 4 };
1312  constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1313  int x = (mm.n = 1, 3);
1314  constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1315
1316  // Here's one reason why allowing this would be a disaster...
1317  template<int n> struct Id { int k = n; };
1318  int f() {
1319    constexpr MM m = { 0 };
1320    ++m.n;
1321    return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1322  }
1323
1324  struct A { int n; };
1325  struct B { mutable A a; }; // expected-note {{here}}
1326  struct C { B b; };
1327  constexpr C c[3] = {};
1328  constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1329}
1330
1331namespace Fold {
1332
1333  // This macro forces its argument to be constant-folded, even if it's not
1334  // otherwise a constant expression.
1335  #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1336
1337  constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1338  constexpr int m = fold((int)(char*)123); // ok
1339  static_assert(m == 123, "");
1340
1341  #undef fold
1342
1343}
1344
1345namespace DR1454 {
1346
1347constexpr const int &f(const int &n) { return n; }
1348constexpr int k1 = f(0); // ok
1349
1350struct Wrap {
1351  const int &value;
1352};
1353constexpr const Wrap &g(const Wrap &w) { return w; }
1354constexpr int k2 = g({0}).value; // ok
1355
1356// The temporary here has static storage duration, so we can bind a constexpr
1357// reference to it.
1358constexpr const int &i = 1;
1359constexpr const int j = i;
1360static_assert(j == 1, "");
1361
1362// The temporary here is not const, so it can't be read outside the expression
1363// in which it was created (per the C++14 rules, which we use to avoid a C++11
1364// defect).
1365constexpr int &&k = 1; // expected-note {{temporary created here}}
1366constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}}
1367
1368void f() {
1369  // The temporary here has automatic storage duration, so we can't bind a
1370  // constexpr reference to it.
1371  constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1372}
1373
1374}
1375
1376namespace RecursiveOpaqueExpr {
1377  template<typename Iter>
1378  constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) {
1379    return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}}
1380  }
1381
1382  constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1383  static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, "");
1384
1385  constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1386  static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, "");
1387
1388  constexpr int arr3[] = {
1389    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1390    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1391    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1392    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1393    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1394    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1395    1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1396    2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1397  static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, "");
1398}
1399
1400namespace VLASizeof {
1401
1402  void f(int k) {
1403    int arr[k]; // expected-warning {{C99}}
1404    constexpr int n = 1 +
1405        sizeof(arr) // expected-error {{constant expression}}
1406        * 3;
1407  }
1408}
1409
1410namespace CompoundLiteral {
1411  // FIXME:
1412  // We don't model the semantics of this correctly: the compound literal is
1413  // represented as a prvalue in the AST, but actually behaves like an lvalue.
1414  // We treat the compound literal as a temporary and refuse to produce a
1415  // pointer to it. This is OK: we're not required to treat this as a constant
1416  // in C++, and in C we model compound literals as lvalues.
1417  constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}}
1418}
1419
1420namespace Vector {
1421  typedef int __attribute__((vector_size(16))) VI4;
1422  constexpr VI4 f(int n) {
1423    return VI4 { n * 3, n + 4, n - 5, n / 6 };
1424  }
1425  constexpr auto v1 = f(10);
1426
1427  typedef double __attribute__((vector_size(32))) VD4;
1428  constexpr VD4 g(int n) {
1429    return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1430  }
1431  constexpr auto v2 = g(4);
1432}
1433
1434// PR12626, redux
1435namespace InvalidClasses {
1436  void test0() {
1437    struct X; // expected-note {{forward declaration}}
1438    struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1439    Y y;
1440    auto& b = y.b;
1441  }
1442}
1443
1444namespace NamespaceAlias {
1445  constexpr int f() {
1446    namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++1y extension}}
1447    return &NS::f != nullptr;
1448  }
1449}
1450
1451// Constructors can be implicitly constexpr, even for a non-literal type.
1452namespace ImplicitConstexpr {
1453  struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1454  struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1455  struct S { R r; }; // expected-note 3{{here}}
1456  struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1457  struct U { T t; }; // expected-note 3{{here}}
1458  static_assert(!__is_literal_type(Q), "");
1459  static_assert(!__is_literal_type(R), "");
1460  static_assert(!__is_literal_type(S), "");
1461  static_assert(!__is_literal_type(T), "");
1462  static_assert(!__is_literal_type(U), "");
1463  struct Test {
1464    friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1465    friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1466    friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1467    friend S::S() noexcept; // expected-error {{follows constexpr}}
1468    friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1469    friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1470    friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}}
1471    friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}}
1472    friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}}
1473  };
1474}
1475
1476// Indirectly test that an implicit lvalue to xvalue conversion performed for
1477// an NRVO move operation isn't implemented as CK_LValueToRValue.
1478namespace PR12826 {
1479  struct Foo {};
1480  constexpr Foo id(Foo x) { return x; }
1481  constexpr Foo res(id(Foo()));
1482}
1483
1484namespace PR13273 {
1485  struct U {
1486    int t;
1487    U() = default;
1488  };
1489
1490  struct S : U {
1491    S() = default;
1492  };
1493
1494  // S's default constructor isn't constexpr, because U's default constructor
1495  // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1496  // actually call it.
1497  static_assert(S{}.t == 0, "");
1498}
1499
1500namespace PR12670 {
1501  struct S {
1502    constexpr S(int a0) : m(a0) {}
1503    constexpr S() : m(6) {}
1504    int m;
1505  };
1506  constexpr S x[3] = { {4}, 5 };
1507  static_assert(x[0].m == 4, "");
1508  static_assert(x[1].m == 5, "");
1509  static_assert(x[2].m == 6, "");
1510}
1511
1512// Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1513// when a conditional operator has one argument of type void and where the other
1514// is a glvalue of class type.
1515namespace ConditionalLValToRVal {
1516  struct A {
1517    constexpr A(int a) : v(a) {}
1518    int v;
1519  };
1520
1521  constexpr A f(const A &a) {
1522    return a.v == 0 ? throw a : a;
1523  }
1524
1525  constexpr A a(4);
1526  static_assert(f(a).v == 4, "");
1527}
1528
1529namespace TLS {
1530  __thread int n;
1531  int m;
1532
1533  constexpr bool b = &n == &n;
1534
1535  constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1536
1537  constexpr int *f() { return &n; }
1538  constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1539  constexpr bool c = f() == f();
1540
1541  constexpr int *g() { return &m; }
1542  constexpr int *r = g();
1543}
1544
1545namespace Void {
1546  constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}}
1547
1548  void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1549#define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1550  template<typename T, size_t S>
1551  constexpr T get(T (&a)[S], size_t k) {
1552    return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1553  }
1554#undef ASSERT
1555  template int get(int (&a)[4], size_t);
1556  constexpr int arr[] = { 4, 1, 2, 3, 4 };
1557  static_assert(get(arr, 1) == 1, "");
1558  static_assert(get(arr, 4) == 4, "");
1559  static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1560  // expected-note{{in call to 'get(arr, 0)'}}
1561}
1562
1563namespace std { struct type_info; }
1564
1565namespace TypeId {
1566  struct A { virtual ~A(); };
1567  A f();
1568  A &g();
1569  constexpr auto &x = typeid(f());
1570  constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \
1571  // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}}
1572}
1573
1574namespace PR14203 {
1575  struct duration {
1576    constexpr duration() {}
1577    constexpr operator int() const { return 0; }
1578  };
1579  template<typename T> void f() {
1580    // If we want to evaluate this at the point of the template definition, we
1581    // need to trigger the implicit definition of the move constructor at that
1582    // point.
1583    // FIXME: C++ does not permit us to implicitly define it at the appropriate
1584    // times, since it is only allowed to be implicitly defined when it is
1585    // odr-used.
1586    constexpr duration d = duration();
1587  }
1588  // FIXME: It's unclear whether this is valid. On the one hand, we're not
1589  // allowed to generate a move constructor. On the other hand, if we did,
1590  // this would be a constant expression. For now, we generate a move
1591  // constructor here.
1592  int n = sizeof(short{duration(duration())});
1593}
1594
1595namespace ArrayEltInit {
1596  struct A {
1597    constexpr A() : p(&p) {}
1598    void *p;
1599  };
1600  constexpr A a[10];
1601  static_assert(a[0].p == &a[0].p, "");
1602  static_assert(a[9].p == &a[9].p, "");
1603  static_assert(a[0].p != &a[9].p, "");
1604  static_assert(a[9].p != &a[0].p, "");
1605
1606  constexpr A b[10] = {};
1607  static_assert(b[0].p == &b[0].p, "");
1608  static_assert(b[9].p == &b[9].p, "");
1609  static_assert(b[0].p != &b[9].p, "");
1610  static_assert(b[9].p != &b[0].p, "");
1611}
1612
1613namespace PR15884 {
1614  struct S {};
1615  constexpr S f() { return {}; }
1616  constexpr S *p = &f();
1617  // expected-error@-1 {{taking the address of a temporary}}
1618  // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1619  // expected-note@-3 {{pointer to temporary is not a constant expression}}
1620  // expected-note@-4 {{temporary created here}}
1621}
1622
1623namespace AfterError {
1624  // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid.
1625  constexpr int error() { // expected-error {{no return statement}}
1626    return foobar; // expected-error {{undeclared identifier}}
1627  }
1628  constexpr int k = error(); // expected-error {{must be initialized by a constant expression}}
1629}
1630
1631namespace std {
1632  typedef decltype(sizeof(int)) size_t;
1633
1634  template <class _E>
1635  class initializer_list
1636  {
1637    const _E* __begin_;
1638    size_t    __size_;
1639
1640    constexpr initializer_list(const _E* __b, size_t __s)
1641      : __begin_(__b),
1642        __size_(__s)
1643    {}
1644
1645  public:
1646    typedef _E        value_type;
1647    typedef const _E& reference;
1648    typedef const _E& const_reference;
1649    typedef size_t    size_type;
1650
1651    typedef const _E* iterator;
1652    typedef const _E* const_iterator;
1653
1654    constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1655
1656    constexpr size_t    size()  const {return __size_;}
1657    constexpr const _E* begin() const {return __begin_;}
1658    constexpr const _E* end()   const {return __begin_ + __size_;}
1659  };
1660}
1661
1662namespace InitializerList {
1663  constexpr int sum(const int *b, const int *e) {
1664    return b != e ? *b + sum(b+1, e) : 0;
1665  }
1666  constexpr int sum(std::initializer_list<int> ints) {
1667    return sum(ints.begin(), ints.end());
1668  }
1669  static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1670}
1671
1672namespace StmtExpr {
1673  struct A { int k; };
1674  void f() {
1675    static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1676    constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1677  }
1678  constexpr int g(int k) {
1679    return ({ // expected-warning {{extension}}
1680      const int x = k;
1681      x * x;
1682    });
1683  }
1684  static_assert(g(123) == 15129, "");
1685  constexpr int h() { // expected-error {{never produces a constant}}
1686    return ({ // expected-warning {{extension}}
1687      return 0; // expected-note {{not supported}}
1688      1;
1689    });
1690  }
1691}
1692
1693namespace VirtualFromBase {
1694  struct S1 {
1695    virtual int f() const;
1696  };
1697  struct S2 {
1698    virtual int f();
1699  };
1700  template <typename T> struct X : T {
1701    constexpr X() {}
1702    double d = 0.0;
1703    constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++1y}}
1704  };
1705
1706  // Virtual f(), not OK.
1707  constexpr X<X<S1>> xxs1;
1708  constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1709  static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
1710
1711  // Non-virtual f(), OK.
1712  constexpr X<X<S2>> xxs2;
1713  constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1714  static_assert(q->f() == sizeof(S2), "");
1715}
1716
1717namespace ConstexprConstructorRecovery {
1718  class X {
1719  public:
1720      enum E : short {
1721          headers = 0x1,
1722          middlefile = 0x2,
1723          choices = 0x4
1724      };
1725      constexpr X() noexcept {};
1726  protected:
1727      E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}}
1728  };
1729  constexpr X x{};
1730}
1731
1732namespace Lifetime {
1733  void f() {
1734    constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1735    constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1736  }
1737
1738  constexpr int &get(int &&n) { return n; }
1739  struct S {
1740    int &&r; // expected-note 2{{declared here}}
1741    int &s;
1742    int t;
1743    constexpr S() : r(0), s(get(0)), t(r) {} // expected-warning {{temporary}}
1744    constexpr S(int) : r(0), s(get(0)), t(s) {} // expected-warning {{temporary}} expected-note {{read of object outside its lifetime}}
1745  };
1746  constexpr int k1 = S().t; // ok, int is lifetime-extended to end of constructor
1747  constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}}
1748}
1749
1750namespace Bitfields {
1751  struct A {
1752    bool b : 1;
1753    unsigned u : 5;
1754    int n : 5;
1755    bool b2 : 3;
1756    unsigned u2 : 74; // expected-warning {{exceeds the size of its type}}
1757    int n2 : 81; // expected-warning {{exceeds the size of its type}}
1758  };
1759
1760  constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
1761  static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
1762                a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
1763                "bad truncation of bitfield values");
1764
1765  struct B {
1766    int n : 3;
1767    constexpr B(int k) : n(k) {}
1768  };
1769  static_assert(B(3).n == 3, "");
1770  static_assert(B(4).n == -4, "");
1771  static_assert(B(7).n == -1, "");
1772  static_assert(B(8).n == 0, "");
1773  static_assert(B(-1).n == -1, "");
1774  static_assert(B(-8889).n == -1, "");
1775
1776  namespace PR16755 {
1777    struct X {
1778      int x : 1;
1779      constexpr static int f(int x) {
1780        return X{x}.x;
1781      }
1782    };
1783    static_assert(X::f(3) == -1, "3 should truncate to -1");
1784  }
1785}
1786
1787namespace ZeroSizeTypes {
1788  constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
1789  constexpr int k = p2 - p1;
1790  // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
1791  // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}}
1792
1793  int arr[5][0];
1794  constexpr int f() { // expected-error {{never produces a constant expression}}
1795    return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}}
1796  }
1797}
1798
1799namespace BadDefaultInit {
1800  template<int N> struct X { static const int n = N; };
1801
1802  struct A { // expected-note {{subexpression}}
1803    int k = X<A().k>::n; // expected-error {{defaulted default constructor of 'A' cannot be used}} expected-error {{not a constant expression}} expected-note {{in call to 'A()'}}
1804  };
1805
1806  // FIXME: The "constexpr constructor must initialize all members" diagnostic
1807  // here is bogus (we discard the k(k) initializer because the parameter 'k'
1808  // has been marked invalid).
1809  struct B { // expected-note 2{{candidate}}
1810    constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}}
1811        int k = X<B().k>::n) : // expected-error {{no matching constructor}}
1812      k(k) {}
1813    int k; // expected-note {{not initialized}}
1814  };
1815}
1816
1817namespace NeverConstantTwoWays {
1818  // If we see something non-constant but foldable followed by something
1819  // non-constant and not foldable, we want the first diagnostic, not the
1820  // second.
1821  constexpr int f(int n) { // expected-error {{never produces a constant expression}}
1822    return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
1823        1 / 0 : // expected-warning {{division by zero}}
1824        0;
1825  }
1826
1827  // FIXME: We should diagnose the cast to long here, not the division by zero.
1828  constexpr int n = // expected-error {{must be initialized by a constant expression}}
1829      (int *)(long)&n == &n ?
1830        1 / 0 : // expected-warning {{division by zero}} expected-note {{division by zero}}
1831        0;
1832}
1833
1834namespace PR17800 {
1835  struct A {
1836    constexpr int operator()() const { return 0; }
1837  };
1838  template <typename ...T> constexpr int sink(T ...) {
1839    return 0;
1840  }
1841  template <int ...N> constexpr int run() {
1842    return sink(A()() + N ...);
1843  }
1844  constexpr int k = run<1, 2, 3>();
1845}
1846
1847namespace BuiltinStrlen {
1848  constexpr const char *a = "foo\0quux";
1849  constexpr char b[] = "foo\0quux";
1850  constexpr int f() { return 'u'; }
1851  constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
1852
1853  static_assert(__builtin_strlen("foo") == 3, "");
1854  static_assert(__builtin_strlen("foo\0quux") == 3, "");
1855  static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
1856
1857  constexpr bool check(const char *p) {
1858    return __builtin_strlen(p) == 3 &&
1859           __builtin_strlen(p + 1) == 2 &&
1860           __builtin_strlen(p + 2) == 1 &&
1861           __builtin_strlen(p + 3) == 0 &&
1862           __builtin_strlen(p + 4) == 4 &&
1863           __builtin_strlen(p + 5) == 3 &&
1864           __builtin_strlen(p + 6) == 2 &&
1865           __builtin_strlen(p + 7) == 1 &&
1866           __builtin_strlen(p + 8) == 0;
1867  }
1868
1869  static_assert(check(a), "");
1870  static_assert(check(b), "");
1871  static_assert(check(c), "");
1872
1873  constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1874  constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1875  constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1876
1877  constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1878  constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1879  constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1880
1881  // FIXME: The diagnostic here could be better.
1882  constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
1883  constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1884}
1885
1886namespace PR19010 {
1887  struct Empty {};
1888  struct Empty2 : Empty {};
1889  struct Test : Empty2 {
1890    constexpr Test() {}
1891    Empty2 array[2];
1892  };
1893  void test() { constexpr Test t; }
1894}
1895