{
self->node.context = ctx;
self->node.destroy = &_ast_node_destroy;
+ self->node.keep = false;
}
/* General expression initialization */
ast_setfunc(&self->expression, codegen, codegen);
}
-ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
+ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t, bool keep)
{
ast_instantiate(ast_value, ctx, ast_value_delete);
ast_expression_init((ast_expression*)self,
(ast_expression_codegen*)&ast_value_codegen);
+ self->expression.node.keep = keep;
self->name = name ? util_strdup(name) : NULL;
self->vtype = t;
if (self->name)
mem_d((void*)self->name);
for (i = 0; i < self->params_count; ++i)
- ast_delete(self->params[i]);
+ ast_unref(self->params[i]);
MEM_VECTOR_CLEAR(self, params);
- if (self->next)
+ if (self->next) /* delete, not unref, types are always copied */
ast_delete(self->next);
if (self->isconst) {
switch (self->vtype)
void ast_binary_delete(ast_binary *self)
{
+ ast_unref(self->left);
+ ast_unref(self->right);
mem_d(self);
}
void ast_store_delete(ast_store *self)
{
+ ast_unref(self->dest);
+ ast_unref(self->source);
mem_d(self);
}
ast_delete(self->locals[i]);
MEM_VECTOR_CLEAR(self, locals);
for (i = 0; i < self->exprs_count; ++i)
- ast_delete(self->exprs[i]);
+ ast_unref(self->exprs[i]);
MEM_VECTOR_CLEAR(self, exprs);
mem_d(self);
}
} ast_node_common;
#define ast_delete(x) ( ( (ast_node*)(x) ) -> node.destroy )((ast_node*)(x))
+#define ast_unref(x) do \
+{ \
+ if (! (((ast_node*)(x))->node.keep) ) { \
+ ast_delete(x); \
+ } \
+} while(0)
/* Expression interface
*
*/
MEM_VECTOR_MAKE(ast_value*, params);
};
-ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int qctype);
+ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int qctype, bool keep);
void ast_value_delete(ast_value*);
void ast_value_set_name(ast_value*, const char *name);