20#define PUSH_ADJUST(seq, location, label) \
21 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
23#define PUSH_ADJUST_RESTORE(seq, label) \
24 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
26#define PUSH_INSN(seq, location, insn) \
27 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
29#define PUSH_INSN1(seq, location, insn, op1) \
30 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
32#define PUSH_INSN2(seq, location, insn, op1, op2) \
33 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
35#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
36 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
38#define PUSH_INSNL(seq, location, insn, label) \
39 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
41#define PUSH_LABEL(seq, label) \
42 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
44#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
45 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
47#define PUSH_SEND(seq, location, id, argc) \
48 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
50#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
51 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
53#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
54 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
56#define PUSH_CALL(seq, location, id, argc) \
57 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
59#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
60 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
62#define PUSH_TRACE(seq, event) \
63 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
65#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
66 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
68#define PUSH_SEQ(seq1, seq2) \
69 APPEND_LIST((seq1), (seq2))
71#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
73 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
74 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
75 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
84pm_iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
86 if (iseq_local_block_param_p(iseq, idx, level)) {
87 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
90 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
92 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
96pm_iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
98 if (iseq_local_block_param_p(iseq, idx, level)) {
99 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
102 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
104 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
107#define PUSH_GETLOCAL(seq, location, idx, level) \
108 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
110#define PUSH_SETLOCAL(seq, location, idx, level) \
111 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
117#define OLD_ISEQ NEW_ISEQ
120#define NEW_ISEQ(node, name, type, line_no) \
121 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
123#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
126#define NEW_CHILD_ISEQ(node, name, type, line_no) \
127 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
129#define PM_COMPILE(node) \
130 pm_compile_node(iseq, (node), ret, popped, scope_node)
132#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
133 pm_compile_node(iseq, (node), _ret, popped, scope_node)
135#define PM_COMPILE_POPPED(node) \
136 pm_compile_node(iseq, (node), ret, true, scope_node)
138#define PM_COMPILE_NOT_POPPED(node) \
139 pm_compile_node(iseq, (node), ret, false, scope_node)
141#define PM_NODE_START_LOCATION(parser, node) \
142 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
144#define PM_NODE_END_LOCATION(parser, node) \
145 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
147#define PM_LOCATION_START_LOCATION(parser, location, id) \
148 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
150#define PM_NODE_START_LINE_COLUMN(parser, node) \
151 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
153#define PM_NODE_END_LINE_COLUMN(parser, node) \
154 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
156#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
157 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
178 if (integer->
values == NULL) {
183 unsigned char *bytes = (
unsigned char *) RSTRING_PTR(
string);
185 size_t offset = integer->
length * 8;
186 for (
size_t value_index = 0; value_index < integer->
length; value_index++) {
187 uint32_t value = integer->
values[value_index];
189 for (
int index = 0; index < 8; index++) {
190 int byte = (value >> (4 * index)) & 0xf;
191 bytes[--offset] =
byte < 10 ?
byte +
'0' :
byte - 10 +
'a';
199 result =
rb_funcall(result, rb_intern(
"-@"), 0);
211 return parse_integer_value(&node->
value);
246 VALUE imaginary_part;
247 switch (PM_NODE_TYPE(node->
numeric)) {
248 case PM_FLOAT_NODE: {
252 case PM_INTEGER_NODE: {
256 case PM_RATIONAL_NODE: {
261 rb_bug(
"Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->
numeric)));
264 return rb_complex_raw(
INT2FIX(0), imaginary_part);
270 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), scope_node->encoding);
283 if (node->
flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
284 encoding = rb_ascii8bit_encoding();
286 else if (node->
flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
287 encoding = rb_utf8_encoding();
293 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
301 if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
302 encoding = rb_ascii8bit_encoding();
304 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
305 encoding = rb_utf8_encoding();
308 encoding = scope_node->encoding;
311 VALUE value = rb_enc_literal_str((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
314 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
315 int line_number = pm_node_line_number(scope_node->parser, node);
316 value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
326 if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
327 encoding = rb_utf8_encoding();
329 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
330 encoding = rb_ascii8bit_encoding();
332 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
333 encoding = rb_usascii_encoding();
336 encoding = scope_node->encoding;
339 return rb_intern3((
const char *) pm_string_source(&symbol->
unescaped), pm_string_length(&symbol->
unescaped), encoding);
343pm_optimizable_range_item_p(
const pm_node_t *node)
345 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
350parse_regexp_error(
rb_iseq_t *iseq, int32_t line_number,
const char *fmt, ...)
354 VALUE error = rb_syntax_error_append(
Qnil, rb_iseq_path(iseq), line_number, -1, NULL,
"%" PRIsVALUE, args);
366 if (explicit_regexp_encoding != NULL) {
367 encoding = explicit_regexp_encoding;
369 else if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
370 encoding = rb_ascii8bit_encoding();
372 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
373 encoding = rb_utf8_encoding();
376 encoding = implicit_regexp_encoding;
379 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
380 VALUE error = rb_reg_check_preprocess(
string);
382 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node),
"%" PRIsVALUE,
rb_obj_as_string(error));
391 for (
size_t index = 0; index < nodes->
size; index++) {
395 switch (PM_NODE_TYPE(part)) {
397 if (implicit_regexp_encoding != NULL) {
399 string = parse_regexp_string_part(iseq, scope_node, part, &((
const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
402 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
403 VALUE error = rb_reg_check_preprocess(
string);
404 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part),
"%" PRIsVALUE,
rb_obj_as_string(error));
408 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
411 case PM_INTERPOLATED_STRING_NODE:
412 string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
414 case PM_EMBEDDED_STATEMENTS_NODE: {
416 string = pm_static_literal_concat(iseq, &cast->
statements->
body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
420 RUBY_ASSERT(
false &&
"unexpected node type in pm_static_literal_concat");
424 if (current !=
Qnil) {
432 return top ? rb_fstring(current) : current;
435#define RE_OPTION_ENCODING_SHIFT 8
436#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
437#define ARG_ENCODING_NONE 32
438#define ARG_ENCODING_FIXED 16
439#define ENC_ASCII8BIT 1
441#define ENC_Windows_31J 3
456 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
457 flags |= ARG_ENCODING_NONE;
460 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
461 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
464 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
465 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
468 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
469 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
472 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
473 flags |= ONIG_OPTION_IGNORECASE;
476 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
477 flags |= ONIG_OPTION_MULTILINE;
480 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
481 flags |= ONIG_OPTION_EXTEND;
487#undef RE_OPTION_ENCODING_SHIFT
488#undef RE_OPTION_ENCODING
489#undef ARG_ENCODING_FIXED
490#undef ARG_ENCODING_NONE
493#undef ENC_Windows_31J
499 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
500 return rb_ascii8bit_encoding();
502 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
503 return rb_utf8_encoding();
505 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
506 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
508 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
509 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
519 VALUE errinfo = rb_errinfo();
521 int32_t line_number = pm_node_line_number(scope_node->parser, node);
522 VALUE regexp = rb_reg_compile(
string, parse_regexp_flags(node), (
const char *) pm_string_source(&scope_node->parser->
filepath), line_number);
525 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
526 rb_set_errinfo(errinfo);
528 parse_regexp_error(iseq, line_number,
"%" PRIsVALUE, message);
539 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
540 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
542 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
543 return parse_regexp(iseq, scope_node, node,
string);
549 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
550 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
552 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
553 return parse_regexp(iseq, scope_node, node,
string);
562 size_t parts_size = parts->
size;
563 bool interpolated =
false;
565 if (parts_size > 0) {
569 for (
size_t index = 0; index < parts_size; index++) {
572 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
576 if (implicit_regexp_encoding == NULL) {
577 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
580 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
583 if (
RTEST(current_string)) {
584 current_string =
rb_str_concat(current_string, string_value);
587 current_string = string_value;
588 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
595 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
603 if (implicit_regexp_encoding == NULL) {
604 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
607 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
610 if (
RTEST(current_string)) {
611 current_string =
rb_str_concat(current_string, string_value);
614 current_string = string_value;
615 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
619 if (!
RTEST(current_string)) {
622 if (implicit_regexp_encoding != NULL) {
623 if (explicit_regexp_encoding != NULL) {
624 encoding = explicit_regexp_encoding;
627 encoding = rb_ascii8bit_encoding();
630 encoding = implicit_regexp_encoding;
634 encoding = scope_node->encoding;
637 if (parts_size == 1) {
638 current_string = rb_enc_str_new(NULL, 0, encoding);
642 if (
RTEST(current_string)) {
643 VALUE operand = rb_fstring(current_string);
644 PUSH_INSN1(ret, current_location, putobject, operand);
648 PM_COMPILE_NOT_POPPED(part);
650 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
651 PUSH_INSN(ret, current_location, dup);
654 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
655 PUSH_INSN1(ret, current_location, objtostring, callinfo);
658 PUSH_INSN(ret, current_location, anytostring);
660 current_string =
Qnil;
666 if (
RTEST(current_string)) {
667 current_string = rb_fstring(current_string);
669 if (stack_size == 0 && interpolated) {
670 PUSH_INSN1(ret, current_location, putstring, current_string);
673 PUSH_INSN1(ret, current_location, putobject, current_string);
676 current_string =
Qnil;
681 PUSH_INSN(ret, *node_location, putnil);
690 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
691 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
693 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding);
694 PUSH_INSN2(ret, *node_location, toregexp,
INT2FIX(parse_regexp_flags(node) & 0xFF),
INT2FIX(length));
701 size_t length = pm_string_length(filepath);
705 return rb_enc_interned_str((
const char *) pm_string_source(filepath), length, filepath_encoding);
708 return rb_fstring_lit(
"<compiled>");
717pm_static_literal_string(
rb_iseq_t *iseq,
VALUE string,
int line_number)
719 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
720 return rb_str_with_debug_created_info(
string, rb_iseq_path(iseq), line_number);
723 return rb_fstring(
string);
737 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
739 switch (PM_NODE_TYPE(node)) {
740 case PM_ARRAY_NODE: {
745 for (
size_t index = 0; index < elements->
size; index++) {
746 rb_ary_push(value, pm_static_literal_value(iseq, elements->
nodes[index], scope_node));
761 for (
size_t index = 0; index < elements->
size; index++) {
764 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
768 VALUE value = rb_hash_new_with_size(elements->
size);
775 case PM_IMAGINARY_NODE:
777 case PM_INTEGER_NODE:
779 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
781 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
783 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
785 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
787 case PM_INTERPOLATED_STRING_NODE: {
789 int line_number = pm_node_line_number(scope_node->parser, node);
790 return pm_static_literal_string(iseq,
string, line_number);
792 case PM_INTERPOLATED_SYMBOL_NODE: {
794 VALUE string = pm_static_literal_concat(iseq, &cast->
parts, scope_node, NULL, NULL,
true);
796 return ID2SYM(rb_intern_str(
string));
798 case PM_MATCH_LAST_LINE_NODE: {
800 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
804 case PM_RATIONAL_NODE:
806 case PM_REGULAR_EXPRESSION_NODE: {
808 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
810 case PM_SOURCE_ENCODING_NODE:
811 return rb_enc_from_encoding(scope_node->encoding);
812 case PM_SOURCE_FILE_NODE: {
814 return pm_source_file_value(cast, scope_node);
816 case PM_SOURCE_LINE_NODE:
817 return INT2FIX(pm_node_line_number(scope_node->parser, node));
818 case PM_STRING_NODE: {
820 return parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
827 rb_bug(
"Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
838 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
839 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
842 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
843 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
852#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
866 if (!then_label) then_label = label;
867 else if (!else_label) else_label = label;
869 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
871 if (LIST_INSN_SIZE_ONE(seq)) {
872 INSN *insn = (
INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
873 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
return;
876 if (!label->refcnt) {
877 if (popped) PUSH_INSN(ret, location, putnil);
880 PUSH_LABEL(seq, label);
892 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
893 PM_COMPILE_NOT_POPPED(node);
896 PUSH_INSN1(ret, location, getglobal, operand);
898 PUSH_SEND(ret, location, idEq,
INT2FIX(1));
899 if (popped) PUSH_INSN(ret, location, pop);
912 int again = !(flip_flop_node->
base.
flags & PM_RANGE_FLAGS_EXCLUDE_END);
914 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
917 PUSH_INSN2(ret, location, getspecial, key,
INT2FIX(0));
918 PUSH_INSNL(ret, location, branchif, lend);
920 if (flip_flop_node->
left) {
921 pm_compile_flip_flop_bound(iseq, flip_flop_node->
left, ret, popped, scope_node);
924 PUSH_INSN(ret, location, putnil);
927 PUSH_INSNL(ret, location, branchunless, else_label);
928 PUSH_INSN1(ret, location, putobject,
Qtrue);
929 PUSH_INSN1(ret, location, setspecial, key);
931 PUSH_INSNL(ret, location, jump, then_label);
934 PUSH_LABEL(ret, lend);
935 if (flip_flop_node->
right) {
936 pm_compile_flip_flop_bound(iseq, flip_flop_node->
right, ret, popped, scope_node);
939 PUSH_INSN(ret, location, putnil);
942 PUSH_INSNL(ret, location, branchunless, then_label);
943 PUSH_INSN1(ret, location, putobject,
Qfalse);
944 PUSH_INSN1(ret, location, setspecial, key);
945 PUSH_INSNL(ret, location, jump, then_label);
956 switch (PM_NODE_TYPE(cond)) {
959 pm_compile_logical(iseq, ret, cast->
left, NULL, else_label, popped, scope_node);
966 pm_compile_logical(iseq, ret, cast->
left, then_label, NULL, popped, scope_node);
973 PUSH_INSNL(ret, location, jump, else_label);
976 case PM_IMAGINARY_NODE:
977 case PM_INTEGER_NODE:
979 case PM_RATIONAL_NODE:
980 case PM_REGULAR_EXPRESSION_NODE:
984 PUSH_INSNL(ret, location, jump, then_label);
986 case PM_FLIP_FLOP_NODE:
987 pm_compile_flip_flop((
const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
989 case PM_DEFINED_NODE: {
991 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
true);
995 DECL_ANCHOR(cond_seq);
996 pm_compile_node(iseq, cond, cond_seq,
false, scope_node);
998 if (LIST_INSN_SIZE_ONE(cond_seq)) {
999 INSN *insn = (
INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1001 if (insn->insn_id == BIN(putobject)) {
1002 if (
RTEST(insn->operands[0])) {
1003 PUSH_INSNL(ret, location, jump, then_label);
1008 PUSH_INSNL(ret, location, jump, else_label);
1014 PUSH_SEQ(ret, cond_seq);
1019 PUSH_INSNL(ret, location, branchunless, else_label);
1020 PUSH_INSNL(ret, location, jump, then_label);
1030 LABEL *then_label = NEW_LABEL(location.
line);
1031 LABEL *else_label = NEW_LABEL(location.
line);
1032 LABEL *end_label = NULL;
1034 DECL_ANCHOR(cond_seq);
1035 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label,
false, scope_node);
1036 PUSH_SEQ(ret, cond_seq);
1041 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1042 conditional_location = pm_code_location(scope_node, node);
1043 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location,
type == PM_IF_NODE ?
"if" :
"unless");
1046 if (then_label->refcnt) {
1047 PUSH_LABEL(ret, then_label);
1049 DECL_ANCHOR(then_seq);
1051 if (statements != NULL) {
1052 pm_compile_node(iseq, (
const pm_node_t *) statements, then_seq, popped, scope_node);
1055 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1058 if (else_label->refcnt) {
1060 if (PM_BRANCH_COVERAGE_P(iseq)) {
1063 if (statements != NULL) {
1064 branch_location = pm_code_location(scope_node, (
const pm_node_t *) statements);
1065 }
else if (
type == PM_IF_NODE) {
1066 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1068 .beg_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column },
1069 .end_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column }
1072 branch_location = conditional_location;
1075 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
type == PM_IF_NODE ?
"then" :
"else", branches);
1078 end_label = NEW_LABEL(location.
line);
1079 PUSH_INSNL(then_seq, location, jump, end_label);
1080 if (!popped) PUSH_INSN(then_seq, location, pop);
1083 PUSH_SEQ(ret, then_seq);
1086 if (else_label->refcnt) {
1087 PUSH_LABEL(ret, else_label);
1089 DECL_ANCHOR(else_seq);
1091 if (subsequent != NULL) {
1092 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1095 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1099 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1102 if (subsequent == NULL) {
1103 branch_location = conditional_location;
1104 }
else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1108 branch_location = pm_code_location(scope_node, (
const pm_node_t *) subsequent);
1111 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1,
type == PM_IF_NODE ?
"else" :
"then", branches);
1114 PUSH_SEQ(ret, else_seq);
1118 PUSH_LABEL(ret, end_label);
1132 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1133 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1134 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1136 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.
line);
1137 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.
line);
1138 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.
line);
1139 LABEL *end_label = NEW_LABEL(location.
line);
1140 LABEL *adjust_label = NEW_LABEL(location.
line);
1142 LABEL *next_catch_label = NEW_LABEL(location.
line);
1143 LABEL *tmp_label = NULL;
1149 push_ensure_entry(iseq, &enl, NULL, NULL);
1152 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1153 tmp_label = NEW_LABEL(location.
line);
1154 PUSH_INSNL(ret, location, jump, tmp_label);
1158 PUSH_INSNL(ret, location, jump, next_label);
1161 PUSH_LABEL(ret, adjust_label);
1162 PUSH_INSN(ret, location, putnil);
1163 PUSH_LABEL(ret, next_catch_label);
1164 PUSH_INSN(ret, location, pop);
1165 PUSH_INSNL(ret, location, jump, next_label);
1166 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1168 PUSH_LABEL(ret, redo_label);
1171 if (PM_BRANCH_COVERAGE_P(iseq)) {
1173 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location,
type == PM_WHILE_NODE ?
"while" :
"until");
1175 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (
const pm_node_t *) statements) : loop_location;
1176 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
"body", branches);
1179 if (statements != NULL) PM_COMPILE_POPPED((
const pm_node_t *) statements);
1180 PUSH_LABEL(ret, next_label);
1182 if (
type == PM_WHILE_NODE) {
1183 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1185 else if (
type == PM_UNTIL_NODE) {
1186 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1189 PUSH_LABEL(ret, end_label);
1190 PUSH_ADJUST_RESTORE(ret, adjust_label);
1191 PUSH_INSN(ret, location, putnil);
1193 PUSH_LABEL(ret, break_label);
1194 if (popped) PUSH_INSN(ret, location, pop);
1196 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1197 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1198 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1200 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1201 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1202 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1203 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1215 st_data_t local_index;
1218 for (level = 0; level < start_depth; level++) {
1219 scope_node = scope_node->previous;
1222 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1225 if (scope_node->previous) {
1226 scope_node = scope_node->previous;
1231 rb_bug(
"Local with constant_id %u does not exist", (
unsigned int) constant_id);
1235 lindex.level = level;
1236 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1250 rb_bug(
"constant_id out of range: %u", (
unsigned int)constant_id);
1252 return scope_node->constants[constant_id - 1];
1258 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1259 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1261 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1262 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1264 isolated_depth ? isolated_depth + 1 : 0,
1265 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1268 pm_scope_node_destroy(node);
1270 rb_jump_tag(error_state);
1272 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1279 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1285 return VM_DEFINECLASS_FLAG_SCOPED;
1289 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1290 return VM_DEFINECLASS_FLAG_SCOPED;
1295 PUSH_INSN1(ret, *node_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1305pm_compile_call_and_or_write_node(
rb_iseq_t *iseq,
bool and_node,
const pm_node_t *receiver,
const pm_node_t *value,
pm_constant_id_t write_name,
pm_constant_id_t read_name,
bool safe_nav,
const pm_node_location_t *node_location,
LINK_ANCHOR *
const ret,
bool popped,
pm_scope_node_t *scope_node)
1309 LABEL *lcfin = NEW_LABEL(location.
line);
1310 LABEL *lskip = NULL;
1312 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1313 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1315 PM_COMPILE_NOT_POPPED(receiver);
1317 lskip = NEW_LABEL(location.
line);
1318 PUSH_INSN(ret, location, dup);
1319 PUSH_INSNL(ret, location, branchnil, lskip);
1322 PUSH_INSN(ret, location, dup);
1323 PUSH_SEND_WITH_FLAG(ret, location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
1324 if (!popped) PUSH_INSN(ret, location, dup);
1327 PUSH_INSNL(ret, location, branchunless, lcfin);
1330 PUSH_INSNL(ret, location, branchif, lcfin);
1333 if (!popped) PUSH_INSN(ret, location, pop);
1334 PM_COMPILE_NOT_POPPED(value);
1337 PUSH_INSN(ret, location, swap);
1338 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
1341 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1342 PUSH_SEND_WITH_FLAG(ret, location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
1343 PUSH_INSNL(ret, location, jump, lfin);
1345 PUSH_LABEL(ret, lcfin);
1346 if (!popped) PUSH_INSN(ret, location, swap);
1348 PUSH_LABEL(ret, lfin);
1350 if (lskip && popped) PUSH_LABEL(ret, lskip);
1351 PUSH_INSN(ret, location, pop);
1352 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1365 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1371 const int max_stack_length = 0x100;
1372 const unsigned int min_tmp_hash_length = 0x800;
1374 int stack_length = 0;
1375 bool first_chunk =
true;
1381 bool static_literal =
false;
1383 DECL_ANCHOR(anchor);
1386#define FLUSH_CHUNK \
1387 if (stack_length) { \
1388 if (first_chunk) { \
1389 PUSH_SEQ(ret, anchor); \
1390 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1391 first_chunk = false; \
1394 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1395 PUSH_INSN(ret, location, swap); \
1396 PUSH_SEQ(ret, anchor); \
1397 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1399 INIT_ANCHOR(anchor); \
1403 for (
size_t index = 0; index < elements->
size; index++) {
1406 switch (PM_NODE_TYPE(element)) {
1407 case PM_ASSOC_NODE: {
1410 (shareability == 0) &&
1411 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1412 (!static_literal && ((index + min_tmp_hash_length) < elements->
size)) ||
1413 (first_chunk && stack_length == 0)
1418 while (index + count < elements->size && PM_NODE_FLAG_P(elements->
nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1420 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1426 for (
size_t tmp_end = index + count; index < tmp_end; index++) {
1430 pm_static_literal_value(iseq, assoc->key, scope_node),
1431 pm_static_literal_value(iseq, assoc->value, scope_node)
1446 PUSH_INSN1(ret, location, duphash, hash);
1447 first_chunk =
false;
1450 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1451 PUSH_INSN(ret, location, swap);
1452 PUSH_INSN1(ret, location, putobject, hash);
1453 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1459 static_literal =
true;
1463 static_literal =
false;
1468 if (shareability == 0) {
1469 pm_compile_node(iseq, element, anchor,
false, scope_node);
1473 pm_compile_shareable_constant_value(iseq, assoc->
key, shareability, path, ret, scope_node,
false);
1474 pm_compile_shareable_constant_value(iseq, assoc->
value, shareability, path, ret, scope_node,
false);
1477 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1480 case PM_ASSOC_SPLAT_NODE: {
1484 bool empty_hash = assoc_splat->
value != NULL && (
1485 (PM_NODE_TYPE_P(assoc_splat->
value, PM_HASH_NODE) && ((
const pm_hash_node_t *) assoc_splat->
value)->elements.size == 0) ||
1486 PM_NODE_TYPE_P(assoc_splat->
value, PM_NIL_NODE)
1489 bool first_element = first_chunk && stack_length == 0;
1490 bool last_element = index == elements->
size - 1;
1491 bool only_element = first_element && last_element;
1494 if (only_element && argument) {
1501 PUSH_INSN(ret, location, putnil);
1503 else if (first_element) {
1506 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1513 if (only_element && argument) {
1519 if (shareability == 0) {
1520 PM_COMPILE_NOT_POPPED(element);
1523 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1531 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1533 if (first_element) {
1534 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1537 PUSH_INSN(ret, location, swap);
1540 if (shareability == 0) {
1541 PM_COMPILE_NOT_POPPED(element);
1544 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1547 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1551 first_chunk =
false;
1552 static_literal =
false;
1556 RUBY_ASSERT(
"Invalid node type for hash" &&
false);
1565#define SPLATARRAY_FALSE 0
1566#define SPLATARRAY_TRUE 1
1567#define DUP_SINGLE_KW_SPLAT 2
1576 bool has_splat =
false;
1577 bool has_keyword_splat =
false;
1579 if (arguments_node == NULL) {
1580 if (*flags & VM_CALL_FCALL) {
1581 *flags |= VM_CALL_VCALL;
1586 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1590 int post_splat_counter = 0;
1594 switch (PM_NODE_TYPE(argument)) {
1596 case PM_KEYWORD_HASH_NODE: {
1600 if (has_keyword_splat || has_splat) {
1601 *flags |= VM_CALL_KW_SPLAT;
1602 has_keyword_splat =
true;
1604 if (elements->
size > 1 || !(elements->
size == 1 && PM_NODE_TYPE_P(elements->
nodes[0], PM_ASSOC_SPLAT_NODE))) {
1608 *flags |= VM_CALL_KW_SPLAT_MUT;
1609 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1611 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1612 *flags |= VM_CALL_KW_SPLAT_MUT;
1613 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1614 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1615 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1616 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1619 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1625 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1630 VALUE stored_indices = rb_hash_new();
1634 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1639 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1640 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1645 if (!
NIL_P(stored_index)) {
1653 rb_hash_aset(stored_indices, keyword,
ULONG2NUM(element_index));
1659 *flags |= VM_CALL_KWARG;
1661 VALUE *keywords = (*kw_arg)->keywords;
1662 (*kw_arg)->references = 0;
1663 (*kw_arg)->keyword_len = (int) size;
1665 size_t keyword_index = 0;
1666 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1671 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1675 PM_COMPILE(assoc->value);
1684 *flags |= VM_CALL_KW_SPLAT;
1686 size_t size = elements->
size;
1691 *flags |= VM_CALL_KW_SPLAT_MUT;
1694 for (
size_t element_index = 0; element_index < size; element_index++) {
1696 PM_COMPILE_NOT_POPPED(assoc->key);
1697 PM_COMPILE_NOT_POPPED(assoc->value);
1700 PUSH_INSN1(ret, location, newhash,
INT2FIX(size * 2));
1705 case PM_SPLAT_NODE: {
1706 *flags |= VM_CALL_ARGS_SPLAT;
1710 PM_COMPILE_NOT_POPPED(splat_node->
expression);
1713 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1714 PUSH_GETLOCAL(ret, location, index.index, index.level);
1717 bool first_splat = !has_splat;
1725 if (index + 1 < arguments->
size || has_regular_blockarg) {
1726 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ?
Qtrue :
Qfalse);
1727 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1735 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1745 PUSH_INSN(ret, location, concattoarray);
1749 post_splat_counter = 0;
1753 case PM_FORWARDING_ARGUMENTS_NODE: {
1754 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1756 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1757 *flags |= VM_CALL_FORWARDING;
1759 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1760 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1767 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1774 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1775 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1776 PUSH_INSN1(ret, location, splatarray,
Qtrue);
1779 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1780 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1783 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1784 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(and_local.level));
1785 PUSH_INSN(ret, location, splatkw);
1790 post_splat_counter++;
1791 PM_COMPILE_NOT_POPPED(argument);
1811 if (index == arguments->
size - 1) {
1813 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1818 switch (PM_NODE_TYPE(next_arg)) {
1820 case PM_KEYWORD_HASH_NODE: {
1821 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1822 PUSH_INSN(ret, location, concatarray);
1825 case PM_SPLAT_NODE: {
1826 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1827 PUSH_INSN(ret, location, concatarray);
1843 if (has_splat) orig_argc++;
1844 if (has_keyword_splat) orig_argc++;
1853pm_setup_args_dup_rest_p(
const pm_node_t *node)
1855 switch (PM_NODE_TYPE(node)) {
1856 case PM_BACK_REFERENCE_READ_NODE:
1857 case PM_CLASS_VARIABLE_READ_NODE:
1858 case PM_CONSTANT_READ_NODE:
1861 case PM_GLOBAL_VARIABLE_READ_NODE:
1862 case PM_IMAGINARY_NODE:
1863 case PM_INSTANCE_VARIABLE_READ_NODE:
1864 case PM_INTEGER_NODE:
1865 case PM_LAMBDA_NODE:
1866 case PM_LOCAL_VARIABLE_READ_NODE:
1868 case PM_NUMBERED_REFERENCE_READ_NODE:
1869 case PM_RATIONAL_NODE:
1870 case PM_REGULAR_EXPRESSION_NODE:
1872 case PM_STRING_NODE:
1873 case PM_SYMBOL_NODE:
1876 case PM_CONSTANT_PATH_NODE: {
1878 if (cast->
parent != NULL) {
1879 return pm_setup_args_dup_rest_p(cast->
parent);
1883 case PM_IMPLICIT_NODE:
1885 case PM_ARRAY_NODE: {
1887 for (
size_t index = 0; index < cast->
elements.
size; index++) {
1888 if (pm_setup_args_dup_rest_p(cast->
elements.
nodes[index])) {
1905 int dup_rest = SPLATARRAY_TRUE;
1908 size_t arguments_size;
1913 arguments_node != NULL &&
1914 (arguments = &arguments_node->
arguments, arguments_size = arguments->
size) >= 2 &&
1915 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
1916 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
1917 PM_NODE_TYPE_P(arguments->
nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1922 dup_rest = SPLATARRAY_FALSE;
1927 for (
size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->
size; index++) {
1930 switch (PM_NODE_TYPE(element)) {
1931 case PM_ASSOC_NODE: {
1933 if (pm_setup_args_dup_rest_p(assoc->
key) || pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1936 case PM_ASSOC_SPLAT_NODE: {
1938 if (assoc->
value != NULL && pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1947 int initial_dup_rest = dup_rest;
1950 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1954 bool regular_block_arg =
true;
1957 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1958 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1959 initial_dup_rest = dup_rest;
1962 DECL_ANCHOR(block_arg);
1963 pm_compile_node(iseq, block, block_arg,
false, scope_node);
1965 *flags |= VM_CALL_ARGS_BLOCKARG;
1967 if (LIST_INSN_SIZE_ONE(block_arg)) {
1969 if (IS_INSN(elem)) {
1971 if (iobj->insn_id == BIN(getblockparam)) {
1972 iobj->insn_id = BIN(getblockparamproxy);
1979 regular_block_arg =
false;
1983 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1984 PUSH_SEQ(ret, block_arg);
1987 argc = pm_setup_args_core(arguments_node, block, flags,
false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1994 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
1995 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2015 if (!popped) PUSH_INSN(ret, location, putnil);
2017 PM_COMPILE_NOT_POPPED(node->
receiver);
2019 int boff = (node->
block == NULL ? 0 : 1);
2020 int flag = PM_NODE_TYPE_P(node->
receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2022 int argc = pm_setup_args(node->
arguments, (
const pm_node_t *) node->
block, &flag, &keywords, iseq, ret, scope_node, node_location);
2024 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2026 PUSH_INSN(ret, location, splatkw);
2029 PUSH_INSN(ret, location, dup);
2030 PUSH_INSN(ret, location, splatkw);
2031 PUSH_INSN(ret, location, pop);
2035 int dup_argn = argc + 1 + boff;
2036 int keyword_len = 0;
2039 keyword_len = keywords->keyword_len;
2040 dup_argn += keyword_len;
2043 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2044 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2045 PM_COMPILE_NOT_POPPED(node->
value);
2048 PUSH_SEND(ret, location, id_operator,
INT2FIX(1));
2051 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2053 if (flag & VM_CALL_ARGS_SPLAT) {
2054 if (flag & VM_CALL_KW_SPLAT) {
2055 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2057 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2058 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2059 flag |= VM_CALL_ARGS_SPLAT_MUT;
2062 PUSH_INSN(ret, location, swap);
2063 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2064 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2065 PUSH_INSN(ret, location, pop);
2069 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2070 PUSH_INSN(ret, location, swap);
2071 PUSH_INSN(ret, location, pop);
2073 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2074 PUSH_INSN(ret, location, swap);
2075 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2076 PUSH_INSN(ret, location, swap);
2077 flag |= VM_CALL_ARGS_SPLAT_MUT;
2079 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2081 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2082 PUSH_INSN(ret, location, pop);
2083 PUSH_INSN(ret, location, pop);
2087 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2089 else if (flag & VM_CALL_KW_SPLAT) {
2091 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2092 PUSH_INSN(ret, location, swap);
2093 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2094 PUSH_INSN(ret, location, pop);
2096 PUSH_INSN(ret, location, swap);
2097 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2099 else if (keyword_len) {
2100 PUSH_INSN(ret, location, dup);
2101 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 2));
2102 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2103 PUSH_INSN(ret, location, pop);
2104 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2108 PUSH_INSN(ret, location, swap);
2110 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2113 PUSH_INSN(ret, location, pop);
2132 if (!popped) PUSH_INSN(ret, location, putnil);
2133 PM_COMPILE_NOT_POPPED(receiver);
2135 int boff = (block == NULL ? 0 : 1);
2136 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2138 int argc = pm_setup_args(arguments, (
const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2140 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2142 PUSH_INSN(ret, location, splatkw);
2145 PUSH_INSN(ret, location, dup);
2146 PUSH_INSN(ret, location, splatkw);
2147 PUSH_INSN(ret, location, pop);
2151 int dup_argn = argc + 1 + boff;
2152 int keyword_len = 0;
2155 keyword_len = keywords->keyword_len;
2156 dup_argn += keyword_len;
2159 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2160 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2162 LABEL *label = NEW_LABEL(location.
line);
2165 PUSH_INSN(ret, location, dup);
2166 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2167 PUSH_INSNL(ret, location, branchunless, label);
2170 PUSH_INSNL(ret, location, branchif, label);
2173 PUSH_INSN(ret, location, pop);
2174 PM_COMPILE_NOT_POPPED(value);
2177 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2180 if (flag & VM_CALL_ARGS_SPLAT) {
2181 if (flag & VM_CALL_KW_SPLAT) {
2182 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2183 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2184 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2185 flag |= VM_CALL_ARGS_SPLAT_MUT;
2188 PUSH_INSN(ret, location, swap);
2189 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2190 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2191 PUSH_INSN(ret, location, pop);
2195 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2196 PUSH_INSN(ret, location, swap);
2197 PUSH_INSN(ret, location, pop);
2199 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2200 PUSH_INSN(ret, location, swap);
2201 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2202 PUSH_INSN(ret, location, swap);
2203 flag |= VM_CALL_ARGS_SPLAT_MUT;
2205 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2207 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2208 PUSH_INSN(ret, location, pop);
2209 PUSH_INSN(ret, location, pop);
2213 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2215 else if (flag & VM_CALL_KW_SPLAT) {
2217 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2218 PUSH_INSN(ret, location, swap);
2219 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2220 PUSH_INSN(ret, location, pop);
2223 PUSH_INSN(ret, location, swap);
2224 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2226 else if (keyword_len) {
2227 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2228 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 0));
2229 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2233 PUSH_INSN(ret, location, swap);
2235 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2238 PUSH_INSN(ret, location, pop);
2239 PUSH_INSNL(ret, location, jump, lfin);
2240 PUSH_LABEL(ret, label);
2242 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2244 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(dup_argn + 1));
2245 PUSH_LABEL(ret, lfin);
2254#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2255#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2256#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2257#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2258#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2263static int pm_compile_pattern(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *matched_label,
LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index);
2272 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2273 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2275 PUSH_INSN(ret, location, dup);
2276 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2278 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2279 PUSH_INSN1(ret, location, putobject, message);
2280 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2281 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2282 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2284 PUSH_INSN1(ret, location, putobject,
Qfalse);
2285 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2287 PUSH_INSN(ret, location, pop);
2288 PUSH_INSN(ret, location, pop);
2289 PUSH_LABEL(ret, match_succeeded_label);
2302 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2303 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2305 PUSH_INSN(ret, location, dup);
2306 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2308 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2309 PUSH_INSN1(ret, location, putobject, message);
2310 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2311 PUSH_INSN(ret, location, dup);
2312 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2313 PUSH_INSN1(ret, location, putobject, length);
2314 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(4));
2315 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2317 PUSH_INSN1(ret, location, putobject,
Qfalse);
2318 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2320 PUSH_INSN(ret, location, pop);
2321 PUSH_INSN(ret, location, pop);
2322 PUSH_LABEL(ret, match_succeeded_label);
2335 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2336 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2338 PUSH_INSN(ret, location, dup);
2339 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2340 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2342 VALUE operand = rb_fstring_lit(
"%p === %p does not return true");
2343 PUSH_INSN1(ret, location, putobject, operand);
2345 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2346 PUSH_INSN1(ret, location, topn,
INT2FIX(5));
2347 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2348 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2349 PUSH_INSN1(ret, location, putobject,
Qfalse);
2350 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2351 PUSH_INSN(ret, location, pop);
2352 PUSH_INSN(ret, location, pop);
2354 PUSH_LABEL(ret, match_succeeded_label);
2355 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2356 PUSH_INSN(ret, location, pop);
2357 PUSH_INSN(ret, location, pop);
2369pm_compile_pattern_match(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2371 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2372 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2373 PUSH_LABEL(ret, matched_label);
2383pm_compile_pattern_deconstruct(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *deconstruct_label,
LABEL *match_failed_label,
LABEL *deconstructed_label,
LABEL *type_error_label,
bool in_single_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2385 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2387 if (use_deconstructed_cache) {
2388 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2389 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2391 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2392 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2394 PUSH_INSN(ret, location, pop);
2395 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2396 PUSH_INSNL(ret, location, jump, deconstructed_label);
2399 PUSH_INSNL(ret, location, jump, deconstruct_label);
2402 PUSH_LABEL(ret, deconstruct_label);
2403 PUSH_INSN(ret, location, dup);
2406 PUSH_INSN1(ret, location, putobject, operand);
2407 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2409 if (use_deconstructed_cache) {
2410 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2413 if (in_single_pattern) {
2414 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1));
2417 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2418 PUSH_SEND(ret, location, rb_intern(
"deconstruct"),
INT2FIX(0));
2420 if (use_deconstructed_cache) {
2421 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2424 PUSH_INSN(ret, location, dup);
2426 PUSH_INSNL(ret, location, branchunless, type_error_label);
2427 PUSH_LABEL(ret, deconstructed_label);
2439 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2441 PUSH_INSN(ret, location, dup);
2442 PM_COMPILE_NOT_POPPED(node);
2444 if (in_single_pattern) {
2445 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
2447 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2448 if (in_single_pattern) {
2449 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2451 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2462 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2463 LABEL *key_error_label = NEW_LABEL(location.
line);
2464 LABEL *cleanup_label = NEW_LABEL(location.
line);
2467 kw_arg->references = 0;
2468 kw_arg->keyword_len = 2;
2469 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
2470 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
2472 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2473 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2474 PUSH_INSNL(ret, location, branchif, key_error_label);
2477 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2480 VALUE operand = rb_fstring_lit(
"%p: %s");
2481 PUSH_INSN1(ret, location, putobject, operand);
2484 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2485 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2486 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2487 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2488 PUSH_INSNL(ret, location, jump, cleanup_label);
2490 PUSH_LABEL(ret, key_error_label);
2492 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2495 VALUE operand = rb_fstring_lit(
"%p: %s");
2496 PUSH_INSN1(ret, location, putobject, operand);
2499 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2500 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2501 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2502 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2503 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2504 PUSH_SEND_R(ret, location, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
2505 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(1));
2506 PUSH_LABEL(ret, cleanup_label);
2508 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(7));
2509 if (!popped) PUSH_INSN(ret, location, putnil);
2510 PUSH_INSNL(ret, location, jump, done_label);
2511 PUSH_INSN1(ret, location, dupn,
INT2FIX(5));
2512 if (popped) PUSH_INSN(ret, location, putnil);
2521 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2523 switch (PM_NODE_TYPE(node)) {
2524 case PM_ARRAY_PATTERN_NODE: {
2536 const size_t posts_size = cast->
posts.
size;
2537 const size_t minimum_size = requireds_size + posts_size;
2539 bool rest_named =
false;
2540 bool use_rest_size =
false;
2542 if (cast->
rest != NULL) {
2543 rest_named = (PM_NODE_TYPE_P(cast->
rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) cast->
rest)->expression != NULL);
2544 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2547 LABEL *match_failed_label = NEW_LABEL(location.
line);
2548 LABEL *type_error_label = NEW_LABEL(location.
line);
2549 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2550 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2552 if (use_rest_size) {
2553 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2554 PUSH_INSN(ret, location, swap);
2559 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2562 CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
2564 PUSH_INSN(ret, location, dup);
2565 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2566 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2567 PUSH_SEND(ret, location, cast->
rest == NULL ? idEq : idGE,
INT2FIX(1));
2568 if (in_single_pattern) {
2569 VALUE message = cast->
rest == NULL ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p)") : rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)");
2570 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message,
INT2FIX(minimum_size), base_index + 1));
2572 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2574 for (
size_t index = 0; index < requireds_size; index++) {
2576 PUSH_INSN(ret, location, dup);
2577 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2578 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2579 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2582 if (cast->
rest != NULL) {
2584 PUSH_INSN(ret, location, dup);
2585 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size));
2586 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2587 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2588 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2589 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2590 PUSH_INSN1(ret, location, setn,
INT2FIX(4));
2591 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2592 CHECK(pm_compile_pattern_match(iseq, scope_node, ((
const pm_splat_node_t *) cast->
rest)->expression, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2594 else if (posts_size > 0) {
2595 PUSH_INSN(ret, location, dup);
2596 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2597 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2598 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2599 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2600 PUSH_INSN(ret, location, pop);
2604 for (
size_t index = 0; index < posts_size; index++) {
2606 PUSH_INSN(ret, location, dup);
2608 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size + index));
2609 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2610 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2611 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2612 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2615 PUSH_INSN(ret, location, pop);
2616 if (use_rest_size) {
2617 PUSH_INSN(ret, location, pop);
2620 PUSH_INSNL(ret, location, jump, matched_label);
2621 PUSH_INSN(ret, location, putnil);
2622 if (use_rest_size) {
2623 PUSH_INSN(ret, location, putnil);
2626 PUSH_LABEL(ret, type_error_label);
2627 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2631 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2632 PUSH_INSN1(ret, location, putobject, operand);
2635 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2636 PUSH_INSN(ret, location, pop);
2638 PUSH_LABEL(ret, match_failed_label);
2639 PUSH_INSN(ret, location, pop);
2640 if (use_rest_size) {
2641 PUSH_INSN(ret, location, pop);
2644 PUSH_INSNL(ret, location, jump, unmatched_label);
2647 case PM_FIND_PATTERN_NODE: {
2659 LABEL *match_failed_label = NEW_LABEL(location.
line);
2660 LABEL *type_error_label = NEW_LABEL(location.
line);
2661 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2662 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2665 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2668 CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
2670 PUSH_INSN(ret, location, dup);
2671 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2672 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2673 PUSH_SEND(ret, location, idGE,
INT2FIX(1));
2674 if (in_single_pattern) {
2675 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)"),
INT2FIX(size), base_index + 1));
2677 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2680 LABEL *while_begin_label = NEW_LABEL(location.
line);
2681 LABEL *next_loop_label = NEW_LABEL(location.
line);
2682 LABEL *find_succeeded_label = NEW_LABEL(location.
line);
2683 LABEL *find_failed_label = NEW_LABEL(location.
line);
2685 PUSH_INSN(ret, location, dup);
2686 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2688 PUSH_INSN(ret, location, dup);
2689 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2690 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2691 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2692 PUSH_LABEL(ret, while_begin_label);
2694 PUSH_INSN(ret, location, dup);
2695 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2696 PUSH_SEND(ret, location, idLE,
INT2FIX(1));
2697 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2699 for (
size_t index = 0; index < size; index++) {
2700 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2701 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2704 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2705 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2708 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2709 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
requireds.
nodes[index], ret, next_loop_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2715 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2716 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2717 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2718 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2719 CHECK(pm_compile_pattern_match(iseq, scope_node, left->
expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2725 if (right->expression != NULL) {
2726 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2727 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2728 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2729 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2730 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2731 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2732 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4);
2735 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2737 PUSH_LABEL(ret, next_loop_label);
2738 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
2739 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2740 PUSH_INSNL(ret, location, jump, while_begin_label);
2742 PUSH_LABEL(ret, find_failed_label);
2743 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2744 if (in_single_pattern) {
2745 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2748 VALUE operand = rb_fstring_lit(
"%p does not match to find pattern");
2749 PUSH_INSN1(ret, location, putobject, operand);
2752 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2753 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2754 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2756 PUSH_INSN1(ret, location, putobject,
Qfalse);
2757 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2759 PUSH_INSN(ret, location, pop);
2760 PUSH_INSN(ret, location, pop);
2762 PUSH_INSNL(ret, location, jump, match_failed_label);
2763 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2765 PUSH_LABEL(ret, find_succeeded_label);
2766 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2769 PUSH_INSN(ret, location, pop);
2770 PUSH_INSNL(ret, location, jump, matched_label);
2771 PUSH_INSN(ret, location, putnil);
2773 PUSH_LABEL(ret, type_error_label);
2774 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2778 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2779 PUSH_INSN1(ret, location, putobject, operand);
2782 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2783 PUSH_INSN(ret, location, pop);
2785 PUSH_LABEL(ret, match_failed_label);
2786 PUSH_INSN(ret, location, pop);
2787 PUSH_INSNL(ret, location, jump, unmatched_label);
2791 case PM_HASH_PATTERN_NODE: {
2806 LABEL *match_failed_label = NEW_LABEL(location.
line);
2807 LABEL *type_error_label = NEW_LABEL(location.
line);
2810 if (has_keys && !has_rest) {
2813 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2815 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2826 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2829 PUSH_INSN(ret, location, dup);
2832 VALUE operand =
ID2SYM(rb_intern(
"deconstruct_keys"));
2833 PUSH_INSN1(ret, location, putobject, operand);
2836 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2837 if (in_single_pattern) {
2838 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1));
2840 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2843 PUSH_INSN(ret, location, putnil);
2846 PUSH_INSN1(ret, location, duparray, keys);
2849 PUSH_SEND(ret, location, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
2851 PUSH_INSN(ret, location, dup);
2853 PUSH_INSNL(ret, location, branchunless, type_error_label);
2856 PUSH_SEND(ret, location, rb_intern(
"dup"),
INT2FIX(0));
2860 DECL_ANCHOR(match_values);
2862 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2864 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2871 PUSH_INSN(ret, location, dup);
2872 PUSH_INSN1(ret, location, putobject, symbol);
2873 PUSH_SEND(ret, location, rb_intern(
"key?"),
INT2FIX(1));
2875 if (in_single_pattern) {
2876 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2878 PUSH_INSN(ret, location, dup);
2879 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2883 PUSH_INSN1(ret, location, putobject, operand);
2886 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2887 PUSH_INSN1(ret, location, putobject,
Qtrue);
2888 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2889 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2890 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2891 PUSH_INSN1(ret, location, putobject, symbol);
2892 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2894 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(4));
2895 PUSH_LABEL(ret, match_succeeded_label);
2898 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2899 PUSH_INSN(match_values, location, dup);
2900 PUSH_INSN1(match_values, location, putobject, symbol);
2901 PUSH_SEND(match_values, location, has_rest ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
2904 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2908 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2911 PUSH_SEQ(ret, match_values);
2914 PUSH_INSN(ret, location, dup);
2915 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2916 if (in_single_pattern) {
2917 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p is not empty"), base_index + 1));
2919 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2923 switch (PM_NODE_TYPE(cast->
rest)) {
2924 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2925 PUSH_INSN(ret, location, dup);
2926 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2927 if (in_single_pattern) {
2928 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1);
2930 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2933 case PM_ASSOC_SPLAT_NODE: {
2935 PUSH_INSN(ret, location, dup);
2936 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1);
2940 rb_bug(
"unreachable");
2945 PUSH_INSN(ret, location, pop);
2946 PUSH_INSNL(ret, location, jump, matched_label);
2947 PUSH_INSN(ret, location, putnil);
2949 PUSH_LABEL(ret, type_error_label);
2950 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2954 VALUE operand = rb_fstring_lit(
"deconstruct_keys must return Hash");
2955 PUSH_INSN1(ret, location, putobject, operand);
2958 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2959 PUSH_INSN(ret, location, pop);
2961 PUSH_LABEL(ret, match_failed_label);
2962 PUSH_INSN(ret, location, pop);
2963 PUSH_INSNL(ret, location, jump, unmatched_label);
2966 case PM_CAPTURE_PATTERN_NODE: {
2978 LABEL *match_failed_label = NEW_LABEL(location.
line);
2980 PUSH_INSN(ret, location, dup);
2981 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index + 1));
2982 CHECK(pm_compile_pattern(iseq, scope_node, (
const pm_node_t *) cast->
target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index));
2983 PUSH_INSN(ret, location, putnil);
2985 PUSH_LABEL(ret, match_failed_label);
2986 PUSH_INSN(ret, location, pop);
2987 PUSH_INSNL(ret, location, jump, unmatched_label);
2991 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3002 if (in_alternation_pattern) {
3003 ID id = pm_constant_id_lookup(scope_node, cast->
name);
3004 const char *name = rb_id2name(
id);
3006 if (name && strlen(name) > 0 && name[0] !=
'_') {
3007 COMPILE_ERROR(iseq, location.
line,
"illegal variable in alternative pattern (%"PRIsVALUE
")", rb_id2str(
id));
3012 PUSH_SETLOCAL(ret, location, index.index, index.level);
3013 PUSH_INSNL(ret, location, jump, matched_label);
3016 case PM_ALTERNATION_PATTERN_NODE: {
3021 LABEL *matched_left_label = NEW_LABEL(location.
line);
3022 LABEL *unmatched_left_label = NEW_LABEL(location.
line);
3026 PUSH_INSN(ret, location, dup);
3027 CHECK(pm_compile_pattern(iseq, scope_node, cast->
left, ret, matched_left_label, unmatched_left_label, in_single_pattern,
true, use_deconstructed_cache, base_index + 1));
3032 PUSH_LABEL(ret, matched_left_label);
3033 PUSH_INSN(ret, location, pop);
3034 PUSH_INSNL(ret, location, jump, matched_label);
3035 PUSH_INSN(ret, location, putnil);
3039 PUSH_LABEL(ret, unmatched_left_label);
3040 CHECK(pm_compile_pattern(iseq, scope_node, cast->
right, ret, matched_label, unmatched_label, in_single_pattern,
true, use_deconstructed_cache, base_index));
3043 case PM_PARENTHESES_NODE:
3048 return pm_compile_pattern(iseq, scope_node, ((
const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index);
3049 case PM_PINNED_EXPRESSION_NODE:
3057 case PM_CLASS_VARIABLE_READ_NODE:
3058 case PM_CONSTANT_PATH_NODE:
3059 case PM_CONSTANT_READ_NODE:
3062 case PM_GLOBAL_VARIABLE_READ_NODE:
3063 case PM_IMAGINARY_NODE:
3064 case PM_INSTANCE_VARIABLE_READ_NODE:
3065 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3066 case PM_INTEGER_NODE:
3067 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3068 case PM_INTERPOLATED_STRING_NODE:
3069 case PM_INTERPOLATED_SYMBOL_NODE:
3070 case PM_INTERPOLATED_X_STRING_NODE:
3071 case PM_LAMBDA_NODE:
3072 case PM_LOCAL_VARIABLE_READ_NODE:
3074 case PM_SOURCE_ENCODING_NODE:
3075 case PM_SOURCE_FILE_NODE:
3076 case PM_SOURCE_LINE_NODE:
3078 case PM_RATIONAL_NODE:
3079 case PM_REGULAR_EXPRESSION_NODE:
3081 case PM_STRING_NODE:
3082 case PM_SYMBOL_NODE:
3084 case PM_X_STRING_NODE: {
3088 PM_COMPILE_NOT_POPPED(node);
3089 if (in_single_pattern) {
3090 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
3093 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3095 if (in_single_pattern) {
3096 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3099 PUSH_INSNL(ret, location, branchif, matched_label);
3100 PUSH_INSNL(ret, location, jump, unmatched_label);
3103 case PM_PINNED_VARIABLE_NODE: {
3109 CHECK(pm_compile_pattern(iseq, scope_node, cast->
variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern,
true, base_index));
3113 case PM_UNLESS_NODE: {
3128 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3143 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
3144 PM_COMPILE_NOT_POPPED(predicate);
3146 if (in_single_pattern) {
3147 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3149 PUSH_INSN(ret, location, dup);
3150 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3151 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3154 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3158 VALUE operand = rb_fstring_lit(
"guard clause does not return true");
3159 PUSH_INSN1(ret, location, putobject, operand);
3162 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3163 PUSH_INSN1(ret, location, putobject,
Qfalse);
3164 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3166 PUSH_INSN(ret, location, pop);
3167 PUSH_INSN(ret, location, pop);
3169 PUSH_LABEL(ret, match_succeeded_label);
3172 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3173 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3176 PUSH_INSNL(ret, location, branchif, unmatched_label);
3179 PUSH_INSNL(ret, location, jump, matched_label);
3186 rb_bug(
"Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3193#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3194#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3195#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3196#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3197#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3207 scope->base.
type = PM_SCOPE_NODE;
3211 scope->previous = previous;
3215 scope->parser = previous->parser;
3216 scope->encoding = previous->encoding;
3218 scope->constants = previous->constants;
3219 scope->coverage_enabled = previous->coverage_enabled;
3223 switch (PM_NODE_TYPE(node)) {
3224 case PM_BLOCK_NODE: {
3226 scope->body = cast->
body;
3227 scope->locals = cast->
locals;
3231 case PM_CLASS_NODE: {
3233 scope->body = cast->
body;
3234 scope->locals = cast->
locals;
3240 scope->body = cast->body;
3241 scope->locals = cast->locals;
3244 case PM_ENSURE_NODE: {
3260 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3265 case PM_LAMBDA_NODE: {
3268 scope->body = cast->
body;
3269 scope->locals = cast->
locals;
3279 case PM_MODULE_NODE: {
3281 scope->body = cast->
body;
3282 scope->locals = cast->
locals;
3285 case PM_POST_EXECUTION_NODE: {
3290 case PM_PROGRAM_NODE: {
3293 scope->locals = cast->locals;
3296 case PM_RESCUE_NODE: {
3301 case PM_RESCUE_MODIFIER_NODE: {
3306 case PM_SINGLETON_CLASS_NODE: {
3308 scope->body = cast->
body;
3309 scope->locals = cast->
locals;
3312 case PM_STATEMENTS_NODE: {
3318 rb_bug(
"unreachable");
3326 if (scope_node->index_lookup_table) {
3327 st_free_table(scope_node->index_lookup_table);
3347 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
3348 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3349 iobj = (
INSN*) get_prev_insn(iobj);
3351 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
3356 if (&iobj->link == LAST_ELEMENT(ret)) {
3364 const char *name = rb_id2name(method_id);
3365 static const char prefix[] =
"__builtin_";
3366 const size_t prefix_len =
sizeof(prefix) - 1;
3368 if (receiver == NULL) {
3369 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3371 return &name[prefix_len];
3374 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3375 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3383 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3398 if (arguments == NULL) {
3399 COMPILE_ERROR(iseq, node_location->
line,
"attr!: no argument");
3405 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3406 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3410 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3413 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
3414 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3416 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
3417 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3419 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
3420 iseq_set_use_block(iseq);
3422 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
3424 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3427 COMPILE_ERROR(iseq, node_location->
line,
"unknown argument to attr!: %s", RSTRING_PTR(
string));
3438 if (arguments == NULL) {
3439 COMPILE_ERROR(iseq, node_location->
line,
"arg!: no argument");
3444 COMPILE_ERROR(iseq, node_location->
line,
"arg!: too many argument");
3449 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3450 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3456 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3458 debugs(
"id: %s idx: %d\n", rb_id2name(name), index);
3459 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3468 const pm_node_t *ast_node = scope_node->ast_node;
3469 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3470 rb_bug(
"mandatory_only?: not in method definition");
3476 if (parameters_node == NULL) {
3477 rb_bug(
"mandatory_only?: in method definition with no parameters");
3482 if (body_node == NULL || !PM_NODE_TYPE_P(body_node, PM_STATEMENTS_NODE) || (((
const pm_statements_node_t *) body_node)->body.size != 1) || !PM_NODE_TYPE_P(((
const pm_statements_node_t *) body_node)->body.nodes[0], PM_IF_NODE)) {
3483 rb_bug(
"mandatory_only?: not in method definition with plain statements");
3489 rb_bug(
"mandatory_only?: can't find mandatory node");
3500 .name = def_node->
name,
3502 .parameters = ¶meters,
3512 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3515 const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
3517 rb_iseq_base_label(iseq),
3519 rb_iseq_realpath(iseq),
3520 node_location->
line,
3524 ISEQ_COMPILE_DATA(iseq)->option,
3527 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
3530 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3531 rb_jump_tag(error_state);
3534 pm_scope_node_destroy(&next_scope_node);
3543 if (parent_block != NULL) {
3544 COMPILE_ERROR(iseq, node_location->
line,
"should not call builtins here.");
3548#define BUILTIN_INLINE_PREFIX "_bi"
3549 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
3550 bool cconst =
false;
3555 if (strcmp(
"cstmt!", builtin_func) == 0 || strcmp(
"cexpr!", builtin_func) == 0) {
3558 else if (strcmp(
"cconst!", builtin_func) == 0) {
3561 else if (strcmp(
"cinit!", builtin_func) == 0) {
3565 else if (strcmp(
"attr!", builtin_func) == 0) {
3566 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3568 else if (strcmp(
"arg!", builtin_func) == 0) {
3569 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3571 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
3573 rb_bug(
"mandatory_only? should be in if condition");
3575 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3576 rb_bug(
"mandatory_only? should be put on top");
3579 PUSH_INSN1(ret, *node_location, putobject,
Qfalse);
3580 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3583 rb_bug(
"can't find builtin function:%s", builtin_func);
3586 COMPILE_ERROR(iseq, node_location->
line,
"can't find builtin function:%s", builtin_func);
3590 int inline_index = node_location->
line;
3591 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
3592 builtin_func = inline_func;
3598 typedef VALUE(*builtin_func0)(
void *,
VALUE);
3599 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
3600 PUSH_INSN1(ret, *node_location, putobject, const_val);
3606 DECL_ANCHOR(args_seq);
3610 int argc = pm_setup_args(arguments, call_node->
block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3612 if (argc != bf->argc) {
3613 COMPILE_ERROR(iseq, node_location->
line,
"argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3617 unsigned int start_index;
3618 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3619 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
3622 PUSH_SEQ(ret, args_seq);
3623 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3626 if (popped) PUSH_INSN(ret, *node_location, pop);
3641 LABEL *else_label = NEW_LABEL(location.
line);
3642 LABEL *end_label = NEW_LABEL(location.
line);
3643 LABEL *retry_end_l = NEW_LABEL(location.
line);
3647 int node_id = location.
node_id;
3649 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3650 if (PM_BRANCH_COVERAGE_P(iseq)) {
3651 const uint8_t *cursors[3] = {
3657 const uint8_t *end_cursor = cursors[0];
3658 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3659 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3662 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3666 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
3667 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
3670 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location,
"&.");
3673 PUSH_INSN(ret, location, dup);
3674 PUSH_INSNL(ret, location, branchnil, else_label);
3676 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0,
"then", branches);
3684 int orig_argc = pm_setup_args(call_node->
arguments, call_node->
block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3685 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3688 if (call_node->
block != NULL && PM_NODE_TYPE_P(call_node->
block, PM_BLOCK_NODE)) {
3691 pm_scope_node_init(call_node->
block, &next_scope_node, scope_node);
3693 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number(scope_node->parser, call_node->
block));
3694 pm_scope_node_destroy(&next_scope_node);
3695 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3698 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3699 flags |= VM_CALL_VCALL;
3703 flags |= VM_CALL_ARGS_SIMPLE;
3707 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3708 flags |= VM_CALL_FCALL;
3711 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3712 if (flags & VM_CALL_ARGS_BLOCKARG) {
3713 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
3714 if (flags & VM_CALL_ARGS_SPLAT) {
3715 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3716 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3718 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 3));
3719 PUSH_INSN(ret, location, pop);
3721 else if (flags & VM_CALL_ARGS_SPLAT) {
3722 PUSH_INSN(ret, location, dup);
3723 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3724 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3725 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 2));
3726 PUSH_INSN(ret, location, pop);
3729 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 1));
3733 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3734 PUSH_INSN(ret, location, splatkw);
3737 LABEL *not_basic_new = NEW_LABEL(location.
line);
3738 LABEL *not_basic_new_finish = NEW_LABEL(location.
line);
3740 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
3741 method_id == rb_intern(
"new") &&
3742 call_node->
block == NULL &&
3743 (flags & VM_CALL_ARGS_BLOCKARG) == 0;
3746 if (LAST_ELEMENT(ret) == opt_new_prelude) {
3747 PUSH_INSN(ret, location, putnil);
3748 PUSH_INSN(ret, location, swap);
3751 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(swap), 0)->link);
3752 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(putnil), 0)->link);
3757 if (flags & VM_CALL_FORWARDING) {
3758 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
3761 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
3764 PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
3765 LABEL_REF(not_basic_new);
3767 PUSH_SEND_R(ret, location, rb_intern(
"initialize"),
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags | VM_CALL_FCALL), kw_arg);
3768 PUSH_INSNL(ret, location, jump, not_basic_new_finish);
3770 PUSH_LABEL(ret, not_basic_new);
3772 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3773 PUSH_INSN(ret, location, swap);
3775 PUSH_LABEL(ret, not_basic_new_finish);
3776 PUSH_INSN(ret, location, pop);
3779 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3782 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3783 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3784 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3787 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3788 PUSH_INSNL(ret, location, jump, end_label);
3789 PUSH_LABEL(ret, else_label);
3790 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1,
"else", branches);
3791 PUSH_LABEL(ret, end_label);
3794 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3795 PUSH_INSN(ret, location, pop);
3798 if (popped) PUSH_INSN(ret, location, pop);
3799 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3829#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3832 enum defined_type dtype = DEFINED_NOT_DEFINED;
3835 switch (PM_NODE_TYPE(node)) {
3840 dtype = DEFINED_NIL;
3843 case PM_INSTANCE_VARIABLE_READ_NODE: {
3847 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3849 PUSH_INSN3(ret, location, definedivar,
ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3854 case PM_LOCAL_VARIABLE_READ_NODE:
3857 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3860 dtype = DEFINED_LVAR;
3863 case PM_GLOBAL_VARIABLE_READ_NODE: {
3867 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3869 PUSH_INSN(ret, location, putnil);
3870 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR),
ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
3875 case PM_CLASS_VARIABLE_READ_NODE: {
3879 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3881 PUSH_INSN(ret, location, putnil);
3882 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR),
ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
3887 case PM_CONSTANT_READ_NODE: {
3891 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3893 PUSH_INSN(ret, location, putnil);
3894 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
3902 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
3904 PUSH_INSN(ret, location, putnil);
3905 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3909 case PM_SUPER_NODE: {
3914 if (cast->
block != NULL && !PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
3915 dtype = DEFINED_EXPR;
3919 PUSH_INSN(ret, location, putnil);
3920 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3923 case PM_FORWARDING_SUPER_NODE: {
3928 if (cast->
block != NULL) {
3929 dtype = DEFINED_EXPR;
3933 PUSH_INSN(ret, location, putnil);
3934 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3941 dtype = DEFINED_SELF;
3947 dtype = DEFINED_TRUE;
3953 dtype = DEFINED_FALSE;
3956 case PM_CALL_AND_WRITE_NODE:
3959 case PM_CALL_OPERATOR_WRITE_NODE:
3962 case PM_CALL_OR_WRITE_NODE:
3965 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3968 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3971 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3974 case PM_CLASS_VARIABLE_WRITE_NODE:
3977 case PM_CONSTANT_AND_WRITE_NODE:
3980 case PM_CONSTANT_OPERATOR_WRITE_NODE:
3983 case PM_CONSTANT_OR_WRITE_NODE:
3986 case PM_CONSTANT_PATH_AND_WRITE_NODE:
3989 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
3992 case PM_CONSTANT_PATH_OR_WRITE_NODE:
3995 case PM_CONSTANT_PATH_WRITE_NODE:
3998 case PM_CONSTANT_WRITE_NODE:
4001 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
4004 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
4007 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
4010 case PM_GLOBAL_VARIABLE_WRITE_NODE:
4013 case PM_INDEX_AND_WRITE_NODE:
4016 case PM_INDEX_OPERATOR_WRITE_NODE:
4019 case PM_INDEX_OR_WRITE_NODE:
4022 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
4025 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
4028 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
4031 case PM_INSTANCE_VARIABLE_WRITE_NODE:
4034 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
4037 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
4040 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
4043 case PM_LOCAL_VARIABLE_WRITE_NODE:
4046 case PM_MULTI_WRITE_NODE:
4049 dtype = DEFINED_ASGN;
4052 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4055 case PM_ALIAS_METHOD_NODE:
4064 case PM_CASE_MATCH_NODE:
4076 case PM_DEFINED_NODE:
4079 case PM_FLIP_FLOP_NODE:
4091 case PM_IMAGINARY_NODE:
4094 case PM_INTEGER_NODE:
4097 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4100 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4103 case PM_INTERPOLATED_STRING_NODE:
4106 case PM_INTERPOLATED_SYMBOL_NODE:
4109 case PM_INTERPOLATED_X_STRING_NODE:
4112 case PM_LAMBDA_NODE:
4115 case PM_MATCH_LAST_LINE_NODE:
4118 case PM_MATCH_PREDICATE_NODE:
4121 case PM_MATCH_REQUIRED_NODE:
4124 case PM_MATCH_WRITE_NODE:
4127 case PM_MODULE_NODE:
4136 case PM_POST_EXECUTION_NODE:
4142 case PM_RATIONAL_NODE:
4148 case PM_REGULAR_EXPRESSION_NODE:
4151 case PM_RESCUE_MODIFIER_NODE:
4157 case PM_RETURN_NODE:
4160 case PM_SINGLETON_CLASS_NODE:
4163 case PM_SOURCE_ENCODING_NODE:
4166 case PM_SOURCE_FILE_NODE:
4169 case PM_SOURCE_LINE_NODE:
4172 case PM_STRING_NODE:
4175 case PM_SYMBOL_NODE:
4181 case PM_UNLESS_NODE:
4190 case PM_X_STRING_NODE:
4193 dtype = DEFINED_EXPR;
4196 case PM_BACK_REFERENCE_READ_NODE: {
4200 VALUE ref = pm_compile_back_reference_ref(cast);
4202 PUSH_INSN(ret, location, putnil);
4203 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4207 case PM_NUMBERED_REFERENCE_READ_NODE: {
4211 VALUE ref = pm_compile_numbered_reference_ref(cast);
4213 PUSH_INSN(ret, location, putnil);
4214 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4219 case PM_CONSTANT_PATH_NODE: {
4223 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4225 if (cast->
parent != NULL) {
4226 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4227 pm_compile_defined_expr0(iseq, cast->
parent, node_location, ret, popped, scope_node,
true, lfinish,
false);
4229 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4230 PM_COMPILE(cast->
parent);
4233 PUSH_INSN1(ret, location, putobject, rb_cObject);
4236 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4240 case PM_BEGIN_NODE: {
4248 dtype = DEFINED_NIL;
4254 pm_compile_defined_expr0(iseq, cast->
statements->
body.
nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4260 dtype = DEFINED_EXPR;
4265 dtype = DEFINED_EXPR;
4270 case PM_PARENTHESES_NODE: {
4275 if (cast->
body == NULL) {
4277 dtype = DEFINED_NIL;
4279 else if (PM_NODE_TYPE_P(cast->
body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
4282 pm_compile_defined_expr0(iseq, ((
const pm_statements_node_t *) cast->
body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4288 dtype = DEFINED_EXPR;
4293 case PM_ARRAY_NODE: {
4299 lfinish[1] = NEW_LABEL(location.
line);
4302 for (
size_t index = 0; index < cast->
elements.
size; index++) {
4303 pm_compile_defined_expr0(iseq, cast->
elements.
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4304 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4307 dtype = DEFINED_EXPR;
4313 case PM_KEYWORD_HASH_NODE: {
4318 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4325 if (elements->
size > 0 && !lfinish[1]) {
4326 lfinish[1] = NEW_LABEL(location.
line);
4329 for (
size_t index = 0; index < elements->
size; index++) {
4330 pm_compile_defined_expr0(iseq, elements->
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4331 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4334 dtype = DEFINED_EXPR;
4337 case PM_ASSOC_NODE: {
4342 pm_compile_defined_expr0(iseq, cast->
key, node_location, ret, popped, scope_node,
true, lfinish,
false);
4343 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4344 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4348 case PM_ASSOC_SPLAT_NODE: {
4353 if (cast->
value == NULL) {
4354 dtype = DEFINED_EXPR;
4358 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4361 case PM_IMPLICIT_NODE: {
4365 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4368 case PM_CALL_NODE: {
4369#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4375 if (BLOCK_P(cast)) {
4376 dtype = DEFINED_EXPR;
4381 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4382 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.
line);
4386 pm_compile_defined_expr0(iseq, (
const pm_node_t *) cast->
arguments, node_location, ret, popped, scope_node,
true, lfinish,
false);
4387 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4390 if (cast->
block && PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
4391 pm_compile_defined_expr0(iseq, cast->
block, node_location, ret, popped, scope_node,
true, lfinish,
false);
4392 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4399 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
true);
4400 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4403 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4405 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4408 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
false);
4409 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4413 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4415 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4416 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_METHOD),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4419 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4421 PUSH_INSN(ret, location, putself);
4422 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4424 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_FUNC),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4431 case PM_ARGUMENTS_NODE: {
4436 for (
size_t index = 0; index < cast->
arguments.
size; index++) {
4437 pm_compile_defined_expr0(iseq, cast->
arguments.
nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4438 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4441 dtype = DEFINED_EXPR;
4444 case PM_BLOCK_ARGUMENT_NODE:
4447 dtype = DEFINED_EXPR;
4449 case PM_FORWARDING_ARGUMENTS_NODE:
4452 dtype = DEFINED_EXPR;
4454 case PM_SPLAT_NODE: {
4460 dtype = DEFINED_EXPR;
4464 pm_compile_defined_expr0(iseq, cast->
expression, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4466 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4467 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4469 dtype = DEFINED_EXPR;
4472 case PM_SHAREABLE_CONSTANT_NODE:
4476 pm_compile_defined_expr0(iseq, ((
const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4479 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4480 case PM_BLOCK_PARAMETER_NODE:
4481 case PM_BLOCK_PARAMETERS_NODE:
4482 case PM_FORWARDING_PARAMETER_NODE:
4483 case PM_IMPLICIT_REST_NODE:
4484 case PM_IT_PARAMETERS_NODE:
4485 case PM_PARAMETERS_NODE:
4486 case PM_KEYWORD_REST_PARAMETER_NODE:
4487 case PM_NO_KEYWORDS_PARAMETER_NODE:
4488 case PM_NUMBERED_PARAMETERS_NODE:
4489 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4490 case PM_OPTIONAL_PARAMETER_NODE:
4491 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4492 case PM_REQUIRED_PARAMETER_NODE:
4493 case PM_REST_PARAMETER_NODE:
4495 case PM_ALTERNATION_PATTERN_NODE:
4496 case PM_ARRAY_PATTERN_NODE:
4497 case PM_CAPTURE_PATTERN_NODE:
4498 case PM_FIND_PATTERN_NODE:
4499 case PM_HASH_PATTERN_NODE:
4500 case PM_PINNED_EXPRESSION_NODE:
4501 case PM_PINNED_VARIABLE_NODE:
4503 case PM_CALL_TARGET_NODE:
4504 case PM_CLASS_VARIABLE_TARGET_NODE:
4505 case PM_CONSTANT_PATH_TARGET_NODE:
4506 case PM_CONSTANT_TARGET_NODE:
4507 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4508 case PM_INDEX_TARGET_NODE:
4509 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4510 case PM_LOCAL_VARIABLE_TARGET_NODE:
4511 case PM_MULTI_TARGET_NODE:
4514 case PM_ENSURE_NODE:
4516 case PM_RESCUE_NODE:
4520 case PM_EMBEDDED_STATEMENTS_NODE:
4521 case PM_EMBEDDED_VARIABLE_NODE:
4522 case PM_MISSING_NODE:
4523 case PM_PRE_EXECUTION_NODE:
4524 case PM_PROGRAM_NODE:
4526 case PM_STATEMENTS_NODE:
4527 rb_bug(
"Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4531 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4540 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4543 LABEL *lstart = NEW_LABEL(node_location->
line);
4544 LABEL *lend = NEW_LABEL(node_location->
line);
4547 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4549 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4558 lstart->rescued = LABEL_RESCUE_BEG;
4559 lend->rescued = LABEL_RESCUE_END;
4561 APPEND_LABEL(ret, lcur, lstart);
4562 PUSH_LABEL(ret, lend);
4563 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4573 lfinish[0] = NEW_LABEL(node_location->
line);
4578 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
4582 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->
line, node_location->
node_id, BIN(putnil), 0)->link);
4583 PUSH_INSN(ret, *node_location, swap);
4585 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4586 PUSH_INSN(ret, *node_location, pop);
4587 PUSH_LABEL(ret, lfinish[1]);
4591 PUSH_LABEL(ret, lfinish[0]);
4602 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4604 DECL_ANCHOR(ensure);
4607 if (enlp->erange != NULL) {
4608 DECL_ANCHOR(ensure_part);
4609 LABEL *lstart = NEW_LABEL(0);
4610 LABEL *lend = NEW_LABEL(0);
4612 add_ensure_range(iseq, enlp->erange, lstart, lend);
4614 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4615 PUSH_LABEL(ensure_part, lstart);
4617 PM_COMPILE_INTO_ANCHOR(ensure_part, (
const pm_node_t *) enlp->ensure_node);
4618 PUSH_LABEL(ensure_part, lend);
4619 PUSH_SEQ(ensure, ensure_part);
4628 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4629 PUSH_SEQ(ret, ensure);
4639pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
4647 int local_index = ctx->local_index;
4649 ID local = pm_constant_id_lookup(scope_node, constant_id);
4650 local_table_for_iseq->ids[local_index] = local;
4652 *value = (st_data_t)local_index;
4668 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4670 ID local = pm_constant_id_lookup(scope_node, constant_id);
4671 local_table_for_iseq->ids[local_index] = local;
4672 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4682 local_table_for_iseq->ids[local_index] = local_name;
4683 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4695 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4698 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4699 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4700 pm_insert_local_index(((
const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4705 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4706 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) left, index_lookup_table, local_table_for_iseq, scope_node, local_index);
4710 if (node->
rest != NULL && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE)) {
4713 if (rest->expression != NULL) {
4714 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4716 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4717 pm_insert_local_index(((
const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4723 for (
size_t index = 0; index < node->
rights.
size; index++) {
4726 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4727 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4728 pm_insert_local_index(((
const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4733 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4734 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) right, index_lookup_table, local_table_for_iseq, scope_node, local_index);
4748 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4750 PUSH_SETLOCAL(ret, location, index.index, index.level);
4764 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4765 bool has_rest = (node->
rest && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE) && (((
const pm_splat_node_t *) node->
rest)->expression) != NULL);
4768 int flag = (has_rest || has_rights) ? 1 : 0;
4771 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4774 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4778 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4789 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4799 for (
size_t index = 0; index < node->
rights.
size; index++) {
4802 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4806 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4866 node->stack_index = state->stack_size + 1;
4867 node->stack_size = stack_size;
4868 node->position = state->position;
4871 if (state->head == NULL) {
4876 state->tail->next = node;
4880 state->stack_size += stack_size;
4893 if (state->stack_size == 0)
return;
4898 while (current != NULL) {
4899 VALUE offset =
INT2FIX(state->stack_size - current->stack_index + current->position);
4900 current->topn->operands[0] = offset;
4906 if (current->stack_size > 1) {
4907 INSN *insn = current->topn;
4909 for (
size_t index = 1; index < current->stack_size; index += 1) {
4913 insn = (
INSN *) element;
4916 insn->operands[0] = offset;
4921 current = current->next;
4961 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4963 switch (PM_NODE_TYPE(node)) {
4964 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4973 PUSH_SETLOCAL(writes, location, index.index, index.level);
4976 case PM_CLASS_VARIABLE_TARGET_NODE: {
4983 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4986 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
4989 case PM_CONSTANT_TARGET_NODE: {
4996 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4999 PUSH_INSN1(writes, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5000 PUSH_INSN1(writes, location, setconstant, operand);
5003 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
5010 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5013 PUSH_INSN1(writes, location, setglobal, operand);
5016 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
5023 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5026 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
5029 case PM_CONSTANT_PATH_TARGET_NODE: {
5039 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5041 if (cast->
parent != NULL) {
5042 pm_compile_node(iseq, cast->
parent, parents,
false, scope_node);
5045 PUSH_INSN1(parents, location, putobject, rb_cObject);
5048 if (state == NULL) {
5049 PUSH_INSN(writes, location, swap);
5052 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5053 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), 1);
5057 PUSH_INSN1(writes, location, setconstant, operand);
5059 if (state != NULL) {
5060 PUSH_INSN(cleanup, location, pop);
5065 case PM_CALL_TARGET_NODE: {
5075 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
5077 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5079 LABEL *safe_label = NULL;
5080 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5081 safe_label = NEW_LABEL(location.
line);
5082 PUSH_INSN(parents, location, dup);
5083 PUSH_INSNL(parents, location, branchnil, safe_label);
5086 if (state != NULL) {
5087 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5088 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), 1);
5089 PUSH_INSN(writes, location, swap);
5092 int flags = VM_CALL_ARGS_SIMPLE;
5093 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5095 PUSH_SEND_WITH_FLAG(writes, location, method_id,
INT2FIX(1),
INT2FIX(flags));
5096 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5097 PUSH_INSN(writes, location, pop);
5098 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5100 if (state != NULL) {
5101 PUSH_INSN(cleanup, location, pop);
5106 case PM_INDEX_TARGET_NODE: {
5118 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5122 int argc = pm_setup_args(cast->
arguments, (
const pm_node_t *) cast->
block, &flags, &kwargs, iseq, parents, scope_node, &location);
5124 if (state != NULL) {
5125 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5126 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), argc + 1);
5129 PUSH_INSN(writes, location, swap);
5132 for (
int index = 0; index < argc; index++) {
5133 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5135 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5143 int ci_argc = argc + 1;
5144 if (flags & VM_CALL_ARGS_SPLAT) {
5146 PUSH_INSN1(writes, location, newarray,
INT2FIX(1));
5147 PUSH_INSN(writes, location, concatarray);
5150 PUSH_SEND_R(writes, location, idASET,
INT2NUM(ci_argc), NULL,
INT2FIX(flags), kwargs);
5151 PUSH_INSN(writes, location, pop);
5153 if (state != NULL) {
5155 PUSH_INSN(writes, location, pop);
5158 for (
int index = 0; index < argc + 1; index++) {
5159 PUSH_INSN(cleanup, location, pop);
5165 case PM_MULTI_TARGET_NODE: {
5172 size_t before_position;
5173 if (state != NULL) {
5174 before_position = state->position;
5178 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5179 if (state != NULL) state->position = before_position;
5183 case PM_SPLAT_NODE: {
5192 pm_compile_target_node(iseq, cast->
expression, parents, writes, cleanup, scope_node, state);
5198 rb_bug(
"Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5211 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5216 switch (PM_NODE_TYPE(node)) {
5217 case PM_MULTI_TARGET_NODE: {
5219 lefts = &cast->
lefts;
5224 case PM_MULTI_WRITE_NODE: {
5226 lefts = &cast->
lefts;
5232 rb_bug(
"Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5236 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) rest)->expression != NULL;
5237 bool has_posts = rights->
size > 0;
5242 PUSH_INSN2(writes, location, expandarray,
INT2FIX(lefts->
size),
INT2FIX((has_rest || has_posts) ? 1 : 0));
5248 if (state == NULL) state = &target_state;
5250 size_t base_position = state->position;
5251 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5254 for (
size_t index = 0; index < lefts->
size; index++) {
5256 state->position = lefts->
size - index + splat_position + base_position;
5257 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5263 state->position = 1 + rights->
size + base_position;
5269 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5274 if (!has_rest && rest != NULL) {
5278 for (
size_t index = 0; index < rights->
size; index++) {
5280 state->position = rights->
size - index + base_position;
5281 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5294 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5296 switch (PM_NODE_TYPE(node)) {
5297 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5300 PUSH_GETLOCAL(ret, location, 1, 0);
5301 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5304 case PM_CLASS_VARIABLE_TARGET_NODE:
5305 case PM_CONSTANT_TARGET_NODE:
5306 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5307 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5308 case PM_CONSTANT_PATH_TARGET_NODE:
5309 case PM_CALL_TARGET_NODE:
5310 case PM_INDEX_TARGET_NODE:
5311 case PM_SPLAT_NODE: {
5315 DECL_ANCHOR(writes);
5316 DECL_ANCHOR(cleanup);
5320 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5322 PUSH_GETLOCAL(ret, location, 1, 0);
5325 PUSH_SEQ(ret, writes);
5326 PUSH_SEQ(ret, cleanup);
5328 pm_multi_target_state_update(&state);
5331 case PM_MULTI_TARGET_NODE: {
5332 DECL_ANCHOR(writes);
5333 DECL_ANCHOR(cleanup);
5335 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5337 LABEL *not_single = NEW_LABEL(location.
line);
5338 LABEL *not_ary = NEW_LABEL(location.
line);
5346 PUSH_GETLOCAL(ret, location, 1, 0);
5347 PUSH_INSN(ret, location, dup);
5348 PUSH_CALL(ret, location, idLength,
INT2FIX(0));
5349 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
5350 PUSH_CALL(ret, location, idEq,
INT2FIX(1));
5351 PUSH_INSNL(ret, location, branchunless, not_single);
5352 PUSH_INSN(ret, location, dup);
5353 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
5354 PUSH_CALL(ret, location, idAREF,
INT2FIX(1));
5355 PUSH_INSN1(ret, location, putobject,
rb_cArray);
5356 PUSH_INSN(ret, location, swap);
5357 PUSH_CALL(ret, location, rb_intern(
"try_convert"),
INT2FIX(1));
5358 PUSH_INSN(ret, location, dup);
5359 PUSH_INSNL(ret, location, branchunless, not_ary);
5360 PUSH_INSN(ret, location, swap);
5362 PUSH_LABEL(ret, not_ary);
5363 PUSH_INSN(ret, location, pop);
5365 PUSH_LABEL(ret, not_single);
5366 PUSH_SEQ(ret, writes);
5367 PUSH_SEQ(ret, cleanup);
5371 rb_bug(
"Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5381 LABEL *lstart = NEW_LABEL(node_location->
line);
5382 LABEL *lend = NEW_LABEL(node_location->
line);
5383 LABEL *lcont = NEW_LABEL(node_location->
line);
5388 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5395 pm_scope_node_destroy(&rescue_scope_node);
5397 lstart->rescued = LABEL_RESCUE_BEG;
5398 lend->rescued = LABEL_RESCUE_END;
5399 PUSH_LABEL(ret, lstart);
5401 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5402 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
5409 PUSH_INSN(ret, location, putnil);
5412 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5413 PUSH_LABEL(ret, lend);
5416 if (!popped) PUSH_INSN(ret, *node_location, pop);
5420 PUSH_INSN(ret, *node_location, nop);
5421 PUSH_LABEL(ret, lcont);
5423 if (popped) PUSH_INSN(ret, *node_location, pop);
5424 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5425 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5435 if (statements != NULL) {
5436 location = PM_NODE_START_LOCATION(parser, statements);
5439 location = *node_location;
5442 LABEL *lstart = NEW_LABEL(location.
line);
5444 LABEL *lcont = NEW_LABEL(location.
line);
5451 if (statements != NULL) {
5452 pm_compile_node(iseq, (
const pm_node_t *) statements, ensr,
true, scope_node);
5456 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5461 push_ensure_entry(iseq, &enl, &er, (
void *) cast->
ensure_clause);
5463 PUSH_LABEL(ret, lstart);
5465 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5468 pm_compile_node(iseq, (
const pm_node_t *) cast->
statements, ret, popped | last_leave, scope_node);
5470 else if (!(popped | last_leave)) {
5471 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5474 PUSH_LABEL(ret, lend);
5475 PUSH_SEQ(ret, ensr);
5476 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5477 PUSH_LABEL(ret, lcont);
5478 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5490 pm_scope_node_destroy(&next_scope_node);
5492 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5493 if (lstart->link.next != &lend->link) {
5495 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5496 erange = erange->next;
5499 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5510 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5512 PM_NODE_TYPE_P(node->
receiver, PM_STRING_NODE) &&
5514 node->
block == NULL &&
5515 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5527 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5532 node->
block == NULL &&
5534 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5546 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5551 node->
block == NULL &&
5553 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5564 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5566 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5567 ISEQ_BODY(iseq)->ic_size++;
5568 VALUE segments = rb_ary_new_from_args(1, name);
5569 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5572 PUSH_INSN(ret, location, putnil);
5573 PUSH_INSN1(ret, location, putobject,
Qtrue);
5574 PUSH_INSN1(ret, location, getconstant, name);
5588 switch (PM_NODE_TYPE(node)) {
5589 case PM_CONSTANT_READ_NODE: {
5593 rb_ary_unshift(parts, name);
5596 case PM_CONSTANT_PATH_NODE: {
5600 rb_ary_unshift(parts, name);
5601 if (cast->
parent == NULL) {
5602 rb_ary_unshift(parts,
ID2SYM(idNULL));
5623 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5625 switch (PM_NODE_TYPE(node)) {
5626 case PM_CONSTANT_READ_NODE: {
5630 PUSH_INSN1(body, location, putobject,
Qtrue);
5631 PUSH_INSN1(body, location, getconstant, name);
5634 case PM_CONSTANT_PATH_NODE: {
5638 if (cast->
parent == NULL) {
5639 PUSH_INSN(body, location, pop);
5640 PUSH_INSN1(body, location, putobject, rb_cObject);
5641 PUSH_INSN1(body, location, putobject,
Qtrue);
5642 PUSH_INSN1(body, location, getconstant, name);
5645 pm_compile_constant_path(iseq, cast->
parent, prefix, body,
false, scope_node);
5646 PUSH_INSN1(body, location, putobject,
Qfalse);
5647 PUSH_INSN1(body, location, getconstant, name);
5652 PM_COMPILE_INTO_ANCHOR(prefix, node);
5663 switch (PM_NODE_TYPE(node)) {
5667 case PM_SYMBOL_NODE:
5668 case PM_REGULAR_EXPRESSION_NODE:
5669 case PM_SOURCE_LINE_NODE:
5670 case PM_INTEGER_NODE:
5672 case PM_RATIONAL_NODE:
5673 case PM_IMAGINARY_NODE:
5674 case PM_SOURCE_ENCODING_NODE:
5675 return pm_static_literal_value(iseq, node, scope_node);
5676 case PM_STRING_NODE:
5677 return parse_static_literal_string(iseq, scope_node, node, &((
const pm_string_node_t *) node)->unescaped);
5678 case PM_SOURCE_FILE_NODE:
5680 case PM_ARRAY_NODE: {
5684 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5685 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->
elements.
nodes[index], scope_node);
5693 case PM_HASH_NODE: {
5697 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5699 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE))
return Qundef;
5703 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->
key, scope_node);
5706 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->
value, scope_node);
5709 rb_hash_aset(result, key, value);
5726 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5728 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5729 PUSH_INSN1(ret, location, putobject, literal);
5733 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5734 switch (PM_NODE_TYPE(node)) {
5735 case PM_ARRAY_NODE: {
5739 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5742 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5743 pm_compile_shareable_constant_value(iseq, cast->
elements.
nodes[index], shareability, path, ret, scope_node,
false);
5749 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5750 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5755 case PM_HASH_NODE: {
5759 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5762 pm_compile_hash_elements(iseq, (
const pm_node_t *) cast, &cast->elements, shareability, path,
false, ret, scope_node);
5765 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5766 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5772 DECL_ANCHOR(value_seq);
5774 pm_compile_node(iseq, node, value_seq,
false, scope_node);
5775 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5776 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5779 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5780 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5781 PUSH_SEQ(ret, value_seq);
5782 PUSH_INSN1(ret, location, putobject, path);
5783 PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5785 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5786 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5787 PUSH_SEQ(ret, value_seq);
5788 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5790 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5791 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5792 PUSH_SEQ(ret, value_seq);
5793 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5809 ID name_id = pm_constant_id_lookup(scope_node, node->
name);
5811 if (shareability != 0) {
5812 pm_compile_shareable_constant_value(iseq, node->
value, shareability, rb_id2str(name_id), ret, scope_node,
true);
5815 PM_COMPILE_NOT_POPPED(node->
value);
5818 if (!popped) PUSH_INSN(ret, location, dup);
5819 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5822 PUSH_INSN1(ret, location, setconstant, operand);
5835 LABEL *end_label = NEW_LABEL(location.
line);
5837 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5838 if (!popped) PUSH_INSN(ret, location, dup);
5840 PUSH_INSNL(ret, location, branchunless, end_label);
5841 if (!popped) PUSH_INSN(ret, location, pop);
5843 if (shareability != 0) {
5844 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5847 PM_COMPILE_NOT_POPPED(node->
value);
5850 if (!popped) PUSH_INSN(ret, location, dup);
5851 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5852 PUSH_INSN1(ret, location, setconstant, name);
5853 PUSH_LABEL(ret, end_label);
5866 LABEL *set_label = NEW_LABEL(location.
line);
5867 LABEL *end_label = NEW_LABEL(location.
line);
5869 PUSH_INSN(ret, location, putnil);
5870 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST), name,
Qtrue);
5871 PUSH_INSNL(ret, location, branchunless, set_label);
5873 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5874 if (!popped) PUSH_INSN(ret, location, dup);
5876 PUSH_INSNL(ret, location, branchif, end_label);
5877 if (!popped) PUSH_INSN(ret, location, pop);
5878 PUSH_LABEL(ret, set_label);
5880 if (shareability != 0) {
5881 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5884 PM_COMPILE_NOT_POPPED(node->
value);
5887 if (!popped) PUSH_INSN(ret, location, dup);
5888 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5889 PUSH_INSN1(ret, location, setconstant, name);
5890 PUSH_LABEL(ret, end_label);
5905 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5907 if (shareability != 0) {
5908 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5911 PM_COMPILE_NOT_POPPED(node->
value);
5914 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5915 if (!popped) PUSH_INSN(ret, location, dup);
5917 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5918 PUSH_INSN1(ret, location, setconstant, name);
5929 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->
name)));
5932 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5934 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->
name)));
5938 if (current == NULL) {
5939 rb_ary_unshift(parts, rb_id2str(idNULL));
5941 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5942 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((
const pm_constant_read_node_t *) current)->name)));
5966 PUSH_INSN1(ret, location, putobject, rb_cObject);
5969 if (shareability != 0) {
5970 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5973 PM_COMPILE_NOT_POPPED(node->
value);
5977 PUSH_INSN(ret, location, swap);
5978 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
5981 PUSH_INSN(ret, location, swap);
5982 PUSH_INSN1(ret, location, setconstant, name);
5999 PM_COMPILE_NOT_POPPED(target->
parent);
6002 PUSH_INSN1(ret, location, putobject, rb_cObject);
6005 PUSH_INSN(ret, location, dup);
6006 PUSH_INSN1(ret, location, putobject,
Qtrue);
6007 PUSH_INSN1(ret, location, getconstant, name);
6009 if (!popped) PUSH_INSN(ret, location, dup);
6010 PUSH_INSNL(ret, location, branchunless, lfin);
6012 if (!popped) PUSH_INSN(ret, location, pop);
6014 if (shareability != 0) {
6015 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6018 PM_COMPILE_NOT_POPPED(node->
value);
6022 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6025 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6026 PUSH_INSN(ret, location, swap);
6029 PUSH_INSN1(ret, location, setconstant, name);
6030 PUSH_LABEL(ret, lfin);
6032 if (!popped) PUSH_INSN(ret, location, swap);
6033 PUSH_INSN(ret, location, pop);
6047 LABEL *lassign = NEW_LABEL(location.
line);
6051 PM_COMPILE_NOT_POPPED(target->
parent);
6054 PUSH_INSN1(ret, location, putobject, rb_cObject);
6057 PUSH_INSN(ret, location, dup);
6058 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM), name,
Qtrue);
6059 PUSH_INSNL(ret, location, branchunless, lassign);
6061 PUSH_INSN(ret, location, dup);
6062 PUSH_INSN1(ret, location, putobject,
Qtrue);
6063 PUSH_INSN1(ret, location, getconstant, name);
6065 if (!popped) PUSH_INSN(ret, location, dup);
6066 PUSH_INSNL(ret, location, branchif, lfin);
6068 if (!popped) PUSH_INSN(ret, location, pop);
6069 PUSH_LABEL(ret, lassign);
6071 if (shareability != 0) {
6072 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6075 PM_COMPILE_NOT_POPPED(node->
value);
6079 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6082 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6083 PUSH_INSN(ret, location, swap);
6086 PUSH_INSN1(ret, location, setconstant, name);
6087 PUSH_LABEL(ret, lfin);
6089 if (!popped) PUSH_INSN(ret, location, swap);
6090 PUSH_INSN(ret, location, pop);
6107 PM_COMPILE_NOT_POPPED(target->
parent);
6110 PUSH_INSN1(ret, location, putobject, rb_cObject);
6113 PUSH_INSN(ret, location, dup);
6114 PUSH_INSN1(ret, location, putobject,
Qtrue);
6115 PUSH_INSN1(ret, location, getconstant, name);
6117 if (shareability != 0) {
6118 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6121 PM_COMPILE_NOT_POPPED(node->
value);
6124 PUSH_CALL(ret, location, method_id,
INT2FIX(1));
6125 PUSH_INSN(ret, location, swap);
6128 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6129 PUSH_INSN(ret, location, swap);
6132 PUSH_INSN1(ret, location, setconstant, name);
6141#define PM_CONTAINER_P(node) (PM_NODE_TYPE_P(node, PM_ARRAY_NODE) || PM_NODE_TYPE_P(node, PM_HASH_NODE) || PM_NODE_TYPE_P(node, PM_RANGE_NODE))
6160 bool trailing_comma =
false;
6162 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6166 if (scope_node->parameters != NULL) {
6167 switch (PM_NODE_TYPE(scope_node->parameters)) {
6168 case PM_BLOCK_PARAMETERS_NODE: {
6170 parameters_node = cast->parameters;
6171 block_locals = &cast->locals;
6173 if (parameters_node) {
6174 if (parameters_node->
rest && PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE)) {
6175 trailing_comma =
true;
6180 case PM_PARAMETERS_NODE: {
6184 case PM_NUMBERED_PARAMETERS_NODE: {
6186 body->
param.lead_num = maximum;
6187 body->
param.flags.ambiguous_param0 = maximum == 1;
6190 case PM_IT_PARAMETERS_NODE:
6191 body->
param.lead_num = 1;
6192 body->
param.flags.ambiguous_param0 =
true;
6195 rb_bug(
"Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
6199 struct rb_iseq_param_keyword *keyword = NULL;
6201 if (parameters_node) {
6202 optionals_list = ¶meters_node->
optionals;
6203 requireds_list = ¶meters_node->
requireds;
6204 keywords_list = ¶meters_node->
keywords;
6205 posts_list = ¶meters_node->
posts;
6207 else if (scope_node->parameters && (PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE) || PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE))) {
6208 body->
param.opt_num = 0;
6211 body->
param.lead_num = 0;
6212 body->
param.opt_num = 0;
6218 size_t locals_size = locals->
size;
6221 st_table *index_lookup_table = st_init_numtable();
6223 int table_size = (int) locals_size;
6227 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6229 if (keywords_list && keywords_list->
size) {
6233 if (requireds_list) {
6234 for (
size_t i = 0; i < requireds_list->
size; i++) {
6239 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6242 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6243 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6252 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6260 if (optionals_list && optionals_list->
size) {
6261 for (
size_t i = 0; i < optionals_list->
size; i++) {
6263 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6273 if (parameters_node) {
6274 if (parameters_node->
rest) {
6275 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6289 if (PM_NODE_TYPE_P(parameters_node->
keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6291 if (requireds_list->
size == 0 && optionals_list->
size == 0 && keywords_list->
size == 0) {
6292 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6293 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6304 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6312 for (
size_t i = 0; i < posts_list->
size; i++) {
6317 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6323 if (keywords_list && keywords_list->
size) {
6324 for (
size_t i = 0; i < keywords_list->
size; i++) {
6326 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6332 if (parameters_node && parameters_node->
block) {
6335 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6343 local_table_for_iseq->size = table_size;
6363 int local_index = 0;
6370 if (requireds_list && requireds_list->
size) {
6371 for (
size_t i = 0; i < requireds_list->
size; i++, local_index++) {
6379 switch (PM_NODE_TYPE(required)) {
6380 case PM_MULTI_TARGET_NODE: {
6383 local = rb_make_temporary_id(local_index);
6384 local_table_for_iseq->ids[local_index] = local;
6387 case PM_REQUIRED_PARAMETER_NODE: {
6392 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6393 ID local = pm_constant_id_lookup(scope_node, param->
name);
6394 local_table_for_iseq->ids[local_index] = local;
6397 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6403 rb_bug(
"Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
6407 body->
param.lead_num = (int) requireds_list->
size;
6408 body->
param.flags.has_lead =
true;
6411 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6412 ID local = rb_make_temporary_id(local_index);
6413 local_table_for_iseq->ids[local_index++] = local;
6418 if (optionals_list && optionals_list->
size) {
6419 body->
param.opt_num = (int) optionals_list->
size;
6420 body->
param.flags.has_opt =
true;
6422 for (
size_t i = 0; i < optionals_list->
size; i++, local_index++) {
6426 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6427 ID local = pm_constant_id_lookup(scope_node, name);
6428 local_table_for_iseq->ids[local_index] = local;
6431 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6438 if (parameters_node && parameters_node->
rest) {
6439 body->
param.rest_start = local_index;
6443 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6444 body->
param.flags.has_rest =
true;
6452 if (PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6453 ID local = pm_constant_id_lookup(scope_node, name);
6454 local_table_for_iseq->ids[local_index] = local;
6457 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6463 body->
param.flags.anon_rest =
true;
6464 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6473 if (posts_list && posts_list->
size) {
6474 body->
param.post_num = (int) posts_list->
size;
6475 body->
param.post_start = local_index;
6476 body->
param.flags.has_post =
true;
6478 for (
size_t i = 0; i < posts_list->
size; i++, local_index++) {
6486 switch (PM_NODE_TYPE(post_node)) {
6487 case PM_MULTI_TARGET_NODE: {
6490 local = rb_make_temporary_id(local_index);
6491 local_table_for_iseq->ids[local_index] = local;
6494 case PM_REQUIRED_PARAMETER_NODE: {
6499 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6500 ID local = pm_constant_id_lookup(scope_node, param->
name);
6501 local_table_for_iseq->ids[local_index] = local;
6504 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6509 rb_bug(
"Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6517 if (keywords_list && keywords_list->
size) {
6518 keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6519 keyword->num = (int) keywords_list->
size;
6524 for (
size_t i = 0; i < keywords_list->
size; i++) {
6530 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6532 keyword->required_num++;
6533 ID local = pm_constant_id_lookup(scope_node, name);
6535 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6536 local_table_for_iseq->ids[local_index] = local;
6539 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6545 for (
size_t i = 0; i < keywords_list->
size; i++) {
6551 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6557 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6558 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6564 ID local = pm_constant_id_lookup(scope_node, name);
6565 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6566 local_table_for_iseq->ids[local_index] = local;
6569 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6579 for (
int i = 0; i <
RARRAY_LEN(default_values); i++) {
6581 if (dv == complex_mark) dv =
Qundef;
6585 keyword->default_values = dvs;
6589 keyword->bits_start = local_index;
6590 ID local = rb_make_temporary_id(local_index);
6591 local_table_for_iseq->ids[local_index] = local;
6594 body->
param.keyword = keyword;
6595 body->
param.flags.has_kw =
true;
6598 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->
size == 1 && !trailing_comma) {
6599 body->
param.flags.ambiguous_param0 =
true;
6602 if (parameters_node) {
6607 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6610 body->
param.flags.accepts_no_kwarg =
true;
6613 case PM_KEYWORD_REST_PARAMETER_NODE: {
6617 if (!body->
param.flags.has_kw) {
6618 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6621 keyword->rest_start = local_index;
6622 body->
param.flags.has_kwrest =
true;
6626 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6627 ID local = pm_constant_id_lookup(scope_node, constant_id);
6628 local_table_for_iseq->ids[local_index] = local;
6631 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6635 body->
param.flags.anon_kwrest =
true;
6636 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6642 case PM_FORWARDING_PARAMETER_NODE: {
6645 if (!ISEQ_BODY(iseq)->param.
flags.forwardable) {
6647 body->
param.rest_start = local_index;
6648 body->
param.flags.has_rest =
true;
6649 body->
param.flags.anon_rest =
true;
6650 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6654 body->
param.flags.has_kw =
false;
6655 body->
param.flags.has_kwrest =
true;
6656 body->
param.flags.anon_kwrest =
true;
6657 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6658 keyword->rest_start = local_index;
6659 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6662 body->
param.block_start = local_index;
6663 body->
param.flags.has_block =
true;
6664 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6668 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6672 rb_bug(
"node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->
keyword_rest)));
6678 if (parameters_node->
block) {
6679 body->
param.block_start = local_index;
6680 body->
param.flags.has_block =
true;
6681 iseq_set_use_block(iseq);
6686 if (PM_NODE_FLAG_P(parameters_node->
block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6687 ID local = pm_constant_id_lookup(scope_node, name);
6688 local_table_for_iseq->ids[local_index] = local;
6691 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6695 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6714 if (requireds_list && requireds_list->
size) {
6715 for (
size_t i = 0; i < requireds_list->
size; i++) {
6721 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6722 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) required, index_lookup_table, local_table_for_iseq, scope_node, local_index);
6728 if (posts_list && posts_list->
size) {
6729 for (
size_t i = 0; i < posts_list->
size; i++) {
6735 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6736 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) post, index_lookup_table, local_table_for_iseq, scope_node, local_index);
6742 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6743 if (PM_NODE_TYPE_P(((
const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6744 body->
param.lead_num++;
6747 body->
param.rest_start = local_index;
6748 body->
param.flags.has_rest =
true;
6751 ID local = rb_make_temporary_id(local_index);
6752 local_table_for_iseq->ids[local_index] = local;
6757 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6760 for (
int i = 0; i < maximum; i++, local_index++) {
6761 const uint8_t param_name[] = {
'_',
'1' + i };
6763 RUBY_ASSERT(constant_id &&
"parser should fill in any gaps in numbered parameters");
6764 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6766 body->
param.lead_num = maximum;
6767 body->
param.flags.has_lead =
true;
6778 if (block_locals && block_locals->
size) {
6779 for (
size_t i = 0; i < block_locals->
size; i++, local_index++) {
6781 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6786 if (scope_node->locals.
size) {
6787 for (
size_t i = 0; i < scope_node->locals.
size; i++) {
6791 ctx.scope_node = scope_node;
6792 ctx.local_table_for_iseq = local_table_for_iseq;
6793 ctx.local_index = local_index;
6795 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6797 local_index = ctx.local_index;
6806 if (scope_node->index_lookup_table) {
6807 st_free_table(scope_node->index_lookup_table);
6809 scope_node->index_lookup_table = index_lookup_table;
6810 iseq_calc_param_size(iseq);
6812 if (ISEQ_BODY(iseq)->param.
flags.forwardable) {
6815 ISEQ_BODY(iseq)->param.size += 1;
6819 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6820 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6822 if (keyword != NULL) {
6823 size_t keyword_start_index = keyword->bits_start - keyword->num;
6824 keyword->table = (
ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6829 if (optionals_list && optionals_list->
size) {
6837 for (
size_t i = 0; i < optionals_list->
size; i++) {
6838 label = NEW_LABEL(location.
line);
6839 opt_table[i] = label;
6840 PUSH_LABEL(ret, label);
6842 PM_COMPILE_NOT_POPPED(optional_node);
6846 label = NEW_LABEL(location.
line);
6847 opt_table[optionals_list->
size] = label;
6848 PUSH_LABEL(ret, label);
6850 body->
param.opt_table = (
const VALUE *) opt_table;
6853 if (keywords_list && keywords_list->
size) {
6854 size_t optional_index = 0;
6855 for (
size_t i = 0; i < keywords_list->
size; i++) {
6859 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6860 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6868 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6869 LABEL *end_label = NEW_LABEL(location.
line);
6872 int kw_bits_idx = table_size - body->
param.keyword->bits_start;
6873 PUSH_INSN2(ret, location, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(optional_index));
6874 PUSH_INSNL(ret, location, branchif, end_label);
6876 PUSH_SETLOCAL(ret, location, index.index, index.level);
6877 PUSH_LABEL(ret, end_label);
6882 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
6887 rb_bug(
"Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6892 if (requireds_list && requireds_list->
size) {
6893 for (
size_t i = 0; i < requireds_list->
size; i++) {
6899 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6900 PUSH_GETLOCAL(ret, location, table_size - (
int)i, 0);
6906 if (posts_list && posts_list->
size) {
6907 for (
size_t i = 0; i < posts_list->
size; i++) {
6913 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6914 PUSH_GETLOCAL(ret, location, table_size - body->
param.post_start - (
int) i, 0);
6920 switch (body->type) {
6921 case ISEQ_TYPE_PLAIN: {
6922 RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
6925 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6929 case ISEQ_TYPE_BLOCK: {
6930 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6931 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6934 start->rescued = LABEL_RESCUE_BEG;
6935 end->rescued = LABEL_RESCUE_END;
6941 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6942 pm_compile_for_node_index(iseq, ((
const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6946 PUSH_INSN(ret, block_location, nop);
6947 PUSH_LABEL(ret, start);
6949 if (scope_node->body != NULL) {
6950 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6951 case PM_POST_EXECUTION_NODE: {
6953 PUSH_INSN1(ret, block_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6957 pm_scope_node_init((
const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6959 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.
line);
6960 pm_scope_node_destroy(&next_scope_node);
6962 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe,
INT2FIX(0), block);
6965 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6967 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6971 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6976 PUSH_INSN(ret, block_location, putnil);
6979 PUSH_LABEL(ret, end);
6981 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6984 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6985 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6988 case ISEQ_TYPE_ENSURE: {
6989 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6990 iseq_set_exception_local_table(iseq);
6992 if (scope_node->body != NULL) {
6993 PM_COMPILE_POPPED((
const pm_node_t *) scope_node->body);
6996 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6997 PUSH_INSN1(ret, statements_location,
throw,
INT2FIX(0));
7000 case ISEQ_TYPE_METHOD: {
7001 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7004 if (scope_node->body) {
7005 PM_COMPILE((
const pm_node_t *) scope_node->body);
7008 PUSH_INSN(ret, location, putnil);
7011 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7014 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
7017 case ISEQ_TYPE_RESCUE: {
7018 iseq_set_exception_local_table(iseq);
7019 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
7021 LABEL *rescue_end = NEW_LABEL(location.
line);
7022 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7024 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7025 PUSH_INSNL(ret, location, branchif, lab);
7026 PUSH_INSNL(ret, location, jump, rescue_end);
7027 PUSH_LABEL(ret, lab);
7029 PM_COMPILE((
const pm_node_t *) scope_node->body);
7030 PUSH_INSN(ret, location, leave);
7031 PUSH_LABEL(ret, rescue_end);
7032 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7035 PM_COMPILE((
const pm_node_t *) scope_node->ast_node);
7037 PUSH_INSN1(ret, location,
throw,
INT2FIX(0));
7042 if (scope_node->body) {
7043 PM_COMPILE((
const pm_node_t *) scope_node->body);
7046 PUSH_INSN(ret, location, putnil);
7051 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7052 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
7054 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.
line;
7057 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7059 PUSH_INSN(ret, location, leave);
7068 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7072 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7073 PUSH_INSN1(ret, *location, putobject, operand);
7078 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7079 PUSH_INSN1(ret, *location, putobject, operand);
7082 PUSH_SEND(ret, *location, id_core_set_variable_alias,
INT2FIX(2));
7083 if (popped) PUSH_INSN(ret, *location, pop);
7089 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7090 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7091 PM_COMPILE_NOT_POPPED(node->
new_name);
7092 PM_COMPILE_NOT_POPPED(node->
old_name);
7094 PUSH_SEND(ret, *location, id_core_set_method_alias,
INT2FIX(3));
7095 if (popped) PUSH_INSN(ret, *location, pop);
7101 LABEL *end_label = NEW_LABEL(location->
line);
7103 PM_COMPILE_NOT_POPPED(node->
left);
7104 if (!popped) PUSH_INSN(ret, *location, dup);
7105 PUSH_INSNL(ret, *location, branchunless, end_label);
7107 if (!popped) PUSH_INSN(ret, *location, pop);
7108 PM_COMPILE(node->
right);
7109 PUSH_LABEL(ret, end_label);
7117 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7122 if (elements->
size) {
7123 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7124 PUSH_INSN1(ret, *location, duparray, value);
7127 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7145 const int max_new_array_size = 0x100;
7146 const unsigned int min_tmp_array_size = 0x40;
7148 int new_array_size = 0;
7149 bool first_chunk =
true;
7155 bool static_literal =
false;
7158#define FLUSH_CHUNK \
7159 if (new_array_size) { \
7160 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7161 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7162 first_chunk = false; \
7163 new_array_size = 0; \
7166 for (
size_t index = 0; index < elements->
size; index++) {
7169 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7174 PM_COMPILE_NOT_POPPED(splat_element->
expression);
7177 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7178 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7184 PUSH_INSN1(ret, *location, splatarray,
Qtrue);
7185 first_chunk =
false;
7188 PUSH_INSN(ret, *location, concattoarray);
7191 static_literal =
false;
7193 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7194 if (new_array_size == 0 && first_chunk) {
7195 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7196 first_chunk =
false;
7213 pm_compile_hash_elements(iseq, element, &keyword_hash->
elements, 0,
Qundef,
false, ret, scope_node);
7220 while (splats < keyword_hash->elements.
size && PM_NODE_TYPE_P(keyword_hash->
elements.
nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7223 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7230 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7231 !PM_CONTAINER_P(element) &&
7233 ((index + min_tmp_array_size) < elements->
size)
7238 size_t right_index = index + 1;
7240 right_index < elements->size &&
7241 PM_NODE_FLAG_P(elements->
nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7242 !PM_CONTAINER_P(elements->
nodes[right_index])
7245 size_t tmp_array_size = right_index - index;
7246 if (tmp_array_size >= min_tmp_array_size) {
7250 for (; tmp_array_size; tmp_array_size--)
7251 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->
nodes[index++], scope_node));
7259 PUSH_INSN1(ret, *location, duparray, tmp_array);
7260 first_chunk =
false;
7263 PUSH_INSN1(ret, *location, putobject, tmp_array);
7264 PUSH_INSN(ret, *location, concattoarray);
7268 PM_COMPILE_NOT_POPPED(element);
7269 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7270 static_literal =
true;
7273 PM_COMPILE_NOT_POPPED(element);
7274 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7275 static_literal =
false;
7280 if (popped) PUSH_INSN(ret, *location, pop);
7288 unsigned long throw_flag = 0;
7290 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7292 LABEL *splabel = NEW_LABEL(0);
7293 PUSH_LABEL(ret, splabel);
7294 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7300 PUSH_INSN(ret, *location, putnil);
7303 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7304 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7305 PUSH_ADJUST_RESTORE(ret, splabel);
7306 if (!popped) PUSH_INSN(ret, *location, putnil);
7312 if (!ISEQ_COMPILE_DATA(ip)) {
7317 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7318 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7320 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7323 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7324 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7328 ip = ISEQ_BODY(ip)->parent_iseq;
7337 PUSH_INSN(ret, *location, putnil);
7340 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7341 if (popped) PUSH_INSN(ret, *location, pop);
7346 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7353 ID method_id = pm_constant_id_lookup(scope_node, node->
name);
7359 const char *builtin_func;
7361 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->
receiver, method_id)) != NULL) {
7362 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7366 LABEL *start = NEW_LABEL(location.
line);
7367 if (node->
block) PUSH_LABEL(ret, start);
7369 switch (method_id) {
7371 if (pm_opt_str_freeze_p(iseq, node)) {
7373 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7374 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7375 if (popped) PUSH_INSN(ret, location, pop);
7381 if (pm_opt_str_freeze_p(iseq, node)) {
7383 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7384 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7385 if (popped) PUSH_INSN(ret, location, pop);
7391 if (pm_opt_aref_with_p(iseq, node)) {
7393 VALUE value = parse_static_literal_string(iseq, scope_node, (
const pm_node_t *)
string, &string->unescaped);
7395 PM_COMPILE_NOT_POPPED(node->
receiver);
7397 const struct rb_callinfo *callinfo = new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE);
7398 PUSH_INSN2(ret, location, opt_aref_with, value, callinfo);
7401 PUSH_INSN(ret, location, pop);
7409 if (pm_opt_aset_with_p(iseq, node)) {
7411 VALUE value = parse_static_literal_string(iseq, scope_node, (
const pm_node_t *)
string, &string->unescaped);
7413 PM_COMPILE_NOT_POPPED(node->
receiver);
7417 PUSH_INSN(ret, location, swap);
7418 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
7421 const struct rb_callinfo *callinfo = new_callinfo(iseq, idASET, 2, 0, NULL, FALSE);
7422 PUSH_INSN2(ret, location, opt_aset_with, value, callinfo);
7423 PUSH_INSN(ret, location, pop);
7430 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7431 PUSH_INSN(ret, location, putnil);
7435 PUSH_INSN(ret, location, putself);
7438 if (method_id == idCall && PM_NODE_TYPE_P(node->
receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7443 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7444 ADD_ELEM(ret, (
LINK_ELEMENT *) new_insn_body(iseq, location.
line, node_id, BIN(getblockparamproxy), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
7447 PM_COMPILE_NOT_POPPED(node->
receiver);
7451 PM_COMPILE_NOT_POPPED(node->
receiver);
7455 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7464 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7465 flag = VM_CALL_FCALL;
7468 PM_COMPILE_NOT_POPPED(node->
receiver);
7470 LABEL *safe_label = NULL;
7471 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7472 safe_label = NEW_LABEL(location->
line);
7473 PUSH_INSN(ret, *location, dup);
7474 PUSH_INSNL(ret, *location, branchnil, safe_label);
7477 PUSH_INSN(ret, *location, dup);
7479 ID id_read_name = pm_constant_id_lookup(scope_node, node->
read_name);
7480 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
7482 PM_COMPILE_NOT_POPPED(node->
value);
7484 PUSH_SEND(ret, *location, id_operator,
INT2FIX(1));
7487 PUSH_INSN(ret, *location, swap);
7488 PUSH_INSN1(ret, *location, topn,
INT2FIX(1));
7491 ID id_write_name = pm_constant_id_lookup(scope_node, node->
write_name);
7492 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
7494 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7495 PUSH_INSN(ret, *location, pop);
7496 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7520 switch (PM_NODE_TYPE(node)) {
7521 case PM_FLOAT_NODE: {
7522 key = pm_static_literal_value(iseq, node, scope_node);
7526 key = (
FIXABLE(intptr) ?
LONG2FIX((
long) intptr) : rb_dbl2big(intptr));
7532 case PM_INTEGER_NODE:
7534 case PM_SOURCE_FILE_NODE:
7535 case PM_SOURCE_LINE_NODE:
7536 case PM_SYMBOL_NODE:
7538 key = pm_static_literal_value(iseq, node, scope_node);
7540 case PM_STRING_NODE: {
7542 key = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
7549 if (
NIL_P(rb_hash_lookup(dispatch, key))) {
7550 rb_hash_aset(dispatch, key, ((
VALUE) label) | 1);
7569 DECL_ANCHOR(cond_seq);
7574 DECL_ANCHOR(body_seq);
7578 LABEL *end_label = NEW_LABEL(location.
line);
7589 if (PM_BRANCH_COVERAGE_P(iseq)) {
7590 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7591 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7597 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7601 int clause_lineno = pm_node_line_number(parser, (
const pm_node_t *) clause);
7602 LABEL *label = NEW_LABEL(clause_lineno);
7603 PUSH_LABEL(body_seq, label);
7606 if (PM_BRANCH_COVERAGE_P(iseq)) {
7608 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7611 if (clause->statements != NULL) {
7612 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7615 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7618 PUSH_INSNL(body_seq, location, jump, end_label);
7623 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7626 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7628 PUSH_INSN(cond_seq, cond_location, putnil);
7629 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7630 PUSH_INSN1(cond_seq, cond_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7631 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7634 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7635 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label,
false, scope_node);
7636 PUSH_LABEL(cond_seq, next_label);
7643 if (PM_BRANCH_COVERAGE_P(iseq)) {
7647 branch_location = case_location;
7654 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7662 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7667 PUSH_INSNL(cond_seq, location, jump, end_label);
7668 PUSH_SEQ(ret, cond_seq);
7676 if (PM_BRANCH_COVERAGE_P(iseq)) {
7677 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7678 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7683 LABEL *else_label = NEW_LABEL(location.
line);
7691 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7692 dispatch = rb_hash_new();
7693 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7704 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7709 LABEL *label = NEW_LABEL(clause_location.
line);
7714 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7716 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7721 if (dispatch !=
Qundef) {
7722 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7725 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7726 PUSH_INSN(cond_seq, condition_location, dup);
7727 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7728 PUSH_INSN1(cond_seq, condition_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7731 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7733 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->
unescaped);
7734 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7737 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7740 PUSH_INSN1(cond_seq, condition_location, topn,
INT2FIX(1));
7741 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7744 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7751 PUSH_LABEL(body_seq, label);
7752 PUSH_INSN(body_seq, clause_location, pop);
7755 if (PM_BRANCH_COVERAGE_P(iseq)) {
7757 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7760 if (clause->statements != NULL) {
7761 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7764 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7767 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7778 if (dispatch !=
Qundef) {
7779 PUSH_INSN(ret, location, dup);
7780 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7781 LABEL_REF(else_label);
7784 PUSH_SEQ(ret, cond_seq);
7788 PUSH_LABEL(ret, else_label);
7792 PUSH_INSN(ret, else_location, pop);
7795 if (PM_BRANCH_COVERAGE_P(iseq)) {
7797 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7801 PUSH_INSNL(ret, else_location, jump, end_label);
7804 PUSH_INSN(ret, location, pop);
7807 if (PM_BRANCH_COVERAGE_P(iseq)) {
7808 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7811 if (!popped) PUSH_INSN(ret, location, putnil);
7812 PUSH_INSNL(ret, location, jump, end_label);
7816 PUSH_SEQ(ret, body_seq);
7817 PUSH_LABEL(ret, end_label);
7826 DECL_ANCHOR(body_seq);
7831 DECL_ANCHOR(cond_seq);
7835 LABEL *end_label = NEW_LABEL(location->
line);
7840 LABEL *else_label = NEW_LABEL(location->
line);
7848 if (PM_BRANCH_COVERAGE_P(iseq)) {
7849 case_location = pm_code_location(scope_node, (
const pm_node_t *) node);
7850 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location,
"case");
7860 if (in_single_pattern) {
7861 PUSH_INSN(ret, *location, putnil);
7862 PUSH_INSN(ret, *location, putnil);
7863 PUSH_INSN1(ret, *location, putobject,
Qfalse);
7864 PUSH_INSN(ret, *location, putnil);
7868 PUSH_INSN(ret, *location, putnil);
7877 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
7880 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7884 PUSH_INSN(body_seq, in_location, putnil);
7887 LABEL *body_label = NEW_LABEL(in_location.
line);
7888 PUSH_LABEL(body_seq, body_label);
7889 PUSH_INSN1(body_seq, in_location, adjuststack,
INT2FIX(in_single_pattern ? 6 : 2));
7892 if (PM_BRANCH_COVERAGE_P(iseq)) {
7894 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"in", branches);
7901 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7904 PUSH_INSNL(body_seq, in_location, jump, end_label);
7905 LABEL *next_pattern_label = NEW_LABEL(pattern_location.
line);
7907 PUSH_INSN(cond_seq, pattern_location, dup);
7908 pm_compile_pattern(iseq, scope_node, in_node->
pattern, cond_seq, body_label, next_pattern_label, in_single_pattern,
false,
true, 2);
7909 PUSH_LABEL(cond_seq, next_pattern_label);
7910 LABEL_UNREMOVABLE(next_pattern_label);
7919 PUSH_LABEL(cond_seq, else_label);
7920 PUSH_INSN(cond_seq, *location, pop);
7921 PUSH_INSN(cond_seq, *location, pop);
7924 if (PM_BRANCH_COVERAGE_P(iseq)) {
7926 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7929 PM_COMPILE_INTO_ANCHOR(cond_seq, (
const pm_node_t *) else_node);
7930 PUSH_INSNL(cond_seq, *location, jump, end_label);
7931 PUSH_INSN(cond_seq, *location, putnil);
7932 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7937 PUSH_LABEL(cond_seq, else_label);
7940 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7942 if (in_single_pattern) {
7943 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, cond_seq, end_label, popped);
7946 PUSH_INSN1(cond_seq, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7948 PUSH_INSN1(cond_seq, *location, topn,
INT2FIX(2));
7949 PUSH_SEND(cond_seq, *location, id_core_raise,
INT2FIX(2));
7951 PUSH_INSN1(cond_seq, *location, adjuststack,
INT2FIX(3));
7952 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7953 PUSH_INSNL(cond_seq, *location, jump, end_label);
7954 PUSH_INSN1(cond_seq, *location, dupn,
INT2FIX(1));
7955 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7962 PUSH_SEQ(ret, cond_seq);
7963 PUSH_SEQ(ret, body_seq);
7964 PUSH_LABEL(ret, end_label);
7972 LABEL *retry_label = NULL;
7973 LABEL *retry_end_l = NULL;
7975 if (node->
block != NULL) {
7976 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7977 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7979 retry_label = NEW_LABEL(location->
line);
7980 retry_end_l = NEW_LABEL(location->
line);
7982 PUSH_LABEL(ret, retry_label);
7985 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7988 PUSH_INSN(ret, *location, putself);
7989 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7991 if (node->
block != NULL) {
7993 pm_scope_node_init((
const pm_node_t *) node->
block, &next_scope_node, scope_node);
7995 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
7996 pm_scope_node_destroy(&next_scope_node);
8003 const rb_iseq_t *local_iseq = body->local_iseq;
8007 int depth = get_lvar_level(iseq);
8009 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
param.flags.forwardable) {
8010 flag |= VM_CALL_FORWARDING;
8011 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
8012 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
8014 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
8015 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
8017 if (popped) PUSH_INSN(ret, *location, pop);
8019 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8024 if (local_body->
param.flags.has_lead) {
8026 for (
int i = 0; i < local_body->
param.lead_num; i++) {
8027 int idx = local_body->local_table_size - i;
8028 PUSH_GETLOCAL(args, *location, idx, depth);
8030 argc += local_body->
param.lead_num;
8033 if (local_body->
param.flags.has_opt) {
8035 for (
int j = 0; j < local_body->
param.opt_num; j++) {
8036 int idx = local_body->local_table_size - (argc + j);
8037 PUSH_GETLOCAL(args, *location, idx, depth);
8039 argc += local_body->
param.opt_num;
8042 if (local_body->
param.flags.has_rest) {
8044 int idx = local_body->local_table_size - local_body->
param.rest_start;
8045 PUSH_GETLOCAL(args, *location, idx, depth);
8046 PUSH_INSN1(args, *location, splatarray,
Qfalse);
8048 argc = local_body->
param.rest_start + 1;
8049 flag |= VM_CALL_ARGS_SPLAT;
8052 if (local_body->
param.flags.has_post) {
8054 int post_len = local_body->
param.post_num;
8055 int post_start = local_body->
param.post_start;
8058 for (; j < post_len; j++) {
8059 int idx = local_body->local_table_size - (post_start + j);
8060 PUSH_GETLOCAL(args, *location, idx, depth);
8063 if (local_body->
param.flags.has_rest) {
8065 PUSH_INSN1(args, *location, newarray,
INT2FIX(j));
8066 PUSH_INSN(args, *location, concatarray);
8069 argc = post_len + post_start;
8073 const struct rb_iseq_param_keyword *
const local_keyword = local_body->
param.keyword;
8074 if (local_body->
param.flags.has_kw) {
8075 int local_size = local_body->local_table_size;
8078 PUSH_INSN1(args, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8080 if (local_body->
param.flags.has_kwrest) {
8081 int idx = local_body->local_table_size - local_keyword->rest_start;
8082 PUSH_GETLOCAL(args, *location, idx, depth);
8084 PUSH_SEND(args, *location, rb_intern(
"dup"),
INT2FIX(0));
8087 PUSH_INSN1(args, *location, newhash,
INT2FIX(0));
8090 for (; i < local_keyword->num; ++i) {
8091 ID id = local_keyword->table[i];
8092 int idx = local_size - get_local_var_idx(local_iseq,
id);
8096 PUSH_INSN1(args, *location, putobject, operand);
8099 PUSH_GETLOCAL(args, *location, idx, depth);
8102 PUSH_SEND(args, *location, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
8103 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8105 else if (local_body->
param.flags.has_kwrest) {
8106 int idx = local_body->local_table_size - local_keyword->rest_start;
8107 PUSH_GETLOCAL(args, *location, idx, depth);
8109 flag |= VM_CALL_KW_SPLAT;
8112 PUSH_SEQ(ret, args);
8115 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8116 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8119 if (node->
block != NULL) {
8120 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8121 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8122 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8125 if (popped) PUSH_INSN(ret, *location, pop);
8131 LABEL *matched_label = NEW_LABEL(location->
line);
8132 LABEL *unmatched_label = NEW_LABEL(location->
line);
8133 LABEL *done_label = NEW_LABEL(location->
line);
8137 PUSH_INSN(ret, *location, putnil);
8138 PUSH_INSN(ret, *location, putnil);
8139 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8140 PUSH_INSN(ret, *location, putnil);
8141 PUSH_INSN(ret, *location, putnil);
8145 PM_COMPILE_NOT_POPPED(node->
value);
8149 PUSH_INSN(ret, *location, dup);
8156 pm_compile_pattern(iseq, scope_node, node->
pattern, ret, matched_label, unmatched_label,
true,
false,
true, 2);
8161 PUSH_LABEL(ret, unmatched_label);
8162 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, ret, done_label, popped);
8166 PUSH_LABEL(ret, matched_label);
8167 PUSH_INSN1(ret, *location, adjuststack,
INT2FIX(6));
8168 if (!popped) PUSH_INSN(ret, *location, putnil);
8169 PUSH_INSNL(ret, *location, jump, done_label);
8171 PUSH_LABEL(ret, done_label);
8177 LABEL *fail_label = NEW_LABEL(location->
line);
8178 LABEL *end_label = NEW_LABEL(location->
line);
8189 PUSH_INSN1(ret, *location, getglobal, operand);
8192 PUSH_INSN(ret, *location, dup);
8193 PUSH_INSNL(ret, *location, branchunless, fail_label);
8199 if (targets_count == 1) {
8201 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8208 PUSH_INSN1(ret, *location, putobject, operand);
8211 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8212 PUSH_LABEL(ret, fail_label);
8213 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8214 if (popped) PUSH_INSN(ret, *location, pop);
8218 DECL_ANCHOR(fail_anchor);
8222 for (
size_t targets_index = 0; targets_index < targets_count; targets_index++) {
8224 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8229 if (((
size_t) targets_index) < (targets_count - 1)) {
8230 PUSH_INSN(ret, *location, dup);
8235 PUSH_INSN1(ret, *location, putobject, operand);
8238 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8239 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8241 PUSH_INSN(fail_anchor, *location, putnil);
8242 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8246 PUSH_INSNL(ret, *location, jump, end_label);
8250 PUSH_LABEL(ret, fail_label);
8251 PUSH_INSN(ret, *location, pop);
8252 PUSH_SEQ(ret, fail_anchor);
8255 PUSH_LABEL(ret, end_label);
8256 if (popped) PUSH_INSN(ret, *location, pop);
8262 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8263 LABEL *splabel = NEW_LABEL(0);
8264 PUSH_LABEL(ret, splabel);
8270 PUSH_INSN(ret, *location, putnil);
8272 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8274 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8275 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8277 PUSH_ADJUST_RESTORE(ret, splabel);
8278 if (!popped) PUSH_INSN(ret, *location, putnil);
8280 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8281 LABEL *splabel = NEW_LABEL(0);
8283 PUSH_LABEL(ret, splabel);
8284 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8290 PUSH_INSN(ret, *location, putnil);
8293 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8294 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8295 PUSH_ADJUST_RESTORE(ret, splabel);
8296 splabel->unremovable = FALSE;
8298 if (!popped) PUSH_INSN(ret, *location, putnil);
8302 unsigned long throw_flag = 0;
8305 if (!ISEQ_COMPILE_DATA(ip)) {
8310 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8311 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8315 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8318 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8319 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8323 ip = ISEQ_BODY(ip)->parent_iseq;
8331 PUSH_INSN(ret, *location, putnil);
8334 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8335 if (popped) PUSH_INSN(ret, *location, pop);
8338 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8346 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8347 LABEL *splabel = NEW_LABEL(0);
8349 PUSH_LABEL(ret, splabel);
8350 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8351 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8353 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8354 PUSH_ADJUST_RESTORE(ret, splabel);
8355 if (!popped) PUSH_INSN(ret, *location, putnil);
8357 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8358 LABEL *splabel = NEW_LABEL(0);
8360 PUSH_LABEL(ret, splabel);
8361 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8362 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8364 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8365 PUSH_ADJUST_RESTORE(ret, splabel);
8366 if (!popped) PUSH_INSN(ret, *location, putnil);
8372 if (!ISEQ_COMPILE_DATA(ip)) {
8377 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8380 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8383 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8384 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8388 ip = ISEQ_BODY(ip)->parent_iseq;
8392 PUSH_INSN(ret, *location, putnil);
8393 PUSH_INSN1(ret, *location,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8394 if (popped) PUSH_INSN(ret, *location, pop);
8397 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8405 iseq_set_exception_local_table(iseq);
8409 LABEL *exception_match_label = NEW_LABEL(location->
line);
8410 LABEL *rescue_end_label = NEW_LABEL(location->
line);
8420 if (exceptions->
size > 0) {
8421 for (
size_t index = 0; index < exceptions->
size; index++) {
8422 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8423 PM_COMPILE(exceptions->
nodes[index]);
8424 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8425 if (PM_NODE_TYPE_P(exceptions->
nodes[index], PM_SPLAT_NODE)) {
8426 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8428 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(checkmatch_flags));
8429 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8433 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8435 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8436 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8441 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8446 PUSH_LABEL(ret, exception_match_label);
8453 DECL_ANCHOR(writes);
8454 DECL_ANCHOR(cleanup);
8456 pm_compile_target_node(iseq, node->
reference, ret, writes, cleanup, scope_node, NULL);
8457 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8459 PUSH_SEQ(ret, writes);
8460 PUSH_SEQ(ret, cleanup);
8470 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8471 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8476 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8479 PUSH_INSN(ret, *location, putnil);
8482 PUSH_INSN(ret, *location, leave);
8488 PUSH_LABEL(ret, rescue_end_label);
8493 PUSH_GETLOCAL(ret, *location, 1, 0);
8501 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8505 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8506 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8507 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq))
break;
8508 parent_type = ISEQ_BODY(parent_iseq)->type;
8511 switch (parent_type) {
8513 case ISEQ_TYPE_MAIN:
8515 rb_warn(
"argument of top-level return is ignored");
8517 if (parent_iseq == iseq) {
8518 type = ISEQ_TYPE_METHOD;
8525 if (
type == ISEQ_TYPE_METHOD) {
8526 splabel = NEW_LABEL(0);
8527 PUSH_LABEL(ret, splabel);
8528 PUSH_ADJUST(ret, *location, 0);
8531 if (arguments != NULL) {
8532 PM_COMPILE_NOT_POPPED((
const pm_node_t *) arguments);
8535 PUSH_INSN(ret, *location, putnil);
8538 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8539 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8541 PUSH_INSN(ret, *location, leave);
8542 PUSH_ADJUST_RESTORE(ret, splabel);
8543 if (!popped) PUSH_INSN(ret, *location, putnil);
8546 PUSH_INSN1(ret, *location,
throw,
INT2FIX(TAG_RETURN));
8547 if (popped) PUSH_INSN(ret, *location, pop);
8556 LABEL *retry_label = NEW_LABEL(location->
line);
8557 LABEL *retry_end_l = NEW_LABEL(location->
line);
8559 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8561 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8563 PUSH_LABEL(ret, retry_label);
8564 PUSH_INSN(ret, *location, putself);
8568 int argc = pm_setup_args(node->
arguments, node->
block, &flags, &keywords, iseq, ret, scope_node, location);
8569 bool is_forwardable = (node->
arguments != NULL) && PM_NODE_FLAG_P(node->
arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8570 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8572 if (node->
block && PM_NODE_TYPE_P(node->
block, PM_BLOCK_NODE)) {
8574 pm_scope_node_init(node->
block, &next_scope_node, scope_node);
8576 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8577 pm_scope_node_destroy(&next_scope_node);
8581 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8584 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8585 PUSH_INSN(args, *location, splatkw);
8588 PUSH_SEQ(ret, args);
8589 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8590 flags |= VM_CALL_FORWARDING;
8593 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8594 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8599 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8600 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8605 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8607 if (popped) PUSH_INSN(ret, *location, pop);
8608 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8609 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8615 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
8617 case ISEQ_TYPE_MAIN:
8618 case ISEQ_TYPE_CLASS:
8619 COMPILE_ERROR(iseq, location->
line,
"Invalid yield");
8629 argc = pm_setup_args(node->
arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8632 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8633 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8635 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8636 if (popped) PUSH_INSN(ret, *location, pop);
8639 for (
const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8640 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8643 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
8661 int lineno = (int) location.
line;
8663 if (PM_NODE_TYPE_P(node, PM_BEGIN_NODE) && (((
const pm_begin_node_t *) node)->statements == NULL) && (((
const pm_begin_node_t *) node)->rescue_clause != NULL)) {
8668 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((
const pm_begin_node_t *) node)->rescue_clause).line;
8671 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8677 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8678 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8679 event |= RUBY_EVENT_COVERAGE_LINE;
8681 PUSH_TRACE(ret, event);
8684 switch (PM_NODE_TYPE(node)) {
8685 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8690 case PM_ALIAS_METHOD_NODE:
8693 pm_compile_alias_method_node(iseq, (
const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8698 pm_compile_and_node(iseq, (
const pm_and_node_t *) node, &location, ret, popped, scope_node);
8700 case PM_ARGUMENTS_NODE: {
8710 if (elements->
size == 1) {
8713 PM_COMPILE(elements->
nodes[0]);
8716 pm_compile_array_node(iseq, (
const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8720 case PM_ARRAY_NODE: {
8724 pm_compile_array_node(iseq, (
const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8727 case PM_ASSOC_NODE: {
8735 PM_COMPILE(cast->
key);
8736 PM_COMPILE(cast->
value);
8740 case PM_ASSOC_SPLAT_NODE: {
8748 if (cast->
value != NULL) {
8749 PM_COMPILE(cast->
value);
8752 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8753 PUSH_GETLOCAL(ret, location, index.index, index.level);
8758 case PM_BACK_REFERENCE_READ_NODE: {
8763 VALUE backref = pm_compile_back_reference_ref(cast);
8765 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), backref);
8769 case PM_BEGIN_NODE: {
8777 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8781 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8790 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8795 case PM_BLOCK_ARGUMENT_NODE: {
8805 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8806 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(local_index.level));
8816 pm_compile_break_node(iseq, (
const pm_break_node_t *) node, &location, ret, popped, scope_node);
8827 pm_compile_call_node(iseq, (
const pm_call_node_t *) node, ret, popped, scope_node);
8829 case PM_CALL_AND_WRITE_NODE: {
8833 pm_compile_call_and_or_write_node(iseq,
true, cast->
receiver, cast->
value, cast->
write_name, cast->
read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
8836 case PM_CALL_OR_WRITE_NODE: {
8840 pm_compile_call_and_or_write_node(iseq,
false, cast->
receiver, cast->
value, cast->
write_name, cast->
read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
8843 case PM_CALL_OPERATOR_WRITE_NODE:
8858 pm_compile_case_node(iseq, (
const pm_case_node_t *) node, &location, ret, popped, scope_node);
8860 case PM_CASE_MATCH_NODE:
8869 pm_compile_case_match_node(iseq, (
const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8871 case PM_CLASS_NODE: {
8876 ID class_id = pm_constant_id_lookup(scope_node, cast->
name);
8877 VALUE class_name =
rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(class_id)));
8880 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
8882 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.
line);
8883 pm_scope_node_destroy(&next_scope_node);
8886 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8887 (cast->
superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8888 pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
8894 PUSH_INSN(ret, location, putnil);
8899 PUSH_INSN3(ret, location, defineclass, operand, class_iseq,
INT2FIX(flags));
8903 if (popped) PUSH_INSN(ret, location, pop);
8906 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
8910 LABEL *end_label = NEW_LABEL(location.
line);
8912 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8915 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8916 if (!popped) PUSH_INSN(ret, location, dup);
8918 PUSH_INSNL(ret, location, branchunless, end_label);
8919 if (!popped) PUSH_INSN(ret, location, pop);
8921 PM_COMPILE_NOT_POPPED(cast->
value);
8922 if (!popped) PUSH_INSN(ret, location, dup);
8924 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8925 PUSH_LABEL(ret, end_label);
8929 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
8934 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8937 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8938 PM_COMPILE_NOT_POPPED(cast->
value);
8941 int flags = VM_CALL_ARGS_SIMPLE;
8942 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
8944 if (!popped) PUSH_INSN(ret, location, dup);
8945 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8949 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
8953 LABEL *end_label = NEW_LABEL(location.
line);
8954 LABEL *start_label = NEW_LABEL(location.
line);
8956 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8959 PUSH_INSN(ret, location, putnil);
8960 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR), name,
Qtrue);
8961 PUSH_INSNL(ret, location, branchunless, start_label);
8963 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8964 if (!popped) PUSH_INSN(ret, location, dup);
8966 PUSH_INSNL(ret, location, branchif, end_label);
8967 if (!popped) PUSH_INSN(ret, location, pop);
8969 PUSH_LABEL(ret, start_label);
8970 PM_COMPILE_NOT_POPPED(cast->
value);
8971 if (!popped) PUSH_INSN(ret, location, dup);
8973 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8974 PUSH_LABEL(ret, end_label);
8978 case PM_CLASS_VARIABLE_READ_NODE: {
8983 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8984 PUSH_INSN2(ret, location, getclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8988 case PM_CLASS_VARIABLE_WRITE_NODE: {
8992 PM_COMPILE_NOT_POPPED(cast->
value);
8993 if (!popped) PUSH_INSN(ret, location, dup);
8995 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8996 PUSH_INSN2(ret, location, setclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
9000 case PM_CONSTANT_PATH_NODE: {
9005 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) !=
Qnil)) {
9006 ISEQ_BODY(iseq)->ic_size++;
9007 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
9010 DECL_ANCHOR(prefix);
9013 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
9014 if (LIST_INSN_SIZE_ZERO(prefix)) {
9015 PUSH_INSN(ret, location, putnil);
9018 PUSH_SEQ(ret, prefix);
9021 PUSH_SEQ(ret, body);
9024 if (popped) PUSH_INSN(ret, location, pop);
9027 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
9031 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9034 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
9038 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9041 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
9045 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9048 case PM_CONSTANT_PATH_WRITE_NODE: {
9052 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9055 case PM_CONSTANT_READ_NODE: {
9061 pm_compile_constant_read(iseq, name, &cast->
base.
location, location.
node_id, ret, scope_node);
9062 if (popped) PUSH_INSN(ret, location, pop);
9066 case PM_CONSTANT_AND_WRITE_NODE: {
9070 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9073 case PM_CONSTANT_OR_WRITE_NODE: {
9077 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9080 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9084 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9087 case PM_CONSTANT_WRITE_NODE: {
9091 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9101 ID method_name = pm_constant_id_lookup(scope_node, cast->
name);
9104 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9106 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.
line);
9107 pm_scope_node_destroy(&next_scope_node);
9110 PM_COMPILE_NOT_POPPED(cast->
receiver);
9111 PUSH_INSN2(ret, location, definesmethod,
ID2SYM(method_name), method_iseq);
9114 PUSH_INSN2(ret, location, definemethod,
ID2SYM(method_name), method_iseq);
9119 PUSH_INSN1(ret, location, putobject,
ID2SYM(method_name));
9124 case PM_DEFINED_NODE: {
9128 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
false);
9131 case PM_EMBEDDED_STATEMENTS_NODE: {
9140 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9143 if (popped) PUSH_INSN(ret, location, pop);
9146 case PM_EMBEDDED_VARIABLE_NODE: {
9153 case PM_FALSE_NODE: {
9157 PUSH_INSN1(ret, location, putobject,
Qfalse);
9161 case PM_ENSURE_NODE: {
9170 case PM_ELSE_NODE: {
9179 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9184 case PM_FLIP_FLOP_NODE: {
9189 LABEL *final_label = NEW_LABEL(location.
line);
9190 LABEL *then_label = NEW_LABEL(location.
line);
9191 LABEL *else_label = NEW_LABEL(location.
line);
9193 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
9195 PUSH_LABEL(ret, then_label);
9196 PUSH_INSN1(ret, location, putobject,
Qtrue);
9197 PUSH_INSNL(ret, location, jump, final_label);
9198 PUSH_LABEL(ret, else_label);
9199 PUSH_INSN1(ret, location, putobject,
Qfalse);
9200 PUSH_LABEL(ret, final_label);
9204 case PM_FLOAT_NODE: {
9209 PUSH_INSN1(ret, location, putobject, operand);
9218 LABEL *retry_label = NEW_LABEL(location.
line);
9219 LABEL *retry_end_l = NEW_LABEL(location.
line);
9222 PUSH_LABEL(ret, retry_label);
9228 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9230 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.
line);
9231 pm_scope_node_destroy(&next_scope_node);
9233 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9234 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9238 PUSH_SEND_WITH_BLOCK(ret, location, idEach,
INT2FIX(0), child_iseq);
9239 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9241 if (popped) PUSH_INSN(ret, location, pop);
9242 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9243 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9246 case PM_FORWARDING_ARGUMENTS_NODE:
9247 rb_bug(
"Cannot compile a ForwardingArgumentsNode directly\n");
9249 case PM_FORWARDING_SUPER_NODE:
9257 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9261 LABEL *end_label = NEW_LABEL(location.
line);
9264 PUSH_INSN1(ret, location, getglobal, name);
9265 if (!popped) PUSH_INSN(ret, location, dup);
9267 PUSH_INSNL(ret, location, branchunless, end_label);
9268 if (!popped) PUSH_INSN(ret, location, pop);
9270 PM_COMPILE_NOT_POPPED(cast->
value);
9271 if (!popped) PUSH_INSN(ret, location, dup);
9273 PUSH_INSN1(ret, location, setglobal, name);
9274 PUSH_LABEL(ret, end_label);
9278 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9284 PUSH_INSN1(ret, location, getglobal, name);
9285 PM_COMPILE_NOT_POPPED(cast->
value);
9288 int flags = VM_CALL_ARGS_SIMPLE;
9289 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9291 if (!popped) PUSH_INSN(ret, location, dup);
9292 PUSH_INSN1(ret, location, setglobal, name);
9296 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9300 LABEL *set_label = NEW_LABEL(location.
line);
9301 LABEL *end_label = NEW_LABEL(location.
line);
9303 PUSH_INSN(ret, location, putnil);
9306 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR), name,
Qtrue);
9307 PUSH_INSNL(ret, location, branchunless, set_label);
9309 PUSH_INSN1(ret, location, getglobal, name);
9310 if (!popped) PUSH_INSN(ret, location, dup);
9312 PUSH_INSNL(ret, location, branchif, end_label);
9313 if (!popped) PUSH_INSN(ret, location, pop);
9315 PUSH_LABEL(ret, set_label);
9316 PM_COMPILE_NOT_POPPED(cast->
value);
9317 if (!popped) PUSH_INSN(ret, location, dup);
9319 PUSH_INSN1(ret, location, setglobal, name);
9320 PUSH_LABEL(ret, end_label);
9324 case PM_GLOBAL_VARIABLE_READ_NODE: {
9330 PUSH_INSN1(ret, location, getglobal, name);
9331 if (popped) PUSH_INSN(ret, location, pop);
9335 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9339 PM_COMPILE_NOT_POPPED(cast->
value);
9340 if (!popped) PUSH_INSN(ret, location, dup);
9342 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9343 PUSH_INSN1(ret, location, setglobal,
ID2SYM(name));
9347 case PM_HASH_NODE: {
9353 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9361 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
9364 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9365 PUSH_INSN1(ret, location, duphash, value);
9386 for (
size_t index = 0; index < elements->
size; index++) {
9387 PM_COMPILE_POPPED(elements->
nodes[index]);
9391 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9407 pm_compile_conditional(iseq, &location, PM_IF_NODE, (
const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
9410 case PM_IMAGINARY_NODE: {
9415 PUSH_INSN1(ret, location, putobject, operand);
9419 case PM_IMPLICIT_NODE: {
9429 PM_COMPILE(cast->
value);
9435 rb_bug(
"Should not ever enter an in node directly");
9438 case PM_INDEX_OPERATOR_WRITE_NODE: {
9442 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9445 case PM_INDEX_AND_WRITE_NODE: {
9449 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9452 case PM_INDEX_OR_WRITE_NODE: {
9456 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9459 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9463 LABEL *end_label = NEW_LABEL(location.
line);
9465 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9468 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9469 if (!popped) PUSH_INSN(ret, location, dup);
9471 PUSH_INSNL(ret, location, branchunless, end_label);
9472 if (!popped) PUSH_INSN(ret, location, pop);
9474 PM_COMPILE_NOT_POPPED(cast->
value);
9475 if (!popped) PUSH_INSN(ret, location, dup);
9477 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9478 PUSH_LABEL(ret, end_label);
9482 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9487 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9490 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9491 PM_COMPILE_NOT_POPPED(cast->
value);
9494 int flags = VM_CALL_ARGS_SIMPLE;
9495 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9497 if (!popped) PUSH_INSN(ret, location, dup);
9498 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9502 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9506 LABEL *end_label = NEW_LABEL(location.
line);
9508 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9511 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9512 if (!popped) PUSH_INSN(ret, location, dup);
9514 PUSH_INSNL(ret, location, branchif, end_label);
9515 if (!popped) PUSH_INSN(ret, location, pop);
9517 PM_COMPILE_NOT_POPPED(cast->
value);
9518 if (!popped) PUSH_INSN(ret, location, dup);
9520 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9521 PUSH_LABEL(ret, end_label);
9525 case PM_INSTANCE_VARIABLE_READ_NODE: {
9530 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9531 PUSH_INSN2(ret, location, getinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9535 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9539 PM_COMPILE_NOT_POPPED(cast->
value);
9540 if (!popped) PUSH_INSN(ret, location, dup);
9542 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9543 PUSH_INSN2(ret, location, setinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9547 case PM_INTEGER_NODE: {
9552 PUSH_INSN1(ret, location, putobject, operand);
9556 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9559 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9561 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9562 PUSH_INSN1(ret, location, putobject, regexp);
9569 PUSH_INSN1(ret, location, getglobal,
rb_id2sym(idLASTLINE));
9570 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9571 if (popped) PUSH_INSN(ret, location, pop);
9575 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9578 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9579 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9581 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9584 pm_scope_node_init(node, &next_scope_node, scope_node);
9586 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.
line);
9587 pm_scope_node_destroy(&next_scope_node);
9589 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9590 PUSH_INSN2(ret, location, once, block_iseq,
INT2FIX(ise_index));
9591 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9593 if (popped) PUSH_INSN(ret, location, pop);
9597 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9599 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9600 PUSH_INSN1(ret, location, putobject, regexp);
9605 if (popped) PUSH_INSN(ret, location, pop);
9610 case PM_INTERPOLATED_STRING_NODE: {
9613 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9615 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9617 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9618 PUSH_INSN1(ret, location, putobject,
string);
9620 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9621 PUSH_INSN1(ret, location, putstring,
string);
9624 PUSH_INSN1(ret, location, putchilledstring,
string);
9630 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL);
9631 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9632 if (popped) PUSH_INSN(ret, location, pop);
9637 case PM_INTERPOLATED_SYMBOL_NODE: {
9641 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL);
9644 PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9648 PUSH_INSN(ret, location, intern);
9651 PUSH_INSN(ret, location, pop);
9656 case PM_INTERPOLATED_X_STRING_NODE: {
9661 PUSH_INSN(ret, location, putself);
9663 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret,
false, scope_node, NULL, NULL);
9664 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9666 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9667 if (popped) PUSH_INSN(ret, location, pop);
9671 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9678 while (current_scope_node) {
9679 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9680 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9684 current_scope_node = current_scope_node->previous;
9687 rb_bug(
"Local `it` does not exist");
9692 case PM_KEYWORD_HASH_NODE: {
9700 PM_COMPILE(element);
9703 if (!popped) PUSH_INSN1(ret, location, newhash,
INT2FIX(elements->
size * 2));
9706 case PM_LAMBDA_NODE: {
9712 pm_scope_node_init(node, &next_scope_node, scope_node);
9714 int opening_lineno = pm_location_line_number(parser, &cast->
opening_loc);
9715 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9716 pm_scope_node_destroy(&next_scope_node);
9719 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9720 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9723 if (popped) PUSH_INSN(ret, location, pop);
9726 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9730 LABEL *end_label = NEW_LABEL(location.
line);
9733 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9734 if (!popped) PUSH_INSN(ret, location, dup);
9736 PUSH_INSNL(ret, location, branchunless, end_label);
9737 if (!popped) PUSH_INSN(ret, location, pop);
9739 PM_COMPILE_NOT_POPPED(cast->
value);
9740 if (!popped) PUSH_INSN(ret, location, dup);
9742 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9743 PUSH_LABEL(ret, end_label);
9747 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9753 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9755 PM_COMPILE_NOT_POPPED(cast->
value);
9758 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
9760 if (!popped) PUSH_INSN(ret, location, dup);
9761 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9765 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9770 LABEL *set_label = NEW_LABEL(location.
line);
9771 LABEL *end_label = NEW_LABEL(location.
line);
9773 PUSH_INSN1(ret, location, putobject,
Qtrue);
9774 PUSH_INSNL(ret, location, branchunless, set_label);
9777 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9778 if (!popped) PUSH_INSN(ret, location, dup);
9780 PUSH_INSNL(ret, location, branchif, end_label);
9781 if (!popped) PUSH_INSN(ret, location, pop);
9783 PUSH_LABEL(ret, set_label);
9784 PM_COMPILE_NOT_POPPED(cast->
value);
9785 if (!popped) PUSH_INSN(ret, location, dup);
9787 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9788 PUSH_LABEL(ret, end_label);
9792 case PM_LOCAL_VARIABLE_READ_NODE: {
9798 PUSH_GETLOCAL(ret, location, index.index, index.level);
9803 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9807 PM_COMPILE_NOT_POPPED(cast->
value);
9808 if (!popped) PUSH_INSN(ret, location, dup);
9811 PUSH_SETLOCAL(ret, location, index.index, index.level);
9814 case PM_MATCH_LAST_LINE_NODE: {
9817 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9819 PUSH_INSN1(ret, location, putobject, regexp);
9821 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9822 if (popped) PUSH_INSN(ret, location, pop);
9826 case PM_MATCH_PREDICATE_NODE: {
9833 PUSH_INSN(ret, location, putnil);
9836 PM_COMPILE_NOT_POPPED(cast->
value);
9837 PUSH_INSN(ret, location, dup);
9841 LABEL *matched_label = NEW_LABEL(location.
line);
9842 LABEL *unmatched_label = NEW_LABEL(location.
line);
9843 LABEL *done_label = NEW_LABEL(location.
line);
9844 pm_compile_pattern(iseq, scope_node, cast->
pattern, ret, matched_label, unmatched_label,
false,
false,
true, 2);
9848 PUSH_LABEL(ret, unmatched_label);
9849 PUSH_INSN(ret, location, pop);
9850 PUSH_INSN(ret, location, pop);
9852 if (!popped) PUSH_INSN1(ret, location, putobject,
Qfalse);
9853 PUSH_INSNL(ret, location, jump, done_label);
9854 PUSH_INSN(ret, location, putnil);
9858 PUSH_LABEL(ret, matched_label);
9859 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(2));
9860 if (!popped) PUSH_INSN1(ret, location, putobject,
Qtrue);
9861 PUSH_INSNL(ret, location, jump, done_label);
9863 PUSH_LABEL(ret, done_label);
9866 case PM_MATCH_REQUIRED_NODE:
9880 case PM_MATCH_WRITE_NODE:
9889 pm_compile_match_write_node(iseq, (
const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9891 case PM_MISSING_NODE:
9892 rb_bug(
"A pm_missing_node_t should not exist in prism's AST.");
9894 case PM_MODULE_NODE: {
9899 ID module_id = pm_constant_id_lookup(scope_node, cast->
name);
9900 VALUE module_name =
rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(module_id)));
9903 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9905 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.
line);
9906 pm_scope_node_destroy(&next_scope_node);
9908 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
9909 PUSH_INSN(ret, location, putnil);
9910 PUSH_INSN3(ret, location, defineclass,
ID2SYM(module_id), module_iseq,
INT2FIX(flags));
9913 if (popped) PUSH_INSN(ret, location, pop);
9916 case PM_REQUIRED_PARAMETER_NODE: {
9922 PUSH_SETLOCAL(ret, location, index.index, index.level);
9925 case PM_MULTI_WRITE_NODE: {
9936 DECL_ANCHOR(writes);
9937 DECL_ANCHOR(cleanup);
9940 state.position = popped ? 0 : 1;
9941 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9943 PM_COMPILE_NOT_POPPED(cast->
value);
9944 if (!popped) PUSH_INSN(ret, location, dup);
9946 PUSH_SEQ(ret, writes);
9947 if (!popped && state.stack_size >= 1) {
9950 PUSH_INSN1(ret, location, setn,
INT2FIX(state.stack_size));
9955 pm_multi_target_state_update(&state);
9957 PUSH_SEQ(ret, cleanup);
9966 pm_compile_next_node(iseq, (
const pm_next_node_t *) node, &location, ret, popped, scope_node);
9972 PUSH_INSN(ret, location, putnil);
9977 case PM_NO_KEYWORDS_PARAMETER_NODE: {
9980 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9983 case PM_NUMBERED_REFERENCE_READ_NODE: {
9990 VALUE ref = pm_compile_numbered_reference_ref(cast);
9991 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), ref);
9994 PUSH_INSN(ret, location, putnil);
10005 LABEL *end_label = NEW_LABEL(location.
line);
10006 PM_COMPILE_NOT_POPPED(cast->
left);
10008 if (!popped) PUSH_INSN(ret, location, dup);
10009 PUSH_INSNL(ret, location, branchif, end_label);
10011 if (!popped) PUSH_INSN(ret, location, pop);
10012 PM_COMPILE(cast->
right);
10013 PUSH_LABEL(ret, end_label);
10017 case PM_OPTIONAL_PARAMETER_NODE: {
10021 PM_COMPILE_NOT_POPPED(cast->
value);
10024 PUSH_SETLOCAL(ret, location, index.index, index.level);
10028 case PM_PARENTHESES_NODE: {
10036 if (cast->
body != NULL) {
10037 PM_COMPILE(cast->
body);
10039 else if (!popped) {
10040 PUSH_INSN(ret, location, putnil);
10045 case PM_PRE_EXECUTION_NODE: {
10057 DECL_ANCHOR(inner_pre);
10060 DECL_ANCHOR(inner_body);
10065 for (
size_t index = 0; index < body->
size; index++) {
10066 pm_compile_node(iseq, body->
nodes[index], inner_body,
true, scope_node);
10071 PUSH_INSN(inner_body, location, putnil);
10077 PUSH_SEQ(outer_pre, inner_pre);
10078 PUSH_SEQ(outer_pre, inner_body);
10083 case PM_POST_EXECUTION_NODE: {
10087 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10090 pm_scope_node_init(node, &next_scope_node, scope_node);
10091 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10092 pm_scope_node_destroy(&next_scope_node);
10094 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10096 int is_index = ISEQ_BODY(iseq)->ise_size++;
10097 PUSH_INSN2(ret, location, once, child_iseq,
INT2FIX(is_index));
10099 if (popped) PUSH_INSN(ret, location, pop);
10101 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10105 case PM_RANGE_NODE: {
10109 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10111 if (pm_optimizable_range_item_p(cast->
left) && pm_optimizable_range_item_p(cast->
right)) {
10117 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) left) :
Qnil,
10118 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const
pm_integer_node_t *) right) :
Qnil,
10122 PUSH_INSN1(ret, location, putobject, val);
10126 if (cast->
left != NULL) {
10127 PM_COMPILE(cast->
left);
10129 else if (!popped) {
10130 PUSH_INSN(ret, location, putnil);
10133 if (cast->
right != NULL) {
10134 PM_COMPILE(cast->
right);
10136 else if (!popped) {
10137 PUSH_INSN(ret, location, putnil);
10141 PUSH_INSN1(ret, location, newrange,
INT2FIX(exclude_end ? 1 : 0));
10146 case PM_RATIONAL_NODE: {
10150 PUSH_INSN1(ret, location, putobject, parse_rational((
const pm_rational_node_t *) node));
10157 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10159 case PM_REGULAR_EXPRESSION_NODE: {
10163 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10164 PUSH_INSN1(ret, location, putobject, regexp);
10168 case PM_RESCUE_NODE:
10171 pm_compile_rescue_node(iseq, (
const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10173 case PM_RESCUE_MODIFIER_NODE: {
10179 pm_scope_node_init((
const pm_node_t *) cast, &rescue_scope_node, scope_node);
10181 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10182 &rescue_scope_node,
10188 pm_scope_node_destroy(&rescue_scope_node);
10190 LABEL *lstart = NEW_LABEL(location.
line);
10191 LABEL *lend = NEW_LABEL(location.
line);
10192 LABEL *lcont = NEW_LABEL(location.
line);
10194 lstart->rescued = LABEL_RESCUE_BEG;
10195 lend->rescued = LABEL_RESCUE_END;
10197 PUSH_LABEL(ret, lstart);
10199 PUSH_LABEL(ret, lend);
10201 PUSH_INSN(ret, location, nop);
10202 PUSH_LABEL(ret, lcont);
10203 if (popped) PUSH_INSN(ret, location, pop);
10205 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10206 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10209 case PM_RETURN_NODE:
10215 pm_compile_return_node(iseq, (
const pm_return_node_t *) node, &location, ret, popped, scope_node);
10217 case PM_RETRY_NODE: {
10220 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10221 PUSH_INSN(ret, location, putnil);
10222 PUSH_INSN1(ret, location,
throw,
INT2FIX(TAG_RETRY));
10223 if (popped) PUSH_INSN(ret, location, pop);
10226 COMPILE_ERROR(iseq, location.
line,
"Invalid retry");
10231 case PM_SCOPE_NODE:
10232 pm_compile_scope_node(iseq, (
pm_scope_node_t *) node, &location, ret, popped);
10234 case PM_SELF_NODE: {
10238 PUSH_INSN(ret, location, putself);
10242 case PM_SHAREABLE_CONSTANT_NODE: {
10246 pm_node_flags_t shareability = (cast->
base.
flags & (PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY));
10248 switch (PM_NODE_TYPE(cast->
write)) {
10249 case PM_CONSTANT_WRITE_NODE:
10252 case PM_CONSTANT_AND_WRITE_NODE:
10255 case PM_CONSTANT_OR_WRITE_NODE:
10258 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10261 case PM_CONSTANT_PATH_WRITE_NODE:
10264 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10267 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10270 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10274 rb_bug(
"Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->
write)));
10280 case PM_SINGLETON_CLASS_NODE: {
10286 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10287 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit(
"singleton class"), ISEQ_TYPE_CLASS, location.
line);
10288 pm_scope_node_destroy(&next_scope_node);
10291 PUSH_INSN(ret, location, putnil);
10294 CONST_ID(singletonclass,
"singletonclass");
10295 PUSH_INSN3(ret, location, defineclass,
ID2SYM(singletonclass), child_iseq,
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10297 if (popped) PUSH_INSN(ret, location, pop);
10302 case PM_SOURCE_ENCODING_NODE: {
10306 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10307 PUSH_INSN1(ret, location, putobject, value);
10311 case PM_SOURCE_FILE_NODE: {
10316 VALUE string = pm_source_file_value(cast, scope_node);
10318 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10319 PUSH_INSN1(ret, location, putobject,
string);
10321 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10322 PUSH_INSN1(ret, location, putstring,
string);
10325 PUSH_INSN1(ret, location, putchilledstring,
string);
10330 case PM_SOURCE_LINE_NODE: {
10334 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10335 PUSH_INSN1(ret, location, putobject, value);
10339 case PM_SPLAT_NODE: {
10348 PUSH_INSN1(ret, location, splatarray,
Qtrue);
10352 case PM_STATEMENTS_NODE: {
10357 if (body->
size > 0) {
10358 for (
size_t index = 0; index < body->
size - 1; index++) {
10359 PM_COMPILE_POPPED(body->
nodes[index]);
10361 PM_COMPILE(body->
nodes[body->
size - 1]);
10364 PUSH_INSN(ret, location, putnil);
10368 case PM_STRING_NODE: {
10373 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10375 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10376 PUSH_INSN1(ret, location, putobject, value);
10378 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10379 PUSH_INSN1(ret, location, putstring, value);
10382 PUSH_INSN1(ret, location, putchilledstring, value);
10387 case PM_SUPER_NODE:
10391 pm_compile_super_node(iseq, (
const pm_super_node_t *) node, &location, ret, popped, scope_node);
10393 case PM_SYMBOL_NODE: {
10397 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10398 PUSH_INSN1(ret, location, putobject, value);
10402 case PM_TRUE_NODE: {
10406 PUSH_INSN1(ret, location, putobject,
Qtrue);
10410 case PM_UNDEF_NODE: {
10416 for (
size_t index = 0; index < names->
size; index++) {
10417 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10418 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10420 PM_COMPILE_NOT_POPPED(names->
nodes[index]);
10421 PUSH_SEND(ret, location, id_core_undef_method,
INT2NUM(2));
10423 if (index < names->size - 1) {
10424 PUSH_INSN(ret, location, pop);
10428 if (popped) PUSH_INSN(ret, location, pop);
10431 case PM_UNLESS_NODE: {
10443 pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (
const pm_node_t *) cast, statements, (
const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
10446 case PM_UNTIL_NODE: {
10453 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_UNTIL_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10456 case PM_WHILE_NODE: {
10463 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_WHILE_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10466 case PM_X_STRING_NODE: {
10470 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10472 PUSH_INSN(ret, location, putself);
10473 PUSH_INSN1(ret, location, putobject, value);
10474 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10475 if (popped) PUSH_INSN(ret, location, pop);
10479 case PM_YIELD_NODE:
10485 pm_compile_yield_node(iseq, (
const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10488 rb_raise(
rb_eNotImpError,
"node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10493#undef PM_CONTAINER_P
10497pm_iseq_pre_execution_p(
rb_iseq_t *iseq)
10499 switch (ISEQ_BODY(iseq)->
type) {
10500 case ISEQ_TYPE_TOP:
10501 case ISEQ_TYPE_EVAL:
10502 case ISEQ_TYPE_MAIN:
10521 if (pm_iseq_pre_execution_p(iseq)) {
10533 pm_compile_node(iseq, (
const pm_node_t *) node, body,
false, node);
10537 PUSH_SEQ(ret, pre);
10538 PUSH_SEQ(ret, body);
10543 pm_compile_node(iseq, (
const pm_node_t *) node, ret,
false, node);
10546 CHECK(iseq_setup_insn(iseq, ret));
10547 return iseq_setup(iseq, ret);
10557 if (result->
node.ast_node != NULL) {
10558 pm_node_destroy(&result->
parser, result->
node.ast_node);
10563 pm_scope_node_destroy(&result->
node);
10566 pm_parser_free(&result->
parser);
10567 pm_string_free(&result->
input);
10568 pm_options_free(&result->
options);
10604#define PM_COLOR_BOLD "\033[1m"
10605#define PM_COLOR_GRAY "\033[2m"
10606#define PM_COLOR_RED "\033[1;31m"
10607#define PM_COLOR_RESET "\033[m"
10608#define PM_ERROR_TRUNCATE 30
10613 if (errors == NULL)
return NULL;
10617 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.
start, start_line);
10618 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10626 (index < error_list->size) &&
10627 (errors[index].error != NULL) &&
10629 (errors[index].line < start.
line) ||
10636 if (index + 1 < error_list->
size) {
10637 memmove(&errors[index + 1], &errors[index],
sizeof(
pm_parse_error_t) * (error_list->
size - index - 1));
10641 uint32_t column_end;
10643 column_end = end.
column;
10645 column_end = (uint32_t) (newline_list->
offsets[start.
line - start_line + 1] - newline_list->
offsets[start.
line - start_line] - 1);
10649 if (start.
column == column_end) column_end++;
10653 .line = start.
line,
10654 .column_start = start.
column,
10655 .column_end = column_end
10663#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10666pm_parse_errors_format_line(
const pm_parser_t *parser,
const pm_newline_list_t *newline_list,
const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end,
pm_buffer_t *buffer) {
10667 int32_t line_delta = line - parser->
start_line;
10668 assert(line_delta >= 0);
10670 size_t index = (size_t) line_delta;
10671 assert(index < newline_list->size);
10673 const uint8_t *start = &parser->
start[newline_list->
offsets[index]];
10674 const uint8_t *end;
10676 if (index >= newline_list->
size - 1) {
10679 end = &parser->
start[newline_list->
offsets[index + 1]];
10682 pm_buffer_append_format(buffer, number_prefix, line);
10685 bool truncate_end =
false;
10686 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10687 end = start + column_end + PM_ERROR_TRUNCATE;
10688 truncate_end =
true;
10692 if (column_start >= PM_ERROR_TRUNCATE) {
10693 pm_buffer_append_string(buffer,
"... ", 4);
10694 start += column_start;
10697 pm_buffer_append_string(buffer, (
const char *) start, (
size_t) (end - start));
10699 if (truncate_end) {
10700 pm_buffer_append_string(buffer,
" ...\n", 5);
10701 }
else if (end == parser->
end && end[-1] !=
'\n') {
10702 pm_buffer_append_string(buffer,
"\n", 1);
10711 assert(error_list->
size != 0);
10715 const int32_t start_line = parser->
start_line;
10718 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10719 if (errors == NULL)
return;
10725 int32_t first_line_number = errors[0].
line;
10726 int32_t last_line_number = errors[error_list->
size - 1].
line;
10731 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10732 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10733 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10735 if (max_line_number < 10) {
10736 if (highlight > 0) {
10738 .
number_prefix = PM_COLOR_GRAY
"%1" PRIi32
" | " PM_COLOR_RESET,
10739 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10740 .divider = PM_COLOR_GRAY
" ~~~~~" PM_COLOR_RESET
"\n"
10745 .blank_prefix =
" | ",
10746 .divider =
" ~~~~~\n"
10749 }
else if (max_line_number < 100) {
10750 if (highlight > 0) {
10752 .
number_prefix = PM_COLOR_GRAY
"%2" PRIi32
" | " PM_COLOR_RESET,
10753 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10754 .divider = PM_COLOR_GRAY
" ~~~~~~" PM_COLOR_RESET
"\n"
10759 .blank_prefix =
" | ",
10760 .divider =
" ~~~~~~\n"
10763 }
else if (max_line_number < 1000) {
10764 if (highlight > 0) {
10766 .
number_prefix = PM_COLOR_GRAY
"%3" PRIi32
" | " PM_COLOR_RESET,
10767 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10768 .divider = PM_COLOR_GRAY
" ~~~~~~~" PM_COLOR_RESET
"\n"
10773 .blank_prefix =
" | ",
10774 .divider =
" ~~~~~~~\n"
10777 }
else if (max_line_number < 10000) {
10778 if (highlight > 0) {
10780 .
number_prefix = PM_COLOR_GRAY
"%4" PRIi32
" | " PM_COLOR_RESET,
10781 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10782 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10787 .blank_prefix =
" | ",
10788 .divider =
" ~~~~~~~~\n"
10792 if (highlight > 0) {
10794 .
number_prefix = PM_COLOR_GRAY
"%5" PRIi32
" | " PM_COLOR_RESET,
10795 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10796 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10801 .blank_prefix =
" | ",
10802 .divider =
" ~~~~~~~~\n"
10816 uint32_t last_column_start = 0;
10819 for (
size_t index = 0; index < error_list->
size; index++) {
10824 if (error->
line - last_line > 1) {
10825 if (error->
line - last_line > 2) {
10826 if ((index != 0) && (error->
line - last_line > 3)) {
10830 pm_buffer_append_string(buffer,
" ", 2);
10831 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 2, 0, 0, buffer);
10834 pm_buffer_append_string(buffer,
" ", 2);
10835 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 1, 0, 0, buffer);
10840 if ((index == 0) || (error->
line != last_line)) {
10841 if (highlight > 1) {
10842 pm_buffer_append_literal(buffer, PM_COLOR_RED
"> " PM_COLOR_RESET);
10843 }
else if (highlight > 0) {
10844 pm_buffer_append_literal(buffer, PM_COLOR_BOLD
"> " PM_COLOR_RESET);
10846 pm_buffer_append_literal(buffer,
"> ");
10853 for (
size_t next_index = index + 1; next_index < error_list->
size; next_index++) {
10854 if (errors[next_index].line != error->
line)
break;
10855 if (errors[next_index].column_end > column_end) column_end = errors[next_index].
column_end;
10861 const uint8_t *start = &parser->
start[newline_list->
offsets[error->
line - start_line]];
10862 if (start == parser->
end) pm_buffer_append_byte(buffer,
'\n');
10873 pm_buffer_append_string(buffer,
" ", 2);
10877 if (last_column_start >= PM_ERROR_TRUNCATE) {
10878 pm_buffer_append_string(buffer,
" ", 4);
10879 column = last_column_start;
10882 while (column < error->column_start) {
10883 pm_buffer_append_byte(buffer,
' ');
10885 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10886 column += (char_width == 0 ? 1 : char_width);
10889 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10890 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10891 pm_buffer_append_byte(buffer,
'^');
10893 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10894 column += (char_width == 0 ? 1 : char_width);
10896 while (column < error->column_end) {
10897 pm_buffer_append_byte(buffer,
'~');
10899 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10900 column += (char_width == 0 ? 1 : char_width);
10903 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10905 if (inline_messages) {
10906 pm_buffer_append_byte(buffer,
' ');
10907 assert(error->
error != NULL);
10910 pm_buffer_append_string(buffer, message, strlen(message));
10913 pm_buffer_append_byte(buffer,
'\n');
10917 last_line = error->
line;
10920 if (index == error_list->
size - 1) {
10921 next_line = (((int32_t) newline_list->
size) + parser->
start_line);
10930 next_line = errors[index + 1].
line;
10933 if (next_line - last_line > 1) {
10934 pm_buffer_append_string(buffer,
" ", 2);
10935 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10938 if (next_line - last_line > 1) {
10939 pm_buffer_append_string(buffer,
" ", 2);
10940 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10948#undef PM_ERROR_TRUNCATE
10949#undef PM_COLOR_GRAY
10951#undef PM_COLOR_RESET
10962 const size_t start_line = pm_newline_list_line_column(&parser->
newline_list, location->
start, 1).
line;
10963 const size_t end_line = pm_newline_list_line_column(&parser->
newline_list, location->
end, 1).
line;
10966 const uint8_t *end = ((end_line == parser->
newline_list.
size) ? parser->
end : (parser->start + parser->newline_list.offsets[end_line]));
10969 while (start < end) {
10970 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0)
return false;
10986 bool valid_utf8 =
true;
10991 int highlight = rb_stderr_tty_p();
10993 const char *no_color = getenv(
"NO_COLOR");
10994 highlight = (no_color == NULL || no_color[0] ==
'\0') ? 2 : 1;
10998 switch (error->level) {
11005 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
11006 valid_utf8 =
false;
11013 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
11015 pm_buffer_append_format(
11017 "%.*s:%" PRIi32
": %s",
11018 (
int) pm_string_length(filepath),
11019 pm_string_source(filepath),
11024 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
11025 pm_buffer_append_byte(&buffer,
'\n');
11028 pm_list_t error_list = { .
size = 1, .head = list_node, .tail = list_node };
11030 pm_parse_errors_format(parser, &error_list, &buffer, highlight,
false);
11033 VALUE value =
rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11034 pm_buffer_free(&buffer);
11050 pm_buffer_append_format(
11052 "%.*s:%" PRIi32
": syntax error%s found\n",
11053 (
int) pm_string_length(filepath),
11054 pm_string_source(filepath),
11055 (int32_t) pm_location_line_number(parser, &head->location),
11060 pm_parse_errors_format(parser, &parser->
error_list, &buffer, highlight,
true);
11064 if (error != head) pm_buffer_append_byte(&buffer,
'\n');
11065 pm_buffer_append_format(&buffer,
"%.*s:%" PRIi32
": %s", (
int) pm_string_length(filepath), pm_string_source(filepath), (int32_t) pm_location_line_number(parser, &error->location), error->message);
11069 VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->
node.encoding);
11073 VALUE path = rb_enc_str_new((
const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
11076 pm_buffer_free(&buffer);
11095 int coverage_enabled = scope_node->coverage_enabled;
11097 pm_scope_node_init(node, scope_node, NULL);
11100 scope_node->encoding = rb_enc_find(parser->
encoding->
name);
11101 if (!scope_node->encoding) rb_bug(
"Encoding not found %s!", parser->
encoding->
name);
11103 scope_node->coverage_enabled = coverage_enabled;
11107 if (script_lines != NULL) {
11113 rb_ary_push(*script_lines, rb_enc_str_new((
const char *) parser->
start + offset, length, scope_node->encoding));
11121 const char *warning_filepath = (
const char *) pm_string_source(&parser->
filepath);
11124 int line = pm_location_line_number(parser, &warning->
location);
11127 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11130 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11136 VALUE error = pm_parse_process_error(result);
11145 scope_node->parser = parser;
11150 scope_node->constants[index] = rb_intern3((
const char *) constant->
start, constant->
length, scope_node->encoding);
11153 scope_node->index_lookup_table = st_init_numtable();
11155 for (
size_t index = 0; index < locals->
size; index++) {
11156 st_insert(scope_node->index_lookup_table, locals->
ids[index], index);
11170pm_options_frozen_string_literal_init(
pm_options_t *options)
11172 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
11174 switch (frozen_string_literal) {
11175 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11177 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11178 pm_options_frozen_string_literal_set(options,
false);
11180 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11181 pm_options_frozen_string_literal_set(options,
true);
11184 rb_bug(
"pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
11197 const char *start = (
const char *) parser->
start;
11198 const char *end = (
const char *) parser->
end;
11203 size_t last_offset = newline_list->
offsets[newline_list->
size - 1];
11204 bool last_push = start + last_offset != end;
11209 for (
size_t index = 0; index < newline_list->
size - 1; index++) {
11210 size_t offset = newline_list->
offsets[index];
11211 size_t length = newline_list->
offsets[index + 1] - offset;
11213 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
11218 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11228pm_read_file(
pm_string_t *
string,
const char *filepath)
11232 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
11235 WCHAR *wfilepath =
xmalloc(
sizeof(WCHAR) * ((
size_t) length));
11236 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
11241 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
11242 if (file == INVALID_HANDLE_VALUE) {
11245 if (GetLastError() == ERROR_ACCESS_DENIED) {
11246 DWORD attributes = GetFileAttributesW(wfilepath);
11247 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
11257 DWORD file_size = GetFileSize(file, NULL);
11258 if (file_size == INVALID_FILE_SIZE) {
11266 if (file_size == 0) {
11269 const uint8_t source[] =
"";
11270 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11275 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
11276 if (mapping == NULL) {
11283 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
11284 CloseHandle(mapping);
11288 if (source == NULL) {
11292 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
11294#elif defined(_POSIX_MAPPED_FILES)
11296 const int open_mode = O_RDONLY | O_NONBLOCK;
11297 int fd = open(filepath, open_mode);
11304 if (fstat(fd, &sb) == -1) {
11310 if (S_ISDIR(sb.st_mode)) {
11318 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
11327 long len = RSTRING_LEN(contents);
11332 size_t length = (size_t)
len;
11333 uint8_t *source = malloc(length);
11334 memcpy(source, RSTRING_PTR(contents), length);
11335 *
string = (
pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
11341 size_t size = (size_t) sb.st_size;
11342 uint8_t *source = NULL;
11346 const uint8_t source[] =
"";
11347 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11351 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
11352 if (source == MAP_FAILED) {
11358 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
11361 return pm_string_file_init(
string, filepath);
11375 pm_options_frozen_string_literal_init(&result->
options);
11384 err = rb_w32_map_errno(GetLastError());
11416 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11419 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11422 VALUE error = pm_parse_process(result, node, script_lines);
11427 ID id_script_lines = rb_intern(
"SCRIPT_LINES__");
11433 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->
node, &result->
parser));
11447 VALUE error = pm_load_file(result, filepath,
false);
11448 if (
NIL_P(error)) {
11449 error = pm_parse_file(result, filepath, script_lines);
11465 if (!rb_enc_asciicompat(encoding)) {
11469 pm_options_frozen_string_literal_init(&result->
options);
11470 pm_string_constant_init(&result->
input, RSTRING_PTR(source), RSTRING_LEN(source));
11471 pm_options_encoding_set(&result->
options, rb_enc_name(encoding));
11474 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11477 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11480 return pm_parse_process(result, node, script_lines);
11487pm_parse_stdin_fgets(
char *
string,
int size,
void *stream)
11496 const char *cstr = RSTRING_PTR(line);
11497 long length = RSTRING_LEN(line);
11499 memcpy(
string, cstr, length);
11500 string[length] =
'\0';
11506void rb_reset_argf_lineno(
long n);
11516 pm_options_frozen_string_literal_init(&result->
options);
11524 pm_string_owned_init(&result->
input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11528 rb_reset_argf_lineno(0);
11530 return pm_parse_process(result, node, NULL);
11534#define NEW_ISEQ OLD_ISEQ
11536#undef NEW_CHILD_ISEQ
11537#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOCV
Old name of RB_ALLOCV.
#define ALLOC
Old name of RB_ALLOC.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define xcalloc
Old name of ruby_xcalloc.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_stdin
STDIN constant.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
#define PM_ENCODING_US_ASCII_ENTRY
This is the US-ASCII encoding.
#define PM_NODE_LIST_FOREACH(list, index, node)
Loop through each node in the node list, writing each node to the given pm_node_t pointer.
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
struct pm_node * old_name
AliasGlobalVariableNode::old_name.
struct pm_node * new_name
AliasGlobalVariableNode::new_name.
struct pm_node * old_name
AliasMethodNode::old_name.
struct pm_node * new_name
AliasMethodNode::new_name.
struct pm_node * left
AlternationPatternNode::left.
struct pm_node * right
AlternationPatternNode::right.
struct pm_node * left
AndNode::left.
struct pm_node * right
AndNode::right.
pm_node_t base
The embedded base node.
struct pm_node_list arguments
ArgumentsNode::arguments.
struct pm_node_list elements
ArrayNode::elements.
struct pm_node_list requireds
ArrayPatternNode::requireds.
struct pm_node * rest
ArrayPatternNode::rest.
struct pm_node * constant
ArrayPatternNode::constant.
struct pm_node_list posts
ArrayPatternNode::posts.
struct pm_node * value
AssocNode::value.
struct pm_node * key
AssocNode::key.
struct pm_node * value
AssocSplatNode::value.
pm_node_t base
The embedded base node.
struct pm_ensure_node * ensure_clause
BeginNode::ensure_clause.
struct pm_rescue_node * rescue_clause
BeginNode::rescue_clause.
struct pm_statements_node * statements
BeginNode::statements.
struct pm_else_node * else_clause
BeginNode::else_clause.
struct pm_node * expression
BlockArgumentNode::expression.
struct pm_node * parameters
BlockNode::parameters.
struct pm_node * body
BlockNode::body.
pm_constant_id_list_t locals
BlockNode::locals.
struct pm_arguments_node * arguments
BreakNode::arguments.
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
struct pm_node * value
CallAndWriteNode::value.
pm_constant_id_t read_name
CallAndWriteNode::read_name.
pm_constant_id_t write_name
CallAndWriteNode::write_name.
struct pm_node * receiver
CallAndWriteNode::receiver.
pm_location_t closing_loc
CallNode::closing_loc.
struct pm_node * receiver
CallNode::receiver.
pm_constant_id_t name
CallNode::name.
pm_node_t base
The embedded base node.
pm_location_t message_loc
CallNode::message_loc.
struct pm_arguments_node * arguments
CallNode::arguments.
struct pm_node * block
CallNode::block.
pm_constant_id_t read_name
CallOperatorWriteNode::read_name.
pm_constant_id_t binary_operator
CallOperatorWriteNode::binary_operator.
struct pm_node * receiver
CallOperatorWriteNode::receiver.
pm_constant_id_t write_name
CallOperatorWriteNode::write_name.
struct pm_node * value
CallOperatorWriteNode::value.
struct pm_node * receiver
CallOrWriteNode::receiver.
struct pm_node * value
CallOrWriteNode::value.
pm_constant_id_t write_name
CallOrWriteNode::write_name.
pm_constant_id_t read_name
CallOrWriteNode::read_name.
pm_constant_id_t name
CallTargetNode::name.
struct pm_node * receiver
CallTargetNode::receiver.
struct pm_local_variable_target_node * target
CapturePatternNode::target.
struct pm_node * value
CapturePatternNode::value.
struct pm_node_list conditions
CaseMatchNode::conditions.
struct pm_else_node * else_clause
CaseMatchNode::else_clause.
struct pm_node * predicate
CaseMatchNode::predicate.
struct pm_node * predicate
CaseNode::predicate.
struct pm_else_node * else_clause
CaseNode::else_clause.
struct pm_node_list conditions
CaseNode::conditions.
struct pm_node * constant_path
ClassNode::constant_path.
pm_constant_id_list_t locals
ClassNode::locals.
pm_constant_id_t name
ClassNode::name.
struct pm_node * body
ClassNode::body.
struct pm_node * superclass
ClassNode::superclass.
ClassVariableAndWriteNode.
struct pm_node * value
ClassVariableAndWriteNode::value.
pm_constant_id_t name
ClassVariableAndWriteNode::name.
ClassVariableOperatorWriteNode.
pm_constant_id_t name
ClassVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
ClassVariableOperatorWriteNode::binary_operator.
struct pm_node * value
ClassVariableOperatorWriteNode::value.
ClassVariableOrWriteNode.
pm_constant_id_t name
ClassVariableOrWriteNode::name.
struct pm_node * value
ClassVariableOrWriteNode::value.
pm_constant_id_t name
ClassVariableReadNode::name.
pm_constant_id_t name
ClassVariableTargetNode::name.
struct pm_node * value
ClassVariableWriteNode::value.
pm_constant_id_t name
ClassVariableWriteNode::name.
pm_location_t name_loc
ConstantAndWriteNode::name_loc.
pm_constant_id_t name
ConstantAndWriteNode::name.
struct pm_node * value
ConstantAndWriteNode::value.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
ConstantOperatorWriteNode.
pm_constant_id_t name
ConstantOperatorWriteNode::name.
pm_location_t name_loc
ConstantOperatorWriteNode::name_loc.
pm_constant_id_t binary_operator
ConstantOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantOperatorWriteNode::value.
pm_location_t name_loc
ConstantOrWriteNode::name_loc.
pm_constant_id_t name
ConstantOrWriteNode::name.
struct pm_node * value
ConstantOrWriteNode::value.
ConstantPathAndWriteNode.
struct pm_constant_path_node * target
ConstantPathAndWriteNode::target.
struct pm_node * value
ConstantPathAndWriteNode::value.
pm_constant_id_t name
ConstantPathNode::name.
struct pm_node * parent
ConstantPathNode::parent.
ConstantPathOperatorWriteNode.
struct pm_constant_path_node * target
ConstantPathOperatorWriteNode::target.
struct pm_node * value
ConstantPathOperatorWriteNode::value.
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantPathOrWriteNode::value.
struct pm_constant_path_node * target
ConstantPathOrWriteNode::target.
struct pm_node * parent
ConstantPathTargetNode::parent.
pm_constant_id_t name
ConstantPathTargetNode::name.
struct pm_constant_path_node * target
ConstantPathWriteNode::target.
struct pm_node * value
ConstantPathWriteNode::value.
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
pm_node_t base
The embedded base node.
pm_constant_id_t name
ConstantReadNode::name.
A constant in the pool which effectively stores a string.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.
pm_constant_id_t name
ConstantTargetNode::name.
struct pm_node * value
ConstantWriteNode::value.
pm_constant_id_t name
ConstantWriteNode::name.
struct pm_parameters_node * parameters
DefNode::parameters.
pm_constant_id_t name
DefNode::name.
struct pm_node * body
DefNode::body.
struct pm_node * receiver
DefNode::receiver.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
DefNode::locals.
struct pm_node * value
DefinedNode::value.
This struct represents a diagnostic generated during parsing.
pm_location_t location
The location of the diagnostic in the source.
const char * message
The message associated with the diagnostic.
pm_list_node_t node
The embedded base node.
uint8_t level
The level of the diagnostic, see pm_error_level_t and pm_warning_level_t for possible values.
struct pm_statements_node * statements
ElseNode::statements.
struct pm_statements_node * statements
EmbeddedStatementsNode::statements.
struct pm_node * variable
EmbeddedVariableNode::variable.
This struct defines the functions necessary to implement the encoding interface so we can determine h...
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
const char * name
The name of the encoding.
struct pm_statements_node * statements
EnsureNode::statements.
struct pm_node * constant
FindPatternNode::constant.
struct pm_node * right
FindPatternNode::right.
struct pm_node_list requireds
FindPatternNode::requireds.
struct pm_splat_node * left
FindPatternNode::left.
pm_node_t base
The embedded base node.
struct pm_node * left
FlipFlopNode::left.
struct pm_node * right
FlipFlopNode::right.
double value
FloatNode::value.
struct pm_statements_node * statements
ForNode::statements.
struct pm_node * collection
ForNode::collection.
struct pm_block_node * block
ForwardingSuperNode::block.
GlobalVariableAndWriteNode.
struct pm_node * value
GlobalVariableAndWriteNode::value.
pm_constant_id_t name
GlobalVariableAndWriteNode::name.
GlobalVariableOperatorWriteNode.
pm_constant_id_t name
GlobalVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
GlobalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
GlobalVariableOperatorWriteNode::value.
GlobalVariableOrWriteNode.
pm_constant_id_t name
GlobalVariableOrWriteNode::name.
struct pm_node * value
GlobalVariableOrWriteNode::value.
pm_constant_id_t name
GlobalVariableReadNode::name.
GlobalVariableTargetNode.
pm_constant_id_t name
GlobalVariableTargetNode::name.
struct pm_node * value
GlobalVariableWriteNode::value.
pm_constant_id_t name
GlobalVariableWriteNode::name.
struct pm_node_list elements
HashNode::elements.
struct pm_node_list elements
HashPatternNode::elements.
struct pm_node * rest
HashPatternNode::rest.
struct pm_node * constant
HashPatternNode::constant.
struct pm_node * predicate
IfNode::predicate.
struct pm_statements_node * statements
IfNode::statements.
struct pm_node * numeric
ImaginaryNode::numeric.
struct pm_node * value
ImplicitNode::value.
struct pm_statements_node * statements
InNode::statements.
struct pm_node * pattern
InNode::pattern.
struct pm_arguments_node * arguments
IndexAndWriteNode::arguments.
struct pm_node * receiver
IndexAndWriteNode::receiver.
struct pm_block_argument_node * block
IndexAndWriteNode::block.
struct pm_node * value
IndexAndWriteNode::value.
struct pm_block_argument_node * block
IndexOperatorWriteNode::block.
struct pm_node * value
IndexOperatorWriteNode::value.
struct pm_arguments_node * arguments
IndexOperatorWriteNode::arguments.
pm_constant_id_t binary_operator
IndexOperatorWriteNode::binary_operator.
struct pm_node * receiver
IndexOperatorWriteNode::receiver.
struct pm_block_argument_node * block
IndexOrWriteNode::block.
struct pm_node * receiver
IndexOrWriteNode::receiver.
struct pm_node * value
IndexOrWriteNode::value.
struct pm_arguments_node * arguments
IndexOrWriteNode::arguments.
struct pm_node * receiver
IndexTargetNode::receiver.
struct pm_arguments_node * arguments
IndexTargetNode::arguments.
struct pm_block_argument_node * block
IndexTargetNode::block.
InstanceVariableAndWriteNode.
struct pm_node * value
InstanceVariableAndWriteNode::value.
pm_constant_id_t name
InstanceVariableAndWriteNode::name.
InstanceVariableOperatorWriteNode.
struct pm_node * value
InstanceVariableOperatorWriteNode::value.
pm_constant_id_t binary_operator
InstanceVariableOperatorWriteNode::binary_operator.
pm_constant_id_t name
InstanceVariableOperatorWriteNode::name.
InstanceVariableOrWriteNode.
struct pm_node * value
InstanceVariableOrWriteNode::value.
pm_constant_id_t name
InstanceVariableOrWriteNode::name.
InstanceVariableReadNode.
pm_constant_id_t name
InstanceVariableReadNode::name.
InstanceVariableTargetNode.
pm_constant_id_t name
InstanceVariableTargetNode::name.
InstanceVariableWriteNode.
pm_constant_id_t name
InstanceVariableWriteNode::name.
struct pm_node * value
InstanceVariableWriteNode::value.
pm_integer_t value
IntegerNode::value.
A structure represents an arbitrary-sized integer.
size_t length
The number of allocated values.
uint32_t value
Embedded value for small integer.
uint32_t * values
List of 32-bit integers.
bool negative
Whether or not the integer is negative.
InterpolatedMatchLastLineNode.
InterpolatedRegularExpressionNode.
struct pm_node_list parts
InterpolatedStringNode::parts.
struct pm_node_list parts
InterpolatedSymbolNode::parts.
struct pm_node_list parts
InterpolatedXStringNode::parts.
struct pm_node_list elements
KeywordHashNode::elements.
KeywordRestParameterNode.
struct pm_node * body
LambdaNode::body.
pm_location_t opening_loc
LambdaNode::opening_loc.
struct pm_node * parameters
LambdaNode::parameters.
pm_location_t operator_loc
LambdaNode::operator_loc.
pm_constant_id_list_t locals
LambdaNode::locals.
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
This struct represents an abstract linked list that provides common functionality.
struct pm_list_node * next
A pointer to the next node in the list.
This represents the overall linked list.
pm_list_node_t * head
A pointer to the head of the list.
size_t size
The size of the list.
the getlocal and setlocal instructions require two parameters.
LocalVariableAndWriteNode.
pm_constant_id_t name
LocalVariableAndWriteNode::name.
uint32_t depth
LocalVariableAndWriteNode::depth.
struct pm_node * value
LocalVariableAndWriteNode::value.
LocalVariableOperatorWriteNode.
uint32_t depth
LocalVariableOperatorWriteNode::depth.
pm_constant_id_t binary_operator
LocalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
LocalVariableOperatorWriteNode::value.
pm_constant_id_t name
LocalVariableOperatorWriteNode::name.
LocalVariableOrWriteNode.
uint32_t depth
LocalVariableOrWriteNode::depth.
struct pm_node * value
LocalVariableOrWriteNode::value.
pm_constant_id_t name
LocalVariableOrWriteNode::name.
uint32_t depth
LocalVariableReadNode::depth.
pm_constant_id_t name
LocalVariableReadNode::name.
uint32_t depth
LocalVariableTargetNode::depth.
pm_constant_id_t name
LocalVariableTargetNode::name.
struct pm_node * value
LocalVariableWriteNode::value.
uint32_t depth
LocalVariableWriteNode::depth.
pm_constant_id_t name
LocalVariableWriteNode::name.
This represents a range of bytes in the source string to which a node or token corresponds.
const uint8_t * start
A pointer to the start location of the range in the source.
const uint8_t * end
A pointer to the end location of the range in the source.
struct pm_node * pattern
MatchPredicateNode::pattern.
struct pm_node * value
MatchPredicateNode::value.
struct pm_node * value
MatchRequiredNode::value.
struct pm_node * pattern
MatchRequiredNode::pattern.
struct pm_node_list targets
MatchWriteNode::targets.
struct pm_call_node * call
MatchWriteNode::call.
struct pm_node * constant_path
ModuleNode::constant_path.
struct pm_node * body
ModuleNode::body.
pm_constant_id_list_t locals
ModuleNode::locals.
pm_constant_id_t name
ModuleNode::name.
struct pm_node_list lefts
MultiTargetNode::lefts.
struct pm_node * rest
MultiTargetNode::rest.
struct pm_node_list rights
MultiTargetNode::rights.
This is a node in the multi target state linked list.
As we're compiling a multi target, we need to track additional information whenever there is a parent...
struct pm_node * value
MultiWriteNode::value.
struct pm_node * rest
MultiWriteNode::rest.
struct pm_node_list rights
MultiWriteNode::rights.
struct pm_node_list lefts
MultiWriteNode::lefts.
A list of offsets of newlines in a string.
const uint8_t * start
A pointer to the start of the source string.
size_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
struct pm_arguments_node * arguments
NextNode::arguments.
A list of nodes in the source, most often used for lists of children.
size_t size
The number of nodes in the list.
struct pm_node ** nodes
The nodes in the list.
This compiler defines its own concept of the location of a node.
int32_t line
This is the line number of a node.
uint32_t node_id
This is a unique identifier for the node.
This is the base structure that represents a node in the syntax tree.
pm_node_type_t type
This represents the type of the node.
uint32_t node_id
The unique identifier for this node, which is deterministic based on 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.
NumberedReferenceReadNode.
uint32_t number
NumberedReferenceReadNode::number.
OptionalKeywordParameterNode.
pm_constant_id_t name
OptionalKeywordParameterNode::name.
struct pm_node * value
OptionalKeywordParameterNode::value.
struct pm_node * value
OptionalParameterNode::value.
pm_constant_id_t name
OptionalParameterNode::name.
The options that can be passed to the parser.
struct pm_node * left
OrNode::left.
struct pm_node * right
OrNode::right.
struct pm_node * rest
ParametersNode::rest.
struct pm_node_list requireds
ParametersNode::requireds.
struct pm_block_parameter_node * block
ParametersNode::block.
struct pm_node_list optionals
ParametersNode::optionals.
struct pm_node_list posts
ParametersNode::posts.
pm_node_t base
The embedded base node.
struct pm_node * keyword_rest
ParametersNode::keyword_rest.
struct pm_node_list keywords
ParametersNode::keywords.
struct pm_node * body
ParenthesesNode::body.
An error that is going to be formatted into the output.
pm_diagnostic_t * error
A pointer to the diagnostic that was generated during parsing.
uint32_t column_end
The column end of the diagnostic message.
int32_t line
The start line of the diagnostic message.
uint32_t column_start
The column start of the diagnostic message.
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_string_t input
The input that represents the source to be parsed.
pm_parser_t parser
The parser that will do the actual parsing.
pm_options_t options
The options that will be passed to the parser.
This struct represents the overall parser.
const pm_encoding_t * encoding
The encoding functions for the current file is attached to the parser as it's parsing so that it can ...
const uint8_t * end
The pointer to the end of the source.
pm_constant_pool_t constant_pool
This constant pool keeps all of the constants defined throughout the file so that we can reference th...
const uint8_t * start
The pointer to the start of the source.
pm_list_t error_list
The list of errors that have been found while parsing.
pm_list_t warning_list
The list of warnings that have been found while parsing.
int32_t start_line
The line number at the start of the parse.
pm_string_t filepath
This is the path of the file being parsed.
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
struct pm_node * variable
PinnedVariableNode::variable.
struct pm_statements_node * statements
PostExecutionNode::statements.
struct pm_statements_node * statements
PreExecutionNode::statements.
struct pm_statements_node * statements
ProgramNode::statements.
struct pm_node * right
RangeNode::right.
struct pm_node * left
RangeNode::left.
pm_integer_t denominator
RationalNode::denominator.
pm_integer_t numerator
RationalNode::numerator.
RequiredKeywordParameterNode.
pm_constant_id_t name
RequiredParameterNode::name.
struct pm_node * rescue_expression
RescueModifierNode::rescue_expression.
struct pm_node * expression
RescueModifierNode::expression.
struct pm_rescue_node * subsequent
RescueNode::subsequent.
struct pm_node * reference
RescueNode::reference.
struct pm_node_list exceptions
RescueNode::exceptions.
struct pm_statements_node * statements
RescueNode::statements.
struct pm_arguments_node * arguments
ReturnNode::arguments.
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
struct iseq_link_anchor * pre_execution_anchor
This will only be set on the top-level scope node.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
struct pm_node * write
ShareableConstantNode::write.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
SingletonClassNode::locals.
struct pm_node * expression
SingletonClassNode::expression.
struct pm_node * body
SingletonClassNode::body.
pm_string_t filepath
SourceFileNode::filepath.
struct pm_node * expression
SplatNode::expression.
struct pm_node_list body
StatementsNode::body.
pm_node_t base
The embedded base node.
pm_string_t unescaped
StringNode::unescaped.
A generic string type that can have various ownership semantics.
struct pm_arguments_node * arguments
SuperNode::arguments.
struct pm_node * block
SuperNode::block.
pm_string_t unescaped
SymbolNode::unescaped.
pm_node_t base
The embedded base node.
struct pm_node_list names
UndefNode::names.
struct pm_statements_node * statements
UnlessNode::statements.
struct pm_node * predicate
UnlessNode::predicate.
struct pm_else_node * else_clause
UnlessNode::else_clause.
pm_node_t base
The embedded base node.
pm_node_t base
The embedded base node.
pm_string_t unescaped
XStringNode::unescaped.
struct pm_arguments_node * arguments
YieldNode::arguments.
struct rb_iseq_constant_body::@154 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.