diff options
author | yui-knk <spiketeika@gmail.com> | 2023-08-22 10:26:38 +0900 |
---|---|---|
committer | Yuichiro Kaneko <spiketeika@gmail.com> | 2023-09-28 11:58:10 +0900 |
commit | 74c67811537c0c1840668c218dc0e2510d00b473 (patch) | |
tree | 1f387a71cf9f797217721345e85e098b790187e5 /node.h | |
parent | 684686a1e14d923b43cfd6c1d5a80222281a4070 (diff) |
Change RNode structure from union to struct
All kind of AST nodes use same struct RNode, which has u1, u2, u3 union members
for holding different kind of data.
This has two problems.
1. Low flexibility of data structure
Some nodes, for example NODE_TRUE, don’t use u1, u2, u3. On the other hand,
NODE_OP_ASGN2 needs more than three union members. However they use same
structure definition, need to allocate three union members for NODE_TRUE and
need to separate NODE_OP_ASGN2 into another node.
This change removes the restriction so make it possible to
change data structure by each node type.
2. No compile time check for union member access
It’s developer’s responsibility for using correct member for each node type when it’s union.
This change clarifies which node has which type of fields and enables compile time check.
This commit also changes node_buffer_elem_struct buf management to handle
different size data with alignment.
Diffstat (limited to 'node.h')
-rw-r--r-- | node.h | 36 |
1 files changed, 19 insertions, 17 deletions
@@ -19,12 +19,14 @@ typedef void (*bug_report_func)(const char *fmt, ...); typedef struct node_buffer_elem_struct { struct node_buffer_elem_struct *next; - long len; - NODE buf[FLEX_ARY_LEN]; + long len; /* Length of nodes */ + size_t allocated; /* Total memory size of allocated buf */ + size_t used; /* Current usage of buf */ + NODE **nodes; /* Array of node pointers */ + 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; @@ -59,14 +61,14 @@ VALUE rb_ast_tokens(rb_ast_t *ast); void rb_ast_node_type_change(NODE *n, enum node_type type); #endif const char *ruby_node_name(int node); -void rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2); +void rb_node_init(NODE *n, enum node_type type); void rb_ast_mark(rb_ast_t*); void rb_ast_update_references(rb_ast_t*); void rb_ast_free(rb_ast_t*); void rb_ast_add_mark_object(rb_ast_t*, VALUE); void rb_ast_set_tokens(rb_ast_t*, VALUE); -NODE *rb_ast_newnode(rb_ast_t*, enum node_type type); +NODE *rb_ast_newnode(rb_ast_t*, enum node_type type, size_t size, size_t alignment); void rb_ast_delete_node(rb_ast_t*, NODE *n); rb_ast_id_table_t *rb_ast_new_local_table(rb_ast_t*, int); rb_ast_id_table_t *rb_ast_resize_latest_local_table(rb_ast_t*, int); @@ -100,21 +102,21 @@ RUBY_SYMBOL_EXPORT_END #define NODE_SPECIAL_EXCESSIVE_COMMA ((ID)1) #define NODE_SPECIAL_NO_REST_KEYWORD ((NODE *)-1) -#define nd_first_column(n) ((int)((n)->nd_loc.beg_pos.column)) -#define nd_set_first_column(n, v) ((n)->nd_loc.beg_pos.column = (v)) -#define nd_first_lineno(n) ((int)((n)->nd_loc.beg_pos.lineno)) -#define nd_set_first_lineno(n, v) ((n)->nd_loc.beg_pos.lineno = (v)) -#define nd_first_loc(n) ((n)->nd_loc.beg_pos) +#define nd_first_column(n) ((int)(RNODE(n)->nd_loc.beg_pos.column)) +#define nd_set_first_column(n, v) (RNODE(n)->nd_loc.beg_pos.column = (v)) +#define nd_first_lineno(n) ((int)(RNODE(n)->nd_loc.beg_pos.lineno)) +#define nd_set_first_lineno(n, v) (RNODE(n)->nd_loc.beg_pos.lineno = (v)) +#define nd_first_loc(n) (RNODE(n)->nd_loc.beg_pos) #define nd_set_first_loc(n, v) (nd_first_loc(n) = (v)) -#define nd_last_column(n) ((int)((n)->nd_loc.end_pos.column)) -#define nd_set_last_column(n, v) ((n)->nd_loc.end_pos.column = (v)) -#define nd_last_lineno(n) ((int)((n)->nd_loc.end_pos.lineno)) -#define nd_set_last_lineno(n, v) ((n)->nd_loc.end_pos.lineno = (v)) -#define nd_last_loc(n) ((n)->nd_loc.end_pos) +#define nd_last_column(n) ((int)(RNODE(n)->nd_loc.end_pos.column)) +#define nd_set_last_column(n, v) (RNODE(n)->nd_loc.end_pos.column = (v)) +#define nd_last_lineno(n) ((int)(RNODE(n)->nd_loc.end_pos.lineno)) +#define nd_set_last_lineno(n, v) (RNODE(n)->nd_loc.end_pos.lineno = (v)) +#define nd_last_loc(n) (RNODE(n)->nd_loc.end_pos) #define nd_set_last_loc(n, v) (nd_last_loc(n) = (v)) -#define nd_node_id(n) ((n)->node_id) -#define nd_set_node_id(n,id) ((n)->node_id = (id)) +#define nd_node_id(n) (RNODE(n)->node_id) +#define nd_set_node_id(n,id) (RNODE(n)->node_id = (id)) static inline bool nd_type_p(const NODE *n, enum node_type t) |