*/
AST_FLAG_ARRAY_INIT = 1 << 10,
+ AST_FLAG_FINAL_DECL = 1 << 11,
+
AST_FLAG_LAST,
AST_FLAG_TYPE_MASK = (AST_FLAG_VARIADIC | AST_FLAG_NORETURN)
};
return false;
}
}
+ else if (!strcmp(parser_tokval(parser), "final")) {
+ flags |= AST_FLAG_FINAL_DECL;
+ if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
+ parseerror(parser, "`final` attribute has no parameters, expected `]]`");
+ *cvq = CV_WRONG;
+ return false;
+ }
+ }
else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
flags |= AST_FLAG_ALIAS;
*message = NULL;
had_var = true;
else if (!strcmp(parser_tokval(parser), "noref"))
had_noref = true;
+ else if (!strcmp(parser_tokval(parser), "final"))
+ flags |= AST_FLAG_FINAL_DECL;
else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
return false;
}
retval = false;
goto cleanup;
}
+ if (old->flags & AST_FLAG_FINAL_DECL) {
+ parseerror(parser, "cannot redeclare variable `%s`, declared final here: %s:%i",
+ var->name, ast_ctx(old).file, ast_ctx(old).line);
+ retval = false;
+ goto cleanup;
+ }
proto = (ast_value*)old;
if (!ast_istype(old, ast_value)) {
parseerror(parser, "internal error: not an ast_value");
goto cleanup;
}
proto->expression.flags |= var->expression.flags;
+ /* copy the context for finals,
+ * so the error can show where it was actually made 'final'
+ */
+ if (proto->expression.flags & AST_FLAG_FINAL_DECL)
+ ast_ctx(old) = ast_ctx(var);
ast_delete(var);
var = proto;
}