5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define PARSER_HT_FIELDS 0
30 #define PARSER_HT_GLOBALS 1
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 1024
35 #define TYPEDEF_HT_SIZE 16
41 ast_expression **globals;
42 ast_expression **fields;
43 ast_function **functions;
44 ast_value **imm_float;
45 ast_value **imm_string;
46 ast_value **imm_vector;
49 /* must be deleted first, they reference immediates and values */
50 ast_value **accessors;
52 ast_value *imm_float_zero;
53 ast_value *imm_float_one;
54 ast_value *imm_vector_zero;
59 ast_function *function;
61 /* All the labels the function defined...
62 * Should they be in ast_function instead?
67 /* A list of hashtables for each scope */
73 /* not to be used directly, we use the hash table */
74 ast_expression **_locals;
76 ast_value **_typedefs;
77 size_t *_blocktypedefs;
81 /* we store the '=' operator info */
82 const oper_info *assign_op;
84 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
85 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
86 * anything else: type error
95 static void parser_enterblock(parser_t *parser);
96 static bool parser_leaveblock(parser_t *parser);
97 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
98 static bool parse_typedef(parser_t *parser);
99 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef);
100 static ast_block* parse_block(parser_t *parser, bool warnreturn);
101 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
102 static ast_expression* parse_statement_or_block(parser_t *parser);
103 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
104 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
105 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
107 static void parseerror(parser_t *parser, const char *fmt, ...)
114 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
118 /* returns true if it counts as an error */
119 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
122 int lvl = LVL_WARNING;
124 if (!OPTS_WARN(warntype))
133 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
139 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
142 int lvl = LVL_WARNING;
144 if (!OPTS_WARN(warntype))
151 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
157 /**********************************************************************
158 * some maths used for constant folding
161 vector vec3_add(vector a, vector b)
170 vector vec3_sub(vector a, vector b)
179 qcfloat vec3_mulvv(vector a, vector b)
181 return (a.x * b.x + a.y * b.y + a.z * b.z);
184 vector vec3_mulvf(vector a, float b)
193 /**********************************************************************
197 bool parser_next(parser_t *parser)
199 /* lex_do kills the previous token */
200 parser->tok = lex_do(parser->lex);
201 if (parser->tok == TOKEN_EOF)
203 if (parser->tok >= TOKEN_ERROR) {
204 parseerror(parser, "lex error");
210 #define parser_tokval(p) ((p)->lex->tok.value)
211 #define parser_token(p) (&((p)->lex->tok))
212 #define parser_ctx(p) ((p)->lex->tok.ctx)
214 static ast_value* parser_const_float(parser_t *parser, double d)
218 for (i = 0; i < vec_size(parser->imm_float); ++i) {
219 const double compare = parser->imm_float[i]->constval.vfloat;
220 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
221 return parser->imm_float[i];
223 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
224 out->constant = true;
225 out->hasvalue = true;
226 out->constval.vfloat = d;
227 vec_push(parser->imm_float, out);
231 static ast_value* parser_const_float_0(parser_t *parser)
233 if (!parser->imm_float_zero)
234 parser->imm_float_zero = parser_const_float(parser, 0);
235 return parser->imm_float_zero;
238 static ast_value* parser_const_float_1(parser_t *parser)
240 if (!parser->imm_float_one)
241 parser->imm_float_one = parser_const_float(parser, 1);
242 return parser->imm_float_one;
245 static char *parser_strdup(const char *str)
248 /* actually dup empty strings */
249 char *out = mem_a(1);
253 return util_strdup(str);
256 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
260 for (i = 0; i < vec_size(parser->imm_string); ++i) {
261 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
262 return parser->imm_string[i];
266 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
267 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
269 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
270 out->constant = true;
271 out->hasvalue = true;
272 out->constval.vstring = parser_strdup(str);
273 vec_push(parser->imm_string, out);
277 static ast_value* parser_const_vector(parser_t *parser, vector v)
281 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
282 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
283 return parser->imm_vector[i];
285 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
286 out->constant = true;
287 out->hasvalue = true;
288 out->constval.vvec = v;
289 vec_push(parser->imm_vector, out);
293 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
299 return parser_const_vector(parser, v);
302 static ast_value* parser_const_vector_0(parser_t *parser)
304 if (!parser->imm_vector_zero)
305 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
306 return parser->imm_vector_zero;
309 static ast_expression* parser_find_field(parser_t *parser, const char *name)
311 return util_htget(parser->htfields, name);
314 static ast_expression* parser_find_global(parser_t *parser, const char *name)
316 return util_htget(parser->htglobals, name);
319 static ast_expression* parser_find_param(parser_t *parser, const char *name)
323 if (!parser->function)
325 fun = parser->function->vtype;
326 for (i = 0; i < vec_size(fun->expression.params); ++i) {
327 if (!strcmp(fun->expression.params[i]->name, name))
328 return (ast_expression*)(fun->expression.params[i]);
333 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
338 hash = util_hthash(parser->htglobals, name);
341 for (i = vec_size(parser->variables); i > upto;) {
343 if ( (e = util_htgeth(parser->variables[i], name, hash)) )
347 return parser_find_param(parser, name);
350 static ast_expression* parser_find_var(parser_t *parser, const char *name)
354 v = parser_find_local(parser, name, 0, &dummy);
355 if (!v) v = parser_find_global(parser, name);
359 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
363 hash = util_hthash(parser->typedefs[0], name);
365 for (i = vec_size(parser->typedefs); i > upto;) {
367 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
375 size_t etype; /* 0 = expression, others are operators */
379 ast_block *block; /* for commas and function calls */
388 #define SY_PAREN_EXPR '('
389 #define SY_PAREN_FUNC 'f'
390 #define SY_PAREN_INDEX '['
392 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
403 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
407 e.out = (ast_expression*)v;
414 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
416 e.etype = 1 + (op - operators);
425 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
437 # define DEBUGSHUNTDO(x) x
439 # define DEBUGSHUNTDO(x)
442 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
443 * so we need to rotate it to become ent.(foo[n]).
445 static bool rotate_entfield_array_index_nodes(ast_expression **out)
447 ast_array_index *index;
448 ast_entfield *entfield;
452 ast_expression *entity;
454 lex_ctx ctx = ast_ctx(*out);
456 if (!ast_istype(*out, ast_array_index))
458 index = (ast_array_index*)*out;
460 if (!ast_istype(index->array, ast_entfield))
462 entfield = (ast_entfield*)index->array;
464 if (!ast_istype(entfield->field, ast_value))
466 field = (ast_value*)entfield->field;
469 entity = entfield->entity;
473 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
474 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
475 *out = (ast_expression*)entfield;
480 static bool parser_sy_pop(parser_t *parser, shunt *sy)
484 ast_expression *out = NULL;
485 ast_expression *exprs[3];
486 ast_block *blocks[3];
487 ast_value *asvalue[3];
488 size_t i, assignop, addop, subop;
489 qcint generated_op = 0;
494 if (!vec_size(sy->ops)) {
495 parseerror(parser, "internal error: missing operator");
499 if (vec_last(sy->ops).paren) {
500 parseerror(parser, "unmatched parenthesis");
504 op = &operators[vec_last(sy->ops).etype - 1];
505 ctx = vec_last(sy->ops).ctx;
507 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
509 if (vec_size(sy->out) < op->operands) {
510 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
511 op->op, (int)op->id);
515 vec_shrinkby(sy->ops, 1);
517 vec_shrinkby(sy->out, op->operands);
518 for (i = 0; i < op->operands; ++i) {
519 exprs[i] = sy->out[vec_size(sy->out)+i].out;
520 blocks[i] = sy->out[vec_size(sy->out)+i].block;
521 asvalue[i] = (ast_value*)exprs[i];
524 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
525 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
529 #define NotSameType(T) \
530 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
531 exprs[0]->expression.vtype != T)
532 #define CanConstFold1(A) \
533 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && ((ast_value*)(A))->constant)
534 #define CanConstFold(A, B) \
535 (CanConstFold1(A) && CanConstFold1(B))
536 #define ConstV(i) (asvalue[(i)]->constval.vvec)
537 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
538 #define ConstS(i) (asvalue[(i)]->constval.vstring)
542 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
546 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
547 if (exprs[1]->expression.vtype != TYPE_FIELD) {
548 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
551 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
553 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
554 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
558 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
564 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
565 !(exprs[0]->expression.vtype == TYPE_FIELD &&
566 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
568 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
569 parseerror(parser, "cannot index value of type %s", ty1);
572 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
573 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
574 parseerror(parser, "index must be of type float, not %s", ty1);
577 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
578 if (rotate_entfield_array_index_nodes(&out))
580 if (opts_standard != COMPILER_GMQCC) {
581 /* this error doesn't need to make us bail out */
582 (void)!parsewarning(parser, WARN_EXTENSIONS,
583 "accessing array-field members of an entity without parenthesis\n"
584 " -> this is an extension from -std=gmqcc");
591 ast_block_add_expr(blocks[0], exprs[1]);
593 blocks[0] = ast_block_new(ctx);
594 ast_block_add_expr(blocks[0], exprs[0]);
595 ast_block_add_expr(blocks[0], exprs[1]);
597 if (!ast_block_set_type(blocks[0], exprs[1]))
600 vec_push(sy->out, syblock(ctx, blocks[0]));
607 switch (exprs[0]->expression.vtype) {
609 if (CanConstFold1(exprs[0]))
610 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
612 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
613 (ast_expression*)parser_const_float_0(parser),
617 if (CanConstFold1(exprs[0]))
618 out = (ast_expression*)parser_const_vector_f(parser,
619 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
621 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
622 (ast_expression*)parser_const_vector_0(parser),
626 parseerror(parser, "invalid types used in expression: cannot negate type %s",
627 type_name[exprs[0]->expression.vtype]);
633 switch (exprs[0]->expression.vtype) {
635 if (CanConstFold1(exprs[0]))
636 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
638 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
641 if (CanConstFold1(exprs[0]))
642 out = (ast_expression*)parser_const_float(parser,
643 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
645 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
648 if (CanConstFold1(exprs[0]))
649 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
651 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
653 /* we don't constant-fold NOT for these types */
655 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
658 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
661 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
662 type_name[exprs[0]->expression.vtype]);
668 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
669 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
671 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
672 type_name[exprs[0]->expression.vtype],
673 type_name[exprs[1]->expression.vtype]);
676 switch (exprs[0]->expression.vtype) {
678 if (CanConstFold(exprs[0], exprs[1]))
680 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
683 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
686 if (CanConstFold(exprs[0], exprs[1]))
687 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
689 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
692 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
693 type_name[exprs[0]->expression.vtype],
694 type_name[exprs[1]->expression.vtype]);
699 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
700 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
702 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
703 type_name[exprs[1]->expression.vtype],
704 type_name[exprs[0]->expression.vtype]);
707 switch (exprs[0]->expression.vtype) {
709 if (CanConstFold(exprs[0], exprs[1]))
710 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
712 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
715 if (CanConstFold(exprs[0], exprs[1]))
716 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
718 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
721 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
722 type_name[exprs[1]->expression.vtype],
723 type_name[exprs[0]->expression.vtype]);
728 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
729 exprs[0]->expression.vtype != TYPE_VECTOR &&
730 exprs[0]->expression.vtype != TYPE_FLOAT &&
731 exprs[1]->expression.vtype != TYPE_VECTOR &&
732 exprs[1]->expression.vtype != TYPE_FLOAT)
734 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
735 type_name[exprs[1]->expression.vtype],
736 type_name[exprs[0]->expression.vtype]);
739 switch (exprs[0]->expression.vtype) {
741 if (exprs[1]->expression.vtype == TYPE_VECTOR)
743 if (CanConstFold(exprs[0], exprs[1]))
744 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
746 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
750 if (CanConstFold(exprs[0], exprs[1]))
751 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
753 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
757 if (exprs[1]->expression.vtype == TYPE_FLOAT)
759 if (CanConstFold(exprs[0], exprs[1]))
760 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
762 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
766 if (CanConstFold(exprs[0], exprs[1]))
767 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
769 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
773 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
774 type_name[exprs[1]->expression.vtype],
775 type_name[exprs[0]->expression.vtype]);
780 if (NotSameType(TYPE_FLOAT)) {
781 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
782 type_name[exprs[0]->expression.vtype],
783 type_name[exprs[1]->expression.vtype]);
786 if (CanConstFold(exprs[0], exprs[1]))
787 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
789 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
793 parseerror(parser, "qc does not have a modulo operator");
797 if (NotSameType(TYPE_FLOAT)) {
798 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
799 type_name[exprs[0]->expression.vtype],
800 type_name[exprs[1]->expression.vtype]);
803 if (CanConstFold(exprs[0], exprs[1]))
804 out = (ast_expression*)parser_const_float(parser,
805 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
806 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
808 out = (ast_expression*)ast_binary_new(ctx,
809 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
813 parseerror(parser, "TODO: bitxor");
818 case opid3('<','<','='):
819 case opid3('>','>','='):
820 parseerror(parser, "TODO: shifts");
824 generated_op += 1; /* INSTR_OR */
826 generated_op += INSTR_AND;
828 if (NotSameType(TYPE_FLOAT)) {
829 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
830 type_name[exprs[0]->expression.vtype],
831 type_name[exprs[1]->expression.vtype]);
832 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
833 parseerror(parser, "TODO: optional early out");
837 if (opts_standard == COMPILER_GMQCC)
838 con_out("TODO: early out logic\n");
839 if (CanConstFold(exprs[0], exprs[1]))
840 out = (ast_expression*)parser_const_float(parser,
841 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
843 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
847 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
848 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
849 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
850 parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
853 if (CanConstFold1(exprs[0]))
854 out = (ConstF(0) ? exprs[1] : exprs[2]);
856 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
860 generated_op += 1; /* INSTR_GT */
862 generated_op += 1; /* INSTR_LT */
863 case opid2('>', '='):
864 generated_op += 1; /* INSTR_GE */
865 case opid2('<', '='):
866 generated_op += INSTR_LE;
867 if (NotSameType(TYPE_FLOAT)) {
868 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
869 type_name[exprs[0]->expression.vtype],
870 type_name[exprs[1]->expression.vtype]);
873 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
875 case opid2('!', '='):
876 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
877 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
878 type_name[exprs[0]->expression.vtype],
879 type_name[exprs[1]->expression.vtype]);
882 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
884 case opid2('=', '='):
885 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
886 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
887 type_name[exprs[0]->expression.vtype],
888 type_name[exprs[1]->expression.vtype]);
891 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
895 if (ast_istype(exprs[0], ast_entfield)) {
896 ast_expression *field = ((ast_entfield*)exprs[0])->field;
897 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
898 exprs[0]->expression.vtype == TYPE_FIELD &&
899 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
901 assignop = type_storep_instr[TYPE_VECTOR];
904 assignop = type_storep_instr[exprs[0]->expression.vtype];
905 if (!ast_compare_type(field->expression.next, exprs[1])) {
906 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
907 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
908 if (opts_standard == COMPILER_QCC &&
909 field->expression.next->expression.vtype == TYPE_FUNCTION &&
910 exprs[1]->expression.vtype == TYPE_FUNCTION)
912 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
913 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
919 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
924 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
925 exprs[0]->expression.vtype == TYPE_FIELD &&
926 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
928 assignop = type_store_instr[TYPE_VECTOR];
931 assignop = type_store_instr[exprs[0]->expression.vtype];
934 if (assignop == AINSTR_END) {
935 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
936 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
937 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
939 else if (!ast_compare_type(exprs[0], exprs[1])) {
940 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
941 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
942 if (opts_standard == COMPILER_QCC &&
943 exprs[0]->expression.vtype == TYPE_FUNCTION &&
944 exprs[1]->expression.vtype == TYPE_FUNCTION)
946 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
947 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
953 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
956 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
957 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
959 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
961 case opid3('+','+','P'):
962 case opid3('-','-','P'):
964 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
965 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
966 parseerror(parser, "invalid type for prefix increment: %s", ty1);
969 if (op->id == opid3('+','+','P'))
973 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
974 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
976 if (ast_istype(exprs[0], ast_entfield)) {
977 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
979 (ast_expression*)parser_const_float_1(parser));
981 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
983 (ast_expression*)parser_const_float_1(parser));
986 case opid3('S','+','+'):
987 case opid3('S','-','-'):
989 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
990 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
991 parseerror(parser, "invalid type for suffix increment: %s", ty1);
994 if (op->id == opid3('S','+','+')) {
1001 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1002 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1004 if (ast_istype(exprs[0], ast_entfield)) {
1005 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1007 (ast_expression*)parser_const_float_1(parser));
1009 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1011 (ast_expression*)parser_const_float_1(parser));
1015 out = (ast_expression*)ast_binary_new(ctx, subop,
1017 (ast_expression*)parser_const_float_1(parser));
1019 case opid2('+','='):
1020 case opid2('-','='):
1021 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1022 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
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: cannot add or subtract type %s and %s",
1030 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1031 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1033 if (ast_istype(exprs[0], ast_entfield))
1034 assignop = type_storep_instr[exprs[0]->expression.vtype];
1036 assignop = type_store_instr[exprs[0]->expression.vtype];
1037 switch (exprs[0]->expression.vtype) {
1039 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1040 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1041 exprs[0], exprs[1]);
1044 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1045 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1046 exprs[0], exprs[1]);
1049 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1050 type_name[exprs[0]->expression.vtype],
1051 type_name[exprs[1]->expression.vtype]);
1055 case opid2('*','='):
1056 case opid2('/','='):
1057 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1058 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1059 exprs[0]->expression.vtype == TYPE_VECTOR))
1061 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1062 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1063 parseerror(parser, "invalid types used in expression: %s and %s",
1067 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1068 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1070 if (ast_istype(exprs[0], ast_entfield))
1071 assignop = type_storep_instr[exprs[0]->expression.vtype];
1073 assignop = type_store_instr[exprs[0]->expression.vtype];
1074 switch (exprs[0]->expression.vtype) {
1076 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1077 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1078 exprs[0], exprs[1]);
1081 if (op->id == opid2('*','=')) {
1082 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1083 exprs[0], exprs[1]);
1085 /* there's no DIV_VF */
1086 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1087 (ast_expression*)parser_const_float_1(parser),
1091 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1096 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1097 type_name[exprs[0]->expression.vtype],
1098 type_name[exprs[1]->expression.vtype]);
1102 case opid2('&','='):
1103 case opid2('|','='):
1104 if (NotSameType(TYPE_FLOAT)) {
1105 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1106 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1107 parseerror(parser, "invalid types used in expression: %s and %s",
1111 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1112 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1114 if (ast_istype(exprs[0], ast_entfield))
1115 assignop = type_storep_instr[exprs[0]->expression.vtype];
1117 assignop = type_store_instr[exprs[0]->expression.vtype];
1118 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1119 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1120 exprs[0], exprs[1]);
1122 case opid3('&','~','='):
1123 /* This is like: a &= ~(b);
1124 * But QC has no bitwise-not, so we implement it as
1127 if (NotSameType(TYPE_FLOAT)) {
1128 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1129 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1130 parseerror(parser, "invalid types used in expression: %s and %s",
1134 if (ast_istype(exprs[0], ast_entfield))
1135 assignop = type_storep_instr[exprs[0]->expression.vtype];
1137 assignop = type_store_instr[exprs[0]->expression.vtype];
1138 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1141 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1142 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1144 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1150 parseerror(parser, "failed to apply operand %s", op->op);
1154 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1155 vec_push(sy->out, syexp(ctx, out));
1159 static bool parser_close_call(parser_t *parser, shunt *sy)
1161 /* was a function call */
1162 ast_expression *fun;
1168 vec_shrinkby(sy->ops, 1);
1169 fid = sy->ops[vec_size(sy->ops)].off;
1171 /* out[fid] is the function
1172 * everything above is parameters...
1173 * 0 params = nothing
1174 * 1 params = ast_expression
1178 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1179 parseerror(parser, "internal error: function call needs function and parameter list...");
1183 fun = sy->out[fid].out;
1185 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1187 parseerror(parser, "out of memory");
1191 if (fid+1 == vec_size(sy->out)) {
1194 } else if (fid+2 == vec_size(sy->out)) {
1196 vec_shrinkby(sy->out, 1);
1197 params = sy->out[vec_size(sy->out)].block;
1201 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1203 paramcount = vec_size(params->exprs);
1204 call->params = params->exprs;
1205 params->exprs = NULL;
1208 if (!ast_call_check_types(call))
1211 parseerror(parser, "invalid function call");
1215 /* overwrite fid, the function, with a call */
1216 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1218 if (fun->expression.vtype != TYPE_FUNCTION) {
1219 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1223 if (!fun->expression.next) {
1224 parseerror(parser, "could not determine function return type");
1227 if (vec_size(fun->expression.params) != paramcount &&
1228 !(fun->expression.variadic &&
1229 vec_size(fun->expression.params) < paramcount))
1232 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1234 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1235 if (opts_standard == COMPILER_GMQCC)
1238 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1239 " -> `%s` has been declared here: %s:%i",
1240 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1241 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1243 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1244 " -> `%s` has been declared here: %s:%i",
1245 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1246 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1252 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1253 "too %s parameters for call to %s: expected %i, got %i\n"
1254 " -> `%s` has been declared here: %s:%i",
1255 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1256 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1258 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1259 "too %s parameters for function call: expected %i, got %i\n"
1260 " -> `%s` has been declared here: %s:%i",
1261 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1262 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1270 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1272 if (!vec_size(sy->ops)) {
1273 parseerror(parser, "unmatched closing paren");
1276 /* this would for bit a + (x) because there are no operators inside (x)
1277 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1278 parseerror(parser, "empty parenthesis expression");
1282 while (vec_size(sy->ops)) {
1283 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1284 if (!parser_close_call(parser, sy))
1288 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1289 vec_shrinkby(sy->ops, 1);
1290 return !functions_only;
1292 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1295 /* pop off the parenthesis */
1296 vec_shrinkby(sy->ops, 1);
1297 /* then apply the index operator */
1298 if (!parser_sy_pop(parser, sy))
1302 if (!parser_sy_pop(parser, sy))
1308 static void parser_reclassify_token(parser_t *parser)
1311 for (i = 0; i < operator_count; ++i) {
1312 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1313 parser->tok = TOKEN_OPERATOR;
1319 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1321 ast_expression *expr = NULL;
1323 bool wantop = false;
1324 bool gotmemberof = false;
1326 /* count the parens because an if starts with one, so the
1327 * end of a condition is an unmatched closing paren
1335 parser->lex->flags.noops = false;
1337 parser_reclassify_token(parser);
1342 gotmemberof = false;
1344 parser->memberof = 0;
1346 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1347 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1349 /* a translatable string */
1353 parseerror(parser, "expected operator or end of statement, got constant");
1357 parser->lex->flags.noops = true;
1358 if (!parser_next(parser) || parser->tok != '(') {
1359 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1362 parser->lex->flags.noops = false;
1363 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1364 parseerror(parser, "expected a constant string in translatable-string extension");
1367 val = parser_const_string(parser, parser_tokval(parser), true);
1371 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1372 DEBUGSHUNTDO(con_out("push string\n"));
1374 if (!parser_next(parser) || parser->tok != ')') {
1375 parseerror(parser, "expected closing paren after translatable string");
1379 else if (parser->tok == TOKEN_IDENT)
1381 ast_expression *var;
1383 parseerror(parser, "expected operator or end of statement");
1388 if (opts_standard == COMPILER_GMQCC)
1390 if (parser->memberof == TYPE_ENTITY) {
1391 /* still get vars first since there could be a fieldpointer */
1392 var = parser_find_var(parser, parser_tokval(parser));
1394 var = parser_find_field(parser, parser_tokval(parser));
1396 else if (parser->memberof == TYPE_VECTOR)
1398 parseerror(parser, "TODO: implement effective vector member access");
1401 else if (parser->memberof) {
1402 parseerror(parser, "namespace for member not found");
1406 var = parser_find_var(parser, parser_tokval(parser));
1408 var = parser_find_var(parser, parser_tokval(parser));
1410 var = parser_find_field(parser, parser_tokval(parser));
1413 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1416 if (ast_istype(var, ast_value))
1417 ((ast_value*)var)->uses++;
1418 vec_push(sy.out, syexp(parser_ctx(parser), var));
1419 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1421 else if (parser->tok == TOKEN_FLOATCONST) {
1424 parseerror(parser, "expected operator or end of statement, got constant");
1428 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1431 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1432 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1434 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1437 parseerror(parser, "expected operator or end of statement, got constant");
1441 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1444 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1445 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1447 else if (parser->tok == TOKEN_STRINGCONST) {
1450 parseerror(parser, "expected operator or end of statement, got constant");
1454 val = parser_const_string(parser, parser_tokval(parser), false);
1457 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1458 DEBUGSHUNTDO(con_out("push string\n"));
1460 else if (parser->tok == TOKEN_VECTORCONST) {
1463 parseerror(parser, "expected operator or end of statement, got constant");
1467 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1470 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1471 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1472 parser_token(parser)->constval.v.x,
1473 parser_token(parser)->constval.v.y,
1474 parser_token(parser)->constval.v.z));
1476 else if (parser->tok == '(') {
1477 parseerror(parser, "internal error: '(' should be classified as operator");
1480 else if (parser->tok == '[') {
1481 parseerror(parser, "internal error: '[' should be classified as operator");
1484 else if (parser->tok == ')') {
1486 DEBUGSHUNTDO(con_out("do[op] )\n"));
1490 /* we do expect an operator next */
1491 /* closing an opening paren */
1492 if (!parser_close_paren(parser, &sy, false))
1495 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1499 /* allowed for function calls */
1500 if (!parser_close_paren(parser, &sy, true))
1505 else if (parser->tok == ']') {
1507 parseerror(parser, "operand expected");
1511 if (!parser_close_paren(parser, &sy, false))
1515 else if (parser->tok != TOKEN_OPERATOR) {
1517 parseerror(parser, "expected operator or end of statement");
1524 /* classify the operator */
1525 const oper_info *op;
1526 const oper_info *olast = NULL;
1528 for (o = 0; o < operator_count; ++o) {
1529 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1530 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1531 !strcmp(parser_tokval(parser), operators[o].op))
1536 if (o == operator_count) {
1537 /* no operator found... must be the end of the statement */
1540 /* found an operator */
1543 /* when declaring variables, a comma starts a new variable */
1544 if (op->id == opid1(',') && !parens && stopatcomma) {
1545 /* fixup the token */
1550 /* a colon without a pervious question mark cannot be a ternary */
1551 if (!ternaries && op->id == opid2(':','?')) {
1556 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1557 olast = &operators[vec_last(sy.ops).etype-1];
1560 (op->prec < olast->prec) ||
1561 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1563 if (!parser_sy_pop(parser, &sy))
1565 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1566 olast = &operators[vec_last(sy.ops).etype-1];
1571 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1572 /* for gmqcc standard: open up the namespace of the previous type */
1573 ast_expression *prevex = vec_last(sy.out).out;
1575 parseerror(parser, "unexpected member operator");
1578 if (prevex->expression.vtype == TYPE_ENTITY)
1579 parser->memberof = TYPE_ENTITY;
1580 else if (prevex->expression.vtype == TYPE_VECTOR)
1581 parser->memberof = TYPE_VECTOR;
1583 parseerror(parser, "type error: type has no members");
1589 if (op->id == opid1('(')) {
1591 size_t sycount = vec_size(sy.out);
1592 DEBUGSHUNTDO(con_out("push [op] (\n"));
1594 /* we expected an operator, this is the function-call operator */
1595 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1598 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1599 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1602 } else if (op->id == opid1('[')) {
1604 parseerror(parser, "unexpected array subscript");
1608 /* push both the operator and the paren, this makes life easier */
1609 vec_push(sy.ops, syop(parser_ctx(parser), op));
1610 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1612 } else if (op->id == opid2('?',':')) {
1614 vec_push(sy.ops, syop(parser_ctx(parser), op));
1617 } else if (op->id == opid2(':','?')) {
1618 /* we don't push this operator */
1622 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1623 vec_push(sy.ops, syop(parser_ctx(parser), op));
1624 wantop = !!(op->flags & OP_SUFFIX);
1627 if (!parser_next(parser)) {
1630 if (parser->tok == ';' ||
1631 (!parens && parser->tok == ']'))
1637 while (vec_size(sy.ops)) {
1638 if (!parser_sy_pop(parser, &sy))
1642 parser->lex->flags.noops = true;
1643 if (!vec_size(sy.out)) {
1644 parseerror(parser, "empty expression");
1647 expr = sy.out[0].out;
1650 DEBUGSHUNTDO(con_out("shunt done\n"));
1654 parser->lex->flags.noops = true;
1660 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1662 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1665 if (!parser_next(parser)) {
1672 static void parser_enterblock(parser_t *parser)
1674 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1675 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1676 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1677 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1680 static bool parser_leaveblock(parser_t *parser)
1683 size_t locals, typedefs;
1685 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1686 parseerror(parser, "internal error: parser_leaveblock with no block");
1690 util_htdel(vec_last(parser->variables));
1691 vec_pop(parser->variables);
1692 if (!vec_size(parser->_blocklocals)) {
1693 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1697 locals = vec_last(parser->_blocklocals);
1698 vec_pop(parser->_blocklocals);
1699 while (vec_size(parser->_locals) != locals) {
1700 ast_expression *e = vec_last(parser->_locals);
1701 ast_value *v = (ast_value*)e;
1702 vec_pop(parser->_locals);
1703 if (ast_istype(e, ast_value) && !v->uses) {
1704 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
1709 typedefs = vec_last(parser->_blocktypedefs);
1710 while (vec_size(parser->_typedefs) != typedefs) {
1711 ast_delete(vec_last(parser->_typedefs));
1712 vec_pop(parser->_typedefs);
1714 util_htdel(vec_last(parser->typedefs));
1715 vec_pop(parser->typedefs);
1720 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1722 vec_push(parser->_locals, e);
1723 util_htset(vec_last(parser->variables), name, (void*)e);
1726 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1729 ast_expression *cond, *ontrue, *onfalse = NULL;
1732 lex_ctx ctx = parser_ctx(parser);
1734 (void)block; /* not touching */
1736 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1737 if (!parser_next(parser)) {
1738 parseerror(parser, "expected condition or 'not'");
1741 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
1743 if (!parser_next(parser)) {
1744 parseerror(parser, "expected condition in parenthesis");
1748 if (parser->tok != '(') {
1749 parseerror(parser, "expected 'if' condition in parenthesis");
1752 /* parse into the expression */
1753 if (!parser_next(parser)) {
1754 parseerror(parser, "expected 'if' condition after opening paren");
1757 /* parse the condition */
1758 cond = parse_expression_leave(parser, false);
1762 if (parser->tok != ')') {
1763 parseerror(parser, "expected closing paren after 'if' condition");
1767 /* parse into the 'then' branch */
1768 if (!parser_next(parser)) {
1769 parseerror(parser, "expected statement for on-true branch of 'if'");
1773 ontrue = parse_statement_or_block(parser);
1778 /* check for an else */
1779 if (!strcmp(parser_tokval(parser), "else")) {
1780 /* parse into the 'else' branch */
1781 if (!parser_next(parser)) {
1782 parseerror(parser, "expected on-false branch after 'else'");
1787 onfalse = parse_statement_or_block(parser);
1796 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1798 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1799 *out = (ast_expression*)ifthen;
1803 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1806 ast_expression *cond, *ontrue;
1808 lex_ctx ctx = parser_ctx(parser);
1810 (void)block; /* not touching */
1812 /* skip the 'while' and check for opening paren */
1813 if (!parser_next(parser) || parser->tok != '(') {
1814 parseerror(parser, "expected 'while' condition in parenthesis");
1817 /* parse into the expression */
1818 if (!parser_next(parser)) {
1819 parseerror(parser, "expected 'while' condition after opening paren");
1822 /* parse the condition */
1823 cond = parse_expression_leave(parser, false);
1827 if (parser->tok != ')') {
1828 parseerror(parser, "expected closing paren after 'while' condition");
1832 /* parse into the 'then' branch */
1833 if (!parser_next(parser)) {
1834 parseerror(parser, "expected while-loop body");
1838 ontrue = parse_statement_or_block(parser);
1844 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1845 *out = (ast_expression*)aloop;
1849 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1852 ast_expression *cond, *ontrue;
1854 lex_ctx ctx = parser_ctx(parser);
1856 (void)block; /* not touching */
1858 /* skip the 'do' and get the body */
1859 if (!parser_next(parser)) {
1860 parseerror(parser, "expected loop body");
1863 ontrue = parse_statement_or_block(parser);
1867 /* expect the "while" */
1868 if (parser->tok != TOKEN_KEYWORD ||
1869 strcmp(parser_tokval(parser), "while"))
1871 parseerror(parser, "expected 'while' and condition");
1876 /* skip the 'while' and check for opening paren */
1877 if (!parser_next(parser) || parser->tok != '(') {
1878 parseerror(parser, "expected 'while' condition in parenthesis");
1882 /* parse into the expression */
1883 if (!parser_next(parser)) {
1884 parseerror(parser, "expected 'while' condition after opening paren");
1888 /* parse the condition */
1889 cond = parse_expression_leave(parser, false);
1893 if (parser->tok != ')') {
1894 parseerror(parser, "expected closing paren after 'while' condition");
1900 if (!parser_next(parser) || parser->tok != ';') {
1901 parseerror(parser, "expected semicolon after condition");
1907 if (!parser_next(parser)) {
1908 parseerror(parser, "parse error");
1914 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1915 *out = (ast_expression*)aloop;
1919 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1922 ast_expression *initexpr, *cond, *increment, *ontrue;
1926 lex_ctx ctx = parser_ctx(parser);
1928 parser_enterblock(parser);
1935 /* skip the 'while' and check for opening paren */
1936 if (!parser_next(parser) || parser->tok != '(') {
1937 parseerror(parser, "expected 'for' expressions in parenthesis");
1940 /* parse into the expression */
1941 if (!parser_next(parser)) {
1942 parseerror(parser, "expected 'for' initializer after opening paren");
1947 if (parser->tok == TOKEN_IDENT)
1948 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
1950 if (typevar || parser->tok == TOKEN_TYPENAME) {
1951 if (opts_standard != COMPILER_GMQCC) {
1952 if (parsewarning(parser, WARN_EXTENSIONS,
1953 "current standard does not allow variable declarations in for-loop initializers"))
1956 if (!parse_variable(parser, block, true, CV_VAR, typevar))
1959 else if (parser->tok != ';')
1961 initexpr = parse_expression_leave(parser, false);
1966 /* move on to condition */
1967 if (parser->tok != ';') {
1968 parseerror(parser, "expected semicolon after for-loop initializer");
1971 if (!parser_next(parser)) {
1972 parseerror(parser, "expected for-loop condition");
1976 /* parse the condition */
1977 if (parser->tok != ';') {
1978 cond = parse_expression_leave(parser, false);
1983 /* move on to incrementor */
1984 if (parser->tok != ';') {
1985 parseerror(parser, "expected semicolon after for-loop initializer");
1988 if (!parser_next(parser)) {
1989 parseerror(parser, "expected for-loop condition");
1993 /* parse the incrementor */
1994 if (parser->tok != ')') {
1995 increment = parse_expression_leave(parser, false);
1998 if (!ast_side_effects(increment)) {
1999 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2005 if (parser->tok != ')') {
2006 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2009 /* parse into the 'then' branch */
2010 if (!parser_next(parser)) {
2011 parseerror(parser, "expected for-loop body");
2014 ontrue = parse_statement_or_block(parser);
2019 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
2020 *out = (ast_expression*)aloop;
2022 if (!parser_leaveblock(parser))
2026 if (initexpr) ast_delete(initexpr);
2027 if (cond) ast_delete(cond);
2028 if (increment) ast_delete(increment);
2029 (void)!parser_leaveblock(parser);
2033 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2035 ast_expression *exp = NULL;
2036 ast_return *ret = NULL;
2037 ast_value *expected = parser->function->vtype;
2039 (void)block; /* not touching */
2041 if (!parser_next(parser)) {
2042 parseerror(parser, "expected return expression");
2046 if (parser->tok != ';') {
2047 exp = parse_expression(parser, false);
2051 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2052 parseerror(parser, "return with invalid expression");
2055 ret = ast_return_new(exp->expression.node.context, exp);
2061 if (!parser_next(parser))
2062 parseerror(parser, "parse error");
2063 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2064 if (opts_standard != COMPILER_GMQCC)
2065 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2067 parseerror(parser, "return without value");
2069 ret = ast_return_new(parser_ctx(parser), NULL);
2071 *out = (ast_expression*)ret;
2075 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2077 lex_ctx ctx = parser_ctx(parser);
2079 (void)block; /* not touching */
2081 if (!parser_next(parser) || parser->tok != ';') {
2082 parseerror(parser, "expected semicolon");
2086 if (!parser_next(parser))
2087 parseerror(parser, "parse error");
2089 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2093 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2095 ast_expression *operand;
2097 ast_switch *switchnode;
2098 ast_switch_case swcase;
2100 lex_ctx ctx = parser_ctx(parser);
2102 (void)block; /* not touching */
2104 /* parse over the opening paren */
2105 if (!parser_next(parser) || parser->tok != '(') {
2106 parseerror(parser, "expected switch operand in parenthesis");
2110 /* parse into the expression */
2111 if (!parser_next(parser)) {
2112 parseerror(parser, "expected switch operand");
2115 /* parse the operand */
2116 operand = parse_expression_leave(parser, false);
2120 switchnode = ast_switch_new(ctx, operand);
2123 if (parser->tok != ')') {
2124 ast_delete(switchnode);
2125 parseerror(parser, "expected closing paren after 'switch' operand");
2129 /* parse over the opening paren */
2130 if (!parser_next(parser) || parser->tok != '{') {
2131 ast_delete(switchnode);
2132 parseerror(parser, "expected list of cases");
2136 if (!parser_next(parser)) {
2137 ast_delete(switchnode);
2138 parseerror(parser, "expected 'case' or 'default'");
2143 while (parser->tok != '}') {
2144 ast_block *caseblock;
2146 if (parser->tok != TOKEN_KEYWORD) {
2147 ast_delete(switchnode);
2148 parseerror(parser, "expected 'case' or 'default'");
2151 if (!strcmp(parser_tokval(parser), "case")) {
2152 if (!parser_next(parser)) {
2153 ast_delete(switchnode);
2154 parseerror(parser, "expected expression for case");
2157 swcase.value = parse_expression_leave(parser, false);
2158 if (!swcase.value) {
2159 ast_delete(switchnode);
2160 parseerror(parser, "expected expression for case");
2163 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2164 opval = (ast_value*)swcase.value;
2165 if (!ast_istype(swcase.value, ast_value)) { /* || !opval->constant) { */
2166 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2172 else if (!strcmp(parser_tokval(parser), "default")) {
2173 swcase.value = NULL;
2174 if (!parser_next(parser)) {
2175 ast_delete(switchnode);
2176 parseerror(parser, "expected colon");
2181 /* Now the colon and body */
2182 if (parser->tok != ':') {
2183 if (swcase.value) ast_unref(swcase.value);
2184 ast_delete(switchnode);
2185 parseerror(parser, "expected colon");
2189 if (!parser_next(parser)) {
2190 if (swcase.value) ast_unref(swcase.value);
2191 ast_delete(switchnode);
2192 parseerror(parser, "expected statements or case");
2195 caseblock = ast_block_new(parser_ctx(parser));
2197 if (swcase.value) ast_unref(swcase.value);
2198 ast_delete(switchnode);
2201 swcase.code = (ast_expression*)caseblock;
2202 vec_push(switchnode->cases, swcase);
2204 ast_expression *expr;
2205 if (parser->tok == '}')
2207 if (parser->tok == TOKEN_KEYWORD) {
2208 if (!strcmp(parser_tokval(parser), "case") ||
2209 !strcmp(parser_tokval(parser), "default"))
2214 if (!parse_statement(parser, caseblock, &expr, true)) {
2215 ast_delete(switchnode);
2220 ast_block_add_expr(caseblock, expr);
2225 if (parser->tok != '}') {
2226 ast_delete(switchnode);
2227 parseerror(parser, "expected closing paren of case list");
2230 if (!parser_next(parser)) {
2231 ast_delete(switchnode);
2232 parseerror(parser, "parse error after switch");
2235 *out = (ast_expression*)switchnode;
2239 static bool parse_goto(parser_t *parser, ast_expression **out)
2244 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2245 parseerror(parser, "expected label name after `goto`");
2249 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2251 for (i = 0; i < vec_size(parser->labels); ++i) {
2252 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2253 ast_goto_set_label(gt, parser->labels[i]);
2257 if (i == vec_size(parser->labels))
2258 vec_push(parser->gotos, gt);
2260 if (!parser_next(parser) || parser->tok != ';') {
2261 parseerror(parser, "semicolon expected after goto label");
2264 if (!parser_next(parser)) {
2265 parseerror(parser, "parse error after goto");
2269 *out = (ast_expression*)gt;
2273 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2276 ast_value *typevar = NULL;
2279 if (parser->tok == TOKEN_IDENT)
2280 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2282 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2284 /* local variable */
2286 parseerror(parser, "cannot declare a variable from here");
2289 if (opts_standard == COMPILER_QCC) {
2290 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2293 if (!parse_variable(parser, block, false, CV_NONE, typevar))
2298 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
2302 else if (parser->tok == TOKEN_KEYWORD)
2304 if (!strcmp(parser_tokval(parser), "local") ||
2305 !strcmp(parser_tokval(parser), "const") ||
2306 !strcmp(parser_tokval(parser), "var"))
2309 if (parser_tokval(parser)[0] == 'c')
2311 else if (parser_tokval(parser)[0] == 'v')
2317 parseerror(parser, "cannot declare a local variable here");
2320 if (!parser_next(parser)) {
2321 parseerror(parser, "expected variable declaration");
2324 if (!parse_variable(parser, block, true, cvq, NULL))
2329 else if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
2334 if (!parser_next(parser)) {
2335 parseerror(parser, "parse error after __builtin_debug_printtype");
2339 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
2341 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
2342 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
2343 if (!parser_next(parser)) {
2344 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
2350 if (!parse_statement(parser, block, out, allow_cases))
2353 con_out("__builtin_debug_printtype: got no output node\n");
2356 ast_type_to_string(*out, ty, sizeof(ty));
2357 con_out("__builtin_debug_printtype: `%s`\n", ty);
2362 else if (!strcmp(parser_tokval(parser), "return"))
2364 return parse_return(parser, block, out);
2366 else if (!strcmp(parser_tokval(parser), "if"))
2368 return parse_if(parser, block, out);
2370 else if (!strcmp(parser_tokval(parser), "while"))
2372 return parse_while(parser, block, out);
2374 else if (!strcmp(parser_tokval(parser), "do"))
2376 return parse_dowhile(parser, block, out);
2378 else if (!strcmp(parser_tokval(parser), "for"))
2380 if (opts_standard == COMPILER_QCC) {
2381 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2384 return parse_for(parser, block, out);
2386 else if (!strcmp(parser_tokval(parser), "break"))
2388 return parse_break_continue(parser, block, out, false);
2390 else if (!strcmp(parser_tokval(parser), "continue"))
2392 return parse_break_continue(parser, block, out, true);
2394 else if (!strcmp(parser_tokval(parser), "switch"))
2396 return parse_switch(parser, block, out);
2398 else if (!strcmp(parser_tokval(parser), "case") ||
2399 !strcmp(parser_tokval(parser), "default"))
2402 parseerror(parser, "unexpected 'case' label");
2407 else if (!strcmp(parser_tokval(parser), "goto"))
2409 return parse_goto(parser, out);
2411 else if (!strcmp(parser_tokval(parser), "typedef"))
2413 if (!parser_next(parser)) {
2414 parseerror(parser, "expected type definition after 'typedef'");
2417 return parse_typedef(parser);
2419 parseerror(parser, "Unexpected keyword");
2422 else if (parser->tok == '{')
2425 inner = parse_block(parser, false);
2428 *out = (ast_expression*)inner;
2431 else if (parser->tok == ':')
2435 if (!parser_next(parser)) {
2436 parseerror(parser, "expected label name");
2439 if (parser->tok != TOKEN_IDENT) {
2440 parseerror(parser, "label must be an identifier");
2443 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
2446 vec_push(parser->labels, label);
2447 *out = (ast_expression*)label;
2448 if (!parser_next(parser)) {
2449 parseerror(parser, "parse error after label");
2452 for (i = 0; i < vec_size(parser->gotos); ++i) {
2453 if (!strcmp(parser->gotos[i]->name, label->name)) {
2454 ast_goto_set_label(parser->gotos[i], label);
2455 vec_remove(parser->gotos, i, 1);
2461 else if (parser->tok == ';')
2463 if (!parser_next(parser)) {
2464 parseerror(parser, "parse error after empty statement");
2471 ast_expression *exp = parse_expression(parser, false);
2475 if (!ast_side_effects(exp)) {
2476 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2483 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2487 parser_enterblock(parser);
2489 if (!parser_next(parser)) { /* skip the '{' */
2490 parseerror(parser, "expected function body");
2494 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2496 ast_expression *expr = NULL;
2497 if (parser->tok == '}')
2500 if (!parse_statement(parser, block, &expr, false)) {
2501 /* parseerror(parser, "parse error"); */
2507 ast_block_add_expr(block, expr);
2510 if (parser->tok != '}') {
2513 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2515 if (!vec_size(block->exprs) ||
2516 !ast_istype(vec_last(block->exprs), ast_return))
2518 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2524 (void)parser_next(parser);
2528 if (!parser_leaveblock(parser))
2530 return retval && !!block;
2533 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2536 block = ast_block_new(parser_ctx(parser));
2539 if (!parse_block_into(parser, block, warnreturn)) {
2540 ast_block_delete(block);
2546 static ast_expression* parse_statement_or_block(parser_t *parser)
2548 ast_expression *expr = NULL;
2549 if (parser->tok == '{')
2550 return (ast_expression*)parse_block(parser, false);
2551 if (!parse_statement(parser, NULL, &expr, false))
2556 static bool create_vector_members(ast_value *var, ast_member **me)
2559 size_t len = strlen(var->name);
2561 for (i = 0; i < 3; ++i) {
2562 char *name = mem_a(len+3);
2563 memcpy(name, var->name, len);
2565 name[len+1] = 'x'+i;
2567 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2576 do { ast_member_delete(me[--i]); } while(i);
2580 static bool parse_function_body(parser_t *parser, ast_value *var)
2582 ast_block *block = NULL;
2587 ast_expression *framenum = NULL;
2588 ast_expression *nextthink = NULL;
2589 /* None of the following have to be deleted */
2590 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2591 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2592 bool has_frame_think;
2596 has_frame_think = false;
2597 old = parser->function;
2599 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
2600 parseerror(parser, "gotos/labels leaking");
2604 if (var->expression.variadic) {
2605 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2606 "variadic function with implementation will not be able to access additional parameters"))
2612 if (parser->tok == '[') {
2613 /* got a frame definition: [ framenum, nextthink ]
2614 * this translates to:
2615 * self.frame = framenum;
2616 * self.nextthink = time + 0.1;
2617 * self.think = nextthink;
2621 fld_think = parser_find_field(parser, "think");
2622 fld_nextthink = parser_find_field(parser, "nextthink");
2623 fld_frame = parser_find_field(parser, "frame");
2624 if (!fld_think || !fld_nextthink || !fld_frame) {
2625 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2626 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2629 gbl_time = parser_find_global(parser, "time");
2630 gbl_self = parser_find_global(parser, "self");
2631 if (!gbl_time || !gbl_self) {
2632 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2633 parseerror(parser, "please declare the following globals: `time`, `self`");
2637 if (!parser_next(parser))
2640 framenum = parse_expression_leave(parser, true);
2642 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2645 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
2646 ast_unref(framenum);
2647 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2651 if (parser->tok != ',') {
2652 ast_unref(framenum);
2653 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2654 parseerror(parser, "Got a %i\n", parser->tok);
2658 if (!parser_next(parser)) {
2659 ast_unref(framenum);
2663 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2665 /* qc allows the use of not-yet-declared functions here
2666 * - this automatically creates a prototype */
2667 ast_value *thinkfunc;
2668 ast_expression *functype = fld_think->expression.next;
2670 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2671 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2672 ast_unref(framenum);
2673 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2677 if (!parser_next(parser)) {
2678 ast_unref(framenum);
2679 ast_delete(thinkfunc);
2683 vec_push(parser->globals, (ast_expression*)thinkfunc);
2684 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2685 nextthink = (ast_expression*)thinkfunc;
2688 nextthink = parse_expression_leave(parser, true);
2690 ast_unref(framenum);
2691 parseerror(parser, "expected a think-function in [frame,think] notation");
2696 if (!ast_istype(nextthink, ast_value)) {
2697 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2701 if (retval && parser->tok != ']') {
2702 parseerror(parser, "expected closing `]` for [frame,think] notation");
2706 if (retval && !parser_next(parser)) {
2710 if (retval && parser->tok != '{') {
2711 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2716 ast_unref(nextthink);
2717 ast_unref(framenum);
2721 has_frame_think = true;
2724 block = ast_block_new(parser_ctx(parser));
2726 parseerror(parser, "failed to allocate block");
2727 if (has_frame_think) {
2728 ast_unref(nextthink);
2729 ast_unref(framenum);
2734 if (has_frame_think) {
2736 ast_expression *self_frame;
2737 ast_expression *self_nextthink;
2738 ast_expression *self_think;
2739 ast_expression *time_plus_1;
2740 ast_store *store_frame;
2741 ast_store *store_nextthink;
2742 ast_store *store_think;
2744 ctx = parser_ctx(parser);
2745 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2746 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2747 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2749 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2750 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2752 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2753 if (self_frame) ast_delete(self_frame);
2754 if (self_nextthink) ast_delete(self_nextthink);
2755 if (self_think) ast_delete(self_think);
2756 if (time_plus_1) ast_delete(time_plus_1);
2762 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2763 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2764 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2767 ast_delete(self_frame);
2770 if (!store_nextthink) {
2771 ast_delete(self_nextthink);
2775 ast_delete(self_think);
2779 if (store_frame) ast_delete(store_frame);
2780 if (store_nextthink) ast_delete(store_nextthink);
2781 if (store_think) ast_delete(store_think);
2784 ast_block_add_expr(block, (ast_expression*)store_frame);
2785 ast_block_add_expr(block, (ast_expression*)store_nextthink);
2786 ast_block_add_expr(block, (ast_expression*)store_think);
2790 parseerror(parser, "failed to generate code for [frame,think]");
2791 ast_unref(nextthink);
2792 ast_unref(framenum);
2798 parser_enterblock(parser);
2800 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2802 ast_value *param = var->expression.params[parami];
2805 if (param->expression.vtype != TYPE_VECTOR &&
2806 (param->expression.vtype != TYPE_FIELD ||
2807 param->expression.next->expression.vtype != TYPE_VECTOR))
2812 if (!create_vector_members(param, me)) {
2813 ast_block_delete(block);
2817 for (e = 0; e < 3; ++e) {
2818 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2819 ast_block_collect(block, (ast_expression*)me[e]);
2823 func = ast_function_new(ast_ctx(var), var->name, var);
2825 parseerror(parser, "failed to allocate function for `%s`", var->name);
2826 ast_block_delete(block);
2829 vec_push(parser->functions, func);
2831 parser->function = func;
2832 if (!parse_block_into(parser, block, true)) {
2833 ast_block_delete(block);
2837 vec_push(func->blocks, block);
2839 parser->function = old;
2840 if (!parser_leaveblock(parser))
2842 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2843 parseerror(parser, "internal error: local scopes left");
2847 if (parser->tok == ';')
2848 return parser_next(parser);
2849 else if (opts_standard == COMPILER_QCC)
2850 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2854 vec_pop(parser->functions);
2855 ast_function_delete(func);
2856 var->constval.vfunc = NULL;
2859 (void)!parser_leaveblock(parser);
2860 parser->function = old;
2864 static ast_expression *array_accessor_split(
2869 ast_expression *left,
2870 ast_expression *right
2876 lex_ctx ctx = ast_ctx(array);
2878 if (!left || !right) {
2879 if (left) ast_delete(left);
2880 if (right) ast_delete(right);
2884 cmp = ast_binary_new(ctx, INSTR_LT,
2885 (ast_expression*)index,
2886 (ast_expression*)parser_const_float(parser, middle));
2890 parseerror(parser, "internal error: failed to create comparison for array setter");
2894 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2896 ast_delete(cmp); /* will delete left and right */
2897 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2901 return (ast_expression*)ifthen;
2904 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2906 lex_ctx ctx = ast_ctx(array);
2908 if (from+1 == afterend) {
2909 /* set this value */
2912 ast_array_index *subscript;
2914 int assignop = type_store_instr[value->expression.vtype];
2916 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2917 assignop = INSTR_STORE_V;
2919 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2923 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2925 ast_delete(subscript);
2929 block = ast_block_new(ctx);
2935 ast_block_add_expr(block, (ast_expression*)st);
2937 ret = ast_return_new(ctx, NULL);
2943 ast_block_add_expr(block, (ast_expression*)ret);
2945 return (ast_expression*)block;
2947 ast_expression *left, *right;
2948 size_t diff = afterend - from;
2949 size_t middle = from + diff/2;
2950 left = array_setter_node(parser, array, index, value, from, middle);
2951 right = array_setter_node(parser, array, index, value, middle, afterend);
2952 return array_accessor_split(parser, array, index, middle, left, right);
2956 static ast_expression *array_field_setter_node(
2965 lex_ctx ctx = ast_ctx(array);
2967 if (from+1 == afterend) {
2968 /* set this value */
2971 ast_entfield *entfield;
2972 ast_array_index *subscript;
2974 int assignop = type_storep_instr[value->expression.vtype];
2976 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2977 assignop = INSTR_STOREP_V;
2979 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2983 entfield = ast_entfield_new_force(ctx,
2984 (ast_expression*)entity,
2985 (ast_expression*)subscript,
2986 (ast_expression*)subscript);
2988 ast_delete(subscript);
2992 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2994 ast_delete(entfield);
2998 block = ast_block_new(ctx);
3004 ast_block_add_expr(block, (ast_expression*)st);
3006 ret = ast_return_new(ctx, NULL);
3012 ast_block_add_expr(block, (ast_expression*)ret);
3014 return (ast_expression*)block;
3016 ast_expression *left, *right;
3017 size_t diff = afterend - from;
3018 size_t middle = from + diff/2;
3019 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
3020 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
3021 return array_accessor_split(parser, array, index, middle, left, right);
3025 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
3027 lex_ctx ctx = ast_ctx(array);
3029 if (from+1 == afterend) {
3031 ast_array_index *subscript;
3033 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3037 ret = ast_return_new(ctx, (ast_expression*)subscript);
3039 ast_delete(subscript);
3043 return (ast_expression*)ret;
3045 ast_expression *left, *right;
3046 size_t diff = afterend - from;
3047 size_t middle = from + diff/2;
3048 left = array_getter_node(parser, array, index, from, middle);
3049 right = array_getter_node(parser, array, index, middle, afterend);
3050 return array_accessor_split(parser, array, index, middle, left, right);
3054 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3056 ast_function *func = NULL;
3057 ast_value *fval = NULL;
3058 ast_block *body = NULL;
3060 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3062 parseerror(parser, "failed to create accessor function value");
3066 func = ast_function_new(ast_ctx(array), funcname, fval);
3069 parseerror(parser, "failed to create accessor function node");
3073 body = ast_block_new(ast_ctx(array));
3075 parseerror(parser, "failed to create block for array accessor");
3081 vec_push(func->blocks, body);
3084 vec_push(parser->accessors, fval);
3089 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3091 ast_expression *root = NULL;
3092 ast_value *index = NULL;
3093 ast_value *value = NULL;
3097 if (!ast_istype(array->expression.next, ast_value)) {
3098 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3102 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3104 func = fval->constval.vfunc;
3105 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3107 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3108 value = ast_value_copy((ast_value*)array->expression.next);
3110 if (!index || !value) {
3111 parseerror(parser, "failed to create locals for array accessor");
3114 (void)!ast_value_set_name(value, "value"); /* not important */
3115 vec_push(fval->expression.params, index);
3116 vec_push(fval->expression.params, value);
3118 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3120 parseerror(parser, "failed to build accessor search tree");
3124 ast_block_add_expr(func->blocks[0], root);
3125 array->setter = fval;
3128 if (index) ast_delete(index);
3129 if (value) ast_delete(value);
3130 if (root) ast_delete(root);
3136 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3138 ast_expression *root = NULL;
3139 ast_value *entity = NULL;
3140 ast_value *index = NULL;
3141 ast_value *value = NULL;
3145 if (!ast_istype(array->expression.next, ast_value)) {
3146 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3150 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3152 func = fval->constval.vfunc;
3153 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3155 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3156 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3157 value = ast_value_copy((ast_value*)array->expression.next);
3158 if (!entity || !index || !value) {
3159 parseerror(parser, "failed to create locals for array accessor");
3162 (void)!ast_value_set_name(value, "value"); /* not important */
3163 vec_push(fval->expression.params, entity);
3164 vec_push(fval->expression.params, index);
3165 vec_push(fval->expression.params, value);
3167 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3169 parseerror(parser, "failed to build accessor search tree");
3173 ast_block_add_expr(func->blocks[0], root);
3174 array->setter = fval;
3177 if (entity) ast_delete(entity);
3178 if (index) ast_delete(index);
3179 if (value) ast_delete(value);
3180 if (root) ast_delete(root);
3186 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3188 ast_expression *root = NULL;
3189 ast_value *index = NULL;
3193 /* NOTE: checking array->expression.next rather than elemtype since
3194 * for fields elemtype is a temporary fieldtype.
3196 if (!ast_istype(array->expression.next, ast_value)) {
3197 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3201 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3203 func = fval->constval.vfunc;
3204 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3206 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3209 parseerror(parser, "failed to create locals for array accessor");
3212 vec_push(fval->expression.params, index);
3214 root = array_getter_node(parser, array, index, 0, array->expression.count);
3216 parseerror(parser, "failed to build accessor search tree");
3220 ast_block_add_expr(func->blocks[0], root);
3221 array->getter = fval;
3224 if (index) ast_delete(index);
3225 if (root) ast_delete(root);
3231 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3232 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3240 bool variadic = false;
3242 ctx = parser_ctx(parser);
3244 /* for the sake of less code we parse-in in this function */
3245 if (!parser_next(parser)) {
3246 parseerror(parser, "expected parameter list");
3252 /* parse variables until we hit a closing paren */
3253 while (parser->tok != ')') {
3255 /* there must be commas between them */
3256 if (parser->tok != ',') {
3257 parseerror(parser, "expected comma or end of parameter list");
3260 if (!parser_next(parser)) {
3261 parseerror(parser, "expected parameter");
3267 if (parser->tok == TOKEN_DOTS) {
3268 /* '...' indicates a varargs function */
3270 if (!parser_next(parser)) {
3271 parseerror(parser, "expected parameter");
3274 if (parser->tok != ')') {
3275 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3281 /* for anything else just parse a typename */
3282 param = parse_typename(parser, NULL, NULL);
3285 vec_push(params, param);
3286 if (param->expression.vtype >= TYPE_VARIANT) {
3287 char typename[1024];
3288 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3289 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3295 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3299 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3300 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3303 if (!parser_next(parser)) {
3304 parseerror(parser, "parse error after typename");
3308 /* now turn 'var' into a function type */
3309 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3310 fval->expression.next = (ast_expression*)var;
3311 fval->expression.variadic = variadic;
3314 var->expression.params = params;
3321 for (i = 0; i < vec_size(params); ++i)
3322 ast_delete(params[i]);
3327 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3329 ast_expression *cexp;
3330 ast_value *cval, *tmp;
3333 ctx = parser_ctx(parser);
3335 if (!parser_next(parser)) {
3337 parseerror(parser, "expected array-size");
3341 cexp = parse_expression_leave(parser, true);
3343 if (!cexp || !ast_istype(cexp, ast_value)) {
3347 parseerror(parser, "expected array-size as constant positive integer");
3350 cval = (ast_value*)cexp;
3352 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3353 tmp->expression.next = (ast_expression*)var;
3356 if (cval->expression.vtype == TYPE_INTEGER)
3357 tmp->expression.count = cval->constval.vint;
3358 else if (cval->expression.vtype == TYPE_FLOAT)
3359 tmp->expression.count = cval->constval.vfloat;
3363 parseerror(parser, "array-size must be a positive integer constant");
3368 if (parser->tok != ']') {
3370 parseerror(parser, "expected ']' after array-size");
3373 if (!parser_next(parser)) {
3375 parseerror(parser, "error after parsing array size");
3381 /* Parse a complete typename.
3382 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3383 * but when parsing variables separated by comma
3384 * 'storebase' should point to where the base-type should be kept.
3385 * The base type makes up every bit of type information which comes *before* the
3388 * The following will be parsed in its entirety:
3390 * The 'basetype' in this case is 'void()'
3391 * and if there's a comma after it, say:
3393 * then the type-information 'void()' can be stored in 'storebase'
3395 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3397 ast_value *var, *tmp;
3400 const char *name = NULL;
3401 bool isfield = false;
3402 bool wasarray = false;
3403 size_t morefields = 0;
3405 ctx = parser_ctx(parser);
3407 /* types may start with a dot */
3408 if (parser->tok == '.') {
3410 /* if we parsed a dot we need a typename now */
3411 if (!parser_next(parser)) {
3412 parseerror(parser, "expected typename for field definition");
3416 /* Further dots are handled seperately because they won't be part of the
3419 while (parser->tok == '.') {
3421 if (!parser_next(parser)) {
3422 parseerror(parser, "expected typename for field definition");
3427 if (parser->tok == TOKEN_IDENT)
3428 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
3429 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3430 parseerror(parser, "expected typename");
3434 /* generate the basic type value */
3435 if (cached_typedef) {
3436 var = ast_value_copy(cached_typedef);
3437 ast_value_set_name(var, "<type(from_def)>");
3439 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3441 for (; morefields; --morefields) {
3442 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
3443 tmp->expression.next = (ast_expression*)var;
3447 /* do not yet turn into a field - remember:
3448 * .void() foo; is a field too
3449 * .void()() foo; is a function
3453 if (!parser_next(parser)) {
3455 parseerror(parser, "parse error after typename");
3459 /* an opening paren now starts the parameter-list of a function
3460 * this is where original-QC has parameter lists.
3461 * We allow a single parameter list here.
3462 * Much like fteqcc we don't allow `float()() x`
3464 if (parser->tok == '(') {
3465 var = parse_parameter_list(parser, var);
3470 /* store the base if requested */
3472 *storebase = ast_value_copy(var);
3474 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3475 tmp->expression.next = (ast_expression*)*storebase;
3480 /* there may be a name now */
3481 if (parser->tok == TOKEN_IDENT) {
3482 name = util_strdup(parser_tokval(parser));
3484 if (!parser_next(parser)) {
3486 parseerror(parser, "error after variable or field declaration");
3491 /* now this may be an array */
3492 if (parser->tok == '[') {
3494 var = parse_arraysize(parser, var);
3499 /* This is the point where we can turn it into a field */
3501 /* turn it into a field if desired */
3502 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3503 tmp->expression.next = (ast_expression*)var;
3507 /* now there may be function parens again */
3508 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3509 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3510 if (parser->tok == '(' && wasarray)
3511 parseerror(parser, "arrays as part of a return type is not supported");
3512 while (parser->tok == '(') {
3513 var = parse_parameter_list(parser, var);
3522 /* finally name it */
3524 if (!ast_value_set_name(var, name)) {
3526 parseerror(parser, "internal error: failed to set name");
3529 /* free the name, ast_value_set_name duplicates */
3536 static bool parse_typedef(parser_t *parser)
3538 ast_value *typevar, *oldtype;
3539 ast_expression *old;
3541 typevar = parse_typename(parser, NULL, NULL);
3546 if ( (old = parser_find_var(parser, typevar->name)) ) {
3547 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3548 " -> `%s` has been declared here: %s:%i",
3549 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3550 ast_delete(typevar);
3554 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
3555 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3556 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3557 ast_delete(typevar);
3561 vec_push(parser->_typedefs, typevar);
3562 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3564 if (parser->tok != ';') {
3565 parseerror(parser, "expected semicolon after typedef");
3568 if (!parser_next(parser)) {
3569 parseerror(parser, "parse error after typedef");
3576 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef)
3580 ast_expression *old;
3584 ast_value *basetype = NULL;
3586 bool isparam = false;
3587 bool isvector = false;
3588 bool cleanvar = true;
3589 bool wasarray = false;
3593 /* get the first complete variable */
3594 var = parse_typename(parser, &basetype, cached_typedef);
3597 ast_delete(basetype);
3605 /* Part 0: finish the type */
3606 if (parser->tok == '(') {
3607 if (opts_standard == COMPILER_QCC)
3608 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3609 var = parse_parameter_list(parser, var);
3615 /* we only allow 1-dimensional arrays */
3616 if (parser->tok == '[') {
3618 var = parse_arraysize(parser, var);
3624 if (parser->tok == '(' && wasarray) {
3625 parseerror(parser, "arrays as part of a return type is not supported");
3626 /* we'll still parse the type completely for now */
3628 /* for functions returning functions */
3629 while (parser->tok == '(') {
3630 if (opts_standard == COMPILER_QCC)
3631 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3632 var = parse_parameter_list(parser, var);
3639 if (is_const_var == CV_CONST)
3640 var->constant = true;
3643 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3644 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3645 * is then filled with the previous definition and the parameter-names replaced.
3648 /* Deal with end_sys_ vars */
3650 if (!strcmp(var->name, "end_sys_globals")) {
3651 parser->crc_globals = vec_size(parser->globals);
3654 else if (!strcmp(var->name, "end_sys_fields")) {
3655 parser->crc_fields = vec_size(parser->fields);
3658 if (was_end && var->expression.vtype == TYPE_FIELD) {
3659 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3660 "global '%s' hint should not be a field",
3661 parser_tokval(parser)))
3668 if (!nofields && var->expression.vtype == TYPE_FIELD)
3670 /* deal with field declarations */
3671 old = parser_find_field(parser, var->name);
3673 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3674 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3683 parseerror(parser, "field `%s` already declared here: %s:%i",
3684 var->name, ast_ctx(old).file, ast_ctx(old).line);
3689 if (opts_standard == COMPILER_QCC &&
3690 (old = parser_find_global(parser, var->name)))
3692 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3693 parseerror(parser, "field `%s` already declared here: %s:%i",
3694 var->name, ast_ctx(old).file, ast_ctx(old).line);
3701 /* deal with other globals */
3702 old = parser_find_global(parser, var->name);
3703 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3705 /* This is a function which had a prototype */
3706 if (!ast_istype(old, ast_value)) {
3707 parseerror(parser, "internal error: prototype is not an ast_value");
3711 proto = (ast_value*)old;
3712 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3713 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3715 ast_ctx(proto).file, ast_ctx(proto).line);
3719 /* we need the new parameter-names */
3720 for (i = 0; i < vec_size(proto->expression.params); ++i)
3721 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3729 if (opts_standard == COMPILER_GMQCC) {
3730 parseerror(parser, "global `%s` already declared here: %s:%i",
3731 var->name, ast_ctx(old).file, ast_ctx(old).line);
3735 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3736 "global `%s` already declared here: %s:%i",
3737 var->name, ast_ctx(old).file, ast_ctx(old).line))
3744 if (opts_standard == COMPILER_QCC &&
3745 (old = parser_find_field(parser, var->name)))
3747 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3748 parseerror(parser, "global `%s` already declared here: %s:%i",
3749 var->name, ast_ctx(old).file, ast_ctx(old).line);
3756 else /* it's not a global */
3758 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3759 if (old && !isparam) {
3760 parseerror(parser, "local `%s` already declared here: %s:%i",
3761 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3765 old = parser_find_local(parser, var->name, 0, &isparam);
3766 if (old && isparam) {
3767 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3768 "local `%s` is shadowing a parameter", var->name))
3770 parseerror(parser, "local `%s` already declared here: %s:%i",
3771 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3775 if (opts_standard != COMPILER_GMQCC) {
3784 * Create the global/local, and deal with vector types.
3787 if (var->expression.vtype == TYPE_VECTOR)
3789 else if (var->expression.vtype == TYPE_FIELD &&
3790 var->expression.next->expression.vtype == TYPE_VECTOR)
3794 if (!create_vector_members(var, me)) {
3801 /* deal with global variables, fields, functions */
3802 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3803 vec_push(parser->fields, (ast_expression*)var);
3804 util_htset(parser->htfields, var->name, var);
3806 for (i = 0; i < 3; ++i) {
3807 vec_push(parser->fields, (ast_expression*)me[i]);
3808 util_htset(parser->htfields, me[i]->name, me[i]);
3813 vec_push(parser->globals, (ast_expression*)var);
3814 util_htset(parser->htglobals, var->name, var);
3816 for (i = 0; i < 3; ++i) {
3817 vec_push(parser->globals, (ast_expression*)me[i]);
3818 util_htset(parser->htglobals, me[i]->name, me[i]);
3823 vec_push(localblock->locals, var);
3824 parser_addlocal(parser, var->name, (ast_expression*)var);
3826 for (i = 0; i < 3; ++i) {
3827 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3828 ast_block_collect(localblock, (ast_expression*)me[i]);
3834 me[0] = me[1] = me[2] = NULL;
3839 if (var->expression.vtype == TYPE_ARRAY) {
3841 snprintf(name, sizeof(name), "%s##SET", var->name);
3842 if (!parser_create_array_setter(parser, var, name))
3844 snprintf(name, sizeof(name), "%s##GET", var->name);
3845 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3848 else if (!localblock && !nofields &&
3849 var->expression.vtype == TYPE_FIELD &&
3850 var->expression.next->expression.vtype == TYPE_ARRAY)
3853 ast_expression *telem;
3855 ast_value *array = (ast_value*)var->expression.next;
3857 if (!ast_istype(var->expression.next, ast_value)) {
3858 parseerror(parser, "internal error: field element type must be an ast_value");
3862 snprintf(name, sizeof(name), "%s##SETF", var->name);
3863 if (!parser_create_array_field_setter(parser, array, name))
3866 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3867 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3868 tfield->expression.next = telem;
3869 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3870 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3878 if (parser->tok == ';') {
3879 ast_delete(basetype);
3880 if (!parser_next(parser)) {
3881 parseerror(parser, "error after variable declaration");
3887 if (parser->tok == ',')
3890 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3891 parseerror(parser, "missing comma or semicolon while parsing variables");
3895 if (localblock && opts_standard == COMPILER_QCC) {
3896 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3897 "initializing expression turns variable `%s` into a constant in this standard",
3904 if (parser->tok != '{') {
3905 if (parser->tok != '=') {
3906 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
3910 if (!parser_next(parser)) {
3911 parseerror(parser, "error parsing initializer");
3915 else if (opts_standard == COMPILER_QCC) {
3916 parseerror(parser, "expected '=' before function body in this standard");
3919 if (parser->tok == '#') {
3920 ast_function *func = NULL;
3923 parseerror(parser, "cannot declare builtins within functions");
3926 if (var->expression.vtype != TYPE_FUNCTION) {
3927 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3930 if (!parser_next(parser)) {
3931 parseerror(parser, "expected builtin number");
3934 if (parser->tok != TOKEN_INTCONST) {
3935 parseerror(parser, "builtin number must be an integer constant");
3938 if (parser_token(parser)->constval.i <= 0) {
3939 parseerror(parser, "builtin number must be an integer greater than zero");
3943 if (var->hasvalue) {
3944 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
3945 "builtin `%s` has already been defined\n"
3946 " -> previous declaration here: %s:%i",
3947 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
3951 func = ast_function_new(ast_ctx(var), var->name, var);
3953 parseerror(parser, "failed to allocate function for `%s`", var->name);
3956 vec_push(parser->functions, func);
3958 func->builtin = -parser_token(parser)->constval.i;
3961 if (!parser_next(parser)) {
3962 parseerror(parser, "expected comma or semicolon");
3964 ast_function_delete(func);
3965 var->constval.vfunc = NULL;
3969 else if (parser->tok == '{' || parser->tok == '[')
3973 parseerror(parser, "cannot declare functions within functions");
3977 if (!parse_function_body(parser, var))
3979 ast_delete(basetype);
3980 for (i = 0; i < vec_size(parser->gotos); ++i)
3981 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
3982 vec_free(parser->gotos);
3983 vec_free(parser->labels);
3986 ast_expression *cexp;
3989 cexp = parse_expression_leave(parser, true);
3994 cval = (ast_value*)cexp;
3995 if (!ast_istype(cval, ast_value) || !cval->hasvalue || !cval->constant)
3996 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3999 if (opts_standard != COMPILER_GMQCC &&
4000 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4001 is_const_var != CV_VAR)
4003 var->constant = true;
4005 var->hasvalue = true;
4006 if (cval->expression.vtype == TYPE_STRING)
4007 var->constval.vstring = parser_strdup(cval->constval.vstring);
4009 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
4014 shunt sy = { NULL, NULL };
4015 cvq = var->constant;
4016 var->constant = false;
4017 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
4018 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
4019 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
4020 if (!parser_sy_pop(parser, &sy))
4023 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
4024 parseerror(parser, "internal error: leaked operands");
4025 ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
4029 var->constant = cvq;
4034 if (parser->tok == ',') {
4035 if (!parser_next(parser)) {
4036 parseerror(parser, "expected another variable");
4040 if (parser->tok != TOKEN_IDENT) {
4041 parseerror(parser, "expected another variable");
4044 var = ast_value_copy(basetype);
4046 ast_value_set_name(var, parser_tokval(parser));
4047 if (!parser_next(parser)) {
4048 parseerror(parser, "error parsing variable declaration");
4054 if (parser->tok != ';') {
4055 parseerror(parser, "missing semicolon after variables");
4059 if (!parser_next(parser)) {
4060 parseerror(parser, "parse error after variable declaration");
4064 ast_delete(basetype);
4068 if (cleanvar && var)
4070 ast_delete(basetype);
4074 ast_delete(basetype);
4075 if (cleanvar && var)
4077 if (me[0]) ast_member_delete(me[0]);
4078 if (me[1]) ast_member_delete(me[1]);
4079 if (me[2]) ast_member_delete(me[2]);
4083 static bool parser_global_statement(parser_t *parser)
4085 ast_value *istype = NULL;
4086 if (parser->tok == TOKEN_IDENT)
4087 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4089 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4091 return parse_variable(parser, NULL, false, CV_NONE, istype);
4093 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
4095 if (!strcmp(parser_tokval(parser), "var")) {
4096 if (!parser_next(parser)) {
4097 parseerror(parser, "expected variable declaration after 'var'");
4100 return parse_variable(parser, NULL, true, CV_VAR, NULL);
4103 else if (parser->tok == TOKEN_KEYWORD)
4105 if (!strcmp(parser_tokval(parser), "const")) {
4106 if (!parser_next(parser)) {
4107 parseerror(parser, "expected variable declaration after 'const'");
4110 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var")) {
4111 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `var` after const qualifier");
4112 if (!parser_next(parser)) {
4113 parseerror(parser, "expected variable declaration after 'const var'");
4117 return parse_variable(parser, NULL, true, CV_CONST, NULL);
4119 else if (!strcmp(parser_tokval(parser), "typedef")) {
4120 if (!parser_next(parser)) {
4121 parseerror(parser, "expected type definition after 'typedef'");
4124 return parse_typedef(parser);
4126 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4129 else if (parser->tok == '$')
4131 if (!parser_next(parser)) {
4132 parseerror(parser, "parse error");
4138 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4144 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4146 return util_crc16(old, str, strlen(str));
4149 static void progdefs_crc_file(const char *str)
4151 /* write to progdefs.h here */
4155 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4157 old = progdefs_crc_sum(old, str);
4158 progdefs_crc_file(str);
4162 static void generate_checksum(parser_t *parser)
4164 uint16_t crc = 0xFFFF;
4168 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4169 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4171 progdefs_crc_file("\tint\tpad;\n");
4172 progdefs_crc_file("\tint\tofs_return[3];\n");
4173 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4174 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4175 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4176 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4177 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4178 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4179 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4180 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4182 for (i = 0; i < parser->crc_globals; ++i) {
4183 if (!ast_istype(parser->globals[i], ast_value))
4185 value = (ast_value*)(parser->globals[i]);
4186 switch (value->expression.vtype) {
4187 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4188 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4189 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4190 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4192 crc = progdefs_crc_both(crc, "\tint\t");
4195 crc = progdefs_crc_both(crc, value->name);
4196 crc = progdefs_crc_both(crc, ";\n");
4198 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
4199 for (i = 0; i < parser->crc_fields; ++i) {
4200 if (!ast_istype(parser->fields[i], ast_value))
4202 value = (ast_value*)(parser->fields[i]);
4203 switch (value->expression.next->expression.vtype) {
4204 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4205 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4206 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4207 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4209 crc = progdefs_crc_both(crc, "\tint\t");
4212 crc = progdefs_crc_both(crc, value->name);
4213 crc = progdefs_crc_both(crc, ";\n");
4215 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
4220 static parser_t *parser;
4226 parser = (parser_t*)mem_a(sizeof(parser_t));
4230 memset(parser, 0, sizeof(*parser));
4232 for (i = 0; i < operator_count; ++i) {
4233 if (operators[i].id == opid1('=')) {
4234 parser->assign_op = operators+i;
4238 if (!parser->assign_op) {
4239 printf("internal error: initializing parser: failed to find assign operator\n");
4244 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
4245 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
4246 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
4247 vec_push(parser->_blocktypedefs, 0);
4251 bool parser_compile()
4253 /* initial lexer/parser state */
4254 parser->lex->flags.noops = true;
4256 if (parser_next(parser))
4258 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4260 if (!parser_global_statement(parser)) {
4261 if (parser->tok == TOKEN_EOF)
4262 parseerror(parser, "unexpected eof");
4263 else if (!parser->errors)
4264 parseerror(parser, "there have been errors, bailing out");
4265 lex_close(parser->lex);
4271 parseerror(parser, "parse error");
4272 lex_close(parser->lex);
4277 lex_close(parser->lex);
4280 return !parser->errors;
4283 bool parser_compile_file(const char *filename)
4285 parser->lex = lex_open(filename);
4287 con_err("failed to open file \"%s\"\n", filename);
4290 return parser_compile();
4293 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4295 parser->lex = lex_open_string(str, len, name);
4297 con_err("failed to create lexer for string \"%s\"\n", name);
4300 return parser_compile();
4303 bool parser_compile_string(const char *name, const char *str)
4305 parser->lex = lex_open_string(str, strlen(str), name);
4307 con_err("failed to create lexer for string \"%s\"\n", name);
4310 return parser_compile();
4313 void parser_cleanup()
4316 for (i = 0; i < vec_size(parser->accessors); ++i) {
4317 ast_delete(parser->accessors[i]->constval.vfunc);
4318 parser->accessors[i]->constval.vfunc = NULL;
4319 ast_delete(parser->accessors[i]);
4321 for (i = 0; i < vec_size(parser->functions); ++i) {
4322 ast_delete(parser->functions[i]);
4324 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4325 ast_delete(parser->imm_vector[i]);
4327 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4328 ast_delete(parser->imm_string[i]);
4330 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4331 ast_delete(parser->imm_float[i]);
4333 for (i = 0; i < vec_size(parser->fields); ++i) {
4334 ast_delete(parser->fields[i]);
4336 for (i = 0; i < vec_size(parser->globals); ++i) {
4337 ast_delete(parser->globals[i]);
4339 vec_free(parser->accessors);
4340 vec_free(parser->functions);
4341 vec_free(parser->imm_vector);
4342 vec_free(parser->imm_string);
4343 vec_free(parser->imm_float);
4344 vec_free(parser->globals);
4345 vec_free(parser->fields);
4347 for (i = 0; i < vec_size(parser->variables); ++i)
4348 util_htdel(parser->variables[i]);
4349 vec_free(parser->variables);
4350 vec_free(parser->_blocklocals);
4351 vec_free(parser->_locals);
4353 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4354 ast_delete(parser->_typedefs[i]);
4355 vec_free(parser->_typedefs);
4356 for (i = 0; i < vec_size(parser->typedefs); ++i)
4357 util_htdel(parser->typedefs[i]);
4358 vec_free(parser->typedefs);
4359 vec_free(parser->_blocktypedefs);
4361 vec_free(parser->labels);
4362 vec_free(parser->gotos);
4367 bool parser_finish(const char *output)
4373 if (!parser->errors)
4375 ir = ir_builder_new("gmqcc_out");
4377 con_out("failed to allocate builder\n");
4381 for (i = 0; i < vec_size(parser->fields); ++i) {
4384 if (!ast_istype(parser->fields[i], ast_value))
4386 field = (ast_value*)parser->fields[i];
4387 hasvalue = field->hasvalue;
4388 field->hasvalue = false;
4389 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4390 con_out("failed to generate field %s\n", field->name);
4391 ir_builder_delete(ir);
4396 ast_expression *subtype;
4397 field->hasvalue = true;
4398 subtype = field->expression.next;
4399 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4400 if (subtype->expression.vtype == TYPE_FIELD)
4401 ifld->fieldtype = subtype->expression.next->expression.vtype;
4402 else if (subtype->expression.vtype == TYPE_FUNCTION)
4403 ifld->outtype = subtype->expression.next->expression.vtype;
4404 (void)!ir_value_set_field(field->ir_v, ifld);
4407 for (i = 0; i < vec_size(parser->globals); ++i) {
4409 if (!ast_istype(parser->globals[i], ast_value))
4411 asvalue = (ast_value*)(parser->globals[i]);
4412 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
4413 if (strcmp(asvalue->name, "end_sys_globals") &&
4414 strcmp(asvalue->name, "end_sys_fields"))
4416 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4417 "unused global: `%s`", asvalue->name);
4420 if (!ast_global_codegen(asvalue, ir, false)) {
4421 con_out("failed to generate global %s\n", asvalue->name);
4422 ir_builder_delete(ir);
4426 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4427 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4428 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4429 ir_builder_delete(ir);
4433 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4434 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4435 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4436 ir_builder_delete(ir);
4440 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4441 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4442 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4443 ir_builder_delete(ir);
4447 for (i = 0; i < vec_size(parser->globals); ++i) {
4449 if (!ast_istype(parser->globals[i], ast_value))
4451 asvalue = (ast_value*)(parser->globals[i]);
4452 if (asvalue->setter) {
4453 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4454 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4455 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4457 printf("failed to generate setter for %s\n", asvalue->name);
4458 ir_builder_delete(ir);
4462 if (asvalue->getter) {
4463 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4464 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4465 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4467 printf("failed to generate getter for %s\n", asvalue->name);
4468 ir_builder_delete(ir);
4473 for (i = 0; i < vec_size(parser->fields); ++i) {
4475 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4477 if (!ast_istype((ast_expression*)asvalue, ast_value))
4479 if (asvalue->expression.vtype != TYPE_ARRAY)
4481 if (asvalue->setter) {
4482 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4483 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4484 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4486 printf("failed to generate setter for %s\n", asvalue->name);
4487 ir_builder_delete(ir);
4491 if (asvalue->getter) {
4492 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4493 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4494 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4496 printf("failed to generate getter for %s\n", asvalue->name);
4497 ir_builder_delete(ir);
4502 for (i = 0; i < vec_size(parser->functions); ++i) {
4503 if (!ast_function_codegen(parser->functions[i], ir)) {
4504 con_out("failed to generate function %s\n", parser->functions[i]->name);
4505 ir_builder_delete(ir);
4510 ir_builder_dump(ir, con_out);
4511 for (i = 0; i < vec_size(parser->functions); ++i) {
4512 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4513 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4514 ir_builder_delete(ir);
4521 ir_builder_dump(ir, con_out);
4523 generate_checksum(parser);
4525 if (!ir_builder_generate(ir, output)) {
4526 con_out("*** failed to generate output file\n");
4527 ir_builder_delete(ir);
4532 ir_builder_delete(ir);
4536 con_out("*** there were compile errors\n");