Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use System.Text.StringBuilder | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Trees | |
| 8 | use Source | |
| 9 | ||
| 10 | use IR | |
| 11 | use IR.Values | |
| 12 | ||
| 13 | use Ghul.Pipes | |
| 14 | ||
| 15 | // Holds a generator function whose state-machine class hasn't | |
| 16 | // been emitted yet — its MoveNext block was filled in while | |
| 17 | // walking the function's body, but the wrapping `.class` lives | |
| 18 | // outside the enclosing user class in IL. Flushed by | |
| 19 | // `emit_pending_state_machines` at the tail of the enclosing | |
| 20 | // class / namespace visit. | |
| 21 | class PENDING_STATE_MACHINE is | |
| 22 | state_machine: Semantic.Symbols.STATE_MACHINE | |
| 23 | move_next_block: IR.Values.BLOCK | |
| 24 | ||
| 25 | init(state_machine: Semantic.Symbols.STATE_MACHINE, move_next_block: IR.Values.BLOCK) is | |
| 26 | self.state_machine = state_machine | |
| 27 | self.move_next_block = move_next_block | |
| 28 | si | |
| 29 | si | |
| 30 | ||
| 31 | // Async sibling of PENDING_STATE_MACHINE. Flushed by | |
| 32 | // `emit_pending_state_machines` alongside the generator list. | |
| 33 | class PENDING_ASYNC_STATE_MACHINE is | |
| 34 | async_state_machine: Semantic.Symbols.ASYNC_STATE_MACHINE | |
| 35 | move_next_block: IR.Values.BLOCK | |
| 36 | move_next: ASYNC_MOVE_NEXT | |
| 37 | ||
| 38 | init( | |
| 39 | async_state_machine: Semantic.Symbols.ASYNC_STATE_MACHINE, | |
| 40 | move_next_block: IR.Values.BLOCK, | |
| 41 | move_next: ASYNC_MOVE_NEXT | |
| 42 | ) is | |
| 43 | self.async_state_machine = async_state_machine | |
| 44 | self.move_next_block = move_next_block | |
| 45 | self.move_next = move_next | |
| 46 | si | |
| 47 | si | |
| 48 | ||
| 49 | // The structure an async MoveNext body is generated into: a | |
| 50 | // resumable body, which a suspend or a failure leaves for the end of | |
| 51 | // the method, holding the region a completed body leaves for the | |
| 52 | // success path, holding the try whose handler routes an escaping | |
| 53 | // exception to the builder. | |
| 54 | class ASYNC_MOVE_NEXT( | |
| 55 | resumable: IR.Values.RESUMABLE, | |
| 56 | success: IR.Values.REGION, | |
| 57 | `try: IR.Values.TRY | |
| 58 | ) | |
| 59 | ||
| 60 | // One entry in a dispatch placeholder — the pair an | |
| 61 | // AWAIT_SUSPEND contributes when it registers with its | |
| 62 | // enclosing protected region. `state` is the suspension state | |
| 63 | // number; `cold_resume_label` is the IR.LABEL inside the same | |
| 64 | // region's `.try` body that GET_RESULT routes through. | |
| 65 | class ASYNC_DISPATCH_ENTRY is | |
| 66 | state: int | |
| 67 | cold_resume_label: IR.LABEL | |
| 68 | ||
| 69 | init(state: int, cold_resume_label: IR.LABEL) is | |
| 70 | self.state = state | |
| 71 | self.cold_resume_label = cold_resume_label | |
| 72 | si | |
| 73 | si | |
| 74 | ||
| 75 | // IL-emission frame for a `val ... lav` block currently in | |
| 76 | // scope. Returns inside the body whose val_block_target is this | |
| 77 | // block push their value and `br` to `end_label`; the natural | |
| 78 | // fall-through emits the tail value and falls into the same | |
| 79 | // label. All paths converge at end_label with exactly one value | |
| 80 | // on the evaluation stack (or empty for a void block). No value | |
| 81 | // ever lives in a CLR local or frame field at a suspension | |
| 82 | // point, so async-await and generator-yield compose without | |
| 83 | // bespoke handling. | |
| 84 | // | |
| 85 | // result_type is the resolved block value type from compile- | |
| 86 | // expressions; void means no value-push. is_void is cached to | |
| 87 | // skip the lookup at every return emission. enclosing_try_count | |
| 88 | // is the number of `.try` entries on the loop-label stack at | |
| 89 | // val-block entry — a return whose current try count exceeds | |
| 90 | // this opened a try INSIDE the val-block body and needs `leave` | |
| 91 | // instead of `br` to exit the protected region. value_block is | |
| 92 | // the captured IR BLOCK used in capture mode; pre(VAL_BLOCK) | |
| 93 | // eagerly allocates the cross-try TEMP on it before walking | |
| 94 | // the body, so the `.locals init` lands at the head of value_ | |
| 95 | // block's IL stream rather than inside an inner `.try {}`. | |
| 96 | class VAL_BLOCK_IL is | |
| 97 | block: Trees.Expressions.VAL_BLOCK | |
| 98 | end_label: IR.LABEL | |
| 99 | result_type: Semantic.Types.Type? | |
| 100 | is_void: bool | |
| 101 | enclosing_try_count: int | |
| 102 | value_block: IR.Values.BLOCK? | |
| 103 | ||
| 104 | // Non-null when generate-il is spilling this val-block to a | |
| 105 | // frame field (body contains a state-machine suspend). Val- | |
| 106 | // targeted returns then `stfld $spill_field` instead of | |
| 107 | // leaving the value on the stack, so all paths converge at | |
| 108 | // end_label with the value in the field and an empty stack. | |
| 109 | spill_field: Semantic.Symbols.Field? public | |
| 110 | ||
| 111 | // Capture-mode cross-try state. The TEMP and label must be | |
| 112 | // allocated BEFORE the body walk, so the declaration is | |
| 113 | // encoded ahead of the load at the post-body join site | |
| 114 | // rather than somewhere inside the body. VAL_BLOCK.pre | |
| 115 | // pre-allocates both eagerly when value_block has a | |
| 116 | // non-void result type and is not spilling. | |
| 117 | // | |
| 118 | // A val-targeted `return E` inside an inner `.try` can't | |
| 119 | // `br end_label` (invalid IL across a protected region) | |
| 120 | // and can't carry its value across `leave` (empty-stack | |
| 121 | // precondition). Mirrors the function-return-from-try | |
| 122 | // pattern: stash the value into `cross_try_temp`, leave | |
| 123 | // `cross_try_join_region`, and at the join `ldloc` the temp | |
| 124 | // back onto the stack so it falls through into end_label. | |
| 125 | // `cross_try_used` flags whether the join block needs to | |
| 126 | // be emitted at all — otherwise the unconditional `br | |
| 127 | // end_label; join: ldloc; end_label` would push | |
| 128 | // uninitialised data onto the stack on the natural path's | |
| 129 | // divergent variants. | |
| 130 | cross_try_temp: IR.TEMP? public | |
| 131 | cross_try_join_region: Values.REGION? public | |
| 132 | cross_try_used: bool public | |
| 133 | ||
| 134 | // The region the val-block's body is generated into; a | |
| 135 | // val-targeted return leaves it. | |
| 136 | region: Values.REGION? public | |
| 137 | ||
| 138 | init( | |
| 139 | block: Trees.Expressions.VAL_BLOCK, | |
| 140 | end_label: IR.LABEL, | |
| 141 | result_type: Semantic.Types.Type?, | |
| 142 | is_void: bool, | |
| 143 | enclosing_try_count: int, | |
| 144 | value_block: IR.Values.BLOCK? | |
| 145 | ) is | |
| 146 | self.block = block | |
| 147 | self.end_label = end_label | |
| 148 | self.result_type = result_type | |
| 149 | self.is_void = is_void | |
| 150 | self.enclosing_try_count = enclosing_try_count | |
| 151 | self.value_block = value_block | |
| 152 | si | |
| 153 | si | |
| 154 | ||
| 155 | // Helper for the four composites whose AST value sits in an | |
| 156 | // `IR.Values.BLOCK` and whose body may suspend (`val ... lav`, | |
| 157 | // a `Statements.LIST` in expression position, `if` / `case` in | |
| 158 | // expression position). The natural emission captures body IL | |
| 159 | // inside the value BLOCK; a consumer later replays it. When the | |
| 160 | // captured IL contains a state-machine suspend, the replay traps | |
| 161 | // the suspend's `leave` / state register inside the consumer's | |
| 162 | // stack setup — invalid IL plus state-loss across MoveNext | |
| 163 | // re-entry. | |
| 164 | // | |
| 165 | // Spill mode breaks the capture: the body emits inline in outer | |
| 166 | // current_block alongside the dispatcher, each value-producing | |
| 167 | // path lands in an anonymous frame field via `ldarg.0; v; stfld`, | |
| 168 | // and the value BLOCK is rewritten to hold just a `ldfld` of the | |
| 169 | // field. Consumers see a clean load. | |
| 170 | // | |
| 171 | // One spiller per composite. Which mode it is in follows from | |
| 172 | // whether compile-expressions recorded a frame field for the | |
| 173 | // node, which is where the spill-vs-capture decision is made. | |
| 174 | // `enter` | |
| 175 | // opens the BLOCK for direct emission when capturing; `emit | |
| 176 | // _value` emits each tail/branch value (spill or forward); | |
| 177 | // `leave` either rewrites the BLOCK's contents to a field load | |
| 178 | // (spill) or closes it with `leave_block` (capture). VAL_BLOCK | |
| 179 | // also reads `spill_field` to make val-targeted returns store | |
| 180 | // into the same field. | |
| 181 | class COMPOSITE_VALUE_SPILLER is | |
| 182 | _gen: GENERATE_IL | |
| 183 | _value_block: IR.Values.BLOCK? | |
| 184 | _frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE? | |
| 185 | _spill_field: Semantic.Symbols.Field? | |
| 186 | ||
| 187 | init( | |
| 188 | gen: GENERATE_IL, | |
| 189 | node: Trees.Node, | |
| 190 | value: IR.Values.Value?, | |
| 191 | frame: Semantic.Symbols.STATE_MACHINE_FRAME_BASE? | |
| 192 | ) is | |
| 193 | _gen = gen | |
| 194 | ||
| 195 | if !value? \/ !isa IR.Values.BLOCK(value) then | |
| 196 | return | |
| 197 | fi | |
| 198 | ||
| 199 | _value_block = cast IR.Values.BLOCK(value) | |
| 200 | ||
| 201 | if !frame? then | |
| 202 | return | |
| 203 | fi | |
| 204 | ||
| 205 | // Compile-expressions makes the spill-vs-capture decision | |
| 206 | // and allocates the field, because a frame field first | |
| 207 | // declared here would be numbered by nothing. An entry | |
| 208 | // means spill; none means capture. | |
| 209 | let spill_field = gen.composite_spill_state.get(node) | |
| 210 | ||
| 211 | if !spill_field? then | |
| 212 | return | |
| 213 | fi | |
| 214 | ||
| 215 | _frame = frame | |
| 216 | _spill_field = spill_field | |
| 217 | si | |
| 218 | ||
| 219 | is_spilling: bool => _spill_field? | |
| 220 | ||
| 221 | spill_field: Semantic.Symbols.Field? => _spill_field | |
| 222 | ||
| 223 | enter() is | |
| 224 | if !_value_block? \/ _spill_field? then | |
| 225 | return | |
| 226 | fi | |
| 227 | _gen.enter_block(_value_block) | |
| 228 | si | |
| 229 | ||
| 230 | emit_value(value: IR.Values.Value) is | |
| 231 | let spill = _spill_field | |
| 232 | ||
| 233 | if spill? then | |
| 234 | _gen.add(_gen._build_frame_field_store(_frame!, spill, value)) | |
| 235 | else | |
| 236 | _gen.add(value) | |
| 237 | fi | |
| 238 | si | |
| 239 | ||
| 240 | leave() is | |
| 241 | if _spill_field? then | |
| 242 | _value_block!.add(_gen._build_frame_field_load(_frame!, _spill_field)) | |
| 243 | elif _value_block? then | |
| 244 | _gen.leave_block() | |
| 245 | fi | |
| 246 | si | |
| 247 | si | |
| 248 | ||
| 249 | // Placeholder block sitting at the top of one protected region's | |
| 250 | // body (MoveNext's outer try, or any inner let-use / user `.try`). | |
| 251 | // While the region's body walks, AWAIT_SUSPENDs / YIELDs that | |
| 252 | // fire inside it append their state-label pair via `register`. | |
| 253 | // Region close populates `block` with `ldloc V_state; ldc.i4 N; | |
| 254 | // beq cold_N` for each entry — keeping the dispatch branches | |
| 255 | // inside the same `.try` as the targets they reach — and hands | |
| 256 | // those states to the enclosing holder, retargeted to | |
| 257 | // `top_label`: from outside a protected region, control may only | |
| 258 | // enter at its first instruction, so the enclosing dispatch | |
| 259 | // routes a resuming MoveNext straight past any statements between | |
| 260 | // the regions (hoisted-variable initializers, loop iterators) | |
| 261 | // that must not re-run on re-entry. | |
| 262 | // | |
| 263 | // `state_local` is the local the state is cached in at MoveNext | |
| 264 | // entry, for async state machines and generators alike. Both nest | |
| 265 | // under the same | |
| 266 | // dispatch-holder machinery; only the local name differs. | |
| 267 | class ASYNC_DISPATCH_HOLDER is | |
| 268 | dispatch: IR.Values.DISPATCH | |
| 269 | state_local: IR.LOCAL | |
| 270 | top_label: IR.LABEL => dispatch.top | |
| 271 | is_state_machine_entry: bool | |
| 272 | _entries: Collections.LIST[ASYNC_DISPATCH_ENTRY] | |
| 273 | ||
| 274 | init( | |
| 275 | dispatch: IR.Values.DISPATCH, | |
| 276 | is_state_machine_entry: bool | |
| 277 | ) is | |
| 278 | self.dispatch = dispatch | |
| 279 | self.state_local = dispatch.state_local | |
| 280 | self.is_state_machine_entry = is_state_machine_entry | |
| 281 | _entries = Collections.LIST[ASYNC_DISPATCH_ENTRY]() | |
| 282 | si | |
| 283 | ||
| 284 | register(state: int, cold_resume_label: IR.LABEL) is | |
| 285 | _entries.add(ASYNC_DISPATCH_ENTRY(state, cold_resume_label)) | |
| 286 | si | |
| 287 | ||
| 288 | entries: Collections.Iterable[ASYNC_DISPATCH_ENTRY] => _entries | |
| 289 | si | |
| 290 | ||
| 291 | class GENERATE_IL: ScopedVisitor is | |
| 292 | _logger: Logger | |
| 293 | _symbol_table: Semantic.SYMBOL_TABLE | |
| 294 | _symbol_loader: Semantic.SYMBOL_LOADER | |
| 295 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 296 | _function_caller: Semantic.FUNCTION_CALLER | |
| 297 | _type_caster: Semantic.TYPE_CASTER | |
| 298 | _overload_resolver: Semantic.OVERLOAD_RESOLVER | |
| 299 | _symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS | |
| 300 | _build_flags: Compiler.GLOBAL_BUILD_FLAGS | |
| 301 | ||
| 302 | // Read by COMPOSITE_VALUE_SPILLER: an entry is compile- | |
| 303 | // expressions telling this composite to spill. | |
| 304 | composite_spill_state: COMPOSITE_SPILL_STORE | |
| 305 | ||
| 306 | // Read for a literal leaf's value-equality test; see | |
| 307 | // VARIABLE_LEFT_STATE. | |
| 308 | _variable_left_state: VARIABLE_LEFT_STATE_STORE | |
| 309 | ||
| 310 | _backend: IR.Backend | |
| 311 | _boxer: VALUE_BOXER | |
| 312 | _loops: LOOP_LABEL_STACK | |
| 313 | _block_context: BlockContext | |
| 314 | ||
| 315 | // Asked which return types complete without a state machine; | |
| 316 | // depends only on the innate lookup, so it is built here rather | |
| 317 | // than threaded through the constructor. | |
| 318 | _task_conversion: Semantic.TASK_CONVERSION | |
| 319 | ||
| 320 | // The `.try` block each catch clause currently being walked is | |
| 321 | // attached to. A catch is visited on its own, after the block | |
| 322 | // it guards has closed, so it has no other route to the extent | |
| 323 | // its region needs. | |
| 324 | // The structured trys whose catch handlers are being generated. | |
| 325 | _catching_trys: Collections.STACK[Values.TRY] | |
| 326 | ||
| 327 | _il_output_depth: int | |
| 328 | ||
| 329 | // Set by an `@entry` pragma, and cleared by the next function the | |
| 330 | // walk enters - the one the pragma annotates. | |
| 331 | ||
| 332 | _in_catch_variable: bool | |
| 333 | ||
| 334 | _indent: int | |
| 335 | _depth: int | |
| 336 | _run_on: bool | |
| 337 | _indent_needed: bool | |
| 338 | ||
| 339 | _are_runtime_symbols_materialized: bool | |
| 340 | _interpolated_value_classifier: Semantic.INTERPOLATED_VALUE_CLASSIFIER | |
| 341 | _display_function: Semantic.Symbols.Function? | |
| 342 | _dispose: Semantic.Symbols.Function? | |
| 343 | _is_dispose_resolved: bool | |
| 344 | ||
| 345 | // Generators whose state-machine class IL emission has been | |
| 346 | // deferred until the enclosing class/namespace closes. The | |
| 347 | // outer function method is emitted inline; the state-machine | |
| 348 | // class needs to land as a SIBLING of the user's class, not | |
| 349 | // inside it (CIL has no nested-class-in-method form). Mirrors | |
| 350 | // the existing closure-FRAME emission pattern (see | |
| 351 | // gen_closures called from visit(`class)). | |
| 352 | _pending_state_machines: Collections.LIST[PENDING_STATE_MACHINE] | |
| 353 | _pending_async_state_machines: Collections.LIST[PENDING_ASYNC_STATE_MACHINE] | |
| 354 | ||
| 355 | // Per-async-function emission context, set/cleared by | |
| 356 | // `_pre_async_function`. Visit handlers for AWAIT / RETURN | |
| 357 | // check `current_function == _current_async_state_machine | |
| 358 | // .function` so a nested closure's own visit doesn't fire | |
| 359 | // the outer function's async paths. | |
| 360 | _current_async_state_machine: Semantic.Symbols.ASYNC_STATE_MACHINE? | |
| 361 | _current_async_success: Values.REGION? | |
| 362 | _current_async_resumable: Values.RESUMABLE? | |
| 363 | ||
| 364 | // Leave target for yields inside a `.try` body. CIL forbids | |
| 365 | // `ret` from inside a protected region; yields that suspend | |
| 366 | // from within an enclosing `.try` leave this node, after which, | |
| 367 | // outside any try, MoveNext returns true. The generator trailer | |
| 368 | // emits that return only if a yield left the node. | |
| 369 | _current_generator_resumable: Values.RESUMABLE? | |
| 370 | ||
| 371 | // Stack of dispatch placeholders for the currently-open | |
| 372 | // protected regions (outermost is the MoveNext body itself). | |
| 373 | // Pushed when entering a `.try` body, popped on close. Each | |
| 374 | // AWAIT_SUSPEND that fires while a holder is active registers | |
| 375 | // its (state, cold_resume_label) pair with the top holder. | |
| 376 | // At close-of-region the holder's BLOCK is populated with | |
| 377 | // `ldloc V_state; ldc.i4 N; beq cold_resume_N` for each | |
| 378 | // registered await — so the `beq` and the target label live | |
| 379 | // in the same `.try`, never crossing a region boundary — and | |
| 380 | // the states are propagated to the enclosing holder against | |
| 381 | // this region's entry, so the MoveNext-entry dispatch reaches | |
| 382 | // resume points nested in `.try`s without re-running the code | |
| 383 | // between the regions. | |
| 384 | _async_dispatch_stack: Collections.LIST[ASYNC_DISPATCH_HOLDER] | |
| 385 | ||
| 386 | // The disposal region each expression body being walked opened, | |
| 387 | // innermost last; null for a body with nothing to dispose. | |
| 388 | _expression_body_regions: Collections.LIST[DISPOSAL_REGION?] | |
| 389 | ||
| 390 | // Stack of `val ... lav` IL frames, parallel to the | |
| 391 | // val_block_target stamped on RETURN nodes by compile- | |
| 392 | // expressions. Each entry holds the block's end_label, the | |
| 393 | // resolved result_type, the open-`.try` count cached at | |
| 394 | // val-block entry, and (in capture mode) the eagerly- | |
| 395 | // allocated cross-try TEMP and join label. visit(RETURN) | |
| 396 | // reads the top via the node's val_block_target identity | |
| 397 | // and dispatches to one of three shapes: spill mode stfld's | |
| 398 | // into the frame field then leave/br end_label; capture | |
| 399 | // mode with no inner try pushes the value and `br`s | |
| 400 | // end_label; capture mode crossing an inner try stashes the | |
| 401 | // value into the TEMP and `leave`s the join label, where | |
| 402 | // a post-body `ldloc` joins it back onto end_label's stack. | |
| 403 | _val_block_il_stack: Collections.LIST[VAL_BLOCK_IL] | |
| 404 | ||
| 405 | current_block: Values.BLOCK => _block_context.current_block | |
| 406 | ||
| 407 | // The interpolation-handler symbols are materialized by | |
| 408 | // ensure_runtime_symbols_are_materialized, and the disposal one by | |
| 409 | // dispose_function, since the runtime types are not loaded yet at | |
| 410 | // construction. | |
| 411 | @suppress("field-definite-assignment") | |
| 412 | init( | |
| 413 | logger: Logger, | |
| 414 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 415 | namespaces: Semantic.NAMESPACES, | |
| 416 | symbol_loader: Semantic.SYMBOL_LOADER, | |
| 417 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup, | |
| 418 | function_caller: Semantic.FUNCTION_CALLER, | |
| 419 | type_caster: Semantic.TYPE_CASTER, | |
| 420 | overload_resolver: Semantic.OVERLOAD_RESOLVER, | |
| 421 | symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS, | |
| 422 | backend: IR.Backend, | |
| 423 | block_context: BlockContext, | |
| 424 | boxer: VALUE_BOXER, | |
| 425 | build_flags: Compiler.GLOBAL_BUILD_FLAGS, | |
| 426 | composite_spill_state: COMPOSITE_SPILL_STORE, | |
| 427 | variable_left_state: VARIABLE_LEFT_STATE_STORE | |
| 428 | ) | |
| 429 | is | |
| 430 | super.init(logger, symbol_table, namespaces) | |
| 431 | ||
| 432 | _logger = logger | |
| 433 | _symbol_table = symbol_table | |
| 434 | _symbol_loader = symbol_loader | |
| 435 | _innate_symbol_lookup = innate_symbol_lookup | |
| 436 | _task_conversion = Semantic.TASK_CONVERSION(innate_symbol_lookup) | |
| 437 | _function_caller = function_caller | |
| 438 | _type_caster = type_caster | |
| 439 | _overload_resolver = overload_resolver | |
| 440 | _symbol_use_locations = symbol_use_locations | |
| 441 | _backend = backend | |
| 442 | _block_context = block_context | |
| 443 | _boxer = boxer | |
| 444 | _build_flags = build_flags | |
| 445 | self.composite_spill_state = composite_spill_state | |
| 446 | _variable_left_state = variable_left_state | |
| 447 | ||
| 448 | _loops = LOOP_LABEL_STACK() | |
| 449 | _catching_trys = Collections.STACK[Values.TRY]() | |
| 450 | _pending_state_machines = Collections.LIST[PENDING_STATE_MACHINE]() | |
| 451 | _pending_async_state_machines = Collections.LIST[PENDING_ASYNC_STATE_MACHINE]() | |
| 452 | _async_dispatch_stack = Collections.LIST[ASYNC_DISPATCH_HOLDER]() | |
| 453 | _expression_body_regions = Collections.LIST[DISPOSAL_REGION?]() | |
| 454 | _val_block_il_stack = Collections.LIST[VAL_BLOCK_IL]() | |
| 455 | si | |
| 456 | ||
| 457 | // The `dispose` a disposal calls, or null where the core library | |
| 458 | // declares no disposable trait. Asked for only by a disposal, so a | |
| 459 | // program that disposes nothing does not need the trait. Where it | |
| 460 | // is missing, looking it up has reported so. | |
| 461 | dispose_function() -> Semantic.Symbols.Function? is | |
| 462 | if !_is_dispose_resolved then | |
| 463 | _is_dispose_resolved = true | |
| 464 | ||
| 465 | let idisposable_type = _innate_symbol_lookup.get_idisposable_type() | |
| 466 | ||
| 467 | // there is only one overload of dispose: | |
| 468 | if let group = cast Semantic.Symbols.FUNCTION_GROUP?(idisposable_type.find_member("dispose")) then | |
| 469 | _dispose = group.functions[0] | |
| 470 | fi | |
| 471 | fi | |
| 472 | ||
| 473 | return _dispose | |
| 474 | si | |
| 475 | ||
| 476 | ensure_runtime_symbols_are_materialized() is | |
| 477 | if _are_runtime_symbols_materialized then | |
| 478 | return | |
| 479 | fi | |
| 480 | ||
| 481 | _are_runtime_symbols_materialized = true | |
| 482 | ||
| 483 | _interpolated_value_classifier = Semantic.INTERPOLATED_VALUE_CLASSIFIER(_innate_symbol_lookup) | |
| 484 | _display_function = _interpolated_value_classifier.display_function | |
| 485 | si | |
| 486 | ||
| 487 | apply(root: Trees.Node) is | |
| 488 | if _logger.any_errors then | |
| 489 | return | |
| 490 | fi | |
| 491 | ||
| 492 | LABEL.set_pass("_") | |
| 493 | ||
| 494 | root.walk(self) | |
| 495 | si | |
| 496 | ||
| 497 | // Mirrors compile_expressions.ghul's enter_node/leave_node. | |
| 498 | // pre(Statements.LIST) takes over the statement walk, so the | |
| 499 | // location bracketing has to be re-applied by hand around | |
| 500 | // each statement. | |
| 501 | enter_node(node: Trees.Node) is | |
| 502 | if !_build_flags.want_source_locations then | |
| 503 | return | |
| 504 | fi | |
| 505 | IoC.CONTAINER.instance.location_stack.push(node.debug_location) | |
| 506 | si | |
| 507 | ||
| 508 | leave_node(node: Trees.Node) is | |
| 509 | if !_build_flags.want_source_locations then | |
| 510 | return | |
| 511 | fi | |
| 512 | IoC.CONTAINER.instance.location_stack.pop() | |
| 513 | si | |
| 514 | ||
| 515 | enter_block(block: Values.BLOCK) is | |
| 516 | _block_context.enter_block(block) | |
| 517 | si | |
| 518 | ||
| 519 | enter_block() is | |
| 520 | _block_context.enter_block() | |
| 521 | si | |
| 522 | ||
| 523 | leave_block() is | |
| 524 | _block_context.leave_block() | |
| 525 | si | |
| 526 | ||
| 527 | add(value: Values.Value) is | |
| 528 | current_block.add(value) | |
| 529 | si | |
| 530 | si | |
| 531 | ||
| 532 | ||
| 533 | // A loop has no `middle`: only the try shape branches through one, | |
| 534 | // and only the try constructor makes it. | |
| 535 | @suppress("field-definite-assignment") | |
| 536 | class LOOP_LABELS is | |
| 537 | is_try: bool | |
| 538 | is_in_finally: bool public | |
| 539 | is_loop: bool => !is_try | |
| 540 | ||
| 541 | name: string? | |
| 542 | start: LABEL | |
| 543 | middle: LABEL | |
| 544 | end: LABEL | |
| 545 | ||
| 546 | // The structured loop a loop entry's body is generated into. | |
| 547 | structured: Values.LOOP? public | |
| 548 | ||
| 549 | // A try entry's structured try, which a return leaves, and the | |
| 550 | // region its finally body is generated into. | |
| 551 | structured_try: Values.TRY? public | |
| 552 | finally_region: Values.REGION? public | |
| 553 | ||
| 554 | return_needed: TEMP? | |
| 555 | return_value: TEMP? | |
| 556 | ||
| 557 | // Loop-as-expression state. `node` correlates the entry with | |
| 558 | // the AST loop, so a `break` whose target compile-expressions | |
| 559 | // resolved by symbol finds this entry without a name search. | |
| 560 | // The rest mirror the val-block's cross-try handling: a | |
| 561 | // value-carrying exit from inside a `.try` cannot `br` with a | |
| 562 | // value on the stack (`leave` requires it empty), so it stashes | |
| 563 | // into cross_try_temp and leaves cross_try_join_region, after | |
| 564 | // which the value is reloaded before result_region ends. | |
| 565 | node: Trees.Statements.Statement? public | |
| 566 | wants_value: bool public | |
| 567 | result_type: Semantic.Types.Type? public | |
| 568 | result_region: Values.REGION? public | |
| 569 | cross_try_temp: TEMP? public | |
| 570 | cross_try_join_region: Values.REGION? public | |
| 571 | cross_try_used: bool public | |
| 572 | spill_field: Semantic.Symbols.Field? public | |
| 573 | enclosing_try_count: int public | |
| 574 | ||
| 575 | init(name: string?) is | |
| 576 | is_try = false | |
| 577 | self.name = name | |
| 578 | start = LABEL() | |
| 579 | end = LABEL() | |
| 580 | si | |
| 581 | ||
| 582 | init(return_needed: TEMP, return_value: TEMP?) is | |
| 583 | is_try = true | |
| 584 | self.return_needed = return_needed | |
| 585 | self.return_value = return_value | |
| 586 | start = LABEL() | |
| 587 | middle = LABEL() | |
| 588 | end = LABEL() | |
| 589 | si | |
| 590 | ||
| 591 | matches(name: string) -> bool => self.name? /\ self.name! =~ name | |
| 592 | si | |
| 593 | ||
| 594 | class LOOP_LABEL_STACK is | |
| 595 | _next_name: string? | |
| 596 | ||
| 597 | loops: Collections.MutableList[LOOP_LABELS] | |
| 598 | is_in_loop: bool => get_current_loop() != null | |
| 599 | get_current_try() -> LOOP_LABELS? is | |
| 600 | let index: int mut = loops.count - 1 | |
| 601 | ||
| 602 | while index >= 0 do | |
| 603 | let result = loops[index] | |
| 604 | ||
| 605 | if result.is_try then | |
| 606 | return result | |
| 607 | fi | |
| 608 | ||
| 609 | index = index - 1 | |
| 610 | od | |
| 611 | return null | |
| 612 | si | |
| 613 | ||
| 614 | // Number of `is_try` entries currently on the stack. Cached | |
| 615 | // at val-block entry; a return inside the body whose count | |
| 616 | // exceeds the cached value opened a try INSIDE the block, | |
| 617 | // so `br end_label` would cross the try's boundary. | |
| 618 | open_try_count: int is | |
| 619 | let count mut = 0 | |
| 620 | for l in loops do | |
| 621 | if l.is_try then | |
| 622 | count = count + 1 | |
| 623 | fi | |
| 624 | od | |
| 625 | return count | |
| 626 | si | |
| 627 | ||
| 628 | get_current_loop() -> LOOP_LABELS? is | |
| 629 | let index: int mut = loops.count - 1 | |
| 630 | ||
| 631 | while index >= 0 do | |
| 632 | let result = loops[index] | |
| 633 | ||
| 634 | if result.is_loop then | |
| 635 | return result | |
| 636 | fi | |
| 637 | ||
| 638 | index = index - 1 | |
| 639 | od | |
| 640 | return null | |
| 641 | si | |
| 642 | ||
| 643 | init() is | |
| 644 | loops = Collections.LIST[LOOP_LABELS]() | |
| 645 | si | |
| 646 | ||
| 647 | next_name(name: string) is | |
| 648 | _next_name = name | |
| 649 | si | |
| 650 | ||
| 651 | find(name: string) -> LOOP_LABELS? is | |
| 652 | let index: int mut = loops.count - 1 | |
| 653 | ||
| 654 | while index >= 0 do | |
| 655 | let result = loops[index] | |
| 656 | ||
| 657 | if result.matches(name) then | |
| 658 | return result | |
| 659 | fi | |
| 660 | ||
| 661 | index = index - 1 | |
| 662 | od | |
| 663 | return null | |
| 664 | si | |
| 665 | ||
| 666 | enter_try(return_needed: TEMP, return_value: TEMP?) -> LOOP_LABELS is | |
| 667 | let result = LOOP_LABELS(return_needed, return_value) | |
| 668 | ||
| 669 | loops.add(result) | |
| 670 | ||
| 671 | return result | |
| 672 | si | |
| 673 | ||
| 674 | enter_loop(node: Trees.Statements.Statement?) -> LOOP_LABELS is | |
| 675 | let result = LOOP_LABELS(_next_name) | |
| 676 | ||
| 677 | result.node = node | |
| 678 | result.enclosing_try_count = open_try_count | |
| 679 | ||
| 680 | _next_name = null | |
| 681 | ||
| 682 | loops.add(result) | |
| 683 | ||
| 684 | return result | |
| 685 | si | |
| 686 | ||
| 687 | find_by_node(node: Trees.Statements.Statement) -> LOOP_LABELS? is | |
| 688 | let index: int mut = loops.count - 1 | |
| 689 | ||
| 690 | while index >= 0 do | |
| 691 | let candidate = loops[index] | |
| 692 | ||
| 693 | if candidate.is_loop /\ candidate.node == node then | |
| 694 | return candidate | |
| 695 | fi | |
| 696 | ||
| 697 | index = index - 1 | |
| 698 | od | |
| 699 | return null | |
| 700 | si | |
| 701 | ||
| 702 | // The innermost enclosing loop that consumes a value — where a | |
| 703 | // valued `break` delivers its result. Intermediate non-expression | |
| 704 | // loops are exited through. | |
| 705 | find_value_loop() -> LOOP_LABELS? is | |
| 706 | let index: int mut = loops.count - 1 | |
| 707 | ||
| 708 | while index >= 0 do | |
| 709 | let candidate = loops[index] | |
| 710 | ||
| 711 | if candidate.is_loop /\ candidate.wants_value then | |
| 712 | return candidate | |
| 713 | fi | |
| 714 | ||
| 715 | index = index - 1 | |
| 716 | od | |
| 717 | return null | |
| 718 | si | |
| 719 | ||
| 720 | leave_loop() is | |
| 721 | loops.remove_at(loops.count - 1) | |
| 722 | si | |
| 723 | si | |
| 724 | si |