From c3cc6f184e6bab86402289609ddd57ce733a7812 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 28 Nov 2013 12:04:01 +0100 Subject: [PATCH] static variables now don't get re-initialized in functions; cannot be initialized with non-constants anymore; and a counter has been added so you can use the same name in a different scope for another static variable again. --- ast.c | 6 ++++++ ast.h | 8 ++++++++ parser.c | 26 +++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ast.c b/ast.c index 7b2da4d..1644e24 100644 --- a/ast.c +++ b/ast.c @@ -1222,6 +1222,9 @@ ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype self->fixedparams = NULL; self->return_value = NULL; + self->static_names = NULL; + self->static_count = 0; + return self; cleanup: @@ -1243,6 +1246,9 @@ 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); diff --git a/ast.h b/ast.h index 52858ac..2643413 100644 --- a/ast.h +++ b/ast.h @@ -625,6 +625,14 @@ struct ast_function_s int builtin; + /* list of used-up names for statics without the count suffix */ + char **static_names; + /* number of static variables, by convention this includes the + * ones without the count-suffix - remember this when dealing + * with savegames. uint instead of size_t as %zu in printf is + * C99, so no windows support. */ + unsigned int static_count; + ir_function *ir_func; ir_block *curblock; ir_block **breakblocks; diff --git a/parser.c b/parser.c index c96f862..f3bfdfb 100644 --- a/parser.c +++ b/parser.c @@ -5441,6 +5441,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield */ char *defname = NULL; size_t prefix_len, ln; + size_t sn, sn_size; ln = strlen(parser->function->name); vec_append(defname, ln, parser->function->name); @@ -5462,6 +5463,24 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield /* now rename the global */ ln = strlen(var->name); vec_append(defname, ln, var->name); + /* if a variable of that name already existed, add the + * counter value. + * The counter is incremented either way. + */ + sn_size = vec_size(parser->function->static_names); + for (sn = 0; sn != sn_size; ++sn) { + if (strcmp(parser->function->static_names[sn], var->name) == 0) + break; + } + if (sn != sn_size) { + char *num = NULL; + int len = util_asprintf(&num, "#%u", parser->function->static_count); + vec_append(defname, len, num); + mem_d(num); + } + else + vec_push(parser->function->static_names, util_strdup(var->name)); + parser->function->static_count++; ast_value_set_name(var, defname); /* push it to the to-be-generated globals */ @@ -5712,17 +5731,18 @@ skipvar: if (!cexp) break; - if (!localblock) { + if (!localblock || is_static) { cval = (ast_value*)cexp; if (cval != parser->nil && (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield)) ) { - parseerror(parser, "cannot initialize a global constant variable with a non-constant expression"); + parseerror(parser, "initializer is non constant"); } else { - if (!OPTS_FLAG(INITIALIZED_NONCONSTANTS) && + if (!is_static && + !OPTS_FLAG(INITIALIZED_NONCONSTANTS) && qualifier != CV_VAR) { var->cvq = CV_CONST; -- 2.39.2