| 1 |
/** |
| 2 |
* Semantic analysis of expressions. |
| 3 |
* |
| 4 |
* Specification: ($LINK2 https://dlang.org/spec/expression.html, Expressions) |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/expressionsem.d, _expressionsem.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_expressionsem.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/expressionsem.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.expressionsem; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
|
| 18 |
import dmd.access; |
| 19 |
import dmd.aggregate; |
| 20 |
import dmd.aliasthis; |
| 21 |
import dmd.arrayop; |
| 22 |
import dmd.arraytypes; |
| 23 |
import dmd.attrib; |
| 24 |
import dmd.astcodegen; |
| 25 |
import dmd.astenums; |
| 26 |
import dmd.canthrow; |
| 27 |
import dmd.chkformat; |
| 28 |
import dmd.cond; |
| 29 |
import dmd.ctorflow; |
| 30 |
import dmd.dscope; |
| 31 |
import dmd.dsymbol; |
| 32 |
import dmd.declaration; |
| 33 |
import dmd.dclass; |
| 34 |
import dmd.dcast; |
| 35 |
import dmd.delegatize; |
| 36 |
import dmd.denum; |
| 37 |
import dmd.deps; |
| 38 |
import dmd.dimport; |
| 39 |
import dmd.dinterpret; |
| 40 |
import dmd.dmodule; |
| 41 |
import dmd.dstruct; |
| 42 |
import dmd.dsymbolsem; |
| 43 |
import dmd.dtemplate; |
| 44 |
import dmd.errors; |
| 45 |
import dmd.errorsink; |
| 46 |
import dmd.enumsem; |
| 47 |
import dmd.escape; |
| 48 |
import dmd.expression; |
| 49 |
import dmd.file_manager; |
| 50 |
import dmd.func; |
| 51 |
import dmd.funcsem; |
| 52 |
import dmd.globals; |
| 53 |
import dmd.hdrgen; |
| 54 |
import dmd.id; |
| 55 |
import dmd.identifier; |
| 56 |
import dmd.imphint; |
| 57 |
import dmd.importc; |
| 58 |
import dmd.init; |
| 59 |
import dmd.initsem; |
| 60 |
import dmd.inline; |
| 61 |
import dmd.intrange; |
| 62 |
import dmd.location; |
| 63 |
import dmd.mangle; |
| 64 |
import dmd.mtype; |
| 65 |
import dmd.mustuse; |
| 66 |
import dmd.nspace; |
| 67 |
import dmd.nogc; |
| 68 |
import dmd.objc; |
| 69 |
import dmd.opover; |
| 70 |
import dmd.optimize; |
| 71 |
import dmd.parse; |
| 72 |
import dmd.printast; |
| 73 |
import dmd.root.array; |
| 74 |
import dmd.root.ctfloat; |
| 75 |
import dmd.root.filename; |
| 76 |
import dmd.common.outbuffer; |
| 77 |
import dmd.rootobject; |
| 78 |
import dmd.root.string; |
| 79 |
import dmd.root.utf; |
| 80 |
import dmd.semantic2; |
| 81 |
import dmd.semantic3; |
| 82 |
import dmd.sideeffect; |
| 83 |
import dmd.safe; |
| 84 |
import dmd.target; |
| 85 |
import dmd.templatesem : matchWithInstance; |
| 86 |
import dmd.tokens; |
| 87 |
import dmd.traits; |
| 88 |
import dmd.typesem; |
| 89 |
import dmd.typinf; |
| 90 |
import dmd.utils; |
| 91 |
import dmd.utils : arrayCastBigEndian; |
| 92 |
import dmd.visitor; |
| 93 |
import dmd.visitor.postorder; |
| 94 |
|
| 95 |
enum LOGSEMANTIC = false; |
| 96 |
|
| 97 |
/*********************************** |
| 98 |
* Determine if a `this` is needed to access `d`. |
| 99 |
* Params: |
| 100 |
* sc = context |
| 101 |
* d = declaration to check |
| 102 |
* Returns: |
| 103 |
* true means a `this` is needed |
| 104 |
*/ |
| 105 |
private bool isNeedThisScope(Scope* sc, Declaration d) |
| 106 |
{ |
| 107 |
if (sc.intypeof == 1) |
| 108 |
return false; |
| 109 |
|
| 110 |
AggregateDeclaration ad = d.isThis(); |
| 111 |
if (!ad) |
| 112 |
return false; |
| 113 |
//printf("d = %s, ad = %s\n", d.toChars(), ad.toChars()); |
| 114 |
|
| 115 |
for (Dsymbol s = sc.parent; s; s = s.toParentLocal()) |
| 116 |
{ |
| 117 |
//printf("\ts = %s %s, toParent2() = %p\n", s.kind(), s.toChars(), s.toParent2()); |
| 118 |
if (AggregateDeclaration ad2 = s.isAggregateDeclaration()) |
| 119 |
{ |
| 120 |
if (ad2 == ad) |
| 121 |
return false; |
| 122 |
if (ad2.isNested()) |
| 123 |
continue; |
| 124 |
return true; |
| 125 |
} |
| 126 |
if (FuncDeclaration f = s.isFuncDeclaration()) |
| 127 |
{ |
| 128 |
if (f.isMemberLocal()) |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/******************************************************** |
| 137 |
* Perform semantic analysis and CTFE on expressions to produce |
| 138 |
* a string. |
| 139 |
* Params: |
| 140 |
* buf = append generated string to buffer |
| 141 |
* sc = context |
| 142 |
* exps = array of Expressions |
| 143 |
* loc = location of the pragma / mixin where this conversion was requested, for supplemental error |
| 144 |
* fmt = format string for supplemental error. May contain 1 `%s` which prints the faulty expression |
| 145 |
* expandTuples = whether tuples should be expanded rather than printed as tuple syntax |
| 146 |
* Returns: |
| 147 |
* true on error |
| 148 |
*/ |
| 149 |
bool expressionsToString(ref OutBuffer buf, Scope* sc, Expressions* exps, |
| 150 |
Loc loc, const(char)* fmt, bool expandTuples) |
| 151 |
{ |
| 152 |
if (!exps) |
| 153 |
return false; |
| 154 |
|
| 155 |
foreach (ex; *exps) |
| 156 |
{ |
| 157 |
bool error() |
| 158 |
{ |
| 159 |
if (loc != Loc.initial && fmt) |
| 160 |
errorSupplemental(loc, fmt, ex.toChars()); |
| 161 |
return true; |
| 162 |
} |
| 163 |
if (!ex) |
| 164 |
continue; |
| 165 |
auto sc2 = sc.startCTFE(); |
| 166 |
sc2.tinst = null; |
| 167 |
sc2.minst = null; // prevents emission of any instantiated templates to object file |
| 168 |
auto e2 = ex.expressionSemantic(sc2); |
| 169 |
auto e3 = resolveProperties(sc2, e2); |
| 170 |
sc2.endCTFE(); |
| 171 |
|
| 172 |
// allowed to contain types as well as expressions |
| 173 |
auto e4 = ctfeInterpretForPragmaMsg(e3); |
| 174 |
if (!e4 || e4.op == EXP.error) |
| 175 |
return error(); |
| 176 |
|
| 177 |
// expand tuple |
| 178 |
if (expandTuples) |
| 179 |
if (auto te = e4.isTupleExp()) |
| 180 |
{ |
| 181 |
if (expressionsToString(buf, sc, te.exps, loc, fmt, true)) |
| 182 |
return error(); |
| 183 |
continue; |
| 184 |
} |
| 185 |
// char literals exp `.toStringExp` return `null` but we cant override it |
| 186 |
// because in most contexts we don't want the conversion to succeed. |
| 187 |
IntegerExp ie = e4.isIntegerExp(); |
| 188 |
const ty = (ie && ie.type) ? ie.type.ty : Terror; |
| 189 |
if (ty.isSomeChar) |
| 190 |
{ |
| 191 |
auto tsa = new TypeSArray(ie.type, IntegerExp.literal!1); |
| 192 |
e4 = new ArrayLiteralExp(ex.loc, tsa, ie); |
| 193 |
} |
| 194 |
|
| 195 |
StringExp se = e4.toStringExp(); |
| 196 |
|
| 197 |
if (se && se.type.nextOf().ty.isSomeChar) |
| 198 |
buf.writestring(se.toUTF8(sc).peekString()); |
| 199 |
else if (!(se && se.len == 0)) // don't print empty array literal `[]` |
| 200 |
buf.writestring(e4.toString()); |
| 201 |
} |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
/***************************************** |
| 206 |
* Determine if `this` is available by walking up the enclosing |
| 207 |
* scopes until a function is found. |
| 208 |
* |
| 209 |
* Params: |
| 210 |
* sc = where to start looking for the enclosing function |
| 211 |
* Returns: |
| 212 |
* Found function if it satisfies `isThis()`, otherwise `null` |
| 213 |
*/ |
| 214 |
FuncDeclaration hasThis(Scope* sc) |
| 215 |
{ |
| 216 |
//printf("hasThis()\n"); |
| 217 |
Dsymbol p = sc.parent; |
| 218 |
while (p && p.isTemplateMixin()) |
| 219 |
p = p.parent; |
| 220 |
FuncDeclaration fdthis = p ? p.isFuncDeclaration() : null; |
| 221 |
//printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis.toChars() : ""); |
| 222 |
|
| 223 |
// Go upwards until we find the enclosing member function |
| 224 |
FuncDeclaration fd = fdthis; |
| 225 |
while (1) |
| 226 |
{ |
| 227 |
if (!fd) |
| 228 |
{ |
| 229 |
return null; |
| 230 |
} |
| 231 |
if (!fd.isNested() || fd.isThis() || (fd.hasDualContext() && fd.isMember2())) |
| 232 |
break; |
| 233 |
|
| 234 |
Dsymbol parent = fd.parent; |
| 235 |
while (1) |
| 236 |
{ |
| 237 |
if (!parent) |
| 238 |
return null; |
| 239 |
TemplateInstance ti = parent.isTemplateInstance(); |
| 240 |
if (ti) |
| 241 |
parent = ti.parent; |
| 242 |
else |
| 243 |
break; |
| 244 |
} |
| 245 |
fd = parent.isFuncDeclaration(); |
| 246 |
} |
| 247 |
|
| 248 |
if (!fd.isThis() && !(fd.hasDualContext() && fd.isMember2())) |
| 249 |
{ |
| 250 |
return null; |
| 251 |
} |
| 252 |
|
| 253 |
assert(fd.vthis); |
| 254 |
return fd; |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
extern (D) bool findTempDecl(DotTemplateInstanceExp exp, Scope* sc) |
| 259 |
{ |
| 260 |
auto ti = exp.ti; |
| 261 |
auto e1 = exp.e1; |
| 262 |
static if (LOGSEMANTIC) |
| 263 |
{ |
| 264 |
printf("DotTemplateInstanceExp::findTempDecl('%s')\n", exp.toChars()); |
| 265 |
} |
| 266 |
if (ti.tempdecl) |
| 267 |
return true; |
| 268 |
|
| 269 |
Expression e = new DotIdExp(exp.loc, e1, ti.name); |
| 270 |
e = e.expressionSemantic(sc); |
| 271 |
if (e.op == EXP.dot) |
| 272 |
e = (cast(DotExp)e).e2; |
| 273 |
|
| 274 |
Dsymbol s = null; |
| 275 |
switch (e.op) |
| 276 |
{ |
| 277 |
case EXP.overloadSet: |
| 278 |
s = (cast(OverExp)e).vars; |
| 279 |
break; |
| 280 |
|
| 281 |
case EXP.dotTemplateDeclaration: |
| 282 |
s = (cast(DotTemplateExp)e).td; |
| 283 |
break; |
| 284 |
|
| 285 |
case EXP.scope_: |
| 286 |
s = (cast(ScopeExp)e).sds; |
| 287 |
break; |
| 288 |
|
| 289 |
case EXP.dotVariable: |
| 290 |
s = (cast(DotVarExp)e).var; |
| 291 |
break; |
| 292 |
|
| 293 |
case EXP.variable: |
| 294 |
s = (cast(VarExp)e).var; |
| 295 |
break; |
| 296 |
|
| 297 |
default: |
| 298 |
return false; |
| 299 |
} |
| 300 |
return ti.updateTempDecl(sc, s); |
| 301 |
} |
| 302 |
|
| 303 |
/*********************************************************** |
| 304 |
* Resolve `exp` as a compile-time known string. |
| 305 |
* Params: |
| 306 |
* sc = scope |
| 307 |
* exp = Expression which expected as a string |
| 308 |
* s = What the string is expected for, will be used in error diagnostic. |
| 309 |
* Returns: |
| 310 |
* String literal, or `null` if error happens. |
| 311 |
*/ |
| 312 |
StringExp semanticString(Scope* sc, Expression exp, const char* s) |
| 313 |
{ |
| 314 |
sc = sc.startCTFE(); |
| 315 |
exp = exp.expressionSemantic(sc); |
| 316 |
exp = resolveProperties(sc, exp); |
| 317 |
sc = sc.endCTFE(); |
| 318 |
|
| 319 |
if (exp.op == EXP.error) |
| 320 |
return null; |
| 321 |
|
| 322 |
auto e = exp; |
| 323 |
if (exp.type.isString()) |
| 324 |
{ |
| 325 |
e = e.ctfeInterpret(); |
| 326 |
if (e.op == EXP.error) |
| 327 |
return null; |
| 328 |
} |
| 329 |
|
| 330 |
if (auto se = e.toStringExp()) |
| 331 |
return se; |
| 332 |
error(exp.loc, "`string` expected for %s, not `(%s)` of type `%s`", |
| 333 |
s, exp.toChars(), exp.type.toChars()); |
| 334 |
return null; |
| 335 |
} |
| 336 |
|
| 337 |
/**************************************** |
| 338 |
* Convert string to char[]. |
| 339 |
*/ |
| 340 |
StringExp toUTF8(StringExp se, Scope* sc) |
| 341 |
{ |
| 342 |
if (se.sz == 1) |
| 343 |
return se; |
| 344 |
// Convert to UTF-8 string |
| 345 |
se.committed = false; |
| 346 |
Expression e = castTo(se, sc, Type.tchar.arrayOf()); |
| 347 |
e = e.optimize(WANTvalue); |
| 348 |
auto result = e.isStringExp(); |
| 349 |
assert(result); |
| 350 |
assert(result.sz == 1); |
| 351 |
return result; |
| 352 |
} |
| 353 |
/******************************** |
| 354 |
* The type for a unary expression is incompatible. |
| 355 |
* Print error message. |
| 356 |
* Returns: |
| 357 |
* ErrorExp |
| 358 |
*/ |
| 359 |
private Expression incompatibleTypes(UnaExp e) |
| 360 |
{ |
| 361 |
if (e.e1.type.toBasetype() == Type.terror) |
| 362 |
return e.e1; |
| 363 |
|
| 364 |
if (e.e1.op == EXP.type) |
| 365 |
{ |
| 366 |
error(e.loc, "incompatible type for `%s(%s)`: cannot use `%s` with types", EXPtoString(e.op).ptr, e.e1.toErrMsg(), EXPtoString(e.op).ptr); |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
error(e.loc, "incompatible type for `%s(%s)`: `%s`", EXPtoString(e.op).ptr, e.e1.toErrMsg(), e.e1.type.toChars()); |
| 371 |
} |
| 372 |
return ErrorExp.get(); |
| 373 |
} |
| 374 |
|
| 375 |
/******************************** |
| 376 |
* The types for a binary expression are incompatible. |
| 377 |
* Print error message. |
| 378 |
* Returns: |
| 379 |
* ErrorExp |
| 380 |
*/ |
| 381 |
extern (D) Expression incompatibleTypes(BinExp e, Scope* sc = null) |
| 382 |
{ |
| 383 |
if (e.e1.type.toBasetype() == Type.terror) |
| 384 |
return e.e1; |
| 385 |
if (e.e2.type.toBasetype() == Type.terror) |
| 386 |
return e.e2; |
| 387 |
|
| 388 |
// CondExp uses 'a ? b : c' but we're comparing 'b : c' |
| 389 |
const(char)* thisOp = (e.op == EXP.question) ? ":" : EXPtoString(e.op).ptr; |
| 390 |
|
| 391 |
if (sc && suggestBinaryOverloads(e, sc)) |
| 392 |
return ErrorExp.get(); |
| 393 |
|
| 394 |
if (e.e1.op == EXP.type || e.e2.op == EXP.type) |
| 395 |
{ |
| 396 |
error(e.loc, "incompatible types for `(%s) %s (%s)`: cannot use `%s` with types", |
| 397 |
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), EXPtoString(e.op).ptr); |
| 398 |
} |
| 399 |
else if (e.e1.type.equals(e.e2.type)) |
| 400 |
{ |
| 401 |
error(e.loc, "incompatible types for `(%s) %s (%s)`: both operands are of type `%s`", |
| 402 |
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), e.e1.type.toChars()); |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
auto ts = toAutoQualChars(e.e1.type, e.e2.type); |
| 407 |
error(e.loc, "incompatible types for `(%s) %s (%s)`: `%s` and `%s`", |
| 408 |
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), ts[0], ts[1]); |
| 409 |
} |
| 410 |
return ErrorExp.get(); |
| 411 |
} |
| 412 |
|
| 413 |
private Expression reorderSettingAAElem(BinExp exp, Scope* sc) |
| 414 |
{ |
| 415 |
BinExp be = exp; |
| 416 |
|
| 417 |
auto ie = be.e1.isIndexExp(); |
| 418 |
if (!ie) |
| 419 |
return be; |
| 420 |
if (ie.e1.type.toBasetype().ty != Taarray) |
| 421 |
return be; |
| 422 |
|
| 423 |
/* Fix evaluation order of setting AA element |
| 424 |
* https://issues.dlang.org/show_bug.cgi?id=3825 |
| 425 |
* Rewrite: |
| 426 |
* aa[k1][k2][k3] op= val; |
| 427 |
* as: |
| 428 |
* auto ref __aatmp = aa; |
| 429 |
* auto ref __aakey3 = k1, __aakey2 = k2, __aakey1 = k3; |
| 430 |
* auto ref __aaval = val; |
| 431 |
* __aatmp[__aakey3][__aakey2][__aakey1] op= __aaval; // assignment |
| 432 |
*/ |
| 433 |
|
| 434 |
Expression e0; |
| 435 |
while (1) |
| 436 |
{ |
| 437 |
Expression de; |
| 438 |
ie.e2 = extractSideEffect(sc, "__aakey", de, ie.e2); |
| 439 |
e0 = Expression.combine(de, e0); |
| 440 |
|
| 441 |
auto ie1 = ie.e1.isIndexExp(); |
| 442 |
if (!ie1 || |
| 443 |
ie1.e1.type.toBasetype().ty != Taarray) |
| 444 |
{ |
| 445 |
break; |
| 446 |
} |
| 447 |
ie = ie1; |
| 448 |
} |
| 449 |
assert(ie.e1.type.toBasetype().ty == Taarray); |
| 450 |
|
| 451 |
Expression de; |
| 452 |
ie.e1 = extractSideEffect(sc, "__aatmp", de, ie.e1); |
| 453 |
e0 = Expression.combine(de, e0); |
| 454 |
|
| 455 |
be.e2 = extractSideEffect(sc, "__aaval", e0, be.e2, true); |
| 456 |
|
| 457 |
//printf("-e0 = %s, be = %s\n", e0.toChars(), be.toChars()); |
| 458 |
return Expression.combine(e0, be); |
| 459 |
} |
| 460 |
|
| 461 |
private Expression checkOpAssignTypes(BinExp binExp, Scope* sc) |
| 462 |
{ |
| 463 |
auto e1 = binExp.e1; |
| 464 |
auto e2 = binExp.e2; |
| 465 |
auto op = binExp.op; |
| 466 |
auto type = binExp.type; |
| 467 |
auto loc = binExp.loc; |
| 468 |
|
| 469 |
// At that point t1 and t2 are the merged types. type is the original type of the lhs. |
| 470 |
Type t1 = e1.type; |
| 471 |
Type t2 = e2.type; |
| 472 |
|
| 473 |
// T opAssign floating yields a floating. Prevent truncating conversions (float to int). |
| 474 |
// See https://issues.dlang.org/show_bug.cgi?id=3841. |
| 475 |
// Should we also prevent double to float (type.isFloating() && type.size() < t2.size()) ? |
| 476 |
if (op == EXP.addAssign || op == EXP.minAssign || |
| 477 |
op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign || |
| 478 |
op == EXP.powAssign) |
| 479 |
{ |
| 480 |
if ((type.isIntegral() && t2.isFloating())) |
| 481 |
{ |
| 482 |
warning(loc, "`%s %s %s` is performing truncating conversion", type.toChars(), EXPtoString(op).ptr, t2.toChars()); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
// generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary |
| 487 |
if (op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign) |
| 488 |
{ |
| 489 |
// Any multiplication by an imaginary or complex number yields a complex result. |
| 490 |
// r *= c, i*=c, r*=i, i*=i are all forbidden operations. |
| 491 |
const(char)* opstr = EXPtoString(op).ptr; |
| 492 |
if (t1.isReal() && t2.isComplex()) |
| 493 |
{ |
| 494 |
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.re`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars()); |
| 495 |
return ErrorExp.get(); |
| 496 |
} |
| 497 |
else if (t1.isImaginary() && t2.isComplex()) |
| 498 |
{ |
| 499 |
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.im`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars()); |
| 500 |
return ErrorExp.get(); |
| 501 |
} |
| 502 |
else if ((t1.isReal() || t1.isImaginary()) && t2.isImaginary()) |
| 503 |
{ |
| 504 |
error(loc, "`%s %s %s` is an undefined operation", t1.toChars(), opstr, t2.toChars()); |
| 505 |
return ErrorExp.get(); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
// generate an error if this is a nonsensical += or -=, eg real += imaginary |
| 510 |
if (op == EXP.addAssign || op == EXP.minAssign) |
| 511 |
{ |
| 512 |
// Addition or subtraction of a real and an imaginary is a complex result. |
| 513 |
// Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations. |
| 514 |
if ((t1.isReal() && (t2.isImaginary() || t2.isComplex())) || (t1.isImaginary() && (t2.isReal() || t2.isComplex()))) |
| 515 |
{ |
| 516 |
error(loc, "`%s %s %s` is undefined (result is complex)", t1.toChars(), EXPtoString(op).ptr, t2.toChars()); |
| 517 |
return ErrorExp.get(); |
| 518 |
} |
| 519 |
if (type.isReal() || type.isImaginary()) |
| 520 |
{ |
| 521 |
assert(global.errors || t2.isFloating()); |
| 522 |
e2 = e2.castTo(sc, t1); |
| 523 |
} |
| 524 |
} |
| 525 |
if (op == EXP.mulAssign && t2.isFloating()) |
| 526 |
{ |
| 527 |
if (t1.isReal()) |
| 528 |
{ |
| 529 |
if (t2.isImaginary() || t2.isComplex()) |
| 530 |
{ |
| 531 |
e2 = e2.castTo(sc, t1); |
| 532 |
} |
| 533 |
} |
| 534 |
else if (t1.isImaginary()) |
| 535 |
{ |
| 536 |
if (t2.isImaginary() || t2.isComplex()) |
| 537 |
{ |
| 538 |
switch (t1.ty) |
| 539 |
{ |
| 540 |
case Timaginary32: |
| 541 |
t2 = Type.tfloat32; |
| 542 |
break; |
| 543 |
|
| 544 |
case Timaginary64: |
| 545 |
t2 = Type.tfloat64; |
| 546 |
break; |
| 547 |
|
| 548 |
case Timaginary80: |
| 549 |
t2 = Type.tfloat80; |
| 550 |
break; |
| 551 |
|
| 552 |
default: |
| 553 |
assert(0); |
| 554 |
} |
| 555 |
e2 = e2.castTo(sc, t2); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
else if (op == EXP.divAssign && t2.isImaginary()) |
| 560 |
{ |
| 561 |
if (t1.isReal()) |
| 562 |
{ |
| 563 |
// x/iv = i(-x/v) |
| 564 |
// Therefore, the result is 0 |
| 565 |
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1)); |
| 566 |
e2.type = t1; |
| 567 |
Expression e = new AssignExp(loc, e1, e2); |
| 568 |
e.type = t1; |
| 569 |
return e; |
| 570 |
} |
| 571 |
else if (t1.isImaginary()) |
| 572 |
{ |
| 573 |
Type t3; |
| 574 |
switch (t1.ty) |
| 575 |
{ |
| 576 |
case Timaginary32: |
| 577 |
t3 = Type.tfloat32; |
| 578 |
break; |
| 579 |
|
| 580 |
case Timaginary64: |
| 581 |
t3 = Type.tfloat64; |
| 582 |
break; |
| 583 |
|
| 584 |
case Timaginary80: |
| 585 |
t3 = Type.tfloat80; |
| 586 |
break; |
| 587 |
|
| 588 |
default: |
| 589 |
assert(0); |
| 590 |
} |
| 591 |
e2 = e2.castTo(sc, t3); |
| 592 |
Expression e = new AssignExp(loc, e1, e2); |
| 593 |
e.type = t1; |
| 594 |
return e; |
| 595 |
} |
| 596 |
} |
| 597 |
else if (op == EXP.modAssign) |
| 598 |
{ |
| 599 |
if (t2.isComplex()) |
| 600 |
{ |
| 601 |
error(loc, "cannot perform modulo complex arithmetic"); |
| 602 |
return ErrorExp.get(); |
| 603 |
} |
| 604 |
} |
| 605 |
return binExp; |
| 606 |
} |
| 607 |
|
| 608 |
private Expression extractOpDollarSideEffect(Scope* sc, UnaExp ue) |
| 609 |
{ |
| 610 |
Expression e0; |
| 611 |
Expression e1 = Expression.extractLast(ue.e1, e0); |
| 612 |
// https://issues.dlang.org/show_bug.cgi?id=12585 |
| 613 |
// Extract the side effect part if ue.e1 is comma. |
| 614 |
|
| 615 |
if (sc.ctfe ? hasSideEffect(e1) : !isTrivialExp(e1)) // match logic in extractSideEffect() |
| 616 |
{ |
| 617 |
/* Even if opDollar is needed, 'e1' should be evaluate only once. So |
| 618 |
* Rewrite: |
| 619 |
* e1.opIndex( ... use of $ ... ) |
| 620 |
* e1.opSlice( ... use of $ ... ) |
| 621 |
* as: |
| 622 |
* (ref __dop = e1, __dop).opIndex( ... __dop.opDollar ...) |
| 623 |
* (ref __dop = e1, __dop).opSlice( ... __dop.opDollar ...) |
| 624 |
*/ |
| 625 |
e1 = extractSideEffect(sc, "__dop", e0, e1, false); |
| 626 |
assert(e1.isVarExp()); |
| 627 |
e1.isVarExp().var.storage_class |= STC.exptemp; // lifetime limited to expression |
| 628 |
} |
| 629 |
ue.e1 = e1; |
| 630 |
return e0; |
| 631 |
} |
| 632 |
|
| 633 |
/**************************************** |
| 634 |
* Expand alias this tuples. |
| 635 |
*/ |
| 636 |
TupleDeclaration isAliasThisTuple(Expression e) |
| 637 |
{ |
| 638 |
if (!e.type) |
| 639 |
return null; |
| 640 |
|
| 641 |
Type t = e.type.toBasetype(); |
| 642 |
while (true) |
| 643 |
{ |
| 644 |
if (Dsymbol s = t.toDsymbol(null)) |
| 645 |
{ |
| 646 |
if (auto ad = s.isAggregateDeclaration()) |
| 647 |
{ |
| 648 |
s = ad.aliasthis ? ad.aliasthis.sym : null; |
| 649 |
if (s && s.isVarDeclaration()) |
| 650 |
{ |
| 651 |
TupleDeclaration td = s.isVarDeclaration().toAlias().isTupleDeclaration(); |
| 652 |
if (td && td.isexp) |
| 653 |
return td; |
| 654 |
} |
| 655 |
if (Type att = t.aliasthisOf()) |
| 656 |
{ |
| 657 |
t = att; |
| 658 |
continue; |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
return null; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
/************************************** |
| 667 |
* Runs semantic on ae.arguments. Declares temporary variables |
| 668 |
* if '$' was used. |
| 669 |
*/ |
| 670 |
Expression resolveOpDollar(Scope* sc, ArrayExp ae, out Expression pe0) |
| 671 |
{ |
| 672 |
assert(!ae.lengthVar); |
| 673 |
AggregateDeclaration ad = isAggregate(ae.e1.type); |
| 674 |
Dsymbol slice = search_function(ad, Id.opSlice); |
| 675 |
//printf("slice = %s %s\n", slice.kind(), slice.toChars()); |
| 676 |
Expression fallback() |
| 677 |
{ |
| 678 |
if (ae.arguments.length == 1) |
| 679 |
return null; |
| 680 |
error(ae.loc, "multi-dimensional slicing requires template `opSlice`"); |
| 681 |
return ErrorExp.get(); |
| 682 |
} |
| 683 |
foreach (i, e; *ae.arguments) |
| 684 |
{ |
| 685 |
if (i == 0) |
| 686 |
pe0 = extractOpDollarSideEffect(sc, ae); |
| 687 |
|
| 688 |
if (e.op == EXP.interval && !(slice && slice.isTemplateDeclaration())) |
| 689 |
{ |
| 690 |
return fallback(); |
| 691 |
} |
| 692 |
//printf("[%d] e = %s\n", i, e.toChars()); |
| 693 |
|
| 694 |
// Create scope for '$' variable for this dimension |
| 695 |
auto sym = new ArrayScopeSymbol(sc, ae); |
| 696 |
sym.parent = sc.scopesym; |
| 697 |
sc = sc.push(sym); |
| 698 |
ae.lengthVar = null; // Create it only if required |
| 699 |
ae.currentDimension = i; // Dimension for $, if required |
| 700 |
|
| 701 |
e = e.expressionSemantic(sc); |
| 702 |
e = resolveProperties(sc, e); |
| 703 |
|
| 704 |
if (ae.lengthVar && sc.func) |
| 705 |
{ |
| 706 |
// If $ was used, declare it now |
| 707 |
Expression de = new DeclarationExp(ae.loc, ae.lengthVar); |
| 708 |
de = de.expressionSemantic(sc); |
| 709 |
pe0 = Expression.combine(pe0, de); |
| 710 |
} |
| 711 |
sc = sc.pop(); |
| 712 |
|
| 713 |
if (auto ie = e.isIntervalExp()) |
| 714 |
{ |
| 715 |
auto tiargs = new Objects(); |
| 716 |
Expression edim = new IntegerExp(ae.loc, i, Type.tsize_t); |
| 717 |
edim = edim.expressionSemantic(sc); |
| 718 |
tiargs.push(edim); |
| 719 |
|
| 720 |
auto fargs = new Expressions(2); |
| 721 |
(*fargs)[0] = ie.lwr; |
| 722 |
(*fargs)[1] = ie.upr; |
| 723 |
|
| 724 |
const xerrors = global.startGagging(); |
| 725 |
sc = sc.push(); |
| 726 |
FuncDeclaration fslice = resolveFuncCall(ae.loc, sc, slice, tiargs, ae.e1.type, ArgumentList(fargs), FuncResolveFlag.quiet); |
| 727 |
sc = sc.pop(); |
| 728 |
global.endGagging(xerrors); |
| 729 |
if (!fslice) |
| 730 |
return fallback(); |
| 731 |
|
| 732 |
e = new DotTemplateInstanceExp(ae.loc, ae.e1, Id.opSlice, tiargs); |
| 733 |
e = new CallExp(ae.loc, e, fargs); |
| 734 |
e = e.expressionSemantic(sc); |
| 735 |
} |
| 736 |
|
| 737 |
if (!e.type) |
| 738 |
{ |
| 739 |
error(ae.loc, "`%s` has no value", e.toErrMsg()); |
| 740 |
e = ErrorExp.get(); |
| 741 |
} |
| 742 |
if (e.op == EXP.error) |
| 743 |
return e; |
| 744 |
|
| 745 |
(*ae.arguments)[i] = e; |
| 746 |
} |
| 747 |
return ae; |
| 748 |
} |
| 749 |
|
| 750 |
/************************************** |
| 751 |
* Runs semantic on se.lwr and se.upr. Declares a temporary variable |
| 752 |
* if '$' was used. |
| 753 |
* Returns: |
| 754 |
* ae, or ErrorExp if errors occurred |
| 755 |
*/ |
| 756 |
Expression resolveOpDollar(Scope* sc, ArrayExp ae, IntervalExp ie, ref Expression pe0) |
| 757 |
{ |
| 758 |
//assert(!ae.lengthVar); |
| 759 |
if (!ie) |
| 760 |
return ae; |
| 761 |
|
| 762 |
VarDeclaration lengthVar = ae.lengthVar; |
| 763 |
bool errors = false; |
| 764 |
|
| 765 |
// create scope for '$' |
| 766 |
auto sym = new ArrayScopeSymbol(sc, ae); |
| 767 |
sym.parent = sc.scopesym; |
| 768 |
sc = sc.push(sym); |
| 769 |
|
| 770 |
Expression sem(Expression e) |
| 771 |
{ |
| 772 |
e = e.expressionSemantic(sc); |
| 773 |
e = resolveProperties(sc, e); |
| 774 |
if (!e.type) |
| 775 |
{ |
| 776 |
error(ae.loc, "`%s` has no value", e.toErrMsg()); |
| 777 |
errors = true; |
| 778 |
} |
| 779 |
return e; |
| 780 |
} |
| 781 |
|
| 782 |
ie.lwr = sem(ie.lwr); |
| 783 |
ie.upr = sem(ie.upr); |
| 784 |
|
| 785 |
if (ie.lwr.isErrorExp() || ie.upr.isErrorExp()) |
| 786 |
errors = true; |
| 787 |
|
| 788 |
if (lengthVar != ae.lengthVar && sc.func) |
| 789 |
{ |
| 790 |
// If $ was used, declare it now |
| 791 |
Expression de = new DeclarationExp(ae.loc, ae.lengthVar); |
| 792 |
de = de.expressionSemantic(sc); |
| 793 |
pe0 = Expression.combine(pe0, de); |
| 794 |
} |
| 795 |
|
| 796 |
sc = sc.pop(); |
| 797 |
|
| 798 |
return errors ? ErrorExp.get() : ae; |
| 799 |
} |
| 800 |
|
| 801 |
/****************************** |
| 802 |
* Perform semantic() on an array of Expressions. |
| 803 |
*/ |
| 804 |
extern(D) bool arrayExpressionSemantic( |
| 805 |
Expression[] exps, Scope* sc, bool preserveErrors = false) |
| 806 |
{ |
| 807 |
bool err = false; |
| 808 |
foreach (ref e; exps) |
| 809 |
{ |
| 810 |
if (e is null) continue; |
| 811 |
auto e2 = e.expressionSemantic(sc); |
| 812 |
if (e2.op == EXP.error) |
| 813 |
err = true; |
| 814 |
if (preserveErrors || e2.op != EXP.error) |
| 815 |
e = e2; |
| 816 |
} |
| 817 |
return err; |
| 818 |
} |
| 819 |
|
| 820 |
/************************************************ |
| 821 |
* Handle the postblit call on lvalue, or the move of rvalue. |
| 822 |
* |
| 823 |
* Params: |
| 824 |
* sc = the scope where the expression is encountered |
| 825 |
* e = the expression the needs to be moved or copied (source) |
| 826 |
* t = if the struct defines a copy constructor, the type of the destination (can be NULL) |
| 827 |
* nrvo = true if the generated copy can be treated as NRVO |
| 828 |
* move = true to allow a move constructor to be used, false to prevent infinite recursion |
| 829 |
* Returns: |
| 830 |
* The expression that copy constructs or moves the value. |
| 831 |
*/ |
| 832 |
extern (D) Expression doCopyOrMove(Scope* sc, Expression e, Type t, bool nrvo, bool move = false) |
| 833 |
{ |
| 834 |
//printf("doCopyOrMove() %s\n", toChars(e)); |
| 835 |
StructDeclaration sd; |
| 836 |
if (t) |
| 837 |
{ |
| 838 |
if (auto ts = t.isTypeStruct()) |
| 839 |
sd = ts.sym; |
| 840 |
} |
| 841 |
|
| 842 |
if (auto ce = e.isCondExp()) |
| 843 |
{ |
| 844 |
ce.e1 = doCopyOrMove(sc, ce.e1, null, nrvo); |
| 845 |
ce.e2 = doCopyOrMove(sc, ce.e2, null, nrvo); |
| 846 |
} |
| 847 |
else if (e.isLvalue()) |
| 848 |
{ |
| 849 |
e = callCpCtor(sc, e, t, nrvo); |
| 850 |
} |
| 851 |
else if (move && sd && sd.hasMoveCtor && !e.isCallExp() && !e.isStructLiteralExp()) |
| 852 |
{ |
| 853 |
// #move |
| 854 |
/* Rewrite as: |
| 855 |
* S __copyrvalue; |
| 856 |
* __copyrvalue.moveCtor(e); |
| 857 |
* __copyrvalue; |
| 858 |
*/ |
| 859 |
VarDeclaration vd = new VarDeclaration(e.loc, e.type, Identifier.generateId("__copyrvalue"), null); |
| 860 |
if (nrvo) |
| 861 |
vd.nrvo = true; |
| 862 |
vd.storage_class |= STC.nodtor; |
| 863 |
vd.dsymbolSemantic(sc); |
| 864 |
Expression de = new DeclarationExp(e.loc, vd); |
| 865 |
Expression ve = new VarExp(e.loc, vd); |
| 866 |
|
| 867 |
Expression er; |
| 868 |
er = new DotIdExp(e.loc, ve, Id.ctor); // ve.ctor |
| 869 |
er = new CallExp(e.loc, er, e); // ve.ctor(e) |
| 870 |
er = new CommaExp(e.loc, er, new VarExp(e.loc, vd)); // ve.ctor(e),vd |
| 871 |
er = Expression.combine(de, er); // de,ve.ctor(e),vd |
| 872 |
|
| 873 |
e = er.expressionSemantic(sc); |
| 874 |
} |
| 875 |
else |
| 876 |
{ |
| 877 |
e = valueNoDtor(e); |
| 878 |
} |
| 879 |
return e; |
| 880 |
} |
| 881 |
|
| 882 |
/********************************************* |
| 883 |
* If e is an instance of a struct, and that struct has a copy constructor, |
| 884 |
* rewrite e as: |
| 885 |
* (tmp = e),tmp |
| 886 |
* Params: |
| 887 |
* sc = just used to specify the scope of created temporary variable |
| 888 |
* destinationType = the type of the object on which the copy constructor is called; |
| 889 |
* may be null if the struct defines a postblit |
| 890 |
* nrvo = true if the generated copy can be treated as NRVO |
| 891 |
*/ |
| 892 |
private Expression callCpCtor(Scope* sc, Expression e, Type destinationType, bool nrvo) |
| 893 |
{ |
| 894 |
//printf("callCpCtor(e: %s et: %s destinationType: %s\n", toChars(e), toChars(e.type), toChars(destinationType)); |
| 895 |
auto ts = e.type.baseElemOf().isTypeStruct(); |
| 896 |
|
| 897 |
if (!ts) |
| 898 |
return e; |
| 899 |
StructDeclaration sd = ts.sym; |
| 900 |
if (!sd.postblit && !sd.hasCopyCtor) |
| 901 |
return e; |
| 902 |
|
| 903 |
/* Create a variable tmp, and replace the argument e with: |
| 904 |
* (tmp = e),tmp |
| 905 |
* and let AssignExp() handle the construction. |
| 906 |
* This is not the most efficient, ideally tmp would be constructed |
| 907 |
* directly onto the stack. |
| 908 |
*/ |
| 909 |
VarDeclaration tmp = copyToTemp(STC.rvalue, "__copytmp", e); |
| 910 |
if (nrvo) |
| 911 |
tmp.nrvo = true; |
| 912 |
if (sd.hasCopyCtor && destinationType) |
| 913 |
{ |
| 914 |
// https://issues.dlang.org/show_bug.cgi?id=22619 |
| 915 |
// If the destination type is inout we can preserve it |
| 916 |
// only if inside an inout function; if we are not inside |
| 917 |
// an inout function, then we will preserve the type of |
| 918 |
// the source |
| 919 |
if (destinationType.hasWild && !(sc.func.storage_class & STC.wild)) |
| 920 |
tmp.type = e.type; |
| 921 |
else |
| 922 |
tmp.type = destinationType; |
| 923 |
} |
| 924 |
tmp.storage_class |= STC.nodtor; |
| 925 |
tmp.dsymbolSemantic(sc); |
| 926 |
Expression de = new DeclarationExp(e.loc, tmp); |
| 927 |
Expression ve = new VarExp(e.loc, tmp); |
| 928 |
de.type = Type.tvoid; |
| 929 |
ve.type = e.type; |
| 930 |
return Expression.combine(de, ve); |
| 931 |
} |
| 932 |
|
| 933 |
/************************************************ |
| 934 |
* If we want the value of this expression, but do not want to call |
| 935 |
* the destructor on it. |
| 936 |
*/ |
| 937 |
Expression valueNoDtor(Expression e) |
| 938 |
{ |
| 939 |
//printf("valueNoDtor() %s\n", toChars(e)); |
| 940 |
auto ex = lastComma(e); |
| 941 |
|
| 942 |
if (auto ce = ex.isCallExp()) |
| 943 |
{ |
| 944 |
/* The struct value returned from the function is transferred |
| 945 |
* so do not call the destructor on it. |
| 946 |
* Recognize: |
| 947 |
* ((S _ctmp = S.init), _ctmp).this(...) |
| 948 |
* and make sure the destructor is not called on _ctmp |
| 949 |
* BUG: if ex is a CommaExp, we should go down the right side. |
| 950 |
*/ |
| 951 |
if (auto dve = ce.e1.isDotVarExp()) |
| 952 |
{ |
| 953 |
if (dve.var.isCtorDeclaration()) |
| 954 |
{ |
| 955 |
// It's a constructor call |
| 956 |
if (auto comma = dve.e1.isCommaExp()) |
| 957 |
{ |
| 958 |
if (auto ve = comma.e2.isVarExp()) |
| 959 |
{ |
| 960 |
VarDeclaration ctmp = ve.var.isVarDeclaration(); |
| 961 |
if (ctmp) |
| 962 |
{ |
| 963 |
ctmp.storage_class |= STC.nodtor; |
| 964 |
assert(!ce.isLvalue()); |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
else if (auto ve = ex.isVarExp()) |
| 972 |
{ |
| 973 |
auto vtmp = ve.var.isVarDeclaration(); |
| 974 |
if (vtmp && (vtmp.storage_class & STC.rvalue)) |
| 975 |
{ |
| 976 |
vtmp.storage_class |= STC.nodtor; |
| 977 |
} |
| 978 |
} |
| 979 |
return e; |
| 980 |
} |
| 981 |
|
| 982 |
/* |
| 983 |
Checks if `exp` contains a direct access to a `noreturn` |
| 984 |
variable. If that is the case, an `assert(0)` expression |
| 985 |
is generated and returned. This function should be called |
| 986 |
only after semantic analysis has been performed on `exp`. |
| 987 |
|
| 988 |
Params: |
| 989 |
exp = expression that is checked |
| 990 |
|
| 991 |
Returns: |
| 992 |
An `assert(0)` expression if `exp` contains a `noreturn` |
| 993 |
variable access, `exp` otherwise. |
| 994 |
*/ |
| 995 |
|
| 996 |
Expression checkNoreturnVarAccess(Expression exp) |
| 997 |
{ |
| 998 |
assert(exp.type); |
| 999 |
|
| 1000 |
Expression result = exp; |
| 1001 |
if (exp.type.isTypeNoreturn() && !exp.isAssertExp() && |
| 1002 |
!exp.isThrowExp() && !exp.isCallExp()) |
| 1003 |
{ |
| 1004 |
auto msg = new StringExp(exp.loc, "Accessed expression of type `noreturn`"); |
| 1005 |
msg.type = Type.tstring; |
| 1006 |
result = new AssertExp(exp.loc, IntegerExp.literal!0, msg); |
| 1007 |
result.type = exp.type; |
| 1008 |
} |
| 1009 |
|
| 1010 |
return result; |
| 1011 |
} |
| 1012 |
|
| 1013 |
/****************************** |
| 1014 |
* Find symbol in accordance with the UFCS name look up rule |
| 1015 |
*/ |
| 1016 |
private Expression searchUFCS(Scope* sc, UnaExp ue, Identifier ident) |
| 1017 |
{ |
| 1018 |
//printf("searchUFCS(ident = %s)\n", ident.toChars()); |
| 1019 |
Loc loc = ue.loc; |
| 1020 |
|
| 1021 |
// TODO: merge with Scope.search.searchScopes() |
| 1022 |
Dsymbol searchScopes(SearchOptFlags flags) |
| 1023 |
{ |
| 1024 |
Dsymbol s = null; |
| 1025 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 1026 |
{ |
| 1027 |
if (!scx.scopesym) |
| 1028 |
continue; |
| 1029 |
if (scx.scopesym.isModule()) |
| 1030 |
flags |= SearchOpt.unqualifiedModule; // tell Module.search() that SearchOpt.localsOnly is to be obeyed |
| 1031 |
s = scx.scopesym.search(loc, ident, flags); |
| 1032 |
if (s) |
| 1033 |
{ |
| 1034 |
// overload set contains only module scope symbols. |
| 1035 |
if (s.isOverloadSet()) |
| 1036 |
break; |
| 1037 |
// selective/renamed imports also be picked up |
| 1038 |
if (AliasDeclaration ad = s.isAliasDeclaration()) |
| 1039 |
{ |
| 1040 |
if (ad._import) |
| 1041 |
break; |
| 1042 |
} |
| 1043 |
// See only module scope symbols for UFCS target. |
| 1044 |
Dsymbol p = s.toParent2(); |
| 1045 |
if (p && p.isModule()) |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
s = null; |
| 1049 |
|
| 1050 |
// Stop when we hit a module, but keep going if that is not just under the global scope |
| 1051 |
if (scx.scopesym.isModule() && !(scx.enclosing && !scx.enclosing.enclosing)) |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
return s; |
| 1055 |
} |
| 1056 |
|
| 1057 |
SearchOptFlags flags = SearchOpt.all; |
| 1058 |
Dsymbol s; |
| 1059 |
|
| 1060 |
if (sc.ignoresymbolvisibility) |
| 1061 |
flags |= SearchOpt.ignoreVisibility; |
| 1062 |
|
| 1063 |
// First look in local scopes |
| 1064 |
s = searchScopes(flags | SearchOpt.localsOnly); |
| 1065 |
if (!s) |
| 1066 |
{ |
| 1067 |
// Second look in imported modules |
| 1068 |
s = searchScopes(flags | SearchOpt.importsOnly); |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (!s) |
| 1072 |
return ue.e1.type.getProperty(sc, loc, ident, 0, ue.e1); |
| 1073 |
|
| 1074 |
FuncDeclaration f = s.isFuncDeclaration(); |
| 1075 |
if (f) |
| 1076 |
{ |
| 1077 |
if (TemplateDeclaration td = getFuncTemplateDecl(f)) |
| 1078 |
{ |
| 1079 |
if (td.overroot) |
| 1080 |
td = td.overroot; |
| 1081 |
s = td; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (auto dti = ue.isDotTemplateInstanceExp()) |
| 1086 |
{ |
| 1087 |
// https://issues.dlang.org/show_bug.cgi?id=23968 |
| 1088 |
// Typically, deprecated alias declarations are caught |
| 1089 |
// when `TemplateInstance.findTempDecl` is called, |
| 1090 |
// however, in this case the tempdecl field is updated |
| 1091 |
// therefore `findTempDecl` will return immediately |
| 1092 |
// and not get the chance to issue the deprecation. |
| 1093 |
if (s.isAliasDeclaration()) |
| 1094 |
s.checkDeprecated(ue.loc, sc); |
| 1095 |
|
| 1096 |
auto ti = new TemplateInstance(loc, s.ident, dti.ti.tiargs); |
| 1097 |
if (!ti.updateTempDecl(sc, s)) |
| 1098 |
return ErrorExp.get(); |
| 1099 |
return new ScopeExp(loc, ti); |
| 1100 |
} |
| 1101 |
else |
| 1102 |
{ |
| 1103 |
//printf("-searchUFCS() %s\n", s.toChars()); |
| 1104 |
return new DsymbolExp(loc, s); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
/****************************** |
| 1109 |
* check e is exp.opDispatch!(tiargs) or not |
| 1110 |
* It's used to switch to UFCS the semantic analysis path |
| 1111 |
*/ |
| 1112 |
private bool isDotOpDispatch(Expression e) |
| 1113 |
{ |
| 1114 |
if (auto dtie = e.isDotTemplateInstanceExp()) |
| 1115 |
return dtie.ti.name == Id.opDispatch; |
| 1116 |
return false; |
| 1117 |
} |
| 1118 |
|
| 1119 |
private void hookDtors(CondExp ce, Scope* sc) |
| 1120 |
{ |
| 1121 |
extern (C++) final class DtorVisitor : StoppableVisitor |
| 1122 |
{ |
| 1123 |
alias visit = typeof(super).visit; |
| 1124 |
public: |
| 1125 |
Scope* sc; |
| 1126 |
CondExp ce; |
| 1127 |
VarDeclaration vcond; |
| 1128 |
bool isThen; |
| 1129 |
|
| 1130 |
extern (D) this(Scope* sc, CondExp ce) @safe |
| 1131 |
{ |
| 1132 |
this.sc = sc; |
| 1133 |
this.ce = ce; |
| 1134 |
} |
| 1135 |
|
| 1136 |
override void visit(Expression e) |
| 1137 |
{ |
| 1138 |
//printf("(e = %s)\n", e.toChars()); |
| 1139 |
} |
| 1140 |
|
| 1141 |
override void visit(DeclarationExp e) |
| 1142 |
{ |
| 1143 |
auto v = e.declaration.isVarDeclaration(); |
| 1144 |
if (!v || v.isDataseg()) |
| 1145 |
return; |
| 1146 |
|
| 1147 |
if (v._init) |
| 1148 |
{ |
| 1149 |
if (auto ei = v._init.isExpInitializer()) |
| 1150 |
walkPostorder(ei.exp, this); |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (v.edtor) |
| 1154 |
walkPostorder(v.edtor, this); |
| 1155 |
|
| 1156 |
if (!v.needsScopeDtor()) |
| 1157 |
return; |
| 1158 |
|
| 1159 |
if (!vcond) |
| 1160 |
{ |
| 1161 |
vcond = copyToTemp(STC.volatile_ | STC.const_, "__cond", ce.econd); |
| 1162 |
vcond.dsymbolSemantic(sc); |
| 1163 |
|
| 1164 |
Expression de = new DeclarationExp(ce.econd.loc, vcond); |
| 1165 |
de = de.expressionSemantic(sc); |
| 1166 |
|
| 1167 |
Expression ve = new VarExp(ce.econd.loc, vcond); |
| 1168 |
ce.econd = Expression.combine(de, ve); |
| 1169 |
} |
| 1170 |
|
| 1171 |
//printf("\t++v = %s, v.edtor = %s\n", v.toChars(), v.edtor.toChars()); |
| 1172 |
Expression ve = new VarExp(vcond.loc, vcond); |
| 1173 |
if (isThen) |
| 1174 |
v.edtor = new LogicalExp(v.edtor.loc, EXP.andAnd, ve, v.edtor); |
| 1175 |
else |
| 1176 |
v.edtor = new LogicalExp(v.edtor.loc, EXP.orOr, ve, v.edtor); |
| 1177 |
v.edtor = v.edtor.expressionSemantic(sc); |
| 1178 |
//printf("\t--v = %s, v.edtor = %s\n", v.toChars(), v.edtor.toChars()); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
scope DtorVisitor v = new DtorVisitor(sc, ce); |
| 1183 |
//printf("+%s\n", toChars()); |
| 1184 |
v.isThen = true; |
| 1185 |
walkPostorder(ce.e1, v); |
| 1186 |
v.isThen = false; |
| 1187 |
walkPostorder(ce.e2, v); |
| 1188 |
//printf("-%s\n", toChars()); |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
/****************************** |
| 1193 |
* Pull out callable entity with UFCS. |
| 1194 |
*/ |
| 1195 |
private Expression resolveUFCS(Scope* sc, CallExp ce) |
| 1196 |
{ |
| 1197 |
Loc loc = ce.loc; |
| 1198 |
Expression eleft; |
| 1199 |
Expression e; |
| 1200 |
|
| 1201 |
if (auto die = ce.e1.isDotIdExp()) |
| 1202 |
{ |
| 1203 |
Identifier ident = die.ident; |
| 1204 |
|
| 1205 |
Expression ex = die.dotIdSemanticPropX(sc); |
| 1206 |
if (ex != die) |
| 1207 |
{ |
| 1208 |
ce.e1 = ex; |
| 1209 |
return null; |
| 1210 |
} |
| 1211 |
eleft = die.e1; |
| 1212 |
|
| 1213 |
Type t = eleft.type.toBasetype(); |
| 1214 |
if (t.isStaticOrDynamicArray() || t.ty == Tnull || (t.isTypeBasic() && t.ty != Tvoid)) |
| 1215 |
{ |
| 1216 |
/* Built-in types and arrays have no callable properties, so do shortcut. |
| 1217 |
* It is necessary in: e.init() |
| 1218 |
*/ |
| 1219 |
} |
| 1220 |
else if (t.ty == Taarray) |
| 1221 |
{ |
| 1222 |
if (ident == Id.remove) |
| 1223 |
{ |
| 1224 |
/* Transform: |
| 1225 |
* aa.remove(arg) into delete aa[arg] |
| 1226 |
*/ |
| 1227 |
if (!ce.arguments || ce.arguments.length != 1) |
| 1228 |
{ |
| 1229 |
error(ce.loc, "expected key as argument to `aa.remove()`"); |
| 1230 |
return ErrorExp.get(); |
| 1231 |
} |
| 1232 |
if (!eleft.type.isMutable()) |
| 1233 |
{ |
| 1234 |
error(ce.loc, "cannot remove key from `%s` associative array `%s`", MODtoChars(t.mod), eleft.toChars()); |
| 1235 |
return ErrorExp.get(); |
| 1236 |
} |
| 1237 |
Expression key = (*ce.arguments)[0]; |
| 1238 |
key = key.expressionSemantic(sc); |
| 1239 |
key = resolveProperties(sc, key); |
| 1240 |
|
| 1241 |
TypeAArray taa = t.isTypeAArray(); |
| 1242 |
key = key.implicitCastTo(sc, taa.index); |
| 1243 |
|
| 1244 |
if (key.checkValue() || key.checkSharedAccess(sc)) |
| 1245 |
return ErrorExp.get(); |
| 1246 |
|
| 1247 |
semanticTypeInfo(sc, taa.index); |
| 1248 |
|
| 1249 |
return new RemoveExp(loc, eleft, key); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
else |
| 1253 |
{ |
| 1254 |
if (arrayExpressionSemantic(ce.arguments.peekSlice(), sc)) |
| 1255 |
return ErrorExp.get(); |
| 1256 |
|
| 1257 |
if (Expression ey = die.dotIdSemanticProp(sc, 1)) |
| 1258 |
{ |
| 1259 |
if (ey.op == EXP.error) |
| 1260 |
return ey; |
| 1261 |
ce.e1 = ey; |
| 1262 |
if (isDotOpDispatch(ey)) |
| 1263 |
{ |
| 1264 |
const errors = global.startGagging(); |
| 1265 |
e = ce.expressionSemantic(sc); |
| 1266 |
if (!global.endGagging(errors)) |
| 1267 |
return e; |
| 1268 |
|
| 1269 |
/* fall down to UFCS */ |
| 1270 |
} |
| 1271 |
else |
| 1272 |
return null; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
/* https://issues.dlang.org/show_bug.cgi?id=13953 |
| 1277 |
* |
| 1278 |
* If a struct has an alias this to an associative array |
| 1279 |
* and remove is used on a struct instance, we have to |
| 1280 |
* check first if there is a remove function that can be called |
| 1281 |
* on the struct. If not we must check the alias this. |
| 1282 |
* |
| 1283 |
* struct A |
| 1284 |
* { |
| 1285 |
* string[string] a; |
| 1286 |
* alias a this; |
| 1287 |
* } |
| 1288 |
* |
| 1289 |
* void fun() |
| 1290 |
* { |
| 1291 |
* A s; |
| 1292 |
* s.remove("foo"); |
| 1293 |
* } |
| 1294 |
*/ |
| 1295 |
const errors = global.startGagging(); |
| 1296 |
e = searchUFCS(sc, die, ident); |
| 1297 |
// if there were any errors and the identifier was remove |
| 1298 |
if (global.endGagging(errors)) |
| 1299 |
{ |
| 1300 |
if (ident == Id.remove) |
| 1301 |
{ |
| 1302 |
// check alias this |
| 1303 |
Expression alias_e = resolveAliasThis(sc, die.e1, 1); |
| 1304 |
if (alias_e && alias_e != die.e1) |
| 1305 |
{ |
| 1306 |
die.e1 = alias_e; |
| 1307 |
CallExp ce2 = ce.syntaxCopy(); |
| 1308 |
ce2.e1 = die; |
| 1309 |
e = ce2.isCallExp().trySemantic(sc); |
| 1310 |
if (e) |
| 1311 |
return e; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
// if alias this did not work out, print the initial errors |
| 1315 |
searchUFCS(sc, die, ident); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
else if (auto dti = ce.e1.isDotTemplateInstanceExp()) |
| 1319 |
{ |
| 1320 |
if (Expression ey = dti.dotTemplateSemanticProp(sc, DotExpFlag.gag)) |
| 1321 |
{ |
| 1322 |
ce.e1 = ey; |
| 1323 |
return null; |
| 1324 |
} |
| 1325 |
eleft = dti.e1; |
| 1326 |
e = searchUFCS(sc, dti, dti.ti.name); |
| 1327 |
} |
| 1328 |
else |
| 1329 |
return null; |
| 1330 |
|
| 1331 |
// Rewrite |
| 1332 |
ce.e1 = e; |
| 1333 |
if (!ce.arguments) |
| 1334 |
ce.arguments = new Expressions(); |
| 1335 |
ce.arguments.shift(eleft); |
| 1336 |
if (!ce.names) |
| 1337 |
ce.names = new Identifiers(); |
| 1338 |
ce.names.shift(null); |
| 1339 |
ce.isUfcsRewrite = true; |
| 1340 |
return null; |
| 1341 |
} |
| 1342 |
|
| 1343 |
int expandAliasThisTuples(Expressions* exps, size_t starti = 0) |
| 1344 |
{ |
| 1345 |
if (!exps || exps.length == 0) |
| 1346 |
return -1; |
| 1347 |
|
| 1348 |
for (size_t u = starti; u < exps.length; u++) |
| 1349 |
{ |
| 1350 |
Expression exp = (*exps)[u]; |
| 1351 |
if (TupleDeclaration td = exp.isAliasThisTuple) |
| 1352 |
{ |
| 1353 |
exps.remove(u); |
| 1354 |
size_t i; |
| 1355 |
td.foreachVar((s) |
| 1356 |
{ |
| 1357 |
auto d = s.isDeclaration(); |
| 1358 |
auto e = new DotVarExp(exp.loc, exp, d); |
| 1359 |
assert(d.type); |
| 1360 |
e.type = d.type; |
| 1361 |
exps.insert(u + i, e); |
| 1362 |
++i; |
| 1363 |
}); |
| 1364 |
version (none) |
| 1365 |
{ |
| 1366 |
printf("expansion ->\n"); |
| 1367 |
foreach (e; exps) |
| 1368 |
{ |
| 1369 |
printf("\texps[%d] e = %s %s\n", i, EXPtoString(e.op), e.toChars()); |
| 1370 |
} |
| 1371 |
} |
| 1372 |
return cast(int)u; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
return -1; |
| 1376 |
} |
| 1377 |
|
| 1378 |
/****************************** |
| 1379 |
* Pull out property with UFCS. |
| 1380 |
*/ |
| 1381 |
private Expression resolveUFCSProperties(Scope* sc, Expression e1, Expression e2 = null) |
| 1382 |
{ |
| 1383 |
Loc loc = e1.loc; |
| 1384 |
Expression eleft; |
| 1385 |
Expression e; |
| 1386 |
|
| 1387 |
if (auto die = e1.isDotIdExp()) |
| 1388 |
{ |
| 1389 |
eleft = die.e1; |
| 1390 |
e = searchUFCS(sc, die, die.ident); |
| 1391 |
} |
| 1392 |
else if (auto dti = e1.isDotTemplateInstanceExp()) |
| 1393 |
{ |
| 1394 |
eleft = dti.e1; |
| 1395 |
e = searchUFCS(sc, dti, dti.ti.name); |
| 1396 |
} |
| 1397 |
else |
| 1398 |
return null; |
| 1399 |
|
| 1400 |
if (e is null) |
| 1401 |
return null; |
| 1402 |
|
| 1403 |
// Rewrite |
| 1404 |
if (e2) |
| 1405 |
{ |
| 1406 |
// run semantic without gagging |
| 1407 |
e2 = e2.expressionSemantic(sc); |
| 1408 |
|
| 1409 |
/* f(e1) = e2 |
| 1410 |
*/ |
| 1411 |
Expression ex = e.copy(); |
| 1412 |
auto a1 = new Expressions(1); |
| 1413 |
(*a1)[0] = eleft; |
| 1414 |
ex = new CallExp(loc, ex, a1); |
| 1415 |
auto e1PassSemantic = ex.trySemantic(sc); |
| 1416 |
|
| 1417 |
/* f(e1, e2) |
| 1418 |
*/ |
| 1419 |
auto a2 = new Expressions(2); |
| 1420 |
(*a2)[0] = eleft; |
| 1421 |
(*a2)[1] = e2; |
| 1422 |
e = new CallExp(loc, e, a2); |
| 1423 |
e = e.trySemantic(sc); |
| 1424 |
if (!e1PassSemantic && !e) |
| 1425 |
{ |
| 1426 |
/* https://issues.dlang.org/show_bug.cgi?id=20448 |
| 1427 |
* |
| 1428 |
* If both versions have failed to pass semantic, |
| 1429 |
* f(e1) = e2 gets priority in error printing |
| 1430 |
* because f might be a templated function that |
| 1431 |
* failed to instantiate and we have to print |
| 1432 |
* the instantiation errors. |
| 1433 |
*/ |
| 1434 |
return e1.expressionSemantic(sc); |
| 1435 |
} |
| 1436 |
else if (ex && !e) |
| 1437 |
{ |
| 1438 |
ex = new AssignExp(loc, ex, e2); |
| 1439 |
return ex.expressionSemantic(sc); |
| 1440 |
} |
| 1441 |
else |
| 1442 |
{ |
| 1443 |
// strict setter prints errors if fails |
| 1444 |
e = e.expressionSemantic(sc); |
| 1445 |
} |
| 1446 |
return e; |
| 1447 |
} |
| 1448 |
else |
| 1449 |
{ |
| 1450 |
/* f(e1) |
| 1451 |
*/ |
| 1452 |
auto arguments = new Expressions(1); |
| 1453 |
(*arguments)[0] = eleft; |
| 1454 |
e = new CallExp(loc, e, arguments); |
| 1455 |
e = e.expressionSemantic(sc); |
| 1456 |
return e; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
/****************************** |
| 1461 |
* If e1 is a property function (template), resolve it. |
| 1462 |
*/ |
| 1463 |
Expression resolvePropertiesOnly(Scope* sc, Expression e1) |
| 1464 |
{ |
| 1465 |
//printf("e1 = %s %s\n", Token.toChars(e1.op), e1.toChars()); |
| 1466 |
|
| 1467 |
Expression handleOverloadSet(OverloadSet os) |
| 1468 |
{ |
| 1469 |
assert(os); |
| 1470 |
foreach (s; os.a) |
| 1471 |
{ |
| 1472 |
auto fd = s.isFuncDeclaration(); |
| 1473 |
auto td = s.isTemplateDeclaration(); |
| 1474 |
if (fd) |
| 1475 |
{ |
| 1476 |
if (fd.type.isTypeFunction().isProperty) |
| 1477 |
return resolveProperties(sc, e1); |
| 1478 |
} |
| 1479 |
else if (td && td.onemember && (fd = td.onemember.isFuncDeclaration()) !is null) |
| 1480 |
{ |
| 1481 |
if (fd.type.isTypeFunction().isProperty || |
| 1482 |
(fd.storage_class2 & STC.property) || |
| 1483 |
(td._scope.stc & STC.property)) |
| 1484 |
return resolveProperties(sc, e1); |
| 1485 |
} |
| 1486 |
} |
| 1487 |
return e1; |
| 1488 |
} |
| 1489 |
|
| 1490 |
Expression handleTemplateDecl(TemplateDeclaration td) |
| 1491 |
{ |
| 1492 |
assert(td); |
| 1493 |
if (td.onemember) |
| 1494 |
{ |
| 1495 |
if (auto fd = td.onemember.isFuncDeclaration()) |
| 1496 |
{ |
| 1497 |
if (fd.type.isTypeFunction().isProperty || |
| 1498 |
(fd.storage_class2 & STC.property) || |
| 1499 |
(td._scope.stc & STC.property)) |
| 1500 |
return resolveProperties(sc, e1); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
return e1; |
| 1504 |
} |
| 1505 |
|
| 1506 |
Expression handleFuncDecl(FuncDeclaration fd) |
| 1507 |
{ |
| 1508 |
assert(fd); |
| 1509 |
if (fd.type.isTypeFunction().isProperty) |
| 1510 |
return resolveProperties(sc, e1); |
| 1511 |
return e1; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if (auto de = e1.isDotExp()) |
| 1515 |
{ |
| 1516 |
if (auto os = de.e2.isOverExp()) |
| 1517 |
return handleOverloadSet(os.vars); |
| 1518 |
} |
| 1519 |
else if (auto oe = e1.isOverExp()) |
| 1520 |
return handleOverloadSet(oe.vars); |
| 1521 |
if (auto dti = e1.isDotTemplateInstanceExp()) |
| 1522 |
{ |
| 1523 |
if (dti.ti.tempdecl) |
| 1524 |
if (auto td = dti.ti.tempdecl.isTemplateDeclaration()) |
| 1525 |
return handleTemplateDecl(td); |
| 1526 |
} |
| 1527 |
else if (auto dte = e1.isDotTemplateExp()) |
| 1528 |
return handleTemplateDecl(dte.td); |
| 1529 |
if (auto se = e1.isScopeExp()) |
| 1530 |
{ |
| 1531 |
Dsymbol s = se.sds; |
| 1532 |
TemplateInstance ti = s.isTemplateInstance(); |
| 1533 |
if (ti && !ti.semanticRun && ti.tempdecl) |
| 1534 |
if (auto td = ti.tempdecl.isTemplateDeclaration()) |
| 1535 |
return handleTemplateDecl(td); |
| 1536 |
} |
| 1537 |
else if (auto et = e1.isTemplateExp()) |
| 1538 |
return handleTemplateDecl(et.td); |
| 1539 |
if (e1.isDotVarExp() && e1.type.isTypeFunction()) |
| 1540 |
{ |
| 1541 |
DotVarExp dve = e1.isDotVarExp(); |
| 1542 |
return handleFuncDecl(dve.var.isFuncDeclaration()); |
| 1543 |
} |
| 1544 |
else if (e1.isVarExp() && e1.type && e1.type.isTypeFunction() && (sc.intypeof || !e1.isVarExp().var.needThis())) |
| 1545 |
return handleFuncDecl(e1.isVarExp().var.isFuncDeclaration()); |
| 1546 |
return e1; |
| 1547 |
} |
| 1548 |
|
| 1549 |
/**************************************** |
| 1550 |
* Turn symbol `s` into the expression it represents. |
| 1551 |
* |
| 1552 |
* Params: |
| 1553 |
* s = symbol to resolve |
| 1554 |
* loc = location of use of `s` |
| 1555 |
* sc = context |
| 1556 |
* hasOverloads = applies if `s` represents a function. |
| 1557 |
* true means it's overloaded and will be resolved later, |
| 1558 |
* false means it's the exact function symbol. |
| 1559 |
* Returns: |
| 1560 |
* `s` turned into an expression, `ErrorExp` if an error occurred |
| 1561 |
*/ |
| 1562 |
Expression symbolToExp(Dsymbol s, Loc loc, Scope* sc, bool hasOverloads) |
| 1563 |
{ |
| 1564 |
static if (LOGSEMANTIC) |
| 1565 |
{ |
| 1566 |
printf("DsymbolExp::resolve(%s %s)\n", s.kind(), s.toChars()); |
| 1567 |
} |
| 1568 |
|
| 1569 |
Lagain: |
| 1570 |
Expression e; |
| 1571 |
|
| 1572 |
//printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); |
| 1573 |
//printf("s = '%s', s.kind = '%s'\n", s.toChars(), s.kind()); |
| 1574 |
Dsymbol olds = s; |
| 1575 |
Declaration d = s.isDeclaration(); |
| 1576 |
if (d && (d.storage_class & STC.templateparameter)) |
| 1577 |
{ |
| 1578 |
s = s.toAlias(); |
| 1579 |
} |
| 1580 |
else |
| 1581 |
{ |
| 1582 |
// functions are checked after overloading |
| 1583 |
// templates are checked after matching constraints |
| 1584 |
if (!s.isFuncDeclaration() && !s.isTemplateDeclaration()) |
| 1585 |
{ |
| 1586 |
s.checkDeprecated(loc, sc); |
| 1587 |
if (d) |
| 1588 |
d.checkDisabled(loc, sc); |
| 1589 |
} |
| 1590 |
|
| 1591 |
// https://issues.dlang.org/show_bug.cgi?id=12023 |
| 1592 |
// if 's' is a tuple variable, the tuple is returned. |
| 1593 |
s = s.toAlias(); |
| 1594 |
|
| 1595 |
//printf("s = '%s', s.kind = '%s', s.needThis() = %p\n", s.toChars(), s.kind(), s.needThis()); |
| 1596 |
if (s != olds && !s.isFuncDeclaration() && !s.isTemplateDeclaration()) |
| 1597 |
{ |
| 1598 |
s.checkDeprecated(loc, sc); |
| 1599 |
if (d) |
| 1600 |
d.checkDisabled(loc, sc); |
| 1601 |
} |
| 1602 |
|
| 1603 |
if (auto sd = s.isDeclaration()) |
| 1604 |
{ |
| 1605 |
if (sd.isSystem()) |
| 1606 |
{ |
| 1607 |
if (sc.setUnsafePreview(sc.previews.systemVariables, false, loc, |
| 1608 |
"access `@system` variable `%s`", sd)) |
| 1609 |
{ |
| 1610 |
if (auto v = sd.isVarDeclaration()) |
| 1611 |
{ |
| 1612 |
if (v.systemInferred) |
| 1613 |
errorSupplemental(v.loc, "`%s` is inferred to be `@system` from its initializer here", v.toChars()); |
| 1614 |
else |
| 1615 |
errorSupplemental(v.loc, "`%s` is declared here", v.toChars()); |
| 1616 |
} |
| 1617 |
return ErrorExp.get(); |
| 1618 |
} |
| 1619 |
} |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
if (auto em = s.isEnumMember()) |
| 1624 |
{ |
| 1625 |
return em.getVarExp(loc, sc); |
| 1626 |
} |
| 1627 |
if (auto v = s.isVarDeclaration()) |
| 1628 |
{ |
| 1629 |
//printf("Identifier '%s' is a variable, type '%s'\n", s.toChars(), v.type.toChars()); |
| 1630 |
if (sc.intypeof == 1 && !v.inuse) |
| 1631 |
v.dsymbolSemantic(sc); |
| 1632 |
if (!v.type || // during variable type inference |
| 1633 |
!v.type.deco && v.inuse) // during variable type semantic |
| 1634 |
{ |
| 1635 |
if (v.inuse) // variable type depends on the variable itself |
| 1636 |
error(loc, "circular reference to %s `%s`", v.kind(), v.toPrettyChars()); |
| 1637 |
else // variable type cannot be determined |
| 1638 |
error(loc, "forward reference to %s `%s`", v.kind(), v.toPrettyChars()); |
| 1639 |
return ErrorExp.get(); |
| 1640 |
} |
| 1641 |
if (v.type.ty == Terror) |
| 1642 |
return ErrorExp.get(); |
| 1643 |
|
| 1644 |
if ((v.storage_class & STC.manifest) && v._init) |
| 1645 |
{ |
| 1646 |
if (v.inuse) |
| 1647 |
{ |
| 1648 |
error(loc, "circular initialization of %s `%s`", v.kind(), v.toPrettyChars()); |
| 1649 |
return ErrorExp.get(); |
| 1650 |
} |
| 1651 |
e = v.expandInitializer(loc); |
| 1652 |
v.inuse++; |
| 1653 |
e = e.expressionSemantic(sc); |
| 1654 |
v.inuse--; |
| 1655 |
return e; |
| 1656 |
} |
| 1657 |
|
| 1658 |
// We need to run semantics to correctly set 'STC.field' if it is a member variable |
| 1659 |
// that could be forward referenced. This is needed for 'v.needThis()' to work |
| 1660 |
if (v.isThis()) |
| 1661 |
v.dsymbolSemantic(sc); |
| 1662 |
|
| 1663 |
// Change the ancestor lambdas to delegate before hasThis(sc) call. |
| 1664 |
if (v.checkNestedReference(sc, loc)) |
| 1665 |
return ErrorExp.get(); |
| 1666 |
|
| 1667 |
if (v.needThis() && hasThis(sc)) |
| 1668 |
e = new DotVarExp(loc, new ThisExp(loc), v); |
| 1669 |
else |
| 1670 |
e = new VarExp(loc, v); |
| 1671 |
e = e.expressionSemantic(sc); |
| 1672 |
return e; |
| 1673 |
} |
| 1674 |
if (auto fld = s.isFuncLiteralDeclaration()) |
| 1675 |
{ |
| 1676 |
//printf("'%s' is a function literal\n", fld.toChars()); |
| 1677 |
e = new FuncExp(loc, fld); |
| 1678 |
return e.expressionSemantic(sc); |
| 1679 |
} |
| 1680 |
if (auto f = s.isFuncDeclaration()) |
| 1681 |
{ |
| 1682 |
f = f.toAliasFunc(); |
| 1683 |
if (!functionSemantic(f)) |
| 1684 |
return ErrorExp.get(); |
| 1685 |
|
| 1686 |
if (!hasOverloads && f.checkForwardRef(loc)) |
| 1687 |
return ErrorExp.get(); |
| 1688 |
|
| 1689 |
auto fd = s.isFuncDeclaration(); |
| 1690 |
fd.type = f.type; |
| 1691 |
return new VarExp(loc, fd, hasOverloads); |
| 1692 |
} |
| 1693 |
if (OverDeclaration od = s.isOverDeclaration()) |
| 1694 |
{ |
| 1695 |
e = new VarExp(loc, od, true); |
| 1696 |
e.type = Type.tvoid; |
| 1697 |
return e; |
| 1698 |
} |
| 1699 |
if (OverloadSet o = s.isOverloadSet()) |
| 1700 |
{ |
| 1701 |
//printf("'%s' is an overload set\n", o.toChars()); |
| 1702 |
return new OverExp(loc, o); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (Import imp = s.isImport()) |
| 1706 |
{ |
| 1707 |
if (!imp.pkg) |
| 1708 |
{ |
| 1709 |
.error(loc, "forward reference of import `%s`", imp.toChars()); |
| 1710 |
return ErrorExp.get(); |
| 1711 |
} |
| 1712 |
auto ie = new ScopeExp(loc, imp.pkg); |
| 1713 |
return ie.expressionSemantic(sc); |
| 1714 |
} |
| 1715 |
if (Package pkg = s.isPackage()) |
| 1716 |
{ |
| 1717 |
auto ie = new ScopeExp(loc, pkg); |
| 1718 |
return ie.expressionSemantic(sc); |
| 1719 |
} |
| 1720 |
if (Module mod = s.isModule()) |
| 1721 |
{ |
| 1722 |
auto ie = new ScopeExp(loc, mod); |
| 1723 |
return ie.expressionSemantic(sc); |
| 1724 |
} |
| 1725 |
if (Nspace ns = s.isNspace()) |
| 1726 |
{ |
| 1727 |
auto ie = new ScopeExp(loc, ns); |
| 1728 |
return ie.expressionSemantic(sc); |
| 1729 |
} |
| 1730 |
|
| 1731 |
if (Type t = s.getType()) |
| 1732 |
{ |
| 1733 |
return (new TypeExp(loc, t)).expressionSemantic(sc); |
| 1734 |
} |
| 1735 |
|
| 1736 |
if (TupleDeclaration tup = s.isTupleDeclaration()) |
| 1737 |
{ |
| 1738 |
if (tup.needThis() && hasThis(sc)) |
| 1739 |
e = new DotVarExp(loc, new ThisExp(loc), tup); |
| 1740 |
else |
| 1741 |
e = new TupleExp(loc, tup); |
| 1742 |
e = e.expressionSemantic(sc); |
| 1743 |
return e; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (TemplateInstance ti = s.isTemplateInstance()) |
| 1747 |
{ |
| 1748 |
ti.dsymbolSemantic(sc); |
| 1749 |
if (!ti.inst || ti.errors) |
| 1750 |
return ErrorExp.get(); |
| 1751 |
s = ti.toAlias(); |
| 1752 |
if (!s.isTemplateInstance()) |
| 1753 |
goto Lagain; |
| 1754 |
e = new ScopeExp(loc, ti); |
| 1755 |
e = e.expressionSemantic(sc); |
| 1756 |
return e; |
| 1757 |
} |
| 1758 |
if (TemplateDeclaration td = s.isTemplateDeclaration()) |
| 1759 |
{ |
| 1760 |
Dsymbol p = td.toParentLocal(); |
| 1761 |
FuncDeclaration fdthis = hasThis(sc); |
| 1762 |
AggregateDeclaration ad = p ? p.isAggregateDeclaration() : null; |
| 1763 |
if (fdthis && ad && fdthis.isMemberLocal() == ad && (td._scope.stc & STC.static_) == 0) |
| 1764 |
{ |
| 1765 |
e = new DotTemplateExp(loc, new ThisExp(loc), td); |
| 1766 |
} |
| 1767 |
else |
| 1768 |
e = new TemplateExp(loc, td); |
| 1769 |
e = e.expressionSemantic(sc); |
| 1770 |
return e; |
| 1771 |
} |
| 1772 |
|
| 1773 |
.error(loc, "%s `%s` is not a variable", s.kind(), s.toChars()); |
| 1774 |
return ErrorExp.get(); |
| 1775 |
} |
| 1776 |
|
| 1777 |
/************************************************************* |
| 1778 |
* Given var, get the |
| 1779 |
* right `this` pointer if var is in an outer class, but our |
| 1780 |
* existing `this` pointer is in an inner class. |
| 1781 |
* Params: |
| 1782 |
* loc = location to use for error messages |
| 1783 |
* sc = context |
| 1784 |
* ad = struct or class we need the correct `this` for |
| 1785 |
* e1 = existing `this` |
| 1786 |
* var = the specific member of ad we're accessing |
| 1787 |
* flag = if true, return `null` instead of throwing an error |
| 1788 |
* Returns: |
| 1789 |
* Expression representing the `this` for the var |
| 1790 |
*/ |
| 1791 |
private Expression getRightThis(Loc loc, Scope* sc, AggregateDeclaration ad, Expression e1, Dsymbol var, int flag = 0) |
| 1792 |
{ |
| 1793 |
//printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1.toChars(), ad.toChars(), var.toChars()); |
| 1794 |
L1: |
| 1795 |
Type t = e1.type.toBasetype(); |
| 1796 |
//printf("e1.type = %s, var.type = %s\n", e1.type.toChars(), var.type.toChars()); |
| 1797 |
|
| 1798 |
if (e1.op == EXP.objcClassReference) |
| 1799 |
{ |
| 1800 |
// We already have an Objective-C class reference, just use that as 'this'. |
| 1801 |
return e1; |
| 1802 |
} |
| 1803 |
else if (ad && ad.isClassDeclaration && ad.isClassDeclaration.classKind == ClassKind.objc && |
| 1804 |
var.isFuncDeclaration && var.isFuncDeclaration.isStatic && |
| 1805 |
var.isFuncDeclaration.objc.selector) |
| 1806 |
{ |
| 1807 |
auto cls = ad.isClassDeclaration(); |
| 1808 |
auto classObj = new ObjcClassReferenceExp(e1.loc, cls); |
| 1809 |
classObj.type = objc.getRuntimeMetaclass(cls).getType(); |
| 1810 |
return classObj; |
| 1811 |
} |
| 1812 |
|
| 1813 |
/* Access of a member which is a template parameter in dual-scope scenario |
| 1814 |
* class A { inc(alias m)() { ++m; } } // `m` needs `this` of `B` |
| 1815 |
* class B {int m; inc() { new A().inc!m(); } } |
| 1816 |
*/ |
| 1817 |
if (e1.op == EXP.this_) |
| 1818 |
{ |
| 1819 |
FuncDeclaration f = hasThis(sc); |
| 1820 |
if (f && f.hasDualContext()) |
| 1821 |
{ |
| 1822 |
if (f.followInstantiationContext(ad)) |
| 1823 |
{ |
| 1824 |
e1 = new VarExp(loc, f.vthis); |
| 1825 |
e1 = new PtrExp(loc, e1); |
| 1826 |
e1 = new IndexExp(loc, e1, IntegerExp.literal!1); |
| 1827 |
e1 = getThisSkipNestedFuncs(loc, sc, f.toParent2(), ad, e1, t, var); |
| 1828 |
if (e1.op == EXP.error) |
| 1829 |
return e1; |
| 1830 |
goto L1; |
| 1831 |
} |
| 1832 |
} |
| 1833 |
} |
| 1834 |
|
| 1835 |
/* If e1 is not the 'this' pointer for ad |
| 1836 |
*/ |
| 1837 |
if (ad && |
| 1838 |
!(t.isTypePointer() && t.nextOf().isTypeStruct() && t.nextOf().isTypeStruct().sym == ad) && |
| 1839 |
!(t.isTypeStruct() && t.isTypeStruct().sym == ad)) |
| 1840 |
{ |
| 1841 |
ClassDeclaration cd = ad.isClassDeclaration(); |
| 1842 |
ClassDeclaration tcd = t.isClassHandle(); |
| 1843 |
|
| 1844 |
/* e1 is the right this if ad is a base class of e1 |
| 1845 |
*/ |
| 1846 |
if (!cd || !tcd || !(tcd == cd || cd.isBaseOf(tcd, null))) |
| 1847 |
{ |
| 1848 |
/* Only classes can be inner classes with an 'outer' |
| 1849 |
* member pointing to the enclosing class instance |
| 1850 |
*/ |
| 1851 |
if (tcd && tcd.isNested()) |
| 1852 |
{ |
| 1853 |
/* e1 is the 'this' pointer for an inner class: tcd. |
| 1854 |
* Rewrite it as the 'this' pointer for the outer class. |
| 1855 |
*/ |
| 1856 |
auto vthis = tcd.followInstantiationContext(ad) ? tcd.vthis2 : tcd.vthis; |
| 1857 |
e1 = new DotVarExp(loc, e1, vthis); |
| 1858 |
e1.type = vthis.type; |
| 1859 |
e1.type = e1.type.addMod(t.mod); |
| 1860 |
// Do not call ensureStaticLinkTo() |
| 1861 |
//e1 = e1.semantic(sc); |
| 1862 |
|
| 1863 |
// Skip up over nested functions, and get the enclosing |
| 1864 |
// class type. |
| 1865 |
e1 = getThisSkipNestedFuncs(loc, sc, tcd.toParentP(ad), ad, e1, t, var); |
| 1866 |
if (e1.op == EXP.error) |
| 1867 |
return e1; |
| 1868 |
goto L1; |
| 1869 |
} |
| 1870 |
|
| 1871 |
/* Can't find a path from e1 to ad |
| 1872 |
*/ |
| 1873 |
if (flag) |
| 1874 |
return null; |
| 1875 |
error(e1.loc, "`this` for `%s` needs to be type `%s` not type `%s`", var.toChars(), ad.toChars(), t.toChars()); |
| 1876 |
return ErrorExp.get(); |
| 1877 |
} |
| 1878 |
} |
| 1879 |
return e1; |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* |
| 1883 |
* Check whether `outerFunc` and `calledFunc` have the same `this`. |
| 1884 |
* If `calledFunc` is the member of a base class of the class that contains |
| 1885 |
* `outerFunc` we consider that they have the same this. |
| 1886 |
* |
| 1887 |
* This function is used to test whether `this` needs to be prepended to |
| 1888 |
* a function call or function symbol. For example: |
| 1889 |
* |
| 1890 |
* struct X |
| 1891 |
* { |
| 1892 |
* void gun() {} |
| 1893 |
* } |
| 1894 |
* struct A |
| 1895 |
* { |
| 1896 |
* void fun() {} |
| 1897 |
* void sun() |
| 1898 |
* { |
| 1899 |
* fun(); |
| 1900 |
* X.gun(); // error |
| 1901 |
* } |
| 1902 |
* } |
| 1903 |
* |
| 1904 |
* When `fun` is called, `outerfunc` = `sun` and `calledFunc = `fun`. |
| 1905 |
* `sun` is a member of `A` and `fun` is also a member of `A`, therefore |
| 1906 |
* `this` can be prepended to `fun`. When `gun` is called (it will result |
| 1907 |
* in an error, but that is not relevant here), which is a member of `X`, |
| 1908 |
* no `this` is needed because the outer function does not have the same |
| 1909 |
* `this` as `gun`. |
| 1910 |
* |
| 1911 |
* Returns: |
| 1912 |
* `true` if outerFunc and calledFunc may use the same `this` pointer. |
| 1913 |
* `false` otherwise. |
| 1914 |
*/ |
| 1915 |
private bool haveSameThis(FuncDeclaration outerFunc, FuncDeclaration calledFunc) |
| 1916 |
{ |
| 1917 |
// https://issues.dlang.org/show_bug.cgi?id=24013 |
| 1918 |
// traits(getOverloads) inserts an alias to select the overload. |
| 1919 |
// When searching for the right this we need to use the aliased |
| 1920 |
// overload/function, not the alias. |
| 1921 |
outerFunc = outerFunc.toAliasFunc(); |
| 1922 |
calledFunc = calledFunc.toAliasFunc(); |
| 1923 |
|
| 1924 |
auto thisAd = outerFunc.isMemberLocal(); |
| 1925 |
if (!thisAd) |
| 1926 |
return false; |
| 1927 |
|
| 1928 |
auto requiredAd = calledFunc.isMemberLocal(); |
| 1929 |
if (!requiredAd) |
| 1930 |
return false; |
| 1931 |
|
| 1932 |
if (thisAd == requiredAd) |
| 1933 |
return true; |
| 1934 |
|
| 1935 |
// if outerfunc is the member of a nested aggregate, then let |
| 1936 |
// getRightThis take care of this. |
| 1937 |
if (thisAd.isNested()) |
| 1938 |
return true; |
| 1939 |
|
| 1940 |
// outerfunc is the member of a base class that contains calledFunc, |
| 1941 |
// then we consider that they have the same this. |
| 1942 |
auto cd = requiredAd.isClassDeclaration(); |
| 1943 |
if (!cd) |
| 1944 |
return false; |
| 1945 |
|
| 1946 |
if (cd.isBaseOf2(thisAd.isClassDeclaration())) |
| 1947 |
return true; |
| 1948 |
|
| 1949 |
return false; |
| 1950 |
} |
| 1951 |
|
| 1952 |
/********************************************* |
| 1953 |
* Calling function f. |
| 1954 |
* Check the purity, i.e. if we're in a pure function |
| 1955 |
* we can only call other pure functions. |
| 1956 |
* Returns true if error occurs. |
| 1957 |
*/ |
| 1958 |
private bool checkPurity(FuncDeclaration f, Loc loc, Scope* sc) |
| 1959 |
{ |
| 1960 |
if (!sc.func) |
| 1961 |
return false; |
| 1962 |
if (sc.func == f) |
| 1963 |
return false; |
| 1964 |
if (sc.intypeof == 1) |
| 1965 |
return false; |
| 1966 |
if (sc.ctfe || sc.debug_) |
| 1967 |
return false; |
| 1968 |
|
| 1969 |
// If the call has a pure parent, then the called func must be pure. |
| 1970 |
if (!f.isPure() && checkImpure(sc, loc, null, f)) |
| 1971 |
{ |
| 1972 |
error(loc, "`pure` %s `%s` cannot call impure %s `%s`", |
| 1973 |
sc.func.kind(), sc.func.toPrettyChars(), f.kind(), |
| 1974 |
f.toPrettyChars()); |
| 1975 |
|
| 1976 |
if (!f.isDtorDeclaration()) |
| 1977 |
errorSupplementalInferredAttr(f, /*max depth*/ 10, /*deprecation*/ false, STC.pure_, global.errorSink); |
| 1978 |
|
| 1979 |
f.checkOverriddenDtor(sc, loc, dd => dd.type.toTypeFunction().purity != PURE.impure, "impure"); |
| 1980 |
return true; |
| 1981 |
} |
| 1982 |
return false; |
| 1983 |
} |
| 1984 |
|
| 1985 |
/** |
| 1986 |
* Checks whether `f` is a generated `DtorDeclaration` that hides a user-defined one |
| 1987 |
* which passes `check` while `f` doesn't (e.g. when the user defined dtor is pure but |
| 1988 |
* the generated dtor is not). |
| 1989 |
* In that case the method will identify and print all members causing the attribute |
| 1990 |
* missmatch. |
| 1991 |
* |
| 1992 |
* Params: |
| 1993 |
* f = potential `DtorDeclaration` |
| 1994 |
* sc = scope |
| 1995 |
* loc = location |
| 1996 |
* check = current check (e.g. whether it's pure) |
| 1997 |
* checkName = the kind of check (e.g. `"pure"`) |
| 1998 |
*/ |
| 1999 |
void checkOverriddenDtor(FuncDeclaration f, Scope* sc, Loc loc, |
| 2000 |
scope bool function(DtorDeclaration) check, const string checkName) |
| 2001 |
{ |
| 2002 |
auto dd = f.isDtorDeclaration(); |
| 2003 |
if (!dd || !dd.isGenerated()) |
| 2004 |
return; |
| 2005 |
|
| 2006 |
// DtorDeclaration without parents should fail at an earlier stage |
| 2007 |
auto ad = cast(AggregateDeclaration) f.toParent2(); |
| 2008 |
assert(ad); |
| 2009 |
|
| 2010 |
if (ad.userDtors.length) |
| 2011 |
{ |
| 2012 |
if (!check(ad.userDtors[0])) // doesn't match check (e.g. is impure as well) |
| 2013 |
return; |
| 2014 |
|
| 2015 |
// Sanity check |
| 2016 |
assert(!check(ad.fieldDtor)); |
| 2017 |
} |
| 2018 |
|
| 2019 |
dd.loc.errorSupplemental("%s`%s.~this` is %.*s because of the following field's destructors:", |
| 2020 |
dd.isGenerated() ? "generated " : "".ptr, |
| 2021 |
ad.toChars, |
| 2022 |
cast(int) checkName.length, checkName.ptr); |
| 2023 |
|
| 2024 |
// Search for the offending fields |
| 2025 |
foreach (field; ad.fields) |
| 2026 |
{ |
| 2027 |
// Only structs may define automatically called destructors |
| 2028 |
auto ts = field.type.isTypeStruct(); |
| 2029 |
if (!ts) |
| 2030 |
{ |
| 2031 |
// But they might be part of a static array |
| 2032 |
auto ta = field.type.isTypeSArray(); |
| 2033 |
if (!ta) |
| 2034 |
continue; |
| 2035 |
|
| 2036 |
ts = ta.baseElemOf().isTypeStruct(); |
| 2037 |
if (!ts) |
| 2038 |
continue; |
| 2039 |
} |
| 2040 |
|
| 2041 |
auto fieldSym = ts.toDsymbol(sc); |
| 2042 |
assert(fieldSym); // Resolving ts must succeed because missing defs. should error before |
| 2043 |
|
| 2044 |
auto fieldSd = fieldSym.isStructDeclaration(); |
| 2045 |
assert(fieldSd); // ts is a TypeStruct, this would imply a malformed ASR |
| 2046 |
|
| 2047 |
if (fieldSd.dtor && !check(fieldSd.dtor)) |
| 2048 |
{ |
| 2049 |
field.loc.errorSupplemental(" - %s %s", field.type.toChars(), field.toChars()); |
| 2050 |
|
| 2051 |
if (fieldSd.dtor.isGenerated()) |
| 2052 |
fieldSd.dtor.checkOverriddenDtor(sc, loc, check, checkName); |
| 2053 |
else |
| 2054 |
fieldSd.dtor.loc.errorSupplemental(" %.*s `%s.~this` is declared here", |
| 2055 |
cast(int) checkName.length, checkName.ptr, fieldSd.toChars()); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
} |
| 2059 |
|
| 2060 |
/******************************************** |
| 2061 |
* Print the reason why `fd` was inferred `@system` as a supplemental error |
| 2062 |
* Params: |
| 2063 |
* fd = function to check |
| 2064 |
* maxDepth = up to how many functions deep to report errors |
| 2065 |
* deprecation = print deprecations instead of errors |
| 2066 |
* stc = storage class of attribute to check |
| 2067 |
* eSink = where the error messages go |
| 2068 |
*/ |
| 2069 |
public void errorSupplementalInferredAttr(FuncDeclaration fd, int maxDepth, bool deprecation, STC stc, ErrorSink eSink) |
| 2070 |
{ |
| 2071 |
auto errorFunc = deprecation ? &eSink.deprecationSupplemental : &eSink.errorSupplemental; |
| 2072 |
|
| 2073 |
AttributeViolation* s; |
| 2074 |
string attr; |
| 2075 |
if (stc & STC.safe) |
| 2076 |
{ |
| 2077 |
s = fd.safetyViolation; |
| 2078 |
attr = "@safe"; |
| 2079 |
} |
| 2080 |
else if (stc & STC.pure_) |
| 2081 |
{ |
| 2082 |
s = fd.pureViolation; |
| 2083 |
attr = "pure"; |
| 2084 |
} |
| 2085 |
else if (stc & STC.nothrow_) |
| 2086 |
{ |
| 2087 |
s = fd.nothrowViolation; |
| 2088 |
attr = "nothrow"; |
| 2089 |
} |
| 2090 |
else if (stc & STC.nogc) |
| 2091 |
{ |
| 2092 |
s = fd.nogcViolation; |
| 2093 |
attr = "@nogc"; |
| 2094 |
} |
| 2095 |
|
| 2096 |
if (!s) |
| 2097 |
return; |
| 2098 |
|
| 2099 |
if (s.action.length > 0) |
| 2100 |
{ |
| 2101 |
errorFunc(s.loc, "and %.*s makes it fail to infer `%.*s`", s.action.fTuple.expand, attr.fTuple.expand); |
| 2102 |
} |
| 2103 |
else if (s.fd) |
| 2104 |
{ |
| 2105 |
if (maxDepth > 0) |
| 2106 |
{ |
| 2107 |
errorFunc(s.loc, "which calls `%s`", s.fd.toErrMsg()); |
| 2108 |
errorSupplementalInferredAttr(s.fd, maxDepth - 1, deprecation, stc, eSink); |
| 2109 |
} |
| 2110 |
} |
| 2111 |
else |
| 2112 |
assert(0); |
| 2113 |
} |
| 2114 |
|
| 2115 |
/******************************************* |
| 2116 |
* Accessing variable v. |
| 2117 |
* Check for purity and safety violations. |
| 2118 |
* Returns true if error occurs. |
| 2119 |
*/ |
| 2120 |
private bool checkPurity(VarDeclaration v, Loc loc, Scope* sc) |
| 2121 |
{ |
| 2122 |
//printf("v = %s %s\n", v.type.toChars(), v.toChars()); |
| 2123 |
/* Look for purity and safety violations when accessing variable v |
| 2124 |
* from current function. |
| 2125 |
*/ |
| 2126 |
if (!sc.func) |
| 2127 |
return false; |
| 2128 |
if (sc.intypeof == 1) |
| 2129 |
return false; // allow violations inside typeof(expression) |
| 2130 |
if (sc.ctfe || sc.debug_) |
| 2131 |
return false; // allow violations inside compile-time evaluated expressions and debug conditionals |
| 2132 |
if (v.ident == Id.ctfe) |
| 2133 |
return false; // magic variable never violates pure and safe |
| 2134 |
if (v.isImmutable()) |
| 2135 |
return false; // always safe and pure to access immutables... |
| 2136 |
if (v.isConst() && !v.isReference() && (v.isDataseg() || v.isParameter()) && v.type.implicitConvTo(v.type.immutableOf())) |
| 2137 |
return false; // or const global/parameter values which have no mutable indirections |
| 2138 |
if (v.storage_class & STC.manifest) |
| 2139 |
return false; // ...or manifest constants |
| 2140 |
|
| 2141 |
// accessing empty structs is pure |
| 2142 |
// https://issues.dlang.org/show_bug.cgi?id=18694 |
| 2143 |
// https://issues.dlang.org/show_bug.cgi?id=21464 |
| 2144 |
// https://issues.dlang.org/show_bug.cgi?id=23589 |
| 2145 |
if (v.type.ty == Tstruct) |
| 2146 |
{ |
| 2147 |
StructDeclaration sd = (cast(TypeStruct)v.type).sym; |
| 2148 |
if (sd.members) // not opaque |
| 2149 |
{ |
| 2150 |
if (sd.semanticRun >= PASS.semanticdone) |
| 2151 |
sd.determineSize(v.loc); |
| 2152 |
if (sd.hasNoFields) |
| 2153 |
return false; |
| 2154 |
} |
| 2155 |
} |
| 2156 |
|
| 2157 |
bool err = false; |
| 2158 |
if (v.isDataseg()) |
| 2159 |
{ |
| 2160 |
// https://issues.dlang.org/show_bug.cgi?id=7533 |
| 2161 |
// Accessing implicit generated __gate is pure. |
| 2162 |
if (v.ident == Id.gate) |
| 2163 |
return false; |
| 2164 |
|
| 2165 |
if (checkImpure(sc, loc, "accessing mutable static data `%s`", v)) |
| 2166 |
{ |
| 2167 |
error(loc, "`pure` %s `%s` cannot access mutable static data `%s`", |
| 2168 |
sc.func.kind(), sc.func.toPrettyChars(), v.toChars()); |
| 2169 |
err = true; |
| 2170 |
} |
| 2171 |
} |
| 2172 |
else |
| 2173 |
{ |
| 2174 |
/* Given: |
| 2175 |
* void f() { |
| 2176 |
* int fx; |
| 2177 |
* pure void g() { |
| 2178 |
* int gx; |
| 2179 |
* /+pure+/ void h() { |
| 2180 |
* int hx; |
| 2181 |
* /+pure+/ void i() { } |
| 2182 |
* } |
| 2183 |
* } |
| 2184 |
* } |
| 2185 |
* i() can modify hx and gx but not fx |
| 2186 |
*/ |
| 2187 |
|
| 2188 |
Dsymbol vparent = v.toParent2(); |
| 2189 |
for (Dsymbol s = sc.func; !err && s; s = s.toParentP(vparent)) |
| 2190 |
{ |
| 2191 |
if (s == vparent) |
| 2192 |
break; |
| 2193 |
|
| 2194 |
if (AggregateDeclaration ad = s.isAggregateDeclaration()) |
| 2195 |
{ |
| 2196 |
if (ad.isNested()) |
| 2197 |
continue; |
| 2198 |
break; |
| 2199 |
} |
| 2200 |
FuncDeclaration ff = s.isFuncDeclaration(); |
| 2201 |
if (!ff) |
| 2202 |
break; |
| 2203 |
if (!ff.isNested() && !ff.isThis()) |
| 2204 |
break; |
| 2205 |
if (ff.type.isImmutable() || |
| 2206 |
ff.type.isShared() && !MODimplicitConv(ff.type.mod, v.type.mod)) |
| 2207 |
{ |
| 2208 |
OutBuffer ffbuf; |
| 2209 |
OutBuffer vbuf; |
| 2210 |
MODMatchToBuffer(&ffbuf, ff.type.mod, v.type.mod); |
| 2211 |
MODMatchToBuffer(&vbuf, v.type.mod, ff.type.mod); |
| 2212 |
error(loc, "%s%s `%s` cannot access %sdata `%s`", |
| 2213 |
ffbuf.peekChars(), ff.kind(), ff.toPrettyChars(), vbuf.peekChars(), v.toChars()); |
| 2214 |
err = true; |
| 2215 |
break; |
| 2216 |
} |
| 2217 |
continue; |
| 2218 |
} |
| 2219 |
} |
| 2220 |
|
| 2221 |
/* Do not allow safe functions to access __gshared data |
| 2222 |
*/ |
| 2223 |
if (v.storage_class & STC.gshared) |
| 2224 |
{ |
| 2225 |
if (sc.setUnsafe(false, loc, "accessing `__gshared` data `%s`", v)) |
| 2226 |
{ |
| 2227 |
err = true; |
| 2228 |
} |
| 2229 |
} |
| 2230 |
|
| 2231 |
return err; |
| 2232 |
} |
| 2233 |
|
| 2234 |
/* |
| 2235 |
Check if sc.func is impure or can be made impure. |
| 2236 |
Returns true on error, i.e. if sc.func is pure and cannot be made impure. |
| 2237 |
*/ |
| 2238 |
private bool checkImpure(Scope* sc, Loc loc, const(char)* fmt, RootObject arg0) |
| 2239 |
{ |
| 2240 |
return sc.func && (isRootTraitsCompilesScope(sc) |
| 2241 |
? sc.func.isPureBypassingInference() >= PURE.weak |
| 2242 |
: sc.func.setImpure(loc, fmt, arg0)); |
| 2243 |
} |
| 2244 |
|
| 2245 |
/********************************************* |
| 2246 |
* Calling function f. |
| 2247 |
* Check the safety, i.e. if we're in a @safe function |
| 2248 |
* we can only call @safe or @trusted functions. |
| 2249 |
* Returns true if error occurs. |
| 2250 |
*/ |
| 2251 |
private bool checkSafety(FuncDeclaration f, ref Loc loc, Scope* sc) |
| 2252 |
{ |
| 2253 |
if (sc.func == f) |
| 2254 |
return false; |
| 2255 |
if (sc.intypeof == 1) |
| 2256 |
return false; |
| 2257 |
if (sc.debug_) |
| 2258 |
return false; |
| 2259 |
if (sc.ctfe && sc.func) |
| 2260 |
return false; |
| 2261 |
|
| 2262 |
if (!sc.func) |
| 2263 |
{ |
| 2264 |
if (sc.varDecl && !f.safetyInprocess && !f.isSafe() && !f.isTrusted()) |
| 2265 |
{ |
| 2266 |
if (sc.varDecl.storage_class & STC.safe) |
| 2267 |
{ |
| 2268 |
error(loc, "`@safe` variable `%s` cannot be initialized by calling `@system` function `%s`", |
| 2269 |
sc.varDecl.toChars(), f.toChars()); |
| 2270 |
return true; |
| 2271 |
} |
| 2272 |
else |
| 2273 |
{ |
| 2274 |
sc.varDecl.storage_class |= STC.system; |
| 2275 |
sc.varDecl.systemInferred = true; |
| 2276 |
} |
| 2277 |
} |
| 2278 |
return false; |
| 2279 |
} |
| 2280 |
|
| 2281 |
if (!f.isSafe() && !f.isTrusted()) |
| 2282 |
{ |
| 2283 |
if (isRootTraitsCompilesScope(sc) ? sc.func.isSafeBypassingInference() : sc.func.setUnsafeCall(f)) |
| 2284 |
{ |
| 2285 |
if (!loc.isValid()) // e.g. implicitly generated dtor |
| 2286 |
loc = sc.func.loc; |
| 2287 |
|
| 2288 |
const prettyChars = f.toPrettyChars(); |
| 2289 |
error(loc, "`@safe` %s `%s` cannot call `@system` %s `%s`", |
| 2290 |
sc.func.kind(), sc.func.toPrettyChars(), f.kind(), |
| 2291 |
prettyChars); |
| 2292 |
if (!f.isDtorDeclaration) |
| 2293 |
errorSupplementalInferredAttr(f, /*max depth*/ 10, /*deprecation*/ false, STC.safe, global.errorSink); |
| 2294 |
.errorSupplemental(f.loc, "`%s` is declared here", prettyChars); |
| 2295 |
|
| 2296 |
f.checkOverriddenDtor(sc, loc, dd => dd.type.toTypeFunction().trust > TRUST.system, "@system"); |
| 2297 |
|
| 2298 |
return true; |
| 2299 |
} |
| 2300 |
} |
| 2301 |
else if (f.isSafe() && f.safetyViolation) |
| 2302 |
{ |
| 2303 |
// for dip1000 by default transition, print deprecations for calling functions that will become `@system` |
| 2304 |
if (sc.func.isSafeBypassingInference()) |
| 2305 |
{ |
| 2306 |
.deprecation(loc, "`@safe` function `%s` calling `%s`", sc.func.toChars(), f.toChars()); |
| 2307 |
errorSupplementalInferredAttr(f, 10, true, STC.safe, global.errorSink); |
| 2308 |
} |
| 2309 |
else if (!sc.func.safetyViolation) |
| 2310 |
{ |
| 2311 |
import dmd.func : AttributeViolation; |
| 2312 |
sc.func.safetyViolation = new AttributeViolation(loc, f); |
| 2313 |
} |
| 2314 |
} |
| 2315 |
return false; |
| 2316 |
} |
| 2317 |
|
| 2318 |
/********************************************* |
| 2319 |
* Calling function f. |
| 2320 |
* Check the @nogc-ness, i.e. if we're in a @nogc function |
| 2321 |
* we can only call other @nogc functions. |
| 2322 |
* Returns true if error occurs. |
| 2323 |
*/ |
| 2324 |
private bool checkNogc(FuncDeclaration f, ref Loc loc, Scope* sc) |
| 2325 |
{ |
| 2326 |
if (!sc.func) |
| 2327 |
return false; |
| 2328 |
if (sc.func == f) |
| 2329 |
return false; |
| 2330 |
if (sc.intypeof == 1) |
| 2331 |
return false; |
| 2332 |
if (sc.ctfe || sc.debug_) |
| 2333 |
return false; |
| 2334 |
/* The original expressions (`new S(...)` or `new S[...]``) will be |
| 2335 |
* verified instead. This is to keep errors related to the original code |
| 2336 |
* and not the lowering. |
| 2337 |
*/ |
| 2338 |
if (f.ident == Id._d_newitemT || f.ident == Id._d_newarrayT || f.ident == Id._d_newarraymTX) |
| 2339 |
return false; |
| 2340 |
|
| 2341 |
if (f.isNogc()) |
| 2342 |
return false; |
| 2343 |
|
| 2344 |
if (isRootTraitsCompilesScope(sc) ? !sc.func.isNogcBypassingInference() : !sc.func.setGCCall(f)) |
| 2345 |
return false; |
| 2346 |
|
| 2347 |
if (loc == Loc.initial) // e.g. implicitly generated dtor |
| 2348 |
loc = sc.func.loc; |
| 2349 |
|
| 2350 |
// Lowered non-@nogc'd hooks will print their own error message inside of nogc.d (NOGCVisitor.visit(CallExp e)), |
| 2351 |
// so don't print anything to avoid double error messages. |
| 2352 |
if (!(f.ident == Id._d_HookTraceImpl || f.ident == Id._d_arraysetlengthT |
| 2353 |
|| f.ident == Id._d_arrayappendT || f.ident == Id._d_arrayappendcTX |
| 2354 |
|| f.ident == Id._d_arraycatnTX || f.ident == Id._d_newclassT)) |
| 2355 |
{ |
| 2356 |
error(loc, "`@nogc` %s `%s` cannot call non-@nogc %s `%s`", |
| 2357 |
sc.func.kind(), sc.func.toPrettyChars(), f.kind(), f.toPrettyChars()); |
| 2358 |
|
| 2359 |
if (!f.isDtorDeclaration) |
| 2360 |
f.errorSupplementalInferredAttr(/*max depth*/ 10, /*deprecation*/ false, STC.nogc, global.errorSink); |
| 2361 |
} |
| 2362 |
|
| 2363 |
f.checkOverriddenDtor(sc, loc, dd => dd.type.toTypeFunction().isNogc, "non-@nogc"); |
| 2364 |
|
| 2365 |
return true; |
| 2366 |
} |
| 2367 |
|
| 2368 |
/******************************************** |
| 2369 |
* Check that the postblit of `t` isn't @disabled and has the right |
| 2370 |
* function attributes for this scope. |
| 2371 |
* |
| 2372 |
* Params: |
| 2373 |
* t = struct type, or static array of struct type to check |
| 2374 |
* loc = error message location |
| 2375 |
* sc = scope in which attributes are checked |
| 2376 |
* Returns: true if there's an error |
| 2377 |
*/ |
| 2378 |
private bool checkPostblit(Type t, ref Loc loc, Scope* sc) |
| 2379 |
{ |
| 2380 |
auto ts = t.baseElemOf().isTypeStruct(); |
| 2381 |
if (!ts) |
| 2382 |
return false; |
| 2383 |
|
| 2384 |
if (global.params.useTypeInfo && Type.dtypeinfo) |
| 2385 |
{ |
| 2386 |
// https://issues.dlang.org/show_bug.cgi?id=11395 |
| 2387 |
// Require TypeInfo generation for array concatenation |
| 2388 |
semanticTypeInfo(sc, t); |
| 2389 |
} |
| 2390 |
|
| 2391 |
StructDeclaration sd = ts.sym; |
| 2392 |
if (!sd.postblit) |
| 2393 |
return false; |
| 2394 |
|
| 2395 |
if (sd.postblit.checkDisabled(loc, sc)) |
| 2396 |
return true; |
| 2397 |
|
| 2398 |
//checkDeprecated(sc, sd.postblit); // necessary? |
| 2399 |
//checkAccess(sd, loc, sc, sd.postblit); // necessary? |
| 2400 |
bool result = false; |
| 2401 |
result |= sd.postblit.checkPurity(loc, sc); |
| 2402 |
result |= sd.postblit.checkSafety(loc, sc); |
| 2403 |
result |= sd.postblit.checkNogc(loc, sc); |
| 2404 |
return result; |
| 2405 |
} |
| 2406 |
|
| 2407 |
/*************************************** |
| 2408 |
* Pull out any properties. |
| 2409 |
*/ |
| 2410 |
private Expression resolvePropertiesX(Scope* sc, Expression e1, Expression e2 = null, Type[2]* aliasThisStop = null) |
| 2411 |
{ |
| 2412 |
//printf("resolvePropertiesX, e1 = %s %s, e2 = %s\n", EXPtoString(e1.op).ptr, e1.toChars(), e2 ? e2.toChars() : null); |
| 2413 |
Loc loc = e1.loc; |
| 2414 |
|
| 2415 |
OverloadSet os; |
| 2416 |
Dsymbol s; |
| 2417 |
Objects* tiargs; |
| 2418 |
Type tthis; |
| 2419 |
if (auto de = e1.isDotExp()) |
| 2420 |
{ |
| 2421 |
if (auto oe = de.e2.isOverExp()) |
| 2422 |
{ |
| 2423 |
tiargs = null; |
| 2424 |
tthis = de.e1.type; |
| 2425 |
os = oe.vars; |
| 2426 |
goto Los; |
| 2427 |
} |
| 2428 |
} |
| 2429 |
else if (e1.isOverExp()) |
| 2430 |
{ |
| 2431 |
tiargs = null; |
| 2432 |
tthis = null; |
| 2433 |
os = e1.isOverExp().vars; |
| 2434 |
Los: |
| 2435 |
assert(os); |
| 2436 |
FuncDeclaration fd = null; |
| 2437 |
if (e2) |
| 2438 |
{ |
| 2439 |
e2 = e2.expressionSemantic(sc); |
| 2440 |
if (e2.op == EXP.error) |
| 2441 |
return ErrorExp.get(); |
| 2442 |
e2 = resolveProperties(sc, e2); |
| 2443 |
|
| 2444 |
Expressions* a = new Expressions(); |
| 2445 |
a.push(e2); |
| 2446 |
|
| 2447 |
for (size_t i = 0; i < os.a.length; i++) |
| 2448 |
{ |
| 2449 |
if (FuncDeclaration f = resolveFuncCall(loc, sc, os.a[i], tiargs, tthis, ArgumentList(a), FuncResolveFlag.quiet)) |
| 2450 |
{ |
| 2451 |
if (f.errors) |
| 2452 |
return ErrorExp.get(); |
| 2453 |
fd = f; |
| 2454 |
assert(fd.type.ty == Tfunction); |
| 2455 |
} |
| 2456 |
} |
| 2457 |
if (fd) |
| 2458 |
{ |
| 2459 |
Expression e = new CallExp(loc, e1, e2); |
| 2460 |
return e.expressionSemantic(sc); |
| 2461 |
} |
| 2462 |
} |
| 2463 |
for (size_t i = 0; i < os.a.length; i++) |
| 2464 |
{ |
| 2465 |
FuncDeclaration f = resolveFuncCall(loc, sc, os.a[i], tiargs, tthis, ArgumentList(), FuncResolveFlag.quiet); |
| 2466 |
if (!f) |
| 2467 |
continue; |
| 2468 |
if (f.errors) |
| 2469 |
return ErrorExp.get(); |
| 2470 |
fd = f; |
| 2471 |
assert(fd.type.ty == Tfunction); |
| 2472 |
auto tf = fd.type.isTypeFunction(); |
| 2473 |
if (!tf.isRef && e2) |
| 2474 |
{ |
| 2475 |
error(loc, "%s is not an lvalue", e1.toErrMsg()); |
| 2476 |
return ErrorExp.get(); |
| 2477 |
} |
| 2478 |
} |
| 2479 |
if (fd) |
| 2480 |
{ |
| 2481 |
Expression e = new CallExp(loc, e1); |
| 2482 |
if (e2) |
| 2483 |
{ |
| 2484 |
e = new AssignExp(loc, e, e2); |
| 2485 |
if (aliasThisStop) |
| 2486 |
return e.expressionSemantic(sc, *aliasThisStop); |
| 2487 |
return e.expressionSemantic(sc); |
| 2488 |
} |
| 2489 |
return e.expressionSemantic(sc); |
| 2490 |
} |
| 2491 |
if (e2) |
| 2492 |
goto Leprop; |
| 2493 |
} |
| 2494 |
else if (auto dti = e1.isDotTemplateInstanceExp()) |
| 2495 |
{ |
| 2496 |
if (!dti.findTempDecl(sc)) |
| 2497 |
goto Leprop; |
| 2498 |
if (!dti.ti.semanticTiargs(sc)) |
| 2499 |
goto Leprop; |
| 2500 |
tiargs = dti.ti.tiargs; |
| 2501 |
tthis = dti.e1.type; |
| 2502 |
if ((os = dti.ti.tempdecl.isOverloadSet()) !is null) |
| 2503 |
goto Los; |
| 2504 |
if ((s = dti.ti.tempdecl) !is null) |
| 2505 |
goto Lfd; |
| 2506 |
} |
| 2507 |
else if (auto dte = e1.isDotTemplateExp()) |
| 2508 |
{ |
| 2509 |
s = dte.td; |
| 2510 |
tiargs = null; |
| 2511 |
tthis = dte.e1.type; |
| 2512 |
goto Lfd; |
| 2513 |
} |
| 2514 |
else if (auto se = e1.isScopeExp()) |
| 2515 |
{ |
| 2516 |
s = se.sds; |
| 2517 |
TemplateInstance ti = s.isTemplateInstance(); |
| 2518 |
if (ti && !ti.semanticRun && ti.tempdecl) |
| 2519 |
{ |
| 2520 |
//assert(ti.needsTypeInference(sc)); |
| 2521 |
if (!ti.semanticTiargs(sc)) |
| 2522 |
goto Leprop; |
| 2523 |
tiargs = ti.tiargs; |
| 2524 |
tthis = null; |
| 2525 |
if ((os = ti.tempdecl.isOverloadSet()) !is null) |
| 2526 |
goto Los; |
| 2527 |
if ((s = ti.tempdecl) !is null) |
| 2528 |
goto Lfd; |
| 2529 |
} |
| 2530 |
} |
| 2531 |
else if (auto te = e1.isTemplateExp()) |
| 2532 |
{ |
| 2533 |
s = te.td; |
| 2534 |
tiargs = null; |
| 2535 |
tthis = null; |
| 2536 |
goto Lfd; |
| 2537 |
} |
| 2538 |
else if (e1.isDotVarExp() && e1.type && (e1.type.toBasetype().isTypeFunction() || e1.isDotVarExp().var.isOverDeclaration())) |
| 2539 |
{ |
| 2540 |
DotVarExp dve = e1.isDotVarExp(); |
| 2541 |
s = dve.var; |
| 2542 |
tiargs = null; |
| 2543 |
tthis = dve.e1.type; |
| 2544 |
goto Lfd; |
| 2545 |
} |
| 2546 |
else if (sc && sc.inCfile && e1.isVarExp() && !e2) |
| 2547 |
{ |
| 2548 |
// ImportC: do not implicitly call function if no ( ) are present |
| 2549 |
} |
| 2550 |
else if (e1.isVarExp() && e1.type && (e1.type.toBasetype().isTypeFunction() || e1.isVarExp().var.isOverDeclaration())) |
| 2551 |
{ |
| 2552 |
s = e1.isVarExp().var; |
| 2553 |
tiargs = null; |
| 2554 |
tthis = null; |
| 2555 |
Lfd: |
| 2556 |
assert(s); |
| 2557 |
if (e2) |
| 2558 |
{ |
| 2559 |
e2 = e2.expressionSemantic(sc); |
| 2560 |
if (e2.op == EXP.error) |
| 2561 |
return ErrorExp.get(); |
| 2562 |
e2 = resolveProperties(sc, e2); |
| 2563 |
|
| 2564 |
Expressions* a = new Expressions(); |
| 2565 |
a.push(e2); |
| 2566 |
|
| 2567 |
FuncDeclaration fd = resolveFuncCall(loc, sc, s, tiargs, tthis, ArgumentList(a), FuncResolveFlag.quiet); |
| 2568 |
if (fd && fd.type) |
| 2569 |
{ |
| 2570 |
if (fd.errors) |
| 2571 |
return ErrorExp.get(); |
| 2572 |
assert(fd.type.ty == Tfunction); |
| 2573 |
Expression e = new CallExp(loc, e1, e2); |
| 2574 |
return e.expressionSemantic(sc); |
| 2575 |
} |
| 2576 |
} |
| 2577 |
FuncDeclaration fd = resolveFuncCall(loc, sc, s, tiargs, tthis, ArgumentList(), FuncResolveFlag.quiet); |
| 2578 |
if (fd && fd.type) |
| 2579 |
{ |
| 2580 |
if (fd.errors) |
| 2581 |
return ErrorExp.get(); |
| 2582 |
TypeFunction tf = fd.type.isTypeFunction(); |
| 2583 |
if (!e2 || tf.isRef) |
| 2584 |
{ |
| 2585 |
Expression e = new CallExp(loc, e1); |
| 2586 |
if (e2) |
| 2587 |
{ |
| 2588 |
e = new AssignExp(loc, e, e2); |
| 2589 |
if (aliasThisStop) |
| 2590 |
return e.expressionSemantic(sc, *aliasThisStop); |
| 2591 |
} |
| 2592 |
return e.expressionSemantic(sc); |
| 2593 |
} |
| 2594 |
} |
| 2595 |
if (FuncDeclaration fd2 = s.isFuncDeclaration()) |
| 2596 |
{ |
| 2597 |
// Keep better diagnostic message for invalid property usage of functions |
| 2598 |
assert(fd2.type.ty == Tfunction); |
| 2599 |
Expression e = new CallExp(loc, e1, e2); |
| 2600 |
return e.expressionSemantic(sc); |
| 2601 |
} |
| 2602 |
if (e2) |
| 2603 |
goto Leprop; |
| 2604 |
} |
| 2605 |
if (auto ve = e1.isVarExp()) |
| 2606 |
{ |
| 2607 |
if (auto v = ve.var.isVarDeclaration()) |
| 2608 |
{ |
| 2609 |
if (v.checkPurity(ve.loc, sc)) |
| 2610 |
return ErrorExp.get(); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
if (e2) |
| 2614 |
return null; |
| 2615 |
|
| 2616 |
if (e1.type && !e1.isTypeExp()) // function type is not a property |
| 2617 |
{ |
| 2618 |
/* Look for e1 being a lazy parameter; rewrite as delegate call |
| 2619 |
* only if the symbol wasn't already treated as a delegate |
| 2620 |
*/ |
| 2621 |
auto ve = e1.isVarExp(); |
| 2622 |
if (ve && ve.var.storage_class & STC.lazy_ && !ve.delegateWasExtracted) |
| 2623 |
{ |
| 2624 |
Expression e = new CallExp(loc, e1); |
| 2625 |
return e.expressionSemantic(sc); |
| 2626 |
} |
| 2627 |
else if (e1.isDotVarExp()) |
| 2628 |
{ |
| 2629 |
// Check for reading overlapped pointer field in @safe code. |
| 2630 |
if (checkUnsafeAccess(sc, e1, true, true)) |
| 2631 |
return ErrorExp.get(); |
| 2632 |
} |
| 2633 |
else if (auto ce = e1.isCallExp()) |
| 2634 |
{ |
| 2635 |
// Check for reading overlapped pointer field in @safe code. |
| 2636 |
if (checkUnsafeAccess(sc, ce.e1, true, true)) |
| 2637 |
return ErrorExp.get(); |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
if (!e1.type) |
| 2642 |
{ |
| 2643 |
error(loc, "cannot resolve type for %s", e1.toErrMsg()); |
| 2644 |
e1 = ErrorExp.get(); |
| 2645 |
} |
| 2646 |
return e1; |
| 2647 |
|
| 2648 |
Leprop: |
| 2649 |
error(loc, "not a property %s", e1.toErrMsg()); |
| 2650 |
return ErrorExp.get(); |
| 2651 |
} |
| 2652 |
|
| 2653 |
private bool checkRightThis(Expression e, Scope* sc) |
| 2654 |
{ |
| 2655 |
if (e.op == EXP.error) |
| 2656 |
return true; |
| 2657 |
if (e.op != EXP.variable || e.type.ty == Terror) |
| 2658 |
return false; |
| 2659 |
|
| 2660 |
VarExp ve = cast(VarExp)e; |
| 2661 |
if (!isNeedThisScope(sc, ve.var)) |
| 2662 |
return false; |
| 2663 |
|
| 2664 |
//printf("checkRightThis sc.intypeof = %d, ad = %p, func = %p, fdthis = %p\n", |
| 2665 |
// sc.intypeof, sc.getStructClassScope(), func, fdthis); |
| 2666 |
auto t = ve.var.isThis(); |
| 2667 |
assert(t); |
| 2668 |
error(e.loc, "accessing non-static variable `%s` requires an instance of `%s`", ve.var.toChars(), t.toChars()); |
| 2669 |
return true; |
| 2670 |
} |
| 2671 |
|
| 2672 |
Expression resolveProperties(Scope* sc, Expression e) |
| 2673 |
{ |
| 2674 |
//printf("resolveProperties(%s)\n", e.toChars()); |
| 2675 |
e = resolvePropertiesX(sc, e); |
| 2676 |
if (e.checkRightThis(sc)) |
| 2677 |
return ErrorExp.get(); |
| 2678 |
return e; |
| 2679 |
} |
| 2680 |
|
| 2681 |
/**************************************** |
| 2682 |
* The common type is determined by applying ?: to each pair. |
| 2683 |
* Output: |
| 2684 |
* exps[] properties resolved, implicitly cast to common type, rewritten in place |
| 2685 |
* Returns: |
| 2686 |
* The common type, or `null` if an error has occured |
| 2687 |
*/ |
| 2688 |
private Type arrayExpressionToCommonType(Scope* sc, ref Expressions exps) |
| 2689 |
{ |
| 2690 |
/* Still have a problem with: |
| 2691 |
* ubyte[][] = [ cast(ubyte[])"hello", [1]]; |
| 2692 |
* which works if the array literal is initialized top down with the ubyte[][] |
| 2693 |
* type, but fails with this function doing bottom up typing. |
| 2694 |
*/ |
| 2695 |
|
| 2696 |
//printf("arrayExpressionToCommonType()\n"); |
| 2697 |
scope IntegerExp integerexp = IntegerExp.literal!0; |
| 2698 |
scope CondExp condexp = new CondExp(Loc.initial, integerexp, null, null); |
| 2699 |
|
| 2700 |
Type t0 = null; |
| 2701 |
Expression e0 = null; |
| 2702 |
bool foundType; |
| 2703 |
|
| 2704 |
for (size_t i = 0; i < exps.length; i++) |
| 2705 |
{ |
| 2706 |
Expression e = exps[i]; |
| 2707 |
if (!e) |
| 2708 |
continue; |
| 2709 |
|
| 2710 |
e = resolveProperties(sc, e); |
| 2711 |
if (!e.type) |
| 2712 |
{ |
| 2713 |
error(e.loc, "`%s` has no value", e.toErrMsg()); |
| 2714 |
t0 = Type.terror; |
| 2715 |
continue; |
| 2716 |
} |
| 2717 |
if (e.op == EXP.type) |
| 2718 |
{ |
| 2719 |
foundType = true; // do not break immediately, there might be more errors |
| 2720 |
e.checkValue(); // report an error "type T has no value" |
| 2721 |
t0 = Type.terror; |
| 2722 |
continue; |
| 2723 |
} |
| 2724 |
if (e.type.ty == Tvoid) |
| 2725 |
{ |
| 2726 |
// void expressions do not concur to the determination of the common |
| 2727 |
// type. |
| 2728 |
continue; |
| 2729 |
} |
| 2730 |
if (checkNonAssignmentArrayOp(e)) |
| 2731 |
{ |
| 2732 |
t0 = Type.terror; |
| 2733 |
continue; |
| 2734 |
} |
| 2735 |
|
| 2736 |
e = doCopyOrMove(sc, e, null, false); |
| 2737 |
|
| 2738 |
if (!foundType && t0 && !t0.equals(e.type)) |
| 2739 |
{ |
| 2740 |
/* This applies ?: to merge the types. It's backwards; |
| 2741 |
* ?: should call this function to merge types. |
| 2742 |
*/ |
| 2743 |
condexp.type = null; |
| 2744 |
condexp.e1 = e0; |
| 2745 |
condexp.e2 = e; |
| 2746 |
condexp.loc = e.loc; |
| 2747 |
Expression ex = condexp.expressionSemantic(sc); |
| 2748 |
if (ex.op == EXP.error) |
| 2749 |
e = ex; |
| 2750 |
else |
| 2751 |
{ |
| 2752 |
// Convert to common type |
| 2753 |
exps[i] = condexp.e1.castTo(sc, condexp.type); |
| 2754 |
e = condexp.e2.castTo(sc, condexp.type); |
| 2755 |
} |
| 2756 |
} |
| 2757 |
e0 = e; |
| 2758 |
t0 = e.type; |
| 2759 |
if (e.op != EXP.error) |
| 2760 |
exps[i] = e; |
| 2761 |
} |
| 2762 |
|
| 2763 |
// [] is typed as void[] |
| 2764 |
if (!t0) |
| 2765 |
return Type.tvoid; |
| 2766 |
|
| 2767 |
// It's an error, don't do the cast |
| 2768 |
if (t0.ty == Terror) |
| 2769 |
return null; |
| 2770 |
|
| 2771 |
for (size_t i = 0; i < exps.length; i++) |
| 2772 |
{ |
| 2773 |
Expression e = exps[i]; |
| 2774 |
if (!e) |
| 2775 |
continue; |
| 2776 |
|
| 2777 |
e = e.implicitCastTo(sc, t0); |
| 2778 |
if (e.op == EXP.error) |
| 2779 |
{ |
| 2780 |
/* https://issues.dlang.org/show_bug.cgi?id=13024 |
| 2781 |
* a workaround for the bug in typeMerge - |
| 2782 |
* it should paint e1 and e2 by deduced common type, |
| 2783 |
* but doesn't in this particular case. |
| 2784 |
*/ |
| 2785 |
return null; |
| 2786 |
} |
| 2787 |
exps[i] = e; |
| 2788 |
} |
| 2789 |
return t0; |
| 2790 |
} |
| 2791 |
|
| 2792 |
private Expression opAssignToOp(Loc loc, EXP op, Expression e1, Expression e2) @safe |
| 2793 |
{ |
| 2794 |
Expression e; |
| 2795 |
switch (op) |
| 2796 |
{ |
| 2797 |
case EXP.addAssign: |
| 2798 |
e = new AddExp(loc, e1, e2); |
| 2799 |
break; |
| 2800 |
|
| 2801 |
case EXP.minAssign: |
| 2802 |
e = new MinExp(loc, e1, e2); |
| 2803 |
break; |
| 2804 |
|
| 2805 |
case EXP.mulAssign: |
| 2806 |
e = new MulExp(loc, e1, e2); |
| 2807 |
break; |
| 2808 |
|
| 2809 |
case EXP.divAssign: |
| 2810 |
e = new DivExp(loc, e1, e2); |
| 2811 |
break; |
| 2812 |
|
| 2813 |
case EXP.modAssign: |
| 2814 |
e = new ModExp(loc, e1, e2); |
| 2815 |
break; |
| 2816 |
|
| 2817 |
case EXP.andAssign: |
| 2818 |
e = new AndExp(loc, e1, e2); |
| 2819 |
break; |
| 2820 |
|
| 2821 |
case EXP.orAssign: |
| 2822 |
e = new OrExp(loc, e1, e2); |
| 2823 |
break; |
| 2824 |
|
| 2825 |
case EXP.xorAssign: |
| 2826 |
e = new XorExp(loc, e1, e2); |
| 2827 |
break; |
| 2828 |
|
| 2829 |
case EXP.leftShiftAssign: |
| 2830 |
e = new ShlExp(loc, e1, e2); |
| 2831 |
break; |
| 2832 |
|
| 2833 |
case EXP.rightShiftAssign: |
| 2834 |
e = new ShrExp(loc, e1, e2); |
| 2835 |
break; |
| 2836 |
|
| 2837 |
case EXP.unsignedRightShiftAssign: |
| 2838 |
e = new UshrExp(loc, e1, e2); |
| 2839 |
break; |
| 2840 |
|
| 2841 |
default: |
| 2842 |
assert(0); |
| 2843 |
} |
| 2844 |
return e; |
| 2845 |
} |
| 2846 |
|
| 2847 |
/********************* |
| 2848 |
* Rewrite: |
| 2849 |
* array.length op= e2 |
| 2850 |
*/ |
| 2851 |
private Expression rewriteOpAssign(BinExp exp) |
| 2852 |
{ |
| 2853 |
ArrayLengthExp ale = exp.e1.isArrayLengthExp(); |
| 2854 |
if (ale.e1.isVarExp()) |
| 2855 |
{ |
| 2856 |
// array.length = array.length op e2 |
| 2857 |
Expression e = opAssignToOp(exp.loc, exp.op, ale, exp.e2); |
| 2858 |
e = new AssignExp(exp.loc, ale.syntaxCopy(), e); |
| 2859 |
return e; |
| 2860 |
} |
| 2861 |
else |
| 2862 |
{ |
| 2863 |
// (ref tmp = array;), tmp.length = tmp.length op e2 |
| 2864 |
auto tmp = copyToTemp(STC.ref_, "__arraylength", ale.e1); |
| 2865 |
Expression e1 = new ArrayLengthExp(ale.loc, new VarExp(ale.loc, tmp)); |
| 2866 |
Expression elvalue = e1.syntaxCopy(); |
| 2867 |
Expression e = opAssignToOp(exp.loc, exp.op, e1, exp.e2); |
| 2868 |
e = new AssignExp(exp.loc, elvalue, e); |
| 2869 |
e = new CommaExp(exp.loc, new DeclarationExp(ale.loc, tmp), e); |
| 2870 |
return e; |
| 2871 |
} |
| 2872 |
} |
| 2873 |
|
| 2874 |
/**************************************** |
| 2875 |
* Preprocess arguments to function. |
| 2876 |
* |
| 2877 |
* Tuples in argumentList get expanded, properties resolved, rewritten in place |
| 2878 |
* |
| 2879 |
* Params: |
| 2880 |
* sc = scope |
| 2881 |
* argumentList = arguments to function |
| 2882 |
* eSink = if not null, used to report errors. Some callers are not |
| 2883 |
* checking actual function params, so they'll do their own error reporting |
| 2884 |
* Returns: |
| 2885 |
* `true` when a semantic error occurred |
| 2886 |
*/ |
| 2887 |
private bool preFunctionParameters(Scope* sc, ArgumentList argumentList, ErrorSink eSink) |
| 2888 |
{ |
| 2889 |
Expressions* exps = argumentList.arguments; |
| 2890 |
if (!exps) |
| 2891 |
return false; |
| 2892 |
|
| 2893 |
expandTuples(exps, argumentList.names); |
| 2894 |
|
| 2895 |
bool err = false; |
| 2896 |
for (size_t i = 0; i < exps.length; i++) |
| 2897 |
{ |
| 2898 |
Expression arg = (*exps)[i]; |
| 2899 |
arg = resolveProperties(sc, arg); |
| 2900 |
arg = arg.arrayFuncConv(sc); |
| 2901 |
if (arg.op == EXP.type) |
| 2902 |
{ |
| 2903 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 2904 |
arg = resolveAliasThis(sc, arg); |
| 2905 |
|
| 2906 |
if (arg.op == EXP.type) |
| 2907 |
{ |
| 2908 |
if (eSink) |
| 2909 |
{ |
| 2910 |
eSink.error(arg.loc, "cannot pass type `%s` as a function argument", arg.toErrMsg()); |
| 2911 |
arg = ErrorExp.get(); |
| 2912 |
} |
| 2913 |
err = true; |
| 2914 |
} |
| 2915 |
} |
| 2916 |
else if (arg.type.toBasetype().ty == Tfunction) |
| 2917 |
{ |
| 2918 |
if (eSink) |
| 2919 |
{ |
| 2920 |
eSink.error(arg.loc, "cannot pass function `%s` as a function argument", arg.toErrMsg()); |
| 2921 |
arg = ErrorExp.get(); |
| 2922 |
} |
| 2923 |
err = true; |
| 2924 |
} |
| 2925 |
else if (checkNonAssignmentArrayOp(arg)) |
| 2926 |
{ |
| 2927 |
arg = ErrorExp.get(); |
| 2928 |
err = true; |
| 2929 |
} |
| 2930 |
(*exps)[i] = arg; |
| 2931 |
} |
| 2932 |
|
| 2933 |
return err; |
| 2934 |
} |
| 2935 |
|
| 2936 |
/******************************************** |
| 2937 |
* Issue an error if default construction is disabled for type t. |
| 2938 |
* Default construction is required for arrays and 'out' parameters. |
| 2939 |
* Returns: |
| 2940 |
* true an error was issued |
| 2941 |
*/ |
| 2942 |
private bool checkDefCtor(Loc loc, Type t) |
| 2943 |
{ |
| 2944 |
if (auto ts = t.baseElemOf().isTypeStruct()) |
| 2945 |
{ |
| 2946 |
StructDeclaration sd = ts.sym; |
| 2947 |
if (sd.noDefaultCtor) |
| 2948 |
{ |
| 2949 |
.error(loc, "%s `%s` default construction is disabled", sd.kind, sd.toPrettyChars); |
| 2950 |
return true; |
| 2951 |
} |
| 2952 |
} |
| 2953 |
return false; |
| 2954 |
} |
| 2955 |
|
| 2956 |
/**************************************** |
| 2957 |
* Now that we know the exact type of the function we're calling, |
| 2958 |
* the arguments[] need to be adjusted: |
| 2959 |
* 1. implicitly convert argument to the corresponding parameter type |
| 2960 |
* 2. add default arguments for any missing arguments |
| 2961 |
* 3. do default promotions on arguments corresponding to ... |
| 2962 |
* 4. add hidden _arguments[] argument |
| 2963 |
* 5. call copy constructor for struct value arguments |
| 2964 |
* Params: |
| 2965 |
* loc = location of function call |
| 2966 |
* sc = context |
| 2967 |
* tf = type of the function |
| 2968 |
* ethis = `this` argument, `null` if none or not known |
| 2969 |
* tthis = type of `this` argument, `null` if no `this` argument |
| 2970 |
* argumentsList = array of actual arguments to function call |
| 2971 |
* fd = the function being called, `null` if called indirectly |
| 2972 |
* prettype = set to return type of function |
| 2973 |
* peprefix = set to expression to execute before `arguments[]` are evaluated, `null` if none |
| 2974 |
* Returns: |
| 2975 |
* true errors happened |
| 2976 |
*/ |
| 2977 |
private bool functionParameters(Loc loc, Scope* sc, |
| 2978 |
TypeFunction tf, Expression ethis, Type tthis, ArgumentList argumentList, FuncDeclaration fd, |
| 2979 |
Type* prettype, Expression* peprefix) |
| 2980 |
{ |
| 2981 |
Expressions* arguments = argumentList.arguments; |
| 2982 |
//printf("functionParameters() fd: %s tf: %s\n", fd ? fd.ident.toChars() : "", toChars(tf)); |
| 2983 |
assert(arguments); |
| 2984 |
assert(fd || tf.next); |
| 2985 |
const size_t nparams = tf.parameterList.length; |
| 2986 |
const olderrors = global.errors; |
| 2987 |
bool err = false; |
| 2988 |
Expression eprefix = null; |
| 2989 |
*peprefix = null; |
| 2990 |
|
| 2991 |
if (argumentList.names) |
| 2992 |
{ |
| 2993 |
OutBuffer buf; |
| 2994 |
auto resolvedArgs = tf.resolveNamedArgs(argumentList, &buf); |
| 2995 |
if (!resolvedArgs) |
| 2996 |
{ |
| 2997 |
// while errors are usually already caught by `tf.callMatch`, |
| 2998 |
// this can happen when calling `typeof(freefunc)` |
| 2999 |
if (buf.length) |
| 3000 |
error(loc, "%s", buf.peekChars()); |
| 3001 |
return true; |
| 3002 |
} |
| 3003 |
// note: the argument list should be mutated with named arguments / default arguments, |
| 3004 |
// so we can't simply change the pointer like `arguments = resolvedArgs;` |
| 3005 |
arguments.setDim(0); |
| 3006 |
arguments.pushSlice((*resolvedArgs)[]); |
| 3007 |
} |
| 3008 |
size_t nargs = arguments ? arguments.length : 0; |
| 3009 |
|
| 3010 |
if (nargs > nparams && tf.parameterList.varargs == VarArg.none) |
| 3011 |
{ |
| 3012 |
error(loc, "expected %llu arguments, not %llu for non-variadic function type `%s`", cast(ulong)nparams, cast(ulong)nargs, tf.toChars()); |
| 3013 |
return true; |
| 3014 |
} |
| 3015 |
|
| 3016 |
// If inferring return type, and semantic3() needs to be run if not already run |
| 3017 |
if (!tf.next && fd.inferRetType) |
| 3018 |
{ |
| 3019 |
functionSemantic(fd); |
| 3020 |
} |
| 3021 |
else if (fd && fd.parent) |
| 3022 |
{ |
| 3023 |
TemplateInstance ti = fd.parent.isTemplateInstance(); |
| 3024 |
if (ti && ti.tempdecl) |
| 3025 |
{ |
| 3026 |
fd.functionSemantic3(); |
| 3027 |
} |
| 3028 |
} |
| 3029 |
|
| 3030 |
/* If calling a pragma(inline, true) function, |
| 3031 |
* set flag to later scan for inlines. |
| 3032 |
*/ |
| 3033 |
if (fd && fd.inlining == PINLINE.always) |
| 3034 |
{ |
| 3035 |
if (sc._module) |
| 3036 |
sc._module.hasAlwaysInlines = true; |
| 3037 |
if (sc.func) |
| 3038 |
sc.func.hasAlwaysInlines = true; |
| 3039 |
} |
| 3040 |
|
| 3041 |
const isCtorCall = fd && fd.needThis() && fd.isCtorDeclaration(); |
| 3042 |
|
| 3043 |
const size_t n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) |
| 3044 |
|
| 3045 |
/* If the function return type has wildcards in it, we'll need to figure out the actual type |
| 3046 |
* based on the actual argument types. |
| 3047 |
* Start with the `this` argument, later on merge into wildmatch the mod bits of the rest |
| 3048 |
* of the arguments. |
| 3049 |
*/ |
| 3050 |
MOD wildmatch = (tthis && !isCtorCall) ? tthis.Type.deduceWild(tf, false) : 0; |
| 3051 |
|
| 3052 |
bool done = false; |
| 3053 |
foreach (const i; 0 .. n) |
| 3054 |
{ |
| 3055 |
Expression arg = (i < nargs) ? (*arguments)[i] : null; |
| 3056 |
|
| 3057 |
if (i >= nparams) |
| 3058 |
break; |
| 3059 |
|
| 3060 |
bool errorArgs() |
| 3061 |
{ |
| 3062 |
error(loc, "expected %llu function arguments, not %llu", cast(ulong)nparams, cast(ulong)nargs); |
| 3063 |
return true; |
| 3064 |
} |
| 3065 |
|
| 3066 |
Parameter p = tf.parameterList[i]; |
| 3067 |
|
| 3068 |
if (!arg) |
| 3069 |
{ |
| 3070 |
if (!p.defaultArg) |
| 3071 |
{ |
| 3072 |
if (tf.parameterList.varargs == VarArg.typesafe && i + 1 == nparams) |
| 3073 |
goto L2; |
| 3074 |
return errorArgs(); |
| 3075 |
} |
| 3076 |
arg = p.defaultArg; |
| 3077 |
if (!arg.type) |
| 3078 |
arg = arg.expressionSemantic(sc); |
| 3079 |
arg = inlineCopy(arg, sc); |
| 3080 |
// __FILE__, __LINE__, __MODULE__, __FUNCTION__, and __PRETTY_FUNCTION__ |
| 3081 |
arg = arg.resolveLoc(loc, sc); |
| 3082 |
if (i >= nargs) |
| 3083 |
{ |
| 3084 |
arguments.push(arg); |
| 3085 |
nargs++; |
| 3086 |
} |
| 3087 |
else |
| 3088 |
(*arguments)[i] = arg; |
| 3089 |
} |
| 3090 |
else if (arg.isDefaultInitExp()) |
| 3091 |
{ |
| 3092 |
arg = arg.resolveLoc(loc, sc); |
| 3093 |
(*arguments)[i] = arg; |
| 3094 |
} |
| 3095 |
|
| 3096 |
if (tf.parameterList.varargs == VarArg.typesafe && i + 1 == nparams) // https://dlang.org/spec/function.html#variadic |
| 3097 |
{ |
| 3098 |
//printf("\t\tvarargs == 2, p.type = '%s'\n", p.type.toChars()); |
| 3099 |
if (MATCH m = arg.implicitConvTo(p.type)) |
| 3100 |
{ |
| 3101 |
if (p.type.nextOf() && arg.implicitConvTo(p.type.nextOf()) >= m) |
| 3102 |
goto L2; |
| 3103 |
else if (nargs != nparams) |
| 3104 |
return errorArgs(); |
| 3105 |
goto L1; |
| 3106 |
} |
| 3107 |
L2: |
| 3108 |
Type tb = p.type.toBasetype(); |
| 3109 |
switch (tb.ty) |
| 3110 |
{ |
| 3111 |
case Tsarray: |
| 3112 |
case Tarray: |
| 3113 |
{ |
| 3114 |
/* Create a static array variable v of type arg.type: |
| 3115 |
* T[dim] __arrayArg = [ arguments[i], ..., arguments[nargs-1] ]; |
| 3116 |
* |
| 3117 |
* The array literal in the initializer of the hidden variable |
| 3118 |
* is now optimized. |
| 3119 |
* https://issues.dlang.org/show_bug.cgi?id=2356 |
| 3120 |
*/ |
| 3121 |
Type tbn = (cast(TypeArray)tb).next; // array element type |
| 3122 |
Type tret = p.isLazyArray(); |
| 3123 |
|
| 3124 |
auto elements = new Expressions(nargs - i); |
| 3125 |
foreach (u; 0 .. elements.length) |
| 3126 |
{ |
| 3127 |
Expression a = (*arguments)[i + u]; |
| 3128 |
assert(a); |
| 3129 |
if (tret && a.implicitConvTo(tret)) |
| 3130 |
{ |
| 3131 |
// p is a lazy array of delegates, tret is return type of the delegates |
| 3132 |
a = a.implicitCastTo(sc, tret) |
| 3133 |
.optimize(WANTvalue) |
| 3134 |
.toDelegate(tret, sc); |
| 3135 |
} |
| 3136 |
else |
| 3137 |
a = a.implicitCastTo(sc, tbn); |
| 3138 |
a = a.addDtorHook(sc); |
| 3139 |
(*elements)[u] = a; |
| 3140 |
} |
| 3141 |
// https://issues.dlang.org/show_bug.cgi?id=14395 |
| 3142 |
// Convert to a static array literal, or its slice. |
| 3143 |
arg = new ArrayLiteralExp(loc, tbn.sarrayOf(nargs - i), elements); |
| 3144 |
if (tb.ty == Tarray) |
| 3145 |
{ |
| 3146 |
arg = new SliceExp(loc, arg, null, null); |
| 3147 |
arg.type = p.type; |
| 3148 |
} |
| 3149 |
break; |
| 3150 |
} |
| 3151 |
case Tclass: |
| 3152 |
{ |
| 3153 |
/* Set arg to be: |
| 3154 |
* new Tclass(arg0, arg1, ..., argn) |
| 3155 |
*/ |
| 3156 |
auto args = new Expressions(nargs - i); |
| 3157 |
foreach (u; i .. nargs) |
| 3158 |
(*args)[u - i] = (*arguments)[u]; |
| 3159 |
arg = new NewExp(loc, null, null, p.type, args); |
| 3160 |
break; |
| 3161 |
} |
| 3162 |
default: |
| 3163 |
if (!arg) |
| 3164 |
{ |
| 3165 |
error(loc, "not enough arguments"); |
| 3166 |
return true; |
| 3167 |
} |
| 3168 |
break; |
| 3169 |
} |
| 3170 |
arg = arg.expressionSemantic(sc); |
| 3171 |
//printf("\targ = '%s'\n", arg.toChars()); |
| 3172 |
arguments.setDim(i + 1); |
| 3173 |
(*arguments)[i] = arg; |
| 3174 |
nargs = i + 1; |
| 3175 |
done = true; |
| 3176 |
} |
| 3177 |
|
| 3178 |
L1: |
| 3179 |
if (!(p.isLazy() && p.type.ty == Tvoid)) |
| 3180 |
{ |
| 3181 |
if (ubyte wm = arg.type.deduceWild(p.type, p.isReference())) |
| 3182 |
{ |
| 3183 |
wildmatch = wildmatch ? MODmerge(wildmatch, wm) : wm; |
| 3184 |
//printf("[%d] p = %s, a = %s, wm = %d, wildmatch = %d\n", i, p.type.toChars(), arg.type.toChars(), wm, wildmatch); |
| 3185 |
} |
| 3186 |
} |
| 3187 |
|
| 3188 |
if (done) |
| 3189 |
break; |
| 3190 |
} |
| 3191 |
if ((wildmatch == MODFlags.mutable || wildmatch == MODFlags.immutable_) && |
| 3192 |
tf.next && tf.next.hasWild() && |
| 3193 |
(tf.isRef || !tf.next.implicitConvTo(tf.next.immutableOf()))) |
| 3194 |
{ |
| 3195 |
bool errorInout(MOD wildmatch) |
| 3196 |
{ |
| 3197 |
const(char)* s = wildmatch == MODFlags.mutable ? "mutable" : MODtoChars(wildmatch); |
| 3198 |
error(loc, "modify `inout` to `%s` is not allowed inside `inout` function", s); |
| 3199 |
return true; |
| 3200 |
} |
| 3201 |
|
| 3202 |
if (fd) |
| 3203 |
{ |
| 3204 |
/* If the called function may return the reference to |
| 3205 |
* outer inout data, it should be rejected. |
| 3206 |
* |
| 3207 |
* void foo(ref inout(int) x) { |
| 3208 |
* ref inout(int) bar(inout(int)) { return x; } |
| 3209 |
* struct S { |
| 3210 |
* ref inout(int) bar() inout { return x; } |
| 3211 |
* ref inout(int) baz(alias a)() inout { return x; } |
| 3212 |
* } |
| 3213 |
* bar(int.init) = 1; // bad! |
| 3214 |
* S().bar() = 1; // bad! |
| 3215 |
* } |
| 3216 |
* void test() { |
| 3217 |
* int a; |
| 3218 |
* auto s = foo(a); |
| 3219 |
* s.baz!a() = 1; // bad! |
| 3220 |
* } |
| 3221 |
* |
| 3222 |
*/ |
| 3223 |
bool checkEnclosingWild(Dsymbol s) |
| 3224 |
{ |
| 3225 |
bool checkWild(Dsymbol s) |
| 3226 |
{ |
| 3227 |
if (!s) |
| 3228 |
return false; |
| 3229 |
if (auto ad = s.isAggregateDeclaration()) |
| 3230 |
{ |
| 3231 |
if (ad.isNested()) |
| 3232 |
return checkEnclosingWild(s); |
| 3233 |
} |
| 3234 |
else if (auto ff = s.isFuncDeclaration()) |
| 3235 |
{ |
| 3236 |
if (ff.type.isTypeFunction().iswild) |
| 3237 |
return errorInout(wildmatch); |
| 3238 |
|
| 3239 |
if (ff.isNested() || ff.isThis()) |
| 3240 |
return checkEnclosingWild(s); |
| 3241 |
} |
| 3242 |
return false; |
| 3243 |
} |
| 3244 |
|
| 3245 |
Dsymbol ctx0 = s.toParent2(); |
| 3246 |
Dsymbol ctx1 = s.toParentLocal(); |
| 3247 |
if (checkWild(ctx0)) |
| 3248 |
return true; |
| 3249 |
if (ctx0 != ctx1) |
| 3250 |
return checkWild(ctx1); |
| 3251 |
return false; |
| 3252 |
} |
| 3253 |
if ((fd.isThis() || fd.isNested()) && checkEnclosingWild(fd)) |
| 3254 |
return true; |
| 3255 |
} |
| 3256 |
else if (tf.isWild()) |
| 3257 |
return errorInout(wildmatch); |
| 3258 |
} |
| 3259 |
|
| 3260 |
Expression firstArg = null; |
| 3261 |
final switch (returnParamDest(tf, tthis)) |
| 3262 |
{ |
| 3263 |
case ReturnParamDest.returnVal: |
| 3264 |
break; |
| 3265 |
case ReturnParamDest.firstArg: |
| 3266 |
firstArg = nargs > 0 ? (*arguments)[0] : null; |
| 3267 |
break; |
| 3268 |
case ReturnParamDest.this_: |
| 3269 |
firstArg = ethis; |
| 3270 |
break; |
| 3271 |
} |
| 3272 |
|
| 3273 |
assert(nargs >= nparams); |
| 3274 |
foreach (const i, arg; (*arguments)[0 .. nargs]) |
| 3275 |
{ |
| 3276 |
assert(arg); |
| 3277 |
if (i < nparams) |
| 3278 |
{ |
| 3279 |
Parameter p = tf.parameterList[i]; |
| 3280 |
Type targ = arg.type; // keep original type for isCopyable() because alias this |
| 3281 |
// resolution may hide an uncopyable type |
| 3282 |
|
| 3283 |
if (!(p.isLazy() && p.type.ty == Tvoid)) |
| 3284 |
{ |
| 3285 |
Type tprm = p.type.hasWild() |
| 3286 |
? p.type.substWildTo(wildmatch) |
| 3287 |
: p.type; |
| 3288 |
|
| 3289 |
const hasCopyCtor = arg.type.isTypeStruct() && arg.type.isTypeStruct().sym.hasCopyCtor; |
| 3290 |
const typesMatch = arg.type.mutableOf().unSharedOf().equals(tprm.mutableOf().unSharedOf()); |
| 3291 |
if (!((hasCopyCtor && typesMatch) || tprm.equals(arg.type))) |
| 3292 |
{ |
| 3293 |
//printf("arg.type = %s, p.type = %s\n", arg.type.toChars(), p.type.toChars()); |
| 3294 |
arg = arg.implicitCastTo(sc, tprm); |
| 3295 |
arg = arg.optimize(WANTvalue, p.isReference()); |
| 3296 |
} |
| 3297 |
} |
| 3298 |
|
| 3299 |
// Support passing rvalue to `in` parameters |
| 3300 |
if ((p.storageClass & (STC.in_ | STC.ref_)) == (STC.in_ | STC.ref_)) |
| 3301 |
{ |
| 3302 |
if (!arg.isLvalue()) |
| 3303 |
{ |
| 3304 |
auto v = copyToTemp(STC.exptemp, "__rvalue", arg); |
| 3305 |
Expression ev = new DeclarationExp(arg.loc, v); |
| 3306 |
ev = new CommaExp(arg.loc, ev, new VarExp(arg.loc, v)); |
| 3307 |
arg = ev.expressionSemantic(sc); |
| 3308 |
} |
| 3309 |
arg = arg.toLvalue(sc, "create `in` parameter from"); |
| 3310 |
|
| 3311 |
// Look for mutable misaligned pointer, etc., in @safe mode |
| 3312 |
err |= checkUnsafeAccess(sc, arg, false, true); |
| 3313 |
} |
| 3314 |
else if (p.storageClass & STC.ref_) |
| 3315 |
{ |
| 3316 |
if (sc.previews.rvalueRefParam && |
| 3317 |
!arg.isLvalue() && |
| 3318 |
targ.isCopyable()) |
| 3319 |
{ /* allow rvalues to be passed to ref parameters by copying |
| 3320 |
* them to a temp, then pass the temp as the argument |
| 3321 |
*/ |
| 3322 |
auto v = copyToTemp(STC.none, "__rvalue", arg); |
| 3323 |
Expression ev = new DeclarationExp(arg.loc, v); |
| 3324 |
ev = new CommaExp(arg.loc, ev, new VarExp(arg.loc, v)); |
| 3325 |
arg = ev.expressionSemantic(sc); |
| 3326 |
} |
| 3327 |
arg = arg.toLvalue(sc, "create `ref` parameter from"); |
| 3328 |
|
| 3329 |
// Look for mutable misaligned pointer, etc., in @safe mode |
| 3330 |
err |= checkUnsafeAccess(sc, arg, false, true); |
| 3331 |
} |
| 3332 |
else if (p.storageClass & STC.out_) |
| 3333 |
{ |
| 3334 |
Type t = arg.type; |
| 3335 |
if (!t.isMutable() || !t.isAssignable()) // check blit assignable |
| 3336 |
{ |
| 3337 |
error(arg.loc, "cannot modify struct `%s` with immutable members", arg.toChars()); |
| 3338 |
err = true; |
| 3339 |
} |
| 3340 |
else |
| 3341 |
{ |
| 3342 |
// Look for misaligned pointer, etc., in @safe mode |
| 3343 |
err |= checkUnsafeAccess(sc, arg, false, true); |
| 3344 |
err |= checkDefCtor(arg.loc, t); // t must be default constructible |
| 3345 |
} |
| 3346 |
arg = arg.toLvalue(sc, "create `out` parameter from"); |
| 3347 |
} |
| 3348 |
else if (p.isLazy()) |
| 3349 |
{ |
| 3350 |
// Convert lazy argument to a delegate |
| 3351 |
auto t = (p.type.ty == Tvoid) ? p.type : arg.type; |
| 3352 |
arg = toDelegate(arg, t, sc); |
| 3353 |
} |
| 3354 |
//printf("arg: %s\n", arg.toChars()); |
| 3355 |
//printf("type: %s\n", arg.type.toChars()); |
| 3356 |
//printf("param: %s\n", p.toChars()); |
| 3357 |
|
| 3358 |
const indirect = (fd is null) || (fd.isVirtual() && !fd.isFinal()); |
| 3359 |
const pStc = tf.parameterStorageClass(tthis, p, fd ? &fd.outerVars : null, indirect); |
| 3360 |
|
| 3361 |
if (firstArg && (pStc & STC.return_)) |
| 3362 |
{ |
| 3363 |
/* Argument value can be assigned to firstArg. |
| 3364 |
* Check arg to see if it matters. |
| 3365 |
*/ |
| 3366 |
err |= checkParamArgumentReturn(*sc, firstArg, arg, p, false); |
| 3367 |
} |
| 3368 |
// Allow 'lazy' to imply 'scope' - lazy parameters can be passed along |
| 3369 |
// as lazy parameters to the next function, but that isn't escaping. |
| 3370 |
// The arguments of `_d_arraycatnTX` are already handled in |
| 3371 |
// expressionsem.d, via `checkNewEscape`. Without `-dip1000`, the |
| 3372 |
// check does not return an error, so the lowering of `a ~ b` to |
| 3373 |
// `_d_arraycatnTX(a, b)` still occurs. |
| 3374 |
else if (!(pStc & STC.lazy_) && (!fd || fd.ident != Id._d_arraycatnTX)) |
| 3375 |
{ |
| 3376 |
/* Argument value can escape from the called function. |
| 3377 |
* Check arg to see if it matters. |
| 3378 |
*/ |
| 3379 |
VarDeclaration vPar = fd ? (fd.parameters ? (*fd.parameters)[i] : null) : null; |
| 3380 |
err |= checkParamArgumentEscape(*sc, fd, p.ident, vPar, cast(STC) pStc, arg, false, false); |
| 3381 |
} |
| 3382 |
|
| 3383 |
// Turning heap allocations into stack allocations is dangerous without dip1000, since `scope` inference |
| 3384 |
// may be unreliable when scope violations only manifest as deprecation warnings. |
| 3385 |
// However, existing `@nogc` code may rely on it, so still do it when the parameter is explicitly marked `scope` |
| 3386 |
const explicitScope = p.isLazy() || |
| 3387 |
((p.storageClass & STC.scope_) && !(p.storageClass & STC.scopeinferred)); |
| 3388 |
if ((pStc & (STC.scope_ | STC.lazy_)) && |
| 3389 |
((sc.useDIP1000 == FeatureState.enabled) || explicitScope) && |
| 3390 |
!(pStc & STC.return_)) |
| 3391 |
{ |
| 3392 |
/* Argument value cannot escape from the called function. |
| 3393 |
*/ |
| 3394 |
Expression a = arg; |
| 3395 |
if (auto ce = a.isCastExp()) |
| 3396 |
a = ce.e1; |
| 3397 |
|
| 3398 |
ArrayLiteralExp ale; |
| 3399 |
if (p.type.toBasetype().ty == Tarray && |
| 3400 |
(ale = a.isArrayLiteralExp()) !is null && ale.elements && ale.elements.length > 0) |
| 3401 |
{ |
| 3402 |
// allocate the array literal as temporary static array on the stack |
| 3403 |
ale.type = ale.type.nextOf().sarrayOf(ale.elements.length); |
| 3404 |
auto tmp = copyToTemp(STC.none, "__arrayliteral_on_stack", ale); |
| 3405 |
tmp.storage_class |= STC.exptemp; |
| 3406 |
auto declareTmp = new DeclarationExp(ale.loc, tmp); |
| 3407 |
auto castToSlice = new CastExp(ale.loc, new VarExp(ale.loc, tmp), |
| 3408 |
p.type.substWildTo(MODFlags.mutable)); |
| 3409 |
arg = CommaExp.combine(declareTmp, castToSlice); |
| 3410 |
arg = arg.expressionSemantic(sc); |
| 3411 |
} |
| 3412 |
else if (auto fe = a.isFuncExp()) |
| 3413 |
{ |
| 3414 |
/* Function literals can only appear once, so if this |
| 3415 |
* appearance was scoped, there cannot be any others. |
| 3416 |
*/ |
| 3417 |
fe.fd.tookAddressOf = 0; |
| 3418 |
} |
| 3419 |
else if (auto de = a.isDelegateExp()) |
| 3420 |
{ |
| 3421 |
/* For passing a delegate to a scoped parameter, |
| 3422 |
* this doesn't count as taking the address of it. |
| 3423 |
* We only worry about 'escaping' references to the function. |
| 3424 |
*/ |
| 3425 |
if (auto ve = de.e1.isVarExp()) |
| 3426 |
{ |
| 3427 |
if (auto f = ve.var.isFuncDeclaration()) |
| 3428 |
{ |
| 3429 |
if (f.tookAddressOf) |
| 3430 |
--f.tookAddressOf; |
| 3431 |
//printf("--tookAddressOf = %d\n", f.tookAddressOf); |
| 3432 |
} |
| 3433 |
} |
| 3434 |
} |
| 3435 |
} |
| 3436 |
if (!p.isReference()) |
| 3437 |
err |= arg.checkSharedAccess(sc); |
| 3438 |
|
| 3439 |
arg = arg.optimize(WANTvalue, p.isReference()); |
| 3440 |
} |
| 3441 |
else |
| 3442 |
{ |
| 3443 |
// These will be the trailing ... arguments |
| 3444 |
// If not D linkage, do promotions |
| 3445 |
if (tf.linkage != LINK.d) |
| 3446 |
{ |
| 3447 |
// Promote bytes, words, etc., to ints |
| 3448 |
arg = integralPromotions(arg, sc); |
| 3449 |
|
| 3450 |
// Promote floats to doubles |
| 3451 |
switch (arg.type.ty) |
| 3452 |
{ |
| 3453 |
case Tfloat32: |
| 3454 |
arg = arg.castTo(sc, Type.tfloat64); |
| 3455 |
break; |
| 3456 |
|
| 3457 |
case Timaginary32: |
| 3458 |
arg = arg.castTo(sc, Type.timaginary64); |
| 3459 |
break; |
| 3460 |
|
| 3461 |
default: |
| 3462 |
break; |
| 3463 |
} |
| 3464 |
if (tf.parameterList.varargs == VarArg.variadic || |
| 3465 |
tf.parameterList.varargs == VarArg.KRvariadic) |
| 3466 |
{ |
| 3467 |
const(char)* p = tf.linkage == LINK.c ? "extern(C)" : "extern(C++)"; |
| 3468 |
if (arg.type.ty == Tarray) |
| 3469 |
{ |
| 3470 |
error(arg.loc, "cannot pass dynamic arrays to `%s` vararg functions", p); |
| 3471 |
err = true; |
| 3472 |
} |
| 3473 |
if (arg.type.ty == Tsarray) |
| 3474 |
{ |
| 3475 |
error(arg.loc, "cannot pass static arrays to `%s` vararg functions", p); |
| 3476 |
err = true; |
| 3477 |
} |
| 3478 |
} |
| 3479 |
} |
| 3480 |
|
| 3481 |
// Do not allow types that need destructors or copy constructors. |
| 3482 |
if (arg.type.needsDestruction()) |
| 3483 |
{ |
| 3484 |
error(arg.loc, "cannot pass types that need destruction as variadic arguments"); |
| 3485 |
err = true; |
| 3486 |
} |
| 3487 |
if (arg.type.needsCopyOrPostblit()) |
| 3488 |
{ |
| 3489 |
error(arg.loc, "cannot pass types with postblits or copy constructors as variadic arguments"); |
| 3490 |
err = true; |
| 3491 |
} |
| 3492 |
|
| 3493 |
// Convert static arrays to dynamic arrays |
| 3494 |
// BUG: I don't think this is right for D2 |
| 3495 |
Type tb = arg.type.toBasetype(); |
| 3496 |
if (auto ts = tb.isTypeSArray()) |
| 3497 |
{ |
| 3498 |
Type ta = ts.next.arrayOf(); |
| 3499 |
if (ts.size(arg.loc) == 0) |
| 3500 |
arg = new NullExp(arg.loc, ta); |
| 3501 |
else |
| 3502 |
arg = arg.castTo(sc, ta); |
| 3503 |
} |
| 3504 |
if (tb.ty == Tstruct) |
| 3505 |
{ |
| 3506 |
//arg = callCpCtor(sc, arg); |
| 3507 |
} |
| 3508 |
// Give error for overloaded function addresses |
| 3509 |
if (auto se = arg.isSymOffExp()) |
| 3510 |
{ |
| 3511 |
if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique()) |
| 3512 |
{ |
| 3513 |
error(arg.loc, "function `%s` is overloaded", arg.toErrMsg()); |
| 3514 |
err = true; |
| 3515 |
} |
| 3516 |
} |
| 3517 |
err |= arg.checkValue(); |
| 3518 |
err |= arg.checkSharedAccess(sc); |
| 3519 |
err |= checkParamArgumentEscape(*sc, fd, Id.dotdotdot, null, cast(STC) tf.parameterList.stc, arg, false, false); |
| 3520 |
arg = arg.optimize(WANTvalue); |
| 3521 |
} |
| 3522 |
(*arguments)[i] = arg; |
| 3523 |
} |
| 3524 |
|
| 3525 |
/* If calling C scanf(), printf(), or any variants, check the format string against the arguments |
| 3526 |
*/ |
| 3527 |
const isVa_list = tf.parameterList.varargs == VarArg.none; |
| 3528 |
if (fd && fd.printf) |
| 3529 |
{ |
| 3530 |
if (auto se = (*arguments)[nparams - 1 - isVa_list].isStringExp()) |
| 3531 |
{ |
| 3532 |
checkPrintfFormat(se.loc, se.peekString(), (*arguments)[nparams .. nargs], isVa_list, sc.eSink); |
| 3533 |
} |
| 3534 |
} |
| 3535 |
else if (fd && fd.scanf) |
| 3536 |
{ |
| 3537 |
if (auto se = (*arguments)[nparams - 1 - isVa_list].isStringExp()) |
| 3538 |
{ |
| 3539 |
checkScanfFormat(se.loc, se.peekString(), (*arguments)[nparams .. nargs], isVa_list, sc.eSink); |
| 3540 |
} |
| 3541 |
} |
| 3542 |
else |
| 3543 |
{ |
| 3544 |
// TODO: not checking the "v" functions yet (for those, check format string only, not args) |
| 3545 |
} |
| 3546 |
|
| 3547 |
/* Remaining problems: |
| 3548 |
* 1. value structs (or static arrays of them) that need to be copy constructed |
| 3549 |
* 2. value structs (or static arrays of them) that have destructors, and subsequent arguments that may throw before the |
| 3550 |
* function gets called. |
| 3551 |
* 3. value structs need to be destructed after the function call for platforms where the caller destroys the arguments. |
| 3552 |
* Those are handled by doing the argument construction in 'eprefix' so that if a later argument throws, they are cleaned |
| 3553 |
* up properly. Pushing arguments on the stack then cannot fail. |
| 3554 |
*/ |
| 3555 |
{ |
| 3556 |
/* Does Problem (3) apply? |
| 3557 |
*/ |
| 3558 |
const bool callerDestroysArgs = !target.isCalleeDestroyingArgs(tf); |
| 3559 |
|
| 3560 |
/* Compute indices of last throwing argument and first arg needing destruction. |
| 3561 |
* Used to not set up destructors unless an arg needs destruction on a throw |
| 3562 |
* in a later argument. |
| 3563 |
*/ |
| 3564 |
ptrdiff_t lastthrow = -1; // last argument that may throw |
| 3565 |
ptrdiff_t firstdtor = -1; // first argument that needs destruction |
| 3566 |
ptrdiff_t lastdtor = -1; // last argument that needs destruction |
| 3567 |
for (ptrdiff_t i = 0; i != nargs; i++) |
| 3568 |
{ |
| 3569 |
Expression arg = (*arguments)[i]; |
| 3570 |
if (canThrow(arg, sc.func, null)) |
| 3571 |
lastthrow = i; |
| 3572 |
if (arg.type.needsDestruction()) |
| 3573 |
{ |
| 3574 |
Parameter p = (i >= nparams ? null : tf.parameterList[i]); |
| 3575 |
if (!(p && (p.isLazy() || p.isReference()))) |
| 3576 |
{ |
| 3577 |
if (firstdtor == -1) |
| 3578 |
firstdtor = i; |
| 3579 |
lastdtor = i; |
| 3580 |
} |
| 3581 |
} |
| 3582 |
} |
| 3583 |
|
| 3584 |
/* Do we need 'eprefix' for problems 2 or 3? |
| 3585 |
*/ |
| 3586 |
const bool needsPrefix = callerDestroysArgs |
| 3587 |
? firstdtor >= 0 // true if any argument needs destruction |
| 3588 |
: firstdtor >= 0 && lastthrow >= 0 && |
| 3589 |
(lastthrow - firstdtor) > 0; // last throw after first destruction |
| 3590 |
const ptrdiff_t lastPrefix = callerDestroysArgs |
| 3591 |
? lastdtor // up to last argument requiring destruction |
| 3592 |
: lastthrow; // up to last potentially throwing argument |
| 3593 |
|
| 3594 |
/* Problem 3: initialize 'eprefix' by declaring the gate |
| 3595 |
*/ |
| 3596 |
VarDeclaration gate; |
| 3597 |
if (needsPrefix && !callerDestroysArgs) |
| 3598 |
{ |
| 3599 |
// eprefix => bool __gate [= false] |
| 3600 |
Identifier idtmp = Identifier.generateId("__gate"); |
| 3601 |
gate = new VarDeclaration(loc, Type.tbool, idtmp, null); |
| 3602 |
gate.storage_class |= STC.temp | STC.ctfe | STC.volatile_; |
| 3603 |
gate.dsymbolSemantic(sc); |
| 3604 |
|
| 3605 |
auto ae = new DeclarationExp(loc, gate); |
| 3606 |
eprefix = ae.expressionSemantic(sc); |
| 3607 |
} |
| 3608 |
|
| 3609 |
foreach (ptrdiff_t i; 0 .. nargs) |
| 3610 |
{ |
| 3611 |
Expression arg = (*arguments)[i]; |
| 3612 |
//printf("arg[%d]: %s\n", cast(int)i, arg.toChars()); |
| 3613 |
|
| 3614 |
Parameter parameter = i < nparams ? tf.parameterList[i] : null; |
| 3615 |
const bool isRef = parameter && parameter.isReference(); |
| 3616 |
const bool isLazy = parameter && parameter.isLazy(); |
| 3617 |
|
| 3618 |
/* Skip lazy parameters |
| 3619 |
*/ |
| 3620 |
if (isLazy) |
| 3621 |
continue; |
| 3622 |
|
| 3623 |
/* Do we have 'eprefix' and aren't past 'lastPrefix' yet? |
| 3624 |
* Then declare a temporary variable for this arg and append that declaration |
| 3625 |
* to 'eprefix', which will implicitly take care of potential problem 1) for |
| 3626 |
* this arg. |
| 3627 |
* 'eprefix' will therefore finally contain all args up to and including 'lastPrefix', |
| 3628 |
* excluding all lazy parameters. |
| 3629 |
*/ |
| 3630 |
if (needsPrefix && i <= lastPrefix) |
| 3631 |
{ |
| 3632 |
const bool needsDtor = !isRef && arg.type.needsDestruction() && |
| 3633 |
// Problem 3: last throwing arg doesn't require dtor patching |
| 3634 |
(callerDestroysArgs || i != lastPrefix); |
| 3635 |
|
| 3636 |
/* Declare temporary 'auto __pfx = arg' (needsDtor) or 'auto __pfy = arg' (!needsDtor) |
| 3637 |
*/ |
| 3638 |
auto tmp = copyToTemp( |
| 3639 |
(parameter ? parameter.storageClass : tf.parameterList.stc) & (STC.return_ | STC.scope_), |
| 3640 |
needsDtor ? "__pfx" : "__pfy", |
| 3641 |
isRef ? arg.addressOf() : arg); |
| 3642 |
tmp.dsymbolSemantic(sc); |
| 3643 |
|
| 3644 |
if (callerDestroysArgs) |
| 3645 |
{ |
| 3646 |
/* Problem 4: Normal temporary, destructed after the call |
| 3647 |
*/ |
| 3648 |
if (needsDtor) |
| 3649 |
tmp.isArgDtorVar = true; // mark it so that the backend passes it by ref to the function being called |
| 3650 |
} |
| 3651 |
else |
| 3652 |
{ |
| 3653 |
/* Problem 2: Modify the destructor so it only runs if gate==false, |
| 3654 |
* i.e., only if there was a throw while constructing the args |
| 3655 |
*/ |
| 3656 |
if (needsDtor) |
| 3657 |
{ |
| 3658 |
// edtor => (__gate || edtor) |
| 3659 |
assert(tmp.edtor); |
| 3660 |
Expression e = tmp.edtor; |
| 3661 |
e = new LogicalExp(e.loc, EXP.orOr, new VarExp(e.loc, gate), e); |
| 3662 |
tmp.edtor = e.expressionSemantic(sc); |
| 3663 |
//printf("edtor: %s\n", tmp.edtor.toChars()); |
| 3664 |
} |
| 3665 |
else |
| 3666 |
{ |
| 3667 |
if (tmp.edtor) |
| 3668 |
{ |
| 3669 |
assert(i == lastPrefix); |
| 3670 |
tmp.edtor = null; |
| 3671 |
} |
| 3672 |
} |
| 3673 |
} |
| 3674 |
|
| 3675 |
// eprefix => (eprefix, auto __pfx/y = arg) |
| 3676 |
auto ae = new DeclarationExp(loc, tmp); |
| 3677 |
eprefix = Expression.combine(eprefix, ae.expressionSemantic(sc)); |
| 3678 |
|
| 3679 |
// arg => __pfx/y |
| 3680 |
arg = new VarExp(loc, tmp); |
| 3681 |
arg = arg.expressionSemantic(sc); |
| 3682 |
if (isRef) |
| 3683 |
{ |
| 3684 |
arg = new PtrExp(loc, arg); |
| 3685 |
arg = arg.expressionSemantic(sc); |
| 3686 |
} |
| 3687 |
|
| 3688 |
/* Problem 2: Last throwing arg? |
| 3689 |
* Then finalize eprefix => (eprefix, gate = true), i.e., disable the |
| 3690 |
* dtors right after constructing the last throwing arg. |
| 3691 |
* From now on, the callee will take care of destructing the args because |
| 3692 |
* the args are implicitly moved into function parameters. |
| 3693 |
*/ |
| 3694 |
if (!callerDestroysArgs && i == lastPrefix) |
| 3695 |
{ |
| 3696 |
auto e = new AssignExp(gate.loc, new VarExp(gate.loc, gate), IntegerExp.createBool(true)); |
| 3697 |
eprefix = Expression.combine(eprefix, e.expressionSemantic(sc)); |
| 3698 |
} |
| 3699 |
} |
| 3700 |
else // not part of 'eprefix' |
| 3701 |
{ |
| 3702 |
/* Handle problem 1) by calling the copy constructor for value structs |
| 3703 |
* (or static arrays of them) if appropriate. |
| 3704 |
*/ |
| 3705 |
Type tv = arg.type.baseElemOf(); |
| 3706 |
if (!isRef && tv.ty == Tstruct) |
| 3707 |
arg = doCopyOrMove(sc, arg, parameter ? parameter.type : null, false); |
| 3708 |
} |
| 3709 |
|
| 3710 |
(*arguments)[i] = arg; |
| 3711 |
} |
| 3712 |
} |
| 3713 |
//if (eprefix) printf("eprefix: %s\n", eprefix.toChars()); |
| 3714 |
|
| 3715 |
/* Test compliance with DIP1021 Argument Ownership and Function Calls |
| 3716 |
*/ |
| 3717 |
if (sc.previews.dip1021 && (tf.trust == TRUST.safe || tf.trust == TRUST.default_) || |
| 3718 |
tf.isLive) |
| 3719 |
err |= checkMutableArguments(*sc, fd, tf, ethis, arguments, false); |
| 3720 |
|
| 3721 |
// If D linkage and variadic, add _arguments[] as first argument |
| 3722 |
if (tf.isDstyleVariadic()) |
| 3723 |
{ |
| 3724 |
assert(arguments.length >= nparams); |
| 3725 |
|
| 3726 |
auto args = new Parameters(arguments.length - nparams); |
| 3727 |
for (size_t i = 0; i < arguments.length - nparams; i++) |
| 3728 |
{ |
| 3729 |
Expression earg = (*arguments)[nparams + i]; |
| 3730 |
auto arg = new Parameter(earg.loc, STC.in_, earg.type, null, null, null); |
| 3731 |
(*args)[i] = arg; |
| 3732 |
} |
| 3733 |
auto tup = new TypeTuple(args); |
| 3734 |
Expression e = (new TypeidExp(loc, tup)).expressionSemantic(sc); |
| 3735 |
arguments.insert(0, e); |
| 3736 |
} |
| 3737 |
|
| 3738 |
/* Determine function return type: tret |
| 3739 |
*/ |
| 3740 |
Type tret = tf.next; |
| 3741 |
if (isCtorCall) |
| 3742 |
{ |
| 3743 |
//printf("[%s] fd = %s %s, %d %d %d\n", loc.toChars(), fd.toChars(), fd.type.toChars(), |
| 3744 |
// wildmatch, tf.isWild(), fd.isReturnIsolated()); |
| 3745 |
if (!tthis) |
| 3746 |
{ |
| 3747 |
assert(sc.intypeof || global.errors); |
| 3748 |
tthis = fd.isThis().type.addMod(fd.type.mod); |
| 3749 |
} |
| 3750 |
if (tf.isWild() && !fd.isReturnIsolated()) |
| 3751 |
{ |
| 3752 |
if (wildmatch) |
| 3753 |
tret = tret.substWildTo(wildmatch); |
| 3754 |
int offset; |
| 3755 |
if (!tret.implicitConvTo(tthis) && !(MODimplicitConv(tret.mod, tthis.mod) && tret.isBaseOf(tthis, &offset) && offset == 0)) |
| 3756 |
{ |
| 3757 |
const(char)* s1 = tret.isNaked() ? " mutable" : tret.modToChars(); |
| 3758 |
const(char)* s2 = tthis.isNaked() ? " mutable" : tthis.modToChars(); |
| 3759 |
.error(loc, "`inout` constructor `%s` creates%s object, not%s", fd.toPrettyChars(), s1, s2); |
| 3760 |
err = true; |
| 3761 |
} |
| 3762 |
} |
| 3763 |
tret = tthis; |
| 3764 |
} |
| 3765 |
else if (wildmatch && tret) |
| 3766 |
{ |
| 3767 |
/* Adjust function return type based on wildmatch |
| 3768 |
*/ |
| 3769 |
//printf("wildmatch = x%x, tret = %s\n", wildmatch, tret.toChars()); |
| 3770 |
tret = tret.substWildTo(wildmatch); |
| 3771 |
} |
| 3772 |
|
| 3773 |
*prettype = tret; |
| 3774 |
*peprefix = eprefix; |
| 3775 |
return (err || olderrors != global.errors); |
| 3776 |
} |
| 3777 |
|
| 3778 |
/** |
| 3779 |
* Determines whether a symbol represents a module or package |
| 3780 |
* (Used as a helper for is(type == module) and is(type == package)) |
| 3781 |
* |
| 3782 |
* Params: |
| 3783 |
* sym = the symbol to be checked |
| 3784 |
* |
| 3785 |
* Returns: |
| 3786 |
* the symbol which `sym` represents (or `null` if it doesn't represent a `Package`) |
| 3787 |
*/ |
| 3788 |
Package resolveIsPackage(Dsymbol sym) |
| 3789 |
{ |
| 3790 |
Package pkg; |
| 3791 |
if (Import imp = sym.isImport()) |
| 3792 |
{ |
| 3793 |
if (imp.pkg is null) |
| 3794 |
{ |
| 3795 |
.error(sym.loc, "internal compiler error: unable to process forward-referenced import `%s`", |
| 3796 |
imp.toChars()); |
| 3797 |
assert(0); |
| 3798 |
} |
| 3799 |
pkg = imp.pkg; |
| 3800 |
} |
| 3801 |
else if (auto mod = sym.isModule()) |
| 3802 |
pkg = mod.isPackageFile ? mod.pkg : sym.isPackage(); |
| 3803 |
else |
| 3804 |
pkg = sym.isPackage(); |
| 3805 |
if (pkg) |
| 3806 |
pkg.resolvePKGunknown(); |
| 3807 |
return pkg; |
| 3808 |
} |
| 3809 |
|
| 3810 |
|
| 3811 |
private extern (C++) final class ExpressionSemanticVisitor : Visitor |
| 3812 |
{ |
| 3813 |
alias visit = Visitor.visit; |
| 3814 |
|
| 3815 |
Scope* sc; |
| 3816 |
Expression result; |
| 3817 |
|
| 3818 |
// For binary expressions, stores recursive 'alias this' types of lhs and rhs to prevent endless loops. |
| 3819 |
// See tryAliasThisSemantic |
| 3820 |
Type[2] aliasThisStop; |
| 3821 |
|
| 3822 |
// (Optional) the expression this was lowered from, for better error messages |
| 3823 |
Expression parent; |
| 3824 |
|
| 3825 |
this(Scope* sc) scope @safe |
| 3826 |
{ |
| 3827 |
this.sc = sc; |
| 3828 |
} |
| 3829 |
|
| 3830 |
private void setError() |
| 3831 |
{ |
| 3832 |
result = ErrorExp.get(); |
| 3833 |
} |
| 3834 |
|
| 3835 |
private void needThisError(Loc loc, FuncDeclaration f) |
| 3836 |
{ |
| 3837 |
auto t = f.isThis(); |
| 3838 |
assert(t); |
| 3839 |
.error(loc, "calling non-static function `%s` requires an instance of type `%s`", f.toChars(), t.toChars()); |
| 3840 |
setError(); |
| 3841 |
} |
| 3842 |
|
| 3843 |
/************************** |
| 3844 |
* Semantically analyze Expression. |
| 3845 |
* Determine types, fold constants, etc. |
| 3846 |
*/ |
| 3847 |
override void visit(Expression e) |
| 3848 |
{ |
| 3849 |
static if (LOGSEMANTIC) |
| 3850 |
{ |
| 3851 |
printf("Expression::semantic() %s\n", e.toChars()); |
| 3852 |
} |
| 3853 |
if (e.type) |
| 3854 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 3855 |
else |
| 3856 |
e.type = Type.tvoid; |
| 3857 |
result = e; |
| 3858 |
} |
| 3859 |
|
| 3860 |
override void visit(IntegerExp e) |
| 3861 |
{ |
| 3862 |
assert(e.type); |
| 3863 |
if (e.type.ty == Terror) |
| 3864 |
return setError(); |
| 3865 |
|
| 3866 |
assert(e.type.deco); |
| 3867 |
e.setInteger(e.getInteger()); |
| 3868 |
result = e; |
| 3869 |
} |
| 3870 |
|
| 3871 |
override void visit(RealExp e) |
| 3872 |
{ |
| 3873 |
if (!e.type) |
| 3874 |
e.type = Type.tfloat64; |
| 3875 |
else if (!e.type.isImaginary || !sc.inCfile) |
| 3876 |
{ |
| 3877 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 3878 |
result = e; |
| 3879 |
return; |
| 3880 |
} |
| 3881 |
|
| 3882 |
/* Convert to core.stdc.config.complex |
| 3883 |
*/ |
| 3884 |
Type t = getComplexLibraryType(e.loc, sc, e.type.ty); |
| 3885 |
if (t.ty == Terror) |
| 3886 |
return setError(); |
| 3887 |
|
| 3888 |
Type tf; |
| 3889 |
switch (e.type.ty) |
| 3890 |
{ |
| 3891 |
case Timaginary32: tf = Type.tfloat32; break; |
| 3892 |
case Timaginary64: tf = Type.tfloat64; break; |
| 3893 |
case Timaginary80: tf = Type.tfloat80; break; |
| 3894 |
default: |
| 3895 |
assert(0); |
| 3896 |
} |
| 3897 |
|
| 3898 |
/* Construct ts{re : 0.0, im : e} |
| 3899 |
*/ |
| 3900 |
TypeStruct ts = t.isTypeStruct; |
| 3901 |
Expressions* elements = new Expressions(2); |
| 3902 |
(*elements)[0] = new RealExp(e.loc, CTFloat.zero, tf); |
| 3903 |
(*elements)[1] = new RealExp(e.loc, e.toImaginary(), tf); |
| 3904 |
Expression sle = new StructLiteralExp(e.loc, ts.sym, elements); |
| 3905 |
result = sle.expressionSemantic(sc); |
| 3906 |
} |
| 3907 |
|
| 3908 |
override void visit(ComplexExp e) |
| 3909 |
{ |
| 3910 |
if (!e.type) |
| 3911 |
e.type = Type.tcomplex80; |
| 3912 |
else |
| 3913 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 3914 |
result = e; |
| 3915 |
} |
| 3916 |
|
| 3917 |
override void visit(IdentifierExp exp) |
| 3918 |
{ |
| 3919 |
static if (LOGSEMANTIC) |
| 3920 |
{ |
| 3921 |
printf("IdentifierExp::semantic('%s')\n", exp.ident.toChars()); |
| 3922 |
} |
| 3923 |
|
| 3924 |
scope (exit) result.rvalue = exp.rvalue; |
| 3925 |
|
| 3926 |
Dsymbol scopesym; |
| 3927 |
Dsymbol s = sc.search(exp.loc, exp.ident, scopesym); |
| 3928 |
if (s) |
| 3929 |
{ |
| 3930 |
if (s.errors) |
| 3931 |
return setError(); |
| 3932 |
|
| 3933 |
Expression e; |
| 3934 |
|
| 3935 |
/* See if the symbol was a member of an enclosing 'with' |
| 3936 |
*/ |
| 3937 |
WithScopeSymbol withsym = scopesym.isWithScopeSymbol(); |
| 3938 |
if (withsym && withsym.withstate.wthis && symbolIsVisible(sc, s)) |
| 3939 |
{ |
| 3940 |
/* Disallow shadowing |
| 3941 |
*/ |
| 3942 |
// First find the scope of the with |
| 3943 |
Scope* scwith = sc; |
| 3944 |
while (scwith.scopesym != scopesym) |
| 3945 |
{ |
| 3946 |
scwith = scwith.enclosing; |
| 3947 |
assert(scwith); |
| 3948 |
} |
| 3949 |
// Look at enclosing scopes for symbols with the same name, |
| 3950 |
// in the same function |
| 3951 |
for (Scope* scx = scwith; scx && scx.func == scwith.func; scx = scx.enclosing) |
| 3952 |
{ |
| 3953 |
Dsymbol s2; |
| 3954 |
if (scx.scopesym && scx.scopesym.symtab && (s2 = scx.scopesym.symtab.lookup(s.ident)) !is null && s != s2) |
| 3955 |
{ |
| 3956 |
error(exp.loc, "with symbol `%s` is shadowing local symbol `%s`", s.toPrettyChars(), s2.toPrettyChars()); |
| 3957 |
return setError(); |
| 3958 |
} |
| 3959 |
} |
| 3960 |
s = s.toAlias(); |
| 3961 |
|
| 3962 |
// Same as wthis.ident |
| 3963 |
// TODO: DotIdExp.semantic will find 'ident' from 'wthis' again. |
| 3964 |
// The redudancy should be removed. |
| 3965 |
e = new VarExp(exp.loc, withsym.withstate.wthis); |
| 3966 |
e = new DotIdExp(exp.loc, e, exp.ident); |
| 3967 |
e = e.expressionSemantic(sc); |
| 3968 |
} |
| 3969 |
else |
| 3970 |
{ |
| 3971 |
if (withsym) |
| 3972 |
{ |
| 3973 |
if (withsym.withstate.exp.type.ty != Tvoid) |
| 3974 |
{ |
| 3975 |
// 'with (exp)' is a type expression |
| 3976 |
// or 's' is not visible there (for error message) |
| 3977 |
e = new TypeExp(exp.loc, withsym.withstate.exp.type); |
| 3978 |
} |
| 3979 |
else |
| 3980 |
{ |
| 3981 |
// 'with (exp)' is a Package/Module |
| 3982 |
e = withsym.withstate.exp; |
| 3983 |
} |
| 3984 |
e = new DotIdExp(exp.loc, e, exp.ident); |
| 3985 |
result = e.expressionSemantic(sc); |
| 3986 |
return; |
| 3987 |
} |
| 3988 |
|
| 3989 |
/* If f is really a function template, |
| 3990 |
* then replace f with the function template declaration. |
| 3991 |
*/ |
| 3992 |
FuncDeclaration f = s.isFuncDeclaration(); |
| 3993 |
if (f) |
| 3994 |
{ |
| 3995 |
TemplateDeclaration td = getFuncTemplateDecl(f); |
| 3996 |
if (td) |
| 3997 |
{ |
| 3998 |
if (td.overroot) // if not start of overloaded list of TemplateDeclaration's |
| 3999 |
td = td.overroot; // then get the start |
| 4000 |
e = new TemplateExp(exp.loc, td, f); |
| 4001 |
e = e.expressionSemantic(sc); |
| 4002 |
result = e; |
| 4003 |
return; |
| 4004 |
} |
| 4005 |
} |
| 4006 |
|
| 4007 |
if (sc.previews.fixAliasThis) |
| 4008 |
{ |
| 4009 |
if (ExpressionDsymbol expDsym = scopesym.isExpressionDsymbol()) |
| 4010 |
{ |
| 4011 |
//printf("expDsym = %s\n", expDsym.exp.toChars()); |
| 4012 |
result = expDsym.exp.expressionSemantic(sc); |
| 4013 |
return; |
| 4014 |
} |
| 4015 |
} |
| 4016 |
// Haven't done overload resolution yet, so pass 1 |
| 4017 |
e = symbolToExp(s, exp.loc, sc, true); |
| 4018 |
} |
| 4019 |
result = e; |
| 4020 |
return; |
| 4021 |
} |
| 4022 |
|
| 4023 |
if (!sc.previews.fixAliasThis && hasThis(sc)) |
| 4024 |
{ |
| 4025 |
for (AggregateDeclaration ad = sc.getStructClassScope(); ad;) |
| 4026 |
{ |
| 4027 |
if (ad.aliasthis) |
| 4028 |
{ |
| 4029 |
Expression e; |
| 4030 |
e = new ThisExp(exp.loc); |
| 4031 |
e = new DotIdExp(exp.loc, e, ad.aliasthis.ident); |
| 4032 |
e = new DotIdExp(exp.loc, e, exp.ident); |
| 4033 |
e = e.trySemantic(sc); |
| 4034 |
if (e) |
| 4035 |
{ |
| 4036 |
result = e; |
| 4037 |
return; |
| 4038 |
} |
| 4039 |
} |
| 4040 |
|
| 4041 |
auto cd = ad.isClassDeclaration(); |
| 4042 |
if (cd && cd.baseClass && cd.baseClass != ClassDeclaration.object) |
| 4043 |
{ |
| 4044 |
ad = cd.baseClass; |
| 4045 |
continue; |
| 4046 |
} |
| 4047 |
break; |
| 4048 |
} |
| 4049 |
} |
| 4050 |
|
| 4051 |
if (exp.ident == Id.ctfe) |
| 4052 |
{ |
| 4053 |
if (sc.ctfe) |
| 4054 |
{ |
| 4055 |
error(exp.loc, "variable `__ctfe` cannot be read at compile time"); |
| 4056 |
return setError(); |
| 4057 |
} |
| 4058 |
|
| 4059 |
// Create the magic __ctfe bool variable |
| 4060 |
auto vd = new VarDeclaration(exp.loc, Type.tbool, Id.ctfe, null); |
| 4061 |
vd.storage_class |= STC.temp; |
| 4062 |
vd.semanticRun = PASS.semanticdone; |
| 4063 |
Expression e = new VarExp(exp.loc, vd); |
| 4064 |
e = e.expressionSemantic(sc); |
| 4065 |
result = e; |
| 4066 |
return; |
| 4067 |
} |
| 4068 |
|
| 4069 |
// If we've reached this point and are inside a with() scope then we may |
| 4070 |
// try one last attempt by checking whether the 'wthis' object supports |
| 4071 |
// dynamic dispatching via opDispatch. |
| 4072 |
// This is done by rewriting this expression as wthis.ident. |
| 4073 |
// The innermost with() scope of the hierarchy to satisfy the condition |
| 4074 |
// above wins. |
| 4075 |
// https://issues.dlang.org/show_bug.cgi?id=6400 |
| 4076 |
for (Scope* sc2 = sc; sc2; sc2 = sc2.enclosing) |
| 4077 |
{ |
| 4078 |
if (!sc2.scopesym) |
| 4079 |
continue; |
| 4080 |
|
| 4081 |
auto ss = sc2.scopesym.isWithScopeSymbol(); |
| 4082 |
if (!ss) |
| 4083 |
continue; |
| 4084 |
|
| 4085 |
if (ss.withstate.wthis) |
| 4086 |
{ |
| 4087 |
Expression e; |
| 4088 |
e = new VarExp(exp.loc, ss.withstate.wthis); |
| 4089 |
e = new DotIdExp(exp.loc, e, exp.ident); |
| 4090 |
e = e.trySemantic(sc); |
| 4091 |
if (e) |
| 4092 |
{ |
| 4093 |
result = e; |
| 4094 |
return; |
| 4095 |
} |
| 4096 |
} |
| 4097 |
// Try Type.opDispatch (so the static version) |
| 4098 |
else if (ss.withstate.exp && ss.withstate.exp.op == EXP.type) |
| 4099 |
{ |
| 4100 |
Type t = ss.withstate.exp.isTypeExp().type; |
| 4101 |
if (!t) |
| 4102 |
continue; |
| 4103 |
|
| 4104 |
Expression e; |
| 4105 |
e = new TypeExp(exp.loc, t); |
| 4106 |
e = new DotIdExp(exp.loc, e, exp.ident); |
| 4107 |
e = e.trySemantic(sc); |
| 4108 |
if (e) |
| 4109 |
{ |
| 4110 |
result = e; |
| 4111 |
return; |
| 4112 |
} |
| 4113 |
} |
| 4114 |
} |
| 4115 |
|
| 4116 |
/* Look for what user might have meant |
| 4117 |
*/ |
| 4118 |
if (const n = importHint(exp.ident.toString())) |
| 4119 |
error(exp.loc, "`%s` is not defined, perhaps `import %.*s;` is needed?", exp.ident.toChars(), cast(int)n.length, n.ptr); |
| 4120 |
else if (auto s2 = sc.search_correct(exp.ident)) |
| 4121 |
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars()); |
| 4122 |
else if (const p = Scope.search_correct_C(exp.ident)) |
| 4123 |
error(exp.loc, "undefined identifier `%s`, did you mean `%s`?", exp.ident.toChars(), p); |
| 4124 |
else if (exp.ident == Id.dollar) |
| 4125 |
error(exp.loc, "undefined identifier `$`"); |
| 4126 |
else |
| 4127 |
error(exp.loc, "undefined identifier `%s`", exp.ident.toChars()); |
| 4128 |
|
| 4129 |
result = ErrorExp.get(); |
| 4130 |
} |
| 4131 |
|
| 4132 |
override void visit(DsymbolExp e) |
| 4133 |
{ |
| 4134 |
result = symbolToExp(e.s, e.loc, sc, e.hasOverloads); |
| 4135 |
} |
| 4136 |
|
| 4137 |
override void visit(ThisExp e) |
| 4138 |
{ |
| 4139 |
static if (LOGSEMANTIC) |
| 4140 |
{ |
| 4141 |
printf("ThisExp::semantic()\n"); |
| 4142 |
} |
| 4143 |
|
| 4144 |
FuncDeclaration fd = hasThis(sc); // fd is the uplevel function with the 'this' variable |
| 4145 |
AggregateDeclaration ad; |
| 4146 |
|
| 4147 |
/* Special case for typeof(this) and typeof(super) since both |
| 4148 |
* should work even if they are not inside a non-static member function |
| 4149 |
*/ |
| 4150 |
if (!fd && sc.intypeof == 1) |
| 4151 |
{ |
| 4152 |
// Find enclosing struct or class |
| 4153 |
for (Dsymbol s = sc.getStructClassScope(); 1; s = s.parent) |
| 4154 |
{ |
| 4155 |
if (!s) |
| 4156 |
{ |
| 4157 |
error(e.loc, "`%s` is not in a class or struct scope", e.toErrMsg()); |
| 4158 |
return setError(); |
| 4159 |
} |
| 4160 |
ClassDeclaration cd = s.isClassDeclaration(); |
| 4161 |
if (cd) |
| 4162 |
{ |
| 4163 |
e.type = cd.type; |
| 4164 |
result = e; |
| 4165 |
return; |
| 4166 |
} |
| 4167 |
StructDeclaration sd = s.isStructDeclaration(); |
| 4168 |
if (sd) |
| 4169 |
{ |
| 4170 |
e.type = sd.type; |
| 4171 |
result = e; |
| 4172 |
return; |
| 4173 |
} |
| 4174 |
} |
| 4175 |
} |
| 4176 |
if (!fd) |
| 4177 |
{ |
| 4178 |
error(e.loc, "`this` is only defined in non-static member functions, not `%s`", sc.parent.toChars()); |
| 4179 |
return setError(); |
| 4180 |
} |
| 4181 |
|
| 4182 |
assert(fd.vthis); |
| 4183 |
e.var = fd.vthis; |
| 4184 |
assert(e.var.parent); |
| 4185 |
ad = fd.isMemberLocal(); |
| 4186 |
if (!ad) |
| 4187 |
ad = fd.isMember2(); |
| 4188 |
assert(ad); |
| 4189 |
e.type = ad.type.addMod(e.var.type.mod); |
| 4190 |
|
| 4191 |
if (e.var.checkNestedReference(sc, e.loc)) |
| 4192 |
return setError(); |
| 4193 |
|
| 4194 |
result = e; |
| 4195 |
} |
| 4196 |
|
| 4197 |
override void visit(SuperExp e) |
| 4198 |
{ |
| 4199 |
static if (LOGSEMANTIC) |
| 4200 |
{ |
| 4201 |
printf("SuperExp::semantic('%s')\n", e.toChars()); |
| 4202 |
} |
| 4203 |
|
| 4204 |
FuncDeclaration fd = hasThis(sc); |
| 4205 |
ClassDeclaration cd; |
| 4206 |
Dsymbol s; |
| 4207 |
|
| 4208 |
void err() |
| 4209 |
{ |
| 4210 |
error(e.loc, "`super` is only allowed in non-static class member functions"); |
| 4211 |
result = ErrorExp.get(); |
| 4212 |
} |
| 4213 |
/* Special case for typeof(this) and typeof(super) since both |
| 4214 |
* should work even if they are not inside a non-static member function |
| 4215 |
*/ |
| 4216 |
if (!fd && sc.intypeof == 1) |
| 4217 |
{ |
| 4218 |
// Find enclosing class |
| 4219 |
for (s = sc.getStructClassScope(); 1; s = s.parent) |
| 4220 |
{ |
| 4221 |
if (!s) |
| 4222 |
{ |
| 4223 |
error(e.loc, "`%s` is not in a class scope", e.toErrMsg()); |
| 4224 |
return setError(); |
| 4225 |
} |
| 4226 |
cd = s.isClassDeclaration(); |
| 4227 |
if (!cd) |
| 4228 |
continue; |
| 4229 |
|
| 4230 |
cd = cd.baseClass; |
| 4231 |
if (!cd) |
| 4232 |
{ |
| 4233 |
error(e.loc, "class `%s` has no `super`", s.toChars()); |
| 4234 |
return setError(); |
| 4235 |
} |
| 4236 |
e.type = cd.type; |
| 4237 |
result = e; |
| 4238 |
return; |
| 4239 |
} |
| 4240 |
} |
| 4241 |
if (!fd) |
| 4242 |
return err(); |
| 4243 |
|
| 4244 |
e.var = fd.vthis; |
| 4245 |
assert(e.var && e.var.parent); |
| 4246 |
|
| 4247 |
s = fd.toParentDecl(); |
| 4248 |
if (s.isTemplateDeclaration()) // allow inside template constraint |
| 4249 |
s = s.toParent(); |
| 4250 |
assert(s); |
| 4251 |
cd = s.isClassDeclaration(); |
| 4252 |
//printf("parent is %s %s\n", fd.toParent().kind(), fd.toParent().toChars()); |
| 4253 |
if (!cd) |
| 4254 |
return err(); |
| 4255 |
if (!cd.baseClass) |
| 4256 |
{ |
| 4257 |
error(e.loc, "no base class for `%s`", cd.toChars()); |
| 4258 |
e.type = cd.type.addMod(e.var.type.mod); |
| 4259 |
} |
| 4260 |
else |
| 4261 |
{ |
| 4262 |
e.type = cd.baseClass.type; |
| 4263 |
e.type = e.type.castMod(e.var.type.mod); |
| 4264 |
} |
| 4265 |
|
| 4266 |
if (e.var.checkNestedReference(sc, e.loc)) |
| 4267 |
return setError(); |
| 4268 |
|
| 4269 |
result = e; |
| 4270 |
} |
| 4271 |
|
| 4272 |
override void visit(NullExp e) |
| 4273 |
{ |
| 4274 |
static if (LOGSEMANTIC) |
| 4275 |
{ |
| 4276 |
printf("NullExp::semantic('%s')\n", e.toChars()); |
| 4277 |
} |
| 4278 |
// NULL is the same as (void *)0 |
| 4279 |
e.type = Type.tnull; |
| 4280 |
result = e; |
| 4281 |
} |
| 4282 |
|
| 4283 |
override void visit(InterpExp e) |
| 4284 |
{ |
| 4285 |
// the lexer breaks up into an odd/even array of literals and expression code |
| 4286 |
// we need to turn that into: |
| 4287 |
/+ |
| 4288 |
tuple( |
| 4289 |
.object.imported!"core.interpolation".InterpolationHeader(), |
| 4290 |
... |
| 4291 |
.object.imported!"core.interpolation".InterpolationFooter() |
| 4292 |
) |
| 4293 |
|
| 4294 |
There the ... loops through them all, making the even ones |
| 4295 |
.object.imported!"core.interpolation".InterpolatedLiteral!str() |
| 4296 |
and making the odd ones |
| 4297 |
.object.imported!"core.interpolation".InterpolatedExpression!str(), |
| 4298 |
the code represented by str |
| 4299 |
|
| 4300 |
Empty string literals are skipped as they provide no additional information. |
| 4301 |
+/ |
| 4302 |
|
| 4303 |
if (e.postfix) |
| 4304 |
error(e.loc, "String postfixes on interpolated expression sequences are not allowed."); |
| 4305 |
|
| 4306 |
Expression makeNonTemplateItem(Identifier which) { |
| 4307 |
Expression id = new IdentifierExp(e.loc, Id.empty); |
| 4308 |
id = new DotIdExp(e.loc, id, Id.object); |
| 4309 |
auto moduleNameArgs = new Objects(); |
| 4310 |
moduleNameArgs.push(new StringExp(e.loc, "core.interpolation")); |
| 4311 |
id = new DotTemplateInstanceExp(e.loc, id, Id.imported, moduleNameArgs); |
| 4312 |
id = new DotIdExp(e.loc, id, which); |
| 4313 |
id = new CallExp(e.loc, id, new Expressions()); |
| 4314 |
return id; |
| 4315 |
} |
| 4316 |
|
| 4317 |
Expression makeTemplateItem(Identifier which, string arg) { |
| 4318 |
Expression id = new IdentifierExp(e.loc, Id.empty); |
| 4319 |
id = new DotIdExp(e.loc, id, Id.object); |
| 4320 |
auto moduleNameArgs = new Objects(); |
| 4321 |
moduleNameArgs.push(new StringExp(e.loc, "core.interpolation")); |
| 4322 |
id = new DotTemplateInstanceExp(e.loc, id, Id.imported, moduleNameArgs); |
| 4323 |
auto tiargs = new Objects(); |
| 4324 |
auto templateStringArg = new StringExp(e.loc, arg); |
| 4325 |
// banning those instead of forwarding them |
| 4326 |
// templateStringArg.postfix = e.postfix; // forward the postfix to these literals |
| 4327 |
tiargs.push(templateStringArg); |
| 4328 |
id = new DotTemplateInstanceExp(e.loc, id, which, tiargs); |
| 4329 |
id = new CallExp(e.loc, id, new Expressions()); |
| 4330 |
return id; |
| 4331 |
} |
| 4332 |
|
| 4333 |
auto arguments = new Expressions(); |
| 4334 |
arguments.push(makeNonTemplateItem(Id.InterpolationHeader)); |
| 4335 |
|
| 4336 |
foreach (idx, str; e.interpolatedSet.parts) |
| 4337 |
{ |
| 4338 |
if (idx % 2 == 0) |
| 4339 |
{ |
| 4340 |
if (str.length > 0) |
| 4341 |
arguments.push(makeTemplateItem(Id.InterpolatedLiteral, str)); |
| 4342 |
} |
| 4343 |
else |
| 4344 |
{ |
| 4345 |
arguments.push(makeTemplateItem(Id.InterpolatedExpression, str)); |
| 4346 |
Expressions* mix = new Expressions(); |
| 4347 |
mix.push(new StringExp(e.loc, str)); |
| 4348 |
// FIXME: i'd rather not use MixinExp but idk how to do it lol |
| 4349 |
arguments.push(new MixinExp(e.loc, mix)); |
| 4350 |
} |
| 4351 |
} |
| 4352 |
|
| 4353 |
arguments.push(makeNonTemplateItem(Id.InterpolationFooter)); |
| 4354 |
|
| 4355 |
auto loweredTo = new TupleExp(e.loc, arguments); |
| 4356 |
visit(loweredTo); |
| 4357 |
|
| 4358 |
result = loweredTo; |
| 4359 |
} |
| 4360 |
|
| 4361 |
override void visit(StringExp e) |
| 4362 |
{ |
| 4363 |
static if (LOGSEMANTIC) |
| 4364 |
{ |
| 4365 |
printf("StringExp::semantic() %s\n", e.toChars()); |
| 4366 |
} |
| 4367 |
|
| 4368 |
OutBuffer buffer; |
| 4369 |
size_t newlen = 0; |
| 4370 |
size_t u; |
| 4371 |
dchar c; |
| 4372 |
|
| 4373 |
if (e.hexString) |
| 4374 |
{ |
| 4375 |
switch (e.postfix) |
| 4376 |
{ |
| 4377 |
case 'd': |
| 4378 |
e.committed = true; |
| 4379 |
e.sz = 4; |
| 4380 |
e.type = Type.tdstring; |
| 4381 |
break; |
| 4382 |
case 'w': |
| 4383 |
e.committed = true; |
| 4384 |
e.sz = 2; |
| 4385 |
e.type = Type.twstring; |
| 4386 |
break; |
| 4387 |
case 'c': |
| 4388 |
e.committed = true; |
| 4389 |
goto default; |
| 4390 |
default: |
| 4391 |
e.type = Type.tstring; |
| 4392 |
e.sz = 1; |
| 4393 |
break; |
| 4394 |
} |
| 4395 |
if ((e.len % e.sz) != 0) |
| 4396 |
error(e.loc, "hex string with `%s` type needs to be multiple of %d bytes, not %d", |
| 4397 |
e.type.toChars(), e.sz, cast(int) e.len); |
| 4398 |
|
| 4399 |
e.setData(arrayCastBigEndian(e.peekData(), e.sz).ptr, e.len / e.sz, e.sz); |
| 4400 |
} |
| 4401 |
else switch (e.postfix) |
| 4402 |
{ |
| 4403 |
case 'd': |
| 4404 |
for (u = 0; u < e.len;) |
| 4405 |
{ |
| 4406 |
if (const p = utf_decodeChar(e.peekString(), u, c)) |
| 4407 |
{ |
| 4408 |
error(e.loc, "%.*s", cast(int)p.length, p.ptr); |
| 4409 |
return setError(); |
| 4410 |
} |
| 4411 |
else |
| 4412 |
{ |
| 4413 |
buffer.write4(c); |
| 4414 |
newlen++; |
| 4415 |
} |
| 4416 |
} |
| 4417 |
buffer.write4(0); |
| 4418 |
e.setData(buffer.extractData(), newlen, 4); |
| 4419 |
if (sc && sc.inCfile) |
| 4420 |
e.type = Type.tuns32.sarrayOf(e.len + 1); |
| 4421 |
else |
| 4422 |
e.type = Type.tdchar.immutableOf().arrayOf(); |
| 4423 |
e.committed = true; |
| 4424 |
break; |
| 4425 |
|
| 4426 |
case 'w': |
| 4427 |
for (u = 0; u < e.len;) |
| 4428 |
{ |
| 4429 |
if (const p = utf_decodeChar(e.peekString(), u, c)) |
| 4430 |
{ |
| 4431 |
error(e.loc, "%.*s", cast(int)p.length, p.ptr); |
| 4432 |
return setError(); |
| 4433 |
} |
| 4434 |
else |
| 4435 |
{ |
| 4436 |
buffer.writeUTF16(c); |
| 4437 |
newlen++; |
| 4438 |
if (c >= 0x10000) |
| 4439 |
newlen++; |
| 4440 |
} |
| 4441 |
} |
| 4442 |
buffer.writeUTF16(0); |
| 4443 |
e.setData(buffer.extractData(), newlen, 2); |
| 4444 |
if (sc && sc.inCfile) |
| 4445 |
e.type = Type.tuns16.sarrayOf(e.len + 1); |
| 4446 |
else |
| 4447 |
e.type = Type.twchar.immutableOf().arrayOf(); |
| 4448 |
e.committed = true; |
| 4449 |
break; |
| 4450 |
|
| 4451 |
case 'c': |
| 4452 |
e.committed = true; |
| 4453 |
goto default; |
| 4454 |
|
| 4455 |
default: |
| 4456 |
if (sc && sc.inCfile) |
| 4457 |
e.type = Type.tchar.sarrayOf(e.len + 1); |
| 4458 |
else |
| 4459 |
e.type = Type.tchar.immutableOf().arrayOf(); |
| 4460 |
break; |
| 4461 |
} |
| 4462 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 4463 |
//type = type.immutableOf(); |
| 4464 |
//printf("type = %s\n", type.toChars()); |
| 4465 |
|
| 4466 |
result = e; |
| 4467 |
} |
| 4468 |
|
| 4469 |
override void visit(TupleExp exp) |
| 4470 |
{ |
| 4471 |
static if (LOGSEMANTIC) |
| 4472 |
{ |
| 4473 |
printf("+TupleExp::semantic(%s)\n", exp.toChars()); |
| 4474 |
} |
| 4475 |
|
| 4476 |
if (exp.e0) |
| 4477 |
exp.e0 = exp.e0.expressionSemantic(sc); |
| 4478 |
|
| 4479 |
// Run semantic() on each argument |
| 4480 |
bool err = false; |
| 4481 |
for (size_t i = 0; i < exp.exps.length; i++) |
| 4482 |
{ |
| 4483 |
Expression e = (*exp.exps)[i]; |
| 4484 |
e = e.expressionSemantic(sc); |
| 4485 |
if (!e.type) |
| 4486 |
{ |
| 4487 |
error(exp.loc, "`%s` has no value", e.toErrMsg()); |
| 4488 |
err = true; |
| 4489 |
} |
| 4490 |
else if (e.op == EXP.error) |
| 4491 |
err = true; |
| 4492 |
else |
| 4493 |
(*exp.exps)[i] = e; |
| 4494 |
} |
| 4495 |
if (err) |
| 4496 |
return setError(); |
| 4497 |
|
| 4498 |
expandTuples(exp.exps); |
| 4499 |
|
| 4500 |
exp.type = new TypeTuple(exp.exps); |
| 4501 |
exp.type = exp.type.typeSemantic(exp.loc, sc); |
| 4502 |
//printf("-TupleExp::semantic(%s)\n", toChars()); |
| 4503 |
result = exp; |
| 4504 |
} |
| 4505 |
|
| 4506 |
override void visit(ArrayLiteralExp e) |
| 4507 |
{ |
| 4508 |
static if (LOGSEMANTIC) |
| 4509 |
{ |
| 4510 |
printf("ArrayLiteralExp::semantic('%s')\n", e.toChars()); |
| 4511 |
} |
| 4512 |
|
| 4513 |
/* Perhaps an empty array literal [ ] should be rewritten as null? |
| 4514 |
*/ |
| 4515 |
|
| 4516 |
if (e.basis) |
| 4517 |
e.basis = e.basis.expressionSemantic(sc); |
| 4518 |
if (arrayExpressionSemantic(e.elements.peekSlice(), sc) || (e.basis && e.basis.op == EXP.error)) |
| 4519 |
return setError(); |
| 4520 |
|
| 4521 |
expandTuples(e.elements); |
| 4522 |
|
| 4523 |
if (e.basis) |
| 4524 |
e.elements.push(e.basis); |
| 4525 |
Type t0 = arrayExpressionToCommonType(sc, *e.elements); |
| 4526 |
if (e.basis) |
| 4527 |
e.basis = e.elements.pop(); |
| 4528 |
if (t0 is null) |
| 4529 |
return setError(); |
| 4530 |
|
| 4531 |
e.type = t0.arrayOf(); |
| 4532 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 4533 |
|
| 4534 |
/* Disallow array literals of type void being used. |
| 4535 |
*/ |
| 4536 |
if (e.elements.length > 0 && t0.ty == Tvoid) |
| 4537 |
{ |
| 4538 |
error(e.loc, "`%s` of type `%s` has no value", e.toErrMsg(), e.type.toChars()); |
| 4539 |
return setError(); |
| 4540 |
} |
| 4541 |
|
| 4542 |
if (global.params.useTypeInfo && Type.dtypeinfo) |
| 4543 |
semanticTypeInfo(sc, e.type); |
| 4544 |
|
| 4545 |
result = e; |
| 4546 |
} |
| 4547 |
|
| 4548 |
override void visit(AssocArrayLiteralExp e) |
| 4549 |
{ |
| 4550 |
static if (LOGSEMANTIC) |
| 4551 |
{ |
| 4552 |
printf("AssocArrayLiteralExp::semantic('%s')\n", e.toChars()); |
| 4553 |
} |
| 4554 |
|
| 4555 |
// Run semantic() on each element |
| 4556 |
bool err_keys = arrayExpressionSemantic(e.keys.peekSlice(), sc); |
| 4557 |
bool err_vals = arrayExpressionSemantic(e.values.peekSlice(), sc); |
| 4558 |
if (err_keys || err_vals) |
| 4559 |
return setError(); |
| 4560 |
|
| 4561 |
expandTuples(e.keys); |
| 4562 |
expandTuples(e.values); |
| 4563 |
if (e.keys.length != e.values.length) |
| 4564 |
{ |
| 4565 |
error(e.loc, "number of keys is %llu, must match number of values %llu", |
| 4566 |
cast(ulong) e.keys.length, cast(ulong) e.values.length); |
| 4567 |
return setError(); |
| 4568 |
} |
| 4569 |
|
| 4570 |
Type tkey = arrayExpressionToCommonType(sc, *e.keys); |
| 4571 |
Type tvalue = arrayExpressionToCommonType(sc, *e.values); |
| 4572 |
if (tkey is null || tvalue is null) |
| 4573 |
return setError(); |
| 4574 |
|
| 4575 |
e.type = new TypeAArray(tvalue, tkey); |
| 4576 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 4577 |
|
| 4578 |
semanticTypeInfo(sc, e.type); |
| 4579 |
|
| 4580 |
if (checkAssocArrayLiteralEscape(*sc, e, false)) |
| 4581 |
return setError(); |
| 4582 |
|
| 4583 |
result = e; |
| 4584 |
} |
| 4585 |
|
| 4586 |
override void visit(StructLiteralExp e) |
| 4587 |
{ |
| 4588 |
static if (LOGSEMANTIC) |
| 4589 |
{ |
| 4590 |
printf("StructLiteralExp::semantic('%s')\n", e.toChars()); |
| 4591 |
} |
| 4592 |
|
| 4593 |
e.sd.size(e.loc); |
| 4594 |
if (e.sd.sizeok != Sizeok.done) |
| 4595 |
return setError(); |
| 4596 |
|
| 4597 |
// run semantic() on each element |
| 4598 |
if (arrayExpressionSemantic(e.elements.peekSlice(), sc)) |
| 4599 |
return setError(); |
| 4600 |
|
| 4601 |
expandTuples(e.elements); |
| 4602 |
|
| 4603 |
/* Fit elements[] to the corresponding type of field[]. |
| 4604 |
*/ |
| 4605 |
if (!e.sd.fit(e.loc, sc, e.elements, e.stype)) |
| 4606 |
return setError(); |
| 4607 |
|
| 4608 |
/* Fill out remainder of elements[] with default initializers for fields[] |
| 4609 |
*/ |
| 4610 |
if (!e.sd.fill(e.loc, *e.elements, false)) |
| 4611 |
{ |
| 4612 |
/* An error in the initializer needs to be recorded as an error |
| 4613 |
* in the enclosing function or template, since the initializer |
| 4614 |
* will be part of the stuct declaration. |
| 4615 |
*/ |
| 4616 |
global.increaseErrorCount(); |
| 4617 |
return setError(); |
| 4618 |
} |
| 4619 |
|
| 4620 |
if (checkFrameAccess(e.loc, sc, e.sd, e.elements.length)) |
| 4621 |
return setError(); |
| 4622 |
|
| 4623 |
e.type = e.stype ? e.stype : e.sd.type; |
| 4624 |
result = e; |
| 4625 |
} |
| 4626 |
|
| 4627 |
override void visit(CompoundLiteralExp cle) |
| 4628 |
{ |
| 4629 |
static if (LOGSEMANTIC) |
| 4630 |
{ |
| 4631 |
printf("CompoundLiteralExp::semantic('%s')\n", cle.toChars()); |
| 4632 |
} |
| 4633 |
Type t = cle.type.typeSemantic(cle.loc, sc); |
| 4634 |
auto init = initializerSemantic(cle.initializer, sc, t, INITnointerpret); |
| 4635 |
auto e = initializerToExpression(init, t, sc.inCfile); |
| 4636 |
if (!e) |
| 4637 |
{ |
| 4638 |
error(cle.loc, "cannot convert initializer `%s` to expression", toChars(init)); |
| 4639 |
return setError(); |
| 4640 |
} |
| 4641 |
result = e; |
| 4642 |
return; |
| 4643 |
} |
| 4644 |
|
| 4645 |
override void visit(TypeExp exp) |
| 4646 |
{ |
| 4647 |
if (exp.type.ty == Terror) |
| 4648 |
return setError(); |
| 4649 |
|
| 4650 |
//printf("TypeExp::semantic(%s)\n", exp.type.toChars()); |
| 4651 |
Expression e; |
| 4652 |
Type t; |
| 4653 |
Dsymbol s; |
| 4654 |
|
| 4655 |
dmd.typesem.resolve(exp.type, exp.loc, sc, e, t, s, true); |
| 4656 |
if (e) |
| 4657 |
{ |
| 4658 |
// `(Type)` is actually `(var)` so if `(var)` is a member requiring `this` |
| 4659 |
// then rewrite as `(this.var)` in case it would be followed by a DotVar |
| 4660 |
// to fix https://issues.dlang.org/show_bug.cgi?id=9490 |
| 4661 |
VarExp ve = e.isVarExp(); |
| 4662 |
if (ve && ve.var && exp.parens && !ve.var.isStatic() && !(sc.stc & STC.static_) && |
| 4663 |
sc.func && sc.func.needThis && ve.var.isMember2()) |
| 4664 |
{ |
| 4665 |
// printf("apply fix for bugzilla issue 9490: add `this.` to `%s`...\n", e.toChars()); |
| 4666 |
e = new DotVarExp(exp.loc, new ThisExp(exp.loc), ve.var, false); |
| 4667 |
} |
| 4668 |
//printf("e = %s %s\n", Token.toChars(e.op), e.toChars()); |
| 4669 |
e = e.expressionSemantic(sc); |
| 4670 |
} |
| 4671 |
else if (t) |
| 4672 |
{ |
| 4673 |
//printf("t = %d %s\n", t.ty, t.toChars()); |
| 4674 |
exp.type = t.typeSemantic(exp.loc, sc); |
| 4675 |
e = exp; |
| 4676 |
} |
| 4677 |
else if (s) |
| 4678 |
{ |
| 4679 |
//printf("s = %s %s\n", s.kind(), s.toChars()); |
| 4680 |
e = symbolToExp(s, exp.loc, sc, true); |
| 4681 |
} |
| 4682 |
else |
| 4683 |
assert(0); |
| 4684 |
|
| 4685 |
exp.type.checkComplexTransition(exp.loc, sc); |
| 4686 |
|
| 4687 |
result = e; |
| 4688 |
} |
| 4689 |
|
| 4690 |
override void visit(ScopeExp exp) |
| 4691 |
{ |
| 4692 |
static if (LOGSEMANTIC) |
| 4693 |
{ |
| 4694 |
printf("+ScopeExp::semantic(%p '%s')\n", exp, exp.toChars()); |
| 4695 |
} |
| 4696 |
|
| 4697 |
ScopeDsymbol sds2 = exp.sds; |
| 4698 |
TemplateInstance ti = sds2.isTemplateInstance(); |
| 4699 |
while (ti) |
| 4700 |
{ |
| 4701 |
WithScopeSymbol withsym; |
| 4702 |
if (!ti.findTempDecl(sc, &withsym) || !ti.semanticTiargs(sc)) |
| 4703 |
return setError(); |
| 4704 |
if (withsym && withsym.withstate.wthis) |
| 4705 |
{ |
| 4706 |
Expression e = new VarExp(exp.loc, withsym.withstate.wthis); |
| 4707 |
e = new DotTemplateInstanceExp(exp.loc, e, ti); |
| 4708 |
result = e.expressionSemantic(sc); |
| 4709 |
return; |
| 4710 |
} |
| 4711 |
if (ti.needsTypeInference(sc)) |
| 4712 |
{ |
| 4713 |
if (TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration()) |
| 4714 |
{ |
| 4715 |
Dsymbol p = td.toParentLocal(); |
| 4716 |
FuncDeclaration fdthis = hasThis(sc); |
| 4717 |
AggregateDeclaration ad = p ? p.isAggregateDeclaration() : null; |
| 4718 |
if (fdthis && ad && fdthis.isMemberLocal() == ad && (td._scope.stc & STC.static_) == 0) |
| 4719 |
{ |
| 4720 |
Expression e = new DotTemplateInstanceExp(exp.loc, new ThisExp(exp.loc), ti); |
| 4721 |
result = e.expressionSemantic(sc); |
| 4722 |
return; |
| 4723 |
} |
| 4724 |
} |
| 4725 |
else if (OverloadSet os = ti.tempdecl.isOverloadSet()) |
| 4726 |
{ |
| 4727 |
FuncDeclaration fdthis = hasThis(sc); |
| 4728 |
AggregateDeclaration ad = os.parent.isAggregateDeclaration(); |
| 4729 |
if (fdthis && ad && fdthis.isMemberLocal() == ad) |
| 4730 |
{ |
| 4731 |
Expression e = new DotTemplateInstanceExp(exp.loc, new ThisExp(exp.loc), ti); |
| 4732 |
result = e.expressionSemantic(sc); |
| 4733 |
return; |
| 4734 |
} |
| 4735 |
} |
| 4736 |
// ti is an instance which requires IFTI. |
| 4737 |
exp.sds = ti; |
| 4738 |
exp.type = Type.tvoid; |
| 4739 |
result = exp; |
| 4740 |
return; |
| 4741 |
} |
| 4742 |
ti.dsymbolSemantic(sc); |
| 4743 |
if (!ti.inst || ti.errors) |
| 4744 |
return setError(); |
| 4745 |
|
| 4746 |
Dsymbol s = ti.toAlias(); |
| 4747 |
if (s == ti) |
| 4748 |
{ |
| 4749 |
exp.sds = ti; |
| 4750 |
exp.type = Type.tvoid; |
| 4751 |
result = exp; |
| 4752 |
return; |
| 4753 |
} |
| 4754 |
sds2 = s.isScopeDsymbol(); |
| 4755 |
if (sds2) |
| 4756 |
{ |
| 4757 |
ti = sds2.isTemplateInstance(); |
| 4758 |
//printf("+ sds2 = %s, '%s'\n", sds2.kind(), sds2.toChars()); |
| 4759 |
continue; |
| 4760 |
} |
| 4761 |
|
| 4762 |
if (auto v = s.isVarDeclaration()) |
| 4763 |
{ |
| 4764 |
if (!v.type) |
| 4765 |
{ |
| 4766 |
error(exp.loc, "forward reference of %s `%s`", v.kind(), v.toChars()); |
| 4767 |
return setError(); |
| 4768 |
} |
| 4769 |
if ((v.storage_class & STC.manifest) && v._init) |
| 4770 |
{ |
| 4771 |
/* When an instance that will be converted to a constant exists, |
| 4772 |
* the instance representation "foo!tiargs" is treated like a |
| 4773 |
* variable name, and its recursive appearance check (note that |
| 4774 |
* it's equivalent with a recursive instantiation of foo) is done |
| 4775 |
* separately from the circular initialization check for the |
| 4776 |
* eponymous enum variable declaration. |
| 4777 |
* |
| 4778 |
* template foo(T) { |
| 4779 |
* enum bool foo = foo; // recursive definition check (v.inuse) |
| 4780 |
* } |
| 4781 |
* template bar(T) { |
| 4782 |
* enum bool bar = bar!T; // recursive instantiation check (ti.inuse) |
| 4783 |
* } |
| 4784 |
*/ |
| 4785 |
if (ti.inuse) |
| 4786 |
{ |
| 4787 |
error(exp.loc, "recursive expansion of %s `%s`", ti.kind(), ti.toPrettyChars()); |
| 4788 |
return setError(); |
| 4789 |
} |
| 4790 |
v.checkDeprecated(exp.loc, sc); |
| 4791 |
auto e = v.expandInitializer(exp.loc); |
| 4792 |
ti.inuse++; |
| 4793 |
e = e.expressionSemantic(sc); |
| 4794 |
ti.inuse--; |
| 4795 |
result = e; |
| 4796 |
return; |
| 4797 |
} |
| 4798 |
} |
| 4799 |
|
| 4800 |
//printf("s = %s, '%s'\n", s.kind(), s.toChars()); |
| 4801 |
auto e = symbolToExp(s, exp.loc, sc, true); |
| 4802 |
//printf("-1ScopeExp::semantic()\n"); |
| 4803 |
result = e; |
| 4804 |
return; |
| 4805 |
} |
| 4806 |
|
| 4807 |
//printf("sds2 = %s, '%s'\n", sds2.kind(), sds2.toChars()); |
| 4808 |
//printf("\tparent = '%s'\n", sds2.parent.toChars()); |
| 4809 |
sds2.dsymbolSemantic(sc); |
| 4810 |
|
| 4811 |
// (Aggregate|Enum)Declaration |
| 4812 |
if (auto t = sds2.getType()) |
| 4813 |
{ |
| 4814 |
result = (new TypeExp(exp.loc, t)).expressionSemantic(sc); |
| 4815 |
return; |
| 4816 |
} |
| 4817 |
|
| 4818 |
if (auto td = sds2.isTemplateDeclaration()) |
| 4819 |
{ |
| 4820 |
result = (new TemplateExp(exp.loc, td)).expressionSemantic(sc); |
| 4821 |
return; |
| 4822 |
} |
| 4823 |
|
| 4824 |
exp.sds = sds2; |
| 4825 |
exp.type = Type.tvoid; |
| 4826 |
//printf("-2ScopeExp::semantic() %s\n", toChars()); |
| 4827 |
result = exp; |
| 4828 |
} |
| 4829 |
|
| 4830 |
/** |
| 4831 |
* Sets the `lowering` field of a `NewExp` to a call to `_d_newitemT` unless |
| 4832 |
* compiling with `-betterC` or within `__traits(compiles)`. |
| 4833 |
* |
| 4834 |
* Params: |
| 4835 |
* ne = the `NewExp` to lower |
| 4836 |
*/ |
| 4837 |
private void tryLowerToNewItem(NewExp ne) |
| 4838 |
{ |
| 4839 |
if (!global.params.useGC || !sc.needsCodegen()) |
| 4840 |
return; |
| 4841 |
|
| 4842 |
auto hook = global.params.tracegc ? Id._d_newitemTTrace : Id._d_newitemT; |
| 4843 |
if (!verifyHookExist(ne.loc, *sc, hook, "new struct")) |
| 4844 |
return; |
| 4845 |
|
| 4846 |
/* Lower the memory allocation and initialization of `new T()` to |
| 4847 |
* `_d_newitemT!T()`. |
| 4848 |
*/ |
| 4849 |
Expression id = new IdentifierExp(ne.loc, Id.empty); |
| 4850 |
id = new DotIdExp(ne.loc, id, Id.object); |
| 4851 |
auto tiargs = new Objects(); |
| 4852 |
/* |
| 4853 |
* Remove `inout`, `const`, `immutable` and `shared` to reduce the |
| 4854 |
* number of generated `_d_newitemT` instances. |
| 4855 |
*/ |
| 4856 |
auto t = ne.type.nextOf.unqualify(MODFlags.wild | MODFlags.const_ | |
| 4857 |
MODFlags.immutable_ | MODFlags.shared_); |
| 4858 |
tiargs.push(t); |
| 4859 |
id = new DotTemplateInstanceExp(ne.loc, id, hook, tiargs); |
| 4860 |
|
| 4861 |
auto arguments = new Expressions(); |
| 4862 |
id = new CallExp(ne.loc, id, arguments); |
| 4863 |
|
| 4864 |
ne.lowering = id.expressionSemantic(sc); |
| 4865 |
} |
| 4866 |
|
| 4867 |
override void visit(NewExp exp) |
| 4868 |
{ |
| 4869 |
static if (LOGSEMANTIC) |
| 4870 |
{ |
| 4871 |
printf("NewExp::semantic() %s\n", exp.toChars()); |
| 4872 |
if (exp.thisexp) |
| 4873 |
printf("\tthisexp = %s\n", exp.thisexp.toChars()); |
| 4874 |
printf("\tnewtype: %s\n", exp.newtype.toChars()); |
| 4875 |
} |
| 4876 |
|
| 4877 |
if (exp.placement) |
| 4878 |
{ |
| 4879 |
exp.placement = exp.placement.expressionSemantic(sc); |
| 4880 |
auto p = exp.placement; |
| 4881 |
if (p.op == EXP.error) |
| 4882 |
return setError(); |
| 4883 |
if (!p.isLvalue()) |
| 4884 |
{ |
| 4885 |
error(p.loc, "PlacementExpression `%s` is an rvalue, but must be an lvalue", p.toChars()); |
| 4886 |
return setError(); |
| 4887 |
} |
| 4888 |
if (sc.setUnsafe(false, p.loc, "`@safe` function `%s` cannot use placement `new`", sc.func)) |
| 4889 |
{ |
| 4890 |
return setError(); |
| 4891 |
} |
| 4892 |
if (!exp.placement.type.isNaked()) |
| 4893 |
{ |
| 4894 |
error(p.loc, "PlacementExpression `%s` of type `%s` be unshared and mutable", p.toChars(), toChars(p.type)); |
| 4895 |
return setError(); |
| 4896 |
} |
| 4897 |
} |
| 4898 |
|
| 4899 |
//for error messages if the argument in [] is not convertible to size_t |
| 4900 |
const originalNewtype = exp.newtype; |
| 4901 |
|
| 4902 |
// https://issues.dlang.org/show_bug.cgi?id=11581 |
| 4903 |
// With the syntax `new T[edim]` or `thisexp.new T[edim]`, |
| 4904 |
// T should be analyzed first and edim should go into arguments iff it's |
| 4905 |
// not a tuple. |
| 4906 |
Expression edim = null; |
| 4907 |
if (!exp.arguments && exp.newtype.isTypeSArray()) |
| 4908 |
{ |
| 4909 |
auto ts = exp.newtype.isTypeSArray(); |
| 4910 |
// check `new Value[Key]` |
| 4911 |
ts.dim = ts.dim.expressionSemantic(sc); |
| 4912 |
if (ts.dim.op == EXP.type) |
| 4913 |
{ |
| 4914 |
exp.newtype = new TypeAArray(ts.next, ts.dim.isTypeExp().type); |
| 4915 |
} |
| 4916 |
else |
| 4917 |
{ |
| 4918 |
edim = ts.dim; |
| 4919 |
exp.newtype = ts.next; |
| 4920 |
} |
| 4921 |
} |
| 4922 |
|
| 4923 |
ClassDeclaration cdthis = null; |
| 4924 |
if (exp.thisexp) |
| 4925 |
{ |
| 4926 |
exp.thisexp = exp.thisexp.expressionSemantic(sc); |
| 4927 |
if (exp.thisexp.op == EXP.error) |
| 4928 |
return setError(); |
| 4929 |
|
| 4930 |
cdthis = exp.thisexp.type.isClassHandle(); |
| 4931 |
if (!cdthis) |
| 4932 |
{ |
| 4933 |
error(exp.loc, "`this` for nested class must be a class type, not `%s`", exp.thisexp.type.toChars()); |
| 4934 |
return setError(); |
| 4935 |
} |
| 4936 |
|
| 4937 |
sc = sc.push(cdthis); |
| 4938 |
exp.type = exp.newtype.typeSemantic(exp.loc, sc); |
| 4939 |
sc = sc.pop(); |
| 4940 |
} |
| 4941 |
else |
| 4942 |
{ |
| 4943 |
exp.type = exp.newtype.typeSemantic(exp.loc, sc); |
| 4944 |
} |
| 4945 |
if (exp.type.ty == Terror) |
| 4946 |
return setError(); |
| 4947 |
|
| 4948 |
if (edim) |
| 4949 |
{ |
| 4950 |
if (exp.type.toBasetype().ty == Ttuple) |
| 4951 |
{ |
| 4952 |
// --> new T[edim] |
| 4953 |
exp.type = new TypeSArray(exp.type, edim); |
| 4954 |
exp.type = exp.type.typeSemantic(exp.loc, sc); |
| 4955 |
if (exp.type.ty == Terror) |
| 4956 |
return setError(); |
| 4957 |
} |
| 4958 |
else |
| 4959 |
{ |
| 4960 |
// --> new T[](edim) |
| 4961 |
exp.arguments = new Expressions(); |
| 4962 |
exp.arguments.push(edim); |
| 4963 |
exp.type = exp.type.arrayOf(); |
| 4964 |
} |
| 4965 |
} |
| 4966 |
|
| 4967 |
exp.newtype = exp.type; // in case type gets cast to something else |
| 4968 |
Type tb = exp.type.toBasetype(); |
| 4969 |
//printf("tb: %s, deco = %s\n", tb.toChars(), tb.deco); |
| 4970 |
if (arrayExpressionSemantic(exp.arguments.peekSlice(), sc)) |
| 4971 |
{ |
| 4972 |
return setError(); |
| 4973 |
} |
| 4974 |
if (preFunctionParameters(sc, exp.argumentList, global.errorSink)) |
| 4975 |
{ |
| 4976 |
return setError(); |
| 4977 |
} |
| 4978 |
|
| 4979 |
if (exp.thisexp && tb.ty != Tclass) |
| 4980 |
{ |
| 4981 |
error(exp.loc, "`.new` is only for allocating nested classes, not `%s`", tb.toChars()); |
| 4982 |
return setError(); |
| 4983 |
} |
| 4984 |
|
| 4985 |
uinteger_t placementSize; |
| 4986 |
if (exp.placement) |
| 4987 |
{ |
| 4988 |
placementSize = size(exp.placement.type, exp.placement.loc); |
| 4989 |
auto objectSize = size(tb, exp.placement.loc); |
| 4990 |
//printf("placementSize: %lld objectSize: %lld\n", placementSize, objectSize); |
| 4991 |
if (!tb.isTypeClass && placementSize < objectSize) |
| 4992 |
{ |
| 4993 |
error(exp.placement.loc, "new placement size %llu must be >= object size %llu", placementSize, objectSize); |
| 4994 |
return setError(); |
| 4995 |
} |
| 4996 |
} |
| 4997 |
|
| 4998 |
const size_t nargs = exp.arguments ? exp.arguments.length : 0; |
| 4999 |
Expression newprefix = null; |
| 5000 |
|
| 5001 |
if (auto tc = tb.isTypeClass()) |
| 5002 |
{ |
| 5003 |
auto cd = tc.sym; |
| 5004 |
if (cd.errors) |
| 5005 |
return setError(); |
| 5006 |
auto objectSize = cd.size(exp.loc); |
| 5007 |
if (cd.sizeok != Sizeok.done) |
| 5008 |
return setError(); |
| 5009 |
if (exp.placement && placementSize < objectSize) |
| 5010 |
{ |
| 5011 |
error(exp.placement.loc, "new placement size %llu must be >= class object size %llu", placementSize, objectSize); |
| 5012 |
return setError(); |
| 5013 |
} |
| 5014 |
if (!cd.ctor) |
| 5015 |
cd.ctor = cd.searchCtor(); |
| 5016 |
if (cd.noDefaultCtor && !nargs && !cd.defaultCtor) |
| 5017 |
{ |
| 5018 |
error(exp.loc, "default construction is disabled for type `%s`", cd.type.toChars()); |
| 5019 |
return setError(); |
| 5020 |
} |
| 5021 |
|
| 5022 |
if (cd.isInterfaceDeclaration()) |
| 5023 |
{ |
| 5024 |
error(exp.loc, "cannot create instance of interface `%s`", cd.toChars()); |
| 5025 |
return setError(); |
| 5026 |
} |
| 5027 |
|
| 5028 |
if (cd.isAbstract()) |
| 5029 |
{ |
| 5030 |
error(exp.loc, "cannot create instance of abstract class `%s`", cd.toChars()); |
| 5031 |
errorSupplemental(cd.loc, "class `%s` is declared here", cd.toChars()); |
| 5032 |
for (size_t i = 0; i < cd.vtbl.length; i++) |
| 5033 |
{ |
| 5034 |
FuncDeclaration fd = cd.vtbl[i].isFuncDeclaration(); |
| 5035 |
if (fd && fd.isAbstract()) |
| 5036 |
{ |
| 5037 |
errorSupplemental(fd.loc, "function `%s` is not implemented", |
| 5038 |
fd.toFullSignature()); |
| 5039 |
} |
| 5040 |
} |
| 5041 |
return setError(); |
| 5042 |
} |
| 5043 |
// checkDeprecated() is already done in newtype.typeSemantic(). |
| 5044 |
|
| 5045 |
if (cd.isNested()) |
| 5046 |
{ |
| 5047 |
/* We need a 'this' pointer for the nested class. |
| 5048 |
* Ensure we have the right one. |
| 5049 |
*/ |
| 5050 |
Dsymbol s = cd.toParentLocal(); |
| 5051 |
|
| 5052 |
//printf("cd isNested, parent = %s '%s'\n", s.kind(), s.toPrettyChars()); |
| 5053 |
if (auto cdn = s.isClassDeclaration()) |
| 5054 |
{ |
| 5055 |
if (!cdthis) |
| 5056 |
{ |
| 5057 |
void noReferenceToOuterClass() |
| 5058 |
{ |
| 5059 |
if (cd.isAnonymous) |
| 5060 |
error(exp.loc, "cannot construct anonymous nested class because no implicit `this` reference to outer class is available"); |
| 5061 |
else |
| 5062 |
error(exp.loc, "cannot construct nested class `%s` because no implicit `this` reference to outer class `%s` is available", |
| 5063 |
cd.toChars(), cdn.toChars()); |
| 5064 |
return setError(); |
| 5065 |
} |
| 5066 |
|
| 5067 |
if (!sc.hasThis) |
| 5068 |
return noReferenceToOuterClass(); |
| 5069 |
|
| 5070 |
// Supply an implicit 'this' and try again |
| 5071 |
exp.thisexp = new ThisExp(exp.loc); |
| 5072 |
for (Dsymbol sp = sc.parent; 1; sp = sp.toParentLocal()) |
| 5073 |
{ |
| 5074 |
if (!sp) |
| 5075 |
return noReferenceToOuterClass(); |
| 5076 |
ClassDeclaration cdp = sp.isClassDeclaration(); |
| 5077 |
if (!cdp) |
| 5078 |
continue; |
| 5079 |
if (cdp == cdn || cdn.isBaseOf(cdp, null)) |
| 5080 |
break; |
| 5081 |
// Add a '.outer' and try again |
| 5082 |
exp.thisexp = new DotIdExp(exp.loc, exp.thisexp, Id.outer); |
| 5083 |
} |
| 5084 |
|
| 5085 |
exp.thisexp = exp.thisexp.expressionSemantic(sc); |
| 5086 |
if (exp.thisexp.op == EXP.error) |
| 5087 |
return setError(); |
| 5088 |
cdthis = exp.thisexp.type.isClassHandle(); |
| 5089 |
} |
| 5090 |
if (cdthis != cdn && !cdn.isBaseOf(cdthis, null)) |
| 5091 |
{ |
| 5092 |
//printf("cdthis = %s\n", cdthis.toChars()); |
| 5093 |
error(exp.loc, "`this` for nested class must be of type `%s`, not `%s`", |
| 5094 |
cdn.toChars(), exp.thisexp.type.toChars()); |
| 5095 |
return setError(); |
| 5096 |
} |
| 5097 |
if (!MODimplicitConv(exp.thisexp.type.mod, exp.newtype.mod)) |
| 5098 |
{ |
| 5099 |
error(exp.loc, "nested type `%s` should have the same or weaker constancy as enclosing type `%s`", |
| 5100 |
exp.newtype.toChars(), exp.thisexp.type.toChars()); |
| 5101 |
return setError(); |
| 5102 |
} |
| 5103 |
} |
| 5104 |
else if (exp.thisexp) |
| 5105 |
{ |
| 5106 |
error(exp.loc, "`.new` is only for allocating nested classes"); |
| 5107 |
return setError(); |
| 5108 |
} |
| 5109 |
else if (auto fdn = s.isFuncDeclaration()) |
| 5110 |
{ |
| 5111 |
// make sure the parent context fdn of cd is reachable from sc |
| 5112 |
if (!ensureStaticLinkTo(sc.parent, fdn)) |
| 5113 |
{ |
| 5114 |
error(exp.loc, "outer function context of `%s` is needed to `new` nested class `%s`", |
| 5115 |
fdn.toPrettyChars(), cd.toPrettyChars()); |
| 5116 |
return setError(); |
| 5117 |
} |
| 5118 |
} |
| 5119 |
else |
| 5120 |
assert(0); |
| 5121 |
} |
| 5122 |
else if (exp.thisexp) |
| 5123 |
{ |
| 5124 |
error(exp.loc, "`.new` is only for allocating nested classes"); |
| 5125 |
return setError(); |
| 5126 |
} |
| 5127 |
|
| 5128 |
if (cd.vthis2) |
| 5129 |
{ |
| 5130 |
if (AggregateDeclaration ad2 = cd.isMember2()) |
| 5131 |
{ |
| 5132 |
Expression te = new ThisExp(exp.loc).expressionSemantic(sc); |
| 5133 |
if (te.op != EXP.error) |
| 5134 |
te = getRightThis(exp.loc, sc, ad2, te, cd); |
| 5135 |
if (te.op == EXP.error) |
| 5136 |
{ |
| 5137 |
error(exp.loc, "need `this` of type `%s` needed to `new` nested class `%s`", ad2.toChars(), cd.toChars()); |
| 5138 |
return setError(); |
| 5139 |
} |
| 5140 |
} |
| 5141 |
} |
| 5142 |
|
| 5143 |
if (cd.disableNew && !exp.onstack) |
| 5144 |
{ |
| 5145 |
error(exp.loc, "cannot allocate `class %s` with `new` because it is annotated with `@disable new()`", |
| 5146 |
originalNewtype.toChars()); |
| 5147 |
return setError(); |
| 5148 |
} |
| 5149 |
|
| 5150 |
if (cd.ctor) |
| 5151 |
{ |
| 5152 |
FuncDeclaration f = resolveFuncCall(exp.loc, sc, cd.ctor, null, tb, exp.argumentList, FuncResolveFlag.standard); |
| 5153 |
if (!f || f.errors) |
| 5154 |
return setError(); |
| 5155 |
|
| 5156 |
checkFunctionAttributes(exp, sc, f); |
| 5157 |
if (!checkSymbolAccess(sc, f)) |
| 5158 |
{ |
| 5159 |
error(exp.loc, "%s `%s` is not accessible from module `%s`", |
| 5160 |
f.kind(), f.toPrettyChars(), sc._module.toChars); |
| 5161 |
return setError(); |
| 5162 |
} |
| 5163 |
|
| 5164 |
TypeFunction tf = f.type.isTypeFunction(); |
| 5165 |
if (!exp.arguments) |
| 5166 |
exp.arguments = new Expressions(); |
| 5167 |
if (functionParameters(exp.loc, sc, tf, null, exp.type, exp.argumentList, f, &exp.type, &exp.argprefix)) |
| 5168 |
return setError(); |
| 5169 |
|
| 5170 |
exp.member = f.isCtorDeclaration(); |
| 5171 |
assert(exp.member); |
| 5172 |
} |
| 5173 |
else |
| 5174 |
{ |
| 5175 |
if (nargs) |
| 5176 |
{ |
| 5177 |
error(exp.loc, "no constructor for `%s`", cd.toChars()); |
| 5178 |
return setError(); |
| 5179 |
} |
| 5180 |
|
| 5181 |
// https://issues.dlang.org/show_bug.cgi?id=19941 |
| 5182 |
// Run semantic on all field initializers to resolve any forward |
| 5183 |
// references. This is the same as done for structs in sd.fill(). |
| 5184 |
for (ClassDeclaration c = cd; c; c = c.baseClass) |
| 5185 |
{ |
| 5186 |
foreach (v; c.fields) |
| 5187 |
{ |
| 5188 |
if (v.inuse || v._scope is null || v._init is null || |
| 5189 |
v._init.isVoidInitializer() || v.semanticRun >= PASS.semantic2done) |
| 5190 |
continue; |
| 5191 |
v.inuse++; |
| 5192 |
v._init = v._init.initializerSemantic(v._scope, v.type, INITinterpret); |
| 5193 |
import dmd.semantic2 : lowerStaticAAs; |
| 5194 |
lowerStaticAAs(v, sc); |
| 5195 |
v.inuse--; |
| 5196 |
} |
| 5197 |
} |
| 5198 |
} |
| 5199 |
|
| 5200 |
// When using `@nogc` exception handling, lower `throw new E(args)` to |
| 5201 |
// `throw (__tmp = _d_newThrowable!E(), __tmp.__ctor(args), __tmp)`. |
| 5202 |
if (sc.previews.dip1008 && exp.thrownew && |
| 5203 |
!cd.isCOMclass() && !cd.isCPPclass()) |
| 5204 |
{ |
| 5205 |
assert(cd.ctor); |
| 5206 |
|
| 5207 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 5208 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 5209 |
|
| 5210 |
auto tiargs = new Objects(); |
| 5211 |
tiargs.push(exp.newtype); |
| 5212 |
|
| 5213 |
id = new DotTemplateInstanceExp(exp.loc, id, Id._d_newThrowable, tiargs); |
| 5214 |
|
| 5215 |
id = new CallExp(exp.loc, id).expressionSemantic(sc); |
| 5216 |
|
| 5217 |
exp.lowering = id.expressionSemantic(sc); |
| 5218 |
|
| 5219 |
result = exp; |
| 5220 |
return; |
| 5221 |
} |
| 5222 |
else if (sc.needsCodegen() && // interpreter doesn't need this lowered |
| 5223 |
!exp.placement && |
| 5224 |
!exp.onstack && !exp.type.isScopeClass()) // these won't use the GC |
| 5225 |
{ |
| 5226 |
/* replace `new T(arguments)` with `core.lifetime._d_newclassT!T(arguments)` |
| 5227 |
* or `_d_newclassTTrace` |
| 5228 |
*/ |
| 5229 |
auto hook = global.params.tracegc ? Id._d_newclassTTrace : Id._d_newclassT; |
| 5230 |
if (!verifyHookExist(exp.loc, *sc, hook, "new class")) |
| 5231 |
return setError(); |
| 5232 |
|
| 5233 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 5234 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 5235 |
|
| 5236 |
auto tiargs = new Objects(); |
| 5237 |
auto t = exp.newtype.unqualify(MODFlags.wild); // remove `inout` |
| 5238 |
tiargs.push(t); |
| 5239 |
id = new DotTemplateInstanceExp(exp.loc, id, hook, tiargs); |
| 5240 |
auto arguments = new Expressions(); |
| 5241 |
id = new CallExp(exp.loc, id, arguments); |
| 5242 |
|
| 5243 |
exp.lowering = id.expressionSemantic(sc); |
| 5244 |
} |
| 5245 |
} |
| 5246 |
else if (auto ts = tb.isTypeStruct()) |
| 5247 |
{ |
| 5248 |
auto sd = ts.sym; |
| 5249 |
sd.size(exp.loc); |
| 5250 |
if (sd.sizeok != Sizeok.done) |
| 5251 |
return setError(); |
| 5252 |
if (!sd.ctor) |
| 5253 |
sd.ctor = sd.searchCtor(); |
| 5254 |
if (sd.noDefaultCtor && !nargs) |
| 5255 |
{ |
| 5256 |
error(exp.loc, "default construction is disabled for type `%s`", sd.type.toChars()); |
| 5257 |
return setError(); |
| 5258 |
} |
| 5259 |
// checkDeprecated() is already done in newtype.typeSemantic(). |
| 5260 |
|
| 5261 |
if (sd.disableNew) |
| 5262 |
{ |
| 5263 |
error(exp.loc, "cannot allocate `struct %s` with `new` because it is annotated with `@disable new()`", |
| 5264 |
originalNewtype.toChars()); |
| 5265 |
return setError(); |
| 5266 |
} |
| 5267 |
|
| 5268 |
// https://issues.dlang.org/show_bug.cgi?id=22639 |
| 5269 |
// If the new expression has arguments, we either should call a |
| 5270 |
// regular constructor of a copy constructor if the first argument |
| 5271 |
// is the same type as the struct |
| 5272 |
if (nargs && (sd.hasRegularCtor() || (sd.ctor && (*exp.arguments)[0].type.mutableOf() == sd.type.mutableOf()))) |
| 5273 |
{ |
| 5274 |
FuncDeclaration f = resolveFuncCall(exp.loc, sc, sd.ctor, null, tb, exp.argumentList, FuncResolveFlag.standard); |
| 5275 |
if (!f || f.errors) |
| 5276 |
return setError(); |
| 5277 |
|
| 5278 |
checkFunctionAttributes(exp, sc, f); |
| 5279 |
checkAccess(sd, exp.loc, sc, f); |
| 5280 |
|
| 5281 |
TypeFunction tf = f.type.isTypeFunction(); |
| 5282 |
if (!exp.arguments) |
| 5283 |
exp.arguments = new Expressions(); |
| 5284 |
if (functionParameters(exp.loc, sc, tf, null, exp.type, exp.argumentList, f, &exp.type, &exp.argprefix)) |
| 5285 |
return setError(); |
| 5286 |
|
| 5287 |
exp.member = f.isCtorDeclaration(); |
| 5288 |
assert(exp.member); |
| 5289 |
|
| 5290 |
if (checkFrameAccess(exp.loc, sc, sd, sd.fields.length)) |
| 5291 |
return setError(); |
| 5292 |
} |
| 5293 |
else |
| 5294 |
{ |
| 5295 |
if (exp.names) |
| 5296 |
{ |
| 5297 |
exp.arguments = resolveStructLiteralNamedArgs(sd, exp.type, sc, exp.loc, |
| 5298 |
exp.names ? (*exp.names)[] : null, |
| 5299 |
(size_t i, Type t) => (*exp.arguments)[i], |
| 5300 |
i => (*exp.arguments)[i].loc |
| 5301 |
); |
| 5302 |
if (!exp.arguments) |
| 5303 |
return setError(); |
| 5304 |
} |
| 5305 |
else if (!exp.arguments) |
| 5306 |
{ |
| 5307 |
exp.arguments = new Expressions(); |
| 5308 |
} |
| 5309 |
|
| 5310 |
if (!sd.fit(exp.loc, sc, exp.arguments, tb)) |
| 5311 |
return setError(); |
| 5312 |
|
| 5313 |
if (!sd.fill(exp.loc, *exp.arguments, false)) |
| 5314 |
return setError(); |
| 5315 |
|
| 5316 |
if (checkFrameAccess(exp.loc, sc, sd, exp.arguments ? exp.arguments.length : 0)) |
| 5317 |
return setError(); |
| 5318 |
|
| 5319 |
/* Since a `new` allocation may escape, check each of the arguments for escaping |
| 5320 |
*/ |
| 5321 |
foreach (arg; *exp.arguments) |
| 5322 |
{ |
| 5323 |
if (arg && checkNewEscape(*sc, arg, false)) |
| 5324 |
return setError(); |
| 5325 |
} |
| 5326 |
} |
| 5327 |
|
| 5328 |
exp.type = exp.type.pointerTo(); |
| 5329 |
if (!exp.placement) |
| 5330 |
tryLowerToNewItem(exp); |
| 5331 |
} |
| 5332 |
else if (tb.ty == Tarray) |
| 5333 |
{ |
| 5334 |
if (exp.placement) |
| 5335 |
{ |
| 5336 |
error(exp.placement.loc, "placement new cannot be used with dynamic arrays"); |
| 5337 |
return setError(); |
| 5338 |
} |
| 5339 |
if (!nargs) |
| 5340 |
{ |
| 5341 |
// https://issues.dlang.org/show_bug.cgi?id=20422 |
| 5342 |
// Without this check the compiler would give a misleading error |
| 5343 |
error(exp.loc, "missing length argument for array"); |
| 5344 |
return setError(); |
| 5345 |
} |
| 5346 |
|
| 5347 |
Type tn = tb.nextOf().baseElemOf(); |
| 5348 |
Dsymbol s = tn.toDsymbol(sc); |
| 5349 |
AggregateDeclaration ad = s ? s.isAggregateDeclaration() : null; |
| 5350 |
if (ad && ad.noDefaultCtor) |
| 5351 |
{ |
| 5352 |
error(exp.loc, "default construction is disabled for type `%s`", tb.nextOf().toChars()); |
| 5353 |
return setError(); |
| 5354 |
} |
| 5355 |
for (size_t i = 0; i < nargs; i++) |
| 5356 |
{ |
| 5357 |
if (tb.ty != Tarray) |
| 5358 |
{ |
| 5359 |
error(exp.loc, "too many arguments for array"); |
| 5360 |
return setError(); |
| 5361 |
} |
| 5362 |
|
| 5363 |
Expression arg = (*exp.arguments)[i]; |
| 5364 |
if (exp.names && (*exp.names)[i]) |
| 5365 |
{ |
| 5366 |
error(exp.loc, "no named argument `%s` allowed for array dimension", (*exp.names)[i].toChars()); |
| 5367 |
return setError(); |
| 5368 |
} |
| 5369 |
|
| 5370 |
arg = resolveProperties(sc, arg); |
| 5371 |
arg = arg.implicitCastTo(sc, Type.tsize_t); |
| 5372 |
if (arg.op == EXP.error) |
| 5373 |
return setError(); |
| 5374 |
arg = arg.optimize(WANTvalue); |
| 5375 |
if (arg.op == EXP.int64 && (target.isLP64 ? |
| 5376 |
cast(sinteger_t)arg.toInteger() : cast(int)arg.toInteger()) < 0) |
| 5377 |
{ |
| 5378 |
error(exp.loc, "negative array dimension `%s`", (*exp.arguments)[i].toChars()); |
| 5379 |
return setError(); |
| 5380 |
} |
| 5381 |
(*exp.arguments)[i] = arg; |
| 5382 |
tb = tb.isTypeDArray().next.toBasetype(); |
| 5383 |
} |
| 5384 |
|
| 5385 |
if (!global.params.useGC && sc.needsCodegen()) |
| 5386 |
{ |
| 5387 |
version(IN_GCC) |
| 5388 |
error(exp.loc, "expression `%s` allocates with the GC and cannot be used with switch `-fno-rtti`", exp.toErrMsg()); |
| 5389 |
else |
| 5390 |
error(exp.loc, "expression `%s` allocates with the GC and cannot be used with switch `-betterC`", exp.toErrMsg()); |
| 5391 |
return setError(); |
| 5392 |
} |
| 5393 |
|
| 5394 |
if (!sc.needsCodegen()) |
| 5395 |
goto LskipNewArrayLowering; |
| 5396 |
|
| 5397 |
/* Class types may inherit base classes that have errors. |
| 5398 |
* This may leak errors from the base class to the derived one |
| 5399 |
* and then to the hook. Semantic analysis is performed eagerly |
| 5400 |
* to a void this. |
| 5401 |
*/ |
| 5402 |
if (auto tc = exp.type.nextOf.isTypeClass()) |
| 5403 |
{ |
| 5404 |
tc.sym.dsymbolSemantic(sc); |
| 5405 |
if (tc.sym.errors) |
| 5406 |
goto LskipNewArrayLowering; |
| 5407 |
} |
| 5408 |
|
| 5409 |
if (exp.placement) // no need to lower |
| 5410 |
{ |
| 5411 |
} |
| 5412 |
else if (nargs == 1) |
| 5413 |
{ |
| 5414 |
auto hook = global.params.tracegc ? Id._d_newarrayTTrace : Id._d_newarrayT; |
| 5415 |
if (!verifyHookExist(exp.loc, *sc, hook, "new array")) |
| 5416 |
goto LskipNewArrayLowering; |
| 5417 |
|
| 5418 |
/* Lower the memory allocation and initialization of `new T[n]` |
| 5419 |
* to `_d_newarrayT!T(n)`. |
| 5420 |
*/ |
| 5421 |
Expression lowering = new IdentifierExp(exp.loc, Id.empty); |
| 5422 |
lowering = new DotIdExp(exp.loc, lowering, Id.object); |
| 5423 |
auto tiargs = new Objects(); |
| 5424 |
/* Remove `inout`, `const`, `immutable` and `shared` to reduce |
| 5425 |
* the number of generated `_d_newarrayT` instances. |
| 5426 |
*/ |
| 5427 |
const isShared = exp.type.nextOf.isShared(); |
| 5428 |
auto t = exp.type.nextOf.unqualify(MODFlags.wild | MODFlags.const_ | |
| 5429 |
MODFlags.immutable_ | MODFlags.shared_); |
| 5430 |
tiargs.push(t); |
| 5431 |
lowering = new DotTemplateInstanceExp(exp.loc, lowering, hook, tiargs); |
| 5432 |
|
| 5433 |
auto arguments = new Expressions(); |
| 5434 |
arguments.push((*exp.arguments)[0]); |
| 5435 |
arguments.push(new IntegerExp(exp.loc, isShared, Type.tbool)); |
| 5436 |
|
| 5437 |
lowering = new CallExp(exp.loc, lowering, arguments); |
| 5438 |
exp.lowering = lowering.expressionSemantic(sc); |
| 5439 |
} |
| 5440 |
else |
| 5441 |
{ |
| 5442 |
auto hook = global.params.tracegc ? Id._d_newarraymTXTrace : Id._d_newarraymTX; |
| 5443 |
if (!verifyHookExist(exp.loc, *sc, hook, "new multi-dimensional array")) |
| 5444 |
goto LskipNewArrayLowering; |
| 5445 |
|
| 5446 |
/* Lower the memory allocation and initialization of `new T[][]...[](n1, n2, ...)` |
| 5447 |
* to `_d_newarraymTX!(T[][]...[], T)([n1, n2, ...])`. |
| 5448 |
*/ |
| 5449 |
Expression lowering = new IdentifierExp(exp.loc, Id.empty); |
| 5450 |
lowering = new DotIdExp(exp.loc, lowering, Id.object); |
| 5451 |
|
| 5452 |
auto tbn = exp.type.nextOf(); |
| 5453 |
size_t i = nargs; |
| 5454 |
while (tbn.ty == Tarray && --i) |
| 5455 |
tbn = tbn.nextOf(); |
| 5456 |
auto unqualTbn = tbn.unqualify(MODFlags.wild | MODFlags.const_ | |
| 5457 |
MODFlags.immutable_ | MODFlags.shared_); |
| 5458 |
|
| 5459 |
auto tiargs = new Objects(); |
| 5460 |
tiargs.push(exp.type); |
| 5461 |
tiargs.push(unqualTbn); |
| 5462 |
lowering = new DotTemplateInstanceExp(exp.loc, lowering, hook, tiargs); |
| 5463 |
|
| 5464 |
auto arguments = new Expressions(); |
| 5465 |
|
| 5466 |
arguments.push(new ArrayLiteralExp(exp.loc, Type.tsize_t.sarrayOf(nargs), exp.arguments)); |
| 5467 |
arguments.push(new IntegerExp(exp.loc, tbn.isShared(), Type.tbool)); |
| 5468 |
|
| 5469 |
lowering = new CallExp(exp.loc, lowering, arguments); |
| 5470 |
exp.lowering = lowering.expressionSemantic(sc); |
| 5471 |
} |
| 5472 |
} |
| 5473 |
else if (tb.isScalar()) |
| 5474 |
{ |
| 5475 |
if (!nargs) |
| 5476 |
{ |
| 5477 |
} |
| 5478 |
else if (nargs == 1) |
| 5479 |
{ |
| 5480 |
if (exp.names && (*exp.names)[0]) |
| 5481 |
{ |
| 5482 |
error(exp.loc, "no named argument `%s` allowed for scalar", (*exp.names)[0].toChars()); |
| 5483 |
return setError(); |
| 5484 |
} |
| 5485 |
Expression e = (*exp.arguments)[0]; |
| 5486 |
e = e.implicitCastTo(sc, tb); |
| 5487 |
(*exp.arguments)[0] = e; |
| 5488 |
} |
| 5489 |
else |
| 5490 |
{ |
| 5491 |
error(exp.loc, "more than one argument for construction of `%s`", exp.type.toChars()); |
| 5492 |
return setError(); |
| 5493 |
} |
| 5494 |
|
| 5495 |
exp.type = exp.type.pointerTo(); |
| 5496 |
if (!exp.placement) |
| 5497 |
tryLowerToNewItem(exp); |
| 5498 |
} |
| 5499 |
else if (tb.ty == Taarray) |
| 5500 |
{ |
| 5501 |
if (exp.placement) |
| 5502 |
{ |
| 5503 |
error(exp.placement.loc, "placement new cannot be used with associative arrays"); |
| 5504 |
return setError(); |
| 5505 |
} |
| 5506 |
// e.g. `new Alias(args)` |
| 5507 |
if (nargs) |
| 5508 |
{ |
| 5509 |
error(exp.loc, "`new` cannot take arguments for an associative array"); |
| 5510 |
return setError(); |
| 5511 |
} |
| 5512 |
} |
| 5513 |
else |
| 5514 |
{ |
| 5515 |
error(exp.loc, "cannot create a `%s` with `new`", exp.type.toChars()); |
| 5516 |
return setError(); |
| 5517 |
} |
| 5518 |
|
| 5519 |
LskipNewArrayLowering: |
| 5520 |
//printf("NewExp: '%s'\n", toChars()); |
| 5521 |
//printf("NewExp:type '%s'\n", type.toChars()); |
| 5522 |
semanticTypeInfo(sc, exp.type); |
| 5523 |
|
| 5524 |
if (newprefix) |
| 5525 |
{ |
| 5526 |
result = Expression.combine(newprefix, exp); |
| 5527 |
return; |
| 5528 |
} |
| 5529 |
result = exp; |
| 5530 |
} |
| 5531 |
|
| 5532 |
override void visit(NewAnonClassExp e) |
| 5533 |
{ |
| 5534 |
static if (LOGSEMANTIC) |
| 5535 |
{ |
| 5536 |
printf("NewAnonClassExp::semantic() %s\n", e.toChars()); |
| 5537 |
//printf("thisexp = %p\n", thisexp); |
| 5538 |
//printf("type: %s\n", type.toChars()); |
| 5539 |
} |
| 5540 |
|
| 5541 |
Expression d = new DeclarationExp(e.loc, e.cd); |
| 5542 |
sc = sc.push(); // just create new scope |
| 5543 |
sc.ctfe = false; // temporary stop CTFE |
| 5544 |
d = d.expressionSemantic(sc); |
| 5545 |
sc = sc.pop(); |
| 5546 |
|
| 5547 |
if (!e.cd.errors && sc.intypeof && !sc.parent.inNonRoot()) |
| 5548 |
{ |
| 5549 |
ScopeDsymbol sds = sc.tinst ? cast(ScopeDsymbol)sc.tinst : sc._module; |
| 5550 |
if (!sds.members) |
| 5551 |
sds.members = new Dsymbols(); |
| 5552 |
sds.members.push(e.cd); |
| 5553 |
} |
| 5554 |
|
| 5555 |
Expression n = new NewExp(e.loc, e.placement, e.thisexp, e.cd.type, e.arguments); |
| 5556 |
|
| 5557 |
Expression c = new CommaExp(e.loc, d, n); |
| 5558 |
result = c.expressionSemantic(sc); |
| 5559 |
} |
| 5560 |
|
| 5561 |
override void visit(SymOffExp e) |
| 5562 |
{ |
| 5563 |
static if (LOGSEMANTIC) |
| 5564 |
{ |
| 5565 |
printf("SymOffExp::semantic('%s')\n", e.toChars()); |
| 5566 |
} |
| 5567 |
//var.dsymbolSemantic(sc); |
| 5568 |
if (!e.type) |
| 5569 |
e.type = e.var.type.pointerTo(); |
| 5570 |
|
| 5571 |
if (auto v = e.var.isVarDeclaration()) |
| 5572 |
{ |
| 5573 |
if (v.checkNestedReference(sc, e.loc)) |
| 5574 |
return setError(); |
| 5575 |
} |
| 5576 |
else if (auto f = e.var.isFuncDeclaration()) |
| 5577 |
{ |
| 5578 |
if (f.checkNestedFuncReference(sc, e.loc)) |
| 5579 |
return setError(); |
| 5580 |
} |
| 5581 |
|
| 5582 |
result = e; |
| 5583 |
} |
| 5584 |
|
| 5585 |
override void visit(VarExp e) |
| 5586 |
{ |
| 5587 |
static if (LOGSEMANTIC) |
| 5588 |
{ |
| 5589 |
printf("VarExp::semantic(%s)\n", e.toChars()); |
| 5590 |
} |
| 5591 |
|
| 5592 |
auto vd = e.var.isVarDeclaration(); |
| 5593 |
auto fd = e.var.isFuncDeclaration(); |
| 5594 |
|
| 5595 |
if (fd) |
| 5596 |
{ |
| 5597 |
//printf("L%d fd = %s\n", __LINE__, f.toChars()); |
| 5598 |
if (!functionSemantic(fd)) |
| 5599 |
return setError(); |
| 5600 |
} |
| 5601 |
|
| 5602 |
if (!e.type) |
| 5603 |
e.type = e.var.type; |
| 5604 |
if (e.type && !e.type.deco) |
| 5605 |
{ |
| 5606 |
auto decl = e.var.isDeclaration(); |
| 5607 |
if (decl) |
| 5608 |
decl.inuse++; |
| 5609 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 5610 |
if (decl) |
| 5611 |
decl.inuse--; |
| 5612 |
} |
| 5613 |
|
| 5614 |
/* Fix for 1161 doesn't work because it causes visibility |
| 5615 |
* problems when instantiating imported templates passing private |
| 5616 |
* variables as alias template parameters. |
| 5617 |
*/ |
| 5618 |
//checkAccess(loc, sc, NULL, var); |
| 5619 |
|
| 5620 |
if (vd) |
| 5621 |
{ |
| 5622 |
if (vd.checkNestedReference(sc, e.loc)) |
| 5623 |
return setError(); |
| 5624 |
|
| 5625 |
// https://issues.dlang.org/show_bug.cgi?id=12025 |
| 5626 |
// If the variable is not actually used in runtime code, |
| 5627 |
// the purity violation error is redundant. |
| 5628 |
//checkPurity(sc, vd); |
| 5629 |
} |
| 5630 |
else if (fd) |
| 5631 |
{ |
| 5632 |
// TODO: If fd isn't yet resolved its overload, the checkNestedFuncReference |
| 5633 |
// call would cause incorrect validation. |
| 5634 |
// Maybe here should be moved in CallExp, or AddrExp for functions. |
| 5635 |
if (fd.checkNestedFuncReference(sc, e.loc)) |
| 5636 |
return setError(); |
| 5637 |
} |
| 5638 |
else if (auto od = e.var.isOverDeclaration()) |
| 5639 |
{ |
| 5640 |
e.type = Type.tvoid; // ambiguous type? |
| 5641 |
} |
| 5642 |
|
| 5643 |
result = e; |
| 5644 |
} |
| 5645 |
|
| 5646 |
private void genIdent(FuncExp exp, Scope* sc) |
| 5647 |
{ |
| 5648 |
if (exp.fd.ident != Id.empty) |
| 5649 |
return; |
| 5650 |
|
| 5651 |
string s; |
| 5652 |
if (exp.fd.fes) |
| 5653 |
s = "__foreachbody"; |
| 5654 |
else if (exp.fd.tok == TOK.reserved) |
| 5655 |
s = "__lambda"; |
| 5656 |
else if (exp.fd.tok == TOK.delegate_) |
| 5657 |
s = "__dgliteral"; |
| 5658 |
else |
| 5659 |
s = "__funcliteral"; |
| 5660 |
|
| 5661 |
DsymbolTable symtab; |
| 5662 |
if (FuncDeclaration func = sc.parent.isFuncDeclaration()) |
| 5663 |
{ |
| 5664 |
if (func.localsymtab is null) |
| 5665 |
{ |
| 5666 |
// Inside template constraint, symtab is not set yet. |
| 5667 |
// Initialize it lazily. |
| 5668 |
func.localsymtab = new DsymbolTable(); |
| 5669 |
} |
| 5670 |
symtab = func.localsymtab; |
| 5671 |
} |
| 5672 |
else |
| 5673 |
{ |
| 5674 |
ScopeDsymbol sds = sc.parent.isScopeDsymbol(); |
| 5675 |
if (!sds.symtab) |
| 5676 |
{ |
| 5677 |
// Inside template constraint, symtab may not be set yet. |
| 5678 |
// Initialize it lazily. |
| 5679 |
assert(sds.isTemplateInstance()); |
| 5680 |
sds.symtab = new DsymbolTable(); |
| 5681 |
} |
| 5682 |
symtab = sds.symtab; |
| 5683 |
} |
| 5684 |
assert(symtab); |
| 5685 |
Identifier id = Identifier.generateIdWithLoc(s, exp.loc, cast(string) toDString(sc.parent.toPrettyChars())); |
| 5686 |
exp.fd.ident = id; |
| 5687 |
if (exp.td) |
| 5688 |
exp.td.ident = id; |
| 5689 |
symtab.insert(exp.td ? cast(Dsymbol)exp.td : cast(Dsymbol)exp.fd); |
| 5690 |
} |
| 5691 |
|
| 5692 |
override void visit(FuncExp exp) |
| 5693 |
{ |
| 5694 |
static if (LOGSEMANTIC) |
| 5695 |
{ |
| 5696 |
printf("FuncExp::semantic(%s)\n", exp.toChars()); |
| 5697 |
if (exp.fd.treq) |
| 5698 |
printf(" treq = %s\n", exp.fd.treq.toChars()); |
| 5699 |
} |
| 5700 |
|
| 5701 |
|
| 5702 |
Expression e = exp; |
| 5703 |
|
| 5704 |
sc = sc.push(); // just create new scope |
| 5705 |
sc.ctfe = false; // temporary stop CTFE |
| 5706 |
sc.visibility = Visibility(Visibility.Kind.public_); // https://issues.dlang.org/show_bug.cgi?id=12506 |
| 5707 |
|
| 5708 |
/* fd.treq might be incomplete type, |
| 5709 |
* so should not semantic it. |
| 5710 |
* void foo(T)(T delegate(int) dg){} |
| 5711 |
* foo(a=>a); // in IFTI, treq == T delegate(int) |
| 5712 |
*/ |
| 5713 |
//if (fd.treq) |
| 5714 |
// fd.treq = fd.treq.dsymbolSemantic(loc, sc); |
| 5715 |
|
| 5716 |
genIdent(exp, sc); |
| 5717 |
|
| 5718 |
// Set target of return type inference |
| 5719 |
if (exp.fd.treq && !exp.fd.type.nextOf()) |
| 5720 |
{ |
| 5721 |
TypeFunction tfv = null; |
| 5722 |
if (exp.fd.treq.ty == Tdelegate || exp.fd.treq.isPtrToFunction()) |
| 5723 |
tfv = cast(TypeFunction)exp.fd.treq.nextOf(); |
| 5724 |
if (tfv) |
| 5725 |
{ |
| 5726 |
TypeFunction tfl = cast(TypeFunction)exp.fd.type; |
| 5727 |
tfl.next = tfv.nextOf(); |
| 5728 |
} |
| 5729 |
} |
| 5730 |
|
| 5731 |
void done() |
| 5732 |
{ |
| 5733 |
sc = sc.pop(); |
| 5734 |
result = e; |
| 5735 |
} |
| 5736 |
|
| 5737 |
//printf("td = %p, treq = %p\n", td, fd.treq); |
| 5738 |
if (exp.td) |
| 5739 |
{ |
| 5740 |
assert(exp.td.parameters && exp.td.parameters.length); |
| 5741 |
exp.td.dsymbolSemantic(sc); |
| 5742 |
exp.type = Type.tvoid; // temporary type |
| 5743 |
|
| 5744 |
if (exp.fd.treq) // defer type determination |
| 5745 |
{ |
| 5746 |
FuncExp fe; |
| 5747 |
if (exp.matchType(exp.fd.treq, sc, &fe, sc.eSink) > MATCH.nomatch) |
| 5748 |
e = fe; |
| 5749 |
else |
| 5750 |
e = ErrorExp.get(); |
| 5751 |
} |
| 5752 |
return done(); |
| 5753 |
} |
| 5754 |
|
| 5755 |
const olderrors = global.errors; |
| 5756 |
exp.fd.dsymbolSemantic(sc); |
| 5757 |
if (olderrors == global.errors) |
| 5758 |
{ |
| 5759 |
exp.fd.semantic2(sc); |
| 5760 |
if (olderrors == global.errors) |
| 5761 |
exp.fd.semantic3(sc); |
| 5762 |
} |
| 5763 |
if (olderrors != global.errors) |
| 5764 |
{ |
| 5765 |
if (exp.fd.type && exp.fd.type.ty == Tfunction && !exp.fd.type.nextOf()) |
| 5766 |
(cast(TypeFunction)exp.fd.type).next = Type.terror; |
| 5767 |
e = ErrorExp.get(); |
| 5768 |
return done(); |
| 5769 |
} |
| 5770 |
|
| 5771 |
// Type is a "delegate to" or "pointer to" the function literal |
| 5772 |
if ((exp.fd.isNested() && exp.fd.tok == TOK.delegate_) || (exp.tok == TOK.reserved && exp.fd.treq && exp.fd.treq.ty == Tdelegate)) |
| 5773 |
{ |
| 5774 |
// https://issues.dlang.org/show_bug.cgi?id=22686 |
| 5775 |
// if the delegate return type is an error |
| 5776 |
// abort semantic of the FuncExp and propagate |
| 5777 |
// the error |
| 5778 |
if (exp.fd.type.isTypeError()) |
| 5779 |
{ |
| 5780 |
e = ErrorExp.get(); |
| 5781 |
return done(); |
| 5782 |
} |
| 5783 |
exp.type = new TypeDelegate(exp.fd.type.isTypeFunction()); |
| 5784 |
exp.type = exp.type.typeSemantic(exp.loc, sc); |
| 5785 |
|
| 5786 |
exp.fd.tok = TOK.delegate_; |
| 5787 |
} |
| 5788 |
else |
| 5789 |
{ |
| 5790 |
exp.type = new TypePointer(exp.fd.type); |
| 5791 |
exp.type = exp.type.typeSemantic(exp.loc, sc); |
| 5792 |
//type = fd.type.pointerTo(); |
| 5793 |
|
| 5794 |
/* A lambda expression deduced to function pointer might become |
| 5795 |
* to a delegate literal implicitly. |
| 5796 |
* |
| 5797 |
* auto foo(void function() fp) { return 1; } |
| 5798 |
* assert(foo({}) == 1); |
| 5799 |
* |
| 5800 |
* So, should keep fd.tok == TOK.reserve if fd.treq == NULL. |
| 5801 |
*/ |
| 5802 |
if (exp.fd.treq && exp.fd.treq.ty == Tpointer) |
| 5803 |
{ |
| 5804 |
// change to non-nested |
| 5805 |
exp.fd.tok = TOK.function_; |
| 5806 |
exp.fd.vthis = null; |
| 5807 |
} |
| 5808 |
} |
| 5809 |
exp.fd.tookAddressOf++; |
| 5810 |
done(); |
| 5811 |
} |
| 5812 |
|
| 5813 |
/** |
| 5814 |
* Perform semantic analysis on function literals |
| 5815 |
* |
| 5816 |
* Test the following construct: |
| 5817 |
* --- |
| 5818 |
* (x, y, z) { return x + y + z; }(42, 84, 1992); |
| 5819 |
* --- |
| 5820 |
*/ |
| 5821 |
Expression callExpSemantic(FuncExp exp, Scope* sc, Expressions* arguments) |
| 5822 |
{ |
| 5823 |
if ((exp.type && exp.type != Type.tvoid) || !exp.td ||! arguments || !arguments.length) |
| 5824 |
return exp.expressionSemantic(sc); |
| 5825 |
|
| 5826 |
for (size_t k = 0; k < arguments.length; k++) |
| 5827 |
{ |
| 5828 |
Expression checkarg = (*arguments)[k]; |
| 5829 |
if (checkarg.op == EXP.error) |
| 5830 |
return checkarg; |
| 5831 |
} |
| 5832 |
|
| 5833 |
genIdent(exp, sc); |
| 5834 |
|
| 5835 |
assert(exp.td.parameters && exp.td.parameters.length); |
| 5836 |
exp.td.dsymbolSemantic(sc); |
| 5837 |
|
| 5838 |
TypeFunction tfl = cast(TypeFunction)exp.fd.type; |
| 5839 |
size_t dim = tfl.parameterList.length; |
| 5840 |
if (arguments.length < dim) |
| 5841 |
{ |
| 5842 |
// Default arguments are always typed, so they don't need inference. |
| 5843 |
Parameter p = tfl.parameterList[arguments.length]; |
| 5844 |
if (p.defaultArg) |
| 5845 |
dim = arguments.length; |
| 5846 |
} |
| 5847 |
|
| 5848 |
if ((tfl.parameterList.varargs == VarArg.none && arguments.length > dim) || |
| 5849 |
arguments.length < dim) |
| 5850 |
{ |
| 5851 |
OutBuffer buf; |
| 5852 |
foreach (idx, ref arg; *arguments) |
| 5853 |
buf.printf("%s%s", (idx ? ", ".ptr : "".ptr), arg.type.toChars()); |
| 5854 |
error(exp.loc, "`%s` is not callable using argument types `(%s)`", |
| 5855 |
exp.fd.toErrMsg(), // parametersTypeToChars(tfl.parameterList), |
| 5856 |
buf.peekChars()); |
| 5857 |
errorSupplemental(exp.loc, "too %s arguments, expected %d, got %d", |
| 5858 |
arguments.length < dim ? "few".ptr : "many".ptr, |
| 5859 |
cast(int)dim, cast(int)arguments.length); |
| 5860 |
return ErrorExp.get(); |
| 5861 |
} |
| 5862 |
|
| 5863 |
auto tiargs = new Objects(); |
| 5864 |
tiargs.reserve(exp.td.parameters.length); |
| 5865 |
|
| 5866 |
for (size_t i = 0; i < exp.td.parameters.length; i++) |
| 5867 |
{ |
| 5868 |
TemplateParameter tp = (*exp.td.parameters)[i]; |
| 5869 |
assert(dim <= tfl.parameterList.length); |
| 5870 |
foreach (u, p; tfl.parameterList) |
| 5871 |
{ |
| 5872 |
if (u == dim) |
| 5873 |
break; |
| 5874 |
|
| 5875 |
if (p.type.ty == Tident && (cast(TypeIdentifier)p.type).ident == tp.ident) |
| 5876 |
{ |
| 5877 |
Expression e = (*arguments)[u]; |
| 5878 |
tiargs.push(e.type); |
| 5879 |
break; |
| 5880 |
} |
| 5881 |
} |
| 5882 |
} |
| 5883 |
|
| 5884 |
auto ti = new TemplateInstance(exp.loc, exp.td, tiargs); |
| 5885 |
return (new ScopeExp(exp.loc, ti)).expressionSemantic(sc); |
| 5886 |
} |
| 5887 |
|
| 5888 |
override void visit(CallExp exp) |
| 5889 |
{ |
| 5890 |
static if (LOGSEMANTIC) |
| 5891 |
{ |
| 5892 |
printf("CallExp::semantic() %s\n", exp.toChars()); |
| 5893 |
} |
| 5894 |
|
| 5895 |
scope (exit) |
| 5896 |
{ |
| 5897 |
if (TypeFunction tf = exp.f ? cast(TypeFunction)exp.f.type : null) |
| 5898 |
{ |
| 5899 |
result.rvalue = tf.isRvalue; |
| 5900 |
if (tf.isRvalue && !tf.isRef) |
| 5901 |
{ |
| 5902 |
error(exp.f.loc, "`__rvalue` only valid on functions that return by `ref`"); |
| 5903 |
setError(); |
| 5904 |
} |
| 5905 |
} |
| 5906 |
} |
| 5907 |
|
| 5908 |
Objects* tiargs = null; // initial list of template arguments |
| 5909 |
Expression ethis = null; |
| 5910 |
Type tthis = null; |
| 5911 |
Expression e1org = exp.e1; |
| 5912 |
|
| 5913 |
if (auto ce = exp.e1.isCommaExp()) |
| 5914 |
{ |
| 5915 |
/* Rewrite (a,b)(args) as (a,(b(args))) |
| 5916 |
*/ |
| 5917 |
exp.e1 = ce.e2; |
| 5918 |
ce.e2 = exp; |
| 5919 |
result = ce.expressionSemantic(sc); |
| 5920 |
return; |
| 5921 |
} |
| 5922 |
if (DelegateExp de = exp.e1.isDelegateExp()) |
| 5923 |
{ |
| 5924 |
exp.e1 = new DotVarExp(de.loc, de.e1, de.func, de.hasOverloads); |
| 5925 |
visit(exp); |
| 5926 |
return; |
| 5927 |
} |
| 5928 |
if (FuncExp fe = exp.e1.isFuncExp()) |
| 5929 |
{ |
| 5930 |
if (arrayExpressionSemantic(exp.arguments.peekSlice(), sc) || |
| 5931 |
preFunctionParameters(sc, exp.argumentList, global.errorSink)) |
| 5932 |
return setError(); |
| 5933 |
|
| 5934 |
// Run e1 semantic even if arguments have any errors |
| 5935 |
exp.e1 = callExpSemantic(fe, sc, exp.arguments); |
| 5936 |
if (exp.e1.op == EXP.error) |
| 5937 |
{ |
| 5938 |
result = exp.e1; |
| 5939 |
return; |
| 5940 |
} |
| 5941 |
} |
| 5942 |
if (sc.inCfile) |
| 5943 |
{ |
| 5944 |
/* See if need to rewrite the AST because of cast/call ambiguity |
| 5945 |
*/ |
| 5946 |
if (auto e = castCallAmbiguity(exp, sc)) |
| 5947 |
{ |
| 5948 |
result = expressionSemantic(e, sc); |
| 5949 |
return; |
| 5950 |
} |
| 5951 |
} |
| 5952 |
|
| 5953 |
if (Expression ex = resolveUFCS(sc, exp)) |
| 5954 |
{ |
| 5955 |
result = ex; |
| 5956 |
return; |
| 5957 |
} |
| 5958 |
|
| 5959 |
/* This recognizes: |
| 5960 |
* foo!(tiargs)(funcargs) |
| 5961 |
*/ |
| 5962 |
if (ScopeExp se = exp.e1.isScopeExp()) |
| 5963 |
{ |
| 5964 |
TemplateInstance ti = se.sds.isTemplateInstance(); |
| 5965 |
if (ti) |
| 5966 |
{ |
| 5967 |
/* Attempt to instantiate ti. If that works, go with it. |
| 5968 |
* If not, go with partial explicit specialization. |
| 5969 |
*/ |
| 5970 |
WithScopeSymbol withsym; |
| 5971 |
if (!ti.findTempDecl(sc, &withsym) || !ti.semanticTiargs(sc)) |
| 5972 |
return setError(); |
| 5973 |
if (withsym && withsym.withstate.wthis) |
| 5974 |
{ |
| 5975 |
exp.e1 = new VarExp(exp.e1.loc, withsym.withstate.wthis); |
| 5976 |
exp.e1 = new DotTemplateInstanceExp(exp.e1.loc, exp.e1, ti); |
| 5977 |
goto Ldotti; |
| 5978 |
} |
| 5979 |
if (ti.needsTypeInference(sc, 1)) |
| 5980 |
{ |
| 5981 |
/* Go with partial explicit specialization |
| 5982 |
*/ |
| 5983 |
tiargs = ti.tiargs; |
| 5984 |
assert(ti.tempdecl); |
| 5985 |
if (TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration()) |
| 5986 |
exp.e1 = new TemplateExp(exp.loc, td); |
| 5987 |
else if (OverDeclaration od = ti.tempdecl.isOverDeclaration()) |
| 5988 |
exp.e1 = new VarExp(exp.loc, od); |
| 5989 |
else |
| 5990 |
exp.e1 = new OverExp(exp.loc, ti.tempdecl.isOverloadSet()); |
| 5991 |
} |
| 5992 |
else |
| 5993 |
{ |
| 5994 |
Expression e1x = exp.e1.expressionSemantic(sc); |
| 5995 |
if (e1x.op == EXP.error) |
| 5996 |
{ |
| 5997 |
result = e1x; |
| 5998 |
return; |
| 5999 |
} |
| 6000 |
exp.e1 = e1x; |
| 6001 |
} |
| 6002 |
} |
| 6003 |
} |
| 6004 |
|
| 6005 |
/* This recognizes: |
| 6006 |
* expr.foo!(tiargs)(funcargs) |
| 6007 |
*/ |
| 6008 |
Ldotti: |
| 6009 |
if (DotTemplateInstanceExp se = exp.e1.isDotTemplateInstanceExp()) |
| 6010 |
{ |
| 6011 |
TemplateInstance ti = se.ti; |
| 6012 |
{ |
| 6013 |
/* Attempt to instantiate ti. If that works, go with it. |
| 6014 |
* If not, go with partial explicit specialization. |
| 6015 |
*/ |
| 6016 |
if (!se.findTempDecl(sc) || !ti.semanticTiargs(sc)) |
| 6017 |
return setError(); |
| 6018 |
if (ti.needsTypeInference(sc, 1)) |
| 6019 |
{ |
| 6020 |
/* Go with partial explicit specialization |
| 6021 |
*/ |
| 6022 |
tiargs = ti.tiargs; |
| 6023 |
assert(ti.tempdecl); |
| 6024 |
if (TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration()) |
| 6025 |
exp.e1 = new DotTemplateExp(exp.loc, se.e1, td); |
| 6026 |
else if (OverDeclaration od = ti.tempdecl.isOverDeclaration()) |
| 6027 |
{ |
| 6028 |
exp.e1 = new DotVarExp(exp.loc, se.e1, od, true); |
| 6029 |
} |
| 6030 |
else |
| 6031 |
exp.e1 = new DotExp(exp.loc, se.e1, new OverExp(exp.loc, ti.tempdecl.isOverloadSet())); |
| 6032 |
} |
| 6033 |
else |
| 6034 |
{ |
| 6035 |
Expression e1x = exp.e1.expressionSemantic(sc); |
| 6036 |
if (e1x.op == EXP.error) |
| 6037 |
{ |
| 6038 |
result = e1x; |
| 6039 |
return; |
| 6040 |
} |
| 6041 |
exp.e1 = e1x; |
| 6042 |
} |
| 6043 |
} |
| 6044 |
} |
| 6045 |
|
| 6046 |
Type att = null; |
| 6047 |
Lagain: |
| 6048 |
//printf("Lagain: %s\n", toChars()); |
| 6049 |
exp.f = null; |
| 6050 |
if (exp.e1.op == EXP.this_ || exp.e1.op == EXP.super_) |
| 6051 |
{ |
| 6052 |
// semantic() run later for these |
| 6053 |
} |
| 6054 |
else |
| 6055 |
{ |
| 6056 |
if (DotIdExp die = exp.e1.isDotIdExp()) |
| 6057 |
{ |
| 6058 |
exp.e1 = die.expressionSemantic(sc); |
| 6059 |
/* Look for e1 having been rewritten to expr.opDispatch!(string) |
| 6060 |
* We handle such earlier, so go back. |
| 6061 |
* Note that in the rewrite, we carefully did not run semantic() on e1 |
| 6062 |
*/ |
| 6063 |
if (exp.e1.op == EXP.dotTemplateInstance) |
| 6064 |
{ |
| 6065 |
goto Ldotti; |
| 6066 |
} |
| 6067 |
} |
| 6068 |
else |
| 6069 |
{ |
| 6070 |
__gshared int nest; |
| 6071 |
if (++nest > global.recursionLimit) |
| 6072 |
{ |
| 6073 |
error(exp.loc, "recursive evaluation of `%s`", exp.toErrMsg()); |
| 6074 |
--nest; |
| 6075 |
return setError(); |
| 6076 |
} |
| 6077 |
Expression ex = unaSemantic(exp, sc); |
| 6078 |
--nest; |
| 6079 |
if (ex) |
| 6080 |
{ |
| 6081 |
result = ex; |
| 6082 |
return; |
| 6083 |
} |
| 6084 |
} |
| 6085 |
|
| 6086 |
/* Look for e1 being a lazy parameter |
| 6087 |
*/ |
| 6088 |
if (VarExp ve = exp.e1.isVarExp()) |
| 6089 |
{ |
| 6090 |
if (ve.var.storage_class & STC.lazy_) |
| 6091 |
{ |
| 6092 |
// lazy parameters can be called without violating purity and safety |
| 6093 |
Type tw = ve.var.type; |
| 6094 |
Type tc = ve.var.type.substWildTo(MODFlags.const_); |
| 6095 |
auto tf = new TypeFunction(ParameterList(), tc, LINK.d, STC.safe | STC.pure_); |
| 6096 |
(tf = cast(TypeFunction)tf.typeSemantic(exp.loc, sc)).next = tw; // hack for bug7757 |
| 6097 |
auto t = new TypeDelegate(tf); |
| 6098 |
ve.type = t.typeSemantic(exp.loc, sc); |
| 6099 |
} |
| 6100 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 6101 |
if (v && v.checkPurity(ve.loc, sc)) |
| 6102 |
return setError(); |
| 6103 |
} |
| 6104 |
|
| 6105 |
if (exp.e1.op == EXP.symbolOffset && (cast(SymOffExp)exp.e1).hasOverloads) |
| 6106 |
{ |
| 6107 |
SymOffExp se = cast(SymOffExp)exp.e1; |
| 6108 |
exp.e1 = new VarExp(se.loc, se.var, true); |
| 6109 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 6110 |
} |
| 6111 |
else if (DotExp de = exp.e1.isDotExp()) |
| 6112 |
{ |
| 6113 |
if (de.e2.op == EXP.overloadSet) |
| 6114 |
{ |
| 6115 |
ethis = de.e1; |
| 6116 |
tthis = de.e1.type; |
| 6117 |
exp.e1 = de.e2; |
| 6118 |
} |
| 6119 |
} |
| 6120 |
else if (exp.e1.op == EXP.star && exp.e1.type.ty == Tfunction) |
| 6121 |
{ |
| 6122 |
// Rewrite (*fp)(arguments) to fp(arguments) |
| 6123 |
exp.e1 = (cast(PtrExp)exp.e1).e1; |
| 6124 |
} |
| 6125 |
else if (exp.e1.op == EXP.type && (sc && sc.inCfile)) |
| 6126 |
{ |
| 6127 |
const numArgs = exp.arguments ? exp.arguments.length : 0; |
| 6128 |
|
| 6129 |
/* Ambiguous cases arise from CParser where there is not enough |
| 6130 |
* information to determine if we have a function call or declaration. |
| 6131 |
* type-name ( identifier ) ; |
| 6132 |
* identifier ( identifier ) ; |
| 6133 |
* If exp.e1 is a type-name, then this is a declaration. C11 does not |
| 6134 |
* have type construction syntax, so don't convert this to a cast(). |
| 6135 |
*/ |
| 6136 |
if (numArgs == 1) |
| 6137 |
{ |
| 6138 |
Expression arg = (*exp.arguments)[0]; |
| 6139 |
if (auto ie = (*exp.arguments)[0].isIdentifierExp()) |
| 6140 |
{ |
| 6141 |
TypeExp te = cast(TypeExp)exp.e1; |
| 6142 |
auto initializer = new VoidInitializer(ie.loc); |
| 6143 |
Dsymbol s = new VarDeclaration(ie.loc, te.type, ie.ident, initializer); |
| 6144 |
auto decls = new Dsymbols(1); |
| 6145 |
(*decls)[0] = s; |
| 6146 |
s = new LinkDeclaration(s.loc, LINK.c, decls); |
| 6147 |
result = new DeclarationExp(exp.loc, s); |
| 6148 |
result = result.expressionSemantic(sc); |
| 6149 |
} |
| 6150 |
else |
| 6151 |
{ |
| 6152 |
error(arg.loc, "identifier or `(` expected"); |
| 6153 |
result = ErrorExp.get(); |
| 6154 |
} |
| 6155 |
return; |
| 6156 |
} |
| 6157 |
error(exp.loc, "identifier or `(` expected before `)`"); |
| 6158 |
result = ErrorExp.get(); |
| 6159 |
return; |
| 6160 |
} |
| 6161 |
} |
| 6162 |
|
| 6163 |
Type t1 = exp.e1.type ? exp.e1.type.toBasetype() : null; |
| 6164 |
|
| 6165 |
if (exp.e1.op == EXP.error) |
| 6166 |
{ |
| 6167 |
result = exp.e1; |
| 6168 |
return; |
| 6169 |
} |
| 6170 |
if (arrayExpressionSemantic(exp.arguments.peekSlice(), sc) || |
| 6171 |
preFunctionParameters(sc, exp.argumentList, global.errorSink)) |
| 6172 |
return setError(); |
| 6173 |
|
| 6174 |
// Check for call operator overload |
| 6175 |
if (t1) |
| 6176 |
{ |
| 6177 |
if (t1.ty == Tstruct) |
| 6178 |
{ |
| 6179 |
auto sd = (cast(TypeStruct)t1).sym; |
| 6180 |
sd.size(exp.loc); // Resolve forward references to construct object |
| 6181 |
if (sd.sizeok != Sizeok.done) |
| 6182 |
return setError(); |
| 6183 |
if (!sd.ctor) |
| 6184 |
sd.ctor = sd.searchCtor(); |
| 6185 |
/* If `sd.ctor` is a generated copy constructor, this means that it |
| 6186 |
is the single constructor that this struct has. In order to not |
| 6187 |
disable default construction, the ctor is nullified. The side effect |
| 6188 |
of this is that the generated copy constructor cannot be called |
| 6189 |
explicitly, but that is ok, because when calling a constructor the |
| 6190 |
default constructor should have priority over the generated copy |
| 6191 |
constructor. |
| 6192 |
*/ |
| 6193 |
if (sd.ctor) |
| 6194 |
{ |
| 6195 |
auto ctor = sd.ctor.isCtorDeclaration(); |
| 6196 |
if (ctor && (ctor.isCpCtor || ctor.isMoveCtor) && ctor.isGenerated()) |
| 6197 |
sd.ctor = null; |
| 6198 |
} |
| 6199 |
|
| 6200 |
// First look for constructor |
| 6201 |
if (exp.e1.op == EXP.type && sd.ctor) |
| 6202 |
{ |
| 6203 |
if (!sd.noDefaultCtor && !(exp.arguments && exp.arguments.length)) |
| 6204 |
goto Lx; |
| 6205 |
|
| 6206 |
/* https://issues.dlang.org/show_bug.cgi?id=20695 |
| 6207 |
If all constructors are copy constructors, then |
| 6208 |
try default construction. |
| 6209 |
*/ |
| 6210 |
if (!sd.hasRegularCtor && |
| 6211 |
// https://issues.dlang.org/show_bug.cgi?id=22639 |
| 6212 |
// we might still have a copy constructor that could be called |
| 6213 |
(*exp.arguments)[0].type.mutableOf != sd.type.mutableOf()) |
| 6214 |
goto Lx; |
| 6215 |
|
| 6216 |
auto sle = new StructLiteralExp(exp.loc, sd, null, exp.e1.type); |
| 6217 |
if (!sd.fill(exp.loc, *sle.elements, true)) |
| 6218 |
return setError(); |
| 6219 |
if (checkFrameAccess(exp.loc, sc, sd, sle.elements.length)) |
| 6220 |
return setError(); |
| 6221 |
|
| 6222 |
// https://issues.dlang.org/show_bug.cgi?id=14556 |
| 6223 |
// Set concrete type to avoid further redundant semantic(). |
| 6224 |
sle.type = exp.e1.type; |
| 6225 |
|
| 6226 |
/* Constructor takes a mutable object, so don't use |
| 6227 |
* the immutable initializer symbol. |
| 6228 |
*/ |
| 6229 |
sle.useStaticInit = false; |
| 6230 |
|
| 6231 |
Expression e = sle; |
| 6232 |
if (auto cf = sd.ctor.isCtorDeclaration()) |
| 6233 |
{ |
| 6234 |
e = new DotVarExp(exp.loc, e, cf, true); |
| 6235 |
} |
| 6236 |
else if (auto td = sd.ctor.isTemplateDeclaration()) |
| 6237 |
{ |
| 6238 |
e = new DotIdExp(exp.loc, e, td.ident); |
| 6239 |
} |
| 6240 |
else if (auto os = sd.ctor.isOverloadSet()) |
| 6241 |
{ |
| 6242 |
e = new DotExp(exp.loc, e, new OverExp(exp.loc, os)); |
| 6243 |
} |
| 6244 |
else |
| 6245 |
assert(0); |
| 6246 |
e = new CallExp(exp.loc, e, exp.arguments, exp.names); |
| 6247 |
e = e.expressionSemantic(sc); |
| 6248 |
result = e; |
| 6249 |
return; |
| 6250 |
} |
| 6251 |
// No constructor, look for overload of opCall |
| 6252 |
if (search_function(sd, Id.opCall)) |
| 6253 |
goto L1; |
| 6254 |
// overload of opCall, therefore it's a call |
| 6255 |
if (exp.e1.op != EXP.type) |
| 6256 |
{ |
| 6257 |
if (sd.aliasthis && !isRecursiveAliasThis(att, exp.e1.type)) |
| 6258 |
{ |
| 6259 |
exp.e1 = resolveAliasThis(sc, exp.e1); |
| 6260 |
goto Lagain; |
| 6261 |
} |
| 6262 |
error(exp.loc, "%s `%s` does not overload ()", sd.kind(), sd.toChars()); |
| 6263 |
return setError(); |
| 6264 |
} |
| 6265 |
|
| 6266 |
/* It's a struct literal |
| 6267 |
*/ |
| 6268 |
Lx: |
| 6269 |
Expressions* resolvedArgs = exp.arguments; |
| 6270 |
if (exp.names) |
| 6271 |
{ |
| 6272 |
resolvedArgs = resolveStructLiteralNamedArgs(sd, exp.e1.type, sc, exp.loc, |
| 6273 |
(*exp.names)[], |
| 6274 |
(size_t i, Type t) => (*exp.arguments)[i], |
| 6275 |
i => (*exp.arguments)[i].loc |
| 6276 |
); |
| 6277 |
if (!resolvedArgs) |
| 6278 |
{ |
| 6279 |
result = ErrorExp.get(); |
| 6280 |
return; |
| 6281 |
} |
| 6282 |
} |
| 6283 |
|
| 6284 |
Expression e = new StructLiteralExp(exp.loc, sd, resolvedArgs, exp.e1.type); |
| 6285 |
e = e.expressionSemantic(sc); |
| 6286 |
result = e; |
| 6287 |
return; |
| 6288 |
} |
| 6289 |
else if (t1.ty == Tclass) |
| 6290 |
{ |
| 6291 |
L1: |
| 6292 |
// Rewrite as e1.call(arguments) |
| 6293 |
Expression e = new DotIdExp(exp.loc, exp.e1, Id.opCall); |
| 6294 |
e = new CallExp(exp.loc, e, exp.arguments, exp.names); |
| 6295 |
e = e.expressionSemantic(sc); |
| 6296 |
result = e; |
| 6297 |
return; |
| 6298 |
} |
| 6299 |
else if (exp.e1.op == EXP.type && t1.isScalar()) |
| 6300 |
{ |
| 6301 |
Expression e; |
| 6302 |
|
| 6303 |
// Make sure to use the enum type itself rather than its |
| 6304 |
// base type |
| 6305 |
// https://issues.dlang.org/show_bug.cgi?id=16346 |
| 6306 |
if (exp.e1.type.ty == Tenum) |
| 6307 |
{ |
| 6308 |
t1 = exp.e1.type; |
| 6309 |
} |
| 6310 |
|
| 6311 |
if (!exp.arguments || exp.arguments.length == 0) |
| 6312 |
{ |
| 6313 |
e = t1.defaultInitLiteral(exp.loc); |
| 6314 |
} |
| 6315 |
else if (exp.arguments.length == 1) |
| 6316 |
{ |
| 6317 |
e = (*exp.arguments)[0]; |
| 6318 |
e = e.implicitCastTo(sc, t1); |
| 6319 |
e = new CastExp(exp.loc, e, t1); |
| 6320 |
} |
| 6321 |
else |
| 6322 |
{ |
| 6323 |
error(exp.loc, "more than one argument for construction of `%s`", t1.toChars()); |
| 6324 |
return setError(); |
| 6325 |
} |
| 6326 |
e = e.expressionSemantic(sc); |
| 6327 |
result = e; |
| 6328 |
return; |
| 6329 |
} |
| 6330 |
} |
| 6331 |
|
| 6332 |
FuncDeclaration resolveOverloadSet(Loc loc, Scope* sc, |
| 6333 |
OverloadSet os, Objects* tiargs, Type tthis, ArgumentList argumentList) |
| 6334 |
{ |
| 6335 |
FuncDeclaration f = null; |
| 6336 |
foreach (s; os.a) |
| 6337 |
{ |
| 6338 |
if (tiargs && s.isFuncDeclaration()) |
| 6339 |
continue; |
| 6340 |
auto f2 = resolveFuncCall(loc, sc, s, tiargs, tthis, argumentList, FuncResolveFlag.quiet); |
| 6341 |
if (!f2) |
| 6342 |
continue; |
| 6343 |
if (f2.errors) |
| 6344 |
return null; |
| 6345 |
if (!f) |
| 6346 |
{ |
| 6347 |
f = f2; |
| 6348 |
continue; |
| 6349 |
} |
| 6350 |
/* Match in more than one overload set, |
| 6351 |
* even if one is a 'better' match than the other. |
| 6352 |
*/ |
| 6353 |
if (f.isCsymbol() && f2.isCsymbol()) |
| 6354 |
{ |
| 6355 |
/* C has global name space, so just pick one, such as f. |
| 6356 |
* If f and f2 are not compatible, that's how C rolls. |
| 6357 |
*/ |
| 6358 |
} |
| 6359 |
else |
| 6360 |
ScopeDsymbol.multiplyDefined(loc, f, f2); // issue error |
| 6361 |
} |
| 6362 |
if (f && f.errors) |
| 6363 |
return null; |
| 6364 |
if (f) |
| 6365 |
return f; |
| 6366 |
.error(loc, "no overload matches for `%s`", exp.toErrMsg()); |
| 6367 |
errorSupplemental(loc, "Candidates are:"); |
| 6368 |
foreach (s; os.a) |
| 6369 |
{ |
| 6370 |
overloadApply(s, (ds){ |
| 6371 |
if (auto fd = ds.isFuncDeclaration()) |
| 6372 |
.errorSupplemental(ds.loc, "%s%s", fd.toChars(), |
| 6373 |
fd.type.toTypeFunction().parameterList.parametersTypeToChars()); |
| 6374 |
else |
| 6375 |
.errorSupplemental(ds.loc, "%s", ds.toChars()); |
| 6376 |
return 0; |
| 6377 |
}); |
| 6378 |
} |
| 6379 |
return f; |
| 6380 |
} |
| 6381 |
|
| 6382 |
bool isSuper = false; |
| 6383 |
if (exp.e1.op == EXP.dotVariable && t1.ty == Tfunction || exp.e1.op == EXP.dotTemplateDeclaration) |
| 6384 |
{ |
| 6385 |
UnaExp ue = cast(UnaExp)exp.e1; |
| 6386 |
|
| 6387 |
Expression ue1old = ue.e1; // need for 'right this' check |
| 6388 |
DotVarExp dve; |
| 6389 |
DotTemplateExp dte; |
| 6390 |
Dsymbol s; |
| 6391 |
if (exp.e1.op == EXP.dotVariable) |
| 6392 |
{ |
| 6393 |
dve = cast(DotVarExp)exp.e1; |
| 6394 |
dte = null; |
| 6395 |
s = dve.var; |
| 6396 |
tiargs = null; |
| 6397 |
} |
| 6398 |
else |
| 6399 |
{ |
| 6400 |
dve = null; |
| 6401 |
dte = cast(DotTemplateExp)exp.e1; |
| 6402 |
s = dte.td; |
| 6403 |
} |
| 6404 |
|
| 6405 |
// Do overload resolution |
| 6406 |
exp.f = resolveFuncCall(exp.loc, sc, s, tiargs, ue.e1.type, exp.argumentList, FuncResolveFlag.standard); |
| 6407 |
if (!exp.f || exp.f.errors || exp.f.type.ty == Terror) |
| 6408 |
return setError(); |
| 6409 |
|
| 6410 |
if (exp.f.interfaceVirtual) |
| 6411 |
{ |
| 6412 |
/* Cast 'this' to the type of the interface, and replace f with the interface's equivalent |
| 6413 |
*/ |
| 6414 |
auto b = exp.f.interfaceVirtual; |
| 6415 |
auto ad2 = b.sym; |
| 6416 |
ue.e1 = ue.e1.castTo(sc, ad2.type.addMod(ue.e1.type.mod)); |
| 6417 |
ue.e1 = ue.e1.expressionSemantic(sc); |
| 6418 |
auto vi = findVtblIndex(exp.f, ad2.vtbl[]); |
| 6419 |
assert(vi >= 0); |
| 6420 |
exp.f = ad2.vtbl[vi].isFuncDeclaration(); |
| 6421 |
assert(exp.f); |
| 6422 |
} |
| 6423 |
if (exp.f.needThis()) |
| 6424 |
{ |
| 6425 |
AggregateDeclaration ad = exp.f.isMemberLocal(); |
| 6426 |
ue.e1 = getRightThis(exp.loc, sc, ad, ue.e1, exp.f); |
| 6427 |
if (ue.e1.op == EXP.error) |
| 6428 |
{ |
| 6429 |
result = ue.e1; |
| 6430 |
return; |
| 6431 |
} |
| 6432 |
ethis = ue.e1; |
| 6433 |
tthis = ue.e1.type; |
| 6434 |
if (!(exp.f.type.ty == Tfunction && (cast(TypeFunction)exp.f.type).isScopeQual)) |
| 6435 |
{ |
| 6436 |
if (checkParamArgumentEscape(*sc, exp.f, Id.This, exp.f.vthis, STC.none, ethis, false, false)) |
| 6437 |
return setError(); |
| 6438 |
} |
| 6439 |
} |
| 6440 |
|
| 6441 |
/* Cannot call public functions from inside invariant |
| 6442 |
* (because then the invariant would have infinite recursion) |
| 6443 |
*/ |
| 6444 |
if (sc.func && sc.func.isInvariantDeclaration() && ue.e1.op == EXP.this_ && exp.f.addPostInvariant()) |
| 6445 |
{ |
| 6446 |
error(exp.loc, "cannot call `public`/`export` function `%s` from invariant", exp.f.toChars()); |
| 6447 |
return setError(); |
| 6448 |
} |
| 6449 |
|
| 6450 |
if (!exp.ignoreAttributes) |
| 6451 |
checkFunctionAttributes(exp, sc, exp.f); |
| 6452 |
|
| 6453 |
// Cut-down version of checkAccess() that doesn't use the "most visible" version of exp.f. |
| 6454 |
// We've already selected an overload here. |
| 6455 |
const parent = exp.f.toParent(); |
| 6456 |
if (parent && parent.isTemplateInstance()) |
| 6457 |
{ |
| 6458 |
// already a deprecation |
| 6459 |
} |
| 6460 |
else if (!checkSymbolAccess(sc, exp.f)) |
| 6461 |
{ |
| 6462 |
error(exp.loc, "%s `%s` of type `%s` is not accessible from module `%s`", |
| 6463 |
exp.f.kind(), exp.f.toPrettyChars(), exp.f.type.toChars(), sc._module.toChars); |
| 6464 |
return setError(); |
| 6465 |
} |
| 6466 |
|
| 6467 |
if (!exp.f.needThis()) |
| 6468 |
{ |
| 6469 |
exp.e1 = Expression.combine(ue.e1, new VarExp(exp.loc, exp.f, false)); |
| 6470 |
} |
| 6471 |
else |
| 6472 |
{ |
| 6473 |
if (ue1old.checkRightThis(sc)) |
| 6474 |
return setError(); |
| 6475 |
if (exp.e1.op == EXP.dotVariable) |
| 6476 |
{ |
| 6477 |
dve.var = exp.f; |
| 6478 |
exp.e1.type = exp.f.type; |
| 6479 |
} |
| 6480 |
else |
| 6481 |
{ |
| 6482 |
exp.e1 = new DotVarExp(exp.loc, dte.e1, exp.f, false); |
| 6483 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 6484 |
if (exp.e1.op == EXP.error) |
| 6485 |
return setError(); |
| 6486 |
ue = cast(UnaExp)exp.e1; |
| 6487 |
} |
| 6488 |
version (none) |
| 6489 |
{ |
| 6490 |
printf("ue.e1 = %s\n", ue.e1.toChars()); |
| 6491 |
printf("f = %s\n", exp.f.toChars()); |
| 6492 |
printf("t1 = %s\n", t1.toChars()); |
| 6493 |
printf("e1 = %s\n", exp.e1.toChars()); |
| 6494 |
printf("e1.type = %s\n", exp.e1.type.toChars()); |
| 6495 |
} |
| 6496 |
|
| 6497 |
// See if we need to adjust the 'this' pointer |
| 6498 |
AggregateDeclaration ad = exp.f.isThis(); |
| 6499 |
ClassDeclaration cd = ue.e1.type.isClassHandle(); |
| 6500 |
if (ad && cd && ad.isClassDeclaration()) |
| 6501 |
{ |
| 6502 |
if (ue.e1.op == EXP.dotType) |
| 6503 |
{ |
| 6504 |
ue.e1 = (cast(DotTypeExp)ue.e1).e1; |
| 6505 |
exp.directcall = true; |
| 6506 |
} |
| 6507 |
else if (ue.e1.op == EXP.super_) |
| 6508 |
exp.directcall = true; |
| 6509 |
else if ((cd.storage_class & STC.final_) != 0) // https://issues.dlang.org/show_bug.cgi?id=14211 |
| 6510 |
exp.directcall = true; |
| 6511 |
|
| 6512 |
if (ad != cd) |
| 6513 |
{ |
| 6514 |
ue.e1 = ue.e1.castTo(sc, ad.type.addMod(ue.e1.type.mod)); |
| 6515 |
ue.e1 = ue.e1.expressionSemantic(sc); |
| 6516 |
} |
| 6517 |
} |
| 6518 |
} |
| 6519 |
// If we've got a pointer to a function then deference it |
| 6520 |
// https://issues.dlang.org/show_bug.cgi?id=16483 |
| 6521 |
if (exp.e1.type.isPtrToFunction()) |
| 6522 |
{ |
| 6523 |
Expression e = new PtrExp(exp.loc, exp.e1); |
| 6524 |
e.type = exp.e1.type.nextOf(); |
| 6525 |
exp.e1 = e; |
| 6526 |
} |
| 6527 |
t1 = exp.e1.type; |
| 6528 |
} |
| 6529 |
else if (exp.e1.op == EXP.super_ || exp.e1.op == EXP.this_) |
| 6530 |
{ |
| 6531 |
auto ad = sc.func ? sc.func.isThis() : null; |
| 6532 |
auto cd = ad ? ad.isClassDeclaration() : null; |
| 6533 |
|
| 6534 |
isSuper = exp.e1.op == EXP.super_; |
| 6535 |
if (isSuper) |
| 6536 |
{ |
| 6537 |
// Base class constructor call |
| 6538 |
if (!cd || !cd.baseClass || !sc.func.isCtorDeclaration()) |
| 6539 |
{ |
| 6540 |
error(exp.loc, "super class constructor call must be in a constructor"); |
| 6541 |
return setError(); |
| 6542 |
} |
| 6543 |
if (!cd.baseClass.ctor) |
| 6544 |
{ |
| 6545 |
error(exp.loc, "no super class constructor for `%s`", cd.baseClass.toChars()); |
| 6546 |
return setError(); |
| 6547 |
} |
| 6548 |
} |
| 6549 |
else |
| 6550 |
{ |
| 6551 |
// `this` call expression must be inside a |
| 6552 |
// constructor |
| 6553 |
if (!ad || !sc.func.isCtorDeclaration()) |
| 6554 |
{ |
| 6555 |
error(exp.loc, "constructor call must be in a constructor"); |
| 6556 |
return setError(); |
| 6557 |
} |
| 6558 |
|
| 6559 |
// https://issues.dlang.org/show_bug.cgi?id=18719 |
| 6560 |
// If `exp` is a call expression to another constructor |
| 6561 |
// then it means that all struct/class fields will be |
| 6562 |
// initialized after this call. |
| 6563 |
foreach (ref field; sc.ctorflow.fieldinit) |
| 6564 |
{ |
| 6565 |
field.csx |= CSX.this_ctor; |
| 6566 |
} |
| 6567 |
} |
| 6568 |
|
| 6569 |
if (!sc.intypeof && !(sc.ctorflow.callSuper & CSX.halt)) |
| 6570 |
{ |
| 6571 |
if (sc.inLoop || sc.ctorflow.callSuper & CSX.label) |
| 6572 |
error(exp.loc, "constructor calls not allowed in loops or after labels"); |
| 6573 |
if (sc.ctorflow.callSuper & (CSX.super_ctor | CSX.this_ctor)) |
| 6574 |
error(exp.loc, "multiple constructor calls"); |
| 6575 |
if ((sc.ctorflow.callSuper & CSX.return_) && !(sc.ctorflow.callSuper & CSX.any_ctor)) |
| 6576 |
error(exp.loc, "an earlier `return` statement skips constructor"); |
| 6577 |
sc.ctorflow.callSuper |= CSX.any_ctor | (isSuper ? CSX.super_ctor : CSX.this_ctor); |
| 6578 |
} |
| 6579 |
|
| 6580 |
tthis = ad.type.addMod(sc.func.type.mod); |
| 6581 |
auto ctor = isSuper ? cd.baseClass.ctor : ad.ctor; |
| 6582 |
if (auto os = ctor.isOverloadSet()) |
| 6583 |
exp.f = resolveOverloadSet(exp.loc, sc, os, null, tthis, exp.argumentList); |
| 6584 |
else |
| 6585 |
exp.f = resolveFuncCall(exp.loc, sc, ctor, null, tthis, exp.argumentList, FuncResolveFlag.standard); |
| 6586 |
|
| 6587 |
if (!exp.f || exp.f.errors) |
| 6588 |
return setError(); |
| 6589 |
|
| 6590 |
checkFunctionAttributes(exp, sc, exp.f); |
| 6591 |
if (!checkSymbolAccess(sc, exp.f)) |
| 6592 |
{ |
| 6593 |
error(exp.loc, "%s `%s` is not accessible from module `%s`", |
| 6594 |
exp.f.kind(), exp.f.toPrettyChars(), sc._module.toChars); |
| 6595 |
return setError(); |
| 6596 |
} |
| 6597 |
|
| 6598 |
exp.e1 = new DotVarExp(exp.e1.loc, exp.e1, exp.f, false); |
| 6599 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 6600 |
// https://issues.dlang.org/show_bug.cgi?id=21095 |
| 6601 |
if (exp.e1.op == EXP.error) |
| 6602 |
return setError(); |
| 6603 |
t1 = exp.e1.type; |
| 6604 |
|
| 6605 |
// BUG: this should really be done by checking the static |
| 6606 |
// call graph |
| 6607 |
if (exp.f == sc.func) |
| 6608 |
{ |
| 6609 |
error(exp.loc, "cyclic constructor call"); |
| 6610 |
return setError(); |
| 6611 |
} |
| 6612 |
} |
| 6613 |
else if (auto oe = exp.e1.isOverExp()) |
| 6614 |
{ |
| 6615 |
exp.f = resolveOverloadSet(exp.loc, sc, oe.vars, tiargs, tthis, exp.argumentList); |
| 6616 |
if (!exp.f) |
| 6617 |
return setError(); |
| 6618 |
if (ethis) |
| 6619 |
exp.e1 = new DotVarExp(exp.loc, ethis, exp.f, false); |
| 6620 |
else |
| 6621 |
exp.e1 = new VarExp(exp.loc, exp.f, false); |
| 6622 |
goto Lagain; |
| 6623 |
} |
| 6624 |
else if (!t1) |
| 6625 |
{ |
| 6626 |
error(exp.loc, "function expected before `()`, not `%s`", exp.e1.toErrMsg()); |
| 6627 |
return setError(); |
| 6628 |
} |
| 6629 |
else if (t1.ty == Terror) |
| 6630 |
{ |
| 6631 |
return setError(); |
| 6632 |
} |
| 6633 |
else if (t1.ty != Tfunction) |
| 6634 |
{ |
| 6635 |
TypeFunction tf; |
| 6636 |
const(char)* p; |
| 6637 |
Dsymbol s; |
| 6638 |
exp.f = null; |
| 6639 |
if (auto fe = exp.e1.isFuncExp()) |
| 6640 |
{ |
| 6641 |
// function literal that direct called is always inferred. |
| 6642 |
assert(fe.fd); |
| 6643 |
exp.f = fe.fd; |
| 6644 |
tf = cast(TypeFunction)exp.f.type; |
| 6645 |
p = "function literal"; |
| 6646 |
} |
| 6647 |
else if (t1.ty == Tdelegate) |
| 6648 |
{ |
| 6649 |
TypeDelegate td = cast(TypeDelegate)t1; |
| 6650 |
assert(td.next.ty == Tfunction); |
| 6651 |
tf = cast(TypeFunction)td.next; |
| 6652 |
p = "delegate"; |
| 6653 |
} |
| 6654 |
else if (auto tfx = t1.isPtrToFunction()) |
| 6655 |
{ |
| 6656 |
tf = tfx; |
| 6657 |
p = "function pointer"; |
| 6658 |
} |
| 6659 |
else if (exp.e1.op == EXP.dotVariable && (cast(DotVarExp)exp.e1).var.isOverDeclaration()) |
| 6660 |
{ |
| 6661 |
DotVarExp dve = cast(DotVarExp)exp.e1; |
| 6662 |
exp.f = resolveFuncCall(exp.loc, sc, dve.var, tiargs, dve.e1.type, exp.argumentList, FuncResolveFlag.overloadOnly); |
| 6663 |
if (!exp.f) |
| 6664 |
return setError(); |
| 6665 |
if (exp.f.needThis()) |
| 6666 |
{ |
| 6667 |
dve.var = exp.f; |
| 6668 |
dve.type = exp.f.type; |
| 6669 |
dve.hasOverloads = false; |
| 6670 |
goto Lagain; |
| 6671 |
} |
| 6672 |
exp.e1 = new VarExp(dve.loc, exp.f, false); |
| 6673 |
Expression e = new CommaExp(exp.loc, dve.e1, exp); |
| 6674 |
result = e.expressionSemantic(sc); |
| 6675 |
return; |
| 6676 |
} |
| 6677 |
else if (exp.e1.op == EXP.variable && (cast(VarExp)exp.e1).var.isOverDeclaration()) |
| 6678 |
{ |
| 6679 |
s = (cast(VarExp)exp.e1).var; |
| 6680 |
goto L2; |
| 6681 |
} |
| 6682 |
else if (exp.e1.op == EXP.template_) |
| 6683 |
{ |
| 6684 |
s = (cast(TemplateExp)exp.e1).td; |
| 6685 |
L2: |
| 6686 |
exp.f = resolveFuncCall(exp.loc, sc, s, tiargs, null, exp.argumentList, |
| 6687 |
exp.isUfcsRewrite ? FuncResolveFlag.ufcs : FuncResolveFlag.standard); |
| 6688 |
if (!exp.f || exp.f.errors) |
| 6689 |
return setError(); |
| 6690 |
if (exp.f.needThis()) |
| 6691 |
{ |
| 6692 |
if (hasThis(sc)) |
| 6693 |
{ |
| 6694 |
// Supply an implicit 'this', as in |
| 6695 |
// this.ident |
| 6696 |
exp.e1 = new DotVarExp(exp.loc, (new ThisExp(exp.loc)).expressionSemantic(sc), exp.f, false); |
| 6697 |
goto Lagain; |
| 6698 |
} |
| 6699 |
else if (isNeedThisScope(sc, exp.f)) |
| 6700 |
{ |
| 6701 |
return needThisError(exp.loc, exp.f); |
| 6702 |
} |
| 6703 |
} |
| 6704 |
exp.e1 = new VarExp(exp.e1.loc, exp.f, false); |
| 6705 |
goto Lagain; |
| 6706 |
} |
| 6707 |
else |
| 6708 |
{ |
| 6709 |
error(exp.loc, "function expected before `()`, not `%s` of type `%s`", exp.e1.toErrMsg(), exp.e1.type.toChars()); |
| 6710 |
return setError(); |
| 6711 |
} |
| 6712 |
|
| 6713 |
void errorHelper(const(char)* failMessage) scope |
| 6714 |
{ |
| 6715 |
OutBuffer buf; |
| 6716 |
buf.writeByte('('); |
| 6717 |
argExpTypesToCBuffer(buf, exp.arguments); |
| 6718 |
buf.writeByte(')'); |
| 6719 |
if (tthis) |
| 6720 |
tthis.modToBuffer(buf); |
| 6721 |
|
| 6722 |
//printf("tf = %s, args = %s\n", tf.deco, (*arguments)[0].type.deco); |
| 6723 |
.error(exp.loc, "%s `%s` is not callable using argument types `%s`", |
| 6724 |
p, exp.e1.toErrMsg(), buf.peekChars()); |
| 6725 |
if (failMessage) |
| 6726 |
errorSupplemental(exp.loc, "%s", failMessage); |
| 6727 |
} |
| 6728 |
|
| 6729 |
if (callMatch(exp.f, tf, null, exp.argumentList, 0, &errorHelper, sc) == MATCH.nomatch) |
| 6730 |
return setError(); |
| 6731 |
|
| 6732 |
// Purity and safety check should run after testing arguments matching |
| 6733 |
if (exp.f) |
| 6734 |
{ |
| 6735 |
exp.f.checkPurity(exp.loc, sc); |
| 6736 |
exp.f.checkSafety(exp.loc, sc); |
| 6737 |
exp.f.checkNogc(exp.loc, sc); |
| 6738 |
if (exp.f.checkNestedFuncReference(sc, exp.loc)) |
| 6739 |
return setError(); |
| 6740 |
} |
| 6741 |
else if (sc.func && sc.intypeof != 1 && !(sc.ctfe || sc.debug_)) |
| 6742 |
{ |
| 6743 |
bool err = false; |
| 6744 |
if (!tf.purity && sc.func.setImpure(exp.loc, "calling impure `%s`", exp.e1)) |
| 6745 |
{ |
| 6746 |
error(exp.loc, "`pure` %s `%s` cannot call impure %s `%s`", |
| 6747 |
sc.func.kind(), sc.func.toPrettyChars(), p, exp.e1.toErrMsg()); |
| 6748 |
err = true; |
| 6749 |
} |
| 6750 |
if (!tf.isNogc && sc.func.setGC(exp.loc, "calling non-@nogc `%s`", exp.e1)) |
| 6751 |
{ |
| 6752 |
error(exp.loc, "`@nogc` %s `%s` cannot call non-@nogc %s `%s`", |
| 6753 |
sc.func.kind(), sc.func.toPrettyChars(), p, exp.e1.toErrMsg()); |
| 6754 |
err = true; |
| 6755 |
} |
| 6756 |
if (tf.trust <= TRUST.system && sc.setUnsafe(true, exp.loc, |
| 6757 |
"calling `@system` `%s`", exp.e1)) |
| 6758 |
{ |
| 6759 |
error(exp.loc, "`@safe` %s `%s` cannot call `@system` %s `%s`", |
| 6760 |
sc.func.kind(), sc.func.toPrettyChars(), p, exp.e1.toErrMsg()); |
| 6761 |
err = true; |
| 6762 |
} |
| 6763 |
if (err) |
| 6764 |
return setError(); |
| 6765 |
} |
| 6766 |
|
| 6767 |
if (t1.ty == Tpointer) |
| 6768 |
{ |
| 6769 |
Expression e = new PtrExp(exp.loc, exp.e1); |
| 6770 |
e.type = tf; |
| 6771 |
exp.e1 = e; |
| 6772 |
} |
| 6773 |
t1 = tf; |
| 6774 |
} |
| 6775 |
else if (VarExp ve = exp.e1.isVarExp()) |
| 6776 |
{ |
| 6777 |
// Do overload resolution |
| 6778 |
exp.f = ve.var.isFuncDeclaration(); |
| 6779 |
assert(exp.f); |
| 6780 |
tiargs = null; |
| 6781 |
|
| 6782 |
if (ve.hasOverloads && exp.f.overnext) |
| 6783 |
exp.f = resolveFuncCall(exp.loc, sc, exp.f, tiargs, null, exp.argumentList, FuncResolveFlag.overloadOnly); |
| 6784 |
else |
| 6785 |
{ |
| 6786 |
exp.f = exp.f.toAliasFunc(); |
| 6787 |
TypeFunction tf = cast(TypeFunction)exp.f.type; |
| 6788 |
|
| 6789 |
void errorHelper2(const(char)* failMessage) scope |
| 6790 |
{ |
| 6791 |
OutBuffer buf; |
| 6792 |
buf.writeByte('('); |
| 6793 |
argExpTypesToCBuffer(buf, exp.arguments); |
| 6794 |
buf.writeByte(')'); |
| 6795 |
|
| 6796 |
//printf("tf = %s, args = %s\n", tf.deco, (*arguments)[0].type.deco); |
| 6797 |
if (exp.isUfcsRewrite) |
| 6798 |
{ |
| 6799 |
const arg = (*exp.argumentList.arguments)[0]; |
| 6800 |
.error(exp.loc, "no property `%s` for `%s` of type `%s`", exp.f.ident.toChars(), arg.toChars(), arg.type.toChars()); |
| 6801 |
.errorSupplemental(exp.loc, "the following error occured while looking for a UFCS match"); |
| 6802 |
} |
| 6803 |
|
| 6804 |
.error(exp.loc, "%s `%s` is not callable using argument types `%s`", |
| 6805 |
exp.f.kind(), exp.f.toErrMsg(), buf.peekChars()); |
| 6806 |
if (failMessage) |
| 6807 |
errorSupplemental(exp.loc, "%s", failMessage); |
| 6808 |
.errorSupplemental(exp.f.loc, "`%s%s` declared here", exp.f.toPrettyChars(), parametersTypeToChars(tf.parameterList)); |
| 6809 |
exp.f = null; |
| 6810 |
} |
| 6811 |
|
| 6812 |
if (callMatch(exp.f, tf, null, exp.argumentList, 0, &errorHelper2, sc) == MATCH.nomatch) |
| 6813 |
exp.f = null; |
| 6814 |
} |
| 6815 |
if (!exp.f || exp.f.errors) |
| 6816 |
return setError(); |
| 6817 |
|
| 6818 |
if (exp.f.needThis()) |
| 6819 |
{ |
| 6820 |
// Change the ancestor lambdas to delegate before hasThis(sc) call. |
| 6821 |
if (exp.f.checkNestedFuncReference(sc, exp.loc)) |
| 6822 |
return setError(); |
| 6823 |
|
| 6824 |
auto memberFunc = hasThis(sc); |
| 6825 |
if (memberFunc && haveSameThis(memberFunc, exp.f)) |
| 6826 |
{ |
| 6827 |
// Supply an implicit 'this', as in |
| 6828 |
// this.ident |
| 6829 |
exp.e1 = new DotVarExp(exp.loc, (new ThisExp(exp.loc)).expressionSemantic(sc), ve.var); |
| 6830 |
// Note: we cannot use f directly, because further overload resolution |
| 6831 |
// through the supplied 'this' may cause different result. |
| 6832 |
goto Lagain; |
| 6833 |
} |
| 6834 |
else if (isNeedThisScope(sc, exp.f)) |
| 6835 |
{ |
| 6836 |
// At this point it is possible that `exp.f` had an ambiguity error that was |
| 6837 |
// silenced because the previous call to `resolveFuncCall` was done using |
| 6838 |
// `FuncResolveFlag.overloadOnly`. To make sure that a proper error message |
| 6839 |
// is printed, redo the call with `FuncResolveFlag.standard`. |
| 6840 |
// |
| 6841 |
// https://issues.dlang.org/show_bug.cgi?id=22157 |
| 6842 |
if (exp.f.overnext) |
| 6843 |
exp.f = resolveFuncCall(exp.loc, sc, exp.f, tiargs, null, exp.argumentList, FuncResolveFlag.standard); |
| 6844 |
|
| 6845 |
if (!exp.f || exp.f.errors) |
| 6846 |
return setError(); |
| 6847 |
|
| 6848 |
// If no error is printed, it means that `f` is the single matching overload |
| 6849 |
// and it needs `this`. |
| 6850 |
return needThisError(exp.loc, exp.f); |
| 6851 |
} |
| 6852 |
} |
| 6853 |
|
| 6854 |
checkFunctionAttributes(exp, sc, exp.f); |
| 6855 |
checkAccess(exp.loc, sc, null, exp.f); |
| 6856 |
if (exp.f.checkNestedFuncReference(sc, exp.loc)) |
| 6857 |
return setError(); |
| 6858 |
|
| 6859 |
ethis = null; |
| 6860 |
tthis = null; |
| 6861 |
|
| 6862 |
if (ve.hasOverloads) |
| 6863 |
{ |
| 6864 |
exp.e1 = new VarExp(ve.loc, exp.f, false); |
| 6865 |
exp.e1.type = exp.f.type; |
| 6866 |
} |
| 6867 |
t1 = exp.f.type; |
| 6868 |
} |
| 6869 |
assert(t1.ty == Tfunction); |
| 6870 |
|
| 6871 |
Expression argprefix; |
| 6872 |
if (!exp.arguments) |
| 6873 |
exp.arguments = new Expressions(); |
| 6874 |
if (functionParameters(exp.loc, sc, cast(TypeFunction)t1, ethis, tthis, exp.argumentList, exp.f, &exp.type, &argprefix)) |
| 6875 |
return setError(); |
| 6876 |
|
| 6877 |
if (!exp.type) |
| 6878 |
{ |
| 6879 |
exp.e1 = e1org; // https://issues.dlang.org/show_bug.cgi?id=10922 |
| 6880 |
// avoid recursive expression printing |
| 6881 |
error(exp.loc, "forward reference to inferred return type of function call `%s`", exp.toErrMsg()); |
| 6882 |
return setError(); |
| 6883 |
} |
| 6884 |
|
| 6885 |
if (exp.f && exp.f.tintro) |
| 6886 |
{ |
| 6887 |
Type t = exp.type; |
| 6888 |
int offset = 0; |
| 6889 |
TypeFunction tf = cast(TypeFunction)exp.f.tintro; |
| 6890 |
if (tf.next.isBaseOf(t, &offset) && offset) |
| 6891 |
{ |
| 6892 |
exp.type = tf.next; |
| 6893 |
result = Expression.combine(argprefix, exp.castTo(sc, t)); |
| 6894 |
return; |
| 6895 |
} |
| 6896 |
} |
| 6897 |
|
| 6898 |
// Handle the case of a direct lambda call |
| 6899 |
if (exp.f && exp.f.isFuncLiteralDeclaration() && sc.func && !sc.intypeof) |
| 6900 |
{ |
| 6901 |
exp.f.tookAddressOf = 0; |
| 6902 |
} |
| 6903 |
|
| 6904 |
result = Expression.combine(argprefix, exp); |
| 6905 |
|
| 6906 |
if (isSuper) |
| 6907 |
{ |
| 6908 |
auto ad = sc.func ? sc.func.isThis() : null; |
| 6909 |
auto cd = ad ? ad.isClassDeclaration() : null; |
| 6910 |
if (cd && cd.classKind == ClassKind.cpp && exp.f && !exp.f.fbody) |
| 6911 |
{ |
| 6912 |
// if super is defined in C++, it sets the vtable pointer to the base class |
| 6913 |
// so we have to restore it, but still return 'this' from super() call: |
| 6914 |
// (auto __vptrTmp = this.__vptr, auto __superTmp = super()), (this.__vptr = __vptrTmp, __superTmp) |
| 6915 |
Loc loc = exp.loc; |
| 6916 |
|
| 6917 |
auto vptr = new DotIdExp(loc, new ThisExp(loc), Id.__vptr); |
| 6918 |
auto vptrTmpDecl = copyToTemp(STC.none, "__vptrTmp", vptr); |
| 6919 |
auto declareVptrTmp = new DeclarationExp(loc, vptrTmpDecl); |
| 6920 |
|
| 6921 |
auto superTmpDecl = copyToTemp(STC.none, "__superTmp", result); |
| 6922 |
auto declareSuperTmp = new DeclarationExp(loc, superTmpDecl); |
| 6923 |
|
| 6924 |
auto declareTmps = new CommaExp(loc, declareVptrTmp, declareSuperTmp); |
| 6925 |
|
| 6926 |
auto restoreVptr = new AssignExp(loc, vptr.syntaxCopy(), new VarExp(loc, vptrTmpDecl)); |
| 6927 |
|
| 6928 |
Expression e = new CommaExp(loc, declareTmps, new CommaExp(loc, restoreVptr, new VarExp(loc, superTmpDecl))); |
| 6929 |
result = e.expressionSemantic(sc); |
| 6930 |
} |
| 6931 |
} |
| 6932 |
|
| 6933 |
// `super.fun()` with fun being abstract and unimplemented |
| 6934 |
auto supDotFun = exp.e1.isDotVarExp(); |
| 6935 |
if (supDotFun && supDotFun.e1.isSuperExp() && exp.f && exp.f.isAbstract() && !exp.f.fbody) |
| 6936 |
{ |
| 6937 |
error(exp.loc, "call to unimplemented abstract function `%s`", exp.f.toFullSignature()); |
| 6938 |
errorSupplemental(exp.loc, "declared here: %s", exp.f.loc.toChars()); |
| 6939 |
} |
| 6940 |
|
| 6941 |
// declare dual-context container |
| 6942 |
if (exp.f && exp.f.hasDualContext() && !sc.intypeof && sc.func) |
| 6943 |
{ |
| 6944 |
// check access to second `this` |
| 6945 |
if (AggregateDeclaration ad2 = exp.f.isMember2()) |
| 6946 |
{ |
| 6947 |
Expression te = new ThisExp(exp.loc).expressionSemantic(sc); |
| 6948 |
if (te.op != EXP.error) |
| 6949 |
te = getRightThis(exp.loc, sc, ad2, te, exp.f); |
| 6950 |
if (te.op == EXP.error) |
| 6951 |
{ |
| 6952 |
error(exp.loc, "need `this` of type `%s` to call function `%s`", ad2.toChars(), exp.f.toChars()); |
| 6953 |
return setError(); |
| 6954 |
} |
| 6955 |
} |
| 6956 |
exp.vthis2 = makeThis2Argument(exp.loc, sc, exp.f); |
| 6957 |
Expression de = new DeclarationExp(exp.loc, exp.vthis2); |
| 6958 |
result = Expression.combine(de, result); |
| 6959 |
result = result.expressionSemantic(sc); |
| 6960 |
} |
| 6961 |
} |
| 6962 |
|
| 6963 |
override void visit(DeclarationExp e) |
| 6964 |
{ |
| 6965 |
static if (LOGSEMANTIC) |
| 6966 |
{ |
| 6967 |
printf("DeclarationExp::semantic() %s\n", e.toChars()); |
| 6968 |
} |
| 6969 |
|
| 6970 |
const olderrors = global.errors; |
| 6971 |
|
| 6972 |
/* This is here to support extern(linkage) declaration, |
| 6973 |
* where the extern(linkage) winds up being an AttribDeclaration |
| 6974 |
* wrapper. |
| 6975 |
*/ |
| 6976 |
Dsymbol s = e.declaration; |
| 6977 |
|
| 6978 |
while (1) |
| 6979 |
{ |
| 6980 |
AttribDeclaration ad = s.isAttribDeclaration(); |
| 6981 |
if (ad && ad.decl && ad.decl.length == 1) |
| 6982 |
s = (*ad.decl)[0]; |
| 6983 |
else |
| 6984 |
break; |
| 6985 |
} |
| 6986 |
|
| 6987 |
//printf("inserting '%s' %p into sc = %p\n", s.toChars(), s, sc); |
| 6988 |
// Insert into both local scope and function scope. |
| 6989 |
// Must be unique in both. |
| 6990 |
if (s.ident) |
| 6991 |
{ |
| 6992 |
VarDeclaration v = s.isVarDeclaration(); |
| 6993 |
if (v) |
| 6994 |
{ |
| 6995 |
if (sc.inCfile) |
| 6996 |
{ |
| 6997 |
/* Do semantic() on the type before inserting v into the symbol table |
| 6998 |
*/ |
| 6999 |
if (!v.originalType) |
| 7000 |
v.originalType = v.type.syntaxCopy(); |
| 7001 |
Scope* sc2 = sc.push(); |
| 7002 |
sc2.stc |= v.storage_class & STC.FUNCATTR; |
| 7003 |
sc2.linkage = LINK.c; // account for the extern(C) in front of the declaration |
| 7004 |
v.inuse++; |
| 7005 |
v.type = v.type.typeSemantic(v.loc, sc2); |
| 7006 |
v.inuse--; |
| 7007 |
sc2.pop(); |
| 7008 |
} |
| 7009 |
else |
| 7010 |
{ |
| 7011 |
/* Do semantic() on initializer first so this will be illegal: |
| 7012 |
* int a = a; |
| 7013 |
*/ |
| 7014 |
e.declaration.dsymbolSemantic(sc); |
| 7015 |
s.parent = sc.parent; |
| 7016 |
} |
| 7017 |
} |
| 7018 |
|
| 7019 |
if (!sc.insert(s)) |
| 7020 |
{ |
| 7021 |
Dsymbol pscopesym; |
| 7022 |
auto conflict = sc.search(Loc.initial, s.ident, pscopesym); |
| 7023 |
error(e.loc, "declaration `%s` is already defined", s.toPrettyChars()); |
| 7024 |
errorSupplemental(conflict.loc, "`%s` `%s` is defined here", |
| 7025 |
conflict.kind(), conflict.toChars()); |
| 7026 |
return setError(); |
| 7027 |
} |
| 7028 |
|
| 7029 |
if (v && sc.inCfile) |
| 7030 |
{ |
| 7031 |
/* Do semantic() on initializer last so this will be legal: |
| 7032 |
* int a = a; |
| 7033 |
*/ |
| 7034 |
e.declaration.dsymbolSemantic(sc); |
| 7035 |
s.parent = sc.parent; |
| 7036 |
} |
| 7037 |
|
| 7038 |
if (sc.func) |
| 7039 |
{ |
| 7040 |
// https://issues.dlang.org/show_bug.cgi?id=11720 |
| 7041 |
if ((s.isFuncDeclaration() || |
| 7042 |
s.isAggregateDeclaration() || |
| 7043 |
s.isEnumDeclaration() || |
| 7044 |
s.isTemplateDeclaration() || |
| 7045 |
v |
| 7046 |
) && !sc.func.localsymtab.insert(s)) |
| 7047 |
{ |
| 7048 |
// Get the previous symbol |
| 7049 |
Dsymbol originalSymbol = sc.func.localsymtab.lookup(s.ident); |
| 7050 |
|
| 7051 |
// Perturb the name mangling so that the symbols can co-exist |
| 7052 |
// instead of colliding |
| 7053 |
s.localNum = cast(ushort)(originalSymbol.localNum + 1); |
| 7054 |
// 65535 should be enough for anyone |
| 7055 |
if (!s.localNum) |
| 7056 |
{ |
| 7057 |
error(e.loc, "more than 65535 symbols with name `%s` generated", s.ident.toChars()); |
| 7058 |
return setError(); |
| 7059 |
} |
| 7060 |
|
| 7061 |
// Replace originalSymbol with s, which updates the localCount |
| 7062 |
sc.func.localsymtab.update(s); |
| 7063 |
|
| 7064 |
// The mangling change only works for D mangling |
| 7065 |
} |
| 7066 |
|
| 7067 |
if (!sc.inCfile) |
| 7068 |
{ |
| 7069 |
/* https://issues.dlang.org/show_bug.cgi?id=21272 |
| 7070 |
* If we are in a foreach body we need to extract the |
| 7071 |
* function containing the foreach |
| 7072 |
*/ |
| 7073 |
FuncDeclaration fes_enclosing_func; |
| 7074 |
if (sc.func && sc.func.fes) |
| 7075 |
fes_enclosing_func = sc.enclosing.enclosing.func; |
| 7076 |
|
| 7077 |
// Disallow shadowing |
| 7078 |
for (Scope* scx = sc.enclosing; scx && (scx.func == sc.func || (fes_enclosing_func && scx.func == fes_enclosing_func)); scx = scx.enclosing) |
| 7079 |
{ |
| 7080 |
if (!scx.scopesym || !scx.scopesym.symtab) |
| 7081 |
continue; |
| 7082 |
Dsymbol s2 = scx.scopesym.symtab.lookup(s.ident); |
| 7083 |
if (s2 is null || s == s2) |
| 7084 |
continue; |
| 7085 |
// allow STC.local symbols to be shadowed |
| 7086 |
// TODO: not really an optimal design |
| 7087 |
auto decl = s2.isDeclaration(); |
| 7088 |
if (decl && (decl.storage_class & STC.local)) |
| 7089 |
continue; |
| 7090 |
if (sc.func.fes) |
| 7091 |
{ |
| 7092 |
deprecation(e.loc, "%s `%s` is shadowing %s `%s`", s.kind(), s.ident.toChars(), s2.kind(), s2.toPrettyChars()); |
| 7093 |
deprecationSupplemental(s2.loc, "declared here"); |
| 7094 |
} |
| 7095 |
else |
| 7096 |
{ |
| 7097 |
error(e.loc, "%s `%s` is shadowing %s `%s`", s.kind(), s.ident.toChars(), s2.kind(), s2.toPrettyChars()); |
| 7098 |
errorSupplemental(s2.loc, "declared here"); |
| 7099 |
return setError(); |
| 7100 |
} |
| 7101 |
} |
| 7102 |
} |
| 7103 |
} |
| 7104 |
} |
| 7105 |
if (!s.isVarDeclaration()) |
| 7106 |
{ |
| 7107 |
Scope* sc2 = sc; |
| 7108 |
if (sc2.stc & (STC.pure_ | STC.nothrow_ | STC.nogc)) |
| 7109 |
sc2 = sc.push(); |
| 7110 |
sc2.stc &= ~(STC.pure_ | STC.nothrow_ | STC.nogc); |
| 7111 |
e.declaration.dsymbolSemantic(sc2); |
| 7112 |
if (sc2 != sc) |
| 7113 |
sc2.pop(); |
| 7114 |
s.parent = sc.parent; |
| 7115 |
} |
| 7116 |
if (global.errors == olderrors) |
| 7117 |
{ |
| 7118 |
e.declaration.semantic2(sc); |
| 7119 |
if (global.errors == olderrors) |
| 7120 |
{ |
| 7121 |
e.declaration.semantic3(sc); |
| 7122 |
} |
| 7123 |
} |
| 7124 |
// todo: error in declaration should be propagated. |
| 7125 |
|
| 7126 |
e.type = Type.tvoid; |
| 7127 |
result = e; |
| 7128 |
} |
| 7129 |
|
| 7130 |
override void visit(TypeidExp exp) |
| 7131 |
{ |
| 7132 |
static if (LOGSEMANTIC) |
| 7133 |
{ |
| 7134 |
printf("TypeidExp::semantic() %s\n", exp.toChars()); |
| 7135 |
} |
| 7136 |
Type ta = isType(exp.obj); |
| 7137 |
Expression ea = isExpression(exp.obj); |
| 7138 |
Dsymbol sa = isDsymbol(exp.obj); |
| 7139 |
//printf("ta %p ea %p sa %p\n", ta, ea, sa); |
| 7140 |
|
| 7141 |
if (ta) |
| 7142 |
{ |
| 7143 |
dmd.typesem.resolve(ta, exp.loc, sc, ea, ta, sa, true); |
| 7144 |
} |
| 7145 |
|
| 7146 |
if (ea) |
| 7147 |
{ |
| 7148 |
if (auto sym = getDsymbol(ea)) |
| 7149 |
ea = symbolToExp(sym, exp.loc, sc, false); |
| 7150 |
else |
| 7151 |
ea = ea.expressionSemantic(sc); |
| 7152 |
ea = resolveProperties(sc, ea); |
| 7153 |
ta = ea.type; |
| 7154 |
if (ea.op == EXP.type) |
| 7155 |
ea = null; |
| 7156 |
} |
| 7157 |
|
| 7158 |
if (!ta) |
| 7159 |
{ |
| 7160 |
//printf("ta %p ea %p sa %p\n", ta, ea, sa); |
| 7161 |
error(exp.loc, "no type for `typeid(%s)`", ea ? ea.toErrMsg() : (sa ? sa.toChars() : "")); |
| 7162 |
return setError(); |
| 7163 |
} |
| 7164 |
|
| 7165 |
ta.checkComplexTransition(exp.loc, sc); |
| 7166 |
|
| 7167 |
auto tb = ta.toBasetype(); |
| 7168 |
if (ea && tb.ty == Tclass) |
| 7169 |
{ |
| 7170 |
if (tb.toDsymbol(sc).isClassDeclaration().classKind == ClassKind.cpp) |
| 7171 |
{ |
| 7172 |
error(exp.loc, "runtime type information is not supported for `extern(C++)` classes"); |
| 7173 |
return setError(); |
| 7174 |
} |
| 7175 |
else if (!Type.typeinfoclass) |
| 7176 |
{ |
| 7177 |
error(exp.loc, "`object.TypeInfo_Class` could not be found, but is implicitly used"); |
| 7178 |
return setError(); |
| 7179 |
} |
| 7180 |
else |
| 7181 |
{ |
| 7182 |
/* Get the dynamic type, which is .classinfo |
| 7183 |
*/ |
| 7184 |
ea = ea.expressionSemantic(sc); |
| 7185 |
Expression e = new TypeidExp(ea.loc, ea); |
| 7186 |
e.type = Type.typeinfoclass.type; |
| 7187 |
result = e; |
| 7188 |
return; |
| 7189 |
} |
| 7190 |
} |
| 7191 |
else if (ta.ty == Terror) |
| 7192 |
{ |
| 7193 |
return setError(); |
| 7194 |
} |
| 7195 |
|
| 7196 |
// Handle this in the glue layer |
| 7197 |
Expression e = new TypeidExp(exp.loc, ta); |
| 7198 |
|
| 7199 |
e.type = getTypeInfoType(exp.loc, ta, sc); |
| 7200 |
semanticTypeInfo(sc, ta); |
| 7201 |
|
| 7202 |
if (ea) |
| 7203 |
{ |
| 7204 |
e = new CommaExp(exp.loc, ea, e); // execute ea |
| 7205 |
e = e.expressionSemantic(sc); |
| 7206 |
} |
| 7207 |
result = e; |
| 7208 |
} |
| 7209 |
|
| 7210 |
override void visit(TraitsExp e) |
| 7211 |
{ |
| 7212 |
result = semanticTraits(e, sc); |
| 7213 |
} |
| 7214 |
|
| 7215 |
override void visit(HaltExp e) |
| 7216 |
{ |
| 7217 |
static if (LOGSEMANTIC) |
| 7218 |
{ |
| 7219 |
printf("HaltExp::semantic()\n"); |
| 7220 |
} |
| 7221 |
e.type = Type.tnoreturn; |
| 7222 |
result = e; |
| 7223 |
} |
| 7224 |
|
| 7225 |
override void visit(IsExp e) |
| 7226 |
{ |
| 7227 |
/* is(targ id tok tspec) |
| 7228 |
* is(targ id : tok2) |
| 7229 |
* is(targ id == tok2) |
| 7230 |
*/ |
| 7231 |
Type tded = null; |
| 7232 |
|
| 7233 |
void yes() |
| 7234 |
{ |
| 7235 |
//printf("yes\n"); |
| 7236 |
if (!e.id) |
| 7237 |
{ |
| 7238 |
result = IntegerExp.createBool(true); |
| 7239 |
return; |
| 7240 |
} |
| 7241 |
|
| 7242 |
Dsymbol s; |
| 7243 |
Tuple tup = isTuple(tded); |
| 7244 |
if (tup) |
| 7245 |
s = new TupleDeclaration(e.loc, e.id, &tup.objects); |
| 7246 |
else |
| 7247 |
s = new AliasDeclaration(e.loc, e.id, tded); |
| 7248 |
s.dsymbolSemantic(sc); |
| 7249 |
|
| 7250 |
/* The reason for the !tup is unclear. It fails Phobos unittests if it is not there. |
| 7251 |
* More investigation is needed. |
| 7252 |
*/ |
| 7253 |
if (!tup && !sc.insert(s)) |
| 7254 |
{ |
| 7255 |
Dsymbol pscopesym; |
| 7256 |
auto conflict = sc.search(Loc.initial, s.ident, pscopesym); |
| 7257 |
error(e.loc, "declaration `%s` is already defined", s.toPrettyChars()); |
| 7258 |
errorSupplemental(conflict.loc, "`%s` `%s` is defined here", |
| 7259 |
conflict.kind(), conflict.toChars()); |
| 7260 |
} |
| 7261 |
|
| 7262 |
unSpeculative(sc, s); |
| 7263 |
|
| 7264 |
result = IntegerExp.createBool(true); |
| 7265 |
} |
| 7266 |
void no() |
| 7267 |
{ |
| 7268 |
result = IntegerExp.createBool(false); |
| 7269 |
//printf("no\n"); |
| 7270 |
} |
| 7271 |
|
| 7272 |
static if (LOGSEMANTIC) |
| 7273 |
{ |
| 7274 |
printf("IsExp::semantic(%s)\n", e.toChars()); |
| 7275 |
} |
| 7276 |
if (e.id && !sc.condition) |
| 7277 |
{ |
| 7278 |
error(e.loc, "can only declare type aliases within `static if` conditionals or `static assert`s"); |
| 7279 |
return setError(); |
| 7280 |
} |
| 7281 |
|
| 7282 |
if (e.tok2 == TOK.package_ || e.tok2 == TOK.module_) // These is() expressions are special because they can work on modules, not just types. |
| 7283 |
{ |
| 7284 |
const oldErrors = global.startGagging(); |
| 7285 |
Dsymbol sym = e.targ.toDsymbol(sc); |
| 7286 |
global.endGagging(oldErrors); |
| 7287 |
|
| 7288 |
if (sym is null) |
| 7289 |
return no(); |
| 7290 |
Package p = resolveIsPackage(sym); |
| 7291 |
if (p is null) |
| 7292 |
return no(); |
| 7293 |
if (e.tok2 == TOK.package_ && p.isModule()) // Note that isModule() will return null for package modules because they're not actually instances of Module. |
| 7294 |
return no(); |
| 7295 |
if (e.tok2 == TOK.module_ && !(p.isModule() || p.isPackageMod())) |
| 7296 |
return no(); |
| 7297 |
tded = e.targ; |
| 7298 |
return yes(); |
| 7299 |
} |
| 7300 |
|
| 7301 |
{ |
| 7302 |
Scope* sc2 = sc.copy(); // keep sc.flags |
| 7303 |
sc2.tinst = null; |
| 7304 |
sc2.minst = null; |
| 7305 |
sc2.fullinst = true; |
| 7306 |
Type t = dmd.typesem.trySemantic(e.targ, e.loc, sc2); |
| 7307 |
sc2.pop(); |
| 7308 |
if (!t) // errors, so condition is false |
| 7309 |
return no(); |
| 7310 |
e.targ = t; |
| 7311 |
} |
| 7312 |
|
| 7313 |
if (e.tok2 != TOK.reserved) |
| 7314 |
{ |
| 7315 |
switch (e.tok2) |
| 7316 |
{ |
| 7317 |
case TOK.struct_: |
| 7318 |
if (e.targ.ty != Tstruct) |
| 7319 |
return no(); |
| 7320 |
if ((cast(TypeStruct)e.targ).sym.isUnionDeclaration()) |
| 7321 |
return no(); |
| 7322 |
tded = e.targ; |
| 7323 |
break; |
| 7324 |
|
| 7325 |
case TOK.union_: |
| 7326 |
if (e.targ.ty != Tstruct) |
| 7327 |
return no(); |
| 7328 |
if (!(cast(TypeStruct)e.targ).sym.isUnionDeclaration()) |
| 7329 |
return no(); |
| 7330 |
tded = e.targ; |
| 7331 |
break; |
| 7332 |
|
| 7333 |
case TOK.class_: |
| 7334 |
if (e.targ.ty != Tclass) |
| 7335 |
return no(); |
| 7336 |
if ((cast(TypeClass)e.targ).sym.isInterfaceDeclaration()) |
| 7337 |
return no(); |
| 7338 |
tded = e.targ; |
| 7339 |
break; |
| 7340 |
|
| 7341 |
case TOK.interface_: |
| 7342 |
if (e.targ.ty != Tclass) |
| 7343 |
return no(); |
| 7344 |
if (!(cast(TypeClass)e.targ).sym.isInterfaceDeclaration()) |
| 7345 |
return no(); |
| 7346 |
tded = e.targ; |
| 7347 |
break; |
| 7348 |
|
| 7349 |
case TOK.const_: |
| 7350 |
if (!e.targ.isConst()) |
| 7351 |
return no(); |
| 7352 |
tded = e.targ; |
| 7353 |
break; |
| 7354 |
|
| 7355 |
case TOK.immutable_: |
| 7356 |
if (!e.targ.isImmutable()) |
| 7357 |
return no(); |
| 7358 |
tded = e.targ; |
| 7359 |
break; |
| 7360 |
|
| 7361 |
case TOK.shared_: |
| 7362 |
if (!e.targ.isShared()) |
| 7363 |
return no(); |
| 7364 |
tded = e.targ; |
| 7365 |
break; |
| 7366 |
|
| 7367 |
case TOK.inout_: |
| 7368 |
if (!e.targ.isWild()) |
| 7369 |
return no(); |
| 7370 |
tded = e.targ; |
| 7371 |
break; |
| 7372 |
|
| 7373 |
case TOK.super_: |
| 7374 |
// If class or interface, get the base class and interfaces |
| 7375 |
if (e.targ.ty != Tclass) |
| 7376 |
return no(); |
| 7377 |
else |
| 7378 |
{ |
| 7379 |
ClassDeclaration cd = (cast(TypeClass)e.targ).sym; |
| 7380 |
auto args = new Parameters(); |
| 7381 |
args.reserve(cd.baseclasses.length); |
| 7382 |
if (cd.semanticRun < PASS.semanticdone) |
| 7383 |
cd.dsymbolSemantic(null); |
| 7384 |
for (size_t i = 0; i < cd.baseclasses.length; i++) |
| 7385 |
{ |
| 7386 |
BaseClass* b = (*cd.baseclasses)[i]; |
| 7387 |
args.push(new Parameter(Loc.initial, STC.in_, b.type, null, null, null)); |
| 7388 |
} |
| 7389 |
tded = new TypeTuple(args); |
| 7390 |
} |
| 7391 |
break; |
| 7392 |
|
| 7393 |
case TOK.enum_: |
| 7394 |
if (e.targ.ty != Tenum) |
| 7395 |
return no(); |
| 7396 |
if (e.id) |
| 7397 |
tded = (cast(TypeEnum)e.targ).sym.getMemtype(e.loc); |
| 7398 |
else |
| 7399 |
tded = e.targ; |
| 7400 |
|
| 7401 |
if (tded.ty == Terror) |
| 7402 |
return setError(); |
| 7403 |
break; |
| 7404 |
|
| 7405 |
case TOK.delegate_: |
| 7406 |
if (e.targ.ty != Tdelegate) |
| 7407 |
return no(); |
| 7408 |
tded = (cast(TypeDelegate)e.targ).next; // the underlying function type |
| 7409 |
break; |
| 7410 |
|
| 7411 |
case TOK.function_: |
| 7412 |
if (e.targ.ty != Tfunction) |
| 7413 |
return no(); |
| 7414 |
goto case; |
| 7415 |
case TOK.parameters: |
| 7416 |
{ |
| 7417 |
if (auto tf = e.targ.isFunction_Delegate_PtrToFunction()) |
| 7418 |
tded = tf; |
| 7419 |
else |
| 7420 |
return no(); |
| 7421 |
|
| 7422 |
/* Generate tuple from function parameter types. |
| 7423 |
*/ |
| 7424 |
auto args = new Parameters(); |
| 7425 |
foreach (i, arg; tded.isTypeFunction().parameterList) |
| 7426 |
{ |
| 7427 |
assert(arg && arg.type); |
| 7428 |
/* If one of the default arguments was an error, |
| 7429 |
don't return an invalid tuple |
| 7430 |
*/ |
| 7431 |
if (e.tok2 == TOK.parameters && arg.defaultArg && arg.defaultArg.op == EXP.error) |
| 7432 |
return setError(); |
| 7433 |
args.push(new Parameter(arg.loc, arg.storageClass, arg.type, (e.tok2 == TOK.parameters) ? arg.ident : null, (e.tok2 == TOK.parameters) ? arg.defaultArg : null, arg.userAttribDecl)); |
| 7434 |
} |
| 7435 |
tded = new TypeTuple(args); |
| 7436 |
break; |
| 7437 |
} |
| 7438 |
case TOK.return_: |
| 7439 |
/* Get the 'return type' for the function, |
| 7440 |
* delegate, or pointer to function. |
| 7441 |
*/ |
| 7442 |
if (auto tf = e.targ.isFunction_Delegate_PtrToFunction()) |
| 7443 |
tded = tf.next; |
| 7444 |
else |
| 7445 |
return no(); |
| 7446 |
break; |
| 7447 |
|
| 7448 |
case TOK.argumentTypes: |
| 7449 |
/* Generate a type tuple of the equivalent types used to determine if a |
| 7450 |
* function argument of this type can be passed in registers. |
| 7451 |
* The results of this are highly platform dependent, and intended |
| 7452 |
* primarly for use in implementing va_arg(). |
| 7453 |
*/ |
| 7454 |
tded = target.toArgTypes(e.targ); |
| 7455 |
if (!tded) |
| 7456 |
return no(); |
| 7457 |
// not valid for a parameter |
| 7458 |
break; |
| 7459 |
|
| 7460 |
case TOK.vector: |
| 7461 |
if (e.targ.ty != Tvector) |
| 7462 |
return no(); |
| 7463 |
tded = (cast(TypeVector)e.targ).basetype; |
| 7464 |
break; |
| 7465 |
|
| 7466 |
default: |
| 7467 |
assert(0); |
| 7468 |
} |
| 7469 |
|
| 7470 |
// https://issues.dlang.org/show_bug.cgi?id=18753 |
| 7471 |
if (tded) |
| 7472 |
return yes(); |
| 7473 |
return no(); |
| 7474 |
} |
| 7475 |
else if (e.tspec && !e.id && !(e.parameters && e.parameters.length)) |
| 7476 |
{ |
| 7477 |
/* Evaluate to true if targ matches tspec |
| 7478 |
* is(targ == tspec) |
| 7479 |
* is(targ : tspec) |
| 7480 |
*/ |
| 7481 |
e.tspec = e.tspec.typeSemantic(e.loc, sc); |
| 7482 |
//printf("targ = %s, %s\n", e.targ.toChars(), e.targ.deco); |
| 7483 |
//printf("tspec = %s, %s\n", e.tspec.toChars(), e.tspec.deco); |
| 7484 |
|
| 7485 |
if (e.tok != TOK.colon) /* == */ |
| 7486 |
{ |
| 7487 |
if (e.targ.equals(e.tspec)) |
| 7488 |
return yes(); |
| 7489 |
else |
| 7490 |
return no(); |
| 7491 |
} |
| 7492 |
|
| 7493 |
// current scope is itself deprecated, or deprecations are not errors |
| 7494 |
const bool deprecationAllowed = sc.isDeprecated |
| 7495 |
|| global.params.useDeprecated != DiagnosticReporting.error; |
| 7496 |
const bool preventAliasThis = e.targ.hasDeprecatedAliasThis && !deprecationAllowed; |
| 7497 |
|
| 7498 |
if (preventAliasThis && e.targ.ty == Tstruct) |
| 7499 |
{ |
| 7500 |
if ((cast(TypeStruct) e.targ).implicitConvToWithoutAliasThis(e.tspec)) |
| 7501 |
return yes(); |
| 7502 |
else |
| 7503 |
return no(); |
| 7504 |
} |
| 7505 |
else if (preventAliasThis && e.targ.ty == Tclass) |
| 7506 |
{ |
| 7507 |
if ((cast(TypeClass) e.targ).implicitConvToWithoutAliasThis(e.tspec)) |
| 7508 |
return yes(); |
| 7509 |
else |
| 7510 |
return no(); |
| 7511 |
} |
| 7512 |
else if (e.targ.implicitConvTo(e.tspec)) |
| 7513 |
return yes(); |
| 7514 |
else |
| 7515 |
return no(); |
| 7516 |
} |
| 7517 |
else if (e.tspec) |
| 7518 |
{ |
| 7519 |
/* Evaluate to true if targ matches tspec. |
| 7520 |
* If true, declare id as an alias for the specialized type. |
| 7521 |
* is(targ == tspec, tpl) |
| 7522 |
* is(targ : tspec, tpl) |
| 7523 |
* is(targ id == tspec) |
| 7524 |
* is(targ id : tspec) |
| 7525 |
* is(targ id == tspec, tpl) |
| 7526 |
* is(targ id : tspec, tpl) |
| 7527 |
*/ |
| 7528 |
Identifier tid = e.id ? e.id : Identifier.generateId("__isexp_id"); |
| 7529 |
e.parameters.insert(0, new TemplateTypeParameter(e.loc, tid, null, null)); |
| 7530 |
|
| 7531 |
Objects dedtypes = Objects(e.parameters.length); |
| 7532 |
dedtypes.zero(); |
| 7533 |
|
| 7534 |
MATCH m = deduceType(e.targ, sc, e.tspec, *e.parameters, dedtypes, null, 0, e.tok == TOK.equal); |
| 7535 |
|
| 7536 |
if (m == MATCH.nomatch || (m != MATCH.exact && e.tok == TOK.equal)) |
| 7537 |
return no(); |
| 7538 |
|
| 7539 |
tded = cast(Type)dedtypes[0]; |
| 7540 |
if (!tded) |
| 7541 |
tded = e.targ; |
| 7542 |
Objects tiargs = Objects(1); |
| 7543 |
tiargs[0] = e.targ; |
| 7544 |
|
| 7545 |
/* Declare trailing parameters |
| 7546 |
*/ |
| 7547 |
for (size_t i = 1; i < e.parameters.length; i++) |
| 7548 |
{ |
| 7549 |
TemplateParameter tp = (*e.parameters)[i]; |
| 7550 |
Declaration s = null; |
| 7551 |
|
| 7552 |
m = tp.matchArg(e.loc, sc, &tiargs, i, e.parameters, dedtypes, &s); |
| 7553 |
if (m == MATCH.nomatch) |
| 7554 |
return no(); |
| 7555 |
s.dsymbolSemantic(sc); |
| 7556 |
if (!sc.insert(s)) |
| 7557 |
{ |
| 7558 |
Dsymbol pscopesym; |
| 7559 |
auto conflict = sc.search(Loc.initial, s.ident, pscopesym); |
| 7560 |
error(e.loc, "declaration `%s` is already defined", s.toPrettyChars()); |
| 7561 |
errorSupplemental(conflict.loc, "`%s` `%s` is defined here", |
| 7562 |
conflict.kind(), conflict.toChars()); |
| 7563 |
} |
| 7564 |
|
| 7565 |
unSpeculative(sc, s); |
| 7566 |
} |
| 7567 |
return yes(); |
| 7568 |
} |
| 7569 |
else if (e.id) |
| 7570 |
{ |
| 7571 |
/* Declare id as an alias for type targ. Evaluate to true |
| 7572 |
* is(targ id) |
| 7573 |
*/ |
| 7574 |
tded = e.targ; |
| 7575 |
} |
| 7576 |
return yes(); |
| 7577 |
} |
| 7578 |
|
| 7579 |
override void visit(BinAssignExp exp) |
| 7580 |
{ |
| 7581 |
|
| 7582 |
if (Expression e = exp.opOverloadBinaryAssign(sc, aliasThisStop)) |
| 7583 |
{ |
| 7584 |
result = e; |
| 7585 |
return; |
| 7586 |
} |
| 7587 |
|
| 7588 |
Expression e; |
| 7589 |
if (exp.e1.op == EXP.arrayLength) |
| 7590 |
{ |
| 7591 |
// arr.length op= e2; |
| 7592 |
e = rewriteOpAssign(exp); |
| 7593 |
e = e.expressionSemantic(sc); |
| 7594 |
result = e; |
| 7595 |
return; |
| 7596 |
} |
| 7597 |
if (exp.e1.op == EXP.slice || exp.e1.type.isStaticOrDynamicArray()) |
| 7598 |
{ |
| 7599 |
if (checkNonAssignmentArrayOp(exp.e1)) |
| 7600 |
return setError(); |
| 7601 |
|
| 7602 |
if (exp.e1.op == EXP.slice) |
| 7603 |
(cast(SliceExp)exp.e1).arrayop = true; |
| 7604 |
|
| 7605 |
// T[] op= ... |
| 7606 |
if (exp.e2.implicitConvTo(exp.e1.type.nextOf())) |
| 7607 |
{ |
| 7608 |
// T[] op= T |
| 7609 |
exp.e2 = exp.e2.castTo(sc, exp.e1.type.nextOf()); |
| 7610 |
} |
| 7611 |
else if (Expression ex = typeCombine(exp, sc)) |
| 7612 |
{ |
| 7613 |
result = ex; |
| 7614 |
return; |
| 7615 |
} |
| 7616 |
exp.type = exp.e1.type; |
| 7617 |
result = arrayOp(exp, sc); |
| 7618 |
return; |
| 7619 |
} |
| 7620 |
|
| 7621 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 7622 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 7623 |
exp.e1 = exp.e1.optimize(WANTvalue, /*keepLvalue*/ true); |
| 7624 |
exp.type = exp.e1.type; |
| 7625 |
|
| 7626 |
if (exp.suggestOpOpAssign(sc, parent) || |
| 7627 |
exp.e1.checkScalar() || |
| 7628 |
exp.e1.checkReadModifyWrite(exp.op, exp.e2) || |
| 7629 |
exp.e1.checkSharedAccess(sc)) |
| 7630 |
return setError(); |
| 7631 |
|
| 7632 |
int arith = (exp.op == EXP.addAssign || exp.op == EXP.minAssign || exp.op == EXP.mulAssign || exp.op == EXP.divAssign || exp.op == EXP.modAssign || exp.op == EXP.powAssign); |
| 7633 |
int bitwise = (exp.op == EXP.andAssign || exp.op == EXP.orAssign || exp.op == EXP.xorAssign); |
| 7634 |
int shift = (exp.op == EXP.leftShiftAssign || exp.op == EXP.rightShiftAssign || exp.op == EXP.unsignedRightShiftAssign); |
| 7635 |
|
| 7636 |
if (bitwise && exp.type.toBasetype().ty == Tbool) |
| 7637 |
exp.e2 = exp.e2.implicitCastTo(sc, exp.type); |
| 7638 |
else if (exp.checkNoBool()) |
| 7639 |
return setError(); |
| 7640 |
|
| 7641 |
if ((exp.op == EXP.addAssign || exp.op == EXP.minAssign) && exp.e1.type.toBasetype().ty == Tpointer && exp.e2.type.toBasetype().isIntegral()) |
| 7642 |
{ |
| 7643 |
result = scaleFactor(exp, sc); |
| 7644 |
return; |
| 7645 |
} |
| 7646 |
|
| 7647 |
if (Expression ex = typeCombine(exp, sc)) |
| 7648 |
{ |
| 7649 |
result = ex; |
| 7650 |
return; |
| 7651 |
} |
| 7652 |
|
| 7653 |
if (arith && (exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc))) |
| 7654 |
return setError(); |
| 7655 |
if ((bitwise || shift) && (exp.checkIntegralBin() || exp.checkSharedAccessBin(sc))) |
| 7656 |
return setError(); |
| 7657 |
|
| 7658 |
if (shift) |
| 7659 |
{ |
| 7660 |
if (exp.e2.type.toBasetype().ty != Tvector) |
| 7661 |
exp.e2 = exp.e2.castTo(sc, Type.tshiftcnt); |
| 7662 |
} |
| 7663 |
|
| 7664 |
if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 7665 |
{ |
| 7666 |
result = exp.incompatibleTypes(); |
| 7667 |
return; |
| 7668 |
} |
| 7669 |
|
| 7670 |
if (exp.e1.op == EXP.error || exp.e2.op == EXP.error) |
| 7671 |
return setError(); |
| 7672 |
|
| 7673 |
e = exp.checkOpAssignTypes(sc); |
| 7674 |
if (e.op == EXP.error) |
| 7675 |
{ |
| 7676 |
result = e; |
| 7677 |
return; |
| 7678 |
} |
| 7679 |
|
| 7680 |
assert(e.op == EXP.assign || e == exp); |
| 7681 |
result = (cast(BinExp)e).reorderSettingAAElem(sc); |
| 7682 |
} |
| 7683 |
|
| 7684 |
private Expression compileIt(MixinExp exp, Scope* sc) |
| 7685 |
{ |
| 7686 |
OutBuffer buf; |
| 7687 |
if (expressionsToString(buf, sc, exp.exps, exp.loc, null, true)) |
| 7688 |
return null; |
| 7689 |
|
| 7690 |
const errors = global.errors; |
| 7691 |
const len = buf.length; |
| 7692 |
const str = buf.extractChars()[0 .. len]; |
| 7693 |
const bool doUnittests = global.params.parsingUnittestsRequired(); |
| 7694 |
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests); |
| 7695 |
adjustLocForMixin(str, exp.loc, *p.baseLoc, global.params.mixinOut); |
| 7696 |
p.linnum = p.baseLoc.startLine; |
| 7697 |
p.nextToken(); |
| 7698 |
//printf("p.loc.linnum = %d\n", p.loc.linnum); |
| 7699 |
|
| 7700 |
Expression e = p.parseExpression(); |
| 7701 |
if (global.errors != errors) |
| 7702 |
return null; |
| 7703 |
|
| 7704 |
if (p.token.value != TOK.endOfFile) |
| 7705 |
{ |
| 7706 |
error(e.loc, "unexpected token `%s` after %s expression", |
| 7707 |
p.token.toChars(), EXPtoString(e.op).ptr); |
| 7708 |
errorSupplemental(e.loc, "while parsing string mixin expression `%s`", |
| 7709 |
str.ptr); |
| 7710 |
return null; |
| 7711 |
} |
| 7712 |
return e; |
| 7713 |
} |
| 7714 |
|
| 7715 |
override void visit(MixinExp exp) |
| 7716 |
{ |
| 7717 |
/* https://dlang.org/spec/expression.html#mixin_expressions |
| 7718 |
*/ |
| 7719 |
|
| 7720 |
static if (LOGSEMANTIC) |
| 7721 |
{ |
| 7722 |
printf("MixinExp::semantic('%s')\n", exp.toChars()); |
| 7723 |
} |
| 7724 |
|
| 7725 |
// The expression is not treated as part of a default argument, |
| 7726 |
// because it is evaluated at compile time. |
| 7727 |
Scope* sc2 = sc.push(); |
| 7728 |
sc2.inDefaultArg = false; |
| 7729 |
|
| 7730 |
auto e = compileIt(exp, sc2); |
| 7731 |
sc2.pop(); |
| 7732 |
if (!e) |
| 7733 |
return setError(); |
| 7734 |
result = e.expressionSemantic(sc); |
| 7735 |
} |
| 7736 |
|
| 7737 |
override void visit(ImportExp e) |
| 7738 |
{ |
| 7739 |
static if (LOGSEMANTIC) |
| 7740 |
{ |
| 7741 |
printf("ImportExp::semantic('%s')\n", e.toChars()); |
| 7742 |
} |
| 7743 |
|
| 7744 |
auto se = semanticString(sc, e.e1, "file name argument"); |
| 7745 |
if (!se) |
| 7746 |
return setError(); |
| 7747 |
se = se.toUTF8(sc); |
| 7748 |
|
| 7749 |
auto namez = se.toStringz(); |
| 7750 |
if (!global.filePath.length) |
| 7751 |
{ |
| 7752 |
error(e.loc, "need `-J` switch to import text file `%s`", namez.ptr); |
| 7753 |
return setError(); |
| 7754 |
} |
| 7755 |
|
| 7756 |
/* Be wary of CWE-22: Improper Limitation of a Pathname to a Restricted Directory |
| 7757 |
* ('Path Traversal') attacks. |
| 7758 |
* https://cwe.mitre.org/data/definitions/22.html |
| 7759 |
*/ |
| 7760 |
|
| 7761 |
if (FileName.absolute(namez)) |
| 7762 |
{ |
| 7763 |
error(e.loc, "absolute path is not allowed in import expression: `%s`", se.toChars()); |
| 7764 |
return setError(); |
| 7765 |
} |
| 7766 |
|
| 7767 |
auto idxReserved = FileName.findReservedChar(namez); |
| 7768 |
if (idxReserved != size_t.max) |
| 7769 |
{ |
| 7770 |
error(e.loc, "`%s` is not a valid filename on this platform", se.toChars()); |
| 7771 |
errorSupplemental(e.loc, "Character `'%c'` is reserved and cannot be used", namez[idxReserved]); |
| 7772 |
return setError(); |
| 7773 |
} |
| 7774 |
|
| 7775 |
if (FileName.refersToParentDir(namez)) |
| 7776 |
{ |
| 7777 |
error(e.loc, "path refers to parent (`..`) directory: `%s`", se.toChars()); |
| 7778 |
return setError(); |
| 7779 |
} |
| 7780 |
|
| 7781 |
auto resolvedNamez = FileName.searchPath(global.filePath[], namez, false); |
| 7782 |
if (!resolvedNamez) |
| 7783 |
{ |
| 7784 |
error(e.loc, "file `%s` cannot be found or not in a path specified with `-J`", se.toChars()); |
| 7785 |
errorSupplemental(e.loc, "Path(s) searched (as provided by `-J`):"); |
| 7786 |
foreach (idx, path; global.filePath[]) |
| 7787 |
{ |
| 7788 |
const attr = FileName.exists(path); |
| 7789 |
const(char)* err = attr == 2 ? "" : |
| 7790 |
(attr == 1 ? " (not a directory)" : " (path not found)"); |
| 7791 |
errorSupplemental(e.loc, "[%llu]: `%s`%s", cast(ulong)idx, path, err); |
| 7792 |
} |
| 7793 |
return setError(); |
| 7794 |
} |
| 7795 |
|
| 7796 |
sc._module.contentImportedFiles.push(resolvedNamez.ptr); |
| 7797 |
if (global.params.v.verbose) |
| 7798 |
{ |
| 7799 |
const slice = se.peekString(); |
| 7800 |
message("file %.*s\t(%s)", cast(int)slice.length, slice.ptr, resolvedNamez.ptr); |
| 7801 |
} |
| 7802 |
|
| 7803 |
addImportExpDep(global.params.moduleDeps, global.params.makeDeps, resolvedNamez, se.peekString(), sc._module); |
| 7804 |
|
| 7805 |
{ |
| 7806 |
auto fileName = FileName(resolvedNamez); |
| 7807 |
if (auto fmResult = global.fileManager.getFileContents(fileName)) |
| 7808 |
{ |
| 7809 |
se = new StringExp(e.loc, fmResult); |
| 7810 |
se.hexString = true; |
| 7811 |
} |
| 7812 |
else |
| 7813 |
{ |
| 7814 |
error(e.loc, "cannot read file `%s`", resolvedNamez.ptr); |
| 7815 |
return setError(); |
| 7816 |
} |
| 7817 |
} |
| 7818 |
result = se.expressionSemantic(sc); |
| 7819 |
} |
| 7820 |
|
| 7821 |
override void visit(AssertExp exp) |
| 7822 |
{ |
| 7823 |
// https://dlang.org/spec/expression.html#assert_expressions |
| 7824 |
static if (LOGSEMANTIC) |
| 7825 |
{ |
| 7826 |
printf("AssertExp::semantic('%s')\n", exp.toChars()); |
| 7827 |
} |
| 7828 |
if (auto e = exp.e1.isStringExp()) |
| 7829 |
{ |
| 7830 |
// deprecated in 2.107 |
| 7831 |
deprecation(e.loc, "assert condition cannot be a string literal"); |
| 7832 |
deprecationSupplemental(e.loc, "If intentional, use `%s !is null` instead to preserve behaviour", |
| 7833 |
e.toErrMsg()); |
| 7834 |
} |
| 7835 |
|
| 7836 |
const generateMsg = !exp.msg && |
| 7837 |
sc.needsCodegen() && // let ctfe interpreter handle the error message |
| 7838 |
global.params.checkAction == CHECKACTION.context && |
| 7839 |
global.params.useAssert == CHECKENABLE.on && |
| 7840 |
!((exp.e1.isIntegerExp() && (exp.e1.toInteger() == 0)) || |
| 7841 |
exp.e1.isNullExp()); |
| 7842 |
Expression temporariesPrefix; |
| 7843 |
|
| 7844 |
if (generateMsg) |
| 7845 |
// no message - use assert expression as msg |
| 7846 |
{ |
| 7847 |
if (!verifyHookExist(exp.loc, *sc, Id._d_assert_fail, "generating assert messages")) |
| 7848 |
return setError(); |
| 7849 |
|
| 7850 |
/* |
| 7851 |
{ |
| 7852 |
auto a = e1, b = e2; |
| 7853 |
assert(a == b, _d_assert_fail!"=="(a, b)); |
| 7854 |
}() |
| 7855 |
*/ |
| 7856 |
|
| 7857 |
/* |
| 7858 |
Stores the result of an operand expression into a temporary |
| 7859 |
if necessary, e.g. if it is an impure fuction call containing side |
| 7860 |
effects as in https://issues.dlang.org/show_bug.cgi?id=20114 |
| 7861 |
|
| 7862 |
Params: |
| 7863 |
op = an expression which may require a temporary (added to |
| 7864 |
`temporariesPrefix`: `auto tmp = op`) and will be replaced |
| 7865 |
by `tmp` if necessary |
| 7866 |
|
| 7867 |
Returns: (possibly replaced) `op` |
| 7868 |
*/ |
| 7869 |
Expression maybePromoteToTmp(ref Expression op) |
| 7870 |
{ |
| 7871 |
// https://issues.dlang.org/show_bug.cgi?id=20989 |
| 7872 |
// Flag that _d_assert_fail will never dereference `array.ptr` to avoid safety |
| 7873 |
// errors for `assert(!array.ptr)` => `_d_assert_fail!"!"(array.ptr)` |
| 7874 |
{ |
| 7875 |
auto die = op.isDotIdExp(); |
| 7876 |
if (die && die.ident == Id.ptr) |
| 7877 |
die.noderef = true; |
| 7878 |
} |
| 7879 |
|
| 7880 |
op = op.expressionSemantic(sc); |
| 7881 |
op = resolveProperties(sc, op); |
| 7882 |
|
| 7883 |
// Detect assert's using static operator overloads (e.g. `"var" in environment`) |
| 7884 |
if (auto te = op.isTypeExp()) |
| 7885 |
{ |
| 7886 |
// Replace the TypeExp with it's textual representation |
| 7887 |
// Including "..." in the error message isn't quite right but |
| 7888 |
// proper solutions require more drastic changes, e.g. directly |
| 7889 |
// using miniFormat and combine instead of calling _d_assert_fail |
| 7890 |
auto name = new StringExp(te.loc, te.toString()); |
| 7891 |
return name.expressionSemantic(sc); |
| 7892 |
} |
| 7893 |
|
| 7894 |
// Create a temporary for expressions with side effects |
| 7895 |
// Defensively assume that function calls may have side effects even |
| 7896 |
// though it's not detected by hasSideEffect (e.g. `debug puts("Hello")` ) |
| 7897 |
// Rewriting CallExp's also avoids some issues with the inliner/debug generation |
| 7898 |
if (op.hasSideEffect(true)) |
| 7899 |
{ |
| 7900 |
// Don't create an invalid temporary for void-expressions |
| 7901 |
// Further semantic will issue an appropriate error |
| 7902 |
if (op.type.ty == Tvoid) |
| 7903 |
return op; |
| 7904 |
|
| 7905 |
// https://issues.dlang.org/show_bug.cgi?id=21590 |
| 7906 |
// Don't create unnecessary temporaries and detect `assert(a = b)` |
| 7907 |
if (op.isAssignExp() || op.isBinAssignExp()) |
| 7908 |
{ |
| 7909 |
auto left = (cast(BinExp) op).e1; |
| 7910 |
|
| 7911 |
// Find leftmost expression to handle other rewrites, |
| 7912 |
// e.g. --(++a) => a += 1 -= 1 |
| 7913 |
while (left.isAssignExp() || left.isBinAssignExp()) |
| 7914 |
left = (cast(BinExp) left).e1; |
| 7915 |
|
| 7916 |
// Only use the assignee if it's a variable and skip |
| 7917 |
// other lvalues (e.g. ref's returned by functions) |
| 7918 |
if (left.isVarExp()) |
| 7919 |
return left; |
| 7920 |
|
| 7921 |
// Sanity check that `op` can be converted to boolean |
| 7922 |
// But don't raise errors for assignments enclosed in another expression |
| 7923 |
if (op is exp.e1) |
| 7924 |
op.toBoolean(sc); |
| 7925 |
} |
| 7926 |
|
| 7927 |
// Tuples with side-effects already receive a temporary during semantic |
| 7928 |
if (op.type.isTypeTuple()) |
| 7929 |
{ |
| 7930 |
auto te = op.isTupleExp(); |
| 7931 |
assert(te); |
| 7932 |
|
| 7933 |
// Create a new tuple without the associated temporary |
| 7934 |
auto res = new TupleExp(op.loc, te.exps); |
| 7935 |
return res.expressionSemantic(sc); |
| 7936 |
} |
| 7937 |
|
| 7938 |
const stc = op.isLvalue() ? STC.ref_ : STC.none; |
| 7939 |
auto tmp = copyToTemp(stc, "__assertOp", op); |
| 7940 |
tmp.dsymbolSemantic(sc); |
| 7941 |
|
| 7942 |
auto decl = new DeclarationExp(op.loc, tmp); |
| 7943 |
temporariesPrefix = Expression.combine(temporariesPrefix, decl); |
| 7944 |
|
| 7945 |
op = new VarExp(op.loc, tmp); |
| 7946 |
op = op.expressionSemantic(sc); |
| 7947 |
} |
| 7948 |
return op; |
| 7949 |
} |
| 7950 |
|
| 7951 |
// if the assert condition is a mixin expression, try to compile it |
| 7952 |
if (auto ce = exp.e1.isMixinExp()) |
| 7953 |
{ |
| 7954 |
if (auto e1 = compileIt(ce, sc)) |
| 7955 |
exp.e1 = e1; |
| 7956 |
} |
| 7957 |
|
| 7958 |
Expressions* es; |
| 7959 |
Objects* tiargs; |
| 7960 |
Loc loc = exp.e1.loc; |
| 7961 |
|
| 7962 |
const op = exp.e1.op; |
| 7963 |
bool isEqualsCallExpression; |
| 7964 |
if (const callExp = exp.e1.isCallExp()) |
| 7965 |
{ |
| 7966 |
// https://issues.dlang.org/show_bug.cgi?id=20331 |
| 7967 |
// callExp.f may be null if the assert contains a call to |
| 7968 |
// a function pointer or literal |
| 7969 |
if (const callExpFunc = callExp.f) |
| 7970 |
{ |
| 7971 |
const callExpIdent = callExpFunc.ident; |
| 7972 |
isEqualsCallExpression = callExpIdent == Id.__equals || |
| 7973 |
callExpIdent == Id.opEquals; |
| 7974 |
} |
| 7975 |
} |
| 7976 |
if (op == EXP.equal || op == EXP.notEqual || |
| 7977 |
op == EXP.lessThan || op == EXP.greaterThan || |
| 7978 |
op == EXP.lessOrEqual || op == EXP.greaterOrEqual || |
| 7979 |
op == EXP.identity || op == EXP.notIdentity || |
| 7980 |
op == EXP.in_ || |
| 7981 |
isEqualsCallExpression) |
| 7982 |
{ |
| 7983 |
es = new Expressions(3); |
| 7984 |
tiargs = new Objects(1); |
| 7985 |
|
| 7986 |
if (isEqualsCallExpression) |
| 7987 |
{ |
| 7988 |
auto callExp = cast(CallExp) exp.e1; |
| 7989 |
auto args = callExp.arguments; |
| 7990 |
|
| 7991 |
// structs with opEquals get rewritten to a DotVarExp: |
| 7992 |
// a.opEquals(b) |
| 7993 |
// https://issues.dlang.org/show_bug.cgi?id=20100 |
| 7994 |
if (args.length == 1) |
| 7995 |
{ |
| 7996 |
auto dv = callExp.e1.isDotVarExp(); |
| 7997 |
assert(dv); |
| 7998 |
|
| 7999 |
// runtime args |
| 8000 |
(*es)[1] = maybePromoteToTmp(dv.e1); |
| 8001 |
(*es)[2] = maybePromoteToTmp((*args)[0]); |
| 8002 |
} |
| 8003 |
else |
| 8004 |
{ |
| 8005 |
// runtime args |
| 8006 |
(*es)[1] = maybePromoteToTmp((*args)[0]); |
| 8007 |
(*es)[2] = maybePromoteToTmp((*args)[1]); |
| 8008 |
} |
| 8009 |
} |
| 8010 |
else |
| 8011 |
{ |
| 8012 |
auto binExp = cast(EqualExp) exp.e1; |
| 8013 |
|
| 8014 |
// runtime args |
| 8015 |
(*es)[1] = maybePromoteToTmp(binExp.e1); |
| 8016 |
(*es)[2] = maybePromoteToTmp(binExp.e2); |
| 8017 |
} |
| 8018 |
|
| 8019 |
// template args |
| 8020 |
Expression comp = new StringExp(loc, isEqualsCallExpression ? "==" : EXPtoString(exp.e1.op)); |
| 8021 |
comp = comp.expressionSemantic(sc); |
| 8022 |
(*es)[0] = comp; |
| 8023 |
(*tiargs)[0] = (*es)[1].type; |
| 8024 |
} |
| 8025 |
|
| 8026 |
// Format exp.e1 before any additional boolean conversion |
| 8027 |
// Ignore &&/|| because "assert(...) failed" is more informative than "false != true" |
| 8028 |
else if (op != EXP.andAnd && op != EXP.orOr) |
| 8029 |
{ |
| 8030 |
es = new Expressions(2); |
| 8031 |
tiargs = new Objects(1); |
| 8032 |
|
| 8033 |
if (auto ne = exp.e1.isNotExp()) |
| 8034 |
{ |
| 8035 |
// Fetch the (potential non-bool) expression and fold |
| 8036 |
// (n) negations into (n % 2) negations, e.g. !!a => a |
| 8037 |
for (bool neg = true; ; neg = !neg) |
| 8038 |
{ |
| 8039 |
if (auto ne2 = ne.e1.isNotExp()) |
| 8040 |
ne = ne2; |
| 8041 |
else |
| 8042 |
{ |
| 8043 |
(*es)[0] = new StringExp(loc, neg ? "!" : ""); |
| 8044 |
(*es)[1] = maybePromoteToTmp(ne.e1); |
| 8045 |
break; |
| 8046 |
} |
| 8047 |
} |
| 8048 |
} |
| 8049 |
else |
| 8050 |
{ // Simply format exp.e1 |
| 8051 |
(*es)[0] = new StringExp(loc, ""); |
| 8052 |
(*es)[1] = maybePromoteToTmp(exp.e1); |
| 8053 |
} |
| 8054 |
|
| 8055 |
(*tiargs)[0] = (*es)[1].type; |
| 8056 |
|
| 8057 |
// Passing __ctfe to auto ref infers ref and aborts compilation: |
| 8058 |
// "cannot modify compiler-generated variable __ctfe" |
| 8059 |
auto ve = (*es)[1].isVarExp(); |
| 8060 |
if (ve && ve.var.ident == Id.ctfe) |
| 8061 |
{ |
| 8062 |
exp.msg = new StringExp(loc, "assert(__ctfe) failed!"); |
| 8063 |
goto LSkip; |
| 8064 |
} |
| 8065 |
} |
| 8066 |
else |
| 8067 |
{ |
| 8068 |
OutBuffer buf; |
| 8069 |
buf.printf("`%s` failed", exp.toChars()); |
| 8070 |
exp.msg = new StringExp(Loc.initial, buf.extractSlice()); |
| 8071 |
goto LSkip; |
| 8072 |
} |
| 8073 |
|
| 8074 |
Expression __assertFail = new IdentifierExp(exp.loc, Id.empty); |
| 8075 |
auto assertFail = new DotIdExp(loc, __assertFail, Id.object); |
| 8076 |
|
| 8077 |
auto dt = new DotTemplateInstanceExp(loc, assertFail, Id._d_assert_fail, tiargs); |
| 8078 |
auto ec = CallExp.create(loc, dt, es); |
| 8079 |
exp.msg = ec; |
| 8080 |
} |
| 8081 |
|
| 8082 |
LSkip: |
| 8083 |
if (Expression ex = unaSemantic(exp, sc)) |
| 8084 |
{ |
| 8085 |
result = ex; |
| 8086 |
return; |
| 8087 |
} |
| 8088 |
|
| 8089 |
exp.e1 = resolveProperties(sc, exp.e1); |
| 8090 |
// BUG: see if we can do compile time elimination of the Assert |
| 8091 |
exp.e1 = exp.e1.optimize(WANTvalue); |
| 8092 |
exp.e1 = exp.e1.toBoolean(sc); |
| 8093 |
|
| 8094 |
if (exp.e1.op == EXP.error) |
| 8095 |
{ |
| 8096 |
result = exp.e1; |
| 8097 |
return; |
| 8098 |
} |
| 8099 |
|
| 8100 |
if (exp.msg) |
| 8101 |
{ |
| 8102 |
exp.msg = expressionSemantic(exp.msg, sc); |
| 8103 |
exp.msg = resolveProperties(sc, exp.msg); |
| 8104 |
exp.msg = exp.msg.implicitCastTo(sc, Type.tchar.constOf().arrayOf()); |
| 8105 |
exp.msg = exp.msg.optimize(WANTvalue); |
| 8106 |
checkParamArgumentEscape(*sc, null, null, null, STC.none, exp.msg, true, false); |
| 8107 |
} |
| 8108 |
|
| 8109 |
if (exp.msg && exp.msg.op == EXP.error) |
| 8110 |
{ |
| 8111 |
result = exp.msg; |
| 8112 |
return; |
| 8113 |
} |
| 8114 |
|
| 8115 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 8116 |
auto f2 = exp.msg && checkNonAssignmentArrayOp(exp.msg); |
| 8117 |
if (f1 || f2) |
| 8118 |
return setError(); |
| 8119 |
|
| 8120 |
if (exp.e1.toBool().hasValue(false)) |
| 8121 |
{ |
| 8122 |
/* This is an `assert(0)` which means halt program execution |
| 8123 |
*/ |
| 8124 |
FuncDeclaration fd = sc.parent.isFuncDeclaration(); |
| 8125 |
sc.ctorflow.orCSX(CSX.halt); |
| 8126 |
|
| 8127 |
if (global.params.useAssert == CHECKENABLE.off) |
| 8128 |
{ |
| 8129 |
Expression e = new HaltExp(exp.loc); |
| 8130 |
e = e.expressionSemantic(sc); |
| 8131 |
result = e; |
| 8132 |
return; |
| 8133 |
} |
| 8134 |
|
| 8135 |
// Only override the type when it isn't already some flavour of noreturn, |
| 8136 |
// e.g. when this assert was generated by defaultInitLiteral |
| 8137 |
if (!exp.type || !exp.type.isTypeNoreturn()) |
| 8138 |
exp.type = Type.tnoreturn; |
| 8139 |
} |
| 8140 |
else |
| 8141 |
exp.type = Type.tvoid; |
| 8142 |
|
| 8143 |
result = !temporariesPrefix |
| 8144 |
? exp |
| 8145 |
: Expression.combine(temporariesPrefix, exp).expressionSemantic(sc); |
| 8146 |
} |
| 8147 |
|
| 8148 |
override void visit(ThrowExp te) |
| 8149 |
{ |
| 8150 |
import dmd.statementsem; |
| 8151 |
|
| 8152 |
te.type = Type.tnoreturn; |
| 8153 |
if (throwSemantic(te.loc, te.e1, sc)) |
| 8154 |
result = te; |
| 8155 |
else |
| 8156 |
setError(); |
| 8157 |
} |
| 8158 |
|
| 8159 |
override void visit(DotIdExp exp) |
| 8160 |
{ |
| 8161 |
static if (LOGSEMANTIC) |
| 8162 |
{ |
| 8163 |
printf("DotIdExp::semantic(this = %p, '%s')\n", exp, exp.toChars()); |
| 8164 |
printAST(exp); |
| 8165 |
} |
| 8166 |
|
| 8167 |
if (sc.inCfile) |
| 8168 |
{ |
| 8169 |
/* See if need to rewrite the AST because of cast/call ambiguity |
| 8170 |
*/ |
| 8171 |
if (auto e = castCallAmbiguity(exp, sc)) |
| 8172 |
{ |
| 8173 |
result = expressionSemantic(e, sc); |
| 8174 |
return; |
| 8175 |
} |
| 8176 |
|
| 8177 |
if (exp.arrow) // ImportC only |
| 8178 |
exp.e1 = exp.e1.expressionSemantic(sc).arrayFuncConv(sc); |
| 8179 |
|
| 8180 |
if (exp.ident == Id.__xalignof && exp.e1.isTypeExp()) |
| 8181 |
{ |
| 8182 |
// C11 6.5.3 says _Alignof only applies to types |
| 8183 |
Expression e; |
| 8184 |
Type t; |
| 8185 |
Dsymbol s; |
| 8186 |
dmd.typesem.resolve(exp.e1.type, exp.e1.loc, sc, e, t, s, true); |
| 8187 |
if (e) |
| 8188 |
{ |
| 8189 |
error(exp.e1.loc, "argument to `_Alignof` must be a type"); |
| 8190 |
return setError(); |
| 8191 |
} |
| 8192 |
else if (t) |
| 8193 |
{ |
| 8194 |
// Note similarity to getProperty() implementation of __xalignof |
| 8195 |
const explicitAlignment = t.alignment(); |
| 8196 |
const naturalAlignment = t.alignsize(); |
| 8197 |
const actualAlignment = (explicitAlignment.isDefault() ? naturalAlignment : explicitAlignment.get()); |
| 8198 |
result = new IntegerExp(exp.loc, actualAlignment, Type.tsize_t); |
| 8199 |
} |
| 8200 |
else if (s) |
| 8201 |
{ |
| 8202 |
error(exp.e1.loc, "argument to `_Alignof` must be a type"); |
| 8203 |
return setError(); |
| 8204 |
} |
| 8205 |
else |
| 8206 |
assert(0); |
| 8207 |
return; |
| 8208 |
} |
| 8209 |
|
| 8210 |
if (exp.ident != Id.__sizeof) |
| 8211 |
{ |
| 8212 |
result = fieldLookup(exp.e1, sc, exp.ident, exp.arrow); |
| 8213 |
return; |
| 8214 |
} |
| 8215 |
} |
| 8216 |
|
| 8217 |
Expression e = exp.dotIdSemanticProp(sc, 1); |
| 8218 |
|
| 8219 |
if (e && isDotOpDispatch(e)) |
| 8220 |
{ |
| 8221 |
auto ode = e; |
| 8222 |
const errors = global.startGagging(); |
| 8223 |
e = resolvePropertiesX(sc, e); |
| 8224 |
// Any error or if 'e' is not resolved, go to UFCS |
| 8225 |
if (global.endGagging(errors) || e is ode) |
| 8226 |
e = null; /* fall down to UFCS */ |
| 8227 |
else |
| 8228 |
{ |
| 8229 |
result = e; |
| 8230 |
return; |
| 8231 |
} |
| 8232 |
} |
| 8233 |
if (!e) // if failed to find the property |
| 8234 |
{ |
| 8235 |
/* If ident is not a valid property, rewrite: |
| 8236 |
* e1.ident |
| 8237 |
* as: |
| 8238 |
* .ident(e1) |
| 8239 |
*/ |
| 8240 |
e = resolveUFCSProperties(sc, exp); |
| 8241 |
} |
| 8242 |
result = e; |
| 8243 |
} |
| 8244 |
|
| 8245 |
override void visit(DotTemplateExp e) |
| 8246 |
{ |
| 8247 |
if (Expression ex = unaSemantic(e, sc)) |
| 8248 |
{ |
| 8249 |
result = ex; |
| 8250 |
return; |
| 8251 |
} |
| 8252 |
// 'void' like TemplateExp |
| 8253 |
e.type = Type.tvoid; |
| 8254 |
result = e; |
| 8255 |
} |
| 8256 |
|
| 8257 |
override void visit(DotVarExp exp) |
| 8258 |
{ |
| 8259 |
static if (LOGSEMANTIC) |
| 8260 |
{ |
| 8261 |
printf("DotVarExp::semantic('%s')\n", exp.toChars()); |
| 8262 |
} |
| 8263 |
|
| 8264 |
exp.var = exp.var.toAlias().isDeclaration(); |
| 8265 |
|
| 8266 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 8267 |
|
| 8268 |
if (auto tup = exp.var.isTupleDeclaration()) |
| 8269 |
{ |
| 8270 |
/* Replace: |
| 8271 |
* e1.tuple(a, b, c) |
| 8272 |
* with: |
| 8273 |
* tuple(e1.a, e1.b, e1.c) |
| 8274 |
*/ |
| 8275 |
Expression e0; |
| 8276 |
Expression ev = sc.func ? extractSideEffect(sc, "__tup", e0, exp.e1) : exp.e1; |
| 8277 |
|
| 8278 |
auto exps = new Expressions(); |
| 8279 |
exps.reserve(tup.objects.length); |
| 8280 |
for (size_t i = 0; i < tup.objects.length; i++) |
| 8281 |
{ |
| 8282 |
RootObject o = (*tup.objects)[i]; |
| 8283 |
Expression e; |
| 8284 |
Declaration var; |
| 8285 |
switch (o.dyncast()) with (DYNCAST) |
| 8286 |
{ |
| 8287 |
case expression: |
| 8288 |
e = cast(Expression)o; |
| 8289 |
if (auto se = e.isDsymbolExp()) |
| 8290 |
var = se.s.isDeclaration(); |
| 8291 |
else if (auto ve = e.isVarExp()) |
| 8292 |
if (!ve.var.isFuncDeclaration()) |
| 8293 |
// Exempt functions for backwards compatibility reasons. |
| 8294 |
// See: https://issues.dlang.org/show_bug.cgi?id=20470#c1 |
| 8295 |
var = ve.var; |
| 8296 |
break; |
| 8297 |
case dsymbol: |
| 8298 |
Dsymbol s = cast(Dsymbol) o; |
| 8299 |
Declaration d = s.isDeclaration(); |
| 8300 |
if (!d || d.isFuncDeclaration()) |
| 8301 |
// Exempt functions for backwards compatibility reasons. |
| 8302 |
// See: https://issues.dlang.org/show_bug.cgi?id=20470#c1 |
| 8303 |
e = new DsymbolExp(exp.loc, s); |
| 8304 |
else |
| 8305 |
var = d; |
| 8306 |
break; |
| 8307 |
case type: |
| 8308 |
e = new TypeExp(exp.loc, cast(Type)o); |
| 8309 |
break; |
| 8310 |
default: |
| 8311 |
error(exp.loc, "`%s` is not an expression", o.toChars()); |
| 8312 |
return setError(); |
| 8313 |
} |
| 8314 |
if (var) |
| 8315 |
e = new DotVarExp(exp.loc, ev, var); |
| 8316 |
exps.push(e); |
| 8317 |
} |
| 8318 |
|
| 8319 |
Expression e = new TupleExp(exp.loc, e0, exps); |
| 8320 |
e = e.expressionSemantic(sc); |
| 8321 |
result = e; |
| 8322 |
return; |
| 8323 |
} |
| 8324 |
else if (auto ad = exp.var.isAliasDeclaration()) |
| 8325 |
{ |
| 8326 |
if (auto t = ad.getType()) |
| 8327 |
{ |
| 8328 |
result = new TypeExp(exp.loc, t).expressionSemantic(sc); |
| 8329 |
return; |
| 8330 |
} |
| 8331 |
} |
| 8332 |
|
| 8333 |
exp.e1 = exp.e1.addDtorHook(sc); |
| 8334 |
|
| 8335 |
Type t1 = exp.e1.type; |
| 8336 |
|
| 8337 |
if (FuncDeclaration fd = exp.var.isFuncDeclaration()) |
| 8338 |
{ |
| 8339 |
// for functions, do checks after overload resolution |
| 8340 |
if (!functionSemantic(fd)) |
| 8341 |
return setError(); |
| 8342 |
|
| 8343 |
/* https://issues.dlang.org/show_bug.cgi?id=13843 |
| 8344 |
* If fd obviously has no overloads, we should |
| 8345 |
* normalize AST, and it will give a chance to wrap fd with FuncExp. |
| 8346 |
*/ |
| 8347 |
if ((fd.isNested() && !fd.isThis()) || fd.isFuncLiteralDeclaration()) |
| 8348 |
{ |
| 8349 |
// (e1, fd) |
| 8350 |
auto e = symbolToExp(fd, exp.loc, sc, false); |
| 8351 |
result = Expression.combine(exp.e1, e); |
| 8352 |
return; |
| 8353 |
} |
| 8354 |
|
| 8355 |
exp.type = fd.type; |
| 8356 |
assert(exp.type); |
| 8357 |
} |
| 8358 |
else if (OverDeclaration od = exp.var.isOverDeclaration()) |
| 8359 |
{ |
| 8360 |
exp.type = Type.tvoid; // ambiguous type? |
| 8361 |
} |
| 8362 |
else |
| 8363 |
{ |
| 8364 |
exp.type = exp.var.type; |
| 8365 |
if (!exp.type && global.errors) // var is goofed up, just return error. |
| 8366 |
return setError(); |
| 8367 |
assert(exp.type); |
| 8368 |
|
| 8369 |
if (t1.ty == Tpointer) |
| 8370 |
t1 = t1.nextOf(); |
| 8371 |
|
| 8372 |
exp.type = exp.type.addMod(t1.mod); |
| 8373 |
|
| 8374 |
// https://issues.dlang.org/show_bug.cgi?id=23109 |
| 8375 |
// Run semantic on the DotVarExp type |
| 8376 |
if (auto handle = exp.type.isClassHandle()) |
| 8377 |
{ |
| 8378 |
if (handle.semanticRun < PASS.semanticdone && !handle.isBaseInfoComplete()) |
| 8379 |
handle.dsymbolSemantic(null); |
| 8380 |
} |
| 8381 |
|
| 8382 |
Dsymbol vparent = exp.var.toParent(); |
| 8383 |
AggregateDeclaration ad = vparent ? vparent.isAggregateDeclaration() : null; |
| 8384 |
if (Expression e1x = getRightThis(exp.loc, sc, ad, exp.e1, exp.var, 1)) |
| 8385 |
exp.e1 = e1x; |
| 8386 |
else |
| 8387 |
{ |
| 8388 |
/* Later checkRightThis will report correct error for invalid field variable access. |
| 8389 |
*/ |
| 8390 |
Expression e = new VarExp(exp.loc, exp.var); |
| 8391 |
e = e.expressionSemantic(sc); |
| 8392 |
result = e; |
| 8393 |
return; |
| 8394 |
} |
| 8395 |
checkAccess(exp.loc, sc, exp.e1, exp.var); |
| 8396 |
|
| 8397 |
VarDeclaration v = exp.var.isVarDeclaration(); |
| 8398 |
if (v && (v.isDataseg() || (v.storage_class & STC.manifest))) |
| 8399 |
{ |
| 8400 |
Expression e = expandVar(WANTvalue, v); |
| 8401 |
if (e) |
| 8402 |
{ |
| 8403 |
result = e; |
| 8404 |
return; |
| 8405 |
} |
| 8406 |
} |
| 8407 |
|
| 8408 |
if (v && (v.isDataseg() || // fix https://issues.dlang.org/show_bug.cgi?id=8238 |
| 8409 |
(!v.needThis() && v.semanticRun > PASS.initial))) // fix https://issues.dlang.org/show_bug.cgi?id=17258 |
| 8410 |
{ |
| 8411 |
// (e1, v) |
| 8412 |
checkAccess(exp.loc, sc, exp.e1, v); |
| 8413 |
Expression e = new VarExp(exp.loc, v); |
| 8414 |
e = new CommaExp(exp.loc, exp.e1, e); |
| 8415 |
e = e.expressionSemantic(sc); |
| 8416 |
result = e; |
| 8417 |
return; |
| 8418 |
} |
| 8419 |
} |
| 8420 |
//printf("-DotVarExp::semantic('%s')\n", toChars()); |
| 8421 |
result = exp; |
| 8422 |
} |
| 8423 |
|
| 8424 |
override void visit(DotTemplateInstanceExp exp) |
| 8425 |
{ |
| 8426 |
static if (LOGSEMANTIC) |
| 8427 |
{ |
| 8428 |
printf("DotTemplateInstanceExp::semantic('%s')\n", exp.toChars()); |
| 8429 |
} |
| 8430 |
// Indicate we need to resolve by UFCS. |
| 8431 |
Expression e = exp.dotTemplateSemanticProp(sc, DotExpFlag.gag); |
| 8432 |
if (!e) |
| 8433 |
e = resolveUFCSProperties(sc, exp); |
| 8434 |
if (e is exp) |
| 8435 |
e.type = Type.tvoid; // Unresolved type, because it needs inference |
| 8436 |
result = e; |
| 8437 |
} |
| 8438 |
|
| 8439 |
override void visit(DelegateExp e) |
| 8440 |
{ |
| 8441 |
static if (LOGSEMANTIC) |
| 8442 |
{ |
| 8443 |
printf("DelegateExp::semantic('%s')\n", e.toChars()); |
| 8444 |
} |
| 8445 |
|
| 8446 |
e.e1 = e.e1.expressionSemantic(sc); |
| 8447 |
|
| 8448 |
e.type = new TypeDelegate(e.func.type.isTypeFunction()); |
| 8449 |
e.type = e.type.typeSemantic(e.loc, sc); |
| 8450 |
|
| 8451 |
FuncDeclaration f = e.func.toAliasFunc(); |
| 8452 |
AggregateDeclaration ad = f.isMemberLocal(); |
| 8453 |
if (f.needThis()) |
| 8454 |
e.e1 = getRightThis(e.loc, sc, ad, e.e1, f); |
| 8455 |
|
| 8456 |
if (f.type.ty == Tfunction) |
| 8457 |
{ |
| 8458 |
TypeFunction tf = cast(TypeFunction)f.type; |
| 8459 |
if (!MODmethodConv(e.e1.type.mod, f.type.mod)) |
| 8460 |
{ |
| 8461 |
OutBuffer thisBuf, funcBuf; |
| 8462 |
MODMatchToBuffer(&thisBuf, e.e1.type.mod, tf.mod); |
| 8463 |
MODMatchToBuffer(&funcBuf, tf.mod, e.e1.type.mod); |
| 8464 |
error(e.loc, "%smethod `%s` is not callable using a %s`%s`", |
| 8465 |
funcBuf.peekChars(), f.toPrettyChars(), thisBuf.peekChars(), e.e1.toErrMsg()); |
| 8466 |
return setError(); |
| 8467 |
} |
| 8468 |
} |
| 8469 |
if (ad && ad.isClassDeclaration() && ad.type != e.e1.type) |
| 8470 |
{ |
| 8471 |
// A downcast is required for interfaces |
| 8472 |
// https://issues.dlang.org/show_bug.cgi?id=3706 |
| 8473 |
e.e1 = new CastExp(e.loc, e.e1, ad.type); |
| 8474 |
e.e1 = e.e1.expressionSemantic(sc); |
| 8475 |
} |
| 8476 |
result = e; |
| 8477 |
// declare dual-context container |
| 8478 |
if (f.hasDualContext() && !sc.intypeof && sc.func) |
| 8479 |
{ |
| 8480 |
// check access to second `this` |
| 8481 |
if (AggregateDeclaration ad2 = f.isMember2()) |
| 8482 |
{ |
| 8483 |
Expression te = new ThisExp(e.loc).expressionSemantic(sc); |
| 8484 |
if (te.op != EXP.error) |
| 8485 |
te = getRightThis(e.loc, sc, ad2, te, f); |
| 8486 |
if (te.op == EXP.error) |
| 8487 |
{ |
| 8488 |
error(e.loc, "need `this` of type `%s` to make delegate from function `%s`", ad2.toChars(), f.toChars()); |
| 8489 |
return setError(); |
| 8490 |
} |
| 8491 |
} |
| 8492 |
VarDeclaration vthis2 = makeThis2Argument(e.loc, sc, f); |
| 8493 |
e.vthis2 = vthis2; |
| 8494 |
Expression de = new DeclarationExp(e.loc, vthis2); |
| 8495 |
result = Expression.combine(de, result); |
| 8496 |
result = result.expressionSemantic(sc); |
| 8497 |
} |
| 8498 |
} |
| 8499 |
|
| 8500 |
override void visit(DotTypeExp exp) |
| 8501 |
{ |
| 8502 |
static if (LOGSEMANTIC) |
| 8503 |
{ |
| 8504 |
printf("DotTypeExp::semantic('%s')\n", exp.toChars()); |
| 8505 |
} |
| 8506 |
|
| 8507 |
if (auto e = unaSemantic(exp, sc)) |
| 8508 |
{ |
| 8509 |
result = e; |
| 8510 |
return; |
| 8511 |
} |
| 8512 |
|
| 8513 |
exp.type = exp.sym.getType().addMod(exp.e1.type.mod); |
| 8514 |
result = exp; |
| 8515 |
} |
| 8516 |
|
| 8517 |
override void visit(AddrExp exp) |
| 8518 |
{ |
| 8519 |
static if (LOGSEMANTIC) |
| 8520 |
{ |
| 8521 |
printf("AddrExp::semantic('%s')\n", exp.toChars()); |
| 8522 |
} |
| 8523 |
|
| 8524 |
if (Expression ex = unaSemantic(exp, sc)) |
| 8525 |
{ |
| 8526 |
result = ex; |
| 8527 |
return; |
| 8528 |
} |
| 8529 |
|
| 8530 |
if (sc.inCfile) |
| 8531 |
{ |
| 8532 |
/* Special handling for &"string"/&(T[]){0, 1} |
| 8533 |
* since C regards string/array literals as lvalues |
| 8534 |
*/ |
| 8535 |
auto e = exp.e1; |
| 8536 |
if(e.isStringExp() || e.isArrayLiteralExp()) |
| 8537 |
{ |
| 8538 |
e.type = typeSemantic(e.type, Loc.initial, sc); |
| 8539 |
// if type is already a pointer exp is an illegal expression of the form `&(&"")` |
| 8540 |
if (!e.type.isTypePointer()) |
| 8541 |
{ |
| 8542 |
e.type = e.type.pointerTo(); |
| 8543 |
result = e; |
| 8544 |
return; |
| 8545 |
} |
| 8546 |
else |
| 8547 |
{ |
| 8548 |
// `toLvalue` call further below is upon exp.e1, omitting & from the error message |
| 8549 |
exp.toLvalue(sc, "take address of"); |
| 8550 |
return setError(); |
| 8551 |
} |
| 8552 |
} |
| 8553 |
} |
| 8554 |
|
| 8555 |
int wasCond = exp.e1.op == EXP.question; |
| 8556 |
|
| 8557 |
if (exp.e1.op == EXP.dotTemplateInstance) |
| 8558 |
{ |
| 8559 |
DotTemplateInstanceExp dti = cast(DotTemplateInstanceExp)exp.e1; |
| 8560 |
TemplateInstance ti = dti.ti; |
| 8561 |
{ |
| 8562 |
//assert(ti.needsTypeInference(sc)); |
| 8563 |
ti.dsymbolSemantic(sc); |
| 8564 |
if (!ti.inst || ti.errors) // if template failed to expand |
| 8565 |
return setError(); |
| 8566 |
|
| 8567 |
Dsymbol s = ti.toAlias(); |
| 8568 |
FuncDeclaration f = s.isFuncDeclaration(); |
| 8569 |
if (f) |
| 8570 |
{ |
| 8571 |
exp.e1 = new DotVarExp(exp.e1.loc, dti.e1, f); |
| 8572 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 8573 |
} |
| 8574 |
} |
| 8575 |
} |
| 8576 |
else if (exp.e1.op == EXP.scope_) |
| 8577 |
{ |
| 8578 |
TemplateInstance ti = (cast(ScopeExp)exp.e1).sds.isTemplateInstance(); |
| 8579 |
if (ti) |
| 8580 |
{ |
| 8581 |
//assert(ti.needsTypeInference(sc)); |
| 8582 |
ti.dsymbolSemantic(sc); |
| 8583 |
if (!ti.inst || ti.errors) // if template failed to expand |
| 8584 |
return setError(); |
| 8585 |
|
| 8586 |
Dsymbol s = ti.toAlias(); |
| 8587 |
FuncDeclaration f = s.isFuncDeclaration(); |
| 8588 |
if (f) |
| 8589 |
{ |
| 8590 |
exp.e1 = new VarExp(exp.e1.loc, f); |
| 8591 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 8592 |
} |
| 8593 |
} |
| 8594 |
} |
| 8595 |
/* https://issues.dlang.org/show_bug.cgi?id=809 |
| 8596 |
* |
| 8597 |
* If the address of a lazy variable is taken, |
| 8598 |
* the expression is rewritten so that the type |
| 8599 |
* of it is the delegate type. This means that |
| 8600 |
* the symbol is not going to represent a call |
| 8601 |
* to the delegate anymore, but rather, the |
| 8602 |
* actual symbol. |
| 8603 |
*/ |
| 8604 |
if (auto ve = exp.e1.isVarExp()) |
| 8605 |
{ |
| 8606 |
if (ve.var.storage_class & STC.lazy_) |
| 8607 |
{ |
| 8608 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 8609 |
exp.e1 = resolveProperties(sc, exp.e1); |
| 8610 |
if (auto callExp = exp.e1.isCallExp()) |
| 8611 |
{ |
| 8612 |
if (callExp.e1.type.toBasetype().ty == Tdelegate) |
| 8613 |
{ |
| 8614 |
/* https://issues.dlang.org/show_bug.cgi?id=20551 |
| 8615 |
* |
| 8616 |
* Cannot take address of lazy parameter in @safe code |
| 8617 |
* because it might end up being a pointer to undefined |
| 8618 |
* memory. |
| 8619 |
*/ |
| 8620 |
if (1) |
| 8621 |
{ |
| 8622 |
if (sc.setUnsafe(false, exp.loc, "taking address of lazy parameter `%s`", ve)) |
| 8623 |
{ |
| 8624 |
setError(); |
| 8625 |
return; |
| 8626 |
} |
| 8627 |
} |
| 8628 |
VarExp ve2 = callExp.e1.isVarExp(); |
| 8629 |
ve2.delegateWasExtracted = true; |
| 8630 |
ve2.var.storage_class |= STC.scope_; |
| 8631 |
result = ve2; |
| 8632 |
return; |
| 8633 |
} |
| 8634 |
} |
| 8635 |
} |
| 8636 |
} |
| 8637 |
|
| 8638 |
exp.e1 = exp.e1.toLvalue(sc, "take address of"); |
| 8639 |
if (exp.e1.op == EXP.error) |
| 8640 |
{ |
| 8641 |
result = exp.e1; |
| 8642 |
return; |
| 8643 |
} |
| 8644 |
if (checkNonAssignmentArrayOp(exp.e1)) |
| 8645 |
return setError(); |
| 8646 |
|
| 8647 |
if (!exp.e1.type) |
| 8648 |
{ |
| 8649 |
error(exp.loc, "cannot take address of `%s`", exp.e1.toErrMsg()); |
| 8650 |
return setError(); |
| 8651 |
} |
| 8652 |
if (!checkAddressable(exp, sc)) |
| 8653 |
return setError(); |
| 8654 |
|
| 8655 |
bool hasOverloads; |
| 8656 |
if (auto f = isFuncAddress(exp, &hasOverloads)) |
| 8657 |
{ |
| 8658 |
if (!hasOverloads && f.checkForwardRef(exp.loc)) |
| 8659 |
return setError(); |
| 8660 |
} |
| 8661 |
else if (!exp.e1.type.deco) |
| 8662 |
{ |
| 8663 |
// try to resolve the type |
| 8664 |
exp.e1.type = exp.e1.type.typeSemantic(exp.e1.loc, sc); |
| 8665 |
if (!exp.e1.type.deco) // still couldn't resolve it |
| 8666 |
{ |
| 8667 |
if (auto ve = exp.e1.isVarExp()) |
| 8668 |
{ |
| 8669 |
Declaration d = ve.var; |
| 8670 |
error(exp.loc, "forward reference to %s `%s`", d.kind(), d.toChars()); |
| 8671 |
} |
| 8672 |
else |
| 8673 |
error(exp.loc, "forward reference to type `%s` of expression `%s`", exp.e1.type.toChars(), exp.e1.toErrMsg()); |
| 8674 |
return setError(); |
| 8675 |
} |
| 8676 |
} |
| 8677 |
|
| 8678 |
exp.type = exp.e1.type.pointerTo(); |
| 8679 |
|
| 8680 |
// See if this should really be a delegate |
| 8681 |
if (exp.e1.op == EXP.dotVariable) |
| 8682 |
{ |
| 8683 |
DotVarExp dve = cast(DotVarExp)exp.e1; |
| 8684 |
FuncDeclaration f = dve.var.isFuncDeclaration(); |
| 8685 |
if (f) |
| 8686 |
{ |
| 8687 |
f = f.toAliasFunc(); // FIXME, should see overloads |
| 8688 |
// https://issues.dlang.org/show_bug.cgi?id=1983 |
| 8689 |
if (!dve.hasOverloads) |
| 8690 |
f.tookAddressOf++; |
| 8691 |
|
| 8692 |
Expression e; |
| 8693 |
if (f.needThis()) |
| 8694 |
e = new DelegateExp(exp.loc, dve.e1, f, dve.hasOverloads); |
| 8695 |
else // It is a function pointer. Convert &v.f() --> (v, &V.f()) |
| 8696 |
e = new CommaExp(exp.loc, dve.e1, new AddrExp(exp.loc, new VarExp(exp.loc, f, dve.hasOverloads))); |
| 8697 |
e = e.expressionSemantic(sc); |
| 8698 |
result = e; |
| 8699 |
return; |
| 8700 |
} |
| 8701 |
|
| 8702 |
// Look for misaligned pointer in @safe mode |
| 8703 |
if (checkUnsafeAccess(sc, dve, !exp.type.isMutable(), true)) |
| 8704 |
return setError(); |
| 8705 |
} |
| 8706 |
else if (exp.e1.op == EXP.variable) |
| 8707 |
{ |
| 8708 |
VarExp ve = cast(VarExp)exp.e1; |
| 8709 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 8710 |
if (v) |
| 8711 |
{ |
| 8712 |
if (!checkAddressVar(sc, exp.e1, v)) |
| 8713 |
return setError(); |
| 8714 |
|
| 8715 |
v.checkPurity(ve.loc, sc); |
| 8716 |
} |
| 8717 |
FuncDeclaration f = ve.var.isFuncDeclaration(); |
| 8718 |
if (f) |
| 8719 |
{ |
| 8720 |
/* Because nested functions cannot be overloaded, |
| 8721 |
* mark here that we took its address because castTo() |
| 8722 |
* may not be called with an exact match. |
| 8723 |
* |
| 8724 |
* https://issues.dlang.org/show_bug.cgi?id=19285 : |
| 8725 |
* We also need to make sure we aren't inside a typeof. Ideally the compiler |
| 8726 |
* would do typeof(...) semantic analysis speculatively then collect information |
| 8727 |
* about what it used rather than relying on what are effectively semantically-global |
| 8728 |
* variables but it doesn't. |
| 8729 |
*/ |
| 8730 |
if (!sc.isFromSpeculativeSemanticContext() && (!ve.hasOverloads || (f.isNested() && !f.needThis()))) |
| 8731 |
{ |
| 8732 |
// TODO: Refactor to use a proper interface that can keep track of causes. |
| 8733 |
f.tookAddressOf++; |
| 8734 |
} |
| 8735 |
|
| 8736 |
if (f.isNested() && !f.needThis()) |
| 8737 |
{ |
| 8738 |
if (f.isFuncLiteralDeclaration()) |
| 8739 |
{ |
| 8740 |
if (!f.FuncDeclaration.isNested()) |
| 8741 |
{ |
| 8742 |
/* Supply a 'null' for a this pointer if no this is available |
| 8743 |
*/ |
| 8744 |
Expression e = new DelegateExp(exp.loc, new NullExp(exp.loc, Type.tnull), f, ve.hasOverloads); |
| 8745 |
e = e.expressionSemantic(sc); |
| 8746 |
result = e; |
| 8747 |
return; |
| 8748 |
} |
| 8749 |
} |
| 8750 |
Expression e = new DelegateExp(exp.loc, exp.e1, f, ve.hasOverloads); |
| 8751 |
e = e.expressionSemantic(sc); |
| 8752 |
result = e; |
| 8753 |
return; |
| 8754 |
} |
| 8755 |
if (f.needThis()) |
| 8756 |
{ |
| 8757 |
auto memberFunc = hasThis(sc); |
| 8758 |
if (memberFunc && haveSameThis(memberFunc, f)) |
| 8759 |
{ |
| 8760 |
/* Should probably supply 'this' after overload resolution, |
| 8761 |
* not before. |
| 8762 |
*/ |
| 8763 |
Expression ethis = new ThisExp(exp.loc); |
| 8764 |
Expression e = new DelegateExp(exp.loc, ethis, f, ve.hasOverloads); |
| 8765 |
e = e.expressionSemantic(sc); |
| 8766 |
result = e; |
| 8767 |
return; |
| 8768 |
} |
| 8769 |
if (sc.func && !sc.intypeof && !sc.debug_) |
| 8770 |
{ |
| 8771 |
sc.setUnsafe(false, exp.loc, "taking address of member `%s` without `this` reference", f); |
| 8772 |
} |
| 8773 |
} |
| 8774 |
} |
| 8775 |
} |
| 8776 |
else if (exp.e1.op == EXP.index) |
| 8777 |
{ |
| 8778 |
/* For: |
| 8779 |
* int[3] a; |
| 8780 |
* &a[i] |
| 8781 |
* check 'a' the same as for a regular variable |
| 8782 |
*/ |
| 8783 |
int deref; |
| 8784 |
if (VarDeclaration v = expToVariable(exp.e1, deref)) |
| 8785 |
{ |
| 8786 |
v.checkPurity(exp.e1.loc, sc); |
| 8787 |
} |
| 8788 |
} |
| 8789 |
else if (wasCond) |
| 8790 |
{ |
| 8791 |
/* a ? b : c was transformed to *(a ? &b : &c), but we still |
| 8792 |
* need to do safety checks |
| 8793 |
*/ |
| 8794 |
assert(exp.e1.op == EXP.star); |
| 8795 |
PtrExp pe = cast(PtrExp)exp.e1; |
| 8796 |
assert(pe.e1.op == EXP.question); |
| 8797 |
CondExp ce = cast(CondExp)pe.e1; |
| 8798 |
assert(ce.e1.op == EXP.address); |
| 8799 |
assert(ce.e2.op == EXP.address); |
| 8800 |
|
| 8801 |
// Re-run semantic on the address expressions only |
| 8802 |
ce.e1.type = null; |
| 8803 |
ce.e1 = ce.e1.expressionSemantic(sc); |
| 8804 |
ce.e2.type = null; |
| 8805 |
ce.e2 = ce.e2.expressionSemantic(sc); |
| 8806 |
} |
| 8807 |
result = exp.optimize(WANTvalue); |
| 8808 |
} |
| 8809 |
|
| 8810 |
override void visit(PtrExp exp) |
| 8811 |
{ |
| 8812 |
static if (LOGSEMANTIC) |
| 8813 |
{ |
| 8814 |
printf("PtrExp::semantic('%s')\n", exp.toChars()); |
| 8815 |
} |
| 8816 |
|
| 8817 |
if (Expression e = exp.opOverloadUnary(sc)) |
| 8818 |
{ |
| 8819 |
result = e; |
| 8820 |
return; |
| 8821 |
} |
| 8822 |
|
| 8823 |
exp.e1 = exp.e1.arrayFuncConv(sc); |
| 8824 |
|
| 8825 |
Type tb = exp.e1.type.toBasetype(); |
| 8826 |
switch (tb.ty) |
| 8827 |
{ |
| 8828 |
case Tpointer: |
| 8829 |
exp.type = (cast(TypePointer)tb).next; |
| 8830 |
break; |
| 8831 |
|
| 8832 |
case Tsarray: |
| 8833 |
case Tarray: |
| 8834 |
if (isNonAssignmentArrayOp(exp.e1)) |
| 8835 |
goto default; |
| 8836 |
error(exp.loc, "using `*` on an array is no longer supported; use `*(%s).ptr` instead", exp.e1.toErrMsg()); |
| 8837 |
exp.type = (cast(TypeArray)tb).next; |
| 8838 |
exp.e1 = exp.e1.castTo(sc, exp.type.pointerTo()); |
| 8839 |
break; |
| 8840 |
|
| 8841 |
case Terror: |
| 8842 |
return setError(); |
| 8843 |
|
| 8844 |
case Tnull: |
| 8845 |
exp.type = Type.tnoreturn; // typeof(*null) is bottom type |
| 8846 |
break; |
| 8847 |
|
| 8848 |
default: |
| 8849 |
error(exp.loc, "can only `*` a pointer, not a `%s`", exp.e1.type.toChars()); |
| 8850 |
goto case Terror; |
| 8851 |
} |
| 8852 |
|
| 8853 |
if (sc.inCfile && exp.type && exp.type.toBasetype().ty == Tvoid) |
| 8854 |
{ |
| 8855 |
// https://issues.dlang.org/show_bug.cgi?id=23752 |
| 8856 |
// `&*((void*)(0))` is allowed in C |
| 8857 |
result = exp; |
| 8858 |
return; |
| 8859 |
} |
| 8860 |
|
| 8861 |
if (exp.checkValue()) |
| 8862 |
return setError(); |
| 8863 |
|
| 8864 |
result = exp; |
| 8865 |
} |
| 8866 |
|
| 8867 |
override void visit(NegExp exp) |
| 8868 |
{ |
| 8869 |
static if (LOGSEMANTIC) |
| 8870 |
{ |
| 8871 |
printf("NegExp::semantic('%s')\n", exp.toChars()); |
| 8872 |
} |
| 8873 |
|
| 8874 |
if (Expression e = exp.opOverloadUnary(sc)) |
| 8875 |
{ |
| 8876 |
result = e; |
| 8877 |
return; |
| 8878 |
} |
| 8879 |
|
| 8880 |
fix16997(sc, exp); |
| 8881 |
exp.type = exp.e1.type; |
| 8882 |
Type tb = exp.type.toBasetype(); |
| 8883 |
if (tb.isStaticOrDynamicArray()) |
| 8884 |
{ |
| 8885 |
if (!isArrayOpValid(exp.e1)) |
| 8886 |
{ |
| 8887 |
result = arrayOpInvalidError(exp); |
| 8888 |
return; |
| 8889 |
} |
| 8890 |
result = exp; |
| 8891 |
return; |
| 8892 |
} |
| 8893 |
if (!target.isVectorOpSupported(tb, exp.op)) |
| 8894 |
{ |
| 8895 |
result = exp.incompatibleTypes(); |
| 8896 |
return; |
| 8897 |
} |
| 8898 |
if (exp.e1.checkNoBool() || |
| 8899 |
exp.e1.checkArithmetic(exp.op) || |
| 8900 |
exp.e1.checkSharedAccess(sc)) |
| 8901 |
return setError(); |
| 8902 |
|
| 8903 |
result = exp; |
| 8904 |
} |
| 8905 |
|
| 8906 |
override void visit(UAddExp exp) |
| 8907 |
{ |
| 8908 |
static if (LOGSEMANTIC) |
| 8909 |
{ |
| 8910 |
printf("UAddExp::semantic('%s')\n", exp.toChars()); |
| 8911 |
} |
| 8912 |
|
| 8913 |
if (Expression e = exp.opOverloadUnary(sc)) |
| 8914 |
{ |
| 8915 |
result = e; |
| 8916 |
return; |
| 8917 |
} |
| 8918 |
|
| 8919 |
fix16997(sc, exp); |
| 8920 |
if (!target.isVectorOpSupported(exp.e1.type.toBasetype(), exp.op)) |
| 8921 |
{ |
| 8922 |
result = exp.incompatibleTypes(); |
| 8923 |
return; |
| 8924 |
} |
| 8925 |
|
| 8926 |
if (exp.e1.checkNoBool() || |
| 8927 |
exp.e1.checkArithmetic(exp.op) || |
| 8928 |
exp.e1.checkSharedAccess(sc)) |
| 8929 |
return setError(); |
| 8930 |
|
| 8931 |
result = exp.e1; |
| 8932 |
} |
| 8933 |
|
| 8934 |
override void visit(ComExp exp) |
| 8935 |
{ |
| 8936 |
|
| 8937 |
if (Expression e = exp.opOverloadUnary(sc)) |
| 8938 |
{ |
| 8939 |
result = e; |
| 8940 |
return; |
| 8941 |
} |
| 8942 |
|
| 8943 |
fix16997(sc, exp); |
| 8944 |
exp.type = exp.e1.type; |
| 8945 |
Type tb = exp.type.toBasetype(); |
| 8946 |
if (tb.isStaticOrDynamicArray()) |
| 8947 |
{ |
| 8948 |
if (!isArrayOpValid(exp.e1)) |
| 8949 |
{ |
| 8950 |
result = arrayOpInvalidError(exp); |
| 8951 |
return; |
| 8952 |
} |
| 8953 |
result = exp; |
| 8954 |
return; |
| 8955 |
} |
| 8956 |
if (!target.isVectorOpSupported(tb, exp.op)) |
| 8957 |
{ |
| 8958 |
result = exp.incompatibleTypes(); |
| 8959 |
return; |
| 8960 |
} |
| 8961 |
if (exp.e1.checkNoBool() || |
| 8962 |
exp.e1.checkIntegral() || |
| 8963 |
exp.e1.checkSharedAccess(sc)) |
| 8964 |
return setError(); |
| 8965 |
|
| 8966 |
result = exp; |
| 8967 |
} |
| 8968 |
|
| 8969 |
override void visit(NotExp e) |
| 8970 |
{ |
| 8971 |
|
| 8972 |
e.setNoderefOperand(); |
| 8973 |
|
| 8974 |
// Note there is no operator overload |
| 8975 |
if (Expression ex = unaSemantic(e, sc)) |
| 8976 |
{ |
| 8977 |
result = ex; |
| 8978 |
return; |
| 8979 |
} |
| 8980 |
|
| 8981 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 8982 |
if (e.e1.op == EXP.type) |
| 8983 |
e.e1 = resolveAliasThis(sc, e.e1); |
| 8984 |
|
| 8985 |
e.e1 = resolveProperties(sc, e.e1); |
| 8986 |
e.e1 = e.e1.toBoolean(sc); |
| 8987 |
if (e.e1.type == Type.terror) |
| 8988 |
{ |
| 8989 |
result = e.e1; |
| 8990 |
return; |
| 8991 |
} |
| 8992 |
|
| 8993 |
if (!target.isVectorOpSupported(e.e1.type.toBasetype(), e.op)) |
| 8994 |
{ |
| 8995 |
result = e.incompatibleTypes(); |
| 8996 |
} |
| 8997 |
// https://issues.dlang.org/show_bug.cgi?id=13910 |
| 8998 |
// Today NotExp can take an array as its operand. |
| 8999 |
if (checkNonAssignmentArrayOp(e.e1)) |
| 9000 |
return setError(); |
| 9001 |
|
| 9002 |
e.type = (sc && sc.inCfile) ? Type.tint32 : Type.tbool; |
| 9003 |
result = e; |
| 9004 |
} |
| 9005 |
|
| 9006 |
override void visit(DeleteExp exp) |
| 9007 |
{ |
| 9008 |
Expression e = exp; |
| 9009 |
|
| 9010 |
if (Expression ex = unaSemantic(exp, sc)) |
| 9011 |
{ |
| 9012 |
result = ex; |
| 9013 |
return; |
| 9014 |
} |
| 9015 |
exp.e1 = resolveProperties(sc, exp.e1); |
| 9016 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 9017 |
if (exp.e1.op == EXP.error) |
| 9018 |
{ |
| 9019 |
result = exp.e1; |
| 9020 |
return; |
| 9021 |
} |
| 9022 |
exp.type = Type.tvoid; |
| 9023 |
|
| 9024 |
Type tb = exp.e1.type.toBasetype(); |
| 9025 |
|
| 9026 |
/* Now that `delete` in user code is an error, we only get here when |
| 9027 |
* `isRAII` has been set to true for the deletion of a `scope class`. */ |
| 9028 |
if (tb.ty != Tclass) |
| 9029 |
{ |
| 9030 |
error(exp.loc, "cannot delete type `%s`", exp.e1.type.toChars()); |
| 9031 |
return setError(); |
| 9032 |
} |
| 9033 |
|
| 9034 |
ClassDeclaration cd = (cast(TypeClass)tb).sym; |
| 9035 |
if (cd.isCOMinterface()) |
| 9036 |
{ |
| 9037 |
/* Because COM classes are deleted by IUnknown.Release() |
| 9038 |
*/ |
| 9039 |
error(exp.loc, "cannot `delete` instance of COM interface `%s`", cd.toChars()); |
| 9040 |
return setError(); |
| 9041 |
} |
| 9042 |
|
| 9043 |
bool err = false; |
| 9044 |
if (cd.dtor) |
| 9045 |
{ |
| 9046 |
err |= !functionSemantic(cd.dtor); |
| 9047 |
err |= cd.dtor.checkPurity(exp.loc, sc); |
| 9048 |
err |= cd.dtor.checkSafety(exp.loc, sc); |
| 9049 |
err |= cd.dtor.checkNogc(exp.loc, sc); |
| 9050 |
} |
| 9051 |
if (err) |
| 9052 |
return setError(); |
| 9053 |
|
| 9054 |
result = e; |
| 9055 |
} |
| 9056 |
|
| 9057 |
override void visit(CastExp exp) |
| 9058 |
{ |
| 9059 |
static if (LOGSEMANTIC) |
| 9060 |
{ |
| 9061 |
printf("CastExp::semantic('%s')\n", exp.toChars()); |
| 9062 |
} |
| 9063 |
//static int x; assert(++x < 10); |
| 9064 |
|
| 9065 |
if ((sc && sc.inCfile) && |
| 9066 |
exp.to && (exp.to.ty == Tident || exp.to.ty == Tsarray) && |
| 9067 |
(exp.e1.op == EXP.address || exp.e1.op == EXP.star || |
| 9068 |
exp.e1.op == EXP.uadd || exp.e1.op == EXP.negate)) |
| 9069 |
{ |
| 9070 |
/* Ambiguous cases arise from CParser if type-name is just an identifier. |
| 9071 |
* ( identifier ) cast-expression |
| 9072 |
* ( identifier [expression]) cast-expression |
| 9073 |
* If we determine that `identifier` is a variable, and cast-expression |
| 9074 |
* is one of the unary operators (& * + -), then rewrite this cast |
| 9075 |
* as a binary expression. |
| 9076 |
*/ |
| 9077 |
Loc loc = exp.loc; |
| 9078 |
Type t; |
| 9079 |
Expression e; |
| 9080 |
Dsymbol s; |
| 9081 |
exp.to.resolve(loc, sc, e, t, s); |
| 9082 |
if (e !is null) |
| 9083 |
{ |
| 9084 |
if (auto ex = exp.e1.isAddrExp()) // (ident) &exp -> (ident & exp) |
| 9085 |
result = new AndExp(loc, e, ex.e1); |
| 9086 |
else if (auto ex = exp.e1.isPtrExp()) // (ident) *exp -> (ident * exp) |
| 9087 |
result = new MulExp(loc, e, ex.e1); |
| 9088 |
else if (auto ex = exp.e1.isUAddExp()) // (ident) +exp -> (ident + exp) |
| 9089 |
result = new AddExp(loc, e, ex.e1); |
| 9090 |
else if (auto ex = exp.e1.isNegExp()) // (ident) -exp -> (ident - exp) |
| 9091 |
result = new MinExp(loc, e, ex.e1); |
| 9092 |
|
| 9093 |
assert(result); |
| 9094 |
result = result.expressionSemantic(sc); |
| 9095 |
return; |
| 9096 |
} |
| 9097 |
} |
| 9098 |
|
| 9099 |
if (exp.to) |
| 9100 |
{ |
| 9101 |
exp.to = exp.to.typeSemantic(exp.loc, sc); |
| 9102 |
if (exp.to == Type.terror) |
| 9103 |
return setError(); |
| 9104 |
|
| 9105 |
if (!exp.to.hasPointers()) |
| 9106 |
exp.setNoderefOperand(); |
| 9107 |
|
| 9108 |
// When e1 is a template lambda, this cast may instantiate it with |
| 9109 |
// the type 'to'. |
| 9110 |
exp.e1 = inferType(exp.e1, exp.to); |
| 9111 |
} |
| 9112 |
|
| 9113 |
if (auto e = unaSemantic(exp, sc)) |
| 9114 |
{ |
| 9115 |
result = e; |
| 9116 |
return; |
| 9117 |
} |
| 9118 |
|
| 9119 |
// https://issues.dlang.org/show_bug.cgi?id=24701 |
| 9120 |
auto r = checkNoreturnVarAccess(exp.e1); |
| 9121 |
if (r != exp.e1 && exp.to && !exp.to.isTypeNoreturn()) |
| 9122 |
{ |
| 9123 |
result = r; |
| 9124 |
return; |
| 9125 |
} |
| 9126 |
|
| 9127 |
if (exp.to && !exp.to.isTypeSArray() && !exp.to.isTypeFunction()) |
| 9128 |
exp.e1 = exp.e1.arrayFuncConv(sc); |
| 9129 |
|
| 9130 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 9131 |
if (exp.e1.op == EXP.type) |
| 9132 |
exp.e1 = resolveAliasThis(sc, exp.e1); |
| 9133 |
|
| 9134 |
auto e1x = resolveProperties(sc, exp.e1); |
| 9135 |
if (e1x.op == EXP.error) |
| 9136 |
{ |
| 9137 |
result = e1x; |
| 9138 |
return; |
| 9139 |
} |
| 9140 |
if (e1x.checkType()) |
| 9141 |
return setError(); |
| 9142 |
exp.e1 = e1x; |
| 9143 |
|
| 9144 |
if (!exp.e1.type) |
| 9145 |
{ |
| 9146 |
error(exp.loc, "cannot cast `%s`", exp.e1.toErrMsg()); |
| 9147 |
return setError(); |
| 9148 |
} |
| 9149 |
|
| 9150 |
// https://issues.dlang.org/show_bug.cgi?id=19954 |
| 9151 |
if (exp.e1.type.ty == Ttuple) |
| 9152 |
{ |
| 9153 |
if (exp.to) |
| 9154 |
{ |
| 9155 |
if (TypeTuple tt = exp.to.isTypeTuple()) |
| 9156 |
{ |
| 9157 |
if (exp.e1.type.implicitConvTo(tt)) |
| 9158 |
{ |
| 9159 |
result = exp.e1.castTo(sc, tt); |
| 9160 |
return; |
| 9161 |
} |
| 9162 |
} |
| 9163 |
} |
| 9164 |
TupleExp te = exp.e1.isTupleExp(); |
| 9165 |
if (te.exps.length == 1) |
| 9166 |
exp.e1 = (*te.exps)[0]; |
| 9167 |
} |
| 9168 |
|
| 9169 |
// only allow S(x) rewrite if cast specified S explicitly. |
| 9170 |
// See https://issues.dlang.org/show_bug.cgi?id=18545 |
| 9171 |
const bool allowImplicitConstruction = exp.to !is null; |
| 9172 |
|
| 9173 |
if (!exp.to) // Handle cast(const) and cast(immutable), etc. |
| 9174 |
{ |
| 9175 |
exp.to = exp.e1.type.castMod(exp.mod); |
| 9176 |
exp.to = exp.to.typeSemantic(exp.loc, sc); |
| 9177 |
|
| 9178 |
if (exp.to == Type.terror) |
| 9179 |
return setError(); |
| 9180 |
} |
| 9181 |
|
| 9182 |
if (exp.to.ty == Ttuple) |
| 9183 |
{ |
| 9184 |
error(exp.loc, "cannot cast `%s` of type `%s` to type sequence `%s`", exp.e1.toErrMsg(), exp.e1.type.toChars(), exp.to.toChars()); |
| 9185 |
return setError(); |
| 9186 |
} |
| 9187 |
|
| 9188 |
// cast(void) is used to mark e1 as unused, so it is safe |
| 9189 |
if (exp.to.ty == Tvoid) |
| 9190 |
{ |
| 9191 |
exp.type = exp.to; |
| 9192 |
result = exp; |
| 9193 |
return; |
| 9194 |
} |
| 9195 |
|
| 9196 |
if (!exp.to.equals(exp.e1.type) && exp.mod == cast(ubyte)~0) |
| 9197 |
{ |
| 9198 |
if (Expression e = exp.opOverloadCast(sc)) |
| 9199 |
{ |
| 9200 |
result = e.implicitCastTo(sc, exp.to); |
| 9201 |
return; |
| 9202 |
} |
| 9203 |
} |
| 9204 |
|
| 9205 |
Type t1b = exp.e1.type.toBasetype(); |
| 9206 |
Type tob = exp.to.toBasetype(); |
| 9207 |
|
| 9208 |
if (allowImplicitConstruction && tob.ty == Tstruct && !tob.equals(t1b)) |
| 9209 |
{ |
| 9210 |
/* Look to replace: |
| 9211 |
* cast(S)t |
| 9212 |
* with: |
| 9213 |
* S(t) |
| 9214 |
*/ |
| 9215 |
|
| 9216 |
// Rewrite as to.call(e1) |
| 9217 |
Expression e = new TypeExp(exp.loc, exp.to); |
| 9218 |
e = new CallExp(exp.loc, e, exp.e1); |
| 9219 |
e = e.trySemantic(sc); |
| 9220 |
if (e) |
| 9221 |
{ |
| 9222 |
result = e; |
| 9223 |
return; |
| 9224 |
} |
| 9225 |
} |
| 9226 |
|
| 9227 |
if (!t1b.equals(tob) && t1b.isStaticOrDynamicArray()) |
| 9228 |
{ |
| 9229 |
if (checkNonAssignmentArrayOp(exp.e1)) |
| 9230 |
return setError(); |
| 9231 |
} |
| 9232 |
|
| 9233 |
Expression ex = exp.e1.castTo(sc, exp.to); |
| 9234 |
if (ex.op == EXP.error) |
| 9235 |
{ |
| 9236 |
result = ex; |
| 9237 |
return; |
| 9238 |
} |
| 9239 |
|
| 9240 |
// Check for unsafe casts |
| 9241 |
string msg; |
| 9242 |
if (!isSafeCast(ex, t1b, tob, msg)) |
| 9243 |
{ |
| 9244 |
if (sc.setUnsafe(false, exp.loc, |
| 9245 |
"cast from `%s` to `%s`", exp.e1.type, exp.to)) |
| 9246 |
{ |
| 9247 |
if (msg.length) |
| 9248 |
errorSupplemental(exp.loc, "%.*s", msg.fTuple.expand); |
| 9249 |
return setError(); |
| 9250 |
} |
| 9251 |
} |
| 9252 |
else if (msg.length) // deprecated unsafe |
| 9253 |
{ |
| 9254 |
const err = sc.setUnsafePreview(FeatureState.default_, false, exp.loc, |
| 9255 |
"cast from `%s` to `%s`", exp.e1.type, exp.to); |
| 9256 |
// if message was printed |
| 9257 |
if (sc.func && sc.func.isSafeBypassingInference() && !sc.isDeprecated()) |
| 9258 |
deprecationSupplemental(exp.loc, "%.*s", msg.fTuple.expand); |
| 9259 |
if (err) |
| 9260 |
return setError(); |
| 9261 |
} |
| 9262 |
|
| 9263 |
// `object.__ArrayCast` is a rewrite of an old runtime hook `_d_arraycast`. `_d_arraycast` was not built |
| 9264 |
// to handle certain casts. Those casts which `object.__ArrayCast` does not support are filtered out. |
| 9265 |
// See `e2ir.toElemCast` for other types of casts. If `object.__ArrayCast` is improved to support more |
| 9266 |
// casts these conditions and potentially some logic in `e2ir.toElemCast` can be removed. |
| 9267 |
if (tob.ty == Tarray) |
| 9268 |
{ |
| 9269 |
// https://issues.dlang.org/show_bug.cgi?id=19840 |
| 9270 |
if (auto ad = isAggregate(t1b)) |
| 9271 |
{ |
| 9272 |
if (ad.aliasthis) |
| 9273 |
{ |
| 9274 |
Expression e = resolveAliasThis(sc, exp.e1); |
| 9275 |
e = new CastExp(exp.loc, e, exp.to); |
| 9276 |
result = e.expressionSemantic(sc); |
| 9277 |
return; |
| 9278 |
} |
| 9279 |
} |
| 9280 |
|
| 9281 |
if(t1b.ty == Tarray && exp.e1.op != EXP.arrayLiteral && sc.needsCodegen()) |
| 9282 |
{ |
| 9283 |
auto tFrom = t1b.nextOf(); |
| 9284 |
auto tTo = tob.nextOf(); |
| 9285 |
|
| 9286 |
// https://issues.dlang.org/show_bug.cgi?id=20130 |
| 9287 |
if (exp.e1.op != EXP.string_ || !ex.isStringExp) |
| 9288 |
{ |
| 9289 |
const uint fromSize = cast(uint)tFrom.size(); |
| 9290 |
const uint toSize = cast(uint)tTo.size(); |
| 9291 |
if (fromSize == SIZE_INVALID || toSize == SIZE_INVALID) |
| 9292 |
return setError(); |
| 9293 |
|
| 9294 |
// If array element sizes do not match, we must adjust the dimensions |
| 9295 |
if (fromSize != toSize) |
| 9296 |
{ |
| 9297 |
if (!verifyHookExist(exp.loc, *sc, Id.__ArrayCast, "casting array of structs")) |
| 9298 |
return setError(); |
| 9299 |
|
| 9300 |
// A runtime check is needed in case arrays don't line up. That check should |
| 9301 |
// be done in the implementation of `object.__ArrayCast` |
| 9302 |
if (toSize == 0 || (fromSize % toSize) != 0) |
| 9303 |
{ |
| 9304 |
// lower to `object.__ArrayCast!(TFrom, TTo)(from)` |
| 9305 |
|
| 9306 |
// fully qualify as `object.__ArrayCast` |
| 9307 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 9308 |
auto dotid = new DotIdExp(exp.loc, id, Id.object); |
| 9309 |
|
| 9310 |
auto tiargs = new Objects(); |
| 9311 |
tiargs.push(tFrom); |
| 9312 |
tiargs.push(tTo); |
| 9313 |
auto dt = new DotTemplateInstanceExp(exp.loc, dotid, Id.__ArrayCast, tiargs); |
| 9314 |
|
| 9315 |
auto arguments = new Expressions(); |
| 9316 |
arguments.push(exp.e1); |
| 9317 |
Expression ce = new CallExp(exp.loc, dt, arguments); |
| 9318 |
|
| 9319 |
result = expressionSemantic(ce, sc); |
| 9320 |
return; |
| 9321 |
} |
| 9322 |
} |
| 9323 |
} |
| 9324 |
} |
| 9325 |
} |
| 9326 |
|
| 9327 |
if (sc && sc.inCfile) |
| 9328 |
{ |
| 9329 |
/* C11 6.5.4-5: A cast does not yield an lvalue. |
| 9330 |
* So ensure that castTo does not strip away the cast so that this |
| 9331 |
* can be enforced in other semantic visitor methods. |
| 9332 |
*/ |
| 9333 |
if (!ex.isCastExp()) |
| 9334 |
{ |
| 9335 |
ex = new CastExp(exp.loc, ex, exp.to); |
| 9336 |
ex.type = exp.to; |
| 9337 |
} |
| 9338 |
} |
| 9339 |
result = ex; |
| 9340 |
} |
| 9341 |
|
| 9342 |
override void visit(VectorExp exp) |
| 9343 |
{ |
| 9344 |
static if (LOGSEMANTIC) |
| 9345 |
{ |
| 9346 |
printf("VectorExp::semantic('%s')\n", exp.toChars()); |
| 9347 |
} |
| 9348 |
|
| 9349 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 9350 |
exp.type = exp.to.typeSemantic(exp.loc, sc); |
| 9351 |
if (exp.e1.op == EXP.error || exp.type.ty == Terror) |
| 9352 |
{ |
| 9353 |
result = exp.e1; |
| 9354 |
return; |
| 9355 |
} |
| 9356 |
|
| 9357 |
Type tb = exp.type.toBasetype(); |
| 9358 |
assert(tb.ty == Tvector); |
| 9359 |
TypeVector tv = cast(TypeVector)tb; |
| 9360 |
Type te = tv.elementType(); |
| 9361 |
exp.dim = cast(int)(tv.size(exp.loc) / te.size(exp.loc)); |
| 9362 |
|
| 9363 |
bool checkElem(Expression elem) |
| 9364 |
{ |
| 9365 |
if (elem.isConst() == 1) |
| 9366 |
return false; |
| 9367 |
|
| 9368 |
error(exp.loc, "constant expression expected, not `%s`", elem.toErrMsg()); |
| 9369 |
return true; |
| 9370 |
} |
| 9371 |
|
| 9372 |
exp.e1 = exp.e1.optimize(WANTvalue); |
| 9373 |
bool res; |
| 9374 |
if (exp.e1.op == EXP.arrayLiteral) |
| 9375 |
{ |
| 9376 |
foreach (i; 0 .. exp.dim) |
| 9377 |
{ |
| 9378 |
// Do not stop on first error - check all AST nodes even if error found |
| 9379 |
res |= checkElem(exp.e1.isArrayLiteralExp()[i]); |
| 9380 |
} |
| 9381 |
} |
| 9382 |
else if (exp.e1.type.ty == Tvoid) |
| 9383 |
checkElem(exp.e1); |
| 9384 |
|
| 9385 |
result = res ? ErrorExp.get() : exp; |
| 9386 |
} |
| 9387 |
|
| 9388 |
override void visit(VectorArrayExp e) |
| 9389 |
{ |
| 9390 |
static if (LOGSEMANTIC) |
| 9391 |
{ |
| 9392 |
printf("VectorArrayExp::semantic('%s')\n", e.toChars()); |
| 9393 |
} |
| 9394 |
if (!e.type) |
| 9395 |
{ |
| 9396 |
unaSemantic(e, sc); |
| 9397 |
e.e1 = resolveProperties(sc, e.e1); |
| 9398 |
|
| 9399 |
if (e.e1.op == EXP.error) |
| 9400 |
{ |
| 9401 |
result = e.e1; |
| 9402 |
return; |
| 9403 |
} |
| 9404 |
assert(e.e1.type.ty == Tvector); |
| 9405 |
e.type = e.e1.type.isTypeVector().basetype; |
| 9406 |
} |
| 9407 |
result = e; |
| 9408 |
} |
| 9409 |
|
| 9410 |
override void visit(SliceExp exp) |
| 9411 |
{ |
| 9412 |
static if (LOGSEMANTIC) |
| 9413 |
{ |
| 9414 |
printf("SliceExp::semantic('%s')\n", exp.toChars()); |
| 9415 |
} |
| 9416 |
|
| 9417 |
// operator overloading should be handled in ArrayExp already. |
| 9418 |
if (Expression ex = unaSemantic(exp, sc)) |
| 9419 |
{ |
| 9420 |
result = ex; |
| 9421 |
return; |
| 9422 |
} |
| 9423 |
exp.e1 = resolveProperties(sc, exp.e1); |
| 9424 |
if (exp.e1.op == EXP.type && exp.e1.type.ty != Ttuple) |
| 9425 |
{ |
| 9426 |
if (exp.lwr || exp.upr) |
| 9427 |
{ |
| 9428 |
error(exp.loc, "cannot slice type `%s`", exp.e1.toErrMsg()); |
| 9429 |
return setError(); |
| 9430 |
} |
| 9431 |
Expression e = new TypeExp(exp.loc, exp.e1.type.arrayOf()); |
| 9432 |
result = e.expressionSemantic(sc); |
| 9433 |
return; |
| 9434 |
} |
| 9435 |
if (!exp.lwr && !exp.upr) |
| 9436 |
{ |
| 9437 |
if (exp.e1.op == EXP.arrayLiteral) |
| 9438 |
{ |
| 9439 |
// Convert [a,b,c][] to [a,b,c] |
| 9440 |
Type t1b = exp.e1.type.toBasetype(); |
| 9441 |
Expression e = exp.e1; |
| 9442 |
if (t1b.ty == Tsarray) |
| 9443 |
{ |
| 9444 |
e = e.copy(); |
| 9445 |
e.type = t1b.nextOf().arrayOf(); |
| 9446 |
} |
| 9447 |
result = e; |
| 9448 |
return; |
| 9449 |
} |
| 9450 |
if (exp.e1.op == EXP.slice) |
| 9451 |
{ |
| 9452 |
// Convert e[][] to e[] |
| 9453 |
SliceExp se = cast(SliceExp)exp.e1; |
| 9454 |
if (!se.lwr && !se.upr) |
| 9455 |
{ |
| 9456 |
result = se; |
| 9457 |
return; |
| 9458 |
} |
| 9459 |
} |
| 9460 |
if (isArrayOpOperand(exp.e1)) |
| 9461 |
{ |
| 9462 |
// Convert (a[]+b[])[] to a[]+b[] |
| 9463 |
result = exp.e1; |
| 9464 |
return; |
| 9465 |
} |
| 9466 |
} |
| 9467 |
if (exp.e1.op == EXP.error) |
| 9468 |
{ |
| 9469 |
result = exp.e1; |
| 9470 |
return; |
| 9471 |
} |
| 9472 |
if (exp.e1.type.ty == Terror) |
| 9473 |
return setError(); |
| 9474 |
|
| 9475 |
Type t1b = exp.e1.type.toBasetype(); |
| 9476 |
if (auto tp = t1b.isTypePointer()) |
| 9477 |
{ |
| 9478 |
if (t1b.isPtrToFunction()) |
| 9479 |
{ |
| 9480 |
error(exp.loc, "cannot slice function pointer `%s`", exp.e1.toErrMsg()); |
| 9481 |
return setError(); |
| 9482 |
} |
| 9483 |
if (!exp.lwr || !exp.upr) |
| 9484 |
{ |
| 9485 |
error(exp.loc, "upper and lower bounds are needed to slice a pointer"); |
| 9486 |
if (auto ad = isAggregate(tp.next.toBasetype())) |
| 9487 |
{ |
| 9488 |
auto s = search_function(ad, Id.opIndex); |
| 9489 |
if (!s) s = search_function(ad, Id.opSlice); |
| 9490 |
if (s) |
| 9491 |
{ |
| 9492 |
auto fd = s.isFuncDeclaration(); |
| 9493 |
if ((fd && !fd.getParameterList().length) || s.isTemplateDeclaration()) |
| 9494 |
{ |
| 9495 |
errorSupplemental(exp.loc, |
| 9496 |
"pointer `%s` points to an aggregate that defines an `%s`, perhaps you meant `(*%s)[]`", |
| 9497 |
exp.e1.toErrMsg(), |
| 9498 |
s.ident.toChars(), |
| 9499 |
exp.e1.toErrMsg() |
| 9500 |
); |
| 9501 |
} |
| 9502 |
|
| 9503 |
} |
| 9504 |
} |
| 9505 |
|
| 9506 |
return setError(); |
| 9507 |
} |
| 9508 |
if (sc.setUnsafe(false, exp.loc, "pointer slicing")) |
| 9509 |
return setError(); |
| 9510 |
} |
| 9511 |
else if (t1b.ty == Tarray) |
| 9512 |
{ |
| 9513 |
} |
| 9514 |
else if (t1b.ty == Tsarray) |
| 9515 |
{ |
| 9516 |
} |
| 9517 |
else if (t1b.ty == Ttuple) |
| 9518 |
{ |
| 9519 |
if (!exp.lwr && !exp.upr) |
| 9520 |
{ |
| 9521 |
result = exp.e1; |
| 9522 |
return; |
| 9523 |
} |
| 9524 |
if (!exp.lwr || !exp.upr) |
| 9525 |
{ |
| 9526 |
error(exp.loc, "need upper and lower bound to slice a sequence"); |
| 9527 |
return setError(); |
| 9528 |
} |
| 9529 |
} |
| 9530 |
else if (t1b.ty == Tvector && exp.e1.isLvalue()) |
| 9531 |
{ |
| 9532 |
// Convert e1 to corresponding static array |
| 9533 |
TypeVector tv1 = cast(TypeVector)t1b; |
| 9534 |
t1b = tv1.basetype; |
| 9535 |
t1b = t1b.castMod(tv1.mod); |
| 9536 |
exp.e1.type = t1b; |
| 9537 |
} |
| 9538 |
else |
| 9539 |
{ |
| 9540 |
error(exp.loc, "`%s` cannot be sliced with `[]`", t1b.ty == Tvoid ? exp.e1.toErrMsg() : t1b.toChars()); |
| 9541 |
return setError(); |
| 9542 |
} |
| 9543 |
|
| 9544 |
/* Run semantic on lwr and upr. |
| 9545 |
*/ |
| 9546 |
Scope* scx = sc; |
| 9547 |
if (t1b.isStaticOrDynamicArray() || t1b.ty == Ttuple) |
| 9548 |
{ |
| 9549 |
// Create scope for 'length' variable |
| 9550 |
ScopeDsymbol sym = new ArrayScopeSymbol(sc, exp); |
| 9551 |
sym.parent = sc.scopesym; |
| 9552 |
sc = sc.push(sym); |
| 9553 |
} |
| 9554 |
if (exp.lwr) |
| 9555 |
{ |
| 9556 |
if (t1b.ty == Ttuple) |
| 9557 |
sc = sc.startCTFE(); |
| 9558 |
exp.lwr = exp.lwr.expressionSemantic(sc); |
| 9559 |
exp.lwr = resolveProperties(sc, exp.lwr); |
| 9560 |
if (t1b.ty == Ttuple) |
| 9561 |
sc = sc.endCTFE(); |
| 9562 |
exp.lwr = exp.lwr.implicitCastTo(sc, Type.tsize_t); |
| 9563 |
} |
| 9564 |
if (exp.upr) |
| 9565 |
{ |
| 9566 |
if (t1b.ty == Ttuple) |
| 9567 |
sc = sc.startCTFE(); |
| 9568 |
exp.upr = exp.upr.expressionSemantic(sc); |
| 9569 |
exp.upr = resolveProperties(sc, exp.upr); |
| 9570 |
if (t1b.ty == Ttuple) |
| 9571 |
sc = sc.endCTFE(); |
| 9572 |
exp.upr = exp.upr.implicitCastTo(sc, Type.tsize_t); |
| 9573 |
} |
| 9574 |
if (sc != scx) |
| 9575 |
sc = sc.pop(); |
| 9576 |
if (exp.lwr && exp.lwr.type == Type.terror || exp.upr && exp.upr.type == Type.terror) |
| 9577 |
return setError(); |
| 9578 |
|
| 9579 |
if (t1b.ty == Ttuple) |
| 9580 |
{ |
| 9581 |
exp.lwr = exp.lwr.ctfeInterpret(); |
| 9582 |
exp.upr = exp.upr.ctfeInterpret(); |
| 9583 |
uinteger_t i1 = exp.lwr.toUInteger(); |
| 9584 |
uinteger_t i2 = exp.upr.toUInteger(); |
| 9585 |
|
| 9586 |
TupleExp te; |
| 9587 |
TypeTuple tup; |
| 9588 |
size_t length; |
| 9589 |
if (exp.e1.op == EXP.tuple) // slicing an expression tuple |
| 9590 |
{ |
| 9591 |
te = cast(TupleExp)exp.e1; |
| 9592 |
tup = null; |
| 9593 |
length = te.exps.length; |
| 9594 |
} |
| 9595 |
else if (exp.e1.op == EXP.type) // slicing a type tuple |
| 9596 |
{ |
| 9597 |
te = null; |
| 9598 |
tup = cast(TypeTuple)t1b; |
| 9599 |
length = Parameter.dim(tup.arguments); |
| 9600 |
} |
| 9601 |
else |
| 9602 |
assert(0); |
| 9603 |
|
| 9604 |
if (i2 < i1 || length < i2) |
| 9605 |
{ |
| 9606 |
error(exp.loc, "string slice `[%llu .. %llu]` is out of bounds", i1, i2); |
| 9607 |
return setError(); |
| 9608 |
} |
| 9609 |
|
| 9610 |
size_t j1 = cast(size_t)i1; |
| 9611 |
size_t j2 = cast(size_t)i2; |
| 9612 |
Expression e; |
| 9613 |
if (exp.e1.op == EXP.tuple) |
| 9614 |
{ |
| 9615 |
auto exps = new Expressions(j2 - j1); |
| 9616 |
for (size_t i = 0; i < j2 - j1; i++) |
| 9617 |
{ |
| 9618 |
(*exps)[i] = (*te.exps)[j1 + i]; |
| 9619 |
} |
| 9620 |
e = new TupleExp(exp.loc, te.e0, exps); |
| 9621 |
} |
| 9622 |
else |
| 9623 |
{ |
| 9624 |
auto args = new Parameters(); |
| 9625 |
args.reserve(j2 - j1); |
| 9626 |
for (size_t i = j1; i < j2; i++) |
| 9627 |
{ |
| 9628 |
Parameter arg = Parameter.getNth(tup.arguments, i); |
| 9629 |
args.push(arg); |
| 9630 |
} |
| 9631 |
e = new TypeExp(exp.e1.loc, new TypeTuple(args)); |
| 9632 |
} |
| 9633 |
e = e.expressionSemantic(sc); |
| 9634 |
result = e; |
| 9635 |
return; |
| 9636 |
} |
| 9637 |
|
| 9638 |
exp.type = t1b.nextOf().arrayOf(); |
| 9639 |
// Allow typedef[] -> typedef[] |
| 9640 |
if (exp.type.equals(t1b)) |
| 9641 |
exp.type = exp.e1.type; |
| 9642 |
|
| 9643 |
// We might know $ now |
| 9644 |
setLengthVarIfKnown(exp.lengthVar, t1b); |
| 9645 |
|
| 9646 |
if (exp.lwr && exp.upr) |
| 9647 |
{ |
| 9648 |
exp.lwr = exp.lwr.optimize(WANTvalue); |
| 9649 |
exp.upr = exp.upr.optimize(WANTvalue); |
| 9650 |
|
| 9651 |
IntRange lwrRange = getIntRange(exp.lwr); |
| 9652 |
IntRange uprRange = getIntRange(exp.upr); |
| 9653 |
|
| 9654 |
if (t1b.isStaticOrDynamicArray()) |
| 9655 |
{ |
| 9656 |
Expression el = new ArrayLengthExp(exp.loc, exp.e1); |
| 9657 |
el = el.expressionSemantic(sc); |
| 9658 |
el = el.optimize(WANTvalue); |
| 9659 |
if (el.op == EXP.int64) |
| 9660 |
{ |
| 9661 |
// Array length is known at compile-time. Upper is in bounds if it fits length. |
| 9662 |
dinteger_t length = el.toInteger(); |
| 9663 |
auto bounds = IntRange(SignExtendedNumber(0), SignExtendedNumber(length)); |
| 9664 |
exp.upperIsInBounds = bounds.contains(uprRange); |
| 9665 |
} |
| 9666 |
else if (exp.upr.op == EXP.int64 && exp.upr.toInteger() == 0) |
| 9667 |
{ |
| 9668 |
// Upper slice expression is '0'. Value is always in bounds. |
| 9669 |
exp.upperIsInBounds = true; |
| 9670 |
} |
| 9671 |
else if (exp.upr.op == EXP.variable && (cast(VarExp)exp.upr).var.ident == Id.dollar) |
| 9672 |
{ |
| 9673 |
// Upper slice expression is '$'. Value is always in bounds. |
| 9674 |
exp.upperIsInBounds = true; |
| 9675 |
} |
| 9676 |
} |
| 9677 |
else if (t1b.ty == Tpointer) |
| 9678 |
{ |
| 9679 |
exp.upperIsInBounds = true; |
| 9680 |
} |
| 9681 |
else |
| 9682 |
assert(0); |
| 9683 |
|
| 9684 |
exp.lowerIsLessThanUpper = (lwrRange.imax <= uprRange.imin); |
| 9685 |
|
| 9686 |
//printf("upperIsInBounds = %d lowerIsLessThanUpper = %d\n", exp.upperIsInBounds, exp.lowerIsLessThanUpper); |
| 9687 |
} |
| 9688 |
|
| 9689 |
result = exp; |
| 9690 |
} |
| 9691 |
|
| 9692 |
override void visit(ArrayLengthExp e) |
| 9693 |
{ |
| 9694 |
static if (LOGSEMANTIC) |
| 9695 |
{ |
| 9696 |
printf("ArrayLengthExp::semantic('%s')\n", e.toChars()); |
| 9697 |
} |
| 9698 |
|
| 9699 |
if (Expression ex = unaSemantic(e, sc)) |
| 9700 |
{ |
| 9701 |
result = ex; |
| 9702 |
return; |
| 9703 |
} |
| 9704 |
e.e1 = resolveProperties(sc, e.e1); |
| 9705 |
|
| 9706 |
e.type = Type.tsize_t; |
| 9707 |
result = e; |
| 9708 |
} |
| 9709 |
|
| 9710 |
override void visit(ArrayExp exp) |
| 9711 |
{ |
| 9712 |
static if (LOGSEMANTIC) |
| 9713 |
{ |
| 9714 |
printf("ArrayExp::semantic('%s')\n", exp.toChars()); |
| 9715 |
} |
| 9716 |
|
| 9717 |
if (sc.inCfile) |
| 9718 |
{ |
| 9719 |
/* See if need to rewrite the AST because of cast/call ambiguity |
| 9720 |
*/ |
| 9721 |
if (auto e = castCallAmbiguity(exp, sc)) |
| 9722 |
{ |
| 9723 |
result = expressionSemantic(e, sc); |
| 9724 |
return; |
| 9725 |
} |
| 9726 |
} |
| 9727 |
|
| 9728 |
result = exp.carraySemantic(sc); // C semantics |
| 9729 |
if (result) |
| 9730 |
return; |
| 9731 |
|
| 9732 |
if (Expression e = exp.opOverloadArray(sc)) |
| 9733 |
{ |
| 9734 |
result = e; |
| 9735 |
return; |
| 9736 |
} |
| 9737 |
|
| 9738 |
if (auto ad = isAggregate(exp.e1.type)) |
| 9739 |
{ |
| 9740 |
if (exp.arguments.length == 0) |
| 9741 |
{ |
| 9742 |
error(exp.loc, "no `[]` operator overload for type `%s`", exp.e1.type.toChars()); |
| 9743 |
errorSupplemental(ad.loc, "perhaps define `auto opIndex() {}` for `%s`", ad.toPrettyChars()); |
| 9744 |
} |
| 9745 |
else |
| 9746 |
{ |
| 9747 |
const(char)* typeString(Expression exp) |
| 9748 |
{ |
| 9749 |
if (auto e = exp.trySemantic(sc)) |
| 9750 |
return e.type.toChars(); |
| 9751 |
else |
| 9752 |
return "__error__"; |
| 9753 |
} |
| 9754 |
|
| 9755 |
if (auto ie = (*exp.arguments)[0].isIntervalExp()) |
| 9756 |
{ |
| 9757 |
error(exp.loc, "no `[%s]` operator overload for type `%s`", ie.toChars(), exp.e1.type.toChars()); |
| 9758 |
errorSupplemental(ad.loc, "perhaps define `auto opSlice(%s lower, %s upper) {}` for `%s`", |
| 9759 |
typeString(ie.lwr), typeString(ie.upr), ad.toPrettyChars()); |
| 9760 |
} |
| 9761 |
else |
| 9762 |
{ |
| 9763 |
OutBuffer buf; |
| 9764 |
buf.printf("%s", typeString((*exp.arguments)[0])); |
| 9765 |
foreach (e; (*exp.arguments)[1 .. $]) |
| 9766 |
buf.printf(", %s", typeString(e)); |
| 9767 |
|
| 9768 |
error(exp.loc, "no `[]` operator overload for type `%s`", exp.e1.type.toChars()); |
| 9769 |
errorSupplemental(ad.loc, "perhaps define `auto opIndex(%s) {}` for `%s`", |
| 9770 |
buf.extractChars, ad.toPrettyChars()); |
| 9771 |
} |
| 9772 |
} |
| 9773 |
} |
| 9774 |
else if (exp.e1.op == EXP.type && exp.e1.type.ty != Ttuple) |
| 9775 |
error(exp.loc, "static array of `%s` with multiple lengths not allowed", exp.e1.type.toChars()); |
| 9776 |
else if (isIndexableNonAggregate(exp.e1.type)) |
| 9777 |
error(exp.loc, "only one index allowed to index `%s`", exp.e1.type.toChars()); |
| 9778 |
else |
| 9779 |
error(exp.loc, "cannot use `[]` operator on expression of type `%s`", exp.e1.type.toChars()); |
| 9780 |
|
| 9781 |
result = ErrorExp.get(); |
| 9782 |
} |
| 9783 |
|
| 9784 |
override void visit(DotExp exp) |
| 9785 |
{ |
| 9786 |
static if (LOGSEMANTIC) |
| 9787 |
{ |
| 9788 |
printf("DotExp::semantic('%s')\n", exp.toChars()); |
| 9789 |
if (exp.type) |
| 9790 |
printf("\ttype = %s\n", exp.type.toChars()); |
| 9791 |
} |
| 9792 |
exp.e1 = exp.e1.expressionSemantic(sc); |
| 9793 |
exp.e2 = exp.e2.expressionSemantic(sc); |
| 9794 |
|
| 9795 |
if (exp.e1.op == EXP.type) |
| 9796 |
{ |
| 9797 |
result = exp.e2; |
| 9798 |
return; |
| 9799 |
} |
| 9800 |
if (exp.e2.op == EXP.type) |
| 9801 |
{ |
| 9802 |
result = exp.e2; |
| 9803 |
return; |
| 9804 |
} |
| 9805 |
if (auto te = exp.e2.isTemplateExp()) |
| 9806 |
{ |
| 9807 |
Expression e = new DotTemplateExp(exp.loc, exp.e1, te.td); |
| 9808 |
result = e.expressionSemantic(sc); |
| 9809 |
return; |
| 9810 |
} |
| 9811 |
if (!exp.type) |
| 9812 |
exp.type = exp.e2.type; |
| 9813 |
result = exp; |
| 9814 |
} |
| 9815 |
|
| 9816 |
override void visit(CommaExp e) |
| 9817 |
{ |
| 9818 |
//printf("Semantic.CommaExp() %s\n", e.toChars()); |
| 9819 |
|
| 9820 |
// Allow `((a,b),(x,y))` |
| 9821 |
if (e.allowCommaExp) |
| 9822 |
{ |
| 9823 |
CommaExp.allow(e.e1); |
| 9824 |
CommaExp.allow(e.e2); |
| 9825 |
} |
| 9826 |
|
| 9827 |
if (Expression ex = binSemanticProp(e, sc)) |
| 9828 |
{ |
| 9829 |
result = ex; |
| 9830 |
return; |
| 9831 |
} |
| 9832 |
e.e1 = e.e1.addDtorHook(sc); |
| 9833 |
|
| 9834 |
if (checkNonAssignmentArrayOp(e.e1)) |
| 9835 |
return setError(); |
| 9836 |
|
| 9837 |
// Comma expressions trigger this conversion |
| 9838 |
e.e2 = e.e2.arrayFuncConv(sc); |
| 9839 |
|
| 9840 |
e.type = e.e2.type; |
| 9841 |
result = e; |
| 9842 |
|
| 9843 |
if (sc.inCfile) |
| 9844 |
return; |
| 9845 |
|
| 9846 |
if (!e.isGenerated) |
| 9847 |
{ |
| 9848 |
if (e.allowCommaExp) |
| 9849 |
{ |
| 9850 |
checkMustUse(e.e1, sc); |
| 9851 |
discardValue(e.e1); |
| 9852 |
} |
| 9853 |
else |
| 9854 |
error(e.loc, "using the result of a comma expression is not allowed"); |
| 9855 |
} |
| 9856 |
} |
| 9857 |
|
| 9858 |
override void visit(IntervalExp e) |
| 9859 |
{ |
| 9860 |
static if (LOGSEMANTIC) |
| 9861 |
{ |
| 9862 |
printf("IntervalExp::semantic('%s')\n", e.toChars()); |
| 9863 |
} |
| 9864 |
|
| 9865 |
Expression le = e.lwr; |
| 9866 |
le = le.expressionSemantic(sc); |
| 9867 |
le = resolveProperties(sc, le); |
| 9868 |
|
| 9869 |
Expression ue = e.upr; |
| 9870 |
ue = ue.expressionSemantic(sc); |
| 9871 |
ue = resolveProperties(sc, ue); |
| 9872 |
|
| 9873 |
if (le.op == EXP.error) |
| 9874 |
{ |
| 9875 |
result = le; |
| 9876 |
return; |
| 9877 |
} |
| 9878 |
if (ue.op == EXP.error) |
| 9879 |
{ |
| 9880 |
result = ue; |
| 9881 |
return; |
| 9882 |
} |
| 9883 |
|
| 9884 |
e.lwr = le; |
| 9885 |
e.upr = ue; |
| 9886 |
|
| 9887 |
e.type = Type.tvoid; |
| 9888 |
result = e; |
| 9889 |
} |
| 9890 |
|
| 9891 |
override void visit(DelegatePtrExp e) |
| 9892 |
{ |
| 9893 |
static if (LOGSEMANTIC) |
| 9894 |
{ |
| 9895 |
printf("DelegatePtrExp::semantic('%s')\n", e.toChars()); |
| 9896 |
} |
| 9897 |
if (!e.type) |
| 9898 |
{ |
| 9899 |
unaSemantic(e, sc); |
| 9900 |
e.e1 = resolveProperties(sc, e.e1); |
| 9901 |
|
| 9902 |
if (e.e1.op == EXP.error) |
| 9903 |
{ |
| 9904 |
result = e.e1; |
| 9905 |
return; |
| 9906 |
} |
| 9907 |
e.type = Type.tvoidptr; |
| 9908 |
} |
| 9909 |
result = e; |
| 9910 |
} |
| 9911 |
|
| 9912 |
override void visit(DelegateFuncptrExp e) |
| 9913 |
{ |
| 9914 |
static if (LOGSEMANTIC) |
| 9915 |
{ |
| 9916 |
printf("DelegateFuncptrExp::semantic('%s')\n", e.toChars()); |
| 9917 |
} |
| 9918 |
if (!e.type) |
| 9919 |
{ |
| 9920 |
unaSemantic(e, sc); |
| 9921 |
e.e1 = resolveProperties(sc, e.e1); |
| 9922 |
if (e.e1.op == EXP.error) |
| 9923 |
{ |
| 9924 |
result = e.e1; |
| 9925 |
return; |
| 9926 |
} |
| 9927 |
e.type = e.e1.type.nextOf().pointerTo(); |
| 9928 |
} |
| 9929 |
result = e; |
| 9930 |
} |
| 9931 |
|
| 9932 |
override void visit(IndexExp exp) |
| 9933 |
{ |
| 9934 |
static if (LOGSEMANTIC) |
| 9935 |
{ |
| 9936 |
printf("IndexExp::semantic('%s')\n", exp.toChars()); |
| 9937 |
} |
| 9938 |
|
| 9939 |
// operator overloading should be handled in ArrayExp already. |
| 9940 |
if (!exp.e1.type) |
| 9941 |
exp.e1 = exp.e1.expressionSemantic(sc).arrayFuncConv(sc); |
| 9942 |
assert(exp.e1.type); // semantic() should already be run on it |
| 9943 |
if (exp.e1.op == EXP.type && exp.e1.type.ty != Ttuple) |
| 9944 |
{ |
| 9945 |
exp.e2 = exp.e2.expressionSemantic(sc); |
| 9946 |
exp.e2 = resolveProperties(sc, exp.e2); |
| 9947 |
Type nt; |
| 9948 |
if (exp.e2.op == EXP.type) |
| 9949 |
nt = new TypeAArray(exp.e1.type, exp.e2.type); |
| 9950 |
else |
| 9951 |
nt = new TypeSArray(exp.e1.type, exp.e2); |
| 9952 |
Expression e = new TypeExp(exp.loc, nt); |
| 9953 |
result = e.expressionSemantic(sc); |
| 9954 |
return; |
| 9955 |
} |
| 9956 |
if (exp.e1.op == EXP.error) |
| 9957 |
{ |
| 9958 |
result = exp.e1; |
| 9959 |
return; |
| 9960 |
} |
| 9961 |
if (exp.e1.type.ty == Terror) |
| 9962 |
return setError(); |
| 9963 |
|
| 9964 |
// Note that unlike C we do not implement the int[ptr] |
| 9965 |
|
| 9966 |
Type t1b = exp.e1.type.toBasetype(); |
| 9967 |
|
| 9968 |
if (TypeVector tv1 = t1b.isTypeVector()) |
| 9969 |
{ |
| 9970 |
// Convert e1 to corresponding static array |
| 9971 |
t1b = tv1.basetype; |
| 9972 |
t1b = t1b.castMod(tv1.mod); |
| 9973 |
exp.e1 = exp.e1.castTo(sc, t1b); |
| 9974 |
} |
| 9975 |
if (t1b.isStaticOrDynamicArray()) |
| 9976 |
{ |
| 9977 |
if (!checkAddressable(exp, sc)) |
| 9978 |
return setError(); |
| 9979 |
} |
| 9980 |
|
| 9981 |
/* Run semantic on e2 |
| 9982 |
*/ |
| 9983 |
Scope* scx = sc; |
| 9984 |
if (t1b.isStaticOrDynamicArray() || t1b.ty == Ttuple) |
| 9985 |
{ |
| 9986 |
// Create scope for 'length' variable |
| 9987 |
ScopeDsymbol sym = new ArrayScopeSymbol(sc, exp); |
| 9988 |
sym.parent = sc.scopesym; |
| 9989 |
sc = sc.push(sym); |
| 9990 |
} |
| 9991 |
if (t1b.ty == Ttuple) |
| 9992 |
sc = sc.startCTFE(); |
| 9993 |
exp.e2 = exp.e2.expressionSemantic(sc).arrayFuncConv(sc); |
| 9994 |
exp.e2 = resolveProperties(sc, exp.e2); |
| 9995 |
if (t1b.ty == Ttuple) |
| 9996 |
sc = sc.endCTFE(); |
| 9997 |
if (exp.e2.op == EXP.tuple) |
| 9998 |
{ |
| 9999 |
TupleExp te = cast(TupleExp)exp.e2; |
| 10000 |
if (te.exps && te.exps.length == 1) |
| 10001 |
exp.e2 = Expression.combine(te.e0, (*te.exps)[0]); // bug 4444 fix |
| 10002 |
} |
| 10003 |
if (sc != scx) |
| 10004 |
sc = sc.pop(); |
| 10005 |
if (exp.e2.type == Type.terror) |
| 10006 |
return setError(); |
| 10007 |
|
| 10008 |
if (checkNonAssignmentArrayOp(exp.e1)) |
| 10009 |
return setError(); |
| 10010 |
|
| 10011 |
switch (t1b.ty) |
| 10012 |
{ |
| 10013 |
case Tpointer: |
| 10014 |
if (t1b.isPtrToFunction()) |
| 10015 |
{ |
| 10016 |
error(exp.loc, "cannot index function pointer `%s`", exp.e1.toErrMsg()); |
| 10017 |
return setError(); |
| 10018 |
} |
| 10019 |
exp.e2 = exp.e2.implicitCastTo(sc, Type.tsize_t); |
| 10020 |
if (exp.e2.type == Type.terror) |
| 10021 |
return setError(); |
| 10022 |
exp.e2 = exp.e2.optimize(WANTvalue); |
| 10023 |
if (exp.e2.op == EXP.int64 && exp.e2.toInteger() == 0) |
| 10024 |
{ |
| 10025 |
} |
| 10026 |
else if (sc.setUnsafe(false, exp.loc, "indexing pointer `%s`", exp.e1)) |
| 10027 |
{ |
| 10028 |
return setError(); |
| 10029 |
} |
| 10030 |
exp.type = (cast(TypeNext)t1b).next; |
| 10031 |
break; |
| 10032 |
|
| 10033 |
case Tarray: |
| 10034 |
exp.e2 = exp.e2.implicitCastTo(sc, Type.tsize_t); |
| 10035 |
if (exp.e2.type == Type.terror) |
| 10036 |
return setError(); |
| 10037 |
exp.type = (cast(TypeNext)t1b).next; |
| 10038 |
break; |
| 10039 |
|
| 10040 |
case Tsarray: |
| 10041 |
{ |
| 10042 |
exp.e2 = exp.e2.implicitCastTo(sc, Type.tsize_t); |
| 10043 |
if (exp.e2.type == Type.terror) |
| 10044 |
return setError(); |
| 10045 |
exp.type = t1b.nextOf(); |
| 10046 |
break; |
| 10047 |
} |
| 10048 |
case Taarray: |
| 10049 |
{ |
| 10050 |
TypeAArray taa = cast(TypeAArray)t1b; |
| 10051 |
/* We can skip the implicit conversion if they differ only by |
| 10052 |
* constness |
| 10053 |
* https://issues.dlang.org/show_bug.cgi?id=2684 |
| 10054 |
* see also bug https://issues.dlang.org/show_bug.cgi?id=2954 b |
| 10055 |
*/ |
| 10056 |
if (!arrayTypeCompatibleWithoutCasting(exp.e2.type, taa.index)) |
| 10057 |
{ |
| 10058 |
exp.e2 = exp.e2.implicitCastTo(sc, taa.index); // type checking |
| 10059 |
if (exp.e2.type == Type.terror) |
| 10060 |
return setError(); |
| 10061 |
} |
| 10062 |
|
| 10063 |
semanticTypeInfo(sc, taa); |
| 10064 |
checkNewEscape(*sc, exp.e2, false); |
| 10065 |
|
| 10066 |
exp.type = taa.next; |
| 10067 |
break; |
| 10068 |
} |
| 10069 |
case Ttuple: |
| 10070 |
{ |
| 10071 |
exp.e2 = exp.e2.implicitCastTo(sc, Type.tsize_t); |
| 10072 |
if (exp.e2.type == Type.terror) |
| 10073 |
return setError(); |
| 10074 |
|
| 10075 |
exp.e2 = exp.e2.ctfeInterpret(); |
| 10076 |
uinteger_t index = exp.e2.toUInteger(); |
| 10077 |
|
| 10078 |
TupleExp te; |
| 10079 |
TypeTuple tup; |
| 10080 |
size_t length; |
| 10081 |
if (exp.e1.op == EXP.tuple) |
| 10082 |
{ |
| 10083 |
te = cast(TupleExp)exp.e1; |
| 10084 |
tup = null; |
| 10085 |
length = te.exps.length; |
| 10086 |
} |
| 10087 |
else if (exp.e1.op == EXP.type) |
| 10088 |
{ |
| 10089 |
te = null; |
| 10090 |
tup = cast(TypeTuple)t1b; |
| 10091 |
length = Parameter.dim(tup.arguments); |
| 10092 |
} |
| 10093 |
else |
| 10094 |
assert(0); |
| 10095 |
|
| 10096 |
if (length <= index) |
| 10097 |
{ |
| 10098 |
error(exp.loc, "sequence index `[%llu]` is outside bounds `[0 .. %llu]`", index, cast(ulong)length); |
| 10099 |
return setError(); |
| 10100 |
} |
| 10101 |
Expression e; |
| 10102 |
if (exp.e1.op == EXP.tuple) |
| 10103 |
{ |
| 10104 |
e = (*te.exps)[cast(size_t)index]; |
| 10105 |
e = Expression.combine(te.e0, e); |
| 10106 |
} |
| 10107 |
else |
| 10108 |
e = new TypeExp(exp.e1.loc, Parameter.getNth(tup.arguments, cast(size_t)index).type); |
| 10109 |
result = e; |
| 10110 |
return; |
| 10111 |
} |
| 10112 |
default: |
| 10113 |
error(exp.loc, "`%s` must be an array or pointer type, not `%s`", exp.e1.toErrMsg(), exp.e1.type.toChars()); |
| 10114 |
return setError(); |
| 10115 |
} |
| 10116 |
|
| 10117 |
// We might know $ now |
| 10118 |
setLengthVarIfKnown(exp.lengthVar, t1b); |
| 10119 |
|
| 10120 |
if (t1b.isStaticOrDynamicArray()) |
| 10121 |
{ |
| 10122 |
Expression el = new ArrayLengthExp(exp.loc, exp.e1); |
| 10123 |
el = el.expressionSemantic(sc); |
| 10124 |
el = el.optimize(WANTvalue); |
| 10125 |
if (el.op == EXP.int64) |
| 10126 |
{ |
| 10127 |
exp.e2 = exp.e2.optimize(WANTvalue); |
| 10128 |
dinteger_t length = el.toInteger(); |
| 10129 |
if (length) |
| 10130 |
{ |
| 10131 |
auto bounds = IntRange(SignExtendedNumber(0), SignExtendedNumber(length - 1)); |
| 10132 |
// OR it in, because it might already be set for C array indexing |
| 10133 |
exp.indexIsInBounds |= bounds.contains(getIntRange(exp.e2)); |
| 10134 |
} |
| 10135 |
else if (sc.inCfile && t1b.ty == Tsarray) |
| 10136 |
{ |
| 10137 |
if (auto ve = exp.e1.isVarExp()) |
| 10138 |
{ |
| 10139 |
/* Rewrite 0-length C array ve[exp.e2] as *(ve + exp.e2) |
| 10140 |
*/ |
| 10141 |
auto vp = ve.castTo(sc, t1b.isTypeSArray().next.pointerTo()); |
| 10142 |
auto e = new AddExp(exp.loc, vp, exp.e2); |
| 10143 |
auto pe = new PtrExp(exp.loc, e); |
| 10144 |
result = pe.expressionSemantic(sc).optimize(WANTvalue); |
| 10145 |
return; |
| 10146 |
} |
| 10147 |
} |
| 10148 |
} |
| 10149 |
} |
| 10150 |
|
| 10151 |
result = exp; |
| 10152 |
} |
| 10153 |
|
| 10154 |
override void visit(PostExp exp) |
| 10155 |
{ |
| 10156 |
static if (LOGSEMANTIC) |
| 10157 |
{ |
| 10158 |
printf("PostExp::semantic('%s')\n", exp.toChars()); |
| 10159 |
} |
| 10160 |
|
| 10161 |
if (sc.inCfile) |
| 10162 |
{ |
| 10163 |
/* See if need to rewrite the AST because of cast/call ambiguity |
| 10164 |
*/ |
| 10165 |
if (auto e = castCallAmbiguity(exp, sc)) |
| 10166 |
{ |
| 10167 |
result = expressionSemantic(e, sc); |
| 10168 |
return; |
| 10169 |
} |
| 10170 |
} |
| 10171 |
|
| 10172 |
if (Expression ex = binSemantic(exp, sc)) |
| 10173 |
{ |
| 10174 |
result = ex; |
| 10175 |
return; |
| 10176 |
} |
| 10177 |
Expression e1x = resolveProperties(sc, exp.e1); |
| 10178 |
if (e1x.op == EXP.error) |
| 10179 |
{ |
| 10180 |
result = e1x; |
| 10181 |
return; |
| 10182 |
} |
| 10183 |
exp.e1 = e1x; |
| 10184 |
|
| 10185 |
if (exp.e1.checkReadModifyWrite(exp.op)) |
| 10186 |
return setError(); |
| 10187 |
|
| 10188 |
if (exp.e1.op == EXP.slice) |
| 10189 |
{ |
| 10190 |
const(char)* s = exp.op == EXP.plusPlus ? "increment" : "decrement"; |
| 10191 |
error(exp.loc, "cannot post-%s array slice `%s`, use pre-%s instead", s, exp.e1.toErrMsg(), s); |
| 10192 |
return setError(); |
| 10193 |
} |
| 10194 |
|
| 10195 |
Type t1 = exp.e1.type.toBasetype(); |
| 10196 |
if (t1.ty == Tclass || t1.ty == Tstruct || exp.e1.op == EXP.arrayLength) |
| 10197 |
{ |
| 10198 |
/* Check for operator overloading, |
| 10199 |
* but rewrite in terms of ++e instead of e++ |
| 10200 |
*/ |
| 10201 |
|
| 10202 |
/* If e1 is not trivial, take a reference to it |
| 10203 |
*/ |
| 10204 |
Expression de = null; |
| 10205 |
if (exp.e1.op != EXP.variable && exp.e1.op != EXP.arrayLength) |
| 10206 |
{ |
| 10207 |
// ref v = e1; |
| 10208 |
auto v = copyToTemp(STC.ref_, "__postref", exp.e1); |
| 10209 |
de = new DeclarationExp(exp.loc, v); |
| 10210 |
exp.e1 = new VarExp(exp.e1.loc, v); |
| 10211 |
} |
| 10212 |
|
| 10213 |
/* Rewrite as: |
| 10214 |
* auto tmp = e1; ++e1; tmp |
| 10215 |
*/ |
| 10216 |
auto tmp = copyToTemp(STC.none, "__pitmp", exp.e1); |
| 10217 |
Expression ea = new DeclarationExp(exp.loc, tmp); |
| 10218 |
|
| 10219 |
Expression eb = exp.e1.syntaxCopy(); |
| 10220 |
eb = new PreExp(exp.op == EXP.plusPlus ? EXP.prePlusPlus : EXP.preMinusMinus, exp.loc, eb); |
| 10221 |
|
| 10222 |
Expression ec = new VarExp(exp.loc, tmp); |
| 10223 |
|
| 10224 |
// Combine de,ea,eb,ec |
| 10225 |
if (de) |
| 10226 |
ea = new CommaExp(exp.loc, de, ea); |
| 10227 |
Expression e = new CommaExp(exp.loc, ea, eb); |
| 10228 |
e = new CommaExp(exp.loc, e, ec); |
| 10229 |
e = e.expressionSemantic(sc); |
| 10230 |
result = e; |
| 10231 |
return; |
| 10232 |
} |
| 10233 |
|
| 10234 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 10235 |
exp.e1 = exp.e1.optimize(WANTvalue, /*keepLvalue*/ true); |
| 10236 |
|
| 10237 |
Expression e = exp; |
| 10238 |
if (exp.e1.checkScalar() || |
| 10239 |
exp.e1.checkSharedAccess(sc)) |
| 10240 |
return setError(); |
| 10241 |
if (exp.e1.checkNoBool()) |
| 10242 |
return setError(); |
| 10243 |
|
| 10244 |
if (exp.e1.type.ty == Tpointer) |
| 10245 |
e = scaleFactor(exp, sc); |
| 10246 |
else |
| 10247 |
exp.e2 = exp.e2.castTo(sc, exp.e1.type); |
| 10248 |
e.type = exp.e1.type; |
| 10249 |
result = e; |
| 10250 |
} |
| 10251 |
|
| 10252 |
override void visit(PreExp exp) |
| 10253 |
{ |
| 10254 |
// printf("PreExp::semantic('%s')\n", toChars()); |
| 10255 |
if (Expression e = exp.opOverloadUnary(sc)) |
| 10256 |
{ |
| 10257 |
result = e; |
| 10258 |
return; |
| 10259 |
} |
| 10260 |
|
| 10261 |
// Rewrite as e1+=1 or e1-=1 |
| 10262 |
Expression e; |
| 10263 |
if (exp.op == EXP.prePlusPlus) |
| 10264 |
e = new AddAssignExp(exp.loc, exp.e1, IntegerExp.literal!1); |
| 10265 |
else |
| 10266 |
e = new MinAssignExp(exp.loc, exp.e1, IntegerExp.literal!1); |
| 10267 |
result = e.expressionSemanticWithParent(sc, exp); |
| 10268 |
} |
| 10269 |
|
| 10270 |
/* |
| 10271 |
* Get the expression initializer for a specific struct |
| 10272 |
* |
| 10273 |
* Params: |
| 10274 |
* sd = the struct for which the expression initializer is needed |
| 10275 |
* loc = the location of the initializer |
| 10276 |
* sc = the scope where the expression is located |
| 10277 |
* t = the type of the expression |
| 10278 |
* |
| 10279 |
* Returns: |
| 10280 |
* The expression initializer or error expression if any errors occured |
| 10281 |
*/ |
| 10282 |
private Expression getInitExp(StructDeclaration sd, Loc loc, Scope* sc, Type t) |
| 10283 |
{ |
| 10284 |
if (sd.zeroInit && !sd.isNested()) |
| 10285 |
{ |
| 10286 |
// https://issues.dlang.org/show_bug.cgi?id=14606 |
| 10287 |
// Always use BlitExp for the special expression: (struct = 0) |
| 10288 |
return IntegerExp.literal!0; |
| 10289 |
} |
| 10290 |
|
| 10291 |
if (sd.isNested()) |
| 10292 |
{ |
| 10293 |
auto sle = new StructLiteralExp(loc, sd, null, t); |
| 10294 |
if (!sd.fill(loc, *sle.elements, true)) |
| 10295 |
return ErrorExp.get(); |
| 10296 |
if (checkFrameAccess(loc, sc, sd, sle.elements.length)) |
| 10297 |
return ErrorExp.get(); |
| 10298 |
|
| 10299 |
sle.type = t; |
| 10300 |
return sle; |
| 10301 |
} |
| 10302 |
|
| 10303 |
return t.defaultInit(loc); |
| 10304 |
} |
| 10305 |
|
| 10306 |
override void visit(AssignExp exp) |
| 10307 |
{ |
| 10308 |
static if (LOGSEMANTIC) |
| 10309 |
{ |
| 10310 |
if (exp.op == EXP.blit) printf("BlitExp.semantic('%s')\n", exp.toChars()); |
| 10311 |
if (exp.op == EXP.assign) printf("AssignExp.semantic('%s')\n", exp.toChars()); |
| 10312 |
if (exp.op == EXP.construct) printf("ConstructExp.semantic('%s')\n", exp.toChars()); |
| 10313 |
} |
| 10314 |
|
| 10315 |
void setResult(Expression e, int line = __LINE__) |
| 10316 |
{ |
| 10317 |
//printf("line %d\n", line); |
| 10318 |
result = e; |
| 10319 |
} |
| 10320 |
|
| 10321 |
Expression e1old = exp.e1; |
| 10322 |
|
| 10323 |
if (auto e2comma = exp.e2.isCommaExp()) |
| 10324 |
{ |
| 10325 |
if (!e2comma.isGenerated && !sc.inCfile) |
| 10326 |
error(exp.loc, "using the result of a comma expression is not allowed"); |
| 10327 |
|
| 10328 |
/* Rewrite to get rid of the comma from rvalue |
| 10329 |
* e1=(e0,e2) => e0,(e1=e2) |
| 10330 |
*/ |
| 10331 |
Expression e0; |
| 10332 |
exp.e2 = Expression.extractLast(e2comma, e0); |
| 10333 |
Expression e = Expression.combine(e0, exp); |
| 10334 |
return setResult(e.expressionSemantic(sc)); |
| 10335 |
} |
| 10336 |
|
| 10337 |
/* Look for operator overloading of a[arguments] = e2. |
| 10338 |
* Do it before e1.expressionSemantic() otherwise the ArrayExp will have been |
| 10339 |
* converted to unary operator overloading already. |
| 10340 |
*/ |
| 10341 |
if (auto ae = exp.e1.isArrayExp()) |
| 10342 |
{ |
| 10343 |
Expression res; |
| 10344 |
|
| 10345 |
ae.e1 = ae.e1.expressionSemantic(sc); |
| 10346 |
ae.e1 = resolveProperties(sc, ae.e1); |
| 10347 |
Expression ae1old = ae.e1; |
| 10348 |
|
| 10349 |
const(bool) maybeSlice = |
| 10350 |
(ae.arguments.length == 0 || |
| 10351 |
ae.arguments.length == 1 && (*ae.arguments)[0].op == EXP.interval); |
| 10352 |
|
| 10353 |
IntervalExp ie = null; |
| 10354 |
if (maybeSlice && ae.arguments.length) |
| 10355 |
{ |
| 10356 |
assert((*ae.arguments)[0].op == EXP.interval); |
| 10357 |
ie = cast(IntervalExp)(*ae.arguments)[0]; |
| 10358 |
} |
| 10359 |
Type att = null; // first cyclic `alias this` type |
| 10360 |
while (true) |
| 10361 |
{ |
| 10362 |
if (ae.e1.op == EXP.error) |
| 10363 |
return setResult(ae.e1); |
| 10364 |
|
| 10365 |
Expression e0 = null; |
| 10366 |
Expression ae1save = ae.e1; |
| 10367 |
ae.lengthVar = null; |
| 10368 |
|
| 10369 |
Type t1b = ae.e1.type.toBasetype(); |
| 10370 |
AggregateDeclaration ad = isAggregate(t1b); |
| 10371 |
if (!ad) |
| 10372 |
break; |
| 10373 |
if (search_function(ad, Id.opIndexAssign)) |
| 10374 |
{ |
| 10375 |
// Deal with $ |
| 10376 |
res = resolveOpDollar(sc, ae, e0); |
| 10377 |
if (!res) // a[i..j] = e2 might be: a.opSliceAssign(e2, i, j) |
| 10378 |
goto Lfallback; |
| 10379 |
if (res.op == EXP.error) |
| 10380 |
return setResult(res); |
| 10381 |
|
| 10382 |
res = exp.e2.expressionSemantic(sc); |
| 10383 |
if (res.op == EXP.error) |
| 10384 |
return setResult(res); |
| 10385 |
exp.e2 = res; |
| 10386 |
|
| 10387 |
/* Rewrite (a[arguments] = e2) as: |
| 10388 |
* a.opIndexAssign(e2, arguments) |
| 10389 |
*/ |
| 10390 |
Expressions* a = ae.arguments.copy(); |
| 10391 |
a.insert(0, exp.e2); |
| 10392 |
res = new DotIdExp(exp.loc, ae.e1, Id.opIndexAssign); |
| 10393 |
res = new CallExp(exp.loc, res, a); |
| 10394 |
if (maybeSlice) // a[] = e2 might be: a.opSliceAssign(e2) |
| 10395 |
res = res.trySemantic(sc); |
| 10396 |
else |
| 10397 |
res = res.expressionSemantic(sc); |
| 10398 |
if (res) |
| 10399 |
return setResult(Expression.combine(e0, res)); |
| 10400 |
} |
| 10401 |
|
| 10402 |
Lfallback: |
| 10403 |
if (maybeSlice && search_function(ad, Id.opSliceAssign)) |
| 10404 |
{ |
| 10405 |
// Deal with $ |
| 10406 |
res = resolveOpDollar(sc, ae, ie, e0); |
| 10407 |
if (res.op == EXP.error) |
| 10408 |
return setResult(res); |
| 10409 |
|
| 10410 |
res = exp.e2.expressionSemantic(sc); |
| 10411 |
if (res.op == EXP.error) |
| 10412 |
return setResult(res); |
| 10413 |
|
| 10414 |
exp.e2 = res; |
| 10415 |
|
| 10416 |
/* Rewrite (a[i..j] = e2) as: |
| 10417 |
* a.opSliceAssign(e2, i, j) |
| 10418 |
*/ |
| 10419 |
auto a = new Expressions(); |
| 10420 |
a.push(exp.e2); |
| 10421 |
if (ie) |
| 10422 |
{ |
| 10423 |
a.push(ie.lwr); |
| 10424 |
a.push(ie.upr); |
| 10425 |
} |
| 10426 |
res = new DotIdExp(exp.loc, ae.e1, Id.opSliceAssign); |
| 10427 |
res = new CallExp(exp.loc, res, a); |
| 10428 |
res = res.expressionSemantic(sc); |
| 10429 |
return setResult(Expression.combine(e0, res)); |
| 10430 |
} |
| 10431 |
|
| 10432 |
// No operator overloading member function found yet, but |
| 10433 |
// there might be an alias this to try. |
| 10434 |
if (ad.aliasthis && !isRecursiveAliasThis(att, ae.e1.type)) |
| 10435 |
{ |
| 10436 |
/* Rewrite (a[arguments] op e2) as: |
| 10437 |
* a.aliasthis[arguments] op e2 |
| 10438 |
*/ |
| 10439 |
ae.e1 = resolveAliasThis(sc, ae1save, true); |
| 10440 |
if (ae.e1) |
| 10441 |
continue; |
| 10442 |
} |
| 10443 |
break; |
| 10444 |
} |
| 10445 |
ae.e1 = ae1old; // recovery |
| 10446 |
ae.lengthVar = null; |
| 10447 |
} |
| 10448 |
|
| 10449 |
/* Run this.e1 semantic. |
| 10450 |
*/ |
| 10451 |
{ |
| 10452 |
Expression e1x = exp.e1; |
| 10453 |
|
| 10454 |
/* With UFCS, e.f = value |
| 10455 |
* Could mean: |
| 10456 |
* .f(e, value) |
| 10457 |
* or: |
| 10458 |
* .f(e) = value |
| 10459 |
*/ |
| 10460 |
if (auto dti = e1x.isDotTemplateInstanceExp()) |
| 10461 |
{ |
| 10462 |
Expression e = dti.dotTemplateSemanticProp(sc, DotExpFlag.gag); |
| 10463 |
if (!e) |
| 10464 |
{ |
| 10465 |
return setResult(resolveUFCSProperties(sc, e1x, exp.e2)); |
| 10466 |
} |
| 10467 |
|
| 10468 |
e1x = e; |
| 10469 |
} |
| 10470 |
else if (sc.inCfile && e1x.isDotIdExp()) |
| 10471 |
{ |
| 10472 |
auto die = e1x.isDotIdExp(); |
| 10473 |
e1x = fieldLookup(die.e1, sc, die.ident, die.arrow); |
| 10474 |
} |
| 10475 |
else if (auto die = e1x.isDotIdExp()) |
| 10476 |
{ |
| 10477 |
Expression e = die.dotIdSemanticProp(sc, 1); |
| 10478 |
if (e && isDotOpDispatch(e)) |
| 10479 |
{ |
| 10480 |
/* https://issues.dlang.org/show_bug.cgi?id=19687 |
| 10481 |
* |
| 10482 |
* On this branch, e2 is semantically analyzed in resolvePropertiesX, |
| 10483 |
* but that call is done with gagged errors. That is the only time when |
| 10484 |
* semantic gets ran on e2, that is why the error never gets to be printed. |
| 10485 |
* In order to make sure that UFCS is tried with correct parameters, e2 |
| 10486 |
* needs to have semantic ran on it. |
| 10487 |
*/ |
| 10488 |
auto ode = e; |
| 10489 |
exp.e2 = exp.e2.expressionSemantic(sc); |
| 10490 |
const errors = global.startGagging(); |
| 10491 |
e = resolvePropertiesX(sc, e, exp.e2); |
| 10492 |
// Any error or if 'e' is not resolved, go to UFCS |
| 10493 |
if (global.endGagging(errors) || e is ode) |
| 10494 |
e = null; /* fall down to UFCS */ |
| 10495 |
else |
| 10496 |
return setResult(e); |
| 10497 |
} |
| 10498 |
if (!e) |
| 10499 |
return setResult(resolveUFCSProperties(sc, e1x, exp.e2)); |
| 10500 |
e1x = e; |
| 10501 |
} |
| 10502 |
else |
| 10503 |
{ |
| 10504 |
if (auto se = e1x.isSliceExp()) |
| 10505 |
se.arrayop = true; |
| 10506 |
|
| 10507 |
e1x = e1x.expressionSemantic(sc); |
| 10508 |
} |
| 10509 |
|
| 10510 |
/* We have f = value. |
| 10511 |
* Could mean: |
| 10512 |
* f(value) |
| 10513 |
* or: |
| 10514 |
* f() = value |
| 10515 |
*/ |
| 10516 |
if (Expression e = resolvePropertiesX(sc, e1x, exp.e2, &aliasThisStop)) |
| 10517 |
return setResult(e); |
| 10518 |
|
| 10519 |
if (e1x.checkRightThis(sc)) |
| 10520 |
{ |
| 10521 |
return setError(); |
| 10522 |
} |
| 10523 |
exp.e1 = e1x; |
| 10524 |
assert(exp.e1.type); |
| 10525 |
} |
| 10526 |
Type t1 = exp.e1.type.isTypeEnum() ? exp.e1.type : exp.e1.type.toBasetype(); |
| 10527 |
|
| 10528 |
/* Run this.e2 semantic. |
| 10529 |
* Different from other binary expressions, the analysis of e2 |
| 10530 |
* depends on the result of e1 in assignments. |
| 10531 |
*/ |
| 10532 |
{ |
| 10533 |
Expression e2x = inferType(exp.e2, t1.baseElemOf()); |
| 10534 |
e2x = e2x.expressionSemantic(sc); |
| 10535 |
if (!t1.isTypeSArray()) |
| 10536 |
e2x = e2x.arrayFuncConv(sc); |
| 10537 |
e2x = resolveProperties(sc, e2x); |
| 10538 |
if (e2x.op == EXP.type) |
| 10539 |
e2x = resolveAliasThis(sc, e2x); //https://issues.dlang.org/show_bug.cgi?id=17684 |
| 10540 |
if (e2x.op == EXP.error) |
| 10541 |
return setResult(e2x); |
| 10542 |
// We delay checking the value for structs/classes as these might have |
| 10543 |
// an opAssign defined. |
| 10544 |
if ((t1.ty != Tstruct && t1.ty != Tclass && e2x.checkValue()) || |
| 10545 |
e2x.checkSharedAccess(sc)) |
| 10546 |
return setError(); |
| 10547 |
|
| 10548 |
auto etmp = checkNoreturnVarAccess(e2x); |
| 10549 |
if (etmp != e2x) |
| 10550 |
return setResult(etmp); |
| 10551 |
|
| 10552 |
exp.e2 = e2x; |
| 10553 |
} |
| 10554 |
|
| 10555 |
/* Rewrite tuple assignment as a tuple of assignments. |
| 10556 |
*/ |
| 10557 |
{ |
| 10558 |
Expression e2x = exp.e2; |
| 10559 |
|
| 10560 |
Ltupleassign: |
| 10561 |
if (exp.e1.op == EXP.tuple && e2x.op == EXP.tuple) |
| 10562 |
{ |
| 10563 |
TupleExp tup1 = cast(TupleExp)exp.e1; |
| 10564 |
TupleExp tup2 = cast(TupleExp)e2x; |
| 10565 |
size_t dim = tup1.exps.length; |
| 10566 |
Expression e = null; |
| 10567 |
if (dim != tup2.exps.length) |
| 10568 |
{ |
| 10569 |
error(exp.loc, "mismatched sequence lengths, %d and %d", cast(int)dim, cast(int)tup2.exps.length); |
| 10570 |
return setError(); |
| 10571 |
} |
| 10572 |
if (dim == 0) |
| 10573 |
{ |
| 10574 |
e = IntegerExp.literal!0; |
| 10575 |
e = new CastExp(exp.loc, e, Type.tvoid); // avoid "has no effect" error |
| 10576 |
e = Expression.combine(tup1.e0, tup2.e0, e); |
| 10577 |
} |
| 10578 |
else |
| 10579 |
{ |
| 10580 |
auto exps = new Expressions(dim); |
| 10581 |
for (size_t i = 0; i < dim; i++) |
| 10582 |
{ |
| 10583 |
Expression ex1 = (*tup1.exps)[i]; |
| 10584 |
Expression ex2 = (*tup2.exps)[i]; |
| 10585 |
(*exps)[i] = new AssignExp(exp.loc, ex1, ex2); |
| 10586 |
} |
| 10587 |
e = new TupleExp(exp.loc, Expression.combine(tup1.e0, tup2.e0), exps); |
| 10588 |
} |
| 10589 |
return setResult(e.expressionSemantic(sc)); |
| 10590 |
} |
| 10591 |
|
| 10592 |
/* Look for form: e1 = e2.aliasthis. |
| 10593 |
*/ |
| 10594 |
if (exp.e1.op == EXP.tuple) |
| 10595 |
{ |
| 10596 |
TupleDeclaration td = isAliasThisTuple(e2x); |
| 10597 |
if (!td) |
| 10598 |
goto Lnomatch; |
| 10599 |
|
| 10600 |
assert(exp.e1.type.ty == Ttuple); |
| 10601 |
TypeTuple tt = cast(TypeTuple)exp.e1.type; |
| 10602 |
|
| 10603 |
Expression e0; |
| 10604 |
Expression ev = extractSideEffect(sc, "__tup", e0, e2x); |
| 10605 |
|
| 10606 |
auto iexps = new Expressions(); |
| 10607 |
iexps.push(ev); |
| 10608 |
for (size_t u = 0; u < iexps.length; u++) |
| 10609 |
{ |
| 10610 |
Lexpand: |
| 10611 |
Expression e = (*iexps)[u]; |
| 10612 |
|
| 10613 |
Parameter arg = Parameter.getNth(tt.arguments, u); |
| 10614 |
//printf("[%d] iexps.length = %d, ", u, iexps.length); |
| 10615 |
//printf("e = (%s %s, %s), ", Token.toChars[e.op], e.toChars(), e.type.toChars()); |
| 10616 |
//printf("arg = (%s, %s)\n", arg.toChars(), arg.type.toChars()); |
| 10617 |
|
| 10618 |
if (!arg || !e.type.implicitConvTo(arg.type)) |
| 10619 |
{ |
| 10620 |
// expand initializer to tuple |
| 10621 |
if (expandAliasThisTuples(iexps, u) != -1) |
| 10622 |
{ |
| 10623 |
if (iexps.length <= u) |
| 10624 |
break; |
| 10625 |
goto Lexpand; |
| 10626 |
} |
| 10627 |
goto Lnomatch; |
| 10628 |
} |
| 10629 |
} |
| 10630 |
e2x = new TupleExp(e2x.loc, e0, iexps); |
| 10631 |
e2x = e2x.expressionSemantic(sc); |
| 10632 |
if (e2x.op == EXP.error) |
| 10633 |
{ |
| 10634 |
result = e2x; |
| 10635 |
return; |
| 10636 |
} |
| 10637 |
// Do not need to overwrite this.e2 |
| 10638 |
goto Ltupleassign; |
| 10639 |
} |
| 10640 |
Lnomatch: |
| 10641 |
} |
| 10642 |
|
| 10643 |
/* Inside constructor, if this is the first assignment of object field, |
| 10644 |
* rewrite this to initializing the field. |
| 10645 |
*/ |
| 10646 |
if (exp.op == EXP.assign |
| 10647 |
&& exp.e1.checkModifiable(sc) == Modifiable.initialization) |
| 10648 |
{ |
| 10649 |
// Check common mistake of misspelled parameters in constructors, |
| 10650 |
// e.g. `this(int feild) { this.field = field; }` |
| 10651 |
if (auto dve1 = exp.e1.isDotVarExp) |
| 10652 |
if (auto dve2 = exp.e2.isDotVarExp) |
| 10653 |
if (sc.func && sc.func.parameters && dve1.e1.isThisExp && dve2.e1.isThisExp() |
| 10654 |
&& dve1.var.ident.equals(dve2.var.ident)) |
| 10655 |
{ |
| 10656 |
// @@@DEPRECATED_2.121@@@ |
| 10657 |
// Deprecated in 2.111, make it an error in 2.121 |
| 10658 |
deprecation(exp.e1.loc, "cannot initialize field `%s` with itself", dve1.var.toChars()); |
| 10659 |
auto findParameter(const(char)[] s, ref int cost) |
| 10660 |
{ |
| 10661 |
foreach (p; *sc.func.parameters) |
| 10662 |
{ |
| 10663 |
if (p.ident.toString == s) |
| 10664 |
{ |
| 10665 |
cost = 1; |
| 10666 |
return p.ident.toString; |
| 10667 |
} |
| 10668 |
} |
| 10669 |
return null; |
| 10670 |
} |
| 10671 |
import dmd.root.speller : speller; |
| 10672 |
if (auto s = speller!findParameter(dve1.var.ident.toString)) |
| 10673 |
{ |
| 10674 |
deprecationSupplemental(sc.func.loc, "did you mean to use parameter `%.*s`?\n", s.fTuple.expand); |
| 10675 |
} |
| 10676 |
} |
| 10677 |
|
| 10678 |
//printf("[%s] change to init - %s\n", exp.loc.toChars(), exp.toChars()); |
| 10679 |
auto t = exp.type; |
| 10680 |
exp = new ConstructExp(exp.loc, exp.e1, exp.e2); |
| 10681 |
exp.type = t; |
| 10682 |
|
| 10683 |
// https://issues.dlang.org/show_bug.cgi?id=13515 |
| 10684 |
// set Index::modifiable flag for complex AA element initialization |
| 10685 |
if (auto ie1 = exp.e1.isIndexExp()) |
| 10686 |
{ |
| 10687 |
Expression e1x = ie1.markSettingAAElem(); |
| 10688 |
if (e1x.op == EXP.error) |
| 10689 |
{ |
| 10690 |
result = e1x; |
| 10691 |
return; |
| 10692 |
} |
| 10693 |
} |
| 10694 |
} |
| 10695 |
else if (exp.op == EXP.construct && exp.e1.op == EXP.variable && |
| 10696 |
(cast(VarExp)exp.e1).var.storage_class & (STC.out_ | STC.ref_)) |
| 10697 |
{ |
| 10698 |
exp.memset = MemorySet.referenceInit; |
| 10699 |
} |
| 10700 |
|
| 10701 |
if (exp.op == EXP.assign) // skip EXP.blit and EXP.construct, which are initializations |
| 10702 |
{ |
| 10703 |
exp.e1.checkSharedAccess(sc); |
| 10704 |
checkUnsafeAccess(sc, exp.e1, false, true); |
| 10705 |
} |
| 10706 |
|
| 10707 |
checkUnsafeAccess(sc, exp.e2, true, true); // Initializer must always be checked |
| 10708 |
|
| 10709 |
/* If it is an assignment from a 'foreign' type, |
| 10710 |
* check for operator overloading. |
| 10711 |
*/ |
| 10712 |
if (exp.memset == MemorySet.referenceInit) |
| 10713 |
{ |
| 10714 |
// If this is an initialization of a reference, |
| 10715 |
// do nothing |
| 10716 |
} |
| 10717 |
else if (t1.ty == Tstruct) |
| 10718 |
{ |
| 10719 |
auto e1x = exp.e1; |
| 10720 |
auto e2x = exp.e2; |
| 10721 |
auto sd = (cast(TypeStruct)t1).sym; |
| 10722 |
|
| 10723 |
if (exp.op == EXP.construct) |
| 10724 |
{ |
| 10725 |
Type t2 = e2x.type.toBasetype(); |
| 10726 |
if (t2.ty == Tstruct && sd == (cast(TypeStruct)t2).sym) |
| 10727 |
{ |
| 10728 |
sd.size(exp.loc); |
| 10729 |
if (sd.sizeok != Sizeok.done) |
| 10730 |
return setError(); |
| 10731 |
if (!sd.ctor) |
| 10732 |
sd.ctor = sd.searchCtor(); |
| 10733 |
|
| 10734 |
// https://issues.dlang.org/show_bug.cgi?id=15661 |
| 10735 |
// Look for the form from last of comma chain. |
| 10736 |
auto e2y = lastComma(e2x); |
| 10737 |
|
| 10738 |
CallExp ce = (e2y.op == EXP.call) ? cast(CallExp)e2y : null; |
| 10739 |
DotVarExp dve = (ce && ce.e1.op == EXP.dotVariable) |
| 10740 |
? cast(DotVarExp)ce.e1 : null; |
| 10741 |
if (sd.ctor && ce && dve && dve.var.isCtorDeclaration() && |
| 10742 |
// https://issues.dlang.org/show_bug.cgi?id=19389 |
| 10743 |
dve.e1.op != EXP.dotVariable && |
| 10744 |
e2y.type.implicitConvTo(t1)) |
| 10745 |
{ |
| 10746 |
/* Look for form of constructor call which is: |
| 10747 |
* __ctmp.ctor(arguments...) |
| 10748 |
*/ |
| 10749 |
|
| 10750 |
/* Before calling the constructor, initialize |
| 10751 |
* variable with a bit copy of the default |
| 10752 |
* initializer |
| 10753 |
*/ |
| 10754 |
Expression einit = getInitExp(sd, exp.loc, sc, t1); |
| 10755 |
if (einit.op == EXP.error) |
| 10756 |
{ |
| 10757 |
result = einit; |
| 10758 |
return; |
| 10759 |
} |
| 10760 |
|
| 10761 |
auto ae = new BlitExp(exp.loc, exp.e1, einit); |
| 10762 |
ae.type = e1x.type; |
| 10763 |
|
| 10764 |
/* Replace __ctmp being constructed with e1. |
| 10765 |
* We need to copy constructor call expression, |
| 10766 |
* because it may be used in other place. |
| 10767 |
*/ |
| 10768 |
auto dvx = cast(DotVarExp)dve.copy(); |
| 10769 |
dvx.e1 = e1x; |
| 10770 |
auto cx = cast(CallExp)ce.copy(); |
| 10771 |
cx.e1 = dvx; |
| 10772 |
if (checkConstructorEscape(*sc, cx, false)) |
| 10773 |
return setError(); |
| 10774 |
|
| 10775 |
Expression e0; |
| 10776 |
Expression.extractLast(e2x, e0); |
| 10777 |
|
| 10778 |
auto e = Expression.combine(e0, ae, cx); |
| 10779 |
e = e.expressionSemantic(sc); |
| 10780 |
result = e; |
| 10781 |
return; |
| 10782 |
} |
| 10783 |
// https://issues.dlang.org/show_bug.cgi?id=21586 |
| 10784 |
// Rewrite CondExp or e1 will miss direct construction, e.g. |
| 10785 |
// e1 = a ? S(1) : ...; -> AST: e1 = a ? (S(0)).this(1) : ...; |
| 10786 |
// a temporary created and an extra destructor call. |
| 10787 |
// AST will be rewritten to: |
| 10788 |
// a ? e1 = 0, e1.this(1) : ...; -> blitting plus construction |
| 10789 |
if (e2x.op == EXP.question) |
| 10790 |
{ |
| 10791 |
/* Rewrite as: |
| 10792 |
* a ? e1 = b : e1 = c; |
| 10793 |
*/ |
| 10794 |
CondExp econd = cast(CondExp)e2x; |
| 10795 |
Expression ea1 = new ConstructExp(econd.e1.loc, e1x, econd.e1); |
| 10796 |
Expression ea2 = new ConstructExp(econd.e2.loc, e1x, econd.e2); |
| 10797 |
Expression e = new CondExp(exp.loc, econd.econd, ea1, ea2); |
| 10798 |
result = e.expressionSemantic(sc); |
| 10799 |
return; |
| 10800 |
} |
| 10801 |
if (sd.postblit || sd.hasCopyCtor) |
| 10802 |
{ |
| 10803 |
/* We have a copy constructor for this |
| 10804 |
*/ |
| 10805 |
|
| 10806 |
//printf("exp: %s\n", toChars(exp)); |
| 10807 |
//printf("e2x: %s\n", toChars(e2x)); |
| 10808 |
if (e2x.isLvalue()) |
| 10809 |
{ |
| 10810 |
if (sd.hasCopyCtor) |
| 10811 |
{ |
| 10812 |
/* Rewrite as: |
| 10813 |
* e1 = init, e1.copyCtor(e2); |
| 10814 |
*/ |
| 10815 |
Expression einit = new BlitExp(exp.loc, exp.e1, getInitExp(sd, exp.loc, sc, t1)); |
| 10816 |
einit.type = e1x.type; |
| 10817 |
|
| 10818 |
Expression e; |
| 10819 |
e = new DotIdExp(exp.loc, e1x, Id.ctor); |
| 10820 |
e = new CallExp(exp.loc, e, e2x); |
| 10821 |
e = new CommaExp(exp.loc, einit, e); |
| 10822 |
|
| 10823 |
//printf("e: %s\n", e.toChars()); |
| 10824 |
|
| 10825 |
result = e.expressionSemantic(sc); |
| 10826 |
return; |
| 10827 |
} |
| 10828 |
else |
| 10829 |
{ |
| 10830 |
if (!e2x.type.implicitConvTo(e1x.type)) |
| 10831 |
{ |
| 10832 |
error(exp.loc, "conversion error from `%s` to `%s`", |
| 10833 |
e2x.type.toChars(), e1x.type.toChars()); |
| 10834 |
return setError(); |
| 10835 |
} |
| 10836 |
|
| 10837 |
/* Rewrite as: |
| 10838 |
* (e1 = e2).postblit(); |
| 10839 |
* |
| 10840 |
* Blit assignment e1 = e2 returns a reference to the original e1, |
| 10841 |
* then call the postblit on it. |
| 10842 |
*/ |
| 10843 |
Expression e = e1x.copy(); |
| 10844 |
e.type = e.type.mutableOf(); |
| 10845 |
if (e.type.isShared && !sd.type.isShared) |
| 10846 |
e.type = e.type.unSharedOf(); |
| 10847 |
e = new BlitExp(exp.loc, e, e2x); |
| 10848 |
e = new DotVarExp(exp.loc, e, sd.postblit, false); |
| 10849 |
e = new CallExp(exp.loc, e); |
| 10850 |
result = e.expressionSemantic(sc); |
| 10851 |
return; |
| 10852 |
} |
| 10853 |
} |
| 10854 |
else if (sd.hasMoveCtor && (!e2x.isCallExp() || e2x.rvalue) && !e2x.isStructLiteralExp()) |
| 10855 |
{ |
| 10856 |
// #move |
| 10857 |
/* The !e2x.isCallExp() is because it is already an rvalue |
| 10858 |
and the move constructor is unnecessary: |
| 10859 |
struct S { |
| 10860 |
alias TT this; |
| 10861 |
long TT(); |
| 10862 |
this(T)(int x) {} |
| 10863 |
this(S); |
| 10864 |
this(ref S); |
| 10865 |
~this(); |
| 10866 |
} |
| 10867 |
S fun(ref S arg); |
| 10868 |
void test() { S st; fun(st); } |
| 10869 |
*/ |
| 10870 |
/* Rewrite as: |
| 10871 |
* e1 = init, e1.moveCtor(e2); |
| 10872 |
*/ |
| 10873 |
Expression einit = new BlitExp(exp.loc, exp.e1, getInitExp(sd, exp.loc, sc, t1)); |
| 10874 |
einit.type = e1x.type; |
| 10875 |
|
| 10876 |
Expression e; |
| 10877 |
e = new DotIdExp(exp.loc, e1x, Id.ctor); |
| 10878 |
e = new CallExp(exp.loc, e, e2x); |
| 10879 |
e = new CommaExp(exp.loc, einit, e); |
| 10880 |
|
| 10881 |
//printf("e: %s\n", e.toChars()); |
| 10882 |
|
| 10883 |
result = e.expressionSemantic(sc); |
| 10884 |
return; |
| 10885 |
} |
| 10886 |
else |
| 10887 |
{ |
| 10888 |
/* The struct value returned from the function is transferred |
| 10889 |
* so should not call the destructor on it. |
| 10890 |
*/ |
| 10891 |
e2x = valueNoDtor(e2x); |
| 10892 |
} |
| 10893 |
} |
| 10894 |
|
| 10895 |
// https://issues.dlang.org/show_bug.cgi?id=19251 |
| 10896 |
// if e2 cannot be converted to e1.type, maybe there is an alias this |
| 10897 |
if (!e2x.implicitConvTo(t1)) |
| 10898 |
{ |
| 10899 |
AggregateDeclaration ad2 = isAggregate(e2x.type); |
| 10900 |
if (ad2 && ad2.aliasthis && !isRecursiveAliasThis(aliasThisStop[1], exp.e2.type)) |
| 10901 |
{ |
| 10902 |
/* Rewrite (e1 op e2) as: |
| 10903 |
* (e1 op e2.aliasthis) |
| 10904 |
*/ |
| 10905 |
exp.e2 = new DotIdExp(exp.e2.loc, exp.e2, ad2.aliasthis.ident); |
| 10906 |
result = exp.expressionSemantic(sc, aliasThisStop); |
| 10907 |
return; |
| 10908 |
} |
| 10909 |
} |
| 10910 |
} |
| 10911 |
else if (!e2x.implicitConvTo(t1)) |
| 10912 |
{ |
| 10913 |
sd.size(exp.loc); |
| 10914 |
if (sd.sizeok != Sizeok.done) |
| 10915 |
return setError(); |
| 10916 |
if (!sd.ctor) |
| 10917 |
sd.ctor = sd.searchCtor(); |
| 10918 |
|
| 10919 |
if (sd.ctor) |
| 10920 |
{ |
| 10921 |
/* Look for implicit constructor call |
| 10922 |
* Rewrite as: |
| 10923 |
* e1 = init, e1.ctor(e2) |
| 10924 |
*/ |
| 10925 |
|
| 10926 |
/* Fix Issue 5153 : https://issues.dlang.org/show_bug.cgi?id=5153 |
| 10927 |
* Using `new` to initialize a struct object is a common mistake, but |
| 10928 |
* the error message from the compiler is not very helpful in that |
| 10929 |
* case. If exp.e2 is a NewExp and the type of new is the same as |
| 10930 |
* the type as exp.e1 (struct in this case), then we know for sure |
| 10931 |
* that the user wants to instantiate a struct. This is done to avoid |
| 10932 |
* issuing an error when the user actually wants to call a constructor |
| 10933 |
* which receives a class object. |
| 10934 |
* |
| 10935 |
* Foo f = new Foo2(0); is a valid expression if Foo has a constructor |
| 10936 |
* which receives an instance of a Foo2 class |
| 10937 |
*/ |
| 10938 |
if (exp.e2.op == EXP.new_) |
| 10939 |
{ |
| 10940 |
auto newExp = cast(NewExp)(exp.e2); |
| 10941 |
if (newExp.newtype && newExp.newtype == t1) |
| 10942 |
{ |
| 10943 |
error(exp.loc, "cannot implicitly convert expression `%s` of type `%s` to `%s`", |
| 10944 |
newExp.toErrMsg(), newExp.type.toChars(), t1.toChars()); |
| 10945 |
errorSupplemental(exp.loc, "Perhaps remove the `new` keyword?"); |
| 10946 |
return setError(); |
| 10947 |
} |
| 10948 |
} |
| 10949 |
|
| 10950 |
Expression einit = new BlitExp(exp.loc, e1x, getInitExp(sd, exp.loc, sc, t1)); |
| 10951 |
einit.type = e1x.type; |
| 10952 |
|
| 10953 |
Expression e; |
| 10954 |
e = new DotIdExp(exp.loc, e1x, Id.ctor); |
| 10955 |
e = new CallExp(exp.loc, e, e2x); |
| 10956 |
e = new CommaExp(exp.loc, einit, e); |
| 10957 |
e = e.expressionSemantic(sc); |
| 10958 |
result = e; |
| 10959 |
return; |
| 10960 |
} |
| 10961 |
if (search_function(sd, Id.opCall)) |
| 10962 |
{ |
| 10963 |
/* Look for static opCall |
| 10964 |
* https://issues.dlang.org/show_bug.cgi?id=2702 |
| 10965 |
* Rewrite as: |
| 10966 |
* e1 = typeof(e1).opCall(arguments) |
| 10967 |
*/ |
| 10968 |
e2x = typeDotIdExp(e2x.loc, e1x.type, Id.opCall); |
| 10969 |
e2x = new CallExp(exp.loc, e2x, exp.e2); |
| 10970 |
|
| 10971 |
e2x = e2x.expressionSemantic(sc); |
| 10972 |
e2x = resolveProperties(sc, e2x); |
| 10973 |
if (e2x.op == EXP.error) |
| 10974 |
{ |
| 10975 |
result = e2x; |
| 10976 |
return; |
| 10977 |
} |
| 10978 |
if (e2x.checkValue() || e2x.checkSharedAccess(sc)) |
| 10979 |
return setError(); |
| 10980 |
} |
| 10981 |
} |
| 10982 |
else // https://issues.dlang.org/show_bug.cgi?id=11355 |
| 10983 |
{ |
| 10984 |
AggregateDeclaration ad2 = isAggregate(e2x.type); |
| 10985 |
if (ad2 && ad2.aliasthis && !isRecursiveAliasThis(aliasThisStop[1], exp.e2.type)) |
| 10986 |
{ |
| 10987 |
/* Rewrite (e1 op e2) as: |
| 10988 |
* (e1 op e2.aliasthis) |
| 10989 |
*/ |
| 10990 |
exp.e2 = new DotIdExp(exp.e2.loc, exp.e2, ad2.aliasthis.ident); |
| 10991 |
result = exp.expressionSemantic(sc, aliasThisStop); |
| 10992 |
return; |
| 10993 |
} |
| 10994 |
} |
| 10995 |
} |
| 10996 |
else if (exp.op == EXP.assign) |
| 10997 |
{ |
| 10998 |
if (e1x.op == EXP.index && (cast(IndexExp)e1x).e1.type.toBasetype().ty == Taarray) |
| 10999 |
{ |
| 11000 |
/* |
| 11001 |
* Rewrite: |
| 11002 |
* aa[key] = e2; |
| 11003 |
* as: |
| 11004 |
* ref __aatmp = aa; |
| 11005 |
* ref __aakey = key; |
| 11006 |
* ref __aaval = e2; |
| 11007 |
* (__aakey in __aatmp |
| 11008 |
* ? __aatmp[__aakey].opAssign(__aaval) |
| 11009 |
* : ConstructExp(__aatmp[__aakey], __aaval)); |
| 11010 |
*/ |
| 11011 |
// ensure we keep the expr modifiable |
| 11012 |
Expression esetting = (cast(IndexExp)e1x).markSettingAAElem(); |
| 11013 |
if (esetting.op == EXP.error) |
| 11014 |
{ |
| 11015 |
result = esetting; |
| 11016 |
return; |
| 11017 |
} |
| 11018 |
assert(esetting.op == EXP.index); |
| 11019 |
IndexExp ie = cast(IndexExp) esetting; |
| 11020 |
Type t2 = e2x.type.toBasetype(); |
| 11021 |
|
| 11022 |
Expression e0 = null; |
| 11023 |
Expression ea = extractSideEffect(sc, "__aatmp", e0, ie.e1); |
| 11024 |
Expression ek = extractSideEffect(sc, "__aakey", e0, ie.e2); |
| 11025 |
Expression ev = extractSideEffect(sc, "__aaval", e0, e2x); |
| 11026 |
|
| 11027 |
AssignExp ae = cast(AssignExp)exp.copy(); |
| 11028 |
ae.e1 = new IndexExp(exp.loc, ea, ek); |
| 11029 |
ae.e1 = ae.e1.expressionSemantic(sc); |
| 11030 |
ae.e1 = ae.e1.optimize(WANTvalue); |
| 11031 |
ae.e2 = ev; |
| 11032 |
if (Expression e = ae.opOverloadAssign(sc, aliasThisStop)) |
| 11033 |
{ |
| 11034 |
Expression ey = null; |
| 11035 |
if (t2.ty == Tstruct && sd == t2.toDsymbol(sc)) |
| 11036 |
{ |
| 11037 |
ey = ev; |
| 11038 |
} |
| 11039 |
else if (!ev.implicitConvTo(ie.type) && sd.ctor) |
| 11040 |
{ |
| 11041 |
// Look for implicit constructor call |
| 11042 |
// Rewrite as S().ctor(e2) |
| 11043 |
ey = new StructLiteralExp(exp.loc, sd, null); |
| 11044 |
ey = new DotIdExp(exp.loc, ey, Id.ctor); |
| 11045 |
ey = new CallExp(exp.loc, ey, ev); |
| 11046 |
ey = ey.trySemantic(sc); |
| 11047 |
} |
| 11048 |
if (ey) |
| 11049 |
{ |
| 11050 |
Expression ex; |
| 11051 |
ex = new IndexExp(exp.loc, ea, ek); |
| 11052 |
ex = ex.expressionSemantic(sc); |
| 11053 |
ex = ex.modifiableLvalue(sc); // allocate new slot |
| 11054 |
ex = ex.optimize(WANTvalue); |
| 11055 |
|
| 11056 |
ey = new ConstructExp(exp.loc, ex, ey); |
| 11057 |
ey = ey.expressionSemantic(sc); |
| 11058 |
if (ey.op == EXP.error) |
| 11059 |
{ |
| 11060 |
result = ey; |
| 11061 |
return; |
| 11062 |
} |
| 11063 |
ex = e; |
| 11064 |
|
| 11065 |
// https://issues.dlang.org/show_bug.cgi?id=14144 |
| 11066 |
// The whole expression should have the common type |
| 11067 |
// of opAssign() return and assigned AA entry. |
| 11068 |
// Even if there's no common type, expression should be typed as void. |
| 11069 |
if (!typeMerge(sc, EXP.question, ex, ey)) |
| 11070 |
{ |
| 11071 |
ex = new CastExp(ex.loc, ex, Type.tvoid); |
| 11072 |
ey = new CastExp(ey.loc, ey, Type.tvoid); |
| 11073 |
} |
| 11074 |
e = new CondExp(exp.loc, new InExp(exp.loc, ek, ea), ex, ey); |
| 11075 |
} |
| 11076 |
e = Expression.combine(e0, e); |
| 11077 |
e = e.expressionSemantic(sc); |
| 11078 |
result = e; |
| 11079 |
return; |
| 11080 |
} |
| 11081 |
} |
| 11082 |
else if (Expression e = exp.isAssignExp().opOverloadAssign(sc, aliasThisStop)) |
| 11083 |
{ |
| 11084 |
result = e; |
| 11085 |
return; |
| 11086 |
} |
| 11087 |
} |
| 11088 |
else |
| 11089 |
assert(exp.op == EXP.blit); |
| 11090 |
|
| 11091 |
if (e2x.checkValue()) |
| 11092 |
return setError(); |
| 11093 |
|
| 11094 |
exp.e1 = e1x; |
| 11095 |
exp.e2 = e2x; |
| 11096 |
} |
| 11097 |
else if (t1.ty == Tclass) |
| 11098 |
{ |
| 11099 |
// Disallow assignment operator overloads for same type |
| 11100 |
if (exp.op == EXP.assign && !exp.e2.implicitConvTo(exp.e1.type)) |
| 11101 |
{ |
| 11102 |
if (Expression e = exp.isAssignExp().opOverloadAssign(sc, aliasThisStop)) |
| 11103 |
{ |
| 11104 |
result = e; |
| 11105 |
return; |
| 11106 |
} |
| 11107 |
} |
| 11108 |
if (exp.e2.checkValue()) |
| 11109 |
return setError(); |
| 11110 |
} |
| 11111 |
else if (t1.ty == Tsarray) |
| 11112 |
{ |
| 11113 |
// SliceExp cannot have static array type without context inference. |
| 11114 |
assert(exp.e1.op != EXP.slice); |
| 11115 |
Expression e1x = exp.e1; |
| 11116 |
Expression e2x = exp.e2; |
| 11117 |
|
| 11118 |
/* C strings come through as static arrays. May need to adjust the size of the |
| 11119 |
* string to match the size of e1. |
| 11120 |
*/ |
| 11121 |
Type t2 = e2x.type.toBasetype(); |
| 11122 |
if (sc.inCfile && e2x.isStringExp() && t2.isTypeSArray()) |
| 11123 |
{ |
| 11124 |
uinteger_t dim1 = t1.isTypeSArray().dim.toInteger(); |
| 11125 |
uinteger_t dim2 = t2.isTypeSArray().dim.toInteger(); |
| 11126 |
if (dim1 + 1 == dim2 || dim2 < dim1) |
| 11127 |
{ |
| 11128 |
auto tsa2 = t2.isTypeSArray(); |
| 11129 |
auto newt = tsa2.next.sarrayOf(dim1).immutableOf(); |
| 11130 |
e2x = castTo(e2x, sc, newt); |
| 11131 |
exp.e2 = e2x; |
| 11132 |
} |
| 11133 |
} |
| 11134 |
|
| 11135 |
if (e2x.implicitConvTo(e1x.type)) |
| 11136 |
{ |
| 11137 |
if (exp.op != EXP.blit && (e2x.op == EXP.slice && (cast(UnaExp)e2x).e1.isLvalue() || e2x.op == EXP.cast_ && (cast(UnaExp)e2x).e1.isLvalue() || e2x.op != EXP.slice && e2x.isLvalue())) |
| 11138 |
{ |
| 11139 |
if (t1.checkPostblit(e1x.loc, sc)) |
| 11140 |
return setError(); |
| 11141 |
} |
| 11142 |
|
| 11143 |
// e2 matches to t1 because of the implicit length match, so |
| 11144 |
if (isUnaArrayOp(e2x.op) || isBinArrayOp(e2x.op)) |
| 11145 |
{ |
| 11146 |
// convert e1 to e1[] |
| 11147 |
// e.g. e1[] = a[] + b[]; |
| 11148 |
auto sle = new SliceExp(e1x.loc, e1x, null, null); |
| 11149 |
sle.arrayop = true; |
| 11150 |
e1x = sle.expressionSemantic(sc); |
| 11151 |
} |
| 11152 |
else |
| 11153 |
{ |
| 11154 |
// convert e2 to t1 later |
| 11155 |
// e.g. e1 = [1, 2, 3]; |
| 11156 |
} |
| 11157 |
} |
| 11158 |
else |
| 11159 |
{ |
| 11160 |
if (e2x.implicitConvTo(t1.nextOf().arrayOf()) > MATCH.nomatch) |
| 11161 |
{ |
| 11162 |
uinteger_t dim1 = (cast(TypeSArray)t1).dim.toInteger(); |
| 11163 |
uinteger_t dim2 = dim1; |
| 11164 |
if (auto ale = e2x.isArrayLiteralExp()) |
| 11165 |
{ |
| 11166 |
dim2 = ale.elements ? ale.elements.length : 0; |
| 11167 |
} |
| 11168 |
else if (auto se = e2x.isSliceExp()) |
| 11169 |
{ |
| 11170 |
Type tx = toStaticArrayType(se); |
| 11171 |
if (tx) |
| 11172 |
dim2 = (cast(TypeSArray)tx).dim.toInteger(); |
| 11173 |
} |
| 11174 |
if (dim1 != dim2) |
| 11175 |
{ |
| 11176 |
error(exp.loc, "mismatched array lengths, %d and %d", cast(int)dim1, cast(int)dim2); |
| 11177 |
return setError(); |
| 11178 |
} |
| 11179 |
} |
| 11180 |
|
| 11181 |
// May be block or element-wise assignment, so |
| 11182 |
// convert e1 to e1[] |
| 11183 |
if (exp.op != EXP.assign) |
| 11184 |
{ |
| 11185 |
// If multidimensional static array, treat as one large array |
| 11186 |
// |
| 11187 |
// Find the appropriate array type depending on the assignment, e.g. |
| 11188 |
// int[3] = int => int[3] |
| 11189 |
// int[3][2] = int => int[6] |
| 11190 |
// int[3][2] = int[] => int[3][2] |
| 11191 |
// int[3][2][4] + int => int[24] |
| 11192 |
// int[3][2][4] + int[] => int[3][8] |
| 11193 |
ulong dim = t1.isTypeSArray().dim.toUInteger(); |
| 11194 |
auto type = t1.nextOf(); |
| 11195 |
|
| 11196 |
for (TypeSArray tsa; (tsa = type.isTypeSArray()) !is null; ) |
| 11197 |
{ |
| 11198 |
import core.checkedint : mulu; |
| 11199 |
|
| 11200 |
// Accumulate skipped dimensions |
| 11201 |
bool overflow = false; |
| 11202 |
dim = mulu(dim, tsa.dim.toUInteger(), overflow); |
| 11203 |
if (overflow || dim >= uint.max) |
| 11204 |
{ |
| 11205 |
// dym exceeds maximum array size |
| 11206 |
error(exp.loc, "static array `%s` size overflowed to %llu", |
| 11207 |
e1x.type.toChars(), cast(ulong) dim); |
| 11208 |
return setError(); |
| 11209 |
} |
| 11210 |
|
| 11211 |
// Move to the element type |
| 11212 |
type = tsa.nextOf().toBasetype(); |
| 11213 |
|
| 11214 |
// Rewrite ex1 as a static array if a matching type was found |
| 11215 |
if (e2x.implicitConvTo(type) > MATCH.nomatch) |
| 11216 |
{ |
| 11217 |
e1x.type = type.sarrayOf(dim); |
| 11218 |
break; |
| 11219 |
} |
| 11220 |
} |
| 11221 |
} |
| 11222 |
auto sle = new SliceExp(e1x.loc, e1x, null, null); |
| 11223 |
sle.arrayop = true; |
| 11224 |
e1x = sle.expressionSemantic(sc); |
| 11225 |
} |
| 11226 |
if (e1x.op == EXP.error) |
| 11227 |
return setResult(e1x); |
| 11228 |
if (e2x.op == EXP.error) |
| 11229 |
return setResult(e2x); |
| 11230 |
|
| 11231 |
exp.e1 = e1x; |
| 11232 |
exp.e2 = e2x; |
| 11233 |
t1 = e1x.type.toBasetype(); |
| 11234 |
} |
| 11235 |
else if (t1.ty == Taarray) |
| 11236 |
{ |
| 11237 |
// when assigning a constant, the need for TypeInfo might change |
| 11238 |
semanticTypeInfo(sc, t1); |
| 11239 |
} |
| 11240 |
/* Check the mutability of e1. |
| 11241 |
*/ |
| 11242 |
if (auto ale = exp.e1.isArrayLengthExp()) |
| 11243 |
{ |
| 11244 |
// e1 is not an lvalue, but we let code generator handle it |
| 11245 |
|
| 11246 |
auto ale1x = ale.e1.modifiableLvalueImpl(sc, exp.e1); |
| 11247 |
if (ale1x.op == EXP.error) |
| 11248 |
return setResult(ale1x); |
| 11249 |
ale.e1 = ale1x; |
| 11250 |
|
| 11251 |
Type tn = ale.e1.type.toBasetype().nextOf(); |
| 11252 |
checkDefCtor(ale.loc, tn); |
| 11253 |
|
| 11254 |
Identifier hook = global.params.tracegc ? Id._d_arraysetlengthTTrace : Id._d_arraysetlengthT; |
| 11255 |
if (!verifyHookExist(exp.loc, *sc, Id._d_arraysetlengthTImpl, "resizing arrays")) |
| 11256 |
return setError(); |
| 11257 |
|
| 11258 |
exp.e2 = exp.e2.expressionSemantic(sc); |
| 11259 |
auto lc = lastComma(exp.e2); |
| 11260 |
lc = lc.optimize(WANTvalue); |
| 11261 |
// use slice expression when arr.length = 0 to avoid runtime call |
| 11262 |
if(lc.op == EXP.int64 && lc.toInteger() == 0) |
| 11263 |
{ |
| 11264 |
Expression se = new SliceExp(ale.loc, ale.e1, lc, lc); |
| 11265 |
Expression as = new AssignExp(ale.loc, ale.e1, se); |
| 11266 |
as = as.expressionSemantic(sc); |
| 11267 |
auto res = Expression.combine(as, exp.e2); |
| 11268 |
res.type = ale.type; |
| 11269 |
return setResult(res); |
| 11270 |
} |
| 11271 |
|
| 11272 |
if (!sc.needsCodegen()) // if compile time creature only |
| 11273 |
{ |
| 11274 |
exp.type = Type.tsize_t; |
| 11275 |
return setResult(exp); |
| 11276 |
} |
| 11277 |
|
| 11278 |
// Lower to object._d_arraysetlengthTImpl!(typeof(e1))._d_arraysetlengthT{,Trace}(e1, e2) |
| 11279 |
Expression id = new IdentifierExp(ale.loc, Id.empty); |
| 11280 |
id = new DotIdExp(ale.loc, id, Id.object); |
| 11281 |
auto tiargs = new Objects(); |
| 11282 |
tiargs.push(ale.e1.type); |
| 11283 |
id = new DotTemplateInstanceExp(ale.loc, id, Id._d_arraysetlengthTImpl, tiargs); |
| 11284 |
id = new DotIdExp(ale.loc, id, hook); |
| 11285 |
id = id.expressionSemantic(sc); |
| 11286 |
|
| 11287 |
auto arguments = new Expressions(); |
| 11288 |
arguments.push(ale.e1); |
| 11289 |
arguments.push(exp.e2); |
| 11290 |
|
| 11291 |
Expression ce = new CallExp(ale.loc, id, arguments).expressionSemantic(sc); |
| 11292 |
auto res = new LoweredAssignExp(exp, ce); |
| 11293 |
// if (global.params.verbose) |
| 11294 |
// message("lowered %s =>\n %s", exp.toChars(), res.toChars()); |
| 11295 |
res.type = Type.tsize_t; |
| 11296 |
return setResult(res); |
| 11297 |
} |
| 11298 |
else if (auto se = exp.e1.isSliceExp()) |
| 11299 |
{ |
| 11300 |
Type tn = se.type.nextOf(); |
| 11301 |
const fun = sc.func; |
| 11302 |
if (exp.op == EXP.assign && !tn.isMutable() && |
| 11303 |
// allow modifiation in module ctor, see |
| 11304 |
// https://issues.dlang.org/show_bug.cgi?id=9884 |
| 11305 |
(!fun || (fun && !fun.isStaticCtorDeclaration()))) |
| 11306 |
{ |
| 11307 |
error(exp.loc, "slice `%s` is not mutable", se.toChars()); |
| 11308 |
return setError(); |
| 11309 |
} |
| 11310 |
|
| 11311 |
if (exp.op == EXP.assign && !tn.baseElemOf().isAssignable()) |
| 11312 |
{ |
| 11313 |
error(exp.loc, "slice `%s` is not mutable, struct `%s` has immutable members", |
| 11314 |
exp.e1.toErrMsg(), tn.baseElemOf().toChars()); |
| 11315 |
result = ErrorExp.get(); |
| 11316 |
return; |
| 11317 |
} |
| 11318 |
|
| 11319 |
// For conditional operator, both branches need conversion. |
| 11320 |
while (se.e1.op == EXP.slice) |
| 11321 |
se = cast(SliceExp)se.e1; |
| 11322 |
if (se.e1.op == EXP.question && se.e1.type.toBasetype().ty == Tsarray) |
| 11323 |
{ |
| 11324 |
se.e1 = se.e1.modifiableLvalueImpl(sc, exp.e1); |
| 11325 |
if (se.e1.op == EXP.error) |
| 11326 |
return setResult(se.e1); |
| 11327 |
} |
| 11328 |
} |
| 11329 |
else |
| 11330 |
{ |
| 11331 |
if (t1.ty == Tsarray && exp.op == EXP.assign) |
| 11332 |
{ |
| 11333 |
Type tn = exp.e1.type.nextOf(); |
| 11334 |
if (tn && !tn.baseElemOf().isAssignable()) |
| 11335 |
{ |
| 11336 |
error(exp.loc, "array `%s` is not mutable, struct `%s` has immutable members", |
| 11337 |
exp.e1.toErrMsg(), tn.baseElemOf().toChars()); |
| 11338 |
result = ErrorExp.get(); |
| 11339 |
return; |
| 11340 |
} |
| 11341 |
} |
| 11342 |
|
| 11343 |
Expression e1x = exp.e1; |
| 11344 |
|
| 11345 |
// Try to do a decent error message with the expression |
| 11346 |
// before it gets constant folded |
| 11347 |
if (exp.op == EXP.assign) |
| 11348 |
e1x = e1x.modifiableLvalueImpl(sc, e1old); |
| 11349 |
|
| 11350 |
e1x = e1x.optimize(WANTvalue, /*keepLvalue*/ true); |
| 11351 |
|
| 11352 |
if (e1x.op == EXP.error) |
| 11353 |
{ |
| 11354 |
result = e1x; |
| 11355 |
return; |
| 11356 |
} |
| 11357 |
exp.e1 = e1x; |
| 11358 |
} |
| 11359 |
|
| 11360 |
/* Tweak e2 based on the type of e1. |
| 11361 |
*/ |
| 11362 |
Expression e2x = exp.e2; |
| 11363 |
Type t2 = e2x.type.toBasetype(); |
| 11364 |
|
| 11365 |
// If it is a array, get the element type. Note that it may be |
| 11366 |
// multi-dimensional. |
| 11367 |
Type telem = t1; |
| 11368 |
while (telem.ty == Tarray) |
| 11369 |
telem = telem.nextOf(); |
| 11370 |
|
| 11371 |
if (exp.e1.op == EXP.slice && t1.nextOf() && |
| 11372 |
(telem.ty != Tvoid || e2x.op == EXP.null_) && |
| 11373 |
e2x.implicitConvTo(t1.nextOf())) |
| 11374 |
{ |
| 11375 |
// Check for block assignment. If it is of type void[], void[][], etc, |
| 11376 |
// '= null' is the only allowable block assignment (Bug 7493) |
| 11377 |
exp.memset = MemorySet.blockAssign; // make it easy for back end to tell what this is |
| 11378 |
e2x = e2x.implicitCastTo(sc, t1.nextOf()); |
| 11379 |
if (exp.op != EXP.blit && e2x.isLvalue() && t1.nextOf.checkPostblit(exp.e1.loc, sc)) |
| 11380 |
return setError(); |
| 11381 |
} |
| 11382 |
else if (exp.e1.op == EXP.slice && |
| 11383 |
t2.isStaticOrDynamicArray() && |
| 11384 |
t2.nextOf().implicitConvTo(t1.nextOf())) |
| 11385 |
{ |
| 11386 |
// Check element-wise assignment. |
| 11387 |
|
| 11388 |
/* If assigned elements number is known at compile time, |
| 11389 |
* check the mismatch. |
| 11390 |
*/ |
| 11391 |
SliceExp se1 = cast(SliceExp)exp.e1; |
| 11392 |
TypeSArray tsa1 = cast(TypeSArray)toStaticArrayType(se1); |
| 11393 |
TypeSArray tsa2 = null; |
| 11394 |
if (auto ale = e2x.isArrayLiteralExp()) |
| 11395 |
tsa2 = cast(TypeSArray)t2.nextOf().sarrayOf(ale.elements.length); |
| 11396 |
else if (auto se = e2x.isSliceExp()) |
| 11397 |
tsa2 = cast(TypeSArray)toStaticArrayType(se); |
| 11398 |
else |
| 11399 |
tsa2 = t2.isTypeSArray(); |
| 11400 |
|
| 11401 |
if (tsa1 && tsa2) |
| 11402 |
{ |
| 11403 |
uinteger_t dim1 = tsa1.dim.toInteger(); |
| 11404 |
uinteger_t dim2 = tsa2.dim.toInteger(); |
| 11405 |
if (dim1 != dim2) |
| 11406 |
{ |
| 11407 |
error(exp.loc, "mismatched array lengths %d and %d for assignment `%s`", cast(int)dim1, cast(int)dim2, exp.toErrMsg()); |
| 11408 |
return setError(); |
| 11409 |
} |
| 11410 |
} |
| 11411 |
|
| 11412 |
if (exp.op != EXP.blit && |
| 11413 |
(e2x.op == EXP.slice && (cast(UnaExp)e2x).e1.isLvalue() || |
| 11414 |
e2x.op == EXP.cast_ && (cast(UnaExp)e2x).e1.isLvalue() || |
| 11415 |
e2x.op != EXP.slice && e2x.isLvalue())) |
| 11416 |
{ |
| 11417 |
if (t1.nextOf().checkPostblit(exp.e1.loc, sc)) |
| 11418 |
return setError(); |
| 11419 |
} |
| 11420 |
|
| 11421 |
Type t2n = t2.nextOf(); |
| 11422 |
Type t1n = t1.nextOf(); |
| 11423 |
int offset; |
| 11424 |
if (t2n.equivalent(t1n) || |
| 11425 |
t1n.isBaseOf(t2n, &offset) && offset == 0) |
| 11426 |
{ |
| 11427 |
/* Allow copy of distinct qualifier elements. |
| 11428 |
* eg. |
| 11429 |
* char[] dst; const(char)[] src; |
| 11430 |
* dst[] = src; |
| 11431 |
* |
| 11432 |
* class C {} class D : C {} |
| 11433 |
* C[2] ca; D[] da; |
| 11434 |
* ca[] = da; |
| 11435 |
*/ |
| 11436 |
if (isArrayOpValid(e2x)) |
| 11437 |
{ |
| 11438 |
// Don't add CastExp to keep AST for array operations |
| 11439 |
e2x = e2x.copy(); |
| 11440 |
e2x.type = exp.e1.type.constOf(); |
| 11441 |
} |
| 11442 |
else |
| 11443 |
e2x = e2x.castTo(sc, exp.e1.type.constOf()); |
| 11444 |
} |
| 11445 |
else |
| 11446 |
{ |
| 11447 |
/* https://issues.dlang.org/show_bug.cgi?id=15778 |
| 11448 |
* A string literal has an array type of immutable |
| 11449 |
* elements by default, and normally it cannot be convertible to |
| 11450 |
* array type of mutable elements. But for element-wise assignment, |
| 11451 |
* elements need to be const at best. So we should give a chance |
| 11452 |
* to change code unit size for polysemous string literal. |
| 11453 |
*/ |
| 11454 |
if (e2x.op == EXP.string_) |
| 11455 |
e2x = e2x.implicitCastTo(sc, exp.e1.type.constOf()); |
| 11456 |
else |
| 11457 |
e2x = e2x.implicitCastTo(sc, exp.e1.type); |
| 11458 |
} |
| 11459 |
} |
| 11460 |
else |
| 11461 |
{ |
| 11462 |
if (exp.op == EXP.blit) |
| 11463 |
e2x = e2x.castTo(sc, exp.e1.type); |
| 11464 |
else |
| 11465 |
{ |
| 11466 |
e2x = e2x.implicitCastTo(sc, exp.e1.type); |
| 11467 |
|
| 11468 |
// Fix Issue 13435: https://issues.dlang.org/show_bug.cgi?id=13435 |
| 11469 |
|
| 11470 |
// If the implicit cast has failed and the assign expression is |
| 11471 |
// the initialization of a struct member field |
| 11472 |
if (e2x.op == EXP.error && exp.op == EXP.construct && t1.ty == Tstruct) |
| 11473 |
{ |
| 11474 |
scope sd = (cast(TypeStruct)t1).sym; |
| 11475 |
Dsymbol opAssign = search_function(sd, Id.opAssign); |
| 11476 |
|
| 11477 |
// and the struct defines an opAssign |
| 11478 |
if (opAssign) |
| 11479 |
{ |
| 11480 |
// offer more information about the cause of the problem |
| 11481 |
errorSupplemental(exp.loc, |
| 11482 |
"`%s` is the first assignment of `%s` therefore it represents its initialization", |
| 11483 |
exp.toErrMsg(), exp.e1.toErrMsg()); |
| 11484 |
errorSupplemental(exp.loc, |
| 11485 |
"`opAssign` methods are not used for initialization, but for subsequent assignments"); |
| 11486 |
} |
| 11487 |
} |
| 11488 |
} |
| 11489 |
} |
| 11490 |
|
| 11491 |
if (exp.e1.op == EXP.slice && |
| 11492 |
t1.isStaticOrDynamicArray() && |
| 11493 |
t1.nextOf().toBasetype().ty == Tvoid) |
| 11494 |
{ |
| 11495 |
if (t2.nextOf().implicitConvTo(t1.nextOf())) |
| 11496 |
{ |
| 11497 |
if (sc.setUnsafe(false, exp.loc, "copying `%s` to `%s`", t2, t1)) |
| 11498 |
return setError(); |
| 11499 |
} |
| 11500 |
else |
| 11501 |
{ |
| 11502 |
// copying from non-void to void was overlooked, deprecate |
| 11503 |
if (sc.setUnsafePreview(FeatureState.default_, false, exp.loc, |
| 11504 |
"copying `%s` to `%s`", t2, t1)) |
| 11505 |
return setError(); |
| 11506 |
} |
| 11507 |
if (sc.previews.fixImmutableConv && !t2.implicitConvTo(t1)) |
| 11508 |
{ |
| 11509 |
error(exp.loc, "cannot copy `%s` to `%s`", |
| 11510 |
t2.toChars(), t1.toChars()); |
| 11511 |
errorSupplemental(exp.loc, |
| 11512 |
"Source data has incompatible type qualifier(s)"); |
| 11513 |
errorSupplemental(exp.loc, "Use `cast(%s)` to force copy", t1.toChars()); |
| 11514 |
return setError(); |
| 11515 |
} |
| 11516 |
} |
| 11517 |
if (e2x.op == EXP.error) |
| 11518 |
{ |
| 11519 |
result = e2x; |
| 11520 |
return; |
| 11521 |
} |
| 11522 |
exp.e2 = e2x; |
| 11523 |
t2 = exp.e2.type.toBasetype(); |
| 11524 |
|
| 11525 |
/* Look for array operations |
| 11526 |
*/ |
| 11527 |
if (t2.isStaticOrDynamicArray() && isArrayOpValid(exp.e2)) |
| 11528 |
{ |
| 11529 |
// Look for valid array operations |
| 11530 |
if (exp.memset != MemorySet.blockAssign && |
| 11531 |
exp.e1.op == EXP.slice && |
| 11532 |
(isUnaArrayOp(exp.e2.op) || isBinArrayOp(exp.e2.op))) |
| 11533 |
{ |
| 11534 |
exp.type = exp.e1.type; |
| 11535 |
if (exp.op == EXP.construct) // https://issues.dlang.org/show_bug.cgi?id=10282 |
| 11536 |
// tweak mutability of e1 element |
| 11537 |
exp.e1.type = exp.e1.type.nextOf().mutableOf().arrayOf(); |
| 11538 |
result = arrayOp(exp, sc); |
| 11539 |
return; |
| 11540 |
} |
| 11541 |
|
| 11542 |
// Drop invalid array operations in e2 |
| 11543 |
// d = a[] + b[], d = (a[] + b[])[0..2], etc |
| 11544 |
if (checkNonAssignmentArrayOp(exp.e2, exp.memset != MemorySet.blockAssign && exp.op == EXP.assign)) |
| 11545 |
return setError(); |
| 11546 |
|
| 11547 |
// Remains valid array assignments |
| 11548 |
// d = d[], d = [1,2,3], etc |
| 11549 |
} |
| 11550 |
|
| 11551 |
/* Don't allow assignment to classes that were allocated on the stack with: |
| 11552 |
* scope Class c = new Class(); |
| 11553 |
*/ |
| 11554 |
if (exp.e1.op == EXP.variable && exp.op == EXP.assign) |
| 11555 |
{ |
| 11556 |
VarExp ve = cast(VarExp)exp.e1; |
| 11557 |
VarDeclaration vd = ve.var.isVarDeclaration(); |
| 11558 |
if (vd && vd.onstack) |
| 11559 |
{ |
| 11560 |
assert(t1.ty == Tclass); |
| 11561 |
error(exp.loc, "cannot rebind scope variables"); |
| 11562 |
} |
| 11563 |
} |
| 11564 |
|
| 11565 |
if (exp.e1.op == EXP.variable && (cast(VarExp)exp.e1).var.ident == Id.ctfe) |
| 11566 |
{ |
| 11567 |
error(exp.loc, "cannot modify compiler-generated variable `__ctfe`"); |
| 11568 |
} |
| 11569 |
|
| 11570 |
exp.type = exp.e1.type; |
| 11571 |
assert(exp.type); |
| 11572 |
auto assignElem = exp.e2; |
| 11573 |
auto res = exp.op == EXP.assign ? exp.reorderSettingAAElem(sc) : exp; |
| 11574 |
/* https://issues.dlang.org/show_bug.cgi?id=22366 |
| 11575 |
* |
| 11576 |
* `reorderSettingAAElem` creates a tree of comma expressions, however, |
| 11577 |
* `checkAssignExp` expects only AssignExps. |
| 11578 |
*/ |
| 11579 |
if (res == exp) // no `AA[k] = v` rewrite was performed |
| 11580 |
checkAssignEscape(*sc, res, false, false); |
| 11581 |
else |
| 11582 |
checkNewEscape(*sc, assignElem, false); // assigning to AA puts it on heap |
| 11583 |
|
| 11584 |
if (auto ae = res.isConstructExp()) |
| 11585 |
{ |
| 11586 |
Type t1b = ae.e1.type.toBasetype(); |
| 11587 |
if (t1b.ty != Tsarray && t1b.ty != Tarray) |
| 11588 |
return setResult(res); |
| 11589 |
|
| 11590 |
// only non-trivial array constructions may need to be lowered (non-POD elements basically) |
| 11591 |
Type t1e = t1b.nextOf(); |
| 11592 |
TypeStruct ts = t1e.baseElemOf().isTypeStruct(); |
| 11593 |
if (!ts || (!ts.sym.postblit && !ts.sym.hasCopyCtor && !ts.sym.dtor)) |
| 11594 |
return setResult(res); |
| 11595 |
|
| 11596 |
// don't lower ref-constructions etc. |
| 11597 |
if (!(t1b.ty == Tsarray || ae.e1.isSliceExp) || |
| 11598 |
(ae.e1.isVarExp && ae.e1.isVarExp.var.isVarDeclaration.isReference)) |
| 11599 |
return setResult(res); |
| 11600 |
|
| 11601 |
// Construction from an equivalent other array? |
| 11602 |
// Only lower with lvalue RHS elements; let the glue layer move rvalue elements. |
| 11603 |
Type t2b = ae.e2.type.toBasetype(); |
| 11604 |
// skip over a (possibly implicit) cast of a static array RHS to a slice |
| 11605 |
Expression rhs = ae.e2; |
| 11606 |
Type rhsType = t2b; |
| 11607 |
if (t2b.ty == Tarray) |
| 11608 |
{ |
| 11609 |
if (auto ce = rhs.isCastExp()) |
| 11610 |
{ |
| 11611 |
auto ct = ce.e1.type.toBasetype(); |
| 11612 |
if (ct.ty == Tsarray) |
| 11613 |
{ |
| 11614 |
rhs = ce.e1; |
| 11615 |
rhsType = ct; |
| 11616 |
} |
| 11617 |
} |
| 11618 |
} |
| 11619 |
|
| 11620 |
if (!sc.needsCodegen()) // interpreter can handle these |
| 11621 |
return setResult(res); |
| 11622 |
|
| 11623 |
const lowerToArrayCtor = |
| 11624 |
( (rhsType.ty == Tarray && !rhs.isArrayLiteralExp) || |
| 11625 |
(rhsType.ty == Tsarray && rhs.isLvalue) ) && |
| 11626 |
t1e.equivalent(t2b.nextOf); |
| 11627 |
|
| 11628 |
// Construction from a single element? |
| 11629 |
// If the RHS is an rvalue, then we'll need to make a temporary for it (copied multiple times). |
| 11630 |
const lowerToArraySetCtor = !lowerToArrayCtor && t1e.equivalent(t2b); |
| 11631 |
|
| 11632 |
if (lowerToArrayCtor || lowerToArraySetCtor) |
| 11633 |
{ |
| 11634 |
auto func = lowerToArrayCtor ? Id._d_arrayctor : Id._d_arraysetctor; |
| 11635 |
const other = lowerToArrayCtor ? "other array" : "value"; |
| 11636 |
if (!verifyHookExist(exp.loc, *sc, func, "construct array with " ~ other, Id.object)) |
| 11637 |
return setError(); |
| 11638 |
|
| 11639 |
// Lower to object._d_array{,set}ctor(e1, e2) |
| 11640 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 11641 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 11642 |
id = new DotIdExp(exp.loc, id, func); |
| 11643 |
|
| 11644 |
auto arguments = new Expressions(); |
| 11645 |
arguments.push(new CastExp(ae.loc, ae.e1, t1e.arrayOf).expressionSemantic(sc)); |
| 11646 |
if (lowerToArrayCtor) |
| 11647 |
{ |
| 11648 |
arguments.push(new CastExp(ae.loc, rhs, t2b.nextOf.arrayOf).expressionSemantic(sc)); |
| 11649 |
Expression ce = new CallExp(exp.loc, id, arguments); |
| 11650 |
res = ce.expressionSemantic(sc); |
| 11651 |
} |
| 11652 |
else |
| 11653 |
{ |
| 11654 |
Expression e0; |
| 11655 |
// promote an rvalue RHS element to a temporary, it's passed by ref to _d_arraysetctor |
| 11656 |
if (!ae.e2.isLvalue) |
| 11657 |
{ |
| 11658 |
auto vd = copyToTemp(STC.scope_, "__setctor", ae.e2); |
| 11659 |
e0 = new DeclarationExp(vd.loc, vd).expressionSemantic(sc); |
| 11660 |
arguments.push(new VarExp(vd.loc, vd).expressionSemantic(sc)); |
| 11661 |
} |
| 11662 |
else |
| 11663 |
arguments.push(ae.e2); |
| 11664 |
|
| 11665 |
Expression ce = new CallExp(exp.loc, id, arguments); |
| 11666 |
res = Expression.combine(e0, ce).expressionSemantic(sc); |
| 11667 |
} |
| 11668 |
|
| 11669 |
if (global.params.v.verbose) |
| 11670 |
message("lowered %s =>\n %s", exp.toChars(), res.toChars()); |
| 11671 |
} |
| 11672 |
} |
| 11673 |
else if (auto ae = res.isAssignExp()) |
| 11674 |
res = lowerArrayAssign(ae); |
| 11675 |
else if (auto ce = res.isCommaExp()) |
| 11676 |
{ |
| 11677 |
if (auto ae1 = ce.e1.isAssignExp()) |
| 11678 |
ce.e1 = lowerArrayAssign(ae1, true); |
| 11679 |
if (auto ae2 = ce.e2.isAssignExp()) |
| 11680 |
ce.e2 = lowerArrayAssign(ae2, true); |
| 11681 |
} |
| 11682 |
|
| 11683 |
return setResult(res); |
| 11684 |
} |
| 11685 |
|
| 11686 |
/*************************************** |
| 11687 |
* Lower AssignExp to `_d_array{setassign,assign_l,assign_r}` if needed. |
| 11688 |
* |
| 11689 |
* Params: |
| 11690 |
* ae = the AssignExp to be lowered |
| 11691 |
* fromCommaExp = indicates whether `ae` is part of a CommaExp or not, |
| 11692 |
* so no unnecessary temporay variable is created. |
| 11693 |
* Returns: |
| 11694 |
* a CommaExp contiaining call a to `_d_array{setassign,assign_l,assign_r}` |
| 11695 |
* if needed or `ae` otherwise |
| 11696 |
*/ |
| 11697 |
private Expression lowerArrayAssign(AssignExp ae, bool fromCommaExp = false) |
| 11698 |
{ |
| 11699 |
Type t1b = ae.e1.type.toBasetype(); |
| 11700 |
if (t1b.ty != Tsarray && t1b.ty != Tarray) |
| 11701 |
return ae; |
| 11702 |
|
| 11703 |
const isArrayAssign = (ae.e1.isSliceExp() || ae.e1.type.ty == Tsarray) && |
| 11704 |
(ae.e2.type.isStaticOrDynamicArray()) && |
| 11705 |
(ae.e1.type.nextOf() && ae.e2.type.nextOf() && ae.e1.type.nextOf.mutableOf.equals(ae.e2.type.nextOf.mutableOf())); |
| 11706 |
|
| 11707 |
const isArraySetAssign = (ae.e1.isSliceExp() || ae.e1.type.ty == Tsarray) && |
| 11708 |
(ae.e1.type.nextOf() && ae.e2.type.implicitConvTo(ae.e1.type.nextOf())); |
| 11709 |
|
| 11710 |
if (!isArrayAssign && !isArraySetAssign) |
| 11711 |
return ae; |
| 11712 |
|
| 11713 |
const ts = t1b.nextOf().baseElemOf().isTypeStruct(); |
| 11714 |
if (!ts || (!ts.sym.postblit && !ts.sym.dtor)) |
| 11715 |
return ae; |
| 11716 |
|
| 11717 |
Expression res; |
| 11718 |
Identifier func = isArraySetAssign ? Id._d_arraysetassign : |
| 11719 |
ae.e2.isLvalue() || ae.e2.isSliceExp() ? Id._d_arrayassign_l : Id._d_arrayassign_r; |
| 11720 |
|
| 11721 |
// Lower to `.object._d_array{setassign,assign_l,assign_r}(e1, e2)`` |
| 11722 |
Expression id = new IdentifierExp(ae.loc, Id.empty); |
| 11723 |
id = new DotIdExp(ae.loc, id, Id.object); |
| 11724 |
id = new DotIdExp(ae.loc, id, func); |
| 11725 |
|
| 11726 |
auto arguments = new Expressions(); |
| 11727 |
arguments.push(new CastExp(ae.loc, ae.e1, ae.e1.type.nextOf.arrayOf) |
| 11728 |
.expressionSemantic(sc)); |
| 11729 |
|
| 11730 |
Expression eValue2, value2 = ae.e2; |
| 11731 |
if (isArrayAssign && value2.isLvalue()) |
| 11732 |
value2 = new CastExp(ae.loc, ae.e2, ae.e2.type.nextOf.arrayOf()) |
| 11733 |
.expressionSemantic(sc); |
| 11734 |
else if (!fromCommaExp && |
| 11735 |
(isArrayAssign || (isArraySetAssign && !value2.isLvalue()))) |
| 11736 |
{ |
| 11737 |
// Rvalues from CommaExps were introduced in `visit(AssignExp)` |
| 11738 |
// and are temporary variables themselves. Rvalues from trivial |
| 11739 |
// SliceExps are simply passed by reference without any copying. |
| 11740 |
|
| 11741 |
// `__assigntmp` will be destroyed together with the array `ae.e1`. |
| 11742 |
// When `ae.e2` is a variadic arg array, it is also `scope`, so |
| 11743 |
// `__assigntmp` may also be scope. |
| 11744 |
STC stc = STC.nodtor; |
| 11745 |
if (isArrayAssign) |
| 11746 |
stc |= STC.rvalue | STC.scope_; |
| 11747 |
|
| 11748 |
auto vd = copyToTemp(stc, "__assigntmp", ae.e2); |
| 11749 |
eValue2 = new DeclarationExp(vd.loc, vd).expressionSemantic(sc); |
| 11750 |
value2 = new VarExp(vd.loc, vd).expressionSemantic(sc); |
| 11751 |
} |
| 11752 |
arguments.push(value2); |
| 11753 |
|
| 11754 |
Expression ce = new CallExp(ae.loc, id, arguments); |
| 11755 |
res = Expression.combine(eValue2, ce).expressionSemantic(sc); |
| 11756 |
if (isArrayAssign) |
| 11757 |
res = Expression.combine(res, ae.e1).expressionSemantic(sc); |
| 11758 |
|
| 11759 |
if (global.params.v.verbose) |
| 11760 |
message("lowered %s =>\n %s", ae.toChars(), res.toChars()); |
| 11761 |
|
| 11762 |
res = new LoweredAssignExp(ae, res); |
| 11763 |
res.type = ae.type; |
| 11764 |
|
| 11765 |
return res; |
| 11766 |
} |
| 11767 |
|
| 11768 |
override void visit(PowAssignExp exp) |
| 11769 |
{ |
| 11770 |
if (Expression e = exp.opOverloadBinaryAssign(sc, aliasThisStop)) |
| 11771 |
{ |
| 11772 |
result = e; |
| 11773 |
return; |
| 11774 |
} |
| 11775 |
|
| 11776 |
if (exp.suggestOpOpAssign(sc, parent) || |
| 11777 |
exp.e1.checkReadModifyWrite(exp.op, exp.e2)) |
| 11778 |
return setError(); |
| 11779 |
|
| 11780 |
assert(exp.e1.type && exp.e2.type); |
| 11781 |
if (exp.e1.op == EXP.slice || exp.e1.type.isStaticOrDynamicArray()) |
| 11782 |
{ |
| 11783 |
if (checkNonAssignmentArrayOp(exp.e1)) |
| 11784 |
return setError(); |
| 11785 |
|
| 11786 |
// T[] ^^= ... |
| 11787 |
if (exp.e2.implicitConvTo(exp.e1.type.nextOf())) |
| 11788 |
{ |
| 11789 |
// T[] ^^= T |
| 11790 |
exp.e2 = exp.e2.castTo(sc, exp.e1.type.nextOf()); |
| 11791 |
} |
| 11792 |
else if (Expression ex = typeCombine(exp, sc)) |
| 11793 |
{ |
| 11794 |
result = ex; |
| 11795 |
return; |
| 11796 |
} |
| 11797 |
|
| 11798 |
// Check element types are arithmetic |
| 11799 |
Type tb1 = exp.e1.type.nextOf().toBasetype(); |
| 11800 |
Type tb2 = exp.e2.type.toBasetype(); |
| 11801 |
if (tb2.isStaticOrDynamicArray()) |
| 11802 |
tb2 = tb2.nextOf().toBasetype(); |
| 11803 |
if ((tb1.isIntegral() || tb1.isFloating()) && (tb2.isIntegral() || tb2.isFloating())) |
| 11804 |
{ |
| 11805 |
exp.type = exp.e1.type; |
| 11806 |
result = arrayOp(exp, sc); |
| 11807 |
return; |
| 11808 |
} |
| 11809 |
} |
| 11810 |
else |
| 11811 |
{ |
| 11812 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 11813 |
} |
| 11814 |
|
| 11815 |
if ((exp.e1.type.isIntegral() || exp.e1.type.isFloating()) && (exp.e2.type.isIntegral() || exp.e2.type.isFloating())) |
| 11816 |
{ |
| 11817 |
Expression e0 = null; |
| 11818 |
Expression e = exp.reorderSettingAAElem(sc); |
| 11819 |
e = Expression.extractLast(e, e0); |
| 11820 |
assert(e == exp); |
| 11821 |
|
| 11822 |
if (exp.e1.op == EXP.variable) |
| 11823 |
{ |
| 11824 |
// Rewrite: e1 = e1 ^^ e2 |
| 11825 |
e = new PowExp(exp.loc, exp.e1.syntaxCopy(), exp.e2); |
| 11826 |
e = new AssignExp(exp.loc, exp.e1, e); |
| 11827 |
} |
| 11828 |
else |
| 11829 |
{ |
| 11830 |
// Rewrite: ref tmp = e1; tmp = tmp ^^ e2 |
| 11831 |
auto v = copyToTemp(STC.ref_, "__powtmp", exp.e1); |
| 11832 |
auto de = new DeclarationExp(exp.e1.loc, v); |
| 11833 |
auto ve = new VarExp(exp.e1.loc, v); |
| 11834 |
e = new PowExp(exp.loc, ve, exp.e2); |
| 11835 |
e = new AssignExp(exp.loc, new VarExp(exp.e1.loc, v), e); |
| 11836 |
e = new CommaExp(exp.loc, de, e); |
| 11837 |
} |
| 11838 |
e = Expression.combine(e0, e); |
| 11839 |
e = e.expressionSemantic(sc); |
| 11840 |
result = e; |
| 11841 |
return; |
| 11842 |
} |
| 11843 |
result = exp.incompatibleTypes(); |
| 11844 |
} |
| 11845 |
|
| 11846 |
override void visit(CatAssignExp exp) |
| 11847 |
{ |
| 11848 |
|
| 11849 |
//printf("CatAssignExp::semantic() %s\n", exp.toChars()); |
| 11850 |
if (Expression e = exp.opOverloadBinaryAssign(sc, aliasThisStop)) |
| 11851 |
{ |
| 11852 |
result = e; |
| 11853 |
return; |
| 11854 |
} |
| 11855 |
|
| 11856 |
if (exp.suggestOpOpAssign(sc, parent)) |
| 11857 |
return setError(); |
| 11858 |
|
| 11859 |
if (SliceExp se = exp.e1.isSliceExp()) |
| 11860 |
{ |
| 11861 |
if (se.e1.type.toBasetype().ty == Tsarray) |
| 11862 |
{ |
| 11863 |
error(exp.loc, "cannot append to static array `%s`", se.e1.type.toChars()); |
| 11864 |
return setError(); |
| 11865 |
} |
| 11866 |
} |
| 11867 |
|
| 11868 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 11869 |
if (exp.e1.op == EXP.error) |
| 11870 |
{ |
| 11871 |
result = exp.e1; |
| 11872 |
return; |
| 11873 |
} |
| 11874 |
if (exp.e2.op == EXP.error) |
| 11875 |
{ |
| 11876 |
result = exp.e2; |
| 11877 |
return; |
| 11878 |
} |
| 11879 |
|
| 11880 |
if (checkNonAssignmentArrayOp(exp.e2)) |
| 11881 |
return setError(); |
| 11882 |
|
| 11883 |
Type tb1 = exp.e1.type.toBasetype(); |
| 11884 |
Type tb1next = tb1.nextOf(); |
| 11885 |
Type tb2 = exp.e2.type.toBasetype(); |
| 11886 |
|
| 11887 |
/* Possibilities: |
| 11888 |
* EXP.concatenateAssign: appending T[] to T[] |
| 11889 |
* EXP.concatenateElemAssign: appending T to T[] |
| 11890 |
* EXP.concatenateDcharAssign: appending dchar to T[] |
| 11891 |
*/ |
| 11892 |
if ((tb1.ty == Tarray) && |
| 11893 |
tb2.isStaticOrDynamicArray() && |
| 11894 |
(exp.e2.implicitConvTo(exp.e1.type) || |
| 11895 |
(tb2.nextOf().implicitConvTo(tb1next) && |
| 11896 |
// Do not strip const(void)[] |
| 11897 |
(!sc.previews.fixImmutableConv || tb1next.ty != Tvoid) && |
| 11898 |
(tb2.nextOf().size(Loc.initial) == tb1next.size(Loc.initial))))) |
| 11899 |
{ |
| 11900 |
// EXP.concatenateAssign |
| 11901 |
assert(exp.op == EXP.concatenateAssign); |
| 11902 |
if (tb1next.checkPostblit(exp.e1.loc, sc)) |
| 11903 |
return setError(); |
| 11904 |
|
| 11905 |
exp.e2 = exp.e2.castTo(sc, exp.e1.type); |
| 11906 |
} |
| 11907 |
else if ((tb1.ty == Tarray) && exp.e2.implicitConvTo(tb1next)) |
| 11908 |
{ |
| 11909 |
/* https://issues.dlang.org/show_bug.cgi?id=19782 |
| 11910 |
* |
| 11911 |
* If e2 is implicitly convertible to tb1next, the conversion |
| 11912 |
* might be done through alias this, in which case, e2 needs to |
| 11913 |
* be modified accordingly (e2 => e2.aliasthis). |
| 11914 |
*/ |
| 11915 |
if (tb2.ty == Tstruct && (cast(TypeStruct)tb2).implicitConvToThroughAliasThis(tb1next)) |
| 11916 |
goto Laliasthis; |
| 11917 |
if (tb2.ty == Tclass && (cast(TypeClass)tb2).implicitConvToThroughAliasThis(tb1next)) |
| 11918 |
goto Laliasthis; |
| 11919 |
// Append element |
| 11920 |
if (tb2.checkPostblit(exp.e2.loc, sc)) |
| 11921 |
return setError(); |
| 11922 |
|
| 11923 |
if (checkNewEscape(*sc, exp.e2, false)) |
| 11924 |
return setError(); |
| 11925 |
|
| 11926 |
auto ecast = exp.e2.castTo(sc, tb1next); |
| 11927 |
if (auto ce = ecast.isCastExp()) |
| 11928 |
ce.trusted = true; |
| 11929 |
|
| 11930 |
exp = new CatElemAssignExp(exp.loc, exp.type, exp.e1, ecast); |
| 11931 |
exp.e2 = doCopyOrMove(sc, exp.e2, null, false); |
| 11932 |
} |
| 11933 |
else if (tb1.ty == Tarray && |
| 11934 |
(tb1next.ty == Tchar || tb1next.ty == Twchar) && |
| 11935 |
exp.e2.type.ty != tb1next.ty && |
| 11936 |
exp.e2.implicitConvTo(Type.tdchar)) |
| 11937 |
{ |
| 11938 |
// Append dchar to char[] or wchar[] |
| 11939 |
exp = new CatDcharAssignExp(exp.loc, exp.type, exp.e1, exp.e2.castTo(sc, Type.tdchar)); |
| 11940 |
|
| 11941 |
/* Do not allow appending wchar to char[] because if wchar happens |
| 11942 |
* to be a surrogate pair, nothing good can result. |
| 11943 |
*/ |
| 11944 |
} |
| 11945 |
else |
| 11946 |
{ |
| 11947 |
Laliasthis: |
| 11948 |
result = checkAliasThisForLhs(isAggregate(exp.e1.type), sc, exp, aliasThisStop); |
| 11949 |
if (result) |
| 11950 |
return; |
| 11951 |
|
| 11952 |
result = checkAliasThisForRhs(isAggregate(exp.e2.type), sc, exp, aliasThisStop); |
| 11953 |
if (result) |
| 11954 |
return; |
| 11955 |
|
| 11956 |
error(exp.loc, "cannot append type `%s` to type `%s`", tb2.toChars(), tb1.toChars()); |
| 11957 |
return setError(); |
| 11958 |
} |
| 11959 |
|
| 11960 |
if (exp.e2.checkValue() || exp.e2.checkSharedAccess(sc)) |
| 11961 |
return setError(); |
| 11962 |
|
| 11963 |
exp.type = exp.e1.type; |
| 11964 |
auto assignElem = exp.e2; |
| 11965 |
auto res = exp.reorderSettingAAElem(sc); |
| 11966 |
if (res != exp) // `AA[k] = v` rewrite was performed |
| 11967 |
checkNewEscape(*sc, assignElem, false); |
| 11968 |
else if (exp.op == EXP.concatenateElemAssign || exp.op == EXP.concatenateDcharAssign) |
| 11969 |
checkAssignEscape(*sc, res, false, false); |
| 11970 |
|
| 11971 |
result = res; |
| 11972 |
|
| 11973 |
if ((exp.op == EXP.concatenateAssign || exp.op == EXP.concatenateElemAssign) && sc.needsCodegen()) |
| 11974 |
{ |
| 11975 |
// if aa ordering is triggered, `res` will be a CommaExp |
| 11976 |
// and `.e2` will be the rewritten original expression. |
| 11977 |
|
| 11978 |
// `output` will point to the expression that the lowering will overwrite |
| 11979 |
Expression* output; |
| 11980 |
if (auto comma = res.isCommaExp()) |
| 11981 |
{ |
| 11982 |
output = &comma.e2; |
| 11983 |
// manual cast because it could be either CatAssignExp or CatElemAssignExp |
| 11984 |
exp = cast(CatAssignExp)comma.e2; |
| 11985 |
} |
| 11986 |
else |
| 11987 |
{ |
| 11988 |
output = &result; |
| 11989 |
exp = cast(CatAssignExp)result; |
| 11990 |
} |
| 11991 |
|
| 11992 |
if (exp.op == EXP.concatenateAssign) |
| 11993 |
{ |
| 11994 |
Identifier hook = global.params.tracegc ? Id._d_arrayappendTTrace : Id._d_arrayappendT; |
| 11995 |
|
| 11996 |
if (!verifyHookExist(exp.loc, *sc, hook, "appending array to arrays", Id.object)) |
| 11997 |
return setError(); |
| 11998 |
|
| 11999 |
// Lower to object._d_arrayappendT{,Trace}({file, line, funcname}, e1, e2) |
| 12000 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 12001 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 12002 |
id = new DotIdExp(exp.loc, id, hook); |
| 12003 |
|
| 12004 |
auto arguments = new Expressions(); |
| 12005 |
arguments.push(exp.e1); |
| 12006 |
arguments.push(exp.e2); |
| 12007 |
Expression ce = new CallExp(exp.loc, id, arguments); |
| 12008 |
|
| 12009 |
exp.lowering = ce.expressionSemantic(sc); |
| 12010 |
*output = exp; |
| 12011 |
} |
| 12012 |
else if (exp.op == EXP.concatenateElemAssign) |
| 12013 |
{ |
| 12014 |
/* Do not lower concats to the indices array returned by |
| 12015 |
*`static foreach`, as this array is only used at compile-time. |
| 12016 |
*/ |
| 12017 |
if (auto ve = exp.e1.isVarExp) |
| 12018 |
{ |
| 12019 |
import core.stdc.ctype : isdigit; |
| 12020 |
// The name of the indices array that static foreach loops uses. |
| 12021 |
// See dmd.cond.lowerNonArrayAggregate |
| 12022 |
enum varName = "__res"; |
| 12023 |
const(char)[] id = ve.var.ident.toString; |
| 12024 |
if (ve.var.storage_class & STC.temp && id.length > varName.length && |
| 12025 |
id[0 .. varName.length] == varName && id[varName.length].isdigit) |
| 12026 |
return; |
| 12027 |
} |
| 12028 |
|
| 12029 |
Identifier hook = global.params.tracegc ? Id._d_arrayappendcTXTrace : Id._d_arrayappendcTX; |
| 12030 |
if (!verifyHookExist(exp.loc, *sc, hook, "appending element to arrays", Id.object)) |
| 12031 |
return setError(); |
| 12032 |
|
| 12033 |
// Lower to object._d_arrayappendcTX{,Trace}(e1, 1), e1[$-1]=e2 |
| 12034 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 12035 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 12036 |
id = new DotIdExp(exp.loc, id, hook); |
| 12037 |
|
| 12038 |
auto arguments = new Expressions(); |
| 12039 |
Expression eValue1; |
| 12040 |
Expression value1 = extractSideEffect(sc, "__appendtmp", eValue1, exp.e1); |
| 12041 |
|
| 12042 |
arguments.push(value1); |
| 12043 |
arguments.push(new IntegerExp(exp.loc, 1, Type.tsize_t)); |
| 12044 |
|
| 12045 |
Expression ce = new CallExp(exp.loc, id, arguments); |
| 12046 |
|
| 12047 |
Expression eValue2; |
| 12048 |
Expression value2 = exp.e2; |
| 12049 |
if (!value2.isVarExp() && !value2.isConst()) |
| 12050 |
{ |
| 12051 |
/* Before the template hook, this check was performed in e2ir.d |
| 12052 |
* for expressions like `a ~= a[$-1]`. Here, $ will be modified |
| 12053 |
* by calling `_d_arrayappendcTX`, so we need to save `a[$-1]` in |
| 12054 |
* a temporary variable. |
| 12055 |
*/ |
| 12056 |
value2 = extractSideEffect(sc, "__appendtmp", eValue2, value2, true); |
| 12057 |
|
| 12058 |
// `__appendtmp*` will be destroyed together with the array `exp.e1`. |
| 12059 |
auto vd = eValue2.isDeclarationExp().declaration.isVarDeclaration(); |
| 12060 |
vd.storage_class |= STC.nodtor; |
| 12061 |
// Be more explicit that this "declaration" is local to the expression |
| 12062 |
vd.storage_class |= STC.exptemp; |
| 12063 |
} |
| 12064 |
|
| 12065 |
auto ale = new ArrayLengthExp(exp.loc, value1); |
| 12066 |
auto elem = new IndexExp(exp.loc, value1, new MinExp(exp.loc, ale, IntegerExp.literal!1)); |
| 12067 |
auto ae = new ConstructExp(exp.loc, elem, value2); |
| 12068 |
|
| 12069 |
auto e0 = Expression.combine(ce, ae).expressionSemantic(sc); |
| 12070 |
e0 = Expression.combine(e0, value1); |
| 12071 |
e0 = Expression.combine(eValue1, e0); |
| 12072 |
e0 = Expression.combine(eValue2, e0); |
| 12073 |
|
| 12074 |
exp.lowering = e0.expressionSemantic(sc); |
| 12075 |
*output = exp; |
| 12076 |
} |
| 12077 |
} |
| 12078 |
} |
| 12079 |
|
| 12080 |
override void visit(AddExp exp) |
| 12081 |
{ |
| 12082 |
static if (LOGSEMANTIC) |
| 12083 |
{ |
| 12084 |
printf("AddExp::semantic('%s')\n", exp.toChars()); |
| 12085 |
} |
| 12086 |
|
| 12087 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12088 |
{ |
| 12089 |
result = e; |
| 12090 |
return; |
| 12091 |
} |
| 12092 |
|
| 12093 |
/* ImportC: convert arrays to pointers, functions to pointers to functions |
| 12094 |
*/ |
| 12095 |
exp.e1 = exp.e1.arrayFuncConv(sc); |
| 12096 |
exp.e2 = exp.e2.arrayFuncConv(sc); |
| 12097 |
|
| 12098 |
Type tb1 = exp.e1.type.toBasetype(); |
| 12099 |
Type tb2 = exp.e2.type.toBasetype(); |
| 12100 |
|
| 12101 |
bool err = false; |
| 12102 |
if (tb1.ty == Tdelegate || tb1.isPtrToFunction()) |
| 12103 |
{ |
| 12104 |
err |= exp.e1.checkArithmetic(exp.op) || exp.e1.checkSharedAccess(sc); |
| 12105 |
} |
| 12106 |
if (tb2.ty == Tdelegate || tb2.isPtrToFunction()) |
| 12107 |
{ |
| 12108 |
err |= exp.e2.checkArithmetic(exp.op) || exp.e2.checkSharedAccess(sc); |
| 12109 |
} |
| 12110 |
if (err) |
| 12111 |
return setError(); |
| 12112 |
|
| 12113 |
if (tb1.ty == Tpointer && exp.e2.type.isIntegral() || tb2.ty == Tpointer && exp.e1.type.isIntegral()) |
| 12114 |
{ |
| 12115 |
result = scaleFactor(exp, sc); |
| 12116 |
return; |
| 12117 |
} |
| 12118 |
|
| 12119 |
if (tb1.ty == Tpointer && tb2.ty == Tpointer || |
| 12120 |
tb1.ty == Tnull && tb2.ty == Tnull) |
| 12121 |
{ |
| 12122 |
result = exp.incompatibleTypes(); |
| 12123 |
return; |
| 12124 |
} |
| 12125 |
|
| 12126 |
if (Expression ex = typeCombine(exp, sc)) |
| 12127 |
{ |
| 12128 |
result = ex; |
| 12129 |
return; |
| 12130 |
} |
| 12131 |
|
| 12132 |
Type tb = exp.type.toBasetype(); |
| 12133 |
if (tb.isStaticOrDynamicArray()) |
| 12134 |
{ |
| 12135 |
if (!isArrayOpValid(exp)) |
| 12136 |
{ |
| 12137 |
result = arrayOpInvalidError(exp); |
| 12138 |
return; |
| 12139 |
} |
| 12140 |
result = exp; |
| 12141 |
return; |
| 12142 |
} |
| 12143 |
|
| 12144 |
if (exp.suggestBinaryOverloads(sc) || exp.checkArithmeticBin()) |
| 12145 |
return setError(); |
| 12146 |
|
| 12147 |
tb1 = exp.e1.type.toBasetype(); |
| 12148 |
if (!target.isVectorOpSupported(tb1, exp.op, tb2)) |
| 12149 |
{ |
| 12150 |
result = exp.incompatibleTypes(); |
| 12151 |
return; |
| 12152 |
} |
| 12153 |
if ((tb1.isReal() && exp.e2.type.isImaginary()) || (tb1.isImaginary() && exp.e2.type.isReal())) |
| 12154 |
{ |
| 12155 |
switch (exp.type.toBasetype().ty) |
| 12156 |
{ |
| 12157 |
case Tfloat32: |
| 12158 |
case Timaginary32: |
| 12159 |
exp.type = Type.tcomplex32; |
| 12160 |
break; |
| 12161 |
|
| 12162 |
case Tfloat64: |
| 12163 |
case Timaginary64: |
| 12164 |
exp.type = Type.tcomplex64; |
| 12165 |
break; |
| 12166 |
|
| 12167 |
case Tfloat80: |
| 12168 |
case Timaginary80: |
| 12169 |
exp.type = Type.tcomplex80; |
| 12170 |
break; |
| 12171 |
|
| 12172 |
default: |
| 12173 |
assert(0); |
| 12174 |
} |
| 12175 |
} |
| 12176 |
result = exp; |
| 12177 |
} |
| 12178 |
|
| 12179 |
override void visit(MinExp exp) |
| 12180 |
{ |
| 12181 |
static if (LOGSEMANTIC) |
| 12182 |
{ |
| 12183 |
printf("MinExp::semantic('%s')\n", exp.toChars()); |
| 12184 |
} |
| 12185 |
|
| 12186 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12187 |
{ |
| 12188 |
result = e; |
| 12189 |
return; |
| 12190 |
} |
| 12191 |
|
| 12192 |
/* ImportC: convert arrays to pointers, functions to pointers to functions |
| 12193 |
*/ |
| 12194 |
exp.e1 = exp.e1.arrayFuncConv(sc); |
| 12195 |
exp.e2 = exp.e2.arrayFuncConv(sc); |
| 12196 |
|
| 12197 |
Type t1 = exp.e1.type.toBasetype(); |
| 12198 |
Type t2 = exp.e2.type.toBasetype(); |
| 12199 |
|
| 12200 |
bool err = false; |
| 12201 |
if (t1.ty == Tdelegate || t1.isPtrToFunction()) |
| 12202 |
{ |
| 12203 |
err |= exp.e1.checkArithmetic(exp.op) || exp.e1.checkSharedAccess(sc); |
| 12204 |
} |
| 12205 |
if (t2.ty == Tdelegate || t2.isPtrToFunction()) |
| 12206 |
{ |
| 12207 |
err |= exp.e2.checkArithmetic(exp.op) || exp.e2.checkSharedAccess(sc); |
| 12208 |
} |
| 12209 |
if (t1.ty == Tnull && t2.ty == Tnull) |
| 12210 |
{ |
| 12211 |
exp.incompatibleTypes(); |
| 12212 |
return setError(); |
| 12213 |
} |
| 12214 |
if (err) |
| 12215 |
return setError(); |
| 12216 |
|
| 12217 |
if (t1.ty == Tpointer) |
| 12218 |
{ |
| 12219 |
Expression e; |
| 12220 |
if (t2.ty == Tpointer) |
| 12221 |
{ |
| 12222 |
// https://dlang.org/spec/expression.html#add_expressions |
| 12223 |
// "If both operands are pointers, and the operator is -, the pointers are |
| 12224 |
// subtracted and the result is divided by the size of the type pointed to |
| 12225 |
// by the operands. It is an error if the pointers point to different types." |
| 12226 |
Type p1 = t1.nextOf(); |
| 12227 |
Type p2 = t2.nextOf(); |
| 12228 |
|
| 12229 |
if (!p1.equivalent(p2)) |
| 12230 |
{ |
| 12231 |
// See https://github.com/dlang/dmd/pull/7332 |
| 12232 |
error(exp.loc, "cannot subtract pointers to different types: `%s` and `%s`.", |
| 12233 |
t1.toChars(), t2.toChars()); |
| 12234 |
return setError(); |
| 12235 |
} |
| 12236 |
|
| 12237 |
// Need to divide the result by the stride |
| 12238 |
// Replace (ptr - ptr) with (ptr - ptr) / stride |
| 12239 |
long stride; |
| 12240 |
|
| 12241 |
// make sure pointer types are compatible |
| 12242 |
if (Expression ex = typeCombine(exp, sc)) |
| 12243 |
{ |
| 12244 |
result = ex; |
| 12245 |
return; |
| 12246 |
} |
| 12247 |
|
| 12248 |
exp.type = Type.tptrdiff_t; |
| 12249 |
stride = t2.nextOf().size(); |
| 12250 |
if (stride == 0) |
| 12251 |
{ |
| 12252 |
e = new IntegerExp(exp.loc, 0, Type.tptrdiff_t); |
| 12253 |
} |
| 12254 |
else if (stride == cast(long)SIZE_INVALID) |
| 12255 |
e = ErrorExp.get(); |
| 12256 |
else |
| 12257 |
{ |
| 12258 |
e = new DivExp(exp.loc, exp, new IntegerExp(Loc.initial, stride, Type.tptrdiff_t)); |
| 12259 |
e.type = Type.tptrdiff_t; |
| 12260 |
} |
| 12261 |
} |
| 12262 |
else if (t2.isIntegral()) |
| 12263 |
e = scaleFactor(exp, sc); |
| 12264 |
else |
| 12265 |
{ |
| 12266 |
error(exp.loc, "can't subtract `%s` from pointer", t2.toChars()); |
| 12267 |
e = ErrorExp.get(); |
| 12268 |
} |
| 12269 |
result = e; |
| 12270 |
return; |
| 12271 |
} |
| 12272 |
if (t2.ty == Tpointer) |
| 12273 |
{ |
| 12274 |
exp.type = exp.e2.type; |
| 12275 |
error(exp.loc, "can't subtract pointer from `%s`", exp.e1.type.toChars()); |
| 12276 |
return setError(); |
| 12277 |
} |
| 12278 |
|
| 12279 |
if (Expression ex = typeCombine(exp, sc)) |
| 12280 |
{ |
| 12281 |
result = ex; |
| 12282 |
return; |
| 12283 |
} |
| 12284 |
|
| 12285 |
Type tb = exp.type.toBasetype(); |
| 12286 |
if (tb.isStaticOrDynamicArray()) |
| 12287 |
{ |
| 12288 |
if (!isArrayOpValid(exp)) |
| 12289 |
{ |
| 12290 |
result = arrayOpInvalidError(exp); |
| 12291 |
return; |
| 12292 |
} |
| 12293 |
result = exp; |
| 12294 |
return; |
| 12295 |
} |
| 12296 |
|
| 12297 |
if (exp.suggestBinaryOverloads(sc) || exp.checkArithmeticBin()) |
| 12298 |
return setError(); |
| 12299 |
|
| 12300 |
t1 = exp.e1.type.toBasetype(); |
| 12301 |
t2 = exp.e2.type.toBasetype(); |
| 12302 |
if (!target.isVectorOpSupported(t1, exp.op, t2)) |
| 12303 |
{ |
| 12304 |
result = exp.incompatibleTypes(); |
| 12305 |
return; |
| 12306 |
} |
| 12307 |
if ((t1.isReal() && t2.isImaginary()) || (t1.isImaginary() && t2.isReal())) |
| 12308 |
{ |
| 12309 |
switch (exp.type.ty) |
| 12310 |
{ |
| 12311 |
case Tfloat32: |
| 12312 |
case Timaginary32: |
| 12313 |
exp.type = Type.tcomplex32; |
| 12314 |
break; |
| 12315 |
|
| 12316 |
case Tfloat64: |
| 12317 |
case Timaginary64: |
| 12318 |
exp.type = Type.tcomplex64; |
| 12319 |
break; |
| 12320 |
|
| 12321 |
case Tfloat80: |
| 12322 |
case Timaginary80: |
| 12323 |
exp.type = Type.tcomplex80; |
| 12324 |
break; |
| 12325 |
|
| 12326 |
default: |
| 12327 |
assert(0); |
| 12328 |
} |
| 12329 |
} |
| 12330 |
result = exp; |
| 12331 |
return; |
| 12332 |
} |
| 12333 |
|
| 12334 |
/** |
| 12335 |
* If the given expression is a `CatExp`, the function tries to lower it to |
| 12336 |
* `_d_arraycatnTX`. |
| 12337 |
* |
| 12338 |
* Params: |
| 12339 |
* ee = the `CatExp` to lower |
| 12340 |
* Returns: |
| 12341 |
* `_d_arraycatnTX(e1, e2, ..., en)` if `ee` is `e1 ~ e2 ~ ... en` |
| 12342 |
* `ee` otherwise |
| 12343 |
*/ |
| 12344 |
private Expression lowerToArrayCat(CatExp exp) |
| 12345 |
{ |
| 12346 |
// String literals are concatenated by the compiler. No lowering is needed. |
| 12347 |
if ((exp.e1.isStringExp() && (exp.e2.isIntegerExp() || exp.e2.isStringExp())) || |
| 12348 |
(exp.e2.isStringExp() && (exp.e1.isIntegerExp() || exp.e1.isStringExp()))) |
| 12349 |
return exp; |
| 12350 |
|
| 12351 |
bool useTraceGCHook = global.params.tracegc && sc.needsCodegen(); |
| 12352 |
|
| 12353 |
Identifier hook = useTraceGCHook ? Id._d_arraycatnTXTrace : Id._d_arraycatnTX; |
| 12354 |
if (!verifyHookExist(exp.loc, *sc, hook, "concatenating arrays")) |
| 12355 |
{ |
| 12356 |
setError(); |
| 12357 |
return result; |
| 12358 |
} |
| 12359 |
|
| 12360 |
void handleCatArgument(Expressions* arguments, Expression e, Type catType, bool isRightArg) |
| 12361 |
{ |
| 12362 |
auto tb = e.type.toBasetype(); |
| 12363 |
|
| 12364 |
if ((isRightArg && e.parens) || (!isRightArg && !tb.equals(catType))) |
| 12365 |
{ |
| 12366 |
arguments.push(e); |
| 12367 |
return; |
| 12368 |
} |
| 12369 |
|
| 12370 |
if (auto ce = e.isCatExp()) |
| 12371 |
{ |
| 12372 |
Expression lowering = ce.lowering; |
| 12373 |
|
| 12374 |
/* Skip `file`, `line`, and `funcname` if the hook of the parent |
| 12375 |
* `CatExp` is `_d_arraycatnTXTrace`. |
| 12376 |
*/ |
| 12377 |
if (auto callExp = isRuntimeHook(lowering, hook)) |
| 12378 |
{ |
| 12379 |
if (hook == Id._d_arraycatnTX) |
| 12380 |
arguments.pushSlice((*callExp.arguments)[]); |
| 12381 |
else |
| 12382 |
arguments.pushSlice((*callExp.arguments)[0 .. $ - 3]); |
| 12383 |
} |
| 12384 |
} |
| 12385 |
else |
| 12386 |
arguments.push(e); |
| 12387 |
} |
| 12388 |
|
| 12389 |
auto arguments = new Expressions(); |
| 12390 |
handleCatArgument(arguments, exp.e1, exp.type.toBasetype(), false); |
| 12391 |
handleCatArgument(arguments, exp.e2, exp.type.toBasetype(), true); |
| 12392 |
|
| 12393 |
Expression id = new IdentifierExp(exp.loc, Id.empty); |
| 12394 |
id = new DotIdExp(exp.loc, id, Id.object); |
| 12395 |
|
| 12396 |
auto tiargs = new Objects(); |
| 12397 |
tiargs.push(exp.type); |
| 12398 |
id = new DotTemplateInstanceExp(exp.loc, id, hook, tiargs); |
| 12399 |
id = new CallExp(exp.loc, id, arguments); |
| 12400 |
return id.expressionSemantic(sc); |
| 12401 |
} |
| 12402 |
|
| 12403 |
void trySetCatExpLowering(Expression exp) |
| 12404 |
{ |
| 12405 |
/* `_d_arraycatnTX` canot be used with `-betterC`, but `CatExp`s may be |
| 12406 |
* used with `-betterC`, but only during CTFE. |
| 12407 |
*/ |
| 12408 |
if (!global.params.useGC) |
| 12409 |
return; |
| 12410 |
|
| 12411 |
if (auto ce = exp.isCatExp()) |
| 12412 |
ce.lowering = lowerToArrayCat(ce); |
| 12413 |
} |
| 12414 |
|
| 12415 |
override void visit(CatExp exp) |
| 12416 |
{ |
| 12417 |
// https://dlang.org/spec/expression.html#cat_expressions |
| 12418 |
//printf("CatExp.semantic() %s\n", toChars()); |
| 12419 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12420 |
{ |
| 12421 |
result = e; |
| 12422 |
return; |
| 12423 |
} |
| 12424 |
|
| 12425 |
Type tb1 = exp.e1.type.toBasetype(); |
| 12426 |
Type tb2 = exp.e2.type.toBasetype(); |
| 12427 |
|
| 12428 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 12429 |
auto f2 = checkNonAssignmentArrayOp(exp.e2); |
| 12430 |
if (f1 || f2) |
| 12431 |
return setError(); |
| 12432 |
|
| 12433 |
Type tb1next = tb1.nextOf(); |
| 12434 |
Type tb2next = tb2.nextOf(); |
| 12435 |
|
| 12436 |
// Check for: array ~ array |
| 12437 |
if (tb1next && tb2next && (tb1next.implicitConvTo(tb2next) >= MATCH.constant || tb2next.implicitConvTo(tb1next) >= MATCH.constant || exp.e1.op == EXP.arrayLiteral && exp.e1.implicitConvTo(tb2) || exp.e2.op == EXP.arrayLiteral && exp.e2.implicitConvTo(tb1))) |
| 12438 |
{ |
| 12439 |
/* https://issues.dlang.org/show_bug.cgi?id=9248 |
| 12440 |
* Here to avoid the case of: |
| 12441 |
* void*[] a = [cast(void*)1]; |
| 12442 |
* void*[] b = [cast(void*)2]; |
| 12443 |
* a ~ b; |
| 12444 |
* becoming: |
| 12445 |
* a ~ [cast(void*)b]; |
| 12446 |
*/ |
| 12447 |
|
| 12448 |
/* https://issues.dlang.org/show_bug.cgi?id=14682 |
| 12449 |
* Also to avoid the case of: |
| 12450 |
* int[][] a; |
| 12451 |
* a ~ []; |
| 12452 |
* becoming: |
| 12453 |
* a ~ cast(int[])[]; |
| 12454 |
*/ |
| 12455 |
goto Lpeer; |
| 12456 |
} |
| 12457 |
|
| 12458 |
// Check for: array ~ element |
| 12459 |
if (tb1.isStaticOrDynamicArray() && tb2.ty != Tvoid) |
| 12460 |
{ |
| 12461 |
if (exp.e1.op == EXP.arrayLiteral) |
| 12462 |
{ |
| 12463 |
exp.e2 = doCopyOrMove(sc, exp.e2, null, false); |
| 12464 |
// https://issues.dlang.org/show_bug.cgi?id=14686 |
| 12465 |
// Postblit call appears in AST, and this is |
| 12466 |
// finally translated to an ArrayLiteralExp in below optimize(). |
| 12467 |
} |
| 12468 |
else if (exp.e1.op == EXP.string_) |
| 12469 |
{ |
| 12470 |
// No postblit call exists on character (integer) value. |
| 12471 |
} |
| 12472 |
else |
| 12473 |
{ |
| 12474 |
if (tb2.checkPostblit(exp.e2.loc, sc)) |
| 12475 |
return setError(); |
| 12476 |
// Postblit call will be done in runtime helper function |
| 12477 |
} |
| 12478 |
|
| 12479 |
if (exp.e1.op == EXP.arrayLiteral && exp.e1.implicitConvTo(tb2.arrayOf())) |
| 12480 |
{ |
| 12481 |
exp.e1 = exp.e1.implicitCastTo(sc, tb2.arrayOf()); |
| 12482 |
exp.type = tb2.arrayOf(); |
| 12483 |
goto L2elem; |
| 12484 |
} |
| 12485 |
if (exp.e2.implicitConvTo(tb1next) >= MATCH.convert) |
| 12486 |
{ |
| 12487 |
exp.e2 = exp.e2.implicitCastTo(sc, tb1next); |
| 12488 |
exp.type = tb1next.arrayOf(); |
| 12489 |
L2elem: |
| 12490 |
if (checkNewEscape(*sc, exp.e2, false)) |
| 12491 |
return setError(); |
| 12492 |
result = exp.optimize(WANTvalue); |
| 12493 |
trySetCatExpLowering(result); |
| 12494 |
return; |
| 12495 |
} |
| 12496 |
} |
| 12497 |
// Check for: element ~ array |
| 12498 |
if (tb2.isStaticOrDynamicArray() && tb1.ty != Tvoid) |
| 12499 |
{ |
| 12500 |
if (exp.e2.op == EXP.arrayLiteral) |
| 12501 |
{ |
| 12502 |
exp.e1 = doCopyOrMove(sc, exp.e1, null, false); |
| 12503 |
} |
| 12504 |
else if (exp.e2.op == EXP.string_) |
| 12505 |
{ |
| 12506 |
} |
| 12507 |
else |
| 12508 |
{ |
| 12509 |
if (tb1.checkPostblit(exp.e1.loc, sc)) |
| 12510 |
return setError(); |
| 12511 |
} |
| 12512 |
|
| 12513 |
if (exp.e2.op == EXP.arrayLiteral && exp.e2.implicitConvTo(tb1.arrayOf())) |
| 12514 |
{ |
| 12515 |
exp.e2 = exp.e2.implicitCastTo(sc, tb1.arrayOf()); |
| 12516 |
exp.type = tb1.arrayOf(); |
| 12517 |
goto L1elem; |
| 12518 |
} |
| 12519 |
if (exp.e1.implicitConvTo(tb2next) >= MATCH.convert) |
| 12520 |
{ |
| 12521 |
exp.e1 = exp.e1.implicitCastTo(sc, tb2next); |
| 12522 |
exp.type = tb2next.arrayOf(); |
| 12523 |
L1elem: |
| 12524 |
if (checkNewEscape(*sc, exp.e1, false)) |
| 12525 |
return setError(); |
| 12526 |
result = exp.optimize(WANTvalue); |
| 12527 |
trySetCatExpLowering(result); |
| 12528 |
return; |
| 12529 |
} |
| 12530 |
} |
| 12531 |
|
| 12532 |
Lpeer: |
| 12533 |
if (tb1.isStaticOrDynamicArray() && tb2.isStaticOrDynamicArray() && |
| 12534 |
(tb1next.mod || tb2next.mod) && (tb1next.mod != tb2next.mod)) |
| 12535 |
{ |
| 12536 |
Type t1 = tb1next.mutableOf().constOf().arrayOf(); |
| 12537 |
Type t2 = tb2next.mutableOf().constOf().arrayOf(); |
| 12538 |
if (exp.e1.op == EXP.string_ && !(cast(StringExp)exp.e1).committed) |
| 12539 |
exp.e1.type = t1; |
| 12540 |
else |
| 12541 |
exp.e1 = exp.e1.castTo(sc, t1); |
| 12542 |
if (exp.e2.op == EXP.string_ && !(cast(StringExp)exp.e2).committed) |
| 12543 |
exp.e2.type = t2; |
| 12544 |
else |
| 12545 |
exp.e2 = exp.e2.castTo(sc, t2); |
| 12546 |
} |
| 12547 |
|
| 12548 |
if (Expression ex = typeCombine(exp, sc)) |
| 12549 |
{ |
| 12550 |
result = ex; |
| 12551 |
trySetCatExpLowering(result); |
| 12552 |
return; |
| 12553 |
} |
| 12554 |
exp.type = exp.type.toHeadMutable(); |
| 12555 |
|
| 12556 |
Type tb = exp.type.toBasetype(); |
| 12557 |
if (tb.ty == Tsarray) |
| 12558 |
exp.type = tb.nextOf().arrayOf(); |
| 12559 |
if (exp.type.ty == Tarray && tb1next && tb2next && tb1next.mod != tb2next.mod) |
| 12560 |
{ |
| 12561 |
// Do not strip const(void)[] |
| 12562 |
if (!sc.previews.fixImmutableConv || tb.nextOf().ty != Tvoid) |
| 12563 |
exp.type = exp.type.nextOf().toHeadMutable().arrayOf(); |
| 12564 |
} |
| 12565 |
if (Type tbn = tb.nextOf()) |
| 12566 |
{ |
| 12567 |
if (tbn.checkPostblit(exp.loc, sc)) |
| 12568 |
return setError(); |
| 12569 |
} |
| 12570 |
Type t1 = exp.e1.type.toBasetype(); |
| 12571 |
Type t2 = exp.e2.type.toBasetype(); |
| 12572 |
Expression e; |
| 12573 |
if (t1.isStaticOrDynamicArray() && t2.isStaticOrDynamicArray()) |
| 12574 |
{ |
| 12575 |
// Normalize to ArrayLiteralExp or StringExp as far as possible |
| 12576 |
e = exp.optimize(WANTvalue); |
| 12577 |
} |
| 12578 |
else |
| 12579 |
{ |
| 12580 |
//printf("(%s) ~ (%s)\n", e1.toChars(), e2.toChars()); |
| 12581 |
result = exp.incompatibleTypes(); |
| 12582 |
return; |
| 12583 |
} |
| 12584 |
|
| 12585 |
result = e; |
| 12586 |
trySetCatExpLowering(result); |
| 12587 |
} |
| 12588 |
|
| 12589 |
bool commonArithBinOpSemantic(BinExp exp) |
| 12590 |
{ |
| 12591 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12592 |
{ |
| 12593 |
result = e; |
| 12594 |
return true; |
| 12595 |
} |
| 12596 |
|
| 12597 |
if (Expression ex = typeCombine(exp, sc)) |
| 12598 |
{ |
| 12599 |
result = ex; |
| 12600 |
return true; |
| 12601 |
} |
| 12602 |
|
| 12603 |
Type tb = exp.type.toBasetype(); |
| 12604 |
if (tb.isStaticOrDynamicArray()) |
| 12605 |
{ |
| 12606 |
if (!isArrayOpValid(exp)) |
| 12607 |
{ |
| 12608 |
result = arrayOpInvalidError(exp); |
| 12609 |
return true; |
| 12610 |
} |
| 12611 |
result = exp; |
| 12612 |
return true; |
| 12613 |
} |
| 12614 |
|
| 12615 |
if (exp.suggestBinaryOverloads(sc) || exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc)) |
| 12616 |
{ |
| 12617 |
setError(); |
| 12618 |
return true; |
| 12619 |
} |
| 12620 |
return false; |
| 12621 |
} |
| 12622 |
override void visit(MulExp exp) |
| 12623 |
{ |
| 12624 |
version (none) |
| 12625 |
{ |
| 12626 |
printf("MulExp::semantic() %s\n", exp.toChars()); |
| 12627 |
} |
| 12628 |
|
| 12629 |
if (commonArithBinOpSemantic(exp)) |
| 12630 |
return; |
| 12631 |
if (exp.type.isFloating()) |
| 12632 |
{ |
| 12633 |
Type t1 = exp.e1.type; |
| 12634 |
Type t2 = exp.e2.type; |
| 12635 |
|
| 12636 |
if (t1.isReal()) |
| 12637 |
{ |
| 12638 |
exp.type = t2; |
| 12639 |
} |
| 12640 |
else if (t2.isReal()) |
| 12641 |
{ |
| 12642 |
exp.type = t1; |
| 12643 |
} |
| 12644 |
else if (t1.isImaginary()) |
| 12645 |
{ |
| 12646 |
if (t2.isImaginary()) |
| 12647 |
{ |
| 12648 |
switch (t1.toBasetype().ty) |
| 12649 |
{ |
| 12650 |
case Timaginary32: |
| 12651 |
exp.type = Type.tfloat32; |
| 12652 |
break; |
| 12653 |
|
| 12654 |
case Timaginary64: |
| 12655 |
exp.type = Type.tfloat64; |
| 12656 |
break; |
| 12657 |
|
| 12658 |
case Timaginary80: |
| 12659 |
exp.type = Type.tfloat80; |
| 12660 |
break; |
| 12661 |
|
| 12662 |
default: |
| 12663 |
assert(0); |
| 12664 |
} |
| 12665 |
|
| 12666 |
// iy * iv = -yv |
| 12667 |
exp.e1.type = exp.type; |
| 12668 |
exp.e2.type = exp.type; |
| 12669 |
Expression e = new NegExp(exp.loc, exp); |
| 12670 |
e = e.expressionSemantic(sc); |
| 12671 |
result = e; |
| 12672 |
return; |
| 12673 |
} |
| 12674 |
else |
| 12675 |
exp.type = t2; // t2 is complex |
| 12676 |
} |
| 12677 |
else if (t2.isImaginary()) |
| 12678 |
{ |
| 12679 |
exp.type = t1; // t1 is complex |
| 12680 |
} |
| 12681 |
} |
| 12682 |
else if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 12683 |
{ |
| 12684 |
result = exp.incompatibleTypes(); |
| 12685 |
return; |
| 12686 |
} |
| 12687 |
result = exp; |
| 12688 |
} |
| 12689 |
|
| 12690 |
override void visit(DivExp exp) |
| 12691 |
{ |
| 12692 |
if (commonArithBinOpSemantic(exp)) |
| 12693 |
return; |
| 12694 |
|
| 12695 |
if (exp.type.isFloating()) |
| 12696 |
{ |
| 12697 |
Type t1 = exp.e1.type; |
| 12698 |
Type t2 = exp.e2.type; |
| 12699 |
|
| 12700 |
if (t1.isReal()) |
| 12701 |
{ |
| 12702 |
exp.type = t2; |
| 12703 |
if (t2.isImaginary()) |
| 12704 |
{ |
| 12705 |
// x/iv = i(-x/v) |
| 12706 |
exp.e2.type = t1; |
| 12707 |
Expression e = new NegExp(exp.loc, exp); |
| 12708 |
e = e.expressionSemantic(sc); |
| 12709 |
result = e; |
| 12710 |
return; |
| 12711 |
} |
| 12712 |
} |
| 12713 |
else if (t2.isReal()) |
| 12714 |
{ |
| 12715 |
exp.type = t1; |
| 12716 |
} |
| 12717 |
else if (t1.isImaginary()) |
| 12718 |
{ |
| 12719 |
if (t2.isImaginary()) |
| 12720 |
{ |
| 12721 |
switch (t1.toBasetype().ty) |
| 12722 |
{ |
| 12723 |
case Timaginary32: |
| 12724 |
exp.type = Type.tfloat32; |
| 12725 |
break; |
| 12726 |
|
| 12727 |
case Timaginary64: |
| 12728 |
exp.type = Type.tfloat64; |
| 12729 |
break; |
| 12730 |
|
| 12731 |
case Timaginary80: |
| 12732 |
exp.type = Type.tfloat80; |
| 12733 |
break; |
| 12734 |
|
| 12735 |
default: |
| 12736 |
assert(0); |
| 12737 |
} |
| 12738 |
} |
| 12739 |
else |
| 12740 |
exp.type = t2; // t2 is complex |
| 12741 |
} |
| 12742 |
else if (t2.isImaginary()) |
| 12743 |
{ |
| 12744 |
exp.type = t1; // t1 is complex |
| 12745 |
} |
| 12746 |
} |
| 12747 |
else if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 12748 |
{ |
| 12749 |
result = exp.incompatibleTypes(); |
| 12750 |
return; |
| 12751 |
} |
| 12752 |
result = exp; |
| 12753 |
} |
| 12754 |
|
| 12755 |
override void visit(ModExp exp) |
| 12756 |
{ |
| 12757 |
if (commonArithBinOpSemantic(exp)) |
| 12758 |
return; |
| 12759 |
|
| 12760 |
if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 12761 |
{ |
| 12762 |
result = exp.incompatibleTypes(); |
| 12763 |
return; |
| 12764 |
} |
| 12765 |
|
| 12766 |
if (exp.type.isFloating()) |
| 12767 |
{ |
| 12768 |
exp.type = exp.e1.type; |
| 12769 |
if (exp.e2.type.isComplex()) |
| 12770 |
{ |
| 12771 |
error(exp.loc, "cannot perform modulo complex arithmetic"); |
| 12772 |
return setError(); |
| 12773 |
} |
| 12774 |
} |
| 12775 |
result = exp; |
| 12776 |
} |
| 12777 |
|
| 12778 |
override void visit(PowExp exp) |
| 12779 |
{ |
| 12780 |
if (commonArithBinOpSemantic(exp)) |
| 12781 |
return; |
| 12782 |
|
| 12783 |
if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 12784 |
{ |
| 12785 |
result = exp.incompatibleTypes(); |
| 12786 |
return; |
| 12787 |
} |
| 12788 |
|
| 12789 |
// First, attempt to fold the expression. |
| 12790 |
Expression e = exp.optimize(WANTvalue); |
| 12791 |
if (e.op != EXP.pow) |
| 12792 |
{ |
| 12793 |
e = e.expressionSemantic(sc); |
| 12794 |
result = e; |
| 12795 |
return; |
| 12796 |
} |
| 12797 |
|
| 12798 |
Module mmath = Module.loadStdMath(); |
| 12799 |
if (!mmath) |
| 12800 |
{ |
| 12801 |
error(e.loc, "`%s` requires `std.math` for `^^` operators", e.toErrMsg()); |
| 12802 |
return setError(); |
| 12803 |
} |
| 12804 |
e = new ScopeExp(exp.loc, mmath); |
| 12805 |
|
| 12806 |
if (exp.e2.op == EXP.float64 && exp.e2.toReal() == CTFloat.half) |
| 12807 |
{ |
| 12808 |
// Replace e1 ^^ 0.5 with .std.math.sqrt(e1) |
| 12809 |
e = new CallExp(exp.loc, new DotIdExp(exp.loc, e, Id._sqrt), exp.e1); |
| 12810 |
} |
| 12811 |
else |
| 12812 |
{ |
| 12813 |
// Replace e1 ^^ e2 with .std.math.pow(e1, e2) |
| 12814 |
e = new CallExp(exp.loc, new DotIdExp(exp.loc, e, Id._pow), exp.e1, exp.e2); |
| 12815 |
} |
| 12816 |
e = e.expressionSemantic(sc); |
| 12817 |
result = e; |
| 12818 |
return; |
| 12819 |
} |
| 12820 |
|
| 12821 |
private void visitShift(BinExp exp) |
| 12822 |
{ |
| 12823 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12824 |
{ |
| 12825 |
result = e; |
| 12826 |
return; |
| 12827 |
} |
| 12828 |
|
| 12829 |
if (exp.suggestBinaryOverloads(sc) || exp.checkIntegralBin() || exp.checkSharedAccessBin(sc)) |
| 12830 |
return setError(); |
| 12831 |
|
| 12832 |
if (!target.isVectorOpSupported(exp.e1.type.toBasetype(), exp.op, exp.e2.type.toBasetype())) |
| 12833 |
{ |
| 12834 |
result = exp.incompatibleTypes(); |
| 12835 |
return; |
| 12836 |
} |
| 12837 |
|
| 12838 |
exp.e1 = integralPromotions(exp.e1, sc); |
| 12839 |
if (exp.e2.type.toBasetype().ty != Tvector) |
| 12840 |
{ |
| 12841 |
Type tb1 = exp.e1.type.toBasetype(); |
| 12842 |
exp.e2 = exp.e2.castTo(sc, tb1.ty == Tvector ? tb1 : Type.tshiftcnt); |
| 12843 |
} |
| 12844 |
|
| 12845 |
exp.type = exp.e1.type; |
| 12846 |
result = exp; |
| 12847 |
} |
| 12848 |
|
| 12849 |
override void visit(ShlExp exp) |
| 12850 |
{ |
| 12851 |
visitShift(exp); |
| 12852 |
} |
| 12853 |
override void visit(ShrExp exp) |
| 12854 |
{ |
| 12855 |
visitShift(exp); |
| 12856 |
} |
| 12857 |
override void visit(UshrExp exp) |
| 12858 |
{ |
| 12859 |
visitShift(exp); |
| 12860 |
} |
| 12861 |
|
| 12862 |
private void visitBinaryBitOp(BinExp exp) |
| 12863 |
{ |
| 12864 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 12865 |
{ |
| 12866 |
result = e; |
| 12867 |
return; |
| 12868 |
} |
| 12869 |
|
| 12870 |
if (exp.e1.type.toBasetype().ty == Tbool && exp.e2.type.toBasetype().ty == Tbool) |
| 12871 |
{ |
| 12872 |
exp.type = exp.e1.type; |
| 12873 |
result = exp; |
| 12874 |
return; |
| 12875 |
} |
| 12876 |
|
| 12877 |
if (Expression ex = typeCombine(exp, sc)) |
| 12878 |
{ |
| 12879 |
result = ex; |
| 12880 |
return; |
| 12881 |
} |
| 12882 |
|
| 12883 |
Type tb = exp.type.toBasetype(); |
| 12884 |
if (tb.isStaticOrDynamicArray()) |
| 12885 |
{ |
| 12886 |
if (!isArrayOpValid(exp)) |
| 12887 |
{ |
| 12888 |
result = arrayOpInvalidError(exp); |
| 12889 |
return; |
| 12890 |
} |
| 12891 |
result = exp; |
| 12892 |
return; |
| 12893 |
} |
| 12894 |
if (!target.isVectorOpSupported(tb, exp.op, exp.e2.type.toBasetype())) |
| 12895 |
{ |
| 12896 |
result = exp.incompatibleTypes(); |
| 12897 |
return; |
| 12898 |
} |
| 12899 |
|
| 12900 |
if (exp.suggestBinaryOverloads(sc) || exp.checkIntegralBin() || exp.checkSharedAccessBin(sc)) |
| 12901 |
return setError(); |
| 12902 |
|
| 12903 |
result = exp; |
| 12904 |
} |
| 12905 |
|
| 12906 |
override void visit(AndExp exp) |
| 12907 |
{ |
| 12908 |
visitBinaryBitOp(exp); |
| 12909 |
} |
| 12910 |
override void visit(OrExp exp) |
| 12911 |
{ |
| 12912 |
visitBinaryBitOp(exp); |
| 12913 |
} |
| 12914 |
override void visit(XorExp exp) |
| 12915 |
{ |
| 12916 |
visitBinaryBitOp(exp); |
| 12917 |
} |
| 12918 |
|
| 12919 |
override void visit(LogicalExp exp) |
| 12920 |
{ |
| 12921 |
static if (LOGSEMANTIC) |
| 12922 |
{ |
| 12923 |
printf("LogicalExp::semantic() %s\n", exp.toChars()); |
| 12924 |
} |
| 12925 |
|
| 12926 |
|
| 12927 |
exp.setNoderefOperands(); |
| 12928 |
|
| 12929 |
Expression e1x = exp.e1.expressionSemantic(sc); |
| 12930 |
|
| 12931 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 12932 |
if (e1x.op == EXP.type) |
| 12933 |
e1x = resolveAliasThis(sc, e1x); |
| 12934 |
|
| 12935 |
e1x = resolveProperties(sc, e1x); |
| 12936 |
e1x = e1x.toBoolean(sc); |
| 12937 |
|
| 12938 |
if (sc.condition) |
| 12939 |
{ |
| 12940 |
/* If in static if, don't evaluate e2 if we don't have to. |
| 12941 |
*/ |
| 12942 |
e1x = e1x.optimize(WANTvalue); |
| 12943 |
if (e1x.toBool().hasValue(exp.op == EXP.orOr)) |
| 12944 |
{ |
| 12945 |
if (sc.inCfile) |
| 12946 |
result = new IntegerExp(exp.op == EXP.orOr); |
| 12947 |
else |
| 12948 |
result = IntegerExp.createBool(exp.op == EXP.orOr); |
| 12949 |
return; |
| 12950 |
} |
| 12951 |
} |
| 12952 |
|
| 12953 |
CtorFlow ctorflow = sc.ctorflow.clone(); |
| 12954 |
Expression e2x = exp.e2.expressionSemantic(sc); |
| 12955 |
sc.merge(exp.loc, ctorflow); |
| 12956 |
ctorflow.freeFieldinit(); |
| 12957 |
|
| 12958 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 12959 |
if (e2x.op == EXP.type) |
| 12960 |
e2x = resolveAliasThis(sc, e2x); |
| 12961 |
|
| 12962 |
e2x = resolveProperties(sc, e2x); |
| 12963 |
|
| 12964 |
auto f1 = checkNonAssignmentArrayOp(e1x); |
| 12965 |
auto f2 = checkNonAssignmentArrayOp(e2x); |
| 12966 |
if (f1 || f2) |
| 12967 |
return setError(); |
| 12968 |
|
| 12969 |
// Unless the right operand is 'void', the expression is converted to 'bool'. |
| 12970 |
if (e2x.type.ty != Tvoid) |
| 12971 |
e2x = e2x.toBoolean(sc); |
| 12972 |
|
| 12973 |
if (e2x.op == EXP.type || e2x.op == EXP.scope_) |
| 12974 |
{ |
| 12975 |
error(exp.loc, "`%s` is not an expression", exp.e2.toErrMsg()); |
| 12976 |
return setError(); |
| 12977 |
} |
| 12978 |
if (e1x.op == EXP.error || e1x.type.ty == Tnoreturn) |
| 12979 |
{ |
| 12980 |
result = e1x; |
| 12981 |
return; |
| 12982 |
} |
| 12983 |
if (e2x.op == EXP.error) |
| 12984 |
{ |
| 12985 |
result = e2x; |
| 12986 |
return; |
| 12987 |
} |
| 12988 |
|
| 12989 |
// The result type is 'bool', unless the right operand has type 'void'. |
| 12990 |
if (e2x.type.ty == Tvoid) |
| 12991 |
exp.type = Type.tvoid; |
| 12992 |
else |
| 12993 |
exp.type = (sc && sc.inCfile) ? Type.tint32 : Type.tbool; |
| 12994 |
|
| 12995 |
exp.e1 = e1x; |
| 12996 |
exp.e2 = e2x; |
| 12997 |
result = exp; |
| 12998 |
} |
| 12999 |
|
| 13000 |
|
| 13001 |
override void visit(CmpExp exp) |
| 13002 |
{ |
| 13003 |
static if (LOGSEMANTIC) |
| 13004 |
{ |
| 13005 |
printf("CmpExp::semantic('%s')\n", exp.toChars()); |
| 13006 |
} |
| 13007 |
|
| 13008 |
exp.setNoderefOperands(); |
| 13009 |
|
| 13010 |
if (Expression ex = binSemanticProp(exp, sc)) |
| 13011 |
{ |
| 13012 |
result = ex; |
| 13013 |
return; |
| 13014 |
} |
| 13015 |
Type t1 = exp.e1.type.toBasetype(); |
| 13016 |
Type t2 = exp.e2.type.toBasetype(); |
| 13017 |
if (t1.ty == Tclass && exp.e2.op == EXP.null_ || t2.ty == Tclass && exp.e1.op == EXP.null_) |
| 13018 |
{ |
| 13019 |
error(exp.loc, "do not use `null` when comparing class types"); |
| 13020 |
return setError(); |
| 13021 |
} |
| 13022 |
|
| 13023 |
if (auto e = exp.opOverloadCmp(sc, aliasThisStop)) |
| 13024 |
{ |
| 13025 |
result = e; |
| 13026 |
return; |
| 13027 |
} |
| 13028 |
|
| 13029 |
if (Expression ex = typeCombine(exp, sc)) |
| 13030 |
{ |
| 13031 |
result = ex; |
| 13032 |
return; |
| 13033 |
} |
| 13034 |
|
| 13035 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 13036 |
auto f2 = checkNonAssignmentArrayOp(exp.e2); |
| 13037 |
if (f1 || f2) |
| 13038 |
return setError(); |
| 13039 |
|
| 13040 |
exp.type = (sc && sc.inCfile) ? Type.tint32 : Type.tbool; |
| 13041 |
|
| 13042 |
// Special handling for array comparisons |
| 13043 |
Expression arrayLowering = null; |
| 13044 |
t1 = exp.e1.type.toBasetype(); |
| 13045 |
t2 = exp.e2.type.toBasetype(); |
| 13046 |
if ((t1.isStaticOrDynamicArray() || t1.ty == Tpointer) && (t2.isStaticOrDynamicArray() || t2.ty == Tpointer)) |
| 13047 |
{ |
| 13048 |
Type t1next = t1.nextOf(); |
| 13049 |
Type t2next = t2.nextOf(); |
| 13050 |
if (t1next.implicitConvTo(t2next) < MATCH.constant && t2next.implicitConvTo(t1next) < MATCH.constant && (t1next.ty != Tvoid && t2next.ty != Tvoid)) |
| 13051 |
{ |
| 13052 |
error(exp.loc, "array comparison type mismatch, `%s` vs `%s`", t1next.toChars(), t2next.toChars()); |
| 13053 |
return setError(); |
| 13054 |
} |
| 13055 |
|
| 13056 |
if (t1.isStaticOrDynamicArray() && t2.isStaticOrDynamicArray()) |
| 13057 |
{ |
| 13058 |
if (!verifyHookExist(exp.loc, *sc, Id.__cmp, "comparing arrays")) |
| 13059 |
return setError(); |
| 13060 |
|
| 13061 |
// Lower to object.__cmp(e1, e2) |
| 13062 |
Expression al = new IdentifierExp(exp.loc, Id.empty); |
| 13063 |
al = new DotIdExp(exp.loc, al, Id.object); |
| 13064 |
al = new DotIdExp(exp.loc, al, Id.__cmp); |
| 13065 |
al = al.expressionSemantic(sc); |
| 13066 |
|
| 13067 |
auto arguments = new Expressions(2); |
| 13068 |
(*arguments)[0] = exp.e1; |
| 13069 |
(*arguments)[1] = exp.e2; |
| 13070 |
|
| 13071 |
al = new CallExp(exp.loc, al, arguments); |
| 13072 |
al = new CmpExp(exp.op, exp.loc, al, IntegerExp.literal!0); |
| 13073 |
|
| 13074 |
arrayLowering = al; |
| 13075 |
} |
| 13076 |
} |
| 13077 |
else if (t1.isTypeClass() && t2.isTypeClass()) |
| 13078 |
{ |
| 13079 |
error(exp.loc, "need member function `opCmp()` for %s `%s` to compare", t1.toDsymbol(sc).kind(), t1.toChars()); |
| 13080 |
return setError(); |
| 13081 |
} |
| 13082 |
else if (t1.isComplex() || t2.isComplex()) |
| 13083 |
{ |
| 13084 |
error(exp.loc, "compare not defined for complex operands"); |
| 13085 |
return setError(); |
| 13086 |
} |
| 13087 |
else if (t1.isTypeFunction() || t2.isTypeFunction()) |
| 13088 |
{ |
| 13089 |
error(exp.loc, "comparison is not defined for function types"); |
| 13090 |
return setError(); |
| 13091 |
} |
| 13092 |
else if (t1.ty == Taarray || t2.ty == Taarray) |
| 13093 |
{ |
| 13094 |
error(exp.loc, "`%s` is not defined for associative arrays", EXPtoString(exp.op).ptr); |
| 13095 |
return setError(); |
| 13096 |
} |
| 13097 |
else if (!target.isVectorOpSupported(t1, exp.op, t2)) |
| 13098 |
{ |
| 13099 |
result = exp.incompatibleTypes(); |
| 13100 |
return; |
| 13101 |
} |
| 13102 |
else |
| 13103 |
{ |
| 13104 |
bool r1 = exp.e1.checkValue() || exp.e1.checkSharedAccess(sc); |
| 13105 |
bool r2 = exp.e2.checkValue() || exp.e2.checkSharedAccess(sc); |
| 13106 |
if (r1 || r2) |
| 13107 |
return setError(); |
| 13108 |
} |
| 13109 |
|
| 13110 |
//printf("CmpExp: %s, type = %s\n", e.toChars(), e.type.toChars()); |
| 13111 |
if (arrayLowering) |
| 13112 |
{ |
| 13113 |
arrayLowering = arrayLowering.expressionSemantic(sc); |
| 13114 |
result = arrayLowering; |
| 13115 |
return; |
| 13116 |
} |
| 13117 |
|
| 13118 |
if (auto tv = t1.isTypeVector()) |
| 13119 |
exp.type = tv.toBooleanVector(); |
| 13120 |
|
| 13121 |
result = exp; |
| 13122 |
return; |
| 13123 |
} |
| 13124 |
|
| 13125 |
override void visit(InExp exp) |
| 13126 |
{ |
| 13127 |
if (Expression e = exp.opOverloadBinary(sc, aliasThisStop)) |
| 13128 |
{ |
| 13129 |
result = e; |
| 13130 |
return; |
| 13131 |
} |
| 13132 |
|
| 13133 |
Type t2b = exp.e2.type.toBasetype(); |
| 13134 |
switch (t2b.ty) |
| 13135 |
{ |
| 13136 |
case Taarray: |
| 13137 |
{ |
| 13138 |
TypeAArray ta = cast(TypeAArray)t2b; |
| 13139 |
|
| 13140 |
// Special handling for array keys |
| 13141 |
if (!arrayTypeCompatibleWithoutCasting(exp.e1.type, ta.index)) |
| 13142 |
{ |
| 13143 |
// Convert key to type of key |
| 13144 |
exp.e1 = exp.e1.implicitCastTo(sc, ta.index); |
| 13145 |
} |
| 13146 |
|
| 13147 |
// even though the glue layer only needs the type info of the index, |
| 13148 |
// this might be the first time an AA literal is accessed, so check |
| 13149 |
// the full type info |
| 13150 |
semanticTypeInfo(sc, ta); |
| 13151 |
|
| 13152 |
// Return type is pointer to value |
| 13153 |
exp.type = ta.nextOf().pointerTo(); |
| 13154 |
break; |
| 13155 |
} |
| 13156 |
|
| 13157 |
case Terror: |
| 13158 |
return setError(); |
| 13159 |
|
| 13160 |
case Tarray, Tsarray: |
| 13161 |
result = exp.incompatibleTypes(sc); |
| 13162 |
errorSupplemental(exp.loc, "`in` is only allowed on associative arrays"); |
| 13163 |
const(char)* slice = (t2b.ty == Tsarray) ? "[]" : ""; |
| 13164 |
errorSupplemental(exp.loc, "perhaps use `std.algorithm.find(%s, %s%s)` instead", |
| 13165 |
exp.e1.toErrMsg(), exp.e2.toErrMsg(), slice); |
| 13166 |
return; |
| 13167 |
|
| 13168 |
default: |
| 13169 |
result = exp.incompatibleTypes(sc); |
| 13170 |
return; |
| 13171 |
} |
| 13172 |
result = exp; |
| 13173 |
} |
| 13174 |
|
| 13175 |
override void visit(RemoveExp e) |
| 13176 |
{ |
| 13177 |
if (Expression ex = binSemantic(e, sc)) |
| 13178 |
{ |
| 13179 |
result = ex; |
| 13180 |
return; |
| 13181 |
} |
| 13182 |
result = e; |
| 13183 |
} |
| 13184 |
|
| 13185 |
override void visit(EqualExp exp) |
| 13186 |
{ |
| 13187 |
//printf("EqualExp::semantic('%s')\n", exp.toChars()); |
| 13188 |
|
| 13189 |
exp.setNoderefOperands(); |
| 13190 |
|
| 13191 |
if (auto e = binSemanticProp(exp, sc)) |
| 13192 |
{ |
| 13193 |
result = e; |
| 13194 |
return; |
| 13195 |
} |
| 13196 |
if (exp.e1.op == EXP.type || exp.e2.op == EXP.type) |
| 13197 |
{ |
| 13198 |
/* https://issues.dlang.org/show_bug.cgi?id=12520 |
| 13199 |
* empty tuples are represented as types so special cases are added |
| 13200 |
* so that they can be compared for equality with tuples of values. |
| 13201 |
*/ |
| 13202 |
static auto extractTypeTupAndExpTup(Expression e) |
| 13203 |
{ |
| 13204 |
static struct Result { bool ttEmpty; bool te; } |
| 13205 |
auto tt = e.op == EXP.type ? e.isTypeExp().type.isTypeTuple() : null; |
| 13206 |
return Result(tt && (!tt.arguments || !tt.arguments.length), e.isTupleExp() !is null); |
| 13207 |
} |
| 13208 |
auto tups1 = extractTypeTupAndExpTup(exp.e1); |
| 13209 |
auto tups2 = extractTypeTupAndExpTup(exp.e2); |
| 13210 |
// AliasSeq!() == AliasSeq!(<at least a value>) |
| 13211 |
if (tups1.ttEmpty && tups2.te) |
| 13212 |
{ |
| 13213 |
result = IntegerExp.createBool(exp.op != EXP.equal); |
| 13214 |
return; |
| 13215 |
} |
| 13216 |
// AliasSeq!(<at least a value>) == AliasSeq!() |
| 13217 |
else if (tups1.te && tups2.ttEmpty) |
| 13218 |
{ |
| 13219 |
result = IntegerExp.createBool(exp.op != EXP.equal); |
| 13220 |
return; |
| 13221 |
} |
| 13222 |
// AliasSeq!() == AliasSeq!() |
| 13223 |
else if (tups1.ttEmpty && tups2.ttEmpty) |
| 13224 |
{ |
| 13225 |
result = IntegerExp.createBool(exp.op == EXP.equal); |
| 13226 |
return; |
| 13227 |
} |
| 13228 |
// otherwise, two types are really not comparable |
| 13229 |
result = exp.incompatibleTypes(); |
| 13230 |
return; |
| 13231 |
} |
| 13232 |
|
| 13233 |
{ |
| 13234 |
auto t1 = exp.e1.type; |
| 13235 |
auto t2 = exp.e2.type; |
| 13236 |
if (t1.ty == Tenum && t2.ty == Tenum && !t1.equivalent(t2)) |
| 13237 |
error(exp.loc, "comparison between different enumeration types `%s` and `%s`; If this behavior is intended consider using `std.conv.asOriginalType`", |
| 13238 |
t1.toChars(), t2.toChars()); |
| 13239 |
} |
| 13240 |
|
| 13241 |
/* Before checking for operator overloading, check to see if we're |
| 13242 |
* comparing the addresses of two statics. If so, we can just see |
| 13243 |
* if they are the same symbol. |
| 13244 |
*/ |
| 13245 |
if (exp.e1.op == EXP.address && exp.e2.op == EXP.address) |
| 13246 |
{ |
| 13247 |
AddrExp ae1 = cast(AddrExp)exp.e1; |
| 13248 |
AddrExp ae2 = cast(AddrExp)exp.e2; |
| 13249 |
if (ae1.e1.op == EXP.variable && ae2.e1.op == EXP.variable) |
| 13250 |
{ |
| 13251 |
VarExp ve1 = cast(VarExp)ae1.e1; |
| 13252 |
VarExp ve2 = cast(VarExp)ae2.e1; |
| 13253 |
if (ve1.var == ve2.var) |
| 13254 |
{ |
| 13255 |
// They are the same, result is 'true' for ==, 'false' for != |
| 13256 |
result = IntegerExp.createBool(exp.op == EXP.equal); |
| 13257 |
return; |
| 13258 |
} |
| 13259 |
} |
| 13260 |
} |
| 13261 |
|
| 13262 |
Type t1 = exp.e1.type.toBasetype(); |
| 13263 |
Type t2 = exp.e2.type.toBasetype(); |
| 13264 |
|
| 13265 |
// Indicates whether the comparison of the 2 specified array types |
| 13266 |
// requires an object.__equals() lowering. |
| 13267 |
static bool needsDirectEq(Type t1, Type t2, Scope* sc) |
| 13268 |
{ |
| 13269 |
Type t1n = t1.nextOf().toBasetype(); |
| 13270 |
Type t2n = t2.nextOf().toBasetype(); |
| 13271 |
if ((t1n.ty.isSomeChar && t2n.ty.isSomeChar) || |
| 13272 |
(t1n.ty == Tvoid || t2n.ty == Tvoid)) |
| 13273 |
{ |
| 13274 |
return false; |
| 13275 |
} |
| 13276 |
if (t1n.constOf() != t2n.constOf()) |
| 13277 |
return true; |
| 13278 |
|
| 13279 |
Type t = t1n; |
| 13280 |
while (t.toBasetype().nextOf()) |
| 13281 |
t = t.nextOf().toBasetype(); |
| 13282 |
if (auto ts = t.isTypeStruct()) |
| 13283 |
{ |
| 13284 |
// semanticTypeInfo() makes sure hasIdentityEquals has been computed |
| 13285 |
if (global.params.useTypeInfo && Type.dtypeinfo) |
| 13286 |
semanticTypeInfo(sc, ts); |
| 13287 |
|
| 13288 |
return ts.sym.hasIdentityEquals; // has custom opEquals |
| 13289 |
} |
| 13290 |
|
| 13291 |
return false; |
| 13292 |
} |
| 13293 |
|
| 13294 |
if (auto e = exp.opOverloadEqual(sc, aliasThisStop)) |
| 13295 |
{ |
| 13296 |
result = e; |
| 13297 |
return; |
| 13298 |
} |
| 13299 |
|
| 13300 |
const isArrayComparison = t1.isStaticOrDynamicArray() && t2.isStaticOrDynamicArray(); |
| 13301 |
const needsArrayLowering = isArrayComparison && needsDirectEq(t1, t2, sc); |
| 13302 |
|
| 13303 |
if (!needsArrayLowering) |
| 13304 |
{ |
| 13305 |
// https://issues.dlang.org/show_bug.cgi?id=23783 |
| 13306 |
if (exp.e1.checkSharedAccess(sc) || exp.e2.checkSharedAccess(sc)) |
| 13307 |
return setError(); |
| 13308 |
if (auto e = typeCombine(exp, sc)) |
| 13309 |
{ |
| 13310 |
result = e; |
| 13311 |
return; |
| 13312 |
} |
| 13313 |
} |
| 13314 |
|
| 13315 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 13316 |
auto f2 = checkNonAssignmentArrayOp(exp.e2); |
| 13317 |
if (f1 || f2) |
| 13318 |
return setError(); |
| 13319 |
|
| 13320 |
exp.type = (sc && sc.inCfile) ? Type.tint32 : Type.tbool; |
| 13321 |
|
| 13322 |
if (!isArrayComparison) |
| 13323 |
{ |
| 13324 |
if (exp.e1.type != exp.e2.type && exp.e1.type.isFloating() && exp.e2.type.isFloating()) |
| 13325 |
{ |
| 13326 |
// Cast both to complex |
| 13327 |
exp.e1 = exp.e1.castTo(sc, Type.tcomplex80); |
| 13328 |
exp.e2 = exp.e2.castTo(sc, Type.tcomplex80); |
| 13329 |
} |
| 13330 |
} |
| 13331 |
|
| 13332 |
// lower some array comparisons to object.__equals(e1, e2) |
| 13333 |
if (needsArrayLowering || (t1.ty == Tarray && t2.ty == Tarray)) |
| 13334 |
{ |
| 13335 |
//printf("Lowering to __equals %s %s\n", exp.e1.toChars(), exp.e2.toChars()); |
| 13336 |
|
| 13337 |
// https://issues.dlang.org/show_bug.cgi?id=22390 |
| 13338 |
// Equality comparison between array of noreturns simply lowers to length equality comparison |
| 13339 |
if (t1.nextOf.isTypeNoreturn() && t2.nextOf.isTypeNoreturn()) |
| 13340 |
{ |
| 13341 |
Expression exp_l1 = new DotIdExp(exp.e1.loc, exp.e1, Id.length); |
| 13342 |
Expression exp_l2 = new DotIdExp(exp.e2.loc, exp.e2, Id.length); |
| 13343 |
auto e = new EqualExp(EXP.equal, exp.loc, exp_l1, exp_l2); |
| 13344 |
result = e.expressionSemantic(sc); |
| 13345 |
return; |
| 13346 |
} |
| 13347 |
|
| 13348 |
if (!verifyHookExist(exp.loc, *sc, Id.__equals, "equal checks on arrays")) |
| 13349 |
return setError(); |
| 13350 |
|
| 13351 |
Expression __equals = new IdentifierExp(exp.loc, Id.empty); |
| 13352 |
Identifier id = Identifier.idPool("__equals"); |
| 13353 |
__equals = new DotIdExp(exp.loc, __equals, Id.object); |
| 13354 |
__equals = new DotIdExp(exp.loc, __equals, id); |
| 13355 |
|
| 13356 |
/* https://issues.dlang.org/show_bug.cgi?id=23674 |
| 13357 |
* |
| 13358 |
* Optimize before creating the call expression to the |
| 13359 |
* druntime hook as the optimizer may output errors |
| 13360 |
* that will get swallowed otherwise. |
| 13361 |
*/ |
| 13362 |
exp.e1 = exp.e1.optimize(WANTvalue); |
| 13363 |
exp.e2 = exp.e2.optimize(WANTvalue); |
| 13364 |
|
| 13365 |
auto arguments = new Expressions(2); |
| 13366 |
(*arguments)[0] = exp.e1; |
| 13367 |
(*arguments)[1] = exp.e2; |
| 13368 |
|
| 13369 |
__equals = new CallExp(exp.loc, __equals, arguments); |
| 13370 |
if (exp.op == EXP.notEqual) |
| 13371 |
{ |
| 13372 |
__equals = new NotExp(exp.loc, __equals); |
| 13373 |
} |
| 13374 |
__equals = __equals.trySemantic(sc); // for better error message |
| 13375 |
if (!__equals) |
| 13376 |
{ |
| 13377 |
error(exp.loc, "incompatible types for array comparison: `%s` and `%s`", |
| 13378 |
exp.e1.type.toChars(), exp.e2.type.toChars()); |
| 13379 |
__equals = ErrorExp.get(); |
| 13380 |
} |
| 13381 |
|
| 13382 |
result = __equals; |
| 13383 |
return; |
| 13384 |
} |
| 13385 |
|
| 13386 |
if (exp.e1.type.toBasetype().ty == Taarray) |
| 13387 |
{ |
| 13388 |
semanticTypeInfo(sc, exp.e1.type.toBasetype()); |
| 13389 |
} |
| 13390 |
|
| 13391 |
if (!target.isVectorOpSupported(t1, exp.op, t2)) |
| 13392 |
{ |
| 13393 |
result = exp.incompatibleTypes(); |
| 13394 |
return; |
| 13395 |
} |
| 13396 |
|
| 13397 |
if (t1.isTypeFunction() || t2.isTypeFunction()) |
| 13398 |
{ |
| 13399 |
error(exp.loc, "operator `==` is not defined for function types"); |
| 13400 |
return setError(); |
| 13401 |
} |
| 13402 |
|
| 13403 |
if (auto tv = t1.isTypeVector()) |
| 13404 |
exp.type = tv.toBooleanVector(); |
| 13405 |
|
| 13406 |
result = exp; |
| 13407 |
} |
| 13408 |
|
| 13409 |
override void visit(IdentityExp exp) |
| 13410 |
{ |
| 13411 |
|
| 13412 |
exp.setNoderefOperands(); |
| 13413 |
|
| 13414 |
if (auto e = binSemanticProp(exp, sc)) |
| 13415 |
{ |
| 13416 |
result = e; |
| 13417 |
return; |
| 13418 |
} |
| 13419 |
|
| 13420 |
if (auto e = typeCombine(exp, sc)) |
| 13421 |
{ |
| 13422 |
result = e; |
| 13423 |
return; |
| 13424 |
} |
| 13425 |
|
| 13426 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 13427 |
auto f2 = checkNonAssignmentArrayOp(exp.e2); |
| 13428 |
if (f1 || f2) |
| 13429 |
return setError(); |
| 13430 |
|
| 13431 |
if (exp.e1.op == EXP.type || exp.e2.op == EXP.type) |
| 13432 |
{ |
| 13433 |
result = exp.incompatibleTypes(); |
| 13434 |
return; |
| 13435 |
} |
| 13436 |
|
| 13437 |
exp.type = Type.tbool; |
| 13438 |
|
| 13439 |
if (exp.e1.type != exp.e2.type && exp.e1.type.isFloating() && exp.e2.type.isFloating()) |
| 13440 |
{ |
| 13441 |
// Cast both to complex |
| 13442 |
exp.e1 = exp.e1.castTo(sc, Type.tcomplex80); |
| 13443 |
exp.e2 = exp.e2.castTo(sc, Type.tcomplex80); |
| 13444 |
} |
| 13445 |
|
| 13446 |
auto tb1 = exp.e1.type.toBasetype(); |
| 13447 |
auto tb2 = exp.e2.type.toBasetype(); |
| 13448 |
if (!target.isVectorOpSupported(tb1, exp.op, tb2)) |
| 13449 |
{ |
| 13450 |
result = exp.incompatibleTypes(); |
| 13451 |
return; |
| 13452 |
} |
| 13453 |
|
| 13454 |
if (exp.e1.op == EXP.call) |
| 13455 |
exp.e1 = (cast(CallExp)exp.e1).addDtorHook(sc); |
| 13456 |
if (exp.e2.op == EXP.call) |
| 13457 |
exp.e2 = (cast(CallExp)exp.e2).addDtorHook(sc); |
| 13458 |
|
| 13459 |
if (exp.e1.type.isTypeFunction() || exp.e2.type.isTypeFunction()) |
| 13460 |
{ |
| 13461 |
error(exp.loc, "operator `is` is not defined for function types"); |
| 13462 |
return setError(); |
| 13463 |
} |
| 13464 |
|
| 13465 |
if (exp.e1.type.toBasetype().ty == Tsarray || |
| 13466 |
exp.e2.type.toBasetype().ty == Tsarray) |
| 13467 |
deprecation(exp.loc, "identity comparison of static arrays " |
| 13468 |
~ "implicitly coerces them to slices, " |
| 13469 |
~ "which are compared by reference"); |
| 13470 |
|
| 13471 |
result = exp; |
| 13472 |
} |
| 13473 |
|
| 13474 |
override void visit(CondExp exp) |
| 13475 |
{ |
| 13476 |
static if (LOGSEMANTIC) |
| 13477 |
{ |
| 13478 |
printf("CondExp::semantic('%s')\n", exp.toChars()); |
| 13479 |
} |
| 13480 |
|
| 13481 |
if (auto die = exp.econd.isDotIdExp()) |
| 13482 |
die.noderef = true; |
| 13483 |
|
| 13484 |
Expression ec = exp.econd.expressionSemantic(sc); |
| 13485 |
ec = resolveProperties(sc, ec); |
| 13486 |
ec = ec.toBoolean(sc); |
| 13487 |
|
| 13488 |
CtorFlow ctorflow_root = sc.ctorflow.clone(); |
| 13489 |
Expression e1x = exp.e1.expressionSemantic(sc).arrayFuncConv(sc); |
| 13490 |
e1x = resolveProperties(sc, e1x); |
| 13491 |
|
| 13492 |
CtorFlow ctorflow1 = sc.ctorflow; |
| 13493 |
sc.ctorflow = ctorflow_root; |
| 13494 |
Expression e2x = exp.e2.expressionSemantic(sc).arrayFuncConv(sc); |
| 13495 |
e2x = resolveProperties(sc, e2x); |
| 13496 |
|
| 13497 |
sc.merge(exp.loc, ctorflow1); |
| 13498 |
ctorflow1.freeFieldinit(); |
| 13499 |
|
| 13500 |
if (ec.op == EXP.error) |
| 13501 |
{ |
| 13502 |
result = ec; |
| 13503 |
return; |
| 13504 |
} |
| 13505 |
if (ec.type == Type.terror) |
| 13506 |
return setError(); |
| 13507 |
exp.econd = ec; |
| 13508 |
|
| 13509 |
if (e1x.op == EXP.error) |
| 13510 |
{ |
| 13511 |
result = e1x; |
| 13512 |
return; |
| 13513 |
} |
| 13514 |
if (e1x.type == Type.terror) |
| 13515 |
return setError(); |
| 13516 |
exp.e1 = e1x; |
| 13517 |
|
| 13518 |
if (e2x.op == EXP.error) |
| 13519 |
{ |
| 13520 |
result = e2x; |
| 13521 |
return; |
| 13522 |
} |
| 13523 |
if (e2x.type == Type.terror) |
| 13524 |
return setError(); |
| 13525 |
exp.e2 = e2x; |
| 13526 |
|
| 13527 |
auto f0 = checkNonAssignmentArrayOp(exp.econd); |
| 13528 |
auto f1 = checkNonAssignmentArrayOp(exp.e1); |
| 13529 |
auto f2 = checkNonAssignmentArrayOp(exp.e2); |
| 13530 |
if (f0 || f1 || f2) |
| 13531 |
return setError(); |
| 13532 |
|
| 13533 |
Type t1 = exp.e1.type; |
| 13534 |
Type t2 = exp.e2.type; |
| 13535 |
|
| 13536 |
// https://issues.dlang.org/show_bug.cgi?id=23767 |
| 13537 |
// `cast(void*) 0` should be treated as `null` so the ternary expression |
| 13538 |
// gets the pointer type of the other branch |
| 13539 |
if (sc.inCfile) |
| 13540 |
{ |
| 13541 |
static void rewriteCNull(ref Expression e, ref Type t) |
| 13542 |
{ |
| 13543 |
if (!t.isTypePointer()) |
| 13544 |
return; |
| 13545 |
if (auto ie = e.optimize(WANTvalue).isIntegerExp()) |
| 13546 |
{ |
| 13547 |
if (ie.getInteger() == 0) |
| 13548 |
{ |
| 13549 |
e = new NullExp(e.loc, Type.tnull); |
| 13550 |
t = Type.tnull; |
| 13551 |
} |
| 13552 |
} |
| 13553 |
} |
| 13554 |
rewriteCNull(exp.e1, t1); |
| 13555 |
rewriteCNull(exp.e2, t2); |
| 13556 |
} |
| 13557 |
|
| 13558 |
if (t1.ty == Tnoreturn) |
| 13559 |
{ |
| 13560 |
exp.type = t2; |
| 13561 |
exp.e1 = specialNoreturnCast(exp.e1, exp.type); |
| 13562 |
} |
| 13563 |
else if (t2.ty == Tnoreturn) |
| 13564 |
{ |
| 13565 |
exp.type = t1; |
| 13566 |
exp.e2 = specialNoreturnCast(exp.e2, exp.type); |
| 13567 |
} |
| 13568 |
// If either operand is void the result is void, we have to cast both |
| 13569 |
// the expression to void so that we explicitly discard the expression |
| 13570 |
// value if any |
| 13571 |
// https://issues.dlang.org/show_bug.cgi?id=16598 |
| 13572 |
else if (t1.ty == Tvoid || t2.ty == Tvoid) |
| 13573 |
{ |
| 13574 |
exp.type = Type.tvoid; |
| 13575 |
exp.e1 = exp.e1.castTo(sc, exp.type); |
| 13576 |
exp.e2 = exp.e2.castTo(sc, exp.type); |
| 13577 |
} |
| 13578 |
else if (t1 == t2) |
| 13579 |
exp.type = t1; |
| 13580 |
else |
| 13581 |
{ |
| 13582 |
if (Expression ex = typeCombine(exp, sc)) |
| 13583 |
{ |
| 13584 |
result = ex; |
| 13585 |
return; |
| 13586 |
} |
| 13587 |
|
| 13588 |
switch (exp.e1.type.toBasetype().ty) |
| 13589 |
{ |
| 13590 |
case Tcomplex32: |
| 13591 |
case Tcomplex64: |
| 13592 |
case Tcomplex80: |
| 13593 |
exp.e2 = exp.e2.castTo(sc, exp.e1.type); |
| 13594 |
break; |
| 13595 |
default: |
| 13596 |
break; |
| 13597 |
} |
| 13598 |
switch (exp.e2.type.toBasetype().ty) |
| 13599 |
{ |
| 13600 |
case Tcomplex32: |
| 13601 |
case Tcomplex64: |
| 13602 |
case Tcomplex80: |
| 13603 |
exp.e1 = exp.e1.castTo(sc, exp.e2.type); |
| 13604 |
break; |
| 13605 |
default: |
| 13606 |
break; |
| 13607 |
} |
| 13608 |
if (exp.type.toBasetype().ty == Tarray) |
| 13609 |
{ |
| 13610 |
exp.e1 = exp.e1.castTo(sc, exp.type); |
| 13611 |
exp.e2 = exp.e2.castTo(sc, exp.type); |
| 13612 |
} |
| 13613 |
} |
| 13614 |
exp.type = exp.type.merge2(); |
| 13615 |
version (none) |
| 13616 |
{ |
| 13617 |
printf("res: %s\n", exp.type.toChars()); |
| 13618 |
printf("e1 : %s\n", exp.e1.type.toChars()); |
| 13619 |
printf("e2 : %s\n", exp.e2.type.toChars()); |
| 13620 |
} |
| 13621 |
|
| 13622 |
/* https://issues.dlang.org/show_bug.cgi?id=14696 |
| 13623 |
* If either e1 or e2 contain temporaries which need dtor, |
| 13624 |
* make them conditional. |
| 13625 |
* Rewrite: |
| 13626 |
* cond ? (__tmp1 = ..., __tmp1) : (__tmp2 = ..., __tmp2) |
| 13627 |
* to: |
| 13628 |
* (auto __cond = cond) ? (... __tmp1) : (... __tmp2) |
| 13629 |
* and replace edtors of __tmp1 and __tmp2 with: |
| 13630 |
* __tmp1.edtor --> __cond && __tmp1.dtor() |
| 13631 |
* __tmp2.edtor --> __cond || __tmp2.dtor() |
| 13632 |
*/ |
| 13633 |
exp.hookDtors(sc); |
| 13634 |
|
| 13635 |
result = exp; |
| 13636 |
} |
| 13637 |
|
| 13638 |
override void visit(GenericExp exp) |
| 13639 |
{ |
| 13640 |
static if (LOGSEMANTIC) |
| 13641 |
{ |
| 13642 |
printf("GenericExp::semantic('%s')\n", exp.toChars()); |
| 13643 |
} |
| 13644 |
// C11 6.5.1.1 Generic Selection |
| 13645 |
|
| 13646 |
auto ec = exp.cntlExp.expressionSemantic(sc).arrayFuncConv(sc); |
| 13647 |
bool errors = ec.isErrorExp() !is null; |
| 13648 |
auto tc = ec.type; |
| 13649 |
|
| 13650 |
auto types = (*exp.types)[]; |
| 13651 |
foreach (i, ref t; types) |
| 13652 |
{ |
| 13653 |
if (!t) |
| 13654 |
continue; // `default:` case |
| 13655 |
t = t.typeSemantic(ec.loc, sc); |
| 13656 |
if (t.isTypeError()) |
| 13657 |
{ |
| 13658 |
errors = true; |
| 13659 |
continue; |
| 13660 |
} |
| 13661 |
|
| 13662 |
/* C11 6.5.1-2 duplicate check |
| 13663 |
*/ |
| 13664 |
/* C11 distinguishes int, long, and long long. But D doesn't, so depending on the |
| 13665 |
* C target, a long may have the same type as `int` in the D type system. |
| 13666 |
* So, skip checks when this may be the case. Later pick the first match |
| 13667 |
*/ |
| 13668 |
if ( |
| 13669 |
(t.ty == Tint32 || t.ty == Tuns32) && target.c.longsize == 4 || |
| 13670 |
(t.ty == Tint64 || t.ty == Tuns64) && target.c.longsize == 8 || |
| 13671 |
(t.ty == Tfloat64 || t.ty == Timaginary64 || t.ty == Tcomplex64) && target.c.long_doublesize == 8 |
| 13672 |
) |
| 13673 |
continue; |
| 13674 |
|
| 13675 |
foreach (t2; types[0 .. i]) |
| 13676 |
{ |
| 13677 |
if (t2 && t2.equals(t)) |
| 13678 |
{ |
| 13679 |
error(ec.loc, "generic association type `%s` can only appear once", t.toChars()); |
| 13680 |
errors = true; |
| 13681 |
break; |
| 13682 |
} |
| 13683 |
} |
| 13684 |
} |
| 13685 |
|
| 13686 |
auto exps = (*exp.exps)[]; |
| 13687 |
foreach (ref e; exps) |
| 13688 |
{ |
| 13689 |
e = e.expressionSemantic(sc); |
| 13690 |
if (e.isErrorExp()) |
| 13691 |
errors = true; |
| 13692 |
} |
| 13693 |
|
| 13694 |
if (errors) |
| 13695 |
return setError(); |
| 13696 |
|
| 13697 |
enum size_t None = ~0; |
| 13698 |
size_t imatch = None; |
| 13699 |
size_t idefault = None; |
| 13700 |
foreach (const i, t; types) |
| 13701 |
{ |
| 13702 |
if (t) |
| 13703 |
{ |
| 13704 |
/* if tc is compatible with t, it's a match |
| 13705 |
* C11 6.2.7 defines a compatible type as being the same type, including qualifiers |
| 13706 |
*/ |
| 13707 |
if (tc.equals(t)) |
| 13708 |
{ |
| 13709 |
assert(imatch == None); |
| 13710 |
imatch = i; |
| 13711 |
break; // pick first match |
| 13712 |
} |
| 13713 |
} |
| 13714 |
else |
| 13715 |
idefault = i; // multiple defaults are not allowed, and are caught by cparse |
| 13716 |
} |
| 13717 |
|
| 13718 |
if (imatch == None) |
| 13719 |
imatch = idefault; |
| 13720 |
if (imatch == None) |
| 13721 |
{ |
| 13722 |
error(exp.loc, "no compatible generic association type for controlling expression type `%s`", tc.toChars()); |
| 13723 |
return setError(); |
| 13724 |
} |
| 13725 |
|
| 13726 |
result = exps[imatch]; |
| 13727 |
} |
| 13728 |
|
| 13729 |
override void visit(FileInitExp e) |
| 13730 |
{ |
| 13731 |
//printf("FileInitExp::semantic()\n"); |
| 13732 |
e.type = Type.tstring; |
| 13733 |
result = e.resolveLoc(e.loc, sc); |
| 13734 |
} |
| 13735 |
|
| 13736 |
override void visit(LineInitExp e) |
| 13737 |
{ |
| 13738 |
e.type = Type.tint32; |
| 13739 |
result = e.resolveLoc(e.loc, sc); |
| 13740 |
} |
| 13741 |
|
| 13742 |
override void visit(ModuleInitExp e) |
| 13743 |
{ |
| 13744 |
//printf("ModuleInitExp::semantic()\n"); |
| 13745 |
e.type = Type.tstring; |
| 13746 |
result = e.resolveLoc(e.loc, sc); |
| 13747 |
} |
| 13748 |
|
| 13749 |
override void visit(FuncInitExp e) |
| 13750 |
{ |
| 13751 |
//printf("FuncInitExp::semantic()\n"); |
| 13752 |
e.type = Type.tstring; |
| 13753 |
result = e.resolveLoc(e.loc, sc); |
| 13754 |
} |
| 13755 |
|
| 13756 |
override void visit(PrettyFuncInitExp e) |
| 13757 |
{ |
| 13758 |
//printf("PrettyFuncInitExp::semantic()\n"); |
| 13759 |
e.type = Type.tstring; |
| 13760 |
result = e.resolveLoc(e.loc, sc); |
| 13761 |
} |
| 13762 |
} |
| 13763 |
|
| 13764 |
/********************************** |
| 13765 |
* Try to run semantic routines. |
| 13766 |
* If they fail, return NULL. |
| 13767 |
*/ |
| 13768 |
Expression trySemantic(Expression exp, Scope* sc) |
| 13769 |
{ |
| 13770 |
//printf("+trySemantic(%s)\n", exp.toChars()); |
| 13771 |
const errors = global.startGagging(); |
| 13772 |
Expression e = expressionSemantic(exp, sc); |
| 13773 |
if (global.endGagging(errors)) |
| 13774 |
{ |
| 13775 |
e = null; |
| 13776 |
} |
| 13777 |
//printf("-trySemantic(%s)\n", exp.toChars()); |
| 13778 |
return e; |
| 13779 |
} |
| 13780 |
|
| 13781 |
/********************************** |
| 13782 |
* Try expression semantic on `exp`, gagging semantic errors, |
| 13783 |
* but don't resolve alias this on a BinExp when the lhs or rhs |
| 13784 |
* has the corresponding type in `aliasThisStop` (See `isRecursiveAliasThis`). |
| 13785 |
* |
| 13786 |
* Params: |
| 13787 |
* exp = expression to try semantic on |
| 13788 |
* sc = scope |
| 13789 |
* aliasThisStop = pair of recursive alias this types to stop endless recursion |
| 13790 |
* Returns: |
| 13791 |
* exp after expression semantic, or `null` on error |
| 13792 |
*/ |
| 13793 |
Expression trySemanticAliasThis(Expression exp, Scope* sc, Type[2] aliasThisStop) |
| 13794 |
{ |
| 13795 |
if (exp.expressionSemanticDone) |
| 13796 |
return exp; |
| 13797 |
|
| 13798 |
const errors = global.startGagging(); |
| 13799 |
Expression e = expressionSemantic(exp, sc, aliasThisStop); |
| 13800 |
|
| 13801 |
if (global.endGagging(errors)) |
| 13802 |
return null; |
| 13803 |
|
| 13804 |
return e; |
| 13805 |
} |
| 13806 |
|
| 13807 |
/************************** |
| 13808 |
* Helper function for easy error propagation. |
| 13809 |
* If error occurs, returns ErrorExp. Otherwise returns NULL. |
| 13810 |
*/ |
| 13811 |
Expression unaSemantic(UnaExp e, Scope* sc) |
| 13812 |
{ |
| 13813 |
static if (LOGSEMANTIC) |
| 13814 |
{ |
| 13815 |
printf("UnaExp::semantic('%s')\n", e.toChars()); |
| 13816 |
} |
| 13817 |
Expression e1x = e.e1.expressionSemantic(sc); |
| 13818 |
if (e1x.op == EXP.error) |
| 13819 |
return e1x; |
| 13820 |
e.e1 = e1x; |
| 13821 |
return null; |
| 13822 |
} |
| 13823 |
|
| 13824 |
/************************** |
| 13825 |
* Helper function for easy error propagation. |
| 13826 |
* If error occurs, returns ErrorExp. Otherwise returns NULL. |
| 13827 |
*/ |
| 13828 |
Expression binSemantic(BinExp e, Scope* sc) |
| 13829 |
{ |
| 13830 |
static if (LOGSEMANTIC) |
| 13831 |
{ |
| 13832 |
printf("BinExp::semantic('%s')\n", e.toChars()); |
| 13833 |
} |
| 13834 |
Expression e1x = e.e1.expressionSemantic(sc); |
| 13835 |
Expression e2x = e.e2.expressionSemantic(sc); |
| 13836 |
|
| 13837 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 13838 |
if (e1x.op == EXP.type) |
| 13839 |
e1x = resolveAliasThis(sc, e1x); |
| 13840 |
if (e2x.op == EXP.type) |
| 13841 |
e2x = resolveAliasThis(sc, e2x); |
| 13842 |
|
| 13843 |
if (e1x.op == EXP.error) |
| 13844 |
return e1x; |
| 13845 |
if (e2x.op == EXP.error) |
| 13846 |
return e2x; |
| 13847 |
e.e1 = e1x; |
| 13848 |
e.e2 = e2x; |
| 13849 |
return null; |
| 13850 |
} |
| 13851 |
|
| 13852 |
Expression binSemanticProp(BinExp e, Scope* sc) |
| 13853 |
{ |
| 13854 |
if (Expression ex = binSemantic(e, sc)) |
| 13855 |
return ex; |
| 13856 |
Expression e1x = resolveProperties(sc, e.e1); |
| 13857 |
Expression e2x = resolveProperties(sc, e.e2); |
| 13858 |
if (e1x.op == EXP.error) |
| 13859 |
return e1x; |
| 13860 |
if (e2x.op == EXP.error) |
| 13861 |
return e2x; |
| 13862 |
e.e1 = e1x; |
| 13863 |
e.e2 = e2x; |
| 13864 |
return null; |
| 13865 |
} |
| 13866 |
|
| 13867 |
/// Returns: whether expressionSemantic() has been run on expression `e` |
| 13868 |
private bool expressionSemanticDone(Expression e) |
| 13869 |
{ |
| 13870 |
// Usually, Expression.type gets set by expressionSemantic and is `null` beforehand |
| 13871 |
// There are some exceptions however: |
| 13872 |
return e.type !is null && !( |
| 13873 |
e.isRealExp() // type sometimes gets set already before semantic |
| 13874 |
|| e.isTypeExp() // stores its type in the Expression.type field |
| 13875 |
|| e.isCompoundLiteralExp() // stores its `(type) {}` in type field, gets rewritten to struct literal |
| 13876 |
|| e.isVarExp() // type sometimes gets set already before semantic |
| 13877 |
); |
| 13878 |
} |
| 13879 |
|
| 13880 |
// entrypoint for semantic ExpressionSemanticVisitor |
| 13881 |
Expression expressionSemantic(Expression e, Scope* sc) |
| 13882 |
{ |
| 13883 |
if (e.expressionSemanticDone) |
| 13884 |
return e; |
| 13885 |
|
| 13886 |
scope v = new ExpressionSemanticVisitor(sc); |
| 13887 |
e.accept(v); |
| 13888 |
return v.result; |
| 13889 |
} |
| 13890 |
|
| 13891 |
// ditto, but passes alias this stop types, see trySemanticAliasThis |
| 13892 |
private Expression expressionSemantic(Expression e, Scope* sc, Type[2] aliasThisStop) |
| 13893 |
{ |
| 13894 |
if (e.expressionSemanticDone) |
| 13895 |
return e; |
| 13896 |
|
| 13897 |
scope v = new ExpressionSemanticVisitor(sc); |
| 13898 |
v.aliasThisStop = aliasThisStop; |
| 13899 |
e.accept(v); |
| 13900 |
return v.result; |
| 13901 |
} |
| 13902 |
|
| 13903 |
// ditto, but with `parent` parameter that represents the expression before rewriting. |
| 13904 |
// This way, when lowering an expression (e.g. i++ to i+=1), error messages can still |
| 13905 |
// refer to the original expression. |
| 13906 |
private Expression expressionSemanticWithParent(Expression e, Scope* sc, Expression parent) |
| 13907 |
{ |
| 13908 |
if (e.expressionSemanticDone) |
| 13909 |
return e; |
| 13910 |
|
| 13911 |
scope v = new ExpressionSemanticVisitor(sc); |
| 13912 |
v.parent = parent; |
| 13913 |
e.accept(v); |
| 13914 |
return v.result; |
| 13915 |
} |
| 13916 |
|
| 13917 |
private Expression dotIdSemanticPropX(DotIdExp exp, Scope* sc) |
| 13918 |
{ |
| 13919 |
//printf("dotIdSemanticPropX() %s\n", toChars(exp)); |
| 13920 |
if (Expression ex = unaSemantic(exp, sc)) |
| 13921 |
return ex; |
| 13922 |
|
| 13923 |
if (!sc.inCfile && exp.ident == Id._mangleof) |
| 13924 |
{ |
| 13925 |
// symbol.mangleof |
| 13926 |
|
| 13927 |
// return mangleof as an Expression |
| 13928 |
static Expression dotMangleof(Loc loc, Scope* sc, Dsymbol ds, bool hasOverloads) |
| 13929 |
{ |
| 13930 |
Expression e; |
| 13931 |
|
| 13932 |
assert(ds); |
| 13933 |
if (auto f = ds.isFuncDeclaration()) |
| 13934 |
{ |
| 13935 |
if (f.checkForwardRef(loc)) |
| 13936 |
return ErrorExp.get(); |
| 13937 |
|
| 13938 |
if (f.purityInprocess || f.safetyInprocess || f.nothrowInprocess || f.nogcInprocess) |
| 13939 |
{ |
| 13940 |
error(loc, "%s `%s` cannot retrieve its `.mangleof` while inferring attributes", f.kind, f.toPrettyChars); |
| 13941 |
return ErrorExp.get(); |
| 13942 |
} |
| 13943 |
|
| 13944 |
if (!hasOverloads) |
| 13945 |
e = StringExp.create(loc, mangleExact(f)); |
| 13946 |
} |
| 13947 |
|
| 13948 |
if (!e) |
| 13949 |
{ |
| 13950 |
OutBuffer buf; |
| 13951 |
mangleToBuffer(ds, buf); |
| 13952 |
e = new StringExp(loc, buf.extractSlice()); |
| 13953 |
} |
| 13954 |
|
| 13955 |
return e.expressionSemantic(sc); |
| 13956 |
} |
| 13957 |
|
| 13958 |
Dsymbol ds; |
| 13959 |
switch (exp.e1.op) |
| 13960 |
{ |
| 13961 |
case EXP.scope_: return dotMangleof(exp.loc, sc, exp.e1.isScopeExp().sds, false); |
| 13962 |
case EXP.overloadSet: return dotMangleof(exp.loc, sc, exp.e1.isOverExp().vars, false); |
| 13963 |
case EXP.variable: |
| 13964 |
{ |
| 13965 |
VarExp ve = exp.e1.isVarExp(); |
| 13966 |
return dotMangleof(exp.loc, sc, ve.var, ve.hasOverloads); |
| 13967 |
} |
| 13968 |
case EXP.dotVariable: |
| 13969 |
{ |
| 13970 |
DotVarExp dve = exp.e1.isDotVarExp(); |
| 13971 |
return dotMangleof(exp.loc, sc, dve.var, dve.hasOverloads); |
| 13972 |
} |
| 13973 |
case EXP.template_: |
| 13974 |
{ |
| 13975 |
TemplateExp te = exp.e1.isTemplateExp(); |
| 13976 |
return dotMangleof(exp.loc, sc, ds = te.fd ? te.fd.isDsymbol() : te.td, false); |
| 13977 |
} |
| 13978 |
|
| 13979 |
default: |
| 13980 |
break; |
| 13981 |
} |
| 13982 |
} |
| 13983 |
|
| 13984 |
if (exp.e1.isVarExp() && exp.e1.type.toBasetype().isTypeSArray() && exp.ident == Id.length) |
| 13985 |
{ |
| 13986 |
// bypass checkPurity |
| 13987 |
return exp.e1.type.dotExp(sc, exp.e1, exp.ident, cast(DotExpFlag) (exp.noderef * DotExpFlag.noDeref)); |
| 13988 |
} |
| 13989 |
|
| 13990 |
if (!exp.e1.isDotExp()) |
| 13991 |
{ |
| 13992 |
exp.e1 = resolvePropertiesX(sc, exp.e1); |
| 13993 |
} |
| 13994 |
|
| 13995 |
if (auto te = exp.e1.isTupleExp()) |
| 13996 |
{ |
| 13997 |
if (exp.ident == Id.offsetof || |
| 13998 |
exp.ident == Id.bitoffsetof || |
| 13999 |
exp.ident == Id.bitwidth) |
| 14000 |
{ |
| 14001 |
/* 'distribute' the .offsetof to each of the tuple elements. |
| 14002 |
*/ |
| 14003 |
auto exps = new Expressions(te.exps.length); |
| 14004 |
foreach (i, e; (*te.exps)[]) |
| 14005 |
{ |
| 14006 |
(*exps)[i] = new DotIdExp(e.loc, e, exp.ident); |
| 14007 |
} |
| 14008 |
// Don't evaluate te.e0 in runtime |
| 14009 |
Expression e = new TupleExp(exp.loc, null, exps); |
| 14010 |
e = e.expressionSemantic(sc); |
| 14011 |
return e; |
| 14012 |
} |
| 14013 |
if (exp.ident == Id.length) |
| 14014 |
{ |
| 14015 |
// Don't evaluate te.e0 in runtime |
| 14016 |
return new IntegerExp(exp.loc, te.exps.length, Type.tsize_t); |
| 14017 |
} |
| 14018 |
} |
| 14019 |
|
| 14020 |
// https://issues.dlang.org/show_bug.cgi?id=14416 |
| 14021 |
// Template has no built-in properties except for 'stringof'. |
| 14022 |
if ((exp.e1.isDotTemplateExp() || exp.e1.isTemplateExp()) && exp.ident != Id.stringof) |
| 14023 |
{ |
| 14024 |
error(exp.loc, "template `%s` does not have property `%s`", exp.e1.toErrMsg(), exp.ident.toChars()); |
| 14025 |
return ErrorExp.get(); |
| 14026 |
} |
| 14027 |
if (!exp.e1.type) |
| 14028 |
{ |
| 14029 |
error(exp.loc, "expression `%s` does not have property `%s`", exp.e1.toErrMsg(), exp.ident.toChars()); |
| 14030 |
return ErrorExp.get(); |
| 14031 |
} |
| 14032 |
|
| 14033 |
return exp; |
| 14034 |
} |
| 14035 |
|
| 14036 |
/****************************** |
| 14037 |
* Resolve properties, i.e. `e1.ident`, without seeing UFCS. |
| 14038 |
* Params: |
| 14039 |
* exp = expression to resolve |
| 14040 |
* sc = context |
| 14041 |
* gag = do not emit error messages, just return `null` |
| 14042 |
* Returns: |
| 14043 |
* resolved expression, null if error |
| 14044 |
*/ |
| 14045 |
Expression dotIdSemanticProp(DotIdExp exp, Scope* sc, bool gag) |
| 14046 |
{ |
| 14047 |
//printf("dotIdSemanticProp('%s')\n", exp.toChars()); |
| 14048 |
|
| 14049 |
//{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } |
| 14050 |
|
| 14051 |
const cfile = sc.inCfile; |
| 14052 |
|
| 14053 |
/* Special case: rewrite this.id and super.id |
| 14054 |
* to be classtype.id and baseclasstype.id |
| 14055 |
* if we have no this pointer. |
| 14056 |
*/ |
| 14057 |
if ((exp.e1.isThisExp() || exp.e1.isSuperExp()) && !hasThis(sc)) |
| 14058 |
{ |
| 14059 |
if (AggregateDeclaration ad = sc.getStructClassScope()) |
| 14060 |
{ |
| 14061 |
if (exp.e1.isThisExp()) |
| 14062 |
{ |
| 14063 |
exp.e1 = new TypeExp(exp.e1.loc, ad.type); |
| 14064 |
} |
| 14065 |
else |
| 14066 |
{ |
| 14067 |
if (auto cd = ad.isClassDeclaration()) |
| 14068 |
{ |
| 14069 |
if (cd.baseClass) |
| 14070 |
exp.e1 = new TypeExp(exp.e1.loc, cd.baseClass.type); |
| 14071 |
} |
| 14072 |
} |
| 14073 |
} |
| 14074 |
} |
| 14075 |
|
| 14076 |
{ |
| 14077 |
Expression e = dotIdSemanticPropX(exp, sc); |
| 14078 |
if (e != exp) |
| 14079 |
return e; |
| 14080 |
} |
| 14081 |
|
| 14082 |
Expression eleft; |
| 14083 |
Expression eright; |
| 14084 |
if (auto de = exp.e1.isDotExp()) |
| 14085 |
{ |
| 14086 |
eleft = de.e1; |
| 14087 |
eright = de.e2; |
| 14088 |
} |
| 14089 |
else |
| 14090 |
{ |
| 14091 |
eleft = null; |
| 14092 |
eright = exp.e1; |
| 14093 |
} |
| 14094 |
|
| 14095 |
Type t1b = exp.e1.type.toBasetype(); |
| 14096 |
|
| 14097 |
if (auto ie = eright.isScopeExp()) // also used for template alias's |
| 14098 |
{ |
| 14099 |
SearchOptFlags flags = SearchOpt.localsOnly; |
| 14100 |
/* Disable access to another module's private imports. |
| 14101 |
* The check for 'is sds our current module' is because |
| 14102 |
* the current module should have access to its own imports. |
| 14103 |
*/ |
| 14104 |
if (ie.sds.isModule() && ie.sds != sc._module) |
| 14105 |
flags |= SearchOpt.ignorePrivateImports; |
| 14106 |
if (sc.ignoresymbolvisibility) |
| 14107 |
flags |= SearchOpt.ignoreVisibility; |
| 14108 |
Dsymbol s = ie.sds.search(exp.loc, exp.ident, flags); |
| 14109 |
/* Check for visibility before resolving aliases because public |
| 14110 |
* aliases to private symbols are public. |
| 14111 |
*/ |
| 14112 |
if (s && !sc.ignoresymbolvisibility && !symbolIsVisible(sc._module, s)) |
| 14113 |
{ |
| 14114 |
s = null; |
| 14115 |
} |
| 14116 |
if (s) |
| 14117 |
{ |
| 14118 |
auto p = s.isPackage(); |
| 14119 |
if (p && checkAccess(sc, p)) |
| 14120 |
{ |
| 14121 |
s = null; |
| 14122 |
} |
| 14123 |
} |
| 14124 |
if (s) |
| 14125 |
{ |
| 14126 |
// if 's' is a tuple variable, the tuple is returned. |
| 14127 |
s = s.toAlias(); |
| 14128 |
|
| 14129 |
s.checkDeprecated(exp.loc, sc); |
| 14130 |
if (auto d = s.isDeclaration()) |
| 14131 |
d.checkDisabled(exp.loc, sc); |
| 14132 |
|
| 14133 |
if (auto em = s.isEnumMember()) |
| 14134 |
{ |
| 14135 |
return em.getVarExp(exp.loc, sc); |
| 14136 |
} |
| 14137 |
if (auto v = s.isVarDeclaration()) |
| 14138 |
{ |
| 14139 |
//printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v.type.toChars()); |
| 14140 |
if (!v.type || |
| 14141 |
!v.type.deco && v.inuse) |
| 14142 |
{ |
| 14143 |
if (v.inuse) |
| 14144 |
error(exp.loc, "circular reference to %s `%s`", v.kind(), v.toPrettyChars()); |
| 14145 |
else |
| 14146 |
error(exp.loc, "forward reference to %s `%s`", v.kind(), v.toPrettyChars()); |
| 14147 |
return ErrorExp.get(); |
| 14148 |
} |
| 14149 |
if (v.type.isTypeError()) |
| 14150 |
return ErrorExp.get(); |
| 14151 |
|
| 14152 |
if ((v.storage_class & STC.manifest) && v._init && !exp.wantsym) |
| 14153 |
{ |
| 14154 |
/* Normally, the replacement of a symbol with its initializer is supposed to be in semantic2(). |
| 14155 |
* Introduced by https://github.com/dlang/dmd/pull/5588 which should probably |
| 14156 |
* be reverted. `wantsym` is the hack to work around the problem. |
| 14157 |
*/ |
| 14158 |
if (v.inuse) |
| 14159 |
{ |
| 14160 |
error(exp.loc, "circular initialization of %s `%s`", v.kind(), v.toPrettyChars()); |
| 14161 |
return ErrorExp.get(); |
| 14162 |
} |
| 14163 |
auto e = v.expandInitializer(exp.loc); |
| 14164 |
v.inuse++; |
| 14165 |
e = e.expressionSemantic(sc); |
| 14166 |
v.inuse--; |
| 14167 |
return e; |
| 14168 |
} |
| 14169 |
|
| 14170 |
Expression e; |
| 14171 |
if (v.needThis()) |
| 14172 |
{ |
| 14173 |
if (!eleft) |
| 14174 |
eleft = new ThisExp(exp.loc); |
| 14175 |
e = new DotVarExp(exp.loc, eleft, v); |
| 14176 |
e = e.expressionSemantic(sc); |
| 14177 |
} |
| 14178 |
else |
| 14179 |
{ |
| 14180 |
e = new VarExp(exp.loc, v); |
| 14181 |
if (eleft) |
| 14182 |
{ |
| 14183 |
e = new CommaExp(exp.loc, eleft, e); |
| 14184 |
e.type = v.type; |
| 14185 |
} |
| 14186 |
} |
| 14187 |
e = e.deref(); |
| 14188 |
return e.expressionSemantic(sc); |
| 14189 |
} |
| 14190 |
|
| 14191 |
if (auto f = s.isFuncDeclaration()) |
| 14192 |
{ |
| 14193 |
//printf("it's a function\n"); |
| 14194 |
if (!functionSemantic(f)) |
| 14195 |
return ErrorExp.get(); |
| 14196 |
Expression e; |
| 14197 |
if (f.needThis()) |
| 14198 |
{ |
| 14199 |
if (!eleft) |
| 14200 |
eleft = new ThisExp(exp.loc); |
| 14201 |
e = new DotVarExp(exp.loc, eleft, f, true); |
| 14202 |
e = e.expressionSemantic(sc); |
| 14203 |
} |
| 14204 |
else |
| 14205 |
{ |
| 14206 |
e = new VarExp(exp.loc, f, true); |
| 14207 |
if (eleft) |
| 14208 |
{ |
| 14209 |
e = new CommaExp(exp.loc, eleft, e); |
| 14210 |
e.type = f.type; |
| 14211 |
} |
| 14212 |
} |
| 14213 |
return e; |
| 14214 |
} |
| 14215 |
if (auto td = s.isTemplateDeclaration()) |
| 14216 |
{ |
| 14217 |
Expression e; |
| 14218 |
if (eleft) |
| 14219 |
e = new DotTemplateExp(exp.loc, eleft, td); |
| 14220 |
else |
| 14221 |
e = new TemplateExp(exp.loc, td); |
| 14222 |
e = e.expressionSemantic(sc); |
| 14223 |
return e; |
| 14224 |
} |
| 14225 |
if (OverDeclaration od = s.isOverDeclaration()) |
| 14226 |
{ |
| 14227 |
Expression e = new VarExp(exp.loc, od, true); |
| 14228 |
if (eleft) |
| 14229 |
{ |
| 14230 |
e = new CommaExp(exp.loc, eleft, e); |
| 14231 |
e.type = Type.tvoid; // ambiguous type? |
| 14232 |
} |
| 14233 |
return e.expressionSemantic(sc); |
| 14234 |
} |
| 14235 |
if (auto o = s.isOverloadSet()) |
| 14236 |
{ |
| 14237 |
//printf("'%s' is an overload set\n", o.toChars()); |
| 14238 |
return new OverExp(exp.loc, o); |
| 14239 |
} |
| 14240 |
|
| 14241 |
if (auto t = s.getType()) |
| 14242 |
{ |
| 14243 |
return (new TypeExp(exp.loc, t)).expressionSemantic(sc); |
| 14244 |
} |
| 14245 |
|
| 14246 |
if (auto tup = s.isTupleDeclaration()) |
| 14247 |
{ |
| 14248 |
if (eleft) |
| 14249 |
{ |
| 14250 |
Expression e = new DotVarExp(exp.loc, eleft, tup); |
| 14251 |
e = e.expressionSemantic(sc); |
| 14252 |
return e; |
| 14253 |
} |
| 14254 |
Expression e = new TupleExp(exp.loc, tup); |
| 14255 |
e = e.expressionSemantic(sc); |
| 14256 |
return e; |
| 14257 |
} |
| 14258 |
|
| 14259 |
if (auto sds = s.isScopeDsymbol()) |
| 14260 |
{ |
| 14261 |
//printf("it's a ScopeDsymbol %s\n", ident.toChars()); |
| 14262 |
Expression e = new ScopeExp(exp.loc, sds); |
| 14263 |
e = e.expressionSemantic(sc); |
| 14264 |
if (eleft) |
| 14265 |
e = new DotExp(exp.loc, eleft, e); |
| 14266 |
return e; |
| 14267 |
} |
| 14268 |
|
| 14269 |
if (auto imp = s.isImport()) |
| 14270 |
{ |
| 14271 |
Expression se = new ScopeExp(exp.loc, imp.pkg); |
| 14272 |
return se.expressionSemantic(sc); |
| 14273 |
} |
| 14274 |
|
| 14275 |
if (auto attr = s.isAttribDeclaration()) |
| 14276 |
{ |
| 14277 |
if (auto sm = ie.sds.search(exp.loc, exp.ident, flags)) |
| 14278 |
{ |
| 14279 |
auto es = new DsymbolExp(exp.loc, sm); |
| 14280 |
return es; |
| 14281 |
} |
| 14282 |
} |
| 14283 |
|
| 14284 |
// BUG: handle other cases like in IdentifierExp::semantic() |
| 14285 |
debug |
| 14286 |
{ |
| 14287 |
printf("s = %p '%s', kind = '%s'\n", s, s.toChars(), s.kind()); |
| 14288 |
} |
| 14289 |
assert(0); |
| 14290 |
} |
| 14291 |
else if (exp.ident == Id.stringof) |
| 14292 |
{ |
| 14293 |
Expression e = new StringExp(exp.loc, ie.toString()); |
| 14294 |
e = e.expressionSemantic(sc); |
| 14295 |
return e; |
| 14296 |
} |
| 14297 |
if (ie.sds.isPackage() || ie.sds.isImport() || ie.sds.isModule()) |
| 14298 |
{ |
| 14299 |
gag = false; |
| 14300 |
} |
| 14301 |
if (gag) |
| 14302 |
return null; |
| 14303 |
s = ie.sds.search_correct(exp.ident); |
| 14304 |
if (s && symbolIsVisible(sc, s)) |
| 14305 |
{ |
| 14306 |
if (s.isPackage()) |
| 14307 |
error(exp.loc, "undefined identifier `%s` in %s `%s`, perhaps add `static import %s;`", exp.ident.toChars(), ie.sds.kind(), ie.sds.toPrettyChars(), s.toPrettyChars()); |
| 14308 |
else |
| 14309 |
error(exp.loc, "undefined identifier `%s` in %s `%s`, did you mean %s `%s`?", exp.ident.toChars(), ie.sds.kind(), ie.sds.toPrettyChars(), s.kind(), s.toChars()); |
| 14310 |
} |
| 14311 |
else |
| 14312 |
error(exp.loc, "undefined identifier `%s` in %s `%s`", exp.ident.toChars(), ie.sds.kind(), ie.sds.toPrettyChars()); |
| 14313 |
return ErrorExp.get(); |
| 14314 |
} |
| 14315 |
else if (t1b.ty == Tpointer && exp.e1.type.ty != Tenum && |
| 14316 |
!( |
| 14317 |
exp.ident == Id.__sizeof || |
| 14318 |
exp.ident == Id.__xalignof || |
| 14319 |
!cfile && |
| 14320 |
(exp.ident == Id._mangleof || |
| 14321 |
exp.ident == Id.offsetof || |
| 14322 |
exp.ident == Id.bitoffsetof || |
| 14323 |
exp.ident == Id.bitwidth || |
| 14324 |
exp.ident == Id._init || |
| 14325 |
exp.ident == Id.stringof) |
| 14326 |
)) |
| 14327 |
{ |
| 14328 |
Type t1bn = t1b.nextOf(); |
| 14329 |
if (gag) |
| 14330 |
{ |
| 14331 |
if (AggregateDeclaration ad = isAggregate(t1bn)) |
| 14332 |
{ |
| 14333 |
if (!ad.members) // https://issues.dlang.org/show_bug.cgi?id=11312 |
| 14334 |
return null; |
| 14335 |
} |
| 14336 |
} |
| 14337 |
|
| 14338 |
/* Rewrite: |
| 14339 |
* p.ident |
| 14340 |
* as: |
| 14341 |
* (*p).ident |
| 14342 |
*/ |
| 14343 |
if (gag && t1bn.ty == Tvoid) |
| 14344 |
return null; |
| 14345 |
Expression e = new PtrExp(exp.loc, exp.e1); |
| 14346 |
e = e.expressionSemantic(sc); |
| 14347 |
const newFlag = cast(DotExpFlag) (gag * DotExpFlag.gag | exp.noderef * DotExpFlag.noDeref); |
| 14348 |
return e.type.dotExp(sc, e, exp.ident, newFlag); |
| 14349 |
} |
| 14350 |
else if (exp.ident == Id.__xalignof && |
| 14351 |
exp.e1.isVarExp() && |
| 14352 |
exp.e1.isVarExp().var.isVarDeclaration() && |
| 14353 |
!exp.e1.isVarExp().var.isVarDeclaration().alignment.isUnknown()) |
| 14354 |
{ |
| 14355 |
// For `x.alignof` get the alignment of the variable, not the alignment of its type |
| 14356 |
const explicitAlignment = exp.e1.isVarExp().var.isVarDeclaration().alignment; |
| 14357 |
const naturalAlignment = exp.e1.type.alignsize(); |
| 14358 |
const actualAlignment = explicitAlignment.isDefault() ? naturalAlignment : explicitAlignment.get(); |
| 14359 |
Expression e = new IntegerExp(exp.loc, actualAlignment, Type.tsize_t); |
| 14360 |
return e; |
| 14361 |
} |
| 14362 |
else if ((exp.ident == Id.max || exp.ident == Id.min) && |
| 14363 |
exp.e1.isVarExp() && |
| 14364 |
exp.e1.isVarExp().var.isBitFieldDeclaration()) |
| 14365 |
{ |
| 14366 |
// For `x.max` and `x.min` get the max/min of the bitfield, not the max/min of its type |
| 14367 |
auto bf = exp.e1.isVarExp().var.isBitFieldDeclaration(); |
| 14368 |
return new IntegerExp(exp.loc, bf.getMinMax(exp.ident), bf.type); |
| 14369 |
} |
| 14370 |
else if ((exp.ident == Id.max || exp.ident == Id.min) && |
| 14371 |
exp.e1.isDotVarExp() && |
| 14372 |
exp.e1.isDotVarExp().var.isBitFieldDeclaration()) |
| 14373 |
{ |
| 14374 |
// For `x.max` and `x.min` get the max/min of the bitfield, not the max/min of its type |
| 14375 |
auto bf = exp.e1.isDotVarExp().var.isBitFieldDeclaration(); |
| 14376 |
return new IntegerExp(exp.loc, bf.getMinMax(exp.ident), bf.type); |
| 14377 |
} |
| 14378 |
else |
| 14379 |
{ |
| 14380 |
if (exp.e1.isTypeExp() || exp.e1.isTemplateExp()) |
| 14381 |
gag = false; |
| 14382 |
|
| 14383 |
const flag = cast(DotExpFlag) (exp.noderef * DotExpFlag.noDeref | gag * DotExpFlag.gag); |
| 14384 |
|
| 14385 |
Expression e = dotExp(exp.e1.type, sc, exp.e1, exp.ident, flag); |
| 14386 |
if (e) |
| 14387 |
{ |
| 14388 |
e = e.expressionSemantic(sc); |
| 14389 |
} |
| 14390 |
return e; |
| 14391 |
} |
| 14392 |
} |
| 14393 |
|
| 14394 |
/** |
| 14395 |
* Resolve `e1.ident!tiargs` without seeing UFCS. |
| 14396 |
* Params: |
| 14397 |
* exp = the `DotTemplateInstanceExp` to resolve |
| 14398 |
* sc = the semantic scope |
| 14399 |
* gag = stop "not a property" error and return `null`. |
| 14400 |
* Returns: |
| 14401 |
* `null` if error or not found, or the resolved expression. |
| 14402 |
*/ |
| 14403 |
Expression dotTemplateSemanticProp(DotTemplateInstanceExp exp, Scope* sc, bool gag) |
| 14404 |
{ |
| 14405 |
static if (LOGSEMANTIC) |
| 14406 |
{ |
| 14407 |
printf("DotTemplateInstanceExpY::semantic('%s')\n", exp.toChars()); |
| 14408 |
} |
| 14409 |
|
| 14410 |
static Expression errorExp() |
| 14411 |
{ |
| 14412 |
return ErrorExp.get(); |
| 14413 |
} |
| 14414 |
|
| 14415 |
Expression e1 = exp.e1; |
| 14416 |
|
| 14417 |
if (exp.ti.tempdecl && exp.ti.tempdecl.parent && exp.ti.tempdecl.parent.isTemplateMixin()) |
| 14418 |
{ |
| 14419 |
// if 'ti.tempdecl' happens to be found in a mixin template don't lose that info |
| 14420 |
// and do the symbol search in that context (Issue: 19476) |
| 14421 |
auto tm = cast(TemplateMixin)exp.ti.tempdecl.parent; |
| 14422 |
e1 = new DotExp(exp.e1.loc, exp.e1, new ScopeExp(tm.loc, tm)); |
| 14423 |
} |
| 14424 |
|
| 14425 |
auto die = new DotIdExp(exp.loc, e1, exp.ti.name); |
| 14426 |
|
| 14427 |
Expression e = die.dotIdSemanticPropX(sc); |
| 14428 |
|
| 14429 |
Expression notTemplate() |
| 14430 |
{ |
| 14431 |
error(exp.loc, "`%s` isn't a template", e.toErrMsg()); |
| 14432 |
return errorExp(); |
| 14433 |
} |
| 14434 |
|
| 14435 |
if (e == die) |
| 14436 |
{ |
| 14437 |
exp.e1 = die.e1; // take back |
| 14438 |
Type t1b = exp.e1.type.toBasetype(); |
| 14439 |
if (t1b.isStaticOrDynamicArray() || t1b.ty == Taarray || t1b.ty == Tnull || (t1b.isTypeBasic() && t1b.ty != Tvoid)) |
| 14440 |
{ |
| 14441 |
/* No built-in type has templatized properties, so do shortcut. |
| 14442 |
* It is necessary in: 1024.max!"a < b" |
| 14443 |
*/ |
| 14444 |
if (gag) |
| 14445 |
return null; |
| 14446 |
} |
| 14447 |
e = die.dotIdSemanticProp(sc, gag); |
| 14448 |
if (gag) |
| 14449 |
{ |
| 14450 |
if (!e || |
| 14451 |
isDotOpDispatch(e)) |
| 14452 |
{ |
| 14453 |
/* opDispatch!tiargs would be a function template that needs IFTI, |
| 14454 |
* so it's not a template |
| 14455 |
*/ |
| 14456 |
return null; |
| 14457 |
} |
| 14458 |
} |
| 14459 |
} |
| 14460 |
assert(e); |
| 14461 |
|
| 14462 |
if (e.op == EXP.error) |
| 14463 |
return e; |
| 14464 |
if (DotVarExp dve = e.isDotVarExp()) |
| 14465 |
{ |
| 14466 |
if (FuncDeclaration fd = dve.var.isFuncDeclaration()) |
| 14467 |
{ |
| 14468 |
if (TemplateDeclaration td = fd.findTemplateDeclRoot()) |
| 14469 |
{ |
| 14470 |
e = new DotTemplateExp(dve.loc, dve.e1, td); |
| 14471 |
e = e.expressionSemantic(sc); |
| 14472 |
} |
| 14473 |
} |
| 14474 |
else if (OverDeclaration od = dve.var.isOverDeclaration()) |
| 14475 |
{ |
| 14476 |
exp.e1 = dve.e1; // pull semantic() result |
| 14477 |
|
| 14478 |
if (!exp.findTempDecl(sc)) |
| 14479 |
return notTemplate(); |
| 14480 |
if (exp.ti.needsTypeInference(sc)) |
| 14481 |
return exp; |
| 14482 |
exp.ti.dsymbolSemantic(sc); |
| 14483 |
if (!exp.ti.inst || exp.ti.errors) // if template failed to expand |
| 14484 |
return errorExp(); |
| 14485 |
|
| 14486 |
if (Declaration v = exp.ti.toAlias().isDeclaration()) |
| 14487 |
{ |
| 14488 |
if (v.type && !v.type.deco) |
| 14489 |
v.type = v.type.typeSemantic(v.loc, sc); |
| 14490 |
return new DotVarExp(exp.loc, exp.e1, v) |
| 14491 |
.expressionSemantic(sc); |
| 14492 |
} |
| 14493 |
return new DotExp(exp.loc, exp.e1, new ScopeExp(exp.loc, exp.ti)) |
| 14494 |
.expressionSemantic(sc); |
| 14495 |
} |
| 14496 |
} |
| 14497 |
else if (e.op == EXP.variable) |
| 14498 |
{ |
| 14499 |
VarExp ve = cast(VarExp)e; |
| 14500 |
if (FuncDeclaration fd = ve.var.isFuncDeclaration()) |
| 14501 |
{ |
| 14502 |
if (TemplateDeclaration td = fd.findTemplateDeclRoot()) |
| 14503 |
{ |
| 14504 |
e = new TemplateExp(ve.loc, td) |
| 14505 |
.expressionSemantic(sc); |
| 14506 |
} |
| 14507 |
} |
| 14508 |
else if (OverDeclaration od = ve.var.isOverDeclaration()) |
| 14509 |
{ |
| 14510 |
exp.ti.tempdecl = od; |
| 14511 |
return new ScopeExp(exp.loc, exp.ti) |
| 14512 |
.expressionSemantic(sc); |
| 14513 |
} |
| 14514 |
} |
| 14515 |
|
| 14516 |
if (DotTemplateExp dte = e.isDotTemplateExp()) |
| 14517 |
{ |
| 14518 |
exp.e1 = dte.e1; // pull semantic() result |
| 14519 |
|
| 14520 |
exp.ti.tempdecl = dte.td; |
| 14521 |
if (!exp.ti.semanticTiargs(sc)) |
| 14522 |
return errorExp(); |
| 14523 |
if (exp.ti.needsTypeInference(sc)) |
| 14524 |
return exp; |
| 14525 |
exp.ti.dsymbolSemantic(sc); |
| 14526 |
if (!exp.ti.inst || exp.ti.errors) // if template failed to expand |
| 14527 |
return errorExp(); |
| 14528 |
|
| 14529 |
if (Declaration v = exp.ti.toAlias().isDeclaration()) |
| 14530 |
{ |
| 14531 |
return new DotVarExp(exp.loc, exp.e1, v) |
| 14532 |
.expressionSemantic(sc); |
| 14533 |
} |
| 14534 |
return new DotExp(exp.loc, exp.e1, new ScopeExp(exp.loc, exp.ti)) |
| 14535 |
.expressionSemantic(sc); |
| 14536 |
} |
| 14537 |
else if (e.op == EXP.template_) |
| 14538 |
{ |
| 14539 |
exp.ti.tempdecl = (cast(TemplateExp)e).td; |
| 14540 |
return new ScopeExp(exp.loc, exp.ti) |
| 14541 |
.expressionSemantic(sc); |
| 14542 |
} |
| 14543 |
else if (DotExp de = e.isDotExp()) |
| 14544 |
{ |
| 14545 |
if (de.e2.op == EXP.overloadSet) |
| 14546 |
{ |
| 14547 |
if (!exp.findTempDecl(sc) || !exp.ti.semanticTiargs(sc)) |
| 14548 |
{ |
| 14549 |
return errorExp(); |
| 14550 |
} |
| 14551 |
if (exp.ti.needsTypeInference(sc)) |
| 14552 |
return exp; |
| 14553 |
exp.ti.dsymbolSemantic(sc); |
| 14554 |
if (!exp.ti.inst || exp.ti.errors) // if template failed to expand |
| 14555 |
return errorExp(); |
| 14556 |
|
| 14557 |
if (Declaration v = exp.ti.toAlias().isDeclaration()) |
| 14558 |
{ |
| 14559 |
if (v.type && !v.type.deco) |
| 14560 |
v.type = v.type.typeSemantic(v.loc, sc); |
| 14561 |
return new DotVarExp(exp.loc, exp.e1, v) |
| 14562 |
.expressionSemantic(sc); |
| 14563 |
} |
| 14564 |
return new DotExp(exp.loc, exp.e1, new ScopeExp(exp.loc, exp.ti)) |
| 14565 |
.expressionSemantic(sc); |
| 14566 |
} |
| 14567 |
} |
| 14568 |
else if (OverExp oe = e.isOverExp()) |
| 14569 |
{ |
| 14570 |
exp.ti.tempdecl = oe.vars; |
| 14571 |
return new ScopeExp(exp.loc, exp.ti) |
| 14572 |
.expressionSemantic(sc); |
| 14573 |
} |
| 14574 |
|
| 14575 |
return notTemplate(); |
| 14576 |
} |
| 14577 |
|
| 14578 |
MATCH matchType(FuncExp funcExp, Type to, Scope* sc, FuncExp* presult, ErrorSink eSink) |
| 14579 |
{ |
| 14580 |
auto loc = funcExp.loc; |
| 14581 |
auto tok = funcExp.tok; |
| 14582 |
auto td = funcExp.td; |
| 14583 |
auto fd = funcExp.fd; |
| 14584 |
auto type = funcExp.type; |
| 14585 |
|
| 14586 |
MATCH cannotInfer() |
| 14587 |
{ |
| 14588 |
eSink.error(loc, "cannot infer parameter types from `%s`", to.toChars()); |
| 14589 |
return MATCH.nomatch; |
| 14590 |
} |
| 14591 |
|
| 14592 |
//printf("FuncExp::matchType('%s'), to=%s\n", type ? type.toChars() : "null", to.toChars()); |
| 14593 |
if (presult) |
| 14594 |
*presult = null; |
| 14595 |
|
| 14596 |
TypeFunction tof = null; |
| 14597 |
if (to.ty == Tdelegate) |
| 14598 |
{ |
| 14599 |
if (tok == TOK.function_) |
| 14600 |
{ |
| 14601 |
eSink.error(loc, "cannot match function literal to delegate type `%s`", to.toChars()); |
| 14602 |
return MATCH.nomatch; |
| 14603 |
} |
| 14604 |
tof = cast(TypeFunction)to.nextOf(); |
| 14605 |
} |
| 14606 |
else if (to.ty == Tpointer && (tof = to.nextOf().isTypeFunction()) !is null) |
| 14607 |
{ |
| 14608 |
if (tok == TOK.delegate_) |
| 14609 |
{ |
| 14610 |
eSink.error(loc, "cannot match delegate literal to function pointer type `%s`", to.toChars()); |
| 14611 |
return MATCH.nomatch; |
| 14612 |
} |
| 14613 |
} |
| 14614 |
|
| 14615 |
if (td) |
| 14616 |
{ |
| 14617 |
if (!tof) |
| 14618 |
{ |
| 14619 |
return cannotInfer(); |
| 14620 |
} |
| 14621 |
|
| 14622 |
// Parameter types inference from 'tof' |
| 14623 |
assert(td._scope); |
| 14624 |
TypeFunction tf = fd.type.isTypeFunction(); |
| 14625 |
//printf("\ttof = %s\n", tof.toChars()); |
| 14626 |
//printf("\ttf = %s\n", tf.toChars()); |
| 14627 |
const dim = tf.parameterList.length; |
| 14628 |
|
| 14629 |
if (tof.parameterList.length != dim || tof.parameterList.varargs != tf.parameterList.varargs) |
| 14630 |
return cannotInfer(); |
| 14631 |
|
| 14632 |
auto tiargs = new Objects(); |
| 14633 |
tiargs.reserve(td.parameters.length); |
| 14634 |
|
| 14635 |
foreach (tp; *td.parameters) |
| 14636 |
{ |
| 14637 |
size_t u = 0; |
| 14638 |
foreach (i, p; tf.parameterList) |
| 14639 |
{ |
| 14640 |
if (auto ti = p.type.isTypeIdentifier()) |
| 14641 |
if (ti && ti.ident == tp.ident) |
| 14642 |
break; |
| 14643 |
|
| 14644 |
++u; |
| 14645 |
} |
| 14646 |
assert(u < dim); |
| 14647 |
Parameter pto = tof.parameterList[u]; |
| 14648 |
Type t = pto.type; |
| 14649 |
if (t.ty == Terror) |
| 14650 |
return cannotInfer(); |
| 14651 |
tf.parameterList[u].storageClass = tof.parameterList[u].storageClass; |
| 14652 |
tiargs.push(t); |
| 14653 |
} |
| 14654 |
|
| 14655 |
// Set target of return type inference |
| 14656 |
if (!tf.next && tof.next) |
| 14657 |
fd.treq = to; |
| 14658 |
|
| 14659 |
auto ti = new TemplateInstance(loc, td, tiargs); |
| 14660 |
Expression ex = (new ScopeExp(loc, ti)).expressionSemantic(td._scope); |
| 14661 |
|
| 14662 |
// Reset inference target for the later re-semantic |
| 14663 |
fd.treq = null; |
| 14664 |
|
| 14665 |
if (ex.op == EXP.error) |
| 14666 |
return MATCH.nomatch; |
| 14667 |
if (auto ef = ex.isFuncExp()) |
| 14668 |
return ef.matchType(to, sc, presult, eSink); |
| 14669 |
else |
| 14670 |
return cannotInfer(); |
| 14671 |
} |
| 14672 |
|
| 14673 |
if (!tof || !tof.next) |
| 14674 |
return MATCH.nomatch; |
| 14675 |
|
| 14676 |
assert(type && type != Type.tvoid); |
| 14677 |
if (fd.type.ty == Terror) |
| 14678 |
return MATCH.nomatch; |
| 14679 |
auto tfx = fd.type.isTypeFunction(); |
| 14680 |
bool convertMatch = (type.ty != to.ty); |
| 14681 |
|
| 14682 |
if (fd.inferRetType && tfx.next.implicitConvTo(tof.next) == MATCH.convert) |
| 14683 |
{ |
| 14684 |
/* If return type is inferred and covariant return, |
| 14685 |
* tweak return statements to required return type. |
| 14686 |
* |
| 14687 |
* interface I {} |
| 14688 |
* class C : Object, I{} |
| 14689 |
* |
| 14690 |
* I delegate() dg = delegate() { return new class C(); } |
| 14691 |
*/ |
| 14692 |
convertMatch = true; |
| 14693 |
|
| 14694 |
auto tfy = new TypeFunction(tfx.parameterList, tof.next, |
| 14695 |
tfx.linkage, STC.none); |
| 14696 |
tfy.mod = tfx.mod; |
| 14697 |
tfy.trust = tfx.trust; |
| 14698 |
tfy.isNothrow = tfx.isNothrow; |
| 14699 |
tfy.isNogc = tfx.isNogc; |
| 14700 |
tfy.purity = tfx.purity; |
| 14701 |
tfy.isProperty = tfx.isProperty; |
| 14702 |
tfy.isRef = tfx.isRef; |
| 14703 |
tfy.isInOutParam = tfx.isInOutParam; |
| 14704 |
tfy.isInOutQual = tfx.isInOutQual; |
| 14705 |
tfy.deco = tfy.merge().deco; |
| 14706 |
|
| 14707 |
tfx = tfy; |
| 14708 |
} |
| 14709 |
Type tx; |
| 14710 |
if (tok == TOK.delegate_ || |
| 14711 |
tok == TOK.reserved && (type.ty == Tdelegate || type.ty == Tpointer && to.ty == Tdelegate)) |
| 14712 |
{ |
| 14713 |
// Allow conversion from implicit function pointer to delegate |
| 14714 |
tx = new TypeDelegate(tfx); |
| 14715 |
tx.deco = tx.merge().deco; |
| 14716 |
} |
| 14717 |
else |
| 14718 |
{ |
| 14719 |
assert(tok == TOK.function_ || tok == TOK.reserved && type.ty == Tpointer || fd.errors); |
| 14720 |
tx = tfx.pointerTo(); |
| 14721 |
} |
| 14722 |
//printf("\ttx = %s, to = %s\n", tx.toChars(), to.toChars()); |
| 14723 |
|
| 14724 |
MATCH m = tx.implicitConvTo(to); |
| 14725 |
if (m > MATCH.nomatch) |
| 14726 |
{ |
| 14727 |
// MATCH.exact: exact type match |
| 14728 |
// MATCH.constant: covairiant type match (eg. attributes difference) |
| 14729 |
// MATCH.convert: context conversion |
| 14730 |
m = convertMatch ? MATCH.convert : tx.equals(to) ? MATCH.exact : MATCH.constant; |
| 14731 |
|
| 14732 |
if (presult) |
| 14733 |
{ |
| 14734 |
(*presult) = cast(FuncExp)funcExp.copy(); |
| 14735 |
(*presult).type = to; |
| 14736 |
|
| 14737 |
// https://issues.dlang.org/show_bug.cgi?id=12508 |
| 14738 |
// Tweak function body for covariant returns. |
| 14739 |
(*presult).fd.modifyReturns(sc, tof.next); |
| 14740 |
} |
| 14741 |
} |
| 14742 |
else if (!cast(ErrorSinkNull)eSink) |
| 14743 |
{ |
| 14744 |
auto ts = toAutoQualChars(tx, to); |
| 14745 |
eSink.error(loc, "cannot implicitly convert expression `%s` of type `%s` to `%s`", |
| 14746 |
funcExp.toErrMsg(), ts[0], ts[1]); |
| 14747 |
} |
| 14748 |
return m; |
| 14749 |
} |
| 14750 |
|
| 14751 |
private bool checkScalar(Expression e) |
| 14752 |
{ |
| 14753 |
if (e.op == EXP.error) |
| 14754 |
return true; |
| 14755 |
if (e.type.toBasetype().ty == Terror) |
| 14756 |
return true; |
| 14757 |
if (!e.type.isScalar()) |
| 14758 |
{ |
| 14759 |
error(e.loc, "`%s` is not a scalar, it is a `%s`", e.toErrMsg(), e.type.toChars()); |
| 14760 |
return true; |
| 14761 |
} |
| 14762 |
return e.checkValue(); |
| 14763 |
} |
| 14764 |
|
| 14765 |
private bool checkNoBool(Expression e) |
| 14766 |
{ |
| 14767 |
if (e.op == EXP.error) |
| 14768 |
return true; |
| 14769 |
if (e.type.toBasetype().ty == Terror) |
| 14770 |
return true; |
| 14771 |
if (e.type.toBasetype().ty == Tbool) |
| 14772 |
{ |
| 14773 |
error(e.loc, "operation not allowed on `bool` `%s`", e.toErrMsg()); |
| 14774 |
return true; |
| 14775 |
} |
| 14776 |
return false; |
| 14777 |
} |
| 14778 |
|
| 14779 |
private bool checkIntegral(Expression e) |
| 14780 |
{ |
| 14781 |
if (e.op == EXP.error) |
| 14782 |
return true; |
| 14783 |
if (e.type.toBasetype().ty == Terror) |
| 14784 |
return true; |
| 14785 |
if (!e.type.isIntegral()) |
| 14786 |
{ |
| 14787 |
error(e.loc, "`%s` is not of integral type, it is a `%s`", e.toErrMsg(), e.type.toChars()); |
| 14788 |
return true; |
| 14789 |
} |
| 14790 |
return e.checkValue(); |
| 14791 |
} |
| 14792 |
|
| 14793 |
private bool checkArithmetic(Expression e, EXP op) |
| 14794 |
{ |
| 14795 |
if (op == EXP.error) |
| 14796 |
return true; |
| 14797 |
if (e.type.toBasetype().ty == Terror) |
| 14798 |
return true; |
| 14799 |
if (!e.type.isIntegral() && !e.type.isFloating()) |
| 14800 |
{ |
| 14801 |
// unary aggregate ops error here |
| 14802 |
const char* msg = e.type.isAggregate() ? |
| 14803 |
"operator `%s` is not defined for `%s` of type `%s`" : |
| 14804 |
"illegal operator `%s` for `%s` of type `%s`"; |
| 14805 |
error(e.loc, msg, EXPtoString(op).ptr, e.toErrMsg(), e.type.toChars()); |
| 14806 |
return true; |
| 14807 |
} |
| 14808 |
|
| 14809 |
if ((op == EXP.add || op == EXP.min) && e.isTypeExp()) |
| 14810 |
{ |
| 14811 |
// @@@DEPRECATED_2.121@@@ |
| 14812 |
// Deprecated in 2.111 |
| 14813 |
// In 2.121, remove this branch to let `checkValue` raise the error |
| 14814 |
deprecation(e.loc, "type `%s` has no value", e.toChars); |
| 14815 |
if (!e.type.isOpaqueType) |
| 14816 |
deprecationSupplemental(e.loc, "perhaps use `%s.init`", e.toChars); |
| 14817 |
return false; |
| 14818 |
} |
| 14819 |
|
| 14820 |
return e.checkValue(); |
| 14821 |
} |
| 14822 |
|
| 14823 |
/******************************* |
| 14824 |
* Check whether the expression allows RMW operations, error with rmw operator diagnostic if not. |
| 14825 |
* ex is the RHS expression, or NULL if ++/-- is used (for diagnostics) |
| 14826 |
* Returns true if error occurs. |
| 14827 |
*/ |
| 14828 |
private bool checkReadModifyWrite(Expression e, EXP rmwOp, Expression ex = null) |
| 14829 |
{ |
| 14830 |
//printf("Expression::checkReadModifyWrite() %s %s", toChars(), ex ? ex.toChars() : ""); |
| 14831 |
if (!e.type || !e.type.isShared() || e.type.isTypeStruct() || e.type.isTypeClass()) |
| 14832 |
return false; |
| 14833 |
|
| 14834 |
// atomicOp uses opAssign (+=/-=) rather than opOp (++/--) for the CT string literal. |
| 14835 |
switch (rmwOp) |
| 14836 |
{ |
| 14837 |
case EXP.plusPlus: |
| 14838 |
case EXP.prePlusPlus: |
| 14839 |
rmwOp = EXP.addAssign; |
| 14840 |
break; |
| 14841 |
case EXP.minusMinus: |
| 14842 |
case EXP.preMinusMinus: |
| 14843 |
rmwOp = EXP.minAssign; |
| 14844 |
break; |
| 14845 |
default: |
| 14846 |
break; |
| 14847 |
} |
| 14848 |
|
| 14849 |
error(e.loc, "read-modify-write operations are not allowed for `shared` variables"); |
| 14850 |
errorSupplemental(e.loc, "Use `core.atomic.atomicOp!\"%s\"(%s, %s)` instead", |
| 14851 |
EXPtoString(rmwOp).ptr, e.toChars(), ex ? ex.toChars() : "1"); |
| 14852 |
return true; |
| 14853 |
} |
| 14854 |
|
| 14855 |
private bool checkSharedAccessBin(BinExp binExp, Scope* sc) |
| 14856 |
{ |
| 14857 |
const r1 = binExp.e1.checkSharedAccess(sc); |
| 14858 |
const r2 = binExp.e2.checkSharedAccess(sc); |
| 14859 |
return (r1 || r2); |
| 14860 |
} |
| 14861 |
|
| 14862 |
private bool checkIntegralBin(BinExp e) |
| 14863 |
{ |
| 14864 |
bool r1 = e.e1.checkIntegral(); |
| 14865 |
bool r2 = e.e2.checkIntegral(); |
| 14866 |
return (r1 || r2); |
| 14867 |
} |
| 14868 |
|
| 14869 |
private bool checkArithmeticBin(BinExp e) |
| 14870 |
{ |
| 14871 |
return (e.e1.checkArithmetic(e.op) || e.e2.checkArithmetic(e.op)); |
| 14872 |
} |
| 14873 |
|
| 14874 |
/**************************************** |
| 14875 |
* Check that the expression has a valid value. |
| 14876 |
* If not, generates an error "... has no value".` |
| 14877 |
* |
| 14878 |
* Params: |
| 14879 |
* e = expression to check |
| 14880 |
* |
| 14881 |
* Returns: |
| 14882 |
* `true` if the expression is not valid or has `void` type. |
| 14883 |
*/ |
| 14884 |
bool checkValue(Expression e) |
| 14885 |
{ |
| 14886 |
if (auto te = e.isTypeExp()) |
| 14887 |
{ |
| 14888 |
error(e.loc, "type `%s` has no value", e.toErrMsg()); |
| 14889 |
if (!e.type.isOpaqueType) |
| 14890 |
errorSupplemental(e.loc, "perhaps use `%s.init`", e.toChars()); |
| 14891 |
return true; |
| 14892 |
} |
| 14893 |
|
| 14894 |
if (auto dtie = e.isDotTemplateInstanceExp()) |
| 14895 |
{ |
| 14896 |
if (dtie.ti.tempdecl && |
| 14897 |
dtie.ti.semantictiargsdone && |
| 14898 |
dtie.ti.semanticRun == PASS.initial) |
| 14899 |
|
| 14900 |
error(e.loc, "partial %s `%s` has no value", dtie.ti.kind(), e.toErrMsg()); |
| 14901 |
else |
| 14902 |
error(e.loc, "%s `%s` has no value", dtie.ti.kind(), dtie.ti.toChars()); |
| 14903 |
return true; |
| 14904 |
} |
| 14905 |
|
| 14906 |
if (auto se = e.isScopeExp()) |
| 14907 |
{ |
| 14908 |
error(e.loc, "%s `%s` has no value", se.sds.kind(), se.sds.toChars()); |
| 14909 |
return true; |
| 14910 |
} |
| 14911 |
|
| 14912 |
if (auto te = e.isTemplateExp()) |
| 14913 |
{ |
| 14914 |
error(e.loc, "%s `%s` has no value", te.td.kind(), te.toChars()); |
| 14915 |
return true; |
| 14916 |
} |
| 14917 |
|
| 14918 |
if (auto fe = e.isFuncExp()) |
| 14919 |
{ |
| 14920 |
if (fe.td) |
| 14921 |
{ |
| 14922 |
error(e.loc, "template lambda has no value"); |
| 14923 |
return true; |
| 14924 |
} |
| 14925 |
return false; |
| 14926 |
} |
| 14927 |
|
| 14928 |
if (auto dte = e.isDotTemplateExp()) |
| 14929 |
{ |
| 14930 |
error(e.loc, "%s `%s` has no value", dte.td.kind(), e.toErrMsg()); |
| 14931 |
return true; |
| 14932 |
} |
| 14933 |
|
| 14934 |
if (e.type && e.type.toBasetype().ty == Tvoid) |
| 14935 |
{ |
| 14936 |
error(e.loc, "expression `%s` is `void` and has no value", e.toErrMsg()); |
| 14937 |
//print(); assert(0); |
| 14938 |
if (!global.gag) |
| 14939 |
e.type = Type.terror; |
| 14940 |
return true; |
| 14941 |
} |
| 14942 |
return false; |
| 14943 |
} |
| 14944 |
|
| 14945 |
/*************************************** |
| 14946 |
* If expression is shared, check that we can access it. |
| 14947 |
* Give error message if not. |
| 14948 |
* |
| 14949 |
* Params: |
| 14950 |
* e = expression to check |
| 14951 |
* sc = context |
| 14952 |
* returnRef = Whether this expression is for a `return` statement |
| 14953 |
* off a `ref` function, in which case a single level |
| 14954 |
* of dereference is allowed (e.g. `shared(int)*`). |
| 14955 |
* Returns: |
| 14956 |
* true on error |
| 14957 |
*/ |
| 14958 |
bool checkSharedAccess(Expression e, Scope* sc, bool returnRef = false) |
| 14959 |
{ |
| 14960 |
if (!sc || |
| 14961 |
!sc.previews.noSharedAccess || |
| 14962 |
sc.intypeof || |
| 14963 |
sc.ctfe) |
| 14964 |
{ |
| 14965 |
return false; |
| 14966 |
} |
| 14967 |
else if (sc._module.ident == Id.atomic && sc._module.parent !is null) |
| 14968 |
{ |
| 14969 |
// Allow core.internal.atomic, it is an compiler implementation for a given platform module. |
| 14970 |
// It is then exposed by other modules such as core.atomic and core.stdc.atomic. |
| 14971 |
// This is available as long as druntime is on the import path and the platform supports that operation. |
| 14972 |
|
| 14973 |
// https://issues.dlang.org/show_bug.cgi?id=24846 |
| 14974 |
|
| 14975 |
Package parent = sc._module.parent.isPackage(); |
| 14976 |
if (parent !is null) |
| 14977 |
{ |
| 14978 |
// This can be easily converted over to apply to core.atomic and core.internal.atomic |
| 14979 |
if (parent.ident == Id.internal) |
| 14980 |
{ |
| 14981 |
parent = parent.parent.isPackage(); |
| 14982 |
|
| 14983 |
if (parent !is null && parent.ident == Id.core && parent.parent is null) |
| 14984 |
return false; |
| 14985 |
} |
| 14986 |
} |
| 14987 |
} |
| 14988 |
|
| 14989 |
//printf("checkSharedAccess() `%s` returnRef: %d\n", e.toChars(), returnRef); |
| 14990 |
|
| 14991 |
bool check(Expression e, bool allowRef) |
| 14992 |
{ |
| 14993 |
bool sharedError(Expression e) |
| 14994 |
{ |
| 14995 |
// https://dlang.org/phobos/core_atomic.html |
| 14996 |
error(e.loc, "direct access to shared `%s` is not allowed, see `core.atomic`", e.toErrMsg()); |
| 14997 |
return true; |
| 14998 |
} |
| 14999 |
|
| 15000 |
// Error by default |
| 15001 |
bool visit(Expression e) |
| 15002 |
{ |
| 15003 |
// https://issues.dlang.org/show_bug.cgi?id=23639 |
| 15004 |
// Should be able to cast(shared) |
| 15005 |
if (!e.isCastExp() && e.type.isShared()) |
| 15006 |
return sharedError(e); |
| 15007 |
return false; |
| 15008 |
} |
| 15009 |
|
| 15010 |
bool visitNew(NewExp e) |
| 15011 |
{ |
| 15012 |
if (e.placement) |
| 15013 |
check(e.placement, false); |
| 15014 |
if (e.thisexp) |
| 15015 |
check(e.thisexp, false); |
| 15016 |
return false; |
| 15017 |
} |
| 15018 |
|
| 15019 |
bool visitVar(VarExp e) |
| 15020 |
{ |
| 15021 |
// https://issues.dlang.org/show_bug.cgi?id=20908 |
| 15022 |
// direct access to init symbols is ok as they |
| 15023 |
// cannot be modified. |
| 15024 |
if (e.var.isSymbolDeclaration()) |
| 15025 |
return false; |
| 15026 |
|
| 15027 |
// https://issues.dlang.org/show_bug.cgi?id=22626 |
| 15028 |
// Synchronized functions don't need to use core.atomic |
| 15029 |
// when accessing `this`. |
| 15030 |
if (sc.func && sc.func.isSynchronized()) |
| 15031 |
{ |
| 15032 |
if (e.var.isThisDeclaration()) |
| 15033 |
return false; |
| 15034 |
} |
| 15035 |
if (!allowRef && e.var.type.isShared()) |
| 15036 |
return sharedError(e); |
| 15037 |
|
| 15038 |
return false; |
| 15039 |
} |
| 15040 |
|
| 15041 |
bool visitAddr(AddrExp e) |
| 15042 |
{ |
| 15043 |
return check(e.e1, true); |
| 15044 |
} |
| 15045 |
|
| 15046 |
bool visitPtr(PtrExp e) |
| 15047 |
{ |
| 15048 |
if (!allowRef && e.type.isShared()) |
| 15049 |
return sharedError(e); |
| 15050 |
|
| 15051 |
if (e.e1.type.isShared()) |
| 15052 |
return sharedError(e); |
| 15053 |
|
| 15054 |
return check(e.e1, false); |
| 15055 |
} |
| 15056 |
|
| 15057 |
bool visitDotVar(DotVarExp e) |
| 15058 |
{ |
| 15059 |
//printf("dotvarexp = %s\n", e.toChars()); |
| 15060 |
if (e.type.isShared()) |
| 15061 |
{ |
| 15062 |
if (e.e1.isThisExp()) |
| 15063 |
{ |
| 15064 |
// https://issues.dlang.org/show_bug.cgi?id=22626 |
| 15065 |
if (sc.func && sc.func.isSynchronized()) |
| 15066 |
return false; |
| 15067 |
|
| 15068 |
// https://issues.dlang.org/show_bug.cgi?id=23790 |
| 15069 |
if (e.e1.type.isTypeStruct()) |
| 15070 |
return false; |
| 15071 |
} |
| 15072 |
|
| 15073 |
auto fd = e.var.isFuncDeclaration(); |
| 15074 |
const sharedFunc = fd && fd.type.isShared; |
| 15075 |
if (!allowRef && !sharedFunc) |
| 15076 |
return sharedError(e); |
| 15077 |
|
| 15078 |
// Allow using `DotVarExp` within value types |
| 15079 |
if (e.e1.type.isTypeSArray() || e.e1.type.isTypeStruct()) |
| 15080 |
return check(e.e1, allowRef); |
| 15081 |
|
| 15082 |
// If we end up with a single `VarExp`, it might be a `ref` param |
| 15083 |
// `shared ref T` param == `shared(T)*`. |
| 15084 |
if (auto ve = e.e1.isVarExp()) |
| 15085 |
{ |
| 15086 |
return check(e.e1, allowRef && (ve.var.storage_class & STC.ref_)); |
| 15087 |
} |
| 15088 |
|
| 15089 |
return sharedError(e); |
| 15090 |
} |
| 15091 |
|
| 15092 |
return check(e.e1, false); |
| 15093 |
} |
| 15094 |
|
| 15095 |
bool visitIndex(IndexExp e) |
| 15096 |
{ |
| 15097 |
if (!allowRef && e.type.isShared()) |
| 15098 |
return sharedError(e); |
| 15099 |
|
| 15100 |
if (e.e1.type.isShared()) |
| 15101 |
return sharedError(e); |
| 15102 |
|
| 15103 |
return check(e.e1, false); |
| 15104 |
} |
| 15105 |
|
| 15106 |
bool visitComma(CommaExp e) |
| 15107 |
{ |
| 15108 |
// Cannot be `return ref` since we can't use the return, |
| 15109 |
// but it's better to show that error than an unrelated `shared` one |
| 15110 |
return check(e.e2, true); |
| 15111 |
} |
| 15112 |
|
| 15113 |
switch (e.op) |
| 15114 |
{ |
| 15115 |
default: return visit(e); |
| 15116 |
|
| 15117 |
// Those have no indirections / can be ignored |
| 15118 |
case EXP.call: |
| 15119 |
case EXP.error: |
| 15120 |
case EXP.complex80: |
| 15121 |
case EXP.int64: |
| 15122 |
case EXP.null_: return false; |
| 15123 |
|
| 15124 |
case EXP.variable: return visitVar(e.isVarExp()); |
| 15125 |
case EXP.new_: return visitNew(e.isNewExp()); |
| 15126 |
case EXP.address: return visitAddr(e.isAddrExp()); |
| 15127 |
case EXP.star: return visitPtr(e.isPtrExp()); |
| 15128 |
case EXP.dotVariable: return visitDotVar(e.isDotVarExp()); |
| 15129 |
case EXP.index: return visitIndex(e.isIndexExp()); |
| 15130 |
} |
| 15131 |
} |
| 15132 |
|
| 15133 |
return check(e, returnRef); |
| 15134 |
} |
| 15135 |
|
| 15136 |
/**************************************** |
| 15137 |
* Resolve __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__, __FILE_FULL_PATH__ to loc. |
| 15138 |
*/ |
| 15139 |
Expression resolveLoc(Expression exp, Loc loc, Scope* sc) |
| 15140 |
{ |
| 15141 |
// Don't replace the special keywords, while we are inside a default |
| 15142 |
// argument. They are replaced later when copied to the call site. |
| 15143 |
if (sc.inDefaultArg) |
| 15144 |
return exp; |
| 15145 |
|
| 15146 |
exp.loc = loc; |
| 15147 |
|
| 15148 |
Expression visit(Expression exp) |
| 15149 |
{ |
| 15150 |
if (auto binExp = exp.isBinExp()) |
| 15151 |
{ |
| 15152 |
binExp.e1 = binExp.e1.resolveLoc(loc, sc); |
| 15153 |
binExp.e2 = binExp.e2.resolveLoc(loc, sc); |
| 15154 |
return binExp; |
| 15155 |
} |
| 15156 |
if (auto unaExp = exp.isUnaExp()) |
| 15157 |
{ |
| 15158 |
unaExp.e1 = unaExp.e1.resolveLoc(loc, sc); |
| 15159 |
return unaExp; |
| 15160 |
} |
| 15161 |
return exp; |
| 15162 |
} |
| 15163 |
|
| 15164 |
Expression visitCond(CondExp exp) |
| 15165 |
{ |
| 15166 |
exp.e1 = exp.e1.resolveLoc(loc, sc); |
| 15167 |
exp.e2 = exp.e2.resolveLoc(loc, sc); |
| 15168 |
exp.econd = exp.econd.resolveLoc(loc, sc); |
| 15169 |
return exp; |
| 15170 |
} |
| 15171 |
|
| 15172 |
Expression visitCat(CatExp exp) |
| 15173 |
{ |
| 15174 |
exp.e1 = exp.e1.resolveLoc(loc, sc); |
| 15175 |
exp.e2 = exp.e2.resolveLoc(loc, sc); |
| 15176 |
if (exp.lowering) |
| 15177 |
exp.lowering = exp.lowering.resolveLoc(loc, sc); |
| 15178 |
return exp; |
| 15179 |
} |
| 15180 |
|
| 15181 |
Expression visitStructLiteral(StructLiteralExp exp) |
| 15182 |
{ |
| 15183 |
if (!exp.elements) |
| 15184 |
return exp; |
| 15185 |
|
| 15186 |
foreach (ref element; *exp.elements) |
| 15187 |
{ |
| 15188 |
if (element) |
| 15189 |
element = element.resolveLoc(loc, sc); |
| 15190 |
} |
| 15191 |
|
| 15192 |
return exp; |
| 15193 |
} |
| 15194 |
|
| 15195 |
Expression visitNew(NewExp exp) |
| 15196 |
{ |
| 15197 |
if (exp.placement) |
| 15198 |
exp.placement = exp.placement.resolveLoc(loc, sc); |
| 15199 |
if (exp.thisexp) |
| 15200 |
exp.thisexp = exp.thisexp.resolveLoc(loc, sc); |
| 15201 |
if (exp.argprefix) |
| 15202 |
exp.argprefix = exp.argprefix.resolveLoc(loc, sc); |
| 15203 |
if (exp.lowering) |
| 15204 |
exp.lowering = exp.lowering.resolveLoc(loc, sc); |
| 15205 |
|
| 15206 |
if (!exp.arguments) |
| 15207 |
return exp; |
| 15208 |
|
| 15209 |
foreach (ref element; *exp.arguments) |
| 15210 |
{ |
| 15211 |
if (element) |
| 15212 |
element = element.resolveLoc(loc, sc); |
| 15213 |
} |
| 15214 |
|
| 15215 |
return exp; |
| 15216 |
} |
| 15217 |
|
| 15218 |
Expression visitCall(CallExp exp) |
| 15219 |
{ |
| 15220 |
if (!exp.arguments) |
| 15221 |
return exp; |
| 15222 |
|
| 15223 |
foreach (ref element; *exp.arguments) |
| 15224 |
{ |
| 15225 |
if (element) |
| 15226 |
element = element.resolveLoc(loc, sc); |
| 15227 |
} |
| 15228 |
|
| 15229 |
return exp; |
| 15230 |
} |
| 15231 |
|
| 15232 |
Expression visitArray(ArrayExp exp) |
| 15233 |
{ |
| 15234 |
exp.e1 = exp.e1.resolveLoc(loc, sc); |
| 15235 |
|
| 15236 |
if (!exp.arguments) |
| 15237 |
return exp; |
| 15238 |
|
| 15239 |
foreach (ref element; *exp.arguments) |
| 15240 |
{ |
| 15241 |
if (element) |
| 15242 |
element = element.resolveLoc(loc, sc); |
| 15243 |
} |
| 15244 |
|
| 15245 |
return exp; |
| 15246 |
} |
| 15247 |
|
| 15248 |
Expression visitSlice(SliceExp exp) |
| 15249 |
{ |
| 15250 |
exp.e1 = exp.e1.resolveLoc(loc, sc); |
| 15251 |
if (exp.lwr) |
| 15252 |
exp.lwr = exp.lwr.resolveLoc(loc, sc); |
| 15253 |
if (exp.upr) |
| 15254 |
exp.upr = exp.upr.resolveLoc(loc, sc); |
| 15255 |
|
| 15256 |
return exp; |
| 15257 |
} |
| 15258 |
|
| 15259 |
Expression visitInterval(IntervalExp exp) |
| 15260 |
{ |
| 15261 |
exp.lwr = exp.lwr.resolveLoc(loc, sc); |
| 15262 |
exp.upr = exp.upr.resolveLoc(loc, sc); |
| 15263 |
|
| 15264 |
return exp; |
| 15265 |
} |
| 15266 |
|
| 15267 |
Expression visitArrayLiteral(ArrayLiteralExp exp) |
| 15268 |
{ |
| 15269 |
if (exp.basis) |
| 15270 |
exp.basis = exp.basis.resolveLoc(loc, sc); |
| 15271 |
|
| 15272 |
if (!exp.elements) |
| 15273 |
return exp; |
| 15274 |
|
| 15275 |
foreach (ref element; *exp.elements) |
| 15276 |
{ |
| 15277 |
if (element) |
| 15278 |
element = element.resolveLoc(loc, sc); |
| 15279 |
} |
| 15280 |
|
| 15281 |
return exp; |
| 15282 |
} |
| 15283 |
|
| 15284 |
Expression visitAssocArrayLiteral(AssocArrayLiteralExp exp) |
| 15285 |
{ |
| 15286 |
foreach (ref element; *exp.keys) |
| 15287 |
{ |
| 15288 |
if (element) |
| 15289 |
element = element.resolveLoc(loc, sc); |
| 15290 |
} |
| 15291 |
|
| 15292 |
foreach (ref element; *exp.values) |
| 15293 |
{ |
| 15294 |
if (element) |
| 15295 |
element = element.resolveLoc(loc, sc); |
| 15296 |
} |
| 15297 |
|
| 15298 |
return exp; |
| 15299 |
} |
| 15300 |
|
| 15301 |
Expression visitFileInit(FileInitExp exp) |
| 15302 |
{ |
| 15303 |
//printf("FileInitExp::resolve() %s\n", exp.toChars()); |
| 15304 |
const(char)* s; |
| 15305 |
if (exp.op == EXP.fileFullPath) |
| 15306 |
s = FileName.toAbsolute(loc.isValid() ? loc.filename : sc._module.srcfile.toChars()); |
| 15307 |
else |
| 15308 |
s = loc.isValid() ? loc.filename : sc._module.ident.toChars(); |
| 15309 |
|
| 15310 |
Expression e = new StringExp(loc, s.toDString()); |
| 15311 |
return e.expressionSemantic(sc); |
| 15312 |
} |
| 15313 |
|
| 15314 |
Expression visitLineInit(LineInitExp exp) |
| 15315 |
{ |
| 15316 |
Expression e = new IntegerExp(loc, loc.linnum, Type.tint32); |
| 15317 |
return e.expressionSemantic(sc); |
| 15318 |
} |
| 15319 |
|
| 15320 |
Expression visitModuleInit(ModuleInitExp exp) |
| 15321 |
{ |
| 15322 |
const auto s = (sc.callsc ? sc.callsc : sc)._module.toPrettyChars().toDString(); |
| 15323 |
Expression e = new StringExp(loc, s); |
| 15324 |
return e.expressionSemantic(sc); |
| 15325 |
} |
| 15326 |
|
| 15327 |
Expression visitFuncInit(FuncInitExp exp) |
| 15328 |
{ |
| 15329 |
const(char)* s; |
| 15330 |
if (sc.callsc && sc.callsc.func) |
| 15331 |
s = sc.callsc.func.Dsymbol.toPrettyChars(); |
| 15332 |
else if (sc.func) |
| 15333 |
s = sc.func.Dsymbol.toPrettyChars(); |
| 15334 |
else |
| 15335 |
s = ""; |
| 15336 |
Expression e = new StringExp(loc, s.toDString()); |
| 15337 |
return e.expressionSemantic(sc); |
| 15338 |
} |
| 15339 |
|
| 15340 |
Expression visitPrettyFunc(PrettyFuncInitExp exp) |
| 15341 |
{ |
| 15342 |
FuncDeclaration fd = (sc.callsc && sc.callsc.func) |
| 15343 |
? sc.callsc.func |
| 15344 |
: sc.func; |
| 15345 |
|
| 15346 |
const(char)* s; |
| 15347 |
if (fd) |
| 15348 |
{ |
| 15349 |
const funcStr = fd.Dsymbol.toPrettyChars(); |
| 15350 |
OutBuffer buf; |
| 15351 |
functionToBufferWithIdent(fd.type.isTypeFunction(), buf, funcStr, fd.isStatic); |
| 15352 |
s = buf.extractChars(); |
| 15353 |
} |
| 15354 |
else |
| 15355 |
{ |
| 15356 |
s = ""; |
| 15357 |
} |
| 15358 |
|
| 15359 |
Expression e = new StringExp(loc, s.toDString()); |
| 15360 |
e = e.expressionSemantic(sc); |
| 15361 |
e.type = Type.tstring; |
| 15362 |
return e; |
| 15363 |
} |
| 15364 |
|
| 15365 |
switch(exp.op) |
| 15366 |
{ |
| 15367 |
default: return visit(exp); |
| 15368 |
case EXP.structLiteral: return visitStructLiteral(exp.isStructLiteralExp()); |
| 15369 |
case EXP.new_: return visitNew(exp.isNewExp()); |
| 15370 |
case EXP.concatenate: return visitCat(exp.isCatExp()); |
| 15371 |
case EXP.call: return visitCall(exp.isCallExp()); |
| 15372 |
case EXP.question: return visitCond(exp.isCondExp()); |
| 15373 |
case EXP.array: return visitArray(exp.isArrayExp()); |
| 15374 |
case EXP.slice: return visitSlice(exp.isSliceExp()); |
| 15375 |
case EXP.interval: return visitInterval(exp.isIntervalExp()); |
| 15376 |
case EXP.arrayLiteral: return visitArrayLiteral(exp.isArrayLiteralExp()); |
| 15377 |
case EXP.assocArrayLiteral: return visitAssocArrayLiteral(exp.isAssocArrayLiteralExp()); |
| 15378 |
case EXP.file: |
| 15379 |
case EXP.fileFullPath: return visitFileInit(exp.isFileInitExp()); |
| 15380 |
case EXP.line: return visitLineInit(exp.isLineInitExp); |
| 15381 |
case EXP.moduleString: return visitModuleInit(exp.isModuleInitExp()); |
| 15382 |
case EXP.functionString: return visitFuncInit(exp.isFuncInitExp()); |
| 15383 |
case EXP.prettyFunction: return visitPrettyFunc(exp.isPrettyFuncInitExp()); |
| 15384 |
} |
| 15385 |
} |
| 15386 |
|
| 15387 |
/************************************************ |
| 15388 |
* Destructors are attached to VarDeclarations. |
| 15389 |
* Hence, if expression returns a temp that needs a destructor, |
| 15390 |
* make sure and create a VarDeclaration for that temp. |
| 15391 |
*/ |
| 15392 |
Expression addDtorHook(Expression e, Scope* sc) |
| 15393 |
{ |
| 15394 |
//printf("addDtorHook() %s\n", toChars(e)); |
| 15395 |
Expression visit(Expression exp) |
| 15396 |
{ |
| 15397 |
return exp; |
| 15398 |
} |
| 15399 |
|
| 15400 |
Expression visitStructLiteral(StructLiteralExp exp) |
| 15401 |
{ |
| 15402 |
auto sd = exp.sd; |
| 15403 |
/* If struct requires a destructor, rewrite as: |
| 15404 |
* (S tmp = S()),tmp |
| 15405 |
* so that the destructor can be hung on tmp. |
| 15406 |
*/ |
| 15407 |
if (sd.dtor && sc.func) |
| 15408 |
{ |
| 15409 |
/* Make an identifier for the temporary of the form: |
| 15410 |
* __sl%s%d, where %s is the struct name |
| 15411 |
*/ |
| 15412 |
char[10] buf = void; |
| 15413 |
const prefix = "__sl"; |
| 15414 |
const ident = sd.ident.toString; |
| 15415 |
const fullLen = prefix.length + ident.length; |
| 15416 |
const len = fullLen < buf.length ? fullLen : buf.length; |
| 15417 |
buf[0 .. prefix.length] = prefix; |
| 15418 |
buf[prefix.length .. len] = ident[0 .. len - prefix.length]; |
| 15419 |
|
| 15420 |
auto tmp = copyToTemp(STC.none, buf[0 .. len], exp); |
| 15421 |
Expression ae = new DeclarationExp(exp.loc, tmp); |
| 15422 |
Expression e = new CommaExp(exp.loc, ae, new VarExp(exp.loc, tmp)); |
| 15423 |
e = e.expressionSemantic(sc); |
| 15424 |
return e; |
| 15425 |
} |
| 15426 |
|
| 15427 |
return exp; |
| 15428 |
} |
| 15429 |
|
| 15430 |
Expression visitCall(CallExp exp) |
| 15431 |
{ |
| 15432 |
auto e1 = exp.e1; |
| 15433 |
auto type = exp.type; |
| 15434 |
/* Only need to add dtor hook if it's a type that needs destruction. |
| 15435 |
* Use same logic as VarDeclaration::callScopeDtor() |
| 15436 |
*/ |
| 15437 |
|
| 15438 |
if (auto tf = e1.type.isTypeFunction()) |
| 15439 |
{ |
| 15440 |
if (tf.isRef) |
| 15441 |
return exp; |
| 15442 |
} |
| 15443 |
|
| 15444 |
Type tv = type.baseElemOf(); |
| 15445 |
if (auto ts = tv.isTypeStruct()) |
| 15446 |
{ |
| 15447 |
StructDeclaration sd = ts.sym; |
| 15448 |
if (sd.dtor) |
| 15449 |
{ |
| 15450 |
/* Type needs destruction, so declare a tmp |
| 15451 |
* which the back end will recognize and call dtor on |
| 15452 |
*/ |
| 15453 |
auto tmp = copyToTemp(STC.none, Id.__tmpfordtor.toString(), exp); |
| 15454 |
auto de = new DeclarationExp(exp.loc, tmp); |
| 15455 |
auto ve = new VarExp(exp.loc, tmp); |
| 15456 |
Expression e = new CommaExp(exp.loc, de, ve); |
| 15457 |
e = e.expressionSemantic(sc); |
| 15458 |
return e; |
| 15459 |
} |
| 15460 |
} |
| 15461 |
|
| 15462 |
return exp; |
| 15463 |
} |
| 15464 |
|
| 15465 |
Expression visitCast(CastExp exp) |
| 15466 |
{ |
| 15467 |
if (exp.to.toBasetype().ty == Tvoid) // look past the cast(void) |
| 15468 |
exp.e1 = exp.e1.addDtorHook(sc); |
| 15469 |
return exp; |
| 15470 |
} |
| 15471 |
|
| 15472 |
Expression visitComma(CommaExp exp) |
| 15473 |
{ |
| 15474 |
exp.e2 = exp.e2.addDtorHook(sc); |
| 15475 |
return exp; |
| 15476 |
} |
| 15477 |
|
| 15478 |
switch(e.op) |
| 15479 |
{ |
| 15480 |
default: return visit(e); |
| 15481 |
|
| 15482 |
case EXP.structLiteral: return visitStructLiteral(e.isStructLiteralExp()); |
| 15483 |
case EXP.call: return visitCall(e.isCallExp()); |
| 15484 |
case EXP.cast_: return visitCast(e.isCastExp()); |
| 15485 |
case EXP.comma: return visitComma(e.isCommaExp()); |
| 15486 |
} |
| 15487 |
} |
| 15488 |
|
| 15489 |
/******************************* |
| 15490 |
* Try to convert an expression to be an lvalue. |
| 15491 |
* |
| 15492 |
* Give error if we're not an lvalue. |
| 15493 |
* Params: |
| 15494 |
* _this = expression to convert |
| 15495 |
* sc = scope |
| 15496 |
* action = for error messages, what the lvalue is needed for (e.g. take address of for `&x`, modify for `x++`) |
| 15497 |
* Returns: converted expression, or `ErrorExp` on error |
| 15498 |
*/ |
| 15499 |
Expression toLvalue(Expression _this, Scope* sc, const(char)* action) |
| 15500 |
{ |
| 15501 |
return toLvalueImpl(_this, sc, action, _this); |
| 15502 |
} |
| 15503 |
|
| 15504 |
// e = original un-lowered expression for error messages, in case of recursive calls |
| 15505 |
private Expression toLvalueImpl(Expression _this, Scope* sc, const(char)* action, Expression e) |
| 15506 |
{ |
| 15507 |
if (!action) |
| 15508 |
action = "create lvalue of"; |
| 15509 |
|
| 15510 |
assert(e); |
| 15511 |
Expression visit(Expression _this) |
| 15512 |
{ |
| 15513 |
// BinaryAssignExp does not have an EXP associated |
| 15514 |
// so it's treated on the default path. |
| 15515 |
// Lvalue-ness will be handled in glue :layer. |
| 15516 |
if (_this.isBinAssignExp()) |
| 15517 |
return _this; |
| 15518 |
if (!_this.loc.isValid()) |
| 15519 |
_this.loc = e.loc; |
| 15520 |
|
| 15521 |
if (e.op == EXP.type) |
| 15522 |
error(_this.loc, "cannot %s type `%s`", action, e.type.toChars()); |
| 15523 |
else if (e.op == EXP.template_) |
| 15524 |
error(_this.loc, "cannot %s template `%s`, perhaps instantiate it first", action, e.toErrMsg()); |
| 15525 |
else |
| 15526 |
error(_this.loc, "cannot %s expression `%s` because it is not an lvalue", action, e.toErrMsg()); |
| 15527 |
|
| 15528 |
return ErrorExp.get(); |
| 15529 |
} |
| 15530 |
|
| 15531 |
Expression visitInteger(IntegerExp _this) |
| 15532 |
{ |
| 15533 |
if (!_this.loc.isValid()) |
| 15534 |
_this.loc = e.loc; |
| 15535 |
error(e.loc, "cannot %s constant `%s`", action, e.toErrMsg()); |
| 15536 |
return ErrorExp.get(); |
| 15537 |
} |
| 15538 |
|
| 15539 |
Expression visitThis(ThisExp _this) |
| 15540 |
{ |
| 15541 |
if (_this.type.toBasetype().ty == Tclass) |
| 15542 |
{ |
| 15543 |
// Class `this` is an rvalue; struct `this` is an lvalue. |
| 15544 |
return visit(_this); |
| 15545 |
} |
| 15546 |
|
| 15547 |
return _this; |
| 15548 |
} |
| 15549 |
|
| 15550 |
Expression visitString(StringExp _this) |
| 15551 |
{ |
| 15552 |
//printf("StringExp::toLvalue(%s) type = %s\n", _this.toChars(), _this.type ? _this.type.toChars() : NULL); |
| 15553 |
return (_this.type && _this.type.toBasetype().ty == Tsarray) ? _this : visit(_this); |
| 15554 |
} |
| 15555 |
|
| 15556 |
Expression visitStructLiteral(StructLiteralExp _this) |
| 15557 |
{ |
| 15558 |
if (sc.inCfile) |
| 15559 |
return _this; // C struct literals are lvalues |
| 15560 |
else |
| 15561 |
return visit(_this); |
| 15562 |
} |
| 15563 |
|
| 15564 |
Expression visitTemplate(TemplateExp _this) |
| 15565 |
{ |
| 15566 |
if (!_this.fd) |
| 15567 |
return visit(_this); |
| 15568 |
|
| 15569 |
assert(sc); |
| 15570 |
return symbolToExp(_this.fd, _this.loc, sc, true); |
| 15571 |
|
| 15572 |
} |
| 15573 |
|
| 15574 |
Expression visitVar(VarExp _this) |
| 15575 |
{ |
| 15576 |
auto var = _this.var; |
| 15577 |
if (var.storage_class & STC.manifest) |
| 15578 |
{ |
| 15579 |
error(_this.loc, "cannot %s manifest constant `%s`", action, var.toChars()); |
| 15580 |
return ErrorExp.get(); |
| 15581 |
} |
| 15582 |
if (var.storage_class & STC.lazy_ && !_this.delegateWasExtracted) |
| 15583 |
{ |
| 15584 |
error(_this.loc, "cannot %s lazy variable `%s`", action, var.toChars()); |
| 15585 |
return ErrorExp.get(); |
| 15586 |
} |
| 15587 |
if (var.ident == Id.ctfe) |
| 15588 |
{ |
| 15589 |
error(_this.loc, "cannot %s compiler-generated variable `__ctfe`", action); |
| 15590 |
return ErrorExp.get(); |
| 15591 |
} |
| 15592 |
if (var.ident == Id.dollar) // https://issues.dlang.org/show_bug.cgi?id=13574 |
| 15593 |
{ |
| 15594 |
error(_this.loc, "cannot %s operator `$`", action); |
| 15595 |
return ErrorExp.get(); |
| 15596 |
} |
| 15597 |
return _this; |
| 15598 |
} |
| 15599 |
|
| 15600 |
Expression visitDotVar(DotVarExp _this) |
| 15601 |
{ |
| 15602 |
auto e1 = _this.e1; |
| 15603 |
auto var = _this.var; |
| 15604 |
//printf("DotVarExp::toLvalue(%s)\n", toChars()); |
| 15605 |
if (sc && sc.inCfile) |
| 15606 |
{ |
| 15607 |
/* C11 6.5.2.3-3: A postfix expression followed by the '.' or '->' operator |
| 15608 |
* is an lvalue if the first expression is an lvalue. |
| 15609 |
*/ |
| 15610 |
if (!e1.isLvalue()) |
| 15611 |
return visit(_this); |
| 15612 |
} |
| 15613 |
if (!_this.isLvalue()) |
| 15614 |
return visit(_this); |
| 15615 |
if (e1.op == EXP.this_ && sc.ctorflow.fieldinit.length && !(sc.ctorflow.callSuper & CSX.any_ctor)) |
| 15616 |
{ |
| 15617 |
if (VarDeclaration vd = var.isVarDeclaration()) |
| 15618 |
{ |
| 15619 |
auto ad = vd.isMember2(); |
| 15620 |
if (ad && ad.fields.length == sc.ctorflow.fieldinit.length) |
| 15621 |
{ |
| 15622 |
foreach (i, f; ad.fields) |
| 15623 |
{ |
| 15624 |
if (f == vd) |
| 15625 |
{ |
| 15626 |
if (!(sc.ctorflow.fieldinit[i].csx & CSX.this_ctor)) |
| 15627 |
{ |
| 15628 |
/* If the address of vd is taken, assume it is thereby initialized |
| 15629 |
* https://issues.dlang.org/show_bug.cgi?id=15869 |
| 15630 |
*/ |
| 15631 |
modifyFieldVar(_this.loc, sc, vd, e1); |
| 15632 |
} |
| 15633 |
break; |
| 15634 |
} |
| 15635 |
} |
| 15636 |
} |
| 15637 |
} |
| 15638 |
} |
| 15639 |
return _this; |
| 15640 |
} |
| 15641 |
|
| 15642 |
Expression visitCall(CallExp _this) |
| 15643 |
{ |
| 15644 |
if (_this.isLvalue()) |
| 15645 |
return _this; |
| 15646 |
return visit(_this); |
| 15647 |
} |
| 15648 |
|
| 15649 |
Expression visitCast(CastExp _this) |
| 15650 |
{ |
| 15651 |
if (sc && sc.inCfile) |
| 15652 |
{ |
| 15653 |
/* C11 6.5.4-5: A cast does not yield an lvalue. |
| 15654 |
*/ |
| 15655 |
return visit(_this); |
| 15656 |
} |
| 15657 |
if (_this.isLvalue()) |
| 15658 |
{ |
| 15659 |
with (_this) |
| 15660 |
if (!trusted && !e1.type.pointerTo().implicitConvTo(to.pointerTo())) |
| 15661 |
sc.setUnsafePreview(FeatureState.default_, false, loc, |
| 15662 |
"using the result of a cast from `%s` to `%s` as an lvalue", |
| 15663 |
e1.type, to); |
| 15664 |
|
| 15665 |
return _this; |
| 15666 |
} |
| 15667 |
return visit(_this); |
| 15668 |
} |
| 15669 |
|
| 15670 |
Expression visitVectorArray(VectorArrayExp _this) |
| 15671 |
{ |
| 15672 |
_this.e1 = _this.e1.toLvalueImpl(sc, action, e); |
| 15673 |
return _this; |
| 15674 |
} |
| 15675 |
|
| 15676 |
Expression visitSlice(SliceExp _this) |
| 15677 |
{ |
| 15678 |
//printf("SliceExp::toLvalue(%s) _this.type = %s\n", _this.toChars(), _this.type ? _this.type.toChars() : NULL); |
| 15679 |
return (_this.type && _this.type.toBasetype().ty == Tsarray) ? _this : visit(_this); |
| 15680 |
} |
| 15681 |
|
| 15682 |
Expression visitArray(ArrayExp _this) |
| 15683 |
{ |
| 15684 |
if (_this.type && _this.type.toBasetype().ty == Tvoid) |
| 15685 |
error(_this.loc, "`void`s have no value"); |
| 15686 |
return _this; |
| 15687 |
} |
| 15688 |
|
| 15689 |
Expression visitComma(CommaExp _this) |
| 15690 |
{ |
| 15691 |
_this.e2 = _this.e2.toLvalue(sc, action); |
| 15692 |
return _this; |
| 15693 |
} |
| 15694 |
|
| 15695 |
Expression visitDelegatePointer(DelegatePtrExp _this) |
| 15696 |
{ |
| 15697 |
_this.e1 = _this.e1.toLvalueImpl(sc, action, e); |
| 15698 |
return _this; |
| 15699 |
} |
| 15700 |
|
| 15701 |
Expression visitDelegateFuncptr(DelegateFuncptrExp _this) |
| 15702 |
{ |
| 15703 |
_this.e1 = _this.e1.toLvalueImpl(sc, action, e); |
| 15704 |
return _this; |
| 15705 |
} |
| 15706 |
|
| 15707 |
Expression visitIndex(IndexExp _this) |
| 15708 |
{ |
| 15709 |
if (_this.isLvalue()) |
| 15710 |
return _this; |
| 15711 |
return visit(_this); |
| 15712 |
} |
| 15713 |
|
| 15714 |
Expression visitAssign(AssignExp _this) |
| 15715 |
{ |
| 15716 |
if (_this.e1.op == EXP.slice || _this.e1.op == EXP.arrayLength) |
| 15717 |
{ |
| 15718 |
return visit(_this); |
| 15719 |
} |
| 15720 |
|
| 15721 |
/* In front-end level, AssignExp should make an lvalue of e1. |
| 15722 |
* Taking the address of e1 will be handled in low level layer, |
| 15723 |
* so this function does nothing. |
| 15724 |
*/ |
| 15725 |
return _this; |
| 15726 |
} |
| 15727 |
|
| 15728 |
Expression visitCond(CondExp _this) |
| 15729 |
{ |
| 15730 |
// convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) |
| 15731 |
CondExp e = cast(CondExp)(_this.copy()); |
| 15732 |
e.e1 = _this.e1.toLvalue(sc, action).addressOf(); |
| 15733 |
e.e2 = _this.e2.toLvalue(sc, action).addressOf(); |
| 15734 |
e.type = _this.type.pointerTo(); |
| 15735 |
return new PtrExp(_this.loc, e, _this.type); |
| 15736 |
|
| 15737 |
} |
| 15738 |
|
| 15739 |
switch(_this.op) |
| 15740 |
{ |
| 15741 |
default: return visit(_this); |
| 15742 |
|
| 15743 |
case EXP.int64: return visitInteger(_this.isIntegerExp()); |
| 15744 |
case EXP.error: return _this; |
| 15745 |
case EXP.identifier: return _this; |
| 15746 |
case EXP.dSymbol: return _this; |
| 15747 |
case EXP.this_: return visitThis(_this.isThisExp()); |
| 15748 |
case EXP.super_: return visitThis(_this.isSuperExp()); |
| 15749 |
case EXP.string_: return visitString(_this.isStringExp()); |
| 15750 |
case EXP.structLiteral: return visitStructLiteral(_this.isStructLiteralExp()); |
| 15751 |
case EXP.template_: return visitTemplate(_this.isTemplateExp()); |
| 15752 |
case EXP.variable: return visitVar(_this.isVarExp()); |
| 15753 |
case EXP.overloadSet: return _this; |
| 15754 |
case EXP.dotVariable: return visitDotVar(_this.isDotVarExp()); |
| 15755 |
case EXP.call: return visitCall(_this.isCallExp()); |
| 15756 |
case EXP.star: return _this; |
| 15757 |
case EXP.cast_: return visitCast(_this.isCastExp()); |
| 15758 |
case EXP.vectorArray: return visitVectorArray(_this.isVectorArrayExp()); |
| 15759 |
case EXP.slice: return visitSlice(_this.isSliceExp()); |
| 15760 |
case EXP.array: return visitArray(_this.isArrayExp()); |
| 15761 |
case EXP.comma: return visitComma(_this.isCommaExp()); |
| 15762 |
case EXP.delegatePointer: return visitDelegatePointer(_this.isDelegatePtrExp()); |
| 15763 |
case EXP.delegateFunctionPointer: return visitDelegateFuncptr(_this.isDelegateFuncptrExp()); |
| 15764 |
case EXP.index: return visitIndex(_this.isIndexExp()); |
| 15765 |
case EXP.construct: return visitAssign(_this.isConstructExp()); |
| 15766 |
case EXP.loweredAssignExp: return visitAssign(_this.isLoweredAssignExp()); |
| 15767 |
case EXP.blit: return visitAssign(_this.isBlitExp()); |
| 15768 |
case EXP.assign: return visitAssign(_this.isAssignExp()); |
| 15769 |
case EXP.question: return visitCond(_this.isCondExp()); |
| 15770 |
} |
| 15771 |
} |
| 15772 |
|
| 15773 |
/*************************************** |
| 15774 |
* Parameters: |
| 15775 |
* sc: scope |
| 15776 |
* flag: 1: do not issue error message for invalid modification |
| 15777 |
2: the exp is a DotVarExp and a subfield of the leftmost |
| 15778 |
variable is modified |
| 15779 |
* Returns: |
| 15780 |
* Whether the type is modifiable |
| 15781 |
*/ |
| 15782 |
Modifiable checkModifiable(Expression exp, Scope* sc, ModifyFlags flag = ModifyFlags.none) |
| 15783 |
{ |
| 15784 |
switch(exp.op) |
| 15785 |
{ |
| 15786 |
case EXP.variable: |
| 15787 |
auto varExp = cast(VarExp)exp; |
| 15788 |
|
| 15789 |
//printf("VarExp::checkModifiable %s", varExp.toChars()); |
| 15790 |
assert(varExp.type); |
| 15791 |
return varExp.var.checkModify(varExp.loc, sc, null, flag); |
| 15792 |
|
| 15793 |
case EXP.dotVariable: |
| 15794 |
auto dotVarExp = cast(DotVarExp)exp; |
| 15795 |
|
| 15796 |
//printf("DotVarExp::checkModifiable %s %s\n", dotVarExp.toChars(), dotVarExp.type.toChars()); |
| 15797 |
if (dotVarExp.e1.op == EXP.this_) |
| 15798 |
return dotVarExp.var.checkModify(dotVarExp.loc, sc, dotVarExp.e1, flag); |
| 15799 |
|
| 15800 |
/* https://issues.dlang.org/show_bug.cgi?id=12764 |
| 15801 |
* If inside a constructor and an expression of type `this.field.var` |
| 15802 |
* is encountered, where `field` is a struct declaration with |
| 15803 |
* default construction disabled, we must make sure that |
| 15804 |
* assigning to `var` does not imply that `field` was initialized |
| 15805 |
*/ |
| 15806 |
if (sc.func && sc.func.isCtorDeclaration()) |
| 15807 |
{ |
| 15808 |
// if inside a constructor scope and e1 of this DotVarExp |
| 15809 |
// is another DotVarExp, then check if the leftmost expression is a `this` identifier |
| 15810 |
if (auto dve = dotVarExp.e1.isDotVarExp()) |
| 15811 |
{ |
| 15812 |
// Iterate the chain of DotVarExp to find `this` |
| 15813 |
// Keep track whether access to fields was limited to union members |
| 15814 |
// s.t. one can initialize an entire struct inside nested unions |
| 15815 |
// (but not its members) |
| 15816 |
bool onlyUnion = true; |
| 15817 |
while (true) |
| 15818 |
{ |
| 15819 |
auto v = dve.var.isVarDeclaration(); |
| 15820 |
assert(v); |
| 15821 |
|
| 15822 |
// Accessing union member? |
| 15823 |
auto t = v.type.isTypeStruct(); |
| 15824 |
if (!t || !t.sym.isUnionDeclaration()) |
| 15825 |
onlyUnion = false; |
| 15826 |
|
| 15827 |
// Another DotVarExp left? |
| 15828 |
if (!dve.e1 || dve.e1.op != EXP.dotVariable) |
| 15829 |
break; |
| 15830 |
|
| 15831 |
dve = cast(DotVarExp) dve.e1; |
| 15832 |
} |
| 15833 |
|
| 15834 |
if (dve.e1.op == EXP.this_) |
| 15835 |
{ |
| 15836 |
scope v = dve.var.isVarDeclaration(); |
| 15837 |
/* if v is a struct member field with no initializer, no default construction |
| 15838 |
* and v wasn't intialized before |
| 15839 |
*/ |
| 15840 |
if (v && v.isField() && !v._init && !v.ctorinit) |
| 15841 |
{ |
| 15842 |
if (auto ts = v.type.isTypeStruct()) |
| 15843 |
{ |
| 15844 |
if (ts.sym.noDefaultCtor) |
| 15845 |
{ |
| 15846 |
/* checkModify will consider that this is an initialization |
| 15847 |
* of v while it is actually an assignment of a field of v |
| 15848 |
*/ |
| 15849 |
scope modifyLevel = v.checkModify(dotVarExp.loc, sc, dve.e1, !onlyUnion ? (flag | ModifyFlags.fieldAssign) : flag); |
| 15850 |
if (modifyLevel == Modifiable.initialization) |
| 15851 |
{ |
| 15852 |
// https://issues.dlang.org/show_bug.cgi?id=22118 |
| 15853 |
// v is a union type field that was assigned |
| 15854 |
// a variable, therefore it counts as initialization |
| 15855 |
if (v.ctorinit) |
| 15856 |
return Modifiable.initialization; |
| 15857 |
|
| 15858 |
return Modifiable.yes; |
| 15859 |
} |
| 15860 |
return modifyLevel; |
| 15861 |
} |
| 15862 |
} |
| 15863 |
} |
| 15864 |
} |
| 15865 |
} |
| 15866 |
} |
| 15867 |
|
| 15868 |
//printf("\te1 = %s\n", e1.toChars()); |
| 15869 |
return dotVarExp.e1.checkModifiable(sc, flag); |
| 15870 |
|
| 15871 |
case EXP.star: |
| 15872 |
auto ptrExp = cast(PtrExp)exp; |
| 15873 |
if (auto se = ptrExp.e1.isSymOffExp()) |
| 15874 |
{ |
| 15875 |
return se.var.checkModify(ptrExp.loc, sc, null, flag); |
| 15876 |
} |
| 15877 |
else if (auto ae = ptrExp.e1.isAddrExp()) |
| 15878 |
{ |
| 15879 |
return ae.e1.checkModifiable(sc, flag); |
| 15880 |
} |
| 15881 |
return Modifiable.yes; |
| 15882 |
|
| 15883 |
case EXP.slice: |
| 15884 |
auto sliceExp = cast(SliceExp)exp; |
| 15885 |
|
| 15886 |
//printf("SliceExp::checkModifiable %s\n", sliceExp.toChars()); |
| 15887 |
auto e1 = sliceExp.e1; |
| 15888 |
if (e1.type.ty == Tsarray || (e1.op == EXP.index && e1.type.ty != Tarray) || e1.op == EXP.slice) |
| 15889 |
{ |
| 15890 |
return e1.checkModifiable(sc, flag); |
| 15891 |
} |
| 15892 |
return Modifiable.yes; |
| 15893 |
|
| 15894 |
case EXP.comma: |
| 15895 |
return (cast(CommaExp)exp).e2.checkModifiable(sc, flag); |
| 15896 |
|
| 15897 |
case EXP.index: |
| 15898 |
auto indexExp = cast(IndexExp)exp; |
| 15899 |
auto e1 = indexExp.e1; |
| 15900 |
if (e1.type.ty == Tsarray || |
| 15901 |
e1.type.ty == Taarray || |
| 15902 |
(e1.op == EXP.index && e1.type.ty != Tarray) || |
| 15903 |
e1.op == EXP.slice) |
| 15904 |
{ |
| 15905 |
return e1.checkModifiable(sc, flag); |
| 15906 |
} |
| 15907 |
return Modifiable.yes; |
| 15908 |
|
| 15909 |
case EXP.question: |
| 15910 |
auto condExp = cast(CondExp)exp; |
| 15911 |
if (condExp.e1.checkModifiable(sc, flag) != Modifiable.no |
| 15912 |
&& condExp.e2.checkModifiable(sc, flag) != Modifiable.no) |
| 15913 |
return Modifiable.yes; |
| 15914 |
return Modifiable.no; |
| 15915 |
|
| 15916 |
default: |
| 15917 |
return exp.type ? Modifiable.yes : Modifiable.no; // default modifiable |
| 15918 |
} |
| 15919 |
} |
| 15920 |
|
| 15921 |
/** |
| 15922 |
* Similar to `toLvalue`, but also enforce it is mutable or raise an error. |
| 15923 |
* Params: |
| 15924 |
* _this = Expression to convert |
| 15925 |
* sc = scope |
| 15926 |
* Returns: `_this` converted to an lvalue, or an `ErrorExp` |
| 15927 |
*/ |
| 15928 |
Expression modifiableLvalue(Expression _this, Scope* sc) |
| 15929 |
{ |
| 15930 |
return modifiableLvalueImpl(_this, sc, _this); |
| 15931 |
} |
| 15932 |
|
| 15933 |
// e = original / un-lowered expression to print in error messages |
| 15934 |
private Expression modifiableLvalueImpl(Expression _this, Scope* sc, Expression e) |
| 15935 |
{ |
| 15936 |
assert(e); |
| 15937 |
Expression visit(Expression exp) |
| 15938 |
{ |
| 15939 |
//printf("Expression::modifiableLvalue() %s, type = %s\n", exp.toChars(), exp.type.toChars()); |
| 15940 |
// See if this expression is a modifiable lvalue (i.e. not const) |
| 15941 |
if (exp.isBinAssignExp()) |
| 15942 |
return exp.toLvalue(sc, "modify"); |
| 15943 |
|
| 15944 |
auto type = exp.type; |
| 15945 |
if (checkModifiable(exp, sc) == Modifiable.yes) |
| 15946 |
{ |
| 15947 |
assert(type); |
| 15948 |
if (!type.isMutable()) |
| 15949 |
{ |
| 15950 |
if (auto dve = exp.isDotVarExp()) |
| 15951 |
{ |
| 15952 |
if (isNeedThisScope(sc, dve.var)) |
| 15953 |
for (Dsymbol s = sc.func; s; s = s.toParentLocal()) |
| 15954 |
{ |
| 15955 |
FuncDeclaration ff = s.isFuncDeclaration(); |
| 15956 |
if (!ff) |
| 15957 |
break; |
| 15958 |
if (!ff.type.isMutable) |
| 15959 |
{ |
| 15960 |
error(exp.loc, "cannot modify `%s` in `%s` function", exp.toErrMsg(), MODtoChars(type.mod)); |
| 15961 |
return ErrorExp.get(); |
| 15962 |
} |
| 15963 |
} |
| 15964 |
} |
| 15965 |
error(exp.loc, "cannot modify `%s` expression `%s`", MODtoChars(type.mod), exp.toErrMsg()); |
| 15966 |
return ErrorExp.get(); |
| 15967 |
} |
| 15968 |
else if (!type.isAssignable()) |
| 15969 |
{ |
| 15970 |
error(exp.loc, "cannot modify struct instance `%s` of type `%s` because it contains `const` or `immutable` members", |
| 15971 |
exp.toChars(), type.toChars()); |
| 15972 |
return ErrorExp.get(); |
| 15973 |
} |
| 15974 |
} |
| 15975 |
return exp.toLvalueImpl(sc, "modify", e); |
| 15976 |
} |
| 15977 |
|
| 15978 |
Expression visitString(StringExp exp) |
| 15979 |
{ |
| 15980 |
error(exp.loc, "cannot modify string literal `%s`", exp.toErrMsg()); |
| 15981 |
return ErrorExp.get(); |
| 15982 |
} |
| 15983 |
|
| 15984 |
Expression visitVar(VarExp exp) |
| 15985 |
{ |
| 15986 |
//printf("VarExp::modifiableLvalue('%s')\n", exp.var.toChars()); |
| 15987 |
if (exp.var.storage_class & STC.manifest) |
| 15988 |
{ |
| 15989 |
error(exp.loc, "cannot modify manifest constant `%s`", exp.toErrMsg()); |
| 15990 |
return ErrorExp.get(); |
| 15991 |
} |
| 15992 |
// See if this expression is a modifiable lvalue (i.e. not const) |
| 15993 |
return visit(exp); |
| 15994 |
} |
| 15995 |
|
| 15996 |
Expression visitPtr(PtrExp exp) |
| 15997 |
{ |
| 15998 |
//printf("PtrExp::modifiableLvalue() %s, type %s\n", exp.toChars(), exp.type.toChars()); |
| 15999 |
Declaration var; |
| 16000 |
auto e1 = exp.e1; |
| 16001 |
if (auto se = e1.isSymOffExp()) |
| 16002 |
var = se.var; |
| 16003 |
else if (auto ve = e1.isVarExp()) |
| 16004 |
var = ve.var; |
| 16005 |
if (var && var.type.isFunction_Delegate_PtrToFunction()) |
| 16006 |
{ |
| 16007 |
if (var.type.isTypeFunction()) |
| 16008 |
error(exp.loc, "function `%s` is not an lvalue and cannot be modified", var.toChars()); |
| 16009 |
else |
| 16010 |
error(exp.loc, "function pointed to by `%s` is not an lvalue and cannot be modified", var.toChars()); |
| 16011 |
return ErrorExp.get(); |
| 16012 |
} |
| 16013 |
return visit(exp); |
| 16014 |
} |
| 16015 |
|
| 16016 |
Expression visitSlice(SliceExp exp) |
| 16017 |
{ |
| 16018 |
error(exp.loc, "slice expression `%s` is not a modifiable lvalue", exp.toErrMsg()); |
| 16019 |
return exp; |
| 16020 |
} |
| 16021 |
|
| 16022 |
Expression visitComma(CommaExp exp) |
| 16023 |
{ |
| 16024 |
exp.e2 = exp.e2.modifiableLvalueImpl(sc, e); |
| 16025 |
return exp; |
| 16026 |
} |
| 16027 |
|
| 16028 |
Expression visitDelegatePtr(DelegatePtrExp exp) |
| 16029 |
{ |
| 16030 |
if (sc.setUnsafe(false, exp.loc, "modifying delegate pointer `%s`", exp)) |
| 16031 |
{ |
| 16032 |
return ErrorExp.get(); |
| 16033 |
} |
| 16034 |
return visit(exp); |
| 16035 |
} |
| 16036 |
|
| 16037 |
Expression visitDelegateFuncptr(DelegateFuncptrExp exp) |
| 16038 |
{ |
| 16039 |
if (sc.setUnsafe(false, exp.loc, "modifying delegate function pointer `%s`", exp)) |
| 16040 |
{ |
| 16041 |
return ErrorExp.get(); |
| 16042 |
} |
| 16043 |
return visit(exp); |
| 16044 |
} |
| 16045 |
|
| 16046 |
Expression visitIndex(IndexExp exp) |
| 16047 |
{ |
| 16048 |
//printf("IndexExp::modifiableLvalue(%s)\n", exp.toChars()); |
| 16049 |
Expression ex = exp.markSettingAAElem(); |
| 16050 |
if (ex.op == EXP.error) |
| 16051 |
return ex; |
| 16052 |
|
| 16053 |
return visit(exp); |
| 16054 |
} |
| 16055 |
|
| 16056 |
Expression visitCond(CondExp exp) |
| 16057 |
{ |
| 16058 |
if (!exp.e1.isLvalue() && !exp.e2.isLvalue()) |
| 16059 |
{ |
| 16060 |
error(exp.loc, "conditional expression `%s` is not a modifiable lvalue", exp.toErrMsg()); |
| 16061 |
return ErrorExp.get(); |
| 16062 |
} |
| 16063 |
exp.e1 = exp.e1.modifiableLvalue(sc); |
| 16064 |
exp.e2 = exp.e2.modifiableLvalue(sc); |
| 16065 |
return exp.toLvalue(sc, "modify"); |
| 16066 |
} |
| 16067 |
|
| 16068 |
switch(_this.op) |
| 16069 |
{ |
| 16070 |
default: return visit(_this); |
| 16071 |
case EXP.string_: return visitString(_this.isStringExp()); |
| 16072 |
case EXP.variable: return visitVar(_this.isVarExp()); |
| 16073 |
case EXP.star: return visitPtr(_this.isPtrExp()); |
| 16074 |
case EXP.slice: return visitSlice(_this.isSliceExp()); |
| 16075 |
case EXP.comma: return visitComma(_this.isCommaExp()); |
| 16076 |
case EXP.delegatePointer: return visitDelegatePtr(_this.isDelegatePtrExp()); |
| 16077 |
case EXP.delegateFunctionPointer: return visitDelegateFuncptr(_this.isDelegateFuncptrExp()); |
| 16078 |
case EXP.index: return visitIndex(_this.isIndexExp()); |
| 16079 |
case EXP.question: return visitCond(_this.isCondExp()); |
| 16080 |
} |
| 16081 |
} |
| 16082 |
|
| 16083 |
|
| 16084 |
/**************************************************** |
| 16085 |
* Determine if `exp`, which gets its address taken, can do so safely. |
| 16086 |
* Params: |
| 16087 |
* sc = context |
| 16088 |
* exp = expression having its address taken |
| 16089 |
* v = the variable getting its address taken |
| 16090 |
* Returns: |
| 16091 |
* `true` if ok, `false` for error |
| 16092 |
*/ |
| 16093 |
private bool checkAddressVar(Scope* sc, Expression exp, VarDeclaration v) |
| 16094 |
{ |
| 16095 |
//printf("checkAddressVar(exp: %s, v: %s)\n", exp.toChars(), v.toChars()); |
| 16096 |
if (v is null) |
| 16097 |
return true; |
| 16098 |
|
| 16099 |
if (!v.canTakeAddressOf()) |
| 16100 |
{ |
| 16101 |
error(exp.loc, "cannot take address of `%s`", exp.toErrMsg()); |
| 16102 |
return false; |
| 16103 |
} |
| 16104 |
if (sc.func && !sc.intypeof && !v.isDataseg()) |
| 16105 |
{ |
| 16106 |
if (sc.useDIP1000 != FeatureState.enabled && |
| 16107 |
!(v.storage_class & STC.temp) && |
| 16108 |
sc.setUnsafe(false, exp.loc, "taking the address of stack-allocated local variable `%s`", v)) |
| 16109 |
{ |
| 16110 |
return false; |
| 16111 |
} |
| 16112 |
} |
| 16113 |
return true; |
| 16114 |
} |
| 16115 |
|
| 16116 |
/************************************** |
| 16117 |
* This check ensures that the object in `exp` can have its address taken, or |
| 16118 |
* issue a diagnostic error. |
| 16119 |
* Params: |
| 16120 |
* e = expression to check |
| 16121 |
* sc = context |
| 16122 |
* Returns: |
| 16123 |
* true if the expression is addressable |
| 16124 |
*/ |
| 16125 |
bool checkAddressable(Expression e, Scope* sc) |
| 16126 |
{ |
| 16127 |
Expression ex = e; |
| 16128 |
while (true) |
| 16129 |
{ |
| 16130 |
switch (ex.op) |
| 16131 |
{ |
| 16132 |
case EXP.dotVariable: |
| 16133 |
// https://issues.dlang.org/show_bug.cgi?id=22749 |
| 16134 |
// Error about taking address of any bit-field, regardless of |
| 16135 |
// whether SCOPE.Cfile is set. |
| 16136 |
if (auto bf = ex.isDotVarExp().var.isBitFieldDeclaration()) |
| 16137 |
{ |
| 16138 |
error(e.loc, "cannot take address of bit-field `%s`", bf.toChars()); |
| 16139 |
return false; |
| 16140 |
} |
| 16141 |
goto case EXP.cast_; |
| 16142 |
|
| 16143 |
case EXP.index: |
| 16144 |
ex = ex.isBinExp().e1; |
| 16145 |
continue; |
| 16146 |
|
| 16147 |
case EXP.address: |
| 16148 |
case EXP.array: |
| 16149 |
case EXP.cast_: |
| 16150 |
ex = ex.isUnaExp().e1; |
| 16151 |
continue; |
| 16152 |
|
| 16153 |
case EXP.variable: |
| 16154 |
if (sc.inCfile) |
| 16155 |
{ |
| 16156 |
// C11 6.5.3.2: A variable that has its address taken cannot be |
| 16157 |
// stored in a register. |
| 16158 |
// C11 6.3.2.1: An array that has its address computed with `[]` |
| 16159 |
// or cast to an lvalue pointer cannot be stored in a register. |
| 16160 |
if (ex.isVarExp().var.storage_class & STC.register) |
| 16161 |
{ |
| 16162 |
if (e.isIndexExp()) |
| 16163 |
error(e.loc, "cannot index through register variable `%s`", ex.toErrMsg()); |
| 16164 |
else |
| 16165 |
error(e.loc, "cannot take address of register variable `%s`", ex.toErrMsg()); |
| 16166 |
return false; |
| 16167 |
} |
| 16168 |
} |
| 16169 |
break; |
| 16170 |
|
| 16171 |
default: |
| 16172 |
break; |
| 16173 |
} |
| 16174 |
break; |
| 16175 |
} |
| 16176 |
return true; |
| 16177 |
} |
| 16178 |
|
| 16179 |
|
| 16180 |
/******************************* |
| 16181 |
* Checks the attributes of a function. |
| 16182 |
* Purity (`pure`), safety (`@safe`), no GC allocations(`@nogc`) |
| 16183 |
* and usage of `deprecated` and `@disabled`-ed symbols are checked. |
| 16184 |
* |
| 16185 |
* Params: |
| 16186 |
* exp = expression to check attributes for |
| 16187 |
* sc = scope of the function |
| 16188 |
* f = function to be checked |
| 16189 |
* Returns: `true` if error occur. |
| 16190 |
*/ |
| 16191 |
private bool checkFunctionAttributes(Expression exp, Scope* sc, FuncDeclaration f) |
| 16192 |
{ |
| 16193 |
bool error = f.checkDisabled(exp.loc, sc); |
| 16194 |
error |= f.checkDeprecated(exp.loc, sc); |
| 16195 |
error |= f.checkPurity(exp.loc, sc); |
| 16196 |
error |= f.checkSafety(exp.loc, sc); |
| 16197 |
error |= f.checkNogc(exp.loc, sc); |
| 16198 |
return error; |
| 16199 |
} |
| 16200 |
|
| 16201 |
/******************************* |
| 16202 |
* Helper function for `getRightThis()`. |
| 16203 |
* Gets `this` of the next outer aggregate. |
| 16204 |
* Params: |
| 16205 |
* loc = location to use for error messages |
| 16206 |
* sc = context |
| 16207 |
* s = the parent symbol of the existing `this` |
| 16208 |
* ad = struct or class we need the correct `this` for |
| 16209 |
* e1 = existing `this` |
| 16210 |
* t = type of the existing `this` |
| 16211 |
* var = the specific member of ad we're accessing |
| 16212 |
* flag = if true, return `null` instead of throwing an error |
| 16213 |
* Returns: |
| 16214 |
* Expression representing the `this` for the var |
| 16215 |
*/ |
| 16216 |
Expression getThisSkipNestedFuncs(Loc loc, Scope* sc, Dsymbol s, AggregateDeclaration ad, Expression e1, Type t, Dsymbol var, bool flag = false) |
| 16217 |
{ |
| 16218 |
int n = 0; |
| 16219 |
while (s && s.isFuncDeclaration()) |
| 16220 |
{ |
| 16221 |
FuncDeclaration f = s.isFuncDeclaration(); |
| 16222 |
if (f.vthis) |
| 16223 |
{ |
| 16224 |
n++; |
| 16225 |
e1 = new VarExp(loc, f.vthis); |
| 16226 |
if (f.hasDualContext()) |
| 16227 |
{ |
| 16228 |
// (*__this)[i] |
| 16229 |
if (n > 1) |
| 16230 |
e1 = e1.expressionSemantic(sc); |
| 16231 |
e1 = new PtrExp(loc, e1); |
| 16232 |
uint i = f.followInstantiationContext(ad); |
| 16233 |
e1 = new IndexExp(loc, e1, new IntegerExp(i)); |
| 16234 |
s = f.toParentP(ad); |
| 16235 |
continue; |
| 16236 |
} |
| 16237 |
} |
| 16238 |
else |
| 16239 |
{ |
| 16240 |
if (flag) |
| 16241 |
return null; |
| 16242 |
error(e1.loc, "need `this` of type `%s` to access member `%s` from static function `%s`", ad.toChars(), var.toChars(), f.toChars()); |
| 16243 |
e1 = ErrorExp.get(); |
| 16244 |
return e1; |
| 16245 |
} |
| 16246 |
s = s.toParent2(); |
| 16247 |
} |
| 16248 |
if (n > 1 || e1.op == EXP.index) |
| 16249 |
e1 = e1.expressionSemantic(sc); |
| 16250 |
if (s && e1.type.equivalent(Type.tvoidptr)) |
| 16251 |
{ |
| 16252 |
if (auto sad = s.isAggregateDeclaration()) |
| 16253 |
{ |
| 16254 |
Type ta = sad.handleType(); |
| 16255 |
if (ta.ty == Tstruct) |
| 16256 |
ta = ta.pointerTo(); |
| 16257 |
e1.type = ta; |
| 16258 |
} |
| 16259 |
} |
| 16260 |
e1.type = e1.type.addMod(t.mod); |
| 16261 |
return e1; |
| 16262 |
} |
| 16263 |
|
| 16264 |
/******************************* |
| 16265 |
* Make a dual-context container for use as a `this` argument. |
| 16266 |
* Params: |
| 16267 |
* loc = location to use for error messages |
| 16268 |
* sc = current scope |
| 16269 |
* fd = target function that will take the `this` argument |
| 16270 |
* Returns: |
| 16271 |
* Temporary closure variable. |
| 16272 |
* Note: |
| 16273 |
* The function `fd` is added to the nested references of the |
| 16274 |
* newly created variable such that a closure is made for the variable when |
| 16275 |
* the address of `fd` is taken. |
| 16276 |
*/ |
| 16277 |
private VarDeclaration makeThis2Argument(Loc loc, Scope* sc, FuncDeclaration fd) |
| 16278 |
{ |
| 16279 |
Type tthis2 = Type.tvoidptr.sarrayOf(2); |
| 16280 |
VarDeclaration vthis2 = new VarDeclaration(loc, tthis2, Identifier.generateId("__this"), null); |
| 16281 |
vthis2.storage_class |= STC.temp; |
| 16282 |
vthis2.dsymbolSemantic(sc); |
| 16283 |
vthis2.parent = sc.parent; |
| 16284 |
// make it a closure var |
| 16285 |
assert(sc.func); |
| 16286 |
sc.func.closureVars.push(vthis2); |
| 16287 |
// add `fd` to the nested refs |
| 16288 |
vthis2.nestedrefs.push(fd); |
| 16289 |
return vthis2; |
| 16290 |
} |
| 16291 |
|
| 16292 |
/******************************* |
| 16293 |
* Make sure that the runtime hook `id` exists. |
| 16294 |
* Params: |
| 16295 |
* loc = location to use for error messages |
| 16296 |
* sc = current scope |
| 16297 |
* id = the hook identifier |
| 16298 |
* description = what the hook does |
| 16299 |
* module_ = what module the hook is located in |
| 16300 |
* Returns: |
| 16301 |
* a `bool` indicating if the hook is present. |
| 16302 |
*/ |
| 16303 |
bool verifyHookExist(Loc loc, ref Scope sc, Identifier id, string description, Identifier module_ = Id.object) |
| 16304 |
{ |
| 16305 |
Dsymbol pscopesym; |
| 16306 |
auto rootSymbol = sc.search(loc, Id.empty, pscopesym); |
| 16307 |
if (auto moduleSymbol = rootSymbol.search(loc, module_)) |
| 16308 |
if (moduleSymbol.search(loc, id)) |
| 16309 |
return true; |
| 16310 |
error(loc, "`%s.%s` not found. The current runtime does not support %.*s, or the runtime is corrupt.", module_.toChars(), id.toChars(), cast(int)description.length, description.ptr); |
| 16311 |
return false; |
| 16312 |
} |
| 16313 |
|
| 16314 |
/*************************************** |
| 16315 |
* Fit elements[] to the corresponding types of the `sd`'s fields. |
| 16316 |
* |
| 16317 |
* Params: |
| 16318 |
* sd = the struct declaration |
| 16319 |
* loc = location to use for error messages |
| 16320 |
* sc = context |
| 16321 |
* elements = explicit arguments used to construct object |
| 16322 |
* stype = the constructed object type. |
| 16323 |
* Returns: |
| 16324 |
* false if any errors occur, |
| 16325 |
* otherwise true and elements[] are rewritten for the output. |
| 16326 |
*/ |
| 16327 |
private bool fit(StructDeclaration sd, Loc loc, Scope* sc, Expressions* elements, Type stype) |
| 16328 |
{ |
| 16329 |
if (!elements) |
| 16330 |
return true; |
| 16331 |
|
| 16332 |
const nfields = sd.nonHiddenFields(); |
| 16333 |
size_t offset = 0; |
| 16334 |
for (size_t i = 0; i < elements.length; i++) |
| 16335 |
{ |
| 16336 |
Expression e = (*elements)[i]; |
| 16337 |
if (!e) |
| 16338 |
continue; |
| 16339 |
|
| 16340 |
e = resolveProperties(sc, e); |
| 16341 |
if (i >= nfields) |
| 16342 |
{ |
| 16343 |
if (i < sd.fields.length && e.op == EXP.null_) |
| 16344 |
{ |
| 16345 |
// CTFE sometimes creates null as hidden pointer; we'll allow this. |
| 16346 |
continue; |
| 16347 |
} |
| 16348 |
.error(loc, "more initializers than fields (%llu) of `%s`", cast(ulong)nfields, sd.toChars()); |
| 16349 |
return false; |
| 16350 |
} |
| 16351 |
VarDeclaration v = sd.fields[i]; |
| 16352 |
if (v.offset < offset) |
| 16353 |
{ |
| 16354 |
.error(loc, "overlapping initialization for `%s`", v.toChars()); |
| 16355 |
if (!sd.isUnionDeclaration()) |
| 16356 |
{ |
| 16357 |
enum errorMsg = "`struct` initializers that contain anonymous unions" ~ |
| 16358 |
" must initialize only the first member of a `union`. All subsequent" ~ |
| 16359 |
" non-overlapping fields are default initialized"; |
| 16360 |
.errorSupplemental(loc, errorMsg); |
| 16361 |
} |
| 16362 |
return false; |
| 16363 |
} |
| 16364 |
const vsize = v.type.size(); |
| 16365 |
if (vsize == SIZE_INVALID) |
| 16366 |
return false; |
| 16367 |
offset = cast(uint)(v.offset + vsize); |
| 16368 |
|
| 16369 |
Type t = v.type; |
| 16370 |
if (stype) |
| 16371 |
t = t.addMod(stype.mod); |
| 16372 |
Type origType = t; |
| 16373 |
Type tb = t.toBasetype(); |
| 16374 |
|
| 16375 |
const hasPointers = tb.hasPointers(); |
| 16376 |
if (hasPointers) |
| 16377 |
{ |
| 16378 |
if ((!stype.alignment.isDefault() && stype.alignment.get() < target.ptrsize || |
| 16379 |
(v.offset & (target.ptrsize - 1))) && |
| 16380 |
(sc.setUnsafe(false, loc, |
| 16381 |
"field `%s.%s` assigning to misaligned pointers", sd, v))) |
| 16382 |
{ |
| 16383 |
return false; |
| 16384 |
} |
| 16385 |
} |
| 16386 |
|
| 16387 |
/* Look for case of initializing a static array with a too-short |
| 16388 |
* string literal, such as: |
| 16389 |
* char[5] foo = "abc"; |
| 16390 |
* Allow this by doing an explicit cast, which will lengthen the string |
| 16391 |
* literal. |
| 16392 |
*/ |
| 16393 |
if (e.op == EXP.string_ && tb.ty == Tsarray) |
| 16394 |
{ |
| 16395 |
StringExp se = cast(StringExp)e; |
| 16396 |
Type typeb = se.type.toBasetype(); |
| 16397 |
TY tynto = tb.nextOf().ty; |
| 16398 |
if (!se.committed && |
| 16399 |
typeb.isStaticOrDynamicArray() && tynto.isSomeChar && |
| 16400 |
se.numberOfCodeUnits(tynto) < (cast(TypeSArray)tb).dim.toInteger()) |
| 16401 |
{ |
| 16402 |
e = se.castTo(sc, t); |
| 16403 |
goto L1; |
| 16404 |
} |
| 16405 |
} |
| 16406 |
|
| 16407 |
while (!e.implicitConvTo(t) && tb.ty == Tsarray) |
| 16408 |
{ |
| 16409 |
/* Static array initialization, as in: |
| 16410 |
* T[3][5] = e; |
| 16411 |
*/ |
| 16412 |
t = tb.nextOf(); |
| 16413 |
tb = t.toBasetype(); |
| 16414 |
} |
| 16415 |
if (!e.implicitConvTo(t)) |
| 16416 |
t = origType; // restore type for better diagnostic |
| 16417 |
|
| 16418 |
e = e.implicitCastTo(sc, t); |
| 16419 |
L1: |
| 16420 |
if (e.op == EXP.error) |
| 16421 |
return false; |
| 16422 |
|
| 16423 |
(*elements)[i] = doCopyOrMove(sc, e, null, false); |
| 16424 |
} |
| 16425 |
return true; |
| 16426 |
} |
| 16427 |
|
| 16428 |
|
| 16429 |
/** |
| 16430 |
* Returns `em` as a VariableExp |
| 16431 |
* Params: |
| 16432 |
* em = the EnumMember to wrap |
| 16433 |
* loc = location of use of em |
| 16434 |
* sc = scope of use of em |
| 16435 |
* Returns: |
| 16436 |
* VarExp referenceing `em` or ErrorExp if `em` if disabled/deprecated |
| 16437 |
*/ |
| 16438 |
Expression getVarExp(EnumMember em, Loc loc, Scope* sc) |
| 16439 |
{ |
| 16440 |
dsymbolSemantic(em, sc); |
| 16441 |
if (em.errors) |
| 16442 |
return ErrorExp.get(); |
| 16443 |
em.checkDisabled(loc, sc); |
| 16444 |
|
| 16445 |
if (em.depdecl && !em.depdecl._scope) |
| 16446 |
{ |
| 16447 |
em.depdecl._scope = sc; |
| 16448 |
em.depdecl._scope.setNoFree(); |
| 16449 |
} |
| 16450 |
em.checkDeprecated(loc, sc); |
| 16451 |
|
| 16452 |
if (em.errors) |
| 16453 |
return ErrorExp.get(); |
| 16454 |
Expression e = new VarExp(loc, em); |
| 16455 |
e = e.expressionSemantic(sc); |
| 16456 |
if (!sc.inCfile && em.isCsymbol()) |
| 16457 |
{ |
| 16458 |
/* C11 types them as int. But if in D file, |
| 16459 |
* type qualified names as the enum |
| 16460 |
*/ |
| 16461 |
e.type = em.parent.isEnumDeclaration().type; |
| 16462 |
assert(e.type); |
| 16463 |
} |
| 16464 |
return e; |
| 16465 |
} |
| 16466 |
|
| 16467 |
|
| 16468 |
/***************************** |
| 16469 |
* Try to treat `exp` as a boolean, |
| 16470 |
* Params: |
| 16471 |
* exp = the expression |
| 16472 |
* sc = scope to evalute `exp` in |
| 16473 |
* Returns: |
| 16474 |
* Modified expression on success, ErrorExp on error |
| 16475 |
*/ |
| 16476 |
Expression toBoolean(Expression exp, Scope* sc) |
| 16477 |
{ |
| 16478 |
switch(exp.op) |
| 16479 |
{ |
| 16480 |
case EXP.delete_: |
| 16481 |
error(exp.loc, "`delete` does not give a boolean result"); |
| 16482 |
return ErrorExp.get(); |
| 16483 |
|
| 16484 |
case EXP.comma: |
| 16485 |
auto ce = exp.isCommaExp(); |
| 16486 |
auto ex2 = ce.e2.toBoolean(sc); |
| 16487 |
if (ex2.op == EXP.error) |
| 16488 |
return ex2; |
| 16489 |
ce.e2 = ex2; |
| 16490 |
ce.type = ce.e2.type; |
| 16491 |
return ce; |
| 16492 |
|
| 16493 |
case EXP.assign: |
| 16494 |
case EXP.construct: |
| 16495 |
case EXP.blit: |
| 16496 |
case EXP.loweredAssignExp: |
| 16497 |
if (sc.inCfile) |
| 16498 |
return exp; |
| 16499 |
// Things like: |
| 16500 |
// if (a = b) ... |
| 16501 |
// are usually mistakes. |
| 16502 |
error(exp.loc, "assignment cannot be used as a condition, perhaps `==` was meant?"); |
| 16503 |
return ErrorExp.get(); |
| 16504 |
|
| 16505 |
//LogicalExp |
| 16506 |
case EXP.andAnd: |
| 16507 |
case EXP.orOr: |
| 16508 |
auto le = exp.isLogicalExp(); |
| 16509 |
auto ex2 = le.e2.toBoolean(sc); |
| 16510 |
if (ex2.op == EXP.error) |
| 16511 |
return ex2; |
| 16512 |
le.e2 = ex2; |
| 16513 |
return le; |
| 16514 |
|
| 16515 |
case EXP.question: |
| 16516 |
auto ce = exp.isCondExp(); |
| 16517 |
auto ex1 = ce.e1.toBoolean(sc); |
| 16518 |
auto ex2 = ce.e2.toBoolean(sc); |
| 16519 |
if (ex1.op == EXP.error) |
| 16520 |
return ex1; |
| 16521 |
if (ex2.op == EXP.error) |
| 16522 |
return ex2; |
| 16523 |
ce.e1 = ex1; |
| 16524 |
ce.e2 = ex2; |
| 16525 |
return ce; |
| 16526 |
|
| 16527 |
|
| 16528 |
default: |
| 16529 |
// Default is 'yes' - do nothing |
| 16530 |
Expression e = arrayFuncConv(exp, sc); |
| 16531 |
Type t = e.type; |
| 16532 |
Type tb = t.toBasetype(); |
| 16533 |
Type att = null; |
| 16534 |
|
| 16535 |
while (1) |
| 16536 |
{ |
| 16537 |
// Structs can be converted to bool using opCast(bool)() |
| 16538 |
if (auto ts = tb.isTypeStruct()) |
| 16539 |
{ |
| 16540 |
AggregateDeclaration ad = ts.sym; |
| 16541 |
/* Don't really need to check for opCast first, but by doing so we |
| 16542 |
* get better error messages if it isn't there. |
| 16543 |
*/ |
| 16544 |
if (Dsymbol fd = search_function(ad, Id.opCast)) |
| 16545 |
{ |
| 16546 |
e = new CastExp(exp.loc, e, Type.tbool); |
| 16547 |
e = e.expressionSemantic(sc); |
| 16548 |
return e; |
| 16549 |
} |
| 16550 |
|
| 16551 |
// Forward to aliasthis. |
| 16552 |
if (ad.aliasthis && !isRecursiveAliasThis(att, tb)) |
| 16553 |
{ |
| 16554 |
e = resolveAliasThis(sc, e); |
| 16555 |
t = e.type; |
| 16556 |
tb = e.type.toBasetype(); |
| 16557 |
continue; |
| 16558 |
} |
| 16559 |
} |
| 16560 |
break; |
| 16561 |
} |
| 16562 |
|
| 16563 |
if (!t.isBoolean()) |
| 16564 |
{ |
| 16565 |
if (tb != Type.terror) |
| 16566 |
error(exp.loc, "expression `%s` of type `%s` does not have a boolean value", |
| 16567 |
exp.toChars(), t.toChars()); |
| 16568 |
return ErrorExp.get(); |
| 16569 |
} |
| 16570 |
return e; |
| 16571 |
} |
| 16572 |
} |
| 16573 |
|
| 16574 |
/******************************************** |
| 16575 |
* Semantically analyze and then evaluate a static condition at compile time. |
| 16576 |
* This is special because short circuit operators &&, || and ?: at the top |
| 16577 |
* level are not semantically analyzed if the result of the expression is not |
| 16578 |
* necessary. |
| 16579 |
* Params: |
| 16580 |
* sc = instantiating scope |
| 16581 |
* original = original expression, for error messages |
| 16582 |
* e = resulting expression |
| 16583 |
* errors = set to `true` if errors occurred |
| 16584 |
* negatives = array to store negative clauses |
| 16585 |
* Returns: |
| 16586 |
* true if evaluates to true |
| 16587 |
*/ |
| 16588 |
bool evalStaticCondition(Scope* sc, Expression original, Expression e, out bool errors, Expressions* negatives = null) |
| 16589 |
{ |
| 16590 |
if (negatives) |
| 16591 |
negatives.setDim(0); |
| 16592 |
|
| 16593 |
bool impl(Expression e) |
| 16594 |
{ |
| 16595 |
if (e.isNotExp()) |
| 16596 |
{ |
| 16597 |
NotExp ne = cast(NotExp)e; |
| 16598 |
return !impl(ne.e1); |
| 16599 |
} |
| 16600 |
|
| 16601 |
if (e.op == EXP.andAnd || e.op == EXP.orOr) |
| 16602 |
{ |
| 16603 |
LogicalExp aae = cast(LogicalExp)e; |
| 16604 |
bool result = impl(aae.e1); |
| 16605 |
if (errors) |
| 16606 |
return false; |
| 16607 |
if (e.op == EXP.andAnd) |
| 16608 |
{ |
| 16609 |
if (!result) |
| 16610 |
return false; |
| 16611 |
} |
| 16612 |
else |
| 16613 |
{ |
| 16614 |
if (result) |
| 16615 |
return true; |
| 16616 |
} |
| 16617 |
result = impl(aae.e2); |
| 16618 |
return !errors && result; |
| 16619 |
} |
| 16620 |
|
| 16621 |
if (e.op == EXP.question) |
| 16622 |
{ |
| 16623 |
CondExp ce = cast(CondExp)e; |
| 16624 |
bool result = impl(ce.econd); |
| 16625 |
if (errors) |
| 16626 |
return false; |
| 16627 |
Expression leg = result ? ce.e1 : ce.e2; |
| 16628 |
result = impl(leg); |
| 16629 |
return !errors && result; |
| 16630 |
} |
| 16631 |
|
| 16632 |
Expression before = e; |
| 16633 |
const uint nerrors = global.errors; |
| 16634 |
|
| 16635 |
sc = sc.startCTFE(); |
| 16636 |
sc.condition = true; |
| 16637 |
|
| 16638 |
e = e.expressionSemantic(sc); |
| 16639 |
e = resolveProperties(sc, e); |
| 16640 |
e = e.toBoolean(sc); |
| 16641 |
|
| 16642 |
sc = sc.endCTFE(); |
| 16643 |
e = e.optimize(WANTvalue); |
| 16644 |
|
| 16645 |
if (nerrors != global.errors || |
| 16646 |
e.isErrorExp() || |
| 16647 |
e.type.toBasetype() == Type.terror) |
| 16648 |
{ |
| 16649 |
errors = true; |
| 16650 |
return false; |
| 16651 |
} |
| 16652 |
|
| 16653 |
e = e.ctfeInterpret(); |
| 16654 |
|
| 16655 |
const opt = e.toBool(); |
| 16656 |
if (opt.isEmpty()) |
| 16657 |
{ |
| 16658 |
if (!e.type.isTypeError()) |
| 16659 |
error(e.loc, "expression `%s` is not constant", e.toErrMsg()); |
| 16660 |
errors = true; |
| 16661 |
return false; |
| 16662 |
} |
| 16663 |
|
| 16664 |
if (negatives && !opt.get()) |
| 16665 |
negatives.push(before); |
| 16666 |
return opt.get(); |
| 16667 |
} |
| 16668 |
return impl(e); |
| 16669 |
} |
| 16670 |
|
| 16671 |
/************************************ |
| 16672 |
* Check to see the aggregate type is nested and its context pointer is |
| 16673 |
* accessible from the current scope. |
| 16674 |
* Returns true if error occurs. |
| 16675 |
*/ |
| 16676 |
bool checkFrameAccess(Loc loc, Scope* sc, AggregateDeclaration ad, size_t iStart = 0) |
| 16677 |
{ |
| 16678 |
Dsymbol sparent = ad.toParentLocal(); |
| 16679 |
Dsymbol sparent2 = ad.toParent2(); |
| 16680 |
Dsymbol s = sc.func; |
| 16681 |
if (ad.isNested() && s) |
| 16682 |
{ |
| 16683 |
//printf("ad = %p %s [%s], parent:%p\n", ad, ad.toChars(), ad.loc.toChars(), ad.parent); |
| 16684 |
//printf("sparent = %p %s [%s], parent: %s\n", sparent, sparent.toChars(), sparent.loc.toChars(), sparent.parent,toChars()); |
| 16685 |
//printf("sparent2 = %p %s [%s], parent: %s\n", sparent2, sparent2.toChars(), sparent2.loc.toChars(), sparent2.parent,toChars()); |
| 16686 |
if (!ensureStaticLinkTo(s, sparent) || sparent != sparent2 && !ensureStaticLinkTo(s, sparent2)) |
| 16687 |
{ |
| 16688 |
error(loc, "cannot access frame pointer of `%s`", ad.toPrettyChars()); |
| 16689 |
return true; |
| 16690 |
} |
| 16691 |
} |
| 16692 |
|
| 16693 |
bool result = false; |
| 16694 |
for (size_t i = iStart; i < ad.fields.length; i++) |
| 16695 |
{ |
| 16696 |
VarDeclaration vd = ad.fields[i]; |
| 16697 |
Type tb = vd.type.baseElemOf(); |
| 16698 |
if (tb.ty == Tstruct) |
| 16699 |
{ |
| 16700 |
result |= checkFrameAccess(loc, sc, (cast(TypeStruct)tb).sym); |
| 16701 |
} |
| 16702 |
} |
| 16703 |
return result; |
| 16704 |
} |
| 16705 |
|
| 16706 |
/// Return value for `checkModifiable` |
| 16707 |
enum Modifiable |
| 16708 |
{ |
| 16709 |
/// Not modifiable |
| 16710 |
no, |
| 16711 |
/// Modifiable (the type is mutable) |
| 16712 |
yes, |
| 16713 |
/// Modifiable because it is initialization |
| 16714 |
initialization, |
| 16715 |
} |
| 16716 |
|
| 16717 |
/** |
| 16718 |
* Specifies how the checkModify deals with certain situations |
| 16719 |
*/ |
| 16720 |
enum ModifyFlags |
| 16721 |
{ |
| 16722 |
/// Issue error messages on invalid modifications of the variable |
| 16723 |
none, |
| 16724 |
/// No errors are emitted for invalid modifications |
| 16725 |
noError = 0x1, |
| 16726 |
/// The modification occurs for a subfield of the current variable |
| 16727 |
fieldAssign = 0x2, |
| 16728 |
} |
| 16729 |
|
| 16730 |
/************************************* |
| 16731 |
* Check to see if declaration can be modified in this context (sc). |
| 16732 |
* Issue error if not. |
| 16733 |
* Params: |
| 16734 |
* loc = location for error messages |
| 16735 |
* e1 = `null` or `this` expression when this declaration is a field |
| 16736 |
* sc = context |
| 16737 |
* flag = if the first bit is set it means do not issue error message for |
| 16738 |
* invalid modification; if the second bit is set, it means that |
| 16739 |
this declaration is a field and a subfield of it is modified. |
| 16740 |
* Returns: |
| 16741 |
* Modifiable.yes or Modifiable.initialization |
| 16742 |
*/ |
| 16743 |
private Modifiable checkModify(Declaration d, Loc loc, Scope* sc, Expression e1, ModifyFlags flag) |
| 16744 |
{ |
| 16745 |
VarDeclaration v = d.isVarDeclaration(); |
| 16746 |
if (v && v.canassign) |
| 16747 |
return Modifiable.initialization; |
| 16748 |
|
| 16749 |
if (d.isParameter() || d.isResult()) |
| 16750 |
{ |
| 16751 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 16752 |
{ |
| 16753 |
if (scx.func == d.parent && scx.contract != Contract.none) |
| 16754 |
{ |
| 16755 |
const(char)* s = d.isParameter() && d.parent.ident != Id.ensure ? "parameter" : "result"; |
| 16756 |
if (!(flag & ModifyFlags.noError)) |
| 16757 |
error(loc, "%s `%s` cannot modify %s `%s` in contract", d.kind, d.toPrettyChars, s, d.toChars()); |
| 16758 |
return Modifiable.initialization; // do not report type related errors |
| 16759 |
} |
| 16760 |
} |
| 16761 |
} |
| 16762 |
|
| 16763 |
if (e1 && e1.op == EXP.this_ && d.isField()) |
| 16764 |
{ |
| 16765 |
VarDeclaration vthis = e1.isThisExp().var; |
| 16766 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 16767 |
{ |
| 16768 |
if (scx.func == vthis.parent && scx.contract != Contract.none) |
| 16769 |
{ |
| 16770 |
if (!(flag & ModifyFlags.noError)) |
| 16771 |
error(loc, "cannot modify member variable `%s` in contract", d.toPrettyChars()); |
| 16772 |
return Modifiable.initialization; // do not report type related errors |
| 16773 |
} |
| 16774 |
} |
| 16775 |
} |
| 16776 |
|
| 16777 |
if (v && (v.isCtorinit() || d.isField())) |
| 16778 |
{ |
| 16779 |
// It's only modifiable if inside the right constructor |
| 16780 |
if ((d.storage_class & (STC.foreach_ | STC.ref_)) == (STC.foreach_ | STC.ref_)) |
| 16781 |
return Modifiable.initialization; |
| 16782 |
if (flag & ModifyFlags.fieldAssign) |
| 16783 |
return Modifiable.yes; |
| 16784 |
return modifyFieldVar(loc, sc, v, e1) ? Modifiable.initialization : Modifiable.yes; |
| 16785 |
} |
| 16786 |
return Modifiable.yes; |
| 16787 |
} |
| 16788 |
|
| 16789 |
/*********************************************** |
| 16790 |
* Mark variable v as modified if it is inside a constructor that var |
| 16791 |
* is a field in. |
| 16792 |
* Also used to allow immutable globals to be initialized inside a static constructor. |
| 16793 |
* Returns: |
| 16794 |
* true if it's an initialization of v |
| 16795 |
*/ |
| 16796 |
private bool modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1) |
| 16797 |
{ |
| 16798 |
//printf("modifyFieldVar(var = %s)\n", var.toChars()); |
| 16799 |
Dsymbol s = sc.func; |
| 16800 |
while (1) |
| 16801 |
{ |
| 16802 |
FuncDeclaration fd = null; |
| 16803 |
if (s) |
| 16804 |
fd = s.isFuncDeclaration(); |
| 16805 |
if (fd && |
| 16806 |
((fd.isCtorDeclaration() && var.isField()) || |
| 16807 |
((fd.isStaticCtorDeclaration() || fd.isCrtCtor) && !var.isField())) && |
| 16808 |
fd.toParentDecl() == var.toParent2() && |
| 16809 |
(!e1 || e1.op == EXP.this_)) |
| 16810 |
{ |
| 16811 |
bool result = true; |
| 16812 |
|
| 16813 |
var.ctorinit = true; |
| 16814 |
//printf("setting ctorinit\n"); |
| 16815 |
|
| 16816 |
if (var.isField() && sc.ctorflow.fieldinit.length && !sc.intypeof) |
| 16817 |
{ |
| 16818 |
assert(e1); |
| 16819 |
auto mustInit = ((var.storage_class & STC.nodefaultctor) != 0 || |
| 16820 |
var.type.needsNested()); |
| 16821 |
|
| 16822 |
const dim = sc.ctorflow.fieldinit.length; |
| 16823 |
auto ad = fd.isMemberDecl(); |
| 16824 |
assert(ad); |
| 16825 |
size_t i; |
| 16826 |
for (i = 0; i < dim; i++) // same as findFieldIndexByName in ctfeexp.c ? |
| 16827 |
{ |
| 16828 |
if (ad.fields[i] == var) |
| 16829 |
break; |
| 16830 |
} |
| 16831 |
assert(i < dim); |
| 16832 |
auto fieldInit = &sc.ctorflow.fieldinit[i]; |
| 16833 |
const fi = fieldInit.csx; |
| 16834 |
|
| 16835 |
if (fi & CSX.this_ctor) |
| 16836 |
{ |
| 16837 |
if (var.type.isMutable() && e1.type.isMutable()) |
| 16838 |
result = false; |
| 16839 |
else |
| 16840 |
{ |
| 16841 |
const(char)* modStr = !var.type.isMutable() ? MODtoChars(var.type.mod) : MODtoChars(e1.type.mod); |
| 16842 |
.error(loc, "%s field `%s` initialized multiple times", modStr, var.toChars()); |
| 16843 |
.errorSupplemental(fieldInit.loc, "Previous initialization is here."); |
| 16844 |
} |
| 16845 |
} |
| 16846 |
else if (sc.inLoop || (fi & CSX.label)) |
| 16847 |
{ |
| 16848 |
if (!mustInit && var.type.isMutable() && e1.type.isMutable()) |
| 16849 |
result = false; |
| 16850 |
else |
| 16851 |
{ |
| 16852 |
const(char)* modStr = !var.type.isMutable() ? MODtoChars(var.type.mod) : MODtoChars(e1.type.mod); |
| 16853 |
.error(loc, "%s field `%s` initialization is not allowed in loops or after labels", modStr, var.toChars()); |
| 16854 |
} |
| 16855 |
} |
| 16856 |
|
| 16857 |
fieldInit.csx |= CSX.this_ctor; |
| 16858 |
fieldInit.loc = e1.loc; |
| 16859 |
if (var.overlapped) // https://issues.dlang.org/show_bug.cgi?id=15258 |
| 16860 |
{ |
| 16861 |
foreach (j, v; ad.fields) |
| 16862 |
{ |
| 16863 |
if (v is var || !var.isOverlappedWith(v)) |
| 16864 |
continue; |
| 16865 |
v.ctorinit = true; |
| 16866 |
sc.ctorflow.fieldinit[j].csx = CSX.this_ctor; |
| 16867 |
} |
| 16868 |
} |
| 16869 |
} |
| 16870 |
else if (fd != sc.func) |
| 16871 |
{ |
| 16872 |
if (var.type.isMutable()) |
| 16873 |
result = false; |
| 16874 |
else if (sc.func.fes) |
| 16875 |
{ |
| 16876 |
const(char)* p = var.isField() ? "field" : var.kind(); |
| 16877 |
.error(loc, "%s %s `%s` initialization is not allowed in foreach loop", |
| 16878 |
MODtoChars(var.type.mod), p, var.toChars()); |
| 16879 |
} |
| 16880 |
else |
| 16881 |
{ |
| 16882 |
const(char)* p = var.isField() ? "field" : var.kind(); |
| 16883 |
.error(loc, "%s %s `%s` initialization is not allowed in nested function `%s`", |
| 16884 |
MODtoChars(var.type.mod), p, var.toChars(), sc.func.toChars()); |
| 16885 |
} |
| 16886 |
} |
| 16887 |
else if (fd.isStaticCtorDeclaration() && !fd.isSharedStaticCtorDeclaration() && |
| 16888 |
var.type.isImmutable()) |
| 16889 |
{ |
| 16890 |
.error(loc, "%s %s `%s` initialization is not allowed in `static this`", |
| 16891 |
MODtoChars(var.type.mod), var.kind(), var.toChars()); |
| 16892 |
errorSupplemental(loc, "Use `shared static this` instead."); |
| 16893 |
} |
| 16894 |
else if (fd.isStaticCtorDeclaration() && !fd.isSharedStaticCtorDeclaration() && |
| 16895 |
var.type.isConst()) |
| 16896 |
{ |
| 16897 |
// @@@DEPRECATED_2.116@@@ |
| 16898 |
// Turn this into an error, merging with the branch above |
| 16899 |
.deprecation(loc, "%s %s `%s` initialization is not allowed in `static this`", |
| 16900 |
MODtoChars(var.type.mod), var.kind(), var.toChars()); |
| 16901 |
deprecationSupplemental(loc, "Use `shared static this` instead."); |
| 16902 |
} |
| 16903 |
return result; |
| 16904 |
} |
| 16905 |
else |
| 16906 |
{ |
| 16907 |
if (s) |
| 16908 |
{ |
| 16909 |
s = s.toParentP(var.toParent2()); |
| 16910 |
continue; |
| 16911 |
} |
| 16912 |
} |
| 16913 |
break; |
| 16914 |
} |
| 16915 |
return false; |
| 16916 |
} |
| 16917 |
|
| 16918 |
/*************************************** |
| 16919 |
* Request additional semantic analysis for TypeInfo generation. |
| 16920 |
* Params: |
| 16921 |
* sc = context |
| 16922 |
* t = type that TypeInfo is being generated for |
| 16923 |
*/ |
| 16924 |
void semanticTypeInfo(Scope* sc, Type t) |
| 16925 |
{ |
| 16926 |
if (sc) |
| 16927 |
{ |
| 16928 |
if (sc.intypeof) |
| 16929 |
return; |
| 16930 |
if (!sc.needsCodegen()) |
| 16931 |
return; |
| 16932 |
} |
| 16933 |
|
| 16934 |
if (!t) |
| 16935 |
return; |
| 16936 |
|
| 16937 |
void visitVector(TypeVector t) |
| 16938 |
{ |
| 16939 |
semanticTypeInfo(sc, t.basetype); |
| 16940 |
} |
| 16941 |
|
| 16942 |
void visitAArray(TypeAArray t) |
| 16943 |
{ |
| 16944 |
semanticTypeInfo(sc, t.index); |
| 16945 |
semanticTypeInfo(sc, t.next); |
| 16946 |
|
| 16947 |
if (global.params.useTypeInfo) |
| 16948 |
getTypeInfoType(t.loc, t, sc); |
| 16949 |
} |
| 16950 |
|
| 16951 |
void visitStruct(TypeStruct t) |
| 16952 |
{ |
| 16953 |
//printf("semanticTypeInfo.visit(TypeStruct = %s)\n", t.toChars()); |
| 16954 |
StructDeclaration sd = t.sym; |
| 16955 |
|
| 16956 |
/* Step 1: create TypeInfoDeclaration |
| 16957 |
*/ |
| 16958 |
if (!sc) // inline may request TypeInfo. |
| 16959 |
{ |
| 16960 |
Scope scx; |
| 16961 |
scx.eSink = global.errorSink; |
| 16962 |
scx._module = sd.getModule(); |
| 16963 |
if (global.params.useTypeInfo) |
| 16964 |
{ |
| 16965 |
getTypeInfoType(sd.loc, t, &scx); |
| 16966 |
sd.requestTypeInfo = true; |
| 16967 |
} |
| 16968 |
} |
| 16969 |
else if (!sc.minst) |
| 16970 |
{ |
| 16971 |
// don't yet have to generate TypeInfo instance if |
| 16972 |
// the typeid(T) expression exists in speculative scope. |
| 16973 |
} |
| 16974 |
else |
| 16975 |
{ |
| 16976 |
getTypeInfoType(sd.loc, t, sc); |
| 16977 |
sd.requestTypeInfo = true; |
| 16978 |
|
| 16979 |
// https://issues.dlang.org/show_bug.cgi?id=15149 |
| 16980 |
// if the typeid operand type comes from a |
| 16981 |
// result of auto function, it may be yet speculative. |
| 16982 |
// unSpeculative(sc, sd); |
| 16983 |
} |
| 16984 |
|
| 16985 |
/* Step 2: If the TypeInfo generation requires sd.semantic3, run it later. |
| 16986 |
* This should be done even if typeid(T) exists in speculative scope. |
| 16987 |
* Because it may appear later in non-speculative scope. |
| 16988 |
*/ |
| 16989 |
if (!sd.members) |
| 16990 |
return; // opaque struct |
| 16991 |
if (!sd.xeq && !sd.xcmp && !sd.postblit && !sd.tidtor && !sd.xhash && !search_toString(sd)) |
| 16992 |
return; // none of TypeInfo-specific members |
| 16993 |
|
| 16994 |
// If the struct is in a non-root module, run semantic3 to get |
| 16995 |
// correct symbols for the member function. |
| 16996 |
if (sd.semanticRun >= PASS.semantic3) |
| 16997 |
{ |
| 16998 |
// semantic3 is already done |
| 16999 |
} |
| 17000 |
else if (TemplateInstance ti = sd.isInstantiated()) |
| 17001 |
{ |
| 17002 |
if (ti.minst && !ti.minst.isRoot()) |
| 17003 |
Module.addDeferredSemantic3(sd); |
| 17004 |
} |
| 17005 |
else |
| 17006 |
{ |
| 17007 |
if (sd.inNonRoot()) |
| 17008 |
{ |
| 17009 |
//printf("deferred sem3 for TypeInfo - sd = %s, inNonRoot = %d\n", sd.toChars(), sd.inNonRoot()); |
| 17010 |
Module.addDeferredSemantic3(sd); |
| 17011 |
} |
| 17012 |
} |
| 17013 |
} |
| 17014 |
|
| 17015 |
void visitTuple(TypeTuple t) |
| 17016 |
{ |
| 17017 |
if (t.arguments) |
| 17018 |
{ |
| 17019 |
foreach (arg; *t.arguments) |
| 17020 |
{ |
| 17021 |
semanticTypeInfo(sc, arg.type); |
| 17022 |
} |
| 17023 |
} |
| 17024 |
} |
| 17025 |
|
| 17026 |
/* Note structural similarity of this Type walker to that in isSpeculativeType() |
| 17027 |
*/ |
| 17028 |
|
| 17029 |
Type tb = t.toBasetype(); |
| 17030 |
switch (tb.ty) |
| 17031 |
{ |
| 17032 |
case Tvector: visitVector(tb.isTypeVector()); break; |
| 17033 |
case Taarray: visitAArray(tb.isTypeAArray()); break; |
| 17034 |
case Tstruct: visitStruct(tb.isTypeStruct()); break; |
| 17035 |
case Ttuple: visitTuple (tb.isTypeTuple()); break; |
| 17036 |
|
| 17037 |
case Tclass: |
| 17038 |
case Tenum: break; |
| 17039 |
|
| 17040 |
default: semanticTypeInfo(sc, tb.nextOf()); break; |
| 17041 |
} |
| 17042 |
} |
| 17043 |
|
| 17044 |
/** |
| 17045 |
* Issue an error if an attempt to call a disabled method is made |
| 17046 |
* |
| 17047 |
* If the declaration is disabled but inside a disabled function, |
| 17048 |
* returns `true` but do not issue an error message. |
| 17049 |
* |
| 17050 |
* Params: |
| 17051 |
* d = Declaration to check |
| 17052 |
* loc = Location information of the call |
| 17053 |
* sc = Scope in which the call occurs |
| 17054 |
* isAliasedDeclaration = if `true` searches overload set |
| 17055 |
* |
| 17056 |
* Returns: |
| 17057 |
* `true` if this `Declaration` is `@disable`d, `false` otherwise. |
| 17058 |
*/ |
| 17059 |
bool checkDisabled(Declaration d, Loc loc, Scope* sc, bool isAliasedDeclaration = false) |
| 17060 |
{ |
| 17061 |
if (!(d.storage_class & STC.disable)) |
| 17062 |
return false; |
| 17063 |
|
| 17064 |
if (sc.func && sc.func.storage_class & STC.disable) |
| 17065 |
return true; |
| 17066 |
|
| 17067 |
if (auto p = d.toParent()) |
| 17068 |
{ |
| 17069 |
if (auto postblit = d.isPostBlitDeclaration()) |
| 17070 |
{ |
| 17071 |
/* https://issues.dlang.org/show_bug.cgi?id=21885 |
| 17072 |
* |
| 17073 |
* If the generated postblit is disabled, it |
| 17074 |
* means that one of the fields has a disabled |
| 17075 |
* postblit. Print the first field that has |
| 17076 |
* a disabled postblit. |
| 17077 |
*/ |
| 17078 |
if (postblit.isGenerated()) |
| 17079 |
{ |
| 17080 |
auto sd = p.isStructDeclaration(); |
| 17081 |
assert(sd); |
| 17082 |
for (size_t i = 0; i < sd.fields.length; i++) |
| 17083 |
{ |
| 17084 |
auto structField = sd.fields[i]; |
| 17085 |
if (structField.overlapped) |
| 17086 |
continue; |
| 17087 |
Type tv = structField.type.baseElemOf(); |
| 17088 |
if (tv.ty != Tstruct) |
| 17089 |
continue; |
| 17090 |
auto sdv = (cast(TypeStruct)tv).sym; |
| 17091 |
if (!sdv.postblit) |
| 17092 |
continue; |
| 17093 |
if (sdv.postblit.isDisabled()) |
| 17094 |
{ |
| 17095 |
.error(loc, "%s `%s` is not copyable because field `%s` is not copyable", p.kind, p.toPrettyChars, structField.toChars()); |
| 17096 |
return true; |
| 17097 |
} |
| 17098 |
} |
| 17099 |
} |
| 17100 |
.error(loc, "%s `%s` is not copyable because it has a disabled postblit", p.kind, p.toPrettyChars); |
| 17101 |
return true; |
| 17102 |
} |
| 17103 |
} |
| 17104 |
|
| 17105 |
// if the function is @disabled, maybe there |
| 17106 |
// is an overload in the overload set that isn't |
| 17107 |
if (isAliasedDeclaration) |
| 17108 |
{ |
| 17109 |
if (FuncDeclaration fd = d.isFuncDeclaration()) |
| 17110 |
{ |
| 17111 |
for (FuncDeclaration ovl = fd; ovl; ovl = cast(FuncDeclaration)ovl.overnext) |
| 17112 |
if (!(ovl.storage_class & STC.disable)) |
| 17113 |
return false; |
| 17114 |
} |
| 17115 |
} |
| 17116 |
|
| 17117 |
if (auto ctor = d.isCtorDeclaration()) |
| 17118 |
{ |
| 17119 |
//printf("checkDisabled() %s %s\n", ctor.toPrettyChars(), toChars(ctor.type)); |
| 17120 |
if (ctor.isCpCtor && ctor.isGenerated()) |
| 17121 |
{ |
| 17122 |
.error(loc, "generating an `inout` copy constructor for `struct %s` failed, therefore instances of it are uncopyable", d.parent.toPrettyChars()); |
| 17123 |
return true; |
| 17124 |
} |
| 17125 |
} |
| 17126 |
.error(loc, "%s `%s` cannot be used because it is annotated with `@disable`", d.kind, d.toPrettyChars); |
| 17127 |
return true; |
| 17128 |
} |
| 17129 |
|
| 17130 |
/******************************************* |
| 17131 |
* Helper function for the expansion of manifest constant. |
| 17132 |
*/ |
| 17133 |
private Expression expandInitializer(VarDeclaration vd, Loc loc) |
| 17134 |
{ |
| 17135 |
assert((vd.storage_class & STC.manifest) && vd._init); |
| 17136 |
|
| 17137 |
auto e = vd.getConstInitializer(); |
| 17138 |
if (!e) |
| 17139 |
{ |
| 17140 |
.error(loc, "cannot make expression out of initializer for `%s`", vd.toChars()); |
| 17141 |
return ErrorExp.get(); |
| 17142 |
} |
| 17143 |
|
| 17144 |
e = e.copy(); |
| 17145 |
e.loc = loc; // for better error message |
| 17146 |
return e; |
| 17147 |
} |
| 17148 |
|
| 17149 |
/***************************************************** |
| 17150 |
* Determine if template instance is really a template function, |
| 17151 |
* and that template function needs to infer types from the function |
| 17152 |
* arguments. |
| 17153 |
* |
| 17154 |
* Like findBestMatch, iterate possible template candidates, |
| 17155 |
* but just looks only the necessity of type inference. |
| 17156 |
*/ |
| 17157 |
private bool needsTypeInference(TemplateInstance ti, Scope* sc, int flag = 0) |
| 17158 |
{ |
| 17159 |
//printf("TemplateInstance.needsTypeInference() %s\n", toChars()); |
| 17160 |
if (ti.semanticRun != PASS.initial) |
| 17161 |
return false; |
| 17162 |
|
| 17163 |
const olderrs = global.errors; |
| 17164 |
Objects dedtypes; |
| 17165 |
size_t count = 0; |
| 17166 |
|
| 17167 |
auto tovers = ti.tempdecl.isOverloadSet(); |
| 17168 |
foreach (size_t oi; 0 .. tovers ? tovers.a.length : 1) |
| 17169 |
{ |
| 17170 |
Dsymbol dstart = tovers ? tovers.a[oi] : ti.tempdecl; |
| 17171 |
int r = overloadApply(dstart, (Dsymbol s) |
| 17172 |
{ |
| 17173 |
auto td = s.isTemplateDeclaration(); |
| 17174 |
if (!td) |
| 17175 |
return 0; |
| 17176 |
|
| 17177 |
/* If any of the overloaded template declarations need inference, |
| 17178 |
* then return true |
| 17179 |
*/ |
| 17180 |
if (!td.onemember) |
| 17181 |
return 0; |
| 17182 |
if (auto td2 = td.onemember.isTemplateDeclaration()) |
| 17183 |
{ |
| 17184 |
if (!td2.onemember || !td2.onemember.isFuncDeclaration()) |
| 17185 |
return 0; |
| 17186 |
if (ti.tiargs.length >= td.parameters.length - (td.isVariadic() ? 1 : 0)) |
| 17187 |
return 0; |
| 17188 |
return 1; |
| 17189 |
} |
| 17190 |
auto fd = td.onemember.isFuncDeclaration(); |
| 17191 |
if (!fd || fd.type.ty != Tfunction) |
| 17192 |
return 0; |
| 17193 |
|
| 17194 |
foreach (tp; *td.parameters) |
| 17195 |
{ |
| 17196 |
if (tp.isTemplateThisParameter()) |
| 17197 |
return 1; |
| 17198 |
} |
| 17199 |
|
| 17200 |
/* Determine if the instance arguments, tiargs, are all that is necessary |
| 17201 |
* to instantiate the template. |
| 17202 |
*/ |
| 17203 |
//printf("tp = %p, td.parameters.length = %d, tiargs.length = %d\n", tp, td.parameters.length, tiargs.length); |
| 17204 |
auto tf = fd.type.isTypeFunction(); |
| 17205 |
if (tf.parameterList.length) |
| 17206 |
{ |
| 17207 |
auto tp = td.isVariadic(); |
| 17208 |
if (tp && td.parameters.length > 1) |
| 17209 |
return 1; |
| 17210 |
|
| 17211 |
if (!tp && ti.tiargs.length < td.parameters.length) |
| 17212 |
{ |
| 17213 |
// Can remain tiargs be filled by default arguments? |
| 17214 |
foreach (size_t i; ti.tiargs.length .. td.parameters.length) |
| 17215 |
{ |
| 17216 |
if (!(*td.parameters)[i].hasDefaultArg()) |
| 17217 |
return 1; |
| 17218 |
} |
| 17219 |
} |
| 17220 |
|
| 17221 |
foreach (i, fparam; tf.parameterList) |
| 17222 |
{ |
| 17223 |
// 'auto ref' needs inference. |
| 17224 |
if (fparam.storageClass & STC.auto_) |
| 17225 |
return 1; |
| 17226 |
} |
| 17227 |
} |
| 17228 |
|
| 17229 |
if (!flag) |
| 17230 |
{ |
| 17231 |
/* Calculate the need for overload resolution. |
| 17232 |
* When only one template can match with tiargs, inference is not necessary. |
| 17233 |
*/ |
| 17234 |
dedtypes.setDim(td.parameters.length); |
| 17235 |
dedtypes.zero(); |
| 17236 |
if (td.semanticRun == PASS.initial) |
| 17237 |
{ |
| 17238 |
if (td._scope) |
| 17239 |
{ |
| 17240 |
// Try to fix forward reference. Ungag errors while doing so. |
| 17241 |
Ungag ungag = td.ungagSpeculative(); |
| 17242 |
td.dsymbolSemantic(td._scope); |
| 17243 |
} |
| 17244 |
if (td.semanticRun == PASS.initial) |
| 17245 |
{ |
| 17246 |
.error(ti.loc, "%s `%s` `%s` forward references template declaration `%s`", |
| 17247 |
ti.kind, ti.toPrettyChars, ti.toChars(), td.toChars()); |
| 17248 |
return 1; |
| 17249 |
} |
| 17250 |
} |
| 17251 |
MATCH m = matchWithInstance(sc, td, ti, dedtypes, ArgumentList(), 0); |
| 17252 |
if (m == MATCH.nomatch) |
| 17253 |
return 0; |
| 17254 |
} |
| 17255 |
|
| 17256 |
/* If there is more than one function template which matches, we may |
| 17257 |
* need type inference (see https://issues.dlang.org/show_bug.cgi?id=4430) |
| 17258 |
*/ |
| 17259 |
return ++count > 1 ? 1 : 0; |
| 17260 |
}); |
| 17261 |
if (r) |
| 17262 |
return true; |
| 17263 |
} |
| 17264 |
|
| 17265 |
if (olderrs != global.errors) |
| 17266 |
{ |
| 17267 |
if (!global.gag) |
| 17268 |
{ |
| 17269 |
errorSupplemental(ti.loc, "while looking for match for `%s`", ti.toChars()); |
| 17270 |
ti.semanticRun = PASS.semanticdone; |
| 17271 |
ti.inst = ti; |
| 17272 |
} |
| 17273 |
ti.errors = true; |
| 17274 |
} |
| 17275 |
//printf("false\n"); |
| 17276 |
return false; |
| 17277 |
} |
| 17278 |
|
| 17279 |
/*************************************** |
| 17280 |
* Fill out remainder of elements[] with default initializers for fields[]. |
| 17281 |
* Params: |
| 17282 |
* sd = struct |
| 17283 |
* loc = location |
| 17284 |
* elements = explicit arguments which given to construct object. |
| 17285 |
* ctorinit = true if the elements will be used for default initialization. |
| 17286 |
* Returns: |
| 17287 |
* false if any errors occur. |
| 17288 |
* Otherwise, returns true and the missing arguments will be pushed in elements[]. |
| 17289 |
*/ |
| 17290 |
bool fill(StructDeclaration sd, Loc loc, ref Expressions elements, bool ctorinit) |
| 17291 |
{ |
| 17292 |
//printf("AggregateDeclaration::fill() %s\n", toChars()); |
| 17293 |
assert(sd.sizeok == Sizeok.done); |
| 17294 |
const nfields = sd.nonHiddenFields(); |
| 17295 |
bool errors = false; |
| 17296 |
|
| 17297 |
size_t dim = elements.length; |
| 17298 |
elements.setDim(nfields); |
| 17299 |
foreach (size_t i; dim .. nfields) |
| 17300 |
elements[i] = null; |
| 17301 |
|
| 17302 |
// Fill in missing any elements with default initializers |
| 17303 |
foreach (i; 0 .. nfields) |
| 17304 |
{ |
| 17305 |
if (elements[i]) |
| 17306 |
continue; |
| 17307 |
|
| 17308 |
auto vd = sd.fields[i]; |
| 17309 |
auto vx = vd; |
| 17310 |
if (vd._init && vd._init.isVoidInitializer()) |
| 17311 |
vx = null; |
| 17312 |
|
| 17313 |
// Find overlapped fields with the hole [vd.offset .. vd.offset.size()]. |
| 17314 |
size_t fieldi = i; |
| 17315 |
foreach (j; 0 .. nfields) |
| 17316 |
{ |
| 17317 |
if (i == j) |
| 17318 |
continue; |
| 17319 |
auto v2 = sd.fields[j]; |
| 17320 |
if (!vd.isOverlappedWith(v2)) |
| 17321 |
continue; |
| 17322 |
|
| 17323 |
if (elements[j]) |
| 17324 |
{ |
| 17325 |
vx = null; |
| 17326 |
break; |
| 17327 |
} |
| 17328 |
if (v2._init && v2._init.isVoidInitializer()) |
| 17329 |
continue; |
| 17330 |
|
| 17331 |
version (all) |
| 17332 |
{ |
| 17333 |
/* Prefer first found non-void-initialized field |
| 17334 |
* union U { int a; int b = 2; } |
| 17335 |
* U u; // Error: overlapping initialization for field a and b |
| 17336 |
*/ |
| 17337 |
if (!vx) |
| 17338 |
{ |
| 17339 |
vx = v2; |
| 17340 |
fieldi = j; |
| 17341 |
} |
| 17342 |
else if (v2._init) |
| 17343 |
{ |
| 17344 |
.error(loc, "overlapping initialization for field `%s` and `%s`", v2.toChars(), vd.toChars()); |
| 17345 |
errors = true; |
| 17346 |
} |
| 17347 |
} |
| 17348 |
else |
| 17349 |
{ |
| 17350 |
// fixes https://issues.dlang.org/show_bug.cgi?id=1432 by enabling this path always |
| 17351 |
|
| 17352 |
/* Prefer explicitly initialized field |
| 17353 |
* union U { int a; int b = 2; } |
| 17354 |
* U u; // OK (u.b == 2) |
| 17355 |
*/ |
| 17356 |
if (!vx || !vx._init && v2._init) |
| 17357 |
{ |
| 17358 |
vx = v2; |
| 17359 |
fieldi = j; |
| 17360 |
} |
| 17361 |
else if (vx != vd && !vx.isOverlappedWith(v2)) |
| 17362 |
{ |
| 17363 |
// Both vx and v2 fills vd, but vx and v2 does not overlap |
| 17364 |
} |
| 17365 |
else if (vx._init && v2._init) |
| 17366 |
{ |
| 17367 |
.error(loc, "overlapping default initialization for field `%s` and `%s`", |
| 17368 |
v2.toChars(), vd.toChars()); |
| 17369 |
errors = true; |
| 17370 |
} |
| 17371 |
else |
| 17372 |
assert(vx._init || !vx._init && !v2._init); |
| 17373 |
} |
| 17374 |
} |
| 17375 |
if (!vx) |
| 17376 |
continue; |
| 17377 |
|
| 17378 |
Expression e; |
| 17379 |
if (vx.type.size() == 0) |
| 17380 |
{ |
| 17381 |
e = null; |
| 17382 |
} |
| 17383 |
else if (vx._init) |
| 17384 |
{ |
| 17385 |
assert(!vx._init.isVoidInitializer()); |
| 17386 |
if (vx.inuse) // https://issues.dlang.org/show_bug.cgi?id=18057 |
| 17387 |
{ |
| 17388 |
.error(loc, "%s `%s` recursive initialization of field", vx.kind(), vx.toPrettyChars()); |
| 17389 |
errors = true; |
| 17390 |
} |
| 17391 |
else |
| 17392 |
e = vx.getConstInitializer(false); |
| 17393 |
} |
| 17394 |
else |
| 17395 |
{ |
| 17396 |
if ((vx.storage_class & STC.nodefaultctor) && !ctorinit) |
| 17397 |
{ |
| 17398 |
.error(loc, "field `%s.%s` must be initialized because it has no default constructor", |
| 17399 |
sd.type.toChars(), vx.toChars()); |
| 17400 |
errors = true; |
| 17401 |
} |
| 17402 |
/* https://issues.dlang.org/show_bug.cgi?id=12509 |
| 17403 |
* Get the element of static array type. |
| 17404 |
*/ |
| 17405 |
Type telem = vx.type; |
| 17406 |
if (telem.ty == Tsarray) |
| 17407 |
{ |
| 17408 |
/* We cannot use Type::baseElemOf() here. |
| 17409 |
* If the bottom of the Tsarray is an enum type, baseElemOf() |
| 17410 |
* will return the base of the enum, and its default initializer |
| 17411 |
* would be different from the enum's. |
| 17412 |
*/ |
| 17413 |
TypeSArray tsa; |
| 17414 |
while ((tsa = telem.toBasetype().isTypeSArray()) !is null) |
| 17415 |
telem = tsa.next; |
| 17416 |
if (telem.ty == Tvoid) |
| 17417 |
telem = Type.tuns8.addMod(telem.mod); |
| 17418 |
} |
| 17419 |
if (telem.needsNested() && ctorinit) |
| 17420 |
e = telem.defaultInit(loc); |
| 17421 |
else |
| 17422 |
e = telem.defaultInitLiteral(loc); |
| 17423 |
} |
| 17424 |
elements[fieldi] = e; |
| 17425 |
} |
| 17426 |
foreach (e; elements) |
| 17427 |
{ |
| 17428 |
if (e && e.op == EXP.error) |
| 17429 |
return false; |
| 17430 |
} |
| 17431 |
|
| 17432 |
return !errors; |
| 17433 |
} |
| 17434 |
|
| 17435 |
/***************************************** |
| 17436 |
* Lower any aggregate that is not an array to an array using a |
| 17437 |
* regular foreach loop within CTFE. If there are multiple |
| 17438 |
* `static foreach` loop variables, an array of tuples is |
| 17439 |
* generated. In thise case, the field `needExpansion` is set to |
| 17440 |
* true to indicate that the static foreach loop expansion will |
| 17441 |
* need to expand the tuples into multiple variables. |
| 17442 |
* |
| 17443 |
* For example, `static foreach (x; range) { ... }` is lowered to: |
| 17444 |
* |
| 17445 |
* static foreach (x; { |
| 17446 |
* typeof({ |
| 17447 |
* foreach (x; range) return x; |
| 17448 |
* }())[] __res; |
| 17449 |
* foreach (x; range) __res ~= x; |
| 17450 |
* return __res; |
| 17451 |
* }()) { ... } |
| 17452 |
* |
| 17453 |
* Finally, call `lowerArrayAggregate` to turn the produced |
| 17454 |
* array into an expression tuple. |
| 17455 |
* |
| 17456 |
* Params: |
| 17457 |
* sfe = The 'static foreach'. |
| 17458 |
* sc = The current scope. |
| 17459 |
*/ |
| 17460 |
extern (C++) void lowerNonArrayAggregate(StaticForeach sfe, Scope* sc) |
| 17461 |
{ |
| 17462 |
import dmd.statement; |
| 17463 |
|
| 17464 |
auto nvars = sfe.aggrfe ? sfe.aggrfe.parameters.length : 1; |
| 17465 |
auto aloc = sfe.aggrfe ? sfe.aggrfe.aggr.loc : sfe.rangefe.lwr.loc; |
| 17466 |
// We need three sets of foreach loop variables because the |
| 17467 |
// lowering contains three foreach loops. |
| 17468 |
Parameters*[3] pparams = [new Parameters(), new Parameters(), new Parameters()]; |
| 17469 |
foreach (i; 0 .. nvars) |
| 17470 |
{ |
| 17471 |
foreach (params; pparams) |
| 17472 |
{ |
| 17473 |
auto p = sfe.aggrfe ? (*sfe.aggrfe.parameters)[i] : sfe.rangefe.param; |
| 17474 |
params.push(new Parameter(aloc, p.storageClass, p.type, p.ident, null, null)); |
| 17475 |
} |
| 17476 |
} |
| 17477 |
Expression[2] res; |
| 17478 |
TypeStruct tplty = null; |
| 17479 |
if (nvars == 1) // only one `static foreach` variable, generate identifiers. |
| 17480 |
{ |
| 17481 |
foreach (i; 0 .. 2) |
| 17482 |
{ |
| 17483 |
res[i] = new IdentifierExp(aloc, (*pparams[i])[0].ident); |
| 17484 |
} |
| 17485 |
} |
| 17486 |
else // multiple `static foreach` variables, generate tuples. |
| 17487 |
{ |
| 17488 |
foreach (i; 0 .. 2) |
| 17489 |
{ |
| 17490 |
auto e = new Expressions(pparams[0].length); |
| 17491 |
foreach (j, ref elem; *e) |
| 17492 |
{ |
| 17493 |
auto p = (*pparams[i])[j]; |
| 17494 |
elem = new IdentifierExp(aloc, p.ident); |
| 17495 |
} |
| 17496 |
if (!tplty) |
| 17497 |
{ |
| 17498 |
tplty = sfe.createTupleType(aloc, e, sc); |
| 17499 |
} |
| 17500 |
res[i] = sfe.createTuple(aloc, tplty, e); |
| 17501 |
} |
| 17502 |
sfe.needExpansion = true; // need to expand the tuples later |
| 17503 |
} |
| 17504 |
// generate remaining code for the new aggregate which is an |
| 17505 |
// array (see documentation comment). |
| 17506 |
if (sfe.rangefe) |
| 17507 |
{ |
| 17508 |
sc = sc.startCTFE(); |
| 17509 |
sfe.rangefe.lwr = sfe.rangefe.lwr.expressionSemantic(sc); |
| 17510 |
sfe.rangefe.lwr = resolveProperties(sc, sfe.rangefe.lwr); |
| 17511 |
sfe.rangefe.upr = sfe.rangefe.upr.expressionSemantic(sc); |
| 17512 |
sfe.rangefe.upr = resolveProperties(sc, sfe.rangefe.upr); |
| 17513 |
sc = sc.endCTFE(); |
| 17514 |
sfe.rangefe.lwr = sfe.rangefe.lwr.optimize(WANTvalue); |
| 17515 |
sfe.rangefe.lwr = sfe.rangefe.lwr.ctfeInterpret(); |
| 17516 |
sfe.rangefe.upr = sfe.rangefe.upr.optimize(WANTvalue); |
| 17517 |
sfe.rangefe.upr = sfe.rangefe.upr.ctfeInterpret(); |
| 17518 |
} |
| 17519 |
auto s1 = new Statements(); |
| 17520 |
auto stmts = new Statements(); |
| 17521 |
if (tplty) stmts.push(new ExpStatement(sfe.loc, tplty.sym)); |
| 17522 |
stmts.push(new ReturnStatement(aloc, res[0])); |
| 17523 |
s1.push(sfe.createForeach(aloc, pparams[0], new CompoundStatement(aloc, stmts))); |
| 17524 |
s1.push(new ExpStatement(aloc, new AssertExp(aloc, IntegerExp.literal!0))); |
| 17525 |
Type ety = new TypeTypeof(aloc, sfe.wrapAndCall(aloc, new CompoundStatement(aloc, s1))); |
| 17526 |
auto aty = ety.arrayOf(); |
| 17527 |
auto idres = Identifier.generateId("__res"); |
| 17528 |
auto vard = new VarDeclaration(aloc, aty, idres, null, STC.temp); |
| 17529 |
auto s2 = new Statements(); |
| 17530 |
|
| 17531 |
// Run 'typeof' gagged to avoid duplicate errors and if it fails just create |
| 17532 |
// an empty foreach to expose them. |
| 17533 |
const olderrors = global.startGagging(); |
| 17534 |
ety = ety.typeSemantic(aloc, sc); |
| 17535 |
if (global.endGagging(olderrors)) |
| 17536 |
s2.push(sfe.createForeach(aloc, pparams[1], null)); |
| 17537 |
else |
| 17538 |
{ |
| 17539 |
s2.push(new ExpStatement(aloc, vard)); |
| 17540 |
auto catass = new CatAssignExp(aloc, new IdentifierExp(aloc, idres), res[1]); |
| 17541 |
s2.push(sfe.createForeach(aloc, pparams[1], new ExpStatement(aloc, catass))); |
| 17542 |
s2.push(new ReturnStatement(aloc, new IdentifierExp(aloc, idres))); |
| 17543 |
} |
| 17544 |
|
| 17545 |
Expression aggr = void; |
| 17546 |
Type indexty = void; |
| 17547 |
|
| 17548 |
if (sfe.rangefe && (indexty = ety).isIntegral()) |
| 17549 |
{ |
| 17550 |
sfe.rangefe.lwr.type = indexty; |
| 17551 |
sfe.rangefe.upr.type = indexty; |
| 17552 |
auto lwrRange = getIntRange(sfe.rangefe.lwr); |
| 17553 |
auto uprRange = getIntRange(sfe.rangefe.upr); |
| 17554 |
|
| 17555 |
const lwr = sfe.rangefe.lwr.toInteger(); |
| 17556 |
auto upr = sfe.rangefe.upr.toInteger(); |
| 17557 |
size_t length = 0; |
| 17558 |
|
| 17559 |
if (lwrRange.imin <= uprRange.imax) |
| 17560 |
length = cast(size_t) (upr - lwr); |
| 17561 |
|
| 17562 |
auto exps = new Expressions(length); |
| 17563 |
|
| 17564 |
if (sfe.rangefe.op == TOK.foreach_) |
| 17565 |
{ |
| 17566 |
foreach (i; 0 .. length) |
| 17567 |
(*exps)[i] = new IntegerExp(aloc, lwr + i, indexty); |
| 17568 |
} |
| 17569 |
else |
| 17570 |
{ |
| 17571 |
--upr; |
| 17572 |
foreach (i; 0 .. length) |
| 17573 |
(*exps)[i] = new IntegerExp(aloc, upr - i, indexty); |
| 17574 |
} |
| 17575 |
aggr = new ArrayLiteralExp(aloc, indexty.arrayOf(), exps); |
| 17576 |
} |
| 17577 |
else |
| 17578 |
{ |
| 17579 |
aggr = sfe.wrapAndCall(aloc, new CompoundStatement(aloc, s2)); |
| 17580 |
sc = sc.startCTFE(); |
| 17581 |
aggr = aggr.expressionSemantic(sc); |
| 17582 |
aggr = resolveProperties(sc, aggr); |
| 17583 |
sc = sc.endCTFE(); |
| 17584 |
aggr = aggr.optimize(WANTvalue); |
| 17585 |
aggr = aggr.ctfeInterpret(); |
| 17586 |
} |
| 17587 |
|
| 17588 |
assert(!!sfe.aggrfe ^ !!sfe.rangefe); |
| 17589 |
sfe.aggrfe = new ForeachStatement(sfe.loc, TOK.foreach_, pparams[2], aggr, |
| 17590 |
sfe.aggrfe ? sfe.aggrfe._body : sfe.rangefe._body, |
| 17591 |
sfe.aggrfe ? sfe.aggrfe.endloc : sfe.rangefe.endloc); |
| 17592 |
sfe.rangefe = null; |
| 17593 |
sfe.lowerArrayAggregate(sc); // finally, turn generated array into expression tuple |
| 17594 |
} |
| 17595 |
|
| 17596 |
/***************************************** |
| 17597 |
* Perform `static foreach` lowerings that are necessary in order |
| 17598 |
* to finally expand the `static foreach` using |
| 17599 |
* `dmd.statementsem.makeTupleForeach`. |
| 17600 |
*/ |
| 17601 |
extern(D) void prepare(StaticForeach sfe, Scope* sc) |
| 17602 |
{ |
| 17603 |
assert(sc); |
| 17604 |
|
| 17605 |
if (sfe.aggrfe) |
| 17606 |
{ |
| 17607 |
sc = sc.startCTFE(); |
| 17608 |
sfe.aggrfe.aggr = sfe.aggrfe.aggr.expressionSemantic(sc); |
| 17609 |
sc = sc.endCTFE(); |
| 17610 |
} |
| 17611 |
|
| 17612 |
if (sfe.aggrfe && sfe.aggrfe.aggr.type.toBasetype().ty == Terror) |
| 17613 |
{ |
| 17614 |
return; |
| 17615 |
} |
| 17616 |
|
| 17617 |
if (!sfe.ready()) |
| 17618 |
{ |
| 17619 |
if (sfe.aggrfe && sfe.aggrfe.aggr.type.toBasetype().ty == Tarray) |
| 17620 |
{ |
| 17621 |
sfe.lowerArrayAggregate(sc); |
| 17622 |
} |
| 17623 |
else |
| 17624 |
{ |
| 17625 |
sfe.lowerNonArrayAggregate(sc); |
| 17626 |
} |
| 17627 |
} |
| 17628 |
} |
| 17629 |
|
| 17630 |
/***************************************** |
| 17631 |
* Turn an aggregate which is an array into an expression tuple |
| 17632 |
* of its elements. I.e., lower |
| 17633 |
* static foreach (x; [1, 2, 3, 4]) { ... } |
| 17634 |
* to |
| 17635 |
* static foreach (x; AliasSeq!(1, 2, 3, 4)) { ... } |
| 17636 |
*/ |
| 17637 |
extern(D) void lowerArrayAggregate(StaticForeach sfe, Scope* sc) |
| 17638 |
{ |
| 17639 |
auto aggr = sfe.aggrfe.aggr; |
| 17640 |
Expression el = new ArrayLengthExp(aggr.loc, aggr); |
| 17641 |
sc = sc.startCTFE(); |
| 17642 |
el = el.expressionSemantic(sc); |
| 17643 |
sc = sc.endCTFE(); |
| 17644 |
el = el.optimize(WANTvalue); |
| 17645 |
el = el.ctfeInterpret(); |
| 17646 |
if (el.op != EXP.int64) |
| 17647 |
{ |
| 17648 |
sfe.aggrfe.aggr = ErrorExp.get(); |
| 17649 |
return; |
| 17650 |
} |
| 17651 |
|
| 17652 |
Expressions* es; |
| 17653 |
if (auto ale = aggr.isArrayLiteralExp()) |
| 17654 |
{ |
| 17655 |
// Directly use the elements of the array for the TupleExp creation |
| 17656 |
es = ale.elements; |
| 17657 |
} |
| 17658 |
else |
| 17659 |
{ |
| 17660 |
const length = cast(size_t)el.toInteger(); |
| 17661 |
es = new Expressions(length); |
| 17662 |
foreach (i; 0 .. length) |
| 17663 |
{ |
| 17664 |
auto index = new IntegerExp(sfe.loc, i, Type.tsize_t); |
| 17665 |
auto value = new IndexExp(aggr.loc, aggr, index); |
| 17666 |
(*es)[i] = value; |
| 17667 |
} |
| 17668 |
} |
| 17669 |
sfe.aggrfe.aggr = new TupleExp(aggr.loc, es); |
| 17670 |
sfe.aggrfe.aggr = sfe.aggrfe.aggr.expressionSemantic(sc); |
| 17671 |
sfe.aggrfe.aggr = sfe.aggrfe.aggr.optimize(WANTvalue); |
| 17672 |
sfe.aggrfe.aggr = sfe.aggrfe.aggr.ctfeInterpret(); |
| 17673 |
} |