summaryrefslogtreecommitdiff
path: root/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'node.c')
-rw-r--r--node.c1181
1 files changed, 787 insertions, 394 deletions
diff --git a/node.c b/node.c
index 32518d3154..d8c1da95ef 100644
--- a/node.c
+++ b/node.c
@@ -9,56 +9,95 @@
**********************************************************************/
+#include "internal.h"
+#include "internal/hash.h"
+#include "internal/variable.h"
#include "ruby/ruby.h"
#include "vm_core.h"
+#define NODE_BUF_DEFAULT_LEN 16
+
#define A(str) rb_str_cat2(buf, (str))
#define AR(str) rb_str_concat(buf, (str))
#define A_INDENT add_indent(buf, indent)
+#define D_INDENT rb_str_cat2(indent, next_indent)
+#define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4)
#define A_ID(id) add_id(buf, (id))
#define A_INT(val) rb_str_catf(buf, "%d", (val))
#define A_LONG(val) rb_str_catf(buf, "%ld", (val))
-#define A_LIT(lit) AR(rb_inspect(lit))
-#define A_NODE_HEADER(node) \
- rb_str_catf(buf, "@ %s (line: %d)", ruby_node_name(nd_type(node)), nd_line(node))
-#define A_FIELD_HEADER(name) \
- rb_str_catf(buf, "+- %s:", (name))
-
-#define D_NULL_NODE A_INDENT; A("(null node)"); A("\n");
-#define D_NODE_HEADER(node) A_INDENT; A_NODE_HEADER(node); A("\n");
-
-#define COMPOUND_FIELD(name, name2, block) \
- do { \
- A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A("\n"); \
- rb_str_cat2(indent, next_indent); \
- block; \
- rb_str_resize(indent, RSTRING_LEN(indent) - 4); \
- } while (0)
-
-#define SIMPLE_FIELD(name, name2, block) \
- do { \
- A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A(" "); block; A("\n"); \
- } while (0)
-
-#define F_CUSTOM1(name, ann, block) SIMPLE_FIELD(#name, #name " (" ann ")", block)
-#define F_ID(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID(node->name))
-#define F_GENTRY(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID((node->name)->id))
-#define F_INT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_INT(node->name))
-#define F_LONG(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_LONG(node->name))
-#define F_LIT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_LIT(node->name))
-#define F_MSG(name, ann, desc) SIMPLE_FIELD(#name, #name " (" ann ")", A(desc))
+#define A_LIT(lit) AR(rb_dump_literal(lit))
+#define A_NODE_HEADER(node, term) \
+ rb_str_catf(buf, "@ %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))%s"term, \
+ ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node), \
+ nd_first_lineno(node), nd_first_column(node), \
+ nd_last_lineno(node), nd_last_column(node), \
+ (node->flags & NODE_FL_NEWLINE ? "*" : ""))
+#define A_FIELD_HEADER(len, name, term) \
+ rb_str_catf(buf, "+- %.*s:"term, (len), (name))
+#define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
+
+#define D_NULL_NODE (A_INDENT, A("(null node)\n"))
+#define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
+
+#define COMPOUND_FIELD(len, name) \
+ FIELD_BLOCK((D_FIELD_HEADER((len), (name), "\n"), D_INDENT), D_DEDENT)
+
+#define COMPOUND_FIELD1(name, ann) \
+ COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
+ FIELD_NAME_DESC(name, ann))
+
+#define FIELD_NAME_DESC(name, ann) name " (" ann ")"
+#define FIELD_NAME_LEN(name, ann) (int)( \
+ comment ? \
+ rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
+ rb_strlen_lit(name))
+#define SIMPLE_FIELD(len, name) \
+ FIELD_BLOCK(D_FIELD_HEADER((len), (name), " "), A("\n"))
+
+#define FIELD_BLOCK(init, reset) \
+ for (init, field_flag = 1; \
+ field_flag; /* should be optimized away */ \
+ reset, field_flag = 0)
+
+#define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
+#define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
+#define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
+#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
+#define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
+#define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
+#define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
+#define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
#define F_NODE(name, ann) \
- COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name))
+ COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, node->name);}
#define ANN(ann) \
if (comment) { \
- A_INDENT; A("| # "); A(ann); A("\n"); \
+ A_INDENT; A("| # " ann "\n"); \
}
#define LAST_NODE (next_indent = " ")
+VALUE
+rb_dump_literal(VALUE lit)
+{
+ if (!RB_SPECIAL_CONST_P(lit)) {
+ VALUE str;
+ switch (RB_BUILTIN_TYPE(lit)) {
+ case T_CLASS: case T_MODULE: case T_ICLASS:
+ str = rb_class_path(lit);
+ if (FL_TEST(lit, FL_SINGLETON)) {
+ str = rb_sprintf("<%"PRIsVALUE">", str);
+ }
+ return str;
+ default:
+ break;
+ }
+ }
+ return rb_inspect(lit);
+}
+
static void
add_indent(VALUE buf, VALUE indent)
{
@@ -77,15 +116,41 @@ add_id(VALUE buf, ID id)
A(":"); AR(str);
}
else {
- A("(internal variable)");
+ rb_str_catf(buf, "(internal variable: 0x%"PRIsVALUE")", id);
}
}
}
+struct add_option_arg {
+ VALUE buf, indent;
+ st_index_t count;
+};
+
+static void dump_node(VALUE, VALUE, int, const NODE *);
+static const char default_indent[] = "| ";
+
static void
-dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
+dump_array(VALUE buf, VALUE indent, int comment, const NODE *node)
{
- const char *next_indent = "| ";
+ int field_flag;
+ const char *next_indent = default_indent;
+ F_LONG(nd_alen, "length");
+ F_NODE(nd_head, "element");
+ while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
+ node = node->nd_next;
+ F_NODE(nd_head, "element");
+ }
+ LAST_NODE;
+ F_NODE(nd_next, "next element");
+}
+
+static void
+dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
+{
+ int field_flag;
+ int i;
+ const char *next_indent = default_indent;
+ enum node_type type;
if (!node) {
D_NULL_NODE;
@@ -94,15 +159,29 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
D_NODE_HEADER(node);
- switch (nd_type(node)) {
+ type = nd_type(node);
+ switch (type) {
case NODE_BLOCK:
ANN("statement sequence");
- ANN("format: [nd_head]; [nd_next]");
+ ANN("format: [nd_head]; ...; [nd_next]");
ANN("example: foo; bar");
- F_NODE(nd_head, "current statement");
- LAST_NODE;
- F_NODE(nd_next, "next block");
- break;
+ i = 0;
+ do {
+ A_INDENT;
+ rb_str_catf(buf, "+- nd_head (%s%d):\n",
+ comment ? "statement #" : "", ++i);
+ if (!node->nd_next) LAST_NODE;
+ D_INDENT;
+ dump_node(buf, indent, comment, node->nd_head);
+ D_DEDENT;
+ } while (node->nd_next &&
+ nd_type_p(node->nd_next, NODE_BLOCK) &&
+ (node = node->nd_next, 1));
+ if (node->nd_next) {
+ LAST_NODE;
+ F_NODE(nd_next, "next block");
+ }
+ return;
case NODE_IF:
ANN("if statement");
@@ -112,7 +191,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_body, "then clause");
LAST_NODE;
F_NODE(nd_else, "else clause");
- break;
+ return;
+
+ case NODE_UNLESS:
+ ANN("unless statement");
+ ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
+ ANN("example: unless x == 1 then foo else bar end");
+ F_NODE(nd_cond, "condition expr");
+ F_NODE(nd_body, "then clause");
+ LAST_NODE;
+ F_NODE(nd_else, "else clause");
+ return;
case NODE_CASE:
ANN("case statement");
@@ -121,23 +210,44 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "case expr");
LAST_NODE;
F_NODE(nd_body, "when clauses");
- break;
+ return;
+ case NODE_CASE2:
+ ANN("case statement with no head");
+ ANN("format: case; [nd_body]; end");
+ ANN("example: case; when 1; foo; when 2; bar; else baz; end");
+ F_NODE(nd_head, "case expr");
+ LAST_NODE;
+ F_NODE(nd_body, "when clauses");
+ return;
+ case NODE_CASE3:
+ ANN("case statement (pattern matching)");
+ ANN("format: case [nd_head]; [nd_body]; end");
+ ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
+ F_NODE(nd_head, "case expr");
+ LAST_NODE;
+ F_NODE(nd_body, "in clauses");
+ return;
case NODE_WHEN:
- ANN("if statement");
+ ANN("when clause");
ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
F_NODE(nd_head, "when value");
- F_NODE(nd_body, "when clause");
+ F_NODE(nd_body, "when body");
LAST_NODE;
F_NODE(nd_next, "next when clause");
- break;
+ return;
+
+ case NODE_IN:
+ ANN("in clause");
+ ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
+ ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
+ F_NODE(nd_head, "in pattern");
+ F_NODE(nd_body, "in body");
+ LAST_NODE;
+ F_NODE(nd_next, "next in clause");
+ return;
- case NODE_OPT_N:
- ANN("wrapper for -n option");
- ANN("format: ruby -ne '[nd_body]' (nd_cond is `gets')");
- ANN("example: ruby -ne 'p $_'");
- goto loop;
case NODE_WHILE:
ANN("while statement");
ANN("format: while [nd_cond]; [nd_body]; end");
@@ -148,14 +258,14 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("format: until [nd_cond]; [nd_body]; end");
ANN("example: until x == 1; foo; end");
loop:
- F_CUSTOM1(nd_state, "begin-end-while?", {
- A_INT((int)node->nd_state);
- A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
- });
+ F_CUSTOM1(nd_state, "begin-end-while?") {
+ A_INT((int)node->nd_state);
+ A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
+ }
F_NODE(nd_cond, "condition");
LAST_NODE;
F_NODE(nd_body, "body");
- break;
+ return;
case NODE_ITER:
ANN("method call with block");
@@ -170,10 +280,18 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_iter, "iteration receiver");
LAST_NODE;
F_NODE(nd_body, "body");
- break;
+ return;
+
+ case NODE_FOR_MASGN:
+ ANN("vars of for statement with masgn");
+ ANN("format: for [nd_var] in ... do ... end");
+ ANN("example: for x, y in 1..3 do foo end");
+ LAST_NODE;
+ F_NODE(nd_var, "var");
+ return;
case NODE_BREAK:
- ANN("for statement");
+ ANN("break statement");
ANN("format: break [nd_stts]");
ANN("example: break 1");
goto jump;
@@ -189,19 +307,19 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
jump:
LAST_NODE;
F_NODE(nd_stts, "value");
- break;
+ return;
case NODE_REDO:
ANN("redo statement");
ANN("format: redo");
ANN("example: redo");
- break;
+ return;
case NODE_RETRY:
ANN("retry statement");
ANN("format: retry");
ANN("example: retry");
- break;
+ return;
case NODE_BEGIN:
ANN("begin statement");
@@ -209,7 +327,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: begin; 1; end");
LAST_NODE;
F_NODE(nd_body, "body");
- break;
+ return;
case NODE_RESCUE:
ANN("rescue clause");
@@ -219,7 +337,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_resq, "rescue clause list");
LAST_NODE;
F_NODE(nd_else, "rescue else clause");
- break;
+ return;
case NODE_RESBODY:
ANN("rescue clause (cont'd)");
@@ -229,7 +347,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_body, "rescue clause");
LAST_NODE;
F_NODE(nd_head, "next rescue clause");
- break;
+ return;
case NODE_ENSURE:
ANN("ensure clause");
@@ -238,7 +356,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "body");
LAST_NODE;
F_NODE(nd_ensr, "ensure clause");
- break;
+ return;
case NODE_AND:
ANN("&& operator");
@@ -248,12 +366,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
case NODE_OR:
ANN("|| operator");
ANN("format: [nd_1st] || [nd_2nd]");
- ANN("example: foo && bar");
+ ANN("example: foo || bar");
andor:
- F_NODE(nd_1st, "left expr");
+ while (1) {
+ F_NODE(nd_1st, "left expr");
+ if (!node->nd_2nd || !nd_type_p(node->nd_2nd, type))
+ break;
+ node = node->nd_2nd;
+ }
LAST_NODE;
F_NODE(nd_2nd, "right expr");
- break;
+ return;
case NODE_MASGN:
ANN("multiple assignment");
@@ -261,50 +384,58 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: a, b = foo");
F_NODE(nd_value, "rhsn");
F_NODE(nd_head, "lhsn");
- if ((VALUE)node->nd_args != (VALUE)-1) {
+ if (NODE_NAMED_REST_P(node->nd_args)) {
LAST_NODE;
F_NODE(nd_args, "splatn");
}
else {
- F_MSG(nd_args, "splatn", "-1 (rest argument without name)");
+ F_MSG(nd_args, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
}
- break;
+ return;
case NODE_LASGN:
ANN("local variable assignment");
ANN("format: [nd_vid](lvar) = [nd_value]");
ANN("example: x = foo");
- goto asgn;
+ F_ID(nd_vid, "local variable");
+ if (NODE_REQUIRED_KEYWORD_P(node)) {
+ F_MSG(nd_value, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
+ }
+ else {
+ LAST_NODE;
+ F_NODE(nd_value, "rvalue");
+ }
+ return;
case NODE_DASGN:
- ANN("dynamic variable assignment (out of current scope)");
+ ANN("dynamic variable assignment");
ANN("format: [nd_vid](dvar) = [nd_value]");
ANN("example: x = nil; 1.times { x = foo }");
- goto asgn;
- case NODE_DASGN_CURR:
- ANN("dynamic variable assignment (in current scope)");
- ANN("format: [nd_vid](current dvar) = [nd_value]");
ANN("example: 1.times { x = foo }");
- goto asgn;
+ F_ID(nd_vid, "local variable");
+ if (NODE_REQUIRED_KEYWORD_P(node)) {
+ F_MSG(nd_value, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
+ }
+ else {
+ LAST_NODE;
+ F_NODE(nd_value, "rvalue");
+ }
+ return;
case NODE_IASGN:
ANN("instance variable assignment");
ANN("format: [nd_vid](ivar) = [nd_value]");
ANN("example: @x = foo");
- goto asgn;
+ F_ID(nd_vid, "instance variable");
+ LAST_NODE;
+ F_NODE(nd_value, "rvalue");
+ return;
case NODE_CVASGN:
ANN("class variable assignment");
ANN("format: [nd_vid](cvar) = [nd_value]");
ANN("example: @@x = foo");
- asgn:
- F_ID(nd_vid, "variable");
+ F_ID(nd_vid, "class variable");
LAST_NODE;
- if (node->nd_value == (NODE *)-1) {
- F_MSG(nd_value, "rvalue", "(required keyword argument)");
- }
- else {
- F_NODE(nd_value, "rvalue");
- }
- break;
-
+ F_NODE(nd_value, "rvalue");
+ return;
case NODE_GASGN:
ANN("global variable assignment");
ANN("format: [nd_entry](gvar) = [nd_value]");
@@ -312,54 +443,49 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_GENTRY(nd_entry, "global variable");
LAST_NODE;
F_NODE(nd_value, "rvalue");
- break;
+ return;
case NODE_CDECL:
ANN("constant declaration");
ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
ANN("example: X = foo");
if (node->nd_vid) {
- F_ID(nd_vid, "variable");
- F_MSG(nd_else, "extension", "not used");
+ F_ID(nd_vid, "constant");
+ F_MSG(nd_else, "extension", "not used");
}
else {
- F_MSG(nd_vid, "variable", "0 (see extension field)");
- F_NODE(nd_else, "extension");
+ F_MSG(nd_vid, "constant", "0 (see extension field)");
+ F_NODE(nd_else, "extension");
}
LAST_NODE;
F_NODE(nd_value, "rvalue");
- break;
+ return;
case NODE_OP_ASGN1:
ANN("array assignment with operator");
- ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
+ ANN("format: [nd_recv] [ [nd_args->nd_head] ] [nd_mid]= [nd_args->nd_body]");
ANN("example: ary[1] += foo");
F_NODE(nd_recv, "receiver");
- F_ID(nd_vid, "operator");
- F_NODE(nd_args->nd_body, "index");
+ F_ID(nd_mid, "operator");
+ F_NODE(nd_args->nd_head, "index");
LAST_NODE;
- F_NODE(nd_args->nd_head, "rvalue");
- break;
+ F_NODE(nd_args->nd_body, "rvalue");
+ return;
case NODE_OP_ASGN2:
ANN("attr assignment with operator");
- ANN("format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
- ANN(" where [attr] reader: [nd_next->nd_vid]");
- ANN(" [attr] writer: [nd_next->nd_aid]");
+ ANN("format: [nd_recv].[attr] [nd_next->nd_mid]= [nd_value]");
+ ANN(" where [attr]: [nd_next->nd_vid]");
ANN("example: struct.field += foo");
F_NODE(nd_recv, "receiver");
- F_ID(nd_next->nd_vid, "reader");
- F_ID(nd_next->nd_aid, "writer");
- F_CUSTOM1(nd_next->nd_mid, "operator", {
- switch (node->nd_next->nd_mid) {
- case 0: A("0 (||)"); break;
- case 1: A("1 (&&)"); break;
- default: A_ID(node->nd_next->nd_mid);
- }
- });
+ F_CUSTOM1(nd_next->nd_vid, "attr") {
+ if (node->nd_next->nd_aid) A("? ");
+ A_ID(node->nd_next->nd_vid);
+ }
+ F_ID(nd_next->nd_mid, "operator");
LAST_NODE;
F_NODE(nd_value, "rvalue");
- break;
+ return;
case NODE_OP_ASGN_AND:
ANN("assignment with && operator");
@@ -374,7 +500,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "variable");
LAST_NODE;
F_NODE(nd_value, "rvalue");
- break;
+ return;
+
+ case NODE_OP_CDECL:
+ ANN("constant declaration with operator");
+ ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
+ ANN("example: A::B ||= 1");
+ F_NODE(nd_head, "constant");
+ F_ID(nd_aid, "operator");
+ LAST_NODE;
+ F_NODE(nd_value, "rvalue");
+ return;
case NODE_CALL:
ANN("method invocation");
@@ -384,7 +520,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_recv, "receiver");
LAST_NODE;
F_NODE(nd_args, "arguments");
- break;
+ return;
+
+ case NODE_OPCALL:
+ ANN("method invocation");
+ ANN("format: [nd_recv] [nd_mid] [nd_args]");
+ ANN("example: foo + bar");
+ F_ID(nd_mid, "method id");
+ F_NODE(nd_recv, "receiver");
+ LAST_NODE;
+ F_NODE(nd_args, "arguments");
+ return;
case NODE_FCALL:
ANN("function call");
@@ -393,14 +539,24 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_ID(nd_mid, "method id");
LAST_NODE;
F_NODE(nd_args, "arguments");
- break;
+ return;
case NODE_VCALL:
ANN("function call with no argument");
ANN("format: [nd_mid]");
ANN("example: foo");
F_ID(nd_mid, "method id");
- break;
+ return;
+
+ case NODE_QCALL:
+ ANN("safe method invocation");
+ ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
+ ANN("example: obj&.foo(1)");
+ F_ID(nd_mid, "method id");
+ F_NODE(nd_recv, "receiver");
+ LAST_NODE;
+ F_NODE(nd_args, "arguments");
+ return;
case NODE_SUPER:
ANN("super invocation");
@@ -408,16 +564,16 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: super 1");
LAST_NODE;
F_NODE(nd_args, "arguments");
- break;
+ return;
case NODE_ZSUPER:
ANN("super invocation with no argument");
ANN("format: super");
ANN("example: super");
- break;
+ return;
- case NODE_ARRAY:
- ANN("array constructor");
+ case NODE_LIST:
+ ANN("list constructor");
ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
ANN("example: [1, 2, 3]");
goto ary;
@@ -426,25 +582,35 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
ANN("example: return 1, 2, 3");
ary:
- F_LONG(nd_alen, "length");
- F_NODE(nd_head, "element");
- LAST_NODE;
- F_NODE(nd_next, "next element");
- break;
+ dump_array(buf, indent, comment, node);
+ return;
- case NODE_ZARRAY:
- ANN("empty array constructor");
+ case NODE_ZLIST:
+ ANN("empty list constructor");
ANN("format: []");
ANN("example: []");
- break;
+ return;
case NODE_HASH:
- ANN("hash constructor");
- ANN("format: { [nd_head] }");
- ANN("example: { 1 => 2, 3 => 4 }");
+ if (!node->nd_brace) {
+ ANN("keyword arguments");
+ ANN("format: nd_head");
+ ANN("example: a: 1, b: 2");
+ }
+ else {
+ ANN("hash constructor");
+ ANN("format: { [nd_head] }");
+ ANN("example: { 1 => 2, 3 => 4 }");
+ }
+ F_CUSTOM1(nd_brace, "keyword arguments or hash literal") {
+ switch (node->nd_brace) {
+ case 0: A("0 (keyword argument)"); break;
+ case 1: A("1 (hash literal)"); break;
+ }
+ }
LAST_NODE;
F_NODE(nd_head, "contents");
- break;
+ return;
case NODE_YIELD:
ANN("yield invocation");
@@ -452,78 +618,83 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: yield 1");
LAST_NODE;
F_NODE(nd_head, "arguments");
- break;
+ return;
case NODE_LVAR:
ANN("local variable reference");
ANN("format: [nd_vid](lvar)");
ANN("example: x");
- goto var;
+ F_ID(nd_vid, "local variable");
+ return;
case NODE_DVAR:
ANN("dynamic variable reference");
ANN("format: [nd_vid](dvar)");
ANN("example: 1.times { x = 1; x }");
- goto var;
+ F_ID(nd_vid, "local variable");
+ return;
case NODE_IVAR:
ANN("instance variable reference");
ANN("format: [nd_vid](ivar)");
ANN("example: @x");
- goto var;
+ F_ID(nd_vid, "instance variable");
+ return;
case NODE_CONST:
ANN("constant reference");
ANN("format: [nd_vid](constant)");
ANN("example: X");
- goto var;
+ F_ID(nd_vid, "constant");
+ return;
case NODE_CVAR:
ANN("class variable reference");
ANN("format: [nd_vid](cvar)");
ANN("example: @@x");
- var:
- F_ID(nd_vid, "local variable");
- break;
+ F_ID(nd_vid, "class variable");
+ return;
case NODE_GVAR:
ANN("global variable reference");
ANN("format: [nd_entry](gvar)");
ANN("example: $x");
F_GENTRY(nd_entry, "global variable");
- break;
+ return;
case NODE_NTH_REF:
ANN("nth special variable reference");
ANN("format: $[nd_nth]");
ANN("example: $1, $2, ..");
- F_CUSTOM1(nd_nth, "variable", { A("$"); A_LONG(node->nd_nth); });
- break;
+ F_CUSTOM1(nd_nth, "variable") { A("$"); A_LONG(node->nd_nth); }
+ return;
case NODE_BACK_REF:
ANN("back special variable reference");
ANN("format: $[nd_nth]");
ANN("example: $&, $`, $', $+");
- F_CUSTOM1(nd_nth, "variable", {
- char name[3];
- name[0] = '$';
- name[1] = (char)node->nd_nth;
- name[2] = '\0';
- A(name);
- });
- break;
+ F_CUSTOM1(nd_nth, "variable") {
+ char name[3] = "$ ";
+ name[1] = (char)node->nd_nth;
+ A(name);
+ }
+ return;
case NODE_MATCH:
ANN("match expression (against $_ implicitly)");
ANN("format: [nd_lit] (in condition)");
ANN("example: if /foo/; foo; end");
F_LIT(nd_lit, "regexp");
- break;
+ return;
case NODE_MATCH2:
ANN("match expression (regexp first)");
ANN("format: [nd_recv] =~ [nd_value]");
ANN("example: /foo/ =~ 'foo'");
F_NODE(nd_recv, "regexp (receiver)");
- LAST_NODE;
+ if (!node->nd_args) LAST_NODE;
F_NODE(nd_value, "string (argument)");
- break;
+ if (node->nd_args) {
+ LAST_NODE;
+ F_NODE(nd_args, "named captures");
+ }
+ return;
case NODE_MATCH3:
ANN("match expression (regexp second)");
@@ -532,7 +703,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_recv, "string (receiver)");
LAST_NODE;
F_NODE(nd_value, "regexp (argument)");
- break;
+ return;
case NODE_LIT:
ANN("literal");
@@ -550,8 +721,15 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: `foo`");
lit:
F_LIT(nd_lit, "literal");
- break;
+ return;
+ case NODE_ONCE:
+ ANN("once evaluation");
+ ANN("format: [nd_body]");
+ ANN("example: /foo#{ bar }baz/o");
+ LAST_NODE;
+ F_NODE(nd_body, "body");
+ return;
case NODE_DSTR:
ANN("string literal with interpolation");
ANN("format: [nd_lit]");
@@ -567,21 +745,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("format: [nd_lit]");
ANN("example: /foo#{ bar }baz/");
goto dlit;
- case NODE_DREGX_ONCE:
- ANN("regexp literal with interpolation and once flag");
- ANN("format: [nd_lit]");
- ANN("example: /foo#{ bar }baz/o");
- goto dlit;
case NODE_DSYM:
ANN("symbol literal with interpolation");
ANN("format: [nd_lit]");
ANN("example: :\"foo#{ bar }baz\"");
dlit:
- F_LIT(nd_lit, "literal");
- F_NODE(nd_next->nd_head, "preceding string");
+ F_LIT(nd_lit, "preceding string");
+ if (!node->nd_next) return;
+ F_NODE(nd_next->nd_head, "interpolation");
LAST_NODE;
- F_NODE(nd_next->nd_next, "interpolation");
- break;
+ F_NODE(nd_next->nd_next, "tailing strings");
+ return;
case NODE_EVSTR:
ANN("interpolation expression");
@@ -589,7 +763,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: \"foo#{ bar }baz\"");
LAST_NODE;
F_NODE(nd_body, "body");
- break;
+ return;
case NODE_ARGSCAT:
ANN("splat argument following arguments");
@@ -598,7 +772,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "preceding array");
LAST_NODE;
F_NODE(nd_body, "following array");
- break;
+ return;
case NODE_ARGSPUSH:
ANN("splat argument following one argument");
@@ -607,7 +781,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "preceding array");
LAST_NODE;
F_NODE(nd_body, "following element");
- break;
+ return;
case NODE_SPLAT:
ANN("splat argument");
@@ -615,7 +789,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: foo(*ary)");
LAST_NODE;
F_NODE(nd_head, "splat'ed array");
- break;
+ return;
case NODE_BLOCK_PASS:
ANN("arguments with block argument");
@@ -624,51 +798,51 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_head, "other arguments");
LAST_NODE;
F_NODE(nd_body, "block argument");
- break;
+ return;
case NODE_DEFN:
ANN("method definition");
ANN("format: def [nd_mid] [nd_defn]; end");
- ANN("example; def foo; bar; end");
+ ANN("example: def foo; bar; end");
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_defn, "method definition");
- break;
+ return;
case NODE_DEFS:
ANN("singleton method definition");
ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
- ANN("example; def obj.foo; bar; end");
+ ANN("example: def obj.foo; bar; end");
F_NODE(nd_recv, "receiver");
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_defn, "method definition");
- break;
+ return;
case NODE_ALIAS:
ANN("method alias statement");
- ANN("format: alias [u1.node] [u2.node]");
+ ANN("format: alias [nd_1st] [nd_2nd]");
ANN("example: alias bar foo");
- F_NODE(u1.node, "new name");
+ F_NODE(nd_1st, "new name");
LAST_NODE;
- F_NODE(u2.node, "old name");
- break;
+ F_NODE(nd_2nd, "old name");
+ return;
case NODE_VALIAS:
ANN("global variable alias statement");
- ANN("format: alias [u1.id](gvar) [u2.id](gvar)");
+ ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
ANN("example: alias $y $x");
- F_ID(u1.id, "new name");
- F_ID(u2.id, "old name");
- break;
+ F_ID(nd_alias, "new name");
+ F_ID(nd_orig, "old name");
+ return;
case NODE_UNDEF:
- ANN("method alias statement");
- ANN("format: undef [u2.node]");
+ ANN("method undef statement");
+ ANN("format: undef [nd_undef]");
ANN("example: undef foo");
LAST_NODE;
- F_NODE(u2.node, "old name");
- break;
+ F_NODE(nd_undef, "old name");
+ return;
case NODE_CLASS:
ANN("class definition");
@@ -678,7 +852,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_super, "superclass");
LAST_NODE;
F_NODE(nd_body, "class definition");
- break;
+ return;
case NODE_MODULE:
ANN("module definition");
@@ -687,7 +861,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_cpath, "module path");
LAST_NODE;
F_NODE(nd_body, "module definition");
- break;
+ return;
case NODE_SCLASS:
ANN("singleton class definition");
@@ -696,7 +870,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_recv, "receiver");
LAST_NODE;
F_NODE(nd_body, "singleton class definition");
- break;
+ return;
case NODE_COLON2:
ANN("scoped constant reference");
@@ -705,14 +879,14 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_ID(nd_mid, "constant name");
LAST_NODE;
F_NODE(nd_head, "receiver");
- break;
+ return;
case NODE_COLON3:
ANN("top-level constant reference");
ANN("format: ::[nd_mid]");
ANN("example: ::Object");
F_ID(nd_mid, "constant name");
- break;
+ return;
case NODE_DOT2:
ANN("range constructor (incl.)");
@@ -737,44 +911,44 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_beg, "begin");
LAST_NODE;
F_NODE(nd_end, "end");
- break;
+ return;
case NODE_SELF:
ANN("self");
ANN("format: self");
ANN("example: self");
- break;
+ return;
case NODE_NIL:
ANN("nil");
ANN("format: nil");
ANN("example: nil");
- break;
+ return;
case NODE_TRUE:
ANN("true");
ANN("format: true");
ANN("example: true");
- break;
+ return;
case NODE_FALSE:
ANN("false");
ANN("format: false");
ANN("example: false");
- break;
+ return;
case NODE_ERRINFO:
ANN("virtual reference to $!");
ANN("format: rescue => id");
ANN("example: rescue => id");
- break;
+ return;
case NODE_DEFINED:
ANN("defined? expression");
ANN("format: defined?([nd_head])");
ANN("example: defined?(foo)");
F_NODE(nd_head, "expr");
- break;
+ return;
case NODE_POSTEXE:
ANN("post-execution");
@@ -782,31 +956,17 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: END { foo }");
LAST_NODE;
F_NODE(nd_body, "END clause");
- break;
+ return;
case NODE_ATTRASGN:
ANN("attr assignment");
ANN("format: [nd_recv].[nd_mid] = [nd_args]");
ANN("example: struct.field = foo");
- if (node->nd_recv == (NODE *) 1) {
- F_MSG(nd_recv, "receiver", "1 (self)");
- }
- else {
- F_NODE(nd_recv, "receiver");
- }
+ F_NODE(nd_recv, "receiver");
F_ID(nd_mid, "method name");
LAST_NODE;
F_NODE(nd_args, "arguments");
- break;
-
- case NODE_PRELUDE:
- ANN("pre-execution");
- ANN("format: BEGIN { [nd_head] }; [nd_body]");
- ANN("example: bar; BEGIN { foo }");
- F_NODE(nd_head, "prelude");
- LAST_NODE;
- F_NODE(nd_body, "body");
- break;
+ return;
case NODE_LAMBDA:
ANN("lambda expression");
@@ -814,7 +974,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
ANN("example: -> { foo }");
LAST_NODE;
F_NODE(nd_body, "lambda clause");
- break;
+ return;
case NODE_OPT_ARG:
ANN("optional arguments");
@@ -823,7 +983,7 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_body, "body");
LAST_NODE;
F_NODE(nd_next, "next");
- break;
+ return;
case NODE_KW_ARG:
ANN("keyword arguments");
@@ -832,63 +992,123 @@ dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
F_NODE(nd_body, "body");
LAST_NODE;
F_NODE(nd_next, "next");
- break;
+ return;
case NODE_POSTARG:
ANN("post arguments");
ANN("format: *[nd_1st], [nd_2nd..] = ..");
ANN("example: a, *rest, z = foo");
- if ((VALUE)node->nd_1st != (VALUE)-1) {
+ if (NODE_NAMED_REST_P(node->nd_1st)) {
F_NODE(nd_1st, "rest argument");
}
else {
- F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)");
+ F_MSG(nd_1st, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
}
LAST_NODE;
F_NODE(nd_2nd, "post arguments");
- break;
+ return;
case NODE_ARGS:
ANN("method parameters");
- ANN("format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
- ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
+ ANN("format: def method_name(.., [nd_ainfo->nd_optargs], *[nd_ainfo->rest_arg], [nd_ainfo->first_post_arg], .., [nd_ainfo->kw_args], **[nd_ainfo->kw_rest_arg], &[nd_ainfo->block_arg])");
+ ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
F_INT(nd_ainfo->pre_args_num, "count of mandatory (pre-)arguments");
F_NODE(nd_ainfo->pre_init, "initialization of (pre-)arguments");
F_INT(nd_ainfo->post_args_num, "count of mandatory post-arguments");
F_NODE(nd_ainfo->post_init, "initialization of post-arguments");
F_ID(nd_ainfo->first_post_arg, "first post argument");
- F_ID(nd_ainfo->rest_arg, "rest argument");
+ F_CUSTOM1(nd_ainfo->rest_arg, "rest argument") {
+ if (node->nd_ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA) {
+ A("1 (excessed comma)");
+ }
+ else {
+ A_ID(node->nd_ainfo->rest_arg);
+ }
+ }
F_ID(nd_ainfo->block_arg, "block argument");
F_NODE(nd_ainfo->opt_args, "optional arguments");
- LAST_NODE;
F_NODE(nd_ainfo->kw_args, "keyword arguments");
+ LAST_NODE;
F_NODE(nd_ainfo->kw_rest_arg, "keyword rest argument");
- break;
+ return;
case NODE_SCOPE:
ANN("new scope");
ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
- F_CUSTOM1(nd_tbl, "local table", {
- ID *tbl = node->nd_tbl;
- int i;
- int size = tbl ? (int)*tbl++ : 0;
- if (size == 0) A("(empty)");
- for (i = 0; i < size; i++) {
- A_ID(tbl[i]); if (i < size - 1) A(",");
- }
- });
+ F_CUSTOM1(nd_tbl, "local table") {
+ rb_ast_id_table_t *tbl = node->nd_tbl;
+ int i;
+ int size = tbl ? tbl->size : 0;
+ if (size == 0) A("(empty)");
+ for (i = 0; i < size; i++) {
+ A_ID(tbl->ids[i]); if (i < size - 1) A(",");
+ }
+ }
F_NODE(nd_args, "arguments");
LAST_NODE;
F_NODE(nd_body, "body");
- break;
+ return;
- default:
- rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
+ case NODE_ARYPTN:
+ ANN("array pattern");
+ ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
+ F_NODE(nd_pconst, "constant");
+ F_NODE(nd_apinfo->pre_args, "pre arguments");
+ if (NODE_NAMED_REST_P(node->nd_apinfo->rest_arg)) {
+ F_NODE(nd_apinfo->rest_arg, "rest argument");
+ }
+ else {
+ F_MSG(nd_apinfo->rest_arg, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
+ }
+ LAST_NODE;
+ F_NODE(nd_apinfo->post_args, "post arguments");
+ return;
+
+ case NODE_FNDPTN:
+ ANN("find pattern");
+ ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
+ F_NODE(nd_pconst, "constant");
+ if (NODE_NAMED_REST_P(node->nd_fpinfo->pre_rest_arg)) {
+ F_NODE(nd_fpinfo->pre_rest_arg, "pre rest argument");
+ }
+ else {
+ F_MSG(nd_fpinfo->pre_rest_arg, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
+ }
+ F_NODE(nd_fpinfo->args, "arguments");
+
+ LAST_NODE;
+ if (NODE_NAMED_REST_P(node->nd_fpinfo->post_rest_arg)) {
+ F_NODE(nd_fpinfo->post_rest_arg, "post rest argument");
+ }
+ else {
+ F_MSG(nd_fpinfo->post_rest_arg, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
+ }
+ return;
+
+ case NODE_HSHPTN:
+ ANN("hash pattern");
+ ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
+ F_NODE(nd_pconst, "constant");
+ F_NODE(nd_pkwargs, "keyword arguments");
+ LAST_NODE;
+ if (node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
+ F_MSG(nd_pkwrestarg, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
+ }
+ else {
+ F_NODE(nd_pkwrestarg, "keyword rest argument");
+ }
+ return;
+
+ case NODE_ARGS_AUX:
+ case NODE_LAST:
+ break;
}
+
+ rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
}
VALUE
-rb_parser_dump_tree(NODE *node, int comment)
+rb_parser_dump_tree(const NODE *node, int comment)
{
VALUE buf = rb_str_new_cstr(
"###########################################################\n"
@@ -900,183 +1120,356 @@ rb_parser_dump_tree(NODE *node, int comment)
return buf;
}
+/* Setup NODE structure.
+ * NODE is not an object managed by GC, but it imitates an object
+ * so that it can work with `RB_TYPE_P(obj, T_NODE)`.
+ * This dirty hack is needed because Ripper jumbles NODEs and other type
+ * objects.
+ */
void
-rb_gc_free_node(VALUE obj)
+rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
{
- switch (nd_type(obj)) {
- case NODE_SCOPE:
- if (RNODE(obj)->nd_tbl) {
- xfree(RNODE(obj)->nd_tbl);
- }
- break;
- case NODE_ARGS:
- if (RNODE(obj)->nd_ainfo) {
- xfree(RNODE(obj)->nd_ainfo);
- }
- break;
- case NODE_ALLOCA:
- xfree(RNODE(obj)->u1.node);
- break;
+ n->flags = T_NODE;
+ nd_init_type(n, type);
+ n->u1.value = a0;
+ n->u2.value = a1;
+ n->u3.value = a2;
+ n->nd_loc.beg_pos.lineno = 0;
+ n->nd_loc.beg_pos.column = 0;
+ n->nd_loc.end_pos.lineno = 0;
+ n->nd_loc.end_pos.column = 0;
+}
+
+typedef struct node_buffer_elem_struct {
+ struct node_buffer_elem_struct *next;
+ long len;
+ NODE buf[FLEX_ARY_LEN];
+} node_buffer_elem_t;
+
+typedef struct {
+ long idx, len;
+ node_buffer_elem_t *head;
+ node_buffer_elem_t *last;
+} node_buffer_list_t;
+
+struct node_buffer_struct {
+ node_buffer_list_t unmarkable;
+ node_buffer_list_t markable;
+ struct rb_ast_local_table_link *local_tables;
+ VALUE mark_hash;
+};
+
+static void
+init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
+{
+ nb->idx = 0;
+ nb->len = NODE_BUF_DEFAULT_LEN;
+ nb->head = nb->last = head;
+ nb->head->len = nb->len;
+ nb->head->next = NULL;
+}
+
+static node_buffer_t *
+rb_node_buffer_new(void)
+{
+ const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
+ const size_t alloc_size = sizeof(node_buffer_t) + (bucket_size * 2);
+ STATIC_ASSERT(
+ integer_overflow,
+ offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE)
+ > sizeof(node_buffer_t) + 2 * sizeof(node_buffer_elem_t));
+ node_buffer_t *nb = ruby_xmalloc(alloc_size);
+ init_node_buffer_list(&nb->unmarkable, (node_buffer_elem_t*)&nb[1]);
+ init_node_buffer_list(&nb->markable, (node_buffer_elem_t*)((size_t)nb->unmarkable.head + bucket_size));
+ nb->local_tables = 0;
+ nb->mark_hash = Qnil;
+ return nb;
+}
+
+static void
+node_buffer_list_free(node_buffer_list_t * nb)
+{
+ node_buffer_elem_t *nbe = nb->head;
+
+ while (nbe != nb->last) {
+ void *buf = nbe;
+ nbe = nbe->next;
+ xfree(buf);
}
}
-size_t
-rb_node_memsize(VALUE obj)
+struct rb_ast_local_table_link {
+ struct rb_ast_local_table_link *next;
+ // struct rb_ast_id_table {
+ int size;
+ ID ids[FLEX_ARY_LEN];
+ // }
+};
+
+static void
+rb_node_buffer_free(node_buffer_t *nb)
{
- size_t size = 0;
- switch (nd_type(obj)) {
- case NODE_SCOPE:
- if (RNODE(obj)->nd_tbl) {
- size += (RNODE(obj)->nd_tbl[0]+1) * sizeof(*RNODE(obj)->nd_tbl);
- }
- break;
- case NODE_ARGS:
- if (RNODE(obj)->nd_ainfo) {
- size += sizeof(*RNODE(obj)->nd_ainfo);
- }
- break;
- case NODE_ALLOCA:
- size += RNODE(obj)->nd_cnt * sizeof(VALUE);
- break;
+ node_buffer_list_free(&nb->unmarkable);
+ node_buffer_list_free(&nb->markable);
+ struct rb_ast_local_table_link *local_table = nb->local_tables;
+ while (local_table) {
+ struct rb_ast_local_table_link *next_table = local_table->next;
+ xfree(local_table);
+ local_table = next_table;
}
- return size;
+ xfree(nb);
}
-VALUE
-rb_gc_mark_node(NODE *obj)
+static NODE *
+ast_newnode_in_bucket(node_buffer_list_t *nb)
{
- switch (nd_type(obj)) {
- case NODE_IF: /* 1,2,3 */
- case NODE_FOR:
- case NODE_ITER:
- case NODE_WHEN:
- case NODE_MASGN:
- case NODE_RESCUE:
- case NODE_RESBODY:
- case NODE_CLASS:
- case NODE_BLOCK_PASS:
- rb_gc_mark(RNODE(obj)->u2.value);
- /* fall through */
- case NODE_BLOCK: /* 1,3 */
- case NODE_ARRAY:
+ if (nb->idx >= nb->len) {
+ long n = nb->len * 2;
+ node_buffer_elem_t *nbe;
+ nbe = rb_xmalloc_mul_add(n, sizeof(NODE), offsetof(node_buffer_elem_t, buf));
+ nbe->len = n;
+ nb->idx = 0;
+ nb->len = n;
+ nbe->next = nb->head;
+ nb->head = nbe;
+ }
+ return &nb->head->buf[nb->idx++];
+}
+
+RBIMPL_ATTR_PURE()
+static bool
+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_DREGX_ONCE:
- case NODE_ENSURE:
- case NODE_CALL:
- case NODE_DEFS:
- case NODE_OP_ASGN1:
- rb_gc_mark(RNODE(obj)->u1.value);
- /* fall through */
- case NODE_SUPER: /* 3 */
- case NODE_FCALL:
- case NODE_DEFN:
- case NODE_ARGS_AUX:
- return RNODE(obj)->u3.value;
+ case NODE_DSYM:
+ case NODE_ARGS:
+ case NODE_ARYPTN:
+ case NODE_FNDPTN:
+ return true;
+ default:
+ return false;
+ }
+}
- case NODE_WHILE: /* 1,2 */
- case NODE_UNTIL:
- case NODE_AND:
- case NODE_OR:
- case NODE_CASE:
- case NODE_SCLASS:
- case NODE_DOT2:
- case NODE_DOT3:
- case NODE_FLIP2:
- case NODE_FLIP3:
- case NODE_MATCH2:
- case NODE_MATCH3:
- case NODE_OP_ASGN_OR:
- case NODE_OP_ASGN_AND:
- case NODE_MODULE:
- case NODE_ALIAS:
- case NODE_VALIAS:
- case NODE_ARGSCAT:
- rb_gc_mark(RNODE(obj)->u1.value);
- /* fall through */
- case NODE_GASGN: /* 2 */
- case NODE_LASGN:
- case NODE_DASGN:
- case NODE_DASGN_CURR:
- case NODE_IASGN:
- case NODE_IASGN2:
- case NODE_CVASGN:
- case NODE_COLON3:
- case NODE_OPT_N:
- case NODE_EVSTR:
- case NODE_UNDEF:
- case NODE_POSTEXE:
- return RNODE(obj)->u2.value;
+NODE *
+rb_ast_newnode(rb_ast_t *ast, enum node_type type)
+{
+ node_buffer_t *nb = ast->node_buffer;
+ node_buffer_list_t *bucket =
+ (nodetype_markable_p(type) ? &nb->markable : &nb->unmarkable);
+ return ast_newnode_in_bucket(bucket);
+}
+
+void
+rb_ast_node_type_change(NODE *n, enum node_type type)
+{
+ enum node_type old_type = nd_type(n);
+ if (nodetype_markable_p(old_type) != nodetype_markable_p(type)) {
+ rb_bug("node type changed: %s -> %s",
+ ruby_node_name(old_type), ruby_node_name(type));
+ }
+}
+
+rb_ast_id_table_t *
+rb_ast_new_local_table(rb_ast_t *ast, int size)
+{
+ size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID);
+ struct rb_ast_local_table_link *link = ruby_xmalloc(alloc_size);
+ link->next = ast->node_buffer->local_tables;
+ ast->node_buffer->local_tables = link;
+ link->size = size;
- case NODE_HASH: /* 1 */
+ return (rb_ast_id_table_t *) &link->size;
+}
+
+rb_ast_id_table_t *
+rb_ast_resize_latest_local_table(rb_ast_t *ast, int size)
+{
+ struct rb_ast_local_table_link *link = ast->node_buffer->local_tables;
+ size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID);
+ link = ruby_xrealloc(link, alloc_size);
+ ast->node_buffer->local_tables = link;
+ link->size = size;
+
+ return (rb_ast_id_table_t *) &link->size;
+}
+
+void
+rb_ast_delete_node(rb_ast_t *ast, NODE *n)
+{
+ (void)ast;
+ (void)n;
+ /* should we implement freelist? */
+}
+
+rb_ast_t *
+rb_ast_new(void)
+{
+ node_buffer_t *nb = rb_node_buffer_new();
+ rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, (VALUE)nb);
+ return ast;
+}
+
+typedef void node_itr_t(void *ctx, NODE * node);
+
+static void
+iterate_buffer_elements(node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx)
+{
+ long cursor;
+ for (cursor = 0; cursor < len; cursor++) {
+ func(ctx, &nbe->buf[cursor]);
+ }
+}
+
+static void
+iterate_node_values(node_buffer_list_t *nb, node_itr_t * func, void *ctx)
+{
+ node_buffer_elem_t *nbe = nb->head;
+
+ /* iterate over the head first because it's not full */
+ iterate_buffer_elements(nbe, nb->idx, func, ctx);
+
+ nbe = nbe->next;
+ while (nbe) {
+ iterate_buffer_elements(nbe, nbe->len, func, ctx);
+ nbe = nbe->next;
+ }
+}
+
+static void
+mark_ast_value(void *ctx, NODE * node)
+{
+ switch (nd_type(node)) {
+ case NODE_ARGS:
+ {
+ struct rb_args_info *args = node->nd_ainfo;
+ rb_gc_mark_movable(args->imemo);
+ break;
+ }
+ case NODE_MATCH:
case NODE_LIT:
case NODE_STR:
case NODE_XSTR:
- case NODE_DEFINED:
+ case NODE_DSTR:
+ case NODE_DXSTR:
+ case NODE_DREGX:
+ case NODE_DSYM:
+ rb_gc_mark_movable(node->nd_lit);
+ break;
+ case NODE_ARYPTN:
+ case NODE_FNDPTN:
+ rb_gc_mark_movable(node->nd_rval);
+ break;
+ default:
+ rb_bug("unreachable node %s", ruby_node_name(nd_type(node)));
+ }
+}
+
+static void
+update_ast_value(void *ctx, NODE * node)
+{
+ switch (nd_type(node)) {
+ case NODE_ARGS:
+ {
+ struct rb_args_info *args = node->nd_ainfo;
+ args->imemo = rb_gc_location(args->imemo);
+ break;
+ }
case NODE_MATCH:
- case NODE_RETURN:
- case NODE_BREAK:
- case NODE_NEXT:
- case NODE_YIELD:
- case NODE_COLON2:
- case NODE_SPLAT:
- case NODE_TO_ARY:
- return RNODE(obj)->u1.value;
+ case NODE_LIT:
+ case NODE_STR:
+ case NODE_XSTR:
+ case NODE_DSTR:
+ case NODE_DXSTR:
+ case NODE_DREGX:
+ case NODE_DSYM:
+ node->nd_lit = rb_gc_location(node->nd_lit);
+ break;
+ case NODE_ARYPTN:
+ case NODE_FNDPTN:
+ node->nd_rval = rb_gc_location(node->nd_rval);
+ break;
+ default:
+ rb_bug("unreachable");
+ }
+}
- case NODE_SCOPE: /* 2,3 */
- case NODE_CDECL:
- case NODE_OPT_ARG:
- rb_gc_mark(RNODE(obj)->u3.value);
- return RNODE(obj)->u2.value;
-
- case NODE_ARGS: /* custom */
- {
- struct rb_args_info *args = obj->u3.args;
- if (args) {
- if (args->pre_init) rb_gc_mark((VALUE)args->pre_init);
- if (args->post_init) rb_gc_mark((VALUE)args->post_init);
- if (args->opt_args) rb_gc_mark((VALUE)args->opt_args);
- if (args->kw_args) rb_gc_mark((VALUE)args->kw_args);
- if (args->kw_rest_arg) rb_gc_mark((VALUE)args->kw_rest_arg);
- }
- }
- return RNODE(obj)->u2.value;
+void
+rb_ast_update_references(rb_ast_t *ast)
+{
+ if (ast->node_buffer) {
+ node_buffer_t *nb = ast->node_buffer;
- case NODE_ZARRAY: /* - */
- case NODE_ZSUPER:
- case NODE_VCALL:
- case NODE_GVAR:
- case NODE_LVAR:
- case NODE_DVAR:
- case NODE_IVAR:
- case NODE_CVAR:
- case NODE_NTH_REF:
- case NODE_BACK_REF:
- case NODE_REDO:
- case NODE_RETRY:
- case NODE_SELF:
- case NODE_NIL:
- case NODE_TRUE:
- case NODE_FALSE:
- case NODE_ERRINFO:
- case NODE_BLOCK_ARG:
- break;
- case NODE_ALLOCA:
- rb_gc_mark_locations((VALUE*)RNODE(obj)->u1.value,
- (VALUE*)RNODE(obj)->u1.value + RNODE(obj)->u3.cnt);
- rb_gc_mark(RNODE(obj)->u2.value);
- break;
+ iterate_node_values(&nb->markable, update_ast_value, NULL);
+ }
+}
+
+void
+rb_ast_mark(rb_ast_t *ast)
+{
+ if (ast->node_buffer) rb_gc_mark(ast->node_buffer->mark_hash);
+ if (ast->body.compile_option) rb_gc_mark(ast->body.compile_option);
+ if (ast->node_buffer) {
+ node_buffer_t *nb = ast->node_buffer;
+
+ iterate_node_values(&nb->markable, mark_ast_value, NULL);
+ }
+ if (ast->body.script_lines) rb_gc_mark(ast->body.script_lines);
+}
- case NODE_CREF:
- rb_gc_mark(obj->nd_refinements);
- rb_gc_mark(RNODE(obj)->nd_clss);
- return (VALUE)RNODE(obj)->nd_next;
+void
+rb_ast_free(rb_ast_t *ast)
+{
+ if (ast->node_buffer) {
+ rb_node_buffer_free(ast->node_buffer);
+ ast->node_buffer = 0;
+ }
+}
+
+static size_t
+buffer_list_size(node_buffer_list_t *nb)
+{
+ size_t size = 0;
+ node_buffer_elem_t *nbe = nb->head;
+ while (nbe != nb->last) {
+ nbe = nbe->next;
+ size += offsetof(node_buffer_elem_t, buf) + nb->len * sizeof(NODE);
+ }
+ return size;
+}
- default: /* unlisted NODE */
- rb_gc_mark_maybe(RNODE(obj)->u1.value);
- rb_gc_mark_maybe(RNODE(obj)->u2.value);
- rb_gc_mark_maybe(RNODE(obj)->u3.value);
+size_t
+rb_ast_memsize(const rb_ast_t *ast)
+{
+ size_t size = 0;
+ node_buffer_t *nb = ast->node_buffer;
+
+ if (nb) {
+ size += sizeof(node_buffer_t) + offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
+ size += buffer_list_size(&nb->unmarkable);
+ size += buffer_list_size(&nb->markable);
+ }
+ return size;
+}
+
+void
+rb_ast_dispose(rb_ast_t *ast)
+{
+ rb_ast_free(ast);
+}
+
+void
+rb_ast_add_mark_object(rb_ast_t *ast, VALUE obj)
+{
+ if (NIL_P(ast->node_buffer->mark_hash)) {
+ RB_OBJ_WRITE(ast, &ast->node_buffer->mark_hash, rb_ident_hash_new());
}
- return 0;
+ rb_hash_aset(ast->node_buffer->mark_hash, obj, Qtrue);
}