std::vector for initlist

This commit is contained in:
Dale Weiler 2015-01-15 14:07:26 -05:00
parent b345b4e21b
commit 987f765c20
4 changed files with 18 additions and 26 deletions

22
ast.cpp
View file

@ -336,7 +336,6 @@ ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
self->inexact = false; self->inexact = false;
self->uses = 0; self->uses = 0;
memset(&self->constval, 0, sizeof(self->constval)); memset(&self->constval, 0, sizeof(self->constval));
self->initlist = NULL;
self->ir_v = NULL; self->ir_v = NULL;
self->ir_values = NULL; self->ir_values = NULL;
@ -381,18 +380,11 @@ void ast_value_delete(ast_value* self)
if (self->desc) if (self->desc)
mem_d(self->desc); mem_d(self->desc);
if (self->initlist) { // initlist imples an array which implies .next in the expression exists.
if (self->expression.next->vtype == TYPE_STRING) { if (self->initlist.size() && self->expression.next->vtype == TYPE_STRING) {
/* strings are allocated, free them */ for (auto &it : self->initlist)
size_t i, len = vec_size(self->initlist); if (it.vstring)
/* in theory, len should be expression.count mem_d(it.vstring);
* 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);
} }
ast_expression_delete((ast_expression*)self); 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) 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; size_t i;
if (count > self->expression.count) { 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) 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); compile_error(ast_ctx(self), "array without size: %s", self->name);
return false; return false;
} }

14
ast.h
View file

@ -174,13 +174,13 @@ struct ast_expression {
* is like creating a 'float foo', foo serving as the type's name. * is like creating a 'float foo', foo serving as the type's name.
*/ */
union basic_value_t { union basic_value_t {
qcfloat_t vfloat; qcfloat_t vfloat;
int vint; int vint;
vec3_t vvec; vec3_t vvec;
const char *vstring; const char *vstring;
int ventity; int ventity;
ast_function *vfunc; ast_function *vfunc;
ast_value *vfield; ast_value *vfield;
}; };
struct ast_value struct ast_value
@ -202,7 +202,7 @@ struct ast_value
* of constants when an initializer list * of constants when an initializer list
* was provided. * was provided.
*/ */
basic_value_t *initlist; std::vector<basic_value_t> initlist;
/* usecount for the parser */ /* usecount for the parser */
size_t uses; size_t uses;

View file

@ -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)) if (fold_can_1(a) && isstring(a))
return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false); return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false);
if (isarray(a)) if (isarray(a))
return fold_constgen_float(fold, vec_size(a->initlist), false); return fold_constgen_float(fold, a->initlist.size(), false);
return NULL; return NULL;
} }

View file

@ -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) static bool parse_array(parser_t *parser, ast_value *array)
{ {
size_t i; size_t i;
if (array->initlist) { if (array->initlist.size()) {
parseerror(parser, "array already initialized elsewhere"); parseerror(parser, "array already initialized elsewhere");
return false; 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"); parseerror(parser, "initializing element must be a compile time constant");
return false; return false;
} }
vec_push(array->initlist, v->constval); array->initlist.push_back(v->constval);
if (v->expression.vtype == TYPE_STRING) { if (v->expression.vtype == TYPE_STRING) {
array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring); array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring);
++i; ++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", parseerror(parser, "array `%s' has already been initialized with %u elements",
array->name, (unsigned)array->expression.count); 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)) if (!create_array_accessors(parser, array))
return false; return false;
} }