Use std::vector for ast switch cases
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 8b74467..efff111 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
@@ -336,7 +336,6 @@ ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
     self->inexact  = false;
     self->uses     = 0;
     memset(&self->constval, 0, sizeof(self->constval));
-    self->initlist = NULL;
 
     self->ir_v           = NULL;
     self->ir_values      = NULL;
@@ -381,18 +380,11 @@ void ast_value_delete(ast_value* self)
     if (self->desc)
         mem_d(self->desc);
 
-    if (self->initlist) {
-        if (self->expression.next->vtype == TYPE_STRING) {
-            /* strings are allocated, free them */
-            size_t i, len = vec_size(self->initlist);
-            /* in theory, len should be expression.count
-             * but let's not take any chances */
-            for (i = 0; i < len; ++i) {
-                if (self->initlist[i].vstring)
-                    mem_d(self->initlist[i].vstring);
-            }
-        }
-        vec_free(self->initlist);
+    // initlist imples an array which implies .next in the expression exists.
+    if (self->initlist.size() && self->expression.next->vtype == TYPE_STRING) {
+        for (auto &it : self->initlist)
+            if (it.vstring)
+                mem_d(it.vstring);
     }
 
     ast_expression_delete((ast_expression*)self);
@@ -872,7 +864,6 @@ ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
 
     self->operand = op;
-    self->cases   = NULL;
 
     ast_propagate_effects(self, op);
 
@@ -881,15 +872,13 @@ ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
 
 void ast_switch_delete(ast_switch *self)
 {
-    size_t i;
     ast_unref(self->operand);
 
-    for (i = 0; i < vec_size(self->cases); ++i) {
-        if (self->cases[i].value)
-            ast_unref(self->cases[i].value);
-        ast_unref(self->cases[i].code);
+    for (auto &it : self->cases) {
+        if (it.value)
+            ast_unref(it.value);
+        ast_unref(it.code);
     }
-    vec_free(self->cases);
 
     ast_expression_delete((ast_expression*)self);
     mem_d(self);
@@ -978,7 +967,6 @@ ast_call* ast_call_new(lex_ctx_t ctx,
 
     ast_side_effects(self) = true;
 
-    self->params   = NULL;
     self->func     = funcexpr;
     self->va_count = NULL;
 
@@ -989,10 +977,8 @@ ast_call* ast_call_new(lex_ctx_t ctx,
 
 void ast_call_delete(ast_call *self)
 {
-    size_t i;
-    for (i = 0; i < vec_size(self->params); ++i)
-        ast_unref(self->params[i]);
-    vec_free(self->params);
+    for (auto &it : self->params)
+        ast_unref(it);
 
     if (self->func)
         ast_unref(self->func);
@@ -1054,7 +1040,7 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
     size_t i;
     bool retval = true;
     const ast_expression *func = self->func;
-    size_t count = vec_size(self->params);
+    size_t count = self->params.size();
     if (count > func->params.size())
         count = func->params.size();
 
@@ -1078,7 +1064,7 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
             retval = false;
         }
     }
-    count = vec_size(self->params);
+    count = self->params.size();
     if (count > func->params.size() && func->varparam) {
         for (; i < count; ++i) {
             if (ast_istype(self->params[i], ast_argpipe)) {
@@ -1188,7 +1174,6 @@ ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype
 
     self->vtype  = vtype;
     self->name   = name ? util_strdup(name) : NULL;
-    self->blocks = NULL;
 
     self->labelcount = 0;
     self->builtin = 0;
@@ -1234,9 +1219,8 @@ void ast_function_delete(ast_function *self)
     for (i = 0; i < vec_size(self->static_names); ++i)
         mem_d(self->static_names[i]);
     vec_free(self->static_names);
-    for (i = 0; i < vec_size(self->blocks); ++i)
-        ast_delete(self->blocks[i]);
-    vec_free(self->blocks);
+    for (auto &it : self->blocks)
+        ast_delete(it);
     vec_free(self->breakblocks);
     vec_free(self->continueblocks);
     if (self->varargs)
@@ -1321,7 +1305,7 @@ bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_valu
 
 static bool ast_global_array_set(ast_value *self)
 {
-    size_t count = vec_size(self->initlist);
+    size_t count = self->initlist.size();
     size_t i;
 
     if (count > self->expression.count) {
@@ -1378,7 +1362,7 @@ static bool ast_global_array_set(ast_value *self)
 
 static bool check_array(ast_value *self, ast_value *array)
 {
-    if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) {
+    if (array->expression.flags & AST_FLAG_ARRAY_INIT && array->initlist.empty()) {
         compile_error(ast_ctx(self), "array without size: %s", self->name);
         return false;
     }
@@ -1796,8 +1780,6 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     ast_expression         *ec;
     ast_expression_codegen *cgen;
 
-    size_t    i;
-
     (void)ir;
 
     irf = self->ir_func;
@@ -1836,7 +1818,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
             return false;
     }
 
-    if (!vec_size(self->blocks)) {
+    if (self->blocks.empty()) {
         compile_error(ast_ctx(self), "function `%s` has no body", self->name);
         return false;
     }
@@ -1871,9 +1853,9 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
         }
     }
 
-    for (i = 0; i < vec_size(self->blocks); ++i) {
-        cgen = self->blocks[i]->expression.codegen;
-        if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
+    for (auto &it : self->blocks) {
+        cgen = it->expression.codegen;
+        if (!(*cgen)((ast_expression*)it, self, false, &dummy))
             return false;
     }
 
@@ -3093,7 +3075,6 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
     ir_block *bout      = NULL;
     ir_block *bfall     = NULL;
     size_t    bout_id;
-    size_t    c;
 
     char      typestr[1024];
     uint16_t  cmpinstr;
@@ -3116,7 +3097,7 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
     if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
         return false;
 
-    if (!vec_size(self->cases))
+    if (self->cases.empty())
         return true;
 
     cmpinstr = type_eq_instr[irop->vtype];
@@ -3135,12 +3116,12 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
     vec_push(func->breakblocks, bout);
 
     /* Now create all cases */
-    for (c = 0; c < vec_size(self->cases); ++c) {
+    for (auto &it : self->cases) {
         ir_value *cond, *val;
         ir_block *bcase, *bnot;
         size_t bnot_id;
 
-        ast_switch_case *swcase = &self->cases[c];
+        ast_switch_case *swcase = &it;
 
         if (swcase->value) {
             /* A regular case */
@@ -3389,13 +3370,10 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value
     params = NULL;
 
     /* parameters */
-    for (i = 0; i < vec_size(self->params); ++i)
-    {
+    for (auto &it : self->params) {
         ir_value *param;
-        ast_expression *expr = self->params[i];
-
-        cgen = expr->codegen;
-        if (!(*cgen)(expr, func, false, &param))
+        cgen = it->codegen;
+        if (!(*cgen)(it, func, false, &param))
             goto error;
         if (!param)
             goto error;