16 MEM_VECTOR_MAKE(varentry_t, globals);
17 MEM_VECTOR_MAKE(varentry_t, fields);
18 MEM_VECTOR_MAKE(ast_function*, functions);
19 MEM_VECTOR_MAKE(ast_value*, imm_float);
20 MEM_VECTOR_MAKE(ast_value*, imm_string);
21 MEM_VECTOR_MAKE(ast_value*, imm_vector);
23 ast_value *imm_float_zero;
24 ast_value *imm_vector_zero;
29 ast_function *function;
30 MEM_VECTOR_MAKE(varentry_t, locals);
35 /* we store the '=' operator info */
36 const oper_info *assign_op;
38 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
39 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
40 * anything else: type error
45 MEM_VEC_FUNCTIONS(parser_t, varentry_t, globals)
46 MEM_VEC_FUNCTIONS(parser_t, varentry_t, fields)
47 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_float)
48 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_string)
49 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
50 MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
51 MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
53 static bool GMQCC_WARN parser_pop_local(parser_t *parser);
54 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields);
55 static ast_block* parse_block(parser_t *parser, bool warnreturn);
56 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
57 static ast_expression* parse_statement_or_block(parser_t *parser);
58 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
59 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
61 static void parseerror(parser_t *parser, const char *fmt, ...)
68 vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
72 /* returns true if it counts as an error */
73 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
76 int lvl = LVL_WARNING;
78 if (!OPTS_WARN(warntype))
87 vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
93 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
96 int lvl = LVL_WARNING;
98 if (!OPTS_WARN(warntype))
105 vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
111 /**********************************************************************
112 * some maths used for constant folding
115 vector vec3_add(vector a, vector b)
124 vector vec3_sub(vector a, vector b)
133 qcfloat vec3_mulvv(vector a, vector b)
135 return (a.x * b.x + a.y * b.y + a.z * b.z);
138 vector vec3_mulvf(vector a, float b)
147 /**********************************************************************
151 bool parser_next(parser_t *parser)
153 /* lex_do kills the previous token */
154 parser->tok = lex_do(parser->lex);
155 if (parser->tok == TOKEN_EOF)
157 if (parser->tok >= TOKEN_ERROR) {
158 parseerror(parser, "lex error");
164 #define parser_tokval(p) ((p)->lex->tok.value)
165 #define parser_token(p) (&((p)->lex->tok))
166 #define parser_ctx(p) ((p)->lex->tok.ctx)
168 static ast_value* parser_const_float(parser_t *parser, double d)
172 for (i = 0; i < parser->imm_float_count; ++i) {
173 if (parser->imm_float[i]->constval.vfloat == d)
174 return parser->imm_float[i];
176 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
178 out->constval.vfloat = d;
179 if (!parser_t_imm_float_add(parser, out)) {
180 ast_value_delete(out);
186 static ast_value* parser_const_float_0(parser_t *parser)
188 if (!parser->imm_float_zero)
189 parser->imm_float_zero = parser_const_float(parser, 0);
190 return parser->imm_float_zero;
193 static char *parser_strdup(const char *str)
196 /* actually dup empty strings */
197 char *out = mem_a(1);
201 return util_strdup(str);
204 static ast_value* parser_const_string(parser_t *parser, const char *str)
208 for (i = 0; i < parser->imm_string_count; ++i) {
209 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
210 return parser->imm_string[i];
212 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
214 out->constval.vstring = parser_strdup(str);
215 if (!parser_t_imm_string_add(parser, out)) {
216 ast_value_delete(out);
222 static ast_value* parser_const_vector(parser_t *parser, vector v)
226 for (i = 0; i < parser->imm_vector_count; ++i) {
227 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
228 return parser->imm_vector[i];
230 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
232 out->constval.vvec = v;
233 if (!parser_t_imm_vector_add(parser, out)) {
234 ast_value_delete(out);
240 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
246 return parser_const_vector(parser, v);
249 static ast_value* parser_const_vector_0(parser_t *parser)
251 if (!parser->imm_vector_zero)
252 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
253 return parser->imm_vector_zero;
256 static ast_expression* parser_find_field(parser_t *parser, const char *name)
259 for (i = 0; i < parser->fields_count; ++i) {
260 if (!strcmp(parser->fields[i].name, name))
261 return parser->fields[i].var;
266 static ast_expression* parser_find_global(parser_t *parser, const char *name)
269 for (i = 0; i < parser->globals_count; ++i) {
270 if (!strcmp(parser->globals[i].name, name))
271 return parser->globals[i].var;
276 static ast_expression* parser_find_param(parser_t *parser, const char *name)
280 if (!parser->function)
282 fun = parser->function->vtype;
283 for (i = 0; i < fun->expression.params_count; ++i) {
284 if (!strcmp(fun->expression.params[i]->name, name))
285 return (ast_expression*)(fun->expression.params[i]);
290 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
294 for (i = parser->locals_count; i > upto;) {
296 if (!strcmp(parser->locals[i].name, name))
297 return parser->locals[i].var;
300 return parser_find_param(parser, name);
303 static ast_expression* parser_find_var(parser_t *parser, const char *name)
307 v = parser_find_local(parser, name, 0, &dummy);
308 if (!v) v = parser_find_global(parser, name);
314 size_t etype; /* 0 = expression, others are operators */
318 ast_block *block; /* for commas and function calls */
323 MEM_VECTOR_MAKE(sy_elem, out);
324 MEM_VECTOR_MAKE(sy_elem, ops);
326 MEM_VEC_FUNCTIONS(shunt, sy_elem, out)
327 MEM_VEC_FUNCTIONS(shunt, sy_elem, ops)
329 #define SY_PAREN_EXPR '('
330 #define SY_PAREN_FUNC 'f'
331 #define SY_PAREN_INDEX '['
333 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
344 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
348 e.out = (ast_expression*)v;
355 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
357 e.etype = 1 + (op - operators);
366 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
378 # define DEBUGSHUNTDO(x) x
380 # define DEBUGSHUNTDO(x)
383 static bool parser_sy_pop(parser_t *parser, shunt *sy)
387 ast_expression *out = NULL;
388 ast_expression *exprs[3];
389 ast_block *blocks[3];
390 ast_value *asvalue[3];
392 qcint generated_op = 0;
397 if (!sy->ops_count) {
398 parseerror(parser, "internal error: missing operator");
402 if (sy->ops[sy->ops_count-1].paren) {
403 parseerror(parser, "unmatched parenthesis");
407 op = &operators[sy->ops[sy->ops_count-1].etype - 1];
408 ctx = sy->ops[sy->ops_count-1].ctx;
410 DEBUGSHUNTDO(printf("apply %s\n", op->op));
412 if (sy->out_count < op->operands) {
413 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", sy->out_count,
414 op->op, (int)op->id);
420 sy->out_count -= op->operands;
421 for (i = 0; i < op->operands; ++i) {
422 exprs[i] = sy->out[sy->out_count+i].out;
423 blocks[i] = sy->out[sy->out_count+i].block;
424 asvalue[i] = (ast_value*)exprs[i];
427 if (blocks[0] && !blocks[0]->exprs_count && op->id != opid1(',')) {
428 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
432 #define NotSameType(T) \
433 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
434 exprs[0]->expression.vtype != T)
435 #define CanConstFold1(A) \
436 (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
437 #define CanConstFold(A, B) \
438 (CanConstFold1(A) && CanConstFold1(B))
439 #define ConstV(i) (asvalue[(i)]->constval.vvec)
440 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
441 #define ConstS(i) (asvalue[(i)]->constval.vstring)
445 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
449 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
450 if (exprs[1]->expression.vtype != TYPE_FIELD) {
451 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
454 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
456 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
457 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
461 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
467 if (exprs[0]->expression.vtype != TYPE_ARRAY) {
468 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
469 parseerror(parser, "cannot index value of type %s", ty1);
472 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
473 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
474 parseerror(parser, "index must be of type float, not %s", ty1);
477 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
482 if (!ast_block_exprs_add(blocks[0], exprs[1]))
485 blocks[0] = ast_block_new(ctx);
486 if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
487 !ast_block_exprs_add(blocks[0], exprs[1]))
492 if (!ast_block_set_type(blocks[0], exprs[1]))
495 sy->out[sy->out_count++] = syblock(ctx, blocks[0]);
499 switch (exprs[0]->expression.vtype) {
501 if (CanConstFold1(exprs[0]))
502 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
504 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
505 (ast_expression*)parser_const_float_0(parser),
509 if (CanConstFold1(exprs[0]))
510 out = (ast_expression*)parser_const_vector_f(parser,
511 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
513 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
514 (ast_expression*)parser_const_vector_0(parser),
518 parseerror(parser, "invalid types used in expression: cannot negate type %s",
519 type_name[exprs[0]->expression.vtype]);
525 switch (exprs[0]->expression.vtype) {
527 if (CanConstFold1(exprs[0]))
528 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
530 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
533 if (CanConstFold1(exprs[0]))
534 out = (ast_expression*)parser_const_float(parser,
535 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
537 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
540 if (CanConstFold1(exprs[0]))
541 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
543 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
545 /* we don't constant-fold NOT for these types */
547 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
550 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
553 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
554 type_name[exprs[0]->expression.vtype]);
560 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
561 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
563 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
564 type_name[exprs[0]->expression.vtype],
565 type_name[exprs[1]->expression.vtype]);
568 switch (exprs[0]->expression.vtype) {
570 if (CanConstFold(exprs[0], exprs[1]))
572 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
575 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
578 if (CanConstFold(exprs[0], exprs[1]))
579 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
581 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
584 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
585 type_name[exprs[0]->expression.vtype],
586 type_name[exprs[1]->expression.vtype]);
591 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
592 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
594 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
595 type_name[exprs[1]->expression.vtype],
596 type_name[exprs[0]->expression.vtype]);
599 switch (exprs[0]->expression.vtype) {
601 if (CanConstFold(exprs[0], exprs[1]))
602 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
604 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
607 if (CanConstFold(exprs[0], exprs[1]))
608 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
610 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
613 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
614 type_name[exprs[1]->expression.vtype],
615 type_name[exprs[0]->expression.vtype]);
620 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
621 exprs[0]->expression.vtype != TYPE_VECTOR &&
622 exprs[0]->expression.vtype != TYPE_FLOAT &&
623 exprs[1]->expression.vtype != TYPE_VECTOR &&
624 exprs[1]->expression.vtype != TYPE_FLOAT)
626 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
627 type_name[exprs[1]->expression.vtype],
628 type_name[exprs[0]->expression.vtype]);
631 switch (exprs[0]->expression.vtype) {
633 if (exprs[1]->expression.vtype == TYPE_VECTOR)
635 if (CanConstFold(exprs[0], exprs[1]))
636 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
638 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
642 if (CanConstFold(exprs[0], exprs[1]))
643 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
645 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
649 if (exprs[1]->expression.vtype == TYPE_FLOAT)
651 if (CanConstFold(exprs[0], exprs[1]))
652 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
654 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
658 if (CanConstFold(exprs[0], exprs[1]))
659 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
661 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
665 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
666 type_name[exprs[1]->expression.vtype],
667 type_name[exprs[0]->expression.vtype]);
672 if (NotSameType(TYPE_FLOAT)) {
673 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
674 type_name[exprs[0]->expression.vtype],
675 type_name[exprs[1]->expression.vtype]);
678 if (CanConstFold(exprs[0], exprs[1]))
679 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
681 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
685 parseerror(parser, "qc does not have a modulo operator");
689 if (NotSameType(TYPE_FLOAT)) {
690 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
691 type_name[exprs[0]->expression.vtype],
692 type_name[exprs[1]->expression.vtype]);
695 if (CanConstFold(exprs[0], exprs[1]))
696 out = (ast_expression*)parser_const_float(parser,
697 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
698 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
700 out = (ast_expression*)ast_binary_new(ctx,
701 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
705 parseerror(parser, "TODO: bitxor");
710 case opid3('<','<','='):
711 case opid3('>','>','='):
712 parseerror(parser, "TODO: shifts");
716 generated_op += 1; /* INSTR_OR */
718 generated_op += INSTR_AND;
719 if (NotSameType(TYPE_FLOAT)) {
720 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
721 type_name[exprs[0]->expression.vtype],
722 type_name[exprs[1]->expression.vtype]);
723 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
724 parseerror(parser, "TODO: optional early out");
727 if (opts_standard == COMPILER_GMQCC)
728 printf("TODO: early out logic\n");
729 if (CanConstFold(exprs[0], exprs[1]))
730 out = (ast_expression*)parser_const_float(parser,
731 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
733 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
737 generated_op += 1; /* INSTR_GT */
739 generated_op += 1; /* INSTR_LT */
740 case opid2('>', '='):
741 generated_op += 1; /* INSTR_GE */
742 case opid2('<', '='):
743 generated_op += INSTR_LE;
744 if (NotSameType(TYPE_FLOAT)) {
745 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
746 type_name[exprs[0]->expression.vtype],
747 type_name[exprs[1]->expression.vtype]);
750 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
752 case opid2('!', '='):
753 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
754 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
755 type_name[exprs[0]->expression.vtype],
756 type_name[exprs[1]->expression.vtype]);
759 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
761 case opid2('=', '='):
762 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
763 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
764 type_name[exprs[0]->expression.vtype],
765 type_name[exprs[1]->expression.vtype]);
768 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
772 if (ast_istype(exprs[0], ast_entfield)) {
773 ast_expression *field = ((ast_entfield*)exprs[0])->field;
774 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
775 exprs[0]->expression.vtype == TYPE_FIELD &&
776 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
778 assignop = type_storep_instr[TYPE_VECTOR];
781 assignop = type_storep_instr[exprs[0]->expression.vtype];
782 if (!ast_compare_type(field->expression.next, exprs[1])) {
783 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
784 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
785 if (opts_standard == COMPILER_QCC &&
786 field->expression.next->expression.vtype == TYPE_FUNCTION &&
787 exprs[1]->expression.vtype == TYPE_FUNCTION)
789 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
790 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
796 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
801 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
802 exprs[0]->expression.vtype == TYPE_FIELD &&
803 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
805 assignop = type_store_instr[TYPE_VECTOR];
808 assignop = type_store_instr[exprs[0]->expression.vtype];
811 if (assignop == AINSTR_END) {
812 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
813 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
814 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
816 else if (!ast_compare_type(exprs[0], exprs[1])) {
817 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
818 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
819 if (opts_standard == COMPILER_QCC &&
820 exprs[0]->expression.vtype == TYPE_FUNCTION &&
821 exprs[1]->expression.vtype == TYPE_FUNCTION)
823 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
824 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
830 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
833 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
837 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
838 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
840 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
841 type_name[exprs[0]->expression.vtype],
842 type_name[exprs[1]->expression.vtype]);
845 if (ast_istype(exprs[0], ast_entfield))
846 assignop = type_storep_instr[exprs[0]->expression.vtype];
848 assignop = type_store_instr[exprs[0]->expression.vtype];
849 switch (exprs[0]->expression.vtype) {
851 out = (ast_expression*)ast_binstore_new(ctx, assignop,
852 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
856 out = (ast_expression*)ast_binstore_new(ctx, assignop,
857 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
861 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
862 type_name[exprs[0]->expression.vtype],
863 type_name[exprs[1]->expression.vtype]);
871 parseerror(parser, "failed to apply operand %s", op->op);
875 DEBUGSHUNTDO(printf("applied %s\n", op->op));
876 sy->out[sy->out_count++] = syexp(ctx, out);
880 static bool parser_close_call(parser_t *parser, shunt *sy)
882 /* was a function call */
890 fid = sy->ops[sy->ops_count].off;
892 /* out[fid] is the function
893 * everything above is parameters...
895 * 1 params = ast_expression
899 if (sy->out_count < 1 || sy->out_count <= fid) {
900 parseerror(parser, "internal error: function call needs function and parameter list...");
904 fun = sy->out[fid].out;
906 call = ast_call_new(sy->ops[sy->ops_count].ctx, fun);
908 parseerror(parser, "out of memory");
912 if (fid+1 == sy->out_count) {
915 } else if (fid+2 == sy->out_count) {
918 params = sy->out[sy->out_count].block;
922 if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
923 ast_delete(sy->out[sy->out_count].out);
924 parseerror(parser, "out of memory");
928 paramcount = params->exprs_count;
929 MEM_VECTOR_MOVE(params, exprs, call, params);
932 if (!ast_call_check_types(call))
935 parseerror(parser, "invalid function call");
939 /* overwrite fid, the function, with a call */
940 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
942 if (fun->expression.vtype != TYPE_FUNCTION) {
943 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
947 if (!fun->expression.next) {
948 parseerror(parser, "could not determine function return type");
951 if (fun->expression.params_count != paramcount &&
952 !(fun->expression.variadic &&
953 fun->expression.params_count < paramcount))
956 const char *fewmany = (fun->expression.params_count > paramcount) ? "few" : "many";
958 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
959 if (opts_standard == COMPILER_GMQCC)
962 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
963 " -> `%s` has been declared here: %s:%i",
964 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
965 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
967 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
968 " -> `%s` has been declared here: %s:%i",
969 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
970 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
976 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
977 "too %s parameters for call to %s: expected %i, got %i\n"
978 " -> `%s` has been declared here: %s:%i",
979 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
980 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
982 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
983 "too %s parameters for function call: expected %i, got %i\n"
984 " -> `%s` has been declared here: %s:%i",
985 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
986 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
994 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
996 if (!sy->ops_count) {
997 parseerror(parser, "unmatched closing paren");
1000 /* this would for bit a + (x) because there are no operators inside (x)
1001 if (sy->ops[sy->ops_count-1].paren == 1) {
1002 parseerror(parser, "empty parenthesis expression");
1006 while (sy->ops_count) {
1007 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_FUNC) {
1008 if (!parser_close_call(parser, sy))
1012 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_EXPR) {
1014 return !functions_only;
1016 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_INDEX) {
1019 /* pop off the parenthesis */
1021 /* then apply the index operator */
1022 if (!parser_sy_pop(parser, sy))
1026 if (!parser_sy_pop(parser, sy))
1032 static void parser_reclassify_token(parser_t *parser)
1035 for (i = 0; i < operator_count; ++i) {
1036 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1037 parser->tok = TOKEN_OPERATOR;
1043 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1045 ast_expression *expr = NULL;
1047 bool wantop = false;
1048 bool gotmemberof = false;
1050 /* count the parens because an if starts with one, so the
1051 * end of a condition is an unmatched closing paren
1055 MEM_VECTOR_INIT(&sy, out);
1056 MEM_VECTOR_INIT(&sy, ops);
1058 parser->lex->flags.noops = false;
1060 parser_reclassify_token(parser);
1065 gotmemberof = false;
1067 parser->memberof = 0;
1069 if (parser->tok == TOKEN_IDENT)
1071 ast_expression *var;
1073 parseerror(parser, "expected operator or end of statement");
1078 if (opts_standard == COMPILER_GMQCC)
1080 if (parser->memberof == TYPE_ENTITY) {
1081 /* still get vars first since there could be a fieldpointer */
1082 var = parser_find_var(parser, parser_tokval(parser));
1084 var = parser_find_field(parser, parser_tokval(parser));
1086 else if (parser->memberof == TYPE_VECTOR)
1088 parseerror(parser, "TODO: implement effective vector member access");
1091 else if (parser->memberof) {
1092 parseerror(parser, "namespace for member not found");
1096 var = parser_find_var(parser, parser_tokval(parser));
1098 var = parser_find_var(parser, parser_tokval(parser));
1100 var = parser_find_field(parser, parser_tokval(parser));
1103 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1106 if (ast_istype(var, ast_value))
1107 ((ast_value*)var)->uses++;
1108 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), var))) {
1109 parseerror(parser, "out of memory");
1112 DEBUGSHUNTDO(printf("push %s\n", parser_tokval(parser)));
1114 else if (parser->tok == TOKEN_FLOATCONST) {
1117 parseerror(parser, "expected operator or end of statement, got constant");
1121 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1124 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1125 parseerror(parser, "out of memory");
1128 DEBUGSHUNTDO(printf("push %g\n", parser_token(parser)->constval.f));
1130 else if (parser->tok == TOKEN_INTCONST) {
1133 parseerror(parser, "expected operator or end of statement, got constant");
1137 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1140 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1141 parseerror(parser, "out of memory");
1144 DEBUGSHUNTDO(printf("push %i\n", parser_token(parser)->constval.i));
1146 else if (parser->tok == TOKEN_STRINGCONST) {
1149 parseerror(parser, "expected operator or end of statement, got constant");
1153 val = parser_const_string(parser, parser_tokval(parser));
1156 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1157 parseerror(parser, "out of memory");
1160 DEBUGSHUNTDO(printf("push string\n"));
1162 else if (parser->tok == TOKEN_VECTORCONST) {
1165 parseerror(parser, "expected operator or end of statement, got constant");
1169 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1172 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1173 parseerror(parser, "out of memory");
1176 DEBUGSHUNTDO(printf("push '%g %g %g'\n",
1177 parser_token(parser)->constval.v.x,
1178 parser_token(parser)->constval.v.y,
1179 parser_token(parser)->constval.v.z));
1181 else if (parser->tok == '(') {
1182 parseerror(parser, "internal error: '(' should be classified as operator");
1185 else if (parser->tok == '[') {
1186 parseerror(parser, "internal error: '[' should be classified as operator");
1189 else if (parser->tok == ')') {
1191 DEBUGSHUNTDO(printf("do[op] )\n"));
1195 /* we do expect an operator next */
1196 /* closing an opening paren */
1197 if (!parser_close_paren(parser, &sy, false))
1200 DEBUGSHUNTDO(printf("do[nop] )\n"));
1204 /* allowed for function calls */
1205 if (!parser_close_paren(parser, &sy, true))
1210 else if (parser->tok == ']') {
1212 parseerror(parser, "operand expected");
1216 if (!parser_close_paren(parser, &sy, false))
1220 else if (parser->tok != TOKEN_OPERATOR) {
1222 parseerror(parser, "expected operator or end of statement");
1229 /* classify the operator */
1230 /* TODO: suffix operators */
1231 const oper_info *op;
1232 const oper_info *olast = NULL;
1234 for (o = 0; o < operator_count; ++o) {
1235 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1236 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1237 !strcmp(parser_tokval(parser), operators[o].op))
1242 if (o == operator_count) {
1243 /* no operator found... must be the end of the statement */
1246 /* found an operator */
1249 /* when declaring variables, a comma starts a new variable */
1250 if (op->id == opid1(',') && !parens && stopatcomma) {
1251 /* fixup the token */
1256 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1257 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1260 (op->prec < olast->prec) ||
1261 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1263 if (!parser_sy_pop(parser, &sy))
1265 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1266 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1271 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1272 /* for gmqcc standard: open up the namespace of the previous type */
1273 ast_expression *prevex = sy.out[sy.out_count-1].out;
1275 parseerror(parser, "unexpected member operator");
1278 if (prevex->expression.vtype == TYPE_ENTITY)
1279 parser->memberof = TYPE_ENTITY;
1280 else if (prevex->expression.vtype == TYPE_VECTOR)
1281 parser->memberof = TYPE_VECTOR;
1283 parseerror(parser, "type error: type has no members");
1289 if (op->id == opid1('(')) {
1291 DEBUGSHUNTDO(printf("push [op] (\n"));
1293 /* we expected an operator, this is the function-call operator */
1294 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_FUNC, sy.out_count-1))) {
1295 parseerror(parser, "out of memory");
1300 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0))) {
1301 parseerror(parser, "out of memory");
1304 DEBUGSHUNTDO(printf("push [nop] (\n"));
1307 } else if (op->id == opid1('[')) {
1309 parseerror(parser, "unexpected array subscript");
1313 /* push both the operator and the paren, this makes life easier */
1314 if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
1316 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0)))
1320 DEBUGSHUNTDO(printf("push operator %s\n", op->op));
1321 if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
1326 if (!parser_next(parser)) {
1329 if (parser->tok == ';' || (!parens && parser->tok == ']')) {
1334 while (sy.ops_count) {
1335 if (!parser_sy_pop(parser, &sy))
1339 parser->lex->flags.noops = true;
1340 if (!sy.out_count) {
1341 parseerror(parser, "empty expression");
1344 expr = sy.out[0].out;
1345 MEM_VECTOR_CLEAR(&sy, out);
1346 MEM_VECTOR_CLEAR(&sy, ops);
1347 DEBUGSHUNTDO(printf("shunt done\n"));
1351 parser->lex->flags.noops = true;
1352 MEM_VECTOR_CLEAR(&sy, out);
1353 MEM_VECTOR_CLEAR(&sy, ops);
1357 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1359 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1362 if (!parser_next(parser)) {
1369 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1372 ast_expression *cond, *ontrue, *onfalse = NULL;
1374 lex_ctx ctx = parser_ctx(parser);
1376 /* skip the 'if' and check for opening paren */
1377 if (!parser_next(parser) || parser->tok != '(') {
1378 parseerror(parser, "expected 'if' condition in parenthesis");
1381 /* parse into the expression */
1382 if (!parser_next(parser)) {
1383 parseerror(parser, "expected 'if' condition after opening paren");
1386 /* parse the condition */
1387 cond = parse_expression_leave(parser, false);
1391 if (parser->tok != ')') {
1392 parseerror(parser, "expected closing paren after 'if' condition");
1396 /* parse into the 'then' branch */
1397 if (!parser_next(parser)) {
1398 parseerror(parser, "expected statement for on-true branch of 'if'");
1402 ontrue = parse_statement_or_block(parser);
1407 /* check for an else */
1408 if (!strcmp(parser_tokval(parser), "else")) {
1409 /* parse into the 'else' branch */
1410 if (!parser_next(parser)) {
1411 parseerror(parser, "expected on-false branch after 'else'");
1416 onfalse = parse_statement_or_block(parser);
1424 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1425 *out = (ast_expression*)ifthen;
1429 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1432 ast_expression *cond, *ontrue;
1434 lex_ctx ctx = parser_ctx(parser);
1436 /* skip the 'while' and check for opening paren */
1437 if (!parser_next(parser) || parser->tok != '(') {
1438 parseerror(parser, "expected 'while' condition in parenthesis");
1441 /* parse into the expression */
1442 if (!parser_next(parser)) {
1443 parseerror(parser, "expected 'while' condition after opening paren");
1446 /* parse the condition */
1447 cond = parse_expression_leave(parser, false);
1451 if (parser->tok != ')') {
1452 parseerror(parser, "expected closing paren after 'while' condition");
1456 /* parse into the 'then' branch */
1457 if (!parser_next(parser)) {
1458 parseerror(parser, "expected while-loop body");
1462 ontrue = parse_statement_or_block(parser);
1468 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1469 *out = (ast_expression*)aloop;
1473 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1476 ast_expression *cond, *ontrue;
1478 lex_ctx ctx = parser_ctx(parser);
1480 /* skip the 'do' and get the body */
1481 if (!parser_next(parser)) {
1482 parseerror(parser, "expected loop body");
1485 ontrue = parse_statement_or_block(parser);
1489 /* expect the "while" */
1490 if (parser->tok != TOKEN_KEYWORD ||
1491 strcmp(parser_tokval(parser), "while"))
1493 parseerror(parser, "expected 'while' and condition");
1498 /* skip the 'while' and check for opening paren */
1499 if (!parser_next(parser) || parser->tok != '(') {
1500 parseerror(parser, "expected 'while' condition in parenthesis");
1504 /* parse into the expression */
1505 if (!parser_next(parser)) {
1506 parseerror(parser, "expected 'while' condition after opening paren");
1510 /* parse the condition */
1511 cond = parse_expression_leave(parser, false);
1515 if (parser->tok != ')') {
1516 parseerror(parser, "expected closing paren after 'while' condition");
1522 if (!parser_next(parser) || parser->tok != ';') {
1523 parseerror(parser, "expected semicolon after condition");
1529 if (!parser_next(parser)) {
1530 parseerror(parser, "parse error");
1536 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1537 *out = (ast_expression*)aloop;
1541 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1544 ast_expression *initexpr, *cond, *increment, *ontrue;
1545 size_t oldblocklocal;
1548 lex_ctx ctx = parser_ctx(parser);
1550 oldblocklocal = parser->blocklocal;
1551 parser->blocklocal = parser->locals_count;
1558 /* skip the 'while' and check for opening paren */
1559 if (!parser_next(parser) || parser->tok != '(') {
1560 parseerror(parser, "expected 'for' expressions in parenthesis");
1563 /* parse into the expression */
1564 if (!parser_next(parser)) {
1565 parseerror(parser, "expected 'for' initializer after opening paren");
1569 if (parser->tok == TOKEN_TYPENAME) {
1570 if (opts_standard != COMPILER_GMQCC) {
1571 if (parsewarning(parser, WARN_EXTENSIONS,
1572 "current standard does not allow variable declarations in for-loop initializers"))
1576 parseerror(parser, "TODO: assignment of new variables to be non-const");
1578 if (!parse_variable(parser, block, true))
1581 else if (parser->tok != ';')
1583 initexpr = parse_expression_leave(parser, false);
1588 /* move on to condition */
1589 if (parser->tok != ';') {
1590 parseerror(parser, "expected semicolon after for-loop initializer");
1593 if (!parser_next(parser)) {
1594 parseerror(parser, "expected for-loop condition");
1598 /* parse the condition */
1599 if (parser->tok != ';') {
1600 cond = parse_expression_leave(parser, false);
1605 /* move on to incrementor */
1606 if (parser->tok != ';') {
1607 parseerror(parser, "expected semicolon after for-loop initializer");
1610 if (!parser_next(parser)) {
1611 parseerror(parser, "expected for-loop condition");
1615 /* parse the incrementor */
1616 if (parser->tok != ')') {
1617 increment = parse_expression_leave(parser, false);
1620 if (!ast_istype(increment, ast_store) &&
1621 !ast_istype(increment, ast_call) &&
1622 !ast_istype(increment, ast_binstore))
1624 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1630 if (parser->tok != ')') {
1631 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1634 /* parse into the 'then' branch */
1635 if (!parser_next(parser)) {
1636 parseerror(parser, "expected for-loop body");
1639 ontrue = parse_statement_or_block(parser);
1644 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1645 *out = (ast_expression*)aloop;
1647 while (parser->locals_count > parser->blocklocal)
1648 retval = retval && parser_pop_local(parser);
1649 parser->blocklocal = oldblocklocal;
1652 if (initexpr) ast_delete(initexpr);
1653 if (cond) ast_delete(cond);
1654 if (increment) ast_delete(increment);
1655 while (parser->locals_count > parser->blocklocal)
1656 (void)!parser_pop_local(parser);
1657 parser->blocklocal = oldblocklocal;
1661 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
1663 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
1665 /* local variable */
1667 parseerror(parser, "cannot declare a variable from here");
1670 if (opts_standard == COMPILER_QCC) {
1671 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1674 if (!parse_variable(parser, block, false))
1679 else if (parser->tok == TOKEN_KEYWORD)
1681 if (!strcmp(parser_tokval(parser), "local"))
1684 parseerror(parser, "cannot declare a local variable here");
1687 if (!parser_next(parser)) {
1688 parseerror(parser, "expected variable declaration");
1691 if (!parse_variable(parser, block, true))
1696 else if (!strcmp(parser_tokval(parser), "return"))
1698 ast_expression *exp = NULL;
1699 ast_return *ret = NULL;
1700 ast_value *expected = parser->function->vtype;
1702 if (!parser_next(parser)) {
1703 parseerror(parser, "expected return expression");
1707 if (parser->tok != ';') {
1708 exp = parse_expression(parser, false);
1712 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1713 parseerror(parser, "return with invalid expression");
1716 ret = ast_return_new(exp->expression.node.context, exp);
1722 if (!parser_next(parser))
1723 parseerror(parser, "parse error");
1724 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1725 if (opts_standard != COMPILER_GMQCC)
1726 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1728 parseerror(parser, "return without value");
1730 ret = ast_return_new(parser_ctx(parser), NULL);
1732 *out = (ast_expression*)ret;
1735 else if (!strcmp(parser_tokval(parser), "if"))
1737 return parse_if(parser, block, out);
1739 else if (!strcmp(parser_tokval(parser), "while"))
1741 return parse_while(parser, block, out);
1743 else if (!strcmp(parser_tokval(parser), "do"))
1745 return parse_dowhile(parser, block, out);
1747 else if (!strcmp(parser_tokval(parser), "for"))
1749 if (opts_standard == COMPILER_QCC) {
1750 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1753 return parse_for(parser, block, out);
1755 parseerror(parser, "Unexpected keyword");
1758 else if (parser->tok == '{')
1761 inner = parse_block(parser, false);
1764 *out = (ast_expression*)inner;
1769 ast_expression *exp = parse_expression(parser, false);
1773 if (!ast_istype(exp, ast_store) &&
1774 !ast_istype(exp, ast_call) &&
1775 !ast_istype(exp, ast_binstore))
1777 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1784 static bool GMQCC_WARN parser_pop_local(parser_t *parser)
1787 parser->locals_count--;
1789 ve = &parser->locals[parser->locals_count];
1790 if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
1791 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
1794 mem_d(parser->locals[parser->locals_count].name);
1798 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
1800 size_t oldblocklocal;
1803 oldblocklocal = parser->blocklocal;
1804 parser->blocklocal = parser->locals_count;
1806 if (!parser_next(parser)) { /* skip the '{' */
1807 parseerror(parser, "expected function body");
1811 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
1813 ast_expression *expr;
1814 if (parser->tok == '}')
1817 if (!parse_statement(parser, block, &expr)) {
1818 /* parseerror(parser, "parse error"); */
1824 if (!ast_block_exprs_add(block, expr)) {
1831 if (parser->tok != '}') {
1834 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
1836 if (!block->exprs_count ||
1837 !ast_istype(block->exprs[block->exprs_count-1], ast_return))
1839 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
1845 (void)parser_next(parser);
1849 while (parser->locals_count > parser->blocklocal)
1850 retval = retval && parser_pop_local(parser);
1851 parser->blocklocal = oldblocklocal;
1855 static ast_block* parse_block(parser_t *parser, bool warnreturn)
1858 block = ast_block_new(parser_ctx(parser));
1861 if (!parse_block_into(parser, block, warnreturn)) {
1862 ast_block_delete(block);
1868 static ast_expression* parse_statement_or_block(parser_t *parser)
1870 ast_expression *expr = NULL;
1871 if (parser->tok == '{')
1872 return (ast_expression*)parse_block(parser, false);
1873 if (!parse_statement(parser, NULL, &expr))
1879 static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
1882 size_t len = strlen(var->name);
1884 for (i = 0; i < 3; ++i) {
1885 ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
1889 ve[i].name = (char*)mem_a(len+3);
1891 ast_delete(ve[i].var);
1895 memcpy(ve[i].name, var->name, len);
1896 ve[i].name[len] = '_';
1897 ve[i].name[len+1] = 'x'+i;
1898 ve[i].name[len+2] = 0;
1907 ast_delete(ve[i].var);
1914 static bool parse_function_body(parser_t *parser, ast_value *var)
1916 ast_block *block = NULL;
1921 ast_expression *framenum = NULL;
1922 ast_expression *nextthink = NULL;
1923 /* None of the following have to be deleted */
1924 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
1925 ast_expression *gbl_time = NULL, *gbl_self = NULL;
1926 bool has_frame_think;
1930 has_frame_think = false;
1931 old = parser->function;
1933 if (var->expression.variadic) {
1934 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
1935 "variadic function with implementation will not be able to access additional parameters"))
1941 if (parser->tok == '[') {
1942 /* got a frame definition: [ framenum, nextthink ]
1943 * this translates to:
1944 * self.frame = framenum;
1945 * self.nextthink = time + 0.1;
1946 * self.think = nextthink;
1950 fld_think = parser_find_field(parser, "think");
1951 fld_nextthink = parser_find_field(parser, "nextthink");
1952 fld_frame = parser_find_field(parser, "frame");
1953 if (!fld_think || !fld_nextthink || !fld_frame) {
1954 parseerror(parser, "cannot use [frame,think] notation without the required fields");
1955 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
1958 gbl_time = parser_find_global(parser, "time");
1959 gbl_self = parser_find_global(parser, "self");
1960 if (!gbl_time || !gbl_self) {
1961 parseerror(parser, "cannot use [frame,think] notation without the required globals");
1962 parseerror(parser, "please declare the following globals: `time`, `self`");
1966 if (!parser_next(parser))
1969 framenum = parse_expression_leave(parser, true);
1971 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
1974 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
1975 ast_unref(framenum);
1976 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
1980 if (parser->tok != ',') {
1981 ast_unref(framenum);
1982 parseerror(parser, "expected comma after frame number in [frame,think] notation");
1983 parseerror(parser, "Got a %i\n", parser->tok);
1987 if (!parser_next(parser)) {
1988 ast_unref(framenum);
1992 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
1994 /* qc allows the use of not-yet-declared functions here
1995 * - this automatically creates a prototype */
1997 ast_value *thinkfunc;
1998 ast_expression *functype = fld_think->expression.next;
2000 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2001 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2002 ast_unref(framenum);
2003 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2007 if (!parser_next(parser)) {
2008 ast_unref(framenum);
2009 ast_delete(thinkfunc);
2013 varent.var = (ast_expression*)thinkfunc;
2014 varent.name = util_strdup(thinkfunc->name);
2015 if (!parser_t_globals_add(parser, varent)) {
2016 ast_unref(framenum);
2017 ast_delete(thinkfunc);
2020 nextthink = (ast_expression*)thinkfunc;
2023 nextthink = parse_expression_leave(parser, true);
2025 ast_unref(framenum);
2026 parseerror(parser, "expected a think-function in [frame,think] notation");
2031 if (!ast_istype(nextthink, ast_value)) {
2032 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2036 if (retval && parser->tok != ']') {
2037 parseerror(parser, "expected closing `]` for [frame,think] notation");
2041 if (retval && !parser_next(parser)) {
2045 if (retval && parser->tok != '{') {
2046 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2051 ast_unref(nextthink);
2052 ast_unref(framenum);
2056 has_frame_think = true;
2059 block = ast_block_new(parser_ctx(parser));
2061 parseerror(parser, "failed to allocate block");
2062 if (has_frame_think) {
2063 ast_unref(nextthink);
2064 ast_unref(framenum);
2069 if (has_frame_think) {
2071 ast_expression *self_frame;
2072 ast_expression *self_nextthink;
2073 ast_expression *self_think;
2074 ast_expression *time_plus_1;
2075 ast_store *store_frame;
2076 ast_store *store_nextthink;
2077 ast_store *store_think;
2079 ctx = parser_ctx(parser);
2080 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2081 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2082 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2084 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2085 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2087 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2088 if (self_frame) ast_delete(self_frame);
2089 if (self_nextthink) ast_delete(self_nextthink);
2090 if (self_think) ast_delete(self_think);
2091 if (time_plus_1) ast_delete(time_plus_1);
2097 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2098 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2099 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2102 ast_delete(self_frame);
2105 if (!store_nextthink) {
2106 ast_delete(self_nextthink);
2110 ast_delete(self_think);
2114 if (store_frame) ast_delete(store_frame);
2115 if (store_nextthink) ast_delete(store_nextthink);
2116 if (store_think) ast_delete(store_think);
2119 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_frame)) {
2120 ast_delete(store_frame);
2121 ast_delete(store_nextthink);
2122 ast_delete(store_think);
2126 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_nextthink)) {
2127 ast_delete(store_nextthink);
2128 ast_delete(store_think);
2132 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_think) )
2134 ast_delete(store_think);
2140 parseerror(parser, "failed to generate code for [frame,think]");
2141 ast_unref(nextthink);
2142 ast_unref(framenum);
2148 for (parami = 0; parami < var->expression.params_count; ++parami) {
2151 ast_value *param = var->expression.params[parami];
2153 if (param->expression.vtype != TYPE_VECTOR &&
2154 (param->expression.vtype != TYPE_FIELD ||
2155 param->expression.next->expression.vtype != TYPE_VECTOR))
2160 if (!create_vector_members(parser, param, ve)) {
2161 ast_block_delete(block);
2165 for (e = 0; e < 3; ++e) {
2166 if (!parser_t_locals_add(parser, ve[e]))
2168 if (!ast_block_collect(block, ve[e].var)) {
2169 parser->locals_count--;
2172 ve[e].var = NULL; /* collected */
2175 parser->locals -= e;
2180 ast_block_delete(block);
2185 func = ast_function_new(ast_ctx(var), var->name, var);
2187 parseerror(parser, "failed to allocate function for `%s`", var->name);
2188 ast_block_delete(block);
2191 if (!parser_t_functions_add(parser, func)) {
2192 parseerror(parser, "failed to allocate slot for function `%s`", var->name);
2193 ast_block_delete(block);
2197 parser->function = func;
2198 if (!parse_block_into(parser, block, true)) {
2199 ast_block_delete(block);
2203 if (!ast_function_blocks_add(func, block)) {
2204 ast_block_delete(block);
2208 parser->function = old;
2209 while (parser->locals_count)
2210 retval = retval && parser_pop_local(parser);
2212 if (parser->tok == ';')
2213 return parser_next(parser);
2214 else if (opts_standard == COMPILER_QCC)
2215 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2219 parser->functions_count--;
2221 ast_function_delete(func);
2222 var->constval.vfunc = NULL;
2225 while (parser->locals_count) {
2226 parser->locals_count--;
2227 mem_d(parser->locals[parser->locals_count].name);
2229 parser->function = old;
2233 static ast_expression *array_accessor_split(
2238 ast_expression *left,
2239 ast_expression *right
2245 lex_ctx ctx = ast_ctx(array);
2247 if (!left || !right) {
2248 if (left) ast_delete(left);
2249 if (right) ast_delete(right);
2253 cmp = ast_binary_new(ctx, INSTR_LT,
2254 (ast_expression*)index,
2255 (ast_expression*)parser_const_float(parser, middle));
2259 parseerror(parser, "internal error: failed to create comparison for array setter");
2263 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2265 ast_delete(cmp); /* will delete left and right */
2266 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2270 return (ast_expression*)ifthen;
2273 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2275 lex_ctx ctx = ast_ctx(array);
2277 if (from+1 == afterend) {
2281 ast_array_index *subscript;
2282 int assignop = type_store_instr[value->expression.vtype];
2284 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2285 assignop = INSTR_STORE_V;
2287 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2291 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2293 ast_delete(subscript);
2297 block = ast_block_new(ctx);
2303 if (!ast_block_exprs_add(block, (ast_expression*)st)) {
2308 ret = ast_return_new(ctx, NULL);
2314 if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
2319 return (ast_expression*)block;
2321 ast_expression *left, *right;
2322 size_t diff = afterend - from;
2323 size_t middle = from + diff/2;
2324 left = array_setter_node(parser, array, index, value, from, middle);
2325 right = array_setter_node(parser, array, index, value, middle, afterend);
2326 return array_accessor_split(parser, array, index, middle, left, right);
2330 static ast_expression *array_field_setter_node(
2339 lex_ctx ctx = ast_ctx(array);
2341 if (from+1 == afterend) {
2345 ast_entfield *entfield;
2346 ast_array_index *subscript;
2347 int assignop = type_store_instr[value->expression.vtype];
2349 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2350 assignop = INSTR_STORE_V;
2352 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2356 entfield = ast_entfield_new_force(ctx,
2357 (ast_expression*)entity,
2358 (ast_expression*)subscript,
2359 (ast_expression*)subscript);
2361 ast_delete(subscript);
2365 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2367 ast_delete(entfield);
2371 block = ast_block_new(ctx);
2377 if (!ast_block_exprs_add(block, (ast_expression*)st)) {
2382 ret = ast_return_new(ctx, NULL);
2388 if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
2393 return (ast_expression*)block;
2395 ast_expression *left, *right;
2396 size_t diff = afterend - from;
2397 size_t middle = from + diff/2;
2398 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2399 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2400 return array_accessor_split(parser, array, index, middle, left, right);
2404 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2406 lex_ctx ctx = ast_ctx(array);
2408 if (from+1 == afterend) {
2410 ast_array_index *subscript;
2412 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2416 ret = ast_return_new(ctx, (ast_expression*)subscript);
2418 ast_delete(subscript);
2422 return (ast_expression*)ret;
2424 ast_expression *left, *right;
2425 size_t diff = afterend - from;
2426 size_t middle = from + diff/2;
2427 left = array_getter_node(parser, array, index, from, middle);
2428 right = array_getter_node(parser, array, index, middle, afterend);
2429 return array_accessor_split(parser, array, index, middle, left, right);
2433 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2435 ast_function *func = NULL;
2436 ast_value *fval = NULL;
2438 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2440 parseerror(parser, "failed to create accessor function value");
2444 func = ast_function_new(ast_ctx(array), funcname, fval);
2447 parseerror(parser, "failed to create accessor function node");
2456 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2458 ast_expression *root = NULL;
2459 ast_block *body = NULL;
2460 ast_value *index = NULL;
2461 ast_value *value = NULL;
2465 if (!ast_istype(array->expression.next, ast_value)) {
2466 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2470 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2472 func = fval->constval.vfunc;
2473 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2475 body = ast_block_new(ast_ctx(array));
2477 parseerror(parser, "failed to create block for array accessor");
2481 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2482 value = ast_value_copy((ast_value*)array->expression.next);
2484 if (!index || !value) {
2485 parseerror(parser, "failed to create locals for array accessor");
2488 (void)!ast_value_set_name(value, "value"); /* not important */
2489 (void)!ast_expression_common_params_add(&fval->expression, index);
2490 (void)!ast_expression_common_params_add(&fval->expression, value);
2492 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2494 parseerror(parser, "failed to build accessor search tree");
2498 (void)!ast_block_exprs_add(body, root);
2499 (void)!ast_function_blocks_add(func, body);
2500 array->setter = fval;
2503 if (body) ast_delete(body);
2504 if (index) ast_delete(index);
2505 if (value) ast_delete(value);
2506 if (root) ast_delete(root);
2512 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2514 ast_expression *root = NULL;
2515 ast_block *body = NULL;
2516 ast_value *entity = NULL;
2517 ast_value *index = NULL;
2518 ast_value *value = NULL;
2522 if (!ast_istype(array->expression.next, ast_value)) {
2523 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2527 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2529 func = fval->constval.vfunc;
2530 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2532 body = ast_block_new(ast_ctx(array));
2534 parseerror(parser, "failed to create block for array accessor");
2538 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2539 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2540 value = ast_value_copy((ast_value*)array->expression.next);
2541 if (!entity || !index || !value) {
2542 parseerror(parser, "failed to create locals for array accessor");
2545 (void)!ast_value_set_name(value, "value"); /* not important */
2546 (void)!ast_expression_common_params_add(&fval->expression, entity);
2547 (void)!ast_expression_common_params_add(&fval->expression, index);
2548 (void)!ast_expression_common_params_add(&fval->expression, value);
2550 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2552 parseerror(parser, "failed to build accessor search tree");
2556 (void)!ast_block_exprs_add(body, root);
2557 (void)!ast_function_blocks_add(func, body);
2558 array->setter = fval;
2561 if (body) ast_delete(body);
2562 if (entity) ast_delete(entity);
2563 if (index) ast_delete(index);
2564 if (value) ast_delete(value);
2565 if (root) ast_delete(root);
2571 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2573 ast_expression *root = NULL;
2574 ast_block *body = NULL;
2575 ast_value *index = NULL;
2579 /* NOTE: checking array->expression.next rather than elemtype since
2580 * for fields elemtype is a temporary fieldtype.
2582 if (!ast_istype(array->expression.next, ast_value)) {
2583 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2587 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2589 func = fval->constval.vfunc;
2590 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2592 body = ast_block_new(ast_ctx(array));
2594 parseerror(parser, "failed to create block for array accessor");
2598 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2601 parseerror(parser, "failed to create locals for array accessor");
2604 (void)!ast_expression_common_params_add(&fval->expression, index);
2606 root = array_getter_node(parser, array, index, 0, array->expression.count);
2608 parseerror(parser, "failed to build accessor search tree");
2612 (void)!ast_block_exprs_add(body, root);
2613 (void)!ast_function_blocks_add(func, body);
2614 array->getter = fval;
2617 if (body) ast_delete(body);
2618 if (index) ast_delete(index);
2619 if (root) ast_delete(root);
2626 MEM_VECTOR_MAKE(ast_value*, p);
2628 MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
2630 static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
2631 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
2639 bool variadic = false;
2641 ctx = parser_ctx(parser);
2643 /* for the sake of less code we parse-in in this function */
2644 if (!parser_next(parser)) {
2645 parseerror(parser, "expected parameter list");
2649 MEM_VECTOR_INIT(¶ms, p);
2651 /* parse variables until we hit a closing paren */
2652 while (parser->tok != ')') {
2654 /* there must be commas between them */
2655 if (parser->tok != ',') {
2656 parseerror(parser, "expected comma or end of parameter list");
2659 if (!parser_next(parser)) {
2660 parseerror(parser, "expected parameter");
2666 if (parser->tok == TOKEN_DOTS) {
2667 /* '...' indicates a varargs function */
2669 if (!parser_next(parser)) {
2670 parseerror(parser, "expected parameter");
2673 if (parser->tok != ')') {
2674 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
2680 /* for anything else just parse a typename */
2681 param = parse_typename(parser, NULL);
2684 if (!paramlist_t_p_add(¶ms, param)) {
2688 if (param->expression.vtype >= TYPE_VARIANT) {
2689 char typename[1024];
2690 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
2691 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
2698 if (params.p_count > 8)
2699 parseerror(parser, "more than 8 parameters are currently not supported");
2702 if (!parser_next(parser)) {
2703 parseerror(parser, "parse error after typename");
2707 /* now turn 'var' into a function type */
2708 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
2709 fval->expression.next = (ast_expression*)var;
2710 fval->expression.variadic = variadic;
2713 MEM_VECTOR_MOVE(¶ms, p, &var->expression, params);
2719 for (i = 0; i < params.p_count; ++i)
2720 ast_delete(params.p[i]);
2721 MEM_VECTOR_CLEAR(¶ms, p);
2725 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
2727 ast_expression *cexp;
2728 ast_value *cval, *tmp;
2731 ctx = parser_ctx(parser);
2733 if (!parser_next(parser)) {
2735 parseerror(parser, "expected array-size");
2739 cexp = parse_expression_leave(parser, true);
2741 if (!cexp || !ast_istype(cexp, ast_value)) {
2745 parseerror(parser, "expected array-size as constant positive integer");
2748 cval = (ast_value*)cexp;
2750 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
2751 tmp->expression.next = (ast_expression*)var;
2754 if (cval->expression.vtype == TYPE_INTEGER)
2755 tmp->expression.count = cval->constval.vint;
2756 else if (cval->expression.vtype == TYPE_FLOAT)
2757 tmp->expression.count = cval->constval.vfloat;
2761 parseerror(parser, "array-size must be a positive integer constant");
2766 if (parser->tok != ']') {
2768 parseerror(parser, "expected ']' after array-size");
2771 if (!parser_next(parser)) {
2773 parseerror(parser, "error after parsing array size");
2779 /* Parse a complete typename.
2780 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
2781 * but when parsing variables separated by comma
2782 * 'storebase' should point to where the base-type should be kept.
2783 * The base type makes up every bit of type information which comes *before* the
2786 * The following will be parsed in its entirety:
2788 * The 'basetype' in this case is 'void()'
2789 * and if there's a comma after it, say:
2791 * then the type-information 'void()' can be stored in 'storebase'
2793 static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
2795 ast_value *var, *tmp;
2798 const char *name = NULL;
2799 bool isfield = false;
2800 bool wasarray = false;
2802 ctx = parser_ctx(parser);
2804 /* types may start with a dot */
2805 if (parser->tok == '.') {
2807 /* if we parsed a dot we need a typename now */
2808 if (!parser_next(parser)) {
2809 parseerror(parser, "expected typename for field definition");
2812 if (parser->tok != TOKEN_TYPENAME) {
2813 parseerror(parser, "expected typename");
2818 /* generate the basic type value */
2819 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
2820 /* do not yet turn into a field - remember:
2821 * .void() foo; is a field too
2822 * .void()() foo; is a function
2826 if (!parser_next(parser)) {
2828 parseerror(parser, "parse error after typename");
2832 /* an opening paren now starts the parameter-list of a function
2833 * this is where original-QC has parameter lists.
2834 * We allow a single parameter list here.
2835 * Much like fteqcc we don't allow `float()() x`
2837 if (parser->tok == '(') {
2838 var = parse_parameter_list(parser, var);
2843 /* store the base if requested */
2845 *storebase = ast_value_copy(var);
2847 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2848 tmp->expression.next = (ast_expression*)*storebase;
2853 /* there may be a name now */
2854 if (parser->tok == TOKEN_IDENT) {
2855 name = util_strdup(parser_tokval(parser));
2857 if (!parser_next(parser)) {
2859 parseerror(parser, "error after variable or field declaration");
2864 /* now this may be an array */
2865 if (parser->tok == '[') {
2867 var = parse_arraysize(parser, var);
2872 /* This is the point where we can turn it into a field */
2874 /* turn it into a field if desired */
2875 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2876 tmp->expression.next = (ast_expression*)var;
2880 /* now there may be function parens again */
2881 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
2882 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2883 if (parser->tok == '(' && wasarray)
2884 parseerror(parser, "arrays as part of a return type is not supported");
2885 while (parser->tok == '(') {
2886 var = parse_parameter_list(parser, var);
2895 /* finally name it */
2897 if (!ast_value_set_name(var, name)) {
2899 parseerror(parser, "internal error: failed to set name");
2902 /* free the name, ast_value_set_name duplicates */
2909 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
2913 ast_expression *old;
2917 ast_value *basetype = NULL;
2919 bool isparam = false;
2920 bool isvector = false;
2921 bool cleanvar = true;
2922 bool wasarray = false;
2924 varentry_t varent, ve[3];
2926 /* get the first complete variable */
2927 var = parse_typename(parser, &basetype);
2930 ast_delete(basetype);
2934 memset(&varent, 0, sizeof(varent));
2935 memset(&ve, 0, sizeof(ve));
2941 /* Part 0: finish the type */
2942 if (parser->tok == '(') {
2943 if (opts_standard == COMPILER_QCC)
2944 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2945 var = parse_parameter_list(parser, var);
2951 /* we only allow 1-dimensional arrays */
2952 if (parser->tok == '[') {
2954 var = parse_arraysize(parser, var);
2960 if (parser->tok == '(' && wasarray) {
2961 parseerror(parser, "arrays as part of a return type is not supported");
2962 /* we'll still parse the type completely for now */
2964 /* for functions returning functions */
2965 while (parser->tok == '(') {
2966 if (opts_standard == COMPILER_QCC)
2967 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2968 var = parse_parameter_list(parser, var);
2976 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
2977 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
2978 * is then filled with the previous definition and the parameter-names replaced.
2981 /* Deal with end_sys_ vars */
2983 if (!strcmp(var->name, "end_sys_globals")) {
2984 parser->crc_globals = parser->globals_count;
2987 else if (!strcmp(var->name, "end_sys_fields")) {
2988 parser->crc_fields = parser->fields_count;
2991 if (was_end && var->expression.vtype == TYPE_FIELD) {
2992 if (parsewarning(parser, WARN_END_SYS_FIELDS,
2993 "global '%s' hint should not be a field",
2994 parser_tokval(parser)))
3001 if (!nofields && var->expression.vtype == TYPE_FIELD)
3003 /* deal with field declarations */
3004 old = parser_find_field(parser, var->name);
3006 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3007 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3016 parseerror(parser, "field `%s` already declared here: %s:%i",
3017 var->name, ast_ctx(old).file, ast_ctx(old).line);
3022 if (opts_standard == COMPILER_QCC &&
3023 (old = parser_find_global(parser, var->name)))
3025 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3026 parseerror(parser, "field `%s` already declared here: %s:%i",
3027 var->name, ast_ctx(old).file, ast_ctx(old).line);
3034 /* deal with other globals */
3035 old = parser_find_global(parser, var->name);
3036 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3038 /* This is a function which had a prototype */
3039 if (!ast_istype(old, ast_value)) {
3040 parseerror(parser, "internal error: prototype is not an ast_value");
3044 proto = (ast_value*)old;
3045 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3046 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3048 ast_ctx(proto).file, ast_ctx(proto).line);
3052 /* we need the new parameter-names */
3053 for (i = 0; i < proto->expression.params_count; ++i)
3054 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3062 parseerror(parser, "global `%s` already declared here: %s:%i",
3063 var->name, ast_ctx(old).file, ast_ctx(old).line);
3067 if (opts_standard == COMPILER_QCC &&
3068 (old = parser_find_field(parser, var->name)))
3070 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3071 parseerror(parser, "global `%s` already declared here: %s:%i",
3072 var->name, ast_ctx(old).file, ast_ctx(old).line);
3079 else /* it's not a global */
3081 old = parser_find_local(parser, var->name, parser->blocklocal, &isparam);
3082 if (old && !isparam) {
3083 parseerror(parser, "local `%s` already declared here: %s:%i",
3084 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3088 old = parser_find_local(parser, var->name, 0, &isparam);
3089 if (old && isparam) {
3090 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3091 "local `%s` is shadowing a parameter", var->name))
3093 parseerror(parser, "local `%s` already declared here: %s:%i",
3094 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3098 if (opts_standard != COMPILER_GMQCC) {
3107 * Create the global/local, and deal with vector types.
3110 if (var->expression.vtype == TYPE_VECTOR)
3112 else if (var->expression.vtype == TYPE_FIELD &&
3113 var->expression.next->expression.vtype == TYPE_VECTOR)
3117 if (!create_vector_members(parser, var, ve)) {
3123 varent.name = util_strdup(var->name);
3124 varent.var = (ast_expression*)var;
3127 /* deal with global variables, fields, functions */
3128 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3129 if (!(retval = parser_t_fields_add(parser, varent)))
3132 for (i = 0; i < 3; ++i) {
3133 if (!(retval = parser_t_fields_add(parser, ve[i])))
3137 parser->fields_count -= i+1;
3143 if (!(retval = parser_t_globals_add(parser, varent)))
3146 for (i = 0; i < 3; ++i) {
3147 if (!(retval = parser_t_globals_add(parser, ve[i])))
3151 parser->globals_count -= i+1;
3157 if (!(retval = parser_t_locals_add(parser, varent)))
3159 if (!(retval = ast_block_locals_add(localblock, var))) {
3160 parser->locals_count--;
3164 for (i = 0; i < 3; ++i) {
3165 if (!(retval = parser_t_locals_add(parser, ve[i])))
3167 if (!(retval = ast_block_collect(localblock, ve[i].var)))
3169 ve[i].var = NULL; /* from here it's being collected in the block */
3172 parser->locals_count -= i+1;
3173 localblock->locals_count--;
3180 ve[0].name = ve[1].name = ve[2].name = NULL;
3181 ve[0].var = ve[1].var = ve[2].var = NULL;
3187 if (var->expression.vtype == TYPE_ARRAY) {
3189 snprintf(name, sizeof(name), "%s##SET", var->name);
3190 if (!parser_create_array_setter(parser, var, name))
3192 snprintf(name, sizeof(name), "%s##GET", var->name);
3193 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3196 else if (!localblock && !nofields &&
3197 var->expression.vtype == TYPE_FIELD &&
3198 var->expression.next->expression.vtype == TYPE_ARRAY)
3201 ast_expression *telem;
3203 ast_value *array = (ast_value*)var->expression.next;
3205 if (!ast_istype(var->expression.next, ast_value)) {
3206 parseerror(parser, "internal error: field element type must be an ast_value");
3210 snprintf(name, sizeof(name), "%s##SETF", var->name);
3211 if (!parser_create_array_field_setter(parser, array, name))
3214 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3215 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3216 tfield->expression.next = telem;
3217 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3218 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3226 if (parser->tok == ';') {
3227 ast_delete(basetype);
3228 if (!parser_next(parser)) {
3229 parseerror(parser, "error after variable declaration");
3235 if (parser->tok == ',')
3238 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3239 parseerror(parser, "missing comma or semicolon while parsing variables");
3243 if (localblock && opts_standard == COMPILER_QCC) {
3244 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3245 "initializing expression turns variable `%s` into a constant in this standard",
3252 if (parser->tok != '{') {
3253 if (parser->tok != '=') {
3254 parseerror(parser, "missing semicolon or initializer");
3258 if (!parser_next(parser)) {
3259 parseerror(parser, "error parsing initializer");
3263 else if (opts_standard == COMPILER_QCC) {
3264 parseerror(parser, "expected '=' before function body in this standard");
3267 if (parser->tok == '#') {
3271 parseerror(parser, "cannot declare builtins within functions");
3274 if (var->expression.vtype != TYPE_FUNCTION) {
3275 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3278 if (!parser_next(parser)) {
3279 parseerror(parser, "expected builtin number");
3282 if (parser->tok != TOKEN_INTCONST) {
3283 parseerror(parser, "builtin number must be an integer constant");
3286 if (parser_token(parser)->constval.i <= 0) {
3287 parseerror(parser, "builtin number must be an integer greater than zero");
3291 func = ast_function_new(ast_ctx(var), var->name, var);
3293 parseerror(parser, "failed to allocate function for `%s`", var->name);
3296 if (!parser_t_functions_add(parser, func)) {
3297 parseerror(parser, "failed to allocate slot for function `%s`", var->name);
3298 ast_function_delete(func);
3299 var->constval.vfunc = NULL;
3303 func->builtin = -parser_token(parser)->constval.i;
3305 if (!parser_next(parser)) {
3306 parseerror(parser, "expected comma or semicolon");
3307 ast_function_delete(func);
3308 var->constval.vfunc = NULL;
3312 else if (parser->tok == '{' || parser->tok == '[')
3315 parseerror(parser, "cannot declare functions within functions");
3319 if (!parse_function_body(parser, var))
3321 ast_delete(basetype);
3324 ast_expression *cexp;
3327 cexp = parse_expression_leave(parser, true);
3332 cval = (ast_value*)cexp;
3333 if (!ast_istype(cval, ast_value) || !cval->isconst)
3334 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3337 var->isconst = true;
3338 if (cval->expression.vtype == TYPE_STRING)
3339 var->constval.vstring = parser_strdup(cval->constval.vstring);
3341 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3346 MEM_VECTOR_INIT(&sy, out);
3347 MEM_VECTOR_INIT(&sy, ops);
3348 if (!shunt_out_add(&sy, syexp(ast_ctx(var), (ast_expression*)var)) ||
3349 !shunt_out_add(&sy, syexp(ast_ctx(cexp), (ast_expression*)cexp)) ||
3350 !shunt_ops_add(&sy, syop(ast_ctx(var), parser->assign_op)))
3352 parseerror(parser, "internal error: failed to prepare initializer");
3355 else if (!parser_sy_pop(parser, &sy))
3358 if (sy.out_count != 1 && sy.ops_count != 0)
3359 parseerror(parser, "internal error: leaked operands");
3360 else if (!ast_block_exprs_add(localblock, (ast_expression*)sy.out[0].out)) {
3361 parseerror(parser, "failed to create intializing expression");
3362 ast_unref(sy.out[0].out);
3366 MEM_VECTOR_CLEAR(&sy, out);
3367 MEM_VECTOR_CLEAR(&sy, ops);
3372 if (parser->tok == ',') {
3373 if (!parser_next(parser)) {
3374 parseerror(parser, "expected another variable");
3378 if (parser->tok != TOKEN_IDENT) {
3379 parseerror(parser, "expected another variable");
3382 var = ast_value_copy(basetype);
3384 ast_value_set_name(var, parser_tokval(parser));
3385 if (!parser_next(parser)) {
3386 parseerror(parser, "error parsing variable declaration");
3392 if (parser->tok != ';') {
3393 parseerror(parser, "missing semicolon after variables");
3397 if (!parser_next(parser)) {
3398 parseerror(parser, "parse error after variable declaration");
3402 ast_delete(basetype);
3406 if (cleanvar && var)
3408 ast_delete(basetype);
3412 ast_delete(basetype);
3413 if (cleanvar && var)
3415 if (varent.name) mem_d(varent.name);
3416 if (ve[0].name) mem_d(ve[0].name);
3417 if (ve[1].name) mem_d(ve[1].name);
3418 if (ve[2].name) mem_d(ve[2].name);
3419 if (ve[0].var) mem_d(ve[0].var);
3420 if (ve[1].var) mem_d(ve[1].var);
3421 if (ve[2].var) mem_d(ve[2].var);
3425 static bool parser_global_statement(parser_t *parser)
3427 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3429 return parse_variable(parser, NULL, false);
3431 else if (parser->tok == TOKEN_KEYWORD)
3433 /* handle 'var' and 'const' */
3434 if (!strcmp(parser_tokval(parser), "var")) {
3435 if (!parser_next(parser)) {
3436 parseerror(parser, "expected variable declaration after 'var'");
3439 return parse_variable(parser, NULL, true);
3443 else if (parser->tok == '$')
3445 if (!parser_next(parser)) {
3446 parseerror(parser, "parse error");
3452 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3458 static parser_t *parser;
3463 parser = (parser_t*)mem_a(sizeof(parser_t));
3467 memset(parser, 0, sizeof(*parser));
3469 for (i = 0; i < operator_count; ++i) {
3470 if (operators[i].id == opid1('=')) {
3471 parser->assign_op = operators+i;
3475 if (!parser->assign_op) {
3476 printf("internal error: initializing parser: failed to find assign operator\n");
3483 bool parser_compile(const char *filename)
3485 parser->lex = lex_open(filename);
3487 printf("failed to open file \"%s\"\n", filename);
3491 /* initial lexer/parser state */
3492 parser->lex->flags.noops = true;
3494 if (parser_next(parser))
3496 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3498 if (!parser_global_statement(parser)) {
3499 if (parser->tok == TOKEN_EOF)
3500 parseerror(parser, "unexpected eof");
3501 else if (!parser->errors)
3502 parseerror(parser, "there have been errors, bailing out");
3503 lex_close(parser->lex);
3509 parseerror(parser, "parse error");
3510 lex_close(parser->lex);
3515 lex_close(parser->lex);
3518 return !parser->errors;
3521 void parser_cleanup()
3524 for (i = 0; i < parser->functions_count; ++i) {
3525 ast_delete(parser->functions[i]);
3527 for (i = 0; i < parser->imm_vector_count; ++i) {
3528 ast_delete(parser->imm_vector[i]);
3530 for (i = 0; i < parser->imm_string_count; ++i) {
3531 ast_delete(parser->imm_string[i]);
3533 for (i = 0; i < parser->imm_float_count; ++i) {
3534 ast_delete(parser->imm_float[i]);
3536 for (i = 0; i < parser->fields_count; ++i) {
3537 ast_delete(parser->fields[i].var);
3538 mem_d(parser->fields[i].name);
3540 for (i = 0; i < parser->globals_count; ++i) {
3541 ast_delete(parser->globals[i].var);
3542 mem_d(parser->globals[i].name);
3544 MEM_VECTOR_CLEAR(parser, functions);
3545 MEM_VECTOR_CLEAR(parser, imm_vector);
3546 MEM_VECTOR_CLEAR(parser, imm_string);
3547 MEM_VECTOR_CLEAR(parser, imm_float);
3548 MEM_VECTOR_CLEAR(parser, globals);
3549 MEM_VECTOR_CLEAR(parser, fields);
3550 MEM_VECTOR_CLEAR(parser, locals);
3555 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3557 return util_crc16(old, str, strlen(str));
3560 static void progdefs_crc_file(const char *str)
3562 /* write to progdefs.h here */
3565 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3567 old = progdefs_crc_sum(old, str);
3568 progdefs_crc_file(str);
3572 static void generate_checksum(parser_t *parser)
3574 uint16_t crc = 0xFFFF;
3577 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3578 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3580 progdefs_crc_file("\tint\tpad;\n");
3581 progdefs_crc_file("\tint\tofs_return[3];\n");
3582 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3583 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3584 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3585 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3586 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3587 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3588 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3589 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3591 for (i = 0; i < parser->crc_globals; ++i) {
3592 if (!ast_istype(parser->globals[i].var, ast_value))
3594 switch (parser->globals[i].var->expression.vtype) {
3595 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3596 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3597 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3598 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3600 crc = progdefs_crc_both(crc, "\tint\t");
3603 crc = progdefs_crc_both(crc, parser->globals[i].name);
3604 crc = progdefs_crc_both(crc, ";\n");
3606 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3607 for (i = 0; i < parser->crc_fields; ++i) {
3608 if (!ast_istype(parser->fields[i].var, ast_value))
3610 switch (parser->fields[i].var->expression.next->expression.vtype) {
3611 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3612 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3613 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3614 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3616 crc = progdefs_crc_both(crc, "\tint\t");
3619 crc = progdefs_crc_both(crc, parser->fields[i].name);
3620 crc = progdefs_crc_both(crc, ";\n");
3622 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3627 bool parser_finish(const char *output)
3633 if (!parser->errors)
3635 ir = ir_builder_new("gmqcc_out");
3637 printf("failed to allocate builder\n");
3641 for (i = 0; i < parser->fields_count; ++i) {
3644 if (!ast_istype(parser->fields[i].var, ast_value))
3646 field = (ast_value*)parser->fields[i].var;
3647 isconst = field->isconst;
3648 field->isconst = false;
3649 if (!ast_global_codegen((ast_value*)field, ir, true)) {
3650 printf("failed to generate field %s\n", field->name);
3651 ir_builder_delete(ir);
3656 ast_expression *subtype;
3657 field->isconst = true;
3658 subtype = field->expression.next;
3659 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
3660 if (subtype->expression.vtype == TYPE_FIELD)
3661 ifld->fieldtype = subtype->expression.next->expression.vtype;
3662 else if (subtype->expression.vtype == TYPE_FUNCTION)
3663 ifld->outtype = subtype->expression.next->expression.vtype;
3664 (void)!ir_value_set_field(field->ir_v, ifld);
3667 for (i = 0; i < parser->globals_count; ++i) {
3669 if (!ast_istype(parser->globals[i].var, ast_value))
3671 asvalue = (ast_value*)(parser->globals[i].var);
3672 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
3673 if (strcmp(asvalue->name, "end_sys_globals") &&
3674 strcmp(asvalue->name, "end_sys_fields"))
3676 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
3677 "unused global: `%s`", asvalue->name);
3680 if (!ast_global_codegen(asvalue, ir, false)) {
3681 printf("failed to generate global %s\n", parser->globals[i].name);
3682 ir_builder_delete(ir);
3686 for (i = 0; i < parser->imm_float_count; ++i) {
3687 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
3688 printf("failed to generate global %s\n", parser->imm_float[i]->name);
3689 ir_builder_delete(ir);
3693 for (i = 0; i < parser->imm_string_count; ++i) {
3694 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
3695 printf("failed to generate global %s\n", parser->imm_string[i]->name);
3696 ir_builder_delete(ir);
3700 for (i = 0; i < parser->imm_vector_count; ++i) {
3701 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
3702 printf("failed to generate global %s\n", parser->imm_vector[i]->name);
3703 ir_builder_delete(ir);
3707 for (i = 0; i < parser->globals_count; ++i) {
3709 if (!ast_istype(parser->globals[i].var, ast_value))
3711 asvalue = (ast_value*)(parser->globals[i].var);
3712 if (asvalue->setter) {
3713 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3714 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3715 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3717 printf("failed to generate setter for %s\n", parser->globals[i].name);
3718 ir_builder_delete(ir);
3722 if (asvalue->getter) {
3723 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3724 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3725 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3727 printf("failed to generate getter for %s\n", parser->globals[i].name);
3728 ir_builder_delete(ir);
3733 for (i = 0; i < parser->fields_count; ++i) {
3735 asvalue = (ast_value*)(parser->fields[i].var->expression.next);
3737 if (!ast_istype((ast_expression*)asvalue, ast_value))
3739 if (asvalue->expression.vtype != TYPE_ARRAY)
3741 if (asvalue->setter) {
3742 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3743 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3744 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3746 printf("failed to generate setter for %s\n", parser->fields[i].name);
3747 ir_builder_delete(ir);
3751 if (asvalue->getter) {
3752 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3753 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3754 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3756 printf("failed to generate getter for %s\n", parser->fields[i].name);
3757 ir_builder_delete(ir);
3762 for (i = 0; i < parser->functions_count; ++i) {
3763 if (!ast_function_codegen(parser->functions[i], ir)) {
3764 printf("failed to generate function %s\n", parser->functions[i]->name);
3765 ir_builder_delete(ir);
3768 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
3769 printf("failed to finalize function %s\n", parser->functions[i]->name);
3770 ir_builder_delete(ir);
3777 ir_builder_dump(ir, printf);
3779 generate_checksum(parser);
3781 if (!ir_builder_generate(ir, output)) {
3782 printf("*** failed to generate output file\n");
3783 ir_builder_delete(ir);
3788 ir_builder_delete(ir);
3792 printf("*** there were compile errors\n");