diff options
author | yui-knk <[email protected]> | 2023-12-28 12:38:24 +0900 |
---|---|---|
committer | Yuichiro Kaneko <[email protected]> | 2023-12-29 18:32:27 +0900 |
commit | 1ade170a6cac402d362d90bbf12e566d6a4f9ba1 (patch) | |
tree | 7bb01bb33a31122ecc7733f81842323022c3265a /compile.c | |
parent | 6f33e3c53dd97426b91b97b7239a1b9a3dd6a446 (diff) |
Introduce NODE_LINE
`__LINE__` was managed by `NODE_LIT` with `Integer` object.
This commit introduces `NODE_LINE` so that
1. `__LINE__` is detectable from AST Node
2. Reduce dependency ruby object
Diffstat (limited to 'compile.c')
-rw-r--r-- | compile.c | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -30,6 +30,7 @@ #include "internal/object.h" #include "internal/rational.h" #include "internal/re.h" +#include "internal/ruby_parser.h" #include "internal/symbol.h" #include "internal/thread.h" #include "internal/variable.h" @@ -1929,6 +1930,9 @@ iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *const optargs, case NODE_LIT: dv = RNODE_LIT(val_node)->nd_lit; break; + case NODE_LINE: + dv = rb_node_line_lineno_val(val_node);; + break; case NODE_NIL: dv = Qnil; break; @@ -4488,6 +4492,7 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *cond, } goto again; case NODE_LIT: /* NODE_LIT is always true */ + case NODE_LINE: case NODE_TRUE: case NODE_STR: case NODE_ZLIST: @@ -4647,6 +4652,7 @@ static_literal_node_p(const NODE *node, const rb_iseq_t *iseq) { switch (nd_type(node)) { case NODE_LIT: + case NODE_LINE: case NODE_NIL: case NODE_TRUE: case NODE_FALSE: @@ -4668,6 +4674,8 @@ static_literal_value(const NODE *node, rb_iseq_t *iseq) return Qtrue; case NODE_FALSE: return Qfalse; + case NODE_LINE: + return rb_node_line_lineno_val(node); case NODE_STR: if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) { VALUE lit; @@ -5055,6 +5063,8 @@ rb_node_case_when_optimizable_literal(const NODE *const node) return Qtrue; case NODE_FALSE: return Qfalse; + case NODE_LINE: + return rb_node_line_lineno_val(node); case NODE_STR: return rb_fstring(RNODE_STR(node)->nd_lit); } @@ -5681,6 +5691,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, /* fall through */ case NODE_STR: case NODE_LIT: + case NODE_LINE: case NODE_ZLIST: case NODE_AND: case NODE_OR: @@ -6255,6 +6266,8 @@ optimizable_range_item_p(const NODE *n) switch (nd_type(n)) { case NODE_LIT: return RB_INTEGER_TYPE_P(RNODE_LIT(n)->nd_lit); + case NODE_LINE: + return TRUE; case NODE_NIL: return TRUE; default: @@ -6262,6 +6275,21 @@ optimizable_range_item_p(const NODE *n) } } +static VALUE +optimized_range_item(const NODE *n) +{ + switch (nd_type(n)) { + case NODE_LIT: + return RNODE_LIT(n)->nd_lit; + case NODE_LINE: + return rb_node_line_lineno_val(n); + case NODE_NIL: + return Qnil; + default: + rb_bug("unexpected node: %s", ruby_node_name(nd_type(n))); + } +} + static int compile_if(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type) { @@ -7076,6 +7104,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c break; } case NODE_LIT: + case NODE_LINE: case NODE_STR: case NODE_XSTR: case NODE_DSTR: @@ -9571,8 +9600,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_p(b, NODE_LIT) ? RNODE_LIT(b)->nd_lit : Qnil; - VALUE ev = nd_type_p(e, NODE_LIT) ? RNODE_LIT(e)->nd_lit : Qnil; + VALUE bv = optimized_range_item(b); + VALUE ev = optimized_range_item(e); VALUE val = rb_range_new(bv, ev, excl); ADD_INSN1(ret, node, putobject, val); RB_OBJ_WRITTEN(iseq, Qundef, val); @@ -9629,6 +9658,7 @@ compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, return COMPILE_NG; } else if (nd_type_p(default_value, NODE_LIT) || + nd_type_p(default_value, NODE_LINE) || nd_type_p(default_value, NODE_NIL) || nd_type_p(default_value, NODE_TRUE) || nd_type_p(default_value, NODE_FALSE)) { @@ -10098,6 +10128,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no } break; } + case NODE_LINE:{ + if (!popped) { + ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node)); + } + break; + } case NODE_STR:{ debugp_param("nd_lit", RNODE_STR(node)->nd_lit); if (!popped) { |