lang: fix a bunch of memory leaks
This commit is contained in:
@@ -252,6 +252,47 @@ void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out)
|
||||
|
||||
void ivy_ast_node_destroy(struct ivy_ast_node *node)
|
||||
{
|
||||
struct ivy_ast_node_iterator it = {};
|
||||
b_queue_push_back(&it.it_queue, &node->n_it.it_entry);
|
||||
node->n_it.it_depth = 0;
|
||||
|
||||
b_queue_iterator q_it;
|
||||
b_queue_iterator_begin(&it.it_queue, &q_it);
|
||||
|
||||
while (b_queue_iterator_is_valid(&q_it)) {
|
||||
struct ivy_ast_node_iterator_entry *it_entry = b_unbox(
|
||||
struct ivy_ast_node_iterator_entry, q_it.entry, it_entry);
|
||||
node = b_unbox(struct ivy_ast_node, it_entry, n_it);
|
||||
|
||||
if (!node) {
|
||||
/* this should never happen. */
|
||||
return;
|
||||
}
|
||||
|
||||
const struct ast_node_type *type = get_ast_node_type(node->n_type);
|
||||
if (type->n_collect_children) {
|
||||
it.it_insert_after = q_it.entry;
|
||||
type->n_collect_children(node, &it);
|
||||
}
|
||||
|
||||
b_queue_iterator_next(&q_it);
|
||||
}
|
||||
|
||||
while (!b_queue_empty(&it.it_queue)) {
|
||||
b_queue_entry *entry = b_queue_pop_back(&it.it_queue);
|
||||
if (!entry) {
|
||||
break;
|
||||
}
|
||||
|
||||
node = b_unbox(struct ivy_ast_node, entry, n_it);
|
||||
|
||||
const struct ast_node_type *type = get_ast_node_type(node->n_type);
|
||||
if (type && type->n_destroy) {
|
||||
type->n_destroy(node);
|
||||
}
|
||||
|
||||
free(node);
|
||||
}
|
||||
}
|
||||
|
||||
#define ENUM_STR(x) \
|
||||
|
||||
Reference in New Issue
Block a user