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;
82 /* we store the '=' operator info */
83 const oper_info *assign_op;
85 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
86 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
87 * anything else: type error
92 static void parser_enterblock(parser_t *parser);
93 static bool parser_leaveblock(parser_t *parser);
94 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
95 static bool parse_typedef(parser_t *parser);
96 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef);
97 static ast_block* parse_block(parser_t *parser);
98 static bool parse_block_into(parser_t *parser, ast_block *block);
99 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
100 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
101 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
102 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
104 static void parseerror(parser_t *parser, const char *fmt, ...)
111 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
115 /* returns true if it counts as an error */
116 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
119 int lvl = LVL_WARNING;
121 if (!OPTS_WARN(warntype))
130 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
136 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
139 int lvl = LVL_WARNING;
141 if (!OPTS_WARN(warntype))
148 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
154 /**********************************************************************
155 * some maths used for constant folding
158 vector vec3_add(vector a, vector b)
167 vector vec3_sub(vector a, vector b)
176 qcfloat vec3_mulvv(vector a, vector b)
178 return (a.x * b.x + a.y * b.y + a.z * b.z);
181 vector vec3_mulvf(vector a, float b)
190 /**********************************************************************
194 bool parser_next(parser_t *parser)
196 /* lex_do kills the previous token */
197 parser->tok = lex_do(parser->lex);
198 if (parser->tok == TOKEN_EOF)
200 if (parser->tok >= TOKEN_ERROR) {
201 parseerror(parser, "lex error");
207 #define parser_tokval(p) ((p)->lex->tok.value)
208 #define parser_token(p) (&((p)->lex->tok))
209 #define parser_ctx(p) ((p)->lex->tok.ctx)
211 static ast_value* parser_const_float(parser_t *parser, double d)
215 for (i = 0; i < vec_size(parser->imm_float); ++i) {
216 const double compare = parser->imm_float[i]->constval.vfloat;
217 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
218 return parser->imm_float[i];
220 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
222 out->hasvalue = true;
223 out->constval.vfloat = d;
224 vec_push(parser->imm_float, out);
228 static ast_value* parser_const_float_0(parser_t *parser)
230 if (!parser->imm_float_zero)
231 parser->imm_float_zero = parser_const_float(parser, 0);
232 return parser->imm_float_zero;
235 static ast_value* parser_const_float_1(parser_t *parser)
237 if (!parser->imm_float_one)
238 parser->imm_float_one = parser_const_float(parser, 1);
239 return parser->imm_float_one;
242 static char *parser_strdup(const char *str)
245 /* actually dup empty strings */
246 char *out = mem_a(1);
250 return util_strdup(str);
253 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
257 for (i = 0; i < vec_size(parser->imm_string); ++i) {
258 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
259 return parser->imm_string[i];
263 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
264 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
266 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
268 out->hasvalue = true;
269 out->constval.vstring = parser_strdup(str);
270 vec_push(parser->imm_string, out);
274 static ast_value* parser_const_vector(parser_t *parser, vector v)
278 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
279 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
280 return parser->imm_vector[i];
282 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
284 out->hasvalue = true;
285 out->constval.vvec = v;
286 vec_push(parser->imm_vector, out);
290 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
296 return parser_const_vector(parser, v);
299 static ast_value* parser_const_vector_0(parser_t *parser)
301 if (!parser->imm_vector_zero)
302 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
303 return parser->imm_vector_zero;
306 static ast_expression* parser_find_field(parser_t *parser, const char *name)
308 return util_htget(parser->htfields, name);
311 static ast_expression* parser_find_global(parser_t *parser, const char *name)
313 return util_htget(parser->htglobals, name);
316 static ast_expression* parser_find_param(parser_t *parser, const char *name)
320 if (!parser->function)
322 fun = parser->function->vtype;
323 for (i = 0; i < vec_size(fun->expression.params); ++i) {
324 if (!strcmp(fun->expression.params[i]->name, name))
325 return (ast_expression*)(fun->expression.params[i]);
330 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
335 hash = util_hthash(parser->htglobals, name);
338 for (i = vec_size(parser->variables); i > upto;) {
340 if ( (e = util_htgeth(parser->variables[i], name, hash)) )
344 return parser_find_param(parser, name);
347 static ast_expression* parser_find_var(parser_t *parser, const char *name)
351 v = parser_find_local(parser, name, 0, &dummy);
352 if (!v) v = parser_find_global(parser, name);
356 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
360 hash = util_hthash(parser->typedefs[0], name);
362 for (i = vec_size(parser->typedefs); i > upto;) {
364 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
372 size_t etype; /* 0 = expression, others are operators */
376 ast_block *block; /* for commas and function calls */
385 #define SY_PAREN_EXPR '('
386 #define SY_PAREN_FUNC 'f'
387 #define SY_PAREN_INDEX '['
388 #define SY_PAREN_TERNARY '?'
390 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
401 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
405 e.out = (ast_expression*)v;
412 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
414 e.etype = 1 + (op - operators);
423 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
435 # define DEBUGSHUNTDO(x) x
437 # define DEBUGSHUNTDO(x)
440 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
441 * so we need to rotate it to become ent.(foo[n]).
443 static bool rotate_entfield_array_index_nodes(ast_expression **out)
445 ast_array_index *index;
446 ast_entfield *entfield;
450 ast_expression *entity;
452 lex_ctx ctx = ast_ctx(*out);
454 if (!ast_istype(*out, ast_array_index))
456 index = (ast_array_index*)*out;
458 if (!ast_istype(index->array, ast_entfield))
460 entfield = (ast_entfield*)index->array;
462 if (!ast_istype(entfield->field, ast_value))
464 field = (ast_value*)entfield->field;
467 entity = entfield->entity;
471 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
472 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
473 *out = (ast_expression*)entfield;
478 static bool parser_sy_pop(parser_t *parser, shunt *sy)
482 ast_expression *out = NULL;
483 ast_expression *exprs[3];
484 ast_block *blocks[3];
485 ast_value *asvalue[3];
486 ast_binstore *asbinstore;
487 size_t i, assignop, addop, subop;
488 qcint generated_op = 0;
493 if (!vec_size(sy->ops)) {
494 parseerror(parser, "internal error: missing operator");
498 if (vec_last(sy->ops).paren) {
499 parseerror(parser, "unmatched parenthesis");
503 op = &operators[vec_last(sy->ops).etype - 1];
504 ctx = vec_last(sy->ops).ctx;
506 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
508 if (vec_size(sy->out) < op->operands) {
509 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
510 op->op, (int)op->id);
514 vec_shrinkby(sy->ops, 1);
516 vec_shrinkby(sy->out, op->operands);
517 for (i = 0; i < op->operands; ++i) {
518 exprs[i] = sy->out[vec_size(sy->out)+i].out;
519 blocks[i] = sy->out[vec_size(sy->out)+i].block;
520 asvalue[i] = (ast_value*)exprs[i];
523 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
524 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
528 #define NotSameType(T) \
529 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
530 exprs[0]->expression.vtype != T)
531 #define CanConstFold1(A) \
532 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && (((ast_value*)(A))->cvq == CV_CONST))
533 #define CanConstFold(A, B) \
534 (CanConstFold1(A) && CanConstFold1(B))
535 #define ConstV(i) (asvalue[(i)]->constval.vvec)
536 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
537 #define ConstS(i) (asvalue[(i)]->constval.vstring)
541 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
545 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
546 if (exprs[1]->expression.vtype != TYPE_FIELD) {
547 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
550 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
552 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
553 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
557 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
563 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
564 !(exprs[0]->expression.vtype == TYPE_FIELD &&
565 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
567 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
568 parseerror(parser, "cannot index value of type %s", ty1);
571 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
572 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
573 parseerror(parser, "index must be of type float, not %s", ty1);
576 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
577 if (rotate_entfield_array_index_nodes(&out))
580 /* This is not broken in fteqcc anymore */
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");
593 ast_block_add_expr(blocks[0], exprs[1]);
595 blocks[0] = ast_block_new(ctx);
596 ast_block_add_expr(blocks[0], exprs[0]);
597 ast_block_add_expr(blocks[0], exprs[1]);
599 if (!ast_block_set_type(blocks[0], exprs[1]))
602 vec_push(sy->out, syblock(ctx, blocks[0]));
609 switch (exprs[0]->expression.vtype) {
611 if (CanConstFold1(exprs[0]))
612 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
614 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
615 (ast_expression*)parser_const_float_0(parser),
619 if (CanConstFold1(exprs[0]))
620 out = (ast_expression*)parser_const_vector_f(parser,
621 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
623 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
624 (ast_expression*)parser_const_vector_0(parser),
628 parseerror(parser, "invalid types used in expression: cannot negate type %s",
629 type_name[exprs[0]->expression.vtype]);
635 switch (exprs[0]->expression.vtype) {
637 if (CanConstFold1(exprs[0]))
638 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
640 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
643 if (CanConstFold1(exprs[0]))
644 out = (ast_expression*)parser_const_float(parser,
645 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
647 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
650 if (CanConstFold1(exprs[0]))
651 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
653 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
655 /* we don't constant-fold NOT for these types */
657 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
660 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
663 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
664 type_name[exprs[0]->expression.vtype]);
670 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
671 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
673 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
674 type_name[exprs[0]->expression.vtype],
675 type_name[exprs[1]->expression.vtype]);
678 switch (exprs[0]->expression.vtype) {
680 if (CanConstFold(exprs[0], exprs[1]))
682 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
685 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
688 if (CanConstFold(exprs[0], exprs[1]))
689 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
691 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
694 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
695 type_name[exprs[0]->expression.vtype],
696 type_name[exprs[1]->expression.vtype]);
701 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
702 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
704 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
705 type_name[exprs[1]->expression.vtype],
706 type_name[exprs[0]->expression.vtype]);
709 switch (exprs[0]->expression.vtype) {
711 if (CanConstFold(exprs[0], exprs[1]))
712 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
714 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
717 if (CanConstFold(exprs[0], exprs[1]))
718 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
720 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
723 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
724 type_name[exprs[1]->expression.vtype],
725 type_name[exprs[0]->expression.vtype]);
730 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
731 exprs[0]->expression.vtype != TYPE_VECTOR &&
732 exprs[0]->expression.vtype != TYPE_FLOAT &&
733 exprs[1]->expression.vtype != TYPE_VECTOR &&
734 exprs[1]->expression.vtype != TYPE_FLOAT)
736 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
737 type_name[exprs[1]->expression.vtype],
738 type_name[exprs[0]->expression.vtype]);
741 switch (exprs[0]->expression.vtype) {
743 if (exprs[1]->expression.vtype == TYPE_VECTOR)
745 if (CanConstFold(exprs[0], exprs[1]))
746 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
748 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
752 if (CanConstFold(exprs[0], exprs[1]))
753 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
755 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
759 if (exprs[1]->expression.vtype == TYPE_FLOAT)
761 if (CanConstFold(exprs[0], exprs[1]))
762 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
764 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
768 if (CanConstFold(exprs[0], exprs[1]))
769 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
771 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
775 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
776 type_name[exprs[1]->expression.vtype],
777 type_name[exprs[0]->expression.vtype]);
782 if (NotSameType(TYPE_FLOAT)) {
783 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
784 type_name[exprs[0]->expression.vtype],
785 type_name[exprs[1]->expression.vtype]);
788 if (CanConstFold(exprs[0], exprs[1]))
789 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
791 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
795 parseerror(parser, "qc does not have a modulo operator");
799 if (NotSameType(TYPE_FLOAT)) {
800 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
801 type_name[exprs[0]->expression.vtype],
802 type_name[exprs[1]->expression.vtype]);
805 if (CanConstFold(exprs[0], exprs[1]))
806 out = (ast_expression*)parser_const_float(parser,
807 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
808 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
810 out = (ast_expression*)ast_binary_new(ctx,
811 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
815 parseerror(parser, "TODO: bitxor");
820 case opid3('<','<','='):
821 case opid3('>','>','='):
822 parseerror(parser, "TODO: shifts");
826 generated_op += 1; /* INSTR_OR */
828 generated_op += INSTR_AND;
830 if (NotSameType(TYPE_FLOAT)) {
831 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
832 type_name[exprs[0]->expression.vtype],
833 type_name[exprs[1]->expression.vtype]);
834 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
835 parseerror(parser, "TODO: optional early out");
839 if (opts_standard == COMPILER_GMQCC)
840 con_out("TODO: early out logic\n");
841 if (CanConstFold(exprs[0], exprs[1]))
842 out = (ast_expression*)parser_const_float(parser,
843 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
845 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
849 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
850 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
851 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
852 parseerror(parser, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
855 if (CanConstFold1(exprs[0]))
856 out = (ConstF(0) ? exprs[1] : exprs[2]);
858 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
862 generated_op += 1; /* INSTR_GT */
864 generated_op += 1; /* INSTR_LT */
865 case opid2('>', '='):
866 generated_op += 1; /* INSTR_GE */
867 case opid2('<', '='):
868 generated_op += INSTR_LE;
869 if (NotSameType(TYPE_FLOAT)) {
870 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
871 type_name[exprs[0]->expression.vtype],
872 type_name[exprs[1]->expression.vtype]);
875 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
877 case opid2('!', '='):
878 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
879 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
880 type_name[exprs[0]->expression.vtype],
881 type_name[exprs[1]->expression.vtype]);
884 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
886 case opid2('=', '='):
887 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
888 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
889 type_name[exprs[0]->expression.vtype],
890 type_name[exprs[1]->expression.vtype]);
893 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
897 if (ast_istype(exprs[0], ast_entfield)) {
898 ast_expression *field = ((ast_entfield*)exprs[0])->field;
899 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
900 exprs[0]->expression.vtype == TYPE_FIELD &&
901 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
903 assignop = type_storep_instr[TYPE_VECTOR];
906 assignop = type_storep_instr[exprs[0]->expression.vtype];
907 if (!ast_compare_type(field->expression.next, exprs[1])) {
908 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
909 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
910 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
911 field->expression.next->expression.vtype == TYPE_FUNCTION &&
912 exprs[1]->expression.vtype == TYPE_FUNCTION)
914 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
915 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
921 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
926 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
927 exprs[0]->expression.vtype == TYPE_FIELD &&
928 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
930 assignop = type_store_instr[TYPE_VECTOR];
933 assignop = type_store_instr[exprs[0]->expression.vtype];
936 if (assignop == AINSTR_END) {
937 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
938 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
939 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
941 else if (!ast_compare_type(exprs[0], exprs[1])) {
942 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
943 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
944 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
945 exprs[0]->expression.vtype == TYPE_FUNCTION &&
946 exprs[1]->expression.vtype == TYPE_FUNCTION)
948 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
949 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
955 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
958 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
959 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
961 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
963 case opid3('+','+','P'):
964 case opid3('-','-','P'):
966 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
967 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
968 parseerror(parser, "invalid type for prefix increment: %s", ty1);
971 if (op->id == opid3('+','+','P'))
975 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
976 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
978 if (ast_istype(exprs[0], ast_entfield)) {
979 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
981 (ast_expression*)parser_const_float_1(parser));
983 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
985 (ast_expression*)parser_const_float_1(parser));
988 case opid3('S','+','+'):
989 case opid3('S','-','-'):
991 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
992 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
993 parseerror(parser, "invalid type for suffix increment: %s", ty1);
996 if (op->id == opid3('S','+','+')) {
1000 addop = INSTR_SUB_F;
1001 subop = INSTR_ADD_F;
1003 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1004 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1006 if (ast_istype(exprs[0], ast_entfield)) {
1007 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1009 (ast_expression*)parser_const_float_1(parser));
1011 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1013 (ast_expression*)parser_const_float_1(parser));
1017 out = (ast_expression*)ast_binary_new(ctx, subop,
1019 (ast_expression*)parser_const_float_1(parser));
1021 case opid2('+','='):
1022 case opid2('-','='):
1023 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1024 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1026 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1027 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1028 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1032 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1033 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1035 if (ast_istype(exprs[0], ast_entfield))
1036 assignop = type_storep_instr[exprs[0]->expression.vtype];
1038 assignop = type_store_instr[exprs[0]->expression.vtype];
1039 switch (exprs[0]->expression.vtype) {
1041 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1042 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1043 exprs[0], exprs[1]);
1046 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1047 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1048 exprs[0], exprs[1]);
1051 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1052 type_name[exprs[0]->expression.vtype],
1053 type_name[exprs[1]->expression.vtype]);
1057 case opid2('*','='):
1058 case opid2('/','='):
1059 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1060 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1061 exprs[0]->expression.vtype == TYPE_VECTOR))
1063 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1064 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1065 parseerror(parser, "invalid types used in expression: %s and %s",
1069 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1070 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1072 if (ast_istype(exprs[0], ast_entfield))
1073 assignop = type_storep_instr[exprs[0]->expression.vtype];
1075 assignop = type_store_instr[exprs[0]->expression.vtype];
1076 switch (exprs[0]->expression.vtype) {
1078 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1079 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1080 exprs[0], exprs[1]);
1083 if (op->id == opid2('*','=')) {
1084 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1085 exprs[0], exprs[1]);
1087 /* there's no DIV_VF */
1088 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1089 (ast_expression*)parser_const_float_1(parser),
1093 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1098 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1099 type_name[exprs[0]->expression.vtype],
1100 type_name[exprs[1]->expression.vtype]);
1104 case opid2('&','='):
1105 case opid2('|','='):
1106 if (NotSameType(TYPE_FLOAT)) {
1107 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1108 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1109 parseerror(parser, "invalid types used in expression: %s and %s",
1113 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1114 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1116 if (ast_istype(exprs[0], ast_entfield))
1117 assignop = type_storep_instr[exprs[0]->expression.vtype];
1119 assignop = type_store_instr[exprs[0]->expression.vtype];
1120 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1121 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1122 exprs[0], exprs[1]);
1124 case opid3('&','~','='):
1125 /* This is like: a &= ~(b);
1126 * But QC has no bitwise-not, so we implement it as
1129 if (NotSameType(TYPE_FLOAT)) {
1130 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1131 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1132 parseerror(parser, "invalid types used in expression: %s and %s",
1136 if (ast_istype(exprs[0], ast_entfield))
1137 assignop = type_storep_instr[exprs[0]->expression.vtype];
1139 assignop = type_store_instr[exprs[0]->expression.vtype];
1140 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1143 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1144 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1146 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1147 asbinstore->keep_dest = true;
1148 out = (ast_expression*)asbinstore;
1154 parseerror(parser, "failed to apply operand %s", op->op);
1158 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1159 vec_push(sy->out, syexp(ctx, out));
1163 static bool parser_close_call(parser_t *parser, shunt *sy)
1165 /* was a function call */
1166 ast_expression *fun;
1172 vec_shrinkby(sy->ops, 1);
1173 fid = sy->ops[vec_size(sy->ops)].off;
1175 /* out[fid] is the function
1176 * everything above is parameters...
1177 * 0 params = nothing
1178 * 1 params = ast_expression
1182 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1183 parseerror(parser, "internal error: function call needs function and parameter list...");
1187 fun = sy->out[fid].out;
1189 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1191 parseerror(parser, "out of memory");
1195 if (fid+1 == vec_size(sy->out)) {
1198 } else if (fid+2 == vec_size(sy->out)) {
1200 vec_shrinkby(sy->out, 1);
1201 params = sy->out[vec_size(sy->out)].block;
1205 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1207 paramcount = vec_size(params->exprs);
1208 call->params = params->exprs;
1209 params->exprs = NULL;
1212 if (!ast_call_check_types(call))
1215 parseerror(parser, "invalid function call");
1219 /* overwrite fid, the function, with a call */
1220 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1222 if (fun->expression.vtype != TYPE_FUNCTION) {
1223 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1227 if (!fun->expression.next) {
1228 parseerror(parser, "could not determine function return type");
1231 if (vec_size(fun->expression.params) != paramcount &&
1232 !(fun->expression.variadic &&
1233 vec_size(fun->expression.params) < paramcount))
1236 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1238 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1239 if (opts_standard == COMPILER_GMQCC)
1242 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1243 " -> `%s` has been declared here: %s:%i",
1244 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1245 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1247 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1248 " -> `%s` has been declared here: %s:%i",
1249 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1250 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1256 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1257 "too %s parameters for call to %s: expected %i, got %i\n"
1258 " -> `%s` has been declared here: %s:%i",
1259 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1260 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1262 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1263 "too %s parameters for function call: expected %i, got %i\n"
1264 " -> `%s` has been declared here: %s:%i",
1265 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1266 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1274 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1276 if (!vec_size(sy->ops)) {
1277 parseerror(parser, "unmatched closing paren");
1280 /* this would for bit a + (x) because there are no operators inside (x)
1281 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1282 parseerror(parser, "empty parenthesis expression");
1286 while (vec_size(sy->ops)) {
1287 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1288 if (!parser_close_call(parser, sy))
1292 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1293 vec_shrinkby(sy->ops, 1);
1294 return !functions_only;
1296 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1299 /* pop off the parenthesis */
1300 vec_shrinkby(sy->ops, 1);
1301 /* then apply the index operator */
1302 if (!parser_sy_pop(parser, sy))
1306 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_TERNARY) {
1309 /* pop off the parenthesis */
1310 vec_shrinkby(sy->ops, 1);
1313 if (!parser_sy_pop(parser, sy))
1319 static void parser_reclassify_token(parser_t *parser)
1322 for (i = 0; i < operator_count; ++i) {
1323 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1324 parser->tok = TOKEN_OPERATOR;
1330 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1332 ast_expression *expr = NULL;
1334 bool wantop = false;
1335 bool gotmemberof = false;
1337 /* count the parens because an if starts with one, so the
1338 * end of a condition is an unmatched closing paren
1346 parser->lex->flags.noops = false;
1348 parser_reclassify_token(parser);
1353 gotmemberof = false;
1355 parser->memberof = 0;
1357 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1358 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1360 /* a translatable string */
1364 parseerror(parser, "expected operator or end of statement, got constant");
1368 parser->lex->flags.noops = true;
1369 if (!parser_next(parser) || parser->tok != '(') {
1370 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1373 parser->lex->flags.noops = false;
1374 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1375 parseerror(parser, "expected a constant string in translatable-string extension");
1378 val = parser_const_string(parser, parser_tokval(parser), true);
1382 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1383 DEBUGSHUNTDO(con_out("push string\n"));
1385 if (!parser_next(parser) || parser->tok != ')') {
1386 parseerror(parser, "expected closing paren after translatable string");
1390 else if (parser->tok == TOKEN_IDENT)
1392 ast_expression *var;
1394 parseerror(parser, "expected operator or end of statement");
1399 if (opts_standard == COMPILER_GMQCC)
1401 if (parser->memberof == TYPE_ENTITY) {
1402 /* still get vars first since there could be a fieldpointer */
1403 var = parser_find_var(parser, parser_tokval(parser));
1405 var = parser_find_field(parser, parser_tokval(parser));
1407 else if (parser->memberof == TYPE_VECTOR)
1409 parseerror(parser, "TODO: implement effective vector member access");
1412 else if (parser->memberof) {
1413 parseerror(parser, "namespace for member not found");
1417 var = parser_find_var(parser, parser_tokval(parser));
1419 var = parser_find_var(parser, parser_tokval(parser));
1421 var = parser_find_field(parser, parser_tokval(parser));
1424 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1427 if (ast_istype(var, ast_value)) {
1428 ((ast_value*)var)->uses++;
1430 else if (ast_istype(var, ast_member)) {
1431 ast_member *mem = (ast_member*)var;
1432 if (ast_istype(mem->owner, ast_value))
1433 ((ast_value*)(mem->owner))->uses++;
1435 vec_push(sy.out, syexp(parser_ctx(parser), var));
1436 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1438 else if (parser->tok == TOKEN_FLOATCONST) {
1441 parseerror(parser, "expected operator or end of statement, got constant");
1445 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1448 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1449 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1451 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1454 parseerror(parser, "expected operator or end of statement, got constant");
1458 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1461 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1462 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1464 else if (parser->tok == TOKEN_STRINGCONST) {
1467 parseerror(parser, "expected operator or end of statement, got constant");
1471 val = parser_const_string(parser, parser_tokval(parser), false);
1474 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1475 DEBUGSHUNTDO(con_out("push string\n"));
1477 else if (parser->tok == TOKEN_VECTORCONST) {
1480 parseerror(parser, "expected operator or end of statement, got constant");
1484 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1487 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1488 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1489 parser_token(parser)->constval.v.x,
1490 parser_token(parser)->constval.v.y,
1491 parser_token(parser)->constval.v.z));
1493 else if (parser->tok == '(') {
1494 parseerror(parser, "internal error: '(' should be classified as operator");
1497 else if (parser->tok == '[') {
1498 parseerror(parser, "internal error: '[' should be classified as operator");
1501 else if (parser->tok == ')') {
1503 DEBUGSHUNTDO(con_out("do[op] )\n"));
1507 /* we do expect an operator next */
1508 /* closing an opening paren */
1509 if (!parser_close_paren(parser, &sy, false))
1512 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1516 /* allowed for function calls */
1517 if (!parser_close_paren(parser, &sy, true))
1522 else if (parser->tok == ']') {
1524 parseerror(parser, "operand expected");
1528 if (!parser_close_paren(parser, &sy, false))
1532 else if (parser->tok != TOKEN_OPERATOR) {
1534 parseerror(parser, "expected operator or end of statement");
1541 /* classify the operator */
1542 const oper_info *op;
1543 const oper_info *olast = NULL;
1545 for (o = 0; o < operator_count; ++o) {
1546 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1547 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1548 !strcmp(parser_tokval(parser), operators[o].op))
1553 if (o == operator_count) {
1554 /* no operator found... must be the end of the statement */
1557 /* found an operator */
1560 /* when declaring variables, a comma starts a new variable */
1561 if (op->id == opid1(',') && !parens && stopatcomma) {
1562 /* fixup the token */
1567 /* a colon without a pervious question mark cannot be a ternary */
1568 if (!ternaries && op->id == opid2(':','?')) {
1573 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1574 olast = &operators[vec_last(sy.ops).etype-1];
1577 (op->prec < olast->prec) ||
1578 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1580 if (!parser_sy_pop(parser, &sy))
1582 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1583 olast = &operators[vec_last(sy.ops).etype-1];
1588 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1589 /* for gmqcc standard: open up the namespace of the previous type */
1590 ast_expression *prevex = vec_last(sy.out).out;
1592 parseerror(parser, "unexpected member operator");
1595 if (prevex->expression.vtype == TYPE_ENTITY)
1596 parser->memberof = TYPE_ENTITY;
1597 else if (prevex->expression.vtype == TYPE_VECTOR)
1598 parser->memberof = TYPE_VECTOR;
1600 parseerror(parser, "type error: type has no members");
1606 if (op->id == opid1('(')) {
1608 size_t sycount = vec_size(sy.out);
1609 DEBUGSHUNTDO(con_out("push [op] (\n"));
1611 /* we expected an operator, this is the function-call operator */
1612 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1615 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1616 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1619 } else if (op->id == opid1('[')) {
1621 parseerror(parser, "unexpected array subscript");
1625 /* push both the operator and the paren, this makes life easier */
1626 vec_push(sy.ops, syop(parser_ctx(parser), op));
1627 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1629 } else if (op->id == opid2('?',':')) {
1631 vec_push(sy.ops, syop(parser_ctx(parser), op));
1632 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_TERNARY, 0));
1635 } else if (op->id == opid2(':','?')) {
1636 /* we don't push this operator */
1637 if (!parser_close_paren(parser, &sy, false))
1642 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1643 vec_push(sy.ops, syop(parser_ctx(parser), op));
1644 wantop = !!(op->flags & OP_SUFFIX);
1647 if (!parser_next(parser)) {
1650 if (parser->tok == ';' ||
1651 (!parens && parser->tok == ']'))
1657 while (vec_size(sy.ops)) {
1658 if (!parser_sy_pop(parser, &sy))
1662 parser->lex->flags.noops = true;
1663 if (!vec_size(sy.out)) {
1664 parseerror(parser, "empty expression");
1667 expr = sy.out[0].out;
1670 DEBUGSHUNTDO(con_out("shunt done\n"));
1674 parser->lex->flags.noops = true;
1680 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1682 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1685 if (!parser_next(parser)) {
1692 static void parser_enterblock(parser_t *parser)
1694 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1695 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1696 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1697 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1698 vec_push(parser->_block_ctx, parser_ctx(parser));
1701 static bool parser_leaveblock(parser_t *parser)
1704 size_t locals, typedefs;
1706 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1707 parseerror(parser, "internal error: parser_leaveblock with no block");
1711 util_htdel(vec_last(parser->variables));
1712 vec_pop(parser->variables);
1713 if (!vec_size(parser->_blocklocals)) {
1714 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1718 locals = vec_last(parser->_blocklocals);
1719 vec_pop(parser->_blocklocals);
1720 while (vec_size(parser->_locals) != locals) {
1721 ast_expression *e = vec_last(parser->_locals);
1722 ast_value *v = (ast_value*)e;
1723 vec_pop(parser->_locals);
1724 if (ast_istype(e, ast_value) && !v->uses) {
1725 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name)) {
1732 typedefs = vec_last(parser->_blocktypedefs);
1733 while (vec_size(parser->_typedefs) != typedefs) {
1734 ast_delete(vec_last(parser->_typedefs));
1735 vec_pop(parser->_typedefs);
1737 util_htdel(vec_last(parser->typedefs));
1738 vec_pop(parser->typedefs);
1740 vec_pop(parser->_block_ctx);
1744 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1746 vec_push(parser->_locals, e);
1747 util_htset(vec_last(parser->variables), name, (void*)e);
1750 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1753 ast_expression *cond, *ontrue, *onfalse = NULL;
1756 lex_ctx ctx = parser_ctx(parser);
1758 (void)block; /* not touching */
1760 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1761 if (!parser_next(parser)) {
1762 parseerror(parser, "expected condition or 'not'");
1765 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
1767 if (!parser_next(parser)) {
1768 parseerror(parser, "expected condition in parenthesis");
1772 if (parser->tok != '(') {
1773 parseerror(parser, "expected 'if' condition in parenthesis");
1776 /* parse into the expression */
1777 if (!parser_next(parser)) {
1778 parseerror(parser, "expected 'if' condition after opening paren");
1781 /* parse the condition */
1782 cond = parse_expression_leave(parser, false);
1786 if (parser->tok != ')') {
1787 parseerror(parser, "expected closing paren after 'if' condition");
1791 /* parse into the 'then' branch */
1792 if (!parser_next(parser)) {
1793 parseerror(parser, "expected statement for on-true branch of 'if'");
1797 if (!parse_statement_or_block(parser, &ontrue)) {
1801 /* check for an else */
1802 if (!strcmp(parser_tokval(parser), "else")) {
1803 /* parse into the 'else' branch */
1804 if (!parser_next(parser)) {
1805 parseerror(parser, "expected on-false branch after 'else'");
1810 if (!parse_statement_or_block(parser, &onfalse)) {
1818 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1820 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1821 *out = (ast_expression*)ifthen;
1825 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1828 ast_expression *cond, *ontrue;
1830 lex_ctx ctx = parser_ctx(parser);
1832 (void)block; /* not touching */
1834 /* skip the 'while' and check for opening paren */
1835 if (!parser_next(parser) || parser->tok != '(') {
1836 parseerror(parser, "expected 'while' condition in parenthesis");
1839 /* parse into the expression */
1840 if (!parser_next(parser)) {
1841 parseerror(parser, "expected 'while' condition after opening paren");
1844 /* parse the condition */
1845 cond = parse_expression_leave(parser, false);
1849 if (parser->tok != ')') {
1850 parseerror(parser, "expected closing paren after 'while' condition");
1854 /* parse into the 'then' branch */
1855 if (!parser_next(parser)) {
1856 parseerror(parser, "expected while-loop body");
1860 if (!parse_statement_or_block(parser, &ontrue)) {
1865 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1866 *out = (ast_expression*)aloop;
1870 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1873 ast_expression *cond, *ontrue;
1875 lex_ctx ctx = parser_ctx(parser);
1877 (void)block; /* not touching */
1879 /* skip the 'do' and get the body */
1880 if (!parser_next(parser)) {
1881 parseerror(parser, "expected loop body");
1884 if (!parse_statement_or_block(parser, &ontrue))
1887 /* expect the "while" */
1888 if (parser->tok != TOKEN_KEYWORD ||
1889 strcmp(parser_tokval(parser), "while"))
1891 parseerror(parser, "expected 'while' and condition");
1896 /* skip the 'while' and check for opening paren */
1897 if (!parser_next(parser) || parser->tok != '(') {
1898 parseerror(parser, "expected 'while' condition in parenthesis");
1902 /* parse into the expression */
1903 if (!parser_next(parser)) {
1904 parseerror(parser, "expected 'while' condition after opening paren");
1908 /* parse the condition */
1909 cond = parse_expression_leave(parser, false);
1913 if (parser->tok != ')') {
1914 parseerror(parser, "expected closing paren after 'while' condition");
1920 if (!parser_next(parser) || parser->tok != ';') {
1921 parseerror(parser, "expected semicolon after condition");
1927 if (!parser_next(parser)) {
1928 parseerror(parser, "parse error");
1934 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1935 *out = (ast_expression*)aloop;
1939 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1942 ast_expression *initexpr, *cond, *increment, *ontrue;
1946 lex_ctx ctx = parser_ctx(parser);
1948 parser_enterblock(parser);
1955 /* skip the 'while' and check for opening paren */
1956 if (!parser_next(parser) || parser->tok != '(') {
1957 parseerror(parser, "expected 'for' expressions in parenthesis");
1960 /* parse into the expression */
1961 if (!parser_next(parser)) {
1962 parseerror(parser, "expected 'for' initializer after opening paren");
1967 if (parser->tok == TOKEN_IDENT)
1968 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
1970 if (typevar || parser->tok == TOKEN_TYPENAME) {
1971 if (opts_standard != COMPILER_GMQCC) {
1972 if (parsewarning(parser, WARN_EXTENSIONS,
1973 "current standard does not allow variable declarations in for-loop initializers"))
1976 if (!parse_variable(parser, block, true, CV_VAR, typevar))
1979 else if (parser->tok != ';')
1981 initexpr = parse_expression_leave(parser, false);
1986 /* move on to condition */
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 condition */
1997 if (parser->tok != ';') {
1998 cond = parse_expression_leave(parser, false);
2003 /* move on to incrementor */
2004 if (parser->tok != ';') {
2005 parseerror(parser, "expected semicolon after for-loop initializer");
2008 if (!parser_next(parser)) {
2009 parseerror(parser, "expected for-loop condition");
2013 /* parse the incrementor */
2014 if (parser->tok != ')') {
2015 increment = parse_expression_leave(parser, false);
2018 if (!ast_side_effects(increment)) {
2019 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2025 if (parser->tok != ')') {
2026 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2029 /* parse into the 'then' branch */
2030 if (!parser_next(parser)) {
2031 parseerror(parser, "expected for-loop body");
2034 if (!parse_statement_or_block(parser, &ontrue))
2037 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
2038 *out = (ast_expression*)aloop;
2040 if (!parser_leaveblock(parser))
2044 if (initexpr) ast_delete(initexpr);
2045 if (cond) ast_delete(cond);
2046 if (increment) ast_delete(increment);
2047 (void)!parser_leaveblock(parser);
2051 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2053 ast_expression *exp = NULL;
2054 ast_return *ret = NULL;
2055 ast_value *expected = parser->function->vtype;
2057 lex_ctx ctx = parser_ctx(parser);
2059 (void)block; /* not touching */
2061 if (!parser_next(parser)) {
2062 parseerror(parser, "expected return expression");
2066 if (parser->tok != ';') {
2067 exp = parse_expression(parser, false);
2071 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2072 parseerror(parser, "return with invalid expression");
2075 ret = ast_return_new(exp->expression.node.context, exp);
2081 if (!parser_next(parser))
2082 parseerror(parser, "parse error");
2083 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2084 if (opts_standard != COMPILER_GMQCC)
2085 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2087 parseerror(parser, "return without value");
2089 ret = ast_return_new(ctx, NULL);
2091 *out = (ast_expression*)ret;
2095 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2097 lex_ctx ctx = parser_ctx(parser);
2099 (void)block; /* not touching */
2101 if (!parser_next(parser) || parser->tok != ';') {
2102 parseerror(parser, "expected semicolon");
2106 if (!parser_next(parser))
2107 parseerror(parser, "parse error");
2109 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2113 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2115 ast_expression *operand;
2118 ast_switch *switchnode;
2119 ast_switch_case swcase;
2121 lex_ctx ctx = parser_ctx(parser);
2123 (void)block; /* not touching */
2126 /* parse over the opening paren */
2127 if (!parser_next(parser) || parser->tok != '(') {
2128 parseerror(parser, "expected switch operand in parenthesis");
2132 /* parse into the expression */
2133 if (!parser_next(parser)) {
2134 parseerror(parser, "expected switch operand");
2137 /* parse the operand */
2138 operand = parse_expression_leave(parser, false);
2142 switchnode = ast_switch_new(ctx, operand);
2145 if (parser->tok != ')') {
2146 ast_delete(switchnode);
2147 parseerror(parser, "expected closing paren after 'switch' operand");
2151 /* parse over the opening paren */
2152 if (!parser_next(parser) || parser->tok != '{') {
2153 ast_delete(switchnode);
2154 parseerror(parser, "expected list of cases");
2158 if (!parser_next(parser)) {
2159 ast_delete(switchnode);
2160 parseerror(parser, "expected 'case' or 'default'");
2164 /* new block; allow some variables to be declared here */
2165 parser_enterblock(parser);
2168 if (parser->tok == TOKEN_IDENT)
2169 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2170 if (typevar || parser->tok == TOKEN_TYPENAME) {
2171 if (!parse_variable(parser, block, false, CV_NONE, typevar)) {
2172 ast_delete(switchnode);
2177 if (!strcmp(parser_tokval(parser), "var") ||
2178 !strcmp(parser_tokval(parser), "local"))
2180 if (!parser_next(parser)) {
2181 parseerror(parser, "expected variable declaration");
2182 ast_delete(switchnode);
2185 if (!parse_variable(parser, block, false, CV_VAR, NULL)) {
2186 ast_delete(switchnode);
2191 if (!strcmp(parser_tokval(parser), "const")) {
2192 if (!parser_next(parser)) {
2193 parseerror(parser, "expected variable declaration");
2194 ast_delete(switchnode);
2197 if (!strcmp(parser_tokval(parser), "var")) {
2198 if (!parser_next(parser)) {
2199 parseerror(parser, "expected variable declaration");
2200 ast_delete(switchnode);
2204 if (!parse_variable(parser, block, false, CV_CONST, NULL)) {
2205 ast_delete(switchnode);
2214 while (parser->tok != '}') {
2215 ast_block *caseblock;
2217 if (parser->tok != TOKEN_KEYWORD) {
2218 ast_delete(switchnode);
2219 parseerror(parser, "expected 'case' or 'default'");
2222 if (!strcmp(parser_tokval(parser), "case")) {
2223 if (!parser_next(parser)) {
2224 ast_delete(switchnode);
2225 parseerror(parser, "expected expression for case");
2228 swcase.value = parse_expression_leave(parser, false);
2229 if (!swcase.value) {
2230 ast_delete(switchnode);
2231 parseerror(parser, "expected expression for case");
2234 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2235 opval = (ast_value*)swcase.value;
2236 if (!ast_istype(swcase.value, ast_value)) { /* || opval->cvq != CV_CONST) { */
2237 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2243 else if (!strcmp(parser_tokval(parser), "default")) {
2244 swcase.value = NULL;
2245 if (!parser_next(parser)) {
2246 ast_delete(switchnode);
2247 parseerror(parser, "expected colon");
2252 /* Now the colon and body */
2253 if (parser->tok != ':') {
2254 if (swcase.value) ast_unref(swcase.value);
2255 ast_delete(switchnode);
2256 parseerror(parser, "expected colon");
2260 if (!parser_next(parser)) {
2261 if (swcase.value) ast_unref(swcase.value);
2262 ast_delete(switchnode);
2263 parseerror(parser, "expected statements or case");
2266 caseblock = ast_block_new(parser_ctx(parser));
2268 if (swcase.value) ast_unref(swcase.value);
2269 ast_delete(switchnode);
2272 swcase.code = (ast_expression*)caseblock;
2273 vec_push(switchnode->cases, swcase);
2275 ast_expression *expr;
2276 if (parser->tok == '}')
2278 if (parser->tok == TOKEN_KEYWORD) {
2279 if (!strcmp(parser_tokval(parser), "case") ||
2280 !strcmp(parser_tokval(parser), "default"))
2285 if (!parse_statement(parser, caseblock, &expr, true)) {
2286 ast_delete(switchnode);
2291 ast_block_add_expr(caseblock, expr);
2295 parser_leaveblock(parser);
2298 if (parser->tok != '}') {
2299 ast_delete(switchnode);
2300 parseerror(parser, "expected closing paren of case list");
2303 if (!parser_next(parser)) {
2304 ast_delete(switchnode);
2305 parseerror(parser, "parse error after switch");
2308 *out = (ast_expression*)switchnode;
2312 static bool parse_goto(parser_t *parser, ast_expression **out)
2317 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2318 parseerror(parser, "expected label name after `goto`");
2322 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2324 for (i = 0; i < vec_size(parser->labels); ++i) {
2325 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2326 ast_goto_set_label(gt, parser->labels[i]);
2330 if (i == vec_size(parser->labels))
2331 vec_push(parser->gotos, gt);
2333 if (!parser_next(parser) || parser->tok != ';') {
2334 parseerror(parser, "semicolon expected after goto label");
2337 if (!parser_next(parser)) {
2338 parseerror(parser, "parse error after goto");
2342 *out = (ast_expression*)gt;
2346 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2349 ast_value *typevar = NULL;
2352 if (parser->tok == TOKEN_IDENT)
2353 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2355 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2357 /* local variable */
2359 parseerror(parser, "cannot declare a variable from here");
2362 if (opts_standard == COMPILER_QCC) {
2363 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2366 if (!parse_variable(parser, block, false, CV_NONE, typevar))
2371 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
2375 else if (parser->tok == TOKEN_KEYWORD)
2377 if (!strcmp(parser_tokval(parser), "local") ||
2378 !strcmp(parser_tokval(parser), "const") ||
2379 !strcmp(parser_tokval(parser), "var"))
2382 if (parser_tokval(parser)[0] == 'c')
2384 else if (parser_tokval(parser)[0] == 'v')
2390 parseerror(parser, "cannot declare a local variable here");
2393 if (!parser_next(parser)) {
2394 parseerror(parser, "expected variable declaration");
2397 if (!parse_variable(parser, block, true, cvq, NULL))
2402 else if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
2407 if (!parser_next(parser)) {
2408 parseerror(parser, "parse error after __builtin_debug_printtype");
2412 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
2414 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
2415 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
2416 if (!parser_next(parser)) {
2417 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
2423 if (!parse_statement(parser, block, out, allow_cases))
2426 con_out("__builtin_debug_printtype: got no output node\n");
2429 ast_type_to_string(*out, ty, sizeof(ty));
2430 con_out("__builtin_debug_printtype: `%s`\n", ty);
2435 else if (!strcmp(parser_tokval(parser), "return"))
2437 return parse_return(parser, block, out);
2439 else if (!strcmp(parser_tokval(parser), "if"))
2441 return parse_if(parser, block, out);
2443 else if (!strcmp(parser_tokval(parser), "while"))
2445 return parse_while(parser, block, out);
2447 else if (!strcmp(parser_tokval(parser), "do"))
2449 return parse_dowhile(parser, block, out);
2451 else if (!strcmp(parser_tokval(parser), "for"))
2453 if (opts_standard == COMPILER_QCC) {
2454 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2457 return parse_for(parser, block, out);
2459 else if (!strcmp(parser_tokval(parser), "break"))
2461 return parse_break_continue(parser, block, out, false);
2463 else if (!strcmp(parser_tokval(parser), "continue"))
2465 return parse_break_continue(parser, block, out, true);
2467 else if (!strcmp(parser_tokval(parser), "switch"))
2469 return parse_switch(parser, block, out);
2471 else if (!strcmp(parser_tokval(parser), "case") ||
2472 !strcmp(parser_tokval(parser), "default"))
2475 parseerror(parser, "unexpected 'case' label");
2480 else if (!strcmp(parser_tokval(parser), "goto"))
2482 return parse_goto(parser, out);
2484 else if (!strcmp(parser_tokval(parser), "typedef"))
2486 if (!parser_next(parser)) {
2487 parseerror(parser, "expected type definition after 'typedef'");
2490 return parse_typedef(parser);
2492 parseerror(parser, "Unexpected keyword");
2495 else if (parser->tok == '{')
2498 inner = parse_block(parser);
2501 *out = (ast_expression*)inner;
2504 else if (parser->tok == ':')
2508 if (!parser_next(parser)) {
2509 parseerror(parser, "expected label name");
2512 if (parser->tok != TOKEN_IDENT) {
2513 parseerror(parser, "label must be an identifier");
2516 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
2519 vec_push(parser->labels, label);
2520 *out = (ast_expression*)label;
2521 if (!parser_next(parser)) {
2522 parseerror(parser, "parse error after label");
2525 for (i = 0; i < vec_size(parser->gotos); ++i) {
2526 if (!strcmp(parser->gotos[i]->name, label->name)) {
2527 ast_goto_set_label(parser->gotos[i], label);
2528 vec_remove(parser->gotos, i, 1);
2534 else if (parser->tok == ';')
2536 if (!parser_next(parser)) {
2537 parseerror(parser, "parse error after empty statement");
2544 ast_expression *exp = parse_expression(parser, false);
2548 if (!ast_side_effects(exp)) {
2549 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2556 static bool parse_block_into(parser_t *parser, ast_block *block)
2560 parser_enterblock(parser);
2562 if (!parser_next(parser)) { /* skip the '{' */
2563 parseerror(parser, "expected function body");
2567 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2569 ast_expression *expr = NULL;
2570 if (parser->tok == '}')
2573 if (!parse_statement(parser, block, &expr, false)) {
2574 /* parseerror(parser, "parse error"); */
2580 ast_block_add_expr(block, expr);
2583 if (parser->tok != '}') {
2586 (void)parser_next(parser);
2590 if (!parser_leaveblock(parser))
2592 return retval && !!block;
2595 static ast_block* parse_block(parser_t *parser)
2598 block = ast_block_new(parser_ctx(parser));
2601 if (!parse_block_into(parser, block)) {
2602 ast_block_delete(block);
2608 static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
2610 if (parser->tok == '{') {
2611 *out = (ast_expression*)parse_block(parser);
2614 return parse_statement(parser, NULL, out, false);
2617 static bool create_vector_members(ast_value *var, ast_member **me)
2620 size_t len = strlen(var->name);
2622 for (i = 0; i < 3; ++i) {
2623 char *name = mem_a(len+3);
2624 memcpy(name, var->name, len);
2626 name[len+1] = 'x'+i;
2628 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2637 do { ast_member_delete(me[--i]); } while(i);
2641 static bool parse_function_body(parser_t *parser, ast_value *var)
2643 ast_block *block = NULL;
2648 ast_expression *framenum = NULL;
2649 ast_expression *nextthink = NULL;
2650 /* None of the following have to be deleted */
2651 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2652 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2653 bool has_frame_think;
2657 has_frame_think = false;
2658 old = parser->function;
2660 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
2661 parseerror(parser, "gotos/labels leaking");
2665 if (var->expression.variadic) {
2666 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2667 "variadic function with implementation will not be able to access additional parameters"))
2673 if (parser->tok == '[') {
2674 /* got a frame definition: [ framenum, nextthink ]
2675 * this translates to:
2676 * self.frame = framenum;
2677 * self.nextthink = time + 0.1;
2678 * self.think = nextthink;
2682 fld_think = parser_find_field(parser, "think");
2683 fld_nextthink = parser_find_field(parser, "nextthink");
2684 fld_frame = parser_find_field(parser, "frame");
2685 if (!fld_think || !fld_nextthink || !fld_frame) {
2686 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2687 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2690 gbl_time = parser_find_global(parser, "time");
2691 gbl_self = parser_find_global(parser, "self");
2692 if (!gbl_time || !gbl_self) {
2693 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2694 parseerror(parser, "please declare the following globals: `time`, `self`");
2698 if (!parser_next(parser))
2701 framenum = parse_expression_leave(parser, true);
2703 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2706 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
2707 ast_unref(framenum);
2708 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2712 if (parser->tok != ',') {
2713 ast_unref(framenum);
2714 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2715 parseerror(parser, "Got a %i\n", parser->tok);
2719 if (!parser_next(parser)) {
2720 ast_unref(framenum);
2724 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2726 /* qc allows the use of not-yet-declared functions here
2727 * - this automatically creates a prototype */
2728 ast_value *thinkfunc;
2729 ast_expression *functype = fld_think->expression.next;
2731 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2732 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2733 ast_unref(framenum);
2734 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2738 if (!parser_next(parser)) {
2739 ast_unref(framenum);
2740 ast_delete(thinkfunc);
2744 vec_push(parser->globals, (ast_expression*)thinkfunc);
2745 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2746 nextthink = (ast_expression*)thinkfunc;
2749 nextthink = parse_expression_leave(parser, true);
2751 ast_unref(framenum);
2752 parseerror(parser, "expected a think-function in [frame,think] notation");
2757 if (!ast_istype(nextthink, ast_value)) {
2758 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2762 if (retval && parser->tok != ']') {
2763 parseerror(parser, "expected closing `]` for [frame,think] notation");
2767 if (retval && !parser_next(parser)) {
2771 if (retval && parser->tok != '{') {
2772 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2777 ast_unref(nextthink);
2778 ast_unref(framenum);
2782 has_frame_think = true;
2785 block = ast_block_new(parser_ctx(parser));
2787 parseerror(parser, "failed to allocate block");
2788 if (has_frame_think) {
2789 ast_unref(nextthink);
2790 ast_unref(framenum);
2795 if (has_frame_think) {
2797 ast_expression *self_frame;
2798 ast_expression *self_nextthink;
2799 ast_expression *self_think;
2800 ast_expression *time_plus_1;
2801 ast_store *store_frame;
2802 ast_store *store_nextthink;
2803 ast_store *store_think;
2805 ctx = parser_ctx(parser);
2806 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2807 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2808 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2810 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2811 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2813 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2814 if (self_frame) ast_delete(self_frame);
2815 if (self_nextthink) ast_delete(self_nextthink);
2816 if (self_think) ast_delete(self_think);
2817 if (time_plus_1) ast_delete(time_plus_1);
2823 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2824 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2825 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2828 ast_delete(self_frame);
2831 if (!store_nextthink) {
2832 ast_delete(self_nextthink);
2836 ast_delete(self_think);
2840 if (store_frame) ast_delete(store_frame);
2841 if (store_nextthink) ast_delete(store_nextthink);
2842 if (store_think) ast_delete(store_think);
2845 ast_block_add_expr(block, (ast_expression*)store_frame);
2846 ast_block_add_expr(block, (ast_expression*)store_nextthink);
2847 ast_block_add_expr(block, (ast_expression*)store_think);
2851 parseerror(parser, "failed to generate code for [frame,think]");
2852 ast_unref(nextthink);
2853 ast_unref(framenum);
2859 parser_enterblock(parser);
2861 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2863 ast_value *param = var->expression.params[parami];
2866 if (param->expression.vtype != TYPE_VECTOR &&
2867 (param->expression.vtype != TYPE_FIELD ||
2868 param->expression.next->expression.vtype != TYPE_VECTOR))
2873 if (!create_vector_members(param, me)) {
2874 ast_block_delete(block);
2878 for (e = 0; e < 3; ++e) {
2879 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2880 ast_block_collect(block, (ast_expression*)me[e]);
2884 func = ast_function_new(ast_ctx(var), var->name, var);
2886 parseerror(parser, "failed to allocate function for `%s`", var->name);
2887 ast_block_delete(block);
2890 vec_push(parser->functions, func);
2892 parser->function = func;
2893 if (!parse_block_into(parser, block)) {
2894 ast_block_delete(block);
2898 vec_push(func->blocks, block);
2900 parser->function = old;
2901 if (!parser_leaveblock(parser))
2903 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2904 parseerror(parser, "internal error: local scopes left");
2908 if (parser->tok == ';')
2909 return parser_next(parser);
2910 else if (opts_standard == COMPILER_QCC)
2911 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2915 vec_pop(parser->functions);
2916 ast_function_delete(func);
2917 var->constval.vfunc = NULL;
2920 (void)!parser_leaveblock(parser);
2921 parser->function = old;
2925 static ast_expression *array_accessor_split(
2930 ast_expression *left,
2931 ast_expression *right
2937 lex_ctx ctx = ast_ctx(array);
2939 if (!left || !right) {
2940 if (left) ast_delete(left);
2941 if (right) ast_delete(right);
2945 cmp = ast_binary_new(ctx, INSTR_LT,
2946 (ast_expression*)index,
2947 (ast_expression*)parser_const_float(parser, middle));
2951 parseerror(parser, "internal error: failed to create comparison for array setter");
2955 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2957 ast_delete(cmp); /* will delete left and right */
2958 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2962 return (ast_expression*)ifthen;
2965 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2967 lex_ctx ctx = ast_ctx(array);
2969 if (from+1 == afterend) {
2970 /* set this value */
2973 ast_array_index *subscript;
2975 int assignop = type_store_instr[value->expression.vtype];
2977 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2978 assignop = INSTR_STORE_V;
2980 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2984 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2986 ast_delete(subscript);
2990 block = ast_block_new(ctx);
2996 ast_block_add_expr(block, (ast_expression*)st);
2998 ret = ast_return_new(ctx, NULL);
3004 ast_block_add_expr(block, (ast_expression*)ret);
3006 return (ast_expression*)block;
3008 ast_expression *left, *right;
3009 size_t diff = afterend - from;
3010 size_t middle = from + diff/2;
3011 left = array_setter_node(parser, array, index, value, from, middle);
3012 right = array_setter_node(parser, array, index, value, middle, afterend);
3013 return array_accessor_split(parser, array, index, middle, left, right);
3017 static ast_expression *array_field_setter_node(
3026 lex_ctx ctx = ast_ctx(array);
3028 if (from+1 == afterend) {
3029 /* set this value */
3032 ast_entfield *entfield;
3033 ast_array_index *subscript;
3035 int assignop = type_storep_instr[value->expression.vtype];
3037 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
3038 assignop = INSTR_STOREP_V;
3040 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3044 entfield = ast_entfield_new_force(ctx,
3045 (ast_expression*)entity,
3046 (ast_expression*)subscript,
3047 (ast_expression*)subscript);
3049 ast_delete(subscript);
3053 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
3055 ast_delete(entfield);
3059 block = ast_block_new(ctx);
3065 ast_block_add_expr(block, (ast_expression*)st);
3067 ret = ast_return_new(ctx, NULL);
3073 ast_block_add_expr(block, (ast_expression*)ret);
3075 return (ast_expression*)block;
3077 ast_expression *left, *right;
3078 size_t diff = afterend - from;
3079 size_t middle = from + diff/2;
3080 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
3081 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
3082 return array_accessor_split(parser, array, index, middle, left, right);
3086 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
3088 lex_ctx ctx = ast_ctx(array);
3090 if (from+1 == afterend) {
3092 ast_array_index *subscript;
3094 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3098 ret = ast_return_new(ctx, (ast_expression*)subscript);
3100 ast_delete(subscript);
3104 return (ast_expression*)ret;
3106 ast_expression *left, *right;
3107 size_t diff = afterend - from;
3108 size_t middle = from + diff/2;
3109 left = array_getter_node(parser, array, index, from, middle);
3110 right = array_getter_node(parser, array, index, middle, afterend);
3111 return array_accessor_split(parser, array, index, middle, left, right);
3115 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3117 ast_function *func = NULL;
3118 ast_value *fval = NULL;
3119 ast_block *body = NULL;
3121 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3123 parseerror(parser, "failed to create accessor function value");
3127 func = ast_function_new(ast_ctx(array), funcname, fval);
3130 parseerror(parser, "failed to create accessor function node");
3134 body = ast_block_new(ast_ctx(array));
3136 parseerror(parser, "failed to create block for array accessor");
3142 vec_push(func->blocks, body);
3145 vec_push(parser->accessors, fval);
3150 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3152 ast_expression *root = NULL;
3153 ast_value *index = NULL;
3154 ast_value *value = NULL;
3158 if (!ast_istype(array->expression.next, ast_value)) {
3159 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3163 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3165 func = fval->constval.vfunc;
3166 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3168 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3169 value = ast_value_copy((ast_value*)array->expression.next);
3171 if (!index || !value) {
3172 parseerror(parser, "failed to create locals for array accessor");
3175 (void)!ast_value_set_name(value, "value"); /* not important */
3176 vec_push(fval->expression.params, index);
3177 vec_push(fval->expression.params, value);
3179 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3181 parseerror(parser, "failed to build accessor search tree");
3185 ast_block_add_expr(func->blocks[0], root);
3186 array->setter = fval;
3189 if (index) ast_delete(index);
3190 if (value) ast_delete(value);
3191 if (root) ast_delete(root);
3197 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3199 ast_expression *root = NULL;
3200 ast_value *entity = NULL;
3201 ast_value *index = NULL;
3202 ast_value *value = NULL;
3206 if (!ast_istype(array->expression.next, ast_value)) {
3207 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3211 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3213 func = fval->constval.vfunc;
3214 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3216 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3217 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3218 value = ast_value_copy((ast_value*)array->expression.next);
3219 if (!entity || !index || !value) {
3220 parseerror(parser, "failed to create locals for array accessor");
3223 (void)!ast_value_set_name(value, "value"); /* not important */
3224 vec_push(fval->expression.params, entity);
3225 vec_push(fval->expression.params, index);
3226 vec_push(fval->expression.params, value);
3228 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3230 parseerror(parser, "failed to build accessor search tree");
3234 ast_block_add_expr(func->blocks[0], root);
3235 array->setter = fval;
3238 if (entity) ast_delete(entity);
3239 if (index) ast_delete(index);
3240 if (value) ast_delete(value);
3241 if (root) ast_delete(root);
3247 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3249 ast_expression *root = NULL;
3250 ast_value *index = NULL;
3254 /* NOTE: checking array->expression.next rather than elemtype since
3255 * for fields elemtype is a temporary fieldtype.
3257 if (!ast_istype(array->expression.next, ast_value)) {
3258 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3262 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3264 func = fval->constval.vfunc;
3265 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3267 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3270 parseerror(parser, "failed to create locals for array accessor");
3273 vec_push(fval->expression.params, index);
3275 root = array_getter_node(parser, array, index, 0, array->expression.count);
3277 parseerror(parser, "failed to build accessor search tree");
3281 ast_block_add_expr(func->blocks[0], root);
3282 array->getter = fval;
3285 if (index) ast_delete(index);
3286 if (root) ast_delete(root);
3292 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3293 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3301 bool variadic = false;
3303 ctx = parser_ctx(parser);
3305 /* for the sake of less code we parse-in in this function */
3306 if (!parser_next(parser)) {
3307 parseerror(parser, "expected parameter list");
3313 /* parse variables until we hit a closing paren */
3314 while (parser->tok != ')') {
3316 /* there must be commas between them */
3317 if (parser->tok != ',') {
3318 parseerror(parser, "expected comma or end of parameter list");
3321 if (!parser_next(parser)) {
3322 parseerror(parser, "expected parameter");
3328 if (parser->tok == TOKEN_DOTS) {
3329 /* '...' indicates a varargs function */
3331 if (!parser_next(parser)) {
3332 parseerror(parser, "expected parameter");
3335 if (parser->tok != ')') {
3336 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3342 /* for anything else just parse a typename */
3343 param = parse_typename(parser, NULL, NULL);
3346 vec_push(params, param);
3347 if (param->expression.vtype >= TYPE_VARIANT) {
3348 char typename[1024];
3349 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3350 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3356 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3360 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3361 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3364 if (!parser_next(parser)) {
3365 parseerror(parser, "parse error after typename");
3369 /* now turn 'var' into a function type */
3370 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3371 fval->expression.next = (ast_expression*)var;
3372 fval->expression.variadic = variadic;
3375 var->expression.params = params;
3382 for (i = 0; i < vec_size(params); ++i)
3383 ast_delete(params[i]);
3388 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3390 ast_expression *cexp;
3391 ast_value *cval, *tmp;
3394 ctx = parser_ctx(parser);
3396 if (!parser_next(parser)) {
3398 parseerror(parser, "expected array-size");
3402 cexp = parse_expression_leave(parser, true);
3404 if (!cexp || !ast_istype(cexp, ast_value)) {
3408 parseerror(parser, "expected array-size as constant positive integer");
3411 cval = (ast_value*)cexp;
3413 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3414 tmp->expression.next = (ast_expression*)var;
3417 if (cval->expression.vtype == TYPE_INTEGER)
3418 tmp->expression.count = cval->constval.vint;
3419 else if (cval->expression.vtype == TYPE_FLOAT)
3420 tmp->expression.count = cval->constval.vfloat;
3424 parseerror(parser, "array-size must be a positive integer constant");
3429 if (parser->tok != ']') {
3431 parseerror(parser, "expected ']' after array-size");
3434 if (!parser_next(parser)) {
3436 parseerror(parser, "error after parsing array size");
3442 /* Parse a complete typename.
3443 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3444 * but when parsing variables separated by comma
3445 * 'storebase' should point to where the base-type should be kept.
3446 * The base type makes up every bit of type information which comes *before* the
3449 * The following will be parsed in its entirety:
3451 * The 'basetype' in this case is 'void()'
3452 * and if there's a comma after it, say:
3454 * then the type-information 'void()' can be stored in 'storebase'
3456 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3458 ast_value *var, *tmp;
3461 const char *name = NULL;
3462 bool isfield = false;
3463 bool wasarray = false;
3464 size_t morefields = 0;
3466 ctx = parser_ctx(parser);
3468 /* types may start with a dot */
3469 if (parser->tok == '.') {
3471 /* if we parsed a dot we need a typename now */
3472 if (!parser_next(parser)) {
3473 parseerror(parser, "expected typename for field definition");
3477 /* Further dots are handled seperately because they won't be part of the
3480 while (parser->tok == '.') {
3482 if (!parser_next(parser)) {
3483 parseerror(parser, "expected typename for field definition");
3488 if (parser->tok == TOKEN_IDENT)
3489 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
3490 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3491 parseerror(parser, "expected typename");
3495 /* generate the basic type value */
3496 if (cached_typedef) {
3497 var = ast_value_copy(cached_typedef);
3498 ast_value_set_name(var, "<type(from_def)>");
3500 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3502 for (; morefields; --morefields) {
3503 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
3504 tmp->expression.next = (ast_expression*)var;
3508 /* do not yet turn into a field - remember:
3509 * .void() foo; is a field too
3510 * .void()() foo; is a function
3514 if (!parser_next(parser)) {
3516 parseerror(parser, "parse error after typename");
3520 /* an opening paren now starts the parameter-list of a function
3521 * this is where original-QC has parameter lists.
3522 * We allow a single parameter list here.
3523 * Much like fteqcc we don't allow `float()() x`
3525 if (parser->tok == '(') {
3526 var = parse_parameter_list(parser, var);
3531 /* store the base if requested */
3533 *storebase = ast_value_copy(var);
3535 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3536 tmp->expression.next = (ast_expression*)*storebase;
3541 /* there may be a name now */
3542 if (parser->tok == TOKEN_IDENT) {
3543 name = util_strdup(parser_tokval(parser));
3545 if (!parser_next(parser)) {
3547 parseerror(parser, "error after variable or field declaration");
3552 /* now this may be an array */
3553 if (parser->tok == '[') {
3555 var = parse_arraysize(parser, var);
3560 /* This is the point where we can turn it into a field */
3562 /* turn it into a field if desired */
3563 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3564 tmp->expression.next = (ast_expression*)var;
3568 /* now there may be function parens again */
3569 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3570 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3571 if (parser->tok == '(' && wasarray)
3572 parseerror(parser, "arrays as part of a return type is not supported");
3573 while (parser->tok == '(') {
3574 var = parse_parameter_list(parser, var);
3583 /* finally name it */
3585 if (!ast_value_set_name(var, name)) {
3587 parseerror(parser, "internal error: failed to set name");
3590 /* free the name, ast_value_set_name duplicates */
3597 static bool parse_typedef(parser_t *parser)
3599 ast_value *typevar, *oldtype;
3600 ast_expression *old;
3602 typevar = parse_typename(parser, NULL, NULL);
3607 if ( (old = parser_find_var(parser, typevar->name)) ) {
3608 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3609 " -> `%s` has been declared here: %s:%i",
3610 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3611 ast_delete(typevar);
3615 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
3616 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3617 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3618 ast_delete(typevar);
3622 vec_push(parser->_typedefs, typevar);
3623 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3625 if (parser->tok != ';') {
3626 parseerror(parser, "expected semicolon after typedef");
3629 if (!parser_next(parser)) {
3630 parseerror(parser, "parse error after typedef");
3637 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef)
3641 ast_expression *old;
3645 ast_value *basetype = NULL;
3647 bool isparam = false;
3648 bool isvector = false;
3649 bool cleanvar = true;
3650 bool wasarray = false;
3654 /* get the first complete variable */
3655 var = parse_typename(parser, &basetype, cached_typedef);
3658 ast_delete(basetype);
3666 /* Part 0: finish the type */
3667 if (parser->tok == '(') {
3668 if (opts_standard == COMPILER_QCC)
3669 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3670 var = parse_parameter_list(parser, var);
3676 /* we only allow 1-dimensional arrays */
3677 if (parser->tok == '[') {
3679 var = parse_arraysize(parser, var);
3685 if (parser->tok == '(' && wasarray) {
3686 parseerror(parser, "arrays as part of a return type is not supported");
3687 /* we'll still parse the type completely for now */
3689 /* for functions returning functions */
3690 while (parser->tok == '(') {
3691 if (opts_standard == COMPILER_QCC)
3692 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3693 var = parse_parameter_list(parser, var);
3700 var->cvq = qualifier;
3703 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3704 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3705 * is then filled with the previous definition and the parameter-names replaced.
3708 /* Deal with end_sys_ vars */
3710 if (!strcmp(var->name, "end_sys_globals")) {
3711 parser->crc_globals = vec_size(parser->globals);
3714 else if (!strcmp(var->name, "end_sys_fields")) {
3715 parser->crc_fields = vec_size(parser->fields);
3718 if (was_end && var->expression.vtype == TYPE_FIELD) {
3719 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3720 "global '%s' hint should not be a field",
3721 parser_tokval(parser)))
3728 if (!nofields && var->expression.vtype == TYPE_FIELD)
3730 /* deal with field declarations */
3731 old = parser_find_field(parser, var->name);
3733 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3734 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3743 parseerror(parser, "field `%s` already declared here: %s:%i",
3744 var->name, ast_ctx(old).file, ast_ctx(old).line);
3749 if (opts_standard == COMPILER_QCC &&
3750 (old = parser_find_global(parser, var->name)))
3752 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3753 parseerror(parser, "field `%s` already declared here: %s:%i",
3754 var->name, ast_ctx(old).file, ast_ctx(old).line);
3761 /* deal with other globals */
3762 old = parser_find_global(parser, var->name);
3763 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3765 /* This is a function which had a prototype */
3766 if (!ast_istype(old, ast_value)) {
3767 parseerror(parser, "internal error: prototype is not an ast_value");
3771 proto = (ast_value*)old;
3772 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3773 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3775 ast_ctx(proto).file, ast_ctx(proto).line);
3779 /* we need the new parameter-names */
3780 for (i = 0; i < vec_size(proto->expression.params); ++i)
3781 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3789 if (opts_standard == COMPILER_GMQCC) {
3790 parseerror(parser, "global `%s` already declared here: %s:%i",
3791 var->name, ast_ctx(old).file, ast_ctx(old).line);
3795 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3796 "global `%s` already declared here: %s:%i",
3797 var->name, ast_ctx(old).file, ast_ctx(old).line))
3802 proto = (ast_value*)old;
3803 if (!ast_istype(old, ast_value)) {
3804 parseerror(parser, "internal error: not an ast_value");
3813 if (opts_standard == COMPILER_QCC &&
3814 (old = parser_find_field(parser, var->name)))
3816 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3817 parseerror(parser, "global `%s` already declared here: %s:%i",
3818 var->name, ast_ctx(old).file, ast_ctx(old).line);
3825 else /* it's not a global */
3827 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3828 if (old && !isparam) {
3829 parseerror(parser, "local `%s` already declared here: %s:%i",
3830 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3834 old = parser_find_local(parser, var->name, 0, &isparam);
3835 if (old && isparam) {
3836 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3837 "local `%s` is shadowing a parameter", var->name))
3839 parseerror(parser, "local `%s` already declared here: %s:%i",
3840 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3844 if (opts_standard != COMPILER_GMQCC) {
3853 * Create the global/local, and deal with vector types.
3856 if (var->expression.vtype == TYPE_VECTOR)
3858 else if (var->expression.vtype == TYPE_FIELD &&
3859 var->expression.next->expression.vtype == TYPE_VECTOR)
3863 if (!create_vector_members(var, me)) {
3870 /* deal with global variables, fields, functions */
3871 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
3872 var->isfield = true;
3873 vec_push(parser->fields, (ast_expression*)var);
3874 util_htset(parser->htfields, var->name, var);
3876 for (i = 0; i < 3; ++i) {
3877 vec_push(parser->fields, (ast_expression*)me[i]);
3878 util_htset(parser->htfields, me[i]->name, me[i]);
3883 vec_push(parser->globals, (ast_expression*)var);
3884 util_htset(parser->htglobals, var->name, var);
3886 for (i = 0; i < 3; ++i) {
3887 vec_push(parser->globals, (ast_expression*)me[i]);
3888 util_htset(parser->htglobals, me[i]->name, me[i]);
3893 vec_push(localblock->locals, var);
3894 parser_addlocal(parser, var->name, (ast_expression*)var);
3896 for (i = 0; i < 3; ++i) {
3897 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3898 ast_block_collect(localblock, (ast_expression*)me[i]);
3904 me[0] = me[1] = me[2] = NULL;
3909 if (var->expression.vtype == TYPE_ARRAY) {
3911 snprintf(name, sizeof(name), "%s##SET", var->name);
3912 if (!parser_create_array_setter(parser, var, name))
3914 snprintf(name, sizeof(name), "%s##GET", var->name);
3915 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3918 else if (!localblock && !nofields &&
3919 var->expression.vtype == TYPE_FIELD &&
3920 var->expression.next->expression.vtype == TYPE_ARRAY)
3923 ast_expression *telem;
3925 ast_value *array = (ast_value*)var->expression.next;
3927 if (!ast_istype(var->expression.next, ast_value)) {
3928 parseerror(parser, "internal error: field element type must be an ast_value");
3932 snprintf(name, sizeof(name), "%s##SETF", var->name);
3933 if (!parser_create_array_field_setter(parser, array, name))
3936 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3937 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3938 tfield->expression.next = telem;
3939 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3940 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3948 if (parser->tok == ';') {
3949 ast_delete(basetype);
3950 if (!parser_next(parser)) {
3951 parseerror(parser, "error after variable declaration");
3957 if (parser->tok == ',')
3961 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3964 parseerror(parser, "missing comma or semicolon while parsing variables");
3968 if (localblock && opts_standard == COMPILER_QCC) {
3969 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3970 "initializing expression turns variable `%s` into a constant in this standard",
3977 if (parser->tok != '{') {
3978 if (parser->tok != '=') {
3979 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
3983 if (!parser_next(parser)) {
3984 parseerror(parser, "error parsing initializer");
3988 else if (opts_standard == COMPILER_QCC) {
3989 parseerror(parser, "expected '=' before function body in this standard");
3992 if (parser->tok == '#') {
3993 ast_function *func = NULL;
3996 parseerror(parser, "cannot declare builtins within functions");
3999 if (var->expression.vtype != TYPE_FUNCTION) {
4000 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
4003 if (!parser_next(parser)) {
4004 parseerror(parser, "expected builtin number");
4007 if (parser->tok != TOKEN_INTCONST) {
4008 parseerror(parser, "builtin number must be an integer constant");
4011 if (parser_token(parser)->constval.i < 0) {
4012 parseerror(parser, "builtin number must be an integer greater than zero");
4016 if (var->hasvalue) {
4017 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
4018 "builtin `%s` has already been defined\n"
4019 " -> previous declaration here: %s:%i",
4020 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
4024 func = ast_function_new(ast_ctx(var), var->name, var);
4026 parseerror(parser, "failed to allocate function for `%s`", var->name);
4029 vec_push(parser->functions, func);
4031 func->builtin = -parser_token(parser)->constval.i-1;
4034 if (!parser_next(parser)) {
4035 parseerror(parser, "expected comma or semicolon");
4037 ast_function_delete(func);
4038 var->constval.vfunc = NULL;
4042 else if (parser->tok == '{' || parser->tok == '[')
4046 parseerror(parser, "cannot declare functions within functions");
4051 ast_ctx(proto) = parser_ctx(parser);
4053 if (!parse_function_body(parser, var))
4055 ast_delete(basetype);
4056 for (i = 0; i < vec_size(parser->gotos); ++i)
4057 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
4058 vec_free(parser->gotos);
4059 vec_free(parser->labels);
4062 ast_expression *cexp;
4065 cexp = parse_expression_leave(parser, true);
4070 cval = (ast_value*)cexp;
4071 if (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
4072 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
4075 if (opts_standard != COMPILER_GMQCC &&
4076 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4077 qualifier != CV_VAR)
4079 var->cvq = CV_CONST;
4081 var->hasvalue = true;
4082 if (cval->expression.vtype == TYPE_STRING)
4083 var->constval.vstring = parser_strdup(cval->constval.vstring);
4084 else if (cval->expression.vtype == TYPE_FIELD)
4085 var->constval.vfield = cval;
4087 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
4092 shunt sy = { NULL, NULL };
4095 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
4096 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
4097 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
4098 if (!parser_sy_pop(parser, &sy))
4101 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
4102 parseerror(parser, "internal error: leaked operands");
4103 ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
4112 if (parser->tok == ',') {
4113 if (!parser_next(parser)) {
4114 parseerror(parser, "expected another variable");
4118 if (parser->tok != TOKEN_IDENT) {
4119 parseerror(parser, "expected another variable");
4122 var = ast_value_copy(basetype);
4124 ast_value_set_name(var, parser_tokval(parser));
4125 if (!parser_next(parser)) {
4126 parseerror(parser, "error parsing variable declaration");
4132 if (parser->tok != ';') {
4133 parseerror(parser, "missing semicolon after variables");
4137 if (!parser_next(parser)) {
4138 parseerror(parser, "parse error after variable declaration");
4142 ast_delete(basetype);
4146 if (cleanvar && var)
4148 ast_delete(basetype);
4152 ast_delete(basetype);
4153 if (cleanvar && var)
4155 if (me[0]) ast_member_delete(me[0]);
4156 if (me[1]) ast_member_delete(me[1]);
4157 if (me[2]) ast_member_delete(me[2]);
4161 static bool parser_global_statement(parser_t *parser)
4163 ast_value *istype = NULL;
4164 if (parser->tok == TOKEN_IDENT)
4165 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4167 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4169 return parse_variable(parser, NULL, false, CV_NONE, istype);
4171 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
4173 if (!strcmp(parser_tokval(parser), "var")) {
4174 if (!parser_next(parser)) {
4175 parseerror(parser, "expected variable declaration after 'var'");
4178 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "const")) {
4179 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `const` after 'var' qualifier");
4180 if (!parser_next(parser)) {
4181 parseerror(parser, "expected variable declaration after 'const var'");
4185 return parse_variable(parser, NULL, true, CV_VAR, NULL);
4188 else if (parser->tok == TOKEN_KEYWORD)
4190 if (!strcmp(parser_tokval(parser), "const")) {
4191 if (!parser_next(parser)) {
4192 parseerror(parser, "expected variable declaration after 'const'");
4195 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var")) {
4196 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `var` after const qualifier");
4197 if (!parser_next(parser)) {
4198 parseerror(parser, "expected variable declaration after 'const var'");
4202 return parse_variable(parser, NULL, true, CV_CONST, NULL);
4204 else if (!strcmp(parser_tokval(parser), "typedef")) {
4205 if (!parser_next(parser)) {
4206 parseerror(parser, "expected type definition after 'typedef'");
4209 return parse_typedef(parser);
4211 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4214 else if (parser->tok == '$')
4216 if (!parser_next(parser)) {
4217 parseerror(parser, "parse error");
4223 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4229 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4231 return util_crc16(old, str, strlen(str));
4234 static void progdefs_crc_file(const char *str)
4236 /* write to progdefs.h here */
4240 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4242 old = progdefs_crc_sum(old, str);
4243 progdefs_crc_file(str);
4247 static void generate_checksum(parser_t *parser)
4249 uint16_t crc = 0xFFFF;
4253 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4254 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4256 progdefs_crc_file("\tint\tpad;\n");
4257 progdefs_crc_file("\tint\tofs_return[3];\n");
4258 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4259 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4260 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4261 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4262 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4263 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4264 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4265 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4267 for (i = 0; i < parser->crc_globals; ++i) {
4268 if (!ast_istype(parser->globals[i], ast_value))
4270 value = (ast_value*)(parser->globals[i]);
4271 switch (value->expression.vtype) {
4272 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4273 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4274 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4275 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4277 crc = progdefs_crc_both(crc, "\tint\t");
4280 crc = progdefs_crc_both(crc, value->name);
4281 crc = progdefs_crc_both(crc, ";\n");
4283 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
4284 for (i = 0; i < parser->crc_fields; ++i) {
4285 if (!ast_istype(parser->fields[i], ast_value))
4287 value = (ast_value*)(parser->fields[i]);
4288 switch (value->expression.next->expression.vtype) {
4289 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4290 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4291 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4292 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4294 crc = progdefs_crc_both(crc, "\tint\t");
4297 crc = progdefs_crc_both(crc, value->name);
4298 crc = progdefs_crc_both(crc, ";\n");
4300 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
4305 static parser_t *parser;
4311 parser = (parser_t*)mem_a(sizeof(parser_t));
4315 memset(parser, 0, sizeof(*parser));
4317 for (i = 0; i < operator_count; ++i) {
4318 if (operators[i].id == opid1('=')) {
4319 parser->assign_op = operators+i;
4323 if (!parser->assign_op) {
4324 printf("internal error: initializing parser: failed to find assign operator\n");
4329 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
4330 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
4331 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
4332 vec_push(parser->_blocktypedefs, 0);
4336 bool parser_compile()
4338 /* initial lexer/parser state */
4339 parser->lex->flags.noops = true;
4341 if (parser_next(parser))
4343 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4345 if (!parser_global_statement(parser)) {
4346 if (parser->tok == TOKEN_EOF)
4347 parseerror(parser, "unexpected eof");
4348 else if (!parser->errors)
4349 parseerror(parser, "there have been errors, bailing out");
4350 lex_close(parser->lex);
4356 parseerror(parser, "parse error");
4357 lex_close(parser->lex);
4362 lex_close(parser->lex);
4365 return !parser->errors;
4368 bool parser_compile_file(const char *filename)
4370 parser->lex = lex_open(filename);
4372 con_err("failed to open file \"%s\"\n", filename);
4375 return parser_compile();
4378 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4380 parser->lex = lex_open_string(str, len, name);
4382 con_err("failed to create lexer for string \"%s\"\n", name);
4385 return parser_compile();
4388 bool parser_compile_string(const char *name, const char *str)
4390 parser->lex = lex_open_string(str, strlen(str), name);
4392 con_err("failed to create lexer for string \"%s\"\n", name);
4395 return parser_compile();
4398 void parser_cleanup()
4401 for (i = 0; i < vec_size(parser->accessors); ++i) {
4402 ast_delete(parser->accessors[i]->constval.vfunc);
4403 parser->accessors[i]->constval.vfunc = NULL;
4404 ast_delete(parser->accessors[i]);
4406 for (i = 0; i < vec_size(parser->functions); ++i) {
4407 ast_delete(parser->functions[i]);
4409 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4410 ast_delete(parser->imm_vector[i]);
4412 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4413 ast_delete(parser->imm_string[i]);
4415 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4416 ast_delete(parser->imm_float[i]);
4418 for (i = 0; i < vec_size(parser->fields); ++i) {
4419 ast_delete(parser->fields[i]);
4421 for (i = 0; i < vec_size(parser->globals); ++i) {
4422 ast_delete(parser->globals[i]);
4424 vec_free(parser->accessors);
4425 vec_free(parser->functions);
4426 vec_free(parser->imm_vector);
4427 vec_free(parser->imm_string);
4428 vec_free(parser->imm_float);
4429 vec_free(parser->globals);
4430 vec_free(parser->fields);
4432 for (i = 0; i < vec_size(parser->variables); ++i)
4433 util_htdel(parser->variables[i]);
4434 vec_free(parser->variables);
4435 vec_free(parser->_blocklocals);
4436 vec_free(parser->_locals);
4438 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4439 ast_delete(parser->_typedefs[i]);
4440 vec_free(parser->_typedefs);
4441 for (i = 0; i < vec_size(parser->typedefs); ++i)
4442 util_htdel(parser->typedefs[i]);
4443 vec_free(parser->typedefs);
4444 vec_free(parser->_blocktypedefs);
4446 vec_free(parser->_block_ctx);
4448 vec_free(parser->labels);
4449 vec_free(parser->gotos);
4454 bool parser_finish(const char *output)
4460 if (!parser->errors)
4462 ir = ir_builder_new("gmqcc_out");
4464 con_out("failed to allocate builder\n");
4468 for (i = 0; i < vec_size(parser->fields); ++i) {
4471 if (!ast_istype(parser->fields[i], ast_value))
4473 field = (ast_value*)parser->fields[i];
4474 hasvalue = field->hasvalue;
4475 field->hasvalue = false;
4476 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4477 con_out("failed to generate field %s\n", field->name);
4478 ir_builder_delete(ir);
4483 ast_expression *subtype;
4484 field->hasvalue = true;
4485 subtype = field->expression.next;
4486 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4487 if (subtype->expression.vtype == TYPE_FIELD)
4488 ifld->fieldtype = subtype->expression.next->expression.vtype;
4489 else if (subtype->expression.vtype == TYPE_FUNCTION)
4490 ifld->outtype = subtype->expression.next->expression.vtype;
4491 (void)!ir_value_set_field(field->ir_v, ifld);
4494 for (i = 0; i < vec_size(parser->globals); ++i) {
4496 if (!ast_istype(parser->globals[i], ast_value))
4498 asvalue = (ast_value*)(parser->globals[i]);
4499 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
4500 if (strcmp(asvalue->name, "end_sys_globals") &&
4501 strcmp(asvalue->name, "end_sys_fields"))
4503 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4504 "unused global: `%s`", asvalue->name);
4507 if (!ast_global_codegen(asvalue, ir, false)) {
4508 con_out("failed to generate global %s\n", asvalue->name);
4509 ir_builder_delete(ir);
4513 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4514 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4515 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4516 ir_builder_delete(ir);
4520 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4521 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4522 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4523 ir_builder_delete(ir);
4527 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4528 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4529 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4530 ir_builder_delete(ir);
4534 for (i = 0; i < vec_size(parser->globals); ++i) {
4536 if (!ast_istype(parser->globals[i], ast_value))
4538 asvalue = (ast_value*)(parser->globals[i]);
4539 if (asvalue->setter) {
4540 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4541 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4542 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4544 printf("failed to generate setter for %s\n", asvalue->name);
4545 ir_builder_delete(ir);
4549 if (asvalue->getter) {
4550 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4551 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4552 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4554 printf("failed to generate getter for %s\n", asvalue->name);
4555 ir_builder_delete(ir);
4560 for (i = 0; i < vec_size(parser->fields); ++i) {
4562 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4564 if (!ast_istype((ast_expression*)asvalue, ast_value))
4566 if (asvalue->expression.vtype != TYPE_ARRAY)
4568 if (asvalue->setter) {
4569 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4570 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4571 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4573 printf("failed to generate setter for %s\n", asvalue->name);
4574 ir_builder_delete(ir);
4578 if (asvalue->getter) {
4579 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4580 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4581 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4583 printf("failed to generate getter for %s\n", asvalue->name);
4584 ir_builder_delete(ir);
4589 for (i = 0; i < vec_size(parser->functions); ++i) {
4590 if (!ast_function_codegen(parser->functions[i], ir)) {
4591 con_out("failed to generate function %s\n", parser->functions[i]->name);
4592 ir_builder_delete(ir);
4597 ir_builder_dump(ir, con_out);
4598 for (i = 0; i < vec_size(parser->functions); ++i) {
4599 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4600 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4601 ir_builder_delete(ir);
4608 ir_builder_dump(ir, con_out);
4610 generate_checksum(parser);
4612 if (!ir_builder_generate(ir, output)) {
4613 con_out("*** failed to generate output file\n");
4614 ir_builder_delete(ir);
4619 ir_builder_delete(ir);
4623 con_out("*** there were compile errors\n");