1#include "prism/internal/static_literals.h"
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"
29key_hash(uint64_t
type, uint64_t a, uint64_t b) {
30 const uint64_t key[3] = {
type, a, b };
31 return pm_xxh3((
const uint8_t *) key,
sizeof(key));
41 uint64_t magnitude = (integer->
length > 2)
42 ? pm_xxh3((
const uint8_t *) integer->
words.
values,
sizeof(uint32_t) * integer->
length)
44 return key_hash(PM_INTEGER_NODE, integer->
negative, magnitude);
53node_hash(
const pm_parser_t *parser,
const pm_node_t *node) {
54 switch (PM_NODE_TYPE(node)) {
56 return integer_hash(&((
const pm_integer_node_t *) node)->value);
57 case PM_SOURCE_LINE_NODE: {
58 int64_t line = (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->
location.
start, parser->start_line).line;
59 return key_hash(PM_INTEGER_NODE, line < 0, (uint64_t) (line < 0 ? -line : line));
62 const pm_float_node_t *
cast = (
const pm_float_node_t *) node;
63 double value =
cast->value;
64 if (value == 0.0) value = 0.0;
67 memcpy(&bits, &value,
sizeof(bits));
68 return key_hash(PM_FLOAT_NODE, bits, 0);
70 case PM_RATIONAL_NODE: {
71 const pm_rational_node_t *
cast = (
const pm_rational_node_t *) node;
72 return key_hash(PM_RATIONAL_NODE, integer_hash(&
cast->numerator), integer_hash(&
cast->denominator));
74 case PM_IMAGINARY_NODE: {
75 const pm_imaginary_node_t *
cast = (
const pm_imaginary_node_t *) node;
76 return key_hash(PM_IMAGINARY_NODE, node_hash(parser,
cast->numeric), 0);
78 case PM_STRING_NODE: {
79 const pm_string_node_t *
cast = (
const pm_string_node_t *) node;
81 pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
82 if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING;
84 return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&
cast->unescaped), pm_string_length(&
cast->unescaped)), node->
flags & mask);
86 case PM_SOURCE_FILE_NODE: {
87 const pm_source_file_node_t *
cast = (
const pm_source_file_node_t *) node;
88 return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&
cast->filepath), pm_string_length(&
cast->filepath)), 0);
90 case PM_REGULAR_EXPRESSION_NODE: {
91 const pm_regular_expression_node_t *
cast = (
const pm_regular_expression_node_t *) node;
92 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);
94 case PM_SYMBOL_NODE: {
95 const pm_symbol_node_t *
cast = (
const pm_symbol_node_t *) node;
97 pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
98 if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING;
100 return key_hash(PM_SYMBOL_NODE, pm_xxh3(pm_string_source(&
cast->unescaped), pm_string_length(&
cast->unescaped)), node->
flags & mask);
104 case PM_SOURCE_ENCODING_NODE:
106 return key_hash(PM_NODE_TYPE(node), 0, 0);
108 PRISM_ASSERT(
false &&
"unreachable");
116#define COMPARE(left_, right_) (((left_) < (right_)) ? -1 : ((left_) > (right_)) ? 1 : 0)
122int64_value(
const pm_parser_t *parser,
const pm_node_t *node) {
123 switch (PM_NODE_TYPE(node)) {
124 case PM_INTEGER_NODE: {
126 if (integer->
length > 1)
return integer->
negative ? INT64_MIN : INT64_MAX;
129 return integer->
negative ? -value : value;
131 case PM_SOURCE_LINE_NODE:
132 return (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->
location.
start, parser->start_line).line;
134 PRISM_ASSERT(
false &&
"unreachable");
144compare_integer_nodes(
const pm_parser_t *parser,
const pm_node_t *left,
const pm_node_t *right) {
145 if (PM_NODE_TYPE_P(left, PM_SOURCE_LINE_NODE) || PM_NODE_TYPE_P(right, PM_SOURCE_LINE_NODE)) {
146 return COMPARE(int64_value(parser, left), int64_value(parser, right));
151 return pm_integer_compare(left_integer, right_integer);
159 return COMPARE(((
const pm_float_node_t *) left)->value, ((
const pm_float_node_t *) right)->value);
166string_value(
const pm_node_t *node) {
167 switch (PM_NODE_TYPE(node)) {
169 return &((
const pm_string_node_t *) node)->unescaped;
170 case PM_SOURCE_FILE_NODE:
171 return &((
const pm_source_file_node_t *) node)->filepath;
173 return &((
const pm_symbol_node_t *) node)->unescaped;
175 PRISM_ASSERT(
false &&
"unreachable");
193compare_string_nodes(
const pm_node_t *left,
const pm_node_t *right, pm_node_flags_t mask) {
194 const pm_string_t *left_string = string_value(left);
195 const pm_string_t *right_string = string_value(right);
197 int result = pm_string_compare(left_string, right_string);
198 if (result != 0)
return result;
205 return COMPARE(left->
flags & mask, right->
flags & mask);
214compare_regular_expression_nodes(
PRISM_UNUSED const pm_parser_t *parser,
const pm_node_t *left,
const pm_node_t *right) {
215 const pm_regular_expression_node_t *left_regexp = (
const pm_regular_expression_node_t *) left;
216 const pm_regular_expression_node_t *right_regexp = (
const pm_regular_expression_node_t *) right;
219 if (result != 0)
return result;
221 pm_node_flags_t mask = (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE;
222 return COMPARE(left->
flags & mask, right->
flags & mask);
230static enum pm_node_type
231static_literal_type(
const pm_node_t *node) {
232 switch (PM_NODE_TYPE(node)) {
233 case PM_SOURCE_LINE_NODE:
234 return PM_INTEGER_NODE;
235 case PM_SOURCE_FILE_NODE:
236 return PM_STRING_NODE;
238 return PM_NODE_TYPE(node);
247compare_nodes(
const pm_parser_t *parser,
const pm_node_t *left,
const pm_node_t *right) {
248 enum pm_node_type left_type = static_literal_type(left);
249 enum pm_node_type right_type = static_literal_type(right);
250 if (left_type != right_type)
return COMPARE(left_type, right_type);
253 case PM_INTEGER_NODE:
254 return compare_integer_nodes(parser, left, right);
256 return compare_float_nodes(parser, left, right);
257 case PM_IMAGINARY_NODE:
258 return compare_nodes(parser, ((
const pm_imaginary_node_t *) left)->numeric, ((
const pm_imaginary_node_t *) right)->numeric);
259 case PM_RATIONAL_NODE: {
260 const pm_rational_node_t *left_rational = (
const pm_rational_node_t *) left;
261 const pm_rational_node_t *right_rational = (
const pm_rational_node_t *) right;
264 if (result != 0)
return result;
268 case PM_STRING_NODE: {
269 pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
270 if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING;
271 return compare_string_nodes(left, right, mask);
273 case PM_SYMBOL_NODE: {
274 pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
275 if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING;
276 return compare_string_nodes(left, right, mask);
278 case PM_REGULAR_EXPRESSION_NODE:
279 return compare_regular_expression_nodes(parser, left, right);
282 case PM_SOURCE_ENCODING_NODE:
286 PRISM_ASSERT(
false &&
"unreachable");
299 uint32_t index = (uint32_t) (entry.hash & mask);
300 while (entries[index].node != NULL) {
301 index = (index + 1) & mask;
303 entries[index] = entry;
314 if (entries == NULL) PRISM_OOM();
319 uint32_t mask = capacity - 1;
321 if (literals->capacity == 0) {
322 for (uint32_t index = 0; index < literals->size; index++) {
323 pm_node_t *node = literals->nodes[index];
327 for (uint32_t index = 0; index < literals->capacity; index++) {
328 if (literals->entries[index].node != NULL) {
329 pm_static_literals_place(entries, mask, literals->entries[index]);
336 literals->entries = entries;
337 literals->capacity = capacity;
348 switch (PM_NODE_TYPE(node)) {
351 case PM_IMAGINARY_NODE:
352 case PM_INTEGER_NODE:
354 case PM_RATIONAL_NODE:
355 case PM_REGULAR_EXPRESSION_NODE:
356 case PM_SOURCE_ENCODING_NODE:
357 case PM_SOURCE_FILE_NODE:
358 case PM_SOURCE_LINE_NODE:
367 if (literals->capacity == 0) {
371 for (uint32_t index = 0; index < literals->size; index++) {
372 pm_node_t *existing = literals->nodes[index];
374 if (compare_nodes(parser, existing, node) == 0) {
375 if (replace) literals->nodes[index] = node;
380 if (literals->size < PM_STATIC_LITERALS_INLINE_CAPACITY) {
381 literals->nodes[literals->size++] = node;
388 pm_static_literals_resize(parser, literals, PM_STATIC_LITERALS_INLINE_CAPACITY * 4);
389 }
else if (literals->size * 2 >= literals->capacity) {
391 pm_static_literals_resize(parser, literals, literals->capacity * 2);
394 uint64_t hash = node_hash(parser, node);
395 uint32_t mask = literals->capacity - 1;
396 uint32_t index = (uint32_t) (hash & mask);
403 while (literals->entries[index].node != NULL) {
405 if (entry->hash == hash && compare_nodes(parser, entry->node, node) == 0)
break;
406 index = (index + 1) & mask;
412 pm_node_t *result = literals->entries[index].node;
414 if (result == NULL) {
417 }
else if (replace) {
418 literals->entries[index].node = node;
437pm_static_literal_positive_p(
const pm_node_t *node) {
438 switch (PM_NODE_TYPE(node)) {
440 return ((
const pm_float_node_t *) node)->value > 0;
441 case PM_INTEGER_NODE:
442 return !((
const pm_integer_node_t *) node)->value.negative;
443 case PM_RATIONAL_NODE:
444 return !((
const pm_rational_node_t *) node)->numerator.negative;
445 case PM_IMAGINARY_NODE:
446 return pm_static_literal_positive_p(((
const pm_imaginary_node_t *) node)->numeric);
448 PRISM_ASSERT(
false &&
"unreachable");
458 switch (PM_NODE_TYPE(node)) {
460 PM_BUFFER_APPEND_LITERAL(buffer,
"false");
462 case PM_FLOAT_NODE: {
463 const double value = ((
const pm_float_node_t *) node)->value;
465 if (PRISM_ISINF(value)) {
467 pm_buffer_append_byte(buffer,
'-');
469 PM_BUFFER_APPEND_LITERAL(buffer,
"Infinity");
470 }
else if (value == 0.0) {
472 pm_buffer_append_byte(buffer,
'-');
474 PM_BUFFER_APPEND_LITERAL(buffer,
"0.0");
476 pm_buffer_append_format(buffer,
"%g", value);
481 if (pm_buffer_index(buffer,
'.') == SIZE_MAX) {
482 size_t exponent_index = pm_buffer_index(buffer,
'e');
483 size_t index = exponent_index == SIZE_MAX ? pm_buffer_length(buffer) : exponent_index;
484 pm_buffer_insert(buffer, index,
".0", 2);
490 case PM_IMAGINARY_NODE: {
491 const pm_node_t *numeric = ((
const pm_imaginary_node_t *) node)->numeric;
492 PM_BUFFER_APPEND_LITERAL(buffer,
"(0");
493 if (pm_static_literal_positive_p(numeric)) pm_buffer_append_byte(buffer,
'+');
494 pm_static_literal_inspect(buffer, parser, numeric);
495 if (PM_NODE_TYPE_P(numeric, PM_RATIONAL_NODE)) {
496 pm_buffer_append_byte(buffer,
'*');
498 PM_BUFFER_APPEND_LITERAL(buffer,
"i)");
501 case PM_INTEGER_NODE:
502 pm_integer_string(buffer, &((
const pm_integer_node_t *) node)->value);
505 PM_BUFFER_APPEND_LITERAL(buffer,
"nil");
507 case PM_RATIONAL_NODE: {
508 const pm_rational_node_t *rational = (
const pm_rational_node_t *) node;
509 pm_buffer_append_byte(buffer,
'(');
510 pm_integer_string(buffer, &rational->
numerator);
511 pm_buffer_append_byte(buffer,
'/');
513 pm_buffer_append_byte(buffer,
')');
516 case PM_REGULAR_EXPRESSION_NODE: {
517 const pm_string_t *unescaped = &((
const pm_regular_expression_node_t *) node)->unescaped;
518 pm_buffer_append_byte(buffer,
'/');
519 pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
520 pm_buffer_append_byte(buffer,
'/');
522 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) pm_buffer_append_byte(buffer,
'm');
523 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) pm_buffer_append_byte(buffer,
'i');
524 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) pm_buffer_append_byte(buffer,
'x');
525 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) pm_buffer_append_byte(buffer,
'n');
529 case PM_SOURCE_ENCODING_NODE:
530 pm_buffer_append_format(buffer,
"#<Encoding:%s>", parser->encoding->name);
532 case PM_SOURCE_FILE_NODE: {
533 const pm_string_t *filepath = &((
const pm_source_file_node_t *) node)->filepath;
534 pm_buffer_append_byte(buffer,
'"');
535 pm_buffer_append_source(buffer, pm_string_source(filepath), pm_string_length(filepath), PM_BUFFER_ESCAPING_RUBY);
536 pm_buffer_append_byte(buffer,
'"');
539 case PM_SOURCE_LINE_NODE:
540 pm_buffer_append_format(buffer,
"%" PRIi32, pm_line_offset_list_line_column(&parser->line_offsets, node->
location.
start, parser->start_line).line);
542 case PM_STRING_NODE: {
543 const pm_string_t *unescaped = &((
const pm_string_node_t *) node)->unescaped;
544 pm_buffer_append_byte(buffer,
'"');
545 pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
546 pm_buffer_append_byte(buffer,
'"');
549 case PM_SYMBOL_NODE: {
550 const pm_string_t *unescaped = &((
const pm_symbol_node_t *) node)->unescaped;
551 pm_buffer_append_byte(buffer,
':');
552 pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
556 PM_BUFFER_APPEND_LITERAL(buffer,
"true");
559 PRISM_ASSERT(
false &&
"unreachable");
#define xcalloc
Old name of ruby_xcalloc.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
static PRISM_INLINE uint64_t pm_integer_uint64_value(const pm_integer_t *integer)
Return the magnitude of an integer whose length is at most 2, which always fits in a uint64_t.
VALUE type(ANYARGS)
ANYARGS-ed function type.
T * cast(pm_node_t *node) noexcept
Cast the given node, which must not be null and must be of the given struct, to that struct.
A structure represents an arbitrary-sized integer.
uint32_t length
The number of words in the magnitude, with no leading zero words.
uint32_t * values
The words of an integer whose length is greater than 2.
bool negative
Whether or not the integer is negative.
uint32_t value[2]
The words of an integer whose length is at most 2.
union pm_integer_t::@344106104334032307171003340315163164214337104357 words
The words of the magnitude.
uint32_t start
The offset of the location from the start of the source.
pm_node_flags_t flags
This represents any flags on the node.
pm_location_t location
This is the location of the node in the source.
pm_integer_t denominator
RationalNode::denominator.
pm_integer_t numerator
RationalNode::numerator.
pm_string_t unescaped
RegularExpressionNode::unescaped.
A view of a sequence of bytes.
#define PRISM_UNUSED
GCC will warn if you specify a function or parameter that is unused at runtime.