X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ast.cpp;h=55e444054ea445513b5901768edb056f1f55b5d4;hp=8b74467cd2101e073565941e6d1974464292e94b;hb=f38e6b48db4e10427e53d87f03a2a8be57c06c12;hpb=67a3c9b0310b915baace7a81cce1d8faa3994f20 diff --git a/ast.cpp b/ast.cpp index 8b74467..55e4440 100644 --- 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; @@ -1196,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; @@ -1234,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) @@ -1321,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) { @@ -1378,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; } @@ -1796,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; @@ -1836,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; } @@ -1871,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; } @@ -2913,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) { @@ -2927,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 */ @@ -3065,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")); @@ -3093,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; @@ -3116,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]; @@ -3132,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 = ⁢ if (swcase->value) { /* A regular case */ @@ -3237,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); @@ -3389,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, ¶m)) + cgen = it->codegen; + if (!(*cgen)(it, func, false, ¶m)) goto error; if (!param) goto error;