]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.cpp
std::vector for initlist
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 8b74467cd2101e073565941e6d1974464292e94b..6db9befa22d489980bc8b82b4d5fd1e1268c7cec 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);
@@ -978,7 +970,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 +980,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 +1043,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 +1067,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)) {
@@ -1321,7 +1310,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 +1367,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;
     }
@@ -3389,13 +3378,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;