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