]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
ast_value and ast_function are linked together when using ast_function_new, note...
authorWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Apr 2012 13:57:19 +0000 (15:57 +0200)
committerWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Apr 2012 18:55:41 +0000 (20:55 +0200)
ast.c
ast.h

diff --git a/ast.c b/ast.c
index a42b0fad700b968aa303ee26eed1ad6937c666cd..773c498ce205be21df24c684f7ae9eb3c3379744 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -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);
diff --git a/ast.h b/ast.h
index 382190c9dc69374a8d678a53564307a764ff5b02..eb456b1e8e7ce64915eeaddaf16a799000aa37a2 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -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);