for now just call the dtors like this
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 948e2548259b3fef31b5815edb72ec7e2abf568f..10356bead71e9afd692dc05a9121d561b3988502 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
@@ -8,6 +8,8 @@
 #include "fold.h"
 //#include "parser.h"
 
+#include "algo.h"
+
 #define ast_instantiate(T, ctx, destroyfn)                          \
     T* self = (T*)mem_a(sizeof(T));                                 \
     if (!self) {                                                    \
@@ -151,7 +153,7 @@ void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
     }
 }
 
-static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
+static ast_expression* ast_shallow_type(lex_ctx_t ctx, qc_type vtype)
 {
     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
     ast_expression_init(self, nullptr);
@@ -318,7 +320,7 @@ void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
 }
 
 static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
-ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
+ast_value* ast_value_new(lex_ctx_t ctx, const char *name, qc_type t)
 {
     ast_instantiate(ast_value, ctx, ast_value_delete);
     ast_expression_init((ast_expression*)self,
@@ -387,6 +389,7 @@ void ast_value_delete(ast_value* self)
     }
 
     ast_expression_delete((ast_expression*)self);
+    self->~ast_value();
     mem_d(self);
 }
 
@@ -464,6 +467,7 @@ void ast_binary_delete(ast_binary *self)
     if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
 
     ast_expression_delete((ast_expression*)self);
+    self->~ast_binary();
     mem_d(self);
 }
 
@@ -492,6 +496,7 @@ void ast_binstore_delete(ast_binstore *self)
         ast_unref(self->dest);
     ast_unref(self->source);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_binstore();
     mem_d(self);
 }
 
@@ -537,6 +542,7 @@ void ast_unary_delete(ast_unary *self)
 {
     if (self->operand) ast_unref(self->operand);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_unary();
     mem_d(self);
 }
 
@@ -558,6 +564,7 @@ void ast_return_delete(ast_return *self)
     if (self->operand)
         ast_unref(self->operand);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_return();
     mem_d(self);
 }
 
@@ -596,6 +603,7 @@ void ast_entfield_delete(ast_entfield *self)
     ast_unref(self->entity);
     ast_unref(self->field);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_entfield();
     mem_d(self);
 }
 
@@ -650,6 +658,7 @@ void ast_member_delete(ast_member *self)
     */
     ast_expression_delete((ast_expression*)self);
     mem_d(self->name);
+    self->~ast_member();
     mem_d(self);
 }
 
@@ -718,6 +727,7 @@ void ast_argpipe_delete(ast_argpipe *self)
     if (self->index)
         ast_unref(self->index);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_argpipe();
     mem_d(self);
 }
 
@@ -751,6 +761,7 @@ void ast_ifthen_delete(ast_ifthen *self)
     if (self->on_false)
         ast_unref(self->on_false);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_ifthen();
     mem_d(self);
 }
 
@@ -788,6 +799,7 @@ void ast_ternary_delete(ast_ternary *self)
     if (self->on_true)  ast_unref(self->on_true);
     if (self->on_false) ast_unref(self->on_false);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_ternary();
     mem_d(self);
 }
 
@@ -837,6 +849,7 @@ void ast_loop_delete(ast_loop *self)
     if (self->body)
         ast_unref(self->body);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_loop();
     mem_d(self);
 }
 
@@ -854,6 +867,7 @@ ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels
 void ast_breakcont_delete(ast_breakcont *self)
 {
     ast_expression_delete((ast_expression*)self);
+    self->~ast_breakcont();
     mem_d(self);
 }
 
@@ -880,6 +894,7 @@ void ast_switch_delete(ast_switch *self)
     }
 
     ast_expression_delete((ast_expression*)self);
+    self->~ast_switch();
     mem_d(self);
 }
 
@@ -901,6 +916,7 @@ void ast_label_delete(ast_label *self)
 {
     mem_d((void*)self->name);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_label();
     mem_d(self);
 }
 
@@ -925,6 +941,7 @@ void ast_goto_delete(ast_goto *self)
 {
     mem_d((void*)self->name);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_goto();
     mem_d(self);
 }
 
@@ -950,6 +967,7 @@ void ast_state_delete(ast_state *self)
         ast_unref(self->nextthink);
 
     ast_expression_delete((ast_expression*)self);
+    self->~ast_state();
     mem_d(self);
 }
 
@@ -986,6 +1004,7 @@ void ast_call_delete(ast_call *self)
         ast_unref(self->va_count);
 
     ast_expression_delete((ast_expression*)self);
+    self->~ast_call();
     mem_d(self);
 }
 
@@ -1111,6 +1130,7 @@ void ast_store_delete(ast_store *self)
     ast_unref(self->dest);
     ast_unref(self->source);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_store();
     mem_d(self);
 }
 
@@ -1146,6 +1166,7 @@ void ast_block_delete(ast_block *self)
     for (auto &it : self->locals) ast_delete(it);
     for (auto &it : self->collect) ast_delete(it);
     ast_expression_delete((ast_expression*)self);
+    self->~ast_block();
     mem_d(self);
 }
 
@@ -1211,8 +1232,9 @@ void ast_function_delete(ast_function *self)
     }
     for (auto &it : self->static_names)
         mem_d(it);
-    for (auto &it : self->blocks)
-        ast_delete(it);
+    // FIXME::DELME:: unique_ptr used on ast_block
+    //for (auto &it : self->blocks)
+    //    ast_delete(it);
     if (self->varargs)
         ast_delete(self->varargs);
     if (self->argc)
@@ -1221,6 +1243,7 @@ void ast_function_delete(ast_function *self)
         ast_unref(self->fixedparams);
     if (self->return_value)
         ast_unref(self->return_value);
+    self->~ast_function();
     mem_d(self);
 }
 
@@ -1407,7 +1430,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             size_t  namelen;
 
             ast_expression *elemtype;
-            int             vtype;
+            qc_type         vtype;
             ast_value      *array = (ast_value*)fieldtype;
 
             if (!ast_istype(fieldtype, ast_value)) {
@@ -1480,7 +1503,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
         size_t  namelen;
 
         ast_expression *elemtype = self->next;
-        int vtype = elemtype->vtype;
+        qc_type vtype = elemtype->vtype;
 
         if (self->flags & AST_FLAG_ARRAY_INIT && !self->count) {
             compile_error(ast_ctx(self), "array `%s' has no size", self->name);
@@ -1595,7 +1618,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
     return true;
 
 error: /* clean up */
-    if(v) ir_value_delete(v);
+    if (v) delete v;
     return false;
 }
 
@@ -1622,7 +1645,7 @@ static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         size_t  namelen;
 
         ast_expression *elemtype = self->next;
-        int vtype = elemtype->vtype;
+        qc_type vtype = elemtype->vtype;
 
         func->flags |= IR_FLAG_HAS_ARRAYS;
 
@@ -1710,7 +1733,7 @@ static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
     return true;
 
 error: /* clean up */
-    ir_value_delete(v);
+    delete v;
     return false;
 }
 
@@ -1729,7 +1752,7 @@ bool ast_generate_accessors(ast_value *self, ir_builder *ir)
             compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
             return false;
         }
-        if (self->ir_values[i]->life) {
+        if (!self->ir_values[i]->life.empty()) {
             compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
             return false;
         }
@@ -1756,9 +1779,8 @@ bool ast_generate_accessors(ast_value *self, ir_builder *ir)
             return false;
         }
     }
-    for (i = 0; i < self->count; ++i) {
-        vec_free(self->ir_values[i]->life);
-    }
+    for (i = 0; i < self->count; ++i)
+        self->ir_values[i]->life.clear();
     opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
     return true;
 }
@@ -1845,7 +1867,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
 
     for (auto &it : self->blocks) {
         cgen = it->codegen;
-        if (!(*cgen)((ast_expression*)it, self, false, &dummy))
+        if (!(*cgen)(it.get(), self, false, &dummy))
             return false;
     }
 
@@ -1867,7 +1889,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
             }
             else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
                                 "control reaches end of non-void function (`%s`) via %s",
-                                self->name, self->curblock->label))
+                                self->name, self->curblock->label.c_str()))
             {
                 return false;
             }
@@ -2069,7 +2091,7 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va
         size_t    merge_id;
 
         /* prepare end-block */
-        merge_id = vec_size(func->ir_func->blocks);
+        merge_id = func->ir_func->blocks.size();
         merge    = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
 
         /* generate the left expression */
@@ -2106,8 +2128,12 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va
         if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
             return false;
 
-        vec_remove(func->ir_func->blocks, merge_id, 1);
-        vec_push(func->ir_func->blocks, merge);
+        algo::shiftback(func->ir_func->blocks.begin() + merge_id,
+                        func->ir_func->blocks.end());
+        // FIXME::DELME::
+        //func->ir_func->blocks[merge_id].release();
+        //func->ir_func->blocks.erase(func->ir_func->blocks.begin() + merge_id);
+        //func->ir_func->blocks.emplace_back(merge);
 
         func->curblock = merge;
         phi = ir_block_create_phi(func->curblock, ast_ctx(self),
@@ -2868,7 +2894,7 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
         bpostcond = end_bpostcond = nullptr;
     }
 
-    bout_id = vec_size(func->ir_func->blocks);
+    bout_id = func->ir_func->blocks.size();
     bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
     if (!bout)
         return false;
@@ -3012,8 +3038,12 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
     }
 
     /* Move 'bout' to the end */
-    vec_remove(func->ir_func->blocks, bout_id, 1);
-    vec_push(func->ir_func->blocks, bout);
+    algo::shiftback(func->ir_func->blocks.begin() + bout_id,
+                    func->ir_func->blocks.end());
+    // FIXME::DELME::
+    //func->ir_func->blocks[bout_id].release(); // it's a vector<unique_ptr<>>
+    //func->ir_func->blocks.erase(func->ir_func->blocks.begin() + bout_id);
+    //func->ir_func->blocks.emplace_back(bout);
 
     return true;
 }
@@ -3096,7 +3126,7 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
         return false;
     }
 
-    bout_id = vec_size(func->ir_func->blocks);
+    bout_id = func->ir_func->blocks.size();
     bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
     if (!bout)
         return false;
@@ -3124,7 +3154,7 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
                 return false;
 
             bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
-            bnot_id = vec_size(func->ir_func->blocks);
+            bnot_id = func->ir_func->blocks.size();
             bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
             if (!bcase || !bnot)
                 return false;
@@ -3152,8 +3182,12 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
 
             /* enter the else and move it down */
             func->curblock = bnot;
-            vec_remove(func->ir_func->blocks, bnot_id, 1);
-            vec_push(func->ir_func->blocks, bnot);
+            algo::shiftback(func->ir_func->blocks.begin() + bnot_id,
+                            func->ir_func->blocks.end());
+            // FIXME::DELME::
+            //func->ir_func->blocks[bnot_id].release();
+            //func->ir_func->blocks.erase(func->ir_func->blocks.begin() + bnot_id);
+            //func->ir_func->blocks.emplace_back(bnot);
         } else {
             /* The default case */
             /* Remember where to fall through from: */
@@ -3210,8 +3244,12 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
     func->breakblocks.pop_back();
 
     /* Move 'bout' to the end, it's nicer */
-    vec_remove(func->ir_func->blocks, bout_id, 1);
-    vec_push(func->ir_func->blocks, bout);
+    algo::shiftback(func->ir_func->blocks.begin() + bout_id,
+                    func->ir_func->blocks.end());
+    // FIXME::DELME::
+    //func->ir_func->blocks[bout_id].release();
+    //func->ir_func->blocks.erase(func->ir_func->blocks.begin() + bout_id);
+    //func->ir_func->blocks.emplace_back(bout);
 
     return true;
 }