1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/* Miscellaneous types used across stage boundaries.
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "elna/boot/result.h"
#include <cstring>
#include <numeric>
namespace elna::boot
{
location::location(const std::size_t line, const std::size_t column)
: m_line(line), m_column(column)
{
}
std::size_t location::line() const
{
return this->m_line;
}
std::size_t location::column() const
{
return this->m_column;
}
bool location::available() const
{
return this->m_line != 0 || this->m_column != 0;
}
source_position::source_position(location start, location end)
: m_start(start), m_end(end)
{
}
const location& source_position::start() const
{
return this->m_start;
}
const location& source_position::end() const
{
return this->m_end;
}
bool source_position::is_span() const
{
return this->m_start != this->m_end;
}
error::error(const source_position position)
: position(position)
{
}
std::deque<std::unique_ptr<error>>& error_container::errors()
{
return m_errors;
}
bool error_container::has_errors() const
{
return !m_errors.empty();
}
identifier::identifier(const std::string& name, const source_position& position)
: m_name(name), m_position(position)
{
}
const std::string& identifier::name() const
{
return this->m_name;
}
const source_position& identifier::position() const
{
return this->m_position;
}
bool identifier::operator==(const identifier& that) const
{
return this->m_name == that;
}
bool identifier::operator==(std::string_view that) const
{
return this->m_name == that;
}
identifier_definition::identifier_definition(const std::string& name,
const source_position& position, const bool exported)
: m_identifier(name, position), m_exported(exported)
{
}
const std::string& identifier_definition::name() const
{
return this->m_identifier.name();
}
const identifier& identifier_definition::id() const
{
return this->m_identifier;
}
bool identifier_definition::exported() const
{
return this->m_exported;
}
integer_literal::integer_literal(bool is_signed, std::size_t size)
: m_signed(is_signed), m_size(size)
{
mpz_init(this->raw);
}
integer_literal::integer_literal(integer_literal&& that) noexcept
: m_signed(that.is_signed()), m_size(that.size())
{
mpz_init(this->raw);
mpz_swap(this->raw, that.raw);
}
integer_literal::integer_literal(const integer_literal& that)
: m_signed(that.is_signed()), m_size(that.size())
{
mpz_init(this->raw);
mpz_set(this->raw, that.raw);
}
integer_literal::~integer_literal()
{
mpz_clear(this->raw);
}
std::optional<integer_literal> integer_literal::add(const integer_literal& that) const
{
integer_literal result = *this;
mpz_add(result.raw, this->raw, that.raw);
return std::move(result).check();
}
std::optional<integer_literal> integer_literal::sub(const integer_literal& that) const
{
integer_literal result = *this;
mpz_sub(result.raw, this->raw, that.raw);
return std::move(result).check();
}
std::optional<integer_literal> integer_literal::mul(const integer_literal& that) const
{
integer_literal result = *this;
mpz_mul(result.raw, this->raw, that.raw);
return std::move(result).check();
}
std::optional<integer_literal> integer_literal::div(const integer_literal& that) const
{
if (mpz_cmp_ui(that.raw, 0U) == 0)
{
return std::nullopt;
}
else
{
integer_literal result = *this;
mpz_div(result.raw, this->raw, that.raw);
return std::make_optional(std::move(result));
}
}
std::optional<integer_literal> integer_literal::mod(const integer_literal& that) const
{
if (mpz_cmp_ui(that.raw, 0U) == 0)
{
return std::nullopt;
}
else
{
integer_literal result = *this;
mpz_mod(result.raw, this->raw, that.raw);
return std::make_optional(std::move(result));
}
}
std::optional<integer_literal> integer_literal::neg() const
{
if (!is_signed() || is_negative_minimum())
{
return std::nullopt;
}
integer_literal result = *this;
mpz_neg(result.raw, this->raw);
return result;
}
std::optional<integer_literal> integer_literal::shl(const integer_literal& that) const
{
if (that >= bits())
{
return std::nullopt;
}
integer_literal result = *this;
mpz_mul_2exp(result.raw, this->raw, static_cast<mp_bitcnt_t>(mpz_get_ui(that.raw)));
return std::move(result).check();
}
std::optional<integer_literal> integer_literal::shr(const integer_literal& that) const
{
integer_literal result = *this;
if (that >= bits())
{
mpz_set_si(result.raw, *this > 0 ? 0 : -1);
}
else
{
mpz_fdiv_q_2exp(result.raw, this->raw, static_cast<mp_bitcnt_t>(mpz_get_ui(that.raw)));
}
return std::make_optional(std::move(result));
}
integer_literal integer_literal::operator|(const integer_literal& that) const
{
integer_literal result = *this;
mpz_ior(result.raw, this->raw, that.raw);
return result;
}
integer_literal integer_literal::operator&(const integer_literal& that) const
{
integer_literal result = *this;
mpz_and(result.raw, this->raw, that.raw);
return result;
}
integer_literal integer_literal::operator^(const integer_literal& that) const
{
integer_literal result = *this;
mpz_xor(result.raw, this->raw, that.raw);
return result;
}
integer_literal integer_literal::operator~() const
{
integer_literal result = *this;
mpz_com(result.raw, this->raw);
return result;
}
bool integer_literal::operator==(const integer_literal& that) const
{
return mpz_cmp(this->raw, that.raw) == 0;
}
std::weak_ordering integer_literal::operator<=>(const integer_literal& that) const
{
return mpz_cmp(this->raw, that.raw) <=> 0;
}
integer_literal& integer_literal::operator=(integer_literal&& that) noexcept
{
swap(*this, that);
return *this;
}
integer_literal& integer_literal::operator=(const integer_literal& that)
{
if (this != &that)
{
integer_literal temp(that);
swap(*this, temp);
}
return *this;
}
std::optional<std::ptrdiff_t> integer_literal::to_signed() const
{
if (!is_signed())
{
return std::nullopt;
}
if (is_negative_minimum(sizeof(std::ptrdiff_t) * CHAR_BIT))
{
return std::numeric_limits<ptrdiff_t>::min();
}
auto [written, rop] = export_to_words<std::ptrdiff_t>();
if (written > 1 || rop < 0)
{
return std::nullopt;
}
return mpz_sgn(this->raw) < 0 ? -rop : rop;
}
std::optional<std::size_t> integer_literal::to_unsigned() const
{
if (is_signed())
{
return std::nullopt;
}
auto [written, rop] = export_to_words<std::size_t>();
return written > 1 ? std::nullopt : std::make_optional(rop);
}
bool integer_literal::fit_into(const std::size_t target_size)
{
if (fits_in(target_size * CHAR_BIT))
{
this->m_size = target_size;
return true;
}
return false;
}
bool integer_literal::is_signed() const
{
return this->m_signed;
}
std::size_t integer_literal::size() const
{
return this->m_size;
}
std::string integer_literal::to_string(const std::uint8_t base) const
{
// +1 sign, +1 null terminator
const size_t buffer_size = mpz_sizeinbase(this->raw, static_cast<int>(base)) + 2;
std::string result(buffer_size, '\0');
mpz_get_str(result.data(), base, this->raw);
result.resize(std::strlen(result.c_str()));
return result;
}
void swap(integer_literal& lhs, integer_literal& rhs) noexcept
{
mpz_swap(lhs.raw, rhs.raw);
std::swap(lhs.m_signed, rhs.m_signed);
std::swap(lhs.m_size, rhs.m_size);
}
bool integer_literal::fits_in(const std::size_t bits) const
{
std::size_t required_bits = mpz_sizeinbase(this->raw, 2);
if (!is_negative_minimum(bits))
{
++required_bits; // Add one bit for the sign.
}
return required_bits <= bits && (mpz_sgn(this->raw) >= 0 || is_signed());
}
std::optional<integer_literal> integer_literal::check() &&
{
return fits_in(bits()) ? std::make_optional(std::move(*this)) : std::nullopt;
}
bool integer_literal::is_negative_minimum(const std::size_t bits) const
{
return mpz_sgn(this->raw) < 0 && mpz_scan1(this->raw, 0) == bits - 1;
}
bool integer_literal::is_negative_minimum() const
{
return is_negative_minimum(bits());
}
std::size_t integer_literal::bits() const
{
return size() * CHAR_BIT;
}
std::size_t constant_value_hash::operator()(const elna::boot::constant_value& value) const noexcept
{
return std::visit([](auto&& alternative) -> std::size_t {
using T = std::decay_t<decltype(alternative)>;
return std::hash<T>{}(alternative);
}, value);
}
hash_accumulator hash_accumulator::operator+(const std::size_t& that) const
{
hash_accumulator result{};
result.m_seed ^= that + hash_accumulator::golden_ratio
// NOLINTNEXTLINE(readability-magic-numbers)
+ (this->m_seed << 6) + (this->m_seed >> 2);
return result;
}
std::size_t hash_accumulator::seed() const
{
return this->m_seed;
}
}
std::size_t std::hash<elna::boot::identifier>::operator()(
const elna::boot::identifier& key) const noexcept
{
return std::hash<std::string>{}(key.name());
}
std::size_t std::hash<elna::boot::constant_aggregate<std::vector>>::operator()(
const elna::boot::constant_aggregate<std::vector>& key) const noexcept
{
const elna::boot::constant_value_hash hasher{};
auto hash = std::accumulate(key->begin(), key->end(), elna::boot::hash_accumulator{},
[&hasher](const auto& accumulator, const auto& element) {
return accumulator + hasher(element);
});
return hash.seed();
}
std::size_t std::hash<elna::boot::constant_aggregate<elna::boot::ordered_map>>::operator()(
const elna::boot::constant_aggregate<elna::boot::ordered_map>& key) const noexcept
{
const elna::boot::constant_value_hash hasher{};
auto hash = std::accumulate(key->begin(), key->end(), elna::boot::hash_accumulator{},
[&hasher](const auto& accumulator, const auto& element) {
return accumulator + std::hash<std::string>{}(element.first) + hasher(element.second);
});
return hash.seed();
}
std::size_t std::hash<elna::boot::integer_literal>::operator()(const elna::boot::integer_literal& key) const noexcept
{
if (key.is_signed())
{
if (auto converted = key.to_signed())
{
return std::hash<std::ptrdiff_t>{}(*converted);
}
}
else
{
if (auto converted = key.to_unsigned())
{
return std::hash<std::size_t>{}(*converted);
}
}
return 0;
}
|