]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Sanity check in ast_generate_accessors
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index d797fc125c97a61436128d4d9d6b47a79a9af27d..f388eb396924d9201bad77404cdff956c0fa18b8 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -855,9 +855,12 @@ ast_call* ast_call_new(lex_ctx ctx,
     self->params = NULL;
     self->func   = funcexpr;
 
+/*
     self->expression.vtype = funcexpr->expression.next->expression.vtype;
     if (funcexpr->expression.next->expression.next)
         self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
+*/
+    ast_type_adopt(self, funcexpr->expression.next);
 
     return self;
 }
@@ -1075,6 +1078,17 @@ const char* ast_function_label(ast_function *self, const char *prefix)
  * But I can't imagine a pituation where the output is truly unnecessary.
  */
 
+void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
+{
+    if (out->vtype == TYPE_FIELD)
+        out->fieldtype = self->next->expression.vtype;
+    if (out->vtype == TYPE_FUNCTION)
+        out->outtype = self->next->expression.vtype;
+}
+
+#define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
+#define codegen_output_type_expr(a,o) (_ast_codegen_output_type(a,(o)))
+
 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
 {
     (void)func;
@@ -1148,8 +1162,6 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
                 return false;
             }
-            if (vtype == TYPE_FIELD)
-                v->fieldtype = elemtype->next->expression.vtype;
             v->context = ast_ctx(self);
             array->ir_v = self->ir_v = v;
 
@@ -1167,8 +1179,6 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                     compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
                     return false;
                 }
-                if (vtype == TYPE_FIELD)
-                    array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
                 array->ir_values[ai]->context = ast_ctx(self);
             }
             mem_d(name);
@@ -1201,8 +1211,6 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
             return false;
         }
-        if (vtype == TYPE_FIELD)
-            v->fieldtype = elemtype->next->expression.vtype;
         v->context = ast_ctx(self);
 
         namelen = strlen(self->name);
@@ -1219,8 +1227,6 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
                 return false;
             }
-            if (vtype == TYPE_FIELD)
-                self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
             self->ir_values[ai]->context = ast_ctx(self);
         }
         mem_d(name);
@@ -1235,8 +1241,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
             return false;
         }
-        if (self->expression.vtype == TYPE_FIELD)
-            v->fieldtype = self->expression.next->expression.vtype;
+        codegen_output_type(self, v);
         v->context = ast_ctx(self);
     }
 
@@ -1264,6 +1269,18 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 /* Cannot generate an IR value for a function,
                  * need a pointer pointing to a function rather.
                  */
+            case TYPE_FIELD:
+                if (!self->constval.vfield) {
+                    compile_error(ast_ctx(self), "field constant without vfield set");
+                    goto error;
+                }
+                if (!self->constval.vfield->ir_v) {
+                    compile_error(ast_ctx(self), "field constant generated before its field");
+                    goto error;
+                }
+                if (!ir_value_set_field(v, self->constval.vfield->ir_v))
+                    goto error;
+                break;
             default:
                 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
                 break;
@@ -1320,8 +1337,6 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
             compile_error(ast_ctx(self), "ir_function_create_local failed");
             return false;
         }
-        if (vtype == TYPE_FIELD)
-            v->fieldtype = elemtype->next->expression.vtype;
         v->context = ast_ctx(self);
 
         namelen = strlen(self->name);
@@ -1336,8 +1351,6 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
                 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
                 return false;
             }
-            if (vtype == TYPE_FIELD)
-                self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
             self->ir_values[ai]->context = ast_ctx(self);
         }
     }
@@ -1346,8 +1359,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         v = ir_function_create_local(func, self->name, self->expression.vtype, param);
         if (!v)
             return false;
-        if (self->expression.vtype == TYPE_FIELD)
-            v->fieldtype = self->expression.next->expression.vtype;
+        codegen_output_type(self, v);
         v->context = ast_ctx(self);
     }
 
@@ -1379,23 +1391,53 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
     v->cvq = self->cvq;
     self->ir_v = v;
 
+    if (!ast_generate_accessors(self, func->owner))
+        return false;
+    return true;
+
+error: /* clean up */
+    ir_value_delete(v);
+    return false;
+}
+
+bool ast_generate_accessors(ast_value *self, ir_builder *ir)
+{
+    size_t i;
+    if (!self->setter || !self->getter)
+        return true;
+    for (i = 0; i < self->expression.count; ++i) {
+        if (!self->ir_values) {
+            compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
+            return false;
+        }
+        if (!self->ir_values[i]) {
+            compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
+            return false;
+        }
+        if (self->ir_values[i]->life) {
+            compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
+            return false;
+        }
+    }
     if (self->setter) {
-        if (!ast_global_codegen(self->setter, func->owner, false) ||
-            !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
+        if (!ast_global_codegen  (self->setter, ir, false) ||
+            !ast_function_codegen(self->setter->constval.vfunc, ir) ||
             !ir_function_finalize(self->setter->constval.vfunc->ir_func))
+        {
+            compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
             return false;
+        }
     }
     if (self->getter) {
-        if (!ast_global_codegen(self->getter, func->owner, false) ||
-            !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
+        if (!ast_global_codegen  (self->getter, ir, false) ||
+            !ast_function_codegen(self->getter->constval.vfunc, ir) ||
             !ir_function_finalize(self->getter->constval.vfunc->ir_func))
+        {
+            compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
             return false;
+        }
     }
     return true;
-
-error: /* clean up */
-    ir_value_delete(v);
-    return false;
 }
 
 bool ast_function_codegen(ast_function *self, ir_builder *ir)
@@ -1959,6 +2001,7 @@ bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, i
     } else {
         *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
                                              ent, field, self->expression.vtype);
+        codegen_output_type(self, *out);
     }
     if (!*out) {
         compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
@@ -2408,7 +2451,7 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
     bbreak = bout;
 
     /* The loop body... */
-    if (self->body)
+    /* if (self->body) */
     {
         bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
         if (!bbody)
@@ -2425,9 +2468,11 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
             func->continueblock = bbody;
 
         /* generate */
-        cgen = self->body->expression.codegen;
-        if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
-            return false;
+        if (self->body) {
+            cgen = self->body->expression.codegen;
+            if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
+                return false;
+        }
 
         end_bbody = func->curblock;
         func->breakblock    = old_bbreak;
@@ -2840,6 +2885,8 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value
     *out = ir_call_value(callinstr);
     self->expression.outr = *out;
 
+    codegen_output_type(self, *out);
+
     vec_free(params);
     return true;
 error: