Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B), next exclusive to main-4 (E) [9]
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
11.3k
key_hash(uint64_t type, uint64_t a, uint64_t b) {
30
11.3k
    const uint64_t key[3] = { type, a, b };
31
11.3k
    return pm_xxh3((const uint8_t *) key, sizeof(key));
32
11.3k
}
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
2.64k
integer_hash(const pm_integer_t *integer) {
41
2.64k
    uint64_t magnitude = (integer->length > 2)
  Branch (41:26): [True: 129, False: 2.51k]
42
2.64k
        ? 
pm_xxh3((const uint8_t *) integer->words.values, sizeof(uint32_t) * integer->length)129
43
2.64k
        : 
pm_integer_uint64_value(integer)2.51k
;
44
2.64k
    return key_hash(PM_INTEGER_NODE, integer->negative, magnitude);
45
2.64k
}
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
9.72k
node_hash(const pm_parser_t *parser, const pm_node_t *node) {
54
9.72k
    switch (PM_NODE_TYPE(node)) {
55
1.05k
        case PM_INTEGER_NODE:
  Branch (55:9): [True: 1.05k, False: 8.67k]
56
1.05k
            return integer_hash(&((const pm_integer_node_t *) node)->value);
57
48
        case PM_SOURCE_LINE_NODE: {
  Branch (57:9): [True: 48, False: 9.68k]
58
48
            int64_t line = (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
59
48
            return key_hash(PM_INTEGER_NODE, line < 0, (uint64_t) (line < 0 ? 
-line0
: line));
  Branch (59:68): [True: 0, False: 48]
60
0
        }
61
368
        case PM_FLOAT_NODE: {
  Branch (61:9): [True: 368, False: 9.36k]
62
368
            const pm_float_node_t *cast = (const pm_float_node_t *) node;
63
368
            double value = cast->value;
64
368
            if (value == 0.0) 
value = 0.017
; /* Normalize -0.0 to 0.0. */
  Branch (64:17): [True: 17, False: 351]
65
66
368
            uint64_t bits;
67
368
            memcpy(&bits, &value, sizeof(bits));
68
368
            return key_hash(PM_FLOAT_NODE, bits, 0);
69
0
        }
70
795
        case PM_RATIONAL_NODE: {
  Branch (70:9): [True: 795, False: 8.93k]
71
795
            const pm_rational_node_t *cast = (const pm_rational_node_t *) node;
72
795
            return key_hash(PM_RATIONAL_NODE, integer_hash(&cast->numerator), integer_hash(&cast->denominator));
73
0
        }
74
504
        case PM_IMAGINARY_NODE: {
  Branch (74:9): [True: 504, False: 9.22k]
75
504
            const pm_imaginary_node_t *cast = (const pm_imaginary_node_t *) node;
76
504
            return key_hash(PM_IMAGINARY_NODE, node_hash(parser, cast->numeric), 0);
77
0
        }
78
56
        case PM_STRING_NODE: {
  Branch (78:9): [True: 56, False: 9.67k]
79
56
            const pm_string_node_t *cast = (const pm_string_node_t *) node;
80
81
56
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
82
56
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (82:17): [True: 0, False: 56]
83
84
56
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
85
0
        }
86
7
        case PM_SOURCE_FILE_NODE: {
  Branch (86:9): [True: 7, False: 9.72k]
87
7
            const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
88
7
            return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->filepath), pm_string_length(&cast->filepath)), 0);
89
0
        }
90
24
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (90:9): [True: 24, False: 9.70k]
91
24
            const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
92
24
            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
6.81k
        case PM_SYMBOL_NODE: {
  Branch (94:9): [True: 6.81k, False: 2.91k]
95
6.81k
            const pm_symbol_node_t *cast = (const pm_symbol_node_t *) node;
96
97
6.81k
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
98
6.81k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING8
;
  Branch (98:17): [True: 8, False: 6.80k]
99
100
6.81k
            return key_hash(PM_SYMBOL_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
101
0
        }
102
3
        case PM_FALSE_NODE:
  Branch (102:9): [True: 3, False: 9.72k]
103
8
        case PM_NIL_NODE:
  Branch (103:9): [True: 5, False: 9.72k]
104
36
        case PM_SOURCE_ENCODING_NODE:
  Branch (104:9): [True: 28, False: 9.70k]
105
56
        case PM_TRUE_NODE:
  Branch (105:9): [True: 20, False: 9.70k]
106
56
            return key_hash(PM_NODE_TYPE(node), 0, 0);
107
0
        default:
  Branch (107:9): [True: 0, False: 9.72k]
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
9.72k
    }
111
9.72k
}
112
113
/**
114
 * Compare two values that can be compared with a simple numeric comparison.
115
 */
116
6.93k
#define COMPARE(left_, right_) (((left_) < (right_)) ? 
-1791
:
((left_) > (right_))6.14k
?
13.66k
:
02.48k
)
  Branch (116:33): [True: 760, False: 3.64k]
  Branch (116:61): [True: 3.64k, False: 0]
  Branch (116:33): [True: 18, False: 21]
  Branch (116:61): [True: 12, False: 9]
  Branch (116:33): [True: 13, False: 118]
  Branch (116:61): [True: 4, False: 114]
  Branch (116:33): [True: 0, False: 2.35k]
  Branch (116:61): [True: 0, False: 2.35k]
  Branch (116:33): [True: 0, False: 0]
  Branch (116:61): [True: 0, False: 0]
117
118
/**
119
 * Return the integer value of the given node as an int64_t.
120
 */
121
static int64_t
122
120
int64_value(const pm_parser_t *parser, const pm_node_t *node) {
123
120
    switch (PM_NODE_TYPE(node)) {
124
51
        case PM_INTEGER_NODE: {
  Branch (124:9): [True: 51, False: 69]
125
51
            const pm_integer_t *integer = &((const pm_integer_node_t *) node)->value;
126
51
            if (integer->length > 1) 
return 5
integer->negative5
?
INT64_MIN : 5
INT64_MAX5
;
  Branch (126:17): [True: 5, False: 46]
  Branch (126:45): [True: 1, False: 4]
127
128
46
            int64_t value = (int64_t) pm_integer_uint64_value(integer);
129
46
            return integer->negative ? 
-value4
:
value42
;
  Branch (129:20): [True: 4, False: 42]
130
51
        }
131
69
        case PM_SOURCE_LINE_NODE:
  Branch (131:9): [True: 69, False: 51]
132
69
            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: 120]
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
120
    }
137
120
}
138
139
/**
140
 * A comparison function for comparing two IntegerNode or SourceLineNode
141
 * instances.
142
 */
143
static int
144
1.23k
compare_integer_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
145
1.23k
    if (PM_NODE_TYPE_P(left, PM_SOURCE_LINE_NODE) || 
PM_NODE_TYPE_P1.21k
(right, PM_SOURCE_LINE_NODE)) {
146
39
        return COMPARE(int64_value(parser, left), int64_value(parser, right));
147
39
    }
148
149
1.19k
    const pm_integer_t *left_integer = &((const pm_integer_node_t *) left)->value;
150
1.19k
    const pm_integer_t *right_integer = &((const pm_integer_node_t *) right)->value;
151
1.19k
    return pm_integer_compare(left_integer, right_integer);
152
1.23k
}
153
154
/**
155
 * A comparison function for comparing two FloatNode instances.
156
 */
157
static int
158
131
compare_float_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
159
131
    return COMPARE(((const pm_float_node_t *) left)->value, ((const pm_float_node_t *) right)->value);
160
131
}
161
162
/**
163
 * Return a pointer to the string value of the given node.
164
 */
165
static const pm_string_t *
166
24.5k
string_value(const pm_node_t *node) {
167
24.5k
    switch (PM_NODE_TYPE(node)) {
168
118
        case PM_STRING_NODE:
  Branch (168:9): [True: 118, False: 24.4k]
169
118
            return &((const pm_string_node_t *) node)->unescaped;
170
0
        case PM_SOURCE_FILE_NODE:
  Branch (170:9): [True: 0, False: 24.5k]
171
0
            return &((const pm_source_file_node_t *) node)->filepath;
172
24.4k
        case PM_SYMBOL_NODE:
  Branch (172:9): [True: 24.4k, False: 118]
173
24.4k
            return &((const pm_symbol_node_t *) node)->unescaped;
174
0
        default:
  Branch (174:9): [True: 0, False: 24.5k]
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
24.5k
    }
178
24.5k
}
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
12.2k
compare_string_nodes(const pm_node_t *left, const pm_node_t *right, pm_node_flags_t mask) {
194
12.2k
    const pm_string_t *left_string = string_value(left);
195
12.2k
    const pm_string_t *right_string = string_value(right);
196
197
12.2k
    int result = pm_string_compare(left_string, right_string);
198
12.2k
    if (result != 0) 
return result9.91k
;
  Branch (198:9): [True: 9.91k, False: 2.35k]
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
2.35k
    return COMPARE(left->flags & mask, right->flags & mask);
206
12.2k
}
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
0
compare_regular_expression_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
215
0
    const pm_regular_expression_node_t *left_regexp = (const pm_regular_expression_node_t *) left;
216
0
    const pm_regular_expression_node_t *right_regexp = (const pm_regular_expression_node_t *) right;
217
218
0
    int result = pm_string_compare(&left_regexp->unescaped, &right_regexp->unescaped);
219
0
    if (result != 0) return result;
  Branch (219:9): [True: 0, False: 0]
220
221
0
    pm_node_flags_t mask = (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE;
222
0
    return COMPARE(left->flags & mask, right->flags & mask);
223
0
}
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
37.6k
static_literal_type(const pm_node_t *node) {
232
37.6k
    switch (PM_NODE_TYPE(node)) {
233
206
        case PM_SOURCE_LINE_NODE:
  Branch (233:9): [True: 206, False: 37.3k]
234
206
            return PM_INTEGER_NODE;
235
18
        case PM_SOURCE_FILE_NODE:
  Branch (235:9): [True: 18, False: 37.5k]
236
18
            return PM_STRING_NODE;
237
37.3k
        default:
  Branch (237:9): [True: 37.3k, False: 224]
238
37.3k
            return PM_NODE_TYPE(node);
239
37.6k
    }
240
37.6k
}
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
18.8k
compare_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
248
18.8k
    enum pm_node_type left_type = static_literal_type(left);
249
18.8k
    enum pm_node_type right_type = static_literal_type(right);
250
18.8k
    if (left_type != right_type) 
return 4.40k
COMPARE4.40k
(left_type, right_type);
  Branch (250:9): [True: 4.40k, False: 14.3k]
251
252
14.3k
    switch (left_type) {
253
1.23k
        case PM_INTEGER_NODE:
  Branch (253:9): [True: 1.23k, False: 13.1k]
254
1.23k
            return compare_integer_nodes(parser, left, right);
255
131
        case PM_FLOAT_NODE:
  Branch (255:9): [True: 131, False: 14.2k]
256
131
            return compare_float_nodes(parser, left, right);
257
304
        case PM_IMAGINARY_NODE:
  Branch (257:9): [True: 304, False: 14.0k]
258
304
            return compare_nodes(parser, ((const pm_imaginary_node_t *) left)->numeric, ((const pm_imaginary_node_t *) right)->numeric);
259
418
        case PM_RATIONAL_NODE: {
  Branch (259:9): [True: 418, False: 13.9k]
260
418
            const pm_rational_node_t *left_rational = (const pm_rational_node_t *) left;
261
418
            const pm_rational_node_t *right_rational = (const pm_rational_node_t *) right;
262
263
418
            int result = pm_integer_compare(&left_rational->denominator, &right_rational->denominator);
264
418
            if (result != 0) 
return result84
;
  Branch (264:17): [True: 84, False: 334]
265
266
334
            return pm_integer_compare(&left_rational->numerator, &right_rational->numerator);
267
418
        }
268
59
        case PM_STRING_NODE: {
  Branch (268:9): [True: 59, False: 14.3k]
269
59
            pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
270
59
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING0
;
  Branch (270:17): [True: 0, False: 59]
271
59
            return compare_string_nodes(left, right, mask);
272
418
        }
273
12.2k
        case PM_SYMBOL_NODE: {
  Branch (273:9): [True: 12.2k, False: 2.17k]
274
12.2k
            pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
275
12.2k
            if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) 
mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING27
;
  Branch (275:17): [True: 27, False: 12.1k]
276
12.2k
            return compare_string_nodes(left, right, mask);
277
418
        }
278
0
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (278:9): [True: 0, False: 14.3k]
279
0
            return compare_regular_expression_nodes(parser, left, right);
280
5
        case PM_FALSE_NODE:
  Branch (280:9): [True: 5, False: 14.3k]
281
9
        case PM_NIL_NODE:
  Branch (281:9): [True: 4, False: 14.3k]
282
10
        case PM_SOURCE_ENCODING_NODE:
  Branch (282:9): [True: 1, False: 14.3k]
283
31
        case PM_TRUE_NODE:
  Branch (283:9): [True: 21, False: 14.3k]
284
31
            return 0;
285
0
        default:
  Branch (285:9): [True: 0, False: 14.3k]
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
14.3k
    }
289
14.3k
}
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
6.87k
pm_static_literals_place(pm_static_literals_entry_t *entries, uint32_t mask, pm_static_literals_entry_t entry) {
299
6.87k
    uint32_t index = (uint32_t) (entry.hash & mask);
300
7.51k
    while (entries[index].node != NULL) {
  Branch (300:12): [True: 639, False: 6.87k]
301
639
        index = (index + 1) & mask;
302
639
    }
303
6.87k
    entries[index] = entry;
304
6.87k
}
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
1.35k
pm_static_literals_resize(const pm_parser_t *parser, pm_static_literals_t *literals, uint32_t capacity) {
313
1.35k
    pm_static_literals_entry_t *entries = xcalloc(capacity, sizeof(pm_static_literals_entry_t));
314
1.35k
    if (entries == NULL) 
PRISM_OOM0
();
  Branch (314:9): [True: 0, False: 1.35k]
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
1.35k
    uint32_t mask = capacity - 1;
320
321
1.35k
    if (literals->capacity == 0) {
  Branch (321:9): [True: 998, False: 360]
322
4.99k
        for (uint32_t index = 0; index < literals->size; 
index++3.99k
) {
  Branch (322:34): [True: 3.99k, False: 998]
323
3.99k
            pm_node_t *node = literals->nodes[index];
324
3.99k
            pm_static_literals_place(entries, mask, (pm_static_literals_entry_t) { .node = node, .hash = node_hash(parser, node) });
325
3.99k
        }
326
998
    } else {
327
6.12k
        for (uint32_t index = 0; index < literals->capacity; 
index++5.76k
) {
  Branch (327:34): [True: 5.76k, False: 360]
328
5.76k
            if (literals->entries[index].node != NULL) {
  Branch (328:17): [True: 2.88k, False: 2.88k]
329
2.88k
                pm_static_literals_place(entries, mask, literals->entries[index]);
330
2.88k
            }
331
5.76k
        }
332
333
360
        xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
334
360
    }
335
336
1.35k
    literals->entries = entries;
337
1.35k
    literals->capacity = capacity;
338
1.35k
}
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
33.8k
pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node, bool replace) {
348
33.8k
    switch (PM_NODE_TYPE(node)) {
349
68
        case PM_FALSE_NODE:
  Branch (349:9): [True: 68, False: 33.7k]
350
477
        case PM_FLOAT_NODE:
  Branch (350:9): [True: 409, False: 33.4k]
351
1.02k
        case PM_IMAGINARY_NODE:
  Branch (351:9): [True: 548, False: 33.2k]
352
2.46k
        case PM_INTEGER_NODE:
  Branch (352:9): [True: 1.43k, False: 32.3k]
353
2.48k
        case PM_NIL_NODE:
  Branch (353:9): [True: 21, False: 33.8k]
354
3.33k
        case PM_RATIONAL_NODE:
  Branch (354:9): [True: 857, False: 32.9k]
355
3.37k
        case PM_REGULAR_EXPRESSION_NODE:
  Branch (355:9): [True: 31, False: 33.7k]
356
3.40k
        case PM_SOURCE_ENCODING_NODE:
  Branch (356:9): [True: 35, False: 33.7k]
357
3.41k
        case PM_SOURCE_FILE_NODE:
  Branch (357:9): [True: 13, False: 33.8k]
358
3.47k
        case PM_SOURCE_LINE_NODE:
  Branch (358:9): [True: 58, False: 33.7k]
359
3.65k
        case PM_STRING_NODE:
  Branch (359:9): [True: 174, False: 33.6k]
360
17.6k
        case PM_SYMBOL_NODE:
  Branch (360:9): [True: 14.0k, False: 19.8k]
361
17.7k
        case PM_TRUE_NODE:
  Branch (361:9): [True: 114, False: 33.7k]
362
17.7k
            break;
363
16.0k
        default:
  Branch (363:9): [True: 16.0k, False: 17.7k]
364
16.0k
            return NULL;
365
33.8k
    }
366
367
17.7k
    if (literals->capacity == 0) {
  Branch (367:9): [True: 13.5k, False: 4.23k]
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
28.6k
        for (uint32_t index = 0; index < literals->size; 
index++15.1k
) {
  Branch (371:34): [True: 17.2k, False: 11.3k]
372
17.2k
            pm_node_t *existing = literals->nodes[index];
373
374
17.2k
            if (compare_nodes(parser, existing, node) == 0) {
  Branch (374:17): [True: 2.14k, False: 15.1k]
375
2.14k
                if (replace) 
literals->nodes[index] = node2.08k
;
  Branch (375:21): [True: 2.08k, False: 61]
376
2.14k
                return existing;
377
2.14k
            }
378
17.2k
        }
379
380
11.3k
        if (literals->size < PM_STATIC_LITERALS_INLINE_CAPACITY) {
  Branch (380:13): [True: 10.3k, False: 998]
381
10.3k
            literals->nodes[literals->size++] = node;
382
10.3k
            return NULL;
383
10.3k
        }
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
998
        pm_static_literals_resize(parser, literals, PM_STATIC_LITERALS_INLINE_CAPACITY * 4);
389
4.23k
    } else if (literals->size * 2 >= literals->capacity) {
  Branch (389:16): [True: 360, False: 3.87k]
390
        /* If we are out of space, we need to resize the hash. */
391
360
        pm_static_literals_resize(parser, literals, literals->capacity * 2);
392
360
    }
393
394
5.23k
    uint64_t hash = node_hash(parser, node);
395
5.23k
    uint32_t mask = literals->capacity - 1;
396
5.23k
    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
7.33k
    while (literals->entries[index].node != NULL) {
  Branch (403:12): [True: 3.32k, False: 4.01k]
404
3.32k
        const pm_static_literals_entry_t *entry = &literals->entries[index];
405
3.32k
        if (entry->hash == hash && 
compare_nodes(parser, entry->node, node) == 01.22k
)
break1.22k
;
  Branch (405:13): [True: 1.22k, False: 2.10k]
  Branch (405:36): [True: 1.22k, False: 0]
406
2.10k
        index = (index + 1) & mask;
407
2.10k
    }
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
5.23k
    pm_node_t *result = literals->entries[index].node;
413
414
5.23k
    if (result == NULL) {
  Branch (414:9): [True: 4.01k, False: 1.22k]
415
4.01k
        literals->size++;
416
4.01k
        literals->entries[index] = (pm_static_literals_entry_t) { .node = node, .hash = hash };
417
4.01k
    } else 
if (1.22k
replace1.22k
) {
  Branch (417:16): [True: 1.20k, False: 13]
418
1.20k
        literals->entries[index].node = node;
419
1.20k
    }
420
421
5.23k
    return result;
422
17.7k
}
423
424
/**
425
 * Free the internal memory associated with the given static literals set.
426
 */
427
void
428
21.6k
pm_static_literals_free(pm_static_literals_t *literals) {
429
21.6k
    if (literals->capacity > 0) xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
  Branch (429:9): [True: 998, False: 20.6k]
430
21.6k
}
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
102
pm_static_literal_positive_p(const pm_node_t *node) {
438
102
    switch (PM_NODE_TYPE(node)) {
439
0
        case PM_FLOAT_NODE:
  Branch (439:9): [True: 0, False: 102]
440
0
            return ((const pm_float_node_t *) node)->value > 0;
441
101
        case PM_INTEGER_NODE: ★ main-4: BR: False (1x)
  Branch (441:9): [True: 101, False: 1]
442
101
            return !((const pm_integer_node_t *) node)->value.negative;
443
1
        case PM_RATIONAL_NODE: ★ main-4: LINE (1x) + BR: True (1x)
  Branch (443:9): [True: 1, False: 101]
444
1
            return !((const pm_rational_node_t *) node)->numerator.negative; ★ main-4: LINE (1x)
445
0
        case PM_IMAGINARY_NODE:
  Branch (445:9): [True: 0, False: 102]
446
0
            return pm_static_literal_positive_p(((const pm_imaginary_node_t *) node)->numeric);
447
0
        default:
  Branch (447:9): [True: 0, False: 102]
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
102
    }
451
102
}
452
453
/**
454
 * Create a string-based representation of the given static literal.
455
 */
456
void
457
1.51k
pm_static_literal_inspect(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node) {
458
1.51k
    switch (PM_NODE_TYPE(node)) {
459
4
        case PM_FALSE_NODE: ★ main-4: LINE (4x) + BR: True (4x)
  Branch (459:9): [True: 4, False: 1.51k]
460
4
            pm_buffer_append_string(buffer, "false", 5); ★ main-4: LINE (4x)
461
4
            break; ★ main-4: LINE (4x)
462
53
        case PM_FLOAT_NODE: {
  Branch (462:9): [True: 53, False: 1.46k]
463
53
            const double value = ((const pm_float_node_t *) node)->value;
464
465
53
            if (PRISM_ISINF(value)) {
  Branch (465:17): [True: 0, False: 53]
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
53
            } else if (value == 0.0) {
  Branch (470:24): [True: 3, False: 50]
471
3
                if (parser->start[node->location.start] == '-') {
  Branch (471:21): [True: 0, False: 3]
472
0
                    pm_buffer_append_byte(buffer, '-');
473
0
                }
474
3
                pm_buffer_append_string(buffer, "0.0", 3);
475
50
            } else {
476
50
                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
50
                if (pm_buffer_index(buffer, '.') == SIZE_MAX) {
  Branch (481:21): [True: 47, False: 3]
482
47
                    size_t exponent_index = pm_buffer_index(buffer, 'e');
483
47
                    size_t index = exponent_index == SIZE_MAX ? pm_buffer_length(buffer) : 
exponent_index0
;
  Branch (483:36): [True: 47, False: 0]
484
47
                    pm_buffer_insert(buffer, index, ".0", 2);
485
47
                }
486
50
            }
487
488
53
            break;
489
0
        }
490
102
        case PM_IMAGINARY_NODE: {
  Branch (490:9): [True: 102, False: 1.41k]
491
102
            const pm_node_t *numeric = ((const pm_imaginary_node_t *) node)->numeric;
492
102
            pm_buffer_append_string(buffer, "(0", 2);
493
102
            if (pm_static_literal_positive_p(numeric)) 
pm_buffer_append_byte(buffer, '+')101
;
  Branch (493:17): [True: 101, False: 1]
494
102
            pm_static_literal_inspect(buffer, parser, numeric);
495
102
            if (PM_NODE_TYPE_P(numeric, PM_RATIONAL_NODE)) { ★ main-4: BR: (PM_NODE_TYPE(node_) == (type_)) → True (1x)
496
1
                pm_buffer_append_byte(buffer, '*'); ★ main-4: LINE (1x)
497
1
            } ★ main-4: LINE (1x)
498
102
            pm_buffer_append_string(buffer, "i)", 2);
499
102
            break;
500
0
        }
501
222
        case PM_INTEGER_NODE:
  Branch (501:9): [True: 222, False: 1.29k]
502
222
            pm_integer_string(buffer, &((const pm_integer_node_t *) node)->value);
503
222
            break;
504
4
        case PM_NIL_NODE:
  Branch (504:9): [True: 4, False: 1.51k]
505
4
            pm_buffer_append_string(buffer, "nil", 3);
506
4
            break;
507
102
        case PM_RATIONAL_NODE: {
  Branch (507:9): [True: 102, False: 1.41k]
508
102
            const pm_rational_node_t *rational = (const pm_rational_node_t *) node;
509
102
            pm_buffer_append_byte(buffer, '(');
510
102
            pm_integer_string(buffer, &rational->numerator);
511
102
            pm_buffer_append_byte(buffer, '/');
512
102
            pm_integer_string(buffer, &rational->denominator);
513
102
            pm_buffer_append_byte(buffer, ')');
514
102
            break;
515
0
        }
516
0
        case PM_REGULAR_EXPRESSION_NODE: {
  Branch (516:9): [True: 0, False: 1.51k]
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
1
        case PM_SOURCE_ENCODING_NODE:
  Branch (529:9): [True: 1, False: 1.51k]
530
1
            pm_buffer_append_format(buffer, "#<Encoding:%s>", parser->encoding->name);
531
1
            break;
532
0
        case PM_SOURCE_FILE_NODE: {
  Branch (532:9): [True: 0, False: 1.51k]
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
9
        case PM_SOURCE_LINE_NODE:
  Branch (539:9): [True: 9, False: 1.50k]
540
9
            pm_buffer_append_format(buffer, "%" PRIi32, pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line);
541
9
            break;
542
0
        case PM_STRING_NODE: {
  Branch (542:9): [True: 0, False: 1.51k]
543
0
            const pm_string_t *unescaped = &((const pm_string_node_t *) node)->unescaped;
544
0
            pm_buffer_append_byte(buffer, '"');
545
0
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
546
0
            pm_buffer_append_byte(buffer, '"');
547
0
            break;
548
0
        }
549
1.01k
        case PM_SYMBOL_NODE: {
  Branch (549:9): [True: 1.01k, False: 504]
550
1.01k
            const pm_string_t *unescaped = &((const pm_symbol_node_t *) node)->unescaped;
551
1.01k
            pm_buffer_append_byte(buffer, ':');
552
1.01k
            pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
553
1.01k
            break;
554
0
        }
555
7
        case PM_TRUE_NODE:
  Branch (555:9): [True: 7, False: 1.51k]
556
7
            pm_buffer_append_string(buffer, "true", 4);
557
7
            break;
558
0
        default:
  Branch (558:9): [True: 0, False: 1.51k]
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
1.51k
    }
562
1.51k
}