1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected -Wno-unused-value %s
// RUN: %clang_cc1 -verify=both,ref -Wno-unused-value %s
constexpr _Complex double z1 = {1.0, 2.0};
static_assert(__real(z1) == 1.0, "");
static_assert(__imag(z1) == 2.0, "");
static_assert(&__imag z1 == &__real z1 + 1, "");
static_assert((*(&__imag z1)) == __imag z1, "");
static_assert((*(&__real z1)) == __real z1, "");
static_assert(((1.25 + 0.5j) * (0.25 - 0.75j)) == (0.6875 - 0.8125j), "");
static_assert(((1.25 + 0.5j) * 0.25) == (0.3125 + 0.125j), "");
static_assert((1.25 * (0.25 - 0.75j)) == (0.3125 - 0.9375j), "");
constexpr _Complex float InfC = {1.0, __builtin_inf()};
constexpr _Complex float InfInf = __builtin_inf() + InfC;
static_assert(__real__(InfInf) == __builtin_inf());
static_assert(__imag__(InfInf) == __builtin_inf());
static_assert(__builtin_isnan(__real__(InfInf * InfInf)));
static_assert(__builtin_isinf_sign(__imag__(InfInf * InfInf)) == 1);
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * 1.0)) == 1);
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * 1.0)) == 1);
static_assert(__builtin_isinf_sign(__real__(1.0 * (__builtin_inf() + 1.0j))) == 1);
static_assert(__builtin_isinf_sign(__imag__(1.0 * (1.0 + InfC))) == 1);
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (1.0 + 1.0j))) == 1);
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + 1.0j))) == -1);
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * (1.0 + 1.0j))) == 1);
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (1.0 + InfC))) == -1);
static_assert(__builtin_isinf_sign(__imag__((1.0 + 1.0j) * (1.0 + InfC))) == 1);
static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + InfC))) == -1);
static_assert(__builtin_isinf_sign(__real__(InfInf * InfInf)) == 0);
constexpr _Complex int IIMA = {1,2};
constexpr _Complex int IIMB = {10,20};
constexpr _Complex int IIMC = IIMA * IIMB;
static_assert(__real(IIMC) == -30, "");
static_assert(__imag(IIMC) == 40, "");
static_assert(1.0j / 0.0 == 1); // both-error {{static assertion}} \
// both-note {{division by zero}}
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / (0.0 + 0.0j))) == 1);
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / 0.0)) == 1); // both-error {{static assertion}} \
// both-note {{division by zero}}
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (0.0 + 0.0j))) == 1);
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) / (0.0 + 0.0j))) == 1);
static_assert(__builtin_isinf_sign(__imag__((InfInf) / (0.0 + 0.0j))) == 1);
constexpr _Complex int IIDA = {10,20};
constexpr _Complex int IIDB = {1,2};
constexpr _Complex int IIDC = IIDA / IIDB;
static_assert(__real(IIDC) == 10, "");
static_assert(__imag(IIDC) == 0, "");
constexpr _Complex int Comma1 = {1, 2};
constexpr _Complex int Comma2 = (0, Comma1);
static_assert(Comma1 == Comma1, "");
constexpr double setter() {
_Complex float d = {1.0, 2.0};
__imag(d) = 4.0;
return __imag(d);
}
static_assert(setter() == 4, "");
constexpr _Complex double getter() {
return {1.0, 3.0};
}
constexpr _Complex double D = getter();
static_assert(__real(D) == 1.0, "");
static_assert(__imag(D) == 3.0, "");
constexpr _Complex int I1 = {1, 2};
static_assert(__real(I1) == 1, "");
static_assert(__imag(I1) == 2, "");
constexpr _Complex double D1 = {};
static_assert(__real(D1) == 0, "");
static_assert(__imag(D1) == 0, "");
constexpr _Complex int I2 = {};
static_assert(__real(I2) == 0, "");
static_assert(__imag(I2) == 0, "");
static_assert(__real(4.0) == 4.0, "");
static_assert(__real(12u) == 12u, "");
static_assert(__imag(4.0) == 0.0, "");
static_assert(__imag(13) == 0, "");
constexpr _Complex long L1 = D;
static_assert(__real(L1) == 1.0, "");
static_assert(__imag(L1) == 3.0, "");
constexpr _Complex short I4 = L1;
static_assert(__real(I4) == 1, "");
static_assert(__imag(I4) == 3, "");
constexpr _Complex float D3 = D;
static_assert(__real(D3) == 1.0, "");
static_assert(__imag(D3) == 3.0, "");
constexpr _Complex int a = 2i;
static_assert(__real(a) == 0, "");
static_assert(__imag(a) == 2, "");
constexpr _Complex double b = 4.0i;
static_assert(__real(b) == 0, "");
static_assert(__imag(b) == 4, "");
constexpr int ignored() {
I2;
(int)I2;
(float)I2;
D1;
(int)D1;
(double)D1;
(_Complex float)I2;
(bool)D1;
(bool)I2;
return 0;
}
static_assert(ignored() == 0, "");
static_assert((int)I1 == 1, "");
static_assert((float)D == 1.0f, "");
static_assert(__real((_Complex unsigned)5) == 5);
static_assert(__imag((_Complex unsigned)5) == 0);
/// Standalone complex expressions.
static_assert(__real((_Complex float){1.0, 3.0}) == 1.0, "");
constexpr _Complex double D2 = {12};
static_assert(__real(D2) == 12, "");
static_assert(__imag(D2) == 0, "");
constexpr _Complex int I3 = {15};
static_assert(__real(I3) == 15, "");
static_assert(__imag(I3) == 0, "");
constexpr _Complex _BitInt(8) A = {4};
static_assert(__real(A) == 4, "");
static_assert(__imag(A) == 0, "");
constexpr _Complex double Doubles[4] = {{1.0, 2.0}};
static_assert(__real(Doubles[0]) == 1.0, "");
static_assert(__imag(Doubles[0]) == 2.0, "");
static_assert(__real(Doubles[1]) == 0.0, "");
static_assert(__imag(Doubles[1]) == 0.0, "");
static_assert(__real(Doubles[2]) == 0.0, "");
static_assert(__imag(Doubles[2]) == 0.0, "");
static_assert(__real(Doubles[3]) == 0.0, "");
static_assert(__imag(Doubles[3]) == 0.0, "");
static_assert(~(0.5 + 1.5j) == (0.5 + -1.5j), "");
static_assert(__extension__ __imag(A) == 0, "");
static_assert(__imag(__extension__ A) == 0, "");
void func(void) {
__complex__ int arr;
_Complex int result;
int ii = 0;
int bb = 0;
/// The following line will call into the constant interpreter.
result = arr * ii;
}
constexpr _Complex float getComplexFloat() {
return {1,2};
}
static_assert(__real(getComplexFloat()) == 1, "");
static_assert(__imag(getComplexFloat()) == 2, "");
namespace CastToBool {
constexpr _Complex int F = {0, 1};
static_assert(F, "");
constexpr _Complex int F2 = {1, 0};
static_assert(F2, "");
constexpr _Complex int F3 = {0, 0};
static_assert(!F3, "");
constexpr _Complex unsigned char F4 = {0, 1};
static_assert(F4, "");
constexpr _Complex unsigned char F5 = {1, 0};
static_assert(F5, "");
constexpr _Complex unsigned char F6 = {0, 0};
static_assert(!F6, "");
constexpr _Complex float F7 = {0, 1};
static_assert(F7, "");
constexpr _Complex float F8 = {1, 0};
static_assert(F8, "");
constexpr _Complex double F9 = {0, 0};
static_assert(!F9, "");
}
namespace BinOps {
namespace Add {
constexpr _Complex float A = { 13.0, 2.0 };
constexpr _Complex float B = { 2.0, 1.0 };
constexpr _Complex float C = A + B;
static_assert(__real(C) == 15.0, "");
static_assert(__imag(C) == 3.0, "");
constexpr _Complex float D = B + A;
static_assert(__real(D) == 15.0, "");
static_assert(__imag(D) == 3.0, "");
constexpr _Complex unsigned int I1 = { 5, 10 };
constexpr _Complex unsigned int I2 = { 40, 2 };
constexpr _Complex unsigned int I3 = I1 + I2;
static_assert(__real(I3) == 45, "");
static_assert(__imag(I3) == 12, "");
static_assert(__real(A + 2.0) == 15, "");
static_assert(__imag(A + 2.0) == 2, "");
static_assert(__real(2.0 + A) == 15, "");
static_assert(__imag(2.0 + A) == 2, "");
static_assert(__real(D + 1) == 16, "");
static_assert(__real(D + 1.0) == 16, "");
constexpr _Complex double D2 = D + 3.0;
static_assert(__real(D2) == 18.0, "");
static_assert(__imag(D2) == 3.0, "");
constexpr _Complex double D3 = 3.0 + D;
static_assert(__real(D3) == 18.0, "");
static_assert(__imag(D3) == 3.0, "");
}
namespace Sub {
constexpr _Complex float A = { 13.0, 2.0 };
constexpr _Complex float B = { 2.0, 1.0 };
constexpr _Complex float C = A - B;
static_assert(__real(C) == 11.0, "");
static_assert(__imag(C) == 1.0, "");
static_assert(__real(A - 2.0) == 11, "");
static_assert(__real(2.0 - A) == -11, "");
constexpr _Complex float D = B - A;
static_assert(__real(D) == -11.0, "");
static_assert(__imag(D) == -1.0, "");
constexpr _Complex unsigned int I1 = { 5, 10 };
constexpr _Complex unsigned int I2 = { 40, 2 };
constexpr _Complex unsigned int I3 = I1 - I2;
static_assert(__real(I3) == -35, "");
static_assert(__imag(I3) == 8, "");
using Bobble = _Complex float;
constexpr _Complex float A_ = { 13.0, 2.0 };
constexpr Bobble B_ = { 2.0, 1.0 };
constexpr _Complex float D_ = A_ - B_;
static_assert(__real(D_) == 11.0, "");
static_assert(__imag(D_) == 1.0, "");
static_assert(__real(D - 1) == -12, "");
static_assert(__real(D - 1.0) == -12, "");
constexpr _Complex double D2 = D - 3.0;
static_assert(__real(D2) == -14.0, "");
static_assert(__imag(D2) == -1.0, "");
constexpr _Complex double D3 = 3.0 - D;
static_assert(__real(D3) == 14.0, "");
static_assert(__imag(D3) == 1.0, "");
}
}
namespace ZeroInit {
typedef _Complex float fcomplex;
typedef _Complex unsigned icomplex;
constexpr fcomplex test7 = fcomplex();
static_assert(__real(test7) == 0.0f, "");
static_assert(__imag(test7) == 0.0f, "");
constexpr icomplex test8 = icomplex();
static_assert(__real(test8) == 0, "");
static_assert(__imag(test8) == 0, "");
constexpr int ignored = (fcomplex(), 0);
}
namespace DeclRefCopy {
constexpr _Complex int ComplexInt = 42 + 24i;
constexpr _Complex int B = ComplexInt;
constexpr _Complex int ArrayOfComplexInt[4] = {ComplexInt, ComplexInt, ComplexInt, ComplexInt};
static_assert(__real(ArrayOfComplexInt[0]) == 42, "");
static_assert(__imag(ArrayOfComplexInt[0]) == 24, "");
static_assert(__real(ArrayOfComplexInt[3]) == 42, "");
static_assert(__imag(ArrayOfComplexInt[3]) == 24, "");
constexpr int localComplexArray() {
_Complex int A = 42 + 24i;
_Complex int ArrayOfComplexInt[4] = {A, A, A, A};
return __real(ArrayOfComplexInt[0]) + __imag(ArrayOfComplexInt[3]);
}
static_assert(localComplexArray() == (24 + 42), "");
}
namespace Builtin {
constexpr _Complex float A = __builtin_complex(10.0f, 20.0f);
static_assert(__real(A) == 10, "");
static_assert(__imag(A) == 20, "");
constexpr _Complex double B = __builtin_complex(10.0, 20.0);
static_assert(__real(B) == 10, "");
static_assert(__imag(B) == 20, "");
constexpr _Complex float C = __builtin_complex(10.0f, 20.0); // both-error {{arguments are of different types}}
}
namespace Cmp {
static_assert((0.0 + 0.0j) == (0.0 + 0.0j));
static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((0.0 + 0.0j) == 0.0);
static_assert(0.0 == (0.0 + 0.0j));
static_assert(0.0 == 0.0j);
static_assert((0.0 + 1.0j) != 0.0);
static_assert(1.0 != (0.0 + 0.0j));
static_assert(0.0 != 1.0j);
// Walk around the complex plane stepping between angular differences and
// equality.
static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((1.0 + 0.0j) == (1.0 + 0.0j));
static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((1.0 + 1.0j) == (1.0 + 1.0j));
static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((0.0 + 1.0j) == (0.0 + 1.0j));
static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j));
static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j));
static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j));
static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((0.0 - 1.0j) == (0.0 - 1.0j));
static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // both-error {{static assertion}} \
// both-note {{evaluates to}}
static_assert((1.0 - 1.0j) == (1.0 - 1.0j));
/// Make sure these are rejected before reaching the constexpr interpreter.
static_assert((0.0 + 0.0j) & (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
static_assert((0.0 + 0.0j) | (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
static_assert((0.0 + 0.0j) < (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
static_assert((0.0 + 0.0j) > (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
static_assert((0.0 + 0.0j) ^ (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
}
/// From test/SemaCXX/constant-expression-cxx11.cpp
///
/// Some of the diagnostics we emit are different than the one of the
/// current interpreter.
///
/// FIXME: For the '&test3 + 1' test, we are _not_ creating an explicit pointer variable
/// anywhere and so the &test3+1 is the same as __imag(test3) for us.
namespace ComplexConstexpr {
constexpr _Complex float test1 = {};
constexpr _Complex float test2 = {1};
constexpr _Complex double test3 = {1,2};
constexpr _Complex int test4 = {4};
constexpr _Complex int test5 = 4;
constexpr _Complex int test6 = {5,6};
typedef _Complex float fcomplex;
constexpr fcomplex test7 = fcomplex();
constexpr const double &t2r = __real test3;
constexpr const double &t2i = __imag test3;
static_assert(&t2r + 1 == &t2i, "");
static_assert(t2r == 1.0, "");
static_assert(t2i == 2.0, "");
constexpr const double *t2p = &t2r;
static_assert(t2p[-1] == 0.0, ""); // both-error {{constant expr}} \
// both-note {{cannot refer to element -1 of array of 2 elements}}
static_assert(t2p[0] == 1.0, "");
static_assert(t2p[1] == 2.0, "");
static_assert(t2p[2] == 0.0, ""); // both-error {{constant expr}} \
// both-note {{one-past-the-end pointer}}
static_assert(t2p[3] == 0.0, ""); // both-error {{constant expr}} \
// both-note {{cannot refer to element 3 of array of 2 elements}}
constexpr _Complex float *p = 0;
constexpr float pr = __real *p; // both-error {{constant expr}} \
// ref-note {{cannot access real component of null}} \
// expected-note {{read of dereferenced null pointer}}
constexpr float pi = __imag *p; // both-error {{constant expr}} \
// ref-note {{cannot access imaginary component of null}} \
// expected-note {{cannot perform pointer arithmetic on null pointer}}
constexpr const _Complex double *q = &test3 + 1;
constexpr double qr = __real *q; // ref-error {{constant expr}} \
// ref-note {{cannot access real component of pointer past the end}}
constexpr double qi = __imag *q; // both-error {{constant expr}} \
// ref-note {{cannot access imaginary component of pointer past the end}} \
// expected-note {{read of dereferenced one-past-the-end pointer}}
static_assert(__real test6 == 5, "");
static_assert(__imag test6 == 6, "");
static_assert(&__imag test6 == &__real test6 + 1, "");
}
|