]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
allow array size to be inferred from the initializer
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 3d1f30b2203e7ab8c01635e4018c67a6bb8f1b76..94b254cc91f5a3ad3931fe81d9654e1d738f4047 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1212,6 +1212,16 @@ static bool ast_global_array_set(ast_value *self)
     size_t count = vec_size(self->initlist);
     size_t i;
 
+    if (count > self->expression.count) {
+        compile_error(ast_ctx(self), "too many elements in initializer");
+        count = self->expression.count;
+    }
+    else if (count < self->expression.count) {
+        /* add this?
+        compile_warning(ast_ctx(self), "not all elements are initialized");
+        */
+    }
+
     for (i = 0; i != count; ++i) {
         switch (self->expression.next->vtype) {
             case TYPE_FLOAT:
@@ -1363,6 +1373,11 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
         ast_expression *elemtype = self->expression.next;
         int vtype = elemtype->vtype;
 
+        if (self->expression.flags & AST_FLAG_ARRAY_INIT && !self->expression.count) {
+            compile_error(ast_ctx(self), "array `%s' has no size", self->name);
+            return false;
+        }
+
         /* same as with field arrays */
         if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);