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