18 ast_function **functions;
19 ast_value **imm_float;
20 ast_value **imm_string;
21 ast_value **imm_vector;
23 /* must be deleted first, they reference immediates and values */
24 ast_value **accessors;
26 ast_value *imm_float_zero;
27 ast_value *imm_vector_zero;
32 ast_function *function;
38 /* we store the '=' operator info */
39 const oper_info *assign_op;
41 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
42 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
43 * anything else: type error
49 static bool GMQCC_WARN parser_pop_local(parser_t *parser);
50 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields);
51 static ast_block* parse_block(parser_t *parser, bool warnreturn);
52 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
53 static ast_expression* parse_statement_or_block(parser_t *parser);
54 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
55 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
57 static void parseerror(parser_t *parser, const char *fmt, ...)
64 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
68 /* returns true if it counts as an error */
69 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
72 int lvl = LVL_WARNING;
74 if (!OPTS_WARN(warntype))
83 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
89 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
92 int lvl = LVL_WARNING;
94 if (!OPTS_WARN(warntype))
101 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
107 /**********************************************************************
108 * some maths used for constant folding
111 vector vec3_add(vector a, vector b)
120 vector vec3_sub(vector a, vector b)
129 qcfloat vec3_mulvv(vector a, vector b)
131 return (a.x * b.x + a.y * b.y + a.z * b.z);
134 vector vec3_mulvf(vector a, float b)
143 /**********************************************************************
147 bool parser_next(parser_t *parser)
149 /* lex_do kills the previous token */
150 parser->tok = lex_do(parser->lex);
151 if (parser->tok == TOKEN_EOF)
153 if (parser->tok >= TOKEN_ERROR) {
154 parseerror(parser, "lex error");
160 #define parser_tokval(p) ((p)->lex->tok.value)
161 #define parser_token(p) (&((p)->lex->tok))
162 #define parser_ctx(p) ((p)->lex->tok.ctx)
164 static ast_value* parser_const_float(parser_t *parser, double d)
168 for (i = 0; i < vec_size(parser->imm_float); ++i) {
169 if (parser->imm_float[i]->constval.vfloat == d)
170 return parser->imm_float[i];
172 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
174 out->constval.vfloat = d;
175 vec_push(parser->imm_float, out);
179 static ast_value* parser_const_float_0(parser_t *parser)
181 if (!parser->imm_float_zero)
182 parser->imm_float_zero = parser_const_float(parser, 0);
183 return parser->imm_float_zero;
186 static char *parser_strdup(const char *str)
189 /* actually dup empty strings */
190 char *out = mem_a(1);
194 return util_strdup(str);
197 static ast_value* parser_const_string(parser_t *parser, const char *str)
201 for (i = 0; i < vec_size(parser->imm_string); ++i) {
202 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
203 return parser->imm_string[i];
205 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
207 out->constval.vstring = parser_strdup(str);
208 vec_push(parser->imm_string, out);
212 static ast_value* parser_const_vector(parser_t *parser, vector v)
216 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
217 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
218 return parser->imm_vector[i];
220 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
222 out->constval.vvec = v;
223 vec_push(parser->imm_vector, out);
227 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
233 return parser_const_vector(parser, v);
236 static ast_value* parser_const_vector_0(parser_t *parser)
238 if (!parser->imm_vector_zero)
239 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
240 return parser->imm_vector_zero;
243 static ast_expression* parser_find_field(parser_t *parser, const char *name)
246 for (i = 0; i < vec_size(parser->fields); ++i) {
247 if (!strcmp(parser->fields[i].name, name))
248 return parser->fields[i].var;
253 static ast_expression* parser_find_global(parser_t *parser, const char *name)
256 for (i = 0; i < vec_size(parser->globals); ++i) {
257 if (!strcmp(parser->globals[i].name, name))
258 return parser->globals[i].var;
263 static ast_expression* parser_find_param(parser_t *parser, const char *name)
267 if (!parser->function)
269 fun = parser->function->vtype;
270 for (i = 0; i < vec_size(fun->expression.params); ++i) {
271 if (!strcmp(fun->expression.params[i]->name, name))
272 return (ast_expression*)(fun->expression.params[i]);
277 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
281 for (i = vec_size(parser->locals); i > upto;) {
283 if (!strcmp(parser->locals[i].name, name))
284 return parser->locals[i].var;
287 return parser_find_param(parser, name);
290 static ast_expression* parser_find_var(parser_t *parser, const char *name)
294 v = parser_find_local(parser, name, 0, &dummy);
295 if (!v) v = parser_find_global(parser, name);
301 size_t etype; /* 0 = expression, others are operators */
305 ast_block *block; /* for commas and function calls */
314 #define SY_PAREN_EXPR '('
315 #define SY_PAREN_FUNC 'f'
316 #define SY_PAREN_INDEX '['
318 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
329 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
333 e.out = (ast_expression*)v;
340 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
342 e.etype = 1 + (op - operators);
351 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
363 # define DEBUGSHUNTDO(x) x
365 # define DEBUGSHUNTDO(x)
368 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
369 * so we need to rotate it to become ent.(foo[n]).
371 static bool rotate_entfield_array_index_nodes(ast_expression **out)
373 ast_array_index *index;
374 ast_entfield *entfield;
378 ast_expression *entity;
380 lex_ctx ctx = ast_ctx(*out);
382 if (!ast_istype(*out, ast_array_index))
384 index = (ast_array_index*)*out;
386 if (!ast_istype(index->array, ast_entfield))
388 entfield = (ast_entfield*)index->array;
390 if (!ast_istype(entfield->field, ast_value))
392 field = (ast_value*)entfield->field;
395 entity = entfield->entity;
399 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
400 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
401 *out = (ast_expression*)entfield;
406 static bool parser_sy_pop(parser_t *parser, shunt *sy)
410 ast_expression *out = NULL;
411 ast_expression *exprs[3];
412 ast_block *blocks[3];
413 ast_value *asvalue[3];
415 qcint generated_op = 0;
420 if (!vec_size(sy->ops)) {
421 parseerror(parser, "internal error: missing operator");
425 if (sy->ops[vec_size(sy->ops)-1].paren) {
426 parseerror(parser, "unmatched parenthesis");
430 op = &operators[sy->ops[vec_size(sy->ops)-1].etype - 1];
431 ctx = sy->ops[vec_size(sy->ops)-1].ctx;
433 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
435 if (vec_size(sy->out) < op->operands) {
436 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
437 op->op, (int)op->id);
441 vec_shrinkby(sy->ops, 1);
443 vec_shrinkby(sy->out, op->operands);
444 for (i = 0; i < op->operands; ++i) {
445 exprs[i] = sy->out[vec_size(sy->out)+i].out;
446 blocks[i] = sy->out[vec_size(sy->out)+i].block;
447 asvalue[i] = (ast_value*)exprs[i];
450 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
451 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
455 #define NotSameType(T) \
456 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
457 exprs[0]->expression.vtype != T)
458 #define CanConstFold1(A) \
459 (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
460 #define CanConstFold(A, B) \
461 (CanConstFold1(A) && CanConstFold1(B))
462 #define ConstV(i) (asvalue[(i)]->constval.vvec)
463 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
464 #define ConstS(i) (asvalue[(i)]->constval.vstring)
468 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
472 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
473 if (exprs[1]->expression.vtype != TYPE_FIELD) {
474 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
477 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
479 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
480 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
484 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
490 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
491 !(exprs[0]->expression.vtype == TYPE_FIELD &&
492 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
494 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
495 parseerror(parser, "cannot index value of type %s", ty1);
498 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
499 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
500 parseerror(parser, "index must be of type float, not %s", ty1);
503 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
504 if (rotate_entfield_array_index_nodes(&out))
506 if (opts_standard != COMPILER_GMQCC) {
507 /* this error doesn't need to make us bail out */
508 (void)!parsewarning(parser, WARN_EXTENSIONS,
509 "accessing array-field members of an entity without parenthesis\n"
510 " -> this is an extension from -std=gmqcc");
517 vec_push(blocks[0]->exprs, exprs[1]);
519 blocks[0] = ast_block_new(ctx);
520 vec_push(blocks[0]->exprs, exprs[0]);
521 vec_push(blocks[0]->exprs, exprs[1]);
523 if (!ast_block_set_type(blocks[0], exprs[1]))
526 vec_push(sy->out, syblock(ctx, blocks[0]));
530 switch (exprs[0]->expression.vtype) {
532 if (CanConstFold1(exprs[0]))
533 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
535 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
536 (ast_expression*)parser_const_float_0(parser),
540 if (CanConstFold1(exprs[0]))
541 out = (ast_expression*)parser_const_vector_f(parser,
542 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
544 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
545 (ast_expression*)parser_const_vector_0(parser),
549 parseerror(parser, "invalid types used in expression: cannot negate type %s",
550 type_name[exprs[0]->expression.vtype]);
556 switch (exprs[0]->expression.vtype) {
558 if (CanConstFold1(exprs[0]))
559 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
561 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
564 if (CanConstFold1(exprs[0]))
565 out = (ast_expression*)parser_const_float(parser,
566 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
568 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
571 if (CanConstFold1(exprs[0]))
572 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
574 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
576 /* we don't constant-fold NOT for these types */
578 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
581 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
584 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
585 type_name[exprs[0]->expression.vtype]);
591 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
592 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
594 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
595 type_name[exprs[0]->expression.vtype],
596 type_name[exprs[1]->expression.vtype]);
599 switch (exprs[0]->expression.vtype) {
601 if (CanConstFold(exprs[0], exprs[1]))
603 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
606 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
609 if (CanConstFold(exprs[0], exprs[1]))
610 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
612 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
615 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
616 type_name[exprs[0]->expression.vtype],
617 type_name[exprs[1]->expression.vtype]);
622 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
623 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
625 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
626 type_name[exprs[1]->expression.vtype],
627 type_name[exprs[0]->expression.vtype]);
630 switch (exprs[0]->expression.vtype) {
632 if (CanConstFold(exprs[0], exprs[1]))
633 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
635 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
638 if (CanConstFold(exprs[0], exprs[1]))
639 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
641 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
644 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
645 type_name[exprs[1]->expression.vtype],
646 type_name[exprs[0]->expression.vtype]);
651 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
652 exprs[0]->expression.vtype != TYPE_VECTOR &&
653 exprs[0]->expression.vtype != TYPE_FLOAT &&
654 exprs[1]->expression.vtype != TYPE_VECTOR &&
655 exprs[1]->expression.vtype != TYPE_FLOAT)
657 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
658 type_name[exprs[1]->expression.vtype],
659 type_name[exprs[0]->expression.vtype]);
662 switch (exprs[0]->expression.vtype) {
664 if (exprs[1]->expression.vtype == TYPE_VECTOR)
666 if (CanConstFold(exprs[0], exprs[1]))
667 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
669 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
673 if (CanConstFold(exprs[0], exprs[1]))
674 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
676 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
680 if (exprs[1]->expression.vtype == TYPE_FLOAT)
682 if (CanConstFold(exprs[0], exprs[1]))
683 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
685 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
689 if (CanConstFold(exprs[0], exprs[1]))
690 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
692 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
696 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
697 type_name[exprs[1]->expression.vtype],
698 type_name[exprs[0]->expression.vtype]);
703 if (NotSameType(TYPE_FLOAT)) {
704 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
705 type_name[exprs[0]->expression.vtype],
706 type_name[exprs[1]->expression.vtype]);
709 if (CanConstFold(exprs[0], exprs[1]))
710 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
712 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
716 parseerror(parser, "qc does not have a modulo operator");
720 if (NotSameType(TYPE_FLOAT)) {
721 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
722 type_name[exprs[0]->expression.vtype],
723 type_name[exprs[1]->expression.vtype]);
726 if (CanConstFold(exprs[0], exprs[1]))
727 out = (ast_expression*)parser_const_float(parser,
728 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
729 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
731 out = (ast_expression*)ast_binary_new(ctx,
732 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
736 parseerror(parser, "TODO: bitxor");
741 case opid3('<','<','='):
742 case opid3('>','>','='):
743 parseerror(parser, "TODO: shifts");
747 generated_op += 1; /* INSTR_OR */
749 generated_op += INSTR_AND;
750 if (NotSameType(TYPE_FLOAT)) {
751 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
752 type_name[exprs[0]->expression.vtype],
753 type_name[exprs[1]->expression.vtype]);
754 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
755 parseerror(parser, "TODO: optional early out");
758 if (opts_standard == COMPILER_GMQCC)
759 con_out("TODO: early out logic\n");
760 if (CanConstFold(exprs[0], exprs[1]))
761 out = (ast_expression*)parser_const_float(parser,
762 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
764 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
768 generated_op += 1; /* INSTR_GT */
770 generated_op += 1; /* INSTR_LT */
771 case opid2('>', '='):
772 generated_op += 1; /* INSTR_GE */
773 case opid2('<', '='):
774 generated_op += INSTR_LE;
775 if (NotSameType(TYPE_FLOAT)) {
776 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
777 type_name[exprs[0]->expression.vtype],
778 type_name[exprs[1]->expression.vtype]);
781 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
783 case opid2('!', '='):
784 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
785 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
786 type_name[exprs[0]->expression.vtype],
787 type_name[exprs[1]->expression.vtype]);
790 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
792 case opid2('=', '='):
793 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
794 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
795 type_name[exprs[0]->expression.vtype],
796 type_name[exprs[1]->expression.vtype]);
799 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
803 if (ast_istype(exprs[0], ast_entfield)) {
804 ast_expression *field = ((ast_entfield*)exprs[0])->field;
805 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
806 exprs[0]->expression.vtype == TYPE_FIELD &&
807 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
809 assignop = type_storep_instr[TYPE_VECTOR];
812 assignop = type_storep_instr[exprs[0]->expression.vtype];
813 if (!ast_compare_type(field->expression.next, exprs[1])) {
814 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
815 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
816 if (opts_standard == COMPILER_QCC &&
817 field->expression.next->expression.vtype == TYPE_FUNCTION &&
818 exprs[1]->expression.vtype == TYPE_FUNCTION)
820 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
821 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
827 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
832 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
833 exprs[0]->expression.vtype == TYPE_FIELD &&
834 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
836 assignop = type_store_instr[TYPE_VECTOR];
839 assignop = type_store_instr[exprs[0]->expression.vtype];
842 if (assignop == AINSTR_END) {
843 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
844 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
845 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
847 else if (!ast_compare_type(exprs[0], exprs[1])) {
848 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
849 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
850 if (opts_standard == COMPILER_QCC &&
851 exprs[0]->expression.vtype == TYPE_FUNCTION &&
852 exprs[1]->expression.vtype == TYPE_FUNCTION)
854 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
855 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
861 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
864 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
868 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
869 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
871 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
872 type_name[exprs[0]->expression.vtype],
873 type_name[exprs[1]->expression.vtype]);
876 if (ast_istype(exprs[0], ast_entfield))
877 assignop = type_storep_instr[exprs[0]->expression.vtype];
879 assignop = type_store_instr[exprs[0]->expression.vtype];
880 switch (exprs[0]->expression.vtype) {
882 out = (ast_expression*)ast_binstore_new(ctx, assignop,
883 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
887 out = (ast_expression*)ast_binstore_new(ctx, assignop,
888 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
892 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
893 type_name[exprs[0]->expression.vtype],
894 type_name[exprs[1]->expression.vtype]);
902 parseerror(parser, "failed to apply operand %s", op->op);
906 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
907 vec_push(sy->out, syexp(ctx, out));
911 static bool parser_close_call(parser_t *parser, shunt *sy)
913 /* was a function call */
920 vec_shrinkby(sy->ops, 1);
921 fid = sy->ops[vec_size(sy->ops)].off;
923 /* out[fid] is the function
924 * everything above is parameters...
926 * 1 params = ast_expression
930 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
931 parseerror(parser, "internal error: function call needs function and parameter list...");
935 fun = sy->out[fid].out;
937 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
939 parseerror(parser, "out of memory");
943 if (fid+1 == vec_size(sy->out)) {
946 } else if (fid+2 == vec_size(sy->out)) {
948 vec_shrinkby(sy->out, 1);
949 params = sy->out[vec_size(sy->out)].block;
953 vec_push(call->params, sy->out[vec_size(sy->out)].out);
955 paramcount = vec_size(params->exprs);
956 call->params = params->exprs;
957 params->exprs = NULL;
960 if (!ast_call_check_types(call))
963 parseerror(parser, "invalid function call");
967 /* overwrite fid, the function, with a call */
968 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
970 if (fun->expression.vtype != TYPE_FUNCTION) {
971 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
975 if (!fun->expression.next) {
976 parseerror(parser, "could not determine function return type");
979 if (vec_size(fun->expression.params) != paramcount &&
980 !(fun->expression.variadic &&
981 vec_size(fun->expression.params) < paramcount))
984 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
986 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
987 if (opts_standard == COMPILER_GMQCC)
990 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
991 " -> `%s` has been declared here: %s:%i",
992 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
993 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
995 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
996 " -> `%s` has been declared here: %s:%i",
997 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
998 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1004 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1005 "too %s parameters for call to %s: expected %i, got %i\n"
1006 " -> `%s` has been declared here: %s:%i",
1007 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1008 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1010 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1011 "too %s parameters for function call: expected %i, got %i\n"
1012 " -> `%s` has been declared here: %s:%i",
1013 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1014 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1022 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1024 if (!vec_size(sy->ops)) {
1025 parseerror(parser, "unmatched closing paren");
1028 /* this would for bit a + (x) because there are no operators inside (x)
1029 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1030 parseerror(parser, "empty parenthesis expression");
1034 while (vec_size(sy->ops)) {
1035 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1036 if (!parser_close_call(parser, sy))
1040 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1041 vec_shrinkby(sy->ops, 1);
1042 return !functions_only;
1044 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1047 /* pop off the parenthesis */
1048 vec_shrinkby(sy->ops, 1);
1049 /* then apply the index operator */
1050 if (!parser_sy_pop(parser, sy))
1054 if (!parser_sy_pop(parser, sy))
1060 static void parser_reclassify_token(parser_t *parser)
1063 for (i = 0; i < operator_count; ++i) {
1064 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1065 parser->tok = TOKEN_OPERATOR;
1071 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1073 ast_expression *expr = NULL;
1075 bool wantop = false;
1076 bool gotmemberof = false;
1078 /* count the parens because an if starts with one, so the
1079 * end of a condition is an unmatched closing paren
1086 parser->lex->flags.noops = false;
1088 parser_reclassify_token(parser);
1093 gotmemberof = false;
1095 parser->memberof = 0;
1097 if (parser->tok == TOKEN_IDENT)
1099 ast_expression *var;
1101 parseerror(parser, "expected operator or end of statement");
1106 if (opts_standard == COMPILER_GMQCC)
1108 if (parser->memberof == TYPE_ENTITY) {
1109 /* still get vars first since there could be a fieldpointer */
1110 var = parser_find_var(parser, parser_tokval(parser));
1112 var = parser_find_field(parser, parser_tokval(parser));
1114 else if (parser->memberof == TYPE_VECTOR)
1116 parseerror(parser, "TODO: implement effective vector member access");
1119 else if (parser->memberof) {
1120 parseerror(parser, "namespace for member not found");
1124 var = parser_find_var(parser, parser_tokval(parser));
1126 var = parser_find_var(parser, parser_tokval(parser));
1128 var = parser_find_field(parser, parser_tokval(parser));
1131 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1134 if (ast_istype(var, ast_value))
1135 ((ast_value*)var)->uses++;
1136 vec_push(sy.out, syexp(parser_ctx(parser), var));
1137 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1139 else if (parser->tok == TOKEN_FLOATCONST) {
1142 parseerror(parser, "expected operator or end of statement, got constant");
1146 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1149 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1150 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1152 else if (parser->tok == TOKEN_INTCONST) {
1155 parseerror(parser, "expected operator or end of statement, got constant");
1159 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1162 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1163 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1165 else if (parser->tok == TOKEN_STRINGCONST) {
1168 parseerror(parser, "expected operator or end of statement, got constant");
1172 val = parser_const_string(parser, parser_tokval(parser));
1175 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1176 DEBUGSHUNTDO(con_out("push string\n"));
1178 else if (parser->tok == TOKEN_VECTORCONST) {
1181 parseerror(parser, "expected operator or end of statement, got constant");
1185 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1188 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1189 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1190 parser_token(parser)->constval.v.x,
1191 parser_token(parser)->constval.v.y,
1192 parser_token(parser)->constval.v.z));
1194 else if (parser->tok == '(') {
1195 parseerror(parser, "internal error: '(' should be classified as operator");
1198 else if (parser->tok == '[') {
1199 parseerror(parser, "internal error: '[' should be classified as operator");
1202 else if (parser->tok == ')') {
1204 DEBUGSHUNTDO(con_out("do[op] )\n"));
1208 /* we do expect an operator next */
1209 /* closing an opening paren */
1210 if (!parser_close_paren(parser, &sy, false))
1213 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1217 /* allowed for function calls */
1218 if (!parser_close_paren(parser, &sy, true))
1223 else if (parser->tok == ']') {
1225 parseerror(parser, "operand expected");
1229 if (!parser_close_paren(parser, &sy, false))
1233 else if (parser->tok != TOKEN_OPERATOR) {
1235 parseerror(parser, "expected operator or end of statement");
1242 /* classify the operator */
1243 /* TODO: suffix operators */
1244 const oper_info *op;
1245 const oper_info *olast = NULL;
1247 for (o = 0; o < operator_count; ++o) {
1248 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1249 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1250 !strcmp(parser_tokval(parser), operators[o].op))
1255 if (o == operator_count) {
1256 /* no operator found... must be the end of the statement */
1259 /* found an operator */
1262 /* when declaring variables, a comma starts a new variable */
1263 if (op->id == opid1(',') && !parens && stopatcomma) {
1264 /* fixup the token */
1269 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1270 olast = &operators[vec_last(sy.ops).etype-1];
1273 (op->prec < olast->prec) ||
1274 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1276 if (!parser_sy_pop(parser, &sy))
1278 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1279 olast = &operators[vec_last(sy.ops).etype-1];
1284 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1285 /* for gmqcc standard: open up the namespace of the previous type */
1286 ast_expression *prevex = vec_last(sy.out).out;
1288 parseerror(parser, "unexpected member operator");
1291 if (prevex->expression.vtype == TYPE_ENTITY)
1292 parser->memberof = TYPE_ENTITY;
1293 else if (prevex->expression.vtype == TYPE_VECTOR)
1294 parser->memberof = TYPE_VECTOR;
1296 parseerror(parser, "type error: type has no members");
1302 if (op->id == opid1('(')) {
1304 size_t sycount = vec_size(sy.out);
1305 DEBUGSHUNTDO(con_out("push [op] (\n"));
1307 /* we expected an operator, this is the function-call operator */
1308 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1311 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1312 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1315 } else if (op->id == opid1('[')) {
1317 parseerror(parser, "unexpected array subscript");
1321 /* push both the operator and the paren, this makes life easier */
1322 vec_push(sy.ops, syop(parser_ctx(parser), op));
1323 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1326 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1327 vec_push(sy.ops, syop(parser_ctx(parser), op));
1331 if (!parser_next(parser)) {
1334 if (parser->tok == ';' || (!parens && parser->tok == ']')) {
1339 while (vec_size(sy.ops)) {
1340 if (!parser_sy_pop(parser, &sy))
1344 parser->lex->flags.noops = true;
1345 if (!vec_size(sy.out)) {
1346 parseerror(parser, "empty expression");
1349 expr = sy.out[0].out;
1352 DEBUGSHUNTDO(con_out("shunt done\n"));
1356 parser->lex->flags.noops = true;
1362 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1364 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1367 if (!parser_next(parser)) {
1374 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1377 ast_expression *cond, *ontrue, *onfalse = NULL;
1380 lex_ctx ctx = parser_ctx(parser);
1382 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1383 if (!parser_next(parser)) {
1384 parseerror(parser, "expected condition or 'not'");
1387 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "not")) {
1389 if (!parser_next(parser)) {
1390 parseerror(parser, "expected condition in parenthesis");
1394 if (parser->tok != '(') {
1395 parseerror(parser, "expected 'if' condition in parenthesis");
1398 /* parse into the expression */
1399 if (!parser_next(parser)) {
1400 parseerror(parser, "expected 'if' condition after opening paren");
1403 /* parse the condition */
1404 cond = parse_expression_leave(parser, false);
1408 if (parser->tok != ')') {
1409 parseerror(parser, "expected closing paren after 'if' condition");
1413 /* parse into the 'then' branch */
1414 if (!parser_next(parser)) {
1415 parseerror(parser, "expected statement for on-true branch of 'if'");
1419 ontrue = parse_statement_or_block(parser);
1424 /* check for an else */
1425 if (!strcmp(parser_tokval(parser), "else")) {
1426 /* parse into the 'else' branch */
1427 if (!parser_next(parser)) {
1428 parseerror(parser, "expected on-false branch after 'else'");
1433 onfalse = parse_statement_or_block(parser);
1442 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1444 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1445 *out = (ast_expression*)ifthen;
1449 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1452 ast_expression *cond, *ontrue;
1454 lex_ctx ctx = parser_ctx(parser);
1456 /* skip the 'while' and check for opening paren */
1457 if (!parser_next(parser) || parser->tok != '(') {
1458 parseerror(parser, "expected 'while' condition in parenthesis");
1461 /* parse into the expression */
1462 if (!parser_next(parser)) {
1463 parseerror(parser, "expected 'while' condition after opening paren");
1466 /* parse the condition */
1467 cond = parse_expression_leave(parser, false);
1471 if (parser->tok != ')') {
1472 parseerror(parser, "expected closing paren after 'while' condition");
1476 /* parse into the 'then' branch */
1477 if (!parser_next(parser)) {
1478 parseerror(parser, "expected while-loop body");
1482 ontrue = parse_statement_or_block(parser);
1488 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1489 *out = (ast_expression*)aloop;
1493 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1496 ast_expression *cond, *ontrue;
1498 lex_ctx ctx = parser_ctx(parser);
1500 /* skip the 'do' and get the body */
1501 if (!parser_next(parser)) {
1502 parseerror(parser, "expected loop body");
1505 ontrue = parse_statement_or_block(parser);
1509 /* expect the "while" */
1510 if (parser->tok != TOKEN_KEYWORD ||
1511 strcmp(parser_tokval(parser), "while"))
1513 parseerror(parser, "expected 'while' and condition");
1518 /* skip the 'while' and check for opening paren */
1519 if (!parser_next(parser) || parser->tok != '(') {
1520 parseerror(parser, "expected 'while' condition in parenthesis");
1524 /* parse into the expression */
1525 if (!parser_next(parser)) {
1526 parseerror(parser, "expected 'while' condition after opening paren");
1530 /* parse the condition */
1531 cond = parse_expression_leave(parser, false);
1535 if (parser->tok != ')') {
1536 parseerror(parser, "expected closing paren after 'while' condition");
1542 if (!parser_next(parser) || parser->tok != ';') {
1543 parseerror(parser, "expected semicolon after condition");
1549 if (!parser_next(parser)) {
1550 parseerror(parser, "parse error");
1556 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1557 *out = (ast_expression*)aloop;
1561 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1564 ast_expression *initexpr, *cond, *increment, *ontrue;
1565 size_t oldblocklocal;
1568 lex_ctx ctx = parser_ctx(parser);
1570 oldblocklocal = parser->blocklocal;
1571 parser->blocklocal = vec_size(parser->locals);
1578 /* skip the 'while' and check for opening paren */
1579 if (!parser_next(parser) || parser->tok != '(') {
1580 parseerror(parser, "expected 'for' expressions in parenthesis");
1583 /* parse into the expression */
1584 if (!parser_next(parser)) {
1585 parseerror(parser, "expected 'for' initializer after opening paren");
1589 if (parser->tok == TOKEN_TYPENAME) {
1590 if (opts_standard != COMPILER_GMQCC) {
1591 if (parsewarning(parser, WARN_EXTENSIONS,
1592 "current standard does not allow variable declarations in for-loop initializers"))
1596 parseerror(parser, "TODO: assignment of new variables to be non-const");
1598 if (!parse_variable(parser, block, true))
1601 else if (parser->tok != ';')
1603 initexpr = parse_expression_leave(parser, false);
1608 /* move on to condition */
1609 if (parser->tok != ';') {
1610 parseerror(parser, "expected semicolon after for-loop initializer");
1613 if (!parser_next(parser)) {
1614 parseerror(parser, "expected for-loop condition");
1618 /* parse the condition */
1619 if (parser->tok != ';') {
1620 cond = parse_expression_leave(parser, false);
1625 /* move on to incrementor */
1626 if (parser->tok != ';') {
1627 parseerror(parser, "expected semicolon after for-loop initializer");
1630 if (!parser_next(parser)) {
1631 parseerror(parser, "expected for-loop condition");
1635 /* parse the incrementor */
1636 if (parser->tok != ')') {
1637 increment = parse_expression_leave(parser, false);
1640 if (!ast_istype(increment, ast_store) &&
1641 !ast_istype(increment, ast_call) &&
1642 !ast_istype(increment, ast_binstore))
1644 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1650 if (parser->tok != ')') {
1651 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1654 /* parse into the 'then' branch */
1655 if (!parser_next(parser)) {
1656 parseerror(parser, "expected for-loop body");
1659 ontrue = parse_statement_or_block(parser);
1664 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1665 *out = (ast_expression*)aloop;
1667 while (vec_size(parser->locals) > parser->blocklocal)
1668 retval = retval && parser_pop_local(parser);
1669 parser->blocklocal = oldblocklocal;
1672 if (initexpr) ast_delete(initexpr);
1673 if (cond) ast_delete(cond);
1674 if (increment) ast_delete(increment);
1675 while (vec_size(parser->locals) > parser->blocklocal)
1676 (void)!parser_pop_local(parser);
1677 parser->blocklocal = oldblocklocal;
1681 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
1683 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
1685 /* local variable */
1687 parseerror(parser, "cannot declare a variable from here");
1690 if (opts_standard == COMPILER_QCC) {
1691 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1694 if (!parse_variable(parser, block, false))
1699 else if (parser->tok == TOKEN_KEYWORD)
1701 if (!strcmp(parser_tokval(parser), "local"))
1704 parseerror(parser, "cannot declare a local variable here");
1707 if (!parser_next(parser)) {
1708 parseerror(parser, "expected variable declaration");
1711 if (!parse_variable(parser, block, true))
1716 else if (!strcmp(parser_tokval(parser), "return"))
1718 ast_expression *exp = NULL;
1719 ast_return *ret = NULL;
1720 ast_value *expected = parser->function->vtype;
1722 if (!parser_next(parser)) {
1723 parseerror(parser, "expected return expression");
1727 if (parser->tok != ';') {
1728 exp = parse_expression(parser, false);
1732 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1733 parseerror(parser, "return with invalid expression");
1736 ret = ast_return_new(exp->expression.node.context, exp);
1742 if (!parser_next(parser))
1743 parseerror(parser, "parse error");
1744 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1745 if (opts_standard != COMPILER_GMQCC)
1746 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1748 parseerror(parser, "return without value");
1750 ret = ast_return_new(parser_ctx(parser), NULL);
1752 *out = (ast_expression*)ret;
1755 else if (!strcmp(parser_tokval(parser), "if"))
1757 return parse_if(parser, block, out);
1759 else if (!strcmp(parser_tokval(parser), "while"))
1761 return parse_while(parser, block, out);
1763 else if (!strcmp(parser_tokval(parser), "do"))
1765 return parse_dowhile(parser, block, out);
1767 else if (!strcmp(parser_tokval(parser), "for"))
1769 if (opts_standard == COMPILER_QCC) {
1770 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1773 return parse_for(parser, block, out);
1775 parseerror(parser, "Unexpected keyword");
1778 else if (parser->tok == '{')
1781 inner = parse_block(parser, false);
1784 *out = (ast_expression*)inner;
1789 ast_expression *exp = parse_expression(parser, false);
1793 if (!ast_istype(exp, ast_store) &&
1794 !ast_istype(exp, ast_call) &&
1795 !ast_istype(exp, ast_binstore))
1797 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1804 static bool GMQCC_WARN parser_pop_local(parser_t *parser)
1809 ve = &vec_last(parser->locals);
1810 if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
1811 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
1815 vec_pop(parser->locals);
1819 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
1821 size_t oldblocklocal;
1824 oldblocklocal = parser->blocklocal;
1825 parser->blocklocal = vec_size(parser->locals);
1827 if (!parser_next(parser)) { /* skip the '{' */
1828 parseerror(parser, "expected function body");
1832 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
1834 ast_expression *expr;
1835 if (parser->tok == '}')
1838 if (!parse_statement(parser, block, &expr)) {
1839 /* parseerror(parser, "parse error"); */
1845 vec_push(block->exprs, expr);
1848 if (parser->tok != '}') {
1851 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
1853 if (!vec_size(block->exprs) ||
1854 !ast_istype(vec_last(block->exprs), ast_return))
1856 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
1862 (void)parser_next(parser);
1866 while (vec_size(parser->locals) > parser->blocklocal)
1867 retval = retval && parser_pop_local(parser);
1868 parser->blocklocal = oldblocklocal;
1872 static ast_block* parse_block(parser_t *parser, bool warnreturn)
1875 block = ast_block_new(parser_ctx(parser));
1878 if (!parse_block_into(parser, block, warnreturn)) {
1879 ast_block_delete(block);
1885 static ast_expression* parse_statement_or_block(parser_t *parser)
1887 ast_expression *expr = NULL;
1888 if (parser->tok == '{')
1889 return (ast_expression*)parse_block(parser, false);
1890 if (!parse_statement(parser, NULL, &expr))
1896 static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
1899 size_t len = strlen(var->name);
1901 for (i = 0; i < 3; ++i) {
1902 ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
1906 ve[i].name = (char*)mem_a(len+3);
1908 ast_delete(ve[i].var);
1912 memcpy(ve[i].name, var->name, len);
1913 ve[i].name[len] = '_';
1914 ve[i].name[len+1] = 'x'+i;
1915 ve[i].name[len+2] = 0;
1924 ast_delete(ve[i].var);
1931 static bool parse_function_body(parser_t *parser, ast_value *var)
1933 ast_block *block = NULL;
1938 ast_expression *framenum = NULL;
1939 ast_expression *nextthink = NULL;
1940 /* None of the following have to be deleted */
1941 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
1942 ast_expression *gbl_time = NULL, *gbl_self = NULL;
1943 bool has_frame_think;
1947 has_frame_think = false;
1948 old = parser->function;
1950 if (var->expression.variadic) {
1951 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
1952 "variadic function with implementation will not be able to access additional parameters"))
1958 if (parser->tok == '[') {
1959 /* got a frame definition: [ framenum, nextthink ]
1960 * this translates to:
1961 * self.frame = framenum;
1962 * self.nextthink = time + 0.1;
1963 * self.think = nextthink;
1967 fld_think = parser_find_field(parser, "think");
1968 fld_nextthink = parser_find_field(parser, "nextthink");
1969 fld_frame = parser_find_field(parser, "frame");
1970 if (!fld_think || !fld_nextthink || !fld_frame) {
1971 parseerror(parser, "cannot use [frame,think] notation without the required fields");
1972 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
1975 gbl_time = parser_find_global(parser, "time");
1976 gbl_self = parser_find_global(parser, "self");
1977 if (!gbl_time || !gbl_self) {
1978 parseerror(parser, "cannot use [frame,think] notation without the required globals");
1979 parseerror(parser, "please declare the following globals: `time`, `self`");
1983 if (!parser_next(parser))
1986 framenum = parse_expression_leave(parser, true);
1988 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
1991 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
1992 ast_unref(framenum);
1993 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
1997 if (parser->tok != ',') {
1998 ast_unref(framenum);
1999 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2000 parseerror(parser, "Got a %i\n", parser->tok);
2004 if (!parser_next(parser)) {
2005 ast_unref(framenum);
2009 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2011 /* qc allows the use of not-yet-declared functions here
2012 * - this automatically creates a prototype */
2014 ast_value *thinkfunc;
2015 ast_expression *functype = fld_think->expression.next;
2017 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2018 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2019 ast_unref(framenum);
2020 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2024 if (!parser_next(parser)) {
2025 ast_unref(framenum);
2026 ast_delete(thinkfunc);
2030 varent.var = (ast_expression*)thinkfunc;
2031 varent.name = util_strdup(thinkfunc->name);
2032 vec_push(parser->globals, varent);
2033 nextthink = (ast_expression*)thinkfunc;
2036 nextthink = parse_expression_leave(parser, true);
2038 ast_unref(framenum);
2039 parseerror(parser, "expected a think-function in [frame,think] notation");
2044 if (!ast_istype(nextthink, ast_value)) {
2045 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2049 if (retval && parser->tok != ']') {
2050 parseerror(parser, "expected closing `]` for [frame,think] notation");
2054 if (retval && !parser_next(parser)) {
2058 if (retval && parser->tok != '{') {
2059 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2064 ast_unref(nextthink);
2065 ast_unref(framenum);
2069 has_frame_think = true;
2072 block = ast_block_new(parser_ctx(parser));
2074 parseerror(parser, "failed to allocate block");
2075 if (has_frame_think) {
2076 ast_unref(nextthink);
2077 ast_unref(framenum);
2082 if (has_frame_think) {
2084 ast_expression *self_frame;
2085 ast_expression *self_nextthink;
2086 ast_expression *self_think;
2087 ast_expression *time_plus_1;
2088 ast_store *store_frame;
2089 ast_store *store_nextthink;
2090 ast_store *store_think;
2092 ctx = parser_ctx(parser);
2093 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2094 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2095 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2097 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2098 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2100 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2101 if (self_frame) ast_delete(self_frame);
2102 if (self_nextthink) ast_delete(self_nextthink);
2103 if (self_think) ast_delete(self_think);
2104 if (time_plus_1) ast_delete(time_plus_1);
2110 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2111 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2112 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2115 ast_delete(self_frame);
2118 if (!store_nextthink) {
2119 ast_delete(self_nextthink);
2123 ast_delete(self_think);
2127 if (store_frame) ast_delete(store_frame);
2128 if (store_nextthink) ast_delete(store_nextthink);
2129 if (store_think) ast_delete(store_think);
2132 vec_push(block->exprs, (ast_expression*)store_frame);
2133 vec_push(block->exprs, (ast_expression*)store_nextthink);
2134 vec_push(block->exprs, (ast_expression*)store_think);
2138 parseerror(parser, "failed to generate code for [frame,think]");
2139 ast_unref(nextthink);
2140 ast_unref(framenum);
2146 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2149 ast_value *param = var->expression.params[parami];
2151 if (param->expression.vtype != TYPE_VECTOR &&
2152 (param->expression.vtype != TYPE_FIELD ||
2153 param->expression.next->expression.vtype != TYPE_VECTOR))
2158 if (!create_vector_members(parser, param, ve)) {
2159 ast_block_delete(block);
2163 for (e = 0; e < 3; ++e) {
2164 vec_push(parser->locals, ve[e]);
2165 ast_block_collect(block, ve[e].var);
2166 ve[e].var = NULL; /* collected */
2170 func = ast_function_new(ast_ctx(var), var->name, var);
2172 parseerror(parser, "failed to allocate function for `%s`", var->name);
2173 ast_block_delete(block);
2176 vec_push(parser->functions, func);
2178 parser->function = func;
2179 if (!parse_block_into(parser, block, true)) {
2180 ast_block_delete(block);
2184 vec_push(func->blocks, block);
2186 parser->function = old;
2187 while (vec_size(parser->locals))
2188 retval = retval && parser_pop_local(parser);
2190 if (parser->tok == ';')
2191 return parser_next(parser);
2192 else if (opts_standard == COMPILER_QCC)
2193 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2197 vec_pop(parser->functions);
2198 ast_function_delete(func);
2199 var->constval.vfunc = NULL;
2202 while (vec_size(parser->locals)) {
2203 mem_d(vec_last(parser->locals).name);
2204 vec_pop(parser->locals);
2206 parser->function = old;
2210 static ast_expression *array_accessor_split(
2215 ast_expression *left,
2216 ast_expression *right
2222 lex_ctx ctx = ast_ctx(array);
2224 if (!left || !right) {
2225 if (left) ast_delete(left);
2226 if (right) ast_delete(right);
2230 cmp = ast_binary_new(ctx, INSTR_LT,
2231 (ast_expression*)index,
2232 (ast_expression*)parser_const_float(parser, middle));
2236 parseerror(parser, "internal error: failed to create comparison for array setter");
2240 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2242 ast_delete(cmp); /* will delete left and right */
2243 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2247 return (ast_expression*)ifthen;
2250 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2252 lex_ctx ctx = ast_ctx(array);
2254 if (from+1 == afterend) {
2258 ast_array_index *subscript;
2259 int assignop = type_store_instr[value->expression.vtype];
2261 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2262 assignop = INSTR_STORE_V;
2264 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2268 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2270 ast_delete(subscript);
2274 block = ast_block_new(ctx);
2280 vec_push(block->exprs, (ast_expression*)st);
2282 ret = ast_return_new(ctx, NULL);
2288 vec_push(block->exprs, (ast_expression*)ret);
2290 return (ast_expression*)block;
2292 ast_expression *left, *right;
2293 size_t diff = afterend - from;
2294 size_t middle = from + diff/2;
2295 left = array_setter_node(parser, array, index, value, from, middle);
2296 right = array_setter_node(parser, array, index, value, middle, afterend);
2297 return array_accessor_split(parser, array, index, middle, left, right);
2301 static ast_expression *array_field_setter_node(
2310 lex_ctx ctx = ast_ctx(array);
2312 if (from+1 == afterend) {
2316 ast_entfield *entfield;
2317 ast_array_index *subscript;
2318 int assignop = type_storep_instr[value->expression.vtype];
2320 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2321 assignop = INSTR_STOREP_V;
2323 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2327 entfield = ast_entfield_new_force(ctx,
2328 (ast_expression*)entity,
2329 (ast_expression*)subscript,
2330 (ast_expression*)subscript);
2332 ast_delete(subscript);
2336 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2338 ast_delete(entfield);
2342 block = ast_block_new(ctx);
2348 vec_push(block->exprs, (ast_expression*)st);
2350 ret = ast_return_new(ctx, NULL);
2356 vec_push(block->exprs, (ast_expression*)ret);
2358 return (ast_expression*)block;
2360 ast_expression *left, *right;
2361 size_t diff = afterend - from;
2362 size_t middle = from + diff/2;
2363 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2364 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2365 return array_accessor_split(parser, array, index, middle, left, right);
2369 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2371 lex_ctx ctx = ast_ctx(array);
2373 if (from+1 == afterend) {
2375 ast_array_index *subscript;
2377 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2381 ret = ast_return_new(ctx, (ast_expression*)subscript);
2383 ast_delete(subscript);
2387 return (ast_expression*)ret;
2389 ast_expression *left, *right;
2390 size_t diff = afterend - from;
2391 size_t middle = from + diff/2;
2392 left = array_getter_node(parser, array, index, from, middle);
2393 right = array_getter_node(parser, array, index, middle, afterend);
2394 return array_accessor_split(parser, array, index, middle, left, right);
2398 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2400 ast_function *func = NULL;
2401 ast_value *fval = NULL;
2402 ast_block *body = NULL;
2404 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2406 parseerror(parser, "failed to create accessor function value");
2410 func = ast_function_new(ast_ctx(array), funcname, fval);
2413 parseerror(parser, "failed to create accessor function node");
2417 body = ast_block_new(ast_ctx(array));
2419 parseerror(parser, "failed to create block for array accessor");
2425 vec_push(func->blocks, body);
2428 vec_push(parser->accessors, fval);
2433 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2435 ast_expression *root = NULL;
2436 ast_value *index = NULL;
2437 ast_value *value = NULL;
2441 if (!ast_istype(array->expression.next, ast_value)) {
2442 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2446 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2448 func = fval->constval.vfunc;
2449 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2451 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2452 value = ast_value_copy((ast_value*)array->expression.next);
2454 if (!index || !value) {
2455 parseerror(parser, "failed to create locals for array accessor");
2458 (void)!ast_value_set_name(value, "value"); /* not important */
2459 vec_push(fval->expression.params, index);
2460 vec_push(fval->expression.params, value);
2462 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2464 parseerror(parser, "failed to build accessor search tree");
2468 vec_push(func->blocks[0]->exprs, root);
2469 array->setter = fval;
2472 if (index) ast_delete(index);
2473 if (value) ast_delete(value);
2474 if (root) ast_delete(root);
2480 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2482 ast_expression *root = NULL;
2483 ast_value *entity = NULL;
2484 ast_value *index = NULL;
2485 ast_value *value = NULL;
2489 if (!ast_istype(array->expression.next, ast_value)) {
2490 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2494 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2496 func = fval->constval.vfunc;
2497 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2499 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2500 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2501 value = ast_value_copy((ast_value*)array->expression.next);
2502 if (!entity || !index || !value) {
2503 parseerror(parser, "failed to create locals for array accessor");
2506 (void)!ast_value_set_name(value, "value"); /* not important */
2507 vec_push(fval->expression.params, entity);
2508 vec_push(fval->expression.params, index);
2509 vec_push(fval->expression.params, value);
2511 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2513 parseerror(parser, "failed to build accessor search tree");
2517 vec_push(func->blocks[0]->exprs, root);
2518 array->setter = fval;
2521 if (entity) ast_delete(entity);
2522 if (index) ast_delete(index);
2523 if (value) ast_delete(value);
2524 if (root) ast_delete(root);
2530 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2532 ast_expression *root = NULL;
2533 ast_value *index = NULL;
2537 /* NOTE: checking array->expression.next rather than elemtype since
2538 * for fields elemtype is a temporary fieldtype.
2540 if (!ast_istype(array->expression.next, ast_value)) {
2541 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2545 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2547 func = fval->constval.vfunc;
2548 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2550 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2553 parseerror(parser, "failed to create locals for array accessor");
2556 vec_push(fval->expression.params, index);
2558 root = array_getter_node(parser, array, index, 0, array->expression.count);
2560 parseerror(parser, "failed to build accessor search tree");
2564 vec_push(func->blocks[0]->exprs, root);
2565 array->getter = fval;
2568 if (index) ast_delete(index);
2569 if (root) ast_delete(root);
2575 static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
2576 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
2584 bool variadic = false;
2586 ctx = parser_ctx(parser);
2588 /* for the sake of less code we parse-in in this function */
2589 if (!parser_next(parser)) {
2590 parseerror(parser, "expected parameter list");
2596 /* parse variables until we hit a closing paren */
2597 while (parser->tok != ')') {
2599 /* there must be commas between them */
2600 if (parser->tok != ',') {
2601 parseerror(parser, "expected comma or end of parameter list");
2604 if (!parser_next(parser)) {
2605 parseerror(parser, "expected parameter");
2611 if (parser->tok == TOKEN_DOTS) {
2612 /* '...' indicates a varargs function */
2614 if (!parser_next(parser)) {
2615 parseerror(parser, "expected parameter");
2618 if (parser->tok != ')') {
2619 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
2625 /* for anything else just parse a typename */
2626 param = parse_typename(parser, NULL);
2629 vec_push(params, param);
2630 if (param->expression.vtype >= TYPE_VARIANT) {
2631 char typename[1024];
2632 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
2633 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
2640 if (vec_size(params) > 8)
2641 parseerror(parser, "more than 8 parameters are currently not supported");
2644 if (!parser_next(parser)) {
2645 parseerror(parser, "parse error after typename");
2649 /* now turn 'var' into a function type */
2650 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
2651 fval->expression.next = (ast_expression*)var;
2652 fval->expression.variadic = variadic;
2655 var->expression.params = params;
2662 for (i = 0; i < vec_size(params); ++i)
2663 ast_delete(params[i]);
2668 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
2670 ast_expression *cexp;
2671 ast_value *cval, *tmp;
2674 ctx = parser_ctx(parser);
2676 if (!parser_next(parser)) {
2678 parseerror(parser, "expected array-size");
2682 cexp = parse_expression_leave(parser, true);
2684 if (!cexp || !ast_istype(cexp, ast_value)) {
2688 parseerror(parser, "expected array-size as constant positive integer");
2691 cval = (ast_value*)cexp;
2693 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
2694 tmp->expression.next = (ast_expression*)var;
2697 if (cval->expression.vtype == TYPE_INTEGER)
2698 tmp->expression.count = cval->constval.vint;
2699 else if (cval->expression.vtype == TYPE_FLOAT)
2700 tmp->expression.count = cval->constval.vfloat;
2704 parseerror(parser, "array-size must be a positive integer constant");
2709 if (parser->tok != ']') {
2711 parseerror(parser, "expected ']' after array-size");
2714 if (!parser_next(parser)) {
2716 parseerror(parser, "error after parsing array size");
2722 /* Parse a complete typename.
2723 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
2724 * but when parsing variables separated by comma
2725 * 'storebase' should point to where the base-type should be kept.
2726 * The base type makes up every bit of type information which comes *before* the
2729 * The following will be parsed in its entirety:
2731 * The 'basetype' in this case is 'void()'
2732 * and if there's a comma after it, say:
2734 * then the type-information 'void()' can be stored in 'storebase'
2736 static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
2738 ast_value *var, *tmp;
2741 const char *name = NULL;
2742 bool isfield = false;
2743 bool wasarray = false;
2745 ctx = parser_ctx(parser);
2747 /* types may start with a dot */
2748 if (parser->tok == '.') {
2750 /* if we parsed a dot we need a typename now */
2751 if (!parser_next(parser)) {
2752 parseerror(parser, "expected typename for field definition");
2755 if (parser->tok != TOKEN_TYPENAME) {
2756 parseerror(parser, "expected typename");
2761 /* generate the basic type value */
2762 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
2763 /* do not yet turn into a field - remember:
2764 * .void() foo; is a field too
2765 * .void()() foo; is a function
2769 if (!parser_next(parser)) {
2771 parseerror(parser, "parse error after typename");
2775 /* an opening paren now starts the parameter-list of a function
2776 * this is where original-QC has parameter lists.
2777 * We allow a single parameter list here.
2778 * Much like fteqcc we don't allow `float()() x`
2780 if (parser->tok == '(') {
2781 var = parse_parameter_list(parser, var);
2786 /* store the base if requested */
2788 *storebase = ast_value_copy(var);
2790 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2791 tmp->expression.next = (ast_expression*)*storebase;
2796 /* there may be a name now */
2797 if (parser->tok == TOKEN_IDENT) {
2798 name = util_strdup(parser_tokval(parser));
2800 if (!parser_next(parser)) {
2802 parseerror(parser, "error after variable or field declaration");
2807 /* now this may be an array */
2808 if (parser->tok == '[') {
2810 var = parse_arraysize(parser, var);
2815 /* This is the point where we can turn it into a field */
2817 /* turn it into a field if desired */
2818 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2819 tmp->expression.next = (ast_expression*)var;
2823 /* now there may be function parens again */
2824 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
2825 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2826 if (parser->tok == '(' && wasarray)
2827 parseerror(parser, "arrays as part of a return type is not supported");
2828 while (parser->tok == '(') {
2829 var = parse_parameter_list(parser, var);
2838 /* finally name it */
2840 if (!ast_value_set_name(var, name)) {
2842 parseerror(parser, "internal error: failed to set name");
2845 /* free the name, ast_value_set_name duplicates */
2852 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
2856 ast_expression *old;
2860 ast_value *basetype = NULL;
2862 bool isparam = false;
2863 bool isvector = false;
2864 bool cleanvar = true;
2865 bool wasarray = false;
2867 varentry_t varent, ve[3];
2869 /* get the first complete variable */
2870 var = parse_typename(parser, &basetype);
2873 ast_delete(basetype);
2877 memset(&varent, 0, sizeof(varent));
2878 memset(&ve, 0, sizeof(ve));
2884 /* Part 0: finish the type */
2885 if (parser->tok == '(') {
2886 if (opts_standard == COMPILER_QCC)
2887 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2888 var = parse_parameter_list(parser, var);
2894 /* we only allow 1-dimensional arrays */
2895 if (parser->tok == '[') {
2897 var = parse_arraysize(parser, var);
2903 if (parser->tok == '(' && wasarray) {
2904 parseerror(parser, "arrays as part of a return type is not supported");
2905 /* we'll still parse the type completely for now */
2907 /* for functions returning functions */
2908 while (parser->tok == '(') {
2909 if (opts_standard == COMPILER_QCC)
2910 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2911 var = parse_parameter_list(parser, var);
2919 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
2920 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
2921 * is then filled with the previous definition and the parameter-names replaced.
2924 /* Deal with end_sys_ vars */
2926 if (!strcmp(var->name, "end_sys_globals")) {
2927 parser->crc_globals = vec_size(parser->globals);
2930 else if (!strcmp(var->name, "end_sys_fields")) {
2931 parser->crc_fields = vec_size(parser->fields);
2934 if (was_end && var->expression.vtype == TYPE_FIELD) {
2935 if (parsewarning(parser, WARN_END_SYS_FIELDS,
2936 "global '%s' hint should not be a field",
2937 parser_tokval(parser)))
2944 if (!nofields && var->expression.vtype == TYPE_FIELD)
2946 /* deal with field declarations */
2947 old = parser_find_field(parser, var->name);
2949 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
2950 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
2959 parseerror(parser, "field `%s` already declared here: %s:%i",
2960 var->name, ast_ctx(old).file, ast_ctx(old).line);
2965 if (opts_standard == COMPILER_QCC &&
2966 (old = parser_find_global(parser, var->name)))
2968 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
2969 parseerror(parser, "field `%s` already declared here: %s:%i",
2970 var->name, ast_ctx(old).file, ast_ctx(old).line);
2977 /* deal with other globals */
2978 old = parser_find_global(parser, var->name);
2979 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
2981 /* This is a function which had a prototype */
2982 if (!ast_istype(old, ast_value)) {
2983 parseerror(parser, "internal error: prototype is not an ast_value");
2987 proto = (ast_value*)old;
2988 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
2989 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
2991 ast_ctx(proto).file, ast_ctx(proto).line);
2995 /* we need the new parameter-names */
2996 for (i = 0; i < vec_size(proto->expression.params); ++i)
2997 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3005 parseerror(parser, "global `%s` already declared here: %s:%i",
3006 var->name, ast_ctx(old).file, ast_ctx(old).line);
3010 if (opts_standard == COMPILER_QCC &&
3011 (old = parser_find_field(parser, var->name)))
3013 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3014 parseerror(parser, "global `%s` already declared here: %s:%i",
3015 var->name, ast_ctx(old).file, ast_ctx(old).line);
3022 else /* it's not a global */
3024 old = parser_find_local(parser, var->name, parser->blocklocal, &isparam);
3025 if (old && !isparam) {
3026 parseerror(parser, "local `%s` already declared here: %s:%i",
3027 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3031 old = parser_find_local(parser, var->name, 0, &isparam);
3032 if (old && isparam) {
3033 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3034 "local `%s` is shadowing a parameter", var->name))
3036 parseerror(parser, "local `%s` already declared here: %s:%i",
3037 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3041 if (opts_standard != COMPILER_GMQCC) {
3050 * Create the global/local, and deal with vector types.
3053 if (var->expression.vtype == TYPE_VECTOR)
3055 else if (var->expression.vtype == TYPE_FIELD &&
3056 var->expression.next->expression.vtype == TYPE_VECTOR)
3060 if (!create_vector_members(parser, var, ve)) {
3066 varent.name = util_strdup(var->name);
3067 varent.var = (ast_expression*)var;
3070 /* deal with global variables, fields, functions */
3071 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3072 vec_push(parser->fields, varent);
3074 for (i = 0; i < 3; ++i)
3075 vec_push(parser->fields, ve[i]);
3079 vec_push(parser->globals, varent);
3081 for (i = 0; i < 3; ++i)
3082 vec_push(parser->globals, ve[i]);
3086 vec_push(parser->locals, varent);
3087 vec_push(localblock->locals, var);
3089 for (i = 0; i < 3; ++i) {
3090 vec_push(parser->locals, ve[i]);
3091 ast_block_collect(localblock, ve[i].var);
3092 ve[i].var = NULL; /* from here it's being collected in the block */
3098 ve[0].name = ve[1].name = ve[2].name = NULL;
3099 ve[0].var = ve[1].var = ve[2].var = NULL;
3105 if (var->expression.vtype == TYPE_ARRAY) {
3107 snprintf(name, sizeof(name), "%s##SET", var->name);
3108 if (!parser_create_array_setter(parser, var, name))
3110 snprintf(name, sizeof(name), "%s##GET", var->name);
3111 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3114 else if (!localblock && !nofields &&
3115 var->expression.vtype == TYPE_FIELD &&
3116 var->expression.next->expression.vtype == TYPE_ARRAY)
3119 ast_expression *telem;
3121 ast_value *array = (ast_value*)var->expression.next;
3123 if (!ast_istype(var->expression.next, ast_value)) {
3124 parseerror(parser, "internal error: field element type must be an ast_value");
3128 snprintf(name, sizeof(name), "%s##SETF", var->name);
3129 if (!parser_create_array_field_setter(parser, array, name))
3132 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3133 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3134 tfield->expression.next = telem;
3135 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3136 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3144 if (parser->tok == ';') {
3145 ast_delete(basetype);
3146 if (!parser_next(parser)) {
3147 parseerror(parser, "error after variable declaration");
3153 if (parser->tok == ',')
3156 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3157 parseerror(parser, "missing comma or semicolon while parsing variables");
3161 if (localblock && opts_standard == COMPILER_QCC) {
3162 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3163 "initializing expression turns variable `%s` into a constant in this standard",
3170 if (parser->tok != '{') {
3171 if (parser->tok != '=') {
3172 parseerror(parser, "missing semicolon or initializer");
3176 if (!parser_next(parser)) {
3177 parseerror(parser, "error parsing initializer");
3181 else if (opts_standard == COMPILER_QCC) {
3182 parseerror(parser, "expected '=' before function body in this standard");
3185 if (parser->tok == '#') {
3189 parseerror(parser, "cannot declare builtins within functions");
3192 if (var->expression.vtype != TYPE_FUNCTION) {
3193 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3196 if (!parser_next(parser)) {
3197 parseerror(parser, "expected builtin number");
3200 if (parser->tok != TOKEN_INTCONST) {
3201 parseerror(parser, "builtin number must be an integer constant");
3204 if (parser_token(parser)->constval.i <= 0) {
3205 parseerror(parser, "builtin number must be an integer greater than zero");
3209 func = ast_function_new(ast_ctx(var), var->name, var);
3211 parseerror(parser, "failed to allocate function for `%s`", var->name);
3214 vec_push(parser->functions, func);
3216 func->builtin = -parser_token(parser)->constval.i;
3218 if (!parser_next(parser)) {
3219 parseerror(parser, "expected comma or semicolon");
3220 ast_function_delete(func);
3221 var->constval.vfunc = NULL;
3225 else if (parser->tok == '{' || parser->tok == '[')
3228 parseerror(parser, "cannot declare functions within functions");
3232 if (!parse_function_body(parser, var))
3234 ast_delete(basetype);
3237 ast_expression *cexp;
3240 cexp = parse_expression_leave(parser, true);
3245 cval = (ast_value*)cexp;
3246 if (!ast_istype(cval, ast_value) || !cval->isconst)
3247 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3250 var->isconst = true;
3251 if (cval->expression.vtype == TYPE_STRING)
3252 var->constval.vstring = parser_strdup(cval->constval.vstring);
3254 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3258 shunt sy = { NULL, NULL };
3259 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
3260 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
3261 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
3262 if (!parser_sy_pop(parser, &sy))
3265 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
3266 parseerror(parser, "internal error: leaked operands");
3267 vec_push(localblock->exprs, (ast_expression*)sy.out[0].out);
3275 if (parser->tok == ',') {
3276 if (!parser_next(parser)) {
3277 parseerror(parser, "expected another variable");
3281 if (parser->tok != TOKEN_IDENT) {
3282 parseerror(parser, "expected another variable");
3285 var = ast_value_copy(basetype);
3287 ast_value_set_name(var, parser_tokval(parser));
3288 if (!parser_next(parser)) {
3289 parseerror(parser, "error parsing variable declaration");
3295 if (parser->tok != ';') {
3296 parseerror(parser, "missing semicolon after variables");
3300 if (!parser_next(parser)) {
3301 parseerror(parser, "parse error after variable declaration");
3305 ast_delete(basetype);
3309 if (cleanvar && var)
3311 ast_delete(basetype);
3315 ast_delete(basetype);
3316 if (cleanvar && var)
3318 if (varent.name) mem_d(varent.name);
3319 if (ve[0].name) mem_d(ve[0].name);
3320 if (ve[1].name) mem_d(ve[1].name);
3321 if (ve[2].name) mem_d(ve[2].name);
3322 if (ve[0].var) mem_d(ve[0].var);
3323 if (ve[1].var) mem_d(ve[1].var);
3324 if (ve[2].var) mem_d(ve[2].var);
3328 static bool parser_global_statement(parser_t *parser)
3330 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3332 return parse_variable(parser, NULL, false);
3334 else if (parser->tok == TOKEN_KEYWORD)
3336 /* handle 'var' and 'const' */
3337 if (!strcmp(parser_tokval(parser), "var")) {
3338 if (!parser_next(parser)) {
3339 parseerror(parser, "expected variable declaration after 'var'");
3342 return parse_variable(parser, NULL, true);
3346 else if (parser->tok == '$')
3348 if (!parser_next(parser)) {
3349 parseerror(parser, "parse error");
3355 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3361 static parser_t *parser;
3366 parser = (parser_t*)mem_a(sizeof(parser_t));
3370 memset(parser, 0, sizeof(*parser));
3372 for (i = 0; i < operator_count; ++i) {
3373 if (operators[i].id == opid1('=')) {
3374 parser->assign_op = operators+i;
3378 if (!parser->assign_op) {
3379 printf("internal error: initializing parser: failed to find assign operator\n");
3386 bool parser_compile()
3388 /* initial lexer/parser state */
3389 parser->lex->flags.noops = true;
3391 if (parser_next(parser))
3393 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3395 if (!parser_global_statement(parser)) {
3396 if (parser->tok == TOKEN_EOF)
3397 parseerror(parser, "unexpected eof");
3398 else if (!parser->errors)
3399 parseerror(parser, "there have been errors, bailing out");
3400 lex_close(parser->lex);
3406 parseerror(parser, "parse error");
3407 lex_close(parser->lex);
3412 lex_close(parser->lex);
3415 return !parser->errors;
3418 bool parser_compile_file(const char *filename)
3420 parser->lex = lex_open(filename);
3422 con_err("failed to open file \"%s\"\n", filename);
3425 return parser_compile();
3428 bool parser_compile_string_len(const char *name, const char *str, size_t len)
3430 parser->lex = lex_open_string(str, len, name);
3432 con_err("failed to create lexer for string \"%s\"\n", name);
3435 return parser_compile();
3438 bool parser_compile_string(const char *name, const char *str)
3440 parser->lex = lex_open_string(str, strlen(str), name);
3442 con_err("failed to create lexer for string \"%s\"\n", name);
3445 return parser_compile();
3448 void parser_cleanup()
3451 for (i = 0; i < vec_size(parser->accessors); ++i) {
3452 ast_delete(parser->accessors[i]->constval.vfunc);
3453 parser->accessors[i]->constval.vfunc = NULL;
3454 ast_delete(parser->accessors[i]);
3456 for (i = 0; i < vec_size(parser->functions); ++i) {
3457 ast_delete(parser->functions[i]);
3459 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3460 ast_delete(parser->imm_vector[i]);
3462 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3463 ast_delete(parser->imm_string[i]);
3465 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3466 ast_delete(parser->imm_float[i]);
3468 for (i = 0; i < vec_size(parser->fields); ++i) {
3469 ast_delete(parser->fields[i].var);
3470 mem_d(parser->fields[i].name);
3472 for (i = 0; i < vec_size(parser->globals); ++i) {
3473 ast_delete(parser->globals[i].var);
3474 mem_d(parser->globals[i].name);
3476 vec_free(parser->accessors);
3477 vec_free(parser->functions);
3478 vec_free(parser->imm_vector);
3479 vec_free(parser->imm_string);
3480 vec_free(parser->imm_float);
3481 vec_free(parser->globals);
3482 vec_free(parser->fields);
3483 vec_free(parser->locals);
3488 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3490 return util_crc16(old, str, strlen(str));
3493 static void progdefs_crc_file(const char *str)
3495 /* write to progdefs.h here */
3498 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3500 old = progdefs_crc_sum(old, str);
3501 progdefs_crc_file(str);
3505 static void generate_checksum(parser_t *parser)
3507 uint16_t crc = 0xFFFF;
3510 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3511 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3513 progdefs_crc_file("\tint\tpad;\n");
3514 progdefs_crc_file("\tint\tofs_return[3];\n");
3515 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3516 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3517 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3518 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3519 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3520 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3521 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3522 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3524 for (i = 0; i < parser->crc_globals; ++i) {
3525 if (!ast_istype(parser->globals[i].var, ast_value))
3527 switch (parser->globals[i].var->expression.vtype) {
3528 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3529 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3530 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3531 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3533 crc = progdefs_crc_both(crc, "\tint\t");
3536 crc = progdefs_crc_both(crc, parser->globals[i].name);
3537 crc = progdefs_crc_both(crc, ";\n");
3539 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3540 for (i = 0; i < parser->crc_fields; ++i) {
3541 if (!ast_istype(parser->fields[i].var, ast_value))
3543 switch (parser->fields[i].var->expression.next->expression.vtype) {
3544 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3545 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3546 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3547 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3549 crc = progdefs_crc_both(crc, "\tint\t");
3552 crc = progdefs_crc_both(crc, parser->fields[i].name);
3553 crc = progdefs_crc_both(crc, ";\n");
3555 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3560 bool parser_finish(const char *output)
3566 if (!parser->errors)
3568 ir = ir_builder_new("gmqcc_out");
3570 con_out("failed to allocate builder\n");
3574 for (i = 0; i < vec_size(parser->fields); ++i) {
3577 if (!ast_istype(parser->fields[i].var, ast_value))
3579 field = (ast_value*)parser->fields[i].var;
3580 isconst = field->isconst;
3581 field->isconst = false;
3582 if (!ast_global_codegen((ast_value*)field, ir, true)) {
3583 con_out("failed to generate field %s\n", field->name);
3584 ir_builder_delete(ir);
3589 ast_expression *subtype;
3590 field->isconst = true;
3591 subtype = field->expression.next;
3592 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
3593 if (subtype->expression.vtype == TYPE_FIELD)
3594 ifld->fieldtype = subtype->expression.next->expression.vtype;
3595 else if (subtype->expression.vtype == TYPE_FUNCTION)
3596 ifld->outtype = subtype->expression.next->expression.vtype;
3597 (void)!ir_value_set_field(field->ir_v, ifld);
3600 for (i = 0; i < vec_size(parser->globals); ++i) {
3602 if (!ast_istype(parser->globals[i].var, ast_value))
3604 asvalue = (ast_value*)(parser->globals[i].var);
3605 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
3606 if (strcmp(asvalue->name, "end_sys_globals") &&
3607 strcmp(asvalue->name, "end_sys_fields"))
3609 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
3610 "unused global: `%s`", asvalue->name);
3613 if (!ast_global_codegen(asvalue, ir, false)) {
3614 con_out("failed to generate global %s\n", parser->globals[i].name);
3615 ir_builder_delete(ir);
3619 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3620 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
3621 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
3622 ir_builder_delete(ir);
3626 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3627 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
3628 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
3629 ir_builder_delete(ir);
3633 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3634 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
3635 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
3636 ir_builder_delete(ir);
3640 for (i = 0; i < vec_size(parser->globals); ++i) {
3642 if (!ast_istype(parser->globals[i].var, ast_value))
3644 asvalue = (ast_value*)(parser->globals[i].var);
3645 if (asvalue->setter) {
3646 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3647 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3648 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3650 printf("failed to generate setter for %s\n", parser->globals[i].name);
3651 ir_builder_delete(ir);
3655 if (asvalue->getter) {
3656 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3657 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3658 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3660 printf("failed to generate getter for %s\n", parser->globals[i].name);
3661 ir_builder_delete(ir);
3666 for (i = 0; i < vec_size(parser->fields); ++i) {
3668 asvalue = (ast_value*)(parser->fields[i].var->expression.next);
3670 if (!ast_istype((ast_expression*)asvalue, ast_value))
3672 if (asvalue->expression.vtype != TYPE_ARRAY)
3674 if (asvalue->setter) {
3675 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3676 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3677 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3679 printf("failed to generate setter for %s\n", parser->fields[i].name);
3680 ir_builder_delete(ir);
3684 if (asvalue->getter) {
3685 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3686 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3687 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3689 printf("failed to generate getter for %s\n", parser->fields[i].name);
3690 ir_builder_delete(ir);
3695 for (i = 0; i < vec_size(parser->functions); ++i) {
3696 if (!ast_function_codegen(parser->functions[i], ir)) {
3697 con_out("failed to generate function %s\n", parser->functions[i]->name);
3698 ir_builder_delete(ir);
3701 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
3702 con_out("failed to finalize function %s\n", parser->functions[i]->name);
3703 ir_builder_delete(ir);
3710 ir_builder_dump(ir, con_out);
3712 generate_checksum(parser);
3714 if (!ir_builder_generate(ir, output)) {
3715 con_out("*** failed to generate output file\n");
3716 ir_builder_delete(ir);
3721 ir_builder_delete(ir);
3725 con_out("*** there were compile errors\n");