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 ast_binstore *asbinstore;
489 size_t i, assignop, addop, subop;
490 qcint generated_op = 0;
495 if (!vec_size(sy->ops)) {
496 parseerror(parser, "internal error: missing operator");
500 if (vec_last(sy->ops).paren) {
501 parseerror(parser, "unmatched parenthesis");
505 op = &operators[vec_last(sy->ops).etype - 1];
506 ctx = vec_last(sy->ops).ctx;
508 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
510 if (vec_size(sy->out) < op->operands) {
511 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
512 op->op, (int)op->id);
516 vec_shrinkby(sy->ops, 1);
518 vec_shrinkby(sy->out, op->operands);
519 for (i = 0; i < op->operands; ++i) {
520 exprs[i] = sy->out[vec_size(sy->out)+i].out;
521 blocks[i] = sy->out[vec_size(sy->out)+i].block;
522 asvalue[i] = (ast_value*)exprs[i];
525 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
526 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
530 #define NotSameType(T) \
531 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
532 exprs[0]->expression.vtype != T)
533 #define CanConstFold1(A) \
534 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && ((ast_value*)(A))->constant)
535 #define CanConstFold(A, B) \
536 (CanConstFold1(A) && CanConstFold1(B))
537 #define ConstV(i) (asvalue[(i)]->constval.vvec)
538 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
539 #define ConstS(i) (asvalue[(i)]->constval.vstring)
543 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
547 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
548 if (exprs[1]->expression.vtype != TYPE_FIELD) {
549 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
552 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
554 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
555 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
559 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
565 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
566 !(exprs[0]->expression.vtype == TYPE_FIELD &&
567 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
569 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
570 parseerror(parser, "cannot index value of type %s", ty1);
573 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
574 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
575 parseerror(parser, "index must be of type float, not %s", ty1);
578 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
579 if (rotate_entfield_array_index_nodes(&out))
581 if (opts_standard != COMPILER_GMQCC) {
582 /* this error doesn't need to make us bail out */
583 (void)!parsewarning(parser, WARN_EXTENSIONS,
584 "accessing array-field members of an entity without parenthesis\n"
585 " -> this is an extension from -std=gmqcc");
592 ast_block_add_expr(blocks[0], exprs[1]);
594 blocks[0] = ast_block_new(ctx);
595 ast_block_add_expr(blocks[0], exprs[0]);
596 ast_block_add_expr(blocks[0], exprs[1]);
598 if (!ast_block_set_type(blocks[0], exprs[1]))
601 vec_push(sy->out, syblock(ctx, blocks[0]));
608 switch (exprs[0]->expression.vtype) {
610 if (CanConstFold1(exprs[0]))
611 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
613 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
614 (ast_expression*)parser_const_float_0(parser),
618 if (CanConstFold1(exprs[0]))
619 out = (ast_expression*)parser_const_vector_f(parser,
620 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
622 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
623 (ast_expression*)parser_const_vector_0(parser),
627 parseerror(parser, "invalid types used in expression: cannot negate type %s",
628 type_name[exprs[0]->expression.vtype]);
634 switch (exprs[0]->expression.vtype) {
636 if (CanConstFold1(exprs[0]))
637 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
639 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
642 if (CanConstFold1(exprs[0]))
643 out = (ast_expression*)parser_const_float(parser,
644 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
646 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
649 if (CanConstFold1(exprs[0]))
650 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
652 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
654 /* we don't constant-fold NOT for these types */
656 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
659 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
662 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
663 type_name[exprs[0]->expression.vtype]);
669 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
670 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
672 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
673 type_name[exprs[0]->expression.vtype],
674 type_name[exprs[1]->expression.vtype]);
677 switch (exprs[0]->expression.vtype) {
679 if (CanConstFold(exprs[0], exprs[1]))
681 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
684 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
687 if (CanConstFold(exprs[0], exprs[1]))
688 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
690 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
693 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
694 type_name[exprs[0]->expression.vtype],
695 type_name[exprs[1]->expression.vtype]);
700 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
701 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
703 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
704 type_name[exprs[1]->expression.vtype],
705 type_name[exprs[0]->expression.vtype]);
708 switch (exprs[0]->expression.vtype) {
710 if (CanConstFold(exprs[0], exprs[1]))
711 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
713 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
716 if (CanConstFold(exprs[0], exprs[1]))
717 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
719 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
722 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
723 type_name[exprs[1]->expression.vtype],
724 type_name[exprs[0]->expression.vtype]);
729 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
730 exprs[0]->expression.vtype != TYPE_VECTOR &&
731 exprs[0]->expression.vtype != TYPE_FLOAT &&
732 exprs[1]->expression.vtype != TYPE_VECTOR &&
733 exprs[1]->expression.vtype != TYPE_FLOAT)
735 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
736 type_name[exprs[1]->expression.vtype],
737 type_name[exprs[0]->expression.vtype]);
740 switch (exprs[0]->expression.vtype) {
742 if (exprs[1]->expression.vtype == TYPE_VECTOR)
744 if (CanConstFold(exprs[0], exprs[1]))
745 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
747 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
751 if (CanConstFold(exprs[0], exprs[1]))
752 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
754 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
758 if (exprs[1]->expression.vtype == TYPE_FLOAT)
760 if (CanConstFold(exprs[0], exprs[1]))
761 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
763 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
767 if (CanConstFold(exprs[0], exprs[1]))
768 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
770 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
774 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
775 type_name[exprs[1]->expression.vtype],
776 type_name[exprs[0]->expression.vtype]);
781 if (NotSameType(TYPE_FLOAT)) {
782 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
783 type_name[exprs[0]->expression.vtype],
784 type_name[exprs[1]->expression.vtype]);
787 if (CanConstFold(exprs[0], exprs[1]))
788 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
790 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
794 parseerror(parser, "qc does not have a modulo operator");
798 if (NotSameType(TYPE_FLOAT)) {
799 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
800 type_name[exprs[0]->expression.vtype],
801 type_name[exprs[1]->expression.vtype]);
804 if (CanConstFold(exprs[0], exprs[1]))
805 out = (ast_expression*)parser_const_float(parser,
806 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
807 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
809 out = (ast_expression*)ast_binary_new(ctx,
810 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
814 parseerror(parser, "TODO: bitxor");
819 case opid3('<','<','='):
820 case opid3('>','>','='):
821 parseerror(parser, "TODO: shifts");
825 generated_op += 1; /* INSTR_OR */
827 generated_op += INSTR_AND;
829 if (NotSameType(TYPE_FLOAT)) {
830 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
831 type_name[exprs[0]->expression.vtype],
832 type_name[exprs[1]->expression.vtype]);
833 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
834 parseerror(parser, "TODO: optional early out");
838 if (opts_standard == COMPILER_GMQCC)
839 con_out("TODO: early out logic\n");
840 if (CanConstFold(exprs[0], exprs[1]))
841 out = (ast_expression*)parser_const_float(parser,
842 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
844 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
848 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
849 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
850 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
851 parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
854 if (CanConstFold1(exprs[0]))
855 out = (ConstF(0) ? exprs[1] : exprs[2]);
857 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
861 generated_op += 1; /* INSTR_GT */
863 generated_op += 1; /* INSTR_LT */
864 case opid2('>', '='):
865 generated_op += 1; /* INSTR_GE */
866 case opid2('<', '='):
867 generated_op += INSTR_LE;
868 if (NotSameType(TYPE_FLOAT)) {
869 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
870 type_name[exprs[0]->expression.vtype],
871 type_name[exprs[1]->expression.vtype]);
874 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
876 case opid2('!', '='):
877 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
878 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
879 type_name[exprs[0]->expression.vtype],
880 type_name[exprs[1]->expression.vtype]);
883 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
885 case opid2('=', '='):
886 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
887 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
888 type_name[exprs[0]->expression.vtype],
889 type_name[exprs[1]->expression.vtype]);
892 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
896 if (ast_istype(exprs[0], ast_entfield)) {
897 ast_expression *field = ((ast_entfield*)exprs[0])->field;
898 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
899 exprs[0]->expression.vtype == TYPE_FIELD &&
900 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
902 assignop = type_storep_instr[TYPE_VECTOR];
905 assignop = type_storep_instr[exprs[0]->expression.vtype];
906 if (!ast_compare_type(field->expression.next, exprs[1])) {
907 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
908 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
909 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
910 field->expression.next->expression.vtype == TYPE_FUNCTION &&
911 exprs[1]->expression.vtype == TYPE_FUNCTION)
913 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
914 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
920 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
925 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
926 exprs[0]->expression.vtype == TYPE_FIELD &&
927 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
929 assignop = type_store_instr[TYPE_VECTOR];
932 assignop = type_store_instr[exprs[0]->expression.vtype];
935 if (assignop == AINSTR_END) {
936 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
937 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
938 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
940 else if (!ast_compare_type(exprs[0], exprs[1])) {
941 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
942 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
943 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
944 exprs[0]->expression.vtype == TYPE_FUNCTION &&
945 exprs[1]->expression.vtype == TYPE_FUNCTION)
947 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
948 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
954 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
957 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
958 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
960 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
962 case opid3('+','+','P'):
963 case opid3('-','-','P'):
965 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
966 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
967 parseerror(parser, "invalid type for prefix increment: %s", ty1);
970 if (op->id == opid3('+','+','P'))
974 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
975 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
977 if (ast_istype(exprs[0], ast_entfield)) {
978 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
980 (ast_expression*)parser_const_float_1(parser));
982 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
984 (ast_expression*)parser_const_float_1(parser));
987 case opid3('S','+','+'):
988 case opid3('S','-','-'):
990 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
991 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
992 parseerror(parser, "invalid type for suffix increment: %s", ty1);
995 if (op->id == opid3('S','+','+')) {
1000 subop = INSTR_ADD_F;
1002 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1003 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1005 if (ast_istype(exprs[0], ast_entfield)) {
1006 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1008 (ast_expression*)parser_const_float_1(parser));
1010 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1012 (ast_expression*)parser_const_float_1(parser));
1016 out = (ast_expression*)ast_binary_new(ctx, subop,
1018 (ast_expression*)parser_const_float_1(parser));
1020 case opid2('+','='):
1021 case opid2('-','='):
1022 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1023 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1025 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1026 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1027 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1031 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1032 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1034 if (ast_istype(exprs[0], ast_entfield))
1035 assignop = type_storep_instr[exprs[0]->expression.vtype];
1037 assignop = type_store_instr[exprs[0]->expression.vtype];
1038 switch (exprs[0]->expression.vtype) {
1040 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1041 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1042 exprs[0], exprs[1]);
1045 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1046 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1047 exprs[0], exprs[1]);
1050 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1051 type_name[exprs[0]->expression.vtype],
1052 type_name[exprs[1]->expression.vtype]);
1056 case opid2('*','='):
1057 case opid2('/','='):
1058 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1059 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1060 exprs[0]->expression.vtype == TYPE_VECTOR))
1062 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1063 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1064 parseerror(parser, "invalid types used in expression: %s and %s",
1068 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1069 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1071 if (ast_istype(exprs[0], ast_entfield))
1072 assignop = type_storep_instr[exprs[0]->expression.vtype];
1074 assignop = type_store_instr[exprs[0]->expression.vtype];
1075 switch (exprs[0]->expression.vtype) {
1077 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1078 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1079 exprs[0], exprs[1]);
1082 if (op->id == opid2('*','=')) {
1083 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1084 exprs[0], exprs[1]);
1086 /* there's no DIV_VF */
1087 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1088 (ast_expression*)parser_const_float_1(parser),
1092 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1097 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1098 type_name[exprs[0]->expression.vtype],
1099 type_name[exprs[1]->expression.vtype]);
1103 case opid2('&','='):
1104 case opid2('|','='):
1105 if (NotSameType(TYPE_FLOAT)) {
1106 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1107 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1108 parseerror(parser, "invalid types used in expression: %s and %s",
1112 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1113 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1115 if (ast_istype(exprs[0], ast_entfield))
1116 assignop = type_storep_instr[exprs[0]->expression.vtype];
1118 assignop = type_store_instr[exprs[0]->expression.vtype];
1119 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1120 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1121 exprs[0], exprs[1]);
1123 case opid3('&','~','='):
1124 /* This is like: a &= ~(b);
1125 * But QC has no bitwise-not, so we implement it as
1128 if (NotSameType(TYPE_FLOAT)) {
1129 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1130 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1131 parseerror(parser, "invalid types used in expression: %s and %s",
1135 if (ast_istype(exprs[0], ast_entfield))
1136 assignop = type_storep_instr[exprs[0]->expression.vtype];
1138 assignop = type_store_instr[exprs[0]->expression.vtype];
1139 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1142 if (ast_istype(exprs[0], ast_value) && asvalue[0]->constant) {
1143 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1145 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1146 asbinstore->keep_dest = true;
1147 out = (ast_expression*)asbinstore;
1153 parseerror(parser, "failed to apply operand %s", op->op);
1157 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1158 vec_push(sy->out, syexp(ctx, out));
1162 static bool parser_close_call(parser_t *parser, shunt *sy)
1164 /* was a function call */
1165 ast_expression *fun;
1171 vec_shrinkby(sy->ops, 1);
1172 fid = sy->ops[vec_size(sy->ops)].off;
1174 /* out[fid] is the function
1175 * everything above is parameters...
1176 * 0 params = nothing
1177 * 1 params = ast_expression
1181 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1182 parseerror(parser, "internal error: function call needs function and parameter list...");
1186 fun = sy->out[fid].out;
1188 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1190 parseerror(parser, "out of memory");
1194 if (fid+1 == vec_size(sy->out)) {
1197 } else if (fid+2 == vec_size(sy->out)) {
1199 vec_shrinkby(sy->out, 1);
1200 params = sy->out[vec_size(sy->out)].block;
1204 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1206 paramcount = vec_size(params->exprs);
1207 call->params = params->exprs;
1208 params->exprs = NULL;
1211 if (!ast_call_check_types(call))
1214 parseerror(parser, "invalid function call");
1218 /* overwrite fid, the function, with a call */
1219 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1221 if (fun->expression.vtype != TYPE_FUNCTION) {
1222 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1226 if (!fun->expression.next) {
1227 parseerror(parser, "could not determine function return type");
1230 if (vec_size(fun->expression.params) != paramcount &&
1231 !(fun->expression.variadic &&
1232 vec_size(fun->expression.params) < paramcount))
1235 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1237 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1238 if (opts_standard == COMPILER_GMQCC)
1241 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1242 " -> `%s` has been declared here: %s:%i",
1243 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1244 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1246 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1247 " -> `%s` has been declared here: %s:%i",
1248 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1249 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1255 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1256 "too %s parameters for call to %s: expected %i, got %i\n"
1257 " -> `%s` has been declared here: %s:%i",
1258 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1259 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1261 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1262 "too %s parameters for function call: expected %i, got %i\n"
1263 " -> `%s` has been declared here: %s:%i",
1264 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1265 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1273 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1275 if (!vec_size(sy->ops)) {
1276 parseerror(parser, "unmatched closing paren");
1279 /* this would for bit a + (x) because there are no operators inside (x)
1280 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1281 parseerror(parser, "empty parenthesis expression");
1285 while (vec_size(sy->ops)) {
1286 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1287 if (!parser_close_call(parser, sy))
1291 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1292 vec_shrinkby(sy->ops, 1);
1293 return !functions_only;
1295 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1298 /* pop off the parenthesis */
1299 vec_shrinkby(sy->ops, 1);
1300 /* then apply the index operator */
1301 if (!parser_sy_pop(parser, sy))
1305 if (!parser_sy_pop(parser, sy))
1311 static void parser_reclassify_token(parser_t *parser)
1314 for (i = 0; i < operator_count; ++i) {
1315 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1316 parser->tok = TOKEN_OPERATOR;
1322 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1324 ast_expression *expr = NULL;
1326 bool wantop = false;
1327 bool gotmemberof = false;
1329 /* count the parens because an if starts with one, so the
1330 * end of a condition is an unmatched closing paren
1338 parser->lex->flags.noops = false;
1340 parser_reclassify_token(parser);
1345 gotmemberof = false;
1347 parser->memberof = 0;
1349 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1350 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1352 /* a translatable string */
1356 parseerror(parser, "expected operator or end of statement, got constant");
1360 parser->lex->flags.noops = true;
1361 if (!parser_next(parser) || parser->tok != '(') {
1362 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1365 parser->lex->flags.noops = false;
1366 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1367 parseerror(parser, "expected a constant string in translatable-string extension");
1370 val = parser_const_string(parser, parser_tokval(parser), true);
1374 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1375 DEBUGSHUNTDO(con_out("push string\n"));
1377 if (!parser_next(parser) || parser->tok != ')') {
1378 parseerror(parser, "expected closing paren after translatable string");
1382 else if (parser->tok == TOKEN_IDENT)
1384 ast_expression *var;
1386 parseerror(parser, "expected operator or end of statement");
1391 if (opts_standard == COMPILER_GMQCC)
1393 if (parser->memberof == TYPE_ENTITY) {
1394 /* still get vars first since there could be a fieldpointer */
1395 var = parser_find_var(parser, parser_tokval(parser));
1397 var = parser_find_field(parser, parser_tokval(parser));
1399 else if (parser->memberof == TYPE_VECTOR)
1401 parseerror(parser, "TODO: implement effective vector member access");
1404 else if (parser->memberof) {
1405 parseerror(parser, "namespace for member not found");
1409 var = parser_find_var(parser, parser_tokval(parser));
1411 var = parser_find_var(parser, parser_tokval(parser));
1413 var = parser_find_field(parser, parser_tokval(parser));
1416 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1419 if (ast_istype(var, ast_value))
1420 ((ast_value*)var)->uses++;
1421 vec_push(sy.out, syexp(parser_ctx(parser), var));
1422 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1424 else if (parser->tok == TOKEN_FLOATCONST) {
1427 parseerror(parser, "expected operator or end of statement, got constant");
1431 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1434 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1435 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1437 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1440 parseerror(parser, "expected operator or end of statement, got constant");
1444 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1447 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1448 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1450 else if (parser->tok == TOKEN_STRINGCONST) {
1453 parseerror(parser, "expected operator or end of statement, got constant");
1457 val = parser_const_string(parser, parser_tokval(parser), false);
1460 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1461 DEBUGSHUNTDO(con_out("push string\n"));
1463 else if (parser->tok == TOKEN_VECTORCONST) {
1466 parseerror(parser, "expected operator or end of statement, got constant");
1470 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1473 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1474 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1475 parser_token(parser)->constval.v.x,
1476 parser_token(parser)->constval.v.y,
1477 parser_token(parser)->constval.v.z));
1479 else if (parser->tok == '(') {
1480 parseerror(parser, "internal error: '(' should be classified as operator");
1483 else if (parser->tok == '[') {
1484 parseerror(parser, "internal error: '[' should be classified as operator");
1487 else if (parser->tok == ')') {
1489 DEBUGSHUNTDO(con_out("do[op] )\n"));
1493 /* we do expect an operator next */
1494 /* closing an opening paren */
1495 if (!parser_close_paren(parser, &sy, false))
1498 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1502 /* allowed for function calls */
1503 if (!parser_close_paren(parser, &sy, true))
1508 else if (parser->tok == ']') {
1510 parseerror(parser, "operand expected");
1514 if (!parser_close_paren(parser, &sy, false))
1518 else if (parser->tok != TOKEN_OPERATOR) {
1520 parseerror(parser, "expected operator or end of statement");
1527 /* classify the operator */
1528 const oper_info *op;
1529 const oper_info *olast = NULL;
1531 for (o = 0; o < operator_count; ++o) {
1532 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1533 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1534 !strcmp(parser_tokval(parser), operators[o].op))
1539 if (o == operator_count) {
1540 /* no operator found... must be the end of the statement */
1543 /* found an operator */
1546 /* when declaring variables, a comma starts a new variable */
1547 if (op->id == opid1(',') && !parens && stopatcomma) {
1548 /* fixup the token */
1553 /* a colon without a pervious question mark cannot be a ternary */
1554 if (!ternaries && op->id == opid2(':','?')) {
1559 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1560 olast = &operators[vec_last(sy.ops).etype-1];
1563 (op->prec < olast->prec) ||
1564 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1566 if (!parser_sy_pop(parser, &sy))
1568 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1569 olast = &operators[vec_last(sy.ops).etype-1];
1574 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1575 /* for gmqcc standard: open up the namespace of the previous type */
1576 ast_expression *prevex = vec_last(sy.out).out;
1578 parseerror(parser, "unexpected member operator");
1581 if (prevex->expression.vtype == TYPE_ENTITY)
1582 parser->memberof = TYPE_ENTITY;
1583 else if (prevex->expression.vtype == TYPE_VECTOR)
1584 parser->memberof = TYPE_VECTOR;
1586 parseerror(parser, "type error: type has no members");
1592 if (op->id == opid1('(')) {
1594 size_t sycount = vec_size(sy.out);
1595 DEBUGSHUNTDO(con_out("push [op] (\n"));
1597 /* we expected an operator, this is the function-call operator */
1598 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1601 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1602 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1605 } else if (op->id == opid1('[')) {
1607 parseerror(parser, "unexpected array subscript");
1611 /* push both the operator and the paren, this makes life easier */
1612 vec_push(sy.ops, syop(parser_ctx(parser), op));
1613 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1615 } else if (op->id == opid2('?',':')) {
1617 vec_push(sy.ops, syop(parser_ctx(parser), op));
1620 } else if (op->id == opid2(':','?')) {
1621 /* we don't push this operator */
1625 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1626 vec_push(sy.ops, syop(parser_ctx(parser), op));
1627 wantop = !!(op->flags & OP_SUFFIX);
1630 if (!parser_next(parser)) {
1633 if (parser->tok == ';' ||
1634 (!parens && parser->tok == ']'))
1640 while (vec_size(sy.ops)) {
1641 if (!parser_sy_pop(parser, &sy))
1645 parser->lex->flags.noops = true;
1646 if (!vec_size(sy.out)) {
1647 parseerror(parser, "empty expression");
1650 expr = sy.out[0].out;
1653 DEBUGSHUNTDO(con_out("shunt done\n"));
1657 parser->lex->flags.noops = true;
1663 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1665 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1668 if (!parser_next(parser)) {
1675 static void parser_enterblock(parser_t *parser)
1677 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1678 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1679 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1680 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1683 static bool parser_leaveblock(parser_t *parser)
1686 size_t locals, typedefs;
1688 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1689 parseerror(parser, "internal error: parser_leaveblock with no block");
1693 util_htdel(vec_last(parser->variables));
1694 vec_pop(parser->variables);
1695 if (!vec_size(parser->_blocklocals)) {
1696 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1700 locals = vec_last(parser->_blocklocals);
1701 vec_pop(parser->_blocklocals);
1702 while (vec_size(parser->_locals) != locals) {
1703 ast_expression *e = vec_last(parser->_locals);
1704 ast_value *v = (ast_value*)e;
1705 vec_pop(parser->_locals);
1706 if (ast_istype(e, ast_value) && !v->uses) {
1707 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
1712 typedefs = vec_last(parser->_blocktypedefs);
1713 while (vec_size(parser->_typedefs) != typedefs) {
1714 ast_delete(vec_last(parser->_typedefs));
1715 vec_pop(parser->_typedefs);
1717 util_htdel(vec_last(parser->typedefs));
1718 vec_pop(parser->typedefs);
1723 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1725 vec_push(parser->_locals, e);
1726 util_htset(vec_last(parser->variables), name, (void*)e);
1729 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1732 ast_expression *cond, *ontrue, *onfalse = NULL;
1735 lex_ctx ctx = parser_ctx(parser);
1737 (void)block; /* not touching */
1739 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1740 if (!parser_next(parser)) {
1741 parseerror(parser, "expected condition or 'not'");
1744 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
1746 if (!parser_next(parser)) {
1747 parseerror(parser, "expected condition in parenthesis");
1751 if (parser->tok != '(') {
1752 parseerror(parser, "expected 'if' condition in parenthesis");
1755 /* parse into the expression */
1756 if (!parser_next(parser)) {
1757 parseerror(parser, "expected 'if' condition after opening paren");
1760 /* parse the condition */
1761 cond = parse_expression_leave(parser, false);
1765 if (parser->tok != ')') {
1766 parseerror(parser, "expected closing paren after 'if' condition");
1770 /* parse into the 'then' branch */
1771 if (!parser_next(parser)) {
1772 parseerror(parser, "expected statement for on-true branch of 'if'");
1776 ontrue = parse_statement_or_block(parser);
1781 /* check for an else */
1782 if (!strcmp(parser_tokval(parser), "else")) {
1783 /* parse into the 'else' branch */
1784 if (!parser_next(parser)) {
1785 parseerror(parser, "expected on-false branch after 'else'");
1790 onfalse = parse_statement_or_block(parser);
1799 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1801 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1802 *out = (ast_expression*)ifthen;
1806 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1809 ast_expression *cond, *ontrue;
1811 lex_ctx ctx = parser_ctx(parser);
1813 (void)block; /* not touching */
1815 /* skip the 'while' and check for opening paren */
1816 if (!parser_next(parser) || parser->tok != '(') {
1817 parseerror(parser, "expected 'while' condition in parenthesis");
1820 /* parse into the expression */
1821 if (!parser_next(parser)) {
1822 parseerror(parser, "expected 'while' condition after opening paren");
1825 /* parse the condition */
1826 cond = parse_expression_leave(parser, false);
1830 if (parser->tok != ')') {
1831 parseerror(parser, "expected closing paren after 'while' condition");
1835 /* parse into the 'then' branch */
1836 if (!parser_next(parser)) {
1837 parseerror(parser, "expected while-loop body");
1841 ontrue = parse_statement_or_block(parser);
1847 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1848 *out = (ast_expression*)aloop;
1852 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1855 ast_expression *cond, *ontrue;
1857 lex_ctx ctx = parser_ctx(parser);
1859 (void)block; /* not touching */
1861 /* skip the 'do' and get the body */
1862 if (!parser_next(parser)) {
1863 parseerror(parser, "expected loop body");
1866 ontrue = parse_statement_or_block(parser);
1870 /* expect the "while" */
1871 if (parser->tok != TOKEN_KEYWORD ||
1872 strcmp(parser_tokval(parser), "while"))
1874 parseerror(parser, "expected 'while' and condition");
1879 /* skip the 'while' and check for opening paren */
1880 if (!parser_next(parser) || parser->tok != '(') {
1881 parseerror(parser, "expected 'while' condition in parenthesis");
1885 /* parse into the expression */
1886 if (!parser_next(parser)) {
1887 parseerror(parser, "expected 'while' condition after opening paren");
1891 /* parse the condition */
1892 cond = parse_expression_leave(parser, false);
1896 if (parser->tok != ')') {
1897 parseerror(parser, "expected closing paren after 'while' condition");
1903 if (!parser_next(parser) || parser->tok != ';') {
1904 parseerror(parser, "expected semicolon after condition");
1910 if (!parser_next(parser)) {
1911 parseerror(parser, "parse error");
1917 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1918 *out = (ast_expression*)aloop;
1922 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1925 ast_expression *initexpr, *cond, *increment, *ontrue;
1929 lex_ctx ctx = parser_ctx(parser);
1931 parser_enterblock(parser);
1938 /* skip the 'while' and check for opening paren */
1939 if (!parser_next(parser) || parser->tok != '(') {
1940 parseerror(parser, "expected 'for' expressions in parenthesis");
1943 /* parse into the expression */
1944 if (!parser_next(parser)) {
1945 parseerror(parser, "expected 'for' initializer after opening paren");
1950 if (parser->tok == TOKEN_IDENT)
1951 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
1953 if (typevar || parser->tok == TOKEN_TYPENAME) {
1954 if (opts_standard != COMPILER_GMQCC) {
1955 if (parsewarning(parser, WARN_EXTENSIONS,
1956 "current standard does not allow variable declarations in for-loop initializers"))
1959 if (!parse_variable(parser, block, true, CV_VAR, typevar))
1962 else if (parser->tok != ';')
1964 initexpr = parse_expression_leave(parser, false);
1969 /* move on to condition */
1970 if (parser->tok != ';') {
1971 parseerror(parser, "expected semicolon after for-loop initializer");
1974 if (!parser_next(parser)) {
1975 parseerror(parser, "expected for-loop condition");
1979 /* parse the condition */
1980 if (parser->tok != ';') {
1981 cond = parse_expression_leave(parser, false);
1986 /* move on to incrementor */
1987 if (parser->tok != ';') {
1988 parseerror(parser, "expected semicolon after for-loop initializer");
1991 if (!parser_next(parser)) {
1992 parseerror(parser, "expected for-loop condition");
1996 /* parse the incrementor */
1997 if (parser->tok != ')') {
1998 increment = parse_expression_leave(parser, false);
2001 if (!ast_side_effects(increment)) {
2002 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2008 if (parser->tok != ')') {
2009 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2012 /* parse into the 'then' branch */
2013 if (!parser_next(parser)) {
2014 parseerror(parser, "expected for-loop body");
2017 ontrue = parse_statement_or_block(parser);
2022 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
2023 *out = (ast_expression*)aloop;
2025 if (!parser_leaveblock(parser))
2029 if (initexpr) ast_delete(initexpr);
2030 if (cond) ast_delete(cond);
2031 if (increment) ast_delete(increment);
2032 (void)!parser_leaveblock(parser);
2036 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2038 ast_expression *exp = NULL;
2039 ast_return *ret = NULL;
2040 ast_value *expected = parser->function->vtype;
2042 (void)block; /* not touching */
2044 if (!parser_next(parser)) {
2045 parseerror(parser, "expected return expression");
2049 if (parser->tok != ';') {
2050 exp = parse_expression(parser, false);
2054 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2055 parseerror(parser, "return with invalid expression");
2058 ret = ast_return_new(exp->expression.node.context, exp);
2064 if (!parser_next(parser))
2065 parseerror(parser, "parse error");
2066 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2067 if (opts_standard != COMPILER_GMQCC)
2068 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2070 parseerror(parser, "return without value");
2072 ret = ast_return_new(parser_ctx(parser), NULL);
2074 *out = (ast_expression*)ret;
2078 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2080 lex_ctx ctx = parser_ctx(parser);
2082 (void)block; /* not touching */
2084 if (!parser_next(parser) || parser->tok != ';') {
2085 parseerror(parser, "expected semicolon");
2089 if (!parser_next(parser))
2090 parseerror(parser, "parse error");
2092 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2096 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2098 ast_expression *operand;
2100 ast_switch *switchnode;
2101 ast_switch_case swcase;
2103 lex_ctx ctx = parser_ctx(parser);
2105 (void)block; /* not touching */
2107 /* parse over the opening paren */
2108 if (!parser_next(parser) || parser->tok != '(') {
2109 parseerror(parser, "expected switch operand in parenthesis");
2113 /* parse into the expression */
2114 if (!parser_next(parser)) {
2115 parseerror(parser, "expected switch operand");
2118 /* parse the operand */
2119 operand = parse_expression_leave(parser, false);
2123 switchnode = ast_switch_new(ctx, operand);
2126 if (parser->tok != ')') {
2127 ast_delete(switchnode);
2128 parseerror(parser, "expected closing paren after 'switch' operand");
2132 /* parse over the opening paren */
2133 if (!parser_next(parser) || parser->tok != '{') {
2134 ast_delete(switchnode);
2135 parseerror(parser, "expected list of cases");
2139 if (!parser_next(parser)) {
2140 ast_delete(switchnode);
2141 parseerror(parser, "expected 'case' or 'default'");
2146 while (parser->tok != '}') {
2147 ast_block *caseblock;
2149 if (parser->tok != TOKEN_KEYWORD) {
2150 ast_delete(switchnode);
2151 parseerror(parser, "expected 'case' or 'default'");
2154 if (!strcmp(parser_tokval(parser), "case")) {
2155 if (!parser_next(parser)) {
2156 ast_delete(switchnode);
2157 parseerror(parser, "expected expression for case");
2160 swcase.value = parse_expression_leave(parser, false);
2161 if (!swcase.value) {
2162 ast_delete(switchnode);
2163 parseerror(parser, "expected expression for case");
2166 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2167 opval = (ast_value*)swcase.value;
2168 if (!ast_istype(swcase.value, ast_value)) { /* || !opval->constant) { */
2169 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2175 else if (!strcmp(parser_tokval(parser), "default")) {
2176 swcase.value = NULL;
2177 if (!parser_next(parser)) {
2178 ast_delete(switchnode);
2179 parseerror(parser, "expected colon");
2184 /* Now the colon and body */
2185 if (parser->tok != ':') {
2186 if (swcase.value) ast_unref(swcase.value);
2187 ast_delete(switchnode);
2188 parseerror(parser, "expected colon");
2192 if (!parser_next(parser)) {
2193 if (swcase.value) ast_unref(swcase.value);
2194 ast_delete(switchnode);
2195 parseerror(parser, "expected statements or case");
2198 caseblock = ast_block_new(parser_ctx(parser));
2200 if (swcase.value) ast_unref(swcase.value);
2201 ast_delete(switchnode);
2204 swcase.code = (ast_expression*)caseblock;
2205 vec_push(switchnode->cases, swcase);
2207 ast_expression *expr;
2208 if (parser->tok == '}')
2210 if (parser->tok == TOKEN_KEYWORD) {
2211 if (!strcmp(parser_tokval(parser), "case") ||
2212 !strcmp(parser_tokval(parser), "default"))
2217 if (!parse_statement(parser, caseblock, &expr, true)) {
2218 ast_delete(switchnode);
2223 ast_block_add_expr(caseblock, expr);
2228 if (parser->tok != '}') {
2229 ast_delete(switchnode);
2230 parseerror(parser, "expected closing paren of case list");
2233 if (!parser_next(parser)) {
2234 ast_delete(switchnode);
2235 parseerror(parser, "parse error after switch");
2238 *out = (ast_expression*)switchnode;
2242 static bool parse_goto(parser_t *parser, ast_expression **out)
2247 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2248 parseerror(parser, "expected label name after `goto`");
2252 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2254 for (i = 0; i < vec_size(parser->labels); ++i) {
2255 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2256 ast_goto_set_label(gt, parser->labels[i]);
2260 if (i == vec_size(parser->labels))
2261 vec_push(parser->gotos, gt);
2263 if (!parser_next(parser) || parser->tok != ';') {
2264 parseerror(parser, "semicolon expected after goto label");
2267 if (!parser_next(parser)) {
2268 parseerror(parser, "parse error after goto");
2272 *out = (ast_expression*)gt;
2276 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2279 ast_value *typevar = NULL;
2282 if (parser->tok == TOKEN_IDENT)
2283 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2285 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2287 /* local variable */
2289 parseerror(parser, "cannot declare a variable from here");
2292 if (opts_standard == COMPILER_QCC) {
2293 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2296 if (!parse_variable(parser, block, false, CV_NONE, typevar))
2301 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
2305 else if (parser->tok == TOKEN_KEYWORD)
2307 if (!strcmp(parser_tokval(parser), "local") ||
2308 !strcmp(parser_tokval(parser), "const") ||
2309 !strcmp(parser_tokval(parser), "var"))
2312 if (parser_tokval(parser)[0] == 'c')
2314 else if (parser_tokval(parser)[0] == 'v')
2320 parseerror(parser, "cannot declare a local variable here");
2323 if (!parser_next(parser)) {
2324 parseerror(parser, "expected variable declaration");
2327 if (!parse_variable(parser, block, true, cvq, NULL))
2332 else if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
2337 if (!parser_next(parser)) {
2338 parseerror(parser, "parse error after __builtin_debug_printtype");
2342 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
2344 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
2345 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
2346 if (!parser_next(parser)) {
2347 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
2353 if (!parse_statement(parser, block, out, allow_cases))
2356 con_out("__builtin_debug_printtype: got no output node\n");
2359 ast_type_to_string(*out, ty, sizeof(ty));
2360 con_out("__builtin_debug_printtype: `%s`\n", ty);
2365 else if (!strcmp(parser_tokval(parser), "return"))
2367 return parse_return(parser, block, out);
2369 else if (!strcmp(parser_tokval(parser), "if"))
2371 return parse_if(parser, block, out);
2373 else if (!strcmp(parser_tokval(parser), "while"))
2375 return parse_while(parser, block, out);
2377 else if (!strcmp(parser_tokval(parser), "do"))
2379 return parse_dowhile(parser, block, out);
2381 else if (!strcmp(parser_tokval(parser), "for"))
2383 if (opts_standard == COMPILER_QCC) {
2384 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2387 return parse_for(parser, block, out);
2389 else if (!strcmp(parser_tokval(parser), "break"))
2391 return parse_break_continue(parser, block, out, false);
2393 else if (!strcmp(parser_tokval(parser), "continue"))
2395 return parse_break_continue(parser, block, out, true);
2397 else if (!strcmp(parser_tokval(parser), "switch"))
2399 return parse_switch(parser, block, out);
2401 else if (!strcmp(parser_tokval(parser), "case") ||
2402 !strcmp(parser_tokval(parser), "default"))
2405 parseerror(parser, "unexpected 'case' label");
2410 else if (!strcmp(parser_tokval(parser), "goto"))
2412 return parse_goto(parser, out);
2414 else if (!strcmp(parser_tokval(parser), "typedef"))
2416 if (!parser_next(parser)) {
2417 parseerror(parser, "expected type definition after 'typedef'");
2420 return parse_typedef(parser);
2422 parseerror(parser, "Unexpected keyword");
2425 else if (parser->tok == '{')
2428 inner = parse_block(parser, false);
2431 *out = (ast_expression*)inner;
2434 else if (parser->tok == ':')
2438 if (!parser_next(parser)) {
2439 parseerror(parser, "expected label name");
2442 if (parser->tok != TOKEN_IDENT) {
2443 parseerror(parser, "label must be an identifier");
2446 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
2449 vec_push(parser->labels, label);
2450 *out = (ast_expression*)label;
2451 if (!parser_next(parser)) {
2452 parseerror(parser, "parse error after label");
2455 for (i = 0; i < vec_size(parser->gotos); ++i) {
2456 if (!strcmp(parser->gotos[i]->name, label->name)) {
2457 ast_goto_set_label(parser->gotos[i], label);
2458 vec_remove(parser->gotos, i, 1);
2464 else if (parser->tok == ';')
2466 if (!parser_next(parser)) {
2467 parseerror(parser, "parse error after empty statement");
2474 ast_expression *exp = parse_expression(parser, false);
2478 if (!ast_side_effects(exp)) {
2479 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2486 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
2490 parser_enterblock(parser);
2492 if (!parser_next(parser)) { /* skip the '{' */
2493 parseerror(parser, "expected function body");
2497 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2499 ast_expression *expr = NULL;
2500 if (parser->tok == '}')
2503 if (!parse_statement(parser, block, &expr, false)) {
2504 /* parseerror(parser, "parse error"); */
2510 ast_block_add_expr(block, expr);
2513 if (parser->tok != '}') {
2516 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
2518 if (!vec_size(block->exprs) ||
2519 !ast_istype(vec_last(block->exprs), ast_return))
2521 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
2527 (void)parser_next(parser);
2531 if (!parser_leaveblock(parser))
2533 return retval && !!block;
2536 static ast_block* parse_block(parser_t *parser, bool warnreturn)
2539 block = ast_block_new(parser_ctx(parser));
2542 if (!parse_block_into(parser, block, warnreturn)) {
2543 ast_block_delete(block);
2549 static ast_expression* parse_statement_or_block(parser_t *parser)
2551 ast_expression *expr = NULL;
2552 if (parser->tok == '{')
2553 return (ast_expression*)parse_block(parser, false);
2554 if (!parse_statement(parser, NULL, &expr, false))
2559 static bool create_vector_members(ast_value *var, ast_member **me)
2562 size_t len = strlen(var->name);
2564 for (i = 0; i < 3; ++i) {
2565 char *name = mem_a(len+3);
2566 memcpy(name, var->name, len);
2568 name[len+1] = 'x'+i;
2570 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2579 do { ast_member_delete(me[--i]); } while(i);
2583 static bool parse_function_body(parser_t *parser, ast_value *var)
2585 ast_block *block = NULL;
2590 ast_expression *framenum = NULL;
2591 ast_expression *nextthink = NULL;
2592 /* None of the following have to be deleted */
2593 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2594 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2595 bool has_frame_think;
2599 has_frame_think = false;
2600 old = parser->function;
2602 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
2603 parseerror(parser, "gotos/labels leaking");
2607 if (var->expression.variadic) {
2608 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2609 "variadic function with implementation will not be able to access additional parameters"))
2615 if (parser->tok == '[') {
2616 /* got a frame definition: [ framenum, nextthink ]
2617 * this translates to:
2618 * self.frame = framenum;
2619 * self.nextthink = time + 0.1;
2620 * self.think = nextthink;
2624 fld_think = parser_find_field(parser, "think");
2625 fld_nextthink = parser_find_field(parser, "nextthink");
2626 fld_frame = parser_find_field(parser, "frame");
2627 if (!fld_think || !fld_nextthink || !fld_frame) {
2628 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2629 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2632 gbl_time = parser_find_global(parser, "time");
2633 gbl_self = parser_find_global(parser, "self");
2634 if (!gbl_time || !gbl_self) {
2635 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2636 parseerror(parser, "please declare the following globals: `time`, `self`");
2640 if (!parser_next(parser))
2643 framenum = parse_expression_leave(parser, true);
2645 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2648 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
2649 ast_unref(framenum);
2650 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2654 if (parser->tok != ',') {
2655 ast_unref(framenum);
2656 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2657 parseerror(parser, "Got a %i\n", parser->tok);
2661 if (!parser_next(parser)) {
2662 ast_unref(framenum);
2666 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2668 /* qc allows the use of not-yet-declared functions here
2669 * - this automatically creates a prototype */
2670 ast_value *thinkfunc;
2671 ast_expression *functype = fld_think->expression.next;
2673 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2674 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2675 ast_unref(framenum);
2676 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2680 if (!parser_next(parser)) {
2681 ast_unref(framenum);
2682 ast_delete(thinkfunc);
2686 vec_push(parser->globals, (ast_expression*)thinkfunc);
2687 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2688 nextthink = (ast_expression*)thinkfunc;
2691 nextthink = parse_expression_leave(parser, true);
2693 ast_unref(framenum);
2694 parseerror(parser, "expected a think-function in [frame,think] notation");
2699 if (!ast_istype(nextthink, ast_value)) {
2700 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2704 if (retval && parser->tok != ']') {
2705 parseerror(parser, "expected closing `]` for [frame,think] notation");
2709 if (retval && !parser_next(parser)) {
2713 if (retval && parser->tok != '{') {
2714 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2719 ast_unref(nextthink);
2720 ast_unref(framenum);
2724 has_frame_think = true;
2727 block = ast_block_new(parser_ctx(parser));
2729 parseerror(parser, "failed to allocate block");
2730 if (has_frame_think) {
2731 ast_unref(nextthink);
2732 ast_unref(framenum);
2737 if (has_frame_think) {
2739 ast_expression *self_frame;
2740 ast_expression *self_nextthink;
2741 ast_expression *self_think;
2742 ast_expression *time_plus_1;
2743 ast_store *store_frame;
2744 ast_store *store_nextthink;
2745 ast_store *store_think;
2747 ctx = parser_ctx(parser);
2748 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2749 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2750 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2752 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2753 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2755 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2756 if (self_frame) ast_delete(self_frame);
2757 if (self_nextthink) ast_delete(self_nextthink);
2758 if (self_think) ast_delete(self_think);
2759 if (time_plus_1) ast_delete(time_plus_1);
2765 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2766 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2767 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2770 ast_delete(self_frame);
2773 if (!store_nextthink) {
2774 ast_delete(self_nextthink);
2778 ast_delete(self_think);
2782 if (store_frame) ast_delete(store_frame);
2783 if (store_nextthink) ast_delete(store_nextthink);
2784 if (store_think) ast_delete(store_think);
2787 ast_block_add_expr(block, (ast_expression*)store_frame);
2788 ast_block_add_expr(block, (ast_expression*)store_nextthink);
2789 ast_block_add_expr(block, (ast_expression*)store_think);
2793 parseerror(parser, "failed to generate code for [frame,think]");
2794 ast_unref(nextthink);
2795 ast_unref(framenum);
2801 parser_enterblock(parser);
2803 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2805 ast_value *param = var->expression.params[parami];
2808 if (param->expression.vtype != TYPE_VECTOR &&
2809 (param->expression.vtype != TYPE_FIELD ||
2810 param->expression.next->expression.vtype != TYPE_VECTOR))
2815 if (!create_vector_members(param, me)) {
2816 ast_block_delete(block);
2820 for (e = 0; e < 3; ++e) {
2821 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2822 ast_block_collect(block, (ast_expression*)me[e]);
2826 func = ast_function_new(ast_ctx(var), var->name, var);
2828 parseerror(parser, "failed to allocate function for `%s`", var->name);
2829 ast_block_delete(block);
2832 vec_push(parser->functions, func);
2834 parser->function = func;
2835 if (!parse_block_into(parser, block, true)) {
2836 ast_block_delete(block);
2840 vec_push(func->blocks, block);
2842 parser->function = old;
2843 if (!parser_leaveblock(parser))
2845 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2846 parseerror(parser, "internal error: local scopes left");
2850 if (parser->tok == ';')
2851 return parser_next(parser);
2852 else if (opts_standard == COMPILER_QCC)
2853 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2857 vec_pop(parser->functions);
2858 ast_function_delete(func);
2859 var->constval.vfunc = NULL;
2862 (void)!parser_leaveblock(parser);
2863 parser->function = old;
2867 static ast_expression *array_accessor_split(
2872 ast_expression *left,
2873 ast_expression *right
2879 lex_ctx ctx = ast_ctx(array);
2881 if (!left || !right) {
2882 if (left) ast_delete(left);
2883 if (right) ast_delete(right);
2887 cmp = ast_binary_new(ctx, INSTR_LT,
2888 (ast_expression*)index,
2889 (ast_expression*)parser_const_float(parser, middle));
2893 parseerror(parser, "internal error: failed to create comparison for array setter");
2897 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2899 ast_delete(cmp); /* will delete left and right */
2900 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2904 return (ast_expression*)ifthen;
2907 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2909 lex_ctx ctx = ast_ctx(array);
2911 if (from+1 == afterend) {
2912 /* set this value */
2915 ast_array_index *subscript;
2917 int assignop = type_store_instr[value->expression.vtype];
2919 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2920 assignop = INSTR_STORE_V;
2922 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2926 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2928 ast_delete(subscript);
2932 block = ast_block_new(ctx);
2938 ast_block_add_expr(block, (ast_expression*)st);
2940 ret = ast_return_new(ctx, NULL);
2946 ast_block_add_expr(block, (ast_expression*)ret);
2948 return (ast_expression*)block;
2950 ast_expression *left, *right;
2951 size_t diff = afterend - from;
2952 size_t middle = from + diff/2;
2953 left = array_setter_node(parser, array, index, value, from, middle);
2954 right = array_setter_node(parser, array, index, value, middle, afterend);
2955 return array_accessor_split(parser, array, index, middle, left, right);
2959 static ast_expression *array_field_setter_node(
2968 lex_ctx ctx = ast_ctx(array);
2970 if (from+1 == afterend) {
2971 /* set this value */
2974 ast_entfield *entfield;
2975 ast_array_index *subscript;
2977 int assignop = type_storep_instr[value->expression.vtype];
2979 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2980 assignop = INSTR_STOREP_V;
2982 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2986 entfield = ast_entfield_new_force(ctx,
2987 (ast_expression*)entity,
2988 (ast_expression*)subscript,
2989 (ast_expression*)subscript);
2991 ast_delete(subscript);
2995 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2997 ast_delete(entfield);
3001 block = ast_block_new(ctx);
3007 ast_block_add_expr(block, (ast_expression*)st);
3009 ret = ast_return_new(ctx, NULL);
3015 ast_block_add_expr(block, (ast_expression*)ret);
3017 return (ast_expression*)block;
3019 ast_expression *left, *right;
3020 size_t diff = afterend - from;
3021 size_t middle = from + diff/2;
3022 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
3023 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
3024 return array_accessor_split(parser, array, index, middle, left, right);
3028 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
3030 lex_ctx ctx = ast_ctx(array);
3032 if (from+1 == afterend) {
3034 ast_array_index *subscript;
3036 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3040 ret = ast_return_new(ctx, (ast_expression*)subscript);
3042 ast_delete(subscript);
3046 return (ast_expression*)ret;
3048 ast_expression *left, *right;
3049 size_t diff = afterend - from;
3050 size_t middle = from + diff/2;
3051 left = array_getter_node(parser, array, index, from, middle);
3052 right = array_getter_node(parser, array, index, middle, afterend);
3053 return array_accessor_split(parser, array, index, middle, left, right);
3057 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3059 ast_function *func = NULL;
3060 ast_value *fval = NULL;
3061 ast_block *body = NULL;
3063 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3065 parseerror(parser, "failed to create accessor function value");
3069 func = ast_function_new(ast_ctx(array), funcname, fval);
3072 parseerror(parser, "failed to create accessor function node");
3076 body = ast_block_new(ast_ctx(array));
3078 parseerror(parser, "failed to create block for array accessor");
3084 vec_push(func->blocks, body);
3087 vec_push(parser->accessors, fval);
3092 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3094 ast_expression *root = NULL;
3095 ast_value *index = NULL;
3096 ast_value *value = NULL;
3100 if (!ast_istype(array->expression.next, ast_value)) {
3101 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3105 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3107 func = fval->constval.vfunc;
3108 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3110 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3111 value = ast_value_copy((ast_value*)array->expression.next);
3113 if (!index || !value) {
3114 parseerror(parser, "failed to create locals for array accessor");
3117 (void)!ast_value_set_name(value, "value"); /* not important */
3118 vec_push(fval->expression.params, index);
3119 vec_push(fval->expression.params, value);
3121 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3123 parseerror(parser, "failed to build accessor search tree");
3127 ast_block_add_expr(func->blocks[0], root);
3128 array->setter = fval;
3131 if (index) ast_delete(index);
3132 if (value) ast_delete(value);
3133 if (root) ast_delete(root);
3139 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3141 ast_expression *root = NULL;
3142 ast_value *entity = NULL;
3143 ast_value *index = NULL;
3144 ast_value *value = NULL;
3148 if (!ast_istype(array->expression.next, ast_value)) {
3149 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3153 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3155 func = fval->constval.vfunc;
3156 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3158 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3159 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3160 value = ast_value_copy((ast_value*)array->expression.next);
3161 if (!entity || !index || !value) {
3162 parseerror(parser, "failed to create locals for array accessor");
3165 (void)!ast_value_set_name(value, "value"); /* not important */
3166 vec_push(fval->expression.params, entity);
3167 vec_push(fval->expression.params, index);
3168 vec_push(fval->expression.params, value);
3170 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3172 parseerror(parser, "failed to build accessor search tree");
3176 ast_block_add_expr(func->blocks[0], root);
3177 array->setter = fval;
3180 if (entity) ast_delete(entity);
3181 if (index) ast_delete(index);
3182 if (value) ast_delete(value);
3183 if (root) ast_delete(root);
3189 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3191 ast_expression *root = NULL;
3192 ast_value *index = NULL;
3196 /* NOTE: checking array->expression.next rather than elemtype since
3197 * for fields elemtype is a temporary fieldtype.
3199 if (!ast_istype(array->expression.next, ast_value)) {
3200 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3204 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3206 func = fval->constval.vfunc;
3207 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3209 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3212 parseerror(parser, "failed to create locals for array accessor");
3215 vec_push(fval->expression.params, index);
3217 root = array_getter_node(parser, array, index, 0, array->expression.count);
3219 parseerror(parser, "failed to build accessor search tree");
3223 ast_block_add_expr(func->blocks[0], root);
3224 array->getter = fval;
3227 if (index) ast_delete(index);
3228 if (root) ast_delete(root);
3234 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3235 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3243 bool variadic = false;
3245 ctx = parser_ctx(parser);
3247 /* for the sake of less code we parse-in in this function */
3248 if (!parser_next(parser)) {
3249 parseerror(parser, "expected parameter list");
3255 /* parse variables until we hit a closing paren */
3256 while (parser->tok != ')') {
3258 /* there must be commas between them */
3259 if (parser->tok != ',') {
3260 parseerror(parser, "expected comma or end of parameter list");
3263 if (!parser_next(parser)) {
3264 parseerror(parser, "expected parameter");
3270 if (parser->tok == TOKEN_DOTS) {
3271 /* '...' indicates a varargs function */
3273 if (!parser_next(parser)) {
3274 parseerror(parser, "expected parameter");
3277 if (parser->tok != ')') {
3278 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3284 /* for anything else just parse a typename */
3285 param = parse_typename(parser, NULL, NULL);
3288 vec_push(params, param);
3289 if (param->expression.vtype >= TYPE_VARIANT) {
3290 char typename[1024];
3291 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3292 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3298 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3302 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3303 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3306 if (!parser_next(parser)) {
3307 parseerror(parser, "parse error after typename");
3311 /* now turn 'var' into a function type */
3312 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3313 fval->expression.next = (ast_expression*)var;
3314 fval->expression.variadic = variadic;
3317 var->expression.params = params;
3324 for (i = 0; i < vec_size(params); ++i)
3325 ast_delete(params[i]);
3330 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3332 ast_expression *cexp;
3333 ast_value *cval, *tmp;
3336 ctx = parser_ctx(parser);
3338 if (!parser_next(parser)) {
3340 parseerror(parser, "expected array-size");
3344 cexp = parse_expression_leave(parser, true);
3346 if (!cexp || !ast_istype(cexp, ast_value)) {
3350 parseerror(parser, "expected array-size as constant positive integer");
3353 cval = (ast_value*)cexp;
3355 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3356 tmp->expression.next = (ast_expression*)var;
3359 if (cval->expression.vtype == TYPE_INTEGER)
3360 tmp->expression.count = cval->constval.vint;
3361 else if (cval->expression.vtype == TYPE_FLOAT)
3362 tmp->expression.count = cval->constval.vfloat;
3366 parseerror(parser, "array-size must be a positive integer constant");
3371 if (parser->tok != ']') {
3373 parseerror(parser, "expected ']' after array-size");
3376 if (!parser_next(parser)) {
3378 parseerror(parser, "error after parsing array size");
3384 /* Parse a complete typename.
3385 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3386 * but when parsing variables separated by comma
3387 * 'storebase' should point to where the base-type should be kept.
3388 * The base type makes up every bit of type information which comes *before* the
3391 * The following will be parsed in its entirety:
3393 * The 'basetype' in this case is 'void()'
3394 * and if there's a comma after it, say:
3396 * then the type-information 'void()' can be stored in 'storebase'
3398 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3400 ast_value *var, *tmp;
3403 const char *name = NULL;
3404 bool isfield = false;
3405 bool wasarray = false;
3406 size_t morefields = 0;
3408 ctx = parser_ctx(parser);
3410 /* types may start with a dot */
3411 if (parser->tok == '.') {
3413 /* if we parsed a dot we need a typename now */
3414 if (!parser_next(parser)) {
3415 parseerror(parser, "expected typename for field definition");
3419 /* Further dots are handled seperately because they won't be part of the
3422 while (parser->tok == '.') {
3424 if (!parser_next(parser)) {
3425 parseerror(parser, "expected typename for field definition");
3430 if (parser->tok == TOKEN_IDENT)
3431 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
3432 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3433 parseerror(parser, "expected typename");
3437 /* generate the basic type value */
3438 if (cached_typedef) {
3439 var = ast_value_copy(cached_typedef);
3440 ast_value_set_name(var, "<type(from_def)>");
3442 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3444 for (; morefields; --morefields) {
3445 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
3446 tmp->expression.next = (ast_expression*)var;
3450 /* do not yet turn into a field - remember:
3451 * .void() foo; is a field too
3452 * .void()() foo; is a function
3456 if (!parser_next(parser)) {
3458 parseerror(parser, "parse error after typename");
3462 /* an opening paren now starts the parameter-list of a function
3463 * this is where original-QC has parameter lists.
3464 * We allow a single parameter list here.
3465 * Much like fteqcc we don't allow `float()() x`
3467 if (parser->tok == '(') {
3468 var = parse_parameter_list(parser, var);
3473 /* store the base if requested */
3475 *storebase = ast_value_copy(var);
3477 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3478 tmp->expression.next = (ast_expression*)*storebase;
3483 /* there may be a name now */
3484 if (parser->tok == TOKEN_IDENT) {
3485 name = util_strdup(parser_tokval(parser));
3487 if (!parser_next(parser)) {
3489 parseerror(parser, "error after variable or field declaration");
3494 /* now this may be an array */
3495 if (parser->tok == '[') {
3497 var = parse_arraysize(parser, var);
3502 /* This is the point where we can turn it into a field */
3504 /* turn it into a field if desired */
3505 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3506 tmp->expression.next = (ast_expression*)var;
3510 /* now there may be function parens again */
3511 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3512 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3513 if (parser->tok == '(' && wasarray)
3514 parseerror(parser, "arrays as part of a return type is not supported");
3515 while (parser->tok == '(') {
3516 var = parse_parameter_list(parser, var);
3525 /* finally name it */
3527 if (!ast_value_set_name(var, name)) {
3529 parseerror(parser, "internal error: failed to set name");
3532 /* free the name, ast_value_set_name duplicates */
3539 static bool parse_typedef(parser_t *parser)
3541 ast_value *typevar, *oldtype;
3542 ast_expression *old;
3544 typevar = parse_typename(parser, NULL, NULL);
3549 if ( (old = parser_find_var(parser, typevar->name)) ) {
3550 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3551 " -> `%s` has been declared here: %s:%i",
3552 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3553 ast_delete(typevar);
3557 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
3558 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3559 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3560 ast_delete(typevar);
3564 vec_push(parser->_typedefs, typevar);
3565 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3567 if (parser->tok != ';') {
3568 parseerror(parser, "expected semicolon after typedef");
3571 if (!parser_next(parser)) {
3572 parseerror(parser, "parse error after typedef");
3579 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef)
3583 ast_expression *old;
3587 ast_value *basetype = NULL;
3589 bool isparam = false;
3590 bool isvector = false;
3591 bool cleanvar = true;
3592 bool wasarray = false;
3596 /* get the first complete variable */
3597 var = parse_typename(parser, &basetype, cached_typedef);
3600 ast_delete(basetype);
3608 /* Part 0: finish the type */
3609 if (parser->tok == '(') {
3610 if (opts_standard == COMPILER_QCC)
3611 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3612 var = parse_parameter_list(parser, var);
3618 /* we only allow 1-dimensional arrays */
3619 if (parser->tok == '[') {
3621 var = parse_arraysize(parser, var);
3627 if (parser->tok == '(' && wasarray) {
3628 parseerror(parser, "arrays as part of a return type is not supported");
3629 /* we'll still parse the type completely for now */
3631 /* for functions returning functions */
3632 while (parser->tok == '(') {
3633 if (opts_standard == COMPILER_QCC)
3634 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3635 var = parse_parameter_list(parser, var);
3642 if (is_const_var == CV_CONST)
3643 var->constant = true;
3646 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3647 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3648 * is then filled with the previous definition and the parameter-names replaced.
3651 /* Deal with end_sys_ vars */
3653 if (!strcmp(var->name, "end_sys_globals")) {
3654 parser->crc_globals = vec_size(parser->globals);
3657 else if (!strcmp(var->name, "end_sys_fields")) {
3658 parser->crc_fields = vec_size(parser->fields);
3661 if (was_end && var->expression.vtype == TYPE_FIELD) {
3662 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3663 "global '%s' hint should not be a field",
3664 parser_tokval(parser)))
3671 if (!nofields && var->expression.vtype == TYPE_FIELD)
3673 /* deal with field declarations */
3674 old = parser_find_field(parser, var->name);
3676 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3677 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3686 parseerror(parser, "field `%s` already declared here: %s:%i",
3687 var->name, ast_ctx(old).file, ast_ctx(old).line);
3692 if (opts_standard == COMPILER_QCC &&
3693 (old = parser_find_global(parser, var->name)))
3695 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3696 parseerror(parser, "field `%s` already declared here: %s:%i",
3697 var->name, ast_ctx(old).file, ast_ctx(old).line);
3704 /* deal with other globals */
3705 old = parser_find_global(parser, var->name);
3706 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3708 /* This is a function which had a prototype */
3709 if (!ast_istype(old, ast_value)) {
3710 parseerror(parser, "internal error: prototype is not an ast_value");
3714 proto = (ast_value*)old;
3715 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3716 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3718 ast_ctx(proto).file, ast_ctx(proto).line);
3722 /* we need the new parameter-names */
3723 for (i = 0; i < vec_size(proto->expression.params); ++i)
3724 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3732 if (opts_standard == COMPILER_GMQCC) {
3733 parseerror(parser, "global `%s` already declared here: %s:%i",
3734 var->name, ast_ctx(old).file, ast_ctx(old).line);
3738 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3739 "global `%s` already declared here: %s:%i",
3740 var->name, ast_ctx(old).file, ast_ctx(old).line))
3745 proto = (ast_value*)old;
3746 if (!ast_istype(old, ast_value)) {
3747 parseerror(parser, "internal error: not an ast_value");
3756 if (opts_standard == COMPILER_QCC &&
3757 (old = parser_find_field(parser, var->name)))
3759 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3760 parseerror(parser, "global `%s` already declared here: %s:%i",
3761 var->name, ast_ctx(old).file, ast_ctx(old).line);
3768 else /* it's not a global */
3770 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3771 if (old && !isparam) {
3772 parseerror(parser, "local `%s` already declared here: %s:%i",
3773 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3777 old = parser_find_local(parser, var->name, 0, &isparam);
3778 if (old && isparam) {
3779 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3780 "local `%s` is shadowing a parameter", var->name))
3782 parseerror(parser, "local `%s` already declared here: %s:%i",
3783 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3787 if (opts_standard != COMPILER_GMQCC) {
3796 * Create the global/local, and deal with vector types.
3799 if (var->expression.vtype == TYPE_VECTOR)
3801 else if (var->expression.vtype == TYPE_FIELD &&
3802 var->expression.next->expression.vtype == TYPE_VECTOR)
3806 if (!create_vector_members(var, me)) {
3813 /* deal with global variables, fields, functions */
3814 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3815 vec_push(parser->fields, (ast_expression*)var);
3816 util_htset(parser->htfields, var->name, var);
3818 for (i = 0; i < 3; ++i) {
3819 vec_push(parser->fields, (ast_expression*)me[i]);
3820 util_htset(parser->htfields, me[i]->name, me[i]);
3825 vec_push(parser->globals, (ast_expression*)var);
3826 util_htset(parser->htglobals, var->name, var);
3828 for (i = 0; i < 3; ++i) {
3829 vec_push(parser->globals, (ast_expression*)me[i]);
3830 util_htset(parser->htglobals, me[i]->name, me[i]);
3835 vec_push(localblock->locals, var);
3836 parser_addlocal(parser, var->name, (ast_expression*)var);
3838 for (i = 0; i < 3; ++i) {
3839 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3840 ast_block_collect(localblock, (ast_expression*)me[i]);
3846 me[0] = me[1] = me[2] = NULL;
3851 if (var->expression.vtype == TYPE_ARRAY) {
3853 snprintf(name, sizeof(name), "%s##SET", var->name);
3854 if (!parser_create_array_setter(parser, var, name))
3856 snprintf(name, sizeof(name), "%s##GET", var->name);
3857 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3860 else if (!localblock && !nofields &&
3861 var->expression.vtype == TYPE_FIELD &&
3862 var->expression.next->expression.vtype == TYPE_ARRAY)
3865 ast_expression *telem;
3867 ast_value *array = (ast_value*)var->expression.next;
3869 if (!ast_istype(var->expression.next, ast_value)) {
3870 parseerror(parser, "internal error: field element type must be an ast_value");
3874 snprintf(name, sizeof(name), "%s##SETF", var->name);
3875 if (!parser_create_array_field_setter(parser, array, name))
3878 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3879 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3880 tfield->expression.next = telem;
3881 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3882 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3890 if (parser->tok == ';') {
3891 ast_delete(basetype);
3892 if (!parser_next(parser)) {
3893 parseerror(parser, "error after variable declaration");
3899 if (parser->tok == ',')
3902 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3903 parseerror(parser, "missing comma or semicolon while parsing variables");
3907 if (localblock && opts_standard == COMPILER_QCC) {
3908 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3909 "initializing expression turns variable `%s` into a constant in this standard",
3916 if (parser->tok != '{') {
3917 if (parser->tok != '=') {
3918 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
3922 if (!parser_next(parser)) {
3923 parseerror(parser, "error parsing initializer");
3927 else if (opts_standard == COMPILER_QCC) {
3928 parseerror(parser, "expected '=' before function body in this standard");
3931 if (parser->tok == '#') {
3932 ast_function *func = NULL;
3935 parseerror(parser, "cannot declare builtins within functions");
3938 if (var->expression.vtype != TYPE_FUNCTION) {
3939 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3942 if (!parser_next(parser)) {
3943 parseerror(parser, "expected builtin number");
3946 if (parser->tok != TOKEN_INTCONST) {
3947 parseerror(parser, "builtin number must be an integer constant");
3950 if (parser_token(parser)->constval.i <= 0) {
3951 parseerror(parser, "builtin number must be an integer greater than zero");
3955 if (var->hasvalue) {
3956 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
3957 "builtin `%s` has already been defined\n"
3958 " -> previous declaration here: %s:%i",
3959 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
3963 func = ast_function_new(ast_ctx(var), var->name, var);
3965 parseerror(parser, "failed to allocate function for `%s`", var->name);
3968 vec_push(parser->functions, func);
3970 func->builtin = -parser_token(parser)->constval.i;
3973 if (!parser_next(parser)) {
3974 parseerror(parser, "expected comma or semicolon");
3976 ast_function_delete(func);
3977 var->constval.vfunc = NULL;
3981 else if (parser->tok == '{' || parser->tok == '[')
3985 parseerror(parser, "cannot declare functions within functions");
3989 if (!parse_function_body(parser, var))
3991 ast_delete(basetype);
3992 for (i = 0; i < vec_size(parser->gotos); ++i)
3993 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
3994 vec_free(parser->gotos);
3995 vec_free(parser->labels);
3998 ast_expression *cexp;
4001 cexp = parse_expression_leave(parser, true);
4006 cval = (ast_value*)cexp;
4007 if (!ast_istype(cval, ast_value) || !cval->hasvalue || !cval->constant)
4008 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
4011 if (opts_standard != COMPILER_GMQCC &&
4012 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4013 is_const_var != CV_VAR)
4015 var->constant = true;
4017 var->hasvalue = true;
4018 if (cval->expression.vtype == TYPE_STRING)
4019 var->constval.vstring = parser_strdup(cval->constval.vstring);
4021 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
4026 shunt sy = { NULL, NULL };
4027 cvq = var->constant;
4028 var->constant = false;
4029 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
4030 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
4031 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
4032 if (!parser_sy_pop(parser, &sy))
4035 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
4036 parseerror(parser, "internal error: leaked operands");
4037 ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
4041 var->constant = cvq;
4046 if (parser->tok == ',') {
4047 if (!parser_next(parser)) {
4048 parseerror(parser, "expected another variable");
4052 if (parser->tok != TOKEN_IDENT) {
4053 parseerror(parser, "expected another variable");
4056 var = ast_value_copy(basetype);
4058 ast_value_set_name(var, parser_tokval(parser));
4059 if (!parser_next(parser)) {
4060 parseerror(parser, "error parsing variable declaration");
4066 if (parser->tok != ';') {
4067 parseerror(parser, "missing semicolon after variables");
4071 if (!parser_next(parser)) {
4072 parseerror(parser, "parse error after variable declaration");
4076 ast_delete(basetype);
4080 if (cleanvar && var)
4082 ast_delete(basetype);
4086 ast_delete(basetype);
4087 if (cleanvar && var)
4089 if (me[0]) ast_member_delete(me[0]);
4090 if (me[1]) ast_member_delete(me[1]);
4091 if (me[2]) ast_member_delete(me[2]);
4095 static bool parser_global_statement(parser_t *parser)
4097 ast_value *istype = NULL;
4098 if (parser->tok == TOKEN_IDENT)
4099 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4101 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4103 return parse_variable(parser, NULL, false, CV_NONE, istype);
4105 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
4107 if (!strcmp(parser_tokval(parser), "var")) {
4108 if (!parser_next(parser)) {
4109 parseerror(parser, "expected variable declaration after 'var'");
4112 return parse_variable(parser, NULL, true, CV_VAR, NULL);
4115 else if (parser->tok == TOKEN_KEYWORD)
4117 if (!strcmp(parser_tokval(parser), "const")) {
4118 if (!parser_next(parser)) {
4119 parseerror(parser, "expected variable declaration after 'const'");
4122 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var")) {
4123 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `var` after const qualifier");
4124 if (!parser_next(parser)) {
4125 parseerror(parser, "expected variable declaration after 'const var'");
4129 return parse_variable(parser, NULL, true, CV_CONST, NULL);
4131 else if (!strcmp(parser_tokval(parser), "typedef")) {
4132 if (!parser_next(parser)) {
4133 parseerror(parser, "expected type definition after 'typedef'");
4136 return parse_typedef(parser);
4138 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4141 else if (parser->tok == '$')
4143 if (!parser_next(parser)) {
4144 parseerror(parser, "parse error");
4150 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4156 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4158 return util_crc16(old, str, strlen(str));
4161 static void progdefs_crc_file(const char *str)
4163 /* write to progdefs.h here */
4167 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4169 old = progdefs_crc_sum(old, str);
4170 progdefs_crc_file(str);
4174 static void generate_checksum(parser_t *parser)
4176 uint16_t crc = 0xFFFF;
4180 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4181 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4183 progdefs_crc_file("\tint\tpad;\n");
4184 progdefs_crc_file("\tint\tofs_return[3];\n");
4185 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4186 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4187 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4188 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4189 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4190 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4191 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4192 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4194 for (i = 0; i < parser->crc_globals; ++i) {
4195 if (!ast_istype(parser->globals[i], ast_value))
4197 value = (ast_value*)(parser->globals[i]);
4198 switch (value->expression.vtype) {
4199 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4200 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4201 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4202 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4204 crc = progdefs_crc_both(crc, "\tint\t");
4207 crc = progdefs_crc_both(crc, value->name);
4208 crc = progdefs_crc_both(crc, ";\n");
4210 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
4211 for (i = 0; i < parser->crc_fields; ++i) {
4212 if (!ast_istype(parser->fields[i], ast_value))
4214 value = (ast_value*)(parser->fields[i]);
4215 switch (value->expression.next->expression.vtype) {
4216 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4217 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4218 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4219 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4221 crc = progdefs_crc_both(crc, "\tint\t");
4224 crc = progdefs_crc_both(crc, value->name);
4225 crc = progdefs_crc_both(crc, ";\n");
4227 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
4232 static parser_t *parser;
4238 parser = (parser_t*)mem_a(sizeof(parser_t));
4242 memset(parser, 0, sizeof(*parser));
4244 for (i = 0; i < operator_count; ++i) {
4245 if (operators[i].id == opid1('=')) {
4246 parser->assign_op = operators+i;
4250 if (!parser->assign_op) {
4251 printf("internal error: initializing parser: failed to find assign operator\n");
4256 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
4257 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
4258 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
4259 vec_push(parser->_blocktypedefs, 0);
4263 bool parser_compile()
4265 /* initial lexer/parser state */
4266 parser->lex->flags.noops = true;
4268 if (parser_next(parser))
4270 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4272 if (!parser_global_statement(parser)) {
4273 if (parser->tok == TOKEN_EOF)
4274 parseerror(parser, "unexpected eof");
4275 else if (!parser->errors)
4276 parseerror(parser, "there have been errors, bailing out");
4277 lex_close(parser->lex);
4283 parseerror(parser, "parse error");
4284 lex_close(parser->lex);
4289 lex_close(parser->lex);
4292 return !parser->errors;
4295 bool parser_compile_file(const char *filename)
4297 parser->lex = lex_open(filename);
4299 con_err("failed to open file \"%s\"\n", filename);
4302 return parser_compile();
4305 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4307 parser->lex = lex_open_string(str, len, name);
4309 con_err("failed to create lexer for string \"%s\"\n", name);
4312 return parser_compile();
4315 bool parser_compile_string(const char *name, const char *str)
4317 parser->lex = lex_open_string(str, strlen(str), name);
4319 con_err("failed to create lexer for string \"%s\"\n", name);
4322 return parser_compile();
4325 void parser_cleanup()
4328 for (i = 0; i < vec_size(parser->accessors); ++i) {
4329 ast_delete(parser->accessors[i]->constval.vfunc);
4330 parser->accessors[i]->constval.vfunc = NULL;
4331 ast_delete(parser->accessors[i]);
4333 for (i = 0; i < vec_size(parser->functions); ++i) {
4334 ast_delete(parser->functions[i]);
4336 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4337 ast_delete(parser->imm_vector[i]);
4339 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4340 ast_delete(parser->imm_string[i]);
4342 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4343 ast_delete(parser->imm_float[i]);
4345 for (i = 0; i < vec_size(parser->fields); ++i) {
4346 ast_delete(parser->fields[i]);
4348 for (i = 0; i < vec_size(parser->globals); ++i) {
4349 ast_delete(parser->globals[i]);
4351 vec_free(parser->accessors);
4352 vec_free(parser->functions);
4353 vec_free(parser->imm_vector);
4354 vec_free(parser->imm_string);
4355 vec_free(parser->imm_float);
4356 vec_free(parser->globals);
4357 vec_free(parser->fields);
4359 for (i = 0; i < vec_size(parser->variables); ++i)
4360 util_htdel(parser->variables[i]);
4361 vec_free(parser->variables);
4362 vec_free(parser->_blocklocals);
4363 vec_free(parser->_locals);
4365 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4366 ast_delete(parser->_typedefs[i]);
4367 vec_free(parser->_typedefs);
4368 for (i = 0; i < vec_size(parser->typedefs); ++i)
4369 util_htdel(parser->typedefs[i]);
4370 vec_free(parser->typedefs);
4371 vec_free(parser->_blocktypedefs);
4373 vec_free(parser->labels);
4374 vec_free(parser->gotos);
4379 bool parser_finish(const char *output)
4385 if (!parser->errors)
4387 ir = ir_builder_new("gmqcc_out");
4389 con_out("failed to allocate builder\n");
4393 for (i = 0; i < vec_size(parser->fields); ++i) {
4396 if (!ast_istype(parser->fields[i], ast_value))
4398 field = (ast_value*)parser->fields[i];
4399 hasvalue = field->hasvalue;
4400 field->hasvalue = false;
4401 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4402 con_out("failed to generate field %s\n", field->name);
4403 ir_builder_delete(ir);
4408 ast_expression *subtype;
4409 field->hasvalue = true;
4410 subtype = field->expression.next;
4411 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4412 if (subtype->expression.vtype == TYPE_FIELD)
4413 ifld->fieldtype = subtype->expression.next->expression.vtype;
4414 else if (subtype->expression.vtype == TYPE_FUNCTION)
4415 ifld->outtype = subtype->expression.next->expression.vtype;
4416 (void)!ir_value_set_field(field->ir_v, ifld);
4419 for (i = 0; i < vec_size(parser->globals); ++i) {
4421 if (!ast_istype(parser->globals[i], ast_value))
4423 asvalue = (ast_value*)(parser->globals[i]);
4424 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
4425 if (strcmp(asvalue->name, "end_sys_globals") &&
4426 strcmp(asvalue->name, "end_sys_fields"))
4428 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4429 "unused global: `%s`", asvalue->name);
4432 if (!ast_global_codegen(asvalue, ir, false)) {
4433 con_out("failed to generate global %s\n", asvalue->name);
4434 ir_builder_delete(ir);
4438 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4439 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4440 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4441 ir_builder_delete(ir);
4445 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4446 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4447 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4448 ir_builder_delete(ir);
4452 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4453 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4454 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4455 ir_builder_delete(ir);
4459 for (i = 0; i < vec_size(parser->globals); ++i) {
4461 if (!ast_istype(parser->globals[i], ast_value))
4463 asvalue = (ast_value*)(parser->globals[i]);
4464 if (asvalue->setter) {
4465 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4466 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4467 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4469 printf("failed to generate setter for %s\n", asvalue->name);
4470 ir_builder_delete(ir);
4474 if (asvalue->getter) {
4475 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4476 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4477 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4479 printf("failed to generate getter for %s\n", asvalue->name);
4480 ir_builder_delete(ir);
4485 for (i = 0; i < vec_size(parser->fields); ++i) {
4487 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4489 if (!ast_istype((ast_expression*)asvalue, ast_value))
4491 if (asvalue->expression.vtype != TYPE_ARRAY)
4493 if (asvalue->setter) {
4494 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4495 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4496 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4498 printf("failed to generate setter for %s\n", asvalue->name);
4499 ir_builder_delete(ir);
4503 if (asvalue->getter) {
4504 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4505 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4506 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4508 printf("failed to generate getter for %s\n", asvalue->name);
4509 ir_builder_delete(ir);
4514 for (i = 0; i < vec_size(parser->functions); ++i) {
4515 if (!ast_function_codegen(parser->functions[i], ir)) {
4516 con_out("failed to generate function %s\n", parser->functions[i]->name);
4517 ir_builder_delete(ir);
4522 ir_builder_dump(ir, con_out);
4523 for (i = 0; i < vec_size(parser->functions); ++i) {
4524 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4525 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4526 ir_builder_delete(ir);
4533 ir_builder_dump(ir, con_out);
4535 generate_checksum(parser);
4537 if (!ir_builder_generate(ir, output)) {
4538 con_out("*** failed to generate output file\n");
4539 ir_builder_delete(ir);
4544 ir_builder_delete(ir);
4548 con_out("*** there were compile errors\n");