]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Added -Wunknown-pragmas
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 69a16feec91ee57bc0cfe1176715cc980fa475b9..d997baee476621711943abd892db646800d65198 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -73,6 +73,7 @@ static void ast_expression_init(ast_expression *self,
     self->expression.outr     = NULL;
     self->expression.variadic = false;
     self->expression.params   = NULL;
+    self->expression.count    = 0;
 }
 
 static void ast_expression_delete(ast_expression *self)
@@ -108,6 +109,7 @@ ast_value* ast_value_copy(const ast_value *self)
     fromex   = &self->expression;
     selfex = &cp->expression;
     selfex->variadic = fromex->variadic;
+    selfex->count    = fromex->count;
     for (i = 0; i < vec_size(fromex->params); ++i) {
         ast_value *v = ast_value_copy(fromex->params[i]);
         if (!v) {
@@ -133,6 +135,7 @@ bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
     fromex   = &other->expression;
     selfex = &self->expression;
     selfex->variadic = fromex->variadic;
+    selfex->count    = fromex->count;
     for (i = 0; i < vec_size(fromex->params); ++i) {
         ast_value *v = ast_value_copy(fromex->params[i]);
         if (!v)
@@ -184,6 +187,7 @@ ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
             selfex->next = NULL;
 
         selfex->variadic = fromex->variadic;
+        selfex->count    = fromex->count;
         for (i = 0; i < vec_size(fromex->params); ++i) {
             ast_value *v = ast_value_copy(fromex->params[i]);
             if (!v) {
@@ -429,16 +433,10 @@ ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
 
     self->keep_dest = false;
 
-    self->expression.vtype = left->expression.vtype;
-    if (left->expression.next) {
-        self->expression.next = ast_type_copy(ctx, left);
-        if (!self->expression.next) {
-            ast_delete(self);
-            return NULL;
-        }
+    if (!ast_type_adopt(self, left)) {
+        ast_delete(self);
+        return NULL;
     }
-    else
-        self->expression.next = NULL;
 
     return self;
 }
@@ -855,11 +853,6 @@ 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;
@@ -915,16 +908,10 @@ ast_store* ast_store_new(lex_ctx ctx, int op,
     self->dest = dest;
     self->source = source;
 
-    self->expression.vtype = dest->expression.vtype;
-    if (dest->expression.next) {
-        self->expression.next = ast_type_copy(ctx, dest);
-        if (!self->expression.next) {
-            ast_delete(self);
-            return NULL;
-        }
+    if (!ast_type_adopt(self, dest)) {
+        ast_delete(self);
+        return NULL;
     }
-    else
-        self->expression.next = NULL;
 
     return self;
 }
@@ -950,10 +937,19 @@ ast_block* ast_block_new(lex_ctx ctx)
     return self;
 }
 
-void ast_block_add_expr(ast_block *self, ast_expression *e)
+bool ast_block_add_expr(ast_block *self, ast_expression *e)
 {
     ast_propagate_effects(self, e);
     vec_push(self->exprs, e);
+    if (self->expression.next) {
+        ast_delete(self->expression.next);
+        self->expression.next = NULL;
+    }
+    if (!ast_type_adopt(self, e)) {
+        compile_error(ast_ctx(self), "internal error: failed to adopt type");
+        return false;
+    }
+    return true;
 }
 
 void ast_block_collect(ast_block *self, ast_expression *expr)
@@ -982,14 +978,8 @@ bool ast_block_set_type(ast_block *self, ast_expression *from)
 {
     if (self->expression.next)
         ast_delete(self->expression.next);
-    self->expression.vtype = from->expression.vtype;
-    if (from->expression.next) {
-        self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
-        if (!self->expression.next)
-            return false;
-    }
-    else
-        self->expression.next = NULL;
+    if (!ast_type_adopt(self, from))
+        return false;
     return true;
 }
 
@@ -1054,7 +1044,7 @@ const char* ast_function_label(ast_function *self, const char *prefix)
     size_t len;
     char  *from;
 
-    if (!opts_dump && !opts_dumpfin)
+    if (!opts.dump && !opts.dumpfin)
         return NULL;
 
     id  = (self->labelcount++);
@@ -1151,7 +1141,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             }
 
             /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
-            if (!array->expression.count || array->expression.count > opts_max_array_size)
+            if (!array->expression.count || array->expression.count > opts.max_array_size)
                 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
 
             elemtype = &array->expression.next->expression;
@@ -1163,6 +1153,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 return false;
             }
             v->context = ast_ctx(self);
+            v->unique_life = true;
             array->ir_v = self->ir_v = v;
 
             namelen = strlen(self->name);
@@ -1180,6 +1171,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                     return false;
                 }
                 array->ir_values[ai]->context = ast_ctx(self);
+                array->ir_values[ai]->unique_life = true;
             }
             mem_d(name);
         }
@@ -1203,7 +1195,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
         int vtype = elemtype->vtype;
 
         /* same as with field arrays */
-        if (!self->expression.count || self->expression.count > opts_max_array_size)
+        if (!self->expression.count || self->expression.count > opts.max_array_size)
             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
 
         v = ir_builder_create_global(ir, self->name, vtype);
@@ -1212,6 +1204,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             return false;
         }
         v->context = ast_ctx(self);
+        v->unique_life = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1228,6 +1221,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 return false;
             }
             self->ir_values[ai]->context = ast_ctx(self);
+            self->ir_values[ai]->unique_life = true;
         }
         mem_d(name);
     }
@@ -1322,7 +1316,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         }
 
         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
-        if (!self->expression.count || self->expression.count > opts_max_array_size) {
+        if (!self->expression.count || self->expression.count > opts.max_array_size) {
             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
         }
 
@@ -1338,6 +1332,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
             return false;
         }
         v->context = ast_ctx(self);
+        v->unique_life = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1352,6 +1347,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
                 return false;
             }
             self->ir_values[ai]->context = ast_ctx(self);
+            self->ir_values[ai]->unique_life = true;
         }
     }
     else
@@ -1400,26 +1396,52 @@ error: /* clean up */
     return false;
 }
 
-bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir)
+bool ast_generate_accessors(ast_value *self, ir_builder *ir)
 {
-    if (asvalue->setter) {
-        if (!ast_global_codegen  (asvalue->setter, ir, false) ||
-            !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
-            !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
+    size_t i;
+    bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
+    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;
+        }
+    }
+
+    options_set(opts.warn, WARN_USED_UNINITIALIZED, false);
+    if (self->setter) {
+        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(asvalue), "internal error: failed to generate setter for `%s`", asvalue->name);
+            compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
+            options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
             return false;
         }
     }
-    if (asvalue->getter) {
-        if (!ast_global_codegen  (asvalue->getter, ir, false) ||
-            !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
-            !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
+    if (self->getter) {
+        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(asvalue), "internal error: failed to generate getter for `%s`", asvalue->name);
+            compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
+            options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
             return false;
         }
     }
+    for (i = 0; i < self->expression.count; ++i) {
+        vec_free(self->ir_values[i]->life);
+    }
+    options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
     return true;
 }
 
@@ -1533,7 +1555,7 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu
     for (i = 0; i < vec_size(self->locals); ++i)
     {
         if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
-            if (opts_debug)
+            if (opts.debug)
                 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
             return false;
         }
@@ -1984,7 +2006,9 @@ 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);
+        /* Done AFTER error checking: 
         codegen_output_type(self, *out);
+        */
     }
     if (!*out) {
         compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
@@ -1992,6 +2016,8 @@ bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, i
                  type_name[self->expression.vtype]);
         return false;
     }
+    if (!lvalue)
+        codegen_output_type(self, *out);
 
     if (lvalue)
         self->expression.outl = *out;
@@ -2320,6 +2346,8 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
     self->expression.outr = ir_phi_value(phi);
     *out = self->expression.outr;
 
+    codegen_output_type(self, *out);
+
     return true;
 }
 
@@ -2593,8 +2621,10 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
 {
     ast_expression_codegen *cgen;
 
-    ast_switch_case *def_case  = NULL;
-    ir_block        *def_bfall = NULL;
+    ast_switch_case *def_case     = NULL;
+    ir_block        *def_bfall    = NULL;
+    ir_block        *def_bfall_to = NULL;
+    bool set_def_bfall_to = false;
 
     ir_value *dummy     = NULL;
     ir_value *irop      = NULL;
@@ -2668,6 +2698,10 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
             bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
             if (!bcase || !bnot)
                 return false;
+            if (set_def_bfall_to) {
+                set_def_bfall_to = false;
+                def_bfall_to = bcase;
+            }
             if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
                 return false;
 
@@ -2697,6 +2731,8 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
             bfall     = NULL;
             /* remember which case it was */
             def_case  = swcase;
+            /* And the next case will be remembered */
+            set_def_bfall_to = true;
         }
     }
 
@@ -2725,6 +2761,13 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
         cgen = def_case->code->expression.codegen;
         if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
             return false;
+
+        /* see if we need to fall through */
+        if (def_bfall_to && !func->curblock->final)
+        {
+            if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
+                return false;
+        }
     }
 
     /* Jump from the last bnot to bout */