5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define PARSER_HT_FIELDS 0
30 #define PARSER_HT_GLOBALS 1
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 1024
35 #define TYPEDEF_HT_SIZE 16
41 ast_expression **globals;
42 ast_expression **fields;
43 ast_function **functions;
44 ast_value **imm_float;
45 ast_value **imm_string;
46 ast_value **imm_vector;
48 /* must be deleted first, they reference immediates and values */
49 ast_value **accessors;
51 ast_value *imm_float_zero;
52 ast_value *imm_float_one;
53 ast_value *imm_vector_zero;
58 ast_function *function;
60 /* A list of hashtables for each scope */
66 /* not to be used directly, we use the hash table */
67 ast_expression **_locals;
69 ast_value **_typedefs;
70 size_t *_blocktypedefs;
74 /* we store the '=' operator info */
75 const oper_info *assign_op;
77 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
78 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
79 * anything else: type error
84 static void parser_enterblock(parser_t *parser);
85 static bool parser_leaveblock(parser_t *parser);
86 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
87 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, bool is_const, ast_value *cached_typedef);
88 static ast_block* parse_block(parser_t *parser, bool warnreturn);
89 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
90 static ast_expression* parse_statement_or_block(parser_t *parser);
91 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
92 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
93 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
95 static void parseerror(parser_t *parser, const char *fmt, ...)
102 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
106 /* returns true if it counts as an error */
107 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
110 int lvl = LVL_WARNING;
112 if (!OPTS_WARN(warntype))
121 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
127 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
130 int lvl = LVL_WARNING;
132 if (!OPTS_WARN(warntype))
139 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
145 /**********************************************************************
146 * some maths used for constant folding
149 vector vec3_add(vector a, vector b)
158 vector vec3_sub(vector a, vector b)
167 qcfloat vec3_mulvv(vector a, vector b)
169 return (a.x * b.x + a.y * b.y + a.z * b.z);
172 vector vec3_mulvf(vector a, float b)
181 /**********************************************************************
185 bool parser_next(parser_t *parser)
187 /* lex_do kills the previous token */
188 parser->tok = lex_do(parser->lex);
189 if (parser->tok == TOKEN_EOF)
191 if (parser->tok >= TOKEN_ERROR) {
192 parseerror(parser, "lex error");
198 #define parser_tokval(p) ((p)->lex->tok.value)
199 #define parser_token(p) (&((p)->lex->tok))
200 #define parser_ctx(p) ((p)->lex->tok.ctx)
202 static ast_value* parser_const_float(parser_t *parser, double d)
206 for (i = 0; i < vec_size(parser->imm_float); ++i) {
207 const double compare = parser->imm_float[i]->constval.vfloat;
208 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
209 return parser->imm_float[i];
211 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
213 out->constval.vfloat = d;
214 vec_push(parser->imm_float, out);
218 static ast_value* parser_const_float_0(parser_t *parser)
220 if (!parser->imm_float_zero)
221 parser->imm_float_zero = parser_const_float(parser, 0);
222 return parser->imm_float_zero;
225 static ast_value* parser_const_float_1(parser_t *parser)
227 if (!parser->imm_float_one)
228 parser->imm_float_one = parser_const_float(parser, 1);
229 return parser->imm_float_one;
232 static char *parser_strdup(const char *str)
235 /* actually dup empty strings */
236 char *out = mem_a(1);
240 return util_strdup(str);
243 static ast_value* parser_const_string(parser_t *parser, const char *str)
247 for (i = 0; i < vec_size(parser->imm_string); ++i) {
248 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
249 return parser->imm_string[i];
251 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
253 out->constval.vstring = parser_strdup(str);
254 vec_push(parser->imm_string, out);
258 static ast_value* parser_const_vector(parser_t *parser, vector v)
262 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
263 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
264 return parser->imm_vector[i];
266 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
268 out->constval.vvec = v;
269 vec_push(parser->imm_vector, out);
273 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
279 return parser_const_vector(parser, v);
282 static ast_value* parser_const_vector_0(parser_t *parser)
284 if (!parser->imm_vector_zero)
285 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
286 return parser->imm_vector_zero;
289 static ast_expression* parser_find_field(parser_t *parser, const char *name)
291 return util_htget(parser->htfields, name);
294 static ast_expression* parser_find_global(parser_t *parser, const char *name)
296 return util_htget(parser->htglobals, name);
299 static ast_expression* parser_find_param(parser_t *parser, const char *name)
303 if (!parser->function)
305 fun = parser->function->vtype;
306 for (i = 0; i < vec_size(fun->expression.params); ++i) {
307 if (!strcmp(fun->expression.params[i]->name, name))
308 return (ast_expression*)(fun->expression.params[i]);
313 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
318 hash = util_hthash(parser->htglobals, name);
321 for (i = vec_size(parser->variables); i > upto;) {
323 if ( (e = util_htgeth(parser->variables[i], name, hash)) )
327 return parser_find_param(parser, name);
330 static ast_expression* parser_find_var(parser_t *parser, const char *name)
334 v = parser_find_local(parser, name, 0, &dummy);
335 if (!v) v = parser_find_global(parser, name);
339 static ast_value* parser_find_typedef(parser_t *parser, const char *name)
343 hash = util_hthash(parser->typedefs[0], name);
345 for (i = vec_size(parser->typedefs); i > 0;) {
347 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
355 size_t etype; /* 0 = expression, others are operators */
359 ast_block *block; /* for commas and function calls */
368 #define SY_PAREN_EXPR '('
369 #define SY_PAREN_FUNC 'f'
370 #define SY_PAREN_INDEX '['
372 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
383 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
387 e.out = (ast_expression*)v;
394 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
396 e.etype = 1 + (op - operators);
405 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
417 # define DEBUGSHUNTDO(x) x
419 # define DEBUGSHUNTDO(x)
422 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
423 * so we need to rotate it to become ent.(foo[n]).
425 static bool rotate_entfield_array_index_nodes(ast_expression **out)
427 ast_array_index *index;
428 ast_entfield *entfield;
432 ast_expression *entity;
434 lex_ctx ctx = ast_ctx(*out);
436 if (!ast_istype(*out, ast_array_index))
438 index = (ast_array_index*)*out;
440 if (!ast_istype(index->array, ast_entfield))
442 entfield = (ast_entfield*)index->array;
444 if (!ast_istype(entfield->field, ast_value))
446 field = (ast_value*)entfield->field;
449 entity = entfield->entity;
453 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
454 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
455 *out = (ast_expression*)entfield;
460 static bool parser_sy_pop(parser_t *parser, shunt *sy)
464 ast_expression *out = NULL;
465 ast_expression *exprs[3];
466 ast_block *blocks[3];
467 ast_value *asvalue[3];
468 size_t i, assignop, addop, subop;
469 qcint generated_op = 0;
474 if (!vec_size(sy->ops)) {
475 parseerror(parser, "internal error: missing operator");
479 if (vec_last(sy->ops).paren) {
480 parseerror(parser, "unmatched parenthesis");
484 op = &operators[vec_last(sy->ops).etype - 1];
485 ctx = vec_last(sy->ops).ctx;
487 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
489 if (vec_size(sy->out) < op->operands) {
490 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
491 op->op, (int)op->id);
495 vec_shrinkby(sy->ops, 1);
497 vec_shrinkby(sy->out, op->operands);
498 for (i = 0; i < op->operands; ++i) {
499 exprs[i] = sy->out[vec_size(sy->out)+i].out;
500 blocks[i] = sy->out[vec_size(sy->out)+i].block;
501 asvalue[i] = (ast_value*)exprs[i];
504 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
505 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
509 #define NotSameType(T) \
510 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
511 exprs[0]->expression.vtype != T)
512 #define CanConstFold1(A) \
513 (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
514 #define CanConstFold(A, B) \
515 (CanConstFold1(A) && CanConstFold1(B))
516 #define ConstV(i) (asvalue[(i)]->constval.vvec)
517 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
518 #define ConstS(i) (asvalue[(i)]->constval.vstring)
522 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
526 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
527 if (exprs[1]->expression.vtype != TYPE_FIELD) {
528 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
531 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
533 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
534 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
538 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
544 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
545 !(exprs[0]->expression.vtype == TYPE_FIELD &&
546 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
548 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
549 parseerror(parser, "cannot index value of type %s", ty1);
552 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
553 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
554 parseerror(parser, "index must be of type float, not %s", ty1);
557 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
558 if (rotate_entfield_array_index_nodes(&out))
560 if (opts_standard != COMPILER_GMQCC) {
561 /* this error doesn't need to make us bail out */
562 (void)!parsewarning(parser, WARN_EXTENSIONS,
563 "accessing array-field members of an entity without parenthesis\n"
564 " -> this is an extension from -std=gmqcc");
571 vec_push(blocks[0]->exprs, exprs[1]);
573 blocks[0] = ast_block_new(ctx);
574 vec_push(blocks[0]->exprs, exprs[0]);
575 vec_push(blocks[0]->exprs, exprs[1]);
577 if (!ast_block_set_type(blocks[0], exprs[1]))
580 vec_push(sy->out, syblock(ctx, blocks[0]));
584 switch (exprs[0]->expression.vtype) {
586 if (CanConstFold1(exprs[0]))
587 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
589 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
590 (ast_expression*)parser_const_float_0(parser),
594 if (CanConstFold1(exprs[0]))
595 out = (ast_expression*)parser_const_vector_f(parser,
596 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
598 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
599 (ast_expression*)parser_const_vector_0(parser),
603 parseerror(parser, "invalid types used in expression: cannot negate type %s",
604 type_name[exprs[0]->expression.vtype]);
610 switch (exprs[0]->expression.vtype) {
612 if (CanConstFold1(exprs[0]))
613 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
615 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
618 if (CanConstFold1(exprs[0]))
619 out = (ast_expression*)parser_const_float(parser,
620 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
622 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
625 if (CanConstFold1(exprs[0]))
626 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
628 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
630 /* we don't constant-fold NOT for these types */
632 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
635 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
638 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
639 type_name[exprs[0]->expression.vtype]);
645 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
646 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
648 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
649 type_name[exprs[0]->expression.vtype],
650 type_name[exprs[1]->expression.vtype]);
653 switch (exprs[0]->expression.vtype) {
655 if (CanConstFold(exprs[0], exprs[1]))
657 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
660 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
663 if (CanConstFold(exprs[0], exprs[1]))
664 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
666 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
669 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
670 type_name[exprs[0]->expression.vtype],
671 type_name[exprs[1]->expression.vtype]);
676 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
677 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
679 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
680 type_name[exprs[1]->expression.vtype],
681 type_name[exprs[0]->expression.vtype]);
684 switch (exprs[0]->expression.vtype) {
686 if (CanConstFold(exprs[0], exprs[1]))
687 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
689 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
692 if (CanConstFold(exprs[0], exprs[1]))
693 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
695 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
698 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
699 type_name[exprs[1]->expression.vtype],
700 type_name[exprs[0]->expression.vtype]);
705 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
706 exprs[0]->expression.vtype != TYPE_VECTOR &&
707 exprs[0]->expression.vtype != TYPE_FLOAT &&
708 exprs[1]->expression.vtype != TYPE_VECTOR &&
709 exprs[1]->expression.vtype != TYPE_FLOAT)
711 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
712 type_name[exprs[1]->expression.vtype],
713 type_name[exprs[0]->expression.vtype]);
716 switch (exprs[0]->expression.vtype) {
718 if (exprs[1]->expression.vtype == TYPE_VECTOR)
720 if (CanConstFold(exprs[0], exprs[1]))
721 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
723 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
727 if (CanConstFold(exprs[0], exprs[1]))
728 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
730 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
734 if (exprs[1]->expression.vtype == TYPE_FLOAT)
736 if (CanConstFold(exprs[0], exprs[1]))
737 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
739 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
743 if (CanConstFold(exprs[0], exprs[1]))
744 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
746 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
750 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
751 type_name[exprs[1]->expression.vtype],
752 type_name[exprs[0]->expression.vtype]);
757 if (NotSameType(TYPE_FLOAT)) {
758 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
759 type_name[exprs[0]->expression.vtype],
760 type_name[exprs[1]->expression.vtype]);
763 if (CanConstFold(exprs[0], exprs[1]))
764 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
766 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
770 parseerror(parser, "qc does not have a modulo operator");
774 if (NotSameType(TYPE_FLOAT)) {
775 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
776 type_name[exprs[0]->expression.vtype],
777 type_name[exprs[1]->expression.vtype]);
780 if (CanConstFold(exprs[0], exprs[1]))
781 out = (ast_expression*)parser_const_float(parser,
782 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
783 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
785 out = (ast_expression*)ast_binary_new(ctx,
786 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
790 parseerror(parser, "TODO: bitxor");
795 case opid3('<','<','='):
796 case opid3('>','>','='):
797 parseerror(parser, "TODO: shifts");
801 generated_op += 1; /* INSTR_OR */
803 generated_op += INSTR_AND;
804 if (NotSameType(TYPE_FLOAT)) {
805 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
806 type_name[exprs[0]->expression.vtype],
807 type_name[exprs[1]->expression.vtype]);
808 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
809 parseerror(parser, "TODO: optional early out");
812 if (opts_standard == COMPILER_GMQCC)
813 con_out("TODO: early out logic\n");
814 if (CanConstFold(exprs[0], exprs[1]))
815 out = (ast_expression*)parser_const_float(parser,
816 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
818 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
822 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
823 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
824 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
825 parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
828 if (CanConstFold1(exprs[0]))
829 out = (ConstF(0) ? exprs[1] : exprs[2]);
831 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
835 generated_op += 1; /* INSTR_GT */
837 generated_op += 1; /* INSTR_LT */
838 case opid2('>', '='):
839 generated_op += 1; /* INSTR_GE */
840 case opid2('<', '='):
841 generated_op += INSTR_LE;
842 if (NotSameType(TYPE_FLOAT)) {
843 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
844 type_name[exprs[0]->expression.vtype],
845 type_name[exprs[1]->expression.vtype]);
848 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
850 case opid2('!', '='):
851 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
852 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
853 type_name[exprs[0]->expression.vtype],
854 type_name[exprs[1]->expression.vtype]);
857 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
859 case opid2('=', '='):
860 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
861 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
862 type_name[exprs[0]->expression.vtype],
863 type_name[exprs[1]->expression.vtype]);
866 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
870 if (ast_istype(exprs[0], ast_entfield)) {
871 ast_expression *field = ((ast_entfield*)exprs[0])->field;
872 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
873 exprs[0]->expression.vtype == TYPE_FIELD &&
874 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
876 assignop = type_storep_instr[TYPE_VECTOR];
879 assignop = type_storep_instr[exprs[0]->expression.vtype];
880 if (!ast_compare_type(field->expression.next, exprs[1])) {
881 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
882 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
883 if (opts_standard == COMPILER_QCC &&
884 field->expression.next->expression.vtype == TYPE_FUNCTION &&
885 exprs[1]->expression.vtype == TYPE_FUNCTION)
887 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
888 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
894 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
899 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
900 exprs[0]->expression.vtype == TYPE_FIELD &&
901 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
903 assignop = type_store_instr[TYPE_VECTOR];
906 assignop = type_store_instr[exprs[0]->expression.vtype];
909 if (assignop == AINSTR_END) {
910 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
911 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
912 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
914 else if (!ast_compare_type(exprs[0], exprs[1])) {
915 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
916 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
917 if (opts_standard == COMPILER_QCC &&
918 exprs[0]->expression.vtype == TYPE_FUNCTION &&
919 exprs[1]->expression.vtype == TYPE_FUNCTION)
921 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
922 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
928 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
931 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
933 case opid3('+','+','P'):
934 case opid3('-','-','P'):
936 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
937 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
938 parseerror(parser, "invalid type for prefix increment: %s", ty1);
941 if (op->id == opid3('+','+','P'))
945 if (ast_istype(exprs[0], ast_entfield)) {
946 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
948 (ast_expression*)parser_const_float_1(parser));
950 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
952 (ast_expression*)parser_const_float_1(parser));
955 case opid3('S','+','+'):
956 case opid3('S','-','-'):
958 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
959 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
960 parseerror(parser, "invalid type for suffix increment: %s", ty1);
963 if (op->id == opid3('S','+','+')) {
970 if (ast_istype(exprs[0], ast_entfield)) {
971 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
973 (ast_expression*)parser_const_float_1(parser));
975 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
977 (ast_expression*)parser_const_float_1(parser));
981 out = (ast_expression*)ast_binary_new(ctx, subop,
983 (ast_expression*)parser_const_float_1(parser));
987 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
988 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
990 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
991 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
992 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
996 if (ast_istype(exprs[0], ast_entfield))
997 assignop = type_storep_instr[exprs[0]->expression.vtype];
999 assignop = type_store_instr[exprs[0]->expression.vtype];
1000 switch (exprs[0]->expression.vtype) {
1002 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1003 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1004 exprs[0], exprs[1]);
1007 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1008 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1009 exprs[0], exprs[1]);
1012 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1013 type_name[exprs[0]->expression.vtype],
1014 type_name[exprs[1]->expression.vtype]);
1018 case opid2('*','='):
1019 case opid2('/','='):
1020 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1021 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1022 exprs[0]->expression.vtype == TYPE_VECTOR))
1024 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1025 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1026 parseerror(parser, "invalid types used in expression: %s and %s",
1030 if (ast_istype(exprs[0], ast_entfield))
1031 assignop = type_storep_instr[exprs[0]->expression.vtype];
1033 assignop = type_store_instr[exprs[0]->expression.vtype];
1034 switch (exprs[0]->expression.vtype) {
1036 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1037 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1038 exprs[0], exprs[1]);
1041 if (op->id == opid2('*','=')) {
1042 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1043 exprs[0], exprs[1]);
1045 /* there's no DIV_VF */
1046 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1047 (ast_expression*)parser_const_float_1(parser),
1051 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1056 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1057 type_name[exprs[0]->expression.vtype],
1058 type_name[exprs[1]->expression.vtype]);
1062 case opid2('&','='):
1063 case opid2('|','='):
1064 if (NotSameType(TYPE_FLOAT)) {
1065 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1066 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1067 parseerror(parser, "invalid types used in expression: %s and %s",
1071 if (ast_istype(exprs[0], ast_entfield))
1072 assignop = type_storep_instr[exprs[0]->expression.vtype];
1074 assignop = type_store_instr[exprs[0]->expression.vtype];
1075 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1076 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1077 exprs[0], exprs[1]);
1079 case opid3('&','~','='):
1080 /* This is like: a &= ~(b);
1081 * But QC has no bitwise-not, so we implement it as
1084 if (NotSameType(TYPE_FLOAT)) {
1085 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1086 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1087 parseerror(parser, "invalid types used in expression: %s and %s",
1091 if (ast_istype(exprs[0], ast_entfield))
1092 assignop = type_storep_instr[exprs[0]->expression.vtype];
1094 assignop = type_store_instr[exprs[0]->expression.vtype];
1095 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1098 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1104 parseerror(parser, "failed to apply operand %s", op->op);
1108 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1109 vec_push(sy->out, syexp(ctx, out));
1113 static bool parser_close_call(parser_t *parser, shunt *sy)
1115 /* was a function call */
1116 ast_expression *fun;
1122 vec_shrinkby(sy->ops, 1);
1123 fid = sy->ops[vec_size(sy->ops)].off;
1125 /* out[fid] is the function
1126 * everything above is parameters...
1127 * 0 params = nothing
1128 * 1 params = ast_expression
1132 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1133 parseerror(parser, "internal error: function call needs function and parameter list...");
1137 fun = sy->out[fid].out;
1139 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1141 parseerror(parser, "out of memory");
1145 if (fid+1 == vec_size(sy->out)) {
1148 } else if (fid+2 == vec_size(sy->out)) {
1150 vec_shrinkby(sy->out, 1);
1151 params = sy->out[vec_size(sy->out)].block;
1155 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1157 paramcount = vec_size(params->exprs);
1158 call->params = params->exprs;
1159 params->exprs = NULL;
1162 if (!ast_call_check_types(call))
1165 parseerror(parser, "invalid function call");
1169 /* overwrite fid, the function, with a call */
1170 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1172 if (fun->expression.vtype != TYPE_FUNCTION) {
1173 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1177 if (!fun->expression.next) {
1178 parseerror(parser, "could not determine function return type");
1181 if (vec_size(fun->expression.params) != paramcount &&
1182 !(fun->expression.variadic &&
1183 vec_size(fun->expression.params) < paramcount))
1186 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1188 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1189 if (opts_standard == COMPILER_GMQCC)
1192 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1193 " -> `%s` has been declared here: %s:%i",
1194 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1195 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1197 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1198 " -> `%s` has been declared here: %s:%i",
1199 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1200 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1206 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1207 "too %s parameters for call to %s: expected %i, got %i\n"
1208 " -> `%s` has been declared here: %s:%i",
1209 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1210 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1212 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1213 "too %s parameters for function call: expected %i, got %i\n"
1214 " -> `%s` has been declared here: %s:%i",
1215 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1216 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1224 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1226 if (!vec_size(sy->ops)) {
1227 parseerror(parser, "unmatched closing paren");
1230 /* this would for bit a + (x) because there are no operators inside (x)
1231 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1232 parseerror(parser, "empty parenthesis expression");
1236 while (vec_size(sy->ops)) {
1237 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1238 if (!parser_close_call(parser, sy))
1242 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1243 vec_shrinkby(sy->ops, 1);
1244 return !functions_only;
1246 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1249 /* pop off the parenthesis */
1250 vec_shrinkby(sy->ops, 1);
1251 /* then apply the index operator */
1252 if (!parser_sy_pop(parser, sy))
1256 if (!parser_sy_pop(parser, sy))
1262 static void parser_reclassify_token(parser_t *parser)
1265 for (i = 0; i < operator_count; ++i) {
1266 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1267 parser->tok = TOKEN_OPERATOR;
1273 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1275 ast_expression *expr = NULL;
1277 bool wantop = false;
1278 bool gotmemberof = false;
1280 /* count the parens because an if starts with one, so the
1281 * end of a condition is an unmatched closing paren
1289 parser->lex->flags.noops = false;
1291 parser_reclassify_token(parser);
1296 gotmemberof = false;
1298 parser->memberof = 0;
1300 if (parser->tok == TOKEN_IDENT)
1302 ast_expression *var;
1304 parseerror(parser, "expected operator or end of statement");
1309 if (opts_standard == COMPILER_GMQCC)
1311 if (parser->memberof == TYPE_ENTITY) {
1312 /* still get vars first since there could be a fieldpointer */
1313 var = parser_find_var(parser, parser_tokval(parser));
1315 var = parser_find_field(parser, parser_tokval(parser));
1317 else if (parser->memberof == TYPE_VECTOR)
1319 parseerror(parser, "TODO: implement effective vector member access");
1322 else if (parser->memberof) {
1323 parseerror(parser, "namespace for member not found");
1327 var = parser_find_var(parser, parser_tokval(parser));
1329 var = parser_find_var(parser, parser_tokval(parser));
1331 var = parser_find_field(parser, parser_tokval(parser));
1334 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1337 if (ast_istype(var, ast_value))
1338 ((ast_value*)var)->uses++;
1339 vec_push(sy.out, syexp(parser_ctx(parser), var));
1340 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1342 else if (parser->tok == TOKEN_FLOATCONST) {
1345 parseerror(parser, "expected operator or end of statement, got constant");
1349 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1352 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1353 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1355 else if (parser->tok == TOKEN_INTCONST) {
1358 parseerror(parser, "expected operator or end of statement, got constant");
1362 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1365 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1366 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1368 else if (parser->tok == TOKEN_STRINGCONST) {
1371 parseerror(parser, "expected operator or end of statement, got constant");
1375 val = parser_const_string(parser, parser_tokval(parser));
1378 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1379 DEBUGSHUNTDO(con_out("push string\n"));
1381 else if (parser->tok == TOKEN_VECTORCONST) {
1384 parseerror(parser, "expected operator or end of statement, got constant");
1388 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1391 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1392 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1393 parser_token(parser)->constval.v.x,
1394 parser_token(parser)->constval.v.y,
1395 parser_token(parser)->constval.v.z));
1397 else if (parser->tok == '(') {
1398 parseerror(parser, "internal error: '(' should be classified as operator");
1401 else if (parser->tok == '[') {
1402 parseerror(parser, "internal error: '[' should be classified as operator");
1405 else if (parser->tok == ')') {
1407 DEBUGSHUNTDO(con_out("do[op] )\n"));
1411 /* we do expect an operator next */
1412 /* closing an opening paren */
1413 if (!parser_close_paren(parser, &sy, false))
1416 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1420 /* allowed for function calls */
1421 if (!parser_close_paren(parser, &sy, true))
1426 else if (parser->tok == ']') {
1428 parseerror(parser, "operand expected");
1432 if (!parser_close_paren(parser, &sy, false))
1436 else if (parser->tok != TOKEN_OPERATOR) {
1438 parseerror(parser, "expected operator or end of statement");
1445 /* classify the operator */
1446 const oper_info *op;
1447 const oper_info *olast = NULL;
1449 for (o = 0; o < operator_count; ++o) {
1450 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1451 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1452 !strcmp(parser_tokval(parser), operators[o].op))
1457 if (o == operator_count) {
1458 /* no operator found... must be the end of the statement */
1461 /* found an operator */
1464 /* when declaring variables, a comma starts a new variable */
1465 if (op->id == opid1(',') && !parens && stopatcomma) {
1466 /* fixup the token */
1471 /* a colon without a pervious question mark cannot be a ternary */
1472 if (!ternaries && op->id == opid2(':','?')) {
1477 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1478 olast = &operators[vec_last(sy.ops).etype-1];
1481 (op->prec < olast->prec) ||
1482 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1484 if (!parser_sy_pop(parser, &sy))
1486 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1487 olast = &operators[vec_last(sy.ops).etype-1];
1492 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1493 /* for gmqcc standard: open up the namespace of the previous type */
1494 ast_expression *prevex = vec_last(sy.out).out;
1496 parseerror(parser, "unexpected member operator");
1499 if (prevex->expression.vtype == TYPE_ENTITY)
1500 parser->memberof = TYPE_ENTITY;
1501 else if (prevex->expression.vtype == TYPE_VECTOR)
1502 parser->memberof = TYPE_VECTOR;
1504 parseerror(parser, "type error: type has no members");
1510 if (op->id == opid1('(')) {
1512 size_t sycount = vec_size(sy.out);
1513 DEBUGSHUNTDO(con_out("push [op] (\n"));
1515 /* we expected an operator, this is the function-call operator */
1516 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1519 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1520 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1523 } else if (op->id == opid1('[')) {
1525 parseerror(parser, "unexpected array subscript");
1529 /* push both the operator and the paren, this makes life easier */
1530 vec_push(sy.ops, syop(parser_ctx(parser), op));
1531 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1533 } else if (op->id == opid2('?',':')) {
1535 vec_push(sy.ops, syop(parser_ctx(parser), op));
1538 } else if (op->id == opid2(':','?')) {
1539 /* we don't push this operator */
1543 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1544 vec_push(sy.ops, syop(parser_ctx(parser), op));
1545 wantop = !!(op->flags & OP_SUFFIX);
1548 if (!parser_next(parser)) {
1551 if (parser->tok == ';' ||
1552 (!parens && parser->tok == ']'))
1558 while (vec_size(sy.ops)) {
1559 if (!parser_sy_pop(parser, &sy))
1563 parser->lex->flags.noops = true;
1564 if (!vec_size(sy.out)) {
1565 parseerror(parser, "empty expression");
1568 expr = sy.out[0].out;
1571 DEBUGSHUNTDO(con_out("shunt done\n"));
1575 parser->lex->flags.noops = true;
1581 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1583 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1586 if (!parser_next(parser)) {
1593 static void parser_enterblock(parser_t *parser)
1595 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1596 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1597 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1598 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1601 static bool parser_leaveblock(parser_t *parser)
1604 size_t locals, typedefs;
1606 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1607 parseerror(parser, "internal error: parser_leaveblock with no block");
1611 util_htdel(vec_last(parser->variables));
1612 vec_pop(parser->variables);
1613 if (!vec_size(parser->_blocklocals)) {
1614 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1618 locals = vec_last(parser->_blocklocals);
1619 vec_pop(parser->_blocklocals);
1620 while (vec_size(parser->_locals) != locals) {
1621 ast_expression *e = vec_last(parser->_locals);
1622 ast_value *v = (ast_value*)e;
1623 vec_pop(parser->_locals);
1624 if (ast_istype(e, ast_value) && !v->uses) {
1625 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
1630 typedefs = vec_last(parser->_blocktypedefs);
1631 while (vec_size(parser->_typedefs) != typedefs) {
1632 ast_delete(vec_last(parser->_typedefs));
1633 vec_pop(parser->_typedefs);
1635 util_htdel(vec_last(parser->typedefs));
1636 vec_pop(parser->typedefs);
1641 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1643 vec_push(parser->_locals, e);
1644 util_htset(vec_last(parser->variables), name, (void*)e);
1647 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1650 ast_expression *cond, *ontrue, *onfalse = NULL;
1653 lex_ctx ctx = parser_ctx(parser);
1655 (void)block; /* not touching */
1657 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1658 if (!parser_next(parser)) {
1659 parseerror(parser, "expected condition or 'not'");
1662 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "not")) {
1664 if (!parser_next(parser)) {
1665 parseerror(parser, "expected condition in parenthesis");
1669 if (parser->tok != '(') {
1670 parseerror(parser, "expected 'if' condition in parenthesis");
1673 /* parse into the expression */
1674 if (!parser_next(parser)) {
1675 parseerror(parser, "expected 'if' condition after opening paren");
1678 /* parse the condition */
1679 cond = parse_expression_leave(parser, false);
1683 if (parser->tok != ')') {
1684 parseerror(parser, "expected closing paren after 'if' condition");
1688 /* parse into the 'then' branch */
1689 if (!parser_next(parser)) {
1690 parseerror(parser, "expected statement for on-true branch of 'if'");
1694 ontrue = parse_statement_or_block(parser);
1699 /* check for an else */
1700 if (!strcmp(parser_tokval(parser), "else")) {
1701 /* parse into the 'else' branch */
1702 if (!parser_next(parser)) {
1703 parseerror(parser, "expected on-false branch after 'else'");
1708 onfalse = parse_statement_or_block(parser);
1717 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1719 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1720 *out = (ast_expression*)ifthen;
1724 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1727 ast_expression *cond, *ontrue;
1729 lex_ctx ctx = parser_ctx(parser);
1731 (void)block; /* not touching */
1733 /* skip the 'while' and check for opening paren */
1734 if (!parser_next(parser) || parser->tok != '(') {
1735 parseerror(parser, "expected 'while' condition in parenthesis");
1738 /* parse into the expression */
1739 if (!parser_next(parser)) {
1740 parseerror(parser, "expected 'while' condition after opening paren");
1743 /* parse the condition */
1744 cond = parse_expression_leave(parser, false);
1748 if (parser->tok != ')') {
1749 parseerror(parser, "expected closing paren after 'while' condition");
1753 /* parse into the 'then' branch */
1754 if (!parser_next(parser)) {
1755 parseerror(parser, "expected while-loop body");
1759 ontrue = parse_statement_or_block(parser);
1765 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1766 *out = (ast_expression*)aloop;
1770 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1773 ast_expression *cond, *ontrue;
1775 lex_ctx ctx = parser_ctx(parser);
1777 (void)block; /* not touching */
1779 /* skip the 'do' and get the body */
1780 if (!parser_next(parser)) {
1781 parseerror(parser, "expected loop body");
1784 ontrue = parse_statement_or_block(parser);
1788 /* expect the "while" */
1789 if (parser->tok != TOKEN_KEYWORD ||
1790 strcmp(parser_tokval(parser), "while"))
1792 parseerror(parser, "expected 'while' and condition");
1797 /* skip the 'while' and check for opening paren */
1798 if (!parser_next(parser) || parser->tok != '(') {
1799 parseerror(parser, "expected 'while' condition in parenthesis");
1803 /* parse into the expression */
1804 if (!parser_next(parser)) {
1805 parseerror(parser, "expected 'while' condition after opening paren");
1809 /* parse the condition */
1810 cond = parse_expression_leave(parser, false);
1814 if (parser->tok != ')') {
1815 parseerror(parser, "expected closing paren after 'while' condition");
1821 if (!parser_next(parser) || parser->tok != ';') {
1822 parseerror(parser, "expected semicolon after condition");
1828 if (!parser_next(parser)) {
1829 parseerror(parser, "parse error");
1835 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1836 *out = (ast_expression*)aloop;
1840 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1843 ast_expression *initexpr, *cond, *increment, *ontrue;
1847 lex_ctx ctx = parser_ctx(parser);
1849 parser_enterblock(parser);
1856 /* skip the 'while' and check for opening paren */
1857 if (!parser_next(parser) || parser->tok != '(') {
1858 parseerror(parser, "expected 'for' expressions in parenthesis");
1861 /* parse into the expression */
1862 if (!parser_next(parser)) {
1863 parseerror(parser, "expected 'for' initializer after opening paren");
1868 if (parser->tok == TOKEN_IDENT)
1869 typevar = parser_find_typedef(parser, parser_tokval(parser));
1871 if (typevar || parser->tok == TOKEN_TYPENAME) {
1872 if (opts_standard != COMPILER_GMQCC) {
1873 if (parsewarning(parser, WARN_EXTENSIONS,
1874 "current standard does not allow variable declarations in for-loop initializers"))
1878 parseerror(parser, "TODO: assignment of new variables to be non-const");
1880 if (!parse_variable(parser, block, true, false, typevar))
1883 else if (parser->tok != ';')
1885 initexpr = parse_expression_leave(parser, false);
1890 /* move on to condition */
1891 if (parser->tok != ';') {
1892 parseerror(parser, "expected semicolon after for-loop initializer");
1895 if (!parser_next(parser)) {
1896 parseerror(parser, "expected for-loop condition");
1900 /* parse the condition */
1901 if (parser->tok != ';') {
1902 cond = parse_expression_leave(parser, false);
1907 /* move on to incrementor */
1908 if (parser->tok != ';') {
1909 parseerror(parser, "expected semicolon after for-loop initializer");
1912 if (!parser_next(parser)) {
1913 parseerror(parser, "expected for-loop condition");
1917 /* parse the incrementor */
1918 if (parser->tok != ')') {
1919 increment = parse_expression_leave(parser, false);
1922 if (!ast_istype(increment, ast_store) &&
1923 !ast_istype(increment, ast_call) &&
1924 !ast_istype(increment, ast_binstore))
1926 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1932 if (parser->tok != ')') {
1933 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1936 /* parse into the 'then' branch */
1937 if (!parser_next(parser)) {
1938 parseerror(parser, "expected for-loop body");
1941 ontrue = parse_statement_or_block(parser);
1946 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1947 *out = (ast_expression*)aloop;
1949 if (!parser_leaveblock(parser))
1953 if (initexpr) ast_delete(initexpr);
1954 if (cond) ast_delete(cond);
1955 if (increment) ast_delete(increment);
1956 (void)!parser_leaveblock(parser);
1960 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
1962 ast_expression *exp = NULL;
1963 ast_return *ret = NULL;
1964 ast_value *expected = parser->function->vtype;
1966 (void)block; /* not touching */
1968 if (!parser_next(parser)) {
1969 parseerror(parser, "expected return expression");
1973 if (parser->tok != ';') {
1974 exp = parse_expression(parser, false);
1978 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1979 parseerror(parser, "return with invalid expression");
1982 ret = ast_return_new(exp->expression.node.context, exp);
1988 if (!parser_next(parser))
1989 parseerror(parser, "parse error");
1990 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1991 if (opts_standard != COMPILER_GMQCC)
1992 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1994 parseerror(parser, "return without value");
1996 ret = ast_return_new(parser_ctx(parser), NULL);
1998 *out = (ast_expression*)ret;
2002 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2004 lex_ctx ctx = parser_ctx(parser);
2006 (void)block; /* not touching */
2008 if (!parser_next(parser) || parser->tok != ';') {
2009 parseerror(parser, "expected semicolon");
2013 if (!parser_next(parser))
2014 parseerror(parser, "parse error");
2016 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2020 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2022 ast_expression *operand;
2024 ast_switch *switchnode;
2025 ast_switch_case swcase;
2027 lex_ctx ctx = parser_ctx(parser);
2029 (void)block; /* not touching */
2031 /* parse over the opening paren */
2032 if (!parser_next(parser) || parser->tok != '(') {
2033 parseerror(parser, "expected switch operand in parenthesis");
2037 /* parse into the expression */
2038 if (!parser_next(parser)) {
2039 parseerror(parser, "expected switch operand");
2042 /* parse the operand */
2043 operand = parse_expression_leave(parser, false);
2047 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2048 opval = (ast_value*)operand;
2049 if (!ast_istype(operand, ast_value) || !opval->isconst) {
2050 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2056 switchnode = ast_switch_new(ctx, operand);
2059 if (parser->tok != ')') {
2060 ast_delete(switchnode);
2061 parseerror(parser, "expected closing paren after 'switch' operand");
2065 /* parse over the opening paren */
2066 if (!parser_next(parser) || parser->tok != '{') {
2067 ast_delete(switchnode);
2068 parseerror(parser, "expected list of cases");
2072 if (!parser_next(parser)) {
2073 ast_delete(switchnode);
2074 parseerror(parser, "expected 'case' or 'default'");
2079 while (parser->tok != '}') {
2080 ast_block *caseblock;
2082 if (parser->tok != TOKEN_KEYWORD) {
2083 ast_delete(switchnode);
2084 parseerror(parser, "expected 'case' or 'default'");
2087 if (!strcmp(parser_tokval(parser), "case")) {
2088 if (!parser_next(parser)) {
2089 ast_delete(switchnode);
2090 parseerror(parser, "expected expression for case");
2093 swcase.value = parse_expression_leave(parser, false);
2094 if (!swcase.value) {
2095 ast_delete(switchnode);
2096 parseerror(parser, "expected expression for case");
2100 else if (!strcmp(parser_tokval(parser), "default")) {
2101 swcase.value = NULL;
2102 if (!parser_next(parser)) {
2103 ast_delete(switchnode);
2104 parseerror(parser, "expected colon");
2109 /* Now the colon and body */
2110 if (parser->tok != ':') {
2111 if (swcase.value) ast_unref(swcase.value);
2112 ast_delete(switchnode);
2113 parseerror(parser, "expected colon");
2117 if (!parser_next(parser)) {
2118 if (swcase.value) ast_unref(swcase.value);
2119 ast_delete(switchnode);
2120 parseerror(parser, "expected statements or case");
2123 caseblock = ast_block_new(parser_ctx(parser));
2125 if (swcase.value) ast_unref(swcase.value);
2126 ast_delete(switchnode);
2129 swcase.code = (ast_expression*)caseblock;
2130 vec_push(switchnode->cases, swcase);
2132 ast_expression *expr;
2133 if (parser->tok == '}')
2135 if (parser->tok == TOKEN_KEYWORD) {
2136 if (!strcmp(parser_tokval(parser), "case") ||
2137 !strcmp(parser_tokval(parser), "default"))
2142 if (!parse_statement(parser, caseblock, &expr, true)) {
2143 ast_delete(switchnode);
2148 vec_push(caseblock->exprs, expr);
2153 if (parser->tok != '}') {
2154 ast_delete(switchnode);
2155 parseerror(parser, "expected closing paren of case list");
2158 if (!parser_next(parser)) {
2159 ast_delete(switchnode);
2160 parseerror(parser, "parse error after switch");
2163 *out = (ast_expression*)switchnode;
2167 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2169 ast_value *typevar = NULL;
2170 if (parser->tok == TOKEN_IDENT)
2171 typevar = parser_find_typedef(parser, parser_tokval(parser));
2173 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2175 /* local variable */
2177 parseerror(parser, "cannot declare a variable from here");
2180 if (opts_standard == COMPILER_QCC) {
2181 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2184 if (!parse_variable(parser, block, false, false, typevar))
2189 else if (parser->tok == TOKEN_KEYWORD)
2191 if (!strcmp(parser_tokval(parser), "local"))
2194 parseerror(parser, "cannot declare a local variable here");
2197 if (!parser_next(parser)) {
2198 parseerror(parser, "expected variable declaration");
2201 if (!parse_variable(parser, block, true, false, NULL))
2206 else if (!strcmp(parser_tokval(parser), "return"))
2208 return parse_return(parser, block, out);
2210 else if (!strcmp(parser_tokval(parser), "if"))
2212 return parse_if(parser, block, out);
2214 else if (!strcmp(parser_tokval(parser), "while"))
2216 return parse_while(parser, block, out);
2218 else if (!strcmp(parser_tokval(parser), "do"))
2220 return parse_dowhile(parser, block, out);
2222 else if (!strcmp(parser_tokval(parser), "for"))
2224 if (opts_standard == COMPILER_QCC) {
2225 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2228 return parse_for(parser, block, out);
2230 else if (!strcmp(parser_tokval(parser), "break"))
2232 return parse_break_continue(parser, block, out, false);
2234 else if (!strcmp(parser_tokval(parser), "continue"))
2236 return parse_break_continue(parser, block, out, true);
2238 else if (!strcmp(parser_tokval(parser), "switch"))
2240 return parse_switch(parser, block, out);
2242 else if (!strcmp(parser_tokval(parser), "case") ||
2243 !strcmp(parser_tokval(parser), "default"))
2246 parseerror(parser, "unexpected 'case' label");
2251 parseerror(parser, "Unexpected keyword");
2254 else if (parser->tok == '{')
2257 inner = parse_block(parser, false);
2260 *out = (ast_expression*)inner;
2265 ast_expression *exp = parse_expression(parser, false);
2269 if (!ast_istype(exp, ast_store) &&
2270 !ast_istype(exp, ast_call) &&
2271 !ast_istype(exp, ast_binstore))
2273 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2280 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2284 parser_enterblock(parser);
2286 if (!parser_next(parser)) { /* skip the '{' */
2287 parseerror(parser, "expected function body");
2291 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2293 ast_expression *expr;
2294 if (parser->tok == '}')
2297 if (!parse_statement(parser, block, &expr, false)) {
2298 /* parseerror(parser, "parse error"); */
2304 vec_push(block->exprs, expr);
2307 if (parser->tok != '}') {
2310 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2312 if (!vec_size(block->exprs) ||
2313 !ast_istype(vec_last(block->exprs), ast_return))
2315 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2321 (void)parser_next(parser);
2325 if (!parser_leaveblock(parser))
2327 return retval && !!block;
2330 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2333 block = ast_block_new(parser_ctx(parser));
2336 if (!parse_block_into(parser, block, warnreturn)) {
2337 ast_block_delete(block);
2343 static ast_expression* parse_statement_or_block(parser_t *parser)
2345 ast_expression *expr = NULL;
2346 if (parser->tok == '{')
2347 return (ast_expression*)parse_block(parser, false);
2348 if (!parse_statement(parser, NULL, &expr, false))
2353 static bool create_vector_members(ast_value *var, ast_member **me)
2356 size_t len = strlen(var->name);
2358 for (i = 0; i < 3; ++i) {
2359 char *name = mem_a(len+3);
2360 memcpy(name, var->name, len);
2362 name[len+1] = 'x'+i;
2364 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2373 do { ast_member_delete(me[--i]); } while(i);
2377 static bool parse_function_body(parser_t *parser, ast_value *var)
2379 ast_block *block = NULL;
2384 ast_expression *framenum = NULL;
2385 ast_expression *nextthink = NULL;
2386 /* None of the following have to be deleted */
2387 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2388 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2389 bool has_frame_think;
2393 has_frame_think = false;
2394 old = parser->function;
2396 if (var->expression.variadic) {
2397 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2398 "variadic function with implementation will not be able to access additional parameters"))
2404 if (parser->tok == '[') {
2405 /* got a frame definition: [ framenum, nextthink ]
2406 * this translates to:
2407 * self.frame = framenum;
2408 * self.nextthink = time + 0.1;
2409 * self.think = nextthink;
2413 fld_think = parser_find_field(parser, "think");
2414 fld_nextthink = parser_find_field(parser, "nextthink");
2415 fld_frame = parser_find_field(parser, "frame");
2416 if (!fld_think || !fld_nextthink || !fld_frame) {
2417 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2418 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2421 gbl_time = parser_find_global(parser, "time");
2422 gbl_self = parser_find_global(parser, "self");
2423 if (!gbl_time || !gbl_self) {
2424 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2425 parseerror(parser, "please declare the following globals: `time`, `self`");
2429 if (!parser_next(parser))
2432 framenum = parse_expression_leave(parser, true);
2434 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2437 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
2438 ast_unref(framenum);
2439 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2443 if (parser->tok != ',') {
2444 ast_unref(framenum);
2445 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2446 parseerror(parser, "Got a %i\n", parser->tok);
2450 if (!parser_next(parser)) {
2451 ast_unref(framenum);
2455 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2457 /* qc allows the use of not-yet-declared functions here
2458 * - this automatically creates a prototype */
2459 ast_value *thinkfunc;
2460 ast_expression *functype = fld_think->expression.next;
2462 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2463 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2464 ast_unref(framenum);
2465 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2469 if (!parser_next(parser)) {
2470 ast_unref(framenum);
2471 ast_delete(thinkfunc);
2475 vec_push(parser->globals, (ast_expression*)thinkfunc);
2476 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2477 nextthink = (ast_expression*)thinkfunc;
2480 nextthink = parse_expression_leave(parser, true);
2482 ast_unref(framenum);
2483 parseerror(parser, "expected a think-function in [frame,think] notation");
2488 if (!ast_istype(nextthink, ast_value)) {
2489 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2493 if (retval && parser->tok != ']') {
2494 parseerror(parser, "expected closing `]` for [frame,think] notation");
2498 if (retval && !parser_next(parser)) {
2502 if (retval && parser->tok != '{') {
2503 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2508 ast_unref(nextthink);
2509 ast_unref(framenum);
2513 has_frame_think = true;
2516 block = ast_block_new(parser_ctx(parser));
2518 parseerror(parser, "failed to allocate block");
2519 if (has_frame_think) {
2520 ast_unref(nextthink);
2521 ast_unref(framenum);
2526 if (has_frame_think) {
2528 ast_expression *self_frame;
2529 ast_expression *self_nextthink;
2530 ast_expression *self_think;
2531 ast_expression *time_plus_1;
2532 ast_store *store_frame;
2533 ast_store *store_nextthink;
2534 ast_store *store_think;
2536 ctx = parser_ctx(parser);
2537 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2538 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2539 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2541 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2542 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2544 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2545 if (self_frame) ast_delete(self_frame);
2546 if (self_nextthink) ast_delete(self_nextthink);
2547 if (self_think) ast_delete(self_think);
2548 if (time_plus_1) ast_delete(time_plus_1);
2554 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2555 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2556 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2559 ast_delete(self_frame);
2562 if (!store_nextthink) {
2563 ast_delete(self_nextthink);
2567 ast_delete(self_think);
2571 if (store_frame) ast_delete(store_frame);
2572 if (store_nextthink) ast_delete(store_nextthink);
2573 if (store_think) ast_delete(store_think);
2576 vec_push(block->exprs, (ast_expression*)store_frame);
2577 vec_push(block->exprs, (ast_expression*)store_nextthink);
2578 vec_push(block->exprs, (ast_expression*)store_think);
2582 parseerror(parser, "failed to generate code for [frame,think]");
2583 ast_unref(nextthink);
2584 ast_unref(framenum);
2590 parser_enterblock(parser);
2592 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2594 ast_value *param = var->expression.params[parami];
2597 if (param->expression.vtype != TYPE_VECTOR &&
2598 (param->expression.vtype != TYPE_FIELD ||
2599 param->expression.next->expression.vtype != TYPE_VECTOR))
2604 if (!create_vector_members(param, me)) {
2605 ast_block_delete(block);
2609 for (e = 0; e < 3; ++e) {
2610 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2611 ast_block_collect(block, (ast_expression*)me[e]);
2615 func = ast_function_new(ast_ctx(var), var->name, var);
2617 parseerror(parser, "failed to allocate function for `%s`", var->name);
2618 ast_block_delete(block);
2621 vec_push(parser->functions, func);
2623 parser->function = func;
2624 if (!parse_block_into(parser, block, true)) {
2625 ast_block_delete(block);
2629 vec_push(func->blocks, block);
2631 parser->function = old;
2632 if (!parser_leaveblock(parser))
2634 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2635 parseerror(parser, "internal error: local scopes left");
2639 if (parser->tok == ';')
2640 return parser_next(parser);
2641 else if (opts_standard == COMPILER_QCC)
2642 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2646 vec_pop(parser->functions);
2647 ast_function_delete(func);
2648 var->constval.vfunc = NULL;
2651 (void)!parser_leaveblock(parser);
2652 parser->function = old;
2656 static ast_expression *array_accessor_split(
2661 ast_expression *left,
2662 ast_expression *right
2668 lex_ctx ctx = ast_ctx(array);
2670 if (!left || !right) {
2671 if (left) ast_delete(left);
2672 if (right) ast_delete(right);
2676 cmp = ast_binary_new(ctx, INSTR_LT,
2677 (ast_expression*)index,
2678 (ast_expression*)parser_const_float(parser, middle));
2682 parseerror(parser, "internal error: failed to create comparison for array setter");
2686 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2688 ast_delete(cmp); /* will delete left and right */
2689 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2693 return (ast_expression*)ifthen;
2696 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2698 lex_ctx ctx = ast_ctx(array);
2700 if (from+1 == afterend) {
2701 /* set this value */
2704 ast_array_index *subscript;
2706 int assignop = type_store_instr[value->expression.vtype];
2708 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2709 assignop = INSTR_STORE_V;
2711 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2715 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2717 ast_delete(subscript);
2721 block = ast_block_new(ctx);
2727 vec_push(block->exprs, (ast_expression*)st);
2729 ret = ast_return_new(ctx, NULL);
2735 vec_push(block->exprs, (ast_expression*)ret);
2737 return (ast_expression*)block;
2739 ast_expression *left, *right;
2740 size_t diff = afterend - from;
2741 size_t middle = from + diff/2;
2742 left = array_setter_node(parser, array, index, value, from, middle);
2743 right = array_setter_node(parser, array, index, value, middle, afterend);
2744 return array_accessor_split(parser, array, index, middle, left, right);
2748 static ast_expression *array_field_setter_node(
2757 lex_ctx ctx = ast_ctx(array);
2759 if (from+1 == afterend) {
2760 /* set this value */
2763 ast_entfield *entfield;
2764 ast_array_index *subscript;
2766 int assignop = type_storep_instr[value->expression.vtype];
2768 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2769 assignop = INSTR_STOREP_V;
2771 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2775 entfield = ast_entfield_new_force(ctx,
2776 (ast_expression*)entity,
2777 (ast_expression*)subscript,
2778 (ast_expression*)subscript);
2780 ast_delete(subscript);
2784 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2786 ast_delete(entfield);
2790 block = ast_block_new(ctx);
2796 vec_push(block->exprs, (ast_expression*)st);
2798 ret = ast_return_new(ctx, NULL);
2804 vec_push(block->exprs, (ast_expression*)ret);
2806 return (ast_expression*)block;
2808 ast_expression *left, *right;
2809 size_t diff = afterend - from;
2810 size_t middle = from + diff/2;
2811 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2812 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2813 return array_accessor_split(parser, array, index, middle, left, right);
2817 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2819 lex_ctx ctx = ast_ctx(array);
2821 if (from+1 == afterend) {
2823 ast_array_index *subscript;
2825 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2829 ret = ast_return_new(ctx, (ast_expression*)subscript);
2831 ast_delete(subscript);
2835 return (ast_expression*)ret;
2837 ast_expression *left, *right;
2838 size_t diff = afterend - from;
2839 size_t middle = from + diff/2;
2840 left = array_getter_node(parser, array, index, from, middle);
2841 right = array_getter_node(parser, array, index, middle, afterend);
2842 return array_accessor_split(parser, array, index, middle, left, right);
2846 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2848 ast_function *func = NULL;
2849 ast_value *fval = NULL;
2850 ast_block *body = NULL;
2852 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2854 parseerror(parser, "failed to create accessor function value");
2858 func = ast_function_new(ast_ctx(array), funcname, fval);
2861 parseerror(parser, "failed to create accessor function node");
2865 body = ast_block_new(ast_ctx(array));
2867 parseerror(parser, "failed to create block for array accessor");
2873 vec_push(func->blocks, body);
2876 vec_push(parser->accessors, fval);
2881 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2883 ast_expression *root = NULL;
2884 ast_value *index = NULL;
2885 ast_value *value = NULL;
2889 if (!ast_istype(array->expression.next, ast_value)) {
2890 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2894 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2896 func = fval->constval.vfunc;
2897 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2899 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2900 value = ast_value_copy((ast_value*)array->expression.next);
2902 if (!index || !value) {
2903 parseerror(parser, "failed to create locals for array accessor");
2906 (void)!ast_value_set_name(value, "value"); /* not important */
2907 vec_push(fval->expression.params, index);
2908 vec_push(fval->expression.params, value);
2910 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2912 parseerror(parser, "failed to build accessor search tree");
2916 vec_push(func->blocks[0]->exprs, root);
2917 array->setter = fval;
2920 if (index) ast_delete(index);
2921 if (value) ast_delete(value);
2922 if (root) ast_delete(root);
2928 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2930 ast_expression *root = NULL;
2931 ast_value *entity = NULL;
2932 ast_value *index = NULL;
2933 ast_value *value = NULL;
2937 if (!ast_istype(array->expression.next, ast_value)) {
2938 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2942 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2944 func = fval->constval.vfunc;
2945 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2947 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2948 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2949 value = ast_value_copy((ast_value*)array->expression.next);
2950 if (!entity || !index || !value) {
2951 parseerror(parser, "failed to create locals for array accessor");
2954 (void)!ast_value_set_name(value, "value"); /* not important */
2955 vec_push(fval->expression.params, entity);
2956 vec_push(fval->expression.params, index);
2957 vec_push(fval->expression.params, value);
2959 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2961 parseerror(parser, "failed to build accessor search tree");
2965 vec_push(func->blocks[0]->exprs, root);
2966 array->setter = fval;
2969 if (entity) ast_delete(entity);
2970 if (index) ast_delete(index);
2971 if (value) ast_delete(value);
2972 if (root) ast_delete(root);
2978 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2980 ast_expression *root = NULL;
2981 ast_value *index = NULL;
2985 /* NOTE: checking array->expression.next rather than elemtype since
2986 * for fields elemtype is a temporary fieldtype.
2988 if (!ast_istype(array->expression.next, ast_value)) {
2989 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2993 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2995 func = fval->constval.vfunc;
2996 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2998 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3001 parseerror(parser, "failed to create locals for array accessor");
3004 vec_push(fval->expression.params, index);
3006 root = array_getter_node(parser, array, index, 0, array->expression.count);
3008 parseerror(parser, "failed to build accessor search tree");
3012 vec_push(func->blocks[0]->exprs, root);
3013 array->getter = fval;
3016 if (index) ast_delete(index);
3017 if (root) ast_delete(root);
3023 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3024 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3032 bool variadic = false;
3034 ctx = parser_ctx(parser);
3036 /* for the sake of less code we parse-in in this function */
3037 if (!parser_next(parser)) {
3038 parseerror(parser, "expected parameter list");
3044 /* parse variables until we hit a closing paren */
3045 while (parser->tok != ')') {
3047 /* there must be commas between them */
3048 if (parser->tok != ',') {
3049 parseerror(parser, "expected comma or end of parameter list");
3052 if (!parser_next(parser)) {
3053 parseerror(parser, "expected parameter");
3059 if (parser->tok == TOKEN_DOTS) {
3060 /* '...' indicates a varargs function */
3062 if (!parser_next(parser)) {
3063 parseerror(parser, "expected parameter");
3066 if (parser->tok != ')') {
3067 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3073 /* for anything else just parse a typename */
3074 param = parse_typename(parser, NULL, NULL);
3077 vec_push(params, param);
3078 if (param->expression.vtype >= TYPE_VARIANT) {
3079 char typename[1024];
3080 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3081 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3088 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3089 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3092 if (!parser_next(parser)) {
3093 parseerror(parser, "parse error after typename");
3097 /* now turn 'var' into a function type */
3098 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3099 fval->expression.next = (ast_expression*)var;
3100 fval->expression.variadic = variadic;
3103 var->expression.params = params;
3110 for (i = 0; i < vec_size(params); ++i)
3111 ast_delete(params[i]);
3116 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3118 ast_expression *cexp;
3119 ast_value *cval, *tmp;
3122 ctx = parser_ctx(parser);
3124 if (!parser_next(parser)) {
3126 parseerror(parser, "expected array-size");
3130 cexp = parse_expression_leave(parser, true);
3132 if (!cexp || !ast_istype(cexp, ast_value)) {
3136 parseerror(parser, "expected array-size as constant positive integer");
3139 cval = (ast_value*)cexp;
3141 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3142 tmp->expression.next = (ast_expression*)var;
3145 if (cval->expression.vtype == TYPE_INTEGER)
3146 tmp->expression.count = cval->constval.vint;
3147 else if (cval->expression.vtype == TYPE_FLOAT)
3148 tmp->expression.count = cval->constval.vfloat;
3152 parseerror(parser, "array-size must be a positive integer constant");
3157 if (parser->tok != ']') {
3159 parseerror(parser, "expected ']' after array-size");
3162 if (!parser_next(parser)) {
3164 parseerror(parser, "error after parsing array size");
3170 /* Parse a complete typename.
3171 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3172 * but when parsing variables separated by comma
3173 * 'storebase' should point to where the base-type should be kept.
3174 * The base type makes up every bit of type information which comes *before* the
3177 * The following will be parsed in its entirety:
3179 * The 'basetype' in this case is 'void()'
3180 * and if there's a comma after it, say:
3182 * then the type-information 'void()' can be stored in 'storebase'
3184 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3186 ast_value *var, *tmp;
3189 const char *name = NULL;
3190 bool isfield = false;
3191 bool wasarray = false;
3193 ctx = parser_ctx(parser);
3195 /* types may start with a dot */
3196 if (parser->tok == '.') {
3198 /* if we parsed a dot we need a typename now */
3199 if (!parser_next(parser)) {
3200 parseerror(parser, "expected typename for field definition");
3203 if (parser->tok == TOKEN_IDENT)
3204 cached_typedef = parser_find_typedef(parser, parser_tokval(parser));
3205 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3206 parseerror(parser, "expected typename");
3211 /* generate the basic type value */
3212 if (cached_typedef) {
3213 var = ast_value_copy(cached_typedef);
3214 ast_value_set_name(var, "<type(from_def)>");
3216 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3217 /* do not yet turn into a field - remember:
3218 * .void() foo; is a field too
3219 * .void()() foo; is a function
3223 if (!parser_next(parser)) {
3225 parseerror(parser, "parse error after typename");
3229 /* an opening paren now starts the parameter-list of a function
3230 * this is where original-QC has parameter lists.
3231 * We allow a single parameter list here.
3232 * Much like fteqcc we don't allow `float()() x`
3234 if (parser->tok == '(') {
3235 var = parse_parameter_list(parser, var);
3240 /* store the base if requested */
3242 *storebase = ast_value_copy(var);
3244 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3245 tmp->expression.next = (ast_expression*)*storebase;
3250 /* there may be a name now */
3251 if (parser->tok == TOKEN_IDENT) {
3252 name = util_strdup(parser_tokval(parser));
3254 if (!parser_next(parser)) {
3256 parseerror(parser, "error after variable or field declaration");
3261 /* now this may be an array */
3262 if (parser->tok == '[') {
3264 var = parse_arraysize(parser, var);
3269 /* This is the point where we can turn it into a field */
3271 /* turn it into a field if desired */
3272 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3273 tmp->expression.next = (ast_expression*)var;
3277 /* now there may be function parens again */
3278 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3279 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3280 if (parser->tok == '(' && wasarray)
3281 parseerror(parser, "arrays as part of a return type is not supported");
3282 while (parser->tok == '(') {
3283 var = parse_parameter_list(parser, var);
3292 /* finally name it */
3294 if (!ast_value_set_name(var, name)) {
3296 parseerror(parser, "internal error: failed to set name");
3299 /* free the name, ast_value_set_name duplicates */
3306 static bool parse_typedef(parser_t *parser)
3308 ast_value *typevar, *oldtype;
3309 ast_expression *old;
3311 typevar = parse_typename(parser, NULL, NULL);
3316 if ( (old = parser_find_var(parser, typevar->name)) ) {
3317 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3318 " -> `%s` has been declared here: %s:%i",
3319 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3320 ast_delete(typevar);
3324 if ( (oldtype = parser_find_typedef(parser, typevar->name)) ) {
3325 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3326 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3327 ast_delete(typevar);
3331 vec_push(parser->_typedefs, typevar);
3332 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3334 if (parser->tok != ';') {
3335 parseerror(parser, "expected semicolon after typedef");
3338 if (!parser_next(parser)) {
3339 parseerror(parser, "parse error after typedef");
3346 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, bool is_const, ast_value *cached_typedef)
3350 ast_expression *old;
3354 ast_value *basetype = NULL;
3356 bool isparam = false;
3357 bool isvector = false;
3358 bool cleanvar = true;
3359 bool wasarray = false;
3363 /* get the first complete variable */
3364 var = parse_typename(parser, &basetype, cached_typedef);
3367 ast_delete(basetype);
3375 /* Part 0: finish the type */
3376 if (parser->tok == '(') {
3377 if (opts_standard == COMPILER_QCC)
3378 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3379 var = parse_parameter_list(parser, var);
3385 /* we only allow 1-dimensional arrays */
3386 if (parser->tok == '[') {
3388 var = parse_arraysize(parser, var);
3394 if (parser->tok == '(' && wasarray) {
3395 parseerror(parser, "arrays as part of a return type is not supported");
3396 /* we'll still parse the type completely for now */
3398 /* for functions returning functions */
3399 while (parser->tok == '(') {
3400 if (opts_standard == COMPILER_QCC)
3401 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3402 var = parse_parameter_list(parser, var);
3410 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3411 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3412 * is then filled with the previous definition and the parameter-names replaced.
3415 /* Deal with end_sys_ vars */
3417 if (!strcmp(var->name, "end_sys_globals")) {
3418 parser->crc_globals = vec_size(parser->globals);
3421 else if (!strcmp(var->name, "end_sys_fields")) {
3422 parser->crc_fields = vec_size(parser->fields);
3425 if (was_end && var->expression.vtype == TYPE_FIELD) {
3426 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3427 "global '%s' hint should not be a field",
3428 parser_tokval(parser)))
3435 if (!nofields && var->expression.vtype == TYPE_FIELD)
3437 /* deal with field declarations */
3438 old = parser_find_field(parser, var->name);
3440 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3441 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3450 parseerror(parser, "field `%s` already declared here: %s:%i",
3451 var->name, ast_ctx(old).file, ast_ctx(old).line);
3456 if (opts_standard == COMPILER_QCC &&
3457 (old = parser_find_global(parser, var->name)))
3459 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3460 parseerror(parser, "field `%s` already declared here: %s:%i",
3461 var->name, ast_ctx(old).file, ast_ctx(old).line);
3468 /* deal with other globals */
3469 old = parser_find_global(parser, var->name);
3470 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3472 /* This is a function which had a prototype */
3473 if (!ast_istype(old, ast_value)) {
3474 parseerror(parser, "internal error: prototype is not an ast_value");
3478 proto = (ast_value*)old;
3479 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3480 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3482 ast_ctx(proto).file, ast_ctx(proto).line);
3486 /* we need the new parameter-names */
3487 for (i = 0; i < vec_size(proto->expression.params); ++i)
3488 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3496 if (opts_standard == COMPILER_GMQCC) {
3497 parseerror(parser, "global `%s` already declared here: %s:%i",
3498 var->name, ast_ctx(old).file, ast_ctx(old).line);
3502 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3503 "global `%s` already declared here: %s:%i",
3504 var->name, ast_ctx(old).file, ast_ctx(old).line))
3511 if (opts_standard == COMPILER_QCC &&
3512 (old = parser_find_field(parser, var->name)))
3514 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3515 parseerror(parser, "global `%s` already declared here: %s:%i",
3516 var->name, ast_ctx(old).file, ast_ctx(old).line);
3523 else /* it's not a global */
3525 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3526 if (old && !isparam) {
3527 parseerror(parser, "local `%s` already declared here: %s:%i",
3528 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3532 old = parser_find_local(parser, var->name, 0, &isparam);
3533 if (old && isparam) {
3534 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3535 "local `%s` is shadowing a parameter", var->name))
3537 parseerror(parser, "local `%s` already declared here: %s:%i",
3538 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3542 if (opts_standard != COMPILER_GMQCC) {
3551 var->isconst = true;
3554 * Create the global/local, and deal with vector types.
3557 if (var->expression.vtype == TYPE_VECTOR)
3559 else if (var->expression.vtype == TYPE_FIELD &&
3560 var->expression.next->expression.vtype == TYPE_VECTOR)
3564 if (!create_vector_members(var, me)) {
3571 /* deal with global variables, fields, functions */
3572 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3573 vec_push(parser->fields, (ast_expression*)var);
3574 util_htset(parser->htfields, var->name, var);
3576 for (i = 0; i < 3; ++i) {
3577 vec_push(parser->fields, (ast_expression*)me[i]);
3578 util_htset(parser->htfields, me[i]->name, me[i]);
3583 vec_push(parser->globals, (ast_expression*)var);
3584 util_htset(parser->htglobals, var->name, var);
3586 for (i = 0; i < 3; ++i) {
3587 vec_push(parser->globals, (ast_expression*)me[i]);
3588 util_htset(parser->htglobals, me[i]->name, me[i]);
3593 vec_push(localblock->locals, var);
3594 parser_addlocal(parser, var->name, (ast_expression*)var);
3596 for (i = 0; i < 3; ++i) {
3597 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3598 ast_block_collect(localblock, (ast_expression*)me[i]);
3603 me[0] = me[1] = me[2] = NULL;
3609 if (var->expression.vtype == TYPE_ARRAY) {
3611 snprintf(name, sizeof(name), "%s##SET", var->name);
3612 if (!parser_create_array_setter(parser, var, name))
3614 snprintf(name, sizeof(name), "%s##GET", var->name);
3615 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3618 else if (!localblock && !nofields &&
3619 var->expression.vtype == TYPE_FIELD &&
3620 var->expression.next->expression.vtype == TYPE_ARRAY)
3623 ast_expression *telem;
3625 ast_value *array = (ast_value*)var->expression.next;
3627 if (!ast_istype(var->expression.next, ast_value)) {
3628 parseerror(parser, "internal error: field element type must be an ast_value");
3632 snprintf(name, sizeof(name), "%s##SETF", var->name);
3633 if (!parser_create_array_field_setter(parser, array, name))
3636 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3637 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3638 tfield->expression.next = telem;
3639 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3640 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3648 if (parser->tok == ';') {
3649 ast_delete(basetype);
3650 if (!parser_next(parser)) {
3651 parseerror(parser, "error after variable declaration");
3657 if (parser->tok == ',')
3660 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3661 parseerror(parser, "missing comma or semicolon while parsing variables");
3665 if (localblock && opts_standard == COMPILER_QCC) {
3666 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3667 "initializing expression turns variable `%s` into a constant in this standard",
3674 if (parser->tok != '{') {
3675 if (parser->tok != '=') {
3676 parseerror(parser, "missing semicolon or initializer");
3680 if (!parser_next(parser)) {
3681 parseerror(parser, "error parsing initializer");
3685 else if (opts_standard == COMPILER_QCC) {
3686 parseerror(parser, "expected '=' before function body in this standard");
3689 if (parser->tok == '#') {
3690 ast_function *func = NULL;
3693 parseerror(parser, "cannot declare builtins within functions");
3696 if (var->expression.vtype != TYPE_FUNCTION) {
3697 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3700 if (!parser_next(parser)) {
3701 parseerror(parser, "expected builtin number");
3704 if (parser->tok != TOKEN_INTCONST) {
3705 parseerror(parser, "builtin number must be an integer constant");
3708 if (parser_token(parser)->constval.i <= 0) {
3709 parseerror(parser, "builtin number must be an integer greater than zero");
3714 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
3715 "builtin `%s` has already been defined\n"
3716 " -> previous declaration here: %s:%i",
3717 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
3721 func = ast_function_new(ast_ctx(var), var->name, var);
3723 parseerror(parser, "failed to allocate function for `%s`", var->name);
3726 vec_push(parser->functions, func);
3728 func->builtin = -parser_token(parser)->constval.i;
3731 if (!parser_next(parser)) {
3732 parseerror(parser, "expected comma or semicolon");
3734 ast_function_delete(func);
3735 var->constval.vfunc = NULL;
3739 else if (parser->tok == '{' || parser->tok == '[')
3742 parseerror(parser, "cannot declare functions within functions");
3746 if (!parse_function_body(parser, var))
3748 ast_delete(basetype);
3751 ast_expression *cexp;
3754 cexp = parse_expression_leave(parser, true);
3759 cval = (ast_value*)cexp;
3760 if (!ast_istype(cval, ast_value) || !cval->isconst)
3761 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3764 var->isconst = true;
3765 if (cval->expression.vtype == TYPE_STRING)
3766 var->constval.vstring = parser_strdup(cval->constval.vstring);
3768 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3772 shunt sy = { NULL, NULL };
3773 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
3774 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
3775 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
3776 if (!parser_sy_pop(parser, &sy))
3779 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
3780 parseerror(parser, "internal error: leaked operands");
3781 vec_push(localblock->exprs, (ast_expression*)sy.out[0].out);
3789 if (parser->tok == ',') {
3790 if (!parser_next(parser)) {
3791 parseerror(parser, "expected another variable");
3795 if (parser->tok != TOKEN_IDENT) {
3796 parseerror(parser, "expected another variable");
3799 var = ast_value_copy(basetype);
3801 ast_value_set_name(var, parser_tokval(parser));
3802 if (!parser_next(parser)) {
3803 parseerror(parser, "error parsing variable declaration");
3809 if (parser->tok != ';') {
3810 parseerror(parser, "missing semicolon after variables");
3814 if (!parser_next(parser)) {
3815 parseerror(parser, "parse error after variable declaration");
3819 ast_delete(basetype);
3823 if (cleanvar && var)
3825 ast_delete(basetype);
3829 ast_delete(basetype);
3830 if (cleanvar && var)
3832 if (me[0]) ast_member_delete(me[0]);
3833 if (me[1]) ast_member_delete(me[1]);
3834 if (me[2]) ast_member_delete(me[2]);
3838 static bool parser_global_statement(parser_t *parser)
3840 ast_value *istype = NULL;
3841 if (parser->tok == TOKEN_IDENT)
3842 istype = parser_find_typedef(parser, parser_tokval(parser));
3844 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3846 return parse_variable(parser, NULL, false, false, istype);
3848 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
3850 if (!strcmp(parser_tokval(parser), "var")) {
3851 if (!parser_next(parser)) {
3852 parseerror(parser, "expected variable declaration after 'var'");
3855 return parse_variable(parser, NULL, true, false, NULL);
3858 else if (parser->tok == TOKEN_KEYWORD)
3860 if (!strcmp(parser_tokval(parser), "const")) {
3861 if (!parser_next(parser)) {
3862 parseerror(parser, "expected variable declaration after 'const'");
3865 return parse_variable(parser, NULL, true, true, NULL);
3867 else if (!strcmp(parser_tokval(parser), "typedef")) {
3868 if (!parser_next(parser)) {
3869 parseerror(parser, "expected type definition after 'typedef'");
3872 return parse_typedef(parser);
3874 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
3877 else if (parser->tok == '$')
3879 if (!parser_next(parser)) {
3880 parseerror(parser, "parse error");
3886 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3892 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3894 return util_crc16(old, str, strlen(str));
3897 static void progdefs_crc_file(const char *str)
3899 /* write to progdefs.h here */
3903 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3905 old = progdefs_crc_sum(old, str);
3906 progdefs_crc_file(str);
3910 static void generate_checksum(parser_t *parser)
3912 uint16_t crc = 0xFFFF;
3916 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3917 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3919 progdefs_crc_file("\tint\tpad;\n");
3920 progdefs_crc_file("\tint\tofs_return[3];\n");
3921 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3922 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3923 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3924 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3925 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3926 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3927 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3928 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3930 for (i = 0; i < parser->crc_globals; ++i) {
3931 if (!ast_istype(parser->globals[i], ast_value))
3933 value = (ast_value*)(parser->globals[i]);
3934 switch (value->expression.vtype) {
3935 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3936 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3937 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3938 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3940 crc = progdefs_crc_both(crc, "\tint\t");
3943 crc = progdefs_crc_both(crc, value->name);
3944 crc = progdefs_crc_both(crc, ";\n");
3946 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3947 for (i = 0; i < parser->crc_fields; ++i) {
3948 if (!ast_istype(parser->fields[i], ast_value))
3950 value = (ast_value*)(parser->fields[i]);
3951 switch (value->expression.next->expression.vtype) {
3952 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3953 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3954 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3955 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3957 crc = progdefs_crc_both(crc, "\tint\t");
3960 crc = progdefs_crc_both(crc, value->name);
3961 crc = progdefs_crc_both(crc, ";\n");
3963 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3968 static parser_t *parser;
3974 parser = (parser_t*)mem_a(sizeof(parser_t));
3978 memset(parser, 0, sizeof(*parser));
3980 for (i = 0; i < operator_count; ++i) {
3981 if (operators[i].id == opid1('=')) {
3982 parser->assign_op = operators+i;
3986 if (!parser->assign_op) {
3987 printf("internal error: initializing parser: failed to find assign operator\n");
3992 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
3993 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
3994 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
3998 bool parser_compile()
4000 /* initial lexer/parser state */
4001 parser->lex->flags.noops = true;
4003 if (parser_next(parser))
4005 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4007 if (!parser_global_statement(parser)) {
4008 if (parser->tok == TOKEN_EOF)
4009 parseerror(parser, "unexpected eof");
4010 else if (!parser->errors)
4011 parseerror(parser, "there have been errors, bailing out");
4012 lex_close(parser->lex);
4018 parseerror(parser, "parse error");
4019 lex_close(parser->lex);
4024 lex_close(parser->lex);
4027 return !parser->errors;
4030 bool parser_compile_file(const char *filename)
4032 parser->lex = lex_open(filename);
4034 con_err("failed to open file \"%s\"\n", filename);
4037 return parser_compile();
4040 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4042 parser->lex = lex_open_string(str, len, name);
4044 con_err("failed to create lexer for string \"%s\"\n", name);
4047 return parser_compile();
4050 bool parser_compile_string(const char *name, const char *str)
4052 parser->lex = lex_open_string(str, strlen(str), name);
4054 con_err("failed to create lexer for string \"%s\"\n", name);
4057 return parser_compile();
4060 void parser_cleanup()
4063 for (i = 0; i < vec_size(parser->accessors); ++i) {
4064 ast_delete(parser->accessors[i]->constval.vfunc);
4065 parser->accessors[i]->constval.vfunc = NULL;
4066 ast_delete(parser->accessors[i]);
4068 for (i = 0; i < vec_size(parser->functions); ++i) {
4069 ast_delete(parser->functions[i]);
4071 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4072 ast_delete(parser->imm_vector[i]);
4074 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4075 ast_delete(parser->imm_string[i]);
4077 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4078 ast_delete(parser->imm_float[i]);
4080 for (i = 0; i < vec_size(parser->fields); ++i) {
4081 ast_delete(parser->fields[i]);
4083 for (i = 0; i < vec_size(parser->globals); ++i) {
4084 ast_delete(parser->globals[i]);
4086 vec_free(parser->accessors);
4087 vec_free(parser->functions);
4088 vec_free(parser->imm_vector);
4089 vec_free(parser->imm_string);
4090 vec_free(parser->imm_float);
4091 vec_free(parser->globals);
4092 vec_free(parser->fields);
4094 for (i = 0; i < vec_size(parser->variables); ++i)
4095 util_htdel(parser->variables[i]);
4096 vec_free(parser->variables);
4097 vec_free(parser->_blocklocals);
4099 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4100 ast_delete(parser->_typedefs[i]);
4101 vec_free(parser->_typedefs);
4102 for (i = 0; i < vec_size(parser->typedefs); ++i)
4103 util_htdel(parser->typedefs[i]);
4104 vec_free(parser->typedefs);
4105 vec_free(parser->_blocktypedefs);
4110 bool parser_finish(const char *output)
4116 if (!parser->errors)
4118 ir = ir_builder_new("gmqcc_out");
4120 con_out("failed to allocate builder\n");
4124 for (i = 0; i < vec_size(parser->fields); ++i) {
4127 if (!ast_istype(parser->fields[i], ast_value))
4129 field = (ast_value*)parser->fields[i];
4130 isconst = field->isconst;
4131 field->isconst = false;
4132 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4133 con_out("failed to generate field %s\n", field->name);
4134 ir_builder_delete(ir);
4139 ast_expression *subtype;
4140 field->isconst = true;
4141 subtype = field->expression.next;
4142 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4143 if (subtype->expression.vtype == TYPE_FIELD)
4144 ifld->fieldtype = subtype->expression.next->expression.vtype;
4145 else if (subtype->expression.vtype == TYPE_FUNCTION)
4146 ifld->outtype = subtype->expression.next->expression.vtype;
4147 (void)!ir_value_set_field(field->ir_v, ifld);
4150 for (i = 0; i < vec_size(parser->globals); ++i) {
4152 if (!ast_istype(parser->globals[i], ast_value))
4154 asvalue = (ast_value*)(parser->globals[i]);
4155 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
4156 if (strcmp(asvalue->name, "end_sys_globals") &&
4157 strcmp(asvalue->name, "end_sys_fields"))
4159 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4160 "unused global: `%s`", asvalue->name);
4163 if (!ast_global_codegen(asvalue, ir, false)) {
4164 con_out("failed to generate global %s\n", asvalue->name);
4165 ir_builder_delete(ir);
4169 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4170 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4171 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4172 ir_builder_delete(ir);
4176 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4177 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4178 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4179 ir_builder_delete(ir);
4183 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4184 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4185 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4186 ir_builder_delete(ir);
4190 for (i = 0; i < vec_size(parser->globals); ++i) {
4192 if (!ast_istype(parser->globals[i], ast_value))
4194 asvalue = (ast_value*)(parser->globals[i]);
4195 if (asvalue->setter) {
4196 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4197 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4198 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4200 printf("failed to generate setter for %s\n", asvalue->name);
4201 ir_builder_delete(ir);
4205 if (asvalue->getter) {
4206 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4207 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4208 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4210 printf("failed to generate getter for %s\n", asvalue->name);
4211 ir_builder_delete(ir);
4216 for (i = 0; i < vec_size(parser->fields); ++i) {
4218 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4220 if (!ast_istype((ast_expression*)asvalue, ast_value))
4222 if (asvalue->expression.vtype != TYPE_ARRAY)
4224 if (asvalue->setter) {
4225 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4226 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4227 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4229 printf("failed to generate setter for %s\n", asvalue->name);
4230 ir_builder_delete(ir);
4234 if (asvalue->getter) {
4235 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4236 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4237 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4239 printf("failed to generate getter for %s\n", asvalue->name);
4240 ir_builder_delete(ir);
4245 for (i = 0; i < vec_size(parser->functions); ++i) {
4246 if (!ast_function_codegen(parser->functions[i], ir)) {
4247 con_out("failed to generate function %s\n", parser->functions[i]->name);
4248 ir_builder_delete(ir);
4253 ir_builder_dump(ir, con_out);
4254 for (i = 0; i < vec_size(parser->functions); ++i) {
4255 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4256 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4257 ir_builder_delete(ir);
4264 ir_builder_dump(ir, con_out);
4266 generate_checksum(parser);
4268 if (!ir_builder_generate(ir, output)) {
4269 con_out("*** failed to generate output file\n");
4270 ir_builder_delete(ir);
4275 ir_builder_delete(ir);
4279 con_out("*** there were compile errors\n");