| 1 |
/** |
| 2 |
* CTFE for expressions involving pointers, slices, array concatenation etc. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ctfeexpr.d, _ctfeexpr.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_ctfeexpr.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/ctfeexpr.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.ctfeexpr; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
import core.stdc.string; |
| 16 |
import dmd.arraytypes; |
| 17 |
import dmd.astenums; |
| 18 |
import dmd.constfold; |
| 19 |
import dmd.compiler; |
| 20 |
import dmd.dcast : implicitConvTo; |
| 21 |
import dmd.dclass; |
| 22 |
import dmd.declaration; |
| 23 |
import dmd.dinterpret; |
| 24 |
import dmd.dstruct; |
| 25 |
import dmd.dtemplate; |
| 26 |
import dmd.errors; |
| 27 |
import dmd.expression; |
| 28 |
import dmd.func; |
| 29 |
import dmd.globals; |
| 30 |
import dmd.location; |
| 31 |
import dmd.mtype; |
| 32 |
import dmd.root.bitarray; |
| 33 |
import dmd.root.complex; |
| 34 |
import dmd.root.ctfloat; |
| 35 |
import dmd.root.port; |
| 36 |
import dmd.root.rmem; |
| 37 |
import dmd.tokens; |
| 38 |
import dmd.typesem; |
| 39 |
import dmd.visitor; |
| 40 |
|
| 41 |
/****************************************************************/ |
| 42 |
/* A type meant as a union of all the Expression types, |
| 43 |
* to serve essentially as a Variant that will sit on the stack |
| 44 |
* during CTFE to reduce memory consumption. |
| 45 |
*/ |
| 46 |
extern (D) struct UnionExp |
| 47 |
{ |
| 48 |
// yes, default constructor does nothing |
| 49 |
extern (D) this(Expression e) nothrow |
| 50 |
{ |
| 51 |
memcpy(&this, cast(void*)e, e.size); |
| 52 |
} |
| 53 |
|
| 54 |
/* Extract pointer to Expression |
| 55 |
*/ |
| 56 |
extern (D) Expression exp() return nothrow |
| 57 |
{ |
| 58 |
return cast(Expression)&u; |
| 59 |
} |
| 60 |
|
| 61 |
/* Convert to an allocated Expression |
| 62 |
*/ |
| 63 |
extern (D) Expression copy() |
| 64 |
{ |
| 65 |
Expression e = exp(); |
| 66 |
//if (e.size > sizeof(u)) printf("%s\n", EXPtoString(e.op).ptr); |
| 67 |
assert(e.size <= u.sizeof); |
| 68 |
switch (e.op) |
| 69 |
{ |
| 70 |
case EXP.cantExpression: return CTFEExp.cantexp; |
| 71 |
case EXP.voidExpression: return CTFEExp.voidexp; |
| 72 |
case EXP.break_: return CTFEExp.breakexp; |
| 73 |
case EXP.continue_: return CTFEExp.continueexp; |
| 74 |
case EXP.goto_: return CTFEExp.gotoexp; |
| 75 |
default: return e.copy(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
// Ensure that the union is suitably aligned. |
| 81 |
align(8) union _AnonStruct_u |
| 82 |
{ |
| 83 |
char[__traits(classInstanceSize, Expression)] exp; |
| 84 |
char[__traits(classInstanceSize, IntegerExp)] integerexp; |
| 85 |
char[__traits(classInstanceSize, ErrorExp)] errorexp; |
| 86 |
char[__traits(classInstanceSize, RealExp)] realexp; |
| 87 |
char[__traits(classInstanceSize, ComplexExp)] complexexp; |
| 88 |
char[__traits(classInstanceSize, SymOffExp)] symoffexp; |
| 89 |
char[__traits(classInstanceSize, StringExp)] stringexp; |
| 90 |
char[__traits(classInstanceSize, ArrayLiteralExp)] arrayliteralexp; |
| 91 |
char[__traits(classInstanceSize, AssocArrayLiteralExp)] assocarrayliteralexp; |
| 92 |
char[__traits(classInstanceSize, StructLiteralExp)] structliteralexp; |
| 93 |
char[__traits(classInstanceSize, CompoundLiteralExp)] compoundliteralexp; |
| 94 |
char[__traits(classInstanceSize, NullExp)] nullexp; |
| 95 |
char[__traits(classInstanceSize, DotVarExp)] dotvarexp; |
| 96 |
char[__traits(classInstanceSize, AddrExp)] addrexp; |
| 97 |
char[__traits(classInstanceSize, IndexExp)] indexexp; |
| 98 |
char[__traits(classInstanceSize, SliceExp)] sliceexp; |
| 99 |
char[__traits(classInstanceSize, VectorExp)] vectorexp; |
| 100 |
} |
| 101 |
|
| 102 |
_AnonStruct_u u; |
| 103 |
} |
| 104 |
|
| 105 |
void emplaceExp(T : Expression, Args...)(void* p, Args args) |
| 106 |
{ |
| 107 |
static if (__VERSION__ < 2099) |
| 108 |
const init = typeid(T).initializer; |
| 109 |
else |
| 110 |
const init = __traits(initSymbol, T); |
| 111 |
p[0 .. __traits(classInstanceSize, T)] = init[]; |
| 112 |
(cast(T)p).__ctor(args); |
| 113 |
} |
| 114 |
|
| 115 |
void emplaceExp(T : UnionExp)(T* p, Expression e) nothrow |
| 116 |
{ |
| 117 |
memcpy(p, cast(void*)e, e.size); |
| 118 |
} |
| 119 |
|
| 120 |
// Generate an error message when this exception is not caught |
| 121 |
void generateUncaughtError(ThrownExceptionExp tee) |
| 122 |
{ |
| 123 |
UnionExp ue = void; |
| 124 |
Expression e = resolveSlice((*tee.thrown.value.elements)[0], &ue); |
| 125 |
StringExp se = e.toStringExp(); |
| 126 |
error(tee.thrown.loc, "uncaught CTFE exception `%s(%s)`", tee.thrown.type.toChars(), se ? se.toChars() : e.toChars()); |
| 127 |
/* Also give the line where the throw statement was. We won't have it |
| 128 |
* in the case where the ThrowStatement is generated internally |
| 129 |
* (eg, in ScopeStatement) |
| 130 |
*/ |
| 131 |
if (tee.loc.isValid() && !tee.loc.equals(tee.thrown.loc)) |
| 132 |
.errorSupplemental(tee.loc, "thrown from here"); |
| 133 |
} |
| 134 |
|
| 135 |
/************************* |
| 136 |
* Same as getFieldIndex, but checks for a direct match with the VarDeclaration |
| 137 |
* Returns: |
| 138 |
* index of the field, or -1 if not found |
| 139 |
*/ |
| 140 |
int findFieldIndexByName(const StructDeclaration sd, const VarDeclaration v) pure @safe nothrow |
| 141 |
{ |
| 142 |
foreach (i, field; sd.fields) |
| 143 |
{ |
| 144 |
if (field == v) |
| 145 |
return cast(int)i; |
| 146 |
} |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
// True if 'e' is CTFEExp::cantexp, or an exception |
| 151 |
bool exceptionOrCantInterpret(const Expression e) @safe nothrow |
| 152 |
{ |
| 153 |
return e && (e.op == EXP.cantExpression || e.op == EXP.thrownException || e.op == EXP.showCtfeContext); |
| 154 |
} |
| 155 |
|
| 156 |
/************** Aggregate literals (AA/string/array/struct) ******************/ |
| 157 |
// Given expr, which evaluates to an array/AA/string literal, |
| 158 |
// return true if it needs to be copied |
| 159 |
bool needToCopyLiteral(const Expression expr) nothrow |
| 160 |
{ |
| 161 |
Expression e = cast()expr; |
| 162 |
for (;;) |
| 163 |
{ |
| 164 |
switch (e.op) |
| 165 |
{ |
| 166 |
case EXP.arrayLiteral: |
| 167 |
return e.isArrayLiteralExp().ownedByCtfe == OwnedBy.code; |
| 168 |
case EXP.assocArrayLiteral: |
| 169 |
return e.isAssocArrayLiteralExp().ownedByCtfe == OwnedBy.code; |
| 170 |
case EXP.structLiteral: |
| 171 |
return e.isStructLiteralExp().ownedByCtfe == OwnedBy.code; |
| 172 |
case EXP.string_: |
| 173 |
case EXP.this_: |
| 174 |
case EXP.variable: |
| 175 |
return false; |
| 176 |
case EXP.assign: |
| 177 |
return false; |
| 178 |
case EXP.index: |
| 179 |
case EXP.dotVariable: |
| 180 |
case EXP.slice: |
| 181 |
case EXP.cast_: |
| 182 |
e = e.isUnaExp().e1; |
| 183 |
continue; |
| 184 |
case EXP.concatenate: |
| 185 |
return needToCopyLiteral(e.isBinExp().e1) || needToCopyLiteral(e.isBinExp().e2); |
| 186 |
case EXP.concatenateAssign: |
| 187 |
case EXP.concatenateElemAssign: |
| 188 |
case EXP.concatenateDcharAssign: |
| 189 |
e = e.isBinExp().e2; |
| 190 |
continue; |
| 191 |
default: |
| 192 |
return false; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
private Expressions* copyLiteralArray(Expressions* oldelems, Expression basis = null) |
| 198 |
{ |
| 199 |
if (!oldelems) |
| 200 |
return oldelems; |
| 201 |
incArrayAllocs(); |
| 202 |
auto newelems = new Expressions(oldelems.length); |
| 203 |
foreach (i, el; *oldelems) |
| 204 |
{ |
| 205 |
(*newelems)[i] = copyLiteral(el ? el : basis).copy(); |
| 206 |
} |
| 207 |
return newelems; |
| 208 |
} |
| 209 |
|
| 210 |
// Make a copy of the ArrayLiteral, AALiteral, String, or StructLiteral. |
| 211 |
// This value will be used for in-place modification. |
| 212 |
UnionExp copyLiteral(Expression e) |
| 213 |
{ |
| 214 |
UnionExp ue = void; |
| 215 |
if (auto se = e.isStringExp()) // syntaxCopy doesn't make a copy for StringExp! |
| 216 |
{ |
| 217 |
char* s = cast(char*)mem.xcalloc(se.len + 1, se.sz); |
| 218 |
const slice = se.peekData(); |
| 219 |
memcpy(s, slice.ptr, slice.length); |
| 220 |
emplaceExp!(StringExp)(&ue, se.loc, s[0 .. se.len * se.sz], se.len, se.sz); |
| 221 |
StringExp se2 = ue.exp().isStringExp(); |
| 222 |
se2.committed = se.committed; |
| 223 |
se2.postfix = se.postfix; |
| 224 |
se2.type = se.type; |
| 225 |
se2.ownedByCtfe = OwnedBy.ctfe; |
| 226 |
return ue; |
| 227 |
} |
| 228 |
if (auto ale = e.isArrayLiteralExp()) |
| 229 |
{ |
| 230 |
auto elements = copyLiteralArray(ale.elements, ale.basis); |
| 231 |
|
| 232 |
emplaceExp!(ArrayLiteralExp)(&ue, e.loc, e.type, elements); |
| 233 |
|
| 234 |
ArrayLiteralExp r = ue.exp().isArrayLiteralExp(); |
| 235 |
r.ownedByCtfe = OwnedBy.ctfe; |
| 236 |
return ue; |
| 237 |
} |
| 238 |
if (auto aae = e.isAssocArrayLiteralExp()) |
| 239 |
{ |
| 240 |
emplaceExp!(AssocArrayLiteralExp)(&ue, aae.loc, copyLiteralArray(aae.keys), copyLiteralArray(aae.values)); |
| 241 |
AssocArrayLiteralExp r = ue.exp().isAssocArrayLiteralExp(); |
| 242 |
r.type = aae.type; |
| 243 |
r.lowering = aae.lowering; |
| 244 |
r.ownedByCtfe = OwnedBy.ctfe; |
| 245 |
return ue; |
| 246 |
} |
| 247 |
if (auto sle = e.isStructLiteralExp()) |
| 248 |
{ |
| 249 |
/* syntaxCopy doesn't work for struct literals, because of a nasty special |
| 250 |
* case: block assignment is permitted inside struct literals, eg, |
| 251 |
* an int[4] array can be initialized with a single int. |
| 252 |
*/ |
| 253 |
auto oldelems = sle.elements; |
| 254 |
auto newelems = new Expressions(oldelems.length); |
| 255 |
foreach (i, ref el; *newelems) |
| 256 |
{ |
| 257 |
// We need the struct definition to detect block assignment |
| 258 |
auto v = sle.sd.fields[i]; |
| 259 |
auto m = (*oldelems)[i]; |
| 260 |
|
| 261 |
// If it is a void assignment, use the default initializer |
| 262 |
if (!m) |
| 263 |
m = voidInitLiteral(v.type, v).copy(); |
| 264 |
|
| 265 |
if (v.type.ty == Tarray || v.type.ty == Taarray) |
| 266 |
{ |
| 267 |
// Don't have to copy array references |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
// Buzilla 15681: Copy the source element always. |
| 272 |
m = copyLiteral(m).copy(); |
| 273 |
|
| 274 |
// Block assignment from inside struct literals |
| 275 |
if (v.type.ty != m.type.ty && v.type.ty == Tsarray) |
| 276 |
{ |
| 277 |
auto tsa = v.type.isTypeSArray(); |
| 278 |
auto len = cast(size_t)tsa.dim.toInteger(); |
| 279 |
m = createBlockDuplicatedArrayLiteral(&ue, e.loc, v.type, m, len); |
| 280 |
if (m == ue.exp()) |
| 281 |
m = ue.copy(); |
| 282 |
} |
| 283 |
} |
| 284 |
el = m; |
| 285 |
} |
| 286 |
emplaceExp!(StructLiteralExp)(&ue, e.loc, sle.sd, newelems, sle.stype); |
| 287 |
auto r = ue.exp().isStructLiteralExp(); |
| 288 |
r.type = e.type; |
| 289 |
r.ownedByCtfe = OwnedBy.ctfe; |
| 290 |
r.origin = sle.origin; |
| 291 |
return ue; |
| 292 |
} |
| 293 |
|
| 294 |
switch(e.op) |
| 295 |
{ |
| 296 |
case EXP.function_: |
| 297 |
case EXP.delegate_: |
| 298 |
case EXP.symbolOffset: |
| 299 |
case EXP.null_: |
| 300 |
case EXP.variable: |
| 301 |
case EXP.dotVariable: |
| 302 |
case EXP.int64: |
| 303 |
case EXP.float64: |
| 304 |
case EXP.complex80: |
| 305 |
case EXP.void_: |
| 306 |
case EXP.vector: |
| 307 |
case EXP.typeid_: |
| 308 |
// Simple value types |
| 309 |
// Keep e1 for DelegateExp and DotVarExp |
| 310 |
emplaceExp!(UnionExp)(&ue, e); |
| 311 |
Expression r = ue.exp(); |
| 312 |
r.type = e.type; |
| 313 |
return ue; |
| 314 |
default: break; |
| 315 |
} |
| 316 |
|
| 317 |
if (auto se = e.isSliceExp()) |
| 318 |
{ |
| 319 |
if (se.type.toBasetype().ty == Tsarray) |
| 320 |
{ |
| 321 |
// same with resolveSlice() |
| 322 |
if (se.e1.op == EXP.null_) |
| 323 |
{ |
| 324 |
emplaceExp!(NullExp)(&ue, se.loc, se.type); |
| 325 |
return ue; |
| 326 |
} |
| 327 |
ue = Slice(se.type, se.e1, se.lwr, se.upr); |
| 328 |
auto r = ue.exp().isArrayLiteralExp(); |
| 329 |
r.elements = copyLiteralArray(r.elements); |
| 330 |
r.ownedByCtfe = OwnedBy.ctfe; |
| 331 |
return ue; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
// Array slices only do a shallow copy |
| 336 |
emplaceExp!(SliceExp)(&ue, e.loc, se.e1, se.lwr, se.upr); |
| 337 |
Expression r = ue.exp(); |
| 338 |
r.type = e.type; |
| 339 |
return ue; |
| 340 |
} |
| 341 |
} |
| 342 |
if (isPointer(e.type)) |
| 343 |
{ |
| 344 |
// For pointers, we only do a shallow copy. |
| 345 |
if (auto ae = e.isAddrExp()) |
| 346 |
emplaceExp!(AddrExp)(&ue, e.loc, ae.e1); |
| 347 |
else if (auto ie = e.isIndexExp()) |
| 348 |
emplaceExp!(IndexExp)(&ue, e.loc, ie.e1, ie.e2); |
| 349 |
else if (auto dve = e.isDotVarExp()) |
| 350 |
{ |
| 351 |
emplaceExp!(DotVarExp)(&ue, e.loc, dve.e1, dve.var, dve.hasOverloads); |
| 352 |
} |
| 353 |
else |
| 354 |
assert(0); |
| 355 |
|
| 356 |
Expression r = ue.exp(); |
| 357 |
r.type = e.type; |
| 358 |
return ue; |
| 359 |
} |
| 360 |
if (auto cre = e.isClassReferenceExp()) |
| 361 |
{ |
| 362 |
emplaceExp!(ClassReferenceExp)(&ue, e.loc, cre.value, e.type); |
| 363 |
return ue; |
| 364 |
} |
| 365 |
if (e.op == EXP.error) |
| 366 |
{ |
| 367 |
emplaceExp!(UnionExp)(&ue, e); |
| 368 |
return ue; |
| 369 |
} |
| 370 |
error(e.loc, "CTFE internal error: literal `%s`", e.toChars()); |
| 371 |
assert(0); |
| 372 |
} |
| 373 |
|
| 374 |
/* Deal with type painting. |
| 375 |
* Type painting is a major nuisance: we can't just set |
| 376 |
* e.type = type, because that would change the original literal. |
| 377 |
* But, we can't simply copy the literal either, because that would change |
| 378 |
* the values of any pointers. |
| 379 |
*/ |
| 380 |
Expression paintTypeOntoLiteral(Type type, Expression lit) |
| 381 |
{ |
| 382 |
if (lit.type.equals(type)) |
| 383 |
return lit; |
| 384 |
return paintTypeOntoLiteralCopy(type, lit).copy(); |
| 385 |
} |
| 386 |
|
| 387 |
Expression paintTypeOntoLiteral(UnionExp* pue, Type type, Expression lit) |
| 388 |
{ |
| 389 |
if (lit.type.equals(type)) |
| 390 |
return lit; |
| 391 |
*pue = paintTypeOntoLiteralCopy(type, lit); |
| 392 |
return pue.exp(); |
| 393 |
} |
| 394 |
|
| 395 |
private UnionExp paintTypeOntoLiteralCopy(Type type, Expression lit) |
| 396 |
{ |
| 397 |
UnionExp ue; |
| 398 |
if (lit.type.equals(type)) |
| 399 |
{ |
| 400 |
emplaceExp!(UnionExp)(&ue, lit); |
| 401 |
return ue; |
| 402 |
} |
| 403 |
// If it is a cast to inout, retain the original type of the referenced part. |
| 404 |
if (type.hasWild()) |
| 405 |
{ |
| 406 |
emplaceExp!(UnionExp)(&ue, lit); |
| 407 |
ue.exp().type = type; |
| 408 |
return ue; |
| 409 |
} |
| 410 |
if (auto se = lit.isSliceExp()) |
| 411 |
{ |
| 412 |
emplaceExp!(SliceExp)(&ue, lit.loc, se.e1, se.lwr, se.upr); |
| 413 |
} |
| 414 |
else if (auto ie = lit.isIndexExp()) |
| 415 |
{ |
| 416 |
emplaceExp!(IndexExp)(&ue, lit.loc, ie.e1, ie.e2); |
| 417 |
} |
| 418 |
else if (lit.op == EXP.arrayLiteral) |
| 419 |
{ |
| 420 |
emplaceExp!(SliceExp)(&ue, lit.loc, lit, ctfeEmplaceExp!IntegerExp(Loc.initial, 0, Type.tsize_t), ArrayLength(Type.tsize_t, lit).copy()); |
| 421 |
} |
| 422 |
else if (lit.op == EXP.string_) |
| 423 |
{ |
| 424 |
// For strings, we need to introduce another level of indirection |
| 425 |
emplaceExp!(SliceExp)(&ue, lit.loc, lit, ctfeEmplaceExp!IntegerExp(Loc.initial, 0, Type.tsize_t), ArrayLength(Type.tsize_t, lit).copy()); |
| 426 |
} |
| 427 |
else if (auto aae = lit.isAssocArrayLiteralExp()) |
| 428 |
{ |
| 429 |
// TODO: we should be creating a reference to this AAExp, not |
| 430 |
// just a ref to the keys and values. |
| 431 |
OwnedBy wasOwned = aae.ownedByCtfe; |
| 432 |
emplaceExp!(AssocArrayLiteralExp)(&ue, lit.loc, aae.keys, aae.values); |
| 433 |
aae = ue.exp().isAssocArrayLiteralExp(); |
| 434 |
aae.ownedByCtfe = wasOwned; |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
// Can't type paint from struct to struct*; this needs another |
| 439 |
// level of indirection |
| 440 |
if (lit.op == EXP.structLiteral && isPointer(type)) |
| 441 |
error(lit.loc, "CTFE internal error: painting `%s`", type.toChars()); |
| 442 |
ue = copyLiteral(lit); |
| 443 |
} |
| 444 |
ue.exp().type = type; |
| 445 |
return ue; |
| 446 |
} |
| 447 |
|
| 448 |
/************************************* |
| 449 |
* If e is a SliceExp, constant fold it. |
| 450 |
* Params: |
| 451 |
* e = expression to resolve |
| 452 |
* pue = if not null, store resulting expression here |
| 453 |
* Returns: |
| 454 |
* resulting expression |
| 455 |
*/ |
| 456 |
Expression resolveSlice(Expression e, UnionExp* pue = null) |
| 457 |
{ |
| 458 |
SliceExp se = e.isSliceExp(); |
| 459 |
if (!se) |
| 460 |
return e; |
| 461 |
if (se.e1.op == EXP.null_) |
| 462 |
return se.e1; |
| 463 |
if (pue) |
| 464 |
{ |
| 465 |
*pue = Slice(e.type, se.e1, se.lwr, se.upr); |
| 466 |
return pue.exp(); |
| 467 |
} |
| 468 |
return Slice(e.type, se.e1, se.lwr, se.upr).copy(); |
| 469 |
} |
| 470 |
|
| 471 |
/* Determine the array length, without interpreting it. |
| 472 |
* e must be an array literal, or a slice |
| 473 |
* It's very wasteful to resolve the slice when we only |
| 474 |
* need the length. |
| 475 |
*/ |
| 476 |
uinteger_t resolveArrayLength(Expression e) |
| 477 |
{ |
| 478 |
switch (e.op) |
| 479 |
{ |
| 480 |
case EXP.vector: |
| 481 |
return e.isVectorExp().dim; |
| 482 |
|
| 483 |
case EXP.null_: |
| 484 |
return 0; |
| 485 |
|
| 486 |
case EXP.slice: |
| 487 |
{ |
| 488 |
auto se = e.isSliceExp(); |
| 489 |
const ilo = se.lwr.toInteger(); |
| 490 |
const iup = se.upr.toInteger(); |
| 491 |
return iup - ilo; |
| 492 |
} |
| 493 |
|
| 494 |
case EXP.string_: |
| 495 |
return e.isStringExp().len; |
| 496 |
|
| 497 |
case EXP.arrayLiteral: |
| 498 |
{ |
| 499 |
const ale = e.isArrayLiteralExp(); |
| 500 |
return ale.elements ? ale.elements.length : 0; |
| 501 |
} |
| 502 |
|
| 503 |
case EXP.assocArrayLiteral: |
| 504 |
{ |
| 505 |
return e.isAssocArrayLiteralExp().keys.length; |
| 506 |
} |
| 507 |
|
| 508 |
default: |
| 509 |
assert(0); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
/****************************** |
| 514 |
* Helper for NewExp |
| 515 |
* Create an array literal consisting of 'elem' duplicated 'dim' times. |
| 516 |
* Params: |
| 517 |
* pue = where to store result |
| 518 |
* loc = source location where the interpretation occurs |
| 519 |
* type = target type of the result |
| 520 |
* elem = the source of array element, it will be owned by the result |
| 521 |
* dim = element number of the result |
| 522 |
* Returns: |
| 523 |
* Constructed ArrayLiteralExp |
| 524 |
*/ |
| 525 |
ArrayLiteralExp createBlockDuplicatedArrayLiteral(UnionExp* pue, Loc loc, Type type, Expression elem, size_t dim) |
| 526 |
{ |
| 527 |
if (type.ty == Tsarray && type.nextOf().ty == Tsarray && elem.type.ty != Tsarray) |
| 528 |
{ |
| 529 |
// If it is a multidimensional array literal, do it recursively |
| 530 |
auto tsa = type.nextOf().isTypeSArray(); |
| 531 |
const len = cast(size_t)tsa.dim.toInteger(); |
| 532 |
elem = createBlockDuplicatedArrayLiteral(pue, loc, type.nextOf(), elem, len); |
| 533 |
if (elem == pue.exp()) |
| 534 |
elem = pue.copy(); |
| 535 |
} |
| 536 |
|
| 537 |
// Buzilla 15681 |
| 538 |
const tb = elem.type.toBasetype(); |
| 539 |
const mustCopy = tb.ty == Tstruct || tb.ty == Tsarray; |
| 540 |
|
| 541 |
auto elements = new Expressions(dim); |
| 542 |
foreach (i, ref el; *elements) |
| 543 |
{ |
| 544 |
el = mustCopy && i ? copyLiteral(elem).copy() : elem; |
| 545 |
} |
| 546 |
emplaceExp!(ArrayLiteralExp)(pue, loc, type, elements); |
| 547 |
auto ale = pue.exp().isArrayLiteralExp(); |
| 548 |
ale.ownedByCtfe = OwnedBy.ctfe; |
| 549 |
return ale; |
| 550 |
} |
| 551 |
|
| 552 |
/****************************** |
| 553 |
* Helper for NewExp |
| 554 |
* Create a string literal consisting of 'value' duplicated 'dim' times. |
| 555 |
*/ |
| 556 |
StringExp createBlockDuplicatedStringLiteral(UnionExp* pue, Loc loc, Type type, dchar value, size_t dim, ubyte sz) |
| 557 |
{ |
| 558 |
auto s = cast(char*)mem.xcalloc(dim, sz); |
| 559 |
foreach (elemi; 0 .. dim) |
| 560 |
{ |
| 561 |
switch (sz) |
| 562 |
{ |
| 563 |
case 1: |
| 564 |
s[elemi] = cast(char)value; |
| 565 |
break; |
| 566 |
case 2: |
| 567 |
(cast(wchar*)s)[elemi] = cast(wchar)value; |
| 568 |
break; |
| 569 |
case 4: |
| 570 |
(cast(dchar*)s)[elemi] = value; |
| 571 |
break; |
| 572 |
case 8: |
| 573 |
(cast(ulong*)s)[elemi] = value; |
| 574 |
break; |
| 575 |
default: |
| 576 |
assert(0); |
| 577 |
} |
| 578 |
} |
| 579 |
emplaceExp!(StringExp)(pue, loc, s[0 .. dim * sz], dim, sz); |
| 580 |
auto se = pue.exp().isStringExp(); |
| 581 |
se.type = type; |
| 582 |
se.committed = true; |
| 583 |
se.ownedByCtfe = OwnedBy.ctfe; |
| 584 |
return se; |
| 585 |
} |
| 586 |
|
| 587 |
// Return true if t is an AA |
| 588 |
bool isAssocArray(Type t) |
| 589 |
{ |
| 590 |
return t.toBasetype().isTypeAArray() !is null; |
| 591 |
} |
| 592 |
|
| 593 |
// Given a template AA type, extract the corresponding built-in AA type |
| 594 |
TypeAArray toBuiltinAAType(Type t) |
| 595 |
{ |
| 596 |
return t.toBasetype().isTypeAArray(); |
| 597 |
} |
| 598 |
|
| 599 |
/************** TypeInfo operations ************************************/ |
| 600 |
// Return true if type is TypeInfo_Class |
| 601 |
bool isTypeInfo_Class(const Type type) nothrow |
| 602 |
{ |
| 603 |
auto tc = cast()type.isTypeClass(); |
| 604 |
return tc && (Type.dtypeinfo == tc.sym || Type.dtypeinfo.isBaseOf(tc.sym, null)); |
| 605 |
} |
| 606 |
|
| 607 |
/************** Pointer operations ************************************/ |
| 608 |
// Return true if t is a pointer (not a function pointer) |
| 609 |
bool isPointer(Type t) |
| 610 |
{ |
| 611 |
Type tb = t.toBasetype(); |
| 612 |
return tb.ty == Tpointer && tb.nextOf().ty != Tfunction; |
| 613 |
} |
| 614 |
|
| 615 |
// For CTFE only. Returns true if 'e' is true or a non-null pointer. |
| 616 |
bool isTrueBool(Expression e) |
| 617 |
{ |
| 618 |
return e.toBool().hasValue(true) || ((e.type.ty == Tpointer || e.type.ty == Tclass) && e.op != EXP.null_); |
| 619 |
} |
| 620 |
|
| 621 |
/* Is it safe to convert from srcPointee* to destPointee* ? |
| 622 |
* srcPointee is the genuine type (never void). |
| 623 |
* destPointee may be void. |
| 624 |
*/ |
| 625 |
bool isSafePointerCast(Type srcPointee, Type destPointee) |
| 626 |
{ |
| 627 |
// It's safe to cast S** to D** if it's OK to cast S* to D* |
| 628 |
while (srcPointee.ty == Tpointer && destPointee.ty == Tpointer) |
| 629 |
{ |
| 630 |
srcPointee = srcPointee.nextOf(); |
| 631 |
destPointee = destPointee.nextOf(); |
| 632 |
} |
| 633 |
// It's OK if both are the same (modulo const) |
| 634 |
if (srcPointee.constConv(destPointee)) |
| 635 |
return true; |
| 636 |
|
| 637 |
// It's ok to cast from/to shared because CTFE is single threaded anyways |
| 638 |
if (srcPointee.unSharedOf() == destPointee.unSharedOf()) |
| 639 |
return true; |
| 640 |
|
| 641 |
// It's OK if function pointers differ only in safe/pure/nothrow |
| 642 |
if (srcPointee.ty == Tfunction && destPointee.ty == Tfunction) |
| 643 |
{ |
| 644 |
return srcPointee.covariant(destPointee) == Covariant.yes || |
| 645 |
destPointee.covariant(srcPointee) == Covariant.yes; |
| 646 |
} |
| 647 |
// it's OK to cast to void* |
| 648 |
if (destPointee.ty == Tvoid) |
| 649 |
return true; |
| 650 |
// It's OK to cast from V[K] to void* |
| 651 |
if (srcPointee.ty == Taarray && destPointee == Type.tvoidptr) |
| 652 |
return true; |
| 653 |
// It's OK if they are the same size (static array of) integers, eg: |
| 654 |
// int* --> uint* |
| 655 |
// int[5][] --> uint[5][] |
| 656 |
if (srcPointee.ty == Tsarray && destPointee.ty == Tsarray) |
| 657 |
{ |
| 658 |
if (srcPointee.size() != destPointee.size()) |
| 659 |
return false; |
| 660 |
srcPointee = srcPointee.baseElemOf(); |
| 661 |
destPointee = destPointee.baseElemOf(); |
| 662 |
} |
| 663 |
return srcPointee.isIntegral() && destPointee.isIntegral() && srcPointee.size() == destPointee.size(); |
| 664 |
} |
| 665 |
|
| 666 |
Expression getAggregateFromPointer(Expression e, dinteger_t* ofs) |
| 667 |
{ |
| 668 |
*ofs = 0; |
| 669 |
if (auto ae = e.isAddrExp()) |
| 670 |
e = ae.e1; |
| 671 |
if (auto soe = e.isSymOffExp()) |
| 672 |
*ofs = soe.offset; |
| 673 |
if (auto dve = e.isDotVarExp()) |
| 674 |
{ |
| 675 |
auto ex = dve.e1; |
| 676 |
const v = dve.var.isVarDeclaration(); |
| 677 |
assert(v); |
| 678 |
StructLiteralExp se = (ex.op == EXP.classReference) |
| 679 |
? ex.isClassReferenceExp().value |
| 680 |
: ex.isStructLiteralExp(); |
| 681 |
|
| 682 |
// We can't use getField, because it makes a copy |
| 683 |
const i = (ex.op == EXP.classReference) |
| 684 |
? ex.isClassReferenceExp().getFieldIndex(e.type, v.offset) |
| 685 |
: se.getFieldIndex(e.type, v.offset); |
| 686 |
e = (*se.elements)[i]; |
| 687 |
} |
| 688 |
if (auto ie = e.isIndexExp()) |
| 689 |
{ |
| 690 |
// Note that each AA element is part of its own memory block |
| 691 |
if ((ie.e1.type.isStaticOrDynamicArray() || ie.e1.op == EXP.string_ || ie.e1.op == EXP.arrayLiteral) && ie.e2.op == EXP.int64) |
| 692 |
{ |
| 693 |
*ofs = ie.e2.toInteger(); |
| 694 |
return ie.e1; |
| 695 |
} |
| 696 |
} |
| 697 |
if (auto se = e.isSliceExp()) |
| 698 |
{ |
| 699 |
if (se && e.type.toBasetype().ty == Tsarray && |
| 700 |
(se.e1.type.isStaticOrDynamicArray() || se.e1.op == EXP.string_ || se.e1.op == EXP.arrayLiteral) && se.lwr.op == EXP.int64) |
| 701 |
{ |
| 702 |
*ofs = se.lwr.toInteger(); |
| 703 |
return se.e1; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
// It can be a `null` disguised as a cast, e.g. `cast(void*)0`. |
| 708 |
if (auto ie = e.isIntegerExp()) |
| 709 |
if (ie.type.ty == Tpointer && ie.getInteger() == 0) |
| 710 |
return new NullExp(ie.loc, e.type.nextOf()); |
| 711 |
// Those casts are invalid, but let the rest of the code handle it, |
| 712 |
// as it could be something like `x !is null`, which doesn't need |
| 713 |
// to dereference the pointer, even if the pointer is `cast(void*)420`. |
| 714 |
|
| 715 |
return e; |
| 716 |
} |
| 717 |
|
| 718 |
/** Return true if agg1 and agg2 are pointers to the same memory block |
| 719 |
*/ |
| 720 |
bool pointToSameMemoryBlock(Expression agg1, Expression agg2) |
| 721 |
{ |
| 722 |
if (agg1 == agg2) |
| 723 |
return true; |
| 724 |
// For integers cast to pointers, we regard them as non-comparable |
| 725 |
// unless they are identical. (This may be overly strict). |
| 726 |
if (agg1.op == EXP.int64 && agg2.op == EXP.int64 && agg1.toInteger() == agg2.toInteger()) |
| 727 |
{ |
| 728 |
return true; |
| 729 |
} |
| 730 |
// Note that type painting can occur with VarExp, so we |
| 731 |
// must compare the variables being pointed to. |
| 732 |
if (agg1.op == EXP.variable && agg2.op == EXP.variable && agg1.isVarExp().var == agg2.isVarExp().var) |
| 733 |
{ |
| 734 |
return true; |
| 735 |
} |
| 736 |
if (agg1.op == EXP.symbolOffset && agg2.op == EXP.symbolOffset && agg1.isSymOffExp().var == agg2.isSymOffExp().var) |
| 737 |
{ |
| 738 |
return true; |
| 739 |
} |
| 740 |
return false; |
| 741 |
} |
| 742 |
|
| 743 |
// return e1 - e2 as an integer, or error if not possible |
| 744 |
Expression pointerDifference(UnionExp* pue, Loc loc, Type type, Expression e1, Expression e2) |
| 745 |
{ |
| 746 |
dinteger_t ofs1, ofs2; |
| 747 |
Expression agg1 = getAggregateFromPointer(e1, &ofs1); |
| 748 |
Expression agg2 = getAggregateFromPointer(e2, &ofs2); |
| 749 |
if (agg1 == agg2) |
| 750 |
{ |
| 751 |
Type pointee = agg1.type.nextOf(); |
| 752 |
const sz = pointee.size(); |
| 753 |
emplaceExp!(IntegerExp)(pue, loc, (ofs1 - ofs2) * sz, type); |
| 754 |
} |
| 755 |
else if (agg1.op == EXP.string_ && agg2.op == EXP.string_ && |
| 756 |
agg1.isStringExp().peekString().ptr == agg2.isStringExp().peekString().ptr) |
| 757 |
{ |
| 758 |
Type pointee = agg1.type.nextOf(); |
| 759 |
const sz = pointee.size(); |
| 760 |
emplaceExp!(IntegerExp)(pue, loc, (ofs1 - ofs2) * sz, type); |
| 761 |
} |
| 762 |
else if (agg1.op == EXP.symbolOffset && agg2.op == EXP.symbolOffset && |
| 763 |
agg1.isSymOffExp().var == agg2.isSymOffExp().var) |
| 764 |
{ |
| 765 |
emplaceExp!(IntegerExp)(pue, loc, ofs1 - ofs2, type); |
| 766 |
} |
| 767 |
else |
| 768 |
{ |
| 769 |
error(loc, "`%s - %s` cannot be interpreted at compile time: cannot subtract pointers to two different memory blocks", e1.toChars(), e2.toChars()); |
| 770 |
emplaceExp!(CTFEExp)(pue, EXP.cantExpression); |
| 771 |
} |
| 772 |
return pue.exp(); |
| 773 |
} |
| 774 |
|
| 775 |
// Return eptr op e2, where eptr is a pointer, e2 is an integer, |
| 776 |
// and op is EXP.add or EXP.min |
| 777 |
Expression pointerArithmetic(UnionExp* pue, Loc loc, EXP op, Type type, Expression eptr, Expression e2) |
| 778 |
{ |
| 779 |
if (eptr.type.nextOf().ty == Tvoid) |
| 780 |
{ |
| 781 |
error(loc, "cannot perform arithmetic on `void*` pointers at compile time"); |
| 782 |
Lcant: |
| 783 |
emplaceExp!(CTFEExp)(pue, EXP.cantExpression); |
| 784 |
return pue.exp(); |
| 785 |
} |
| 786 |
if (eptr.op == EXP.address) |
| 787 |
eptr = eptr.isAddrExp().e1; |
| 788 |
dinteger_t ofs1; |
| 789 |
Expression agg1 = getAggregateFromPointer(eptr, &ofs1); |
| 790 |
if (agg1.op == EXP.symbolOffset) |
| 791 |
{ |
| 792 |
if (agg1.isSymOffExp().var.type.ty != Tsarray) |
| 793 |
{ |
| 794 |
error(loc, "cannot perform pointer arithmetic on arrays of unknown length at compile time"); |
| 795 |
goto Lcant; |
| 796 |
} |
| 797 |
} |
| 798 |
else if (agg1.op != EXP.string_ && agg1.op != EXP.arrayLiteral) |
| 799 |
{ |
| 800 |
error(loc, "cannot perform pointer arithmetic on non-arrays at compile time"); |
| 801 |
goto Lcant; |
| 802 |
} |
| 803 |
dinteger_t ofs2 = e2.toInteger(); |
| 804 |
Type pointee = agg1.type.toBasetype().nextOf(); |
| 805 |
dinteger_t sz = pointee.size(); |
| 806 |
sinteger_t indx; |
| 807 |
dinteger_t len; |
| 808 |
if (auto soe = agg1.isSymOffExp()) |
| 809 |
{ |
| 810 |
indx = ofs1 / sz; |
| 811 |
len = soe.var.type.isTypeSArray().dim.toInteger(); |
| 812 |
} |
| 813 |
else |
| 814 |
{ |
| 815 |
Expression dollar = ArrayLength(Type.tsize_t, agg1).copy(); |
| 816 |
assert(!CTFEExp.isCantExp(dollar)); |
| 817 |
indx = ofs1; |
| 818 |
len = dollar.toInteger(); |
| 819 |
} |
| 820 |
if (op == EXP.add || op == EXP.addAssign || op == EXP.plusPlus) |
| 821 |
indx += ofs2 / sz; |
| 822 |
else if (op == EXP.min || op == EXP.minAssign || op == EXP.minusMinus) |
| 823 |
indx -= ofs2 / sz; |
| 824 |
else |
| 825 |
{ |
| 826 |
error(loc, "CTFE internal error: bad pointer operation"); |
| 827 |
goto Lcant; |
| 828 |
} |
| 829 |
if (indx < 0 || len < indx) |
| 830 |
{ |
| 831 |
error(loc, "cannot assign pointer to index %lld inside memory block `[0..%lld]`", indx, len); |
| 832 |
goto Lcant; |
| 833 |
} |
| 834 |
if (agg1.op == EXP.symbolOffset) |
| 835 |
{ |
| 836 |
emplaceExp!(SymOffExp)(pue, loc, agg1.isSymOffExp().var, indx * sz); |
| 837 |
SymOffExp se = pue.exp().isSymOffExp(); |
| 838 |
se.type = type; |
| 839 |
return pue.exp(); |
| 840 |
} |
| 841 |
if (agg1.op != EXP.arrayLiteral && agg1.op != EXP.string_) |
| 842 |
{ |
| 843 |
error(loc, "CTFE internal error: pointer arithmetic `%s`", agg1.toChars()); |
| 844 |
goto Lcant; |
| 845 |
} |
| 846 |
if (auto tsa = eptr.type.toBasetype().isTypeSArray()) |
| 847 |
{ |
| 848 |
dinteger_t dim = tsa.dim.toInteger(); |
| 849 |
// Create a CTFE pointer &agg1[indx .. indx+dim] |
| 850 |
auto se = ctfeEmplaceExp!SliceExp(loc, agg1, |
| 851 |
ctfeEmplaceExp!IntegerExp(loc, indx, Type.tsize_t), |
| 852 |
ctfeEmplaceExp!IntegerExp(loc, indx + dim, Type.tsize_t)); |
| 853 |
se.type = type.toBasetype().nextOf(); |
| 854 |
emplaceExp!(AddrExp)(pue, loc, se); |
| 855 |
pue.exp().type = type; |
| 856 |
return pue.exp(); |
| 857 |
} |
| 858 |
// Create a CTFE pointer &agg1[indx] |
| 859 |
auto ofs = ctfeEmplaceExp!IntegerExp(loc, indx, Type.tsize_t); |
| 860 |
Expression ie = ctfeEmplaceExp!IndexExp(loc, agg1, ofs); |
| 861 |
ie.type = type.toBasetype().nextOf(); // https://issues.dlang.org/show_bug.cgi?id=13992 |
| 862 |
emplaceExp!(AddrExp)(pue, loc, ie); |
| 863 |
pue.exp().type = type; |
| 864 |
return pue.exp(); |
| 865 |
} |
| 866 |
|
| 867 |
// Return 1 if true, 0 if false |
| 868 |
// -1 if comparison is illegal because they point to non-comparable memory blocks |
| 869 |
int comparePointers(EXP op, Expression agg1, dinteger_t ofs1, Expression agg2, dinteger_t ofs2) |
| 870 |
{ |
| 871 |
if (pointToSameMemoryBlock(agg1, agg2)) |
| 872 |
{ |
| 873 |
int n; |
| 874 |
switch (op) |
| 875 |
{ |
| 876 |
case EXP.lessThan: |
| 877 |
n = (ofs1 < ofs2); |
| 878 |
break; |
| 879 |
case EXP.lessOrEqual: |
| 880 |
n = (ofs1 <= ofs2); |
| 881 |
break; |
| 882 |
case EXP.greaterThan: |
| 883 |
n = (ofs1 > ofs2); |
| 884 |
break; |
| 885 |
case EXP.greaterOrEqual: |
| 886 |
n = (ofs1 >= ofs2); |
| 887 |
break; |
| 888 |
case EXP.identity: |
| 889 |
case EXP.equal: |
| 890 |
n = (ofs1 == ofs2); |
| 891 |
break; |
| 892 |
case EXP.notIdentity: |
| 893 |
case EXP.notEqual: |
| 894 |
n = (ofs1 != ofs2); |
| 895 |
break; |
| 896 |
default: |
| 897 |
assert(0); |
| 898 |
} |
| 899 |
return n; |
| 900 |
} |
| 901 |
const null1 = (agg1.op == EXP.null_); |
| 902 |
const null2 = (agg2.op == EXP.null_); |
| 903 |
int cmp; |
| 904 |
if (null1 || null2) |
| 905 |
{ |
| 906 |
switch (op) |
| 907 |
{ |
| 908 |
case EXP.lessThan: |
| 909 |
cmp = null1 && !null2; |
| 910 |
break; |
| 911 |
case EXP.greaterThan: |
| 912 |
cmp = !null1 && null2; |
| 913 |
break; |
| 914 |
case EXP.lessOrEqual: |
| 915 |
cmp = null1; |
| 916 |
break; |
| 917 |
case EXP.greaterOrEqual: |
| 918 |
cmp = null2; |
| 919 |
break; |
| 920 |
case EXP.identity: |
| 921 |
case EXP.equal: |
| 922 |
case EXP.notIdentity: // 'cmp' gets inverted below |
| 923 |
case EXP.notEqual: |
| 924 |
cmp = (null1 == null2); |
| 925 |
break; |
| 926 |
default: |
| 927 |
assert(0); |
| 928 |
} |
| 929 |
} |
| 930 |
else |
| 931 |
{ |
| 932 |
switch (op) |
| 933 |
{ |
| 934 |
case EXP.identity: |
| 935 |
case EXP.equal: |
| 936 |
case EXP.notIdentity: // 'cmp' gets inverted below |
| 937 |
case EXP.notEqual: |
| 938 |
cmp = 0; |
| 939 |
break; |
| 940 |
default: |
| 941 |
return -1; // memory blocks are different |
| 942 |
} |
| 943 |
} |
| 944 |
if (op == EXP.notIdentity || op == EXP.notEqual) |
| 945 |
cmp ^= 1; |
| 946 |
return cmp; |
| 947 |
} |
| 948 |
|
| 949 |
// True if conversion from type 'from' to 'to' involves a reinterpret_cast |
| 950 |
// floating point -> integer or integer -> floating point |
| 951 |
bool isFloatIntPaint(Type to, Type from) |
| 952 |
{ |
| 953 |
return from.size() == to.size() && (from.isIntegral() && to.isFloating() || from.isFloating() && to.isIntegral()); |
| 954 |
} |
| 955 |
|
| 956 |
// Reinterpret float/int value 'fromVal' as a float/integer of type 'to'. |
| 957 |
Expression paintFloatInt(UnionExp* pue, Expression fromVal, Type to) |
| 958 |
{ |
| 959 |
if (exceptionOrCantInterpret(fromVal)) |
| 960 |
return fromVal; |
| 961 |
assert(to.size() == 4 || to.size() == 8); |
| 962 |
return Compiler.paintAsType(pue, fromVal, to); |
| 963 |
} |
| 964 |
|
| 965 |
/******** Constant folding, with support for CTFE ***************************/ |
| 966 |
/// Return true if non-pointer expression e can be compared |
| 967 |
/// with >,is, ==, etc, using ctfeCmp, ctfeEqual, ctfeIdentity |
| 968 |
bool isCtfeComparable(Expression e) |
| 969 |
{ |
| 970 |
if (e.op == EXP.slice) |
| 971 |
e = e.isSliceExp().e1; |
| 972 |
if (e.isConst() != 1) |
| 973 |
{ |
| 974 |
if (e.op == EXP.null_ || e.op == EXP.string_ || e.op == EXP.function_ || e.op == EXP.delegate_ || e.op == EXP.arrayLiteral || e.op == EXP.structLiteral || e.op == EXP.assocArrayLiteral || e.op == EXP.classReference) |
| 975 |
{ |
| 976 |
return true; |
| 977 |
} |
| 978 |
// https://issues.dlang.org/show_bug.cgi?id=14123 |
| 979 |
// TypeInfo object is comparable in CTFE |
| 980 |
if (e.op == EXP.typeid_) |
| 981 |
return true; |
| 982 |
return false; |
| 983 |
} |
| 984 |
return true; |
| 985 |
} |
| 986 |
|
| 987 |
/// Map EXP comparison ops |
| 988 |
private bool numCmp(N)(EXP op, N n1, N n2) nothrow |
| 989 |
{ |
| 990 |
switch (op) |
| 991 |
{ |
| 992 |
case EXP.lessThan: |
| 993 |
return n1 < n2; |
| 994 |
case EXP.lessOrEqual: |
| 995 |
return n1 <= n2; |
| 996 |
case EXP.greaterThan: |
| 997 |
return n1 > n2; |
| 998 |
case EXP.greaterOrEqual: |
| 999 |
return n1 >= n2; |
| 1000 |
|
| 1001 |
default: |
| 1002 |
assert(0); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
/// Returns cmp OP 0; where OP is ==, !=, <, >=, etc. Result is 0 or 1 |
| 1007 |
bool specificCmp(EXP op, int rawCmp) @safe nothrow |
| 1008 |
{ |
| 1009 |
return numCmp!int(op, rawCmp, 0); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/// Returns e1 OP e2; where OP is ==, !=, <, >=, etc. Result is 0 or 1 |
| 1013 |
bool intUnsignedCmp(EXP op, dinteger_t n1, dinteger_t n2) @safe nothrow |
| 1014 |
{ |
| 1015 |
return numCmp!dinteger_t(op, n1, n2); |
| 1016 |
} |
| 1017 |
|
| 1018 |
/// Returns e1 OP e2; where OP is ==, !=, <, >=, etc. Result is 0 or 1 |
| 1019 |
bool intSignedCmp(EXP op, sinteger_t n1, sinteger_t n2) @safe nothrow |
| 1020 |
{ |
| 1021 |
return numCmp!sinteger_t(op, n1, n2); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/// Returns e1 OP e2; where OP is ==, !=, <, >=, etc. Result is 0 or 1 |
| 1025 |
bool realCmp(EXP op, real_t r1, real_t r2) @safe nothrow |
| 1026 |
{ |
| 1027 |
// Don't rely on compiler, handle NAN arguments separately |
| 1028 |
if (CTFloat.isNaN(r1) || CTFloat.isNaN(r2)) // if unordered |
| 1029 |
{ |
| 1030 |
switch (op) |
| 1031 |
{ |
| 1032 |
case EXP.lessThan: |
| 1033 |
case EXP.lessOrEqual: |
| 1034 |
case EXP.greaterThan: |
| 1035 |
case EXP.greaterOrEqual: |
| 1036 |
return false; |
| 1037 |
|
| 1038 |
default: |
| 1039 |
assert(0); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
else |
| 1043 |
{ |
| 1044 |
return numCmp!real_t(op, r1, r2); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
/* Conceptually the same as memcmp(e1, e2). |
| 1049 |
* e1 and e2 may be strings, arrayliterals, or slices. |
| 1050 |
* For string types, return <0 if e1 < e2, 0 if e1==e2, >0 if e1 > e2. |
| 1051 |
* For all other types, return 0 if e1 == e2, !=0 if e1 != e2. |
| 1052 |
* Returns: |
| 1053 |
* -1,0,1 |
| 1054 |
*/ |
| 1055 |
private int ctfeCmpArrays(Loc loc, Expression e1, Expression e2, uinteger_t len) |
| 1056 |
{ |
| 1057 |
// Resolve slices, if necessary |
| 1058 |
uinteger_t lo1 = 0; |
| 1059 |
uinteger_t lo2 = 0; |
| 1060 |
|
| 1061 |
Expression x1 = e1; |
| 1062 |
if (auto sle1 = x1.isSliceExp()) |
| 1063 |
{ |
| 1064 |
lo1 = sle1.lwr.toInteger(); |
| 1065 |
x1 = sle1.e1; |
| 1066 |
} |
| 1067 |
auto se1 = x1.isStringExp(); |
| 1068 |
auto ae1 = x1.isArrayLiteralExp(); |
| 1069 |
|
| 1070 |
Expression x2 = e2; |
| 1071 |
if (auto sle2 = x2.isSliceExp()) |
| 1072 |
{ |
| 1073 |
lo2 = sle2.lwr.toInteger(); |
| 1074 |
x2 = sle2.e1; |
| 1075 |
} |
| 1076 |
auto se2 = x2.isStringExp(); |
| 1077 |
auto ae2 = x2.isArrayLiteralExp(); |
| 1078 |
|
| 1079 |
// Now both must be either EXP.arrayLiteral or EXP.string_ |
| 1080 |
if (se1 && se2) |
| 1081 |
return sliceCmpStringWithString(se1, se2, cast(size_t)lo1, cast(size_t)lo2, cast(size_t)len); |
| 1082 |
if (se1 && ae2) |
| 1083 |
return sliceCmpStringWithArray(se1, ae2, cast(size_t)lo1, cast(size_t)lo2, cast(size_t)len); |
| 1084 |
if (se2 && ae1) |
| 1085 |
return -sliceCmpStringWithArray(se2, ae1, cast(size_t)lo2, cast(size_t)lo1, cast(size_t)len); |
| 1086 |
assert(ae1 && ae2); |
| 1087 |
// Comparing two array literals. This case is potentially recursive. |
| 1088 |
// If they aren't strings, we just need an equality check rather than |
| 1089 |
// a full cmp. |
| 1090 |
const bool needCmp = ae1.type.nextOf().isIntegral(); |
| 1091 |
foreach (size_t i; 0 .. cast(size_t)len) |
| 1092 |
{ |
| 1093 |
Expression ee1 = (*ae1.elements)[cast(size_t)(lo1 + i)]; |
| 1094 |
Expression ee2 = (*ae2.elements)[cast(size_t)(lo2 + i)]; |
| 1095 |
if (needCmp) |
| 1096 |
{ |
| 1097 |
const sinteger_t c = ee1.toInteger() - ee2.toInteger(); |
| 1098 |
if (c > 0) |
| 1099 |
return 1; |
| 1100 |
if (c < 0) |
| 1101 |
return -1; |
| 1102 |
} |
| 1103 |
else |
| 1104 |
{ |
| 1105 |
if (ctfeRawCmp(loc, ee1, ee2)) |
| 1106 |
return 1; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
return 0; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* Given a delegate expression e, return .funcptr. |
| 1113 |
* If e is NullExp, return NULL. |
| 1114 |
*/ |
| 1115 |
private FuncDeclaration funcptrOf(Expression e) @safe nothrow |
| 1116 |
{ |
| 1117 |
assert(e.type.ty == Tdelegate); |
| 1118 |
if (auto de = e.isDelegateExp()) |
| 1119 |
return de.func; |
| 1120 |
if (auto fe = e.isFuncExp()) |
| 1121 |
return fe.fd; |
| 1122 |
assert(e.op == EXP.null_); |
| 1123 |
return null; |
| 1124 |
} |
| 1125 |
|
| 1126 |
private bool isArray(const Expression e) @safe nothrow |
| 1127 |
{ |
| 1128 |
return e.op == EXP.arrayLiteral || e.op == EXP.string_ || e.op == EXP.slice || e.op == EXP.null_; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/***** |
| 1132 |
* Params: |
| 1133 |
* loc = source file location |
| 1134 |
* e1 = left operand |
| 1135 |
* e2 = right operand |
| 1136 |
* identity = true for `is` identity comparisons |
| 1137 |
* Returns: |
| 1138 |
* For strings, return <0 if e1 < e2, 0 if e1==e2, >0 if e1 > e2. |
| 1139 |
* For all other types, return 0 if e1 == e2, !=0 if e1 != e2. |
| 1140 |
*/ |
| 1141 |
private int ctfeRawCmp(Loc loc, Expression e1, Expression e2, bool identity = false) |
| 1142 |
{ |
| 1143 |
if (e1.op == EXP.classReference || e2.op == EXP.classReference) |
| 1144 |
{ |
| 1145 |
if (e1.op == EXP.classReference && e2.op == EXP.classReference && |
| 1146 |
e1.isClassReferenceExp().value == e2.isClassReferenceExp().value) |
| 1147 |
return 0; |
| 1148 |
return 1; |
| 1149 |
} |
| 1150 |
if (e1.op == EXP.typeid_ && e2.op == EXP.typeid_) |
| 1151 |
{ |
| 1152 |
// printf("e1: %s\n", e1.toChars()); |
| 1153 |
// printf("e2: %s\n", e2.toChars()); |
| 1154 |
Type t1 = isType(e1.isTypeidExp().obj); |
| 1155 |
Type t2 = isType(e2.isTypeidExp().obj); |
| 1156 |
assert(t1); |
| 1157 |
assert(t2); |
| 1158 |
return t1 != t2; |
| 1159 |
} |
| 1160 |
// null == null, regardless of type |
| 1161 |
if (e1.op == EXP.null_ && e2.op == EXP.null_) |
| 1162 |
return 0; |
| 1163 |
if (e1.type.ty == Tpointer && e2.type.ty == Tpointer) |
| 1164 |
{ |
| 1165 |
// Can only be an equality test. |
| 1166 |
dinteger_t ofs1, ofs2; |
| 1167 |
Expression agg1 = getAggregateFromPointer(e1, &ofs1); |
| 1168 |
Expression agg2 = getAggregateFromPointer(e2, &ofs2); |
| 1169 |
if ((agg1 == agg2) || (agg1.op == EXP.variable && agg2.op == EXP.variable && agg1.isVarExp().var == agg2.isVarExp().var)) |
| 1170 |
{ |
| 1171 |
if (ofs1 == ofs2) |
| 1172 |
return 0; |
| 1173 |
} |
| 1174 |
return 1; |
| 1175 |
} |
| 1176 |
if (e1.type.ty == Tdelegate && e2.type.ty == Tdelegate) |
| 1177 |
{ |
| 1178 |
// If .funcptr isn't the same, they are not equal |
| 1179 |
if (funcptrOf(e1) != funcptrOf(e2)) |
| 1180 |
return 1; |
| 1181 |
// If both are delegate literals, assume they have the |
| 1182 |
// same closure pointer. TODO: We don't support closures yet! |
| 1183 |
if (e1.op == EXP.function_ && e2.op == EXP.function_) |
| 1184 |
return 0; |
| 1185 |
assert(e1.op == EXP.delegate_ && e2.op == EXP.delegate_); |
| 1186 |
// Same .funcptr. Do they have the same .ptr? |
| 1187 |
Expression ptr1 = e1.isDelegateExp().e1; |
| 1188 |
Expression ptr2 = e2.isDelegateExp().e1; |
| 1189 |
dinteger_t ofs1, ofs2; |
| 1190 |
Expression agg1 = getAggregateFromPointer(ptr1, &ofs1); |
| 1191 |
Expression agg2 = getAggregateFromPointer(ptr2, &ofs2); |
| 1192 |
// If they are EXP.variable, it means they are FuncDeclarations |
| 1193 |
if ((agg1 == agg2 && ofs1 == ofs2) || (agg1.op == EXP.variable && agg2.op == EXP.variable && agg1.isVarExp().var == agg2.isVarExp().var)) |
| 1194 |
{ |
| 1195 |
return 0; |
| 1196 |
} |
| 1197 |
return 1; |
| 1198 |
} |
| 1199 |
if (isArray(e1) && isArray(e2)) |
| 1200 |
{ |
| 1201 |
const uinteger_t len1 = resolveArrayLength(e1); |
| 1202 |
const uinteger_t len2 = resolveArrayLength(e2); |
| 1203 |
// workaround for dmc optimizer bug calculating wrong len for |
| 1204 |
// uinteger_t len = (len1 < len2 ? len1 : len2); |
| 1205 |
// if (len == 0) ... |
| 1206 |
if (len1 > 0 && len2 > 0) |
| 1207 |
{ |
| 1208 |
const uinteger_t len = (len1 < len2 ? len1 : len2); |
| 1209 |
const int res = ctfeCmpArrays(loc, e1, e2, len); |
| 1210 |
if (res != 0) |
| 1211 |
return res; |
| 1212 |
} |
| 1213 |
return cast(int)(len1 - len2); |
| 1214 |
} |
| 1215 |
if (e1.type.isIntegral()) |
| 1216 |
{ |
| 1217 |
return e1.toInteger() != e2.toInteger(); |
| 1218 |
} |
| 1219 |
if (identity && e1.type.isFloating()) |
| 1220 |
return !e1.isIdentical(e2); |
| 1221 |
if (e1.type.isReal() || e1.type.isImaginary()) |
| 1222 |
{ |
| 1223 |
real_t r1 = e1.type.isReal() ? e1.toReal() : e1.toImaginary(); |
| 1224 |
real_t r2 = e1.type.isReal() ? e2.toReal() : e2.toImaginary(); |
| 1225 |
if (CTFloat.isNaN(r1) || CTFloat.isNaN(r2)) // if unordered |
| 1226 |
{ |
| 1227 |
return 1; // they are not equal |
| 1228 |
} |
| 1229 |
return (r1 != r2); |
| 1230 |
} |
| 1231 |
else if (e1.type.isComplex()) |
| 1232 |
{ |
| 1233 |
return e1.toComplex() != e2.toComplex(); |
| 1234 |
} |
| 1235 |
if (e1.op == EXP.structLiteral && e2.op == EXP.structLiteral) |
| 1236 |
{ |
| 1237 |
StructLiteralExp es1 = e1.isStructLiteralExp(); |
| 1238 |
StructLiteralExp es2 = e2.isStructLiteralExp(); |
| 1239 |
// For structs, we only need to return 0 or 1 (< and > aren't legal). |
| 1240 |
if (es1.sd != es2.sd) |
| 1241 |
return 1; |
| 1242 |
if ((!es1.elements || !es1.elements.length) && (!es2.elements || !es2.elements.length)) |
| 1243 |
return 0; // both arrays are empty |
| 1244 |
if (!es1.elements || !es2.elements) |
| 1245 |
return 1; |
| 1246 |
if (es1.elements.length != es2.elements.length) |
| 1247 |
return 1; |
| 1248 |
foreach (size_t i; 0 .. es1.elements.length) |
| 1249 |
{ |
| 1250 |
Expression ee1 = (*es1.elements)[i]; |
| 1251 |
Expression ee2 = (*es2.elements)[i]; |
| 1252 |
|
| 1253 |
// https://issues.dlang.org/show_bug.cgi?id=16284 |
| 1254 |
if (ee1.op == EXP.void_ && ee2.op == EXP.void_) // if both are VoidInitExp |
| 1255 |
continue; |
| 1256 |
|
| 1257 |
if (ee1 == ee2) |
| 1258 |
continue; |
| 1259 |
if (!ee1 || !ee2) |
| 1260 |
return 1; |
| 1261 |
const int cmp = ctfeRawCmp(loc, ee1, ee2, identity); |
| 1262 |
if (cmp) |
| 1263 |
return 1; |
| 1264 |
} |
| 1265 |
return 0; // All elements are equal |
| 1266 |
} |
| 1267 |
if (e1.op == EXP.assocArrayLiteral && e2.op == EXP.assocArrayLiteral) |
| 1268 |
{ |
| 1269 |
AssocArrayLiteralExp es1 = e1.isAssocArrayLiteralExp(); |
| 1270 |
AssocArrayLiteralExp es2 = e2.isAssocArrayLiteralExp(); |
| 1271 |
size_t dim = es1.keys.length; |
| 1272 |
if (es2.keys.length != dim) |
| 1273 |
return 1; |
| 1274 |
BitArray used; |
| 1275 |
used.length = dim; |
| 1276 |
foreach (size_t i; 0 .. dim) |
| 1277 |
{ |
| 1278 |
Expression k1 = (*es1.keys)[i]; |
| 1279 |
Expression v1 = (*es1.values)[i]; |
| 1280 |
Expression v2 = null; |
| 1281 |
foreach (size_t j; 0 .. dim) |
| 1282 |
{ |
| 1283 |
if (used[j]) |
| 1284 |
continue; |
| 1285 |
Expression k2 = (*es2.keys)[j]; |
| 1286 |
if (ctfeRawCmp(loc, k1, k2, identity)) |
| 1287 |
continue; |
| 1288 |
used[j] = true; |
| 1289 |
v2 = (*es2.values)[j]; |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
if (!v2 || ctfeRawCmp(loc, v1, v2, identity)) |
| 1293 |
{ |
| 1294 |
return 1; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
else if (e1.op == EXP.assocArrayLiteral && e2.op == EXP.null_) |
| 1300 |
{ |
| 1301 |
return e1.isAssocArrayLiteralExp.keys.length != 0; |
| 1302 |
} |
| 1303 |
else if (e1.op == EXP.null_ && e2.op == EXP.assocArrayLiteral) |
| 1304 |
{ |
| 1305 |
return e2.isAssocArrayLiteralExp.keys.length != 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
error(loc, "CTFE internal error: bad compare of `%s` and `%s`", e1.toChars(), e2.toChars()); |
| 1309 |
assert(0); |
| 1310 |
} |
| 1311 |
|
| 1312 |
/// Evaluate ==, !=. Resolves slices before comparing. Returns 0 or 1 |
| 1313 |
bool ctfeEqual(Loc loc, EXP op, Expression e1, Expression e2) |
| 1314 |
{ |
| 1315 |
return !ctfeRawCmp(loc, e1, e2) ^ (op == EXP.notEqual); |
| 1316 |
} |
| 1317 |
|
| 1318 |
/// Evaluate is, !is. Resolves slices before comparing. Returns 0 or 1 |
| 1319 |
bool ctfeIdentity(Loc loc, EXP op, Expression e1, Expression e2) |
| 1320 |
{ |
| 1321 |
//printf("ctfeIdentity %s %s\n", e1.toChars(), e2.toChars()); |
| 1322 |
//printf("ctfeIdentity op = '%s', e1 = %s %s, e2 = %s %s\n", EXPtoString(op).ptr, |
| 1323 |
// EXPtoString(e1.op).ptr, e1.toChars(), EXPtoString(e2.op).ptr, e1.toChars()); |
| 1324 |
bool cmp; |
| 1325 |
if (e1.op == EXP.null_) |
| 1326 |
{ |
| 1327 |
cmp = (e2.op == EXP.null_); |
| 1328 |
} |
| 1329 |
else if (e2.op == EXP.null_) |
| 1330 |
{ |
| 1331 |
cmp = false; |
| 1332 |
} |
| 1333 |
else if (e1.op == EXP.symbolOffset && e2.op == EXP.symbolOffset) |
| 1334 |
{ |
| 1335 |
SymOffExp es1 = e1.isSymOffExp(); |
| 1336 |
SymOffExp es2 = e2.isSymOffExp(); |
| 1337 |
cmp = (es1.var == es2.var && es1.offset == es2.offset); |
| 1338 |
} |
| 1339 |
else if (e1.type.isFloating()) |
| 1340 |
cmp = e1.isIdentical(e2); |
| 1341 |
else |
| 1342 |
{ |
| 1343 |
cmp = !ctfeRawCmp(loc, e1, e2, true); |
| 1344 |
} |
| 1345 |
if (op == EXP.notIdentity || op == EXP.notEqual) |
| 1346 |
cmp ^= true; |
| 1347 |
return cmp; |
| 1348 |
} |
| 1349 |
|
| 1350 |
/// Evaluate >,<=, etc. Resolves slices before comparing. Returns 0 or 1 |
| 1351 |
bool ctfeCmp(Loc loc, EXP op, Expression e1, Expression e2) |
| 1352 |
{ |
| 1353 |
Type t1 = e1.type.toBasetype(); |
| 1354 |
Type t2 = e2.type.toBasetype(); |
| 1355 |
|
| 1356 |
if (t1.isString() && t2.isString()) |
| 1357 |
return specificCmp(op, ctfeRawCmp(loc, e1, e2)); |
| 1358 |
if (t1.isReal()) |
| 1359 |
return realCmp(op, e1.toReal(), e2.toReal()); |
| 1360 |
if (t1.isImaginary()) |
| 1361 |
return realCmp(op, e1.toImaginary(), e2.toImaginary()); |
| 1362 |
if (t1.isUnsigned() || t2.isUnsigned()) |
| 1363 |
return intUnsignedCmp(op, e1.toInteger(), e2.toInteger()); |
| 1364 |
else |
| 1365 |
return intSignedCmp(op, e1.toInteger(), e2.toInteger()); |
| 1366 |
} |
| 1367 |
|
| 1368 |
UnionExp ctfeCat(Loc loc, Type type, Expression e1, Expression e2) |
| 1369 |
{ |
| 1370 |
Type t1 = e1.type.toBasetype(); |
| 1371 |
Type t2 = e2.type.toBasetype(); |
| 1372 |
UnionExp ue; |
| 1373 |
if (e2.op == EXP.string_ && e1.op == EXP.arrayLiteral && t1.nextOf().isIntegral()) |
| 1374 |
{ |
| 1375 |
// [chars] ~ string => string (only valid for CTFE) |
| 1376 |
StringExp es1 = e2.isStringExp(); |
| 1377 |
ArrayLiteralExp es2 = e1.isArrayLiteralExp(); |
| 1378 |
const len = es1.len + es2.elements.length; |
| 1379 |
const sz = es1.sz; |
| 1380 |
void* s = mem.xmalloc((len + 1) * sz); |
| 1381 |
const data1 = es1.peekData(); |
| 1382 |
memcpy(cast(char*)s + sz * es2.elements.length, data1.ptr, data1.length); |
| 1383 |
foreach (size_t i; 0 .. es2.elements.length) |
| 1384 |
{ |
| 1385 |
Expression es2e = (*es2.elements)[i]; |
| 1386 |
if (es2e.op != EXP.int64) |
| 1387 |
{ |
| 1388 |
emplaceExp!(CTFEExp)(&ue, EXP.cantExpression); |
| 1389 |
return ue; |
| 1390 |
} |
| 1391 |
dinteger_t v = es2e.toInteger(); |
| 1392 |
Port.valcpy(cast(char*)s + i * sz, v, sz); |
| 1393 |
} |
| 1394 |
// Add terminating 0 |
| 1395 |
memset(cast(char*)s + len * sz, 0, sz); |
| 1396 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1397 |
StringExp es = ue.exp().isStringExp(); |
| 1398 |
es.committed = false; |
| 1399 |
es.type = type; |
| 1400 |
return ue; |
| 1401 |
} |
| 1402 |
if (e1.op == EXP.string_ && e2.op == EXP.arrayLiteral && t2.nextOf().isIntegral()) |
| 1403 |
{ |
| 1404 |
// string ~ [chars] => string (only valid for CTFE) |
| 1405 |
// Concatenate the strings |
| 1406 |
StringExp es1 = e1.isStringExp(); |
| 1407 |
ArrayLiteralExp es2 = e2.isArrayLiteralExp(); |
| 1408 |
const len = es1.len + es2.elements.length; |
| 1409 |
const sz = es1.sz; |
| 1410 |
void* s = mem.xmalloc((len + 1) * sz); |
| 1411 |
auto slice = es1.peekData(); |
| 1412 |
memcpy(s, slice.ptr, slice.length); |
| 1413 |
foreach (size_t i; 0 .. es2.elements.length) |
| 1414 |
{ |
| 1415 |
Expression es2e = (*es2.elements)[i]; |
| 1416 |
if (es2e.op != EXP.int64) |
| 1417 |
{ |
| 1418 |
emplaceExp!(CTFEExp)(&ue, EXP.cantExpression); |
| 1419 |
return ue; |
| 1420 |
} |
| 1421 |
const v = es2e.toInteger(); |
| 1422 |
Port.valcpy(cast(char*)s + (es1.len + i) * sz, v, sz); |
| 1423 |
} |
| 1424 |
// Add terminating 0 |
| 1425 |
memset(cast(char*)s + len * sz, 0, sz); |
| 1426 |
emplaceExp!(StringExp)(&ue, loc, s[0 .. len * sz], len, sz); |
| 1427 |
StringExp es = ue.exp().isStringExp(); |
| 1428 |
es.sz = sz; |
| 1429 |
es.committed = false; //es1.committed; |
| 1430 |
es.type = type; |
| 1431 |
return ue; |
| 1432 |
} |
| 1433 |
if (e1.op == EXP.arrayLiteral && e2.op == EXP.arrayLiteral && t1.nextOf().equals(t2.nextOf())) |
| 1434 |
{ |
| 1435 |
// [ e1 ] ~ [ e2 ] ---> [ e1, e2 ] |
| 1436 |
ArrayLiteralExp es1 = e1.isArrayLiteralExp(); |
| 1437 |
ArrayLiteralExp es2 = e2.isArrayLiteralExp(); |
| 1438 |
emplaceExp!(ArrayLiteralExp)(&ue, es1.loc, type, copyLiteralArray(es1.elements)); |
| 1439 |
es1 = ue.exp().isArrayLiteralExp(); |
| 1440 |
es1.elements.insert(es1.elements.length, copyLiteralArray(es2.elements)); |
| 1441 |
return ue; |
| 1442 |
} |
| 1443 |
if (e1.op == EXP.arrayLiteral && e2.op == EXP.null_ && t1.nextOf().equals(t2.nextOf())) |
| 1444 |
{ |
| 1445 |
// [ e1 ] ~ null ----> [ e1 ].dup |
| 1446 |
ue = paintTypeOntoLiteralCopy(type, copyLiteral(e1).copy()); |
| 1447 |
return ue; |
| 1448 |
} |
| 1449 |
if (e1.op == EXP.null_ && e2.op == EXP.arrayLiteral && t1.nextOf().equals(t2.nextOf())) |
| 1450 |
{ |
| 1451 |
// null ~ [ e2 ] ----> [ e2 ].dup |
| 1452 |
ue = paintTypeOntoLiteralCopy(type, copyLiteral(e2).copy()); |
| 1453 |
return ue; |
| 1454 |
} |
| 1455 |
ue = Cat(loc, type, e1, e2); |
| 1456 |
return ue; |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* Given an AA literal 'ae', and a key 'e2': |
| 1460 |
* Return ae[e2] if present, or NULL if not found. |
| 1461 |
*/ |
| 1462 |
Expression findKeyInAA(Loc loc, AssocArrayLiteralExp ae, Expression e2) |
| 1463 |
{ |
| 1464 |
/* Search the keys backwards, in case there are duplicate keys |
| 1465 |
*/ |
| 1466 |
for (size_t i = ae.keys.length; i;) |
| 1467 |
{ |
| 1468 |
--i; |
| 1469 |
Expression ekey = (*ae.keys)[i]; |
| 1470 |
const int eq = ctfeEqual(loc, EXP.equal, ekey, e2); |
| 1471 |
if (eq) |
| 1472 |
{ |
| 1473 |
return (*ae.values)[i]; |
| 1474 |
} |
| 1475 |
} |
| 1476 |
return null; |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* Same as for constfold.Index, except that it only works for static arrays, |
| 1480 |
* dynamic arrays, and strings. We know that e1 is an |
| 1481 |
* interpreted CTFE expression, so it cannot have side-effects. |
| 1482 |
*/ |
| 1483 |
Expression ctfeIndex(UnionExp* pue, Loc loc, Type type, Expression e1, uinteger_t indx) |
| 1484 |
{ |
| 1485 |
//printf("ctfeIndex(e1 = %s)\n", e1.toChars()); |
| 1486 |
assert(e1.type); |
| 1487 |
if (auto es1 = e1.isStringExp()) |
| 1488 |
{ |
| 1489 |
if (indx >= es1.len) |
| 1490 |
{ |
| 1491 |
error(loc, "string index %llu is out of bounds `[0 .. %llu]`", indx, cast(ulong)es1.len); |
| 1492 |
return CTFEExp.cantexp; |
| 1493 |
} |
| 1494 |
emplaceExp!IntegerExp(pue, loc, es1.getIndex(cast(size_t) indx), type); |
| 1495 |
return pue.exp(); |
| 1496 |
} |
| 1497 |
|
| 1498 |
if (auto ale = e1.isArrayLiteralExp()) |
| 1499 |
{ |
| 1500 |
if (indx >= ale.elements.length) |
| 1501 |
{ |
| 1502 |
error(loc, "array index %llu is out of bounds `%s[0 .. %llu]`", indx, e1.toChars(), cast(ulong)ale.elements.length); |
| 1503 |
return CTFEExp.cantexp; |
| 1504 |
} |
| 1505 |
Expression e = (*ale.elements)[cast(size_t)indx]; |
| 1506 |
return paintTypeOntoLiteral(pue, type, e); |
| 1507 |
} |
| 1508 |
|
| 1509 |
assert(0); |
| 1510 |
} |
| 1511 |
|
| 1512 |
Expression ctfeCast(UnionExp* pue, Loc loc, Type type, Type to, Expression e, bool explicitCast = false) |
| 1513 |
{ |
| 1514 |
Expression paint() |
| 1515 |
{ |
| 1516 |
return paintTypeOntoLiteral(pue, to, e); |
| 1517 |
} |
| 1518 |
|
| 1519 |
if (e.op == EXP.null_) |
| 1520 |
return paint(); |
| 1521 |
|
| 1522 |
if (e.op == EXP.classReference) |
| 1523 |
{ |
| 1524 |
// Disallow reinterpreting class casts. Do this by ensuring that |
| 1525 |
// the original class can implicitly convert to the target class. |
| 1526 |
// Also do not check 'alias this' for explicit cast expressions. |
| 1527 |
auto tclass = e.isClassReferenceExp().originalClass().type.isTypeClass(); |
| 1528 |
auto match = explicitCast ? tclass.implicitConvToWithoutAliasThis(to.mutableOf()) |
| 1529 |
: tclass.implicitConvTo(to.mutableOf()); |
| 1530 |
if (match) |
| 1531 |
return paint(); |
| 1532 |
|
| 1533 |
emplaceExp!(NullExp)(pue, loc, to); |
| 1534 |
return pue.exp(); |
| 1535 |
} |
| 1536 |
|
| 1537 |
// Allow TypeInfo type painting |
| 1538 |
if (isTypeInfo_Class(e.type) && e.type.implicitConvTo(to)) |
| 1539 |
return paint(); |
| 1540 |
|
| 1541 |
// Allow casting away const for struct literals |
| 1542 |
if (e.op == EXP.structLiteral && e.type.toBasetype().castMod(0) == to.toBasetype().castMod(0)) |
| 1543 |
return paint(); |
| 1544 |
|
| 1545 |
Expression r; |
| 1546 |
if (e.type.equals(type) && type.equals(to)) |
| 1547 |
{ |
| 1548 |
// necessary not to change e's address for pointer comparisons |
| 1549 |
r = e; |
| 1550 |
} |
| 1551 |
else if (to.toBasetype().ty == Tarray && |
| 1552 |
type.toBasetype().ty == Tarray && |
| 1553 |
to.toBasetype().nextOf().size() == type.toBasetype().nextOf().size()) |
| 1554 |
{ |
| 1555 |
// https://issues.dlang.org/show_bug.cgi?id=12495 |
| 1556 |
// Array reinterpret casts: eg. string to immutable(ubyte)[] |
| 1557 |
return paint(); |
| 1558 |
} |
| 1559 |
else |
| 1560 |
{ |
| 1561 |
*pue = Cast(loc, type, to, e); |
| 1562 |
r = pue.exp(); |
| 1563 |
} |
| 1564 |
|
| 1565 |
if (CTFEExp.isCantExp(r)) |
| 1566 |
error(loc, "cannot cast `%s` to `%s` at compile time", e.toChars(), to.toChars()); |
| 1567 |
|
| 1568 |
if (auto ae = e.isArrayLiteralExp()) |
| 1569 |
ae.ownedByCtfe = OwnedBy.ctfe; |
| 1570 |
|
| 1571 |
if (auto se = e.isStringExp()) |
| 1572 |
se.ownedByCtfe = OwnedBy.ctfe; |
| 1573 |
|
| 1574 |
return r; |
| 1575 |
} |
| 1576 |
|
| 1577 |
/******** Assignment helper functions ***************************/ |
| 1578 |
/* Set dest = src, where both dest and src are container value literals |
| 1579 |
* (ie, struct literals, or static arrays (can be an array literal or a string)) |
| 1580 |
* Assignment is recursively in-place. |
| 1581 |
* Purpose: any reference to a member of 'dest' will remain valid after the |
| 1582 |
* assignment. |
| 1583 |
*/ |
| 1584 |
void assignInPlace(Expression dest, Expression src) |
| 1585 |
{ |
| 1586 |
if (!(dest.op == EXP.structLiteral || dest.op == EXP.arrayLiteral || dest.op == EXP.string_)) |
| 1587 |
{ |
| 1588 |
printf("invalid op %d %d\n", src.op, dest.op); |
| 1589 |
assert(0); |
| 1590 |
} |
| 1591 |
Expressions* oldelems; |
| 1592 |
Expressions* newelems; |
| 1593 |
if (dest.op == EXP.structLiteral) |
| 1594 |
{ |
| 1595 |
assert(dest.op == src.op); |
| 1596 |
oldelems = dest.isStructLiteralExp().elements; |
| 1597 |
newelems = src.isStructLiteralExp().elements; |
| 1598 |
auto sd = dest.isStructLiteralExp().sd; |
| 1599 |
const nfields = sd.nonHiddenFields(); |
| 1600 |
const nvthis = sd.fields.length - nfields; |
| 1601 |
if (nvthis && oldelems.length >= nfields && oldelems.length < newelems.length) |
| 1602 |
foreach (_; 0 .. newelems.length - oldelems.length) |
| 1603 |
oldelems.push(null); |
| 1604 |
} |
| 1605 |
else if (dest.op == EXP.arrayLiteral && src.op == EXP.arrayLiteral) |
| 1606 |
{ |
| 1607 |
oldelems = dest.isArrayLiteralExp().elements; |
| 1608 |
newelems = src.isArrayLiteralExp().elements; |
| 1609 |
} |
| 1610 |
else if (dest.op == EXP.string_ && src.op == EXP.string_) |
| 1611 |
{ |
| 1612 |
sliceAssignStringFromString(dest.isStringExp(), src.isStringExp(), 0); |
| 1613 |
return; |
| 1614 |
} |
| 1615 |
else if (dest.op == EXP.arrayLiteral && src.op == EXP.string_) |
| 1616 |
{ |
| 1617 |
sliceAssignArrayLiteralFromString(dest.isArrayLiteralExp(), src.isStringExp(), 0); |
| 1618 |
return; |
| 1619 |
} |
| 1620 |
else if (src.op == EXP.arrayLiteral && dest.op == EXP.string_) |
| 1621 |
{ |
| 1622 |
sliceAssignStringFromArrayLiteral(dest.isStringExp(), src.isArrayLiteralExp(), 0); |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
else |
| 1626 |
{ |
| 1627 |
printf("invalid op %d %d\n", src.op, dest.op); |
| 1628 |
assert(0); |
| 1629 |
} |
| 1630 |
assert(oldelems.length == newelems.length); |
| 1631 |
foreach (size_t i; 0 .. oldelems.length) |
| 1632 |
{ |
| 1633 |
Expression e = (*newelems)[i]; |
| 1634 |
Expression o = (*oldelems)[i]; |
| 1635 |
if (e.op == EXP.structLiteral) |
| 1636 |
{ |
| 1637 |
assert(o.op == e.op); |
| 1638 |
assignInPlace(o, e); |
| 1639 |
} |
| 1640 |
else if (e.type.ty == Tsarray && e.op != EXP.void_ && o.type.ty == Tsarray) |
| 1641 |
{ |
| 1642 |
assignInPlace(o, e); |
| 1643 |
} |
| 1644 |
else |
| 1645 |
{ |
| 1646 |
(*oldelems)[i] = (*newelems)[i]; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
// Given an AA literal aae, set aae[index] = newval and return newval. |
| 1652 |
Expression assignAssocArrayElement(Loc loc, AssocArrayLiteralExp aae, Expression index, Expression newval) |
| 1653 |
{ |
| 1654 |
/* Create new associative array literal reflecting updated key/value |
| 1655 |
*/ |
| 1656 |
Expressions* keysx = aae.keys; |
| 1657 |
Expressions* valuesx = aae.values; |
| 1658 |
int updated = 0; |
| 1659 |
for (size_t j = valuesx.length; j;) |
| 1660 |
{ |
| 1661 |
j--; |
| 1662 |
Expression ekey = (*aae.keys)[j]; |
| 1663 |
int eq = ctfeEqual(loc, EXP.equal, ekey, index); |
| 1664 |
if (eq) |
| 1665 |
{ |
| 1666 |
(*valuesx)[j] = newval; |
| 1667 |
updated = 1; |
| 1668 |
} |
| 1669 |
} |
| 1670 |
if (!updated) |
| 1671 |
{ |
| 1672 |
// Append index/newval to keysx[]/valuesx[] |
| 1673 |
valuesx.push(newval); |
| 1674 |
keysx.push(index); |
| 1675 |
} |
| 1676 |
return newval; |
| 1677 |
} |
| 1678 |
|
| 1679 |
/// Given array literal oldval of type ArrayLiteralExp or StringExp, of length |
| 1680 |
/// oldlen, change its length to newlen. If the newlen is longer than oldlen, |
| 1681 |
/// all new elements will be set to the default initializer for the element type. |
| 1682 |
Expression changeArrayLiteralLength(UnionExp* pue, Loc loc, TypeArray arrayType, Expression oldval, size_t oldlen, size_t newlen) |
| 1683 |
{ |
| 1684 |
Type elemType = arrayType.next; |
| 1685 |
assert(elemType); |
| 1686 |
Expression defaultElem = elemType.defaultInitLiteral(loc); |
| 1687 |
auto elements = new Expressions(newlen); |
| 1688 |
// Resolve slices |
| 1689 |
size_t indxlo = 0; |
| 1690 |
if (oldval.op == EXP.slice) |
| 1691 |
{ |
| 1692 |
indxlo = cast(size_t)oldval.isSliceExp().lwr.toInteger(); |
| 1693 |
oldval = oldval.isSliceExp().e1; |
| 1694 |
} |
| 1695 |
size_t copylen = oldlen < newlen ? oldlen : newlen; |
| 1696 |
if (oldval.op == EXP.string_) |
| 1697 |
{ |
| 1698 |
StringExp oldse = oldval.isStringExp(); |
| 1699 |
void* s = mem.xcalloc(newlen + 1, oldse.sz); |
| 1700 |
const data = oldse.peekData(); |
| 1701 |
memcpy(s, data.ptr, copylen * oldse.sz); |
| 1702 |
const defaultValue = cast(ulong)defaultElem.toInteger(); |
| 1703 |
foreach (size_t elemi; copylen .. newlen) |
| 1704 |
{ |
| 1705 |
switch (oldse.sz) |
| 1706 |
{ |
| 1707 |
case 1: |
| 1708 |
(cast(char*)s)[cast(size_t)(indxlo + elemi)] = cast(char)defaultValue; |
| 1709 |
break; |
| 1710 |
case 2: |
| 1711 |
(cast(wchar*)s)[cast(size_t)(indxlo + elemi)] = cast(wchar)defaultValue; |
| 1712 |
break; |
| 1713 |
case 4: |
| 1714 |
(cast(dchar*)s)[cast(size_t)(indxlo + elemi)] = cast(dchar)defaultValue; |
| 1715 |
break; |
| 1716 |
case 8: |
| 1717 |
(cast(ulong*)s)[cast(size_t)(indxlo + elemi)] = cast(ulong)defaultValue; |
| 1718 |
break; |
| 1719 |
default: |
| 1720 |
assert(0); |
| 1721 |
} |
| 1722 |
} |
| 1723 |
emplaceExp!(StringExp)(pue, loc, s[0 .. newlen * oldse.sz], newlen, oldse.sz); |
| 1724 |
StringExp se = pue.exp().isStringExp(); |
| 1725 |
se.type = arrayType; |
| 1726 |
se.sz = oldse.sz; |
| 1727 |
se.committed = oldse.committed; |
| 1728 |
se.ownedByCtfe = OwnedBy.ctfe; |
| 1729 |
} |
| 1730 |
else |
| 1731 |
{ |
| 1732 |
if (oldlen != 0) |
| 1733 |
{ |
| 1734 |
assert(oldval.op == EXP.arrayLiteral); |
| 1735 |
ArrayLiteralExp ae = oldval.isArrayLiteralExp(); |
| 1736 |
foreach (size_t i; 0 .. copylen) |
| 1737 |
(*elements)[i] = (*ae.elements)[indxlo + i]; |
| 1738 |
} |
| 1739 |
if (elemType.ty == Tstruct || elemType.ty == Tsarray) |
| 1740 |
{ |
| 1741 |
/* If it is an aggregate literal representing a value type, |
| 1742 |
* we need to create a unique copy for each element |
| 1743 |
*/ |
| 1744 |
foreach (size_t i; copylen .. newlen) |
| 1745 |
(*elements)[i] = copyLiteral(defaultElem).copy(); |
| 1746 |
} |
| 1747 |
else |
| 1748 |
{ |
| 1749 |
foreach (size_t i; copylen .. newlen) |
| 1750 |
(*elements)[i] = defaultElem; |
| 1751 |
} |
| 1752 |
emplaceExp!(ArrayLiteralExp)(pue, loc, arrayType, elements); |
| 1753 |
ArrayLiteralExp aae = pue.exp().isArrayLiteralExp(); |
| 1754 |
aae.ownedByCtfe = OwnedBy.ctfe; |
| 1755 |
} |
| 1756 |
return pue.exp(); |
| 1757 |
} |
| 1758 |
|
| 1759 |
/*************************** CTFE Sanity Checks ***************************/ |
| 1760 |
|
| 1761 |
bool isCtfeValueValid(Expression newval) |
| 1762 |
{ |
| 1763 |
Type tb = newval.type.toBasetype(); |
| 1764 |
switch (newval.op) |
| 1765 |
{ |
| 1766 |
case EXP.int64: |
| 1767 |
case EXP.float64: |
| 1768 |
case EXP.complex80: |
| 1769 |
return tb.isScalar(); |
| 1770 |
|
| 1771 |
case EXP.null_: |
| 1772 |
return tb.ty == Tnull || |
| 1773 |
tb.ty == Tpointer || |
| 1774 |
tb.ty == Tarray || |
| 1775 |
tb.ty == Taarray || |
| 1776 |
tb.ty == Tclass || |
| 1777 |
tb.ty == Tdelegate; |
| 1778 |
|
| 1779 |
case EXP.string_: |
| 1780 |
return true; // CTFE would directly use the StringExp in AST. |
| 1781 |
|
| 1782 |
case EXP.arrayLiteral: |
| 1783 |
return true; //((ArrayLiteralExp *)newval)->ownedByCtfe; |
| 1784 |
|
| 1785 |
case EXP.assocArrayLiteral: |
| 1786 |
return true; //((AssocArrayLiteralExp *)newval)->ownedByCtfe; |
| 1787 |
|
| 1788 |
case EXP.structLiteral: |
| 1789 |
return true; //((StructLiteralExp *)newval)->ownedByCtfe; |
| 1790 |
|
| 1791 |
case EXP.classReference: |
| 1792 |
return true; |
| 1793 |
|
| 1794 |
case EXP.type: |
| 1795 |
return true; |
| 1796 |
|
| 1797 |
case EXP.vector: |
| 1798 |
return true; // vector literal |
| 1799 |
|
| 1800 |
case EXP.function_: |
| 1801 |
return true; // function literal or delegate literal |
| 1802 |
|
| 1803 |
case EXP.delegate_: |
| 1804 |
{ |
| 1805 |
// &struct.func or &clasinst.func |
| 1806 |
// &nestedfunc |
| 1807 |
Expression ethis = newval.isDelegateExp().e1; |
| 1808 |
return (ethis.op == EXP.structLiteral || ethis.op == EXP.classReference || ethis.op == EXP.variable && ethis.isVarExp().var == newval.isDelegateExp().func); |
| 1809 |
} |
| 1810 |
|
| 1811 |
case EXP.symbolOffset: |
| 1812 |
{ |
| 1813 |
// function pointer, or pointer to static variable |
| 1814 |
Declaration d = newval.isSymOffExp().var; |
| 1815 |
return d.isFuncDeclaration() || d.isDataseg(); |
| 1816 |
} |
| 1817 |
|
| 1818 |
case EXP.typeid_: |
| 1819 |
{ |
| 1820 |
// always valid |
| 1821 |
return true; |
| 1822 |
} |
| 1823 |
|
| 1824 |
case EXP.address: |
| 1825 |
{ |
| 1826 |
// e1 should be a CTFE reference |
| 1827 |
Expression e1 = newval.isAddrExp().e1; |
| 1828 |
return tb.ty == Tpointer && |
| 1829 |
( |
| 1830 |
(e1.op == EXP.structLiteral || e1.op == EXP.arrayLiteral) && isCtfeValueValid(e1) || |
| 1831 |
e1.op == EXP.variable || |
| 1832 |
e1.op == EXP.dotVariable && isCtfeReferenceValid(e1) || |
| 1833 |
e1.op == EXP.index && isCtfeReferenceValid(e1) || |
| 1834 |
e1.op == EXP.slice && e1.type.toBasetype().ty == Tsarray |
| 1835 |
); |
| 1836 |
} |
| 1837 |
|
| 1838 |
case EXP.slice: |
| 1839 |
{ |
| 1840 |
// e1 should be an array aggregate |
| 1841 |
const SliceExp se = newval.isSliceExp(); |
| 1842 |
assert(se.lwr && se.lwr.op == EXP.int64); |
| 1843 |
assert(se.upr && se.upr.op == EXP.int64); |
| 1844 |
return tb.isStaticOrDynamicArray() && (se.e1.op == EXP.string_ || se.e1.op == EXP.arrayLiteral); |
| 1845 |
} |
| 1846 |
|
| 1847 |
case EXP.void_: |
| 1848 |
return true; // uninitialized value |
| 1849 |
|
| 1850 |
default: |
| 1851 |
error(newval.loc, "CTFE internal error: illegal CTFE value `%s`", newval.toChars()); |
| 1852 |
return false; |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
bool isCtfeReferenceValid(Expression newval) |
| 1857 |
{ |
| 1858 |
switch (newval.op) |
| 1859 |
{ |
| 1860 |
case EXP.this_: |
| 1861 |
return true; |
| 1862 |
|
| 1863 |
case EXP.variable: |
| 1864 |
{ |
| 1865 |
const VarDeclaration v = newval.isVarExp().var.isVarDeclaration(); |
| 1866 |
assert(v); |
| 1867 |
// Must not be a reference to a reference |
| 1868 |
return true; |
| 1869 |
} |
| 1870 |
|
| 1871 |
case EXP.index: |
| 1872 |
{ |
| 1873 |
const Expression eagg = newval.isIndexExp().e1; |
| 1874 |
return eagg.op == EXP.string_ || eagg.op == EXP.arrayLiteral || eagg.op == EXP.assocArrayLiteral; |
| 1875 |
} |
| 1876 |
|
| 1877 |
case EXP.dotVariable: |
| 1878 |
{ |
| 1879 |
Expression eagg = newval.isDotVarExp().e1; |
| 1880 |
return (eagg.op == EXP.structLiteral || eagg.op == EXP.classReference) && isCtfeValueValid(eagg); |
| 1881 |
} |
| 1882 |
|
| 1883 |
default: |
| 1884 |
// Internally a ref variable may directly point a stack memory. |
| 1885 |
// e.g. ref int v = 1; |
| 1886 |
return isCtfeValueValid(newval); |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
// Used for debugging only |
| 1891 |
void showCtfeExpr(Expression e, int level = 0) |
| 1892 |
{ |
| 1893 |
for (int i = level; i > 0; --i) |
| 1894 |
printf(" "); |
| 1895 |
Expressions* elements = null; |
| 1896 |
// We need the struct definition to detect block assignment |
| 1897 |
StructDeclaration sd = null; |
| 1898 |
ClassDeclaration cd = null; |
| 1899 |
if (e.op == EXP.structLiteral) |
| 1900 |
{ |
| 1901 |
elements = e.isStructLiteralExp().elements; |
| 1902 |
sd = e.isStructLiteralExp().sd; |
| 1903 |
printf("STRUCT type = %s %p:\n", e.type.toChars(), e); |
| 1904 |
} |
| 1905 |
else if (e.op == EXP.classReference) |
| 1906 |
{ |
| 1907 |
elements = e.isClassReferenceExp().value.elements; |
| 1908 |
cd = e.isClassReferenceExp().originalClass(); |
| 1909 |
printf("CLASS type = %s %p:\n", e.type.toChars(), e.isClassReferenceExp().value); |
| 1910 |
} |
| 1911 |
else if (e.op == EXP.arrayLiteral) |
| 1912 |
{ |
| 1913 |
elements = e.isArrayLiteralExp().elements; |
| 1914 |
printf("ARRAY LITERAL type=%s %p:\n", e.type.toChars(), e); |
| 1915 |
} |
| 1916 |
else if (e.op == EXP.assocArrayLiteral) |
| 1917 |
{ |
| 1918 |
printf("AA LITERAL type=%s %p:\n", e.type.toChars(), e); |
| 1919 |
} |
| 1920 |
else if (e.op == EXP.string_) |
| 1921 |
{ |
| 1922 |
printf("STRING %s %p\n", e.toChars(), e.isStringExp.peekString.ptr); |
| 1923 |
} |
| 1924 |
else if (e.op == EXP.slice) |
| 1925 |
{ |
| 1926 |
printf("SLICE %p: %s\n", e, e.toChars()); |
| 1927 |
showCtfeExpr(e.isSliceExp().e1, level + 1); |
| 1928 |
} |
| 1929 |
else if (e.op == EXP.variable) |
| 1930 |
{ |
| 1931 |
printf("VAR %p %s\n", e, e.toChars()); |
| 1932 |
VarDeclaration v = e.isVarExp().var.isVarDeclaration(); |
| 1933 |
if (v && getValue(v)) |
| 1934 |
showCtfeExpr(getValue(v), level + 1); |
| 1935 |
} |
| 1936 |
else if (e.op == EXP.address) |
| 1937 |
{ |
| 1938 |
// This is potentially recursive. We mustn't try to print the thing we're pointing to. |
| 1939 |
printf("POINTER %p to %p: %s\n", e, e.isAddrExp().e1, e.toChars()); |
| 1940 |
} |
| 1941 |
else |
| 1942 |
printf("VALUE %p: %s\n", e, e.toChars()); |
| 1943 |
if (elements) |
| 1944 |
{ |
| 1945 |
size_t fieldsSoFar = 0; |
| 1946 |
for (size_t i = 0; i < elements.length; i++) |
| 1947 |
{ |
| 1948 |
Expression z = null; |
| 1949 |
VarDeclaration v = null; |
| 1950 |
if (i > 15) |
| 1951 |
{ |
| 1952 |
printf("...(total %d elements)\n", cast(int)elements.length); |
| 1953 |
return; |
| 1954 |
} |
| 1955 |
if (sd) |
| 1956 |
{ |
| 1957 |
v = sd.fields[i]; |
| 1958 |
z = (*elements)[i]; |
| 1959 |
} |
| 1960 |
else if (cd) |
| 1961 |
{ |
| 1962 |
while (i - fieldsSoFar >= cd.fields.length) |
| 1963 |
{ |
| 1964 |
fieldsSoFar += cd.fields.length; |
| 1965 |
cd = cd.baseClass; |
| 1966 |
for (int j = level; j > 0; --j) |
| 1967 |
printf(" "); |
| 1968 |
printf(" BASE CLASS: %s\n", cd.toChars()); |
| 1969 |
} |
| 1970 |
v = cd.fields[i - fieldsSoFar]; |
| 1971 |
assert((elements.length + i) >= (fieldsSoFar + cd.fields.length)); |
| 1972 |
size_t indx = (elements.length - fieldsSoFar) - cd.fields.length + i; |
| 1973 |
assert(indx < elements.length); |
| 1974 |
z = (*elements)[indx]; |
| 1975 |
} |
| 1976 |
if (!z) |
| 1977 |
{ |
| 1978 |
for (int j = level; j > 0; --j) |
| 1979 |
printf(" "); |
| 1980 |
printf(" void\n"); |
| 1981 |
continue; |
| 1982 |
} |
| 1983 |
if (v) |
| 1984 |
{ |
| 1985 |
// If it is a void assignment, use the default initializer |
| 1986 |
if ((v.type.ty != z.type.ty) && v.type.ty == Tsarray) |
| 1987 |
{ |
| 1988 |
for (int j = level; --j;) |
| 1989 |
printf(" "); |
| 1990 |
printf(" field: block initialized static array\n"); |
| 1991 |
continue; |
| 1992 |
} |
| 1993 |
} |
| 1994 |
showCtfeExpr(z, level + 1); |
| 1995 |
} |
| 1996 |
} |
| 1997 |
} |
| 1998 |
|
| 1999 |
/*************************** Void initialization ***************************/ |
| 2000 |
UnionExp voidInitLiteral(Type t, VarDeclaration var) |
| 2001 |
{ |
| 2002 |
UnionExp ue; |
| 2003 |
if (auto tsa = t.isTypeSArray()) |
| 2004 |
{ |
| 2005 |
Expression elem = voidInitLiteral(tsa.next, var).copy(); |
| 2006 |
// For aggregate value types (structs, static arrays) we must |
| 2007 |
// create an a separate copy for each element. |
| 2008 |
const mustCopy = (elem.op == EXP.arrayLiteral || elem.op == EXP.structLiteral); |
| 2009 |
const d = cast(size_t)tsa.dim.toInteger(); |
| 2010 |
auto elements = new Expressions(d); |
| 2011 |
foreach (i; 0 .. d) |
| 2012 |
{ |
| 2013 |
if (mustCopy && i > 0) |
| 2014 |
elem = copyLiteral(elem).copy(); |
| 2015 |
(*elements)[i] = elem; |
| 2016 |
} |
| 2017 |
emplaceExp!(ArrayLiteralExp)(&ue, var.loc, tsa, elements); |
| 2018 |
ArrayLiteralExp ae = ue.exp().isArrayLiteralExp(); |
| 2019 |
ae.ownedByCtfe = OwnedBy.ctfe; |
| 2020 |
} |
| 2021 |
else if (auto ts = t.isTypeStruct()) |
| 2022 |
{ |
| 2023 |
auto exps = new Expressions(ts.sym.fields.length); |
| 2024 |
foreach (size_t i; 0 .. ts.sym.fields.length) |
| 2025 |
{ |
| 2026 |
(*exps)[i] = voidInitLiteral(ts.sym.fields[i].type, ts.sym.fields[i]).copy(); |
| 2027 |
} |
| 2028 |
emplaceExp!(StructLiteralExp)(&ue, var.loc, ts.sym, exps); |
| 2029 |
StructLiteralExp se = ue.exp().isStructLiteralExp(); |
| 2030 |
se.type = ts; |
| 2031 |
se.ownedByCtfe = OwnedBy.ctfe; |
| 2032 |
} |
| 2033 |
else |
| 2034 |
emplaceExp!(VoidInitExp)(&ue, var); |
| 2035 |
return ue; |
| 2036 |
} |