Skip to content

Commit ddd8da4

Browse files
hasumikinyui-knk
authored andcommitted
[Universal parser] Improve AST structure
This patch moves `ast->node_buffer->config` to `ast->config` aiming to improve readability and maintainability of the source. ## Background We could not add the `config` field to the `rb_ast_t *` due to the five-word restriction of the IMEMO object. But it is now doable by merging #10618 ## About assigning `&rb_global_parser_config` to `ast->config` in `ast_alloc()` The approach of not setting `ast->config` in `ast_alloc()` means that the client, CRuby in this scenario, that directly calls `ast_alloc()` will be responsible for releasing it if a resource that is passed to AST needs to be released. However, we have put on hold whether we can guarantee the above so far, thus, this patch looks like that. ``` // ruby_parser.c static VALUE ast_alloc(void) { rb_ast_t *ast; VALUE vast = TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast); #ifdef UNIVERSAL_PARSER ast = (rb_ast_t *)DATA_PTR(vast); ast->config = &rb_global_parser_config; #endif return vast; } ```
1 parent 8ad0b2c commit ddd8da4

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed

node.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,16 @@ rb_node_buffer_new(void)
5959
init_node_buffer_list(&nb->buffer_list, (node_buffer_elem_t*)&nb[1], ruby_xmalloc);
6060
nb->local_tables = 0;
6161
nb->tokens = 0;
62-
#ifdef UNIVERSAL_PARSER
63-
nb->config = config;
64-
#endif
6562
return nb;
6663
}
6764

6865
#ifdef UNIVERSAL_PARSER
6966
#undef ruby_xmalloc
70-
#define ruby_xmalloc ast->node_buffer->config->malloc
67+
#define ruby_xmalloc ast->config->malloc
7168
#undef xfree
72-
#define xfree ast->node_buffer->config->free
73-
#define rb_xmalloc_mul_add ast->node_buffer->config->xmalloc_mul_add
74-
#define ruby_xrealloc(var,size) (ast->node_buffer->config->realloc_n((void *)var, 1, size))
69+
#define xfree ast->config->free
70+
#define rb_xmalloc_mul_add ast->config->xmalloc_mul_add
71+
#define ruby_xrealloc(var,size) (ast->config->realloc_n((void *)var, 1, size))
7572
#endif
7673

7774
typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE *node);
@@ -218,8 +215,8 @@ free_ast_value(rb_ast_t *ast, void *ctx, NODE *node)
218215
static void
219216
rb_node_buffer_free(rb_ast_t *ast, node_buffer_t *nb)
220217
{
221-
if (ast->node_buffer && ast->node_buffer->tokens) {
222-
parser_tokens_free(ast, ast->node_buffer->tokens);
218+
if (nb && nb->tokens) {
219+
parser_tokens_free(ast, nb->tokens);
223220
}
224221
iterate_node_values(ast, &nb->buffer_list, free_ast_value, NULL);
225222
node_buffer_list_free(ast, &nb->buffer_list);
@@ -304,7 +301,9 @@ rb_ast_t *
304301
rb_ast_new(const rb_parser_config_t *config)
305302
{
306303
node_buffer_t *nb = rb_node_buffer_new(config);
307-
return config->ast_new(nb);
304+
rb_ast_t *ast = config->ast_new(nb);
305+
ast->config = config;
306+
return ast;
308307
}
309308
#else
310309
rb_ast_t *
@@ -351,24 +350,8 @@ script_lines_free(rb_ast_t *ast, rb_parser_ary_t *script_lines)
351350
void
352351
rb_ast_free(rb_ast_t *ast)
353352
{
354-
/* TODO
355-
* The current impl. of rb_ast_free() and rb_ast_dispose() is complicated.
356-
* Upcoming task of changing `ast->node_buffer->config` to `ast->config`,
357-
* that is based on "deIMEMO" of `rb_ast_t *`, will let us simplify the code.
358-
*/
359-
#ifdef UNIVERSAL_PARSER
360-
if (ast && ast->node_buffer) {
361-
void (*free_func)(void *) = xfree;
362-
script_lines_free(ast, ast->body.script_lines);
363-
ast->body.script_lines = NULL;
364-
rb_node_buffer_free(ast, ast->node_buffer);
365-
ast->node_buffer = 0;
366-
free_func(ast);
367-
}
368-
#else
369353
rb_ast_dispose(ast);
370354
xfree(ast);
371-
#endif
372355
}
373356

374357
static size_t
@@ -431,16 +414,12 @@ rb_ast_memsize(const rb_ast_t *ast)
431414
void
432415
rb_ast_dispose(rb_ast_t *ast)
433416
{
434-
#ifdef UNIVERSAL_PARSER
435-
// noop. See the comment in rb_ast_free().
436-
#else
437417
if (ast && ast->node_buffer) {
438418
script_lines_free(ast, ast->body.script_lines);
439419
ast->body.script_lines = NULL;
440420
rb_node_buffer_free(ast, ast->node_buffer);
441421
ast->node_buffer = 0;
442422
}
443-
#endif
444423
}
445424

446425
VALUE

node.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ struct node_buffer_struct {
3939
// - location info
4040
// Array, whose entry is array
4141
rb_parser_ary_t *tokens;
42-
#ifdef UNIVERSAL_PARSER
43-
const rb_parser_config_t *config;
44-
#endif
4542
};
4643

4744
RUBY_SYMBOL_EXPORT_BEGIN

ruby_parser.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,7 @@ static void
758758
ast_free(void *ptr)
759759
{
760760
rb_ast_t *ast = (rb_ast_t *)ptr;
761-
if (ast) {
762-
rb_ast_free(ast);
763-
}
761+
rb_ast_free(ast);
764762
}
765763

766764
static const rb_data_type_t ast_data_type = {
@@ -777,7 +775,12 @@ static VALUE
777775
ast_alloc(void)
778776
{
779777
rb_ast_t *ast;
780-
return TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast);
778+
VALUE vast = TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast);
779+
#ifdef UNIVERSAL_PARSER
780+
ast = (rb_ast_t *)DATA_PTR(vast);
781+
ast->config = &rb_global_parser_config;
782+
#endif
783+
return vast;
781784
}
782785

783786
VALUE

rubyparser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,10 @@ typedef struct RNode_ERROR {
12091209

12101210
typedef struct node_buffer_struct node_buffer_t;
12111211

1212+
#ifdef UNIVERSAL_PARSER
1213+
typedef struct rb_parser_config_struct rb_parser_config_t;
1214+
#endif
1215+
12121216
typedef struct rb_ast_body_struct {
12131217
const NODE *root;
12141218
rb_parser_ary_t *script_lines;
@@ -1219,6 +1223,9 @@ typedef struct rb_ast_body_struct {
12191223
typedef struct rb_ast_struct {
12201224
node_buffer_t *node_buffer;
12211225
rb_ast_body_t body;
1226+
#ifdef UNIVERSAL_PARSER
1227+
const rb_parser_config_t *config;
1228+
#endif
12221229
} rb_ast_t;
12231230

12241231

0 commit comments

Comments
 (0)