| 1 |
/** |
| 2 |
* Does semantic analysis for statements. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/statement.html, Statements) |
| 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/statementsem.d, _statementsem.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_statementsem.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/statementsem.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.statementsem; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
|
| 18 |
import dmd.aggregate; |
| 19 |
import dmd.arrayop; |
| 20 |
import dmd.arraytypes; |
| 21 |
import dmd.astcodegen; |
| 22 |
import dmd.astenums; |
| 23 |
import dmd.blockexit; |
| 24 |
import dmd.clone; |
| 25 |
import dmd.cond; |
| 26 |
import dmd.ctorflow; |
| 27 |
import dmd.dcast; |
| 28 |
import dmd.dclass; |
| 29 |
import dmd.declaration; |
| 30 |
import dmd.denum; |
| 31 |
import dmd.dimport; |
| 32 |
import dmd.dinterpret; |
| 33 |
import dmd.dmodule; |
| 34 |
import dmd.dscope; |
| 35 |
import dmd.dsymbol; |
| 36 |
import dmd.dsymbolsem; |
| 37 |
import dmd.errors; |
| 38 |
import dmd.escape; |
| 39 |
import dmd.expression; |
| 40 |
import dmd.expressionsem; |
| 41 |
import dmd.func; |
| 42 |
import dmd.funcsem; |
| 43 |
import dmd.globals; |
| 44 |
import dmd.hdrgen; |
| 45 |
import dmd.iasm; |
| 46 |
import dmd.id; |
| 47 |
import dmd.identifier; |
| 48 |
import dmd.importc; |
| 49 |
import dmd.init; |
| 50 |
import dmd.intrange; |
| 51 |
import dmd.location; |
| 52 |
import dmd.mtype; |
| 53 |
import dmd.mustuse; |
| 54 |
import dmd.nogc; |
| 55 |
import dmd.optimize; |
| 56 |
import dmd.opover; |
| 57 |
import dmd.parse; |
| 58 |
import dmd.common.outbuffer; |
| 59 |
import dmd.root.string; |
| 60 |
import dmd.safe : isSafe, isSaferD, setUnsafe; |
| 61 |
import dmd.semantic2; |
| 62 |
import dmd.sideeffect; |
| 63 |
import dmd.statement; |
| 64 |
import dmd.target; |
| 65 |
import dmd.tokens; |
| 66 |
import dmd.typesem; |
| 67 |
import dmd.visitor; |
| 68 |
|
| 69 |
version (DMDLIB) |
| 70 |
{ |
| 71 |
version = CallbackAPI; |
| 72 |
} |
| 73 |
|
| 74 |
/***************************************** |
| 75 |
* CTFE requires FuncDeclaration::labtab for the interpretation. |
| 76 |
* So fixing the label name inside in/out contracts is necessary |
| 77 |
* for the uniqueness in labtab. |
| 78 |
* Params: |
| 79 |
* sc = context |
| 80 |
* ident = statement label name to be adjusted |
| 81 |
* Returns: |
| 82 |
* adjusted label name |
| 83 |
*/ |
| 84 |
private Identifier fixupLabelName(Scope* sc, Identifier ident) |
| 85 |
{ |
| 86 |
Contract c = sc.contract; |
| 87 |
const id = ident.toString(); |
| 88 |
if (c != Contract.none && c != Contract.invariant_ && |
| 89 |
!(id.length >= 2 && id[0] == '_' && id[1] == '_')) // does not start with "__" |
| 90 |
{ |
| 91 |
OutBuffer buf; |
| 92 |
buf.writestring(c == Contract.require ? "__in_" : "__out_"); |
| 93 |
buf.writestring(ident.toString()); |
| 94 |
|
| 95 |
ident = Identifier.idPool(buf[]); |
| 96 |
} |
| 97 |
return ident; |
| 98 |
} |
| 99 |
|
| 100 |
/******************************************* |
| 101 |
* Check to see if statement is the innermost labeled statement. |
| 102 |
* Params: |
| 103 |
* sc = context |
| 104 |
* statement = Statement to check |
| 105 |
* Returns: |
| 106 |
* if `true`, then the `LabelStatement`, otherwise `null` |
| 107 |
*/ |
| 108 |
private LabelStatement checkLabeledLoop(Scope* sc, Statement statement) @safe |
| 109 |
{ |
| 110 |
if (sc.slabel && sc.slabel.statement == statement) |
| 111 |
{ |
| 112 |
return sc.slabel; |
| 113 |
} |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
/*********************************************************** |
| 118 |
* Check an assignment is used as a condition. |
| 119 |
* Intended to be use before the `semantic` call on `e`. |
| 120 |
* Params: |
| 121 |
* e = condition expression which is not yet run semantic analysis. |
| 122 |
* Returns: |
| 123 |
* `e` or ErrorExp. |
| 124 |
*/ |
| 125 |
private Expression checkAssignmentAsCondition(Expression e, Scope* sc) |
| 126 |
{ |
| 127 |
if (sc.inCfile) |
| 128 |
return e; |
| 129 |
auto ec = lastComma(e); |
| 130 |
if (ec.op == EXP.assign) |
| 131 |
{ |
| 132 |
error(ec.loc, "assignment cannot be used as a condition, perhaps `==` was meant?"); |
| 133 |
return ErrorExp.get(); |
| 134 |
} |
| 135 |
return e; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Performs semantic analysis in Statement AST nodes |
| 140 |
* |
| 141 |
* Params: |
| 142 |
* s = statement to perform semantic analysis on |
| 143 |
* sc = scope in which statement resides |
| 144 |
* |
| 145 |
* Returns: statement `s` after semantic analysis. |
| 146 |
* Can be `null`, for example with `pragma(msg, "")` |
| 147 |
*/ |
| 148 |
Statement statementSemantic(Statement s, Scope* sc) |
| 149 |
{ |
| 150 |
import dmd.compiler; |
| 151 |
|
| 152 |
version (CallbackAPI) |
| 153 |
Compiler.onStatementSemanticStart(s, sc); |
| 154 |
|
| 155 |
Statement result = statementSemanticVisit(s, sc); |
| 156 |
|
| 157 |
version (CallbackAPI) |
| 158 |
Compiler.onStatementSemanticDone(s, sc); |
| 159 |
|
| 160 |
return result; |
| 161 |
} |
| 162 |
|
| 163 |
package (dmd) |
| 164 |
Statement statementSemanticVisit(Statement s, Scope* sc) |
| 165 |
{ |
| 166 |
Statement result; |
| 167 |
|
| 168 |
void setError() |
| 169 |
{ |
| 170 |
result = new ErrorStatement(); |
| 171 |
} |
| 172 |
|
| 173 |
void visitDefaultCase(Statement s) |
| 174 |
{ |
| 175 |
result = s; |
| 176 |
} |
| 177 |
|
| 178 |
void visitError(ErrorStatement s) |
| 179 |
{ |
| 180 |
result = s; |
| 181 |
} |
| 182 |
|
| 183 |
void visitPeel(PeelStatement s) |
| 184 |
{ |
| 185 |
/* "peel" off this wrapper, and don't run semantic() |
| 186 |
* on the result. |
| 187 |
*/ |
| 188 |
result = s.s; |
| 189 |
} |
| 190 |
|
| 191 |
void visitExp(ExpStatement s) |
| 192 |
{ |
| 193 |
/* https://dlang.org/spec/statement.html#expression-statement |
| 194 |
*/ |
| 195 |
|
| 196 |
if (!s.exp) |
| 197 |
{ |
| 198 |
result = s; |
| 199 |
return; |
| 200 |
} |
| 201 |
//printf("ExpStatement::semantic() %s\n", exp.toChars()); |
| 202 |
|
| 203 |
// Allow CommaExp in ExpStatement because return isn't used |
| 204 |
CommaExp.allow(s.exp); |
| 205 |
|
| 206 |
s.exp = s.exp.expressionSemantic(sc); |
| 207 |
s.exp = resolveProperties(sc, s.exp); |
| 208 |
s.exp = s.exp.addDtorHook(sc); |
| 209 |
if (checkNonAssignmentArrayOp(s.exp)) |
| 210 |
s.exp = ErrorExp.get(); |
| 211 |
if (auto f = isFuncAddress(s.exp)) |
| 212 |
{ |
| 213 |
if (f.checkForwardRef(s.exp.loc)) |
| 214 |
s.exp = ErrorExp.get(); |
| 215 |
} |
| 216 |
if (checkMustUse(s.exp, sc)) |
| 217 |
s.exp = ErrorExp.get(); |
| 218 |
if (!sc.inCfile && discardValue(s.exp)) |
| 219 |
s.exp = ErrorExp.get(); |
| 220 |
|
| 221 |
s.exp = s.exp.optimize(WANTvalue); |
| 222 |
s.exp = checkGC(sc, s.exp); |
| 223 |
if (s.exp.op == EXP.error) |
| 224 |
return setError(); |
| 225 |
result = s; |
| 226 |
} |
| 227 |
|
| 228 |
void visitDtorExp(DtorExpStatement s) |
| 229 |
{ |
| 230 |
visitExp(s); |
| 231 |
} |
| 232 |
|
| 233 |
void visitMixin(MixinStatement cs) |
| 234 |
{ |
| 235 |
/* https://dlang.org/spec/statement.html#mixin-statement |
| 236 |
*/ |
| 237 |
|
| 238 |
//printf("MixinStatement::semantic() %s\n", exp.toChars()); |
| 239 |
Statements* a = cs.flatten(sc); |
| 240 |
if (!a) |
| 241 |
return; |
| 242 |
Statement s = new CompoundStatement(cs.loc, a); |
| 243 |
result = s.statementSemantic(sc); |
| 244 |
} |
| 245 |
|
| 246 |
void visitCompound(CompoundStatement cs) |
| 247 |
{ |
| 248 |
//printf("CompoundStatement::semantic(this = %p, sc = %p)\n", cs, sc); |
| 249 |
version (none) |
| 250 |
{ |
| 251 |
foreach (i, s; cs.statements) |
| 252 |
{ |
| 253 |
if (s) |
| 254 |
printf("[%d]: %s", i, s.toChars()); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
for (size_t i = 0; i < cs.statements.length;) |
| 259 |
{ |
| 260 |
Statement s = (*cs.statements)[i]; |
| 261 |
if (!s) |
| 262 |
{ |
| 263 |
++i; |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
if (auto flt = s.flatten(sc)) |
| 268 |
{ |
| 269 |
cs.statements.remove(i); |
| 270 |
cs.statements.insert(i, flt); |
| 271 |
continue; |
| 272 |
} |
| 273 |
s = s.statementSemantic(sc); |
| 274 |
(*cs.statements)[i] = s; |
| 275 |
if (!s) |
| 276 |
{ |
| 277 |
/* Remove NULL statements from the list. |
| 278 |
*/ |
| 279 |
cs.statements.remove(i); |
| 280 |
continue; |
| 281 |
} |
| 282 |
if (s.isErrorStatement()) |
| 283 |
{ |
| 284 |
result = s; // propagate error up the AST |
| 285 |
++i; |
| 286 |
continue; // look for errors in rest of statements |
| 287 |
} |
| 288 |
|
| 289 |
// expand tuple variables in order to attach destruction/exception logic |
| 290 |
if (auto es = s.isExpStatement()) |
| 291 |
{ |
| 292 |
if (es.exp && es.exp.isDeclarationExp()) |
| 293 |
{ |
| 294 |
auto de = es.exp.isDeclarationExp(); |
| 295 |
auto vd = de.declaration.isVarDeclaration(); |
| 296 |
if (vd && vd.aliasTuple && vd.aliasTuple.objects.length) |
| 297 |
{ |
| 298 |
auto j = i; |
| 299 |
cs.statements.insert(i, vd.aliasTuple.objects.length - 1, null); |
| 300 |
vd.aliasTuple.foreachVar((v) { (*cs.statements)[j++] = toStatement(v); }); |
| 301 |
s = (*cs.statements)[i]; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
Statement sentry; |
| 307 |
Statement sexception; |
| 308 |
Statement sfinally; |
| 309 |
|
| 310 |
(*cs.statements)[i] = s.scopeCode(sc, sentry, sexception, sfinally); |
| 311 |
if (sentry) |
| 312 |
{ |
| 313 |
sentry = sentry.statementSemantic(sc); |
| 314 |
cs.statements.insert(i, sentry); |
| 315 |
i++; |
| 316 |
} |
| 317 |
if (sexception) |
| 318 |
sexception = sexception.statementSemantic(sc); |
| 319 |
if (sexception) |
| 320 |
{ |
| 321 |
/* Returns: true if statements[] are empty statements |
| 322 |
*/ |
| 323 |
static bool isEmpty(const Statement[] statements) |
| 324 |
{ |
| 325 |
foreach (s; statements) |
| 326 |
{ |
| 327 |
if (const cs = s.isCompoundStatement()) |
| 328 |
{ |
| 329 |
if (!isEmpty((*cs.statements)[])) |
| 330 |
return false; |
| 331 |
} |
| 332 |
else |
| 333 |
return false; |
| 334 |
} |
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
if (!sfinally && isEmpty((*cs.statements)[i + 1 .. cs.statements.length])) |
| 339 |
{ |
| 340 |
} |
| 341 |
else |
| 342 |
{ |
| 343 |
/* Rewrite: |
| 344 |
* s; s1; s2; |
| 345 |
* As: |
| 346 |
* s; |
| 347 |
* try { s1; s2; } |
| 348 |
* catch (Throwable __o) |
| 349 |
* { sexception; throw __o; } |
| 350 |
*/ |
| 351 |
auto a = new Statements(); |
| 352 |
a.pushSlice((*cs.statements)[i + 1 .. cs.statements.length]); |
| 353 |
cs.statements.setDim(i + 1); |
| 354 |
|
| 355 |
Statement _body = new CompoundStatement(Loc.initial, a); |
| 356 |
_body = new ScopeStatement(Loc.initial, _body, Loc.initial); |
| 357 |
|
| 358 |
Identifier id = Identifier.generateId("__o"); |
| 359 |
|
| 360 |
Statement handler = new PeelStatement(sexception); |
| 361 |
if (sexception.blockExit(sc.func, null) & BE.fallthru) |
| 362 |
{ |
| 363 |
auto ts = new ThrowStatement(Loc.initial, new IdentifierExp(Loc.initial, id)); |
| 364 |
ts.internalThrow = true; |
| 365 |
handler = new CompoundStatement(Loc.initial, handler, ts); |
| 366 |
} |
| 367 |
|
| 368 |
auto catches = new Catches(); |
| 369 |
auto ctch = new Catch(Loc.initial, getThrowable(), id, handler); |
| 370 |
ctch.internalCatch = true; |
| 371 |
catches.push(ctch); |
| 372 |
|
| 373 |
Statement st = new TryCatchStatement(Loc.initial, _body, catches); |
| 374 |
if (sfinally) |
| 375 |
st = new TryFinallyStatement(Loc.initial, st, sfinally); |
| 376 |
st = st.statementSemantic(sc); |
| 377 |
|
| 378 |
cs.statements.push(st); |
| 379 |
break; |
| 380 |
} |
| 381 |
} |
| 382 |
else if (sfinally) |
| 383 |
{ |
| 384 |
if (0 && i + 1 == cs.statements.length) |
| 385 |
{ |
| 386 |
cs.statements.push(sfinally); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
/* Rewrite: |
| 391 |
* s; s1; s2; |
| 392 |
* As: |
| 393 |
* s; try { s1; s2; } finally { sfinally; } |
| 394 |
*/ |
| 395 |
auto a = new Statements(); |
| 396 |
a.pushSlice((*cs.statements)[i + 1 .. cs.statements.length]); |
| 397 |
cs.statements.setDim(i + 1); |
| 398 |
|
| 399 |
auto _body = new CompoundStatement(Loc.initial, a); |
| 400 |
Statement stf = new TryFinallyStatement(Loc.initial, _body, sfinally); |
| 401 |
stf = stf.statementSemantic(sc); |
| 402 |
cs.statements.push(stf); |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
i++; |
| 407 |
} |
| 408 |
|
| 409 |
/* Flatten them in place |
| 410 |
*/ |
| 411 |
void flattenStatements(ref Statements statements) |
| 412 |
{ |
| 413 |
for (size_t i = 0; i < statements.length;) |
| 414 |
{ |
| 415 |
if (auto s = statements[i]) |
| 416 |
{ |
| 417 |
if (auto flt = s.flatten(sc)) |
| 418 |
{ |
| 419 |
statements.remove(i); |
| 420 |
statements.insert(i, flt); |
| 421 |
continue; |
| 422 |
} |
| 423 |
} |
| 424 |
++i; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
/* https://issues.dlang.org/show_bug.cgi?id=11653 |
| 429 |
* 'semantic' may return another CompoundStatement |
| 430 |
* (eg. CaseRangeStatement), so flatten it here. |
| 431 |
*/ |
| 432 |
flattenStatements(*cs.statements); |
| 433 |
|
| 434 |
foreach (s; *cs.statements) |
| 435 |
{ |
| 436 |
if (!s) |
| 437 |
continue; |
| 438 |
|
| 439 |
if (auto se = s.isErrorStatement()) |
| 440 |
{ |
| 441 |
result = se; |
| 442 |
return; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (cs.statements.length == 1) |
| 447 |
{ |
| 448 |
result = (*cs.statements)[0]; |
| 449 |
return; |
| 450 |
} |
| 451 |
result = cs; |
| 452 |
} |
| 453 |
|
| 454 |
void visitUnrolledLoop(UnrolledLoopStatement uls) |
| 455 |
{ |
| 456 |
//printf("UnrolledLoopStatement::semantic(this = %p, sc = %p)\n", uls, sc); |
| 457 |
Scope* scd = sc.push(); |
| 458 |
scd.sbreak = uls; |
| 459 |
scd.scontinue = uls; |
| 460 |
|
| 461 |
Statement serror = null; |
| 462 |
foreach (i, ref s; *uls.statements) |
| 463 |
{ |
| 464 |
if (s) |
| 465 |
{ |
| 466 |
//printf("[%d]: %s\n", i, s.toChars()); |
| 467 |
s = s.statementSemantic(scd); |
| 468 |
if (s && !serror) |
| 469 |
serror = s.isErrorStatement(); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
scd.pop(); |
| 474 |
result = serror ? serror : uls; |
| 475 |
} |
| 476 |
|
| 477 |
void visitScope(ScopeStatement ss) |
| 478 |
{ |
| 479 |
//printf("ScopeStatement::semantic(sc = %p)\n", sc); |
| 480 |
if (!ss.statement) |
| 481 |
{ |
| 482 |
result = ss; |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
ScopeDsymbol sym = new ScopeDsymbol(); |
| 487 |
sym.parent = sc.scopesym; |
| 488 |
sym.endlinnum = ss.endloc.linnum; |
| 489 |
sc = sc.push(sym); |
| 490 |
|
| 491 |
Statements* a = ss.statement.flatten(sc); |
| 492 |
if (a) |
| 493 |
{ |
| 494 |
ss.statement = new CompoundStatement(ss.loc, a); |
| 495 |
} |
| 496 |
|
| 497 |
ss.statement = ss.statement.statementSemantic(sc); |
| 498 |
if (ss.statement) |
| 499 |
{ |
| 500 |
if (ss.statement.isErrorStatement()) |
| 501 |
{ |
| 502 |
sc.pop(); |
| 503 |
result = ss.statement; |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
Statement sentry; |
| 508 |
Statement sexception; |
| 509 |
Statement sfinally; |
| 510 |
ss.statement = ss.statement.scopeCode(sc, sentry, sexception, sfinally); |
| 511 |
assert(!sentry); |
| 512 |
assert(!sexception); |
| 513 |
if (sfinally) |
| 514 |
{ |
| 515 |
//printf("adding sfinally\n"); |
| 516 |
sfinally = sfinally.statementSemantic(sc); |
| 517 |
ss.statement = new CompoundStatement(ss.loc, ss.statement, sfinally); |
| 518 |
} |
| 519 |
} |
| 520 |
sc.pop(); |
| 521 |
result = ss; |
| 522 |
} |
| 523 |
|
| 524 |
void visitForwarding(ForwardingStatement ss) |
| 525 |
{ |
| 526 |
assert(ss.sym); |
| 527 |
for (Scope* csc = sc; !ss.sym.parent; csc = csc.enclosing) |
| 528 |
{ |
| 529 |
assert(csc); |
| 530 |
ss.sym.parent = csc.scopesym; |
| 531 |
} |
| 532 |
sc = sc.push(ss.sym); |
| 533 |
sc.sbreak = ss; |
| 534 |
sc.scontinue = ss; |
| 535 |
ss.statement = ss.statement.statementSemantic(sc); |
| 536 |
sc = sc.pop(); |
| 537 |
result = ss.statement; |
| 538 |
} |
| 539 |
|
| 540 |
void visitWhile(WhileStatement ws) |
| 541 |
{ |
| 542 |
/* Rewrite as a for(;condition;) loop |
| 543 |
* https://dlang.org/spec/statement.html#while-statement |
| 544 |
*/ |
| 545 |
Expression cond = ws.condition; |
| 546 |
Statement _body = ws._body; |
| 547 |
if (ws.param) |
| 548 |
{ |
| 549 |
/** |
| 550 |
* If the while loop is of form `while(auto a = exp) { loop_body }`, |
| 551 |
* rewrite to: |
| 552 |
* |
| 553 |
* while(true) |
| 554 |
* if (auto a = exp) |
| 555 |
* { loop_body } |
| 556 |
* else |
| 557 |
* { break; } |
| 558 |
*/ |
| 559 |
_body = new IfStatement(ws.loc, ws.param, ws.condition, ws._body, new BreakStatement(ws.loc, null), ws.endloc); |
| 560 |
cond = IntegerExp.createBool(true); |
| 561 |
} |
| 562 |
Statement s = new ForStatement(ws.loc, null, cond, null, _body, ws.endloc); |
| 563 |
s = s.statementSemantic(sc); |
| 564 |
result = s; |
| 565 |
} |
| 566 |
|
| 567 |
void visitDo(DoStatement ds) |
| 568 |
{ |
| 569 |
/* https://dlang.org/spec/statement.html#do-statement |
| 570 |
*/ |
| 571 |
const inLoopSave = sc.inLoop; |
| 572 |
sc.inLoop = true; |
| 573 |
if (ds._body) |
| 574 |
ds._body = ds._body.semanticScope(sc, ds, ds, null); |
| 575 |
sc.inLoop = inLoopSave; |
| 576 |
|
| 577 |
if (auto dotid = ds.condition.isDotIdExp()) |
| 578 |
dotid.noderef = true; |
| 579 |
|
| 580 |
// check in syntax level |
| 581 |
ds.condition = checkAssignmentAsCondition(ds.condition, sc); |
| 582 |
|
| 583 |
ds.condition = ds.condition.expressionSemantic(sc); |
| 584 |
ds.condition = resolveProperties(sc, ds.condition); |
| 585 |
if (checkNonAssignmentArrayOp(ds.condition)) |
| 586 |
ds.condition = ErrorExp.get(); |
| 587 |
ds.condition = ds.condition.optimize(WANTvalue); |
| 588 |
ds.condition = checkGC(sc, ds.condition); |
| 589 |
|
| 590 |
ds.condition = ds.condition.toBoolean(sc); |
| 591 |
|
| 592 |
if (ds.condition.op == EXP.error) |
| 593 |
return setError(); |
| 594 |
if (ds._body && ds._body.isErrorStatement()) |
| 595 |
{ |
| 596 |
result = ds._body; |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
result = ds; |
| 601 |
} |
| 602 |
|
| 603 |
void visitFor(ForStatement fs) |
| 604 |
{ |
| 605 |
/* https://dlang.org/spec/statement.html#for-statement |
| 606 |
*/ |
| 607 |
//printf("ForStatement::semantic %s\n", fs.toChars()); |
| 608 |
|
| 609 |
if (fs._init) |
| 610 |
{ |
| 611 |
/* Rewrite: |
| 612 |
* for (auto v1 = i1, v2 = i2; condition; increment) { ... } |
| 613 |
* to: |
| 614 |
* { auto v1 = i1, v2 = i2; for (; condition; increment) { ... } } |
| 615 |
* then lowered to: |
| 616 |
* auto v1 = i1; |
| 617 |
* try { |
| 618 |
* auto v2 = i2; |
| 619 |
* try { |
| 620 |
* for (; condition; increment) { ... } |
| 621 |
* } finally { v2.~this(); } |
| 622 |
* } finally { v1.~this(); } |
| 623 |
*/ |
| 624 |
auto ainit = new Statements(); |
| 625 |
ainit.push(fs._init); |
| 626 |
fs._init = null; |
| 627 |
ainit.push(fs); |
| 628 |
Statement s = new CompoundStatement(fs.loc, ainit); |
| 629 |
s = new ScopeStatement(fs.loc, s, fs.endloc); |
| 630 |
s = s.statementSemantic(sc); |
| 631 |
if (!s.isErrorStatement()) |
| 632 |
{ |
| 633 |
if (LabelStatement ls = checkLabeledLoop(sc, fs)) |
| 634 |
ls.gotoTarget = fs; |
| 635 |
fs.relatedLabeled = s; |
| 636 |
} |
| 637 |
result = s; |
| 638 |
return; |
| 639 |
} |
| 640 |
assert(fs._init is null); |
| 641 |
|
| 642 |
auto sym = new ScopeDsymbol(); |
| 643 |
sym.parent = sc.scopesym; |
| 644 |
sym.endlinnum = fs.endloc.linnum; |
| 645 |
sc = sc.push(sym); |
| 646 |
sc.inLoop = true; |
| 647 |
|
| 648 |
if (fs.condition) |
| 649 |
{ |
| 650 |
if (auto dotid = fs.condition.isDotIdExp()) |
| 651 |
dotid.noderef = true; |
| 652 |
|
| 653 |
// check in syntax level |
| 654 |
fs.condition = checkAssignmentAsCondition(fs.condition, sc); |
| 655 |
|
| 656 |
fs.condition = fs.condition.expressionSemantic(sc); |
| 657 |
fs.condition = resolveProperties(sc, fs.condition); |
| 658 |
if (checkNonAssignmentArrayOp(fs.condition)) |
| 659 |
fs.condition = ErrorExp.get(); |
| 660 |
fs.condition = fs.condition.optimize(WANTvalue); |
| 661 |
fs.condition = checkGC(sc, fs.condition); |
| 662 |
|
| 663 |
fs.condition = fs.condition.toBoolean(sc); |
| 664 |
} |
| 665 |
if (fs.increment) |
| 666 |
{ |
| 667 |
CommaExp.allow(fs.increment); |
| 668 |
fs.increment = fs.increment.expressionSemantic(sc); |
| 669 |
fs.increment = resolveProperties(sc, fs.increment); |
| 670 |
// @@@DEPRECATED_2.112@@@ |
| 671 |
// remove gagging and deprecation() to turn deprecation into an error when |
| 672 |
// deprecation cycle is over |
| 673 |
const olderrors = global.startGagging(); |
| 674 |
discardValue(fs.increment); |
| 675 |
if (global.endGagging(olderrors)) |
| 676 |
deprecation(fs.increment.loc, "`%s` has no effect", fs.increment.toErrMsg()); |
| 677 |
if (checkNonAssignmentArrayOp(fs.increment)) |
| 678 |
fs.increment = ErrorExp.get(); |
| 679 |
fs.increment = fs.increment.optimize(WANTvalue); |
| 680 |
fs.increment = checkGC(sc, fs.increment); |
| 681 |
} |
| 682 |
|
| 683 |
sc.sbreak = fs; |
| 684 |
sc.scontinue = fs; |
| 685 |
if (fs._body) |
| 686 |
fs._body = fs._body.semanticNoScope(sc); |
| 687 |
|
| 688 |
sc.pop(); |
| 689 |
|
| 690 |
if (fs.condition && fs.condition.op == EXP.error || |
| 691 |
fs.increment && fs.increment.op == EXP.error || |
| 692 |
fs._body && fs._body.isErrorStatement()) |
| 693 |
return setError(); |
| 694 |
result = fs; |
| 695 |
} |
| 696 |
|
| 697 |
void visitForeach(ForeachStatement fs) |
| 698 |
{ |
| 699 |
/* https://dlang.org/spec/statement.html#foreach-statement |
| 700 |
*/ |
| 701 |
|
| 702 |
//printf("ForeachStatement::semantic() %p\n", fs); |
| 703 |
|
| 704 |
/****** |
| 705 |
* Issue error if any of the ForeachTypes were not supplied and could not be inferred. |
| 706 |
* Returns: |
| 707 |
* true if error issued |
| 708 |
*/ |
| 709 |
static bool checkForArgTypes(ForeachStatement fs) |
| 710 |
{ |
| 711 |
bool result = false; |
| 712 |
foreach (p; *fs.parameters) |
| 713 |
{ |
| 714 |
if (!p.type) |
| 715 |
{ |
| 716 |
error(fs.loc, "cannot infer type for `foreach` variable `%s`, perhaps set it explicitly", p.ident.toChars()); |
| 717 |
p.type = Type.terror; |
| 718 |
result = true; |
| 719 |
} |
| 720 |
} |
| 721 |
return result; |
| 722 |
} |
| 723 |
|
| 724 |
const loc = fs.loc; |
| 725 |
const dim = fs.parameters.length; |
| 726 |
|
| 727 |
fs.func = sc.func; |
| 728 |
if (fs.func.fes) |
| 729 |
fs.func = fs.func.fes.func; |
| 730 |
|
| 731 |
VarDeclaration vinit = null; |
| 732 |
fs.aggr = fs.aggr.expressionSemantic(sc); |
| 733 |
fs.aggr = resolveProperties(sc, fs.aggr); |
| 734 |
fs.aggr = fs.aggr.optimize(WANTvalue); |
| 735 |
if (fs.aggr.op == EXP.error) |
| 736 |
return setError(); |
| 737 |
Expression oaggr = fs.aggr; // remember original for error messages |
| 738 |
if (fs.aggr.type && fs.aggr.type.toBasetype().isTypeStruct() && |
| 739 |
fs.aggr.type.toBasetype().isTypeStruct().sym.dtor && |
| 740 |
!fs.aggr.isTypeExp() && !fs.aggr.isLvalue()) |
| 741 |
{ |
| 742 |
// https://issues.dlang.org/show_bug.cgi?id=14653 |
| 743 |
// Extend the life of rvalue aggregate till the end of foreach. |
| 744 |
vinit = copyToTemp(STC.rvalue, "__aggr", fs.aggr); |
| 745 |
vinit.endlinnum = fs.endloc.linnum; |
| 746 |
vinit.dsymbolSemantic(sc); |
| 747 |
fs.aggr = new VarExp(fs.aggr.loc, vinit); |
| 748 |
} |
| 749 |
|
| 750 |
/* If aggregate is a vector type, add the .array to make it a static array |
| 751 |
*/ |
| 752 |
if (fs.aggr.type) |
| 753 |
if (auto tv = fs.aggr.type.toBasetype().isTypeVector()) |
| 754 |
{ |
| 755 |
auto vae = new VectorArrayExp(fs.aggr.loc, fs.aggr); |
| 756 |
vae.type = tv.basetype; |
| 757 |
fs.aggr = vae; |
| 758 |
} |
| 759 |
|
| 760 |
Dsymbol sapply = null; // the inferred opApply() or front() function |
| 761 |
if (!inferForeachAggregate(sc, fs.op == TOK.foreach_, fs.aggr, sapply)) |
| 762 |
{ |
| 763 |
assert(oaggr.type); |
| 764 |
|
| 765 |
error(fs.loc, "invalid `%s` aggregate `%s` of type `%s`", |
| 766 |
Token.toChars(fs.op), oaggr.toChars(), oaggr.type.toPrettyChars()); |
| 767 |
|
| 768 |
if (auto ad = isAggregate(fs.aggr.type)) |
| 769 |
{ |
| 770 |
if (fs.op == TOK.foreach_reverse_) |
| 771 |
{ |
| 772 |
fs.loc.errorSupplemental("`foreach_reverse` works with bidirectional ranges"~ |
| 773 |
" (implementing `back` and `popBack`), aggregates implementing" ~ |
| 774 |
" `opApplyReverse`, or the result of an aggregate's `.tupleof` property"); |
| 775 |
fs.loc.errorSupplemental("https://dlang.org/phobos/std_range_primitives.html#isBidirectionalRange"); |
| 776 |
} |
| 777 |
else |
| 778 |
{ |
| 779 |
fs.loc.errorSupplemental("`foreach` works with input ranges"~ |
| 780 |
" (implementing `front` and `popFront`), aggregates implementing" ~ |
| 781 |
" `opApply`, or the result of an aggregate's `.tupleof` property"); |
| 782 |
fs.loc.errorSupplemental("https://dlang.org/phobos/std_range_primitives.html#isInputRange"); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
return setError(); |
| 787 |
} |
| 788 |
|
| 789 |
Dsymbol sapplyOld = sapply; // 'sapply' will be NULL if and after 'inferApplyArgTypes' errors |
| 790 |
|
| 791 |
/* Check for inference errors and apply mutability checks inline */ |
| 792 |
if (!inferApplyArgTypes(fs, sc, sapply)) |
| 793 |
{ |
| 794 |
bool foundMismatch = false; |
| 795 |
size_t foreachParamCount = 0; |
| 796 |
if (sapplyOld) |
| 797 |
{ |
| 798 |
if (FuncDeclaration fd = sapplyOld.isFuncDeclaration()) |
| 799 |
{ |
| 800 |
auto fparameters = fd.getParameterList(); |
| 801 |
|
| 802 |
if (fparameters.length == 1) |
| 803 |
{ |
| 804 |
// first param should be the callback function |
| 805 |
Parameter fparam = fparameters[0]; |
| 806 |
if ((fparam.type.ty == Tpointer || |
| 807 |
fparam.type.ty == Tdelegate) && |
| 808 |
fparam.type.nextOf().isTypeFunction()) |
| 809 |
{ |
| 810 |
auto tf = fparam.type.nextOf().isTypeFunction(); |
| 811 |
foreachParamCount = tf.parameterList.length; |
| 812 |
foundMismatch = true; |
| 813 |
|
| 814 |
// Mutability check |
| 815 |
if (fs.aggr && fs.aggr.type && fd.type && fs.aggr.type.isConst() && !fd.type.isConst()) |
| 816 |
{ |
| 817 |
// First error: The call site |
| 818 |
error(fs.loc, "mutable method `%s.%s` is not callable using a `const` object", |
| 819 |
fd.parent ? fd.parent.toPrettyChars() : "unknown", fd.toChars()); |
| 820 |
|
| 821 |
// Second error: Suggest how to fix |
| 822 |
errorSupplemental(fd.loc, "Consider adding `const` or `inout` here"); |
| 823 |
|
| 824 |
return setError(); |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
//printf("dim = %d, parameters.length = %d\n", dim, parameters.length); |
| 832 |
if (foundMismatch && dim != foreachParamCount) |
| 833 |
{ |
| 834 |
const(char)* plural = foreachParamCount > 1 ? "s" : ""; |
| 835 |
error(fs.loc, "cannot infer argument types, expected %llu argument%s, not %llu", |
| 836 |
cast(ulong) foreachParamCount, plural, cast(ulong) dim); |
| 837 |
} |
| 838 |
else |
| 839 |
error(fs.loc, "cannot uniquely infer `foreach` argument types"); |
| 840 |
|
| 841 |
return setError(); |
| 842 |
} |
| 843 |
|
| 844 |
// If inference succeeds, proceed with post-checks |
| 845 |
if (sapply && sapply.isFuncDeclaration()) |
| 846 |
{ |
| 847 |
FuncDeclaration fd = sapply.isFuncDeclaration(); |
| 848 |
|
| 849 |
if (fs.aggr && fs.aggr.type && fd.type && fs.aggr.type.isConst() && !fd.type.isConst()) |
| 850 |
{ |
| 851 |
// First error: The call site |
| 852 |
error(fs.loc, "mutable method `%s.%s` is not callable using a `const` object", |
| 853 |
fd.parent ? fd.parent.toPrettyChars() : "unknown", fd.toChars()); |
| 854 |
|
| 855 |
// Second error: Suggest how to fix |
| 856 |
errorSupplemental(fd.loc, "Consider adding `const` or `inout` here"); |
| 857 |
|
| 858 |
return setError(); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
Type tab = fs.aggr.type.toBasetype(); |
| 863 |
|
| 864 |
if (tab.ty == Ttuple) // don't generate new scope for tuple loops |
| 865 |
{ |
| 866 |
Statement s = makeTupleForeach(sc, false, false, fs, null, false).statement; |
| 867 |
if (vinit) |
| 868 |
s = new CompoundStatement(loc, new ExpStatement(loc, vinit), s); |
| 869 |
result = s.statementSemantic(sc); |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
auto sym = new ScopeDsymbol(); |
| 874 |
sym.parent = sc.scopesym; |
| 875 |
sym.endlinnum = fs.endloc.linnum; |
| 876 |
auto sc2 = sc.push(sym); |
| 877 |
sc2.inLoop = true; |
| 878 |
|
| 879 |
foreach (Parameter p; *fs.parameters) |
| 880 |
{ |
| 881 |
if (p.storageClass & STC.manifest) |
| 882 |
{ |
| 883 |
error(fs.loc, "cannot declare `enum` loop variables for non-unrolled foreach"); |
| 884 |
} |
| 885 |
if (p.storageClass & STC.alias_) |
| 886 |
{ |
| 887 |
error(fs.loc, "cannot declare `alias` loop variables for non-unrolled foreach"); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
void retError() |
| 892 |
{ |
| 893 |
sc2.pop(); |
| 894 |
result = new ErrorStatement(); |
| 895 |
} |
| 896 |
|
| 897 |
void rangeError() |
| 898 |
{ |
| 899 |
error(fs.loc, "cannot infer argument types"); |
| 900 |
return retError(); |
| 901 |
} |
| 902 |
|
| 903 |
void retStmt(Statement s) |
| 904 |
{ |
| 905 |
if (!s) |
| 906 |
return retError(); |
| 907 |
s = s.statementSemantic(sc2); |
| 908 |
sc2.pop(); |
| 909 |
result = s; |
| 910 |
} |
| 911 |
|
| 912 |
Type tn = null; |
| 913 |
Type tnv = null; |
| 914 |
Statement apply() |
| 915 |
{ |
| 916 |
if (checkForArgTypes(fs)) |
| 917 |
return null; |
| 918 |
|
| 919 |
TypeFunction tfld = null; |
| 920 |
if (sapply) |
| 921 |
{ |
| 922 |
if (auto fdapply = sapply.isFuncDeclaration()) |
| 923 |
{ |
| 924 |
assert(fdapply.type && fdapply.type.isTypeFunction()); |
| 925 |
tfld = fdapply.type.typeSemantic(loc, sc2).isTypeFunction(); |
| 926 |
goto Lget; |
| 927 |
} |
| 928 |
else if (tab.isTypeDelegate()) |
| 929 |
{ |
| 930 |
tfld = tab.nextOf().isTypeFunction(); |
| 931 |
Lget: |
| 932 |
//printf("tfld = %s\n", tfld.toChars()); |
| 933 |
if (tfld.parameterList.parameters.length == 1) |
| 934 |
{ |
| 935 |
Parameter p = tfld.parameterList[0]; |
| 936 |
if (p.type && p.type.isTypeDelegate()) |
| 937 |
{ |
| 938 |
auto t = p.type.typeSemantic(loc, sc2); |
| 939 |
assert(t.ty == Tdelegate); |
| 940 |
tfld = t.nextOf().isTypeFunction(); |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
FuncExp flde = foreachBodyToFunction(sc2, fs, tfld); |
| 947 |
if (!flde) |
| 948 |
return null; |
| 949 |
|
| 950 |
// Resolve any forward referenced goto's |
| 951 |
foreach (ScopeStatement ss; *fs.gotos) |
| 952 |
{ |
| 953 |
GotoStatement gs = ss.statement.isGotoStatement(); |
| 954 |
if (!gs.label.statement) |
| 955 |
{ |
| 956 |
// 'Promote' it to this scope, and replace with a return |
| 957 |
fs.cases.push(gs); |
| 958 |
ss.statement = new ReturnStatement(Loc.initial, new IntegerExp(fs.cases.length + 1)); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
Expression e = null; |
| 963 |
if (vinit) |
| 964 |
{ |
| 965 |
e = new DeclarationExp(loc, vinit); |
| 966 |
e = e.expressionSemantic(sc2); |
| 967 |
if (e.op == EXP.error) |
| 968 |
return null; |
| 969 |
} |
| 970 |
|
| 971 |
Expression ec; |
| 972 |
switch (tab.ty) |
| 973 |
{ |
| 974 |
case Tarray: |
| 975 |
case Tsarray: ec = applyArray (fs, flde, tab, sc2, tn, tnv); break; |
| 976 |
case Tdelegate: ec = applyDelegate (fs, flde, tab, sc2); break; |
| 977 |
case Taarray: ec = applyAssocArray(fs, flde, tab); break; |
| 978 |
default: ec = applyOpApply (fs, flde, tab, sc2, sapply); break; |
| 979 |
} |
| 980 |
if (!ec) |
| 981 |
return null; |
| 982 |
|
| 983 |
e = Expression.combine(e, ec); |
| 984 |
return loopReturn(e, fs.cases, loc); |
| 985 |
} |
| 986 |
|
| 987 |
switch (tab.ty) |
| 988 |
{ |
| 989 |
case Tarray: |
| 990 |
case Tsarray: |
| 991 |
{ |
| 992 |
if (checkForArgTypes(fs)) |
| 993 |
return retError(); |
| 994 |
|
| 995 |
if (dim < 1 || dim > 2) |
| 996 |
{ |
| 997 |
error(fs.loc, "only one or two arguments for array `foreach`"); |
| 998 |
return retError(); |
| 999 |
} |
| 1000 |
|
| 1001 |
// Finish semantic on all foreach parameter types. |
| 1002 |
foreach (i; 0 .. dim) |
| 1003 |
{ |
| 1004 |
Parameter p = (*fs.parameters)[i]; |
| 1005 |
p.type = p.type.typeSemantic(loc, sc2); |
| 1006 |
p.type = p.type.addStorageClass(p.storageClass); |
| 1007 |
} |
| 1008 |
|
| 1009 |
tn = tab.nextOf().toBasetype(); |
| 1010 |
|
| 1011 |
if (dim == 2) |
| 1012 |
{ |
| 1013 |
Type tindex = (*fs.parameters)[0].type; |
| 1014 |
if (!tindex.isIntegral()) |
| 1015 |
{ |
| 1016 |
error(fs.loc, "foreach: key cannot be of non-integral type `%s`", tindex.toChars()); |
| 1017 |
return retError(); |
| 1018 |
} |
| 1019 |
/* What cases to deprecate implicit conversions for: |
| 1020 |
* 1. foreach aggregate is a dynamic array |
| 1021 |
* 2. foreach body is lowered to _aApply (see special case below). |
| 1022 |
*/ |
| 1023 |
Type tv = (*fs.parameters)[1].type.toBasetype(); |
| 1024 |
if ((tab.isTypeDArray() || |
| 1025 |
(tn.ty != tv.ty && tn.ty.isSomeChar && tv.ty.isSomeChar)) && |
| 1026 |
!Type.tsize_t.implicitConvTo(tindex)) |
| 1027 |
{ |
| 1028 |
bool err = true; |
| 1029 |
if (tab.isTypeDArray()) |
| 1030 |
{ |
| 1031 |
// check if overflow is possible |
| 1032 |
const maxLen = IntRange.fromType(tindex).imax.value + 1; |
| 1033 |
if (auto ale = fs.aggr.isArrayLiteralExp()) |
| 1034 |
err = ale.elements.length > maxLen; |
| 1035 |
else if (auto se = fs.aggr.isSliceExp()) |
| 1036 |
err = !(se.upr && se.upr.isConst() && se.upr.toInteger() <= maxLen); |
| 1037 |
} |
| 1038 |
if (err) |
| 1039 |
deprecation(fs.loc, "foreach: loop index implicitly converted from `size_t` to `%s`", |
| 1040 |
tindex.toChars()); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* Look for special case of parsing char types out of char type |
| 1045 |
* array. |
| 1046 |
*/ |
| 1047 |
if (tn.ty.isSomeChar) |
| 1048 |
{ |
| 1049 |
int i = (dim == 1) ? 0 : 1; // index of value |
| 1050 |
Parameter p = (*fs.parameters)[i]; |
| 1051 |
tnv = p.type.toBasetype(); |
| 1052 |
if (tnv.ty != tn.ty && tnv.ty.isSomeChar) |
| 1053 |
{ |
| 1054 |
if (p.storageClass & STC.ref_) |
| 1055 |
{ |
| 1056 |
error(fs.loc, "`foreach`: value of UTF conversion cannot be `ref`"); |
| 1057 |
return retError(); |
| 1058 |
} |
| 1059 |
if (dim == 2) |
| 1060 |
{ |
| 1061 |
p = (*fs.parameters)[0]; |
| 1062 |
if (p.storageClass & STC.ref_) |
| 1063 |
{ |
| 1064 |
error(fs.loc, "`foreach`: key cannot be `ref`"); |
| 1065 |
return retError(); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
return retStmt(apply()); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
// Declare the key |
| 1073 |
if (dim == 2) |
| 1074 |
{ |
| 1075 |
Parameter p = (*fs.parameters)[0]; |
| 1076 |
fs.key = new VarDeclaration(loc, p.type.mutableOf(), Identifier.generateId("__key"), null); |
| 1077 |
fs.key.storage_class |= STC.temp | STC.foreach_; |
| 1078 |
if (fs.key.isReference()) |
| 1079 |
fs.key.storage_class |= STC.nodtor; |
| 1080 |
|
| 1081 |
if (p.storageClass & STC.ref_) |
| 1082 |
{ |
| 1083 |
if (fs.key.type.constConv(p.type) == MATCH.nomatch) |
| 1084 |
{ |
| 1085 |
error(fs.loc, "key type mismatch, `%s` to `ref %s`", |
| 1086 |
fs.key.type.toChars(), p.type.toChars()); |
| 1087 |
return retError(); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
if (auto ta = tab.isTypeSArray()) |
| 1091 |
{ |
| 1092 |
IntRange dimrange = getIntRange(ta.dim); |
| 1093 |
// https://issues.dlang.org/show_bug.cgi?id=12504 |
| 1094 |
dimrange.imax = SignExtendedNumber(dimrange.imax.value-1); |
| 1095 |
if (!IntRange.fromType(fs.key.type).contains(dimrange)) |
| 1096 |
{ |
| 1097 |
error(fs.loc, "index type `%s` cannot cover index range 0..%llu", |
| 1098 |
p.type.toChars(), ta.dim.toInteger()); |
| 1099 |
return retError(); |
| 1100 |
} |
| 1101 |
fs.key.range = new IntRange(SignExtendedNumber(0), dimrange.imax); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
// Now declare the value |
| 1105 |
{ |
| 1106 |
Parameter p = (*fs.parameters)[dim - 1]; |
| 1107 |
fs.value = new VarDeclaration(loc, p.type, p.ident, null); |
| 1108 |
fs.value.storage_class |= STC.foreach_; |
| 1109 |
fs.value.storage_class |= p.storageClass & (STC.scope_ | STC.IOR | STC.TYPECTOR); |
| 1110 |
if (fs.value.isReference()) |
| 1111 |
{ |
| 1112 |
fs.value.storage_class |= STC.nodtor; |
| 1113 |
|
| 1114 |
if (fs.aggr.checkModifiable(sc2, ModifyFlags.noError) == Modifiable.initialization) |
| 1115 |
fs.value.setInCtorOnly = true; |
| 1116 |
|
| 1117 |
Type t = tab.nextOf(); |
| 1118 |
if (t.constConv(p.type) == MATCH.nomatch) |
| 1119 |
{ |
| 1120 |
error(fs.loc, "argument type mismatch, `%s` to `ref %s`", |
| 1121 |
t.toChars(), p.type.toChars()); |
| 1122 |
return retError(); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* Convert to a ForStatement |
| 1128 |
* foreach (key, value; a) body => |
| 1129 |
* for (T[] tmp = a[], size_t key; key < tmp.length; ++key) |
| 1130 |
* { T value = tmp[k]; body } |
| 1131 |
* |
| 1132 |
* foreach_reverse (key, value; a) body => |
| 1133 |
* for (T[] tmp = a[], size_t key = tmp.length; key--; ) |
| 1134 |
* { T value = tmp[k]; body } |
| 1135 |
*/ |
| 1136 |
auto id = Identifier.generateId("__r"); |
| 1137 |
auto ie = new ExpInitializer(loc, new SliceExp(loc, fs.aggr, null, null)); |
| 1138 |
const valueIsRef = (*fs.parameters)[$ - 1].isReference(); |
| 1139 |
VarDeclaration tmp; |
| 1140 |
if (fs.aggr.isArrayLiteralExp() && !valueIsRef) |
| 1141 |
{ |
| 1142 |
auto ale = fs.aggr.isArrayLiteralExp(); |
| 1143 |
size_t edim = ale.elements ? ale.elements.length : 0; |
| 1144 |
auto telem = (*fs.parameters)[dim - 1].type; |
| 1145 |
|
| 1146 |
// https://issues.dlang.org/show_bug.cgi?id=12936 |
| 1147 |
// if telem has been specified explicitly, |
| 1148 |
// converting array literal elements to telem might make it @nogc. |
| 1149 |
fs.aggr = fs.aggr.implicitCastTo(sc, telem.sarrayOf(edim)); |
| 1150 |
if (fs.aggr.op == EXP.error) |
| 1151 |
return retError(); |
| 1152 |
|
| 1153 |
// for (T[edim] tmp = a, ...) |
| 1154 |
tmp = new VarDeclaration(loc, fs.aggr.type, id, ie); |
| 1155 |
} |
| 1156 |
else |
| 1157 |
{ |
| 1158 |
tmp = new VarDeclaration(loc, tab.nextOf().arrayOf(), id, ie); |
| 1159 |
if (!valueIsRef) |
| 1160 |
tmp.storage_class |= STC.scope_; |
| 1161 |
} |
| 1162 |
tmp.storage_class |= STC.temp; |
| 1163 |
|
| 1164 |
Expression tmp_length = new DotIdExp(loc, new VarExp(loc, tmp), Id.length); |
| 1165 |
|
| 1166 |
if (!fs.key) |
| 1167 |
{ |
| 1168 |
Identifier idkey = Identifier.generateId("__key"); |
| 1169 |
fs.key = new VarDeclaration(loc, Type.tsize_t, idkey, null); |
| 1170 |
fs.key.storage_class |= STC.temp; |
| 1171 |
} |
| 1172 |
else if (fs.key.type.ty != Type.tsize_t.ty) |
| 1173 |
{ |
| 1174 |
tmp_length = new CastExp(loc, tmp_length, fs.key.type); |
| 1175 |
} |
| 1176 |
if (fs.op == TOK.foreach_reverse_) |
| 1177 |
fs.key._init = new ExpInitializer(loc, tmp_length); |
| 1178 |
else |
| 1179 |
fs.key._init = new ExpInitializer(loc, new IntegerExp(loc, 0, fs.key.type)); |
| 1180 |
|
| 1181 |
auto cs = new Statements(); |
| 1182 |
if (vinit) |
| 1183 |
cs.push(new ExpStatement(loc, vinit)); |
| 1184 |
cs.push(new ExpStatement(loc, tmp)); |
| 1185 |
cs.push(new ExpStatement(loc, fs.key)); |
| 1186 |
Statement forinit = new CompoundDeclarationStatement(loc, cs); |
| 1187 |
|
| 1188 |
Expression cond; |
| 1189 |
if (fs.op == TOK.foreach_reverse_) |
| 1190 |
{ |
| 1191 |
// key-- |
| 1192 |
cond = new PostExp(EXP.minusMinus, loc, new VarExp(loc, fs.key)); |
| 1193 |
} |
| 1194 |
else |
| 1195 |
{ |
| 1196 |
// key < tmp.length |
| 1197 |
cond = new CmpExp(EXP.lessThan, loc, new VarExp(loc, fs.key), tmp_length); |
| 1198 |
} |
| 1199 |
|
| 1200 |
Expression increment = null; |
| 1201 |
if (fs.op == TOK.foreach_) |
| 1202 |
{ |
| 1203 |
// key += 1 |
| 1204 |
increment = new AddAssignExp(loc, new VarExp(loc, fs.key), new IntegerExp(loc, 1, fs.key.type)); |
| 1205 |
} |
| 1206 |
|
| 1207 |
// T value = tmp[key]; |
| 1208 |
IndexExp indexExp = new IndexExp(loc, new VarExp(loc, tmp), new VarExp(loc, fs.key)); |
| 1209 |
indexExp.indexIsInBounds = true; // disabling bounds checking in foreach statements. |
| 1210 |
fs.value._init = new ExpInitializer(loc, indexExp); |
| 1211 |
Statement ds = new ExpStatement(loc, fs.value); |
| 1212 |
|
| 1213 |
if (dim == 2) |
| 1214 |
{ |
| 1215 |
Parameter p = (*fs.parameters)[0]; |
| 1216 |
if ((p.storageClass & STC.ref_) && p.type.equals(fs.key.type)) |
| 1217 |
{ |
| 1218 |
fs.key.range = null; |
| 1219 |
auto v = new AliasDeclaration(loc, p.ident, fs.key); |
| 1220 |
fs._body = new CompoundStatement(loc, new ExpStatement(loc, v), fs._body); |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
auto ei = new ExpInitializer(loc, new IdentifierExp(loc, fs.key.ident)); |
| 1225 |
auto v = new VarDeclaration(loc, p.type, p.ident, ei); |
| 1226 |
v.storage_class |= STC.foreach_ | (p.storageClass & STC.ref_); |
| 1227 |
fs._body = new CompoundStatement(loc, new ExpStatement(loc, v), fs._body); |
| 1228 |
if (fs.key.range && !p.type.isMutable()) |
| 1229 |
{ |
| 1230 |
/* Limit the range of the key to the specified range |
| 1231 |
*/ |
| 1232 |
v.range = new IntRange(fs.key.range.imin, fs.key.range.imax - SignExtendedNumber(1)); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
} |
| 1236 |
fs._body = new CompoundStatement(loc, ds, fs._body); |
| 1237 |
|
| 1238 |
Statement s = new ForStatement(loc, forinit, cond, increment, fs._body, fs.endloc); |
| 1239 |
if (auto ls = checkLabeledLoop(sc, fs)) // https://issues.dlang.org/show_bug.cgi?id=15450 |
| 1240 |
// don't use sc2 |
| 1241 |
ls.gotoTarget = s; |
| 1242 |
return retStmt(s); |
| 1243 |
} |
| 1244 |
case Taarray: |
| 1245 |
if (fs.op == TOK.foreach_reverse_) |
| 1246 |
error(fs.loc, "cannot use `foreach_reverse` with an associative array"); |
| 1247 |
if (checkForArgTypes(fs)) |
| 1248 |
return retError(); |
| 1249 |
|
| 1250 |
if (dim < 1 || dim > 2) |
| 1251 |
{ |
| 1252 |
error(fs.loc, "only one or two arguments for associative array `foreach`"); |
| 1253 |
return retError(); |
| 1254 |
} |
| 1255 |
return retStmt(apply()); |
| 1256 |
|
| 1257 |
case Tclass: |
| 1258 |
case Tstruct: |
| 1259 |
/* Prefer using opApply, if it exists |
| 1260 |
*/ |
| 1261 |
if (sapply) |
| 1262 |
return retStmt(apply()); |
| 1263 |
{ |
| 1264 |
/* Look for range iteration, i.e. the properties |
| 1265 |
* .empty, .popFront, .popBack, .front and .back |
| 1266 |
* foreach (e; aggr) { ... } |
| 1267 |
* translates to: |
| 1268 |
* for (auto __r = aggr[]; !__r.empty; __r.popFront()) { |
| 1269 |
* auto e = __r.front; |
| 1270 |
* ... |
| 1271 |
* } |
| 1272 |
*/ |
| 1273 |
auto ad = (tab.ty == Tclass) ? |
| 1274 |
cast(AggregateDeclaration)tab.isTypeClass().sym : |
| 1275 |
cast(AggregateDeclaration)tab.isTypeStruct().sym; |
| 1276 |
Identifier idfront; |
| 1277 |
Identifier idpopFront; |
| 1278 |
if (fs.op == TOK.foreach_) |
| 1279 |
{ |
| 1280 |
idfront = Id.Ffront; |
| 1281 |
idpopFront = Id.FpopFront; |
| 1282 |
} |
| 1283 |
else |
| 1284 |
{ |
| 1285 |
idfront = Id.Fback; |
| 1286 |
idpopFront = Id.FpopBack; |
| 1287 |
} |
| 1288 |
auto sfront = ad.search(Loc.initial, idfront); |
| 1289 |
if (!sfront) |
| 1290 |
return retStmt(apply()); |
| 1291 |
|
| 1292 |
/* Generate a temporary __r and initialize it with the aggregate. |
| 1293 |
*/ |
| 1294 |
VarDeclaration r; |
| 1295 |
Statement _init; |
| 1296 |
if (vinit && fs.aggr.isVarExp() && fs.aggr.isVarExp().var == vinit) |
| 1297 |
{ |
| 1298 |
r = vinit; |
| 1299 |
_init = new ExpStatement(loc, vinit); |
| 1300 |
} |
| 1301 |
else |
| 1302 |
{ |
| 1303 |
r = copyToTemp(STC.none, "__r", fs.aggr); |
| 1304 |
r.dsymbolSemantic(sc); |
| 1305 |
_init = new ExpStatement(loc, r); |
| 1306 |
if (vinit) |
| 1307 |
_init = new CompoundStatement(loc, new ExpStatement(loc, vinit), _init); |
| 1308 |
} |
| 1309 |
|
| 1310 |
// !__r.empty |
| 1311 |
Expression e = new VarExp(loc, r); |
| 1312 |
e = new DotIdExp(loc, e, Id.Fempty); |
| 1313 |
Expression condition = new NotExp(loc, e); |
| 1314 |
|
| 1315 |
// __r.idpopFront() |
| 1316 |
e = new VarExp(loc, r); |
| 1317 |
Expression increment = new CallExp(loc, new DotIdExp(loc, e, idpopFront)); |
| 1318 |
|
| 1319 |
/* Declaration statement for e: |
| 1320 |
* auto e = __r.idfront; |
| 1321 |
*/ |
| 1322 |
e = new VarExp(loc, r); |
| 1323 |
Expression einit = new DotIdExp(loc, e, idfront); |
| 1324 |
Statement makeargs, forbody; |
| 1325 |
bool ignoreRef = false; // If a range returns a non-ref front we ignore ref on foreach |
| 1326 |
|
| 1327 |
Type tfront; |
| 1328 |
if (auto fd = sfront.isFuncDeclaration()) |
| 1329 |
{ |
| 1330 |
if (!functionSemantic(fd)) |
| 1331 |
return rangeError(); |
| 1332 |
tfront = fd.type; |
| 1333 |
} |
| 1334 |
else if (auto td = sfront.isTemplateDeclaration()) |
| 1335 |
{ |
| 1336 |
if (auto f = resolveFuncCall(loc, sc, td, null, tab, ArgumentList(), FuncResolveFlag.quiet)) |
| 1337 |
tfront = f.type; |
| 1338 |
} |
| 1339 |
else if (auto d = sfront.toAlias().isDeclaration()) |
| 1340 |
{ |
| 1341 |
tfront = d.type; |
| 1342 |
} |
| 1343 |
if (!tfront || tfront.ty == Terror) |
| 1344 |
return rangeError(); |
| 1345 |
if (auto ftt = tfront.toBasetype().isTypeFunction()) |
| 1346 |
{ |
| 1347 |
tfront = tfront.toBasetype().nextOf(); |
| 1348 |
if (!ftt.isRef) |
| 1349 |
{ |
| 1350 |
// .front() does not return a ref. We ignore ref on foreach arg. |
| 1351 |
// see https://issues.dlang.org/show_bug.cgi?id=11934 |
| 1352 |
if (tfront.needsDestruction()) ignoreRef = true; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
if (tfront.ty == Tvoid) |
| 1356 |
{ |
| 1357 |
error(fs.loc, "`%s.front` is `void` and has no value", oaggr.toChars()); |
| 1358 |
return retError(); |
| 1359 |
} |
| 1360 |
|
| 1361 |
if (dim == 1) |
| 1362 |
{ |
| 1363 |
auto p = (*fs.parameters)[0]; |
| 1364 |
auto ve = new VarDeclaration(loc, p.type, p.ident, new ExpInitializer(loc, einit)); |
| 1365 |
ve.storage_class |= STC.foreach_; |
| 1366 |
ve.storage_class |= p.storageClass & (STC.scope_ | STC.IOR | STC.TYPECTOR); |
| 1367 |
|
| 1368 |
if (ignoreRef) |
| 1369 |
ve.storage_class &= ~STC.ref_; |
| 1370 |
|
| 1371 |
makeargs = new ExpStatement(loc, ve); |
| 1372 |
} |
| 1373 |
else |
| 1374 |
{ |
| 1375 |
auto vd = copyToTemp(STC.ref_, "__front", einit); |
| 1376 |
vd.dsymbolSemantic(sc); |
| 1377 |
makeargs = new ExpStatement(loc, vd); |
| 1378 |
|
| 1379 |
// Resolve inout qualifier of front type |
| 1380 |
tfront = tfront.substWildTo(tab.mod); |
| 1381 |
|
| 1382 |
Expression ve = new VarExp(loc, vd); |
| 1383 |
ve.type = tfront; |
| 1384 |
|
| 1385 |
auto exps = new Expressions(); |
| 1386 |
exps.push(ve); |
| 1387 |
int pos = 0; |
| 1388 |
while (exps.length < dim) |
| 1389 |
{ |
| 1390 |
pos = expandAliasThisTuples(exps, pos); |
| 1391 |
if (pos == -1) |
| 1392 |
break; |
| 1393 |
} |
| 1394 |
if (exps.length != dim) |
| 1395 |
{ |
| 1396 |
const(char)* plural = exps.length > 1 ? "s" : ""; |
| 1397 |
error(fs.loc, "cannot infer argument types, expected %llu argument%s, not %llu", |
| 1398 |
cast(ulong) exps.length, plural, cast(ulong) dim); |
| 1399 |
return retError(); |
| 1400 |
} |
| 1401 |
|
| 1402 |
foreach (i; 0 .. dim) |
| 1403 |
{ |
| 1404 |
auto p = (*fs.parameters)[i]; |
| 1405 |
auto exp = (*exps)[i]; |
| 1406 |
version (none) |
| 1407 |
{ |
| 1408 |
printf("[%lu] p = %s %s, exp = %s %s\n", i, |
| 1409 |
p.type ? p.type.toChars() : "?", p.ident.toChars(), |
| 1410 |
exp.type.toChars(), exp.toChars()); |
| 1411 |
} |
| 1412 |
if (!p.type) |
| 1413 |
p.type = exp.type; |
| 1414 |
|
| 1415 |
auto sc = p.storageClass; |
| 1416 |
if (ignoreRef) sc &= ~STC.ref_; |
| 1417 |
p.type = p.type.addStorageClass(sc).typeSemantic(loc, sc2); |
| 1418 |
if (!exp.implicitConvTo(p.type)) |
| 1419 |
{ |
| 1420 |
error(fs.loc, "cannot implicitly convert tuple element of type `%s` to variable `%s` of type `%s`", |
| 1421 |
exp.type.toChars(), p.toChars(), p.type.toChars()); |
| 1422 |
return retError(); |
| 1423 |
} |
| 1424 |
|
| 1425 |
auto var = new VarDeclaration(loc, p.type, p.ident, new ExpInitializer(loc, exp)); |
| 1426 |
var.storage_class |= STC.ctfe | STC.ref_ | STC.foreach_; |
| 1427 |
makeargs = new CompoundStatement(loc, makeargs, new ExpStatement(loc, var)); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
forbody = new CompoundStatement(loc, makeargs, fs._body); |
| 1432 |
|
| 1433 |
Statement s = new ForStatement(loc, _init, condition, increment, forbody, fs.endloc); |
| 1434 |
if (auto ls = checkLabeledLoop(sc, fs)) |
| 1435 |
ls.gotoTarget = s; |
| 1436 |
|
| 1437 |
version (none) |
| 1438 |
{ |
| 1439 |
printf("init: %s\n", toChars(_init)); |
| 1440 |
printf("condition: %s\n", condition.toChars()); |
| 1441 |
printf("increment: %s\n", increment.toChars()); |
| 1442 |
printf("body: %s\n", forbody.toChars()); |
| 1443 |
} |
| 1444 |
return retStmt(s); |
| 1445 |
} |
| 1446 |
case Tdelegate: |
| 1447 |
if (fs.op == TOK.foreach_reverse_) |
| 1448 |
error(fs.loc, "cannot use `foreach_reverse` with a delegate"); |
| 1449 |
return retStmt(apply()); |
| 1450 |
case Terror: |
| 1451 |
return retError(); |
| 1452 |
default: |
| 1453 |
error(fs.loc, "`foreach`: `%s` is not an aggregate type", fs.aggr.type.toChars()); |
| 1454 |
return retError(); |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
void visitForeachRange(ForeachRangeStatement fs) |
| 1459 |
{ |
| 1460 |
/* https://dlang.org/spec/statement.html#foreach-range-statement |
| 1461 |
*/ |
| 1462 |
|
| 1463 |
//printf("ForeachRangeStatement::semantic() %p\n", fs); |
| 1464 |
auto loc = fs.loc; |
| 1465 |
fs.lwr = fs.lwr.expressionSemantic(sc); |
| 1466 |
fs.lwr = resolveProperties(sc, fs.lwr); |
| 1467 |
fs.lwr = fs.lwr.optimize(WANTvalue); |
| 1468 |
if (!fs.lwr.type) |
| 1469 |
{ |
| 1470 |
error(fs.loc, "invalid range lower bound `%s`", fs.lwr.toChars()); |
| 1471 |
return setError(); |
| 1472 |
} |
| 1473 |
|
| 1474 |
fs.upr = fs.upr.expressionSemantic(sc); |
| 1475 |
fs.upr = resolveProperties(sc, fs.upr); |
| 1476 |
fs.upr = fs.upr.optimize(WANTvalue); |
| 1477 |
if (!fs.upr.type) |
| 1478 |
{ |
| 1479 |
error(fs.loc, "invalid range upper bound `%s`", fs.upr.toChars()); |
| 1480 |
return setError(); |
| 1481 |
} |
| 1482 |
|
| 1483 |
if (fs.param.type) |
| 1484 |
{ |
| 1485 |
fs.param.type = fs.param.type.typeSemantic(loc, sc); |
| 1486 |
fs.param.type = fs.param.type.addStorageClass(fs.param.storageClass); |
| 1487 |
fs.lwr = fs.lwr.implicitCastTo(sc, fs.param.type); |
| 1488 |
|
| 1489 |
if (fs.upr.implicitConvTo(fs.param.type) || (fs.param.storageClass & STC.ref_)) |
| 1490 |
{ |
| 1491 |
fs.upr = fs.upr.implicitCastTo(sc, fs.param.type); |
| 1492 |
} |
| 1493 |
else |
| 1494 |
{ |
| 1495 |
// See if upr-1 fits in param.type |
| 1496 |
Expression limit = new MinExp(loc, fs.upr, IntegerExp.literal!1); |
| 1497 |
limit = limit.expressionSemantic(sc); |
| 1498 |
limit = limit.optimize(WANTvalue); |
| 1499 |
if (!limit.implicitConvTo(fs.param.type)) |
| 1500 |
{ |
| 1501 |
fs.upr = fs.upr.implicitCastTo(sc, fs.param.type); |
| 1502 |
} |
| 1503 |
} |
| 1504 |
} |
| 1505 |
else |
| 1506 |
{ |
| 1507 |
/* Must infer types from lwr and upr |
| 1508 |
*/ |
| 1509 |
Type tlwr = fs.lwr.type.toBasetype(); |
| 1510 |
if (tlwr.ty == Tstruct || tlwr.ty == Tclass) |
| 1511 |
{ |
| 1512 |
/* Just picking the first really isn't good enough. |
| 1513 |
*/ |
| 1514 |
fs.param.type = fs.lwr.type; |
| 1515 |
} |
| 1516 |
else if (fs.lwr.type == fs.upr.type) |
| 1517 |
{ |
| 1518 |
/* Same logic as CondExp ?lwr:upr |
| 1519 |
*/ |
| 1520 |
fs.param.type = fs.lwr.type; |
| 1521 |
} |
| 1522 |
else |
| 1523 |
{ |
| 1524 |
scope AddExp ea = new AddExp(loc, fs.lwr, fs.upr); |
| 1525 |
if (typeCombine(ea, sc)) |
| 1526 |
return setError(); |
| 1527 |
fs.param.type = ea.type; |
| 1528 |
fs.lwr = ea.e1; |
| 1529 |
fs.upr = ea.e2; |
| 1530 |
} |
| 1531 |
fs.param.type = fs.param.type.addStorageClass(fs.param.storageClass); |
| 1532 |
} |
| 1533 |
if (fs.param.type.ty == Terror || fs.lwr.op == EXP.error || fs.upr.op == EXP.error) |
| 1534 |
{ |
| 1535 |
return setError(); |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* Convert to a for loop: |
| 1539 |
* foreach (key; lwr .. upr) => |
| 1540 |
* for (auto key = lwr, auto tmp = upr; key < tmp; ++key) |
| 1541 |
* |
| 1542 |
* foreach_reverse (key; lwr .. upr) => |
| 1543 |
* for (auto tmp = lwr, auto key = upr; key-- > tmp;) |
| 1544 |
*/ |
| 1545 |
auto ie = new ExpInitializer(loc, (fs.op == TOK.foreach_) ? fs.lwr : fs.upr); |
| 1546 |
fs.key = new VarDeclaration(loc, fs.upr.type.mutableOf(), Identifier.generateId("__key"), ie); |
| 1547 |
fs.key.storage_class |= STC.temp; |
| 1548 |
SignExtendedNumber lower = getIntRange(fs.lwr).imin; |
| 1549 |
SignExtendedNumber upper = getIntRange(fs.upr).imax; |
| 1550 |
if (lower <= upper) |
| 1551 |
{ |
| 1552 |
fs.key.range = new IntRange(lower, upper); |
| 1553 |
} |
| 1554 |
|
| 1555 |
Identifier id = Identifier.generateId("__limit"); |
| 1556 |
ie = new ExpInitializer(loc, (fs.op == TOK.foreach_) ? fs.upr : fs.lwr); |
| 1557 |
auto tmp = new VarDeclaration(loc, fs.upr.type, id, ie); |
| 1558 |
tmp.storage_class |= STC.temp; |
| 1559 |
|
| 1560 |
auto cs = new Statements(); |
| 1561 |
// Keep order of evaluation as lwr, then upr |
| 1562 |
if (fs.op == TOK.foreach_) |
| 1563 |
{ |
| 1564 |
cs.push(new ExpStatement(loc, fs.key)); |
| 1565 |
cs.push(new ExpStatement(loc, tmp)); |
| 1566 |
} |
| 1567 |
else |
| 1568 |
{ |
| 1569 |
cs.push(new ExpStatement(loc, tmp)); |
| 1570 |
cs.push(new ExpStatement(loc, fs.key)); |
| 1571 |
} |
| 1572 |
Statement forinit = new CompoundDeclarationStatement(loc, cs); |
| 1573 |
|
| 1574 |
Expression cond; |
| 1575 |
if (fs.op == TOK.foreach_reverse_) |
| 1576 |
{ |
| 1577 |
cond = new PostExp(EXP.minusMinus, loc, new VarExp(loc, fs.key)); |
| 1578 |
if (fs.param.type.isScalar()) |
| 1579 |
{ |
| 1580 |
// key-- > tmp |
| 1581 |
cond = new CmpExp(EXP.greaterThan, loc, cond, new VarExp(loc, tmp)); |
| 1582 |
} |
| 1583 |
else |
| 1584 |
{ |
| 1585 |
// key-- != tmp |
| 1586 |
cond = new EqualExp(EXP.notEqual, loc, cond, new VarExp(loc, tmp)); |
| 1587 |
} |
| 1588 |
} |
| 1589 |
else |
| 1590 |
{ |
| 1591 |
if (fs.param.type.isScalar()) |
| 1592 |
{ |
| 1593 |
// key < tmp |
| 1594 |
cond = new CmpExp(EXP.lessThan, loc, new VarExp(loc, fs.key), new VarExp(loc, tmp)); |
| 1595 |
} |
| 1596 |
else |
| 1597 |
{ |
| 1598 |
// key != tmp |
| 1599 |
cond = new EqualExp(EXP.notEqual, loc, new VarExp(loc, fs.key), new VarExp(loc, tmp)); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
Expression increment = null; |
| 1604 |
if (fs.op == TOK.foreach_) |
| 1605 |
{ |
| 1606 |
// key += 1 |
| 1607 |
//increment = new AddAssignExp(loc, new VarExp(loc, fs.key), IntegerExp.literal!1); |
| 1608 |
increment = new PreExp(EXP.prePlusPlus, loc, new VarExp(loc, fs.key)); |
| 1609 |
} |
| 1610 |
if ((fs.param.storageClass & STC.ref_) && fs.param.type.equals(fs.key.type)) |
| 1611 |
{ |
| 1612 |
fs.key.range = null; |
| 1613 |
auto v = new AliasDeclaration(loc, fs.param.ident, fs.key); |
| 1614 |
fs._body = new CompoundStatement(loc, new ExpStatement(loc, v), fs._body); |
| 1615 |
} |
| 1616 |
else |
| 1617 |
{ |
| 1618 |
ie = new ExpInitializer(loc, new CastExp(loc, new VarExp(loc, fs.key), fs.param.type)); |
| 1619 |
auto v = new VarDeclaration(loc, fs.param.type, fs.param.ident, ie); |
| 1620 |
v.storage_class |= STC.temp | STC.foreach_ | (fs.param.storageClass & STC.ref_); |
| 1621 |
fs._body = new CompoundStatement(loc, new ExpStatement(loc, v), fs._body); |
| 1622 |
if (fs.key.range && !fs.param.type.isMutable()) |
| 1623 |
{ |
| 1624 |
/* Limit the range of the key to the specified range |
| 1625 |
*/ |
| 1626 |
v.range = new IntRange(fs.key.range.imin, fs.key.range.imax - SignExtendedNumber(1)); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
if (fs.param.storageClass & STC.ref_) |
| 1630 |
{ |
| 1631 |
if (fs.key.type.constConv(fs.param.type) == MATCH.nomatch) |
| 1632 |
{ |
| 1633 |
error(fs.loc, "argument type mismatch, `%s` to `ref %s`", fs.key.type.toChars(), fs.param.type.toChars()); |
| 1634 |
return setError(); |
| 1635 |
} |
| 1636 |
} |
| 1637 |
|
| 1638 |
auto s = new ForStatement(loc, forinit, cond, increment, fs._body, fs.endloc); |
| 1639 |
if (LabelStatement ls = checkLabeledLoop(sc, fs)) |
| 1640 |
ls.gotoTarget = s; |
| 1641 |
result = s.statementSemantic(sc); |
| 1642 |
} |
| 1643 |
|
| 1644 |
void visitIf(IfStatement ifs) |
| 1645 |
{ |
| 1646 |
/* https://dlang.org/spec/statement.html#IfStatement |
| 1647 |
*/ |
| 1648 |
|
| 1649 |
// check in syntax level |
| 1650 |
ifs.condition = checkAssignmentAsCondition(ifs.condition, sc); |
| 1651 |
|
| 1652 |
auto sym = new ScopeDsymbol(); |
| 1653 |
sym.parent = sc.scopesym; |
| 1654 |
sym.endlinnum = ifs.endloc.linnum; |
| 1655 |
Scope* scd = sc.push(sym); |
| 1656 |
if (ifs.param) |
| 1657 |
{ |
| 1658 |
/* Declare param, which we will set to be the |
| 1659 |
* result of condition. |
| 1660 |
*/ |
| 1661 |
auto ei = new ExpInitializer(ifs.loc, ifs.condition); |
| 1662 |
ifs.match = new VarDeclaration(ifs.loc, ifs.param.type, ifs.param.ident, ei); |
| 1663 |
ifs.match.parent = scd.func; |
| 1664 |
ifs.match.storage_class |= ifs.param.storageClass; |
| 1665 |
ifs.match.dsymbolSemantic(scd); |
| 1666 |
|
| 1667 |
auto de = new DeclarationExp(ifs.loc, ifs.match); |
| 1668 |
auto ve = new VarExp(ifs.loc, ifs.match); |
| 1669 |
ifs.condition = new CommaExp(ifs.loc, de, ve); |
| 1670 |
ifs.condition = ifs.condition.expressionSemantic(scd); |
| 1671 |
|
| 1672 |
if (ifs.match.edtor) |
| 1673 |
{ |
| 1674 |
Statement sdtor = new DtorExpStatement(ifs.loc, ifs.match.edtor, ifs.match); |
| 1675 |
sdtor = new ScopeGuardStatement(ifs.loc, TOK.onScopeExit, sdtor); |
| 1676 |
ifs.ifbody = new CompoundStatement(ifs.loc, sdtor, ifs.ifbody); |
| 1677 |
ifs.match.storage_class |= STC.nodtor; |
| 1678 |
|
| 1679 |
// the destructor is always called |
| 1680 |
// whether the 'ifbody' is executed or not |
| 1681 |
Statement sdtor2 = new DtorExpStatement(ifs.loc, ifs.match.edtor, ifs.match); |
| 1682 |
if (ifs.elsebody) |
| 1683 |
ifs.elsebody = new CompoundStatement(ifs.loc, sdtor2, ifs.elsebody); |
| 1684 |
else |
| 1685 |
ifs.elsebody = sdtor2; |
| 1686 |
} |
| 1687 |
} |
| 1688 |
else |
| 1689 |
{ |
| 1690 |
if (auto dotid = ifs.condition.isDotIdExp()) |
| 1691 |
dotid.noderef = true; |
| 1692 |
|
| 1693 |
ifs.condition = ifs.condition.expressionSemantic(scd); |
| 1694 |
ifs.condition = resolveProperties(scd, ifs.condition); |
| 1695 |
ifs.condition = ifs.condition.addDtorHook(scd); |
| 1696 |
} |
| 1697 |
if (checkNonAssignmentArrayOp(ifs.condition)) |
| 1698 |
ifs.condition = ErrorExp.get(); |
| 1699 |
|
| 1700 |
// Convert to boolean after declaring param so this works: |
| 1701 |
// if (S param = S()) {} |
| 1702 |
// where S is a struct that defines opCast!bool. |
| 1703 |
ifs.condition = ifs.condition.toBoolean(scd); |
| 1704 |
|
| 1705 |
// If we can short-circuit evaluate the if statement, don't do the |
| 1706 |
// semantic analysis of the skipped code. |
| 1707 |
// This feature allows a limited form of conditional compilation. |
| 1708 |
ifs.condition = ifs.condition.optimize(WANTvalue); |
| 1709 |
|
| 1710 |
// checkGC after optimizing the condition so that |
| 1711 |
// compile time constants are reduced. |
| 1712 |
ifs.condition = checkGC(scd, ifs.condition); |
| 1713 |
|
| 1714 |
// Save 'root' of two branches (then and else) at the point where it forks |
| 1715 |
CtorFlow ctorflow_root = scd.ctorflow.clone(); |
| 1716 |
|
| 1717 |
/* Rewrite `if (!__ctfe) A else B` as `if (__ctfe) B else A` |
| 1718 |
*/ |
| 1719 |
NotExp notExp; |
| 1720 |
if (ifs.elsebody && |
| 1721 |
(notExp = ifs.condition.isNotExp()) !is null && |
| 1722 |
notExp.e1.isVarExp() && |
| 1723 |
notExp.e1.isVarExp().var.ident == Id.ctfe) |
| 1724 |
{ |
| 1725 |
ifs.condition = notExp.e1; |
| 1726 |
auto sbody = ifs.ifbody; |
| 1727 |
ifs.ifbody = ifs.elsebody; |
| 1728 |
ifs.elsebody = sbody; |
| 1729 |
} |
| 1730 |
|
| 1731 |
/* Detect `if (__ctfe)` |
| 1732 |
*/ |
| 1733 |
if (ifs.isIfCtfeBlock()) |
| 1734 |
{ |
| 1735 |
Scope* scd2 = scd.push(); |
| 1736 |
scd2.ctfeBlock = true; |
| 1737 |
ifs.ifbody = ifs.ifbody.semanticNoScope(scd2); |
| 1738 |
scd2.pop(); |
| 1739 |
} |
| 1740 |
else |
| 1741 |
ifs.ifbody = ifs.ifbody.semanticNoScope(scd); |
| 1742 |
scd.pop(); |
| 1743 |
|
| 1744 |
CtorFlow ctorflow_then = sc.ctorflow; // move flow results |
| 1745 |
sc.ctorflow = ctorflow_root; // reset flow analysis back to root |
| 1746 |
if (ifs.elsebody) |
| 1747 |
ifs.elsebody = ifs.elsebody.semanticScope(sc, null, null, null); |
| 1748 |
|
| 1749 |
// Merge 'then' results into 'else' results |
| 1750 |
sc.merge(ifs.loc, ctorflow_then); |
| 1751 |
|
| 1752 |
ctorflow_then.freeFieldinit(); // free extra copy of the data |
| 1753 |
|
| 1754 |
if (ifs.condition.op == EXP.error || |
| 1755 |
(ifs.ifbody && ifs.ifbody.isErrorStatement()) || |
| 1756 |
(ifs.elsebody && ifs.elsebody.isErrorStatement())) |
| 1757 |
{ |
| 1758 |
return setError(); |
| 1759 |
} |
| 1760 |
result = ifs; |
| 1761 |
} |
| 1762 |
|
| 1763 |
void visitConditional(ConditionalStatement cs) |
| 1764 |
{ |
| 1765 |
//printf("ConditionalStatement::semantic()\n"); |
| 1766 |
|
| 1767 |
// If we can short-circuit evaluate the if statement, don't do the |
| 1768 |
// semantic analysis of the skipped code. |
| 1769 |
// This feature allows a limited form of conditional compilation. |
| 1770 |
if (cs.condition.include(sc)) |
| 1771 |
{ |
| 1772 |
if (DebugCondition dc = cs.condition.isDebugCondition()) |
| 1773 |
{ |
| 1774 |
sc = sc.push(); |
| 1775 |
sc.debug_ = true; |
| 1776 |
cs.ifbody = cs.ifbody.statementSemantic(sc); |
| 1777 |
sc.pop(); |
| 1778 |
} |
| 1779 |
else |
| 1780 |
cs.ifbody = cs.ifbody.statementSemantic(sc); |
| 1781 |
result = cs.ifbody; |
| 1782 |
} |
| 1783 |
else |
| 1784 |
{ |
| 1785 |
if (cs.elsebody) |
| 1786 |
cs.elsebody = cs.elsebody.statementSemantic(sc); |
| 1787 |
result = cs.elsebody; |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
void visitPragma(PragmaStatement ps) |
| 1792 |
{ |
| 1793 |
/* https://dlang.org/spec/statement.html#pragma-statement |
| 1794 |
*/ |
| 1795 |
import dmd.pragmasem : pragmaStmtSemantic; |
| 1796 |
if (!pragmaStmtSemantic(ps, sc)) |
| 1797 |
return setError(); |
| 1798 |
|
| 1799 |
result = ps._body; |
| 1800 |
} |
| 1801 |
|
| 1802 |
void visitStaticAssert(StaticAssertStatement s) |
| 1803 |
{ |
| 1804 |
s.sa.semantic2(sc); |
| 1805 |
if (s.sa.errors) |
| 1806 |
return setError(); |
| 1807 |
} |
| 1808 |
|
| 1809 |
void visitSwitch(SwitchStatement ss) |
| 1810 |
{ |
| 1811 |
/* https://dlang.org/spec/statement.html#switch-statement |
| 1812 |
*/ |
| 1813 |
|
| 1814 |
//printf("SwitchStatement::semantic(%p)\n", ss); |
| 1815 |
ss.tryBody = sc.tryBody; |
| 1816 |
ss.tryFinally = sc.tryFinally; |
| 1817 |
if (ss.cases) |
| 1818 |
{ |
| 1819 |
result = ss; // already run |
| 1820 |
return; |
| 1821 |
} |
| 1822 |
|
| 1823 |
if (ss.param) |
| 1824 |
{ |
| 1825 |
/** |
| 1826 |
* If the switch statement is of form `switch(auto a = exp) { body }`, |
| 1827 |
* rewrite to the following inside it's own scope: |
| 1828 |
* |
| 1829 |
* auto a = exp |
| 1830 |
* switch(a) |
| 1831 |
* { body } |
| 1832 |
*/ |
| 1833 |
auto statements = new Statements(); |
| 1834 |
auto vardecl = new VarDeclaration(ss.param.loc, |
| 1835 |
ss.param.type, |
| 1836 |
ss.param.ident, |
| 1837 |
new ExpInitializer(ss.condition.loc, ss.condition), |
| 1838 |
ss.param.storageClass); |
| 1839 |
|
| 1840 |
statements.push(new ExpStatement(ss.param.loc, vardecl)); |
| 1841 |
|
| 1842 |
ss.condition = new VarExp(ss.param.loc, vardecl, false); |
| 1843 |
ss.param = null; |
| 1844 |
|
| 1845 |
statements.push(ss); |
| 1846 |
|
| 1847 |
Statement s = new CompoundStatement(ss.loc, statements); |
| 1848 |
s = new ScopeStatement(ss.loc, s, ss.endloc); |
| 1849 |
s = s.statementSemantic(sc); |
| 1850 |
result = s; |
| 1851 |
return; |
| 1852 |
} |
| 1853 |
|
| 1854 |
bool conditionError = false; |
| 1855 |
ss.condition = ss.condition.expressionSemantic(sc); |
| 1856 |
ss.condition = resolveProperties(sc, ss.condition); |
| 1857 |
|
| 1858 |
Type att = null; |
| 1859 |
TypeEnum te = null; |
| 1860 |
while (!ss.condition.isErrorExp()) |
| 1861 |
{ |
| 1862 |
// preserve enum type for final switches |
| 1863 |
if (auto tenum = ss.condition.type.isTypeEnum()) |
| 1864 |
te = tenum; |
| 1865 |
if (ss.condition.type.isString()) |
| 1866 |
{ |
| 1867 |
// If it's not an array, cast it to one |
| 1868 |
if (ss.condition.type.ty != Tarray) |
| 1869 |
{ |
| 1870 |
ss.condition = ss.condition.implicitCastTo(sc, ss.condition.type.nextOf().arrayOf()); |
| 1871 |
} |
| 1872 |
ss.condition.type = ss.condition.type.constOf(); |
| 1873 |
break; |
| 1874 |
} |
| 1875 |
ss.condition = integralPromotions(ss.condition, sc); |
| 1876 |
if (!ss.condition.isErrorExp() && ss.condition.type.isIntegral()) |
| 1877 |
break; |
| 1878 |
|
| 1879 |
auto ad = isAggregate(ss.condition.type); |
| 1880 |
if (ad && ad.aliasthis && !isRecursiveAliasThis(att, ss.condition.type)) |
| 1881 |
{ |
| 1882 |
if (auto e = resolveAliasThis(sc, ss.condition, true)) |
| 1883 |
{ |
| 1884 |
ss.condition = e; |
| 1885 |
continue; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (!ss.condition.isErrorExp()) |
| 1890 |
{ |
| 1891 |
error(ss.loc, "`%s` must be of integral or string type, it is a `%s`", |
| 1892 |
ss.condition.toChars(), ss.condition.type.toChars()); |
| 1893 |
conditionError = true; |
| 1894 |
break; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
if (checkNonAssignmentArrayOp(ss.condition)) |
| 1898 |
ss.condition = ErrorExp.get(); |
| 1899 |
ss.condition = ss.condition.optimize(WANTvalue); |
| 1900 |
ss.condition = checkGC(sc, ss.condition); |
| 1901 |
if (ss.condition.op == EXP.error) |
| 1902 |
conditionError = true; |
| 1903 |
|
| 1904 |
bool needswitcherror = false; |
| 1905 |
|
| 1906 |
ss.lastVar = sc.lastVar; |
| 1907 |
|
| 1908 |
sc = sc.push(); |
| 1909 |
sc.sbreak = ss; |
| 1910 |
sc.switchStatement = ss; |
| 1911 |
|
| 1912 |
ss.cases = new CaseStatements(); |
| 1913 |
const inLoopSave = sc.inLoop; |
| 1914 |
sc.inLoop = true; // BUG: should use Scope::mergeCallSuper() for each case instead |
| 1915 |
ss._body = ss._body.statementSemantic(sc); |
| 1916 |
sc.inLoop = inLoopSave; |
| 1917 |
|
| 1918 |
if (conditionError || (ss._body && ss._body.isErrorStatement())) |
| 1919 |
{ |
| 1920 |
sc.pop(); |
| 1921 |
return setError(); |
| 1922 |
} |
| 1923 |
|
| 1924 |
// Resolve any goto case's with exp |
| 1925 |
Lgotocase: |
| 1926 |
foreach (gcs; ss.gotoCases) |
| 1927 |
{ |
| 1928 |
if (!gcs.exp) |
| 1929 |
{ |
| 1930 |
error(gcs.loc, "no `case` statement following `goto case;`"); |
| 1931 |
sc.pop(); |
| 1932 |
return setError(); |
| 1933 |
} |
| 1934 |
|
| 1935 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 1936 |
{ |
| 1937 |
if (!scx.switchStatement) |
| 1938 |
continue; |
| 1939 |
foreach (cs; *scx.switchStatement.cases) |
| 1940 |
{ |
| 1941 |
if (cs.exp.equals(gcs.exp)) |
| 1942 |
{ |
| 1943 |
gcs.cs = cs; |
| 1944 |
continue Lgotocase; |
| 1945 |
} |
| 1946 |
} |
| 1947 |
} |
| 1948 |
error(gcs.loc, "`case %s` not found", gcs.exp.toChars()); |
| 1949 |
sc.pop(); |
| 1950 |
return setError(); |
| 1951 |
} |
| 1952 |
|
| 1953 |
if (ss.isFinal) |
| 1954 |
{ |
| 1955 |
Type t = ss.condition.type; |
| 1956 |
Dsymbol ds; |
| 1957 |
EnumDeclaration ed = null; |
| 1958 |
if (t && ((ds = t.toDsymbol(sc)) !is null)) |
| 1959 |
ed = ds.isEnumDeclaration(); // typedef'ed enum |
| 1960 |
if (!ed && te && ((ds = te.toDsymbol(sc)) !is null)) |
| 1961 |
ed = ds.isEnumDeclaration(); |
| 1962 |
if (ed && ss.cases.length < ed.members.length) |
| 1963 |
{ |
| 1964 |
int missingMembers = 0; |
| 1965 |
const maxShown = global.params.v.errorSupplementCount(); |
| 1966 |
Lmembers: |
| 1967 |
foreach (es; *ed.members) |
| 1968 |
{ |
| 1969 |
EnumMember em = es.isEnumMember(); |
| 1970 |
if (em) |
| 1971 |
{ |
| 1972 |
foreach (cs; *ss.cases) |
| 1973 |
{ |
| 1974 |
if (cs.exp.equals(em.value) || (!cs.exp.type.isString() && |
| 1975 |
!em.value.type.isString() && cs.exp.toInteger() == em.value.toInteger())) |
| 1976 |
continue Lmembers; |
| 1977 |
} |
| 1978 |
if (missingMembers == 0) |
| 1979 |
error(ss.loc, "missing cases for `enum` members in `final switch`:"); |
| 1980 |
|
| 1981 |
if (missingMembers < maxShown) |
| 1982 |
errorSupplemental(ss.loc, "`%s`", em.toChars()); |
| 1983 |
missingMembers++; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
if (missingMembers > 0) |
| 1987 |
{ |
| 1988 |
if (missingMembers > maxShown) |
| 1989 |
errorSupplemental(ss.loc, "... (%d more, -v to show) ...", missingMembers - maxShown); |
| 1990 |
sc.pop(); |
| 1991 |
return setError(); |
| 1992 |
} |
| 1993 |
} |
| 1994 |
else |
| 1995 |
needswitcherror = true; |
| 1996 |
} |
| 1997 |
|
| 1998 |
ss.hasDefault = sc.switchStatement.sdefault || |
| 1999 |
!(!ss.isFinal || needswitcherror || global.params.useAssert == CHECKENABLE.on || sc.func.isSafe || sc.func.isSaferD); |
| 2000 |
if (!ss.hasDefault) |
| 2001 |
{ |
| 2002 |
if (!ss.isFinal && (!ss._body || !ss._body.isErrorStatement()) && !sc.inCfile) |
| 2003 |
error(ss.loc, "`switch` statement without a `default`; use `final switch` or add `default: assert(0);` or add `default: break;`"); |
| 2004 |
|
| 2005 |
// Generate runtime error if the default is hit |
| 2006 |
auto a = new Statements(); |
| 2007 |
CompoundStatement cs; |
| 2008 |
Statement s; |
| 2009 |
|
| 2010 |
if (sc.inCfile) |
| 2011 |
{ |
| 2012 |
s = new BreakStatement(ss.loc, null); // default for C is `default: break;` |
| 2013 |
} |
| 2014 |
else if (!sc.needsCodegen()) |
| 2015 |
{ |
| 2016 |
// something for the interpreter to deal with |
| 2017 |
s = new ExpStatement(ss.loc, new AssertExp(ss.loc, IntegerExp.literal!0)); |
| 2018 |
} |
| 2019 |
else if (global.params.useSwitchError == CHECKENABLE.on && |
| 2020 |
global.params.checkAction != CHECKACTION.halt) |
| 2021 |
{ |
| 2022 |
if (global.params.checkAction == CHECKACTION.C) |
| 2023 |
{ |
| 2024 |
/* Rewrite as an assert(0) and let e2ir generate |
| 2025 |
* the call to the C assert failure function |
| 2026 |
*/ |
| 2027 |
s = new ExpStatement(ss.loc, new AssertExp(ss.loc, IntegerExp.literal!0)); |
| 2028 |
} |
| 2029 |
else |
| 2030 |
{ |
| 2031 |
if (!verifyHookExist(ss.loc, *sc, Id.__switch_error, "generating assert messages")) |
| 2032 |
return setError(); |
| 2033 |
|
| 2034 |
Expression sl = new IdentifierExp(ss.loc, Id.empty); |
| 2035 |
sl = new DotIdExp(ss.loc, sl, Id.object); |
| 2036 |
sl = new DotIdExp(ss.loc, sl, Id.__switch_error); |
| 2037 |
|
| 2038 |
Expressions* args = new Expressions(2); |
| 2039 |
(*args)[0] = new StringExp(ss.loc, ss.loc.filename.toDString()); |
| 2040 |
(*args)[1] = new IntegerExp(ss.loc.linnum); |
| 2041 |
|
| 2042 |
sl = new CallExp(ss.loc, sl, args); |
| 2043 |
sl = sl.expressionSemantic(sc); |
| 2044 |
|
| 2045 |
s = new SwitchErrorStatement(ss.loc, sl); |
| 2046 |
} |
| 2047 |
} |
| 2048 |
else |
| 2049 |
s = new ExpStatement(ss.loc, new HaltExp(ss.loc)); |
| 2050 |
|
| 2051 |
a.reserve(2); |
| 2052 |
sc.switchStatement.sdefault = new DefaultStatement(ss.loc, s); |
| 2053 |
a.push(ss._body); |
| 2054 |
if (ss._body.blockExit(sc.func, null) & BE.fallthru) |
| 2055 |
a.push(new BreakStatement(Loc.initial, null)); |
| 2056 |
a.push(sc.switchStatement.sdefault); |
| 2057 |
cs = new CompoundStatement(ss.loc, a); |
| 2058 |
ss._body = cs; |
| 2059 |
} |
| 2060 |
|
| 2061 |
if (!sc.inCfile && ss.checkLabel()) |
| 2062 |
{ |
| 2063 |
sc.pop(); |
| 2064 |
return setError(); |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
if (!(ss.condition.type.isString() && sc.needsCodegen())) |
| 2069 |
{ |
| 2070 |
sc.pop(); |
| 2071 |
result = ss; |
| 2072 |
return; |
| 2073 |
} |
| 2074 |
|
| 2075 |
// Transform a switch with string labels into a switch with integer labels. |
| 2076 |
|
| 2077 |
// The integer value of each case corresponds to the index of each label |
| 2078 |
// string in the sorted array of label strings. |
| 2079 |
|
| 2080 |
// The value of the integer condition is obtained by calling the druntime template |
| 2081 |
// switch(object.__switch(cond, options...)) {0: {...}, 1: {...}, ...} |
| 2082 |
|
| 2083 |
// We sort a copy of the array of labels because we want to do a binary search in object.__switch, |
| 2084 |
// without modifying the order of the case blocks here in the compiler. |
| 2085 |
|
| 2086 |
if (!verifyHookExist(ss.loc, *sc, Id.__switch, "switch cases on strings")) |
| 2087 |
return setError(); |
| 2088 |
|
| 2089 |
size_t numcases = 0; |
| 2090 |
if (ss.cases) |
| 2091 |
numcases = ss.cases.length; |
| 2092 |
|
| 2093 |
for (size_t i = 0; i < numcases; i++) |
| 2094 |
{ |
| 2095 |
CaseStatement cs = (*ss.cases)[i]; |
| 2096 |
cs.index = cast(int)i; |
| 2097 |
} |
| 2098 |
|
| 2099 |
// Make a copy of all the cases so that qsort doesn't scramble the actual |
| 2100 |
// data we pass to codegen (the order of the cases in the switch). |
| 2101 |
CaseStatements* csCopy = (*ss.cases).copy(); |
| 2102 |
|
| 2103 |
if (numcases) |
| 2104 |
{ |
| 2105 |
static int sort_compare(in CaseStatement* x, in CaseStatement* y) @trusted |
| 2106 |
{ |
| 2107 |
auto se1 = x.exp.isStringExp(); |
| 2108 |
auto se2 = y.exp.isStringExp(); |
| 2109 |
return (se1 && se2) ? se1.compare(se2) : 0; |
| 2110 |
} |
| 2111 |
// Sort cases for efficient lookup |
| 2112 |
csCopy.sort!sort_compare; |
| 2113 |
} |
| 2114 |
|
| 2115 |
// The actual lowering |
| 2116 |
auto arguments = new Expressions(); |
| 2117 |
arguments.push(ss.condition); |
| 2118 |
|
| 2119 |
auto compileTimeArgs = new Objects(); |
| 2120 |
|
| 2121 |
// The type & label no. |
| 2122 |
compileTimeArgs.push(new TypeExp(ss.loc, ss.condition.type.nextOf())); |
| 2123 |
|
| 2124 |
// The switch labels |
| 2125 |
foreach (caseString; *csCopy) |
| 2126 |
{ |
| 2127 |
compileTimeArgs.push(caseString.exp); |
| 2128 |
} |
| 2129 |
|
| 2130 |
Expression sl = new IdentifierExp(ss.loc, Id.empty); |
| 2131 |
sl = new DotIdExp(ss.loc, sl, Id.object); |
| 2132 |
sl = new DotTemplateInstanceExp(ss.loc, sl, Id.__switch, compileTimeArgs); |
| 2133 |
|
| 2134 |
sl = new CallExp(ss.loc, sl, arguments); |
| 2135 |
sl = sl.expressionSemantic(sc); |
| 2136 |
ss.condition = sl; |
| 2137 |
|
| 2138 |
auto i = 0; |
| 2139 |
foreach (c; *csCopy) |
| 2140 |
{ |
| 2141 |
(*ss.cases)[c.index].exp = new IntegerExp(i++); |
| 2142 |
} |
| 2143 |
|
| 2144 |
//printf("%s\n", ss._body.toChars()); |
| 2145 |
ss.statementSemantic(sc); |
| 2146 |
|
| 2147 |
sc.pop(); |
| 2148 |
result = ss; |
| 2149 |
} |
| 2150 |
|
| 2151 |
void visitCase(CaseStatement cs) |
| 2152 |
{ |
| 2153 |
SwitchStatement sw = sc.switchStatement; |
| 2154 |
bool errors = false; |
| 2155 |
|
| 2156 |
//printf("CaseStatement::semantic() %s\n", toChars()); |
| 2157 |
sc = sc.startCTFE(); |
| 2158 |
cs.exp = cs.exp.expressionSemantic(sc); |
| 2159 |
cs.exp = resolveProperties(sc, cs.exp); |
| 2160 |
sc = sc.endCTFE(); |
| 2161 |
|
| 2162 |
if (sw) |
| 2163 |
{ |
| 2164 |
Expression initialExp = cs.exp; |
| 2165 |
|
| 2166 |
// The switch'ed value has errors and doesn't provide the actual type |
| 2167 |
// Omit the cast to enable further semantic (exluding the check for matching types) |
| 2168 |
if (sw.condition.type && !sw.condition.type.isTypeError()) |
| 2169 |
cs.exp = cs.exp.implicitCastTo(sc, sw.condition.type); |
| 2170 |
cs.exp = cs.exp.optimize(WANTvalue | WANTexpand); |
| 2171 |
|
| 2172 |
Expression e = cs.exp; |
| 2173 |
// Remove all the casts the user and/or implicitCastTo may introduce |
| 2174 |
// otherwise we'd sometimes fail the check below. |
| 2175 |
while (e.isCastExp()) |
| 2176 |
e = e.isCastExp().e1; |
| 2177 |
|
| 2178 |
/* This is where variables are allowed as case expressions. |
| 2179 |
*/ |
| 2180 |
if (auto ve = e.isVarExp()) |
| 2181 |
{ |
| 2182 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 2183 |
Type t = cs.exp.type.toBasetype(); |
| 2184 |
if (v && (t.isIntegral() || t.ty == Tclass)) |
| 2185 |
{ |
| 2186 |
/* Flag that we need to do special code generation |
| 2187 |
* for this, i.e. generate a sequence of if-then-else |
| 2188 |
*/ |
| 2189 |
sw.hasVars = true; |
| 2190 |
|
| 2191 |
/* TODO check if v can be uninitialized at that point. |
| 2192 |
*/ |
| 2193 |
if (!v.isConst() && !v.isImmutable()) |
| 2194 |
{ |
| 2195 |
error(cs.loc, "`case` variables have to be `const` or `immutable`"); |
| 2196 |
} |
| 2197 |
|
| 2198 |
if (sw.isFinal) |
| 2199 |
{ |
| 2200 |
error(cs.loc, "`case` variables not allowed in `final switch` statements"); |
| 2201 |
errors = true; |
| 2202 |
} |
| 2203 |
|
| 2204 |
/* Find the outermost scope `scx` that set `sw`. |
| 2205 |
* Then search scope `scx` for a declaration of `v`. |
| 2206 |
*/ |
| 2207 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 2208 |
{ |
| 2209 |
if (scx.enclosing && scx.enclosing.switchStatement == sw) |
| 2210 |
continue; |
| 2211 |
assert(scx.switchStatement == sw); |
| 2212 |
|
| 2213 |
Dsymbol pscopesym; |
| 2214 |
if (!scx.search(cs.exp.loc, v.ident, pscopesym)) |
| 2215 |
{ |
| 2216 |
error(cs.loc, "`case` variable `%s` declared at %s cannot be declared in `switch` body", |
| 2217 |
v.toChars(), v.loc.toChars()); |
| 2218 |
errors = true; |
| 2219 |
} |
| 2220 |
break; |
| 2221 |
} |
| 2222 |
goto L1; |
| 2223 |
} |
| 2224 |
} |
| 2225 |
else |
| 2226 |
cs.exp = cs.exp.ctfeInterpret(); |
| 2227 |
|
| 2228 |
if (StringExp se = cs.exp.toStringExp()) |
| 2229 |
cs.exp = se; |
| 2230 |
else if (!cs.exp.isIntegerExp() && !cs.exp.isErrorExp()) |
| 2231 |
{ |
| 2232 |
error(cs.loc, "`case` expression must be a compile-time `string` or an integral constant, not `%s`", cs.exp.toChars()); |
| 2233 |
errors = true; |
| 2234 |
} |
| 2235 |
|
| 2236 |
L1: |
| 2237 |
// // Don't check other cases if this has errors |
| 2238 |
if (!cs.exp.isErrorExp()) |
| 2239 |
foreach (cs2; *sw.cases) |
| 2240 |
{ |
| 2241 |
//printf("comparing '%s' with '%s'\n", exp.toChars(), cs.exp.toChars()); |
| 2242 |
if (cs2.exp.equals(cs.exp)) |
| 2243 |
{ |
| 2244 |
// https://issues.dlang.org/show_bug.cgi?id=15909 |
| 2245 |
error(cs.loc, "duplicate `case %s` in `switch` statement", initialExp.toChars()); |
| 2246 |
errors = true; |
| 2247 |
break; |
| 2248 |
} |
| 2249 |
} |
| 2250 |
|
| 2251 |
sw.cases.push(cs); |
| 2252 |
|
| 2253 |
// Resolve any goto case's with no exp to this case statement |
| 2254 |
for (size_t i = 0; i < sw.gotoCases.length;) |
| 2255 |
{ |
| 2256 |
GotoCaseStatement gcs = sw.gotoCases[i]; |
| 2257 |
if (!gcs.exp) |
| 2258 |
{ |
| 2259 |
gcs.cs = cs; |
| 2260 |
sw.gotoCases.remove(i); // remove from array |
| 2261 |
continue; |
| 2262 |
} |
| 2263 |
i++; |
| 2264 |
} |
| 2265 |
|
| 2266 |
if (sc.switchStatement.tryFinally != sc.tryFinally) |
| 2267 |
{ |
| 2268 |
error(cs.loc, "`switch` and `case` are in different `finally` blocks"); |
| 2269 |
errors = true; |
| 2270 |
} |
| 2271 |
if (sc.switchStatement.tryBody != sc.tryBody) |
| 2272 |
{ |
| 2273 |
error(cs.loc, "case cannot be in different `try` block level from `switch`"); |
| 2274 |
errors = true; |
| 2275 |
} |
| 2276 |
} |
| 2277 |
else |
| 2278 |
{ |
| 2279 |
error(cs.loc, "`case` not in `switch` statement"); |
| 2280 |
errors = true; |
| 2281 |
} |
| 2282 |
|
| 2283 |
sc.ctorflow.orCSX(CSX.label); |
| 2284 |
cs.statement = cs.statement.statementSemantic(sc); |
| 2285 |
if (cs.statement.isErrorStatement()) |
| 2286 |
{ |
| 2287 |
result = cs.statement; |
| 2288 |
return; |
| 2289 |
} |
| 2290 |
if (errors || cs.exp.op == EXP.error) |
| 2291 |
return setError(); |
| 2292 |
|
| 2293 |
cs.lastVar = sc.lastVar; |
| 2294 |
result = cs; |
| 2295 |
} |
| 2296 |
|
| 2297 |
void visitCaseRange(CaseRangeStatement crs) |
| 2298 |
{ |
| 2299 |
SwitchStatement sw = sc.switchStatement; |
| 2300 |
if (sw is null) |
| 2301 |
{ |
| 2302 |
error(crs.loc, "case range not in `switch` statement"); |
| 2303 |
return setError(); |
| 2304 |
} |
| 2305 |
|
| 2306 |
//printf("CaseRangeStatement::semantic() %s\n", toChars()); |
| 2307 |
bool errors = false; |
| 2308 |
if (sw.isFinal) |
| 2309 |
{ |
| 2310 |
error(crs.loc, "case ranges not allowed in `final switch`"); |
| 2311 |
errors = true; |
| 2312 |
} |
| 2313 |
|
| 2314 |
sc = sc.startCTFE(); |
| 2315 |
crs.first = crs.first.expressionSemantic(sc); |
| 2316 |
crs.first = resolveProperties(sc, crs.first); |
| 2317 |
sc = sc.endCTFE(); |
| 2318 |
crs.first = crs.first.implicitCastTo(sc, sw.condition.type); |
| 2319 |
crs.first = crs.first.ctfeInterpret(); |
| 2320 |
|
| 2321 |
sc = sc.startCTFE(); |
| 2322 |
crs.last = crs.last.expressionSemantic(sc); |
| 2323 |
crs.last = resolveProperties(sc, crs.last); |
| 2324 |
sc = sc.endCTFE(); |
| 2325 |
crs.last = crs.last.implicitCastTo(sc, sw.condition.type); |
| 2326 |
crs.last = crs.last.ctfeInterpret(); |
| 2327 |
|
| 2328 |
if (crs.first.op == EXP.error || crs.last.op == EXP.error || errors) |
| 2329 |
{ |
| 2330 |
if (crs.statement) |
| 2331 |
crs.statement.statementSemantic(sc); |
| 2332 |
return setError(); |
| 2333 |
} |
| 2334 |
|
| 2335 |
uinteger_t fval = crs.first.toInteger(); |
| 2336 |
uinteger_t lval = crs.last.toInteger(); |
| 2337 |
if ((crs.first.type.isUnsigned() && fval > lval) || (!crs.first.type.isUnsigned() && cast(sinteger_t)fval > cast(sinteger_t)lval)) |
| 2338 |
{ |
| 2339 |
error(crs.loc, "first `case %s` is greater than last `case %s`", crs.first.toChars(), crs.last.toChars()); |
| 2340 |
errors = true; |
| 2341 |
lval = fval; |
| 2342 |
} |
| 2343 |
|
| 2344 |
if (lval - fval > 256) |
| 2345 |
{ |
| 2346 |
error(crs.loc, "had %llu cases which is more than 257 cases in case range", 1 + lval - fval); |
| 2347 |
errors = true; |
| 2348 |
lval = fval + 256; |
| 2349 |
} |
| 2350 |
|
| 2351 |
if (errors) |
| 2352 |
return setError(); |
| 2353 |
|
| 2354 |
/* This works by replacing the CaseRange with an array of Case's. |
| 2355 |
* |
| 2356 |
* case a: .. case b: s; |
| 2357 |
* => |
| 2358 |
* case a: |
| 2359 |
* [...] |
| 2360 |
* case b: |
| 2361 |
* s; |
| 2362 |
*/ |
| 2363 |
|
| 2364 |
auto statements = new Statements(); |
| 2365 |
for (uinteger_t i = fval; i != lval + 1; i++) |
| 2366 |
{ |
| 2367 |
Statement s = crs.statement; |
| 2368 |
if (i != lval) // if not last case |
| 2369 |
s = new ExpStatement(crs.loc, cast(Expression)null); |
| 2370 |
Expression e = new IntegerExp(crs.loc, i, crs.first.type); |
| 2371 |
Statement cs = new CaseStatement(crs.loc, e, s); |
| 2372 |
statements.push(cs); |
| 2373 |
} |
| 2374 |
Statement s = new CompoundStatement(crs.loc, statements); |
| 2375 |
sc.ctorflow.orCSX(CSX.label); |
| 2376 |
s = s.statementSemantic(sc); |
| 2377 |
result = s; |
| 2378 |
} |
| 2379 |
|
| 2380 |
void visitDefault(DefaultStatement ds) |
| 2381 |
{ |
| 2382 |
//printf("DefaultStatement::semantic()\n"); |
| 2383 |
bool errors = false; |
| 2384 |
if (sc.switchStatement) |
| 2385 |
{ |
| 2386 |
if (sc.switchStatement.sdefault) |
| 2387 |
{ |
| 2388 |
error(ds.loc, "`switch` statement already has a default"); |
| 2389 |
errors = true; |
| 2390 |
} |
| 2391 |
sc.switchStatement.sdefault = ds; |
| 2392 |
|
| 2393 |
if (sc.switchStatement.tryFinally != sc.tryFinally) |
| 2394 |
{ |
| 2395 |
error(ds.loc, "`switch` and `default` are in different `finally` blocks"); |
| 2396 |
errors = true; |
| 2397 |
} |
| 2398 |
if (sc.switchStatement.tryBody != sc.tryBody) |
| 2399 |
{ |
| 2400 |
error(ds.loc, "default cannot be in different `try` block level from `switch`"); |
| 2401 |
errors = true; |
| 2402 |
} |
| 2403 |
if (sc.switchStatement.isFinal) |
| 2404 |
{ |
| 2405 |
error(ds.loc, "`default` statement not allowed in `final switch` statement"); |
| 2406 |
errors = true; |
| 2407 |
} |
| 2408 |
} |
| 2409 |
else |
| 2410 |
{ |
| 2411 |
error(ds.loc, "`default` not in `switch` statement"); |
| 2412 |
errors = true; |
| 2413 |
} |
| 2414 |
|
| 2415 |
sc.ctorflow.orCSX(CSX.label); |
| 2416 |
ds.statement = ds.statement.statementSemantic(sc); |
| 2417 |
if (errors || ds.statement.isErrorStatement()) |
| 2418 |
return setError(); |
| 2419 |
|
| 2420 |
ds.lastVar = sc.lastVar; |
| 2421 |
result = ds; |
| 2422 |
} |
| 2423 |
|
| 2424 |
void visitGotoDefault(GotoDefaultStatement gds) |
| 2425 |
{ |
| 2426 |
/* https://dlang.org/spec/statement.html#goto-statement |
| 2427 |
*/ |
| 2428 |
|
| 2429 |
gds.sw = sc.switchStatement; |
| 2430 |
if (!gds.sw) |
| 2431 |
{ |
| 2432 |
error(gds.loc, "`goto default` not in `switch` statement"); |
| 2433 |
return setError(); |
| 2434 |
} |
| 2435 |
if (gds.sw.isFinal) |
| 2436 |
{ |
| 2437 |
error(gds.loc, "`goto default` not allowed in `final switch` statement"); |
| 2438 |
return setError(); |
| 2439 |
} |
| 2440 |
result = gds; |
| 2441 |
} |
| 2442 |
|
| 2443 |
void visitGotoCase(GotoCaseStatement gcs) |
| 2444 |
{ |
| 2445 |
/* https://dlang.org/spec/statement.html#goto-statement |
| 2446 |
*/ |
| 2447 |
|
| 2448 |
if (!sc.switchStatement) |
| 2449 |
{ |
| 2450 |
error(gcs.loc, "`goto case` not in `switch` statement"); |
| 2451 |
return setError(); |
| 2452 |
} |
| 2453 |
|
| 2454 |
if (gcs.exp) |
| 2455 |
{ |
| 2456 |
gcs.exp = gcs.exp.expressionSemantic(sc); |
| 2457 |
gcs.exp = gcs.exp.implicitCastTo(sc, sc.switchStatement.condition.type); |
| 2458 |
gcs.exp = gcs.exp.optimize(WANTvalue); |
| 2459 |
if (gcs.exp.op == EXP.error) |
| 2460 |
return setError(); |
| 2461 |
} |
| 2462 |
|
| 2463 |
sc.switchStatement.gotoCases.push(gcs); |
| 2464 |
result = gcs; |
| 2465 |
} |
| 2466 |
|
| 2467 |
void visitReturn(ReturnStatement rs) |
| 2468 |
{ |
| 2469 |
/* https://dlang.org/spec/statement.html#return-statement |
| 2470 |
*/ |
| 2471 |
|
| 2472 |
//printf("ReturnStatement.dsymbolSemantic() %s\n", toChars(rs)); |
| 2473 |
|
| 2474 |
FuncDeclaration fd = sc.parent.isFuncDeclaration(); |
| 2475 |
if (fd.fes) |
| 2476 |
fd = fd.fes.func; // fd is now function enclosing foreach |
| 2477 |
|
| 2478 |
auto tf = fd.type.isTypeFunction(); |
| 2479 |
|
| 2480 |
if (rs.exp && rs.exp.isVarExp() && rs.exp.isVarExp().var == fd.vresult) |
| 2481 |
{ |
| 2482 |
// return vresult; |
| 2483 |
if (sc.fes) |
| 2484 |
{ |
| 2485 |
assert(rs.caseDim == 0); |
| 2486 |
sc.fes.cases.push(rs); |
| 2487 |
result = new ReturnStatement(Loc.initial, new IntegerExp(sc.fes.cases.length + 1)); |
| 2488 |
return; |
| 2489 |
} |
| 2490 |
if (fd.returnLabel) |
| 2491 |
{ |
| 2492 |
auto gs = new GotoStatement(rs.loc, Id.returnLabel); |
| 2493 |
gs.label = fd.returnLabel; |
| 2494 |
result = gs; |
| 2495 |
return; |
| 2496 |
} |
| 2497 |
|
| 2498 |
if (!fd.returns) |
| 2499 |
fd.returns = new ReturnStatements(); |
| 2500 |
fd.returns.push(rs); |
| 2501 |
result = rs; |
| 2502 |
return; |
| 2503 |
} |
| 2504 |
|
| 2505 |
Type tret = tf.next; |
| 2506 |
Type tbret = tret ? tret.toBasetype() : null; |
| 2507 |
|
| 2508 |
bool inferRef = (tf.isRef && (fd.storage_class & STC.auto_)); |
| 2509 |
Expression e0 = null; |
| 2510 |
|
| 2511 |
bool errors = false; |
| 2512 |
if (sc.contract) |
| 2513 |
{ |
| 2514 |
error(rs.loc, "`return` statements cannot be in contracts"); |
| 2515 |
errors = true; |
| 2516 |
} |
| 2517 |
if (sc.scopeGuard) |
| 2518 |
{ |
| 2519 |
// @@@DEPRECATED_2.112@@@ |
| 2520 |
// Deprecated in 2.100, transform into an error in 2.112 |
| 2521 |
if (sc.scopeGuard.tok == TOK.onScopeFailure) |
| 2522 |
{ |
| 2523 |
deprecation(rs.loc, "`return` statements cannot be in `scope(failure)` bodies."); |
| 2524 |
deprecationSupplemental(rs.loc, "Use try-catch blocks for this purpose"); |
| 2525 |
} |
| 2526 |
else |
| 2527 |
{ |
| 2528 |
error(rs.loc, "`return` statements cannot be in `%s` bodies", Token.toChars(sc.scopeGuard.tok)); |
| 2529 |
errors = true; |
| 2530 |
} |
| 2531 |
} |
| 2532 |
if (sc.tryFinally) |
| 2533 |
{ |
| 2534 |
error(rs.loc, "`return` statements cannot be in `finally` bodies"); |
| 2535 |
errors = true; |
| 2536 |
} |
| 2537 |
|
| 2538 |
if (fd.isCtorDeclaration()) |
| 2539 |
{ |
| 2540 |
if (rs.exp) |
| 2541 |
{ |
| 2542 |
error(rs.loc, "cannot return expression from constructor"); |
| 2543 |
errors = true; |
| 2544 |
} |
| 2545 |
|
| 2546 |
// Constructors implicitly do: |
| 2547 |
// return this; |
| 2548 |
rs.exp = new ThisExp(Loc.initial); |
| 2549 |
rs.exp.type = tret; |
| 2550 |
} |
| 2551 |
else if (rs.exp) |
| 2552 |
{ |
| 2553 |
fd.hasMultipleReturnExp = fd.hasReturnExp; |
| 2554 |
fd.hasReturnExp = true; |
| 2555 |
|
| 2556 |
FuncLiteralDeclaration fld = fd.isFuncLiteralDeclaration(); |
| 2557 |
if (tret) |
| 2558 |
rs.exp = inferType(rs.exp, tret); |
| 2559 |
else if (fld && fld.treq) |
| 2560 |
rs.exp = inferType(rs.exp, fld.treq.nextOf().nextOf()); |
| 2561 |
|
| 2562 |
rs.exp = rs.exp.expressionSemantic(sc); |
| 2563 |
rs.exp = rs.exp.arrayFuncConv(sc); |
| 2564 |
// If we're returning by ref, allow the expression to be `shared` |
| 2565 |
const returnSharedRef = (tf.isRef && (fd.inferRetType || tret.isShared())); |
| 2566 |
rs.exp.checkSharedAccess(sc, returnSharedRef); |
| 2567 |
|
| 2568 |
// for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684 |
| 2569 |
if (rs.exp.isTypeExp()) |
| 2570 |
rs.exp = resolveAliasThis(sc, rs.exp); |
| 2571 |
|
| 2572 |
rs.exp = resolveProperties(sc, rs.exp); |
| 2573 |
if (rs.exp.checkType()) |
| 2574 |
rs.exp = ErrorExp.get(); |
| 2575 |
if (auto f = isFuncAddress(rs.exp)) |
| 2576 |
{ |
| 2577 |
if (fd.inferRetType && f.checkForwardRef(rs.exp.loc)) |
| 2578 |
rs.exp = ErrorExp.get(); |
| 2579 |
} |
| 2580 |
if (checkNonAssignmentArrayOp(rs.exp)) |
| 2581 |
rs.exp = ErrorExp.get(); |
| 2582 |
|
| 2583 |
// Extract side-effect part |
| 2584 |
rs.exp = Expression.extractLast(rs.exp, e0); |
| 2585 |
if (rs.exp.isCallExp()) |
| 2586 |
rs.exp = valueNoDtor(rs.exp); |
| 2587 |
|
| 2588 |
/* Void-return function can have void / noreturn typed expression |
| 2589 |
* on return statement. |
| 2590 |
*/ |
| 2591 |
auto texp = rs.exp.type; |
| 2592 |
const convToVoid = texp.ty == Tvoid || texp.isTypeNoreturn(); |
| 2593 |
|
| 2594 |
if (tbret && tbret.ty == Tvoid || convToVoid) |
| 2595 |
{ |
| 2596 |
if (!convToVoid) |
| 2597 |
{ |
| 2598 |
error(rs.loc, "cannot return non-void from `void` function"); |
| 2599 |
errors = true; |
| 2600 |
rs.exp = new CastExp(rs.loc, rs.exp, Type.tvoid); |
| 2601 |
rs.exp = rs.exp.expressionSemantic(sc); |
| 2602 |
} |
| 2603 |
|
| 2604 |
// https://issues.dlang.org/show_bug.cgi?id=23063 |
| 2605 |
rs.exp = checkNoreturnVarAccess(rs.exp); |
| 2606 |
|
| 2607 |
// @@@DEPRECATED_2.111@@@ |
| 2608 |
const olderrors = global.startGagging(); |
| 2609 |
// uncomment to turn deprecation into an error when |
| 2610 |
// deprecation cycle is over |
| 2611 |
if (discardValue(rs.exp)) |
| 2612 |
{ |
| 2613 |
//errors = true; |
| 2614 |
} |
| 2615 |
if (global.endGagging(olderrors)) |
| 2616 |
deprecation(rs.exp.loc, "`%s` has no effect", rs.exp.toErrMsg()); |
| 2617 |
|
| 2618 |
/* Replace: |
| 2619 |
* return exp; |
| 2620 |
* with: |
| 2621 |
* exp; return; |
| 2622 |
*/ |
| 2623 |
e0 = Expression.combine(e0, rs.exp); |
| 2624 |
rs.exp = null; |
| 2625 |
} |
| 2626 |
if (e0) |
| 2627 |
{ |
| 2628 |
e0 = e0.optimize(WANTvalue); |
| 2629 |
e0 = checkGC(sc, e0); |
| 2630 |
} |
| 2631 |
} |
| 2632 |
|
| 2633 |
if (rs.exp) |
| 2634 |
{ |
| 2635 |
if (fd.inferRetType) // infer return type |
| 2636 |
{ |
| 2637 |
if (!tret) |
| 2638 |
{ |
| 2639 |
tf.next = rs.exp.type; |
| 2640 |
} |
| 2641 |
else if (!tret.isTypeError() && !rs.exp.type.equals(tret)) |
| 2642 |
{ |
| 2643 |
int m1 = rs.exp.type.implicitConvTo(tret); |
| 2644 |
int m2 = tret.implicitConvTo(rs.exp.type); |
| 2645 |
//printf("exp.type = %s m2<-->m1 tret %s\n", exp.type.toChars(), tret.toChars()); |
| 2646 |
//printf("m1 = %d, m2 = %d\n", m1, m2); |
| 2647 |
|
| 2648 |
if (m1 && m2) |
| 2649 |
{ |
| 2650 |
} |
| 2651 |
else if (!m1 && m2) |
| 2652 |
tf.next = rs.exp.type; |
| 2653 |
else if (m1 && !m2) |
| 2654 |
{ |
| 2655 |
} |
| 2656 |
else if (!rs.exp.isErrorExp()) |
| 2657 |
{ |
| 2658 |
error(rs.loc, "expected return type of `%s`, not `%s`:", |
| 2659 |
tret.toChars(), |
| 2660 |
rs.exp.type.toChars()); |
| 2661 |
errorSupplemental((fd.returns) ? (*fd.returns)[0].loc : fd.loc, |
| 2662 |
"Return type of `%s` inferred here.", |
| 2663 |
tret.toChars()); |
| 2664 |
|
| 2665 |
errors = true; |
| 2666 |
tf.next = Type.terror; |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
tret = tf.next; |
| 2671 |
tbret = tret.toBasetype(); |
| 2672 |
} |
| 2673 |
|
| 2674 |
if (inferRef) // deduce 'auto ref' |
| 2675 |
{ |
| 2676 |
/* Determine "refness" of function return: |
| 2677 |
* if it's an lvalue, return by ref, else return by value |
| 2678 |
* https://dlang.org/spec/function.html#auto-ref-functions |
| 2679 |
*/ |
| 2680 |
|
| 2681 |
void turnOffRef(scope void delegate() supplemental) |
| 2682 |
{ |
| 2683 |
tf.isRef = false; // return by value |
| 2684 |
tf.isReturn = false; // ignore 'return' attribute, whether explicit or inferred |
| 2685 |
fd.storage_class &= ~STC.return_; |
| 2686 |
|
| 2687 |
// If we previously assumed the function could be ref when |
| 2688 |
// checking for `shared`, make sure we were right |
| 2689 |
if (sc.previews.noSharedAccess && rs.exp.type.isShared()) |
| 2690 |
{ |
| 2691 |
.error(fd.loc, "%s `%s` function returns `shared` but cannot be inferred `ref`", fd.kind, fd.toPrettyChars); |
| 2692 |
supplemental(); |
| 2693 |
} |
| 2694 |
} |
| 2695 |
|
| 2696 |
if (rs.exp.isLvalue()) |
| 2697 |
{ |
| 2698 |
/* May return by ref |
| 2699 |
*/ |
| 2700 |
Scope* sc2 = sc.push(); |
| 2701 |
sc2.eSink = global.errorSinkNull; |
| 2702 |
bool err = checkReturnEscapeRef(*sc2, rs.exp, true); |
| 2703 |
sc2.pop(); |
| 2704 |
|
| 2705 |
if (err) |
| 2706 |
turnOffRef(() { checkReturnEscapeRef(*sc, rs.exp, false); }); |
| 2707 |
else if (!rs.exp.type.constConv(tf.next)) |
| 2708 |
turnOffRef( |
| 2709 |
() => rs.loc.errorSupplemental("cannot implicitly convert `%s` of type `%s` to `%s`", |
| 2710 |
rs.exp.toChars(), rs.exp.type.toChars(), tf.next.toChars()) |
| 2711 |
); |
| 2712 |
} |
| 2713 |
else |
| 2714 |
turnOffRef( |
| 2715 |
() => rs.loc.errorSupplemental("return value `%s` is not an lvalue", rs.exp.toChars()) |
| 2716 |
); |
| 2717 |
|
| 2718 |
/* The "refness" is determined by all of return statements. |
| 2719 |
* This means: |
| 2720 |
* return 3; return x; // ok, x can be a value |
| 2721 |
* return x; return 3; // ok, x can be a value |
| 2722 |
*/ |
| 2723 |
} |
| 2724 |
} |
| 2725 |
else |
| 2726 |
{ |
| 2727 |
// Type of the returned expression (if any), might've been moved to e0 |
| 2728 |
auto resType = e0 ? e0.type : Type.tvoid; |
| 2729 |
|
| 2730 |
// infer return type |
| 2731 |
if (fd.inferRetType) |
| 2732 |
{ |
| 2733 |
// 1. First `return <noreturn exp>?` |
| 2734 |
// 2. Potentially found a returning branch, update accordingly |
| 2735 |
if (!tf.next || tf.next.toBasetype().isTypeNoreturn()) |
| 2736 |
{ |
| 2737 |
tf.next = resType; // infer void or noreturn |
| 2738 |
} |
| 2739 |
// Found an actual return value before |
| 2740 |
else if (tf.next.ty != Tvoid && !resType.toBasetype().isTypeNoreturn()) |
| 2741 |
{ |
| 2742 |
if (!tf.next.isTypeError()) |
| 2743 |
{ |
| 2744 |
error(rs.loc, "mismatched function return type inference of `void` and `%s`", tf.next.toChars()); |
| 2745 |
} |
| 2746 |
errors = true; |
| 2747 |
tf.next = Type.terror; |
| 2748 |
} |
| 2749 |
|
| 2750 |
tret = tf.next; |
| 2751 |
tbret = tret.toBasetype(); |
| 2752 |
} |
| 2753 |
|
| 2754 |
// https://issues.dlang.org/show_bug.cgi?id=23914 |
| 2755 |
if (inferRef && !resType.isTypeNoreturn()) // deduce 'auto ref' |
| 2756 |
tf.isRef = false; |
| 2757 |
|
| 2758 |
if (tbret.ty != Tvoid && !resType.isTypeNoreturn()) // if non-void return |
| 2759 |
{ |
| 2760 |
if (!tbret.isTypeError()) |
| 2761 |
{ |
| 2762 |
if (e0) |
| 2763 |
error(rs.loc, "expected return type of `%s`, not `%s`", tret.toChars(), resType.toChars()); |
| 2764 |
else if (tbret.isTypeNoreturn()) |
| 2765 |
{ |
| 2766 |
error(rs.loc, "cannot return from `noreturn` function"); |
| 2767 |
.errorSupplemental(rs.loc, |
| 2768 |
"Consider adding an endless loop, `assert(0)`, or another `noreturn` expression"); |
| 2769 |
} |
| 2770 |
else |
| 2771 |
error(rs.loc, "`return` expression expected"); |
| 2772 |
} |
| 2773 |
errors = true; |
| 2774 |
} |
| 2775 |
else if (fd.isMain()) |
| 2776 |
{ |
| 2777 |
// main() returns 0, even if it returns void |
| 2778 |
rs.exp = IntegerExp.literal!0; |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
// If any branches have called a ctor, but this branch hasn't, it's an error |
| 2783 |
if (sc.ctorflow.callSuper & CSX.any_ctor && !(sc.ctorflow.callSuper & (CSX.this_ctor | CSX.super_ctor))) |
| 2784 |
{ |
| 2785 |
error(rs.loc, "`return` without calling constructor"); |
| 2786 |
errors = true; |
| 2787 |
} |
| 2788 |
|
| 2789 |
if (sc.ctorflow.fieldinit.length) // if aggregate fields are being constructed |
| 2790 |
{ |
| 2791 |
auto ad = fd.isMemberLocal(); |
| 2792 |
assert(ad); |
| 2793 |
foreach (i, v; ad.fields) |
| 2794 |
{ |
| 2795 |
bool mustInit = (v.storage_class & STC.nodefaultctor || v.type.needsNested()); |
| 2796 |
if (mustInit && !(sc.ctorflow.fieldinit[i].csx & CSX.this_ctor)) |
| 2797 |
{ |
| 2798 |
error(rs.loc, "an earlier `return` statement skips field `%s` initialization", v.toChars()); |
| 2799 |
errors = true; |
| 2800 |
} |
| 2801 |
} |
| 2802 |
} |
| 2803 |
sc.ctorflow.orCSX(CSX.return_); |
| 2804 |
|
| 2805 |
if (errors) |
| 2806 |
return setError(); |
| 2807 |
|
| 2808 |
if (sc.fes) |
| 2809 |
{ |
| 2810 |
if (!rs.exp) |
| 2811 |
{ |
| 2812 |
// Send out "case receiver" statement to the foreach. |
| 2813 |
// return exp; |
| 2814 |
Statement s = new ReturnStatement(Loc.initial, rs.exp); |
| 2815 |
sc.fes.cases.push(s); |
| 2816 |
|
| 2817 |
// Immediately rewrite "this" return statement as: |
| 2818 |
// return cases.length+1; |
| 2819 |
rs.exp = new IntegerExp(sc.fes.cases.length + 1); |
| 2820 |
if (e0) |
| 2821 |
{ |
| 2822 |
result = new CompoundStatement(rs.loc, new ExpStatement(rs.loc, e0), rs); |
| 2823 |
return; |
| 2824 |
} |
| 2825 |
result = rs; |
| 2826 |
return; |
| 2827 |
} |
| 2828 |
else |
| 2829 |
{ |
| 2830 |
fd.buildResultVar(null, rs.exp.type); |
| 2831 |
bool r = fd.vresult.checkNestedReference(sc, Loc.initial); |
| 2832 |
assert(!r); // vresult should be always accessible |
| 2833 |
|
| 2834 |
// Send out "case receiver" statement to the foreach. |
| 2835 |
// return vresult; |
| 2836 |
Statement s = new ReturnStatement(Loc.initial, new VarExp(Loc.initial, fd.vresult)); |
| 2837 |
sc.fes.cases.push(s); |
| 2838 |
|
| 2839 |
// Save receiver index for the later rewriting from: |
| 2840 |
// return exp; |
| 2841 |
// to: |
| 2842 |
// vresult = exp; retrun caseDim; |
| 2843 |
rs.caseDim = sc.fes.cases.length + 1; |
| 2844 |
} |
| 2845 |
} |
| 2846 |
if (rs.exp) |
| 2847 |
{ |
| 2848 |
if (!fd.returns) |
| 2849 |
fd.returns = new ReturnStatements(); |
| 2850 |
fd.returns.push(rs); |
| 2851 |
} |
| 2852 |
if (e0) |
| 2853 |
{ |
| 2854 |
if (e0.isDeclarationExp() || e0.isCommaExp()) |
| 2855 |
{ |
| 2856 |
rs.exp = Expression.combine(e0, rs.exp); |
| 2857 |
} |
| 2858 |
else |
| 2859 |
{ |
| 2860 |
auto es = new ExpStatement(rs.loc, e0); |
| 2861 |
if (e0.type.isTypeNoreturn()) |
| 2862 |
result = es; // Omit unreachable return; |
| 2863 |
else |
| 2864 |
result = new CompoundStatement(rs.loc, es, rs); |
| 2865 |
|
| 2866 |
return; |
| 2867 |
} |
| 2868 |
} |
| 2869 |
result = rs; |
| 2870 |
} |
| 2871 |
|
| 2872 |
void visitBreak(BreakStatement bs) |
| 2873 |
{ |
| 2874 |
/* https://dlang.org/spec/statement.html#break-statement |
| 2875 |
*/ |
| 2876 |
|
| 2877 |
//printf("BreakStatement::semantic()\n"); |
| 2878 |
|
| 2879 |
// If: |
| 2880 |
// break Identifier; |
| 2881 |
if (bs.ident) |
| 2882 |
{ |
| 2883 |
bs.ident = fixupLabelName(sc, bs.ident); |
| 2884 |
|
| 2885 |
FuncDeclaration thisfunc = sc.func; |
| 2886 |
|
| 2887 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 2888 |
{ |
| 2889 |
if (scx.func != thisfunc) // if in enclosing function |
| 2890 |
{ |
| 2891 |
if (sc.fes) // if this is the body of a foreach |
| 2892 |
{ |
| 2893 |
/* Post this statement to the fes, and replace |
| 2894 |
* it with a return value that caller will put into |
| 2895 |
* a switch. Caller will figure out where the break |
| 2896 |
* label actually is. |
| 2897 |
* Case numbers start with 2, not 0, as 0 is continue |
| 2898 |
* and 1 is break. |
| 2899 |
*/ |
| 2900 |
sc.fes.cases.push(bs); |
| 2901 |
result = new ReturnStatement(Loc.initial, new IntegerExp(sc.fes.cases.length + 1)); |
| 2902 |
return; |
| 2903 |
} |
| 2904 |
break; // can't break to it |
| 2905 |
} |
| 2906 |
|
| 2907 |
LabelStatement ls = scx.slabel; |
| 2908 |
if (ls && ls.ident == bs.ident) |
| 2909 |
{ |
| 2910 |
Statement s = ls.statement; |
| 2911 |
if (!s || !s.hasBreak()) |
| 2912 |
error(bs.loc, "label `%s` has no `break`", bs.ident.toChars()); |
| 2913 |
else if (ls.tf != sc.tryFinally) |
| 2914 |
error(bs.loc, "cannot break out of `finally` block"); |
| 2915 |
else |
| 2916 |
{ |
| 2917 |
ls.breaks = true; |
| 2918 |
result = bs; |
| 2919 |
return; |
| 2920 |
} |
| 2921 |
return setError(); |
| 2922 |
} |
| 2923 |
} |
| 2924 |
error(bs.loc, "enclosing label `%s` for `break` not found", bs.ident.toChars()); |
| 2925 |
return setError(); |
| 2926 |
} |
| 2927 |
else if (!sc.sbreak) |
| 2928 |
{ |
| 2929 |
if (sc.scopeGuard && sc.scopeGuard.tok != TOK.onScopeFailure) |
| 2930 |
{ |
| 2931 |
error(bs.loc, "`break` is not allowed inside `%s` bodies", Token.toChars(sc.scopeGuard.tok)); |
| 2932 |
} |
| 2933 |
else if (sc.fes) |
| 2934 |
{ |
| 2935 |
// Replace break; with return 1; |
| 2936 |
result = new ReturnStatement(Loc.initial, IntegerExp.literal!1); |
| 2937 |
return; |
| 2938 |
} |
| 2939 |
else |
| 2940 |
error(bs.loc, "`break` is not inside a loop or `switch`"); |
| 2941 |
return setError(); |
| 2942 |
} |
| 2943 |
else if (sc.sbreak.isForwardingStatement()) |
| 2944 |
{ |
| 2945 |
error(bs.loc, "must use labeled `break` within `static foreach`"); |
| 2946 |
} |
| 2947 |
result = bs; |
| 2948 |
} |
| 2949 |
|
| 2950 |
void visitContinue(ContinueStatement cs) |
| 2951 |
{ |
| 2952 |
/* https://dlang.org/spec/statement.html#continue-statement |
| 2953 |
*/ |
| 2954 |
|
| 2955 |
//printf("ContinueStatement::semantic() %p\n", cs); |
| 2956 |
if (cs.ident) |
| 2957 |
{ |
| 2958 |
cs.ident = fixupLabelName(sc, cs.ident); |
| 2959 |
|
| 2960 |
Scope* scx; |
| 2961 |
FuncDeclaration thisfunc = sc.func; |
| 2962 |
|
| 2963 |
for (scx = sc; scx; scx = scx.enclosing) |
| 2964 |
{ |
| 2965 |
LabelStatement ls; |
| 2966 |
if (scx.func != thisfunc) // if in enclosing function |
| 2967 |
{ |
| 2968 |
if (sc.fes) // if this is the body of a foreach |
| 2969 |
{ |
| 2970 |
for (; scx; scx = scx.enclosing) |
| 2971 |
{ |
| 2972 |
ls = scx.slabel; |
| 2973 |
if (ls && ls.ident == cs.ident && ls.statement == sc.fes) |
| 2974 |
{ |
| 2975 |
// Replace continue ident; with return 0; |
| 2976 |
result = new ReturnStatement(Loc.initial, IntegerExp.literal!0); |
| 2977 |
return; |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
/* Post this statement to the fes, and replace |
| 2982 |
* it with a return value that caller will put into |
| 2983 |
* a switch. Caller will figure out where the break |
| 2984 |
* label actually is. |
| 2985 |
* Case numbers start with 2, not 0, as 0 is continue |
| 2986 |
* and 1 is break. |
| 2987 |
*/ |
| 2988 |
sc.fes.cases.push(cs); |
| 2989 |
result = new ReturnStatement(Loc.initial, new IntegerExp(sc.fes.cases.length + 1)); |
| 2990 |
return; |
| 2991 |
} |
| 2992 |
break; // can't continue to it |
| 2993 |
} |
| 2994 |
|
| 2995 |
ls = scx.slabel; |
| 2996 |
if (ls && ls.ident == cs.ident) |
| 2997 |
{ |
| 2998 |
Statement s = ls.statement; |
| 2999 |
if (!s || !s.hasContinue()) |
| 3000 |
error(cs.loc, "label `%s` has no `continue`", cs.ident.toChars()); |
| 3001 |
else if (ls.tf != sc.tryFinally) |
| 3002 |
error(cs.loc, "cannot continue out of `finally` block"); |
| 3003 |
else |
| 3004 |
{ |
| 3005 |
result = cs; |
| 3006 |
return; |
| 3007 |
} |
| 3008 |
return setError(); |
| 3009 |
} |
| 3010 |
} |
| 3011 |
error(cs.loc, "enclosing label `%s` for `continue` not found", cs.ident.toChars()); |
| 3012 |
return setError(); |
| 3013 |
} |
| 3014 |
else if (!sc.scontinue) |
| 3015 |
{ |
| 3016 |
if (sc.scopeGuard && sc.scopeGuard.tok != TOK.onScopeFailure) |
| 3017 |
{ |
| 3018 |
error(cs.loc, "`continue` is not allowed inside `%s` bodies", Token.toChars(sc.scopeGuard.tok)); |
| 3019 |
} |
| 3020 |
else if (sc.fes) |
| 3021 |
{ |
| 3022 |
// Replace continue; with return 0; |
| 3023 |
result = new ReturnStatement(Loc.initial, IntegerExp.literal!0); |
| 3024 |
return; |
| 3025 |
} |
| 3026 |
else |
| 3027 |
error(cs.loc, "`continue` is not inside a loop"); |
| 3028 |
return setError(); |
| 3029 |
} |
| 3030 |
else if (sc.scontinue.isForwardingStatement()) |
| 3031 |
{ |
| 3032 |
error(cs.loc, "must use labeled `continue` within `static foreach`"); |
| 3033 |
} |
| 3034 |
result = cs; |
| 3035 |
} |
| 3036 |
|
| 3037 |
void visitSynchronized(SynchronizedStatement ss) |
| 3038 |
{ |
| 3039 |
/* https://dlang.org/spec/statement.html#synchronized-statement |
| 3040 |
*/ |
| 3041 |
|
| 3042 |
if (ss.exp) |
| 3043 |
{ |
| 3044 |
ss.exp = ss.exp.expressionSemantic(sc); |
| 3045 |
ss.exp = resolveProperties(sc, ss.exp); |
| 3046 |
ss.exp = ss.exp.optimize(WANTvalue); |
| 3047 |
ss.exp = checkGC(sc, ss.exp); |
| 3048 |
if (ss.exp.op == EXP.error) |
| 3049 |
{ |
| 3050 |
if (ss._body) |
| 3051 |
ss._body = ss._body.statementSemantic(sc); |
| 3052 |
return setError(); |
| 3053 |
} |
| 3054 |
|
| 3055 |
ClassDeclaration cd = ss.exp.type.isClassHandle(); |
| 3056 |
if (!cd) |
| 3057 |
{ |
| 3058 |
error(ss.loc, "can only `synchronize` on class objects, not `%s`", ss.exp.type.toChars()); |
| 3059 |
return setError(); |
| 3060 |
} |
| 3061 |
else if (cd.isInterfaceDeclaration()) |
| 3062 |
{ |
| 3063 |
/* Cast the interface to an object, as the object has the monitor, |
| 3064 |
* not the interface. |
| 3065 |
*/ |
| 3066 |
if (!ClassDeclaration.object) |
| 3067 |
{ |
| 3068 |
ObjectNotFound(ss.loc, Id.Object); |
| 3069 |
return setError(); |
| 3070 |
} |
| 3071 |
|
| 3072 |
Type t = ClassDeclaration.object.type; |
| 3073 |
t = t.typeSemantic(Loc.initial, sc).toBasetype(); |
| 3074 |
assert(t.ty == Tclass); |
| 3075 |
|
| 3076 |
ss.exp = new CastExp(ss.loc, ss.exp, t); |
| 3077 |
ss.exp = ss.exp.expressionSemantic(sc); |
| 3078 |
} |
| 3079 |
version (all) |
| 3080 |
{ |
| 3081 |
/* Rewrite as: |
| 3082 |
* auto tmp = exp; |
| 3083 |
* _d_monitorenter(tmp); |
| 3084 |
* try { body } finally { _d_monitorexit(tmp); } |
| 3085 |
*/ |
| 3086 |
auto tmp = copyToTemp(STC.none, "__sync", ss.exp); |
| 3087 |
tmp.dsymbolSemantic(sc); |
| 3088 |
|
| 3089 |
auto cs = new Statements(); |
| 3090 |
cs.push(new ExpStatement(ss.loc, tmp)); |
| 3091 |
|
| 3092 |
auto args = new Parameters(); |
| 3093 |
args.push(new Parameter(Loc.initial, STC.none, ClassDeclaration.object.type, null, null, null)); |
| 3094 |
|
| 3095 |
FuncDeclaration fdenter = FuncDeclaration.genCfunc(args, Type.tvoid, Id.monitorenter); |
| 3096 |
Expression e = new CallExp(ss.loc, fdenter, new VarExp(ss.loc, tmp)); |
| 3097 |
e.type = Type.tvoid; // do not run semantic on e |
| 3098 |
|
| 3099 |
cs.push(new ExpStatement(ss.loc, e)); |
| 3100 |
FuncDeclaration fdexit = FuncDeclaration.genCfunc(args, Type.tvoid, Id.monitorexit); |
| 3101 |
e = new CallExp(ss.loc, fdexit, new VarExp(ss.loc, tmp)); |
| 3102 |
e.type = Type.tvoid; // do not run semantic on e |
| 3103 |
Statement s = new ExpStatement(ss.loc, e); |
| 3104 |
s = new TryFinallyStatement(ss.loc, ss._body, s); |
| 3105 |
cs.push(s); |
| 3106 |
|
| 3107 |
s = new CompoundStatement(ss.loc, cs); |
| 3108 |
result = s.statementSemantic(sc); |
| 3109 |
} |
| 3110 |
} |
| 3111 |
else |
| 3112 |
{ |
| 3113 |
/* Generate our own critical section, then rewrite as: |
| 3114 |
* static shared void* __critsec; |
| 3115 |
* _d_criticalenter2(&__critsec); |
| 3116 |
* try { body } finally { _d_criticalexit(__critsec); } |
| 3117 |
*/ |
| 3118 |
auto id = Identifier.generateId("__critsec"); |
| 3119 |
auto t = Type.tvoidptr; |
| 3120 |
auto tmp = new VarDeclaration(ss.loc, t, id, null); |
| 3121 |
tmp.storage_class |= STC.temp | STC.shared_ | STC.static_; |
| 3122 |
Expression tmpExp = new VarExp(ss.loc, tmp); |
| 3123 |
|
| 3124 |
auto cs = new Statements(); |
| 3125 |
cs.push(new ExpStatement(ss.loc, tmp)); |
| 3126 |
|
| 3127 |
/* This is just a dummy variable for "goto skips declaration" error. |
| 3128 |
* Backend optimizer could remove this unused variable. |
| 3129 |
*/ |
| 3130 |
auto v = new VarDeclaration(ss.loc, Type.tvoidptr, Identifier.generateId("__sync"), null); |
| 3131 |
v.dsymbolSemantic(sc); |
| 3132 |
cs.push(new ExpStatement(ss.loc, v)); |
| 3133 |
|
| 3134 |
auto enterArgs = new Parameters(); |
| 3135 |
enterArgs.push(new Parameter(Loc.initial, STC.none, t.pointerTo(), null, null, null)); |
| 3136 |
|
| 3137 |
FuncDeclaration fdenter = FuncDeclaration.genCfunc(enterArgs, Type.tvoid, Id.criticalenter, STC.nothrow_); |
| 3138 |
Expression e = new AddrExp(ss.loc, tmpExp); |
| 3139 |
e = e.expressionSemantic(sc); |
| 3140 |
e = new CallExp(ss.loc, fdenter, e); |
| 3141 |
e.type = Type.tvoid; // do not run semantic on e |
| 3142 |
cs.push(new ExpStatement(ss.loc, e)); |
| 3143 |
|
| 3144 |
auto exitArgs = new Parameters(); |
| 3145 |
exitArgs.push(new Parameter(Loc.initial, STC.none, t, null, null, null)); |
| 3146 |
|
| 3147 |
FuncDeclaration fdexit = FuncDeclaration.genCfunc(exitArgs, Type.tvoid, Id.criticalexit, STC.nothrow_); |
| 3148 |
e = new CallExp(ss.loc, fdexit, tmpExp); |
| 3149 |
e.type = Type.tvoid; // do not run semantic on e |
| 3150 |
Statement s = new ExpStatement(ss.loc, e); |
| 3151 |
s = new TryFinallyStatement(ss.loc, ss._body, s); |
| 3152 |
cs.push(s); |
| 3153 |
|
| 3154 |
s = new CompoundStatement(ss.loc, cs); |
| 3155 |
result = s.statementSemantic(sc); |
| 3156 |
} |
| 3157 |
} |
| 3158 |
|
| 3159 |
void visitWith(WithStatement ws) |
| 3160 |
{ |
| 3161 |
/* https://dlang.org/spec/statement.html#with-statement |
| 3162 |
*/ |
| 3163 |
|
| 3164 |
ScopeDsymbol sym; |
| 3165 |
Initializer _init; |
| 3166 |
|
| 3167 |
//printf("WithStatement::semantic()\n"); |
| 3168 |
ws.exp = ws.exp.expressionSemantic(sc); |
| 3169 |
ws.exp = resolveProperties(sc, ws.exp); |
| 3170 |
ws.exp = ws.exp.optimize(WANTvalue); |
| 3171 |
ws.exp = checkGC(sc, ws.exp); |
| 3172 |
if (ws.exp.op == EXP.error) |
| 3173 |
return setError(); |
| 3174 |
if (ws.exp.op == EXP.scope_) |
| 3175 |
{ |
| 3176 |
sym = new WithScopeSymbol(ws); |
| 3177 |
sym.parent = sc.scopesym; |
| 3178 |
sym.endlinnum = ws.endloc.linnum; |
| 3179 |
} |
| 3180 |
else if (auto et = ws.exp.isTypeExp()) |
| 3181 |
{ |
| 3182 |
Dsymbol s = et.type.toDsymbol(sc); |
| 3183 |
if (!s || !s.isScopeDsymbol()) |
| 3184 |
{ |
| 3185 |
error(ws.loc, "`with` type `%s` has no members", ws.exp.toChars()); |
| 3186 |
return setError(); |
| 3187 |
} |
| 3188 |
sym = new WithScopeSymbol(ws); |
| 3189 |
sym.parent = sc.scopesym; |
| 3190 |
sym.endlinnum = ws.endloc.linnum; |
| 3191 |
} |
| 3192 |
else |
| 3193 |
{ |
| 3194 |
Type texp = ws.exp.type; |
| 3195 |
Type t = texp.toBasetype(); |
| 3196 |
|
| 3197 |
Expression olde = ws.exp; |
| 3198 |
if (t.ty == Tpointer) |
| 3199 |
{ |
| 3200 |
ws.exp = new PtrExp(ws.loc, ws.exp); |
| 3201 |
ws.exp = ws.exp.expressionSemantic(sc); |
| 3202 |
texp = ws.exp.type; |
| 3203 |
t = texp.toBasetype(); |
| 3204 |
} |
| 3205 |
|
| 3206 |
assert(t); |
| 3207 |
t = t.toBasetype(); |
| 3208 |
if (t.isClassHandle()) |
| 3209 |
{ |
| 3210 |
_init = new ExpInitializer(ws.loc, ws.exp); |
| 3211 |
ws.wthis = new VarDeclaration(ws.loc, ws.exp.type, Id.withSym, _init); |
| 3212 |
ws.wthis.storage_class |= STC.temp; |
| 3213 |
ws.wthis.dsymbolSemantic(sc); |
| 3214 |
|
| 3215 |
sym = new WithScopeSymbol(ws); |
| 3216 |
sym.parent = sc.scopesym; |
| 3217 |
sym.endlinnum = ws.endloc.linnum; |
| 3218 |
} |
| 3219 |
else if (t.ty == Tstruct) |
| 3220 |
{ |
| 3221 |
if (!ws.exp.isLvalue()) |
| 3222 |
{ |
| 3223 |
/* Re-write to |
| 3224 |
* { |
| 3225 |
* auto __withtmp = exp |
| 3226 |
* with(__withtmp) |
| 3227 |
* { |
| 3228 |
* ... |
| 3229 |
* } |
| 3230 |
* } |
| 3231 |
*/ |
| 3232 |
auto tmp = copyToTemp(STC.none, "__withtmp", ws.exp); |
| 3233 |
tmp.dsymbolSemantic(sc); |
| 3234 |
auto es = new ExpStatement(ws.loc, tmp); |
| 3235 |
ws.exp = new VarExp(ws.loc, tmp); |
| 3236 |
Statement ss = new ScopeStatement(ws.loc, new CompoundStatement(ws.loc, es, ws), ws.endloc); |
| 3237 |
result = ss.statementSemantic(sc); |
| 3238 |
return; |
| 3239 |
} |
| 3240 |
Expression e = ws.exp.addressOf(); |
| 3241 |
_init = new ExpInitializer(ws.loc, e); |
| 3242 |
ws.wthis = new VarDeclaration(ws.loc, e.type, Id.withSym, _init); |
| 3243 |
ws.wthis.storage_class |= STC.temp; |
| 3244 |
ws.wthis.dsymbolSemantic(sc); |
| 3245 |
sym = new WithScopeSymbol(ws); |
| 3246 |
// Need to set the scope to make use of resolveAliasThis |
| 3247 |
sym.setScope(sc); |
| 3248 |
sym.parent = sc.scopesym; |
| 3249 |
sym.endlinnum = ws.endloc.linnum; |
| 3250 |
} |
| 3251 |
else if (auto tenum = texp.isTypeEnum()) |
| 3252 |
{ |
| 3253 |
ws.exp = new TypeExp(ws.exp.loc, tenum); |
| 3254 |
sym = new WithScopeSymbol(ws); |
| 3255 |
sym.parent = sc.scopesym; |
| 3256 |
sym.endlinnum = ws.endloc.linnum; |
| 3257 |
} |
| 3258 |
else |
| 3259 |
{ |
| 3260 |
error(ws.loc, "`with` expression types must be enums or aggregates or pointers to them, not `%s`", olde.type.toChars()); |
| 3261 |
return setError(); |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
if (ws._body) |
| 3266 |
{ |
| 3267 |
sym._scope = sc; |
| 3268 |
sc = sc.push(sym); |
| 3269 |
sc.insert(sym); |
| 3270 |
ws._body = ws._body.statementSemantic(sc); |
| 3271 |
sc.pop(); |
| 3272 |
if (ws._body && ws._body.isErrorStatement()) |
| 3273 |
{ |
| 3274 |
result = ws._body; |
| 3275 |
return; |
| 3276 |
} |
| 3277 |
} |
| 3278 |
|
| 3279 |
result = ws; |
| 3280 |
} |
| 3281 |
|
| 3282 |
// https://dlang.org/spec/statement.html#TryStatement |
| 3283 |
void visitTryCatch(TryCatchStatement tcs) |
| 3284 |
{ |
| 3285 |
//printf("TryCatchStatement.semantic()\n"); |
| 3286 |
|
| 3287 |
if (!global.params.useExceptions) |
| 3288 |
{ |
| 3289 |
error(tcs.loc, "cannot use try-catch statements with %s", global.params.betterC ? "-betterC".ptr : "-nothrow".ptr); |
| 3290 |
return setError(); |
| 3291 |
} |
| 3292 |
|
| 3293 |
if (!ClassDeclaration.throwable) |
| 3294 |
{ |
| 3295 |
error(tcs.loc, "cannot use try-catch statements because `object.Throwable` was not declared"); |
| 3296 |
return setError(); |
| 3297 |
} |
| 3298 |
|
| 3299 |
uint flags; |
| 3300 |
enum FLAGcpp = 1; |
| 3301 |
enum FLAGd = 2; |
| 3302 |
|
| 3303 |
tcs.tryBody = sc.tryBody; // chain on the in-flight tryBody |
| 3304 |
tcs._body = tcs._body.semanticScope(sc, null, null, tcs); |
| 3305 |
|
| 3306 |
/* Even if body is empty, still do semantic analysis on catches |
| 3307 |
*/ |
| 3308 |
bool catchErrors = false; |
| 3309 |
foreach (i, c; *tcs.catches) |
| 3310 |
{ |
| 3311 |
c.catchSemantic(sc); |
| 3312 |
if (c.errors) |
| 3313 |
{ |
| 3314 |
catchErrors = true; |
| 3315 |
continue; |
| 3316 |
} |
| 3317 |
auto cd = c.type.toBasetype().isClassHandle(); |
| 3318 |
flags |= cd.isCPPclass() ? FLAGcpp : FLAGd; |
| 3319 |
|
| 3320 |
// Determine if current catch 'hides' any previous catches |
| 3321 |
foreach (j; 0 .. i) |
| 3322 |
{ |
| 3323 |
Catch cj = (*tcs.catches)[j]; |
| 3324 |
const si = c.loc.toChars(); |
| 3325 |
const sj = cj.loc.toChars(); |
| 3326 |
if (c.type.toBasetype().implicitConvTo(cj.type.toBasetype())) |
| 3327 |
{ |
| 3328 |
error(tcs.loc, "`catch` at %s hides `catch` at %s", sj, si); |
| 3329 |
catchErrors = true; |
| 3330 |
} |
| 3331 |
} |
| 3332 |
} |
| 3333 |
|
| 3334 |
if (sc.func) |
| 3335 |
{ |
| 3336 |
sc.func.hasCatches = true; |
| 3337 |
if (flags == (FLAGcpp | FLAGd)) |
| 3338 |
{ |
| 3339 |
error(tcs.loc, "cannot mix catching D and C++ exceptions in the same try-catch"); |
| 3340 |
catchErrors = true; |
| 3341 |
} |
| 3342 |
} |
| 3343 |
|
| 3344 |
if (catchErrors) |
| 3345 |
return setError(); |
| 3346 |
|
| 3347 |
// No actual code in the try (i.e. omitted any conditionally compiled code) |
| 3348 |
// Could also be extended to check for hasCode |
| 3349 |
if (!tcs._body) |
| 3350 |
return; |
| 3351 |
|
| 3352 |
if (tcs._body.isErrorStatement()) |
| 3353 |
{ |
| 3354 |
result = tcs._body; |
| 3355 |
return; |
| 3356 |
} |
| 3357 |
|
| 3358 |
/* If the try body never throws, we can eliminate any catches |
| 3359 |
* of recoverable exceptions. |
| 3360 |
*/ |
| 3361 |
if (!(tcs._body.blockExit(sc.func, null) & BE.throw_) && ClassDeclaration.exception) |
| 3362 |
{ |
| 3363 |
foreach_reverse (i; 0 .. tcs.catches.length) |
| 3364 |
{ |
| 3365 |
Catch c = (*tcs.catches)[i]; |
| 3366 |
|
| 3367 |
/* If catch exception type is derived from Exception |
| 3368 |
*/ |
| 3369 |
if (c.type.toBasetype().implicitConvTo(ClassDeclaration.exception.type) && |
| 3370 |
(!c.handler || !c.handler.comeFrom()) && !sc.debug_) |
| 3371 |
{ |
| 3372 |
// Remove c from the array of catches |
| 3373 |
tcs.catches.remove(i); |
| 3374 |
} |
| 3375 |
} |
| 3376 |
} |
| 3377 |
|
| 3378 |
if (tcs.catches.length == 0) |
| 3379 |
{ |
| 3380 |
result = tcs._body.hasCode() ? tcs._body : null; |
| 3381 |
return; |
| 3382 |
} |
| 3383 |
|
| 3384 |
result = tcs; |
| 3385 |
} |
| 3386 |
|
| 3387 |
void visitTryFinally(TryFinallyStatement tfs) |
| 3388 |
{ |
| 3389 |
//printf("TryFinallyStatement::semantic()\n"); |
| 3390 |
tfs.tryBody = sc.tryBody; // chain on in-flight tryBody |
| 3391 |
tfs._body = tfs._body.semanticScope(sc, null, null, tfs); |
| 3392 |
|
| 3393 |
sc = sc.push(); |
| 3394 |
sc.tryFinally = tfs; |
| 3395 |
sc.sbreak = null; |
| 3396 |
sc.scontinue = null; // no break or continue out of finally block |
| 3397 |
tfs.finalbody = tfs.finalbody.semanticNoScope(sc); |
| 3398 |
sc.pop(); |
| 3399 |
|
| 3400 |
if (!tfs._body) |
| 3401 |
{ |
| 3402 |
result = tfs.finalbody; |
| 3403 |
return; |
| 3404 |
} |
| 3405 |
if (!tfs.finalbody) |
| 3406 |
{ |
| 3407 |
result = tfs._body; |
| 3408 |
return; |
| 3409 |
} |
| 3410 |
|
| 3411 |
auto blockexit = tfs._body.blockExit(sc.func, null); |
| 3412 |
|
| 3413 |
// if not worrying about exceptions |
| 3414 |
if (!(global.params.useExceptions && ClassDeclaration.throwable)) |
| 3415 |
blockexit &= ~BE.throw_; // don't worry about paths that otherwise may throw |
| 3416 |
|
| 3417 |
// Don't care about paths that halt, either |
| 3418 |
if ((blockexit & ~BE.halt) == BE.fallthru) |
| 3419 |
{ |
| 3420 |
result = new CompoundStatement(tfs.loc, tfs._body, tfs.finalbody); |
| 3421 |
return; |
| 3422 |
} |
| 3423 |
tfs.bodyFallsThru = (blockexit & BE.fallthru) != 0; |
| 3424 |
result = tfs; |
| 3425 |
} |
| 3426 |
|
| 3427 |
void visitScopeGuard(ScopeGuardStatement oss) |
| 3428 |
{ |
| 3429 |
/* https://dlang.org/spec/statement.html#scope-guard-statement |
| 3430 |
*/ |
| 3431 |
|
| 3432 |
if (oss.tok != TOK.onScopeExit) |
| 3433 |
{ |
| 3434 |
// https://issues.dlang.org/show_bug.cgi?id=23159 |
| 3435 |
if (!global.params.useExceptions) |
| 3436 |
{ |
| 3437 |
version (IN_GCC) |
| 3438 |
error(oss.loc, "`%s` cannot be used with `-fno-exceptions`", Token.toChars(oss.tok)); |
| 3439 |
else |
| 3440 |
error(oss.loc, "`%s` cannot be used with -betterC", Token.toChars(oss.tok)); |
| 3441 |
return setError(); |
| 3442 |
} |
| 3443 |
|
| 3444 |
// scope(success) and scope(failure) are rewritten to try-catch(-finally) statement, |
| 3445 |
// so the generated catch block cannot be placed in finally block. |
| 3446 |
// See also Catch::semantic. |
| 3447 |
if (sc.scopeGuard && sc.scopeGuard.tok != TOK.onScopeFailure) |
| 3448 |
{ |
| 3449 |
// If enclosing is scope(success) or scope(exit), this will be placed in finally block. |
| 3450 |
error(oss.loc, "cannot put `%s` statement inside `%s`", Token.toChars(oss.tok), Token.toChars(sc.scopeGuard.tok)); |
| 3451 |
return setError(); |
| 3452 |
} |
| 3453 |
if (sc.tryFinally) |
| 3454 |
{ |
| 3455 |
error(oss.loc, "cannot put `%s` statement inside `finally` block", Token.toChars(oss.tok)); |
| 3456 |
return setError(); |
| 3457 |
} |
| 3458 |
} |
| 3459 |
|
| 3460 |
sc = sc.push(); |
| 3461 |
sc.tryFinally = null; |
| 3462 |
sc.scopeGuard = oss; |
| 3463 |
if (oss.tok != TOK.onScopeFailure) |
| 3464 |
{ |
| 3465 |
// Jump out from scope(failure) block is allowed. |
| 3466 |
sc.sbreak = null; |
| 3467 |
sc.scontinue = null; |
| 3468 |
} |
| 3469 |
oss.statement = oss.statement.semanticNoScope(sc); |
| 3470 |
sc.pop(); |
| 3471 |
|
| 3472 |
if (!oss.statement || oss.statement.isErrorStatement()) |
| 3473 |
{ |
| 3474 |
result = oss.statement; |
| 3475 |
return; |
| 3476 |
} |
| 3477 |
result = oss; |
| 3478 |
} |
| 3479 |
|
| 3480 |
void visitThrow(ThrowStatement ts) |
| 3481 |
{ |
| 3482 |
/* https://dlang.org/spec/statement.html#throw-statement |
| 3483 |
*/ |
| 3484 |
|
| 3485 |
//printf("ThrowStatement::semantic()\n"); |
| 3486 |
if (throwSemantic(ts.loc, ts.exp, sc)) |
| 3487 |
result = ts; |
| 3488 |
else |
| 3489 |
setError(); |
| 3490 |
|
| 3491 |
} |
| 3492 |
|
| 3493 |
void visitDebug(DebugStatement ds) |
| 3494 |
{ |
| 3495 |
if (ds.statement) |
| 3496 |
{ |
| 3497 |
sc = sc.push(); |
| 3498 |
sc.debug_ = true; |
| 3499 |
ds.statement = ds.statement.statementSemantic(sc); |
| 3500 |
debugThrowWalker(ds.statement); |
| 3501 |
sc.pop(); |
| 3502 |
} |
| 3503 |
result = ds.statement; |
| 3504 |
} |
| 3505 |
|
| 3506 |
void visitGoto(GotoStatement gs) |
| 3507 |
{ |
| 3508 |
/* https://dlang.org/spec/statement.html#goto-statement |
| 3509 |
*/ |
| 3510 |
|
| 3511 |
//printf("GotoStatement::semantic()\n"); |
| 3512 |
FuncDeclaration fd = sc.func; |
| 3513 |
|
| 3514 |
gs.ident = fixupLabelName(sc, gs.ident); |
| 3515 |
gs.label = fd.searchLabel(gs.ident, gs.loc); |
| 3516 |
gs.tryBody = sc.tryBody; |
| 3517 |
gs.tf = sc.tryFinally; |
| 3518 |
gs.os = sc.scopeGuard; |
| 3519 |
gs.lastVar = sc.lastVar; |
| 3520 |
gs.inCtfeBlock = sc.ctfeBlock; |
| 3521 |
|
| 3522 |
if (!gs.label.statement && sc.fes) |
| 3523 |
{ |
| 3524 |
/* Either the goto label is forward referenced or it |
| 3525 |
* is in the function that the enclosing foreach is in. |
| 3526 |
* Can't know yet, so wrap the goto in a scope statement |
| 3527 |
* so we can patch it later, and add it to a 'look at this later' |
| 3528 |
* list. |
| 3529 |
*/ |
| 3530 |
gs.label.deleted = true; |
| 3531 |
auto ss = new ScopeStatement(gs.loc, gs, gs.loc); |
| 3532 |
sc.fes.gotos.push(ss); // 'look at this later' list |
| 3533 |
result = ss; |
| 3534 |
return; |
| 3535 |
} |
| 3536 |
|
| 3537 |
// Add to fwdref list to check later |
| 3538 |
if (!gs.label.statement) |
| 3539 |
{ |
| 3540 |
if (!fd.gotos) |
| 3541 |
fd.gotos = new GotoStatements(); |
| 3542 |
fd.gotos.push(gs); |
| 3543 |
} |
| 3544 |
else if (!sc.inCfile && gs.checkLabel()) |
| 3545 |
return setError(); |
| 3546 |
|
| 3547 |
result = gs; |
| 3548 |
} |
| 3549 |
|
| 3550 |
void visitLabel(LabelStatement ls) |
| 3551 |
{ |
| 3552 |
//printf("LabelStatement::semantic()\n"); |
| 3553 |
FuncDeclaration fd = sc.parent.isFuncDeclaration(); |
| 3554 |
|
| 3555 |
ls.ident = fixupLabelName(sc, ls.ident); |
| 3556 |
ls.tryBody = sc.tryBody; |
| 3557 |
ls.tf = sc.tryFinally; |
| 3558 |
ls.os = sc.scopeGuard; |
| 3559 |
ls.lastVar = sc.lastVar; |
| 3560 |
ls.inCtfeBlock = sc.ctfeBlock; |
| 3561 |
|
| 3562 |
LabelDsymbol ls2 = fd.searchLabel(ls.ident, ls.loc); |
| 3563 |
if (ls2.statement && !ls2.duplicated) |
| 3564 |
{ |
| 3565 |
if (ls.loc == ls2.loc) |
| 3566 |
{ |
| 3567 |
ls2.duplicated = true; |
| 3568 |
error(ls.loc, "label `%s` is duplicated", ls2.toChars()); |
| 3569 |
.errorSupplemental(ls2.loc, "labels cannot be used in a static foreach with more than 1 iteration"); |
| 3570 |
} |
| 3571 |
else |
| 3572 |
{ |
| 3573 |
error(ls.loc, "label `%s` is already defined", ls2.toChars()); |
| 3574 |
.errorSupplemental(ls2.loc, "first definition is here"); |
| 3575 |
} |
| 3576 |
return setError(); |
| 3577 |
} |
| 3578 |
else |
| 3579 |
ls2.statement = ls; |
| 3580 |
|
| 3581 |
sc = sc.push(); |
| 3582 |
sc.lastVar = sc.enclosing.lastVar; |
| 3583 |
sc.scopesym = sc.enclosing.scopesym; |
| 3584 |
|
| 3585 |
sc.ctorflow.orCSX(CSX.label); |
| 3586 |
|
| 3587 |
sc.slabel = ls; |
| 3588 |
if (ls.statement) |
| 3589 |
ls.statement = ls.statement.statementSemantic(sc); |
| 3590 |
|
| 3591 |
//issue 24534: lastVar may have been updated in the nested scope |
| 3592 |
sc.enclosing.lastVar = sc.lastVar; |
| 3593 |
|
| 3594 |
sc.pop(); |
| 3595 |
|
| 3596 |
result = ls; |
| 3597 |
} |
| 3598 |
|
| 3599 |
void visitAsm(AsmStatement s) |
| 3600 |
{ |
| 3601 |
/* https://dlang.org/spec/statement.html#asm |
| 3602 |
*/ |
| 3603 |
|
| 3604 |
//printf("AsmStatement()::semantic()\n"); |
| 3605 |
result = asmSemantic(s, sc); |
| 3606 |
} |
| 3607 |
|
| 3608 |
void visitCompoundAsm(CompoundAsmStatement cas) |
| 3609 |
{ |
| 3610 |
//printf("CompoundAsmStatement()::semantic()\n"); |
| 3611 |
// Apply postfix attributes of the asm block to each statement. |
| 3612 |
sc = sc.push(); |
| 3613 |
sc.stc |= cas.stc; |
| 3614 |
|
| 3615 |
/* Go through the statements twice, first to declare any labels, |
| 3616 |
* second for anything else. |
| 3617 |
*/ |
| 3618 |
|
| 3619 |
foreach (ref s; *cas.statements) |
| 3620 |
{ |
| 3621 |
if (s) |
| 3622 |
{ |
| 3623 |
if (auto ls = s.isLabelStatement()) |
| 3624 |
{ |
| 3625 |
sc.func.searchLabel(ls.ident, ls.loc); |
| 3626 |
} |
| 3627 |
} |
| 3628 |
} |
| 3629 |
|
| 3630 |
foreach (ref s; *cas.statements) |
| 3631 |
{ |
| 3632 |
s = s ? s.statementSemantic(sc) : null; |
| 3633 |
} |
| 3634 |
|
| 3635 |
assert(sc.func); |
| 3636 |
if (!(cas.stc & STC.pure_) && sc.func.setImpure(cas.loc, "executing an `asm` statement without `pure` annotation")) |
| 3637 |
error(cas.loc, "`asm` statement is assumed to be impure - mark it with `pure` if it is not"); |
| 3638 |
if (!(cas.stc & STC.nogc) && sc.func.setGC(cas.loc, "executing an `asm` statement without `@nogc` annotation")) |
| 3639 |
error(cas.loc, "`asm` statement is assumed to use the GC - mark it with `@nogc` if it does not"); |
| 3640 |
// @@@DEPRECATED_2.114@@@ |
| 3641 |
// change deprecation() to error(), add `else` and remove `| STC.safe` |
| 3642 |
// to turn deprecation into an error when deprecation cycle is over |
| 3643 |
if (cas.stc & STC.safe) |
| 3644 |
deprecation(cas.loc, "`asm` statement cannot be marked `@safe`, use `@system` or `@trusted` instead"); |
| 3645 |
if (!(cas.stc & (STC.trusted | STC.safe))) |
| 3646 |
{ |
| 3647 |
sc.setUnsafe(false, cas.loc, "executing an `asm` statement without `@trusted` annotation"); |
| 3648 |
} |
| 3649 |
|
| 3650 |
sc.pop(); |
| 3651 |
result = cas; |
| 3652 |
} |
| 3653 |
|
| 3654 |
void visitImport(ImportStatement imps) |
| 3655 |
{ |
| 3656 |
/* https://dlang.org/spec/module.html#ImportDeclaration |
| 3657 |
*/ |
| 3658 |
|
| 3659 |
foreach (i; 0 .. imps.imports.length) |
| 3660 |
{ |
| 3661 |
Import s = (*imps.imports)[i].isImport(); |
| 3662 |
assert(!s.aliasdecls.length); |
| 3663 |
foreach (j, name; s.names) |
| 3664 |
{ |
| 3665 |
Identifier _alias = s.aliases[j]; |
| 3666 |
if (!_alias) |
| 3667 |
_alias = name; |
| 3668 |
|
| 3669 |
auto tname = new TypeIdentifier(s.loc, name); |
| 3670 |
auto ad = new AliasDeclaration(s.loc, _alias, tname); |
| 3671 |
ad._import = s; |
| 3672 |
s.aliasdecls.push(ad); |
| 3673 |
} |
| 3674 |
|
| 3675 |
s.dsymbolSemantic(sc); |
| 3676 |
|
| 3677 |
// https://issues.dlang.org/show_bug.cgi?id=19942 |
| 3678 |
// If the module that's being imported doesn't exist, don't add it to the symbol table |
| 3679 |
// for the current scope. |
| 3680 |
if (s.mod !is null) |
| 3681 |
{ |
| 3682 |
Module.addDeferredSemantic2(s); // https://issues.dlang.org/show_bug.cgi?id=14666 |
| 3683 |
sc.insert(s); |
| 3684 |
|
| 3685 |
foreach (aliasdecl; s.aliasdecls) |
| 3686 |
{ |
| 3687 |
sc.insert(aliasdecl); |
| 3688 |
} |
| 3689 |
} |
| 3690 |
} |
| 3691 |
result = imps; |
| 3692 |
} |
| 3693 |
|
| 3694 |
mixin VisitStatement!void visit; |
| 3695 |
visit.VisitStatement(s); |
| 3696 |
return result; |
| 3697 |
} |
| 3698 |
|
| 3699 |
/** |
| 3700 |
* Run semantic on `throw <exp>`. |
| 3701 |
* |
| 3702 |
* Params: |
| 3703 |
* loc = location of the `throw` |
| 3704 |
* exp = value to be thrown |
| 3705 |
* sc = enclosing scope |
| 3706 |
* |
| 3707 |
* Returns: true if the `throw` is valid, or false if an error was found |
| 3708 |
*/ |
| 3709 |
public bool throwSemantic(Loc loc, ref Expression exp, Scope* sc) |
| 3710 |
{ |
| 3711 |
if (!global.params.useExceptions) |
| 3712 |
{ |
| 3713 |
version (IN_GCC) |
| 3714 |
loc.error("cannot use `throw` statements with `-fno-exceptions`"); |
| 3715 |
else |
| 3716 |
loc.error("cannot use `throw` statements with %s", global.params.betterC ? "-betterC".ptr : "-nothrow".ptr); |
| 3717 |
return false; |
| 3718 |
} |
| 3719 |
|
| 3720 |
if (!ClassDeclaration.throwable) |
| 3721 |
{ |
| 3722 |
loc.error("cannot use `throw` statements because `object.Throwable` was not declared"); |
| 3723 |
return false; |
| 3724 |
} |
| 3725 |
|
| 3726 |
if (auto ne = exp.isNewExp()) |
| 3727 |
{ |
| 3728 |
ne.thrownew = true; |
| 3729 |
} |
| 3730 |
|
| 3731 |
exp = exp.expressionSemantic(sc); |
| 3732 |
exp = resolveProperties(sc, exp); |
| 3733 |
exp = checkGC(sc, exp); |
| 3734 |
if (exp.isErrorExp()) |
| 3735 |
return false; |
| 3736 |
if (!exp.type.isNaked()) |
| 3737 |
{ |
| 3738 |
// @@@DEPRECATED_2.112@@@ |
| 3739 |
// Deprecated in 2.102, change into an error & return false in 2.112 |
| 3740 |
exp.loc.deprecation("cannot throw object of qualified type `%s`", exp.type.toChars()); |
| 3741 |
//return false; |
| 3742 |
} |
| 3743 |
checkThrowEscape(*sc, exp, false); |
| 3744 |
|
| 3745 |
ClassDeclaration cd = exp.type.toBasetype().isClassHandle(); |
| 3746 |
if (!cd || ((cd != ClassDeclaration.throwable) && !ClassDeclaration.throwable.isBaseOf(cd, null))) |
| 3747 |
{ |
| 3748 |
loc.error("can only throw class objects derived from `Throwable`, not type `%s`", exp.type.toChars()); |
| 3749 |
return false; |
| 3750 |
} |
| 3751 |
return true; |
| 3752 |
} |
| 3753 |
|
| 3754 |
private extern(D) Expression applyOpApply(ForeachStatement fs, Expression flde, |
| 3755 |
Type tab, Scope* sc2, Dsymbol sapply) |
| 3756 |
{ |
| 3757 |
version (none) |
| 3758 |
{ |
| 3759 |
if (sc2.useDIP1000 == FeatureState.enabled) |
| 3760 |
{ |
| 3761 |
message(loc, "To enforce `@safe`, the compiler allocates a closure unless `opApply()` uses `scope`"); |
| 3762 |
} |
| 3763 |
flde.isFuncExp().fd.tookAddressOf = 1; |
| 3764 |
} |
| 3765 |
else |
| 3766 |
{ |
| 3767 |
if (sc2.useDIP1000 == FeatureState.enabled) |
| 3768 |
++flde.isFuncExp().fd.tookAddressOf; // allocate a closure unless the opApply() uses 'scope' |
| 3769 |
} |
| 3770 |
assert(tab.ty == Tstruct || tab.ty == Tclass); |
| 3771 |
assert(sapply); |
| 3772 |
/* Call: |
| 3773 |
* aggr.apply(flde) |
| 3774 |
*/ |
| 3775 |
Expression ec; |
| 3776 |
ec = new DotIdExp(fs.loc, fs.aggr, sapply.ident); |
| 3777 |
ec = new CallExp(fs.loc, ec, flde); |
| 3778 |
ec = ec.expressionSemantic(sc2); |
| 3779 |
if (ec.isErrorExp()) |
| 3780 |
return null; |
| 3781 |
if (ec.type != Type.tint32) |
| 3782 |
{ |
| 3783 |
error(fs.loc, "`opApply()` function for `%s` must return an `int`", tab.toChars()); |
| 3784 |
return null; |
| 3785 |
} |
| 3786 |
return ec; |
| 3787 |
} |
| 3788 |
|
| 3789 |
private extern(D) Expression applyDelegate(ForeachStatement fs, Expression flde, |
| 3790 |
Type tab, Scope* sc2) |
| 3791 |
{ |
| 3792 |
Expression ec; |
| 3793 |
/* Call: |
| 3794 |
* aggr(flde) |
| 3795 |
*/ |
| 3796 |
if (auto de = fs.aggr.isDelegateExp()) |
| 3797 |
if (de.func.isNested() && |
| 3798 |
!de.func.needThis()) |
| 3799 |
{ |
| 3800 |
// https://issues.dlang.org/show_bug.cgi?id=3560 |
| 3801 |
fs.aggr = de.e1; |
| 3802 |
} |
| 3803 |
ec = new CallExp(fs.loc, fs.aggr, flde); |
| 3804 |
ec = ec.expressionSemantic(sc2); |
| 3805 |
if (ec.op == EXP.error) |
| 3806 |
return null; |
| 3807 |
if (ec.type != Type.tint32) |
| 3808 |
{ |
| 3809 |
error(fs.loc, "`opApply()` function for `%s` must return an `int`", tab.toChars()); |
| 3810 |
return null; |
| 3811 |
} |
| 3812 |
return ec; |
| 3813 |
} |
| 3814 |
|
| 3815 |
private extern(D) Expression applyArray(ForeachStatement fs, Expression flde, |
| 3816 |
Type tab, Scope* sc2, Type tn, Type tnv) |
| 3817 |
{ |
| 3818 |
Expression ec; |
| 3819 |
const dim = fs.parameters.length; |
| 3820 |
const loc = fs.loc; |
| 3821 |
/* Call: |
| 3822 |
* _aApply(aggr, flde) |
| 3823 |
*/ |
| 3824 |
static immutable fntab = |
| 3825 |
[ |
| 3826 |
"cc", "cw", "cd", |
| 3827 |
"wc", "cc", "wd", |
| 3828 |
"dc", "dw", "dd" |
| 3829 |
]; |
| 3830 |
|
| 3831 |
const(size_t) BUFFER_LEN = 7 + 1 + 2 + dim.sizeof * 3 + 1; |
| 3832 |
char[BUFFER_LEN] fdname; |
| 3833 |
int flag; |
| 3834 |
|
| 3835 |
switch (tn.ty) |
| 3836 |
{ |
| 3837 |
case Tchar: flag = 0; break; |
| 3838 |
case Twchar: flag = 3; break; |
| 3839 |
case Tdchar: flag = 6; break; |
| 3840 |
default: |
| 3841 |
assert(0); |
| 3842 |
} |
| 3843 |
switch (tnv.ty) |
| 3844 |
{ |
| 3845 |
case Tchar: flag += 0; break; |
| 3846 |
case Twchar: flag += 1; break; |
| 3847 |
case Tdchar: flag += 2; break; |
| 3848 |
default: |
| 3849 |
assert(0); |
| 3850 |
} |
| 3851 |
const(char)* r = (fs.op == TOK.foreach_reverse_) ? "R" : ""; |
| 3852 |
int j = snprintf(fdname.ptr, BUFFER_LEN, "_aApply%s%.*s%llu", r, 2, fntab[flag].ptr, cast(ulong)dim); |
| 3853 |
assert(j < BUFFER_LEN); |
| 3854 |
|
| 3855 |
FuncDeclaration fdapply; |
| 3856 |
TypeDelegate dgty; |
| 3857 |
auto params = new Parameters(); |
| 3858 |
params.push(new Parameter(Loc.initial, STC.in_, tn.arrayOf(), null, null, null)); |
| 3859 |
auto dgparams = new Parameters(); |
| 3860 |
dgparams.push(new Parameter(Loc.initial, STC.none, Type.tvoidptr, null, null, null)); |
| 3861 |
if (dim == 2) |
| 3862 |
dgparams.push(new Parameter(Loc.initial, STC.none, Type.tvoidptr, null, null, null)); |
| 3863 |
dgty = new TypeDelegate(new TypeFunction(ParameterList(dgparams), Type.tint32, LINK.d)); |
| 3864 |
params.push(new Parameter(Loc.initial, STC.none, dgty, null, null, null)); |
| 3865 |
fdapply = FuncDeclaration.genCfunc(params, Type.tint32, fdname.ptr); |
| 3866 |
|
| 3867 |
if (tab.isTypeSArray()) |
| 3868 |
fs.aggr = fs.aggr.castTo(sc2, tn.arrayOf()); |
| 3869 |
// paint delegate argument to the type runtime expects |
| 3870 |
Expression fexp = flde; |
| 3871 |
if (!dgty.equals(flde.type)) |
| 3872 |
{ |
| 3873 |
fexp = new CastExp(loc, flde, flde.type); |
| 3874 |
fexp.type = dgty; |
| 3875 |
} |
| 3876 |
ec = new VarExp(Loc.initial, fdapply, false); |
| 3877 |
ec = new CallExp(loc, ec, fs.aggr, fexp); |
| 3878 |
ec.type = Type.tint32; // don't run semantic() on ec |
| 3879 |
return ec; |
| 3880 |
} |
| 3881 |
|
| 3882 |
private extern(D) Expression applyAssocArray(ForeachStatement fs, Expression flde, Type tab) |
| 3883 |
{ |
| 3884 |
auto taa = tab.isTypeAArray(); |
| 3885 |
Expression ec; |
| 3886 |
const dim = fs.parameters.length; |
| 3887 |
// Check types |
| 3888 |
Parameter p = (*fs.parameters)[0]; |
| 3889 |
bool isRef = (p.storageClass & STC.ref_) != 0; |
| 3890 |
Type ta = p.type; |
| 3891 |
if (dim == 2) |
| 3892 |
{ |
| 3893 |
Type ti = (isRef ? taa.index.addMod(MODFlags.const_) : taa.index); |
| 3894 |
if (isRef ? !ti.constConv(ta) : !ti.implicitConvTo(ta)) |
| 3895 |
{ |
| 3896 |
error(fs.loc, "`foreach`: index must be type `%s`, not `%s`", |
| 3897 |
ti.toChars(), ta.toChars()); |
| 3898 |
return null; |
| 3899 |
} |
| 3900 |
p = (*fs.parameters)[1]; |
| 3901 |
isRef = (p.storageClass & STC.ref_) != 0; |
| 3902 |
ta = p.type; |
| 3903 |
} |
| 3904 |
Type taav = taa.nextOf(); |
| 3905 |
if (isRef ? !taav.constConv(ta) : !taav.implicitConvTo(ta)) |
| 3906 |
{ |
| 3907 |
error(fs.loc, "`foreach`: value must be type `%s`, not `%s`", |
| 3908 |
taav.toChars(), ta.toChars()); |
| 3909 |
return null; |
| 3910 |
} |
| 3911 |
|
| 3912 |
/* Call: |
| 3913 |
* extern(C) int _aaApply(void*, in size_t, int delegate(void*)) |
| 3914 |
* _aaApply(aggr, keysize, flde) |
| 3915 |
* |
| 3916 |
* extern(C) int _aaApply2(void*, in size_t, int delegate(void*, void*)) |
| 3917 |
* _aaApply2(aggr, keysize, flde) |
| 3918 |
*/ |
| 3919 |
__gshared FuncDeclaration* fdapply = [null, null]; |
| 3920 |
__gshared TypeDelegate* fldeTy = [null, null]; |
| 3921 |
ubyte i = (dim == 2 ? 1 : 0); |
| 3922 |
if (!fdapply[i]) |
| 3923 |
{ |
| 3924 |
auto params = new Parameters(); |
| 3925 |
params.push(new Parameter(Loc.initial, STC.none, Type.tvoid.pointerTo(), null, null, null)); |
| 3926 |
params.push(new Parameter(Loc.initial, STC.const_, Type.tsize_t, null, null, null)); |
| 3927 |
auto dgparams = new Parameters(); |
| 3928 |
dgparams.push(new Parameter(Loc.initial, STC.none, Type.tvoidptr, null, null, null)); |
| 3929 |
if (dim == 2) |
| 3930 |
dgparams.push(new Parameter(Loc.initial, STC.none, Type.tvoidptr, null, null, null)); |
| 3931 |
fldeTy[i] = new TypeDelegate(new TypeFunction(ParameterList(dgparams), Type.tint32, LINK.d)); |
| 3932 |
params.push(new Parameter(Loc.initial, STC.none, fldeTy[i], null, null, null)); |
| 3933 |
fdapply[i] = FuncDeclaration.genCfunc(params, Type.tint32, i ? Id._aaApply2 : Id._aaApply); |
| 3934 |
} |
| 3935 |
|
| 3936 |
auto exps = new Expressions(); |
| 3937 |
exps.push(fs.aggr); |
| 3938 |
auto keysize = taa.index.size(); |
| 3939 |
if (keysize == SIZE_INVALID) |
| 3940 |
return null; |
| 3941 |
assert(keysize < keysize.max - target.ptrsize); |
| 3942 |
keysize = (keysize + (target.ptrsize - 1)) & ~(target.ptrsize - 1); |
| 3943 |
// paint delegate argument to the type runtime expects |
| 3944 |
Expression fexp = flde; |
| 3945 |
if (!fldeTy[i].equals(flde.type)) |
| 3946 |
{ |
| 3947 |
fexp = new CastExp(fs.loc, flde, flde.type); |
| 3948 |
fexp.type = fldeTy[i]; |
| 3949 |
} |
| 3950 |
exps.push(new IntegerExp(Loc.initial, keysize, Type.tsize_t)); |
| 3951 |
exps.push(fexp); |
| 3952 |
ec = new VarExp(Loc.initial, fdapply[i], false); |
| 3953 |
ec = new CallExp(fs.loc, ec, exps); |
| 3954 |
ec.type = Type.tint32; // don't run semantic() on ec |
| 3955 |
return ec; |
| 3956 |
} |
| 3957 |
|
| 3958 |
private extern(D) Statement loopReturn(Expression e, Statements* cases, Loc loc) |
| 3959 |
{ |
| 3960 |
if (!cases.length) |
| 3961 |
{ |
| 3962 |
// Easy case, a clean exit from the loop |
| 3963 |
e = new CastExp(loc, e, Type.tvoid); // https://issues.dlang.org/show_bug.cgi?id=13899 |
| 3964 |
return new ExpStatement(loc, e); |
| 3965 |
} |
| 3966 |
// Construct a switch statement around the return value |
| 3967 |
// of the apply function. |
| 3968 |
Statement s; |
| 3969 |
auto a = new Statements(); |
| 3970 |
|
| 3971 |
// default: break; takes care of cases 0 and 1 |
| 3972 |
s = new BreakStatement(Loc.initial, null); |
| 3973 |
s = new DefaultStatement(Loc.initial, s); |
| 3974 |
a.push(s); |
| 3975 |
|
| 3976 |
// cases 2... |
| 3977 |
foreach (i, c; *cases) |
| 3978 |
{ |
| 3979 |
s = new CaseStatement(Loc.initial, new IntegerExp(i + 2), c); |
| 3980 |
a.push(s); |
| 3981 |
} |
| 3982 |
|
| 3983 |
s = new CompoundStatement(loc, a); |
| 3984 |
return new SwitchStatement(loc, null, e, s, false, loc); |
| 3985 |
} |
| 3986 |
|
| 3987 |
/************************************* |
| 3988 |
* Turn foreach body into the function literal: |
| 3989 |
* int delegate(ref T param) { body } |
| 3990 |
* Params: |
| 3991 |
* sc = context |
| 3992 |
* fs = ForeachStatement |
| 3993 |
* tfld = type of function literal to be created (type of opApply() function if any), can be null |
| 3994 |
* Returns: |
| 3995 |
* Function literal created, as an expression |
| 3996 |
* null if error. |
| 3997 |
*/ |
| 3998 |
private FuncExp foreachBodyToFunction(Scope* sc, ForeachStatement fs, TypeFunction tfld) |
| 3999 |
{ |
| 4000 |
auto params = new Parameters(); |
| 4001 |
foreach (i, p; *fs.parameters) |
| 4002 |
{ |
| 4003 |
STC stc = STC.ref_ | (p.storageClass & STC.scope_); |
| 4004 |
Identifier id; |
| 4005 |
|
| 4006 |
p.type = p.type.typeSemantic(fs.loc, sc); |
| 4007 |
p.type = p.type.addStorageClass(p.storageClass); |
| 4008 |
if (tfld) |
| 4009 |
{ |
| 4010 |
Parameter param = tfld.parameterList[i]; |
| 4011 |
//printf("\tparam = %s%s\n", (param.storageClass&STC.ref_?"ref ":"").ptr, param.ident.toChars()); |
| 4012 |
stc = (param.storageClass & STC.ref_) | (p.storageClass & STC.scope_); |
| 4013 |
if ((p.storageClass & STC.ref_) != (param.storageClass & STC.ref_)) |
| 4014 |
{ |
| 4015 |
if (!(param.storageClass & STC.ref_)) |
| 4016 |
{ |
| 4017 |
error(fs.loc, "`foreach`: cannot make `%s` `ref`", p.ident.toChars()); |
| 4018 |
return null; |
| 4019 |
} |
| 4020 |
goto LcopyArg; |
| 4021 |
} |
| 4022 |
id = p.ident; // argument copy is not need. |
| 4023 |
} |
| 4024 |
else if (p.storageClass & STC.ref_) |
| 4025 |
{ |
| 4026 |
// default delegate parameters are marked as ref, then |
| 4027 |
// argument copy is not need. |
| 4028 |
id = p.ident; |
| 4029 |
} |
| 4030 |
else |
| 4031 |
{ |
| 4032 |
// Make a copy of the ref argument so it isn't |
| 4033 |
// a reference. |
| 4034 |
LcopyArg: |
| 4035 |
id = Identifier.generateId("__applyArg", cast(int)i); |
| 4036 |
|
| 4037 |
Initializer ie = new ExpInitializer(fs.loc, new IdentifierExp(fs.loc, id)); |
| 4038 |
auto v = new VarDeclaration(fs.loc, p.type, p.ident, ie); |
| 4039 |
v.storage_class |= STC.temp | (stc & STC.scope_); |
| 4040 |
Statement s = new ExpStatement(fs.loc, v); |
| 4041 |
fs._body = new CompoundStatement(fs.loc, s, fs._body); |
| 4042 |
} |
| 4043 |
params.push(new Parameter(fs.loc, stc, p.type, id, null, null)); |
| 4044 |
} |
| 4045 |
// https://issues.dlang.org/show_bug.cgi?id=13840 |
| 4046 |
// Throwable nested function inside nothrow function is acceptable. |
| 4047 |
STC stc = mergeFuncAttrs(STC.safe | STC.pure_ | STC.nogc, fs.func); |
| 4048 |
auto tf = new TypeFunction(ParameterList(params), Type.tint32, LINK.d, stc); |
| 4049 |
fs.cases = new Statements(); |
| 4050 |
fs.gotos = new ScopeStatements(); |
| 4051 |
auto fld = new FuncLiteralDeclaration(fs.loc, fs.endloc, tf, TOK.delegate_, fs); |
| 4052 |
fld.fbody = fs._body; |
| 4053 |
Expression flde = new FuncExp(fs.loc, fld); |
| 4054 |
flde = flde.expressionSemantic(sc); |
| 4055 |
fld.tookAddressOf = 0; |
| 4056 |
if (flde.op == EXP.error) |
| 4057 |
return null; |
| 4058 |
return flde.isFuncExp(); |
| 4059 |
} |
| 4060 |
|
| 4061 |
|
| 4062 |
void catchSemantic(Catch c, Scope* sc) |
| 4063 |
{ |
| 4064 |
//printf("Catch::semantic(%s)\n", ident.toChars()); |
| 4065 |
|
| 4066 |
if (sc.scopeGuard && sc.scopeGuard.tok != TOK.onScopeFailure) |
| 4067 |
{ |
| 4068 |
// If enclosing is scope(success) or scope(exit), this will be placed in finally block. |
| 4069 |
error(c.loc, "cannot put `catch` statement inside `%s`", Token.toChars(sc.scopeGuard.tok)); |
| 4070 |
c.errors = true; |
| 4071 |
} |
| 4072 |
if (sc.tryFinally) |
| 4073 |
{ |
| 4074 |
/* This is because the _d_local_unwind() gets the stack munged |
| 4075 |
* up on this. The workaround is to place any try-catches into |
| 4076 |
* a separate function, and call that. |
| 4077 |
* To fix, have the compiler automatically convert the finally |
| 4078 |
* body into a nested function. |
| 4079 |
*/ |
| 4080 |
error(c.loc, "cannot put `catch` statement inside `finally` block"); |
| 4081 |
c.errors = true; |
| 4082 |
} |
| 4083 |
|
| 4084 |
auto sym = new ScopeDsymbol(); |
| 4085 |
sym.parent = sc.scopesym; |
| 4086 |
sc = sc.push(sym); |
| 4087 |
|
| 4088 |
if (!c.type) |
| 4089 |
{ |
| 4090 |
error(c.loc, "`catch` statement without an exception specification is deprecated"); |
| 4091 |
errorSupplemental(c.loc, "use `catch(Throwable)` for old behavior"); |
| 4092 |
c.errors = true; |
| 4093 |
|
| 4094 |
// reference .object.Throwable |
| 4095 |
c.type = getThrowable(); |
| 4096 |
} |
| 4097 |
else if (!c.type.isNaked() && !c.type.isConst()) |
| 4098 |
{ |
| 4099 |
// @@@DEPRECATED_2.115@@@ |
| 4100 |
// Deprecated in 2.105, change into an error & uncomment assign in 2.115 |
| 4101 |
deprecation(c.loc, "can only catch mutable or const qualified types, not `%s`", c.type.toChars()); |
| 4102 |
//c.errors = true; |
| 4103 |
} |
| 4104 |
c.type = c.type.typeSemantic(c.loc, sc); |
| 4105 |
if (c.type == Type.terror) |
| 4106 |
{ |
| 4107 |
c.errors = true; |
| 4108 |
sc.pop(); |
| 4109 |
return; |
| 4110 |
} |
| 4111 |
|
| 4112 |
STC stc; |
| 4113 |
auto cd = c.type.toBasetype().isClassHandle(); |
| 4114 |
if (!cd) |
| 4115 |
{ |
| 4116 |
error(c.loc, "can only catch class objects, not `%s`", c.type.toChars()); |
| 4117 |
c.errors = true; |
| 4118 |
} |
| 4119 |
else if (cd.isCPPclass()) |
| 4120 |
{ |
| 4121 |
if (!target.cpp.exceptions) |
| 4122 |
{ |
| 4123 |
error(c.loc, "catching C++ class objects not supported for this target"); |
| 4124 |
c.errors = true; |
| 4125 |
} |
| 4126 |
if (!c.internalCatch) |
| 4127 |
{ |
| 4128 |
if (sc.setUnsafe(false, c.loc, "catching C++ class objects")) |
| 4129 |
c.errors = true; |
| 4130 |
} |
| 4131 |
} |
| 4132 |
else if (cd != ClassDeclaration.throwable && !ClassDeclaration.throwable.isBaseOf(cd, null)) |
| 4133 |
{ |
| 4134 |
error(c.loc, "can only catch class objects derived from `Throwable`, not `%s`", c.type.toChars()); |
| 4135 |
c.errors = true; |
| 4136 |
} |
| 4137 |
else if (!c.internalCatch && ClassDeclaration.exception && |
| 4138 |
cd != ClassDeclaration.exception && !ClassDeclaration.exception.isBaseOf(cd, null) && |
| 4139 |
sc.setUnsafe(false, c.loc, |
| 4140 |
"catching class objects derived from `%s` instead of `Exception`", c.type)) |
| 4141 |
{ |
| 4142 |
c.errors = true; |
| 4143 |
} |
| 4144 |
else if (sc.previews.dip1008) |
| 4145 |
{ |
| 4146 |
stc |= STC.scope_; |
| 4147 |
} |
| 4148 |
|
| 4149 |
// DIP1008 requires destruction of the Throwable, even if the user didn't specify an identifier |
| 4150 |
auto ident = c.ident; |
| 4151 |
if (!ident && sc.previews.dip1008) |
| 4152 |
ident = Identifier.generateAnonymousId("var"); |
| 4153 |
|
| 4154 |
if (ident) |
| 4155 |
{ |
| 4156 |
c.var = new VarDeclaration(c.loc, c.type, ident, null, stc); |
| 4157 |
c.var.iscatchvar = true; |
| 4158 |
c.var.dsymbolSemantic(sc); |
| 4159 |
sc.insert(c.var); |
| 4160 |
|
| 4161 |
if (sc.previews.dip1008 && stc & STC.scope_) |
| 4162 |
{ |
| 4163 |
/* Add a destructor for c.var |
| 4164 |
* try { handler } finally { if (!__ctfe) _d_delThrowable(var); } |
| 4165 |
*/ |
| 4166 |
assert(!c.var.edtor); // ensure we didn't create one in callScopeDtor() |
| 4167 |
|
| 4168 |
Loc loc = c.loc; |
| 4169 |
Expression e = new VarExp(loc, c.var); |
| 4170 |
e = new CallExp(loc, new IdentifierExp(loc, Id._d_delThrowable), e); |
| 4171 |
|
| 4172 |
Expression ec = new IdentifierExp(loc, Id.ctfe); |
| 4173 |
ec = new NotExp(loc, ec); |
| 4174 |
Statement s = new IfStatement(loc, null, ec, new ExpStatement(loc, e), null, loc); |
| 4175 |
c.handler = new TryFinallyStatement(loc, c.handler, s); |
| 4176 |
} |
| 4177 |
|
| 4178 |
} |
| 4179 |
c.handler = c.handler.statementSemantic(sc); |
| 4180 |
if (c.handler && c.handler.isErrorStatement()) |
| 4181 |
c.errors = true; |
| 4182 |
|
| 4183 |
sc.pop(); |
| 4184 |
} |
| 4185 |
|
| 4186 |
Statement semanticNoScope(Statement s, Scope* sc) |
| 4187 |
{ |
| 4188 |
//printf("Statement::semanticNoScope() %s\n", toChars()); |
| 4189 |
if (!s.isCompoundStatement() && !s.isScopeStatement()) |
| 4190 |
{ |
| 4191 |
s = new CompoundStatement(s.loc, s); // so scopeCode() gets called |
| 4192 |
} |
| 4193 |
s = s.statementSemantic(sc); |
| 4194 |
return s; |
| 4195 |
} |
| 4196 |
|
| 4197 |
// Same as semanticNoScope(), but do create a new scope |
| 4198 |
private Statement semanticScope(Statement s, Scope* sc, Statement sbreak, Statement scontinue, Statement tryBody) |
| 4199 |
{ |
| 4200 |
auto sym = new ScopeDsymbol(); |
| 4201 |
sym.parent = sc.scopesym; |
| 4202 |
Scope* scd = sc.push(sym); |
| 4203 |
if (sbreak) |
| 4204 |
scd.sbreak = sbreak; |
| 4205 |
if (scontinue) |
| 4206 |
scd.scontinue = scontinue; |
| 4207 |
if (tryBody) |
| 4208 |
scd.tryBody = tryBody; |
| 4209 |
s = s.semanticNoScope(scd); |
| 4210 |
scd.pop(); |
| 4211 |
return s; |
| 4212 |
} |
| 4213 |
|
| 4214 |
|
| 4215 |
/**************************************** |
| 4216 |
* If `statement` has code that needs to run in a finally clause |
| 4217 |
* at the end of the current scope, return that code in the form of |
| 4218 |
* a Statement. |
| 4219 |
* Params: |
| 4220 |
* statement = the statement |
| 4221 |
* sc = context |
| 4222 |
* sentry = set to code executed upon entry to the scope |
| 4223 |
* sexception = set to code executed upon exit from the scope via exception |
| 4224 |
* sfinally = set to code executed in finally block |
| 4225 |
* Returns: |
| 4226 |
* code to be run in the finally clause |
| 4227 |
*/ |
| 4228 |
Statement scopeCode(Statement statement, Scope* sc, out Statement sentry, out Statement sexception, out Statement sfinally) |
| 4229 |
{ |
| 4230 |
if (auto es = statement.isExpStatement()) |
| 4231 |
{ |
| 4232 |
if (es.exp && es.exp.isDeclarationExp()) |
| 4233 |
{ |
| 4234 |
auto de = es.exp.isDeclarationExp(); |
| 4235 |
auto v = de.declaration.isVarDeclaration(); |
| 4236 |
if (v && !v.isDataseg()) |
| 4237 |
{ |
| 4238 |
if (v.needsScopeDtor()) |
| 4239 |
{ |
| 4240 |
sfinally = new DtorExpStatement(es.loc, v.edtor, v); |
| 4241 |
v.storage_class |= STC.nodtor; // don't add in dtor again |
| 4242 |
} |
| 4243 |
} |
| 4244 |
} |
| 4245 |
return es; |
| 4246 |
|
| 4247 |
} |
| 4248 |
else if (auto sgs = statement.isScopeGuardStatement()) |
| 4249 |
{ |
| 4250 |
Statement s = new PeelStatement(sgs.statement); |
| 4251 |
|
| 4252 |
switch (sgs.tok) |
| 4253 |
{ |
| 4254 |
case TOK.onScopeExit: |
| 4255 |
sfinally = s; |
| 4256 |
break; |
| 4257 |
|
| 4258 |
case TOK.onScopeFailure: |
| 4259 |
sexception = s; |
| 4260 |
break; |
| 4261 |
|
| 4262 |
case TOK.onScopeSuccess: |
| 4263 |
{ |
| 4264 |
/* Create: |
| 4265 |
* sentry: bool x = false; |
| 4266 |
* sexception: x = true; |
| 4267 |
* sfinally: if (!x) statement; |
| 4268 |
*/ |
| 4269 |
auto v = copyToTemp(STC.none, "__os", IntegerExp.createBool(false)); |
| 4270 |
v.dsymbolSemantic(sc); |
| 4271 |
sentry = new ExpStatement(statement.loc, v); |
| 4272 |
|
| 4273 |
Expression e = IntegerExp.createBool(true); |
| 4274 |
e = new AssignExp(Loc.initial, new VarExp(Loc.initial, v), e); |
| 4275 |
sexception = new ExpStatement(Loc.initial, e); |
| 4276 |
|
| 4277 |
e = new VarExp(Loc.initial, v); |
| 4278 |
e = new NotExp(Loc.initial, e); |
| 4279 |
sfinally = new IfStatement(Loc.initial, null, e, s, null, Loc.initial); |
| 4280 |
|
| 4281 |
break; |
| 4282 |
} |
| 4283 |
default: |
| 4284 |
assert(0); |
| 4285 |
} |
| 4286 |
return null; |
| 4287 |
} |
| 4288 |
else if (auto ls = statement.isLabelStatement()) |
| 4289 |
{ |
| 4290 |
if (ls.statement) |
| 4291 |
ls.statement = ls.statement.scopeCode(sc, sentry, sexception, sfinally); |
| 4292 |
return ls; |
| 4293 |
} |
| 4294 |
|
| 4295 |
return statement; |
| 4296 |
} |
| 4297 |
|
| 4298 |
/******************* |
| 4299 |
* Type check and unroll `foreach` over an expression tuple as well |
| 4300 |
* as `static foreach` statements and `static foreach` |
| 4301 |
* declarations. For `static foreach` statements and `static |
| 4302 |
* foreach` declarations, the visitor interface is used (and the |
| 4303 |
* result is written into the `result` field.) For `static |
| 4304 |
* foreach` declarations, the resulting Dsymbols* are returned |
| 4305 |
* directly. |
| 4306 |
* |
| 4307 |
* The unrolled body is wrapped into a |
| 4308 |
* - UnrolledLoopStatement, for `foreach` over an expression tuple. |
| 4309 |
* - ForwardingStatement, for `static foreach` statements. |
| 4310 |
* - ForwardingAttribDeclaration, for `static foreach` declarations. |
| 4311 |
* |
| 4312 |
* `static foreach` variables are declared as `STC.local`, such |
| 4313 |
* that they are inserted into the local symbol tables of the |
| 4314 |
* forwarding constructs instead of forwarded. For `static |
| 4315 |
* foreach` with multiple foreach loop variables whose aggregate |
| 4316 |
* has been lowered into a sequence of tuples, this function |
| 4317 |
* expands the tuples into multiple `STC.local` `static foreach` |
| 4318 |
* variables. |
| 4319 |
*/ |
| 4320 |
public auto makeTupleForeach(Scope* sc, bool isStatic, bool isDecl, ForeachStatement fs, Dsymbols* dbody, bool needExpansion) |
| 4321 |
{ |
| 4322 |
// Voldemort return type |
| 4323 |
union U |
| 4324 |
{ |
| 4325 |
Statement statement; |
| 4326 |
Dsymbols* decl; |
| 4327 |
} |
| 4328 |
|
| 4329 |
U result; |
| 4330 |
|
| 4331 |
auto returnEarly() |
| 4332 |
{ |
| 4333 |
if (isDecl) |
| 4334 |
result.decl = null; |
| 4335 |
else |
| 4336 |
result.statement = new ErrorStatement(); |
| 4337 |
return result; |
| 4338 |
} |
| 4339 |
|
| 4340 |
auto loc = fs.loc; |
| 4341 |
size_t dim = fs.parameters.length; |
| 4342 |
const bool skipCheck = isStatic && needExpansion; |
| 4343 |
if (!skipCheck && (dim < 1 || dim > 2)) |
| 4344 |
{ |
| 4345 |
error(fs.loc, "only one (value) or two (key,value) arguments allowed for sequence `foreach`"); |
| 4346 |
return returnEarly(); |
| 4347 |
} |
| 4348 |
|
| 4349 |
Type paramtype = (*fs.parameters)[dim - 1].type; |
| 4350 |
if (paramtype) |
| 4351 |
{ |
| 4352 |
paramtype = paramtype.typeSemantic(loc, sc); |
| 4353 |
if (paramtype.ty == Terror) |
| 4354 |
{ |
| 4355 |
return returnEarly(); |
| 4356 |
} |
| 4357 |
} |
| 4358 |
|
| 4359 |
Type tab = fs.aggr.type.toBasetype(); |
| 4360 |
TypeTuple tuple = tab.isTypeTuple(); |
| 4361 |
|
| 4362 |
Statements* statements; |
| 4363 |
Dsymbols* declarations; |
| 4364 |
if (isDecl) |
| 4365 |
declarations = new Dsymbols(); |
| 4366 |
else |
| 4367 |
statements = new Statements(); |
| 4368 |
|
| 4369 |
//printf("aggr: op = %d, %s\n", fs.aggr.op, fs.aggr.toChars()); |
| 4370 |
size_t n; |
| 4371 |
TupleExp te = null; |
| 4372 |
if (auto ate = fs.aggr.isTupleExp()) // expression tuple |
| 4373 |
{ |
| 4374 |
te = ate; |
| 4375 |
n = te.exps.length; |
| 4376 |
} |
| 4377 |
else if (fs.aggr.isTypeExp()) // type tuple |
| 4378 |
{ |
| 4379 |
n = Parameter.dim(tuple.arguments); |
| 4380 |
} |
| 4381 |
else |
| 4382 |
assert(0); |
| 4383 |
foreach (j; 0 .. n) |
| 4384 |
{ |
| 4385 |
size_t k = (fs.op == TOK.foreach_) ? j : n - 1 - j; |
| 4386 |
Expression e = null; |
| 4387 |
Type t = null; |
| 4388 |
if (te) |
| 4389 |
e = (*te.exps)[k]; |
| 4390 |
else |
| 4391 |
t = Parameter.getNth(tuple.arguments, k).type; |
| 4392 |
Parameter p = (*fs.parameters)[0]; |
| 4393 |
|
| 4394 |
Statements* stmts; |
| 4395 |
Dsymbols* decls; |
| 4396 |
if (isDecl) |
| 4397 |
decls = new Dsymbols(); |
| 4398 |
else |
| 4399 |
stmts = new Statements(); |
| 4400 |
|
| 4401 |
const bool skip = isStatic && needExpansion; |
| 4402 |
if (!skip && dim == 2) |
| 4403 |
{ |
| 4404 |
// Declare key |
| 4405 |
if (p.isReference() || p.isLazy()) |
| 4406 |
{ |
| 4407 |
error(fs.loc, "no storage class for key `%s`", p.ident.toChars()); |
| 4408 |
return returnEarly(); |
| 4409 |
} |
| 4410 |
|
| 4411 |
if (isStatic) |
| 4412 |
{ |
| 4413 |
if (!p.type) |
| 4414 |
{ |
| 4415 |
p.type = Type.tsize_t; |
| 4416 |
} |
| 4417 |
} |
| 4418 |
p.type = p.type.typeSemantic(loc, sc); |
| 4419 |
|
| 4420 |
if (!p.type.isIntegral()) |
| 4421 |
{ |
| 4422 |
error(fs.loc, "foreach: key cannot be of non-integral type `%s`", |
| 4423 |
p.type.toChars()); |
| 4424 |
return returnEarly(); |
| 4425 |
} |
| 4426 |
|
| 4427 |
const length = te ? te.exps.length : tuple.arguments.length; |
| 4428 |
IntRange dimrange = IntRange(SignExtendedNumber(length))._cast(Type.tsize_t); |
| 4429 |
// https://issues.dlang.org/show_bug.cgi?id=12504 |
| 4430 |
dimrange.imax = SignExtendedNumber(dimrange.imax.value-1); |
| 4431 |
if (!IntRange.fromType(p.type).contains(dimrange)) |
| 4432 |
{ |
| 4433 |
error(fs.loc, "index type `%s` cannot cover index range 0..%llu", |
| 4434 |
p.type.toChars(), cast(ulong)length); |
| 4435 |
return returnEarly(); |
| 4436 |
} |
| 4437 |
Initializer ie = new ExpInitializer(Loc.initial, new IntegerExp(k)); |
| 4438 |
auto var = new VarDeclaration(loc, p.type, p.ident, ie); |
| 4439 |
var.storage_class |= STC.foreach_ | STC.manifest; |
| 4440 |
if (isStatic) |
| 4441 |
var.storage_class |= STC.local; |
| 4442 |
|
| 4443 |
if (isDecl) |
| 4444 |
decls.push(var); |
| 4445 |
else |
| 4446 |
stmts.push(new ExpStatement(loc, var)); |
| 4447 |
|
| 4448 |
p = (*fs.parameters)[1]; // value |
| 4449 |
} |
| 4450 |
/*********************** |
| 4451 |
* Declares a unrolled `foreach` loop variable or a `static foreach` variable. |
| 4452 |
* |
| 4453 |
* Params: |
| 4454 |
* storageClass = The storage class of the variable. |
| 4455 |
* type = The declared type of the variable. |
| 4456 |
* ident = The name of the variable. |
| 4457 |
* e = The initializer of the variable (i.e. the current element of the looped over aggregate). |
| 4458 |
* t = The type of the initializer. |
| 4459 |
* Returns: |
| 4460 |
* `true` iff the declaration was successful. |
| 4461 |
*/ |
| 4462 |
bool declareVariable(STC storageClass, Type type, Identifier ident, Expression e, Type t) |
| 4463 |
{ |
| 4464 |
if (storageClass & (STC.out_ | STC.lazy_) || |
| 4465 |
storageClass & STC.ref_ && !te) |
| 4466 |
{ |
| 4467 |
error(fs.loc, "no storage class for value `%s`", ident.toChars()); |
| 4468 |
return false; |
| 4469 |
} |
| 4470 |
Declaration var; |
| 4471 |
if (e) |
| 4472 |
{ |
| 4473 |
Type tb = e.type.toBasetype(); |
| 4474 |
Dsymbol ds = null; |
| 4475 |
if (!(storageClass & STC.manifest)) |
| 4476 |
{ |
| 4477 |
if (isStatic || tb.ty == Tfunction || storageClass & STC.alias_) |
| 4478 |
{ |
| 4479 |
if (auto ve = e.isVarExp()) |
| 4480 |
ds = ve.var; |
| 4481 |
else if (auto dve = e.isDotVarExp()) |
| 4482 |
ds = dve.var; |
| 4483 |
} |
| 4484 |
if (auto te = e.isTemplateExp()) |
| 4485 |
ds = te.td; |
| 4486 |
else if (auto se = e.isScopeExp()) |
| 4487 |
ds = se.sds; |
| 4488 |
else if (auto fe = e.isFuncExp()) |
| 4489 |
ds = fe.td ? fe.td : fe.fd; |
| 4490 |
else if (auto oe = e.isOverExp()) |
| 4491 |
ds = oe.vars; |
| 4492 |
} |
| 4493 |
else if (storageClass & STC.alias_) |
| 4494 |
{ |
| 4495 |
error(fs.loc, "`foreach` loop variable cannot be both `enum` and `alias`"); |
| 4496 |
return false; |
| 4497 |
} |
| 4498 |
|
| 4499 |
if (ds) |
| 4500 |
{ |
| 4501 |
var = new AliasDeclaration(loc, ident, ds); |
| 4502 |
if (storageClass & STC.ref_) |
| 4503 |
{ |
| 4504 |
error(fs.loc, "symbol `%s` cannot be `ref`", ds.toChars()); |
| 4505 |
return false; |
| 4506 |
} |
| 4507 |
if (paramtype) |
| 4508 |
{ |
| 4509 |
error(fs.loc, "cannot specify element type for symbol `%s`", ds.toChars()); |
| 4510 |
return false; |
| 4511 |
} |
| 4512 |
} |
| 4513 |
else if (e.op == EXP.type) |
| 4514 |
{ |
| 4515 |
var = new AliasDeclaration(loc, ident, e.type); |
| 4516 |
if (paramtype) |
| 4517 |
{ |
| 4518 |
error(fs.loc, "cannot specify element type for type `%s`", e.type.toChars()); |
| 4519 |
return false; |
| 4520 |
} |
| 4521 |
} |
| 4522 |
else |
| 4523 |
{ |
| 4524 |
e = resolveProperties(sc, e); |
| 4525 |
Initializer ie = new ExpInitializer(Loc.initial, e); |
| 4526 |
auto v = new VarDeclaration(loc, type, ident, ie, storageClass); |
| 4527 |
v.storage_class |= STC.foreach_; |
| 4528 |
if (storageClass & STC.ref_) |
| 4529 |
v.storage_class |= STC.ref_; |
| 4530 |
if (isStatic || storageClass&STC.manifest || e.isConst() || |
| 4531 |
e.op == EXP.string_ || |
| 4532 |
e.op == EXP.structLiteral || |
| 4533 |
e.op == EXP.arrayLiteral) |
| 4534 |
{ |
| 4535 |
if (v.storage_class & STC.ref_) |
| 4536 |
{ |
| 4537 |
if (!isStatic) |
| 4538 |
{ |
| 4539 |
error(fs.loc, "constant value `%s` cannot be `ref`", toChars(ie)); |
| 4540 |
} |
| 4541 |
else |
| 4542 |
{ |
| 4543 |
if (!needExpansion) |
| 4544 |
{ |
| 4545 |
error(fs.loc, "constant value `%s` cannot be `ref`", toChars(ie)); |
| 4546 |
} |
| 4547 |
else |
| 4548 |
{ |
| 4549 |
error(fs.loc, "constant value `%s` cannot be `ref`", ident.toChars()); |
| 4550 |
} |
| 4551 |
} |
| 4552 |
return false; |
| 4553 |
} |
| 4554 |
else |
| 4555 |
v.storage_class |= STC.manifest; |
| 4556 |
} |
| 4557 |
var = v; |
| 4558 |
} |
| 4559 |
} |
| 4560 |
else |
| 4561 |
{ |
| 4562 |
var = new AliasDeclaration(loc, ident, t); |
| 4563 |
if (paramtype) |
| 4564 |
{ |
| 4565 |
error(fs.loc, "cannot specify element type for symbol `%s`", fs.toChars()); |
| 4566 |
return false; |
| 4567 |
} |
| 4568 |
} |
| 4569 |
if (isStatic) |
| 4570 |
{ |
| 4571 |
var.storage_class |= STC.local; |
| 4572 |
} |
| 4573 |
|
| 4574 |
if (isDecl) |
| 4575 |
decls.push(var); |
| 4576 |
else |
| 4577 |
stmts.push(new ExpStatement(loc, var)); |
| 4578 |
return true; |
| 4579 |
} |
| 4580 |
|
| 4581 |
if (!isStatic) |
| 4582 |
{ |
| 4583 |
// Declare value |
| 4584 |
if (!declareVariable(p.storageClass, p.type, p.ident, e, t)) |
| 4585 |
{ |
| 4586 |
return returnEarly(); |
| 4587 |
} |
| 4588 |
} |
| 4589 |
else |
| 4590 |
{ |
| 4591 |
if (!needExpansion) |
| 4592 |
{ |
| 4593 |
// Declare value |
| 4594 |
if (!declareVariable(p.storageClass, p.type, p.ident, e, t)) |
| 4595 |
{ |
| 4596 |
return returnEarly(); |
| 4597 |
} |
| 4598 |
} |
| 4599 |
else |
| 4600 |
{ // expand tuples into multiple `static foreach` variables. |
| 4601 |
assert(e && !t); |
| 4602 |
auto ident = Identifier.generateId("__value"); |
| 4603 |
declareVariable(STC.none, e.type, ident, e, null); |
| 4604 |
import dmd.cond: StaticForeach; |
| 4605 |
auto field = Identifier.idPool(StaticForeach.tupleFieldName.ptr,StaticForeach.tupleFieldName.length); |
| 4606 |
Expression access = new DotIdExp(loc, e, field); |
| 4607 |
access = expressionSemantic(access, sc); |
| 4608 |
access = access.optimize(WANTvalue); |
| 4609 |
if (!tuple) return returnEarly(); |
| 4610 |
//printf("%s\n",tuple.toChars()); |
| 4611 |
foreach (l; 0 .. dim) |
| 4612 |
{ |
| 4613 |
auto cp = (*fs.parameters)[l]; |
| 4614 |
Expression init_ = new IndexExp(loc, access, new IntegerExp(loc, l, Type.tsize_t)); |
| 4615 |
init_ = init_.expressionSemantic(sc); |
| 4616 |
assert(init_.type); |
| 4617 |
declareVariable(p.storageClass, init_.type, cp.ident, init_, null); |
| 4618 |
} |
| 4619 |
} |
| 4620 |
} |
| 4621 |
|
| 4622 |
Statement s; |
| 4623 |
Dsymbol d; |
| 4624 |
if (isDecl) |
| 4625 |
decls.append(Dsymbol.arraySyntaxCopy(dbody)); |
| 4626 |
else |
| 4627 |
{ |
| 4628 |
stmts.push(fs._body.syntaxCopy()); |
| 4629 |
s = new CompoundStatement(loc, stmts); |
| 4630 |
} |
| 4631 |
|
| 4632 |
if (!isStatic) |
| 4633 |
{ |
| 4634 |
s = new ScopeStatement(loc, s, fs.endloc); |
| 4635 |
} |
| 4636 |
else if (isDecl) |
| 4637 |
{ |
| 4638 |
import dmd.attrib: ForwardingAttribDeclaration; |
| 4639 |
d = new ForwardingAttribDeclaration(decls); |
| 4640 |
} |
| 4641 |
else |
| 4642 |
{ |
| 4643 |
s = new ForwardingStatement(loc, s); |
| 4644 |
} |
| 4645 |
|
| 4646 |
if (isDecl) |
| 4647 |
declarations.push(d); |
| 4648 |
else |
| 4649 |
statements.push(s); |
| 4650 |
} |
| 4651 |
|
| 4652 |
if (!isStatic) |
| 4653 |
{ |
| 4654 |
Statement res = new UnrolledLoopStatement(loc, statements); |
| 4655 |
if (LabelStatement ls = checkLabeledLoop(sc, fs)) |
| 4656 |
ls.gotoTarget = res; |
| 4657 |
if (te && te.e0) |
| 4658 |
res = new CompoundStatement(loc, new ExpStatement(te.e0.loc, te.e0), res); |
| 4659 |
result.statement = res; |
| 4660 |
} |
| 4661 |
else if (isDecl) |
| 4662 |
result.decl = declarations; |
| 4663 |
else |
| 4664 |
result.statement = new CompoundStatement(loc, statements); |
| 4665 |
|
| 4666 |
return result; |
| 4667 |
} |
| 4668 |
|
| 4669 |
/********************************* |
| 4670 |
* Flatten out the scope by presenting `statement` |
| 4671 |
* as an array of statements. |
| 4672 |
* Params: |
| 4673 |
* statement = the statement to flatten |
| 4674 |
* sc = context |
| 4675 |
* Returns: |
| 4676 |
* The array of `Statements`, or `null` if no flattening necessary |
| 4677 |
*/ |
| 4678 |
private Statements* flatten(Statement statement, Scope* sc) |
| 4679 |
{ |
| 4680 |
static auto errorStatements() |
| 4681 |
{ |
| 4682 |
auto a = new Statements(); |
| 4683 |
a.push(new ErrorStatement()); |
| 4684 |
return a; |
| 4685 |
} |
| 4686 |
|
| 4687 |
|
| 4688 |
/*compound and expression statements have classes that inherit from them with the same |
| 4689 |
*flattening behavior, so the isXXX methods won't work |
| 4690 |
*/ |
| 4691 |
switch(statement.stmt) |
| 4692 |
{ |
| 4693 |
case STMT.Compound: |
| 4694 |
case STMT.CompoundDeclaration: |
| 4695 |
return (cast(CompoundStatement)statement).statements; |
| 4696 |
|
| 4697 |
case STMT.Exp: |
| 4698 |
case STMT.DtorExp: |
| 4699 |
auto es = cast(ExpStatement)statement; |
| 4700 |
/* https://issues.dlang.org/show_bug.cgi?id=14243 |
| 4701 |
* expand template mixin in statement scope |
| 4702 |
* to handle variable destructors. |
| 4703 |
*/ |
| 4704 |
if (!es.exp || !es.exp.isDeclarationExp()) |
| 4705 |
return null; |
| 4706 |
|
| 4707 |
Dsymbol d = es.exp.isDeclarationExp().declaration; |
| 4708 |
auto tm = d.isTemplateMixin(); |
| 4709 |
if (!tm) |
| 4710 |
return null; |
| 4711 |
|
| 4712 |
Expression e = es.exp.expressionSemantic(sc); |
| 4713 |
if (e.op == EXP.error || tm.errors) |
| 4714 |
return errorStatements(); |
| 4715 |
assert(tm.members); |
| 4716 |
|
| 4717 |
Statement s = toStatement(tm); |
| 4718 |
version (none) |
| 4719 |
{ |
| 4720 |
OutBuffer buf; |
| 4721 |
buf.doindent = 1; |
| 4722 |
HdrGenState hgs; |
| 4723 |
hgs.hdrgen = true; |
| 4724 |
toCBuffer(s, &buf, &hgs); |
| 4725 |
printf("tm ==> s = %s\n", buf.peekChars()); |
| 4726 |
} |
| 4727 |
auto a = new Statements(); |
| 4728 |
a.push(s); |
| 4729 |
return a; |
| 4730 |
|
| 4731 |
case STMT.Forwarding: |
| 4732 |
/*********************** |
| 4733 |
* ForwardingStatements are distributed over the flattened |
| 4734 |
* sequence of statements. This prevents flattening to be |
| 4735 |
* "blocked" by a ForwardingStatement and is necessary, for |
| 4736 |
* example, to support generating scope guards with `static |
| 4737 |
* foreach`: |
| 4738 |
* |
| 4739 |
* static foreach(i; 0 .. 10) scope(exit) writeln(i); |
| 4740 |
* writeln("this is printed first"); |
| 4741 |
* // then, it prints 10, 9, 8, 7, ... |
| 4742 |
*/ |
| 4743 |
auto fs = statement.isForwardingStatement(); |
| 4744 |
if (!fs.statement) |
| 4745 |
{ |
| 4746 |
return null; |
| 4747 |
} |
| 4748 |
sc = sc.push(fs.sym); |
| 4749 |
auto a = fs.statement.flatten(sc); |
| 4750 |
sc = sc.pop(); |
| 4751 |
if (!a) |
| 4752 |
{ |
| 4753 |
return a; |
| 4754 |
} |
| 4755 |
auto b = new Statements(a.length); |
| 4756 |
foreach (i, s; *a) |
| 4757 |
{ |
| 4758 |
(*b)[i] = s ? new ForwardingStatement(s.loc, fs.sym, s) : null; |
| 4759 |
} |
| 4760 |
return b; |
| 4761 |
|
| 4762 |
case STMT.Conditional: |
| 4763 |
auto cs = statement.isConditionalStatement(); |
| 4764 |
Statement s; |
| 4765 |
|
| 4766 |
//printf("ConditionalStatement::flatten()\n"); |
| 4767 |
if (cs.condition.include(sc)) |
| 4768 |
{ |
| 4769 |
DebugCondition dc = cs.condition.isDebugCondition(); |
| 4770 |
if (dc) |
| 4771 |
{ |
| 4772 |
s = new DebugStatement(cs.loc, cs.ifbody); |
| 4773 |
} |
| 4774 |
else |
| 4775 |
s = cs.ifbody; |
| 4776 |
} |
| 4777 |
else |
| 4778 |
s = cs.elsebody; |
| 4779 |
|
| 4780 |
auto a = new Statements(); |
| 4781 |
a.push(s); |
| 4782 |
return a; |
| 4783 |
|
| 4784 |
case STMT.StaticForeach: |
| 4785 |
auto sfs = statement.isStaticForeachStatement(); |
| 4786 |
sfs.sfe.prepare(sc); |
| 4787 |
if (sfs.sfe.ready()) |
| 4788 |
{ |
| 4789 |
Statement s = makeTupleForeach(sc, true, false, sfs.sfe.aggrfe, null, sfs.sfe.needExpansion).statement; |
| 4790 |
auto result = s.flatten(sc); |
| 4791 |
if (result) |
| 4792 |
{ |
| 4793 |
return result; |
| 4794 |
} |
| 4795 |
result = new Statements(); |
| 4796 |
result.push(s); |
| 4797 |
return result; |
| 4798 |
} |
| 4799 |
else |
| 4800 |
return errorStatements(); |
| 4801 |
|
| 4802 |
case STMT.Debug: |
| 4803 |
auto ds = statement.isDebugStatement(); |
| 4804 |
Statements* a = ds.statement ? ds.statement.flatten(sc) : null; |
| 4805 |
if (!a) |
| 4806 |
return null; |
| 4807 |
|
| 4808 |
foreach (ref s; *a) |
| 4809 |
{ |
| 4810 |
s = new DebugStatement(ds.loc, s); |
| 4811 |
} |
| 4812 |
return a; |
| 4813 |
|
| 4814 |
case STMT.Label: |
| 4815 |
auto ls = statement.isLabelStatement(); |
| 4816 |
if (!ls.statement) |
| 4817 |
return null; |
| 4818 |
|
| 4819 |
Statements* a = null; |
| 4820 |
a = ls.statement.flatten(sc); |
| 4821 |
if (!a) |
| 4822 |
return null; |
| 4823 |
|
| 4824 |
if (!a.length) |
| 4825 |
{ |
| 4826 |
a.push(new ExpStatement(ls.loc, cast(Expression)null)); |
| 4827 |
} |
| 4828 |
|
| 4829 |
// reuse 'this' LabelStatement |
| 4830 |
ls.statement = (*a)[0]; |
| 4831 |
(*a)[0] = ls; |
| 4832 |
return a; |
| 4833 |
|
| 4834 |
case STMT.Mixin: |
| 4835 |
auto cs = statement.isMixinStatement(); |
| 4836 |
|
| 4837 |
|
| 4838 |
OutBuffer buf; |
| 4839 |
if (expressionsToString(buf, sc, cs.exps, cs.loc, null, true)) |
| 4840 |
return errorStatements(); |
| 4841 |
|
| 4842 |
const errors = global.errors; |
| 4843 |
const len = buf.length; |
| 4844 |
buf.writeByte(0); |
| 4845 |
const str = buf.extractSlice()[0 .. len]; |
| 4846 |
const bool doUnittests = global.params.parsingUnittestsRequired(); |
| 4847 |
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests); |
| 4848 |
adjustLocForMixin(str, cs.loc, *p.baseLoc, global.params.mixinOut); |
| 4849 |
p.linnum = p.baseLoc.startLine; |
| 4850 |
p.nextToken(); |
| 4851 |
|
| 4852 |
auto a = new Statements(); |
| 4853 |
while (p.token.value != TOK.endOfFile) |
| 4854 |
{ |
| 4855 |
Statement s = p.parseStatement(ParseStatementFlags.curlyScope); |
| 4856 |
if (!s || global.errors != errors) |
| 4857 |
return errorStatements(); |
| 4858 |
a.push(s); |
| 4859 |
} |
| 4860 |
return a; |
| 4861 |
default: |
| 4862 |
return null; |
| 4863 |
} |
| 4864 |
} |
| 4865 |
|
| 4866 |
/*********************************************************** |
| 4867 |
* Convert TemplateMixin members (which are Dsymbols) to Statements. |
| 4868 |
* Params: |
| 4869 |
* s = the symbol to convert to a Statement |
| 4870 |
* Returns: |
| 4871 |
* s redone as a Statement |
| 4872 |
*/ |
| 4873 |
private Statement toStatement(Dsymbol s) |
| 4874 |
{ |
| 4875 |
Statement result; |
| 4876 |
|
| 4877 |
if (auto tm = s.isTemplateMixin()) |
| 4878 |
{ |
| 4879 |
auto a = new Statements(); |
| 4880 |
foreach (m; *tm.members) |
| 4881 |
{ |
| 4882 |
if (Statement sx = toStatement(m)) |
| 4883 |
a.push(sx); |
| 4884 |
} |
| 4885 |
result = new CompoundStatement(tm.loc, a); |
| 4886 |
} |
| 4887 |
else if (s.isVarDeclaration() || |
| 4888 |
s.isAggregateDeclaration() || |
| 4889 |
s.isFuncDeclaration() || |
| 4890 |
s.isEnumDeclaration() || |
| 4891 |
s.isAliasDeclaration() || |
| 4892 |
s.isTemplateDeclaration()) |
| 4893 |
{ |
| 4894 |
/* Perhaps replace the above with isScopeDsymbol() || isDeclaration() |
| 4895 |
*/ |
| 4896 |
/* An actual declaration symbol will be converted to DeclarationExp |
| 4897 |
* with ExpStatement. |
| 4898 |
*/ |
| 4899 |
auto de = new DeclarationExp(s.loc, s); |
| 4900 |
de.type = Type.tvoid; // avoid repeated semantic |
| 4901 |
result = new ExpStatement(s.loc, de); |
| 4902 |
} |
| 4903 |
else if (auto d = s.isAttribDeclaration()) |
| 4904 |
{ |
| 4905 |
/* All attributes have been already picked by the semantic analysis of |
| 4906 |
* 'bottom' declarations (function, struct, class, etc). |
| 4907 |
* So we don't have to copy them. |
| 4908 |
*/ |
| 4909 |
if (Dsymbols* a = d.include(null)) |
| 4910 |
{ |
| 4911 |
auto statements = new Statements(); |
| 4912 |
foreach (sx; *a) |
| 4913 |
{ |
| 4914 |
statements.push(toStatement(sx)); |
| 4915 |
} |
| 4916 |
result = new CompoundStatement(d.loc, statements); |
| 4917 |
} |
| 4918 |
} |
| 4919 |
else if (s.isStaticAssert() || |
| 4920 |
s.isImport()) |
| 4921 |
{ |
| 4922 |
/* Ignore as they are not Statements |
| 4923 |
*/ |
| 4924 |
} |
| 4925 |
else |
| 4926 |
{ |
| 4927 |
.error(Loc.initial, "internal compiler error: cannot mixin %s `%s`\n", s.kind(), s.toChars()); |
| 4928 |
result = new ErrorStatement(); |
| 4929 |
} |
| 4930 |
|
| 4931 |
return result; |
| 4932 |
} |
| 4933 |
|
| 4934 |
/** |
| 4935 |
Marks all occurring ThrowStatements as internalThrows. |
| 4936 |
This is intended to be called from a DebugStatement as it allows |
| 4937 |
to mark all its nodes as nothrow. |
| 4938 |
|
| 4939 |
Params: |
| 4940 |
s = AST Node to traverse |
| 4941 |
*/ |
| 4942 |
private void debugThrowWalker(Statement s) |
| 4943 |
{ |
| 4944 |
if (!s) |
| 4945 |
return; |
| 4946 |
extern(C++) final class DebugWalker : SemanticTimeTransitiveVisitor |
| 4947 |
{ |
| 4948 |
alias visit = SemanticTimeTransitiveVisitor.visit; |
| 4949 |
public: |
| 4950 |
|
| 4951 |
override void visit(ThrowStatement s) |
| 4952 |
{ |
| 4953 |
s.internalThrow = true; |
| 4954 |
} |
| 4955 |
|
| 4956 |
override void visit(CallExp s) |
| 4957 |
{ |
| 4958 |
s.inDebugStatement = true; |
| 4959 |
} |
| 4960 |
} |
| 4961 |
|
| 4962 |
scope walker = new DebugWalker(); |
| 4963 |
s.accept(walker); |
| 4964 |
} |
| 4965 |
|
| 4966 |
/************************************ |
| 4967 |
* Check for skipped variable declarations. |
| 4968 |
* Params: |
| 4969 |
* ss = statement to check |
| 4970 |
* Returns: |
| 4971 |
* true if error |
| 4972 |
*/ |
| 4973 |
private bool checkLabel(SwitchStatement ss) |
| 4974 |
{ |
| 4975 |
/* |
| 4976 |
* Checks the scope of a label for existing variable declaration. |
| 4977 |
* Params: |
| 4978 |
* vd = last variable declared before this case/default label |
| 4979 |
* Returns: `true` if the variables declared in this label would be skipped. |
| 4980 |
*/ |
| 4981 |
bool checkVar(VarDeclaration vd) |
| 4982 |
{ |
| 4983 |
for (auto v = vd; v && v != ss.lastVar; v = v.lastVar) |
| 4984 |
{ |
| 4985 |
if (v.isDataseg() || (v.storage_class & (STC.manifest | STC.temp) && vd.ident != Id.withSym) || v._init.isVoidInitializer()) |
| 4986 |
continue; |
| 4987 |
if (vd.ident == Id.withSym) |
| 4988 |
error(ss.loc, "`switch` skips declaration of `with` temporary"); |
| 4989 |
else |
| 4990 |
error(ss.loc, "`switch` skips declaration of variable `%s`", v.toPrettyChars()); |
| 4991 |
errorSupplemental(v.loc, "declared here"); |
| 4992 |
return true; |
| 4993 |
} |
| 4994 |
return false; |
| 4995 |
} |
| 4996 |
|
| 4997 |
enum error = true; |
| 4998 |
|
| 4999 |
if (ss.sdefault && checkVar(ss.sdefault.lastVar)) |
| 5000 |
return !error; // return error once fully deprecated |
| 5001 |
|
| 5002 |
foreach (scase; *ss.cases) |
| 5003 |
{ |
| 5004 |
if (scase && checkVar(scase.lastVar)) |
| 5005 |
return !error; // return error once fully deprecated |
| 5006 |
} |
| 5007 |
return !error; |
| 5008 |
} |
| 5009 |
|
| 5010 |
|
| 5011 |
/************** |
| 5012 |
* Check for skipped variable declarations. |
| 5013 |
* Params: |
| 5014 |
* gs = statement to check |
| 5015 |
* Returns: true for error |
| 5016 |
*/ |
| 5017 |
bool checkLabel(GotoStatement gs) |
| 5018 |
{ |
| 5019 |
if (!gs.label.statement) |
| 5020 |
return true; // error should have been issued for this already |
| 5021 |
|
| 5022 |
if (gs.label.statement.os != gs.os) |
| 5023 |
{ |
| 5024 |
if (gs.os && gs.os.tok == TOK.onScopeFailure && !gs.label.statement.os) |
| 5025 |
{ |
| 5026 |
// Jump out from scope(failure) block is allowed. |
| 5027 |
} |
| 5028 |
else |
| 5029 |
{ |
| 5030 |
if (gs.label.statement.os) |
| 5031 |
error(gs.loc, "cannot `goto` in to `%s` block", Token.toChars(gs.label.statement.os.tok)); |
| 5032 |
else |
| 5033 |
error(gs.loc, "cannot `goto` out of `%s` block", Token.toChars(gs.os.tok)); |
| 5034 |
return true; |
| 5035 |
} |
| 5036 |
} |
| 5037 |
|
| 5038 |
if (gs.label.statement.tf != gs.tf) |
| 5039 |
{ |
| 5040 |
error(gs.loc, "cannot `goto` in or out of `finally` block"); |
| 5041 |
return true; |
| 5042 |
} |
| 5043 |
|
| 5044 |
if (gs.label.statement.inCtfeBlock && !gs.inCtfeBlock) |
| 5045 |
{ |
| 5046 |
error(gs.loc, "cannot `goto` into `if (__ctfe)` block"); |
| 5047 |
return true; |
| 5048 |
} |
| 5049 |
|
| 5050 |
Statement stbnext; |
| 5051 |
for (auto stb = gs.tryBody; stb != gs.label.statement.tryBody; stb = stbnext) |
| 5052 |
{ |
| 5053 |
if (!stb) |
| 5054 |
{ |
| 5055 |
error(gs.loc, "cannot `goto` into `try` block"); |
| 5056 |
return true; |
| 5057 |
} |
| 5058 |
if (auto stf = stb.isTryFinallyStatement()) |
| 5059 |
stbnext = stf.tryBody; |
| 5060 |
else if (auto stc = stb.isTryCatchStatement()) |
| 5061 |
stbnext = stc.tryBody; |
| 5062 |
else |
| 5063 |
assert(0); |
| 5064 |
} |
| 5065 |
|
| 5066 |
VarDeclaration vd = gs.label.statement.lastVar; |
| 5067 |
if (!vd || vd.isDataseg() || (vd.storage_class & STC.manifest)) |
| 5068 |
return false; |
| 5069 |
|
| 5070 |
VarDeclaration last = gs.lastVar; |
| 5071 |
while (last && last != vd) |
| 5072 |
last = last.lastVar; |
| 5073 |
if (last == vd) |
| 5074 |
{ |
| 5075 |
// All good, the label's scope has no variables |
| 5076 |
} |
| 5077 |
else if (vd.storage_class & STC.exptemp) |
| 5078 |
{ |
| 5079 |
// Lifetime ends at end of expression, so no issue with skipping the statement |
| 5080 |
} |
| 5081 |
else |
| 5082 |
{ |
| 5083 |
if (vd.ident == Id.withSym) |
| 5084 |
error(gs.loc, "`goto` skips declaration of `with` temporary"); |
| 5085 |
else |
| 5086 |
error(gs.loc, "`goto` skips declaration of variable `%s`", vd.toPrettyChars()); |
| 5087 |
errorSupplemental(vd.loc, "declared here"); |
| 5088 |
return true; |
| 5089 |
} |
| 5090 |
return false; |
| 5091 |
} |