5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define PARSER_HT_FIELDS 0
30 #define PARSER_HT_GLOBALS 1
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 1024
35 #define TYPEDEF_HT_SIZE 16
41 ast_expression **globals;
42 ast_expression **fields;
43 ast_function **functions;
44 ast_value **imm_float;
45 ast_value **imm_string;
46 ast_value **imm_vector;
49 /* must be deleted first, they reference immediates and values */
50 ast_value **accessors;
52 ast_value *imm_float_zero;
53 ast_value *imm_float_one;
54 ast_value *imm_vector_zero;
59 ast_function *function;
61 /* All the labels the function defined...
62 * Should they be in ast_function instead?
67 /* A list of hashtables for each scope */
73 /* not to be used directly, we use the hash table */
74 ast_expression **_locals;
76 ast_value **_typedefs;
77 size_t *_blocktypedefs;
81 /* we store the '=' operator info */
82 const oper_info *assign_op;
84 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
85 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
86 * anything else: type error
95 static void parser_enterblock(parser_t *parser);
96 static bool parser_leaveblock(parser_t *parser);
97 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
98 static bool parse_typedef(parser_t *parser);
99 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef);
100 static ast_block* parse_block(parser_t *parser, bool warnreturn);
101 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
102 static ast_expression* parse_statement_or_block(parser_t *parser);
103 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
104 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
105 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
107 static void parseerror(parser_t *parser, const char *fmt, ...)
114 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
118 /* returns true if it counts as an error */
119 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
122 int lvl = LVL_WARNING;
124 if (!OPTS_WARN(warntype))
133 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
139 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
142 int lvl = LVL_WARNING;
144 if (!OPTS_WARN(warntype))
151 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
157 /**********************************************************************
158 * some maths used for constant folding
161 vector vec3_add(vector a, vector b)
170 vector vec3_sub(vector a, vector b)
179 qcfloat vec3_mulvv(vector a, vector b)
181 return (a.x * b.x + a.y * b.y + a.z * b.z);
184 vector vec3_mulvf(vector a, float b)
193 /**********************************************************************
197 bool parser_next(parser_t *parser)
199 /* lex_do kills the previous token */
200 parser->tok = lex_do(parser->lex);
201 if (parser->tok == TOKEN_EOF)
203 if (parser->tok >= TOKEN_ERROR) {
204 parseerror(parser, "lex error");
210 #define parser_tokval(p) ((p)->lex->tok.value)
211 #define parser_token(p) (&((p)->lex->tok))
212 #define parser_ctx(p) ((p)->lex->tok.ctx)
214 static ast_value* parser_const_float(parser_t *parser, double d)
218 for (i = 0; i < vec_size(parser->imm_float); ++i) {
219 const double compare = parser->imm_float[i]->constval.vfloat;
220 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
221 return parser->imm_float[i];
223 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
224 out->constant = true;
225 out->hasvalue = true;
226 out->constval.vfloat = d;
227 vec_push(parser->imm_float, out);
231 static ast_value* parser_const_float_0(parser_t *parser)
233 if (!parser->imm_float_zero)
234 parser->imm_float_zero = parser_const_float(parser, 0);
235 return parser->imm_float_zero;
238 static ast_value* parser_const_float_1(parser_t *parser)
240 if (!parser->imm_float_one)
241 parser->imm_float_one = parser_const_float(parser, 1);
242 return parser->imm_float_one;
245 static char *parser_strdup(const char *str)
248 /* actually dup empty strings */
249 char *out = mem_a(1);
253 return util_strdup(str);
256 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
260 for (i = 0; i < vec_size(parser->imm_string); ++i) {
261 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
262 return parser->imm_string[i];
266 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
267 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
269 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
270 out->constant = true;
271 out->hasvalue = true;
272 out->constval.vstring = parser_strdup(str);
273 vec_push(parser->imm_string, out);
277 static ast_value* parser_const_vector(parser_t *parser, vector v)
281 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
282 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
283 return parser->imm_vector[i];
285 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
286 out->constant = true;
287 out->hasvalue = true;
288 out->constval.vvec = v;
289 vec_push(parser->imm_vector, out);
293 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
299 return parser_const_vector(parser, v);
302 static ast_value* parser_const_vector_0(parser_t *parser)
304 if (!parser->imm_vector_zero)
305 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
306 return parser->imm_vector_zero;
309 static ast_expression* parser_find_field(parser_t *parser, const char *name)
311 return util_htget(parser->htfields, name);
314 static ast_expression* parser_find_global(parser_t *parser, const char *name)
316 return util_htget(parser->htglobals, name);
319 static ast_expression* parser_find_param(parser_t *parser, const char *name)
323 if (!parser->function)
325 fun = parser->function->vtype;
326 for (i = 0; i < vec_size(fun->expression.params); ++i) {
327 if (!strcmp(fun->expression.params[i]->name, name))
328 return (ast_expression*)(fun->expression.params[i]);
333 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
338 hash = util_hthash(parser->htglobals, name);
341 for (i = vec_size(parser->variables); i > upto;) {
343 if ( (e = util_htgeth(parser->variables[i], name, hash)) )
347 return parser_find_param(parser, name);
350 static ast_expression* parser_find_var(parser_t *parser, const char *name)
354 v = parser_find_local(parser, name, 0, &dummy);
355 if (!v) v = parser_find_global(parser, name);
359 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
363 hash = util_hthash(parser->typedefs[0], name);
365 for (i = vec_size(parser->typedefs); i > upto;) {
367 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
375 size_t etype; /* 0 = expression, others are operators */
379 ast_block *block; /* for commas and function calls */
388 #define SY_PAREN_EXPR '('
389 #define SY_PAREN_FUNC 'f'
390 #define SY_PAREN_INDEX '['
392 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
403 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
407 e.out = (ast_expression*)v;
414 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
416 e.etype = 1 + (op - operators);
425 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
437 # define DEBUGSHUNTDO(x) x
439 # define DEBUGSHUNTDO(x)
442 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
443 * so we need to rotate it to become ent.(foo[n]).
445 static bool rotate_entfield_array_index_nodes(ast_expression **out)
447 ast_array_index *index;
448 ast_entfield *entfield;
452 ast_expression *entity;
454 lex_ctx ctx = ast_ctx(*out);
456 if (!ast_istype(*out, ast_array_index))
458 index = (ast_array_index*)*out;
460 if (!ast_istype(index->array, ast_entfield))
462 entfield = (ast_entfield*)index->array;
464 if (!ast_istype(entfield->field, ast_value))
466 field = (ast_value*)entfield->field;
469 entity = entfield->entity;
473 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
474 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
475 *out = (ast_expression*)entfield;
480 static bool parser_sy_pop(parser_t *parser, shunt *sy)
484 ast_expression *out = NULL;
485 ast_expression *exprs[3];
486 ast_block *blocks[3];
487 ast_value *asvalue[3];
488 size_t i, assignop, addop, subop;
489 qcint generated_op = 0;
494 if (!vec_size(sy->ops)) {
495 parseerror(parser, "internal error: missing operator");
499 if (vec_last(sy->ops).paren) {
500 parseerror(parser, "unmatched parenthesis");
504 op = &operators[vec_last(sy->ops).etype - 1];
505 ctx = vec_last(sy->ops).ctx;
507 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
509 if (vec_size(sy->out) < op->operands) {
510 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
511 op->op, (int)op->id);
515 vec_shrinkby(sy->ops, 1);
517 vec_shrinkby(sy->out, op->operands);
518 for (i = 0; i < op->operands; ++i) {
519 exprs[i] = sy->out[vec_size(sy->out)+i].out;
520 blocks[i] = sy->out[vec_size(sy->out)+i].block;
521 asvalue[i] = (ast_value*)exprs[i];
524 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
525 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
529 #define NotSameType(T) \
530 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
531 exprs[0]->expression.vtype != T)
532 #define CanConstFold1(A) \
533 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && ((ast_value*)(A))->constant)
534 #define CanConstFold(A, B) \
535 (CanConstFold1(A) && CanConstFold1(B))
536 #define ConstV(i) (asvalue[(i)]->constval.vvec)
537 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
538 #define ConstS(i) (asvalue[(i)]->constval.vstring)
542 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
546 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
547 if (exprs[1]->expression.vtype != TYPE_FIELD) {
548 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
551 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
553 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
554 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
558 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
564 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
565 !(exprs[0]->expression.vtype == TYPE_FIELD &&
566 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
568 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
569 parseerror(parser, "cannot index value of type %s", ty1);
572 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
573 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
574 parseerror(parser, "index must be of type float, not %s", ty1);
577 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
578 if (rotate_entfield_array_index_nodes(&out))
580 if (opts_standard != COMPILER_GMQCC) {
581 /* this error doesn't need to make us bail out */
582 (void)!parsewarning(parser, WARN_EXTENSIONS,
583 "accessing array-field members of an entity without parenthesis\n"
584 " -> this is an extension from -std=gmqcc");
591 ast_block_add_expr(blocks[0], exprs[1]);
593 blocks[0] = ast_block_new(ctx);
594 ast_block_add_expr(blocks[0], exprs[0]);
595 ast_block_add_expr(blocks[0], exprs[1]);
597 if (!ast_block_set_type(blocks[0], exprs[1]))
600 vec_push(sy->out, syblock(ctx, blocks[0]));
604 switch (exprs[0]->expression.vtype) {
606 if (CanConstFold1(exprs[0]))
607 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
609 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
610 (ast_expression*)parser_const_float_0(parser),
614 if (CanConstFold1(exprs[0]))
615 out = (ast_expression*)parser_const_vector_f(parser,
616 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
618 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
619 (ast_expression*)parser_const_vector_0(parser),
623 parseerror(parser, "invalid types used in expression: cannot negate type %s",
624 type_name[exprs[0]->expression.vtype]);
630 switch (exprs[0]->expression.vtype) {
632 if (CanConstFold1(exprs[0]))
633 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
635 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
638 if (CanConstFold1(exprs[0]))
639 out = (ast_expression*)parser_const_float(parser,
640 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
642 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
645 if (CanConstFold1(exprs[0]))
646 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
648 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
650 /* we don't constant-fold NOT for these types */
652 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
655 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
658 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
659 type_name[exprs[0]->expression.vtype]);
665 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
666 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
668 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
669 type_name[exprs[0]->expression.vtype],
670 type_name[exprs[1]->expression.vtype]);
673 switch (exprs[0]->expression.vtype) {
675 if (CanConstFold(exprs[0], exprs[1]))
677 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
680 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
683 if (CanConstFold(exprs[0], exprs[1]))
684 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
686 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
689 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
690 type_name[exprs[0]->expression.vtype],
691 type_name[exprs[1]->expression.vtype]);
696 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
697 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
699 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
700 type_name[exprs[1]->expression.vtype],
701 type_name[exprs[0]->expression.vtype]);
704 switch (exprs[0]->expression.vtype) {
706 if (CanConstFold(exprs[0], exprs[1]))
707 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
709 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
712 if (CanConstFold(exprs[0], exprs[1]))
713 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
715 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
718 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
719 type_name[exprs[1]->expression.vtype],
720 type_name[exprs[0]->expression.vtype]);
725 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
726 exprs[0]->expression.vtype != TYPE_VECTOR &&
727 exprs[0]->expression.vtype != TYPE_FLOAT &&
728 exprs[1]->expression.vtype != TYPE_VECTOR &&
729 exprs[1]->expression.vtype != TYPE_FLOAT)
731 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
732 type_name[exprs[1]->expression.vtype],
733 type_name[exprs[0]->expression.vtype]);
736 switch (exprs[0]->expression.vtype) {
738 if (exprs[1]->expression.vtype == TYPE_VECTOR)
740 if (CanConstFold(exprs[0], exprs[1]))
741 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
743 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
747 if (CanConstFold(exprs[0], exprs[1]))
748 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
750 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
754 if (exprs[1]->expression.vtype == TYPE_FLOAT)
756 if (CanConstFold(exprs[0], exprs[1]))
757 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
759 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
763 if (CanConstFold(exprs[0], exprs[1]))
764 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
766 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
770 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
771 type_name[exprs[1]->expression.vtype],
772 type_name[exprs[0]->expression.vtype]);
777 if (NotSameType(TYPE_FLOAT)) {
778 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
779 type_name[exprs[0]->expression.vtype],
780 type_name[exprs[1]->expression.vtype]);
783 if (CanConstFold(exprs[0], exprs[1]))
784 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
786 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
790 parseerror(parser, "qc does not have a modulo operator");
794 if (NotSameType(TYPE_FLOAT)) {
795 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
796 type_name[exprs[0]->expression.vtype],
797 type_name[exprs[1]->expression.vtype]);
800 if (CanConstFold(exprs[0], exprs[1]))
801 out = (ast_expression*)parser_const_float(parser,
802 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
803 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
805 out = (ast_expression*)ast_binary_new(ctx,
806 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
810 parseerror(parser, "TODO: bitxor");
815 case opid3('<','<','='):
816 case opid3('>','>','='):
817 parseerror(parser, "TODO: shifts");
821 generated_op += 1; /* INSTR_OR */
823 generated_op += INSTR_AND;
825 if (NotSameType(TYPE_FLOAT)) {
826 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
827 type_name[exprs[0]->expression.vtype],
828 type_name[exprs[1]->expression.vtype]);
829 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
830 parseerror(parser, "TODO: optional early out");
834 if (opts_standard == COMPILER_GMQCC)
835 con_out("TODO: early out logic\n");
836 if (CanConstFold(exprs[0], exprs[1]))
837 out = (ast_expression*)parser_const_float(parser,
838 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
840 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
844 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
845 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
846 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
847 parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
850 if (CanConstFold1(exprs[0]))
851 out = (ConstF(0) ? exprs[1] : exprs[2]);
853 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
857 generated_op += 1; /* INSTR_GT */
859 generated_op += 1; /* INSTR_LT */
860 case opid2('>', '='):
861 generated_op += 1; /* INSTR_GE */
862 case opid2('<', '='):
863 generated_op += INSTR_LE;
864 if (NotSameType(TYPE_FLOAT)) {
865 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
866 type_name[exprs[0]->expression.vtype],
867 type_name[exprs[1]->expression.vtype]);
870 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
872 case opid2('!', '='):
873 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
874 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
875 type_name[exprs[0]->expression.vtype],
876 type_name[exprs[1]->expression.vtype]);
879 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
881 case opid2('=', '='):
882 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
883 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
884 type_name[exprs[0]->expression.vtype],
885 type_name[exprs[1]->expression.vtype]);
888 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
892 if (ast_istype(exprs[0], ast_entfield)) {
893 ast_expression *field = ((ast_entfield*)exprs[0])->field;
894 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
895 exprs[0]->expression.vtype == TYPE_FIELD &&
896 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
898 assignop = type_storep_instr[TYPE_VECTOR];
901 assignop = type_storep_instr[exprs[0]->expression.vtype];
902 if (!ast_compare_type(field->expression.next, exprs[1])) {
903 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
904 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
905 if (opts_standard == COMPILER_QCC &&
906 field->expression.next->expression.vtype == TYPE_FUNCTION &&
907 exprs[1]->expression.vtype == TYPE_FUNCTION)
909 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
910 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
916 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
921 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
922 exprs[0]->expression.vtype == TYPE_FIELD &&
923 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
925 assignop = type_store_instr[TYPE_VECTOR];
928 assignop = type_store_instr[exprs[0]->expression.vtype];
931 if (assignop == AINSTR_END) {
932 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
933 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
934 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
936 else if (!ast_compare_type(exprs[0], exprs[1])) {
937 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
938 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
939 if (opts_standard == COMPILER_QCC &&
940 exprs[0]->expression.vtype == TYPE_FUNCTION &&
941 exprs[1]->expression.vtype == TYPE_FUNCTION)
943 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
944 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
950 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
953 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
954 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
956 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
958 case opid3('+','+','P'):
959 case opid3('-','-','P'):
961 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
962 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
963 parseerror(parser, "invalid type for prefix increment: %s", ty1);
966 if (op->id == opid3('+','+','P'))
970 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
971 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
973 if (ast_istype(exprs[0], ast_entfield)) {
974 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
976 (ast_expression*)parser_const_float_1(parser));
978 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
980 (ast_expression*)parser_const_float_1(parser));
983 case opid3('S','+','+'):
984 case opid3('S','-','-'):
986 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
987 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
988 parseerror(parser, "invalid type for suffix increment: %s", ty1);
991 if (op->id == opid3('S','+','+')) {
998 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
999 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1001 if (ast_istype(exprs[0], ast_entfield)) {
1002 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1004 (ast_expression*)parser_const_float_1(parser));
1006 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1008 (ast_expression*)parser_const_float_1(parser));
1012 out = (ast_expression*)ast_binary_new(ctx, subop,
1014 (ast_expression*)parser_const_float_1(parser));
1016 case opid2('+','='):
1017 case opid2('-','='):
1018 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1019 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1021 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1022 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1023 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1027 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1028 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1030 if (ast_istype(exprs[0], ast_entfield))
1031 assignop = type_storep_instr[exprs[0]->expression.vtype];
1033 assignop = type_store_instr[exprs[0]->expression.vtype];
1034 switch (exprs[0]->expression.vtype) {
1036 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1037 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1038 exprs[0], exprs[1]);
1041 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1042 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1043 exprs[0], exprs[1]);
1046 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1047 type_name[exprs[0]->expression.vtype],
1048 type_name[exprs[1]->expression.vtype]);
1052 case opid2('*','='):
1053 case opid2('/','='):
1054 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1055 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1056 exprs[0]->expression.vtype == TYPE_VECTOR))
1058 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1059 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1060 parseerror(parser, "invalid types used in expression: %s and %s",
1064 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1065 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1067 if (ast_istype(exprs[0], ast_entfield))
1068 assignop = type_storep_instr[exprs[0]->expression.vtype];
1070 assignop = type_store_instr[exprs[0]->expression.vtype];
1071 switch (exprs[0]->expression.vtype) {
1073 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1074 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1075 exprs[0], exprs[1]);
1078 if (op->id == opid2('*','=')) {
1079 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1080 exprs[0], exprs[1]);
1082 /* there's no DIV_VF */
1083 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1084 (ast_expression*)parser_const_float_1(parser),
1088 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1093 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1094 type_name[exprs[0]->expression.vtype],
1095 type_name[exprs[1]->expression.vtype]);
1099 case opid2('&','='):
1100 case opid2('|','='):
1101 if (NotSameType(TYPE_FLOAT)) {
1102 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1103 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1104 parseerror(parser, "invalid types used in expression: %s and %s",
1108 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1109 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1111 if (ast_istype(exprs[0], ast_entfield))
1112 assignop = type_storep_instr[exprs[0]->expression.vtype];
1114 assignop = type_store_instr[exprs[0]->expression.vtype];
1115 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1116 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1117 exprs[0], exprs[1]);
1119 case opid3('&','~','='):
1120 /* This is like: a &= ~(b);
1121 * But QC has no bitwise-not, so we implement it as
1124 if (NotSameType(TYPE_FLOAT)) {
1125 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1126 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1127 parseerror(parser, "invalid types used in expression: %s and %s",
1131 if (ast_istype(exprs[0], ast_entfield))
1132 assignop = type_storep_instr[exprs[0]->expression.vtype];
1134 assignop = type_store_instr[exprs[0]->expression.vtype];
1135 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1138 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1139 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1141 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1147 parseerror(parser, "failed to apply operand %s", op->op);
1151 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1152 vec_push(sy->out, syexp(ctx, out));
1156 static bool parser_close_call(parser_t *parser, shunt *sy)
1158 /* was a function call */
1159 ast_expression *fun;
1165 vec_shrinkby(sy->ops, 1);
1166 fid = sy->ops[vec_size(sy->ops)].off;
1168 /* out[fid] is the function
1169 * everything above is parameters...
1170 * 0 params = nothing
1171 * 1 params = ast_expression
1175 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1176 parseerror(parser, "internal error: function call needs function and parameter list...");
1180 fun = sy->out[fid].out;
1182 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1184 parseerror(parser, "out of memory");
1188 if (fid+1 == vec_size(sy->out)) {
1191 } else if (fid+2 == vec_size(sy->out)) {
1193 vec_shrinkby(sy->out, 1);
1194 params = sy->out[vec_size(sy->out)].block;
1198 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1200 paramcount = vec_size(params->exprs);
1201 call->params = params->exprs;
1202 params->exprs = NULL;
1205 if (!ast_call_check_types(call))
1208 parseerror(parser, "invalid function call");
1212 /* overwrite fid, the function, with a call */
1213 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1215 if (fun->expression.vtype != TYPE_FUNCTION) {
1216 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1220 if (!fun->expression.next) {
1221 parseerror(parser, "could not determine function return type");
1224 if (vec_size(fun->expression.params) != paramcount &&
1225 !(fun->expression.variadic &&
1226 vec_size(fun->expression.params) < paramcount))
1229 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1231 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1232 if (opts_standard == COMPILER_GMQCC)
1235 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1236 " -> `%s` has been declared here: %s:%i",
1237 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1238 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1240 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1241 " -> `%s` has been declared here: %s:%i",
1242 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1243 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1249 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1250 "too %s parameters for call to %s: expected %i, got %i\n"
1251 " -> `%s` has been declared here: %s:%i",
1252 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1253 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1255 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1256 "too %s parameters for function call: expected %i, got %i\n"
1257 " -> `%s` has been declared here: %s:%i",
1258 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1259 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1267 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1269 if (!vec_size(sy->ops)) {
1270 parseerror(parser, "unmatched closing paren");
1273 /* this would for bit a + (x) because there are no operators inside (x)
1274 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1275 parseerror(parser, "empty parenthesis expression");
1279 while (vec_size(sy->ops)) {
1280 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1281 if (!parser_close_call(parser, sy))
1285 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1286 vec_shrinkby(sy->ops, 1);
1287 return !functions_only;
1289 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1292 /* pop off the parenthesis */
1293 vec_shrinkby(sy->ops, 1);
1294 /* then apply the index operator */
1295 if (!parser_sy_pop(parser, sy))
1299 if (!parser_sy_pop(parser, sy))
1305 static void parser_reclassify_token(parser_t *parser)
1308 for (i = 0; i < operator_count; ++i) {
1309 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1310 parser->tok = TOKEN_OPERATOR;
1316 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1318 ast_expression *expr = NULL;
1320 bool wantop = false;
1321 bool gotmemberof = false;
1323 /* count the parens because an if starts with one, so the
1324 * end of a condition is an unmatched closing paren
1332 parser->lex->flags.noops = false;
1334 parser_reclassify_token(parser);
1339 gotmemberof = false;
1341 parser->memberof = 0;
1343 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1344 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1346 /* a translatable string */
1350 parseerror(parser, "expected operator or end of statement, got constant");
1354 parser->lex->flags.noops = true;
1355 if (!parser_next(parser) || parser->tok != '(') {
1356 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1359 parser->lex->flags.noops = false;
1360 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1361 parseerror(parser, "expected a constant string in translatable-string extension");
1364 val = parser_const_string(parser, parser_tokval(parser), true);
1368 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1369 DEBUGSHUNTDO(con_out("push string\n"));
1371 if (!parser_next(parser) || parser->tok != ')') {
1372 parseerror(parser, "expected closing paren after translatable string");
1376 else if (parser->tok == TOKEN_IDENT)
1378 ast_expression *var;
1380 parseerror(parser, "expected operator or end of statement");
1385 if (opts_standard == COMPILER_GMQCC)
1387 if (parser->memberof == TYPE_ENTITY) {
1388 /* still get vars first since there could be a fieldpointer */
1389 var = parser_find_var(parser, parser_tokval(parser));
1391 var = parser_find_field(parser, parser_tokval(parser));
1393 else if (parser->memberof == TYPE_VECTOR)
1395 parseerror(parser, "TODO: implement effective vector member access");
1398 else if (parser->memberof) {
1399 parseerror(parser, "namespace for member not found");
1403 var = parser_find_var(parser, parser_tokval(parser));
1405 var = parser_find_var(parser, parser_tokval(parser));
1407 var = parser_find_field(parser, parser_tokval(parser));
1410 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1413 if (ast_istype(var, ast_value))
1414 ((ast_value*)var)->uses++;
1415 vec_push(sy.out, syexp(parser_ctx(parser), var));
1416 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1418 else if (parser->tok == TOKEN_FLOATCONST) {
1421 parseerror(parser, "expected operator or end of statement, got constant");
1425 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1428 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1429 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1431 else if (parser->tok == TOKEN_INTCONST) {
1434 parseerror(parser, "expected operator or end of statement, got constant");
1438 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1441 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1442 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1444 else if (parser->tok == TOKEN_STRINGCONST) {
1447 parseerror(parser, "expected operator or end of statement, got constant");
1451 val = parser_const_string(parser, parser_tokval(parser), false);
1454 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1455 DEBUGSHUNTDO(con_out("push string\n"));
1457 else if (parser->tok == TOKEN_VECTORCONST) {
1460 parseerror(parser, "expected operator or end of statement, got constant");
1464 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1467 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1468 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1469 parser_token(parser)->constval.v.x,
1470 parser_token(parser)->constval.v.y,
1471 parser_token(parser)->constval.v.z));
1473 else if (parser->tok == '(') {
1474 parseerror(parser, "internal error: '(' should be classified as operator");
1477 else if (parser->tok == '[') {
1478 parseerror(parser, "internal error: '[' should be classified as operator");
1481 else if (parser->tok == ')') {
1483 DEBUGSHUNTDO(con_out("do[op] )\n"));
1487 /* we do expect an operator next */
1488 /* closing an opening paren */
1489 if (!parser_close_paren(parser, &sy, false))
1492 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1496 /* allowed for function calls */
1497 if (!parser_close_paren(parser, &sy, true))
1502 else if (parser->tok == ']') {
1504 parseerror(parser, "operand expected");
1508 if (!parser_close_paren(parser, &sy, false))
1512 else if (parser->tok != TOKEN_OPERATOR) {
1514 parseerror(parser, "expected operator or end of statement");
1521 /* classify the operator */
1522 const oper_info *op;
1523 const oper_info *olast = NULL;
1525 for (o = 0; o < operator_count; ++o) {
1526 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1527 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1528 !strcmp(parser_tokval(parser), operators[o].op))
1533 if (o == operator_count) {
1534 /* no operator found... must be the end of the statement */
1537 /* found an operator */
1540 /* when declaring variables, a comma starts a new variable */
1541 if (op->id == opid1(',') && !parens && stopatcomma) {
1542 /* fixup the token */
1547 /* a colon without a pervious question mark cannot be a ternary */
1548 if (!ternaries && op->id == opid2(':','?')) {
1553 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1554 olast = &operators[vec_last(sy.ops).etype-1];
1557 (op->prec < olast->prec) ||
1558 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1560 if (!parser_sy_pop(parser, &sy))
1562 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1563 olast = &operators[vec_last(sy.ops).etype-1];
1568 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1569 /* for gmqcc standard: open up the namespace of the previous type */
1570 ast_expression *prevex = vec_last(sy.out).out;
1572 parseerror(parser, "unexpected member operator");
1575 if (prevex->expression.vtype == TYPE_ENTITY)
1576 parser->memberof = TYPE_ENTITY;
1577 else if (prevex->expression.vtype == TYPE_VECTOR)
1578 parser->memberof = TYPE_VECTOR;
1580 parseerror(parser, "type error: type has no members");
1586 if (op->id == opid1('(')) {
1588 size_t sycount = vec_size(sy.out);
1589 DEBUGSHUNTDO(con_out("push [op] (\n"));
1591 /* we expected an operator, this is the function-call operator */
1592 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1595 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1596 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1599 } else if (op->id == opid1('[')) {
1601 parseerror(parser, "unexpected array subscript");
1605 /* push both the operator and the paren, this makes life easier */
1606 vec_push(sy.ops, syop(parser_ctx(parser), op));
1607 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1609 } else if (op->id == opid2('?',':')) {
1611 vec_push(sy.ops, syop(parser_ctx(parser), op));
1614 } else if (op->id == opid2(':','?')) {
1615 /* we don't push this operator */
1619 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1620 vec_push(sy.ops, syop(parser_ctx(parser), op));
1621 wantop = !!(op->flags & OP_SUFFIX);
1624 if (!parser_next(parser)) {
1627 if (parser->tok == ';' ||
1628 (!parens && parser->tok == ']'))
1634 while (vec_size(sy.ops)) {
1635 if (!parser_sy_pop(parser, &sy))
1639 parser->lex->flags.noops = true;
1640 if (!vec_size(sy.out)) {
1641 parseerror(parser, "empty expression");
1644 expr = sy.out[0].out;
1647 DEBUGSHUNTDO(con_out("shunt done\n"));
1651 parser->lex->flags.noops = true;
1657 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1659 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1662 if (!parser_next(parser)) {
1669 static void parser_enterblock(parser_t *parser)
1671 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1672 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1673 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1674 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1677 static bool parser_leaveblock(parser_t *parser)
1680 size_t locals, typedefs;
1682 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1683 parseerror(parser, "internal error: parser_leaveblock with no block");
1687 util_htdel(vec_last(parser->variables));
1688 vec_pop(parser->variables);
1689 if (!vec_size(parser->_blocklocals)) {
1690 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1694 locals = vec_last(parser->_blocklocals);
1695 vec_pop(parser->_blocklocals);
1696 while (vec_size(parser->_locals) != locals) {
1697 ast_expression *e = vec_last(parser->_locals);
1698 ast_value *v = (ast_value*)e;
1699 vec_pop(parser->_locals);
1700 if (ast_istype(e, ast_value) && !v->uses) {
1701 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
1706 typedefs = vec_last(parser->_blocktypedefs);
1707 while (vec_size(parser->_typedefs) != typedefs) {
1708 ast_delete(vec_last(parser->_typedefs));
1709 vec_pop(parser->_typedefs);
1711 util_htdel(vec_last(parser->typedefs));
1712 vec_pop(parser->typedefs);
1717 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1719 vec_push(parser->_locals, e);
1720 util_htset(vec_last(parser->variables), name, (void*)e);
1723 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1726 ast_expression *cond, *ontrue, *onfalse = NULL;
1729 lex_ctx ctx = parser_ctx(parser);
1731 (void)block; /* not touching */
1733 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1734 if (!parser_next(parser)) {
1735 parseerror(parser, "expected condition or 'not'");
1738 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
1740 if (!parser_next(parser)) {
1741 parseerror(parser, "expected condition in parenthesis");
1745 if (parser->tok != '(') {
1746 parseerror(parser, "expected 'if' condition in parenthesis");
1749 /* parse into the expression */
1750 if (!parser_next(parser)) {
1751 parseerror(parser, "expected 'if' condition after opening paren");
1754 /* parse the condition */
1755 cond = parse_expression_leave(parser, false);
1759 if (parser->tok != ')') {
1760 parseerror(parser, "expected closing paren after 'if' condition");
1764 /* parse into the 'then' branch */
1765 if (!parser_next(parser)) {
1766 parseerror(parser, "expected statement for on-true branch of 'if'");
1770 ontrue = parse_statement_or_block(parser);
1775 /* check for an else */
1776 if (!strcmp(parser_tokval(parser), "else")) {
1777 /* parse into the 'else' branch */
1778 if (!parser_next(parser)) {
1779 parseerror(parser, "expected on-false branch after 'else'");
1784 onfalse = parse_statement_or_block(parser);
1793 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1795 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1796 *out = (ast_expression*)ifthen;
1800 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1803 ast_expression *cond, *ontrue;
1805 lex_ctx ctx = parser_ctx(parser);
1807 (void)block; /* not touching */
1809 /* skip the 'while' and check for opening paren */
1810 if (!parser_next(parser) || parser->tok != '(') {
1811 parseerror(parser, "expected 'while' condition in parenthesis");
1814 /* parse into the expression */
1815 if (!parser_next(parser)) {
1816 parseerror(parser, "expected 'while' condition after opening paren");
1819 /* parse the condition */
1820 cond = parse_expression_leave(parser, false);
1824 if (parser->tok != ')') {
1825 parseerror(parser, "expected closing paren after 'while' condition");
1829 /* parse into the 'then' branch */
1830 if (!parser_next(parser)) {
1831 parseerror(parser, "expected while-loop body");
1835 ontrue = parse_statement_or_block(parser);
1841 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1842 *out = (ast_expression*)aloop;
1846 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1849 ast_expression *cond, *ontrue;
1851 lex_ctx ctx = parser_ctx(parser);
1853 (void)block; /* not touching */
1855 /* skip the 'do' and get the body */
1856 if (!parser_next(parser)) {
1857 parseerror(parser, "expected loop body");
1860 ontrue = parse_statement_or_block(parser);
1864 /* expect the "while" */
1865 if (parser->tok != TOKEN_KEYWORD ||
1866 strcmp(parser_tokval(parser), "while"))
1868 parseerror(parser, "expected 'while' and condition");
1873 /* skip the 'while' and check for opening paren */
1874 if (!parser_next(parser) || parser->tok != '(') {
1875 parseerror(parser, "expected 'while' condition in parenthesis");
1879 /* parse into the expression */
1880 if (!parser_next(parser)) {
1881 parseerror(parser, "expected 'while' condition after opening paren");
1885 /* parse the condition */
1886 cond = parse_expression_leave(parser, false);
1890 if (parser->tok != ')') {
1891 parseerror(parser, "expected closing paren after 'while' condition");
1897 if (!parser_next(parser) || parser->tok != ';') {
1898 parseerror(parser, "expected semicolon after condition");
1904 if (!parser_next(parser)) {
1905 parseerror(parser, "parse error");
1911 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1912 *out = (ast_expression*)aloop;
1916 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1919 ast_expression *initexpr, *cond, *increment, *ontrue;
1923 lex_ctx ctx = parser_ctx(parser);
1925 parser_enterblock(parser);
1932 /* skip the 'while' and check for opening paren */
1933 if (!parser_next(parser) || parser->tok != '(') {
1934 parseerror(parser, "expected 'for' expressions in parenthesis");
1937 /* parse into the expression */
1938 if (!parser_next(parser)) {
1939 parseerror(parser, "expected 'for' initializer after opening paren");
1944 if (parser->tok == TOKEN_IDENT)
1945 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
1947 if (typevar || parser->tok == TOKEN_TYPENAME) {
1948 if (opts_standard != COMPILER_GMQCC) {
1949 if (parsewarning(parser, WARN_EXTENSIONS,
1950 "current standard does not allow variable declarations in for-loop initializers"))
1953 if (!parse_variable(parser, block, true, CV_VAR, typevar))
1956 else if (parser->tok != ';')
1958 initexpr = parse_expression_leave(parser, false);
1963 /* move on to condition */
1964 if (parser->tok != ';') {
1965 parseerror(parser, "expected semicolon after for-loop initializer");
1968 if (!parser_next(parser)) {
1969 parseerror(parser, "expected for-loop condition");
1973 /* parse the condition */
1974 if (parser->tok != ';') {
1975 cond = parse_expression_leave(parser, false);
1980 /* move on to incrementor */
1981 if (parser->tok != ';') {
1982 parseerror(parser, "expected semicolon after for-loop initializer");
1985 if (!parser_next(parser)) {
1986 parseerror(parser, "expected for-loop condition");
1990 /* parse the incrementor */
1991 if (parser->tok != ')') {
1992 increment = parse_expression_leave(parser, false);
1995 if (!ast_side_effects(increment)) {
1996 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2002 if (parser->tok != ')') {
2003 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2006 /* parse into the 'then' branch */
2007 if (!parser_next(parser)) {
2008 parseerror(parser, "expected for-loop body");
2011 ontrue = parse_statement_or_block(parser);
2016 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
2017 *out = (ast_expression*)aloop;
2019 if (!parser_leaveblock(parser))
2023 if (initexpr) ast_delete(initexpr);
2024 if (cond) ast_delete(cond);
2025 if (increment) ast_delete(increment);
2026 (void)!parser_leaveblock(parser);
2030 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2032 ast_expression *exp = NULL;
2033 ast_return *ret = NULL;
2034 ast_value *expected = parser->function->vtype;
2036 (void)block; /* not touching */
2038 if (!parser_next(parser)) {
2039 parseerror(parser, "expected return expression");
2043 if (parser->tok != ';') {
2044 exp = parse_expression(parser, false);
2048 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2049 parseerror(parser, "return with invalid expression");
2052 ret = ast_return_new(exp->expression.node.context, exp);
2058 if (!parser_next(parser))
2059 parseerror(parser, "parse error");
2060 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2061 if (opts_standard != COMPILER_GMQCC)
2062 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2064 parseerror(parser, "return without value");
2066 ret = ast_return_new(parser_ctx(parser), NULL);
2068 *out = (ast_expression*)ret;
2072 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2074 lex_ctx ctx = parser_ctx(parser);
2076 (void)block; /* not touching */
2078 if (!parser_next(parser) || parser->tok != ';') {
2079 parseerror(parser, "expected semicolon");
2083 if (!parser_next(parser))
2084 parseerror(parser, "parse error");
2086 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2090 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2092 ast_expression *operand;
2094 ast_switch *switchnode;
2095 ast_switch_case swcase;
2097 lex_ctx ctx = parser_ctx(parser);
2099 (void)block; /* not touching */
2101 /* parse over the opening paren */
2102 if (!parser_next(parser) || parser->tok != '(') {
2103 parseerror(parser, "expected switch operand in parenthesis");
2107 /* parse into the expression */
2108 if (!parser_next(parser)) {
2109 parseerror(parser, "expected switch operand");
2112 /* parse the operand */
2113 operand = parse_expression_leave(parser, false);
2117 switchnode = ast_switch_new(ctx, operand);
2120 if (parser->tok != ')') {
2121 ast_delete(switchnode);
2122 parseerror(parser, "expected closing paren after 'switch' operand");
2126 /* parse over the opening paren */
2127 if (!parser_next(parser) || parser->tok != '{') {
2128 ast_delete(switchnode);
2129 parseerror(parser, "expected list of cases");
2133 if (!parser_next(parser)) {
2134 ast_delete(switchnode);
2135 parseerror(parser, "expected 'case' or 'default'");
2140 while (parser->tok != '}') {
2141 ast_block *caseblock;
2143 if (parser->tok != TOKEN_KEYWORD) {
2144 ast_delete(switchnode);
2145 parseerror(parser, "expected 'case' or 'default'");
2148 if (!strcmp(parser_tokval(parser), "case")) {
2149 if (!parser_next(parser)) {
2150 ast_delete(switchnode);
2151 parseerror(parser, "expected expression for case");
2154 swcase.value = parse_expression_leave(parser, false);
2155 if (!swcase.value) {
2156 ast_delete(switchnode);
2157 parseerror(parser, "expected expression for case");
2160 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2161 opval = (ast_value*)swcase.value;
2162 if (!ast_istype(swcase.value, ast_value) || !opval->constant) {
2163 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2169 else if (!strcmp(parser_tokval(parser), "default")) {
2170 swcase.value = NULL;
2171 if (!parser_next(parser)) {
2172 ast_delete(switchnode);
2173 parseerror(parser, "expected colon");
2178 /* Now the colon and body */
2179 if (parser->tok != ':') {
2180 if (swcase.value) ast_unref(swcase.value);
2181 ast_delete(switchnode);
2182 parseerror(parser, "expected colon");
2186 if (!parser_next(parser)) {
2187 if (swcase.value) ast_unref(swcase.value);
2188 ast_delete(switchnode);
2189 parseerror(parser, "expected statements or case");
2192 caseblock = ast_block_new(parser_ctx(parser));
2194 if (swcase.value) ast_unref(swcase.value);
2195 ast_delete(switchnode);
2198 swcase.code = (ast_expression*)caseblock;
2199 vec_push(switchnode->cases, swcase);
2201 ast_expression *expr;
2202 if (parser->tok == '}')
2204 if (parser->tok == TOKEN_KEYWORD) {
2205 if (!strcmp(parser_tokval(parser), "case") ||
2206 !strcmp(parser_tokval(parser), "default"))
2211 if (!parse_statement(parser, caseblock, &expr, true)) {
2212 ast_delete(switchnode);
2217 ast_block_add_expr(caseblock, expr);
2222 if (parser->tok != '}') {
2223 ast_delete(switchnode);
2224 parseerror(parser, "expected closing paren of case list");
2227 if (!parser_next(parser)) {
2228 ast_delete(switchnode);
2229 parseerror(parser, "parse error after switch");
2232 *out = (ast_expression*)switchnode;
2236 static bool parse_goto(parser_t *parser, ast_expression **out)
2241 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2242 parseerror(parser, "expected label name after `goto`");
2246 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2248 for (i = 0; i < vec_size(parser->labels); ++i) {
2249 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2250 ast_goto_set_label(gt, parser->labels[i]);
2254 if (i == vec_size(parser->labels))
2255 vec_push(parser->gotos, gt);
2257 if (!parser_next(parser) || parser->tok != ';') {
2258 parseerror(parser, "semicolon expected after goto label");
2261 if (!parser_next(parser)) {
2262 parseerror(parser, "parse error after goto");
2266 *out = (ast_expression*)gt;
2270 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2272 ast_value *typevar = NULL;
2275 if (parser->tok == TOKEN_IDENT)
2276 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2278 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2280 /* local variable */
2282 parseerror(parser, "cannot declare a variable from here");
2285 if (opts_standard == COMPILER_QCC) {
2286 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2289 if (!parse_variable(parser, block, false, CV_NONE, typevar))
2294 else if (parser->tok == TOKEN_KEYWORD)
2296 if (!strcmp(parser_tokval(parser), "local") ||
2297 !strcmp(parser_tokval(parser), "const"))
2299 int cvq = parser_tokval(parser)[0] == 'c' ? CV_CONST : CV_VAR;
2302 parseerror(parser, "cannot declare a local variable here");
2305 if (!parser_next(parser)) {
2306 parseerror(parser, "expected variable declaration");
2309 if (!parse_variable(parser, block, true, cvq, NULL))
2314 else if (!strcmp(parser_tokval(parser), "return"))
2316 return parse_return(parser, block, out);
2318 else if (!strcmp(parser_tokval(parser), "if"))
2320 return parse_if(parser, block, out);
2322 else if (!strcmp(parser_tokval(parser), "while"))
2324 return parse_while(parser, block, out);
2326 else if (!strcmp(parser_tokval(parser), "do"))
2328 return parse_dowhile(parser, block, out);
2330 else if (!strcmp(parser_tokval(parser), "for"))
2332 if (opts_standard == COMPILER_QCC) {
2333 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2336 return parse_for(parser, block, out);
2338 else if (!strcmp(parser_tokval(parser), "break"))
2340 return parse_break_continue(parser, block, out, false);
2342 else if (!strcmp(parser_tokval(parser), "continue"))
2344 return parse_break_continue(parser, block, out, true);
2346 else if (!strcmp(parser_tokval(parser), "switch"))
2348 return parse_switch(parser, block, out);
2350 else if (!strcmp(parser_tokval(parser), "case") ||
2351 !strcmp(parser_tokval(parser), "default"))
2354 parseerror(parser, "unexpected 'case' label");
2359 else if (!strcmp(parser_tokval(parser), "goto"))
2361 return parse_goto(parser, out);
2363 else if (!strcmp(parser_tokval(parser), "typedef"))
2365 if (!parser_next(parser)) {
2366 parseerror(parser, "expected type definition after 'typedef'");
2369 return parse_typedef(parser);
2371 parseerror(parser, "Unexpected keyword");
2374 else if (parser->tok == '{')
2377 inner = parse_block(parser, false);
2380 *out = (ast_expression*)inner;
2383 else if (parser->tok == ':')
2387 if (!parser_next(parser)) {
2388 parseerror(parser, "expected label name");
2391 if (parser->tok != TOKEN_IDENT) {
2392 parseerror(parser, "label must be an identifier");
2395 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
2398 vec_push(parser->labels, label);
2399 *out = (ast_expression*)label;
2400 if (!parser_next(parser)) {
2401 parseerror(parser, "parse error after label");
2404 for (i = 0; i < vec_size(parser->gotos); ++i) {
2405 if (!strcmp(parser->gotos[i]->name, label->name)) {
2406 ast_goto_set_label(parser->gotos[i], label);
2407 vec_remove(parser->gotos, i, 1);
2413 else if (parser->tok == ';')
2415 if (!parser_next(parser)) {
2416 parseerror(parser, "parse error after empty statement");
2423 ast_expression *exp = parse_expression(parser, false);
2427 if (!ast_side_effects(exp)) {
2428 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2435 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2439 parser_enterblock(parser);
2441 if (!parser_next(parser)) { /* skip the '{' */
2442 parseerror(parser, "expected function body");
2446 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2448 ast_expression *expr = NULL;
2449 if (parser->tok == '}')
2452 if (!parse_statement(parser, block, &expr, false)) {
2453 /* parseerror(parser, "parse error"); */
2459 ast_block_add_expr(block, expr);
2462 if (parser->tok != '}') {
2465 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2467 if (!vec_size(block->exprs) ||
2468 !ast_istype(vec_last(block->exprs), ast_return))
2470 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2476 (void)parser_next(parser);
2480 if (!parser_leaveblock(parser))
2482 return retval && !!block;
2485 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2488 block = ast_block_new(parser_ctx(parser));
2491 if (!parse_block_into(parser, block, warnreturn)) {
2492 ast_block_delete(block);
2498 static ast_expression* parse_statement_or_block(parser_t *parser)
2500 ast_expression *expr = NULL;
2501 if (parser->tok == '{')
2502 return (ast_expression*)parse_block(parser, false);
2503 if (!parse_statement(parser, NULL, &expr, false))
2508 static bool create_vector_members(ast_value *var, ast_member **me)
2511 size_t len = strlen(var->name);
2513 for (i = 0; i < 3; ++i) {
2514 char *name = mem_a(len+3);
2515 memcpy(name, var->name, len);
2517 name[len+1] = 'x'+i;
2519 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2528 do { ast_member_delete(me[--i]); } while(i);
2532 static bool parse_function_body(parser_t *parser, ast_value *var)
2534 ast_block *block = NULL;
2539 ast_expression *framenum = NULL;
2540 ast_expression *nextthink = NULL;
2541 /* None of the following have to be deleted */
2542 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2543 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2544 bool has_frame_think;
2548 has_frame_think = false;
2549 old = parser->function;
2551 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
2552 parseerror(parser, "gotos/labels leaking");
2556 if (var->expression.variadic) {
2557 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2558 "variadic function with implementation will not be able to access additional parameters"))
2564 if (parser->tok == '[') {
2565 /* got a frame definition: [ framenum, nextthink ]
2566 * this translates to:
2567 * self.frame = framenum;
2568 * self.nextthink = time + 0.1;
2569 * self.think = nextthink;
2573 fld_think = parser_find_field(parser, "think");
2574 fld_nextthink = parser_find_field(parser, "nextthink");
2575 fld_frame = parser_find_field(parser, "frame");
2576 if (!fld_think || !fld_nextthink || !fld_frame) {
2577 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2578 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2581 gbl_time = parser_find_global(parser, "time");
2582 gbl_self = parser_find_global(parser, "self");
2583 if (!gbl_time || !gbl_self) {
2584 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2585 parseerror(parser, "please declare the following globals: `time`, `self`");
2589 if (!parser_next(parser))
2592 framenum = parse_expression_leave(parser, true);
2594 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2597 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
2598 ast_unref(framenum);
2599 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2603 if (parser->tok != ',') {
2604 ast_unref(framenum);
2605 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2606 parseerror(parser, "Got a %i\n", parser->tok);
2610 if (!parser_next(parser)) {
2611 ast_unref(framenum);
2615 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2617 /* qc allows the use of not-yet-declared functions here
2618 * - this automatically creates a prototype */
2619 ast_value *thinkfunc;
2620 ast_expression *functype = fld_think->expression.next;
2622 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2623 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2624 ast_unref(framenum);
2625 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2629 if (!parser_next(parser)) {
2630 ast_unref(framenum);
2631 ast_delete(thinkfunc);
2635 vec_push(parser->globals, (ast_expression*)thinkfunc);
2636 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2637 nextthink = (ast_expression*)thinkfunc;
2640 nextthink = parse_expression_leave(parser, true);
2642 ast_unref(framenum);
2643 parseerror(parser, "expected a think-function in [frame,think] notation");
2648 if (!ast_istype(nextthink, ast_value)) {
2649 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2653 if (retval && parser->tok != ']') {
2654 parseerror(parser, "expected closing `]` for [frame,think] notation");
2658 if (retval && !parser_next(parser)) {
2662 if (retval && parser->tok != '{') {
2663 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2668 ast_unref(nextthink);
2669 ast_unref(framenum);
2673 has_frame_think = true;
2676 block = ast_block_new(parser_ctx(parser));
2678 parseerror(parser, "failed to allocate block");
2679 if (has_frame_think) {
2680 ast_unref(nextthink);
2681 ast_unref(framenum);
2686 if (has_frame_think) {
2688 ast_expression *self_frame;
2689 ast_expression *self_nextthink;
2690 ast_expression *self_think;
2691 ast_expression *time_plus_1;
2692 ast_store *store_frame;
2693 ast_store *store_nextthink;
2694 ast_store *store_think;
2696 ctx = parser_ctx(parser);
2697 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2698 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2699 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2701 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2702 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2704 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2705 if (self_frame) ast_delete(self_frame);
2706 if (self_nextthink) ast_delete(self_nextthink);
2707 if (self_think) ast_delete(self_think);
2708 if (time_plus_1) ast_delete(time_plus_1);
2714 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2715 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2716 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2719 ast_delete(self_frame);
2722 if (!store_nextthink) {
2723 ast_delete(self_nextthink);
2727 ast_delete(self_think);
2731 if (store_frame) ast_delete(store_frame);
2732 if (store_nextthink) ast_delete(store_nextthink);
2733 if (store_think) ast_delete(store_think);
2736 ast_block_add_expr(block, (ast_expression*)store_frame);
2737 ast_block_add_expr(block, (ast_expression*)store_nextthink);
2738 ast_block_add_expr(block, (ast_expression*)store_think);
2742 parseerror(parser, "failed to generate code for [frame,think]");
2743 ast_unref(nextthink);
2744 ast_unref(framenum);
2750 parser_enterblock(parser);
2752 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2754 ast_value *param = var->expression.params[parami];
2757 if (param->expression.vtype != TYPE_VECTOR &&
2758 (param->expression.vtype != TYPE_FIELD ||
2759 param->expression.next->expression.vtype != TYPE_VECTOR))
2764 if (!create_vector_members(param, me)) {
2765 ast_block_delete(block);
2769 for (e = 0; e < 3; ++e) {
2770 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2771 ast_block_collect(block, (ast_expression*)me[e]);
2775 func = ast_function_new(ast_ctx(var), var->name, var);
2777 parseerror(parser, "failed to allocate function for `%s`", var->name);
2778 ast_block_delete(block);
2781 vec_push(parser->functions, func);
2783 parser->function = func;
2784 if (!parse_block_into(parser, block, true)) {
2785 ast_block_delete(block);
2789 vec_push(func->blocks, block);
2791 parser->function = old;
2792 if (!parser_leaveblock(parser))
2794 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2795 parseerror(parser, "internal error: local scopes left");
2799 if (parser->tok == ';')
2800 return parser_next(parser);
2801 else if (opts_standard == COMPILER_QCC)
2802 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2806 vec_pop(parser->functions);
2807 ast_function_delete(func);
2808 var->constval.vfunc = NULL;
2811 (void)!parser_leaveblock(parser);
2812 parser->function = old;
2816 static ast_expression *array_accessor_split(
2821 ast_expression *left,
2822 ast_expression *right
2828 lex_ctx ctx = ast_ctx(array);
2830 if (!left || !right) {
2831 if (left) ast_delete(left);
2832 if (right) ast_delete(right);
2836 cmp = ast_binary_new(ctx, INSTR_LT,
2837 (ast_expression*)index,
2838 (ast_expression*)parser_const_float(parser, middle));
2842 parseerror(parser, "internal error: failed to create comparison for array setter");
2846 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2848 ast_delete(cmp); /* will delete left and right */
2849 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2853 return (ast_expression*)ifthen;
2856 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2858 lex_ctx ctx = ast_ctx(array);
2860 if (from+1 == afterend) {
2861 /* set this value */
2864 ast_array_index *subscript;
2866 int assignop = type_store_instr[value->expression.vtype];
2868 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2869 assignop = INSTR_STORE_V;
2871 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2875 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2877 ast_delete(subscript);
2881 block = ast_block_new(ctx);
2887 ast_block_add_expr(block, (ast_expression*)st);
2889 ret = ast_return_new(ctx, NULL);
2895 ast_block_add_expr(block, (ast_expression*)ret);
2897 return (ast_expression*)block;
2899 ast_expression *left, *right;
2900 size_t diff = afterend - from;
2901 size_t middle = from + diff/2;
2902 left = array_setter_node(parser, array, index, value, from, middle);
2903 right = array_setter_node(parser, array, index, value, middle, afterend);
2904 return array_accessor_split(parser, array, index, middle, left, right);
2908 static ast_expression *array_field_setter_node(
2917 lex_ctx ctx = ast_ctx(array);
2919 if (from+1 == afterend) {
2920 /* set this value */
2923 ast_entfield *entfield;
2924 ast_array_index *subscript;
2926 int assignop = type_storep_instr[value->expression.vtype];
2928 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2929 assignop = INSTR_STOREP_V;
2931 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2935 entfield = ast_entfield_new_force(ctx,
2936 (ast_expression*)entity,
2937 (ast_expression*)subscript,
2938 (ast_expression*)subscript);
2940 ast_delete(subscript);
2944 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2946 ast_delete(entfield);
2950 block = ast_block_new(ctx);
2956 ast_block_add_expr(block, (ast_expression*)st);
2958 ret = ast_return_new(ctx, NULL);
2964 ast_block_add_expr(block, (ast_expression*)ret);
2966 return (ast_expression*)block;
2968 ast_expression *left, *right;
2969 size_t diff = afterend - from;
2970 size_t middle = from + diff/2;
2971 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2972 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2973 return array_accessor_split(parser, array, index, middle, left, right);
2977 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2979 lex_ctx ctx = ast_ctx(array);
2981 if (from+1 == afterend) {
2983 ast_array_index *subscript;
2985 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2989 ret = ast_return_new(ctx, (ast_expression*)subscript);
2991 ast_delete(subscript);
2995 return (ast_expression*)ret;
2997 ast_expression *left, *right;
2998 size_t diff = afterend - from;
2999 size_t middle = from + diff/2;
3000 left = array_getter_node(parser, array, index, from, middle);
3001 right = array_getter_node(parser, array, index, middle, afterend);
3002 return array_accessor_split(parser, array, index, middle, left, right);
3006 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3008 ast_function *func = NULL;
3009 ast_value *fval = NULL;
3010 ast_block *body = NULL;
3012 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3014 parseerror(parser, "failed to create accessor function value");
3018 func = ast_function_new(ast_ctx(array), funcname, fval);
3021 parseerror(parser, "failed to create accessor function node");
3025 body = ast_block_new(ast_ctx(array));
3027 parseerror(parser, "failed to create block for array accessor");
3033 vec_push(func->blocks, body);
3036 vec_push(parser->accessors, fval);
3041 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3043 ast_expression *root = NULL;
3044 ast_value *index = NULL;
3045 ast_value *value = NULL;
3049 if (!ast_istype(array->expression.next, ast_value)) {
3050 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3054 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3056 func = fval->constval.vfunc;
3057 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3059 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3060 value = ast_value_copy((ast_value*)array->expression.next);
3062 if (!index || !value) {
3063 parseerror(parser, "failed to create locals for array accessor");
3066 (void)!ast_value_set_name(value, "value"); /* not important */
3067 vec_push(fval->expression.params, index);
3068 vec_push(fval->expression.params, value);
3070 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3072 parseerror(parser, "failed to build accessor search tree");
3076 ast_block_add_expr(func->blocks[0], root);
3077 array->setter = fval;
3080 if (index) ast_delete(index);
3081 if (value) ast_delete(value);
3082 if (root) ast_delete(root);
3088 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3090 ast_expression *root = NULL;
3091 ast_value *entity = NULL;
3092 ast_value *index = NULL;
3093 ast_value *value = NULL;
3097 if (!ast_istype(array->expression.next, ast_value)) {
3098 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3102 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3104 func = fval->constval.vfunc;
3105 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3107 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3108 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3109 value = ast_value_copy((ast_value*)array->expression.next);
3110 if (!entity || !index || !value) {
3111 parseerror(parser, "failed to create locals for array accessor");
3114 (void)!ast_value_set_name(value, "value"); /* not important */
3115 vec_push(fval->expression.params, entity);
3116 vec_push(fval->expression.params, index);
3117 vec_push(fval->expression.params, value);
3119 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3121 parseerror(parser, "failed to build accessor search tree");
3125 ast_block_add_expr(func->blocks[0], root);
3126 array->setter = fval;
3129 if (entity) ast_delete(entity);
3130 if (index) ast_delete(index);
3131 if (value) ast_delete(value);
3132 if (root) ast_delete(root);
3138 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3140 ast_expression *root = NULL;
3141 ast_value *index = NULL;
3145 /* NOTE: checking array->expression.next rather than elemtype since
3146 * for fields elemtype is a temporary fieldtype.
3148 if (!ast_istype(array->expression.next, ast_value)) {
3149 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3153 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3155 func = fval->constval.vfunc;
3156 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3158 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3161 parseerror(parser, "failed to create locals for array accessor");
3164 vec_push(fval->expression.params, index);
3166 root = array_getter_node(parser, array, index, 0, array->expression.count);
3168 parseerror(parser, "failed to build accessor search tree");
3172 ast_block_add_expr(func->blocks[0], root);
3173 array->getter = fval;
3176 if (index) ast_delete(index);
3177 if (root) ast_delete(root);
3183 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3184 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3192 bool variadic = false;
3194 ctx = parser_ctx(parser);
3196 /* for the sake of less code we parse-in in this function */
3197 if (!parser_next(parser)) {
3198 parseerror(parser, "expected parameter list");
3204 /* parse variables until we hit a closing paren */
3205 while (parser->tok != ')') {
3207 /* there must be commas between them */
3208 if (parser->tok != ',') {
3209 parseerror(parser, "expected comma or end of parameter list");
3212 if (!parser_next(parser)) {
3213 parseerror(parser, "expected parameter");
3219 if (parser->tok == TOKEN_DOTS) {
3220 /* '...' indicates a varargs function */
3222 if (!parser_next(parser)) {
3223 parseerror(parser, "expected parameter");
3226 if (parser->tok != ')') {
3227 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3233 /* for anything else just parse a typename */
3234 param = parse_typename(parser, NULL, NULL);
3237 vec_push(params, param);
3238 if (param->expression.vtype >= TYPE_VARIANT) {
3239 char typename[1024];
3240 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3241 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3247 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3251 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3252 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3255 if (!parser_next(parser)) {
3256 parseerror(parser, "parse error after typename");
3260 /* now turn 'var' into a function type */
3261 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3262 fval->expression.next = (ast_expression*)var;
3263 fval->expression.variadic = variadic;
3266 var->expression.params = params;
3273 for (i = 0; i < vec_size(params); ++i)
3274 ast_delete(params[i]);
3279 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3281 ast_expression *cexp;
3282 ast_value *cval, *tmp;
3285 ctx = parser_ctx(parser);
3287 if (!parser_next(parser)) {
3289 parseerror(parser, "expected array-size");
3293 cexp = parse_expression_leave(parser, true);
3295 if (!cexp || !ast_istype(cexp, ast_value)) {
3299 parseerror(parser, "expected array-size as constant positive integer");
3302 cval = (ast_value*)cexp;
3304 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3305 tmp->expression.next = (ast_expression*)var;
3308 if (cval->expression.vtype == TYPE_INTEGER)
3309 tmp->expression.count = cval->constval.vint;
3310 else if (cval->expression.vtype == TYPE_FLOAT)
3311 tmp->expression.count = cval->constval.vfloat;
3315 parseerror(parser, "array-size must be a positive integer constant");
3320 if (parser->tok != ']') {
3322 parseerror(parser, "expected ']' after array-size");
3325 if (!parser_next(parser)) {
3327 parseerror(parser, "error after parsing array size");
3333 /* Parse a complete typename.
3334 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3335 * but when parsing variables separated by comma
3336 * 'storebase' should point to where the base-type should be kept.
3337 * The base type makes up every bit of type information which comes *before* the
3340 * The following will be parsed in its entirety:
3342 * The 'basetype' in this case is 'void()'
3343 * and if there's a comma after it, say:
3345 * then the type-information 'void()' can be stored in 'storebase'
3347 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3349 ast_value *var, *tmp;
3352 const char *name = NULL;
3353 bool isfield = false;
3354 bool wasarray = false;
3355 size_t morefields = 0;
3357 ctx = parser_ctx(parser);
3359 /* types may start with a dot */
3360 if (parser->tok == '.') {
3362 /* if we parsed a dot we need a typename now */
3363 if (!parser_next(parser)) {
3364 parseerror(parser, "expected typename for field definition");
3368 /* Further dots are handled seperately because they won't be part of the
3371 while (parser->tok == '.') {
3373 if (!parser_next(parser)) {
3374 parseerror(parser, "expected typename for field definition");
3379 if (parser->tok == TOKEN_IDENT)
3380 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
3381 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3382 parseerror(parser, "expected typename");
3387 /* generate the basic type value */
3388 if (cached_typedef) {
3389 var = ast_value_copy(cached_typedef);
3390 ast_value_set_name(var, "<type(from_def)>");
3392 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3394 for (; morefields; --morefields) {
3395 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
3396 tmp->expression.next = (ast_expression*)var;
3400 /* do not yet turn into a field - remember:
3401 * .void() foo; is a field too
3402 * .void()() foo; is a function
3406 if (!parser_next(parser)) {
3408 parseerror(parser, "parse error after typename");
3412 /* an opening paren now starts the parameter-list of a function
3413 * this is where original-QC has parameter lists.
3414 * We allow a single parameter list here.
3415 * Much like fteqcc we don't allow `float()() x`
3417 if (parser->tok == '(') {
3418 var = parse_parameter_list(parser, var);
3423 /* store the base if requested */
3425 *storebase = ast_value_copy(var);
3427 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3428 tmp->expression.next = (ast_expression*)*storebase;
3433 /* there may be a name now */
3434 if (parser->tok == TOKEN_IDENT) {
3435 name = util_strdup(parser_tokval(parser));
3437 if (!parser_next(parser)) {
3439 parseerror(parser, "error after variable or field declaration");
3444 /* now this may be an array */
3445 if (parser->tok == '[') {
3447 var = parse_arraysize(parser, var);
3452 /* This is the point where we can turn it into a field */
3454 /* turn it into a field if desired */
3455 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3456 tmp->expression.next = (ast_expression*)var;
3460 /* now there may be function parens again */
3461 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3462 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3463 if (parser->tok == '(' && wasarray)
3464 parseerror(parser, "arrays as part of a return type is not supported");
3465 while (parser->tok == '(') {
3466 var = parse_parameter_list(parser, var);
3475 /* finally name it */
3477 if (!ast_value_set_name(var, name)) {
3479 parseerror(parser, "internal error: failed to set name");
3482 /* free the name, ast_value_set_name duplicates */
3489 static bool parse_typedef(parser_t *parser)
3491 ast_value *typevar, *oldtype;
3492 ast_expression *old;
3494 typevar = parse_typename(parser, NULL, NULL);
3499 if ( (old = parser_find_var(parser, typevar->name)) ) {
3500 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3501 " -> `%s` has been declared here: %s:%i",
3502 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3503 ast_delete(typevar);
3507 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
3508 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3509 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3510 ast_delete(typevar);
3514 vec_push(parser->_typedefs, typevar);
3515 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3517 if (parser->tok != ';') {
3518 parseerror(parser, "expected semicolon after typedef");
3521 if (!parser_next(parser)) {
3522 parseerror(parser, "parse error after typedef");
3529 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef)
3533 ast_expression *old;
3537 ast_value *basetype = NULL;
3539 bool isparam = false;
3540 bool isvector = false;
3541 bool cleanvar = true;
3542 bool wasarray = false;
3546 /* get the first complete variable */
3547 var = parse_typename(parser, &basetype, cached_typedef);
3550 ast_delete(basetype);
3558 /* Part 0: finish the type */
3559 if (parser->tok == '(') {
3560 if (opts_standard == COMPILER_QCC)
3561 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3562 var = parse_parameter_list(parser, var);
3568 /* we only allow 1-dimensional arrays */
3569 if (parser->tok == '[') {
3571 var = parse_arraysize(parser, var);
3577 if (parser->tok == '(' && wasarray) {
3578 parseerror(parser, "arrays as part of a return type is not supported");
3579 /* we'll still parse the type completely for now */
3581 /* for functions returning functions */
3582 while (parser->tok == '(') {
3583 if (opts_standard == COMPILER_QCC)
3584 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3585 var = parse_parameter_list(parser, var);
3592 if (is_const_var == CV_CONST)
3593 var->constant = true;
3596 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3597 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3598 * is then filled with the previous definition and the parameter-names replaced.
3601 /* Deal with end_sys_ vars */
3603 if (!strcmp(var->name, "end_sys_globals")) {
3604 parser->crc_globals = vec_size(parser->globals);
3607 else if (!strcmp(var->name, "end_sys_fields")) {
3608 parser->crc_fields = vec_size(parser->fields);
3611 if (was_end && var->expression.vtype == TYPE_FIELD) {
3612 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3613 "global '%s' hint should not be a field",
3614 parser_tokval(parser)))
3621 if (!nofields && var->expression.vtype == TYPE_FIELD)
3623 /* deal with field declarations */
3624 old = parser_find_field(parser, var->name);
3626 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3627 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3636 parseerror(parser, "field `%s` already declared here: %s:%i",
3637 var->name, ast_ctx(old).file, ast_ctx(old).line);
3642 if (opts_standard == COMPILER_QCC &&
3643 (old = parser_find_global(parser, var->name)))
3645 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3646 parseerror(parser, "field `%s` already declared here: %s:%i",
3647 var->name, ast_ctx(old).file, ast_ctx(old).line);
3654 /* deal with other globals */
3655 old = parser_find_global(parser, var->name);
3656 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3658 /* This is a function which had a prototype */
3659 if (!ast_istype(old, ast_value)) {
3660 parseerror(parser, "internal error: prototype is not an ast_value");
3664 proto = (ast_value*)old;
3665 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3666 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3668 ast_ctx(proto).file, ast_ctx(proto).line);
3672 /* we need the new parameter-names */
3673 for (i = 0; i < vec_size(proto->expression.params); ++i)
3674 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3682 if (opts_standard == COMPILER_GMQCC) {
3683 parseerror(parser, "global `%s` already declared here: %s:%i",
3684 var->name, ast_ctx(old).file, ast_ctx(old).line);
3688 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3689 "global `%s` already declared here: %s:%i",
3690 var->name, ast_ctx(old).file, ast_ctx(old).line))
3697 if (opts_standard == COMPILER_QCC &&
3698 (old = parser_find_field(parser, var->name)))
3700 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3701 parseerror(parser, "global `%s` already declared here: %s:%i",
3702 var->name, ast_ctx(old).file, ast_ctx(old).line);
3709 else /* it's not a global */
3711 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3712 if (old && !isparam) {
3713 parseerror(parser, "local `%s` already declared here: %s:%i",
3714 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3718 old = parser_find_local(parser, var->name, 0, &isparam);
3719 if (old && isparam) {
3720 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3721 "local `%s` is shadowing a parameter", var->name))
3723 parseerror(parser, "local `%s` already declared here: %s:%i",
3724 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3728 if (opts_standard != COMPILER_GMQCC) {
3737 * Create the global/local, and deal with vector types.
3740 if (var->expression.vtype == TYPE_VECTOR)
3742 else if (var->expression.vtype == TYPE_FIELD &&
3743 var->expression.next->expression.vtype == TYPE_VECTOR)
3747 if (!create_vector_members(var, me)) {
3754 /* deal with global variables, fields, functions */
3755 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3756 vec_push(parser->fields, (ast_expression*)var);
3757 util_htset(parser->htfields, var->name, var);
3759 for (i = 0; i < 3; ++i) {
3760 vec_push(parser->fields, (ast_expression*)me[i]);
3761 util_htset(parser->htfields, me[i]->name, me[i]);
3766 vec_push(parser->globals, (ast_expression*)var);
3767 util_htset(parser->htglobals, var->name, var);
3769 for (i = 0; i < 3; ++i) {
3770 vec_push(parser->globals, (ast_expression*)me[i]);
3771 util_htset(parser->htglobals, me[i]->name, me[i]);
3776 vec_push(localblock->locals, var);
3777 parser_addlocal(parser, var->name, (ast_expression*)var);
3779 for (i = 0; i < 3; ++i) {
3780 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3781 ast_block_collect(localblock, (ast_expression*)me[i]);
3787 me[0] = me[1] = me[2] = NULL;
3792 if (var->expression.vtype == TYPE_ARRAY) {
3794 snprintf(name, sizeof(name), "%s##SET", var->name);
3795 if (!parser_create_array_setter(parser, var, name))
3797 snprintf(name, sizeof(name), "%s##GET", var->name);
3798 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3801 else if (!localblock && !nofields &&
3802 var->expression.vtype == TYPE_FIELD &&
3803 var->expression.next->expression.vtype == TYPE_ARRAY)
3806 ast_expression *telem;
3808 ast_value *array = (ast_value*)var->expression.next;
3810 if (!ast_istype(var->expression.next, ast_value)) {
3811 parseerror(parser, "internal error: field element type must be an ast_value");
3815 snprintf(name, sizeof(name), "%s##SETF", var->name);
3816 if (!parser_create_array_field_setter(parser, array, name))
3819 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3820 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3821 tfield->expression.next = telem;
3822 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3823 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3831 if (parser->tok == ';') {
3832 ast_delete(basetype);
3833 if (!parser_next(parser)) {
3834 parseerror(parser, "error after variable declaration");
3840 if (parser->tok == ',')
3843 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3844 parseerror(parser, "missing comma or semicolon while parsing variables");
3848 if (localblock && opts_standard == COMPILER_QCC) {
3849 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3850 "initializing expression turns variable `%s` into a constant in this standard",
3857 if (parser->tok != '{') {
3858 if (parser->tok != '=') {
3859 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
3863 if (!parser_next(parser)) {
3864 parseerror(parser, "error parsing initializer");
3868 else if (opts_standard == COMPILER_QCC) {
3869 parseerror(parser, "expected '=' before function body in this standard");
3872 if (parser->tok == '#') {
3873 ast_function *func = NULL;
3876 parseerror(parser, "cannot declare builtins within functions");
3879 if (var->expression.vtype != TYPE_FUNCTION) {
3880 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3883 if (!parser_next(parser)) {
3884 parseerror(parser, "expected builtin number");
3887 if (parser->tok != TOKEN_INTCONST) {
3888 parseerror(parser, "builtin number must be an integer constant");
3891 if (parser_token(parser)->constval.i <= 0) {
3892 parseerror(parser, "builtin number must be an integer greater than zero");
3896 if (var->hasvalue) {
3897 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
3898 "builtin `%s` has already been defined\n"
3899 " -> previous declaration here: %s:%i",
3900 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
3904 func = ast_function_new(ast_ctx(var), var->name, var);
3906 parseerror(parser, "failed to allocate function for `%s`", var->name);
3909 vec_push(parser->functions, func);
3911 func->builtin = -parser_token(parser)->constval.i;
3914 if (!parser_next(parser)) {
3915 parseerror(parser, "expected comma or semicolon");
3917 ast_function_delete(func);
3918 var->constval.vfunc = NULL;
3922 else if (parser->tok == '{' || parser->tok == '[')
3926 parseerror(parser, "cannot declare functions within functions");
3930 if (!parse_function_body(parser, var))
3932 ast_delete(basetype);
3933 for (i = 0; i < vec_size(parser->gotos); ++i)
3934 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
3935 vec_free(parser->gotos);
3936 vec_free(parser->labels);
3939 ast_expression *cexp;
3942 cexp = parse_expression_leave(parser, true);
3947 cval = (ast_value*)cexp;
3948 if (!ast_istype(cval, ast_value) || !cval->hasvalue || !cval->constant)
3949 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3952 if (opts_standard != COMPILER_GMQCC &&
3953 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
3954 is_const_var != CV_VAR)
3956 var->constant = true;
3958 var->hasvalue = true;
3959 if (cval->expression.vtype == TYPE_STRING)
3960 var->constval.vstring = parser_strdup(cval->constval.vstring);
3962 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3967 shunt sy = { NULL, NULL };
3968 cvq = var->constant;
3969 var->constant = false;
3970 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
3971 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
3972 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
3973 if (!parser_sy_pop(parser, &sy))
3976 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
3977 parseerror(parser, "internal error: leaked operands");
3978 ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
3982 var->constant = cvq;
3987 if (parser->tok == ',') {
3988 if (!parser_next(parser)) {
3989 parseerror(parser, "expected another variable");
3993 if (parser->tok != TOKEN_IDENT) {
3994 parseerror(parser, "expected another variable");
3997 var = ast_value_copy(basetype);
3999 ast_value_set_name(var, parser_tokval(parser));
4000 if (!parser_next(parser)) {
4001 parseerror(parser, "error parsing variable declaration");
4007 if (parser->tok != ';') {
4008 parseerror(parser, "missing semicolon after variables");
4012 if (!parser_next(parser)) {
4013 parseerror(parser, "parse error after variable declaration");
4017 ast_delete(basetype);
4021 if (cleanvar && var)
4023 ast_delete(basetype);
4027 ast_delete(basetype);
4028 if (cleanvar && var)
4030 if (me[0]) ast_member_delete(me[0]);
4031 if (me[1]) ast_member_delete(me[1]);
4032 if (me[2]) ast_member_delete(me[2]);
4036 static bool parser_global_statement(parser_t *parser)
4038 ast_value *istype = NULL;
4039 if (parser->tok == TOKEN_IDENT)
4040 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4042 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4044 return parse_variable(parser, NULL, false, CV_NONE, istype);
4046 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
4048 if (!strcmp(parser_tokval(parser), "var")) {
4049 if (!parser_next(parser)) {
4050 parseerror(parser, "expected variable declaration after 'var'");
4053 return parse_variable(parser, NULL, true, CV_VAR, NULL);
4056 else if (parser->tok == TOKEN_KEYWORD)
4058 if (!strcmp(parser_tokval(parser), "const")) {
4059 if (!parser_next(parser)) {
4060 parseerror(parser, "expected variable declaration after 'const'");
4063 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var")) {
4064 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `var` after const qualifier");
4065 if (!parser_next(parser)) {
4066 parseerror(parser, "expected variable declaration after 'const var'");
4070 return parse_variable(parser, NULL, true, CV_CONST, NULL);
4072 else if (!strcmp(parser_tokval(parser), "typedef")) {
4073 if (!parser_next(parser)) {
4074 parseerror(parser, "expected type definition after 'typedef'");
4077 return parse_typedef(parser);
4079 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4082 else if (parser->tok == '$')
4084 if (!parser_next(parser)) {
4085 parseerror(parser, "parse error");
4091 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4097 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4099 return util_crc16(old, str, strlen(str));
4102 static void progdefs_crc_file(const char *str)
4104 /* write to progdefs.h here */
4108 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4110 old = progdefs_crc_sum(old, str);
4111 progdefs_crc_file(str);
4115 static void generate_checksum(parser_t *parser)
4117 uint16_t crc = 0xFFFF;
4121 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4122 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4124 progdefs_crc_file("\tint\tpad;\n");
4125 progdefs_crc_file("\tint\tofs_return[3];\n");
4126 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4127 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4128 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4129 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4130 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4131 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4132 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4133 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4135 for (i = 0; i < parser->crc_globals; ++i) {
4136 if (!ast_istype(parser->globals[i], ast_value))
4138 value = (ast_value*)(parser->globals[i]);
4139 switch (value->expression.vtype) {
4140 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4141 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4142 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4143 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4145 crc = progdefs_crc_both(crc, "\tint\t");
4148 crc = progdefs_crc_both(crc, value->name);
4149 crc = progdefs_crc_both(crc, ";\n");
4151 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
4152 for (i = 0; i < parser->crc_fields; ++i) {
4153 if (!ast_istype(parser->fields[i], ast_value))
4155 value = (ast_value*)(parser->fields[i]);
4156 switch (value->expression.next->expression.vtype) {
4157 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4158 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4159 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4160 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4162 crc = progdefs_crc_both(crc, "\tint\t");
4165 crc = progdefs_crc_both(crc, value->name);
4166 crc = progdefs_crc_both(crc, ";\n");
4168 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
4173 static parser_t *parser;
4179 parser = (parser_t*)mem_a(sizeof(parser_t));
4183 memset(parser, 0, sizeof(*parser));
4185 for (i = 0; i < operator_count; ++i) {
4186 if (operators[i].id == opid1('=')) {
4187 parser->assign_op = operators+i;
4191 if (!parser->assign_op) {
4192 printf("internal error: initializing parser: failed to find assign operator\n");
4197 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
4198 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
4199 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
4200 vec_push(parser->_blocktypedefs, 0);
4204 bool parser_compile()
4206 /* initial lexer/parser state */
4207 parser->lex->flags.noops = true;
4209 if (parser_next(parser))
4211 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4213 if (!parser_global_statement(parser)) {
4214 if (parser->tok == TOKEN_EOF)
4215 parseerror(parser, "unexpected eof");
4216 else if (!parser->errors)
4217 parseerror(parser, "there have been errors, bailing out");
4218 lex_close(parser->lex);
4224 parseerror(parser, "parse error");
4225 lex_close(parser->lex);
4230 lex_close(parser->lex);
4233 return !parser->errors;
4236 bool parser_compile_file(const char *filename)
4238 parser->lex = lex_open(filename);
4240 con_err("failed to open file \"%s\"\n", filename);
4243 return parser_compile();
4246 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4248 parser->lex = lex_open_string(str, len, name);
4250 con_err("failed to create lexer for string \"%s\"\n", name);
4253 return parser_compile();
4256 bool parser_compile_string(const char *name, const char *str)
4258 parser->lex = lex_open_string(str, strlen(str), name);
4260 con_err("failed to create lexer for string \"%s\"\n", name);
4263 return parser_compile();
4266 void parser_cleanup()
4269 for (i = 0; i < vec_size(parser->accessors); ++i) {
4270 ast_delete(parser->accessors[i]->constval.vfunc);
4271 parser->accessors[i]->constval.vfunc = NULL;
4272 ast_delete(parser->accessors[i]);
4274 for (i = 0; i < vec_size(parser->functions); ++i) {
4275 ast_delete(parser->functions[i]);
4277 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4278 ast_delete(parser->imm_vector[i]);
4280 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4281 ast_delete(parser->imm_string[i]);
4283 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4284 ast_delete(parser->imm_float[i]);
4286 for (i = 0; i < vec_size(parser->fields); ++i) {
4287 ast_delete(parser->fields[i]);
4289 for (i = 0; i < vec_size(parser->globals); ++i) {
4290 ast_delete(parser->globals[i]);
4292 vec_free(parser->accessors);
4293 vec_free(parser->functions);
4294 vec_free(parser->imm_vector);
4295 vec_free(parser->imm_string);
4296 vec_free(parser->imm_float);
4297 vec_free(parser->globals);
4298 vec_free(parser->fields);
4300 for (i = 0; i < vec_size(parser->variables); ++i)
4301 util_htdel(parser->variables[i]);
4302 vec_free(parser->variables);
4303 vec_free(parser->_blocklocals);
4304 vec_free(parser->_locals);
4306 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4307 ast_delete(parser->_typedefs[i]);
4308 vec_free(parser->_typedefs);
4309 for (i = 0; i < vec_size(parser->typedefs); ++i)
4310 util_htdel(parser->typedefs[i]);
4311 vec_free(parser->typedefs);
4312 vec_free(parser->_blocktypedefs);
4314 vec_free(parser->labels);
4315 vec_free(parser->gotos);
4320 bool parser_finish(const char *output)
4326 if (!parser->errors)
4328 ir = ir_builder_new("gmqcc_out");
4330 con_out("failed to allocate builder\n");
4334 for (i = 0; i < vec_size(parser->fields); ++i) {
4337 if (!ast_istype(parser->fields[i], ast_value))
4339 field = (ast_value*)parser->fields[i];
4340 hasvalue = field->hasvalue;
4341 field->hasvalue = false;
4342 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4343 con_out("failed to generate field %s\n", field->name);
4344 ir_builder_delete(ir);
4349 ast_expression *subtype;
4350 field->hasvalue = true;
4351 subtype = field->expression.next;
4352 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4353 if (subtype->expression.vtype == TYPE_FIELD)
4354 ifld->fieldtype = subtype->expression.next->expression.vtype;
4355 else if (subtype->expression.vtype == TYPE_FUNCTION)
4356 ifld->outtype = subtype->expression.next->expression.vtype;
4357 (void)!ir_value_set_field(field->ir_v, ifld);
4360 for (i = 0; i < vec_size(parser->globals); ++i) {
4362 if (!ast_istype(parser->globals[i], ast_value))
4364 asvalue = (ast_value*)(parser->globals[i]);
4365 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
4366 if (strcmp(asvalue->name, "end_sys_globals") &&
4367 strcmp(asvalue->name, "end_sys_fields"))
4369 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4370 "unused global: `%s`", asvalue->name);
4373 if (!ast_global_codegen(asvalue, ir, false)) {
4374 con_out("failed to generate global %s\n", asvalue->name);
4375 ir_builder_delete(ir);
4379 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4380 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4381 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4382 ir_builder_delete(ir);
4386 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4387 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4388 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4389 ir_builder_delete(ir);
4393 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4394 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4395 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4396 ir_builder_delete(ir);
4400 for (i = 0; i < vec_size(parser->globals); ++i) {
4402 if (!ast_istype(parser->globals[i], ast_value))
4404 asvalue = (ast_value*)(parser->globals[i]);
4405 if (asvalue->setter) {
4406 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4407 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4408 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4410 printf("failed to generate setter for %s\n", asvalue->name);
4411 ir_builder_delete(ir);
4415 if (asvalue->getter) {
4416 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4417 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4418 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4420 printf("failed to generate getter for %s\n", asvalue->name);
4421 ir_builder_delete(ir);
4426 for (i = 0; i < vec_size(parser->fields); ++i) {
4428 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4430 if (!ast_istype((ast_expression*)asvalue, ast_value))
4432 if (asvalue->expression.vtype != TYPE_ARRAY)
4434 if (asvalue->setter) {
4435 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4436 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4437 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4439 printf("failed to generate setter for %s\n", asvalue->name);
4440 ir_builder_delete(ir);
4444 if (asvalue->getter) {
4445 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4446 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4447 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4449 printf("failed to generate getter for %s\n", asvalue->name);
4450 ir_builder_delete(ir);
4455 for (i = 0; i < vec_size(parser->functions); ++i) {
4456 if (!ast_function_codegen(parser->functions[i], ir)) {
4457 con_out("failed to generate function %s\n", parser->functions[i]->name);
4458 ir_builder_delete(ir);
4463 ir_builder_dump(ir, con_out);
4464 for (i = 0; i < vec_size(parser->functions); ++i) {
4465 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4466 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4467 ir_builder_delete(ir);
4474 ir_builder_dump(ir, con_out);
4476 generate_checksum(parser);
4478 if (!ir_builder_generate(ir, output)) {
4479 con_out("*** failed to generate output file\n");
4480 ir_builder_delete(ir);
4485 ir_builder_delete(ir);
4489 con_out("*** there were compile errors\n");