Skip to content
← Back

src/semantic/symbols/closure.ghul

1
namespace Semantic.Symbols is
2
use IO.Std
3
4
use System.Text.StringBuilder
5
6
use IoC
7
use Logging
8
use Source
9
10
use IR.Values.Value
11
12
use Ghul.Pipes
13
14
class Closure: Function, Types.Typed abstract is
15
_is_loading_captures: bool
16
17
// A null element is the `$self` capture: the frame's captured
18
// instance has no symbol of its own, and its position in the set
19
// fixes its position in the frame constructor's argument list.
20
captured_values: Collections.SET[Symbol?]?
21
captured_type_arguments: Collections.MAP[Symbol, CAPTURED_TYPE_ARGUMENT]?
22
next_type_argument_index: int
23
24
is_self_captured: bool
25
is_delegate: bool
26
is_anon_func: bool => !frame? /\ !is_delegate
27
28
// The named delegate type this literal was compiled as, when its
29
// context expected one. The literal's own type stays the
30
// equivalent function type, so inference and the body walk are
31
// unaffected; only the constructed delegate differs.
32
delegate_target_type: Types.Type? public
33
34
// The parameters a literal written with an argument pack spread
35
// out declares in place of the one tuple it is emitted taking, in
36
// order, and absent for every other literal. The emitted method
37
// takes `pack_group` and unpacks it into a local for each on entry.
38
packed_parameters: Collections.List[Variable]? public
39
pack_group: LOCAL_ARGUMENT? public
40
41
// Set while this literal's body is being compiled, and emitted,
42
// inline in the method that would otherwise have called it through
43
// a delegate: the method it is inlined into, the literal's own
44
// parameter, and the method local that parameter reads. A fused
45
// pipe stage sets it.
46
//
47
// The closure stays the owner of every symbol, scope and nested
48
// literal in its body, so names resolve exactly as they do when it
49
// is compiled as a closure, at every nesting depth. What changes is
50
// how those symbols load: `parameter` as `local`, a captured outer
51
// variable as the method's own local, and neither through a frame,
52
// since an inlined literal has none. Every other symbol the
53
// closure owns - a local a block in the body declares - loads as
54
// it does in any method, which is why the load tests the parameter
55
// symbol itself rather than what owns the symbol it is given.
56
inlined_into: (method: Function, parameter: Symbol, local: Variable)? public
57
58
is_inlined: bool => inlined_into?
59
60
// The block the inlined body's IL is emitted into, by generate-il's
61
// visit of the literal, and which the fused loop emits where it
62
// wants the body. Emitting into a block rather than in place is what
63
// lets the body be walked at the literal and land in the loop.
64
inlined_block: IR.Values.BLOCK? public
65
66
// The body node the block is filled from, and whether it has been.
67
inlined_body_node: Syntax.Trees.Expressions.Expression? public
68
is_inlined_body_filled: bool public
69
70
// Set for a generated argument pack thunk, which has no literal
71
// behind it and whose body is emitted from this.
72
pack_thunk: PACK_THUNK? public
73
74
is_packed_parameter(symbol: Symbol) -> bool is
75
if let packed = packed_parameters then
76
for p in packed do
77
if p == symbol then
78
return true
79
fi
80
od
81
fi
82
83
return false
84
si
85
86
// The type of the delegate this literal constructs, and so of the
87
// value its load yields.
88
// The target a context pushed may name a placeholder whose origin
89
// has since settled; the delegate is constructed at what it
90
// settled to.
91
constructed_delegate_type: Types.Type =>
92
if let target = delegate_target_type then
93
if target.contains_inferred then
94
SETTLED_PLACEHOLDER_RESOLVER.instance.resolve(target)
95
else
96
target
97
fi
98
else
99
type!
100
fi
101
102
could_be_delegate: bool => false
103
104
frame: FRAME?
105
106
short_description: string => description
107
108
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
109
PARTS.type_ref(type!)
110
111
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "closure"
112
113
symbol_kind: SymbolKind => SymbolKind.FUNCTION
114
completion_kind: CompletionKind => CompletionKind.FUNCTION
115
116
qualified_name: string => "[closure]{name}" // FIXME
117
118
is_closure: bool => true
119
120
// The local a nested named function statement declared for this
121
// literal. The local is not assigned until that statement
122
// completes, so a reference to the name from inside the body -
123
// the function calling itself - is served from the recurse field
124
// rather than from the local, which is still empty there.
125
self_reference_variable: Variable? public
126
is_recursive: bool
127
128
init(location: LOCATION, owner: Scope, name: string, enclosing_scope: Scope, is_recursive: bool) is
129
super.init(location, location, owner, name, enclosing_scope)
130
131
self.is_recursive = is_recursive
132
si
133
134
is_stateless_delegate: bool
135
136
// Set once a cacheable literal that captures nothing has been given an
137
// empty frame, so that the delegate it caches targets an instance
138
// method. The frame makes the literal look like one that captures, so
139
// the decision to cache it is latched here rather than re-derived.
140
is_cached_on_frame: bool
141
142
// Set when the delegate's body loads self (its `ldarg.0` target).
143
// Such a delegate, e.g. `() => self`, returns a self-dependent
144
// result, so it must not be shared across receivers even though it
145
// captures no values.
146
_delegate_body_loads_self: bool
147
148
delegate_body_loads_self: bool => _delegate_body_loads_self
149
150
note_delegate_body_loads_self() is
151
_delegate_body_loads_self = true
152
si
153
154
// True only while this closure's own literal body is being walked,
155
// so a self-load can be attributed to the body rather than to a
156
// separate scope constructing the delegate (binding its target).
157
_walking_literal_body: bool
158
159
enter_literal_body() is
160
_walking_literal_body = true
161
162
// Each walk of the body answers for itself: a self-load an
163
// earlier walk saw - a nested delegate bound to the receiver
164
// before it was found capture-free, say - is not one this
165
// walk makes.
166
_delegate_body_loads_self = false
167
si
168
169
leave_literal_body() is
170
_walking_literal_body = false
171
si
172
173
// A receiver load (`ldarg.0`) is about to be emitted; mark the
174
// closure whose body it lands in. That is the innermost closure
175
// currently walking its literal body - not necessarily the closure
176
// that answered the load: a nested literal's construction site sits
177
// in its enclosing literal's body, so binding the nested delegate's
178
// target loads the receiver in the enclosing body.
179
note_enclosing_body_loads_self() is
180
let stack = IoC.CONTAINER.instance.symbol_table.stack
181
let index mut = stack.count - 1
182
183
while index >= 0 do
184
if let closure: Closure = stack[index] then
185
if closure._walking_literal_body then
186
closure.note_delegate_body_loads_self()
187
return
188
fi
189
fi
190
191
index = index - 1
192
od
193
si
194
195
// A literal written inside one that is being compiled inline
196
// captures different symbols than it did when the enclosing
197
// literal was a closure: what was the enclosing literal's
198
// parameter is a local of the method it is inlined into. The
199
// captures an earlier walk recorded name the symbols it read
200
// then, and a capture is keyed on the symbol while the frame
201
// field it declares is keyed on the name, so keeping both sets
202
// leaves the frame with one field and the constructor call with
203
// two arguments. Dropping them lets the walk that follows record
204
// what the body now reads.
205
reset_captures() is
206
is_self_captured = false
207
captured_values = null
208
frame = null
209
si
210
211
convert_to_delegate() is
212
assert could_be_delegate
213
214
is_stateless_delegate = !captured_values?
215
216
is_self_captured = false
217
captured_values = null
218
frame = null
219
is_delegate = true
220
221
_drop_carried_type_arguments()
222
si
223
224
// A frame is a class of its own, so it declares every type
225
// parameter the closure's signature names. A delegate's method
226
// is emitted on the enclosing class instead and reaches that
227
// class's parameters through the receiver, so it must not
228
// declare them again - only an enclosing generic method's are
229
// left for it to carry.
230
_drop_carried_type_arguments() is
231
if !captured_type_arguments? then
232
return
233
fi
234
235
let kept =
236
get_sorted_captured_type_argument_symbols()! |>
237
filter(p => !is_type_argument_carried_by_emitting_class(p)) |>
238
collect()
239
240
// The slots that remain number from zero again: the position
241
// each installs is its index in the emitted method's own
242
// parameter list. Renumbered in place rather than recorded
243
// again, because recording goes back through the guard that
244
// treats a frozen copy of a parameter as that parameter, and
245
// a survivor that is a frozen copy of another survivor would
246
// be dropped there instead of renumbered.
247
let renumbered = Collections.MAP[Symbol, CAPTURED_TYPE_ARGUMENT]()
248
249
for i in 0..kept.count do
250
renumbered.add(kept[i], CAPTURED_TYPE_ARGUMENT(i))
251
od
252
253
next_type_argument_index = renumbered.count
254
255
if renumbered.count > 0 then
256
captured_type_arguments = renumbered
257
else
258
captured_type_arguments = null
259
fi
260
si
261
262
// True when this closure is emitted into the class that declares
263
// `parameter`, which then reaches the closure's body through the
264
// receiver rather than through a parameter of its own. A trait's
265
// parameter never is: the closure is not emitted on the trait.
266
is_type_argument_carried_by_emitting_class(parameter: Symbol) -> bool is
267
let symbol: Symbol? mut = self
268
269
while symbol? do
270
if let classy: Classy = symbol then
271
return !classy.is_trait /\ parameter.owner == classy
272
fi
273
274
let enclosing = cast Symbol?(symbol.owner)
275
276
// A namespace is its own owner, so a chain that reaches
277
// one without passing a class ends there.
278
if enclosing == symbol then
279
return false
280
fi
281
282
symbol = enclosing
283
od
284
285
return false
286
si
287
288
// A stateless, ground function literal whose delegate carries no
289
// per-evaluation state, so it can be built once and cached in a
290
// static field instead of reallocated on every use. Two shapes
291
// qualify, both with no captured type arguments and a fully-settled,
292
// ground function type:
293
//
294
// - a static or global anonymous function with no captures, cached
295
// on the enclosing namespace's `$globals`;
296
// - an instance-context delegate that captures no values and never
297
// loads self in its body, cached on its enclosing non-generic
298
// class, so the cache retains no receiver instance.
299
//
300
// Either way the cached delegate targets an empty frame: see
301
// _load_cached_on_frame.
302
//
303
// The settled/ground-type gate excludes literals whose inference
304
// never resolved or whose type mentions a type variable (either
305
// would produce an invalid cache field). The self-load gate
306
// excludes a delegate like `() => self` whose result depends on the
307
// receiver even though it captures nothing.
308
is_memoizable_delegate: bool is
309
if is_cached_on_frame then
310
return true
311
fi
312
313
if captured_type_arguments? then
314
return false
315
fi
316
317
let function_type = type
318
319
if !function_type? \/ !function_type.is_settled \/ !_is_ground_type(function_type) then
320
return false
321
fi
322
323
if is_anon_func then
324
return _enclosing_namespace()?
325
fi
326
327
if is_delegate /\ is_stateless_delegate /\ !_delegate_body_loads_self then
328
// Async literals lower through the state-machine path;
329
// their delegate creation is not worth caching (the state
330
// machine allocation dominates) and is left untouched.
331
if async_state_machine_for(self)? then
332
return false
333
fi
334
335
// Any class-like owner, a struct among them. A struct's
336
// instance member holds self as a managed pointer, which
337
// is not a legal delegate target, so a literal there has
338
// to reach this static shape rather than fall through to
339
// the receiver-bound one.
340
if let owner: Classy = get_il_owner() then
341
return !owner.is_generic
342
fi
343
344
return false
345
fi
346
347
return false
348
si
349
350
_is_ground_type(candidate: Semantic.Types.Type) -> bool is
351
let mentions_type_variable mut = false
352
353
candidate.walk((element: Semantic.Types.Type) is
354
if element.is_type_variable then
355
mentions_type_variable = true
356
fi
357
358
// A plain named reference to a generic type's own symbol -
359
// a union variant among these - rather than an
360
// instantiation of it names the open generic. It carries no
361
// arguments for a type-argument walk to see, and an open
362
// generic in a static field signature fails to load. A
363
// pack wrapper whose generic callee has not yet had its
364
// type arguments bound reaches this shape.
365
if
366
isa Semantic.Types.NAMED(element) /\
367
!isa Semantic.Types.GENERIC(element) /\
368
isa Semantic.Symbols.Classy(element.symbol) /\
369
cast Semantic.Symbols.Classy(element.symbol).is_generic
370
then
371
mentions_type_variable = true
372
fi
373
si)
374
375
return !mentions_type_variable
376
si
377
378
// Static field caching this literal's delegate. A static/global
379
// anonymous function caches on the enclosing namespace's `$globals`
380
// class; a stateless instance-context delegate caches on its
381
// enclosing class. Built lazily and shared between the load-site
382
// reference and the definition emitted by generate-il.
383
_delegate_cache_field: Field?
384
385
// True once a load site has referenced the cache field; the field
386
// definition is emitted iff this holds, so the definition tracks
387
// what the frozen load-site IL actually references.
388
has_delegate_cache_field: bool => _delegate_cache_field?
389
390
delegate_cache_field: Field is
391
if !_delegate_cache_field? then
392
let `field: Field =
393
if is_anon_func then
394
Symbols.GLOBAL_VARIABLE(LOCATION.internal, _enclosing_namespace()!, "{name}$cache")
395
else
396
Symbols.STATIC_FIELD(LOCATION.internal, get_il_owner(), "{name}$cache")
397
fi
398
`field.set_type(constructed_delegate_type)
399
_delegate_cache_field = `field
400
fi
401
402
// Retyped on every access: the closure's own type is
403
// rebuilt whenever its return is settled again, and the
404
// field created by an earlier walk has to hold what the
405
// final walk constructs.
406
let `field = _delegate_cache_field!
407
408
`field.set_type(constructed_delegate_type)
409
410
return `field
411
si
412
413
// Wrap a delegate-creation value so it is built once and reused.
414
// A no-op for literals that aren't memoizable.
415
memoize_delegate(value: Value) -> Value is
416
if !is_memoizable_delegate then
417
return value
418
fi
419
420
return IR.Values.MEMOIZED_DELEGATE(
421
value, delegate_cache_field, constructed_delegate_type, _callable_without_frame)
422
si
423
424
// A cacheable literal that captures nothing: its cached delegate targets
425
// one instance of an empty frame, built when the cache is first filled,
426
// so the method the delegate calls is an instance method. The JIT does
427
// not devirtualise or inline a call through a delegate over a static
428
// method.
429
_load_cached_on_frame(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
430
// Fetched before the frame exists: building the frame moves the
431
// literal's owner, and the cache stays where it has always been.
432
let cache = delegate_cache_field
433
434
is_cached_on_frame = true
435
436
_ensure_frame()
437
438
// Straight to the frame, not through load_closure: a literal already
439
// converted to a delegate would be bound to self there.
440
let delegate = _load_closure_non_generic(_get_actual_arguments(loader), loader)
441
442
return IR.Values.MEMOIZED_DELEGATE(delegate, cache, constructed_delegate_type, _callable_without_frame)
443
si
444
445
// This literal, unless it calls itself through rec, which reads it
446
// back from its frame.
447
_callable_without_frame: Function? =>
448
if frame?.find_member("$recurse")? then null else self fi
449
450
// Whether this load is one to route through _load_cached_on_frame.
451
_is_cached_on_frame_load: bool =>
452
is_cached_on_frame \/ (!frame? /\ !captured_type_arguments? /\ is_memoizable_delegate)
453
454
_enclosing_namespace() -> NAMESPACE? is
455
let scope: Scope? mut = owner
456
457
let guard mut = 0
458
while scope? /\ guard < 64 do
459
if let ns: NAMESPACE = scope then
460
return ns
461
fi
462
463
if let ns_scope: Semantic.NAMESPACE_SCOPE = scope then
464
return ns_scope.containing_namespace
465
fi
466
467
if let symbol: Symbol = scope then
468
scope = symbol.owner
469
else
470
return null
471
fi
472
473
guard = guard + 1
474
od
475
476
return null
477
si
478
479
// The frame and its captured-value set materialise together on
480
// first capture.
481
_ensure_frame() -> FRAME is
482
if !frame? then
483
captured_values = Collections.SET[Symbol?]()
484
485
frame = Semantic.Symbols.FRAME(owner!, self)
486
fi
487
488
return frame!
489
si
490
491
// A captured value's type is what makes the frame generic, and
492
// the frame is a class of its own, so every parameter that type
493
// names has to be one of its own. Recording it where the
494
// capture is made rather than where the frame is built is what
495
// lets anything compiled against the frame agree with it: the
496
// set is otherwise empty until emission.
497
_capture_type_arguments_of(type: Types.Type?) is
498
if !type? then
499
return
500
fi
501
502
type.walk(
503
t is
504
if t.is_type_variable then
505
add_type_argument_reference(t.symbol)
506
fi
507
si)
508
si
509
510
find_or_add_capture(variable: Symbol?) -> Field is
511
let frame = _ensure_frame()
512
let captured_values = self.captured_values!
513
514
if variable? then
515
if !captured_values.contains(variable) then
516
captured_values.add(variable)
517
518
// `storage_type` is the slot/field type at IL
519
// level — `Ghul.BOX[T]` when the captured
520
// local is boxed, plain `T` otherwise. For
521
// boxed locals the frame holds a reference to
522
// the shared box, so closure and enclosing
523
// scope agree on the cell.
524
let `field_type =
525
if isa Variable(variable) then
526
variable.storage_type
527
else
528
cast Types.Typed?(variable)!.type
529
fi
530
531
let captured = frame.declare_captured(variable.name, `field_type, IoC.CONTAINER.instance.symbol_definition_locations)
532
533
captured.captured_symbol = variable
534
535
_capture_type_arguments_of(`field_type)
536
537
return captured
538
else
539
let existing = frame.get_captured(variable.name)
540
541
if should_refresh_capture(existing, variable) then
542
// Mirror the boxed/unboxed distinction
543
// from declaration time — see comment in
544
// the declare-new branch above.
545
let refreshed_type =
546
if isa Variable(variable) then
547
variable.storage_type
548
else
549
cast Types.Typed?(variable)!.type
550
fi
551
552
existing!.set_type(refreshed_type!)
553
554
_capture_type_arguments_of(refreshed_type)
555
fi
556
557
return existing!
558
fi
559
else
560
if !is_self_captured then
561
is_self_captured = true
562
563
captured_values.add(null)
564
565
return frame.declare_captured("$self", _self_capture_type(), IoC.CONTAINER.instance.symbol_definition_locations)
566
else
567
// is_self_captured guarantees a prior declare_captured
568
// populated the "$self" slot.
569
return frame.get_captured("$self")!
570
fi
571
fi
572
si
573
574
// Type of the frame's captured `$self` field. The enclosing
575
// instance type comes from the scope stack rather than from the
576
// closure's owner, which for a body written in a `partial` or
577
// `impl` block is the block's own scope and carries no type.
578
_self_capture_type() -> Types.Type? is
579
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context ?? cast Classy?(owner)
580
581
if !context? then
582
return owner!.type
583
fi
584
585
let resolved = Collections.LIST[Symbol]()
586
587
let result = SELF_CAPTURE_TYPE((classy, name) => _own_type_argument(classy, name, resolved)).of(context)
588
589
// Recording is what gives the frame class the parameters the
590
// instantiated reference resolves against, so it applies only
591
// once every parameter resolved and that reference was built.
592
if isa Types.GENERIC(result) then
593
for argument in resolved do
594
add_type_argument_reference(argument)
595
od
596
fi
597
598
return result
599
si
600
601
_own_type_argument(context: Classy, name: string, resolved: Collections.LIST[Symbol]) -> Types.Type? is
602
let argument = context.find_direct(name)
603
604
if !argument? \/ !argument.type? then
605
return null
606
fi
607
608
resolved.add(argument)
609
610
return argument.type
611
si
612
613
// True iff the captured field's recorded type should be replaced
614
// on a re-capture pass. The previous iteration may have captured
615
// `source` before its type was IL-emit-stable, leaving the field
616
// pinned to an `INFERRED_VARIABLE_TYPE` sentinel or a null slot.
617
// Refresh when the source has a usable shape AND the existing
618
// field's recorded type is itself still un-settled. Two source-
619
// side gates:
620
//
621
// - `!is_sentinel` rather than `is_settled` so a composite like
622
// `Function[T,U]` with inner placeholders still promotes the
623
// field from `INFERRED_VARIABLE_TYPE` to `Function[T,U]`,
624
// letting call sites on the captured value see a NAMED
625
// function rather than an opaque sentinel. Inner T/U slots
626
// refine via the next body-retry iteration.
627
//
628
// - `is_defined` (for LOCAL_VARIABLE sources) guards against a
629
// `let f = ... f ...` forward self-reference silently
630
// "fixing" itself before the user-visible "cannot emit IL for
631
// function with unresolved return type" diagnostic fires.
632
// Destructured locals set `is_defined` ahead of the inner
633
// closure reference, so they still refresh correctly.
634
//
635
// A settled field over an unsettled source is refreshed too. Within
636
// one build a local keeps its placeholder until it settles, so that
637
// pairing only arises when the file's expressions are walked again
638
// against a retained closure: the field still holds what the
639
// previous walk settled, and binding the body against it would hide
640
// the body's uses from the placeholder the new walk is inferring.
641
should_refresh_capture(existing: Field?, source: Symbol?) -> bool static is
642
if !existing? \/ !source? then
643
return false
644
fi
645
646
let source_type = source.type
647
648
if !source_type? \/ source_type.is_sentinel then
649
return false
650
fi
651
652
let local = cast LOCAL_VARIABLE?(source)
653
if local? /\ !local.is_defined then
654
return false
655
fi
656
657
let existing_type = existing.type
658
659
return !existing_type? \/ !existing_type.is_settled \/ !source_type.is_settled
660
si
661
662
find_or_add_recurse() -> Field is
663
let frame = _ensure_frame()
664
665
let recurse = frame.find_member("$recurse")
666
667
if recurse? then
668
return cast Field?(recurse)!
669
else
670
return frame.declare_recurse(IoC.CONTAINER.instance.symbol_definition_locations)
671
fi
672
si
673
674
find_or_add_capture_self() -> Field => find_or_add_capture(null)
675
676
outer_recurse_field_name(outer: Closure) -> string =>
677
"$outer_recurse_{outer.name}"
678
679
// Captures the recurse-target of an outer recursive ancestor
680
// so `rec` can be used from a nested non-recursive lambda.
681
// The captured Symbol stored in `captured_values` is the
682
// outer Closure itself, used as a sentinel by
683
// `_get_actual_arguments` to dispatch to the outer-recurse
684
// codepath. Declaration order in `captured_values` mirrors
685
// declaration order in the frame, so the constructor-arg
686
// ordering stays in step with the frame-field ordering.
687
find_or_add_captured_outer_recurse(outer: Closure) -> Field is
688
assert outer != self else "cannot capture self as outer recurse"
689
690
let frame = _ensure_frame()
691
let captured_values = self.captured_values!
692
693
let field_name = outer_recurse_field_name(outer)
694
let existing = frame.find_member(field_name)
695
696
if existing? then
697
let `field = cast Field?(existing)!
698
699
// Always refresh from outer.type when non-error.
700
// outer.type is rebuilt by Function.set_return_type
701
// each iteration so a stale field would miss any
702
// widening (arg slot CONS[int] → List[int] from rec
703
// match propagation) or any return-slot resolution (INFERRED
704
// → string). Type-level `matches` returns true whenever
705
// one side is INFERRED, so it can't distinguish
706
// "still placeholder" from "now concrete".
707
// Refreshing unconditionally is safe: gen_type on
708
// placeholders emits a comment (per
709
// INFERRED_VARIABLE_TYPE / INFERRED_RETURN_TYPE
710
// overrides) and the next iter's freeze produces a
711
// cleaner RAW.
712
let outer_type = outer.type
713
714
if outer_type? /\ !outer_type.is_error then
715
`field.set_type(outer_type)
716
fi
717
718
return `field
719
fi
720
721
captured_values.add(outer)
722
723
return frame.declare_captured(field_name, outer.type, IoC.CONTAINER.instance.symbol_definition_locations)
724
si
725
726
load_captured_outer_recurse(location: LOCATION, outer: Closure, loader: SYMBOL_LOADER) -> Value is
727
let `field = find_or_add_captured_outer_recurse(outer)
728
729
return loader.load_instance_variable(LOCATION.internal, _capture_frame_self_load(), `field)
730
si
731
732
// Called from the enclosing closure's _get_actual_arguments to
733
// produce the value to pass for an inner frame's
734
// $outer_recurse_<outer> capture. If we are outer directly,
735
// load $recurse from our own frame; otherwise we must have
736
// captured outer's recurse ourselves (the chain-establish loop
737
// in visit(RECURSE) guarantees it).
738
load_outer_recurse_value(outer: Closure, location: LOCATION, loader: SYMBOL_LOADER) -> Value is
739
if self == outer then
740
return load_recurse(location, loader)
741
fi
742
743
// the chain-establish loop captured outer's recurse here,
744
// so the frame exists
745
let frame = self.frame!
746
747
let field_name = outer_recurse_field_name(outer)
748
let `field = cast Field?(frame.find_member(field_name))
749
750
assert `field? else "outer recurse field {field_name} not found on intermediate closure {name}"
751
752
return loader.load_instance_variable(LOCATION.internal, _capture_frame_self_load(), `field)
753
si
754
755
// Walks the live scope stack to find the closure that
756
// lexically encloses self — i.e. the closure into whose body
757
// the IR currently being built will be inserted. `current_function`
758
// is no good here: when this runs, self is still on the stack so
759
// `current_function` returns self.
760
_find_enclosing_closure() -> Closure? is
761
let stack = IoC.CONTAINER.instance.symbol_table.stack
762
let i mut = stack.count - 1
763
let seen_self mut = false
764
765
while i >= 0 do
766
let scope = stack[i]
767
768
if seen_self /\ isa Closure(scope) then
769
return scope
770
fi
771
772
if scope == self then
773
seen_self = true
774
fi
775
776
i = i - 1
777
od
778
return null
779
si
780
781
type_updated(type: Semantic.Types.Type) is
782
if let self.frame? /\ is_recursive then
783
frame.try_update_recurse_type(type)
784
fi
785
si
786
787
add_type_argument_reference(type: Symbol) is
788
// A type variable is emitted as an index into the type
789
// parameters of whatever declares it, and a block scope
790
// declares none: one owned by a block scope has no index to
791
// be written as. Such a variable reaches here from a formal
792
// resolved against a scope of its own, and recording it makes
793
// the frame generic in a parameter nothing binds, so the
794
// reference comes out against the enclosing method's
795
// parameters, which are not there, and the assembly will not
796
// load.
797
if isa BLOCK_SCOPE(type.owner) then
798
return
799
fi
800
801
if !captured_type_arguments? then
802
captured_type_arguments = Collections.MAP[Symbol,CAPTURED_TYPE_ARGUMENT]()
803
fi
804
805
let captures = captured_type_arguments!
806
807
if captures.contains_key(type) then
808
return
809
fi
810
811
// One frame slot per parameter, not per symbol standing for
812
// one. A captured type can arrive as the parameter's own
813
// symbol or as a frozen copy of it, and those are different
814
// objects; giving each a slot would number one parameter
815
// twice, and the position each slot installs is held against
816
// the parameter, so the second would overwrite the first and
817
// leave one of the two frame indices unreachable.
818
if _is_captured_parameter(captures, type) then
819
return
820
fi
821
822
captures.add(type, CAPTURED_TYPE_ARGUMENT(next_type_argument_index))
823
next_type_argument_index = next_type_argument_index + 1
824
si
825
826
_is_captured_parameter(
827
captures: Collections.MAP[Symbol,CAPTURED_TYPE_ARGUMENT],
828
type: Symbol
829
) -> bool is
830
let argument = cast GenericArgument?(type)
831
832
if !argument? then
833
return false
834
fi
835
836
let key = TYPE_PARAMETER_KEY.of(argument)
837
838
for captured in captures.keys do
839
if let captured_argument: GenericArgument = captured then
840
if TYPE_PARAMETER_KEY.of(captured_argument) =~ key then
841
return true
842
fi
843
fi
844
od
845
846
return false
847
si
848
849
set_type_arguments() is
850
if !captured_type_arguments? then
851
// not needed
852
return
853
fi
854
855
if let self.frame? then
856
frame.set_type_arguments(get_sorted_captured_type_argument_symbols()!)
857
return
858
fi
859
860
if generic_arguments.count > 0 then
861
// already done
862
return
863
fi
864
865
let args = get_sorted_captured_type_argument_symbols()!
866
867
generic_arguments = args |> map(a => a.type!) |> collect()
868
generic_argument_names = args |> map(a => a.name) |> collect()
869
si
870
871
get_sorted_captured_type_argument_symbols() -> Collections.List[Symbol]? =>
872
if captured_type_arguments? then
873
captured_type_arguments |> sort((a, b) => a.value.index - b.value.index) |> map(p => p.key) |> collect()
874
else
875
null
876
fi
877
878
get_sorted_captured_type_argument_types() -> Collections.List[Types.Type]? =>
879
if captured_type_arguments? then
880
captured_type_arguments |> sort((a, b) => a.value.index - b.value.index) |> map(p => p.key.type!) |> collect()
881
else
882
null
883
fi
884
885
get_type_arguments_as_specialize_map() -> Collections.Map[Symbol,Types.Type]? =>
886
if captured_type_arguments? then
887
Collections.MAP[Symbol,Types.Type](
888
// filter ensures key.type and key.type.freeze() are non-null
889
captured_type_arguments.keys
890
|> filter(key => let kt = key.type in kt? /\ kt.freeze()?) |>
891
map(key => Collections.KeyValuePair[Symbol,Types.Type](key, key.type!.freeze()!))
892
)
893
else
894
null
895
fi
896
897
// Saved emitted positions per captured type-arg symbol, set on
898
// entry to map_type_arguments and reinstated by
899
// unmap_type_arguments. Without this save and restore, an outer
900
// override (e.g. the enclosing generator's
901
// install_body_emission_overrides) would be clobbered by the
902
// closure's nullout in unmap and any later freeze of the
903
// outer's IR would emit the wrong type-arg index.
904
//
905
// It is a stack because the same closure's bracket nests inside
906
// itself: emitting a signature within a body already bracketed
907
// for that closure enters it a second time. With one slot per
908
// symbol the inner entry saves the closure's own position over
909
// the outer entry's saved value, and the matching pair of unmaps
910
// then clears the position instead of reinstating what was
911
// installed before the outermost entry.
912
_saved_emitted_positions: Collections.MAP[Symbol, Collections.LIST[(() -> TYPE_PARAMETER_POSITION)?]]?
913
914
map_type_arguments() is
915
if !captured_type_arguments? then
916
return
917
fi
918
919
let saved =
920
_saved_emitted_positions ??
921
Collections.MAP[Symbol, Collections.LIST[(() -> TYPE_PARAMETER_POSITION)?]]()
922
923
_saved_emitted_positions = saved
924
925
for symbol_cta in captured_type_arguments! do
926
let symbol = symbol_cta.key
927
let cta = symbol_cta.value
928
929
let prior_positions: Collections.LIST[(() -> TYPE_PARAMETER_POSITION)?] mut
930
931
if !saved.try_get_value(symbol, prior_positions ref) then
932
prior_positions = Collections.LIST[(() -> TYPE_PARAMETER_POSITION)?]()
933
saved[symbol] = prior_positions
934
fi
935
936
prior_positions.add(symbol.current_emitted_position)
937
938
// A closure with a frame reaches the argument as one of
939
// the frame class's own; without one it stays on the
940
// method. Which it is can still change after this point,
941
// so the answer is deferred rather than decided here.
942
symbol.set_emitted_position(
943
() => TYPE_PARAMETER_POSITION(frame?, cta.index))
944
od
945
si
946
947
unmap_type_arguments() is
948
if !captured_type_arguments? then
949
return
950
fi
951
952
let saved = _saved_emitted_positions
953
954
for symbol_cta in captured_type_arguments do
955
let symbol = symbol_cta.key
956
957
let prior_positions: Collections.LIST[(() -> TYPE_PARAMETER_POSITION)?] mut
958
959
if
960
saved? /\
961
saved.try_get_value(symbol, prior_positions ref) /\
962
prior_positions.count > 0
963
then
964
let last = prior_positions.count - 1
965
966
symbol.set_emitted_position(prior_positions[last])
967
prior_positions.remove_at(last)
968
else
969
symbol.set_emitted_position(null)
970
fi
971
od
972
si
973
974
load(location: LOCATION, loader: SYMBOL_LOADER) -> Value => throw System.NotImplementedException("abstract")
975
load_closure(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
976
try
977
return _load_closure(location, loader)
978
catch e: System.Exception
979
IoC.CONTAINER.instance.logger.exception(location, e, "exception loading closure")
980
981
return IR.Values.DUMMY(Types.ERROR(), location)
982
yrt
983
si
984
985
_get_actual_arguments(loader: SYMBOL_LOADER) -> Collections.LIST[Value] is
986
let actual_arguments = Collections.LIST[Value]()
987
988
let capture_type_argument = (t: Types.Type) is
989
if t.is_type_variable then
990
add_type_argument_reference(t.symbol)
991
fi
992
si
993
994
let enclosing: Closure? mut = null
995
996
for c in captured_values! do
997
if !c? then
998
// The "captured self" passed to the closure's
999
// frame constructor. Same construction-site
1000
// distinction as `_load_delegate`: inside a
1001
// generator's MoveNext, route through the state-
1002
// machine frame's `_outer_self` so the frame ctor
1003
// receives the user's instance and not the state
1004
// machine.
1005
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context
1006
let outer_self: Value? mut = null
1007
if context? then
1008
outer_self = _try_load_self_for_construction_via_outer_self(context)
1009
fi
1010
if !outer_self? then
1011
outer_self = loader.load_outer_self(location)
1012
fi
1013
1014
actual_arguments.add(outer_self)
1015
1016
// Guard: a forward-reference capture (e.g.
1017
// mutual recursion between two let-bound
1018
// lambdas) leaves the capture's type null
1019
// during the first walk. Skipping the type-var
1020
// discovery here is correct — the outer
1021
// function's body retry will re-walk the
1022
// closure with the resolved type. NRE-ing here
1023
// would mask the underlying "variable is not
1024
// defined here" diagnostic.
1025
if outer_self.type? then
1026
outer_self.type.walk(capture_type_argument)
1027
fi
1028
elif isa Closure(c) then
1029
let outer = c
1030
1031
if !enclosing? then
1032
enclosing = _find_enclosing_closure()
1033
fi
1034
1035
let value = enclosing!.load_outer_recurse_value(outer, location, loader)
1036
1037
actual_arguments.add(value)
1038
1039
if value.type? then
1040
value.type.walk(capture_type_argument)
1041
fi
1042
else
1043
let outer_captured_value =
1044
c.load_outer(
1045
location,
1046
null,
1047
loader
1048
)
1049
1050
actual_arguments.add(outer_captured_value)
1051
1052
if outer_captured_value.type? then
1053
outer_captured_value.type.walk(capture_type_argument)
1054
fi
1055
fi
1056
od
1057
1058
return actual_arguments
1059
si
1060
1061
// A closure emitted on the enclosing class reaches that class's
1062
// type parameters as `!N` through the instance, which is why
1063
// RECORD_TYPE_ARGUMENT_USES leaves them uncaptured. A closure
1064
// with captured values is emitted on a frame instead, and the
1065
// frame is a class of its own with no parameters of the
1066
// enclosing one, so whatever its signature names it must carry.
1067
// Whether there is a frame is not known when that pass runs.
1068
capture_signature_type_arguments() is
1069
let capture = (t: Types.Type) is
1070
if t.is_type_variable then
1071
add_type_argument_reference(t.symbol)
1072
fi
1073
si
1074
1075
for argument in arguments do
1076
argument.walk(capture)
1077
od
1078
1079
if let self.return_type? then
1080
return_type.walk(capture)
1081
fi
1082
si
1083
1084
_load_closure(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
1085
let closure_type = type
1086
1087
if !closure_type? then
1088
return IR.Values.DUMMY(Types.ERROR(), LOCATION.internal)
1089
fi
1090
1091
if closure_type.is_error then
1092
return IR.Values.DUMMY(closure_type, LOCATION.internal)
1093
fi
1094
1095
if is_delegate then
1096
return loader.load_instance_anonymous_function(self, constructed_delegate_type)
1097
fi
1098
1099
let actual_arguments = _get_actual_arguments(loader)
1100
1101
capture_signature_type_arguments()
1102
1103
if captured_type_arguments? then
1104
return _load_closure_generic(actual_arguments, loader)
1105
else
1106
return _load_closure_non_generic(actual_arguments, loader)
1107
fi
1108
si
1109
1110
_load_closure_generic(actual_arguments: Collections.LIST[Value], loader: SYMBOL_LOADER) -> Value is
1111
// See _load_closure_non_generic for the failed-speculation
1112
// try-catch rationale.
1113
try
1114
return _load_closure_generic_body(actual_arguments, loader)
1115
catch ex: System.Exception
1116
IoC.CONTAINER.instance.logger.mark_consumed_any()
1117
return IR.Values.DUMMY(self.type!, location)
1118
yrt
1119
si
1120
1121
_load_closure_generic_body(actual_arguments: Collections.LIST[Value], loader: SYMBOL_LOADER) -> Value is
1122
let frame = self.frame!
1123
1124
let block = IR.Values.BLOCK(constructed_delegate_type)
1125
1126
owner = frame
1127
1128
set_type_arguments()
1129
1130
// we want these type arguments to remain in the current context.
1131
// Frozen forms are filtered for non-null so we hand a real
1132
// List[Type] to get_create_instance — a captured generic with
1133
// no resolved freeze would point at an unresolved slot.
1134
let frozen_argument_types = get_sorted_captured_type_argument_types()!
1135
|> map(t => t.freeze()) |>
1136
filter(f => f?) |>
1137
map(f => f!) |>
1138
collect()
1139
1140
// but the formal arguments of the frame class constructor need to be in the frame's context
1141
map_type_arguments()
1142
let frame_instance = frame.get_create_instance(actual_arguments, frozen_argument_types)
1143
unmap_type_arguments()
1144
1145
let frame_instance_copier = frame_instance!.get_temp_copier(block, "frame-instance")
1146
1147
owner = frame_instance.type!.symbol
1148
1149
let result = IR.Values.MAKE_CLOSURE(constructed_delegate_type, self, frame_instance_copier())
1150
1151
let result_copier = result.get_temp_copier(block, "closure")
1152
1153
let rec_member_symbol = owner!.find_member("$recurse")
1154
1155
if rec_member_symbol? /\ !frame_instance.type!.is_error then
1156
let ff = frame_instance_copier()
1157
let rr = result_copier()
1158
map_type_arguments()
1159
block.add(rec_member_symbol.store(location, ff, rr, loader, true))
1160
unmap_type_arguments()
1161
fi
1162
1163
block.add(result_copier())
1164
block.close()
1165
return block
1166
si
1167
1168
_load_closure_non_generic(actual_arguments: Collections.LIST[Value], loader: SYMBOL_LOADER) -> Value is
1169
// Bail early when any captured value has an error or
1170
// unresolved type. Trying to construct the frame
1171
// instance with ERROR-typed captures cascades into IL
1172
// gen_type throws further down the path.
1173
for a in actual_arguments do
1174
if !a.type? \/ a.type.is_error then
1175
return IR.Values.DUMMY(Types.ERROR(), location)
1176
fi
1177
od
1178
1179
// The iterative-inference body-retry loop may reach
1180
// closure loading during an early iter where the FRAME's
1181
// declare_constructor defensive-defaulted a not-yet-typed
1182
// field to Types.ERROR — gen_type throws on that.
1183
// Treat the exception as failed speculation: return a
1184
// DUMMY of the closure's own type so downstream paths
1185
// see a placeholder closure rather than a poison value,
1186
// and mark_consumed_any so the body-retry loop iterates
1187
// again from improved types.
1188
try
1189
return _load_closure_non_generic_body(actual_arguments, loader)
1190
catch ex: System.Exception
1191
IoC.CONTAINER.instance.logger.mark_consumed_any()
1192
return IR.Values.DUMMY(self.type!, location)
1193
yrt
1194
si
1195
1196
_load_closure_non_generic_body(actual_arguments: Collections.LIST[Value], loader: SYMBOL_LOADER) -> Value is
1197
let frame = self.frame!
1198
1199
let block = IR.Values.BLOCK(constructed_delegate_type)
1200
1201
owner = frame
1202
1203
let frame_instance = frame.get_create_instance(actual_arguments, null)
1204
let frame_instance_copier = frame_instance!.get_temp_copier(block, "frame-instance")
1205
1206
owner = frame_instance.type!.symbol
1207
1208
let result = IR.Values.MAKE_CLOSURE(constructed_delegate_type, self, frame_instance_copier())
1209
1210
let result_copier = result.get_temp_copier(block, "closure")
1211
1212
let rec_member_symbol = frame.find_member("$recurse")
1213
1214
if rec_member_symbol? /\ !frame_instance.type!.is_error then
1215
block.add(rec_member_symbol.store(location, frame_instance_copier(), result_copier(), loader, true))
1216
fi
1217
1218
block.add(result_copier())
1219
block.close()
1220
return block
1221
si
1222
1223
// The delegate of a closure that takes no frame, built closed over
1224
// `target` as its first argument: the method is static and the
1225
// delegate's call shape is the method's less that parameter, which
1226
// is the shape the runtime allows for a reference first argument.
1227
// Built afresh at each use, since the target is a value.
1228
load_closed_over(location: LOCATION, target: Value, delegate_type: Types.Type) -> Value is
1229
if captured_type_arguments? then
1230
set_type_arguments()
1231
1232
let args_map = get_type_arguments_as_specialize_map()!
1233
1234
map_type_arguments()
1235
1236
let specialized = specialize_function(args_map, null)
1237
let delegate = IR.Values.MAKE_CLOSURE(delegate_type, specialized, target)
1238
1239
unmap_type_arguments()
1240
1241
return delegate
1242
fi
1243
1244
return IR.Values.MAKE_CLOSURE(delegate_type, self, target)
1245
si
1246
1247
_load_lambda() -> Value is
1248
let closure_type = type
1249
1250
if !closure_type? then
1251
return IR.Values.DUMMY(Types.ERROR(), LOCATION.internal)
1252
fi
1253
1254
if closure_type.is_error then
1255
return IR.Values.DUMMY(closure_type, LOCATION.internal)
1256
fi
1257
1258
if captured_type_arguments? then
1259
return _load_lambda_generic()
1260
else
1261
return _load_lambda_non_generic()
1262
fi
1263
si
1264
1265
_load_lambda_generic() -> Value is
1266
set_type_arguments()
1267
1268
// Generic lambdas always capture type arguments, so the
1269
// specialize map is present on this path.
1270
let args_map = get_type_arguments_as_specialize_map()!
1271
1272
map_type_arguments()
1273
1274
let specialized_lambda = specialize_function(args_map, null)
1275
1276
let delegate = IR.Values.MAKE_CLOSURE(delegate_target_type ?? specialized_lambda.type!, specialized_lambda, IR.Values.NULL(Types.NULL()))
1277
1278
unmap_type_arguments()
1279
1280
return delegate
1281
si
1282
1283
_load_lambda_non_generic() -> Value is
1284
let delegate = IR.Values.MAKE_CLOSURE(constructed_delegate_type, self, IR.Values.NULL(Types.NULL()))
1285
1286
return memoize_delegate(delegate)
1287
si
1288
1289
_load_delegate(loader: SYMBOL_LOADER) -> Value is
1290
let closure_type = type
1291
1292
if !closure_type? then
1293
return IR.Values.DUMMY(Types.ERROR(), LOCATION.internal)
1294
fi
1295
1296
if closure_type.is_error then
1297
return IR.Values.DUMMY(closure_type, LOCATION.internal)
1298
fi
1299
1300
// A delegate is converted from a literal that captured no
1301
// type arguments, but the walk that follows the conversion
1302
// can still settle a parameter type naming one. Such a
1303
// delegate is emitted as a generic method and reached
1304
// through an instantiation, as a frameless literal is.
1305
if captured_type_arguments? then
1306
return _load_delegate_generic(loader)
1307
fi
1308
1309
return IR.Values.MAKE_CLOSURE(constructed_delegate_type, self, _load_delegate_target(loader))
1310
si
1311
1312
// Delegate target: the receiver the runtime binds to the
1313
// delegate. Inside a generator's MoveNext, `ldarg.0` is
1314
// the state machine, so a plain `loader.load_self` would
1315
// bind the delegate to it rather than the user's
1316
// instance. Route through the state-machine frame's
1317
// `_outer_self` field at construction-time when there's
1318
// an enclosing generator; otherwise fall back to the
1319
// ordinary load_self path.
1320
_load_delegate_target(loader: SYMBOL_LOADER) -> Value is
1321
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context
1322
let self_pointer: Value? mut = null
1323
if context? then
1324
self_pointer = _try_load_self_for_construction_via_outer_self(context)
1325
fi
1326
if !self_pointer? then
1327
self_pointer = loader.load_self(LOCATION.internal)
1328
fi
1329
1330
// Binding the target loads the receiver at the construction
1331
// site, which sits in the enclosing literal's body when this
1332
// delegate is nested inside another literal.
1333
note_enclosing_body_loads_self()
1334
1335
return self_pointer
1336
si
1337
1338
// The delegate's own method carries the type parameters its
1339
// signature names, and the construction site reaches it through
1340
// an instantiation of them. Mirrors the frameless literal path;
1341
// a generic delegate is never memoised, since the cache field
1342
// would have to be one per instantiation.
1343
_load_delegate_generic(loader: SYMBOL_LOADER) -> Value is
1344
let self_pointer = _load_delegate_target(loader)
1345
1346
set_type_arguments()
1347
1348
let args_map = get_type_arguments_as_specialize_map()!
1349
1350
map_type_arguments()
1351
1352
let specialized = specialize_function(args_map, null)
1353
1354
let delegate =
1355
IR.Values.MAKE_CLOSURE(
1356
delegate_target_type ?? specialized.type!,
1357
specialized,
1358
self_pointer
1359
)
1360
1361
unmap_type_arguments()
1362
1363
return delegate
1364
si
1365
1366
load_outer_self(location: Source.LOCATION, loader: SYMBOL_LOADER) -> Value? is
1367
let symbol_table = IoC.CONTAINER.instance.symbol_table
1368
let context = symbol_table.current_instance_context
1369
1370
if is_delegate then
1371
note_enclosing_body_loads_self()
1372
1373
return IR.Values.Load.REFERENCE_SELF(context!, context.type)
1374
fi
1375
1376
let is_captured = false
1377
1378
let stack = symbol_table.stack
1379
1380
let index mut = stack.count - 1
1381
let seen_self mut = false
1382
1383
while index >= 0 do
1384
let scope = stack[index]
1385
1386
if scope.is_namespace then
1387
break
1388
fi
1389
1390
if seen_self /\ scope.is_capture_context then
1391
if scope.is_closure then
1392
let c = cast Closure?(scope)!
1393
1394
// FIXME: may not be needed - load_self() should capture self:
1395
c.find_or_add_capture_self()
1396
1397
return c.load_self(location, loader)
1398
fi
1399
fi
1400
1401
if scope == self then
1402
seen_self = true
1403
fi
1404
1405
index = index - 1
1406
od
1407
1408
if !is_captured then
1409
// load self normally:
1410
return IR.Values.Load.REFERENCE_SELF(context!, context.type)
1411
fi
1412
return null
1413
si
1414
1415
load_self(location: Source.LOCATION, loader: SYMBOL_LOADER) -> Value is
1416
if is_delegate then
1417
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context!
1418
1419
note_enclosing_body_loads_self()
1420
1421
// A delegate is emitted as a method on the enclosing type,
1422
// so its receiver is normally `ldarg.0`. For an async
1423
// literal the body lives in the state machine's MoveNext,
1424
// where `ldarg.0` is the state machine — the receiver is
1425
// reached through its `$outer_self` field instead. Only the
1426
// body is inside MoveNext: the load that binds the
1427
// delegate's target sits in the enclosing method, where
1428
// `ldarg.0` is the receiver as usual.
1429
if _walking_literal_body then
1430
if let redirected = _state_machine_outer_self_load(context) then
1431
return redirected
1432
fi
1433
fi
1434
1435
return IR.Values.Load.REFERENCE_SELF(context, context.type)
1436
fi
1437
1438
let `field = find_or_add_capture_self()
1439
1440
return loader.load_instance_variable(location, _capture_frame_self_load(), `field)
1441
si
1442
1443
// At a closure-construction emission site that ends up inside
1444
// an enclosing function's MoveNext, `ldarg.0` refers to that
1445
// function's state machine — so the closure's delegate target
1446
// / captured-self value can't be loaded via REFERENCE_SELF
1447
// (which would emit `ldarg.0` of the state machine). Instead
1448
// load through the state-machine frame's `_outer_self` field,
1449
// which the outer method populates with the user's instance at
1450
// .ctor time.
1451
//
1452
// Walks outward from this closure to find the closest
1453
// enclosing Function. If that function compiles into a state
1454
// machine whose frame carries `_outer_self`, returns the
1455
// matching `OUTER_SELF` IR Value; otherwise returns null and
1456
// the caller falls back to a plain REFERENCE_SELF.
1457
//
1458
// Called only at construction sites (`_load_delegate`,
1459
// `_get_actual_arguments`), which is what makes the *enclosing*
1460
// function's state machine the right one to look for. A
1461
// self-load in the closure's own body wants its own state
1462
// machine instead, and `load_self` handles that separately off
1463
// `_walking_literal_body`.
1464
_try_load_self_for_construction_via_outer_self(context: Symbol) -> Value? is
1465
let stack = IoC.CONTAINER.instance.symbol_table.stack
1466
let index mut = stack.count - 1
1467
1468
while index >= 0 do
1469
let scope = stack[index]
1470
1471
if scope == self then
1472
index = index - 1
1473
continue
1474
fi
1475
1476
if let function: Function = scope then
1477
if let outer_self_field = _enclosing_state_machine_outer_self_field(function) then
1478
return IR.Values.Load.OUTER_SELF(context, context.type, outer_self_field)
1479
fi
1480
1481
return null
1482
fi
1483
1484
index = index - 1
1485
od
1486
1487
return null
1488
si
1489
1490
// The `$outer_self` field of whichever state machine `function`
1491
// compiles into — generator or async. Null when it compiles into
1492
// neither, or when the frame carries no outer self (statics and
1493
// globals have none). declare() materialises the field; it is lazy
1494
// and idempotent, and required because the frame property only
1495
// constructs the bare FRAME object.
1496
_enclosing_state_machine_outer_self_field(function: Function) -> Field? is
1497
if let sm = state_machine_for(function) then
1498
if let frame = sm.frame then
1499
frame.declare()
1500
1501
return frame.outer_self_field
1502
fi
1503
elif let async_sm = async_state_machine_for(function) then
1504
if let frame = async_sm.frame then
1505
frame.declare()
1506
1507
return frame.outer_self_field
1508
fi
1509
fi
1510
1511
return null
1512
si
1513
1514
load_outer_captured_value(location: LOCATION, symbol: Variable, loader: SYMBOL_LOADER) -> Value? is
1515
let is_captured = false
1516
1517
let stack = IoC.CONTAINER.instance.symbol_table.stack
1518
1519
let index mut = stack.count - 1
1520
let seen_self mut = false
1521
1522
while index >= 0 do
1523
let scope = stack[index]
1524
1525
if seen_self /\ scope.is_capture_context then
1526
if symbol.owner == scope then
1527
// FIXME: use the symbol loader
1528
if isa LOCAL_ARGUMENT(symbol) then
1529
return IR.Values.Load.LOCAL_ARGUMENT(symbol)
1530
else
1531
return IR.Values.Load.LOCAL_VARIABLE(symbol)
1532
fi
1533
elif scope.is_closure then
1534
let c = cast Closure?(scope)!
1535
1536
// A literal compiled inline has no frame to relay
1537
// the value through, so the walk continues past it
1538
// to whatever encloses it. Capturing into it would
1539
// record a field on a frame nothing constructs.
1540
if !c.is_inlined then
1541
// FIXME: probably redundant
1542
c.find_or_add_capture(symbol)
1543
1544
c.load_captured_value(location, symbol, loader)
1545
1546
let result = c.load_captured_value(location, symbol, loader)
1547
1548
return result
1549
fi
1550
fi
1551
fi
1552
1553
if scope == self then
1554
seen_self = true
1555
fi
1556
1557
index = index - 1
1558
od
1559
return null
1560
si
1561
1562
load_recurse(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
1563
let recurse = find_or_add_recurse()
1564
1565
return recurse.load(location, _capture_frame_self_load(), loader)
1566
si
1567
1568
// Closure-body store into an outer-scope captured local.
1569
// Only legal when the captured local is boxed — outer
1570
// scope and closure share a heap cell, so writing via
1571
// `.value` on the captured box updates the cell visible
1572
// to the outer scope. Unboxed captures are still
1573
// rejected upstream by the symbol-loader diagnostic.
1574
store_captured_value(location: LOCATION, symbol: Variable, value: Value, loader: SYMBOL_LOADER) -> Value is
1575
symbol.is_captured = true
1576
1577
let `field = find_or_add_capture(symbol)
1578
1579
let frame_field_load =
1580
loader.load_instance_variable(LOCATION.internal, _capture_frame_self_load(), `field)
1581
1582
if symbol.is_boxed then
1583
let value_member = loader.resolve_box_value_member(symbol)
1584
1585
if value_member? then
1586
return value_member.store(LOCATION.internal, frame_field_load, value, loader, false)
1587
fi
1588
fi
1589
1590
// Fall back to a frame-field store for the unboxed
1591
// case — should not be reached in practice because
1592
// the upstream diagnostic blocks unboxed captured
1593
// assignment, but the defensive path keeps us out of
1594
// NRE territory if it ever does.
1595
return loader.store_instance_variable(LOCATION.internal, _capture_frame_self_load(), `field, value)
1596
si
1597
1598
// A `T ref` parameter's slot lives on the enclosing method's
1599
// stack and dies when it returns, so a frame field cannot hold
1600
// it: the emitted ctor would store the byref through a raw
1601
// pointer whose pointee is gone by the time the closure runs.
1602
captures_reference(symbol: Variable) -> bool static is
1603
if let symbol_type = symbol.type then
1604
return isa Types.REFERENCE(symbol_type)
1605
fi
1606
1607
return false
1608
si
1609
1610
load_captured_value(location: LOCATION, symbol: Variable, loader: SYMBOL_LOADER) -> Value is
1611
if captures_reference(symbol) then
1612
IoC.CONTAINER.instance.logger.error(
1613
location,
1614
"a function literal cannot capture a ref parameter, copy the value into a local variable first")
1615
fi
1616
1617
// Capture-before-define is detected by LOCAL_VARIABLE.load
1618
// (`variable.ghul`) via its own check_is_defined call;
1619
// duplicating the check here fired the same diagnostic
1620
// twice for the captured-load path. The capture field is
1621
// built with whatever type the variable has at this
1622
// point (often null/ERROR), which without the upstream
1623
// check would crash Type.gen_type at IL emission — but
1624
// the upstream check is the only place we need.
1625
symbol.is_captured = true
1626
1627
let `field = find_or_add_capture(symbol)
1628
1629
// Returns the raw frame field — for boxed captures
1630
// this is the BOX[T] reference. The user-code-side
1631
// unwrap to `.value` happens in
1632
// `SYMBOL_LOADER.load_local_variable` after this
1633
// returns, so that inter-frame capture transfers
1634
// (via `load_outer_captured_value`) keep passing the
1635
// box reference between frames untouched.
1636
return loader.load_instance_variable(LOCATION.internal, _capture_frame_self_load(), `field)
1637
si
1638
1639
// Load the captures-frame `this` for capture access. Normally
1640
// REFERENCE_SELF (`ldarg.0` of the frame's type). For an
1641
// ASYNC_CLOSURE compiled down the state-machine path, the
1642
// body lives in the SM's MoveNext where `ldarg.0` is the SM
1643
// instance — the captures-frame is reached via the SM's
1644
// `_outer_self` field. Mirror of INSTANCE_ASYNC_METHOD's
1645
// load_self.
1646
_capture_frame_self_load() -> Value is
1647
let frame = self.frame!
1648
1649
return _state_machine_outer_self_load(frame) ?? IR.Values.Load.REFERENCE_SELF(frame, frame.type)
1650
si
1651
1652
// The `$outer_self` load that stands in for `ldarg.0` inside this
1653
// literal's own MoveNext, where `ldarg.0` is the state machine
1654
// rather than whatever the body means by self. `context` is the
1655
// symbol the loaded value is typed against — the captures frame
1656
// for a framed literal, the enclosing instance for a delegate.
1657
// Null when this literal does not compile into a state machine,
1658
// leaving the caller to emit an ordinary self reference.
1659
//
1660
// That is an async literal or a generator literal: either one's
1661
// body is inside its own MoveNext.
1662
_state_machine_outer_self_load(context: Symbol) -> Value? is
1663
if let outer_self_field = _enclosing_state_machine_outer_self_field(self) then
1664
return IR.Values.Load.OUTER_SELF(context, context.type, outer_self_field)
1665
fi
1666
1667
return null
1668
si
1669
1670
get_il_owner() -> Scope is
1671
if frame? \/ is_delegate then
1672
return owner!
1673
fi
1674
1675
let temp_owner = owner
1676
1677
let classy_owner = cast Classy?(owner)
1678
1679
if classy_owner? /\ classy_owner.owner? then
1680
return classy_owner.owner
1681
fi
1682
1683
// owner may be incorrect
1684
debug_always("suspect closure owner: {owner}")
1685
1686
return owner!
1687
si
1688
1689
// Whether the method emitted for this closure takes a receiver,
1690
// which is not the same question as `is_instance`: a closure is
1691
// not an instance member of anything the language can see, while
1692
// the method emitted for one whose captures live on a frame does
1693
// take that frame, and reaches the captures through it.
1694
//
1695
// Its flags and its calling convention both turn on this, and
1696
// they have to agree.
1697
is_emitted_with_receiver: bool => !is_anon_func
1698
1699
to_string() -> string => "[closure {name}]"
1700
si
1701
1702
// FIXME: pull common code across these Closure subclasses up
1703
// into Closure
1704
class INSTANCE_CLOSURE: Closure is
1705
is_instance: bool => true
1706
1707
could_be_delegate: bool =>
1708
let captured_values = self.captured_values in
1709
!captured_values? \/ (is_self_captured /\ captured_values.count == 1) /\ !is_recursive /\ _can_carry_captured_type_arguments
1710
1711
// A delegate's method is emitted on the enclosing class, which
1712
// supplies that class's own type parameters and nothing else, so
1713
// the only ones the method can declare for itself are an
1714
// enclosing generic method's. A trait's parameter is neither -
1715
// the method is not emitted on the trait - so a literal that
1716
// captured one keeps its frame, which declares every parameter
1717
// its signature names.
1718
_can_carry_captured_type_arguments: bool is
1719
let captures = captured_type_arguments
1720
1721
if !captures? then
1722
return true
1723
fi
1724
1725
for parameter in captures.keys do
1726
if
1727
!isa FUNCTION_GENERIC_ARGUMENT(parameter) /\
1728
!is_type_argument_carried_by_emitting_class(parameter)
1729
then
1730
return false
1731
fi
1732
od
1733
1734
return true
1735
si
1736
1737
init(location: LOCATION, owner: Scope, name: string, enclosing_scope: Scope, is_recursive: bool) is
1738
super.init(location, owner, name, enclosing_scope, is_recursive)
1739
si
1740
1741
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1742
declare_closure_symbol(location, Symbols.INSTANCE_CLOSURE(location, owner, name, enclosing, is_recursive))
1743
1744
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1745
declare_closure_symbol(location, Symbols.INSTANCE_ASYNC_CLOSURE(location, owner, name, enclosing, is_recursive))
1746
1747
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1748
declare_closure_symbol(location, Symbols.INSTANCE_GENERATOR_CLOSURE(location, owner, name, enclosing, is_recursive))
1749
1750
load(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
1751
if _is_cached_on_frame_load then
1752
return _load_cached_on_frame(location, loader)
1753
fi
1754
1755
if frame? then
1756
return super.load_closure(location, loader)
1757
fi
1758
1759
if is_delegate then
1760
return _load_delegate(loader)
1761
fi
1762
1763
return _load_lambda()
1764
si
1765
1766
to_string() -> string => "[instance closure {name}]"
1767
si
1768
1769
class STATIC_CLOSURE: Closure is
1770
init(location: LOCATION, owner: Scope, name: string, enclosing_scope: Scope, is_recursive: bool) is
1771
super.init(location, owner, name, enclosing_scope, is_recursive)
1772
si
1773
1774
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1775
declare_closure_symbol(location, Symbols.STATIC_CLOSURE(location, owner, name, enclosing, is_recursive))
1776
1777
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1778
declare_closure_symbol(location, Symbols.STATIC_ASYNC_CLOSURE(location, owner, name, enclosing, is_recursive))
1779
1780
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1781
declare_closure_symbol(location, Symbols.STATIC_GENERATOR_CLOSURE(location, owner, name, enclosing, is_recursive))
1782
1783
load(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
1784
if _is_cached_on_frame_load then
1785
return _load_cached_on_frame(location, loader)
1786
fi
1787
1788
if frame? then
1789
return super.load_closure(location, loader)
1790
fi
1791
1792
return _load_lambda()
1793
si
1794
1795
to_string() -> string => "[static closure {name}]"
1796
si
1797
1798
class GLOBAL_CLOSURE: Closure is
1799
init(location: LOCATION, owner: Scope, name: string, enclosing_scope: Scope, is_recursive: bool) is
1800
super.init(location, owner, name, enclosing_scope, is_recursive)
1801
si
1802
1803
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1804
declare_closure_symbol(location, Symbols.GLOBAL_CLOSURE(location, owner, name, enclosing, is_recursive))
1805
1806
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1807
declare_closure_symbol(location, Symbols.GLOBAL_ASYNC_CLOSURE(location, owner, name, enclosing, is_recursive))
1808
1809
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1810
declare_closure_symbol(location, Symbols.GLOBAL_GENERATOR_CLOSURE(location, owner, name, enclosing, is_recursive))
1811
1812
load(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
1813
let closure_type = type
1814
1815
if !closure_type? then
1816
return IR.Values.DUMMY(Types.ERROR(), LOCATION.internal)
1817
fi
1818
1819
if closure_type.is_error then
1820
return IR.Values.DUMMY(closure_type, LOCATION.internal)
1821
fi
1822
1823
if _is_cached_on_frame_load then
1824
return _load_cached_on_frame(location, loader)
1825
fi
1826
1827
if frame? then
1828
return super.load_closure(location, loader)
1829
fi
1830
1831
set_type_arguments()
1832
1833
return memoize_delegate(loader.load_global_anonymous_function(self, constructed_delegate_type))
1834
si
1835
1836
to_string() -> string => "[global closure {name}]"
1837
si
1838
1839
// TODO: this could be replaced with plain 'int'
1840
struct CAPTURED_TYPE_ARGUMENT is
1841
index: int
1842
1843
init(index: int) is
1844
self.index = index
1845
si
1846
si
1847
si