]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
ast_block_add_expr now lets the block fully adopt the type of the added expression
authorWolfgang (Blub) Bumiller <blub@speed.at>
Wed, 5 Dec 2012 11:57:00 +0000 (12:57 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Wed, 5 Dec 2012 11:57:00 +0000 (12:57 +0100)
ast.c
ast.h
parser.c

diff --git a/ast.c b/ast.c
index 96fdf65382853a97a1df5ad89ac953bf1a3d1612..2ba8cfa276987e7bc65c473c277aa4b02c443b58 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -950,10 +950,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)
diff --git a/ast.h b/ast.h
index 41ef31821b8d81fe3878d342c3b02a9ce323be1b..25243c069c0f4ba3d6d65464fae87782581b4385 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -573,7 +573,7 @@ bool ast_block_set_type(ast_block*, ast_expression *from);
 bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
 void ast_block_collect(ast_block*, ast_expression*);
 
-void ast_block_add_expr(ast_block*, ast_expression*);
+bool GMQCC_WARN ast_block_add_expr(ast_block*, ast_expression*);
 
 /* Function
  *
index 769cced2b2f13783029b313a97055a0bad2f662b..909fd435ba3241b4f2c44b298cf579c0984e31a9 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -603,11 +603,15 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
 
         case opid1(','):
             if (blocks[0]) {
-                ast_block_add_expr(blocks[0], exprs[1]);
+                if (!ast_block_add_expr(blocks[0], exprs[1]))
+                    return false;
             } else {
                 blocks[0] = ast_block_new(ctx);
-                ast_block_add_expr(blocks[0], exprs[0]);
-                ast_block_add_expr(blocks[0], exprs[1]);
+                if (!ast_block_add_expr(blocks[0], exprs[0]) ||
+                    !ast_block_add_expr(blocks[0], exprs[1]))
+                {
+                    return false;
+                }
             }
             if (!ast_block_set_type(blocks[0], exprs[1]))
                 return false;
@@ -2368,7 +2372,10 @@ static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **ou
             }
             if (!expr)
                 continue;
-            ast_block_add_expr(caseblock, expr);
+            if (!ast_block_add_expr(caseblock, expr)) {
+                ast_delete(switchnode);
+                return false;
+            }
         }
     }
 
@@ -2704,7 +2711,11 @@ static bool parse_block_into(parser_t *parser, ast_block *block)
         }
         if (!expr)
             continue;
-        ast_block_add_expr(block, expr);
+        if (!ast_block_add_expr(block, expr)) {
+            ast_delete(block);
+            block = NULL;
+            goto cleanup;
+        }
     }
 
     if (parser->tok != '}') {
@@ -2969,9 +2980,12 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
                 if (store_think)     ast_delete(store_think);
                 retval = false;
             }
-            ast_block_add_expr(block, (ast_expression*)store_frame);
-            ast_block_add_expr(block, (ast_expression*)store_nextthink);
-            ast_block_add_expr(block, (ast_expression*)store_think);
+            if (!ast_block_add_expr(block, (ast_expression*)store_frame) ||
+                !ast_block_add_expr(block, (ast_expression*)store_nextthink) ||
+                !ast_block_add_expr(block, (ast_expression*)store_think))
+            {
+                retval = false;
+            }
         }
 
         if (!retval) {
@@ -3120,7 +3134,10 @@ static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast
             return NULL;
         }
 
-        ast_block_add_expr(block, (ast_expression*)st);
+        if (!ast_block_add_expr(block, (ast_expression*)st)) {
+            ast_delete(block);
+            return NULL;
+        }
 
         ret = ast_return_new(ctx, NULL);
         if (!ret) {
@@ -3128,7 +3145,10 @@ static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast
             return NULL;
         }
 
-        ast_block_add_expr(block, (ast_expression*)ret);
+        if (!ast_block_add_expr(block, (ast_expression*)ret)) {
+            ast_delete(block);
+            return NULL;
+        }
 
         return (ast_expression*)block;
     } else {
@@ -3189,7 +3209,10 @@ static ast_expression *array_field_setter_node(
             return NULL;
         }
 
-        ast_block_add_expr(block, (ast_expression*)st);
+        if (!ast_block_add_expr(block, (ast_expression*)st)) {
+            ast_delete(block);
+            return NULL;
+        }
 
         ret = ast_return_new(ctx, NULL);
         if (!ret) {
@@ -3197,7 +3220,10 @@ static ast_expression *array_field_setter_node(
             return NULL;
         }
 
-        ast_block_add_expr(block, (ast_expression*)ret);
+        if (!ast_block_add_expr(block, (ast_expression*)ret)) {
+            ast_delete(block);
+            return NULL;
+        }
 
         return (ast_expression*)block;
     } else {
@@ -3309,9 +3335,8 @@ static bool parser_create_array_setter(parser_t *parser, ast_value *array, const
         goto cleanup;
     }
 
-    ast_block_add_expr(func->blocks[0], root);
     array->setter = fval;
-    return true;
+    return ast_block_add_expr(func->blocks[0], root);
 cleanup:
     if (index) ast_delete(index);
     if (value) ast_delete(value);
@@ -3358,9 +3383,8 @@ static bool parser_create_array_field_setter(parser_t *parser, ast_value *array,
         goto cleanup;
     }
 
-    ast_block_add_expr(func->blocks[0], root);
     array->setter = fval;
-    return true;
+    return ast_block_add_expr(func->blocks[0], root);
 cleanup:
     if (entity) ast_delete(entity);
     if (index)  ast_delete(index);
@@ -3405,9 +3429,8 @@ static bool parser_create_array_getter(parser_t *parser, ast_value *array, const
         goto cleanup;
     }
 
-    ast_block_add_expr(func->blocks[0], root);
     array->getter = fval;
-    return true;
+    return ast_block_add_expr(func->blocks[0], root);
 cleanup:
     if (index) ast_delete(index);
     if (root)  ast_delete(root);
@@ -4232,7 +4255,8 @@ skipvar:
                 else {
                     if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
                         parseerror(parser, "internal error: leaked operands");
-                    ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
+                    if (!ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out))
+                        break;
                 }
                 vec_free(sy.out);
                 vec_free(sy.ops);