Ruby 3.5.0dev (2025-09-14 revision c4ac3e987bf05a743ef9dcfb653bbb7e5b8637df)
prism_compile.c (c4ac3e987bf05a743ef9dcfb653bbb7e5b8637df)
1#include "prism.h"
2#include "ruby/version.h"
3
9typedef struct {
11 int32_t line;
12
14 uint32_t node_id;
16
17/******************************************************************************/
18/* These macros operate on pm_node_location_t structs as opposed to NODE*s. */
19/******************************************************************************/
20
21#define PUSH_ADJUST(seq, location, label) \
22 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
23
24#define PUSH_ADJUST_RESTORE(seq, label) \
25 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
26
27#define PUSH_INSN(seq, location, insn) \
28 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
29
30#define PUSH_INSN1(seq, location, insn, op1) \
31 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
32
33#define PUSH_INSN2(seq, location, insn, op1, op2) \
34 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
35
36#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
37 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
38
39#define PUSH_INSNL(seq, location, insn, label) \
40 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
41
42#define PUSH_LABEL(seq, label) \
43 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
44
45#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
46 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
47
48#define PUSH_SEND(seq, location, id, argc) \
49 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
50
51#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
52 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
53
54#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
55 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
56
57#define PUSH_CALL(seq, location, id, argc) \
58 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
59
60#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
61 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
62
63#define PUSH_TRACE(seq, event) \
64 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
65
66#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
67 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
68
69#define PUSH_SEQ(seq1, seq2) \
70 APPEND_LIST((seq1), (seq2))
71
72#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
73 do { \
74 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
75 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
76 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
77 } while (0)
78
79/******************************************************************************/
80/* These functions compile getlocal/setlocal instructions but operate on */
81/* prism locations instead of NODEs. */
82/******************************************************************************/
83
84static void
85pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
86{
87 if (iseq_local_block_param_p(iseq, idx, level)) {
88 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
89 }
90 else {
91 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
92 }
93 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qfalse);
94}
95
96static void
97pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
98{
99 if (iseq_local_block_param_p(iseq, idx, level)) {
100 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
101 }
102 else {
103 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
104 }
105 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
106}
107
108#define PUSH_GETLOCAL(seq, location, idx, level) \
109 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
110
111#define PUSH_SETLOCAL(seq, location, idx, level) \
112 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
113
114/******************************************************************************/
115/* These are helper macros for the compiler. */
116/******************************************************************************/
117
118#define OLD_ISEQ NEW_ISEQ
119#undef NEW_ISEQ
120
121#define NEW_ISEQ(node, name, type, line_no) \
122 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
123
124#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
125#undef NEW_CHILD_ISEQ
126
127#define NEW_CHILD_ISEQ(node, name, type, line_no) \
128 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
129
130#define PM_COMPILE(node) \
131 pm_compile_node(iseq, (node), ret, popped, scope_node)
132
133#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
134 pm_compile_node(iseq, (node), _ret, popped, scope_node)
135
136#define PM_COMPILE_POPPED(node) \
137 pm_compile_node(iseq, (node), ret, true, scope_node)
138
139#define PM_COMPILE_NOT_POPPED(node) \
140 pm_compile_node(iseq, (node), ret, false, scope_node)
141
142#define PM_NODE_START_LOCATION(parser, node) \
143 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
144
145#define PM_NODE_END_LOCATION(parser, node) \
146 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
147
148#define PM_LOCATION_START_LOCATION(parser, location, id) \
149 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
150
151#define PM_NODE_START_LINE_COLUMN(parser, node) \
152 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
153
154#define PM_NODE_END_LINE_COLUMN(parser, node) \
155 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
156
157#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
158 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
159
160static int
161pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
162{
163 return (int) pm_newline_list_line(&parser->newline_list, node->location.start, parser->start_line);
164}
165
166static int
167pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
168 return (int) pm_newline_list_line(&parser->newline_list, location->start, parser->start_line);
169}
170
174static VALUE
175parse_integer_value(const pm_integer_t *integer)
176{
177 VALUE result;
178
179 if (integer->values == NULL) {
180 result = UINT2NUM(integer->value);
181 }
182 else {
183 VALUE string = rb_str_new(NULL, integer->length * 8);
184 unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);
185
186 size_t offset = integer->length * 8;
187 for (size_t value_index = 0; value_index < integer->length; value_index++) {
188 uint32_t value = integer->values[value_index];
189
190 for (int index = 0; index < 8; index++) {
191 int byte = (value >> (4 * index)) & 0xf;
192 bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
193 }
194 }
195
196 result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
197 }
198
199 if (integer->negative) {
200 result = rb_funcall(result, rb_intern("-@"), 0);
201 }
202
203 return result;
204}
205
209static inline VALUE
210parse_integer(const pm_integer_node_t *node)
211{
212 return parse_integer_value(&node->value);
213}
214
218static VALUE
219parse_float(const pm_float_node_t *node)
220{
221 return DBL2NUM(node->value);
222}
223
230static VALUE
231parse_rational(const pm_rational_node_t *node)
232{
233 VALUE numerator = parse_integer_value(&node->numerator);
234 VALUE denominator = parse_integer_value(&node->denominator);
235 return rb_rational_new(numerator, denominator);
236}
237
244static VALUE
245parse_imaginary(const pm_imaginary_node_t *node)
246{
247 VALUE imaginary_part;
248 switch (PM_NODE_TYPE(node->numeric)) {
249 case PM_FLOAT_NODE: {
250 imaginary_part = parse_float((const pm_float_node_t *) node->numeric);
251 break;
252 }
253 case PM_INTEGER_NODE: {
254 imaginary_part = parse_integer((const pm_integer_node_t *) node->numeric);
255 break;
256 }
257 case PM_RATIONAL_NODE: {
258 imaginary_part = parse_rational((const pm_rational_node_t *) node->numeric);
259 break;
260 }
261 default:
262 rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
263 }
264
265 return rb_complex_raw(INT2FIX(0), imaginary_part);
266}
267
268static inline VALUE
269parse_string(const pm_scope_node_t *scope_node, const pm_string_t *string)
270{
271 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), scope_node->encoding);
272}
273
279static inline VALUE
280parse_string_encoded(const pm_node_t *node, const pm_string_t *string, rb_encoding *default_encoding)
281{
282 rb_encoding *encoding;
283
284 if (node->flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
285 encoding = rb_ascii8bit_encoding();
286 }
287 else if (node->flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
288 encoding = rb_utf8_encoding();
289 }
290 else {
291 encoding = default_encoding;
292 }
293
294 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
295}
296
297static inline VALUE
298parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
299{
300 rb_encoding *encoding;
301
302 if (node->flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
303 encoding = rb_ascii8bit_encoding();
304 }
305 else if (node->flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
306 encoding = rb_utf8_encoding();
307 }
308 else {
309 encoding = scope_node->encoding;
310 }
311
312 VALUE value = rb_enc_literal_str((const char *) pm_string_source(string), pm_string_length(string), encoding);
314
315 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
316 int line_number = pm_node_line_number(scope_node->parser, node);
317 value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
318 }
319
320 return value;
321}
322
323static inline ID
324parse_string_symbol(const pm_scope_node_t *scope_node, const pm_symbol_node_t *symbol)
325{
326 rb_encoding *encoding;
327 if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
328 encoding = rb_utf8_encoding();
329 }
330 else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
331 encoding = rb_ascii8bit_encoding();
332 }
333 else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
334 encoding = rb_usascii_encoding();
335 }
336 else {
337 encoding = scope_node->encoding;
338 }
339
340 return rb_intern3((const char *) pm_string_source(&symbol->unescaped), pm_string_length(&symbol->unescaped), encoding);
341}
342
343static int
344pm_optimizable_range_item_p(const pm_node_t *node)
345{
346 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
347}
348
350static VALUE
351parse_regexp_error(rb_iseq_t *iseq, int32_t line_number, const char *fmt, ...)
352{
353 va_list args;
354 va_start(args, fmt);
355 VALUE error = rb_syntax_error_append(Qnil, rb_iseq_path(iseq), line_number, -1, NULL, "%" PRIsVALUE, args);
356 va_end(args);
357 rb_exc_raise(error);
358}
359
360static VALUE
361parse_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)
362{
363 // If we were passed an explicit regexp encoding, then we need to double
364 // check that it's okay here for this fragment of the string.
365 rb_encoding *encoding;
366
367 if (explicit_regexp_encoding != NULL) {
368 encoding = explicit_regexp_encoding;
369 }
370 else if (node->flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
371 encoding = rb_ascii8bit_encoding();
372 }
373 else if (node->flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
374 encoding = rb_utf8_encoding();
375 }
376 else {
377 encoding = implicit_regexp_encoding;
378 }
379
380 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
381 VALUE error = rb_reg_check_preprocess(string);
382
383 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node), "%" PRIsVALUE, rb_obj_as_string(error));
384 return string;
385}
386
387static VALUE
388pm_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)
389{
390 VALUE current = Qnil;
391
392 for (size_t index = 0; index < nodes->size; index++) {
393 const pm_node_t *part = nodes->nodes[index];
394 VALUE string;
395
396 switch (PM_NODE_TYPE(part)) {
397 case PM_STRING_NODE:
398 if (implicit_regexp_encoding != NULL) {
399 if (top) {
400 string = parse_regexp_string_part(iseq, scope_node, part, &((const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
401 }
402 else {
403 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
404 VALUE error = rb_reg_check_preprocess(string);
405 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part), "%" PRIsVALUE, rb_obj_as_string(error));
406 }
407 }
408 else {
409 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
410 }
411 break;
412 case PM_INTERPOLATED_STRING_NODE:
413 string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
414 break;
415 case PM_EMBEDDED_STATEMENTS_NODE: {
417 string = pm_static_literal_concat(iseq, &cast->statements->body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
418 break;
419 }
420 default:
421 RUBY_ASSERT(false && "unexpected node type in pm_static_literal_concat");
422 return Qnil;
423 }
424
425 if (current != Qnil) {
426 current = rb_str_concat(current, string);
427 }
428 else {
429 current = string;
430 }
431 }
432
433 return top ? rb_fstring(current) : current;
434}
435
436#define RE_OPTION_ENCODING_SHIFT 8
437#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
438#define ARG_ENCODING_NONE 32
439#define ARG_ENCODING_FIXED 16
440#define ENC_ASCII8BIT 1
441#define ENC_EUC_JP 2
442#define ENC_Windows_31J 3
443#define ENC_UTF8 4
444
449static int
450parse_regexp_flags(const pm_node_t *node)
451{
452 int flags = 0;
453
454 // Check "no encoding" first so that flags don't get clobbered
455 // We're calling `rb_char_to_option_kcode` in this case so that
456 // we don't need to have access to `ARG_ENCODING_NONE`
457 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
458 flags |= ARG_ENCODING_NONE;
459 }
460
461 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
462 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
463 }
464
465 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
466 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
467 }
468
469 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
470 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
471 }
472
473 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
474 flags |= ONIG_OPTION_IGNORECASE;
475 }
476
477 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
478 flags |= ONIG_OPTION_MULTILINE;
479 }
480
481 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
482 flags |= ONIG_OPTION_EXTEND;
483 }
484
485 return flags;
486}
487
488#undef RE_OPTION_ENCODING_SHIFT
489#undef RE_OPTION_ENCODING
490#undef ARG_ENCODING_FIXED
491#undef ARG_ENCODING_NONE
492#undef ENC_ASCII8BIT
493#undef ENC_EUC_JP
494#undef ENC_Windows_31J
495#undef ENC_UTF8
496
497static rb_encoding *
498parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
499{
500 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
501 return rb_ascii8bit_encoding();
502 }
503 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
504 return rb_utf8_encoding();
505 }
506 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
507 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
508 }
509 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
510 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
511 }
512 else {
513 return NULL;
514 }
515}
516
517static VALUE
518parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
519{
520 VALUE errinfo = rb_errinfo();
521
522 int32_t line_number = pm_node_line_number(scope_node->parser, node);
523 VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(&scope_node->parser->filepath), line_number);
524
525 if (NIL_P(regexp)) {
526 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
527 rb_set_errinfo(errinfo);
528
529 parse_regexp_error(iseq, line_number, "%" PRIsVALUE, message);
530 return Qnil;
531 }
532
533 rb_obj_freeze(regexp);
534 return regexp;
535}
536
537static inline VALUE
538parse_regexp_literal(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
539{
540 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
541 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
542
543 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
544 return parse_regexp(iseq, scope_node, node, string);
545}
546
547static inline VALUE
548parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
549{
550 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
551 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
552
553 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
554 return parse_regexp(iseq, scope_node, node, string);
555}
556
557static 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);
558
559static int
560pm_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, bool mutable_result)
561{
562 int stack_size = 0;
563 size_t parts_size = parts->size;
564 bool interpolated = false;
565
566 if (parts_size > 0) {
567 VALUE current_string = Qnil;
568 pm_node_location_t current_location = *node_location;
569
570 for (size_t index = 0; index < parts_size; index++) {
571 const pm_node_t *part = parts->nodes[index];
572
573 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
574 const pm_string_node_t *string_node = (const pm_string_node_t *) part;
575 VALUE string_value;
576
577 if (implicit_regexp_encoding == NULL) {
578 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
579 }
580 else {
581 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
582 }
583
584 if (RTEST(current_string)) {
585 current_string = rb_str_concat(current_string, string_value);
586 }
587 else {
588 current_string = string_value;
589 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
590 }
591 }
592 else {
593 interpolated = true;
594
595 if (
596 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
597 ((const pm_embedded_statements_node_t *) part)->statements != NULL &&
598 ((const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
599 PM_NODE_TYPE_P(((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
600 ) {
601 const pm_string_node_t *string_node = (const pm_string_node_t *) ((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
602 VALUE string_value;
603
604 if (implicit_regexp_encoding == NULL) {
605 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
606 }
607 else {
608 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
609 }
610
611 if (RTEST(current_string)) {
612 current_string = rb_str_concat(current_string, string_value);
613 }
614 else {
615 current_string = string_value;
616 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
617 }
618 }
619 else {
620 if (!RTEST(current_string)) {
621 rb_encoding *encoding;
622
623 if (implicit_regexp_encoding != NULL) {
624 if (explicit_regexp_encoding != NULL) {
625 encoding = explicit_regexp_encoding;
626 }
627 else if (scope_node->parser->encoding == PM_ENCODING_US_ASCII_ENTRY) {
628 encoding = rb_ascii8bit_encoding();
629 }
630 else {
631 encoding = implicit_regexp_encoding;
632 }
633 }
634 else {
635 encoding = scope_node->encoding;
636 }
637
638 if (parts_size == 1) {
639 current_string = rb_enc_str_new(NULL, 0, encoding);
640 }
641 }
642
643 if (RTEST(current_string)) {
644 VALUE operand = rb_fstring(current_string);
645 PUSH_INSN1(ret, current_location, putobject, operand);
646 stack_size++;
647 }
648
649 PM_COMPILE_NOT_POPPED(part);
650
651 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
652 PUSH_INSN(ret, current_location, dup);
653
654 {
655 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
656 PUSH_INSN1(ret, current_location, objtostring, callinfo);
657 }
658
659 PUSH_INSN(ret, current_location, anytostring);
660
661 current_string = Qnil;
662 stack_size++;
663 }
664 }
665 }
666
667 if (RTEST(current_string)) {
668 current_string = rb_fstring(current_string);
669
670 if (stack_size == 0 && (interpolated || mutable_result)) {
671 PUSH_INSN1(ret, current_location, putstring, current_string);
672 }
673 else {
674 PUSH_INSN1(ret, current_location, putobject, current_string);
675 }
676
677 current_string = Qnil;
678 stack_size++;
679 }
680 }
681 else {
682 PUSH_INSN(ret, *node_location, putnil);
683 }
684
685 return stack_size;
686}
687
688static void
689pm_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)
690{
691 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
692 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
693
694 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
695 PUSH_INSN2(ret, *node_location, toregexp, INT2FIX(parse_regexp_flags(node) & 0xFF), INT2FIX(length));
696}
697
698static VALUE
699pm_source_file_value(const pm_source_file_node_t *node, const pm_scope_node_t *scope_node)
700{
701 const pm_string_t *filepath = &node->filepath;
702 size_t length = pm_string_length(filepath);
703
704 if (length > 0) {
705 rb_encoding *filepath_encoding = scope_node->filepath_encoding != NULL ? scope_node->filepath_encoding : rb_utf8_encoding();
706 return rb_enc_interned_str((const char *) pm_string_source(filepath), length, filepath_encoding);
707 }
708 else {
709 return rb_fstring_lit("<compiled>");
710 }
711}
712
717static VALUE
718pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
719{
720 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
721 return rb_str_with_debug_created_info(string, rb_iseq_path(iseq), line_number);
722 }
723 else {
724 return rb_fstring(string);
725 }
726}
727
733static VALUE
734pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
735{
736 // Every node that comes into this function should already be marked as
737 // static literal. If it's not, then we have a bug somewhere.
738 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
739
740 switch (PM_NODE_TYPE(node)) {
741 case PM_ARRAY_NODE: {
742 const pm_array_node_t *cast = (const pm_array_node_t *) node;
743 const pm_node_list_t *elements = &cast->elements;
744
745 VALUE value = rb_ary_hidden_new(elements->size);
746 for (size_t index = 0; index < elements->size; index++) {
747 rb_ary_push(value, pm_static_literal_value(iseq, elements->nodes[index], scope_node));
748 }
749
750 OBJ_FREEZE(value);
751 return value;
752 }
753 case PM_FALSE_NODE:
754 return Qfalse;
755 case PM_FLOAT_NODE:
756 return parse_float((const pm_float_node_t *) node);
757 case PM_HASH_NODE: {
758 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
759 const pm_node_list_t *elements = &cast->elements;
760
761 VALUE array = rb_ary_hidden_new(elements->size * 2);
762 for (size_t index = 0; index < elements->size; index++) {
763 RUBY_ASSERT(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
764 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) elements->nodes[index];
765 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
766 rb_ary_cat(array, pair, 2);
767 }
768
769 VALUE value = rb_hash_new_with_size(elements->size);
770 rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
771
772 value = rb_obj_hide(value);
773 OBJ_FREEZE(value);
774 return value;
775 }
776 case PM_IMAGINARY_NODE:
777 return parse_imaginary((const pm_imaginary_node_t *) node);
778 case PM_INTEGER_NODE:
779 return parse_integer((const pm_integer_node_t *) node);
780 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
782 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
783 }
784 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
786 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
787 }
788 case PM_INTERPOLATED_STRING_NODE: {
789 VALUE string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL, false);
790 int line_number = pm_node_line_number(scope_node->parser, node);
791 return pm_static_literal_string(iseq, string, line_number);
792 }
793 case PM_INTERPOLATED_SYMBOL_NODE: {
795 VALUE string = pm_static_literal_concat(iseq, &cast->parts, scope_node, NULL, NULL, true);
796
797 return ID2SYM(rb_intern_str(string));
798 }
799 case PM_MATCH_LAST_LINE_NODE: {
800 const pm_match_last_line_node_t *cast = (const pm_match_last_line_node_t *) node;
801 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
802 }
803 case PM_NIL_NODE:
804 return Qnil;
805 case PM_RATIONAL_NODE:
806 return parse_rational((const pm_rational_node_t *) node);
807 case PM_REGULAR_EXPRESSION_NODE: {
809 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
810 }
811 case PM_SOURCE_ENCODING_NODE:
812 return rb_enc_from_encoding(scope_node->encoding);
813 case PM_SOURCE_FILE_NODE: {
814 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
815 return pm_source_file_value(cast, scope_node);
816 }
817 case PM_SOURCE_LINE_NODE:
818 return INT2FIX(pm_node_line_number(scope_node->parser, node));
819 case PM_STRING_NODE: {
820 const pm_string_node_t *cast = (const pm_string_node_t *) node;
821 return parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
822 }
823 case PM_SYMBOL_NODE:
824 return ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) node));
825 case PM_TRUE_NODE:
826 return Qtrue;
827 default:
828 rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
829 return Qfalse;
830 }
831}
832
837pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
838{
839 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
840 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
841
842 return (rb_code_location_t) {
843 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
844 .end_pos = { .lineno = end_location.line, .column = end_location.column }
845 };
846}
847
853#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
854
855static void
856pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
857 LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
858
859static void
860pm_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)
861{
862 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
863
864 DECL_ANCHOR(seq);
865
866 LABEL *label = NEW_LABEL(location.line);
867 if (!then_label) then_label = label;
868 else if (!else_label) else_label = label;
869
870 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
871
872 if (LIST_INSN_SIZE_ONE(seq)) {
873 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
874 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label) return;
875 }
876
877 if (!label->refcnt) {
878 if (popped) PUSH_INSN(ret, location, putnil);
879 }
880 else {
881 PUSH_LABEL(seq, label);
882 }
883
884 PUSH_SEQ(ret, seq);
885 return;
886}
887
888static void
889pm_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)
890{
891 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
892
893 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
894 PM_COMPILE_NOT_POPPED(node);
895
896 VALUE operand = ID2SYM(rb_intern("$."));
897 PUSH_INSN1(ret, location, getglobal, operand);
898
899 PUSH_SEND(ret, location, idEq, INT2FIX(1));
900 if (popped) PUSH_INSN(ret, location, pop);
901 }
902 else {
903 PM_COMPILE(node);
904 }
905}
906
907static void
908pm_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)
909{
910 const pm_node_location_t location = { .line = lineno, .node_id = -1 };
911 LABEL *lend = NEW_LABEL(location.line);
912
913 int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
914
915 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
916 VALUE key = INT2FIX(count);
917
918 PUSH_INSN2(ret, location, getspecial, key, INT2FIX(0));
919 PUSH_INSNL(ret, location, branchif, lend);
920
921 if (flip_flop_node->left) {
922 pm_compile_flip_flop_bound(iseq, flip_flop_node->left, ret, popped, scope_node);
923 }
924 else {
925 PUSH_INSN(ret, location, putnil);
926 }
927
928 PUSH_INSNL(ret, location, branchunless, else_label);
929 PUSH_INSN1(ret, location, putobject, Qtrue);
930 PUSH_INSN1(ret, location, setspecial, key);
931 if (!again) {
932 PUSH_INSNL(ret, location, jump, then_label);
933 }
934
935 PUSH_LABEL(ret, lend);
936 if (flip_flop_node->right) {
937 pm_compile_flip_flop_bound(iseq, flip_flop_node->right, ret, popped, scope_node);
938 }
939 else {
940 PUSH_INSN(ret, location, putnil);
941 }
942
943 PUSH_INSNL(ret, location, branchunless, then_label);
944 PUSH_INSN1(ret, location, putobject, Qfalse);
945 PUSH_INSN1(ret, location, setspecial, key);
946 PUSH_INSNL(ret, location, jump, then_label);
947}
948
949static 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);
950
951static void
952pm_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)
953{
954 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
955
956again:
957 switch (PM_NODE_TYPE(cond)) {
958 case PM_AND_NODE: {
959 const pm_and_node_t *cast = (const pm_and_node_t *) cond;
960 pm_compile_logical(iseq, ret, cast->left, NULL, else_label, popped, scope_node);
961
962 cond = cast->right;
963 goto again;
964 }
965 case PM_OR_NODE: {
966 const pm_or_node_t *cast = (const pm_or_node_t *) cond;
967 pm_compile_logical(iseq, ret, cast->left, then_label, NULL, popped, scope_node);
968
969 cond = cast->right;
970 goto again;
971 }
972 case PM_FALSE_NODE:
973 case PM_NIL_NODE:
974 PUSH_INSNL(ret, location, jump, else_label);
975 return;
976 case PM_FLOAT_NODE:
977 case PM_IMAGINARY_NODE:
978 case PM_INTEGER_NODE:
979 case PM_LAMBDA_NODE:
980 case PM_RATIONAL_NODE:
981 case PM_REGULAR_EXPRESSION_NODE:
982 case PM_STRING_NODE:
983 case PM_SYMBOL_NODE:
984 case PM_TRUE_NODE:
985 PUSH_INSNL(ret, location, jump, then_label);
986 return;
987 case PM_FLIP_FLOP_NODE:
988 pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, popped, scope_node);
989 return;
990 case PM_DEFINED_NODE: {
991 const pm_defined_node_t *cast = (const pm_defined_node_t *) cond;
992 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, true);
993 break;
994 }
995 default: {
996 DECL_ANCHOR(cond_seq);
997 pm_compile_node(iseq, cond, cond_seq, false, scope_node);
998
999 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1000 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1001
1002 if (insn->insn_id == BIN(putobject)) {
1003 if (RTEST(insn->operands[0])) {
1004 PUSH_INSNL(ret, location, jump, then_label);
1005 // maybe unreachable
1006 return;
1007 }
1008 else {
1009 PUSH_INSNL(ret, location, jump, else_label);
1010 return;
1011 }
1012 }
1013 }
1014
1015 PUSH_SEQ(ret, cond_seq);
1016 break;
1017 }
1018 }
1019
1020 PUSH_INSNL(ret, location, branchunless, else_label);
1021 PUSH_INSNL(ret, location, jump, then_label);
1022}
1023
1027static void
1028pm_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)
1029{
1030 const pm_node_location_t location = *node_location;
1031 LABEL *then_label = NEW_LABEL(location.line);
1032 LABEL *else_label = NEW_LABEL(location.line);
1033 LABEL *end_label = NULL;
1034
1035 DECL_ANCHOR(cond_seq);
1036 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, false, scope_node);
1037 PUSH_SEQ(ret, cond_seq);
1038
1039 rb_code_location_t conditional_location = { 0 };
1040 VALUE branches = Qfalse;
1041
1042 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1043 conditional_location = pm_code_location(scope_node, node);
1044 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location, type == PM_IF_NODE ? "if" : "unless");
1045 }
1046
1047 if (then_label->refcnt) {
1048 PUSH_LABEL(ret, then_label);
1049
1050 DECL_ANCHOR(then_seq);
1051
1052 if (statements != NULL) {
1053 pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
1054 }
1055 else if (!popped) {
1056 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1057 }
1058
1059 if (else_label->refcnt) {
1060 // Establish branch coverage for the then block.
1061 if (PM_BRANCH_COVERAGE_P(iseq)) {
1062 rb_code_location_t branch_location;
1063
1064 if (statements != NULL) {
1065 branch_location = pm_code_location(scope_node, (const pm_node_t *) statements);
1066 } else if (type == PM_IF_NODE) {
1067 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1068 branch_location = (rb_code_location_t) {
1069 .beg_pos = { .lineno = predicate_end.line, .column = predicate_end.column },
1070 .end_pos = { .lineno = predicate_end.line, .column = predicate_end.column }
1071 };
1072 } else {
1073 branch_location = conditional_location;
1074 }
1075
1076 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, type == PM_IF_NODE ? "then" : "else", branches);
1077 }
1078
1079 end_label = NEW_LABEL(location.line);
1080 PUSH_INSNL(then_seq, location, jump, end_label);
1081 if (!popped) PUSH_INSN(then_seq, location, pop);
1082 }
1083
1084 PUSH_SEQ(ret, then_seq);
1085 }
1086
1087 if (else_label->refcnt) {
1088 PUSH_LABEL(ret, else_label);
1089
1090 DECL_ANCHOR(else_seq);
1091
1092 if (subsequent != NULL) {
1093 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1094 }
1095 else if (!popped) {
1096 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1097 }
1098
1099 // Establish branch coverage for the else block.
1100 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1101 rb_code_location_t branch_location;
1102
1103 if (subsequent == NULL) {
1104 branch_location = conditional_location;
1105 } else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1106 const pm_else_node_t *else_node = (const pm_else_node_t *) subsequent;
1107 branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : (const pm_node_t *) else_node);
1108 } else {
1109 branch_location = pm_code_location(scope_node, (const pm_node_t *) subsequent);
1110 }
1111
1112 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1, type == PM_IF_NODE ? "else" : "then", branches);
1113 }
1114
1115 PUSH_SEQ(ret, else_seq);
1116 }
1117
1118 if (end_label) {
1119 PUSH_LABEL(ret, end_label);
1120 }
1121
1122 return;
1123}
1124
1128static void
1129pm_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)
1130{
1131 const pm_node_location_t location = *node_location;
1132
1133 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1134 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1135 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1136
1137 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.line); /* next */
1138 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.line); /* redo */
1139 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.line); /* break */
1140 LABEL *end_label = NEW_LABEL(location.line);
1141 LABEL *adjust_label = NEW_LABEL(location.line);
1142
1143 LABEL *next_catch_label = NEW_LABEL(location.line);
1144 LABEL *tmp_label = NULL;
1145
1146 // We're pushing onto the ensure stack because breaks need to break out of
1147 // this loop and not break into the ensure statements within the same
1148 // lexical scope.
1150 push_ensure_entry(iseq, &enl, NULL, NULL);
1151
1152 // begin; end while true
1153 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1154 tmp_label = NEW_LABEL(location.line);
1155 PUSH_INSNL(ret, location, jump, tmp_label);
1156 }
1157 else {
1158 // while true; end
1159 PUSH_INSNL(ret, location, jump, next_label);
1160 }
1161
1162 PUSH_LABEL(ret, adjust_label);
1163 PUSH_INSN(ret, location, putnil);
1164 PUSH_LABEL(ret, next_catch_label);
1165 PUSH_INSN(ret, location, pop);
1166 PUSH_INSNL(ret, location, jump, next_label);
1167 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1168
1169 PUSH_LABEL(ret, redo_label);
1170
1171 // Establish branch coverage for the loop.
1172 if (PM_BRANCH_COVERAGE_P(iseq)) {
1173 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1174 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location, type == PM_WHILE_NODE ? "while" : "until");
1175
1176 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (const pm_node_t *) statements) : loop_location;
1177 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, "body", branches);
1178 }
1179
1180 if (statements != NULL) PM_COMPILE_POPPED((const pm_node_t *) statements);
1181 PUSH_LABEL(ret, next_label);
1182
1183 if (type == PM_WHILE_NODE) {
1184 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1185 }
1186 else if (type == PM_UNTIL_NODE) {
1187 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1188 }
1189
1190 PUSH_LABEL(ret, end_label);
1191 PUSH_ADJUST_RESTORE(ret, adjust_label);
1192 PUSH_INSN(ret, location, putnil);
1193
1194 PUSH_LABEL(ret, break_label);
1195 if (popped) PUSH_INSN(ret, location, pop);
1196
1197 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1198 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1199 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1200
1201 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1202 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1203 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1204 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1205
1206 return;
1207}
1208
1209// This recurses through scopes and finds the local index at any scope level
1210// It also takes a pointer to depth, and increments depth appropriately
1211// according to the depth of the local.
1212static pm_local_index_t
1213pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
1214{
1215 pm_local_index_t lindex = { 0 };
1216 st_data_t local_index;
1217
1218 int level;
1219 for (level = 0; level < start_depth; level++) {
1220 scope_node = scope_node->previous;
1221 }
1222
1223 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1224 level++;
1225
1226 if (scope_node->previous) {
1227 scope_node = scope_node->previous;
1228 }
1229 else {
1230 // We have recursed up all scope nodes
1231 // and have not found the local yet
1232 rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
1233 }
1234 }
1235
1236 lindex.level = level;
1237 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1238 return lindex;
1239}
1240
1241// This returns the CRuby ID which maps to the pm_constant_id_t
1242//
1243// Constant_ids in prism are indexes of the constants in prism's constant pool.
1244// We add a constants mapping on the scope_node which is a mapping from
1245// these constant_id indexes to the CRuby IDs that they represent.
1246// This helper method allows easy access to those IDs
1247static ID
1248pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
1249{
1250 if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
1251 rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
1252 }
1253 return scope_node->constants[constant_id - 1];
1254}
1255
1256static rb_iseq_t *
1257pm_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)
1258{
1259 debugs("[new_child_iseq]> ---------------------------------------\n");
1260 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1261 int error_state;
1262 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1263 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1264 line_no, parent,
1265 isolated_depth ? isolated_depth + 1 : 0,
1266 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1267
1268 if (error_state) {
1269 pm_scope_node_destroy(node);
1270 RUBY_ASSERT(ret_iseq == NULL);
1271 rb_jump_tag(error_state);
1272 }
1273 debugs("[new_child_iseq]< ---------------------------------------\n");
1274 return ret_iseq;
1275}
1276
1277static int
1278pm_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)
1279{
1280 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1281 const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
1282
1283 if (parent) {
1284 /* Bar::Foo */
1285 PM_COMPILE(parent);
1286 return VM_DEFINECLASS_FLAG_SCOPED;
1287 }
1288 else {
1289 /* toplevel class ::Foo */
1290 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1291 return VM_DEFINECLASS_FLAG_SCOPED;
1292 }
1293 }
1294 else {
1295 /* class at cbase Foo */
1296 PUSH_INSN1(ret, *node_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1297 return 0;
1298 }
1299}
1300
1305static void
1306pm_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)
1307{
1308 const pm_node_location_t location = *node_location;
1309 LABEL *lfin = NEW_LABEL(location.line);
1310 LABEL *lcfin = NEW_LABEL(location.line);
1311 LABEL *lskip = NULL;
1312
1313 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1314 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1315
1316 PM_COMPILE_NOT_POPPED(receiver);
1317 if (safe_nav) {
1318 lskip = NEW_LABEL(location.line);
1319 PUSH_INSN(ret, location, dup);
1320 PUSH_INSNL(ret, location, branchnil, lskip);
1321 }
1322
1323 PUSH_INSN(ret, location, dup);
1324 PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
1325 if (!popped) PUSH_INSN(ret, location, dup);
1326
1327 if (and_node) {
1328 PUSH_INSNL(ret, location, branchunless, lcfin);
1329 }
1330 else {
1331 PUSH_INSNL(ret, location, branchif, lcfin);
1332 }
1333
1334 if (!popped) PUSH_INSN(ret, location, pop);
1335 PM_COMPILE_NOT_POPPED(value);
1336
1337 if (!popped) {
1338 PUSH_INSN(ret, location, swap);
1339 PUSH_INSN1(ret, location, topn, INT2FIX(1));
1340 }
1341
1342 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1343 PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
1344 PUSH_INSNL(ret, location, jump, lfin);
1345
1346 PUSH_LABEL(ret, lcfin);
1347 if (!popped) PUSH_INSN(ret, location, swap);
1348
1349 PUSH_LABEL(ret, lfin);
1350
1351 if (lskip && popped) PUSH_LABEL(ret, lskip);
1352 PUSH_INSN(ret, location, pop);
1353 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1354}
1355
1356static 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);
1357
1363static void
1364pm_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)
1365{
1366 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1367
1368 // If this element is not popped, then we need to create the hash on the
1369 // stack. Neighboring plain assoc nodes should be grouped together (either
1370 // by newhash or hash merge). Double splat nodes should be merged using the
1371 // merge_kwd method call.
1372 const int max_stack_length = 0x100;
1373 const unsigned int min_tmp_hash_length = 0x800;
1374
1375 int stack_length = 0;
1376 bool first_chunk = true;
1377
1378 // This is an optimization wherein we keep track of whether or not the
1379 // previous element was a static literal. If it was, then we do not attempt
1380 // to check if we have a subhash that can be optimized. If it was not, then
1381 // we do check.
1382 bool static_literal = false;
1383
1384 DECL_ANCHOR(anchor);
1385
1386 // Convert pushed elements to a hash, and merge if needed.
1387#define FLUSH_CHUNK \
1388 if (stack_length) { \
1389 if (first_chunk) { \
1390 PUSH_SEQ(ret, anchor); \
1391 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1392 first_chunk = false; \
1393 } \
1394 else { \
1395 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1396 PUSH_INSN(ret, location, swap); \
1397 PUSH_SEQ(ret, anchor); \
1398 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1399 } \
1400 INIT_ANCHOR(anchor); \
1401 stack_length = 0; \
1402 }
1403
1404 for (size_t index = 0; index < elements->size; index++) {
1405 const pm_node_t *element = elements->nodes[index];
1406
1407 switch (PM_NODE_TYPE(element)) {
1408 case PM_ASSOC_NODE: {
1409 // Pre-allocation check (this branch can be omitted).
1410 if (
1411 (shareability == 0) &&
1412 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1413 (!static_literal && ((index + min_tmp_hash_length) < elements->size)) ||
1414 (first_chunk && stack_length == 0)
1415 )
1416 ) {
1417 // Count the elements that are statically-known.
1418 size_t count = 1;
1419 while (index + count < elements->size && PM_NODE_FLAG_P(elements->nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1420
1421 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1422 // The subsequence of elements in this hash is long enough
1423 // to merit its own hash.
1424 VALUE ary = rb_ary_hidden_new(count);
1425
1426 // Create a hidden hash.
1427 for (size_t tmp_end = index + count; index < tmp_end; index++) {
1428 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[index];
1429
1430 VALUE elem[2] = {
1431 pm_static_literal_value(iseq, assoc->key, scope_node),
1432 pm_static_literal_value(iseq, assoc->value, scope_node)
1433 };
1434
1435 rb_ary_cat(ary, elem, 2);
1436 }
1437 index --;
1438
1439 VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary) / 2);
1440 rb_hash_bulk_insert(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary), hash);
1441 hash = rb_obj_hide(hash);
1442 OBJ_FREEZE(hash);
1443
1444 // Emit optimized code.
1445 FLUSH_CHUNK;
1446 if (first_chunk) {
1447 PUSH_INSN1(ret, location, duphash, hash);
1448 first_chunk = false;
1449 }
1450 else {
1451 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1452 PUSH_INSN(ret, location, swap);
1453 PUSH_INSN1(ret, location, putobject, hash);
1454 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1455 }
1456
1457 break;
1458 }
1459 else {
1460 static_literal = true;
1461 }
1462 }
1463 else {
1464 static_literal = false;
1465 }
1466
1467 // If this is a plain assoc node, then we can compile it directly
1468 // and then add the total number of values on the stack.
1469 if (shareability == 0) {
1470 pm_compile_node(iseq, element, anchor, false, scope_node);
1471 }
1472 else {
1473 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
1474 pm_compile_shareable_constant_value(iseq, assoc->key, shareability, path, ret, scope_node, false);
1475 pm_compile_shareable_constant_value(iseq, assoc->value, shareability, path, ret, scope_node, false);
1476 }
1477
1478 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1479 break;
1480 }
1481 case PM_ASSOC_SPLAT_NODE: {
1482 FLUSH_CHUNK;
1483
1484 const pm_assoc_splat_node_t *assoc_splat = (const pm_assoc_splat_node_t *) element;
1485 bool empty_hash = assoc_splat->value != NULL && (
1486 (PM_NODE_TYPE_P(assoc_splat->value, PM_HASH_NODE) && ((const pm_hash_node_t *) assoc_splat->value)->elements.size == 0) ||
1487 PM_NODE_TYPE_P(assoc_splat->value, PM_NIL_NODE)
1488 );
1489
1490 bool first_element = first_chunk && stack_length == 0;
1491 bool last_element = index == elements->size - 1;
1492 bool only_element = first_element && last_element;
1493
1494 if (empty_hash) {
1495 if (only_element && argument) {
1496 // **{} appears at the only keyword argument in method call,
1497 // so it won't be modified.
1498 //
1499 // This is only done for method calls and not for literal
1500 // hashes, because literal hashes should always result in a
1501 // new hash.
1502 PUSH_INSN(ret, location, putnil);
1503 }
1504 else if (first_element) {
1505 // **{} appears as the first keyword argument, so it may be
1506 // modified. We need to create a fresh hash object.
1507 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1508 }
1509 // Any empty keyword splats that are not the first can be
1510 // ignored since merging an empty hash into the existing hash is
1511 // the same as not merging it.
1512 }
1513 else {
1514 if (only_element && argument) {
1515 // ** is only keyword argument in the method call. Use it
1516 // directly. This will be not be flagged as mutable. This is
1517 // only done for method calls and not for literal hashes,
1518 // because literal hashes should always result in a new
1519 // hash.
1520 if (shareability == 0) {
1521 PM_COMPILE_NOT_POPPED(element);
1522 }
1523 else {
1524 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
1525 }
1526 }
1527 else {
1528 // There is more than one keyword argument, or this is not a
1529 // method call. In that case, we need to add an empty hash
1530 // (if first keyword), or merge the hash to the accumulated
1531 // hash (if not the first keyword).
1532 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1533
1534 if (first_element) {
1535 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1536 }
1537 else {
1538 PUSH_INSN(ret, location, swap);
1539 }
1540
1541 if (shareability == 0) {
1542 PM_COMPILE_NOT_POPPED(element);
1543 }
1544 else {
1545 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
1546 }
1547
1548 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1549 }
1550 }
1551
1552 first_chunk = false;
1553 static_literal = false;
1554 break;
1555 }
1556 default:
1557 RUBY_ASSERT("Invalid node type for hash" && false);
1558 break;
1559 }
1560 }
1561
1562 FLUSH_CHUNK;
1563#undef FLUSH_CHUNK
1564}
1565
1566#define SPLATARRAY_FALSE 0
1567#define SPLATARRAY_TRUE 1
1568#define DUP_SINGLE_KW_SPLAT 2
1569
1570// This is details. Users should call pm_setup_args() instead.
1571static int
1572pm_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)
1573{
1574 const pm_node_location_t location = *node_location;
1575
1576 int orig_argc = 0;
1577 bool has_splat = false;
1578 bool has_keyword_splat = false;
1579
1580 if (arguments_node == NULL) {
1581 if (*flags & VM_CALL_FCALL) {
1582 *flags |= VM_CALL_VCALL;
1583 }
1584 }
1585 else {
1586 const pm_node_list_t *arguments = &arguments_node->arguments;
1587 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1588
1589 // We count the number of elements post the splat node that are not keyword elements to
1590 // eventually pass as an argument to newarray
1591 int post_splat_counter = 0;
1592 const pm_node_t *argument;
1593
1594 PM_NODE_LIST_FOREACH(arguments, index, argument) {
1595 switch (PM_NODE_TYPE(argument)) {
1596 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1597 case PM_KEYWORD_HASH_NODE: {
1598 const pm_keyword_hash_node_t *keyword_arg = (const pm_keyword_hash_node_t *) argument;
1599 const pm_node_list_t *elements = &keyword_arg->elements;
1600
1601 if (has_keyword_splat || has_splat) {
1602 *flags |= VM_CALL_KW_SPLAT;
1603 has_keyword_splat = true;
1604
1605 if (elements->size > 1 || !(elements->size == 1 && PM_NODE_TYPE_P(elements->nodes[0], PM_ASSOC_SPLAT_NODE))) {
1606 // A new hash will be created for the keyword arguments
1607 // in this case, so mark the method as passing mutable
1608 // keyword splat.
1609 *flags |= VM_CALL_KW_SPLAT_MUT;
1610 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1611 }
1612 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1613 *flags |= VM_CALL_KW_SPLAT_MUT;
1614 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1615 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1616 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1617 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1618 }
1619 else {
1620 pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
1621 }
1622 }
1623 else {
1624 // We need to first figure out if all elements of the
1625 // KeywordHashNode are AssocNodes with symbol keys.
1626 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1627 // If they are all symbol keys then we can pass them as
1628 // keyword arguments. The first thing we need to do is
1629 // deduplicate. We'll do this using the combination of a
1630 // Ruby hash and a Ruby array.
1631 VALUE stored_indices = rb_hash_new();
1632 VALUE keyword_indices = rb_ary_new_capa(elements->size);
1633
1634 size_t size = 0;
1635 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1636 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1637
1638 // Retrieve the stored index from the hash for this
1639 // keyword.
1640 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1641 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1642
1643 // If this keyword was already seen in the hash,
1644 // then mark the array at that index as false and
1645 // decrement the keyword size.
1646 if (!NIL_P(stored_index)) {
1647 rb_ary_store(keyword_indices, NUM2LONG(stored_index), Qfalse);
1648 size--;
1649 }
1650
1651 // Store (and possibly overwrite) the index for this
1652 // keyword in the hash, mark the array at that index
1653 // as true, and increment the keyword size.
1654 rb_hash_aset(stored_indices, keyword, ULONG2NUM(element_index));
1655 rb_ary_store(keyword_indices, (long) element_index, Qtrue);
1656 size++;
1657 }
1658
1659 *kw_arg = rb_xmalloc_mul_add(size, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
1660 *flags |= VM_CALL_KWARG;
1661
1662 VALUE *keywords = (*kw_arg)->keywords;
1663 (*kw_arg)->references = 0;
1664 (*kw_arg)->keyword_len = (int) size;
1665
1666 size_t keyword_index = 0;
1667 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1668 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1669 bool popped = true;
1670
1671 if (rb_ary_entry(keyword_indices, (long) element_index) == Qtrue) {
1672 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1673 popped = false;
1674 }
1675
1676 PM_COMPILE(assoc->value);
1677 }
1678
1679 RUBY_ASSERT(keyword_index == size);
1680 }
1681 else {
1682 // If they aren't all symbol keys then we need to
1683 // construct a new hash and pass that as an argument.
1684 orig_argc++;
1685 *flags |= VM_CALL_KW_SPLAT;
1686
1687 size_t size = elements->size;
1688 if (size > 1) {
1689 // A new hash will be created for the keyword
1690 // arguments in this case, so mark the method as
1691 // passing mutable keyword splat.
1692 *flags |= VM_CALL_KW_SPLAT_MUT;
1693 }
1694
1695 for (size_t element_index = 0; element_index < size; element_index++) {
1696 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1697 PM_COMPILE_NOT_POPPED(assoc->key);
1698 PM_COMPILE_NOT_POPPED(assoc->value);
1699 }
1700
1701 PUSH_INSN1(ret, location, newhash, INT2FIX(size * 2));
1702 }
1703 }
1704 break;
1705 }
1706 case PM_SPLAT_NODE: {
1707 *flags |= VM_CALL_ARGS_SPLAT;
1708 const pm_splat_node_t *splat_node = (const pm_splat_node_t *) argument;
1709
1710 if (splat_node->expression) {
1711 PM_COMPILE_NOT_POPPED(splat_node->expression);
1712 }
1713 else {
1714 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1715 PUSH_GETLOCAL(ret, location, index.index, index.level);
1716 }
1717
1718 bool first_splat = !has_splat;
1719
1720 if (first_splat) {
1721 // If this is the first splat array seen and it's not the
1722 // last parameter, we want splatarray to dup it.
1723 //
1724 // foo(a, *b, c)
1725 // ^^
1726 if (index + 1 < arguments->size || has_regular_blockarg) {
1727 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ? Qtrue : Qfalse);
1728 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1729 }
1730 // If this is the first spalt array seen and it's the last
1731 // parameter, we don't want splatarray to dup it.
1732 //
1733 // foo(a, *b)
1734 // ^^
1735 else {
1736 PUSH_INSN1(ret, location, splatarray, Qfalse);
1737 }
1738 }
1739 else {
1740 // If this is not the first splat array seen and it is also
1741 // the last parameter, we don't want splatarray to dup it
1742 // and we need to concat the array.
1743 //
1744 // foo(a, *b, *c)
1745 // ^^
1746 PUSH_INSN(ret, location, concattoarray);
1747 }
1748
1749 has_splat = true;
1750 post_splat_counter = 0;
1751
1752 break;
1753 }
1754 case PM_FORWARDING_ARGUMENTS_NODE: { // not counted in argc return value
1755 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1756
1757 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1758 *flags |= VM_CALL_FORWARDING;
1759
1760 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1761 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1762
1763 break;
1764 }
1765
1766 if (has_splat) {
1767 // If we already have a splat, we're concatenating to existing array
1768 orig_argc += 1;
1769 } else {
1770 orig_argc += 2;
1771 }
1772
1773 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1774
1775 // Forwarding arguments nodes are treated as foo(*, **, &)
1776 // So foo(...) equals foo(*, **, &) and as such the local
1777 // table for this method is known in advance
1778 //
1779 // Push the *
1780 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1781 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1782
1783 if (has_splat) {
1784 // If we already have a splat, we need to concatenate arrays
1785 PUSH_INSN(ret, location, concattoarray);
1786 } else {
1787 PUSH_INSN1(ret, location, splatarray, Qfalse);
1788 }
1789
1790 // Push the **
1791 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1792 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1793
1794 // Push the &
1795 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1796 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
1797
1798 break;
1799 }
1800 default: {
1801 post_splat_counter++;
1802 PM_COMPILE_NOT_POPPED(argument);
1803
1804 // If we have a splat and we've seen a splat, we need to process
1805 // everything after the splat.
1806 if (has_splat) {
1807 // Stack items are turned into an array and concatenated in
1808 // the following cases:
1809 //
1810 // If the next node is a splat:
1811 //
1812 // foo(*a, b, *c)
1813 //
1814 // If the next node is a kwarg or kwarg splat:
1815 //
1816 // foo(*a, b, c: :d)
1817 // foo(*a, b, **c)
1818 //
1819 // If the next node is NULL (we have hit the end):
1820 //
1821 // foo(*a, b)
1822 if (index == arguments->size - 1) {
1823 RUBY_ASSERT(post_splat_counter > 0);
1824 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
1825 }
1826 else {
1827 pm_node_t *next_arg = arguments->nodes[index + 1];
1828
1829 switch (PM_NODE_TYPE(next_arg)) {
1830 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1831 case PM_KEYWORD_HASH_NODE: {
1832 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1833 PUSH_INSN(ret, location, concatarray);
1834 break;
1835 }
1836 case PM_SPLAT_NODE: {
1837 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1838 PUSH_INSN(ret, location, concatarray);
1839 break;
1840 }
1841 default:
1842 break;
1843 }
1844 }
1845 }
1846 else {
1847 orig_argc++;
1848 }
1849 }
1850 }
1851 }
1852 }
1853
1854 if (has_splat) orig_argc++;
1855 if (has_keyword_splat) orig_argc++;
1856 return orig_argc;
1857}
1858
1863static inline bool
1864pm_setup_args_dup_rest_p(const pm_node_t *node)
1865{
1866 switch (PM_NODE_TYPE(node)) {
1867 case PM_BACK_REFERENCE_READ_NODE:
1868 case PM_CLASS_VARIABLE_READ_NODE:
1869 case PM_CONSTANT_READ_NODE:
1870 case PM_FALSE_NODE:
1871 case PM_FLOAT_NODE:
1872 case PM_GLOBAL_VARIABLE_READ_NODE:
1873 case PM_IMAGINARY_NODE:
1874 case PM_INSTANCE_VARIABLE_READ_NODE:
1875 case PM_INTEGER_NODE:
1876 case PM_LAMBDA_NODE:
1877 case PM_LOCAL_VARIABLE_READ_NODE:
1878 case PM_NIL_NODE:
1879 case PM_NUMBERED_REFERENCE_READ_NODE:
1880 case PM_RATIONAL_NODE:
1881 case PM_REGULAR_EXPRESSION_NODE:
1882 case PM_SELF_NODE:
1883 case PM_STRING_NODE:
1884 case PM_SYMBOL_NODE:
1885 case PM_TRUE_NODE:
1886 return false;
1887 case PM_CONSTANT_PATH_NODE: {
1888 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
1889 if (cast->parent != NULL) {
1890 return pm_setup_args_dup_rest_p(cast->parent);
1891 }
1892 return false;
1893 }
1894 case PM_IMPLICIT_NODE:
1895 return pm_setup_args_dup_rest_p(((const pm_implicit_node_t *) node)->value);
1896 case PM_ARRAY_NODE: {
1897 const pm_array_node_t *cast = (const pm_array_node_t *) node;
1898 for (size_t index = 0; index < cast->elements.size; index++) {
1899 if (pm_setup_args_dup_rest_p(cast->elements.nodes[index])) {
1900 return true;
1901 }
1902 }
1903 return false;
1904 }
1905 default:
1906 return true;
1907 }
1908}
1909
1913static int
1914pm_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)
1915{
1916 int dup_rest = SPLATARRAY_TRUE;
1917
1918 const pm_node_list_t *arguments;
1919 size_t arguments_size;
1920
1921 // Calls like foo(1, *f, **hash) that use splat and kwsplat could be
1922 // eligible for eliding duping the rest array (dup_reset=false).
1923 if (
1924 arguments_node != NULL &&
1925 (arguments = &arguments_node->arguments, arguments_size = arguments->size) >= 2 &&
1926 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
1927 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
1928 PM_NODE_TYPE_P(arguments->nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1929 ) {
1930 // Start by assuming that dup_rest=false, then check each element of the
1931 // hash to ensure we don't need to flip it back to true (in case one of
1932 // the elements could potentially mutate the array).
1933 dup_rest = SPLATARRAY_FALSE;
1934
1935 const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) arguments->nodes[arguments_size - 1];
1936 const pm_node_list_t *elements = &keyword_hash->elements;
1937
1938 for (size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->size; index++) {
1939 const pm_node_t *element = elements->nodes[index];
1940
1941 switch (PM_NODE_TYPE(element)) {
1942 case PM_ASSOC_NODE: {
1943 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
1944 if (pm_setup_args_dup_rest_p(assoc->key) || pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
1945 break;
1946 }
1947 case PM_ASSOC_SPLAT_NODE: {
1948 const pm_assoc_splat_node_t *assoc = (const pm_assoc_splat_node_t *) element;
1949 if (assoc->value != NULL && pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
1950 break;
1951 }
1952 default:
1953 break;
1954 }
1955 }
1956 }
1957
1958 int initial_dup_rest = dup_rest;
1959 int argc;
1960
1961 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1962 // We compile the `&block_arg` expression first and stitch it later
1963 // since the nature of the expression influences whether splat should
1964 // duplicate the array.
1965 bool regular_block_arg = true;
1966 const pm_node_t *block_expr = ((const pm_block_argument_node_t *)block)->expression;
1967
1968 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1969 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1970 initial_dup_rest = dup_rest;
1971 }
1972
1973 DECL_ANCHOR(block_arg);
1974 pm_compile_node(iseq, block, block_arg, false, scope_node);
1975
1976 *flags |= VM_CALL_ARGS_BLOCKARG;
1977
1978 if (LIST_INSN_SIZE_ONE(block_arg)) {
1979 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
1980 if (IS_INSN(elem)) {
1981 INSN *iobj = (INSN *) elem;
1982 if (iobj->insn_id == BIN(getblockparam)) {
1983 iobj->insn_id = BIN(getblockparamproxy);
1984 }
1985
1986 // Allow splat without duplication for simple one-instruction
1987 // block arguments like `&arg`. It is known that this
1988 // optimization can be too aggressive in some cases. See
1989 // [Bug #16504].
1990 regular_block_arg = false;
1991 }
1992 }
1993
1994 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1995 PUSH_SEQ(ret, block_arg);
1996 }
1997 else {
1998 argc = pm_setup_args_core(arguments_node, block, flags, false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1999 }
2000
2001 // If the dup_rest flag was consumed while compiling the arguments (which
2002 // effectively means we found the splat node), then it would have changed
2003 // during the call to pm_setup_args_core. In this case, we want to add the
2004 // VM_CALL_ARGS_SPLAT_MUT flag.
2005 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
2006 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2007 }
2008
2009 return argc;
2010}
2011
2022static void
2023pm_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)
2024{
2025 const pm_node_location_t location = *node_location;
2026 if (!popped) PUSH_INSN(ret, location, putnil);
2027
2028 PM_COMPILE_NOT_POPPED(node->receiver);
2029
2030 int boff = (node->block == NULL ? 0 : 1);
2031 int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2032 struct rb_callinfo_kwarg *keywords = NULL;
2033 int argc = pm_setup_args(node->arguments, (const pm_node_t *) node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
2034
2035 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2036 if (boff) {
2037 PUSH_INSN(ret, location, splatkw);
2038 }
2039 else {
2040 PUSH_INSN(ret, location, dup);
2041 PUSH_INSN(ret, location, splatkw);
2042 PUSH_INSN(ret, location, pop);
2043 }
2044 }
2045
2046 int dup_argn = argc + 1 + boff;
2047 int keyword_len = 0;
2048
2049 if (keywords) {
2050 keyword_len = keywords->keyword_len;
2051 dup_argn += keyword_len;
2052 }
2053
2054 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
2055 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2056 PM_COMPILE_NOT_POPPED(node->value);
2057
2058 ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
2059 PUSH_SEND(ret, location, id_operator, INT2FIX(1));
2060
2061 if (!popped) {
2062 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2063 }
2064 if (flag & VM_CALL_ARGS_SPLAT) {
2065 if (flag & VM_CALL_KW_SPLAT) {
2066 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
2067
2068 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2069 PUSH_INSN1(ret, location, splatarray, Qtrue);
2070 flag |= VM_CALL_ARGS_SPLAT_MUT;
2071 }
2072
2073 PUSH_INSN(ret, location, swap);
2074 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2075 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
2076 PUSH_INSN(ret, location, pop);
2077 }
2078 else {
2079 if (boff > 0) {
2080 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2081 PUSH_INSN(ret, location, swap);
2082 PUSH_INSN(ret, location, pop);
2083 }
2084 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2085 PUSH_INSN(ret, location, swap);
2086 PUSH_INSN1(ret, location, splatarray, Qtrue);
2087 PUSH_INSN(ret, location, swap);
2088 flag |= VM_CALL_ARGS_SPLAT_MUT;
2089 }
2090 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2091 if (boff > 0) {
2092 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2093 PUSH_INSN(ret, location, pop);
2094 PUSH_INSN(ret, location, pop);
2095 }
2096 }
2097
2098 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
2099 }
2100 else if (flag & VM_CALL_KW_SPLAT) {
2101 if (boff > 0) {
2102 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2103 PUSH_INSN(ret, location, swap);
2104 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2105 PUSH_INSN(ret, location, pop);
2106 }
2107 PUSH_INSN(ret, location, swap);
2108 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2109 }
2110 else if (keyword_len) {
2111 PUSH_INSN(ret, location, dup);
2112 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 2));
2113 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
2114 PUSH_INSN(ret, location, pop);
2115 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2116 }
2117 else {
2118 if (boff > 0) {
2119 PUSH_INSN(ret, location, swap);
2120 }
2121 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2122 }
2123
2124 PUSH_INSN(ret, location, pop);
2125}
2126
2139static void
2140pm_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)
2141{
2142 const pm_node_location_t location = *node_location;
2143 if (!popped) PUSH_INSN(ret, location, putnil);
2144 PM_COMPILE_NOT_POPPED(receiver);
2145
2146 int boff = (block == NULL ? 0 : 1);
2147 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2148 struct rb_callinfo_kwarg *keywords = NULL;
2149 int argc = pm_setup_args(arguments, (const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2150
2151 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2152 if (boff) {
2153 PUSH_INSN(ret, location, splatkw);
2154 }
2155 else {
2156 PUSH_INSN(ret, location, dup);
2157 PUSH_INSN(ret, location, splatkw);
2158 PUSH_INSN(ret, location, pop);
2159 }
2160 }
2161
2162 int dup_argn = argc + 1 + boff;
2163 int keyword_len = 0;
2164
2165 if (keywords) {
2166 keyword_len = keywords->keyword_len;
2167 dup_argn += keyword_len;
2168 }
2169
2170 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
2171 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2172
2173 LABEL *label = NEW_LABEL(location.line);
2174 LABEL *lfin = NEW_LABEL(location.line);
2175
2176 PUSH_INSN(ret, location, dup);
2177 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2178 PUSH_INSNL(ret, location, branchunless, label);
2179 }
2180 else {
2181 PUSH_INSNL(ret, location, branchif, label);
2182 }
2183
2184 PUSH_INSN(ret, location, pop);
2185 PM_COMPILE_NOT_POPPED(value);
2186
2187 if (!popped) {
2188 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2189 }
2190
2191 if (flag & VM_CALL_ARGS_SPLAT) {
2192 if (flag & VM_CALL_KW_SPLAT) {
2193 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
2194 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2195 PUSH_INSN1(ret, location, splatarray, Qtrue);
2196 flag |= VM_CALL_ARGS_SPLAT_MUT;
2197 }
2198
2199 PUSH_INSN(ret, location, swap);
2200 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2201 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
2202 PUSH_INSN(ret, location, pop);
2203 }
2204 else {
2205 if (boff > 0) {
2206 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2207 PUSH_INSN(ret, location, swap);
2208 PUSH_INSN(ret, location, pop);
2209 }
2210 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2211 PUSH_INSN(ret, location, swap);
2212 PUSH_INSN1(ret, location, splatarray, Qtrue);
2213 PUSH_INSN(ret, location, swap);
2214 flag |= VM_CALL_ARGS_SPLAT_MUT;
2215 }
2216 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
2217 if (boff > 0) {
2218 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2219 PUSH_INSN(ret, location, pop);
2220 PUSH_INSN(ret, location, pop);
2221 }
2222 }
2223
2224 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
2225 }
2226 else if (flag & VM_CALL_KW_SPLAT) {
2227 if (boff > 0) {
2228 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2229 PUSH_INSN(ret, location, swap);
2230 PUSH_INSN1(ret, location, setn, INT2FIX(3));
2231 PUSH_INSN(ret, location, pop);
2232 }
2233
2234 PUSH_INSN(ret, location, swap);
2235 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2236 }
2237 else if (keyword_len) {
2238 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
2239 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 0));
2240 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2241 }
2242 else {
2243 if (boff > 0) {
2244 PUSH_INSN(ret, location, swap);
2245 }
2246 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
2247 }
2248
2249 PUSH_INSN(ret, location, pop);
2250 PUSH_INSNL(ret, location, jump, lfin);
2251 PUSH_LABEL(ret, label);
2252 if (!popped) {
2253 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
2254 }
2255 PUSH_INSN1(ret, location, adjuststack, INT2FIX(dup_argn + 1));
2256 PUSH_LABEL(ret, lfin);
2257}
2258
2259// When we compile a pattern matching expression, we use the stack as a scratch
2260// space to store lots of different values (consider it like we have a pattern
2261// matching function and we need space for a bunch of different local
2262// variables). The "base index" refers to the index on the stack where we
2263// started compiling the pattern matching expression. These offsets from that
2264// base index indicate the location of the various locals we need.
2265#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2266#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2267#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2268#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2269#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2270
2271// A forward declaration because this is the recursive function that handles
2272// compiling a pattern. It can be reentered by nesting patterns, as in the case
2273// of arrays or hashes.
2274static 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);
2275
2280static int
2281pm_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)
2282{
2283 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2284 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2285
2286 PUSH_INSN(ret, location, dup);
2287 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2288
2289 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2290 PUSH_INSN1(ret, location, putobject, message);
2291 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2292 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
2293 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2294
2295 PUSH_INSN1(ret, location, putobject, Qfalse);
2296 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2297
2298 PUSH_INSN(ret, location, pop);
2299 PUSH_INSN(ret, location, pop);
2300 PUSH_LABEL(ret, match_succeeded_label);
2301
2302 return COMPILE_OK;
2303}
2304
2310static int
2311pm_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)
2312{
2313 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2314 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2315
2316 PUSH_INSN(ret, location, dup);
2317 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2318
2319 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2320 PUSH_INSN1(ret, location, putobject, message);
2321 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2322 PUSH_INSN(ret, location, dup);
2323 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2324 PUSH_INSN1(ret, location, putobject, length);
2325 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(4));
2326 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2327
2328 PUSH_INSN1(ret, location, putobject, Qfalse);
2329 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2330
2331 PUSH_INSN(ret, location, pop);
2332 PUSH_INSN(ret, location, pop);
2333 PUSH_LABEL(ret, match_succeeded_label);
2334
2335 return COMPILE_OK;
2336}
2337
2343static int
2344pm_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)
2345{
2346 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2347 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2348
2349 PUSH_INSN(ret, location, dup);
2350 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2351 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2352
2353 VALUE operand = rb_fstring_lit("%p === %p does not return true");
2354 PUSH_INSN1(ret, location, putobject, operand);
2355
2356 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2357 PUSH_INSN1(ret, location, topn, INT2FIX(5));
2358 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2359 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2360 PUSH_INSN1(ret, location, putobject, Qfalse);
2361 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2362 PUSH_INSN(ret, location, pop);
2363 PUSH_INSN(ret, location, pop);
2364
2365 PUSH_LABEL(ret, match_succeeded_label);
2366 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2367 PUSH_INSN(ret, location, pop);
2368 PUSH_INSN(ret, location, pop);
2369
2370 return COMPILE_OK;
2371}
2372
2379static int
2380pm_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)
2381{
2382 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2383 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2384 PUSH_LABEL(ret, matched_label);
2385 return COMPILE_OK;
2386}
2387
2393static int
2394pm_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)
2395{
2396 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2397
2398 if (use_deconstructed_cache) {
2399 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2400 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2401
2402 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2403 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2404
2405 PUSH_INSN(ret, location, pop);
2406 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2407 PUSH_INSNL(ret, location, jump, deconstructed_label);
2408 }
2409 else {
2410 PUSH_INSNL(ret, location, jump, deconstruct_label);
2411 }
2412
2413 PUSH_LABEL(ret, deconstruct_label);
2414 PUSH_INSN(ret, location, dup);
2415
2416 VALUE operand = ID2SYM(rb_intern("deconstruct"));
2417 PUSH_INSN1(ret, location, putobject, operand);
2418 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2419
2420 if (use_deconstructed_cache) {
2421 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2422 }
2423
2424 if (in_single_pattern) {
2425 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1));
2426 }
2427
2428 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2429 PUSH_SEND(ret, location, rb_intern("deconstruct"), INT2FIX(0));
2430
2431 if (use_deconstructed_cache) {
2432 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2433 }
2434
2435 PUSH_INSN(ret, location, dup);
2436 PUSH_INSN1(ret, location, checktype, INT2FIX(T_ARRAY));
2437 PUSH_INSNL(ret, location, branchunless, type_error_label);
2438 PUSH_LABEL(ret, deconstructed_label);
2439
2440 return COMPILE_OK;
2441}
2442
2447static int
2448pm_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)
2449{
2450 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2451
2452 PUSH_INSN(ret, location, dup);
2453 PM_COMPILE_NOT_POPPED(node);
2454
2455 if (in_single_pattern) {
2456 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
2457 }
2458 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2459 if (in_single_pattern) {
2460 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2461 }
2462 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2463 return COMPILE_OK;
2464}
2465
2470static void
2471pm_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)
2472{
2473 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2474 LABEL *key_error_label = NEW_LABEL(location.line);
2475 LABEL *cleanup_label = NEW_LABEL(location.line);
2476
2477 struct rb_callinfo_kwarg *kw_arg = rb_xmalloc_mul_add(2, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
2478 kw_arg->references = 0;
2479 kw_arg->keyword_len = 2;
2480 kw_arg->keywords[0] = ID2SYM(rb_intern("matchee"));
2481 kw_arg->keywords[1] = ID2SYM(rb_intern("key"));
2482
2483 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2484 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2485 PUSH_INSNL(ret, location, branchif, key_error_label);
2486
2487 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternError);
2488 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2489
2490 {
2491 VALUE operand = rb_fstring_lit("%p: %s");
2492 PUSH_INSN1(ret, location, putobject, operand);
2493 }
2494
2495 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2496 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2497 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2498 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2499 PUSH_INSNL(ret, location, jump, cleanup_label);
2500
2501 PUSH_LABEL(ret, key_error_label);
2502 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternKeyError);
2503 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2504
2505 {
2506 VALUE operand = rb_fstring_lit("%p: %s");
2507 PUSH_INSN1(ret, location, putobject, operand);
2508 }
2509
2510 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2511 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2512 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2513 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2514 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2515 PUSH_SEND_R(ret, location, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
2516 PUSH_SEND(ret, location, id_core_raise, INT2FIX(1));
2517 PUSH_LABEL(ret, cleanup_label);
2518
2519 PUSH_INSN1(ret, location, adjuststack, INT2FIX(7));
2520 if (!popped) PUSH_INSN(ret, location, putnil);
2521 PUSH_INSNL(ret, location, jump, done_label);
2522 PUSH_INSN1(ret, location, dupn, INT2FIX(5));
2523 if (popped) PUSH_INSN(ret, location, putnil);
2524}
2525
2529static int
2530pm_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)
2531{
2532 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2533
2534 switch (PM_NODE_TYPE(node)) {
2535 case PM_ARRAY_PATTERN_NODE: {
2536 // Array patterns in pattern matching are triggered by using commas in
2537 // a pattern or wrapping it in braces. They are represented by a
2538 // ArrayPatternNode. This looks like:
2539 //
2540 // foo => [1, 2, 3]
2541 //
2542 // It can optionally have a splat in the middle of it, which can
2543 // optionally have a name attached.
2544 const pm_array_pattern_node_t *cast = (const pm_array_pattern_node_t *) node;
2545
2546 const size_t requireds_size = cast->requireds.size;
2547 const size_t posts_size = cast->posts.size;
2548 const size_t minimum_size = requireds_size + posts_size;
2549
2550 bool rest_named = false;
2551 bool use_rest_size = false;
2552
2553 if (cast->rest != NULL) {
2554 rest_named = (PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) cast->rest)->expression != NULL);
2555 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2556 }
2557
2558 LABEL *match_failed_label = NEW_LABEL(location.line);
2559 LABEL *type_error_label = NEW_LABEL(location.line);
2560 LABEL *deconstruct_label = NEW_LABEL(location.line);
2561 LABEL *deconstructed_label = NEW_LABEL(location.line);
2562
2563 if (use_rest_size) {
2564 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2565 PUSH_INSN(ret, location, swap);
2566 base_index++;
2567 }
2568
2569 if (cast->constant != NULL) {
2570 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2571 }
2572
2573 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));
2574
2575 PUSH_INSN(ret, location, dup);
2576 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2577 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2578 PUSH_SEND(ret, location, cast->rest == NULL ? idEq : idGE, INT2FIX(1));
2579 if (in_single_pattern) {
2580 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+)");
2581 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message, INT2FIX(minimum_size), base_index + 1));
2582 }
2583 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2584
2585 for (size_t index = 0; index < requireds_size; index++) {
2586 const pm_node_t *required = cast->requireds.nodes[index];
2587 PUSH_INSN(ret, location, dup);
2588 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2589 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2590 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2591 }
2592
2593 if (cast->rest != NULL) {
2594 if (rest_named) {
2595 PUSH_INSN(ret, location, dup);
2596 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size));
2597 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2598 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2599 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2600 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2601 PUSH_INSN1(ret, location, setn, INT2FIX(4));
2602 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2603 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));
2604 }
2605 else if (posts_size > 0) {
2606 PUSH_INSN(ret, location, dup);
2607 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2608 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2609 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2610 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2611 PUSH_INSN(ret, location, pop);
2612 }
2613 }
2614
2615 for (size_t index = 0; index < posts_size; index++) {
2616 const pm_node_t *post = cast->posts.nodes[index];
2617 PUSH_INSN(ret, location, dup);
2618
2619 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size + index));
2620 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2621 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2622 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2623 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2624 }
2625
2626 PUSH_INSN(ret, location, pop);
2627 if (use_rest_size) {
2628 PUSH_INSN(ret, location, pop);
2629 }
2630
2631 PUSH_INSNL(ret, location, jump, matched_label);
2632 PUSH_INSN(ret, location, putnil);
2633 if (use_rest_size) {
2634 PUSH_INSN(ret, location, putnil);
2635 }
2636
2637 PUSH_LABEL(ret, type_error_label);
2638 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2639 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2640
2641 {
2642 VALUE operand = rb_fstring_lit("deconstruct must return Array");
2643 PUSH_INSN1(ret, location, putobject, operand);
2644 }
2645
2646 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2647 PUSH_INSN(ret, location, pop);
2648
2649 PUSH_LABEL(ret, match_failed_label);
2650 PUSH_INSN(ret, location, pop);
2651 if (use_rest_size) {
2652 PUSH_INSN(ret, location, pop);
2653 }
2654
2655 PUSH_INSNL(ret, location, jump, unmatched_label);
2656 break;
2657 }
2658 case PM_FIND_PATTERN_NODE: {
2659 // Find patterns in pattern matching are triggered by using commas in
2660 // a pattern or wrapping it in braces and using a splat on both the left
2661 // and right side of the pattern. This looks like:
2662 //
2663 // foo => [*, 1, 2, 3, *]
2664 //
2665 // There can be any number of requireds in the middle. The splats on
2666 // both sides can optionally have names attached.
2667 const pm_find_pattern_node_t *cast = (const pm_find_pattern_node_t *) node;
2668 const size_t size = cast->requireds.size;
2669
2670 LABEL *match_failed_label = NEW_LABEL(location.line);
2671 LABEL *type_error_label = NEW_LABEL(location.line);
2672 LABEL *deconstruct_label = NEW_LABEL(location.line);
2673 LABEL *deconstructed_label = NEW_LABEL(location.line);
2674
2675 if (cast->constant) {
2676 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2677 }
2678
2679 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));
2680
2681 PUSH_INSN(ret, location, dup);
2682 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2683 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2684 PUSH_SEND(ret, location, idGE, INT2FIX(1));
2685 if (in_single_pattern) {
2686 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));
2687 }
2688 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2689
2690 {
2691 LABEL *while_begin_label = NEW_LABEL(location.line);
2692 LABEL *next_loop_label = NEW_LABEL(location.line);
2693 LABEL *find_succeeded_label = NEW_LABEL(location.line);
2694 LABEL *find_failed_label = NEW_LABEL(location.line);
2695
2696 PUSH_INSN(ret, location, dup);
2697 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2698
2699 PUSH_INSN(ret, location, dup);
2700 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2701 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2702 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2703 PUSH_LABEL(ret, while_begin_label);
2704
2705 PUSH_INSN(ret, location, dup);
2706 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2707 PUSH_SEND(ret, location, idLE, INT2FIX(1));
2708 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2709
2710 for (size_t index = 0; index < size; index++) {
2711 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2712 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2713
2714 if (index != 0) {
2715 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2716 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2717 }
2718
2719 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2720 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));
2721 }
2722
2723 const pm_splat_node_t *left = cast->left;
2724
2725 if (left->expression != NULL) {
2726 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2727 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2728 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2729 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2730 CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
2731 }
2732
2733 RUBY_ASSERT(PM_NODE_TYPE_P(cast->right, PM_SPLAT_NODE));
2734 const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
2735
2736 if (right->expression != NULL) {
2737 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2738 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2739 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2740 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2741 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2742 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2743 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
2744 }
2745
2746 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2747
2748 PUSH_LABEL(ret, next_loop_label);
2749 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
2750 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2751 PUSH_INSNL(ret, location, jump, while_begin_label);
2752
2753 PUSH_LABEL(ret, find_failed_label);
2754 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2755 if (in_single_pattern) {
2756 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2757
2758 {
2759 VALUE operand = rb_fstring_lit("%p does not match to find pattern");
2760 PUSH_INSN1(ret, location, putobject, operand);
2761 }
2762
2763 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2764 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
2765 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2766
2767 PUSH_INSN1(ret, location, putobject, Qfalse);
2768 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2769
2770 PUSH_INSN(ret, location, pop);
2771 PUSH_INSN(ret, location, pop);
2772 }
2773 PUSH_INSNL(ret, location, jump, match_failed_label);
2774 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2775
2776 PUSH_LABEL(ret, find_succeeded_label);
2777 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2778 }
2779
2780 PUSH_INSN(ret, location, pop);
2781 PUSH_INSNL(ret, location, jump, matched_label);
2782 PUSH_INSN(ret, location, putnil);
2783
2784 PUSH_LABEL(ret, type_error_label);
2785 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2786 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2787
2788 {
2789 VALUE operand = rb_fstring_lit("deconstruct must return Array");
2790 PUSH_INSN1(ret, location, putobject, operand);
2791 }
2792
2793 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2794 PUSH_INSN(ret, location, pop);
2795
2796 PUSH_LABEL(ret, match_failed_label);
2797 PUSH_INSN(ret, location, pop);
2798 PUSH_INSNL(ret, location, jump, unmatched_label);
2799
2800 break;
2801 }
2802 case PM_HASH_PATTERN_NODE: {
2803 // Hash patterns in pattern matching are triggered by using labels and
2804 // values in a pattern or by using the ** operator. They are represented
2805 // by the HashPatternNode. This looks like:
2806 //
2807 // foo => { a: 1, b: 2, **bar }
2808 //
2809 // It can optionally have an assoc splat in the middle of it, which can
2810 // optionally have a name.
2811 const pm_hash_pattern_node_t *cast = (const pm_hash_pattern_node_t *) node;
2812
2813 // We don't consider it a "rest" parameter if it's a ** that is unnamed.
2814 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);
2815 bool has_keys = cast->elements.size > 0 || cast->rest != NULL;
2816
2817 LABEL *match_failed_label = NEW_LABEL(location.line);
2818 LABEL *type_error_label = NEW_LABEL(location.line);
2819 VALUE keys = Qnil;
2820
2821 if (has_keys && !has_rest) {
2822 keys = rb_ary_new_capa(cast->elements.size);
2823
2824 for (size_t index = 0; index < cast->elements.size; index++) {
2825 const pm_node_t *element = cast->elements.nodes[index];
2826 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2827
2828 const pm_node_t *key = ((const pm_assoc_node_t *) element)->key;
2829 RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
2830
2831 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2832 rb_ary_push(keys, symbol);
2833 }
2834 }
2835
2836 if (cast->constant) {
2837 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2838 }
2839
2840 PUSH_INSN(ret, location, dup);
2841
2842 {
2843 VALUE operand = ID2SYM(rb_intern("deconstruct_keys"));
2844 PUSH_INSN1(ret, location, putobject, operand);
2845 }
2846
2847 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2848 if (in_single_pattern) {
2849 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
2850 }
2851 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2852
2853 if (NIL_P(keys)) {
2854 PUSH_INSN(ret, location, putnil);
2855 }
2856 else {
2857 PUSH_INSN1(ret, location, duparray, keys);
2858 RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
2859 }
2860 PUSH_SEND(ret, location, rb_intern("deconstruct_keys"), INT2FIX(1));
2861
2862 PUSH_INSN(ret, location, dup);
2863 PUSH_INSN1(ret, location, checktype, INT2FIX(T_HASH));
2864 PUSH_INSNL(ret, location, branchunless, type_error_label);
2865
2866 if (has_rest) {
2867 PUSH_SEND(ret, location, rb_intern("dup"), INT2FIX(0));
2868 }
2869
2870 if (has_keys) {
2871 DECL_ANCHOR(match_values);
2872
2873 for (size_t index = 0; index < cast->elements.size; index++) {
2874 const pm_node_t *element = cast->elements.nodes[index];
2875 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2876
2877 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
2878 const pm_node_t *key = assoc->key;
2879 RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
2880
2881 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2882 PUSH_INSN(ret, location, dup);
2883 PUSH_INSN1(ret, location, putobject, symbol);
2884 PUSH_SEND(ret, location, rb_intern("key?"), INT2FIX(1));
2885
2886 if (in_single_pattern) {
2887 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2888
2889 PUSH_INSN(ret, location, dup);
2890 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2891
2892 {
2893 VALUE operand = rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol));
2894 PUSH_INSN1(ret, location, putobject, operand);
2895 }
2896
2897 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2898 PUSH_INSN1(ret, location, putobject, Qtrue);
2899 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2900 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2901 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2902 PUSH_INSN1(ret, location, putobject, symbol);
2903 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2904
2905 PUSH_INSN1(ret, location, adjuststack, INT2FIX(4));
2906 PUSH_LABEL(ret, match_succeeded_label);
2907 }
2908
2909 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2910 PUSH_INSN(match_values, location, dup);
2911 PUSH_INSN1(match_values, location, putobject, symbol);
2912 PUSH_SEND(match_values, location, has_rest ? rb_intern("delete") : idAREF, INT2FIX(1));
2913
2914 const pm_node_t *value = assoc->value;
2915 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2916 value = ((const pm_implicit_node_t *) value)->value;
2917 }
2918
2919 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2920 }
2921
2922 PUSH_SEQ(ret, match_values);
2923 }
2924 else {
2925 PUSH_INSN(ret, location, dup);
2926 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2927 if (in_single_pattern) {
2928 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p is not empty"), base_index + 1));
2929 }
2930 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2931 }
2932
2933 if (has_rest) {
2934 switch (PM_NODE_TYPE(cast->rest)) {
2935 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2936 PUSH_INSN(ret, location, dup);
2937 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2938 if (in_single_pattern) {
2939 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("rest of %p is not empty"), base_index + 1);
2940 }
2941 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2942 break;
2943 }
2944 case PM_ASSOC_SPLAT_NODE: {
2945 const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
2946 PUSH_INSN(ret, location, dup);
2947 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
2948 break;
2949 }
2950 default:
2951 rb_bug("unreachable");
2952 break;
2953 }
2954 }
2955
2956 PUSH_INSN(ret, location, pop);
2957 PUSH_INSNL(ret, location, jump, matched_label);
2958 PUSH_INSN(ret, location, putnil);
2959
2960 PUSH_LABEL(ret, type_error_label);
2961 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2962 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2963
2964 {
2965 VALUE operand = rb_fstring_lit("deconstruct_keys must return Hash");
2966 PUSH_INSN1(ret, location, putobject, operand);
2967 }
2968
2969 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2970 PUSH_INSN(ret, location, pop);
2971
2972 PUSH_LABEL(ret, match_failed_label);
2973 PUSH_INSN(ret, location, pop);
2974 PUSH_INSNL(ret, location, jump, unmatched_label);
2975 break;
2976 }
2977 case PM_CAPTURE_PATTERN_NODE: {
2978 // Capture patterns allow you to pattern match against an element in a
2979 // pattern and also capture the value into a local variable. This looks
2980 // like:
2981 //
2982 // [1] => [Integer => foo]
2983 //
2984 // In this case the `Integer => foo` will be represented by a
2985 // CapturePatternNode, which has both a value (the pattern to match
2986 // against) and a target (the place to write the variable into).
2987 const pm_capture_pattern_node_t *cast = (const pm_capture_pattern_node_t *) node;
2988
2989 LABEL *match_failed_label = NEW_LABEL(location.line);
2990
2991 PUSH_INSN(ret, location, dup);
2992 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));
2993 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));
2994 PUSH_INSN(ret, location, putnil);
2995
2996 PUSH_LABEL(ret, match_failed_label);
2997 PUSH_INSN(ret, location, pop);
2998 PUSH_INSNL(ret, location, jump, unmatched_label);
2999
3000 break;
3001 }
3002 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3003 // Local variables can be targeted by placing identifiers in the place
3004 // of a pattern. For example, foo in bar. This results in the value
3005 // being matched being written to that local variable.
3007 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
3008
3009 // If this local variable is being written from within an alternation
3010 // pattern, then it cannot actually be added to the local table since
3011 // it's ambiguous which value should be used. So instead we indicate
3012 // this with a compile error.
3013 if (in_alternation_pattern) {
3014 ID id = pm_constant_id_lookup(scope_node, cast->name);
3015 const char *name = rb_id2name(id);
3016
3017 if (name && strlen(name) > 0 && name[0] != '_') {
3018 COMPILE_ERROR(iseq, location.line, "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
3019 return COMPILE_NG;
3020 }
3021 }
3022
3023 PUSH_SETLOCAL(ret, location, index.index, index.level);
3024 PUSH_INSNL(ret, location, jump, matched_label);
3025 break;
3026 }
3027 case PM_ALTERNATION_PATTERN_NODE: {
3028 // Alternation patterns allow you to specify multiple patterns in a
3029 // single expression using the | operator.
3031
3032 LABEL *matched_left_label = NEW_LABEL(location.line);
3033 LABEL *unmatched_left_label = NEW_LABEL(location.line);
3034
3035 // First, we're going to attempt to match against the left pattern. If
3036 // that pattern matches, then we'll skip matching the right pattern.
3037 PUSH_INSN(ret, location, dup);
3038 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));
3039
3040 // If we get here, then we matched on the left pattern. In this case we
3041 // should pop out the duplicate value that we preemptively added to
3042 // match against the right pattern and then jump to the match label.
3043 PUSH_LABEL(ret, matched_left_label);
3044 PUSH_INSN(ret, location, pop);
3045 PUSH_INSNL(ret, location, jump, matched_label);
3046 PUSH_INSN(ret, location, putnil);
3047
3048 // If we get here, then we didn't match on the left pattern. In this
3049 // case we attempt to match against the right pattern.
3050 PUSH_LABEL(ret, unmatched_left_label);
3051 CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, use_deconstructed_cache, base_index));
3052 break;
3053 }
3054 case PM_PARENTHESES_NODE:
3055 // Parentheses are allowed to wrap expressions in pattern matching and
3056 // they do nothing since they can only wrap individual expressions and
3057 // not groups. In this case we'll recurse back into this same function
3058 // with the body of the parentheses.
3059 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);
3060 case PM_PINNED_EXPRESSION_NODE:
3061 // Pinned expressions are a way to match against the value of an
3062 // expression that should be evaluated at runtime. This looks like:
3063 // foo in ^(bar). To compile these, we compile the expression as if it
3064 // were a literal value by falling through to the literal case.
3065 node = ((const pm_pinned_expression_node_t *) node)->expression;
3066 /* fallthrough */
3067 case PM_ARRAY_NODE:
3068 case PM_CLASS_VARIABLE_READ_NODE:
3069 case PM_CONSTANT_PATH_NODE:
3070 case PM_CONSTANT_READ_NODE:
3071 case PM_FALSE_NODE:
3072 case PM_FLOAT_NODE:
3073 case PM_GLOBAL_VARIABLE_READ_NODE:
3074 case PM_IMAGINARY_NODE:
3075 case PM_INSTANCE_VARIABLE_READ_NODE:
3076 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3077 case PM_INTEGER_NODE:
3078 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3079 case PM_INTERPOLATED_STRING_NODE:
3080 case PM_INTERPOLATED_SYMBOL_NODE:
3081 case PM_INTERPOLATED_X_STRING_NODE:
3082 case PM_LAMBDA_NODE:
3083 case PM_LOCAL_VARIABLE_READ_NODE:
3084 case PM_NIL_NODE:
3085 case PM_SOURCE_ENCODING_NODE:
3086 case PM_SOURCE_FILE_NODE:
3087 case PM_SOURCE_LINE_NODE:
3088 case PM_RANGE_NODE:
3089 case PM_RATIONAL_NODE:
3090 case PM_REGULAR_EXPRESSION_NODE:
3091 case PM_SELF_NODE:
3092 case PM_STRING_NODE:
3093 case PM_SYMBOL_NODE:
3094 case PM_TRUE_NODE:
3095 case PM_X_STRING_NODE: {
3096 // These nodes are all simple patterns, which means we'll use the
3097 // checkmatch instruction to match against them, which is effectively a
3098 // VM-level === operator.
3099 PM_COMPILE_NOT_POPPED(node);
3100 if (in_single_pattern) {
3101 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
3102 }
3103
3104 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3105
3106 if (in_single_pattern) {
3107 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3108 }
3109
3110 PUSH_INSNL(ret, location, branchif, matched_label);
3111 PUSH_INSNL(ret, location, jump, unmatched_label);
3112 break;
3113 }
3114 case PM_PINNED_VARIABLE_NODE: {
3115 // Pinned variables are a way to match against the value of a variable
3116 // without it looking like you're trying to write to the variable. This
3117 // looks like: foo in ^@bar. To compile these, we compile the variable
3118 // that they hold.
3119 const pm_pinned_variable_node_t *cast = (const pm_pinned_variable_node_t *) node;
3120 CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
3121 break;
3122 }
3123 case PM_IF_NODE:
3124 case PM_UNLESS_NODE: {
3125 // If and unless nodes can show up here as guards on `in` clauses. This
3126 // looks like:
3127 //
3128 // case foo
3129 // in bar if baz?
3130 // qux
3131 // end
3132 //
3133 // Because we know they're in the modifier form and they can't have any
3134 // variation on this pattern, we compile them differently (more simply)
3135 // here than we would in the normal compilation path.
3136 const pm_node_t *predicate;
3137 const pm_node_t *statement;
3138
3139 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3140 const pm_if_node_t *cast = (const pm_if_node_t *) node;
3141 predicate = cast->predicate;
3142
3143 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
3144 statement = cast->statements->body.nodes[0];
3145 }
3146 else {
3147 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
3148 predicate = cast->predicate;
3149
3150 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
3151 statement = cast->statements->body.nodes[0];
3152 }
3153
3154 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
3155 PM_COMPILE_NOT_POPPED(predicate);
3156
3157 if (in_single_pattern) {
3158 LABEL *match_succeeded_label = NEW_LABEL(location.line);
3159
3160 PUSH_INSN(ret, location, dup);
3161 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3162 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3163 }
3164 else {
3165 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3166 }
3167
3168 {
3169 VALUE operand = rb_fstring_lit("guard clause does not return true");
3170 PUSH_INSN1(ret, location, putobject, operand);
3171 }
3172
3173 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3174 PUSH_INSN1(ret, location, putobject, Qfalse);
3175 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3176
3177 PUSH_INSN(ret, location, pop);
3178 PUSH_INSN(ret, location, pop);
3179
3180 PUSH_LABEL(ret, match_succeeded_label);
3181 }
3182
3183 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3184 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3185 }
3186 else {
3187 PUSH_INSNL(ret, location, branchif, unmatched_label);
3188 }
3189
3190 PUSH_INSNL(ret, location, jump, matched_label);
3191 break;
3192 }
3193 default:
3194 // If we get here, then we have a node type that should not be in this
3195 // position. This would be a bug in the parser, because a different node
3196 // type should never have been created in this position in the tree.
3197 rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3198 break;
3199 }
3200
3201 return COMPILE_OK;
3202}
3203
3204#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3205#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3206#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3207#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3208#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3209
3210// Generate a scope node from the given node.
3211void
3212pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
3213{
3214 // This is very important, otherwise the scope node could be seen as having
3215 // certain flags set that _should not_ be set.
3216 memset(scope, 0, sizeof(pm_scope_node_t));
3217
3218 scope->base.type = PM_SCOPE_NODE;
3219 scope->base.location.start = node->location.start;
3220 scope->base.location.end = node->location.end;
3221
3222 scope->previous = previous;
3223 scope->ast_node = (pm_node_t *) node;
3224
3225 if (previous) {
3226 scope->parser = previous->parser;
3227 scope->encoding = previous->encoding;
3228 scope->filepath_encoding = previous->filepath_encoding;
3229 scope->constants = previous->constants;
3230 scope->coverage_enabled = previous->coverage_enabled;
3231 scope->script_lines = previous->script_lines;
3232 }
3233
3234 switch (PM_NODE_TYPE(node)) {
3235 case PM_BLOCK_NODE: {
3236 const pm_block_node_t *cast = (const pm_block_node_t *) node;
3237 scope->body = cast->body;
3238 scope->locals = cast->locals;
3239 scope->parameters = cast->parameters;
3240 break;
3241 }
3242 case PM_CLASS_NODE: {
3243 const pm_class_node_t *cast = (const pm_class_node_t *) node;
3244 scope->body = cast->body;
3245 scope->locals = cast->locals;
3246 break;
3247 }
3248 case PM_DEF_NODE: {
3249 const pm_def_node_t *cast = (const pm_def_node_t *) node;
3250 scope->parameters = (pm_node_t *) cast->parameters;
3251 scope->body = cast->body;
3252 scope->locals = cast->locals;
3253 break;
3254 }
3255 case PM_ENSURE_NODE: {
3256 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
3257 scope->body = (pm_node_t *) node;
3258
3259 if (cast->statements != NULL) {
3260 scope->base.location.start = cast->statements->base.location.start;
3261 scope->base.location.end = cast->statements->base.location.end;
3262 }
3263
3264 break;
3265 }
3266 case PM_FOR_NODE: {
3267 const pm_for_node_t *cast = (const pm_for_node_t *) node;
3268 scope->body = (pm_node_t *) cast->statements;
3269 break;
3270 }
3271 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3272 RUBY_ASSERT(node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE);
3273 scope->body = (pm_node_t *) node;
3274 break;
3275 }
3276 case PM_LAMBDA_NODE: {
3277 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
3278 scope->parameters = cast->parameters;
3279 scope->body = cast->body;
3280 scope->locals = cast->locals;
3281
3282 if (cast->parameters != NULL) {
3283 scope->base.location.start = cast->parameters->location.start;
3284 }
3285 else {
3286 scope->base.location.start = cast->operator_loc.end;
3287 }
3288 break;
3289 }
3290 case PM_MODULE_NODE: {
3291 const pm_module_node_t *cast = (const pm_module_node_t *) node;
3292 scope->body = cast->body;
3293 scope->locals = cast->locals;
3294 break;
3295 }
3296 case PM_POST_EXECUTION_NODE: {
3297 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) node;
3298 scope->body = (pm_node_t *) cast->statements;
3299 break;
3300 }
3301 case PM_PROGRAM_NODE: {
3302 const pm_program_node_t *cast = (const pm_program_node_t *) node;
3303 scope->body = (pm_node_t *) cast->statements;
3304 scope->locals = cast->locals;
3305 break;
3306 }
3307 case PM_RESCUE_NODE: {
3308 const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
3309 scope->body = (pm_node_t *) cast->statements;
3310 break;
3311 }
3312 case PM_RESCUE_MODIFIER_NODE: {
3313 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
3314 scope->body = (pm_node_t *) cast->rescue_expression;
3315 break;
3316 }
3317 case PM_SINGLETON_CLASS_NODE: {
3318 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
3319 scope->body = cast->body;
3320 scope->locals = cast->locals;
3321 break;
3322 }
3323 case PM_STATEMENTS_NODE: {
3324 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
3325 scope->body = (pm_node_t *) cast;
3326 break;
3327 }
3328 default:
3329 rb_bug("unreachable");
3330 break;
3331 }
3332}
3333
3334void
3335pm_scope_node_destroy(pm_scope_node_t *scope_node)
3336{
3337 if (scope_node->index_lookup_table) {
3338 st_free_table(scope_node->index_lookup_table);
3339 }
3340}
3341
3353static void
3354pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *retry_end_l)
3355{
3356 INSN *iobj;
3357 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
3358 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
3359 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3360 iobj = (INSN*) get_prev_insn(iobj);
3361 }
3362 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
3363
3364 // LINK_ANCHOR has a pointer to the last element, but
3365 // ELEM_INSERT_NEXT does not update it even if we add an insn to the
3366 // last of LINK_ANCHOR. So this updates it manually.
3367 if (&iobj->link == LAST_ELEMENT(ret)) {
3368 ret->last = (LINK_ELEMENT*) retry_end_l;
3369 }
3370}
3371
3372static const char *
3373pm_iseq_builtin_function_name(const pm_scope_node_t *scope_node, const pm_node_t *receiver, ID method_id)
3374{
3375 const char *name = rb_id2name(method_id);
3376 static const char prefix[] = "__builtin_";
3377 const size_t prefix_len = sizeof(prefix) - 1;
3378
3379 if (receiver == NULL) {
3380 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3381 // __builtin_foo
3382 return &name[prefix_len];
3383 }
3384 }
3385 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3386 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3387 const pm_call_node_t *cast = (const pm_call_node_t *) receiver;
3388 if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("__builtin")) {
3389 // __builtin.foo
3390 return name;
3391 }
3392 }
3393 }
3394 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3395 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) receiver;
3396 if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("Primitive")) {
3397 // Primitive.foo
3398 return name;
3399 }
3400 }
3401
3402 return NULL;
3403}
3404
3405// Compile Primitive.attr! :leaf, ...
3406static int
3407pm_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)
3408{
3409 if (arguments == NULL) {
3410 COMPILE_ERROR(iseq, node_location->line, "attr!: no argument");
3411 return COMPILE_NG;
3412 }
3413
3414 const pm_node_t *argument;
3415 PM_NODE_LIST_FOREACH(&arguments->arguments, index, argument) {
3416 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3417 COMPILE_ERROR(iseq, node_location->line, "non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3418 return COMPILE_NG;
3419 }
3420
3421 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3422 VALUE string = rb_sym2str(symbol);
3423
3424 if (strcmp(RSTRING_PTR(string), "leaf") == 0) {
3425 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3426 }
3427 else if (strcmp(RSTRING_PTR(string), "inline_block") == 0) {
3428 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3429 }
3430 else if (strcmp(RSTRING_PTR(string), "use_block") == 0) {
3431 iseq_set_use_block(iseq);
3432 }
3433 else if (strcmp(RSTRING_PTR(string), "c_trace") == 0) {
3434 // Let the iseq act like a C method in backtraces
3435 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3436 }
3437 else {
3438 COMPILE_ERROR(iseq, node_location->line, "unknown argument to attr!: %s", RSTRING_PTR(string));
3439 return COMPILE_NG;
3440 }
3441 }
3442
3443 return COMPILE_OK;
3444}
3445
3446static int
3447pm_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)
3448{
3449 if (arguments == NULL) {
3450 COMPILE_ERROR(iseq, node_location->line, "arg!: no argument");
3451 return COMPILE_NG;
3452 }
3453
3454 if (arguments->arguments.size != 1) {
3455 COMPILE_ERROR(iseq, node_location->line, "arg!: too many argument");
3456 return COMPILE_NG;
3457 }
3458
3459 const pm_node_t *argument = arguments->arguments.nodes[0];
3460 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3461 COMPILE_ERROR(iseq, node_location->line, "non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3462 return COMPILE_NG;
3463 }
3464
3465 if (!popped) {
3466 ID name = parse_string_symbol(scope_node, ((const pm_symbol_node_t *) argument));
3467 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3468
3469 debugs("id: %s idx: %d\n", rb_id2name(name), index);
3470 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3471 }
3472
3473 return COMPILE_OK;
3474}
3475
3476static int
3477pm_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)
3478{
3479 const pm_node_t *ast_node = scope_node->ast_node;
3480 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3481 rb_bug("mandatory_only?: not in method definition");
3482 return COMPILE_NG;
3483 }
3484
3485 const pm_def_node_t *def_node = (const pm_def_node_t *) ast_node;
3486 const pm_parameters_node_t *parameters_node = def_node->parameters;
3487 if (parameters_node == NULL) {
3488 rb_bug("mandatory_only?: in method definition with no parameters");
3489 return COMPILE_NG;
3490 }
3491
3492 const pm_node_t *body_node = def_node->body;
3493 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)) {
3494 rb_bug("mandatory_only?: not in method definition with plain statements");
3495 return COMPILE_NG;
3496 }
3497
3498 const pm_if_node_t *if_node = (const pm_if_node_t *) ((const pm_statements_node_t *) body_node)->body.nodes[0];
3499 if (if_node->predicate != ((const pm_node_t *) call_node)) {
3500 rb_bug("mandatory_only?: can't find mandatory node");
3501 return COMPILE_NG;
3502 }
3503
3504 pm_parameters_node_t parameters = {
3505 .base = parameters_node->base,
3506 .requireds = parameters_node->requireds
3507 };
3508
3509 const pm_def_node_t def = {
3510 .base = def_node->base,
3511 .name = def_node->name,
3512 .receiver = def_node->receiver,
3513 .parameters = &parameters,
3514 .body = (pm_node_t *) if_node->statements,
3515 .locals = {
3516 .ids = def_node->locals.ids,
3517 .size = parameters_node->requireds.size,
3518 .capacity = def_node->locals.capacity
3519 }
3520 };
3521
3522 pm_scope_node_t next_scope_node;
3523 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3524
3525 int error_state;
3526 const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
3527 &next_scope_node,
3528 rb_iseq_base_label(iseq),
3529 rb_iseq_path(iseq),
3530 rb_iseq_realpath(iseq),
3531 node_location->line,
3532 NULL,
3533 0,
3534 ISEQ_TYPE_METHOD,
3535 ISEQ_COMPILE_DATA(iseq)->option,
3536 &error_state
3537 );
3538 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (VALUE)mandatory_only_iseq);
3539
3540 if (error_state) {
3541 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3542 rb_jump_tag(error_state);
3543 }
3544
3545 pm_scope_node_destroy(&next_scope_node);
3546 return COMPILE_OK;
3547}
3548
3549static int
3550pm_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)
3551{
3552 const pm_arguments_node_t *arguments = call_node->arguments;
3553
3554 if (parent_block != NULL) {
3555 COMPILE_ERROR(iseq, node_location->line, "should not call builtins here.");
3556 return COMPILE_NG;
3557 }
3558
3559#define BUILTIN_INLINE_PREFIX "_bi"
3560 char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)];
3561 bool cconst = false;
3562retry:;
3563 const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func);
3564
3565 if (bf == NULL) {
3566 if (strcmp("cstmt!", builtin_func) == 0 || strcmp("cexpr!", builtin_func) == 0) {
3567 // ok
3568 }
3569 else if (strcmp("cconst!", builtin_func) == 0) {
3570 cconst = true;
3571 }
3572 else if (strcmp("cinit!", builtin_func) == 0) {
3573 // ignore
3574 return COMPILE_OK;
3575 }
3576 else if (strcmp("attr!", builtin_func) == 0) {
3577 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3578 }
3579 else if (strcmp("arg!", builtin_func) == 0) {
3580 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3581 }
3582 else if (strcmp("mandatory_only?", builtin_func) == 0) {
3583 if (popped) {
3584 rb_bug("mandatory_only? should be in if condition");
3585 }
3586 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3587 rb_bug("mandatory_only? should be put on top");
3588 }
3589
3590 PUSH_INSN1(ret, *node_location, putobject, Qfalse);
3591 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3592 }
3593 else if (1) {
3594 rb_bug("can't find builtin function:%s", builtin_func);
3595 }
3596 else {
3597 COMPILE_ERROR(iseq, node_location->line, "can't find builtin function:%s", builtin_func);
3598 return COMPILE_NG;
3599 }
3600
3601 int inline_index = node_location->line;
3602 snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
3603 builtin_func = inline_func;
3604 arguments = NULL;
3605 goto retry;
3606 }
3607
3608 if (cconst) {
3609 typedef VALUE(*builtin_func0)(void *, VALUE);
3610 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL, Qnil);
3611 PUSH_INSN1(ret, *node_location, putobject, const_val);
3612 return COMPILE_OK;
3613 }
3614
3615 // fprintf(stderr, "func_name:%s -> %p\n", builtin_func, bf->func_ptr);
3616
3617 DECL_ANCHOR(args_seq);
3618
3619 int flags = 0;
3620 struct rb_callinfo_kwarg *keywords = NULL;
3621 int argc = pm_setup_args(arguments, call_node->block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3622
3623 if (argc != bf->argc) {
3624 COMPILE_ERROR(iseq, node_location->line, "argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3625 return COMPILE_NG;
3626 }
3627
3628 unsigned int start_index;
3629 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3630 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf, INT2FIX(start_index));
3631 }
3632 else {
3633 PUSH_SEQ(ret, args_seq);
3634 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3635 }
3636
3637 if (popped) PUSH_INSN(ret, *node_location, pop);
3638 return COMPILE_OK;
3639}
3640
3644static void
3645pm_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)
3646{
3647 const pm_location_t *message_loc = &call_node->message_loc;
3648 if (message_loc->start == NULL) message_loc = &call_node->base.location;
3649
3650 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, call_node->base.node_id);
3651
3652 LABEL *else_label = NEW_LABEL(location.line);
3653 LABEL *end_label = NEW_LABEL(location.line);
3654 LABEL *retry_end_l = NEW_LABEL(location.line);
3655
3656 VALUE branches = Qfalse;
3657 rb_code_location_t code_location = { 0 };
3658 int node_id = location.node_id;
3659
3660 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3661 if (PM_BRANCH_COVERAGE_P(iseq)) {
3662 const uint8_t *cursors[3] = {
3663 call_node->closing_loc.end,
3664 call_node->arguments == NULL ? NULL : call_node->arguments->base.location.end,
3665 call_node->message_loc.end
3666 };
3667
3668 const uint8_t *end_cursor = cursors[0];
3669 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3670 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3671 if (!end_cursor) end_cursor = call_node->closing_loc.end;
3672
3673 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3674 const pm_line_column_t end_location = pm_newline_list_line_column(&scope_node->parser->newline_list, end_cursor, scope_node->parser->start_line);
3675
3676 code_location = (rb_code_location_t) {
3677 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
3678 .end_pos = { .lineno = end_location.line, .column = end_location.column }
3679 };
3680
3681 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location, "&.");
3682 }
3683
3684 PUSH_INSN(ret, location, dup);
3685 PUSH_INSNL(ret, location, branchnil, else_label);
3686
3687 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0, "then", branches);
3688 }
3689
3690 LINK_ELEMENT *opt_new_prelude = LAST_ELEMENT(ret);
3691
3692 int flags = 0;
3693 struct rb_callinfo_kwarg *kw_arg = NULL;
3694
3695 int orig_argc = pm_setup_args(call_node->arguments, call_node->block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3696 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3697 const rb_iseq_t *block_iseq = NULL;
3698
3699 if (call_node->block != NULL && PM_NODE_TYPE_P(call_node->block, PM_BLOCK_NODE)) {
3700 // Scope associated with the block
3701 pm_scope_node_t next_scope_node;
3702 pm_scope_node_init(call_node->block, &next_scope_node, scope_node);
3703
3704 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));
3705 pm_scope_node_destroy(&next_scope_node);
3706 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3707 }
3708 else {
3709 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3710 flags |= VM_CALL_VCALL;
3711 }
3712
3713 if (!flags) {
3714 flags |= VM_CALL_ARGS_SIMPLE;
3715 }
3716 }
3717
3718 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3719 flags |= VM_CALL_FCALL;
3720 }
3721
3722 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3723 if (flags & VM_CALL_ARGS_BLOCKARG) {
3724 PUSH_INSN1(ret, location, topn, INT2FIX(1));
3725 if (flags & VM_CALL_ARGS_SPLAT) {
3726 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3727 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3728 }
3729 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 3));
3730 PUSH_INSN(ret, location, pop);
3731 }
3732 else if (flags & VM_CALL_ARGS_SPLAT) {
3733 PUSH_INSN(ret, location, dup);
3734 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3735 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3736 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 2));
3737 PUSH_INSN(ret, location, pop);
3738 }
3739 else {
3740 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 1));
3741 }
3742 }
3743
3744 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3745 PUSH_INSN(ret, location, splatkw);
3746 }
3747
3748 LABEL *not_basic_new = NEW_LABEL(location.line);
3749 LABEL *not_basic_new_finish = NEW_LABEL(location.line);
3750
3751 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
3752 method_id == rb_intern("new") &&
3753 call_node->block == NULL &&
3754 (flags & VM_CALL_ARGS_BLOCKARG) == 0;
3755
3756 if (inline_new) {
3757 if (LAST_ELEMENT(ret) == opt_new_prelude) {
3758 PUSH_INSN(ret, location, putnil);
3759 PUSH_INSN(ret, location, swap);
3760 }
3761 else {
3762 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.line, location.node_id, BIN(swap), 0)->link);
3763 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.line, location.node_id, BIN(putnil), 0)->link);
3764 }
3765
3766 // Jump unless the receiver uses the "basic" implementation of "new"
3767 VALUE ci;
3768 if (flags & VM_CALL_FORWARDING) {
3769 ci = (VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
3770 }
3771 else {
3772 ci = (VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
3773 }
3774
3775 PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
3776 LABEL_REF(not_basic_new);
3777 // optimized path
3778 PUSH_SEND_R(ret, location, rb_intern("initialize"), INT2FIX(orig_argc), block_iseq, INT2FIX(flags | VM_CALL_FCALL), kw_arg);
3779 PUSH_INSNL(ret, location, jump, not_basic_new_finish);
3780
3781 PUSH_LABEL(ret, not_basic_new);
3782 // Fall back to normal send
3783 PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
3784 PUSH_INSN(ret, location, swap);
3785
3786 PUSH_LABEL(ret, not_basic_new_finish);
3787 PUSH_INSN(ret, location, pop);
3788 }
3789 else {
3790 PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
3791 }
3792
3793 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3794 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3795 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3796 }
3797
3798 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3799 PUSH_INSNL(ret, location, jump, end_label);
3800 PUSH_LABEL(ret, else_label);
3801 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1, "else", branches);
3802 PUSH_LABEL(ret, end_label);
3803 }
3804
3805 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3806 PUSH_INSN(ret, location, pop);
3807 }
3808
3809 if (popped) PUSH_INSN(ret, location, pop);
3810 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3811}
3812
3817static inline VALUE
3818pm_compile_back_reference_ref(const pm_back_reference_read_node_t *node)
3819{
3820 const char *type = (const char *) (node->base.location.start + 1);
3821
3822 // Since a back reference is `$<char>`, Ruby represents the ID as an
3823 // rb_intern on the value after the `$`.
3824 return INT2FIX(rb_intern2(type, 1)) << 1 | 1;
3825}
3826
3831static inline VALUE
3832pm_compile_numbered_reference_ref(const pm_numbered_reference_read_node_t *node)
3833{
3834 return INT2FIX(node->number << 1);
3835}
3836
3837static void
3838pm_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)
3839{
3840#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3841
3842 // in_condition is the same as compile.c's needstr
3843 enum defined_type dtype = DEFINED_NOT_DEFINED;
3844 const pm_node_location_t location = *node_location;
3845
3846 switch (PM_NODE_TYPE(node)) {
3847/* DEFINED_NIL ****************************************************************/
3848 case PM_NIL_NODE:
3849 // defined?(nil)
3850 // ^^^
3851 dtype = DEFINED_NIL;
3852 break;
3853/* DEFINED_IVAR ***************************************************************/
3854 case PM_INSTANCE_VARIABLE_READ_NODE: {
3855 // defined?(@a)
3856 // ^^
3858 ID name = pm_constant_id_lookup(scope_node, cast->name);
3859
3860 PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3861
3862 return;
3863 }
3864/* DEFINED_LVAR ***************************************************************/
3865 case PM_LOCAL_VARIABLE_READ_NODE:
3866 // a = 1; defined?(a)
3867 // ^
3868 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3869 // 1.then { defined?(it) }
3870 // ^^
3871 dtype = DEFINED_LVAR;
3872 break;
3873/* DEFINED_GVAR ***************************************************************/
3874 case PM_GLOBAL_VARIABLE_READ_NODE: {
3875 // defined?($a)
3876 // ^^
3878 ID name = pm_constant_id_lookup(scope_node, cast->name);
3879
3880 PUSH_INSN(ret, location, putnil);
3881 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
3882
3883 return;
3884 }
3885/* DEFINED_CVAR ***************************************************************/
3886 case PM_CLASS_VARIABLE_READ_NODE: {
3887 // defined?(@@a)
3888 // ^^^
3890 ID name = pm_constant_id_lookup(scope_node, cast->name);
3891
3892 PUSH_INSN(ret, location, putnil);
3893 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
3894
3895 return;
3896 }
3897/* DEFINED_CONST **************************************************************/
3898 case PM_CONSTANT_READ_NODE: {
3899 // defined?(A)
3900 // ^
3901 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
3902 ID name = pm_constant_id_lookup(scope_node, cast->name);
3903
3904 PUSH_INSN(ret, location, putnil);
3905 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
3906
3907 return;
3908 }
3909/* DEFINED_YIELD **************************************************************/
3910 case PM_YIELD_NODE:
3911 // defined?(yield)
3912 // ^^^^^
3913 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
3914
3915 PUSH_INSN(ret, location, putnil);
3916 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3917
3918 return;
3919/* DEFINED_ZSUPER *************************************************************/
3920 case PM_SUPER_NODE: {
3921 // defined?(super 1, 2)
3922 // ^^^^^^^^^^
3923 const pm_super_node_t *cast = (const pm_super_node_t *) node;
3924
3925 if (cast->block != NULL && !PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
3926 dtype = DEFINED_EXPR;
3927 break;
3928 }
3929
3930 PUSH_INSN(ret, location, putnil);
3931 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3932 return;
3933 }
3934 case PM_FORWARDING_SUPER_NODE: {
3935 // defined?(super)
3936 // ^^^^^
3937 const pm_forwarding_super_node_t *cast = (const pm_forwarding_super_node_t *) node;
3938
3939 if (cast->block != NULL) {
3940 dtype = DEFINED_EXPR;
3941 break;
3942 }
3943
3944 PUSH_INSN(ret, location, putnil);
3945 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3946 return;
3947 }
3948/* DEFINED_SELF ***************************************************************/
3949 case PM_SELF_NODE:
3950 // defined?(self)
3951 // ^^^^
3952 dtype = DEFINED_SELF;
3953 break;
3954/* DEFINED_TRUE ***************************************************************/
3955 case PM_TRUE_NODE:
3956 // defined?(true)
3957 // ^^^^
3958 dtype = DEFINED_TRUE;
3959 break;
3960/* DEFINED_FALSE **************************************************************/
3961 case PM_FALSE_NODE:
3962 // defined?(false)
3963 // ^^^^^
3964 dtype = DEFINED_FALSE;
3965 break;
3966/* DEFINED_ASGN ***************************************************************/
3967 case PM_CALL_AND_WRITE_NODE:
3968 // defined?(a.a &&= 1)
3969 // ^^^^^^^^^
3970 case PM_CALL_OPERATOR_WRITE_NODE:
3971 // defined?(a.a += 1)
3972 // ^^^^^^^^
3973 case PM_CALL_OR_WRITE_NODE:
3974 // defined?(a.a ||= 1)
3975 // ^^^^^^^^^
3976 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3977 // defined?(@@a &&= 1)
3978 // ^^^^^^^^^
3979 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3980 // defined?(@@a += 1)
3981 // ^^^^^^^^
3982 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3983 // defined?(@@a ||= 1)
3984 // ^^^^^^^^^
3985 case PM_CLASS_VARIABLE_WRITE_NODE:
3986 // defined?(@@a = 1)
3987 // ^^^^^^^
3988 case PM_CONSTANT_AND_WRITE_NODE:
3989 // defined?(A &&= 1)
3990 // ^^^^^^^
3991 case PM_CONSTANT_OPERATOR_WRITE_NODE:
3992 // defined?(A += 1)
3993 // ^^^^^^
3994 case PM_CONSTANT_OR_WRITE_NODE:
3995 // defined?(A ||= 1)
3996 // ^^^^^^^
3997 case PM_CONSTANT_PATH_AND_WRITE_NODE:
3998 // defined?(A::A &&= 1)
3999 // ^^^^^^^^^^
4000 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
4001 // defined?(A::A += 1)
4002 // ^^^^^^^^^
4003 case PM_CONSTANT_PATH_OR_WRITE_NODE:
4004 // defined?(A::A ||= 1)
4005 // ^^^^^^^^^^
4006 case PM_CONSTANT_PATH_WRITE_NODE:
4007 // defined?(A::A = 1)
4008 // ^^^^^^^^
4009 case PM_CONSTANT_WRITE_NODE:
4010 // defined?(A = 1)
4011 // ^^^^^
4012 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
4013 // defined?($a &&= 1)
4014 // ^^^^^^^^
4015 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
4016 // defined?($a += 1)
4017 // ^^^^^^^
4018 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
4019 // defined?($a ||= 1)
4020 // ^^^^^^^^
4021 case PM_GLOBAL_VARIABLE_WRITE_NODE:
4022 // defined?($a = 1)
4023 // ^^^^^^
4024 case PM_INDEX_AND_WRITE_NODE:
4025 // defined?(a[1] &&= 1)
4026 // ^^^^^^^^^^
4027 case PM_INDEX_OPERATOR_WRITE_NODE:
4028 // defined?(a[1] += 1)
4029 // ^^^^^^^^^
4030 case PM_INDEX_OR_WRITE_NODE:
4031 // defined?(a[1] ||= 1)
4032 // ^^^^^^^^^^
4033 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
4034 // defined?(@a &&= 1)
4035 // ^^^^^^^^
4036 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
4037 // defined?(@a += 1)
4038 // ^^^^^^^
4039 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
4040 // defined?(@a ||= 1)
4041 // ^^^^^^^^
4042 case PM_INSTANCE_VARIABLE_WRITE_NODE:
4043 // defined?(@a = 1)
4044 // ^^^^^^
4045 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
4046 // defined?(a &&= 1)
4047 // ^^^^^^^
4048 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
4049 // defined?(a += 1)
4050 // ^^^^^^
4051 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
4052 // defined?(a ||= 1)
4053 // ^^^^^^^
4054 case PM_LOCAL_VARIABLE_WRITE_NODE:
4055 // defined?(a = 1)
4056 // ^^^^^
4057 case PM_MULTI_WRITE_NODE:
4058 // defined?((a, = 1))
4059 // ^^^^^^
4060 dtype = DEFINED_ASGN;
4061 break;
4062/* DEFINED_EXPR ***************************************************************/
4063 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4064 // defined?((alias $a $b))
4065 // ^^^^^^^^^^^
4066 case PM_ALIAS_METHOD_NODE:
4067 // defined?((alias a b))
4068 // ^^^^^^^^^
4069 case PM_AND_NODE:
4070 // defined?(a and b)
4071 // ^^^^^^^
4072 case PM_BREAK_NODE:
4073 // defined?(break 1)
4074 // ^^^^^^^
4075 case PM_CASE_MATCH_NODE:
4076 // defined?(case 1; in 1; end)
4077 // ^^^^^^^^^^^^^^^^^
4078 case PM_CASE_NODE:
4079 // defined?(case 1; when 1; end)
4080 // ^^^^^^^^^^^^^^^^^^^
4081 case PM_CLASS_NODE:
4082 // defined?(class Foo; end)
4083 // ^^^^^^^^^^^^^^
4084 case PM_DEF_NODE:
4085 // defined?(def a() end)
4086 // ^^^^^^^^^^^
4087 case PM_DEFINED_NODE:
4088 // defined?(defined?(a))
4089 // ^^^^^^^^^^^
4090 case PM_FLIP_FLOP_NODE:
4091 // defined?(not (a .. b))
4092 // ^^^^^^
4093 case PM_FLOAT_NODE:
4094 // defined?(1.0)
4095 // ^^^
4096 case PM_FOR_NODE:
4097 // defined?(for a in 1 do end)
4098 // ^^^^^^^^^^^^^^^^^
4099 case PM_IF_NODE:
4100 // defined?(if a then end)
4101 // ^^^^^^^^^^^^^
4102 case PM_IMAGINARY_NODE:
4103 // defined?(1i)
4104 // ^^
4105 case PM_INTEGER_NODE:
4106 // defined?(1)
4107 // ^
4108 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4109 // defined?(not /#{1}/)
4110 // ^^^^^^
4111 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4112 // defined?(/#{1}/)
4113 // ^^^^^^
4114 case PM_INTERPOLATED_STRING_NODE:
4115 // defined?("#{1}")
4116 // ^^^^^^
4117 case PM_INTERPOLATED_SYMBOL_NODE:
4118 // defined?(:"#{1}")
4119 // ^^^^^^^
4120 case PM_INTERPOLATED_X_STRING_NODE:
4121 // defined?(`#{1}`)
4122 // ^^^^^^
4123 case PM_LAMBDA_NODE:
4124 // defined?(-> {})
4125 // ^^^^^
4126 case PM_MATCH_LAST_LINE_NODE:
4127 // defined?(not //)
4128 // ^^^^^^
4129 case PM_MATCH_PREDICATE_NODE:
4130 // defined?(1 in 1)
4131 // ^^^^^^
4132 case PM_MATCH_REQUIRED_NODE:
4133 // defined?(1 => 1)
4134 // ^^^^^^
4135 case PM_MATCH_WRITE_NODE:
4136 // defined?(/(?<a>)/ =~ "")
4137 // ^^^^^^^^^^^^^^
4138 case PM_MODULE_NODE:
4139 // defined?(module A end)
4140 // ^^^^^^^^^^^^
4141 case PM_NEXT_NODE:
4142 // defined?(next 1)
4143 // ^^^^^^
4144 case PM_OR_NODE:
4145 // defined?(a or b)
4146 // ^^^^^^
4147 case PM_POST_EXECUTION_NODE:
4148 // defined?((END {}))
4149 // ^^^^^^^^
4150 case PM_RANGE_NODE:
4151 // defined?(1..1)
4152 // ^^^^
4153 case PM_RATIONAL_NODE:
4154 // defined?(1r)
4155 // ^^
4156 case PM_REDO_NODE:
4157 // defined?(redo)
4158 // ^^^^
4159 case PM_REGULAR_EXPRESSION_NODE:
4160 // defined?(//)
4161 // ^^
4162 case PM_RESCUE_MODIFIER_NODE:
4163 // defined?(a rescue b)
4164 // ^^^^^^^^^^
4165 case PM_RETRY_NODE:
4166 // defined?(retry)
4167 // ^^^^^
4168 case PM_RETURN_NODE:
4169 // defined?(return)
4170 // ^^^^^^
4171 case PM_SINGLETON_CLASS_NODE:
4172 // defined?(class << self; end)
4173 // ^^^^^^^^^^^^^^^^^^
4174 case PM_SOURCE_ENCODING_NODE:
4175 // defined?(__ENCODING__)
4176 // ^^^^^^^^^^^^
4177 case PM_SOURCE_FILE_NODE:
4178 // defined?(__FILE__)
4179 // ^^^^^^^^
4180 case PM_SOURCE_LINE_NODE:
4181 // defined?(__LINE__)
4182 // ^^^^^^^^
4183 case PM_STRING_NODE:
4184 // defined?("")
4185 // ^^
4186 case PM_SYMBOL_NODE:
4187 // defined?(:a)
4188 // ^^
4189 case PM_UNDEF_NODE:
4190 // defined?((undef a))
4191 // ^^^^^^^
4192 case PM_UNLESS_NODE:
4193 // defined?(unless a then end)
4194 // ^^^^^^^^^^^^^^^^^
4195 case PM_UNTIL_NODE:
4196 // defined?(until a do end)
4197 // ^^^^^^^^^^^^^^
4198 case PM_WHILE_NODE:
4199 // defined?(while a do end)
4200 // ^^^^^^^^^^^^^^
4201 case PM_X_STRING_NODE:
4202 // defined?(``)
4203 // ^^
4204 dtype = DEFINED_EXPR;
4205 break;
4206/* DEFINED_REF ****************************************************************/
4207 case PM_BACK_REFERENCE_READ_NODE: {
4208 // defined?($+)
4209 // ^^
4211 VALUE ref = pm_compile_back_reference_ref(cast);
4212
4213 PUSH_INSN(ret, location, putnil);
4214 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4215
4216 return;
4217 }
4218 case PM_NUMBERED_REFERENCE_READ_NODE: {
4219 // defined?($1)
4220 // ^^
4222 VALUE ref = pm_compile_numbered_reference_ref(cast);
4223
4224 PUSH_INSN(ret, location, putnil);
4225 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4226
4227 return;
4228 }
4229/* DEFINED_CONST_FROM *********************************************************/
4230 case PM_CONSTANT_PATH_NODE: {
4231 // defined?(A::A)
4232 // ^^^^
4233 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
4234 ID name = pm_constant_id_lookup(scope_node, cast->name);
4235
4236 if (cast->parent != NULL) {
4237 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
4238 pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
4239
4240 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4241 PM_COMPILE(cast->parent);
4242 }
4243 else {
4244 PUSH_INSN1(ret, location, putobject, rb_cObject);
4245 }
4246
4247 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4248 return;
4249 }
4250/* Containers *****************************************************************/
4251 case PM_BEGIN_NODE: {
4252 // defined?(begin end)
4253 // ^^^^^^^^^
4254 const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
4255
4256 if (cast->rescue_clause == NULL && cast->ensure_clause == NULL && cast->else_clause == NULL) {
4257 if (cast->statements == NULL) {
4258 // If we have empty statements, then we want to return "nil".
4259 dtype = DEFINED_NIL;
4260 }
4261 else if (cast->statements->body.size == 1) {
4262 // If we have a begin node that is wrapping a single statement
4263 // then we want to recurse down to that statement and compile
4264 // it.
4265 pm_compile_defined_expr0(iseq, cast->statements->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, false);
4266 return;
4267 }
4268 else {
4269 // Otherwise, we have a begin wrapping multiple statements, in
4270 // which case this is defined as "expression".
4271 dtype = DEFINED_EXPR;
4272 }
4273 } else {
4274 // If we have any of the other clauses besides the main begin/end,
4275 // this is defined as "expression".
4276 dtype = DEFINED_EXPR;
4277 }
4278
4279 break;
4280 }
4281 case PM_PARENTHESES_NODE: {
4282 // defined?(())
4283 // ^^
4284 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
4285
4286 if (cast->body == NULL) {
4287 // If we have empty parentheses, then we want to return "nil".
4288 dtype = DEFINED_NIL;
4289 }
4290 else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
4291 // If we have a parentheses node that is wrapping a single statement
4292 // then we want to recurse down to that statement and compile it.
4293 pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, false);
4294 return;
4295 }
4296 else {
4297 // Otherwise, we have parentheses wrapping multiple statements, in
4298 // which case this is defined as "expression".
4299 dtype = DEFINED_EXPR;
4300 }
4301
4302 break;
4303 }
4304 case PM_ARRAY_NODE: {
4305 // defined?([])
4306 // ^^
4307 const pm_array_node_t *cast = (const pm_array_node_t *) node;
4308
4309 if (cast->elements.size > 0 && !lfinish[1]) {
4310 lfinish[1] = NEW_LABEL(location.line);
4311 }
4312
4313 for (size_t index = 0; index < cast->elements.size; index++) {
4314 pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
4315 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4316 }
4317
4318 dtype = DEFINED_EXPR;
4319 break;
4320 }
4321 case PM_HASH_NODE:
4322 // defined?({ a: 1 })
4323 // ^^^^^^^^
4324 case PM_KEYWORD_HASH_NODE: {
4325 // defined?(a(a: 1))
4326 // ^^^^
4327 const pm_node_list_t *elements;
4328
4329 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4330 elements = &((const pm_hash_node_t *) node)->elements;
4331 }
4332 else {
4333 elements = &((const pm_keyword_hash_node_t *) node)->elements;
4334 }
4335
4336 if (elements->size > 0 && !lfinish[1]) {
4337 lfinish[1] = NEW_LABEL(location.line);
4338 }
4339
4340 for (size_t index = 0; index < elements->size; index++) {
4341 pm_compile_defined_expr0(iseq, elements->nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
4342 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4343 }
4344
4345 dtype = DEFINED_EXPR;
4346 break;
4347 }
4348 case PM_ASSOC_NODE: {
4349 // defined?({ a: 1 })
4350 // ^^^^
4351 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
4352
4353 pm_compile_defined_expr0(iseq, cast->key, node_location, ret, popped, scope_node, true, lfinish, false);
4354 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4355 pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
4356
4357 return;
4358 }
4359 case PM_ASSOC_SPLAT_NODE: {
4360 // defined?({ **a })
4361 // ^^^^
4362 const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
4363
4364 if (cast->value == NULL) {
4365 dtype = DEFINED_EXPR;
4366 break;
4367 }
4368
4369 pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
4370 return;
4371 }
4372 case PM_IMPLICIT_NODE: {
4373 // defined?({ a: })
4374 // ^^
4375 const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
4376 pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, in_condition, lfinish, false);
4377 return;
4378 }
4379 case PM_CALL_NODE: {
4380#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4381
4382 // defined?(a(1, 2, 3))
4383 // ^^^^^^^^^^
4384 const pm_call_node_t *cast = ((const pm_call_node_t *) node);
4385
4386 if (BLOCK_P(cast)) {
4387 dtype = DEFINED_EXPR;
4388 break;
4389 }
4390
4391 if (cast->receiver || cast->arguments || (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE))) {
4392 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
4393 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.line);
4394 }
4395
4396 if (cast->arguments) {
4397 pm_compile_defined_expr0(iseq, (const pm_node_t *) cast->arguments, node_location, ret, popped, scope_node, true, lfinish, false);
4398 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4399 }
4400
4401 if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
4402 pm_compile_defined_expr0(iseq, cast->block, node_location, ret, popped, scope_node, true, lfinish, false);
4403 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4404 }
4405
4406 if (cast->receiver) {
4407 if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE) && !BLOCK_P((const pm_call_node_t *) cast->receiver)) {
4408 // Special behavior here where we chain calls together. This is
4409 // the only path that sets explicit_receiver to true.
4410 pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
4411 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4412
4413 const pm_call_node_t *receiver = (const pm_call_node_t *) cast->receiver;
4414 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4415
4416 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4417 }
4418 else {
4419 pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, false);
4420 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4421 PM_COMPILE(cast->receiver);
4422 }
4423
4424 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
4425
4426 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4427 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4428 }
4429 else {
4430 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
4431
4432 PUSH_INSN(ret, location, putself);
4433 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4434
4435 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4436 }
4437
4438 return;
4439
4440#undef BLOCK_P
4441 }
4442 case PM_ARGUMENTS_NODE: {
4443 // defined?(a(1, 2, 3))
4444 // ^^^^^^^
4445 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
4446
4447 for (size_t index = 0; index < cast->arguments.size; index++) {
4448 pm_compile_defined_expr0(iseq, cast->arguments.nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish, false);
4449 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4450 }
4451
4452 dtype = DEFINED_EXPR;
4453 break;
4454 }
4455 case PM_BLOCK_ARGUMENT_NODE:
4456 // defined?(a(&b))
4457 // ^^
4458 dtype = DEFINED_EXPR;
4459 break;
4460 case PM_FORWARDING_ARGUMENTS_NODE:
4461 // def a(...) = defined?(a(...))
4462 // ^^^
4463 dtype = DEFINED_EXPR;
4464 break;
4465 case PM_SPLAT_NODE: {
4466 // def a(*) = defined?(a(*))
4467 // ^
4468 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
4469
4470 if (cast->expression == NULL) {
4471 dtype = DEFINED_EXPR;
4472 break;
4473 }
4474
4475 pm_compile_defined_expr0(iseq, cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish, false);
4476
4477 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
4478 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4479
4480 dtype = DEFINED_EXPR;
4481 break;
4482 }
4483 case PM_SHAREABLE_CONSTANT_NODE:
4484 // # shareable_constant_value: literal
4485 // defined?(A = 1)
4486 // ^^^^^
4487 pm_compile_defined_expr0(iseq, ((const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4488 return;
4489/* Unreachable (parameters) ***************************************************/
4490 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4491 case PM_BLOCK_PARAMETER_NODE:
4492 case PM_BLOCK_PARAMETERS_NODE:
4493 case PM_FORWARDING_PARAMETER_NODE:
4494 case PM_IMPLICIT_REST_NODE:
4495 case PM_IT_PARAMETERS_NODE:
4496 case PM_PARAMETERS_NODE:
4497 case PM_KEYWORD_REST_PARAMETER_NODE:
4498 case PM_NO_KEYWORDS_PARAMETER_NODE:
4499 case PM_NUMBERED_PARAMETERS_NODE:
4500 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4501 case PM_OPTIONAL_PARAMETER_NODE:
4502 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4503 case PM_REQUIRED_PARAMETER_NODE:
4504 case PM_REST_PARAMETER_NODE:
4505/* Unreachable (pattern matching) *********************************************/
4506 case PM_ALTERNATION_PATTERN_NODE:
4507 case PM_ARRAY_PATTERN_NODE:
4508 case PM_CAPTURE_PATTERN_NODE:
4509 case PM_FIND_PATTERN_NODE:
4510 case PM_HASH_PATTERN_NODE:
4511 case PM_PINNED_EXPRESSION_NODE:
4512 case PM_PINNED_VARIABLE_NODE:
4513/* Unreachable (indirect writes) **********************************************/
4514 case PM_CALL_TARGET_NODE:
4515 case PM_CLASS_VARIABLE_TARGET_NODE:
4516 case PM_CONSTANT_PATH_TARGET_NODE:
4517 case PM_CONSTANT_TARGET_NODE:
4518 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4519 case PM_INDEX_TARGET_NODE:
4520 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4521 case PM_LOCAL_VARIABLE_TARGET_NODE:
4522 case PM_MULTI_TARGET_NODE:
4523/* Unreachable (clauses) ******************************************************/
4524 case PM_ELSE_NODE:
4525 case PM_ENSURE_NODE:
4526 case PM_IN_NODE:
4527 case PM_RESCUE_NODE:
4528 case PM_WHEN_NODE:
4529/* Unreachable (miscellaneous) ************************************************/
4530 case PM_BLOCK_NODE:
4531 case PM_EMBEDDED_STATEMENTS_NODE:
4532 case PM_EMBEDDED_VARIABLE_NODE:
4533 case PM_MISSING_NODE:
4534 case PM_PRE_EXECUTION_NODE:
4535 case PM_PROGRAM_NODE:
4536 case PM_SCOPE_NODE:
4537 case PM_STATEMENTS_NODE:
4538 rb_bug("Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4539 }
4540
4541 RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
4542 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4543
4544#undef PUSH_VAL
4545}
4546
4547static void
4548pm_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)
4549{
4550 LINK_ELEMENT *lcur = ret->last;
4551 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
4552
4553 if (lfinish[1]) {
4554 LABEL *lstart = NEW_LABEL(node_location->line);
4555 LABEL *lend = NEW_LABEL(node_location->line);
4556
4558 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4559
4560 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4561 iseq,
4562 ifunc,
4563 rb_str_concat(rb_str_new2("defined guard in "), ISEQ_BODY(iseq)->location.label),
4564 iseq,
4565 ISEQ_TYPE_RESCUE,
4566 0
4567 );
4568
4569 lstart->rescued = LABEL_RESCUE_BEG;
4570 lend->rescued = LABEL_RESCUE_END;
4571
4572 APPEND_LABEL(ret, lcur, lstart);
4573 PUSH_LABEL(ret, lend);
4574 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4575 }
4576}
4577
4578static void
4579pm_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)
4580{
4581 LABEL *lfinish[3];
4582 LINK_ELEMENT *last = ret->last;
4583
4584 lfinish[0] = NEW_LABEL(node_location->line);
4585 lfinish[1] = 0;
4586 lfinish[2] = 0;
4587
4588 if (!popped) {
4589 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
4590 }
4591
4592 if (lfinish[1]) {
4593 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->line, node_location->node_id, BIN(putnil), 0)->link);
4594 PUSH_INSN(ret, *node_location, swap);
4595
4596 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4597 PUSH_INSN(ret, *node_location, pop);
4598 PUSH_LABEL(ret, lfinish[1]);
4599
4600 }
4601
4602 PUSH_LABEL(ret, lfinish[0]);
4603}
4604
4605// This is exactly the same as add_ensure_iseq, except it compiled
4606// the node as a Prism node, and not a CRuby node
4607static void
4608pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_scope_node_t *scope_node)
4609{
4610 RUBY_ASSERT(can_add_ensure_iseq(iseq));
4611
4613 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4614 struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
4615 DECL_ANCHOR(ensure);
4616
4617 while (enlp) {
4618 if (enlp->erange != NULL) {
4619 DECL_ANCHOR(ensure_part);
4620 LABEL *lstart = NEW_LABEL(0);
4621 LABEL *lend = NEW_LABEL(0);
4622
4623 add_ensure_range(iseq, enlp->erange, lstart, lend);
4624
4625 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4626 PUSH_LABEL(ensure_part, lstart);
4627 bool popped = true;
4628 PM_COMPILE_INTO_ANCHOR(ensure_part, (const pm_node_t *) enlp->ensure_node);
4629 PUSH_LABEL(ensure_part, lend);
4630 PUSH_SEQ(ensure, ensure_part);
4631 }
4632 else {
4633 if (!is_return) {
4634 break;
4635 }
4636 }
4637 enlp = enlp->prev;
4638 }
4639 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4640 PUSH_SEQ(ret, ensure);
4641}
4642
4644 pm_scope_node_t *scope_node;
4645 rb_ast_id_table_t *local_table_for_iseq;
4646 int local_index;
4647};
4648
4649static int
4650pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
4651{
4652 if (!existing) {
4653 pm_constant_id_t constant_id = (pm_constant_id_t) *key;
4654 struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *) arg;
4655
4656 pm_scope_node_t *scope_node = ctx->scope_node;
4657 rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
4658 int local_index = ctx->local_index;
4659
4660 ID local = pm_constant_id_lookup(scope_node, constant_id);
4661 local_table_for_iseq->ids[local_index] = local;
4662
4663 *value = (st_data_t)local_index;
4664
4665 ctx->local_index++;
4666 }
4667
4668 return ST_CONTINUE;
4669}
4670
4676static void
4677pm_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)
4678{
4679 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4680
4681 ID local = pm_constant_id_lookup(scope_node, constant_id);
4682 local_table_for_iseq->ids[local_index] = local;
4683 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4684}
4685
4690static void
4691pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
4692{
4693 local_table_for_iseq->ids[local_index] = local_name;
4694 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4695}
4696
4703static int
4704pm_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)
4705{
4706 for (size_t index = 0; index < node->lefts.size; index++) {
4707 const pm_node_t *left = node->lefts.nodes[index];
4708
4709 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4710 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4711 pm_insert_local_index(((const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4712 local_index++;
4713 }
4714 }
4715 else {
4716 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4717 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);
4718 }
4719 }
4720
4721 if (node->rest != NULL && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE)) {
4722 const pm_splat_node_t *rest = (const pm_splat_node_t *) node->rest;
4723
4724 if (rest->expression != NULL) {
4725 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4726
4727 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4728 pm_insert_local_index(((const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4729 local_index++;
4730 }
4731 }
4732 }
4733
4734 for (size_t index = 0; index < node->rights.size; index++) {
4735 const pm_node_t *right = node->rights.nodes[index];
4736
4737 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4738 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4739 pm_insert_local_index(((const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4740 local_index++;
4741 }
4742 }
4743 else {
4744 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4745 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);
4746 }
4747 }
4748
4749 return local_index;
4750}
4751
4756static inline void
4757pm_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)
4758{
4759 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4760 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
4761 PUSH_SETLOCAL(ret, location, index.index, index.level);
4762}
4763
4772static void
4773pm_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)
4774{
4775 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4776 bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((const pm_splat_node_t *) node->rest)->expression) != NULL);
4777 bool has_rights = node->rights.size > 0;
4778
4779 int flag = (has_rest || has_rights) ? 1 : 0;
4780 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->lefts.size), INT2FIX(flag));
4781
4782 for (size_t index = 0; index < node->lefts.size; index++) {
4783 const pm_node_t *left = node->lefts.nodes[index];
4784
4785 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4786 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) left, ret, scope_node);
4787 }
4788 else {
4789 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4790 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) left, ret, scope_node);
4791 }
4792 }
4793
4794 if (has_rest) {
4795 if (has_rights) {
4796 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(3));
4797 }
4798
4799 const pm_node_t *rest = ((const pm_splat_node_t *) node->rest)->expression;
4800 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4801
4802 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) rest, ret, scope_node);
4803 }
4804
4805 if (has_rights) {
4806 if (!has_rest) {
4807 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(2));
4808 }
4809
4810 for (size_t index = 0; index < node->rights.size; index++) {
4811 const pm_node_t *right = node->rights.nodes[index];
4812
4813 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4814 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) right, ret, scope_node);
4815 }
4816 else {
4817 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4818 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) right, ret, scope_node);
4819 }
4820 }
4821 }
4822}
4823
4829 // The pointer to the topn instruction that will need to be modified after
4830 // we know the total stack size of all of the targets.
4831 INSN *topn;
4832
4833 // The index of the stack from the base of the entire multi target at which
4834 // the parent expression is located.
4835 size_t stack_index;
4836
4837 // The number of slots in the stack that this node occupies.
4838 size_t stack_size;
4839
4840 // The position of the node in the list of targets.
4841 size_t position;
4842
4843 // A pointer to the next node in this linked list.
4844 struct pm_multi_target_state_node *next;
4846
4854typedef struct {
4855 // The total number of slots in the stack that this multi target occupies.
4856 size_t stack_size;
4857
4858 // The position of the current node being compiled. This is forwarded to
4859 // nodes when they are allocated.
4860 size_t position;
4861
4862 // A pointer to the head of this linked list.
4864
4865 // A pointer to the tail of this linked list.
4868
4872static void
4873pm_multi_target_state_push(pm_multi_target_state_t *state, INSN *topn, size_t stack_size)
4874{
4876 node->topn = topn;
4877 node->stack_index = state->stack_size + 1;
4878 node->stack_size = stack_size;
4879 node->position = state->position;
4880 node->next = NULL;
4881
4882 if (state->head == NULL) {
4883 state->head = node;
4884 state->tail = node;
4885 }
4886 else {
4887 state->tail->next = node;
4888 state->tail = node;
4889 }
4890
4891 state->stack_size += stack_size;
4892}
4893
4899static void
4900pm_multi_target_state_update(pm_multi_target_state_t *state)
4901{
4902 // If nothing was ever pushed onto the stack, then we don't need to do any
4903 // kind of updates.
4904 if (state->stack_size == 0) return;
4905
4906 pm_multi_target_state_node_t *current = state->head;
4908
4909 while (current != NULL) {
4910 VALUE offset = INT2FIX(state->stack_size - current->stack_index + current->position);
4911 current->topn->operands[0] = offset;
4912
4913 // stack_size will be > 1 in the case that we compiled an index target
4914 // and it had arguments. In this case, we use multiple topn instructions
4915 // to grab up all of the arguments as well, so those offsets need to be
4916 // updated as well.
4917 if (current->stack_size > 1) {
4918 INSN *insn = current->topn;
4919
4920 for (size_t index = 1; index < current->stack_size; index += 1) {
4921 LINK_ELEMENT *element = get_next_insn(insn);
4922 RUBY_ASSERT(IS_INSN(element));
4923
4924 insn = (INSN *) element;
4925 RUBY_ASSERT(insn->insn_id == BIN(topn));
4926
4927 insn->operands[0] = offset;
4928 }
4929 }
4930
4931 previous = current;
4932 current = current->next;
4933
4934 xfree(previous);
4935 }
4936}
4937
4938static void
4939pm_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);
4940
4969static void
4970pm_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)
4971{
4972 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4973
4974 switch (PM_NODE_TYPE(node)) {
4975 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4976 // Local variable targets have no parent expression, so they only need
4977 // to compile the write.
4978 //
4979 // for i in []; end
4980 //
4982 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
4983
4984 PUSH_SETLOCAL(writes, location, index.index, index.level);
4985 break;
4986 }
4987 case PM_CLASS_VARIABLE_TARGET_NODE: {
4988 // Class variable targets have no parent expression, so they only need
4989 // to compile the write.
4990 //
4991 // for @@i in []; end
4992 //
4994 ID name = pm_constant_id_lookup(scope_node, cast->name);
4995
4996 VALUE operand = ID2SYM(name);
4997 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
4998 break;
4999 }
5000 case PM_CONSTANT_TARGET_NODE: {
5001 // Constant targets have no parent expression, so they only need to
5002 // compile the write.
5003 //
5004 // for I in []; end
5005 //
5006 const pm_constant_target_node_t *cast = (const pm_constant_target_node_t *) node;
5007 ID name = pm_constant_id_lookup(scope_node, cast->name);
5008
5009 VALUE operand = ID2SYM(name);
5010 PUSH_INSN1(writes, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5011 PUSH_INSN1(writes, location, setconstant, operand);
5012 break;
5013 }
5014 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
5015 // Global variable targets have no parent expression, so they only need
5016 // to compile the write.
5017 //
5018 // for $i in []; end
5019 //
5021 ID name = pm_constant_id_lookup(scope_node, cast->name);
5022
5023 VALUE operand = ID2SYM(name);
5024 PUSH_INSN1(writes, location, setglobal, operand);
5025 break;
5026 }
5027 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
5028 // Instance variable targets have no parent expression, so they only
5029 // need to compile the write.
5030 //
5031 // for @i in []; end
5032 //
5034 ID name = pm_constant_id_lookup(scope_node, cast->name);
5035
5036 VALUE operand = ID2SYM(name);
5037 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
5038 break;
5039 }
5040 case PM_CONSTANT_PATH_TARGET_NODE: {
5041 // Constant path targets have a parent expression that is the object
5042 // that owns the constant. This needs to be compiled first into the
5043 // parents sequence. If no parent is found, then it represents using the
5044 // unary :: operator to indicate a top-level constant. In that case we
5045 // need to push Object onto the stack.
5046 //
5047 // for I::J in []; end
5048 //
5050 ID name = pm_constant_id_lookup(scope_node, cast->name);
5051
5052 if (cast->parent != NULL) {
5053 pm_compile_node(iseq, cast->parent, parents, false, scope_node);
5054 }
5055 else {
5056 PUSH_INSN1(parents, location, putobject, rb_cObject);
5057 }
5058
5059 if (state == NULL) {
5060 PUSH_INSN(writes, location, swap);
5061 }
5062 else {
5063 PUSH_INSN1(writes, location, topn, INT2FIX(1));
5064 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5065 }
5066
5067 VALUE operand = ID2SYM(name);
5068 PUSH_INSN1(writes, location, setconstant, operand);
5069
5070 if (state != NULL) {
5071 PUSH_INSN(cleanup, location, pop);
5072 }
5073
5074 break;
5075 }
5076 case PM_CALL_TARGET_NODE: {
5077 // Call targets have a parent expression that is the receiver of the
5078 // method being called. This needs to be compiled first into the parents
5079 // sequence. These nodes cannot have arguments, so the method call is
5080 // compiled with a single argument which represents the value being
5081 // written.
5082 //
5083 // for i.j in []; end
5084 //
5085 const pm_call_target_node_t *cast = (const pm_call_target_node_t *) node;
5086 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
5087
5088 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
5089
5090 LABEL *safe_label = NULL;
5091 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5092 safe_label = NEW_LABEL(location.line);
5093 PUSH_INSN(parents, location, dup);
5094 PUSH_INSNL(parents, location, branchnil, safe_label);
5095 }
5096
5097 if (state != NULL) {
5098 PUSH_INSN1(writes, location, topn, INT2FIX(1));
5099 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5100 PUSH_INSN(writes, location, swap);
5101 }
5102
5103 int flags = VM_CALL_ARGS_SIMPLE;
5104 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5105
5106 PUSH_SEND_WITH_FLAG(writes, location, method_id, INT2FIX(1), INT2FIX(flags));
5107 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5108 PUSH_INSN(writes, location, pop);
5109 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5110
5111 if (state != NULL) {
5112 PUSH_INSN(cleanup, location, pop);
5113 }
5114
5115 break;
5116 }
5117 case PM_INDEX_TARGET_NODE: {
5118 // Index targets have a parent expression that is the receiver of the
5119 // method being called and any additional arguments that are being
5120 // passed along with the value being written. The receiver and arguments
5121 // both need to be on the stack. Note that this is even more complicated
5122 // by the fact that these nodes can hold a block using the unary &
5123 // operator.
5124 //
5125 // for i[:j] in []; end
5126 //
5127 const pm_index_target_node_t *cast = (const pm_index_target_node_t *) node;
5128
5129 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
5130
5131 int flags = 0;
5132 struct rb_callinfo_kwarg *kwargs = NULL;
5133 int argc = pm_setup_args(cast->arguments, (const pm_node_t *) cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
5134
5135 if (state != NULL) {
5136 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
5137 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
5138
5139 if (argc == 0) {
5140 PUSH_INSN(writes, location, swap);
5141 }
5142 else {
5143 for (int index = 0; index < argc; index++) {
5144 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
5145 }
5146 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
5147 }
5148 }
5149
5150 // The argc that we're going to pass to the send instruction is the
5151 // number of arguments + 1 for the value being written. If there's a
5152 // splat, then we need to insert newarray and concatarray instructions
5153 // after the arguments have been written.
5154 int ci_argc = argc + 1;
5155 if (flags & VM_CALL_ARGS_SPLAT) {
5156 ci_argc--;
5157 PUSH_INSN1(writes, location, newarray, INT2FIX(1));
5158 PUSH_INSN(writes, location, concatarray);
5159 }
5160
5161 PUSH_SEND_R(writes, location, idASET, INT2NUM(ci_argc), NULL, INT2FIX(flags), kwargs);
5162 PUSH_INSN(writes, location, pop);
5163
5164 if (state != NULL) {
5165 if (argc != 0) {
5166 PUSH_INSN(writes, location, pop);
5167 }
5168
5169 for (int index = 0; index < argc + 1; index++) {
5170 PUSH_INSN(cleanup, location, pop);
5171 }
5172 }
5173
5174 break;
5175 }
5176 case PM_MULTI_TARGET_NODE: {
5177 // Multi target nodes represent a set of writes to multiple variables.
5178 // The parent expressions are the combined set of the parent expressions
5179 // of its inner target nodes.
5180 //
5181 // for i, j in []; end
5182 //
5183 size_t before_position;
5184 if (state != NULL) {
5185 before_position = state->position;
5186 state->position--;
5187 }
5188
5189 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5190 if (state != NULL) state->position = before_position;
5191
5192 break;
5193 }
5194 case PM_SPLAT_NODE: {
5195 // Splat nodes capture all values into an array. They can be used
5196 // as targets in assignments or for loops.
5197 //
5198 // for *x in []; end
5199 //
5200 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
5201
5202 if (cast->expression != NULL) {
5203 pm_compile_target_node(iseq, cast->expression, parents, writes, cleanup, scope_node, state);
5204 }
5205
5206 break;
5207 }
5208 default:
5209 rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5210 break;
5211 }
5212}
5213
5219static void
5220pm_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)
5221{
5222 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5223 const pm_node_list_t *lefts;
5224 const pm_node_t *rest;
5225 const pm_node_list_t *rights;
5226
5227 switch (PM_NODE_TYPE(node)) {
5228 case PM_MULTI_TARGET_NODE: {
5229 const pm_multi_target_node_t *cast = (const pm_multi_target_node_t *) node;
5230 lefts = &cast->lefts;
5231 rest = cast->rest;
5232 rights = &cast->rights;
5233 break;
5234 }
5235 case PM_MULTI_WRITE_NODE: {
5236 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
5237 lefts = &cast->lefts;
5238 rest = cast->rest;
5239 rights = &cast->rights;
5240 break;
5241 }
5242 default:
5243 rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5244 break;
5245 }
5246
5247 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) rest)->expression != NULL;
5248 bool has_posts = rights->size > 0;
5249
5250 // The first instruction in the writes sequence is going to spread the
5251 // top value of the stack onto the number of values that we're going to
5252 // write.
5253 PUSH_INSN2(writes, location, expandarray, INT2FIX(lefts->size), INT2FIX((has_rest || has_posts) ? 1 : 0));
5254
5255 // We need to keep track of some additional state information as we're
5256 // going through the targets because we will need to revisit them once
5257 // we know how many values are being pushed onto the stack.
5258 pm_multi_target_state_t target_state = { 0 };
5259 if (state == NULL) state = &target_state;
5260
5261 size_t base_position = state->position;
5262 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5263
5264 // Next, we'll iterate through all of the leading targets.
5265 for (size_t index = 0; index < lefts->size; index++) {
5266 const pm_node_t *target = lefts->nodes[index];
5267 state->position = lefts->size - index + splat_position + base_position;
5268 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5269 }
5270
5271 // Next, we'll compile the rest target if there is one.
5272 if (has_rest) {
5273 const pm_node_t *target = ((const pm_splat_node_t *) rest)->expression;
5274 state->position = 1 + rights->size + base_position;
5275
5276 if (has_posts) {
5277 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(3));
5278 }
5279
5280 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5281 }
5282
5283 // Finally, we'll compile the trailing targets.
5284 if (has_posts) {
5285 if (!has_rest && rest != NULL) {
5286 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(2));
5287 }
5288
5289 for (size_t index = 0; index < rights->size; index++) {
5290 const pm_node_t *target = rights->nodes[index];
5291 state->position = rights->size - index + base_position;
5292 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5293 }
5294 }
5295}
5296
5302static void
5303pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
5304{
5305 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5306
5307 switch (PM_NODE_TYPE(node)) {
5308 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5309 // For local variables, all we have to do is retrieve the value and then
5310 // compile the index node.
5311 PUSH_GETLOCAL(ret, location, 1, 0);
5312 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5313 break;
5314 }
5315 case PM_CLASS_VARIABLE_TARGET_NODE:
5316 case PM_CONSTANT_TARGET_NODE:
5317 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5318 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5319 case PM_CONSTANT_PATH_TARGET_NODE:
5320 case PM_CALL_TARGET_NODE:
5321 case PM_INDEX_TARGET_NODE:
5322 case PM_SPLAT_NODE: {
5323 // For other targets, we need to potentially compile the parent or
5324 // owning expression of this target, then retrieve the value, expand it,
5325 // and then compile the necessary writes.
5326 DECL_ANCHOR(writes);
5327 DECL_ANCHOR(cleanup);
5328
5329 pm_multi_target_state_t state = { 0 };
5330 state.position = 1;
5331 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5332
5333 PUSH_GETLOCAL(ret, location, 1, 0);
5334 PUSH_INSN2(ret, location, expandarray, INT2FIX(1), INT2FIX(0));
5335
5336 PUSH_SEQ(ret, writes);
5337 PUSH_SEQ(ret, cleanup);
5338
5339 pm_multi_target_state_update(&state);
5340 break;
5341 }
5342 case PM_MULTI_TARGET_NODE: {
5343 DECL_ANCHOR(writes);
5344 DECL_ANCHOR(cleanup);
5345
5346 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5347
5348 LABEL *not_single = NEW_LABEL(location.line);
5349 LABEL *not_ary = NEW_LABEL(location.line);
5350
5351 // When there are multiple targets, we'll do a bunch of work to convert
5352 // the value into an array before we expand it. Effectively we're trying
5353 // to accomplish:
5354 //
5355 // (args.length == 1 && Array.try_convert(args[0])) || args
5356 //
5357 PUSH_GETLOCAL(ret, location, 1, 0);
5358 PUSH_INSN(ret, location, dup);
5359 PUSH_CALL(ret, location, idLength, INT2FIX(0));
5360 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
5361 PUSH_CALL(ret, location, idEq, INT2FIX(1));
5362 PUSH_INSNL(ret, location, branchunless, not_single);
5363 PUSH_INSN(ret, location, dup);
5364 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
5365 PUSH_CALL(ret, location, idAREF, INT2FIX(1));
5366 PUSH_INSN1(ret, location, putobject, rb_cArray);
5367 PUSH_INSN(ret, location, swap);
5368 PUSH_CALL(ret, location, rb_intern("try_convert"), INT2FIX(1));
5369 PUSH_INSN(ret, location, dup);
5370 PUSH_INSNL(ret, location, branchunless, not_ary);
5371 PUSH_INSN(ret, location, swap);
5372
5373 PUSH_LABEL(ret, not_ary);
5374 PUSH_INSN(ret, location, pop);
5375
5376 PUSH_LABEL(ret, not_single);
5377 PUSH_SEQ(ret, writes);
5378 PUSH_SEQ(ret, cleanup);
5379 break;
5380 }
5381 default:
5382 rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5383 break;
5384 }
5385}
5386
5387static void
5388pm_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)
5389{
5390 const pm_parser_t *parser = scope_node->parser;
5391
5392 LABEL *lstart = NEW_LABEL(node_location->line);
5393 LABEL *lend = NEW_LABEL(node_location->line);
5394 LABEL *lcont = NEW_LABEL(node_location->line);
5395
5396 pm_scope_node_t rescue_scope_node;
5397 pm_scope_node_init((const pm_node_t *) cast->rescue_clause, &rescue_scope_node, scope_node);
5398
5399 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5400 &rescue_scope_node,
5401 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
5402 ISEQ_TYPE_RESCUE,
5403 pm_node_line_number(parser, (const pm_node_t *) cast->rescue_clause)
5404 );
5405
5406 pm_scope_node_destroy(&rescue_scope_node);
5407
5408 lstart->rescued = LABEL_RESCUE_BEG;
5409 lend->rescued = LABEL_RESCUE_END;
5410 PUSH_LABEL(ret, lstart);
5411
5412 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5413 ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
5414
5415 if (cast->statements != NULL) {
5416 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->statements);
5417 }
5418 else {
5419 const pm_node_location_t location = PM_NODE_START_LOCATION(parser, cast->rescue_clause);
5420 PUSH_INSN(ret, location, putnil);
5421 }
5422
5423 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5424 PUSH_LABEL(ret, lend);
5425
5426 if (cast->else_clause != NULL) {
5427 if (!popped) PUSH_INSN(ret, *node_location, pop);
5428 PM_COMPILE((const pm_node_t *) cast->else_clause);
5429 }
5430
5431 PUSH_INSN(ret, *node_location, nop);
5432 PUSH_LABEL(ret, lcont);
5433
5434 if (popped) PUSH_INSN(ret, *node_location, pop);
5435 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5436 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5437}
5438
5439static void
5440pm_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)
5441{
5442 const pm_parser_t *parser = scope_node->parser;
5443 const pm_statements_node_t *statements = cast->ensure_clause->statements;
5444
5445 pm_node_location_t location;
5446 if (statements != NULL) {
5447 location = PM_NODE_START_LOCATION(parser, statements);
5448 }
5449 else {
5450 location = *node_location;
5451 }
5452
5453 LABEL *lstart = NEW_LABEL(location.line);
5454 LABEL *lend = NEW_LABEL(location.line);
5455 LABEL *lcont = NEW_LABEL(location.line);
5456
5457 struct ensure_range er;
5459 struct ensure_range *erange;
5460
5461 DECL_ANCHOR(ensr);
5462 if (statements != NULL) {
5463 pm_compile_node(iseq, (const pm_node_t *) statements, ensr, true, scope_node);
5464 }
5465
5466 LINK_ELEMENT *last = ensr->last;
5467 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5468
5469 er.begin = lstart;
5470 er.end = lend;
5471 er.next = 0;
5472 push_ensure_entry(iseq, &enl, &er, (void *) cast->ensure_clause);
5473
5474 PUSH_LABEL(ret, lstart);
5475 if (cast->rescue_clause != NULL) {
5476 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5477 }
5478 else if (cast->statements != NULL) {
5479 pm_compile_node(iseq, (const pm_node_t *) cast->statements, ret, popped | last_leave, scope_node);
5480 }
5481 else if (!(popped | last_leave)) {
5482 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5483 }
5484
5485 PUSH_LABEL(ret, lend);
5486 PUSH_SEQ(ret, ensr);
5487 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5488 PUSH_LABEL(ret, lcont);
5489 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5490
5491 pm_scope_node_t next_scope_node;
5492 pm_scope_node_init((const pm_node_t *) cast->ensure_clause, &next_scope_node, scope_node);
5493
5494 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
5495 &next_scope_node,
5496 rb_str_concat(rb_str_new2("ensure in "), ISEQ_BODY(iseq)->location.label),
5497 ISEQ_TYPE_ENSURE,
5498 location.line
5499 );
5500
5501 pm_scope_node_destroy(&next_scope_node);
5502
5503 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5504 if (lstart->link.next != &lend->link) {
5505 while (erange) {
5506 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5507 erange = erange->next;
5508 }
5509 }
5510 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5511}
5512
5517static inline bool
5518pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
5519{
5520 return (
5521 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5522 node->receiver != NULL &&
5523 PM_NODE_TYPE_P(node->receiver, PM_STRING_NODE) &&
5524 node->arguments == NULL &&
5525 node->block == NULL &&
5526 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5527 );
5528}
5529
5534static void
5535pm_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)
5536{
5537 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5538
5539 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5540 ISEQ_BODY(iseq)->ic_size++;
5541 VALUE segments = rb_ary_new_from_args(1, name);
5542 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5543 }
5544 else {
5545 PUSH_INSN(ret, location, putnil);
5546 PUSH_INSN1(ret, location, putobject, Qtrue);
5547 PUSH_INSN1(ret, location, getconstant, name);
5548 }
5549}
5550
5555static VALUE
5556pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
5557{
5558 VALUE parts = rb_ary_new();
5559
5560 while (true) {
5561 switch (PM_NODE_TYPE(node)) {
5562 case PM_CONSTANT_READ_NODE: {
5563 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
5564 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5565
5566 rb_ary_unshift(parts, name);
5567 return parts;
5568 }
5569 case PM_CONSTANT_PATH_NODE: {
5570 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
5571 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5572
5573 rb_ary_unshift(parts, name);
5574 if (cast->parent == NULL) {
5575 rb_ary_unshift(parts, ID2SYM(idNULL));
5576 return parts;
5577 }
5578
5579 node = cast->parent;
5580 break;
5581 }
5582 default:
5583 return Qnil;
5584 }
5585 }
5586}
5587
5593static void
5594pm_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)
5595{
5596 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5597
5598 switch (PM_NODE_TYPE(node)) {
5599 case PM_CONSTANT_READ_NODE: {
5600 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
5601 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5602
5603 PUSH_INSN1(body, location, putobject, Qtrue);
5604 PUSH_INSN1(body, location, getconstant, name);
5605 break;
5606 }
5607 case PM_CONSTANT_PATH_NODE: {
5608 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
5609 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5610
5611 if (cast->parent == NULL) {
5612 PUSH_INSN(body, location, pop);
5613 PUSH_INSN1(body, location, putobject, rb_cObject);
5614 PUSH_INSN1(body, location, putobject, Qtrue);
5615 PUSH_INSN1(body, location, getconstant, name);
5616 }
5617 else {
5618 pm_compile_constant_path(iseq, cast->parent, prefix, body, false, scope_node);
5619 PUSH_INSN1(body, location, putobject, Qfalse);
5620 PUSH_INSN1(body, location, getconstant, name);
5621 }
5622 break;
5623 }
5624 default:
5625 PM_COMPILE_INTO_ANCHOR(prefix, node);
5626 break;
5627 }
5628}
5629
5633static VALUE
5634pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
5635{
5636 switch (PM_NODE_TYPE(node)) {
5637 case PM_TRUE_NODE:
5638 case PM_FALSE_NODE:
5639 case PM_NIL_NODE:
5640 case PM_SYMBOL_NODE:
5641 case PM_REGULAR_EXPRESSION_NODE:
5642 case PM_SOURCE_LINE_NODE:
5643 case PM_INTEGER_NODE:
5644 case PM_FLOAT_NODE:
5645 case PM_RATIONAL_NODE:
5646 case PM_IMAGINARY_NODE:
5647 case PM_SOURCE_ENCODING_NODE:
5648 return pm_static_literal_value(iseq, node, scope_node);
5649 case PM_STRING_NODE:
5650 return parse_static_literal_string(iseq, scope_node, node, &((const pm_string_node_t *) node)->unescaped);
5651 case PM_SOURCE_FILE_NODE:
5652 return pm_source_file_value((const pm_source_file_node_t *) node, scope_node);
5653 case PM_ARRAY_NODE: {
5654 const pm_array_node_t *cast = (const pm_array_node_t *) node;
5655 VALUE result = rb_ary_new_capa(cast->elements.size);
5656
5657 for (size_t index = 0; index < cast->elements.size; index++) {
5658 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->elements.nodes[index], scope_node);
5659 if (element == Qundef) return Qundef;
5660
5661 rb_ary_push(result, element);
5662 }
5663
5664 return rb_ractor_make_shareable(result);
5665 }
5666 case PM_HASH_NODE: {
5667 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
5668 VALUE result = rb_hash_new_capa(cast->elements.size);
5669
5670 for (size_t index = 0; index < cast->elements.size; index++) {
5671 const pm_node_t *element = cast->elements.nodes[index];
5672 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE)) return Qundef;
5673
5674 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
5675
5676 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->key, scope_node);
5677 if (key == Qundef) return Qundef;
5678
5679 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->value, scope_node);
5680 if (value == Qundef) return Qundef;
5681
5682 rb_hash_aset(result, key, value);
5683 }
5684
5685 return rb_ractor_make_shareable(result);
5686 }
5687 default:
5688 return Qundef;
5689 }
5690}
5691
5696static void
5697pm_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)
5698{
5699 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5700 if (literal != Qundef) {
5701 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5702 PUSH_INSN1(ret, location, putobject, literal);
5703 return;
5704 }
5705
5706 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5707 switch (PM_NODE_TYPE(node)) {
5708 case PM_ARRAY_NODE: {
5709 const pm_array_node_t *cast = (const pm_array_node_t *) node;
5710
5711 if (top) {
5712 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5713 }
5714
5715 for (size_t index = 0; index < cast->elements.size; index++) {
5716 pm_compile_shareable_constant_value(iseq, cast->elements.nodes[index], shareability, path, ret, scope_node, false);
5717 }
5718
5719 PUSH_INSN1(ret, location, newarray, INT2FIX(cast->elements.size));
5720
5721 if (top) {
5722 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
5723 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5724 }
5725
5726 return;
5727 }
5728 case PM_HASH_NODE: {
5729 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
5730
5731 if (top) {
5732 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5733 }
5734
5735 pm_compile_hash_elements(iseq, (const pm_node_t *) cast, &cast->elements, shareability, path, false, ret, scope_node);
5736
5737 if (top) {
5738 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
5739 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5740 }
5741
5742 return;
5743 }
5744 default: {
5745 DECL_ANCHOR(value_seq);
5746
5747 pm_compile_node(iseq, node, value_seq, false, scope_node);
5748 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5749 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus, INT2FIX(0), INT2FIX(VM_CALL_ARGS_SIMPLE));
5750 }
5751
5752 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5753 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5754 PUSH_SEQ(ret, value_seq);
5755 PUSH_INSN1(ret, location, putobject, path);
5756 PUSH_SEND_WITH_FLAG(ret, location, rb_intern("ensure_shareable"), INT2FIX(2), INT2FIX(VM_CALL_ARGS_SIMPLE));
5757 }
5758 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5759 if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5760 PUSH_SEQ(ret, value_seq);
5761 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable_copy"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5762 }
5763 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5764 if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5765 PUSH_SEQ(ret, value_seq);
5766 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5767 }
5768
5769 break;
5770 }
5771 }
5772}
5773
5778static void
5779pm_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)
5780{
5781 const pm_node_location_t location = *node_location;
5782 ID name_id = pm_constant_id_lookup(scope_node, node->name);
5783
5784 if (shareability != 0) {
5785 pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_id2str(name_id), ret, scope_node, true);
5786 }
5787 else {
5788 PM_COMPILE_NOT_POPPED(node->value);
5789 }
5790
5791 if (!popped) PUSH_INSN(ret, location, dup);
5792 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5793
5794 VALUE operand = ID2SYM(name_id);
5795 PUSH_INSN1(ret, location, setconstant, operand);
5796}
5797
5802static void
5803pm_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)
5804{
5805 const pm_node_location_t location = *node_location;
5806
5807 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5808 LABEL *end_label = NEW_LABEL(location.line);
5809
5810 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5811 if (!popped) PUSH_INSN(ret, location, dup);
5812
5813 PUSH_INSNL(ret, location, branchunless, end_label);
5814 if (!popped) PUSH_INSN(ret, location, pop);
5815
5816 if (shareability != 0) {
5817 pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
5818 }
5819 else {
5820 PM_COMPILE_NOT_POPPED(node->value);
5821 }
5822
5823 if (!popped) PUSH_INSN(ret, location, dup);
5824 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5825 PUSH_INSN1(ret, location, setconstant, name);
5826 PUSH_LABEL(ret, end_label);
5827}
5828
5833static void
5834pm_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)
5835{
5836 const pm_node_location_t location = *node_location;
5837 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5838
5839 LABEL *set_label = NEW_LABEL(location.line);
5840 LABEL *end_label = NEW_LABEL(location.line);
5841
5842 PUSH_INSN(ret, location, putnil);
5843 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, Qtrue);
5844 PUSH_INSNL(ret, location, branchunless, set_label);
5845
5846 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5847 if (!popped) PUSH_INSN(ret, location, dup);
5848
5849 PUSH_INSNL(ret, location, branchif, end_label);
5850 if (!popped) PUSH_INSN(ret, location, pop);
5851 PUSH_LABEL(ret, set_label);
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 if (!popped) PUSH_INSN(ret, location, dup);
5861 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5862 PUSH_INSN1(ret, location, setconstant, name);
5863 PUSH_LABEL(ret, end_label);
5864}
5865
5870static void
5871pm_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)
5872{
5873 const pm_node_location_t location = *node_location;
5874
5875 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
5876 ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
5877
5878 pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
5879
5880 if (shareability != 0) {
5881 pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
5882 }
5883 else {
5884 PM_COMPILE_NOT_POPPED(node->value);
5885 }
5886
5887 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5888 if (!popped) PUSH_INSN(ret, location, dup);
5889
5890 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5891 PUSH_INSN1(ret, location, setconstant, name);
5892}
5893
5898static VALUE
5899pm_constant_path_path(const pm_constant_path_node_t *node, const pm_scope_node_t *scope_node)
5900{
5901 VALUE parts = rb_ary_new();
5902 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->name)));
5903
5904 const pm_node_t *current = node->parent;
5905 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5906 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) current;
5907 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->name)));
5908 current = cast->parent;
5909 }
5910
5911 if (current == NULL) {
5912 rb_ary_unshift(parts, rb_id2str(idNULL));
5913 }
5914 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5915 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) current)->name)));
5916 }
5917 else {
5918 rb_ary_unshift(parts, rb_str_new_cstr("..."));
5919 }
5920
5921 return rb_ary_join(parts, rb_str_new_cstr("::"));
5922}
5923
5928static void
5929pm_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)
5930{
5931 const pm_node_location_t location = *node_location;
5932 const pm_constant_path_node_t *target = node->target;
5933 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5934
5935 if (target->parent) {
5936 PM_COMPILE_NOT_POPPED((const pm_node_t *) target->parent);
5937 }
5938 else {
5939 PUSH_INSN1(ret, location, putobject, rb_cObject);
5940 }
5941
5942 if (shareability != 0) {
5943 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5944 }
5945 else {
5946 PM_COMPILE_NOT_POPPED(node->value);
5947 }
5948
5949 if (!popped) {
5950 PUSH_INSN(ret, location, swap);
5951 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5952 }
5953
5954 PUSH_INSN(ret, location, swap);
5955 PUSH_INSN1(ret, location, setconstant, name);
5956}
5957
5962static void
5963pm_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)
5964{
5965 const pm_node_location_t location = *node_location;
5966 const pm_constant_path_node_t *target = node->target;
5967
5968 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5969 LABEL *lfin = NEW_LABEL(location.line);
5970
5971 if (target->parent) {
5972 PM_COMPILE_NOT_POPPED(target->parent);
5973 }
5974 else {
5975 PUSH_INSN1(ret, location, putobject, rb_cObject);
5976 }
5977
5978 PUSH_INSN(ret, location, dup);
5979 PUSH_INSN1(ret, location, putobject, Qtrue);
5980 PUSH_INSN1(ret, location, getconstant, name);
5981
5982 if (!popped) PUSH_INSN(ret, location, dup);
5983 PUSH_INSNL(ret, location, branchunless, lfin);
5984
5985 if (!popped) PUSH_INSN(ret, location, pop);
5986
5987 if (shareability != 0) {
5988 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
5989 }
5990 else {
5991 PM_COMPILE_NOT_POPPED(node->value);
5992 }
5993
5994 if (popped) {
5995 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5996 }
5997 else {
5998 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
5999 PUSH_INSN(ret, location, swap);
6000 }
6001
6002 PUSH_INSN1(ret, location, setconstant, name);
6003 PUSH_LABEL(ret, lfin);
6004
6005 if (!popped) PUSH_INSN(ret, location, swap);
6006 PUSH_INSN(ret, location, pop);
6007}
6008
6013static void
6014pm_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)
6015{
6016 const pm_node_location_t location = *node_location;
6017 const pm_constant_path_node_t *target = node->target;
6018
6019 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
6020 LABEL *lassign = NEW_LABEL(location.line);
6021 LABEL *lfin = NEW_LABEL(location.line);
6022
6023 if (target->parent) {
6024 PM_COMPILE_NOT_POPPED(target->parent);
6025 }
6026 else {
6027 PUSH_INSN1(ret, location, putobject, rb_cObject);
6028 }
6029
6030 PUSH_INSN(ret, location, dup);
6031 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, Qtrue);
6032 PUSH_INSNL(ret, location, branchunless, lassign);
6033
6034 PUSH_INSN(ret, location, dup);
6035 PUSH_INSN1(ret, location, putobject, Qtrue);
6036 PUSH_INSN1(ret, location, getconstant, name);
6037
6038 if (!popped) PUSH_INSN(ret, location, dup);
6039 PUSH_INSNL(ret, location, branchif, lfin);
6040
6041 if (!popped) PUSH_INSN(ret, location, pop);
6042 PUSH_LABEL(ret, lassign);
6043
6044 if (shareability != 0) {
6045 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
6046 }
6047 else {
6048 PM_COMPILE_NOT_POPPED(node->value);
6049 }
6050
6051 if (popped) {
6052 PUSH_INSN1(ret, location, topn, INT2FIX(1));
6053 }
6054 else {
6055 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
6056 PUSH_INSN(ret, location, swap);
6057 }
6058
6059 PUSH_INSN1(ret, location, setconstant, name);
6060 PUSH_LABEL(ret, lfin);
6061
6062 if (!popped) PUSH_INSN(ret, location, swap);
6063 PUSH_INSN(ret, location, pop);
6064}
6065
6070static void
6071pm_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)
6072{
6073 const pm_node_location_t location = *node_location;
6074 const pm_constant_path_node_t *target = node->target;
6075
6076 ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
6077 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
6078
6079 if (target->parent) {
6080 PM_COMPILE_NOT_POPPED(target->parent);
6081 }
6082 else {
6083 PUSH_INSN1(ret, location, putobject, rb_cObject);
6084 }
6085
6086 PUSH_INSN(ret, location, dup);
6087 PUSH_INSN1(ret, location, putobject, Qtrue);
6088 PUSH_INSN1(ret, location, getconstant, name);
6089
6090 if (shareability != 0) {
6091 pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
6092 }
6093 else {
6094 PM_COMPILE_NOT_POPPED(node->value);
6095 }
6096
6097 PUSH_CALL(ret, location, method_id, INT2FIX(1));
6098 PUSH_INSN(ret, location, swap);
6099
6100 if (!popped) {
6101 PUSH_INSN1(ret, location, topn, INT2FIX(1));
6102 PUSH_INSN(ret, location, swap);
6103 }
6104
6105 PUSH_INSN1(ret, location, setconstant, name);
6106}
6107
6114#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))
6115
6120static inline void
6121pm_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)
6122{
6123 const pm_node_location_t location = *node_location;
6124 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
6125
6126 pm_constant_id_list_t *locals = &scope_node->locals;
6127 pm_parameters_node_t *parameters_node = NULL;
6128 pm_node_list_t *keywords_list = NULL;
6129 pm_node_list_t *optionals_list = NULL;
6130 pm_node_list_t *posts_list = NULL;
6131 pm_node_list_t *requireds_list = NULL;
6132 pm_node_list_t *block_locals = NULL;
6133 bool trailing_comma = false;
6134
6135 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6136 PUSH_TRACE(ret, RUBY_EVENT_CLASS);
6137 }
6138
6139 if (scope_node->parameters != NULL) {
6140 switch (PM_NODE_TYPE(scope_node->parameters)) {
6141 case PM_BLOCK_PARAMETERS_NODE: {
6142 pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
6143 parameters_node = cast->parameters;
6144 block_locals = &cast->locals;
6145
6146 if (parameters_node) {
6147 if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
6148 trailing_comma = true;
6149 }
6150 }
6151 break;
6152 }
6153 case PM_PARAMETERS_NODE: {
6154 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
6155 break;
6156 }
6157 case PM_NUMBERED_PARAMETERS_NODE: {
6158 uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6159 body->param.lead_num = maximum;
6160 body->param.flags.ambiguous_param0 = maximum == 1;
6161 break;
6162 }
6163 case PM_IT_PARAMETERS_NODE:
6164 body->param.lead_num = 1;
6165 body->param.flags.ambiguous_param0 = true;
6166 break;
6167 default:
6168 rb_bug("Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
6169 }
6170 }
6171
6172 struct rb_iseq_param_keyword *keyword = NULL;
6173
6174 if (parameters_node) {
6175 optionals_list = &parameters_node->optionals;
6176 requireds_list = &parameters_node->requireds;
6177 keywords_list = &parameters_node->keywords;
6178 posts_list = &parameters_node->posts;
6179 }
6180 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))) {
6181 body->param.opt_num = 0;
6182 }
6183 else {
6184 body->param.lead_num = 0;
6185 body->param.opt_num = 0;
6186 }
6187
6188 //********STEP 1**********
6189 // Goal: calculate the table size for the locals, accounting for
6190 // hidden variables and multi target nodes
6191 size_t locals_size = locals->size;
6192
6193 // Index lookup table buffer size is only the number of the locals
6194 st_table *index_lookup_table = st_init_numtable();
6195
6196 int table_size = (int) locals_size;
6197
6198 // For nodes have a hidden iteration variable. We add that to the local
6199 // table size here.
6200 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6201
6202 if (keywords_list && keywords_list->size) {
6203 table_size++;
6204 }
6205
6206 if (requireds_list) {
6207 for (size_t i = 0; i < requireds_list->size; i++) {
6208 // For each MultiTargetNode, we're going to have one
6209 // additional anonymous local not represented in the locals table
6210 // We want to account for this in our table size
6211 pm_node_t *required = requireds_list->nodes[i];
6212 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6213 table_size++;
6214 }
6215 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6216 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6217 table_size++;
6218 }
6219 }
6220 }
6221 }
6222
6223 // If we have the `it` implicit local variable, we need to account for
6224 // it in the local table size.
6225 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6226 table_size++;
6227 }
6228
6229 // Ensure there is enough room in the local table for any
6230 // parameters that have been repeated
6231 // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
6232 // ^^^^^^^^^^^^
6233 if (optionals_list && optionals_list->size) {
6234 for (size_t i = 0; i < optionals_list->size; i++) {
6235 pm_node_t * node = optionals_list->nodes[i];
6236 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6237 table_size++;
6238 }
6239 }
6240 }
6241
6242 // If we have an anonymous "rest" node, we'll need to increase the local
6243 // table size to take it in to account.
6244 // def m(foo, *, bar)
6245 // ^
6246 if (parameters_node) {
6247 if (parameters_node->rest) {
6248 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
6249 if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6250 table_size++;
6251 }
6252 }
6253 }
6254
6255 // def foo(_, **_); _; end
6256 // ^^^
6257 if (parameters_node->keyword_rest) {
6258 // def foo(...); end
6259 // ^^^
6260 // When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
6261 // we need to leave space for 4 locals: *, **, &, ...
6262 if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6263 // Only optimize specifically methods like this: `foo(...)`
6264 if (requireds_list->size == 0 && optionals_list->size == 0 && keywords_list->size == 0) {
6265 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6266 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6267 table_size += 1;
6268 }
6269 else {
6270 table_size += 4;
6271 }
6272 }
6273 else {
6274 const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
6275
6276 // If it's anonymous or repeated, then we need to allocate stack space
6277 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6278 table_size++;
6279 }
6280 }
6281 }
6282 }
6283
6284 if (posts_list) {
6285 for (size_t i = 0; i < posts_list->size; i++) {
6286 // For each MultiTargetNode, we're going to have one
6287 // additional anonymous local not represented in the locals table
6288 // We want to account for this in our table size
6289 pm_node_t *required = posts_list->nodes[i];
6290 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6291 table_size++;
6292 }
6293 }
6294 }
6295
6296 if (keywords_list && keywords_list->size) {
6297 for (size_t i = 0; i < keywords_list->size; i++) {
6298 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6299 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6300 table_size++;
6301 }
6302 }
6303 }
6304
6305 if (parameters_node && parameters_node->block) {
6306 const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
6307
6308 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6309 table_size++;
6310 }
6311 }
6312
6313 // We can create local_table_for_iseq with the correct size
6314 VALUE idtmp = 0;
6315 rb_ast_id_table_t *local_table_for_iseq = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
6316 local_table_for_iseq->size = table_size;
6317
6318 //********END OF STEP 1**********
6319
6320 //********STEP 2**********
6321 // Goal: populate iv index table as well as local table, keeping the
6322 // layout of the local table consistent with the layout of the
6323 // stack when calling the method
6324 //
6325 // Do a first pass on all of the parameters, setting their values in
6326 // the local_table_for_iseq, _except_ for Multis who get a hidden
6327 // variable in this step, and will get their names inserted in step 3
6328
6329 // local_index is a cursor that keeps track of the current
6330 // index into local_table_for_iseq. The local table is actually a list,
6331 // and the order of that list must match the order of the items pushed
6332 // on the stack. We need to take in to account things pushed on the
6333 // stack that _might not have a name_ (for example array destructuring).
6334 // This index helps us know which item we're dealing with and also give
6335 // those anonymous items temporary names (as below)
6336 int local_index = 0;
6337
6338 // Here we figure out local table indices and insert them in to the
6339 // index lookup table and local tables.
6340 //
6341 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6342 // ^^^^^^^^^^^^^
6343 if (requireds_list && requireds_list->size) {
6344 for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
6345 ID local;
6346
6347 // For each MultiTargetNode, we're going to have one additional
6348 // anonymous local not represented in the locals table. We want
6349 // to account for this in our table size.
6350 pm_node_t *required = requireds_list->nodes[i];
6351
6352 switch (PM_NODE_TYPE(required)) {
6353 case PM_MULTI_TARGET_NODE: {
6354 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6355 // ^^^^^^^^^^
6356 local = rb_make_temporary_id(local_index);
6357 local_table_for_iseq->ids[local_index] = local;
6358 break;
6359 }
6360 case PM_REQUIRED_PARAMETER_NODE: {
6361 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6362 // ^
6363 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
6364
6365 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6366 ID local = pm_constant_id_lookup(scope_node, param->name);
6367 local_table_for_iseq->ids[local_index] = local;
6368 }
6369 else {
6370 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6371 }
6372
6373 break;
6374 }
6375 default:
6376 rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
6377 }
6378 }
6379
6380 body->param.lead_num = (int) requireds_list->size;
6381 body->param.flags.has_lead = true;
6382 }
6383
6384 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6385 ID local = rb_make_temporary_id(local_index);
6386 local_table_for_iseq->ids[local_index++] = local;
6387 }
6388
6389 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6390 // ^^^^^
6391 if (optionals_list && optionals_list->size) {
6392 body->param.opt_num = (int) optionals_list->size;
6393 body->param.flags.has_opt = true;
6394
6395 for (size_t i = 0; i < optionals_list->size; i++, local_index++) {
6396 pm_node_t * node = optionals_list->nodes[i];
6397 pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
6398
6399 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6400 ID local = pm_constant_id_lookup(scope_node, name);
6401 local_table_for_iseq->ids[local_index] = local;
6402 }
6403 else {
6404 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6405 }
6406 }
6407 }
6408
6409 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6410 // ^^
6411 if (parameters_node && parameters_node->rest) {
6412 body->param.rest_start = local_index;
6413
6414 // If there's a trailing comma, we'll have an implicit rest node,
6415 // and we don't want it to impact the rest variables on param
6416 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
6417 body->param.flags.has_rest = true;
6418 RUBY_ASSERT(body->param.rest_start != -1);
6419
6420 pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
6421
6422 if (name) {
6423 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6424 // ^^
6425 if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6426 ID local = pm_constant_id_lookup(scope_node, name);
6427 local_table_for_iseq->ids[local_index] = local;
6428 }
6429 else {
6430 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6431 }
6432 }
6433 else {
6434 // def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
6435 // ^
6436 body->param.flags.anon_rest = true;
6437 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6438 }
6439
6440 local_index++;
6441 }
6442 }
6443
6444 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6445 // ^^^^^^^^^^^^^
6446 if (posts_list && posts_list->size) {
6447 body->param.post_num = (int) posts_list->size;
6448 body->param.post_start = local_index;
6449 body->param.flags.has_post = true;
6450
6451 for (size_t i = 0; i < posts_list->size; i++, local_index++) {
6452 ID local;
6453
6454 // For each MultiTargetNode, we're going to have one additional
6455 // anonymous local not represented in the locals table. We want
6456 // to account for this in our table size.
6457 const pm_node_t *post_node = posts_list->nodes[i];
6458
6459 switch (PM_NODE_TYPE(post_node)) {
6460 case PM_MULTI_TARGET_NODE: {
6461 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6462 // ^^^^^^^^^^
6463 local = rb_make_temporary_id(local_index);
6464 local_table_for_iseq->ids[local_index] = local;
6465 break;
6466 }
6467 case PM_REQUIRED_PARAMETER_NODE: {
6468 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6469 // ^
6470 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
6471
6472 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6473 ID local = pm_constant_id_lookup(scope_node, param->name);
6474 local_table_for_iseq->ids[local_index] = local;
6475 }
6476 else {
6477 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6478 }
6479 break;
6480 }
6481 default:
6482 rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6483 }
6484 }
6485 }
6486
6487 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6488 // ^^^^^^^^
6489 // Keywords create an internal variable on the parse tree
6490 if (keywords_list && keywords_list->size) {
6491 keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6492 keyword->num = (int) keywords_list->size;
6493
6494 const VALUE default_values = rb_ary_hidden_new(1);
6495 const VALUE complex_mark = rb_str_tmp_new(0);
6496
6497 for (size_t i = 0; i < keywords_list->size; i++) {
6498 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6499 pm_constant_id_t name;
6500
6501 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6502 // ^^
6503 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6504 name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
6505 keyword->required_num++;
6506 ID local = pm_constant_id_lookup(scope_node, name);
6507
6508 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6509 local_table_for_iseq->ids[local_index] = local;
6510 }
6511 else {
6512 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6513 }
6514 local_index++;
6515 }
6516 }
6517
6518 for (size_t i = 0; i < keywords_list->size; i++) {
6519 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6520 pm_constant_id_t name;
6521
6522 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6523 // ^^^^
6524 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6525 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6526
6527 pm_node_t *value = cast->value;
6528 name = cast->name;
6529
6530 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6531 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6532 }
6533 else {
6534 rb_ary_push(default_values, complex_mark);
6535 }
6536
6537 ID local = pm_constant_id_lookup(scope_node, name);
6538 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6539 local_table_for_iseq->ids[local_index] = local;
6540 }
6541 else {
6542 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6543 }
6544 local_index++;
6545 }
6546
6547 }
6548
6549 if (RARRAY_LEN(default_values)) {
6550 VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
6551
6552 for (int i = 0; i < RARRAY_LEN(default_values); i++) {
6553 VALUE dv = RARRAY_AREF(default_values, i);
6554 if (dv == complex_mark) dv = Qundef;
6555 RB_OBJ_WRITE(iseq, &dvs[i], dv);
6556 }
6557
6558 keyword->default_values = dvs;
6559 }
6560
6561 // Hidden local for keyword arguments
6562 keyword->bits_start = local_index;
6563 ID local = rb_make_temporary_id(local_index);
6564 local_table_for_iseq->ids[local_index] = local;
6565 local_index++;
6566
6567 body->param.keyword = keyword;
6568 body->param.flags.has_kw = true;
6569 }
6570
6571 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
6572 body->param.flags.ambiguous_param0 = true;
6573 }
6574
6575 if (parameters_node) {
6576 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6577 // ^^^
6578 if (parameters_node->keyword_rest) {
6579 switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
6580 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6581 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
6582 // ^^^^^
6583 body->param.flags.accepts_no_kwarg = true;
6584 break;
6585 }
6586 case PM_KEYWORD_REST_PARAMETER_NODE: {
6587 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6588 // ^^^
6589 const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
6590 if (!body->param.flags.has_kw) {
6591 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6592 }
6593
6594 keyword->rest_start = local_index;
6595 body->param.flags.has_kwrest = true;
6596
6597 pm_constant_id_t constant_id = kw_rest_node->name;
6598 if (constant_id) {
6599 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6600 ID local = pm_constant_id_lookup(scope_node, constant_id);
6601 local_table_for_iseq->ids[local_index] = local;
6602 }
6603 else {
6604 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6605 }
6606 }
6607 else {
6608 body->param.flags.anon_kwrest = true;
6609 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6610 }
6611
6612 local_index++;
6613 break;
6614 }
6615 case PM_FORWARDING_PARAMETER_NODE: {
6616 // def foo(...)
6617 // ^^^
6618 if (!ISEQ_BODY(iseq)->param.flags.forwardable) {
6619 // Add the anonymous *
6620 body->param.rest_start = local_index;
6621 body->param.flags.has_rest = true;
6622 body->param.flags.anon_rest = true;
6623 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6624
6625 // Add the anonymous **
6626 RUBY_ASSERT(!body->param.flags.has_kw);
6627 body->param.flags.has_kw = false;
6628 body->param.flags.has_kwrest = true;
6629 body->param.flags.anon_kwrest = true;
6630 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
6631 keyword->rest_start = local_index;
6632 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6633
6634 // Add the anonymous &
6635 body->param.block_start = local_index;
6636 body->param.flags.has_block = true;
6637 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6638 }
6639
6640 // Add the ...
6641 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6642 break;
6643 }
6644 default:
6645 rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
6646 }
6647 }
6648
6649 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6650 // ^^
6651 if (parameters_node->block) {
6652 body->param.block_start = local_index;
6653 body->param.flags.has_block = true;
6654 iseq_set_use_block(iseq);
6655
6656 pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
6657
6658 if (name) {
6659 if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6660 ID local = pm_constant_id_lookup(scope_node, name);
6661 local_table_for_iseq->ids[local_index] = local;
6662 }
6663 else {
6664 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6665 }
6666 }
6667 else {
6668 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6669 }
6670
6671 local_index++;
6672 }
6673 }
6674
6675 //********END OF STEP 2**********
6676 // The local table is now consistent with expected
6677 // stack layout
6678
6679 // If there's only one required element in the parameters
6680 // CRuby needs to recognize it as an ambiguous parameter
6681
6682 //********STEP 3**********
6683 // Goal: fill in the names of the parameters in MultiTargetNodes
6684 //
6685 // Go through requireds again to set the multis
6686
6687 if (requireds_list && requireds_list->size) {
6688 for (size_t i = 0; i < requireds_list->size; i++) {
6689 // For each MultiTargetNode, we're going to have one
6690 // additional anonymous local not represented in the locals table
6691 // We want to account for this in our table size
6692 const pm_node_t *required = requireds_list->nodes[i];
6693
6694 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6695 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);
6696 }
6697 }
6698 }
6699
6700 // Go through posts again to set the multis
6701 if (posts_list && posts_list->size) {
6702 for (size_t i = 0; i < posts_list->size; i++) {
6703 // For each MultiTargetNode, we're going to have one
6704 // additional anonymous local not represented in the locals table
6705 // We want to account for this in our table size
6706 const pm_node_t *post = posts_list->nodes[i];
6707
6708 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6709 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);
6710 }
6711 }
6712 }
6713
6714 // Set any anonymous locals for the for node
6715 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6716 if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6717 body->param.lead_num++;
6718 }
6719 else {
6720 body->param.rest_start = local_index;
6721 body->param.flags.has_rest = true;
6722 }
6723
6724 ID local = rb_make_temporary_id(local_index);
6725 local_table_for_iseq->ids[local_index] = local;
6726 local_index++;
6727 }
6728
6729 // Fill in any NumberedParameters, if they exist
6730 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6731 int maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6732 RUBY_ASSERT(0 < maximum && maximum <= 9);
6733 for (int i = 0; i < maximum; i++, local_index++) {
6734 const uint8_t param_name[] = { '_', '1' + i };
6735 pm_constant_id_t constant_id = pm_constant_pool_find(&scope_node->parser->constant_pool, param_name, 2);
6736 RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
6737 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6738 }
6739 body->param.lead_num = maximum;
6740 body->param.flags.has_lead = true;
6741 }
6742
6743 // Fill in the anonymous `it` parameter, if it exists
6744 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6745 body->param.lead_num = 1;
6746 body->param.flags.has_lead = true;
6747 }
6748
6749 //********END OF STEP 3**********
6750
6751 //********STEP 4**********
6752 // Goal: fill in the method body locals
6753 // To be explicit, these are the non-parameter locals
6754 // We fill in the block_locals, if they exist
6755 // lambda { |x; y| y }
6756 // ^
6757 if (block_locals && block_locals->size) {
6758 for (size_t i = 0; i < block_locals->size; i++, local_index++) {
6759 pm_constant_id_t constant_id = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
6760 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6761 }
6762 }
6763
6764 // Fill in any locals we missed
6765 if (scope_node->locals.size) {
6766 for (size_t i = 0; i < scope_node->locals.size; i++) {
6767 pm_constant_id_t constant_id = locals->ids[i];
6768 if (constant_id) {
6769 struct pm_local_table_insert_ctx ctx;
6770 ctx.scope_node = scope_node;
6771 ctx.local_table_for_iseq = local_table_for_iseq;
6772 ctx.local_index = local_index;
6773
6774 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6775
6776 local_index = ctx.local_index;
6777 }
6778 }
6779 }
6780
6781 //********END OF STEP 4**********
6782
6783 // We set the index_lookup_table on the scope node so we can
6784 // refer to the parameters correctly
6785 if (scope_node->index_lookup_table) {
6786 st_free_table(scope_node->index_lookup_table);
6787 }
6788 scope_node->index_lookup_table = index_lookup_table;
6789 iseq_calc_param_size(iseq);
6790
6791 if (ISEQ_BODY(iseq)->param.flags.forwardable) {
6792 // We're treating `...` as a parameter so that frame
6793 // pushing won't clobber it.
6794 ISEQ_BODY(iseq)->param.size += 1;
6795 }
6796
6797 // FIXME: args?
6798 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6799 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6800
6801 if (keyword != NULL) {
6802 size_t keyword_start_index = keyword->bits_start - keyword->num;
6803 keyword->table = (ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6804 }
6805
6806 //********STEP 5************
6807 // Goal: compile anything that needed to be compiled
6808 if (optionals_list && optionals_list->size) {
6809 LABEL **opt_table = (LABEL **) ALLOC_N(VALUE, optionals_list->size + 1);
6810 LABEL *label;
6811
6812 // TODO: Should we make an api for NEW_LABEL where you can pass
6813 // a pointer to the label it should fill out? We already
6814 // have a list of labels allocated above so it seems wasteful
6815 // to do the copies.
6816 for (size_t i = 0; i < optionals_list->size; i++) {
6817 label = NEW_LABEL(location.line);
6818 opt_table[i] = label;
6819 PUSH_LABEL(ret, label);
6820 pm_node_t *optional_node = optionals_list->nodes[i];
6821 PM_COMPILE_NOT_POPPED(optional_node);
6822 }
6823
6824 // Set the last label
6825 label = NEW_LABEL(location.line);
6826 opt_table[optionals_list->size] = label;
6827 PUSH_LABEL(ret, label);
6828
6829 body->param.opt_table = (const VALUE *) opt_table;
6830 }
6831
6832 if (keywords_list && keywords_list->size) {
6833 size_t optional_index = 0;
6834 for (size_t i = 0; i < keywords_list->size; i++) {
6835 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
6836 pm_constant_id_t name;
6837
6838 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6839 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6840 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6841 // ^^^^
6842 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6843
6844 pm_node_t *value = cast->value;
6845 name = cast->name;
6846
6847 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6848 LABEL *end_label = NEW_LABEL(location.line);
6849
6850 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
6851 int kw_bits_idx = table_size - body->param.keyword->bits_start;
6852 PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
6853 PUSH_INSNL(ret, location, branchif, end_label);
6854 PM_COMPILE(value);
6855 PUSH_SETLOCAL(ret, location, index.index, index.level);
6856 PUSH_LABEL(ret, end_label);
6857 }
6858 optional_index++;
6859 break;
6860 }
6861 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
6862 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
6863 // ^^
6864 break;
6865 default:
6866 rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6867 }
6868 }
6869 }
6870
6871 if (requireds_list && requireds_list->size) {
6872 for (size_t i = 0; i < requireds_list->size; i++) {
6873 // For each MultiTargetNode, we're going to have one additional
6874 // anonymous local not represented in the locals table. We want
6875 // to account for this in our table size.
6876 const pm_node_t *required = requireds_list->nodes[i];
6877
6878 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6879 PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
6880 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
6881 }
6882 }
6883 }
6884
6885 if (posts_list && posts_list->size) {
6886 for (size_t i = 0; i < posts_list->size; i++) {
6887 // For each MultiTargetNode, we're going to have one additional
6888 // anonymous local not represented in the locals table. We want
6889 // to account for this in our table size.
6890 const pm_node_t *post = posts_list->nodes[i];
6891
6892 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6893 PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
6894 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
6895 }
6896 }
6897 }
6898
6899 switch (body->type) {
6900 case ISEQ_TYPE_PLAIN: {
6901 RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
6902
6904 pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6905
6906 break;
6907 }
6908 case ISEQ_TYPE_BLOCK: {
6909 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6910 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6911 const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->node_id };
6912
6913 start->rescued = LABEL_RESCUE_BEG;
6914 end->rescued = LABEL_RESCUE_END;
6915
6916 // For nodes automatically assign the iteration variable to whatever
6917 // index variable. We need to handle that write here because it has
6918 // to happen in the context of the block. Note that this happens
6919 // before the B_CALL tracepoint event.
6920 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6921 pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6922 }
6923
6924 PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
6925 PUSH_INSN(ret, block_location, nop);
6926 PUSH_LABEL(ret, start);
6927
6928 if (scope_node->body != NULL) {
6929 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6930 case PM_POST_EXECUTION_NODE: {
6931 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
6932 PUSH_INSN1(ret, block_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6933
6934 // We create another ScopeNode from the statements within the PostExecutionNode
6935 pm_scope_node_t next_scope_node;
6936 pm_scope_node_init((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6937
6938 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
6939 pm_scope_node_destroy(&next_scope_node);
6940
6941 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
6942 break;
6943 }
6944 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6946 pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6947 break;
6948 }
6949 default:
6950 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6951 break;
6952 }
6953 }
6954 else {
6955 PUSH_INSN(ret, block_location, putnil);
6956 }
6957
6958 PUSH_LABEL(ret, end);
6959 PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
6960 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6961
6962 /* wide range catch handler must put at last */
6963 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6964 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6965 break;
6966 }
6967 case ISEQ_TYPE_ENSURE: {
6968 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6969 iseq_set_exception_local_table(iseq);
6970
6971 if (scope_node->body != NULL) {
6972 PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
6973 }
6974
6975 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6976 PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
6977 return;
6978 }
6979 case ISEQ_TYPE_METHOD: {
6980 ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
6981 PUSH_TRACE(ret, RUBY_EVENT_CALL);
6982
6983 if (scope_node->body) {
6984 PM_COMPILE((const pm_node_t *) scope_node->body);
6985 }
6986 else {
6987 PUSH_INSN(ret, location, putnil);
6988 }
6989
6990 ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
6991 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
6992
6993 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6994 break;
6995 }
6996 case ISEQ_TYPE_RESCUE: {
6997 iseq_set_exception_local_table(iseq);
6998 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
6999 LABEL *lab = NEW_LABEL(location.line);
7000 LABEL *rescue_end = NEW_LABEL(location.line);
7001 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7002 PUSH_INSN1(ret, location, putobject, rb_eStandardError);
7003 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7004 PUSH_INSNL(ret, location, branchif, lab);
7005 PUSH_INSNL(ret, location, jump, rescue_end);
7006 PUSH_LABEL(ret, lab);
7007 PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
7008 PM_COMPILE((const pm_node_t *) scope_node->body);
7009 PUSH_INSN(ret, location, leave);
7010 PUSH_LABEL(ret, rescue_end);
7011 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7012 }
7013 else {
7014 PM_COMPILE((const pm_node_t *) scope_node->ast_node);
7015 }
7016 PUSH_INSN1(ret, location, throw, INT2FIX(0));
7017
7018 return;
7019 }
7020 default:
7021 if (scope_node->body) {
7022 PM_COMPILE((const pm_node_t *) scope_node->body);
7023 }
7024 else {
7025 PUSH_INSN(ret, location, putnil);
7026 }
7027 break;
7028 }
7029
7030 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7031 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
7032 PUSH_TRACE(ret, RUBY_EVENT_END);
7033 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
7034 }
7035
7036 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7037 const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->node_id };
7038 PUSH_INSN(ret, location, leave);
7039 }
7040}
7041
7042static inline void
7043pm_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)
7044{
7045 // alias $foo $bar
7046 // ^^^^^^^^^^^^^^^
7047 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7048
7049 {
7050 const pm_location_t *name_loc = &node->new_name->location;
7051 VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
7052 PUSH_INSN1(ret, *location, putobject, operand);
7053 }
7054
7055 {
7056 const pm_location_t *name_loc = &node->old_name->location;
7057 VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
7058 PUSH_INSN1(ret, *location, putobject, operand);
7059 }
7060
7061 PUSH_SEND(ret, *location, id_core_set_variable_alias, INT2FIX(2));
7062 if (popped) PUSH_INSN(ret, *location, pop);
7063}
7064
7065static inline void
7066pm_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)
7067{
7068 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7069 PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7070 PM_COMPILE_NOT_POPPED(node->new_name);
7071 PM_COMPILE_NOT_POPPED(node->old_name);
7072
7073 PUSH_SEND(ret, *location, id_core_set_method_alias, INT2FIX(3));
7074 if (popped) PUSH_INSN(ret, *location, pop);
7075}
7076
7077static inline void
7078pm_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)
7079{
7080 LABEL *end_label = NEW_LABEL(location->line);
7081
7082 PM_COMPILE_NOT_POPPED(node->left);
7083 if (!popped) PUSH_INSN(ret, *location, dup);
7084 PUSH_INSNL(ret, *location, branchunless, end_label);
7085
7086 if (!popped) PUSH_INSN(ret, *location, pop);
7087 PM_COMPILE(node->right);
7088 PUSH_LABEL(ret, end_label);
7089}
7090
7091static inline void
7092pm_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)
7093{
7094 // If every node in the array is static, then we can compile the entire
7095 // array now instead of later.
7096 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7097 // We're only going to compile this node if it's not popped. If it
7098 // is popped, then we know we don't need to do anything since it's
7099 // statically known.
7100 if (!popped) {
7101 if (elements->size) {
7102 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7103 PUSH_INSN1(ret, *location, duparray, value);
7104 }
7105 else {
7106 PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
7107 }
7108 }
7109 return;
7110 }
7111
7112 // Here since we know there are possible side-effects inside the
7113 // array contents, we're going to build it entirely at runtime.
7114 // We'll do this by pushing all of the elements onto the stack and
7115 // then combining them with newarray.
7116 //
7117 // If this array is popped, then this serves only to ensure we enact
7118 // all side-effects (like method calls) that are contained within
7119 // the array contents.
7120 //
7121 // We treat all sequences of non-splat elements as their
7122 // own arrays, followed by a newarray, and then continually
7123 // concat the arrays with the SplatNode nodes.
7124 const int max_new_array_size = 0x100;
7125 const unsigned int min_tmp_array_size = 0x40;
7126
7127 int new_array_size = 0;
7128 bool first_chunk = true;
7129
7130 // This is an optimization wherein we keep track of whether or not
7131 // the previous element was a static literal. If it was, then we do
7132 // not attempt to check if we have a subarray that can be optimized.
7133 // If it was not, then we do check.
7134 bool static_literal = false;
7135
7136 // Either create a new array, or push to the existing array.
7137#define FLUSH_CHUNK \
7138 if (new_array_size) { \
7139 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7140 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7141 first_chunk = false; \
7142 new_array_size = 0; \
7143 }
7144
7145 for (size_t index = 0; index < elements->size; index++) {
7146 const pm_node_t *element = elements->nodes[index];
7147
7148 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7149 FLUSH_CHUNK;
7150
7151 const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
7152 if (splat_element->expression) {
7153 PM_COMPILE_NOT_POPPED(splat_element->expression);
7154 }
7155 else {
7156 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7157 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7158 }
7159
7160 if (first_chunk) {
7161 // If this is the first element of the array then we
7162 // need to splatarray the elements into the list.
7163 PUSH_INSN1(ret, *location, splatarray, Qtrue);
7164 first_chunk = false;
7165 }
7166 else {
7167 PUSH_INSN(ret, *location, concattoarray);
7168 }
7169
7170 static_literal = false;
7171 }
7172 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7173 if (new_array_size == 0 && first_chunk) {
7174 PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
7175 first_chunk = false;
7176 }
7177 else {
7178 FLUSH_CHUNK;
7179 }
7180
7181 // If we get here, then this is the last element of the
7182 // array/arguments, because it cannot be followed by
7183 // anything else without a syntax error. This looks like:
7184 //
7185 // [foo, bar, baz: qux]
7186 // ^^^^^^^^
7187 //
7188 // [foo, bar, **baz]
7189 // ^^^^^
7190 //
7191 const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) element;
7192 pm_compile_hash_elements(iseq, element, &keyword_hash->elements, 0, Qundef, false, ret, scope_node);
7193
7194 // This boolean controls the manner in which we push the
7195 // hash onto the array. If it's all keyword splats, then we
7196 // can use the very specialized pushtoarraykwsplat
7197 // instruction to check if it's empty before we push it.
7198 size_t splats = 0;
7199 while (splats < keyword_hash->elements.size && PM_NODE_TYPE_P(keyword_hash->elements.nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7200
7201 if (keyword_hash->elements.size == splats) {
7202 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7203 }
7204 else {
7205 new_array_size++;
7206 }
7207 }
7208 else if (
7209 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7210 !PM_CONTAINER_P(element) &&
7211 !static_literal &&
7212 ((index + min_tmp_array_size) < elements->size)
7213 ) {
7214 // If we have a static literal, then there's the potential
7215 // to group a bunch of them together with a literal array
7216 // and then concat them together.
7217 size_t right_index = index + 1;
7218 while (
7219 right_index < elements->size &&
7220 PM_NODE_FLAG_P(elements->nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7221 !PM_CONTAINER_P(elements->nodes[right_index])
7222 ) right_index++;
7223
7224 size_t tmp_array_size = right_index - index;
7225 if (tmp_array_size >= min_tmp_array_size) {
7226 VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
7227
7228 // Create the temporary array.
7229 for (; tmp_array_size; tmp_array_size--)
7230 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->nodes[index++], scope_node));
7231
7232 index--; // about to be incremented by for loop
7233 OBJ_FREEZE(tmp_array);
7234
7235 // Emit the optimized code.
7236 FLUSH_CHUNK;
7237 if (first_chunk) {
7238 PUSH_INSN1(ret, *location, duparray, tmp_array);
7239 first_chunk = false;
7240 }
7241 else {
7242 PUSH_INSN1(ret, *location, putobject, tmp_array);
7243 PUSH_INSN(ret, *location, concattoarray);
7244 }
7245 }
7246 else {
7247 PM_COMPILE_NOT_POPPED(element);
7248 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7249 static_literal = true;
7250 }
7251 } else {
7252 PM_COMPILE_NOT_POPPED(element);
7253 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7254 static_literal = false;
7255 }
7256 }
7257
7258 FLUSH_CHUNK;
7259 if (popped) PUSH_INSN(ret, *location, pop);
7260
7261#undef FLUSH_CHUNK
7262}
7263
7264static inline void
7265pm_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)
7266{
7267 unsigned long throw_flag = 0;
7268
7269 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7270 /* while/until */
7271 LABEL *splabel = NEW_LABEL(0);
7272 PUSH_LABEL(ret, splabel);
7273 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7274
7275 if (node->arguments != NULL) {
7276 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
7277 }
7278 else {
7279 PUSH_INSN(ret, *location, putnil);
7280 }
7281
7282 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7283 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7284 PUSH_ADJUST_RESTORE(ret, splabel);
7285 if (!popped) PUSH_INSN(ret, *location, putnil);
7286 }
7287 else {
7288 const rb_iseq_t *ip = iseq;
7289
7290 while (ip) {
7291 if (!ISEQ_COMPILE_DATA(ip)) {
7292 ip = 0;
7293 break;
7294 }
7295
7296 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7297 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7298 }
7299 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
7300 throw_flag = 0;
7301 }
7302 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
7303 COMPILE_ERROR(iseq, location->line, "Invalid break");
7304 return;
7305 }
7306 else {
7307 ip = ISEQ_BODY(ip)->parent_iseq;
7308 continue;
7309 }
7310
7311 /* escape from block */
7312 if (node->arguments != NULL) {
7313 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
7314 }
7315 else {
7316 PUSH_INSN(ret, *location, putnil);
7317 }
7318
7319 PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_BREAK));
7320 if (popped) PUSH_INSN(ret, *location, pop);
7321
7322 return;
7323 }
7324
7325 COMPILE_ERROR(iseq, location->line, "Invalid break");
7326 }
7327}
7328
7329static inline void
7330pm_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)
7331{
7332 ID method_id = pm_constant_id_lookup(scope_node, node->name);
7333
7334 const pm_location_t *message_loc = &node->message_loc;
7335 if (message_loc->start == NULL) message_loc = &node->base.location;
7336
7337 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, node->base.node_id);
7338 const char *builtin_func;
7339
7340 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->receiver, method_id)) != NULL) {
7341 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7342 return;
7343 }
7344
7345 LABEL *start = NEW_LABEL(location.line);
7346 if (node->block) PUSH_LABEL(ret, start);
7347
7348 switch (method_id) {
7349 case idUMinus: {
7350 if (pm_opt_str_freeze_p(iseq, node)) {
7351 VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
7352 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7353 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7354 if (popped) PUSH_INSN(ret, location, pop);
7355 return;
7356 }
7357 break;
7358 }
7359 case idFreeze: {
7360 if (pm_opt_str_freeze_p(iseq, node)) {
7361 VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
7362 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7363 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7364 if (popped) PUSH_INSN(ret, location, pop);
7365 return;
7366 }
7367 break;
7368 }
7369 }
7370
7371 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7372 PUSH_INSN(ret, location, putnil);
7373 }
7374
7375 if (node->receiver == NULL) {
7376 PUSH_INSN(ret, location, putself);
7377 }
7378 else {
7379 if (method_id == idCall && PM_NODE_TYPE_P(node->receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7380 const pm_local_variable_read_node_t *read_node_cast = (const pm_local_variable_read_node_t *) node->receiver;
7381 uint32_t node_id = node->receiver->node_id;
7382 int idx, level;
7383
7384 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7385 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)));
7386 }
7387 else {
7388 PM_COMPILE_NOT_POPPED(node->receiver);
7389 }
7390 }
7391 else {
7392 PM_COMPILE_NOT_POPPED(node->receiver);
7393 }
7394 }
7395
7396 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7397 return;
7398}
7399
7400static inline void
7401pm_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)
7402{
7403 int flag = 0;
7404
7405 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7406 flag = VM_CALL_FCALL;
7407 }
7408
7409 PM_COMPILE_NOT_POPPED(node->receiver);
7410
7411 LABEL *safe_label = NULL;
7412 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7413 safe_label = NEW_LABEL(location->line);
7414 PUSH_INSN(ret, *location, dup);
7415 PUSH_INSNL(ret, *location, branchnil, safe_label);
7416 }
7417
7418 PUSH_INSN(ret, *location, dup);
7419
7420 ID id_read_name = pm_constant_id_lookup(scope_node, node->read_name);
7421 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name, INT2FIX(0), INT2FIX(flag));
7422
7423 PM_COMPILE_NOT_POPPED(node->value);
7424 ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
7425 PUSH_SEND(ret, *location, id_operator, INT2FIX(1));
7426
7427 if (!popped) {
7428 PUSH_INSN(ret, *location, swap);
7429 PUSH_INSN1(ret, *location, topn, INT2FIX(1));
7430 }
7431
7432 ID id_write_name = pm_constant_id_lookup(scope_node, node->write_name);
7433 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name, INT2FIX(1), INT2FIX(flag));
7434
7435 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7436 PUSH_INSN(ret, *location, pop);
7437 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7438}
7439
7456static VALUE
7457pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
7458{
7459 VALUE key = Qundef;
7460
7461 switch (PM_NODE_TYPE(node)) {
7462 case PM_FLOAT_NODE: {
7463 key = pm_static_literal_value(iseq, node, scope_node);
7464 double intptr;
7465
7466 if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
7467 key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
7468 }
7469
7470 break;
7471 }
7472 case PM_FALSE_NODE:
7473 case PM_INTEGER_NODE:
7474 case PM_NIL_NODE:
7475 case PM_SOURCE_FILE_NODE:
7476 case PM_SOURCE_LINE_NODE:
7477 case PM_SYMBOL_NODE:
7478 case PM_TRUE_NODE:
7479 key = pm_static_literal_value(iseq, node, scope_node);
7480 break;
7481 case PM_STRING_NODE: {
7482 const pm_string_node_t *cast = (const pm_string_node_t *) node;
7483 key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
7484 break;
7485 }
7486 default:
7487 return Qundef;
7488 }
7489
7490 if (NIL_P(rb_hash_lookup(dispatch, key))) {
7491 rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
7492 }
7493
7494 return dispatch;
7495}
7496
7500static inline void
7501pm_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)
7502{
7503 const pm_parser_t *parser = scope_node->parser;
7504 const pm_node_location_t location = *node_location;
7505 const pm_node_list_t *conditions = &cast->conditions;
7506
7507 // This is the anchor that we will compile the conditions of the various
7508 // `when` nodes into. If a match is found, they will need to jump into
7509 // the body_seq anchor to the correct spot.
7510 DECL_ANCHOR(cond_seq);
7511
7512 // This is the anchor that we will compile the bodies of the various
7513 // `when` nodes into. We'll make sure that the clauses that are compiled
7514 // jump into the correct spots within this anchor.
7515 DECL_ANCHOR(body_seq);
7516
7517 // This is the label where all of the when clauses will jump to if they
7518 // have matched and are done executing their bodies.
7519 LABEL *end_label = NEW_LABEL(location.line);
7520
7521 // If we have a predicate on this case statement, then it's going to
7522 // compare all of the various when clauses to the predicate. If we
7523 // don't, then it's basically an if-elsif-else chain.
7524 if (cast->predicate == NULL) {
7525 // Establish branch coverage for the case node.
7526 VALUE branches = Qfalse;
7527 rb_code_location_t case_location = { 0 };
7528 int branch_id = 0;
7529
7530 if (PM_BRANCH_COVERAGE_P(iseq)) {
7531 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
7532 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
7533 }
7534
7535 // Loop through each clauses in the case node and compile each of
7536 // the conditions within them into cond_seq. If they match, they
7537 // should jump into their respective bodies in body_seq.
7538 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
7539 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
7540 const pm_node_list_t *conditions = &clause->conditions;
7541
7542 int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
7543 LABEL *label = NEW_LABEL(clause_lineno);
7544 PUSH_LABEL(body_seq, label);
7545
7546 // Establish branch coverage for the when clause.
7547 if (PM_BRANCH_COVERAGE_P(iseq)) {
7548 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));
7549 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
7550 }
7551
7552 if (clause->statements != NULL) {
7553 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7554 }
7555 else if (!popped) {
7556 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7557 }
7558
7559 PUSH_INSNL(body_seq, location, jump, end_label);
7560
7561 // Compile each of the conditions for the when clause into the
7562 // cond_seq. Each one should have a unique condition and should
7563 // jump to the subsequent one if it doesn't match.
7564 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
7565 const pm_node_t *condition = conditions->nodes[condition_index];
7566
7567 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7568 pm_node_location_t cond_location = PM_NODE_START_LOCATION(parser, condition);
7569 PUSH_INSN(cond_seq, cond_location, putnil);
7570 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7571 PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7572 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7573 }
7574 else {
7575 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7576 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
7577 PUSH_LABEL(cond_seq, next_label);
7578 }
7579 }
7580 }
7581
7582 // Establish branch coverage for the else clause (implicit or
7583 // explicit).
7584 if (PM_BRANCH_COVERAGE_P(iseq)) {
7585 rb_code_location_t branch_location;
7586
7587 if (cast->else_clause == NULL) {
7588 branch_location = case_location;
7589 } else if (cast->else_clause->statements == NULL) {
7590 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause);
7591 } else {
7592 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause->statements);
7593 }
7594
7595 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7596 }
7597
7598 // Compile the else clause if there is one.
7599 if (cast->else_clause != NULL) {
7600 pm_compile_node(iseq, (const pm_node_t *) cast->else_clause, cond_seq, popped, scope_node);
7601 }
7602 else if (!popped) {
7603 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7604 }
7605
7606 // Finally, jump to the end label if none of the other conditions
7607 // have matched.
7608 PUSH_INSNL(cond_seq, location, jump, end_label);
7609 PUSH_SEQ(ret, cond_seq);
7610 }
7611 else {
7612 // Establish branch coverage for the case node.
7613 VALUE branches = Qfalse;
7614 rb_code_location_t case_location = { 0 };
7615 int branch_id = 0;
7616
7617 if (PM_BRANCH_COVERAGE_P(iseq)) {
7618 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
7619 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
7620 }
7621
7622 // This is the label where everything will fall into if none of the
7623 // conditions matched.
7624 LABEL *else_label = NEW_LABEL(location.line);
7625
7626 // It's possible for us to speed up the case node by using a
7627 // dispatch hash. This is a hash that maps the conditions of the
7628 // various when clauses to the labels of their bodies. If we can
7629 // compile the conditions into a hash key, then we can use a hash
7630 // lookup to jump directly to the correct when clause body.
7631 VALUE dispatch = Qundef;
7632 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7633 dispatch = rb_hash_new();
7634 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7635 }
7636
7637 // We're going to loop through each of the conditions in the case
7638 // node and compile each of their contents into both the cond_seq
7639 // and the body_seq. Each condition will use its own label to jump
7640 // from its conditions into its body.
7641 //
7642 // Note that none of the code in the loop below should be adding
7643 // anything to ret, as we're going to be laying out the entire case
7644 // node instructions later.
7645 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
7646 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
7647 pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (const pm_node_t *) clause);
7648
7649 const pm_node_list_t *conditions = &clause->conditions;
7650 LABEL *label = NEW_LABEL(clause_location.line);
7651
7652 // Compile each of the conditions for the when clause into the
7653 // cond_seq. Each one should have a unique comparison that then
7654 // jumps into the body if it matches.
7655 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
7656 const pm_node_t *condition = conditions->nodes[condition_index];
7657 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7658
7659 // If we haven't already abandoned the optimization, then
7660 // we're going to try to compile the condition into the
7661 // dispatch hash.
7662 if (dispatch != Qundef) {
7663 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7664 }
7665
7666 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7667 PUSH_INSN(cond_seq, condition_location, dup);
7668 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7669 PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7670 }
7671 else {
7672 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7673 const pm_string_node_t *string = (const pm_string_node_t *) condition;
7674 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
7675 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7676 }
7677 else {
7678 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
7679 }
7680
7681 PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
7682 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7683 }
7684
7685 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7686 }
7687
7688 // Now, add the label to the body and compile the body of the
7689 // when clause. This involves popping the predicate, compiling
7690 // the statements to be executed, and then compiling a jump to
7691 // the end of the case node.
7692 PUSH_LABEL(body_seq, label);
7693 PUSH_INSN(body_seq, clause_location, pop);
7694
7695 // Establish branch coverage for the when clause.
7696 if (PM_BRANCH_COVERAGE_P(iseq)) {
7697 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));
7698 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
7699 }
7700
7701 if (clause->statements != NULL) {
7702 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7703 }
7704 else if (!popped) {
7705 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7706 }
7707
7708 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7709 }
7710
7711 // Now that we have compiled the conditions and the bodies of the
7712 // various when clauses, we can compile the predicate, lay out the
7713 // conditions, compile the fallback subsequent if there is one, and
7714 // finally put in the bodies of the when clauses.
7715 PM_COMPILE_NOT_POPPED(cast->predicate);
7716
7717 // If we have a dispatch hash, then we'll use it here to create the
7718 // optimization.
7719 if (dispatch != Qundef) {
7720 PUSH_INSN(ret, location, dup);
7721 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7722 LABEL_REF(else_label);
7723 }
7724
7725 PUSH_SEQ(ret, cond_seq);
7726
7727 // Compile either the explicit else clause or an implicit else
7728 // clause.
7729 PUSH_LABEL(ret, else_label);
7730
7731 if (cast->else_clause != NULL) {
7732 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));
7733 PUSH_INSN(ret, else_location, pop);
7734
7735 // Establish branch coverage for the else clause.
7736 if (PM_BRANCH_COVERAGE_P(iseq)) {
7737 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));
7738 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7739 }
7740
7741 PM_COMPILE((const pm_node_t *) cast->else_clause);
7742 PUSH_INSNL(ret, else_location, jump, end_label);
7743 }
7744 else {
7745 PUSH_INSN(ret, location, pop);
7746
7747 // Establish branch coverage for the implicit else clause.
7748 if (PM_BRANCH_COVERAGE_P(iseq)) {
7749 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
7750 }
7751
7752 if (!popped) PUSH_INSN(ret, location, putnil);
7753 PUSH_INSNL(ret, location, jump, end_label);
7754 }
7755 }
7756
7757 PUSH_SEQ(ret, body_seq);
7758 PUSH_LABEL(ret, end_label);
7759}
7760
7761static inline void
7762pm_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)
7763{
7764 // This is the anchor that we will compile the bodies of the various
7765 // `in` nodes into. We'll make sure that the patterns that are compiled
7766 // jump into the correct spots within this anchor.
7767 DECL_ANCHOR(body_seq);
7768
7769 // This is the anchor that we will compile the patterns of the various
7770 // `in` nodes into. If a match is found, they will need to jump into the
7771 // body_seq anchor to the correct spot.
7772 DECL_ANCHOR(cond_seq);
7773
7774 // This label is used to indicate the end of the entire node. It is
7775 // jumped to after the entire stack is cleaned up.
7776 LABEL *end_label = NEW_LABEL(location->line);
7777
7778 // This label is used as the fallback for the case match. If no match is
7779 // found, then we jump to this label. This is either an `else` clause or
7780 // an error handler.
7781 LABEL *else_label = NEW_LABEL(location->line);
7782
7783 // We're going to use this to uniquely identify each branch so that we
7784 // can track coverage information.
7785 rb_code_location_t case_location = { 0 };
7786 VALUE branches = Qfalse;
7787 int branch_id = 0;
7788
7789 if (PM_BRANCH_COVERAGE_P(iseq)) {
7790 case_location = pm_code_location(scope_node, (const pm_node_t *) node);
7791 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location, "case");
7792 }
7793
7794 // If there is only one pattern, then the behavior changes a bit. It
7795 // effectively gets treated as a match required node (this is how it is
7796 // represented in the other parser).
7797 bool in_single_pattern = node->else_clause == NULL && node->conditions.size == 1;
7798
7799 // First, we're going to push a bunch of stuff onto the stack that is
7800 // going to serve as our scratch space.
7801 if (in_single_pattern) {
7802 PUSH_INSN(ret, *location, putnil); // key error key
7803 PUSH_INSN(ret, *location, putnil); // key error matchee
7804 PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
7805 PUSH_INSN(ret, *location, putnil); // error string
7806 }
7807
7808 // Now we're going to compile the value to match against.
7809 PUSH_INSN(ret, *location, putnil); // deconstruct cache
7810 PM_COMPILE_NOT_POPPED(node->predicate);
7811
7812 // Next, we'll loop through every in clause and compile its body into
7813 // the body_seq anchor and its pattern into the cond_seq anchor. We'll
7814 // make sure the pattern knows how to jump correctly into the body if it
7815 // finds a match.
7816 for (size_t index = 0; index < node->conditions.size; index++) {
7817 const pm_node_t *condition = node->conditions.nodes[index];
7818 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
7819
7820 const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
7821 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7822 const pm_node_location_t pattern_location = PM_NODE_START_LOCATION(scope_node->parser, in_node->pattern);
7823
7824 if (branch_id) {
7825 PUSH_INSN(body_seq, in_location, putnil);
7826 }
7827
7828 LABEL *body_label = NEW_LABEL(in_location.line);
7829 PUSH_LABEL(body_seq, body_label);
7830 PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
7831
7832 // Establish branch coverage for the in clause.
7833 if (PM_BRANCH_COVERAGE_P(iseq)) {
7834 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));
7835 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
7836 }
7837
7838 if (in_node->statements != NULL) {
7839 PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
7840 }
7841 else if (!popped) {
7842 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7843 }
7844
7845 PUSH_INSNL(body_seq, in_location, jump, end_label);
7846 LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
7847
7848 PUSH_INSN(cond_seq, pattern_location, dup);
7849 pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
7850 PUSH_LABEL(cond_seq, next_pattern_label);
7851 LABEL_UNREMOVABLE(next_pattern_label);
7852 }
7853
7854 if (node->else_clause != NULL) {
7855 // If we have an `else` clause, then this becomes our fallback (and
7856 // there is no need to compile in code to potentially raise an
7857 // error).
7858 const pm_else_node_t *else_node = node->else_clause;
7859
7860 PUSH_LABEL(cond_seq, else_label);
7861 PUSH_INSN(cond_seq, *location, pop);
7862 PUSH_INSN(cond_seq, *location, pop);
7863
7864 // Establish branch coverage for the else clause.
7865 if (PM_BRANCH_COVERAGE_P(iseq)) {
7866 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));
7867 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
7868 }
7869
7870 PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
7871 PUSH_INSNL(cond_seq, *location, jump, end_label);
7872 PUSH_INSN(cond_seq, *location, putnil);
7873 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7874 }
7875 else {
7876 // Otherwise, if we do not have an `else` clause, we will compile in
7877 // the code to handle raising an appropriate error.
7878 PUSH_LABEL(cond_seq, else_label);
7879
7880 // Establish branch coverage for the implicit else clause.
7881 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
7882
7883 if (in_single_pattern) {
7884 pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, cond_seq, end_label, popped);
7885 }
7886 else {
7887 PUSH_INSN1(cond_seq, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7888 PUSH_INSN1(cond_seq, *location, putobject, rb_eNoMatchingPatternError);
7889 PUSH_INSN1(cond_seq, *location, topn, INT2FIX(2));
7890 PUSH_SEND(cond_seq, *location, id_core_raise, INT2FIX(2));
7891
7892 PUSH_INSN1(cond_seq, *location, adjuststack, INT2FIX(3));
7893 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7894 PUSH_INSNL(cond_seq, *location, jump, end_label);
7895 PUSH_INSN1(cond_seq, *location, dupn, INT2FIX(1));
7896 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7897 }
7898 }
7899
7900 // At the end of all of this compilation, we will add the code for the
7901 // conditions first, then the various bodies, then mark the end of the
7902 // entire sequence with the end label.
7903 PUSH_SEQ(ret, cond_seq);
7904 PUSH_SEQ(ret, body_seq);
7905 PUSH_LABEL(ret, end_label);
7906}
7907
7908static inline void
7909pm_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)
7910{
7911 const rb_iseq_t *block = NULL;
7912 const rb_iseq_t *previous_block = NULL;
7913 LABEL *retry_label = NULL;
7914 LABEL *retry_end_l = NULL;
7915
7916 if (node->block != NULL) {
7917 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7918 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7919
7920 retry_label = NEW_LABEL(location->line);
7921 retry_end_l = NEW_LABEL(location->line);
7922
7923 PUSH_LABEL(ret, retry_label);
7924 }
7925 else {
7926 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7927 }
7928
7929 PUSH_INSN(ret, *location, putself);
7930 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7931
7932 if (node->block != NULL) {
7933 pm_scope_node_t next_scope_node;
7934 pm_scope_node_init((const pm_node_t *) node->block, &next_scope_node, scope_node);
7935
7936 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
7937 pm_scope_node_destroy(&next_scope_node);
7938 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
7939 }
7940
7941 DECL_ANCHOR(args);
7942
7943 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
7944 const rb_iseq_t *local_iseq = body->local_iseq;
7945 const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(local_iseq);
7946
7947 int argc = 0;
7948 int depth = get_lvar_level(iseq);
7949
7950 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
7951 flag |= VM_CALL_FORWARDING;
7952 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
7953 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
7954
7955 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
7956 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
7957
7958 if (popped) PUSH_INSN(ret, *location, pop);
7959 if (node->block) {
7960 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7961 }
7962 return;
7963 }
7964
7965 if (local_body->param.flags.has_lead) {
7966 /* required arguments */
7967 for (int i = 0; i < local_body->param.lead_num; i++) {
7968 int idx = local_body->local_table_size - i;
7969 PUSH_GETLOCAL(args, *location, idx, depth);
7970 }
7971 argc += local_body->param.lead_num;
7972 }
7973
7974 if (local_body->param.flags.has_opt) {
7975 /* optional arguments */
7976 for (int j = 0; j < local_body->param.opt_num; j++) {
7977 int idx = local_body->local_table_size - (argc + j);
7978 PUSH_GETLOCAL(args, *location, idx, depth);
7979 }
7980 argc += local_body->param.opt_num;
7981 }
7982
7983 if (local_body->param.flags.has_rest) {
7984 /* rest argument */
7985 int idx = local_body->local_table_size - local_body->param.rest_start;
7986 PUSH_GETLOCAL(args, *location, idx, depth);
7987 PUSH_INSN1(args, *location, splatarray, Qfalse);
7988
7989 argc = local_body->param.rest_start + 1;
7990 flag |= VM_CALL_ARGS_SPLAT;
7991 }
7992
7993 if (local_body->param.flags.has_post) {
7994 /* post arguments */
7995 int post_len = local_body->param.post_num;
7996 int post_start = local_body->param.post_start;
7997
7998 int j = 0;
7999 for (; j < post_len; j++) {
8000 int idx = local_body->local_table_size - (post_start + j);
8001 PUSH_GETLOCAL(args, *location, idx, depth);
8002 }
8003
8004 if (local_body->param.flags.has_rest) {
8005 // argc remains unchanged from rest branch
8006 PUSH_INSN1(args, *location, newarray, INT2FIX(j));
8007 PUSH_INSN(args, *location, concatarray);
8008 }
8009 else {
8010 argc = post_len + post_start;
8011 }
8012 }
8013
8014 const struct rb_iseq_param_keyword *const local_keyword = local_body->param.keyword;
8015 if (local_body->param.flags.has_kw) {
8016 int local_size = local_body->local_table_size;
8017 argc++;
8018
8019 PUSH_INSN1(args, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8020
8021 if (local_body->param.flags.has_kwrest) {
8022 int idx = local_body->local_table_size - local_keyword->rest_start;
8023 PUSH_GETLOCAL(args, *location, idx, depth);
8024 RUBY_ASSERT(local_keyword->num > 0);
8025 PUSH_SEND(args, *location, rb_intern("dup"), INT2FIX(0));
8026 }
8027 else {
8028 PUSH_INSN1(args, *location, newhash, INT2FIX(0));
8029 }
8030 int i = 0;
8031 for (; i < local_keyword->num; ++i) {
8032 ID id = local_keyword->table[i];
8033 int idx = local_size - get_local_var_idx(local_iseq, id);
8034
8035 {
8036 VALUE operand = ID2SYM(id);
8037 PUSH_INSN1(args, *location, putobject, operand);
8038 }
8039
8040 PUSH_GETLOCAL(args, *location, idx, depth);
8041 }
8042
8043 PUSH_SEND(args, *location, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
8044 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8045 }
8046 else if (local_body->param.flags.has_kwrest) {
8047 int idx = local_body->local_table_size - local_keyword->rest_start;
8048 PUSH_GETLOCAL(args, *location, idx, depth);
8049 argc++;
8050 flag |= VM_CALL_KW_SPLAT;
8051 }
8052
8053 PUSH_SEQ(ret, args);
8054
8055 {
8056 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8057 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8058 }
8059
8060 if (node->block != NULL) {
8061 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8062 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8063 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8064 }
8065
8066 if (popped) PUSH_INSN(ret, *location, pop);
8067}
8068
8069static inline void
8070pm_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)
8071{
8072 LABEL *matched_label = NEW_LABEL(location->line);
8073 LABEL *unmatched_label = NEW_LABEL(location->line);
8074 LABEL *done_label = NEW_LABEL(location->line);
8075
8076 // First, we're going to push a bunch of stuff onto the stack that is
8077 // going to serve as our scratch space.
8078 PUSH_INSN(ret, *location, putnil); // key error key
8079 PUSH_INSN(ret, *location, putnil); // key error matchee
8080 PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
8081 PUSH_INSN(ret, *location, putnil); // error string
8082 PUSH_INSN(ret, *location, putnil); // deconstruct cache
8083
8084 // Next we're going to compile the value expression such that it's on
8085 // the stack.
8086 PM_COMPILE_NOT_POPPED(node->value);
8087
8088 // Here we'll dup it so that it can be used for comparison, but also be
8089 // used for error handling.
8090 PUSH_INSN(ret, *location, dup);
8091
8092 // Next we'll compile the pattern. We indicate to the pm_compile_pattern
8093 // function that this is the only pattern that will be matched against
8094 // through the in_single_pattern parameter. We also indicate that the
8095 // value to compare against is 2 slots from the top of the stack (the
8096 // base_index parameter).
8097 pm_compile_pattern(iseq, scope_node, node->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
8098
8099 // If the pattern did not match the value, then we're going to compile
8100 // in our error handler code. This will determine which error to raise
8101 // and raise it.
8102 PUSH_LABEL(ret, unmatched_label);
8103 pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, ret, done_label, popped);
8104
8105 // If the pattern did match, we'll clean up the values we've pushed onto
8106 // the stack and then push nil onto the stack if it's not popped.
8107 PUSH_LABEL(ret, matched_label);
8108 PUSH_INSN1(ret, *location, adjuststack, INT2FIX(6));
8109 if (!popped) PUSH_INSN(ret, *location, putnil);
8110 PUSH_INSNL(ret, *location, jump, done_label);
8111
8112 PUSH_LABEL(ret, done_label);
8113}
8114
8115static inline void
8116pm_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)
8117{
8118 LABEL *fail_label = NEW_LABEL(location->line);
8119 LABEL *end_label = NEW_LABEL(location->line);
8120
8121 // First, we'll compile the call so that all of its instructions are
8122 // present. Then we'll compile all of the local variable targets.
8123 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->call);
8124
8125 // Now, check if the match was successful. If it was, then we'll
8126 // continue on and assign local variables. Otherwise we'll skip over the
8127 // assignment code.
8128 {
8129 VALUE operand = rb_id2sym(idBACKREF);
8130 PUSH_INSN1(ret, *location, getglobal, operand);
8131 }
8132
8133 PUSH_INSN(ret, *location, dup);
8134 PUSH_INSNL(ret, *location, branchunless, fail_label);
8135
8136 // If there's only a single local variable target, we can skip some of
8137 // the bookkeeping, so we'll put a special branch here.
8138 size_t targets_count = node->targets.size;
8139
8140 if (targets_count == 1) {
8141 const pm_node_t *target = node->targets.nodes[0];
8142 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8143
8144 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
8145 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
8146
8147 {
8148 VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
8149 PUSH_INSN1(ret, *location, putobject, operand);
8150 }
8151
8152 PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
8153 PUSH_LABEL(ret, fail_label);
8154 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8155 if (popped) PUSH_INSN(ret, *location, pop);
8156 return;
8157 }
8158
8159 DECL_ANCHOR(fail_anchor);
8160
8161 // Otherwise there is more than one local variable target, so we'll need
8162 // to do some bookkeeping.
8163 for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
8164 const pm_node_t *target = node->targets.nodes[targets_index];
8165 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8166
8167 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
8168 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
8169
8170 if (((size_t) targets_index) < (targets_count - 1)) {
8171 PUSH_INSN(ret, *location, dup);
8172 }
8173
8174 {
8175 VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
8176 PUSH_INSN1(ret, *location, putobject, operand);
8177 }
8178
8179 PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
8180 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8181
8182 PUSH_INSN(fail_anchor, *location, putnil);
8183 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8184 }
8185
8186 // Since we matched successfully, now we'll jump to the end.
8187 PUSH_INSNL(ret, *location, jump, end_label);
8188
8189 // In the case that the match failed, we'll loop through each local
8190 // variable target and set all of them to `nil`.
8191 PUSH_LABEL(ret, fail_label);
8192 PUSH_INSN(ret, *location, pop);
8193 PUSH_SEQ(ret, fail_anchor);
8194
8195 // Finally, we can push the end label for either case.
8196 PUSH_LABEL(ret, end_label);
8197 if (popped) PUSH_INSN(ret, *location, pop);
8198}
8199
8200static inline void
8201pm_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)
8202{
8203 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8204 LABEL *splabel = NEW_LABEL(0);
8205 PUSH_LABEL(ret, splabel);
8206
8207 if (node->arguments) {
8208 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
8209 }
8210 else {
8211 PUSH_INSN(ret, *location, putnil);
8212 }
8213 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8214
8215 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8216 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8217
8218 PUSH_ADJUST_RESTORE(ret, splabel);
8219 if (!popped) PUSH_INSN(ret, *location, putnil);
8220 }
8221 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8222 LABEL *splabel = NEW_LABEL(0);
8223
8224 PUSH_LABEL(ret, splabel);
8225 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8226
8227 if (node->arguments != NULL) {
8228 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
8229 }
8230 else {
8231 PUSH_INSN(ret, *location, putnil);
8232 }
8233
8234 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8235 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8236 PUSH_ADJUST_RESTORE(ret, splabel);
8237 splabel->unremovable = FALSE;
8238
8239 if (!popped) PUSH_INSN(ret, *location, putnil);
8240 }
8241 else {
8242 const rb_iseq_t *ip = iseq;
8243 unsigned long throw_flag = 0;
8244
8245 while (ip) {
8246 if (!ISEQ_COMPILE_DATA(ip)) {
8247 ip = 0;
8248 break;
8249 }
8250
8251 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8252 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8253 /* while loop */
8254 break;
8255 }
8256 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
8257 break;
8258 }
8259 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
8260 COMPILE_ERROR(iseq, location->line, "Invalid next");
8261 return;
8262 }
8263
8264 ip = ISEQ_BODY(ip)->parent_iseq;
8265 }
8266
8267 if (ip != 0) {
8268 if (node->arguments) {
8269 PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
8270 }
8271 else {
8272 PUSH_INSN(ret, *location, putnil);
8273 }
8274
8275 PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_NEXT));
8276 if (popped) PUSH_INSN(ret, *location, pop);
8277 }
8278 else {
8279 COMPILE_ERROR(iseq, location->line, "Invalid next");
8280 }
8281 }
8282}
8283
8284static inline void
8285pm_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)
8286{
8287 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8288 LABEL *splabel = NEW_LABEL(0);
8289
8290 PUSH_LABEL(ret, splabel);
8291 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8292 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8293
8294 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8295 PUSH_ADJUST_RESTORE(ret, splabel);
8296 if (!popped) PUSH_INSN(ret, *location, putnil);
8297 }
8298 else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8299 LABEL *splabel = NEW_LABEL(0);
8300
8301 PUSH_LABEL(ret, splabel);
8302 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8303 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8304
8305 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8306 PUSH_ADJUST_RESTORE(ret, splabel);
8307 if (!popped) PUSH_INSN(ret, *location, putnil);
8308 }
8309 else {
8310 const rb_iseq_t *ip = iseq;
8311
8312 while (ip) {
8313 if (!ISEQ_COMPILE_DATA(ip)) {
8314 ip = 0;
8315 break;
8316 }
8317
8318 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8319 break;
8320 }
8321 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
8322 break;
8323 }
8324 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
8325 COMPILE_ERROR(iseq, location->line, "Invalid redo");
8326 return;
8327 }
8328
8329 ip = ISEQ_BODY(ip)->parent_iseq;
8330 }
8331
8332 if (ip != 0) {
8333 PUSH_INSN(ret, *location, putnil);
8334 PUSH_INSN1(ret, *location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8335 if (popped) PUSH_INSN(ret, *location, pop);
8336 }
8337 else {
8338 COMPILE_ERROR(iseq, location->line, "Invalid redo");
8339 }
8340 }
8341}
8342
8343static inline void
8344pm_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)
8345{
8346 iseq_set_exception_local_table(iseq);
8347
8348 // First, establish the labels that we need to be able to jump to within
8349 // this compilation block.
8350 LABEL *exception_match_label = NEW_LABEL(location->line);
8351 LABEL *rescue_end_label = NEW_LABEL(location->line);
8352
8353 // Next, compile each of the exceptions that we're going to be
8354 // handling. For each one, we'll add instructions to check if the
8355 // exception matches the raised one, and if it does then jump to the
8356 // exception_match_label label. Otherwise it will fall through to the
8357 // subsequent check. If there are no exceptions, we'll only check
8358 // StandardError.
8359 const pm_node_list_t *exceptions = &node->exceptions;
8360
8361 if (exceptions->size > 0) {
8362 for (size_t index = 0; index < exceptions->size; index++) {
8363 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8364 PM_COMPILE(exceptions->nodes[index]);
8365 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8366 if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
8367 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8368 }
8369 PUSH_INSN1(ret, *location, checkmatch, INT2FIX(checkmatch_flags));
8370 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8371 }
8372 }
8373 else {
8374 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8375 PUSH_INSN1(ret, *location, putobject, rb_eStandardError);
8376 PUSH_INSN1(ret, *location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8377 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8378 }
8379
8380 // If none of the exceptions that we are matching against matched, then
8381 // we'll jump straight to the rescue_end_label label.
8382 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8383
8384 // Here we have the exception_match_label, which is where the
8385 // control-flow goes in the case that one of the exceptions matched.
8386 // Here we will compile the instructions to handle the exception.
8387 PUSH_LABEL(ret, exception_match_label);
8388 PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
8389
8390 // If we have a reference to the exception, then we'll compile the write
8391 // into the instruction sequence. This can look quite different
8392 // depending on the kind of write being performed.
8393 if (node->reference) {
8394 DECL_ANCHOR(writes);
8395 DECL_ANCHOR(cleanup);
8396
8397 pm_compile_target_node(iseq, node->reference, ret, writes, cleanup, scope_node, NULL);
8398 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8399
8400 PUSH_SEQ(ret, writes);
8401 PUSH_SEQ(ret, cleanup);
8402 }
8403
8404 // If we have statements to execute, we'll compile them here. Otherwise
8405 // we'll push nil onto the stack.
8406 if (node->statements != NULL) {
8407 // We'll temporarily remove the end_label location from the iseq
8408 // when compiling the statements so that next/redo statements
8409 // inside the body will throw to the correct place instead of
8410 // jumping straight to the end of this iseq
8411 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8412 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8413
8414 PM_COMPILE((const pm_node_t *) node->statements);
8415
8416 // Now restore the end_label
8417 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8418 }
8419 else {
8420 PUSH_INSN(ret, *location, putnil);
8421 }
8422
8423 PUSH_INSN(ret, *location, leave);
8424
8425 // Here we'll insert the rescue_end_label label, which is jumped to if
8426 // none of the exceptions matched. It will cause the control-flow to
8427 // either jump to the next rescue clause or it will fall through to the
8428 // subsequent instruction returning the raised error.
8429 PUSH_LABEL(ret, rescue_end_label);
8430 if (node->subsequent != NULL) {
8431 PM_COMPILE((const pm_node_t *) node->subsequent);
8432 }
8433 else {
8434 PUSH_GETLOCAL(ret, *location, 1, 0);
8435 }
8436}
8437
8438static inline void
8439pm_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)
8440{
8441 const pm_arguments_node_t *arguments = node->arguments;
8442 enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
8443 LABEL *splabel = 0;
8444
8445 const rb_iseq_t *parent_iseq = iseq;
8446 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8447 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8448 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq)) break;
8449 parent_type = ISEQ_BODY(parent_iseq)->type;
8450 }
8451
8452 switch (parent_type) {
8453 case ISEQ_TYPE_TOP:
8454 case ISEQ_TYPE_MAIN:
8455 if (arguments) {
8456 rb_warn("argument of top-level return is ignored");
8457 }
8458 if (parent_iseq == iseq) {
8459 type = ISEQ_TYPE_METHOD;
8460 }
8461 break;
8462 default:
8463 break;
8464 }
8465
8466 if (type == ISEQ_TYPE_METHOD) {
8467 splabel = NEW_LABEL(0);
8468 PUSH_LABEL(ret, splabel);
8469 PUSH_ADJUST(ret, *location, 0);
8470 }
8471
8472 if (arguments != NULL) {
8473 PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
8474 }
8475 else {
8476 PUSH_INSN(ret, *location, putnil);
8477 }
8478
8479 if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8480 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8481 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
8482 PUSH_INSN(ret, *location, leave);
8483 PUSH_ADJUST_RESTORE(ret, splabel);
8484 if (!popped) PUSH_INSN(ret, *location, putnil);
8485 }
8486 else {
8487 PUSH_INSN1(ret, *location, throw, INT2FIX(TAG_RETURN));
8488 if (popped) PUSH_INSN(ret, *location, pop);
8489 }
8490}
8491
8492static inline void
8493pm_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)
8494{
8495 DECL_ANCHOR(args);
8496
8497 LABEL *retry_label = NEW_LABEL(location->line);
8498 LABEL *retry_end_l = NEW_LABEL(location->line);
8499
8500 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8501 const rb_iseq_t *current_block;
8502 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8503
8504 PUSH_LABEL(ret, retry_label);
8505 PUSH_INSN(ret, *location, putself);
8506
8507 int flags = 0;
8508 struct rb_callinfo_kwarg *keywords = NULL;
8509 int argc = pm_setup_args(node->arguments, node->block, &flags, &keywords, iseq, ret, scope_node, location);
8510 bool is_forwardable = (node->arguments != NULL) && PM_NODE_FLAG_P(node->arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8511 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8512
8513 if (node->block && PM_NODE_TYPE_P(node->block, PM_BLOCK_NODE)) {
8514 pm_scope_node_t next_scope_node;
8515 pm_scope_node_init(node->block, &next_scope_node, scope_node);
8516
8517 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
8518 pm_scope_node_destroy(&next_scope_node);
8519 }
8520
8521 if (!node->block) {
8522 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8523 }
8524
8525 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8526 PUSH_INSN(args, *location, splatkw);
8527 }
8528
8529 PUSH_SEQ(ret, args);
8530 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
8531 flags |= VM_CALL_FORWARDING;
8532
8533 {
8534 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8535 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8536 }
8537 }
8538 else {
8539 {
8540 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8541 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8542 }
8543
8544 }
8545
8546 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8547
8548 if (popped) PUSH_INSN(ret, *location, pop);
8549 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8550 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8551}
8552
8553static inline void
8554pm_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)
8555{
8556 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
8557 case ISEQ_TYPE_TOP:
8558 case ISEQ_TYPE_MAIN:
8559 case ISEQ_TYPE_CLASS:
8560 COMPILE_ERROR(iseq, location->line, "Invalid yield");
8561 return;
8562 default: /* valid */;
8563 }
8564
8565 int argc = 0;
8566 int flags = 0;
8567 struct rb_callinfo_kwarg *keywords = NULL;
8568
8569 if (node->arguments) {
8570 argc = pm_setup_args(node->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8571 }
8572
8573 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8574 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8575
8576 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8577 if (popped) PUSH_INSN(ret, *location, pop);
8578
8579 int level = 0;
8580 for (const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8581 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8582 }
8583
8584 if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
8585}
8586
8597static void
8598pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
8599{
8600 const pm_parser_t *parser = scope_node->parser;
8601 const pm_node_location_t location = PM_NODE_START_LOCATION(parser, node);
8602 int lineno = (int) location.line;
8603
8604 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)) {
8605 // If this node is a begin node and it has empty statements and also
8606 // has a rescue clause, then the other parser considers it as
8607 // starting on the same line as the rescue, as opposed to the
8608 // location of the begin keyword. We replicate that behavior here.
8609 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((const pm_begin_node_t *) node)->rescue_clause).line;
8610 }
8611
8612 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8613 // If this node has the newline flag set and it is on a new line
8614 // from the previous nodes that have been compiled for this ISEQ,
8615 // then we need to emit a newline event.
8616 int event = RUBY_EVENT_LINE;
8617
8618 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8619 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8620 event |= RUBY_EVENT_COVERAGE_LINE;
8621 }
8622 PUSH_TRACE(ret, event);
8623 }
8624
8625 switch (PM_NODE_TYPE(node)) {
8626 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8627 // alias $foo $bar
8628 // ^^^^^^^^^^^^^^^
8629 pm_compile_alias_global_variable_node(iseq, (const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
8630 return;
8631 case PM_ALIAS_METHOD_NODE:
8632 // alias foo bar
8633 // ^^^^^^^^^^^^^
8634 pm_compile_alias_method_node(iseq, (const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8635 return;
8636 case PM_AND_NODE:
8637 // a and b
8638 // ^^^^^^^
8639 pm_compile_and_node(iseq, (const pm_and_node_t *) node, &location, ret, popped, scope_node);
8640 return;
8641 case PM_ARGUMENTS_NODE: {
8642 // break foo
8643 // ^^^
8644 //
8645 // These are ArgumentsNodes that are not compiled directly by their
8646 // parent call nodes, used in the cases of NextNodes, ReturnNodes, and
8647 // BreakNodes. They can create an array like ArrayNode.
8648 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
8649 const pm_node_list_t *elements = &cast->arguments;
8650
8651 if (elements->size == 1) {
8652 // If we are only returning a single element through one of the jump
8653 // nodes, then we will only compile that node directly.
8654 PM_COMPILE(elements->nodes[0]);
8655 }
8656 else {
8657 pm_compile_array_node(iseq, (const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8658 }
8659 return;
8660 }
8661 case PM_ARRAY_NODE: {
8662 // [foo, bar, baz]
8663 // ^^^^^^^^^^^^^^^
8664 const pm_array_node_t *cast = (const pm_array_node_t *) node;
8665 pm_compile_array_node(iseq, (const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8666 return;
8667 }
8668 case PM_ASSOC_NODE: {
8669 // { foo: 1 }
8670 // ^^^^^^
8671 //
8672 // foo(bar: 1)
8673 // ^^^^^^
8674 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
8675
8676 PM_COMPILE(cast->key);
8677 PM_COMPILE(cast->value);
8678
8679 return;
8680 }
8681 case PM_ASSOC_SPLAT_NODE: {
8682 // { **foo }
8683 // ^^^^^
8684 //
8685 // def foo(**); bar(**); end
8686 // ^^
8687 const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
8688
8689 if (cast->value != NULL) {
8690 PM_COMPILE(cast->value);
8691 }
8692 else if (!popped) {
8693 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8694 PUSH_GETLOCAL(ret, location, index.index, index.level);
8695 }
8696
8697 return;
8698 }
8699 case PM_BACK_REFERENCE_READ_NODE: {
8700 // $+
8701 // ^^
8702 if (!popped) {
8704 VALUE backref = pm_compile_back_reference_ref(cast);
8705
8706 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref);
8707 }
8708 return;
8709 }
8710 case PM_BEGIN_NODE: {
8711 // begin end
8712 // ^^^^^^^^^
8713 const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
8714
8715 if (cast->ensure_clause) {
8716 // Compiling the ensure clause will compile the rescue clause (if
8717 // there is one), which will compile the begin statements.
8718 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8719 }
8720 else if (cast->rescue_clause) {
8721 // Compiling rescue will compile begin statements (if applicable).
8722 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8723 }
8724 else {
8725 // If there is neither ensure or rescue, the just compile the
8726 // statements.
8727 if (cast->statements != NULL) {
8728 PM_COMPILE((const pm_node_t *) cast->statements);
8729 }
8730 else if (!popped) {
8731 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8732 }
8733 }
8734 return;
8735 }
8736 case PM_BLOCK_ARGUMENT_NODE: {
8737 // foo(&bar)
8738 // ^^^^
8739 const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
8740
8741 if (cast->expression != NULL) {
8742 PM_COMPILE(cast->expression);
8743 }
8744 else {
8745 // If there's no expression, this must be block forwarding.
8746 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8747 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
8748 }
8749 return;
8750 }
8751 case PM_BREAK_NODE:
8752 // break
8753 // ^^^^^
8754 //
8755 // break foo
8756 // ^^^^^^^^^
8757 pm_compile_break_node(iseq, (const pm_break_node_t *) node, &location, ret, popped, scope_node);
8758 return;
8759 case PM_CALL_NODE:
8760 // foo
8761 // ^^^
8762 //
8763 // foo.bar
8764 // ^^^^^^^
8765 //
8766 // foo.bar() {}
8767 // ^^^^^^^^^^^^
8768 pm_compile_call_node(iseq, (const pm_call_node_t *) node, ret, popped, scope_node);
8769 return;
8770 case PM_CALL_AND_WRITE_NODE: {
8771 // foo.bar &&= baz
8772 // ^^^^^^^^^^^^^^^
8773 const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
8774 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);
8775 return;
8776 }
8777 case PM_CALL_OR_WRITE_NODE: {
8778 // foo.bar ||= baz
8779 // ^^^^^^^^^^^^^^^
8780 const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
8781 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);
8782 return;
8783 }
8784 case PM_CALL_OPERATOR_WRITE_NODE:
8785 // foo.bar += baz
8786 // ^^^^^^^^^^^^^^^
8787 //
8788 // Call operator writes occur when you have a call node on the left-hand
8789 // side of a write operator that is not `=`. As an example,
8790 // `foo.bar *= 1`. This breaks down to caching the receiver on the
8791 // stack and then performing three method calls, one to read the value,
8792 // one to compute the result, and one to write the result back to the
8793 // receiver.
8794 pm_compile_call_operator_write_node(iseq, (const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
8795 return;
8796 case PM_CASE_NODE:
8797 // case foo; when bar; end
8798 // ^^^^^^^^^^^^^^^^^^^^^^^
8799 pm_compile_case_node(iseq, (const pm_case_node_t *) node, &location, ret, popped, scope_node);
8800 return;
8801 case PM_CASE_MATCH_NODE:
8802 // case foo; in bar; end
8803 // ^^^^^^^^^^^^^^^^^^^^^
8804 //
8805 // If you use the `case` keyword to create a case match node, it will
8806 // match against all of the `in` clauses until it finds one that
8807 // matches. If it doesn't find one, it can optionally fall back to an
8808 // `else` clause. If none is present and a match wasn't found, it will
8809 // raise an appropriate error.
8810 pm_compile_case_match_node(iseq, (const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8811 return;
8812 case PM_CLASS_NODE: {
8813 // class Foo; end
8814 // ^^^^^^^^^^^^^^
8815 const pm_class_node_t *cast = (const pm_class_node_t *) node;
8816
8817 ID class_id = pm_constant_id_lookup(scope_node, cast->name);
8818 VALUE class_name = rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(class_id)));
8819
8820 pm_scope_node_t next_scope_node;
8821 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
8822
8823 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.line);
8824 pm_scope_node_destroy(&next_scope_node);
8825
8826 // TODO: Once we merge constant path nodes correctly, fix this flag
8827 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8828 (cast->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8829 pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
8830
8831 if (cast->superclass) {
8832 PM_COMPILE_NOT_POPPED(cast->superclass);
8833 }
8834 else {
8835 PUSH_INSN(ret, location, putnil);
8836 }
8837
8838 {
8839 VALUE operand = ID2SYM(class_id);
8840 PUSH_INSN3(ret, location, defineclass, operand, class_iseq, INT2FIX(flags));
8841 }
8842 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
8843
8844 if (popped) PUSH_INSN(ret, location, pop);
8845 return;
8846 }
8847 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
8848 // @@foo &&= bar
8849 // ^^^^^^^^^^^^^
8851 LABEL *end_label = NEW_LABEL(location.line);
8852
8853 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8854 VALUE name = ID2SYM(name_id);
8855
8856 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8857 if (!popped) PUSH_INSN(ret, location, dup);
8858
8859 PUSH_INSNL(ret, location, branchunless, end_label);
8860 if (!popped) PUSH_INSN(ret, location, pop);
8861
8862 PM_COMPILE_NOT_POPPED(cast->value);
8863 if (!popped) PUSH_INSN(ret, location, dup);
8864
8865 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8866 PUSH_LABEL(ret, end_label);
8867
8868 return;
8869 }
8870 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
8871 // @@foo += bar
8872 // ^^^^^^^^^^^^
8874
8875 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8876 VALUE name = ID2SYM(name_id);
8877
8878 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8879 PM_COMPILE_NOT_POPPED(cast->value);
8880
8881 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
8882 int flags = VM_CALL_ARGS_SIMPLE;
8883 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
8884
8885 if (!popped) PUSH_INSN(ret, location, dup);
8886 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8887
8888 return;
8889 }
8890 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
8891 // @@foo ||= bar
8892 // ^^^^^^^^^^^^^
8894 LABEL *end_label = NEW_LABEL(location.line);
8895 LABEL *start_label = NEW_LABEL(location.line);
8896
8897 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
8898 VALUE name = ID2SYM(name_id);
8899
8900 PUSH_INSN(ret, location, putnil);
8901 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, Qtrue);
8902 PUSH_INSNL(ret, location, branchunless, start_label);
8903
8904 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8905 if (!popped) PUSH_INSN(ret, location, dup);
8906
8907 PUSH_INSNL(ret, location, branchif, end_label);
8908 if (!popped) PUSH_INSN(ret, location, pop);
8909
8910 PUSH_LABEL(ret, start_label);
8911 PM_COMPILE_NOT_POPPED(cast->value);
8912 if (!popped) PUSH_INSN(ret, location, dup);
8913
8914 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8915 PUSH_LABEL(ret, end_label);
8916
8917 return;
8918 }
8919 case PM_CLASS_VARIABLE_READ_NODE: {
8920 // @@foo
8921 // ^^^^^
8922 if (!popped) {
8924 ID name = pm_constant_id_lookup(scope_node, cast->name);
8925 PUSH_INSN2(ret, location, getclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
8926 }
8927 return;
8928 }
8929 case PM_CLASS_VARIABLE_WRITE_NODE: {
8930 // @@foo = 1
8931 // ^^^^^^^^^
8933 PM_COMPILE_NOT_POPPED(cast->value);
8934 if (!popped) PUSH_INSN(ret, location, dup);
8935
8936 ID name = pm_constant_id_lookup(scope_node, cast->name);
8937 PUSH_INSN2(ret, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
8938
8939 return;
8940 }
8941 case PM_CONSTANT_PATH_NODE: {
8942 // Foo::Bar
8943 // ^^^^^^^^
8944 VALUE parts;
8945
8946 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
8947 ISEQ_BODY(iseq)->ic_size++;
8948 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
8949 }
8950 else {
8951 DECL_ANCHOR(prefix);
8952 DECL_ANCHOR(body);
8953
8954 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
8955 if (LIST_INSN_SIZE_ZERO(prefix)) {
8956 PUSH_INSN(ret, location, putnil);
8957 }
8958 else {
8959 PUSH_SEQ(ret, prefix);
8960 }
8961
8962 PUSH_SEQ(ret, body);
8963 }
8964
8965 if (popped) PUSH_INSN(ret, location, pop);
8966 return;
8967 }
8968 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
8969 // Foo::Bar &&= baz
8970 // ^^^^^^^^^^^^^^^^
8972 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8973 return;
8974 }
8975 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
8976 // Foo::Bar ||= baz
8977 // ^^^^^^^^^^^^^^^^
8979 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8980 return;
8981 }
8982 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
8983 // Foo::Bar += baz
8984 // ^^^^^^^^^^^^^^^
8986 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8987 return;
8988 }
8989 case PM_CONSTANT_PATH_WRITE_NODE: {
8990 // Foo::Bar = 1
8991 // ^^^^^^^^^^^^
8993 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8994 return;
8995 }
8996 case PM_CONSTANT_READ_NODE: {
8997 // Foo
8998 // ^^^
8999 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
9000 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
9001
9002 pm_compile_constant_read(iseq, name, &cast->base.location, location.node_id, ret, scope_node);
9003 if (popped) PUSH_INSN(ret, location, pop);
9004
9005 return;
9006 }
9007 case PM_CONSTANT_AND_WRITE_NODE: {
9008 // Foo &&= bar
9009 // ^^^^^^^^^^^
9011 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9012 return;
9013 }
9014 case PM_CONSTANT_OR_WRITE_NODE: {
9015 // Foo ||= bar
9016 // ^^^^^^^^^^^
9017 const pm_constant_or_write_node_t *cast = (const pm_constant_or_write_node_t *) node;
9018 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9019 return;
9020 }
9021 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9022 // Foo += bar
9023 // ^^^^^^^^^^
9025 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9026 return;
9027 }
9028 case PM_CONSTANT_WRITE_NODE: {
9029 // Foo = 1
9030 // ^^^^^^^
9031 const pm_constant_write_node_t *cast = (const pm_constant_write_node_t *) node;
9032 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9033 return;
9034 }
9035 case PM_DEF_NODE: {
9036 // def foo; end
9037 // ^^^^^^^^^^^^
9038 //
9039 // def self.foo; end
9040 // ^^^^^^^^^^^^^^^^^
9041 const pm_def_node_t *cast = (const pm_def_node_t *) node;
9042 ID method_name = pm_constant_id_lookup(scope_node, cast->name);
9043
9044 pm_scope_node_t next_scope_node;
9045 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
9046
9047 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.line);
9048 pm_scope_node_destroy(&next_scope_node);
9049
9050 if (cast->receiver) {
9051 PM_COMPILE_NOT_POPPED(cast->receiver);
9052 PUSH_INSN2(ret, location, definesmethod, ID2SYM(method_name), method_iseq);
9053 }
9054 else {
9055 PUSH_INSN2(ret, location, definemethod, ID2SYM(method_name), method_iseq);
9056 }
9057 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) method_iseq);
9058
9059 if (!popped) {
9060 PUSH_INSN1(ret, location, putobject, ID2SYM(method_name));
9061 }
9062
9063 return;
9064 }
9065 case PM_DEFINED_NODE: {
9066 // defined?(a)
9067 // ^^^^^^^^^^^
9068 const pm_defined_node_t *cast = (const pm_defined_node_t *) node;
9069 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, false);
9070 return;
9071 }
9072 case PM_EMBEDDED_STATEMENTS_NODE: {
9073 // "foo #{bar}"
9074 // ^^^^^^
9076
9077 if (cast->statements != NULL) {
9078 PM_COMPILE((const pm_node_t *) (cast->statements));
9079 }
9080 else {
9081 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9082 }
9083
9084 if (popped) PUSH_INSN(ret, location, pop);
9085 return;
9086 }
9087 case PM_EMBEDDED_VARIABLE_NODE: {
9088 // "foo #@bar"
9089 // ^^^^^
9090 const pm_embedded_variable_node_t *cast = (const pm_embedded_variable_node_t *) node;
9091 PM_COMPILE(cast->variable);
9092 return;
9093 }
9094 case PM_FALSE_NODE: {
9095 // false
9096 // ^^^^^
9097 if (!popped) {
9098 PUSH_INSN1(ret, location, putobject, Qfalse);
9099 }
9100 return;
9101 }
9102 case PM_ENSURE_NODE: {
9103 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
9104
9105 if (cast->statements != NULL) {
9106 PM_COMPILE((const pm_node_t *) cast->statements);
9107 }
9108
9109 return;
9110 }
9111 case PM_ELSE_NODE: {
9112 // if foo then bar else baz end
9113 // ^^^^^^^^^^^^
9114 const pm_else_node_t *cast = (const pm_else_node_t *) node;
9115
9116 if (cast->statements != NULL) {
9117 PM_COMPILE((const pm_node_t *) cast->statements);
9118 }
9119 else if (!popped) {
9120 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9121 }
9122
9123 return;
9124 }
9125 case PM_FLIP_FLOP_NODE: {
9126 // if foo .. bar; end
9127 // ^^^^^^^^^^
9128 const pm_flip_flop_node_t *cast = (const pm_flip_flop_node_t *) node;
9129
9130 LABEL *final_label = NEW_LABEL(location.line);
9131 LABEL *then_label = NEW_LABEL(location.line);
9132 LABEL *else_label = NEW_LABEL(location.line);
9133
9134 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.line, ret, popped, scope_node);
9135
9136 PUSH_LABEL(ret, then_label);
9137 PUSH_INSN1(ret, location, putobject, Qtrue);
9138 PUSH_INSNL(ret, location, jump, final_label);
9139 PUSH_LABEL(ret, else_label);
9140 PUSH_INSN1(ret, location, putobject, Qfalse);
9141 PUSH_LABEL(ret, final_label);
9142
9143 return;
9144 }
9145 case PM_FLOAT_NODE: {
9146 // 1.0
9147 // ^^^
9148 if (!popped) {
9149 VALUE operand = parse_float((const pm_float_node_t *) node);
9150 PUSH_INSN1(ret, location, putobject, operand);
9151 }
9152 return;
9153 }
9154 case PM_FOR_NODE: {
9155 // for foo in bar do end
9156 // ^^^^^^^^^^^^^^^^^^^^^
9157 const pm_for_node_t *cast = (const pm_for_node_t *) node;
9158
9159 LABEL *retry_label = NEW_LABEL(location.line);
9160 LABEL *retry_end_l = NEW_LABEL(location.line);
9161
9162 // First, compile the collection that we're going to be iterating over.
9163 PUSH_LABEL(ret, retry_label);
9164 PM_COMPILE_NOT_POPPED(cast->collection);
9165
9166 // Next, create the new scope that is going to contain the block that
9167 // will be passed to the each method.
9168 pm_scope_node_t next_scope_node;
9169 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
9170
9171 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
9172 pm_scope_node_destroy(&next_scope_node);
9173
9174 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9175 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9176
9177 // Now, create the method call to each that will be used to iterate over
9178 // the collection, and pass the newly created iseq as the block.
9179 PUSH_SEND_WITH_BLOCK(ret, location, idEach, INT2FIX(0), child_iseq);
9180 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9181
9182 if (popped) PUSH_INSN(ret, location, pop);
9183 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9184 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9185 return;
9186 }
9187 case PM_FORWARDING_ARGUMENTS_NODE:
9188 rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
9189 return;
9190 case PM_FORWARDING_SUPER_NODE:
9191 // super
9192 // ^^^^^
9193 //
9194 // super {}
9195 // ^^^^^^^^
9196 pm_compile_forwarding_super_node(iseq, (const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
9197 return;
9198 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9199 // $foo &&= bar
9200 // ^^^^^^^^^^^^
9202 LABEL *end_label = NEW_LABEL(location.line);
9203
9204 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
9205 PUSH_INSN1(ret, location, getglobal, name);
9206 if (!popped) PUSH_INSN(ret, location, dup);
9207
9208 PUSH_INSNL(ret, location, branchunless, end_label);
9209 if (!popped) PUSH_INSN(ret, location, pop);
9210
9211 PM_COMPILE_NOT_POPPED(cast->value);
9212 if (!popped) PUSH_INSN(ret, location, dup);
9213
9214 PUSH_INSN1(ret, location, setglobal, name);
9215 PUSH_LABEL(ret, end_label);
9216
9217 return;
9218 }
9219 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9220 // $foo += bar
9221 // ^^^^^^^^^^^
9223
9224 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
9225 PUSH_INSN1(ret, location, getglobal, name);
9226 PM_COMPILE_NOT_POPPED(cast->value);
9227
9228 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
9229 int flags = VM_CALL_ARGS_SIMPLE;
9230 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
9231
9232 if (!popped) PUSH_INSN(ret, location, dup);
9233 PUSH_INSN1(ret, location, setglobal, name);
9234
9235 return;
9236 }
9237 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9238 // $foo ||= bar
9239 // ^^^^^^^^^^^^
9241 LABEL *set_label = NEW_LABEL(location.line);
9242 LABEL *end_label = NEW_LABEL(location.line);
9243
9244 PUSH_INSN(ret, location, putnil);
9245 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
9246
9247 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, Qtrue);
9248 PUSH_INSNL(ret, location, branchunless, set_label);
9249
9250 PUSH_INSN1(ret, location, getglobal, name);
9251 if (!popped) PUSH_INSN(ret, location, dup);
9252
9253 PUSH_INSNL(ret, location, branchif, end_label);
9254 if (!popped) PUSH_INSN(ret, location, pop);
9255
9256 PUSH_LABEL(ret, set_label);
9257 PM_COMPILE_NOT_POPPED(cast->value);
9258 if (!popped) PUSH_INSN(ret, location, dup);
9259
9260 PUSH_INSN1(ret, location, setglobal, name);
9261 PUSH_LABEL(ret, end_label);
9262
9263 return;
9264 }
9265 case PM_GLOBAL_VARIABLE_READ_NODE: {
9266 // $foo
9267 // ^^^^
9269 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
9270
9271 PUSH_INSN1(ret, location, getglobal, name);
9272 if (popped) PUSH_INSN(ret, location, pop);
9273
9274 return;
9275 }
9276 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9277 // $foo = 1
9278 // ^^^^^^^^
9280 PM_COMPILE_NOT_POPPED(cast->value);
9281 if (!popped) PUSH_INSN(ret, location, dup);
9282
9283 ID name = pm_constant_id_lookup(scope_node, cast->name);
9284 PUSH_INSN1(ret, location, setglobal, ID2SYM(name));
9285
9286 return;
9287 }
9288 case PM_HASH_NODE: {
9289 // {}
9290 // ^^
9291 //
9292 // If every node in the hash is static, then we can compile the entire
9293 // hash now instead of later.
9294 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9295 // We're only going to compile this node if it's not popped. If it
9296 // is popped, then we know we don't need to do anything since it's
9297 // statically known.
9298 if (!popped) {
9299 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
9300
9301 if (cast->elements.size == 0) {
9302 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
9303 }
9304 else {
9305 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9306 PUSH_INSN1(ret, location, duphash, value);
9307 RB_OBJ_WRITTEN(iseq, Qundef, value);
9308 }
9309 }
9310 }
9311 else {
9312 // Here since we know there are possible side-effects inside the
9313 // hash contents, we're going to build it entirely at runtime. We'll
9314 // do this by pushing all of the key-value pairs onto the stack and
9315 // then combining them with newhash.
9316 //
9317 // If this hash is popped, then this serves only to ensure we enact
9318 // all side-effects (like method calls) that are contained within
9319 // the hash contents.
9320 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
9321 const pm_node_list_t *elements = &cast->elements;
9322
9323 if (popped) {
9324 // If this hash is popped, then we can iterate through each
9325 // element and compile it. The result of each compilation will
9326 // only include the side effects of the element itself.
9327 for (size_t index = 0; index < elements->size; index++) {
9328 PM_COMPILE_POPPED(elements->nodes[index]);
9329 }
9330 }
9331 else {
9332 pm_compile_hash_elements(iseq, node, elements, 0, Qundef, false, ret, scope_node);
9333 }
9334 }
9335
9336 return;
9337 }
9338 case PM_IF_NODE: {
9339 // if foo then bar end
9340 // ^^^^^^^^^^^^^^^^^^^
9341 //
9342 // bar if foo
9343 // ^^^^^^^^^^
9344 //
9345 // foo ? bar : baz
9346 // ^^^^^^^^^^^^^^^
9347 const pm_if_node_t *cast = (const pm_if_node_t *) node;
9348 pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
9349 return;
9350 }
9351 case PM_IMAGINARY_NODE: {
9352 // 1i
9353 // ^^
9354 if (!popped) {
9355 VALUE operand = parse_imaginary((const pm_imaginary_node_t *) node);
9356 PUSH_INSN1(ret, location, putobject, operand);
9357 }
9358 return;
9359 }
9360 case PM_IMPLICIT_NODE: {
9361 // Implicit nodes mark places in the syntax tree where explicit syntax
9362 // was omitted, but implied. For example,
9363 //
9364 // { foo: }
9365 //
9366 // In this case a method call/local variable read is implied by virtue
9367 // of the missing value. To compile these nodes, we simply compile the
9368 // value that is implied, which is helpfully supplied by the parser.
9369 const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
9370 PM_COMPILE(cast->value);
9371 return;
9372 }
9373 case PM_IN_NODE: {
9374 // In nodes are handled by the case match node directly, so we should
9375 // never end up hitting them through this path.
9376 rb_bug("Should not ever enter an in node directly");
9377 return;
9378 }
9379 case PM_INDEX_OPERATOR_WRITE_NODE: {
9380 // foo[bar] += baz
9381 // ^^^^^^^^^^^^^^^
9383 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9384 return;
9385 }
9386 case PM_INDEX_AND_WRITE_NODE: {
9387 // foo[bar] &&= baz
9388 // ^^^^^^^^^^^^^^^^
9389 const pm_index_and_write_node_t *cast = (const pm_index_and_write_node_t *) node;
9390 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
9391 return;
9392 }
9393 case PM_INDEX_OR_WRITE_NODE: {
9394 // foo[bar] ||= baz
9395 // ^^^^^^^^^^^^^^^^
9396 const pm_index_or_write_node_t *cast = (const pm_index_or_write_node_t *) node;
9397 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
9398 return;
9399 }
9400 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9401 // @foo &&= bar
9402 // ^^^^^^^^^^^^
9404 LABEL *end_label = NEW_LABEL(location.line);
9405
9406 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9407 VALUE name = ID2SYM(name_id);
9408
9409 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9410 if (!popped) PUSH_INSN(ret, location, dup);
9411
9412 PUSH_INSNL(ret, location, branchunless, end_label);
9413 if (!popped) PUSH_INSN(ret, location, pop);
9414
9415 PM_COMPILE_NOT_POPPED(cast->value);
9416 if (!popped) PUSH_INSN(ret, location, dup);
9417
9418 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9419 PUSH_LABEL(ret, end_label);
9420
9421 return;
9422 }
9423 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9424 // @foo += bar
9425 // ^^^^^^^^^^^
9427
9428 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9429 VALUE name = ID2SYM(name_id);
9430
9431 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9432 PM_COMPILE_NOT_POPPED(cast->value);
9433
9434 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
9435 int flags = VM_CALL_ARGS_SIMPLE;
9436 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
9437
9438 if (!popped) PUSH_INSN(ret, location, dup);
9439 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9440
9441 return;
9442 }
9443 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9444 // @foo ||= bar
9445 // ^^^^^^^^^^^^
9447 LABEL *end_label = NEW_LABEL(location.line);
9448
9449 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
9450 VALUE name = ID2SYM(name_id);
9451
9452 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9453 if (!popped) PUSH_INSN(ret, location, dup);
9454
9455 PUSH_INSNL(ret, location, branchif, end_label);
9456 if (!popped) PUSH_INSN(ret, location, pop);
9457
9458 PM_COMPILE_NOT_POPPED(cast->value);
9459 if (!popped) PUSH_INSN(ret, location, dup);
9460
9461 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9462 PUSH_LABEL(ret, end_label);
9463
9464 return;
9465 }
9466 case PM_INSTANCE_VARIABLE_READ_NODE: {
9467 // @foo
9468 // ^^^^
9469 if (!popped) {
9471 ID name = pm_constant_id_lookup(scope_node, cast->name);
9472 PUSH_INSN2(ret, location, getinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
9473 }
9474 return;
9475 }
9476 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9477 // @foo = 1
9478 // ^^^^^^^^
9480 PM_COMPILE_NOT_POPPED(cast->value);
9481 if (!popped) PUSH_INSN(ret, location, dup);
9482
9483 ID name = pm_constant_id_lookup(scope_node, cast->name);
9484 PUSH_INSN2(ret, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
9485
9486 return;
9487 }
9488 case PM_INTEGER_NODE: {
9489 // 1
9490 // ^
9491 if (!popped) {
9492 VALUE operand = parse_integer((const pm_integer_node_t *) node);
9493 PUSH_INSN1(ret, location, putobject, operand);
9494 }
9495 return;
9496 }
9497 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9498 // if /foo #{bar}/ then end
9499 // ^^^^^^^^^^^^
9500 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9501 if (!popped) {
9502 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9503 PUSH_INSN1(ret, location, putobject, regexp);
9504 }
9505 }
9506 else {
9507 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
9508 }
9509
9510 PUSH_INSN1(ret, location, getglobal, rb_id2sym(idLASTLINE));
9511 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
9512 if (popped) PUSH_INSN(ret, location, pop);
9513
9514 return;
9515 }
9516 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9517 // /foo #{bar}/
9518 // ^^^^^^^^^^^^
9519 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9520 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9521 const rb_iseq_t *block_iseq = NULL;
9522 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9523
9524 pm_scope_node_t next_scope_node;
9525 pm_scope_node_init(node, &next_scope_node, scope_node);
9526
9527 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.line);
9528 pm_scope_node_destroy(&next_scope_node);
9529
9530 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9531 PUSH_INSN2(ret, location, once, block_iseq, INT2FIX(ise_index));
9532 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9533
9534 if (popped) PUSH_INSN(ret, location, pop);
9535 return;
9536 }
9537
9538 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9539 if (!popped) {
9540 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9541 PUSH_INSN1(ret, location, putobject, regexp);
9542 }
9543 }
9544 else {
9545 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
9546 if (popped) PUSH_INSN(ret, location, pop);
9547 }
9548
9549 return;
9550 }
9551 case PM_INTERPOLATED_STRING_NODE: {
9552 // "foo #{bar}"
9553 // ^^^^^^^^^^^^
9554 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9555 if (!popped) {
9556 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9557
9558 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9559 PUSH_INSN1(ret, location, putobject, string);
9560 }
9561 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9562 PUSH_INSN1(ret, location, putstring, string);
9563 }
9564 else {
9565 PUSH_INSN1(ret, location, putchilledstring, string);
9566 }
9567 }
9568 }
9569 else {
9571 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, !PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN));
9572 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9573 if (popped) PUSH_INSN(ret, location, pop);
9574 }
9575
9576 return;
9577 }
9578 case PM_INTERPOLATED_SYMBOL_NODE: {
9579 // :"foo #{bar}"
9580 // ^^^^^^^^^^^^^
9582 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, false);
9583
9584 if (length > 1) {
9585 PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9586 }
9587
9588 if (!popped) {
9589 PUSH_INSN(ret, location, intern);
9590 }
9591 else {
9592 PUSH_INSN(ret, location, pop);
9593 }
9594
9595 return;
9596 }
9597 case PM_INTERPOLATED_X_STRING_NODE: {
9598 // `foo #{bar}`
9599 // ^^^^^^^^^^^^
9601
9602 PUSH_INSN(ret, location, putself);
9603
9604 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL, false);
9605 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
9606
9607 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9608 if (popped) PUSH_INSN(ret, location, pop);
9609
9610 return;
9611 }
9612 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9613 // -> { it }
9614 // ^^
9615 if (!popped) {
9616 pm_scope_node_t *current_scope_node = scope_node;
9617 int level = 0;
9618
9619 while (current_scope_node) {
9620 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9621 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9622 return;
9623 }
9624
9625 current_scope_node = current_scope_node->previous;
9626 level++;
9627 }
9628 rb_bug("Local `it` does not exist");
9629 }
9630
9631 return;
9632 }
9633 case PM_KEYWORD_HASH_NODE: {
9634 // foo(bar: baz)
9635 // ^^^^^^^^
9636 const pm_keyword_hash_node_t *cast = (const pm_keyword_hash_node_t *) node;
9637 const pm_node_list_t *elements = &cast->elements;
9638
9639 const pm_node_t *element;
9640 PM_NODE_LIST_FOREACH(elements, index, element) {
9641 PM_COMPILE(element);
9642 }
9643
9644 if (!popped) PUSH_INSN1(ret, location, newhash, INT2FIX(elements->size * 2));
9645 return;
9646 }
9647 case PM_LAMBDA_NODE: {
9648 // -> {}
9649 // ^^^^^
9650 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
9651
9652 pm_scope_node_t next_scope_node;
9653 pm_scope_node_init(node, &next_scope_node, scope_node);
9654
9655 int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
9656 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9657 pm_scope_node_destroy(&next_scope_node);
9658
9659 VALUE argc = INT2FIX(0);
9660 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9661 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9662 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
9663
9664 if (popped) PUSH_INSN(ret, location, pop);
9665 return;
9666 }
9667 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9668 // foo &&= bar
9669 // ^^^^^^^^^^^
9671 LABEL *end_label = NEW_LABEL(location.line);
9672
9673 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9674 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9675 if (!popped) PUSH_INSN(ret, location, dup);
9676
9677 PUSH_INSNL(ret, location, branchunless, end_label);
9678 if (!popped) PUSH_INSN(ret, location, pop);
9679
9680 PM_COMPILE_NOT_POPPED(cast->value);
9681 if (!popped) PUSH_INSN(ret, location, dup);
9682
9683 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9684 PUSH_LABEL(ret, end_label);
9685
9686 return;
9687 }
9688 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9689 // foo += bar
9690 // ^^^^^^^^^^
9692
9693 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9694 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9695
9696 PM_COMPILE_NOT_POPPED(cast->value);
9697
9698 ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
9699 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
9700
9701 if (!popped) PUSH_INSN(ret, location, dup);
9702 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9703
9704 return;
9705 }
9706 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9707 // foo ||= bar
9708 // ^^^^^^^^^^^
9710
9711 LABEL *set_label = NEW_LABEL(location.line);
9712 LABEL *end_label = NEW_LABEL(location.line);
9713
9714 PUSH_INSN1(ret, location, putobject, Qtrue);
9715 PUSH_INSNL(ret, location, branchunless, set_label);
9716
9717 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9718 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9719 if (!popped) PUSH_INSN(ret, location, dup);
9720
9721 PUSH_INSNL(ret, location, branchif, end_label);
9722 if (!popped) PUSH_INSN(ret, location, pop);
9723
9724 PUSH_LABEL(ret, set_label);
9725 PM_COMPILE_NOT_POPPED(cast->value);
9726 if (!popped) PUSH_INSN(ret, location, dup);
9727
9728 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9729 PUSH_LABEL(ret, end_label);
9730
9731 return;
9732 }
9733 case PM_LOCAL_VARIABLE_READ_NODE: {
9734 // foo
9735 // ^^^
9736 if (!popped) {
9738 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9739 PUSH_GETLOCAL(ret, location, index.index, index.level);
9740 }
9741
9742 return;
9743 }
9744 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9745 // foo = 1
9746 // ^^^^^^^
9748 PM_COMPILE_NOT_POPPED(cast->value);
9749 if (!popped) PUSH_INSN(ret, location, dup);
9750
9751 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
9752 PUSH_SETLOCAL(ret, location, index.index, index.level);
9753 return;
9754 }
9755 case PM_MATCH_LAST_LINE_NODE: {
9756 // if /foo/ then end
9757 // ^^^^^
9758 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9759
9760 PUSH_INSN1(ret, location, putobject, regexp);
9761 PUSH_INSN2(ret, location, getspecial, INT2FIX(0), INT2FIX(0));
9762 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
9763 if (popped) PUSH_INSN(ret, location, pop);
9764
9765 return;
9766 }
9767 case PM_MATCH_PREDICATE_NODE: {
9768 // foo in bar
9769 // ^^^^^^^^^^
9770 const pm_match_predicate_node_t *cast = (const pm_match_predicate_node_t *) node;
9771
9772 // First, allocate some stack space for the cached return value of any
9773 // calls to #deconstruct.
9774 PUSH_INSN(ret, location, putnil);
9775
9776 // Next, compile the expression that we're going to match against.
9777 PM_COMPILE_NOT_POPPED(cast->value);
9778 PUSH_INSN(ret, location, dup);
9779
9780 // Now compile the pattern that is going to be used to match against the
9781 // expression.
9782 LABEL *matched_label = NEW_LABEL(location.line);
9783 LABEL *unmatched_label = NEW_LABEL(location.line);
9784 LABEL *done_label = NEW_LABEL(location.line);
9785 pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
9786
9787 // If the pattern did not match, then compile the necessary instructions
9788 // to handle pushing false onto the stack, then jump to the end.
9789 PUSH_LABEL(ret, unmatched_label);
9790 PUSH_INSN(ret, location, pop);
9791 PUSH_INSN(ret, location, pop);
9792
9793 if (!popped) PUSH_INSN1(ret, location, putobject, Qfalse);
9794 PUSH_INSNL(ret, location, jump, done_label);
9795 PUSH_INSN(ret, location, putnil);
9796
9797 // If the pattern did match, then compile the necessary instructions to
9798 // handle pushing true onto the stack, then jump to the end.
9799 PUSH_LABEL(ret, matched_label);
9800 PUSH_INSN1(ret, location, adjuststack, INT2FIX(2));
9801 if (!popped) PUSH_INSN1(ret, location, putobject, Qtrue);
9802 PUSH_INSNL(ret, location, jump, done_label);
9803
9804 PUSH_LABEL(ret, done_label);
9805 return;
9806 }
9807 case PM_MATCH_REQUIRED_NODE:
9808 // foo => bar
9809 // ^^^^^^^^^^
9810 //
9811 // A match required node represents pattern matching against a single
9812 // pattern using the => operator. For example,
9813 //
9814 // foo => bar
9815 //
9816 // This is somewhat analogous to compiling a case match statement with a
9817 // single pattern. In both cases, if the pattern fails it should
9818 // immediately raise an error.
9819 pm_compile_match_required_node(iseq, (const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
9820 return;
9821 case PM_MATCH_WRITE_NODE:
9822 // /(?<foo>foo)/ =~ bar
9823 // ^^^^^^^^^^^^^^^^^^^^
9824 //
9825 // Match write nodes are specialized call nodes that have a regular
9826 // expression with valid named capture groups on the left, the =~
9827 // operator, and some value on the right. The nodes themselves simply
9828 // wrap the call with the local variable targets that will be written
9829 // when the call is executed.
9830 pm_compile_match_write_node(iseq, (const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9831 return;
9832 case PM_MISSING_NODE:
9833 rb_bug("A pm_missing_node_t should not exist in prism's AST.");
9834 return;
9835 case PM_MODULE_NODE: {
9836 // module Foo; end
9837 // ^^^^^^^^^^^^^^^
9838 const pm_module_node_t *cast = (const pm_module_node_t *) node;
9839
9840 ID module_id = pm_constant_id_lookup(scope_node, cast->name);
9841 VALUE module_name = rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(module_id)));
9842
9843 pm_scope_node_t next_scope_node;
9844 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
9845
9846 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.line);
9847 pm_scope_node_destroy(&next_scope_node);
9848
9849 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
9850 PUSH_INSN(ret, location, putnil);
9851 PUSH_INSN3(ret, location, defineclass, ID2SYM(module_id), module_iseq, INT2FIX(flags));
9852 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) module_iseq);
9853
9854 if (popped) PUSH_INSN(ret, location, pop);
9855 return;
9856 }
9857 case PM_REQUIRED_PARAMETER_NODE: {
9858 // def foo(bar); end
9859 // ^^^
9861 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
9862
9863 PUSH_SETLOCAL(ret, location, index.index, index.level);
9864 return;
9865 }
9866 case PM_MULTI_WRITE_NODE: {
9867 // foo, bar = baz
9868 // ^^^^^^^^^^^^^^
9869 //
9870 // A multi write node represents writing to multiple values using an =
9871 // operator. Importantly these nodes are only parsed when the left-hand
9872 // side of the operator has multiple targets. The right-hand side of the
9873 // operator having multiple targets represents an implicit array
9874 // instead.
9875 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
9876
9877 DECL_ANCHOR(writes);
9878 DECL_ANCHOR(cleanup);
9879
9880 pm_multi_target_state_t state = { 0 };
9881 state.position = popped ? 0 : 1;
9882 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9883
9884 PM_COMPILE_NOT_POPPED(cast->value);
9885 if (!popped) PUSH_INSN(ret, location, dup);
9886
9887 PUSH_SEQ(ret, writes);
9888 if (!popped && state.stack_size >= 1) {
9889 // Make sure the value on the right-hand side of the = operator is
9890 // being returned before we pop the parent expressions.
9891 PUSH_INSN1(ret, location, setn, INT2FIX(state.stack_size));
9892 }
9893
9894 // Now, we need to go back and modify the topn instructions in order to
9895 // ensure they can correctly retrieve the parent expressions.
9896 pm_multi_target_state_update(&state);
9897
9898 PUSH_SEQ(ret, cleanup);
9899 return;
9900 }
9901 case PM_NEXT_NODE:
9902 // next
9903 // ^^^^
9904 //
9905 // next foo
9906 // ^^^^^^^^
9907 pm_compile_next_node(iseq, (const pm_next_node_t *) node, &location, ret, popped, scope_node);
9908 return;
9909 case PM_NIL_NODE: {
9910 // nil
9911 // ^^^
9912 if (!popped) {
9913 PUSH_INSN(ret, location, putnil);
9914 }
9915
9916 return;
9917 }
9918 case PM_NO_KEYWORDS_PARAMETER_NODE: {
9919 // def foo(**nil); end
9920 // ^^^^^
9921 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9922 return;
9923 }
9924 case PM_NUMBERED_REFERENCE_READ_NODE: {
9925 // $1
9926 // ^^
9927 if (!popped) {
9929
9930 if (cast->number != 0) {
9931 VALUE ref = pm_compile_numbered_reference_ref(cast);
9932 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), ref);
9933 }
9934 else {
9935 PUSH_INSN(ret, location, putnil);
9936 }
9937 }
9938
9939 return;
9940 }
9941 case PM_OR_NODE: {
9942 // a or b
9943 // ^^^^^^
9944 const pm_or_node_t *cast = (const pm_or_node_t *) node;
9945
9946 LABEL *end_label = NEW_LABEL(location.line);
9947 PM_COMPILE_NOT_POPPED(cast->left);
9948
9949 if (!popped) PUSH_INSN(ret, location, dup);
9950 PUSH_INSNL(ret, location, branchif, end_label);
9951
9952 if (!popped) PUSH_INSN(ret, location, pop);
9953 PM_COMPILE(cast->right);
9954 PUSH_LABEL(ret, end_label);
9955
9956 return;
9957 }
9958 case PM_OPTIONAL_PARAMETER_NODE: {
9959 // def foo(bar = 1); end
9960 // ^^^^^^^
9962 PM_COMPILE_NOT_POPPED(cast->value);
9963
9964 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
9965 PUSH_SETLOCAL(ret, location, index.index, index.level);
9966
9967 return;
9968 }
9969 case PM_PARENTHESES_NODE: {
9970 // ()
9971 // ^^
9972 //
9973 // (1)
9974 // ^^^
9975 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
9976
9977 if (cast->body != NULL) {
9978 PM_COMPILE(cast->body);
9979 }
9980 else if (!popped) {
9981 PUSH_INSN(ret, location, putnil);
9982 }
9983
9984 return;
9985 }
9986 case PM_PRE_EXECUTION_NODE: {
9987 // BEGIN {}
9988 // ^^^^^^^^
9989 const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
9990
9991 LINK_ANCHOR *outer_pre = scope_node->pre_execution_anchor;
9992 RUBY_ASSERT(outer_pre != NULL);
9993
9994 // BEGIN{} nodes can be nested, so here we're going to do the same thing
9995 // that we did for the top-level compilation where we create two
9996 // anchors and then join them in the correct order into the resulting
9997 // anchor.
9998 DECL_ANCHOR(inner_pre);
9999 scope_node->pre_execution_anchor = inner_pre;
10000
10001 DECL_ANCHOR(inner_body);
10002
10003 if (cast->statements != NULL) {
10004 const pm_node_list_t *body = &cast->statements->body;
10005
10006 for (size_t index = 0; index < body->size; index++) {
10007 pm_compile_node(iseq, body->nodes[index], inner_body, true, scope_node);
10008 }
10009 }
10010
10011 if (!popped) {
10012 PUSH_INSN(inner_body, location, putnil);
10013 }
10014
10015 // Now that everything has been compiled, join both anchors together
10016 // into the correct outer pre execution anchor, and reset the value so
10017 // that subsequent BEGIN{} nodes can be compiled correctly.
10018 PUSH_SEQ(outer_pre, inner_pre);
10019 PUSH_SEQ(outer_pre, inner_body);
10020 scope_node->pre_execution_anchor = outer_pre;
10021
10022 return;
10023 }
10024 case PM_POST_EXECUTION_NODE: {
10025 // END {}
10026 // ^^^^^^
10027 const rb_iseq_t *child_iseq;
10028 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10029
10030 pm_scope_node_t next_scope_node;
10031 pm_scope_node_init(node, &next_scope_node, scope_node);
10032 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10033 pm_scope_node_destroy(&next_scope_node);
10034
10035 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10036
10037 int is_index = ISEQ_BODY(iseq)->ise_size++;
10038 PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
10039 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
10040 if (popped) PUSH_INSN(ret, location, pop);
10041
10042 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10043
10044 return;
10045 }
10046 case PM_RANGE_NODE: {
10047 // 0..5
10048 // ^^^^
10049 const pm_range_node_t *cast = (const pm_range_node_t *) node;
10050 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10051
10052 if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
10053 if (!popped) {
10054 const pm_node_t *left = cast->left;
10055 const pm_node_t *right = cast->right;
10056
10057 VALUE val = rb_range_new(
10058 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
10059 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
10060 exclude_end
10061 );
10062
10063 PUSH_INSN1(ret, location, putobject, val);
10064 }
10065 }
10066 else {
10067 if (cast->left != NULL) {
10068 PM_COMPILE(cast->left);
10069 }
10070 else if (!popped) {
10071 PUSH_INSN(ret, location, putnil);
10072 }
10073
10074 if (cast->right != NULL) {
10075 PM_COMPILE(cast->right);
10076 }
10077 else if (!popped) {
10078 PUSH_INSN(ret, location, putnil);
10079 }
10080
10081 if (!popped) {
10082 PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
10083 }
10084 }
10085 return;
10086 }
10087 case PM_RATIONAL_NODE: {
10088 // 1r
10089 // ^^
10090 if (!popped) {
10091 PUSH_INSN1(ret, location, putobject, parse_rational((const pm_rational_node_t *) node));
10092 }
10093 return;
10094 }
10095 case PM_REDO_NODE:
10096 // redo
10097 // ^^^^
10098 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10099 return;
10100 case PM_REGULAR_EXPRESSION_NODE: {
10101 // /foo/
10102 // ^^^^^
10103 if (!popped) {
10104 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10105 PUSH_INSN1(ret, location, putobject, regexp);
10106 }
10107 return;
10108 }
10109 case PM_RESCUE_NODE:
10110 // begin; rescue; end
10111 // ^^^^^^^
10112 pm_compile_rescue_node(iseq, (const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10113 return;
10114 case PM_RESCUE_MODIFIER_NODE: {
10115 // foo rescue bar
10116 // ^^^^^^^^^^^^^^
10117 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
10118
10119 pm_scope_node_t rescue_scope_node;
10120 pm_scope_node_init((const pm_node_t *) cast, &rescue_scope_node, scope_node);
10121
10122 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10123 &rescue_scope_node,
10124 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
10125 ISEQ_TYPE_RESCUE,
10126 pm_node_line_number(parser, cast->rescue_expression)
10127 );
10128
10129 pm_scope_node_destroy(&rescue_scope_node);
10130
10131 LABEL *lstart = NEW_LABEL(location.line);
10132 LABEL *lend = NEW_LABEL(location.line);
10133 LABEL *lcont = NEW_LABEL(location.line);
10134
10135 lstart->rescued = LABEL_RESCUE_BEG;
10136 lend->rescued = LABEL_RESCUE_END;
10137
10138 PUSH_LABEL(ret, lstart);
10139 PM_COMPILE_NOT_POPPED(cast->expression);
10140 PUSH_LABEL(ret, lend);
10141
10142 PUSH_INSN(ret, location, nop);
10143 PUSH_LABEL(ret, lcont);
10144 if (popped) PUSH_INSN(ret, location, pop);
10145
10146 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10147 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10148 return;
10149 }
10150 case PM_RETURN_NODE:
10151 // return
10152 // ^^^^^^
10153 //
10154 // return 1
10155 // ^^^^^^^^
10156 pm_compile_return_node(iseq, (const pm_return_node_t *) node, &location, ret, popped, scope_node);
10157 return;
10158 case PM_RETRY_NODE: {
10159 // retry
10160 // ^^^^^
10161 if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
10162 PUSH_INSN(ret, location, putnil);
10163 PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETRY));
10164 if (popped) PUSH_INSN(ret, location, pop);
10165 }
10166 else {
10167 COMPILE_ERROR(iseq, location.line, "Invalid retry");
10168 return;
10169 }
10170 return;
10171 }
10172 case PM_SCOPE_NODE:
10173 pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
10174 return;
10175 case PM_SELF_NODE: {
10176 // self
10177 // ^^^^
10178 if (!popped) {
10179 PUSH_INSN(ret, location, putself);
10180 }
10181 return;
10182 }
10183 case PM_SHAREABLE_CONSTANT_NODE: {
10184 // A value that is being written to a constant that is being marked as
10185 // shared depending on the current lexical context.
10187 pm_node_flags_t shareability = (cast->base.flags & (PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY));
10188
10189 switch (PM_NODE_TYPE(cast->write)) {
10190 case PM_CONSTANT_WRITE_NODE:
10191 pm_compile_constant_write_node(iseq, (const pm_constant_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10192 break;
10193 case PM_CONSTANT_AND_WRITE_NODE:
10194 pm_compile_constant_and_write_node(iseq, (const pm_constant_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10195 break;
10196 case PM_CONSTANT_OR_WRITE_NODE:
10197 pm_compile_constant_or_write_node(iseq, (const pm_constant_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10198 break;
10199 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10200 pm_compile_constant_operator_write_node(iseq, (const pm_constant_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10201 break;
10202 case PM_CONSTANT_PATH_WRITE_NODE:
10203 pm_compile_constant_path_write_node(iseq, (const pm_constant_path_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10204 break;
10205 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10206 pm_compile_constant_path_and_write_node(iseq, (const pm_constant_path_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10207 break;
10208 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10209 pm_compile_constant_path_or_write_node(iseq, (const pm_constant_path_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10210 break;
10211 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10212 pm_compile_constant_path_operator_write_node(iseq, (const pm_constant_path_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
10213 break;
10214 default:
10215 rb_bug("Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->write)));
10216 break;
10217 }
10218
10219 return;
10220 }
10221 case PM_SINGLETON_CLASS_NODE: {
10222 // class << self; end
10223 // ^^^^^^^^^^^^^^^^^^
10224 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
10225
10226 pm_scope_node_t next_scope_node;
10227 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
10228 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, location.line);
10229 pm_scope_node_destroy(&next_scope_node);
10230
10231 PM_COMPILE_NOT_POPPED(cast->expression);
10232 PUSH_INSN(ret, location, putnil);
10233
10234 ID singletonclass;
10235 CONST_ID(singletonclass, "singletonclass");
10236 PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10237
10238 if (popped) PUSH_INSN(ret, location, pop);
10239 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
10240
10241 return;
10242 }
10243 case PM_SOURCE_ENCODING_NODE: {
10244 // __ENCODING__
10245 // ^^^^^^^^^^^^
10246 if (!popped) {
10247 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10248 PUSH_INSN1(ret, location, putobject, value);
10249 }
10250 return;
10251 }
10252 case PM_SOURCE_FILE_NODE: {
10253 // __FILE__
10254 // ^^^^^^^^
10255 if (!popped) {
10256 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
10257 VALUE string = pm_source_file_value(cast, scope_node);
10258
10259 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10260 PUSH_INSN1(ret, location, putobject, string);
10261 }
10262 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10263 PUSH_INSN1(ret, location, putstring, string);
10264 }
10265 else {
10266 PUSH_INSN1(ret, location, putchilledstring, string);
10267 }
10268 }
10269 return;
10270 }
10271 case PM_SOURCE_LINE_NODE: {
10272 // __LINE__
10273 // ^^^^^^^^
10274 if (!popped) {
10275 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10276 PUSH_INSN1(ret, location, putobject, value);
10277 }
10278 return;
10279 }
10280 case PM_SPLAT_NODE: {
10281 // foo(*bar)
10282 // ^^^^
10283 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
10284 if (cast->expression) {
10285 PM_COMPILE(cast->expression);
10286 }
10287
10288 if (!popped) {
10289 PUSH_INSN1(ret, location, splatarray, Qtrue);
10290 }
10291 return;
10292 }
10293 case PM_STATEMENTS_NODE: {
10294 // A list of statements.
10295 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
10296 const pm_node_list_t *body = &cast->body;
10297
10298 if (body->size > 0) {
10299 for (size_t index = 0; index < body->size - 1; index++) {
10300 PM_COMPILE_POPPED(body->nodes[index]);
10301 }
10302 PM_COMPILE(body->nodes[body->size - 1]);
10303 }
10304 else {
10305 PUSH_INSN(ret, location, putnil);
10306 }
10307 return;
10308 }
10309 case PM_STRING_NODE: {
10310 // "foo"
10311 // ^^^^^
10312 if (!popped) {
10313 const pm_string_node_t *cast = (const pm_string_node_t *) node;
10314 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
10315
10316 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10317 PUSH_INSN1(ret, location, putobject, value);
10318 }
10319 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10320 PUSH_INSN1(ret, location, putstring, value);
10321 }
10322 else {
10323 PUSH_INSN1(ret, location, putchilledstring, value);
10324 }
10325 }
10326 return;
10327 }
10328 case PM_SUPER_NODE:
10329 // super()
10330 // super(foo)
10331 // super(...)
10332 pm_compile_super_node(iseq, (const pm_super_node_t *) node, &location, ret, popped, scope_node);
10333 return;
10334 case PM_SYMBOL_NODE: {
10335 // :foo
10336 // ^^^^
10337 if (!popped) {
10338 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10339 PUSH_INSN1(ret, location, putobject, value);
10340 }
10341 return;
10342 }
10343 case PM_TRUE_NODE: {
10344 // true
10345 // ^^^^
10346 if (!popped) {
10347 PUSH_INSN1(ret, location, putobject, Qtrue);
10348 }
10349 return;
10350 }
10351 case PM_UNDEF_NODE: {
10352 // undef foo
10353 // ^^^^^^^^^
10354 const pm_undef_node_t *cast = (const pm_undef_node_t *) node;
10355 const pm_node_list_t *names = &cast->names;
10356
10357 for (size_t index = 0; index < names->size; index++) {
10358 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10359 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10360
10361 PM_COMPILE_NOT_POPPED(names->nodes[index]);
10362 PUSH_SEND(ret, location, id_core_undef_method, INT2NUM(2));
10363
10364 if (index < names->size - 1) {
10365 PUSH_INSN(ret, location, pop);
10366 }
10367 }
10368
10369 if (popped) PUSH_INSN(ret, location, pop);
10370 return;
10371 }
10372 case PM_UNLESS_NODE: {
10373 // unless foo; bar end
10374 // ^^^^^^^^^^^^^^^^^^^
10375 //
10376 // bar unless foo
10377 // ^^^^^^^^^^^^^^
10378 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
10379 const pm_statements_node_t *statements = NULL;
10380 if (cast->else_clause != NULL) {
10381 statements = ((const pm_else_node_t *) cast->else_clause)->statements;
10382 }
10383
10384 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);
10385 return;
10386 }
10387 case PM_UNTIL_NODE: {
10388 // until foo; bar end
10389 // ^^^^^^^^^^^^^^^^^
10390 //
10391 // bar until foo
10392 // ^^^^^^^^^^^^^
10393 const pm_until_node_t *cast = (const pm_until_node_t *) node;
10394 pm_compile_loop(iseq, &location, cast->base.flags, PM_UNTIL_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10395 return;
10396 }
10397 case PM_WHILE_NODE: {
10398 // while foo; bar end
10399 // ^^^^^^^^^^^^^^^^^^
10400 //
10401 // bar while foo
10402 // ^^^^^^^^^^^^^
10403 const pm_while_node_t *cast = (const pm_while_node_t *) node;
10404 pm_compile_loop(iseq, &location, cast->base.flags, PM_WHILE_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10405 return;
10406 }
10407 case PM_X_STRING_NODE: {
10408 // `foo`
10409 // ^^^^^
10410 const pm_x_string_node_t *cast = (const pm_x_string_node_t *) node;
10411 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
10412
10413 PUSH_INSN(ret, location, putself);
10414 PUSH_INSN1(ret, location, putobject, value);
10415 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10416 if (popped) PUSH_INSN(ret, location, pop);
10417
10418 return;
10419 }
10420 case PM_YIELD_NODE:
10421 // yield
10422 // ^^^^^
10423 //
10424 // yield 1
10425 // ^^^^^^^
10426 pm_compile_yield_node(iseq, (const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10427 return;
10428 default:
10429 rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10430 return;
10431 }
10432}
10433
10434#undef PM_CONTAINER_P
10435
10437static inline bool
10438pm_iseq_pre_execution_p(rb_iseq_t *iseq)
10439{
10440 switch (ISEQ_BODY(iseq)->type) {
10441 case ISEQ_TYPE_TOP:
10442 case ISEQ_TYPE_EVAL:
10443 case ISEQ_TYPE_MAIN:
10444 return true;
10445 default:
10446 return false;
10447 }
10448}
10449
10457VALUE
10458pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
10459{
10460 DECL_ANCHOR(ret);
10461
10462 if (pm_iseq_pre_execution_p(iseq)) {
10463 // Because these ISEQs can have BEGIN{}, we're going to create two
10464 // anchors to compile them, a "pre" and a "body". We'll mark the "pre"
10465 // on the scope node so that when BEGIN{} is found, its contents will be
10466 // added to the "pre" anchor.
10467 DECL_ANCHOR(pre);
10468 node->pre_execution_anchor = pre;
10469
10470 // Now we'll compile the body as normal. We won't compile directly into
10471 // the "ret" anchor yet because we want to add the "pre" anchor to the
10472 // beginning of the "ret" anchor first.
10473 DECL_ANCHOR(body);
10474 pm_compile_node(iseq, (const pm_node_t *) node, body, false, node);
10475
10476 // Now we'll join both anchors together so that the content is in the
10477 // correct order.
10478 PUSH_SEQ(ret, pre);
10479 PUSH_SEQ(ret, body);
10480 }
10481 else {
10482 // In other circumstances, we can just compile the node directly into
10483 // the "ret" anchor.
10484 pm_compile_node(iseq, (const pm_node_t *) node, ret, false, node);
10485 }
10486
10487 CHECK(iseq_setup_insn(iseq, ret));
10488 return iseq_setup(iseq, ret);
10489}
10490
10495void
10496pm_parse_result_free(pm_parse_result_t *result)
10497{
10498 if (result->node.ast_node != NULL) {
10499 pm_node_destroy(&result->parser, result->node.ast_node);
10500 }
10501
10502 if (result->parsed) {
10503 xfree(result->node.constants);
10504 pm_scope_node_destroy(&result->node);
10505 }
10506
10507 pm_parser_free(&result->parser);
10508 pm_string_free(&result->input);
10509 pm_options_free(&result->options);
10510}
10511
10513typedef struct {
10516
10518 int32_t line;
10519
10522
10524 uint32_t column_end;
10526
10528typedef struct {
10530 const char *number_prefix;
10531
10533 const char *blank_prefix;
10534
10536 const char *divider;
10537
10540
10544
10545#define PM_COLOR_BOLD "\033[1m"
10546#define PM_COLOR_GRAY "\033[2m"
10547#define PM_COLOR_RED "\033[1;31m"
10548#define PM_COLOR_RESET "\033[m"
10549#define PM_ERROR_TRUNCATE 30
10550
10551static inline pm_parse_error_t *
10552pm_parse_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
10553 pm_parse_error_t *errors = xcalloc(error_list->size, sizeof(pm_parse_error_t));
10554 if (errors == NULL) return NULL;
10555
10556 int32_t start_line = parser->start_line;
10557 pm_diagnostic_t *finish = (pm_diagnostic_t * )error_list->tail->next;
10558
10559 for (pm_diagnostic_t *error = (pm_diagnostic_t *) error_list->head; error != finish; error = (pm_diagnostic_t *) error->node.next) {
10560 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.start, start_line);
10561 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10562
10563 // We're going to insert this error into the array in sorted order. We
10564 // do this by finding the first error that has a line number greater
10565 // than the current error and then inserting the current error before
10566 // that one.
10567 size_t index = 0;
10568 while (
10569 (index < error_list->size) &&
10570 (errors[index].error != NULL) &&
10571 (
10572 (errors[index].line < start.line) ||
10573 ((errors[index].line == start.line) && (errors[index].column_start < start.column))
10574 )
10575 ) index++;
10576
10577 // Now we're going to shift all of the errors after this one down one
10578 // index to make room for the new error.
10579 if (index + 1 < error_list->size) {
10580 memmove(&errors[index + 1], &errors[index], sizeof(pm_parse_error_t) * (error_list->size - index - 1));
10581 }
10582
10583 // Finally, we'll insert the error into the array.
10584 uint32_t column_end;
10585 if (start.line == end.line) {
10586 column_end = end.column;
10587 } else {
10588 column_end = (uint32_t) (newline_list->offsets[start.line - start_line + 1] - newline_list->offsets[start.line - start_line] - 1);
10589 }
10590
10591 // Ensure we have at least one column of error.
10592 if (start.column == column_end) column_end++;
10593
10594 errors[index] = (pm_parse_error_t) {
10595 .error = error,
10596 .line = start.line,
10597 .column_start = start.column,
10598 .column_end = column_end
10599 };
10600 }
10601
10602 return errors;
10603}
10604
10605/* Append a literal string to the buffer. */
10606#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10607
10608static inline void
10609pm_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) {
10610 int32_t line_delta = line - parser->start_line;
10611 assert(line_delta >= 0);
10612
10613 size_t index = (size_t) line_delta;
10614 assert(index < newline_list->size);
10615
10616 const uint8_t *start = &parser->start[newline_list->offsets[index]];
10617 const uint8_t *end;
10618
10619 if (index >= newline_list->size - 1) {
10620 end = parser->end;
10621 } else {
10622 end = &parser->start[newline_list->offsets[index + 1]];
10623 }
10624
10625 pm_buffer_append_format(buffer, number_prefix, line);
10626
10627 // Here we determine if we should truncate the end of the line.
10628 bool truncate_end = false;
10629 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10630 const uint8_t *end_candidate = start + column_end + PM_ERROR_TRUNCATE;
10631
10632 for (const uint8_t *ptr = start; ptr < end_candidate;) {
10633 size_t char_width = parser->encoding->char_width(ptr, parser->end - ptr);
10634
10635 // If we failed to decode a character, then just bail out and
10636 // truncate at the fixed width.
10637 if (char_width == 0) break;
10638
10639 // If this next character would go past the end candidate,
10640 // then we need to truncate before it.
10641 if (ptr + char_width > end_candidate) {
10642 end_candidate = ptr;
10643 break;
10644 }
10645
10646 ptr += char_width;
10647 }
10648
10649 end = end_candidate;
10650 truncate_end = true;
10651 }
10652
10653 // Here we determine if we should truncate the start of the line.
10654 if (column_start >= PM_ERROR_TRUNCATE) {
10655 pm_buffer_append_string(buffer, "... ", 4);
10656 start += column_start;
10657 }
10658
10659 pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));
10660
10661 if (truncate_end) {
10662 pm_buffer_append_string(buffer, " ...\n", 5);
10663 } else if (end == parser->end && end[-1] != '\n') {
10664 pm_buffer_append_string(buffer, "\n", 1);
10665 }
10666}
10667
10671static void
10672pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, int highlight, bool inline_messages) {
10673 assert(error_list->size != 0);
10674
10675 // First, we're going to sort all of the errors by line number using an
10676 // insertion sort into a newly allocated array.
10677 const int32_t start_line = parser->start_line;
10678 const pm_newline_list_t *newline_list = &parser->newline_list;
10679
10680 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10681 if (errors == NULL) return;
10682
10683 // Now we're going to determine how we're going to format line numbers and
10684 // blank lines based on the maximum number of digits in the line numbers
10685 // that are going to be displaid.
10686 pm_parse_error_format_t error_format;
10687 int32_t first_line_number = errors[0].line;
10688 int32_t last_line_number = errors[error_list->size - 1].line;
10689
10690 // If we have a maximum line number that is negative, then we're going to
10691 // use the absolute value for comparison but multiple by 10 to additionally
10692 // have a column for the negative sign.
10693 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10694 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10695 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10696
10697 if (max_line_number < 10) {
10698 if (highlight > 0) {
10699 error_format = (pm_parse_error_format_t) {
10700 .number_prefix = PM_COLOR_GRAY "%1" 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 = "%1" PRIi32 " | ",
10707 .blank_prefix = " | ",
10708 .divider = " ~~~~~\n"
10709 };
10710 }
10711 } else if (max_line_number < 100) {
10712 if (highlight > 0) {
10713 error_format = (pm_parse_error_format_t) {
10714 .number_prefix = PM_COLOR_GRAY "%2" 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 = "%2" PRIi32 " | ",
10721 .blank_prefix = " | ",
10722 .divider = " ~~~~~~\n"
10723 };
10724 }
10725 } else if (max_line_number < 1000) {
10726 if (highlight > 0) {
10727 error_format = (pm_parse_error_format_t) {
10728 .number_prefix = PM_COLOR_GRAY "%3" 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 = "%3" PRIi32 " | ",
10735 .blank_prefix = " | ",
10736 .divider = " ~~~~~~~\n"
10737 };
10738 }
10739 } else if (max_line_number < 10000) {
10740 if (highlight > 0) {
10741 error_format = (pm_parse_error_format_t) {
10742 .number_prefix = PM_COLOR_GRAY "%4" PRIi32 " | " PM_COLOR_RESET,
10743 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10744 .divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
10745 };
10746 } else {
10747 error_format = (pm_parse_error_format_t) {
10748 .number_prefix = "%4" PRIi32 " | ",
10749 .blank_prefix = " | ",
10750 .divider = " ~~~~~~~~\n"
10751 };
10752 }
10753 } else {
10754 if (highlight > 0) {
10755 error_format = (pm_parse_error_format_t) {
10756 .number_prefix = PM_COLOR_GRAY "%5" PRIi32 " | " PM_COLOR_RESET,
10757 .blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
10758 .divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
10759 };
10760 } else {
10761 error_format = (pm_parse_error_format_t) {
10762 .number_prefix = "%5" PRIi32 " | ",
10763 .blank_prefix = " | ",
10764 .divider = " ~~~~~~~~\n"
10765 };
10766 }
10767 }
10768
10769 error_format.blank_prefix_length = strlen(error_format.blank_prefix);
10770 error_format.divider_length = strlen(error_format.divider);
10771
10772 // Now we're going to iterate through every error in our error list and
10773 // display it. While we're iterating, we will display some padding lines of
10774 // the source before the error to give some context. We'll be careful not to
10775 // display the same line twice in case the errors are close enough in the
10776 // source.
10777 int32_t last_line = parser->start_line - 1;
10778 uint32_t last_column_start = 0;
10779 const pm_encoding_t *encoding = parser->encoding;
10780
10781 for (size_t index = 0; index < error_list->size; index++) {
10782 pm_parse_error_t *error = &errors[index];
10783
10784 // Here we determine how many lines of padding of the source to display,
10785 // based on the difference from the last line that was displaid.
10786 if (error->line - last_line > 1) {
10787 if (error->line - last_line > 2) {
10788 if ((index != 0) && (error->line - last_line > 3)) {
10789 pm_buffer_append_string(buffer, error_format.divider, error_format.divider_length);
10790 }
10791
10792 pm_buffer_append_string(buffer, " ", 2);
10793 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 2, 0, 0, buffer);
10794 }
10795
10796 pm_buffer_append_string(buffer, " ", 2);
10797 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 1, 0, 0, buffer);
10798 }
10799
10800 // If this is the first error or we're on a new line, then we'll display
10801 // the line that has the error in it.
10802 if ((index == 0) || (error->line != last_line)) {
10803 if (highlight > 1) {
10804 pm_buffer_append_literal(buffer, PM_COLOR_RED "> " PM_COLOR_RESET);
10805 } else if (highlight > 0) {
10806 pm_buffer_append_literal(buffer, PM_COLOR_BOLD "> " PM_COLOR_RESET);
10807 } else {
10808 pm_buffer_append_literal(buffer, "> ");
10809 }
10810
10811 last_column_start = error->column_start;
10812
10813 // Find the maximum column end of all the errors on this line.
10814 uint32_t column_end = error->column_end;
10815 for (size_t next_index = index + 1; next_index < error_list->size; next_index++) {
10816 if (errors[next_index].line != error->line) break;
10817 if (errors[next_index].column_end > column_end) column_end = errors[next_index].column_end;
10818 }
10819
10820 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line, error->column_start, column_end, buffer);
10821 }
10822
10823 const uint8_t *start = &parser->start[newline_list->offsets[error->line - start_line]];
10824 if (start == parser->end) pm_buffer_append_byte(buffer, '\n');
10825
10826 // Now we'll display the actual error message. We'll do this by first
10827 // putting the prefix to the line, then a bunch of blank spaces
10828 // depending on the column, then as many carets as we need to display
10829 // the width of the error, then the error message itself.
10830 //
10831 // Note that this doesn't take into account the width of the actual
10832 // character when displaid in the terminal. For some east-asian
10833 // languages or emoji, this means it can be thrown off pretty badly. We
10834 // will need to solve this eventually.
10835 pm_buffer_append_string(buffer, " ", 2);
10836 pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
10837
10838 size_t column = 0;
10839 if (last_column_start >= PM_ERROR_TRUNCATE) {
10840 pm_buffer_append_string(buffer, " ", 4);
10841 column = last_column_start;
10842 }
10843
10844 while (column < error->column_start) {
10845 pm_buffer_append_byte(buffer, ' ');
10846
10847 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10848 column += (char_width == 0 ? 1 : char_width);
10849 }
10850
10851 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10852 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10853 pm_buffer_append_byte(buffer, '^');
10854
10855 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10856 column += (char_width == 0 ? 1 : char_width);
10857
10858 while (column < error->column_end) {
10859 pm_buffer_append_byte(buffer, '~');
10860
10861 size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
10862 column += (char_width == 0 ? 1 : char_width);
10863 }
10864
10865 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10866
10867 if (inline_messages) {
10868 pm_buffer_append_byte(buffer, ' ');
10869 assert(error->error != NULL);
10870
10871 const char *message = error->error->message;
10872 pm_buffer_append_string(buffer, message, strlen(message));
10873 }
10874
10875 pm_buffer_append_byte(buffer, '\n');
10876
10877 // Here we determine how many lines of padding to display after the
10878 // error, depending on where the next error is in source.
10879 last_line = error->line;
10880 int32_t next_line;
10881
10882 if (index == error_list->size - 1) {
10883 next_line = (((int32_t) newline_list->size) + parser->start_line);
10884
10885 // If the file ends with a newline, subtract one from our "next_line"
10886 // so that we don't output an extra line at the end of the file
10887 if ((parser->start + newline_list->offsets[newline_list->size - 1]) == parser->end) {
10888 next_line--;
10889 }
10890 }
10891 else {
10892 next_line = errors[index + 1].line;
10893 }
10894
10895 if (next_line - last_line > 1) {
10896 pm_buffer_append_string(buffer, " ", 2);
10897 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
10898 }
10899
10900 if (next_line - last_line > 1) {
10901 pm_buffer_append_string(buffer, " ", 2);
10902 pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
10903 }
10904 }
10905
10906 // Finally, we'll free the array of errors that we allocated.
10907 xfree(errors);
10908}
10909
10910#undef PM_ERROR_TRUNCATE
10911#undef PM_COLOR_GRAY
10912#undef PM_COLOR_RED
10913#undef PM_COLOR_RESET
10914
10921static bool
10922pm_parse_process_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
10923{
10924 const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
10925 const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
10926
10927 const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
10928 const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
10929 size_t width;
10930
10931 while (start < end) {
10932 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
10933 start += width;
10934 }
10935
10936 return true;
10937}
10938
10943static VALUE
10944pm_parse_process_error(const pm_parse_result_t *result)
10945{
10946 const pm_parser_t *parser = &result->parser;
10947 const pm_diagnostic_t *head = (const pm_diagnostic_t *) parser->error_list.head;
10948 bool valid_utf8 = true;
10949
10950 pm_buffer_t buffer = { 0 };
10951 const pm_string_t *filepath = &parser->filepath;
10952
10953 int highlight = rb_stderr_tty_p();
10954 if (highlight) {
10955 const char *no_color = getenv("NO_COLOR");
10956 highlight = (no_color == NULL || no_color[0] == '\0') ? 2 : 1;
10957 }
10958
10959 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
10960 switch (error->level) {
10962 // It is implicitly assumed that the error messages will be
10963 // encodeable as UTF-8. Because of this, we can't include source
10964 // examples that contain invalid byte sequences. So if any source
10965 // examples include invalid UTF-8 byte sequences, we will skip
10966 // showing source examples entirely.
10967 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
10968 valid_utf8 = false;
10969 }
10970 break;
10972 // Any errors with the level PM_ERROR_LEVEL_ARGUMENT take over as
10973 // the only argument that gets raised. This is to allow priority
10974 // messages that should be handled before anything else.
10975 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
10976
10977 pm_buffer_append_format(
10978 &buffer,
10979 "%.*s:%" PRIi32 ": %s",
10980 (int) pm_string_length(filepath),
10981 pm_string_source(filepath),
10982 line_number,
10983 error->message
10984 );
10985
10986 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
10987 pm_buffer_append_byte(&buffer, '\n');
10988
10989 pm_list_node_t *list_node = (pm_list_node_t *) error;
10990 pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
10991
10992 pm_parse_errors_format(parser, &error_list, &buffer, highlight, false);
10993 }
10994
10995 VALUE value = rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
10996 pm_buffer_free(&buffer);
10997
10998 return value;
10999 }
11000 case PM_ERROR_LEVEL_LOAD: {
11001 // Load errors are much simpler, because they don't include any of
11002 // the source in them. We create the error directly from the
11003 // message.
11004 VALUE message = rb_enc_str_new_cstr(error->message, rb_locale_encoding());
11005 VALUE value = rb_exc_new3(rb_eLoadError, message);
11006 rb_ivar_set(value, rb_intern_const("@path"), Qnil);
11007 return value;
11008 }
11009 }
11010 }
11011
11012 pm_buffer_append_format(
11013 &buffer,
11014 "%.*s:%" PRIi32 ": syntax error%s found\n",
11015 (int) pm_string_length(filepath),
11016 pm_string_source(filepath),
11017 (int32_t) pm_location_line_number(parser, &head->location),
11018 (parser->error_list.size > 1) ? "s" : ""
11019 );
11020
11021 if (valid_utf8) {
11022 pm_parse_errors_format(parser, &parser->error_list, &buffer, highlight, true);
11023 }
11024 else {
11025 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
11026 if (error != head) pm_buffer_append_byte(&buffer, '\n');
11027 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);
11028 }
11029 }
11030
11031 VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->node.encoding);
11032 VALUE error = rb_exc_new_str(rb_eSyntaxError, message);
11033
11034 rb_encoding *filepath_encoding = result->node.filepath_encoding != NULL ? result->node.filepath_encoding : rb_utf8_encoding();
11035 VALUE path = rb_enc_str_new((const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
11036
11037 rb_ivar_set(error, rb_intern_const("@path"), path);
11038 pm_buffer_free(&buffer);
11039
11040 return error;
11041}
11042
11048static VALUE
11049pm_parse_process(pm_parse_result_t *result, pm_node_t *node, VALUE *script_lines)
11050{
11051 pm_parser_t *parser = &result->parser;
11052
11053 // First, set up the scope node so that the AST node is attached and can be
11054 // freed regardless of whether or we return an error.
11055 pm_scope_node_t *scope_node = &result->node;
11056 rb_encoding *filepath_encoding = scope_node->filepath_encoding;
11057 int coverage_enabled = scope_node->coverage_enabled;
11058
11059 pm_scope_node_init(node, scope_node, NULL);
11060 scope_node->filepath_encoding = filepath_encoding;
11061
11062 scope_node->encoding = rb_enc_find(parser->encoding->name);
11063 if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
11064
11065 scope_node->coverage_enabled = coverage_enabled;
11066
11067 // If RubyVM.keep_script_lines is set to true, then we need to create that
11068 // array of script lines here.
11069 if (script_lines != NULL) {
11070 *script_lines = rb_ary_new_capa(parser->newline_list.size);
11071
11072 for (size_t index = 0; index < parser->newline_list.size; index++) {
11073 size_t offset = parser->newline_list.offsets[index];
11074 size_t length = index == parser->newline_list.size - 1 ? ((size_t) (parser->end - (parser->start + offset))) : (parser->newline_list.offsets[index + 1] - offset);
11075 rb_ary_push(*script_lines, rb_enc_str_new((const char *) parser->start + offset, length, scope_node->encoding));
11076 }
11077
11078 scope_node->script_lines = script_lines;
11079 }
11080
11081 // Emit all of the various warnings from the parse.
11082 const pm_diagnostic_t *warning;
11083 const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);
11084
11085 for (warning = (const pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (const pm_diagnostic_t *) warning->node.next) {
11086 int line = pm_location_line_number(parser, &warning->location);
11087
11088 if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
11089 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line, "%s", warning->message);
11090 }
11091 else {
11092 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line, "%s", warning->message);
11093 }
11094 }
11095
11096 // If there are errors, raise an appropriate error and free the result.
11097 if (parser->error_list.size > 0) {
11098 VALUE error = pm_parse_process_error(result);
11099
11100 // TODO: We need to set the backtrace.
11101 // rb_funcallv(error, rb_intern("set_backtrace"), 1, &path);
11102 return error;
11103 }
11104
11105 // Now set up the constant pool and intern all of the various constants into
11106 // their corresponding IDs.
11107 scope_node->parser = parser;
11108 scope_node->constants = parser->constant_pool.size ? xcalloc(parser->constant_pool.size, sizeof(ID)) : NULL;
11109
11110 for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
11111 pm_constant_t *constant = &parser->constant_pool.constants[index];
11112 scope_node->constants[index] = rb_intern3((const char *) constant->start, constant->length, scope_node->encoding);
11113 }
11114
11115 scope_node->index_lookup_table = st_init_numtable();
11116 pm_constant_id_list_t *locals = &scope_node->locals;
11117 for (size_t index = 0; index < locals->size; index++) {
11118 st_insert(scope_node->index_lookup_table, locals->ids[index], index);
11119 }
11120
11121 // If we got here, this is a success and we can return Qnil to indicate that
11122 // no error should be raised.
11123 result->parsed = true;
11124 return Qnil;
11125}
11126
11131static void
11132pm_options_frozen_string_literal_init(pm_options_t *options)
11133{
11134 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
11135
11136 switch (frozen_string_literal) {
11137 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11138 break;
11139 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11141 break;
11142 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11144 break;
11145 default:
11146 rb_bug("pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
11147 break;
11148 }
11149}
11150
11155static inline VALUE
11156pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t *parser)
11157{
11158 const pm_newline_list_t *newline_list = &parser->newline_list;
11159 const char *start = (const char *) parser->start;
11160 const char *end = (const char *) parser->end;
11161
11162 // If we end exactly on a newline, then there's no need to push on a final
11163 // segment. If we don't, then we need to push on the last offset up to the
11164 // end of the string.
11165 size_t last_offset = newline_list->offsets[newline_list->size - 1];
11166 bool last_push = start + last_offset != end;
11167
11168 // Create the ruby strings that represent the lines of the source.
11169 VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
11170
11171 for (size_t index = 0; index < newline_list->size - 1; index++) {
11172 size_t offset = newline_list->offsets[index];
11173 size_t length = newline_list->offsets[index + 1] - offset;
11174
11175 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
11176 }
11177
11178 // Push on the last line if we need to.
11179 if (last_push) {
11180 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11181 }
11182
11183 return lines;
11184}
11185
11186// This is essentially pm_string_mapped_init(), preferring to memory map the
11187// file, with additional handling for files that require blocking to properly
11188// read (e.g. pipes).
11190pm_read_file(pm_string_t *string, const char *filepath)
11191{
11192#ifdef _WIN32
11193 // Open the file for reading.
11194 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
11195 if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
11196
11197 WCHAR *wfilepath = xmalloc(sizeof(WCHAR) * ((size_t) length));
11198 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
11199 xfree(wfilepath);
11201 }
11202
11203 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
11204 if (file == INVALID_HANDLE_VALUE) {
11206
11207 if (GetLastError() == ERROR_ACCESS_DENIED) {
11208 DWORD attributes = GetFileAttributesW(wfilepath);
11209 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
11211 }
11212 }
11213
11214 xfree(wfilepath);
11215 return result;
11216 }
11217
11218 // Get the file size.
11219 DWORD file_size = GetFileSize(file, NULL);
11220 if (file_size == INVALID_FILE_SIZE) {
11221 CloseHandle(file);
11222 xfree(wfilepath);
11224 }
11225
11226 // If the file is empty, then we don't need to do anything else, we'll set
11227 // the source to a constant empty string and return.
11228 if (file_size == 0) {
11229 CloseHandle(file);
11230 xfree(wfilepath);
11231 const uint8_t source[] = "";
11232 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11234 }
11235
11236 // Create a mapping of the file.
11237 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
11238 if (mapping == NULL) {
11239 CloseHandle(file);
11240 xfree(wfilepath);
11242 }
11243
11244 // Map the file into memory.
11245 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
11246 CloseHandle(mapping);
11247 CloseHandle(file);
11248 xfree(wfilepath);
11249
11250 if (source == NULL) {
11252 }
11253
11254 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
11256#elif defined(_POSIX_MAPPED_FILES)
11257 // Open the file for reading
11258 const int open_mode = O_RDONLY | O_NONBLOCK;
11259 int fd = open(filepath, open_mode);
11260 if (fd == -1) {
11262 }
11263
11264 // Stat the file to get the file size
11265 struct stat sb;
11266 if (fstat(fd, &sb) == -1) {
11267 close(fd);
11269 }
11270
11271 // Ensure it is a file and not a directory
11272 if (S_ISDIR(sb.st_mode)) {
11273 close(fd);
11275 }
11276
11277 // We need to wait for data first before reading from pipes and character
11278 // devices. To not block the entire VM, we need to release the GVL while
11279 // reading. Use IO#read to do this and let the GC handle closing the FD.
11280 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
11281 VALUE io = rb_io_fdopen((int) fd, open_mode, filepath);
11283 VALUE contents = rb_funcall(io, rb_intern("read"), 0);
11284
11285 if (!RB_TYPE_P(contents, T_STRING)) {
11287 }
11288
11289 long len = RSTRING_LEN(contents);
11290 if (len < 0) {
11292 }
11293
11294 size_t length = (size_t) len;
11295 uint8_t *source = malloc(length);
11296 memcpy(source, RSTRING_PTR(contents), length);
11297 *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
11298
11300 }
11301
11302 // mmap the file descriptor to virtually get the contents
11303 size_t size = (size_t) sb.st_size;
11304 uint8_t *source = NULL;
11305
11306 if (size == 0) {
11307 close(fd);
11308 const uint8_t source[] = "";
11309 *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11311 }
11312
11313 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
11314 if (source == MAP_FAILED) {
11315 close(fd);
11317 }
11318
11319 close(fd);
11320 *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
11322#else
11323 return pm_string_file_init(string, filepath);
11324#endif
11325}
11326
11331VALUE
11332pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
11333{
11334 pm_string_init_result_t init_result = pm_read_file(&result->input, RSTRING_PTR(filepath));
11335
11336 if (init_result == PM_STRING_INIT_SUCCESS) {
11337 pm_options_frozen_string_literal_init(&result->options);
11338 return Qnil;
11339 }
11340
11341 int err;
11342 if (init_result == PM_STRING_INIT_ERROR_DIRECTORY) {
11343 err = EISDIR;
11344 } else {
11345#ifdef _WIN32
11346 err = rb_w32_map_errno(GetLastError());
11347#else
11348 err = errno;
11349#endif
11350 }
11351
11352 VALUE error;
11353 if (load_error) {
11354 VALUE message = rb_str_buf_new_cstr(strerror(err));
11355 rb_str_cat2(message, " -- ");
11356 rb_str_append(message, filepath);
11357
11358 error = rb_exc_new3(rb_eLoadError, message);
11359 rb_ivar_set(error, rb_intern_const("@path"), filepath);
11360 } else {
11361 error = rb_syserr_new(err, RSTRING_PTR(filepath));
11362 RB_GC_GUARD(filepath);
11363 }
11364
11365 return error;
11366}
11367
11374VALUE
11375pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
11376{
11377 result->node.filepath_encoding = rb_enc_get(filepath);
11378 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
11379 RB_GC_GUARD(filepath);
11380
11381 pm_options_version_for_current_ruby_set(&result->options);
11382
11383 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
11384 pm_node_t *node = pm_parse(&result->parser);
11385
11386 VALUE error = pm_parse_process(result, node, script_lines);
11387
11388 // If we're parsing a filepath, then we need to potentially support the
11389 // SCRIPT_LINES__ constant, which can be a hash that has an array of lines
11390 // of every read file.
11391 ID id_script_lines = rb_intern("SCRIPT_LINES__");
11392
11393 if (rb_const_defined_at(rb_cObject, id_script_lines)) {
11394 VALUE constant_script_lines = rb_const_get_at(rb_cObject, id_script_lines);
11395
11396 if (RB_TYPE_P(constant_script_lines, T_HASH)) {
11397 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->node, &result->parser));
11398 }
11399 }
11400
11401 return error;
11402}
11403
11408VALUE
11409pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
11410{
11411 VALUE error = pm_load_file(result, filepath, false);
11412 if (NIL_P(error)) {
11413 error = pm_parse_file(result, filepath, script_lines);
11414 }
11415
11416 return error;
11417}
11418
11425VALUE
11426pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines)
11427{
11428 rb_encoding *encoding = rb_enc_get(source);
11429 if (!rb_enc_asciicompat(encoding)) {
11430 return rb_exc_new_cstr(rb_eArgError, "invalid source encoding");
11431 }
11432
11433 pm_options_frozen_string_literal_init(&result->options);
11434 pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
11435 pm_options_encoding_set(&result->options, rb_enc_name(encoding));
11436
11437 result->node.filepath_encoding = rb_enc_get(filepath);
11438 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
11439 RB_GC_GUARD(filepath);
11440
11441 pm_options_version_for_current_ruby_set(&result->options);
11442
11443 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
11444 pm_node_t *node = pm_parse(&result->parser);
11445
11446 return pm_parse_process(result, node, script_lines);
11447}
11448
11450 VALUE rb_stdin;
11451 int eof_seen;
11452};
11453
11454static int
11455pm_parse_stdin_eof(void *stream)
11456{
11457 struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
11458 return wrapped_stdin->eof_seen;
11459}
11460
11464static char *
11465pm_parse_stdin_fgets(char *string, int size, void *stream)
11466{
11467 RUBY_ASSERT(size > 0);
11468
11469 struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
11470
11471 VALUE line = rb_funcall(wrapped_stdin->rb_stdin, rb_intern("gets"), 1, INT2FIX(size - 1));
11472 if (NIL_P(line)) {
11473 return NULL;
11474 }
11475
11476 const char *cstr = RSTRING_PTR(line);
11477 long length = RSTRING_LEN(line);
11478
11479 memcpy(string, cstr, length);
11480 string[length] = '\0';
11481
11482 // We're reading strings from stdin via gets. We'll assume that if the
11483 // string is smaller than the requested length, and doesn't end with a
11484 // newline, that we hit EOF.
11485 if (length < (size - 1) && string[length - 1] != '\n') {
11486 wrapped_stdin->eof_seen = 1;
11487 }
11488
11489 return string;
11490}
11491
11492// We need access to this function when we're done parsing stdin.
11493void rb_reset_argf_lineno(long n);
11494
11500VALUE
11501pm_parse_stdin(pm_parse_result_t *result)
11502{
11503 pm_options_frozen_string_literal_init(&result->options);
11504
11505 struct rb_stdin_wrapper wrapped_stdin = {
11506 rb_stdin,
11507 0
11508 };
11509
11510 pm_buffer_t buffer;
11511 pm_node_t *node = pm_parse_stream(&result->parser, &buffer, (void *) &wrapped_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof, &result->options);
11512
11513 // Copy the allocated buffer contents into the input string so that it gets
11514 // freed. At this point we've handed over ownership, so we don't need to
11515 // free the buffer itself.
11516 pm_string_owned_init(&result->input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11517
11518 // When we're done parsing, we reset $. because we don't want the fact that
11519 // we went through an IO object to be visible to the user.
11520 rb_reset_argf_lineno(0);
11521
11522 return pm_parse_process(result, node, NULL);
11523}
11524
11525#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
11526#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) PM_OPTIONS_VERSION_CRUBY_##major##_##minor
11527
11528void pm_options_version_for_current_ruby_set(pm_options_t *options) {
11529 options->version = PM_VERSION_FOR_RELEASE(RUBY_API_VERSION_MAJOR, RUBY_API_VERSION_MINOR);
11530}
11531
11532#undef NEW_ISEQ
11533#define NEW_ISEQ OLD_ISEQ
11534
11535#undef NEW_CHILD_ISEQ
11536#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
Definition diagnostic.h:412
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
Definition diagnostic.h:398
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
Definition diagnostic.h:401
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
Definition diagnostic.h:395
#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:1674
#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:1682
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:401
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define UINT2NUM
Old name of RB_UINT2NUM.
Definition int.h:46
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition error.h:486
VALUE rb_eNotImpError
NotImplementedError exception.
Definition error.c:1440
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:682
VALUE rb_eStandardError
StandardError exception.
Definition error.c:1427
VALUE rb_eLoadError
LoadError exception.
Definition error.c:1448
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
Definition error.c:1443
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition error.c:1468
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
Definition error.c:1444
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1481
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1447
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
Definition error.c:3894
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:100
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:1326
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:932
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:12658
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:1133
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
Definition io.c:9369
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
Definition range.c:69
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:3760
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1714
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1497
#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:1669
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1638
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:3997
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3241
#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:1513
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1818
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:1984
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:3463
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:3786
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:974
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:993
@ RUBY_IO_READABLE
IO::READABLE
Definition io.h:97
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
Definition io.c:1454
int len
Length of the buffer.
Definition io.h:8
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:1429
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
#define RUBY_API_VERSION_MAJOR
Major version.
Definition version.h:64
#define RUBY_API_VERSION_MINOR
Minor version.
Definition version.h:70
#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.
PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding)
Set the encoding option on the given options struct.
Definition options.c:24
PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options)
Free the internal memory associated with the options.
Definition options.c:208
PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal)
Set the frozen string literal option on the given options struct.
Definition options.c:48
PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath)
Set the filepath option on the given options struct.
Definition options.c:16
void pm_buffer_free(pm_buffer_t *buffer)
Free the memory associated with the buffer.
Definition pm_buffer.c:355
size_t pm_buffer_length(const pm_buffer_t *buffer)
Return the length of the buffer.
Definition pm_buffer.c:43
char * pm_buffer_value(const pm_buffer_t *buffer)
Return the value of the buffer.
Definition pm_buffer.c:35
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
Definition pm_string.c:353
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string)
Returns the start pointer associated with the string.
Definition pm_string.c:361
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition pm_string.c:369
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
Definition pm_string.c:210
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
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser)
Parse the Ruby source associated with the given parser and return the tree.
Definition prism.c:22911
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser)
Free any memory associated with the given parser.
Definition prism.c:22885
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const pm_options_t *options)
Parse a stream of Ruby source and return the tree.
Definition prism.c:22998
PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options)
Initialize a parser with the given start and end pointers.
Definition prism.c:22586
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RTEST
This is an old name of RB_TEST.
AliasGlobalVariableNode.
Definition ast.h:1106
struct pm_node * old_name
AliasGlobalVariableNode::old_name.
Definition ast.h:1129
struct pm_node * new_name
AliasGlobalVariableNode::new_name.
Definition ast.h:1119
AliasMethodNode.
Definition ast.h:1154
struct pm_node * old_name
AliasMethodNode::old_name.
Definition ast.h:1189
struct pm_node * new_name
AliasMethodNode::new_name.
Definition ast.h:1173
AlternationPatternNode.
Definition ast.h:1214
struct pm_node * left
AlternationPatternNode::left.
Definition ast.h:1227
struct pm_node * right
AlternationPatternNode::right.
Definition ast.h:1237
AndNode.
Definition ast.h:1262
struct pm_node * left
AndNode::left.
Definition ast.h:1278
struct pm_node * right
AndNode::right.
Definition ast.h:1291
ArgumentsNode.
Definition ast.h:1323
pm_node_t base
The embedded base node.
Definition ast.h:1325
struct pm_node_list arguments
ArgumentsNode::arguments.
Definition ast.h:1336
ArrayNode.
Definition ast.h:1354
struct pm_node_list elements
ArrayNode::elements.
Definition ast.h:1364
ArrayPatternNode.
Definition ast.h:1415
struct pm_node_list requireds
ArrayPatternNode::requireds.
Definition ast.h:1444
struct pm_node * rest
ArrayPatternNode::rest.
Definition ast.h:1454
struct pm_node * constant
ArrayPatternNode::constant.
Definition ast.h:1434
struct pm_node_list posts
ArrayPatternNode::posts.
Definition ast.h:1464
AssocNode.
Definition ast.h:1499
struct pm_node * value
AssocNode::value.
Definition ast.h:1531
struct pm_node * key
AssocNode::key.
Definition ast.h:1518
AssocSplatNode.
Definition ast.h:1556
struct pm_node * value
AssocSplatNode::value.
Definition ast.h:1569
BackReferenceReadNode.
Definition ast.h:1594
pm_node_t base
The embedded base node.
Definition ast.h:1596
BeginNode.
Definition ast.h:1625
struct pm_ensure_node * ensure_clause
BeginNode::ensure_clause.
Definition ast.h:1678
struct pm_rescue_node * rescue_clause
BeginNode::rescue_clause.
Definition ast.h:1658
struct pm_statements_node * statements
BeginNode::statements.
Definition ast.h:1648
struct pm_else_node * else_clause
BeginNode::else_clause.
Definition ast.h:1668
BlockArgumentNode.
Definition ast.h:1703
struct pm_node * expression
BlockArgumentNode::expression.
Definition ast.h:1716
BlockLocalVariableNode.
Definition ast.h:1744
BlockNode.
Definition ast.h:1772
struct pm_node * parameters
BlockNode::parameters.
Definition ast.h:1799
struct pm_node * body
BlockNode::body.
Definition ast.h:1809
pm_constant_id_list_t locals
BlockNode::locals.
Definition ast.h:1785
BlockParameterNode.
Definition ast.h:1848
BlockParametersNode.
Definition ast.h:1902
BreakNode.
Definition ast.h:1976
struct pm_arguments_node * arguments
BreakNode::arguments.
Definition ast.h:1989
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition pm_buffer.h:22
CallAndWriteNode.
Definition ast.h:2020
struct pm_node * value
CallAndWriteNode::value.
Definition ast.h:2093
pm_constant_id_t read_name
CallAndWriteNode::read_name.
Definition ast.h:2063
pm_constant_id_t write_name
CallAndWriteNode::write_name.
Definition ast.h:2073
struct pm_node * receiver
CallAndWriteNode::receiver.
Definition ast.h:2033
CallNode.
Definition ast.h:2129
pm_location_t closing_loc
CallNode::closing_loc.
Definition ast.h:2210
struct pm_node * receiver
CallNode::receiver.
Definition ast.h:2148
pm_constant_id_t name
CallNode::name.
Definition ast.h:2171
pm_node_t base
The embedded base node.
Definition ast.h:2131
pm_location_t message_loc
CallNode::message_loc.
Definition ast.h:2181
struct pm_arguments_node * arguments
CallNode::arguments.
Definition ast.h:2200
struct pm_node * block
CallNode::block.
Definition ast.h:2220
CallOperatorWriteNode.
Definition ast.h:2241
pm_constant_id_t read_name
CallOperatorWriteNode::read_name.
Definition ast.h:2284
pm_constant_id_t binary_operator
CallOperatorWriteNode::binary_operator.
Definition ast.h:2304
struct pm_node * receiver
CallOperatorWriteNode::receiver.
Definition ast.h:2254
pm_constant_id_t write_name
CallOperatorWriteNode::write_name.
Definition ast.h:2294
struct pm_node * value
CallOperatorWriteNode::value.
Definition ast.h:2324
CallOrWriteNode.
Definition ast.h:2345
struct pm_node * receiver
CallOrWriteNode::receiver.
Definition ast.h:2358
struct pm_node * value
CallOrWriteNode::value.
Definition ast.h:2418
pm_constant_id_t write_name
CallOrWriteNode::write_name.
Definition ast.h:2398
pm_constant_id_t read_name
CallOrWriteNode::read_name.
Definition ast.h:2388
CallTargetNode.
Definition ast.h:2447
pm_constant_id_t name
CallTargetNode::name.
Definition ast.h:2480
struct pm_node * receiver
CallTargetNode::receiver.
Definition ast.h:2460
CapturePatternNode.
Definition ast.h:2505
struct pm_local_variable_target_node * target
CapturePatternNode::target.
Definition ast.h:2528
struct pm_node * value
CapturePatternNode::value.
Definition ast.h:2518
CaseMatchNode.
Definition ast.h:2555
struct pm_node_list conditions
CaseMatchNode::conditions.
Definition ast.h:2578
struct pm_else_node * else_clause
CaseMatchNode::else_clause.
Definition ast.h:2588
struct pm_node * predicate
CaseMatchNode::predicate.
Definition ast.h:2568
CaseNode.
Definition ast.h:2625
struct pm_node * predicate
CaseNode::predicate.
Definition ast.h:2638
struct pm_else_node * else_clause
CaseNode::else_clause.
Definition ast.h:2658
struct pm_node_list conditions
CaseNode::conditions.
Definition ast.h:2648
ClassNode.
Definition ast.h:2693
struct pm_node * constant_path
ClassNode::constant_path.
Definition ast.h:2716
pm_constant_id_list_t locals
ClassNode::locals.
Definition ast.h:2701
pm_constant_id_t name
ClassNode::name.
Definition ast.h:2766
struct pm_node * body
ClassNode::body.
Definition ast.h:2747
struct pm_node * superclass
ClassNode::superclass.
Definition ast.h:2736
ClassVariableAndWriteNode.
Definition ast.h:2781
struct pm_node * value
ClassVariableAndWriteNode::value.
Definition ast.h:2824
pm_constant_id_t name
ClassVariableAndWriteNode::name.
Definition ast.h:2794
ClassVariableOperatorWriteNode.
Definition ast.h:2839
pm_constant_id_t name
ClassVariableOperatorWriteNode::name.
Definition ast.h:2847
pm_constant_id_t binary_operator
ClassVariableOperatorWriteNode::binary_operator.
Definition ast.h:2867
struct pm_node * value
ClassVariableOperatorWriteNode::value.
Definition ast.h:2862
ClassVariableOrWriteNode.
Definition ast.h:2882
pm_constant_id_t name
ClassVariableOrWriteNode::name.
Definition ast.h:2890
struct pm_node * value
ClassVariableOrWriteNode::value.
Definition ast.h:2905
ClassVariableReadNode.
Definition ast.h:2920
pm_constant_id_t name
ClassVariableReadNode::name.
Definition ast.h:2934
ClassVariableTargetNode.
Definition ast.h:2949
pm_constant_id_t name
ClassVariableTargetNode::name.
Definition ast.h:2957
ClassVariableWriteNode.
Definition ast.h:2972
struct pm_node * value
ClassVariableWriteNode::value.
Definition ast.h:3009
pm_constant_id_t name
ClassVariableWriteNode::name.
Definition ast.h:2986
ConstantAndWriteNode.
Definition ast.h:3034
pm_location_t name_loc
ConstantAndWriteNode::name_loc.
Definition ast.h:3047
pm_constant_id_t name
ConstantAndWriteNode::name.
Definition ast.h:3042
struct pm_node * value
ConstantAndWriteNode::value.
Definition ast.h:3057
A list of constant IDs.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
ConstantOperatorWriteNode.
Definition ast.h:3072
pm_constant_id_t name
ConstantOperatorWriteNode::name.
Definition ast.h:3080
pm_location_t name_loc
ConstantOperatorWriteNode::name_loc.
Definition ast.h:3085
pm_constant_id_t binary_operator
ConstantOperatorWriteNode::binary_operator.
Definition ast.h:3100
struct pm_node * value
ConstantOperatorWriteNode::value.
Definition ast.h:3095
ConstantOrWriteNode.
Definition ast.h:3115
pm_location_t name_loc
ConstantOrWriteNode::name_loc.
Definition ast.h:3128
pm_constant_id_t name
ConstantOrWriteNode::name.
Definition ast.h:3123
struct pm_node * value
ConstantOrWriteNode::value.
Definition ast.h:3138
ConstantPathAndWriteNode.
Definition ast.h:3153
struct pm_constant_path_node * target
ConstantPathAndWriteNode::target.
Definition ast.h:3161
struct pm_node * value
ConstantPathAndWriteNode::value.
Definition ast.h:3171
ConstantPathNode.
Definition ast.h:3186
pm_constant_id_t name
ConstantPathNode::name.
Definition ast.h:3212
struct pm_node * parent
ConstantPathNode::parent.
Definition ast.h:3205
ConstantPathOperatorWriteNode.
Definition ast.h:3253
struct pm_constant_path_node * target
ConstantPathOperatorWriteNode::target.
Definition ast.h:3261
struct pm_node * value
ConstantPathOperatorWriteNode::value.
Definition ast.h:3271
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode::binary_operator.
Definition ast.h:3276
ConstantPathOrWriteNode.
Definition ast.h:3291
struct pm_node * value
ConstantPathOrWriteNode::value.
Definition ast.h:3309
struct pm_constant_path_node * target
ConstantPathOrWriteNode::target.
Definition ast.h:3299
ConstantPathTargetNode.
Definition ast.h:3324
struct pm_node * parent
ConstantPathTargetNode::parent.
Definition ast.h:3332
pm_constant_id_t name
ConstantPathTargetNode::name.
Definition ast.h:3337
ConstantPathWriteNode.
Definition ast.h:3368
struct pm_constant_path_node * target
ConstantPathWriteNode::target.
Definition ast.h:3384
struct pm_node * value
ConstantPathWriteNode::value.
Definition ast.h:3404
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
ConstantReadNode.
Definition ast.h:3419
pm_node_t base
The embedded base node.
Definition ast.h:3421
pm_constant_id_t name
ConstantReadNode::name.
Definition ast.h:3433
A constant in the pool which effectively stores a string.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.
ConstantTargetNode.
Definition ast.h:3448
pm_constant_id_t name
ConstantTargetNode::name.
Definition ast.h:3456
ConstantWriteNode.
Definition ast.h:3471
struct pm_node * value
ConstantWriteNode::value.
Definition ast.h:3508
pm_constant_id_t name
ConstantWriteNode::name.
Definition ast.h:3485
DefNode.
Definition ast.h:3534
struct pm_parameters_node * parameters
DefNode::parameters.
Definition ast.h:3557
pm_constant_id_t name
DefNode::name.
Definition ast.h:3542
struct pm_node * body
DefNode::body.
Definition ast.h:3562
struct pm_node * receiver
DefNode::receiver.
Definition ast.h:3552
pm_node_t base
The embedded base node.
Definition ast.h:3536
pm_constant_id_list_t locals
DefNode::locals.
Definition ast.h:3567
DefinedNode.
Definition ast.h:3612
struct pm_node * value
DefinedNode::value.
Definition ast.h:3625
This struct represents a diagnostic generated during parsing.
Definition diagnostic.h:363
pm_location_t location
The location of the diagnostic in the source.
Definition diagnostic.h:368
const char * message
The message associated with the diagnostic.
Definition diagnostic.h:374
pm_list_node_t node
The embedded base node.
Definition diagnostic.h:365
uint8_t level
The level of the diagnostic, see pm_error_level_t and pm_warning_level_t for possible values.
Definition diagnostic.h:387
ElseNode.
Definition ast.h:3650
struct pm_statements_node * statements
ElseNode::statements.
Definition ast.h:3663
EmbeddedStatementsNode.
Definition ast.h:3683
struct pm_statements_node * statements
EmbeddedStatementsNode::statements.
Definition ast.h:3696
EmbeddedVariableNode.
Definition ast.h:3716
struct pm_node * variable
EmbeddedVariableNode::variable.
Definition ast.h:3729
This struct defines the functions necessary to implement the encoding interface so we can determine h...
Definition encoding.h:23
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
Definition encoding.h:29
const char * name
The name of the encoding.
Definition encoding.h:56
EnsureNode.
Definition ast.h:3748
struct pm_statements_node * statements
EnsureNode::statements.
Definition ast.h:3761
FindPatternNode.
Definition ast.h:3808
struct pm_node * constant
FindPatternNode::constant.
Definition ast.h:3821
struct pm_node * right
FindPatternNode::right.
Definition ast.h:3860
struct pm_node_list requireds
FindPatternNode::requireds.
Definition ast.h:3847
struct pm_splat_node * left
FindPatternNode::left.
Definition ast.h:3834
FlipFlopNode.
Definition ast.h:3904
pm_node_t base
The embedded base node.
Definition ast.h:3906
struct pm_node * left
FlipFlopNode::left.
Definition ast.h:3912
struct pm_node * right
FlipFlopNode::right.
Definition ast.h:3917
FloatNode.
Definition ast.h:3937
double value
FloatNode::value.
Definition ast.h:3947
ForNode.
Definition ast.h:3962
struct pm_statements_node * statements
ForNode::statements.
Definition ast.h:3997
struct pm_node * collection
ForNode::collection.
Definition ast.h:3985
ForwardingSuperNode.
Definition ast.h:4091
struct pm_block_node * block
ForwardingSuperNode::block.
Definition ast.h:4099
GlobalVariableAndWriteNode.
Definition ast.h:4114
struct pm_node * value
GlobalVariableAndWriteNode::value.
Definition ast.h:4137
pm_constant_id_t name
GlobalVariableAndWriteNode::name.
Definition ast.h:4122
GlobalVariableOperatorWriteNode.
Definition ast.h:4152
pm_constant_id_t name
GlobalVariableOperatorWriteNode::name.
Definition ast.h:4160
pm_constant_id_t binary_operator
GlobalVariableOperatorWriteNode::binary_operator.
Definition ast.h:4180
struct pm_node * value
GlobalVariableOperatorWriteNode::value.
Definition ast.h:4175
GlobalVariableOrWriteNode.
Definition ast.h:4195
pm_constant_id_t name
GlobalVariableOrWriteNode::name.
Definition ast.h:4203
struct pm_node * value
GlobalVariableOrWriteNode::value.
Definition ast.h:4218
GlobalVariableReadNode.
Definition ast.h:4233
pm_constant_id_t name
GlobalVariableReadNode::name.
Definition ast.h:4247
GlobalVariableTargetNode.
Definition ast.h:4262
pm_constant_id_t name
GlobalVariableTargetNode::name.
Definition ast.h:4270
GlobalVariableWriteNode.
Definition ast.h:4285
struct pm_node * value
GlobalVariableWriteNode::value.
Definition ast.h:4322
pm_constant_id_t name
GlobalVariableWriteNode::name.
Definition ast.h:4299
HashNode.
Definition ast.h:4347
struct pm_node_list elements
HashNode::elements.
Definition ast.h:4373
HashPatternNode.
Definition ast.h:4407
struct pm_node_list elements
HashPatternNode::elements.
Definition ast.h:4433
struct pm_node * rest
HashPatternNode::rest.
Definition ast.h:4449
struct pm_node * constant
HashPatternNode::constant.
Definition ast.h:4423
IfNode.
Definition ast.h:4496
struct pm_node * predicate
IfNode::predicate.
Definition ast.h:4529
struct pm_statements_node * statements
IfNode::statements.
Definition ast.h:4556
ImaginaryNode.
Definition ast.h:4602
struct pm_node * numeric
ImaginaryNode::numeric.
Definition ast.h:4610
ImplicitNode.
Definition ast.h:4631
struct pm_node * value
ImplicitNode::value.
Definition ast.h:4639
InNode.
Definition ast.h:4681
struct pm_statements_node * statements
InNode::statements.
Definition ast.h:4694
struct pm_node * pattern
InNode::pattern.
Definition ast.h:4689
IndexAndWriteNode.
Definition ast.h:4725
struct pm_arguments_node * arguments
IndexAndWriteNode::arguments.
Definition ast.h:4748
struct pm_node * receiver
IndexAndWriteNode::receiver.
Definition ast.h:4733
struct pm_block_argument_node * block
IndexAndWriteNode::block.
Definition ast.h:4758
struct pm_node * value
IndexAndWriteNode::value.
Definition ast.h:4768
IndexOperatorWriteNode.
Definition ast.h:4789
struct pm_block_argument_node * block
IndexOperatorWriteNode::block.
Definition ast.h:4822
struct pm_node * value
IndexOperatorWriteNode::value.
Definition ast.h:4837
struct pm_arguments_node * arguments
IndexOperatorWriteNode::arguments.
Definition ast.h:4812
pm_constant_id_t binary_operator
IndexOperatorWriteNode::binary_operator.
Definition ast.h:4827
struct pm_node * receiver
IndexOperatorWriteNode::receiver.
Definition ast.h:4797
IndexOrWriteNode.
Definition ast.h:4858
struct pm_block_argument_node * block
IndexOrWriteNode::block.
Definition ast.h:4891
struct pm_node * receiver
IndexOrWriteNode::receiver.
Definition ast.h:4866
struct pm_node * value
IndexOrWriteNode::value.
Definition ast.h:4901
struct pm_arguments_node * arguments
IndexOrWriteNode::arguments.
Definition ast.h:4881
IndexTargetNode.
Definition ast.h:4930
struct pm_node * receiver
IndexTargetNode::receiver.
Definition ast.h:4938
struct pm_arguments_node * arguments
IndexTargetNode::arguments.
Definition ast.h:4948
struct pm_block_argument_node * block
IndexTargetNode::block.
Definition ast.h:4958
InstanceVariableAndWriteNode.
Definition ast.h:4973
struct pm_node * value
InstanceVariableAndWriteNode::value.
Definition ast.h:4996
pm_constant_id_t name
InstanceVariableAndWriteNode::name.
Definition ast.h:4981
InstanceVariableOperatorWriteNode.
Definition ast.h:5011
struct pm_node * value
InstanceVariableOperatorWriteNode::value.
Definition ast.h:5034
pm_constant_id_t binary_operator
InstanceVariableOperatorWriteNode::binary_operator.
Definition ast.h:5039
pm_constant_id_t name
InstanceVariableOperatorWriteNode::name.
Definition ast.h:5019
InstanceVariableOrWriteNode.
Definition ast.h:5054
struct pm_node * value
InstanceVariableOrWriteNode::value.
Definition ast.h:5077
pm_constant_id_t name
InstanceVariableOrWriteNode::name.
Definition ast.h:5062
InstanceVariableReadNode.
Definition ast.h:5092
pm_constant_id_t name
InstanceVariableReadNode::name.
Definition ast.h:5106
InstanceVariableTargetNode.
Definition ast.h:5121
pm_constant_id_t name
InstanceVariableTargetNode::name.
Definition ast.h:5129
InstanceVariableWriteNode.
Definition ast.h:5144
pm_constant_id_t name
InstanceVariableWriteNode::name.
Definition ast.h:5158
struct pm_node * value
InstanceVariableWriteNode::value.
Definition ast.h:5181
IntegerNode.
Definition ast.h:5212
pm_integer_t value
IntegerNode::value.
Definition ast.h:5222
A structure represents an arbitrary-sized integer.
Definition pm_integer.h:20
size_t length
The number of allocated values.
Definition pm_integer.h:25
uint32_t value
Embedded value for small integer.
Definition pm_integer.h:36
uint32_t * values
List of 32-bit integers.
Definition pm_integer.h:30
bool negative
Whether or not the integer is negative.
Definition pm_integer.h:42
InterpolatedMatchLastLineNode.
Definition ast.h:5250
InterpolatedRegularExpressionNode.
Definition ast.h:5296
InterpolatedStringNode.
Definition ast.h:5333
struct pm_node_list parts
InterpolatedStringNode::parts.
Definition ast.h:5346
InterpolatedSymbolNode.
Definition ast.h:5366
struct pm_node_list parts
InterpolatedSymbolNode::parts.
Definition ast.h:5379
InterpolatedXStringNode.
Definition ast.h:5399
struct pm_node_list parts
InterpolatedXStringNode::parts.
Definition ast.h:5412
KeywordHashNode.
Definition ast.h:5471
struct pm_node_list elements
KeywordHashNode::elements.
Definition ast.h:5479
KeywordRestParameterNode.
Definition ast.h:5498
LambdaNode.
Definition ast.h:5531
struct pm_node * body
LambdaNode::body.
Definition ast.h:5564
pm_location_t opening_loc
LambdaNode::opening_loc.
Definition ast.h:5549
struct pm_node * parameters
LambdaNode::parameters.
Definition ast.h:5559
pm_location_t operator_loc
LambdaNode::operator_loc.
Definition ast.h:5544
pm_constant_id_list_t locals
LambdaNode::locals.
Definition ast.h:5539
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
This struct represents an abstract linked list that provides common functionality.
Definition pm_list.h:46
struct pm_list_node * next
A pointer to the next node in the list.
Definition pm_list.h:48
This represents the overall linked list.
Definition pm_list.h:55
pm_list_node_t * tail
A pointer to the tail of the list.
Definition pm_list.h:63
pm_list_node_t * head
A pointer to the head of the list.
Definition pm_list.h:60
size_t size
The size of the list.
Definition pm_list.h:57
the getlocal and setlocal instructions require two parameters.
LocalVariableAndWriteNode.
Definition ast.h:5579
pm_constant_id_t name
LocalVariableAndWriteNode::name.
Definition ast.h:5602
uint32_t depth
LocalVariableAndWriteNode::depth.
Definition ast.h:5607
struct pm_node * value
LocalVariableAndWriteNode::value.
Definition ast.h:5597
LocalVariableOperatorWriteNode.
Definition ast.h:5622
uint32_t depth
LocalVariableOperatorWriteNode::depth.
Definition ast.h:5655
pm_constant_id_t binary_operator
LocalVariableOperatorWriteNode::binary_operator.
Definition ast.h:5650
struct pm_node * value
LocalVariableOperatorWriteNode::value.
Definition ast.h:5640
pm_constant_id_t name
LocalVariableOperatorWriteNode::name.
Definition ast.h:5645
LocalVariableOrWriteNode.
Definition ast.h:5670
uint32_t depth
LocalVariableOrWriteNode::depth.
Definition ast.h:5698
struct pm_node * value
LocalVariableOrWriteNode::value.
Definition ast.h:5688
pm_constant_id_t name
LocalVariableOrWriteNode::name.
Definition ast.h:5693
LocalVariableReadNode.
Definition ast.h:5713
uint32_t depth
LocalVariableReadNode::depth.
Definition ast.h:5744
pm_constant_id_t name
LocalVariableReadNode::name.
Definition ast.h:5731
LocalVariableTargetNode.
Definition ast.h:5762
uint32_t depth
LocalVariableTargetNode::depth.
Definition ast.h:5775
pm_constant_id_t name
LocalVariableTargetNode::name.
Definition ast.h:5770
LocalVariableWriteNode.
Definition ast.h:5790
struct pm_node * value
LocalVariableWriteNode::value.
Definition ast.h:5844
uint32_t depth
LocalVariableWriteNode::depth.
Definition ast.h:5817
pm_constant_id_t name
LocalVariableWriteNode::name.
Definition ast.h:5804
This represents a range of bytes in the source string to which a node or token corresponds.
Definition ast.h:544
const uint8_t * start
A pointer to the start location of the range in the source.
Definition ast.h:546
const uint8_t * end
A pointer to the end location of the range in the source.
Definition ast.h:549
MatchLastLineNode.
Definition ast.h:5882
MatchPredicateNode.
Definition ast.h:5920
struct pm_node * pattern
MatchPredicateNode::pattern.
Definition ast.h:5933
struct pm_node * value
MatchPredicateNode::value.
Definition ast.h:5928
MatchRequiredNode.
Definition ast.h:5953
struct pm_node * value
MatchRequiredNode::value.
Definition ast.h:5966
struct pm_node * pattern
MatchRequiredNode::pattern.
Definition ast.h:6015
MatchWriteNode.
Definition ast.h:6040
struct pm_node_list targets
MatchWriteNode::targets.
Definition ast.h:6053
struct pm_call_node * call
MatchWriteNode::call.
Definition ast.h:6048
ModuleNode.
Definition ast.h:6083
struct pm_node * constant_path
ModuleNode::constant_path.
Definition ast.h:6101
struct pm_node * body
ModuleNode::body.
Definition ast.h:6106
pm_constant_id_list_t locals
ModuleNode::locals.
Definition ast.h:6091
pm_constant_id_t name
ModuleNode::name.
Definition ast.h:6116
MultiTargetNode.
Definition ast.h:6136
struct pm_node_list lefts
MultiTargetNode::lefts.
Definition ast.h:6154
struct pm_node * rest
MultiTargetNode::rest.
Definition ast.h:6174
struct pm_node_list rights
MultiTargetNode::rights.
Definition ast.h:6184
This is a node in the multi target state linked list.
As we're compiling a multi target, we need to track additional information whenever there is a parent...
MultiWriteNode.
Definition ast.h:6219
struct pm_node * value
MultiWriteNode::value.
Definition ast.h:6307
struct pm_node * rest
MultiWriteNode::rest.
Definition ast.h:6257
struct pm_node_list rights
MultiWriteNode::rights.
Definition ast.h:6267
struct pm_node_list lefts
MultiWriteNode::lefts.
Definition ast.h:6237
A list of offsets of newlines in a string.
const uint8_t * start
A pointer to the start of the source string.
size_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
NextNode.
Definition ast.h:6322
struct pm_arguments_node * arguments
NextNode::arguments.
Definition ast.h:6330
A list of nodes in the source, most often used for lists of children.
Definition ast.h:557
size_t size
The number of nodes in the list.
Definition ast.h:559
struct pm_node ** nodes
The nodes in the list.
Definition ast.h:565
This compiler defines its own concept of the location of a node.
int32_t line
This is the line number of a node.
uint32_t node_id
This is a unique identifier for the node.
This is the base structure that represents a node in the syntax tree.
Definition ast.h:1068
pm_node_type_t type
This represents the type of the node.
Definition ast.h:1073
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
Definition ast.h:1085
pm_node_flags_t flags
This represents any flags on the node.
Definition ast.h:1079
pm_location_t location
This is the location of the node in the source.
Definition ast.h:1091
NumberedParametersNode.
Definition ast.h:6397
NumberedReferenceReadNode.
Definition ast.h:6420
uint32_t number
NumberedReferenceReadNode::number.
Definition ast.h:6436
OptionalKeywordParameterNode.
Definition ast.h:6455
pm_constant_id_t name
OptionalKeywordParameterNode::name.
Definition ast.h:6463
struct pm_node * value
OptionalKeywordParameterNode::value.
Definition ast.h:6473
OptionalParameterNode.
Definition ast.h:6492
struct pm_node * value
OptionalParameterNode::value.
Definition ast.h:6515
pm_constant_id_t name
OptionalParameterNode::name.
Definition ast.h:6500
The options that can be passed to the parser.
Definition options.h:104
pm_options_version_t version
The version of prism that we should be parsing with.
Definition options.h:150
OrNode.
Definition ast.h:6530
struct pm_node * left
OrNode::left.
Definition ast.h:6546
struct pm_node * right
OrNode::right.
Definition ast.h:6559
ParametersNode.
Definition ast.h:6585
struct pm_node * rest
ParametersNode::rest.
Definition ast.h:6603
struct pm_node_list requireds
ParametersNode::requireds.
Definition ast.h:6593
struct pm_block_parameter_node * block
ParametersNode::block.
Definition ast.h:6623
struct pm_node_list optionals
ParametersNode::optionals.
Definition ast.h:6598
struct pm_node_list posts
ParametersNode::posts.
Definition ast.h:6608
pm_node_t base
The embedded base node.
Definition ast.h:6587
struct pm_node * keyword_rest
ParametersNode::keyword_rest.
Definition ast.h:6618
struct pm_node_list keywords
ParametersNode::keywords.
Definition ast.h:6613
ParenthesesNode.
Definition ast.h:6641
struct pm_node * body
ParenthesesNode::body.
Definition ast.h:6649
The format that will be used to format the errors into the output.
size_t blank_prefix_length
The length of the blank prefix.
const char * blank_prefix
The prefix that will be used for blank lines.
size_t divider_length
The length of the divider.
const char * number_prefix
The prefix that will be used for line numbers.
const char * divider
The divider that will be used between sections of source code.
An error that is going to be formatted into the output.
pm_diagnostic_t * error
A pointer to the diagnostic that was generated during parsing.
uint32_t column_end
The column end of the diagnostic message.
int32_t line
The start line of the diagnostic message.
uint32_t column_start
The column start of the diagnostic message.
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_string_t input
The input that represents the source to be parsed.
pm_parser_t parser
The parser that will do the actual parsing.
pm_options_t options
The options that will be passed to the parser.
This struct represents the overall parser.
Definition parser.h:640
const pm_encoding_t * encoding
The encoding functions for the current file is attached to the parser as it's parsing so that it can ...
Definition parser.h:755
const uint8_t * end
The pointer to the end of the source.
Definition parser.h:694
pm_constant_pool_t constant_pool
This constant pool keeps all of the constants defined throughout the file so that we can reference th...
Definition parser.h:786
const uint8_t * start
The pointer to the start of the source.
Definition parser.h:691
pm_list_t error_list
The list of errors that have been found while parsing.
Definition parser.h:734
pm_list_t warning_list
The list of warnings that have been found while parsing.
Definition parser.h:731
int32_t start_line
The line number at the start of the parse.
Definition parser.h:809
pm_string_t filepath
This is the path of the file being parsed.
Definition parser.h:780
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
Definition parser.h:789
PinnedExpressionNode.
Definition ast.h:6674
PinnedVariableNode.
Definition ast.h:6732
struct pm_node * variable
PinnedVariableNode::variable.
Definition ast.h:6745
PostExecutionNode.
Definition ast.h:6770
struct pm_statements_node * statements
PostExecutionNode::statements.
Definition ast.h:6778
PreExecutionNode.
Definition ast.h:6808
struct pm_statements_node * statements
PreExecutionNode::statements.
Definition ast.h:6816
ProgramNode.
Definition ast.h:6843
struct pm_statements_node * statements
ProgramNode::statements.
Definition ast.h:6856
RangeNode.
Definition ast.h:6877
struct pm_node * right
RangeNode::right.
Definition ast.h:6907
struct pm_node * left
RangeNode::left.
Definition ast.h:6893
RationalNode.
Definition ast.h:6935
pm_integer_t denominator
RationalNode::denominator.
Definition ast.h:6956
pm_integer_t numerator
RationalNode::numerator.
Definition ast.h:6947
RegularExpressionNode.
Definition ast.h:7002
RequiredKeywordParameterNode.
Definition ast.h:7044
RequiredParameterNode.
Definition ast.h:7076
pm_constant_id_t name
RequiredParameterNode::name.
Definition ast.h:7084
RescueModifierNode.
Definition ast.h:7099
struct pm_node * rescue_expression
RescueModifierNode::rescue_expression.
Definition ast.h:7117
struct pm_node * expression
RescueModifierNode::expression.
Definition ast.h:7107
RescueNode.
Definition ast.h:7137
struct pm_rescue_node * subsequent
RescueNode::subsequent.
Definition ast.h:7175
struct pm_node * reference
RescueNode::reference.
Definition ast.h:7160
struct pm_node_list exceptions
RescueNode::exceptions.
Definition ast.h:7150
struct pm_statements_node * statements
RescueNode::statements.
Definition ast.h:7170
RestParameterNode.
Definition ast.h:7194
ReturnNode.
Definition ast.h:7245
struct pm_arguments_node * arguments
ReturnNode::arguments.
Definition ast.h:7258
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
struct iseq_link_anchor * pre_execution_anchor
This will only be set on the top-level scope node.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
ShareableConstantNode.
Definition ast.h:7297
struct pm_node * write
ShareableConstantNode::write.
Definition ast.h:7307
pm_node_t base
The embedded base node.
Definition ast.h:7299
SingletonClassNode.
Definition ast.h:7322
pm_constant_id_list_t locals
SingletonClassNode::locals.
Definition ast.h:7330
struct pm_node * expression
SingletonClassNode::expression.
Definition ast.h:7345
struct pm_node * body
SingletonClassNode::body.
Definition ast.h:7350
SourceFileNode.
Definition ast.h:7394
pm_string_t filepath
SourceFileNode::filepath.
Definition ast.h:7404
SplatNode.
Definition ast.h:7437
struct pm_node * expression
SplatNode::expression.
Definition ast.h:7450
StatementsNode.
Definition ast.h:7465
struct pm_node_list body
StatementsNode::body.
Definition ast.h:7473
pm_node_t base
The embedded base node.
Definition ast.h:7467
StringNode.
Definition ast.h:7500
pm_string_t unescaped
StringNode::unescaped.
Definition ast.h:7523
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
SuperNode.
Definition ast.h:7541
struct pm_arguments_node * arguments
SuperNode::arguments.
Definition ast.h:7559
struct pm_node * block
SuperNode::block.
Definition ast.h:7569
SymbolNode.
Definition ast.h:7592
pm_string_t unescaped
SymbolNode::unescaped.
Definition ast.h:7615
pm_node_t base
The embedded base node.
Definition ast.h:7594
UndefNode.
Definition ast.h:7648
struct pm_node_list names
UndefNode::names.
Definition ast.h:7656
UnlessNode.
Definition ast.h:7679
struct pm_statements_node * statements
UnlessNode::statements.
Definition ast.h:7729
struct pm_node * predicate
UnlessNode::predicate.
Definition ast.h:7708
struct pm_else_node * else_clause
UnlessNode::else_clause.
Definition ast.h:7739
UntilNode.
Definition ast.h:7770
pm_node_t base
The embedded base node.
Definition ast.h:7772
WhenNode.
Definition ast.h:7815
WhileNode.
Definition ast.h:7859
pm_node_t base
The embedded base node.
Definition ast.h:7861
XStringNode.
Definition ast.h:7906
pm_string_t unescaped
XStringNode::unescaped.
Definition ast.h:7929
YieldNode.
Definition ast.h:7944
struct pm_arguments_node * arguments
YieldNode::arguments.
Definition ast.h:7962
struct rb_iseq_constant_body::@154 param
parameter information
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376