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