ast_value and ast_function are linked together when using ast_function_new, note however, that neither will delete the other in their _delete functions.

This commit is contained in:
Wolfgang Bumiller 2012-04-28 15:57:19 +02:00
parent df9a685f61
commit 0f38a560b6
2 changed files with 25 additions and 2 deletions

25
ast.c
View file

@ -90,6 +90,10 @@ void ast_value_delete(ast_value* self)
case TYPE_STRING:
mem_d((void*)self->constval.vstring);
break;
case TYPE_FUNCTION:
/* unlink us from the function node */
self->constval.vfunc->vtype = NULL;
break;
/* NOTE: delete function? currently collected in
* the parser structure
*/
@ -176,12 +180,22 @@ void ast_block_delete(ast_block *self)
ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
{
if (!vtype)
return NULL;
if (vtype->isconst)
return NULL;
if (vtype->vtype != TYPE_FUNCTION)
return NULL;
ast_instantiate(ast_function, ctx, ast_function_delete);
self->vtype = vtype;
self->name = name ? util_strdup(name) : NULL;
MEM_VECTOR_INIT(self, blocks);
vtype->isconst = true;
vtype->constval.vfunc = self;
return self;
}
@ -192,8 +206,15 @@ void ast_function_delete(ast_function *self)
size_t i;
if (self->name)
mem_d((void*)self->name);
if (self->vtype)
ast_value_delete(self->vtype);
if (self->vtype) {
/* ast_value_delete(self->vtype); */
self->vtype->isconst = false;
self->vtype->constval.vfunc = NULL;
/* We use unref - if it was stored in a global table it is supposed
* to be deleted from *there*
*/
ast_unref(self->vtype);
}
for (i = 0; i < self->blocks_count; ++i)
ast_delete(self->blocks[i]);
MEM_VECTOR_CLEAR(self, blocks);

2
ast.h
View file

@ -107,6 +107,7 @@ struct ast_value_s
MEM_VECTOR_MAKE(ast_value*, params);
};
ast_value* ast_value_new(lex_ctx ctx, const char *name, int qctype, bool keep);
/* This will NOT delete an underlying ast_function */
void ast_value_delete(ast_value*);
bool ast_value_set_name(ast_value*, const char *name);
@ -192,6 +193,7 @@ struct ast_function_s
MEM_VECTOR_MAKE(ast_block*, blocks);
};
ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype);
/* This will NOT delete the underlying ast_value */
void ast_function_delete(ast_function*);
MEM_VECTOR_PROTO(ast_function, ast_block*, blocks);