+ ast_block *block = NULL;
+ ast_function *func;
+ ast_function *old;
+ size_t parami;
+
+ ast_expression *framenum = NULL;
+ ast_expression *nextthink = NULL;
+ /* None of the following have to be deleted */
+ ast_expression *fld_think, *fld_nextthink, *fld_frame;
+ ast_expression *gbl_time, *gbl_self;
+ bool has_frame_think;
+
+ bool retval = true;
+
+ has_frame_think = false;
+ old = parser->function;
+
+ if (var->expression.variadic) {
+ if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
+ "variadic function with implementation will not be able to access additional parameters"))
+ {
+ return false;
+ }
+ }
+
+ if (parser->tok == '[') {
+ /* got a frame definition: [ framenum, nextthink ]
+ * this translates to:
+ * self.frame = framenum;
+ * self.nextthink = time + 0.1;
+ * self.think = nextthink;
+ */
+ nextthink = NULL;
+
+ fld_think = parser_find_field(parser, "think");
+ fld_nextthink = parser_find_field(parser, "nextthink");
+ fld_frame = parser_find_field(parser, "frame");
+ if (!fld_think || !fld_nextthink || !fld_frame) {
+ parseerror(parser, "cannot use [frame,think] notation without the required fields");
+ parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
+ return false;
+ }
+ gbl_time = parser_find_global(parser, "time");
+ gbl_self = parser_find_global(parser, "self");
+ if (!gbl_time || !gbl_self) {
+ parseerror(parser, "cannot use [frame,think] notation without the required globals");
+ parseerror(parser, "please declare the following globals: `time`, `self`");
+ return false;
+ }
+
+ if (!parser_next(parser))
+ return false;
+
+ framenum = parse_expression_leave(parser, true);
+ if (!framenum) {
+ parseerror(parser, "expected a framenumber constant in[frame,think] notation");
+ return false;
+ }
+ if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
+ ast_unref(framenum);
+ parseerror(parser, "framenumber in [frame,think] notation must be a constant");
+ return false;
+ }
+
+ if (parser->tok != ',') {
+ ast_unref(framenum);
+ parseerror(parser, "expected comma after frame number in [frame,think] notation");
+ parseerror(parser, "Got a %i\n", parser->tok);
+ return false;
+ }
+
+ if (!parser_next(parser)) {
+ ast_unref(framenum);
+ return false;
+ }
+
+ if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
+ {
+ /* qc allows the use of not-yet-declared functions here
+ * - this automatically creates a prototype */
+ varentry_t varent;
+ ast_value *thinkfunc;
+ ast_expression *functype = fld_think->expression.next;
+
+ thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
+ if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
+ ast_unref(framenum);
+ parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
+ return false;
+ }
+
+ if (!parser_next(parser)) {
+ ast_unref(framenum);
+ ast_delete(thinkfunc);
+ return false;
+ }
+
+ varent.var = (ast_expression*)thinkfunc;
+ varent.name = util_strdup(thinkfunc->name);
+ if (!parser_t_globals_add(parser, varent)) {
+ ast_unref(framenum);
+ ast_delete(thinkfunc);
+ return false;
+ }
+ nextthink = (ast_expression*)thinkfunc;
+
+ } else {
+ nextthink = parse_expression_leave(parser, true);
+ if (!nextthink) {
+ ast_unref(framenum);
+ parseerror(parser, "expected a think-function in [frame,think] notation");
+ return false;
+ }
+ }
+
+ 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 enderrfn;
+ }
+
+ if (!ast_function_blocks_add(func, block)) {
+ ast_block_delete(block);
+ goto enderrfn;
+ }
+
+ 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;
+
+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];
+