Skip to content
← Back

src/ir/values/new.ghul

1
namespace IR.Values is
2
use Ghul.Pipes;
3
4
use Semantic.Types.Type;
5
6
class NEW: Value is
7
// Constructing an object writes only that new object's own
8
// state, which no caller can hold a narrowing fact about. The
9
// construction is state-changing only when the constructor can
10
// write a pre-existing slot reachable through an argument or a
11
// static — its construction-store-free classification, which
12
// trusts writes to the fresh receiver's own fields that the
13
// strict store-free bit (used for `obj.init(...)`) does not.
14
is_state_changing_call: bool => !constructor.constructs_store_free;
15
type: Type;
16
constructor: Semantic.Symbols.Function;
17
arguments: Collections.List[Value];
18
19
init(
20
type: Type,
21
constructor: Semantic.Symbols.Function,
22
arguments: Collections.List[Value]
23
) is
24
super.init();
25
26
self.type = type;
27
self.constructor = constructor;
28
self.arguments = arguments;
29
si
30
31
gen(context: IR.CONTEXT) is
32
let owner = constructor.owner!;
33
34
if owner.is_unit_variant then
35
let sig_buffer = System.Text.StringBuilder();
36
owner.unspecialized_symbol!.gen_reference(sig_buffer);
37
let field_sig = sig_buffer.to_string();
38
39
let container_buffer = System.Text.StringBuilder();
40
owner.gen_reference(container_buffer);
41
let container_ref = container_buffer.to_string();
42
43
context.write_line("ldsfld {field_sig}{container_ref}::'_instance'");
44
45
return;
46
fi
47
48
for a in arguments do
49
gen(a, context);
50
od
51
52
context.write_line("newobj {constructor.get_il_reference()}");
53
si
54
55
to_string() -> string =>
56
"new:[{type}](\"{constructor.name}\",{arguments |> join(",")})";
57
si
58
si