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 bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
55 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
56 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
58 static void parseerror(parser_t *parser, const char *fmt, ...)
65 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
69 /* returns true if it counts as an error */
70 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
73 int lvl = LVL_WARNING;
75 if (!OPTS_WARN(warntype))
84 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
90 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
93 int lvl = LVL_WARNING;
95 if (!OPTS_WARN(warntype))
102 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
108 /**********************************************************************
109 * some maths used for constant folding
112 vector vec3_add(vector a, vector b)
121 vector vec3_sub(vector a, vector b)
130 qcfloat vec3_mulvv(vector a, vector b)
132 return (a.x * b.x + a.y * b.y + a.z * b.z);
135 vector vec3_mulvf(vector a, float b)
144 /**********************************************************************
148 bool parser_next(parser_t *parser)
150 /* lex_do kills the previous token */
151 parser->tok = lex_do(parser->lex);
152 if (parser->tok == TOKEN_EOF)
154 if (parser->tok >= TOKEN_ERROR) {
155 parseerror(parser, "lex error");
161 #define parser_tokval(p) ((p)->lex->tok.value)
162 #define parser_token(p) (&((p)->lex->tok))
163 #define parser_ctx(p) ((p)->lex->tok.ctx)
165 static ast_value* parser_const_float(parser_t *parser, double d)
169 for (i = 0; i < vec_size(parser->imm_float); ++i) {
170 if (parser->imm_float[i]->constval.vfloat == d)
171 return parser->imm_float[i];
173 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
175 out->constval.vfloat = d;
176 vec_push(parser->imm_float, out);
180 static ast_value* parser_const_float_0(parser_t *parser)
182 if (!parser->imm_float_zero)
183 parser->imm_float_zero = parser_const_float(parser, 0);
184 return parser->imm_float_zero;
187 static char *parser_strdup(const char *str)
190 /* actually dup empty strings */
191 char *out = mem_a(1);
195 return util_strdup(str);
198 static ast_value* parser_const_string(parser_t *parser, const char *str)
202 for (i = 0; i < vec_size(parser->imm_string); ++i) {
203 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
204 return parser->imm_string[i];
206 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
208 out->constval.vstring = parser_strdup(str);
209 vec_push(parser->imm_string, out);
213 static ast_value* parser_const_vector(parser_t *parser, vector v)
217 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
218 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
219 return parser->imm_vector[i];
221 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
223 out->constval.vvec = v;
224 vec_push(parser->imm_vector, out);
228 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
234 return parser_const_vector(parser, v);
237 static ast_value* parser_const_vector_0(parser_t *parser)
239 if (!parser->imm_vector_zero)
240 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
241 return parser->imm_vector_zero;
244 static ast_expression* parser_find_field(parser_t *parser, const char *name)
247 for (i = 0; i < vec_size(parser->fields); ++i) {
248 if (!strcmp(parser->fields[i].name, name))
249 return parser->fields[i].var;
254 static ast_expression* parser_find_global(parser_t *parser, const char *name)
257 for (i = 0; i < vec_size(parser->globals); ++i) {
258 if (!strcmp(parser->globals[i].name, name))
259 return parser->globals[i].var;
264 static ast_expression* parser_find_param(parser_t *parser, const char *name)
268 if (!parser->function)
270 fun = parser->function->vtype;
271 for (i = 0; i < vec_size(fun->expression.params); ++i) {
272 if (!strcmp(fun->expression.params[i]->name, name))
273 return (ast_expression*)(fun->expression.params[i]);
278 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
282 for (i = vec_size(parser->locals); i > upto;) {
284 if (!strcmp(parser->locals[i].name, name))
285 return parser->locals[i].var;
288 return parser_find_param(parser, name);
291 static ast_expression* parser_find_var(parser_t *parser, const char *name)
295 v = parser_find_local(parser, name, 0, &dummy);
296 if (!v) v = parser_find_global(parser, name);
302 size_t etype; /* 0 = expression, others are operators */
306 ast_block *block; /* for commas and function calls */
315 #define SY_PAREN_EXPR '('
316 #define SY_PAREN_FUNC 'f'
317 #define SY_PAREN_INDEX '['
319 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
330 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
334 e.out = (ast_expression*)v;
341 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
343 e.etype = 1 + (op - operators);
352 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
364 # define DEBUGSHUNTDO(x) x
366 # define DEBUGSHUNTDO(x)
369 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
370 * so we need to rotate it to become ent.(foo[n]).
372 static bool rotate_entfield_array_index_nodes(ast_expression **out)
374 ast_array_index *index;
375 ast_entfield *entfield;
379 ast_expression *entity;
381 lex_ctx ctx = ast_ctx(*out);
383 if (!ast_istype(*out, ast_array_index))
385 index = (ast_array_index*)*out;
387 if (!ast_istype(index->array, ast_entfield))
389 entfield = (ast_entfield*)index->array;
391 if (!ast_istype(entfield->field, ast_value))
393 field = (ast_value*)entfield->field;
396 entity = entfield->entity;
400 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
401 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
402 *out = (ast_expression*)entfield;
407 static bool parser_sy_pop(parser_t *parser, shunt *sy)
411 ast_expression *out = NULL;
412 ast_expression *exprs[3];
413 ast_block *blocks[3];
414 ast_value *asvalue[3];
416 qcint generated_op = 0;
421 if (!vec_size(sy->ops)) {
422 parseerror(parser, "internal error: missing operator");
426 if (sy->ops[vec_size(sy->ops)-1].paren) {
427 parseerror(parser, "unmatched parenthesis");
431 op = &operators[sy->ops[vec_size(sy->ops)-1].etype - 1];
432 ctx = sy->ops[vec_size(sy->ops)-1].ctx;
434 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
436 if (vec_size(sy->out) < op->operands) {
437 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
438 op->op, (int)op->id);
442 vec_shrinkby(sy->ops, 1);
444 vec_shrinkby(sy->out, op->operands);
445 for (i = 0; i < op->operands; ++i) {
446 exprs[i] = sy->out[vec_size(sy->out)+i].out;
447 blocks[i] = sy->out[vec_size(sy->out)+i].block;
448 asvalue[i] = (ast_value*)exprs[i];
451 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
452 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
456 #define NotSameType(T) \
457 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
458 exprs[0]->expression.vtype != T)
459 #define CanConstFold1(A) \
460 (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
461 #define CanConstFold(A, B) \
462 (CanConstFold1(A) && CanConstFold1(B))
463 #define ConstV(i) (asvalue[(i)]->constval.vvec)
464 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
465 #define ConstS(i) (asvalue[(i)]->constval.vstring)
469 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
473 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
474 if (exprs[1]->expression.vtype != TYPE_FIELD) {
475 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
478 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
480 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
481 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
485 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
491 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
492 !(exprs[0]->expression.vtype == TYPE_FIELD &&
493 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
495 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
496 parseerror(parser, "cannot index value of type %s", ty1);
499 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
500 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
501 parseerror(parser, "index must be of type float, not %s", ty1);
504 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
505 if (rotate_entfield_array_index_nodes(&out))
507 if (opts_standard != COMPILER_GMQCC) {
508 /* this error doesn't need to make us bail out */
509 (void)!parsewarning(parser, WARN_EXTENSIONS,
510 "accessing array-field members of an entity without parenthesis\n"
511 " -> this is an extension from -std=gmqcc");
518 vec_push(blocks[0]->exprs, exprs[1]);
520 blocks[0] = ast_block_new(ctx);
521 vec_push(blocks[0]->exprs, exprs[0]);
522 vec_push(blocks[0]->exprs, exprs[1]);
524 if (!ast_block_set_type(blocks[0], exprs[1]))
527 vec_push(sy->out, syblock(ctx, blocks[0]));
531 switch (exprs[0]->expression.vtype) {
533 if (CanConstFold1(exprs[0]))
534 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
536 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
537 (ast_expression*)parser_const_float_0(parser),
541 if (CanConstFold1(exprs[0]))
542 out = (ast_expression*)parser_const_vector_f(parser,
543 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
545 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
546 (ast_expression*)parser_const_vector_0(parser),
550 parseerror(parser, "invalid types used in expression: cannot negate type %s",
551 type_name[exprs[0]->expression.vtype]);
557 switch (exprs[0]->expression.vtype) {
559 if (CanConstFold1(exprs[0]))
560 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
562 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
565 if (CanConstFold1(exprs[0]))
566 out = (ast_expression*)parser_const_float(parser,
567 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
569 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
572 if (CanConstFold1(exprs[0]))
573 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
575 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
577 /* we don't constant-fold NOT for these types */
579 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
582 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
585 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
586 type_name[exprs[0]->expression.vtype]);
592 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
593 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
595 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
596 type_name[exprs[0]->expression.vtype],
597 type_name[exprs[1]->expression.vtype]);
600 switch (exprs[0]->expression.vtype) {
602 if (CanConstFold(exprs[0], exprs[1]))
604 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
607 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
610 if (CanConstFold(exprs[0], exprs[1]))
611 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
613 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
616 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
617 type_name[exprs[0]->expression.vtype],
618 type_name[exprs[1]->expression.vtype]);
623 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
624 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
626 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
627 type_name[exprs[1]->expression.vtype],
628 type_name[exprs[0]->expression.vtype]);
631 switch (exprs[0]->expression.vtype) {
633 if (CanConstFold(exprs[0], exprs[1]))
634 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
636 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
639 if (CanConstFold(exprs[0], exprs[1]))
640 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
642 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
645 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
646 type_name[exprs[1]->expression.vtype],
647 type_name[exprs[0]->expression.vtype]);
652 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
653 exprs[0]->expression.vtype != TYPE_VECTOR &&
654 exprs[0]->expression.vtype != TYPE_FLOAT &&
655 exprs[1]->expression.vtype != TYPE_VECTOR &&
656 exprs[1]->expression.vtype != TYPE_FLOAT)
658 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
659 type_name[exprs[1]->expression.vtype],
660 type_name[exprs[0]->expression.vtype]);
663 switch (exprs[0]->expression.vtype) {
665 if (exprs[1]->expression.vtype == TYPE_VECTOR)
667 if (CanConstFold(exprs[0], exprs[1]))
668 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
670 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
674 if (CanConstFold(exprs[0], exprs[1]))
675 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
677 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
681 if (exprs[1]->expression.vtype == TYPE_FLOAT)
683 if (CanConstFold(exprs[0], exprs[1]))
684 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
686 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
690 if (CanConstFold(exprs[0], exprs[1]))
691 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
693 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
697 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
698 type_name[exprs[1]->expression.vtype],
699 type_name[exprs[0]->expression.vtype]);
704 if (NotSameType(TYPE_FLOAT)) {
705 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
706 type_name[exprs[0]->expression.vtype],
707 type_name[exprs[1]->expression.vtype]);
710 if (CanConstFold(exprs[0], exprs[1]))
711 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
713 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
717 parseerror(parser, "qc does not have a modulo operator");
721 if (NotSameType(TYPE_FLOAT)) {
722 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
723 type_name[exprs[0]->expression.vtype],
724 type_name[exprs[1]->expression.vtype]);
727 if (CanConstFold(exprs[0], exprs[1]))
728 out = (ast_expression*)parser_const_float(parser,
729 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
730 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
732 out = (ast_expression*)ast_binary_new(ctx,
733 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
737 parseerror(parser, "TODO: bitxor");
742 case opid3('<','<','='):
743 case opid3('>','>','='):
744 parseerror(parser, "TODO: shifts");
748 generated_op += 1; /* INSTR_OR */
750 generated_op += INSTR_AND;
751 if (NotSameType(TYPE_FLOAT)) {
752 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
753 type_name[exprs[0]->expression.vtype],
754 type_name[exprs[1]->expression.vtype]);
755 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
758 if (CanConstFold(exprs[0], exprs[1]))
759 out = (ast_expression*)parser_const_float(parser,
760 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
762 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
766 generated_op += 1; /* INSTR_GT */
768 generated_op += 1; /* INSTR_LT */
769 case opid2('>', '='):
770 generated_op += 1; /* INSTR_GE */
771 case opid2('<', '='):
772 generated_op += INSTR_LE;
773 if (NotSameType(TYPE_FLOAT)) {
774 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
775 type_name[exprs[0]->expression.vtype],
776 type_name[exprs[1]->expression.vtype]);
779 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
781 case opid2('!', '='):
782 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
783 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
784 type_name[exprs[0]->expression.vtype],
785 type_name[exprs[1]->expression.vtype]);
788 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
790 case opid2('=', '='):
791 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
792 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
793 type_name[exprs[0]->expression.vtype],
794 type_name[exprs[1]->expression.vtype]);
797 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
801 if (ast_istype(exprs[0], ast_entfield)) {
802 ast_expression *field = ((ast_entfield*)exprs[0])->field;
803 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
804 exprs[0]->expression.vtype == TYPE_FIELD &&
805 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
807 assignop = type_storep_instr[TYPE_VECTOR];
810 assignop = type_storep_instr[exprs[0]->expression.vtype];
811 if (!ast_compare_type(field->expression.next, exprs[1])) {
812 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
813 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
814 if (opts_standard == COMPILER_QCC &&
815 field->expression.next->expression.vtype == TYPE_FUNCTION &&
816 exprs[1]->expression.vtype == TYPE_FUNCTION)
818 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
819 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
825 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
830 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
831 exprs[0]->expression.vtype == TYPE_FIELD &&
832 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
834 assignop = type_store_instr[TYPE_VECTOR];
837 assignop = type_store_instr[exprs[0]->expression.vtype];
840 if (assignop == AINSTR_END) {
841 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
842 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
843 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
845 else if (!ast_compare_type(exprs[0], exprs[1])) {
846 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
847 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
848 if (opts_standard == COMPILER_QCC &&
849 exprs[0]->expression.vtype == TYPE_FUNCTION &&
850 exprs[1]->expression.vtype == TYPE_FUNCTION)
852 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
853 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
859 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
862 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
866 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
867 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
869 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
870 type_name[exprs[0]->expression.vtype],
871 type_name[exprs[1]->expression.vtype]);
874 if (ast_istype(exprs[0], ast_entfield))
875 assignop = type_storep_instr[exprs[0]->expression.vtype];
877 assignop = type_store_instr[exprs[0]->expression.vtype];
878 switch (exprs[0]->expression.vtype) {
880 out = (ast_expression*)ast_binstore_new(ctx, assignop,
881 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
885 out = (ast_expression*)ast_binstore_new(ctx, assignop,
886 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
890 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
891 type_name[exprs[0]->expression.vtype],
892 type_name[exprs[1]->expression.vtype]);
900 parseerror(parser, "failed to apply operand %s", op->op);
904 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
905 vec_push(sy->out, syexp(ctx, out));
909 static bool parser_close_call(parser_t *parser, shunt *sy)
911 /* was a function call */
918 vec_shrinkby(sy->ops, 1);
919 fid = sy->ops[vec_size(sy->ops)].off;
921 /* out[fid] is the function
922 * everything above is parameters...
924 * 1 params = ast_expression
928 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
929 parseerror(parser, "internal error: function call needs function and parameter list...");
933 fun = sy->out[fid].out;
935 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
937 parseerror(parser, "out of memory");
941 if (fid+1 == vec_size(sy->out)) {
944 } else if (fid+2 == vec_size(sy->out)) {
946 vec_shrinkby(sy->out, 1);
947 params = sy->out[vec_size(sy->out)].block;
951 vec_push(call->params, sy->out[vec_size(sy->out)].out);
953 paramcount = vec_size(params->exprs);
954 call->params = params->exprs;
955 params->exprs = NULL;
958 if (!ast_call_check_types(call))
961 parseerror(parser, "invalid function call");
965 /* overwrite fid, the function, with a call */
966 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
968 if (fun->expression.vtype != TYPE_FUNCTION) {
969 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
973 if (!fun->expression.next) {
974 parseerror(parser, "could not determine function return type");
977 if (vec_size(fun->expression.params) != paramcount &&
978 !(fun->expression.variadic &&
979 vec_size(fun->expression.params) < paramcount))
982 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
984 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
985 if (opts_standard == COMPILER_GMQCC)
988 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
989 " -> `%s` has been declared here: %s:%i",
990 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
991 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
993 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
994 " -> `%s` has been declared here: %s:%i",
995 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
996 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1002 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1003 "too %s parameters for call to %s: expected %i, got %i\n"
1004 " -> `%s` has been declared here: %s:%i",
1005 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1006 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1008 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1009 "too %s parameters for function call: expected %i, got %i\n"
1010 " -> `%s` has been declared here: %s:%i",
1011 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1012 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1020 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1022 if (!vec_size(sy->ops)) {
1023 parseerror(parser, "unmatched closing paren");
1026 /* this would for bit a + (x) because there are no operators inside (x)
1027 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1028 parseerror(parser, "empty parenthesis expression");
1032 while (vec_size(sy->ops)) {
1033 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1034 if (!parser_close_call(parser, sy))
1038 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1039 vec_shrinkby(sy->ops, 1);
1040 return !functions_only;
1042 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1045 /* pop off the parenthesis */
1046 vec_shrinkby(sy->ops, 1);
1047 /* then apply the index operator */
1048 if (!parser_sy_pop(parser, sy))
1052 if (!parser_sy_pop(parser, sy))
1058 static void parser_reclassify_token(parser_t *parser)
1061 for (i = 0; i < operator_count; ++i) {
1062 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1063 parser->tok = TOKEN_OPERATOR;
1069 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1071 ast_expression *expr = NULL;
1073 bool wantop = false;
1074 bool gotmemberof = false;
1076 /* count the parens because an if starts with one, so the
1077 * end of a condition is an unmatched closing paren
1085 parser->lex->flags.noops = false;
1087 parser_reclassify_token(parser);
1092 gotmemberof = false;
1094 parser->memberof = 0;
1096 if (parser->tok == TOKEN_IDENT)
1098 ast_expression *var;
1100 parseerror(parser, "expected operator or end of statement");
1105 if (opts_standard == COMPILER_GMQCC)
1107 if (parser->memberof == TYPE_ENTITY) {
1108 /* still get vars first since there could be a fieldpointer */
1109 var = parser_find_var(parser, parser_tokval(parser));
1111 var = parser_find_field(parser, parser_tokval(parser));
1113 else if (parser->memberof == TYPE_VECTOR)
1115 parseerror(parser, "TODO: implement effective vector member access");
1118 else if (parser->memberof) {
1119 parseerror(parser, "namespace for member not found");
1123 var = parser_find_var(parser, parser_tokval(parser));
1125 var = parser_find_var(parser, parser_tokval(parser));
1127 var = parser_find_field(parser, parser_tokval(parser));
1130 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1133 if (ast_istype(var, ast_value))
1134 ((ast_value*)var)->uses++;
1135 vec_push(sy.out, syexp(parser_ctx(parser), var));
1136 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1138 else if (parser->tok == TOKEN_FLOATCONST) {
1141 parseerror(parser, "expected operator or end of statement, got constant");
1145 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1148 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1149 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1151 else if (parser->tok == TOKEN_INTCONST) {
1154 parseerror(parser, "expected operator or end of statement, got constant");
1158 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1161 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1162 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1164 else if (parser->tok == TOKEN_STRINGCONST) {
1167 parseerror(parser, "expected operator or end of statement, got constant");
1171 val = parser_const_string(parser, parser_tokval(parser));
1174 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1175 DEBUGSHUNTDO(con_out("push string\n"));
1177 else if (parser->tok == TOKEN_VECTORCONST) {
1180 parseerror(parser, "expected operator or end of statement, got constant");
1184 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1187 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1188 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1189 parser_token(parser)->constval.v.x,
1190 parser_token(parser)->constval.v.y,
1191 parser_token(parser)->constval.v.z));
1193 else if (parser->tok == '(') {
1194 parseerror(parser, "internal error: '(' should be classified as operator");
1197 else if (parser->tok == '[') {
1198 parseerror(parser, "internal error: '[' should be classified as operator");
1201 else if (parser->tok == ')') {
1203 DEBUGSHUNTDO(con_out("do[op] )\n"));
1207 /* we do expect an operator next */
1208 /* closing an opening paren */
1209 if (!parser_close_paren(parser, &sy, false))
1212 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1216 /* allowed for function calls */
1217 if (!parser_close_paren(parser, &sy, true))
1222 else if (parser->tok == ']') {
1224 parseerror(parser, "operand expected");
1228 if (!parser_close_paren(parser, &sy, false))
1232 else if (parser->tok != TOKEN_OPERATOR) {
1234 parseerror(parser, "expected operator or end of statement");
1241 /* classify the operator */
1242 /* TODO: suffix operators */
1243 const oper_info *op;
1244 const oper_info *olast = NULL;
1246 for (o = 0; o < operator_count; ++o) {
1247 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1248 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1249 !strcmp(parser_tokval(parser), operators[o].op))
1254 if (o == operator_count) {
1255 /* no operator found... must be the end of the statement */
1258 /* found an operator */
1261 /* when declaring variables, a comma starts a new variable */
1262 if (op->id == opid1(',') && !parens && stopatcomma) {
1263 /* fixup the token */
1268 /* a colon without a pervious question mark cannot be a ternary */
1269 if (op->id == opid2(':','?')) {
1274 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1275 olast = &operators[vec_last(sy.ops).etype-1];
1278 (op->prec < olast->prec) ||
1279 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1281 if (!parser_sy_pop(parser, &sy))
1283 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1284 olast = &operators[vec_last(sy.ops).etype-1];
1289 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1290 /* for gmqcc standard: open up the namespace of the previous type */
1291 ast_expression *prevex = vec_last(sy.out).out;
1293 parseerror(parser, "unexpected member operator");
1296 if (prevex->expression.vtype == TYPE_ENTITY)
1297 parser->memberof = TYPE_ENTITY;
1298 else if (prevex->expression.vtype == TYPE_VECTOR)
1299 parser->memberof = TYPE_VECTOR;
1301 parseerror(parser, "type error: type has no members");
1307 if (op->id == opid1('(')) {
1309 size_t sycount = vec_size(sy.out);
1310 DEBUGSHUNTDO(con_out("push [op] (\n"));
1312 /* we expected an operator, this is the function-call operator */
1313 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1316 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1317 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1320 } else if (op->id == opid1('[')) {
1322 parseerror(parser, "unexpected array subscript");
1326 /* push both the operator and the paren, this makes life easier */
1327 vec_push(sy.ops, syop(parser_ctx(parser), op));
1328 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1330 } else if (op->id == opid2('?',':')) {
1332 vec_push(sy.ops, syop(parser_ctx(parser), op));
1335 } else if (op->id == opid2(':','?')) {
1336 /* we don't push this operator */
1340 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1341 vec_push(sy.ops, syop(parser_ctx(parser), op));
1345 if (!parser_next(parser)) {
1348 if (parser->tok == ';' ||
1349 (!parens && parser->tok == ']'))
1355 while (vec_size(sy.ops)) {
1356 if (!parser_sy_pop(parser, &sy))
1360 parser->lex->flags.noops = true;
1361 if (!vec_size(sy.out)) {
1362 parseerror(parser, "empty expression");
1365 expr = sy.out[0].out;
1368 DEBUGSHUNTDO(con_out("shunt done\n"));
1372 parser->lex->flags.noops = true;
1378 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1380 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1383 if (!parser_next(parser)) {
1390 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1393 ast_expression *cond, *ontrue, *onfalse = NULL;
1396 lex_ctx ctx = parser_ctx(parser);
1398 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1399 if (!parser_next(parser)) {
1400 parseerror(parser, "expected condition or 'not'");
1403 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "not")) {
1405 if (!parser_next(parser)) {
1406 parseerror(parser, "expected condition in parenthesis");
1410 if (parser->tok != '(') {
1411 parseerror(parser, "expected 'if' condition in parenthesis");
1414 /* parse into the expression */
1415 if (!parser_next(parser)) {
1416 parseerror(parser, "expected 'if' condition after opening paren");
1419 /* parse the condition */
1420 cond = parse_expression_leave(parser, false);
1424 if (parser->tok != ')') {
1425 parseerror(parser, "expected closing paren after 'if' condition");
1429 /* parse into the 'then' branch */
1430 if (!parser_next(parser)) {
1431 parseerror(parser, "expected statement for on-true branch of 'if'");
1435 ontrue = parse_statement_or_block(parser);
1440 /* check for an else */
1441 if (!strcmp(parser_tokval(parser), "else")) {
1442 /* parse into the 'else' branch */
1443 if (!parser_next(parser)) {
1444 parseerror(parser, "expected on-false branch after 'else'");
1449 onfalse = parse_statement_or_block(parser);
1458 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1460 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1461 *out = (ast_expression*)ifthen;
1465 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1468 ast_expression *cond, *ontrue;
1470 lex_ctx ctx = parser_ctx(parser);
1472 /* skip the 'while' and check for opening paren */
1473 if (!parser_next(parser) || parser->tok != '(') {
1474 parseerror(parser, "expected 'while' condition in parenthesis");
1477 /* parse into the expression */
1478 if (!parser_next(parser)) {
1479 parseerror(parser, "expected 'while' condition after opening paren");
1482 /* parse the condition */
1483 cond = parse_expression_leave(parser, false);
1487 if (parser->tok != ')') {
1488 parseerror(parser, "expected closing paren after 'while' condition");
1492 /* parse into the 'then' branch */
1493 if (!parser_next(parser)) {
1494 parseerror(parser, "expected while-loop body");
1498 ontrue = parse_statement_or_block(parser);
1504 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1505 *out = (ast_expression*)aloop;
1509 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1512 ast_expression *cond, *ontrue;
1514 lex_ctx ctx = parser_ctx(parser);
1516 /* skip the 'do' and get the body */
1517 if (!parser_next(parser)) {
1518 parseerror(parser, "expected loop body");
1521 ontrue = parse_statement_or_block(parser);
1525 /* expect the "while" */
1526 if (parser->tok != TOKEN_KEYWORD ||
1527 strcmp(parser_tokval(parser), "while"))
1529 parseerror(parser, "expected 'while' and condition");
1534 /* skip the 'while' and check for opening paren */
1535 if (!parser_next(parser) || parser->tok != '(') {
1536 parseerror(parser, "expected 'while' condition in parenthesis");
1540 /* parse into the expression */
1541 if (!parser_next(parser)) {
1542 parseerror(parser, "expected 'while' condition after opening paren");
1546 /* parse the condition */
1547 cond = parse_expression_leave(parser, false);
1551 if (parser->tok != ')') {
1552 parseerror(parser, "expected closing paren after 'while' condition");
1558 if (!parser_next(parser) || parser->tok != ';') {
1559 parseerror(parser, "expected semicolon after condition");
1565 if (!parser_next(parser)) {
1566 parseerror(parser, "parse error");
1572 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1573 *out = (ast_expression*)aloop;
1577 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1580 ast_expression *initexpr, *cond, *increment, *ontrue;
1581 size_t oldblocklocal;
1584 lex_ctx ctx = parser_ctx(parser);
1586 oldblocklocal = parser->blocklocal;
1587 parser->blocklocal = vec_size(parser->locals);
1594 /* skip the 'while' and check for opening paren */
1595 if (!parser_next(parser) || parser->tok != '(') {
1596 parseerror(parser, "expected 'for' expressions in parenthesis");
1599 /* parse into the expression */
1600 if (!parser_next(parser)) {
1601 parseerror(parser, "expected 'for' initializer after opening paren");
1605 if (parser->tok == TOKEN_TYPENAME) {
1606 if (opts_standard != COMPILER_GMQCC) {
1607 if (parsewarning(parser, WARN_EXTENSIONS,
1608 "current standard does not allow variable declarations in for-loop initializers"))
1612 parseerror(parser, "TODO: assignment of new variables to be non-const");
1614 if (!parse_variable(parser, block, true))
1617 else if (parser->tok != ';')
1619 initexpr = parse_expression_leave(parser, false);
1624 /* move on to condition */
1625 if (parser->tok != ';') {
1626 parseerror(parser, "expected semicolon after for-loop initializer");
1629 if (!parser_next(parser)) {
1630 parseerror(parser, "expected for-loop condition");
1634 /* parse the condition */
1635 if (parser->tok != ';') {
1636 cond = parse_expression_leave(parser, false);
1641 /* move on to incrementor */
1642 if (parser->tok != ';') {
1643 parseerror(parser, "expected semicolon after for-loop initializer");
1646 if (!parser_next(parser)) {
1647 parseerror(parser, "expected for-loop condition");
1651 /* parse the incrementor */
1652 if (parser->tok != ')') {
1653 increment = parse_expression_leave(parser, false);
1656 if (!ast_istype(increment, ast_store) &&
1657 !ast_istype(increment, ast_call) &&
1658 !ast_istype(increment, ast_binstore))
1660 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1666 if (parser->tok != ')') {
1667 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1670 /* parse into the 'then' branch */
1671 if (!parser_next(parser)) {
1672 parseerror(parser, "expected for-loop body");
1675 ontrue = parse_statement_or_block(parser);
1680 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1681 *out = (ast_expression*)aloop;
1683 while (vec_size(parser->locals) > parser->blocklocal)
1684 retval = retval && parser_pop_local(parser);
1685 parser->blocklocal = oldblocklocal;
1688 if (initexpr) ast_delete(initexpr);
1689 if (cond) ast_delete(cond);
1690 if (increment) ast_delete(increment);
1691 while (vec_size(parser->locals) > parser->blocklocal)
1692 (void)!parser_pop_local(parser);
1693 parser->blocklocal = oldblocklocal;
1697 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
1699 ast_expression *exp = NULL;
1700 ast_return *ret = NULL;
1701 ast_value *expected = parser->function->vtype;
1703 if (!parser_next(parser)) {
1704 parseerror(parser, "expected return expression");
1708 if (parser->tok != ';') {
1709 exp = parse_expression(parser, false);
1713 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1714 parseerror(parser, "return with invalid expression");
1717 ret = ast_return_new(exp->expression.node.context, exp);
1723 if (!parser_next(parser))
1724 parseerror(parser, "parse error");
1725 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1726 if (opts_standard != COMPILER_GMQCC)
1727 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1729 parseerror(parser, "return without value");
1731 ret = ast_return_new(parser_ctx(parser), NULL);
1733 *out = (ast_expression*)ret;
1737 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
1739 lex_ctx ctx = parser_ctx(parser);
1741 if (!parser_next(parser) || parser->tok != ';') {
1742 parseerror(parser, "expected semicolon");
1746 if (!parser_next(parser))
1747 parseerror(parser, "parse error");
1749 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
1753 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
1755 ast_expression *operand;
1757 ast_switch *switchnode;
1758 ast_switch_case swcase;
1760 lex_ctx ctx = parser_ctx(parser);
1762 /* parse over the opening paren */
1763 if (!parser_next(parser) || parser->tok != '(') {
1764 parseerror(parser, "expected switch operand in parenthesis");
1768 /* parse into the expression */
1769 if (!parser_next(parser)) {
1770 parseerror(parser, "expected switch operand");
1773 /* parse the operand */
1774 operand = parse_expression_leave(parser, false);
1778 if (!OPTS_FLAG(RELAXED_SWITCH)) {
1779 opval = (ast_value*)operand;
1780 if (!ast_istype(operand, ast_value) || !opval->isconst) {
1781 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
1787 switchnode = ast_switch_new(ctx, operand);
1790 if (parser->tok != ')') {
1791 ast_delete(switchnode);
1792 parseerror(parser, "expected closing paren after 'switch' operand");
1796 /* parse over the opening paren */
1797 if (!parser_next(parser) || parser->tok != '{') {
1798 ast_delete(switchnode);
1799 parseerror(parser, "expected list of cases");
1803 if (!parser_next(parser)) {
1804 ast_delete(switchnode);
1805 parseerror(parser, "expected 'case' or 'default'");
1810 while (parser->tok != '}') {
1813 if (parser->tok != TOKEN_KEYWORD) {
1814 ast_delete(switchnode);
1815 parseerror(parser, "expected 'case' or 'default'");
1818 if (!strcmp(parser_tokval(parser), "case")) {
1819 if (!parser_next(parser)) {
1820 ast_delete(switchnode);
1821 parseerror(parser, "expected expression for case");
1824 swcase.value = parse_expression_leave(parser, false);
1825 if (!swcase.value) {
1826 ast_delete(switchnode);
1827 parseerror(parser, "expected expression for case");
1831 else if (!strcmp(parser_tokval(parser), "default")) {
1832 swcase.value = NULL;
1833 if (!parser_next(parser)) {
1834 ast_delete(switchnode);
1835 parseerror(parser, "expected colon");
1840 /* Now the colon and body */
1841 if (parser->tok != ':') {
1842 if (swcase.value) ast_unref(swcase.value);
1843 ast_delete(switchnode);
1844 parseerror(parser, "expected colon");
1848 if (!parser_next(parser)) {
1849 if (swcase.value) ast_unref(swcase.value);
1850 ast_delete(switchnode);
1851 parseerror(parser, "expected statements or case");
1854 block = ast_block_new(parser_ctx(parser));
1856 if (swcase.value) ast_unref(swcase.value);
1857 ast_delete(switchnode);
1860 swcase.code = (ast_expression*)block;
1861 vec_push(switchnode->cases, swcase);
1863 ast_expression *expr;
1864 if (parser->tok == '}')
1866 if (parser->tok == TOKEN_KEYWORD) {
1867 if (!strcmp(parser_tokval(parser), "case") ||
1868 !strcmp(parser_tokval(parser), "default"))
1873 if (!parse_statement(parser, block, &expr, true)) {
1874 ast_delete(switchnode);
1879 vec_push(block->exprs, expr);
1884 if (parser->tok != '}') {
1885 ast_delete(switchnode);
1886 parseerror(parser, "expected closing paren of case list");
1889 if (!parser_next(parser)) {
1890 ast_delete(switchnode);
1891 parseerror(parser, "parse error after switch");
1894 *out = (ast_expression*)switchnode;
1898 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
1900 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
1902 /* local variable */
1904 parseerror(parser, "cannot declare a variable from here");
1907 if (opts_standard == COMPILER_QCC) {
1908 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1911 if (!parse_variable(parser, block, false))
1916 else if (parser->tok == TOKEN_KEYWORD)
1918 if (!strcmp(parser_tokval(parser), "local"))
1921 parseerror(parser, "cannot declare a local variable here");
1924 if (!parser_next(parser)) {
1925 parseerror(parser, "expected variable declaration");
1928 if (!parse_variable(parser, block, true))
1933 else if (!strcmp(parser_tokval(parser), "return"))
1935 return parse_return(parser, block, out);
1937 else if (!strcmp(parser_tokval(parser), "if"))
1939 return parse_if(parser, block, out);
1941 else if (!strcmp(parser_tokval(parser), "while"))
1943 return parse_while(parser, block, out);
1945 else if (!strcmp(parser_tokval(parser), "do"))
1947 return parse_dowhile(parser, block, out);
1949 else if (!strcmp(parser_tokval(parser), "for"))
1951 if (opts_standard == COMPILER_QCC) {
1952 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1955 return parse_for(parser, block, out);
1957 else if (!strcmp(parser_tokval(parser), "break"))
1959 return parse_break_continue(parser, block, out, false);
1961 else if (!strcmp(parser_tokval(parser), "continue"))
1963 return parse_break_continue(parser, block, out, true);
1965 else if (!strcmp(parser_tokval(parser), "switch"))
1967 return parse_switch(parser, block, out);
1969 else if (!strcmp(parser_tokval(parser), "case") ||
1970 !strcmp(parser_tokval(parser), "default"))
1973 parseerror(parser, "unexpected 'case' label");
1978 parseerror(parser, "Unexpected keyword");
1981 else if (parser->tok == '{')
1984 inner = parse_block(parser, false);
1987 *out = (ast_expression*)inner;
1992 ast_expression *exp = parse_expression(parser, false);
1996 if (!ast_istype(exp, ast_store) &&
1997 !ast_istype(exp, ast_call) &&
1998 !ast_istype(exp, ast_binstore))
2000 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2007 static bool GMQCC_WARN parser_pop_local(parser_t *parser)
2012 ve = &vec_last(parser->locals);
2013 if (!parser->errors) {
2014 if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
2015 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
2020 vec_pop(parser->locals);
2024 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2026 size_t oldblocklocal;
2029 oldblocklocal = parser->blocklocal;
2030 parser->blocklocal = vec_size(parser->locals);
2032 if (!parser_next(parser)) { /* skip the '{' */
2033 parseerror(parser, "expected function body");
2037 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2039 ast_expression *expr;
2040 if (parser->tok == '}')
2043 if (!parse_statement(parser, block, &expr, false)) {
2044 /* parseerror(parser, "parse error"); */
2050 vec_push(block->exprs, expr);
2053 if (parser->tok != '}') {
2056 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2058 if (!vec_size(block->exprs) ||
2059 !ast_istype(vec_last(block->exprs), ast_return))
2061 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2067 (void)parser_next(parser);
2071 while (vec_size(parser->locals) > parser->blocklocal)
2072 retval = retval && parser_pop_local(parser);
2073 parser->blocklocal = oldblocklocal;
2077 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2080 block = ast_block_new(parser_ctx(parser));
2083 if (!parse_block_into(parser, block, warnreturn)) {
2084 ast_block_delete(block);
2090 static ast_expression* parse_statement_or_block(parser_t *parser)
2092 ast_expression *expr = NULL;
2093 if (parser->tok == '{')
2094 return (ast_expression*)parse_block(parser, false);
2095 if (!parse_statement(parser, NULL, &expr, false))
2101 static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
2104 size_t len = strlen(var->name);
2106 for (i = 0; i < 3; ++i) {
2107 ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
2111 ve[i].name = (char*)mem_a(len+3);
2113 ast_delete(ve[i].var);
2117 memcpy(ve[i].name, var->name, len);
2118 ve[i].name[len] = '_';
2119 ve[i].name[len+1] = 'x'+i;
2120 ve[i].name[len+2] = 0;
2129 ast_delete(ve[i].var);
2136 static bool parse_function_body(parser_t *parser, ast_value *var)
2138 ast_block *block = NULL;
2143 ast_expression *framenum = NULL;
2144 ast_expression *nextthink = NULL;
2145 /* None of the following have to be deleted */
2146 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2147 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2148 bool has_frame_think;
2152 has_frame_think = false;
2153 old = parser->function;
2155 if (var->expression.variadic) {
2156 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2157 "variadic function with implementation will not be able to access additional parameters"))
2163 if (parser->tok == '[') {
2164 /* got a frame definition: [ framenum, nextthink ]
2165 * this translates to:
2166 * self.frame = framenum;
2167 * self.nextthink = time + 0.1;
2168 * self.think = nextthink;
2172 fld_think = parser_find_field(parser, "think");
2173 fld_nextthink = parser_find_field(parser, "nextthink");
2174 fld_frame = parser_find_field(parser, "frame");
2175 if (!fld_think || !fld_nextthink || !fld_frame) {
2176 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2177 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2180 gbl_time = parser_find_global(parser, "time");
2181 gbl_self = parser_find_global(parser, "self");
2182 if (!gbl_time || !gbl_self) {
2183 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2184 parseerror(parser, "please declare the following globals: `time`, `self`");
2188 if (!parser_next(parser))
2191 framenum = parse_expression_leave(parser, true);
2193 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2196 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
2197 ast_unref(framenum);
2198 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2202 if (parser->tok != ',') {
2203 ast_unref(framenum);
2204 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2205 parseerror(parser, "Got a %i\n", parser->tok);
2209 if (!parser_next(parser)) {
2210 ast_unref(framenum);
2214 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2216 /* qc allows the use of not-yet-declared functions here
2217 * - this automatically creates a prototype */
2219 ast_value *thinkfunc;
2220 ast_expression *functype = fld_think->expression.next;
2222 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2223 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2224 ast_unref(framenum);
2225 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2229 if (!parser_next(parser)) {
2230 ast_unref(framenum);
2231 ast_delete(thinkfunc);
2235 varent.var = (ast_expression*)thinkfunc;
2236 varent.name = util_strdup(thinkfunc->name);
2237 vec_push(parser->globals, varent);
2238 nextthink = (ast_expression*)thinkfunc;
2241 nextthink = parse_expression_leave(parser, true);
2243 ast_unref(framenum);
2244 parseerror(parser, "expected a think-function in [frame,think] notation");
2249 if (!ast_istype(nextthink, ast_value)) {
2250 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2254 if (retval && parser->tok != ']') {
2255 parseerror(parser, "expected closing `]` for [frame,think] notation");
2259 if (retval && !parser_next(parser)) {
2263 if (retval && parser->tok != '{') {
2264 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2269 ast_unref(nextthink);
2270 ast_unref(framenum);
2274 has_frame_think = true;
2277 block = ast_block_new(parser_ctx(parser));
2279 parseerror(parser, "failed to allocate block");
2280 if (has_frame_think) {
2281 ast_unref(nextthink);
2282 ast_unref(framenum);
2287 if (has_frame_think) {
2289 ast_expression *self_frame;
2290 ast_expression *self_nextthink;
2291 ast_expression *self_think;
2292 ast_expression *time_plus_1;
2293 ast_store *store_frame;
2294 ast_store *store_nextthink;
2295 ast_store *store_think;
2297 ctx = parser_ctx(parser);
2298 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2299 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2300 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2302 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2303 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2305 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2306 if (self_frame) ast_delete(self_frame);
2307 if (self_nextthink) ast_delete(self_nextthink);
2308 if (self_think) ast_delete(self_think);
2309 if (time_plus_1) ast_delete(time_plus_1);
2315 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2316 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2317 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2320 ast_delete(self_frame);
2323 if (!store_nextthink) {
2324 ast_delete(self_nextthink);
2328 ast_delete(self_think);
2332 if (store_frame) ast_delete(store_frame);
2333 if (store_nextthink) ast_delete(store_nextthink);
2334 if (store_think) ast_delete(store_think);
2337 vec_push(block->exprs, (ast_expression*)store_frame);
2338 vec_push(block->exprs, (ast_expression*)store_nextthink);
2339 vec_push(block->exprs, (ast_expression*)store_think);
2343 parseerror(parser, "failed to generate code for [frame,think]");
2344 ast_unref(nextthink);
2345 ast_unref(framenum);
2351 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2354 ast_value *param = var->expression.params[parami];
2356 if (param->expression.vtype != TYPE_VECTOR &&
2357 (param->expression.vtype != TYPE_FIELD ||
2358 param->expression.next->expression.vtype != TYPE_VECTOR))
2363 if (!create_vector_members(parser, param, ve)) {
2364 ast_block_delete(block);
2368 for (e = 0; e < 3; ++e) {
2369 vec_push(parser->locals, ve[e]);
2370 ast_block_collect(block, ve[e].var);
2371 ve[e].var = NULL; /* collected */
2375 func = ast_function_new(ast_ctx(var), var->name, var);
2377 parseerror(parser, "failed to allocate function for `%s`", var->name);
2378 ast_block_delete(block);
2381 vec_push(parser->functions, func);
2383 parser->function = func;
2384 if (!parse_block_into(parser, block, true)) {
2385 ast_block_delete(block);
2389 vec_push(func->blocks, block);
2391 parser->function = old;
2392 while (vec_size(parser->locals))
2393 retval = retval && parser_pop_local(parser);
2395 if (parser->tok == ';')
2396 return parser_next(parser);
2397 else if (opts_standard == COMPILER_QCC)
2398 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2402 vec_pop(parser->functions);
2403 ast_function_delete(func);
2404 var->constval.vfunc = NULL;
2407 while (vec_size(parser->locals)) {
2408 mem_d(vec_last(parser->locals).name);
2409 vec_pop(parser->locals);
2411 parser->function = old;
2415 static ast_expression *array_accessor_split(
2420 ast_expression *left,
2421 ast_expression *right
2427 lex_ctx ctx = ast_ctx(array);
2429 if (!left || !right) {
2430 if (left) ast_delete(left);
2431 if (right) ast_delete(right);
2435 cmp = ast_binary_new(ctx, INSTR_LT,
2436 (ast_expression*)index,
2437 (ast_expression*)parser_const_float(parser, middle));
2441 parseerror(parser, "internal error: failed to create comparison for array setter");
2445 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2447 ast_delete(cmp); /* will delete left and right */
2448 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2452 return (ast_expression*)ifthen;
2455 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2457 lex_ctx ctx = ast_ctx(array);
2459 if (from+1 == afterend) {
2463 ast_array_index *subscript;
2464 int assignop = type_store_instr[value->expression.vtype];
2466 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2467 assignop = INSTR_STORE_V;
2469 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2473 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2475 ast_delete(subscript);
2479 block = ast_block_new(ctx);
2485 vec_push(block->exprs, (ast_expression*)st);
2487 ret = ast_return_new(ctx, NULL);
2493 vec_push(block->exprs, (ast_expression*)ret);
2495 return (ast_expression*)block;
2497 ast_expression *left, *right;
2498 size_t diff = afterend - from;
2499 size_t middle = from + diff/2;
2500 left = array_setter_node(parser, array, index, value, from, middle);
2501 right = array_setter_node(parser, array, index, value, middle, afterend);
2502 return array_accessor_split(parser, array, index, middle, left, right);
2506 static ast_expression *array_field_setter_node(
2515 lex_ctx ctx = ast_ctx(array);
2517 if (from+1 == afterend) {
2521 ast_entfield *entfield;
2522 ast_array_index *subscript;
2523 int assignop = type_storep_instr[value->expression.vtype];
2525 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2526 assignop = INSTR_STOREP_V;
2528 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2532 entfield = ast_entfield_new_force(ctx,
2533 (ast_expression*)entity,
2534 (ast_expression*)subscript,
2535 (ast_expression*)subscript);
2537 ast_delete(subscript);
2541 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2543 ast_delete(entfield);
2547 block = ast_block_new(ctx);
2553 vec_push(block->exprs, (ast_expression*)st);
2555 ret = ast_return_new(ctx, NULL);
2561 vec_push(block->exprs, (ast_expression*)ret);
2563 return (ast_expression*)block;
2565 ast_expression *left, *right;
2566 size_t diff = afterend - from;
2567 size_t middle = from + diff/2;
2568 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2569 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2570 return array_accessor_split(parser, array, index, middle, left, right);
2574 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2576 lex_ctx ctx = ast_ctx(array);
2578 if (from+1 == afterend) {
2580 ast_array_index *subscript;
2582 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2586 ret = ast_return_new(ctx, (ast_expression*)subscript);
2588 ast_delete(subscript);
2592 return (ast_expression*)ret;
2594 ast_expression *left, *right;
2595 size_t diff = afterend - from;
2596 size_t middle = from + diff/2;
2597 left = array_getter_node(parser, array, index, from, middle);
2598 right = array_getter_node(parser, array, index, middle, afterend);
2599 return array_accessor_split(parser, array, index, middle, left, right);
2603 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2605 ast_function *func = NULL;
2606 ast_value *fval = NULL;
2607 ast_block *body = NULL;
2609 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2611 parseerror(parser, "failed to create accessor function value");
2615 func = ast_function_new(ast_ctx(array), funcname, fval);
2618 parseerror(parser, "failed to create accessor function node");
2622 body = ast_block_new(ast_ctx(array));
2624 parseerror(parser, "failed to create block for array accessor");
2630 vec_push(func->blocks, body);
2633 vec_push(parser->accessors, fval);
2638 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2640 ast_expression *root = NULL;
2641 ast_value *index = NULL;
2642 ast_value *value = NULL;
2646 if (!ast_istype(array->expression.next, ast_value)) {
2647 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2651 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2653 func = fval->constval.vfunc;
2654 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2656 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2657 value = ast_value_copy((ast_value*)array->expression.next);
2659 if (!index || !value) {
2660 parseerror(parser, "failed to create locals for array accessor");
2663 (void)!ast_value_set_name(value, "value"); /* not important */
2664 vec_push(fval->expression.params, index);
2665 vec_push(fval->expression.params, value);
2667 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2669 parseerror(parser, "failed to build accessor search tree");
2673 vec_push(func->blocks[0]->exprs, root);
2674 array->setter = fval;
2677 if (index) ast_delete(index);
2678 if (value) ast_delete(value);
2679 if (root) ast_delete(root);
2685 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2687 ast_expression *root = NULL;
2688 ast_value *entity = NULL;
2689 ast_value *index = NULL;
2690 ast_value *value = NULL;
2694 if (!ast_istype(array->expression.next, ast_value)) {
2695 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2699 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2701 func = fval->constval.vfunc;
2702 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2704 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2705 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2706 value = ast_value_copy((ast_value*)array->expression.next);
2707 if (!entity || !index || !value) {
2708 parseerror(parser, "failed to create locals for array accessor");
2711 (void)!ast_value_set_name(value, "value"); /* not important */
2712 vec_push(fval->expression.params, entity);
2713 vec_push(fval->expression.params, index);
2714 vec_push(fval->expression.params, value);
2716 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2718 parseerror(parser, "failed to build accessor search tree");
2722 vec_push(func->blocks[0]->exprs, root);
2723 array->setter = fval;
2726 if (entity) ast_delete(entity);
2727 if (index) ast_delete(index);
2728 if (value) ast_delete(value);
2729 if (root) ast_delete(root);
2735 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2737 ast_expression *root = NULL;
2738 ast_value *index = NULL;
2742 /* NOTE: checking array->expression.next rather than elemtype since
2743 * for fields elemtype is a temporary fieldtype.
2745 if (!ast_istype(array->expression.next, ast_value)) {
2746 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2750 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2752 func = fval->constval.vfunc;
2753 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2755 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2758 parseerror(parser, "failed to create locals for array accessor");
2761 vec_push(fval->expression.params, index);
2763 root = array_getter_node(parser, array, index, 0, array->expression.count);
2765 parseerror(parser, "failed to build accessor search tree");
2769 vec_push(func->blocks[0]->exprs, root);
2770 array->getter = fval;
2773 if (index) ast_delete(index);
2774 if (root) ast_delete(root);
2780 static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
2781 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
2789 bool variadic = false;
2791 ctx = parser_ctx(parser);
2793 /* for the sake of less code we parse-in in this function */
2794 if (!parser_next(parser)) {
2795 parseerror(parser, "expected parameter list");
2801 /* parse variables until we hit a closing paren */
2802 while (parser->tok != ')') {
2804 /* there must be commas between them */
2805 if (parser->tok != ',') {
2806 parseerror(parser, "expected comma or end of parameter list");
2809 if (!parser_next(parser)) {
2810 parseerror(parser, "expected parameter");
2816 if (parser->tok == TOKEN_DOTS) {
2817 /* '...' indicates a varargs function */
2819 if (!parser_next(parser)) {
2820 parseerror(parser, "expected parameter");
2823 if (parser->tok != ')') {
2824 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
2830 /* for anything else just parse a typename */
2831 param = parse_typename(parser, NULL);
2834 vec_push(params, param);
2835 if (param->expression.vtype >= TYPE_VARIANT) {
2836 char typename[1024];
2837 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
2838 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
2845 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
2846 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
2849 if (!parser_next(parser)) {
2850 parseerror(parser, "parse error after typename");
2854 /* now turn 'var' into a function type */
2855 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
2856 fval->expression.next = (ast_expression*)var;
2857 fval->expression.variadic = variadic;
2860 var->expression.params = params;
2867 for (i = 0; i < vec_size(params); ++i)
2868 ast_delete(params[i]);
2873 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
2875 ast_expression *cexp;
2876 ast_value *cval, *tmp;
2879 ctx = parser_ctx(parser);
2881 if (!parser_next(parser)) {
2883 parseerror(parser, "expected array-size");
2887 cexp = parse_expression_leave(parser, true);
2889 if (!cexp || !ast_istype(cexp, ast_value)) {
2893 parseerror(parser, "expected array-size as constant positive integer");
2896 cval = (ast_value*)cexp;
2898 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
2899 tmp->expression.next = (ast_expression*)var;
2902 if (cval->expression.vtype == TYPE_INTEGER)
2903 tmp->expression.count = cval->constval.vint;
2904 else if (cval->expression.vtype == TYPE_FLOAT)
2905 tmp->expression.count = cval->constval.vfloat;
2909 parseerror(parser, "array-size must be a positive integer constant");
2914 if (parser->tok != ']') {
2916 parseerror(parser, "expected ']' after array-size");
2919 if (!parser_next(parser)) {
2921 parseerror(parser, "error after parsing array size");
2927 /* Parse a complete typename.
2928 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
2929 * but when parsing variables separated by comma
2930 * 'storebase' should point to where the base-type should be kept.
2931 * The base type makes up every bit of type information which comes *before* the
2934 * The following will be parsed in its entirety:
2936 * The 'basetype' in this case is 'void()'
2937 * and if there's a comma after it, say:
2939 * then the type-information 'void()' can be stored in 'storebase'
2941 static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
2943 ast_value *var, *tmp;
2946 const char *name = NULL;
2947 bool isfield = false;
2948 bool wasarray = false;
2950 ctx = parser_ctx(parser);
2952 /* types may start with a dot */
2953 if (parser->tok == '.') {
2955 /* if we parsed a dot we need a typename now */
2956 if (!parser_next(parser)) {
2957 parseerror(parser, "expected typename for field definition");
2960 if (parser->tok != TOKEN_TYPENAME) {
2961 parseerror(parser, "expected typename");
2966 /* generate the basic type value */
2967 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
2968 /* do not yet turn into a field - remember:
2969 * .void() foo; is a field too
2970 * .void()() foo; is a function
2974 if (!parser_next(parser)) {
2976 parseerror(parser, "parse error after typename");
2980 /* an opening paren now starts the parameter-list of a function
2981 * this is where original-QC has parameter lists.
2982 * We allow a single parameter list here.
2983 * Much like fteqcc we don't allow `float()() x`
2985 if (parser->tok == '(') {
2986 var = parse_parameter_list(parser, var);
2991 /* store the base if requested */
2993 *storebase = ast_value_copy(var);
2995 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2996 tmp->expression.next = (ast_expression*)*storebase;
3001 /* there may be a name now */
3002 if (parser->tok == TOKEN_IDENT) {
3003 name = util_strdup(parser_tokval(parser));
3005 if (!parser_next(parser)) {
3007 parseerror(parser, "error after variable or field declaration");
3012 /* now this may be an array */
3013 if (parser->tok == '[') {
3015 var = parse_arraysize(parser, var);
3020 /* This is the point where we can turn it into a field */
3022 /* turn it into a field if desired */
3023 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3024 tmp->expression.next = (ast_expression*)var;
3028 /* now there may be function parens again */
3029 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3030 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3031 if (parser->tok == '(' && wasarray)
3032 parseerror(parser, "arrays as part of a return type is not supported");
3033 while (parser->tok == '(') {
3034 var = parse_parameter_list(parser, var);
3043 /* finally name it */
3045 if (!ast_value_set_name(var, name)) {
3047 parseerror(parser, "internal error: failed to set name");
3050 /* free the name, ast_value_set_name duplicates */
3057 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
3061 ast_expression *old;
3065 ast_value *basetype = NULL;
3067 bool isparam = false;
3068 bool isvector = false;
3069 bool cleanvar = true;
3070 bool wasarray = false;
3072 varentry_t varent, ve[3];
3074 /* get the first complete variable */
3075 var = parse_typename(parser, &basetype);
3078 ast_delete(basetype);
3082 memset(&varent, 0, sizeof(varent));
3083 memset(&ve, 0, sizeof(ve));
3089 /* Part 0: finish the type */
3090 if (parser->tok == '(') {
3091 if (opts_standard == COMPILER_QCC)
3092 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3093 var = parse_parameter_list(parser, var);
3099 /* we only allow 1-dimensional arrays */
3100 if (parser->tok == '[') {
3102 var = parse_arraysize(parser, var);
3108 if (parser->tok == '(' && wasarray) {
3109 parseerror(parser, "arrays as part of a return type is not supported");
3110 /* we'll still parse the type completely for now */
3112 /* for functions returning functions */
3113 while (parser->tok == '(') {
3114 if (opts_standard == COMPILER_QCC)
3115 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3116 var = parse_parameter_list(parser, var);
3124 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3125 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3126 * is then filled with the previous definition and the parameter-names replaced.
3129 /* Deal with end_sys_ vars */
3131 if (!strcmp(var->name, "end_sys_globals")) {
3132 parser->crc_globals = vec_size(parser->globals);
3135 else if (!strcmp(var->name, "end_sys_fields")) {
3136 parser->crc_fields = vec_size(parser->fields);
3139 if (was_end && var->expression.vtype == TYPE_FIELD) {
3140 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3141 "global '%s' hint should not be a field",
3142 parser_tokval(parser)))
3149 if (!nofields && var->expression.vtype == TYPE_FIELD)
3151 /* deal with field declarations */
3152 old = parser_find_field(parser, var->name);
3154 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3155 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3164 parseerror(parser, "field `%s` already declared here: %s:%i",
3165 var->name, ast_ctx(old).file, ast_ctx(old).line);
3170 if (opts_standard == COMPILER_QCC &&
3171 (old = parser_find_global(parser, var->name)))
3173 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3174 parseerror(parser, "field `%s` already declared here: %s:%i",
3175 var->name, ast_ctx(old).file, ast_ctx(old).line);
3182 /* deal with other globals */
3183 old = parser_find_global(parser, var->name);
3184 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3186 /* This is a function which had a prototype */
3187 if (!ast_istype(old, ast_value)) {
3188 parseerror(parser, "internal error: prototype is not an ast_value");
3192 proto = (ast_value*)old;
3193 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3194 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3196 ast_ctx(proto).file, ast_ctx(proto).line);
3200 /* we need the new parameter-names */
3201 for (i = 0; i < vec_size(proto->expression.params); ++i)
3202 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3210 parseerror(parser, "global `%s` already declared here: %s:%i",
3211 var->name, ast_ctx(old).file, ast_ctx(old).line);
3215 if (opts_standard == COMPILER_QCC &&
3216 (old = parser_find_field(parser, var->name)))
3218 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3219 parseerror(parser, "global `%s` already declared here: %s:%i",
3220 var->name, ast_ctx(old).file, ast_ctx(old).line);
3227 else /* it's not a global */
3229 old = parser_find_local(parser, var->name, parser->blocklocal, &isparam);
3230 if (old && !isparam) {
3231 parseerror(parser, "local `%s` already declared here: %s:%i",
3232 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3236 old = parser_find_local(parser, var->name, 0, &isparam);
3237 if (old && isparam) {
3238 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3239 "local `%s` is shadowing a parameter", var->name))
3241 parseerror(parser, "local `%s` already declared here: %s:%i",
3242 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3246 if (opts_standard != COMPILER_GMQCC) {
3255 * Create the global/local, and deal with vector types.
3258 if (var->expression.vtype == TYPE_VECTOR)
3260 else if (var->expression.vtype == TYPE_FIELD &&
3261 var->expression.next->expression.vtype == TYPE_VECTOR)
3265 if (!create_vector_members(parser, var, ve)) {
3271 varent.name = util_strdup(var->name);
3272 varent.var = (ast_expression*)var;
3275 /* deal with global variables, fields, functions */
3276 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3277 vec_push(parser->fields, varent);
3279 for (i = 0; i < 3; ++i)
3280 vec_push(parser->fields, ve[i]);
3284 vec_push(parser->globals, varent);
3286 for (i = 0; i < 3; ++i)
3287 vec_push(parser->globals, ve[i]);
3291 vec_push(parser->locals, varent);
3292 vec_push(localblock->locals, var);
3294 for (i = 0; i < 3; ++i) {
3295 vec_push(parser->locals, ve[i]);
3296 ast_block_collect(localblock, ve[i].var);
3297 ve[i].var = NULL; /* from here it's being collected in the block */
3303 ve[0].name = ve[1].name = ve[2].name = NULL;
3304 ve[0].var = ve[1].var = ve[2].var = NULL;
3310 if (var->expression.vtype == TYPE_ARRAY) {
3312 snprintf(name, sizeof(name), "%s##SET", var->name);
3313 if (!parser_create_array_setter(parser, var, name))
3315 snprintf(name, sizeof(name), "%s##GET", var->name);
3316 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3319 else if (!localblock && !nofields &&
3320 var->expression.vtype == TYPE_FIELD &&
3321 var->expression.next->expression.vtype == TYPE_ARRAY)
3324 ast_expression *telem;
3326 ast_value *array = (ast_value*)var->expression.next;
3328 if (!ast_istype(var->expression.next, ast_value)) {
3329 parseerror(parser, "internal error: field element type must be an ast_value");
3333 snprintf(name, sizeof(name), "%s##SETF", var->name);
3334 if (!parser_create_array_field_setter(parser, array, name))
3337 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3338 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3339 tfield->expression.next = telem;
3340 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3341 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3349 if (parser->tok == ';') {
3350 ast_delete(basetype);
3351 if (!parser_next(parser)) {
3352 parseerror(parser, "error after variable declaration");
3358 if (parser->tok == ',')
3361 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3362 parseerror(parser, "missing comma or semicolon while parsing variables");
3366 if (localblock && opts_standard == COMPILER_QCC) {
3367 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3368 "initializing expression turns variable `%s` into a constant in this standard",
3375 if (parser->tok != '{') {
3376 if (parser->tok != '=') {
3377 parseerror(parser, "missing semicolon or initializer");
3381 if (!parser_next(parser)) {
3382 parseerror(parser, "error parsing initializer");
3386 else if (opts_standard == COMPILER_QCC) {
3387 parseerror(parser, "expected '=' before function body in this standard");
3390 if (parser->tok == '#') {
3394 parseerror(parser, "cannot declare builtins within functions");
3397 if (var->expression.vtype != TYPE_FUNCTION) {
3398 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3401 if (!parser_next(parser)) {
3402 parseerror(parser, "expected builtin number");
3405 if (parser->tok != TOKEN_INTCONST) {
3406 parseerror(parser, "builtin number must be an integer constant");
3409 if (parser_token(parser)->constval.i <= 0) {
3410 parseerror(parser, "builtin number must be an integer greater than zero");
3414 func = ast_function_new(ast_ctx(var), var->name, var);
3416 parseerror(parser, "failed to allocate function for `%s`", var->name);
3419 vec_push(parser->functions, func);
3421 func->builtin = -parser_token(parser)->constval.i;
3423 if (!parser_next(parser)) {
3424 parseerror(parser, "expected comma or semicolon");
3425 ast_function_delete(func);
3426 var->constval.vfunc = NULL;
3430 else if (parser->tok == '{' || parser->tok == '[')
3433 parseerror(parser, "cannot declare functions within functions");
3437 if (!parse_function_body(parser, var))
3439 ast_delete(basetype);
3442 ast_expression *cexp;
3445 cexp = parse_expression_leave(parser, true);
3450 cval = (ast_value*)cexp;
3451 if (!ast_istype(cval, ast_value) || !cval->isconst)
3452 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3455 var->isconst = true;
3456 if (cval->expression.vtype == TYPE_STRING)
3457 var->constval.vstring = parser_strdup(cval->constval.vstring);
3459 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3463 shunt sy = { NULL, NULL };
3464 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
3465 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
3466 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
3467 if (!parser_sy_pop(parser, &sy))
3470 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
3471 parseerror(parser, "internal error: leaked operands");
3472 vec_push(localblock->exprs, (ast_expression*)sy.out[0].out);
3480 if (parser->tok == ',') {
3481 if (!parser_next(parser)) {
3482 parseerror(parser, "expected another variable");
3486 if (parser->tok != TOKEN_IDENT) {
3487 parseerror(parser, "expected another variable");
3490 var = ast_value_copy(basetype);
3492 ast_value_set_name(var, parser_tokval(parser));
3493 if (!parser_next(parser)) {
3494 parseerror(parser, "error parsing variable declaration");
3500 if (parser->tok != ';') {
3501 parseerror(parser, "missing semicolon after variables");
3505 if (!parser_next(parser)) {
3506 parseerror(parser, "parse error after variable declaration");
3510 ast_delete(basetype);
3514 if (cleanvar && var)
3516 ast_delete(basetype);
3520 ast_delete(basetype);
3521 if (cleanvar && var)
3523 if (varent.name) mem_d(varent.name);
3524 if (ve[0].name) mem_d(ve[0].name);
3525 if (ve[1].name) mem_d(ve[1].name);
3526 if (ve[2].name) mem_d(ve[2].name);
3527 if (ve[0].var) mem_d(ve[0].var);
3528 if (ve[1].var) mem_d(ve[1].var);
3529 if (ve[2].var) mem_d(ve[2].var);
3533 static bool parser_global_statement(parser_t *parser)
3535 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3537 return parse_variable(parser, NULL, false);
3539 else if (parser->tok == TOKEN_KEYWORD)
3541 /* handle 'var' and 'const' */
3542 if (!strcmp(parser_tokval(parser), "var")) {
3543 if (!parser_next(parser)) {
3544 parseerror(parser, "expected variable declaration after 'var'");
3547 return parse_variable(parser, NULL, true);
3551 else if (parser->tok == '$')
3553 if (!parser_next(parser)) {
3554 parseerror(parser, "parse error");
3560 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3566 static parser_t *parser;
3571 parser = (parser_t*)mem_a(sizeof(parser_t));
3575 memset(parser, 0, sizeof(*parser));
3577 for (i = 0; i < operator_count; ++i) {
3578 if (operators[i].id == opid1('=')) {
3579 parser->assign_op = operators+i;
3583 if (!parser->assign_op) {
3584 printf("internal error: initializing parser: failed to find assign operator\n");
3591 bool parser_compile()
3593 /* initial lexer/parser state */
3594 parser->lex->flags.noops = true;
3596 if (parser_next(parser))
3598 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3600 if (!parser_global_statement(parser)) {
3601 if (parser->tok == TOKEN_EOF)
3602 parseerror(parser, "unexpected eof");
3603 else if (!parser->errors)
3604 parseerror(parser, "there have been errors, bailing out");
3605 lex_close(parser->lex);
3611 parseerror(parser, "parse error");
3612 lex_close(parser->lex);
3617 lex_close(parser->lex);
3620 return !parser->errors;
3623 bool parser_compile_file(const char *filename)
3625 parser->lex = lex_open(filename);
3627 con_err("failed to open file \"%s\"\n", filename);
3630 return parser_compile();
3633 bool parser_compile_string_len(const char *name, const char *str, size_t len)
3635 parser->lex = lex_open_string(str, len, name);
3637 con_err("failed to create lexer for string \"%s\"\n", name);
3640 return parser_compile();
3643 bool parser_compile_string(const char *name, const char *str)
3645 parser->lex = lex_open_string(str, strlen(str), name);
3647 con_err("failed to create lexer for string \"%s\"\n", name);
3650 return parser_compile();
3653 void parser_cleanup()
3656 for (i = 0; i < vec_size(parser->accessors); ++i) {
3657 ast_delete(parser->accessors[i]->constval.vfunc);
3658 parser->accessors[i]->constval.vfunc = NULL;
3659 ast_delete(parser->accessors[i]);
3661 for (i = 0; i < vec_size(parser->functions); ++i) {
3662 ast_delete(parser->functions[i]);
3664 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3665 ast_delete(parser->imm_vector[i]);
3667 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3668 ast_delete(parser->imm_string[i]);
3670 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3671 ast_delete(parser->imm_float[i]);
3673 for (i = 0; i < vec_size(parser->fields); ++i) {
3674 ast_delete(parser->fields[i].var);
3675 mem_d(parser->fields[i].name);
3677 for (i = 0; i < vec_size(parser->globals); ++i) {
3678 ast_delete(parser->globals[i].var);
3679 mem_d(parser->globals[i].name);
3681 vec_free(parser->accessors);
3682 vec_free(parser->functions);
3683 vec_free(parser->imm_vector);
3684 vec_free(parser->imm_string);
3685 vec_free(parser->imm_float);
3686 vec_free(parser->globals);
3687 vec_free(parser->fields);
3688 vec_free(parser->locals);
3693 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3695 return util_crc16(old, str, strlen(str));
3698 static void progdefs_crc_file(const char *str)
3700 /* write to progdefs.h here */
3703 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3705 old = progdefs_crc_sum(old, str);
3706 progdefs_crc_file(str);
3710 static void generate_checksum(parser_t *parser)
3712 uint16_t crc = 0xFFFF;
3715 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3716 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3718 progdefs_crc_file("\tint\tpad;\n");
3719 progdefs_crc_file("\tint\tofs_return[3];\n");
3720 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3721 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3722 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3723 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3724 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3725 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3726 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3727 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3729 for (i = 0; i < parser->crc_globals; ++i) {
3730 if (!ast_istype(parser->globals[i].var, ast_value))
3732 switch (parser->globals[i].var->expression.vtype) {
3733 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3734 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3735 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3736 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3738 crc = progdefs_crc_both(crc, "\tint\t");
3741 crc = progdefs_crc_both(crc, parser->globals[i].name);
3742 crc = progdefs_crc_both(crc, ";\n");
3744 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3745 for (i = 0; i < parser->crc_fields; ++i) {
3746 if (!ast_istype(parser->fields[i].var, ast_value))
3748 switch (parser->fields[i].var->expression.next->expression.vtype) {
3749 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3750 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3751 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3752 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3754 crc = progdefs_crc_both(crc, "\tint\t");
3757 crc = progdefs_crc_both(crc, parser->fields[i].name);
3758 crc = progdefs_crc_both(crc, ";\n");
3760 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3765 bool parser_finish(const char *output)
3771 if (!parser->errors)
3773 ir = ir_builder_new("gmqcc_out");
3775 con_out("failed to allocate builder\n");
3779 for (i = 0; i < vec_size(parser->fields); ++i) {
3782 if (!ast_istype(parser->fields[i].var, ast_value))
3784 field = (ast_value*)parser->fields[i].var;
3785 isconst = field->isconst;
3786 field->isconst = false;
3787 if (!ast_global_codegen((ast_value*)field, ir, true)) {
3788 con_out("failed to generate field %s\n", field->name);
3789 ir_builder_delete(ir);
3794 ast_expression *subtype;
3795 field->isconst = true;
3796 subtype = field->expression.next;
3797 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
3798 if (subtype->expression.vtype == TYPE_FIELD)
3799 ifld->fieldtype = subtype->expression.next->expression.vtype;
3800 else if (subtype->expression.vtype == TYPE_FUNCTION)
3801 ifld->outtype = subtype->expression.next->expression.vtype;
3802 (void)!ir_value_set_field(field->ir_v, ifld);
3805 for (i = 0; i < vec_size(parser->globals); ++i) {
3807 if (!ast_istype(parser->globals[i].var, ast_value))
3809 asvalue = (ast_value*)(parser->globals[i].var);
3810 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
3811 if (strcmp(asvalue->name, "end_sys_globals") &&
3812 strcmp(asvalue->name, "end_sys_fields"))
3814 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
3815 "unused global: `%s`", asvalue->name);
3818 if (!ast_global_codegen(asvalue, ir, false)) {
3819 con_out("failed to generate global %s\n", parser->globals[i].name);
3820 ir_builder_delete(ir);
3824 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3825 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
3826 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
3827 ir_builder_delete(ir);
3831 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3832 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
3833 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
3834 ir_builder_delete(ir);
3838 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3839 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
3840 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
3841 ir_builder_delete(ir);
3845 for (i = 0; i < vec_size(parser->globals); ++i) {
3847 if (!ast_istype(parser->globals[i].var, ast_value))
3849 asvalue = (ast_value*)(parser->globals[i].var);
3850 if (asvalue->setter) {
3851 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3852 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3853 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3855 printf("failed to generate setter for %s\n", parser->globals[i].name);
3856 ir_builder_delete(ir);
3860 if (asvalue->getter) {
3861 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3862 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3863 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3865 printf("failed to generate getter for %s\n", parser->globals[i].name);
3866 ir_builder_delete(ir);
3871 for (i = 0; i < vec_size(parser->fields); ++i) {
3873 asvalue = (ast_value*)(parser->fields[i].var->expression.next);
3875 if (!ast_istype((ast_expression*)asvalue, ast_value))
3877 if (asvalue->expression.vtype != TYPE_ARRAY)
3879 if (asvalue->setter) {
3880 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3881 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3882 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3884 printf("failed to generate setter for %s\n", parser->fields[i].name);
3885 ir_builder_delete(ir);
3889 if (asvalue->getter) {
3890 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3891 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3892 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3894 printf("failed to generate getter for %s\n", parser->fields[i].name);
3895 ir_builder_delete(ir);
3900 for (i = 0; i < vec_size(parser->functions); ++i) {
3901 if (!ast_function_codegen(parser->functions[i], ir)) {
3902 con_out("failed to generate function %s\n", parser->functions[i]->name);
3903 ir_builder_delete(ir);
3906 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
3907 con_out("failed to finalize function %s\n", parser->functions[i]->name);
3908 ir_builder_delete(ir);
3915 ir_builder_dump(ir, con_out);
3917 generate_checksum(parser);
3919 if (!ir_builder_generate(ir, output)) {
3920 con_out("*** failed to generate output file\n");
3921 ir_builder_delete(ir);
3926 ir_builder_delete(ir);
3930 con_out("*** there were compile errors\n");