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