]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.cpp
Use std::vector for break and continue ast blocks
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 29e298ab817a2b551ca5fc7abee7063d7d2c72bd..55e444054ea445513b5901768edb056f1f55b5d4 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
@@ -1,3 +1,5 @@
+#include <new>
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -10,6 +12,7 @@
     if (!self) {                                                    \
         return NULL;                                                \
     }                                                               \
+    new (self) T();                                                 \
     ast_node_init((ast_node*)self, ctx, TYPE_##T);                  \
     ( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn
 
@@ -89,7 +92,6 @@ static void ast_expression_init(ast_expression *self,
     self->next     = NULL;
     self->outl     = NULL;
     self->outr     = NULL;
-    self->params   = NULL;
     self->count    = 0;
     self->varparam = NULL;
     self->flags    = 0;
@@ -99,13 +101,10 @@ static void ast_expression_init(ast_expression *self,
 
 static void ast_expression_delete(ast_expression *self)
 {
-    size_t i;
     if (self->next)
         ast_delete(self->next);
-    for (i = 0; i < vec_size(self->params); ++i) {
-        ast_delete(self->params[i]);
-    }
-    vec_free(self->params);
+    for (auto &it : self->params)
+        ast_delete(it);
     if (self->varparam)
         ast_delete(self->varparam);
 }
@@ -118,40 +117,38 @@ static void ast_expression_delete_full(ast_expression *self)
 
 ast_value* ast_value_copy(const ast_value *self)
 {
-    size_t i;
     const ast_expression *fromex;
-    ast_expression       *selfex;
+    ast_expression *selfex;
     ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
     if (self->expression.next) {
         cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
     }
-    fromex   = &self->expression;
+    fromex = &self->expression;
     selfex = &cp->expression;
-    selfex->count    = fromex->count;
-    selfex->flags    = fromex->flags;
-    for (i = 0; i < vec_size(fromex->params); ++i) {
-        ast_value *v = ast_value_copy(fromex->params[i]);
-        vec_push(selfex->params, v);
+    selfex->count = fromex->count;
+    selfex->flags = fromex->flags;
+    for (auto &it : fromex->params) {
+        ast_value *v = ast_value_copy(it);
+        selfex->params.push_back(v);
     }
     return cp;
 }
 
 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
 {
-    size_t i;
     const ast_expression *fromex;
-    ast_expression       *selfex;
+    ast_expression *selfex;
     self->vtype = other->vtype;
     if (other->next) {
         self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
     }
     fromex = other;
     selfex = self;
-    selfex->count    = fromex->count;
-    selfex->flags    = fromex->flags;
-    for (i = 0; i < vec_size(fromex->params); ++i) {
-        ast_value *v = ast_value_copy(fromex->params[i]);
-        vec_push(selfex->params, v);
+    selfex->count = fromex->count;
+    selfex->flags = fromex->flags;
+    for (auto &it : fromex->params) {
+        ast_value *v = ast_value_copy(it);
+        selfex->params.push_back(v);
     }
 }
 
@@ -167,7 +164,6 @@ static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
 
 ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
 {
-    size_t i;
     const ast_expression *fromex;
     ast_expression       *selfex;
 
@@ -190,11 +186,11 @@ ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
         else
             selfex->next = NULL;
 
-        selfex->count    = fromex->count;
-        selfex->flags    = fromex->flags;
-        for (i = 0; i < vec_size(fromex->params); ++i) {
-            ast_value *v = ast_value_copy(fromex->params[i]);
-            vec_push(selfex->params, v);
+        selfex->count = fromex->count;
+        selfex->flags = fromex->flags;
+        for (auto &it : fromex->params) {
+            ast_value *v = ast_value_copy(it);
+            selfex->params.push_back(v);
         }
 
         return self;
@@ -210,16 +206,16 @@ bool ast_compare_type(ast_expression *a, ast_expression *b)
         return false;
     if (!a->next != !b->next)
         return false;
-    if (vec_size(a->params) != vec_size(b->params))
+    if (a->params.size() != b->params.size())
         return false;
     if ((a->flags & AST_FLAG_TYPE_MASK) !=
         (b->flags & AST_FLAG_TYPE_MASK) )
     {
         return false;
     }
-    if (vec_size(a->params)) {
+    if (a->params.size()) {
         size_t i;
-        for (i = 0; i < vec_size(a->params); ++i) {
+        for (i = 0; i < a->params.size(); ++i) {
             if (!ast_compare_type((ast_expression*)a->params[i],
                                   (ast_expression*)b->params[i]))
                 return false;
@@ -270,14 +266,14 @@ static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsi
             pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
             if (pos + 2 >= bufsize)
                 goto full;
-            if (!vec_size(e->params)) {
+            if (e->params.empty()) {
                 buf[pos++] = '(';
                 buf[pos++] = ')';
                 return pos;
             }
             buf[pos++] = '(';
             pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
-            for (i = 1; i < vec_size(e->params); ++i) {
+            for (i = 1; i < e->params.size(); ++i) {
                 if (pos + 2 >= bufsize)
                     goto full;
                 buf[pos++] = ',';
@@ -340,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;
@@ -385,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);
@@ -405,7 +393,7 @@ void ast_value_delete(ast_value* self)
 
 void ast_value_params_add(ast_value *self, ast_value *p)
 {
-    vec_push(self->expression.params, p);
+    self->expression.params.push_back(p);
 }
 
 bool ast_value_set_name(ast_value *self, const char *name)
@@ -876,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);
 
@@ -885,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);
@@ -908,7 +893,6 @@ ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
 
     self->name      = util_strdup(name);
     self->irblock   = NULL;
-    self->gotos     = NULL;
     self->undefined = undefined;
 
     return self;
@@ -917,14 +901,13 @@ ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
 void ast_label_delete(ast_label *self)
 {
     mem_d((void*)self->name);
-    vec_free(self->gotos);
     ast_expression_delete((ast_expression*)self);
     mem_d(self);
 }
 
 static void ast_label_register_goto(ast_label *self, ast_goto *g)
 {
-    vec_push(self->gotos, g);
+   self->gotos.push_back(g);
 }
 
 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
@@ -984,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;
 
@@ -995,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);
@@ -1058,11 +1038,11 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
     char texp[1024];
     char tgot[1024];
     size_t i;
-    bool   retval = true;
-    const  ast_expression *func = self->func;
-    size_t count = vec_size(self->params);
-    if (count > vec_size(func->params))
-        count = vec_size(func->params);
+    bool retval = true;
+    const ast_expression *func = self->func;
+    size_t count = self->params.size();
+    if (count > func->params.size())
+        count = func->params.size();
 
     for (i = 0; i < count; ++i) {
         if (ast_istype(self->params[i], ast_argpipe)) {
@@ -1084,8 +1064,8 @@ bool ast_call_check_types(ast_call *self, ast_expression *va_type)
             retval = false;
         }
     }
-    count = vec_size(self->params);
-    if (count > vec_size(func->params) && func->varparam) {
+    count = self->params.size();
+    if (count > func->params.size() && func->varparam) {
         for (; i < count; ++i) {
             if (ast_istype(self->params[i], ast_argpipe)) {
                 /* warn about type safety instead */
@@ -1140,18 +1120,13 @@ ast_block* ast_block_new(lex_ctx_t ctx)
     ast_instantiate(ast_block, ctx, ast_block_delete);
     ast_expression_init((ast_expression*)self,
                         (ast_expression_codegen*)&ast_block_codegen);
-
-    self->locals  = NULL;
-    self->exprs   = NULL;
-    self->collect = NULL;
-
     return self;
 }
 
 bool ast_block_add_expr(ast_block *self, ast_expression *e)
 {
     ast_propagate_effects(self, e);
-    vec_push(self->exprs, e);
+    self->exprs.push_back(e);
     if (self->expression.next) {
         ast_delete(self->expression.next);
         self->expression.next = NULL;
@@ -1162,22 +1137,15 @@ bool ast_block_add_expr(ast_block *self, ast_expression *e)
 
 void ast_block_collect(ast_block *self, ast_expression *expr)
 {
-    vec_push(self->collect, expr);
+    self->collect.push_back(expr);
     expr->node.keep = true;
 }
 
 void ast_block_delete(ast_block *self)
 {
-    size_t i;
-    for (i = 0; i < vec_size(self->exprs); ++i)
-        ast_unref(self->exprs[i]);
-    vec_free(self->exprs);
-    for (i = 0; i < vec_size(self->locals); ++i)
-        ast_delete(self->locals[i]);
-    vec_free(self->locals);
-    for (i = 0; i < vec_size(self->collect); ++i)
-        ast_delete(self->collect[i]);
-    vec_free(self->collect);
+    for (auto &it : self->exprs) ast_unref(it);
+    for (auto &it : self->locals) ast_delete(it);
+    for (auto &it : self->collect) ast_delete(it);
     ast_expression_delete((ast_expression*)self);
     mem_d(self);
 }
@@ -1206,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;
@@ -1214,9 +1181,6 @@ ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype
     self->ir_func = NULL;
     self->curblock = NULL;
 
-    self->breakblocks    = NULL;
-    self->continueblocks = NULL;
-
     vtype->hasvalue = true;
     vtype->constval.vfunc = self;
 
@@ -1252,11 +1216,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);
-    vec_free(self->breakblocks);
-    vec_free(self->continueblocks);
+    for (auto &it : self->blocks)
+        ast_delete(it);
     if (self->varargs)
         ast_delete(self->varargs);
     if (self->argc)
@@ -1339,7 +1300,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) {
@@ -1396,7 +1357,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;
     }
@@ -1814,8 +1775,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;
@@ -1826,14 +1785,13 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
 
     /* fill the parameter list */
     ec = &self->vtype->expression;
-    for (i = 0; i < vec_size(ec->params); ++i)
-    {
-        if (ec->params[i]->expression.vtype == TYPE_FIELD)
-            vec_push(irf->params, ec->params[i]->expression.next->vtype);
+    for (auto &it : ec->params) {
+        if (it->expression.vtype == TYPE_FIELD)
+            vec_push(irf->params, it->expression.next->vtype);
         else
-            vec_push(irf->params, ec->params[i]->expression.vtype);
+            vec_push(irf->params, it->expression.vtype);
         if (!self->builtin) {
-            if (!ast_local_codegen(ec->params[i], self->ir_func, true))
+            if (!ast_local_codegen(it, self->ir_func, true))
                 return false;
         }
     }
@@ -1855,7 +1813,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;
     }
@@ -1890,9 +1848,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;
     }
 
@@ -1943,8 +1901,6 @@ static bool starts_a_label(ast_expression *ex)
  */
 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
 {
-    size_t i;
-
     /* We don't use this
      * Note: an ast-representation using the comma-operator
      * of the form: (a, b, c) = x should not assign to c...
@@ -1968,25 +1924,23 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu
     *out = NULL;
 
     /* generate locals */
-    for (i = 0; i < vec_size(self->locals); ++i)
-    {
-        if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
+    for (auto &it : self->locals) {
+        if (!ast_local_codegen(it, func->ir_func, false)) {
             if (OPTS_OPTION_BOOL(OPTION_DEBUG))
-                compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
+                compile_error(ast_ctx(self), "failed to generate local `%s`", it->name);
             return false;
         }
     }
 
-    for (i = 0; i < vec_size(self->exprs); ++i)
-    {
+    for (auto &it : self->exprs) {
         ast_expression_codegen *gen;
-        if (func->curblock->final && !starts_a_label(self->exprs[i])) {
-            if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
+        if (func->curblock->final && !starts_a_label(it)) {
+            if (compile_warning(ast_ctx(it), WARN_UNREACHABLE_CODE, "unreachable statement"))
                 return false;
             continue;
         }
-        gen = self->exprs[i]->codegen;
-        if (!(*gen)(self->exprs[i], func, false, out))
+        gen = it->codegen;
+        if (!(*gen)(it, func, false, out))
             return false;
     }
 
@@ -2936,11 +2890,11 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
         /* enter */
         func->curblock = bbody;
 
-        vec_push(func->breakblocks,    bbreak);
+        func->breakblocks.push_back(bbreak);
         if (bcontinue)
-            vec_push(func->continueblocks, bcontinue);
+            func->continueblocks.push_back(bcontinue);
         else
-            vec_push(func->continueblocks, bbody);
+            func->continueblocks.push_back(bbody);
 
         /* generate */
         if (self->body) {
@@ -2950,8 +2904,8 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
         }
 
         end_bbody = func->curblock;
-        vec_pop(func->breakblocks);
-        vec_pop(func->continueblocks);
+        func->breakblocks.pop_back();
+        func->continueblocks.pop_back();
     }
 
     /* post-loop-condition */
@@ -3088,9 +3042,9 @@ bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue,
     self->expression.outr = (ir_value*)1;
 
     if (self->is_continue)
-        target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
+        target = func->continueblocks[func->continueblocks.size()-1-self->levels];
     else
-        target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
+        target = func->breakblocks[func->breakblocks.size()-1-self->levels];
 
     if (!target) {
         compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
@@ -3116,7 +3070,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;
@@ -3139,7 +3092,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];
@@ -3155,15 +3108,15 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
         return false;
 
     /* setup the break block */
-    vec_push(func->breakblocks, bout);
+    func->breakblocks.push_back(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 */
@@ -3260,7 +3213,7 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
     func->curblock = bout;
 
     /* restore the break block */
-    vec_pop(func->breakblocks);
+    func->breakblocks.pop_back();
 
     /* Move 'bout' to the end, it's nicer */
     vec_remove(func->ir_func->blocks, bout_id, 1);
@@ -3271,7 +3224,6 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
 
 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
 {
-    size_t i;
     ir_value *dummy;
 
     if (self->undefined) {
@@ -3300,8 +3252,8 @@ bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_valu
     func->curblock = self->irblock;
 
     /* Generate all the leftover gotos */
-    for (i = 0; i < vec_size(self->gotos); ++i) {
-        if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
+    for (auto &it : self->gotos) {
+        if (!ast_goto_codegen(it, func, false, &dummy))
             return false;
     }
 
@@ -3413,13 +3365,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;