]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Factoring out variable parsing so it can be used for locals too
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 21e43b3933ad188e425f7b496155001bb3382360..d519919ac10b50504b4c0e7d846be66c6cb5fe90 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -316,8 +316,11 @@ ast_call* ast_call_new(lex_ctx ctx,
 
     MEM_VECTOR_INIT(self, params);
 
+    self->func = funcexpr;
+
     return self;
 }
+MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
 
 void ast_call_delete(ast_call *self)
 {
@@ -396,7 +399,6 @@ ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
     self->vtype = vtype;
     self->name = name ? util_strdup(name) : NULL;
     MEM_VECTOR_INIT(self, blocks);
-    MEM_VECTOR_INIT(self, params);
 
     self->labelcount = 0;
     self->builtin = 0;
@@ -414,7 +416,6 @@ ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
 }
 
 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
-MEM_VEC_FUNCTIONS(ast_function, ast_value*, params)
 
 void ast_function_delete(ast_function *self)
 {
@@ -433,9 +434,6 @@ void ast_function_delete(ast_function *self)
     for (i = 0; i < self->blocks_count; ++i)
         ast_delete(self->blocks[i]);
     MEM_VECTOR_CLEAR(self, blocks);
-    for (i = 0; i < self->params_count; ++i)
-        ast_delete(self->params[i]);
-    MEM_VECTOR_CLEAR(self, params);
     mem_d(self);
 }
 
@@ -488,8 +486,10 @@ bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_valu
      * and the ast-user should take care of ast_global_codegen to be used
      * on all the globals.
      */
-    if (!self->ir_v)
+    if (!self->ir_v) {
+        printf("ast_value used before generated (%s)\n", self->name);
         return false;
+    }
     *out = self->ir_v;
     return true;
 }
@@ -504,6 +504,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir)
             return false;
 
         self->constval.vfunc->ir_func = func;
+        self->ir_v = func->value;
         /* The function is filled later on ast_function_codegen... */
         return true;
     }
@@ -608,9 +609,9 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
         return false;
     }
 
-    for (i = 0; i < self->params_count; ++i)
+    for (i = 0; i < self->vtype->params_count; ++i)
     {
-        if (!ir_function_params_add(irf, self->params[i]->expression.vtype))
+        if (!ir_function_params_add(irf, self->vtype->params[i]->expression.vtype))
             return false;
     }
 
@@ -634,7 +635,9 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     {
         if (!self->vtype->expression.next ||
             self->vtype->expression.next->expression.vtype == TYPE_VOID)
+        {
             return ir_block_create_return(self->curblock, NULL);
+        }
         else
         {
             /* error("missing return"); */