Skip to content

Commit

Permalink
Add nd_type_p macro
Browse files Browse the repository at this point in the history
  • Loading branch information
S-H-GAMELINKS committed Dec 3, 2021
1 parent 28fb6d6 commit ec7f14d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 101 deletions.
6 changes: 3 additions & 3 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ dump_block(rb_ast_t *ast, const NODE *node)
do {
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
} while (node->nd_next &&
nd_type(node->nd_next) == NODE_BLOCK &&
nd_type_p(node->nd_next, NODE_BLOCK) &&
(node = node->nd_next, 1));
if (node->nd_next) {
rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
Expand All @@ -313,7 +313,7 @@ dump_array(rb_ast_t *ast, const NODE *node)
VALUE ary = rb_ary_new();
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));

while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
node = node->nd_next;
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
}
Expand Down Expand Up @@ -399,7 +399,7 @@ node_children(rb_ast_t *ast, const NODE *node)

while (1) {
rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
if (!node->nd_2nd || nd_type(node->nd_2nd) != (int)type)
if (!node->nd_2nd || !nd_type_p(node->nd_2nd, type))
break;
node = node->nd_2nd;
}
Expand Down
90 changes: 45 additions & 45 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node)
iseq_set_local_table(iseq, 0);
}
/* assume node is T_NODE */
else if (nd_type(node) == NODE_SCOPE) {
else if (nd_type_p(node, NODE_SCOPE)) {
/* iseq type of top, method, class, block */
iseq_set_local_table(iseq, node->nd_tbl);
iseq_set_arguments(iseq, ret, node->nd_args);
Expand Down Expand Up @@ -1783,7 +1783,7 @@ iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *const optargs,
dv = Qfalse;
break;
default:
NO_CHECK(COMPILE_POPPED(optargs, "kwarg", node)); /* nd_type(node) == NODE_KW_ARG */
NO_CHECK(COMPILE_POPPED(optargs, "kwarg", node)); /* nd_type_p(node, NODE_KW_ARG) */
dv = complex_mark;
}

Expand Down Expand Up @@ -3914,7 +3914,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(head) == NODE_STR) {
if (nd_type_p(head, NODE_STR)) {
lit = rb_fstring(head->nd_lit);
ADD_INSN1(ret, head, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
Expand All @@ -3938,7 +3938,7 @@ compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cons
static int
compile_block(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int popped)
{
while (node && nd_type(node) == NODE_BLOCK) {
while (node && nd_type_p(node, NODE_BLOCK)) {
CHECK(COMPILE_(ret, "BLOCK body", node->nd_head,
(node->nd_next ? 1 : popped)));
node = node->nd_next;
Expand Down Expand Up @@ -4083,7 +4083,7 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *co
static int
keyword_node_p(const NODE *const node)
{
return nd_type(node) == NODE_HASH && (node->nd_brace & HASH_BRACE) != HASH_BRACE;
return nd_type_p(node, NODE_HASH) && (node->nd_brace & HASH_BRACE) != HASH_BRACE;
}

static int
Expand All @@ -4094,16 +4094,16 @@ compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
{
if (kw_arg_ptr == NULL) return FALSE;

if (root_node->nd_head && nd_type(root_node->nd_head) == NODE_LIST) {
if (root_node->nd_head && nd_type_p(root_node->nd_head, NODE_LIST)) {
const NODE *node = root_node->nd_head;
int seen_nodes = 0;

while (node) {
const NODE *key_node = node->nd_head;
seen_nodes++;

assert(nd_type(node) == NODE_LIST);
if (key_node && nd_type(key_node) == NODE_LIT && SYMBOL_P(key_node->nd_lit)) {
assert(nd_type_p(node, NODE_LIST));
if (key_node && nd_type_p(key_node, NODE_LIT) && SYMBOL_P(key_node->nd_lit)) {
/* can be keywords */
}
else {
Expand Down Expand Up @@ -4224,7 +4224,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int pop
{
const NODE *line_node = node;

if (nd_type(node) == NODE_ZLIST) {
if (nd_type_p(node, NODE_ZLIST)) {
if (!popped) {
ADD_INSN1(ret, line_node, newarray, INT2FIX(0));
}
Expand Down Expand Up @@ -4361,7 +4361,7 @@ compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int meth

node = node->nd_head;

if (!node || nd_type(node) == NODE_ZLIST) {
if (!node || nd_type_p(node, NODE_ZLIST)) {
if (!popped) {
ADD_INSN1(ret, line_node, newhash, INT2FIX(0));
}
Expand Down Expand Up @@ -4484,7 +4484,7 @@ compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int meth
FLUSH_CHUNK();

const NODE *kw = node->nd_next->nd_head;
int empty_kw = nd_type(kw) == NODE_LIT && RB_TYPE_P(kw->nd_lit, T_HASH); /* foo( ..., **{}, ...) */
int empty_kw = nd_type_p(kw, NODE_LIT) && RB_TYPE_P(kw->nd_lit, T_HASH); /* foo( ..., **{}, ...) */
int first_kw = first_chunk && stack_len == 0; /* foo(1,2,3, **kw, ...) */
int last_kw = !node->nd_next->nd_next; /* foo( ..., **kw) */
int only_kw = last_kw && first_kw; /* foo(1,2,3, **kw) */
Expand Down Expand Up @@ -4590,7 +4590,7 @@ when_vals(rb_iseq_t *iseq, LINK_ANCHOR *const cond_seq, const NODE *vals,
rb_hash_aset(literals, lit, (VALUE)(l1) | 1);
}

if (nd_type(val) == NODE_STR) {
if (nd_type_p(val, NODE_STR)) {
debugp_param("nd_lit", val->nd_lit);
lit = rb_fstring(val->nd_lit);
ADD_INSN1(cond_seq, val, putobject, lit);
Expand Down Expand Up @@ -4884,7 +4884,7 @@ compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
mem[memindex++] = (v); \
}

if (rhsn == 0 || nd_type(rhsn) != NODE_LIST) {
if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
return 0;
}

Expand Down Expand Up @@ -4952,7 +4952,7 @@ compile_massign0(rb_iseq_t *iseq, LINK_ANCHOR *const pre, LINK_ANCHOR *const rhs
}

if (lhs_splat) {
if (nd_type(splatn) == NODE_POSTARG) {
if (nd_type_p(splatn, NODE_POSTARG)) {
/*a, b, *r, p1, p2 */
const NODE *postn = splatn->nd_2nd;
const NODE *restn = splatn->nd_1st;
Expand Down Expand Up @@ -5069,7 +5069,7 @@ compile_const_prefix(rb_iseq_t *iseq, const NODE *const node,
static int
compile_cpath(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const NODE *cpath)
{
if (nd_type(cpath) == NODE_COLON3) {
if (nd_type_p(cpath, NODE_COLON3)) {
/* toplevel class ::Foo */
ADD_INSN1(ret, cpath, putobject, rb_cObject);
return VM_DEFINECLASS_FLAG_SCOPED;
Expand All @@ -5090,7 +5090,7 @@ compile_cpath(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const NODE *cpath)
static inline int
private_recv_p(const NODE *node)
{
if (nd_type(node->nd_recv) == NODE_SELF) {
if (nd_type_p(node->nd_recv, NODE_SELF)) {
NODE *self = node->nd_recv;
return self->nd_state != 0;
}
Expand Down Expand Up @@ -5479,7 +5479,7 @@ check_keyword(const NODE *node)
{
/* This check is essentially a code clone of compile_keyword_arg. */

if (nd_type(node) == NODE_LIST) {
if (nd_type_p(node, NODE_LIST)) {
while (node->nd_next) {
node = node->nd_next;
}
Expand All @@ -5503,9 +5503,9 @@ setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
}
case NODE_ARGSCAT:
case NODE_ARGSPUSH: {
int next_is_list = (nd_type(argn->nd_head) == NODE_LIST);
int next_is_list = (nd_type_p(argn->nd_head, NODE_LIST));
VALUE argc = setup_args_core(iseq, args, argn->nd_head, 1, NULL, NULL);
if (nd_type(argn->nd_body) == NODE_LIST) {
if (nd_type_p(argn->nd_body, NODE_LIST)) {
/* This branch is needed to avoid "newarraykwsplat" [Bug #16442] */
int rest_len = compile_args(iseq, args, argn->nd_body, NULL, NULL);
ADD_INSN1(args, argn, newarray, INT2FIX(rest_len));
Expand All @@ -5520,7 +5520,7 @@ setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
if (check_keyword(argn->nd_body))
*flag |= VM_CALL_KW_SPLAT;
}
if (nd_type(argn) == NODE_ARGSCAT) {
if (nd_type_p(argn, NODE_ARGSCAT)) {
if (next_is_list) {
ADD_INSN1(args, argn, splatarray, Qtrue);
return INT2FIX(FIX2INT(argc) + 1);
Expand Down Expand Up @@ -5554,7 +5554,7 @@ setup_args(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
unsigned int *flag, struct rb_callinfo_kwarg **keywords)
{
VALUE ret;
if (argn && nd_type(argn) == NODE_BLOCK_PASS) {
if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
unsigned int dup_rest = 1;
DECL_ANCHOR(arg_block);
INIT_ANCHOR(arg_block);
Expand Down Expand Up @@ -5896,7 +5896,7 @@ compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no
INIT_ANCHOR(body_seq);
endlabel = NEW_LABEL(nd_line(node));

while (node && nd_type(node) == NODE_WHEN) {
while (node && nd_type_p(node, NODE_WHEN)) {
const int line = nd_line(node);
LABEL *l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
Expand Down Expand Up @@ -6427,7 +6427,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
NODE *value_node = args->nd_next->nd_head;
VALUE key;

if (nd_type(key_node) != NODE_LIT) {
if (!nd_type_p(key_node, NODE_LIT)) {
UNKNOWN_NODE("NODE_IN", key_node, COMPILE_NG);
}
key = key_node->nd_lit;
Expand Down Expand Up @@ -6595,7 +6595,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
match_succeeded = NEW_LABEL(line);

ADD_INSN(ret, line_node, dup);
if (nd_type(node) == NODE_IF) {
if (nd_type_p(node, NODE_IF)) {
ADD_INSNL(ret, line_node, branchif, match_succeeded);
}
else {
Expand All @@ -6612,7 +6612,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c

ADD_LABEL(ret, match_succeeded);
}
if (nd_type(node) == NODE_IF) {
if (nd_type_p(node, NODE_IF)) {
ADD_INSNL(ret, line_node, branchunless, match_failed);
}
else {
Expand All @@ -6627,7 +6627,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
match_failed = NEW_LABEL(line);

n = node->nd_head;
if (! (nd_type(n) == NODE_LIST && n->nd_alen == 2)) {
if (! (nd_type_p(n, NODE_LIST) && n->nd_alen == 2)) {
COMPILE_ERROR(ERROR_ARGS "unexpected node");
return COMPILE_NG;
}
Expand Down Expand Up @@ -7144,7 +7144,7 @@ compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, in
const rb_iseq_t *child_iseq;

ADD_LABEL(ret, retry_label);
if (nd_type(node) == NODE_FOR) {
if (nd_type_p(node, NODE_FOR)) {
CHECK(COMPILE(ret, "iter caller (for)", node->nd_iter));

ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
Expand Down Expand Up @@ -7687,7 +7687,7 @@ compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE
/* optimization shortcut
* "literal".freeze -> opt_str_freeze("literal")
*/
if (node->nd_recv && nd_type(node->nd_recv) == NODE_STR &&
if (node->nd_recv && nd_type_p(node->nd_recv, NODE_STR) &&
(node->nd_mid == idFreeze || node->nd_mid == idUMinus) &&
node->nd_args == NULL &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
Expand All @@ -7711,8 +7711,8 @@ compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE
* obj["literal"] -> opt_aref_with(obj, "literal")
*/
if (node->nd_mid == idAREF && !private_recv_p(node) && node->nd_args &&
nd_type(node->nd_args) == NODE_LIST && node->nd_args->nd_alen == 1 &&
nd_type(node->nd_args->nd_head) == NODE_STR &&
nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 1 &&
nd_type_p(node->nd_args->nd_head, NODE_STR) &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
Expand Down Expand Up @@ -7850,11 +7850,11 @@ static int
compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, const NODE *line_node, int popped)
{
if (!node) goto no_arg;
if (nd_type(node) != NODE_LIST) goto bad_arg;
if (!nd_type_p(node, NODE_LIST)) goto bad_arg;
if (node->nd_next) goto too_many_arg;
node = node->nd_head;
if (!node) goto no_arg;
if (nd_type(node) != NODE_LIT) goto bad_arg;
if (!nd_type_p(node, NODE_LIT)) goto bad_arg;
VALUE name = node->nd_lit;
if (!SYMBOL_P(name)) goto non_symbol_arg;
if (!popped) {
Expand Down Expand Up @@ -8060,7 +8060,7 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, co
INIT_ANCHOR(recv);
INIT_ANCHOR(args);
#if OPT_SUPPORT_JOKE
if (nd_type(node) == NODE_VCALL) {
if (nd_type_p(node, NODE_VCALL)) {
ID id_bitblt;
ID id_answer;

Expand All @@ -8084,7 +8084,7 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, co
CONST_ID(goto_id, "__goto__");
CONST_ID(label_id, "__label__");

if (nd_type(node) == NODE_FCALL &&
if (nd_type_p(node, NODE_FCALL) &&
(mid == goto_id || mid == label_id)) {
LABEL *label;
st_data_t data;
Expand All @@ -8095,7 +8095,7 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, co
labels_table = st_init_numtable();
ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
}
if (nd_type(node->nd_args->nd_head) == NODE_LIT &&
if (nd_type_p(node->nd_args->nd_head, NODE_LIT) &&
SYMBOL_P(node->nd_args->nd_head->nd_lit)) {

label_name = node->nd_args->nd_head->nd_lit;
Expand Down Expand Up @@ -8136,7 +8136,7 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, co
int idx, level;

if (mid == idCall &&
nd_type(node->nd_recv) == NODE_LVAR &&
nd_type_p(node->nd_recv, NODE_LVAR) &&
iseq_block_param_id_p(iseq, node->nd_recv->nd_vid, &idx, &level)) {
ADD_INSN2(recv, node->nd_recv, getblockparamproxy, INT2FIX(idx + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
Expand Down Expand Up @@ -8514,7 +8514,7 @@ compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
LABEL *lfin = NEW_LABEL(line);
LABEL *lassign;

if (type == NODE_OP_ASGN_OR && nd_type(node->nd_head) != NODE_IVAR) {
if (type == NODE_OP_ASGN_OR && !nd_type_p(node->nd_head, NODE_IVAR)) {
LABEL *lfinish[2];
lfinish[0] = lfin;
lfinish[1] = 0;
Expand Down Expand Up @@ -8857,8 +8857,8 @@ compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, in

if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
if (!popped) {
VALUE bv = nd_type(b) == NODE_LIT ? b->nd_lit : Qnil;
VALUE ev = nd_type(e) == NODE_LIT ? e->nd_lit : Qnil;
VALUE bv = nd_type_p(b, NODE_LIT) ? b->nd_lit : Qnil;
VALUE ev = nd_type_p(e, NODE_LIT) ? e->nd_lit : Qnil;
VALUE val = rb_range_new(bv, ev, excl);
ADD_INSN1(ret, node, putobject, val);
RB_OBJ_WRITTEN(iseq, Qundef, val);
Expand Down Expand Up @@ -8914,10 +8914,10 @@ compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
COMPILE_ERROR(ERROR_ARGS "unreachable");
return COMPILE_NG;
}
else if (nd_type(default_value) == NODE_LIT ||
nd_type(default_value) == NODE_NIL ||
nd_type(default_value) == NODE_TRUE ||
nd_type(default_value) == NODE_FALSE) {
else if (nd_type_p(default_value, NODE_LIT) ||
nd_type_p(default_value, NODE_NIL) ||
nd_type_p(default_value, NODE_TRUE) ||
nd_type_p(default_value, NODE_FALSE)) {
COMPILE_ERROR(ERROR_ARGS "unreachable");
return COMPILE_NG;
}
Expand Down Expand Up @@ -8952,8 +8952,8 @@ compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node
* obj["literal"] = value -> opt_aset_with(obj, "literal", value)
*/
if (mid == idASET && !private_recv_p(node) && node->nd_args &&
nd_type(node->nd_args) == NODE_LIST && node->nd_args->nd_alen == 2 &&
nd_type(node->nd_args->nd_head) == NODE_STR &&
nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 2 &&
nd_type_p(node->nd_args->nd_head, NODE_STR) &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
Expand Down
6 changes: 3 additions & 3 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dump_array(VALUE buf, VALUE indent, int comment, const NODE *node)
const char *next_indent = default_indent;
F_LONG(nd_alen, "length");
F_NODE(nd_head, "element");
while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
node = node->nd_next;
F_NODE(nd_head, "element");
}
Expand Down Expand Up @@ -175,7 +175,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
dump_node(buf, indent, comment, node->nd_head);
D_DEDENT;
} while (node->nd_next &&
nd_type(node->nd_next) == NODE_BLOCK &&
nd_type_p(node->nd_next, NODE_BLOCK) &&
(node = node->nd_next, 1));
if (node->nd_next) {
LAST_NODE;
Expand Down Expand Up @@ -370,7 +370,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
andor:
while (1) {
F_NODE(nd_1st, "left expr");
if (!node->nd_2nd || nd_type(node->nd_2nd) != (int)type)
if (!node->nd_2nd || !nd_type_p(node->nd_2nd, type))
break;
node = node->nd_2nd;
}
Expand Down

0 comments on commit ec7f14d

Please sign in to comment.