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->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;
}

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.
*/
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<basic_value_t> initlist;
/* usecount for the parser */
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))
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;
}

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)
{
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;
}