diff options
Diffstat (limited to 'node.c')
| -rw-r--r-- | node.c | 1261 |
1 files changed, 404 insertions, 857 deletions
@@ -9,891 +9,438 @@ **********************************************************************/ -#include "ruby/ruby.h" -#include "vm_core.h" - -#define A(str) rb_str_cat2(buf, (str)) -#define AR(str) rb_str_concat(buf, (str)) - -#define A_INDENT add_indent(buf, indent) -#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 F_CUSTOM2(name, ann, block) \ - COMPOUND_FIELD(#name, #name " (" ann ")", block) - -#define F_NODE(name, ann) \ - COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name)) - -#define ANN(ann) \ - if (comment) { \ - A_INDENT; A("| # "); A(ann); A("\n"); \ - } +#ifdef UNIVERSAL_PARSER +#include <stddef.h> +#include "node.h" +#include "rubyparser.h" +#endif + +#include "internal/variable.h" -#define LAST_NODE (next_indent = " ") +#define NODE_BUF_DEFAULT_SIZE (sizeof(struct RNode) * 16) static void -add_indent(VALUE buf, VALUE indent) +init_node_buffer_elem(node_buffer_elem_t *nbe, size_t allocated, void *xmalloc(size_t)) { - AR(indent); + nbe->allocated = allocated; + nbe->used = 0; + nbe->len = 0; + nbe->nodes = xmalloc(allocated / sizeof(struct RNode) * sizeof(struct RNode *)); /* All node requires at least RNode */ } static void -add_id(VALUE buf, ID id) +init_node_buffer_list(node_buffer_list_t *nb, node_buffer_elem_t *head, void *xmalloc(size_t)) +{ + init_node_buffer_elem(head, NODE_BUF_DEFAULT_SIZE, xmalloc); + nb->head = nb->last = head; + nb->head->next = NULL; +} + +#ifdef UNIVERSAL_PARSER +#define ruby_xmalloc config->malloc +#endif + +#ifdef UNIVERSAL_PARSER +static node_buffer_t * +rb_node_buffer_new(const rb_parser_config_t *config) +#else +static node_buffer_t * +rb_node_buffer_new(void) +#endif +{ + const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_SIZE; + const size_t alloc_size = sizeof(node_buffer_t) + (bucket_size); + STATIC_ASSERT( + integer_overflow, + offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_SIZE + > sizeof(node_buffer_t) + sizeof(node_buffer_elem_t)); + node_buffer_t *nb = ruby_xmalloc(alloc_size); + init_node_buffer_list(&nb->buffer_list, (node_buffer_elem_t*)&nb[1], ruby_xmalloc); + nb->local_tables = 0; + nb->tokens = 0; + return nb; +} + +#ifdef UNIVERSAL_PARSER +#undef ruby_xmalloc +#define ruby_xmalloc ast->config->malloc +#undef xfree +#define xfree ast->config->free +#define rb_xmalloc_mul_add ast->config->xmalloc_mul_add +#define ruby_xrealloc(var,size) (ast->config->realloc_n((void *)var, 1, size)) +#endif + +typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE *node); +static void iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, void *ctx); + +void +rb_node_init(NODE *n, enum node_type type) +{ + RNODE(n)->flags = 0; + nd_init_type(RNODE(n), type); + RNODE(n)->nd_loc.beg_pos.lineno = 0; + RNODE(n)->nd_loc.beg_pos.column = 0; + RNODE(n)->nd_loc.end_pos.lineno = 0; + RNODE(n)->nd_loc.end_pos.column = 0; + RNODE(n)->node_id = -1; +} + +const char * +rb_node_name(int node) { - if (id == 0) { - A("(null)"); + switch (node) { +#include "node_name.inc" + default: + return 0; } - else { - VALUE str = rb_id2str(id); - if (str) { - A(":"); AR(str); - } - else { - A("(internal variable)"); - } +} + +#ifdef UNIVERSAL_PARSER +const char * +ruby_node_name(int node) +{ + return rb_node_name(node); +} +#else +const char * +ruby_node_name(int node) +{ + const char *name = rb_node_name(node); + + if (!name) rb_bug("unknown node: %d", node); + return name; +} +#endif + +static void +node_buffer_list_free(rb_ast_t *ast, node_buffer_list_t * nb) +{ + node_buffer_elem_t *nbe = nb->head; + while (nbe != nb->last) { + void *buf = nbe; + xfree(nbe->nodes); + nbe = nbe->next; + xfree(buf); } + + /* The last node_buffer_elem_t is allocated in the node_buffer_t, so we + * only need to free the nodes. */ + xfree(nbe->nodes); } +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 -dump_node(VALUE buf, VALUE indent, int comment, NODE *node) +parser_string_free(rb_ast_t *ast, rb_parser_string_t *str) { - const char *next_indent = "| "; + if (!str) return; + xfree(str->ptr); + xfree(str); +} + +static void +parser_ast_token_free(rb_ast_t *ast, rb_parser_ast_token_t *token) +{ + if (!token) return; + parser_string_free(ast, token->str); + xfree(token); +} - if (!node) { - D_NULL_NODE; - return; +static void +parser_tokens_free(rb_ast_t *ast, rb_parser_ary_t *tokens) +{ + for (long i = 0; i < tokens->len; i++) { + parser_ast_token_free(ast, tokens->data[i]); } + xfree(tokens->data); + xfree(tokens); +} - D_NODE_HEADER(node); +static void +parser_nodes_free(rb_ast_t *ast, rb_parser_ary_t *nodes) +{ + /* Do nothing for nodes because nodes are freed when rb_ast_t is freed */ + xfree(nodes->data); + xfree(nodes); +} +static void +free_ast_value(rb_ast_t *ast, void *ctx, NODE *node) +{ switch (nd_type(node)) { - case NODE_BLOCK: - ANN("statement sequence"); - 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; - - case NODE_IF: - ANN("if statement"); - ANN("format: if [nd_cond] then [nd_body] else [nd_else] end"); - ANN("example: if 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"); - break; - - case NODE_CASE: - ANN("case statement"); - ANN("format: case [nd_head]; [nd_body]; end"); - ANN("example: case x; when 1; foo; when 2; bar; else baz; end"); - F_NODE(nd_head, "case expr"); - LAST_NODE; - F_NODE(nd_body, "when clauses"); - break; - - case NODE_WHEN: - ANN("if statement"); - 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"); - LAST_NODE; - F_NODE(nd_next, "next when clause"); - break; - - 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"); - ANN("example: while x == 1; foo; end"); - goto loop; - case NODE_UNTIL: - ANN("until statement"); - 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_NODE(nd_cond, "condition"); - LAST_NODE; - F_NODE(nd_body, "body"); - break; - - case NODE_ITER: - ANN("method call with block"); - ANN("format: [nd_iter] { [nd_body] }"); - ANN("example: 3.times { foo }"); - goto iter; - case NODE_FOR: - ANN("for statement"); - ANN("format: for * in [nd_iter] do [nd_body] end"); - ANN("example: for i in 1..3 do foo end"); - iter: - F_NODE(nd_iter, "iteration receiver"); - LAST_NODE; - F_NODE(nd_body, "body"); - break; - - case NODE_BREAK: - ANN("for statement"); - ANN("format: break [nd_stts]"); - ANN("example: break 1"); - goto jump; - case NODE_NEXT: - ANN("next statement"); - ANN("format: next [nd_stts]"); - ANN("example: next 1"); - goto jump; - case NODE_RETURN: - ANN("return statement"); - ANN("format: return [nd_stts]"); - ANN("example: return 1"); - jump: - LAST_NODE; - F_NODE(nd_stts, "value"); - break; - - case NODE_REDO: - ANN("redo statement"); - ANN("format: redo"); - ANN("example: redo"); - break; - - case NODE_RETRY: - ANN("retry statement"); - ANN("format: retry"); - ANN("example: retry"); - break; - - case NODE_BEGIN: - ANN("begin statement"); - ANN("format: begin; [nd_body]; end"); - ANN("example: begin; 1; end"); - LAST_NODE; - F_NODE(nd_body, "body"); - break; - - case NODE_RESCUE: - ANN("rescue clause"); - ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end"); - ANN("example: begin; foo; rescue; bar; else; baz; end"); - F_NODE(nd_head, "body"); - F_NODE(nd_resq, "rescue clause list"); - LAST_NODE; - F_NODE(nd_else, "rescue else clause"); - break; - - case NODE_RESBODY: - ANN("rescue clause (cont'd)"); - ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]"); - ANN("example: begin; foo; rescue; bar; else; baz; end"); - F_NODE(nd_args, "rescue exceptions"); - F_NODE(nd_body, "rescue clause"); - LAST_NODE; - F_NODE(nd_head, "next rescue clause"); - break; - - case NODE_ENSURE: - ANN("ensure clause"); - ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end"); - ANN("example: begin; foo; ensure; bar; end"); - F_NODE(nd_head, "body"); - LAST_NODE; - F_NODE(nd_ensr, "ensure clause"); - break; - - case NODE_AND: - ANN("&& operator"); - ANN("format: [nd_1st] && [nd_2nd]"); - ANN("example: foo && bar"); - goto andor; - case NODE_OR: - ANN("|| operator"); - ANN("format: [nd_1st] || [nd_2nd]"); - ANN("example: foo && bar"); - andor: - F_NODE(nd_1st, "left expr"); - LAST_NODE; - F_NODE(nd_2nd, "right expr"); - break; - - case NODE_MASGN: - ANN("multiple assignment"); - ANN("format: [nd_head], [nd_args] = [nd_value]"); - ANN("example: a, b = foo"); - F_NODE(nd_value, "rhsn"); - F_NODE(nd_head, "lhsn"); - if ((VALUE)node->nd_args != (VALUE)-1) { - LAST_NODE; - F_NODE(nd_args, "splatn"); - } - else { - F_MSG(nd_args, "splatn", "-1 (rest argument without name)"); - } - break; - - case NODE_LASGN: - ANN("local variable assignment"); - ANN("format: [nd_vid](lvar) = [nd_value]"); - ANN("example: x = foo"); - goto asgn; - case NODE_DASGN: - ANN("dynamic variable assignment (out of current scope)"); - 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; - case NODE_IASGN: - ANN("instance variable assignment"); - ANN("format: [nd_vid](ivar) = [nd_value]"); - ANN("example: @x = foo"); - goto asgn; - case NODE_CVASGN: - ANN("class variable assignment"); - ANN("format: [nd_vid](cvar) = [nd_value]"); - ANN("example: @@x = foo"); - asgn: - F_ID(nd_vid, "variable"); - LAST_NODE; - F_NODE(nd_value, "rvalue"); - break; - - case NODE_GASGN: - ANN("global variable assignment"); - ANN("format: [nd_entry](gvar) = [nd_value]"); - ANN("example: $x = foo"); - F_GENTRY(nd_entry, "global variable"); - LAST_NODE; - F_NODE(nd_value, "rvalue"); - break; - - 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"); - } - else { - F_MSG(nd_vid, "variable", "0 (see extension field)"); - F_NODE(nd_else, "extension"); - } - LAST_NODE; - F_NODE(nd_value, "rvalue"); - break; - - case NODE_OP_ASGN1: - ANN("array assignment with operator"); - ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]"); - ANN("example: ary[1] += foo"); - F_NODE(nd_recv, "receiver"); - F_ID(nd_vid, "operator"); - F_NODE(nd_args->nd_body, "index"); - LAST_NODE; - F_NODE(nd_args->nd_head, "rvalue"); - break; - - 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("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); - } - }); - LAST_NODE; - F_NODE(nd_value, "rvalue"); - break; - - case NODE_OP_ASGN_AND: - ANN("assignment with && operator"); - ANN("format: [nd_head] &&= [nd_value]"); - ANN("example: foo &&= bar"); - goto asgn_andor; - case NODE_OP_ASGN_OR: - ANN("assignment with || operator"); - ANN("format: [nd_head] ||= [nd_value]"); - ANN("example: foo ||= bar"); - asgn_andor: - F_NODE(nd_head, "variable"); - LAST_NODE; - F_NODE(nd_value, "rvalue"); - break; - - case NODE_CALL: - ANN("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"); - break; - - case NODE_FCALL: - ANN("function call"); - ANN("format: [nd_mid]([nd_args])"); - ANN("example: foo(1)"); - F_ID(nd_mid, "method id"); - LAST_NODE; - F_NODE(nd_args, "arguments"); - break; - - case NODE_VCALL: - ANN("function call with no argument"); - ANN("format: [nd_mid]"); - ANN("example: foo"); - F_ID(nd_mid, "method id"); - break; - - case NODE_SUPER: - ANN("super invocation"); - ANN("format: super [nd_args]"); - ANN("example: super 1"); - LAST_NODE; - F_NODE(nd_args, "arguments"); - break; - - case NODE_ZSUPER: - ANN("super invocation with no argument"); - ANN("format: super"); - ANN("example: super"); - break; - - case NODE_ARRAY: - ANN("array constructor"); - ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])"); - ANN("example: [1, 2, 3]"); - goto ary; - case NODE_VALUES: - ANN("return arguments"); - 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; - - case NODE_ZARRAY: - ANN("empty array constructor"); - ANN("format: []"); - ANN("example: []"); - break; - - case NODE_HASH: - ANN("hash constructor"); - ANN("format: { [nd_head] }"); - ANN("example: { 1 => 2, 3 => 4 }"); - LAST_NODE; - F_NODE(nd_head, "contents"); - break; - - case NODE_YIELD: - ANN("yield invocation"); - ANN("format: yield [nd_head]"); - ANN("example: yield 1"); - LAST_NODE; - F_NODE(nd_head, "arguments"); - break; - - case NODE_LVAR: - ANN("local variable reference"); - ANN("format: [nd_vid](lvar)"); - ANN("example: x"); - goto var; - case NODE_DVAR: - ANN("dynamic variable reference"); - ANN("format: [nd_vid](dvar)"); - ANN("example: 1.times { x = 1; x }"); - goto var; - case NODE_IVAR: - ANN("instance variable reference"); - ANN("format: [nd_vid](ivar)"); - ANN("example: @x"); - goto var; - case NODE_CONST: - ANN("constant reference"); - ANN("format: [nd_vid](constant)"); - ANN("example: X"); - goto var; - case NODE_CVAR: - ANN("class variable reference"); - ANN("format: [nd_vid](cvar)"); - ANN("example: @@x"); - var: - F_ID(nd_vid, "local variable"); - break; - - case NODE_GVAR: - ANN("global variable reference"); - ANN("format: [nd_entry](gvar)"); - ANN("example: $x"); - F_GENTRY(nd_entry, "global variable"); - break; - - 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; - - 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; - - 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; - - 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; - F_NODE(nd_value, "string (argument)"); - break; - - case NODE_MATCH3: - ANN("match expression (regexp second)"); - ANN("format: [nd_recv] =~ [nd_value]"); - ANN("example: 'foo' =~ /foo/"); - F_NODE(nd_recv, "string (receiver)"); - LAST_NODE; - F_NODE(nd_value, "regexp (argument)"); - break; - - case NODE_LIT: - ANN("literal"); - ANN("format: [nd_lit]"); - ANN("example: 1, /foo/"); - goto lit; case NODE_STR: - ANN("string literal"); - ANN("format: [nd_lit]"); - ANN("example: 'foo'"); - goto lit; - case NODE_XSTR: - ANN("xstring literal"); - ANN("format: [nd_lit]"); - ANN("example: `foo`"); - lit: - F_LIT(nd_lit, "literal"); - break; - + parser_string_free(ast, RNODE_STR(node)->string); + break; case NODE_DSTR: - ANN("string literal with interpolation"); - ANN("format: [nd_lit]"); - ANN("example: \"foo#{ bar }baz\""); - goto dlit; + parser_string_free(ast, RNODE_DSTR(node)->string); + break; + case NODE_XSTR: + parser_string_free(ast, RNODE_XSTR(node)->string); + break; case NODE_DXSTR: - ANN("xstring literal with interpolation"); - ANN("format: [nd_lit]"); - ANN("example: `foo#{ bar }baz`"); - goto dlit; - case NODE_DREGX: - ANN("regexp literal with interpolation"); - 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; + parser_string_free(ast, RNODE_DXSTR(node)->string); + break; + case NODE_SYM: + parser_string_free(ast, RNODE_SYM(node)->string); + break; + case NODE_REGX: + case NODE_MATCH: + parser_string_free(ast, RNODE_REGX(node)->string); + break; 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"); - LAST_NODE; - F_NODE(nd_next->nd_next, "interpolation"); - break; - - case NODE_EVSTR: - ANN("interpolation expression"); - ANN("format: \"..#{ [nd_lit] }..\""); - ANN("example: \"foo#{ bar }baz\""); - LAST_NODE; - F_NODE(nd_body, "body"); - break; - - case NODE_ARGSCAT: - ANN("splat argument following arguments"); - ANN("format: ..(*[nd_head], [nd_body..])"); - ANN("example: foo(*ary, post_arg1, post_arg2)"); - F_NODE(nd_head, "preceding array"); - LAST_NODE; - F_NODE(nd_body, "following array"); - break; - - case NODE_ARGSPUSH: - ANN("splat argument following one argument"); - ANN("format: ..(*[nd_head], [nd_body])"); - ANN("example: foo(*ary, post_arg)"); - F_NODE(nd_head, "preceding array"); - LAST_NODE; - F_NODE(nd_body, "following element"); - break; - - case NODE_SPLAT: - ANN("splat argument"); - ANN("format: *[nd_head]"); - ANN("example: foo(*ary)"); - LAST_NODE; - F_NODE(nd_head, "splat'ed array"); - break; - - case NODE_BLOCK_PASS: - ANN("arguments with block argument"); - ANN("format: ..([nd_head], &[nd_body])"); - ANN("example: foo(x, &blk)"); - F_NODE(nd_head, "other arguments"); - LAST_NODE; - F_NODE(nd_body, "block argument"); - break; - - case NODE_DEFN: - ANN("method definition"); - ANN("format: def [nd_mid] [nd_defn]; end"); - ANN("example; def foo; bar; end"); - F_ID(nd_mid, "method name"); - LAST_NODE; - F_NODE(nd_defn, "method definition"); - break; - - case NODE_DEFS: - ANN("singleton method definition"); - ANN("format: def [nd_recv].[nd_mid] [nd_defn]; 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; - - case NODE_ALIAS: - ANN("method alias statement"); - ANN("format: alias [u1.node] [u2.node]"); - ANN("example: alias bar foo"); - F_NODE(u1.node, "new name"); - LAST_NODE; - F_NODE(u2.node, "old name"); - break; - - case NODE_VALIAS: - ANN("global variable alias statement"); - ANN("format: alias [u1.id](gvar) [u2.id](gvar)"); - ANN("example: alias $y $x"); - F_ID(u1.id, "new name"); - F_ID(u2.id, "old name"); - break; - + parser_string_free(ast, RNODE_DSYM(node)->string); + break; + case NODE_DREGX: + parser_string_free(ast, RNODE_DREGX(node)->string); + break; + case NODE_FILE: + parser_string_free(ast, RNODE_FILE(node)->path); + break; + case NODE_INTEGER: + xfree(RNODE_INTEGER(node)->val); + break; + case NODE_FLOAT: + xfree(RNODE_FLOAT(node)->val); + break; + case NODE_RATIONAL: + xfree(RNODE_RATIONAL(node)->val); + break; + case NODE_IMAGINARY: + xfree(RNODE_IMAGINARY(node)->val); + break; case NODE_UNDEF: - ANN("method alias statement"); - ANN("format: undef [u2.node]"); - ANN("example: undef foo"); - LAST_NODE; - F_NODE(u2.node, "old name"); - break; - - case NODE_CLASS: - ANN("class definition"); - ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end"); - ANN("example: class C2 < C; ..; end"); - F_NODE(nd_cpath, "class path"); - F_NODE(nd_super, "superclass"); - LAST_NODE; - F_NODE(nd_body, "class definition"); - break; - - case NODE_MODULE: - ANN("module definition"); - ANN("format: module [nd_cpath]; [nd_body]; end"); - ANN("example: module M; ..; end"); - F_NODE(nd_cpath, "module path"); - LAST_NODE; - F_NODE(nd_body, "module definition"); - break; - - case NODE_SCLASS: - ANN("singleton class definition"); - ANN("format: class << [nd_recv]; [nd_body]; end"); - ANN("example: class << obj; ..; end"); - F_NODE(nd_recv, "receiver"); - LAST_NODE; - F_NODE(nd_body, "singleton class definition"); - break; - - case NODE_COLON2: - ANN("scoped constant reference"); - ANN("format: [nd_head]::[nd_mid]"); - ANN("example: M::C"); - F_ID(nd_mid, "constant name"); - LAST_NODE; - F_NODE(nd_head, "receiver"); - break; - - case NODE_COLON3: - ANN("top-level constant reference"); - ANN("format: ::[nd_mid]"); - ANN("example: ::Object"); - F_ID(nd_mid, "constant name"); - break; - - case NODE_DOT2: - ANN("range constructor (incl.)"); - ANN("format: [nd_beg]..[nd_end]"); - ANN("example: 1..5"); - goto dot; - case NODE_DOT3: - ANN("range constructor (excl.)"); - ANN("format: [nd_beg]...[nd_end]"); - ANN("example: 1...5"); - goto dot; - case NODE_FLIP2: - ANN("flip-flop condition (incl.)"); - ANN("format: [nd_beg]..[nd_end]"); - ANN("example: if (x==1)..(x==5); foo; end"); - goto dot; - case NODE_FLIP3: - ANN("flip-flop condition (excl.)"); - ANN("format: [nd_beg]...[nd_end]"); - ANN("example: if (x==1)...(x==5); foo; end"); - dot: - F_NODE(nd_beg, "begin"); - LAST_NODE; - F_NODE(nd_end, "end"); - break; - - case NODE_SELF: - ANN("self"); - ANN("format: self"); - ANN("example: self"); - break; - - case NODE_NIL: - ANN("nil"); - ANN("format: nil"); - ANN("example: nil"); - break; - - case NODE_TRUE: - ANN("true"); - ANN("format: true"); - ANN("example: true"); - break; - - case NODE_FALSE: - ANN("false"); - ANN("format: false"); - ANN("example: false"); - break; - - case NODE_ERRINFO: - ANN("virtual reference to $!"); - ANN("format: rescue => id"); - ANN("example: rescue => id"); - break; - - case NODE_DEFINED: - ANN("defined? expression"); - ANN("format: defined?([nd_head])"); - ANN("example: defined?(foo)"); - F_NODE(nd_head, "expr"); - break; - - case NODE_POSTEXE: - ANN("post-execution"); - ANN("format: END { [nd_body] }"); - ANN("example: END { foo }"); - LAST_NODE; - F_NODE(nd_body, "END clause"); - break; - - 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_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; - - case NODE_LAMBDA: - ANN("lambda expression"); - ANN("format: -> [nd_body]"); - ANN("example: -> { foo }"); - LAST_NODE; - F_NODE(nd_body, "lambda clause"); - break; - - case NODE_OPT_ARG: - ANN("optional arguments"); - ANN("format: def method_name([nd_body=some], [nd_next..])"); - ANN("example: def foo(a, b=1, c); end"); - F_NODE(nd_body, "body"); - LAST_NODE; - F_NODE(nd_next, "next"); - break; - - case NODE_KW_ARG: - ANN("keyword arguments"); - ANN("format: def method_name([nd_body=some], [nd_next..])"); - ANN("example: def foo(a:1, b:2); end"); - F_NODE(nd_body, "body"); - LAST_NODE; - F_NODE(nd_next, "next"); - break; - - 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) { - F_NODE(nd_1st, "rest argument"); - } - else { - F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)"); - } - LAST_NODE; - F_NODE(nd_2nd, "post arguments"); - break; - - 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"); - 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_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"); - F_NODE(nd_ainfo->kw_rest_arg, "keyword rest argument"); - break; - - 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_NODE(nd_args, "arguments"); - LAST_NODE; - F_NODE(nd_body, "body"); - break; - + parser_nodes_free(ast, RNODE_UNDEF(node)->nd_undefs); + break; default: - rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node))); + break; + } +} + +static void +rb_node_buffer_free(rb_ast_t *ast, node_buffer_t *nb) +{ + if (nb->tokens) { + parser_tokens_free(ast, nb->tokens); + } + iterate_node_values(ast, &nb->buffer_list, free_ast_value, NULL); + node_buffer_list_free(ast, &nb->buffer_list); + 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; + } + xfree(nb); +} + +#define buf_add_offset(nbe, offset) ((char *)(nbe->buf) + (offset)) + +static NODE * +ast_newnode_in_bucket(rb_ast_t *ast, node_buffer_list_t *nb, size_t size, size_t alignment) +{ + size_t padding; + NODE *ptr; + + padding = alignment - (size_t)buf_add_offset(nb->head, nb->head->used) % alignment; + padding = padding == alignment ? 0 : padding; + + if (nb->head->used + size + padding > nb->head->allocated) { + size_t n = nb->head->allocated * 2; + node_buffer_elem_t *nbe; + nbe = rb_xmalloc_mul_add(n, sizeof(char *), offsetof(node_buffer_elem_t, buf)); + init_node_buffer_elem(nbe, n, ruby_xmalloc); + nbe->next = nb->head; + nb->head = nbe; + padding = 0; /* malloc returns aligned address then no need to add padding */ + } + + ptr = (NODE *)buf_add_offset(nb->head, nb->head->used + padding); + nb->head->used += (size + padding); + nb->head->nodes[nb->head->len++] = ptr; + return ptr; +} + +NODE * +rb_ast_newnode(rb_ast_t *ast, enum node_type type, size_t size, size_t alignment) +{ + node_buffer_t *nb = ast->node_buffer; + node_buffer_list_t *bucket = &nb->buffer_list; + return ast_newnode_in_bucket(ast, bucket, size, alignment); +} + +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; + + 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? */ +} + +#ifdef UNIVERSAL_PARSER +rb_ast_t * +rb_ast_new(const rb_parser_config_t *config) +{ + node_buffer_t *nb = rb_node_buffer_new(config); + rb_ast_t *ast = (rb_ast_t *)config->calloc(1, sizeof(rb_ast_t)); + ast->config = config; + ast->node_buffer = nb; + return ast; +} +#else +rb_ast_t * +rb_ast_new(void) +{ + node_buffer_t *nb = rb_node_buffer_new(); + rb_ast_t *ast = ruby_xcalloc(1, sizeof(rb_ast_t)); + ast->node_buffer = nb; + return ast; +} +#endif + +static void +iterate_buffer_elements(rb_ast_t *ast, node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx) +{ + long cursor; + for (cursor = 0; cursor < len; cursor++) { + func(ast, ctx, nbe->nodes[cursor]); + } +} + +static void +iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, void *ctx) +{ + node_buffer_elem_t *nbe = nb->head; + + while (nbe) { + iterate_buffer_elements(ast, nbe, nbe->len, func, ctx); + nbe = nbe->next; + } +} + +static void +script_lines_free(rb_ast_t *ast, rb_parser_ary_t *script_lines) +{ + if (!script_lines) return; + for (long i = 0; i < script_lines->len; i++) { + parser_string_free(ast, (rb_parser_string_t *)script_lines->data[i]); + } + xfree(script_lines->data); + xfree(script_lines); +} + +void +rb_ast_free(rb_ast_t *ast) +{ + rb_ast_dispose(ast); + xfree(ast); +} + +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) { + size += offsetof(node_buffer_elem_t, buf) + nbe->used; + nbe = nbe->next; + } + return size; +} + +size_t +rb_ast_memsize(const rb_ast_t *ast) +{ + size_t size = sizeof(rb_ast_t); + node_buffer_t *nb = ast->node_buffer; + rb_parser_ary_t *tokens = NULL; + struct rb_ast_local_table_link *link = NULL; + rb_parser_ary_t *script_lines = ast->body.script_lines; + + long i; + + if (nb) { + size += sizeof(node_buffer_t); + size += buffer_list_size(&nb->buffer_list); + link = nb->local_tables; + tokens = nb->tokens; + } + + while (link) { + size += sizeof(struct rb_ast_local_table_link); + size += link->size * sizeof(ID); + link = link->next; + } + + if (tokens) { + size += sizeof(rb_parser_ary_t); + for (i = 0; i < tokens->len; i++) { + size += sizeof(rb_parser_ast_token_t); + rb_parser_ast_token_t *token = tokens->data[i]; + size += sizeof(rb_parser_string_t); + size += token->str->len + 1; + } + } + + if (script_lines) { + size += sizeof(rb_parser_ary_t); + for (i = 0; i < script_lines->len; i++) { + size += sizeof(rb_parser_string_t); + size += ((rb_parser_string_t *)script_lines->data[i])->len + 1; + } + } + + return size; +} + +void +rb_ast_dispose(rb_ast_t *ast) +{ + if (ast && ast->node_buffer) { + script_lines_free(ast, ast->body.script_lines); + ast->body.script_lines = NULL; + rb_node_buffer_free(ast, ast->node_buffer); + ast->node_buffer = 0; } } VALUE -rb_parser_dump_tree(NODE *node, int comment) +rb_node_set_type(NODE *n, enum node_type t) +{ + return nd_init_type(n, t); +} + +enum node_type +rb_node_get_type(const NODE *n) { - VALUE buf = rb_str_new_cstr( - "###########################################################\n" - "## Do NOT use this node dump for any purpose other than ##\n" - "## debug and research. Compatibility is not guaranteed. ##\n" - "###########################################################\n\n" - ); - dump_node(buf, rb_str_new_cstr("# "), comment, node); - return buf; + return (enum node_type)nd_type(n); } |
