Skip to content
← Back

src/syntax/process/numeric_literal_classifier.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Source.LOCATION
4
5
use Semantic.Lookups.InnateSymbolLookup
6
use Semantic.Types.Type
7
8
use IR.Values
9
10
// Classify a parsed integer literal: pick the target type from
11
// any trailing type-suffix (`b`/`s`/`i`/`l`/`w`/`c`, optionally
12
// prefixed by `u` for unsigned or `s` for sign-without-width),
13
// strip the suffix from the value string, and range-check the
14
// result against the chosen type. The lexer accepts arbitrarily
15
// long digit strings and any suffix combination; this is where
16
// suffix → type and value → fits-in-type get decided.
17
//
18
// Bounds errors and "character literals cannot be unsigned"
19
// structural errors are logged via the constructor-supplied
20
// logger. Callers receive a non-null `Literal.NUMBER` on success
21
// and null on either kind of failure.
22
//
23
// The `fits` / `parse_hex` / `max_unsigned_for` helpers are
24
// exposed for unit testing in isolation from the suffix path.
25
class NUMERIC_LITERAL_CLASSIFIER is
26
_logger: Logger
27
_innate_symbol_lookup: InnateSymbolLookup
28
29
init(logger: Logger, innate_symbol_lookup: InnateSymbolLookup) is
30
super.init()
31
32
_logger = logger
33
_innate_symbol_lookup = innate_symbol_lookup
34
si
35
36
// How many trailing characters of an integer literal are its
37
// type suffix.
38
//
39
// The digits are scanned maximally in the literal's own radix,
40
// and the suffix is whatever is left. That is what keeps `b`
41
// and `c` - hex digits as well as size selectors - reading as
42
// digits in a hex literal, so `0x20AC` is 8364 rather than
43
// 0x20A as a char. A backtick attaches a suffix that would
44
// otherwise be read as a digit, and is counted as part of the
45
// suffix so that stripping it yields the value.
46
//
47
// Every reader of a given token has to resolve this the same
48
// way, so it is the one place the rule lives.
49
suffix_length(value_string: string) -> int static is
50
if value_string.length == 0 then
51
return 0
52
fi
53
54
let tick = value_string.index_of('`')
55
56
if tick >= 0 then
57
return value_string.length - tick
58
fi
59
60
// A sign belongs to the value rather than to either end of
61
// it, and a caller may hand the text over with one still
62
// attached.
63
let signed = value_string[0] == '-' \/ value_string[0] == '+'
64
let start = if signed then 1 else 0 fi
65
66
let is_hex =
67
value_string.length > start + 1 /\
68
value_string[start] == '0' /\
69
(value_string[start + 1] == 'x' \/ value_string[start + 1] == 'X')
70
71
let first_digit = if is_hex then start + 2 else start fi
72
let index mut = first_digit
73
74
while index < value_string.length /\ is_digit(value_string[index], is_hex) do
75
index = index + 1
76
od
77
78
return value_string.length - index
79
si
80
81
// Whether `c` is a digit of the literal's radix. `_` never
82
// reaches here - the lexer drops digit-group separators before
83
// the token text is built.
84
is_digit(c: char, is_hex: bool) -> bool static =>
85
(c >= '0' /\ c <= '9') \/
86
(is_hex /\ ((c >= 'a' /\ c <= 'f') \/ (c >= 'A' /\ c <= 'F')))
87
88
// The literal with its type suffix removed, which is the value
89
// it denotes and the string every back end should read.
90
strip_suffix(value_string: string) -> string static =>
91
value_string.substring(0, value_string.length - suffix_length(value_string))
92
93
// The value an integer literal denotes. Every scalar suffix
94
// yields a `Literal.NUMBER`; `n` yields a `Literal.BIGINT`,
95
// which has no constant to load and is built at run time.
96
classify(location: LOCATION, value_string: string) -> Value? =>
97
if is_bigint(value_string) then
98
classify_bigint(location, value_string)
99
else
100
classify_integer(location, value_string)
101
fi
102
103
// Whether the literal carries the `n` suffix. The size selector
104
// is always the last character of the suffix, so this is the
105
// whole of the test.
106
is_bigint(value_string: string) -> bool static is
107
if suffix_length(value_string) == 0 then
108
return false
109
fi
110
111
let last = value_string[value_string.length - 1]
112
113
return last == 'n' \/ last == 'N'
114
si
115
116
// The selectors of a suffixed literal, with any attaching
117
// backtick dropped - it marks the boundary and says nothing
118
// about the type. Empty when the backtick had nothing after it,
119
// which the lexer has already reported.
120
selectors(value_string: string) -> string static is
121
let cutoff = value_string.length - suffix_length(value_string)
122
123
if value_string[cutoff] == '`' then
124
return value_string.substring(cutoff + 1, value_string.length - cutoff - 1)
125
fi
126
127
return value_string.substring(cutoff, value_string.length - cutoff)
128
si
129
130
classify_bigint(location: LOCATION, value_string: string) -> Literal.BIGINT? is
131
let selected = selectors(value_string)
132
133
if selected.length == 0 then
134
return null
135
fi
136
137
if "uU".contains(selected[0]) then
138
_logger.error(location, "bigint literal cannot be unsigned")
139
return null
140
fi
141
142
let working = strip_suffix(value_string)
143
144
let (ok, digits) = decimal_magnitude(working)
145
146
if !ok then
147
_logger.error(location, "invalid bigint literal {working}")
148
return null
149
fi
150
151
let as_long: long mut = 0l
152
let fits_long = fits_in_long(digits) /\ long.try_parse(digits, as_long ref)
153
154
return Literal.BIGINT(digits, _innate_symbol_lookup.get_bigint_type(), as_long, fits_long)
155
si
156
157
// Largest magnitude a `long` holds, as the digit string
158
// `fits_in_long` compares against.
159
LONG_MAX_DIGITS: string static => "9223372036854775807"
160
161
// A bigint literal has no width to overflow, so its magnitude is
162
// carried as digits rather than range-checked. `long` is only
163
// the shorter of the two ways to build one at run time, so
164
// whether the value fits is a question about which instructions
165
// to emit rather than about whether the literal is legal.
166
fits_in_long(digits: string) -> bool =>
167
digits.length < LONG_MAX_DIGITS.length \/
168
(digits.length == LONG_MAX_DIGITS.length /\ string.compare_ordinal(digits, LONG_MAX_DIGITS) <= 0)
169
170
// The literal's magnitude in decimal digits, with no leading
171
// zeros. A decimal literal already is that, once its zeros are
172
// dropped; a hex one is accumulated a digit at a time, because
173
// the value has no bound to parse it into.
174
decimal_magnitude(value_string: string) -> (ok: bool, digits: string) is
175
let is_hex = value_string.starts_with("0x") \/ value_string.starts_with("0X")
176
let source = if is_hex then value_string.substring(2, value_string.length - 2) else value_string fi
177
178
if source.length == 0 then
179
return (false, "0")
180
fi
181
182
// Least significant first, one decimal digit each, so a
183
// carry appends rather than shifting the whole number.
184
let accumulator = Collections.LIST[int]([0])
185
let radix = if is_hex then 16 else 10 fi
186
187
for c in source do
188
let digit: int mut
189
190
if c >= '0' /\ c <= '9' then
191
digit = cast int(c) - cast int('0')
192
elif is_hex /\ c >= 'a' /\ c <= 'f' then
193
digit = cast int(c) - cast int('a') + 10
194
elif is_hex /\ c >= 'A' /\ c <= 'F' then
195
digit = cast int(c) - cast int('A') + 10
196
else
197
return (false, "0")
198
fi
199
200
let carry mut = digit
201
202
for i in 0..accumulator.count do
203
let product = accumulator[i] * radix + carry
204
accumulator[i] = product % 10
205
carry = product / 10
206
od
207
208
while carry > 0 do
209
accumulator.add(carry % 10)
210
carry = carry / 10
211
od
212
od
213
214
let buffer = System.Text.StringBuilder(accumulator.count)
215
let top mut = accumulator.count - 1
216
217
while top > 0 /\ accumulator[top] == 0 do
218
top = top - 1
219
od
220
221
for i in 0::top do
222
buffer.append(cast char(cast int('0') + accumulator[top - i]))
223
od
224
225
return (true, buffer.to_string())
226
si
227
228
classify_integer(location: LOCATION, value_string: string) -> Literal.NUMBER? is
229
let type: Type? mut = null
230
let is_wide mut = false
231
let narrowing: IR.Scalar? mut = null
232
let working mut = value_string
233
234
let suffix = suffix_length(working)
235
236
if suffix > 0 then
237
let cutoff = working.length - suffix
238
239
// The selectors themselves, with any attaching backtick
240
// dropped - it marks the boundary and says nothing about
241
// the type.
242
let selectors =
243
if working[cutoff] == '`' then
244
working.substring(cutoff + 1, suffix - 1)
245
else
246
working.substring(cutoff, suffix)
247
fi
248
249
// A backtick with nothing after it. The lexer has already
250
// reported it; failing quietly here keeps it to one
251
// diagnostic.
252
if selectors.length == 0 then
253
return null
254
fi
255
256
let last_char = selectors[selectors.length - 1]
257
let is_unsigned = "uU".contains(selectors[0])
258
259
case last_char
260
when 'b', 'B' then
261
if is_unsigned then
262
type = _innate_symbol_lookup.get_ubyte_type()
263
else
264
type = _innate_symbol_lookup.get_byte_type()
265
fi
266
267
when 'c', 'C' then
268
if is_unsigned then
269
_logger.error(location, "numeric character literal cannot be unsigned")
270
else
271
type = _innate_symbol_lookup.get_char_type()
272
fi
273
274
when 's', 'S' then
275
if is_unsigned then
276
type = _innate_symbol_lookup.get_ushort_type()
277
else
278
type = _innate_symbol_lookup.get_short_type()
279
fi
280
281
when 'i', 'I' then
282
if is_unsigned then
283
type = _innate_symbol_lookup.get_uint_type()
284
else
285
type = _innate_symbol_lookup.get_int_type()
286
fi
287
288
when 'l', 'L' then
289
is_wide = true
290
291
if is_unsigned then
292
type = _innate_symbol_lookup.get_ulong_type()
293
else
294
type = _innate_symbol_lookup.get_long_type()
295
fi
296
297
when 'w', 'W' then
298
is_wide = true
299
300
if is_unsigned then
301
type = _innate_symbol_lookup.get_uword_type()
302
narrowing = IR.Scalar.UINT_NATIVE
303
else
304
type = _innate_symbol_lookup.get_word_type()
305
narrowing = IR.Scalar.INT_NATIVE
306
fi
307
308
else
309
if is_unsigned then
310
type = _innate_symbol_lookup.get_uint_type()
311
else
312
type = _innate_symbol_lookup.get_int_type()
313
fi
314
esac
315
316
working = working.substring(0, cutoff)
317
else
318
type = _innate_symbol_lookup.get_int_type()
319
fi
320
321
if !type? then
322
return null
323
fi
324
325
let (ok, parsed) = parse_magnitude(working)
326
327
if !ok \/ !fits(location, working, type) then
328
return null
329
fi
330
331
let constant =
332
if is_wide then
333
Literal.CONSTANT.I8(cast long(parsed))
334
else
335
Literal.CONSTANT.I4(cast int(cast uint(parsed)))
336
fi
337
338
return Literal.NUMBER(constant, type, narrowing)
339
si
340
341
// The magnitude a literal denotes, whatever radix it is written
342
// in. Both the range check and the constant the literal loads
343
// need it, so it is read once here rather than each reading the
344
// spelling for itself.
345
parse_magnitude(value_string: string) -> (ok: bool, value: ulong) =>
346
if value_string.starts_with("0x") \/ value_string.starts_with("0X") then
347
parse_hex(value_string.substring(2, value_string.length - 2))
348
else
349
(
350
let v: ulong mut = _
351
let parsed_ok = ulong.try_parse(value_string, v ref)
352
(parsed_ok, v)
353
)
354
fi
355
356
// Range-check a parsed integer literal against its target
357
// type. Comparing as a ulong using each type's unsigned max
358
// preserves the standard "literal fits in N unsigned bits,
359
// unary minus negates separately" convention. Without this,
360
// oversize literals reach IL emission, where the constant no
361
// longer fits the instruction it is loaded by (issue #580).
362
fits(location: LOCATION, value_string: string, type: Type) -> bool is
363
let (ok, parsed) = parse_magnitude(value_string)
364
365
if !ok then
366
_logger.error(location, "numeric literal {value_string} does not fit in {type}")
367
return false
368
fi
369
370
let max_value = max_unsigned_for(type)
371
372
if max_value > 0UL /\ parsed > max_value then
373
_logger.error(location, "numeric literal {value_string} does not fit in {type}")
374
return false
375
fi
376
377
return true
378
si
379
380
// Parse a hex digit string (no `0x` prefix) into a ulong.
381
// Returns (false, 0) for empty, > 16 digits, or any non-hex
382
// character.
383
parse_hex(s: string) -> (ok: bool, value: ulong) is
384
if s.length == 0 \/ s.length > 16 then
385
return (false, 0UL)
386
fi
387
388
let value: ulong mut = 0UL
389
390
for c in s do
391
let digit: int mut
392
393
if c >= '0' /\ c <= '9' then
394
digit = cast int(c) - cast int('0')
395
elif c >= 'a' /\ c <= 'f' then
396
digit = cast int(c) - cast int('a') + 10
397
elif c >= 'A' /\ c <= 'F' then
398
digit = cast int(c) - cast int('A') + 10
399
else
400
return (false, 0UL)
401
fi
402
403
value = (value << 4) + cast ulong(digit)
404
od
405
406
return (true, value)
407
si
408
409
// Largest non-negative literal value the type accepts, or 0
410
// to signal "no narrower bound than the ulong-parse range"
411
// (used for ulong / uword: ulong.try_parse already enforces
412
// the 64-bit ceiling). 0 makes `fits` skip the narrower-
413
// bound check.
414
max_unsigned_for(type: Type) -> ulong is
415
if type.matches(_innate_symbol_lookup.get_byte_type()) then
416
return 127UL
417
elif type.matches(_innate_symbol_lookup.get_ubyte_type()) then
418
return 255UL
419
elif type.matches(_innate_symbol_lookup.get_short_type()) then
420
return 32767UL
421
elif type.matches(_innate_symbol_lookup.get_ushort_type()) then
422
return 65535UL
423
elif type.matches(_innate_symbol_lookup.get_char_type()) then
424
return 65535UL
425
elif type.matches(_innate_symbol_lookup.get_int_type()) then
426
return 2147483647UL
427
elif type.matches(_innate_symbol_lookup.get_uint_type()) then
428
return 4294967295UL
429
elif type.matches(_innate_symbol_lookup.get_long_type()) then
430
return 9223372036854775807UL
431
elif type.matches(_innate_symbol_lookup.get_word_type()) then
432
return 9223372036854775807UL
433
fi
434
435
return 0UL
436
si
437
si
438
si