]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Fix a possibly uninitialized variable
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index f12c57431344ec4b1cf2cee276ec4941acc21a2a..a155f50d879662f4b0bbd4231d405bff1b647d2b 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -308,6 +308,31 @@ void ast_loop_delete(ast_loop *self)
     mem_d(self);
 }
 
+ast_call* ast_call_new(lex_ctx ctx,
+                       ast_expression *funcexpr)
+{
+    ast_instantiate(ast_call, ctx, ast_call_delete);
+    ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
+
+    MEM_VECTOR_INIT(self, params);
+
+    return self;
+}
+
+void ast_call_delete(ast_call *self)
+{
+    size_t i;
+    for (i = 0; i < self->params_count; ++i)
+        ast_unref(self->params[i]);
+    MEM_VECTOR_CLEAR(self, params);
+
+    if (self->func)
+        ast_unref(self->func);
+
+    ast_expression_delete((ast_expression*)self);
+    mem_d(self);
+}
+
 ast_store* ast_store_new(lex_ctx ctx, int op,
                          ast_value *dest, ast_expression *source)
 {
@@ -772,7 +797,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
             return false;
     } else
         ontrue = NULL;
-    
+
     /* on-false path */
     if (self->on_false) {
         /* create on-false block */
@@ -793,7 +818,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     /* Merge block were they all merge in to */
     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
     if (!merge)
-        return NULL;
+        return false;
 
     /* add jumps ot the merge block */
     if (ontrue && !ir_block_create_jump(ontrue, merge))
@@ -844,7 +869,7 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
         return false;
 
     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
-    
+
     /* generate the condition */
     func->curblock = cond;
     cgen = self->cond->expression.codegen;
@@ -865,7 +890,7 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
             return false;
     }
-    
+
     /* create on-false block */
     onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
     if (!onfalse)
@@ -884,7 +909,7 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
     /* create merge block */
     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
     if (!merge)
-        return NULL;
+        return false;
     /* jump to merge block */
     if (!ir_block_create_jump(ontrue, merge))
         return false;
@@ -1148,3 +1173,56 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
 
     return true;
 }
+
+bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
+{
+    /* TODO: call ir codegen */
+    ast_expression_codegen *cgen;
+    ir_value_vector         params;
+    ir_instr               *callinstr;
+    size_t i;
+
+    ir_value *funval = NULL;
+
+    /* return values are never rvalues */
+    (void)lvalue;
+
+    cgen = self->func->expression.codegen;
+    if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
+        return false;
+    if (!funval)
+        return false;
+
+    MEM_VECTOR_INIT(&params, v);
+
+    /* parameters */
+    for (i = 0; i < self->params_count; ++i)
+    {
+        ir_value *param;
+        ast_expression *expr = self->params[i];
+
+        cgen = expr->expression.codegen;
+        if (!(*cgen)(expr, func, false, &param))
+            goto error;
+        if (!param)
+            goto error;
+        if (!ir_value_vector_v_add(&params, param))
+            goto error;
+    }
+
+    callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval, funval->outtype);
+    if (!callinstr)
+        goto error;
+
+    for (i = 0; i < params.v_count; ++i) {
+        if (!ir_call_param(callinstr, params.v[i]))
+            goto error;
+    }
+
+    *out = ir_call_value(callinstr);
+
+    return true;
+error:
+    MEM_VECTOR_CLEAR(&params, v);
+    return false;
+}