Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/static_literals.c
Line
Count
Source
1
#include "prism/internal/static_literals.h"
2
3
#include "prism/compiler/inline.h"
4
#include "prism/compiler/unused.h"
5
6
#include "prism/internal/allocator.h"
7
#include "prism/internal/assert.h"
8
#include "prism/internal/buffer.h"
9
#include "prism/internal/integer.h"
10
#include "prism/internal/isinf.h"
11
#include "prism/internal/oom.h"
12
#include "prism/internal/parser.h"
13
#include "prism/internal/stringy.h"
14
#include "prism/internal/xxh3.h"
15
16
#include <inttypes.h>
17
#include <math.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
/**
22
 * Hash the key built from the given values. Nodes that compare as equal must
23
 * produce identical keys, so each value is either held directly or is the
24
 * digest of a variable-length value. The key is an array so that its values are
25
 * contiguous with no padding between them, which makes every hashed byte
26
 * defined.
27
 */
28
static PRISM_INLINE uint64_t
29
724
key_hash(uint64_t type, uint64_t a, uint64_t b) {
30
724
    const uint64_t key[3] = { type, a, b };
31
724
    return pm_xxh3((const uint8_t *) key, sizeof(key));
32
724
}
33
34
/**
35
 * Return the hash of the value of an integer. An integer whose words are stored
36
 * inline keys on its value directly, and a larger one keys on the digest of its
37
 * words.
38
 */
39
static uint64_t
40
6
integer_hash(const pm_integer_t *integer) {
41
6
    uint64_t magnitude = (integer->length > 2)
  Branch (41:26): [True: 1, False: 5]
42
6
        ? 
pm_xxh3((const uint8_t *) integer->words.values, sizeof(uint32_t) * integer->length)1
43
6
        : 
pm_integer_uint64_value(integer)5
;
44
6
    return key_hash(PM_INTEGER_NODE, integer->negative, magnitude);
45
6
}
46
47
/**
48
 * Return the hash of the key of the given node. It is important that nodes that
49
 * have equivalent static literal values have the same key. This is because we
50
 * hash these keys to look for duplicates.
51
 */
52
static uint64_t
53
722
node_hash(const pm_parser_t *parser, const pm_node_t *node) {
54
722
    switch (PM_NODE_TYPE(node)) {
55
4
        case PM_INTEGER_NODE:
  Branch (55:9): [True: 4, False: 718]
56
4
            return integer_hash(&((const pm_integer_node_t *) node)->value);
57
4
        case PM_SOURCE_LINE_NODE: {
  Branch (57:9): [True: 4, False: 718]
58
4
            int64_t line = (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
59
4
            return key_hash(PM_INTEGER_NODE, line < 0, (uint64_t) (line < 0 ? 
-line0
: line));
  Branch (59:68): [True: 0, False: 4]
60
0
        }
61
0
        case PM_FLOAT_NODE: {
  Branch (61:9): [True: 0, False: 722]
62
0
            const pm_float_node_t *cast = (const pm_float_node_t *) node;
63
0
            double value = cast->value;
64
0
            if (value == 0.0) value = 0.0; /* Normalize -0.0 to 0.0. */
  Branch (64:17): [True: 0, False: 0]
65
66
0
            uint64_t bits;
67
0
            memcpy(&bits, &value, sizeof(bits));
68
0
            return key_hash(PM_FLOAT_NODE, bits, 0);
69
0
        }
70
1
        case PM_RATIONAL_NODE: {
  Branch (70:9): [True: 1, False: 721]
71
1
            const pm_rational_node_t *cast = (const pm_rational_node_t *) node;
72
1
            return key_hash(PM_RATIONAL_NODE, integer_hash(&cast->numerator), integer_hash(&cast->denominator));
73
0
        }
74
1
        case PM_IMAGINARY_NODE: {
  Branch (74:9): [True: 1, False: 721]
75
1
            const pm_imaginary_node_t *cast = (const pm_imaginary_node_t *) node;
76
1
            return key_hash(PM_IMAGINARY_NODE, node_hash(parser, cast->numeric), 0);
77
0
        }
78
7
        case PM_STRING_NODE: {
  Branch (78:9): [True: 7, False: 715]
79
7
            const pm_string_node_t *cast = (const pm_string_node_t *) node;
80
81
7
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
82
7
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (82:17): [True: 0, False: 7]
83
84
7
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
85
0
        }
86
1
        case PM_SOURCE_FILE_NODE: {
  Branch (86:9): [True: 1, False: 721]
87
1
            const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
88
1
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->filepath), pm_string_length(&cast->filepath)), 0);
89
0
        }
90
0
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (90:9): [True: 0, False: 722]
91
0
            const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
92
0
            return key_hash(PM_REGULAR_EXPRESSION_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE);
93
0
        }
94
703
        case PM_SYMBOL_NODE: {
  Branch (94:9): [True: 703, False: 19]
95
703
            const pm_symbol_node_t *cast = (const pm_symbol_node_t *) node;
96
97
703
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
98
703
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (98:17): [True: 0, False: 703]
99
100
703
            return key_hash(PM_SYMBOL_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
101
0
        }
102
1
        case PM_FALSE_NODE:
  Branch (102:9): [True: 1, False: 721]
103
1
        case PM_NIL_NODE:
  Branch (103:9): [True: 0, False: 722]
104
1
        case PM_SOURCE_ENCODING_NODE:
  Branch (104:9): [True: 0, False: 722]
105
1
        case PM_TRUE_NODE:
  Branch (105:9): [True: 0, False: 722]
106
1
            return key_hash(PM_NODE_TYPE(node), 0, 0);
107
0
        default:
  Branch (107:9): [True: 0, False: 722]
108
0
            PRISM_ASSERT(false && "unreachable");
  Branch (108:13): [Folded, False: 0]
  Branch (108:13): [True: 0, False: 0]
  Branch (108:13): [Folded, False: 0]
  Branch (108:13): [True: 0, False: 0]
109
0
            return 0;
110
722
    }
111
722
}
112
113
/**
114
 * Compare two values that can be compared with a simple numeric comparison.
115
 */
116
237
#define COMPARE(left_, right_) (((left_) < (right_)) ? 
-119
:
((left_) > (right_))218
?
194
:
0124
)
  Branch (116:33): [True: 15, False: 91]
  Branch (116:61): [True: 91, False: 0]
  Branch (116:33): [True: 4, False: 4]
  Branch (116:61): [True: 2, False: 2]
  Branch (116:33): [True: 0, False: 0]
  Branch (116:61): [True: 0, False: 0]
  Branch (116:33): [True: 0, False: 117]
  Branch (116:61): [True: 0, False: 117]
  Branch (116:33): [True: 0, False: 6]
  Branch (116:61): [True: 1, False: 5]
117
118
/**
119
 * Return the integer value of the given node as an int64_t.
120
 */
121
static int64_t
122
24
int64_value(const pm_parser_t *parser, const pm_node_t *node) {
123
24
    switch (PM_NODE_TYPE(node)) {
124
10
        case PM_INTEGER_NODE: {
  Branch (124:9): [True: 10, False: 14]
125
10
            const pm_integer_t *integer = &((const pm_integer_node_t *) node)->value;
126
10
            if (integer->length > 1) 
return 0
integer->negative0
?
INT64_MIN : 0
INT64_MAX0
;
  Branch (126:17): [True: 0, False: 10]
  Branch (126:45): [True: 0, False: 0]
127
128
10
            int64_t value = (int64_t) pm_integer_uint64_value(integer);
129
10
            return integer->negative ? 
-value0
: value;
  Branch (129:20): [True: 0, False: 10]
130
10
        }
131
14
        case PM_SOURCE_LINE_NODE:
  Branch (131:9): [True: 14, False: 10]
132
14
            return (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
133
0
        default:
  Branch (133:9): [True: 0, False: 24]
134
0
            PRISM_ASSERT(false && "unreachable");
  Branch (134:13): [Folded, False: 0]
  Branch (134:13): [True: 0, False: 0]
  Branch (134:13): [Folded, False: 0]
  Branch (134:13): [True: 0, False: 0]
135
0
            return 0;
136
24
    }
137
24
}
138
139
/**
140
 * A comparison function for comparing two IntegerNode or SourceLineNode
141
 * instances.
142
 */
143
static int
144
196
compare_integer_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
145
196
    if (PM_NODE_TYPE_P(left, PM_SOURCE_LINE_NODE) || 
PM_NODE_TYPE_P190
(right, PM_SOURCE_LINE_NODE)) {
146
8
        return COMPARE(int64_value(parser, left), int64_value(parser, right));
147
8
    }
148
149
188
    const pm_integer_t *left_integer = &((const pm_integer_node_t *) left)->value;
150
188
    const pm_integer_t *right_integer = &((const pm_integer_node_t *) right)->value;
151
188
    return pm_integer_compare(left_integer, right_integer);
152
196
}
153
154
/**
155
 * A comparison function for comparing two FloatNode instances.
156
 */
157
static int
158
0
compare_float_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
159
0
    return COMPARE(((const pm_float_node_t *) left)->value, ((const pm_float_node_t *) right)->value);
160
0
}
161
162
/**
163
 * Return a pointer to the string value of the given node.
164
 */
165
static const pm_string_t *
166
2.88k
string_value(const pm_node_t *node) {
167
2.88k
    switch (PM_NODE_TYPE(node)) {
168
526
        case PM_STRING_NODE:
  Branch (168:9): [True: 526, False: 2.35k]
169
526
            return &((const pm_string_node_t *) node)->unescaped;
170
4
        case PM_SOURCE_FILE_NODE:
  Branch (170:9): [True: 4, False: 2.87k]
171
4
            return &((const pm_source_file_node_t *) node)->filepath;
172
2.35k
        case PM_SYMBOL_NODE:
  Branch (172:9): [True: 2.35k, False: 530]
173
2.35k
            return &((const pm_symbol_node_t *) node)->unescaped;
174
0
        default:
  Branch (174:9): [True: 0, False: 2.88k]
175
0
            PRISM_ASSERT(false && "unreachable");
  Branch (175:13): [Folded, False: 0]
  Branch (175:13): [True: 0, False: 0]
  Branch (175:13): [Folded, False: 0]
  Branch (175:13): [True: 0, False: 0]
176
0
            return NULL;
177
2.88k
    }
178
2.88k
}
179
180
/**
181
 * A comparison function for comparing two nodes that have attached strings.
182
 * Only the flags in the given mask take part in the comparison.
183
 *
184
 * The FORCED_* flags record how a literal was written. An escape that locks the
185
 * encoding to UTF-8 (a `\u` escape above U+007F, or any `\u` in a character
186
 * literal) sets FORCED_UTF8 whether or not the file is already UTF-8. So in a
187
 * UTF-8 file a `\u00E9` escape and a literal e-acute carry different flags
188
 * while being the same string, and the flag is left out of the mask for them
189
 * to compare equally. A source file node never carries a FORCED_* flag, because
190
 * every instance of __FILE__ in a parse resolves to the same encoding.
191
 */
192
static int
193
1.44k
compare_string_nodes(const pm_node_t *left, const pm_node_t *right, pm_node_flags_t mask) {
194
1.44k
    const pm_string_t *left_string = string_value(left);
195
1.44k
    const pm_string_t *right_string = string_value(right);
196
197
1.44k
    int result = pm_string_compare(left_string, right_string);
198
1.44k
    if (result != 0) 
return result1.32k
;
  Branch (198:9): [True: 1.32k, False: 117]
199
200
    /*
201
     * Equal bytes are not enough. In a binary source file the two bytes written
202
     * as `"\xC3\xA9"` stay BINARY while a `"\u00E9"` escape is forced to UTF-8,
203
     * so those are distinct keys even though the bytes match.
204
     */
205
117
    return COMPARE(left->flags & mask, right->flags & mask);
206
1.44k
}
207
208
/**
209
 * A comparison function for comparing two RegularExpressionNode instances. The
210
 * once flag is left out because it only controls how many times an
211
 * interpolated regular expression is compiled, not its value.
212
 */
213
static int
214
13
compare_regular_expression_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
215
13
    const pm_regular_expression_node_t *left_regexp = (const pm_regular_expression_node_t *) left;
216
13
    const pm_regular_expression_node_t *right_regexp = (const pm_regular_expression_node_t *) right;
217
218
13
    int result = pm_string_compare(&left_regexp->unescaped, &right_regexp->unescaped);
219
13
    if (result != 0) 
return result7
;
  Branch (219:9): [True: 7, False: 6]
220
221
6
    pm_node_flags_t mask = (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE;
222
6
    return COMPARE(left->flags & mask, right->flags & mask);
223
13
}
224
225
/**
226
 * Return the type that the given node is compared as. A source line node can
227
 * equal an integer node and a source file node can equal a string node, so
228
 * each takes the type of the node it can equal.
229
 */
230
static enum pm_node_type
231
3.52k
static_literal_type(const pm_node_t *node) {
232
3.52k
    switch (PM_NODE_TYPE(node)) {
233
15
        case PM_SOURCE_LINE_NODE:
  Branch (233:9): [True: 15, False: 3.50k]
234
15
            return PM_INTEGER_NODE;
235
19
        case PM_SOURCE_FILE_NODE:
  Branch (235:9): [True: 19, False: 3.50k]
236
19
            return PM_STRING_NODE;
237
3.48k
        default:
  Branch (237:9): [True: 3.48k, False: 34]
238
3.48k
            return PM_NODE_TYPE(node);
239
3.52k
    }
240
3.52k
}
241
242
/**
243
 * A comparison function for comparing any two static literal nodes. Nodes that
244
 * are compared as different types are never equal.
245
 */
246
static int
247
1.76k
compare_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
248
1.76k
    enum pm_node_type left_type = static_literal_type(left);
249
1.76k
    enum pm_node_type right_type = static_literal_type(right);
250
1.76k
    if (left_type != right_type) 
return 106
COMPARE106
(left_type, right_type);
  Branch (250:9): [True: 106, False: 1.65k]
251
252
1.65k
    switch (left_type) {
253
196
        case PM_INTEGER_NODE:
  Branch (253:9): [True: 196, False: 1.45k]
254
196
            return compare_integer_nodes(parser, left, right);
255
0
        case PM_FLOAT_NODE:
  Branch (255:9): [True: 0, False: 1.65k]
256
0
            return compare_float_nodes(parser, left, right);
257
0
        case PM_IMAGINARY_NODE:
  Branch (257:9): [True: 0, False: 1.65k]
258
0
            return compare_nodes(parser, ((const pm_imaginary_node_t *) left)->numeric, ((const pm_imaginary_node_t *) right)->numeric);
259
0
        case PM_RATIONAL_NODE: {
  Branch (259:9): [True: 0, False: 1.65k]
260
0
            const pm_rational_node_t *left_rational = (const pm_rational_node_t *) left;
261
0
            const pm_rational_node_t *right_rational = (const pm_rational_node_t *) right;
262
263
0
            int result = pm_integer_compare(&left_rational->denominator, &right_rational->denominator);
264
0
            if (result != 0) return result;
  Branch (264:17): [True: 0, False: 0]
265
266
0
            return pm_integer_compare(&left_rational->numerator, &right_rational->numerator);
267
0
        }
268
265
        case PM_STRING_NODE: {
  Branch (268:9): [True: 265, False: 1.39k]
269
265
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
270
265
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (270:17): [True: 0, False: 265]
271
265
            return compare_string_nodes(left, right, mask);
272
0
        }
273
1.17k
        case PM_SYMBOL_NODE: {
  Branch (273:9): [True: 1.17k, False: 479]
274
1.17k
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
275
1.17k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING4
;
  Branch (275:17): [True: 4, False: 1.17k]
276
1.17k
            return compare_string_nodes(left, right, mask);
277
0
        }
278
13
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (278:9): [True: 13, False: 1.64k]
279
13
            return compare_regular_expression_nodes(parser, left, right);
280
1
        case PM_FALSE_NODE:
  Branch (280:9): [True: 1, False: 1.65k]
281
5
        case PM_NIL_NODE:
  Branch (281:9): [True: 4, False: 1.65k]
282
5
        case PM_SOURCE_ENCODING_NODE:
  Branch (282:9): [True: 0, False: 1.65k]
283
5
        case PM_TRUE_NODE:
  Branch (283:9): [True: 0, False: 1.65k]
284
5
            return 0;
285
0
        default:
  Branch (285:9): [True: 0, False: 1.65k]
286
0
            PRISM_ASSERT(false && "unreachable");
  Branch (286:13): [Folded, False: 0]
  Branch (286:13): [True: 0, False: 0]
  Branch (286:13): [Folded, False: 0]
  Branch (286:13): [True: 0, False: 0]
287
0
            return 0;
288
1.65k
    }
289
1.65k
}
290
291
#undef COMPARE
292
293
/**
294
 * Place an entry into a hash table that is known not to hold an equal node, so
295
 * no comparisons are needed to find its slot.
296
 */
297
static void
298
640
pm_static_literals_place(pm_static_literals_entry_t *entries, uint32_t mask, pm_static_literals_entry_t entry) {
299
640
    uint32_t index = (uint32_t) (entry.hash & mask);
300
689
    while (entries[index].node != NULL) {
  Branch (300:12): [True: 49, False: 640]
301
49
        index = (index + 1) & mask;
302
49
    }
303
640
    entries[index] = entry;
304
640
}
305
306
/**
307
 * Move the nodes of the set into a new hash table with the given capacity. A
308
 * set that has no hash table yet hashes its inline nodes. A set that already
309
 * has one moves its entries, which keep their hashes.
310
 */
311
static void
312
115
pm_static_literals_resize(const pm_parser_t *parser, pm_static_literals_t *literals, uint32_t capacity) {
313
115
    pm_static_literals_entry_t *entries = xcalloc(capacity, sizeof(pm_static_literals_entry_t));
314
115
    if (entries == NULL) 
PRISM_OOM0
();
  Branch (314:9): [True: 0, False: 115]
315
316
    /* It turns out to be more efficient to mask the hash value than to use the
317
     * modulo operator. Because our capacities are always powers of two, we can
318
     * use a bitwise AND to get the same result as the modulo operator. */
319
115
    uint32_t mask = capacity - 1;
320
321
115
    if (literals->capacity == 0) {
  Branch (321:9): [True: 70, False: 45]
322
350
        for (uint32_t index = 0; index < literals->size; 
index++280
) {
  Branch (322:34): [True: 280, False: 70]
323
280
            pm_node_t *node = literals->nodes[index];
324
280
            pm_static_literals_place(entries, mask, (pm_static_literals_entry_t) { .node = node, .hash = node_hash(parser, node) });
325
280
        }
326
70
    } else {
327
765
        for (uint32_t index = 0; index < literals->capacity; 
index++720
) {
  Branch (327:34): [True: 720, False: 45]
328
720
            if (literals->entries[index].node != NULL) {
  Branch (328:17): [True: 360, False: 360]
329
360
                pm_static_literals_place(entries, mask, literals->entries[index]);
330
360
            }
331
720
        }
332
333
45
        xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
334
45
    }
335
336
115
    literals->entries = entries;
337
115
    literals->capacity = capacity;
338
115
}
339
340
/**
341
 * Add a node to the set of static literals. If an equal node is already in the
342
 * set, it is returned (and replaced by the new node if replace is set).
343
 * Otherwise the new node is inserted and NULL is returned. Nodes that are not
344
 * static literals are ignored.
345
 */
346
pm_node_t *
347
7.00k
pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node, bool replace) {
348
7.00k
    switch (PM_NODE_TYPE(node)) {
349
34
        case PM_FALSE_NODE:
  Branch (349:9): [True: 34, False: 6.97k]
350
37
        case PM_FLOAT_NODE:
  Branch (350:9): [True: 3, False: 7.00k]
351
40
        case PM_IMAGINARY_NODE:
  Branch (351:9): [True: 3, False: 7.00k]
352
530
        case PM_INTEGER_NODE:
  Branch (352:9): [True: 490, False: 6.51k]
353
538
        case PM_NIL_NODE:
  Branch (353:9): [True: 8, False: 7.00k]
354
543
        case PM_RATIONAL_NODE:
  Branch (354:9): [True: 5, False: 7.00k]
355
564
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (355:9): [True: 21, False: 6.98k]
356
564
        case PM_SOURCE_ENCODING_NODE:
  Branch (356:9): [True: 0, False: 7.00k]
357
579
        case PM_SOURCE_FILE_NODE:
  Branch (357:9): [True: 15, False: 6.99k]
358
591
        case PM_SOURCE_LINE_NODE:
  Branch (358:9): [True: 12, False: 6.99k]
359
1.47k
        case PM_STRING_NODE:
  Branch (359:9): [True: 879, False: 6.12k]
360
3.87k
        case PM_SYMBOL_NODE:
  Branch (360:9): [True: 2.40k, False: 4.60k]
361
3.89k
        case PM_TRUE_NODE:
  Branch (361:9): [True: 18, False: 6.99k]
362
3.89k
            break;
363
3.11k
        default:
  Branch (363:9): [True: 3.11k, False: 3.89k]
364
3.11k
            return NULL;
365
7.00k
    }
366
367
3.89k
    if (literals->capacity == 0) {
  Branch (367:9): [True: 3.52k, False: 371]
368
        /* A set without a hash table compares the new node against each of
369
         * its nodes directly. Comparing nodes of different types returns
370
         * early, so this costs about as much as hashing the new node. */
371
5.13k
        for (uint32_t index = 0; index < literals->size; 
index++1.60k
) {
  Branch (371:34): [True: 1.69k, False: 3.43k]
372
1.69k
            pm_node_t *existing = literals->nodes[index];
373
374
1.69k
            if (compare_nodes(parser, existing, node) == 0) {
  Branch (374:17): [True: 90, False: 1.60k]
375
90
                if (replace) 
literals->nodes[index] = node80
;
  Branch (375:21): [True: 80, False: 10]
376
90
                return existing;
377
90
            }
378
1.69k
        }
379
380
3.43k
        if (literals->size < PM_STATIC_LITERALS_INLINE_CAPACITY) {
  Branch (380:13): [True: 3.36k, False: 70]
381
3.36k
            literals->nodes[literals->size++] = node;
382
3.36k
            return NULL;
383
3.36k
        }
384
385
        /* The set is full, so its nodes move into a hash table. The table is
386
         * sized to stay at most half full after this node is added, with room
387
         * to spare before its first resize. */
388
70
        pm_static_literals_resize(parser, literals, PM_STATIC_LITERALS_INLINE_CAPACITY * 4);
389
371
    } else if (literals->size * 2 >= literals->capacity) {
  Branch (389:16): [True: 45, False: 326]
390
        /* If we are out of space, we need to resize the hash. */
391
45
        pm_static_literals_resize(parser, literals, literals->capacity * 2);
392
45
    }
393
394
441
    uint64_t hash = node_hash(parser, node);
395
441
    uint32_t mask = literals->capacity - 1;
396
441
    uint32_t index = (uint32_t) (hash & mask);
397
398
    /* We use linear probing to resolve collisions. This means that if the
399
     * current index is occupied, we will move to the next index and try again.
400
     * We are guaranteed that this will eventually find an empty slot because we
401
     * resize the hash when it gets too full. Entries whose hash differs cannot
402
     * be equal, so only entries with a matching hash are compared. */
403
627
    while (literals->entries[index].node != NULL) {
  Branch (403:12): [True: 248, False: 379]
404
248
        const pm_static_literals_entry_t *entry = &literals->entries[index];
405
248
        if (entry->hash == hash && 
compare_nodes(parser, entry->node, node) == 062
)
break62
;
  Branch (405:13): [True: 62, False: 186]
  Branch (405:36): [True: 62, False: 0]
406
186
        index = (index + 1) & mask;
407
186
    }
408
409
    /* If the current index is occupied, we need to return the node that was
410
     * already in the hash. Otherwise, we can just increment the size and insert
411
     * the new node. */
412
441
    pm_node_t *result = literals->entries[index].node;
413
414
441
    if (result == NULL) {
  Branch (414:9): [True: 379, False: 62]
415
379
        literals->size++;
416
379
        literals->entries[index] = (pm_static_literals_entry_t) { .node = node, .hash = hash };
417
379
    } else 
if (62
replace62
) {
  Branch (417:16): [True: 62, False: 0]
418
62
        literals->entries[index].node = node;
419
62
    }
420
421
441
    return result;
422
3.89k
}
423
424
/**
425
 * Free the internal memory associated with the given static literals set.
426
 */
427
void
428
5.65k
pm_static_literals_free(pm_static_literals_t *literals) {
429
5.65k
    if (literals->capacity > 0) xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
  Branch (429:9): [True: 70, False: 5.58k]
430
5.65k
}
431
432
/**
433
 * A helper to determine if the given node is a static literal that is positive.
434
 * This is used for formatting imaginary nodes.
435
 */
436
static bool
437
0
pm_static_literal_positive_p(const pm_node_t *node) {
438
0
    switch (PM_NODE_TYPE(node)) {
439
0
        case PM_FLOAT_NODE:
  Branch (439:9): [True: 0, False: 0]
440
0
            return ((const pm_float_node_t *) node)->value > 0;
441
0
        case PM_INTEGER_NODE:
  Branch (441:9): [True: 0, False: 0]
442
0
            return !((const pm_integer_node_t *) node)->value.negative;
443
0
        case PM_RATIONAL_NODE:
  Branch (443:9): [True: 0, False: 0]
444
0
            return !((const pm_rational_node_t *) node)->numerator.negative;
445
0
        case PM_IMAGINARY_NODE:
  Branch (445:9): [True: 0, False: 0]
446
0
            return pm_static_literal_positive_p(((const pm_imaginary_node_t *) node)->numeric);
447
0
        default:
  Branch (447:9): [True: 0, False: 0]
448
0
            PRISM_ASSERT(false && "unreachable");
  Branch (448:13): [Folded, False: 0]
  Branch (448:13): [True: 0, False: 0]
  Branch (448:13): [Folded, False: 0]
  Branch (448:13): [True: 0, False: 0]
449
0
            return false;
450
0
    }
451
0
}
452
453
/**
454
 * Create a string-based representation of the given static literal.
455
 */
456
void
457
106
pm_static_literal_inspect(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node) {
458
106
    switch (PM_NODE_TYPE(node)) {
459
0
        case PM_FALSE_NODE:
  Branch (459:9): [True: 0, False: 106]
460
0
            pm_buffer_append_string(buffer, "false", 5);
461
0
            break;
462
0
        case PM_FLOAT_NODE: {
  Branch (462:9): [True: 0, False: 106]
463
0
            const double value = ((const pm_float_node_t *) node)->value;
464
465
0
            if (PRISM_ISINF(value)) {
  Branch (465:17): [True: 0, False: 0]
466
0
                if (parser->start[node->location.start] == '-') {
  Branch (466:21): [True: 0, False: 0]
467
0
                    pm_buffer_append_byte(buffer, '-');
468
0
                }
469
0
                pm_buffer_append_string(buffer, "Infinity", 8);
470
0
            } else if (value == 0.0) {
  Branch (470:24): [True: 0, False: 0]
471
0
                if (parser->start[node->location.start] == '-') {
  Branch (471:21): [True: 0, False: 0]
472
0
                    pm_buffer_append_byte(buffer, '-');
473
0
                }
474
0
                pm_buffer_append_string(buffer, "0.0", 3);
475
0
            } else {
476
0
                pm_buffer_append_format(buffer, "%g", value);
477
478
                /* %g will not insert a .0 for 1e100 (we'll get back 1e+100). So
479
                 * we check for the decimal point and add it in here if it's not
480
                 * present. */
481
0
                if (pm_buffer_index(buffer, '.') == SIZE_MAX) {
  Branch (481:21): [True: 0, False: 0]
482
0
                    size_t exponent_index = pm_buffer_index(buffer, 'e');
483
0
                    size_t index = exponent_index == SIZE_MAX ? pm_buffer_length(buffer) : exponent_index;
  Branch (483:36): [True: 0, False: 0]
484
0
                    pm_buffer_insert(buffer, index, ".0", 2);
485
0
                }
486
0
            }
487
488
0
            break;
489
0
        }
490
0
        case PM_IMAGINARY_NODE: {
  Branch (490:9): [True: 0, False: 106]
491
0
            const pm_node_t *numeric = ((const pm_imaginary_node_t *) node)->numeric;
492
0
            pm_buffer_append_string(buffer, "(0", 2);
493
0
            if (pm_static_literal_positive_p(numeric)) pm_buffer_append_byte(buffer, '+');
  Branch (493:17): [True: 0, False: 0]
494
0
            pm_static_literal_inspect(buffer, parser, numeric);
495
0
            if (PM_NODE_TYPE_P(numeric, PM_RATIONAL_NODE)) {
496
0
                pm_buffer_append_byte(buffer, '*');
497
0
            }
498
0
            pm_buffer_append_string(buffer, "i)", 2);
499
0
            break;
500
0
        }
501
21
        case PM_INTEGER_NODE:
  Branch (501:9): [True: 21, False: 85]
502
21
            pm_integer_string(buffer, &((const pm_integer_node_t *) node)->value);
503
21
            break;
504
4
        case PM_NIL_NODE:
  Branch (504:9): [True: 4, False: 102]
505
4
            pm_buffer_append_string(buffer, "nil", 3);
506
4
            break;
507
0
        case PM_RATIONAL_NODE: {
  Branch (507:9): [True: 0, False: 106]
508
0
            const pm_rational_node_t *rational = (const pm_rational_node_t *) node;
509
0
            pm_buffer_append_byte(buffer, '(');
510
0
            pm_integer_string(buffer, &rational->numerator);
511
0
            pm_buffer_append_byte(buffer, '/');
512
0
            pm_integer_string(buffer, &rational->denominator);
513
0
            pm_buffer_append_byte(buffer, ')');
514
0
            break;
515
0
        }
516
0
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (516:9): [True: 0, False: 106]
517
0
            const pm_string_t *unescaped = &((const pm_regular_expression_node_t *) node)->unescaped;
518
0
            pm_buffer_append_byte(buffer, '/');
519
0
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
520
0
            pm_buffer_append_byte(buffer, '/');
521
522
0
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) pm_buffer_append_string(buffer, "m", 1);
523
0
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) pm_buffer_append_string(buffer, "i", 1);
524
0
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) pm_buffer_append_string(buffer, "x", 1);
525
0
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) pm_buffer_append_string(buffer, "n", 1);
526
527
0
            break;
528
0
        }
529
0
        case PM_SOURCE_ENCODING_NODE:
  Branch (529:9): [True: 0, False: 106]
530
0
            pm_buffer_append_format(buffer, "#<Encoding:%s>", parser->encoding->name);
531
0
            break;
532
0
        case PM_SOURCE_FILE_NODE: {
  Branch (532:9): [True: 0, False: 106]
533
0
            const pm_string_t *filepath = &((const pm_source_file_node_t *) node)->filepath;
534
0
            pm_buffer_append_byte(buffer, '"');
535
0
            pm_buffer_append_source(buffer, pm_string_source(filepath), pm_string_length(filepath), PM_BUFFER_ESCAPING_RUBY);
536
0
            pm_buffer_append_byte(buffer, '"');
537
0
            break;
538
0
        }
539
2
        case PM_SOURCE_LINE_NODE:
  Branch (539:9): [True: 2, False: 104]
540
2
            pm_buffer_append_format(buffer, "%" PRIi32, pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line);
541
2
            break;
542
3
        case PM_STRING_NODE: {
  Branch (542:9): [True: 3, False: 103]
543
3
            const pm_string_t *unescaped = &((const pm_string_node_t *) node)->unescaped;
544
3
            pm_buffer_append_byte(buffer, '"');
545
3
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
546
3
            pm_buffer_append_byte(buffer, '"');
547
3
            break;
548
0
        }
549
76
        case PM_SYMBOL_NODE: {
  Branch (549:9): [True: 76, False: 30]
550
76
            const pm_string_t *unescaped = &((const pm_symbol_node_t *) node)->unescaped;
551
76
            pm_buffer_append_byte(buffer, ':');
552
76
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
553
76
            break;
554
0
        }
555
0
        case PM_TRUE_NODE:
  Branch (555:9): [True: 0, False: 106]
556
0
            pm_buffer_append_string(buffer, "true", 4);
557
0
            break;
558
0
        default:
  Branch (558:9): [True: 0, False: 106]
559
0
            PRISM_ASSERT(false && "unreachable");
  Branch (559:13): [Folded, False: 0]
  Branch (559:13): [True: 0, False: 0]
  Branch (559:13): [Folded, False: 0]
  Branch (559:13): [True: 0, False: 0]
560
0
            break;
561
106
    }
562
106
}