Skip to content
← Back

src/ir/values/literal/number.ghul

1
namespace IR.Values.Literal is
2
use TypeTyped = Semantic.Types.Typed
3
use Semantic.Types.Type
4
5
// The constant a numeric literal loads, at the width it is loaded at.
6
//
7
// An integer is held at the width its load asks for rather than as a
8
// magnitude, because the two are not the same: `0xFF00FF00` is a
9
// `uint` in range, and the constant `ldc.i4` takes is the 32-bit
10
// pattern, which read as a magnitude is negative.
11
union CONSTANT is
12
I4(value: int)
13
I8(value: long)
14
R4(value: single)
15
R8(value: double)
16
si
17
18
// A numeric constant, held as the value rather than as the way it was
19
// written down. A source literal can be written in any radix and with
20
// any suffix, so the spelling and the value are not the same thing,
21
// and reading that spelling back is not something emission should
22
// have to do.
23
class NUMBER: Value, TypeTyped is
24
constant: CONSTANT
25
type: Type
26
27
// The scalar the loaded constant is converted to, where the
28
// width it is loaded at is not the literal's declared type.
29
narrowing: IR.Scalar?
30
31
is_lightweight_pure: bool => true
32
33
is_real: bool =>
34
case constant
35
when _: CONSTANT.R4 then true
36
when _: CONSTANT.R8 then true
37
else false
38
esac
39
40
// The constant written out, used to render an attribute
41
// argument and as a key for comparing case-arm constants.
42
rendered: string =>
43
case constant
44
when i4: CONSTANT.I4 then "{i4.value}"
45
when i8: CONSTANT.I8 then "{i8.value}"
46
when r4: CONSTANT.R4 then "{r4.value}"
47
when r8: CONSTANT.R8 then "{r8.value}"
48
esac
49
50
init(constant: CONSTANT, type: Type, narrowing: IR.Scalar?) is
51
super.init()
52
53
self.constant = constant
54
self.type = type
55
self.narrowing = narrowing
56
si
57
58
init(constant: CONSTANT, type: Type) is
59
self.init(constant, type, null)
60
si
61
62
init(value: int, type: Type) is
63
self.init(CONSTANT.I4(value), type, null)
64
si
65
66
init(value: long, type: Type) is
67
self.init(CONSTANT.I8(value), type, null)
68
si
69
70
init(value: single, type: Type) is
71
self.init(CONSTANT.R4(value), type, null)
72
si
73
74
init(value: double, type: Type) is
75
self.init(CONSTANT.R8(value), type, null)
76
si
77
78
accept(visitor: Visitor) is
79
visitor.visit(self)
80
si
81
82
to_string() -> string =>
83
"literal:[{type}]({constant})"
84
si
85
si