self->fixedparams = NULL;
self->return_value = NULL;
+ self->static_names = NULL;
+ self->static_count = 0;
+
return self;
cleanup:
*/
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);
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;
*/
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);
/* 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 */
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;