Skip to content
← Back

src/syntax/trees/expressions/val_block.ghul

1
namespace Syntax.Trees.Expressions is
2
use Source
3
4
// `val ... lav` — a value-producing block.
5
//
6
// The block's value is the LUB of every contributing source: the
7
// tail expression of `body` (if the tail provides a value) and
8
// every `return E` whose target is this block. `return E` inside
9
// a `val ... lav` exits this block (innermost wins on nesting),
10
// not the enclosing function.
11
class VAL_BLOCK: Expression, ScopeCarrier is
12
scope: Semantic.Scope? public
13
14
body: Statements.LIST public
15
16
// True when the block was written in its parenthesised
17
// spelling `(statement; ...; value)` rather than as
18
// `val ... lav`. Semantics are identical; the formatter uses
19
// this to round-trip the spelling the user wrote.
20
is_parenthesised: bool public
21
22
// A block yields a tuple literal when its tail is one and no
23
// return contributes a value of another shape.
24
is_tuple_literal: bool => body.is_tuple_literal /\ !has_targeted_return
25
26
// Set by the wrapping context (compile-expressions). False
27
// when the block is in expression-statement position or in a
28
// void-returning `=>` body — fall-through is allowed to be
29
// void there. True everywhere else.
30
want_value: bool public => block_state.want_value, = value is block_state.want_value = value si
31
32
// True where a value is wanted but a void one is acceptable —
33
// the body of a function literal whose return type is still
34
// being inferred. A body that yields no value there settles
35
// the block, and the literal's return, as void.
36
void_tolerated: bool public => block_state.void_tolerated, = value is block_state.void_tolerated = value si
37
38
// Pushed expected_type from outer context, threaded onto the
39
// tail expression and every return that targets us so they
40
// participate in inference / overload resolution against the
41
// expected type.
42
expected_type: Semantic.Types.Type? public => block_state.expected_type, = value is block_state.expected_type = value si
43
expected_type_error_message: string? public => block_state.expected_type_error_message, = value is block_state.expected_type_error_message = value si
44
45
// Collected at compile-expressions time: the value type of
46
// every return targeting this block. visit() LUBs these with
47
// the tail's value type to settle the block's value type.
48
return_types: Collections.MutableList[Semantic.Types.Type] => block_state.returns()
49
50
// True when any RETURN statement inside the body targets
51
// this block (with or without an expression). `return_types`
52
// alone misses bare `return;`; this flag is set by
53
// compile_bindings.pre_return whenever a return's target is
54
// resolved to this block.
55
has_targeted_return: bool public => block_state.has_targeted_return, = value is block_state.has_targeted_return = value si
56
57
// Label placed at end of block body in generate-il; returns
58
// with this block as target push their value and `br` here.
59
// All paths (every targeted return + the natural fall-
60
// through) converge with the value on the evaluation stack —
61
// the same shape `if`/`case`-in-expression IL uses, so async
62
// and generator state machines work without value-across-
63
// suspend special handling.
64
end_label: IR.LABEL? public => block_state.end_label, = value is block_state.end_label = value si
65
66
block_state: Syntax.Process.VAL_BLOCK_STATE field
67
68
init(location: LOCATION, body: Statements.LIST) is
69
super.init(location)
70
71
72
self.body = body
73
self.want_value = true
74
self.void_tolerated = false
75
si
76
77
clear() is
78
super.clear()
79
block_state.clear()
80
si
81
82
clear_outputs() is
83
super.clear_outputs()
84
block_state.clear_outputs()
85
si
86
87
set_expected_type(expected_type: Semantic.Types.Type?, error_message: string?) is
88
self.expected_type = expected_type
89
self.expected_type_error_message = error_message
90
91
// Push the constraint onto the tail expression so it
92
// participates in inference / overload resolution against
93
// the expected type — same shape as `if`/`case`
94
// expression-position branches. Returns targeting this
95
// block pick the constraint up from `expected_type` via
96
// compile_bindings.pre_return.
97
body.set_expected_type(expected_type, error_message)
98
si
99
100
clear_expected_type() is
101
expected_type = null
102
expected_type_error_message = null
103
body.clear_expected_type()
104
si
105
106
accept(visitor: Visitor) is
107
visitor.visit(self)
108
si
109
110
walk(visitor: Visitor) is
111
if !visitor.pre(self) then
112
body.walk(visitor)
113
fi
114
115
accept(visitor)
116
si
117
si
118
si