Skip to content
← Back

src/syntax/process/compile-expressions/compile_expressions_expressions.ghul

1
namespace Syntax.Process is
2
use System.Exception
3
use Ghul.Disposable
4
5
use IO.Std
6
7
use Logging
8
use Source
9
10
use IR.Values
11
use IR.VALUE_CONVERTER
12
use IR.VALUE_BOXER
13
14
use Semantic.LEAST_UPPER_BOUND_MAP
15
use Semantic.Types.Type
16
17
use Syntax.Trees.Definitions.PRAGMA
18
19
use Ghul.Pipes
20
21
22
// Expression walks: function literals, tuples, sequences, self, super, spill, await, cast,
23
// isa, typeof, operators, indexing, member access, generic applications and literals.
24
partial COMPILE_EXPRESSIONS is
25
pre(function: Trees.Expressions.FUNCTION) -> bool is
26
// Push a function-literal boundary marker on the val-
27
// block stack. A `return` inside this lambda's body
28
// looks up the innermost val-block via the top of the
29
// stack; the null marker reports "no val-block target"
30
// and the return falls through to the function-return
31
// path — exiting the lambda, not the enclosing val-
32
// block.
33
_val_block_stack.add(null)
34
35
return true
36
si
37
38
visit(function: Trees.Expressions.FUNCTION) is
39
let mark = _logger.mark()
40
let enclosing_disposal_host = disposal_host
41
let enclosing_disposal_list = disposal_list
42
disposal_host = null
43
disposal_list = null
44
let use uses_guard = _symbol_use_locations.mark_then_release()
45
46
// The lambda body walks under the enclosing method's
47
// environment (a closure may soundly observe a narrow
48
// in force at its construction point), but its own ifs /
49
// assignments / divergence must not leak back out — so
50
// the enclosing environment is saved and restored.
51
//
52
// The tracked deferred-init locals are deliberately NOT
53
// swapped out. Closures capture by value at construction
54
// time, so the closure body walking under `saved_env`
55
// observes exactly the definite-assignment facts in
56
// force where the closure is built — reading a captured
57
// local that is unassigned there is a genuine
58
// use-before-assignment (the closure captured a
59
// not-yet-assigned value), not a false positive.
60
let saved_env = _flow.current_env.copy()
61
62
try
63
RETRY_SITE_STATS.note("expressions.function_literal")
64
_logger.speculate()
65
_symbol_use_locations.speculate()
66
67
super.pre(function)
68
69
_lambdas.visit_function(function)
70
71
// The lambda's signature is settled by the line above,
72
// so this is the first point at which an async one is
73
// known to have a state machine — and the last before
74
// its body walks. A named async function's frame is
75
// realised at the equivalent point in
76
// visit(Definitions.FUNCTION); without the same here,
77
// the frame's constructor is first declared while the
78
// body is emitted, long after its row would have been
79
// numbered.
80
_declare_lambda_state_machine_frame(function)
81
82
super.visit(function)
83
84
_logger.commit()
85
_symbol_use_locations.commit()
86
catch e: Exception
87
function.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), function.location)
88
_logger.release(mark)
89
90
_logger.exception(function.location, e, "exception compiling function literal")
91
yrt
92
93
disposal_host = enclosing_disposal_host
94
disposal_list = enclosing_disposal_list
95
96
_flow.set_env(saved_env)
97
98
// Pop the function-literal boundary marker pushed by
99
// pre(FUNCTION). After this point an outer val-block
100
// (if any) is once again the innermost return target.
101
assert _val_block_stack.count > 0 else "val_block_stack underflow at function literal exit"
102
assert !_val_block_stack[_val_block_stack.count - 1]? else "val_block_stack head is not the function-literal marker"
103
_val_block_stack.remove_at(_val_block_stack.count - 1)
104
si
105
106
// See COMPILE_LAMBDAS.try_load_self_reference. Reached from the
107
// identifier load, which cannot see the lambda machinery itself.
108
try_load_self_reference(location: Source.LOCATION, symbol: Semantic.Symbols.Symbol) -> IR.Values.Value? =>
109
_lambdas.try_load_self_reference(location, symbol)
110
111
visit(recurse: Trees.Expressions.RECURSE) is
112
_lambdas.visit_recurse(recurse)
113
si
114
115
pre(tuple: Trees.Expressions.TUPLE) -> bool =>
116
_tuples.pre_tuple(tuple)
117
118
visit(tuple: Trees.Expressions.TUPLE) is
119
_tuples.visit_tuple(tuple)
120
si
121
122
pre(sequence: Trees.Expressions.SEQUENCE) -> bool => true
123
visit(sequence: Trees.Expressions.SEQUENCE) is
124
let mark = _logger.mark()
125
126
try
127
RETRY_SITE_STATS.note("expressions.sequence")
128
_logger.speculate()
129
130
super.pre(sequence)
131
132
sequence.type_expression.walk(self)
133
134
// If the sequence is going to compile against a known
135
// list/array type — either an inline type annotation
136
// (`[1, 2, 3]: int[]`) or a constraint pushed by the
137
// surrounding context (typed initializer / list-of-
138
// lambdas) — push the corresponding element type down
139
// to each element as its own constraint. Constraint-
140
// aware element types (notably function literals) use
141
// it to infer their argument types; element types that
142
// ignore constraint (most literals) are unaffected.
143
let element_constraint: Type? mut = _
144
145
if let sequence.type_expression? /\ !isa Trees.TypeExpressions.INFER(type_expression), type_expression.type? then
146
element_constraint = type.get_element_type()
147
elif let sequence.expected_type? then
148
element_constraint = expected_type.get_element_type()
149
fi
150
151
for e in sequence.elements do
152
if element_constraint? then
153
e.set_expected_type(element_constraint, "element type {{0}} not compatible with inferred list type {{1}}")
154
else
155
e.clear_expected_type()
156
fi
157
od
158
159
sequence.elements.walk(self)
160
161
_tuples.visit_sequence(sequence)
162
163
_logger.commit()
164
catch e: Exception
165
sequence.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), sequence.location)
166
_logger.release(mark)
167
168
_logger.exception(sequence.location, e, "exception compiling call")
169
yrt
170
si
171
172
visit(`self: Trees.Expressions.SELF) is
173
let s = current_instance_context
174
175
let type: Type? mut = null
176
177
if s? then
178
let f = current_function
179
180
if !f? \/ !f.is_instance then
181
_logger.error(`self.location, "cannot access self from non-instance context")
182
fi
183
184
// A closure body that reads `self` is self-dependent even
185
// if it captures no values (e.g. `() => self`), so its
186
// delegate must not be memoized and shared across receivers.
187
if let closure: Semantic.Symbols.Closure = f then
188
closure.note_delegate_body_loads_self()
189
190
// A delegate's target is an object, and a struct's
191
// instance member holds self as a managed pointer, so
192
// a literal written there has nothing to bind to.
193
// Copying the struct in would give the literal a
194
// snapshot rather than the receiver, which is a
195
// different program from the one written.
196
if isa Semantic.Symbols.STRUCT(s) then
197
_logger.error(
198
`self.location,
199
"cannot access self of a struct in a function literal"
200
)
201
fi
202
fi
203
204
if s.argument_names.count > 0 then
205
let arguments = Collections.LIST[Type]()
206
207
for n in s.argument_names do
208
let argument = s.find_member(n)
209
let argument_type = if argument? then argument.type else null fi
210
211
if argument? /\ argument.is_type_variable /\ argument_type? then
212
arguments.add(argument_type)
213
fi
214
od
215
216
if arguments.count == s.argument_names.count then
217
type = Semantic.Types.GENERIC(
218
`self.location,
219
cast Semantic.Symbols.Classy(s),
220
arguments)
221
fi
222
fi
223
224
// A flow narrowing on `self` (keyed on its instance
225
// context) presents `self` at the narrowed type, so
226
// `isa`/destructure and member access see the variant.
227
// A bare variant narrow (`CONS`) is specialised against
228
// `self`'s closed type (`List[T]`) so it resolves to its
229
// closed-generic form (`CONS[T]`) for a loadable reference.
230
if let narrowed = _flow.current_env.narrowed_type_of(s) then
231
if type? then
232
type = _condition_analyzer.specialize_variant_for_receiver(type, narrowed)
233
else
234
type = narrowed
235
fi
236
fi
237
238
// Generator instance methods read `self` from the
239
// frame's _outer_self field — the state-machine's
240
// ldarg.0 is the state machine itself, not the
241
// user instance.
242
let state_machine = Semantic.Symbols.state_machine_for(current_function)
243
244
if state_machine? /\ state_machine.frame? /\ !s.is_value_type then
245
if let frame = state_machine.frame then
246
frame.declare()
247
248
if frame.outer_self_field? then
249
`self.compile_expressions_state.value = Load.OUTER_SELF(s, type, frame.outer_self_field)
250
fi
251
fi
252
fi
253
254
// A closure body's `ldarg.0` is the frame rather than the
255
// enclosing instance, so an explicit `self` reads the
256
// captured field that an implicit one already reads. The
257
// value is not cached: a literal that turns out to need no
258
// frame becomes a delegate after its body is walked, and
259
// the load has to follow it.
260
if let closure_context: Semantic.Symbols.Closure = f then
261
// The frame's own field is typed in the frame class's
262
// captured copies of the enclosing type arguments, which
263
// are different symbols from the method's however alike
264
// they render, so the load is presented at the type this
265
// walk resolved for `self`.
266
let from_frame = closure_context.load_self(`self.location, _symbol_loader)
267
268
`self.compile_expressions_state.value =
269
if type? then
270
IR.Values.TYPE_WRAPPER(type, from_frame)
271
else
272
from_frame
273
fi
274
fi
275
276
if !`self.value? then
277
if s.is_value_type then
278
`self.compile_expressions_state.value =
279
Load.VALUE_SELF(
280
s, type
281
)
282
else
283
`self.compile_expressions_state.value =
284
Load.REFERENCE_SELF(
285
s, type
286
)
287
fi
288
fi
289
290
// FIXME: this breaks VSCode rename symbol:
291
// _symbol_use_locations.add_symbol_use(`self.location, s);
292
else
293
_logger.error(`self.location, "cannot access self from non-instance context")
294
fi
295
si
296
297
visit(`super: Trees.Expressions.SUPER) is
298
// FIXME:
299
let s = _symbol_table.current_instance_context
300
301
if s? then
302
if s.is_trait then
303
_logger.error(`super.location, "{s.short_description} does not have a super class")
304
305
return
306
fi
307
308
let `classy = cast Semantic.Symbols.Classy(s)
309
let super_type mut = `classy.ancestors[0]
310
311
// If the enclosing method overrides exactly one trait method,
312
// prefer that trait as the super type so super.foo() resolves
313
// to the trait's default body and emits as a non-virtual
314
// `call` (.NET DIM). Class-chain super stays on `ancestors[0]`
315
// because that already specialises generic base types — using
316
// the overridee's owner type would lose the specialisation.
317
let current = current_function
318
if current? /\ current.overridees? then
319
let overridee_count = current.overridees |> count()
320
if overridee_count == 1 then
321
let overridee = current.overridees |> first()
322
if overridee? /\ isa Semantic.Symbols.Classy(overridee.owner) then
323
let owner = cast Semantic.Symbols.Classy(overridee.owner)
324
let owner_type = owner.type
325
326
if owner.is_trait /\ owner_type? then
327
super_type = owner_type
328
fi
329
fi
330
fi
331
fi
332
333
`super.compile_expressions_state.value = Load.SUPER(s, super_type)
334
335
// FIXME: breaks VSCode rename symbol:
336
// _symbol_use_locations.add_symbol_use(`super.location, s);
337
fi
338
si
339
340
// SPILL is synthesised by the SPILL_AWAITS pass, in the
341
// rewrite-syntax-trees phase, so one is already in the tree by
342
// the time this runs. It types as a transparent wrapper over
343
// its operand; generate-il emits the operand eagerly and stores
344
// the result in the frame field allocated here.
345
visit(spill: Trees.Expressions.SPILL) is
346
spill.compile_expressions_state.value = null
347
348
let operand_value = spill.operand.value
349
350
if !operand_value? \/ !operand_value.type? then
351
spill.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), spill.location)
352
return
353
fi
354
355
// The receiver of a static call names a type rather than
356
// holding a value, so there is nothing on the stack for the
357
// suspend to lose and nothing a frame field could hold.
358
// Pass it through untouched.
359
if operand_value.is_type_expression then
360
spill.compile_expressions_state.value = operand_value
361
return
362
fi
363
364
_declare_spill_field(spill, operand_value.type)
365
366
spill.compile_expressions_state.value = IR.Values.WRAPPER(DUMMY(operand_value.type!, spill.location))
367
si
368
369
visit(`await: Trees.Expressions.AWAIT) is
370
`await.compile_expressions_state.value = null
371
372
let operand_value = `await.operand.value
373
374
if !operand_value? \/ !operand_value.type? then
375
`await.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `await.location)
376
return
377
fi
378
379
let operand_type = operand_value.type
380
381
let awaitable = _awaitable_resolver.resolve(`await.location, operand_type)
382
383
if !awaitable? then
384
`await.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `await.location)
385
return
386
fi
387
388
`await.awaitable = awaitable
389
390
let element_type = awaitable.result_type
391
392
if !element_type.is_void then
393
_declare_await_result_field(`await, element_type)
394
fi
395
396
_declare_awaiter_field(`await, awaitable.awaiter_type)
397
398
// Generate-il fills the wrapper with IR.Values.AWAIT_SUSPEND
399
// when emitting the SM body. The wrapper carries the result
400
// type so surrounding expressions / let-bindings see the
401
// right type at compile-expressions time.
402
`await.compile_expressions_state.value = IR.Values.WRAPPER(DUMMY(element_type, `await.location))
403
si
404
405
_use_local_id_counter: int static
406
407
// `use E` holds E's value in a local of its own and registers
408
// that local for disposal at the end of the enclosing statement
409
// list, or of the expression body where there is no list. The
410
// expression is the value, so it reads as E wherever it appears.
411
visit(`use: Trees.Expressions.USE) is
412
`use.compile_expressions_state.value = null
413
414
let operand = `use.operand.value
415
416
if !operand? \/ !operand.type? then
417
`use.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `use.location)
418
return
419
fi
420
421
let type = operand.type
422
423
if type.is_error then
424
`use.compile_expressions_state.value = operand
425
return
426
fi
427
428
if !_innate_symbol_lookup.get_idisposable_type().is_assignable_from(type) then
429
_logger.error(`use.operand.location, "not disposable")
430
`use.compile_expressions_state.value = operand
431
return
432
fi
433
434
let target = _use_target(`use, type)
435
436
if !target? then
437
`use.compile_expressions_state.value = operand
438
return
439
fi
440
441
if let list = disposal_list then
442
if !list.want_dispose \/ !(list.variables_to_dispose |> any(v => v == target)) then
443
list.add_variable_to_dispose(target)
444
fi
445
elif let host = disposal_host then
446
let registered = host.let_in_disposals
447
448
if !registered? \/ !(registered |> any(v => v == target)) then
449
host.add_let_in_disposal(target)
450
fi
451
else
452
_logger.error(`use.location, "cannot dispose a value here")
453
`use.compile_expressions_state.value = operand
454
return
455
fi
456
457
let block = IR.Values.BLOCK(type)
458
459
block.add(target.store(`use.location, null, operand, _symbol_loader, true))
460
block.add(target.load(`use.location, null, _symbol_loader))
461
block.close()
462
463
`use.compile_expressions_state.value = block
464
si
465
466
// The local a `use` holds its value in, declared on the first walk
467
// and kept on the node for later ones, since a registration or a
468
// frame field made on one walk has to name the same local on the
469
// next. It is declared in a scope of its own, so nothing can look
470
// it up by name, with an IL name no source local can have.
471
_use_target(`use: Trees.Expressions.USE, type: Semantic.Types.Type) -> Semantic.Symbols.Variable? is
472
let variable: Semantic.Symbols.Variable mut
473
474
if let existing = `use.target then
475
variable = existing
476
else
477
let scope = Semantic.BLOCK_SCOPE(_symbol_table.current_scope)
478
let local_id_generator = IoC.CONTAINER.instance.local_id_generator
479
480
local_id_generator.enter_function()
481
482
let declared = scope.declare_variable(`use.location, "use", false, null)
483
484
local_id_generator.leave_function()
485
486
if let local: Semantic.Symbols.Variable = declared then
487
variable = local
488
else
489
return null
490
fi
491
492
variable.il_name_override = ".use.{_use_local_id_counter}"
493
_use_local_id_counter = _use_local_id_counter + 1
494
495
variable.define()
496
variable.mark_synthesized()
497
498
`use.target = variable
499
fi
500
501
if let settable: Semantic.Types.SettableTyped = variable then
502
settable.set_type(type)
503
fi
504
505
if let frame = _enclosing_state_machine_frame() then
506
frame.declare_local_field(variable)
507
fi
508
509
return variable
510
si
511
512
// The `cast` keyword's own span. A CAST's location runs to the
513
// closing parenthesis, and the node keeps no separate span for
514
// the keyword, so it is derived: the location starts at the
515
// keyword, which is four characters wide, and a span's end
516
// column is inclusive.
517
_cast_keyword_location(`cast: Trees.Expressions.CAST) -> Source.LOCATION =>
518
Source.LOCATION(
519
`cast.location.file_name,
520
`cast.location.start_line,
521
`cast.location.start_column,
522
`cast.location.start_line,
523
`cast.location.start_column + 3
524
)
525
526
visit(`cast: Trees.Expressions.CAST) is
527
`cast.compile_expressions_state.value = null
528
529
// `cast (X)(args)` settled as a call of the cast value: the
530
// written-out reading was walked as this node's only child,
531
// so its value is this node's value.
532
if `cast.reading == Trees.Expressions.CastReading.CALLED then
533
`cast.compile_expressions_state.value = `cast.called_form!.value
534
535
return
536
fi
537
538
// `cast(v)` writes no target type and takes the one its
539
// context supplies, the way a bare `_` does. Nothing has
540
// pushed one in on the first walk of a call argument or an
541
// operator operand, so the error here is what tells the
542
// consumer this node is still waiting; the consumer rolls
543
// it back and re-walks once resolution has settled on a
544
// formal type.
545
let type mut =
546
if `cast.type_expression? then
547
`cast.type_expression.type
548
else
549
`cast.expected_type
550
fi
551
552
if !type? then
553
`cast.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `cast.location)
554
555
if `cast.type_expression? then
556
_logger.error(`cast.type_expression.location, "cast has no type")
557
else
558
_logger.error(`cast.location, "cannot infer the type to cast to here")
559
fi
560
561
return
562
fi
563
564
// The slot a typeless cast takes its target from can still
565
// hold an inference placeholder - the element type of a
566
// `LIST()` that a sibling `add` settles, say. A placeholder
567
// cannot be named in an instruction, so it is no more of a
568
// target than a missing one: report, and flag the walk as
569
// having consumed an unresolved type so the body-retry loop
570
// walks again once the placeholder has settled.
571
if !`cast.type_expression? /\ type.contains_inferred then
572
_logger.mark_consumed_any()
573
_logger.error(`cast.location, "cannot infer the type to cast to here")
574
575
`cast.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `cast.location)
576
577
return
578
fi
579
580
if `cast.type_expression? then
581
`cast.type_expression.check_is_not_reference(_logger, "cannot cast to a ref type")
582
else
583
// The target type is written nowhere, so hover over the
584
// keyword is where the reader sees what it resolved to.
585
// Recorded over the keyword alone rather than the whole
586
// expression, so hovering the value being converted
587
// still describes the value.
588
_symbol_use_locations.add_inferred_type_hover(_cast_keyword_location(`cast), type)
589
fi
590
591
let right_value = `cast.right.value
592
593
if !right_value? \/ !right_value.type? then
594
_logger.poison(`cast.right.location, "cast has no value")
595
596
return
597
fi
598
599
let specialized = _condition_analyzer.specialize_variant_for_receiver(
600
right_value.type,
601
type
602
)
603
604
if specialized? then
605
type = specialized
606
fi
607
608
// just check the cast is possible at this stage
609
_type_caster.check_cast_is_valid(`cast.location, right_value.type!, type)
610
611
// Warn when the cast can never succeed at runtime: the source's
612
// statically-known type is sealed enough that we can rule out
613
// any subtype that satisfies the target. The conservative form
614
// is a value-type source against a *reference* target that
615
// isn't one of its boxed-form ancestors — a value type's
616
// runtime type is its declared type, so `cast string(42)` and
617
// similar are unconditionally a null result. Value→value
618
// casts go through the numeric-conversion path in TYPE_CASTER
619
// and are left to that check; reference-source casts stay
620
// unwarned because the runtime value may be a subtype
621
// unrelated to the declared source.
622
//
623
// Skip the warning when the source is optional — `T?` to
624
// a reference target lowers to boxing the Nullable<T>
625
// (null when absent, the boxed T when present), which the
626
// CLR supports unconditionally. The strict non-nullable-
627
// by-default rule rejects `T? → T` at slot assignment, but
628
// an explicit cast expressing the boxing is sound and the
629
// warning would be a false positive.
630
let source_type = right_value.type
631
632
let is_impossible_cast =
633
source_type? /\
634
source_type.is_settled /\ !source_type.is_type_variable /\
635
!source_type.is_sentinel /\ !source_type.is_error /\
636
type.is_settled /\ !type.is_type_variable /\
637
!type.is_sentinel /\ !type.is_error /\
638
source_type.is_value_type /\ !type.is_value_type /\
639
!source_type.is_optional /\
640
!type.is_assignable_from(source_type) /\
641
!source_type.is_assignable_from(type) /\
642
!_type_caster.find_user_defined_conversion(source_type, type)?
643
644
if !_build_flags.no_warn_impossible_cast /\ is_impossible_cast then
645
_logger.warn(
646
`cast.location,
647
"impossible-cast",
648
"cast from {source_type} to {type} can never succeed"
649
)
650
fi
651
652
// A cast to a non-optional reference target throws when it
653
// fails, so a site that means "null on failure" wants
654
// `cast T?(...)`, which says so at the type level and lets the
655
// strict-optional slot check catch misuses where the value is
656
// stored. Skipped when the target is not a non-optional
657
// reference - `cast T?(...)` is the fix rather than a target of
658
// the warning, and a value-type or type-variable target does
659
// not throw - when the source is statically assignable to the
660
// target, so the cast is provably safe, and when the
661
// impossible-cast form already reported the site, since that
662
// message subsumes this one. A cast at an internal location was
663
// written by the compiler rather than by the user, who has
664
// nowhere to apply the advice.
665
if
666
!_build_flags.no_warn_cast_may_throw /\
667
!`cast.location.is_internal /\
668
!is_impossible_cast /\
669
source_type? /\
670
source_type.is_settled /\ !source_type.is_type_variable /\
671
!source_type.is_sentinel /\ !source_type.is_error /\
672
_is_non_optional_reference(type) /\
673
!_cast_target_covers_source(type, source_type)
674
then
675
let target = type
676
_logger.warn(
677
`cast.location,
678
"cast-may-throw",
679
"cast to non-optional {target} may throw",
680
`cast.location,
681
"help: use cast {target}? for null on failure"
682
)
683
fi
684
685
// we will fill this wrapper with the actual code to cast in the generate IL pass:
686
`cast.compile_expressions_state.value =
687
IR.Values.WRAPPER(DUMMY(type, `cast.location))
688
si
689
690
visit(`isa: Trees.Expressions.ISA) is
691
// Stamp the log position the test is walked at, so facts
692
// formed from it skip the calls that ran before it.
693
_flow.note_test_site(`isa)
694
695
`isa.compile_expressions_state.value = null
696
697
let isa_type mut = `isa.type_expression.type
698
699
if isa_type == null then
700
_logger.error(`isa.type_expression.location, "isa has no type")
701
`isa.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `isa.location)
702
return
703
fi
704
705
if let `isa.right?, right.value?, value.type? then
706
let specialized = _condition_analyzer.specialize_variant_for_receiver(
707
type,
708
isa_type
709
)
710
711
if specialized? then
712
isa_type = specialized
713
fi
714
fi
715
716
let bool_type = _innate_symbol_lookup.get_bool_type()
717
718
`isa.compile_expressions_state.value =
719
ISA(
720
bool_type,
721
isa_type,
722
`isa.right.value!
723
)
724
si
725
726
visit(`typeof: Trees.Expressions.TYPEOF) is
727
`typeof.compile_expressions_state.value = null
728
729
let typeof_type = `typeof.type_expression.type
730
731
if !typeof_type? \/ typeof_type.is_error then
732
if !typeof_type? then
733
_logger.error(`typeof.type_expression.location, "typeof has no type")
734
fi
735
736
`typeof.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), `typeof.location)
737
return
738
fi
739
740
let type_type = _innate_symbol_lookup.get_type_type()
741
742
`typeof.compile_expressions_state.value =
743
TYPEOF(
744
type_type,
745
typeof_type
746
)
747
si
748
749
visit(construct: Trees.Expressions.CONSTRUCT) is
750
_calls.visit_construct(construct)
751
_check_pure_slots(construct.location, construct.value, construct.arguments)
752
_note_call(construct.location, construct.value)
753
si
754
755
visit(unary: Trees.Expressions.UNARY) is
756
_operators.visit_unary(unary)
757
_check_pure_slots(unary.location, unary.value, null)
758
_note_call(unary.location, unary.value)
759
si
760
761
pre(binary: Trees.Expressions.BINARY) -> bool is
762
return _operators.pre_binary(binary)
763
si
764
765
visit(binary: Trees.Expressions.BINARY) is
766
try
767
_operators.visit_binary(binary)
768
_check_pure_slots(binary.location, binary.value, null)
769
_note_call(binary.location, binary.value)
770
catch ex: Exception
771
_logger.exception(binary.location, ex, "exception compiling binary operator (called from {Logging.CALL_STACK.text.replace_line_endings(" ")})")
772
yrt
773
si
774
775
visit(field_equals: Trees.Expressions.FIELD_EQUALS) is
776
try
777
_operators.visit_field_equals(field_equals)
778
_note_call(field_equals.location, field_equals.value)
779
catch ex: Exception
780
_logger.exception(field_equals.location, ex, "exception compiling field comparison (called from {Logging.CALL_STACK.text.replace_line_endings(" ")})")
781
yrt
782
si
783
784
visit(memberwise_equals: Trees.Expressions.MEMBERWISE_EQUALS) is
785
try
786
_operators.visit_memberwise_equals(memberwise_equals)
787
_note_call(memberwise_equals.location, memberwise_equals.value)
788
catch ex: Exception
789
_logger.exception(memberwise_equals.location, ex, "exception compiling synthesized equality")
790
yrt
791
si
792
793
visit(memberwise_hash: Trees.Expressions.MEMBERWISE_HASH) is
794
try
795
_operators.visit_memberwise_hash(memberwise_hash)
796
_note_call(memberwise_hash.location, memberwise_hash.value)
797
catch ex: Exception
798
_logger.exception(memberwise_hash.location, ex, "exception compiling synthesized hash")
799
yrt
800
si
801
802
visit(hash_operand: Trees.Expressions.HASH_OPERAND) is
803
try
804
_operators.visit_hash_operand(hash_operand)
805
_note_call(hash_operand.location, hash_operand.value)
806
catch ex: Exception
807
_logger.exception(hash_operand.location, ex, "exception compiling hash operand (called from {Logging.CALL_STACK.text.replace_line_endings(" ")})")
808
yrt
809
si
810
811
// Build a value-equality test Value for a `case` `when` label,
812
// the way `=~` (or `<>`, compared to zero) would. Forwards to
813
// the shared operator machinery so `case` reuses the same
814
// null-safe lowering and resolution as the `=~` operator. See
815
// COMPILE_OPERATORS._try_equality_test.
816
//
817
// No source token in either caller names the operator, so the
818
// operator use is recorded at whatever location the caller
819
// hands in: the whole `case` arm for a label, the leaf itself
820
// for a literal destructure, colouring that span as a method
821
// call. Suppress symbol-use recording for the whole build. A
822
// written `=~` is unaffected because the suppression is scoped
823
// to this call frame: the binary-operator visitor records its
824
// use outside it, whichever lowering it takes (including
825
// _build_null_safe_equality, which this builder also reaches).
826
build_equality_test(
827
left_value: IR.Values.Value,
828
right_value: IR.Values.Value,
829
op_name: string,
830
location: Source.LOCATION
831
) -> IR.Values.Value? is
832
_symbol_use_locations.begin_suppress()
833
834
try
835
return _operators._try_equality_test(left_value, right_value, op_name, location)
836
finally
837
_symbol_use_locations.end_suppress()
838
yrt
839
si
840
841
pre(index: Trees.Expressions.INDEX) -> bool =>
842
_operators.pre_index(index)
843
844
visit(index: Trees.Expressions.INDEX) is
845
try
846
_operators.visit_index(index)
847
check_receiver_present(index.left)
848
_check_pure_slots(index.location, index.value, null)
849
_note_call(index.location, index.value)
850
catch e: Exception
851
index.compile_expressions_state.value = null
852
853
_logger.exception(index.location, e, "exception compiling index")
854
yrt
855
si
856
857
visit(member: Trees.Expressions.MEMBER) is
858
_access.visit_member(member)
859
_note_call(member.location, member.value)
860
si
861
862
pre(ambiguous_expression: Trees.Expressions.AMBIGUOUS_EXPRESSION) -> bool =>
863
_generic_application.pre_ambiguous_expression(ambiguous_expression)
864
865
visit(ambiguous_expression: Trees.Expressions.AMBIGUOUS_EXPRESSION) is
866
si
867
868
pre(generic_application: Trees.Expressions.GENERIC_APPLICATION) -> bool =>
869
_generic_application.pre_generic_application(generic_application)
870
871
visit(generic_application: Trees.Expressions.GENERIC_APPLICATION) is
872
si
873
874
visit(integer: Trees.Expressions.Literals.INTEGER) is
875
_literals.visit_integer(integer)
876
si
877
878
visit(float: Trees.Expressions.Literals.FLOAT) is
879
_literals.visit_float(float)
880
si
881
882
visit(interpolation: Trees.Expressions.STRING_INTERPOLATION) is
883
_literals.visit_interpolation(interpolation)
884
885
// An alignment clause is handed to the interpolation
886
// handler's int parameter as-is, so a non-int alignment
887
// has to be rejected here rather than reach emission.
888
let int_type = _innate_symbol_lookup.get_int_type()
889
890
for fragment in interpolation.values do
891
if !fragment.is_expression then
892
continue
893
fi
894
895
if let alignment = fragment.alignment then
896
if let alignment_type = alignment.value?.type then
897
if !alignment_type.is_error /\ !alignment_type.is_inferred /\
898
alignment_type.compare(int_type) != Semantic.Types.MATCH.SAME then
899
_logger.error(
900
alignment.location,
901
"interpolation alignment must be int, not {alignment_type}")
902
fi
903
fi
904
fi
905
od
906
907
// Interpolation formats each fragment through to_string at
908
// run time, but those calls are synthesised at emission and
909
// never surface as call values here, so the call transfer
910
// has to fire for them. Each fragment is recorded against
911
// the to_string it will dispatch to, so the judge can
912
// discharge it like any other call; a fragment whose target
913
// cannot be named records an unbounded crossing instead.
914
_note_interpolation_calls(interpolation)
915
si
916
917
_note_interpolation_calls(interpolation: Trees.Expressions.STRING_INTERPOLATION) is
918
for fragment in interpolation.values do
919
if !fragment.is_expression then
920
continue
921
fi
922
923
// a format specifier selects a different, culture-aware
924
// formatting path
925
if fragment.format? then
926
_flow.on_call(interpolation.location)
927
return
928
fi
929
930
let fragment_type = fragment.expression.value?.type
931
932
if !fragment_type? then
933
_flow.on_call(interpolation.location)
934
return
935
fi
936
937
let to_string = _zero_argument_to_string(fragment_type.find_member("to_string"))
938
939
if !to_string? then
940
_flow.on_call(interpolation.location)
941
return
942
fi
943
944
// same structural gate as _note_call
945
if to_string.is_store_free then
946
continue
947
fi
948
949
_flow.on_call_of(interpolation.location, to_string)
950
od
951
si
952
953
// The zero-argument to_string a fragment formats through.
954
_zero_argument_to_string(member: Semantic.Symbols.Symbol?) -> Semantic.Symbols.Function? is
955
if let group: Semantic.Symbols.FUNCTION_GROUP = member then
956
for function in group.functions do
957
if !function.are_arguments_declared \/ function.arguments.count == 0 then
958
return function
959
fi
960
od
961
962
return null
963
fi
964
965
if let function: Semantic.Symbols.Function = member then
966
if !function.are_arguments_declared \/ function.arguments.count == 0 then
967
return function
968
fi
969
fi
970
971
return null
972
si
973
974
visit(`string: Trees.Expressions.Literals.STRING) is
975
_literals.visit_string(`string)
976
si
977
978
visit(character: Trees.Expressions.Literals.CHARACTER) is
979
_literals.visit_character(character)
980
si
981
982
visit(boolean: Trees.Expressions.Literals.BOOLEAN) is
983
_literals.visit_boolean(boolean)
984
si
985
986
si
987
si