2009-12-09 17:05:59 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
node.c - ruby node tree
|
|
|
|
|
|
|
|
$Author: mame $
|
|
|
|
created at: 09/12/06 21:23:44 JST
|
|
|
|
|
|
|
|
Copyright (C) 2009 Yusuke Endoh
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "node.h"
|
|
|
|
#include "rubyparser.h"
|
|
|
|
#include "internal/parse.h"
|
|
|
|
#define T_NODE 0x1b
|
|
|
|
|
|
|
|
#define ruby_xmalloc ast->node_buffer->config->malloc
|
|
|
|
#undef xfree
|
|
|
|
#define xfree ast->node_buffer->config->free
|
|
|
|
#define rb_ident_hash_new ast->node_buffer->config->ident_hash_new
|
|
|
|
#define rb_xmalloc_mul_add ast->node_buffer->config->xmalloc_mul_add
|
|
|
|
#define ruby_xrealloc(var,size) (ast->node_buffer->config->realloc_n((void *)var, 1, size))
|
|
|
|
#define rb_gc_mark ast->node_buffer->config->gc_mark
|
|
|
|
#define rb_gc_location ast->node_buffer->config->gc_location
|
|
|
|
#define rb_gc_mark_movable ast->node_buffer->config->gc_mark_movable
|
|
|
|
#define Qnil ast->node_buffer->config->qnil
|
|
|
|
#define Qtrue ast->node_buffer->config->qtrue
|
|
|
|
#define NIL_P ast->node_buffer->config->nil_p
|
|
|
|
#define rb_hash_aset ast->node_buffer->config->hash_aset
|
|
|
|
#define RB_OBJ_WRITE(old, slot, young) ast->node_buffer->config->obj_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/hash.h"
|
|
|
|
#include "internal/variable.h"
|
2009-12-09 17:05:59 +03:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#endif
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#define NODE_BUF_DEFAULT_LEN 16
|
2014-08-16 05:53:10 +04:00
|
|
|
|
2018-01-17 16:15:13 +03:00
|
|
|
/* Setup NODE structure.
|
2017-11-04 13:02:43 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-10-24 09:16:31 +03:00
|
|
|
void
|
|
|
|
rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
|
|
|
|
{
|
|
|
|
n->flags = T_NODE;
|
2021-01-14 09:42:45 +03:00
|
|
|
nd_init_type(n, type);
|
2017-10-24 09:16:31 +03:00
|
|
|
n->u1.value = a0;
|
|
|
|
n->u2.value = a1;
|
|
|
|
n->u3.value = a2;
|
2018-01-09 11:45:35 +03:00
|
|
|
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;
|
2022-07-29 21:12:39 +03:00
|
|
|
n->node_id = -1;
|
2017-10-24 09:16:31 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 15:55:49 +03:00
|
|
|
const char *
|
2023-05-28 14:00:20 +03:00
|
|
|
rb_node_name(int node)
|
2023-05-22 15:55:49 +03:00
|
|
|
{
|
|
|
|
switch (node) {
|
|
|
|
#include "node_name.inc"
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#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);
|
2019-09-09 19:37:06 +03:00
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
if (!name) rb_bug("unknown node: %d", node);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif
|
2017-10-27 19:44:57 +03:00
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static void
|
|
|
|
init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
nb->idx = 0;
|
2019-04-23 01:24:52 +03:00
|
|
|
nb->len = NODE_BUF_DEFAULT_LEN;
|
2019-09-09 19:37:06 +03:00
|
|
|
nb->head = nb->last = head;
|
|
|
|
nb->head->len = nb->len;
|
2017-10-27 19:44:57 +03:00
|
|
|
nb->head->next = NULL;
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
static node_buffer_t *
|
|
|
|
rb_node_buffer_new(rb_parser_config_t *config)
|
|
|
|
{
|
|
|
|
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 = config->malloc(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 = config->qnil;
|
|
|
|
nb->tokens = config->qnil;
|
|
|
|
nb->config = config;
|
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
#else
|
2019-09-09 19:37:06 +03:00
|
|
|
static node_buffer_t *
|
|
|
|
rb_node_buffer_new(void)
|
|
|
|
{
|
2019-10-07 10:56:08 +03:00
|
|
|
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);
|
2019-09-09 19:37:06 +03:00
|
|
|
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));
|
2020-07-24 02:00:28 +03:00
|
|
|
nb->local_tables = 0;
|
2019-11-05 03:04:58 +03:00
|
|
|
nb->mark_hash = Qnil;
|
2022-09-23 16:40:02 +03:00
|
|
|
nb->tokens = Qnil;
|
2017-10-27 19:44:57 +03:00
|
|
|
return nb;
|
|
|
|
}
|
2023-05-28 14:00:20 +03:00
|
|
|
#endif
|
2017-10-27 19:44:57 +03:00
|
|
|
|
2017-11-16 11:19:46 +03:00
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
node_buffer_list_free(rb_ast_t *ast, node_buffer_list_t * nb)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
node_buffer_elem_t *nbe = nb->head;
|
|
|
|
|
2018-01-16 18:12:09 +03:00
|
|
|
while (nbe != nb->last) {
|
2017-10-27 19:44:57 +03:00
|
|
|
void *buf = nbe;
|
|
|
|
nbe = nbe->next;
|
|
|
|
xfree(buf);
|
|
|
|
}
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 21:40:49 +03:00
|
|
|
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];
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
rb_node_buffer_free(rb_ast_t *ast, node_buffer_t *nb)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
2023-05-28 14:00:20 +03:00
|
|
|
node_buffer_list_free(ast, &nb->unmarkable);
|
|
|
|
node_buffer_list_free(ast, &nb->markable);
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *local_table = nb->local_tables;
|
2020-07-24 02:00:28 +03:00
|
|
|
while (local_table) {
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *next_table = local_table->next;
|
2020-07-24 02:00:28 +03:00
|
|
|
xfree(local_table);
|
|
|
|
local_table = next_table;
|
|
|
|
}
|
2017-10-27 19:44:57 +03:00
|
|
|
xfree(nb);
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static NODE *
|
2023-05-28 14:00:20 +03:00
|
|
|
ast_newnode_in_bucket(rb_ast_t *ast, node_buffer_list_t *nb)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
if (nb->idx >= nb->len) {
|
|
|
|
long n = nb->len * 2;
|
|
|
|
node_buffer_elem_t *nbe;
|
2019-10-07 10:56:08 +03:00
|
|
|
nbe = rb_xmalloc_mul_add(n, sizeof(NODE), offsetof(node_buffer_elem_t, buf));
|
2019-09-09 19:37:06 +03:00
|
|
|
nbe->len = n;
|
2017-10-27 19:44:57 +03:00
|
|
|
nb->idx = 0;
|
|
|
|
nb->len = n;
|
|
|
|
nbe->next = nb->head;
|
|
|
|
nb->head = nbe;
|
|
|
|
}
|
|
|
|
return &nb->head->buf[nb->idx++];
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:42:45 +03:00
|
|
|
RBIMPL_ATTR_PURE()
|
|
|
|
static bool
|
|
|
|
nodetype_markable_p(enum node_type type)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_MATCH:
|
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
|
|
|
case NODE_ARGS:
|
|
|
|
case NODE_ARYPTN:
|
2020-06-14 03:24:36 +03:00
|
|
|
case NODE_FNDPTN:
|
2021-01-14 09:42:45 +03:00
|
|
|
return true;
|
2019-09-27 04:20:56 +03:00
|
|
|
default:
|
2021-01-14 09:42:45 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-05-28 14:00:20 +03:00
|
|
|
return ast_newnode_in_bucket(ast, bucket);
|
2021-01-14 09:42:45 +03:00
|
|
|
}
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#if RUBY_DEBUG
|
2021-01-14 09:42:45 +03:00
|
|
|
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",
|
2023-05-24 11:24:41 +03:00
|
|
|
ruby_node_name(old_type), ruby_node_name(type));
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-28 14:00:20 +03:00
|
|
|
#endif
|
2019-09-09 19:37:06 +03:00
|
|
|
|
2021-11-17 21:40:49 +03:00
|
|
|
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)
|
2020-07-24 02:00:28 +03:00
|
|
|
{
|
2021-11-17 21:40:49 +03:00
|
|
|
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;
|
2020-07-24 02:00:28 +03:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_delete_node(rb_ast_t *ast, NODE *n)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
(void)ast;
|
|
|
|
(void)n;
|
|
|
|
/* should we implement freelist? */
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
rb_ast_t *
|
|
|
|
rb_ast_new(rb_parser_config_t *config)
|
|
|
|
{
|
|
|
|
node_buffer_t *nb = rb_node_buffer_new(config);
|
|
|
|
config->counter++;
|
|
|
|
return config->ast_new((VALUE)nb);
|
|
|
|
}
|
|
|
|
#else
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_t *
|
2017-10-27 19:44:57 +03:00
|
|
|
rb_ast_new(void)
|
|
|
|
{
|
2018-08-22 13:38:55 +03:00
|
|
|
node_buffer_t *nb = rb_node_buffer_new();
|
2021-04-27 09:31:19 +03:00
|
|
|
rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, (VALUE)nb);
|
2018-08-22 13:38:55 +03:00
|
|
|
return ast;
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
2023-05-28 14:00:20 +03:00
|
|
|
#endif
|
2017-10-27 19:44:57 +03:00
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE * node);
|
2019-09-09 19:37:06 +03:00
|
|
|
|
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_buffer_elements(rb_ast_t *ast, node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
long cursor;
|
|
|
|
for (cursor = 0; cursor < len; cursor++) {
|
2023-05-28 14:00:20 +03:00
|
|
|
func(ast, ctx, &nbe->buf[cursor]);
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, void *ctx)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
node_buffer_elem_t *nbe = nb->head;
|
|
|
|
|
|
|
|
/* iterate over the head first because it's not full */
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_buffer_elements(ast, nbe, nb->idx, func, ctx);
|
2019-09-09 19:37:06 +03:00
|
|
|
|
|
|
|
nbe = nbe->next;
|
|
|
|
while (nbe) {
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_buffer_elements(ast, nbe, nbe->len, func, ctx);
|
2019-09-09 19:37:06 +03:00
|
|
|
nbe = nbe->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
mark_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
bug_report_func rb_bug = ast->node_buffer->config->bug;
|
|
|
|
#endif
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
switch (nd_type(node)) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_ARGS:
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
struct rb_args_info *args = node->nd_ainfo;
|
2019-09-10 01:46:07 +03:00
|
|
|
rb_gc_mark_movable(args->imemo);
|
2019-09-09 19:37:06 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_MATCH:
|
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
2020-11-01 10:19:07 +03:00
|
|
|
rb_gc_mark_movable(node->nd_lit);
|
|
|
|
break;
|
2020-08-01 19:03:43 +03:00
|
|
|
case NODE_ARYPTN:
|
|
|
|
case NODE_FNDPTN:
|
2020-11-01 10:19:07 +03:00
|
|
|
rb_gc_mark_movable(node->nd_rval);
|
2019-09-27 04:20:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-24 11:24:41 +03:00
|
|
|
rb_bug("unreachable node %s", ruby_node_name(nd_type(node)));
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:46:07 +03:00
|
|
|
static void
|
2023-05-28 14:00:20 +03:00
|
|
|
update_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
|
2019-09-10 01:46:07 +03:00
|
|
|
{
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
bug_report_func rb_bug = ast->node_buffer->config->bug;
|
|
|
|
#endif
|
|
|
|
|
2019-09-10 01:46:07 +03:00
|
|
|
switch (nd_type(node)) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_ARGS:
|
2019-09-10 01:46:07 +03:00
|
|
|
{
|
|
|
|
struct rb_args_info *args = node->nd_ainfo;
|
|
|
|
args->imemo = rb_gc_location(args->imemo);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-28 22:56:36 +03:00
|
|
|
case NODE_MATCH:
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
2020-11-01 10:19:07 +03:00
|
|
|
node->nd_lit = rb_gc_location(node->nd_lit);
|
|
|
|
break;
|
2020-08-01 19:03:43 +03:00
|
|
|
case NODE_ARYPTN:
|
|
|
|
case NODE_FNDPTN:
|
2020-11-01 10:19:07 +03:00
|
|
|
node->nd_rval = rb_gc_location(node->nd_rval);
|
2019-09-27 04:20:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("unreachable");
|
2019-09-10 01:46:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ast_update_references(rb_ast_t *ast)
|
|
|
|
{
|
|
|
|
if (ast->node_buffer) {
|
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
|
|
|
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_node_values(ast, &nb->markable, update_ast_value, NULL);
|
2019-09-10 01:46:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_mark(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2022-09-23 16:40:02 +03:00
|
|
|
if (ast->node_buffer) {
|
|
|
|
rb_gc_mark(ast->node_buffer->mark_hash);
|
|
|
|
rb_gc_mark(ast->node_buffer->tokens);
|
2019-09-09 19:37:06 +03:00
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
2023-05-28 14:00:20 +03:00
|
|
|
iterate_node_values(ast, &nb->markable, mark_ast_value, NULL);
|
|
|
|
if (ast->body.script_lines) rb_gc_mark(ast->body.script_lines);
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_free(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2017-11-10 16:46:38 +03:00
|
|
|
if (ast->node_buffer) {
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
rb_parser_config_t *config = ast->node_buffer->config;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_node_buffer_free(ast, ast->node_buffer);
|
2017-11-10 16:46:38 +03:00
|
|
|
ast->node_buffer = 0;
|
2023-05-28 14:00:20 +03:00
|
|
|
#ifdef UNIVERSAL_PARSER
|
|
|
|
config->counter--;
|
|
|
|
if (config->counter <= 0) {
|
|
|
|
rb_ruby_parser_config_free(config);
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-10 16:46:38 +03:00
|
|
|
}
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-23 01:24:52 +03:00
|
|
|
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);
|
2019-09-09 19:37:06 +03:00
|
|
|
size += buffer_list_size(&nb->unmarkable);
|
|
|
|
size += buffer_list_size(&nb->markable);
|
2019-04-23 01:24:52 +03:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_dispose(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
rb_ast_free(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_add_mark_object(rb_ast_t *ast, VALUE obj)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2019-11-05 03:04:58 +03:00
|
|
|
if (NIL_P(ast->node_buffer->mark_hash)) {
|
|
|
|
RB_OBJ_WRITE(ast, &ast->node_buffer->mark_hash, rb_ident_hash_new());
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
2019-11-05 03:04:58 +03:00
|
|
|
rb_hash_aset(ast->node_buffer->mark_hash, obj, Qtrue);
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
2022-09-23 16:40:02 +03:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ast_tokens(rb_ast_t *ast)
|
|
|
|
{
|
|
|
|
return ast->node_buffer->tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ast_set_tokens(rb_ast_t *ast, VALUE tokens)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(ast, &ast->node_buffer->tokens, tokens);
|
|
|
|
}
|
2023-05-28 14:00:20 +03:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_node_set_type(NODE *n, enum node_type t)
|
|
|
|
{
|
|
|
|
#if RUBY_DEBUG
|
|
|
|
rb_ast_node_type_change(n, t);
|
|
|
|
#endif
|
|
|
|
return nd_init_type(n, t);
|
|
|
|
}
|