From: Dale Weiler Date: Thu, 15 Jan 2015 19:07:26 +0000 (-0500) Subject: std::vector for initlist X-Git-Tag: xonotic-v0.8.2~42 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=987f765c201bd81a84fb20d8858de70a2f4e47ae;p=xonotic%2Fgmqcc.git std::vector for initlist --- diff --git a/ast.cpp b/ast.cpp index 57a7cc2..6db9bef 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); @@ -1318,7 +1310,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 +1367,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; } diff --git a/ast.h b/ast.h index 3486d65..3938f2a 100644 --- a/ast.h +++ b/ast.h @@ -174,13 +174,13 @@ struct ast_expression { * is like creating a 'float foo', foo serving as the type's name. */ union basic_value_t { - qcfloat_t vfloat; - int vint; - vec3_t vvec; - const char *vstring; - int ventity; + qcfloat_t vfloat; + int vint; + vec3_t vvec; + const char *vstring; + int ventity; ast_function *vfunc; - ast_value *vfield; + ast_value *vfield; }; struct ast_value @@ -202,7 +202,7 @@ struct ast_value * of constants when an initializer list * was provided. */ - basic_value_t *initlist; + std::vector initlist; /* usecount for the parser */ size_t uses; diff --git a/fold.cpp b/fold.cpp index 5e40955..1e4bcd4 100644 --- a/fold.cpp +++ b/fold.cpp @@ -1365,7 +1365,7 @@ static GMQCC_INLINE ast_expression *fold_op_length(fold_t *fold, ast_value *a) { if (fold_can_1(a) && isstring(a)) return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false); if (isarray(a)) - return fold_constgen_float(fold, vec_size(a->initlist), false); + return fold_constgen_float(fold, a->initlist.size(), false); return NULL; } diff --git a/parser.cpp b/parser.cpp index 0d71b54..c2d3eb3 100644 --- a/parser.cpp +++ b/parser.cpp @@ -5005,7 +5005,7 @@ static bool create_array_accessors(parser_t *parser, ast_value *var) static bool parse_array(parser_t *parser, ast_value *array) { size_t i; - if (array->initlist) { + if (array->initlist.size()) { parseerror(parser, "array already initialized elsewhere"); return false; } @@ -5023,7 +5023,7 @@ static bool parse_array(parser_t *parser, ast_value *array) parseerror(parser, "initializing element must be a compile time constant"); return false; } - vec_push(array->initlist, v->constval); + array->initlist.push_back(v->constval); if (v->expression.vtype == TYPE_STRING) { array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring); ++i; @@ -5052,7 +5052,7 @@ static bool parse_array(parser_t *parser, ast_value *array) parseerror(parser, "array `%s' has already been initialized with %u elements", array->name, (unsigned)array->expression.count); } - array->expression.count = vec_size(array->initlist); + array->expression.count = array->initlist.size(); if (!create_array_accessors(parser, array)) return false; }