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
3.58k
key_hash(uint64_t type, uint64_t a, uint64_t b) {
30
3.58k
    const uint64_t key[3] = { type, a, b };
31
3.58k
    return pm_xxh3((const uint8_t *) key, sizeof(key));
32
3.58k
}
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
181
integer_hash(const pm_integer_t *integer) {
41
181
    uint64_t magnitude = (integer->length > 2)
  Branch (41:26): [True: 49, False: 132]
42
181
        ? 
pm_xxh3((const uint8_t *) integer->words.values, sizeof(uint32_t) * integer->length)49
43
181
        : 
pm_integer_uint64_value(integer)132
;
44
181
    return key_hash(PM_INTEGER_NODE, integer->negative, magnitude);
45
181
}
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
3.57k
node_hash(const pm_parser_t *parser, const pm_node_t *node) {
54
3.57k
    switch (PM_NODE_TYPE(node)) {
55
177
        case PM_INTEGER_NODE:
  Branch (55:9): [True: 177, False: 3.39k]
56
177
            return integer_hash(&((const pm_integer_node_t *) node)->value);
57
6
        case PM_SOURCE_LINE_NODE: {
  Branch (57:9): [True: 6, False: 3.57k]
58
6
            int64_t line = (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
59
6
            return key_hash(PM_INTEGER_NODE, line < 0, (uint64_t) (line < 0 ? 
-line0
: line));
  Branch (59:68): [True: 0, False: 6]
60
0
        }
61
12
        case PM_FLOAT_NODE: {
  Branch (61:9): [True: 12, False: 3.56k]
62
12
            const pm_float_node_t *cast = (const pm_float_node_t *) node;
63
12
            double value = cast->value;
64
12
            if (value == 0.0) 
value = 0.01
; /* Normalize -0.0 to 0.0. */
  Branch (64:17): [True: 1, False: 11]
65
66
12
            uint64_t bits;
67
12
            memcpy(&bits, &value, sizeof(bits));
68
12
            return key_hash(PM_FLOAT_NODE, bits, 0);
69
0
        }
70
2
        case PM_RATIONAL_NODE: {
  Branch (70:9): [True: 2, False: 3.57k]
71
2
            const pm_rational_node_t *cast = (const pm_rational_node_t *) node;
72
2
            return key_hash(PM_RATIONAL_NODE, integer_hash(&cast->numerator), integer_hash(&cast->denominator));
73
0
        }
74
2
        case PM_IMAGINARY_NODE: {
  Branch (74:9): [True: 2, False: 3.57k]
75
2
            const pm_imaginary_node_t *cast = (const pm_imaginary_node_t *) node;
76
2
            return key_hash(PM_IMAGINARY_NODE, node_hash(parser, cast->numeric), 0);
77
0
        }
78
9
        case PM_STRING_NODE: {
  Branch (78:9): [True: 9, False: 3.56k]
79
9
            const pm_string_node_t *cast = (const pm_string_node_t *) node;
80
81
9
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
82
9
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (82:17): [True: 0, False: 9]
83
84
9
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
85
0
        }
86
2
        case PM_SOURCE_FILE_NODE: {
  Branch (86:9): [True: 2, False: 3.57k]
87
2
            const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
88
2
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->filepath), pm_string_length(&cast->filepath)), 0);
89
0
        }
90
1
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (90:9): [True: 1, False: 3.57k]
91
1
            const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
92
1
            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
3.36k
        case PM_SYMBOL_NODE: {
  Branch (94:9): [True: 3.36k, False: 215]
95
3.36k
            const pm_symbol_node_t *cast = (const pm_symbol_node_t *) node;
96
97
3.36k
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
98
3.36k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING5
;
  Branch (98:17): [True: 5, False: 3.35k]
99
100
3.36k
            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: 3.57k]
103
2
        case PM_NIL_NODE:
  Branch (103:9): [True: 1, False: 3.57k]
104
3
        case PM_SOURCE_ENCODING_NODE:
  Branch (104:9): [True: 1, False: 3.57k]
105
4
        case PM_TRUE_NODE:
  Branch (105:9): [True: 1, False: 3.57k]
106
4
            return key_hash(PM_NODE_TYPE(node), 0, 0);
107
0
        default:
  Branch (107:9): [True: 0, False: 3.57k]
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
3.57k
    }
111
3.57k
}
112
113
/**
114
 * Compare two values that can be compared with a simple numeric comparison.
115
 */
116
599
#define COMPARE(left_, right_) (((left_) < (right_)) ? 
-192
:
((left_) > (right_))507
?
1215
:
0292
)
  Branch (116:33): [True: 51, False: 177]
  Branch (116:61): [True: 177, False: 0]
  Branch (116:33): [True: 26, False: 26]
  Branch (116:61): [True: 19, False: 7]
  Branch (116:33): [True: 15, False: 32]
  Branch (116:61): [True: 19, False: 13]
  Branch (116:33): [True: 0, False: 271]
  Branch (116:61): [True: 0, False: 271]
  Branch (116:33): [True: 0, False: 1]
  Branch (116:61): [True: 0, False: 1]
117
118
/**
119
 * Return the integer value of the given node as an int64_t.
120
 */
121
static int64_t
122
156
int64_value(const pm_parser_t *parser, const pm_node_t *node) {
123
156
    switch (PM_NODE_TYPE(node)) {
124
72
        case PM_INTEGER_NODE: {
  Branch (124:9): [True: 72, False: 84]
125
72
            const pm_integer_t *integer = &((const pm_integer_node_t *) node)->value;
126
72
            if (integer->length > 1) 
return 6
integer->negative6
?
INT64_MIN : 6
INT64_MAX6
;
  Branch (126:17): [True: 6, False: 66]
  Branch (126:45): [True: 2, False: 4]
127
128
66
            int64_t value = (int64_t) pm_integer_uint64_value(integer);
129
66
            return integer->negative ? 
-value10
:
value56
;
  Branch (129:20): [True: 10, False: 56]
130
72
        }
131
84
        case PM_SOURCE_LINE_NODE:
  Branch (131:9): [True: 84, False: 72]
132
84
            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: 156]
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
156
    }
137
156
}
138
139
/**
140
 * A comparison function for comparing two IntegerNode or SourceLineNode
141
 * instances.
142
 */
143
static int
144
905
compare_integer_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
145
905
    if (PM_NODE_TYPE_P(left, PM_SOURCE_LINE_NODE) || 
PM_NODE_TYPE_P855
(right, PM_SOURCE_LINE_NODE)) {
146
52
        return COMPARE(int64_value(parser, left), int64_value(parser, right));
147
52
    }
148
149
853
    const pm_integer_t *left_integer = &((const pm_integer_node_t *) left)->value;
150
853
    const pm_integer_t *right_integer = &((const pm_integer_node_t *) right)->value;
151
853
    return pm_integer_compare(left_integer, right_integer);
152
905
}
153
154
/**
155
 * A comparison function for comparing two FloatNode instances.
156
 */
157
static int
158
47
compare_float_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
159
47
    return COMPARE(((const pm_float_node_t *) left)->value, ((const pm_float_node_t *) right)->value);
160
47
}
161
162
/**
163
 * Return a pointer to the string value of the given node.
164
 */
165
static const pm_string_t *
166
12.4k
string_value(const pm_node_t *node) {
167
12.4k
    switch (PM_NODE_TYPE(node)) {
168
2.00k
        case PM_STRING_NODE:
  Branch (168:9): [True: 2.00k, False: 10.4k]
169
2.00k
            return &((const pm_string_node_t *) node)->unescaped;
170
1
        case PM_SOURCE_FILE_NODE:
  Branch (170:9): [True: 1, False: 12.4k]
171
1
            return &((const pm_source_file_node_t *) node)->filepath;
172
10.4k
        case PM_SYMBOL_NODE:
  Branch (172:9): [True: 10.4k, False: 2.00k]
173
10.4k
            return &((const pm_symbol_node_t *) node)->unescaped;
174
0
        default:
  Branch (174:9): [True: 0, False: 12.4k]
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
12.4k
    }
178
12.4k
}
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
6.20k
compare_string_nodes(const pm_node_t *left, const pm_node_t *right, pm_node_flags_t mask) {
194
6.20k
    const pm_string_t *left_string = string_value(left);
195
6.20k
    const pm_string_t *right_string = string_value(right);
196
197
6.20k
    int result = pm_string_compare(left_string, right_string);
198
6.20k
    if (result != 0) 
return result5.93k
;
  Branch (198:9): [True: 5.93k, False: 271]
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
271
    return COMPARE(left->flags & mask, right->flags & mask);
206
6.20k
}
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
9
compare_regular_expression_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
215
9
    const pm_regular_expression_node_t *left_regexp = (const pm_regular_expression_node_t *) left;
216
9
    const pm_regular_expression_node_t *right_regexp = (const pm_regular_expression_node_t *) right;
217
218
9
    int result = pm_string_compare(&left_regexp->unescaped, &right_regexp->unescaped);
219
9
    if (result != 0) 
return result8
;
  Branch (219:9): [True: 8, False: 1]
220
221
1
    pm_node_flags_t mask = (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE;
222
1
    return COMPARE(left->flags & mask, right->flags & mask);
223
9
}
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
14.8k
static_literal_type(const pm_node_t *node) {
232
14.8k
    switch (PM_NODE_TYPE(node)) {
233
70
        case PM_SOURCE_LINE_NODE:
  Branch (233:9): [True: 70, False: 14.7k]
234
70
            return PM_INTEGER_NODE;
235
12
        case PM_SOURCE_FILE_NODE:
  Branch (235:9): [True: 12, False: 14.7k]
236
12
            return PM_STRING_NODE;
237
14.7k
        default:
  Branch (237:9): [True: 14.7k, False: 82]
238
14.7k
            return PM_NODE_TYPE(node);
239
14.8k
    }
240
14.8k
}
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
7.40k
compare_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
248
7.40k
    enum pm_node_type left_type = static_literal_type(left);
249
7.40k
    enum pm_node_type right_type = static_literal_type(right);
250
7.40k
    if (left_type != right_type) 
return 228
COMPARE228
(left_type, right_type);
  Branch (250:9): [True: 228, False: 7.17k]
251
252
7.17k
    switch (left_type) {
253
905
        case PM_INTEGER_NODE:
  Branch (253:9): [True: 905, False: 6.27k]
254
905
            return compare_integer_nodes(parser, left, right);
255
47
        case PM_FLOAT_NODE:
  Branch (255:9): [True: 47, False: 7.13k]
256
47
            return compare_float_nodes(parser, left, right);
257
0
        case PM_IMAGINARY_NODE:
  Branch (257:9): [True: 0, False: 7.17k]
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: 7.17k]
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
1.00k
        case PM_STRING_NODE: {
  Branch (268:9): [True: 1.00k, False: 6.17k]
269
1.00k
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
270
1.00k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING2
;
  Branch (270:17): [True: 2, False: 1.00k]
271
1.00k
            return compare_string_nodes(left, right, mask);
272
0
        }
273
5.20k
        case PM_SYMBOL_NODE: {
  Branch (273:9): [True: 5.20k, False: 1.97k]
274
5.20k
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
275
5.20k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING30
;
  Branch (275:17): [True: 30, False: 5.17k]
276
5.20k
            return compare_string_nodes(left, right, mask);
277
0
        }
278
9
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (278:9): [True: 9, False: 7.16k]
279
9
            return compare_regular_expression_nodes(parser, left, right);
280
2
        case PM_FALSE_NODE:
  Branch (280:9): [True: 2, False: 7.17k]
281
2
        case PM_NIL_NODE:
  Branch (281:9): [True: 0, False: 7.17k]
282
2
        case PM_SOURCE_ENCODING_NODE:
  Branch (282:9): [True: 0, False: 7.17k]
283
8
        case PM_TRUE_NODE:
  Branch (283:9): [True: 6, False: 7.17k]
284
8
            return 0;
285
0
        default:
  Branch (285:9): [True: 0, False: 7.17k]
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
7.17k
    }
289
7.17k
}
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
3.19k
pm_static_literals_place(pm_static_literals_entry_t *entries, uint32_t mask, pm_static_literals_entry_t entry) {
299
3.19k
    uint32_t index = (uint32_t) (entry.hash & mask);
300
3.54k
    while (entries[index].node != NULL) {
  Branch (300:12): [True: 349, False: 3.19k]
301
349
        index = (index + 1) & mask;
302
349
    }
303
3.19k
    entries[index] = entry;
304
3.19k
}
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
578
pm_static_literals_resize(const pm_parser_t *parser, pm_static_literals_t *literals, uint32_t capacity) {
313
578
    pm_static_literals_entry_t *entries = xcalloc(capacity, sizeof(pm_static_literals_entry_t));
314
578
    if (entries == NULL) 
PRISM_OOM0
();
  Branch (314:9): [True: 0, False: 578]
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
578
    uint32_t mask = capacity - 1;
320
321
578
    if (literals->capacity == 0) {
  Branch (321:9): [True: 357, False: 221]
322
1.78k
        for (uint32_t index = 0; index < literals->size; 
index++1.42k
) {
  Branch (322:34): [True: 1.42k, False: 357]
323
1.42k
            pm_node_t *node = literals->nodes[index];
324
1.42k
            pm_static_literals_place(entries, mask, (pm_static_literals_entry_t) { .node = node, .hash = node_hash(parser, node) });
325
1.42k
        }
326
357
    } else {
327
3.75k
        for (uint32_t index = 0; index < literals->capacity; 
index++3.53k
) {
  Branch (327:34): [True: 3.53k, False: 221]
328
3.53k
            if (literals->entries[index].node != NULL) {
  Branch (328:17): [True: 1.76k, False: 1.76k]
329
1.76k
                pm_static_literals_place(entries, mask, literals->entries[index]);
330
1.76k
            }
331
3.53k
        }
332
333
221
        xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
334
221
    }
335
336
578
    literals->entries = entries;
337
578
    literals->capacity = capacity;
338
578
}
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
31.0k
pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node, bool replace) {
348
31.0k
    switch (PM_NODE_TYPE(node)) {
349
47
        case PM_FALSE_NODE:
  Branch (349:9): [True: 47, False: 30.9k]
350
126
        case PM_FLOAT_NODE:
  Branch (350:9): [True: 79, False: 30.9k]
351
138
        case PM_IMAGINARY_NODE:
  Branch (351:9): [True: 12, False: 31.0k]
352
2.23k
        case PM_INTEGER_NODE:
  Branch (352:9): [True: 2.09k, False: 28.9k]
353
2.23k
        case PM_NIL_NODE:
  Branch (353:9): [True: 5, False: 31.0k]
354
2.26k
        case PM_RATIONAL_NODE:
  Branch (354:9): [True: 24, False: 31.0k]
355
2.29k
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (355:9): [True: 29, False: 30.9k]
356
2.29k
        case PM_SOURCE_ENCODING_NODE:
  Branch (356:9): [True: 3, False: 31.0k]
357
2.30k
        case PM_SOURCE_FILE_NODE:
  Branch (357:9): [True: 15, False: 31.0k]
358
2.36k
        case PM_SOURCE_LINE_NODE:
  Branch (358:9): [True: 56, False: 30.9k]
359
5.42k
        case PM_STRING_NODE:
  Branch (359:9): [True: 3.05k, False: 27.9k]
360
15.1k
        case PM_SYMBOL_NODE:
  Branch (360:9): [True: 9.77k, False: 21.2k]
361
15.2k
        case PM_TRUE_NODE:
  Branch (361:9): [True: 61, False: 30.9k]
362
15.2k
            break;
363
15.7k
        default:
  Branch (363:9): [True: 15.7k, False: 15.2k]
364
15.7k
            return NULL;
365
31.0k
    }
366
367
15.2k
    if (literals->capacity == 0) {
  Branch (367:9): [True: 13.4k, False: 1.78k]
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
20.4k
        for (uint32_t index = 0; index < literals->size; 
index++7.03k
) {
  Branch (371:34): [True: 7.30k, False: 13.1k]
372
7.30k
            pm_node_t *existing = literals->nodes[index];
373
374
7.30k
            if (compare_nodes(parser, existing, node) == 0) {
  Branch (374:17): [True: 271, False: 7.03k]
375
271
                if (replace) 
literals->nodes[index] = node225
;
  Branch (375:21): [True: 225, False: 46]
376
271
                return existing;
377
271
            }
378
7.30k
        }
379
380
13.1k
        if (literals->size < PM_STATIC_LITERALS_INLINE_CAPACITY) {
  Branch (380:13): [True: 12.8k, False: 357]
381
12.8k
            literals->nodes[literals->size++] = node;
382
12.8k
            return NULL;
383
12.8k
        }
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
357
        pm_static_literals_resize(parser, literals, PM_STATIC_LITERALS_INLINE_CAPACITY * 4);
389
1.78k
    } else if (literals->size * 2 >= literals->capacity) {
  Branch (389:16): [True: 221, False: 1.56k]
390
        /* If we are out of space, we need to resize the hash. */
391
221
        pm_static_literals_resize(parser, literals, literals->capacity * 2);
392
221
    }
393
394
2.14k
    uint64_t hash = node_hash(parser, node);
395
2.14k
    uint32_t mask = literals->capacity - 1;
396
2.14k
    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
3.10k
    while (literals->entries[index].node != NULL) {
  Branch (403:12): [True: 1.05k, False: 2.04k]
404
1.05k
        const pm_static_literals_entry_t *entry = &literals->entries[index];
405
1.05k
        if (entry->hash == hash && 
compare_nodes(parser, entry->node, node) == 0103
)
break103
;
  Branch (405:13): [True: 103, False: 956]
  Branch (405:36): [True: 103, False: 0]
406
956
        index = (index + 1) & mask;
407
956
    }
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
2.14k
    pm_node_t *result = literals->entries[index].node;
413
414
2.14k
    if (result == NULL) {
  Branch (414:9): [True: 2.04k, False: 103]
415
2.04k
        literals->size++;
416
2.04k
        literals->entries[index] = (pm_static_literals_entry_t) { .node = node, .hash = hash };
417
2.04k
    } else 
if (103
replace103
) {
  Branch (417:16): [True: 103, False: 0]
418
103
        literals->entries[index].node = node;
419
103
    }
420
421
2.14k
    return result;
422
15.2k
}
423
424
/**
425
 * Free the internal memory associated with the given static literals set.
426
 */
427
void
428
26.0k
pm_static_literals_free(pm_static_literals_t *literals) {
429
26.0k
    if (literals->capacity > 0) xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
  Branch (429:9): [True: 357, False: 25.6k]
430
26.0k
}
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
163
pm_static_literal_inspect(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node) {
458
163
    switch (PM_NODE_TYPE(node)) {
459
0
        case PM_FALSE_NODE:
  Branch (459:9): [True: 0, False: 163]
460
0
            pm_buffer_append_string(buffer, "false", 5);
461
0
            break;
462
13
        case PM_FLOAT_NODE: {
  Branch (462:9): [True: 13, False: 150]
463
13
            const double value = ((const pm_float_node_t *) node)->value;
464
465
13
            if (PRISM_ISINF(value)) {
  Branch (465:17): [True: 5, False: 8]
466
5
                if (parser->start[node->location.start] == '-') {
  Branch (466:21): [True: 0, False: 5]
467
0
                    pm_buffer_append_byte(buffer, '-');
468
0
                }
469
5
                pm_buffer_append_string(buffer, "Infinity", 8);
470
8
            } else if (value == 0.0) {
  Branch (470:24): [True: 0, False: 8]
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
8
            } else {
476
8
                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
8
                if (pm_buffer_index(buffer, '.') == SIZE_MAX) {
  Branch (481:21): [True: 5, False: 3]
482
5
                    size_t exponent_index = pm_buffer_index(buffer, 'e');
483
5
                    size_t index = exponent_index == SIZE_MAX ? pm_buffer_length(buffer) : 
exponent_index0
;
  Branch (483:36): [True: 5, False: 0]
484
5
                    pm_buffer_insert(buffer, index, ".0", 2);
485
5
                }
486
8
            }
487
488
13
            break;
489
0
        }
490
0
        case PM_IMAGINARY_NODE: {
  Branch (490:9): [True: 0, False: 163]
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
38
        case PM_INTEGER_NODE:
  Branch (501:9): [True: 38, False: 125]
502
38
            pm_integer_string(buffer, &((const pm_integer_node_t *) node)->value);
503
38
            break;
504
0
        case PM_NIL_NODE:
  Branch (504:9): [True: 0, False: 163]
505
0
            pm_buffer_append_string(buffer, "nil", 3);
506
0
            break;
507
0
        case PM_RATIONAL_NODE: {
  Branch (507:9): [True: 0, False: 163]
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
1
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (516:9): [True: 1, False: 162]
517
1
            const pm_string_t *unescaped = &((const pm_regular_expression_node_t *) node)->unescaped;
518
1
            pm_buffer_append_byte(buffer, '/');
519
1
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
520
1
            pm_buffer_append_byte(buffer, '/');
521
522
1
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) 
pm_buffer_append_string(buffer, "m", 1)0
;
523
1
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) 
pm_buffer_append_string(buffer, "i", 1)0
;
524
1
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) 
pm_buffer_append_string(buffer, "x", 1)0
;
525
1
            if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) 
pm_buffer_append_string(buffer, "n", 1)0
;
526
527
1
            break;
528
0
        }
529
0
        case PM_SOURCE_ENCODING_NODE:
  Branch (529:9): [True: 0, False: 163]
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: 163]
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
7
        case PM_SOURCE_LINE_NODE:
  Branch (539:9): [True: 7, False: 156]
540
7
            pm_buffer_append_format(buffer, "%" PRIi32, pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line);
541
7
            break;
542
5
        case PM_STRING_NODE: {
  Branch (542:9): [True: 5, False: 158]
543
5
            const pm_string_t *unescaped = &((const pm_string_node_t *) node)->unescaped;
544
5
            pm_buffer_append_byte(buffer, '"');
545
5
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
546
5
            pm_buffer_append_byte(buffer, '"');
547
5
            break;
548
0
        }
549
99
        case PM_SYMBOL_NODE: {
  Branch (549:9): [True: 99, False: 64]
550
99
            const pm_string_t *unescaped = &((const pm_symbol_node_t *) node)->unescaped;
551
99
            pm_buffer_append_byte(buffer, ':');
552
99
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
553
99
            break;
554
0
        }
555
0
        case PM_TRUE_NODE:
  Branch (555:9): [True: 0, False: 163]
556
0
            pm_buffer_append_string(buffer, "true", 4);
557
0
            break;
558
0
        default:
  Branch (558:9): [True: 0, False: 163]
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
163
    }
562
163
}