Skip to content
← Back

src/ir/values/wrap_optional.ghul

1
namespace IR.Values is
2
use Semantic.Types.Type
3
4
// Wrap a value-type T in its value-type optional form NULLABLE[T] (=
5
// System.Nullable[T]). Emits the Nullable<T>::.ctor(T) call IL.
6
// Counterpart to BOX (T → object) at value-meets-slot boundaries —
7
// used by VALUE_BOXER.wrap_if_needed when an implicit conversion
8
// from T to T? is required.
9
class WRAP_OPTIONAL: Value is
10
value: Value
11
_optional_type: Type
12
type: Type => _optional_type
13
is_value_type: bool => true
14
15
init(
16
optional_type: Type,
17
value: Value
18
) is
19
super.init()
20
21
self._optional_type = optional_type
22
self.value = value
23
si
24
25
gen(context: IR.CONTEXT) is
26
gen(value, context)
27
28
let body = context.current_srm_body_emitter!
29
body.new_object(context.resolve_generic_constructor(_optional_type, 1))
30
si
31
32
to_string() -> string =>
33
"wrap-optional:[{_optional_type}]({value})"
34
si
35
si