From 47243d0df1bffefb08b91643db02bd6dbb7cb331 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Blub) Bumiller" Date: Thu, 16 Aug 2012 15:14:39 +0200 Subject: [PATCH] allow redeclaration of fields, but warn about them --- parser.c | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/parser.c b/parser.c index 2f65393..8bde63a 100644 --- a/parser.c +++ b/parser.c @@ -1961,6 +1961,7 @@ static bool parser_do(parser_t *parser) { ast_value *var; ast_value *fld; + ast_expression *oldex; bool isfunc = false; int basetype; lex_ctx ctx = parser_ctx(parser); @@ -2005,35 +2006,43 @@ static bool parser_do(parser_t *parser) return false; } } - if (parser_find_field(parser, parser_tokval(parser))) { - parseerror(parser, "field %s already exists", parser_tokval(parser)); - ast_delete(var); - return false; - } - /* if it was a function, turn it into a function */ - if (isfunc) { - ast_value *fval; - /* turn var into a value of TYPE_FUNCTION, with the old var - * as return type - */ - fval = ast_value_new(ctx, var->name, TYPE_FUNCTION); - if (!fval) { - ast_value_delete(var); - ast_value_delete(fval); + /* turn it into a field */ + fld = ast_value_new(ctx, parser_tokval(parser), TYPE_FIELD); + fld->expression.next = (ast_expression*)var; + + if ( (oldex = parser_find_field(parser, parser_tokval(parser)))) { + if (ast_istype(oldex, ast_member)) { + parseerror(parser, "cannot declare a field with the same name as a vector component, component %s has been declared here: %s:%i", + parser_tokval(parser), ast_ctx(oldex).file, (int)ast_ctx(oldex).line); + ast_delete(fld); + return false; + } + if (!ast_istype(oldex, ast_value)) { + /* not possible / sanity check */ + parseerror(parser, "internal error: %s is not an ast_value", parser_tokval(parser)); + ast_delete(fld); return false; } - fval->expression.next = (ast_expression*)var; - MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params); + if (!ast_compare_type(oldex, (ast_expression*)fld)) { + parseerror(parser, "field %s has previously been declared with a different type here: %s:%i", + parser_tokval(parser), ast_ctx(oldex).file, (int)ast_ctx(oldex).line); + ast_delete(fld); + return false; + } else { + if (parsewarning(parser, WARN_FIELD_REDECLARED, "field %s has already been declared here: %s:%i", + parser_tokval(parser), ast_ctx(oldex).file, (int)ast_ctx(oldex).line)) + { + ast_delete(fld); + return false; + } + } - var = fval; + ast_delete(fld); + goto nextfield; } - /* turn it into a field */ - fld = ast_value_new(ctx, parser_tokval(parser), TYPE_FIELD); - fld->expression.next = (ast_expression*)var; - varent.var = (ast_expression*)fld; varent.name = util_strdup(fld->name); (void)!parser_t_fields_add(parser, varent); @@ -2064,6 +2073,7 @@ static bool parser_do(parser_t *parser) (void)!parser_t_fields_add(parser, vz); } +nextfield: if (!parser_next(parser)) { parseerror(parser, "expected semicolon or another field name"); return false; -- 2.39.2