Skip to content
← Back

src/ir/value_boxer.ghul

1
namespace IR is
2
use Semantic.Types.Type
3
4
use IR.Values.Value
5
6
class VALUE_BOXER(_logger: Logging.Logger) is
7
want_boxing: bool public
8
9
super()
10
11
box_if_value(value: Value) -> Value =>
12
if want_boxing /\ value.is_value_type then
13
Values.BOX(value)
14
else
15
value
16
fi
17
18
box_if_needed(value: Value, target_type: Type) -> Value is
19
// Try the T → T? wrap first — covers the value-type
20
// optional widening (Nullable<T>::.ctor at the slot
21
// boundary). The target stays a value type, so no
22
// subsequent BOX fires.
23
let wrapped = wrap_if_needed(value, target_type)!
24
25
if wrapped != value then
26
return wrapped
27
fi
28
29
if want_boxing /\ value.is_value_type /\ !target_type.is_value_type then
30
return Values.BOX(value)
31
else
32
return value
33
fi
34
si
35
36
// Implicit T → T? widening at slot boundaries. A non-optional
37
// value-type T flowing into a value-type optional target gets
38
// wrapped via Nullable<T>::.ctor — the value-type analogue of
39
// the box-on-assignment-to-object path. Reference T → T? needs
40
// no wrap (the bits are identical, only the type-system flag
41
// differs) so this helper only fires when the target is a
42
// value-type optional.
43
wrap_if_needed(value: Value?, target_type: Type?) -> Value? is
44
if !value? \/ !target_type? \/ !value.type? then
45
return value
46
fi
47
48
if let repacked = _repack_tuple(value, target_type) then
49
return repacked
50
fi
51
52
if !value.type? then
53
return value
54
fi
55
56
// MAYBE[T] → reference T?: `MAYBE[T].value` throws when
57
// absent (it is a checked unwrap in its own right), so the
58
// coercion has to test `has_value` first and only read
59
// `value` on the present path — mirroring the `??`
60
// value-shape lowering in COMPILE_OPERATORS. The absent
61
// arm is a bare `null`: a reference T? represents absence
62
// that way, with no exception involved.
63
if
64
value.type.is_maybe /\
65
target_type.is_optional /\
66
!target_type.is_value_type
67
then
68
let has_value_member = value.type!.find_member("has_value")
69
let value_member = value.type!.find_member("value")
70
71
if has_value_member? /\ value_member? then
72
let symbol_loader = IoC.CONTAINER.instance.symbol_loader
73
let address_stand_in = Values.STACK_TOP_ADDRESS(value.type!)
74
75
let presence_test = has_value_member.load(value.location, address_stand_in, symbol_loader)
76
let value_extract = value_member.load(value.location, address_stand_in, symbol_loader)
77
78
// The payload reaches a reference target, so a
79
// value-type payload — or a type-variable one that
80
// may instantiate as a value type — arrives boxed;
81
// the extract alone would leave a bare struct on
82
// the stack where the slot expects a reference.
83
let payload_type = value_extract.type
84
85
let present_arm: Values.Value =
86
if payload_type? /\ (payload_type.is_value_type \/ payload_type.is_type_variable) then
87
Values.BOX(Values.STACK_TOP(payload_type))
88
else
89
Values.STACK_TOP(target_type)
90
fi
91
92
return Values.NULL_COALESCE_VALUE(
93
value,
94
presence_test,
95
value_extract,
96
present_arm,
97
Values.DEFAULT(target_type),
98
target_type
99
)
100
fi
101
fi
102
103
if !target_type.is_value_type \/ !target_type.is_optional then
104
return value
105
fi
106
107
// A bare `null` flowing into a value-type optional slot is the
108
// empty optional — a zeroed value, not a null reference. Lower
109
// it like `default`, mirroring the `null`-into-NULLABLE path in
110
// compile_expressions. The constraint that would otherwise drive
111
// that lowering does not reach a call argument, so the coercion
112
// happens here at the boxing boundary instead.
113
if value.type!.is_null then
114
return Values.DEFAULT(target_type)
115
fi
116
117
// MAYBE[T] → value-type T?: the same pair of carriers the other
118
// way round. Each is its own type, so the payload is read out
119
// on the present path and wrapped in the target's carrier.
120
if value.type!.is_maybe /\ !target_type.is_maybe then
121
let has_value_member = value.type!.find_member("has_value")
122
let value_member = value.type!.find_member("value")
123
124
if has_value_member? /\ value_member? then
125
let symbol_loader = IoC.CONTAINER.instance.symbol_loader
126
let address_stand_in = Values.STACK_TOP_ADDRESS(value.type!)
127
128
let presence_test = has_value_member.load(value.location, address_stand_in, symbol_loader)
129
let value_extract = value_member.load(value.location, address_stand_in, symbol_loader)
130
131
return Values.NULL_COALESCE_VALUE(
132
value,
133
presence_test,
134
value_extract,
135
Values.WRAP_OPTIONAL(target_type, Values.STACK_TOP(value_extract.type!)),
136
Values.DEFAULT(target_type),
137
target_type
138
)
139
fi
140
fi
141
142
// Any other `T?` lowering flowing into a MAYBE[T] slot.
143
// The three optional carriers are intercompatible in the
144
// type system but distinct CLR types, so the coercion is
145
// explicit: test the source for presence, and either wrap
146
// its payload with MAYBE<T>::.ctor or leave the empty
147
// MAYBE. A reference source needs no local — the null test
148
// is a dup-and-branch on the value itself.
149
if target_type.is_maybe /\ !target_type.is_equivalent_to(value.type!) then
150
if value.type!.is_optional /\ !value.type!.is_value_type then
151
return Values.WRAP_MAYBE(target_type, value)
152
fi
153
154
if value.type!.is_optional then
155
let has_value_member = value.type!.find_member("has_value")
156
let value_member = value.type!.find_member("value")
157
158
if has_value_member? /\ value_member? then
159
let symbol_loader = IoC.CONTAINER.instance.symbol_loader
160
let address_stand_in = Values.STACK_TOP_ADDRESS(value.type!)
161
162
let presence_test = has_value_member.load(value.location, address_stand_in, symbol_loader)
163
let value_extract = value_member.load(value.location, address_stand_in, symbol_loader)
164
165
return Values.NULL_COALESCE_VALUE(
166
value,
167
presence_test,
168
value_extract,
169
Values.WRAP_OPTIONAL(target_type, Values.STACK_TOP(value_extract.type!)),
170
Values.DEFAULT(target_type),
171
target_type
172
)
173
fi
174
fi
175
fi
176
177
if value.type!.is_optional then
178
return value
179
fi
180
181
// Value-type optional — source-side NULLABLE or a reflected
182
// Nullable<T> wrapper; both are GENERICs carrying the inner
183
// T as their single argument.
184
if isa Semantic.Types.GENERIC(target_type) then
185
let optional = target_type
186
187
if optional.arguments.count != 1 then
188
return value
189
fi
190
191
if !optional.arguments[0].is_assignable_from(value.type!) then
192
return value
193
fi
194
195
return Values.WRAP_OPTIONAL(target_type, value)
196
fi
197
198
return value
199
si
200
201
// A tuple is assignable to a tuple type whose elements each
202
// accept its own, but the CLR type is invariant: the target is
203
// rebuilt from the source's elements, each converted in turn.
204
_repack_tuple(value: Value, target_type: Type) -> Value? is
205
if
206
let source = value.type /\
207
(source.is_value_tuple \/ isa Semantic.Types.TUPLE(source)) /\
208
(target_type.is_value_tuple \/ isa Semantic.Types.TUPLE(target_type))
209
then
210
let target = target_type
211
212
if
213
source.is_optional \/
214
target.is_optional \/
215
source.arguments.count != target.arguments.count \/
216
target.is_equivalent_to(source)
217
then
218
return null
219
fi
220
221
// A tuple literal is built at the target type directly from its
222
// own element values. Spilling it first would build the source
223
// tuple, whose element types - a bare `null`, say - may have no
224
// runtime representation.
225
if let rebuilt = _rebuild_tuple_literal(value, target) then
226
return rebuilt
227
fi
228
229
let symbol_loader = IoC.CONTAINER.instance.symbol_loader
230
let temp = LOCAL(".repack.{IR.TEMP.get_next_id()}", source)
231
let elements = Collections.LIST[Value]()
232
233
for i in 0..source.arguments.count do
234
let member = source.find_destructure_member(i)
235
236
if !member? then
237
return null
238
fi
239
240
let element = member.load(value.location, Values.Load.TEMP(temp), symbol_loader)
241
242
elements.add(box_if_needed(element, target.arguments[i]))
243
od
244
245
return Values.TUPLE_REPACK(value, temp, Values.TUPLE(target, elements))
246
fi
247
248
return null
249
si
250
251
// `value` rebuilt at `target` when it is a tuple literal, or a block
252
// whose result is one, as an `if` or `case` arm is; null otherwise.
253
_rebuild_tuple_literal(value: Value, target: Type) -> Value? is
254
if let literal: Values.TUPLE = value then
255
if literal.values.count != target.arguments.count then
256
return null
257
fi
258
259
let converted = Collections.LIST[Value]()
260
261
for i in 0..literal.values.count do
262
converted.add(box_if_needed(literal.values[i], target.arguments[i]))
263
od
264
265
return Values.TUPLE(target, converted)
266
fi
267
268
if let block: Values.BLOCK = value, result = block.result_value then
269
if let rebuilt_result = _rebuild_tuple_literal(result, target) then
270
let rebuilt = Values.BLOCK(target)
271
272
for i in 0..block.values.count - 1 do
273
rebuilt.add(block.values[i])
274
od
275
276
rebuilt.add(rebuilt_result)
277
278
return rebuilt
279
fi
280
fi
281
282
return null
283
si
284
285
// Whether `value` is a value type passed where `target_type` is a
286
// reference. An argument that an earlier error left without a type
287
// is passed as it is.
288
_needs_box(value: Value, target_type: Type?) -> bool =>
289
want_boxing /\ target_type? /\ !target_type.is_value_type /\ (value.type?.is_value_type ?? false)
290
291
box_arguments(arguments: Collections.List[Value], argument_types: Collections.List[Type]) -> Collections.List[Value] is
292
if arguments.count != argument_types.count then
293
_logger.poison(Source.LOCATION.internal, "boxed incomplete arguments")
294
return arguments
295
fi
296
297
let any_need_change mut = false
298
299
for i in 0..arguments.count do
300
301
// Apply the wrap first — a value-typed source that
302
// coerces to a reference-typed target (e.g. MAYBE[T] →
303
// T?) emerges from `wrap_if_needed` as the .value
304
// reference, so the boxing branch below must see the
305
// already-coerced value or it boxes the wrong thing.
306
let wrapped = wrap_if_needed(arguments[i], argument_types[i])!
307
308
if wrapped != arguments[i] then
309
any_need_change = true
310
break
311
fi
312
313
if _needs_box(wrapped, argument_types[i]) then
314
any_need_change = true
315
break
316
fi
317
od
318
319
if !any_need_change then
320
return arguments
321
fi
322
323
let result = Collections.LIST[Value](arguments.count)
324
325
for i in 0..arguments.count do
326
let wrapped = wrap_if_needed(arguments[i], argument_types[i])!
327
328
if _needs_box(wrapped, argument_types[i]) then
329
result.add(Values.BOX(wrapped))
330
else
331
result.add(wrapped)
332
fi
333
od
334
335
return result
336
si
337
si
338
si