Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use System.Exception | |
| 3 | ||
| 4 | use IO.Std | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Source | |
| 8 | ||
| 9 | use IR.Values | |
| 10 | use IR.VALUE_CONVERTER | |
| 11 | use IR.VALUE_BOXER | |
| 12 | ||
| 13 | use Semantic.LEAST_UPPER_BOUND_MAP | |
| 14 | use Semantic.Types.Type | |
| 15 | use Semantic.ARM_NULL_JOIN | |
| 16 | use Semantic.ArmNullJoinDecision | |
| 17 | ||
| 18 | use Syntax.Trees.Definitions.PRAGMA | |
| 19 | ||
| 20 | use Ghul.Pipes | |
| 21 | ||
| 22 | ||
| 23 | // Control-flow walks: yield, throw, break, continue, assertions, try, catch, do, if, case, | |
| 24 | // statement lists and expression bodies. | |
| 25 | partial COMPILE_EXPRESSIONS is | |
| 26 | visit(`throw: Trees.Statements.THROW) is | |
| 27 | super.visit(`throw) | |
| 28 | ||
| 29 | // Control does not fall through a throw. | |
| 30 | _flow.set_unreachable() | |
| 31 | ||
| 32 | if !`throw.expression? then | |
| 33 | return | |
| 34 | fi | |
| 35 | ||
| 36 | if | |
| 37 | !Value.check_is_consumable(_logger, `throw.expression.location, `throw.expression.value) | |
| 38 | then | |
| 39 | return | |
| 40 | fi | |
| 41 | ||
| 42 | let exception_type = _innate_symbol_lookup.get_exception_type() | |
| 43 | ||
| 44 | if !exception_type.is_assignable_from(`throw.expression!.value!.type!) then | |
| 45 | _logger.warn(`throw.expression!.location, "non-exception-throw", "thrown value is not derived from Ghul.Exception") | |
| 46 | fi | |
| 47 | ||
| 48 | // FIXME: need to signal to any enclosing expression if statement that this is a throw so | |
| 49 | // if all branches are throws, an error can be reported | |
| 50 | si | |
| 51 | ||
| 52 | pre(`yield: Trees.Statements.YIELD) -> bool is | |
| 53 | super.pre(`yield) | |
| 54 | ||
| 55 | // The yielded expression's type must match the element | |
| 56 | // type T (drawn from the enclosing function's | |
| 57 | // `Iterable[T]` / `Iterator[T]` return type). Setting | |
| 58 | // the constraint here — before walking the expression — | |
| 59 | // lets the expression participate in inference / overload | |
| 60 | // resolution against the expected type, like RETURN's | |
| 61 | // value does in compile_bindings.pre_return. | |
| 62 | let function = _symbol_table.current_function | |
| 63 | ||
| 64 | assert function? else "yield outside a function" | |
| 65 | ||
| 66 | let state_machine = Semantic.Symbols.state_machine_for(function) | |
| 67 | ||
| 68 | if state_machine? /\ function.return_type? then | |
| 69 | let element_type = _yield_element_type_for(function) | |
| 70 | ||
| 71 | if element_type? /\ !element_type.is_inferred then | |
| 72 | `yield.expression.set_expected_type( | |
| 73 | element_type, | |
| 74 | "yielded value of type {{0}} is not assignable to element type {{1}}" | |
| 75 | ) | |
| 76 | fi | |
| 77 | fi | |
| 78 | ||
| 79 | return false | |
| 80 | si | |
| 81 | ||
| 82 | visit(`yield: Trees.Statements.YIELD) is | |
| 83 | super.visit(`yield) | |
| 84 | ||
| 85 | let function = _symbol_table.current_function | |
| 86 | let state_machine = Semantic.Symbols.state_machine_for(function) | |
| 87 | ||
| 88 | if !state_machine? then | |
| 89 | if function? /\ function.is_closure then | |
| 90 | _logger.error( | |
| 91 | `yield.location, | |
| 92 | "cannot yield in function literal" | |
| 93 | ) | |
| 94 | else | |
| 95 | _logger.error( | |
| 96 | `yield.location, | |
| 97 | "generator must return Iterable[T], Iterator[T] or Pipe[T]" | |
| 98 | ) | |
| 99 | fi | |
| 100 | ||
| 101 | return | |
| 102 | fi | |
| 103 | ||
| 104 | assert function? else "state_machine_for returned non-null for null function" | |
| 105 | ||
| 106 | if !Value.check_is_consumable(_logger, `yield.expression.location, `yield.expression.value) then | |
| 107 | return | |
| 108 | fi | |
| 109 | ||
| 110 | if let yielded = `yield.expression.value?.type then | |
| 111 | _settle_generator_literal_return(function, yielded) | |
| 112 | fi | |
| 113 | ||
| 114 | // Verify the function's return type is actually an | |
| 115 | // Iterable[T] / Iterator[T]. The yield-presence test in | |
| 116 | // declare-symbols routes here without inspecting the | |
| 117 | // return type, so a body with `yield` and a non-iterable | |
| 118 | // return type lands here as a diagnostic. | |
| 119 | let element_type = _yield_element_type_for(function) | |
| 120 | ||
| 121 | if !element_type? then | |
| 122 | // A literal still waiting on a yield to settle its | |
| 123 | // element type is reported once its walk ends. | |
| 124 | if _is_unsettled_generator_literal(function) then | |
| 125 | return | |
| 126 | fi | |
| 127 | ||
| 128 | _logger.error( | |
| 129 | `yield.location, | |
| 130 | "generator must return Iterable[T], Iterator[T] or Pipe[T]" | |
| 131 | ) | |
| 132 | ||
| 133 | return | |
| 134 | fi | |
| 135 | ||
| 136 | // The expected type set in `pre` steers inference towards the | |
| 137 | // element type; it does not answer for a value that has a type | |
| 138 | // already. Without this the yielded value is written into the | |
| 139 | // element slot whatever it is, so a long yielded as a bigint | |
| 140 | // reads back as whatever those bytes are. `yield in E` makes | |
| 141 | // the same check against E's element type. | |
| 142 | let value = `yield.expression.value | |
| 143 | ||
| 144 | if !value? \/ !value.type? \/ value.type.is_error then | |
| 145 | return | |
| 146 | fi | |
| 147 | ||
| 148 | let yielded_type = value.type! | |
| 149 | ||
| 150 | if !element_type.is_assignable_from(yielded_type) then | |
| 151 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(yielded_type, element_type) | |
| 152 | _logger.error( | |
| 153 | `yield.expression.location, | |
| 154 | "yielded value of type {yielded_type} is not assignable to element type {element_type}" | |
| 155 | ) | |
| 156 | fi | |
| 157 | si | |
| 158 | ||
| 159 | // `yield in E` - the elements of E are yielded one at a time, | |
| 160 | // so E is resolved exactly as a `for` loop's expression is and | |
| 161 | // it is E's element type, not E itself, that has to match the | |
| 162 | // generator's. | |
| 163 | visit(`yield: Trees.Statements.YIELD_ALL) is | |
| 164 | super.visit(`yield) | |
| 165 | ||
| 166 | let function = _symbol_table.current_function | |
| 167 | let state_machine = Semantic.Symbols.state_machine_for(function) | |
| 168 | ||
| 169 | if !state_machine? then | |
| 170 | if function? /\ function.is_closure then | |
| 171 | _logger.error( | |
| 172 | `yield.location, | |
| 173 | "cannot yield in function literal" | |
| 174 | ) | |
| 175 | else | |
| 176 | _logger.error( | |
| 177 | `yield.location, | |
| 178 | "generator must return Iterable[T], Iterator[T] or Pipe[T]" | |
| 179 | ) | |
| 180 | fi | |
| 181 | ||
| 182 | return | |
| 183 | fi | |
| 184 | ||
| 185 | assert function? else "state_machine_for returned non-null for null function" | |
| 186 | ||
| 187 | let expression = `yield.expression | |
| 188 | ||
| 189 | if !Value.check_is_consumable(_logger, expression.location, expression.value) then | |
| 190 | return | |
| 191 | fi | |
| 192 | ||
| 193 | record_iterable_constraint(expression) | |
| 194 | ||
| 195 | let value = expression.value | |
| 196 | ||
| 197 | if !value? \/ !value.type? \/ value.type.is_error then | |
| 198 | return | |
| 199 | fi | |
| 200 | ||
| 201 | if !set_iterator_for(`yield, value.type!, false) then | |
| 202 | return | |
| 203 | fi | |
| 204 | ||
| 205 | check_receiver_present(expression) | |
| 206 | ||
| 207 | let yielded_type = `yield.read_current!.return_type! | |
| 208 | ||
| 209 | _settle_generator_literal_return(function, yielded_type) | |
| 210 | ||
| 211 | let element_type = _yield_element_type_for(function) | |
| 212 | ||
| 213 | if !element_type? then | |
| 214 | // A literal still waiting on a yield to settle its | |
| 215 | // element type is reported once its walk ends. | |
| 216 | if _is_unsettled_generator_literal(function) then | |
| 217 | return | |
| 218 | fi | |
| 219 | ||
| 220 | _logger.error( | |
| 221 | `yield.location, | |
| 222 | "generator must return Iterable[T], Iterator[T] or Pipe[T]" | |
| 223 | ) | |
| 224 | ||
| 225 | return | |
| 226 | fi | |
| 227 | ||
| 228 | if !element_type.is_assignable_from(yielded_type) then | |
| 229 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(yielded_type, element_type) | |
| 230 | _logger.error( | |
| 231 | expression.location, | |
| 232 | "yielded value of type {yielded_type} is not assignable to element type {element_type}" | |
| 233 | ) | |
| 234 | fi | |
| 235 | si | |
| 236 | ||
| 237 | // Extract `T` from a generator's `Pipe[T]` return type, or | |
| 238 | // null if the return type is not a Pipe[T] (a generator must | |
| 239 | // return Ghul.Pipes.Pipe[T]). T is taken from the Pipe itself, | |
| 240 | // not its Iterable[T] base, so the concrete element type — not | |
| 241 | // Pipe's own type parameter — is recovered. | |
| 242 | // | |
| 243 | // When the Pipe trait is unavailable (compiling ghul-runtime | |
| 244 | // itself, where the assembly is not yet loadable) fall back to | |
| 245 | // the bare Iterable[T] / Iterator[T] forms. | |
| 246 | _yield_element_type_for(function: Semantic.Symbols.Function) -> Semantic.Types.Type? is | |
| 247 | if let return_type = function.return_type /\ return_type.is_inferred then | |
| 248 | return null | |
| 249 | fi | |
| 250 | ||
| 251 | return Semantic.Symbols.generator_element_type(function) | |
| 252 | si | |
| 253 | ||
| 254 | _is_unsettled_generator_literal(function: Semantic.Symbols.Function) -> bool => | |
| 255 | function.is_closure /\ (function.return_type?.is_inferred ?? false) | |
| 256 | ||
| 257 | // A generator literal whose return is left to be inferred takes | |
| 258 | // it from its first yield: Pipe[T] over the type that yield | |
| 259 | // produces. The yields after it are checked against that T. | |
| 260 | _settle_generator_literal_return(function: Semantic.Symbols.Function, yielded_type: Semantic.Types.Type) is | |
| 261 | if !function.is_closure \/ !yielded_type.is_settled \/ yielded_type.is_error \/ isa Semantic.Types.NULL(yielded_type) then | |
| 262 | return | |
| 263 | fi | |
| 264 | ||
| 265 | if let return_type = function.return_type /\ return_type.is_inferred then | |
| 266 | if let pipe_type = Semantic.Symbols.generator_return_type(yielded_type) then | |
| 267 | function.set_return_type(pipe_type) | |
| 268 | fi | |
| 269 | fi | |
| 270 | si | |
| 271 | ||
| 272 | // `continue` keeps its label-only grammar; resolution goes | |
| 273 | // through the same loop-label symbols as `break`, and the | |
| 274 | // resolved target rides on the node. An unresolved label is an | |
| 275 | // error here rather than at codegen: every diagnostic must be | |
| 276 | // visible to analysis mode, which never runs generate-il. | |
| 277 | pre(`continue: Trees.Statements.CONTINUE) -> bool is | |
| 278 | super.pre(`continue) | |
| 279 | ||
| 280 | if let `continue.label? then | |
| 281 | let symbol = try_find(`continue.label!) | |
| 282 | ||
| 283 | if let label: Semantic.Symbols.LABEL = symbol then | |
| 284 | let frame = _loops.find_labelled_loop_frame(label.name) | |
| 285 | ||
| 286 | if frame? then | |
| 287 | `continue.resolved_target = frame.node | |
| 288 | _symbol_use_locations.add_symbol_use(`continue.label!.location, label) | |
| 289 | else | |
| 290 | let name = label.name | |
| 291 | ||
| 292 | _logger.error(`continue.location, "no enclosing loop named {name}") | |
| 293 | fi | |
| 294 | else | |
| 295 | let name = `continue.label!.name | |
| 296 | ||
| 297 | _logger.error(`continue.location, "no enclosing loop labelled {name}") | |
| 298 | fi | |
| 299 | fi | |
| 300 | ||
| 301 | return false | |
| 302 | si | |
| 303 | ||
| 304 | visit(`continue: Trees.Statements.CONTINUE) is | |
| 305 | super.visit(`continue) | |
| 306 | ||
| 307 | // Control does not fall through a continue. | |
| 308 | _flow.set_unreachable() | |
| 309 | si | |
| 310 | ||
| 311 | _check_assertion_condition(condition: Trees.Expressions.Expression) -> bool is | |
| 312 | let value = condition.value | |
| 313 | ||
| 314 | if | |
| 315 | !value? \/ | |
| 316 | !value.type? \/ | |
| 317 | !value.check_is_consumable(_logger, condition.location) | |
| 318 | then | |
| 319 | return false | |
| 320 | fi | |
| 321 | ||
| 322 | if | |
| 323 | !_innate_symbol_lookup | |
| 324 | .get_bool_type() | |
| 325 | .is_assignable_from(value.type!) | |
| 326 | then | |
| 327 | _logger.error(condition.location, "assertion expression must be bool") | |
| 328 | fi | |
| 329 | ||
| 330 | return true | |
| 331 | si | |
| 332 | ||
| 333 | _check_assertion_message(message: Trees.Expressions.Expression?) is | |
| 334 | if !message? then | |
| 335 | return | |
| 336 | fi | |
| 337 | ||
| 338 | let value = message.value | |
| 339 | ||
| 340 | if !value? \/ !value.type? then | |
| 341 | return | |
| 342 | fi | |
| 343 | ||
| 344 | value.check_is_consumable(_logger, message.location) | |
| 345 | ||
| 346 | if | |
| 347 | !_innate_symbol_lookup | |
| 348 | .get_exception_type() | |
| 349 | .is_assignable_from(value.type!) /\ | |
| 350 | !_innate_symbol_lookup | |
| 351 | .get_string_type() | |
| 352 | .is_assignable_from(value.type!) | |
| 353 | then | |
| 354 | _logger.error(message.location, "assertion else must be string or Ghul.Exception") | |
| 355 | fi | |
| 356 | si | |
| 357 | ||
| 358 | pre(`assert: Trees.Statements.ASSERT) -> bool is | |
| 359 | super.pre(`assert) | |
| 360 | ||
| 361 | // Controlled walk so the epoch span covers the condition | |
| 362 | // alone: a kill during its walk means its derived heap | |
| 363 | // facts cannot survive (`assert _f? /\ mutate();`), but | |
| 364 | // the message only runs on the failure path, so its kills | |
| 365 | // don't invalidate what the passing condition proves. | |
| 366 | let epoch = _flow.heap_epoch | |
| 367 | let mark = _flow.crossing_mark | |
| 368 | ||
| 369 | _access.assert_condition_depth = _access.assert_condition_depth + 1 | |
| 370 | ||
| 371 | `assert.expression.walk(self) | |
| 372 | ||
| 373 | _access.assert_condition_depth = _access.assert_condition_depth - 1 | |
| 374 | ||
| 375 | _assert_condition_killed_stack.add(_flow.heap_killed_since(epoch)) | |
| 376 | _assert_condition_marks_stack.add((from_mark = mark, to_mark = _flow.crossing_mark)) | |
| 377 | ||
| 378 | if `assert.message? then | |
| 379 | `assert.message.walk(self) | |
| 380 | fi | |
| 381 | ||
| 382 | return true | |
| 383 | si | |
| 384 | ||
| 385 | visit(`assert: Trees.Statements.ASSERT) is | |
| 386 | super.visit(`assert) | |
| 387 | ||
| 388 | let condition_killed = _assert_condition_killed_stack[_assert_condition_killed_stack.count - 1] | |
| 389 | _assert_condition_killed_stack.remove_at(_assert_condition_killed_stack.count - 1) | |
| 390 | ||
| 391 | let condition_marks = _assert_condition_marks_stack[_assert_condition_marks_stack.count - 1] | |
| 392 | _assert_condition_marks_stack.remove_at(_assert_condition_marks_stack.count - 1) | |
| 393 | ||
| 394 | // `assert false` always throws — control does not | |
| 395 | // continue past it. | |
| 396 | if | |
| 397 | isa Trees.Expressions.Literals.BOOLEAN(`assert.expression) /\ | |
| 398 | (cast Trees.Expressions.Literals.BOOLEAN(`assert.expression)).value_string =~ "false" | |
| 399 | then | |
| 400 | _flow.set_unreachable() | |
| 401 | fi | |
| 402 | ||
| 403 | if !_check_assertion_condition(`assert.expression) then | |
| 404 | return | |
| 405 | fi | |
| 406 | ||
| 407 | // Apply the assert's narrowing to the fall-through: | |
| 408 | // since a failed assert throws, only the then-branch of | |
| 409 | // the condition reaches subsequent code. So `assert x?` | |
| 410 | // narrows x to non-optional and `assert isa T(x)` to T | |
| 411 | // in the rest of the enclosing scope, the same way | |
| 412 | // `if !cond then throw ... fi` would. | |
| 413 | let facts = _condition_analyzer.analyze_condition(`assert.expression, _flow.current_env) | |
| 414 | ||
| 415 | if condition_killed then | |
| 416 | facts.then_env.drop_heap_facts() | |
| 417 | else | |
| 418 | _flow.adopt_crossings_between(facts.then_env, condition_marks.from_mark, condition_marks.to_mark) | |
| 419 | fi | |
| 420 | ||
| 421 | _flow.set_env(facts.then_env) | |
| 422 | ||
| 423 | _check_assertion_message(`assert.message) | |
| 424 | si | |
| 425 | ||
| 426 | pre(`try: Trees.Statements.TRY) -> bool is | |
| 427 | super.pre(`try) | |
| 428 | return _loops.pre_try(`try) | |
| 429 | si | |
| 430 | ||
| 431 | visit(`try: Trees.Statements.TRY) is | |
| 432 | super.visit(`try) | |
| 433 | _loops.visit_try(`try) | |
| 434 | si | |
| 435 | ||
| 436 | pre(`catch: Trees.Statements.CATCH) -> bool is | |
| 437 | super.pre(`catch) | |
| 438 | return _loops.pre_catch(`catch) | |
| 439 | si | |
| 440 | ||
| 441 | visit(`catch: Trees.Statements.CATCH) is | |
| 442 | super.visit(`catch) | |
| 443 | _loops.visit_catch(`catch) | |
| 444 | si | |
| 445 | ||
| 446 | pre(`do: Trees.Statements.DO) -> bool is | |
| 447 | super.pre(`do) | |
| 448 | ||
| 449 | _loops.push_loop_value_frame( | |
| 450 | `do, | |
| 451 | take_pending_label(), | |
| 452 | `do.want_value, | |
| 453 | `do.expected_type, | |
| 454 | `do.expected_type_error_message | |
| 455 | ) | |
| 456 | ||
| 457 | return _loops.pre_do(`do) | |
| 458 | si | |
| 459 | ||
| 460 | visit(`do: Trees.Statements.DO) is | |
| 461 | if | |
| 462 | let `do?.condition? /\ | |
| 463 | Value.check_is_consumable(_logger, condition.location, condition.value) | |
| 464 | then | |
| 465 | if !condition.value!.type!.matches(_innate_symbol_lookup.get_bool_type()) then | |
| 466 | _logger.error(condition.location, "while condition must be bool") | |
| 467 | fi | |
| 468 | fi | |
| 469 | ||
| 470 | super.visit(`do) | |
| 471 | ||
| 472 | _loops.visit_do(`do) | |
| 473 | ||
| 474 | _loops.pop_and_settle_loop_value(`do) | |
| 475 | si | |
| 476 | ||
| 477 | // The label of a LABELLED wrapper, consumed by the wrapped | |
| 478 | // loop's pre. Empty whenever the last LABELLED did not wrap a | |
| 479 | // loop (the parser does not produce one). | |
| 480 | take_pending_label() -> string? is | |
| 481 | if _pending_label_names.count == 0 then | |
| 482 | return null | |
| 483 | fi | |
| 484 | ||
| 485 | let label = _pending_label_names[_pending_label_names.count - 1] | |
| 486 | _pending_label_names.remove_at(_pending_label_names.count - 1) | |
| 487 | ||
| 488 | return label.name | |
| 489 | si | |
| 490 | ||
| 491 | pre(labelled: Trees.Statements.LABELLED) -> bool is | |
| 492 | super.pre(labelled) | |
| 493 | ||
| 494 | _pending_label_names.add(labelled.label) | |
| 495 | ||
| 496 | return false | |
| 497 | si | |
| 498 | ||
| 499 | pre(`break: Trees.Statements.BREAK) -> bool is | |
| 500 | super.pre(`break) | |
| 501 | ||
| 502 | let expression = `break.expression | |
| 503 | ||
| 504 | if !expression? then | |
| 505 | return false | |
| 506 | fi | |
| 507 | ||
| 508 | // A bare qualified-less identifier may name a loop label; | |
| 509 | // ordinary scope lookup decides. A hit targets that loop | |
| 510 | // and is not compiled as a value; a miss falls through to | |
| 511 | // the value reading. | |
| 512 | if let identifier: Trees.Expressions.IDENTIFIER = expression then | |
| 513 | if !identifier.identifier.qualifier? then | |
| 514 | let symbol = try_find(identifier.identifier) | |
| 515 | ||
| 516 | if let label: Semantic.Symbols.LABEL = symbol then | |
| 517 | let frame = _loops.find_labelled_loop_frame(label.name) | |
| 518 | ||
| 519 | if !frame? then | |
| 520 | _logger.error(identifier.location, "no enclosing loop named {label.name}") | |
| 521 | ||
| 522 | return true | |
| 523 | fi | |
| 524 | ||
| 525 | `break.resolved_target = frame.node | |
| 526 | _symbol_use_locations.add_symbol_use(identifier.right_location, label) | |
| 527 | ||
| 528 | return true | |
| 529 | fi | |
| 530 | fi | |
| 531 | fi | |
| 532 | ||
| 533 | // Value break: thread the consuming loop's expected type, | |
| 534 | // unwrapped, onto the expression so it infers against T. | |
| 535 | let frame = _loops.innermost_consuming_frame() | |
| 536 | ||
| 537 | if frame? /\ frame.expected_type? then | |
| 538 | let expected mut = frame.expected_type | |
| 539 | let inner = expected.optional_inner_type | |
| 540 | ||
| 541 | if inner? then | |
| 542 | expected = inner | |
| 543 | fi | |
| 544 | ||
| 545 | expression.set_expected_type(expected, frame.expected_type_error_message) | |
| 546 | fi | |
| 547 | ||
| 548 | return false | |
| 549 | si | |
| 550 | ||
| 551 | visit(`break: Trees.Statements.BREAK) is | |
| 552 | let expression = `break.expression | |
| 553 | ||
| 554 | // A valued break targets the innermost *consuming* loop: | |
| 555 | // loops that are not expressions are exited through, since | |
| 556 | // the value needs a taker. Labelled breaks were resolved in | |
| 557 | // pre and contribute nothing. | |
| 558 | if !`break.resolved_target? then | |
| 559 | if expression? then | |
| 560 | let frame = _loops.innermost_consuming_frame() | |
| 561 | ||
| 562 | if frame? then | |
| 563 | if let v = expression.value, t = v.type then | |
| 564 | frame.note_contribution(t, expression.location) | |
| 565 | fi | |
| 566 | elif loop_value_frames.count > 0 then | |
| 567 | _logger.error(expression.location, "no enclosing loop takes this break value") | |
| 568 | else | |
| 569 | _logger.error(expression.location, "break outside of loop") | |
| 570 | fi | |
| 571 | elif loop_value_frames.count == 0 then | |
| 572 | _logger.error(`break.location, "break outside of loop") | |
| 573 | fi | |
| 574 | fi | |
| 575 | ||
| 576 | // Control does not fall through a break. | |
| 577 | _flow.set_unreachable() | |
| 578 | si | |
| 579 | ||
| 580 | pre(`if: Trees.Statements.IF_BRANCH) -> bool is | |
| 581 | super.pre(`if) | |
| 582 | return _conditionals.pre_if_branch(`if) | |
| 583 | si | |
| 584 | ||
| 585 | visit(`if: Trees.Statements.IF_BRANCH) is | |
| 586 | _conditionals.visit_if_branch(`if) | |
| 587 | super.visit(`if) | |
| 588 | si | |
| 589 | ||
| 590 | pre(expression: Trees.Bodies.EXPRESSION) -> bool is | |
| 591 | super.pre(expression) | |
| 592 | ||
| 593 | let function = current_function | |
| 594 | ||
| 595 | // A body an async state machine emits leaves its value in the | |
| 596 | // machine's result rather than returning it, so it has no end | |
| 597 | // to dispose at. | |
| 598 | if !function? \/ !Semantic.Symbols.async_state_machine_for(function)? then | |
| 599 | disposal_host = expression | |
| 600 | fi | |
| 601 | ||
| 602 | if | |
| 603 | !function? \/ | |
| 604 | !function.return_type? | |
| 605 | then | |
| 606 | return false | |
| 607 | fi | |
| 608 | ||
| 609 | if function.return_type.is_sentinel then | |
| 610 | // An inferred return type has not yet said whether the | |
| 611 | // body yields a value, so a body that only performs | |
| 612 | // void work is not an error here. Mark the position | |
| 613 | // void-tolerant; visit settles the return as void once | |
| 614 | // the body has been walked. | |
| 615 | if function.return_type.is_inferred then | |
| 616 | _mark_body_void_tolerated(expression.expression) | |
| 617 | fi | |
| 618 | ||
| 619 | return false | |
| 620 | fi | |
| 621 | ||
| 622 | // The marker a pack formal put on this position says what the | |
| 623 | // value is for, whatever type it is then expected to have. | |
| 624 | let pack_marker = expression.expression.compile_expressions_state.pack_marker | |
| 625 | let pack_slot = expression.expression.compile_expressions_state.pack_slot | |
| 626 | ||
| 627 | expression.expression.set_expected_type(function.return_type, "cannot return value of type {{0}} where {{1}} expected") | |
| 628 | ||
| 629 | if pack_marker > 0 then | |
| 630 | // A literal is compiled to the shape the return has. Anything | |
| 631 | // else is a function of the pack's elements, or returns one | |
| 632 | // that many hops down, to be presented in that shape, and | |
| 633 | // is expected to be one. | |
| 634 | if !COMPILE_CALLS.parenthesised_literal(expression.expression)? then | |
| 635 | if let spread = Semantic.ARGUMENT_PACK.spread_spine(function.return_type, pack_marker - 1) then | |
| 636 | expression.expression.set_expected_type(spread, "cannot return value of type {{0}} where {{1}} expected") | |
| 637 | fi | |
| 638 | fi | |
| 639 | ||
| 640 | expression.expression.set_expects_pack(pack_marker - 1, pack_slot) | |
| 641 | fi | |
| 642 | ||
| 643 | // A void-returning `=> body` doesn't need its body to yield | |
| 644 | // a value. Push that down into any STATEMENT- / VAL_BLOCK- | |
| 645 | // shaped body so a non-value-providing tail is accepted | |
| 646 | // silently. | |
| 647 | if function.return_type!.is_void then | |
| 648 | if let statement_expression: Trees.Expressions.STATEMENT = expression.expression then | |
| 649 | statement_expression.want_value = false | |
| 650 | elif let val_block: Trees.Expressions.VAL_BLOCK = expression.expression then | |
| 651 | val_block.want_value = false | |
| 652 | fi | |
| 653 | fi | |
| 654 | ||
| 655 | return false | |
| 656 | ||
| 657 | si | |
| 658 | ||
| 659 | _mark_body_void_tolerated(body: Trees.Expressions.Expression) is | |
| 660 | if let statement_expression: Trees.Expressions.STATEMENT = body then | |
| 661 | statement_expression.void_tolerated = true | |
| 662 | elif let val_block: Trees.Expressions.VAL_BLOCK = body then | |
| 663 | val_block.void_tolerated = true | |
| 664 | fi | |
| 665 | si | |
| 666 | ||
| 667 | visit(expression: Trees.Bodies.EXPRESSION) is | |
| 668 | let function = current_function | |
| 669 | ||
| 670 | disposal_host = null | |
| 671 | ||
| 672 | // A diverging body — `=> throw E`, or an `=> if/case` whose | |
| 673 | // every arm diverges — yields no value but is valid. Keep an | |
| 674 | // explicitly declared return type; settle an inferred one as | |
| 675 | // void since there is nothing to infer from. | |
| 676 | if | |
| 677 | function? /\ | |
| 678 | !expression.expression.value? /\ | |
| 679 | _flow.is_unreachable | |
| 680 | then | |
| 681 | super.visit(expression) | |
| 682 | ||
| 683 | if DIVERGING_VALUE_POSITION.settles_inferred_return_to_void(function.return_type) then | |
| 684 | function.set_return_type(_innate_symbol_lookup.get_void_type()) | |
| 685 | fi | |
| 686 | ||
| 687 | return | |
| 688 | fi | |
| 689 | ||
| 690 | // A body that evaluates to a function of the pack's elements, | |
| 691 | // in a position a pack formal marked as taking their tuple - | |
| 692 | // or, further from the marker, to a function that returns one | |
| 693 | // - is presented as the function of the tuple. | |
| 694 | if expression.expression.pack_depth >= 0 then | |
| 695 | if let presented = _calls.present_function_value(expression.expression, expression.expression.pack_depth) then | |
| 696 | expression.expression.compile_expressions_state.value = presented | |
| 697 | fi | |
| 698 | fi | |
| 699 | ||
| 700 | let value = expression.expression?.value | |
| 701 | ||
| 702 | if | |
| 703 | !function? \/ | |
| 704 | !function.return_type? \/ | |
| 705 | !value? \/ | |
| 706 | !value.type? | |
| 707 | then | |
| 708 | super.visit(expression) | |
| 709 | ||
| 710 | expression.expression.compile_expressions_state.value = IR.Values.DUMMY(Semantic.Types.ERROR(), expression.expression.location) | |
| 711 | ||
| 712 | if function? /\ (!function.return_type? \/ function.return_type.is_wild) then | |
| 713 | function.set_return_type(Semantic.Types.ERROR()) | |
| 714 | fi | |
| 715 | ||
| 716 | return | |
| 717 | fi | |
| 718 | ||
| 719 | let void_type = _innate_symbol_lookup.get_void_type() | |
| 720 | ||
| 721 | if function.return_type!.is_inferred then | |
| 722 | if Value.check_is_consumable_allow_void(_logger, expression.expression.location, value) then | |
| 723 | // Body contains let-await → wrap the bare-T body | |
| 724 | // expression as `Tasks.TASK.from_result(expr)` and | |
| 725 | // settle the inferred return as Task[T]. Values | |
| 726 | // already typed Task[?] are pinned as-is. | |
| 727 | // Mirrors the wrap in visit_return. | |
| 728 | let value_type: Semantic.Types.Type? mut = value.type! | |
| 729 | ||
| 730 | if | |
| 731 | function.wrap_inferred_return_as_task /\ | |
| 732 | value_type.is_settled /\ | |
| 733 | !_bindings.task_conversion.is_task_type(value_type) | |
| 734 | then | |
| 735 | if Semantic.Symbols.async_state_machine_for(function)? then | |
| 736 | // The body awaits, so the machine delivers | |
| 737 | // the value to its builder: the return is | |
| 738 | // pinned to the task carrying it rather than | |
| 739 | // the value being wrapped, exactly as the | |
| 740 | // RETURN visitor pins it. | |
| 741 | if value_type.matches(void_type) then | |
| 742 | if let void_task = _innate_symbol_lookup.get_void_task_type() then | |
| 743 | value_type = void_task | |
| 744 | function.is_void_async = true | |
| 745 | fi | |
| 746 | elif let task_type = _bindings.task_conversion.async_return_type_for(function, value_type!) then | |
| 747 | value_type = task_type | |
| 748 | fi | |
| 749 | elif function.async_task_like_template? then | |
| 750 | // A task-like other than Task has no | |
| 751 | // from_result to wrap through: the state | |
| 752 | // machine delivers the value to its builder, | |
| 753 | // and only the return type is pinned here. | |
| 754 | let task_type = _bindings.task_conversion.async_return_type_for(function, value_type!) | |
| 755 | ||
| 756 | if task_type? then | |
| 757 | value_type = task_type | |
| 758 | fi | |
| 759 | else | |
| 760 | let task_type = _innate_symbol_lookup.get_task_type(value_type) | |
| 761 | ||
| 762 | if task_type? then | |
| 763 | let wrapped = _bindings.task_conversion.try_wrap_value_as_task_expression(expression.expression, task_type, self) | |
| 764 | if wrapped? then | |
| 765 | expression.expression = wrapped | |
| 766 | value_type = expression.expression.value!.type | |
| 767 | fi | |
| 768 | fi | |
| 769 | fi | |
| 770 | fi | |
| 771 | ||
| 772 | // A null body says the return can be absent without | |
| 773 | // saying what it holds when present, so it settles | |
| 774 | // nothing: what it is optional of comes from the slot | |
| 775 | // the literal goes into, the same answer `return | |
| 776 | // null` reaches in a block body. | |
| 777 | if Semantic.ARM_NULL_JOIN().is_genuine_null(value_type!) then | |
| 778 | function.returned_genuine_null = true | |
| 779 | elif Semantic.OPERAND_WAIT.is_held_by(value_type!) then | |
| 780 | // Part of what the body produced is a rule waiting | |
| 781 | // for an operand the call site types. Pinning the | |
| 782 | // return to it now freezes that wait into the | |
| 783 | // literal's own type, where no later walk reaches | |
| 784 | // it; the return stays inferred instead. No | |
| 785 | // progress signal of its own: the walk that | |
| 786 | // settles the operand raises one. | |
| 787 | else | |
| 788 | // A value type reaches the slot's reference type by | |
| 789 | // being boxed, and the box is emitted where the | |
| 790 | // conversion is made. Settling the return at what | |
| 791 | // the body produced leaves the literal typed at the | |
| 792 | // struct with nothing but the delegate's assignment | |
| 793 | // to carry the conversion, and that needs a return | |
| 794 | // covariance a value-type instantiation does not | |
| 795 | // have. It settles at the body's own implementation | |
| 796 | // of what the slot returns instead. | |
| 797 | if | |
| 798 | let settled = | |
| 799 | Semantic.VALUE_RETURN_PIN.of( | |
| 800 | function.pin_inferred_return_at, value_type) | |
| 801 | then | |
| 802 | value_type = settled | |
| 803 | fi | |
| 804 | ||
| 805 | function.set_return_type(Semantic.MAYBE_RETURN_PIN.of(function, value_type!, _innate_symbol_lookup)) | |
| 806 | fi | |
| 807 | elif value.type? /\ value.type.is_error then | |
| 808 | function.set_return_type(Semantic.Types.ERROR()) | |
| 809 | fi | |
| 810 | elif function.return_type!.matches(void_type) /\ !function.return_type!.is_type_variable then | |
| 811 | // `=> E` is the one-expression spelling of `is E si`, | |
| 812 | // where a void body discards whatever its tail leaves | |
| 813 | // standing, so a value here is discarded rather than | |
| 814 | // returned. Generate-il pops it. | |
| 815 | Value.check_is_consumable_allow_void(_logger, expression.expression.location, value) | |
| 816 | elif let async_sm = Semantic.Symbols.async_state_machine_for(function) then | |
| 817 | // The body awaits, so the machine delivers its value to | |
| 818 | // the builder, whose result slot holds the element type. | |
| 819 | // Judging against the task, and wrapping to reach it, | |
| 820 | // would hand the builder a task where it takes a T - | |
| 821 | // the same reason the RETURN visitor judges against the | |
| 822 | // frame's element. | |
| 823 | if let element = async_sm.frame?.result_type then | |
| 824 | if !element.is_assignable_from(value.type!) then | |
| 825 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value.type, element) | |
| 826 | _logger | |
| 827 | .error( | |
| 828 | expression.location, | |
| 829 | "cannot return value of type {value.type} where {element} expected" | |
| 830 | ) | |
| 831 | fi | |
| 832 | fi | |
| 833 | elif !function.return_type!.is_assignable_from(value.type!) then | |
| 834 | // Implicit T → TASK[T] widening at expression-body return | |
| 835 | // position. Same mechanism as visit_return: synthesise | |
| 836 | // Tasks.TASK.from_result(orig) and re-resolve. | |
| 837 | let wrapped = _bindings.task_conversion.try_wrap_value_as_task_expression(expression.expression, function.return_type!, self) | |
| 838 | if wrapped? then | |
| 839 | expression.expression = wrapped | |
| 840 | else | |
| 841 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(value.type, function.return_type) | |
| 842 | _logger | |
| 843 | .error( | |
| 844 | expression.location, | |
| 845 | "cannot return value of type {value.type} where {function.return_type} expected" | |
| 846 | ) | |
| 847 | fi | |
| 848 | else | |
| 849 | Value.check_is_consumable_allow_void(_logger, expression.expression.location, value) | |
| 850 | fi | |
| 851 | ||
| 852 | check_non_optional(function.return_type, expression.expression, expression.expression.location) | |
| 853 | ||
| 854 | _pure_slots.check_store(expression.expression.location, function.return_type, expression.expression.value) | |
| 855 | ||
| 856 | super.visit(expression) | |
| 857 | si | |
| 858 | ||
| 859 | pre(statement: Trees.Expressions.STATEMENT) -> bool is | |
| 860 | super.pre(statement) | |
| 861 | ||
| 862 | statement.statement.compile_expressions_state.want_value = statement.want_value | |
| 863 | statement.statement.compile_expressions_state.void_tolerated = statement.void_tolerated | |
| 864 | ||
| 865 | return false | |
| 866 | si | |
| 867 | ||
| 868 | visit(statement: Trees.Expressions.STATEMENT) is | |
| 869 | // An `if` / `case` in a void-tolerant position that declined | |
| 870 | // to produce a value is a void body, not a missing one, so | |
| 871 | // this position is void too. Dropping want_value leaves the | |
| 872 | // statement shape IL generation already emits for a body | |
| 873 | // whose return type was written out as void. | |
| 874 | if statement.void_tolerated /\ !statement.statement.value? then | |
| 875 | statement.want_value = false | |
| 876 | fi | |
| 877 | ||
| 878 | // A diverging statement (`throw`, or an `if`/`case` whose | |
| 879 | // every arm diverges) legitimately yields no value; the | |
| 880 | // unreachable continuation needs none. | |
| 881 | if DIVERGING_VALUE_POSITION.is_missing_value_reportable(statement.want_value, statement.statement.value?, _flow.is_unreachable) then | |
| 882 | _logger.warn(statement.location, "statement-expression-no-value", "statement expression has no value") | |
| 883 | fi | |
| 884 | ||
| 885 | if statement.statement.value? then | |
| 886 | statement.compile_expressions_state.value = statement.statement.value | |
| 887 | elif !statement.want_value then | |
| 888 | // Void-tolerant position (expression-statement, void- | |
| 889 | // returning `=>` body) with an `if`/`case` whose | |
| 890 | // branches don't all provide values: synthesise a void | |
| 891 | // block value so the consumer has something to thread | |
| 892 | // through rather than a null that downstream passes | |
| 893 | // treat as an error. | |
| 894 | statement.compile_expressions_state.value = IR.Values.BLOCK( | |
| 895 | _innate_symbol_lookup.get_void_type() | |
| 896 | ) | |
| 897 | fi | |
| 898 | si | |
| 899 | ||
| 900 | pre(block: Trees.Expressions.VAL_BLOCK) -> bool is | |
| 901 | super.pre(block) | |
| 902 | ||
| 903 | // The body's tail is the only fall-through value | |
| 904 | // contributor. Push want_value down through the LIST to | |
| 905 | // its last statement so it must provide a value when this | |
| 906 | // block is consumed; the surrounding context (expression- | |
| 907 | // statement, void-returning `=>` body) may also write | |
| 908 | // false here to allow a void tail. | |
| 909 | block.body.compile_expressions_state.want_value = block.want_value | |
| 910 | block.body.compile_expressions_state.void_tolerated = block.void_tolerated | |
| 911 | ||
| 912 | // The returns targeting this block are collected afresh by | |
| 913 | // every walk of its body; entries left by an earlier walk | |
| 914 | // of the same body carry that walk's provisional types. | |
| 915 | block.return_types.clear() | |
| 916 | block.has_targeted_return = false | |
| 917 | ||
| 918 | // Push this block as the innermost return target before | |
| 919 | // walking its body. Returns inside read the top of stack | |
| 920 | // in pre_return / visit_return; nested val-blocks override | |
| 921 | // the current top while their own body is walked. | |
| 922 | _val_block_stack.add(block) | |
| 923 | ||
| 924 | return false | |
| 925 | si | |
| 926 | ||
| 927 | visit(block: Trees.Expressions.VAL_BLOCK) is | |
| 928 | super.visit(block) | |
| 929 | ||
| 930 | // Pop ourselves off the val-block stack regardless of | |
| 931 | // outcome — every push in pre must be balanced by a pop | |
| 932 | // here, otherwise an outer return-target lookup picks up | |
| 933 | // a stale inner block. | |
| 934 | assert _val_block_stack.count > 0 else "val_block_stack underflow" | |
| 935 | let top = _val_block_stack[_val_block_stack.count - 1] | |
| 936 | assert top? /\ top == block else "val_block_stack head is not the block being visited" | |
| 937 | _val_block_stack.remove_at(_val_block_stack.count - 1) | |
| 938 | ||
| 939 | // A body that yields no value in a void-tolerant position | |
| 940 | // (an inferred function-literal return) makes the block | |
| 941 | // void rather than valueless. Nothing returns out of it | |
| 942 | // either, so there is no other contributor to LUB with. | |
| 943 | if | |
| 944 | block.void_tolerated /\ | |
| 945 | !block.body.value? /\ | |
| 946 | block.return_types.count == 0 | |
| 947 | then | |
| 948 | block.want_value = false | |
| 949 | fi | |
| 950 | ||
| 951 | if !block.want_value then | |
| 952 | // Void-tolerant position: yield a void block value so | |
| 953 | // the consumer (expression-statement, void-returning | |
| 954 | // `=>` body) has something to thread through. Returns | |
| 955 | // from inside the block are still emitted as branches | |
| 956 | // by generate-il; the value here is the fall-through | |
| 957 | // type, which is void in this branch. | |
| 958 | block.compile_expressions_state.value = IR.Values.BLOCK( | |
| 959 | _innate_symbol_lookup.get_void_type() | |
| 960 | ) | |
| 961 | return | |
| 962 | fi | |
| 963 | ||
| 964 | // LUB over every value-contributing source: returns | |
| 965 | // targeting us (recorded by visit_return as we walked the | |
| 966 | // body) and the tail expression's value type (if the tail | |
| 967 | // provides one). When the tail itself diverges (every | |
| 968 | // path returns from us), block.body.value may still be | |
| 969 | // set by the LIST visitor — it conservatively types as | |
| 970 | // BLOCK(last.value.type) — but the LUB shape below | |
| 971 | // tolerates either presence. | |
| 972 | // | |
| 973 | // A settled `null` contributor joins as optionality only, | |
| 974 | // never as a pool entry: on its own it would win | |
| 975 | // best-assignable outright, and beside a named type it | |
| 976 | // has no ancestors to intersect with, so the map would | |
| 977 | // discard the named type with it. The non-null pool LUBs | |
| 978 | // first; a genuine null then widens the result to its | |
| 979 | // optional carrier — the same join if / case expression | |
| 980 | // arms use. Without the widen, a `null` tail settles this | |
| 981 | // block at the null type itself, and a state-machine | |
| 982 | // frame spill field declared from that type is unencodable. | |
| 983 | let lub = LEAST_UPPER_BOUND_MAP() | |
| 984 | let null_join = ARM_NULL_JOIN() | |
| 985 | let seen_genuine_null mut = false | |
| 986 | ||
| 987 | for t in block.return_types do | |
| 988 | if !t.is_error then | |
| 989 | if null_join.is_genuine_null(t) then | |
| 990 | seen_genuine_null = true | |
| 991 | else | |
| 992 | lub.add(t) | |
| 993 | fi | |
| 994 | fi | |
| 995 | od | |
| 996 | ||
| 997 | if let block.body.value?, value.type? /\ !type.is_error then | |
| 998 | if null_join.is_genuine_null(type) then | |
| 999 | seen_genuine_null = true | |
| 1000 | else | |
| 1001 | lub.add(type) | |
| 1002 | fi | |
| 1003 | fi | |
| 1004 | ||
| 1005 | let lub_type mut = lub.get_result() | |
| 1006 | ||
| 1007 | // Passing seen_null as seen_genuine_null keeps sentinels | |
| 1008 | // and error types (which also answer is_null) on the old | |
| 1009 | // path: an unsettled contribution must not force a widen | |
| 1010 | // or an incompatible verdict while inference converges. | |
| 1011 | let decision = null_join.decide(lub_type, seen_genuine_null, seen_genuine_null) | |
| 1012 | ||
| 1013 | if decision == ArmNullJoinDecision.WIDEN_VALUE_OPTIONAL then | |
| 1014 | lub_type = _innate_symbol_lookup.get_optional_type(lub_type!) | |
| 1015 | elif decision == ArmNullJoinDecision.WIDEN_REFERENCE_OPTIONAL then | |
| 1016 | lub_type = lub_type!.as_optional() | |
| 1017 | elif decision == ArmNullJoinDecision.INCOMPATIBLE then | |
| 1018 | // A type-variable LUB against a settled null: the same | |
| 1019 | // verdict if / case arms reach, since NULLABLE[T] is | |
| 1020 | // wrong for a type variable. Report and recover on the | |
| 1021 | // plain LUB, as those callers do. | |
| 1022 | _logger.error( | |
| 1023 | block.location, | |
| 1024 | "incompatible types in val-block: null and {lub_type}" | |
| 1025 | ) | |
| 1026 | fi | |
| 1027 | ||
| 1028 | if lub_type? then | |
| 1029 | block.compile_expressions_state.value = IR.Values.BLOCK(lub_type) | |
| 1030 | elif seen_genuine_null /\ block.expected_type? /\ block.expected_type.is_assignable_from(Semantic.Types.NULL()) then | |
| 1031 | // Every contributor was a settled null, so there is no | |
| 1032 | // LUB of our own to take; settle at the type the | |
| 1033 | // consumer's context pushed down instead. Returns were | |
| 1034 | // already checked against it by visit_return. | |
| 1035 | block.compile_expressions_state.value = IR.Values.BLOCK(block.expected_type!) | |
| 1036 | elif seen_genuine_null /\ !block.expected_type? then | |
| 1037 | // All-null with no context to take a type from: the | |
| 1038 | // same verdict the if-expression form reaches, rather | |
| 1039 | // than settling the block at the null type. | |
| 1040 | _logger.error(block.location, "all val-block contributions are null") | |
| 1041 | ||
| 1042 | block.compile_expressions_state.value = IR.Values.DUMMY_BLOCK( | |
| 1043 | Semantic.Types.ERROR(), | |
| 1044 | block.location, | |
| 1045 | "all val-block contributions are null" | |
| 1046 | ) | |
| 1047 | elif block.body.value? then | |
| 1048 | // No usable LUB contributions but the body produced a | |
| 1049 | // value of some sentinel / error type — pass it | |
| 1050 | // through so downstream phases see a real Value and | |
| 1051 | // diagnose at the original location. | |
| 1052 | block.compile_expressions_state.value = block.body.value | |
| 1053 | else | |
| 1054 | // Body's tail doesn't provide a value and there are | |
| 1055 | // no returns to LUB with. The body walk has already | |
| 1056 | // emitted "expected a value" at the offending | |
| 1057 | // statement (compile_expressions.visit(Statements | |
| 1058 | // .LIST) — the val-block contract is the same as | |
| 1059 | // the LIST contract there); add the parallel "no | |
| 1060 | // value" warning the if/case-in-expression form | |
| 1061 | // emits in the same situation, and stand up a DUMMY | |
| 1062 | // value so downstream walks have a typed Value. | |
| 1063 | _logger.warn( | |
| 1064 | block.location, | |
| 1065 | "statement-expression-no-value", | |
| 1066 | "statement expression has no value" | |
| 1067 | ) | |
| 1068 | ||
| 1069 | block.compile_expressions_state.value = IR.Values.DUMMY_BLOCK( | |
| 1070 | Semantic.Types.ERROR(), | |
| 1071 | block.location, | |
| 1072 | "val block produced no value" | |
| 1073 | ) | |
| 1074 | fi | |
| 1075 | ||
| 1076 | _declare_composite_spill_field(block, block.value) | |
| 1077 | si | |
| 1078 | ||
| 1079 | pre(list: Trees.Statements.LIST) -> bool is | |
| 1080 | super.pre(list) | |
| 1081 | return true | |
| 1082 | si | |
| 1083 | ||
| 1084 | // Wrap a body tail's bare-T value as the task the return | |
| 1085 | // calls for, the way the RETURN visitor wraps a `return T`. | |
| 1086 | // Answers the tail's type after the wrap, and its type | |
| 1087 | // unchanged where no wrap applies. Both tail-wrap callers - | |
| 1088 | // a declared task return and an inferred one about to be | |
| 1089 | // settled from the tail - ask the same question of the same | |
| 1090 | // tail, and differ only in how they decide to ask it. | |
| 1091 | _wrap_tail_as_task( | |
| 1092 | tail: Trees.Statements.Statement, | |
| 1093 | tail_type: Semantic.Types.Type? | |
| 1094 | ) -> Semantic.Types.Type? is | |
| 1095 | if | |
| 1096 | !tail_type? \/ | |
| 1097 | !tail_type.is_settled \/ | |
| 1098 | _bindings.task_conversion.is_task_type(tail_type) \/ | |
| 1099 | !isa Trees.Statements.EXPRESSION(tail) | |
| 1100 | then | |
| 1101 | return tail_type | |
| 1102 | fi | |
| 1103 | ||
| 1104 | let se = cast Trees.Statements.EXPRESSION(tail) | |
| 1105 | let task_type = _innate_symbol_lookup.get_task_type(tail_type) | |
| 1106 | ||
| 1107 | if !task_type? then | |
| 1108 | return tail_type | |
| 1109 | fi | |
| 1110 | ||
| 1111 | let wrapped = | |
| 1112 | _bindings.task_conversion.try_wrap_value_as_task_expression( | |
| 1113 | se.expression, | |
| 1114 | task_type, | |
| 1115 | self | |
| 1116 | ) | |
| 1117 | ||
| 1118 | if !wrapped? then | |
| 1119 | return tail_type | |
| 1120 | fi | |
| 1121 | ||
| 1122 | se.expression = wrapped | |
| 1123 | tail.compile_expressions_state.value = wrapped.value! | |
| 1124 | ||
| 1125 | return wrapped.value!.type | |
| 1126 | si | |
| 1127 | ||
| 1128 | visit(list: Trees.Statements.LIST) is | |
| 1129 | let enclosing_statement_list = current_statement_list | |
| 1130 | let enclosing_disposal_list = disposal_list | |
| 1131 | current_statement_list = list | |
| 1132 | ||
| 1133 | disposal_list = list | |
| 1134 | ||
| 1135 | try | |
| 1136 | if let list.last? then | |
| 1137 | last.compile_expressions_state.want_value = list.want_value | |
| 1138 | last.compile_expressions_state.void_tolerated = list.void_tolerated | |
| 1139 | fi | |
| 1140 | ||
| 1141 | let enclosing_disposal_host = disposal_host | |
| 1142 | let block = innermost_val_block | |
| 1143 | let hosts_disposal = !block? \/ block.body != list | |
| 1144 | ||
| 1145 | for s in list.statements do | |
| 1146 | self.enter_node(s) | |
| 1147 | ||
| 1148 | if hosts_disposal then | |
| 1149 | disposal_host = s | |
| 1150 | fi | |
| 1151 | ||
| 1152 | try | |
| 1153 | s.walk(self) | |
| 1154 | finally | |
| 1155 | self.leave_node(s) | |
| 1156 | disposal_host = enclosing_disposal_host | |
| 1157 | yrt | |
| 1158 | od | |
| 1159 | ||
| 1160 | // Implicit tail return: this list is a function body | |
| 1161 | // under a tail demand, and the tail is judged by its | |
| 1162 | // type while scope and flow are live - the async wrap | |
| 1163 | // replaces the tail expression and re-walks it in | |
| 1164 | // place, keeping resolution inside the function scope. | |
| 1165 | if list.function_tail /\ !_flow.is_unreachable then | |
| 1166 | if let fs = current_function, rt = fs.return_type, tail = list.last, v = tail.value then | |
| 1167 | // A void tail is effect-only: not an error, not a | |
| 1168 | // return. The demand goes undelivered, the body | |
| 1169 | // falls through to the default-value return, and | |
| 1170 | // the definite-return warning judges that path. | |
| 1171 | // The null literal's type matches every type, void | |
| 1172 | // included, but a null tail is a value. | |
| 1173 | let tail_is_void = | |
| 1174 | v.type? /\ | |
| 1175 | !isa Semantic.Types.NULL(v.type) /\ | |
| 1176 | v.type.matches(_innate_symbol_lookup.get_void_type()) | |
| 1177 | ||
| 1178 | if !tail_is_void /\ v.check_is_consumable(_logger, tail.location) then | |
| 1179 | let tt: Semantic.Types.Type? mut = v.type | |
| 1180 | ||
| 1181 | // An async function's tail wraps a bare-T value as | |
| 1182 | // TASK[T], exactly as an explicit `return T` does - | |
| 1183 | // except where a state machine consumes the return. | |
| 1184 | // Its result slot holds the element type (SetResult | |
| 1185 | // takes T), so like the RETURN visitor those | |
| 1186 | // functions never see the wrap and the tail is | |
| 1187 | // judged against the element. Await-free asyncs | |
| 1188 | // lower without a machine and keep the wrap. | |
| 1189 | let sm = Semantic.Symbols.async_state_machine_for(fs) | |
| 1190 | ||
| 1191 | let judge: Semantic.Types.Type? mut = rt | |
| 1192 | ||
| 1193 | if let frm = sm?.frame, et = frm.result_type then | |
| 1194 | judge = et | |
| 1195 | fi | |
| 1196 | ||
| 1197 | // A function literal that left its return | |
| 1198 | // type to be inferred, and said nothing | |
| 1199 | // about it in a `return`, is settled by its | |
| 1200 | // tail: nothing else in the body names the | |
| 1201 | // type, and the tail is the value the | |
| 1202 | // literal hands back. An async literal | |
| 1203 | // wraps a bare-T tail first, as the RETURN | |
| 1204 | // visitor does before pinning. | |
| 1205 | if rt.is_inferred /\ sm? then | |
| 1206 | // The literal awaits, so its return is | |
| 1207 | // pinned rather than wrapped: the machine | |
| 1208 | // delivers the value to its builder, whose | |
| 1209 | // result slot holds the element. Parity | |
| 1210 | // with the RETURN visitor's state-machine | |
| 1211 | // branch, which pins the same way. | |
| 1212 | if let settled = tt then | |
| 1213 | if | |
| 1214 | fs.wrap_inferred_return_as_task /\ | |
| 1215 | settled.is_settled /\ | |
| 1216 | !settled.is_sentinel /\ | |
| 1217 | !_bindings.task_conversion.is_task_type(settled) | |
| 1218 | then | |
| 1219 | if let task_type = _bindings.task_conversion.async_return_type_for(fs, settled) then | |
| 1220 | fs.set_return_type(task_type) | |
| 1221 | ||
| 1222 | if let element = sm.frame?.result_type then | |
| 1223 | judge = element | |
| 1224 | fi | |
| 1225 | fi | |
| 1226 | fi | |
| 1227 | fi | |
| 1228 | elif rt.is_inferred /\ !sm? then | |
| 1229 | if fs.wrap_inferred_return_as_task then | |
| 1230 | tt = _wrap_tail_as_task(tail, tt) | |
| 1231 | fi | |
| 1232 | ||
| 1233 | if let settled = tt then | |
| 1234 | if settled.is_settled /\ !settled.is_sentinel then | |
| 1235 | fs.set_return_type(Semantic.MAYBE_RETURN_PIN.of(fs, settled, _innate_symbol_lookup)) | |
| 1236 | ||
| 1237 | judge = fs.return_type | |
| 1238 | fi | |
| 1239 | fi | |
| 1240 | fi | |
| 1241 | ||
| 1242 | if | |
| 1243 | !sm? /\ | |
| 1244 | !rt.is_sentinel /\ | |
| 1245 | _bindings.task_conversion.is_task_type(rt) | |
| 1246 | then | |
| 1247 | tt = _wrap_tail_as_task(tail, tt) | |
| 1248 | fi | |
| 1249 | ||
| 1250 | // A judge still standing at a placeholder | |
| 1251 | // settles nothing: an inferred return whose | |
| 1252 | // tail reads the literal's own not-yet-known | |
| 1253 | // type has to wait for a later walk, and | |
| 1254 | // delivering against the placeholder would | |
| 1255 | // take the tail as a return value the | |
| 1256 | // literal never had. | |
| 1257 | if let t2 = tt, jd = judge /\ !jd.is_sentinel then | |
| 1258 | list.compile_expressions_state.function_tail_delivered = true | |
| 1259 | ||
| 1260 | // A tail still holding placeholders learns them | |
| 1261 | // from the return type, as a `return` does. | |
| 1262 | if t2.contains_inferred then | |
| 1263 | _overload_resolver.match_propagator.propagate_match(jd, t2) | |
| 1264 | fi | |
| 1265 | ||
| 1266 | if !t2.is_error /\ !jd.is_assignable_from(t2) then | |
| 1267 | let use qualified = IoC.CONTAINER.instance.name_display.qualifying_alike(t2, jd) | |
| 1268 | _logger.error( | |
| 1269 | tail.location, | |
| 1270 | string.format( | |
| 1271 | "cannot return value of type {{0}} where {{1}} expected", | |
| 1272 | t2, | |
| 1273 | jd | |
| 1274 | ) | |
| 1275 | ) | |
| 1276 | elif sm? then | |
| 1277 | // Parity with the RETURN visitor's | |
| 1278 | // state-machine branch: the same two | |
| 1279 | // value-quality checks run on the value | |
| 1280 | // the tail delivers. | |
| 1281 | if let se = cast Trees.Statements.EXPRESSION?(tail) then | |
| 1282 | check_non_optional(jd, se.expression, tail.location) | |
| 1283 | fi | |
| 1284 | ||
| 1285 | _pure_slots.check_store(tail.location, jd, v) | |
| 1286 | fi | |
| 1287 | fi | |
| 1288 | fi | |
| 1289 | fi | |
| 1290 | fi | |
| 1291 | ||
| 1292 | if | |
| 1293 | list.function_tail /\ | |
| 1294 | !list.compile_expressions_state.function_tail_delivered | |
| 1295 | then | |
| 1296 | // Nothing to deliver: a void or valueless tail under a | |
| 1297 | // function-body demand falls through rather than | |
| 1298 | // returning, so no list value reaches IL generation. | |
| 1299 | // The tail goes back to being an ordinary statement - | |
| 1300 | // with want_value standing, IL generation would | |
| 1301 | // suppress its emission for a consumer that no longer | |
| 1302 | // exists, losing its side effects. | |
| 1303 | if let tail = list.last then | |
| 1304 | tail.compile_expressions_state.want_value = false | |
| 1305 | fi | |
| 1306 | ||
| 1307 | // An awaiting literal whose return was left to its | |
| 1308 | // tail, and whose tail turned out to deliver nothing, | |
| 1309 | // is void-async after all: settle it as the | |
| 1310 | // result-less task, which is what the pre-walk | |
| 1311 | // classification declines to do while a value tail is | |
| 1312 | // still possible. | |
| 1313 | if let fs = current_function, rt = fs.return_type then | |
| 1314 | if | |
| 1315 | rt.is_inferred /\ | |
| 1316 | fs.wrap_inferred_return_as_task /\ | |
| 1317 | Semantic.Symbols.async_state_machine_for(fs)? | |
| 1318 | then | |
| 1319 | if let void_task = _innate_symbol_lookup.get_void_task_type() then | |
| 1320 | fs.set_return_type(void_task) | |
| 1321 | fs.is_void_async = true | |
| 1322 | fi | |
| 1323 | fi | |
| 1324 | fi | |
| 1325 | ||
| 1326 | // A machine-less Tasks.TASK return completes at | |
| 1327 | // fall-through rather than returning the default null | |
| 1328 | // task: append the completing return after the tail, | |
| 1329 | // resolved here while the function's scope is live. | |
| 1330 | // The appended return joins the list, so a re-walk | |
| 1331 | // walks it before this visit runs again and leaves | |
| 1332 | // flow unreachable - the !is_unreachable guard above | |
| 1333 | // is what prevents a second append, and dropping it | |
| 1334 | // would grow one completing return per re-walk. A | |
| 1335 | // machine-less return of some other result-less | |
| 1336 | // task-like completes the same way, but its value | |
| 1337 | // comes from driving its builder, which IL | |
| 1338 | // generation emits at the fall-through - no append. | |
| 1339 | if | |
| 1340 | let fs = current_function, | |
| 1341 | rt = fs.return_type | |
| 1342 | then | |
| 1343 | if | |
| 1344 | !_flow.is_unreachable /\ | |
| 1345 | !rt.is_sentinel /\ | |
| 1346 | _bindings.task_conversion.is_void_task_return(rt) /\ | |
| 1347 | !Semantic.Symbols.async_state_machine_for(fs)? /\ | |
| 1348 | !Semantic.Symbols.state_machine_for(fs)? | |
| 1349 | then | |
| 1350 | let loc = if let tail = list.last then tail.location else list.location fi | |
| 1351 | ||
| 1352 | let completed = | |
| 1353 | Trees.Statements.RETURN( | |
| 1354 | loc, | |
| 1355 | Semantic.TASK_CONVERSION.build_completed_task_expression(loc)) | |
| 1356 | ||
| 1357 | list.add(completed) | |
| 1358 | ||
| 1359 | completed.expression!.walk(self) | |
| 1360 | fi | |
| 1361 | fi | |
| 1362 | ||
| 1363 | return | |
| 1364 | fi | |
| 1365 | ||
| 1366 | if !list.want_value then | |
| 1367 | return | |
| 1368 | fi | |
| 1369 | ||
| 1370 | if list.is_empty then | |
| 1371 | _logger.error(list.location, "expected a value") | |
| 1372 | return | |
| 1373 | fi | |
| 1374 | ||
| 1375 | let last = list.last | |
| 1376 | ||
| 1377 | if !last? then | |
| 1378 | return | |
| 1379 | fi | |
| 1380 | ||
| 1381 | if !last.provides_value then | |
| 1382 | // Tolerate a non-value-providing tail when every | |
| 1383 | // path through the body has already diverged | |
| 1384 | // (returned / thrown). Fall-through is dead | |
| 1385 | // code; the consumer needs the IL emitted for | |
| 1386 | // its side effects but no value need be left | |
| 1387 | // on the stack. A void-tolerant list - a function | |
| 1388 | // body under a tail demand, or an arm of the if | |
| 1389 | // that tail is - reads a valueless tail (a loop, | |
| 1390 | // say) as a void one: it falls through, and | |
| 1391 | // definite-return judges the fall-through, the | |
| 1392 | // same as a body whose tail is that loop directly. | |
| 1393 | if !_flow.is_unreachable /\ !list.void_tolerated then | |
| 1394 | _logger.error(last.location, "expected a value") | |
| 1395 | fi | |
| 1396 | return | |
| 1397 | fi | |
| 1398 | ||
| 1399 | if let last.value? then | |
| 1400 | // A tail whose type is still an inference | |
| 1401 | // placeholder - a local read before its own | |
| 1402 | // initializer has completed, say - carries no type | |
| 1403 | // to build the block from. The list takes the error | |
| 1404 | // type instead, so a consumer that joins it against | |
| 1405 | // a sibling reports the mismatch rather than | |
| 1406 | // dereferencing a type that is not there. | |
| 1407 | list.compile_expressions_state.value = | |
| 1408 | IR.Values.BLOCK(_list_block_type(list, value.type)) | |
| 1409 | ||
| 1410 | // A list that disposes needs its body inside the | |
| 1411 | // protected region, which the spill path does not | |
| 1412 | // build - so it always captures. | |
| 1413 | // | |
| 1414 | // The direct body of a val-block never generates | |
| 1415 | // through visit(Statements.LIST) — generate-il's | |
| 1416 | // pre(VAL_BLOCK) walks the statements itself — so a | |
| 1417 | // spill field recorded here would be a member | |
| 1418 | // nothing reads. Nested lists (an if arm inside the | |
| 1419 | // block, say) keep theirs: those do generate here. | |
| 1420 | let is_direct_body mut = false | |
| 1421 | ||
| 1422 | if _val_block_stack.count > 0 then | |
| 1423 | let top = _val_block_stack[_val_block_stack.count - 1] | |
| 1424 | ||
| 1425 | if top? /\ top.body == list then | |
| 1426 | is_direct_body = true | |
| 1427 | fi | |
| 1428 | fi | |
| 1429 | ||
| 1430 | if !list.want_dispose /\ !is_direct_body then | |
| 1431 | _declare_composite_spill_field(list, list.value) | |
| 1432 | fi | |
| 1433 | fi | |
| 1434 | finally | |
| 1435 | current_statement_list = enclosing_statement_list | |
| 1436 | disposal_list = enclosing_disposal_list | |
| 1437 | yrt | |
| 1438 | si | |
| 1439 | ||
| 1440 | pre(`if: Trees.Statements.IF) -> bool is | |
| 1441 | super.pre(`if) | |
| 1442 | return _conditionals.pre_if(`if) | |
| 1443 | si | |
| 1444 | ||
| 1445 | visit(`if: Trees.Statements.IF) is | |
| 1446 | _conditionals.visit_if(`if) | |
| 1447 | ||
| 1448 | _declare_composite_spill_field(`if, `if.value) | |
| 1449 | si | |
| 1450 | ||
| 1451 | pre(`case: Trees.Statements.CASE) -> bool is | |
| 1452 | super.pre(`case) | |
| 1453 | return _conditionals.pre_case(`case) | |
| 1454 | si | |
| 1455 | ||
| 1456 | visit(`case: Trees.Statements.CASE) is | |
| 1457 | _conditionals.visit_case(`case) | |
| 1458 | ||
| 1459 | _declare_composite_spill_field(`case, `case.value) | |
| 1460 | ||
| 1461 | super.visit(`case) | |
| 1462 | si | |
| 1463 | ||
| 1464 | pre(arm: Trees.Statements.CASE_MATCH) -> bool is | |
| 1465 | super.pre(arm) | |
| 1466 | return _conditionals.pre_case_match(arm) | |
| 1467 | si | |
| 1468 | ||
| 1469 | visit(arm: Trees.Statements.CASE_MATCH) is | |
| 1470 | _conditionals.visit_case_match(arm) | |
| 1471 | super.visit(arm) | |
| 1472 | si | |
| 1473 | ||
| 1474 | // Do not descend into properties (otherwise accessor functions will be walked twice) | |
| 1475 | pre(property: Trees.Definitions.PROPERTY) -> bool => true | |
| 1476 | ||
| 1477 | visit(property: Trees.Definitions.PROPERTY) is | |
| 1478 | si | |
| 1479 | ||
| 1480 | // A function body whose tail is `null` delivers the empty optional | |
| 1481 | // of the function's return type, so its block takes that type; the | |
| 1482 | // null type describes no value a caller could be handed. | |
| 1483 | _list_block_type(list: Trees.Statements.LIST, tail_type: Semantic.Types.Type?) -> Semantic.Types.Type is | |
| 1484 | if | |
| 1485 | list.function_tail /\ | |
| 1486 | list.compile_expressions_state.function_tail_delivered /\ | |
| 1487 | tail_type? /\ | |
| 1488 | tail_type.is_null | |
| 1489 | then | |
| 1490 | if let rt = current_function?.return_type /\ !rt.is_sentinel /\ !rt.is_inferred then | |
| 1491 | return rt | |
| 1492 | fi | |
| 1493 | fi | |
| 1494 | ||
| 1495 | return tail_type ?? Semantic.Types.ERROR() | |
| 1496 | si | |
| 1497 | si | |
| 1498 | si |