12 #ifdef UNIVERSAL_PARSER
15 #include "rubyparser.h"
18 #include "internal/variable.h"
20 #define NODE_BUF_DEFAULT_SIZE (sizeof(struct RNode) * 16)
23 init_node_buffer_elem(node_buffer_elem_t *nbe,
size_t allocated,
void *
xmalloc(
size_t))
25 nbe->allocated = allocated;
34 init_node_buffer_elem(head, NODE_BUF_DEFAULT_SIZE,
xmalloc);
35 nb->head = nb->last = head;
36 nb->head->next = NULL;
39 #ifdef UNIVERSAL_PARSER
40 #define ruby_xmalloc config->malloc
43 #ifdef UNIVERSAL_PARSER
45 rb_node_buffer_new(
const rb_parser_config_t *config)
48 rb_node_buffer_new(
void)
51 const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_SIZE;
52 const size_t alloc_size =
sizeof(
node_buffer_t) + (bucket_size);
55 offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_SIZE
58 init_node_buffer_list(&nb->buffer_list, (node_buffer_elem_t*)&nb[1],
ruby_xmalloc);
64 #ifdef UNIVERSAL_PARSER
66 #define ruby_xmalloc ast->config->malloc
68 #define xfree ast->config->free
69 #define rb_xmalloc_mul_add ast->config->xmalloc_mul_add
70 #define ruby_xrealloc(var,size) (ast->config->realloc_n((void *)var, 1, size))
73 typedef void node_itr_t(
rb_ast_t *ast,
void *ctx,
NODE *node);
77 rb_node_init(
NODE *n,
enum node_type
type)
80 nd_init_type(RNODE(n),
type);
81 RNODE(n)->nd_loc.beg_pos.lineno = 0;
82 RNODE(n)->nd_loc.beg_pos.column = 0;
83 RNODE(n)->nd_loc.end_pos.lineno = 0;
84 RNODE(n)->nd_loc.end_pos.column = 0;
85 RNODE(n)->node_id = -1;
89 rb_node_name(
int node)
92 #include "node_name.inc"
98 #ifdef UNIVERSAL_PARSER
100 ruby_node_name(
int node)
102 return rb_node_name(node);
106 ruby_node_name(
int node)
108 const char *name = rb_node_name(node);
110 if (!name)
rb_bug(
"unknown node: %d", node);
118 node_buffer_elem_t *nbe = nb->head;
119 while (nbe != nb->last) {
135 ID ids[FLEX_ARY_LEN];
151 parser_string_free(ast, token->str);
158 for (
long i = 0; i < tokens->len; i++) {
159 parser_ast_token_free(ast, tokens->data[i]);
176 switch (nd_type(node)) {
178 parser_string_free(ast, RNODE_STR(node)->
string);
181 parser_string_free(ast, RNODE_DSTR(node)->
string);
184 parser_string_free(ast, RNODE_XSTR(node)->
string);
187 parser_string_free(ast, RNODE_DXSTR(node)->
string);
190 parser_string_free(ast, RNODE_SYM(node)->
string);
194 parser_string_free(ast, RNODE_REGX(node)->
string);
197 parser_string_free(ast, RNODE_DSYM(node)->
string);
200 parser_string_free(ast, RNODE_DREGX(node)->
string);
203 parser_string_free(ast, RNODE_FILE(node)->path);
206 xfree(RNODE_INTEGER(node)->val);
209 xfree(RNODE_FLOAT(node)->val);
212 xfree(RNODE_RATIONAL(node)->val);
215 xfree(RNODE_IMAGINARY(node)->val);
218 parser_nodes_free(ast, RNODE_UNDEF(node)->nd_undefs);
228 if (nb && nb->tokens) {
229 parser_tokens_free(ast, nb->tokens);
231 iterate_node_values(ast, &nb->buffer_list, free_ast_value, NULL);
232 node_buffer_list_free(ast, &nb->buffer_list);
234 while (local_table) {
237 local_table = next_table;
242 #define buf_add_offset(nbe, offset) ((char *)(nbe->buf) + (offset))
250 padding = alignment - (size_t)buf_add_offset(nb->head, nb->head->used) % alignment;
251 padding = padding == alignment ? 0 : padding;
253 if (nb->head->used + size + padding > nb->head->allocated) {
254 size_t n = nb->head->allocated * 2;
255 node_buffer_elem_t *nbe;
256 nbe = rb_xmalloc_mul_add(n,
sizeof(
char *), offsetof(node_buffer_elem_t, buf));
258 nbe->next = nb->head;
263 ptr = (
NODE *)buf_add_offset(nb->head, nb->head->used + padding);
264 nb->head->used += (size + padding);
265 nb->head->nodes[nb->head->len++] =
ptr;
270 rb_ast_newnode(
rb_ast_t *ast,
enum node_type
type,
size_t size,
size_t alignment)
274 return ast_newnode_in_bucket(ast, bucket, size, alignment);
278 rb_ast_new_local_table(
rb_ast_t *ast,
int size)
282 link->next = ast->node_buffer->local_tables;
283 ast->node_buffer->local_tables = link;
290 rb_ast_resize_latest_local_table(
rb_ast_t *ast,
int size)
295 ast->node_buffer->local_tables = link;
309 #ifdef UNIVERSAL_PARSER
311 rb_ast_new(
const rb_parser_config_t *config)
315 ast->config = config;
316 ast->node_buffer = nb;
325 ast->node_buffer = nb;
331 iterate_buffer_elements(
rb_ast_t *ast, node_buffer_elem_t *nbe,
long len, node_itr_t *func,
void *ctx)
334 for (cursor = 0; cursor <
len; cursor++) {
335 func(ast, ctx, nbe->nodes[cursor]);
342 node_buffer_elem_t *nbe = nb->head;
345 iterate_buffer_elements(ast, nbe, nbe->len, func, ctx);
353 if (!script_lines)
return;
354 for (
long i = 0; i < script_lines->len; i++) {
357 xfree(script_lines->data);
372 node_buffer_elem_t *nbe = nb->head;
373 while (nbe != nb->last) {
374 size += offsetof(node_buffer_elem_t, buf) + nbe->used;
393 size += buffer_list_size(&nb->buffer_list);
394 link = nb->local_tables;
400 size += link->size *
sizeof(
ID);
406 for (i = 0; i < tokens->len; i++) {
410 size += token->str->len + 1;
416 for (i = 0; i < script_lines->len; i++) {
428 if (ast && ast->node_buffer) {
429 script_lines_free(ast, ast->body.script_lines);
430 ast->body.script_lines = NULL;
431 rb_node_buffer_free(ast, ast->node_buffer);
432 ast->node_buffer = 0;
437 rb_node_set_type(
NODE *n,
enum node_type t)
439 return nd_init_type(n, t);
#define xfree
Old name of ruby_xfree.
#define xmalloc
Old name of ruby_xmalloc.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
VALUE type(ANYARGS)
ANYARGS-ed function type.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
void * ruby_xcalloc(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc2(), except it returns a zero-filled storage instance.
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.