| 1 |
/** |
| 2 |
* Perform constant folding of arithmetic expressions. |
| 3 |
* |
| 4 |
* The routines in this module are called from `optimize.d`. |
| 5 |
* |
| 6 |
* Specification: $(LINK2 https://dlang.org/spec/float.html#fp_const_folding, Floating Point Constant Folding) |
| 7 |
* |
| 8 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 9 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 10 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 11 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/constfold.d, _constfold.d) |
| 12 |
* Documentation: https://dlang.org/phobos/dmd_constfold.html |
| 13 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/constfold.d |
| 14 |
*/ |
| 15 |
|
| 16 |
module dmd.constfold; |
| 17 |
|
| 18 |
import core.stdc.string; |
| 19 |
import core.stdc.stdio; |
| 20 |
import dmd.arraytypes; |
| 21 |
import dmd.astenums; |
| 22 |
import dmd.ctfeexpr; |
| 23 |
import dmd.dcast; |
| 24 |
import dmd.declaration; |
| 25 |
import dmd.dstruct; |
| 26 |
import dmd.errors; |
| 27 |
import dmd.expression; |
| 28 |
import dmd.globals; |
| 29 |
import dmd.location; |
| 30 |
import dmd.mtype; |
| 31 |
import dmd.root.complex; |
| 32 |
import dmd.root.ctfloat; |
| 33 |
import dmd.root.port; |
| 34 |
import dmd.root.rmem; |
| 35 |
import dmd.root.utf; |
| 36 |
import dmd.sideeffect; |
| 37 |
import dmd.target; |
| 38 |
import dmd.tokens; |
| 39 |
import dmd.typesem : toDsymbol, equivalent, sarrayOf, size; |
| 40 |
|
| 41 |
private enum LOG = false; |
| 42 |
|
| 43 |
private Expression expType(Type type, Expression e) |
| 44 |
{ |
| 45 |
if (type != e.type) |
| 46 |
{ |
| 47 |
e = e.copy(); |
| 48 |
e.type = type; |
| 49 |
} |
| 50 |
return e; |
| 51 |
} |
| 52 |
|
| 53 |
/********************************** |
| 54 |
* Initialize a EXP.cantExpression Expression. |
| 55 |
* Params: |
| 56 |
* ue = where to write it |
| 57 |
*/ |
| 58 |
void cantExp(out UnionExp ue) |
| 59 |
{ |
| 60 |
emplaceExp!(CTFEExp)(&ue, EXP.cantExpression); |
| 61 |
} |
| 62 |
|
| 63 |
/* =============================== constFold() ============================== */ |
| 64 |
/* The constFold() functions were redundant with the optimize() ones, |
| 65 |
* and so have been folded in with them. |
| 66 |
*/ |
| 67 |
/* ========================================================================== */ |
| 68 |
UnionExp Neg(Type type, Expression e1) |
| 69 |
{ |
| 70 |
UnionExp ue = void; |
| 71 |
Loc loc = e1.loc; |
| 72 |
if (e1.type.isReal()) |
| 73 |
{ |
| 74 |
emplaceExp!(RealExp)(&ue, loc, -e1.toReal(), type); |
| 75 |
} |
| 76 |
else if (e1.type.isImaginary()) |
| 77 |
{ |
| 78 |
emplaceExp!(RealExp)(&ue, loc, -e1.toImaginary(), type); |
| 79 |
} |
| 80 |
else if (e1.type.isComplex()) |
| 81 |
{ |
| 82 |
emplaceExp!(ComplexExp)(&ue, loc, -e1.toComplex(), type); |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
emplaceExp!(IntegerExp)(&ue, loc, -e1.toInteger(), type); |
| 87 |
} |
| 88 |
return ue; |
| 89 |
} |
| 90 |
|
| 91 |
UnionExp Com(Type type, Expression e1) |
| 92 |
{ |
| 93 |
UnionExp ue = void; |
| 94 |
Loc loc = e1.loc; |
| 95 |
emplaceExp!(IntegerExp)(&ue, loc, ~e1.toInteger(), type); |
| 96 |
return ue; |
| 97 |
} |
| 98 |
|
| 99 |
UnionExp Not(Type type, Expression e1) |
| 100 |
{ |
| 101 |
UnionExp ue = void; |
| 102 |
Loc loc = e1.loc; |
| 103 |
// BUG: Should be replaced with e1.toBool().get(), but this is apparently |
| 104 |
// executed for some expressions that cannot be const-folded |
| 105 |
// To be fixed in another PR |
| 106 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toBool().hasValue(false) ? 1 : 0, type); |
| 107 |
return ue; |
| 108 |
} |
| 109 |
|
| 110 |
UnionExp Add(Loc loc, Type type, Expression e1, Expression e2) |
| 111 |
{ |
| 112 |
UnionExp ue = void; |
| 113 |
static if (LOG) |
| 114 |
{ |
| 115 |
printf("Add(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); |
| 116 |
} |
| 117 |
if (type.isReal()) |
| 118 |
{ |
| 119 |
emplaceExp!(RealExp)(&ue, loc, e1.toReal() + e2.toReal(), type); |
| 120 |
} |
| 121 |
else if (type.isImaginary()) |
| 122 |
{ |
| 123 |
emplaceExp!(RealExp)(&ue, loc, e1.toImaginary() + e2.toImaginary(), type); |
| 124 |
} |
| 125 |
else if (type.isComplex()) |
| 126 |
{ |
| 127 |
// This rigamarole is necessary so that -0.0 doesn't get |
| 128 |
// converted to +0.0 by doing an extraneous add with +0.0 |
| 129 |
auto c1 = complex_t(CTFloat.zero); |
| 130 |
real_t r1 = CTFloat.zero; |
| 131 |
real_t i1 = CTFloat.zero; |
| 132 |
auto c2 = complex_t(CTFloat.zero); |
| 133 |
real_t r2 = CTFloat.zero; |
| 134 |
real_t i2 = CTFloat.zero; |
| 135 |
auto v = complex_t(CTFloat.zero); |
| 136 |
int x; |
| 137 |
if (e1.type.isReal()) |
| 138 |
{ |
| 139 |
r1 = e1.toReal(); |
| 140 |
x = 0; |
| 141 |
} |
| 142 |
else if (e1.type.isImaginary()) |
| 143 |
{ |
| 144 |
i1 = e1.toImaginary(); |
| 145 |
x = 3; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
c1 = e1.toComplex(); |
| 150 |
x = 6; |
| 151 |
} |
| 152 |
if (e2.type.isReal()) |
| 153 |
{ |
| 154 |
r2 = e2.toReal(); |
| 155 |
} |
| 156 |
else if (e2.type.isImaginary()) |
| 157 |
{ |
| 158 |
i2 = e2.toImaginary(); |
| 159 |
x += 1; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
c2 = e2.toComplex(); |
| 164 |
x += 2; |
| 165 |
} |
| 166 |
switch (x) |
| 167 |
{ |
| 168 |
case 0 + 0: |
| 169 |
v = complex_t(r1 + r2); |
| 170 |
break; |
| 171 |
case 0 + 1: |
| 172 |
v = complex_t(r1, i2); |
| 173 |
break; |
| 174 |
case 0 + 2: |
| 175 |
v = complex_t(r1 + creall(c2), cimagl(c2)); |
| 176 |
break; |
| 177 |
case 3 + 0: |
| 178 |
v = complex_t(r2, i1); |
| 179 |
break; |
| 180 |
case 3 + 1: |
| 181 |
v = complex_t(CTFloat.zero, i1 + i2); |
| 182 |
break; |
| 183 |
case 3 + 2: |
| 184 |
v = complex_t(creall(c2), i1 + cimagl(c2)); |
| 185 |
break; |
| 186 |
case 6 + 0: |
| 187 |
v = complex_t(creall(c1) + r2, cimagl(c2)); |
| 188 |
break; |
| 189 |
case 6 + 1: |
| 190 |
v = complex_t(creall(c1), cimagl(c1) + i2); |
| 191 |
break; |
| 192 |
case 6 + 2: |
| 193 |
v = c1 + c2; |
| 194 |
break; |
| 195 |
default: |
| 196 |
assert(0); |
| 197 |
} |
| 198 |
emplaceExp!(ComplexExp)(&ue, loc, v, type); |
| 199 |
} |
| 200 |
else if (SymOffExp soe = e1.isSymOffExp()) |
| 201 |
{ |
| 202 |
emplaceExp!(SymOffExp)(&ue, loc, soe.var, soe.offset + e2.toInteger()); |
| 203 |
ue.exp().type = type; |
| 204 |
} |
| 205 |
else if (SymOffExp soe = e2.isSymOffExp()) |
| 206 |
{ |
| 207 |
emplaceExp!(SymOffExp)(&ue, loc, soe.var, soe.offset + e1.toInteger()); |
| 208 |
ue.exp().type = type; |
| 209 |
} |
| 210 |
else |
| 211 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() + e2.toInteger(), type); |
| 212 |
return ue; |
| 213 |
} |
| 214 |
|
| 215 |
UnionExp Min(Loc loc, Type type, Expression e1, Expression e2) |
| 216 |
{ |
| 217 |
// Compute e1-e2 as e1+(-e2) |
| 218 |
UnionExp neg = Neg(e2.type, e2); |
| 219 |
UnionExp ue = Add(loc, type, e1, neg.exp()); |
| 220 |
return ue; |
| 221 |
} |
| 222 |
|
| 223 |
UnionExp Mul(Loc loc, Type type, Expression e1, Expression e2) |
| 224 |
{ |
| 225 |
UnionExp ue = void; |
| 226 |
if (type.isFloating()) |
| 227 |
{ |
| 228 |
auto c = complex_t(CTFloat.zero); |
| 229 |
real_t r = CTFloat.zero; |
| 230 |
if (e1.type.isReal()) |
| 231 |
{ |
| 232 |
r = e1.toReal(); |
| 233 |
c = e2.toComplex(); |
| 234 |
c = complex_t(r * creall(c), r * cimagl(c)); |
| 235 |
} |
| 236 |
else if (e1.type.isImaginary()) |
| 237 |
{ |
| 238 |
r = e1.toImaginary(); |
| 239 |
c = e2.toComplex(); |
| 240 |
c = complex_t(-r * cimagl(c), r * creall(c)); |
| 241 |
} |
| 242 |
else if (e2.type.isReal()) |
| 243 |
{ |
| 244 |
r = e2.toReal(); |
| 245 |
c = e1.toComplex(); |
| 246 |
c = complex_t(r * creall(c), r * cimagl(c)); |
| 247 |
} |
| 248 |
else if (e2.type.isImaginary()) |
| 249 |
{ |
| 250 |
r = e2.toImaginary(); |
| 251 |
c = e1.toComplex(); |
| 252 |
c = complex_t(-r * cimagl(c), r * creall(c)); |
| 253 |
} |
| 254 |
else |
| 255 |
c = e1.toComplex() * e2.toComplex(); |
| 256 |
if (type.isReal()) |
| 257 |
emplaceExp!(RealExp)(&ue, loc, creall(c), type); |
| 258 |
else if (type.isImaginary()) |
| 259 |
emplaceExp!(RealExp)(&ue, loc, cimagl(c), type); |
| 260 |
else if (type.isComplex()) |
| 261 |
emplaceExp!(ComplexExp)(&ue, loc, c, type); |
| 262 |
else |
| 263 |
assert(0); |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() * e2.toInteger(), type); |
| 268 |
} |
| 269 |
return ue; |
| 270 |
} |
| 271 |
|
| 272 |
UnionExp Div(Loc loc, Type type, Expression e1, Expression e2) |
| 273 |
{ |
| 274 |
UnionExp ue = void; |
| 275 |
if (type.isFloating()) |
| 276 |
{ |
| 277 |
auto c = complex_t(CTFloat.zero); |
| 278 |
if (e2.type.isReal()) |
| 279 |
{ |
| 280 |
if (e1.type.isReal()) |
| 281 |
{ |
| 282 |
emplaceExp!(RealExp)(&ue, loc, e1.toReal() / e2.toReal(), type); |
| 283 |
return ue; |
| 284 |
} |
| 285 |
const r = e2.toReal(); |
| 286 |
c = e1.toComplex(); |
| 287 |
c = complex_t(creall(c) / r, cimagl(c) / r); |
| 288 |
} |
| 289 |
else if (e2.type.isImaginary()) |
| 290 |
{ |
| 291 |
const r = e2.toImaginary(); |
| 292 |
c = e1.toComplex(); |
| 293 |
c = complex_t(cimagl(c) / r, -creall(c) / r); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
c = e1.toComplex() / e2.toComplex(); |
| 298 |
} |
| 299 |
|
| 300 |
if (type.isReal()) |
| 301 |
emplaceExp!(RealExp)(&ue, loc, creall(c), type); |
| 302 |
else if (type.isImaginary()) |
| 303 |
emplaceExp!(RealExp)(&ue, loc, cimagl(c), type); |
| 304 |
else if (type.isComplex()) |
| 305 |
emplaceExp!(ComplexExp)(&ue, loc, c, type); |
| 306 |
else |
| 307 |
assert(0); |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
sinteger_t n1; |
| 312 |
sinteger_t n2; |
| 313 |
sinteger_t n; |
| 314 |
n1 = e1.toInteger(); |
| 315 |
n2 = e2.toInteger(); |
| 316 |
if (n2 == 0) |
| 317 |
{ |
| 318 |
error(e2.loc, "divide by 0"); |
| 319 |
emplaceExp!(ErrorExp)(&ue); |
| 320 |
return ue; |
| 321 |
} |
| 322 |
if (n2 == -1 && !type.isUnsigned()) |
| 323 |
{ |
| 324 |
// Check for int.min / -1 |
| 325 |
if (n1 == 0xFFFFFFFF80000000UL && type.toBasetype().ty != Tint64) |
| 326 |
{ |
| 327 |
error(e2.loc, "integer overflow: `int.min / -1`"); |
| 328 |
emplaceExp!(ErrorExp)(&ue); |
| 329 |
return ue; |
| 330 |
} |
| 331 |
else if (n1 == 0x8000000000000000L) // long.min / -1 |
| 332 |
{ |
| 333 |
error(e2.loc, "integer overflow: `long.min / -1L`"); |
| 334 |
emplaceExp!(ErrorExp)(&ue); |
| 335 |
return ue; |
| 336 |
} |
| 337 |
} |
| 338 |
if (e1.type.isUnsigned() || e2.type.isUnsigned()) |
| 339 |
n = (cast(dinteger_t)n1) / (cast(dinteger_t)n2); |
| 340 |
else |
| 341 |
n = n1 / n2; |
| 342 |
emplaceExp!(IntegerExp)(&ue, loc, n, type); |
| 343 |
} |
| 344 |
return ue; |
| 345 |
} |
| 346 |
|
| 347 |
UnionExp Mod(Loc loc, Type type, Expression e1, Expression e2) |
| 348 |
{ |
| 349 |
UnionExp ue = void; |
| 350 |
if (type.isFloating()) |
| 351 |
{ |
| 352 |
auto c = complex_t(CTFloat.zero); |
| 353 |
if (e2.type.isReal()) |
| 354 |
{ |
| 355 |
const r2 = e2.toReal(); |
| 356 |
c = complex_t(e1.toReal() % r2, e1.toImaginary() % r2); |
| 357 |
} |
| 358 |
else if (e2.type.isImaginary()) |
| 359 |
{ |
| 360 |
const i2 = e2.toImaginary(); |
| 361 |
c = complex_t(e1.toReal() % i2, e1.toImaginary() % i2); |
| 362 |
} |
| 363 |
else |
| 364 |
assert(0); |
| 365 |
if (type.isReal()) |
| 366 |
emplaceExp!(RealExp)(&ue, loc, creall(c), type); |
| 367 |
else if (type.isImaginary()) |
| 368 |
emplaceExp!(RealExp)(&ue, loc, cimagl(c), type); |
| 369 |
else if (type.isComplex()) |
| 370 |
emplaceExp!(ComplexExp)(&ue, loc, c, type); |
| 371 |
else |
| 372 |
assert(0); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
sinteger_t n1; |
| 377 |
sinteger_t n2; |
| 378 |
sinteger_t n; |
| 379 |
n1 = e1.toInteger(); |
| 380 |
n2 = e2.toInteger(); |
| 381 |
if (n2 == 0) |
| 382 |
{ |
| 383 |
error(e2.loc, "divide by 0"); |
| 384 |
emplaceExp!(ErrorExp)(&ue); |
| 385 |
return ue; |
| 386 |
} |
| 387 |
if (n2 == -1 && !type.isUnsigned()) |
| 388 |
{ |
| 389 |
// Check for int.min % -1 |
| 390 |
if (n1 == 0xFFFFFFFF80000000UL && type.toBasetype().ty != Tint64) |
| 391 |
{ |
| 392 |
error(e2.loc, "integer overflow: `int.min %% -1`"); |
| 393 |
emplaceExp!(ErrorExp)(&ue); |
| 394 |
return ue; |
| 395 |
} |
| 396 |
else if (n1 == 0x8000000000000000L) // long.min % -1 |
| 397 |
{ |
| 398 |
error(e2.loc, "integer overflow: `long.min %% -1L`"); |
| 399 |
emplaceExp!(ErrorExp)(&ue); |
| 400 |
return ue; |
| 401 |
} |
| 402 |
} |
| 403 |
if (e1.type.isUnsigned() || e2.type.isUnsigned()) |
| 404 |
n = (cast(dinteger_t)n1) % (cast(dinteger_t)n2); |
| 405 |
else |
| 406 |
n = n1 % n2; |
| 407 |
emplaceExp!(IntegerExp)(&ue, loc, n, type); |
| 408 |
} |
| 409 |
return ue; |
| 410 |
} |
| 411 |
|
| 412 |
UnionExp Pow(Loc loc, Type type, Expression e1, Expression e2) |
| 413 |
{ |
| 414 |
//printf("Pow()\n"); |
| 415 |
UnionExp ue; |
| 416 |
// Handle integer power operations. |
| 417 |
if (e2.type.isIntegral()) |
| 418 |
{ |
| 419 |
dinteger_t n = e2.toInteger(); |
| 420 |
bool neg; |
| 421 |
if (!e2.type.isUnsigned() && cast(sinteger_t)n < 0) |
| 422 |
{ |
| 423 |
if (e1.type.isIntegral()) |
| 424 |
{ |
| 425 |
cantExp(ue); |
| 426 |
return ue; |
| 427 |
} |
| 428 |
// Don't worry about overflow, from now on n is unsigned. |
| 429 |
neg = true; |
| 430 |
n = -n; |
| 431 |
} |
| 432 |
else |
| 433 |
neg = false; |
| 434 |
UnionExp ur, uv; |
| 435 |
if (e1.type.isComplex()) |
| 436 |
{ |
| 437 |
emplaceExp!(ComplexExp)(&ur, loc, e1.toComplex(), e1.type); |
| 438 |
emplaceExp!(ComplexExp)(&uv, loc, complex_t(CTFloat.one), e1.type); |
| 439 |
} |
| 440 |
else if (e1.type.isFloating()) |
| 441 |
{ |
| 442 |
emplaceExp!(RealExp)(&ur, loc, e1.toReal(), e1.type); |
| 443 |
emplaceExp!(RealExp)(&uv, loc, CTFloat.one, e1.type); |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
emplaceExp!(IntegerExp)(&ur, loc, e1.toInteger(), e1.type); |
| 448 |
emplaceExp!(IntegerExp)(&uv, loc, 1, e1.type); |
| 449 |
} |
| 450 |
Expression r = ur.exp(); |
| 451 |
Expression v = uv.exp(); |
| 452 |
while (n != 0) |
| 453 |
{ |
| 454 |
if (n & 1) |
| 455 |
{ |
| 456 |
// v = v * r; |
| 457 |
uv = Mul(loc, v.type, v, r); |
| 458 |
} |
| 459 |
n >>= 1; |
| 460 |
// r = r * r |
| 461 |
ur = Mul(loc, r.type, r, r); |
| 462 |
} |
| 463 |
if (neg) |
| 464 |
{ |
| 465 |
// ue = 1.0 / v |
| 466 |
UnionExp one; |
| 467 |
emplaceExp!(RealExp)(&one, loc, CTFloat.one, v.type); |
| 468 |
uv = Div(loc, v.type, one.exp(), v); |
| 469 |
} |
| 470 |
if (type.isComplex()) |
| 471 |
emplaceExp!(ComplexExp)(&ue, loc, v.toComplex(), type); |
| 472 |
else if (type.isIntegral()) |
| 473 |
emplaceExp!(IntegerExp)(&ue, loc, v.toInteger(), type); |
| 474 |
else |
| 475 |
emplaceExp!(RealExp)(&ue, loc, v.toReal(), type); |
| 476 |
} |
| 477 |
else if (e2.type.isFloating()) |
| 478 |
{ |
| 479 |
// x ^^ y for x < 0 and y not an integer is not defined; so set result as NaN |
| 480 |
if (e1.toReal() < CTFloat.zero) |
| 481 |
{ |
| 482 |
emplaceExp!(RealExp)(&ue, loc, target.RealProperties.nan, type); |
| 483 |
} |
| 484 |
else |
| 485 |
cantExp(ue); |
| 486 |
} |
| 487 |
else |
| 488 |
cantExp(ue); |
| 489 |
return ue; |
| 490 |
} |
| 491 |
|
| 492 |
UnionExp Shl(Loc loc, Type type, Expression e1, Expression e2) |
| 493 |
{ |
| 494 |
UnionExp ue = void; |
| 495 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() << e2.toInteger(), type); |
| 496 |
return ue; |
| 497 |
} |
| 498 |
|
| 499 |
UnionExp Shr(Loc loc, Type type, Expression e1, Expression e2) |
| 500 |
{ |
| 501 |
UnionExp ue = void; |
| 502 |
dinteger_t value = e1.toInteger(); |
| 503 |
dinteger_t dcount = e2.toInteger(); |
| 504 |
assert(dcount <= 0xFFFFFFFF); |
| 505 |
uint count = cast(uint)dcount; |
| 506 |
switch (e1.type.toBasetype().ty) |
| 507 |
{ |
| 508 |
case Tint8: |
| 509 |
value = cast(byte)value >> count; |
| 510 |
break; |
| 511 |
case Tuns8: |
| 512 |
case Tchar: |
| 513 |
value = cast(ubyte)value >> count; |
| 514 |
break; |
| 515 |
case Tint16: |
| 516 |
value = cast(short)value >> count; |
| 517 |
break; |
| 518 |
case Tuns16: |
| 519 |
case Twchar: |
| 520 |
value = cast(ushort)value >> count; |
| 521 |
break; |
| 522 |
case Tint32: |
| 523 |
value = cast(int)value >> count; |
| 524 |
break; |
| 525 |
case Tuns32: |
| 526 |
case Tdchar: |
| 527 |
value = cast(uint)value >> count; |
| 528 |
break; |
| 529 |
case Tint64: |
| 530 |
value = cast(long)value >> count; |
| 531 |
break; |
| 532 |
case Tuns64: |
| 533 |
value = cast(ulong)value >> count; |
| 534 |
break; |
| 535 |
case Terror: |
| 536 |
emplaceExp!(ErrorExp)(&ue); |
| 537 |
return ue; |
| 538 |
default: |
| 539 |
assert(0); |
| 540 |
} |
| 541 |
emplaceExp!(IntegerExp)(&ue, loc, value, type); |
| 542 |
return ue; |
| 543 |
} |
| 544 |
|
| 545 |
UnionExp Ushr(Loc loc, Type type, Expression e1, Expression e2) |
| 546 |
{ |
| 547 |
UnionExp ue = void; |
| 548 |
dinteger_t value = e1.toInteger(); |
| 549 |
dinteger_t dcount = e2.toInteger(); |
| 550 |
assert(dcount <= 0xFFFFFFFF); |
| 551 |
uint count = cast(uint)dcount; |
| 552 |
switch (e1.type.toBasetype().ty) |
| 553 |
{ |
| 554 |
case Tint8: |
| 555 |
case Tuns8: |
| 556 |
case Tchar: |
| 557 |
// Possible only with >>>=. >>> always gets promoted to int. |
| 558 |
value = (value & 0xFF) >>> count; |
| 559 |
break; |
| 560 |
case Tint16: |
| 561 |
case Tuns16: |
| 562 |
case Twchar: |
| 563 |
// Possible only with >>>=. >>> always gets promoted to int. |
| 564 |
value = (value & 0xFFFF) >>> count; |
| 565 |
break; |
| 566 |
case Tint32: |
| 567 |
case Tuns32: |
| 568 |
case Tdchar: |
| 569 |
value = (value & 0xFFFFFFFF) >>> count; |
| 570 |
break; |
| 571 |
case Tint64: |
| 572 |
case Tuns64: |
| 573 |
value = value >>> count; |
| 574 |
break; |
| 575 |
case Terror: |
| 576 |
emplaceExp!(ErrorExp)(&ue); |
| 577 |
return ue; |
| 578 |
default: |
| 579 |
assert(0); |
| 580 |
} |
| 581 |
emplaceExp!(IntegerExp)(&ue, loc, value, type); |
| 582 |
return ue; |
| 583 |
} |
| 584 |
|
| 585 |
UnionExp And(Loc loc, Type type, Expression e1, Expression e2) |
| 586 |
{ |
| 587 |
UnionExp ue = void; |
| 588 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() & e2.toInteger(), type); |
| 589 |
return ue; |
| 590 |
} |
| 591 |
|
| 592 |
UnionExp Or(Loc loc, Type type, Expression e1, Expression e2) |
| 593 |
{ |
| 594 |
UnionExp ue = void; |
| 595 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() | e2.toInteger(), type); |
| 596 |
return ue; |
| 597 |
} |
| 598 |
|
| 599 |
UnionExp Xor(Loc loc, Type type, Expression e1, Expression e2) |
| 600 |
{ |
| 601 |
//printf("Xor(linnum = %d, e1 = %s, e2 = %s)\n", loc.linnum, e1.toChars(), e2.toChars()); |
| 602 |
UnionExp ue = void; |
| 603 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger() ^ e2.toInteger(), type); |
| 604 |
return ue; |
| 605 |
} |
| 606 |
|
| 607 |
/* Also returns EXP.cantExpression if cannot be computed. |
| 608 |
*/ |
| 609 |
UnionExp Equal(EXP op, Loc loc, Type type, Expression e1, Expression e2) |
| 610 |
{ |
| 611 |
UnionExp ue = void; |
| 612 |
int cmp = 0; |
| 613 |
real_t r1 = CTFloat.zero; |
| 614 |
real_t r2 = CTFloat.zero; |
| 615 |
//printf("Equal(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); |
| 616 |
assert(op == EXP.equal || op == EXP.notEqual); |
| 617 |
if (e1.op == EXP.null_) |
| 618 |
{ |
| 619 |
if (e2.op == EXP.null_) |
| 620 |
cmp = 1; |
| 621 |
else if (StringExp es2 = e2.isStringExp()) |
| 622 |
{ |
| 623 |
cmp = (0 == es2.len); |
| 624 |
} |
| 625 |
else if (ArrayLiteralExp es2 = e2.isArrayLiteralExp()) |
| 626 |
{ |
| 627 |
cmp = !es2.elements || (0 == es2.elements.length); |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
cantExp(ue); |
| 632 |
return ue; |
| 633 |
} |
| 634 |
} |
| 635 |
else if (e2.op == EXP.null_) |
| 636 |
{ |
| 637 |
if (StringExp es1 = e1.isStringExp()) |
| 638 |
{ |
| 639 |
cmp = (0 == es1.len); |
| 640 |
} |
| 641 |
else if (ArrayLiteralExp es1 = e1.isArrayLiteralExp()) |
| 642 |
{ |
| 643 |
cmp = !es1.elements || (0 == es1.elements.length); |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
cantExp(ue); |
| 648 |
return ue; |
| 649 |
} |
| 650 |
} |
| 651 |
else if (e1.op == EXP.string_ && e2.op == EXP.string_) |
| 652 |
{ |
| 653 |
StringExp es1 = e1.isStringExp(); |
| 654 |
StringExp es2 = e2.isStringExp(); |
| 655 |
if (es1.sz != es2.sz) |
| 656 |
{ |
| 657 |
assert(global.errors); |
| 658 |
cantExp(ue); |
| 659 |
return ue; |
| 660 |
} |
| 661 |
const data1 = es1.peekData(); |
| 662 |
const data2 = es2.peekData(); |
| 663 |
if (es1.len == es2.len && memcmp(data1.ptr, data2.ptr, es1.sz * es1.len) == 0) |
| 664 |
cmp = 1; |
| 665 |
else |
| 666 |
cmp = 0; |
| 667 |
} |
| 668 |
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.arrayLiteral) |
| 669 |
{ |
| 670 |
ArrayLiteralExp es1 = e1.isArrayLiteralExp(); |
| 671 |
ArrayLiteralExp es2 = e2.isArrayLiteralExp(); |
| 672 |
if ((!es1.elements || !es1.elements.length) && (!es2.elements || !es2.elements.length)) |
| 673 |
cmp = 1; // both arrays are empty |
| 674 |
else if (!es1.elements || !es2.elements) |
| 675 |
cmp = 0; |
| 676 |
else if (es1.elements.length != es2.elements.length) |
| 677 |
cmp = 0; |
| 678 |
else |
| 679 |
{ |
| 680 |
for (size_t i = 0; i < es1.elements.length; i++) |
| 681 |
{ |
| 682 |
auto ee1 = es1[i]; |
| 683 |
auto ee2 = es2[i]; |
| 684 |
ue = Equal(EXP.equal, loc, Type.tint32, ee1, ee2); |
| 685 |
if (CTFEExp.isCantExp(ue.exp())) |
| 686 |
return ue; |
| 687 |
cmp = cast(int)ue.exp().toInteger(); |
| 688 |
if (cmp == 0) |
| 689 |
break; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.string_) |
| 694 |
{ |
| 695 |
// Swap operands and use common code |
| 696 |
Expression etmp = e1; |
| 697 |
e1 = e2; |
| 698 |
e2 = etmp; |
| 699 |
goto Lsa; |
| 700 |
} |
| 701 |
else if (e1.op == EXP.string_ && e2.op == EXP.arrayLiteral) |
| 702 |
{ |
| 703 |
Lsa: |
| 704 |
StringExp es1 = e1.isStringExp(); |
| 705 |
ArrayLiteralExp es2 = e2.isArrayLiteralExp(); |
| 706 |
size_t dim1 = es1.len; |
| 707 |
size_t dim2 = es2.elements ? es2.elements.length : 0; |
| 708 |
if (dim1 != dim2) |
| 709 |
cmp = 0; |
| 710 |
else |
| 711 |
{ |
| 712 |
cmp = 1; // if dim1 winds up being 0 |
| 713 |
foreach (i; 0 .. dim1) |
| 714 |
{ |
| 715 |
uinteger_t c = es1.getIndex(i); |
| 716 |
auto ee2 = es2[i]; |
| 717 |
if (ee2.isConst() != 1) |
| 718 |
{ |
| 719 |
cantExp(ue); |
| 720 |
return ue; |
| 721 |
} |
| 722 |
cmp = (c == ee2.toInteger()); |
| 723 |
if (cmp == 0) |
| 724 |
break; |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
else if (e1.op == EXP.structLiteral && e2.op == EXP.structLiteral) |
| 729 |
{ |
| 730 |
StructLiteralExp es1 = e1.isStructLiteralExp(); |
| 731 |
StructLiteralExp es2 = e2.isStructLiteralExp(); |
| 732 |
if (es1.sd != es2.sd) |
| 733 |
cmp = 0; |
| 734 |
else if ((!es1.elements || !es1.elements.length) && (!es2.elements || !es2.elements.length)) |
| 735 |
cmp = 1; // both arrays are empty |
| 736 |
else if (!es1.elements || !es2.elements) |
| 737 |
cmp = 0; |
| 738 |
else if (es1.elements.length != es2.elements.length) |
| 739 |
cmp = 0; |
| 740 |
else |
| 741 |
{ |
| 742 |
cmp = 1; |
| 743 |
for (size_t i = 0; i < es1.elements.length; i++) |
| 744 |
{ |
| 745 |
Expression ee1 = (*es1.elements)[i]; |
| 746 |
Expression ee2 = (*es2.elements)[i]; |
| 747 |
if (ee1 == ee2) |
| 748 |
continue; |
| 749 |
if (!ee1 || !ee2) |
| 750 |
{ |
| 751 |
cmp = 0; |
| 752 |
break; |
| 753 |
} |
| 754 |
ue = Equal(EXP.equal, loc, Type.tint32, ee1, ee2); |
| 755 |
if (ue.exp().op == EXP.cantExpression) |
| 756 |
return ue; |
| 757 |
cmp = cast(int)ue.exp().toInteger(); |
| 758 |
if (cmp == 0) |
| 759 |
break; |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
else if (e1.isConst() != 1 || e2.isConst() != 1) |
| 764 |
{ |
| 765 |
cantExp(ue); |
| 766 |
return ue; |
| 767 |
} |
| 768 |
else if (e1.type.isReal()) |
| 769 |
{ |
| 770 |
r1 = e1.toReal(); |
| 771 |
r2 = e2.toReal(); |
| 772 |
goto L1; |
| 773 |
} |
| 774 |
else if (e1.type.isImaginary()) |
| 775 |
{ |
| 776 |
r1 = e1.toImaginary(); |
| 777 |
r2 = e2.toImaginary(); |
| 778 |
L1: |
| 779 |
if (CTFloat.isNaN(r1) || CTFloat.isNaN(r2)) // if unordered |
| 780 |
{ |
| 781 |
cmp = 0; |
| 782 |
} |
| 783 |
else |
| 784 |
{ |
| 785 |
cmp = (r1 == r2); |
| 786 |
} |
| 787 |
} |
| 788 |
else if (e1.type.isComplex()) |
| 789 |
{ |
| 790 |
cmp = e1.toComplex() == e2.toComplex(); |
| 791 |
} |
| 792 |
else if (e1.type.isIntegral() || e1.type.toBasetype().ty == Tpointer) |
| 793 |
{ |
| 794 |
cmp = (e1.toInteger() == e2.toInteger()); |
| 795 |
} |
| 796 |
else |
| 797 |
{ |
| 798 |
cantExp(ue); |
| 799 |
return ue; |
| 800 |
} |
| 801 |
if (op == EXP.notEqual) |
| 802 |
cmp ^= 1; |
| 803 |
emplaceExp!(IntegerExp)(&ue, loc, cmp, type); |
| 804 |
return ue; |
| 805 |
} |
| 806 |
|
| 807 |
UnionExp Identity(EXP op, Loc loc, Type type, Expression e1, Expression e2) |
| 808 |
{ |
| 809 |
//printf("Identity %s %s\n", e1.toChars(), e2.toChars()); |
| 810 |
UnionExp ue = void; |
| 811 |
int cmp; |
| 812 |
if (e1.op == EXP.null_) |
| 813 |
{ |
| 814 |
cmp = (e2.op == EXP.null_); |
| 815 |
} |
| 816 |
else if (e2.op == EXP.null_) |
| 817 |
{ |
| 818 |
cmp = 0; |
| 819 |
} |
| 820 |
else if (e1.op == EXP.symbolOffset && e2.op == EXP.symbolOffset) |
| 821 |
{ |
| 822 |
SymOffExp es1 = e1.isSymOffExp(); |
| 823 |
SymOffExp es2 = e2.isSymOffExp(); |
| 824 |
cmp = es1.offset == es2.offset; |
| 825 |
if (cmp) |
| 826 |
{ |
| 827 |
cmp = es1.var == es2.var; |
| 828 |
if (!cmp && (es1.var.isParameter() || es2.var.isParameter())) |
| 829 |
{ |
| 830 |
// because of ref's, they may still be the same, we cannot tell |
| 831 |
cantExp(ue); |
| 832 |
return ue; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
else |
| 837 |
{ |
| 838 |
if (e1.type.isFloating()) |
| 839 |
cmp = e1.isIdentical(e2); |
| 840 |
else |
| 841 |
{ |
| 842 |
ue = Equal((op == EXP.identity) ? EXP.equal : EXP.notEqual, loc, type, e1, e2); |
| 843 |
return ue; |
| 844 |
} |
| 845 |
} |
| 846 |
if (op == EXP.notIdentity) |
| 847 |
cmp ^= 1; |
| 848 |
emplaceExp!(IntegerExp)(&ue, loc, cmp, type); |
| 849 |
return ue; |
| 850 |
} |
| 851 |
|
| 852 |
UnionExp Cmp(EXP op, Loc loc, Type type, Expression e1, Expression e2) |
| 853 |
{ |
| 854 |
UnionExp ue = void; |
| 855 |
dinteger_t n; |
| 856 |
real_t r1 = CTFloat.zero; |
| 857 |
real_t r2 = CTFloat.zero; |
| 858 |
//printf("Cmp(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); |
| 859 |
if (e1.op == EXP.string_ && e2.op == EXP.string_) |
| 860 |
{ |
| 861 |
StringExp es1 = e1.isStringExp(); |
| 862 |
StringExp es2 = e2.isStringExp(); |
| 863 |
size_t sz = es1.sz; |
| 864 |
assert(sz == es2.sz); |
| 865 |
size_t len = es1.len; |
| 866 |
if (es2.len < len) |
| 867 |
len = es2.len; |
| 868 |
const data1 = es1.peekData(); |
| 869 |
const data2 = es1.peekData(); |
| 870 |
int rawCmp = memcmp(data1.ptr, data2.ptr, sz * len); |
| 871 |
if (rawCmp == 0) |
| 872 |
rawCmp = cast(int)(es1.len - es2.len); |
| 873 |
n = specificCmp(op, rawCmp); |
| 874 |
} |
| 875 |
else if (e1.isConst() != 1 || e2.isConst() != 1) |
| 876 |
{ |
| 877 |
cantExp(ue); |
| 878 |
return ue; |
| 879 |
} |
| 880 |
else if (e1.type.isReal()) |
| 881 |
{ |
| 882 |
r1 = e1.toReal(); |
| 883 |
r2 = e2.toReal(); |
| 884 |
goto L1; |
| 885 |
} |
| 886 |
else if (e1.type.isImaginary()) |
| 887 |
{ |
| 888 |
r1 = e1.toImaginary(); |
| 889 |
r2 = e2.toImaginary(); |
| 890 |
L1: |
| 891 |
n = realCmp(op, r1, r2); |
| 892 |
} |
| 893 |
else if (e1.type.isComplex()) |
| 894 |
{ |
| 895 |
assert(0); |
| 896 |
} |
| 897 |
else |
| 898 |
{ |
| 899 |
sinteger_t n1; |
| 900 |
sinteger_t n2; |
| 901 |
n1 = e1.toInteger(); |
| 902 |
n2 = e2.toInteger(); |
| 903 |
if (e1.type.isUnsigned() || e2.type.isUnsigned()) |
| 904 |
n = intUnsignedCmp(op, n1, n2); |
| 905 |
else |
| 906 |
n = intSignedCmp(op, n1, n2); |
| 907 |
} |
| 908 |
emplaceExp!(IntegerExp)(&ue, loc, n, type); |
| 909 |
return ue; |
| 910 |
} |
| 911 |
|
| 912 |
/* Also returns EXP.cantExpression if cannot be computed. |
| 913 |
* to: type to cast to |
| 914 |
* type: type to paint the result |
| 915 |
*/ |
| 916 |
UnionExp Cast(Loc loc, Type type, Type to, Expression e1) |
| 917 |
{ |
| 918 |
UnionExp ue = void; |
| 919 |
Type tb = to.toBasetype(); |
| 920 |
Type typeb = type.toBasetype(); |
| 921 |
//printf("Cast(type = %s, to = %s, e1 = %s)\n", type.toChars(), to.toChars(), e1.toChars()); |
| 922 |
//printf("\te1.type = %s\n", e1.type.toChars()); |
| 923 |
if (e1.type.equals(type) && type.equals(to)) |
| 924 |
{ |
| 925 |
emplaceExp!(UnionExp)(&ue, e1); |
| 926 |
return ue; |
| 927 |
} |
| 928 |
if (e1.op == EXP.vector && (cast(TypeVector)e1.type).basetype.equals(type) && type.equals(to)) |
| 929 |
{ |
| 930 |
Expression ex = e1.isVectorExp().e1; |
| 931 |
emplaceExp!(UnionExp)(&ue, ex); |
| 932 |
return ue; |
| 933 |
} |
| 934 |
if (e1.type.toBasetype.equals(type) && type.equals(to)) |
| 935 |
{ |
| 936 |
emplaceExp!(UnionExp)(&ue, e1); |
| 937 |
ue.exp().type = type; |
| 938 |
return ue; |
| 939 |
} |
| 940 |
if (e1.type.implicitConvTo(to) >= MATCH.constant || to.implicitConvTo(e1.type) >= MATCH.constant) |
| 941 |
{ |
| 942 |
goto L1; |
| 943 |
} |
| 944 |
// Allow covariant converions of delegates |
| 945 |
// (Perhaps implicit conversion from pure to impure should be a MATCH.constant, |
| 946 |
// then we wouldn't need this extra check.) |
| 947 |
if (e1.type.toBasetype().ty == Tdelegate && e1.type.implicitConvTo(to) == MATCH.convert) |
| 948 |
{ |
| 949 |
goto L1; |
| 950 |
} |
| 951 |
/* Allow casting from one string type to another |
| 952 |
*/ |
| 953 |
if (e1.op == EXP.string_) |
| 954 |
{ |
| 955 |
if (tb.ty == Tarray && typeb.ty == Tarray && tb.nextOf().size() == typeb.nextOf().size()) |
| 956 |
{ |
| 957 |
goto L1; |
| 958 |
} |
| 959 |
} |
| 960 |
if (e1.op == EXP.arrayLiteral && typeb == tb) |
| 961 |
{ |
| 962 |
L1: |
| 963 |
Expression ex = expType(to, e1); |
| 964 |
emplaceExp!(UnionExp)(&ue, ex); |
| 965 |
return ue; |
| 966 |
} |
| 967 |
if (e1.isConst() != 1) |
| 968 |
{ |
| 969 |
cantExp(ue); |
| 970 |
} |
| 971 |
else if (tb.ty == Tbool) |
| 972 |
{ |
| 973 |
const opt = e1.toBool(); |
| 974 |
if (opt.isEmpty()) |
| 975 |
{ |
| 976 |
cantExp(ue); |
| 977 |
return ue; |
| 978 |
} |
| 979 |
|
| 980 |
emplaceExp!(IntegerExp)(&ue, loc, opt.get(), type); |
| 981 |
} |
| 982 |
else if (type.isIntegral()) |
| 983 |
{ |
| 984 |
if (e1.type.isFloating()) |
| 985 |
{ |
| 986 |
dinteger_t result; |
| 987 |
real_t r = e1.toReal(); |
| 988 |
switch (typeb.ty) |
| 989 |
{ |
| 990 |
case Tint8: |
| 991 |
result = cast(byte)cast(sinteger_t)r; |
| 992 |
break; |
| 993 |
case Tchar: |
| 994 |
case Tuns8: |
| 995 |
result = cast(ubyte)cast(dinteger_t)r; |
| 996 |
break; |
| 997 |
case Tint16: |
| 998 |
result = cast(short)cast(sinteger_t)r; |
| 999 |
break; |
| 1000 |
case Twchar: |
| 1001 |
case Tuns16: |
| 1002 |
result = cast(ushort)cast(dinteger_t)r; |
| 1003 |
break; |
| 1004 |
case Tint32: |
| 1005 |
result = cast(int)r; |
| 1006 |
break; |
| 1007 |
case Tdchar: |
| 1008 |
case Tuns32: |
| 1009 |
result = cast(uint)r; |
| 1010 |
break; |
| 1011 |
case Tint64: |
| 1012 |
result = cast(long)r; |
| 1013 |
break; |
| 1014 |
case Tuns64: |
| 1015 |
result = cast(ulong)r; |
| 1016 |
break; |
| 1017 |
default: |
| 1018 |
assert(0); |
| 1019 |
} |
| 1020 |
emplaceExp!(IntegerExp)(&ue, loc, result, type); |
| 1021 |
} |
| 1022 |
else if (type.isUnsigned()) |
| 1023 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toUInteger(), type); |
| 1024 |
else |
| 1025 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger(), type); |
| 1026 |
} |
| 1027 |
else if (tb.isReal()) |
| 1028 |
{ |
| 1029 |
real_t value = e1.toReal(); |
| 1030 |
emplaceExp!(RealExp)(&ue, loc, value, type); |
| 1031 |
} |
| 1032 |
else if (tb.isImaginary()) |
| 1033 |
{ |
| 1034 |
real_t value = e1.toImaginary(); |
| 1035 |
emplaceExp!(RealExp)(&ue, loc, value, type); |
| 1036 |
} |
| 1037 |
else if (tb.isComplex()) |
| 1038 |
{ |
| 1039 |
complex_t value = e1.toComplex(); |
| 1040 |
emplaceExp!(ComplexExp)(&ue, loc, value, type); |
| 1041 |
} |
| 1042 |
else if (tb.isScalar()) |
| 1043 |
{ |
| 1044 |
emplaceExp!(IntegerExp)(&ue, loc, e1.toInteger(), type); |
| 1045 |
} |
| 1046 |
else if (tb.ty == Tvoid) |
| 1047 |
{ |
| 1048 |
cantExp(ue); |
| 1049 |
} |
| 1050 |
else if (tb.ty == Tstruct && e1.op == EXP.int64) |
| 1051 |
{ |
| 1052 |
// Struct = 0; |
| 1053 |
StructDeclaration sd = tb.toDsymbol(null).isStructDeclaration(); |
| 1054 |
assert(sd); |
| 1055 |
auto elements = new Expressions(); |
| 1056 |
for (size_t i = 0; i < sd.fields.length; i++) |
| 1057 |
{ |
| 1058 |
VarDeclaration v = sd.fields[i]; |
| 1059 |
UnionExp zero; |
| 1060 |
emplaceExp!(IntegerExp)(&zero, 0); |
| 1061 |
ue = Cast(loc, v.type, v.type, zero.exp()); |
| 1062 |
if (ue.exp().op == EXP.cantExpression) |
| 1063 |
return ue; |
| 1064 |
elements.push(ue.exp().copy()); |
| 1065 |
} |
| 1066 |
emplaceExp!(StructLiteralExp)(&ue, loc, sd, elements); |
| 1067 |
ue.exp().type = type; |
| 1068 |
} |
| 1069 |
else |
| 1070 |
{ |
| 1071 |
if (type != Type.terror) |
| 1072 |
{ |
| 1073 |
// have to change to internal compiler error |
| 1074 |
// all invalid casts should be handled already in Expression::castTo(). |
| 1075 |
error(loc, "cannot cast `%s` to `%s`", e1.type.toChars(), type.toChars()); |
| 1076 |
} |
| 1077 |
emplaceExp!(ErrorExp)(&ue); |
| 1078 |
} |
| 1079 |
return ue; |
| 1080 |
} |
| 1081 |
|
| 1082 |
UnionExp ArrayLength(Type type, Expression e1) |
| 1083 |
{ |
| 1084 |
UnionExp ue = void; |
| 1085 |
Loc loc = e1.loc; |
| 1086 |
if (StringExp es1 = e1.isStringExp()) |
| 1087 |
{ |
| 1088 |
emplaceExp!(IntegerExp)(&ue, loc, es1.len, type); |
| 1089 |
} |
| 1090 |
else if (ArrayLiteralExp ale = e1.isArrayLiteralExp()) |
| 1091 |
{ |
| 1092 |
size_t dim = ale.elements ? ale.elements.length : 0; |
| 1093 |
emplaceExp!(IntegerExp)(&ue, loc, dim, type); |
| 1094 |
} |
| 1095 |
else if (AssocArrayLiteralExp ale = e1.isAssocArrayLiteralExp) |
| 1096 |
{ |
| 1097 |
size_t dim = ale.keys.length; |
| 1098 |
emplaceExp!(IntegerExp)(&ue, loc, dim, type); |
| 1099 |
} |
| 1100 |
else if (e1.type.toBasetype().ty == Tsarray) |
| 1101 |
{ |
| 1102 |
Expression e = (cast(TypeSArray)e1.type.toBasetype()).dim; |
| 1103 |
emplaceExp!(UnionExp)(&ue, e); |
| 1104 |
} |
| 1105 |
else if (e1.isNullExp()) |
| 1106 |
{ |
| 1107 |
emplaceExp!(IntegerExp)(&ue, loc, 0, type); |
| 1108 |
} |
| 1109 |
else |
| 1110 |
cantExp(ue); |
| 1111 |
return ue; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Also return EXP.cantExpression if this fails |
| 1115 |
*/ |
| 1116 |
UnionExp Index(Type type, Expression e1, Expression e2, bool indexIsInBounds) |
| 1117 |
{ |
| 1118 |
UnionExp ue = void; |
| 1119 |
Loc loc = e1.loc; |
| 1120 |
//printf("Index(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); |
| 1121 |
assert(e1.type); |
| 1122 |
if (e1.op == EXP.string_ && e2.op == EXP.int64) |
| 1123 |
{ |
| 1124 |
StringExp es1 = e1.isStringExp(); |
| 1125 |
uinteger_t i = e2.toInteger(); |
| 1126 |
if (i >= es1.len) |
| 1127 |
{ |
| 1128 |
error(e1.loc, "string index %llu is out of bounds `[0 .. %llu]`", i, cast(ulong)es1.len); |
| 1129 |
emplaceExp!(ErrorExp)(&ue); |
| 1130 |
} |
| 1131 |
else |
| 1132 |
{ |
| 1133 |
emplaceExp!(IntegerExp)(&ue, loc, es1.getIndex(cast(size_t) i), type); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
else if (e1.type.toBasetype().ty == Tsarray && e2.op == EXP.int64) |
| 1137 |
{ |
| 1138 |
TypeSArray tsa = cast(TypeSArray)e1.type.toBasetype(); |
| 1139 |
uinteger_t length = tsa.dim.toInteger(); |
| 1140 |
uinteger_t i = e2.toInteger(); |
| 1141 |
if (i >= length && (e1.op == EXP.arrayLiteral || !indexIsInBounds)) |
| 1142 |
{ |
| 1143 |
// C code only checks bounds if an ArrayLiteralExp |
| 1144 |
error(e1.loc, "array index %llu is out of bounds `%s[0 .. %llu]`", i, e1.toChars(), length); |
| 1145 |
emplaceExp!(ErrorExp)(&ue); |
| 1146 |
} |
| 1147 |
else if (ArrayLiteralExp ale = e1.isArrayLiteralExp()) |
| 1148 |
{ |
| 1149 |
auto e = ale[cast(size_t)i]; |
| 1150 |
e.type = type; |
| 1151 |
e.loc = loc; |
| 1152 |
if (hasSideEffect(e)) |
| 1153 |
cantExp(ue); |
| 1154 |
else |
| 1155 |
emplaceExp!(UnionExp)(&ue, e); |
| 1156 |
} |
| 1157 |
else |
| 1158 |
cantExp(ue); |
| 1159 |
} |
| 1160 |
else if (e1.type.toBasetype().ty == Tarray && e2.op == EXP.int64) |
| 1161 |
{ |
| 1162 |
uinteger_t i = e2.toInteger(); |
| 1163 |
if (ArrayLiteralExp ale = e1.isArrayLiteralExp()) |
| 1164 |
{ |
| 1165 |
if (i >= ale.elements.length) |
| 1166 |
{ |
| 1167 |
error(e1.loc, "array index %llu is out of bounds `%s[0 .. %llu]`", i, e1.toChars(), cast(ulong) ale.elements.length); |
| 1168 |
emplaceExp!(ErrorExp)(&ue); |
| 1169 |
} |
| 1170 |
else |
| 1171 |
{ |
| 1172 |
auto e = ale[cast(size_t)i]; |
| 1173 |
e.type = type; |
| 1174 |
e.loc = loc; |
| 1175 |
if (hasSideEffect(e)) |
| 1176 |
cantExp(ue); |
| 1177 |
else |
| 1178 |
emplaceExp!(UnionExp)(&ue, e); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
else |
| 1182 |
cantExp(ue); |
| 1183 |
} |
| 1184 |
else if (AssocArrayLiteralExp ae = e1.isAssocArrayLiteralExp()) |
| 1185 |
{ |
| 1186 |
/* Search the keys backwards, in case there are duplicate keys |
| 1187 |
*/ |
| 1188 |
for (size_t i = ae.keys.length; i;) |
| 1189 |
{ |
| 1190 |
i--; |
| 1191 |
Expression ekey = (*ae.keys)[i]; |
| 1192 |
ue = Equal(EXP.equal, loc, Type.tbool, ekey, e2); |
| 1193 |
if (CTFEExp.isCantExp(ue.exp())) |
| 1194 |
return ue; |
| 1195 |
if (ue.exp().toBool().hasValue(true)) |
| 1196 |
{ |
| 1197 |
Expression e = (*ae.values)[i]; |
| 1198 |
e.type = type; |
| 1199 |
e.loc = loc; |
| 1200 |
if (hasSideEffect(e)) |
| 1201 |
cantExp(ue); |
| 1202 |
else |
| 1203 |
emplaceExp!(UnionExp)(&ue, e); |
| 1204 |
return ue; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
cantExp(ue); |
| 1208 |
} |
| 1209 |
else |
| 1210 |
cantExp(ue); |
| 1211 |
return ue; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* Also return EXP.cantExpression if this fails |
| 1215 |
*/ |
| 1216 |
UnionExp Slice(Type type, Expression e1, Expression lwr, Expression upr) |
| 1217 |
{ |
| 1218 |
UnionExp ue = void; |
| 1219 |
Loc loc = e1.loc; |
| 1220 |
static if (LOG) |
| 1221 |
{ |
| 1222 |
printf("Slice()\n"); |
| 1223 |
if (lwr) |
| 1224 |
{ |
| 1225 |
printf("\te1 = %s\n", e1.toChars()); |
| 1226 |
printf("\tlwr = %s\n", lwr.toChars()); |
| 1227 |
printf("\tupr = %s\n", upr.toChars()); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
if (!lwr) |
| 1232 |
{ |
| 1233 |
if (e1.op == EXP.string_) |
| 1234 |
emplaceExp(&ue, e1); |
| 1235 |
else |
| 1236 |
cantExp(ue); |
| 1237 |
} |
| 1238 |
else if (e1.op == EXP.string_ && lwr.op == EXP.int64 && upr.op == EXP.int64) |
| 1239 |
{ |
| 1240 |
StringExp es1 = e1.isStringExp(); |
| 1241 |
const uinteger_t ilwr = lwr.toInteger(); |
| 1242 |
const uinteger_t iupr = upr.toInteger(); |
| 1243 |
if (sliceBoundsCheck(0, es1.len, ilwr, iupr)) |
| 1244 |
cantExp(ue); // https://issues.dlang.org/show_bug.cgi?id=18115 |
| 1245 |
else |
| 1246 |
{ |
| 1247 |
const len = cast(size_t)(iupr - ilwr); |
| 1248 |
const sz = es1.sz; |
| 1249 |
void* s = mem.xmalloc(len * sz); |
| 1250 |
const data1 = es1.peekData(); |
| 1251 |
memcpy(s, data1.ptr + ilwr * sz, len * sz); |
| 1252 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz, es1.postfix); |
| 1253 |
StringExp es = ue.exp().isStringExp(); |
| 1254 |
es.committed = es1.committed; |
| 1255 |
es.type = type; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
else if (e1.op == EXP.arrayLiteral && lwr.op == EXP.int64 && upr.op == EXP.int64 && !hasSideEffect(e1)) |
| 1259 |
{ |
| 1260 |
ArrayLiteralExp es1 = e1.isArrayLiteralExp(); |
| 1261 |
const uinteger_t ilwr = lwr.toInteger(); |
| 1262 |
const uinteger_t iupr = upr.toInteger(); |
| 1263 |
if (sliceBoundsCheck(0, es1.elements.length, ilwr, iupr)) |
| 1264 |
cantExp(ue); |
| 1265 |
else |
| 1266 |
{ |
| 1267 |
auto elements = new Expressions(cast(size_t)(iupr - ilwr)); |
| 1268 |
memcpy(elements.tdata(), es1.elements.tdata() + ilwr, cast(size_t)(iupr - ilwr) * ((*es1.elements)[0]).sizeof); |
| 1269 |
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, type, elements); |
| 1270 |
} |
| 1271 |
} |
| 1272 |
else |
| 1273 |
cantExp(ue); |
| 1274 |
return ue; |
| 1275 |
} |
| 1276 |
|
| 1277 |
/* Check whether slice `[newlwr .. newupr]` is in the range `[lwr .. upr]` |
| 1278 |
*/ |
| 1279 |
bool sliceBoundsCheck(uinteger_t lwr, uinteger_t upr, uinteger_t newlwr, uinteger_t newupr) pure @safe |
| 1280 |
{ |
| 1281 |
assert(lwr <= upr); |
| 1282 |
return !(newlwr <= newupr && |
| 1283 |
lwr <= newlwr && |
| 1284 |
newupr <= upr); |
| 1285 |
} |
| 1286 |
|
| 1287 |
/* Set a slice of char/integer array literal 'existingAE' from a string 'newval'. |
| 1288 |
* existingAE[firstIndex..firstIndex+newval.length] = newval. |
| 1289 |
*/ |
| 1290 |
void sliceAssignArrayLiteralFromString(ArrayLiteralExp existingAE, const StringExp newval, size_t firstIndex) |
| 1291 |
{ |
| 1292 |
const len = newval.len; |
| 1293 |
Type elemType = existingAE.type.nextOf(); |
| 1294 |
foreach (j; 0 .. len) |
| 1295 |
{ |
| 1296 |
const val = newval.getIndex(j); |
| 1297 |
(*existingAE.elements)[j + firstIndex] = new IntegerExp(newval.loc, val, elemType); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* Set a slice of string 'existingSE' from a char array literal 'newae'. |
| 1302 |
* existingSE[firstIndex..firstIndex+newae.length] = newae. |
| 1303 |
*/ |
| 1304 |
void sliceAssignStringFromArrayLiteral(StringExp existingSE, ArrayLiteralExp newae, size_t firstIndex) |
| 1305 |
{ |
| 1306 |
assert(existingSE.ownedByCtfe != OwnedBy.code); |
| 1307 |
foreach (j; 0 .. newae.elements.length) |
| 1308 |
{ |
| 1309 |
existingSE.setCodeUnit(firstIndex + j, cast(dchar)newae[j].toInteger()); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* Set a slice of string 'existingSE' from a string 'newstr'. |
| 1314 |
* existingSE[firstIndex..firstIndex+newstr.length] = newstr. |
| 1315 |
*/ |
| 1316 |
void sliceAssignStringFromString(StringExp existingSE, const StringExp newstr, size_t firstIndex) |
| 1317 |
{ |
| 1318 |
assert(existingSE.ownedByCtfe != OwnedBy.code); |
| 1319 |
size_t sz = existingSE.sz; |
| 1320 |
assert(sz == newstr.sz); |
| 1321 |
auto data1 = existingSE.borrowData(); |
| 1322 |
const data2 = newstr.peekData(); |
| 1323 |
memcpy(data1.ptr + firstIndex * sz, data2.ptr, data2.length); |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* Compare a string slice with another string slice. |
| 1327 |
* Conceptually equivalent to memcmp( se1[lo1..lo1+len], se2[lo2..lo2+len]) |
| 1328 |
*/ |
| 1329 |
int sliceCmpStringWithString(const StringExp se1, const StringExp se2, size_t lo1, size_t lo2, size_t len) |
| 1330 |
{ |
| 1331 |
size_t sz = se1.sz; |
| 1332 |
assert(sz == se2.sz); |
| 1333 |
const data1 = se1.peekData(); |
| 1334 |
const data2 = se2.peekData(); |
| 1335 |
return memcmp(data1.ptr + sz * lo1, data2.ptr + sz * lo2, sz * len); |
| 1336 |
} |
| 1337 |
|
| 1338 |
/* Compare a string slice with an array literal slice |
| 1339 |
* Conceptually equivalent to memcmp( se1[lo1..lo1+len], ae2[lo2..lo2+len]) |
| 1340 |
*/ |
| 1341 |
int sliceCmpStringWithArray(const StringExp se1, ArrayLiteralExp ae2, size_t lo1, size_t lo2, size_t len) |
| 1342 |
{ |
| 1343 |
foreach (j; 0 .. len) |
| 1344 |
{ |
| 1345 |
const val2 = ae2[j + lo2].toInteger(); |
| 1346 |
const val1 = se1.getIndex(j + lo1); |
| 1347 |
const int c = (val1 > val2) - (val1 < val2); |
| 1348 |
if (c) |
| 1349 |
return c; |
| 1350 |
} |
| 1351 |
return 0; |
| 1352 |
} |
| 1353 |
|
| 1354 |
/** Copy element `Expressions` in the parameters when they're `ArrayLiteralExp`s. |
| 1355 |
* Params: |
| 1356 |
* e1 = If it's ArrayLiteralExp, its `elements` will be copied. |
| 1357 |
* Otherwise, `e1` itself will be pushed into the new `Expressions`. |
| 1358 |
* e2 = If it's not `null`, it will be pushed/appended to the new |
| 1359 |
* `Expressions` by the same way with `e1`. |
| 1360 |
* Returns: |
| 1361 |
* Newly allocated `Expressions`. Note that it points to the original |
| 1362 |
* `Expression` values in e1 and e2. |
| 1363 |
*/ |
| 1364 |
private Expressions* copyElements(Expression e1, Expression e2 = null) |
| 1365 |
{ |
| 1366 |
auto elems = new Expressions(); |
| 1367 |
|
| 1368 |
void append(ArrayLiteralExp ale) |
| 1369 |
{ |
| 1370 |
if (!ale.elements) |
| 1371 |
return; |
| 1372 |
auto d = elems.length; |
| 1373 |
elems.append(ale.elements); |
| 1374 |
foreach (ref el; (*elems)[d .. elems.length]) |
| 1375 |
{ |
| 1376 |
if (!el) |
| 1377 |
el = ale.basis; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
if (auto ale = e1.isArrayLiteralExp()) |
| 1382 |
append(ale); |
| 1383 |
else |
| 1384 |
elems.push(e1); |
| 1385 |
|
| 1386 |
if (e2) |
| 1387 |
{ |
| 1388 |
if (auto ale = e2.isArrayLiteralExp()) |
| 1389 |
append(ale); |
| 1390 |
else |
| 1391 |
elems.push(e2); |
| 1392 |
} |
| 1393 |
|
| 1394 |
return elems; |
| 1395 |
} |
| 1396 |
|
| 1397 |
/* Also return EXP.cantExpression if this fails |
| 1398 |
*/ |
| 1399 |
UnionExp Cat(Loc loc, Type type, Expression e1, Expression e2) |
| 1400 |
{ |
| 1401 |
UnionExp ue = void; |
| 1402 |
Expression e = CTFEExp.cantexp; |
| 1403 |
Type t; |
| 1404 |
Type t1 = e1.type.toBasetype(); |
| 1405 |
Type t2 = e2.type.toBasetype(); |
| 1406 |
//printf("Cat(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars()); |
| 1407 |
//printf("\tt1 = %s, t2 = %s, type = %s\n", t1.toChars(), t2.toChars(), type.toChars()); |
| 1408 |
|
| 1409 |
/* e is the non-null operand, t is the type of the null operand |
| 1410 |
*/ |
| 1411 |
UnionExp catNull(Expression e, Type t) |
| 1412 |
{ |
| 1413 |
Type tn = e.type.toBasetype(); |
| 1414 |
if (tn.ty.isSomeChar) |
| 1415 |
{ |
| 1416 |
// Create a StringExp |
| 1417 |
if (t.nextOf()) |
| 1418 |
t = t.nextOf().toBasetype(); |
| 1419 |
const sz = cast(ubyte)t.size(); |
| 1420 |
dinteger_t v = e.toInteger(); |
| 1421 |
const len = (t.ty == tn.ty) ? 1 : utf_codeLength(sz, cast(dchar)v); |
| 1422 |
void* s = mem.xmalloc(len * sz); |
| 1423 |
if (t.ty == tn.ty) |
| 1424 |
Port.valcpy(s, v, sz); |
| 1425 |
else |
| 1426 |
utf_encode(sz, s, cast(dchar)v); |
| 1427 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1428 |
StringExp es = ue.exp().isStringExp(); |
| 1429 |
es.type = type; |
| 1430 |
es.committed = true; |
| 1431 |
} |
| 1432 |
else |
| 1433 |
{ |
| 1434 |
// Create an ArrayLiteralExp |
| 1435 |
auto elements = new Expressions(); |
| 1436 |
elements.push(e); |
| 1437 |
emplaceExp!(ArrayLiteralExp)(&ue, e.loc, type, elements); |
| 1438 |
} |
| 1439 |
assert(ue.exp().type); |
| 1440 |
return ue; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (e1.op == EXP.null_ && (e2.op == EXP.int64 || e2.op == EXP.structLiteral)) |
| 1444 |
{ |
| 1445 |
return catNull(e2, t1); |
| 1446 |
} |
| 1447 |
else if ((e1.op == EXP.int64 || e1.op == EXP.structLiteral) && e2.op == EXP.null_) |
| 1448 |
{ |
| 1449 |
return catNull(e1, t2); |
| 1450 |
} |
| 1451 |
else if (e1.op == EXP.null_ && e2.op == EXP.null_) |
| 1452 |
{ |
| 1453 |
if (type == e1.type) |
| 1454 |
{ |
| 1455 |
// Handle null ~= null |
| 1456 |
if (t1.ty == Tarray && t2 == t1.nextOf()) |
| 1457 |
{ |
| 1458 |
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, type, e2); |
| 1459 |
assert(ue.exp().type); |
| 1460 |
return ue; |
| 1461 |
} |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
emplaceExp!(UnionExp)(&ue, e1); |
| 1465 |
assert(ue.exp().type); |
| 1466 |
return ue; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
if (type == e2.type) |
| 1470 |
{ |
| 1471 |
emplaceExp!(UnionExp)(&ue, e2); |
| 1472 |
assert(ue.exp().type); |
| 1473 |
return ue; |
| 1474 |
} |
| 1475 |
emplaceExp!(NullExp)(&ue, e1.loc, type); |
| 1476 |
assert(ue.exp().type); |
| 1477 |
return ue; |
| 1478 |
} |
| 1479 |
else if (e1.op == EXP.string_ && e2.op == EXP.string_) |
| 1480 |
{ |
| 1481 |
// Concatenate the strings |
| 1482 |
StringExp es1 = e1.isStringExp(); |
| 1483 |
StringExp es2 = e2.isStringExp(); |
| 1484 |
size_t len = es1.len + es2.len; |
| 1485 |
ubyte sz = es1.sz; |
| 1486 |
if (sz != es2.sz) |
| 1487 |
{ |
| 1488 |
/* Can happen with: |
| 1489 |
* auto s = "foo"d ~ "bar"c; |
| 1490 |
*/ |
| 1491 |
assert(global.errors); |
| 1492 |
cantExp(ue); |
| 1493 |
assert(ue.exp().type); |
| 1494 |
return ue; |
| 1495 |
} |
| 1496 |
void* s = mem.xmalloc(len * sz); |
| 1497 |
const data1 = es1.peekData(); |
| 1498 |
const data2 = es2.peekData(); |
| 1499 |
memcpy(cast(char*)s, data1.ptr, es1.len * sz); |
| 1500 |
memcpy(cast(char*)s + es1.len * sz, data2.ptr, es2.len * sz); |
| 1501 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1502 |
StringExp es = ue.exp().isStringExp(); |
| 1503 |
es.committed = es1.committed | es2.committed; |
| 1504 |
es.type = type; |
| 1505 |
assert(ue.exp().type); |
| 1506 |
return ue; |
| 1507 |
} |
| 1508 |
else if (e2.op == EXP.string_ && e1.op == EXP.arrayLiteral && t1.nextOf().isIntegral()) |
| 1509 |
{ |
| 1510 |
// [chars] ~ string --> [chars] |
| 1511 |
StringExp es = e2.isStringExp(); |
| 1512 |
ArrayLiteralExp ea = e1.isArrayLiteralExp(); |
| 1513 |
size_t len = es.len + ea.elements.length; |
| 1514 |
auto elems = new Expressions(len); |
| 1515 |
for (size_t i = 0; i < ea.elements.length; ++i) |
| 1516 |
{ |
| 1517 |
(*elems)[i] = ea[i]; |
| 1518 |
} |
| 1519 |
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, type, elems); |
| 1520 |
ArrayLiteralExp dest = ue.exp().isArrayLiteralExp(); |
| 1521 |
sliceAssignArrayLiteralFromString(dest, es, ea.elements.length); |
| 1522 |
assert(ue.exp().type); |
| 1523 |
return ue; |
| 1524 |
} |
| 1525 |
else if (e1.op == EXP.string_ && e2.op == EXP.arrayLiteral && t2.nextOf().isIntegral()) |
| 1526 |
{ |
| 1527 |
// string ~ [chars] --> [chars] |
| 1528 |
StringExp es = e1.isStringExp(); |
| 1529 |
ArrayLiteralExp ea = e2.isArrayLiteralExp(); |
| 1530 |
size_t len = es.len + ea.elements.length; |
| 1531 |
auto elems = new Expressions(len); |
| 1532 |
for (size_t i = 0; i < ea.elements.length; ++i) |
| 1533 |
{ |
| 1534 |
(*elems)[es.len + i] = ea[i]; |
| 1535 |
} |
| 1536 |
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, type, elems); |
| 1537 |
ArrayLiteralExp dest = ue.exp().isArrayLiteralExp(); |
| 1538 |
sliceAssignArrayLiteralFromString(dest, es, 0); |
| 1539 |
assert(ue.exp().type); |
| 1540 |
return ue; |
| 1541 |
} |
| 1542 |
else if (e1.op == EXP.string_ && e2.op == EXP.int64) |
| 1543 |
{ |
| 1544 |
// string ~ char --> string |
| 1545 |
StringExp es1 = e1.isStringExp(); |
| 1546 |
StringExp es; |
| 1547 |
const sz = es1.sz; |
| 1548 |
dinteger_t v = e2.toInteger(); |
| 1549 |
// Is it a concatenation of homogenous types? |
| 1550 |
// (char[] ~ char, wchar[]~wchar, or dchar[]~dchar) |
| 1551 |
bool homoConcat = (sz == t2.size()); |
| 1552 |
const len = es1.len + (homoConcat ? 1 : utf_codeLength(sz, cast(dchar)v)); |
| 1553 |
void* s = mem.xmalloc(len * sz); |
| 1554 |
const data1 = es1.peekData(); |
| 1555 |
memcpy(s, data1.ptr, data1.length); |
| 1556 |
if (homoConcat) |
| 1557 |
Port.valcpy(cast(char*)s + (sz * es1.len), v, sz); |
| 1558 |
else |
| 1559 |
utf_encode(sz, cast(char*)s + (sz * es1.len), cast(dchar)v); |
| 1560 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1561 |
es = ue.exp().isStringExp(); |
| 1562 |
es.committed = es1.committed; |
| 1563 |
es.type = type; |
| 1564 |
assert(ue.exp().type); |
| 1565 |
return ue; |
| 1566 |
} |
| 1567 |
else if (e1.op == EXP.int64 && e2.op == EXP.string_) |
| 1568 |
{ |
| 1569 |
// [w|d]?char ~ string --> string |
| 1570 |
// We assume that we only ever prepend one char of the same type |
| 1571 |
// (wchar,dchar) as the string's characters. |
| 1572 |
StringExp es2 = e2.isStringExp(); |
| 1573 |
const len = 1 + es2.len; |
| 1574 |
const sz = es2.sz; |
| 1575 |
dinteger_t v = e1.toInteger(); |
| 1576 |
void* s = mem.xmalloc(len * sz); |
| 1577 |
Port.valcpy(cast(char*)s, v, sz); |
| 1578 |
const data2 = es2.peekData(); |
| 1579 |
memcpy(cast(char*)s + sz, data2.ptr, data2.length); |
| 1580 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1581 |
StringExp es = ue.exp().isStringExp(); |
| 1582 |
es.sz = sz; |
| 1583 |
es.committed = es2.committed; |
| 1584 |
es.type = type; |
| 1585 |
assert(ue.exp().type); |
| 1586 |
return ue; |
| 1587 |
} |
| 1588 |
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.arrayLiteral && t1.nextOf().equals(t2.nextOf())) |
| 1589 |
{ |
| 1590 |
// Concatenate the arrays |
| 1591 |
auto elems = copyElements(e1, e2); |
| 1592 |
|
| 1593 |
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, cast(Type)null, elems); |
| 1594 |
|
| 1595 |
e = ue.exp(); |
| 1596 |
if (type.toBasetype().ty == Tsarray) |
| 1597 |
{ |
| 1598 |
e.type = t1.nextOf().sarrayOf(elems.length); |
| 1599 |
} |
| 1600 |
else |
| 1601 |
e.type = type; |
| 1602 |
assert(ue.exp().type); |
| 1603 |
return ue; |
| 1604 |
} |
| 1605 |
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.null_ && t1.nextOf().equals(t2.nextOf())) |
| 1606 |
{ |
| 1607 |
e = e1; |
| 1608 |
goto L3; |
| 1609 |
} |
| 1610 |
else if (e1.op == EXP.null_ && e2.op == EXP.arrayLiteral && t1.nextOf().equals(t2.nextOf())) |
| 1611 |
{ |
| 1612 |
e = e2; |
| 1613 |
L3: |
| 1614 |
// Concatenate the array with null |
| 1615 |
auto elems = copyElements(e); |
| 1616 |
|
| 1617 |
emplaceExp!(ArrayLiteralExp)(&ue, e.loc, cast(Type)null, elems); |
| 1618 |
|
| 1619 |
e = ue.exp(); |
| 1620 |
if (type.toBasetype().ty == Tsarray) |
| 1621 |
{ |
| 1622 |
e.type = t1.nextOf().sarrayOf(elems.length); |
| 1623 |
} |
| 1624 |
else |
| 1625 |
e.type = type; |
| 1626 |
assert(ue.exp().type); |
| 1627 |
return ue; |
| 1628 |
} |
| 1629 |
else if ((e1.op == EXP.arrayLiteral || e1.op == EXP.null_) && e1.type.toBasetype().nextOf() && e1.type.toBasetype().nextOf().equals(e2.type)) |
| 1630 |
{ |
| 1631 |
auto elems = (e1.op == EXP.arrayLiteral) |
| 1632 |
? copyElements(e1) : new Expressions(); |
| 1633 |
elems.push(e2); |
| 1634 |
|
| 1635 |
emplaceExp!(ArrayLiteralExp)(&ue, loc, cast(Type)null, elems); |
| 1636 |
|
| 1637 |
e = ue.exp(); |
| 1638 |
if (type.toBasetype().ty == Tsarray) |
| 1639 |
{ |
| 1640 |
e.type = e2.type.sarrayOf(elems.length); |
| 1641 |
} |
| 1642 |
else |
| 1643 |
e.type = type; |
| 1644 |
assert(ue.exp().type); |
| 1645 |
return ue; |
| 1646 |
} |
| 1647 |
else if (e2.op == EXP.arrayLiteral && e2.type.toBasetype().nextOf().equals(e1.type)) |
| 1648 |
{ |
| 1649 |
auto elems = copyElements(e1, e2); |
| 1650 |
|
| 1651 |
emplaceExp!(ArrayLiteralExp)(&ue, loc, cast(Type)null, elems); |
| 1652 |
|
| 1653 |
e = ue.exp(); |
| 1654 |
if (type.toBasetype().ty == Tsarray) |
| 1655 |
{ |
| 1656 |
e.type = e1.type.sarrayOf(elems.length); |
| 1657 |
} |
| 1658 |
else |
| 1659 |
e.type = type; |
| 1660 |
assert(ue.exp().type); |
| 1661 |
return ue; |
| 1662 |
} |
| 1663 |
else if (e1.op == EXP.null_ && e2.op == EXP.string_) |
| 1664 |
{ |
| 1665 |
t = e1.type; |
| 1666 |
e = e2; |
| 1667 |
goto L1; |
| 1668 |
} |
| 1669 |
else if (e1.op == EXP.string_ && e2.op == EXP.null_) |
| 1670 |
{ |
| 1671 |
e = e1; |
| 1672 |
t = e2.type; |
| 1673 |
L1: |
| 1674 |
Type tb = t.toBasetype(); |
| 1675 |
if (tb.ty == Tarray && tb.nextOf().equivalent(e.type)) |
| 1676 |
{ |
| 1677 |
auto expressions = new Expressions(); |
| 1678 |
expressions.push(e); |
| 1679 |
emplaceExp!(ArrayLiteralExp)(&ue, loc, t, expressions); |
| 1680 |
e = ue.exp(); |
| 1681 |
} |
| 1682 |
else |
| 1683 |
{ |
| 1684 |
emplaceExp!(UnionExp)(&ue, e); |
| 1685 |
e = ue.exp(); |
| 1686 |
} |
| 1687 |
if (!e.type.equals(type)) |
| 1688 |
{ |
| 1689 |
StringExp se = e.copy().isStringExp(); |
| 1690 |
e = se.castTo(null, type); |
| 1691 |
emplaceExp!(UnionExp)(&ue, e); |
| 1692 |
e = ue.exp(); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
else |
| 1696 |
cantExp(ue); |
| 1697 |
assert(ue.exp().type); |
| 1698 |
return ue; |
| 1699 |
} |
| 1700 |
|
| 1701 |
UnionExp Ptr(Type type, Expression e1) |
| 1702 |
{ |
| 1703 |
//printf("Ptr(e1 = %s)\n", e1.toChars()); |
| 1704 |
UnionExp ue = void; |
| 1705 |
if (AddExp ae = e1.isAddExp()) |
| 1706 |
{ |
| 1707 |
if (AddrExp ade = ae.e1.isAddrExp()) |
| 1708 |
{ |
| 1709 |
if (ae.e2.op == EXP.int64) |
| 1710 |
if (StructLiteralExp se = ade.e1.isStructLiteralExp()) |
| 1711 |
{ |
| 1712 |
uint offset = cast(uint)ae.e2.toInteger(); |
| 1713 |
Expression e = se.getField(type, offset); |
| 1714 |
if (e) |
| 1715 |
{ |
| 1716 |
emplaceExp!(UnionExp)(&ue, e); |
| 1717 |
return ue; |
| 1718 |
} |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| 1722 |
cantExp(ue); |
| 1723 |
return ue; |
| 1724 |
} |