X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ast.cpp;h=85244f48d7caa2d4a9c23341eab95be287e851bd;hp=57a7cc217e081f90c28296e7098713cb13031e73;hb=6e68526680ac6da49163c7ee3e251bea0cdaa132;hpb=4de08db0e7b73a832d638acc03c6936395fd2095;ds=sidebyside diff --git a/ast.cpp b/ast.cpp index 57a7cc2..85244f4 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); @@ -1185,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; @@ -1193,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; @@ -1203,8 +1188,6 @@ ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype self->argc = NULL; self->fixedparams = NULL; self->return_value = NULL; - - self->static_names = NULL; self->static_count = 0; return self; @@ -1216,7 +1199,6 @@ cleanup: void ast_function_delete(ast_function *self) { - size_t i; if (self->name) mem_d((void*)self->name); if (self->vtype) { @@ -1228,14 +1210,10 @@ void ast_function_delete(ast_function *self) */ ast_unref(self->vtype); } - 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->static_names) + mem_d(it); + for (auto &it : self->blocks) + ast_delete(it); if (self->varargs) ast_delete(self->varargs); if (self->argc) @@ -1318,7 +1296,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) { @@ -1375,7 +1353,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; } @@ -1793,8 +1771,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; @@ -1833,7 +1809,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; } @@ -1868,9 +1844,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; } @@ -2910,11 +2886,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) { @@ -2924,8 +2900,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 */ @@ -3062,9 +3038,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")); @@ -3090,7 +3066,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; @@ -3113,7 +3088,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]; @@ -3129,15 +3104,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 */ @@ -3234,7 +3209,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); @@ -3360,9 +3335,8 @@ bool ast_state_codegen(ast_state *self, ast_function *func, bool lvalue, ir_valu bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out) { ast_expression_codegen *cgen; - ir_value **params; - ir_instr *callinstr; - size_t i; + std::vector params; + ir_instr *callinstr; ir_value *funval = NULL; @@ -3383,17 +3357,15 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value if (!funval) return false; - params = NULL; - /* parameters */ for (auto &it : self->params) { ir_value *param; cgen = it->codegen; if (!(*cgen)(it, func, false, ¶m)) - goto error; + return false; if (!param) - goto error; - vec_push(params, param); + return false; + params.push_back(param); } /* varargs counter */ @@ -3414,20 +3386,15 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value ast_function_label(func, "call"), funval, !!(self->func->flags & AST_FLAG_NORETURN)); if (!callinstr) - goto error; + return false; - for (i = 0; i < vec_size(params); ++i) { - ir_call_param(callinstr, params[i]); - } + for (auto &it : params) + ir_call_param(callinstr, it); *out = ir_call_value(callinstr); self->expression.outr = *out; codegen_output_type(self, *out); - vec_free(params); return true; -error: - vec_free(params); - return false; }