Skip to content
← Back

src/semantic/type_caster.ghul

1
namespace Semantic is
2
use IR.Scalar
3
use IO.Std
4
5
use Logging
6
7
use Types.Type
8
9
use IR.Values
10
11
use IR.TEMP
12
use IR.BRANCH
13
14
class TYPE_CASTER(
15
_block_context: IR.BlockContext,
16
_value_boxer: IR.VALUE_BOXER,
17
_value_converter: IR.VALUE_CONVERTER,
18
_innate_symbol_lookup: Lookups.InnateSymbolLookup,
19
_conversion_lookup: USER_DEFINED_CONVERSION_LOOKUP,
20
_logger: Logger
21
) is
22
_bridge: POINTER_SIZED_DECIMAL_BRIDGE
23
24
super()
25
26
init(..) is
27
_bridge = POINTER_SIZED_DECIMAL_BRIDGE(_innate_symbol_lookup)
28
si
29
30
check_cast_is_valid(location: Source.LOCATION, source_type: Type, target_type: Type) -> bool is
31
if source_type.is_type_variable /\ target_type.is_type_variable then
32
_logger.error(location, "cannot convert {source_type} to {target_type}")
33
34
return false
35
fi
36
37
if source_type.is_type_variable \/ target_type.is_type_variable then
38
return true
39
fi
40
41
if source_type.is_value_type /\ target_type.is_value_type then
42
let decimal_type = _innate_symbol_lookup.get_decimal_type()
43
44
// Reached in two steps - see the matching branch in
45
// cast_value.
46
if _bridge.find(source_type, target_type)? then
47
return true
48
fi
49
50
if source_type.matches(decimal_type) \/ target_type.matches(decimal_type) then
51
if
52
!_value_converter.get_decimal_conversion(source_type, target_type)? /\
53
!_conversion_lookup.find(source_type, target_type)?
54
then
55
_logger.error(location, "cannot convert {source_type} to {target_type}")
56
return false
57
fi
58
59
return true
60
fi
61
62
let enum_type = _innate_symbol_lookup.get_enum_type()
63
64
if _value_converter.scalar(target_type)? then
65
// A built-in conversion only accepts a source in a
66
// compatible primitive shape - one of the built-in
67
// scalars, or an enum (which decays to its underlying
68
// integral type on the stack per ECMA-335). A
69
// multi-field value type like Half or Int128 is
70
// neither, so a scalar target alone isn't enough here.
71
if
72
!_value_converter.scalar(source_type)? /\
73
!enum_type.is_assignable_from(source_type) /\
74
!_conversion_lookup.find(source_type, target_type)?
75
then
76
_logger.error(location, "cannot convert {source_type} to {target_type}")
77
return false
78
fi
79
elif
80
// Only a target enum is accepted here, matching
81
// cast_value below: `conv.i4` re-annotates the
82
// stack value's type as `target_type`, which is
83
// only sound when that target itself decays to
84
// int32 the same way a source enum does. An enum
85
// source with some other non-scalar target is the
86
// same unsound-IL shape the check above rejects
87
// for a struct source.
88
!enum_type.is_assignable_from(target_type) /\
89
!_conversion_lookup.find(source_type, target_type)?
90
then
91
_logger.error(location, "cannot convert {source_type} to {target_type}")
92
return false
93
fi
94
fi
95
96
return true
97
si
98
99
// `is_checked` says what a failed cast should do: fault, which is
100
// what the program asked for by writing `cast T(v)`, or decline,
101
// which is what a refutable test and the compiler's own coercions
102
// want. See IR.Values.CAST.
103
cast_value(
104
location: Source.LOCATION,
105
value: IR.Values.Value,
106
target_type: Type,
107
quiet: bool,
108
is_checked: bool
109
) -> IR.Values.Value is
110
let need_conditional_unbox = false
111
112
if value.type!.is_type_variable /\ target_type.is_type_variable then
113
if !quiet then
114
_logger.error(location, "cannot convert {value.type} to {target_type}")
115
fi
116
117
return
118
DUMMY(
119
target_type,
120
location
121
)
122
fi
123
124
if value.type!.is_type_variable \/ target_type.is_type_variable then
125
let result: IR.Values.Value mut =
126
CAST(
127
target_type,
128
value,
129
is_checked
130
)
131
132
if target_type.is_value_type then
133
result = _conditionally_unbox(result, target_type)
134
fi
135
136
return result
137
fi
138
139
if value.type!.is_value_type /\ target_type.is_value_type then
140
let decimal_type = _innate_symbol_lookup.get_decimal_type()
141
142
// System.Decimal declares a conversion for each of the
143
// fixed-width integers but none for a pointer-sized one,
144
// so that pair goes through the 64-bit integer of the
145
// same signedness, which it does declare.
146
if let bridge = _bridge.find(value.type!, target_type) then
147
return
148
cast_value(
149
location,
150
cast_value(location, value, bridge, quiet, is_checked),
151
target_type,
152
quiet,
153
is_checked
154
)
155
fi
156
157
if value.type!.matches(decimal_type) \/ target_type.matches(decimal_type) then
158
let decimal_conversion = _value_converter.get_decimal_conversion(value.type!, target_type)
159
160
if !decimal_conversion? then
161
let conversion_function = _conversion_lookup.find(value.type!, target_type)
162
163
if conversion_function? then
164
return _build_conversion_call(value, conversion_function, target_type)
165
fi
166
167
if !quiet then
168
_logger.error(location, "cannot convert {value.type} to {target_type}")
169
fi
170
171
return
172
DUMMY(
173
target_type,
174
location
175
)
176
fi
177
178
// Decimal to decimal is not a conversion at all;
179
// the value on the stack is already what it needs
180
// to be.
181
if decimal_conversion.is_identity then
182
return value
183
fi
184
185
return
186
IR.Values.DECIMAL_CONVERT(
187
target_type,
188
value,
189
value.type!
190
)
191
fi
192
193
let target_scalar = _value_converter.scalar(target_type)
194
let source_scalar = _value_converter.scalar(value.type!)
195
let enum_type = _innate_symbol_lookup.get_enum_type()
196
197
if let to = target_scalar then
198
// See the matching comment in check_cast_is_valid: a
199
// scalar target says nothing about whether `value`
200
// can be converted by a built-in conversion. A
201
// multi-field value type like Half or Int128 can't,
202
// and reaching the JIT with one gives an
203
// InvalidProgramException rather than a compile
204
// error.
205
if source_scalar? \/ enum_type.is_assignable_from(value.type!) then
206
return CONVERT(target_type, value, source_scalar, to)
207
fi
208
elif enum_type.is_assignable_from(target_type) then
209
// An enum target decays to int32 the way an enum
210
// source does.
211
return CONVERT(target_type, value, source_scalar, Scalar.INT32)
212
fi
213
214
let conversion_function = _conversion_lookup.find(value.type!, target_type)
215
216
if conversion_function? then
217
return _build_conversion_call(value, conversion_function, target_type)
218
fi
219
220
if !quiet then
221
_logger.error(location, "cannot convert {value.type} to {target_type}")
222
fi
223
224
return
225
DUMMY(
226
target_type,
227
location
228
)
229
fi
230
231
let target_non_optional = target_type.optional_inner_type ?? target_type
232
233
if
234
!target_non_optional.is_assignable_from(value.type!) /\
235
!value.type!.is_assignable_from(target_non_optional)
236
then
237
let conversion_function = _conversion_lookup.find(value.type!, target_type)
238
239
if conversion_function? then
240
return _build_conversion_call(value, conversion_function, target_type)
241
fi
242
fi
243
244
let result: Value mut =
245
CAST(
246
target_type,
247
value,
248
is_checked
249
)
250
251
if !value.type!.is_value_type /\ target_type.is_value_type then
252
result = _conditionally_unbox(result, target_type)
253
fi
254
255
return result
256
si
257
258
// Public so compile_expressions can exclude a cast covered by a
259
// user-defined conversion from the impossible-cast warning.
260
find_user_defined_conversion(source_type: Type, target_type: Type) -> Symbols.Function? =>
261
_conversion_lookup.find(source_type, target_type)
262
263
_build_conversion_call(value: Value, function: Symbols.Function, target_type: Type) -> Value is
264
let arguments = Collections.LIST[Value]()
265
arguments.add(value)
266
267
let call: Value =
268
Call.STATIC(
269
function,
270
_value_boxer.box_arguments(arguments, function.arguments),
271
function.return_type,
272
null
273
)
274
275
if !target_type.is_optional then
276
return call
277
fi
278
279
return _guard_against_conversion_failure(call, target_type)
280
si
281
282
// A user-defined conversion is arbitrary code and can throw;
283
// `cast T?(...)` promises never to, for the two failure kinds
284
// the C# conversion-operator guidelines specify. Any other
285
// exception is a bug and propagates.
286
_guard_against_conversion_failure(call: Value, target_type: Type) -> Value is
287
let block = _block_context.enter_block(target_type)
288
let void_type = _innate_symbol_lookup.get_void_type()
289
290
let result = TEMP(block, "user_convert", target_type)
291
292
let `try = IR.Values.TRY(IR.LABEL(), void_type)
293
294
block.add(`try)
295
296
block.open_body(`try.body)
297
result.store(_value_boxer.box_if_needed(call, target_type))
298
block.add(IR.Values.BREAK(`try))
299
block.close_body(`try.body)
300
301
for exception_type in [
302
_innate_symbol_lookup.get_invalid_cast_exception_type(),
303
_innate_symbol_lookup.get_overflow_exception_type()
304
] do
305
let handler = IR.Values.CATCH(exception_type, void_type)
306
307
`try.catches.add(handler)
308
309
block.open_body(handler.body)
310
block.add(IR.Values.DISCARD())
311
result.store(IR.Values.DEFAULT(target_type))
312
block.add(IR.Values.BREAK(`try))
313
block.close_body(handler.body)
314
od
315
316
`try.close()
317
318
block.add(result.load())
319
320
_block_context.leave_block()
321
322
return block
323
si
324
325
_conditionally_unbox(
326
value: IR.Values.Value,
327
target_type: Types.Type
328
) -> IR.Values.Value
329
is
330
// value is the result of a cast instruction ('isinsnt'). we now need
331
// to check if the result is not null before we attempt to unbox, or
332
// if the result is null, we return the value type's default value instead
333
334
let block = _block_context.enter_block(target_type)
335
let void_type = _innate_symbol_lookup.get_void_type()
336
337
let temp = TYPE_WRAPPER(_innate_symbol_lookup.get_object_type(), value).get_temp_copier(block, "cast")
338
339
let done = IR.Values.REGION(IR.LABEL(), void_type)
340
let present = IR.Values.REGION(IR.LABEL(), void_type)
341
342
block.add(done)
343
block.open_body(done.body)
344
block.add(present)
345
done.body.open_body(present.body)
346
347
// is the `isinst` result null?
348
block.add(IR.Values.BREAK(present, BRANCH.Z, temp()))
349
350
// `isinst` result is not null, so safe to unbox:
351
block.add(
352
UNBOX(
353
TYPE_WRAPPER(target_type, temp())
354
)
355
)
356
357
block.add(IR.Values.BREAK(done))
358
359
done.body.close_body(present.body)
360
present.close()
361
362
// 'isinst' result is null, so attempting unbox would throw
363
// NullReferenceException. return default value instead:
364
block.add(IR.Values.DEFAULT(target_type))
365
366
block.close_body(done.body)
367
done.close()
368
369
_block_context.leave_block()
370
371
return block
372
si
373
si
374
si