Skip to content
← Back

src/ir/value_converter.ghul

1
namespace IR is
2
use IO.Std
3
4
use Semantic.Types.Type
5
use NAMED_TYPE = Semantic.Types.NAMED
6
7
// How a value becomes a decimal, or stops being one. `is_identity`
8
// is the decimal-to-decimal case, where nothing has to happen;
9
// otherwise these are the types the conversion goes between, and
10
// a widening one loses nothing.
11
class DECIMAL_CONVERSION(
12
is_identity: bool,
13
is_widening: bool,
14
parameter_type: Type,
15
return_type: Type
16
)
17
18
// The decimal tables are built by complete_initialization, which
19
// every entry point calls before reading them.
20
@suppress("field-definite-assignment")
21
class VALUE_CONVERTER(
22
_logger: Logging.Logger,
23
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
24
) is
25
// The built-in scalar types, each with its kind.
26
_scalars: Collections.LIST[(type: Type, scalar: Scalar)]?
27
28
// The numeric types a decimal converts to and from directly,
29
// and those that widen to decimal without loss. Populated by
30
// complete_initialization alongside _scalars.
31
_decimal_neighbours: Collections.SET[string]
32
_decimal_implicit_sources: Collections.SET[string]
33
34
super()
35
36
complete_initialization() is
37
if _scalars? then
38
return
39
fi
40
41
_scalars = Collections.LIST[(type: Type, scalar: Scalar)]()
42
43
add_scalar(_innate_symbol_lookup.get_bool_type(), Scalar.BOOL)
44
45
add_scalar(_innate_symbol_lookup.get_ubyte_type(), Scalar.UINT8)
46
add_scalar(_innate_symbol_lookup.get_byte_type(), Scalar.INT8)
47
48
add_scalar(_innate_symbol_lookup.get_char_type(), Scalar.CHAR)
49
50
add_scalar(_innate_symbol_lookup.get_ushort_type(), Scalar.UINT16)
51
add_scalar(_innate_symbol_lookup.get_short_type(), Scalar.INT16)
52
53
add_scalar(_innate_symbol_lookup.get_uint_type(), Scalar.UINT32)
54
add_scalar(_innate_symbol_lookup.get_int_type(), Scalar.INT32)
55
56
add_scalar(_innate_symbol_lookup.get_ulong_type(), Scalar.UINT64)
57
add_scalar(_innate_symbol_lookup.get_long_type(), Scalar.INT64)
58
59
add_scalar(_innate_symbol_lookup.get_uword_type(), Scalar.UINT_NATIVE)
60
add_scalar(_innate_symbol_lookup.get_word_type(), Scalar.INT_NATIVE)
61
62
add_scalar(_innate_symbol_lookup.get_single_type(), Scalar.FLOAT32)
63
add_scalar(_innate_symbol_lookup.get_double_type(), Scalar.FLOAT64)
64
65
_decimal_neighbours = Collections.SET[string]()
66
_decimal_implicit_sources = Collections.SET[string]()
67
68
// char is omitted intentionally — Decimal's conversion
69
// overloads here are the ones taking or returning a CIL
70
// primitive, and char is not among them.
71
// System.Decimal does declare op_Implicit(char) and
72
// an op_Explicit returning char - TYPE_CASTER.find_user_defined_conversion
73
// reaches those through the general methodref machinery
74
// instead, so `cast char(d)` and `cast decimal(c)` still
75
// work even though this fast-path table doesn't cover them.
76
register_decimal_neighbour(_innate_symbol_lookup.get_byte_type(), true)
77
register_decimal_neighbour(_innate_symbol_lookup.get_ubyte_type(), true)
78
register_decimal_neighbour(_innate_symbol_lookup.get_short_type(), true)
79
register_decimal_neighbour(_innate_symbol_lookup.get_ushort_type(), true)
80
register_decimal_neighbour(_innate_symbol_lookup.get_int_type(), true)
81
register_decimal_neighbour(_innate_symbol_lookup.get_uint_type(), true)
82
register_decimal_neighbour(_innate_symbol_lookup.get_long_type(), true)
83
register_decimal_neighbour(_innate_symbol_lookup.get_ulong_type(), true)
84
register_decimal_neighbour(_innate_symbol_lookup.get_single_type(), false)
85
register_decimal_neighbour(_innate_symbol_lookup.get_double_type(), false)
86
si
87
88
add_scalar(type: Type, scalar: Scalar) is
89
_scalars!.add((type, scalar))
90
si
91
92
register_decimal_neighbour(type: Type, is_implicit_to_decimal: bool) is
93
let qn = type.symbol.qualified_name
94
_decimal_neighbours.add(qn)
95
if is_implicit_to_decimal then
96
_decimal_implicit_sources.add(qn)
97
fi
98
si
99
100
// The scalar kind of `type`, or null where it is not one of the
101
// built-in scalar types.
102
scalar(type: Type?) -> Scalar? is
103
complete_initialization()
104
105
if !type? then
106
return null
107
fi
108
109
let symbol = type.symbol
110
111
for entry in _scalars! do
112
if entry.type.symbol == symbol then
113
return entry.scalar
114
fi
115
od
116
117
return null
118
si
119
120
// How a value of `source` on the stack becomes a `target`, when
121
// at least one of them is decimal. Null when neither is, or
122
// when the pair has no `Decimal::op_Implicit`/`op_Explicit`
123
// overload.
124
//
125
// `is_identity` is the decimal-to-decimal case, which is not a
126
// call: the stack stays typed as decimal either way. Otherwise
127
// the conversion is that one static method on `System.Decimal`,
128
// described by the types it goes between rather than by the IL
129
// line that calls it — the caller needs a token for it, and a
130
// rendered call is not something a token can be read out of.
131
get_decimal_conversion(source: Type?, target: Type?) -> DECIMAL_CONVERSION? is
132
complete_initialization()
133
134
if !source? \/ !target? then
135
return null
136
fi
137
138
let decimal_type = _innate_symbol_lookup.get_decimal_type()
139
140
let source_is_decimal = source.matches(decimal_type)
141
let target_is_decimal = target.matches(decimal_type)
142
143
if !source_is_decimal /\ !target_is_decimal then
144
return null
145
fi
146
147
if source_is_decimal /\ target_is_decimal then
148
return DECIMAL_CONVERSION(true, false, decimal_type, decimal_type)
149
fi
150
151
if target_is_decimal then
152
if !_decimal_neighbours.contains(source.symbol.qualified_name) then
153
return null
154
fi
155
156
return DECIMAL_CONVERSION(
157
false,
158
_decimal_implicit_sources.contains(source.symbol.qualified_name),
159
source,
160
decimal_type)
161
fi
162
163
if !_decimal_neighbours.contains(target.symbol.qualified_name) then
164
return null
165
fi
166
167
return DECIMAL_CONVERSION(false, false, decimal_type, target)
168
si
169
170
si
171
si