Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)
prism_compile.c
1#include "prism.h"
2
8typedef struct {
10 int32_t line;
11
13 uint32_t node_id;
15
16/******************************************************************************/
17/* These macros operate on pm_node_location_t structs as opposed to NODE*s. */
18/******************************************************************************/
19
20#define PUSH_ADJUST(seq, location, label) \
21 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
22
23#define PUSH_ADJUST_RESTORE(seq, label) \
24 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
25
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))
28
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)))
31
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)))
34
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)))
37
38#define PUSH_INSNL(seq, location, insn, label) \
39 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
40
41#define PUSH_LABEL(seq, label) \
42 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
43
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)))
46
47#define PUSH_SEND(seq, location, id, argc) \
48 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
49
50#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
51 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
52
53#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
54 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
55
56#define PUSH_CALL(seq, location, id, argc) \
57 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
58
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)
61
62#define PUSH_TRACE(seq, event) \
63 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
64
65#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
66 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
67
68#define PUSH_SEQ(seq1, seq2) \
69 APPEND_LIST((seq1), (seq2))
70
71#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
72 do { \
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); \
76 } while (0)
77
78/******************************************************************************/
79/* These functions compile getlocal/setlocal instructions but operate on */
80/* prism locations instead of NODEs. */
81/******************************************************************************/
82
83static void
84pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
85{
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)));
88 }
89 else {
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)));
91 }
92 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qfalse);
93}
94
95static void
96pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
97{
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)));
100 }
101 else {
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)));
103 }
104 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
105}
106
107#define PUSH_GETLOCAL(seq, location, idx, level) \
108 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
109
110#define PUSH_SETLOCAL(seq, location, idx, level) \
111 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
112
113/******************************************************************************/
114/* These are helper macros for the compiler. */
115/******************************************************************************/
116
117#define OLD_ISEQ NEW_ISEQ
118#undef NEW_ISEQ
119
120#define NEW_ISEQ(node, name, type, line_no) \
121 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
122
123#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
124#undef NEW_CHILD_ISEQ
125
126#define NEW_CHILD_ISEQ(node, name, type, line_no) \
127 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
128
129#define PM_COMPILE(node) \
130 pm_compile_node(iseq, (node), ret, popped, scope_node)
131
132#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
133 pm_compile_node(iseq, (node), _ret, popped, scope_node)
134
135#define PM_COMPILE_POPPED(node) \
136 pm_compile_node(iseq, (node), ret, true, scope_node)
137
138#define PM_COMPILE_NOT_POPPED(node) \
139 pm_compile_node(iseq, (node), ret, false, scope_node)
140
141#define PM_SPECIAL_CONSTANT_FLAG ((pm_constant_id_t)(1 << 31))
142#define PM_CONSTANT_AND ((pm_constant_id_t)(idAnd | PM_SPECIAL_CONSTANT_FLAG))
143#define PM_CONSTANT_DOT3 ((pm_constant_id_t)(idDot3 | PM_SPECIAL_CONSTANT_FLAG))
144#define PM_CONSTANT_MULT ((pm_constant_id_t)(idMULT | PM_SPECIAL_CONSTANT_FLAG))
145#define PM_CONSTANT_POW ((pm_constant_id_t)(idPow | PM_SPECIAL_CONSTANT_FLAG))
146
147#define PM_NODE_START_LOCATION(parser, node) \
148 ((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 })
149
150#define PM_NODE_END_LOCATION(parser, node) \
151 ((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 })
152
153#define PM_LOCATION_START_LOCATION(parser, location, id) \
154 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
155
156#define PM_NODE_START_LINE_COLUMN(parser, node) \
157 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
158
159#define PM_NODE_END_LINE_COLUMN(parser, node) \
160 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
161
162#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
163 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
164
165static int
166pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
167{
168 return (int) pm_newline_list_line(&parser->newline_list, node->location.start, parser->start_line);
169}
170
171static int
172pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
173 return (int) pm_newline_list_line(&parser->newline_list, location->start, parser->start_line);
174}
175
179static VALUE
180parse_integer_value(const pm_integer_t *integer)
181{
182 VALUE result;
183
184 if (integer->values == NULL) {
185 result = UINT2NUM(integer->value);
186 }
187 else {
188 VALUE string = rb_str_new(NULL, integer->length * 8);
189 unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);
190
191 size_t offset = integer->length * 8;
192 for (size_t value_index = 0; value_index < integer->length; value_index++) {
193 uint32_t value = integer->values[value_index];
194
195 for (int index = 0; index < 8; index++) {
196 int byte = (value >> (4 * index)) & 0xf;
197 bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
198 }
199 }
200
201 result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
202 }
203
204 if (integer->negative) {
205 result = rb_funcall(result, rb_intern("-@"), 0);
206 }
207
208 return result;
209}
210
214static inline VALUE
215parse_integer(const pm_integer_node_t *node)
216{
217 return parse_integer_value(&node->value);
218}
219
223static VALUE
224parse_float(const pm_float_node_t *node)
225{
226 return DBL2NUM(node->value);
227}
228
235static VALUE
236parse_rational(const pm_rational_node_t *node)
237{
238 VALUE numerator = parse_integer_value(&node->numerator);
239 VALUE denominator = parse_integer_value(&node->denominator);
240 return rb_rational_new(numerator, denominator);
241}
242
249static VALUE
250parse_imaginary(const pm_imaginary_node_t *node)
251{
252 VALUE imaginary_part;
253 switch (PM_NODE_TYPE(node->numeric)) {
254 case PM_FLOAT_NODE: {
255 imaginary_part = parse_float((const pm_float_node_t *) node->numeric);
256 break;
257 }
258 case PM_INTEGER_NODE: {
259 imaginary_part = parse_integer((const pm_integer_node_t *) node->numeric);
260 break;
261 }
262 case PM_RATIONAL_NODE: {
263 imaginary_part = parse_rational((const pm_rational_node_t *) node->numeric);
264 break;
265 }
266 default:
267 rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
268 }
269
270 return rb_complex_raw(INT2FIX(0), imaginary_part);
271}
272
273static inline VALUE
274parse_string(const pm_scope_node_t *scope_node, const pm_string_t *string)
275{
276 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), scope_node->encoding);
277}
278
284static inline VALUE
285parse_string_encoded(const pm_node_t *node, const pm_string_t *string, rb_encoding *default_encoding)
286{
287 rb_encoding *encoding;
288
290 encoding = rb_ascii8bit_encoding();
291 }
293 encoding = rb_utf8_encoding();
294 }
295 else {
296 encoding = default_encoding;
297 }
298
299 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
300}
301
302static inline VALUE
303parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
304{
305 rb_encoding *encoding;
306
308 encoding = rb_ascii8bit_encoding();
309 }
311 encoding = rb_utf8_encoding();
312 }
313 else {
314 encoding = scope_node->encoding;
315 }
316
317 VALUE value = rb_enc_literal_str((const char *) pm_string_source(string), pm_string_length(string), encoding);
319
320 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
321 int line_number = pm_node_line_number(scope_node->parser, node);
322 value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
323 }
324
325 return value;
326}
327
328static inline ID
329parse_string_symbol(const pm_scope_node_t *scope_node, const pm_symbol_node_t *symbol)
330{
331 rb_encoding *encoding;
333 encoding = rb_utf8_encoding();
334 }
336 encoding = rb_ascii8bit_encoding();
337 }
339 encoding = rb_usascii_encoding();
340 }
341 else {
342 encoding = scope_node->encoding;
343 }
344
345 return rb_intern3((const char *) pm_string_source(&symbol->unescaped), pm_string_length(&symbol->unescaped), encoding);
346}
347
348static int
349pm_optimizable_range_item_p(const pm_node_t *node)
350{
351 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
352}
353
355static VALUE
356parse_regexp_error(rb_iseq_t *iseq, int32_t line_number, const char *fmt, ...)
357{
358 va_list args;
359 va_start(args, fmt);
360 VALUE error = rb_syntax_error_append(Qnil, rb_iseq_path(iseq), line_number, -1, NULL, "%" PRIsVALUE, args);
361 va_end(args);
362 rb_exc_raise(error);
363}
364
365static VALUE
366parse_regexp_string_part(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
367{
368 // If we were passed an explicit regexp encoding, then we need to double
369 // check that it's okay here for this fragment of the string.
370 rb_encoding *encoding;
371
372 if (explicit_regexp_encoding != NULL) {
373 encoding = explicit_regexp_encoding;
374 }
376 encoding = rb_ascii8bit_encoding();
377 }
379 encoding = rb_utf8_encoding();
380 }
381 else {
382 encoding = implicit_regexp_encoding;
383 }
384
385 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
386 VALUE error = rb_reg_check_preprocess(string);
387
388 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node), "%" PRIsVALUE, rb_obj_as_string(error));
389 return string;
390}
391
392static VALUE
393pm_static_literal_concat(rb_iseq_t *iseq, const pm_node_list_t *nodes, const pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool top)
394{
395 VALUE current = Qnil;
396
397 for (size_t index = 0; index < nodes->size; index++) {
398 const pm_node_t *part = nodes->nodes[index];
399 VALUE string;
400
401 switch (PM_NODE_TYPE(part)) {
402 case PM_STRING_NODE:
403 if (implicit_regexp_encoding != NULL) {
404 if (top) {
405 string = parse_regexp_string_part(iseq, scope_node, part, &((const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
406 }
407 else {
408 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
409 VALUE error = rb_reg_check_preprocess(string);
410 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part), "%" PRIsVALUE, rb_obj_as_string(error));
411 }
412 }
413 else {
414 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
415 }
416 break;
418 string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
419 break;
422 string = pm_static_literal_concat(iseq, &cast->statements->body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
423 break;
424 }
425 default:
426 RUBY_ASSERT(false && "unexpected node type in pm_static_literal_concat");
427 return Qnil;
428 }
429
430 if (current != Qnil) {
431 current = rb_str_concat(current, string);
432 }
433 else {
434 current = string;
435 }
436 }
437
438 return top ? rb_fstring(current) : current;
439}
440
441#define RE_OPTION_ENCODING_SHIFT 8
442#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
443#define ARG_ENCODING_NONE 32
444#define ARG_ENCODING_FIXED 16
445#define ENC_ASCII8BIT 1
446#define ENC_EUC_JP 2
447#define ENC_Windows_31J 3
448#define ENC_UTF8 4
449
454static int
455parse_regexp_flags(const pm_node_t *node)
456{
457 int flags = 0;
458
459 // Check "no encoding" first so that flags don't get clobbered
460 // We're calling `rb_char_to_option_kcode` in this case so that
461 // we don't need to have access to `ARG_ENCODING_NONE`
463 flags |= ARG_ENCODING_NONE;
464 }
465
467 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
468 }
469
471 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
472 }
473
475 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
476 }
477
479 flags |= ONIG_OPTION_IGNORECASE;
480 }
481
483 flags |= ONIG_OPTION_MULTILINE;
484 }
485
487 flags |= ONIG_OPTION_EXTEND;
488 }
489
490 return flags;
491}
492
493#undef RE_OPTION_ENCODING_SHIFT
494#undef RE_OPTION_ENCODING
495#undef ARG_ENCODING_FIXED
496#undef ARG_ENCODING_NONE
497#undef ENC_ASCII8BIT
498#undef ENC_EUC_JP
499#undef ENC_Windows_31J
500#undef ENC_UTF8
501
502static rb_encoding *
503parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
504{
506 return rb_ascii8bit_encoding();
507 }
509 return rb_utf8_encoding();
510 }
512 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
513 }
515 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
516 }
517 else {
518 return NULL;
519 }
520}
521
522static VALUE
523parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
524{
525 VALUE errinfo = rb_errinfo();
526
527 int32_t line_number = pm_node_line_number(scope_node->parser, node);
528 VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(&scope_node->parser->filepath), line_number);
529
530 if (NIL_P(regexp)) {
531 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
532 rb_set_errinfo(errinfo);
533
534 parse_regexp_error(iseq, line_number, "%" PRIsVALUE, message);
535 return Qnil;
536 }
537
538 rb_obj_freeze(regexp);
539 return regexp;
540}
541
542static inline VALUE
543parse_regexp_literal(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
544{
545 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
546 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
547
548 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
549 return parse_regexp(iseq, scope_node, node, string);
550}
551
552static inline VALUE
553parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
554{
555 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
556 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
557
558 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
559 return parse_regexp(iseq, scope_node, node, string);
560}
561
562static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node);
563
564static int
565pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
566{
567 int stack_size = 0;
568 size_t parts_size = parts->size;
569 bool interpolated = false;
570
571 if (parts_size > 0) {
572 VALUE current_string = Qnil;
573 pm_node_location_t current_location = *node_location;
574
575 for (size_t index = 0; index < parts_size; index++) {
576 const pm_node_t *part = parts->nodes[index];
577
578 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
579 const pm_string_node_t *string_node = (const pm_string_node_t *) part;
580 VALUE string_value;
581
582 if (implicit_regexp_encoding == NULL) {
583 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
584 }
585 else {
586 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
587 }
588
589 if (RTEST(current_string)) {
590 current_string = rb_str_concat(current_string, string_value);
591 }
592 else {
593 current_string = string_value;
594 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
595 }
596 }
597 else {
598 interpolated = true;
599
600 if (
602 ((const pm_embedded_statements_node_t *) part)->statements != NULL &&
603 ((const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
604 PM_NODE_TYPE_P(((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
605 ) {
606 const pm_string_node_t *string_node = (const pm_string_node_t *) ((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
607 VALUE string_value;
608
609 if (implicit_regexp_encoding == NULL) {
610 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
611 }
612 else {
613 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
614 }
615
616 if (RTEST(current_string)) {
617 current_string = rb_str_concat(current_string, string_value);
618 }
619 else {
620 current_string = string_value;
621 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
622 }
623 }
624 else {
625 if (!RTEST(current_string)) {
626 rb_encoding *encoding;
627
628 if (implicit_regexp_encoding != NULL) {
629 if (explicit_regexp_encoding != NULL) {
630 encoding = explicit_regexp_encoding;
631 }
632 else if (scope_node->parser->encoding == PM_ENCODING_US_ASCII_ENTRY) {
633 encoding = rb_ascii8bit_encoding();
634 }
635 else {
636 encoding = implicit_regexp_encoding;
637 }
638 }
639 else {
640 encoding = scope_node->encoding;
641 }
642
643 if (parts_size == 1) {
644 current_string = rb_enc_str_new(NULL, 0, encoding);
645 }
646 }
647
648 if (RTEST(current_string)) {
649 VALUE operand = rb_fstring(current_string);
650 PUSH_INSN1(ret, current_location, putobject, operand);
651 stack_size++;
652 }
653
654 PM_COMPILE_NOT_POPPED(part);
655
656 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
657 PUSH_INSN(ret, current_location, dup);
658
659 {
660 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
661 PUSH_INSN1(ret, current_location, objtostring, callinfo);
662 }
663
664 PUSH_INSN(ret, current_location, anytostring);
665
666 current_string = Qnil;
667 stack_size++;
668 }
669 }
670 }
671
672 if (RTEST(current_string)) {
673 current_string = rb_fstring(current_string);
674
675 if (stack_size == 0 && interpolated) {
676 PUSH_INSN1(ret, current_location, putstring, current_string);
677 }
678 else {
679 PUSH_INSN1(ret, current_location, putobject, current_string);
680 }
681
682 current_string = Qnil;
683 stack_size++;
684 }
685 }
686 else {
687 PUSH_INSN(ret, *node_location, putnil);
688 }
689
690 return stack_size;
691}
692
693static void
694pm_compile_regexp_dynamic(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
695{
696 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
697 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
698
699 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding);
700 PUSH_INSN2(ret, *node_location, toregexp, INT2FIX(parse_regexp_flags(node) & 0xFF), INT2FIX(length));
701}
702
703static VALUE
704pm_source_file_value(const pm_source_file_node_t *node, const pm_scope_node_t *scope_node)
705{
706 const pm_string_t *filepath = &node->filepath;
707 size_t length = pm_string_length(filepath);
708
709 if (length > 0) {
710 rb_encoding *filepath_encoding = scope_node->filepath_encoding != NULL ? scope_node->filepath_encoding : rb_utf8_encoding();
711 return rb_enc_interned_str((const char *) pm_string_source(filepath), length, filepath_encoding);
712 }
713 else {
714 return rb_fstring_lit("<compiled>");
715 }
716}
717
722static VALUE
723pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
724{
725 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
726 return rb_str_with_debug_created_info(string, rb_iseq_path(iseq), line_number);
727 }
728 else {
729 return rb_fstring(string);
730 }
731}
732
738static VALUE
739pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
740{
741 // Every node that comes into this function should already be marked as
742 // static literal. If it's not, then we have a bug somewhere.
743 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
744
745 switch (PM_NODE_TYPE(node)) {
746 case PM_ARRAY_NODE: {
747 const pm_array_node_t *cast = (const pm_array_node_t *) node;
748 const pm_node_list_t *elements = &cast->elements;
749
750 VALUE value = rb_ary_hidden_new(elements->size);
751 for (size_t index = 0; index < elements->size; index++) {
752 rb_ary_push(value, pm_static_literal_value(iseq, elements->nodes[index], scope_node));
753 }
754
755 OBJ_FREEZE(value);
756 return value;
757 }
758 case PM_FALSE_NODE:
759 return Qfalse;
760 case PM_FLOAT_NODE:
761 return parse_float((const pm_float_node_t *) node);
762 case PM_HASH_NODE: {
763 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
764 const pm_node_list_t *elements = &cast->elements;
765
766 VALUE array = rb_ary_hidden_new(elements->size * 2);
767 for (size_t index = 0; index < elements->size; index++) {
768 RUBY_ASSERT(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
769 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) elements->nodes[index];
770 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
771 rb_ary_cat(array, pair, 2);
772 }
773
774 VALUE value = rb_hash_new_with_size(elements->size);
775 rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
776
777 value = rb_obj_hide(value);
778 OBJ_FREEZE(value);
779 return value;
780 }
782 return parse_imaginary((const pm_imaginary_node_t *) node);
783 case PM_INTEGER_NODE:
784 return parse_integer((const pm_integer_node_t *) node);
787 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
788 }
791 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
792 }
794 VALUE string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL, false);
795 int line_number = pm_node_line_number(scope_node->parser, node);
796 return pm_static_literal_string(iseq, string, line_number);
797 }
800 VALUE string = pm_static_literal_concat(iseq, &cast->parts, scope_node, NULL, NULL, true);
801
802 return ID2SYM(rb_intern_str(string));
803 }
805 const pm_match_last_line_node_t *cast = (const pm_match_last_line_node_t *) node;
806 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
807 }
808 case PM_NIL_NODE:
809 return Qnil;
810 case PM_RATIONAL_NODE:
811 return parse_rational((const pm_rational_node_t *) node);
814 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
815 }
817 return rb_enc_from_encoding(scope_node->encoding);
818 case PM_SOURCE_FILE_NODE: {
819 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
820 return pm_source_file_value(cast, scope_node);
821 }
823 return INT2FIX(pm_node_line_number(scope_node->parser, node));
824 case PM_STRING_NODE: {
825 const pm_string_node_t *cast = (const pm_string_node_t *) node;
826 return parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
827 }
828 case PM_SYMBOL_NODE:
829 return ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) node));
830 case PM_TRUE_NODE:
831 return Qtrue;
832 default:
833 rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
834 return Qfalse;
835 }
836}
837
841static rb_code_location_t
842pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
843{
844 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
845 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
846
847 return (rb_code_location_t) {
848 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
849 .end_pos = { .lineno = end_location.line, .column = end_location.column }
850 };
851}
852
858#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
859
860static void
861pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
862 LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
863
864static void
865pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
866{
867 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
868
869 DECL_ANCHOR(seq);
870
871 LABEL *label = NEW_LABEL(location.line);
872 if (!then_label) then_label = label;
873 else if (!else_label) else_label = label;
874
875 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
876
877 if (LIST_INSN_SIZE_ONE(seq)) {
878 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
879 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label) return;
880 }
881
882 if (!label->refcnt) {
883 if (popped) PUSH_INSN(ret, location, putnil);
884 }
885 else {
886 PUSH_LABEL(seq, label);
887 }
888
889 PUSH_SEQ(ret, seq);
890 return;
891}
892
893static void
894pm_compile_flip_flop_bound(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
895{
896 const pm_node_location_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .node_id = -1 };
897
898 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
899 PM_COMPILE_NOT_POPPED(node);
900
901 VALUE operand = ID2SYM(rb_intern("$."));
902 PUSH_INSN1(ret, location, getglobal, operand);
903
904 PUSH_SEND(ret, location, idEq, INT2FIX(1));
905 if (popped) PUSH_INSN(ret, location, pop);
906 }
907 else {
908 PM_COMPILE(node);
909 }
910}
911
912static void
913pm_compile_flip_flop(const pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
914{
915 const pm_node_location_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .node_id = -1 };
916 LABEL *lend = NEW_LABEL(location.line);
917
918 int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
919
920 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
921 VALUE key = INT2FIX(count);
922
923 PUSH_INSN2(ret, location, getspecial, key, INT2FIX(0));
924 PUSH_INSNL(ret, location, branchif, lend);
925
926 if (flip_flop_node->left) {
927 pm_compile_flip_flop_bound(iseq, flip_flop_node->left, ret, popped, scope_node);
928 }
929 else {
930 PUSH_INSN(ret, location, putnil);
931 }
932
933 PUSH_INSNL(ret, location, branchunless, else_label);
934 PUSH_INSN1(ret, location, putobject, Qtrue);
935 PUSH_INSN1(ret, location, setspecial, key);
936 if (!again) {
937 PUSH_INSNL(ret, location, jump, then_label);
938 }
939
940 PUSH_LABEL(ret, lend);
941 if (flip_flop_node->right) {
942 pm_compile_flip_flop_bound(iseq, flip_flop_node->right, ret, popped, scope_node);
943 }
944 else {
945 PUSH_INSN(ret, location, putnil);
946 }
947
948 PUSH_INSNL(ret, location, branchunless, then_label);
949 PUSH_INSN1(ret, location, putobject, Qfalse);
950 PUSH_INSN1(ret, location, setspecial, key);
951 PUSH_INSNL(ret, location, jump, then_label);
952}
953
954static void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition);
955
956static void
957pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
958{
959 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
960
961again:
962 switch (PM_NODE_TYPE(cond)) {
963 case PM_AND_NODE: {
964 const pm_and_node_t *cast = (const pm_and_node_t *) cond;
965 pm_compile_logical(iseq, ret, cast->left, NULL, else_label, popped, scope_node);
966
967 cond = cast->right;
968 goto again;
969 }
970 case PM_OR_NODE: {
971 const pm_or_node_t *cast = (const pm_or_node_t *) cond;
972 pm_compile_logical(iseq, ret, cast->left, then_label, NULL, popped, scope_node);
973
974 cond = cast->right;
975 goto again;
976 }
977 case PM_FALSE_NODE:
978 case PM_NIL_NODE:
979 PUSH_INSNL(ret, location, jump, else_label);
980 return;
981 case PM_FLOAT_NODE:
983 case PM_INTEGER_NODE:
984 case PM_LAMBDA_NODE:
985 case PM_RATIONAL_NODE:
987 case PM_STRING_NODE:
988 case PM_SYMBOL_NODE:
989 case PM_TRUE_NODE:
990 PUSH_INSNL(ret, location, jump, then_label);
991 return;
993 pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, popped, scope_node);
994 return;
995 case PM_DEFINED_NODE: {
996 const pm_defined_node_t *cast = (const pm_defined_node_t *) cond;
997 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, true);
998 break;
999 }
1000 default: {
1001 DECL_ANCHOR(cond_seq);
1002 pm_compile_node(iseq, cond, cond_seq, false, scope_node);
1003
1004 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1005 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1006
1007 if (insn->insn_id == BIN(putobject)) {
1008 if (RTEST(insn->operands[0])) {
1009 PUSH_INSNL(ret, location, jump, then_label);
1010 // maybe unreachable
1011 return;
1012 }
1013 else {
1014 PUSH_INSNL(ret, location, jump, else_label);
1015 return;
1016 }
1017 }
1018 }
1019
1020 PUSH_SEQ(ret, cond_seq);
1021 break;
1022 }
1023 }
1024
1025 PUSH_INSNL(ret, location, branchunless, else_label);
1026 PUSH_INSNL(ret, location, jump, then_label);
1027}
1028
1032static void
1033pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_type_t type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *subsequent, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1034{
1035 const pm_node_location_t location = *node_location;
1036 LABEL *then_label = NEW_LABEL(location.line);
1037 LABEL *else_label = NEW_LABEL(location.line);
1038 LABEL *end_label = NULL;
1039
1040 DECL_ANCHOR(cond_seq);
1041 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, false, scope_node);
1042 PUSH_SEQ(ret, cond_seq);
1043
1044 rb_code_location_t conditional_location = { 0 };
1045 VALUE branches = Qfalse;
1046
1047 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1048 conditional_location = pm_code_location(scope_node, node);
1049 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location, type == PM_IF_NODE ? "if" : "unless");
1050 }
1051
1052 if (then_label->refcnt) {
1053 PUSH_LABEL(ret, then_label);
1054
1055 DECL_ANCHOR(then_seq);
1056
1057 if (statements != NULL) {
1058 pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
1059 }
1060 else if (!popped) {
1061 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1062 }
1063
1064 if (else_label->refcnt) {
1065 // Establish branch coverage for the then block.
1066 if (PM_BRANCH_COVERAGE_P(iseq)) {
1067 rb_code_location_t branch_location;
1068
1069 if (statements != NULL) {
1070 branch_location = pm_code_location(scope_node, (const pm_node_t *) statements);
1071 } else if (type == PM_IF_NODE) {
1072 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1073 branch_location = (rb_code_location_t) {
1074 .beg_pos = { .lineno = predicate_end.line, .column = predicate_end.column },
1075 .end_pos = { .lineno = predicate_end.line, .column = predicate_end.column }
1076 };
1077 } else {
1078 branch_location = conditional_location;
1079 }
1080
1081 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, type == PM_IF_NODE ? "then" : "else", branches);
1082 }
1083
1084 end_label = NEW_LABEL(location.line);
1085 PUSH_INSNL(then_seq, location, jump, end_label);
1086 if (!popped) PUSH_INSN(then_seq, location, pop);
1087 }
1088
1089 PUSH_SEQ(ret, then_seq);
1090 }
1091
1092 if (else_label->refcnt) {
1093 PUSH_LABEL(ret, else_label);
1094
1095 DECL_ANCHOR(else_seq);
1096
1097 if (subsequent != NULL) {
1098 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1099 }
1100 else if (!popped) {
1101 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1102 }
1103
1104 // Establish branch coverage for the else block.
1105 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1106 rb_code_location_t branch_location;
1107
1108 if (subsequent == NULL) {
1109 branch_location = conditional_location;
1110 } else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1111 const pm_else_node_t *else_node = (const pm_else_node_t *) subsequent;
1112 branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : (const pm_node_t *) else_node);
1113 } else {
1114 branch_location = pm_code_location(scope_node, (const pm_node_t *) subsequent);
1115 }
1116
1117 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1, type == PM_IF_NODE ? "else" : "then", branches);
1118 }
1119
1120 PUSH_SEQ(ret, else_seq);
1121 }
1122
1123 if (end_label) {
1124 PUSH_LABEL(ret, end_label);
1125 }
1126
1127 return;
1128}
1129
1133static void
1134pm_compile_loop(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_flags_t flags, enum pm_node_type type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1135{
1136 const pm_node_location_t location = *node_location;
1137
1138 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1139 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1140 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1141
1142 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.line); /* next */
1143 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.line); /* redo */
1144 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.line); /* break */
1145 LABEL *end_label = NEW_LABEL(location.line);
1146 LABEL *adjust_label = NEW_LABEL(location.line);
1147
1148 LABEL *next_catch_label = NEW_LABEL(location.line);
1149 LABEL *tmp_label = NULL;
1150
1151 // We're pushing onto the ensure stack because breaks need to break out of
1152 // this loop and not break into the ensure statements within the same
1153 // lexical scope.
1155 push_ensure_entry(iseq, &enl, NULL, NULL);
1156
1157 // begin; end while true
1158 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1159 tmp_label = NEW_LABEL(location.line);
1160 PUSH_INSNL(ret, location, jump, tmp_label);
1161 }
1162 else {
1163 // while true; end
1164 PUSH_INSNL(ret, location, jump, next_label);
1165 }
1166
1167 PUSH_LABEL(ret, adjust_label);
1168 PUSH_INSN(ret, location, putnil);
1169 PUSH_LABEL(ret, next_catch_label);
1170 PUSH_INSN(ret, location, pop);
1171 PUSH_INSNL(ret, location, jump, next_label);
1172 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1173
1174 PUSH_LABEL(ret, redo_label);
1175
1176 // Establish branch coverage for the loop.
1177 if (PM_BRANCH_COVERAGE_P(iseq)) {
1178 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1179 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location, type == PM_WHILE_NODE ? "while" : "until");
1180
1181 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (const pm_node_t *) statements) : loop_location;
1182 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, "body", branches);
1183 }
1184
1185 if (statements != NULL) PM_COMPILE_POPPED((const pm_node_t *) statements);
1186 PUSH_LABEL(ret, next_label);
1187
1188 if (type == PM_WHILE_NODE) {
1189 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1190 }
1191 else if (type == PM_UNTIL_NODE) {
1192 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1193 }
1194
1195 PUSH_LABEL(ret, end_label);
1196 PUSH_ADJUST_RESTORE(ret, adjust_label);
1197 PUSH_INSN(ret, location, putnil);
1198
1199 PUSH_LABEL(ret, break_label);
1200 if (popped) PUSH_INSN(ret, location, pop);
1201
1202 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1203 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1204 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1205
1206 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1207 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1208 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1209 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1210
1211 return;
1212}
1213
1214// This recurses through scopes and finds the local index at any scope level
1215// It also takes a pointer to depth, and increments depth appropriately
1216// according to the depth of the local.
1217static pm_local_index_t
1218pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
1219{
1220 pm_local_index_t lindex = { 0 };
1221 st_data_t local_index;
1222
1223 int level;
1224 for (level = 0; level < start_depth; level++) {
1225 scope_node = scope_node->previous;
1226 }
1227
1228 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1229 level++;
1230
1231 if (scope_node->previous) {
1232 scope_node = scope_node->previous;
1233 }
1234 else {
1235 // We have recursed up all scope nodes
1236 // and have not found the local yet
1237 rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
1238 }
1239 }
1240
1241 lindex.level = level;
1242 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1243 return lindex;
1244}
1245
1246// This returns the CRuby ID which maps to the pm_constant_id_t
1247//
1248// Constant_ids in prism are indexes of the constants in prism's constant pool.
1249// We add a constants mapping on the scope_node which is a mapping from
1250// these constant_id indexes to the CRuby IDs that they represent.
1251// This helper method allows easy access to those IDs
1252static ID
1253pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
1254{
1255 if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
1256 rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
1257 }
1258 return scope_node->constants[constant_id - 1];
1259}
1260
1261static rb_iseq_t *
1262pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t *node, VALUE name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
1263{
1264 debugs("[new_child_iseq]> ---------------------------------------\n");
1265 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1266 int error_state;
1267 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1268 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1269 line_no, parent,
1270 isolated_depth ? isolated_depth + 1 : 0,
1271 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1272
1273 if (error_state) {
1274 RUBY_ASSERT(ret_iseq == NULL);
1275 rb_jump_tag(error_state);
1276 }
1277 debugs("[new_child_iseq]< ---------------------------------------\n");
1278 return ret_iseq;
1279}
1280
1281static int
1282pm_compile_class_path(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1283{
1285 const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
1286
1287 if (parent) {
1288 /* Bar::Foo */
1289 PM_COMPILE(parent);
1290 return VM_DEFINECLASS_FLAG_SCOPED;
1291 }
1292 else {
1293 /* toplevel class ::Foo */
1294 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1295 return VM_DEFINECLASS_FLAG_SCOPED;
1296 }
1297 }
1298 else {
1299 /* class at cbase Foo */
1300 PUSH_INSN1(ret, *node_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1301 return 0;
1302 }
1303}
1304
1309static void
1310pm_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)
1311{
1312 const pm_node_location_t location = *node_location;
1313 LABEL *lfin = NEW_LABEL(location.line);
1314 LABEL *lcfin = NEW_LABEL(location.line);
1315 LABEL *lskip = NULL;
1316
1317 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1318 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1319
1320 PM_COMPILE_NOT_POPPED(receiver);
1321 if (safe_nav) {
1322 lskip = NEW_LABEL(location.line);
1323 PUSH_INSN(ret, location, dup);
1324 PUSH_INSNL(ret, location, branchnil, lskip);
1325 }
1326
1327 PUSH_INSN(ret, location, dup);
1328 PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
1329 if (!popped) PUSH_INSN(ret, location, dup);
1330
1331 if (and_node) {
1332 PUSH_INSNL(ret, location, branchunless, lcfin);
1333 }
1334 else {
1335 PUSH_INSNL(ret, location, branchif, lcfin);
1336 }
1337
1338 if (!popped) PUSH_INSN(ret, location, pop);
1339 PM_COMPILE_NOT_POPPED(value);
1340
1341 if (!popped) {
1342 PUSH_INSN(ret, location, swap);
1343 PUSH_INSN1(ret, location, topn, INT2FIX(1));
1344 }
1345
1346 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1347 PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
1348 PUSH_INSNL(ret, location, jump, lfin);
1349
1350 PUSH_LABEL(ret, lcfin);
1351 if (!popped) PUSH_INSN(ret, location, swap);
1352
1353 PUSH_LABEL(ret, lfin);
1354
1355 if (lskip && popped) PUSH_LABEL(ret, lskip);
1356 PUSH_INSN(ret, location, pop);
1357 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1358}
1359
1360static void pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_flags_t shareability, VALUE path, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, bool top);
1361
1367static void
1368pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_flags_t shareability, VALUE path, bool argument, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
1369{
1370 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1371
1372 // If this element is not popped, then we need to create the hash on the
1373 // stack. Neighboring plain assoc nodes should be grouped together (either
1374 // by newhash or hash merge). Double splat nodes should be merged using the
1375 // merge_kwd method call.
1376 const int max_stack_length = 0x100;
1377 const unsigned int min_tmp_hash_length = 0x800;
1378
1379 int stack_length = 0;
1380 bool first_chunk = true;
1381
1382 // This is an optimization wherein we keep track of whether or not the
1383 // previous element was a static literal. If it was, then we do not attempt
1384 // to check if we have a subhash that can be optimized. If it was not, then
1385 // we do check.
1386 bool static_literal = false;
1387
1388 DECL_ANCHOR(anchor);
1389
1390 // Convert pushed elements to a hash, and merge if needed.
1391#define FLUSH_CHUNK \
1392 if (stack_length) { \
1393 if (first_chunk) { \
1394 PUSH_SEQ(ret, anchor); \
1395 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1396 first_chunk = false; \
1397 } \
1398 else { \
1399 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1400 PUSH_INSN(ret, location, swap); \
1401 PUSH_SEQ(ret, anchor); \
1402 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1403 } \
1404 INIT_ANCHOR(anchor); \
1405 stack_length = 0; \
1406 }
1407
1408 for (size_t index = 0; index < elements->size; index++) {
1409 const pm_node_t *element = elements->nodes[index];
1410
1411 switch (PM_NODE_TYPE(element)) {
1412 case PM_ASSOC_NODE: {
1413 // Pre-allocation check (this branch can be omitted).
1414 if (
1415 (shareability == 0) &&
1416 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1417 (!static_literal && ((index + min_tmp_hash_length) < elements->size)) ||
1418 (first_chunk && stack_length == 0)
1419 )
1420 ) {
1421 // Count the elements that are statically-known.
1422 size_t count = 1;
1423 while (index + count < elements->size && PM_NODE_FLAG_P(elements->nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1424
1425 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1426 // The subsequence of elements in this hash is long enough
1427 // to merit its own hash.
1428 VALUE ary = rb_ary_hidden_new(count);
1429
1430 // Create a hidden hash.
1431 for (size_t tmp_end = index + count; index < tmp_end; index++) {
1432 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[index];
1433
1434 VALUE elem[2] = {
1435 pm_static_literal_value(iseq, assoc->key, scope_node),
1436 pm_static_literal_value(iseq, assoc->value, scope_node)
1437 };
1438
1439 rb_ary_cat(ary, elem, 2);
1440 }
1441 index --;
1442
1443 VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary) / 2);
1444 rb_hash_bulk_insert(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary), hash);
1445 hash = rb_obj_hide(hash);
1446 OBJ_FREEZE(hash);
1447
1448 // Emit optimized code.
1449 FLUSH_CHUNK;
1450 if (first_chunk) {
1451 PUSH_INSN1(ret, location, duphash, hash);
1452 first_chunk = false;
1453 }
1454 else {
1455 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1456 PUSH_INSN(ret, location, swap);
1457 PUSH_INSN1(ret, location, putobject, hash);
1458 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1459 }
1460
1461 break;
1462 }
1463 else {
1464 static_literal = true;
1465 }
1466 }
1467 else {
1468 static_literal = false;
1469 }
1470
1471 // If this is a plain assoc node, then we can compile it directly
1472 // and then add the total number of values on the stack.
1473 if (shareability == 0) {
1474 pm_compile_node(iseq, element, anchor, false, scope_node);
1475 }
1476 else {
1477 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
1478 pm_compile_shareable_constant_value(iseq, assoc->key, shareability, path, ret, scope_node, false);
1479 pm_compile_shareable_constant_value(iseq, assoc->value, shareability, path, ret, scope_node, false);
1480 }
1481
1482 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1483 break;
1484 }
1485 case PM_ASSOC_SPLAT_NODE: {
1486 FLUSH_CHUNK;
1487
1488 const pm_assoc_splat_node_t *assoc_splat = (const pm_assoc_splat_node_t *) element;
1489 bool empty_hash = assoc_splat->value != NULL && (
1490 (PM_NODE_TYPE_P(assoc_splat->value, PM_HASH_NODE) && ((const pm_hash_node_t *) assoc_splat->value)->elements.size == 0) ||
1491 PM_NODE_TYPE_P(assoc_splat->value, PM_NIL_NODE)
1492 );
1493
1494 bool first_element = first_chunk && stack_length == 0;
1495 bool last_element = index == elements->size - 1;
1496 bool only_element = first_element && last_element;
1497
1498 if (empty_hash) {
1499 if (only_element && argument) {
1500 // **{} appears at the only keyword argument in method call,
1501 // so it won't be modified.
1502 //
1503 // This is only done for method calls and not for literal
1504 // hashes, because literal hashes should always result in a
1505 // new hash.
1506 PUSH_INSN(ret, location, putnil);
1507 }
1508 else if (first_element) {
1509 // **{} appears as the first keyword argument, so it may be
1510 // modified. We need to create a fresh hash object.
1511 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1512 }
1513 // Any empty keyword splats that are not the first can be
1514 // ignored since merging an empty hash into the existing hash is
1515 // the same as not merging it.
1516 }
1517 else {
1518 if (only_element && argument) {
1519 // ** is only keyword argument in the method call. Use it
1520 // directly. This will be not be flagged as mutable. This is
1521 // only done for method calls and not for literal hashes,
1522 // because literal hashes should always result in a new
1523 // hash.
1524 if (shareability == 0) {
1525 PM_COMPILE_NOT_POPPED(element);
1526 }
1527 else {
1528 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
1529 }
1530 }
1531 else {
1532 // There is more than one keyword argument, or this is not a
1533 // method call. In that case, we need to add an empty hash
1534 // (if first keyword), or merge the hash to the accumulated
1535 // hash (if not the first keyword).
1536 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1537
1538 if (first_element) {
1539 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1540 }
1541 else {
1542 PUSH_INSN(ret, location, swap);
1543 }
1544
1545 if (shareability == 0) {
1546 PM_COMPILE_NOT_POPPED(element);
1547 }
1548 else {
1549 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
1550 }
1551
1552 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1553 }
1554 }
1555
1556 first_chunk = false;
1557 static_literal = false;
1558 break;
1559 }
1560 default:
1561 RUBY_ASSERT("Invalid node type for hash" && false);
1562 break;
1563 }
1564 }
1565
1566 FLUSH_CHUNK;
1567#undef FLUSH_CHUNK
1568}
1569
1570#define SPLATARRAY_FALSE 0
1571#define SPLATARRAY_TRUE 1
1572#define DUP_SINGLE_KW_SPLAT 2
1573
1574// This is details. Users should call pm_setup_args() instead.
1575static int
1576pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, const bool has_regular_blockarg, struct rb_callinfo_kwarg **kw_arg, int *dup_rest, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
1577{
1578 const pm_node_location_t location = *node_location;
1579
1580 int orig_argc = 0;
1581 bool has_splat = false;
1582 bool has_keyword_splat = false;
1583
1584 if (arguments_node == NULL) {
1585 if (*flags & VM_CALL_FCALL) {
1586 *flags |= VM_CALL_VCALL;
1587 }
1588 }
1589 else {
1590 const pm_node_list_t *arguments = &arguments_node->arguments;
1591 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1592
1593 // We count the number of elements post the splat node that are not keyword elements to
1594 // eventually pass as an argument to newarray
1595 int post_splat_counter = 0;
1596 const pm_node_t *argument;
1597
1598 PM_NODE_LIST_FOREACH(arguments, index, argument) {
1599 switch (PM_NODE_TYPE(argument)) {
1600 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1601 case PM_KEYWORD_HASH_NODE: {
1602 const pm_keyword_hash_node_t *keyword_arg = (const pm_keyword_hash_node_t *) argument;
1603 const pm_node_list_t *elements = &keyword_arg->elements;
1604
1605 if (has_keyword_splat || has_splat) {
1606 *flags |= VM_CALL_KW_SPLAT;
1607 has_keyword_splat = true;
1608
1609 if (elements->size > 1 || !(elements->size == 1 && PM_NODE_TYPE_P(elements->nodes[0], PM_ASSOC_SPLAT_NODE))) {
1610 // A new hash will be created for the keyword arguments
1611 // in this case, so mark the method as passing mutable
1612 // keyword splat.
1613 *flags |= VM_CALL_KW_SPLAT_MUT;
1614 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1615 }
1616 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1617 *flags |= VM_CALL_KW_SPLAT_MUT;
1618 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1619 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1620 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1621 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1622 }
1623 else {
1624 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1625 }
1626 }
1627 else {
1628 // We need to first figure out if all elements of the
1629 // KeywordHashNode are AssocNodes with symbol keys.
1631 // If they are all symbol keys then we can pass them as
1632 // keyword arguments. The first thing we need to do is
1633 // deduplicate. We'll do this using the combination of a
1634 // Ruby hash and a Ruby array.
1635 VALUE stored_indices = rb_hash_new();
1636 VALUE keyword_indices = rb_ary_new_capa(elements->size);
1637
1638 size_t size = 0;
1639 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1640 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1641
1642 // Retrieve the stored index from the hash for this
1643 // keyword.
1644 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1645 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1646
1647 // If this keyword was already seen in the hash,
1648 // then mark the array at that index as false and
1649 // decrement the keyword size.
1650 if (!NIL_P(stored_index)) {
1651 rb_ary_store(keyword_indices, NUM2LONG(stored_index), Qfalse);
1652 size--;
1653 }
1654
1655 // Store (and possibly overwrite) the index for this
1656 // keyword in the hash, mark the array at that index
1657 // as true, and increment the keyword size.
1658 rb_hash_aset(stored_indices, keyword, ULONG2NUM(element_index));
1659 rb_ary_store(keyword_indices, (long) element_index, Qtrue);
1660 size++;
1661 }
1662
1663 *kw_arg = rb_xmalloc_mul_add(size, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
1664 *flags |= VM_CALL_KWARG;
1665
1666 VALUE *keywords = (*kw_arg)->keywords;
1667 (*kw_arg)->references = 0;
1668 (*kw_arg)->keyword_len = (int) size;
1669
1670 size_t keyword_index = 0;
1671 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1672 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1673 bool popped = true;
1674
1675 if (rb_ary_entry(keyword_indices, (long) element_index) == Qtrue) {
1676 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1677 popped = false;
1678 }
1679
1680 PM_COMPILE(assoc->value);
1681 }
1682
1683 RUBY_ASSERT(keyword_index == size);
1684 }
1685 else {
1686 // If they aren't all symbol keys then we need to
1687 // construct a new hash and pass that as an argument.
1688 orig_argc++;
1689 *flags |= VM_CALL_KW_SPLAT;
1690
1691 size_t size = elements->size;
1692 if (size > 1) {
1693 // A new hash will be created for the keyword
1694 // arguments in this case, so mark the method as
1695 // passing mutable keyword splat.
1696 *flags |= VM_CALL_KW_SPLAT_MUT;
1697 }
1698
1699 for (size_t element_index = 0; element_index < size; element_index++) {
1700 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1701 PM_COMPILE_NOT_POPPED(assoc->key);
1702 PM_COMPILE_NOT_POPPED(assoc->value);
1703 }
1704
1705 PUSH_INSN1(ret, location, newhash, INT2FIX(size * 2));
1706 }
1707 }
1708 break;
1709 }
1710 case PM_SPLAT_NODE: {
1711 *flags |= VM_CALL_ARGS_SPLAT;
1712 const pm_splat_node_t *splat_node = (const pm_splat_node_t *) argument;
1713
1714 if (splat_node->expression) {
1715 PM_COMPILE_NOT_POPPED(splat_node->expression);
1716 }
1717 else {
1718 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1719 PUSH_GETLOCAL(ret, location, index.index, index.level);
1720 }
1721
1722 bool first_splat = !has_splat;
1723
1724 if (first_splat) {
1725 // If this is the first splat array seen and it's not the
1726 // last parameter, we want splatarray to dup it.
1727 //
1728 // foo(a, *b, c)
1729 // ^^
1730 if (index + 1 < arguments->size || has_regular_blockarg) {
1731 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ? Qtrue : Qfalse);
1732 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1733 }
1734 // If this is the first spalt array seen and it's the last
1735 // parameter, we don't want splatarray to dup it.
1736 //
1737 // foo(a, *b)
1738 // ^^
1739 else {
1740 PUSH_INSN1(ret, location, splatarray, Qfalse);
1741 }
1742 }
1743 else {
1744 // If this is not the first splat array seen and it is also
1745 // the last parameter, we don't want splatarray to dup it
1746 // and we need to concat the array.
1747 //
1748 // foo(a, *b, *c)
1749 // ^^
1750 PUSH_INSN(ret, location, concattoarray);
1751 }
1752
1753 has_splat = true;
1754 post_splat_counter = 0;
1755
1756 break;
1757 }
1758 case PM_FORWARDING_ARGUMENTS_NODE: { // not counted in argc return value
1759 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1760 *flags |= VM_CALL_FORWARDING;
1761
1762 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1763 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1764
1765 break;
1766 }
1767
1768 orig_argc += 2;
1769
1770 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1771
1772 // Forwarding arguments nodes are treated as foo(*, **, &)
1773 // So foo(...) equals foo(*, **, &) and as such the local
1774 // table for this method is known in advance
1775 //
1776 // Push the *
1777 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1778 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1779 PUSH_INSN1(ret, location, splatarray, Qtrue);
1780
1781 // Push the **
1782 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1783 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1784
1785 // Push the &
1786 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1787 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
1788 PUSH_INSN(ret, location, splatkw);
1789
1790 break;
1791 }
1792 default: {
1793 post_splat_counter++;
1794 PM_COMPILE_NOT_POPPED(argument);
1795
1796 // If we have a splat and we've seen a splat, we need to process
1797 // everything after the splat.
1798 if (has_splat) {
1799 // Stack items are turned into an array and concatenated in
1800 // the following cases:
1801 //
1802 // If the next node is a splat:
1803 //
1804 // foo(*a, b, *c)
1805 //
1806 // If the next node is a kwarg or kwarg splat:
1807 //
1808 // foo(*a, b, c: :d)
1809 // foo(*a, b, **c)
1810 //
1811 // If the next node is NULL (we have hit the end):
1812 //
1813 // foo(*a, b)
1814 if (index == arguments->size - 1) {
1815 RUBY_ASSERT(post_splat_counter > 0);
1816 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
1817 }
1818 else {
1819 pm_node_t *next_arg = arguments->nodes[index + 1];
1820
1821 switch (PM_NODE_TYPE(next_arg)) {
1822 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1823 case PM_KEYWORD_HASH_NODE: {
1824 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1825 PUSH_INSN(ret, location, concatarray);
1826 break;
1827 }
1828 case PM_SPLAT_NODE: {
1829 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1830 PUSH_INSN(ret, location, concatarray);
1831 break;
1832 }
1833 default:
1834 break;
1835 }
1836 }
1837 }
1838 else {
1839 orig_argc++;
1840 }
1841 }
1842 }
1843 }
1844 }
1845
1846 if (has_splat) orig_argc++;
1847 if (has_keyword_splat) orig_argc++;
1848 return orig_argc;
1849}
1850
1855static inline bool
1856pm_setup_args_dup_rest_p(const pm_node_t *node)
1857{
1858 switch (PM_NODE_TYPE(node)) {
1863 case PM_FALSE_NODE:
1864 case PM_FLOAT_NODE:
1866 case PM_IMAGINARY_NODE:
1868 case PM_INTEGER_NODE:
1869 case PM_LAMBDA_NODE:
1871 case PM_NIL_NODE:
1873 case PM_RATIONAL_NODE:
1875 case PM_SELF_NODE:
1876 case PM_STRING_NODE:
1877 case PM_SYMBOL_NODE:
1878 case PM_TRUE_NODE:
1879 return false;
1880 case PM_IMPLICIT_NODE:
1881 return pm_setup_args_dup_rest_p(((const pm_implicit_node_t *) node)->value);
1882 default:
1883 return true;
1884 }
1885}
1886
1890static int
1891pm_setup_args(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
1892{
1893 int dup_rest = SPLATARRAY_TRUE;
1894
1895 const pm_node_list_t *arguments;
1896 size_t arguments_size;
1897
1898 // Calls like foo(1, *f, **hash) that use splat and kwsplat could be
1899 // eligible for eliding duping the rest array (dup_reset=false).
1900 if (
1901 arguments_node != NULL &&
1902 (arguments = &arguments_node->arguments, arguments_size = arguments->size) >= 2 &&
1905 PM_NODE_TYPE_P(arguments->nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1906 ) {
1907 // Start by assuming that dup_rest=false, then check each element of the
1908 // hash to ensure we don't need to flip it back to true (in case one of
1909 // the elements could potentially mutate the array).
1910 dup_rest = SPLATARRAY_FALSE;
1911
1912 const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) arguments->nodes[arguments_size - 1];
1913 const pm_node_list_t *elements = &keyword_hash->elements;
1914
1915 for (size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->size; index++) {
1916 const pm_node_t *element = elements->nodes[index];
1917
1918 switch (PM_NODE_TYPE(element)) {
1919 case PM_ASSOC_NODE: {
1920 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
1921 if (pm_setup_args_dup_rest_p(assoc->key) || pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
1922 break;
1923 }
1924 case PM_ASSOC_SPLAT_NODE: {
1925 const pm_assoc_splat_node_t *assoc = (const pm_assoc_splat_node_t *) element;
1926 if (assoc->value != NULL && pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
1927 break;
1928 }
1929 default:
1930 break;
1931 }
1932 }
1933 }
1934
1935 int initial_dup_rest = dup_rest;
1936 int argc;
1937
1938 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1939 // We compile the `&block_arg` expression first and stitch it later
1940 // since the nature of the expression influences whether splat should
1941 // duplicate the array.
1942 bool regular_block_arg = true;
1943 const pm_node_t *block_expr = ((const pm_block_argument_node_t *)block)->expression;
1944
1945 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1946 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1947 initial_dup_rest = dup_rest;
1948 }
1949
1950 DECL_ANCHOR(block_arg);
1951 pm_compile_node(iseq, block, block_arg, false, scope_node);
1952
1953 *flags |= VM_CALL_ARGS_BLOCKARG;
1954
1955 if (LIST_INSN_SIZE_ONE(block_arg)) {
1956 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
1957 if (IS_INSN(elem)) {
1958 INSN *iobj = (INSN *) elem;
1959 if (iobj->insn_id == BIN(getblockparam)) {
1960 iobj->insn_id = BIN(getblockparamproxy);
1961 }
1962
1963 // Allow splat without duplication for simple one-instruction
1964 // block arguments like `&arg`. It is known that this
1965 // optimization can be too aggressive in some cases. See
1966 // [Bug #16504].
1967 regular_block_arg = false;
1968 }
1969 }
1970
1971 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1972 PUSH_SEQ(ret, block_arg);
1973 }
1974 else {
1975 argc = pm_setup_args_core(arguments_node, block, flags, false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1976 }
1977
1978 // If the dup_rest flag was consumed while compiling the arguments (which
1979 // effectively means we found the splat node), then it would have changed
1980 // during the call to pm_setup_args_core. In this case, we want to add the
1981 // VM_CALL_ARGS_SPLAT_MUT flag.
1982 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
1983 *flags |= VM_CALL_ARGS_SPLAT_MUT;
1984 }
1985
1986 return argc;
1987}
1988
1999static void
2000pm_compile_index_operator_write_node(rb_iseq_t *iseq, const pm_index_operator_write_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
2001{
2002 const pm_node_location_t location = *node_location;
2003 if (!popped) PUSH_INSN(ret, location, putnil);
2004
2005 PM_COMPILE_NOT_POPPED(node->receiver);
2006
2007 int boff = (node->block == NULL ? 0 : 1);
2008 int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2009 struct rb_callinfo_kwarg *keywords = NULL;
2010 int argc = pm_setup_args(node->arguments, (const pm_node_t *) node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
2011
2012 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2013 if (boff) {
2014 PUSH_INSN(ret, location, splatkw);
2015 }
2016 else {
2017 PUSH_INSN(ret, location, dup);
2018 PUSH_INSN(ret, location, splatkw);
2019 PUSH_INSN(ret, location, pop);
2020 }
2021 }
2022
2023 int dup_argn = argc + 1 + boff;
2024 int keyword_len = 0;
2025
2026 if (keywords) {
2027 keyword_len = keywords->keyword_len;
2028 dup_argn += keyword_len;
2029 }
2030
2031 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
2032 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2033 PM_COMPILE_NOT_POPPED(node->value);
2034
2035 ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
2036 PUSH_SEND(ret, location, id_operator, INT2FIX(1));
2037
2038 if (!popped) {
2039 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2040 }
2041 if (flag & VM_CALL_ARGS_SPLAT) {
2042 if (flag & VM_CALL_KW_SPLAT) {
2043 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
2044
2045 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2046 PUSH_INSN1(ret, location, splatarray, Qtrue);
2047 flag |= VM_CALL_ARGS_SPLAT_MUT;
2048 }
2049
2050 PUSH_INSN(ret, location, swap);
2051 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2052 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
2053 PUSH_INSN(ret, location, pop);
2054 }
2055 else {
2056 if (boff > 0) {
2057 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2058 PUSH_INSN(ret, location, swap);
2059 PUSH_INSN(ret, location, pop);
2060 }
2061 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2062 PUSH_INSN(ret, location, swap);
2063 PUSH_INSN1(ret, location, splatarray, Qtrue);
2064 PUSH_INSN(ret, location, swap);
2065 flag |= VM_CALL_ARGS_SPLAT_MUT;
2066 }
2067 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2068 if (boff > 0) {
2069 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2070 PUSH_INSN(ret, location, pop);
2071 PUSH_INSN(ret, location, pop);
2072 }
2073 }
2074
2075 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
2076 }
2077 else if (flag & VM_CALL_KW_SPLAT) {
2078 if (boff > 0) {
2079 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2080 PUSH_INSN(ret, location, swap);
2081 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2082 PUSH_INSN(ret, location, pop);
2083 }
2084 PUSH_INSN(ret, location, swap);
2085 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2086 }
2087 else if (keyword_len) {
2088 PUSH_INSN(ret, location, dup);
2089 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 2));
2090 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
2091 PUSH_INSN(ret, location, pop);
2092 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2093 }
2094 else {
2095 if (boff > 0) {
2096 PUSH_INSN(ret, location, swap);
2097 }
2098 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2099 }
2100
2101 PUSH_INSN(ret, location, pop);
2102}
2103
2116static void
2117pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_t *receiver, const pm_arguments_node_t *arguments, const pm_block_argument_node_t *block, const pm_node_t *value, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
2118{
2119 const pm_node_location_t location = *node_location;
2120 if (!popped) PUSH_INSN(ret, location, putnil);
2121 PM_COMPILE_NOT_POPPED(receiver);
2122
2123 int boff = (block == NULL ? 0 : 1);
2124 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2125 struct rb_callinfo_kwarg *keywords = NULL;
2126 int argc = pm_setup_args(arguments, (const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2127
2128 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2129 if (boff) {
2130 PUSH_INSN(ret, location, splatkw);
2131 }
2132 else {
2133 PUSH_INSN(ret, location, dup);
2134 PUSH_INSN(ret, location, splatkw);
2135 PUSH_INSN(ret, location, pop);
2136 }
2137 }
2138
2139 int dup_argn = argc + 1 + boff;
2140 int keyword_len = 0;
2141
2142 if (keywords) {
2143 keyword_len = keywords->keyword_len;
2144 dup_argn += keyword_len;
2145 }
2146
2147 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
2148 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2149
2150 LABEL *label = NEW_LABEL(location.line);
2151 LABEL *lfin = NEW_LABEL(location.line);
2152
2153 PUSH_INSN(ret, location, dup);
2155 PUSH_INSNL(ret, location, branchunless, label);
2156 }
2157 else {
2158 PUSH_INSNL(ret, location, branchif, label);
2159 }
2160
2161 PUSH_INSN(ret, location, pop);
2162 PM_COMPILE_NOT_POPPED(value);
2163
2164 if (!popped) {
2165 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2166 }
2167
2168 if (flag & VM_CALL_ARGS_SPLAT) {
2169 if (flag & VM_CALL_KW_SPLAT) {
2170 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
2171 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2172 PUSH_INSN1(ret, location, splatarray, Qtrue);
2173 flag |= VM_CALL_ARGS_SPLAT_MUT;
2174 }
2175
2176 PUSH_INSN(ret, location, swap);
2177 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2178 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
2179 PUSH_INSN(ret, location, pop);
2180 }
2181 else {
2182 if (boff > 0) {
2183 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2184 PUSH_INSN(ret, location, swap);
2185 PUSH_INSN(ret, location, pop);
2186 }
2187 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2188 PUSH_INSN(ret, location, swap);
2189 PUSH_INSN1(ret, location, splatarray, Qtrue);
2190 PUSH_INSN(ret, location, swap);
2191 flag |= VM_CALL_ARGS_SPLAT_MUT;
2192 }
2193 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2194 if (boff > 0) {
2195 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2196 PUSH_INSN(ret, location, pop);
2197 PUSH_INSN(ret, location, pop);
2198 }
2199 }
2200
2201 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
2202 }
2203 else if (flag & VM_CALL_KW_SPLAT) {
2204 if (boff > 0) {
2205 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2206 PUSH_INSN(ret, location, swap);
2207 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2208 PUSH_INSN(ret, location, pop);
2209 }
2210
2211 PUSH_INSN(ret, location, swap);
2212 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2213 }
2214 else if (keyword_len) {
2215 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
2216 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 0));
2217 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2218 }
2219 else {
2220 if (boff > 0) {
2221 PUSH_INSN(ret, location, swap);
2222 }
2223 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2224 }
2225
2226 PUSH_INSN(ret, location, pop);
2227 PUSH_INSNL(ret, location, jump, lfin);
2228 PUSH_LABEL(ret, label);
2229 if (!popped) {
2230 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2231 }
2232 PUSH_INSN1(ret, location, adjuststack, INT2FIX(dup_argn + 1));
2233 PUSH_LABEL(ret, lfin);
2234}
2235
2236// When we compile a pattern matching expression, we use the stack as a scratch
2237// space to store lots of different values (consider it like we have a pattern
2238// matching function and we need space for a bunch of different local
2239// variables). The "base index" refers to the index on the stack where we
2240// started compiling the pattern matching expression. These offsets from that
2241// base index indicate the location of the various locals we need.
2242#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2243#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2244#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2245#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2246#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2247
2248// A forward declaration because this is the recursive function that handles
2249// compiling a pattern. It can be reentered by nesting patterns, as in the case
2250// of arrays or hashes.
2251static 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);
2252
2257static int
2258pm_compile_pattern_generic_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, unsigned int base_index)
2259{
2260 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2261 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2262
2263 PUSH_INSN(ret, location, dup);
2264 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2265
2266 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2267 PUSH_INSN1(ret, location, putobject, message);
2268 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2269 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
2270 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2271
2272 PUSH_INSN1(ret, location, putobject, Qfalse);
2273 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2274
2275 PUSH_INSN(ret, location, pop);
2276 PUSH_INSN(ret, location, pop);
2277 PUSH_LABEL(ret, match_succeeded_label);
2278
2279 return COMPILE_OK;
2280}
2281
2287static int
2288pm_compile_pattern_length_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, VALUE length, unsigned int base_index)
2289{
2290 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2291 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2292
2293 PUSH_INSN(ret, location, dup);
2294 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2295
2296 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2297 PUSH_INSN1(ret, location, putobject, message);
2298 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2299 PUSH_INSN(ret, location, dup);
2300 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2301 PUSH_INSN1(ret, location, putobject, length);
2302 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(4));
2303 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2304
2305 PUSH_INSN1(ret, location, putobject, Qfalse);
2306 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2307
2308 PUSH_INSN(ret, location, pop);
2309 PUSH_INSN(ret, location, pop);
2310 PUSH_LABEL(ret, match_succeeded_label);
2311
2312 return COMPILE_OK;
2313}
2314
2320static int
2321pm_compile_pattern_eqq_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, unsigned int base_index)
2322{
2323 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2324 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2325
2326 PUSH_INSN(ret, location, dup);
2327 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2328 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2329
2330 VALUE operand = rb_fstring_lit("%p === %p does not return true");
2331 PUSH_INSN1(ret, location, putobject, operand);
2332
2333 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2334 PUSH_INSN1(ret, location, topn, INT2FIX(5));
2335 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2336 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2337 PUSH_INSN1(ret, location, putobject, Qfalse);
2338 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2339 PUSH_INSN(ret, location, pop);
2340 PUSH_INSN(ret, location, pop);
2341
2342 PUSH_LABEL(ret, match_succeeded_label);
2343 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2344 PUSH_INSN(ret, location, pop);
2345 PUSH_INSN(ret, location, pop);
2346
2347 return COMPILE_OK;
2348}
2349
2356static int
2357pm_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)
2358{
2359 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2360 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2361 PUSH_LABEL(ret, matched_label);
2362 return COMPILE_OK;
2363}
2364
2370static int
2371pm_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)
2372{
2373 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2374
2375 if (use_deconstructed_cache) {
2376 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2377 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2378
2379 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2380 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2381
2382 PUSH_INSN(ret, location, pop);
2383 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2384 PUSH_INSNL(ret, location, jump, deconstructed_label);
2385 }
2386 else {
2387 PUSH_INSNL(ret, location, jump, deconstruct_label);
2388 }
2389
2390 PUSH_LABEL(ret, deconstruct_label);
2391 PUSH_INSN(ret, location, dup);
2392
2393 VALUE operand = ID2SYM(rb_intern("deconstruct"));
2394 PUSH_INSN1(ret, location, putobject, operand);
2395 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2396
2397 if (use_deconstructed_cache) {
2398 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2399 }
2400
2401 if (in_single_pattern) {
2402 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1));
2403 }
2404
2405 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2406 PUSH_SEND(ret, location, rb_intern("deconstruct"), INT2FIX(0));
2407
2408 if (use_deconstructed_cache) {
2409 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2410 }
2411
2412 PUSH_INSN(ret, location, dup);
2413 PUSH_INSN1(ret, location, checktype, INT2FIX(T_ARRAY));
2414 PUSH_INSNL(ret, location, branchunless, type_error_label);
2415 PUSH_LABEL(ret, deconstructed_label);
2416
2417 return COMPILE_OK;
2418}
2419
2424static int
2425pm_compile_pattern_constant(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *match_failed_label, bool in_single_pattern, unsigned int base_index)
2426{
2427 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2428
2429 PUSH_INSN(ret, location, dup);
2430 PM_COMPILE_NOT_POPPED(node);
2431
2432 if (in_single_pattern) {
2433 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
2434 }
2435 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2436 if (in_single_pattern) {
2437 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2438 }
2439 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2440 return COMPILE_OK;
2441}
2442
2447static void
2448pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *done_label, bool popped)
2449{
2450 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2451 LABEL *key_error_label = NEW_LABEL(location.line);
2452 LABEL *cleanup_label = NEW_LABEL(location.line);
2453
2454 struct rb_callinfo_kwarg *kw_arg = rb_xmalloc_mul_add(2, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
2455 kw_arg->references = 0;
2456 kw_arg->keyword_len = 2;
2457 kw_arg->keywords[0] = ID2SYM(rb_intern("matchee"));
2458 kw_arg->keywords[1] = ID2SYM(rb_intern("key"));
2459
2460 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2461 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2462 PUSH_INSNL(ret, location, branchif, key_error_label);
2463
2464 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternError);
2465 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2466
2467 {
2468 VALUE operand = rb_fstring_lit("%p: %s");
2469 PUSH_INSN1(ret, location, putobject, operand);
2470 }
2471
2472 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2473 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2474 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2475 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2476 PUSH_INSNL(ret, location, jump, cleanup_label);
2477
2478 PUSH_LABEL(ret, key_error_label);
2479 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternKeyError);
2480 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2481
2482 {
2483 VALUE operand = rb_fstring_lit("%p: %s");
2484 PUSH_INSN1(ret, location, putobject, operand);
2485 }
2486
2487 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2488 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2489 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2490 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2491 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2492 PUSH_SEND_R(ret, location, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
2493 PUSH_SEND(ret, location, id_core_raise, INT2FIX(1));
2494 PUSH_LABEL(ret, cleanup_label);
2495
2496 PUSH_INSN1(ret, location, adjuststack, INT2FIX(7));
2497 if (!popped) PUSH_INSN(ret, location, putnil);
2498 PUSH_INSNL(ret, location, jump, done_label);
2499 PUSH_INSN1(ret, location, dupn, INT2FIX(5));
2500 if (popped) PUSH_INSN(ret, location, putnil);
2501}
2502
2506static int
2507pm_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)
2508{
2509 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2510
2511 switch (PM_NODE_TYPE(node)) {
2512 case PM_ARRAY_PATTERN_NODE: {
2513 // Array patterns in pattern matching are triggered by using commas in
2514 // a pattern or wrapping it in braces. They are represented by a
2515 // ArrayPatternNode. This looks like:
2516 //
2517 // foo => [1, 2, 3]
2518 //
2519 // It can optionally have a splat in the middle of it, which can
2520 // optionally have a name attached.
2521 const pm_array_pattern_node_t *cast = (const pm_array_pattern_node_t *) node;
2522
2523 const size_t requireds_size = cast->requireds.size;
2524 const size_t posts_size = cast->posts.size;
2525 const size_t minimum_size = requireds_size + posts_size;
2526
2527 bool rest_named = false;
2528 bool use_rest_size = false;
2529
2530 if (cast->rest != NULL) {
2531 rest_named = (PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) cast->rest)->expression != NULL);
2532 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2533 }
2534
2535 LABEL *match_failed_label = NEW_LABEL(location.line);
2536 LABEL *type_error_label = NEW_LABEL(location.line);
2537 LABEL *deconstruct_label = NEW_LABEL(location.line);
2538 LABEL *deconstructed_label = NEW_LABEL(location.line);
2539
2540 if (use_rest_size) {
2541 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2542 PUSH_INSN(ret, location, swap);
2543 base_index++;
2544 }
2545
2546 if (cast->constant != NULL) {
2547 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2548 }
2549
2550 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));
2551
2552 PUSH_INSN(ret, location, dup);
2553 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2554 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2555 PUSH_SEND(ret, location, cast->rest == NULL ? idEq : idGE, INT2FIX(1));
2556 if (in_single_pattern) {
2557 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+)");
2558 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message, INT2FIX(minimum_size), base_index + 1));
2559 }
2560 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2561
2562 for (size_t index = 0; index < requireds_size; index++) {
2563 const pm_node_t *required = cast->requireds.nodes[index];
2564 PUSH_INSN(ret, location, dup);
2565 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2566 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2567 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2568 }
2569
2570 if (cast->rest != NULL) {
2571 if (rest_named) {
2572 PUSH_INSN(ret, location, dup);
2573 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size));
2574 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2575 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2576 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2577 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2578 PUSH_INSN1(ret, location, setn, INT2FIX(4));
2579 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2580 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));
2581 }
2582 else if (posts_size > 0) {
2583 PUSH_INSN(ret, location, dup);
2584 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2585 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2586 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2587 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2588 PUSH_INSN(ret, location, pop);
2589 }
2590 }
2591
2592 for (size_t index = 0; index < posts_size; index++) {
2593 const pm_node_t *post = cast->posts.nodes[index];
2594 PUSH_INSN(ret, location, dup);
2595
2596 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size + index));
2597 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2598 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2599 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2600 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2601 }
2602
2603 PUSH_INSN(ret, location, pop);
2604 if (use_rest_size) {
2605 PUSH_INSN(ret, location, pop);
2606 }
2607
2608 PUSH_INSNL(ret, location, jump, matched_label);
2609 PUSH_INSN(ret, location, putnil);
2610 if (use_rest_size) {
2611 PUSH_INSN(ret, location, putnil);
2612 }
2613
2614 PUSH_LABEL(ret, type_error_label);
2615 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2616 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2617
2618 {
2619 VALUE operand = rb_fstring_lit("deconstruct must return Array");
2620 PUSH_INSN1(ret, location, putobject, operand);
2621 }
2622
2623 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2624 PUSH_INSN(ret, location, pop);
2625
2626 PUSH_LABEL(ret, match_failed_label);
2627 PUSH_INSN(ret, location, pop);
2628 if (use_rest_size) {
2629 PUSH_INSN(ret, location, pop);
2630 }
2631
2632 PUSH_INSNL(ret, location, jump, unmatched_label);
2633 break;
2634 }
2635 case PM_FIND_PATTERN_NODE: {
2636 // Find patterns in pattern matching are triggered by using commas in
2637 // a pattern or wrapping it in braces and using a splat on both the left
2638 // and right side of the pattern. This looks like:
2639 //
2640 // foo => [*, 1, 2, 3, *]
2641 //
2642 // There can be any number of requireds in the middle. The splats on
2643 // both sides can optionally have names attached.
2644 const pm_find_pattern_node_t *cast = (const pm_find_pattern_node_t *) node;
2645 const size_t size = cast->requireds.size;
2646
2647 LABEL *match_failed_label = NEW_LABEL(location.line);
2648 LABEL *type_error_label = NEW_LABEL(location.line);
2649 LABEL *deconstruct_label = NEW_LABEL(location.line);
2650 LABEL *deconstructed_label = NEW_LABEL(location.line);
2651
2652 if (cast->constant) {
2653 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2654 }
2655
2656 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));
2657
2658 PUSH_INSN(ret, location, dup);
2659 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2660 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2661 PUSH_SEND(ret, location, idGE, INT2FIX(1));
2662 if (in_single_pattern) {
2663 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));
2664 }
2665 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2666
2667 {
2668 LABEL *while_begin_label = NEW_LABEL(location.line);
2669 LABEL *next_loop_label = NEW_LABEL(location.line);
2670 LABEL *find_succeeded_label = NEW_LABEL(location.line);
2671 LABEL *find_failed_label = NEW_LABEL(location.line);
2672
2673 PUSH_INSN(ret, location, dup);
2674 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2675
2676 PUSH_INSN(ret, location, dup);
2677 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2678 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2679 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2680 PUSH_LABEL(ret, while_begin_label);
2681
2682 PUSH_INSN(ret, location, dup);
2683 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2684 PUSH_SEND(ret, location, idLE, INT2FIX(1));
2685 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2686
2687 for (size_t index = 0; index < size; index++) {
2688 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2689 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2690
2691 if (index != 0) {
2692 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2693 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2694 }
2695
2696 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2697 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));
2698 }
2699
2700 const pm_splat_node_t *left = cast->left;
2701
2702 if (left->expression != NULL) {
2703 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2704 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2705 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2706 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2707 CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
2708 }
2709
2711 const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
2712
2713 if (right->expression != NULL) {
2714 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2715 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2716 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2717 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2718 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2719 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2720 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
2721 }
2722
2723 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2724
2725 PUSH_LABEL(ret, next_loop_label);
2726 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
2727 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2728 PUSH_INSNL(ret, location, jump, while_begin_label);
2729
2730 PUSH_LABEL(ret, find_failed_label);
2731 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2732 if (in_single_pattern) {
2733 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2734
2735 {
2736 VALUE operand = rb_fstring_lit("%p does not match to find pattern");
2737 PUSH_INSN1(ret, location, putobject, operand);
2738 }
2739
2740 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2741 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
2742 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2743
2744 PUSH_INSN1(ret, location, putobject, Qfalse);
2745 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2746
2747 PUSH_INSN(ret, location, pop);
2748 PUSH_INSN(ret, location, pop);
2749 }
2750 PUSH_INSNL(ret, location, jump, match_failed_label);
2751 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2752
2753 PUSH_LABEL(ret, find_succeeded_label);
2754 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2755 }
2756
2757 PUSH_INSN(ret, location, pop);
2758 PUSH_INSNL(ret, location, jump, matched_label);
2759 PUSH_INSN(ret, location, putnil);
2760
2761 PUSH_LABEL(ret, type_error_label);
2762 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2763 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2764
2765 {
2766 VALUE operand = rb_fstring_lit("deconstruct must return Array");
2767 PUSH_INSN1(ret, location, putobject, operand);
2768 }
2769
2770 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2771 PUSH_INSN(ret, location, pop);
2772
2773 PUSH_LABEL(ret, match_failed_label);
2774 PUSH_INSN(ret, location, pop);
2775 PUSH_INSNL(ret, location, jump, unmatched_label);
2776
2777 break;
2778 }
2779 case PM_HASH_PATTERN_NODE: {
2780 // Hash patterns in pattern matching are triggered by using labels and
2781 // values in a pattern or by using the ** operator. They are represented
2782 // by the HashPatternNode. This looks like:
2783 //
2784 // foo => { a: 1, b: 2, **bar }
2785 //
2786 // It can optionally have an assoc splat in the middle of it, which can
2787 // optionally have a name.
2788 const pm_hash_pattern_node_t *cast = (const pm_hash_pattern_node_t *) node;
2789
2790 // We don't consider it a "rest" parameter if it's a ** that is unnamed.
2791 bool has_rest = cast->rest != NULL && !(PM_NODE_TYPE_P(cast->rest, PM_ASSOC_SPLAT_NODE) && ((const pm_assoc_splat_node_t *) cast->rest)->value == NULL);
2792 bool has_keys = cast->elements.size > 0 || cast->rest != NULL;
2793
2794 LABEL *match_failed_label = NEW_LABEL(location.line);
2795 LABEL *type_error_label = NEW_LABEL(location.line);
2796 VALUE keys = Qnil;
2797
2798 if (has_keys && !has_rest) {
2799 keys = rb_ary_new_capa(cast->elements.size);
2800
2801 for (size_t index = 0; index < cast->elements.size; index++) {
2802 const pm_node_t *element = cast->elements.nodes[index];
2804
2805 const pm_node_t *key = ((const pm_assoc_node_t *) element)->key;
2807
2808 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2809 rb_ary_push(keys, symbol);
2810 }
2811 }
2812
2813 if (cast->constant) {
2814 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2815 }
2816
2817 PUSH_INSN(ret, location, dup);
2818
2819 {
2820 VALUE operand = ID2SYM(rb_intern("deconstruct_keys"));
2821 PUSH_INSN1(ret, location, putobject, operand);
2822 }
2823
2824 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2825 if (in_single_pattern) {
2826 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
2827 }
2828 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2829
2830 if (NIL_P(keys)) {
2831 PUSH_INSN(ret, location, putnil);
2832 }
2833 else {
2834 PUSH_INSN1(ret, location, duparray, keys);
2835 RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
2836 }
2837 PUSH_SEND(ret, location, rb_intern("deconstruct_keys"), INT2FIX(1));
2838
2839 PUSH_INSN(ret, location, dup);
2840 PUSH_INSN1(ret, location, checktype, INT2FIX(T_HASH));
2841 PUSH_INSNL(ret, location, branchunless, type_error_label);
2842
2843 if (has_rest) {
2844 PUSH_SEND(ret, location, rb_intern("dup"), INT2FIX(0));
2845 }
2846
2847 if (has_keys) {
2848 DECL_ANCHOR(match_values);
2849
2850 for (size_t index = 0; index < cast->elements.size; index++) {
2851 const pm_node_t *element = cast->elements.nodes[index];
2853
2854 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
2855 const pm_node_t *key = assoc->key;
2857
2858 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2859 PUSH_INSN(ret, location, dup);
2860 PUSH_INSN1(ret, location, putobject, symbol);
2861 PUSH_SEND(ret, location, rb_intern("key?"), INT2FIX(1));
2862
2863 if (in_single_pattern) {
2864 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2865
2866 PUSH_INSN(ret, location, dup);
2867 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2868
2869 {
2870 VALUE operand = rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol));
2871 PUSH_INSN1(ret, location, putobject, operand);
2872 }
2873
2874 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2875 PUSH_INSN1(ret, location, putobject, Qtrue);
2876 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2877 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2878 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2879 PUSH_INSN1(ret, location, putobject, symbol);
2880 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2881
2882 PUSH_INSN1(ret, location, adjuststack, INT2FIX(4));
2883 PUSH_LABEL(ret, match_succeeded_label);
2884 }
2885
2886 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2887 PUSH_INSN(match_values, location, dup);
2888 PUSH_INSN1(match_values, location, putobject, symbol);
2889 PUSH_SEND(match_values, location, has_rest ? rb_intern("delete") : idAREF, INT2FIX(1));
2890
2891 const pm_node_t *value = assoc->value;
2892 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2893 value = ((const pm_implicit_node_t *) value)->value;
2894 }
2895
2896 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2897 }
2898
2899 PUSH_SEQ(ret, match_values);
2900 }
2901 else {
2902 PUSH_INSN(ret, location, dup);
2903 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2904 if (in_single_pattern) {
2905 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p is not empty"), base_index + 1));
2906 }
2907 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2908 }
2909
2910 if (has_rest) {
2911 switch (PM_NODE_TYPE(cast->rest)) {
2913 PUSH_INSN(ret, location, dup);
2914 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2915 if (in_single_pattern) {
2916 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("rest of %p is not empty"), base_index + 1);
2917 }
2918 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2919 break;
2920 }
2921 case PM_ASSOC_SPLAT_NODE: {
2922 const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
2923 PUSH_INSN(ret, location, dup);
2924 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
2925 break;
2926 }
2927 default:
2928 rb_bug("unreachable");
2929 break;
2930 }
2931 }
2932
2933 PUSH_INSN(ret, location, pop);
2934 PUSH_INSNL(ret, location, jump, matched_label);
2935 PUSH_INSN(ret, location, putnil);
2936
2937 PUSH_LABEL(ret, type_error_label);
2938 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2939 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2940
2941 {
2942 VALUE operand = rb_fstring_lit("deconstruct_keys must return Hash");
2943 PUSH_INSN1(ret, location, putobject, operand);
2944 }
2945
2946 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2947 PUSH_INSN(ret, location, pop);
2948
2949 PUSH_LABEL(ret, match_failed_label);
2950 PUSH_INSN(ret, location, pop);
2951 PUSH_INSNL(ret, location, jump, unmatched_label);
2952 break;
2953 }
2955 // Capture patterns allow you to pattern match against an element in a
2956 // pattern and also capture the value into a local variable. This looks
2957 // like:
2958 //
2959 // [1] => [Integer => foo]
2960 //
2961 // In this case the `Integer => foo` will be represented by a
2962 // CapturePatternNode, which has both a value (the pattern to match
2963 // against) and a target (the place to write the variable into).
2964 const pm_capture_pattern_node_t *cast = (const pm_capture_pattern_node_t *) node;
2965
2966 LABEL *match_failed_label = NEW_LABEL(location.line);
2967
2968 PUSH_INSN(ret, location, dup);
2969 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));
2970 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));
2971 PUSH_INSN(ret, location, putnil);
2972
2973 PUSH_LABEL(ret, match_failed_label);
2974 PUSH_INSN(ret, location, pop);
2975 PUSH_INSNL(ret, location, jump, unmatched_label);
2976
2977 break;
2978 }
2980 // Local variables can be targeted by placing identifiers in the place
2981 // of a pattern. For example, foo in bar. This results in the value
2982 // being matched being written to that local variable.
2984 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
2985
2986 // If this local variable is being written from within an alternation
2987 // pattern, then it cannot actually be added to the local table since
2988 // it's ambiguous which value should be used. So instead we indicate
2989 // this with a compile error.
2990 if (in_alternation_pattern) {
2991 ID id = pm_constant_id_lookup(scope_node, cast->name);
2992 const char *name = rb_id2name(id);
2993
2994 if (name && strlen(name) > 0 && name[0] != '_') {
2995 COMPILE_ERROR(iseq, location.line, "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
2996 return COMPILE_NG;
2997 }
2998 }
2999
3000 PUSH_SETLOCAL(ret, location, index.index, index.level);
3001 PUSH_INSNL(ret, location, jump, matched_label);
3002 break;
3003 }
3005 // Alternation patterns allow you to specify multiple patterns in a
3006 // single expression using the | operator.
3008
3009 LABEL *matched_left_label = NEW_LABEL(location.line);
3010 LABEL *unmatched_left_label = NEW_LABEL(location.line);
3011
3012 // First, we're going to attempt to match against the left pattern. If
3013 // that pattern matches, then we'll skip matching the right pattern.
3014 PUSH_INSN(ret, location, dup);
3015 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));
3016
3017 // If we get here, then we matched on the left pattern. In this case we
3018 // should pop out the duplicate value that we preemptively added to
3019 // match against the right pattern and then jump to the match label.
3020 PUSH_LABEL(ret, matched_left_label);
3021 PUSH_INSN(ret, location, pop);
3022 PUSH_INSNL(ret, location, jump, matched_label);
3023 PUSH_INSN(ret, location, putnil);
3024
3025 // If we get here, then we didn't match on the left pattern. In this
3026 // case we attempt to match against the right pattern.
3027 PUSH_LABEL(ret, unmatched_left_label);
3028 CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, use_deconstructed_cache, base_index));
3029 break;
3030 }
3032 // Parentheses are allowed to wrap expressions in pattern matching and
3033 // they do nothing since they can only wrap individual expressions and
3034 // not groups. In this case we'll recurse back into this same function
3035 // with the body of the parentheses.
3036 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);
3038 // Pinned expressions are a way to match against the value of an
3039 // expression that should be evaluated at runtime. This looks like:
3040 // foo in ^(bar). To compile these, we compile the expression as if it
3041 // were a literal value by falling through to the literal case.
3042 node = ((const pm_pinned_expression_node_t *) node)->expression;
3043 /* fallthrough */
3044 case PM_ARRAY_NODE:
3048 case PM_FALSE_NODE:
3049 case PM_FLOAT_NODE:
3051 case PM_IMAGINARY_NODE:
3054 case PM_INTEGER_NODE:
3059 case PM_LAMBDA_NODE:
3061 case PM_NIL_NODE:
3065 case PM_RANGE_NODE:
3066 case PM_RATIONAL_NODE:
3068 case PM_SELF_NODE:
3069 case PM_STRING_NODE:
3070 case PM_SYMBOL_NODE:
3071 case PM_TRUE_NODE:
3072 case PM_X_STRING_NODE: {
3073 // These nodes are all simple patterns, which means we'll use the
3074 // checkmatch instruction to match against them, which is effectively a
3075 // VM-level === operator.
3076 PM_COMPILE_NOT_POPPED(node);
3077 if (in_single_pattern) {
3078 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
3079 }
3080
3081 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3082
3083 if (in_single_pattern) {
3084 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3085 }
3086
3087 PUSH_INSNL(ret, location, branchif, matched_label);
3088 PUSH_INSNL(ret, location, jump, unmatched_label);
3089 break;
3090 }
3092 // Pinned variables are a way to match against the value of a variable
3093 // without it looking like you're trying to write to the variable. This
3094 // looks like: foo in ^@bar. To compile these, we compile the variable
3095 // that they hold.
3096 const pm_pinned_variable_node_t *cast = (const pm_pinned_variable_node_t *) node;
3097 CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
3098 break;
3099 }
3100 case PM_IF_NODE:
3101 case PM_UNLESS_NODE: {
3102 // If and unless nodes can show up here as guards on `in` clauses. This
3103 // looks like:
3104 //
3105 // case foo
3106 // in bar if baz?
3107 // qux
3108 // end
3109 //
3110 // Because we know they're in the modifier form and they can't have any
3111 // variation on this pattern, we compile them differently (more simply)
3112 // here than we would in the normal compilation path.
3113 const pm_node_t *predicate;
3114 const pm_node_t *statement;
3115
3116 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3117 const pm_if_node_t *cast = (const pm_if_node_t *) node;
3118 predicate = cast->predicate;
3119
3120 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
3121 statement = cast->statements->body.nodes[0];
3122 }
3123 else {
3124 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
3125 predicate = cast->predicate;
3126
3127 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
3128 statement = cast->statements->body.nodes[0];
3129 }
3130
3131 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
3132 PM_COMPILE_NOT_POPPED(predicate);
3133
3134 if (in_single_pattern) {
3135 LABEL *match_succeeded_label = NEW_LABEL(location.line);
3136
3137 PUSH_INSN(ret, location, dup);
3138 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3139 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3140 }
3141 else {
3142 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3143 }
3144
3145 {
3146 VALUE operand = rb_fstring_lit("guard clause does not return true");
3147 PUSH_INSN1(ret, location, putobject, operand);
3148 }
3149
3150 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3151 PUSH_INSN1(ret, location, putobject, Qfalse);
3152 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3153
3154 PUSH_INSN(ret, location, pop);
3155 PUSH_INSN(ret, location, pop);
3156
3157 PUSH_LABEL(ret, match_succeeded_label);
3158 }
3159
3160 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3161 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3162 }
3163 else {
3164 PUSH_INSNL(ret, location, branchif, unmatched_label);
3165 }
3166
3167 PUSH_INSNL(ret, location, jump, matched_label);
3168 break;
3169 }
3170 default:
3171 // If we get here, then we have a node type that should not be in this
3172 // position. This would be a bug in the parser, because a different node
3173 // type should never have been created in this position in the tree.
3174 rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3175 break;
3176 }
3177
3178 return COMPILE_OK;
3179}
3180
3181#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3182#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3183#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3184#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3185#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3186
3187// Generate a scope node from the given node.
3188void
3189pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
3190{
3191 // This is very important, otherwise the scope node could be seen as having
3192 // certain flags set that _should not_ be set.
3193 memset(scope, 0, sizeof(pm_scope_node_t));
3194
3195 scope->base.type = PM_SCOPE_NODE;
3196 scope->base.location.start = node->location.start;
3197 scope->base.location.end = node->location.end;
3198
3199 scope->previous = previous;
3200 scope->ast_node = (pm_node_t *) node;
3201
3202 if (previous) {
3203 scope->parser = previous->parser;
3204 scope->encoding = previous->encoding;
3205 scope->filepath_encoding = previous->filepath_encoding;
3206 scope->constants = previous->constants;
3207 scope->coverage_enabled = previous->coverage_enabled;
3208 scope->script_lines = previous->script_lines;
3209 }
3210
3211 switch (PM_NODE_TYPE(node)) {
3212 case PM_BLOCK_NODE: {
3213 const pm_block_node_t *cast = (const pm_block_node_t *) node;
3214 scope->body = cast->body;
3215 scope->locals = cast->locals;
3216 scope->parameters = cast->parameters;
3217 break;
3218 }
3219 case PM_CLASS_NODE: {
3220 const pm_class_node_t *cast = (const pm_class_node_t *) node;
3221 scope->body = cast->body;
3222 scope->locals = cast->locals;
3223 break;
3224 }
3225 case PM_DEF_NODE: {
3226 const pm_def_node_t *cast = (const pm_def_node_t *) node;
3227 scope->parameters = (pm_node_t *) cast->parameters;
3228 scope->body = cast->body;
3229 scope->locals = cast->locals;
3230 break;
3231 }
3232 case PM_ENSURE_NODE: {
3233 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
3234 scope->body = (pm_node_t *) node;
3235
3236 if (cast->statements != NULL) {
3237 scope->base.location.start = cast->statements->base.location.start;
3238 scope->base.location.end = cast->statements->base.location.end;
3239 }
3240
3241 break;
3242 }
3243 case PM_FOR_NODE: {
3244 const pm_for_node_t *cast = (const pm_for_node_t *) node;
3245 scope->body = (pm_node_t *) cast->statements;
3246 break;
3247 }
3250 scope->body = (pm_node_t *) node;
3251 break;
3252 }
3253 case PM_LAMBDA_NODE: {
3254 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
3255 scope->parameters = cast->parameters;
3256 scope->body = cast->body;
3257 scope->locals = cast->locals;
3258
3259 if (cast->parameters != NULL) {
3260 scope->base.location.start = cast->parameters->location.start;
3261 }
3262 else {
3263 scope->base.location.start = cast->operator_loc.end;
3264 }
3265 break;
3266 }
3267 case PM_MODULE_NODE: {
3268 const pm_module_node_t *cast = (const pm_module_node_t *) node;
3269 scope->body = cast->body;
3270 scope->locals = cast->locals;
3271 break;
3272 }
3274 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) node;
3275 scope->body = (pm_node_t *) cast->statements;
3276 break;
3277 }
3278 case PM_PROGRAM_NODE: {
3279 const pm_program_node_t *cast = (const pm_program_node_t *) node;
3280 scope->body = (pm_node_t *) cast->statements;
3281 scope->locals = cast->locals;
3282 break;
3283 }
3284 case PM_RESCUE_NODE: {
3285 const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
3286 scope->body = (pm_node_t *) cast->statements;
3287 break;
3288 }
3290 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
3291 scope->body = (pm_node_t *) cast->rescue_expression;
3292 break;
3293 }
3295 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
3296 scope->body = cast->body;
3297 scope->locals = cast->locals;
3298 break;
3299 }
3300 case PM_STATEMENTS_NODE: {
3301 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
3302 scope->body = (pm_node_t *) cast;
3303 break;
3304 }
3305 default:
3306 rb_bug("unreachable");
3307 break;
3308 }
3309}
3310
3311void
3312pm_scope_node_destroy(pm_scope_node_t *scope_node)
3313{
3314 if (scope_node->index_lookup_table) {
3315 st_free_table(scope_node->index_lookup_table);
3316 }
3317}
3318
3330static void
3331pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *retry_end_l)
3332{
3333 INSN *iobj;
3334 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
3335 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
3336 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3337 iobj = (INSN*) get_prev_insn(iobj);
3338 }
3339 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
3340
3341 // LINK_ANCHOR has a pointer to the last element, but
3342 // ELEM_INSERT_NEXT does not update it even if we add an insn to the
3343 // last of LINK_ANCHOR. So this updates it manually.
3344 if (&iobj->link == LAST_ELEMENT(ret)) {
3345 ret->last = (LINK_ELEMENT*) retry_end_l;
3346 }
3347}
3348
3349static const char *
3350pm_iseq_builtin_function_name(const pm_scope_node_t *scope_node, const pm_node_t *receiver, ID method_id)
3351{
3352 const char *name = rb_id2name(method_id);
3353 static const char prefix[] = "__builtin_";
3354 const size_t prefix_len = sizeof(prefix) - 1;
3355
3356 if (receiver == NULL) {
3357 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3358 // __builtin_foo
3359 return &name[prefix_len];
3360 }
3361 }
3362 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3364 const pm_call_node_t *cast = (const pm_call_node_t *) receiver;
3365 if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("__builtin")) {
3366 // __builtin.foo
3367 return name;
3368 }
3369 }
3370 }
3371 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3372 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) receiver;
3373 if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("Primitive")) {
3374 // Primitive.foo
3375 return name;
3376 }
3377 }
3378
3379 return NULL;
3380}
3381
3382// Compile Primitive.attr! :leaf, ...
3383static int
3384pm_compile_builtin_attr(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location)
3385{
3386 if (arguments == NULL) {
3387 COMPILE_ERROR(iseq, node_location->line, "attr!: no argument");
3388 return COMPILE_NG;
3389 }
3390
3391 const pm_node_t *argument;
3392 PM_NODE_LIST_FOREACH(&arguments->arguments, index, argument) {
3393 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3394 COMPILE_ERROR(iseq, node_location->line, "non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3395 return COMPILE_NG;
3396 }
3397
3398 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3399 VALUE string = rb_sym2str(symbol);
3400
3401 if (strcmp(RSTRING_PTR(string), "leaf") == 0) {
3402 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3403 }
3404 else if (strcmp(RSTRING_PTR(string), "inline_block") == 0) {
3405 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3406 }
3407 else if (strcmp(RSTRING_PTR(string), "use_block") == 0) {
3408 iseq_set_use_block(iseq);
3409 }
3410 else if (strcmp(RSTRING_PTR(string), "c_trace") == 0) {
3411 // Let the iseq act like a C method in backtraces
3412 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3413 }
3414 else {
3415 COMPILE_ERROR(iseq, node_location->line, "unknown argument to attr!: %s", RSTRING_PTR(string));
3416 return COMPILE_NG;
3417 }
3418 }
3419
3420 return COMPILE_OK;
3421}
3422
3423static int
3424pm_compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location, int popped)
3425{
3426 if (arguments == NULL) {
3427 COMPILE_ERROR(iseq, node_location->line, "arg!: no argument");
3428 return COMPILE_NG;
3429 }
3430
3431 if (arguments->arguments.size != 1) {
3432 COMPILE_ERROR(iseq, node_location->line, "arg!: too many argument");
3433 return COMPILE_NG;
3434 }
3435
3436 const pm_node_t *argument = arguments->arguments.nodes[0];
3437 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3438 COMPILE_ERROR(iseq, node_location->line, "non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3439 return COMPILE_NG;
3440 }
3441
3442 if (!popped) {
3443 ID name = parse_string_symbol(scope_node, ((const pm_symbol_node_t *) argument));
3444 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3445
3446 debugs("id: %s idx: %d\n", rb_id2name(name), index);
3447 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3448 }
3449
3450 return COMPILE_OK;
3451}
3452
3453static int
3454pm_compile_builtin_mandatory_only_method(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_call_node_t *call_node, const pm_node_location_t *node_location)
3455{
3456 const pm_node_t *ast_node = scope_node->ast_node;
3457 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3458 rb_bug("mandatory_only?: not in method definition");
3459 return COMPILE_NG;
3460 }
3461
3462 const pm_def_node_t *def_node = (const pm_def_node_t *) ast_node;
3463 const pm_parameters_node_t *parameters_node = def_node->parameters;
3464 if (parameters_node == NULL) {
3465 rb_bug("mandatory_only?: in method definition with no parameters");
3466 return COMPILE_NG;
3467 }
3468
3469 const pm_node_t *body_node = def_node->body;
3470 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)) {
3471 rb_bug("mandatory_only?: not in method definition with plain statements");
3472 return COMPILE_NG;
3473 }
3474
3475 const pm_if_node_t *if_node = (const pm_if_node_t *) ((const pm_statements_node_t *) body_node)->body.nodes[0];
3476 if (if_node->predicate != ((const pm_node_t *) call_node)) {
3477 rb_bug("mandatory_only?: can't find mandatory node");
3478 return COMPILE_NG;
3479 }
3480
3481 pm_parameters_node_t parameters = {
3482 .base = parameters_node->base,
3483 .requireds = parameters_node->requireds
3484 };
3485
3486 const pm_def_node_t def = {
3487 .base = def_node->base,
3488 .name = def_node->name,
3489 .receiver = def_node->receiver,
3490 .parameters = &parameters,
3491 .body = (pm_node_t *) if_node->statements,
3492 .locals = {
3493 .ids = def_node->locals.ids,
3494 .size = parameters_node->requireds.size,
3495 .capacity = def_node->locals.capacity
3496 }
3497 };
3498
3499 pm_scope_node_t next_scope_node;
3500 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3501
3502 int error_state;
3503 ISEQ_BODY(iseq)->mandatory_only_iseq = pm_iseq_new_with_opt(
3504 &next_scope_node,
3505 rb_iseq_base_label(iseq),
3506 rb_iseq_path(iseq),
3507 rb_iseq_realpath(iseq),
3508 node_location->line,
3509 NULL,
3510 0,
3511 ISEQ_TYPE_METHOD,
3512 ISEQ_COMPILE_DATA(iseq)->option,
3513 &error_state
3514 );
3515
3516 if (error_state) {
3517 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3518 rb_jump_tag(error_state);
3519 }
3520
3521 pm_scope_node_destroy(&next_scope_node);
3522 return COMPILE_OK;
3523}
3524
3525static int
3526pm_compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_call_node_t *call_node, const pm_node_location_t *node_location, int popped, const rb_iseq_t *parent_block, const char *builtin_func)
3527{
3528 const pm_arguments_node_t *arguments = call_node->arguments;
3529
3530 if (parent_block != NULL) {
3531 COMPILE_ERROR(iseq, node_location->line, "should not call builtins here.");
3532 return COMPILE_NG;
3533 }
3534
3535#define BUILTIN_INLINE_PREFIX "_bi"
3536 char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)];
3537 bool cconst = false;
3538retry:;
3539 const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func);
3540
3541 if (bf == NULL) {
3542 if (strcmp("cstmt!", builtin_func) == 0 || strcmp("cexpr!", builtin_func) == 0) {
3543 // ok
3544 }
3545 else if (strcmp("cconst!", builtin_func) == 0) {
3546 cconst = true;
3547 }
3548 else if (strcmp("cinit!", builtin_func) == 0) {
3549 // ignore
3550 return COMPILE_OK;
3551 }
3552 else if (strcmp("attr!", builtin_func) == 0) {
3553 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3554 }
3555 else if (strcmp("arg!", builtin_func) == 0) {
3556 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3557 }
3558 else if (strcmp("mandatory_only?", builtin_func) == 0) {
3559 if (popped) {
3560 rb_bug("mandatory_only? should be in if condition");
3561 }
3562 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3563 rb_bug("mandatory_only? should be put on top");
3564 }
3565
3566 PUSH_INSN1(ret, *node_location, putobject, Qfalse);
3567 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3568 }
3569 else if (1) {
3570 rb_bug("can't find builtin function:%s", builtin_func);
3571 }
3572 else {
3573 COMPILE_ERROR(iseq, node_location->line, "can't find builtin function:%s", builtin_func);
3574 return COMPILE_NG;
3575 }
3576
3577 int inline_index = node_location->line;
3578 snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
3579 builtin_func = inline_func;
3580 arguments = NULL;
3581 goto retry;
3582 }
3583
3584 if (cconst) {
3585 typedef VALUE(*builtin_func0)(void *, VALUE);
3586 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL, Qnil);
3587 PUSH_INSN1(ret, *node_location, putobject, const_val);
3588 return COMPILE_OK;
3589 }
3590
3591 // fprintf(stderr, "func_name:%s -> %p\n", builtin_func, bf->func_ptr);
3592
3593 DECL_ANCHOR(args_seq);
3594
3595 int flags = 0;
3596 struct rb_callinfo_kwarg *keywords = NULL;
3597 int argc = pm_setup_args(arguments, call_node->block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3598
3599 if (argc != bf->argc) {
3600 COMPILE_ERROR(iseq, node_location->line, "argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3601 return COMPILE_NG;
3602 }
3603
3604 unsigned int start_index;
3605 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3606 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf, INT2FIX(start_index));
3607 }
3608 else {
3609 PUSH_SEQ(ret, args_seq);
3610 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3611 }
3612
3613 if (popped) PUSH_INSN(ret, *node_location, pop);
3614 return COMPILE_OK;
3615}
3616
3620static void
3621pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, ID method_id, LABEL *start)
3622{
3623 const pm_location_t *message_loc = &call_node->message_loc;
3624 if (message_loc->start == NULL) message_loc = &call_node->base.location;
3625
3626 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, call_node->base.node_id);
3627 LABEL *else_label = NEW_LABEL(location.line);
3628 LABEL *end_label = NEW_LABEL(location.line);
3629 LABEL *retry_end_l = NEW_LABEL(location.line);
3630
3631 VALUE branches = Qfalse;
3632 rb_code_location_t code_location = { 0 };
3633 int node_id = location.node_id;
3634
3636 if (PM_BRANCH_COVERAGE_P(iseq)) {
3637 const uint8_t *cursors[3] = {
3638 call_node->closing_loc.end,
3639 call_node->arguments == NULL ? NULL : call_node->arguments->base.location.end,
3640 call_node->message_loc.end
3641 };
3642
3643 const uint8_t *end_cursor = cursors[0];
3644 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3645 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3646 if (!end_cursor) end_cursor = call_node->closing_loc.end;
3647
3648 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3649 const pm_line_column_t end_location = pm_newline_list_line_column(&scope_node->parser->newline_list, end_cursor, scope_node->parser->start_line);
3650
3651 code_location = (rb_code_location_t) {
3652 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
3653 .end_pos = { .lineno = end_location.line, .column = end_location.column }
3654 };
3655
3656 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location, "&.");
3657 }
3658
3659 PUSH_INSN(ret, location, dup);
3660 PUSH_INSNL(ret, location, branchnil, else_label);
3661
3662 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0, "then", branches);
3663 }
3664
3665 int flags = 0;
3666 struct rb_callinfo_kwarg *kw_arg = NULL;
3667
3668 int orig_argc = pm_setup_args(call_node->arguments, call_node->block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3669 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3670 const rb_iseq_t *block_iseq = NULL;
3671
3672 if (call_node->block != NULL && PM_NODE_TYPE_P(call_node->block, PM_BLOCK_NODE)) {
3673 // Scope associated with the block
3674 pm_scope_node_t next_scope_node;
3675 pm_scope_node_init(call_node->block, &next_scope_node, scope_node);
3676
3677 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));
3678 pm_scope_node_destroy(&next_scope_node);
3679 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3680 }
3681 else {
3683 flags |= VM_CALL_VCALL;
3684 }
3685
3686 if (!flags) {
3687 flags |= VM_CALL_ARGS_SIMPLE;
3688 }
3689 }
3690
3692 flags |= VM_CALL_FCALL;
3693 }
3694
3695 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3696 if (flags & VM_CALL_ARGS_BLOCKARG) {
3697 PUSH_INSN1(ret, location, topn, INT2FIX(1));
3698 if (flags & VM_CALL_ARGS_SPLAT) {
3699 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3700 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3701 }
3702 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 3));
3703 PUSH_INSN(ret, location, pop);
3704 }
3705 else if (flags & VM_CALL_ARGS_SPLAT) {
3706 PUSH_INSN(ret, location, dup);
3707 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3708 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3709 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 2));
3710 PUSH_INSN(ret, location, pop);
3711 }
3712 else {
3713 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 1));
3714 }
3715 }
3716
3717 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3718 PUSH_INSN(ret, location, splatkw);
3719 }
3720
3721 PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
3722
3723 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3724 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3725 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3726 }
3727
3729 PUSH_INSNL(ret, location, jump, end_label);
3730 PUSH_LABEL(ret, else_label);
3731 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1, "else", branches);
3732 PUSH_LABEL(ret, end_label);
3733 }
3734
3735 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3736 PUSH_INSN(ret, location, pop);
3737 }
3738
3739 if (popped) PUSH_INSN(ret, location, pop);
3740 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3741}
3742
3743static void
3744pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
3745{
3746 // in_condition is the same as compile.c's needstr
3747 enum defined_type dtype = DEFINED_NOT_DEFINED;
3748 const pm_node_location_t location = *node_location;
3749
3750 switch (PM_NODE_TYPE(node)) {
3751 case PM_ARGUMENTS_NODE: {
3752 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
3753 const pm_node_list_t *arguments = &cast->arguments;
3754 for (size_t idx = 0; idx < arguments->size; idx++) {
3755 const pm_node_t *argument = arguments->nodes[idx];
3756 pm_compile_defined_expr0(iseq, argument, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
3757
3758 if (!lfinish[1]) {
3759 lfinish[1] = NEW_LABEL(location.line);
3760 }
3761 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3762 }
3763 dtype = DEFINED_TRUE;
3764 break;
3765 }
3766 case PM_NIL_NODE:
3767 dtype = DEFINED_NIL;
3768 break;
3769 case PM_PARENTHESES_NODE: {
3770 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
3771
3772 if (cast->body == NULL) {
3773 // If we have empty parentheses, then we want to return "nil".
3774 dtype = DEFINED_NIL;
3775 }
3776 else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
3777 // If we have a parentheses node that is wrapping a single statement
3778 // then we want to recurse down to that statement and compile it.
3779 pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
3780 return;
3781 }
3782 else {
3783 // Otherwise, we have parentheses wrapping multiple statements, in
3784 // which case this is defined as "expression".
3785 dtype = DEFINED_EXPR;
3786 }
3787
3788 break;
3789 }
3790 case PM_SELF_NODE:
3791 dtype = DEFINED_SELF;
3792 break;
3793 case PM_TRUE_NODE:
3794 dtype = DEFINED_TRUE;
3795 break;
3796 case PM_FALSE_NODE:
3797 dtype = DEFINED_FALSE;
3798 break;
3799 case PM_ARRAY_NODE: {
3800 const pm_array_node_t *cast = (const pm_array_node_t *) node;
3801
3802 if (cast->elements.size > 0 && !lfinish[1]) {
3803 lfinish[1] = NEW_LABEL(location.line);
3804 }
3805
3806 for (size_t index = 0; index < cast->elements.size; index++) {
3807 pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
3808 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3809 }
3810
3811 dtype = DEFINED_EXPR;
3812 break;
3813 }
3814 case PM_HASH_NODE:
3815 case PM_KEYWORD_HASH_NODE: {
3816 const pm_node_list_t *elements;
3817
3818 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
3819 elements = &((const pm_hash_node_t *) node)->elements;
3820 }
3821 else {
3822 elements = &((const pm_keyword_hash_node_t *) node)->elements;
3823 }
3824
3825 if (elements->size > 0 && !lfinish[1]) {
3826 lfinish[1] = NEW_LABEL(location.line);
3827 }
3828
3829 for (size_t index = 0; index < elements->size; index++) {
3830 const pm_node_t *element = elements->nodes[index];
3831
3832 switch (PM_NODE_TYPE(element)) {
3833 case PM_ASSOC_NODE: {
3834 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
3835
3836 pm_compile_defined_expr0(iseq, assoc->key, node_location, ret, popped, scope_node, true, lfinish, false);
3837 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3838
3839 pm_compile_defined_expr0(iseq, assoc->value, node_location, ret, popped, scope_node, true, lfinish, false);
3840 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3841
3842 break;
3843 }
3844 case PM_ASSOC_SPLAT_NODE: {
3845 const pm_assoc_splat_node_t *assoc_splat = (const pm_assoc_splat_node_t *) element;
3846
3847 pm_compile_defined_expr0(iseq, assoc_splat->value, node_location, ret, popped, scope_node, true, lfinish, false);
3848 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3849
3850 break;
3851 }
3852 default:
3853 rb_bug("unexpected node type in hash node: %s", pm_node_type_to_str(PM_NODE_TYPE(element)));
3854 break;
3855 }
3856 }
3857
3858 dtype = DEFINED_EXPR;
3859 break;
3860 }
3861 case PM_SPLAT_NODE: {
3862 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
3863 pm_compile_defined_expr0(iseq, cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish, false);
3864
3865 if (!lfinish[1]) {
3866 lfinish[1] = NEW_LABEL(location.line);
3867 }
3868
3869 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3870 dtype = DEFINED_EXPR;
3871 break;
3872 }
3873 case PM_IMPLICIT_NODE: {
3874 const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
3875 pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
3876 return;
3877 }
3878 case PM_AND_NODE:
3879 case PM_BEGIN_NODE:
3880 case PM_BREAK_NODE:
3881 case PM_CASE_NODE:
3882 case PM_CASE_MATCH_NODE:
3883 case PM_CLASS_NODE:
3884 case PM_DEF_NODE:
3885 case PM_DEFINED_NODE:
3886 case PM_FLOAT_NODE:
3887 case PM_FOR_NODE:
3888 case PM_IF_NODE:
3889 case PM_IMAGINARY_NODE:
3890 case PM_INTEGER_NODE:
3895 case PM_LAMBDA_NODE:
3899 case PM_MODULE_NODE:
3900 case PM_NEXT_NODE:
3901 case PM_OR_NODE:
3902 case PM_RANGE_NODE:
3903 case PM_RATIONAL_NODE:
3904 case PM_REDO_NODE:
3906 case PM_RETRY_NODE:
3907 case PM_RETURN_NODE:
3912 case PM_STRING_NODE:
3913 case PM_SYMBOL_NODE:
3914 case PM_UNLESS_NODE:
3915 case PM_UNTIL_NODE:
3916 case PM_WHILE_NODE:
3917 case PM_X_STRING_NODE:
3918 dtype = DEFINED_EXPR;
3919 break;
3921 dtype = DEFINED_LVAR;
3922 break;
3923
3924#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3925
3928
3929 ID name = pm_constant_id_lookup(scope_node, cast->name);
3930 PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3931
3932 return;
3933 }
3935 const char *char_ptr = (const char *) (node->location.start + 1);
3936 ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
3937
3938 PUSH_INSN(ret, location, putnil);
3939 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), backref_val, PUSH_VAL(DEFINED_GVAR));
3940
3941 return;
3942 }
3944 uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
3945
3946 PUSH_INSN(ret, location, putnil);
3947 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), INT2FIX(reference_number << 1), PUSH_VAL(DEFINED_GVAR));
3948
3949 return;
3950 }
3953 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3954
3955 PUSH_INSN(ret, location, putnil);
3956 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, PUSH_VAL(DEFINED_GVAR));
3957
3958 return;
3959 }
3962 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3963
3964 PUSH_INSN(ret, location, putnil);
3965 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, PUSH_VAL(DEFINED_CVAR));
3966
3967 return;
3968 }
3969 case PM_CONSTANT_READ_NODE: {
3970 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
3971 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3972
3973 PUSH_INSN(ret, location, putnil);
3974 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, PUSH_VAL(DEFINED_CONST));
3975
3976 return;
3977 }
3978 case PM_CONSTANT_PATH_NODE: {
3979 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
3980 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3981
3982 if (cast->parent != NULL) {
3983 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
3984 pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
3985
3986 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3987 PM_COMPILE(cast->parent);
3988 }
3989 else {
3990 PUSH_INSN1(ret, location, putobject, rb_cObject);
3991 }
3992
3993 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, PUSH_VAL(DEFINED_CONST));
3994 return;
3995 }
3996 case PM_CALL_NODE: {
3997 const pm_call_node_t *cast = ((const pm_call_node_t *) node);
3998
3999 if (cast->block != NULL && PM_NODE_TYPE_P(cast->block, PM_BLOCK_NODE)) {
4000 dtype = DEFINED_EXPR;
4001 break;
4002 }
4003
4004 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
4005
4006 if (cast->receiver || cast->arguments) {
4007 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
4008 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.line);
4009 }
4010
4011 if (cast->arguments) {
4012 pm_compile_defined_expr0(iseq, (const pm_node_t *) cast->arguments, node_location, ret, popped, scope_node, true, lfinish, false);
4013 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4014 }
4015
4016 if (cast->receiver) {
4017 pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
4018
4019 if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE)) {
4020 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4021
4022 const pm_call_node_t *receiver = (const pm_call_node_t *) cast->receiver;
4023 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4024 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4025 }
4026 else {
4027 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4028 PM_COMPILE(cast->receiver);
4029 }
4030
4031 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4032 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4033 }
4034 else {
4035 PUSH_INSN(ret, location, putself);
4036 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4037 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4038 }
4039
4040 return;
4041 }
4042 case PM_YIELD_NODE:
4043 PUSH_INSN(ret, location, putnil);
4044 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
4045 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
4046 return;
4047 case PM_SUPER_NODE:
4049 PUSH_INSN(ret, location, putnil);
4050 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
4051 return;
4055
4060
4065
4070
4075
4079
4084
4089
4091 dtype = DEFINED_ASGN;
4092 break;
4093 default:
4094 rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4095 }
4096
4097 RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
4098 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4099#undef PUSH_VAL
4100}
4101
4102static void
4103pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
4104{
4105 LINK_ELEMENT *lcur = ret->last;
4106 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
4107
4108 if (lfinish[1]) {
4109 LABEL *lstart = NEW_LABEL(node_location->line);
4110 LABEL *lend = NEW_LABEL(node_location->line);
4111
4113 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4114
4115 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4116 iseq,
4117 ifunc,
4118 rb_str_concat(rb_str_new2("defined guard in "), ISEQ_BODY(iseq)->location.label),
4119 iseq,
4120 ISEQ_TYPE_RESCUE,
4121 0
4122 );
4123
4124 lstart->rescued = LABEL_RESCUE_BEG;
4125 lend->rescued = LABEL_RESCUE_END;
4126
4127 APPEND_LABEL(ret, lcur, lstart);
4128 PUSH_LABEL(ret, lend);
4129 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4130 }
4131}
4132
4133static void
4134pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition)
4135{
4136 LABEL *lfinish[3];
4137 LINK_ELEMENT *last = ret->last;
4138
4139 lfinish[0] = NEW_LABEL(node_location->line);
4140 lfinish[1] = 0;
4141 lfinish[2] = 0;
4142
4143 if (!popped) {
4144 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
4145 }
4146
4147 if (lfinish[1]) {
4148 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->line, node_location->node_id, BIN(putnil), 0)->link);
4149 PUSH_INSN(ret, *node_location, swap);
4150
4151 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4152 PUSH_INSN(ret, *node_location, pop);
4153 PUSH_LABEL(ret, lfinish[1]);
4154
4155 }
4156
4157 PUSH_LABEL(ret, lfinish[0]);
4158}
4159
4160// This is exactly the same as add_ensure_iseq, except it compiled
4161// the node as a Prism node, and not a CRuby node
4162static void
4163pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_scope_node_t *scope_node)
4164{
4165 RUBY_ASSERT(can_add_ensure_iseq(iseq));
4166
4168 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4169 struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
4170 DECL_ANCHOR(ensure);
4171
4172 while (enlp) {
4173 if (enlp->erange != NULL) {
4174 DECL_ANCHOR(ensure_part);
4175 LABEL *lstart = NEW_LABEL(0);
4176 LABEL *lend = NEW_LABEL(0);
4177
4178 add_ensure_range(iseq, enlp->erange, lstart, lend);
4179
4180 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4181 PUSH_LABEL(ensure_part, lstart);
4182 bool popped = true;
4183 PM_COMPILE_INTO_ANCHOR(ensure_part, (const pm_node_t *) enlp->ensure_node);
4184 PUSH_LABEL(ensure_part, lend);
4185 PUSH_SEQ(ensure, ensure_part);
4186 }
4187 else {
4188 if (!is_return) {
4189 break;
4190 }
4191 }
4192 enlp = enlp->prev;
4193 }
4194 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4195 PUSH_SEQ(ret, ensure);
4196}
4197
4199 pm_scope_node_t *scope_node;
4200 rb_ast_id_table_t *local_table_for_iseq;
4201 int local_index;
4202};
4203
4204static int
4205pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
4206{
4207 if (!existing) {
4208 pm_constant_id_t constant_id = (pm_constant_id_t) *key;
4209 struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *) arg;
4210
4211 pm_scope_node_t *scope_node = ctx->scope_node;
4212 rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
4213 int local_index = ctx->local_index;
4214
4215 ID local = pm_constant_id_lookup(scope_node, constant_id);
4216 local_table_for_iseq->ids[local_index] = local;
4217
4218 *value = (st_data_t)local_index;
4219
4220 ctx->local_index++;
4221 }
4222
4223 return ST_CONTINUE;
4224}
4225
4231static void
4232pm_insert_local_index(pm_constant_id_t constant_id, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node)
4233{
4234 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4235
4236 ID local = pm_constant_id_lookup(scope_node, constant_id);
4237 local_table_for_iseq->ids[local_index] = local;
4238 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4239}
4240
4245static void
4246pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
4247{
4248 local_table_for_iseq->ids[local_index] = local_name;
4249 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4250}
4251
4258static int
4259pm_compile_destructured_param_locals(const pm_multi_target_node_t *node, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node, int local_index)
4260{
4261 for (size_t index = 0; index < node->lefts.size; index++) {
4262 const pm_node_t *left = node->lefts.nodes[index];
4263
4266 pm_insert_local_index(((const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4267 local_index++;
4268 }
4269 }
4270 else {
4272 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);
4273 }
4274 }
4275
4276 if (node->rest != NULL && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE)) {
4277 const pm_splat_node_t *rest = (const pm_splat_node_t *) node->rest;
4278
4279 if (rest->expression != NULL) {
4281
4282 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4283 pm_insert_local_index(((const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4284 local_index++;
4285 }
4286 }
4287 }
4288
4289 for (size_t index = 0; index < node->rights.size; index++) {
4290 const pm_node_t *right = node->rights.nodes[index];
4291
4294 pm_insert_local_index(((const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4295 local_index++;
4296 }
4297 }
4298 else {
4300 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);
4301 }
4302 }
4303
4304 return local_index;
4305}
4306
4311static inline void
4312pm_compile_destructured_param_write(rb_iseq_t *iseq, const pm_required_parameter_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
4313{
4314 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4315 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
4316 PUSH_SETLOCAL(ret, location, index.index, index.level);
4317}
4318
4327static void
4328pm_compile_destructured_param_writes(rb_iseq_t *iseq, const pm_multi_target_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
4329{
4330 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4331 bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((const pm_splat_node_t *) node->rest)->expression) != NULL);
4332 bool has_rights = node->rights.size > 0;
4333
4334 int flag = (has_rest || has_rights) ? 1 : 0;
4335 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->lefts.size), INT2FIX(flag));
4336
4337 for (size_t index = 0; index < node->lefts.size; index++) {
4338 const pm_node_t *left = node->lefts.nodes[index];
4339
4341 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) left, ret, scope_node);
4342 }
4343 else {
4345 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) left, ret, scope_node);
4346 }
4347 }
4348
4349 if (has_rest) {
4350 if (has_rights) {
4351 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(3));
4352 }
4353
4354 const pm_node_t *rest = ((const pm_splat_node_t *) node->rest)->expression;
4356
4357 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) rest, ret, scope_node);
4358 }
4359
4360 if (has_rights) {
4361 if (!has_rest) {
4362 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(2));
4363 }
4364
4365 for (size_t index = 0; index < node->rights.size; index++) {
4366 const pm_node_t *right = node->rights.nodes[index];
4367
4369 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) right, ret, scope_node);
4370 }
4371 else {
4373 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) right, ret, scope_node);
4374 }
4375 }
4376 }
4377}
4378
4384 // The pointer to the topn instruction that will need to be modified after
4385 // we know the total stack size of all of the targets.
4386 INSN *topn;
4387
4388 // The index of the stack from the base of the entire multi target at which
4389 // the parent expression is located.
4390 size_t stack_index;
4391
4392 // The number of slots in the stack that this node occupies.
4393 size_t stack_size;
4394
4395 // The position of the node in the list of targets.
4396 size_t position;
4397
4398 // A pointer to the next node in this linked list.
4399 struct pm_multi_target_state_node *next;
4400} pm_multi_target_state_node_t;
4401
4409typedef struct {
4410 // The total number of slots in the stack that this multi target occupies.
4411 size_t stack_size;
4412
4413 // The position of the current node being compiled. This is forwarded to
4414 // nodes when they are allocated.
4415 size_t position;
4416
4417 // A pointer to the head of this linked list.
4418 pm_multi_target_state_node_t *head;
4419
4420 // A pointer to the tail of this linked list.
4421 pm_multi_target_state_node_t *tail;
4423
4427static void
4428pm_multi_target_state_push(pm_multi_target_state_t *state, INSN *topn, size_t stack_size)
4429{
4430 pm_multi_target_state_node_t *node = ALLOC(pm_multi_target_state_node_t);
4431 node->topn = topn;
4432 node->stack_index = state->stack_size + 1;
4433 node->stack_size = stack_size;
4434 node->position = state->position;
4435 node->next = NULL;
4436
4437 if (state->head == NULL) {
4438 state->head = node;
4439 state->tail = node;
4440 }
4441 else {
4442 state->tail->next = node;
4443 state->tail = node;
4444 }
4445
4446 state->stack_size += stack_size;
4447}
4448
4454static void
4455pm_multi_target_state_update(pm_multi_target_state_t *state)
4456{
4457 // If nothing was ever pushed onto the stack, then we don't need to do any
4458 // kind of updates.
4459 if (state->stack_size == 0) return;
4460
4461 pm_multi_target_state_node_t *current = state->head;
4462 pm_multi_target_state_node_t *previous;
4463
4464 while (current != NULL) {
4465 VALUE offset = INT2FIX(state->stack_size - current->stack_index + current->position);
4466 current->topn->operands[0] = offset;
4467
4468 // stack_size will be > 1 in the case that we compiled an index target
4469 // and it had arguments. In this case, we use multiple topn instructions
4470 // to grab up all of the arguments as well, so those offsets need to be
4471 // updated as well.
4472 if (current->stack_size > 1) {
4473 INSN *insn = current->topn;
4474
4475 for (size_t index = 1; index < current->stack_size; index += 1) {
4476 LINK_ELEMENT *element = get_next_insn(insn);
4477 RUBY_ASSERT(IS_INSN(element));
4478
4479 insn = (INSN *) element;
4480 RUBY_ASSERT(insn->insn_id == BIN(topn));
4481
4482 insn->operands[0] = offset;
4483 }
4484 }
4485
4486 previous = current;
4487 current = current->next;
4488
4489 xfree(previous);
4490 }
4491}
4492
4493static void
4494pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state);
4495
4524static void
4525pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
4526{
4527 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4528
4529 switch (PM_NODE_TYPE(node)) {
4531 // Local variable targets have no parent expression, so they only need
4532 // to compile the write.
4533 //
4534 // for i in []; end
4535 //
4537 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
4538
4539 PUSH_SETLOCAL(writes, location, index.index, index.level);
4540 break;
4541 }
4543 // Class variable targets have no parent expression, so they only need
4544 // to compile the write.
4545 //
4546 // for @@i in []; end
4547 //
4549 ID name = pm_constant_id_lookup(scope_node, cast->name);
4550
4551 VALUE operand = ID2SYM(name);
4552 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
4553 break;
4554 }
4556 // Constant targets have no parent expression, so they only need to
4557 // compile the write.
4558 //
4559 // for I in []; end
4560 //
4561 const pm_constant_target_node_t *cast = (const pm_constant_target_node_t *) node;
4562 ID name = pm_constant_id_lookup(scope_node, cast->name);
4563
4564 VALUE operand = ID2SYM(name);
4565 PUSH_INSN1(writes, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
4566 PUSH_INSN1(writes, location, setconstant, operand);
4567 break;
4568 }
4570 // Global variable targets have no parent expression, so they only need
4571 // to compile the write.
4572 //
4573 // for $i in []; end
4574 //
4576 ID name = pm_constant_id_lookup(scope_node, cast->name);
4577
4578 VALUE operand = ID2SYM(name);
4579 PUSH_INSN1(writes, location, setglobal, operand);
4580 break;
4581 }
4583 // Instance variable targets have no parent expression, so they only
4584 // need to compile the write.
4585 //
4586 // for @i in []; end
4587 //
4589 ID name = pm_constant_id_lookup(scope_node, cast->name);
4590
4591 VALUE operand = ID2SYM(name);
4592 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
4593 break;
4594 }
4596 // Constant path targets have a parent expression that is the object
4597 // that owns the constant. This needs to be compiled first into the
4598 // parents sequence. If no parent is found, then it represents using the
4599 // unary :: operator to indicate a top-level constant. In that case we
4600 // need to push Object onto the stack.
4601 //
4602 // for I::J in []; end
4603 //
4605 ID name = pm_constant_id_lookup(scope_node, cast->name);
4606
4607 if (cast->parent != NULL) {
4608 pm_compile_node(iseq, cast->parent, parents, false, scope_node);
4609 }
4610 else {
4611 PUSH_INSN1(parents, location, putobject, rb_cObject);
4612 }
4613
4614 if (state == NULL) {
4615 PUSH_INSN(writes, location, swap);
4616 }
4617 else {
4618 PUSH_INSN1(writes, location, topn, INT2FIX(1));
4619 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
4620 }
4621
4622 VALUE operand = ID2SYM(name);
4623 PUSH_INSN1(writes, location, setconstant, operand);
4624
4625 if (state != NULL) {
4626 PUSH_INSN(cleanup, location, pop);
4627 }
4628
4629 break;
4630 }
4631 case PM_CALL_TARGET_NODE: {
4632 // Call targets have a parent expression that is the receiver of the
4633 // method being called. This needs to be compiled first into the parents
4634 // sequence. These nodes cannot have arguments, so the method call is
4635 // compiled with a single argument which represents the value being
4636 // written.
4637 //
4638 // for i.j in []; end
4639 //
4640 const pm_call_target_node_t *cast = (const pm_call_target_node_t *) node;
4641 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
4642
4643 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
4644
4645 LABEL *safe_label = NULL;
4647 safe_label = NEW_LABEL(location.line);
4648 PUSH_INSN(parents, location, dup);
4649 PUSH_INSNL(parents, location, branchnil, safe_label);
4650 }
4651
4652 if (state != NULL) {
4653 PUSH_INSN1(writes, location, topn, INT2FIX(1));
4654 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
4655 PUSH_INSN(writes, location, swap);
4656 }
4657
4658 int flags = VM_CALL_ARGS_SIMPLE;
4659 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
4660
4661 PUSH_SEND_WITH_FLAG(writes, location, method_id, INT2FIX(1), INT2FIX(flags));
4662 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
4663 PUSH_INSN(writes, location, pop);
4664 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
4665
4666 if (state != NULL) {
4667 PUSH_INSN(cleanup, location, pop);
4668 }
4669
4670 break;
4671 }
4672 case PM_INDEX_TARGET_NODE: {
4673 // Index targets have a parent expression that is the receiver of the
4674 // method being called and any additional arguments that are being
4675 // passed along with the value being written. The receiver and arguments
4676 // both need to be on the stack. Note that this is even more complicated
4677 // by the fact that these nodes can hold a block using the unary &
4678 // operator.
4679 //
4680 // for i[:j] in []; end
4681 //
4682 const pm_index_target_node_t *cast = (const pm_index_target_node_t *) node;
4683
4684 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
4685
4686 int flags = 0;
4687 struct rb_callinfo_kwarg *kwargs = NULL;
4688 int argc = pm_setup_args(cast->arguments, (const pm_node_t *) cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
4689
4690 if (state != NULL) {
4691 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
4692 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
4693
4694 if (argc == 0) {
4695 PUSH_INSN(writes, location, swap);
4696 }
4697 else {
4698 for (int index = 0; index < argc; index++) {
4699 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
4700 }
4701 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
4702 }
4703 }
4704
4705 // The argc that we're going to pass to the send instruction is the
4706 // number of arguments + 1 for the value being written. If there's a
4707 // splat, then we need to insert newarray and concatarray instructions
4708 // after the arguments have been written.
4709 int ci_argc = argc + 1;
4710 if (flags & VM_CALL_ARGS_SPLAT) {
4711 ci_argc--;
4712 PUSH_INSN1(writes, location, newarray, INT2FIX(1));
4713 PUSH_INSN(writes, location, concatarray);
4714 }
4715
4716 PUSH_SEND_R(writes, location, idASET, INT2NUM(ci_argc), NULL, INT2FIX(flags), kwargs);
4717 PUSH_INSN(writes, location, pop);
4718
4719 if (state != NULL) {
4720 if (argc != 0) {
4721 PUSH_INSN(writes, location, pop);
4722 }
4723
4724 for (int index = 0; index < argc + 1; index++) {
4725 PUSH_INSN(cleanup, location, pop);
4726 }
4727 }
4728
4729 break;
4730 }
4731 case PM_MULTI_TARGET_NODE: {
4732 // Multi target nodes represent a set of writes to multiple variables.
4733 // The parent expressions are the combined set of the parent expressions
4734 // of its inner target nodes.
4735 //
4736 // for i, j in []; end
4737 //
4738 size_t before_position;
4739 if (state != NULL) {
4740 before_position = state->position;
4741 state->position--;
4742 }
4743
4744 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
4745 if (state != NULL) state->position = before_position;
4746
4747 break;
4748 }
4749 default:
4750 rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4751 break;
4752 }
4753}
4754
4760static void
4761pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
4762{
4763 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4764 const pm_node_list_t *lefts;
4765 const pm_node_t *rest;
4766 const pm_node_list_t *rights;
4767
4768 switch (PM_NODE_TYPE(node)) {
4769 case PM_MULTI_TARGET_NODE: {
4770 const pm_multi_target_node_t *cast = (const pm_multi_target_node_t *) node;
4771 lefts = &cast->lefts;
4772 rest = cast->rest;
4773 rights = &cast->rights;
4774 break;
4775 }
4776 case PM_MULTI_WRITE_NODE: {
4777 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
4778 lefts = &cast->lefts;
4779 rest = cast->rest;
4780 rights = &cast->rights;
4781 break;
4782 }
4783 default:
4784 rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4785 break;
4786 }
4787
4788 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) rest)->expression != NULL;
4789 bool has_posts = rights->size > 0;
4790
4791 // The first instruction in the writes sequence is going to spread the
4792 // top value of the stack onto the number of values that we're going to
4793 // write.
4794 PUSH_INSN2(writes, location, expandarray, INT2FIX(lefts->size), INT2FIX((has_rest || has_posts) ? 1 : 0));
4795
4796 // We need to keep track of some additional state information as we're
4797 // going through the targets because we will need to revisit them once
4798 // we know how many values are being pushed onto the stack.
4799 pm_multi_target_state_t target_state = { 0 };
4800 if (state == NULL) state = &target_state;
4801
4802 size_t base_position = state->position;
4803 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
4804
4805 // Next, we'll iterate through all of the leading targets.
4806 for (size_t index = 0; index < lefts->size; index++) {
4807 const pm_node_t *target = lefts->nodes[index];
4808 state->position = lefts->size - index + splat_position + base_position;
4809 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
4810 }
4811
4812 // Next, we'll compile the rest target if there is one.
4813 if (has_rest) {
4814 const pm_node_t *target = ((const pm_splat_node_t *) rest)->expression;
4815 state->position = 1 + rights->size + base_position;
4816
4817 if (has_posts) {
4818 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(3));
4819 }
4820
4821 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
4822 }
4823
4824 // Finally, we'll compile the trailing targets.
4825 if (has_posts) {
4826 if (!has_rest && rest != NULL) {
4827 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(2));
4828 }
4829
4830 for (size_t index = 0; index < rights->size; index++) {
4831 const pm_node_t *target = rights->nodes[index];
4832 state->position = rights->size - index + base_position;
4833 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
4834 }
4835 }
4836}
4837
4843static void
4844pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
4845{
4846 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4847
4848 switch (PM_NODE_TYPE(node)) {
4850 // For local variables, all we have to do is retrieve the value and then
4851 // compile the index node.
4852 PUSH_GETLOCAL(ret, location, 1, 0);
4853 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
4854 break;
4855 }
4862 case PM_INDEX_TARGET_NODE: {
4863 // For other targets, we need to potentially compile the parent or
4864 // owning expression of this target, then retrieve the value, expand it,
4865 // and then compile the necessary writes.
4866 DECL_ANCHOR(writes);
4867 DECL_ANCHOR(cleanup);
4868
4869 pm_multi_target_state_t state = { 0 };
4870 state.position = 1;
4871 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
4872
4873 PUSH_GETLOCAL(ret, location, 1, 0);
4874 PUSH_INSN2(ret, location, expandarray, INT2FIX(1), INT2FIX(0));
4875
4876 PUSH_SEQ(ret, writes);
4877 PUSH_SEQ(ret, cleanup);
4878
4879 pm_multi_target_state_update(&state);
4880 break;
4881 }
4882 case PM_MULTI_TARGET_NODE: {
4883 DECL_ANCHOR(writes);
4884 DECL_ANCHOR(cleanup);
4885
4886 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
4887
4888 LABEL *not_single = NEW_LABEL(location.line);
4889 LABEL *not_ary = NEW_LABEL(location.line);
4890
4891 // When there are multiple targets, we'll do a bunch of work to convert
4892 // the value into an array before we expand it. Effectively we're trying
4893 // to accomplish:
4894 //
4895 // (args.length == 1 && Array.try_convert(args[0])) || args
4896 //
4897 PUSH_GETLOCAL(ret, location, 1, 0);
4898 PUSH_INSN(ret, location, dup);
4899 PUSH_CALL(ret, location, idLength, INT2FIX(0));
4900 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
4901 PUSH_CALL(ret, location, idEq, INT2FIX(1));
4902 PUSH_INSNL(ret, location, branchunless, not_single);
4903 PUSH_INSN(ret, location, dup);
4904 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
4905 PUSH_CALL(ret, location, idAREF, INT2FIX(1));
4906 PUSH_INSN1(ret, location, putobject, rb_cArray);
4907 PUSH_INSN(ret, location, swap);
4908 PUSH_CALL(ret, location, rb_intern("try_convert"), INT2FIX(1));
4909 PUSH_INSN(ret, location, dup);
4910 PUSH_INSNL(ret, location, branchunless, not_ary);
4911 PUSH_INSN(ret, location, swap);
4912
4913 PUSH_LABEL(ret, not_ary);
4914 PUSH_INSN(ret, location, pop);
4915
4916 PUSH_LABEL(ret, not_single);
4917 PUSH_SEQ(ret, writes);
4918 PUSH_SEQ(ret, cleanup);
4919 break;
4920 }
4921 default:
4922 rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4923 break;
4924 }
4925}
4926
4927static void
4928pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
4929{
4930 const pm_parser_t *parser = scope_node->parser;
4931
4932 LABEL *lstart = NEW_LABEL(node_location->line);
4933 LABEL *lend = NEW_LABEL(node_location->line);
4934 LABEL *lcont = NEW_LABEL(node_location->line);
4935
4936 pm_scope_node_t rescue_scope_node;
4937 pm_scope_node_init((const pm_node_t *) cast->rescue_clause, &rescue_scope_node, scope_node);
4938
4939 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
4940 &rescue_scope_node,
4941 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
4942 ISEQ_TYPE_RESCUE,
4943 pm_node_line_number(parser, (const pm_node_t *) cast->rescue_clause)
4944 );
4945
4946 pm_scope_node_destroy(&rescue_scope_node);
4947
4948 lstart->rescued = LABEL_RESCUE_BEG;
4949 lend->rescued = LABEL_RESCUE_END;
4950 PUSH_LABEL(ret, lstart);
4951
4952 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
4953 ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
4954
4955 if (cast->statements != NULL) {
4956 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->statements);
4957 }
4958 else {
4959 const pm_node_location_t location = PM_NODE_START_LOCATION(parser, cast->rescue_clause);
4960 PUSH_INSN(ret, location, putnil);
4961 }
4962
4963 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
4964 PUSH_LABEL(ret, lend);
4965
4966 if (cast->else_clause != NULL) {
4967 if (!popped) PUSH_INSN(ret, *node_location, pop);
4968 PM_COMPILE((const pm_node_t *) cast->else_clause);
4969 }
4970
4971 PUSH_INSN(ret, *node_location, nop);
4972 PUSH_LABEL(ret, lcont);
4973
4974 if (popped) PUSH_INSN(ret, *node_location, pop);
4975 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
4976 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
4977}
4978
4979static void
4980pm_compile_ensure(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
4981{
4982 const pm_parser_t *parser = scope_node->parser;
4983 const pm_statements_node_t *statements = cast->ensure_clause->statements;
4984
4985 pm_node_location_t location;
4986 if (statements != NULL) {
4987 location = PM_NODE_START_LOCATION(parser, statements);
4988 }
4989 else {
4990 location = *node_location;
4991 }
4992
4993 LABEL *lstart = NEW_LABEL(location.line);
4994 LABEL *lend = NEW_LABEL(location.line);
4995 LABEL *lcont = NEW_LABEL(location.line);
4996
4997 struct ensure_range er;
4999 struct ensure_range *erange;
5000
5001 DECL_ANCHOR(ensr);
5002 if (statements != NULL) {
5003 pm_compile_node(iseq, (const pm_node_t *) statements, ensr, true, scope_node);
5004 }
5005
5006 LINK_ELEMENT *last = ensr->last;
5007 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5008
5009 er.begin = lstart;
5010 er.end = lend;
5011 er.next = 0;
5012 push_ensure_entry(iseq, &enl, &er, (void *) cast->ensure_clause);
5013
5014 PUSH_LABEL(ret, lstart);
5015 if (cast->rescue_clause != NULL) {
5016 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5017 }
5018 else if (cast->statements != NULL) {
5019 pm_compile_node(iseq, (const pm_node_t *) cast->statements, ret, popped | last_leave, scope_node);
5020 }
5021 else if (!(popped | last_leave)) {
5022 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5023 }
5024
5025 PUSH_LABEL(ret, lend);
5026 PUSH_SEQ(ret, ensr);
5027 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5028 PUSH_LABEL(ret, lcont);
5029 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5030
5031 pm_scope_node_t next_scope_node;
5032 pm_scope_node_init((const pm_node_t *) cast->ensure_clause, &next_scope_node, scope_node);
5033
5034 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
5035 &next_scope_node,
5036 rb_str_concat(rb_str_new2("ensure in "), ISEQ_BODY(iseq)->location.label),
5037 ISEQ_TYPE_ENSURE,
5038 location.line
5039 );
5040
5041 pm_scope_node_destroy(&next_scope_node);
5042
5043 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5044 if (lstart->link.next != &lend->link) {
5045 while (erange) {
5046 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5047 erange = erange->next;
5048 }
5049 }
5050 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5051}
5052
5057static inline bool
5058pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
5059{
5060 return (
5062 node->receiver != NULL &&
5064 node->arguments == NULL &&
5065 node->block == NULL &&
5066 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5067 );
5068}
5069
5074static inline bool
5075pm_opt_aref_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
5076{
5077 return (
5079 node->arguments != NULL &&
5081 ((const pm_arguments_node_t *) node->arguments)->arguments.size == 1 &&
5082 PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
5083 node->block == NULL &&
5084 !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
5085 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5086 );
5087}
5088
5093static inline bool
5094pm_opt_aset_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
5095{
5096 return (
5098 node->arguments != NULL &&
5100 ((const pm_arguments_node_t *) node->arguments)->arguments.size == 2 &&
5101 PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
5102 node->block == NULL &&
5103 !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
5104 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5105 );
5106}
5107
5112static void
5113pm_compile_constant_read(rb_iseq_t *iseq, VALUE name, const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
5114{
5115 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5116
5117 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5118 ISEQ_BODY(iseq)->ic_size++;
5119 VALUE segments = rb_ary_new_from_args(1, name);
5120 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5121 }
5122 else {
5123 PUSH_INSN(ret, location, putnil);
5124 PUSH_INSN1(ret, location, putobject, Qtrue);
5125 PUSH_INSN1(ret, location, getconstant, name);
5126 }
5127}
5128
5133static VALUE
5134pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
5135{
5136 VALUE parts = rb_ary_new();
5137
5138 while (true) {
5139 switch (PM_NODE_TYPE(node)) {
5140 case PM_CONSTANT_READ_NODE: {
5141 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
5142 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5143
5144 rb_ary_unshift(parts, name);
5145 return parts;
5146 }
5147 case PM_CONSTANT_PATH_NODE: {
5148 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
5149 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5150
5151 rb_ary_unshift(parts, name);
5152 if (cast->parent == NULL) {
5153 rb_ary_unshift(parts, ID2SYM(idNULL));
5154 return parts;
5155 }
5156
5157 node = cast->parent;
5158 break;
5159 }
5160 default:
5161 return Qnil;
5162 }
5163 }
5164}
5165
5171static void
5172pm_compile_constant_path(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const prefix, LINK_ANCHOR *const body, bool popped, pm_scope_node_t *scope_node)
5173{
5174 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5175
5176 switch (PM_NODE_TYPE(node)) {
5177 case PM_CONSTANT_READ_NODE: {
5178 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
5179 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5180
5181 PUSH_INSN1(body, location, putobject, Qtrue);
5182 PUSH_INSN1(body, location, getconstant, name);
5183 break;
5184 }
5185 case PM_CONSTANT_PATH_NODE: {
5186 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
5187 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5188
5189 if (cast->parent == NULL) {
5190 PUSH_INSN(body, location, pop);
5191 PUSH_INSN1(body, location, putobject, rb_cObject);
5192 PUSH_INSN1(body, location, putobject, Qtrue);
5193 PUSH_INSN1(body, location, getconstant, name);
5194 }
5195 else {
5196 pm_compile_constant_path(iseq, cast->parent, prefix, body, false, scope_node);
5197 PUSH_INSN1(body, location, putobject, Qfalse);
5198 PUSH_INSN1(body, location, getconstant, name);
5199 }
5200 break;
5201 }
5202 default:
5203 PM_COMPILE_INTO_ANCHOR(prefix, node);
5204 break;
5205 }
5206}
5207
5211static VALUE
5212pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
5213{
5214 switch (PM_NODE_TYPE(node)) {
5215 case PM_TRUE_NODE:
5216 case PM_FALSE_NODE:
5217 case PM_NIL_NODE:
5218 case PM_SYMBOL_NODE:
5221 case PM_INTEGER_NODE:
5222 case PM_FLOAT_NODE:
5223 case PM_RATIONAL_NODE:
5224 case PM_IMAGINARY_NODE:
5226 return pm_static_literal_value(iseq, node, scope_node);
5227 case PM_STRING_NODE:
5228 return parse_static_literal_string(iseq, scope_node, node, &((const pm_string_node_t *) node)->unescaped);
5230 return pm_source_file_value((const pm_source_file_node_t *) node, scope_node);
5231 case PM_ARRAY_NODE: {
5232 const pm_array_node_t *cast = (const pm_array_node_t *) node;
5233 VALUE result = rb_ary_new_capa(cast->elements.size);
5234
5235 for (size_t index = 0; index < cast->elements.size; index++) {
5236 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->elements.nodes[index], scope_node);
5237 if (element == Qundef) return Qundef;
5238
5239 rb_ary_push(result, element);
5240 }
5241
5242 return rb_ractor_make_shareable(result);
5243 }
5244 case PM_HASH_NODE: {
5245 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
5246 VALUE result = rb_hash_new_capa(cast->elements.size);
5247
5248 for (size_t index = 0; index < cast->elements.size; index++) {
5249 const pm_node_t *element = cast->elements.nodes[index];
5250 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE)) return Qundef;
5251
5252 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
5253
5254 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->key, scope_node);
5255 if (key == Qundef) return Qundef;
5256
5257 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->value, scope_node);
5258 if (value == Qundef) return Qundef;
5259
5260 rb_hash_aset(result, key, value);
5261 }
5262
5263 return rb_ractor_make_shareable(result);
5264 }
5265 default:
5266 return Qundef;
5267 }
5268}
5269
5274static void
5275pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_flags_t shareability, VALUE path, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, bool top)
5276{
5277 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5278 if (literal != Qundef) {
5279 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5280 PUSH_INSN1(ret, location, putobject, literal);
5281 return;
5282 }
5283
5284 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5285 switch (PM_NODE_TYPE(node)) {
5286 case PM_ARRAY_NODE: {
5287 const pm_array_node_t *cast = (const pm_array_node_t *) node;
5288
5289 if (top) {
5290 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5291 }
5292
5293 for (size_t index = 0; index < cast->elements.size; index++) {
5294 pm_compile_shareable_constant_value(iseq, cast->elements.nodes[index], shareability, path, ret, scope_node, false);
5295 }
5296
5297 PUSH_INSN1(ret, location, newarray, INT2FIX(cast->elements.size));
5298
5299 if (top) {
5300 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
5301 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5302 }
5303
5304 return;
5305 }
5306 case PM_HASH_NODE: {
5307 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
5308
5309 if (top) {
5310 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5311 }
5312
5313 pm_compile_hash_elements(iseq, (const pm_node_t *) cast, &cast->elements, shareability, path, false, ret, scope_node);
5314
5315 if (top) {
5316 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
5317 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5318 }
5319
5320 return;
5321 }
5322 default: {
5323 DECL_ANCHOR(value_seq);
5324
5325 pm_compile_node(iseq, node, value_seq, false, scope_node);
5327 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus, INT2FIX(0), INT2FIX(VM_CALL_ARGS_SIMPLE));
5328 }
5329
5330 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5331 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5332 PUSH_SEQ(ret, value_seq);
5333 PUSH_INSN1(ret, location, putobject, path);
5334 PUSH_SEND_WITH_FLAG(ret, location, rb_intern("ensure_shareable"), INT2FIX(2), INT2FIX(VM_CALL_ARGS_SIMPLE));
5335 }
5337 if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5338 PUSH_SEQ(ret, value_seq);
5339 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable_copy"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5340 }
5342 if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5343 PUSH_SEQ(ret, value_seq);
5344 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5345 }
5346
5347 break;
5348 }
5349 }
5350}
5351
5356static void
5357pm_compile_constant_write_node(rb_iseq_t *iseq, const pm_constant_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5358{
5359 const pm_node_location_t location = *node_location;
5360 ID name_id = pm_constant_id_lookup(scope_node, node->name);
5361
5362 if (shareability != 0) {
5363 pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_id2str(name_id), ret, scope_node, true);
5364 }
5365 else {
5366 PM_COMPILE_NOT_POPPED(node->value);
5367 }
5368
5369 if (!popped) PUSH_INSN(ret, location, dup);
5370 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5371
5372 VALUE operand = ID2SYM(name_id);
5373 PUSH_INSN1(ret, location, setconstant, operand);
5374}
5375
5380static void
5381pm_compile_constant_and_write_node(rb_iseq_t *iseq, const pm_constant_and_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5382{
5383 const pm_node_location_t location = *node_location;
5384
5385 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5386 LABEL *end_label = NEW_LABEL(location.line);
5387
5388 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5389 if (!popped) PUSH_INSN(ret, location, dup);
5390
5391 PUSH_INSNL(ret, location, branchunless, end_label);
5392 if (!popped) PUSH_INSN(ret, location, pop);
5393
5394 if (shareability != 0) {
5395 pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
5396 }
5397 else {
5398 PM_COMPILE_NOT_POPPED(node->value);
5399 }
5400
5401 if (!popped) PUSH_INSN(ret, location, dup);
5402 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5403 PUSH_INSN1(ret, location, setconstant, name);
5404 PUSH_LABEL(ret, end_label);
5405}
5406
5411static void
5412pm_compile_constant_or_write_node(rb_iseq_t *iseq, const pm_constant_or_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5413{
5414 const pm_node_location_t location = *node_location;
5415 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5416
5417 LABEL *set_label = NEW_LABEL(location.line);
5418 LABEL *end_label = NEW_LABEL(location.line);
5419
5420 PUSH_INSN(ret, location, putnil);
5421 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, Qtrue);
5422 PUSH_INSNL(ret, location, branchunless, set_label);
5423
5424 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5425 if (!popped) PUSH_INSN(ret, location, dup);
5426
5427 PUSH_INSNL(ret, location, branchif, end_label);
5428 if (!popped) PUSH_INSN(ret, location, pop);
5429 PUSH_LABEL(ret, set_label);
5430
5431 if (shareability != 0) {
5432 pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
5433 }
5434 else {
5435 PM_COMPILE_NOT_POPPED(node->value);
5436 }
5437
5438 if (!popped) PUSH_INSN(ret, location, dup);
5439 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5440 PUSH_INSN1(ret, location, setconstant, name);
5441 PUSH_LABEL(ret, end_label);
5442}
5443
5448static void
5449pm_compile_constant_operator_write_node(rb_iseq_t *iseq, const pm_constant_operator_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5450{
5451 const pm_node_location_t location = *node_location;
5452
5453 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5454 ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
5455
5456 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5457
5458 if (shareability != 0) {
5459 pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
5460 }
5461 else {
5462 PM_COMPILE_NOT_POPPED(node->value);
5463 }
5464
5465 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5466 if (!popped) PUSH_INSN(ret, location, dup);
5467
5468 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5469 PUSH_INSN1(ret, location, setconstant, name);
5470}
5471
5476static VALUE
5477pm_constant_path_path(const pm_constant_path_node_t *node, const pm_scope_node_t *scope_node)
5478{
5479 VALUE parts = rb_ary_new();
5480 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->name)));
5481
5482 const pm_node_t *current = node->parent;
5483 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5484 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) current;
5485 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->name)));
5486 current = cast->parent;
5487 }
5488
5489 if (current == NULL) {
5490 rb_ary_unshift(parts, rb_id2str(idNULL));
5491 }
5492 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5493 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) current)->name)));
5494 }
5495 else {
5496 rb_ary_unshift(parts, rb_str_new_cstr("..."));
5497 }
5498
5499 return rb_ary_join(parts, rb_str_new_cstr("::"));
5500}
5501
5506static void
5507pm_compile_constant_path_write_node(rb_iseq_t *iseq, const pm_constant_path_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5508{
5509 const pm_node_location_t location = *node_location;
5510 const pm_constant_path_node_t *target = node->target;
5511 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5512
5513 if (target->parent) {
5514 PM_COMPILE_NOT_POPPED((const pm_node_t *) target->parent);
5515 }
5516 else {
5517 PUSH_INSN1(ret, location, putobject, rb_cObject);
5518 }
5519
5520 if (shareability != 0) {
5521 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5522 }
5523 else {
5524 PM_COMPILE_NOT_POPPED(node->value);
5525 }
5526
5527 if (!popped) {
5528 PUSH_INSN(ret, location, swap);
5529 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5530 }
5531
5532 PUSH_INSN(ret, location, swap);
5533 PUSH_INSN1(ret, location, setconstant, name);
5534}
5535
5540static void
5541pm_compile_constant_path_and_write_node(rb_iseq_t *iseq, const pm_constant_path_and_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5542{
5543 const pm_node_location_t location = *node_location;
5544 const pm_constant_path_node_t *target = node->target;
5545
5546 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5547 LABEL *lfin = NEW_LABEL(location.line);
5548
5549 if (target->parent) {
5550 PM_COMPILE_NOT_POPPED(target->parent);
5551 }
5552 else {
5553 PUSH_INSN1(ret, location, putobject, rb_cObject);
5554 }
5555
5556 PUSH_INSN(ret, location, dup);
5557 PUSH_INSN1(ret, location, putobject, Qtrue);
5558 PUSH_INSN1(ret, location, getconstant, name);
5559
5560 if (!popped) PUSH_INSN(ret, location, dup);
5561 PUSH_INSNL(ret, location, branchunless, lfin);
5562
5563 if (!popped) PUSH_INSN(ret, location, pop);
5564
5565 if (shareability != 0) {
5566 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5567 }
5568 else {
5569 PM_COMPILE_NOT_POPPED(node->value);
5570 }
5571
5572 if (popped) {
5573 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5574 }
5575 else {
5576 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
5577 PUSH_INSN(ret, location, swap);
5578 }
5579
5580 PUSH_INSN1(ret, location, setconstant, name);
5581 PUSH_LABEL(ret, lfin);
5582
5583 if (!popped) PUSH_INSN(ret, location, swap);
5584 PUSH_INSN(ret, location, pop);
5585}
5586
5591static void
5592pm_compile_constant_path_or_write_node(rb_iseq_t *iseq, const pm_constant_path_or_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5593{
5594 const pm_node_location_t location = *node_location;
5595 const pm_constant_path_node_t *target = node->target;
5596
5597 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5598 LABEL *lassign = NEW_LABEL(location.line);
5599 LABEL *lfin = NEW_LABEL(location.line);
5600
5601 if (target->parent) {
5602 PM_COMPILE_NOT_POPPED(target->parent);
5603 }
5604 else {
5605 PUSH_INSN1(ret, location, putobject, rb_cObject);
5606 }
5607
5608 PUSH_INSN(ret, location, dup);
5609 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, Qtrue);
5610 PUSH_INSNL(ret, location, branchunless, lassign);
5611
5612 PUSH_INSN(ret, location, dup);
5613 PUSH_INSN1(ret, location, putobject, Qtrue);
5614 PUSH_INSN1(ret, location, getconstant, name);
5615
5616 if (!popped) PUSH_INSN(ret, location, dup);
5617 PUSH_INSNL(ret, location, branchif, lfin);
5618
5619 if (!popped) PUSH_INSN(ret, location, pop);
5620 PUSH_LABEL(ret, lassign);
5621
5622 if (shareability != 0) {
5623 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5624 }
5625 else {
5626 PM_COMPILE_NOT_POPPED(node->value);
5627 }
5628
5629 if (popped) {
5630 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5631 }
5632 else {
5633 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
5634 PUSH_INSN(ret, location, swap);
5635 }
5636
5637 PUSH_INSN1(ret, location, setconstant, name);
5638 PUSH_LABEL(ret, lfin);
5639
5640 if (!popped) PUSH_INSN(ret, location, swap);
5641 PUSH_INSN(ret, location, pop);
5642}
5643
5648static void
5649pm_compile_constant_path_operator_write_node(rb_iseq_t *iseq, const pm_constant_path_operator_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
5650{
5651 const pm_node_location_t location = *node_location;
5652 const pm_constant_path_node_t *target = node->target;
5653
5654 ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
5655 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5656
5657 if (target->parent) {
5658 PM_COMPILE_NOT_POPPED(target->parent);
5659 }
5660 else {
5661 PUSH_INSN1(ret, location, putobject, rb_cObject);
5662 }
5663
5664 PUSH_INSN(ret, location, dup);
5665 PUSH_INSN1(ret, location, putobject, Qtrue);
5666 PUSH_INSN1(ret, location, getconstant, name);
5667
5668 if (shareability != 0) {
5669 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5670 }
5671 else {
5672 PM_COMPILE_NOT_POPPED(node->value);
5673 }
5674
5675 PUSH_CALL(ret, location, method_id, INT2FIX(1));
5676 PUSH_INSN(ret, location, swap);
5677
5678 if (!popped) {
5679 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5680 PUSH_INSN(ret, location, swap);
5681 }
5682
5683 PUSH_INSN1(ret, location, setconstant, name);
5684}
5685
5692#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))
5693
5698static inline void
5699pm_compile_scope_node(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped)
5700{
5701 const pm_node_location_t location = *node_location;
5702 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
5703
5704 pm_constant_id_list_t *locals = &scope_node->locals;
5705 pm_parameters_node_t *parameters_node = NULL;
5706 pm_node_list_t *keywords_list = NULL;
5707 pm_node_list_t *optionals_list = NULL;
5708 pm_node_list_t *posts_list = NULL;
5709 pm_node_list_t *requireds_list = NULL;
5710 pm_node_list_t *block_locals = NULL;
5711 bool trailing_comma = false;
5712
5713 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
5714 PUSH_TRACE(ret, RUBY_EVENT_CLASS);
5715 }
5716
5717 if (scope_node->parameters != NULL) {
5718 switch (PM_NODE_TYPE(scope_node->parameters)) {
5720 pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
5721 parameters_node = cast->parameters;
5722 block_locals = &cast->locals;
5723
5724 if (parameters_node) {
5725 if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
5726 trailing_comma = true;
5727 }
5728 }
5729 break;
5730 }
5731 case PM_PARAMETERS_NODE: {
5732 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
5733 break;
5734 }
5736 uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
5737 body->param.lead_num = maximum;
5738 body->param.flags.ambiguous_param0 = maximum == 1;
5739 break;
5740 }
5742 body->param.lead_num = 1;
5743 body->param.flags.ambiguous_param0 = true;
5744 break;
5745 default:
5746 rb_bug("Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
5747 }
5748 }
5749
5750 struct rb_iseq_param_keyword *keyword = NULL;
5751
5752 if (parameters_node) {
5753 optionals_list = &parameters_node->optionals;
5754 requireds_list = &parameters_node->requireds;
5755 keywords_list = &parameters_node->keywords;
5756 posts_list = &parameters_node->posts;
5757 }
5758 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))) {
5759 body->param.opt_num = 0;
5760 }
5761 else {
5762 body->param.lead_num = 0;
5763 body->param.opt_num = 0;
5764 }
5765
5766 //********STEP 1**********
5767 // Goal: calculate the table size for the locals, accounting for
5768 // hidden variables and multi target nodes
5769 size_t locals_size = locals->size;
5770
5771 // Index lookup table buffer size is only the number of the locals
5772 st_table *index_lookup_table = st_init_numtable();
5773
5774 int table_size = (int) locals_size;
5775
5776 // For nodes have a hidden iteration variable. We add that to the local
5777 // table size here.
5778 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
5779
5780 if (keywords_list && keywords_list->size) {
5781 table_size++;
5782 }
5783
5784 if (requireds_list) {
5785 for (size_t i = 0; i < requireds_list->size; i++) {
5786 // For each MultiTargetNode, we're going to have one
5787 // additional anonymous local not represented in the locals table
5788 // We want to account for this in our table size
5789 pm_node_t *required = requireds_list->nodes[i];
5790 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
5791 table_size++;
5792 }
5793 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
5795 table_size++;
5796 }
5797 }
5798 }
5799 }
5800
5801 // If we have the `it` implicit local variable, we need to account for
5802 // it in the local table size.
5803 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
5804 table_size++;
5805 }
5806
5807 // Ensure there is enough room in the local table for any
5808 // parameters that have been repeated
5809 // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
5810 // ^^^^^^^^^^^^
5811 if (optionals_list && optionals_list->size) {
5812 for (size_t i = 0; i < optionals_list->size; i++) {
5813 pm_node_t * node = optionals_list->nodes[i];
5815 table_size++;
5816 }
5817 }
5818 }
5819
5820 // If we have an anonymous "rest" node, we'll need to increase the local
5821 // table size to take it in to account.
5822 // def m(foo, *, bar)
5823 // ^
5824 if (parameters_node) {
5825 if (parameters_node->rest) {
5826 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
5827 if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
5828 table_size++;
5829 }
5830 }
5831 }
5832
5833 // def foo(_, **_); _; end
5834 // ^^^
5835 if (parameters_node->keyword_rest) {
5836 // def foo(...); end
5837 // ^^^
5838 // When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
5839 // we need to leave space for 4 locals: *, **, &, ...
5841 // Only optimize specifically methods like this: `foo(...)`
5842 if (requireds_list->size == 0 && optionals_list->size == 0 && keywords_list->size == 0) {
5843 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
5844 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
5845 table_size += 1;
5846 }
5847 else {
5848 table_size += 4;
5849 }
5850 }
5851 else {
5852 const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
5853
5854 // If it's anonymous or repeated, then we need to allocate stack space
5855 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
5856 table_size++;
5857 }
5858 }
5859 }
5860 }
5861
5862 if (posts_list) {
5863 for (size_t i = 0; i < posts_list->size; i++) {
5864 // For each MultiTargetNode, we're going to have one
5865 // additional anonymous local not represented in the locals table
5866 // We want to account for this in our table size
5867 pm_node_t *required = posts_list->nodes[i];
5869 table_size++;
5870 }
5871 }
5872 }
5873
5874 if (keywords_list && keywords_list->size) {
5875 for (size_t i = 0; i < keywords_list->size; i++) {
5876 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
5877 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
5878 table_size++;
5879 }
5880 }
5881 }
5882
5883 if (parameters_node && parameters_node->block) {
5884 const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
5885
5886 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
5887 table_size++;
5888 }
5889 }
5890
5891 // We can create local_table_for_iseq with the correct size
5892 VALUE idtmp = 0;
5893 rb_ast_id_table_t *local_table_for_iseq = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
5894 local_table_for_iseq->size = table_size;
5895
5896 //********END OF STEP 1**********
5897
5898 //********STEP 2**********
5899 // Goal: populate iv index table as well as local table, keeping the
5900 // layout of the local table consistent with the layout of the
5901 // stack when calling the method
5902 //
5903 // Do a first pass on all of the parameters, setting their values in
5904 // the local_table_for_iseq, _except_ for Multis who get a hidden
5905 // variable in this step, and will get their names inserted in step 3
5906
5907 // local_index is a cursor that keeps track of the current
5908 // index into local_table_for_iseq. The local table is actually a list,
5909 // and the order of that list must match the order of the items pushed
5910 // on the stack. We need to take in to account things pushed on the
5911 // stack that _might not have a name_ (for example array destructuring).
5912 // This index helps us know which item we're dealing with and also give
5913 // those anonymous items temporary names (as below)
5914 int local_index = 0;
5915
5916 // Here we figure out local table indices and insert them in to the
5917 // index lookup table and local tables.
5918 //
5919 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
5920 // ^^^^^^^^^^^^^
5921 if (requireds_list && requireds_list->size) {
5922 for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
5923 ID local;
5924
5925 // For each MultiTargetNode, we're going to have one additional
5926 // anonymous local not represented in the locals table. We want
5927 // to account for this in our table size.
5928 pm_node_t *required = requireds_list->nodes[i];
5929
5930 switch (PM_NODE_TYPE(required)) {
5931 case PM_MULTI_TARGET_NODE: {
5932 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
5933 // ^^^^^^^^^^
5934 local = rb_make_temporary_id(local_index);
5935 local_table_for_iseq->ids[local_index] = local;
5936 break;
5937 }
5939 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
5940 // ^
5941 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
5942
5944 ID local = pm_constant_id_lookup(scope_node, param->name);
5945 local_table_for_iseq->ids[local_index] = local;
5946 }
5947 else {
5948 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
5949 }
5950
5951 break;
5952 }
5953 default:
5954 rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
5955 }
5956 }
5957
5958 body->param.lead_num = (int) requireds_list->size;
5959 body->param.flags.has_lead = true;
5960 }
5961
5962 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
5963 ID local = rb_make_temporary_id(local_index);
5964 local_table_for_iseq->ids[local_index++] = local;
5965 }
5966
5967 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
5968 // ^^^^^
5969 if (optionals_list && optionals_list->size) {
5970 body->param.opt_num = (int) optionals_list->size;
5971 body->param.flags.has_opt = true;
5972
5973 for (size_t i = 0; i < optionals_list->size; i++, local_index++) {
5974 pm_node_t * node = optionals_list->nodes[i];
5975 pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
5976
5978 ID local = pm_constant_id_lookup(scope_node, name);
5979 local_table_for_iseq->ids[local_index] = local;
5980 }
5981 else {
5982 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
5983 }
5984 }
5985 }
5986
5987 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
5988 // ^^
5989 if (parameters_node && parameters_node->rest) {
5990 body->param.rest_start = local_index;
5991
5992 // If there's a trailing comma, we'll have an implicit rest node,
5993 // and we don't want it to impact the rest variables on param
5994 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
5995 body->param.flags.has_rest = true;
5996 RUBY_ASSERT(body->param.rest_start != -1);
5997
5998 pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
5999
6000 if (name) {
6001 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6002 // ^^
6004 ID local = pm_constant_id_lookup(scope_node, name);
6005 local_table_for_iseq->ids[local_index] = local;
6006 }
6007 else {
6008 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6009 }
6010 }
6011 else {
6012 // def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
6013 // ^
6014 body->param.flags.anon_rest = true;
6015 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6016 }
6017
6018 local_index++;
6019 }
6020 }
6021
6022 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6023 // ^^^^^^^^^^^^^
6024 if (posts_list && posts_list->size) {
6025 body->param.post_num = (int) posts_list->size;
6026 body->param.post_start = local_index;
6027 body->param.flags.has_post = true;
6028
6029 for (size_t i = 0; i < posts_list->size; i++, local_index++) {
6030 ID local;
6031
6032 // For each MultiTargetNode, we're going to have one additional
6033 // anonymous local not represented in the locals table. We want
6034 // to account for this in our table size.
6035 const pm_node_t *post_node = posts_list->nodes[i];
6036
6037 switch (PM_NODE_TYPE(post_node)) {
6038 case PM_MULTI_TARGET_NODE: {
6039 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6040 // ^^^^^^^^^^
6041 local = rb_make_temporary_id(local_index);
6042 local_table_for_iseq->ids[local_index] = local;
6043 break;
6044 }
6046 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6047 // ^
6048 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
6049
6051 ID local = pm_constant_id_lookup(scope_node, param->name);
6052 local_table_for_iseq->ids[local_index] = local;
6053 }
6054 else {
6055 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6056 }
6057 break;
6058 }
6059 default:
6060 rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6061 }
6062 }
6063 }
6064
6065 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6066 // ^^^^^^^^
6067 // Keywords create an internal variable on the parse tree
6068 if (keywords_list && keywords_list->size) {
6069 keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6070 keyword->num = (int) keywords_list->size;
6071
6072 const VALUE default_values = rb_ary_hidden_new(1);
6073 const VALUE complex_mark = rb_str_tmp_new(0);
6074
6075 for (size_t i = 0; i < keywords_list->size; i++) {
6076 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6077 pm_constant_id_t name;
6078
6079 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6080 // ^^
6081 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6082 name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
6083 keyword->required_num++;
6084 ID local = pm_constant_id_lookup(scope_node, name);
6085
6086 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6087 local_table_for_iseq->ids[local_index] = local;
6088 }
6089 else {
6090 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6091 }
6092 local_index++;
6093 }
6094 }
6095
6096 for (size_t i = 0; i < keywords_list->size; i++) {
6097 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6098 pm_constant_id_t name;
6099
6100 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6101 // ^^^^
6102 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6103 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6104
6105 pm_node_t *value = cast->value;
6106 name = cast->name;
6107
6108 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6109 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6110 }
6111 else {
6112 rb_ary_push(default_values, complex_mark);
6113 }
6114
6115 ID local = pm_constant_id_lookup(scope_node, name);
6116 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6117 local_table_for_iseq->ids[local_index] = local;
6118 }
6119 else {
6120 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6121 }
6122 local_index++;
6123 }
6124
6125 }
6126
6127 if (RARRAY_LEN(default_values)) {
6128 VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
6129
6130 for (int i = 0; i < RARRAY_LEN(default_values); i++) {
6131 VALUE dv = RARRAY_AREF(default_values, i);
6132 if (dv == complex_mark) dv = Qundef;
6133 RB_OBJ_WRITE(iseq, &dvs[i], dv);
6134 }
6135
6136 keyword->default_values = dvs;
6137 }
6138
6139 // Hidden local for keyword arguments
6140 keyword->bits_start = local_index;
6141 ID local = rb_make_temporary_id(local_index);
6142 local_table_for_iseq->ids[local_index] = local;
6143 local_index++;
6144
6145 body->param.keyword = keyword;
6146 body->param.flags.has_kw = true;
6147 }
6148
6149 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
6150 body->param.flags.ambiguous_param0 = true;
6151 }
6152
6153 if (parameters_node) {
6154 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6155 // ^^^
6156 if (parameters_node->keyword_rest) {
6157 switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
6159 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
6160 // ^^^^^
6161 body->param.flags.accepts_no_kwarg = true;
6162 break;
6163 }
6165 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6166 // ^^^
6167 const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
6168 if (!body->param.flags.has_kw) {
6169 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6170 }
6171
6172 keyword->rest_start = local_index;
6173 body->param.flags.has_kwrest = true;
6174
6175 pm_constant_id_t constant_id = kw_rest_node->name;
6176 if (constant_id) {
6178 ID local = pm_constant_id_lookup(scope_node, constant_id);
6179 local_table_for_iseq->ids[local_index] = local;
6180 }
6181 else {
6182 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6183 }
6184 }
6185 else {
6186 body->param.flags.anon_kwrest = true;
6187 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6188 }
6189
6190 local_index++;
6191 break;
6192 }
6194 // def foo(...)
6195 // ^^^
6196 if (!ISEQ_BODY(iseq)->param.flags.forwardable) {
6197 // Add the anonymous *
6198 body->param.rest_start = local_index;
6199 body->param.flags.has_rest = true;
6200 body->param.flags.anon_rest = true;
6201 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6202
6203 // Add the anonymous **
6204 RUBY_ASSERT(!body->param.flags.has_kw);
6205 body->param.flags.has_kw = false;
6206 body->param.flags.has_kwrest = true;
6207 body->param.flags.anon_kwrest = true;
6208 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6209 keyword->rest_start = local_index;
6210 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6211
6212 // Add the anonymous &
6213 body->param.block_start = local_index;
6214 body->param.flags.has_block = true;
6215 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6216 }
6217
6218 // Add the ...
6219 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6220 break;
6221 }
6222 default:
6223 rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
6224 }
6225 }
6226
6227 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6228 // ^^
6229 if (parameters_node->block) {
6230 body->param.block_start = local_index;
6231 body->param.flags.has_block = true;
6232 iseq_set_use_block(iseq);
6233
6234 pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
6235
6236 if (name) {
6238 ID local = pm_constant_id_lookup(scope_node, name);
6239 local_table_for_iseq->ids[local_index] = local;
6240 }
6241 else {
6242 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6243 }
6244 }
6245 else {
6246 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6247 }
6248
6249 local_index++;
6250 }
6251 }
6252
6253 //********END OF STEP 2**********
6254 // The local table is now consistent with expected
6255 // stack layout
6256
6257 // If there's only one required element in the parameters
6258 // CRuby needs to recognize it as an ambiguous parameter
6259
6260 //********STEP 3**********
6261 // Goal: fill in the names of the parameters in MultiTargetNodes
6262 //
6263 // Go through requireds again to set the multis
6264
6265 if (requireds_list && requireds_list->size) {
6266 for (size_t i = 0; i < requireds_list->size; i++) {
6267 // For each MultiTargetNode, we're going to have one
6268 // additional anonymous local not represented in the locals table
6269 // We want to account for this in our table size
6270 const pm_node_t *required = requireds_list->nodes[i];
6271
6272 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6273 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);
6274 }
6275 }
6276 }
6277
6278 // Go through posts again to set the multis
6279 if (posts_list && posts_list->size) {
6280 for (size_t i = 0; i < posts_list->size; i++) {
6281 // For each MultiTargetNode, we're going to have one
6282 // additional anonymous local not represented in the locals table
6283 // We want to account for this in our table size
6284 const pm_node_t *post = posts_list->nodes[i];
6285
6287 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);
6288 }
6289 }
6290 }
6291
6292 // Set any anonymous locals for the for node
6293 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6294 if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6295 body->param.lead_num++;
6296 }
6297 else {
6298 body->param.rest_start = local_index;
6299 body->param.flags.has_rest = true;
6300 }
6301
6302 ID local = rb_make_temporary_id(local_index);
6303 local_table_for_iseq->ids[local_index] = local;
6304 local_index++;
6305 }
6306
6307 // Fill in any NumberedParameters, if they exist
6308 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6309 int maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6310 RUBY_ASSERT(0 < maximum && maximum <= 9);
6311 for (int i = 0; i < maximum; i++, local_index++) {
6312 const uint8_t param_name[] = { '_', '1' + i };
6313 pm_constant_id_t constant_id = pm_constant_pool_find(&scope_node->parser->constant_pool, param_name, 2);
6314 RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
6315 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6316 }
6317 body->param.lead_num = maximum;
6318 body->param.flags.has_lead = true;
6319 }
6320
6321 //********END OF STEP 3**********
6322
6323 //********STEP 4**********
6324 // Goal: fill in the method body locals
6325 // To be explicit, these are the non-parameter locals
6326 // We fill in the block_locals, if they exist
6327 // lambda { |x; y| y }
6328 // ^
6329 if (block_locals && block_locals->size) {
6330 for (size_t i = 0; i < block_locals->size; i++, local_index++) {
6331 pm_constant_id_t constant_id = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
6332 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6333 }
6334 }
6335
6336 // Fill in any locals we missed
6337 if (scope_node->locals.size) {
6338 for (size_t i = 0; i < scope_node->locals.size; i++) {
6339 pm_constant_id_t constant_id = locals->ids[i];
6340 if (constant_id) {
6341 struct pm_local_table_insert_ctx ctx;
6342 ctx.scope_node = scope_node;
6343 ctx.local_table_for_iseq = local_table_for_iseq;
6344 ctx.local_index = local_index;
6345
6346 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6347
6348 local_index = ctx.local_index;
6349 }
6350 }
6351 }
6352
6353 //********END OF STEP 4**********
6354
6355 // We set the index_lookup_table on the scope node so we can
6356 // refer to the parameters correctly
6357 if (scope_node->index_lookup_table) {
6358 st_free_table(scope_node->index_lookup_table);
6359 }
6360 scope_node->index_lookup_table = index_lookup_table;
6361 iseq_calc_param_size(iseq);
6362
6363 if (ISEQ_BODY(iseq)->param.flags.forwardable) {
6364 // We're treating `...` as a parameter so that frame
6365 // pushing won't clobber it.
6366 ISEQ_BODY(iseq)->param.size += 1;
6367 }
6368
6369 // FIXME: args?
6370 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6371 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6372
6373 if (keyword != NULL) {
6374 size_t keyword_start_index = keyword->bits_start - keyword->num;
6375 keyword->table = (ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6376 }
6377
6378 //********STEP 5************
6379 // Goal: compile anything that needed to be compiled
6380 if (optionals_list && optionals_list->size) {
6381 LABEL **opt_table = (LABEL **) ALLOC_N(VALUE, optionals_list->size + 1);
6382 LABEL *label;
6383
6384 // TODO: Should we make an api for NEW_LABEL where you can pass
6385 // a pointer to the label it should fill out? We already
6386 // have a list of labels allocated above so it seems wasteful
6387 // to do the copies.
6388 for (size_t i = 0; i < optionals_list->size; i++) {
6389 label = NEW_LABEL(location.line);
6390 opt_table[i] = label;
6391 PUSH_LABEL(ret, label);
6392 pm_node_t *optional_node = optionals_list->nodes[i];
6393 PM_COMPILE_NOT_POPPED(optional_node);
6394 }
6395
6396 // Set the last label
6397 label = NEW_LABEL(location.line);
6398 opt_table[optionals_list->size] = label;
6399 PUSH_LABEL(ret, label);
6400
6401 body->param.opt_table = (const VALUE *) opt_table;
6402 }
6403
6404 if (keywords_list && keywords_list->size) {
6405 size_t optional_index = 0;
6406 for (size_t i = 0; i < keywords_list->size; i++) {
6407 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6408 pm_constant_id_t name;
6409
6410 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6412 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6413 // ^^^^
6414 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6415
6416 pm_node_t *value = cast->value;
6417 name = cast->name;
6418
6419 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6420 LABEL *end_label = NEW_LABEL(location.line);
6421
6422 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
6423 int kw_bits_idx = table_size - body->param.keyword->bits_start;
6424 PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
6425 PUSH_INSNL(ret, location, branchif, end_label);
6426 PM_COMPILE(value);
6427 PUSH_SETLOCAL(ret, location, index.index, index.level);
6428 PUSH_LABEL(ret, end_label);
6429 }
6430 optional_index++;
6431 break;
6432 }
6434 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6435 // ^^
6436 break;
6437 default:
6438 rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6439 }
6440 }
6441 }
6442
6443 if (requireds_list && requireds_list->size) {
6444 for (size_t i = 0; i < requireds_list->size; i++) {
6445 // For each MultiTargetNode, we're going to have one additional
6446 // anonymous local not represented in the locals table. We want
6447 // to account for this in our table size.
6448 const pm_node_t *required = requireds_list->nodes[i];
6449
6450 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6451 PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
6452 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
6453 }
6454 }
6455 }
6456
6457 if (posts_list && posts_list->size) {
6458 for (size_t i = 0; i < posts_list->size; i++) {
6459 // For each MultiTargetNode, we're going to have one additional
6460 // anonymous local not represented in the locals table. We want
6461 // to account for this in our table size.
6462 const pm_node_t *post = posts_list->nodes[i];
6463
6465 PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
6466 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
6467 }
6468 }
6469 }
6470
6471 switch (body->type) {
6472 case ISEQ_TYPE_PLAIN: {
6474
6476 pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6477
6478 break;
6479 }
6480 case ISEQ_TYPE_BLOCK: {
6481 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6482 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6483 const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = -1 };
6484
6485 start->rescued = LABEL_RESCUE_BEG;
6486 end->rescued = LABEL_RESCUE_END;
6487
6488 // For nodes automatically assign the iteration variable to whatever
6489 // index variable. We need to handle that write here because it has
6490 // to happen in the context of the block. Note that this happens
6491 // before the B_CALL tracepoint event.
6492 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6493 pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6494 }
6495
6496 PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
6497 PUSH_INSN(ret, block_location, nop);
6498 PUSH_LABEL(ret, start);
6499
6500 if (scope_node->body != NULL) {
6501 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6503 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
6504 PUSH_INSN1(ret, block_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6505
6506 // We create another ScopeNode from the statements within the PostExecutionNode
6507 pm_scope_node_t next_scope_node;
6508 pm_scope_node_init((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6509
6510 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
6511 pm_scope_node_destroy(&next_scope_node);
6512
6513 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
6514 break;
6515 }
6518 pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6519 break;
6520 }
6521 default:
6522 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6523 break;
6524 }
6525 }
6526 else {
6527 PUSH_INSN(ret, block_location, putnil);
6528 }
6529
6530 PUSH_LABEL(ret, end);
6531 PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
6532 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6533
6534 /* wide range catch handler must put at last */
6535 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6536 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6537 break;
6538 }
6539 case ISEQ_TYPE_ENSURE: {
6540 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6541 iseq_set_exception_local_table(iseq);
6542
6543 if (scope_node->body != NULL) {
6544 PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
6545 }
6546
6547 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6548 PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
6549 return;
6550 }
6551 case ISEQ_TYPE_METHOD: {
6552 ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
6553 PUSH_TRACE(ret, RUBY_EVENT_CALL);
6554
6555 if (scope_node->body) {
6556 PM_COMPILE((const pm_node_t *) scope_node->body);
6557 }
6558 else {
6559 PUSH_INSN(ret, location, putnil);
6560 }
6561
6562 ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
6563 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
6564
6565 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6566 break;
6567 }
6568 case ISEQ_TYPE_RESCUE: {
6569 iseq_set_exception_local_table(iseq);
6570 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
6571 LABEL *lab = NEW_LABEL(location.line);
6572 LABEL *rescue_end = NEW_LABEL(location.line);
6573 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
6574 PUSH_INSN1(ret, location, putobject, rb_eStandardError);
6575 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
6576 PUSH_INSNL(ret, location, branchif, lab);
6577 PUSH_INSNL(ret, location, jump, rescue_end);
6578 PUSH_LABEL(ret, lab);
6579 PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
6580 PM_COMPILE((const pm_node_t *) scope_node->body);
6581 PUSH_INSN(ret, location, leave);
6582 PUSH_LABEL(ret, rescue_end);
6583 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
6584 }
6585 else {
6586 PM_COMPILE((const pm_node_t *) scope_node->ast_node);
6587 }
6588 PUSH_INSN1(ret, location, throw, INT2FIX(0));
6589
6590 return;
6591 }
6592 default:
6593 if (scope_node->body) {
6594 PM_COMPILE((const pm_node_t *) scope_node->body);
6595 }
6596 else {
6597 PUSH_INSN(ret, location, putnil);
6598 }
6599 break;
6600 }
6601
6602 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6603 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
6604 PUSH_TRACE(ret, RUBY_EVENT_END);
6605 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
6606 }
6607
6608 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
6609 const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = -1 };
6610 PUSH_INSN(ret, location, leave);
6611 }
6612}
6613
6614static inline void
6615pm_compile_alias_global_variable_node(rb_iseq_t *iseq, const pm_alias_global_variable_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6616{
6617 // alias $foo $bar
6618 // ^^^^^^^^^^^^^^^
6619 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6620
6621 {
6622 const pm_location_t *name_loc = &node->new_name->location;
6623 VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
6624 PUSH_INSN1(ret, *location, putobject, operand);
6625 }
6626
6627 {
6628 const pm_location_t *name_loc = &node->old_name->location;
6629 VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
6630 PUSH_INSN1(ret, *location, putobject, operand);
6631 }
6632
6633 PUSH_SEND(ret, *location, id_core_set_variable_alias, INT2FIX(2));
6634 if (popped) PUSH_INSN(ret, *location, pop);
6635}
6636
6637static inline void
6638pm_compile_alias_method_node(rb_iseq_t *iseq, const pm_alias_method_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6639{
6640 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6641 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
6642 PM_COMPILE_NOT_POPPED(node->new_name);
6643 PM_COMPILE_NOT_POPPED(node->old_name);
6644
6645 PUSH_SEND(ret, *location, id_core_set_method_alias, INT2FIX(3));
6646 if (popped) PUSH_INSN(ret, *location, pop);
6647}
6648
6649static inline void
6650pm_compile_and_node(rb_iseq_t *iseq, const pm_and_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6651{
6652 LABEL *end_label = NEW_LABEL(location->line);
6653
6654 PM_COMPILE_NOT_POPPED(node->left);
6655 if (!popped) PUSH_INSN(ret, *location, dup);
6656 PUSH_INSNL(ret, *location, branchunless, end_label);
6657
6658 if (!popped) PUSH_INSN(ret, *location, pop);
6659 PM_COMPILE(node->right);
6660 PUSH_LABEL(ret, end_label);
6661}
6662
6663static inline void
6664pm_compile_array_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6665{
6666 // If every node in the array is static, then we can compile the entire
6667 // array now instead of later.
6668 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6669 // We're only going to compile this node if it's not popped. If it
6670 // is popped, then we know we don't need to do anything since it's
6671 // statically known.
6672 if (!popped) {
6673 if (elements->size) {
6674 VALUE value = pm_static_literal_value(iseq, node, scope_node);
6675 PUSH_INSN1(ret, *location, duparray, value);
6676 }
6677 else {
6678 PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
6679 }
6680 }
6681 return;
6682 }
6683
6684 // Here since we know there are possible side-effects inside the
6685 // array contents, we're going to build it entirely at runtime.
6686 // We'll do this by pushing all of the elements onto the stack and
6687 // then combining them with newarray.
6688 //
6689 // If this array is popped, then this serves only to ensure we enact
6690 // all side-effects (like method calls) that are contained within
6691 // the array contents.
6692 //
6693 // We treat all sequences of non-splat elements as their
6694 // own arrays, followed by a newarray, and then continually
6695 // concat the arrays with the SplatNode nodes.
6696 const int max_new_array_size = 0x100;
6697 const unsigned int min_tmp_array_size = 0x40;
6698
6699 int new_array_size = 0;
6700 bool first_chunk = true;
6701
6702 // This is an optimization wherein we keep track of whether or not
6703 // the previous element was a static literal. If it was, then we do
6704 // not attempt to check if we have a subarray that can be optimized.
6705 // If it was not, then we do check.
6706 bool static_literal = false;
6707
6708 // Either create a new array, or push to the existing array.
6709#define FLUSH_CHUNK \
6710 if (new_array_size) { \
6711 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
6712 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
6713 first_chunk = false; \
6714 new_array_size = 0; \
6715 }
6716
6717 for (size_t index = 0; index < elements->size; index++) {
6718 const pm_node_t *element = elements->nodes[index];
6719
6720 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
6721 FLUSH_CHUNK;
6722
6723 const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
6724 if (splat_element->expression) {
6725 PM_COMPILE_NOT_POPPED(splat_element->expression);
6726 }
6727 else {
6728 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
6729 PUSH_GETLOCAL(ret, *location, index.index, index.level);
6730 }
6731
6732 if (first_chunk) {
6733 // If this is the first element of the array then we
6734 // need to splatarray the elements into the list.
6735 PUSH_INSN1(ret, *location, splatarray, Qtrue);
6736 first_chunk = false;
6737 }
6738 else {
6739 PUSH_INSN(ret, *location, concattoarray);
6740 }
6741
6742 static_literal = false;
6743 }
6744 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
6745 if (new_array_size == 0 && first_chunk) {
6746 PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
6747 first_chunk = false;
6748 }
6749 else {
6750 FLUSH_CHUNK;
6751 }
6752
6753 // If we get here, then this is the last element of the
6754 // array/arguments, because it cannot be followed by
6755 // anything else without a syntax error. This looks like:
6756 //
6757 // [foo, bar, baz: qux]
6758 // ^^^^^^^^
6759 //
6760 // [foo, bar, **baz]
6761 // ^^^^^
6762 //
6763 const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) element;
6764 pm_compile_hash_elements(iseq, element, &keyword_hash->elements, 0, Qundef, false, ret, scope_node);
6765
6766 // This boolean controls the manner in which we push the
6767 // hash onto the array. If it's all keyword splats, then we
6768 // can use the very specialized pushtoarraykwsplat
6769 // instruction to check if it's empty before we push it.
6770 size_t splats = 0;
6771 while (splats < keyword_hash->elements.size && PM_NODE_TYPE_P(keyword_hash->elements.nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
6772
6773 if (keyword_hash->elements.size == splats) {
6774 PUSH_INSN(ret, *location, pushtoarraykwsplat);
6775 }
6776 else {
6777 new_array_size++;
6778 }
6779 }
6780 else if (
6781 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
6782 !PM_CONTAINER_P(element) &&
6783 !static_literal &&
6784 ((index + min_tmp_array_size) < elements->size)
6785 ) {
6786 // If we have a static literal, then there's the potential
6787 // to group a bunch of them together with a literal array
6788 // and then concat them together.
6789 size_t right_index = index + 1;
6790 while (
6791 right_index < elements->size &&
6792 PM_NODE_FLAG_P(elements->nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
6793 !PM_CONTAINER_P(elements->nodes[right_index])
6794 ) right_index++;
6795
6796 size_t tmp_array_size = right_index - index;
6797 if (tmp_array_size >= min_tmp_array_size) {
6798 VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
6799
6800 // Create the temporary array.
6801 for (; tmp_array_size; tmp_array_size--)
6802 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->nodes[index++], scope_node));
6803
6804 index--; // about to be incremented by for loop
6805 OBJ_FREEZE(tmp_array);
6806
6807 // Emit the optimized code.
6808 FLUSH_CHUNK;
6809 if (first_chunk) {
6810 PUSH_INSN1(ret, *location, duparray, tmp_array);
6811 first_chunk = false;
6812 }
6813 else {
6814 PUSH_INSN1(ret, *location, putobject, tmp_array);
6815 PUSH_INSN(ret, *location, concattoarray);
6816 }
6817 }
6818 else {
6819 PM_COMPILE_NOT_POPPED(element);
6820 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
6821 static_literal = true;
6822 }
6823 } else {
6824 PM_COMPILE_NOT_POPPED(element);
6825 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
6826 static_literal = false;
6827 }
6828 }
6829
6830 FLUSH_CHUNK;
6831 if (popped) PUSH_INSN(ret, *location, pop);
6832
6833#undef FLUSH_CHUNK
6834}
6835
6836static inline void
6837pm_compile_break_node(rb_iseq_t *iseq, const pm_break_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6838{
6839 unsigned long throw_flag = 0;
6840
6841 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
6842 /* while/until */
6843 LABEL *splabel = NEW_LABEL(0);
6844 PUSH_LABEL(ret, splabel);
6845 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
6846
6847 if (node->arguments != NULL) {
6848 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
6849 }
6850 else {
6851 PUSH_INSN(ret, *location, putnil);
6852 }
6853
6854 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
6855 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
6856 PUSH_ADJUST_RESTORE(ret, splabel);
6857 if (!popped) PUSH_INSN(ret, *location, putnil);
6858 }
6859 else {
6860 const rb_iseq_t *ip = iseq;
6861
6862 while (ip) {
6863 if (!ISEQ_COMPILE_DATA(ip)) {
6864 ip = 0;
6865 break;
6866 }
6867
6868 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
6869 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
6870 }
6871 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
6872 throw_flag = 0;
6873 }
6874 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
6875 COMPILE_ERROR(iseq, location->line, "Invalid break");
6876 return;
6877 }
6878 else {
6879 ip = ISEQ_BODY(ip)->parent_iseq;
6880 continue;
6881 }
6882
6883 /* escape from block */
6884 if (node->arguments != NULL) {
6885 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
6886 }
6887 else {
6888 PUSH_INSN(ret, *location, putnil);
6889 }
6890
6891 PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_BREAK));
6892 if (popped) PUSH_INSN(ret, *location, pop);
6893
6894 return;
6895 }
6896
6897 COMPILE_ERROR(iseq, location->line, "Invalid break");
6898 }
6899}
6900
6901static inline void
6902pm_compile_call_node(rb_iseq_t *iseq, const pm_call_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
6903{
6904 ID method_id = pm_constant_id_lookup(scope_node, node->name);
6905
6906 const pm_location_t *message_loc = &node->message_loc;
6907 if (message_loc->start == NULL) message_loc = &node->base.location;
6908
6909 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, node->base.node_id);
6910 const char *builtin_func;
6911
6912 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->receiver, method_id)) != NULL) {
6913 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
6914 return;
6915 }
6916
6917 LABEL *start = NEW_LABEL(location.line);
6918 if (node->block) PUSH_LABEL(ret, start);
6919
6920 switch (method_id) {
6921 case idUMinus: {
6922 if (pm_opt_str_freeze_p(iseq, node)) {
6923 VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
6924 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
6925 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
6926 if (popped) PUSH_INSN(ret, location, pop);
6927 return;
6928 }
6929 break;
6930 }
6931 case idFreeze: {
6932 if (pm_opt_str_freeze_p(iseq, node)) {
6933 VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
6934 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
6935 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
6936 if (popped) PUSH_INSN(ret, location, pop);
6937 return;
6938 }
6939 break;
6940 }
6941 case idAREF: {
6942 if (pm_opt_aref_with_p(iseq, node)) {
6943 const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0];
6944 VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
6945
6946 PM_COMPILE_NOT_POPPED(node->receiver);
6947
6948 const struct rb_callinfo *callinfo = new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE);
6949 PUSH_INSN2(ret, location, opt_aref_with, value, callinfo);
6950
6951 if (popped) {
6952 PUSH_INSN(ret, location, pop);
6953 }
6954
6955 return;
6956 }
6957 break;
6958 }
6959 case idASET: {
6960 if (pm_opt_aset_with_p(iseq, node)) {
6961 const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0];
6962 VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
6963
6964 PM_COMPILE_NOT_POPPED(node->receiver);
6965 PM_COMPILE_NOT_POPPED(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[1]);
6966
6967 if (!popped) {
6968 PUSH_INSN(ret, location, swap);
6969 PUSH_INSN1(ret, location, topn, INT2FIX(1));
6970 }
6971
6972 const struct rb_callinfo *callinfo = new_callinfo(iseq, idASET, 2, 0, NULL, FALSE);
6973 PUSH_INSN2(ret, location, opt_aset_with, value, callinfo);
6974 PUSH_INSN(ret, location, pop);
6975 return;
6976 }
6977 break;
6978 }
6979 }
6980
6981 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
6982 PUSH_INSN(ret, location, putnil);
6983 }
6984
6985 if (node->receiver == NULL) {
6986 PUSH_INSN(ret, location, putself);
6987 }
6988 else {
6989 if (method_id == idCall && PM_NODE_TYPE_P(node->receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
6990 const pm_local_variable_read_node_t *read_node_cast = (const pm_local_variable_read_node_t *) node->receiver;
6991 uint32_t node_id = node->receiver->node_id;
6992 int idx, level;
6993
6994 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
6995 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)));
6996 }
6997 else {
6998 PM_COMPILE_NOT_POPPED(node->receiver);
6999 }
7000 }
7001 else {
7002 PM_COMPILE_NOT_POPPED(node->receiver);
7003 }
7004 }
7005
7006 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7007 return;
7008}
7009
7010static inline void
7011pm_compile_call_operator_write_node(rb_iseq_t *iseq, const pm_call_operator_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7012{
7013 int flag = 0;
7014
7016 flag = VM_CALL_FCALL;
7017 }
7018
7019 PM_COMPILE_NOT_POPPED(node->receiver);
7020
7021 LABEL *safe_label = NULL;
7023 safe_label = NEW_LABEL(location->line);
7024 PUSH_INSN(ret, *location, dup);
7025 PUSH_INSNL(ret, *location, branchnil, safe_label);
7026 }
7027
7028 PUSH_INSN(ret, *location, dup);
7029
7030 ID id_read_name = pm_constant_id_lookup(scope_node, node->read_name);
7031 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name, INT2FIX(0), INT2FIX(flag));
7032
7033 PM_COMPILE_NOT_POPPED(node->value);
7034 ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
7035 PUSH_SEND(ret, *location, id_operator, INT2FIX(1));
7036
7037 if (!popped) {
7038 PUSH_INSN(ret, *location, swap);
7039 PUSH_INSN1(ret, *location, topn, INT2FIX(1));
7040 }
7041
7042 ID id_write_name = pm_constant_id_lookup(scope_node, node->write_name);
7043 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name, INT2FIX(1), INT2FIX(flag));
7044
7045 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7046 PUSH_INSN(ret, *location, pop);
7047 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7048}
7049
7066static VALUE
7067pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
7068{
7069 VALUE key = Qundef;
7070
7071 switch (PM_NODE_TYPE(node)) {
7072 case PM_FLOAT_NODE: {
7073 key = pm_static_literal_value(iseq, node, scope_node);
7074 double intptr;
7075
7076 if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
7077 key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
7078 }
7079
7080 break;
7081 }
7082 case PM_FALSE_NODE:
7083 case PM_INTEGER_NODE:
7084 case PM_NIL_NODE:
7087 case PM_SYMBOL_NODE:
7088 case PM_TRUE_NODE:
7089 key = pm_static_literal_value(iseq, node, scope_node);
7090 break;
7091 case PM_STRING_NODE: {
7092 const pm_string_node_t *cast = (const pm_string_node_t *) node;
7093 key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
7094 break;
7095 }
7096 default:
7097 return Qundef;
7098 }
7099
7100 if (NIL_P(rb_hash_lookup(dispatch, key))) {
7101 rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
7102 }
7103
7104 return dispatch;
7105}
7106
7110static inline void
7111pm_compile_case_node(rb_iseq_t *iseq, const pm_case_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7112{
7113 const pm_parser_t *parser = scope_node->parser;
7114 const pm_node_location_t location = *node_location;
7115 const pm_node_list_t *conditions = &cast->conditions;
7116
7117 // This is the anchor that we will compile the conditions of the various
7118 // `when` nodes into. If a match is found, they will need to jump into
7119 // the body_seq anchor to the correct spot.
7120 DECL_ANCHOR(cond_seq);
7121
7122 // This is the anchor that we will compile the bodies of the various
7123 // `when` nodes into. We'll make sure that the clauses that are compiled
7124 // jump into the correct spots within this anchor.
7125 DECL_ANCHOR(body_seq);
7126
7127 // This is the label where all of the when clauses will jump to if they
7128 // have matched and are done executing their bodies.
7129 LABEL *end_label = NEW_LABEL(location.line);
7130
7131 // If we have a predicate on this case statement, then it's going to
7132 // compare all of the various when clauses to the predicate. If we
7133 // don't, then it's basically an if-elsif-else chain.
7134 if (cast->predicate == NULL) {
7135 // Establish branch coverage for the case node.
7136 VALUE branches = Qfalse;
7137 rb_code_location_t case_location = { 0 };
7138 int branch_id = 0;
7139
7140 if (PM_BRANCH_COVERAGE_P(iseq)) {
7141 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
7142 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
7143 }
7144
7145 // Loop through each clauses in the case node and compile each of
7146 // the conditions within them into cond_seq. If they match, they
7147 // should jump into their respective bodies in body_seq.
7148 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
7149 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
7150 const pm_node_list_t *conditions = &clause->conditions;
7151
7152 int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
7153 LABEL *label = NEW_LABEL(clause_lineno);
7154 PUSH_LABEL(body_seq, label);
7155
7156 // Establish branch coverage for the when clause.
7157 if (PM_BRANCH_COVERAGE_P(iseq)) {
7158 rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
7159 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
7160 }
7161
7162 if (clause->statements != NULL) {
7163 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7164 }
7165 else if (!popped) {
7166 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7167 }
7168
7169 PUSH_INSNL(body_seq, location, jump, end_label);
7170
7171 // Compile each of the conditions for the when clause into the
7172 // cond_seq. Each one should have a unique condition and should
7173 // jump to the subsequent one if it doesn't match.
7174 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
7175 const pm_node_t *condition = conditions->nodes[condition_index];
7176
7177 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7178 pm_node_location_t cond_location = PM_NODE_START_LOCATION(parser, condition);
7179 PUSH_INSN(cond_seq, cond_location, putnil);
7180 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7181 PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7182 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7183 }
7184 else {
7185 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7186 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
7187 PUSH_LABEL(cond_seq, next_label);
7188 }
7189 }
7190 }
7191
7192 // Establish branch coverage for the else clause (implicit or
7193 // explicit).
7194 if (PM_BRANCH_COVERAGE_P(iseq)) {
7195 rb_code_location_t branch_location;
7196
7197 if (cast->else_clause == NULL) {
7198 branch_location = case_location;
7199 } else if (cast->else_clause->statements == NULL) {
7200 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause);
7201 } else {
7202 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause->statements);
7203 }
7204
7205 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7206 }
7207
7208 // Compile the else clause if there is one.
7209 if (cast->else_clause != NULL) {
7210 pm_compile_node(iseq, (const pm_node_t *) cast->else_clause, cond_seq, popped, scope_node);
7211 }
7212 else if (!popped) {
7213 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7214 }
7215
7216 // Finally, jump to the end label if none of the other conditions
7217 // have matched.
7218 PUSH_INSNL(cond_seq, location, jump, end_label);
7219 PUSH_SEQ(ret, cond_seq);
7220 }
7221 else {
7222 // Establish branch coverage for the case node.
7223 VALUE branches = Qfalse;
7224 rb_code_location_t case_location = { 0 };
7225 int branch_id = 0;
7226
7227 if (PM_BRANCH_COVERAGE_P(iseq)) {
7228 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
7229 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
7230 }
7231
7232 // This is the label where everything will fall into if none of the
7233 // conditions matched.
7234 LABEL *else_label = NEW_LABEL(location.line);
7235
7236 // It's possible for us to speed up the case node by using a
7237 // dispatch hash. This is a hash that maps the conditions of the
7238 // various when clauses to the labels of their bodies. If we can
7239 // compile the conditions into a hash key, then we can use a hash
7240 // lookup to jump directly to the correct when clause body.
7241 VALUE dispatch = Qundef;
7242 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7243 dispatch = rb_hash_new();
7244 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7245 }
7246
7247 // We're going to loop through each of the conditions in the case
7248 // node and compile each of their contents into both the cond_seq
7249 // and the body_seq. Each condition will use its own label to jump
7250 // from its conditions into its body.
7251 //
7252 // Note that none of the code in the loop below should be adding
7253 // anything to ret, as we're going to be laying out the entire case
7254 // node instructions later.
7255 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
7256 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
7257 pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (const pm_node_t *) clause);
7258
7259 const pm_node_list_t *conditions = &clause->conditions;
7260 LABEL *label = NEW_LABEL(clause_location.line);
7261
7262 // Compile each of the conditions for the when clause into the
7263 // cond_seq. Each one should have a unique comparison that then
7264 // jumps into the body if it matches.
7265 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
7266 const pm_node_t *condition = conditions->nodes[condition_index];
7267 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7268
7269 // If we haven't already abandoned the optimization, then
7270 // we're going to try to compile the condition into the
7271 // dispatch hash.
7272 if (dispatch != Qundef) {
7273 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7274 }
7275
7276 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7277 PUSH_INSN(cond_seq, condition_location, dup);
7278 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7279 PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7280 }
7281 else {
7282 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7283 const pm_string_node_t *string = (const pm_string_node_t *) condition;
7284 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
7285 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7286 }
7287 else {
7288 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7289 }
7290
7291 PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
7292 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7293 }
7294
7295 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7296 }
7297
7298 // Now, add the label to the body and compile the body of the
7299 // when clause. This involves popping the predicate, compiling
7300 // the statements to be executed, and then compiling a jump to
7301 // the end of the case node.
7302 PUSH_LABEL(body_seq, label);
7303 PUSH_INSN(body_seq, clause_location, pop);
7304
7305 // Establish branch coverage for the when clause.
7306 if (PM_BRANCH_COVERAGE_P(iseq)) {
7307 rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
7308 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
7309 }
7310
7311 if (clause->statements != NULL) {
7312 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7313 }
7314 else if (!popped) {
7315 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7316 }
7317
7318 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7319 }
7320
7321 // Now that we have compiled the conditions and the bodies of the
7322 // various when clauses, we can compile the predicate, lay out the
7323 // conditions, compile the fallback subsequent if there is one, and
7324 // finally put in the bodies of the when clauses.
7325 PM_COMPILE_NOT_POPPED(cast->predicate);
7326
7327 // If we have a dispatch hash, then we'll use it here to create the
7328 // optimization.
7329 if (dispatch != Qundef) {
7330 PUSH_INSN(ret, location, dup);
7331 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7332 LABEL_REF(else_label);
7333 }
7334
7335 PUSH_SEQ(ret, cond_seq);
7336
7337 // Compile either the explicit else clause or an implicit else
7338 // clause.
7339 PUSH_LABEL(ret, else_label);
7340
7341 if (cast->else_clause != NULL) {
7342 pm_node_location_t else_location = PM_NODE_START_LOCATION(parser, cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
7343 PUSH_INSN(ret, else_location, pop);
7344
7345 // Establish branch coverage for the else clause.
7346 if (PM_BRANCH_COVERAGE_P(iseq)) {
7347 rb_code_location_t branch_location = pm_code_location(scope_node, cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
7348 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7349 }
7350
7351 PM_COMPILE((const pm_node_t *) cast->else_clause);
7352 PUSH_INSNL(ret, else_location, jump, end_label);
7353 }
7354 else {
7355 PUSH_INSN(ret, location, pop);
7356
7357 // Establish branch coverage for the implicit else clause.
7358 if (PM_BRANCH_COVERAGE_P(iseq)) {
7359 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
7360 }
7361
7362 if (!popped) PUSH_INSN(ret, location, putnil);
7363 PUSH_INSNL(ret, location, jump, end_label);
7364 }
7365 }
7366
7367 PUSH_SEQ(ret, body_seq);
7368 PUSH_LABEL(ret, end_label);
7369}
7370
7371static inline void
7372pm_compile_case_match_node(rb_iseq_t *iseq, const pm_case_match_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7373{
7374 // This is the anchor that we will compile the bodies of the various
7375 // `in` nodes into. We'll make sure that the patterns that are compiled
7376 // jump into the correct spots within this anchor.
7377 DECL_ANCHOR(body_seq);
7378
7379 // This is the anchor that we will compile the patterns of the various
7380 // `in` nodes into. If a match is found, they will need to jump into the
7381 // body_seq anchor to the correct spot.
7382 DECL_ANCHOR(cond_seq);
7383
7384 // This label is used to indicate the end of the entire node. It is
7385 // jumped to after the entire stack is cleaned up.
7386 LABEL *end_label = NEW_LABEL(location->line);
7387
7388 // This label is used as the fallback for the case match. If no match is
7389 // found, then we jump to this label. This is either an `else` clause or
7390 // an error handler.
7391 LABEL *else_label = NEW_LABEL(location->line);
7392
7393 // We're going to use this to uniquely identify each branch so that we
7394 // can track coverage information.
7395 rb_code_location_t case_location = { 0 };
7396 VALUE branches = Qfalse;
7397 int branch_id = 0;
7398
7399 if (PM_BRANCH_COVERAGE_P(iseq)) {
7400 case_location = pm_code_location(scope_node, (const pm_node_t *) node);
7401 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location, "case");
7402 }
7403
7404 // If there is only one pattern, then the behavior changes a bit. It
7405 // effectively gets treated as a match required node (this is how it is
7406 // represented in the other parser).
7407 bool in_single_pattern = node->else_clause == NULL && node->conditions.size == 1;
7408
7409 // First, we're going to push a bunch of stuff onto the stack that is
7410 // going to serve as our scratch space.
7411 if (in_single_pattern) {
7412 PUSH_INSN(ret, *location, putnil); // key error key
7413 PUSH_INSN(ret, *location, putnil); // key error matchee
7414 PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
7415 PUSH_INSN(ret, *location, putnil); // error string
7416 }
7417
7418 // Now we're going to compile the value to match against.
7419 PUSH_INSN(ret, *location, putnil); // deconstruct cache
7420 PM_COMPILE_NOT_POPPED(node->predicate);
7421
7422 // Next, we'll loop through every in clause and compile its body into
7423 // the body_seq anchor and its pattern into the cond_seq anchor. We'll
7424 // make sure the pattern knows how to jump correctly into the body if it
7425 // finds a match.
7426 for (size_t index = 0; index < node->conditions.size; index++) {
7427 const pm_node_t *condition = node->conditions.nodes[index];
7429
7430 const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
7431 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7432 const pm_node_location_t pattern_location = PM_NODE_START_LOCATION(scope_node->parser, in_node->pattern);
7433
7434 if (branch_id) {
7435 PUSH_INSN(body_seq, in_location, putnil);
7436 }
7437
7438 LABEL *body_label = NEW_LABEL(in_location.line);
7439 PUSH_LABEL(body_seq, body_label);
7440 PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
7441
7442 // Establish branch coverage for the in clause.
7443 if (PM_BRANCH_COVERAGE_P(iseq)) {
7444 rb_code_location_t branch_location = pm_code_location(scope_node, in_node->statements != NULL ? ((const pm_node_t *) in_node->statements) : ((const pm_node_t *) in_node));
7445 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
7446 }
7447
7448 if (in_node->statements != NULL) {
7449 PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
7450 }
7451 else if (!popped) {
7452 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7453 }
7454
7455 PUSH_INSNL(body_seq, in_location, jump, end_label);
7456 LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
7457
7458 PUSH_INSN(cond_seq, pattern_location, dup);
7459 pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
7460 PUSH_LABEL(cond_seq, next_pattern_label);
7461 LABEL_UNREMOVABLE(next_pattern_label);
7462 }
7463
7464 if (node->else_clause != NULL) {
7465 // If we have an `else` clause, then this becomes our fallback (and
7466 // there is no need to compile in code to potentially raise an
7467 // error).
7468 const pm_else_node_t *else_node = node->else_clause;
7469
7470 PUSH_LABEL(cond_seq, else_label);
7471 PUSH_INSN(cond_seq, *location, pop);
7472 PUSH_INSN(cond_seq, *location, pop);
7473
7474 // Establish branch coverage for the else clause.
7475 if (PM_BRANCH_COVERAGE_P(iseq)) {
7476 rb_code_location_t branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : ((const pm_node_t *) else_node));
7477 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7478 }
7479
7480 PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
7481 PUSH_INSNL(cond_seq, *location, jump, end_label);
7482 PUSH_INSN(cond_seq, *location, putnil);
7483 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7484 }
7485 else {
7486 // Otherwise, if we do not have an `else` clause, we will compile in
7487 // the code to handle raising an appropriate error.
7488 PUSH_LABEL(cond_seq, else_label);
7489
7490 // Establish branch coverage for the implicit else clause.
7491 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
7492
7493 if (in_single_pattern) {
7494 pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, cond_seq, end_label, popped);
7495 }
7496 else {
7497 PUSH_INSN1(cond_seq, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7498 PUSH_INSN1(cond_seq, *location, putobject, rb_eNoMatchingPatternError);
7499 PUSH_INSN1(cond_seq, *location, topn, INT2FIX(2));
7500 PUSH_SEND(cond_seq, *location, id_core_raise, INT2FIX(2));
7501
7502 PUSH_INSN1(cond_seq, *location, adjuststack, INT2FIX(3));
7503 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7504 PUSH_INSNL(cond_seq, *location, jump, end_label);
7505 PUSH_INSN1(cond_seq, *location, dupn, INT2FIX(1));
7506 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7507 }
7508 }
7509
7510 // At the end of all of this compilation, we will add the code for the
7511 // conditions first, then the various bodies, then mark the end of the
7512 // entire sequence with the end label.
7513 PUSH_SEQ(ret, cond_seq);
7514 PUSH_SEQ(ret, body_seq);
7515 PUSH_LABEL(ret, end_label);
7516}
7517
7518static inline void
7519pm_compile_forwarding_super_node(rb_iseq_t *iseq, const pm_forwarding_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7520{
7521 const rb_iseq_t *block = NULL;
7522 const rb_iseq_t *previous_block = NULL;
7523 LABEL *retry_label = NULL;
7524 LABEL *retry_end_l = NULL;
7525
7526 if (node->block != NULL) {
7527 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7528 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7529
7530 retry_label = NEW_LABEL(location->line);
7531 retry_end_l = NEW_LABEL(location->line);
7532
7533 PUSH_LABEL(ret, retry_label);
7534 }
7535 else {
7536 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7537 }
7538
7539 PUSH_INSN(ret, *location, putself);
7540 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7541
7542 if (node->block != NULL) {
7543 pm_scope_node_t next_scope_node;
7544 pm_scope_node_init((const pm_node_t *) node->block, &next_scope_node, scope_node);
7545
7546 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
7547 pm_scope_node_destroy(&next_scope_node);
7548 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
7549 }
7550
7551 DECL_ANCHOR(args);
7552
7553 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
7554 const rb_iseq_t *local_iseq = body->local_iseq;
7555 const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(local_iseq);
7556
7557 int argc = 0;
7558 int depth = get_lvar_level(iseq);
7559
7560 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
7561 flag |= VM_CALL_FORWARDING;
7562 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
7563 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
7564
7565 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
7566 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
7567
7568 if (popped) PUSH_INSN(ret, *location, pop);
7569 if (node->block) {
7570 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7571 }
7572 return;
7573 }
7574
7575 if (local_body->param.flags.has_lead) {
7576 /* required arguments */
7577 for (int i = 0; i < local_body->param.lead_num; i++) {
7578 int idx = local_body->local_table_size - i;
7579 PUSH_GETLOCAL(args, *location, idx, depth);
7580 }
7581 argc += local_body->param.lead_num;
7582 }
7583
7584 if (local_body->param.flags.has_opt) {
7585 /* optional arguments */
7586 for (int j = 0; j < local_body->param.opt_num; j++) {
7587 int idx = local_body->local_table_size - (argc + j);
7588 PUSH_GETLOCAL(args, *location, idx, depth);
7589 }
7590 argc += local_body->param.opt_num;
7591 }
7592
7593 if (local_body->param.flags.has_rest) {
7594 /* rest argument */
7595 int idx = local_body->local_table_size - local_body->param.rest_start;
7596 PUSH_GETLOCAL(args, *location, idx, depth);
7597 PUSH_INSN1(args, *location, splatarray, Qfalse);
7598
7599 argc = local_body->param.rest_start + 1;
7600 flag |= VM_CALL_ARGS_SPLAT;
7601 }
7602
7603 if (local_body->param.flags.has_post) {
7604 /* post arguments */
7605 int post_len = local_body->param.post_num;
7606 int post_start = local_body->param.post_start;
7607
7608 int j = 0;
7609 for (; j < post_len; j++) {
7610 int idx = local_body->local_table_size - (post_start + j);
7611 PUSH_GETLOCAL(args, *location, idx, depth);
7612 }
7613
7614 if (local_body->param.flags.has_rest) {
7615 // argc remains unchanged from rest branch
7616 PUSH_INSN1(args, *location, newarray, INT2FIX(j));
7617 PUSH_INSN(args, *location, concatarray);
7618 }
7619 else {
7620 argc = post_len + post_start;
7621 }
7622 }
7623
7624 const struct rb_iseq_param_keyword *const local_keyword = local_body->param.keyword;
7625 if (local_body->param.flags.has_kw) {
7626 int local_size = local_body->local_table_size;
7627 argc++;
7628
7629 PUSH_INSN1(args, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7630
7631 if (local_body->param.flags.has_kwrest) {
7632 int idx = local_body->local_table_size - local_keyword->rest_start;
7633 PUSH_GETLOCAL(args, *location, idx, depth);
7634 RUBY_ASSERT(local_keyword->num > 0);
7635 PUSH_SEND(args, *location, rb_intern("dup"), INT2FIX(0));
7636 }
7637 else {
7638 PUSH_INSN1(args, *location, newhash, INT2FIX(0));
7639 }
7640 int i = 0;
7641 for (; i < local_keyword->num; ++i) {
7642 ID id = local_keyword->table[i];
7643 int idx = local_size - get_local_var_idx(local_iseq, id);
7644
7645 {
7646 VALUE operand = ID2SYM(id);
7647 PUSH_INSN1(args, *location, putobject, operand);
7648 }
7649
7650 PUSH_GETLOCAL(args, *location, idx, depth);
7651 }
7652
7653 PUSH_SEND(args, *location, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
7654 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
7655 }
7656 else if (local_body->param.flags.has_kwrest) {
7657 int idx = local_body->local_table_size - local_keyword->rest_start;
7658 PUSH_GETLOCAL(args, *location, idx, depth);
7659 argc++;
7660 flag |= VM_CALL_KW_SPLAT;
7661 }
7662
7663 PUSH_SEQ(ret, args);
7664
7665 {
7666 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
7667 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
7668 }
7669
7670 if (node->block != NULL) {
7671 pm_compile_retry_end_label(iseq, ret, retry_end_l);
7672 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
7673 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7674 }
7675
7676 if (popped) PUSH_INSN(ret, *location, pop);
7677}
7678
7679static inline void
7680pm_compile_match_required_node(rb_iseq_t *iseq, const pm_match_required_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7681{
7682 LABEL *matched_label = NEW_LABEL(location->line);
7683 LABEL *unmatched_label = NEW_LABEL(location->line);
7684 LABEL *done_label = NEW_LABEL(location->line);
7685
7686 // First, we're going to push a bunch of stuff onto the stack that is
7687 // going to serve as our scratch space.
7688 PUSH_INSN(ret, *location, putnil); // key error key
7689 PUSH_INSN(ret, *location, putnil); // key error matchee
7690 PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
7691 PUSH_INSN(ret, *location, putnil); // error string
7692 PUSH_INSN(ret, *location, putnil); // deconstruct cache
7693
7694 // Next we're going to compile the value expression such that it's on
7695 // the stack.
7696 PM_COMPILE_NOT_POPPED(node->value);
7697
7698 // Here we'll dup it so that it can be used for comparison, but also be
7699 // used for error handling.
7700 PUSH_INSN(ret, *location, dup);
7701
7702 // Next we'll compile the pattern. We indicate to the pm_compile_pattern
7703 // function that this is the only pattern that will be matched against
7704 // through the in_single_pattern parameter. We also indicate that the
7705 // value to compare against is 2 slots from the top of the stack (the
7706 // base_index parameter).
7707 pm_compile_pattern(iseq, scope_node, node->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
7708
7709 // If the pattern did not match the value, then we're going to compile
7710 // in our error handler code. This will determine which error to raise
7711 // and raise it.
7712 PUSH_LABEL(ret, unmatched_label);
7713 pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, ret, done_label, popped);
7714
7715 // If the pattern did match, we'll clean up the values we've pushed onto
7716 // the stack and then push nil onto the stack if it's not popped.
7717 PUSH_LABEL(ret, matched_label);
7718 PUSH_INSN1(ret, *location, adjuststack, INT2FIX(6));
7719 if (!popped) PUSH_INSN(ret, *location, putnil);
7720 PUSH_INSNL(ret, *location, jump, done_label);
7721
7722 PUSH_LABEL(ret, done_label);
7723}
7724
7725static inline void
7726pm_compile_match_write_node(rb_iseq_t *iseq, const pm_match_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7727{
7728 LABEL *fail_label = NEW_LABEL(location->line);
7729 LABEL *end_label = NEW_LABEL(location->line);
7730
7731 // First, we'll compile the call so that all of its instructions are
7732 // present. Then we'll compile all of the local variable targets.
7733 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->call);
7734
7735 // Now, check if the match was successful. If it was, then we'll
7736 // continue on and assign local variables. Otherwise we'll skip over the
7737 // assignment code.
7738 {
7739 VALUE operand = rb_id2sym(idBACKREF);
7740 PUSH_INSN1(ret, *location, getglobal, operand);
7741 }
7742
7743 PUSH_INSN(ret, *location, dup);
7744 PUSH_INSNL(ret, *location, branchunless, fail_label);
7745
7746 // If there's only a single local variable target, we can skip some of
7747 // the bookkeeping, so we'll put a special branch here.
7748 size_t targets_count = node->targets.size;
7749
7750 if (targets_count == 1) {
7751 const pm_node_t *target = node->targets.nodes[0];
7753
7754 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
7755 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
7756
7757 {
7758 VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
7759 PUSH_INSN1(ret, *location, putobject, operand);
7760 }
7761
7762 PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
7763 PUSH_LABEL(ret, fail_label);
7764 PUSH_SETLOCAL(ret, *location, index.index, index.level);
7765 if (popped) PUSH_INSN(ret, *location, pop);
7766 return;
7767 }
7768
7769 DECL_ANCHOR(fail_anchor);
7770
7771 // Otherwise there is more than one local variable target, so we'll need
7772 // to do some bookkeeping.
7773 for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
7774 const pm_node_t *target = node->targets.nodes[targets_index];
7776
7777 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
7778 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
7779
7780 if (((size_t) targets_index) < (targets_count - 1)) {
7781 PUSH_INSN(ret, *location, dup);
7782 }
7783
7784 {
7785 VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
7786 PUSH_INSN1(ret, *location, putobject, operand);
7787 }
7788
7789 PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
7790 PUSH_SETLOCAL(ret, *location, index.index, index.level);
7791
7792 PUSH_INSN(fail_anchor, *location, putnil);
7793 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
7794 }
7795
7796 // Since we matched successfully, now we'll jump to the end.
7797 PUSH_INSNL(ret, *location, jump, end_label);
7798
7799 // In the case that the match failed, we'll loop through each local
7800 // variable target and set all of them to `nil`.
7801 PUSH_LABEL(ret, fail_label);
7802 PUSH_INSN(ret, *location, pop);
7803 PUSH_SEQ(ret, fail_anchor);
7804
7805 // Finally, we can push the end label for either case.
7806 PUSH_LABEL(ret, end_label);
7807 if (popped) PUSH_INSN(ret, *location, pop);
7808}
7809
7810static inline void
7811pm_compile_next_node(rb_iseq_t *iseq, const pm_next_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7812{
7813 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7814 LABEL *splabel = NEW_LABEL(0);
7815 PUSH_LABEL(ret, splabel);
7816
7817 if (node->arguments) {
7818 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
7819 }
7820 else {
7821 PUSH_INSN(ret, *location, putnil);
7822 }
7823 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7824
7825 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7826 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7827
7828 PUSH_ADJUST_RESTORE(ret, splabel);
7829 if (!popped) PUSH_INSN(ret, *location, putnil);
7830 }
7831 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
7832 LABEL *splabel = NEW_LABEL(0);
7833
7834 PUSH_LABEL(ret, splabel);
7835 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
7836
7837 if (node->arguments != NULL) {
7838 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
7839 }
7840 else {
7841 PUSH_INSN(ret, *location, putnil);
7842 }
7843
7844 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7845 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7846 PUSH_ADJUST_RESTORE(ret, splabel);
7847 splabel->unremovable = FALSE;
7848
7849 if (!popped) PUSH_INSN(ret, *location, putnil);
7850 }
7851 else {
7852 const rb_iseq_t *ip = iseq;
7853 unsigned long throw_flag = 0;
7854
7855 while (ip) {
7856 if (!ISEQ_COMPILE_DATA(ip)) {
7857 ip = 0;
7858 break;
7859 }
7860
7861 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7862 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7863 /* while loop */
7864 break;
7865 }
7866 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
7867 break;
7868 }
7869 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
7870 COMPILE_ERROR(iseq, location->line, "Invalid next");
7871 return;
7872 }
7873
7874 ip = ISEQ_BODY(ip)->parent_iseq;
7875 }
7876
7877 if (ip != 0) {
7878 if (node->arguments) {
7879 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
7880 }
7881 else {
7882 PUSH_INSN(ret, *location, putnil);
7883 }
7884
7885 PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_NEXT));
7886 if (popped) PUSH_INSN(ret, *location, pop);
7887 }
7888 else {
7889 COMPILE_ERROR(iseq, location->line, "Invalid next");
7890 }
7891 }
7892}
7893
7894static inline void
7895pm_compile_redo_node(rb_iseq_t *iseq, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7896{
7897 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
7898 LABEL *splabel = NEW_LABEL(0);
7899
7900 PUSH_LABEL(ret, splabel);
7901 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7902 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7903
7904 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
7905 PUSH_ADJUST_RESTORE(ret, splabel);
7906 if (!popped) PUSH_INSN(ret, *location, putnil);
7907 }
7908 else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
7909 LABEL *splabel = NEW_LABEL(0);
7910
7911 PUSH_LABEL(ret, splabel);
7912 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7913 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
7914
7915 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7916 PUSH_ADJUST_RESTORE(ret, splabel);
7917 if (!popped) PUSH_INSN(ret, *location, putnil);
7918 }
7919 else {
7920 const rb_iseq_t *ip = iseq;
7921
7922 while (ip) {
7923 if (!ISEQ_COMPILE_DATA(ip)) {
7924 ip = 0;
7925 break;
7926 }
7927
7928 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7929 break;
7930 }
7931 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
7932 break;
7933 }
7934 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
7935 COMPILE_ERROR(iseq, location->line, "Invalid redo");
7936 return;
7937 }
7938
7939 ip = ISEQ_BODY(ip)->parent_iseq;
7940 }
7941
7942 if (ip != 0) {
7943 PUSH_INSN(ret, *location, putnil);
7944 PUSH_INSN1(ret, *location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
7945 if (popped) PUSH_INSN(ret, *location, pop);
7946 }
7947 else {
7948 COMPILE_ERROR(iseq, location->line, "Invalid redo");
7949 }
7950 }
7951}
7952
7953static inline void
7954pm_compile_rescue_node(rb_iseq_t *iseq, const pm_rescue_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
7955{
7956 iseq_set_exception_local_table(iseq);
7957
7958 // First, establish the labels that we need to be able to jump to within
7959 // this compilation block.
7960 LABEL *exception_match_label = NEW_LABEL(location->line);
7961 LABEL *rescue_end_label = NEW_LABEL(location->line);
7962
7963 // Next, compile each of the exceptions that we're going to be
7964 // handling. For each one, we'll add instructions to check if the
7965 // exception matches the raised one, and if it does then jump to the
7966 // exception_match_label label. Otherwise it will fall through to the
7967 // subsequent check. If there are no exceptions, we'll only check
7968 // StandardError.
7969 const pm_node_list_t *exceptions = &node->exceptions;
7970
7971 if (exceptions->size > 0) {
7972 for (size_t index = 0; index < exceptions->size; index++) {
7973 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
7974 PM_COMPILE(exceptions->nodes[index]);
7975 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
7976 if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
7977 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
7978 }
7979 PUSH_INSN1(ret, *location, checkmatch, INT2FIX(checkmatch_flags));
7980 PUSH_INSNL(ret, *location, branchif, exception_match_label);
7981 }
7982 }
7983 else {
7984 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
7985 PUSH_INSN1(ret, *location, putobject, rb_eStandardError);
7986 PUSH_INSN1(ret, *location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7987 PUSH_INSNL(ret, *location, branchif, exception_match_label);
7988 }
7989
7990 // If none of the exceptions that we are matching against matched, then
7991 // we'll jump straight to the rescue_end_label label.
7992 PUSH_INSNL(ret, *location, jump, rescue_end_label);
7993
7994 // Here we have the exception_match_label, which is where the
7995 // control-flow goes in the case that one of the exceptions matched.
7996 // Here we will compile the instructions to handle the exception.
7997 PUSH_LABEL(ret, exception_match_label);
7998 PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
7999
8000 // If we have a reference to the exception, then we'll compile the write
8001 // into the instruction sequence. This can look quite different
8002 // depending on the kind of write being performed.
8003 if (node->reference) {
8004 DECL_ANCHOR(writes);
8005 DECL_ANCHOR(cleanup);
8006
8007 pm_compile_target_node(iseq, node->reference, ret, writes, cleanup, scope_node, NULL);
8008 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8009
8010 PUSH_SEQ(ret, writes);
8011 PUSH_SEQ(ret, cleanup);
8012 }
8013
8014 // If we have statements to execute, we'll compile them here. Otherwise
8015 // we'll push nil onto the stack.
8016 if (node->statements != NULL) {
8017 // We'll temporarily remove the end_label location from the iseq
8018 // when compiling the statements so that next/redo statements
8019 // inside the body will throw to the correct place instead of
8020 // jumping straight to the end of this iseq
8021 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8022 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8023
8024 PM_COMPILE((const pm_node_t *) node->statements);
8025
8026 // Now restore the end_label
8027 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8028 }
8029 else {
8030 PUSH_INSN(ret, *location, putnil);
8031 }
8032
8033 PUSH_INSN(ret, *location, leave);
8034
8035 // Here we'll insert the rescue_end_label label, which is jumped to if
8036 // none of the exceptions matched. It will cause the control-flow to
8037 // either jump to the next rescue clause or it will fall through to the
8038 // subsequent instruction returning the raised error.
8039 PUSH_LABEL(ret, rescue_end_label);
8040 if (node->subsequent != NULL) {
8041 PM_COMPILE((const pm_node_t *) node->subsequent);
8042 }
8043 else {
8044 PUSH_GETLOCAL(ret, *location, 1, 0);
8045 }
8046}
8047
8048static inline void
8049pm_compile_return_node(rb_iseq_t *iseq, const pm_return_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
8050{
8051 const pm_arguments_node_t *arguments = node->arguments;
8052 enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
8053 LABEL *splabel = 0;
8054
8055 const rb_iseq_t *parent_iseq = iseq;
8056 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8057 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8058 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq)) break;
8059 parent_type = ISEQ_BODY(parent_iseq)->type;
8060 }
8061
8062 switch (parent_type) {
8063 case ISEQ_TYPE_TOP:
8064 case ISEQ_TYPE_MAIN:
8065 if (arguments) {
8066 rb_warn("argument of top-level return is ignored");
8067 }
8068 if (parent_iseq == iseq) {
8069 type = ISEQ_TYPE_METHOD;
8070 }
8071 break;
8072 default:
8073 break;
8074 }
8075
8076 if (type == ISEQ_TYPE_METHOD) {
8077 splabel = NEW_LABEL(0);
8078 PUSH_LABEL(ret, splabel);
8079 PUSH_ADJUST(ret, *location, 0);
8080 }
8081
8082 if (arguments != NULL) {
8083 PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
8084 }
8085 else {
8086 PUSH_INSN(ret, *location, putnil);
8087 }
8088
8089 if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8090 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8091 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
8092 PUSH_INSN(ret, *location, leave);
8093 PUSH_ADJUST_RESTORE(ret, splabel);
8094 if (!popped) PUSH_INSN(ret, *location, putnil);
8095 }
8096 else {
8097 PUSH_INSN1(ret, *location, throw, INT2FIX(TAG_RETURN));
8098 if (popped) PUSH_INSN(ret, *location, pop);
8099 }
8100}
8101
8102static inline void
8103pm_compile_super_node(rb_iseq_t *iseq, const pm_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
8104{
8105 DECL_ANCHOR(args);
8106
8107 LABEL *retry_label = NEW_LABEL(location->line);
8108 LABEL *retry_end_l = NEW_LABEL(location->line);
8109
8110 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8111 const rb_iseq_t *current_block;
8112 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8113
8114 PUSH_LABEL(ret, retry_label);
8115 PUSH_INSN(ret, *location, putself);
8116
8117 int flags = 0;
8118 struct rb_callinfo_kwarg *keywords = NULL;
8119 int argc = pm_setup_args(node->arguments, node->block, &flags, &keywords, iseq, ret, scope_node, location);
8120 bool is_forwardable = (node->arguments != NULL) && PM_NODE_FLAG_P(node->arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8121 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8122
8123 if (node->block && PM_NODE_TYPE_P(node->block, PM_BLOCK_NODE)) {
8124 pm_scope_node_t next_scope_node;
8125 pm_scope_node_init(node->block, &next_scope_node, scope_node);
8126
8127 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
8128 pm_scope_node_destroy(&next_scope_node);
8129 }
8130
8131 if (!node->block) {
8132 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8133 }
8134
8135 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8136 PUSH_INSN(args, *location, splatkw);
8137 }
8138
8139 PUSH_SEQ(ret, args);
8140 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
8141 flags |= VM_CALL_FORWARDING;
8142
8143 {
8144 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8145 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8146 }
8147 }
8148 else {
8149 {
8150 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8151 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8152 }
8153
8154 }
8155
8156 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8157
8158 if (popped) PUSH_INSN(ret, *location, pop);
8159 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8160 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8161}
8162
8163static inline void
8164pm_compile_yield_node(rb_iseq_t *iseq, const pm_yield_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
8165{
8166 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
8167 case ISEQ_TYPE_TOP:
8168 case ISEQ_TYPE_MAIN:
8169 case ISEQ_TYPE_CLASS:
8170 COMPILE_ERROR(iseq, location->line, "Invalid yield");
8171 return;
8172 default: /* valid */;
8173 }
8174
8175 int argc = 0;
8176 int flags = 0;
8177 struct rb_callinfo_kwarg *keywords = NULL;
8178
8179 if (node->arguments) {
8180 argc = pm_setup_args(node->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8181 }
8182
8183 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8184 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8185
8186 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8187 if (popped) PUSH_INSN(ret, *location, pop);
8188
8189 int level = 0;
8190 for (const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8191 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8192 }
8193
8194 if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
8195}
8196
8207static void
8208pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
8209{
8210 const pm_parser_t *parser = scope_node->parser;
8211 const pm_node_location_t location = PM_NODE_START_LOCATION(parser, node);
8212 int lineno = (int) location.line;
8213
8214 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)) {
8215 // If this node is a begin node and it has empty statements and also
8216 // has a rescue clause, then the other parser considers it as
8217 // starting on the same line as the rescue, as opposed to the
8218 // location of the begin keyword. We replicate that behavior here.
8219 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((const pm_begin_node_t *) node)->rescue_clause).line;
8220 }
8221
8222 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8223 // If this node has the newline flag set and it is on a new line
8224 // from the previous nodes that have been compiled for this ISEQ,
8225 // then we need to emit a newline event.
8226 int event = RUBY_EVENT_LINE;
8227
8228 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8229 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8230 event |= RUBY_EVENT_COVERAGE_LINE;
8231 }
8232 PUSH_TRACE(ret, event);
8233 }
8234
8235 switch (PM_NODE_TYPE(node)) {
8237 // alias $foo $bar
8238 // ^^^^^^^^^^^^^^^
8239 pm_compile_alias_global_variable_node(iseq, (const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
8240 return;
8242 // alias foo bar
8243 // ^^^^^^^^^^^^^
8244 pm_compile_alias_method_node(iseq, (const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8245 return;
8246 case PM_AND_NODE:
8247 // a and b
8248 // ^^^^^^^
8249 pm_compile_and_node(iseq, (const pm_and_node_t *) node, &location, ret, popped, scope_node);
8250 return;
8251 case PM_ARGUMENTS_NODE: {
8252 // break foo
8253 // ^^^
8254 //
8255 // These are ArgumentsNodes that are not compiled directly by their
8256 // parent call nodes, used in the cases of NextNodes, ReturnNodes, and
8257 // BreakNodes. They can create an array like ArrayNode.
8258 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
8259 const pm_node_list_t *elements = &cast->arguments;
8260
8261 if (elements->size == 1) {
8262 // If we are only returning a single element through one of the jump
8263 // nodes, then we will only compile that node directly.
8264 PM_COMPILE(elements->nodes[0]);
8265 }
8266 else {
8267 pm_compile_array_node(iseq, (const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8268 }
8269 return;
8270 }
8271 case PM_ARRAY_NODE: {
8272 // [foo, bar, baz]
8273 // ^^^^^^^^^^^^^^^
8274 const pm_array_node_t *cast = (const pm_array_node_t *) node;
8275 pm_compile_array_node(iseq, (const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8276 return;
8277 }
8278 case PM_ASSOC_NODE: {
8279 // { foo: 1 }
8280 // ^^^^^^
8281 //
8282 // foo(bar: 1)
8283 // ^^^^^^
8284 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
8285
8286 PM_COMPILE(cast->key);
8287 PM_COMPILE(cast->value);
8288
8289 return;
8290 }
8291 case PM_ASSOC_SPLAT_NODE: {
8292 // { **foo }
8293 // ^^^^^
8294 //
8295 // def foo(**); bar(**); end
8296 // ^^
8297 const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
8298
8299 if (cast->value != NULL) {
8300 PM_COMPILE(cast->value);
8301 }
8302 else if (!popped) {
8303 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8304 PUSH_GETLOCAL(ret, location, index.index, index.level);
8305 }
8306
8307 return;
8308 }
8310 // $+
8311 // ^^
8312 if (!popped) {
8313 // Since a back reference is `$<char>`, ruby represents the ID as the
8314 // an rb_intern on the value after the `$`.
8315 char *char_ptr = (char *)(node->location.start) + 1;
8316 ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
8317 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref_val);
8318 }
8319 return;
8320 }
8321 case PM_BEGIN_NODE: {
8322 // begin end
8323 // ^^^^^^^^^
8324 const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
8325
8326 if (cast->ensure_clause) {
8327 // Compiling the ensure clause will compile the rescue clause (if
8328 // there is one), which will compile the begin statements.
8329 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8330 }
8331 else if (cast->rescue_clause) {
8332 // Compiling rescue will compile begin statements (if applicable).
8333 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8334 }
8335 else {
8336 // If there is neither ensure or rescue, the just compile the
8337 // statements.
8338 if (cast->statements != NULL) {
8339 PM_COMPILE((const pm_node_t *) cast->statements);
8340 }
8341 else if (!popped) {
8342 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8343 }
8344 }
8345 return;
8346 }
8348 // foo(&bar)
8349 // ^^^^
8350 const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
8351
8352 if (cast->expression != NULL) {
8353 PM_COMPILE(cast->expression);
8354 }
8355 else {
8356 // If there's no expression, this must be block forwarding.
8357 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8358 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
8359 }
8360 return;
8361 }
8362 case PM_BREAK_NODE:
8363 // break
8364 // ^^^^^
8365 //
8366 // break foo
8367 // ^^^^^^^^^
8368 pm_compile_break_node(iseq, (const pm_break_node_t *) node, &location, ret, popped, scope_node);
8369 return;
8370 case PM_CALL_NODE:
8371 // foo
8372 // ^^^
8373 //
8374 // foo.bar
8375 // ^^^^^^^
8376 //
8377 // foo.bar() {}
8378 // ^^^^^^^^^^^^
8379 pm_compile_call_node(iseq, (const pm_call_node_t *) node, ret, popped, scope_node);
8380 return;
8382 // foo.bar &&= baz
8383 // ^^^^^^^^^^^^^^^
8384 const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
8385 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);
8386 return;
8387 }
8388 case PM_CALL_OR_WRITE_NODE: {
8389 // foo.bar ||= baz
8390 // ^^^^^^^^^^^^^^^
8391 const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
8392 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);
8393 return;
8394 }
8396 // foo.bar += baz
8397 // ^^^^^^^^^^^^^^^
8398 //
8399 // Call operator writes occur when you have a call node on the left-hand
8400 // side of a write operator that is not `=`. As an example,
8401 // `foo.bar *= 1`. This breaks down to caching the receiver on the
8402 // stack and then performing three method calls, one to read the value,
8403 // one to compute the result, and one to write the result back to the
8404 // receiver.
8405 pm_compile_call_operator_write_node(iseq, (const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
8406 return;
8407 case PM_CASE_NODE:
8408 // case foo; when bar; end
8409 // ^^^^^^^^^^^^^^^^^^^^^^^
8410 pm_compile_case_node(iseq, (const pm_case_node_t *) node, &location, ret, popped, scope_node);
8411 return;
8412 case PM_CASE_MATCH_NODE:
8413 // case foo; in bar; end
8414 // ^^^^^^^^^^^^^^^^^^^^^
8415 //
8416 // If you use the `case` keyword to create a case match node, it will
8417 // match against all of the `in` clauses until it finds one that
8418 // matches. If it doesn't find one, it can optionally fall back to an
8419 // `else` clause. If none is present and a match wasn't found, it will
8420 // raise an appropriate error.
8421 pm_compile_case_match_node(iseq, (const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8422 return;
8423 case PM_CLASS_NODE: {
8424 // class Foo; end
8425 // ^^^^^^^^^^^^^^
8426 const pm_class_node_t *cast = (const pm_class_node_t *) node;
8427
8428 ID class_id = pm_constant_id_lookup(scope_node, cast->name);
8429 VALUE class_name = rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(class_id)));
8430
8431 pm_scope_node_t next_scope_node;
8432 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
8433
8434 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.line);
8435 pm_scope_node_destroy(&next_scope_node);
8436
8437 // TODO: Once we merge constant path nodes correctly, fix this flag
8438 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8439 (cast->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8440 pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
8441
8442 if (cast->superclass) {
8443 PM_COMPILE_NOT_POPPED(cast->superclass);
8444 }
8445 else {
8446 PUSH_INSN(ret, location, putnil);
8447 }
8448
8449 {
8450 VALUE operand = ID2SYM(class_id);
8451 PUSH_INSN3(ret, location, defineclass, operand, class_iseq, INT2FIX(flags));
8452 }
8453 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
8454
8455 if (popped) PUSH_INSN(ret, location, pop);
8456 return;
8457 }
8459 // @@foo &&= bar
8460 // ^^^^^^^^^^^^^
8462 LABEL *end_label = NEW_LABEL(location.line);
8463
8464 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8465 VALUE name = ID2SYM(name_id);
8466
8467 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8468 if (!popped) PUSH_INSN(ret, location, dup);
8469
8470 PUSH_INSNL(ret, location, branchunless, end_label);
8471 if (!popped) PUSH_INSN(ret, location, pop);
8472
8473 PM_COMPILE_NOT_POPPED(cast->value);
8474 if (!popped) PUSH_INSN(ret, location, dup);
8475
8476 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8477 PUSH_LABEL(ret, end_label);
8478
8479 return;
8480 }
8482 // @@foo += bar
8483 // ^^^^^^^^^^^^
8485
8486 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8487 VALUE name = ID2SYM(name_id);
8488
8489 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8490 PM_COMPILE_NOT_POPPED(cast->value);
8491
8492 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
8493 int flags = VM_CALL_ARGS_SIMPLE;
8494 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
8495
8496 if (!popped) PUSH_INSN(ret, location, dup);
8497 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8498
8499 return;
8500 }
8502 // @@foo ||= bar
8503 // ^^^^^^^^^^^^^
8505 LABEL *end_label = NEW_LABEL(location.line);
8506 LABEL *start_label = NEW_LABEL(location.line);
8507
8508 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8509 VALUE name = ID2SYM(name_id);
8510
8511 PUSH_INSN(ret, location, putnil);
8512 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, Qtrue);
8513 PUSH_INSNL(ret, location, branchunless, start_label);
8514
8515 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8516 if (!popped) PUSH_INSN(ret, location, dup);
8517
8518 PUSH_INSNL(ret, location, branchif, end_label);
8519 if (!popped) PUSH_INSN(ret, location, pop);
8520
8521 PUSH_LABEL(ret, start_label);
8522 PM_COMPILE_NOT_POPPED(cast->value);
8523 if (!popped) PUSH_INSN(ret, location, dup);
8524
8525 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8526 PUSH_LABEL(ret, end_label);
8527
8528 return;
8529 }
8531 // @@foo
8532 // ^^^^^
8533 if (!popped) {
8535 ID name = pm_constant_id_lookup(scope_node, cast->name);
8536 PUSH_INSN2(ret, location, getclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
8537 }
8538 return;
8539 }
8541 // @@foo = 1
8542 // ^^^^^^^^^
8544 PM_COMPILE_NOT_POPPED(cast->value);
8545 if (!popped) PUSH_INSN(ret, location, dup);
8546
8547 ID name = pm_constant_id_lookup(scope_node, cast->name);
8548 PUSH_INSN2(ret, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
8549
8550 return;
8551 }
8552 case PM_CONSTANT_PATH_NODE: {
8553 // Foo::Bar
8554 // ^^^^^^^^
8555 VALUE parts;
8556
8557 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
8558 ISEQ_BODY(iseq)->ic_size++;
8559 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
8560 }
8561 else {
8562 DECL_ANCHOR(prefix);
8563 DECL_ANCHOR(body);
8564
8565 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
8566 if (LIST_INSN_SIZE_ZERO(prefix)) {
8567 PUSH_INSN(ret, location, putnil);
8568 }
8569 else {
8570 PUSH_SEQ(ret, prefix);
8571 }
8572
8573 PUSH_SEQ(ret, body);
8574 }
8575
8576 if (popped) PUSH_INSN(ret, location, pop);
8577 return;
8578 }
8580 // Foo::Bar &&= baz
8581 // ^^^^^^^^^^^^^^^^
8583 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8584 return;
8585 }
8587 // Foo::Bar ||= baz
8588 // ^^^^^^^^^^^^^^^^
8590 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8591 return;
8592 }
8594 // Foo::Bar += baz
8595 // ^^^^^^^^^^^^^^^
8597 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8598 return;
8599 }
8601 // Foo::Bar = 1
8602 // ^^^^^^^^^^^^
8604 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8605 return;
8606 }
8607 case PM_CONSTANT_READ_NODE: {
8608 // Foo
8609 // ^^^
8610 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
8611 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
8612
8613 pm_compile_constant_read(iseq, name, &cast->base.location, location.node_id, ret, scope_node);
8614 if (popped) PUSH_INSN(ret, location, pop);
8615
8616 return;
8617 }
8619 // Foo &&= bar
8620 // ^^^^^^^^^^^
8622 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8623 return;
8624 }
8626 // Foo ||= bar
8627 // ^^^^^^^^^^^
8628 const pm_constant_or_write_node_t *cast = (const pm_constant_or_write_node_t *) node;
8629 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8630 return;
8631 }
8633 // Foo += bar
8634 // ^^^^^^^^^^
8636 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8637 return;
8638 }
8640 // Foo = 1
8641 // ^^^^^^^
8642 const pm_constant_write_node_t *cast = (const pm_constant_write_node_t *) node;
8643 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8644 return;
8645 }
8646 case PM_DEF_NODE: {
8647 // def foo; end
8648 // ^^^^^^^^^^^^
8649 //
8650 // def self.foo; end
8651 // ^^^^^^^^^^^^^^^^^
8652 const pm_def_node_t *cast = (const pm_def_node_t *) node;
8653 ID method_name = pm_constant_id_lookup(scope_node, cast->name);
8654
8655 pm_scope_node_t next_scope_node;
8656 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
8657
8658 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.line);
8659 pm_scope_node_destroy(&next_scope_node);
8660
8661 if (cast->receiver) {
8662 PM_COMPILE_NOT_POPPED(cast->receiver);
8663 PUSH_INSN2(ret, location, definesmethod, ID2SYM(method_name), method_iseq);
8664 }
8665 else {
8666 PUSH_INSN2(ret, location, definemethod, ID2SYM(method_name), method_iseq);
8667 }
8668 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) method_iseq);
8669
8670 if (!popped) {
8671 PUSH_INSN1(ret, location, putobject, ID2SYM(method_name));
8672 }
8673
8674 return;
8675 }
8676 case PM_DEFINED_NODE: {
8677 // defined?(a)
8678 // ^^^^^^^^^^^
8679 const pm_defined_node_t *cast = (const pm_defined_node_t *) node;
8680 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, false);
8681 return;
8682 }
8684 // "foo #{bar}"
8685 // ^^^^^^
8687
8688 if (cast->statements != NULL) {
8689 PM_COMPILE((const pm_node_t *) (cast->statements));
8690 }
8691 else {
8692 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8693 }
8694
8695 if (popped) PUSH_INSN(ret, location, pop);
8696 return;
8697 }
8699 // "foo #@bar"
8700 // ^^^^^
8701 const pm_embedded_variable_node_t *cast = (const pm_embedded_variable_node_t *) node;
8702 PM_COMPILE(cast->variable);
8703 return;
8704 }
8705 case PM_FALSE_NODE: {
8706 // false
8707 // ^^^^^
8708 if (!popped) {
8709 PUSH_INSN1(ret, location, putobject, Qfalse);
8710 }
8711 return;
8712 }
8713 case PM_ENSURE_NODE: {
8714 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
8715
8716 if (cast->statements != NULL) {
8717 LABEL *start = NEW_LABEL(location.line);
8718 LABEL *end = NEW_LABEL(location.line);
8719 PUSH_LABEL(ret, start);
8720
8721 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8722 ISEQ_COMPILE_DATA(iseq)->end_label = end;
8723
8724 PM_COMPILE((const pm_node_t *) cast->statements);
8725 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8726 PUSH_LABEL(ret, end);
8727 }
8728
8729 return;
8730 }
8731 case PM_ELSE_NODE: {
8732 // if foo then bar else baz end
8733 // ^^^^^^^^^^^^
8734 const pm_else_node_t *cast = (const pm_else_node_t *) node;
8735
8736 if (cast->statements != NULL) {
8737 PM_COMPILE((const pm_node_t *) cast->statements);
8738 }
8739 else if (!popped) {
8740 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8741 }
8742
8743 return;
8744 }
8745 case PM_FLIP_FLOP_NODE: {
8746 // if foo .. bar; end
8747 // ^^^^^^^^^^
8748 const pm_flip_flop_node_t *cast = (const pm_flip_flop_node_t *) node;
8749
8750 LABEL *final_label = NEW_LABEL(location.line);
8751 LABEL *then_label = NEW_LABEL(location.line);
8752 LABEL *else_label = NEW_LABEL(location.line);
8753
8754 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.line, ret, popped, scope_node);
8755
8756 PUSH_LABEL(ret, then_label);
8757 PUSH_INSN1(ret, location, putobject, Qtrue);
8758 PUSH_INSNL(ret, location, jump, final_label);
8759 PUSH_LABEL(ret, else_label);
8760 PUSH_INSN1(ret, location, putobject, Qfalse);
8761 PUSH_LABEL(ret, final_label);
8762
8763 return;
8764 }
8765 case PM_FLOAT_NODE: {
8766 // 1.0
8767 // ^^^
8768 if (!popped) {
8769 VALUE operand = parse_float((const pm_float_node_t *) node);
8770 PUSH_INSN1(ret, location, putobject, operand);
8771 }
8772 return;
8773 }
8774 case PM_FOR_NODE: {
8775 // for foo in bar do end
8776 // ^^^^^^^^^^^^^^^^^^^^^
8777 const pm_for_node_t *cast = (const pm_for_node_t *) node;
8778
8779 LABEL *retry_label = NEW_LABEL(location.line);
8780 LABEL *retry_end_l = NEW_LABEL(location.line);
8781
8782 // First, compile the collection that we're going to be iterating over.
8783 PUSH_LABEL(ret, retry_label);
8784 PM_COMPILE_NOT_POPPED(cast->collection);
8785
8786 // Next, create the new scope that is going to contain the block that
8787 // will be passed to the each method.
8788 pm_scope_node_t next_scope_node;
8789 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
8790
8791 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
8792 pm_scope_node_destroy(&next_scope_node);
8793
8794 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8795 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
8796
8797 // Now, create the method call to each that will be used to iterate over
8798 // the collection, and pass the newly created iseq as the block.
8799 PUSH_SEND_WITH_BLOCK(ret, location, idEach, INT2FIX(0), child_iseq);
8800 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8801
8802 if (popped) PUSH_INSN(ret, location, pop);
8803 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
8804 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8805 return;
8806 }
8808 rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
8809 return;
8811 // super
8812 // ^^^^^
8813 //
8814 // super {}
8815 // ^^^^^^^^
8816 pm_compile_forwarding_super_node(iseq, (const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
8817 return;
8819 // $foo &&= bar
8820 // ^^^^^^^^^^^^
8822 LABEL *end_label = NEW_LABEL(location.line);
8823
8824 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
8825 PUSH_INSN1(ret, location, getglobal, name);
8826 if (!popped) PUSH_INSN(ret, location, dup);
8827
8828 PUSH_INSNL(ret, location, branchunless, end_label);
8829 if (!popped) PUSH_INSN(ret, location, pop);
8830
8831 PM_COMPILE_NOT_POPPED(cast->value);
8832 if (!popped) PUSH_INSN(ret, location, dup);
8833
8834 PUSH_INSN1(ret, location, setglobal, name);
8835 PUSH_LABEL(ret, end_label);
8836
8837 return;
8838 }
8840 // $foo += bar
8841 // ^^^^^^^^^^^
8843
8844 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
8845 PUSH_INSN1(ret, location, getglobal, name);
8846 PM_COMPILE_NOT_POPPED(cast->value);
8847
8848 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
8849 int flags = VM_CALL_ARGS_SIMPLE;
8850 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
8851
8852 if (!popped) PUSH_INSN(ret, location, dup);
8853 PUSH_INSN1(ret, location, setglobal, name);
8854
8855 return;
8856 }
8858 // $foo ||= bar
8859 // ^^^^^^^^^^^^
8861 LABEL *set_label = NEW_LABEL(location.line);
8862 LABEL *end_label = NEW_LABEL(location.line);
8863
8864 PUSH_INSN(ret, location, putnil);
8865 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
8866
8867 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, Qtrue);
8868 PUSH_INSNL(ret, location, branchunless, set_label);
8869
8870 PUSH_INSN1(ret, location, getglobal, name);
8871 if (!popped) PUSH_INSN(ret, location, dup);
8872
8873 PUSH_INSNL(ret, location, branchif, end_label);
8874 if (!popped) PUSH_INSN(ret, location, pop);
8875
8876 PUSH_LABEL(ret, set_label);
8877 PM_COMPILE_NOT_POPPED(cast->value);
8878 if (!popped) PUSH_INSN(ret, location, dup);
8879
8880 PUSH_INSN1(ret, location, setglobal, name);
8881 PUSH_LABEL(ret, end_label);
8882
8883 return;
8884 }
8886 // $foo
8887 // ^^^^
8889 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
8890
8891 PUSH_INSN1(ret, location, getglobal, name);
8892 if (popped) PUSH_INSN(ret, location, pop);
8893
8894 return;
8895 }
8897 // $foo = 1
8898 // ^^^^^^^^
8900 PM_COMPILE_NOT_POPPED(cast->value);
8901 if (!popped) PUSH_INSN(ret, location, dup);
8902
8903 ID name = pm_constant_id_lookup(scope_node, cast->name);
8904 PUSH_INSN1(ret, location, setglobal, ID2SYM(name));
8905
8906 return;
8907 }
8908 case PM_HASH_NODE: {
8909 // {}
8910 // ^^
8911 //
8912 // If every node in the hash is static, then we can compile the entire
8913 // hash now instead of later.
8914 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
8915 // We're only going to compile this node if it's not popped. If it
8916 // is popped, then we know we don't need to do anything since it's
8917 // statically known.
8918 if (!popped) {
8919 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
8920
8921 if (cast->elements.size == 0) {
8922 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
8923 }
8924 else {
8925 VALUE value = pm_static_literal_value(iseq, node, scope_node);
8926 PUSH_INSN1(ret, location, duphash, value);
8927 RB_OBJ_WRITTEN(iseq, Qundef, value);
8928 }
8929 }
8930 }
8931 else {
8932 // Here since we know there are possible side-effects inside the
8933 // hash contents, we're going to build it entirely at runtime. We'll
8934 // do this by pushing all of the key-value pairs onto the stack and
8935 // then combining them with newhash.
8936 //
8937 // If this hash is popped, then this serves only to ensure we enact
8938 // all side-effects (like method calls) that are contained within
8939 // the hash contents.
8940 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
8941 const pm_node_list_t *elements = &cast->elements;
8942
8943 if (popped) {
8944 // If this hash is popped, then we can iterate through each
8945 // element and compile it. The result of each compilation will
8946 // only include the side effects of the element itself.
8947 for (size_t index = 0; index < elements->size; index++) {
8948 PM_COMPILE_POPPED(elements->nodes[index]);
8949 }
8950 }
8951 else {
8952 pm_compile_hash_elements(iseq, node, elements, 0, Qundef, false, ret, scope_node);
8953 }
8954 }
8955
8956 return;
8957 }
8958 case PM_IF_NODE: {
8959 // if foo then bar end
8960 // ^^^^^^^^^^^^^^^^^^^
8961 //
8962 // bar if foo
8963 // ^^^^^^^^^^
8964 //
8965 // foo ? bar : baz
8966 // ^^^^^^^^^^^^^^^
8967 const pm_if_node_t *cast = (const pm_if_node_t *) node;
8968 pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
8969 return;
8970 }
8971 case PM_IMAGINARY_NODE: {
8972 // 1i
8973 // ^^
8974 if (!popped) {
8975 VALUE operand = parse_imaginary((const pm_imaginary_node_t *) node);
8976 PUSH_INSN1(ret, location, putobject, operand);
8977 }
8978 return;
8979 }
8980 case PM_IMPLICIT_NODE: {
8981 // Implicit nodes mark places in the syntax tree where explicit syntax
8982 // was omitted, but implied. For example,
8983 //
8984 // { foo: }
8985 //
8986 // In this case a method call/local variable read is implied by virtue
8987 // of the missing value. To compile these nodes, we simply compile the
8988 // value that is implied, which is helpfully supplied by the parser.
8989 const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
8990 PM_COMPILE(cast->value);
8991 return;
8992 }
8993 case PM_IN_NODE: {
8994 // In nodes are handled by the case match node directly, so we should
8995 // never end up hitting them through this path.
8996 rb_bug("Should not ever enter an in node directly");
8997 return;
8998 }
9000 // foo[bar] += baz
9001 // ^^^^^^^^^^^^^^^
9003 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9004 return;
9005 }
9007 // foo[bar] &&= baz
9008 // ^^^^^^^^^^^^^^^^
9009 const pm_index_and_write_node_t *cast = (const pm_index_and_write_node_t *) node;
9010 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
9011 return;
9012 }
9014 // foo[bar] ||= baz
9015 // ^^^^^^^^^^^^^^^^
9016 const pm_index_or_write_node_t *cast = (const pm_index_or_write_node_t *) node;
9017 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
9018 return;
9019 }
9021 // @foo &&= bar
9022 // ^^^^^^^^^^^^
9024 LABEL *end_label = NEW_LABEL(location.line);
9025
9026 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9027 VALUE name = ID2SYM(name_id);
9028
9029 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9030 if (!popped) PUSH_INSN(ret, location, dup);
9031
9032 PUSH_INSNL(ret, location, branchunless, end_label);
9033 if (!popped) PUSH_INSN(ret, location, pop);
9034
9035 PM_COMPILE_NOT_POPPED(cast->value);
9036 if (!popped) PUSH_INSN(ret, location, dup);
9037
9038 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9039 PUSH_LABEL(ret, end_label);
9040
9041 return;
9042 }
9044 // @foo += bar
9045 // ^^^^^^^^^^^
9047
9048 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9049 VALUE name = ID2SYM(name_id);
9050
9051 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9052 PM_COMPILE_NOT_POPPED(cast->value);
9053
9054 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
9055 int flags = VM_CALL_ARGS_SIMPLE;
9056 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
9057
9058 if (!popped) PUSH_INSN(ret, location, dup);
9059 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9060
9061 return;
9062 }
9064 // @foo ||= bar
9065 // ^^^^^^^^^^^^
9067 LABEL *end_label = NEW_LABEL(location.line);
9068
9069 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9070 VALUE name = ID2SYM(name_id);
9071
9072 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9073 if (!popped) PUSH_INSN(ret, location, dup);
9074
9075 PUSH_INSNL(ret, location, branchif, end_label);
9076 if (!popped) PUSH_INSN(ret, location, pop);
9077
9078 PM_COMPILE_NOT_POPPED(cast->value);
9079 if (!popped) PUSH_INSN(ret, location, dup);
9080
9081 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9082 PUSH_LABEL(ret, end_label);
9083
9084 return;
9085 }
9087 // @foo
9088 // ^^^^
9089 if (!popped) {
9091 ID name = pm_constant_id_lookup(scope_node, cast->name);
9092 PUSH_INSN2(ret, location, getinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
9093 }
9094 return;
9095 }
9097 // @foo = 1
9098 // ^^^^^^^^
9100 PM_COMPILE_NOT_POPPED(cast->value);
9101 if (!popped) PUSH_INSN(ret, location, dup);
9102
9103 ID name = pm_constant_id_lookup(scope_node, cast->name);
9104 PUSH_INSN2(ret, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
9105
9106 return;
9107 }
9108 case PM_INTEGER_NODE: {
9109 // 1
9110 // ^
9111 if (!popped) {
9112 VALUE operand = parse_integer((const pm_integer_node_t *) node);
9113 PUSH_INSN1(ret, location, putobject, operand);
9114 }
9115 return;
9116 }
9118 // if /foo #{bar}/ then end
9119 // ^^^^^^^^^^^^
9120 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9121 if (!popped) {
9122 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9123 PUSH_INSN1(ret, location, putobject, regexp);
9124 }
9125 }
9126 else {
9127 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
9128 }
9129
9130 PUSH_INSN1(ret, location, getglobal, rb_id2sym(idLASTLINE));
9131 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
9132 if (popped) PUSH_INSN(ret, location, pop);
9133
9134 return;
9135 }
9137 // /foo #{bar}/
9138 // ^^^^^^^^^^^^
9140 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9141 const rb_iseq_t *block_iseq = NULL;
9142 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9143
9144 pm_scope_node_t next_scope_node;
9145 pm_scope_node_init(node, &next_scope_node, scope_node);
9146
9147 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.line);
9148 pm_scope_node_destroy(&next_scope_node);
9149
9150 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9151 PUSH_INSN2(ret, location, once, block_iseq, INT2FIX(ise_index));
9152 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9153
9154 if (popped) PUSH_INSN(ret, location, pop);
9155 return;
9156 }
9157
9158 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9159 if (!popped) {
9160 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9161 PUSH_INSN1(ret, location, putobject, regexp);
9162 }
9163 }
9164 else {
9165 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
9166 if (popped) PUSH_INSN(ret, location, pop);
9167 }
9168
9169 return;
9170 }
9172 // "foo #{bar}"
9173 // ^^^^^^^^^^^^
9174 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9175 if (!popped) {
9176 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9177
9179 PUSH_INSN1(ret, location, putobject, string);
9180 }
9182 PUSH_INSN1(ret, location, putstring, string);
9183 }
9184 else {
9185 PUSH_INSN1(ret, location, putchilledstring, string);
9186 }
9187 }
9188 }
9189 else {
9191 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
9192 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9193 if (popped) PUSH_INSN(ret, location, pop);
9194 }
9195
9196 return;
9197 }
9199 // :"foo #{bar}"
9200 // ^^^^^^^^^^^^^
9202 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
9203
9204 if (length > 1) {
9205 PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9206 }
9207
9208 if (!popped) {
9209 PUSH_INSN(ret, location, intern);
9210 }
9211 else {
9212 PUSH_INSN(ret, location, pop);
9213 }
9214
9215 return;
9216 }
9218 // `foo #{bar}`
9219 // ^^^^^^^^^^^^
9221
9222 PUSH_INSN(ret, location, putself);
9223
9224 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL);
9225 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9226
9227 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9228 if (popped) PUSH_INSN(ret, location, pop);
9229
9230 return;
9231 }
9233 // -> { it }
9234 // ^^
9235 if (!popped) {
9236 PUSH_GETLOCAL(ret, location, scope_node->local_table_for_iseq_size, 0);
9237 }
9238
9239 return;
9240 }
9241 case PM_KEYWORD_HASH_NODE: {
9242 // foo(bar: baz)
9243 // ^^^^^^^^
9244 const pm_keyword_hash_node_t *cast = (const pm_keyword_hash_node_t *) node;
9245 const pm_node_list_t *elements = &cast->elements;
9246
9247 const pm_node_t *element;
9248 PM_NODE_LIST_FOREACH(elements, index, element) {
9249 PM_COMPILE(element);
9250 }
9251
9252 if (!popped) PUSH_INSN1(ret, location, newhash, INT2FIX(elements->size * 2));
9253 return;
9254 }
9255 case PM_LAMBDA_NODE: {
9256 // -> {}
9257 // ^^^^^
9258 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
9259
9260 pm_scope_node_t next_scope_node;
9261 pm_scope_node_init(node, &next_scope_node, scope_node);
9262
9263 int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
9264 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9265 pm_scope_node_destroy(&next_scope_node);
9266
9267 VALUE argc = INT2FIX(0);
9268 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9269 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9270 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
9271
9272 if (popped) PUSH_INSN(ret, location, pop);
9273 return;
9274 }
9276 // foo &&= bar
9277 // ^^^^^^^^^^^
9279 LABEL *end_label = NEW_LABEL(location.line);
9280
9281 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9282 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9283 if (!popped) PUSH_INSN(ret, location, dup);
9284
9285 PUSH_INSNL(ret, location, branchunless, end_label);
9286 if (!popped) PUSH_INSN(ret, location, pop);
9287
9288 PM_COMPILE_NOT_POPPED(cast->value);
9289 if (!popped) PUSH_INSN(ret, location, dup);
9290
9291 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9292 PUSH_LABEL(ret, end_label);
9293
9294 return;
9295 }
9297 // foo += bar
9298 // ^^^^^^^^^^
9300
9301 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9302 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9303
9304 PM_COMPILE_NOT_POPPED(cast->value);
9305
9306 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
9307 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
9308
9309 if (!popped) PUSH_INSN(ret, location, dup);
9310 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9311
9312 return;
9313 }
9315 // foo ||= bar
9316 // ^^^^^^^^^^^
9318
9319 LABEL *set_label = NEW_LABEL(location.line);
9320 LABEL *end_label = NEW_LABEL(location.line);
9321
9322 PUSH_INSN1(ret, location, putobject, Qtrue);
9323 PUSH_INSNL(ret, location, branchunless, set_label);
9324
9325 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9326 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9327 if (!popped) PUSH_INSN(ret, location, dup);
9328
9329 PUSH_INSNL(ret, location, branchif, end_label);
9330 if (!popped) PUSH_INSN(ret, location, pop);
9331
9332 PUSH_LABEL(ret, set_label);
9333 PM_COMPILE_NOT_POPPED(cast->value);
9334 if (!popped) PUSH_INSN(ret, location, dup);
9335
9336 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9337 PUSH_LABEL(ret, end_label);
9338
9339 return;
9340 }
9342 // foo
9343 // ^^^
9344 if (!popped) {
9346 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9347 PUSH_GETLOCAL(ret, location, index.index, index.level);
9348 }
9349
9350 return;
9351 }
9353 // foo = 1
9354 // ^^^^^^^
9356 PM_COMPILE_NOT_POPPED(cast->value);
9357 if (!popped) PUSH_INSN(ret, location, dup);
9358
9359 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9360 PUSH_SETLOCAL(ret, location, index.index, index.level);
9361 return;
9362 }
9364 // if /foo/ then end
9365 // ^^^^^
9366 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9367
9368 PUSH_INSN1(ret, location, putobject, regexp);
9369 PUSH_INSN2(ret, location, getspecial, INT2FIX(0), INT2FIX(0));
9370 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
9371 if (popped) PUSH_INSN(ret, location, pop);
9372
9373 return;
9374 }
9376 // foo in bar
9377 // ^^^^^^^^^^
9378 const pm_match_predicate_node_t *cast = (const pm_match_predicate_node_t *) node;
9379
9380 // First, allocate some stack space for the cached return value of any
9381 // calls to #deconstruct.
9382 PUSH_INSN(ret, location, putnil);
9383
9384 // Next, compile the expression that we're going to match against.
9385 PM_COMPILE_NOT_POPPED(cast->value);
9386 PUSH_INSN(ret, location, dup);
9387
9388 // Now compile the pattern that is going to be used to match against the
9389 // expression.
9390 LABEL *matched_label = NEW_LABEL(location.line);
9391 LABEL *unmatched_label = NEW_LABEL(location.line);
9392 LABEL *done_label = NEW_LABEL(location.line);
9393 pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
9394
9395 // If the pattern did not match, then compile the necessary instructions
9396 // to handle pushing false onto the stack, then jump to the end.
9397 PUSH_LABEL(ret, unmatched_label);
9398 PUSH_INSN(ret, location, pop);
9399 PUSH_INSN(ret, location, pop);
9400
9401 if (!popped) PUSH_INSN1(ret, location, putobject, Qfalse);
9402 PUSH_INSNL(ret, location, jump, done_label);
9403 PUSH_INSN(ret, location, putnil);
9404
9405 // If the pattern did match, then compile the necessary instructions to
9406 // handle pushing true onto the stack, then jump to the end.
9407 PUSH_LABEL(ret, matched_label);
9408 PUSH_INSN1(ret, location, adjuststack, INT2FIX(2));
9409 if (!popped) PUSH_INSN1(ret, location, putobject, Qtrue);
9410 PUSH_INSNL(ret, location, jump, done_label);
9411
9412 PUSH_LABEL(ret, done_label);
9413 return;
9414 }
9416 // foo => bar
9417 // ^^^^^^^^^^
9418 //
9419 // A match required node represents pattern matching against a single
9420 // pattern using the => operator. For example,
9421 //
9422 // foo => bar
9423 //
9424 // This is somewhat analogous to compiling a case match statement with a
9425 // single pattern. In both cases, if the pattern fails it should
9426 // immediately raise an error.
9427 pm_compile_match_required_node(iseq, (const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
9428 return;
9430 // /(?<foo>foo)/ =~ bar
9431 // ^^^^^^^^^^^^^^^^^^^^
9432 //
9433 // Match write nodes are specialized call nodes that have a regular
9434 // expression with valid named capture groups on the left, the =~
9435 // operator, and some value on the right. The nodes themselves simply
9436 // wrap the call with the local variable targets that will be written
9437 // when the call is executed.
9438 pm_compile_match_write_node(iseq, (const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9439 return;
9440 case PM_MISSING_NODE:
9441 rb_bug("A pm_missing_node_t should not exist in prism's AST.");
9442 return;
9443 case PM_MODULE_NODE: {
9444 // module Foo; end
9445 // ^^^^^^^^^^^^^^^
9446 const pm_module_node_t *cast = (const pm_module_node_t *) node;
9447
9448 ID module_id = pm_constant_id_lookup(scope_node, cast->name);
9449 VALUE module_name = rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(module_id)));
9450
9451 pm_scope_node_t next_scope_node;
9452 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
9453
9454 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.line);
9455 pm_scope_node_destroy(&next_scope_node);
9456
9457 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
9458 PUSH_INSN(ret, location, putnil);
9459 PUSH_INSN3(ret, location, defineclass, ID2SYM(module_id), module_iseq, INT2FIX(flags));
9460 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) module_iseq);
9461
9462 if (popped) PUSH_INSN(ret, location, pop);
9463 return;
9464 }
9466 // def foo(bar); end
9467 // ^^^
9469 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
9470
9471 PUSH_SETLOCAL(ret, location, index.index, index.level);
9472 return;
9473 }
9474 case PM_MULTI_WRITE_NODE: {
9475 // foo, bar = baz
9476 // ^^^^^^^^^^^^^^
9477 //
9478 // A multi write node represents writing to multiple values using an =
9479 // operator. Importantly these nodes are only parsed when the left-hand
9480 // side of the operator has multiple targets. The right-hand side of the
9481 // operator having multiple targets represents an implicit array
9482 // instead.
9483 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
9484
9485 DECL_ANCHOR(writes);
9486 DECL_ANCHOR(cleanup);
9487
9488 pm_multi_target_state_t state = { 0 };
9489 state.position = popped ? 0 : 1;
9490 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9491
9492 PM_COMPILE_NOT_POPPED(cast->value);
9493 if (!popped) PUSH_INSN(ret, location, dup);
9494
9495 PUSH_SEQ(ret, writes);
9496 if (!popped && state.stack_size >= 1) {
9497 // Make sure the value on the right-hand side of the = operator is
9498 // being returned before we pop the parent expressions.
9499 PUSH_INSN1(ret, location, setn, INT2FIX(state.stack_size));
9500 }
9501
9502 // Now, we need to go back and modify the topn instructions in order to
9503 // ensure they can correctly retrieve the parent expressions.
9504 pm_multi_target_state_update(&state);
9505
9506 PUSH_SEQ(ret, cleanup);
9507 return;
9508 }
9509 case PM_NEXT_NODE:
9510 // next
9511 // ^^^^
9512 //
9513 // next foo
9514 // ^^^^^^^^
9515 pm_compile_next_node(iseq, (const pm_next_node_t *) node, &location, ret, popped, scope_node);
9516 return;
9517 case PM_NIL_NODE: {
9518 // nil
9519 // ^^^
9520 if (!popped) {
9521 PUSH_INSN(ret, location, putnil);
9522 }
9523
9524 return;
9525 }
9527 // def foo(**nil); end
9528 // ^^^^^
9529 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9530 return;
9531 }
9533 // $1
9534 // ^^
9535 if (!popped) {
9536 uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
9537
9538 if (reference_number > 0) {
9539 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), INT2FIX(reference_number << 1));
9540 }
9541 else {
9542 PUSH_INSN(ret, location, putnil);
9543 }
9544 }
9545
9546 return;
9547 }
9548 case PM_OR_NODE: {
9549 // a or b
9550 // ^^^^^^
9551 const pm_or_node_t *cast = (const pm_or_node_t *) node;
9552
9553 LABEL *end_label = NEW_LABEL(location.line);
9554 PM_COMPILE_NOT_POPPED(cast->left);
9555
9556 if (!popped) PUSH_INSN(ret, location, dup);
9557 PUSH_INSNL(ret, location, branchif, end_label);
9558
9559 if (!popped) PUSH_INSN(ret, location, pop);
9560 PM_COMPILE(cast->right);
9561 PUSH_LABEL(ret, end_label);
9562
9563 return;
9564 }
9566 // def foo(bar = 1); end
9567 // ^^^^^^^
9569 PM_COMPILE_NOT_POPPED(cast->value);
9570
9571 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
9572 PUSH_SETLOCAL(ret, location, index.index, index.level);
9573
9574 return;
9575 }
9576 case PM_PARENTHESES_NODE: {
9577 // ()
9578 // ^^
9579 //
9580 // (1)
9581 // ^^^
9582 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
9583
9584 if (cast->body != NULL) {
9585 PM_COMPILE(cast->body);
9586 }
9587 else if (!popped) {
9588 PUSH_INSN(ret, location, putnil);
9589 }
9590
9591 return;
9592 }
9593 case PM_PRE_EXECUTION_NODE: {
9594 // BEGIN {}
9595 // ^^^^^^^^
9596 const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
9597
9598 LINK_ANCHOR *outer_pre = scope_node->pre_execution_anchor;
9599 RUBY_ASSERT(outer_pre != NULL);
9600
9601 // BEGIN{} nodes can be nested, so here we're going to do the same thing
9602 // that we did for the top-level compilation where we create two
9603 // anchors and then join them in the correct order into the resulting
9604 // anchor.
9605 DECL_ANCHOR(inner_pre);
9606 scope_node->pre_execution_anchor = inner_pre;
9607
9608 DECL_ANCHOR(inner_body);
9609
9610 if (cast->statements != NULL) {
9611 const pm_node_list_t *body = &cast->statements->body;
9612
9613 for (size_t index = 0; index < body->size; index++) {
9614 pm_compile_node(iseq, body->nodes[index], inner_body, true, scope_node);
9615 }
9616 }
9617
9618 if (!popped) {
9619 PUSH_INSN(inner_body, location, putnil);
9620 }
9621
9622 // Now that everything has been compiled, join both anchors together
9623 // into the correct outer pre execution anchor, and reset the value so
9624 // that subsequent BEGIN{} nodes can be compiled correctly.
9625 PUSH_SEQ(outer_pre, inner_pre);
9626 PUSH_SEQ(outer_pre, inner_body);
9627 scope_node->pre_execution_anchor = outer_pre;
9628
9629 return;
9630 }
9632 // END {}
9633 // ^^^^^^
9634 const rb_iseq_t *child_iseq;
9635 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9636
9637 pm_scope_node_t next_scope_node;
9638 pm_scope_node_init(node, &next_scope_node, scope_node);
9639 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
9640 pm_scope_node_destroy(&next_scope_node);
9641
9642 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9643
9644 int is_index = ISEQ_BODY(iseq)->ise_size++;
9645 PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
9646 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
9647 if (popped) PUSH_INSN(ret, location, pop);
9648
9649 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9650
9651 return;
9652 }
9653 case PM_RANGE_NODE: {
9654 // 0..5
9655 // ^^^^
9656 const pm_range_node_t *cast = (const pm_range_node_t *) node;
9657 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
9658
9659 if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
9660 if (!popped) {
9661 const pm_node_t *left = cast->left;
9662 const pm_node_t *right = cast->right;
9663
9664 VALUE val = rb_range_new(
9665 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
9666 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
9667 exclude_end
9668 );
9669
9670 PUSH_INSN1(ret, location, putobject, val);
9671 }
9672 }
9673 else {
9674 if (cast->left != NULL) {
9675 PM_COMPILE(cast->left);
9676 }
9677 else if (!popped) {
9678 PUSH_INSN(ret, location, putnil);
9679 }
9680
9681 if (cast->right != NULL) {
9682 PM_COMPILE(cast->right);
9683 }
9684 else if (!popped) {
9685 PUSH_INSN(ret, location, putnil);
9686 }
9687
9688 if (!popped) {
9689 PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
9690 }
9691 }
9692 return;
9693 }
9694 case PM_RATIONAL_NODE: {
9695 // 1r
9696 // ^^
9697 if (!popped) {
9698 PUSH_INSN1(ret, location, putobject, parse_rational((const pm_rational_node_t *) node));
9699 }
9700 return;
9701 }
9702 case PM_REDO_NODE:
9703 // redo
9704 // ^^^^
9705 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
9706 return;
9708 // /foo/
9709 // ^^^^^
9710 if (!popped) {
9711 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9712 PUSH_INSN1(ret, location, putobject, regexp);
9713 }
9714 return;
9715 }
9716 case PM_RESCUE_NODE:
9717 // begin; rescue; end
9718 // ^^^^^^^
9719 pm_compile_rescue_node(iseq, (const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
9720 return;
9722 // foo rescue bar
9723 // ^^^^^^^^^^^^^^
9724 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
9725
9726 pm_scope_node_t rescue_scope_node;
9727 pm_scope_node_init((const pm_node_t *) cast, &rescue_scope_node, scope_node);
9728
9729 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
9730 &rescue_scope_node,
9731 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
9732 ISEQ_TYPE_RESCUE,
9733 pm_node_line_number(parser, cast->rescue_expression)
9734 );
9735
9736 pm_scope_node_destroy(&rescue_scope_node);
9737
9738 LABEL *lstart = NEW_LABEL(location.line);
9739 LABEL *lend = NEW_LABEL(location.line);
9740 LABEL *lcont = NEW_LABEL(location.line);
9741
9742 lstart->rescued = LABEL_RESCUE_BEG;
9743 lend->rescued = LABEL_RESCUE_END;
9744
9745 PUSH_LABEL(ret, lstart);
9746 PM_COMPILE_NOT_POPPED(cast->expression);
9747 PUSH_LABEL(ret, lend);
9748
9749 PUSH_INSN(ret, location, nop);
9750 PUSH_LABEL(ret, lcont);
9751 if (popped) PUSH_INSN(ret, location, pop);
9752
9753 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
9754 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
9755 return;
9756 }
9757 case PM_RETURN_NODE:
9758 // return
9759 // ^^^^^^
9760 //
9761 // return 1
9762 // ^^^^^^^^
9763 pm_compile_return_node(iseq, (const pm_return_node_t *) node, &location, ret, popped, scope_node);
9764 return;
9765 case PM_RETRY_NODE: {
9766 // retry
9767 // ^^^^^
9768 if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
9769 PUSH_INSN(ret, location, putnil);
9770 PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETRY));
9771 if (popped) PUSH_INSN(ret, location, pop);
9772 }
9773 else {
9774 COMPILE_ERROR(iseq, location.line, "Invalid retry");
9775 return;
9776 }
9777 return;
9778 }
9779 case PM_SCOPE_NODE:
9780 pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
9781 return;
9782 case PM_SELF_NODE: {
9783 // self
9784 // ^^^^
9785 if (!popped) {
9786 PUSH_INSN(ret, location, putself);
9787 }
9788 return;
9789 }
9791 // A value that is being written to a constant that is being marked as
9792 // shared depending on the current lexical context.
9795
9796 switch (PM_NODE_TYPE(cast->write)) {
9798 pm_compile_constant_write_node(iseq, (const pm_constant_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9799 break;
9801 pm_compile_constant_and_write_node(iseq, (const pm_constant_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9802 break;
9804 pm_compile_constant_or_write_node(iseq, (const pm_constant_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9805 break;
9807 pm_compile_constant_operator_write_node(iseq, (const pm_constant_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9808 break;
9810 pm_compile_constant_path_write_node(iseq, (const pm_constant_path_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9811 break;
9813 pm_compile_constant_path_and_write_node(iseq, (const pm_constant_path_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9814 break;
9816 pm_compile_constant_path_or_write_node(iseq, (const pm_constant_path_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9817 break;
9819 pm_compile_constant_path_operator_write_node(iseq, (const pm_constant_path_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
9820 break;
9821 default:
9822 rb_bug("Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->write)));
9823 break;
9824 }
9825
9826 return;
9827 }
9829 // class << self; end
9830 // ^^^^^^^^^^^^^^^^^^
9831 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
9832
9833 pm_scope_node_t next_scope_node;
9834 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
9835 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, location.line);
9836 pm_scope_node_destroy(&next_scope_node);
9837
9838 PM_COMPILE_NOT_POPPED(cast->expression);
9839 PUSH_INSN(ret, location, putnil);
9840
9841 ID singletonclass;
9842 CONST_ID(singletonclass, "singletonclass");
9843 PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
9844
9845 if (popped) PUSH_INSN(ret, location, pop);
9846 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
9847
9848 return;
9849 }
9851 // __ENCODING__
9852 // ^^^^^^^^^^^^
9853 if (!popped) {
9854 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9855 PUSH_INSN1(ret, location, putobject, value);
9856 }
9857 return;
9858 }
9859 case PM_SOURCE_FILE_NODE: {
9860 // __FILE__
9861 // ^^^^^^^^
9862 if (!popped) {
9863 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
9864 VALUE string = pm_source_file_value(cast, scope_node);
9865
9867 PUSH_INSN1(ret, location, putobject, string);
9868 }
9869 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
9870 PUSH_INSN1(ret, location, putstring, string);
9871 }
9872 else {
9873 PUSH_INSN1(ret, location, putchilledstring, string);
9874 }
9875 }
9876 return;
9877 }
9878 case PM_SOURCE_LINE_NODE: {
9879 // __LINE__
9880 // ^^^^^^^^
9881 if (!popped) {
9882 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9883 PUSH_INSN1(ret, location, putobject, value);
9884 }
9885 return;
9886 }
9887 case PM_SPLAT_NODE: {
9888 // foo(*bar)
9889 // ^^^^
9890 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
9891 if (cast->expression) {
9892 PM_COMPILE(cast->expression);
9893 }
9894
9895 if (!popped) {
9896 PUSH_INSN1(ret, location, splatarray, Qtrue);
9897 }
9898 return;
9899 }
9900 case PM_STATEMENTS_NODE: {
9901 // A list of statements.
9902 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
9903 const pm_node_list_t *body = &cast->body;
9904
9905 if (body->size > 0) {
9906 for (size_t index = 0; index < body->size - 1; index++) {
9907 PM_COMPILE_POPPED(body->nodes[index]);
9908 }
9909 PM_COMPILE(body->nodes[body->size - 1]);
9910 }
9911 else {
9912 PUSH_INSN(ret, location, putnil);
9913 }
9914 return;
9915 }
9916 case PM_STRING_NODE: {
9917 // "foo"
9918 // ^^^^^
9919 if (!popped) {
9920 const pm_string_node_t *cast = (const pm_string_node_t *) node;
9921 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
9922
9924 PUSH_INSN1(ret, location, putobject, value);
9925 }
9926 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
9927 PUSH_INSN1(ret, location, putstring, value);
9928 }
9929 else {
9930 PUSH_INSN1(ret, location, putchilledstring, value);
9931 }
9932 }
9933 return;
9934 }
9935 case PM_SUPER_NODE:
9936 // super()
9937 // super(foo)
9938 // super(...)
9939 pm_compile_super_node(iseq, (const pm_super_node_t *) node, &location, ret, popped, scope_node);
9940 return;
9941 case PM_SYMBOL_NODE: {
9942 // :foo
9943 // ^^^^
9944 if (!popped) {
9945 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9946 PUSH_INSN1(ret, location, putobject, value);
9947 }
9948 return;
9949 }
9950 case PM_TRUE_NODE: {
9951 // true
9952 // ^^^^
9953 if (!popped) {
9954 PUSH_INSN1(ret, location, putobject, Qtrue);
9955 }
9956 return;
9957 }
9958 case PM_UNDEF_NODE: {
9959 // undef foo
9960 // ^^^^^^^^^
9961 const pm_undef_node_t *cast = (const pm_undef_node_t *) node;
9962 const pm_node_list_t *names = &cast->names;
9963
9964 for (size_t index = 0; index < names->size; index++) {
9965 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9966 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
9967
9968 PM_COMPILE_NOT_POPPED(names->nodes[index]);
9969 PUSH_SEND(ret, location, id_core_undef_method, INT2NUM(2));
9970
9971 if (index < names->size - 1) {
9972 PUSH_INSN(ret, location, pop);
9973 }
9974 }
9975
9976 if (popped) PUSH_INSN(ret, location, pop);
9977 return;
9978 }
9979 case PM_UNLESS_NODE: {
9980 // unless foo; bar end
9981 // ^^^^^^^^^^^^^^^^^^^
9982 //
9983 // bar unless foo
9984 // ^^^^^^^^^^^^^^
9985 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
9986 const pm_statements_node_t *statements = NULL;
9987 if (cast->else_clause != NULL) {
9988 statements = ((const pm_else_node_t *) cast->else_clause)->statements;
9989 }
9990
9991 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);
9992 return;
9993 }
9994 case PM_UNTIL_NODE: {
9995 // until foo; bar end
9996 // ^^^^^^^^^^^^^^^^^
9997 //
9998 // bar until foo
9999 // ^^^^^^^^^^^^^
10000 const pm_until_node_t *cast = (const pm_until_node_t *) node;
10001 pm_compile_loop(iseq, &location, cast->base.flags, PM_UNTIL_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10002 return;
10003 }
10004 case PM_WHILE_NODE: {
10005 // while foo; bar end
10006 // ^^^^^^^^^^^^^^^^^^
10007 //
10008 // bar while foo
10009 // ^^^^^^^^^^^^^
10010 const pm_while_node_t *cast = (const pm_while_node_t *) node;
10011 pm_compile_loop(iseq, &location, cast->base.flags, PM_WHILE_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10012 return;
10013 }
10014 case PM_X_STRING_NODE: {
10015 // `foo`
10016 // ^^^^^
10017 const pm_x_string_node_t *cast = (const pm_x_string_node_t *) node;
10018 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
10019
10020 PUSH_INSN(ret, location, putself);
10021 PUSH_INSN1(ret, location, putobject, value);
10022 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10023 if (popped) PUSH_INSN(ret, location, pop);
10024
10025 return;
10026 }
10027 case PM_YIELD_NODE:
10028 // yield
10029 // ^^^^^
10030 //
10031 // yield 1
10032 // ^^^^^^^
10033 pm_compile_yield_node(iseq, (const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10034 return;
10035 default:
10036 rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10037 return;
10038 }
10039}
10040
10041#undef PM_CONTAINER_P
10042
10044static inline bool
10045pm_iseq_pre_execution_p(rb_iseq_t *iseq)
10046{
10047 switch (ISEQ_BODY(iseq)->type) {
10048 case ISEQ_TYPE_TOP:
10049 case ISEQ_TYPE_EVAL:
10050 case ISEQ_TYPE_MAIN:
10051 return true;
10052 default:
10053 return false;
10054 }
10055}
10056
10064VALUE
10065pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
10066{
10067 DECL_ANCHOR(ret);
10068
10069 if (pm_iseq_pre_execution_p(iseq)) {
10070 // Because these ISEQs can have BEGIN{}, we're going to create two
10071 // anchors to compile them, a "pre" and a "body". We'll mark the "pre"
10072 // on the scope node so that when BEGIN{} is found, its contents will be
10073 // added to the "pre" anchor.
10074 DECL_ANCHOR(pre);
10075 node->pre_execution_anchor = pre;
10076
10077 // Now we'll compile the body as normal. We won't compile directly into
10078 // the "ret" anchor yet because we want to add the "pre" anchor to the
10079 // beginning of the "ret" anchor first.
10080 DECL_ANCHOR(body);
10081 pm_compile_node(iseq, (const pm_node_t *) node, body, false, node);
10082
10083 // Now we'll join both anchors together so that the content is in the
10084 // correct order.
10085 PUSH_SEQ(ret, pre);
10086 PUSH_SEQ(ret, body);
10087 }
10088 else {
10089 // In other circumstances, we can just compile the node directly into
10090 // the "ret" anchor.
10091 pm_compile_node(iseq, (const pm_node_t *) node, ret, false, node);
10092 }
10093
10094 CHECK(iseq_setup_insn(iseq, ret));
10095 return iseq_setup(iseq, ret);
10096}
10097
10102void
10103pm_parse_result_free(pm_parse_result_t *result)
10104{
10105 if (result->node.ast_node != NULL) {
10106 pm_node_destroy(&result->parser, result->node.ast_node);
10107 }
10108
10109 if (result->parsed) {
10110 xfree(result->node.constants);
10111 pm_scope_node_destroy(&result->node);
10112 }
10113
10114 pm_parser_free(&result->parser);
10115 pm_string_free(&result->input);
10116 pm_options_free(&result->options);
10117}
10118
10120typedef struct {
10123
10125 int32_t line;
10126
10129
10131 uint32_t column_end;
10133
10135typedef struct {
10137 const char *number_prefix;
10138
10140 const char *blank_prefix;
10141
10143 const char *divider;
10144
10147
10151
10152#define PM_COLOR_BOLD "\033[1m"
10153#define PM_COLOR_GRAY "\033[2m"
10154#define PM_COLOR_RED "\033[1;31m"
10155#define PM_COLOR_RESET "\033[m"
10156#define PM_ERROR_TRUNCATE 30
10157
10158static inline pm_parse_error_t *
10159pm_parse_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
10160 pm_parse_error_t *errors = xcalloc(error_list->size, sizeof(pm_parse_error_t));
10161 if (errors == NULL) return NULL;
10162
10163 int32_t start_line = parser->start_line;
10164 for (pm_diagnostic_t *error = (pm_diagnostic_t *) error_list->head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
10165 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.start, start_line);
10166 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10167
10168 // We're going to insert this error into the array in sorted order. We
10169 // do this by finding the first error that has a line number greater
10170 // than the current error and then inserting the current error before
10171 // that one.
10172 size_t index = 0;
10173 while (
10174 (index < error_list->size) &&
10175 (errors[index].error != NULL) &&
10176 (
10177 (errors[index].line < start.line) ||
10178 ((errors[index].line == start.line) && (errors[index].column_start < start.column))
10179 )
10180 ) index++;
10181
10182 // Now we're going to shift all of the errors after this one down one
10183 // index to make room for the new error.
10184 if (index + 1 < error_list->size) {
10185 memmove(&errors[index + 1], &errors[index], sizeof(pm_parse_error_t) * (error_list->size - index - 1));
10186 }
10187
10188 // Finally, we'll insert the error into the array.
10189 uint32_t column_end;
10190 if (start.line == end.line) {
10191 column_end = end.column;
10192 } else {
10193 column_end = (uint32_t) (newline_list->offsets[start.line - start_line + 1] - newline_list->offsets[start.line - start_line] - 1);
10194 }
10195
10196 // Ensure we have at least one column of error.
10197 if (start.column == column_end) column_end++;
10198
10199 errors[index] = (pm_parse_error_t) {
10200 .error = error,
10201 .line = start.line,
10202 .column_start = start.column,
10203 .column_end = column_end
10204 };
10205 }
10206
10207 return errors;
10208}
10209
10210/* Append a literal string to the buffer. */
10211#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10212
10213static inline void
10214pm_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) {
10215 int32_t line_delta = line - parser->start_line;
10216 assert(line_delta >= 0);
10217
10218 size_t index = (size_t) line_delta;
10219 assert(index < newline_list->size);
10220
10221 const uint8_t *start = &parser->start[newline_list->offsets[index]];
10222 const uint8_t *end;
10223
10224 if (index >= newline_list->size - 1) {
10225 end = parser->end;
10226 } else {
10227 end = &parser->start[newline_list->offsets[index + 1]];
10228 }
10229
10230 pm_buffer_append_format(buffer, number_prefix, line);
10231
10232 // Here we determine if we should truncate the end of the line.
10233 bool truncate_end = false;
10234 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10235 end = start + column_end + PM_ERROR_TRUNCATE;
10236 truncate_end = true;
10237 }
10238
10239 // Here we determine if we should truncate the start of the line.
10240 if (column_start >= PM_ERROR_TRUNCATE) {
10241 pm_buffer_append_string(buffer, "... ", 4);
10242 start += column_start;
10243 }
10244
10245 pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));
10246
10247 if (truncate_end) {
10248 pm_buffer_append_string(buffer, " ...\n", 5);
10249 } else if (end == parser->end && end[-1] != '\n') {
10250 pm_buffer_append_string(buffer, "\n", 1);
10251 }
10252}
10253
10257static void
10258pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, int highlight, bool inline_messages) {
10259 assert(error_list->size != 0);
10260
10261 // First, we're going to sort all of the errors by line number using an
10262 // insertion sort into a newly allocated array.
10263 const int32_t start_line = parser->start_line;
10264 const pm_newline_list_t *newline_list = &parser->newline_list;
10265
10266 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10267 if (errors == NULL) return;
10268
10269 // Now we're going to determine how we're going to format line numbers and
10270 // blank lines based on the maximum number of digits in the line numbers
10271 // that are going to be displaid.
10272 pm_parse_error_format_t error_format;
10273 int32_t first_line_number = errors[0].line;
10274 int32_t last_line_number = errors[error_list->size - 1].line;
10275
10276 // If we have a maximum line number that is negative, then we're going to
10277 // use the absolute value for comparison but multiple by 10 to additionally
10278 // have a column for the negative sign.
10279 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10280 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10281 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10282
10283 if (max_line_number < 10) {
10284 if (highlight > 0) {
10285 error_format = (pm_parse_error_format_t) {
10286 .number_prefix = PM_COLOR_GRAY "%1" PRIi32 " | " PM_COLOR_RESET,
10287 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10288 .divider = PM_COLOR_GRAY " ~~~~~" PM_COLOR_RESET "\n"
10289 };
10290 } else {
10291 error_format = (pm_parse_error_format_t) {
10292 .number_prefix = "%1" PRIi32 " | ",
10293 .blank_prefix = " | ",
10294 .divider = " ~~~~~\n"
10295 };
10296 }
10297 } else if (max_line_number < 100) {
10298 if (highlight > 0) {
10299 error_format = (pm_parse_error_format_t) {
10300 .number_prefix = PM_COLOR_GRAY "%2" PRIi32 " | " PM_COLOR_RESET,
10301 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10302 .divider = PM_COLOR_GRAY " ~~~~~~" PM_COLOR_RESET "\n"
10303 };
10304 } else {
10305 error_format = (pm_parse_error_format_t) {
10306 .number_prefix = "%2" PRIi32 " | ",
10307 .blank_prefix = " | ",
10308 .divider = " ~~~~~~\n"
10309 };
10310 }
10311 } else if (max_line_number < 1000) {
10312 if (highlight > 0) {
10313 error_format = (pm_parse_error_format_t) {
10314 .number_prefix = PM_COLOR_GRAY "%3" PRIi32 " | " PM_COLOR_RESET,
10315 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10316 .divider = PM_COLOR_GRAY " ~~~~~~~" PM_COLOR_RESET "\n"
10317 };
10318 } else {
10319 error_format = (pm_parse_error_format_t) {
10320 .number_prefix = "%3" PRIi32 " | ",
10321 .blank_prefix = " | ",
10322 .divider = " ~~~~~~~\n"
10323 };
10324 }
10325 } else if (max_line_number < 10000) {
10326 if (highlight > 0) {
10327 error_format = (pm_parse_error_format_t) {
10328 .number_prefix = PM_COLOR_GRAY "%4" PRIi32 " | " PM_COLOR_RESET,
10329 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10330 .divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
10331 };
10332 } else {
10333 error_format = (pm_parse_error_format_t) {
10334 .number_prefix = "%4" PRIi32 " | ",
10335 .blank_prefix = " | ",
10336 .divider = " ~~~~~~~~\n"
10337 };
10338 }
10339 } else {
10340 if (highlight > 0) {
10341 error_format = (pm_parse_error_format_t) {
10342 .number_prefix = PM_COLOR_GRAY "%5" PRIi32 " | " PM_COLOR_RESET,
10343 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10344 .divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
10345 };
10346 } else {
10347 error_format = (pm_parse_error_format_t) {
10348 .number_prefix = "%5" PRIi32 " | ",
10349 .blank_prefix = " | ",
10350 .divider = " ~~~~~~~~\n"
10351 };
10352 }
10353 }
10354
10355 error_format.blank_prefix_length = strlen(error_format.blank_prefix);
10356 error_format.divider_length = strlen(error_format.divider);
10357
10358 // Now we're going to iterate through every error in our error list and
10359 // display it. While we're iterating, we will display some padding lines of
10360 // the source before the error to give some context. We'll be careful not to
10361 // display the same line twice in case the errors are close enough in the
10362 // source.
10363 int32_t last_line = parser->start_line - 1;
10364 uint32_t last_column_start = 0;
10365 const pm_encoding_t *encoding = parser->encoding;
10366
10367 for (size_t index = 0; index < error_list->size; index++) {
10368 pm_parse_error_t *error = &errors[index];
10369
10370 // Here we determine how many lines of padding of the source to display,
10371 // based on the difference from the last line that was displaid.
10372 if (error->line - last_line > 1) {
10373 if (error->line - last_line > 2) {
10374 if ((index != 0) && (error->line - last_line > 3)) {
10375 pm_buffer_append_string(buffer, error_format.divider, error_format.divider_length);
10376 }
10377
10378 pm_buffer_append_string(buffer, " ", 2);
10379 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 2, 0, 0, buffer);
10380 }
10381
10382 pm_buffer_append_string(buffer, " ", 2);
10383 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 1, 0, 0, buffer);
10384 }
10385
10386 // If this is the first error or we're on a new line, then we'll display
10387 // the line that has the error in it.
10388 if ((index == 0) || (error->line != last_line)) {
10389 if (highlight > 1) {
10390 pm_buffer_append_literal(buffer, PM_COLOR_RED "> " PM_COLOR_RESET);
10391 } else if (highlight > 0) {
10392 pm_buffer_append_literal(buffer, PM_COLOR_BOLD "> " PM_COLOR_RESET);
10393 } else {
10394 pm_buffer_append_literal(buffer, "> ");
10395 }
10396
10397 last_column_start = error->column_start;
10398
10399 // Find the maximum column end of all the errors on this line.
10400 uint32_t column_end = error->column_end;
10401 for (size_t next_index = index + 1; next_index < error_list->size; next_index++) {
10402 if (errors[next_index].line != error->line) break;
10403 if (errors[next_index].column_end > column_end) column_end = errors[next_index].column_end;
10404 }
10405
10406 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line, error->column_start, column_end, buffer);
10407 }
10408
10409 const uint8_t *start = &parser->start[newline_list->offsets[error->line - start_line]];
10410 if (start == parser->end) pm_buffer_append_byte(buffer, '\n');
10411
10412 // Now we'll display the actual error message. We'll do this by first
10413 // putting the prefix to the line, then a bunch of blank spaces
10414 // depending on the column, then as many carets as we need to display
10415 // the width of the error, then the error message itself.
10416 //
10417 // Note that this doesn't take into account the width of the actual
10418 // character when displaid in the terminal. For some east-asian
10419 // languages or emoji, this means it can be thrown off pretty badly. We
10420 // will need to solve this eventually.
10421 pm_buffer_append_string(buffer, " ", 2);
10422 pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
10423
10424 size_t column = 0;
10425 if (last_column_start >= PM_ERROR_TRUNCATE) {
10426 pm_buffer_append_string(buffer, " ", 4);
10427 column = last_column_start;
10428 }
10429
10430 while (column < error->column_start) {
10431 pm_buffer_append_byte(buffer, ' ');
10432
10433 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10434 column += (char_width == 0 ? 1 : char_width);
10435 }
10436
10437 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10438 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10439 pm_buffer_append_byte(buffer, '^');
10440
10441 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10442 column += (char_width == 0 ? 1 : char_width);
10443
10444 while (column < error->column_end) {
10445 pm_buffer_append_byte(buffer, '~');
10446
10447 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10448 column += (char_width == 0 ? 1 : char_width);
10449 }
10450
10451 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10452
10453 if (inline_messages) {
10454 pm_buffer_append_byte(buffer, ' ');
10455 assert(error->error != NULL);
10456
10457 const char *message = error->error->message;
10458 pm_buffer_append_string(buffer, message, strlen(message));
10459 }
10460
10461 pm_buffer_append_byte(buffer, '\n');
10462
10463 // Here we determine how many lines of padding to display after the
10464 // error, depending on where the next error is in source.
10465 last_line = error->line;
10466 int32_t next_line;
10467
10468 if (index == error_list->size - 1) {
10469 next_line = (((int32_t) newline_list->size) + parser->start_line);
10470
10471 // If the file ends with a newline, subtract one from our "next_line"
10472 // so that we don't output an extra line at the end of the file
10473 if ((parser->start + newline_list->offsets[newline_list->size - 1]) == parser->end) {
10474 next_line--;
10475 }
10476 }
10477 else {
10478 next_line = errors[index + 1].line;
10479 }
10480
10481 if (next_line - last_line > 1) {
10482 pm_buffer_append_string(buffer, " ", 2);
10483 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
10484 }
10485
10486 if (next_line - last_line > 1) {
10487 pm_buffer_append_string(buffer, " ", 2);
10488 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
10489 }
10490 }
10491
10492 // Finally, we'll free the array of errors that we allocated.
10493 xfree(errors);
10494}
10495
10496#undef PM_ERROR_TRUNCATE
10497#undef PM_COLOR_GRAY
10498#undef PM_COLOR_RED
10499#undef PM_COLOR_RESET
10500
10507static bool
10508pm_parse_process_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
10509{
10510 const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
10511 const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
10512
10513 const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
10514 const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
10515 size_t width;
10516
10517 while (start < end) {
10518 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
10519 start += width;
10520 }
10521
10522 return true;
10523}
10524
10529static VALUE
10530pm_parse_process_error(const pm_parse_result_t *result)
10531{
10532 const pm_parser_t *parser = &result->parser;
10533 const pm_diagnostic_t *head = (const pm_diagnostic_t *) parser->error_list.head;
10534 bool valid_utf8 = true;
10535
10536 pm_buffer_t buffer = { 0 };
10537 const pm_string_t *filepath = &parser->filepath;
10538
10539 int highlight = rb_stderr_tty_p();
10540 if (highlight) {
10541 const char *no_color = getenv("NO_COLOR");
10542 highlight = (no_color == NULL || no_color[0] == '\0') ? 2 : 1;
10543 }
10544
10545 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
10546 switch (error->level) {
10548 // It is implicitly assumed that the error messages will be
10549 // encodeable as UTF-8. Because of this, we can't include source
10550 // examples that contain invalid byte sequences. So if any source
10551 // examples include invalid UTF-8 byte sequences, we will skip
10552 // showing source examples entirely.
10553 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
10554 valid_utf8 = false;
10555 }
10556 break;
10558 // Any errors with the level PM_ERROR_LEVEL_ARGUMENT take over as
10559 // the only argument that gets raised. This is to allow priority
10560 // messages that should be handled before anything else.
10561 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
10562
10563 pm_buffer_append_format(
10564 &buffer,
10565 "%.*s:%" PRIi32 ": %s",
10566 (int) pm_string_length(filepath),
10567 pm_string_source(filepath),
10568 line_number,
10569 error->message
10570 );
10571
10572 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
10573 pm_buffer_append_byte(&buffer, '\n');
10574
10575 pm_list_node_t *list_node = (pm_list_node_t *) error;
10576 pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
10577
10578 pm_parse_errors_format(parser, &error_list, &buffer, highlight, false);
10579 }
10580
10581 VALUE value = rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
10582 pm_buffer_free(&buffer);
10583
10584 return value;
10585 }
10586 case PM_ERROR_LEVEL_LOAD: {
10587 // Load errors are much simpler, because they don't include any of
10588 // the source in them. We create the error directly from the
10589 // message.
10590 VALUE message = rb_enc_str_new_cstr(error->message, rb_locale_encoding());
10591 VALUE value = rb_exc_new3(rb_eLoadError, message);
10592 rb_ivar_set(value, rb_intern_const("@path"), Qnil);
10593 return value;
10594 }
10595 }
10596 }
10597
10598 pm_buffer_append_format(
10599 &buffer,
10600 "%.*s:%" PRIi32 ": syntax error%s found\n",
10601 (int) pm_string_length(filepath),
10602 pm_string_source(filepath),
10603 (int32_t) pm_location_line_number(parser, &head->location),
10604 (parser->error_list.size > 1) ? "s" : ""
10605 );
10606
10607 if (valid_utf8) {
10608 pm_parse_errors_format(parser, &parser->error_list, &buffer, highlight, true);
10609 }
10610 else {
10611 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
10612 if (error != head) pm_buffer_append_byte(&buffer, '\n');
10613 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);
10614 }
10615 }
10616
10617 VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->node.encoding);
10618 VALUE error = rb_exc_new_str(rb_eSyntaxError, message);
10619
10620 rb_encoding *filepath_encoding = result->node.filepath_encoding != NULL ? result->node.filepath_encoding : rb_utf8_encoding();
10621 VALUE path = rb_enc_str_new((const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
10622
10623 rb_ivar_set(error, rb_intern_const("@path"), path);
10624 pm_buffer_free(&buffer);
10625
10626 return error;
10627}
10628
10634static VALUE
10635pm_parse_process(pm_parse_result_t *result, pm_node_t *node, VALUE *script_lines)
10636{
10637 pm_parser_t *parser = &result->parser;
10638
10639 // First, set up the scope node so that the AST node is attached and can be
10640 // freed regardless of whether or we return an error.
10641 pm_scope_node_t *scope_node = &result->node;
10642 rb_encoding *filepath_encoding = scope_node->filepath_encoding;
10643 int coverage_enabled = scope_node->coverage_enabled;
10644
10645 pm_scope_node_init(node, scope_node, NULL);
10646 scope_node->filepath_encoding = filepath_encoding;
10647
10648 scope_node->encoding = rb_enc_find(parser->encoding->name);
10649 if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
10650
10651 scope_node->coverage_enabled = coverage_enabled;
10652
10653 // If RubyVM.keep_script_lines is set to true, then we need to create that
10654 // array of script lines here.
10655 if (script_lines != NULL) {
10656 *script_lines = rb_ary_new_capa(parser->newline_list.size);
10657
10658 for (size_t index = 0; index < parser->newline_list.size; index++) {
10659 size_t offset = parser->newline_list.offsets[index];
10660 size_t length = index == parser->newline_list.size - 1 ? ((size_t) (parser->end - (parser->start + offset))) : (parser->newline_list.offsets[index + 1] - offset);
10661 rb_ary_push(*script_lines, rb_enc_str_new((const char *) parser->start + offset, length, scope_node->encoding));
10662 }
10663
10664 scope_node->script_lines = script_lines;
10665 }
10666
10667 // Emit all of the various warnings from the parse.
10668 const pm_diagnostic_t *warning;
10669 const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);
10670
10671 for (warning = (const pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (const pm_diagnostic_t *) warning->node.next) {
10672 int line = pm_location_line_number(parser, &warning->location);
10673
10674 if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
10675 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line, "%s", warning->message);
10676 }
10677 else {
10678 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line, "%s", warning->message);
10679 }
10680 }
10681
10682 // If there are errors, raise an appropriate error and free the result.
10683 if (parser->error_list.size > 0) {
10684 VALUE error = pm_parse_process_error(result);
10685
10686 // TODO: We need to set the backtrace.
10687 // rb_funcallv(error, rb_intern("set_backtrace"), 1, &path);
10688 return error;
10689 }
10690
10691 // Now set up the constant pool and intern all of the various constants into
10692 // their corresponding IDs.
10693 scope_node->parser = parser;
10694 scope_node->constants = xcalloc(parser->constant_pool.size, sizeof(ID));
10695
10696 for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
10697 pm_constant_t *constant = &parser->constant_pool.constants[index];
10698 scope_node->constants[index] = rb_intern3((const char *) constant->start, constant->length, scope_node->encoding);
10699 }
10700
10701 scope_node->index_lookup_table = st_init_numtable();
10702 pm_constant_id_list_t *locals = &scope_node->locals;
10703 for (size_t index = 0; index < locals->size; index++) {
10704 st_insert(scope_node->index_lookup_table, locals->ids[index], index);
10705 }
10706
10707 // If we got here, this is a success and we can return Qnil to indicate that
10708 // no error should be raised.
10709 result->parsed = true;
10710 return Qnil;
10711}
10712
10717static void
10718pm_options_frozen_string_literal_init(pm_options_t *options)
10719{
10720 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
10721
10722 switch (frozen_string_literal) {
10723 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
10724 break;
10725 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
10726 pm_options_frozen_string_literal_set(options, false);
10727 break;
10728 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
10729 pm_options_frozen_string_literal_set(options, true);
10730 break;
10731 default:
10732 rb_bug("pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
10733 break;
10734 }
10735}
10736
10741static inline VALUE
10742pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t *parser)
10743{
10744 const pm_newline_list_t *newline_list = &parser->newline_list;
10745 const char *start = (const char *) parser->start;
10746 const char *end = (const char *) parser->end;
10747
10748 // If we end exactly on a newline, then there's no need to push on a final
10749 // segment. If we don't, then we need to push on the last offset up to the
10750 // end of the string.
10751 size_t last_offset = newline_list->offsets[newline_list->size - 1];
10752 bool last_push = start + last_offset != end;
10753
10754 // Create the ruby strings that represent the lines of the source.
10755 VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
10756
10757 for (size_t index = 0; index < newline_list->size - 1; index++) {
10758 size_t offset = newline_list->offsets[index];
10759 size_t length = newline_list->offsets[index + 1] - offset;
10760
10761 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
10762 }
10763
10764 // Push on the last line if we need to.
10765 if (last_push) {
10766 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
10767 }
10768
10769 return lines;
10770}
10771
10772// This is essentially pm_string_mapped_init(), preferring to memory map the
10773// file, with additional handling for files that require blocking to properly
10774// read (e.g. pipes).
10776pm_read_file(pm_string_t *string, const char *filepath)
10777{
10778#ifdef _WIN32
10779 // Open the file for reading.
10780 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
10781 if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
10782
10783 WCHAR *wfilepath = xmalloc(sizeof(WCHAR) * ((size_t) length));
10784 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
10785 xfree(wfilepath);
10787 }
10788
10789 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
10790 if (file == INVALID_HANDLE_VALUE) {
10792
10793 if (GetLastError() == ERROR_ACCESS_DENIED) {
10794 DWORD attributes = GetFileAttributesW(wfilepath);
10795 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
10797 }
10798 }
10799
10800 xfree(wfilepath);
10801 return result;
10802 }
10803
10804 // Get the file size.
10805 DWORD file_size = GetFileSize(file, NULL);
10806 if (file_size == INVALID_FILE_SIZE) {
10807 CloseHandle(file);
10808 xfree(wfilepath);
10810 }
10811
10812 // If the file is empty, then we don't need to do anything else, we'll set
10813 // the source to a constant empty string and return.
10814 if (file_size == 0) {
10815 CloseHandle(file);
10816 xfree(wfilepath);
10817 const uint8_t source[] = "";
10818 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
10820 }
10821
10822 // Create a mapping of the file.
10823 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
10824 if (mapping == NULL) {
10825 CloseHandle(file);
10826 xfree(wfilepath);
10828 }
10829
10830 // Map the file into memory.
10831 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
10832 CloseHandle(mapping);
10833 CloseHandle(file);
10834 xfree(wfilepath);
10835
10836 if (source == NULL) {
10838 }
10839
10840 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
10842#elif defined(_POSIX_MAPPED_FILES)
10843 // Open the file for reading
10844 const int open_mode = O_RDONLY | O_NONBLOCK;
10845 int fd = open(filepath, open_mode);
10846 if (fd == -1) {
10848 }
10849
10850 // Stat the file to get the file size
10851 struct stat sb;
10852 if (fstat(fd, &sb) == -1) {
10853 close(fd);
10855 }
10856
10857 // Ensure it is a file and not a directory
10858 if (S_ISDIR(sb.st_mode)) {
10859 close(fd);
10861 }
10862
10863 // We need to wait for data first before reading from pipes and character
10864 // devices. To not block the entire VM, we need to release the GVL while
10865 // reading. Use IO#read to do this and let the GC handle closing the FD.
10866 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
10867 VALUE io = rb_io_fdopen((int) fd, open_mode, filepath);
10869 VALUE contents = rb_funcall(io, rb_intern("read"), 0);
10870
10871 if (!RB_TYPE_P(contents, T_STRING)) {
10873 }
10874
10875 long len = RSTRING_LEN(contents);
10876 if (len < 0) {
10878 }
10879
10880 size_t length = (size_t) len;
10881 uint8_t *source = malloc(length);
10882 memcpy(source, RSTRING_PTR(contents), length);
10883 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
10884
10886 }
10887
10888 // mmap the file descriptor to virtually get the contents
10889 size_t size = (size_t) sb.st_size;
10890 uint8_t *source = NULL;
10891
10892 if (size == 0) {
10893 close(fd);
10894 const uint8_t source[] = "";
10895 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
10897 }
10898
10899 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
10900 if (source == MAP_FAILED) {
10901 close(fd);
10903 }
10904
10905 close(fd);
10906 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
10908#else
10909 return pm_string_file_init(string, filepath);
10910#endif
10911}
10912
10917VALUE
10918pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
10919{
10920 pm_string_init_result_t init_result = pm_read_file(&result->input, RSTRING_PTR(filepath));
10921
10922 if (init_result == PM_STRING_INIT_SUCCESS) {
10923 pm_options_frozen_string_literal_init(&result->options);
10924 return Qnil;
10925 }
10926
10927 int err;
10928 if (init_result == PM_STRING_INIT_ERROR_DIRECTORY) {
10929 err = EISDIR;
10930 } else {
10931#ifdef _WIN32
10932 err = rb_w32_map_errno(GetLastError());
10933#else
10934 err = errno;
10935#endif
10936 }
10937
10938 VALUE error;
10939 if (load_error) {
10940 VALUE message = rb_str_buf_new_cstr(strerror(err));
10941 rb_str_cat2(message, " -- ");
10942 rb_str_append(message, filepath);
10943
10944 error = rb_exc_new3(rb_eLoadError, message);
10945 rb_ivar_set(error, rb_intern_const("@path"), filepath);
10946 } else {
10947 error = rb_syserr_new(err, RSTRING_PTR(filepath));
10948 RB_GC_GUARD(filepath);
10949 }
10950
10951 return error;
10952}
10953
10960VALUE
10961pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
10962{
10963 result->node.filepath_encoding = rb_enc_get(filepath);
10964 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
10965 RB_GC_GUARD(filepath);
10966
10967 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
10968 pm_node_t *node = pm_parse(&result->parser);
10969
10970 VALUE error = pm_parse_process(result, node, script_lines);
10971
10972 // If we're parsing a filepath, then we need to potentially support the
10973 // SCRIPT_LINES__ constant, which can be a hash that has an array of lines
10974 // of every read file.
10975 ID id_script_lines = rb_intern("SCRIPT_LINES__");
10976
10977 if (rb_const_defined_at(rb_cObject, id_script_lines)) {
10978 VALUE constant_script_lines = rb_const_get_at(rb_cObject, id_script_lines);
10979
10980 if (RB_TYPE_P(constant_script_lines, T_HASH)) {
10981 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->node, &result->parser));
10982 }
10983 }
10984
10985 return error;
10986}
10987
10992VALUE
10993pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
10994{
10995 VALUE error = pm_load_file(result, filepath, false);
10996 if (NIL_P(error)) {
10997 error = pm_parse_file(result, filepath, script_lines);
10998 }
10999
11000 return error;
11001}
11002
11009VALUE
11010pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines)
11011{
11012 rb_encoding *encoding = rb_enc_get(source);
11013 if (!rb_enc_asciicompat(encoding)) {
11014 return rb_exc_new_cstr(rb_eArgError, "invalid source encoding");
11015 }
11016
11017 pm_options_frozen_string_literal_init(&result->options);
11018 pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
11019 pm_options_encoding_set(&result->options, rb_enc_name(encoding));
11020
11021 result->node.filepath_encoding = rb_enc_get(filepath);
11022 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
11023 RB_GC_GUARD(filepath);
11024
11025 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
11026 pm_node_t *node = pm_parse(&result->parser);
11027
11028 return pm_parse_process(result, node, script_lines);
11029}
11030
11034static char *
11035pm_parse_stdin_fgets(char *string, int size, void *stream)
11036{
11037 RUBY_ASSERT(size > 0);
11038
11039 VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1));
11040 if (NIL_P(line)) {
11041 return NULL;
11042 }
11043
11044 const char *cstr = RSTRING_PTR(line);
11045 long length = RSTRING_LEN(line);
11046
11047 memcpy(string, cstr, length);
11048 string[length] = '\0';
11049
11050 return string;
11051}
11052
11053// We need access to this function when we're done parsing stdin.
11054void rb_reset_argf_lineno(long n);
11055
11061VALUE
11062pm_parse_stdin(pm_parse_result_t *result)
11063{
11064 pm_options_frozen_string_literal_init(&result->options);
11065
11066 pm_buffer_t buffer;
11067 pm_node_t *node = pm_parse_stream(&result->parser, &buffer, (void *) rb_stdin, pm_parse_stdin_fgets, &result->options);
11068
11069 // Copy the allocated buffer contents into the input string so that it gets
11070 // freed. At this point we've handed over ownership, so we don't need to
11071 // free the buffer itself.
11072 pm_string_owned_init(&result->input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11073
11074 // When we're done parsing, we reset $. because we don't want the fact that
11075 // we went through an IO object to be visible to the user.
11076 rb_reset_argf_lineno(0);
11077
11078 return pm_parse_process(result, node, NULL);
11079}
11080
11081#undef NEW_ISEQ
11082#define NEW_ISEQ OLD_ISEQ
11083
11084#undef NEW_CHILD_ISEQ
11085#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.
Definition assert.h:219
struct pm_block_parameter_node pm_block_parameter_node_t
BlockParameterNode.
struct pm_call_target_node pm_call_target_node_t
CallTargetNode.
struct pm_else_node pm_else_node_t
ElseNode.
struct pm_assoc_node pm_assoc_node_t
AssocNode.
struct pm_undef_node pm_undef_node_t
UndefNode.
struct pm_class_variable_and_write_node pm_class_variable_and_write_node_t
ClassVariableAndWriteNode.
struct pm_index_and_write_node pm_index_and_write_node_t
IndexAndWriteNode.
struct pm_index_target_node pm_index_target_node_t
IndexTargetNode.
struct pm_local_variable_target_node pm_local_variable_target_node_t
LocalVariableTargetNode.
struct pm_constant_path_or_write_node pm_constant_path_or_write_node_t
ConstantPathOrWriteNode.
struct pm_embedded_statements_node pm_embedded_statements_node_t
EmbeddedStatementsNode.
struct pm_block_node pm_block_node_t
BlockNode.
struct pm_hash_pattern_node pm_hash_pattern_node_t
HashPatternNode.
struct pm_optional_parameter_node pm_optional_parameter_node_t
OptionalParameterNode.
struct pm_x_string_node pm_x_string_node_t
XStringNode.
struct pm_forwarding_super_node pm_forwarding_super_node_t
ForwardingSuperNode.
struct pm_numbered_reference_read_node pm_numbered_reference_read_node_t
NumberedReferenceReadNode.
struct pm_embedded_variable_node pm_embedded_variable_node_t
EmbeddedVariableNode.
struct pm_class_variable_write_node pm_class_variable_write_node_t
ClassVariableWriteNode.
struct pm_interpolated_string_node pm_interpolated_string_node_t
InterpolatedStringNode.
struct pm_class_variable_or_write_node pm_class_variable_or_write_node_t
ClassVariableOrWriteNode.
struct pm_optional_keyword_parameter_node pm_optional_keyword_parameter_node_t
OptionalKeywordParameterNode.
struct pm_call_or_write_node pm_call_or_write_node_t
CallOrWriteNode.
struct pm_call_node pm_call_node_t
CallNode.
struct pm_class_variable_read_node pm_class_variable_read_node_t
ClassVariableReadNode.
struct pm_match_required_node pm_match_required_node_t
MatchRequiredNode.
struct pm_shareable_constant_node pm_shareable_constant_node_t
ShareableConstantNode.
struct pm_constant_and_write_node pm_constant_and_write_node_t
ConstantAndWriteNode.
@ PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE
mutable by virtue of a frozen_string_literal: false comment or --disable-frozen-string-literal; only ...
Definition ast.h:7822
@ PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN
frozen by virtue of a frozen_string_literal: true comment or --enable-frozen-string-literal; only for...
Definition ast.h:7819
struct pm_constant_path_operator_write_node pm_constant_path_operator_write_node_t
ConstantPathOperatorWriteNode.
@ PM_RANGE_FLAGS_EXCLUDE_END
... operator
Definition ast.h:7854
struct pm_local_variable_or_write_node pm_local_variable_or_write_node_t
LocalVariableOrWriteNode.
struct pm_local_variable_read_node pm_local_variable_read_node_t
LocalVariableReadNode.
struct pm_global_variable_and_write_node pm_global_variable_and_write_node_t
GlobalVariableAndWriteNode.
struct pm_arguments_node pm_arguments_node_t
ArgumentsNode.
pm_node_type
This enum represents every type of node in the Ruby syntax tree.
Definition ast.h:572
@ PM_DEFINED_NODE
DefinedNode.
Definition ast.h:709
@ PM_PRE_EXECUTION_NODE
PreExecutionNode.
Definition ast.h:931
@ PM_RETRY_NODE
RetryNode.
Definition ast.h:964
@ PM_REDO_NODE
RedoNode.
Definition ast.h:943
@ PM_CONSTANT_PATH_WRITE_NODE
ConstantPathWriteNode.
Definition ast.h:694
@ PM_INDEX_AND_WRITE_NODE
IndexAndWriteNode.
Definition ast.h:787
@ PM_SOURCE_LINE_NODE
SourceLineNode.
Definition ast.h:985
@ PM_UNLESS_NODE
UnlessNode.
Definition ast.h:1009
@ PM_EMBEDDED_VARIABLE_NODE
EmbeddedVariableNode.
Definition ast.h:718
@ PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE
GlobalVariableOperatorWriteNode.
Definition ast.h:751
@ PM_CALL_NODE
CallNode.
Definition ast.h:628
@ PM_NIL_NODE
NilNode.
Definition ast.h:895
@ PM_GLOBAL_VARIABLE_READ_NODE
GlobalVariableReadNode.
Definition ast.h:757
@ PM_RATIONAL_NODE
RationalNode.
Definition ast.h:940
@ PM_YIELD_NODE
YieldNode.
Definition ast.h:1024
@ PM_LOCAL_VARIABLE_AND_WRITE_NODE
LocalVariableAndWriteNode.
Definition ast.h:850
@ PM_CONSTANT_AND_WRITE_NODE
ConstantAndWriteNode.
Definition ast.h:670
@ PM_CLASS_NODE
ClassNode.
Definition ast.h:649
@ PM_FIND_PATTERN_NODE
FindPatternNode.
Definition ast.h:727
@ PM_CALL_OPERATOR_WRITE_NODE
CallOperatorWriteNode.
Definition ast.h:631
@ PM_MATCH_WRITE_NODE
MatchWriteNode.
Definition ast.h:877
@ PM_ARRAY_NODE
ArrayNode.
Definition ast.h:589
@ PM_CONSTANT_PATH_TARGET_NODE
ConstantPathTargetNode.
Definition ast.h:691
@ PM_PROGRAM_NODE
ProgramNode.
Definition ast.h:934
@ PM_OR_NODE
OrNode.
Definition ast.h:913
@ PM_MULTI_WRITE_NODE
MultiWriteNode.
Definition ast.h:889
@ PM_IF_NODE
IfNode.
Definition ast.h:772
@ PM_IMPLICIT_NODE
ImplicitNode.
Definition ast.h:778
@ PM_ARGUMENTS_NODE
ArgumentsNode.
Definition ast.h:586
@ PM_FORWARDING_SUPER_NODE
ForwardingSuperNode.
Definition ast.h:745
@ PM_WHILE_NODE
WhileNode.
Definition ast.h:1018
@ PM_INTERPOLATED_STRING_NODE
InterpolatedStringNode.
Definition ast.h:826
@ PM_FALSE_NODE
FalseNode.
Definition ast.h:724
@ PM_FORWARDING_PARAMETER_NODE
ForwardingParameterNode.
Definition ast.h:742
@ PM_HASH_NODE
HashNode.
Definition ast.h:766
@ PM_UNTIL_NODE
UntilNode.
Definition ast.h:1012
@ PM_MATCH_PREDICATE_NODE
MatchPredicateNode.
Definition ast.h:871
@ PM_X_STRING_NODE
XStringNode.
Definition ast.h:1021
@ PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE
LocalVariableOperatorWriteNode.
Definition ast.h:853
@ PM_LOCAL_VARIABLE_OR_WRITE_NODE
LocalVariableOrWriteNode.
Definition ast.h:856
@ PM_INSTANCE_VARIABLE_AND_WRITE_NODE
InstanceVariableAndWriteNode.
Definition ast.h:799
@ PM_GLOBAL_VARIABLE_TARGET_NODE
GlobalVariableTargetNode.
Definition ast.h:760
@ PM_AND_NODE
AndNode.
Definition ast.h:583
@ PM_CONSTANT_TARGET_NODE
ConstantTargetNode.
Definition ast.h:700
@ PM_IT_LOCAL_VARIABLE_READ_NODE
ItLocalVariableReadNode.
Definition ast.h:835
@ PM_CONSTANT_PATH_AND_WRITE_NODE
ConstantPathAndWriteNode.
Definition ast.h:679
@ PM_IN_NODE
InNode.
Definition ast.h:784
@ PM_CAPTURE_PATTERN_NODE
CapturePatternNode.
Definition ast.h:640
@ PM_SOURCE_FILE_NODE
SourceFileNode.
Definition ast.h:982
@ PM_NO_KEYWORDS_PARAMETER_NODE
NoKeywordsParameterNode.
Definition ast.h:898
@ PM_CONSTANT_PATH_OPERATOR_WRITE_NODE
ConstantPathOperatorWriteNode.
Definition ast.h:685
@ PM_MULTI_TARGET_NODE
MultiTargetNode.
Definition ast.h:886
@ PM_SPLAT_NODE
SplatNode.
Definition ast.h:988
@ PM_LAMBDA_NODE
LambdaNode.
Definition ast.h:847
@ PM_CLASS_VARIABLE_READ_NODE
ClassVariableReadNode.
Definition ast.h:661
@ PM_REQUIRED_KEYWORD_PARAMETER_NODE
RequiredKeywordParameterNode.
Definition ast.h:949
@ PM_CALL_TARGET_NODE
CallTargetNode.
Definition ast.h:637
@ PM_ELSE_NODE
ElseNode.
Definition ast.h:712
@ PM_INTERPOLATED_MATCH_LAST_LINE_NODE
InterpolatedMatchLastLineNode.
Definition ast.h:820
@ PM_NUMBERED_PARAMETERS_NODE
NumberedParametersNode.
Definition ast.h:901
@ PM_SYMBOL_NODE
SymbolNode.
Definition ast.h:1000
@ PM_RESCUE_MODIFIER_NODE
RescueModifierNode.
Definition ast.h:955
@ PM_ALIAS_METHOD_NODE
AliasMethodNode.
Definition ast.h:577
@ PM_MATCH_REQUIRED_NODE
MatchRequiredNode.
Definition ast.h:874
@ PM_FORWARDING_ARGUMENTS_NODE
ForwardingArgumentsNode.
Definition ast.h:739
@ PM_BACK_REFERENCE_READ_NODE
BackReferenceReadNode.
Definition ast.h:601
@ PM_SCOPE_NODE
A special kind of node used for compilation.
Definition ast.h:1027
@ PM_BLOCK_ARGUMENT_NODE
BlockArgumentNode.
Definition ast.h:607
@ PM_MISSING_NODE
MissingNode.
Definition ast.h:880
@ PM_SELF_NODE
SelfNode.
Definition ast.h:970
@ PM_IMPLICIT_REST_NODE
ImplicitRestNode.
Definition ast.h:781
@ PM_TRUE_NODE
TrueNode.
Definition ast.h:1003
@ PM_ASSOC_SPLAT_NODE
AssocSplatNode.
Definition ast.h:598
@ PM_CLASS_VARIABLE_AND_WRITE_NODE
ClassVariableAndWriteNode.
Definition ast.h:652
@ PM_RANGE_NODE
RangeNode.
Definition ast.h:937
@ PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE
InstanceVariableOperatorWriteNode.
Definition ast.h:802
@ PM_LOCAL_VARIABLE_READ_NODE
LocalVariableReadNode.
Definition ast.h:859
@ PM_SHAREABLE_CONSTANT_NODE
ShareableConstantNode.
Definition ast.h:973
@ PM_NEXT_NODE
NextNode.
Definition ast.h:892
@ PM_INSTANCE_VARIABLE_OR_WRITE_NODE
InstanceVariableOrWriteNode.
Definition ast.h:805
@ PM_REGULAR_EXPRESSION_NODE
RegularExpressionNode.
Definition ast.h:946
@ PM_CLASS_VARIABLE_OR_WRITE_NODE
ClassVariableOrWriteNode.
Definition ast.h:658
@ PM_BLOCK_PARAMETERS_NODE
BlockParametersNode.
Definition ast.h:619
@ PM_CONSTANT_WRITE_NODE
ConstantWriteNode.
Definition ast.h:703
@ PM_HASH_PATTERN_NODE
HashPatternNode.
Definition ast.h:769
@ PM_INDEX_OPERATOR_WRITE_NODE
IndexOperatorWriteNode.
Definition ast.h:790
@ PM_UNDEF_NODE
UndefNode.
Definition ast.h:1006
@ PM_ALTERNATION_PATTERN_NODE
AlternationPatternNode.
Definition ast.h:580
@ PM_ENSURE_NODE
EnsureNode.
Definition ast.h:721
@ PM_LOCAL_VARIABLE_WRITE_NODE
LocalVariableWriteNode.
Definition ast.h:865
@ PM_SINGLETON_CLASS_NODE
SingletonClassNode.
Definition ast.h:976
@ PM_KEYWORD_HASH_NODE
KeywordHashNode.
Definition ast.h:841
@ PM_PARENTHESES_NODE
ParenthesesNode.
Definition ast.h:919
@ PM_FOR_NODE
ForNode.
Definition ast.h:736
@ PM_CLASS_VARIABLE_WRITE_NODE
ClassVariableWriteNode.
Definition ast.h:667
@ PM_POST_EXECUTION_NODE
PostExecutionNode.
Definition ast.h:928
@ PM_CONSTANT_OPERATOR_WRITE_NODE
ConstantOperatorWriteNode.
Definition ast.h:673
@ PM_RETURN_NODE
ReturnNode.
Definition ast.h:967
@ PM_MODULE_NODE
ModuleNode.
Definition ast.h:883
@ PM_ARRAY_PATTERN_NODE
ArrayPatternNode.
Definition ast.h:592
@ PM_SUPER_NODE
SuperNode.
Definition ast.h:997
@ PM_MATCH_LAST_LINE_NODE
MatchLastLineNode.
Definition ast.h:868
@ PM_CONSTANT_PATH_NODE
ConstantPathNode.
Definition ast.h:682
@ PM_INTERPOLATED_SYMBOL_NODE
InterpolatedSymbolNode.
Definition ast.h:829
@ PM_CALL_AND_WRITE_NODE
CallAndWriteNode.
Definition ast.h:625
@ PM_OPTIONAL_KEYWORD_PARAMETER_NODE
OptionalKeywordParameterNode.
Definition ast.h:907
@ PM_CLASS_VARIABLE_TARGET_NODE
ClassVariableTargetNode.
Definition ast.h:664
@ PM_CASE_MATCH_NODE
CaseMatchNode.
Definition ast.h:643
@ PM_BREAK_NODE
BreakNode.
Definition ast.h:622
@ PM_CALL_OR_WRITE_NODE
CallOrWriteNode.
Definition ast.h:634
@ PM_IMAGINARY_NODE
ImaginaryNode.
Definition ast.h:775
@ PM_DEF_NODE
DefNode.
Definition ast.h:706
@ PM_CONSTANT_READ_NODE
ConstantReadNode.
Definition ast.h:697
@ PM_GLOBAL_VARIABLE_WRITE_NODE
GlobalVariableWriteNode.
Definition ast.h:763
@ PM_SOURCE_ENCODING_NODE
SourceEncodingNode.
Definition ast.h:979
@ PM_BEGIN_NODE
BeginNode.
Definition ast.h:604
@ PM_INTERPOLATED_X_STRING_NODE
InterpolatedXStringNode.
Definition ast.h:832
@ PM_INSTANCE_VARIABLE_READ_NODE
InstanceVariableReadNode.
Definition ast.h:808
@ PM_FLIP_FLOP_NODE
FlipFlopNode.
Definition ast.h:730
@ PM_PINNED_VARIABLE_NODE
PinnedVariableNode.
Definition ast.h:925
@ PM_REQUIRED_PARAMETER_NODE
RequiredParameterNode.
Definition ast.h:952
@ PM_INSTANCE_VARIABLE_WRITE_NODE
InstanceVariableWriteNode.
Definition ast.h:814
@ PM_INSTANCE_VARIABLE_TARGET_NODE
InstanceVariableTargetNode.
Definition ast.h:811
@ PM_GLOBAL_VARIABLE_AND_WRITE_NODE
GlobalVariableAndWriteNode.
Definition ast.h:748
@ PM_CASE_NODE
CaseNode.
Definition ast.h:646
@ PM_RESCUE_NODE
RescueNode.
Definition ast.h:958
@ PM_FLOAT_NODE
FloatNode.
Definition ast.h:733
@ PM_ASSOC_NODE
AssocNode.
Definition ast.h:595
@ PM_IT_PARAMETERS_NODE
ItParametersNode.
Definition ast.h:838
@ PM_INTEGER_NODE
IntegerNode.
Definition ast.h:817
@ PM_LOCAL_VARIABLE_TARGET_NODE
LocalVariableTargetNode.
Definition ast.h:862
@ PM_STRING_NODE
StringNode.
Definition ast.h:994
@ PM_INDEX_OR_WRITE_NODE
IndexOrWriteNode.
Definition ast.h:793
@ PM_ALIAS_GLOBAL_VARIABLE_NODE
AliasGlobalVariableNode.
Definition ast.h:574
@ PM_PARAMETERS_NODE
ParametersNode.
Definition ast.h:916
@ PM_NUMBERED_REFERENCE_READ_NODE
NumberedReferenceReadNode.
Definition ast.h:904
@ PM_CONSTANT_PATH_OR_WRITE_NODE
ConstantPathOrWriteNode.
Definition ast.h:688
@ PM_GLOBAL_VARIABLE_OR_WRITE_NODE
GlobalVariableOrWriteNode.
Definition ast.h:754
@ PM_CONSTANT_OR_WRITE_NODE
ConstantOrWriteNode.
Definition ast.h:676
@ PM_STATEMENTS_NODE
StatementsNode.
Definition ast.h:991
@ PM_OPTIONAL_PARAMETER_NODE
OptionalParameterNode.
Definition ast.h:910
@ PM_PINNED_EXPRESSION_NODE
PinnedExpressionNode.
Definition ast.h:922
@ PM_BLOCK_NODE
BlockNode.
Definition ast.h:613
@ PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE
ClassVariableOperatorWriteNode.
Definition ast.h:655
@ PM_EMBEDDED_STATEMENTS_NODE
EmbeddedStatementsNode.
Definition ast.h:715
@ PM_INTERPOLATED_REGULAR_EXPRESSION_NODE
InterpolatedRegularExpressionNode.
Definition ast.h:823
@ PM_INDEX_TARGET_NODE
IndexTargetNode.
Definition ast.h:796
@ PM_KEYWORD_REST_PARAMETER_NODE
KeywordRestParameterNode.
Definition ast.h:844
struct pm_begin_node pm_begin_node_t
BeginNode.
struct pm_statements_node pm_statements_node_t
StatementsNode.
struct pm_instance_variable_write_node pm_instance_variable_write_node_t
InstanceVariableWriteNode.
struct pm_keyword_hash_node pm_keyword_hash_node_t
KeywordHashNode.
struct pm_return_node pm_return_node_t
ReturnNode.
static const pm_node_flags_t PM_NODE_FLAG_NEWLINE
We store the flags enum in every node in the tree.
Definition ast.h:1046
@ PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING
internal bytes forced the encoding to UTF-8
Definition ast.h:7931
@ PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING
internal bytes forced the encoding to US-ASCII
Definition ast.h:7937
@ PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING
internal bytes forced the encoding to binary
Definition ast.h:7934
struct pm_constant_path_node pm_constant_path_node_t
ConstantPathNode.
struct pm_local_variable_write_node pm_local_variable_write_node_t
LocalVariableWriteNode.
@ PM_STRING_FLAGS_FROZEN
frozen by virtue of a frozen_string_literal: true comment or --enable-frozen-string-literal
Definition ast.h:7920
@ PM_STRING_FLAGS_FORCED_BINARY_ENCODING
internal bytes forced the encoding to binary
Definition ast.h:7917
@ PM_STRING_FLAGS_MUTABLE
mutable by virtue of a frozen_string_literal: false comment or --disable-frozen-string-literal
Definition ast.h:7923
@ PM_STRING_FLAGS_FORCED_UTF8_ENCODING
internal bytes forced the encoding to UTF-8
Definition ast.h:7914
struct pm_implicit_node pm_implicit_node_t
ImplicitNode.
struct pm_yield_node pm_yield_node_t
YieldNode.
@ PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT
if the arguments contain a splat
Definition ast.h:7755
@ PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING
if the arguments contain forwarding
Definition ast.h:7746
@ PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT
if the arguments contain a keyword splat
Definition ast.h:7752
@ PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS
if the arguments contain multiple splats
Definition ast.h:7758
struct pm_local_variable_and_write_node pm_local_variable_and_write_node_t
LocalVariableAndWriteNode.
struct pm_parameters_node pm_parameters_node_t
ParametersNode.
struct pm_lambda_node pm_lambda_node_t
LambdaNode.
#define PM_NODE_FLAG_P(node, flag)
Return true if the given flag is set on the given node.
Definition ast.h:1063
struct pm_module_node pm_module_node_t
ModuleNode.
struct pm_case_node pm_case_node_t
CaseNode.
struct pm_in_node pm_in_node_t
InNode.
struct pm_if_node pm_if_node_t
IfNode.
struct pm_constant_path_write_node pm_constant_path_write_node_t
ConstantPathWriteNode.
struct pm_pre_execution_node pm_pre_execution_node_t
PreExecutionNode.
struct pm_rescue_modifier_node pm_rescue_modifier_node_t
RescueModifierNode.
struct pm_splat_node pm_splat_node_t
SplatNode.
struct pm_match_write_node pm_match_write_node_t
MatchWriteNode.
struct pm_multi_write_node pm_multi_write_node_t
MultiWriteNode.
struct pm_local_variable_operator_write_node pm_local_variable_operator_write_node_t
LocalVariableOperatorWriteNode.
struct pm_block_argument_node pm_block_argument_node_t
BlockArgumentNode.
struct pm_interpolated_x_string_node pm_interpolated_x_string_node_t
InterpolatedXStringNode.
struct pm_constant_write_node pm_constant_write_node_t
ConstantWriteNode.
struct pm_flip_flop_node pm_flip_flop_node_t
FlipFlopNode.
struct pm_required_keyword_parameter_node pm_required_keyword_parameter_node_t
RequiredKeywordParameterNode.
#define PM_NODE_TYPE_P(node, type)
Return true if the type of the given node matches the given type.
Definition ast.h:1058
#define PM_NODE_TYPE(node)
Cast the type to an enum to allow the compiler to provide exhaustiveness checking.
Definition ast.h:1053
struct pm_alias_global_variable_node pm_alias_global_variable_node_t
AliasGlobalVariableNode.
struct pm_post_execution_node pm_post_execution_node_t
PostExecutionNode.
struct pm_alias_method_node pm_alias_method_node_t
AliasMethodNode.
struct pm_keyword_rest_parameter_node pm_keyword_rest_parameter_node_t
KeywordRestParameterNode.
struct pm_global_variable_read_node pm_global_variable_read_node_t
GlobalVariableReadNode.
struct pm_match_last_line_node pm_match_last_line_node_t
MatchLastLineNode.
struct pm_hash_node pm_hash_node_t
HashNode.
struct pm_block_local_variable_node pm_block_local_variable_node_t
BlockLocalVariableNode.
struct pm_multi_target_node pm_multi_target_node_t
MultiTargetNode.
struct pm_rational_node pm_rational_node_t
RationalNode.
struct pm_class_node pm_class_node_t
ClassNode.
struct pm_pinned_expression_node pm_pinned_expression_node_t
PinnedExpressionNode.
struct pm_constant_operator_write_node pm_constant_operator_write_node_t
ConstantOperatorWriteNode.
struct pm_ensure_node pm_ensure_node_t
EnsureNode.
struct pm_index_or_write_node pm_index_or_write_node_t
IndexOrWriteNode.
struct pm_constant_or_write_node pm_constant_or_write_node_t
ConstantOrWriteNode.
struct pm_index_operator_write_node pm_index_operator_write_node_t
IndexOperatorWriteNode.
struct pm_when_node pm_when_node_t
WhenNode.
struct pm_super_node pm_super_node_t
SuperNode.
struct pm_range_node pm_range_node_t
RangeNode.
struct pm_and_node pm_and_node_t
AndNode.
struct pm_constant_path_and_write_node pm_constant_path_and_write_node_t
ConstantPathAndWriteNode.
struct pm_rest_parameter_node pm_rest_parameter_node_t
RestParameterNode.
struct pm_assoc_splat_node pm_assoc_splat_node_t
AssocSplatNode.
@ PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY
a call that ignores method visibility
Definition ast.h:7783
@ PM_CALL_NODE_FLAGS_SAFE_NAVIGATION
&.
Definition ast.h:7774
@ PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE
a call that is an attribute write, so the value being written should be returned
Definition ast.h:7780
@ PM_CALL_NODE_FLAGS_VARIABLE_CALL
a call that could have been a local variable
Definition ast.h:7777
struct pm_constant_read_node pm_constant_read_node_t
ConstantReadNode.
struct pm_match_predicate_node pm_match_predicate_node_t
MatchPredicateNode.
struct pm_or_node pm_or_node_t
OrNode.
struct pm_case_match_node pm_case_match_node_t
CaseMatchNode.
struct pm_call_and_write_node pm_call_and_write_node_t
CallAndWriteNode.
@ PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING
constant writes that should be modified with shareable constant value experimental everything
Definition ast.h:7903
@ PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL
constant writes that should be modified with shareable constant value literal
Definition ast.h:7900
@ PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY
constant writes that should be modified with shareable constant value experimental copy
Definition ast.h:7906
struct pm_until_node pm_until_node_t
UntilNode.
uint16_t pm_node_type_t
This is the type of node embedded in the node struct.
Definition ast.h:1034
struct pm_imaginary_node pm_imaginary_node_t
ImaginaryNode.
struct pm_array_pattern_node pm_array_pattern_node_t
ArrayPatternNode.
@ PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS
a keyword hash which only has AssocNode elements all with symbol keys, which means the elements can b...
Definition ast.h:7830
struct pm_break_node pm_break_node_t
BreakNode.
struct pm_integer_node pm_integer_node_t
IntegerNode.
struct pm_constant_path_target_node pm_constant_path_target_node_t
ConstantPathTargetNode.
struct pm_call_operator_write_node pm_call_operator_write_node_t
CallOperatorWriteNode.
struct pm_for_node pm_for_node_t
ForNode.
struct pm_global_variable_target_node pm_global_variable_target_node_t
GlobalVariableTargetNode.
struct pm_node_list pm_node_list_t
A list of nodes in the source, most often used for lists of children.
struct pm_required_parameter_node pm_required_parameter_node_t
RequiredParameterNode.
struct pm_symbol_node pm_symbol_node_t
SymbolNode.
struct pm_block_parameters_node pm_block_parameters_node_t
BlockParametersNode.
struct pm_alternation_pattern_node pm_alternation_pattern_node_t
AlternationPatternNode.
struct pm_parentheses_node pm_parentheses_node_t
ParenthesesNode.
@ PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING
internal bytes forced the encoding to binary
Definition ast.h:7889
@ PM_REGULAR_EXPRESSION_FLAGS_EUC_JP
e - forces the EUC-JP encoding
Definition ast.h:7874
@ PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE
i - ignores the case of characters when matching
Definition ast.h:7862
@ PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT
n - forces the ASCII-8BIT encoding
Definition ast.h:7877
@ PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE
m - allows $ to match the end of lines within strings
Definition ast.h:7868
@ PM_REGULAR_EXPRESSION_FLAGS_EXTENDED
x - ignores whitespace and allows comments in regular expressions
Definition ast.h:7865
@ PM_REGULAR_EXPRESSION_FLAGS_ONCE
o - only interpolates values into the regular expression once
Definition ast.h:7871
@ PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J
s - forces the Windows-31J encoding
Definition ast.h:7880
@ PM_REGULAR_EXPRESSION_FLAGS_UTF_8
u - forces the UTF-8 encoding
Definition ast.h:7883
struct pm_instance_variable_read_node pm_instance_variable_read_node_t
InstanceVariableReadNode.
struct pm_constant_target_node pm_constant_target_node_t
ConstantTargetNode.
struct pm_node pm_node_t
This is the base structure that represents a node in the syntax tree.
struct pm_defined_node pm_defined_node_t
DefinedNode.
struct pm_interpolated_symbol_node pm_interpolated_symbol_node_t
InterpolatedSymbolNode.
struct pm_class_variable_target_node pm_class_variable_target_node_t
ClassVariableTargetNode.
struct pm_def_node pm_def_node_t
DefNode.
struct pm_singleton_class_node pm_singleton_class_node_t
SingletonClassNode.
uint16_t pm_node_flags_t
These are the flags embedded in the node struct.
Definition ast.h:1040
struct pm_capture_pattern_node pm_capture_pattern_node_t
CapturePatternNode.
struct pm_source_file_node pm_source_file_node_t
SourceFileNode.
struct pm_regular_expression_node pm_regular_expression_node_t
RegularExpressionNode.
struct pm_global_variable_or_write_node pm_global_variable_or_write_node_t
GlobalVariableOrWriteNode.
struct pm_rescue_node pm_rescue_node_t
RescueNode.
struct pm_array_node pm_array_node_t
ArrayNode.
struct pm_while_node pm_while_node_t
WhileNode.
struct pm_global_variable_write_node pm_global_variable_write_node_t
GlobalVariableWriteNode.
struct pm_instance_variable_or_write_node pm_instance_variable_or_write_node_t
InstanceVariableOrWriteNode.
@ PM_PARAMETER_FLAGS_REPEATED_PARAMETER
a parameter name that has been repeated in the method signature
Definition ast.h:7846
@ PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING
internal bytes forced the encoding to binary
Definition ast.h:7794
@ PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING
internal bytes forced the encoding to UTF-8
Definition ast.h:7791
struct pm_interpolated_match_last_line_node pm_interpolated_match_last_line_node_t
InterpolatedMatchLastLineNode.
struct pm_numbered_parameters_node pm_numbered_parameters_node_t
NumberedParametersNode.
struct pm_class_variable_operator_write_node pm_class_variable_operator_write_node_t
ClassVariableOperatorWriteNode.
struct pm_next_node pm_next_node_t
NextNode.
struct pm_unless_node pm_unless_node_t
UnlessNode.
struct pm_interpolated_regular_expression_node pm_interpolated_regular_expression_node_t
InterpolatedRegularExpressionNode.
struct pm_instance_variable_target_node pm_instance_variable_target_node_t
InstanceVariableTargetNode.
struct pm_string_node pm_string_node_t
StringNode.
struct pm_float_node pm_float_node_t
FloatNode.
struct pm_global_variable_operator_write_node pm_global_variable_operator_write_node_t
GlobalVariableOperatorWriteNode.
struct pm_instance_variable_operator_write_node pm_instance_variable_operator_write_node_t
InstanceVariableOperatorWriteNode.
@ PM_LOOP_FLAGS_BEGIN_MODIFIER
a loop after a begin statement, so the body is executed first before the condition
Definition ast.h:7838
struct pm_pinned_variable_node pm_pinned_variable_node_t
PinnedVariableNode.
struct pm_instance_variable_and_write_node pm_instance_variable_and_write_node_t
InstanceVariableAndWriteNode.
struct pm_program_node pm_program_node_t
ProgramNode.
struct pm_find_pattern_node pm_find_pattern_node_t
FindPatternNode.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
Definition diagnostic.h:408
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
Definition diagnostic.h:394
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
Definition diagnostic.h:397
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
Definition diagnostic.h:391
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:40
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:56
#define RUBY_EVENT_CLASS
Encountered a new class.
Definition event.h:39
#define RUBY_EVENT_LINE
Encountered a new line.
Definition event.h:38
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:42
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
Definition event.h:55
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
Definition event.h:41
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
Definition event.h:61
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:404
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1683
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:135
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:401
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define UINT2NUM
Old name of RB_UINT2NUM.
Definition int.h:46
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition error.h:486
VALUE rb_eNotImpError
NotImplementedError exception.
Definition error.c:1440
VALUE rb_eStandardError
StandardError exception.
Definition error.c:1427
VALUE rb_eLoadError
LoadError exception.
Definition error.c:1448
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
Definition error.c:1443
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition error.c:1468
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
Definition error.c:1444
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.
Definition error.c:1481
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1447
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
Definition error.c:3863
VALUE rb_cArray
Array class.
Definition array.c:40
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:104
VALUE rb_stdin
STDIN constant.
Definition io.c:201
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1260
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:900
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.
Definition string.c:12520
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.
Definition string.c:1098
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1099
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
Definition io.c:9332
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
Definition range.c:68
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
Definition rational.c:1974
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3676
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1671
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
#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.
Definition string.h:1670
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1639
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:3918
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3176
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1514
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.
Definition variable.c:1871
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3169
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3491
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:951
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:970
@ RUBY_IO_READABLE
IO::READABLE
Definition io.h:82
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
Definition io.c:1447
int len
Length of the buffer.
Definition io.h:8
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:3115
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
#define RB_INT2NUM
Just another name of rb_int2num_inline.
Definition int.h:37
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE type(ANYARGS)
ANYARGS-ed function type.
struct pm_options pm_options_t
The options that can be passed to the parser.
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
Definition parser.h:267
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
struct pm_list_node pm_list_node_t
This struct represents an abstract linked list that provides common functionality.
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
Definition pm_string.h:105
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
Definition pm_string.h:107
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
Definition pm_string.h:112
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition pm_string.h:116
#define PM_ENCODING_US_ASCII_ENTRY
This is the US-ASCII encoding.
Definition encoding.h:252
#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.
Definition node.h:17
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RTEST
This is an old name of RB_TEST.
struct pm_node * old_name
AliasGlobalVariableNode#old_name.
Definition ast.h:1130
struct pm_node * new_name
AliasGlobalVariableNode#new_name.
Definition ast.h:1120
struct pm_node * old_name
AliasMethodNode#old_name.
Definition ast.h:1190
struct pm_node * new_name
AliasMethodNode#new_name.
Definition ast.h:1174
struct pm_node * left
AlternationPatternNode#left.
Definition ast.h:1228
struct pm_node * right
AlternationPatternNode#right.
Definition ast.h:1238
struct pm_node * left
AndNode#left.
Definition ast.h:1279
struct pm_node * right
AndNode#right.
Definition ast.h:1292
pm_node_t base
The embedded base node.
Definition ast.h:1326
struct pm_node_list arguments
ArgumentsNode#arguments.
Definition ast.h:1337
struct pm_node_list elements
ArrayNode#elements.
Definition ast.h:1365
struct pm_node_list requireds
ArrayPatternNode#requireds.
Definition ast.h:1434
struct pm_node * rest
ArrayPatternNode#rest.
Definition ast.h:1444
struct pm_node * constant
ArrayPatternNode#constant.
Definition ast.h:1424
struct pm_node_list posts
ArrayPatternNode#posts.
Definition ast.h:1454
struct pm_node * value
AssocNode#value.
Definition ast.h:1521
struct pm_node * key
AssocNode#key.
Definition ast.h:1508
struct pm_node * value
AssocSplatNode#value.
Definition ast.h:1559
struct pm_ensure_node * ensure_clause
BeginNode#ensure_clause.
Definition ast.h:1668
struct pm_rescue_node * rescue_clause
BeginNode#rescue_clause.
Definition ast.h:1648
struct pm_statements_node * statements
BeginNode#statements.
Definition ast.h:1638
struct pm_else_node * else_clause
BeginNode#else_clause.
Definition ast.h:1658
struct pm_node * expression
BlockArgumentNode#expression.
Definition ast.h:1706
struct pm_node * parameters
BlockNode#parameters.
Definition ast.h:1789
struct pm_node * body
BlockNode#body.
Definition ast.h:1799
pm_constant_id_list_t locals
BlockNode#locals.
Definition ast.h:1775
struct pm_arguments_node * arguments
BreakNode#arguments.
Definition ast.h:1979
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition pm_buffer.h:22
struct pm_node * value
CallAndWriteNode#value.
Definition ast.h:2083
pm_constant_id_t read_name
CallAndWriteNode#read_name.
Definition ast.h:2053
pm_constant_id_t write_name
CallAndWriteNode#write_name.
Definition ast.h:2063
struct pm_node * receiver
CallAndWriteNode#receiver.
Definition ast.h:2023
pm_location_t closing_loc
CallNode#closing_loc.
Definition ast.h:2200
struct pm_node * receiver
CallNode#receiver.
Definition ast.h:2138
pm_constant_id_t name
CallNode::name.
Definition ast.h:2161
pm_node_t base
The embedded base node.
Definition ast.h:2121
pm_location_t message_loc
CallNode#message_loc.
Definition ast.h:2171
struct pm_arguments_node * arguments
CallNode#arguments.
Definition ast.h:2190
struct pm_node * block
CallNode#block.
Definition ast.h:2210
pm_constant_id_t read_name
CallOperatorWriteNode#read_name.
Definition ast.h:2274
pm_constant_id_t binary_operator
CallOperatorWriteNode#binary_operator.
Definition ast.h:2294
struct pm_node * receiver
CallOperatorWriteNode#receiver.
Definition ast.h:2244
pm_constant_id_t write_name
CallOperatorWriteNode#write_name.
Definition ast.h:2284
struct pm_node * value
CallOperatorWriteNode#value.
Definition ast.h:2314
struct pm_node * receiver
CallOrWriteNode#receiver.
Definition ast.h:2348
struct pm_node * value
CallOrWriteNode#value.
Definition ast.h:2408
pm_constant_id_t write_name
CallOrWriteNode#write_name.
Definition ast.h:2388
pm_constant_id_t read_name
CallOrWriteNode#read_name.
Definition ast.h:2378
pm_constant_id_t name
CallTargetNode#name.
Definition ast.h:2470
struct pm_node * receiver
CallTargetNode#receiver.
Definition ast.h:2450
struct pm_local_variable_target_node * target
CapturePatternNode#target.
Definition ast.h:2518
struct pm_node * value
CapturePatternNode#value.
Definition ast.h:2508
struct pm_node_list conditions
CaseMatchNode#conditions.
Definition ast.h:2568
struct pm_else_node * else_clause
CaseMatchNode#else_clause.
Definition ast.h:2578
struct pm_node * predicate
CaseMatchNode#predicate.
Definition ast.h:2558
struct pm_node * predicate
CaseNode#predicate.
Definition ast.h:2628
struct pm_else_node * else_clause
CaseNode#else_clause.
Definition ast.h:2648
struct pm_node_list conditions
CaseNode#conditions.
Definition ast.h:2638
struct pm_node * constant_path
ClassNode#constant_path.
Definition ast.h:2701
pm_constant_id_list_t locals
ClassNode#locals.
Definition ast.h:2691
pm_constant_id_t name
ClassNode#name.
Definition ast.h:2726
struct pm_node * body
ClassNode#body.
Definition ast.h:2716
struct pm_node * superclass
ClassNode#superclass.
Definition ast.h:2711
struct pm_node * value
ClassVariableAndWriteNode#value.
Definition ast.h:2784
pm_constant_id_t name
ClassVariableAndWriteNode#name.
Definition ast.h:2754
pm_constant_id_t name
ClassVariableOperatorWriteNode#name.
Definition ast.h:2807
pm_constant_id_t binary_operator
ClassVariableOperatorWriteNode#binary_operator.
Definition ast.h:2827
struct pm_node * value
ClassVariableOperatorWriteNode#value.
Definition ast.h:2822
pm_constant_id_t name
ClassVariableOrWriteNode#name.
Definition ast.h:2850
struct pm_node * value
ClassVariableOrWriteNode#value.
Definition ast.h:2865
pm_constant_id_t name
ClassVariableReadNode#name.
Definition ast.h:2894
pm_constant_id_t name
ClassVariableTargetNode#name.
Definition ast.h:2917
struct pm_node * value
ClassVariableWriteNode#value.
Definition ast.h:2969
pm_constant_id_t name
ClassVariableWriteNode#name.
Definition ast.h:2946
pm_location_t name_loc
ConstantAndWriteNode#name_loc.
Definition ast.h:3007
pm_constant_id_t name
ConstantAndWriteNode#name.
Definition ast.h:3002
struct pm_node * value
ConstantAndWriteNode#value.
Definition ast.h:3017
A list of constant IDs.
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.
pm_constant_id_t name
ConstantOperatorWriteNode#name.
Definition ast.h:3040
pm_location_t name_loc
ConstantOperatorWriteNode#name_loc.
Definition ast.h:3045
pm_constant_id_t binary_operator
ConstantOperatorWriteNode#binary_operator.
Definition ast.h:3060
struct pm_node * value
ConstantOperatorWriteNode#value.
Definition ast.h:3055
pm_location_t name_loc
ConstantOrWriteNode#name_loc.
Definition ast.h:3088
pm_constant_id_t name
ConstantOrWriteNode#name.
Definition ast.h:3083
struct pm_node * value
ConstantOrWriteNode#value.
Definition ast.h:3098
struct pm_constant_path_node * target
ConstantPathAndWriteNode#target.
Definition ast.h:3121
struct pm_node * value
ConstantPathAndWriteNode#value.
Definition ast.h:3131
pm_constant_id_t name
ConstantPathNode#name.
Definition ast.h:3172
struct pm_node * parent
ConstantPathNode#parent.
Definition ast.h:3165
struct pm_constant_path_node * target
ConstantPathOperatorWriteNode#target.
Definition ast.h:3221
struct pm_node * value
ConstantPathOperatorWriteNode#value.
Definition ast.h:3231
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode#binary_operator.
Definition ast.h:3236
struct pm_node * value
ConstantPathOrWriteNode#value.
Definition ast.h:3269
struct pm_constant_path_node * target
ConstantPathOrWriteNode#target.
Definition ast.h:3259
struct pm_node * parent
ConstantPathTargetNode#parent.
Definition ast.h:3292
pm_constant_id_t name
ConstantPathTargetNode#name.
Definition ast.h:3297
struct pm_constant_path_node * target
ConstantPathWriteNode#target.
Definition ast.h:3344
struct pm_node * value
ConstantPathWriteNode#value.
Definition ast.h:3364
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.
Definition ast.h:3381
pm_constant_id_t name
ConstantReadNode#name.
Definition ast.h:3393
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.
Definition ast.h:3416
struct pm_node * value
ConstantWriteNode#value.
Definition ast.h:3468
pm_constant_id_t name
ConstantWriteNode#name.
Definition ast.h:3445
struct pm_parameters_node * parameters
DefNode#parameters.
Definition ast.h:3517
pm_constant_id_t name
DefNode#name.
Definition ast.h:3502
struct pm_node * body
DefNode#body.
Definition ast.h:3522
struct pm_node * receiver
DefNode#receiver.
Definition ast.h:3512
pm_node_t base
The embedded base node.
Definition ast.h:3496
pm_constant_id_list_t locals
DefNode#locals.
Definition ast.h:3527
struct pm_node * value
DefinedNode#value.
Definition ast.h:3585
This struct represents a diagnostic generated during parsing.
Definition diagnostic.h:359
pm_location_t location
The location of the diagnostic in the source.
Definition diagnostic.h:364
const char * message
The message associated with the diagnostic.
Definition diagnostic.h:370
pm_list_node_t node
The embedded base node.
Definition diagnostic.h:361
uint8_t level
The level of the diagnostic, see pm_error_level_t and pm_warning_level_t for possible values.
Definition diagnostic.h:383
struct pm_statements_node * statements
ElseNode#statements.
Definition ast.h:3623
struct pm_statements_node * statements
EmbeddedStatementsNode#statements.
Definition ast.h:3656
struct pm_node * variable
EmbeddedVariableNode#variable.
Definition ast.h:3689
This struct defines the functions necessary to implement the encoding interface so we can determine h...
Definition encoding.h:23
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.
Definition encoding.h:29
const char * name
The name of the encoding.
Definition encoding.h:56
struct pm_statements_node * statements
EnsureNode#statements.
Definition ast.h:3721
struct pm_node * constant
FindPatternNode#constant.
Definition ast.h:3773
struct pm_node * right
FindPatternNode#right.
Definition ast.h:3788
struct pm_node_list requireds
FindPatternNode#requireds.
Definition ast.h:3783
struct pm_splat_node * left
FindPatternNode#left.
Definition ast.h:3778
pm_node_t base
The embedded base node.
Definition ast.h:3818
struct pm_node * left
FlipFlopNode#left.
Definition ast.h:3824
struct pm_node * right
FlipFlopNode#right.
Definition ast.h:3829
double value
FloatNode#value.
Definition ast.h:3859
struct pm_statements_node * statements
ForNode#statements.
Definition ast.h:3909
struct pm_node * collection
ForNode#collection.
Definition ast.h:3897
struct pm_block_node * block
ForwardingSuperNode#block.
Definition ast.h:4011
struct pm_node * value
GlobalVariableAndWriteNode#value.
Definition ast.h:4049
pm_constant_id_t name
GlobalVariableAndWriteNode#name.
Definition ast.h:4034
pm_constant_id_t name
GlobalVariableOperatorWriteNode#name.
Definition ast.h:4072
pm_constant_id_t binary_operator
GlobalVariableOperatorWriteNode#binary_operator.
Definition ast.h:4092
struct pm_node * value
GlobalVariableOperatorWriteNode#value.
Definition ast.h:4087
pm_constant_id_t name
GlobalVariableOrWriteNode#name.
Definition ast.h:4115
struct pm_node * value
GlobalVariableOrWriteNode#value.
Definition ast.h:4130
pm_constant_id_t name
GlobalVariableReadNode#name.
Definition ast.h:4159
pm_constant_id_t name
GlobalVariableTargetNode#name.
Definition ast.h:4182
struct pm_node * value
GlobalVariableWriteNode#value.
Definition ast.h:4234
pm_constant_id_t name
GlobalVariableWriteNode#name.
Definition ast.h:4211
struct pm_node_list elements
HashNode#elements.
Definition ast.h:4285
struct pm_node_list elements
HashPatternNode#elements.
Definition ast.h:4326
struct pm_node * rest
HashPatternNode#rest.
Definition ast.h:4331
struct pm_node * constant
HashPatternNode#constant.
Definition ast.h:4321
struct pm_node * predicate
IfNode#predicate.
Definition ast.h:4395
struct pm_statements_node * statements
IfNode#statements.
Definition ast.h:4422
struct pm_node * numeric
ImaginaryNode#numeric.
Definition ast.h:4476
struct pm_node * value
ImplicitNode#value.
Definition ast.h:4505
struct pm_statements_node * statements
InNode#statements.
Definition ast.h:4560
struct pm_node * pattern
InNode#pattern.
Definition ast.h:4555
struct pm_arguments_node * arguments
IndexAndWriteNode#arguments.
Definition ast.h:4614
struct pm_node * receiver
IndexAndWriteNode#receiver.
Definition ast.h:4599
struct pm_block_argument_node * block
IndexAndWriteNode#block.
Definition ast.h:4624
struct pm_node * value
IndexAndWriteNode#value.
Definition ast.h:4634
struct pm_block_argument_node * block
IndexOperatorWriteNode#block.
Definition ast.h:4688
struct pm_node * value
IndexOperatorWriteNode#value.
Definition ast.h:4703
struct pm_arguments_node * arguments
IndexOperatorWriteNode#arguments.
Definition ast.h:4678
pm_constant_id_t binary_operator
IndexOperatorWriteNode#binary_operator.
Definition ast.h:4693
struct pm_node * receiver
IndexOperatorWriteNode#receiver.
Definition ast.h:4663
struct pm_block_argument_node * block
IndexOrWriteNode#block.
Definition ast.h:4757
struct pm_node * receiver
IndexOrWriteNode#receiver.
Definition ast.h:4732
struct pm_node * value
IndexOrWriteNode#value.
Definition ast.h:4767
struct pm_arguments_node * arguments
IndexOrWriteNode#arguments.
Definition ast.h:4747
struct pm_node * receiver
IndexTargetNode#receiver.
Definition ast.h:4804
struct pm_arguments_node * arguments
IndexTargetNode#arguments.
Definition ast.h:4814
struct pm_block_argument_node * block
IndexTargetNode#block.
Definition ast.h:4824
struct pm_node * value
InstanceVariableAndWriteNode#value.
Definition ast.h:4862
pm_constant_id_t name
InstanceVariableAndWriteNode#name.
Definition ast.h:4847
struct pm_node * value
InstanceVariableOperatorWriteNode#value.
Definition ast.h:4900
pm_constant_id_t binary_operator
InstanceVariableOperatorWriteNode#binary_operator.
Definition ast.h:4905
pm_constant_id_t name
InstanceVariableOperatorWriteNode#name.
Definition ast.h:4885
struct pm_node * value
InstanceVariableOrWriteNode#value.
Definition ast.h:4943
pm_constant_id_t name
InstanceVariableOrWriteNode#name.
Definition ast.h:4928
pm_constant_id_t name
InstanceVariableReadNode#name.
Definition ast.h:4972
pm_constant_id_t name
InstanceVariableTargetNode#name.
Definition ast.h:4995
pm_constant_id_t name
InstanceVariableWriteNode#name.
Definition ast.h:5024
struct pm_node * value
InstanceVariableWriteNode#value.
Definition ast.h:5047
pm_integer_t value
IntegerNode#value.
Definition ast.h:5088
A structure represents an arbitrary-sized integer.
Definition pm_integer.h:20
size_t length
The number of allocated values.
Definition pm_integer.h:25
uint32_t value
Embedded value for small integer.
Definition pm_integer.h:36
uint32_t * values
List of 32-bit integers.
Definition pm_integer.h:30
bool negative
Whether or not the integer is negative.
Definition pm_integer.h:42
struct pm_node_list parts
InterpolatedStringNode#parts.
Definition ast.h:5212
struct pm_node_list parts
InterpolatedSymbolNode#parts.
Definition ast.h:5245
struct pm_node_list parts
InterpolatedXStringNode#parts.
Definition ast.h:5278
struct pm_node_list elements
KeywordHashNode#elements.
Definition ast.h:5345
struct pm_node * body
LambdaNode#body.
Definition ast.h:5430
pm_location_t opening_loc
LambdaNode#opening_loc.
Definition ast.h:5415
struct pm_node * parameters
LambdaNode#parameters.
Definition ast.h:5425
pm_location_t operator_loc
LambdaNode#operator_loc.
Definition ast.h:5410
pm_constant_id_list_t locals
LambdaNode#locals.
Definition ast.h:5405
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
struct pm_list_node * next
A pointer to the next node in the list.
Definition pm_list.h:48
This represents the overall linked list.
Definition pm_list.h:55
pm_list_node_t * head
A pointer to the head of the list.
Definition pm_list.h:60
size_t size
The size of the list.
Definition pm_list.h:57
pm_constant_id_t name
LocalVariableAndWriteNode#name.
Definition ast.h:5468
uint32_t depth
LocalVariableAndWriteNode#depth.
Definition ast.h:5473
struct pm_node * value
LocalVariableAndWriteNode#value.
Definition ast.h:5463
uint32_t depth
LocalVariableOperatorWriteNode#depth.
Definition ast.h:5521
pm_constant_id_t binary_operator
LocalVariableOperatorWriteNode#binary_operator.
Definition ast.h:5516
struct pm_node * value
LocalVariableOperatorWriteNode#value.
Definition ast.h:5506
pm_constant_id_t name
LocalVariableOperatorWriteNode#name.
Definition ast.h:5511
uint32_t depth
LocalVariableOrWriteNode#depth.
Definition ast.h:5564
struct pm_node * value
LocalVariableOrWriteNode#value.
Definition ast.h:5554
pm_constant_id_t name
LocalVariableOrWriteNode#name.
Definition ast.h:5559
uint32_t depth
LocalVariableReadNode#depth.
Definition ast.h:5610
pm_constant_id_t name
LocalVariableReadNode#name.
Definition ast.h:5597
uint32_t depth
LocalVariableTargetNode#depth.
Definition ast.h:5638
pm_constant_id_t name
LocalVariableTargetNode#name.
Definition ast.h:5633
struct pm_node * value
LocalVariableWriteNode#value.
Definition ast.h:5707
uint32_t depth
LocalVariableWriteNode#depth.
Definition ast.h:5680
pm_constant_id_t name
LocalVariableWriteNode#name.
Definition ast.h:5667
This represents a range of bytes in the source string to which a node or token corresponds.
Definition ast.h:545
const uint8_t * start
A pointer to the start location of the range in the source.
Definition ast.h:547
const uint8_t * end
A pointer to the end location of the range in the source.
Definition ast.h:550
struct pm_node * pattern
MatchPredicateNode#pattern.
Definition ast.h:5796
struct pm_node * value
MatchPredicateNode#value.
Definition ast.h:5791
struct pm_node * value
MatchRequiredNode#value.
Definition ast.h:5824
struct pm_node * pattern
MatchRequiredNode#pattern.
Definition ast.h:5829
struct pm_node_list targets
MatchWriteNode#targets.
Definition ast.h:5862
struct pm_call_node * call
MatchWriteNode#call.
Definition ast.h:5857
struct pm_node * constant_path
ModuleNode#constant_path.
Definition ast.h:5910
struct pm_node * body
ModuleNode#body.
Definition ast.h:5915
pm_constant_id_list_t locals
ModuleNode#locals.
Definition ast.h:5900
pm_constant_id_t name
ModuleNode#name.
Definition ast.h:5925
struct pm_node_list lefts
MultiTargetNode#lefts.
Definition ast.h:5963
struct pm_node * rest
MultiTargetNode#rest.
Definition ast.h:5983
struct pm_node_list rights
MultiTargetNode#rights.
Definition ast.h:5993
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.
Definition ast.h:6116
struct pm_node * rest
MultiWriteNode#rest.
Definition ast.h:6066
struct pm_node_list rights
MultiWriteNode#rights.
Definition ast.h:6076
struct pm_node_list lefts
MultiWriteNode#lefts.
Definition ast.h:6046
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.
Definition ast.h:6139
size_t size
The number of nodes in the list.
Definition ast.h:560
struct pm_node ** nodes
The nodes in the list.
Definition ast.h:566
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.
pm_node_type_t type
This represents the type of the node.
Definition ast.h:1074
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
Definition ast.h:1086
pm_node_flags_t flags
This represents any flags on the node.
Definition ast.h:1080
pm_location_t location
This is the location of the node in the source.
Definition ast.h:1092
pm_constant_id_t name
OptionalKeywordParameterNode#name.
Definition ast.h:6272
struct pm_node * value
OptionalKeywordParameterNode#value.
Definition ast.h:6282
struct pm_node * value
OptionalParameterNode#value.
Definition ast.h:6324
pm_constant_id_t name
OptionalParameterNode#name.
Definition ast.h:6309
struct pm_node * left
OrNode#left.
Definition ast.h:6355
struct pm_node * right
OrNode#right.
Definition ast.h:6368
struct pm_node * rest
ParametersNode#rest.
Definition ast.h:6412
struct pm_node_list requireds
ParametersNode#requireds.
Definition ast.h:6402
struct pm_block_parameter_node * block
ParametersNode#block.
Definition ast.h:6432
struct pm_node_list optionals
ParametersNode#optionals.
Definition ast.h:6407
struct pm_node_list posts
ParametersNode#posts.
Definition ast.h:6417
pm_node_t base
The embedded base node.
Definition ast.h:6396
struct pm_node * keyword_rest
ParametersNode#keyword_rest.
Definition ast.h:6427
struct pm_node_list keywords
ParametersNode#keywords.
Definition ast.h:6422
struct pm_node * body
ParenthesesNode#body.
Definition ast.h:6455
The format that will be used to format the errors into the output.
size_t blank_prefix_length
The length of the blank prefix.
const char * blank_prefix
The prefix that will be used for blank lines.
size_t divider_length
The length of the divider.
const char * number_prefix
The prefix that will be used for line numbers.
const char * divider
The divider that will be used between sections of source code.
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.
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 ...
Definition parser.h:755
const uint8_t * end
The pointer to the end of the source.
Definition parser.h:694
pm_constant_pool_t constant_pool
This constant pool keeps all of the constants defined throughout the file so that we can reference th...
Definition parser.h:786
const uint8_t * start
The pointer to the start of the source.
Definition parser.h:691
pm_list_t error_list
The list of errors that have been found while parsing.
Definition parser.h:734
pm_list_t warning_list
The list of warnings that have been found while parsing.
Definition parser.h:731
int32_t start_line
The line number at the start of the parse.
Definition parser.h:809
pm_string_t filepath
This is the path of the file being parsed.
Definition parser.h:780
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
Definition parser.h:789
struct pm_node * variable
PinnedVariableNode#variable.
Definition ast.h:6526
struct pm_statements_node * statements
PostExecutionNode#statements.
Definition ast.h:6554
struct pm_statements_node * statements
PreExecutionNode#statements.
Definition ast.h:6592
struct pm_statements_node * statements
ProgramNode#statements.
Definition ast.h:6632
struct pm_node * right
RangeNode#right.
Definition ast.h:6683
struct pm_node * left
RangeNode#left.
Definition ast.h:6669
pm_integer_t denominator
RationalNode#denominator.
Definition ast.h:6732
pm_integer_t numerator
RationalNode#numerator.
Definition ast.h:6723
pm_constant_id_t name
RequiredParameterNode#name.
Definition ast.h:6860
struct pm_node * rescue_expression
RescueModifierNode#rescue_expression.
Definition ast.h:6893
struct pm_node * expression
RescueModifierNode#expression.
Definition ast.h:6883
struct pm_rescue_node * subsequent
RescueNode#subsequent.
Definition ast.h:6946
struct pm_node * reference
RescueNode#reference.
Definition ast.h:6936
struct pm_node_list exceptions
RescueNode#exceptions.
Definition ast.h:6926
struct pm_statements_node * statements
RescueNode#statements.
Definition ast.h:6941
struct pm_arguments_node * arguments
ReturnNode#arguments.
Definition ast.h:7029
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.
Definition ast.h:7078
pm_node_t base
The embedded base node.
Definition ast.h:7070
pm_constant_id_list_t locals
SingletonClassNode#locals.
Definition ast.h:7101
struct pm_node * expression
SingletonClassNode#expression.
Definition ast.h:7116
struct pm_node * body
SingletonClassNode#body.
Definition ast.h:7121
pm_string_t filepath
SourceFileNode#filepath.
Definition ast.h:7175
struct pm_node * expression
SplatNode#expression.
Definition ast.h:7221
struct pm_node_list body
StatementsNode#body.
Definition ast.h:7244
pm_node_t base
The embedded base node.
Definition ast.h:7238
pm_string_t unescaped
StringNode#unescaped.
Definition ast.h:7294
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
struct pm_arguments_node * arguments
SuperNode#arguments.
Definition ast.h:7330
struct pm_node * block
SuperNode#block.
Definition ast.h:7340
pm_string_t unescaped
SymbolNode#unescaped.
Definition ast.h:7386
pm_node_t base
The embedded base node.
Definition ast.h:7365
struct pm_node_list names
UndefNode#names.
Definition ast.h:7427
struct pm_statements_node * statements
UnlessNode#statements.
Definition ast.h:7500
struct pm_node * predicate
UnlessNode#predicate.
Definition ast.h:7479
struct pm_else_node * else_clause
UnlessNode#else_clause.
Definition ast.h:7510
pm_node_t base
The embedded base node.
Definition ast.h:7543
pm_node_t base
The embedded base node.
Definition ast.h:7632
pm_string_t unescaped
XStringNode#unescaped.
Definition ast.h:7700
struct pm_arguments_node * arguments
YieldNode#arguments.
Definition ast.h:7733
struct rb_iseq_constant_body::@000024342312237062266020177166377106262102236123 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376