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 (vec_last(sy->ops).paren) {
427 parseerror(parser, "unmatched parenthesis");
431 op = &operators[vec_last(sy->ops).etype - 1];
432 ctx = vec_last(sy->ops).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 CanConstFold3(A, B, C) \
464 (CanConstFold1(A) && CanConstFold1(B) && CanConstFold1(C))
465 #define ConstV(i) (asvalue[(i)]->constval.vvec)
466 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
467 #define ConstS(i) (asvalue[(i)]->constval.vstring)
471 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
475 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
476 if (exprs[1]->expression.vtype != TYPE_FIELD) {
477 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
480 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
482 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
483 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
487 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
493 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
494 !(exprs[0]->expression.vtype == TYPE_FIELD &&
495 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
497 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
498 parseerror(parser, "cannot index value of type %s", ty1);
501 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
502 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
503 parseerror(parser, "index must be of type float, not %s", ty1);
506 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
507 if (rotate_entfield_array_index_nodes(&out))
509 if (opts_standard != COMPILER_GMQCC) {
510 /* this error doesn't need to make us bail out */
511 (void)!parsewarning(parser, WARN_EXTENSIONS,
512 "accessing array-field members of an entity without parenthesis\n"
513 " -> this is an extension from -std=gmqcc");
520 vec_push(blocks[0]->exprs, exprs[1]);
522 blocks[0] = ast_block_new(ctx);
523 vec_push(blocks[0]->exprs, exprs[0]);
524 vec_push(blocks[0]->exprs, exprs[1]);
526 if (!ast_block_set_type(blocks[0], exprs[1]))
529 vec_push(sy->out, syblock(ctx, blocks[0]));
533 switch (exprs[0]->expression.vtype) {
535 if (CanConstFold1(exprs[0]))
536 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
538 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
539 (ast_expression*)parser_const_float_0(parser),
543 if (CanConstFold1(exprs[0]))
544 out = (ast_expression*)parser_const_vector_f(parser,
545 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
547 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
548 (ast_expression*)parser_const_vector_0(parser),
552 parseerror(parser, "invalid types used in expression: cannot negate type %s",
553 type_name[exprs[0]->expression.vtype]);
559 switch (exprs[0]->expression.vtype) {
561 if (CanConstFold1(exprs[0]))
562 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
564 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
567 if (CanConstFold1(exprs[0]))
568 out = (ast_expression*)parser_const_float(parser,
569 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
571 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
574 if (CanConstFold1(exprs[0]))
575 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
577 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
579 /* we don't constant-fold NOT for these types */
581 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
584 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
587 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
588 type_name[exprs[0]->expression.vtype]);
594 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
595 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
597 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
598 type_name[exprs[0]->expression.vtype],
599 type_name[exprs[1]->expression.vtype]);
602 switch (exprs[0]->expression.vtype) {
604 if (CanConstFold(exprs[0], exprs[1]))
606 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
609 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
612 if (CanConstFold(exprs[0], exprs[1]))
613 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
615 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
618 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
619 type_name[exprs[0]->expression.vtype],
620 type_name[exprs[1]->expression.vtype]);
625 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
626 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
628 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
629 type_name[exprs[1]->expression.vtype],
630 type_name[exprs[0]->expression.vtype]);
633 switch (exprs[0]->expression.vtype) {
635 if (CanConstFold(exprs[0], exprs[1]))
636 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
638 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
641 if (CanConstFold(exprs[0], exprs[1]))
642 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
644 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
647 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
648 type_name[exprs[1]->expression.vtype],
649 type_name[exprs[0]->expression.vtype]);
654 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
655 exprs[0]->expression.vtype != TYPE_VECTOR &&
656 exprs[0]->expression.vtype != TYPE_FLOAT &&
657 exprs[1]->expression.vtype != TYPE_VECTOR &&
658 exprs[1]->expression.vtype != TYPE_FLOAT)
660 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
661 type_name[exprs[1]->expression.vtype],
662 type_name[exprs[0]->expression.vtype]);
665 switch (exprs[0]->expression.vtype) {
667 if (exprs[1]->expression.vtype == TYPE_VECTOR)
669 if (CanConstFold(exprs[0], exprs[1]))
670 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
672 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
676 if (CanConstFold(exprs[0], exprs[1]))
677 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
679 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
683 if (exprs[1]->expression.vtype == TYPE_FLOAT)
685 if (CanConstFold(exprs[0], exprs[1]))
686 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
688 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
692 if (CanConstFold(exprs[0], exprs[1]))
693 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
695 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
699 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
700 type_name[exprs[1]->expression.vtype],
701 type_name[exprs[0]->expression.vtype]);
706 if (NotSameType(TYPE_FLOAT)) {
707 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
708 type_name[exprs[0]->expression.vtype],
709 type_name[exprs[1]->expression.vtype]);
712 if (CanConstFold(exprs[0], exprs[1]))
713 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
715 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
719 parseerror(parser, "qc does not have a modulo operator");
723 if (NotSameType(TYPE_FLOAT)) {
724 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
725 type_name[exprs[0]->expression.vtype],
726 type_name[exprs[1]->expression.vtype]);
729 if (CanConstFold(exprs[0], exprs[1]))
730 out = (ast_expression*)parser_const_float(parser,
731 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
732 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
734 out = (ast_expression*)ast_binary_new(ctx,
735 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
739 parseerror(parser, "TODO: bitxor");
744 case opid3('<','<','='):
745 case opid3('>','>','='):
746 parseerror(parser, "TODO: shifts");
750 generated_op += 1; /* INSTR_OR */
752 generated_op += INSTR_AND;
753 if (NotSameType(TYPE_FLOAT)) {
754 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
755 type_name[exprs[0]->expression.vtype],
756 type_name[exprs[1]->expression.vtype]);
757 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
758 parseerror(parser, "TODO: optional early out");
761 if (opts_standard == COMPILER_GMQCC)
762 con_out("TODO: early out logic\n");
763 if (CanConstFold(exprs[0], exprs[1]))
764 out = (ast_expression*)parser_const_float(parser,
765 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
767 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
771 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
772 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
773 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
774 parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
777 if (CanConstFold1(exprs[0]))
778 out = (ConstF(0) ? exprs[1] : exprs[2]);
780 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
784 generated_op += 1; /* INSTR_GT */
786 generated_op += 1; /* INSTR_LT */
787 case opid2('>', '='):
788 generated_op += 1; /* INSTR_GE */
789 case opid2('<', '='):
790 generated_op += INSTR_LE;
791 if (NotSameType(TYPE_FLOAT)) {
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, generated_op, exprs[0], exprs[1]);
799 case opid2('!', '='):
800 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
801 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
802 type_name[exprs[0]->expression.vtype],
803 type_name[exprs[1]->expression.vtype]);
806 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
808 case opid2('=', '='):
809 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
810 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
811 type_name[exprs[0]->expression.vtype],
812 type_name[exprs[1]->expression.vtype]);
815 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
819 if (ast_istype(exprs[0], ast_entfield)) {
820 ast_expression *field = ((ast_entfield*)exprs[0])->field;
821 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
822 exprs[0]->expression.vtype == TYPE_FIELD &&
823 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
825 assignop = type_storep_instr[TYPE_VECTOR];
828 assignop = type_storep_instr[exprs[0]->expression.vtype];
829 if (!ast_compare_type(field->expression.next, exprs[1])) {
830 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
831 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
832 if (opts_standard == COMPILER_QCC &&
833 field->expression.next->expression.vtype == TYPE_FUNCTION &&
834 exprs[1]->expression.vtype == TYPE_FUNCTION)
836 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
837 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
843 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
848 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
849 exprs[0]->expression.vtype == TYPE_FIELD &&
850 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
852 assignop = type_store_instr[TYPE_VECTOR];
855 assignop = type_store_instr[exprs[0]->expression.vtype];
858 if (assignop == AINSTR_END) {
859 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
860 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
861 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
863 else if (!ast_compare_type(exprs[0], exprs[1])) {
864 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
865 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
866 if (opts_standard == COMPILER_QCC &&
867 exprs[0]->expression.vtype == TYPE_FUNCTION &&
868 exprs[1]->expression.vtype == TYPE_FUNCTION)
870 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
871 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
877 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
880 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
884 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
885 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
887 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
888 type_name[exprs[0]->expression.vtype],
889 type_name[exprs[1]->expression.vtype]);
892 if (ast_istype(exprs[0], ast_entfield))
893 assignop = type_storep_instr[exprs[0]->expression.vtype];
895 assignop = type_store_instr[exprs[0]->expression.vtype];
896 switch (exprs[0]->expression.vtype) {
898 out = (ast_expression*)ast_binstore_new(ctx, assignop,
899 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
903 out = (ast_expression*)ast_binstore_new(ctx, assignop,
904 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
908 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
909 type_name[exprs[0]->expression.vtype],
910 type_name[exprs[1]->expression.vtype]);
918 parseerror(parser, "failed to apply operand %s", op->op);
922 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
923 vec_push(sy->out, syexp(ctx, out));
927 static bool parser_close_call(parser_t *parser, shunt *sy)
929 /* was a function call */
936 vec_shrinkby(sy->ops, 1);
937 fid = sy->ops[vec_size(sy->ops)].off;
939 /* out[fid] is the function
940 * everything above is parameters...
942 * 1 params = ast_expression
946 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
947 parseerror(parser, "internal error: function call needs function and parameter list...");
951 fun = sy->out[fid].out;
953 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
955 parseerror(parser, "out of memory");
959 if (fid+1 == vec_size(sy->out)) {
962 } else if (fid+2 == vec_size(sy->out)) {
964 vec_shrinkby(sy->out, 1);
965 params = sy->out[vec_size(sy->out)].block;
969 vec_push(call->params, sy->out[vec_size(sy->out)].out);
971 paramcount = vec_size(params->exprs);
972 call->params = params->exprs;
973 params->exprs = NULL;
976 if (!ast_call_check_types(call))
979 parseerror(parser, "invalid function call");
983 /* overwrite fid, the function, with a call */
984 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
986 if (fun->expression.vtype != TYPE_FUNCTION) {
987 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
991 if (!fun->expression.next) {
992 parseerror(parser, "could not determine function return type");
995 if (vec_size(fun->expression.params) != paramcount &&
996 !(fun->expression.variadic &&
997 vec_size(fun->expression.params) < paramcount))
1000 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1002 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1003 if (opts_standard == COMPILER_GMQCC)
1006 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1007 " -> `%s` has been declared here: %s:%i",
1008 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1009 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1011 parseerror(parser, "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);
1020 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1021 "too %s parameters for call to %s: expected %i, got %i\n"
1022 " -> `%s` has been declared here: %s:%i",
1023 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1024 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1026 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1027 "too %s parameters for function call: expected %i, got %i\n"
1028 " -> `%s` has been declared here: %s:%i",
1029 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1030 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1038 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1040 if (!vec_size(sy->ops)) {
1041 parseerror(parser, "unmatched closing paren");
1044 /* this would for bit a + (x) because there are no operators inside (x)
1045 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1046 parseerror(parser, "empty parenthesis expression");
1050 while (vec_size(sy->ops)) {
1051 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1052 if (!parser_close_call(parser, sy))
1056 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1057 vec_shrinkby(sy->ops, 1);
1058 return !functions_only;
1060 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1063 /* pop off the parenthesis */
1064 vec_shrinkby(sy->ops, 1);
1065 /* then apply the index operator */
1066 if (!parser_sy_pop(parser, sy))
1070 if (!parser_sy_pop(parser, sy))
1076 static void parser_reclassify_token(parser_t *parser)
1079 for (i = 0; i < operator_count; ++i) {
1080 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1081 parser->tok = TOKEN_OPERATOR;
1087 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1089 ast_expression *expr = NULL;
1091 bool wantop = false;
1092 bool gotmemberof = false;
1094 /* count the parens because an if starts with one, so the
1095 * end of a condition is an unmatched closing paren
1103 parser->lex->flags.noops = false;
1105 parser_reclassify_token(parser);
1110 gotmemberof = false;
1112 parser->memberof = 0;
1114 if (parser->tok == TOKEN_IDENT)
1116 ast_expression *var;
1118 parseerror(parser, "expected operator or end of statement");
1123 if (opts_standard == COMPILER_GMQCC)
1125 if (parser->memberof == TYPE_ENTITY) {
1126 /* still get vars first since there could be a fieldpointer */
1127 var = parser_find_var(parser, parser_tokval(parser));
1129 var = parser_find_field(parser, parser_tokval(parser));
1131 else if (parser->memberof == TYPE_VECTOR)
1133 parseerror(parser, "TODO: implement effective vector member access");
1136 else if (parser->memberof) {
1137 parseerror(parser, "namespace for member not found");
1141 var = parser_find_var(parser, parser_tokval(parser));
1143 var = parser_find_var(parser, parser_tokval(parser));
1145 var = parser_find_field(parser, parser_tokval(parser));
1148 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1151 if (ast_istype(var, ast_value))
1152 ((ast_value*)var)->uses++;
1153 vec_push(sy.out, syexp(parser_ctx(parser), var));
1154 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1156 else if (parser->tok == TOKEN_FLOATCONST) {
1159 parseerror(parser, "expected operator or end of statement, got constant");
1163 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1166 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1167 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1169 else if (parser->tok == TOKEN_INTCONST) {
1172 parseerror(parser, "expected operator or end of statement, got constant");
1176 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1179 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1180 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1182 else if (parser->tok == TOKEN_STRINGCONST) {
1185 parseerror(parser, "expected operator or end of statement, got constant");
1189 val = parser_const_string(parser, parser_tokval(parser));
1192 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1193 DEBUGSHUNTDO(con_out("push string\n"));
1195 else if (parser->tok == TOKEN_VECTORCONST) {
1198 parseerror(parser, "expected operator or end of statement, got constant");
1202 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1205 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1206 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1207 parser_token(parser)->constval.v.x,
1208 parser_token(parser)->constval.v.y,
1209 parser_token(parser)->constval.v.z));
1211 else if (parser->tok == '(') {
1212 parseerror(parser, "internal error: '(' should be classified as operator");
1215 else if (parser->tok == '[') {
1216 parseerror(parser, "internal error: '[' should be classified as operator");
1219 else if (parser->tok == ')') {
1221 DEBUGSHUNTDO(con_out("do[op] )\n"));
1225 /* we do expect an operator next */
1226 /* closing an opening paren */
1227 if (!parser_close_paren(parser, &sy, false))
1230 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1234 /* allowed for function calls */
1235 if (!parser_close_paren(parser, &sy, true))
1240 else if (parser->tok == ']') {
1242 parseerror(parser, "operand expected");
1246 if (!parser_close_paren(parser, &sy, false))
1250 else if (parser->tok != TOKEN_OPERATOR) {
1252 parseerror(parser, "expected operator or end of statement");
1259 /* classify the operator */
1260 /* TODO: suffix operators */
1261 const oper_info *op;
1262 const oper_info *olast = NULL;
1264 for (o = 0; o < operator_count; ++o) {
1265 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1266 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1267 !strcmp(parser_tokval(parser), operators[o].op))
1272 if (o == operator_count) {
1273 /* no operator found... must be the end of the statement */
1276 /* found an operator */
1279 /* when declaring variables, a comma starts a new variable */
1280 if (op->id == opid1(',') && !parens && stopatcomma) {
1281 /* fixup the token */
1286 /* a colon without a pervious question mark cannot be a ternary */
1287 if (!ternaries && op->id == opid2(':','?')) {
1292 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1293 olast = &operators[vec_last(sy.ops).etype-1];
1296 (op->prec < olast->prec) ||
1297 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1299 if (!parser_sy_pop(parser, &sy))
1301 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1302 olast = &operators[vec_last(sy.ops).etype-1];
1307 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1308 /* for gmqcc standard: open up the namespace of the previous type */
1309 ast_expression *prevex = vec_last(sy.out).out;
1311 parseerror(parser, "unexpected member operator");
1314 if (prevex->expression.vtype == TYPE_ENTITY)
1315 parser->memberof = TYPE_ENTITY;
1316 else if (prevex->expression.vtype == TYPE_VECTOR)
1317 parser->memberof = TYPE_VECTOR;
1319 parseerror(parser, "type error: type has no members");
1325 if (op->id == opid1('(')) {
1327 size_t sycount = vec_size(sy.out);
1328 DEBUGSHUNTDO(con_out("push [op] (\n"));
1330 /* we expected an operator, this is the function-call operator */
1331 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1334 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1335 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1338 } else if (op->id == opid1('[')) {
1340 parseerror(parser, "unexpected array subscript");
1344 /* push both the operator and the paren, this makes life easier */
1345 vec_push(sy.ops, syop(parser_ctx(parser), op));
1346 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1348 } else if (op->id == opid2('?',':')) {
1350 vec_push(sy.ops, syop(parser_ctx(parser), op));
1353 } else if (op->id == opid2(':','?')) {
1354 /* we don't push this operator */
1358 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1359 vec_push(sy.ops, syop(parser_ctx(parser), op));
1363 if (!parser_next(parser)) {
1366 if (parser->tok == ';' ||
1367 (!parens && parser->tok == ']'))
1373 while (vec_size(sy.ops)) {
1374 if (!parser_sy_pop(parser, &sy))
1378 parser->lex->flags.noops = true;
1379 if (!vec_size(sy.out)) {
1380 parseerror(parser, "empty expression");
1383 expr = sy.out[0].out;
1386 DEBUGSHUNTDO(con_out("shunt done\n"));
1390 parser->lex->flags.noops = true;
1396 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1398 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1401 if (!parser_next(parser)) {
1408 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1411 ast_expression *cond, *ontrue, *onfalse = NULL;
1414 lex_ctx ctx = parser_ctx(parser);
1416 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1417 if (!parser_next(parser)) {
1418 parseerror(parser, "expected condition or 'not'");
1421 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "not")) {
1423 if (!parser_next(parser)) {
1424 parseerror(parser, "expected condition in parenthesis");
1428 if (parser->tok != '(') {
1429 parseerror(parser, "expected 'if' condition in parenthesis");
1432 /* parse into the expression */
1433 if (!parser_next(parser)) {
1434 parseerror(parser, "expected 'if' condition after opening paren");
1437 /* parse the condition */
1438 cond = parse_expression_leave(parser, false);
1442 if (parser->tok != ')') {
1443 parseerror(parser, "expected closing paren after 'if' condition");
1447 /* parse into the 'then' branch */
1448 if (!parser_next(parser)) {
1449 parseerror(parser, "expected statement for on-true branch of 'if'");
1453 ontrue = parse_statement_or_block(parser);
1458 /* check for an else */
1459 if (!strcmp(parser_tokval(parser), "else")) {
1460 /* parse into the 'else' branch */
1461 if (!parser_next(parser)) {
1462 parseerror(parser, "expected on-false branch after 'else'");
1467 onfalse = parse_statement_or_block(parser);
1476 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1478 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1479 *out = (ast_expression*)ifthen;
1483 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1486 ast_expression *cond, *ontrue;
1488 lex_ctx ctx = parser_ctx(parser);
1490 /* skip the 'while' and check for opening paren */
1491 if (!parser_next(parser) || parser->tok != '(') {
1492 parseerror(parser, "expected 'while' condition in parenthesis");
1495 /* parse into the expression */
1496 if (!parser_next(parser)) {
1497 parseerror(parser, "expected 'while' condition after opening paren");
1500 /* parse the condition */
1501 cond = parse_expression_leave(parser, false);
1505 if (parser->tok != ')') {
1506 parseerror(parser, "expected closing paren after 'while' condition");
1510 /* parse into the 'then' branch */
1511 if (!parser_next(parser)) {
1512 parseerror(parser, "expected while-loop body");
1516 ontrue = parse_statement_or_block(parser);
1522 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1523 *out = (ast_expression*)aloop;
1527 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1530 ast_expression *cond, *ontrue;
1532 lex_ctx ctx = parser_ctx(parser);
1534 /* skip the 'do' and get the body */
1535 if (!parser_next(parser)) {
1536 parseerror(parser, "expected loop body");
1539 ontrue = parse_statement_or_block(parser);
1543 /* expect the "while" */
1544 if (parser->tok != TOKEN_KEYWORD ||
1545 strcmp(parser_tokval(parser), "while"))
1547 parseerror(parser, "expected 'while' and condition");
1552 /* skip the 'while' and check for opening paren */
1553 if (!parser_next(parser) || parser->tok != '(') {
1554 parseerror(parser, "expected 'while' condition in parenthesis");
1558 /* parse into the expression */
1559 if (!parser_next(parser)) {
1560 parseerror(parser, "expected 'while' condition after opening paren");
1564 /* parse the condition */
1565 cond = parse_expression_leave(parser, false);
1569 if (parser->tok != ')') {
1570 parseerror(parser, "expected closing paren after 'while' condition");
1576 if (!parser_next(parser) || parser->tok != ';') {
1577 parseerror(parser, "expected semicolon after condition");
1583 if (!parser_next(parser)) {
1584 parseerror(parser, "parse error");
1590 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1591 *out = (ast_expression*)aloop;
1595 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1598 ast_expression *initexpr, *cond, *increment, *ontrue;
1599 size_t oldblocklocal;
1602 lex_ctx ctx = parser_ctx(parser);
1604 oldblocklocal = parser->blocklocal;
1605 parser->blocklocal = vec_size(parser->locals);
1612 /* skip the 'while' and check for opening paren */
1613 if (!parser_next(parser) || parser->tok != '(') {
1614 parseerror(parser, "expected 'for' expressions in parenthesis");
1617 /* parse into the expression */
1618 if (!parser_next(parser)) {
1619 parseerror(parser, "expected 'for' initializer after opening paren");
1623 if (parser->tok == TOKEN_TYPENAME) {
1624 if (opts_standard != COMPILER_GMQCC) {
1625 if (parsewarning(parser, WARN_EXTENSIONS,
1626 "current standard does not allow variable declarations in for-loop initializers"))
1630 parseerror(parser, "TODO: assignment of new variables to be non-const");
1632 if (!parse_variable(parser, block, true))
1635 else if (parser->tok != ';')
1637 initexpr = parse_expression_leave(parser, false);
1642 /* move on to condition */
1643 if (parser->tok != ';') {
1644 parseerror(parser, "expected semicolon after for-loop initializer");
1647 if (!parser_next(parser)) {
1648 parseerror(parser, "expected for-loop condition");
1652 /* parse the condition */
1653 if (parser->tok != ';') {
1654 cond = parse_expression_leave(parser, false);
1659 /* move on to incrementor */
1660 if (parser->tok != ';') {
1661 parseerror(parser, "expected semicolon after for-loop initializer");
1664 if (!parser_next(parser)) {
1665 parseerror(parser, "expected for-loop condition");
1669 /* parse the incrementor */
1670 if (parser->tok != ')') {
1671 increment = parse_expression_leave(parser, false);
1674 if (!ast_istype(increment, ast_store) &&
1675 !ast_istype(increment, ast_call) &&
1676 !ast_istype(increment, ast_binstore))
1678 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1684 if (parser->tok != ')') {
1685 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1688 /* parse into the 'then' branch */
1689 if (!parser_next(parser)) {
1690 parseerror(parser, "expected for-loop body");
1693 ontrue = parse_statement_or_block(parser);
1698 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1699 *out = (ast_expression*)aloop;
1701 while (vec_size(parser->locals) > parser->blocklocal)
1702 retval = retval && parser_pop_local(parser);
1703 parser->blocklocal = oldblocklocal;
1706 if (initexpr) ast_delete(initexpr);
1707 if (cond) ast_delete(cond);
1708 if (increment) ast_delete(increment);
1709 while (vec_size(parser->locals) > parser->blocklocal)
1710 (void)!parser_pop_local(parser);
1711 parser->blocklocal = oldblocklocal;
1715 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
1717 ast_expression *exp = NULL;
1718 ast_return *ret = NULL;
1719 ast_value *expected = parser->function->vtype;
1721 if (!parser_next(parser)) {
1722 parseerror(parser, "expected return expression");
1726 if (parser->tok != ';') {
1727 exp = parse_expression(parser, false);
1731 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1732 parseerror(parser, "return with invalid expression");
1735 ret = ast_return_new(exp->expression.node.context, exp);
1741 if (!parser_next(parser))
1742 parseerror(parser, "parse error");
1743 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1744 if (opts_standard != COMPILER_GMQCC)
1745 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1747 parseerror(parser, "return without value");
1749 ret = ast_return_new(parser_ctx(parser), NULL);
1751 *out = (ast_expression*)ret;
1755 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
1757 lex_ctx ctx = parser_ctx(parser);
1759 if (!parser_next(parser) || parser->tok != ';') {
1760 parseerror(parser, "expected semicolon");
1764 if (!parser_next(parser))
1765 parseerror(parser, "parse error");
1767 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
1771 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
1773 ast_expression *operand;
1775 ast_switch *switchnode;
1776 ast_switch_case swcase;
1778 lex_ctx ctx = parser_ctx(parser);
1780 /* parse over the opening paren */
1781 if (!parser_next(parser) || parser->tok != '(') {
1782 parseerror(parser, "expected switch operand in parenthesis");
1786 /* parse into the expression */
1787 if (!parser_next(parser)) {
1788 parseerror(parser, "expected switch operand");
1791 /* parse the operand */
1792 operand = parse_expression_leave(parser, false);
1796 if (!OPTS_FLAG(RELAXED_SWITCH)) {
1797 opval = (ast_value*)operand;
1798 if (!ast_istype(operand, ast_value) || !opval->isconst) {
1799 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
1805 switchnode = ast_switch_new(ctx, operand);
1808 if (parser->tok != ')') {
1809 ast_delete(switchnode);
1810 parseerror(parser, "expected closing paren after 'switch' operand");
1814 /* parse over the opening paren */
1815 if (!parser_next(parser) || parser->tok != '{') {
1816 ast_delete(switchnode);
1817 parseerror(parser, "expected list of cases");
1821 if (!parser_next(parser)) {
1822 ast_delete(switchnode);
1823 parseerror(parser, "expected 'case' or 'default'");
1828 while (parser->tok != '}') {
1831 if (parser->tok != TOKEN_KEYWORD) {
1832 ast_delete(switchnode);
1833 parseerror(parser, "expected 'case' or 'default'");
1836 if (!strcmp(parser_tokval(parser), "case")) {
1837 if (!parser_next(parser)) {
1838 ast_delete(switchnode);
1839 parseerror(parser, "expected expression for case");
1842 swcase.value = parse_expression_leave(parser, false);
1843 if (!swcase.value) {
1844 ast_delete(switchnode);
1845 parseerror(parser, "expected expression for case");
1849 else if (!strcmp(parser_tokval(parser), "default")) {
1850 swcase.value = NULL;
1851 if (!parser_next(parser)) {
1852 ast_delete(switchnode);
1853 parseerror(parser, "expected colon");
1858 /* Now the colon and body */
1859 if (parser->tok != ':') {
1860 if (swcase.value) ast_unref(swcase.value);
1861 ast_delete(switchnode);
1862 parseerror(parser, "expected colon");
1866 if (!parser_next(parser)) {
1867 if (swcase.value) ast_unref(swcase.value);
1868 ast_delete(switchnode);
1869 parseerror(parser, "expected statements or case");
1872 block = ast_block_new(parser_ctx(parser));
1874 if (swcase.value) ast_unref(swcase.value);
1875 ast_delete(switchnode);
1878 swcase.code = (ast_expression*)block;
1879 vec_push(switchnode->cases, swcase);
1881 ast_expression *expr;
1882 if (parser->tok == '}')
1884 if (parser->tok == TOKEN_KEYWORD) {
1885 if (!strcmp(parser_tokval(parser), "case") ||
1886 !strcmp(parser_tokval(parser), "default"))
1891 if (!parse_statement(parser, block, &expr, true)) {
1892 ast_delete(switchnode);
1897 vec_push(block->exprs, expr);
1902 if (parser->tok != '}') {
1903 ast_delete(switchnode);
1904 parseerror(parser, "expected closing paren of case list");
1907 if (!parser_next(parser)) {
1908 ast_delete(switchnode);
1909 parseerror(parser, "parse error after switch");
1912 *out = (ast_expression*)switchnode;
1916 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
1918 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
1920 /* local variable */
1922 parseerror(parser, "cannot declare a variable from here");
1925 if (opts_standard == COMPILER_QCC) {
1926 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1929 if (!parse_variable(parser, block, false))
1934 else if (parser->tok == TOKEN_KEYWORD)
1936 if (!strcmp(parser_tokval(parser), "local"))
1939 parseerror(parser, "cannot declare a local variable here");
1942 if (!parser_next(parser)) {
1943 parseerror(parser, "expected variable declaration");
1946 if (!parse_variable(parser, block, true))
1951 else if (!strcmp(parser_tokval(parser), "return"))
1953 return parse_return(parser, block, out);
1955 else if (!strcmp(parser_tokval(parser), "if"))
1957 return parse_if(parser, block, out);
1959 else if (!strcmp(parser_tokval(parser), "while"))
1961 return parse_while(parser, block, out);
1963 else if (!strcmp(parser_tokval(parser), "do"))
1965 return parse_dowhile(parser, block, out);
1967 else if (!strcmp(parser_tokval(parser), "for"))
1969 if (opts_standard == COMPILER_QCC) {
1970 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1973 return parse_for(parser, block, out);
1975 else if (!strcmp(parser_tokval(parser), "break"))
1977 return parse_break_continue(parser, block, out, false);
1979 else if (!strcmp(parser_tokval(parser), "continue"))
1981 return parse_break_continue(parser, block, out, true);
1983 else if (!strcmp(parser_tokval(parser), "switch"))
1985 return parse_switch(parser, block, out);
1987 else if (!strcmp(parser_tokval(parser), "case") ||
1988 !strcmp(parser_tokval(parser), "default"))
1991 parseerror(parser, "unexpected 'case' label");
1996 parseerror(parser, "Unexpected keyword");
1999 else if (parser->tok == '{')
2002 inner = parse_block(parser, false);
2005 *out = (ast_expression*)inner;
2010 ast_expression *exp = parse_expression(parser, false);
2014 if (!ast_istype(exp, ast_store) &&
2015 !ast_istype(exp, ast_call) &&
2016 !ast_istype(exp, ast_binstore))
2018 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2025 static bool GMQCC_WARN parser_pop_local(parser_t *parser)
2030 ve = &vec_last(parser->locals);
2031 if (!parser->errors) {
2032 if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
2033 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
2038 vec_pop(parser->locals);
2042 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2044 size_t oldblocklocal;
2047 oldblocklocal = parser->blocklocal;
2048 parser->blocklocal = vec_size(parser->locals);
2050 if (!parser_next(parser)) { /* skip the '{' */
2051 parseerror(parser, "expected function body");
2055 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2057 ast_expression *expr;
2058 if (parser->tok == '}')
2061 if (!parse_statement(parser, block, &expr, false)) {
2062 /* parseerror(parser, "parse error"); */
2068 vec_push(block->exprs, expr);
2071 if (parser->tok != '}') {
2074 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2076 if (!vec_size(block->exprs) ||
2077 !ast_istype(vec_last(block->exprs), ast_return))
2079 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2085 (void)parser_next(parser);
2089 while (vec_size(parser->locals) > parser->blocklocal)
2090 retval = retval && parser_pop_local(parser);
2091 parser->blocklocal = oldblocklocal;
2095 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2098 block = ast_block_new(parser_ctx(parser));
2101 if (!parse_block_into(parser, block, warnreturn)) {
2102 ast_block_delete(block);
2108 static ast_expression* parse_statement_or_block(parser_t *parser)
2110 ast_expression *expr = NULL;
2111 if (parser->tok == '{')
2112 return (ast_expression*)parse_block(parser, false);
2113 if (!parse_statement(parser, NULL, &expr, false))
2119 static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
2122 size_t len = strlen(var->name);
2124 for (i = 0; i < 3; ++i) {
2125 ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
2129 ve[i].name = (char*)mem_a(len+3);
2131 ast_delete(ve[i].var);
2135 memcpy(ve[i].name, var->name, len);
2136 ve[i].name[len] = '_';
2137 ve[i].name[len+1] = 'x'+i;
2138 ve[i].name[len+2] = 0;
2147 ast_delete(ve[i].var);
2154 static bool parse_function_body(parser_t *parser, ast_value *var)
2156 ast_block *block = NULL;
2161 ast_expression *framenum = NULL;
2162 ast_expression *nextthink = NULL;
2163 /* None of the following have to be deleted */
2164 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2165 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2166 bool has_frame_think;
2170 has_frame_think = false;
2171 old = parser->function;
2173 if (var->expression.variadic) {
2174 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2175 "variadic function with implementation will not be able to access additional parameters"))
2181 if (parser->tok == '[') {
2182 /* got a frame definition: [ framenum, nextthink ]
2183 * this translates to:
2184 * self.frame = framenum;
2185 * self.nextthink = time + 0.1;
2186 * self.think = nextthink;
2190 fld_think = parser_find_field(parser, "think");
2191 fld_nextthink = parser_find_field(parser, "nextthink");
2192 fld_frame = parser_find_field(parser, "frame");
2193 if (!fld_think || !fld_nextthink || !fld_frame) {
2194 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2195 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2198 gbl_time = parser_find_global(parser, "time");
2199 gbl_self = parser_find_global(parser, "self");
2200 if (!gbl_time || !gbl_self) {
2201 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2202 parseerror(parser, "please declare the following globals: `time`, `self`");
2206 if (!parser_next(parser))
2209 framenum = parse_expression_leave(parser, true);
2211 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2214 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
2215 ast_unref(framenum);
2216 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2220 if (parser->tok != ',') {
2221 ast_unref(framenum);
2222 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2223 parseerror(parser, "Got a %i\n", parser->tok);
2227 if (!parser_next(parser)) {
2228 ast_unref(framenum);
2232 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2234 /* qc allows the use of not-yet-declared functions here
2235 * - this automatically creates a prototype */
2237 ast_value *thinkfunc;
2238 ast_expression *functype = fld_think->expression.next;
2240 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2241 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2242 ast_unref(framenum);
2243 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2247 if (!parser_next(parser)) {
2248 ast_unref(framenum);
2249 ast_delete(thinkfunc);
2253 varent.var = (ast_expression*)thinkfunc;
2254 varent.name = util_strdup(thinkfunc->name);
2255 vec_push(parser->globals, varent);
2256 nextthink = (ast_expression*)thinkfunc;
2259 nextthink = parse_expression_leave(parser, true);
2261 ast_unref(framenum);
2262 parseerror(parser, "expected a think-function in [frame,think] notation");
2267 if (!ast_istype(nextthink, ast_value)) {
2268 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2272 if (retval && parser->tok != ']') {
2273 parseerror(parser, "expected closing `]` for [frame,think] notation");
2277 if (retval && !parser_next(parser)) {
2281 if (retval && parser->tok != '{') {
2282 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2287 ast_unref(nextthink);
2288 ast_unref(framenum);
2292 has_frame_think = true;
2295 block = ast_block_new(parser_ctx(parser));
2297 parseerror(parser, "failed to allocate block");
2298 if (has_frame_think) {
2299 ast_unref(nextthink);
2300 ast_unref(framenum);
2305 if (has_frame_think) {
2307 ast_expression *self_frame;
2308 ast_expression *self_nextthink;
2309 ast_expression *self_think;
2310 ast_expression *time_plus_1;
2311 ast_store *store_frame;
2312 ast_store *store_nextthink;
2313 ast_store *store_think;
2315 ctx = parser_ctx(parser);
2316 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2317 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2318 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2320 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2321 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2323 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2324 if (self_frame) ast_delete(self_frame);
2325 if (self_nextthink) ast_delete(self_nextthink);
2326 if (self_think) ast_delete(self_think);
2327 if (time_plus_1) ast_delete(time_plus_1);
2333 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2334 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2335 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2338 ast_delete(self_frame);
2341 if (!store_nextthink) {
2342 ast_delete(self_nextthink);
2346 ast_delete(self_think);
2350 if (store_frame) ast_delete(store_frame);
2351 if (store_nextthink) ast_delete(store_nextthink);
2352 if (store_think) ast_delete(store_think);
2355 vec_push(block->exprs, (ast_expression*)store_frame);
2356 vec_push(block->exprs, (ast_expression*)store_nextthink);
2357 vec_push(block->exprs, (ast_expression*)store_think);
2361 parseerror(parser, "failed to generate code for [frame,think]");
2362 ast_unref(nextthink);
2363 ast_unref(framenum);
2369 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2372 ast_value *param = var->expression.params[parami];
2374 if (param->expression.vtype != TYPE_VECTOR &&
2375 (param->expression.vtype != TYPE_FIELD ||
2376 param->expression.next->expression.vtype != TYPE_VECTOR))
2381 if (!create_vector_members(parser, param, ve)) {
2382 ast_block_delete(block);
2386 for (e = 0; e < 3; ++e) {
2387 vec_push(parser->locals, ve[e]);
2388 ast_block_collect(block, ve[e].var);
2389 ve[e].var = NULL; /* collected */
2393 func = ast_function_new(ast_ctx(var), var->name, var);
2395 parseerror(parser, "failed to allocate function for `%s`", var->name);
2396 ast_block_delete(block);
2399 vec_push(parser->functions, func);
2401 parser->function = func;
2402 if (!parse_block_into(parser, block, true)) {
2403 ast_block_delete(block);
2407 vec_push(func->blocks, block);
2409 parser->function = old;
2410 while (vec_size(parser->locals))
2411 retval = retval && parser_pop_local(parser);
2413 if (parser->tok == ';')
2414 return parser_next(parser);
2415 else if (opts_standard == COMPILER_QCC)
2416 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2420 vec_pop(parser->functions);
2421 ast_function_delete(func);
2422 var->constval.vfunc = NULL;
2425 while (vec_size(parser->locals)) {
2426 mem_d(vec_last(parser->locals).name);
2427 vec_pop(parser->locals);
2429 parser->function = old;
2433 static ast_expression *array_accessor_split(
2438 ast_expression *left,
2439 ast_expression *right
2445 lex_ctx ctx = ast_ctx(array);
2447 if (!left || !right) {
2448 if (left) ast_delete(left);
2449 if (right) ast_delete(right);
2453 cmp = ast_binary_new(ctx, INSTR_LT,
2454 (ast_expression*)index,
2455 (ast_expression*)parser_const_float(parser, middle));
2459 parseerror(parser, "internal error: failed to create comparison for array setter");
2463 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2465 ast_delete(cmp); /* will delete left and right */
2466 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2470 return (ast_expression*)ifthen;
2473 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2475 lex_ctx ctx = ast_ctx(array);
2477 if (from+1 == afterend) {
2481 ast_array_index *subscript;
2482 int assignop = type_store_instr[value->expression.vtype];
2484 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2485 assignop = INSTR_STORE_V;
2487 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2491 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2493 ast_delete(subscript);
2497 block = ast_block_new(ctx);
2503 vec_push(block->exprs, (ast_expression*)st);
2505 ret = ast_return_new(ctx, NULL);
2511 vec_push(block->exprs, (ast_expression*)ret);
2513 return (ast_expression*)block;
2515 ast_expression *left, *right;
2516 size_t diff = afterend - from;
2517 size_t middle = from + diff/2;
2518 left = array_setter_node(parser, array, index, value, from, middle);
2519 right = array_setter_node(parser, array, index, value, middle, afterend);
2520 return array_accessor_split(parser, array, index, middle, left, right);
2524 static ast_expression *array_field_setter_node(
2533 lex_ctx ctx = ast_ctx(array);
2535 if (from+1 == afterend) {
2539 ast_entfield *entfield;
2540 ast_array_index *subscript;
2541 int assignop = type_storep_instr[value->expression.vtype];
2543 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2544 assignop = INSTR_STOREP_V;
2546 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2550 entfield = ast_entfield_new_force(ctx,
2551 (ast_expression*)entity,
2552 (ast_expression*)subscript,
2553 (ast_expression*)subscript);
2555 ast_delete(subscript);
2559 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2561 ast_delete(entfield);
2565 block = ast_block_new(ctx);
2571 vec_push(block->exprs, (ast_expression*)st);
2573 ret = ast_return_new(ctx, NULL);
2579 vec_push(block->exprs, (ast_expression*)ret);
2581 return (ast_expression*)block;
2583 ast_expression *left, *right;
2584 size_t diff = afterend - from;
2585 size_t middle = from + diff/2;
2586 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2587 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2588 return array_accessor_split(parser, array, index, middle, left, right);
2592 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2594 lex_ctx ctx = ast_ctx(array);
2596 if (from+1 == afterend) {
2598 ast_array_index *subscript;
2600 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2604 ret = ast_return_new(ctx, (ast_expression*)subscript);
2606 ast_delete(subscript);
2610 return (ast_expression*)ret;
2612 ast_expression *left, *right;
2613 size_t diff = afterend - from;
2614 size_t middle = from + diff/2;
2615 left = array_getter_node(parser, array, index, from, middle);
2616 right = array_getter_node(parser, array, index, middle, afterend);
2617 return array_accessor_split(parser, array, index, middle, left, right);
2621 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2623 ast_function *func = NULL;
2624 ast_value *fval = NULL;
2625 ast_block *body = NULL;
2627 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2629 parseerror(parser, "failed to create accessor function value");
2633 func = ast_function_new(ast_ctx(array), funcname, fval);
2636 parseerror(parser, "failed to create accessor function node");
2640 body = ast_block_new(ast_ctx(array));
2642 parseerror(parser, "failed to create block for array accessor");
2648 vec_push(func->blocks, body);
2651 vec_push(parser->accessors, fval);
2656 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2658 ast_expression *root = NULL;
2659 ast_value *index = NULL;
2660 ast_value *value = NULL;
2664 if (!ast_istype(array->expression.next, ast_value)) {
2665 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2669 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2671 func = fval->constval.vfunc;
2672 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2674 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2675 value = ast_value_copy((ast_value*)array->expression.next);
2677 if (!index || !value) {
2678 parseerror(parser, "failed to create locals for array accessor");
2681 (void)!ast_value_set_name(value, "value"); /* not important */
2682 vec_push(fval->expression.params, index);
2683 vec_push(fval->expression.params, value);
2685 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2687 parseerror(parser, "failed to build accessor search tree");
2691 vec_push(func->blocks[0]->exprs, root);
2692 array->setter = fval;
2695 if (index) ast_delete(index);
2696 if (value) ast_delete(value);
2697 if (root) ast_delete(root);
2703 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2705 ast_expression *root = NULL;
2706 ast_value *entity = NULL;
2707 ast_value *index = NULL;
2708 ast_value *value = NULL;
2712 if (!ast_istype(array->expression.next, ast_value)) {
2713 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2717 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2719 func = fval->constval.vfunc;
2720 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2722 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2723 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2724 value = ast_value_copy((ast_value*)array->expression.next);
2725 if (!entity || !index || !value) {
2726 parseerror(parser, "failed to create locals for array accessor");
2729 (void)!ast_value_set_name(value, "value"); /* not important */
2730 vec_push(fval->expression.params, entity);
2731 vec_push(fval->expression.params, index);
2732 vec_push(fval->expression.params, value);
2734 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2736 parseerror(parser, "failed to build accessor search tree");
2740 vec_push(func->blocks[0]->exprs, root);
2741 array->setter = fval;
2744 if (entity) ast_delete(entity);
2745 if (index) ast_delete(index);
2746 if (value) ast_delete(value);
2747 if (root) ast_delete(root);
2753 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2755 ast_expression *root = NULL;
2756 ast_value *index = NULL;
2760 /* NOTE: checking array->expression.next rather than elemtype since
2761 * for fields elemtype is a temporary fieldtype.
2763 if (!ast_istype(array->expression.next, ast_value)) {
2764 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2768 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2770 func = fval->constval.vfunc;
2771 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2773 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2776 parseerror(parser, "failed to create locals for array accessor");
2779 vec_push(fval->expression.params, index);
2781 root = array_getter_node(parser, array, index, 0, array->expression.count);
2783 parseerror(parser, "failed to build accessor search tree");
2787 vec_push(func->blocks[0]->exprs, root);
2788 array->getter = fval;
2791 if (index) ast_delete(index);
2792 if (root) ast_delete(root);
2798 static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
2799 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
2807 bool variadic = false;
2809 ctx = parser_ctx(parser);
2811 /* for the sake of less code we parse-in in this function */
2812 if (!parser_next(parser)) {
2813 parseerror(parser, "expected parameter list");
2819 /* parse variables until we hit a closing paren */
2820 while (parser->tok != ')') {
2822 /* there must be commas between them */
2823 if (parser->tok != ',') {
2824 parseerror(parser, "expected comma or end of parameter list");
2827 if (!parser_next(parser)) {
2828 parseerror(parser, "expected parameter");
2834 if (parser->tok == TOKEN_DOTS) {
2835 /* '...' indicates a varargs function */
2837 if (!parser_next(parser)) {
2838 parseerror(parser, "expected parameter");
2841 if (parser->tok != ')') {
2842 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
2848 /* for anything else just parse a typename */
2849 param = parse_typename(parser, NULL);
2852 vec_push(params, param);
2853 if (param->expression.vtype >= TYPE_VARIANT) {
2854 char typename[1024];
2855 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
2856 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
2863 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
2864 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
2867 if (!parser_next(parser)) {
2868 parseerror(parser, "parse error after typename");
2872 /* now turn 'var' into a function type */
2873 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
2874 fval->expression.next = (ast_expression*)var;
2875 fval->expression.variadic = variadic;
2878 var->expression.params = params;
2885 for (i = 0; i < vec_size(params); ++i)
2886 ast_delete(params[i]);
2891 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
2893 ast_expression *cexp;
2894 ast_value *cval, *tmp;
2897 ctx = parser_ctx(parser);
2899 if (!parser_next(parser)) {
2901 parseerror(parser, "expected array-size");
2905 cexp = parse_expression_leave(parser, true);
2907 if (!cexp || !ast_istype(cexp, ast_value)) {
2911 parseerror(parser, "expected array-size as constant positive integer");
2914 cval = (ast_value*)cexp;
2916 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
2917 tmp->expression.next = (ast_expression*)var;
2920 if (cval->expression.vtype == TYPE_INTEGER)
2921 tmp->expression.count = cval->constval.vint;
2922 else if (cval->expression.vtype == TYPE_FLOAT)
2923 tmp->expression.count = cval->constval.vfloat;
2927 parseerror(parser, "array-size must be a positive integer constant");
2932 if (parser->tok != ']') {
2934 parseerror(parser, "expected ']' after array-size");
2937 if (!parser_next(parser)) {
2939 parseerror(parser, "error after parsing array size");
2945 /* Parse a complete typename.
2946 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
2947 * but when parsing variables separated by comma
2948 * 'storebase' should point to where the base-type should be kept.
2949 * The base type makes up every bit of type information which comes *before* the
2952 * The following will be parsed in its entirety:
2954 * The 'basetype' in this case is 'void()'
2955 * and if there's a comma after it, say:
2957 * then the type-information 'void()' can be stored in 'storebase'
2959 static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
2961 ast_value *var, *tmp;
2964 const char *name = NULL;
2965 bool isfield = false;
2966 bool wasarray = false;
2968 ctx = parser_ctx(parser);
2970 /* types may start with a dot */
2971 if (parser->tok == '.') {
2973 /* if we parsed a dot we need a typename now */
2974 if (!parser_next(parser)) {
2975 parseerror(parser, "expected typename for field definition");
2978 if (parser->tok != TOKEN_TYPENAME) {
2979 parseerror(parser, "expected typename");
2984 /* generate the basic type value */
2985 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
2986 /* do not yet turn into a field - remember:
2987 * .void() foo; is a field too
2988 * .void()() foo; is a function
2992 if (!parser_next(parser)) {
2994 parseerror(parser, "parse error after typename");
2998 /* an opening paren now starts the parameter-list of a function
2999 * this is where original-QC has parameter lists.
3000 * We allow a single parameter list here.
3001 * Much like fteqcc we don't allow `float()() x`
3003 if (parser->tok == '(') {
3004 var = parse_parameter_list(parser, var);
3009 /* store the base if requested */
3011 *storebase = ast_value_copy(var);
3013 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3014 tmp->expression.next = (ast_expression*)*storebase;
3019 /* there may be a name now */
3020 if (parser->tok == TOKEN_IDENT) {
3021 name = util_strdup(parser_tokval(parser));
3023 if (!parser_next(parser)) {
3025 parseerror(parser, "error after variable or field declaration");
3030 /* now this may be an array */
3031 if (parser->tok == '[') {
3033 var = parse_arraysize(parser, var);
3038 /* This is the point where we can turn it into a field */
3040 /* turn it into a field if desired */
3041 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3042 tmp->expression.next = (ast_expression*)var;
3046 /* now there may be function parens again */
3047 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3048 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3049 if (parser->tok == '(' && wasarray)
3050 parseerror(parser, "arrays as part of a return type is not supported");
3051 while (parser->tok == '(') {
3052 var = parse_parameter_list(parser, var);
3061 /* finally name it */
3063 if (!ast_value_set_name(var, name)) {
3065 parseerror(parser, "internal error: failed to set name");
3068 /* free the name, ast_value_set_name duplicates */
3075 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
3079 ast_expression *old;
3083 ast_value *basetype = NULL;
3085 bool isparam = false;
3086 bool isvector = false;
3087 bool cleanvar = true;
3088 bool wasarray = false;
3090 varentry_t varent, ve[3];
3092 /* get the first complete variable */
3093 var = parse_typename(parser, &basetype);
3096 ast_delete(basetype);
3100 memset(&varent, 0, sizeof(varent));
3101 memset(&ve, 0, sizeof(ve));
3107 /* Part 0: finish the type */
3108 if (parser->tok == '(') {
3109 if (opts_standard == COMPILER_QCC)
3110 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3111 var = parse_parameter_list(parser, var);
3117 /* we only allow 1-dimensional arrays */
3118 if (parser->tok == '[') {
3120 var = parse_arraysize(parser, var);
3126 if (parser->tok == '(' && wasarray) {
3127 parseerror(parser, "arrays as part of a return type is not supported");
3128 /* we'll still parse the type completely for now */
3130 /* for functions returning functions */
3131 while (parser->tok == '(') {
3132 if (opts_standard == COMPILER_QCC)
3133 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3134 var = parse_parameter_list(parser, var);
3142 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3143 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3144 * is then filled with the previous definition and the parameter-names replaced.
3147 /* Deal with end_sys_ vars */
3149 if (!strcmp(var->name, "end_sys_globals")) {
3150 parser->crc_globals = vec_size(parser->globals);
3153 else if (!strcmp(var->name, "end_sys_fields")) {
3154 parser->crc_fields = vec_size(parser->fields);
3157 if (was_end && var->expression.vtype == TYPE_FIELD) {
3158 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3159 "global '%s' hint should not be a field",
3160 parser_tokval(parser)))
3167 if (!nofields && var->expression.vtype == TYPE_FIELD)
3169 /* deal with field declarations */
3170 old = parser_find_field(parser, var->name);
3172 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3173 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3182 parseerror(parser, "field `%s` already declared here: %s:%i",
3183 var->name, ast_ctx(old).file, ast_ctx(old).line);
3188 if (opts_standard == COMPILER_QCC &&
3189 (old = parser_find_global(parser, var->name)))
3191 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3192 parseerror(parser, "field `%s` already declared here: %s:%i",
3193 var->name, ast_ctx(old).file, ast_ctx(old).line);
3200 /* deal with other globals */
3201 old = parser_find_global(parser, var->name);
3202 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3204 /* This is a function which had a prototype */
3205 if (!ast_istype(old, ast_value)) {
3206 parseerror(parser, "internal error: prototype is not an ast_value");
3210 proto = (ast_value*)old;
3211 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3212 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3214 ast_ctx(proto).file, ast_ctx(proto).line);
3218 /* we need the new parameter-names */
3219 for (i = 0; i < vec_size(proto->expression.params); ++i)
3220 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3228 parseerror(parser, "global `%s` already declared here: %s:%i",
3229 var->name, ast_ctx(old).file, ast_ctx(old).line);
3233 if (opts_standard == COMPILER_QCC &&
3234 (old = parser_find_field(parser, var->name)))
3236 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3237 parseerror(parser, "global `%s` already declared here: %s:%i",
3238 var->name, ast_ctx(old).file, ast_ctx(old).line);
3245 else /* it's not a global */
3247 old = parser_find_local(parser, var->name, parser->blocklocal, &isparam);
3248 if (old && !isparam) {
3249 parseerror(parser, "local `%s` already declared here: %s:%i",
3250 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3254 old = parser_find_local(parser, var->name, 0, &isparam);
3255 if (old && isparam) {
3256 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3257 "local `%s` is shadowing a parameter", var->name))
3259 parseerror(parser, "local `%s` already declared here: %s:%i",
3260 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3264 if (opts_standard != COMPILER_GMQCC) {
3273 * Create the global/local, and deal with vector types.
3276 if (var->expression.vtype == TYPE_VECTOR)
3278 else if (var->expression.vtype == TYPE_FIELD &&
3279 var->expression.next->expression.vtype == TYPE_VECTOR)
3283 if (!create_vector_members(parser, var, ve)) {
3289 varent.name = util_strdup(var->name);
3290 varent.var = (ast_expression*)var;
3293 /* deal with global variables, fields, functions */
3294 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3295 vec_push(parser->fields, varent);
3297 for (i = 0; i < 3; ++i)
3298 vec_push(parser->fields, ve[i]);
3302 vec_push(parser->globals, varent);
3304 for (i = 0; i < 3; ++i)
3305 vec_push(parser->globals, ve[i]);
3309 vec_push(parser->locals, varent);
3310 vec_push(localblock->locals, var);
3312 for (i = 0; i < 3; ++i) {
3313 vec_push(parser->locals, ve[i]);
3314 ast_block_collect(localblock, ve[i].var);
3315 ve[i].var = NULL; /* from here it's being collected in the block */
3321 ve[0].name = ve[1].name = ve[2].name = NULL;
3322 ve[0].var = ve[1].var = ve[2].var = NULL;
3328 if (var->expression.vtype == TYPE_ARRAY) {
3330 snprintf(name, sizeof(name), "%s##SET", var->name);
3331 if (!parser_create_array_setter(parser, var, name))
3333 snprintf(name, sizeof(name), "%s##GET", var->name);
3334 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3337 else if (!localblock && !nofields &&
3338 var->expression.vtype == TYPE_FIELD &&
3339 var->expression.next->expression.vtype == TYPE_ARRAY)
3342 ast_expression *telem;
3344 ast_value *array = (ast_value*)var->expression.next;
3346 if (!ast_istype(var->expression.next, ast_value)) {
3347 parseerror(parser, "internal error: field element type must be an ast_value");
3351 snprintf(name, sizeof(name), "%s##SETF", var->name);
3352 if (!parser_create_array_field_setter(parser, array, name))
3355 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3356 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3357 tfield->expression.next = telem;
3358 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3359 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3367 if (parser->tok == ';') {
3368 ast_delete(basetype);
3369 if (!parser_next(parser)) {
3370 parseerror(parser, "error after variable declaration");
3376 if (parser->tok == ',')
3379 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3380 parseerror(parser, "missing comma or semicolon while parsing variables");
3384 if (localblock && opts_standard == COMPILER_QCC) {
3385 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3386 "initializing expression turns variable `%s` into a constant in this standard",
3393 if (parser->tok != '{') {
3394 if (parser->tok != '=') {
3395 parseerror(parser, "missing semicolon or initializer");
3399 if (!parser_next(parser)) {
3400 parseerror(parser, "error parsing initializer");
3404 else if (opts_standard == COMPILER_QCC) {
3405 parseerror(parser, "expected '=' before function body in this standard");
3408 if (parser->tok == '#') {
3412 parseerror(parser, "cannot declare builtins within functions");
3415 if (var->expression.vtype != TYPE_FUNCTION) {
3416 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3419 if (!parser_next(parser)) {
3420 parseerror(parser, "expected builtin number");
3423 if (parser->tok != TOKEN_INTCONST) {
3424 parseerror(parser, "builtin number must be an integer constant");
3427 if (parser_token(parser)->constval.i <= 0) {
3428 parseerror(parser, "builtin number must be an integer greater than zero");
3432 func = ast_function_new(ast_ctx(var), var->name, var);
3434 parseerror(parser, "failed to allocate function for `%s`", var->name);
3437 vec_push(parser->functions, func);
3439 func->builtin = -parser_token(parser)->constval.i;
3441 if (!parser_next(parser)) {
3442 parseerror(parser, "expected comma or semicolon");
3443 ast_function_delete(func);
3444 var->constval.vfunc = NULL;
3448 else if (parser->tok == '{' || parser->tok == '[')
3451 parseerror(parser, "cannot declare functions within functions");
3455 if (!parse_function_body(parser, var))
3457 ast_delete(basetype);
3460 ast_expression *cexp;
3463 cexp = parse_expression_leave(parser, true);
3468 cval = (ast_value*)cexp;
3469 if (!ast_istype(cval, ast_value) || !cval->isconst)
3470 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3473 var->isconst = true;
3474 if (cval->expression.vtype == TYPE_STRING)
3475 var->constval.vstring = parser_strdup(cval->constval.vstring);
3477 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3481 shunt sy = { NULL, NULL };
3482 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
3483 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
3484 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
3485 if (!parser_sy_pop(parser, &sy))
3488 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
3489 parseerror(parser, "internal error: leaked operands");
3490 vec_push(localblock->exprs, (ast_expression*)sy.out[0].out);
3498 if (parser->tok == ',') {
3499 if (!parser_next(parser)) {
3500 parseerror(parser, "expected another variable");
3504 if (parser->tok != TOKEN_IDENT) {
3505 parseerror(parser, "expected another variable");
3508 var = ast_value_copy(basetype);
3510 ast_value_set_name(var, parser_tokval(parser));
3511 if (!parser_next(parser)) {
3512 parseerror(parser, "error parsing variable declaration");
3518 if (parser->tok != ';') {
3519 parseerror(parser, "missing semicolon after variables");
3523 if (!parser_next(parser)) {
3524 parseerror(parser, "parse error after variable declaration");
3528 ast_delete(basetype);
3532 if (cleanvar && var)
3534 ast_delete(basetype);
3538 ast_delete(basetype);
3539 if (cleanvar && var)
3541 if (varent.name) mem_d(varent.name);
3542 if (ve[0].name) mem_d(ve[0].name);
3543 if (ve[1].name) mem_d(ve[1].name);
3544 if (ve[2].name) mem_d(ve[2].name);
3545 if (ve[0].var) mem_d(ve[0].var);
3546 if (ve[1].var) mem_d(ve[1].var);
3547 if (ve[2].var) mem_d(ve[2].var);
3551 static bool parser_global_statement(parser_t *parser)
3553 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3555 return parse_variable(parser, NULL, false);
3557 else if (parser->tok == TOKEN_KEYWORD)
3559 /* handle 'var' and 'const' */
3560 if (!strcmp(parser_tokval(parser), "var")) {
3561 if (!parser_next(parser)) {
3562 parseerror(parser, "expected variable declaration after 'var'");
3565 return parse_variable(parser, NULL, true);
3569 else if (parser->tok == '$')
3571 if (!parser_next(parser)) {
3572 parseerror(parser, "parse error");
3578 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3584 static parser_t *parser;
3589 parser = (parser_t*)mem_a(sizeof(parser_t));
3593 memset(parser, 0, sizeof(*parser));
3595 for (i = 0; i < operator_count; ++i) {
3596 if (operators[i].id == opid1('=')) {
3597 parser->assign_op = operators+i;
3601 if (!parser->assign_op) {
3602 printf("internal error: initializing parser: failed to find assign operator\n");
3609 bool parser_compile()
3611 /* initial lexer/parser state */
3612 parser->lex->flags.noops = true;
3614 if (parser_next(parser))
3616 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3618 if (!parser_global_statement(parser)) {
3619 if (parser->tok == TOKEN_EOF)
3620 parseerror(parser, "unexpected eof");
3621 else if (!parser->errors)
3622 parseerror(parser, "there have been errors, bailing out");
3623 lex_close(parser->lex);
3629 parseerror(parser, "parse error");
3630 lex_close(parser->lex);
3635 lex_close(parser->lex);
3638 return !parser->errors;
3641 bool parser_compile_file(const char *filename)
3643 parser->lex = lex_open(filename);
3645 con_err("failed to open file \"%s\"\n", filename);
3648 return parser_compile();
3651 bool parser_compile_string_len(const char *name, const char *str, size_t len)
3653 parser->lex = lex_open_string(str, len, name);
3655 con_err("failed to create lexer for string \"%s\"\n", name);
3658 return parser_compile();
3661 bool parser_compile_string(const char *name, const char *str)
3663 parser->lex = lex_open_string(str, strlen(str), name);
3665 con_err("failed to create lexer for string \"%s\"\n", name);
3668 return parser_compile();
3671 void parser_cleanup()
3674 for (i = 0; i < vec_size(parser->accessors); ++i) {
3675 ast_delete(parser->accessors[i]->constval.vfunc);
3676 parser->accessors[i]->constval.vfunc = NULL;
3677 ast_delete(parser->accessors[i]);
3679 for (i = 0; i < vec_size(parser->functions); ++i) {
3680 ast_delete(parser->functions[i]);
3682 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3683 ast_delete(parser->imm_vector[i]);
3685 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3686 ast_delete(parser->imm_string[i]);
3688 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3689 ast_delete(parser->imm_float[i]);
3691 for (i = 0; i < vec_size(parser->fields); ++i) {
3692 ast_delete(parser->fields[i].var);
3693 mem_d(parser->fields[i].name);
3695 for (i = 0; i < vec_size(parser->globals); ++i) {
3696 ast_delete(parser->globals[i].var);
3697 mem_d(parser->globals[i].name);
3699 vec_free(parser->accessors);
3700 vec_free(parser->functions);
3701 vec_free(parser->imm_vector);
3702 vec_free(parser->imm_string);
3703 vec_free(parser->imm_float);
3704 vec_free(parser->globals);
3705 vec_free(parser->fields);
3706 vec_free(parser->locals);
3711 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3713 return util_crc16(old, str, strlen(str));
3716 static void progdefs_crc_file(const char *str)
3718 /* write to progdefs.h here */
3721 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3723 old = progdefs_crc_sum(old, str);
3724 progdefs_crc_file(str);
3728 static void generate_checksum(parser_t *parser)
3730 uint16_t crc = 0xFFFF;
3733 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3734 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3736 progdefs_crc_file("\tint\tpad;\n");
3737 progdefs_crc_file("\tint\tofs_return[3];\n");
3738 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3739 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3740 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3741 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3742 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3743 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3744 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3745 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3747 for (i = 0; i < parser->crc_globals; ++i) {
3748 if (!ast_istype(parser->globals[i].var, ast_value))
3750 switch (parser->globals[i].var->expression.vtype) {
3751 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3752 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3753 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3754 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3756 crc = progdefs_crc_both(crc, "\tint\t");
3759 crc = progdefs_crc_both(crc, parser->globals[i].name);
3760 crc = progdefs_crc_both(crc, ";\n");
3762 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3763 for (i = 0; i < parser->crc_fields; ++i) {
3764 if (!ast_istype(parser->fields[i].var, ast_value))
3766 switch (parser->fields[i].var->expression.next->expression.vtype) {
3767 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3768 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3769 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3770 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3772 crc = progdefs_crc_both(crc, "\tint\t");
3775 crc = progdefs_crc_both(crc, parser->fields[i].name);
3776 crc = progdefs_crc_both(crc, ";\n");
3778 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3783 bool parser_finish(const char *output)
3789 if (!parser->errors)
3791 ir = ir_builder_new("gmqcc_out");
3793 con_out("failed to allocate builder\n");
3797 for (i = 0; i < vec_size(parser->fields); ++i) {
3800 if (!ast_istype(parser->fields[i].var, ast_value))
3802 field = (ast_value*)parser->fields[i].var;
3803 isconst = field->isconst;
3804 field->isconst = false;
3805 if (!ast_global_codegen((ast_value*)field, ir, true)) {
3806 con_out("failed to generate field %s\n", field->name);
3807 ir_builder_delete(ir);
3812 ast_expression *subtype;
3813 field->isconst = true;
3814 subtype = field->expression.next;
3815 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
3816 if (subtype->expression.vtype == TYPE_FIELD)
3817 ifld->fieldtype = subtype->expression.next->expression.vtype;
3818 else if (subtype->expression.vtype == TYPE_FUNCTION)
3819 ifld->outtype = subtype->expression.next->expression.vtype;
3820 (void)!ir_value_set_field(field->ir_v, ifld);
3823 for (i = 0; i < vec_size(parser->globals); ++i) {
3825 if (!ast_istype(parser->globals[i].var, ast_value))
3827 asvalue = (ast_value*)(parser->globals[i].var);
3828 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
3829 if (strcmp(asvalue->name, "end_sys_globals") &&
3830 strcmp(asvalue->name, "end_sys_fields"))
3832 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
3833 "unused global: `%s`", asvalue->name);
3836 if (!ast_global_codegen(asvalue, ir, false)) {
3837 con_out("failed to generate global %s\n", parser->globals[i].name);
3838 ir_builder_delete(ir);
3842 for (i = 0; i < vec_size(parser->imm_float); ++i) {
3843 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
3844 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
3845 ir_builder_delete(ir);
3849 for (i = 0; i < vec_size(parser->imm_string); ++i) {
3850 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
3851 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
3852 ir_builder_delete(ir);
3856 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
3857 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
3858 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
3859 ir_builder_delete(ir);
3863 for (i = 0; i < vec_size(parser->globals); ++i) {
3865 if (!ast_istype(parser->globals[i].var, ast_value))
3867 asvalue = (ast_value*)(parser->globals[i].var);
3868 if (asvalue->setter) {
3869 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3870 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3871 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3873 printf("failed to generate setter for %s\n", parser->globals[i].name);
3874 ir_builder_delete(ir);
3878 if (asvalue->getter) {
3879 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3880 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3881 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3883 printf("failed to generate getter for %s\n", parser->globals[i].name);
3884 ir_builder_delete(ir);
3889 for (i = 0; i < vec_size(parser->fields); ++i) {
3891 asvalue = (ast_value*)(parser->fields[i].var->expression.next);
3893 if (!ast_istype((ast_expression*)asvalue, ast_value))
3895 if (asvalue->expression.vtype != TYPE_ARRAY)
3897 if (asvalue->setter) {
3898 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3899 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3900 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3902 printf("failed to generate setter for %s\n", parser->fields[i].name);
3903 ir_builder_delete(ir);
3907 if (asvalue->getter) {
3908 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3909 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3910 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3912 printf("failed to generate getter for %s\n", parser->fields[i].name);
3913 ir_builder_delete(ir);
3918 for (i = 0; i < vec_size(parser->functions); ++i) {
3919 if (!ast_function_codegen(parser->functions[i], ir)) {
3920 con_out("failed to generate function %s\n", parser->functions[i]->name);
3921 ir_builder_delete(ir);
3924 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
3925 con_out("failed to finalize function %s\n", parser->functions[i]->name);
3926 ir_builder_delete(ir);
3933 ir_builder_dump(ir, con_out);
3935 generate_checksum(parser);
3937 if (!ir_builder_generate(ir, output)) {
3938 con_out("*** failed to generate output file\n");
3939 ir_builder_delete(ir);
3944 ir_builder_delete(ir);
3948 con_out("*** there were compile errors\n");