Skip to content
← Back

src/ir/values/null_coalesce.ghul

1
namespace IR.Values is
2
use Semantic.Types.Type
3
4
// The result of `a ?? b` for a reference-typed `a: T?`. `T?` is a
5
// reference that may be null, so a null test is enough — `b` is
6
// evaluated only when `a` is null.
7
//
8
// Built by COMPILE_OPERATORS.visit_binary when the operator is `??`.
9
// The two operands are evaluated by their own Values; this Value
10
// composes the surrounding short-circuit shell.
11
class NULL_COALESCE: Composite is
12
left: Value
13
right: Value
14
_result_type: Type
15
16
type: Type => _result_type
17
is_lightweight_pure: bool => false
18
19
init(left: Value, right: Value, result_type: Type) is
20
super.init()
21
22
self.left = left
23
self.right = right
24
self._result_type = result_type
25
si
26
27
// `a` where it is present, and `b` only where it is not.
28
expand() -> BLOCK is
29
let expansion = IR.EXPANSION(type)
30
let result = expansion.temp("coalesce", type)
31
32
expansion.store(result, left)
33
34
let done = expansion.region()
35
let absent = expansion.region()
36
37
expansion.exit_if(absent, IR.BRANCH.Z, result.load())
38
expansion.exit(done)
39
expansion.close(absent)
40
41
expansion.store(result, right)
42
expansion.close(done)
43
44
expansion.add(result.load())
45
46
return expansion.result
47
si
48
49
to_string() -> string =>
50
"null-coalesce:[{type}]({left},{right})"
51
si
52
si