+ }
+
+ if (!ast_istype(nextthink, ast_value)) {
+ parseerror(parser, "think-function in [frame,think] notation must be a constant");
+ retval = false;
+ }
+
+ if (retval && parser->tok != ']') {
+ parseerror(parser, "expected closing `]` for [frame,think] notation");
+ retval = false;
+ }
+
+ if (retval && !parser_next(parser)) {
+ retval = false;
+ }
+
+ if (retval && parser->tok != '{') {
+ parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
+ retval = false;
+ }
+
+ if (!retval) {
+ ast_unref(nextthink);
+ ast_unref(framenum);
+ return false;
+ }
+
+ has_frame_think = true;
+ }
+
+ block = ast_block_new(parser_ctx(parser));
+ if (!block) {
+ parseerror(parser, "failed to allocate block");
+ if (has_frame_think) {
+ ast_unref(nextthink);
+ ast_unref(framenum);
+ }
+ return false;
+ }
+
+ if (has_frame_think) {
+ lex_ctx ctx;
+ ast_expression *self_frame;
+ ast_expression *self_nextthink;
+ ast_expression *self_think;
+ ast_expression *time_plus_1;
+ ast_store *store_frame;
+ ast_store *store_nextthink;
+ ast_store *store_think;
+
+ ctx = parser_ctx(parser);
+ self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
+ self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
+ self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
+
+ time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
+ gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
+
+ if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
+ if (self_frame) ast_delete(self_frame);
+ if (self_nextthink) ast_delete(self_nextthink);
+ if (self_think) ast_delete(self_think);
+ if (time_plus_1) ast_delete(time_plus_1);
+ retval = false;
+ }
+
+ if (retval)
+ {
+ store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
+ store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
+ store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
+
+ if (!store_frame) {
+ ast_delete(self_frame);
+ retval = false;
+ }
+ if (!store_nextthink) {
+ ast_delete(self_nextthink);
+ retval = false;
+ }
+ if (!store_think) {
+ ast_delete(self_think);
+ retval = false;
+ }
+ if (!retval) {
+ if (store_frame) ast_delete(store_frame);
+ if (store_nextthink) ast_delete(store_nextthink);
+ if (store_think) ast_delete(store_think);
+ retval = false;
+ }
+ if (retval && !ast_block_exprs_add(block, (ast_expression*)store_frame)) {
+ ast_delete(store_frame);
+ ast_delete(store_nextthink);
+ ast_delete(store_think);
+ retval = false;
+ }
+
+ if (retval && !ast_block_exprs_add(block, (ast_expression*)store_nextthink)) {
+ ast_delete(store_nextthink);
+ ast_delete(store_think);
+ retval = false;
+ }
+
+ if (retval && !ast_block_exprs_add(block, (ast_expression*)store_think) )
+ {
+ ast_delete(store_think);
+ retval = false;
+ }
+ }
+
+ if (!retval) {
+ parseerror(parser, "failed to generate code for [frame,think]");
+ ast_unref(nextthink);
+ ast_unref(framenum);
+ ast_delete(block);
+ return false;
+ }
+ }
+
+ for (parami = 0; parami < var->expression.params_count; ++parami) {
+ size_t e;
+ varentry_t ve[3];
+ ast_value *param = var->expression.params[parami];
+
+ if (param->expression.vtype != TYPE_VECTOR &&
+ (param->expression.vtype != TYPE_FIELD ||
+ param->expression.next->expression.vtype != TYPE_VECTOR))
+ {
+ continue;
+ }
+
+ if (!create_vector_members(parser, param, ve)) {
+ ast_block_delete(block);
+ return false;
+ }
+
+ for (e = 0; e < 3; ++e) {
+ if (!parser_t_locals_add(parser, ve[e]))
+ break;
+ if (!ast_block_collect(block, ve[e].var)) {
+ parser->locals_count--;
+ break;
+ }
+ ve[e].var = NULL; /* collected */
+ }
+ if (e != e) {
+ parser->locals -= e;
+ do {
+ mem_d(ve[e].name);
+ --e;
+ } while (e);
+ ast_block_delete(block);
+ return false;
+ }
+ }
+
+ func = ast_function_new(ast_ctx(var), var->name, var);
+ if (!func) {
+ parseerror(parser, "failed to allocate function for `%s`", var->name);
+ ast_block_delete(block);
+ goto enderr;
+ }
+ if (!parser_t_functions_add(parser, func)) {
+ parseerror(parser, "failed to allocate slot for function `%s`", var->name);
+ ast_block_delete(block);
+ goto enderrfn;
+ }
+
+ parser->function = func;
+ if (!parse_block_into(parser, block, true)) {
+ ast_block_delete(block);
+ goto enderrfn2;
+ }
+
+ if (!ast_function_blocks_add(func, block)) {
+ ast_block_delete(block);
+ goto enderrfn2;
+ }
+
+ parser->function = old;
+ while (parser->locals_count)
+ retval = retval && parser_pop_local(parser);
+
+ if (parser->tok == ';')
+ return parser_next(parser);
+ else if (opts_standard == COMPILER_QCC)
+ parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
+ return retval;
+
+enderrfn2:
+ parser->functions_count--;
+enderrfn:
+ ast_function_delete(func);
+ var->constval.vfunc = NULL;
+
+enderr:
+ while (parser->locals_count) {
+ parser->locals_count--;
+ mem_d(parser->locals[parser->locals_count].name);
+ }
+ parser->function = old;
+ return false;
+}
+
+static bool parse_variable(parser_t *parser, ast_block *localblock)
+{
+ bool isfunc = false;
+ lex_ctx ctx;
+
+ ast_value *var = NULL;
+ bool cleanvar = false;
+
+ varentry_t varent;
+ varentry_t ve[3];
+
+ ast_expression *olddecl;
+
+ ast_value *typevar;
+
+ bool hadproto;
+ bool isparam;
+
+ bool retval = true;
+
+ /* go */
+
+ int basetype = parser_token(parser)->constval.t;
+
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected variable definition");
+ return false;
+ }
+
+ typevar = parse_type(parser, basetype, &isfunc);
+ if (!typevar)
+ return false;
+
+ while (true)
+ {
+ hadproto = false;
+ olddecl = NULL;
+ isparam = false;
+ varent.name = NULL;
+
+ ve[0].name = ve[1].name = ve[2].name = NULL;
+ ve[0].var = ve[1].var = ve[2].var = NULL;
+
+ ctx = parser_ctx(parser);
+ var = ast_value_copy(typevar);
+ cleanvar = true;
+
+ if (!var) {
+ parseerror(parser, "failed to create variable");
+ retval = false;
+ goto cleanup;
+ }
+
+ if (parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "expected variable name");
+ retval = false;
+ goto cleanup;
+ }
+
+ if (!localblock) {
+ if (!strcmp(parser_tokval(parser), "end_sys_globals"))
+ parser->crc_globals = parser->globals_count;
+ else if (!strcmp(parser_tokval(parser), "end_sys_fields"))
+ parser->crc_fields = parser->fields_count;
+ }
+
+ if (!isfunc) {
+ if (!localblock && (olddecl = parser_find_global(parser, parser_tokval(parser)))) {
+ parseerror(parser, "global `%s` already declared here: %s:%i",
+ parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+ retval = false;
+ goto cleanup;
+ }
+
+ if (localblock) {
+ olddecl = parser_find_local(parser, parser_tokval(parser), parser->blocklocal, &isparam);
+ if (opts_standard == COMPILER_GMQCC)
+ {
+ if (olddecl)
+ {
+ if (!isparam) {
+ parseerror(parser, "local `%s` already declared here: %s:%i",
+ parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+ retval = false;
+ goto cleanup;
+ }
+ }
+
+ if( (!isparam && olddecl) ||
+ (olddecl = parser_find_local(parser, parser_tokval(parser), 0, &isparam))
+ )
+ {
+ if (parsewarning(parser, WARN_LOCAL_SHADOWS,
+ "local `%s` is shadowing a parameter", parser_tokval(parser)))
+ {
+ parseerror(parser, "local `%s` already declared here: %s:%i",
+ parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+ retval = false;
+ goto cleanup;
+ }
+ }
+ }
+ else
+ {
+ if (olddecl)
+ {
+ ast_value_delete(var);
+ var = NULL;
+ if (isparam &&
+ parsewarning(parser, WARN_LOCAL_SHADOWS,
+ "a parameter is shadowing local `%s`", parser_tokval(parser)))
+ {
+ retval = false;
+ goto cleanup;
+ }
+ else if (!isparam)
+ {
+ parseerror(parser, "local `%s` already declared here: %s:%i",
+ parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+ retval = false;
+ goto cleanup;
+ }
+ goto nextvar;
+ }
+ }
+ }
+ }
+
+ if (!ast_value_set_name(var, parser_tokval(parser))) {
+ parseerror(parser, "failed to set variable name\n");
+ retval = false;
+ goto cleanup;
+ }
+
+ if (isfunc) {
+ /* a function was defined */
+ ast_value *fval;
+ ast_value *proto = NULL;
+ bool dummy;
+
+ if (!localblock)
+ olddecl = parser_find_global(parser, parser_tokval(parser));
+ else
+ olddecl = parser_find_local(parser, parser_tokval(parser), parser->blocklocal, &dummy);
+
+ if (olddecl) {
+ /* we had a prototype */
+ if (!ast_istype(olddecl, ast_value)) {
+ /* vector v;
+ * void() v_x = {}
+ */
+ parseerror(parser, "cannot declare a function with the same name as a vector's member: %s",
+ parser_tokval(parser));
+ retval = false;
+ goto cleanup;
+ }
+
+ proto = (ast_value*)olddecl;
+ }
+
+ /* 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) {
+ retval = false;
+ goto cleanup;
+ }