ruby-changes:55045
From: mame <ko1@a...>
Date: Thu, 14 Mar 2019 15:43:56 +0900 (JST)
Subject: [ruby-changes:55045] mame:r67252 (trunk): compile.c: factor out "compile_args" from "compile_array"
mame 2019-03-14 15:43:50 +0900 (Thu, 14 Mar 2019) New Revision: 67252 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67252 Log: compile.c: factor out "compile_args" from "compile_array" compile_array function had three usages: array literal, hash literal, and method arguments. I think the third is completely different than the first and second. For example, method arguments and popped are meaningless; keywords_ptr and flag parameter for array/hash literal is also unused. This change refactors them: a function "compile_args" is created for the third, and removes no longer used parameters of "compile_array". Modified files: trunk/compile.c Index: compile.c =================================================================== --- compile.c (revision 67251) +++ compile.c (revision 67252) @@ -3815,7 +3815,7 @@ compile_branch_condition(rb_iseq_t *iseq https://github.com/ruby/ruby/blob/trunk/compile.c#L3815 } static int -compile_array_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, +compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const root_node, struct rb_call_info_kw_arg **const kw_arg_ptr, unsigned int *flag) @@ -3867,10 +3867,33 @@ compile_array_keyword_arg(rb_iseq_t *ise https://github.com/ruby/ruby/blob/trunk/compile.c#L3867 return FALSE; } +static int +compile_args(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_root, + struct rb_call_info_kw_arg **keywords_ptr, unsigned int *flag) +{ + const NODE *node = node_root; + int len = 0; + + for (; node; len++, node = node->nd_next) { + if (CPDEBUG > 0) { + EXPECT_NODE("compile_args", node, NODE_ARRAY, -1); + } + + if (node->nd_next == NULL /* last node */ && + compile_keyword_arg(iseq, ret, node->nd_head, keywords_ptr, flag)) { + len--; + } + else { + COMPILE_(ret, "array element", node->nd_head, FALSE); + } + } + + return len; +} + enum compile_array_type_t { COMPILE_ARRAY_TYPE_ARRAY, - COMPILE_ARRAY_TYPE_HASH, - COMPILE_ARRAY_TYPE_ARGS + COMPILE_ARRAY_TYPE_HASH }; static inline int @@ -3919,8 +3942,7 @@ static_literal_value(const NODE *node, r https://github.com/ruby/ruby/blob/trunk/compile.c#L3942 static int compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_root, - enum compile_array_type_t type, struct rb_call_info_kw_arg **keywords_ptr, - unsigned int *flag, int popped) + enum compile_array_type_t type, int popped) { const NODE *node = node_root; int line = (int)nd_line(node); @@ -3931,7 +3953,6 @@ compile_array(rb_iseq_t *iseq, LINK_ANCH https://github.com/ruby/ruby/blob/trunk/compile.c#L3953 switch (type) { case COMPILE_ARRAY_TYPE_ARRAY: ADD_INSN1(ret, line, newarray, INT2FIX(0)); break; case COMPILE_ARRAY_TYPE_HASH: ADD_INSN1(ret, line, newhash, INT2FIX(0)); break; - case COMPILE_ARRAY_TYPE_ARGS: /* do nothing */ break; } } } @@ -3951,7 +3972,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCH https://github.com/ruby/ruby/blob/trunk/compile.c#L3972 EXPECT_NODE("compile_array", node, NODE_ARRAY, -1); } - if (type != COMPILE_ARRAY_TYPE_ARRAY && !node->nd_head) { + if (type == COMPILE_ARRAY_TYPE_HASH && !node->nd_head) { kw = node->nd_next; node = 0; if (kw) { @@ -3965,17 +3986,10 @@ compile_array(rb_iseq_t *iseq, LINK_ANCH https://github.com/ruby/ruby/blob/trunk/compile.c#L3986 opt_p = 0; } - if (type == COMPILE_ARRAY_TYPE_ARGS && - node->nd_next == NULL /* last node */ && - compile_array_keyword_arg(iseq, anchor, node->nd_head, keywords_ptr, flag)) { - len--; - } - else { - COMPILE_(anchor, "array element", node->nd_head, popped); - } + COMPILE_(anchor, "array element", node->nd_head, popped); } - if (opt_p && type != COMPILE_ARRAY_TYPE_ARGS) { + if (opt_p) { if (!popped) { VALUE ary = rb_ary_tmp_new(i); @@ -4077,9 +4091,6 @@ compile_array(rb_iseq_t *iseq, LINK_ANCH https://github.com/ruby/ruby/blob/trunk/compile.c#L4091 } first = 0; break; - case COMPILE_ARRAY_TYPE_ARGS: - APPEND_LIST(ret, anchor); - break; } } else { @@ -4839,7 +4850,7 @@ setup_args(rb_iseq_t *iseq, LINK_ANCHOR https://github.com/ruby/ruby/blob/trunk/compile.c#L4850 *flag |= VM_CALL_KW_SPLAT; if (next_is_array) { - int len = compile_array(iseq, args, argn->nd_head, COMPILE_ARRAY_TYPE_ARGS, NULL, flag, FALSE); + int len = compile_args(iseq, args, argn->nd_head, NULL, flag); if (len < 0) return Qnil; argc = INT2FIX(len + 1); } @@ -4851,7 +4862,7 @@ setup_args(rb_iseq_t *iseq, LINK_ANCHOR https://github.com/ruby/ruby/blob/trunk/compile.c#L4862 } case NODE_ARRAY: { - int len = compile_array(iseq, args, argn, COMPILE_ARRAY_TYPE_ARGS, keywords, flag, FALSE); + int len = compile_args(iseq, args, argn, keywords, flag); if (len < 0) return Qnil; argc = INT2FIX(len); break; @@ -6807,7 +6818,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L6818 break; } case NODE_ARRAY:{ - CHECK(compile_array(iseq, ret, node, COMPILE_ARRAY_TYPE_ARRAY, NULL, NULL, popped) >= 0); + CHECK(compile_array(iseq, ret, node, COMPILE_ARRAY_TYPE_ARRAY, popped) >= 0); break; } case NODE_ZARRAY:{ @@ -6835,7 +6846,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L6846 INIT_ANCHOR(list); switch (type) { case NODE_ARRAY: - CHECK(compile_array(iseq, list, node->nd_head, COMPILE_ARRAY_TYPE_HASH, NULL, NULL, popped) >= 0); + CHECK(compile_array(iseq, list, node->nd_head, COMPILE_ARRAY_TYPE_HASH, popped) >= 0); ADD_SEQ(ret, list); break; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/