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

Remove ruby object from string nodes #9892

Merged
merged 1 commit into from
Feb 9, 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
5 changes: 3 additions & 2 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,10 @@ node_children(rb_ast_t *ast, const NODE *node)
return rb_ary_new_from_node_args(ast, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
case NODE_MATCH:
case NODE_LIT:
return rb_ary_new_from_args(1, RNODE_LIT(node)->nd_lit);
case NODE_STR:
case NODE_XSTR:
return rb_ary_new_from_args(1, RNODE_LIT(node)->nd_lit);
return rb_ary_new_from_args(1, rb_node_str_string_val(node));
case NODE_INTEGER:
return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
case NODE_FLOAT:
Expand All @@ -579,7 +580,7 @@ node_children(rb_ast_t *ast, const NODE *node)
head = NEW_CHILD(ast, n->nd_head);
next = NEW_CHILD(ast, n->nd_next);
}
return rb_ary_new_from_args(3, RNODE_DSTR(node)->nd_lit, head, next);
return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
}
case NODE_SYM:
return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
Expand Down
25 changes: 12 additions & 13 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ get_string_value(const NODE *node)
{
switch (nd_type(node)) {
case NODE_STR:
return RNODE_STR(node)->nd_lit;
return rb_node_str_string_val(node);
case NODE_FILE:
return rb_node_file_path_val(node);
default:
Expand Down Expand Up @@ -4310,7 +4310,7 @@ static int
compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int *cntp)
{
const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
VALUE lit = RNODE_DSTR(node)->nd_lit;
VALUE lit = rb_node_dstr_string_val(node);
LINK_ELEMENT *first_lit = 0;
int cnt = 0;

Expand All @@ -4331,7 +4331,7 @@ compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cons
while (list) {
const NODE *const head = list->nd_head;
if (nd_type_p(head, NODE_STR)) {
lit = rb_fstring(RNODE_STR(head)->nd_lit);
lit = rb_fstring(rb_node_str_string_val(head));
ADD_INSN1(ret, head, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
lit = Qnil;
Expand Down Expand Up @@ -4370,7 +4370,7 @@ compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node)
{
int cnt;
if (!RNODE_DSTR(node)->nd_next) {
VALUE lit = rb_fstring(RNODE_DSTR(node)->nd_lit);
VALUE lit = rb_fstring(rb_node_dstr_string_val(node));
ADD_INSN1(ret, node, putstring, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
Expand All @@ -4387,14 +4387,13 @@ compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, i
int cnt;

if (!RNODE_DREGX(node)->nd_next) {
VALUE match = RNODE_DREGX(node)->nd_lit;
if (RB_TYPE_P(match, T_REGEXP)) {
if (!popped) {
ADD_INSN1(ret, node, putobject, match);
RB_OBJ_WRITTEN(iseq, Qundef, match);
}
return COMPILE_OK;
if (!popped) {
VALUE src = rb_node_dregx_string_val(node);
VALUE match = rb_reg_compile(src, (int)RNODE_DREGX(node)->nd_cflag, NULL, 0);
ADD_INSN1(ret, node, putobject, match);
RB_OBJ_WRITTEN(iseq, Qundef, match);
}
return COMPILE_OK;
}

CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
Expand Down Expand Up @@ -5135,7 +5134,7 @@ rb_node_case_when_optimizable_literal(const NODE *const node)
case NODE_LINE:
return rb_node_line_lineno_val(node);
case NODE_STR:
return rb_fstring(RNODE_STR(node)->nd_lit);
return rb_fstring(rb_node_str_string_val(node));
case NODE_FILE:
return rb_fstring(rb_node_file_path_val(node));
}
Expand Down Expand Up @@ -10364,7 +10363,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
}
case NODE_XSTR:{
ADD_CALL_RECEIVER(ret, node);
VALUE str = rb_fstring(RNODE_XSTR(node)->nd_lit);
VALUE str = rb_fstring(rb_node_str_string_val(node));
ADD_INSN1(ret, node, putobject, str);
RB_OBJ_WRITTEN(iseq, Qundef, str);
ADD_CALL(ret, node, idBackquote, INT2FIX(1));
Expand Down
3 changes: 3 additions & 0 deletions internal/ruby_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ RUBY_SYMBOL_EXPORT_BEGIN
VALUE rb_str_new_parser_string(rb_parser_string_t *str);
RUBY_SYMBOL_EXPORT_END

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_dregx_string_val(const NODE *);
VALUE rb_node_line_lineno_val(const NODE *);
VALUE rb_node_file_path_val(const NODE *);
VALUE rb_node_encoding_val(const NODE *);
Expand Down
38 changes: 20 additions & 18 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,36 @@ struct rb_ast_local_table_link {
static void
parser_string_free(rb_ast_t *ast, rb_parser_string_t *str)
{
if (!str) return;
xfree(str->ptr);
xfree(str);
}

static void
free_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
{
switch (nd_type(node)) {
case NODE_STR:
parser_string_free(ast, RNODE_STR(node)->string);
break;
case NODE_DSTR:
parser_string_free(ast, RNODE_DSTR(node)->string);
break;
case NODE_XSTR:
parser_string_free(ast, RNODE_XSTR(node)->string);
break;
case NODE_DXSTR:
parser_string_free(ast, RNODE_DXSTR(node)->string);
break;
case NODE_SYM:
parser_string_free(ast, RNODE_SYM(node)->string);
break;
case NODE_DSYM:
parser_string_free(ast, RNODE_DSYM(node)->string);
break;
case NODE_DREGX:
parser_string_free(ast, RNODE_DREGX(node)->string);
break;
case NODE_FILE:
parser_string_free(ast, RNODE_FILE(node)->path);
break;
Expand Down Expand Up @@ -251,12 +271,6 @@ nodetype_markable_p(enum node_type type)
switch (type) {
case NODE_MATCH:
case NODE_LIT:
case NODE_STR:
case NODE_XSTR:
case NODE_DSTR:
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DSYM:
return true;
default:
return false;
Expand Down Expand Up @@ -363,12 +377,6 @@ mark_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
switch (nd_type(node)) {
case NODE_MATCH:
case NODE_LIT:
case NODE_STR:
case NODE_XSTR:
case NODE_DSTR:
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DSYM:
rb_gc_mark_movable(RNODE_LIT(node)->nd_lit);
break;
default:
Expand All @@ -386,12 +394,6 @@ update_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
switch (nd_type(node)) {
case NODE_MATCH:
case NODE_LIT:
case NODE_STR:
case NODE_XSTR:
case NODE_DSTR:
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DSYM:
RNODE_LIT(node)->nd_lit = rb_gc_location(RNODE_LIT(node)->nd_lit);
break;
default:
Expand Down
11 changes: 6 additions & 5 deletions node_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,19 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("literal");
ANN("format: [nd_lit]");
ANN("example: :sym, /foo/");
goto lit;
F_LIT(nd_lit, RNODE_LIT, "literal");
return;
case NODE_STR:
ANN("string literal");
ANN("format: [nd_lit]");
ANN("example: 'foo'");
goto lit;
goto str;
case NODE_XSTR:
ANN("xstring literal");
ANN("format: [nd_lit]");
ANN("example: `foo`");
lit:
F_LIT(nd_lit, RNODE_LIT, "literal");
str:
F_VALUE(string, rb_node_str_string_val(node), "literal");
return;

case NODE_INTEGER:
Expand Down Expand Up @@ -777,7 +778,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("format: [nd_lit]");
ANN("example: :\"foo#{ bar }baz\"");
dlit:
F_LIT(nd_lit, RNODE_DSTR, "preceding string");
F_VALUE(string, rb_node_dstr_string_val(node), "preceding string");
if (!RNODE_DSTR(node)->nd_next) return;
F_NODE(nd_next->nd_head, RNODE_DSTR, "interpolation");
LAST_NODE;
Expand Down