Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NODE_REGX to manage regexp literal #10023

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ node_children(rb_ast_t *ast, const NODE *node)
return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
case NODE_IMAGINARY:
return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
case NODE_REGX:
return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
case NODE_ONCE:
return rb_ary_new_from_node_args(ast, 1, RNODE_ONCE(node)->nd_body);
case NODE_DSTR:
Expand Down
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15931,6 +15931,7 @@ ruby_parser.$(OBJEXT): $(top_srcdir)/internal/fixnum.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/imemo.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/numeric.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/rational.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/re.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/ruby_parser.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/serial.h
ruby_parser.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
Expand Down
20 changes: 19 additions & 1 deletion compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,9 @@ iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *const optargs,
case NODE_SYM:
dv = rb_node_sym_string_val(val_node);
break;
case NODE_REGX:
dv = rb_node_regx_string_val(val_node);
break;
case NODE_LINE:
dv = rb_node_line_lineno_val(val_node);
break;
Expand Down Expand Up @@ -4499,6 +4502,7 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *cond,
case NODE_IMAGINARY: /* NODE_IMAGINARY is always true */
case NODE_TRUE:
case NODE_STR:
case NODE_REGX:
case NODE_ZLIST:
case NODE_LAMBDA:
/* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
Expand Down Expand Up @@ -4702,6 +4706,7 @@ static_literal_node_p(const NODE *node, const rb_iseq_t *iseq, bool hash_key)
switch (nd_type(node)) {
case NODE_LIT:
case NODE_SYM:
case NODE_REGX:
case NODE_LINE:
case NODE_ENCODING:
case NODE_INTEGER:
Expand Down Expand Up @@ -4740,6 +4745,8 @@ static_literal_value(const NODE *node, rb_iseq_t *iseq)
return Qfalse;
case NODE_SYM:
return rb_node_sym_string_val(node);
case NODE_REGX:
return rb_node_regx_string_val(node);
case NODE_LINE:
return rb_node_line_lineno_val(node);
case NODE_ENCODING:
Expand Down Expand Up @@ -5785,6 +5792,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
case NODE_STR:
case NODE_LIT:
case NODE_SYM:
case NODE_REGX:
case NODE_LINE:
case NODE_FILE:
case NODE_ENCODING:
Expand Down Expand Up @@ -7212,6 +7220,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
}
case NODE_LIT:
case NODE_SYM:
case NODE_REGX:
case NODE_LINE:
case NODE_INTEGER:
case NODE_FLOAT:
Expand Down Expand Up @@ -9637,7 +9646,7 @@ compile_match(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, i
INIT_ANCHOR(val);
switch ((int)type) {
case NODE_MATCH:
ADD_INSN1(recv, node, putobject, RNODE_MATCH(node)->nd_lit);
ADD_INSN1(recv, node, putobject, rb_node_regx_string_val(node));
ADD_INSN2(val, node, getspecial, INT2FIX(0),
INT2FIX(0));
break;
Expand Down Expand Up @@ -9799,6 +9808,7 @@ compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
}
else if (nd_type_p(default_value, NODE_LIT) ||
nd_type_p(default_value, NODE_SYM) ||
nd_type_p(default_value, NODE_REGX) ||
nd_type_p(default_value, NODE_LINE) ||
nd_type_p(default_value, NODE_INTEGER) ||
nd_type_p(default_value, NODE_FLOAT) ||
Expand Down Expand Up @@ -10385,6 +10395,14 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
case NODE_EVSTR:
CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
break;
case NODE_REGX:{
if (!popped) {
VALUE lit = rb_node_regx_string_val(node);
ADD_INSN1(ret, node, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
break;
}
case NODE_DREGX:
compile_dregx(iseq, ret, node, popped);
break;
Expand Down
1 change: 1 addition & 0 deletions internal/ruby_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ VALUE rb_str_new_parser_string(rb_parser_string_t *str);
VALUE rb_node_str_string_val(const NODE *);
VALUE rb_node_sym_string_val(const NODE *);
VALUE rb_node_dstr_string_val(const NODE *);
VALUE rb_node_regx_string_val(const NODE *);
VALUE rb_node_dregx_string_val(const NODE *);
VALUE rb_node_line_lineno_val(const NODE *);
VALUE rb_node_file_path_val(const NODE *);
Expand Down
2 changes: 2 additions & 0 deletions misc/lldb_rb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ def inspect(self, val):
self._append_expression("*(struct RNode_DXSTR *) %0#x" % val.GetValueAsUnsigned())
elif nd_type == self.ruby_globals["NODE_EVSTR"]:
self._append_expression("*(struct RNode_EVSTR *) %0#x" % val.GetValueAsUnsigned())
elif nd_type == self.ruby_globals["NODE_REGX"]:
self._append_expression("*(struct RNode_REGX *) %0#x" % val.GetValueAsUnsigned())
elif nd_type == self.ruby_globals["NODE_DREGX"]:
self._append_expression("*(struct RNode_DREGX *) %0#x" % val.GetValueAsUnsigned())
elif nd_type == self.ruby_globals["NODE_ONCE"]:
Expand Down
6 changes: 4 additions & 2 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ free_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
case NODE_SYM:
parser_string_free(ast, RNODE_SYM(node)->string);
break;
case NODE_REGX:
case NODE_MATCH:
parser_string_free(ast, RNODE_REGX(node)->string);
break;
case NODE_DSYM:
parser_string_free(ast, RNODE_DSYM(node)->string);
break;
Expand Down Expand Up @@ -268,7 +272,6 @@ static bool
nodetype_markable_p(enum node_type type)
{
switch (type) {
case NODE_MATCH:
case NODE_LIT:
return true;
default:
Expand Down Expand Up @@ -374,7 +377,6 @@ mark_and_move_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
#endif

switch (nd_type(node)) {
case NODE_MATCH:
case NODE_LIT:
rb_gc_mark_and_move(&RNODE_LIT(node)->nd_lit);
break;
Expand Down
11 changes: 10 additions & 1 deletion node_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("match expression (against $_ implicitly)");
ANN("format: [nd_lit] (in condition)");
ANN("example: if /foo/; foo; end");
F_LIT(nd_lit, RNODE_MATCH, "regexp");
LAST_NODE;
F_VALUE(string, rb_node_regx_string_val(node), "string");
return;

case NODE_MATCH2:
Expand Down Expand Up @@ -750,6 +751,14 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
F_VALUE(val, rb_node_imaginary_literal_val(node), "val");
return;

case NODE_REGX:
ANN("regexp literal");
ANN("format: [string]");
ANN("example: /foo/");
LAST_NODE;
F_VALUE(string, rb_node_regx_string_val(node), "string");
return;

case NODE_ONCE:
ANN("once evaluation");
ANN("format: [nd_body]");
Expand Down