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