X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ast.cpp;h=4768e6e5bb9db83be59b9415cb73733d66d829b9;hp=29e298ab817a2b551ca5fc7abee7063d7d2c72bd;hb=db9c37d18bcced1c3b0f1421044155b1d88f0b25;hpb=65362d93aa4678209bfeeba92fb5aa41f5955777 diff --git a/ast.cpp b/ast.cpp index 29e298a..4768e6e 100644 --- a/ast.cpp +++ b/ast.cpp @@ -1,15 +1,19 @@ +#include + #include #include #include "gmqcc.h" #include "ast.h" -#include "parser.h" +#include "fold.h" +//#include "parser.h" #define ast_instantiate(T, ctx, destroyfn) \ T* self = (T*)mem_a(sizeof(T)); \ if (!self) { \ - return NULL; \ + return nullptr; \ } \ + new (self) T(); \ ast_node_init((ast_node*)self, ctx, TYPE_##T); \ ( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn @@ -86,12 +90,11 @@ static void ast_expression_init(ast_expression *self, { self->codegen = codegen; self->vtype = TYPE_VOID; - self->next = NULL; - self->outl = NULL; - self->outr = NULL; - self->params = NULL; + self->next = nullptr; + self->outl = nullptr; + self->outr = nullptr; self->count = 0; - self->varparam = NULL; + self->varparam = nullptr; self->flags = 0; if (OPTS_OPTION_BOOL(OPTION_COVERAGE)) self->flags |= AST_FLAG_BLOCK_COVERAGE; @@ -99,13 +102,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,83 +118,80 @@ 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); } } static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype) { ast_instantiate(ast_expression, ctx, ast_expression_delete_full); - ast_expression_init(self, NULL); - self->codegen = NULL; - self->next = NULL; + ast_expression_init(self, nullptr); + self->codegen = nullptr; + self->next = nullptr; self->vtype = vtype; return self; } ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex) { - size_t i; const ast_expression *fromex; ast_expression *selfex; if (!ex) - return NULL; + return nullptr; else { ast_instantiate(ast_expression, ctx, ast_expression_delete_full); - ast_expression_init(self, NULL); + ast_expression_init(self, nullptr); fromex = ex; selfex = self; /* This may never be codegen()d */ - selfex->codegen = NULL; + selfex->codegen = nullptr; selfex->vtype = fromex->vtype; if (fromex->next) selfex->next = ast_type_copy(ctx, fromex->next); else - selfex->next = NULL; + selfex->next = nullptr; - 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 +207,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 +267,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++] = ','; @@ -330,9 +327,9 @@ ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t) (ast_expression_codegen*)&ast_value_codegen); self->expression.node.keep = true; /* keep */ - self->name = name ? util_strdup(name) : NULL; + self->name = name ? util_strdup(name) : nullptr; self->expression.vtype = t; - self->expression.next = NULL; + self->expression.next = nullptr; self->isfield = false; self->cvq = CV_NONE; self->hasvalue = false; @@ -340,17 +337,16 @@ 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; + self->ir_v = nullptr; + self->ir_values = nullptr; self->ir_value_count = 0; - self->setter = NULL; - self->getter = NULL; - self->desc = NULL; + self->setter = nullptr; + self->getter = nullptr; + self->desc = nullptr; - self->argcounter = NULL; + self->argcounter = nullptr; self->intrinsic = false; return self; @@ -370,7 +366,7 @@ void ast_value_delete(ast_value* self) break; case TYPE_FUNCTION: /* unlink us from the function node */ - self->constval.vfunc->vtype = NULL; + self->constval.vfunc->vtype = nullptr; break; /* NOTE: delete function? currently collected in * the parser structure @@ -385,18 +381,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 +394,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) @@ -578,7 +567,7 @@ ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expres { if (field->vtype != TYPE_FIELD) { compile_error(ctx, "ast_entfield_new with expression not of type field"); - return NULL; + return nullptr; } return ast_entfield_new_force(ctx, entity, field, field->next); } @@ -590,7 +579,7 @@ ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_ if (!outtype) { mem_d(self); /* Error: field has no type... */ - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen); @@ -617,14 +606,14 @@ ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int fi ast_instantiate(ast_member, ctx, ast_member_delete); if (field >= 3) { mem_d(self); - return NULL; + return nullptr; } if (owner->vtype != TYPE_VECTOR && owner->vtype != TYPE_FIELD) { compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->vtype]); mem_d(self); - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen); @@ -632,7 +621,7 @@ ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int fi if (owner->vtype == TYPE_VECTOR) { self->expression.vtype = TYPE_FLOAT; - self->expression.next = NULL; + self->expression.next = nullptr; } else { self->expression.vtype = TYPE_FIELD; self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT); @@ -646,7 +635,7 @@ ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int fi if (name) self->name = util_strdup(name); else - self->name = NULL; + self->name = nullptr; return self; } @@ -683,7 +672,7 @@ ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_e if (!outtype) { mem_d(self); /* Error: field has no type... */ - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen); @@ -698,7 +687,7 @@ ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_e if (self->expression.vtype != TYPE_ARRAY) { compile_error(ast_ctx(self), "array_index node on type"); ast_array_index_delete(self); - return NULL; + return nullptr; } self->array = outtype; self->expression.vtype = TYPE_FIELD; @@ -740,7 +729,7 @@ ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression * if (!ontrue && !onfalse) { /* because it is invalid */ mem_d(self); - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen); @@ -771,10 +760,10 @@ ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression { ast_expression *exprtype = ontrue; ast_instantiate(ast_ternary, ctx, ast_ternary_delete); - /* This time NEITHER must be NULL */ + /* This time NEITHER must be nullptr */ if (!ontrue || !onfalse) { mem_d(self); - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen); @@ -795,7 +784,7 @@ ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression void ast_ternary_delete(ast_ternary *self) { /* the if()s are only there because computed-gotos can set them - * to NULL + * to nullptr */ if (self->cond) ast_unref(self->cond); if (self->on_true) ast_unref(self->on_true); @@ -876,7 +865,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 +873,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); @@ -907,8 +893,7 @@ ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined) self->expression.vtype = TYPE_NOEXPR; self->name = util_strdup(name); - self->irblock = NULL; - self->gotos = NULL; + self->irblock = nullptr; self->undefined = undefined; return self; @@ -917,14 +902,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) @@ -933,8 +917,8 @@ ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name) ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen); self->name = util_strdup(name); - self->target = NULL; - self->irblock_from = NULL; + self->target = nullptr; + self->irblock_from = nullptr; return self; } @@ -978,15 +962,14 @@ ast_call* ast_call_new(lex_ctx_t ctx, if (!funcexpr->next) { compile_error(ctx, "not a function"); mem_d(self); - return NULL; + return nullptr; } ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen); ast_side_effects(self) = true; - self->params = NULL; self->func = funcexpr; - self->va_count = NULL; + self->va_count = nullptr; ast_type_adopt(self, funcexpr->next); @@ -995,10 +978,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 +1039,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 +1065,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,21 +1121,16 @@ 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; + self->expression.next = nullptr; } ast_type_adopt(self, e); return true; @@ -1162,22 +1138,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); } @@ -1205,58 +1174,47 @@ 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->name = name ? util_strdup(name) : nullptr; self->labelcount = 0; self->builtin = 0; - self->ir_func = NULL; - self->curblock = NULL; - - self->breakblocks = NULL; - self->continueblocks = NULL; + self->ir_func = nullptr; + self->curblock = nullptr; vtype->hasvalue = true; vtype->constval.vfunc = self; - self->varargs = NULL; - self->argc = NULL; - self->fixedparams = NULL; - self->return_value = NULL; - - self->static_names = NULL; + self->varargs = nullptr; + self->argc = nullptr; + self->fixedparams = nullptr; + self->return_value = nullptr; self->static_count = 0; return self; cleanup: mem_d(self); - return NULL; + return nullptr; } void ast_function_delete(ast_function *self) { - size_t i; if (self->name) mem_d((void*)self->name); if (self->vtype) { /* ast_value_delete(self->vtype); */ self->vtype->hasvalue = false; - self->vtype->constval.vfunc = NULL; + self->vtype->constval.vfunc = nullptr; /* We use unref - if it was stored in a global table it is supposed * to be deleted from *there* */ 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) @@ -1278,7 +1236,7 @@ const char* ast_function_label(ast_function *self, const char *prefix) !OPTS_OPTION_BOOL(OPTION_DUMPFIN) && !OPTS_OPTION_BOOL(OPTION_DEBUG)) { - return NULL; + return nullptr; } id = (self->labelcount++); @@ -1297,7 +1255,7 @@ const char* ast_function_label(ast_function *self, const char *prefix) /*********************************************************************/ /* AST codegen part - * by convention you must never pass NULL to the 'ir_value **out' + * by convention you must never pass nullptr to the 'ir_value **out' * parameter. If you really don't care about the output, pass a dummy. * But I can't imagine a pituation where the output is truly unnecessary. */ @@ -1339,7 +1297,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 +1354,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; } @@ -1410,7 +1368,7 @@ static bool check_array(ast_value *self, ast_value *array) bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) { - ir_value *v = NULL; + ir_value *v = nullptr; if (self->expression.vtype == TYPE_NIL) { compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL"); @@ -1645,7 +1603,7 @@ error: /* clean up */ static bool ast_local_codegen(ast_value *self, ir_function *func, bool param) { - ir_value *v = NULL; + ir_value *v = nullptr; if (self->expression.vtype == TYPE_NIL) { compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL"); @@ -1814,8 +1772,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 +1782,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 +1810,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 +1845,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; } @@ -1902,7 +1857,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir) if (!self->vtype->expression.next || self->vtype->expression.next->vtype == TYPE_VOID) { - return ir_block_create_return(self->curblock, ast_ctx(self), NULL); + return ir_block_create_return(self->curblock, ast_ctx(self), nullptr); } else if (vec_size(self->curblock->entries) || self->curblock == irf->first) { @@ -1918,7 +1873,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir) { return false; } - return ir_block_create_return(self->curblock, ast_ctx(self), NULL); + return ir_block_create_return(self->curblock, ast_ctx(self), nullptr); } } return true; @@ -1943,8 +1898,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... @@ -1959,34 +1912,32 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu return true; } - /* output is NULL at first, we'll have each expression + /* output is nullptr at first, we'll have each expression * assign to out output, thus, a comma-operator represention * using an ast_block will return the last generated value, * so: (b, c) + a executed both b and c, and returns c, * which is then added to a. */ - *out = NULL; + *out = nullptr; /* 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; } @@ -1998,12 +1949,12 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out) { ast_expression_codegen *cgen; - ir_value *left = NULL; - ir_value *right = NULL; + ir_value *left = nullptr; + ir_value *right = nullptr; ast_value *arr; ast_value *idx = 0; - ast_array_index *ai = NULL; + ast_array_index *ai = nullptr; if (lvalue && self->expression.outl) { *out = self->expression.outl; @@ -2022,7 +1973,7 @@ bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_valu idx = (ast_value*)ai->index; if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST) - ai = NULL; + ai = nullptr; } if (ai) { @@ -2239,12 +2190,12 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out) { ast_expression_codegen *cgen; - ir_value *leftl = NULL, *leftr, *right, *bin; + ir_value *leftl = nullptr, *leftr, *right, *bin; ast_value *arr; ast_value *idx = 0; - ast_array_index *ai = NULL; - ir_value *iridx = NULL; + ast_array_index *ai = nullptr; + ir_value *iridx = nullptr; if (lvalue && self->expression.outl) { *out = self->expression.outl; @@ -2263,7 +2214,7 @@ bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, i idx = (ast_value*)ai->index; if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST) - ai = NULL; + ai = nullptr; } /* for a binstore we need both an lvalue and an rvalue for the left side */ @@ -2374,7 +2325,7 @@ bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_va ast_expression_codegen *cgen; ir_value *operand; - *out = NULL; + *out = nullptr; /* In the context of a return operation, we don't actually return * anything... @@ -2399,7 +2350,7 @@ bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_va if (!ir_block_create_return(func->curblock, ast_ctx(self), operand)) return false; } else { - if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL)) + if (!ir_block_create_return(func->curblock, ast_ctx(self), nullptr)) return false; } @@ -2491,7 +2442,7 @@ bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_va *out = ir_value_vector_member(vec, self->field); self->expression.outl = *out; - return (*out != NULL); + return (*out != nullptr); } bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out) @@ -2586,7 +2537,7 @@ bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lva bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out) { - *out = NULL; + *out = nullptr; if (lvalue) { compile_error(ast_ctx(self), "argpipe node: not an lvalue"); return false; @@ -2607,10 +2558,10 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va ir_block *cond; ir_block *ontrue; ir_block *onfalse; - ir_block *ontrue_endblock = NULL; - ir_block *onfalse_endblock = NULL; - ir_block *merge = NULL; - int fold = 0; + ir_block *ontrue_endblock = nullptr; + ir_block *onfalse_endblock = nullptr; + ir_block *merge = nullptr; + int folded = 0; /* We don't output any value, thus also don't care about r/lvalue */ (void)out; @@ -2630,8 +2581,8 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va cond = func->curblock; /* try constant folding away the condition */ - if ((fold = fold_cond_ifthen(condval, func, self)) != -1) - return fold; + if ((folded = fold::cond_ifthen(condval, func, self)) != -1) + return folded; if (self->on_true) { /* create on-true block */ @@ -2650,7 +2601,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va /* we now need to work from the current endpoint */ ontrue_endblock = func->curblock; } else - ontrue = NULL; + ontrue = nullptr; /* on-false path */ if (self->on_false) { @@ -2670,7 +2621,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va /* we now need to work from the current endpoint */ onfalse_endblock = func->curblock; } else - onfalse = NULL; + onfalse = nullptr; /* Merge block were they all merge in to */ if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final) @@ -2709,11 +2660,11 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_ ir_instr *phi; ir_block *cond = func->curblock; - ir_block *cond_out = NULL; - ir_block *ontrue, *ontrue_out = NULL; - ir_block *onfalse, *onfalse_out = NULL; + ir_block *cond_out = nullptr; + ir_block *ontrue, *ontrue_out = nullptr; + ir_block *onfalse, *onfalse_out = nullptr; ir_block *merge; - int fold = 0; + int folded = 0; /* Ternary can never create an lvalue... */ if (lvalue) @@ -2739,8 +2690,8 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_ cond_out = func->curblock; /* try constant folding away the condition */ - if ((fold = fold_cond_ternary(condval, func, self)) != -1) - return fold; + if ((folded = fold::cond_ternary(condval, func, self)) != -1) + return folded; /* create on-true block */ ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T")); @@ -2823,28 +2774,28 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value { ast_expression_codegen *cgen; - ir_value *dummy = NULL; - ir_value *precond = NULL; - ir_value *postcond = NULL; + ir_value *dummy = nullptr; + ir_value *precond = nullptr; + ir_value *postcond = nullptr; /* Since we insert some jumps "late" so we have blocks * ordered "nicely", we need to keep track of the actual end-blocks * of expressions to add the jumps to. */ - ir_block *bbody = NULL, *end_bbody = NULL; - ir_block *bprecond = NULL, *end_bprecond = NULL; - ir_block *bpostcond = NULL, *end_bpostcond = NULL; - ir_block *bincrement = NULL, *end_bincrement = NULL; - ir_block *bout = NULL, *bin = NULL; + ir_block *bbody = nullptr, *end_bbody = nullptr; + ir_block *bprecond = nullptr, *end_bprecond = nullptr; + ir_block *bpostcond = nullptr, *end_bpostcond = nullptr; + ir_block *bincrement = nullptr, *end_bincrement = nullptr; + ir_block *bout = nullptr, *bin = nullptr; /* let's at least move the outgoing block to the end */ size_t bout_id; /* 'break' and 'continue' need to be able to find the right blocks */ - ir_block *bcontinue = NULL; - ir_block *bbreak = NULL; + ir_block *bcontinue = nullptr; + ir_block *bbreak = nullptr; - ir_block *tmpblock = NULL; + ir_block *tmpblock = nullptr; (void)lvalue; (void)out; @@ -2896,7 +2847,7 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value end_bprecond = func->curblock; } else { - bprecond = end_bprecond = NULL; + bprecond = end_bprecond = nullptr; } /* Now the next blocks won't be ordered nicely, but we need to @@ -2908,7 +2859,7 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value return false; bcontinue = bincrement; /* increment comes before the pre-loop-condition */ } else { - bincrement = end_bincrement = NULL; + bincrement = end_bincrement = nullptr; } if (self->postcond) { @@ -2917,7 +2868,7 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value return false; bcontinue = bpostcond; /* postcond comes before the increment */ } else { - bpostcond = end_bpostcond = NULL; + bpostcond = end_bpostcond = nullptr; } bout_id = vec_size(func->ir_func->blocks); @@ -2936,11 +2887,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 +2901,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 */ @@ -3074,7 +3025,7 @@ bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, { ir_block *target; - *out = NULL; + *out = nullptr; if (lvalue) { compile_error(ast_ctx(self), "break/continue expression is not an l-value"); @@ -3088,9 +3039,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")); @@ -3106,17 +3057,16 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va { ast_expression_codegen *cgen; - ast_switch_case *def_case = NULL; - ir_block *def_bfall = NULL; - ir_block *def_bfall_to = NULL; + ast_switch_case *def_case = nullptr; + ir_block *def_bfall = nullptr; + ir_block *def_bfall_to = nullptr; bool set_def_bfall_to = false; - ir_value *dummy = NULL; - ir_value *irop = NULL; - ir_block *bout = NULL; - ir_block *bfall = NULL; + ir_value *dummy = nullptr; + ir_value *irop = nullptr; + ir_block *bout = nullptr; + ir_block *bfall = nullptr; size_t bout_id; - size_t c; char typestr[1024]; uint16_t cmpinstr; @@ -3139,7 +3089,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 +3105,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 */ @@ -3211,7 +3161,7 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va /* The default case */ /* Remember where to fall through from: */ def_bfall = bfall; - bfall = NULL; + bfall = nullptr; /* remember which case it was */ def_case = swcase; /* And the next case will be remembered */ @@ -3260,7 +3210,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 +3221,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) { @@ -3279,7 +3228,7 @@ bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_valu return false; } - *out = NULL; + *out = nullptr; if (lvalue) { compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue"); return false; @@ -3300,8 +3249,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; } @@ -3310,7 +3259,7 @@ bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_valu bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out) { - *out = NULL; + *out = nullptr; if (lvalue) { compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue"); return false; @@ -3361,7 +3310,7 @@ bool ast_state_codegen(ast_state *self, ast_function *func, bool lvalue, ir_valu compile_error(ast_ctx(self), "internal error: ast_state cannot be reused!"); return false; } - *out = NULL; + *out = nullptr; cgen = self->framenum->codegen; if (!(*cgen)((ast_expression*)(self->framenum), func, false, &frameval)) @@ -3387,11 +3336,10 @@ 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; + ir_value *funval = nullptr; /* return values are never lvalues */ if (lvalue) { @@ -3410,20 +3358,15 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value if (!funval) return false; - 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)) - goto error; + cgen = it->codegen; + if (!(*cgen)(it, func, false, ¶m)) + return false; if (!param) - goto error; - vec_push(params, param); + return false; + params.push_back(param); } /* varargs counter */ @@ -3444,20 +3387,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; }