From d293d9e1917d28bf77f690e3c944b6ad876efd0c Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 28 Sep 2023 20:44:45 +0900 Subject: [PATCH] Expand pattern_info struct into ARYPTN Node and FNDPTN Node --- ast.c | 14 ++++++-------- compile.c | 36 +++++++++++++++++------------------- node.c | 6 ------ node_dump.c | 24 ++++++++++++------------ parse.y | 49 +++++++++++++++++++++---------------------------- rubyparser.h | 20 ++++++-------------- 6 files changed, 62 insertions(+), 87 deletions(-) diff --git a/ast.c b/ast.c index b63b5c3173..5ead8aad67 100644 --- a/ast.c +++ b/ast.c @@ -648,23 +648,21 @@ node_children(rb_ast_t *ast, const NODE *node) } case NODE_ARYPTN: { - struct rb_ary_pattern_info *apinfo = RNODE_ARYPTN(node)->nd_apinfo; - VALUE rest = rest_arg(ast, apinfo->rest_arg); + VALUE rest = rest_arg(ast, RNODE_ARYPTN(node)->rest_arg); return rb_ary_new_from_args(4, NEW_CHILD(ast, RNODE_ARYPTN(node)->nd_pconst), - NEW_CHILD(ast, apinfo->pre_args), + NEW_CHILD(ast, RNODE_ARYPTN(node)->pre_args), rest, - NEW_CHILD(ast, apinfo->post_args)); + NEW_CHILD(ast, RNODE_ARYPTN(node)->post_args)); } case NODE_FNDPTN: { - struct rb_fnd_pattern_info *fpinfo = RNODE_FNDPTN(node)->nd_fpinfo; - VALUE pre_rest = rest_arg(ast, fpinfo->pre_rest_arg); - VALUE post_rest = rest_arg(ast, fpinfo->post_rest_arg); + VALUE pre_rest = rest_arg(ast, RNODE_FNDPTN(node)->pre_rest_arg); + VALUE post_rest = rest_arg(ast, RNODE_FNDPTN(node)->post_rest_arg); return rb_ary_new_from_args(4, NEW_CHILD(ast, RNODE_FNDPTN(node)->nd_pconst), pre_rest, - NEW_CHILD(ast, fpinfo->args), + NEW_CHILD(ast, RNODE_FNDPTN(node)->args), post_rest); } case NODE_HSHPTN: diff --git a/compile.c b/compile.c index 8cb1209138..2fd7cc62d1 100644 --- a/compile.c +++ b/compile.c @@ -6326,14 +6326,13 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c * match_failed: * goto unmatched */ - struct rb_ary_pattern_info *apinfo = RNODE_ARYPTN(node)->nd_apinfo; - const NODE *args = apinfo->pre_args; - const int pre_args_num = apinfo->pre_args ? rb_long2int(RNODE_LIST(apinfo->pre_args)->as.nd_alen) : 0; - const int post_args_num = apinfo->post_args ? rb_long2int(RNODE_LIST(apinfo->post_args)->as.nd_alen) : 0; + const NODE *args = RNODE_ARYPTN(node)->pre_args; + const int pre_args_num = RNODE_ARYPTN(node)->pre_args ? rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0; + const int post_args_num = RNODE_ARYPTN(node)->post_args ? rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0; const int min_argc = pre_args_num + post_args_num; - const int use_rest_num = apinfo->rest_arg && (NODE_NAMED_REST_P(apinfo->rest_arg) || - (!NODE_NAMED_REST_P(apinfo->rest_arg) && post_args_num > 0)); + const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) || + (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0)); LABEL *match_failed, *type_error, *deconstruct, *deconstructed; int i; @@ -6357,10 +6356,10 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c ADD_INSN(ret, line_node, dup); ADD_SEND(ret, line_node, idLength, INT2FIX(0)); ADD_INSN1(ret, line_node, putobject, INT2FIX(min_argc)); - ADD_SEND(ret, line_node, apinfo->rest_arg ? idGE : idEq, INT2FIX(1)); // (1) + ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq, INT2FIX(1)); // (1) if (in_single_pattern) { CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node, - apinfo->rest_arg ? rb_fstring_lit("%p length mismatch (given %p, expected %p+)") : + RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit("%p length mismatch (given %p, expected %p+)") : rb_fstring_lit("%p length mismatch (given %p, expected %p)"), INT2FIX(min_argc), base_index + 1 /* (1) */)); } @@ -6374,8 +6373,8 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c args = RNODE_LIST(args)->nd_next; } - if (apinfo->rest_arg) { - if (NODE_NAMED_REST_P(apinfo->rest_arg)) { + if (RNODE_ARYPTN(node)->rest_arg) { + if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) { ADD_INSN(ret, line_node, dup); ADD_INSN1(ret, line_node, putobject, INT2FIX(pre_args_num)); ADD_INSN1(ret, line_node, topn, INT2FIX(1)); @@ -6385,7 +6384,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c ADD_INSN1(ret, line_node, setn, INT2FIX(4)); ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (3) - CHECK(iseq_compile_pattern_match(iseq, ret, apinfo->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (3) */, false)); + CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (3) */, false)); } else { if (post_args_num > 0) { @@ -6399,7 +6398,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c } } - args = apinfo->post_args; + args = RNODE_ARYPTN(node)->post_args; for (i = 0; i < post_args_num; i++) { ADD_INSN(ret, line_node, dup); @@ -6487,9 +6486,8 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c * match_failed: * goto unmatched */ - struct rb_fnd_pattern_info *fpinfo = RNODE_FNDPTN(node)->nd_fpinfo; - const NODE *args = fpinfo->args; - const int args_num = fpinfo->args ? rb_long2int(RNODE_LIST(fpinfo->args)->as.nd_alen) : 0; + const NODE *args = RNODE_FNDPTN(node)->args; + const int args_num = RNODE_FNDPTN(node)->args ? rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0; LABEL *match_failed, *type_error, *deconstruct, *deconstructed; match_failed = NEW_LABEL(line); @@ -6546,21 +6544,21 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c args = RNODE_LIST(args)->nd_next; } - if (NODE_NAMED_REST_P(fpinfo->pre_rest_arg)) { + if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) { ADD_INSN1(ret, line_node, topn, INT2FIX(3)); ADD_INSN1(ret, line_node, putobject, INT2FIX(0)); ADD_INSN1(ret, line_node, topn, INT2FIX(2)); ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (6) - CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3), (4), (6) */, false)); + CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3), (4), (6) */, false)); } - if (NODE_NAMED_REST_P(fpinfo->post_rest_arg)) { + if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) { ADD_INSN1(ret, line_node, topn, INT2FIX(3)); ADD_INSN1(ret, line_node, topn, INT2FIX(1)); ADD_INSN1(ret, line_node, putobject, INT2FIX(args_num)); ADD_SEND(ret, line_node, idPLUS, INT2FIX(1)); ADD_INSN1(ret, line_node, topn, INT2FIX(3)); ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (7) - CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3),(4), (7) */, false)); + CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3),(4), (7) */, false)); } ADD_INSNL(ret, line_node, jump, find_succeeded); diff --git a/node.c b/node.c index 19fe84c672..975ed9264f 100644 --- a/node.c +++ b/node.c @@ -175,12 +175,6 @@ free_ast_value(rb_ast_t *ast, void *ctx, NODE *node) case NODE_ARGS: xfree(RNODE_ARGS(node)->nd_ainfo); break; - case NODE_ARYPTN: - xfree(RNODE_ARYPTN(node)->nd_apinfo); - break; - case NODE_FNDPTN: - xfree(RNODE_FNDPTN(node)->nd_fpinfo); - break; } } diff --git a/node_dump.c b/node_dump.c index 889fd521d8..56a6dd6dc5 100644 --- a/node_dump.c +++ b/node_dump.c @@ -1054,35 +1054,35 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node) ANN("array pattern"); ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)"); F_NODE(nd_pconst, RNODE_ARYPTN, "constant"); - F_NODE(nd_apinfo->pre_args, RNODE_ARYPTN, "pre arguments"); - if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->nd_apinfo->rest_arg)) { - F_NODE(nd_apinfo->rest_arg, RNODE_ARYPTN, "rest argument"); + F_NODE(pre_args, RNODE_ARYPTN, "pre arguments"); + if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) { + F_NODE(rest_arg, RNODE_ARYPTN, "rest argument"); } else { - F_MSG(nd_apinfo->rest_arg, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); + F_MSG(rest_arg, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); } LAST_NODE; - F_NODE(nd_apinfo->post_args, RNODE_ARYPTN, "post arguments"); + F_NODE(post_args, RNODE_ARYPTN, "post arguments"); return; case NODE_FNDPTN: ANN("find pattern"); ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])"); F_NODE(nd_pconst, RNODE_FNDPTN, "constant"); - if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->nd_fpinfo->pre_rest_arg)) { - F_NODE(nd_fpinfo->pre_rest_arg, RNODE_FNDPTN, "pre rest argument"); + if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) { + F_NODE(pre_rest_arg, RNODE_FNDPTN, "pre rest argument"); } else { - F_MSG(nd_fpinfo->pre_rest_arg, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); + F_MSG(pre_rest_arg, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); } - F_NODE(nd_fpinfo->args, RNODE_FNDPTN, "arguments"); + F_NODE(args, RNODE_FNDPTN, "arguments"); LAST_NODE; - if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->nd_fpinfo->post_rest_arg)) { - F_NODE(nd_fpinfo->post_rest_arg, RNODE_FNDPTN, "post rest argument"); + if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) { + F_NODE(post_rest_arg, RNODE_FNDPTN, "post rest argument"); } else { - F_MSG(nd_fpinfo->post_rest_arg, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); + F_MSG(post_rest_arg, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)"); } return; diff --git a/parse.y b/parse.y index 88b6d07bc5..ca983e946a 100644 --- a/parse.y +++ b/parse.y @@ -973,9 +973,9 @@ static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_ static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc); static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, const YYLTYPE *loc); -static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, const YYLTYPE *loc); +static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc); static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc); -static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, const YYLTYPE *loc); +static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc); static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc); #define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc) @@ -1074,9 +1074,9 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE #define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc) #define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc) #define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc) -#define NEW_ARYPTN(loc) (NODE *)rb_node_aryptn_new(p,loc) +#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc) #define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc) -#define NEW_FNDPTN(loc) (NODE *)rb_node_fndptn_new(p,loc) +#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc) #define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc) #endif @@ -12033,11 +12033,13 @@ rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd } static rb_node_aryptn_t * -rb_node_aryptn_new(struct parser_params *p, const YYLTYPE *loc) +rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc) { rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc); n->nd_pconst = 0; - n->nd_apinfo = 0; + n->pre_args = pre_args; + n->rest_arg = rest_arg; + n->post_args = post_args; return n; } @@ -12054,11 +12056,13 @@ rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, N } static rb_node_fndptn_t * -rb_node_fndptn_new(struct parser_params *p, const YYLTYPE *loc) +rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc) { rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc); n->nd_pconst = 0; - n->nd_fpinfo = 0; + n->pre_rest_arg = pre_rest_arg; + n->args = args; + n->post_rest_arg = post_rest_arg; return n; } @@ -14337,17 +14341,15 @@ args_with_numbered(struct parser_params *p, NODE *args, int max_numparam) static NODE* new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc) { - struct rb_ary_pattern_info *apinfo = RNODE_ARYPTN(aryptn)->nd_apinfo; - RNODE_ARYPTN(aryptn)->nd_pconst = constant; if (pre_arg) { NODE *pre_args = NEW_LIST(pre_arg, loc); - if (apinfo->pre_args) { - apinfo->pre_args = list_concat(pre_args, apinfo->pre_args); + if (RNODE_ARYPTN(aryptn)->pre_args) { + RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args); } else { - apinfo->pre_args = pre_args; + RNODE_ARYPTN(aryptn)->pre_args = pre_args; } } return aryptn; @@ -14357,20 +14359,14 @@ static NODE* new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc) { int saved_line = p->ruby_sourceline; - NODE *node = NEW_ARYPTN(loc); - struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info); - RNODE_ARYPTN(node)->nd_apinfo = apinfo; - - apinfo->pre_args = pre_args; if (has_rest) { - apinfo->rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST; + rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST; } else { - apinfo->rest_arg = NULL; + rest_arg = NULL; } - - apinfo->post_args = post_args; + NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc); p->ruby_sourceline = saved_line; return node; @@ -14388,13 +14384,10 @@ static NODE* new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc) { int saved_line = p->ruby_sourceline; - NODE *node = NEW_FNDPTN(loc); - struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info); - RNODE_FNDPTN(node)->nd_fpinfo = fpinfo; - fpinfo->pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST; - fpinfo->args = args; - fpinfo->post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST; + pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST; + post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST; + NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc); p->ruby_sourceline = saved_line; return node; diff --git a/rubyparser.h b/rubyparser.h index 4e22f8d350..08c20678ac 100644 --- a/rubyparser.h +++ b/rubyparser.h @@ -963,7 +963,9 @@ typedef struct RNode_ARYPTN { NODE node; struct RNode *nd_pconst; - struct rb_ary_pattern_info *nd_apinfo; + NODE *pre_args; + NODE *rest_arg; + NODE *post_args; } rb_node_aryptn_t; typedef struct RNode_HSHPTN { @@ -978,7 +980,9 @@ typedef struct RNode_FNDPTN { NODE node; struct RNode *nd_pconst; - struct rb_fnd_pattern_info *nd_fpinfo; + NODE *pre_rest_arg; + NODE *args; + NODE *post_rest_arg; } rb_node_fndptn_t; typedef struct RNode_ERROR { @@ -1155,18 +1159,6 @@ struct rb_args_info { unsigned int forwarding: 1; }; -struct rb_ary_pattern_info { - NODE *pre_args; - NODE *rest_arg; - NODE *post_args; -}; - -struct rb_fnd_pattern_info { - NODE *pre_rest_arg; - NODE *args; - NODE *post_rest_arg; -}; - typedef struct node_buffer_struct node_buffer_t; /* T_IMEMO/ast */ typedef struct rb_ast_body_struct {