2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define PARSER_HT_LOCALS 2
31 #define PARSER_HT_SIZE 512
32 #define TYPEDEF_HT_SIZE 512
34 static void parser_enterblock(parser_t *parser);
35 static bool parser_leaveblock(parser_t *parser);
36 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
37 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
38 static bool parse_typedef(parser_t *parser);
39 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring);
40 static ast_block* parse_block(parser_t *parser);
41 static bool parse_block_into(parser_t *parser, ast_block *block);
42 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
43 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
44 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels);
45 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels);
46 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
47 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
48 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
50 static void parseerror(parser_t *parser, const char *fmt, ...)
54 vcompile_error(parser->lex->tok.ctx, fmt, ap);
58 /* returns true if it counts as an error */
59 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
64 r = vcompile_warning(parser->lex->tok.ctx, warntype, fmt, ap);
69 /**********************************************************************
73 static bool parser_next(parser_t *parser)
75 /* lex_do kills the previous token */
76 parser->tok = lex_do(parser->lex);
77 if (parser->tok == TOKEN_EOF)
79 if (parser->tok >= TOKEN_ERROR) {
80 parseerror(parser, "lex error");
86 #define parser_tokval(p) ((p)->lex->tok.value)
87 #define parser_token(p) (&((p)->lex->tok))
89 char *parser_strdup(const char *str)
92 /* actually dup empty strings */
93 char *out = (char*)mem_a(1);
97 return util_strdup(str);
100 static ast_expression* parser_find_field(parser_t *parser, const char *name)
102 return ( ast_expression*)util_htget(parser->htfields, name);
105 static ast_expression* parser_find_label(parser_t *parser, const char *name)
108 for(i = 0; i < vec_size(parser->labels); i++)
109 if (!strcmp(parser->labels[i]->name, name))
110 return (ast_expression*)parser->labels[i];
114 ast_expression* parser_find_global(parser_t *parser, const char *name)
116 ast_expression *var = (ast_expression*)util_htget(parser->aliases, parser_tokval(parser));
119 return (ast_expression*)util_htget(parser->htglobals, name);
122 static ast_expression* parser_find_param(parser_t *parser, const char *name)
126 if (!parser->function)
128 fun = parser->function->vtype;
129 for (i = 0; i < vec_size(fun->expression.params); ++i) {
130 if (!strcmp(fun->expression.params[i]->name, name))
131 return (ast_expression*)(fun->expression.params[i]);
136 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
141 hash = util_hthash(parser->htglobals, name);
144 for (i = vec_size(parser->variables); i > upto;) {
146 if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
150 return parser_find_param(parser, name);
153 static ast_expression* parser_find_var(parser_t *parser, const char *name)
157 v = parser_find_local(parser, name, 0, &dummy);
158 if (!v) v = parser_find_global(parser, name);
162 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
166 hash = util_hthash(parser->typedefs[0], name);
168 for (i = vec_size(parser->typedefs); i > upto;) {
170 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
178 size_t etype; /* 0 = expression, others are operators */
182 ast_block *block; /* for commas and function calls */
201 static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
212 static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
216 e.out = (ast_expression*)v;
223 static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
225 e.etype = 1 + (op - operators);
234 static sy_elem syparen(lex_ctx_t ctx, size_t off) {
245 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
246 * so we need to rotate it to become ent.(foo[n]).
248 static bool rotate_entfield_array_index_nodes(ast_expression **out)
250 ast_array_index *index, *oldindex;
251 ast_entfield *entfield;
255 ast_expression *entity;
257 lex_ctx_t ctx = ast_ctx(*out);
259 if (!ast_istype(*out, ast_array_index))
261 index = (ast_array_index*)*out;
263 if (!ast_istype(index->array, ast_entfield))
265 entfield = (ast_entfield*)index->array;
267 if (!ast_istype(entfield->field, ast_value))
269 field = (ast_value*)entfield->field;
272 entity = entfield->entity;
276 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
277 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
278 *out = (ast_expression*)entfield;
280 oldindex->array = NULL;
281 oldindex->index = NULL;
282 ast_delete(oldindex);
287 static bool check_write_to(lex_ctx_t ctx, ast_expression *expr)
289 if (ast_istype(expr, ast_value)) {
290 ast_value *val = (ast_value*)expr;
291 if (val->cvq == CV_CONST) {
292 if (val->name[0] == '#') {
293 compile_error(ctx, "invalid assignment to a literal constant");
297 * To work around quakeworld we must elide the error and make it
300 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC)
301 compile_error(ctx, "assignment to constant `%s`", val->name);
303 (void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->name);
310 static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
314 ast_expression *out = NULL;
315 ast_expression *exprs[3];
316 ast_block *blocks[3];
317 ast_binstore *asbinstore;
318 size_t i, assignop, addop, subop;
319 qcint_t generated_op = 0;
324 if (!vec_size(sy->ops)) {
325 parseerror(parser, "internal error: missing operator");
329 if (vec_last(sy->ops).isparen) {
330 parseerror(parser, "unmatched parenthesis");
334 op = &operators[vec_last(sy->ops).etype - 1];
335 ctx = vec_last(sy->ops).ctx;
337 if (vec_size(sy->out) < op->operands) {
338 if (op->flags & OP_PREFIX)
339 compile_error(ctx, "expected expression after unary operator `%s`", op->op, (int)op->id);
340 else /* this should have errored previously already */
341 compile_error(ctx, "expected expression after operator `%s`", op->op, (int)op->id);
345 vec_shrinkby(sy->ops, 1);
347 /* op(:?) has no input and no output */
351 vec_shrinkby(sy->out, op->operands);
352 for (i = 0; i < op->operands; ++i) {
353 exprs[i] = sy->out[vec_size(sy->out)+i].out;
354 blocks[i] = sy->out[vec_size(sy->out)+i].block;
356 if (exprs[i]->vtype == TYPE_NOEXPR &&
357 !(i != 0 && op->id == opid2('?',':')) &&
358 !(i == 1 && op->id == opid1('.')))
360 if (ast_istype(exprs[i], ast_label))
361 compile_error(ast_ctx(exprs[i]), "expected expression, got an unknown identifier");
363 compile_error(ast_ctx(exprs[i]), "not an expression");
364 (void)!compile_warning(ast_ctx(exprs[i]), WARN_DEBUG, "expression %u\n", (unsigned int)i);
368 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
369 compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
373 #define NotSameType(T) \
374 (exprs[0]->vtype != exprs[1]->vtype || \
375 exprs[0]->vtype != T)
379 compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
383 if (exprs[0]->vtype == TYPE_VECTOR &&
384 exprs[1]->vtype == TYPE_NOEXPR)
386 if (exprs[1] == (ast_expression*)parser->const_vec[0])
387 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
388 else if (exprs[1] == (ast_expression*)parser->const_vec[1])
389 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
390 else if (exprs[1] == (ast_expression*)parser->const_vec[2])
391 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
393 compile_error(ctx, "access to invalid vector component");
397 else if (exprs[0]->vtype == TYPE_ENTITY) {
398 if (exprs[1]->vtype != TYPE_FIELD) {
399 compile_error(ast_ctx(exprs[1]), "type error: right hand of member-operand should be an entity-field");
402 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
404 else if (exprs[0]->vtype == TYPE_VECTOR) {
405 compile_error(ast_ctx(exprs[1]), "vectors cannot be accessed this way");
409 compile_error(ast_ctx(exprs[1]), "type error: member-of operator on something that is not an entity or vector");
415 if (exprs[0]->vtype != TYPE_ARRAY &&
416 !(exprs[0]->vtype == TYPE_FIELD &&
417 exprs[0]->next->vtype == TYPE_ARRAY))
419 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
420 compile_error(ast_ctx(exprs[0]), "cannot index value of type %s", ty1);
423 if (exprs[1]->vtype != TYPE_FLOAT) {
424 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
425 compile_error(ast_ctx(exprs[1]), "index must be of type float, not %s", ty1);
428 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
429 if (rotate_entfield_array_index_nodes(&out))
432 /* This is not broken in fteqcc anymore */
433 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
434 /* this error doesn't need to make us bail out */
435 (void)!parsewarning(parser, WARN_EXTENSIONS,
436 "accessing array-field members of an entity without parenthesis\n"
437 " -> this is an extension from -std=gmqcc");
444 if (vec_size(sy->paren) && vec_last(sy->paren) == PAREN_FUNC) {
445 vec_push(sy->out, syexp(ctx, exprs[0]));
446 vec_push(sy->out, syexp(ctx, exprs[1]));
447 vec_last(sy->argc)++;
451 if (!ast_block_add_expr(blocks[0], exprs[1]))
454 blocks[0] = ast_block_new(ctx);
455 if (!ast_block_add_expr(blocks[0], exprs[0]) ||
456 !ast_block_add_expr(blocks[0], exprs[1]))
461 ast_block_set_type(blocks[0], exprs[1]);
463 vec_push(sy->out, syblock(ctx, blocks[0]));
470 if ((out = fold_op(parser->fold, op, exprs)))
472 if (exprs[0]->vtype != TYPE_FLOAT &&
473 exprs[0]->vtype != TYPE_VECTOR) {
474 compile_error(ctx, "invalid types used in unary expression: cannot negate type %s",
475 type_name[exprs[0]->vtype]);
478 out = (ast_expression*)ast_unary_new(ctx, (VINSTR_NEG_F-TYPE_FLOAT) + exprs[0]->vtype, exprs[0]);
482 if (!(out = fold_op(parser->fold, op, exprs))) {
483 switch (exprs[0]->vtype) {
485 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
488 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
491 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
492 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
494 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
496 /* we don't constant-fold NOT for these types */
498 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
501 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
504 compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
505 type_name[exprs[0]->vtype]);
512 if (exprs[0]->vtype != exprs[1]->vtype ||
513 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
515 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
516 type_name[exprs[0]->vtype],
517 type_name[exprs[1]->vtype]);
520 if (!(out = fold_op(parser->fold, op, exprs))) {
521 switch (exprs[0]->vtype) {
523 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
526 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
529 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
530 type_name[exprs[0]->vtype],
531 type_name[exprs[1]->vtype]);
537 if (exprs[0]->vtype != exprs[1]->vtype ||
538 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT))
540 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
541 type_name[exprs[1]->vtype],
542 type_name[exprs[0]->vtype]);
545 if (!(out = fold_op(parser->fold, op, exprs))) {
546 switch (exprs[0]->vtype) {
548 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
551 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
554 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
555 type_name[exprs[1]->vtype],
556 type_name[exprs[0]->vtype]);
562 if (exprs[0]->vtype != exprs[1]->vtype &&
563 !(exprs[0]->vtype == TYPE_VECTOR &&
564 exprs[1]->vtype == TYPE_FLOAT) &&
565 !(exprs[1]->vtype == TYPE_VECTOR &&
566 exprs[0]->vtype == TYPE_FLOAT)
569 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
570 type_name[exprs[1]->vtype],
571 type_name[exprs[0]->vtype]);
574 if (!(out = fold_op(parser->fold, op, exprs))) {
575 switch (exprs[0]->vtype) {
577 if (exprs[1]->vtype == TYPE_VECTOR)
578 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
580 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
583 if (exprs[1]->vtype == TYPE_FLOAT)
584 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
586 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
589 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
590 type_name[exprs[1]->vtype],
591 type_name[exprs[0]->vtype]);
598 if (exprs[1]->vtype != TYPE_FLOAT) {
599 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
600 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
601 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
604 if (!(out = fold_op(parser->fold, op, exprs))) {
605 if (exprs[0]->vtype == TYPE_FLOAT)
606 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
608 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
609 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
610 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
617 if (NotSameType(TYPE_FLOAT)) {
618 compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
619 type_name[exprs[0]->vtype],
620 type_name[exprs[1]->vtype]);
622 } else if (!(out = fold_op(parser->fold, op, exprs))) {
623 /* generate a call to __builtin_mod */
624 ast_expression *mod = intrin_func(parser->intrin, "mod");
625 ast_call *call = NULL;
626 if (!mod) return false; /* can return null for missing floor */
628 call = ast_call_new(parser_ctx(parser), mod);
629 vec_push(call->params, exprs[0]);
630 vec_push(call->params, exprs[1]);
632 out = (ast_expression*)call;
637 compile_error(ctx, "%= is unimplemented");
643 if ( !(exprs[0]->vtype == TYPE_FLOAT && exprs[1]->vtype == TYPE_FLOAT) &&
644 !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_FLOAT) &&
645 !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_VECTOR))
647 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
648 type_name[exprs[0]->vtype],
649 type_name[exprs[1]->vtype]);
653 if (!(out = fold_op(parser->fold, op, exprs))) {
655 * IF the first expression is float, the following will be too
656 * since scalar ^ vector is not allowed.
658 if (exprs[0]->vtype == TYPE_FLOAT) {
659 out = (ast_expression*)ast_binary_new(ctx,
660 (op->id == opid1('^') ? VINSTR_BITXOR : op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
664 * The first is a vector: vector is allowed to bitop with vector and
665 * with scalar, branch here for the second operand.
667 if (exprs[1]->vtype == TYPE_VECTOR) {
669 * Bitop all the values of the vector components against the
670 * vectors components in question.
672 out = (ast_expression*)ast_binary_new(ctx,
673 (op->id == opid1('^') ? VINSTR_BITXOR_V : op->id == opid1('|') ? VINSTR_BITOR_V : VINSTR_BITAND_V),
676 out = (ast_expression*)ast_binary_new(ctx,
677 (op->id == opid1('^') ? VINSTR_BITXOR_VF : op->id == opid1('|') ? VINSTR_BITOR_VF : VINSTR_BITAND_VF),
686 case opid3('<','<','='):
687 case opid3('>','>','='):
688 if(!(out = fold_op(parser->fold, op, exprs))) {
689 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-shifts");
695 generated_op += 1; /* INSTR_OR */
697 generated_op += INSTR_AND;
698 if (!(out = fold_op(parser->fold, op, exprs))) {
699 if (OPTS_FLAG(PERL_LOGIC) && !ast_compare_type(exprs[0], exprs[1])) {
700 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
701 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
702 compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
705 for (i = 0; i < 2; ++i) {
706 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->vtype == TYPE_VECTOR) {
707 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[i]);
709 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
711 exprs[i] = out; out = NULL;
712 if (OPTS_FLAG(PERL_LOGIC)) {
713 /* here we want to keep the right expressions' type */
717 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->vtype == TYPE_STRING) {
718 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[i]);
720 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
722 exprs[i] = out; out = NULL;
723 if (OPTS_FLAG(PERL_LOGIC)) {
724 /* here we want to keep the right expressions' type */
729 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
734 if (vec_last(sy->paren) != PAREN_TERNARY2) {
735 compile_error(ctx, "mismatched parenthesis/ternary");
739 if (!ast_compare_type(exprs[1], exprs[2])) {
740 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
741 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
742 compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
745 if (!(out = fold_op(parser->fold, op, exprs)))
746 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
749 case opid2('*', '*'):
750 if (NotSameType(TYPE_FLOAT)) {
751 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
752 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
753 compile_error(ctx, "invalid types used in exponentiation: %s and %s",
758 if (!(out = fold_op(parser->fold, op, exprs))) {
759 ast_call *gencall = ast_call_new(parser_ctx(parser), intrin_func(parser->intrin, "pow"));
760 vec_push(gencall->params, exprs[0]);
761 vec_push(gencall->params, exprs[1]);
762 out = (ast_expression*)gencall;
766 case opid2('>', '<'):
767 if (NotSameType(TYPE_VECTOR)) {
768 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
769 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
770 compile_error(ctx, "invalid types used in cross product: %s and %s",
775 if (!(out = fold_op(parser->fold, op, exprs))) {
776 out = (ast_expression*)ast_binary_new(
786 case opid3('<','=','>'): /* -1, 0, or 1 */
787 if (NotSameType(TYPE_FLOAT)) {
788 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
789 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
790 compile_error(ctx, "invalid types used in comparision: %s and %s",
796 if (!(out = fold_op(parser->fold, op, exprs))) {
797 ast_binary *eq = ast_binary_new(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
799 eq->refs = AST_REF_NONE;
802 out = (ast_expression*)ast_ternary_new(ctx,
803 (ast_expression*)ast_binary_new(ctx, INSTR_LT, exprs[0], exprs[1]),
805 (ast_expression*)parser->fold->imm_float[2],
808 (ast_expression*)ast_ternary_new(ctx, (ast_expression*)eq,
810 (ast_expression*)parser->fold->imm_float[0],
813 (ast_expression*)parser->fold->imm_float[1]
823 generated_op += 1; /* INSTR_GT */
825 generated_op += 1; /* INSTR_LT */
826 case opid2('>', '='):
827 generated_op += 1; /* INSTR_GE */
828 case opid2('<', '='):
829 generated_op += INSTR_LE;
830 if (NotSameType(TYPE_FLOAT)) {
831 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
832 type_name[exprs[0]->vtype],
833 type_name[exprs[1]->vtype]);
836 if (!(out = fold_op(parser->fold, op, exprs)))
837 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
839 case opid2('!', '='):
840 if (exprs[0]->vtype != exprs[1]->vtype) {
841 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
842 type_name[exprs[0]->vtype],
843 type_name[exprs[1]->vtype]);
846 if (!(out = fold_op(parser->fold, op, exprs)))
847 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->vtype], exprs[0], exprs[1]);
849 case opid2('=', '='):
850 if (exprs[0]->vtype != exprs[1]->vtype) {
851 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
852 type_name[exprs[0]->vtype],
853 type_name[exprs[1]->vtype]);
856 if (!(out = fold_op(parser->fold, op, exprs)))
857 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->vtype], exprs[0], exprs[1]);
861 if (ast_istype(exprs[0], ast_entfield)) {
862 ast_expression *field = ((ast_entfield*)exprs[0])->field;
863 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
864 exprs[0]->vtype == TYPE_FIELD &&
865 exprs[0]->next->vtype == TYPE_VECTOR)
867 assignop = type_storep_instr[TYPE_VECTOR];
870 assignop = type_storep_instr[exprs[0]->vtype];
871 if (assignop == VINSTR_END || !ast_compare_type(field->next, exprs[1]))
873 ast_type_to_string(field->next, ty1, sizeof(ty1));
874 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
875 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
876 field->next->vtype == TYPE_FUNCTION &&
877 exprs[1]->vtype == TYPE_FUNCTION)
879 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
880 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
883 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
888 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
889 exprs[0]->vtype == TYPE_FIELD &&
890 exprs[0]->next->vtype == TYPE_VECTOR)
892 assignop = type_store_instr[TYPE_VECTOR];
895 assignop = type_store_instr[exprs[0]->vtype];
898 if (assignop == VINSTR_END) {
899 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
900 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
901 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
903 else if (!ast_compare_type(exprs[0], exprs[1]))
905 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
906 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
907 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
908 exprs[0]->vtype == TYPE_FUNCTION &&
909 exprs[1]->vtype == TYPE_FUNCTION)
911 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
912 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
915 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
918 (void)check_write_to(ctx, exprs[0]);
919 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
921 case opid3('+','+','P'):
922 case opid3('-','-','P'):
924 if (exprs[0]->vtype != TYPE_FLOAT) {
925 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
926 compile_error(ast_ctx(exprs[0]), "invalid type for prefix increment: %s", ty1);
929 if (op->id == opid3('+','+','P'))
933 (void)check_write_to(ast_ctx(exprs[0]), exprs[0]);
934 if (ast_istype(exprs[0], ast_entfield)) {
935 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
937 (ast_expression*)parser->fold->imm_float[1]);
939 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
941 (ast_expression*)parser->fold->imm_float[1]);
944 case opid3('S','+','+'):
945 case opid3('S','-','-'):
947 if (exprs[0]->vtype != TYPE_FLOAT) {
948 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
949 compile_error(ast_ctx(exprs[0]), "invalid type for suffix increment: %s", ty1);
952 if (op->id == opid3('S','+','+')) {
959 (void)check_write_to(ast_ctx(exprs[0]), exprs[0]);
960 if (ast_istype(exprs[0], ast_entfield)) {
961 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
963 (ast_expression*)parser->fold->imm_float[1]);
965 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
967 (ast_expression*)parser->fold->imm_float[1]);
971 out = (ast_expression*)ast_binary_new(ctx, subop,
973 (ast_expression*)parser->fold->imm_float[1]);
977 if (exprs[0]->vtype != exprs[1]->vtype ||
978 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
980 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
981 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
982 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
986 (void)check_write_to(ctx, exprs[0]);
987 if (ast_istype(exprs[0], ast_entfield))
988 assignop = type_storep_instr[exprs[0]->vtype];
990 assignop = type_store_instr[exprs[0]->vtype];
991 switch (exprs[0]->vtype) {
993 out = (ast_expression*)ast_binstore_new(ctx, assignop,
994 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
998 out = (ast_expression*)ast_binstore_new(ctx, assignop,
999 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1000 exprs[0], exprs[1]);
1003 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1004 type_name[exprs[0]->vtype],
1005 type_name[exprs[1]->vtype]);
1009 case opid2('*','='):
1010 case opid2('/','='):
1011 if (exprs[1]->vtype != TYPE_FLOAT ||
1012 !(exprs[0]->vtype == TYPE_FLOAT ||
1013 exprs[0]->vtype == TYPE_VECTOR))
1015 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1016 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1017 compile_error(ctx, "invalid types used in expression: %s and %s",
1021 (void)check_write_to(ctx, exprs[0]);
1022 if (ast_istype(exprs[0], ast_entfield))
1023 assignop = type_storep_instr[exprs[0]->vtype];
1025 assignop = type_store_instr[exprs[0]->vtype];
1026 switch (exprs[0]->vtype) {
1028 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1029 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1030 exprs[0], exprs[1]);
1033 if (op->id == opid2('*','=')) {
1034 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1035 exprs[0], exprs[1]);
1037 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1038 (ast_expression*)parser->fold->imm_float[1],
1041 compile_error(ctx, "internal error: failed to generate division");
1044 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1049 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1050 type_name[exprs[0]->vtype],
1051 type_name[exprs[1]->vtype]);
1055 case opid2('&','='):
1056 case opid2('|','='):
1057 case opid2('^','='):
1058 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1059 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1060 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1061 compile_error(ctx, "invalid types used in expression: %s and %s",
1065 (void)check_write_to(ctx, exprs[0]);
1066 if (ast_istype(exprs[0], ast_entfield))
1067 assignop = type_storep_instr[exprs[0]->vtype];
1069 assignop = type_store_instr[exprs[0]->vtype];
1070 if (exprs[0]->vtype == TYPE_FLOAT)
1071 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1072 (op->id == opid2('^','=') ? VINSTR_BITXOR : op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1073 exprs[0], exprs[1]);
1075 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1076 (op->id == opid2('^','=') ? VINSTR_BITXOR_V : op->id == opid2('&','=') ? VINSTR_BITAND_V : VINSTR_BITOR_V),
1077 exprs[0], exprs[1]);
1079 case opid3('&','~','='):
1080 /* This is like: a &= ~(b);
1081 * But QC has no bitwise-not, so we implement it as
1084 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1085 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1086 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1087 compile_error(ctx, "invalid types used in expression: %s and %s",
1091 if (ast_istype(exprs[0], ast_entfield))
1092 assignop = type_storep_instr[exprs[0]->vtype];
1094 assignop = type_store_instr[exprs[0]->vtype];
1095 if (exprs[0]->vtype == TYPE_FLOAT)
1096 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1098 out = (ast_expression*)ast_binary_new(ctx, VINSTR_BITAND_V, exprs[0], exprs[1]);
1101 (void)check_write_to(ctx, exprs[0]);
1102 if (exprs[0]->vtype == TYPE_FLOAT)
1103 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1105 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_V, exprs[0], out);
1106 asbinstore->keep_dest = true;
1107 out = (ast_expression*)asbinstore;
1110 case opid2('~', 'P'):
1111 if (exprs[0]->vtype != TYPE_FLOAT && exprs[0]->vtype != TYPE_VECTOR) {
1112 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1113 compile_error(ast_ctx(exprs[0]), "invalid type for bit not: %s", ty1);
1116 if (!(out = fold_op(parser->fold, op, exprs))) {
1117 if (exprs[0]->vtype == TYPE_FLOAT) {
1118 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser->fold->imm_float[2], exprs[0]);
1120 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, (ast_expression*)parser->fold->imm_vector[1], exprs[0]);
1127 compile_error(ctx, "failed to apply operator %s", op->op);
1131 vec_push(sy->out, syexp(ctx, out));
1135 static bool parser_close_call(parser_t *parser, shunt *sy)
1137 /* was a function call */
1138 ast_expression *fun;
1139 ast_value *funval = NULL;
1143 size_t paramcount, i;
1146 fid = vec_last(sy->ops).off;
1147 vec_shrinkby(sy->ops, 1);
1149 /* out[fid] is the function
1150 * everything above is parameters...
1152 if (!vec_size(sy->argc)) {
1153 parseerror(parser, "internal error: no argument counter available");
1157 paramcount = vec_last(sy->argc);
1160 if (vec_size(sy->out) < fid) {
1161 parseerror(parser, "internal error: broken function call%lu < %lu+%lu\n",
1162 (unsigned long)vec_size(sy->out),
1164 (unsigned long)paramcount);
1169 * TODO handle this at the intrinsic level with an ast_intrinsic
1172 if ((fun = sy->out[fid].out) == intrin_debug_typestring(parser->intrin)) {
1174 if (fid+2 != vec_size(sy->out) ||
1175 vec_last(sy->out).block)
1177 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1180 ast_type_to_string(vec_last(sy->out).out, ty, sizeof(ty));
1181 ast_unref(vec_last(sy->out).out);
1182 sy->out[fid] = syexp(ast_ctx(vec_last(sy->out).out),
1183 (ast_expression*)fold_constgen_string(parser->fold, ty, false));
1184 vec_shrinkby(sy->out, 1);
1189 * Now we need to determine if the function that is being called is
1190 * an intrinsic so we can evaluate if the arguments to it are constant
1191 * and than fruitfully fold them.
1193 #define fold_can_1(X) \
1194 (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
1195 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
1197 if (fid + 1 < vec_size(sy->out))
1200 for (i = 0; i < paramcount; ++i) {
1201 if (!fold_can_1((ast_value*)sy->out[fid + 1 + i].out)) {
1208 * All is well which ends well, if we make it into here we can ignore the
1209 * intrinsic call and just evaluate it i.e constant fold it.
1211 if (fold && ast_istype(fun, ast_value) && ((ast_value*)fun)->intrinsic) {
1212 ast_expression **exprs = NULL;
1213 ast_expression *foldval = NULL;
1215 for (i = 0; i < paramcount; i++)
1216 vec_push(exprs, sy->out[fid+1 + i].out);
1218 if (!(foldval = intrin_fold(parser->intrin, (ast_value*)fun, exprs))) {
1224 * Blub: what sorts of unreffing and resizing of
1225 * sy->out should I be doing here?
1227 sy->out[fid] = syexp(foldval->node.context, foldval);
1228 vec_shrinkby(sy->out, 1);
1235 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1240 if (fid+1 + paramcount != vec_size(sy->out)) {
1241 parseerror(parser, "internal error: parameter count mismatch: (%lu+1+%lu), %lu",
1242 (unsigned long)fid, (unsigned long)paramcount, (unsigned long)vec_size(sy->out));
1246 for (i = 0; i < paramcount; ++i)
1247 vec_push(call->params, sy->out[fid+1 + i].out);
1248 vec_shrinkby(sy->out, paramcount);
1249 (void)!ast_call_check_types(call, parser->function->vtype->expression.varparam);
1250 if (parser->max_param_count < paramcount)
1251 parser->max_param_count = paramcount;
1253 if (ast_istype(fun, ast_value)) {
1254 funval = (ast_value*)fun;
1255 if ((fun->flags & AST_FLAG_VARIADIC) &&
1256 !(/*funval->cvq == CV_CONST && */ funval->hasvalue && funval->constval.vfunc->builtin))
1258 call->va_count = (ast_expression*)fold_constgen_float(parser->fold, (qcfloat_t)paramcount);
1262 /* overwrite fid, the function, with a call */
1263 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1265 if (fun->vtype != TYPE_FUNCTION) {
1266 parseerror(parser, "not a function (%s)", type_name[fun->vtype]);
1271 parseerror(parser, "could not determine function return type");
1274 ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1276 if (fun->flags & AST_FLAG_DEPRECATED) {
1278 return !parsewarning(parser, WARN_DEPRECATED,
1279 "call to function (which is marked deprecated)\n",
1280 "-> it has been declared here: %s:%i",
1281 ast_ctx(fun).file, ast_ctx(fun).line);
1284 return !parsewarning(parser, WARN_DEPRECATED,
1285 "call to `%s` (which is marked deprecated)\n"
1286 "-> `%s` declared here: %s:%i",
1287 fval->name, fval->name, ast_ctx(fun).file, ast_ctx(fun).line);
1289 return !parsewarning(parser, WARN_DEPRECATED,
1290 "call to `%s` (deprecated: %s)\n"
1291 "-> `%s` declared here: %s:%i",
1292 fval->name, fval->desc, fval->name, ast_ctx(fun).file,
1296 if (vec_size(fun->params) != paramcount &&
1297 !((fun->flags & AST_FLAG_VARIADIC) &&
1298 vec_size(fun->params) < paramcount))
1300 const char *fewmany = (vec_size(fun->params) > paramcount) ? "few" : "many";
1302 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1303 "too %s parameters for call to %s: expected %i, got %i\n"
1304 " -> `%s` has been declared here: %s:%i",
1305 fewmany, fval->name, (int)vec_size(fun->params), (int)paramcount,
1306 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1308 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1309 "too %s parameters for function call: expected %i, got %i\n"
1310 " -> it has been declared here: %s:%i",
1311 fewmany, (int)vec_size(fun->params), (int)paramcount,
1312 ast_ctx(fun).file, (int)ast_ctx(fun).line);
1319 static bool parser_close_paren(parser_t *parser, shunt *sy)
1321 if (!vec_size(sy->ops)) {
1322 parseerror(parser, "unmatched closing paren");
1326 while (vec_size(sy->ops)) {
1327 if (vec_last(sy->ops).isparen) {
1328 if (vec_last(sy->paren) == PAREN_FUNC) {
1330 if (!parser_close_call(parser, sy))
1334 if (vec_last(sy->paren) == PAREN_EXPR) {
1336 if (!vec_size(sy->out)) {
1337 compile_error(vec_last(sy->ops).ctx, "empty paren expression");
1338 vec_shrinkby(sy->ops, 1);
1341 vec_shrinkby(sy->ops, 1);
1344 if (vec_last(sy->paren) == PAREN_INDEX) {
1346 /* pop off the parenthesis */
1347 vec_shrinkby(sy->ops, 1);
1348 /* then apply the index operator */
1349 if (!parser_sy_apply_operator(parser, sy))
1353 if (vec_last(sy->paren) == PAREN_TERNARY1) {
1354 vec_last(sy->paren) = PAREN_TERNARY2;
1355 /* pop off the parenthesis */
1356 vec_shrinkby(sy->ops, 1);
1359 compile_error(vec_last(sy->ops).ctx, "invalid parenthesis");
1362 if (!parser_sy_apply_operator(parser, sy))
1368 static void parser_reclassify_token(parser_t *parser)
1371 if (parser->tok >= TOKEN_START)
1373 for (i = 0; i < operator_count; ++i) {
1374 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1375 parser->tok = TOKEN_OPERATOR;
1381 static ast_expression* parse_vararg_do(parser_t *parser)
1383 ast_expression *idx, *out;
1385 ast_value *funtype = parser->function->vtype;
1386 lex_ctx_t ctx = parser_ctx(parser);
1388 if (!parser->function->varargs) {
1389 parseerror(parser, "function has no variable argument list");
1393 if (!parser_next(parser) || parser->tok != '(') {
1394 parseerror(parser, "expected parameter index and type in parenthesis");
1397 if (!parser_next(parser)) {
1398 parseerror(parser, "error parsing parameter index");
1402 idx = parse_expression_leave(parser, true, false, false);
1406 if (parser->tok != ',') {
1407 if (parser->tok != ')') {
1409 parseerror(parser, "expected comma after parameter index");
1412 /* vararg piping: ...(start) */
1413 out = (ast_expression*)ast_argpipe_new(ctx, idx);
1417 if (!parser_next(parser) || (parser->tok != TOKEN_IDENT && parser->tok != TOKEN_TYPENAME)) {
1419 parseerror(parser, "expected typename for vararg");
1423 typevar = parse_typename(parser, NULL, NULL);
1429 if (parser->tok != ')') {
1431 ast_delete(typevar);
1432 parseerror(parser, "expected closing paren");
1436 if (funtype->expression.varparam &&
1437 !ast_compare_type((ast_expression*)typevar, (ast_expression*)funtype->expression.varparam))
1441 ast_type_to_string((ast_expression*)typevar, ty1, sizeof(ty1));
1442 ast_type_to_string((ast_expression*)funtype->expression.varparam, ty2, sizeof(ty2));
1443 compile_error(ast_ctx(typevar),
1444 "function was declared to take varargs of type `%s`, requested type is: %s",
1448 out = (ast_expression*)ast_array_index_new(ctx, (ast_expression*)(parser->function->varargs), idx);
1449 ast_type_adopt(out, typevar);
1450 ast_delete(typevar);
1454 static ast_expression* parse_vararg(parser_t *parser)
1456 bool old_noops = parser->lex->flags.noops;
1458 ast_expression *out;
1460 parser->lex->flags.noops = true;
1461 out = parse_vararg_do(parser);
1463 parser->lex->flags.noops = old_noops;
1467 /* not to be exposed */
1468 bool ftepp_predef_exists(const char *name);
1469 static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
1471 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1472 parser->tok == TOKEN_IDENT &&
1473 !strcmp(parser_tokval(parser), "_"))
1475 /* a translatable string */
1478 parser->lex->flags.noops = true;
1479 if (!parser_next(parser) || parser->tok != '(') {
1480 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1483 parser->lex->flags.noops = false;
1484 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1485 parseerror(parser, "expected a constant string in translatable-string extension");
1488 val = (ast_value*)fold_constgen_string(parser->fold, parser_tokval(parser), true);
1491 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1493 if (!parser_next(parser) || parser->tok != ')') {
1494 parseerror(parser, "expected closing paren after translatable string");
1499 else if (parser->tok == TOKEN_DOTS)
1502 if (!OPTS_FLAG(VARIADIC_ARGS)) {
1503 parseerror(parser, "cannot access varargs (try -fvariadic-args)");
1506 va = parse_vararg(parser);
1509 vec_push(sy->out, syexp(parser_ctx(parser), va));
1512 else if (parser->tok == TOKEN_FLOATCONST) {
1513 ast_expression *val = fold_constgen_float(parser->fold, (parser_token(parser)->constval.f));
1516 vec_push(sy->out, syexp(parser_ctx(parser), val));
1519 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1520 ast_expression *val = fold_constgen_float(parser->fold, (qcfloat_t)(parser_token(parser)->constval.i));
1523 vec_push(sy->out, syexp(parser_ctx(parser), val));
1526 else if (parser->tok == TOKEN_STRINGCONST) {
1527 ast_expression *val = fold_constgen_string(parser->fold, parser_tokval(parser), false);
1530 vec_push(sy->out, syexp(parser_ctx(parser), val));
1533 else if (parser->tok == TOKEN_VECTORCONST) {
1534 ast_expression *val = fold_constgen_vector(parser->fold, parser_token(parser)->constval.v);
1537 vec_push(sy->out, syexp(parser_ctx(parser), val));
1540 else if (parser->tok == TOKEN_IDENT)
1542 const char *ctoken = parser_tokval(parser);
1543 ast_expression *prev = vec_size(sy->out) ? vec_last(sy->out).out : NULL;
1544 ast_expression *var;
1545 /* a_vector.{x,y,z} */
1546 if (!vec_size(sy->ops) ||
1547 !vec_last(sy->ops).etype ||
1548 operators[vec_last(sy->ops).etype-1].id != opid1('.'))
1550 /* When adding more intrinsics, fix the above condition */
1553 if (prev && prev->vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
1555 var = (ast_expression*)parser->const_vec[ctoken[0]-'x'];
1557 var = parser_find_var(parser, parser_tokval(parser));
1559 var = parser_find_field(parser, parser_tokval(parser));
1561 if (!var && with_labels) {
1562 var = (ast_expression*)parser_find_label(parser, parser_tokval(parser));
1564 ast_label *lbl = ast_label_new(parser_ctx(parser), parser_tokval(parser), true);
1565 var = (ast_expression*)lbl;
1566 vec_push(parser->labels, lbl);
1569 if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
1570 var = (ast_expression*)fold_constgen_string(parser->fold, parser->function->name, false);
1573 * now we try for the real intrinsic hashtable. If the string
1574 * begins with __builtin, we simply skip past it, otherwise we
1575 * use the identifier as is.
1577 if (!strncmp(parser_tokval(parser), "__builtin_", 10)) {
1578 var = intrin_func(parser->intrin, parser_tokval(parser));
1582 char *correct = NULL;
1586 * sometimes people use preprocessing predefs without enabling them
1587 * i've done this thousands of times already myself. Lets check for
1588 * it in the predef table. And diagnose it better :)
1590 if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
1591 parseerror(parser, "unexpected identifier: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
1596 * TODO: determine the best score for the identifier: be it
1597 * a variable, a field.
1599 * We should also consider adding correction tables for
1600 * other things as well.
1602 if (OPTS_OPTION_BOOL(OPTION_CORRECTION) && strlen(parser_tokval(parser)) <= 16) {
1604 correct_init(&corr);
1606 for (i = 0; i < vec_size(parser->correct_variables); i++) {
1607 correct = correct_str(&corr, parser->correct_variables[i], parser_tokval(parser));
1608 if (strcmp(correct, parser_tokval(parser))) {
1615 correct_free(&corr);
1618 parseerror(parser, "unexpected identifier: %s (did you mean %s?)", parser_tokval(parser), correct);
1623 parseerror(parser, "unexpected identifier: %s", parser_tokval(parser));
1629 if (ast_istype(var, ast_value)) {
1630 ((ast_value*)var)->uses++;
1632 else if (ast_istype(var, ast_member)) {
1633 ast_member *mem = (ast_member*)var;
1634 if (ast_istype(mem->owner, ast_value))
1635 ((ast_value*)(mem->owner))->uses++;
1638 vec_push(sy->out, syexp(parser_ctx(parser), var));
1641 parseerror(parser, "unexpected token `%s`", parser_tokval(parser));
1645 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels)
1647 ast_expression *expr = NULL;
1650 bool wantop = false;
1651 /* only warn once about an assignment in a truth value because the current code
1652 * would trigger twice on: if(a = b && ...), once for the if-truth-value, once for the && part
1654 bool warn_parenthesis = true;
1656 /* count the parens because an if starts with one, so the
1657 * end of a condition is an unmatched closing paren
1661 memset(&sy, 0, sizeof(sy));
1663 parser->lex->flags.noops = false;
1665 parser_reclassify_token(parser);
1669 if (parser->tok == TOKEN_TYPENAME) {
1670 parseerror(parser, "unexpected typename `%s`", parser_tokval(parser));
1674 if (parser->tok == TOKEN_OPERATOR)
1676 /* classify the operator */
1677 const oper_info *op;
1678 const oper_info *olast = NULL;
1680 for (o = 0; o < operator_count; ++o) {
1681 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
1682 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1683 !strcmp(parser_tokval(parser), operators[o].op))
1688 if (o == operator_count) {
1689 compile_error(parser_ctx(parser), "unexpected operator: %s", parser_tokval(parser));
1692 /* found an operator */
1695 /* when declaring variables, a comma starts a new variable */
1696 if (op->id == opid1(',') && !vec_size(sy.paren) && stopatcomma) {
1697 /* fixup the token */
1702 /* a colon without a pervious question mark cannot be a ternary */
1703 if (!ternaries && op->id == opid2(':','?')) {
1708 if (op->id == opid1(',')) {
1709 if (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
1710 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
1714 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
1715 olast = &operators[vec_last(sy.ops).etype-1];
1717 /* first only apply higher precedences, assoc_left+equal comes after we warn about precedence rules */
1718 while (olast && op->prec < olast->prec)
1720 if (!parser_sy_apply_operator(parser, &sy))
1722 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
1723 olast = &operators[vec_last(sy.ops).etype-1];
1728 #define IsAssignOp(x) (\
1729 (x) == opid1('=') || \
1730 (x) == opid2('+','=') || \
1731 (x) == opid2('-','=') || \
1732 (x) == opid2('*','=') || \
1733 (x) == opid2('/','=') || \
1734 (x) == opid2('%','=') || \
1735 (x) == opid2('&','=') || \
1736 (x) == opid2('|','=') || \
1737 (x) == opid3('&','~','=') \
1739 if (warn_parenthesis) {
1740 if ( (olast && IsAssignOp(olast->id) && (op->id == opid2('&','&') || op->id == opid2('|','|'))) ||
1741 (olast && IsAssignOp(op->id) && (olast->id == opid2('&','&') || olast->id == opid2('|','|'))) ||
1742 (truthvalue && !vec_size(sy.paren) && IsAssignOp(op->id))
1745 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around assignment used as truth value");
1746 warn_parenthesis = false;
1749 if (olast && olast->id != op->id) {
1750 if ((op->id == opid1('&') || op->id == opid1('|') || op->id == opid1('^')) &&
1751 (olast->id == opid1('&') || olast->id == opid1('|') || olast->id == opid1('^')))
1753 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around bitwise operations");
1754 warn_parenthesis = false;
1756 else if ((op->id == opid2('&','&') || op->id == opid2('|','|')) &&
1757 (olast->id == opid2('&','&') || olast->id == opid2('|','|')))
1759 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around logical operations");
1760 warn_parenthesis = false;
1766 (op->prec < olast->prec) ||
1767 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1769 if (!parser_sy_apply_operator(parser, &sy))
1771 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
1772 olast = &operators[vec_last(sy.ops).etype-1];
1777 if (op->id == opid1('(')) {
1779 size_t sycount = vec_size(sy.out);
1780 /* we expected an operator, this is the function-call operator */
1781 vec_push(sy.paren, PAREN_FUNC);
1782 vec_push(sy.ops, syparen(parser_ctx(parser), sycount-1));
1783 vec_push(sy.argc, 0);
1785 vec_push(sy.paren, PAREN_EXPR);
1786 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1789 } else if (op->id == opid1('[')) {
1791 parseerror(parser, "unexpected array subscript");
1794 vec_push(sy.paren, PAREN_INDEX);
1795 /* push both the operator and the paren, this makes life easier */
1796 vec_push(sy.ops, syop(parser_ctx(parser), op));
1797 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1799 } else if (op->id == opid2('?',':')) {
1800 vec_push(sy.ops, syop(parser_ctx(parser), op));
1801 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1804 vec_push(sy.paren, PAREN_TERNARY1);
1805 } else if (op->id == opid2(':','?')) {
1806 if (!vec_size(sy.paren)) {
1807 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1810 if (vec_last(sy.paren) != PAREN_TERNARY1) {
1811 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1814 if (!parser_close_paren(parser, &sy))
1816 vec_push(sy.ops, syop(parser_ctx(parser), op));
1820 vec_push(sy.ops, syop(parser_ctx(parser), op));
1821 wantop = !!(op->flags & OP_SUFFIX);
1824 else if (parser->tok == ')') {
1825 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
1826 if (!parser_sy_apply_operator(parser, &sy))
1829 if (!vec_size(sy.paren))
1832 if (vec_last(sy.paren) == PAREN_TERNARY1) {
1833 parseerror(parser, "mismatched parentheses (closing paren in ternary expression?)");
1836 if (!parser_close_paren(parser, &sy))
1839 /* must be a function call without parameters */
1840 if (vec_last(sy.paren) != PAREN_FUNC) {
1841 parseerror(parser, "closing paren in invalid position");
1844 if (!parser_close_paren(parser, &sy))
1849 else if (parser->tok == '(') {
1850 parseerror(parser, "internal error: '(' should be classified as operator");
1853 else if (parser->tok == '[') {
1854 parseerror(parser, "internal error: '[' should be classified as operator");
1857 else if (parser->tok == ']') {
1858 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
1859 if (!parser_sy_apply_operator(parser, &sy))
1862 if (!vec_size(sy.paren))
1864 if (vec_last(sy.paren) != PAREN_INDEX) {
1865 parseerror(parser, "mismatched parentheses, unexpected ']'");
1868 if (!parser_close_paren(parser, &sy))
1873 if (!parse_sya_operand(parser, &sy, with_labels))
1876 if (vec_size(sy.paren) && vec_last(sy.ops).isparen && vec_last(sy.paren) == PAREN_FUNC)
1877 vec_last(sy.argc)++;
1882 /* in this case we might want to allow constant string concatenation */
1883 bool concatenated = false;
1884 if (parser->tok == TOKEN_STRINGCONST && vec_size(sy.out)) {
1885 ast_expression *lexpr = vec_last(sy.out).out;
1886 if (ast_istype(lexpr, ast_value)) {
1887 ast_value *last = (ast_value*)lexpr;
1888 if (last->isimm == true && last->cvq == CV_CONST &&
1889 last->hasvalue && last->expression.vtype == TYPE_STRING)
1891 char *newstr = NULL;
1892 util_asprintf(&newstr, "%s%s", last->constval.vstring, parser_tokval(parser));
1893 vec_last(sy.out).out = (ast_expression*)fold_constgen_string(parser->fold, newstr, false);
1895 concatenated = true;
1899 if (!concatenated) {
1900 parseerror(parser, "expected operator or end of statement");
1905 if (!parser_next(parser)) {
1908 if (parser->tok == ';' ||
1909 ((!vec_size(sy.paren) || (vec_size(sy.paren) == 1 && vec_last(sy.paren) == PAREN_TERNARY2)) &&
1910 (parser->tok == ']' || parser->tok == ')' || parser->tok == '}')))
1916 while (vec_size(sy.ops)) {
1917 if (!parser_sy_apply_operator(parser, &sy))
1921 parser->lex->flags.noops = true;
1922 if (vec_size(sy.out) != 1) {
1923 parseerror(parser, "expression expected");
1926 expr = sy.out[0].out;
1929 if (vec_size(sy.paren)) {
1930 parseerror(parser, "internal error: vec_size(sy.paren) = %lu", (unsigned long)vec_size(sy.paren));
1938 parser->lex->flags.noops = true;
1939 for (i = 0; i < vec_size(sy.out); ++i) {
1941 ast_unref(sy.out[i].out);
1950 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels)
1952 ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
1955 if (parser->tok != ';') {
1956 parseerror(parser, "semicolon expected after expression");
1960 if (!parser_next(parser)) {
1967 static void parser_enterblock(parser_t *parser)
1969 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1970 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1971 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1972 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1973 vec_push(parser->_block_ctx, parser_ctx(parser));
1976 vec_push(parser->correct_variables, correct_trie_new());
1977 vec_push(parser->correct_variables_score, NULL);
1980 static bool parser_leaveblock(parser_t *parser)
1983 size_t locals, typedefs;
1985 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1986 parseerror(parser, "internal error: parser_leaveblock with no block");
1990 util_htdel(vec_last(parser->variables));
1991 correct_del(vec_last(parser->correct_variables), vec_last(parser->correct_variables_score));
1993 vec_pop(parser->variables);
1994 vec_pop(parser->correct_variables);
1995 vec_pop(parser->correct_variables_score);
1996 if (!vec_size(parser->_blocklocals)) {
1997 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
2001 locals = vec_last(parser->_blocklocals);
2002 vec_pop(parser->_blocklocals);
2003 while (vec_size(parser->_locals) != locals) {
2004 ast_expression *e = vec_last(parser->_locals);
2005 ast_value *v = (ast_value*)e;
2006 vec_pop(parser->_locals);
2007 if (ast_istype(e, ast_value) && !v->uses) {
2008 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
2013 typedefs = vec_last(parser->_blocktypedefs);
2014 while (vec_size(parser->_typedefs) != typedefs) {
2015 ast_delete(vec_last(parser->_typedefs));
2016 vec_pop(parser->_typedefs);
2018 util_htdel(vec_last(parser->typedefs));
2019 vec_pop(parser->typedefs);
2021 vec_pop(parser->_block_ctx);
2026 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
2028 vec_push(parser->_locals, e);
2029 util_htset(vec_last(parser->variables), name, (void*)e);
2033 vec_last(parser->correct_variables),
2034 &vec_last(parser->correct_variables_score),
2039 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
2041 vec_push(parser->globals, e);
2042 util_htset(parser->htglobals, name, e);
2046 parser->correct_variables[0],
2047 &parser->correct_variables_score[0],
2052 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
2056 ast_expression *prev;
2058 if (cond->vtype == TYPE_VOID || cond->vtype >= TYPE_VARIANT) {
2060 ast_type_to_string(cond, ty, sizeof(ty));
2061 compile_error(ast_ctx(cond), "invalid type for if() condition: %s", ty);
2064 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->vtype == TYPE_STRING)
2067 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_S, cond);
2070 parseerror(parser, "internal error: failed to process condition");
2075 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->vtype == TYPE_VECTOR)
2077 /* vector types need to be cast to true booleans */
2078 ast_binary *bin = (ast_binary*)cond;
2079 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->op == INSTR_AND || bin->op == INSTR_OR))
2081 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
2083 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_V, cond);
2086 parseerror(parser, "internal error: failed to process condition");
2093 unary = (ast_unary*)cond;
2094 /* ast_istype dereferences cond, should test here for safety */
2095 while (cond && ast_istype(cond, ast_unary) && unary->op == INSTR_NOT_F)
2097 cond = unary->operand;
2098 unary->operand = NULL;
2101 unary = (ast_unary*)cond;
2105 parseerror(parser, "internal error: failed to process condition");
2107 if (ifnot) *_ifnot = !*_ifnot;
2111 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
2114 ast_expression *cond, *ontrue = NULL, *onfalse = NULL;
2117 lex_ctx_t ctx = parser_ctx(parser);
2119 (void)block; /* not touching */
2121 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2122 if (!parser_next(parser)) {
2123 parseerror(parser, "expected condition or 'not'");
2126 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
2128 if (!parser_next(parser)) {
2129 parseerror(parser, "expected condition in parenthesis");
2133 if (parser->tok != '(') {
2134 parseerror(parser, "expected 'if' condition in parenthesis");
2137 /* parse into the expression */
2138 if (!parser_next(parser)) {
2139 parseerror(parser, "expected 'if' condition after opening paren");
2142 /* parse the condition */
2143 cond = parse_expression_leave(parser, false, true, false);
2147 if (parser->tok != ')') {
2148 parseerror(parser, "expected closing paren after 'if' condition");
2152 /* parse into the 'then' branch */
2153 if (!parser_next(parser)) {
2154 parseerror(parser, "expected statement for on-true branch of 'if'");
2158 if (!parse_statement_or_block(parser, &ontrue)) {
2163 ontrue = (ast_expression*)ast_block_new(parser_ctx(parser));
2164 /* check for an else */
2165 if (!strcmp(parser_tokval(parser), "else")) {
2166 /* parse into the 'else' branch */
2167 if (!parser_next(parser)) {
2168 parseerror(parser, "expected on-false branch after 'else'");
2173 if (!parse_statement_or_block(parser, &onfalse)) {
2180 cond = process_condition(parser, cond, &ifnot);
2182 if (ontrue) ast_delete(ontrue);
2183 if (onfalse) ast_delete(onfalse);
2188 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
2190 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
2191 *out = (ast_expression*)ifthen;
2195 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out);
2196 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
2201 /* skip the 'while' and get the body */
2202 if (!parser_next(parser)) {
2203 if (OPTS_FLAG(LOOP_LABELS))
2204 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2206 parseerror(parser, "expected 'while' condition in parenthesis");
2210 if (parser->tok == ':') {
2211 if (!OPTS_FLAG(LOOP_LABELS))
2212 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2213 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2214 parseerror(parser, "expected loop label");
2217 label = util_strdup(parser_tokval(parser));
2218 if (!parser_next(parser)) {
2220 parseerror(parser, "expected 'while' condition in parenthesis");
2225 if (parser->tok != '(') {
2226 parseerror(parser, "expected 'while' condition in parenthesis");
2230 vec_push(parser->breaks, label);
2231 vec_push(parser->continues, label);
2233 rv = parse_while_go(parser, block, out);
2236 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2237 parseerror(parser, "internal error: label stack corrupted");
2243 vec_pop(parser->breaks);
2244 vec_pop(parser->continues);
2249 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out)
2252 ast_expression *cond, *ontrue;
2256 lex_ctx_t ctx = parser_ctx(parser);
2258 (void)block; /* not touching */
2260 /* parse into the expression */
2261 if (!parser_next(parser)) {
2262 parseerror(parser, "expected 'while' condition after opening paren");
2265 /* parse the condition */
2266 cond = parse_expression_leave(parser, false, true, false);
2270 if (parser->tok != ')') {
2271 parseerror(parser, "expected closing paren after 'while' condition");
2275 /* parse into the 'then' branch */
2276 if (!parser_next(parser)) {
2277 parseerror(parser, "expected while-loop body");
2281 if (!parse_statement_or_block(parser, &ontrue)) {
2286 cond = process_condition(parser, cond, &ifnot);
2291 aloop = ast_loop_new(ctx, NULL, cond, ifnot, NULL, false, NULL, ontrue);
2292 *out = (ast_expression*)aloop;
2296 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out);
2297 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
2302 /* skip the 'do' and get the body */
2303 if (!parser_next(parser)) {
2304 if (OPTS_FLAG(LOOP_LABELS))
2305 parseerror(parser, "expected loop label or body");
2307 parseerror(parser, "expected loop body");
2311 if (parser->tok == ':') {
2312 if (!OPTS_FLAG(LOOP_LABELS))
2313 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2314 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2315 parseerror(parser, "expected loop label");
2318 label = util_strdup(parser_tokval(parser));
2319 if (!parser_next(parser)) {
2321 parseerror(parser, "expected loop body");
2326 vec_push(parser->breaks, label);
2327 vec_push(parser->continues, label);
2329 rv = parse_dowhile_go(parser, block, out);
2332 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2333 parseerror(parser, "internal error: label stack corrupted");
2336 * Test for NULL otherwise ast_delete dereferences null pointer
2344 vec_pop(parser->breaks);
2345 vec_pop(parser->continues);
2350 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out)
2353 ast_expression *cond, *ontrue;
2357 lex_ctx_t ctx = parser_ctx(parser);
2359 (void)block; /* not touching */
2361 if (!parse_statement_or_block(parser, &ontrue))
2364 /* expect the "while" */
2365 if (parser->tok != TOKEN_KEYWORD ||
2366 strcmp(parser_tokval(parser), "while"))
2368 parseerror(parser, "expected 'while' and condition");
2373 /* skip the 'while' and check for opening paren */
2374 if (!parser_next(parser) || parser->tok != '(') {
2375 parseerror(parser, "expected 'while' condition in parenthesis");
2379 /* parse into the expression */
2380 if (!parser_next(parser)) {
2381 parseerror(parser, "expected 'while' condition after opening paren");
2385 /* parse the condition */
2386 cond = parse_expression_leave(parser, false, true, false);
2390 if (parser->tok != ')') {
2391 parseerror(parser, "expected closing paren after 'while' condition");
2397 if (!parser_next(parser) || parser->tok != ';') {
2398 parseerror(parser, "expected semicolon after condition");
2404 if (!parser_next(parser)) {
2405 parseerror(parser, "parse error");
2411 cond = process_condition(parser, cond, &ifnot);
2416 aloop = ast_loop_new(ctx, NULL, NULL, false, cond, ifnot, NULL, ontrue);
2417 *out = (ast_expression*)aloop;
2421 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
2422 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
2427 /* skip the 'for' and check for opening paren */
2428 if (!parser_next(parser)) {
2429 if (OPTS_FLAG(LOOP_LABELS))
2430 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2432 parseerror(parser, "expected 'for' expressions in parenthesis");
2436 if (parser->tok == ':') {
2437 if (!OPTS_FLAG(LOOP_LABELS))
2438 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2439 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2440 parseerror(parser, "expected loop label");
2443 label = util_strdup(parser_tokval(parser));
2444 if (!parser_next(parser)) {
2446 parseerror(parser, "expected 'for' expressions in parenthesis");
2451 if (parser->tok != '(') {
2452 parseerror(parser, "expected 'for' expressions in parenthesis");
2456 vec_push(parser->breaks, label);
2457 vec_push(parser->continues, label);
2459 rv = parse_for_go(parser, block, out);
2462 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2463 parseerror(parser, "internal error: label stack corrupted");
2469 vec_pop(parser->breaks);
2470 vec_pop(parser->continues);
2474 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
2477 ast_expression *initexpr, *cond, *increment, *ontrue;
2482 lex_ctx_t ctx = parser_ctx(parser);
2484 parser_enterblock(parser);
2491 /* parse into the expression */
2492 if (!parser_next(parser)) {
2493 parseerror(parser, "expected 'for' initializer after opening paren");
2498 if (parser->tok == TOKEN_IDENT)
2499 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2501 if (typevar || parser->tok == TOKEN_TYPENAME) {
2503 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
2504 if (parsewarning(parser, WARN_EXTENSIONS,
2505 "current standard does not allow variable declarations in for-loop initializers"))
2509 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, NULL))
2512 else if (parser->tok != ';')
2514 initexpr = parse_expression_leave(parser, false, false, false);
2519 /* move on to condition */
2520 if (parser->tok != ';') {
2521 parseerror(parser, "expected semicolon after for-loop initializer");
2524 if (!parser_next(parser)) {
2525 parseerror(parser, "expected for-loop condition");
2529 /* parse the condition */
2530 if (parser->tok != ';') {
2531 cond = parse_expression_leave(parser, false, true, false);
2536 /* move on to incrementor */
2537 if (parser->tok != ';') {
2538 parseerror(parser, "expected semicolon after for-loop initializer");
2541 if (!parser_next(parser)) {
2542 parseerror(parser, "expected for-loop condition");
2546 /* parse the incrementor */
2547 if (parser->tok != ')') {
2548 lex_ctx_t condctx = parser_ctx(parser);
2549 increment = parse_expression_leave(parser, false, false, false);
2552 if (!ast_side_effects(increment)) {
2553 if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2559 if (parser->tok != ')') {
2560 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2563 /* parse into the 'then' branch */
2564 if (!parser_next(parser)) {
2565 parseerror(parser, "expected for-loop body");
2568 if (!parse_statement_or_block(parser, &ontrue))
2572 cond = process_condition(parser, cond, &ifnot);
2576 aloop = ast_loop_new(ctx, initexpr, cond, ifnot, NULL, false, increment, ontrue);
2577 *out = (ast_expression*)aloop;
2579 if (!parser_leaveblock(parser)) {
2585 if (initexpr) ast_unref(initexpr);
2586 if (cond) ast_unref(cond);
2587 if (increment) ast_unref(increment);
2588 (void)!parser_leaveblock(parser);
2592 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2594 ast_expression *exp = NULL;
2595 ast_expression *var = NULL;
2596 ast_return *ret = NULL;
2597 ast_value *retval = parser->function->return_value;
2598 ast_value *expected = parser->function->vtype;
2600 lex_ctx_t ctx = parser_ctx(parser);
2602 (void)block; /* not touching */
2604 if (!parser_next(parser)) {
2605 parseerror(parser, "expected return expression");
2609 /* return assignments */
2610 if (parser->tok == '=') {
2611 if (!OPTS_FLAG(RETURN_ASSIGNMENTS)) {
2612 parseerror(parser, "return assignments not activated, try using -freturn-assigments");
2616 if (type_store_instr[expected->expression.next->vtype] == VINSTR_END) {
2618 ast_type_to_string(expected->expression.next, ty1, sizeof(ty1));
2619 parseerror(parser, "invalid return type: `%s'", ty1);
2623 if (!parser_next(parser)) {
2624 parseerror(parser, "expected return assignment expression");
2628 if (!(exp = parse_expression_leave(parser, false, false, false)))
2631 /* prepare the return value */
2633 retval = ast_value_new(ctx, "#LOCAL_RETURN", TYPE_VOID);
2634 ast_type_adopt(retval, expected->expression.next);
2635 parser->function->return_value = retval;
2638 if (!ast_compare_type(exp, (ast_expression*)retval)) {
2639 char ty1[1024], ty2[1024];
2640 ast_type_to_string(exp, ty1, sizeof(ty1));
2641 ast_type_to_string(&retval->expression, ty2, sizeof(ty2));
2642 parseerror(parser, "invalid type for return value: `%s', expected `%s'", ty1, ty2);
2645 /* store to 'return' local variable */
2646 var = (ast_expression*)ast_store_new(
2648 type_store_instr[expected->expression.next->vtype],
2649 (ast_expression*)retval, exp);
2656 if (parser->tok != ';')
2657 parseerror(parser, "missing semicolon after return assignment");
2658 else if (!parser_next(parser))
2659 parseerror(parser, "parse error after return assignment");
2665 if (parser->tok != ';') {
2666 exp = parse_expression(parser, false, false);
2670 if (exp->vtype != TYPE_NIL &&
2671 exp->vtype != ((ast_expression*)expected)->next->vtype)
2673 parseerror(parser, "return with invalid expression");
2676 ret = ast_return_new(ctx, exp);
2682 if (!parser_next(parser))
2683 parseerror(parser, "parse error");
2685 if (!retval && expected->expression.next->vtype != TYPE_VOID)
2687 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2689 ret = ast_return_new(ctx, (ast_expression*)retval);
2691 *out = (ast_expression*)ret;
2695 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2698 unsigned int levels = 0;
2699 lex_ctx_t ctx = parser_ctx(parser);
2700 const char **loops = (is_continue ? parser->continues : parser->breaks);
2702 (void)block; /* not touching */
2703 if (!parser_next(parser)) {
2704 parseerror(parser, "expected semicolon or loop label");
2708 if (!vec_size(loops)) {
2710 parseerror(parser, "`continue` can only be used inside loops");
2712 parseerror(parser, "`break` can only be used inside loops or switches");
2715 if (parser->tok == TOKEN_IDENT) {
2716 if (!OPTS_FLAG(LOOP_LABELS))
2717 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2718 i = vec_size(loops);
2720 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
2723 parseerror(parser, "no such loop to %s: `%s`",
2724 (is_continue ? "continue" : "break out of"),
2725 parser_tokval(parser));
2730 if (!parser_next(parser)) {
2731 parseerror(parser, "expected semicolon");
2736 if (parser->tok != ';') {
2737 parseerror(parser, "expected semicolon");
2741 if (!parser_next(parser))
2742 parseerror(parser, "parse error");
2744 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
2748 /* returns true when it was a variable qualifier, false otherwise!
2749 * on error, cvq is set to CV_WRONG
2751 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
2753 bool had_const = false;
2754 bool had_var = false;
2755 bool had_noref = false;
2756 bool had_attrib = false;
2757 bool had_static = false;
2762 if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
2764 /* parse an attribute */
2765 if (!parser_next(parser)) {
2766 parseerror(parser, "expected attribute after `[[`");
2770 if (!strcmp(parser_tokval(parser), "noreturn")) {
2771 flags |= AST_FLAG_NORETURN;
2772 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2773 parseerror(parser, "`noreturn` attribute has no parameters, expected `]]`");
2778 else if (!strcmp(parser_tokval(parser), "noref")) {
2780 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2781 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2786 else if (!strcmp(parser_tokval(parser), "inline")) {
2787 flags |= AST_FLAG_INLINE;
2788 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2789 parseerror(parser, "`inline` attribute has no parameters, expected `]]`");
2794 else if (!strcmp(parser_tokval(parser), "eraseable")) {
2795 flags |= AST_FLAG_ERASEABLE;
2796 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2797 parseerror(parser, "`eraseable` attribute has no parameters, expected `]]`");
2802 else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
2803 flags |= AST_FLAG_ALIAS;
2806 if (!parser_next(parser)) {
2807 parseerror(parser, "parse error in attribute");
2811 if (parser->tok == '(') {
2812 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
2813 parseerror(parser, "`alias` attribute missing parameter");
2817 *message = util_strdup(parser_tokval(parser));
2819 if (!parser_next(parser)) {
2820 parseerror(parser, "parse error in attribute");
2824 if (parser->tok != ')') {
2825 parseerror(parser, "`alias` attribute expected `)` after parameter");
2829 if (!parser_next(parser)) {
2830 parseerror(parser, "parse error in attribute");
2835 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2836 parseerror(parser, "`alias` attribute expected `]]`");
2840 else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
2841 flags |= AST_FLAG_DEPRECATED;
2844 if (!parser_next(parser)) {
2845 parseerror(parser, "parse error in attribute");
2849 if (parser->tok == '(') {
2850 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
2851 parseerror(parser, "`deprecated` attribute missing parameter");
2855 *message = util_strdup(parser_tokval(parser));
2857 if (!parser_next(parser)) {
2858 parseerror(parser, "parse error in attribute");
2862 if(parser->tok != ')') {
2863 parseerror(parser, "`deprecated` attribute expected `)` after parameter");
2867 if (!parser_next(parser)) {
2868 parseerror(parser, "parse error in attribute");
2873 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2874 parseerror(parser, "`deprecated` attribute expected `]]`");
2877 if (*message) mem_d(*message);
2885 /* Skip tokens until we hit a ]] */
2886 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
2887 while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2888 if (!parser_next(parser)) {
2889 parseerror(parser, "error inside attribute");
2896 else if (with_local && !strcmp(parser_tokval(parser), "static"))
2898 else if (!strcmp(parser_tokval(parser), "const"))
2900 else if (!strcmp(parser_tokval(parser), "var"))
2902 else if (with_local && !strcmp(parser_tokval(parser), "local"))
2904 else if (!strcmp(parser_tokval(parser), "noref"))
2906 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
2911 if (!parser_next(parser))
2921 *is_static = had_static;
2925 parseerror(parser, "parse error after variable qualifier");
2930 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
2931 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2936 /* skip the 'while' and get the body */
2937 if (!parser_next(parser)) {
2938 if (OPTS_FLAG(LOOP_LABELS))
2939 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
2941 parseerror(parser, "expected 'switch' operand in parenthesis");
2945 if (parser->tok == ':') {
2946 if (!OPTS_FLAG(LOOP_LABELS))
2947 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2948 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2949 parseerror(parser, "expected loop label");
2952 label = util_strdup(parser_tokval(parser));
2953 if (!parser_next(parser)) {
2955 parseerror(parser, "expected 'switch' operand in parenthesis");
2960 if (parser->tok != '(') {
2961 parseerror(parser, "expected 'switch' operand in parenthesis");
2965 vec_push(parser->breaks, label);
2967 rv = parse_switch_go(parser, block, out);
2970 if (vec_last(parser->breaks) != label) {
2971 parseerror(parser, "internal error: label stack corrupted");
2977 vec_pop(parser->breaks);
2982 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
2984 ast_expression *operand;
2987 ast_switch *switchnode;
2988 ast_switch_case swcase;
2991 bool noref, is_static;
2992 uint32_t qflags = 0;
2994 lex_ctx_t ctx = parser_ctx(parser);
2996 (void)block; /* not touching */
2999 /* parse into the expression */
3000 if (!parser_next(parser)) {
3001 parseerror(parser, "expected switch operand");
3004 /* parse the operand */
3005 operand = parse_expression_leave(parser, false, false, false);
3009 switchnode = ast_switch_new(ctx, operand);
3012 if (parser->tok != ')') {
3013 ast_delete(switchnode);
3014 parseerror(parser, "expected closing paren after 'switch' operand");
3018 /* parse over the opening paren */
3019 if (!parser_next(parser) || parser->tok != '{') {
3020 ast_delete(switchnode);
3021 parseerror(parser, "expected list of cases");
3025 if (!parser_next(parser)) {
3026 ast_delete(switchnode);
3027 parseerror(parser, "expected 'case' or 'default'");
3031 /* new block; allow some variables to be declared here */
3032 parser_enterblock(parser);
3035 if (parser->tok == TOKEN_IDENT)
3036 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3037 if (typevar || parser->tok == TOKEN_TYPENAME) {
3038 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, NULL)) {
3039 ast_delete(switchnode);
3044 if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, NULL))
3046 if (cvq == CV_WRONG) {
3047 ast_delete(switchnode);
3050 if (!parse_variable(parser, block, false, cvq, NULL, noref, is_static, qflags, NULL)) {
3051 ast_delete(switchnode);
3060 while (parser->tok != '}') {
3061 ast_block *caseblock;
3063 if (!strcmp(parser_tokval(parser), "case")) {
3064 if (!parser_next(parser)) {
3065 ast_delete(switchnode);
3066 parseerror(parser, "expected expression for case");
3069 swcase.value = parse_expression_leave(parser, false, false, false);
3070 if (!swcase.value) {
3071 ast_delete(switchnode);
3072 parseerror(parser, "expected expression for case");
3075 if (!OPTS_FLAG(RELAXED_SWITCH)) {
3076 if (!ast_istype(swcase.value, ast_value)) { /* || ((ast_value*)swcase.value)->cvq != CV_CONST) { */
3077 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
3083 else if (!strcmp(parser_tokval(parser), "default")) {
3084 swcase.value = NULL;
3085 if (!parser_next(parser)) {
3086 ast_delete(switchnode);
3087 parseerror(parser, "expected colon");
3092 ast_delete(switchnode);
3093 parseerror(parser, "expected 'case' or 'default'");
3097 /* Now the colon and body */
3098 if (parser->tok != ':') {
3099 if (swcase.value) ast_unref(swcase.value);
3100 ast_delete(switchnode);
3101 parseerror(parser, "expected colon");
3105 if (!parser_next(parser)) {
3106 if (swcase.value) ast_unref(swcase.value);
3107 ast_delete(switchnode);
3108 parseerror(parser, "expected statements or case");
3111 caseblock = ast_block_new(parser_ctx(parser));
3113 if (swcase.value) ast_unref(swcase.value);
3114 ast_delete(switchnode);
3117 swcase.code = (ast_expression*)caseblock;
3118 vec_push(switchnode->cases, swcase);
3120 ast_expression *expr;
3121 if (parser->tok == '}')
3123 if (parser->tok == TOKEN_KEYWORD) {
3124 if (!strcmp(parser_tokval(parser), "case") ||
3125 !strcmp(parser_tokval(parser), "default"))
3130 if (!parse_statement(parser, caseblock, &expr, true)) {
3131 ast_delete(switchnode);
3136 if (!ast_block_add_expr(caseblock, expr)) {
3137 ast_delete(switchnode);
3143 parser_leaveblock(parser);
3146 if (parser->tok != '}') {
3147 ast_delete(switchnode);
3148 parseerror(parser, "expected closing paren of case list");
3151 if (!parser_next(parser)) {
3152 ast_delete(switchnode);
3153 parseerror(parser, "parse error after switch");
3156 *out = (ast_expression*)switchnode;
3160 /* parse computed goto sides */
3161 static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
3162 ast_expression *on_true;
3163 ast_expression *on_false;
3164 ast_expression *cond;
3169 if (ast_istype(*side, ast_ternary)) {
3170 ast_ternary *tern = (ast_ternary*)*side;
3171 on_true = parse_goto_computed(parser, &tern->on_true);
3172 on_false = parse_goto_computed(parser, &tern->on_false);
3174 if (!on_true || !on_false) {
3175 parseerror(parser, "expected label or expression in ternary");
3176 if (on_true) ast_unref(on_true);
3177 if (on_false) ast_unref(on_false);
3185 return (ast_expression*)ast_ifthen_new(parser_ctx(parser), cond, on_true, on_false);
3186 } else if (ast_istype(*side, ast_label)) {
3187 ast_goto *gt = ast_goto_new(parser_ctx(parser), ((ast_label*)*side)->name);
3188 ast_goto_set_label(gt, ((ast_label*)*side));
3190 return (ast_expression*)gt;
3195 static bool parse_goto(parser_t *parser, ast_expression **out)
3197 ast_goto *gt = NULL;
3198 ast_expression *lbl;
3200 if (!parser_next(parser))
3203 if (parser->tok != TOKEN_IDENT) {
3204 ast_expression *expression;
3206 /* could be an expression i.e computed goto :-) */
3207 if (parser->tok != '(') {
3208 parseerror(parser, "expected label name after `goto`");
3212 /* failed to parse expression for goto */
3213 if (!(expression = parse_expression(parser, false, true)) ||
3214 !(*out = parse_goto_computed(parser, &expression))) {
3215 parseerror(parser, "invalid goto expression");
3217 ast_unref(expression);
3224 /* not computed goto */
3225 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
3226 lbl = parser_find_label(parser, gt->name);
3228 if (!ast_istype(lbl, ast_label)) {
3229 parseerror(parser, "internal error: label is not an ast_label");
3233 ast_goto_set_label(gt, (ast_label*)lbl);
3236 vec_push(parser->gotos, gt);
3238 if (!parser_next(parser) || parser->tok != ';') {
3239 parseerror(parser, "semicolon expected after goto label");
3242 if (!parser_next(parser)) {
3243 parseerror(parser, "parse error after goto");
3247 *out = (ast_expression*)gt;
3251 static bool parse_skipwhite(parser_t *parser)
3254 if (!parser_next(parser))
3256 } while (parser->tok == TOKEN_WHITE && parser->tok < TOKEN_ERROR);
3257 return parser->tok < TOKEN_ERROR;
3260 static bool parse_eol(parser_t *parser)
3262 if (!parse_skipwhite(parser))
3264 return parser->tok == TOKEN_EOL;
3267 static bool parse_pragma_do(parser_t *parser)
3269 if (!parser_next(parser) ||
3270 parser->tok != TOKEN_IDENT ||
3271 strcmp(parser_tokval(parser), "pragma"))
3273 parseerror(parser, "expected `pragma` keyword after `#`, got `%s`", parser_tokval(parser));
3276 if (!parse_skipwhite(parser) || parser->tok != TOKEN_IDENT) {
3277 parseerror(parser, "expected pragma, got `%s`", parser_tokval(parser));
3281 if (!strcmp(parser_tokval(parser), "noref")) {
3282 if (!parse_skipwhite(parser) || parser->tok != TOKEN_INTCONST) {
3283 parseerror(parser, "`noref` pragma requires an argument: 0 or 1");
3286 parser->noref = !!parser_token(parser)->constval.i;
3287 if (!parse_eol(parser)) {
3288 parseerror(parser, "parse error after `noref` pragma");
3294 (void)!parsewarning(parser, WARN_UNKNOWN_PRAGMAS, "ignoring #pragma %s", parser_tokval(parser));
3297 while (!parse_eol(parser)) {
3298 parser_next(parser);
3307 static bool parse_pragma(parser_t *parser)
3310 parser->lex->flags.preprocessing = true;
3311 parser->lex->flags.mergelines = true;
3312 rv = parse_pragma_do(parser);
3313 if (parser->tok != TOKEN_EOL) {
3314 parseerror(parser, "junk after pragma");
3317 parser->lex->flags.preprocessing = false;
3318 parser->lex->flags.mergelines = false;
3319 if (!parser_next(parser)) {
3320 parseerror(parser, "parse error after pragma");
3326 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
3328 bool noref, is_static;
3330 uint32_t qflags = 0;
3331 ast_value *typevar = NULL;
3332 char *vstring = NULL;
3336 if (parser->tok == TOKEN_IDENT)
3337 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3339 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3341 /* local variable */
3343 parseerror(parser, "cannot declare a variable from here");
3346 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3347 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
3350 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, NULL))
3354 else if (parse_qualifiers(parser, !!block, &cvq, &noref, &is_static, &qflags, &vstring))
3356 if (cvq == CV_WRONG)
3358 return parse_variable(parser, block, true, cvq, NULL, noref, is_static, qflags, vstring);
3360 else if (parser->tok == TOKEN_KEYWORD)
3362 if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
3367 if (!parser_next(parser)) {
3368 parseerror(parser, "parse error after __builtin_debug_printtype");
3372 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
3374 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
3375 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
3376 if (!parser_next(parser)) {
3377 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
3383 if (!parse_statement(parser, block, out, allow_cases))
3386 con_out("__builtin_debug_printtype: got no output node\n");
3389 ast_type_to_string(*out, ty, sizeof(ty));
3390 con_out("__builtin_debug_printtype: `%s`\n", ty);
3395 else if (!strcmp(parser_tokval(parser), "return"))
3397 return parse_return(parser, block, out);
3399 else if (!strcmp(parser_tokval(parser), "if"))
3401 return parse_if(parser, block, out);
3403 else if (!strcmp(parser_tokval(parser), "while"))
3405 return parse_while(parser, block, out);
3407 else if (!strcmp(parser_tokval(parser), "do"))
3409 return parse_dowhile(parser, block, out);
3411 else if (!strcmp(parser_tokval(parser), "for"))
3413 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3414 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
3417 return parse_for(parser, block, out);
3419 else if (!strcmp(parser_tokval(parser), "break"))
3421 return parse_break_continue(parser, block, out, false);
3423 else if (!strcmp(parser_tokval(parser), "continue"))
3425 return parse_break_continue(parser, block, out, true);
3427 else if (!strcmp(parser_tokval(parser), "switch"))
3429 return parse_switch(parser, block, out);
3431 else if (!strcmp(parser_tokval(parser), "case") ||
3432 !strcmp(parser_tokval(parser), "default"))
3435 parseerror(parser, "unexpected 'case' label");
3440 else if (!strcmp(parser_tokval(parser), "goto"))
3442 return parse_goto(parser, out);
3444 else if (!strcmp(parser_tokval(parser), "typedef"))
3446 if (!parser_next(parser)) {
3447 parseerror(parser, "expected type definition after 'typedef'");
3450 return parse_typedef(parser);
3452 parseerror(parser, "Unexpected keyword: `%s'", parser_tokval(parser));
3455 else if (parser->tok == '{')
3458 inner = parse_block(parser);
3461 *out = (ast_expression*)inner;
3464 else if (parser->tok == ':')
3468 if (!parser_next(parser)) {
3469 parseerror(parser, "expected label name");
3472 if (parser->tok != TOKEN_IDENT) {
3473 parseerror(parser, "label must be an identifier");
3476 label = (ast_label*)parser_find_label(parser, parser_tokval(parser));
3478 if (!label->undefined) {
3479 parseerror(parser, "label `%s` already defined", label->name);
3482 label->undefined = false;
3485 label = ast_label_new(parser_ctx(parser), parser_tokval(parser), false);
3486 vec_push(parser->labels, label);
3488 *out = (ast_expression*)label;
3489 if (!parser_next(parser)) {
3490 parseerror(parser, "parse error after label");
3493 for (i = 0; i < vec_size(parser->gotos); ++i) {
3494 if (!strcmp(parser->gotos[i]->name, label->name)) {
3495 ast_goto_set_label(parser->gotos[i], label);
3496 vec_remove(parser->gotos, i, 1);
3502 else if (parser->tok == ';')
3504 if (!parser_next(parser)) {
3505 parseerror(parser, "parse error after empty statement");
3512 lex_ctx_t ctx = parser_ctx(parser);
3513 ast_expression *exp = parse_expression(parser, false, false);
3517 if (!ast_side_effects(exp)) {
3518 if (compile_warning(ctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
3525 static bool parse_enum(parser_t *parser)
3528 bool reverse = false;
3530 ast_value **values = NULL;
3531 ast_value *var = NULL;
3534 ast_expression *old;
3536 if (!parser_next(parser) || (parser->tok != '{' && parser->tok != ':')) {
3537 parseerror(parser, "expected `{` or `:` after `enum` keyword");
3541 /* enumeration attributes (can add more later) */
3542 if (parser->tok == ':') {
3543 if (!parser_next(parser) || parser->tok != TOKEN_IDENT){
3544 parseerror(parser, "expected `flag` or `reverse` for enumeration attribute");
3549 if (!strcmp(parser_tokval(parser), "flag")) {
3553 else if (!strcmp(parser_tokval(parser), "reverse")) {
3557 parseerror(parser, "invalid attribute `%s` for enumeration", parser_tokval(parser));
3561 if (!parser_next(parser) || parser->tok != '{') {
3562 parseerror(parser, "expected `{` after enum attribute ");
3568 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
3569 if (parser->tok == '}') {
3570 /* allow an empty enum */
3573 parseerror(parser, "expected identifier or `}`");
3577 old = parser_find_field(parser, parser_tokval(parser));
3579 old = parser_find_global(parser, parser_tokval(parser));
3581 parseerror(parser, "value `%s` has already been declared here: %s:%i",
3582 parser_tokval(parser), ast_ctx(old).file, ast_ctx(old).line);
3586 var = ast_value_new(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
3587 vec_push(values, var);
3588 var->cvq = CV_CONST;
3589 var->hasvalue = true;
3591 /* for flagged enumerations increment in POTs of TWO */
3592 var->constval.vfloat = (flag) ? (num *= 2) : (num ++);
3593 parser_addglobal(parser, var->name, (ast_expression*)var);
3595 if (!parser_next(parser)) {
3596 parseerror(parser, "expected `=`, `}` or comma after identifier");
3600 if (parser->tok == ',')
3602 if (parser->tok == '}')
3604 if (parser->tok != '=') {
3605 parseerror(parser, "expected `=`, `}` or comma after identifier");
3609 if (!parser_next(parser)) {
3610 parseerror(parser, "expected expression after `=`");
3614 /* We got a value! */
3615 old = parse_expression_leave(parser, true, false, false);
3616 asvalue = (ast_value*)old;
3617 if (!ast_istype(old, ast_value) || asvalue->cvq != CV_CONST || !asvalue->hasvalue) {
3618 compile_error(ast_ctx(var), "constant value or expression expected");
3621 num = (var->constval.vfloat = asvalue->constval.vfloat) + 1;
3623 if (parser->tok == '}')
3625 if (parser->tok != ',') {
3626 parseerror(parser, "expected `}` or comma after expression");
3631 /* patch them all (for reversed attribute) */
3634 for (i = 0; i < vec_size(values); i++)
3635 values[i]->constval.vfloat = vec_size(values) - i - 1;
3638 if (parser->tok != '}') {
3639 parseerror(parser, "internal error: breaking without `}`");
3643 if (!parser_next(parser) || parser->tok != ';') {
3644 parseerror(parser, "expected semicolon after enumeration");
3648 if (!parser_next(parser)) {
3649 parseerror(parser, "parse error after enumeration");
3661 static bool parse_block_into(parser_t *parser, ast_block *block)
3665 parser_enterblock(parser);
3667 if (!parser_next(parser)) { /* skip the '{' */
3668 parseerror(parser, "expected function body");
3672 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3674 ast_expression *expr = NULL;
3675 if (parser->tok == '}')
3678 if (!parse_statement(parser, block, &expr, false)) {
3679 /* parseerror(parser, "parse error"); */
3685 if (!ast_block_add_expr(block, expr)) {
3692 if (parser->tok != '}') {
3695 (void)parser_next(parser);
3699 if (!parser_leaveblock(parser))
3701 return retval && !!block;
3704 static ast_block* parse_block(parser_t *parser)
3707 block = ast_block_new(parser_ctx(parser));
3710 if (!parse_block_into(parser, block)) {
3711 ast_block_delete(block);
3717 static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
3719 if (parser->tok == '{') {
3720 *out = (ast_expression*)parse_block(parser);
3723 return parse_statement(parser, NULL, out, false);
3726 static bool create_vector_members(ast_value *var, ast_member **me)
3729 size_t len = strlen(var->name);
3731 for (i = 0; i < 3; ++i) {
3732 char *name = (char*)mem_a(len+3);
3733 memcpy(name, var->name, len);
3735 name[len+1] = 'x'+i;
3737 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
3746 do { ast_member_delete(me[--i]); } while(i);
3750 static bool parse_function_body(parser_t *parser, ast_value *var)
3752 ast_block *block = NULL;
3757 ast_expression *framenum = NULL;
3758 ast_expression *nextthink = NULL;
3759 /* None of the following have to be deleted */
3760 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
3761 ast_expression *gbl_time = NULL, *gbl_self = NULL;
3762 bool has_frame_think;
3766 has_frame_think = false;
3767 old = parser->function;
3769 if (var->expression.flags & AST_FLAG_ALIAS) {
3770 parseerror(parser, "function aliases cannot have bodies");
3774 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
3775 parseerror(parser, "gotos/labels leaking");
3779 if (!OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC) {
3780 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
3781 "variadic function with implementation will not be able to access additional parameters (try -fvariadic-args)"))
3787 if (parser->tok == '[') {
3788 /* got a frame definition: [ framenum, nextthink ]
3789 * this translates to:
3790 * self.frame = framenum;
3791 * self.nextthink = time + 0.1;
3792 * self.think = nextthink;
3796 fld_think = parser_find_field(parser, "think");
3797 fld_nextthink = parser_find_field(parser, "nextthink");
3798 fld_frame = parser_find_field(parser, "frame");
3799 if (!fld_think || !fld_nextthink || !fld_frame) {
3800 parseerror(parser, "cannot use [frame,think] notation without the required fields");
3801 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
3804 gbl_time = parser_find_global(parser, "time");
3805 gbl_self = parser_find_global(parser, "self");
3806 if (!gbl_time || !gbl_self) {
3807 parseerror(parser, "cannot use [frame,think] notation without the required globals");
3808 parseerror(parser, "please declare the following globals: `time`, `self`");
3812 if (!parser_next(parser))
3815 framenum = parse_expression_leave(parser, true, false, false);
3817 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
3820 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
3821 ast_unref(framenum);
3822 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
3826 if (parser->tok != ',') {
3827 ast_unref(framenum);
3828 parseerror(parser, "expected comma after frame number in [frame,think] notation");
3829 parseerror(parser, "Got a %i\n", parser->tok);
3833 if (!parser_next(parser)) {
3834 ast_unref(framenum);
3838 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
3840 /* qc allows the use of not-yet-declared functions here
3841 * - this automatically creates a prototype */
3842 ast_value *thinkfunc;
3843 ast_expression *functype = fld_think->next;
3845 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->vtype);
3846 if (!thinkfunc) { /* || !ast_type_adopt(thinkfunc, functype)*/
3847 ast_unref(framenum);
3848 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
3851 ast_type_adopt(thinkfunc, functype);
3853 if (!parser_next(parser)) {
3854 ast_unref(framenum);
3855 ast_delete(thinkfunc);
3859 parser_addglobal(parser, thinkfunc->name, (ast_expression*)thinkfunc);
3861 nextthink = (ast_expression*)thinkfunc;
3864 nextthink = parse_expression_leave(parser, true, false, false);
3866 ast_unref(framenum);
3867 parseerror(parser, "expected a think-function in [frame,think] notation");
3872 if (!ast_istype(nextthink, ast_value)) {
3873 parseerror(parser, "think-function in [frame,think] notation must be a constant");
3877 if (retval && parser->tok != ']') {
3878 parseerror(parser, "expected closing `]` for [frame,think] notation");
3882 if (retval && !parser_next(parser)) {
3886 if (retval && parser->tok != '{') {
3887 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
3892 ast_unref(nextthink);
3893 ast_unref(framenum);
3897 has_frame_think = true;
3900 block = ast_block_new(parser_ctx(parser));
3902 parseerror(parser, "failed to allocate block");
3903 if (has_frame_think) {
3904 ast_unref(nextthink);
3905 ast_unref(framenum);
3910 if (has_frame_think) {
3912 ast_expression *self_frame;
3913 ast_expression *self_nextthink;
3914 ast_expression *self_think;
3915 ast_expression *time_plus_1;
3916 ast_store *store_frame;
3917 ast_store *store_nextthink;
3918 ast_store *store_think;
3920 ctx = parser_ctx(parser);
3921 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
3922 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
3923 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
3925 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
3926 gbl_time, (ast_expression*)fold_constgen_float(parser->fold, 0.1f));
3928 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
3929 if (self_frame) ast_delete(self_frame);
3930 if (self_nextthink) ast_delete(self_nextthink);
3931 if (self_think) ast_delete(self_think);
3932 if (time_plus_1) ast_delete(time_plus_1);
3938 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
3939 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
3940 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
3943 ast_delete(self_frame);
3946 if (!store_nextthink) {
3947 ast_delete(self_nextthink);
3951 ast_delete(self_think);
3955 if (store_frame) ast_delete(store_frame);
3956 if (store_nextthink) ast_delete(store_nextthink);
3957 if (store_think) ast_delete(store_think);
3960 if (!ast_block_add_expr(block, (ast_expression*)store_frame) ||
3961 !ast_block_add_expr(block, (ast_expression*)store_nextthink) ||
3962 !ast_block_add_expr(block, (ast_expression*)store_think))
3969 parseerror(parser, "failed to generate code for [frame,think]");
3970 ast_unref(nextthink);
3971 ast_unref(framenum);
3977 if (var->hasvalue) {
3978 parseerror(parser, "function `%s` declared with multiple bodies", var->name);
3979 ast_block_delete(block);
3983 func = ast_function_new(ast_ctx(var), var->name, var);
3985 parseerror(parser, "failed to allocate function for `%s`", var->name);
3986 ast_block_delete(block);
3989 vec_push(parser->functions, func);
3991 parser_enterblock(parser);
3993 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
3995 ast_value *param = var->expression.params[parami];
3998 if (param->expression.vtype != TYPE_VECTOR &&
3999 (param->expression.vtype != TYPE_FIELD ||
4000 param->expression.next->vtype != TYPE_VECTOR))
4005 if (!create_vector_members(param, me)) {
4006 ast_block_delete(block);
4010 for (e = 0; e < 3; ++e) {
4011 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
4012 ast_block_collect(block, (ast_expression*)me[e]);
4016 if (var->argcounter) {
4017 ast_value *argc = ast_value_new(ast_ctx(var), var->argcounter, TYPE_FLOAT);
4018 parser_addlocal(parser, argc->name, (ast_expression*)argc);
4022 if (OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC) {
4024 ast_value *varargs = ast_value_new(ast_ctx(var), "reserved:va_args", TYPE_ARRAY);
4025 varargs->expression.flags |= AST_FLAG_IS_VARARG;
4026 varargs->expression.next = (ast_expression*)ast_value_new(ast_ctx(var), NULL, TYPE_VECTOR);
4027 varargs->expression.count = 0;
4028 platform_snprintf(name, sizeof(name), "%s##va##SET", var->name);
4029 if (!parser_create_array_setter_proto(parser, varargs, name)) {
4030 ast_delete(varargs);
4031 ast_block_delete(block);
4034 platform_snprintf(name, sizeof(name), "%s##va##GET", var->name);
4035 if (!parser_create_array_getter_proto(parser, varargs, varargs->expression.next, name)) {
4036 ast_delete(varargs);
4037 ast_block_delete(block);
4040 func->varargs = varargs;
4041 func->fixedparams = (ast_value*)fold_constgen_float(parser->fold, vec_size(var->expression.params));
4044 parser->function = func;
4045 if (!parse_block_into(parser, block)) {
4046 ast_block_delete(block);
4050 vec_push(func->blocks, block);
4053 parser->function = old;
4054 if (!parser_leaveblock(parser))
4056 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
4057 parseerror(parser, "internal error: local scopes left");
4061 if (parser->tok == ';')
4062 return parser_next(parser);
4063 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4064 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
4068 (void)!parser_leaveblock(parser);
4069 vec_pop(parser->functions);
4070 ast_function_delete(func);
4071 var->constval.vfunc = NULL;
4074 parser->function = old;
4078 static ast_expression *array_accessor_split(
4083 ast_expression *left,
4084 ast_expression *right
4090 lex_ctx_t ctx = ast_ctx(array);
4092 if (!left || !right) {
4093 if (left) ast_delete(left);
4094 if (right) ast_delete(right);
4098 cmp = ast_binary_new(ctx, INSTR_LT,
4099 (ast_expression*)index,
4100 (ast_expression*)fold_constgen_float(parser->fold, middle));
4104 parseerror(parser, "internal error: failed to create comparison for array setter");
4108 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
4110 ast_delete(cmp); /* will delete left and right */
4111 parseerror(parser, "internal error: failed to create conditional jump for array setter");
4115 return (ast_expression*)ifthen;
4118 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
4120 lex_ctx_t ctx = ast_ctx(array);
4122 if (from+1 == afterend) {
4123 /* set this value */
4126 ast_array_index *subscript;
4128 int assignop = type_store_instr[value->expression.vtype];
4130 if (value->expression.vtype == TYPE_FIELD && value->expression.next->vtype == TYPE_VECTOR)
4131 assignop = INSTR_STORE_V;
4133 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)fold_constgen_float(parser->fold, from));
4137 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
4139 ast_delete(subscript);
4143 block = ast_block_new(ctx);
4149 if (!ast_block_add_expr(block, (ast_expression*)st)) {
4154 ret = ast_return_new(ctx, NULL);
4160 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
4165 return (ast_expression*)block;
4167 ast_expression *left, *right;
4168 size_t diff = afterend - from;
4169 size_t middle = from + diff/2;
4170 left = array_setter_node(parser, array, index, value, from, middle);
4171 right = array_setter_node(parser, array, index, value, middle, afterend);
4172 return array_accessor_split(parser, array, index, middle, left, right);
4176 static ast_expression *array_field_setter_node(
4185 lex_ctx_t ctx = ast_ctx(array);
4187 if (from+1 == afterend) {
4188 /* set this value */
4191 ast_entfield *entfield;
4192 ast_array_index *subscript;
4194 int assignop = type_storep_instr[value->expression.vtype];
4196 if (value->expression.vtype == TYPE_FIELD && value->expression.next->vtype == TYPE_VECTOR)
4197 assignop = INSTR_STOREP_V;
4199 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)fold_constgen_float(parser->fold, from));
4203 subscript->expression.next = ast_type_copy(ast_ctx(subscript), (ast_expression*)subscript);
4204 subscript->expression.vtype = TYPE_FIELD;
4206 entfield = ast_entfield_new_force(ctx,
4207 (ast_expression*)entity,
4208 (ast_expression*)subscript,
4209 (ast_expression*)subscript);
4211 ast_delete(subscript);
4215 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
4217 ast_delete(entfield);
4221 block = ast_block_new(ctx);
4227 if (!ast_block_add_expr(block, (ast_expression*)st)) {
4232 ret = ast_return_new(ctx, NULL);
4238 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
4243 return (ast_expression*)block;
4245 ast_expression *left, *right;
4246 size_t diff = afterend - from;
4247 size_t middle = from + diff/2;
4248 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
4249 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
4250 return array_accessor_split(parser, array, index, middle, left, right);
4254 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
4256 lex_ctx_t ctx = ast_ctx(array);
4258 if (from+1 == afterend) {
4260 ast_array_index *subscript;
4262 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)fold_constgen_float(parser->fold, from));
4266 ret = ast_return_new(ctx, (ast_expression*)subscript);
4268 ast_delete(subscript);
4272 return (ast_expression*)ret;
4274 ast_expression *left, *right;
4275 size_t diff = afterend - from;
4276 size_t middle = from + diff/2;
4277 left = array_getter_node(parser, array, index, from, middle);
4278 right = array_getter_node(parser, array, index, middle, afterend);
4279 return array_accessor_split(parser, array, index, middle, left, right);
4283 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
4285 ast_function *func = NULL;
4286 ast_value *fval = NULL;
4287 ast_block *body = NULL;
4289 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
4291 parseerror(parser, "failed to create accessor function value");
4295 func = ast_function_new(ast_ctx(array), funcname, fval);
4298 parseerror(parser, "failed to create accessor function node");
4302 body = ast_block_new(ast_ctx(array));
4304 parseerror(parser, "failed to create block for array accessor");
4310 vec_push(func->blocks, body);
4313 vec_push(parser->accessors, fval);
4318 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname)
4320 ast_value *index = NULL;
4321 ast_value *value = NULL;
4325 if (!ast_istype(array->expression.next, ast_value)) {
4326 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4330 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4332 func = fval->constval.vfunc;
4333 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
4335 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4336 value = ast_value_copy((ast_value*)array->expression.next);
4338 if (!index || !value) {
4339 parseerror(parser, "failed to create locals for array accessor");
4342 (void)!ast_value_set_name(value, "value"); /* not important */
4343 vec_push(fval->expression.params, index);
4344 vec_push(fval->expression.params, value);
4346 array->setter = fval;
4349 if (index) ast_delete(index);
4350 if (value) ast_delete(value);
4356 static bool parser_create_array_setter_impl(parser_t *parser, ast_value *array)
4358 ast_expression *root = NULL;
4359 root = array_setter_node(parser, array,
4360 array->setter->expression.params[0],
4361 array->setter->expression.params[1],
4362 0, array->expression.count);
4364 parseerror(parser, "failed to build accessor search tree");
4367 if (!ast_block_add_expr(array->setter->constval.vfunc->blocks[0], root)) {
4374 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
4376 if (!parser_create_array_setter_proto(parser, array, funcname))
4378 return parser_create_array_setter_impl(parser, array);
4381 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
4383 ast_expression *root = NULL;
4384 ast_value *entity = NULL;
4385 ast_value *index = NULL;
4386 ast_value *value = NULL;
4390 if (!ast_istype(array->expression.next, ast_value)) {
4391 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4395 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4397 func = fval->constval.vfunc;
4398 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
4400 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
4401 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4402 value = ast_value_copy((ast_value*)array->expression.next);
4403 if (!entity || !index || !value) {
4404 parseerror(parser, "failed to create locals for array accessor");
4407 (void)!ast_value_set_name(value, "value"); /* not important */
4408 vec_push(fval->expression.params, entity);
4409 vec_push(fval->expression.params, index);
4410 vec_push(fval->expression.params, value);
4412 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
4414 parseerror(parser, "failed to build accessor search tree");
4418 array->setter = fval;
4419 return ast_block_add_expr(func->blocks[0], root);
4421 if (entity) ast_delete(entity);
4422 if (index) ast_delete(index);
4423 if (value) ast_delete(value);
4424 if (root) ast_delete(root);
4430 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4432 ast_value *index = NULL;
4436 /* NOTE: checking array->expression.next rather than elemtype since
4437 * for fields elemtype is a temporary fieldtype.
4439 if (!ast_istype(array->expression.next, ast_value)) {
4440 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4444 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4446 func = fval->constval.vfunc;
4447 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
4449 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4452 parseerror(parser, "failed to create locals for array accessor");
4455 vec_push(fval->expression.params, index);
4457 array->getter = fval;
4460 if (index) ast_delete(index);
4466 static bool parser_create_array_getter_impl(parser_t *parser, ast_value *array)
4468 ast_expression *root = NULL;
4470 root = array_getter_node(parser, array, array->getter->expression.params[0], 0, array->expression.count);
4472 parseerror(parser, "failed to build accessor search tree");
4475 if (!ast_block_add_expr(array->getter->constval.vfunc->blocks[0], root)) {
4482 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4484 if (!parser_create_array_getter_proto(parser, array, elemtype, funcname))
4486 return parser_create_array_getter_impl(parser, array);
4489 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
4490 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
4498 bool variadic = false;
4499 ast_value *varparam = NULL;
4500 char *argcounter = NULL;
4502 ctx = parser_ctx(parser);
4504 /* for the sake of less code we parse-in in this function */
4505 if (!parser_next(parser)) {
4507 parseerror(parser, "expected parameter list");
4513 /* parse variables until we hit a closing paren */
4514 while (parser->tok != ')') {
4516 /* there must be commas between them */
4517 if (parser->tok != ',') {
4518 parseerror(parser, "expected comma or end of parameter list");
4521 if (!parser_next(parser)) {
4522 parseerror(parser, "expected parameter");
4528 if (parser->tok == TOKEN_DOTS) {
4529 /* '...' indicates a varargs function */
4531 if (!parser_next(parser) || (parser->tok != ')' && parser->tok != TOKEN_IDENT)) {
4532 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4535 if (parser->tok == TOKEN_IDENT) {
4536 argcounter = util_strdup(parser_tokval(parser));
4537 if (!parser_next(parser) || parser->tok != ')') {
4538 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4545 /* for anything else just parse a typename */
4546 param = parse_typename(parser, NULL, NULL);
4549 vec_push(params, param);
4550 if (param->expression.vtype >= TYPE_VARIANT) {
4551 char tname[1024]; /* typename is reserved in C++ */
4552 ast_type_to_string((ast_expression*)param, tname, sizeof(tname));
4553 parseerror(parser, "type not supported as part of a parameter list: %s", tname);
4556 /* type-restricted varargs */
4557 if (parser->tok == TOKEN_DOTS) {
4559 varparam = vec_last(params);
4561 if (!parser_next(parser) || (parser->tok != ')' && parser->tok != TOKEN_IDENT)) {
4562 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4565 if (parser->tok == TOKEN_IDENT) {
4566 argcounter = util_strdup(parser_tokval(parser));
4567 if (!parser_next(parser) || parser->tok != ')') {
4568 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4576 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
4580 if (vec_size(params) > 8 && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4581 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
4584 if (!parser_next(parser)) {
4585 parseerror(parser, "parse error after typename");
4589 /* now turn 'var' into a function type */
4590 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
4591 fval->expression.next = (ast_expression*)var;
4593 fval->expression.flags |= AST_FLAG_VARIADIC;
4596 var->expression.params = params;
4597 var->expression.varparam = (ast_expression*)varparam;
4598 var->argcounter = argcounter;
4607 ast_delete(varparam);
4609 for (i = 0; i < vec_size(params); ++i)
4610 ast_delete(params[i]);
4615 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
4617 ast_expression *cexp;
4618 ast_value *cval, *tmp;
4621 ctx = parser_ctx(parser);
4623 if (!parser_next(parser)) {
4625 parseerror(parser, "expected array-size");
4629 if (parser->tok != ']') {
4630 cexp = parse_expression_leave(parser, true, false, false);
4632 if (!cexp || !ast_istype(cexp, ast_value)) {
4636 parseerror(parser, "expected array-size as constant positive integer");
4639 cval = (ast_value*)cexp;
4646 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
4647 tmp->expression.next = (ast_expression*)var;
4651 if (cval->expression.vtype == TYPE_INTEGER)
4652 tmp->expression.count = cval->constval.vint;
4653 else if (cval->expression.vtype == TYPE_FLOAT)
4654 tmp->expression.count = cval->constval.vfloat;
4658 parseerror(parser, "array-size must be a positive integer constant");
4664 var->expression.count = -1;
4665 var->expression.flags |= AST_FLAG_ARRAY_INIT;
4668 if (parser->tok != ']') {
4670 parseerror(parser, "expected ']' after array-size");
4673 if (!parser_next(parser)) {
4675 parseerror(parser, "error after parsing array size");
4681 /* Parse a complete typename.
4682 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
4683 * but when parsing variables separated by comma
4684 * 'storebase' should point to where the base-type should be kept.
4685 * The base type makes up every bit of type information which comes *before* the
4688 * NOTE: The value must either be named, have a NULL name, or a name starting
4689 * with '<'. In the first case, this will be the actual variable or type
4690 * name, in the other cases it is assumed that the name will appear
4691 * later, and an error is generated otherwise.
4693 * The following will be parsed in its entirety:
4695 * The 'basetype' in this case is 'void()'
4696 * and if there's a comma after it, say:
4698 * then the type-information 'void()' can be stored in 'storebase'
4700 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
4702 ast_value *var, *tmp;
4705 const char *name = NULL;
4706 bool isfield = false;
4707 bool wasarray = false;
4708 size_t morefields = 0;
4710 ctx = parser_ctx(parser);
4712 /* types may start with a dot */
4713 if (parser->tok == '.') {
4715 /* if we parsed a dot we need a typename now */
4716 if (!parser_next(parser)) {
4717 parseerror(parser, "expected typename for field definition");
4721 /* Further dots are handled seperately because they won't be part of the
4724 while (parser->tok == '.') {
4726 if (!parser_next(parser)) {
4727 parseerror(parser, "expected typename for field definition");
4732 if (parser->tok == TOKEN_IDENT)
4733 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
4734 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
4735 parseerror(parser, "expected typename");
4739 /* generate the basic type value */
4740 if (cached_typedef) {
4741 var = ast_value_copy(cached_typedef);
4742 ast_value_set_name(var, "<type(from_def)>");
4744 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
4746 for (; morefields; --morefields) {
4747 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
4748 tmp->expression.next = (ast_expression*)var;
4752 /* do not yet turn into a field - remember:
4753 * .void() foo; is a field too
4754 * .void()() foo; is a function
4758 if (!parser_next(parser)) {
4760 parseerror(parser, "parse error after typename");
4764 /* an opening paren now starts the parameter-list of a function
4765 * this is where original-QC has parameter lists.
4766 * We allow a single parameter list here.
4767 * Much like fteqcc we don't allow `float()() x`
4769 if (parser->tok == '(') {
4770 var = parse_parameter_list(parser, var);
4775 /* store the base if requested */
4777 *storebase = ast_value_copy(var);
4779 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4780 tmp->expression.next = (ast_expression*)*storebase;
4785 /* there may be a name now */
4786 if (parser->tok == TOKEN_IDENT) {
4787 name = util_strdup(parser_tokval(parser));
4789 if (!parser_next(parser)) {
4792 parseerror(parser, "error after variable or field declaration");
4797 /* now this may be an array */
4798 if (parser->tok == '[') {
4800 var = parse_arraysize(parser, var);
4802 if (name) mem_d(name);
4807 /* This is the point where we can turn it into a field */
4809 /* turn it into a field if desired */
4810 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4811 tmp->expression.next = (ast_expression*)var;
4815 /* now there may be function parens again */
4816 if (parser->tok == '(' && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4817 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4818 if (parser->tok == '(' && wasarray)
4819 parseerror(parser, "arrays as part of a return type is not supported");
4820 while (parser->tok == '(') {
4821 var = parse_parameter_list(parser, var);
4823 if (name) mem_d(name);
4828 /* finally name it */
4830 if (!ast_value_set_name(var, name)) {
4833 parseerror(parser, "internal error: failed to set name");
4836 /* free the name, ast_value_set_name duplicates */
4843 static bool parse_typedef(parser_t *parser)
4845 ast_value *typevar, *oldtype;
4846 ast_expression *old;
4848 typevar = parse_typename(parser, NULL, NULL);
4853 /* while parsing types, the ast_value's get named '<something>' */
4854 if (!typevar->name || typevar->name[0] == '<') {
4855 parseerror(parser, "missing name in typedef");
4856 ast_delete(typevar);
4860 if ( (old = parser_find_var(parser, typevar->name)) ) {
4861 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
4862 " -> `%s` has been declared here: %s:%i",
4863 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
4864 ast_delete(typevar);
4868 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
4869 parseerror(parser, "type `%s` has already been declared here: %s:%i",
4870 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
4871 ast_delete(typevar);
4875 vec_push(parser->_typedefs, typevar);
4876 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
4878 if (parser->tok != ';') {
4879 parseerror(parser, "expected semicolon after typedef");
4882 if (!parser_next(parser)) {
4883 parseerror(parser, "parse error after typedef");
4890 static const char *cvq_to_str(int cvq) {
4892 case CV_NONE: return "none";
4893 case CV_VAR: return "`var`";
4894 case CV_CONST: return "`const`";
4895 default: return "<INVALID>";
4899 static bool parser_check_qualifiers(parser_t *parser, const ast_value *var, const ast_value *proto)
4902 if (proto->cvq != var->cvq) {
4903 if (!(proto->cvq == CV_CONST && var->cvq == CV_NONE &&
4904 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4905 parser->tok == '='))
4907 return !parsewarning(parser, WARN_DIFFERENT_QUALIFIERS,
4908 "`%s` declared with different qualifiers: %s\n"
4909 " -> previous declaration here: %s:%i uses %s",
4910 var->name, cvq_to_str(var->cvq),
4911 ast_ctx(proto).file, ast_ctx(proto).line,
4912 cvq_to_str(proto->cvq));
4915 av = (var ->expression.flags & AST_FLAG_NORETURN);
4916 ao = (proto->expression.flags & AST_FLAG_NORETURN);
4918 return !parsewarning(parser, WARN_DIFFERENT_ATTRIBUTES,
4919 "`%s` declared with different attributes%s\n"
4920 " -> previous declaration here: %s:%i",
4921 var->name, (av ? ": noreturn" : ""),
4922 ast_ctx(proto).file, ast_ctx(proto).line,
4923 (ao ? ": noreturn" : ""));
4928 static bool create_array_accessors(parser_t *parser, ast_value *var)
4931 platform_snprintf(name, sizeof(name), "%s##SET", var->name);
4932 if (!parser_create_array_setter(parser, var, name))
4934 platform_snprintf(name, sizeof(name), "%s##GET", var->name);
4935 if (!parser_create_array_getter(parser, var, var->expression.next, name))
4940 static bool parse_array(parser_t *parser, ast_value *array)
4943 if (array->initlist) {
4944 parseerror(parser, "array already initialized elsewhere");
4947 if (!parser_next(parser)) {
4948 parseerror(parser, "parse error in array initializer");
4952 while (parser->tok != '}') {
4953 ast_value *v = (ast_value*)parse_expression_leave(parser, true, false, false);
4956 if (!ast_istype(v, ast_value) || !v->hasvalue || v->cvq != CV_CONST) {
4958 parseerror(parser, "initializing element must be a compile time constant");
4961 vec_push(array->initlist, v->constval);
4962 if (v->expression.vtype == TYPE_STRING) {
4963 array->initlist[i].vstring = util_strdupe(array->initlist[i].vstring);
4967 if (parser->tok == '}')
4969 if (parser->tok != ',' || !parser_next(parser)) {
4970 parseerror(parser, "expected comma or '}' in element list");
4974 if (!parser_next(parser) || parser->tok != ';') {
4975 parseerror(parser, "expected semicolon after initializer, got %s");
4979 if (!parser_next(parser)) {
4980 parseerror(parser, "parse error after initializer");
4985 if (array->expression.flags & AST_FLAG_ARRAY_INIT) {
4986 if (array->expression.count != (size_t)-1) {
4987 parseerror(parser, "array `%s' has already been initialized with %u elements",
4988 array->name, (unsigned)array->expression.count);
4990 array->expression.count = vec_size(array->initlist);
4991 if (!create_array_accessors(parser, array))
4997 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring)
5001 ast_expression *old;
5005 ast_value *basetype = NULL;
5007 bool isparam = false;
5008 bool isvector = false;
5009 bool cleanvar = true;
5010 bool wasarray = false;
5012 ast_member *me[3] = { NULL, NULL, NULL };
5014 if (!localblock && is_static)
5015 parseerror(parser, "`static` qualifier is not supported in global scope");
5017 /* get the first complete variable */
5018 var = parse_typename(parser, &basetype, cached_typedef);
5021 ast_delete(basetype);
5025 /* while parsing types, the ast_value's get named '<something>' */
5026 if (!var->name || var->name[0] == '<') {
5027 parseerror(parser, "declaration does not declare anything");
5029 ast_delete(basetype);
5037 /* Part 0: finish the type */
5038 if (parser->tok == '(') {
5039 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
5040 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
5041 var = parse_parameter_list(parser, var);
5047 /* we only allow 1-dimensional arrays */
5048 if (parser->tok == '[') {
5050 var = parse_arraysize(parser, var);
5056 if (parser->tok == '(' && wasarray) {
5057 parseerror(parser, "arrays as part of a return type is not supported");
5058 /* we'll still parse the type completely for now */
5060 /* for functions returning functions */
5061 while (parser->tok == '(') {
5062 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
5063 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
5064 var = parse_parameter_list(parser, var);
5071 var->cvq = qualifier;
5072 var->expression.flags |= qflags;
5075 * store the vstring back to var for alias and
5076 * deprecation messages.
5078 if (var->expression.flags & AST_FLAG_DEPRECATED ||
5079 var->expression.flags & AST_FLAG_ALIAS)
5080 var->desc = vstring;
5083 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
5084 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
5085 * is then filled with the previous definition and the parameter-names replaced.
5087 if (!strcmp(var->name, "nil")) {
5088 if (OPTS_FLAG(UNTYPED_NIL)) {
5089 if (!localblock || !OPTS_FLAG(PERMISSIVE))
5090 parseerror(parser, "name `nil` not allowed (try -fpermissive)");
5092 (void)!parsewarning(parser, WARN_RESERVED_NAMES, "variable name `nil` is reserved");
5095 /* Deal with end_sys_ vars */
5097 if (!strcmp(var->name, "end_sys_globals")) {
5099 parser->crc_globals = vec_size(parser->globals);
5102 else if (!strcmp(var->name, "end_sys_fields")) {
5104 parser->crc_fields = vec_size(parser->fields);
5107 if (was_end && var->expression.vtype == TYPE_FIELD) {
5108 if (parsewarning(parser, WARN_END_SYS_FIELDS,
5109 "global '%s' hint should not be a field",
5110 parser_tokval(parser)))
5117 if (!nofields && var->expression.vtype == TYPE_FIELD)
5119 /* deal with field declarations */
5120 old = parser_find_field(parser, var->name);
5122 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
5123 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
5132 parseerror(parser, "field `%s` already declared here: %s:%i",
5133 var->name, ast_ctx(old).file, ast_ctx(old).line);
5138 if ((OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC || OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) &&
5139 (old = parser_find_global(parser, var->name)))
5141 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5142 parseerror(parser, "field `%s` already declared here: %s:%i",
5143 var->name, ast_ctx(old).file, ast_ctx(old).line);
5150 /* deal with other globals */
5151 old = parser_find_global(parser, var->name);
5152 if (old && var->expression.vtype == TYPE_FUNCTION && old->vtype == TYPE_FUNCTION)
5154 /* This is a function which had a prototype */
5155 if (!ast_istype(old, ast_value)) {
5156 parseerror(parser, "internal error: prototype is not an ast_value");
5160 proto = (ast_value*)old;
5161 proto->desc = var->desc;
5162 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
5163 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
5165 ast_ctx(proto).file, ast_ctx(proto).line);
5169 /* we need the new parameter-names */
5170 for (i = 0; i < vec_size(proto->expression.params); ++i)
5171 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
5172 if (!parser_check_qualifiers(parser, var, proto)) {
5179 proto->expression.flags |= var->expression.flags;
5187 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
5188 "global `%s` already declared here: %s:%i",
5189 var->name, ast_ctx(old).file, ast_ctx(old).line))
5194 proto = (ast_value*)old;
5195 if (!ast_istype(old, ast_value)) {
5196 parseerror(parser, "internal error: not an ast_value");
5201 if (!parser_check_qualifiers(parser, var, proto)) {
5206 proto->expression.flags |= var->expression.flags;
5210 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC &&
5211 (old = parser_find_field(parser, var->name)))
5213 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5214 parseerror(parser, "global `%s` already declared here: %s:%i",
5215 var->name, ast_ctx(old).file, ast_ctx(old).line);
5222 else /* it's not a global */
5224 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
5225 if (old && !isparam) {
5226 parseerror(parser, "local `%s` already declared here: %s:%i",
5227 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
5231 /* doing this here as the above is just for a single scope */
5232 old = parser_find_local(parser, var->name, 0, &isparam);
5233 if (old && isparam) {
5234 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
5235 "local `%s` is shadowing a parameter", var->name))
5237 parseerror(parser, "local `%s` already declared here: %s:%i",
5238 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
5242 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
5244 if (ast_istype(old, ast_value))
5245 var = proto = (ast_value*)old;
5254 /* in a noref section we simply bump the usecount */
5255 if (noref || parser->noref)
5259 * Create the global/local, and deal with vector types.
5262 if (var->expression.vtype == TYPE_VECTOR)
5264 else if (var->expression.vtype == TYPE_FIELD &&
5265 var->expression.next->vtype == TYPE_VECTOR)
5269 if (!create_vector_members(var, me)) {
5276 /* deal with global variables, fields, functions */
5277 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
5278 var->isfield = true;
5279 vec_push(parser->fields, (ast_expression*)var);
5280 util_htset(parser->htfields, var->name, var);
5282 for (i = 0; i < 3; ++i) {
5283 vec_push(parser->fields, (ast_expression*)me[i]);
5284 util_htset(parser->htfields, me[i]->name, me[i]);
5289 if (!(var->expression.flags & AST_FLAG_ALIAS)) {
5290 parser_addglobal(parser, var->name, (ast_expression*)var);
5292 for (i = 0; i < 3; ++i) {
5293 parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
5297 ast_expression *find = parser_find_global(parser, var->desc);
5300 compile_error(parser_ctx(parser), "undeclared variable `%s` for alias `%s`", var->desc, var->name);
5304 if (!ast_compare_type((ast_expression*)var, find)) {
5308 ast_type_to_string(find, ty1, sizeof(ty1));
5309 ast_type_to_string((ast_expression*)var, ty2, sizeof(ty2));
5311 compile_error(parser_ctx(parser), "incompatible types `%s` and `%s` for alias `%s`",
5318 * add alias to aliases table and to corrector
5319 * so corrections can apply for aliases as well.
5321 util_htset(parser->aliases, var->name, find);
5324 * add to corrector so corrections can work
5325 * even for aliases too.
5328 vec_last(parser->correct_variables),
5329 &vec_last(parser->correct_variables_score),
5333 /* generate aliases for vector components */
5337 util_asprintf(&buffer[0], "%s_x", var->desc);
5338 util_asprintf(&buffer[1], "%s_y", var->desc);
5339 util_asprintf(&buffer[2], "%s_z", var->desc);
5341 util_htset(parser->aliases, me[0]->name, parser_find_global(parser, buffer[0]));
5342 util_htset(parser->aliases, me[1]->name, parser_find_global(parser, buffer[1]));
5343 util_htset(parser->aliases, me[2]->name, parser_find_global(parser, buffer[2]));
5350 * add to corrector so corrections can work
5351 * even for aliases too.
5354 vec_last(parser->correct_variables),
5355 &vec_last(parser->correct_variables_score),
5359 vec_last(parser->correct_variables),
5360 &vec_last(parser->correct_variables_score),
5364 vec_last(parser->correct_variables),
5365 &vec_last(parser->correct_variables_score),
5373 /* a static adds itself to be generated like any other global
5374 * but is added to the local namespace instead
5376 char *defname = NULL;
5377 size_t prefix_len, ln;
5379 ln = strlen(parser->function->name);
5380 vec_append(defname, ln, parser->function->name);
5382 vec_append(defname, 2, "::");
5383 /* remember the length up to here */
5384 prefix_len = vec_size(defname);
5386 /* Add it to the local scope */
5387 util_htset(vec_last(parser->variables), var->name, (void*)var);
5391 vec_last(parser->correct_variables),
5392 &vec_last(parser->correct_variables_score),
5396 /* now rename the global */
5397 ln = strlen(var->name);
5398 vec_append(defname, ln, var->name);
5399 ast_value_set_name(var, defname);
5401 /* push it to the to-be-generated globals */
5402 vec_push(parser->globals, (ast_expression*)var);
5404 /* same game for the vector members */
5406 for (i = 0; i < 3; ++i) {
5407 util_htset(vec_last(parser->variables), me[i]->name, (void*)(me[i]));
5411 vec_last(parser->correct_variables),
5412 &vec_last(parser->correct_variables_score),
5416 vec_shrinkto(defname, prefix_len);
5417 ln = strlen(me[i]->name);
5418 vec_append(defname, ln, me[i]->name);
5419 ast_member_set_name(me[i], defname);
5421 vec_push(parser->globals, (ast_expression*)me[i]);
5426 vec_push(localblock->locals, var);
5427 parser_addlocal(parser, var->name, (ast_expression*)var);
5429 for (i = 0; i < 3; ++i) {
5430 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
5431 ast_block_collect(localblock, (ast_expression*)me[i]);
5437 me[0] = me[1] = me[2] = NULL;
5442 if (var->expression.vtype == TYPE_ARRAY) {
5443 if (var->expression.count != (size_t)-1) {
5444 if (!create_array_accessors(parser, var))
5448 else if (!localblock && !nofields &&
5449 var->expression.vtype == TYPE_FIELD &&
5450 var->expression.next->vtype == TYPE_ARRAY)
5453 ast_expression *telem;
5455 ast_value *array = (ast_value*)var->expression.next;
5457 if (!ast_istype(var->expression.next, ast_value)) {
5458 parseerror(parser, "internal error: field element type must be an ast_value");
5462 platform_snprintf(name, sizeof(name), "%s##SETF", var->name);
5463 if (!parser_create_array_field_setter(parser, array, name))
5466 telem = ast_type_copy(ast_ctx(var), array->expression.next);
5467 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
5468 tfield->expression.next = telem;
5469 platform_snprintf(name, sizeof(name), "%s##GETFP", var->name);
5470 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
5478 if (parser->tok == ';') {
5479 ast_delete(basetype);
5480 if (!parser_next(parser)) {
5481 parseerror(parser, "error after variable declaration");
5487 if (parser->tok == ',')
5491 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
5494 parseerror(parser, "missing comma or semicolon while parsing variables");
5498 if (localblock && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5499 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
5500 "initializing expression turns variable `%s` into a constant in this standard",
5507 if (parser->tok != '{' || var->expression.vtype != TYPE_FUNCTION) {
5508 if (parser->tok != '=') {
5509 if (!strcmp(parser_tokval(parser), "break")) {
5510 if (!parser_next(parser)) {
5511 parseerror(parser, "error parsing break definition");
5514 (void)!parsewarning(parser, WARN_BREAKDEF, "break definition ignored (suggest removing it)");
5516 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
5521 if (!parser_next(parser)) {
5522 parseerror(parser, "error parsing initializer");
5526 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5527 parseerror(parser, "expected '=' before function body in this standard");
5530 if (parser->tok == '#') {
5531 ast_function *func = NULL;
5532 ast_value *number = NULL;
5538 parseerror(parser, "cannot declare builtins within functions");
5541 if (var->expression.vtype != TYPE_FUNCTION) {
5542 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
5545 if (!parser_next(parser)) {
5546 parseerror(parser, "expected builtin number");
5550 if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)) {
5551 number = (ast_value*)parse_expression_leave(parser, true, false, false);
5553 parseerror(parser, "builtin number expected");
5556 if (!ast_istype(number, ast_value) || !number->hasvalue || number->cvq != CV_CONST)
5559 parseerror(parser, "builtin number must be a compile time constant");
5562 if (number->expression.vtype == TYPE_INTEGER)
5563 builtin_num = number->constval.vint;
5564 else if (number->expression.vtype == TYPE_FLOAT)
5565 builtin_num = number->constval.vfloat;
5568 parseerror(parser, "builtin number must be an integer constant");
5573 fractional = modff(builtin_num, &integral);
5574 if (builtin_num < 0 || fractional != 0) {
5575 parseerror(parser, "builtin number must be an integer greater than zero");
5579 /* we only want the integral part anyways */
5580 builtin_num = integral;
5581 } else if (parser->tok == TOKEN_INTCONST) {
5582 builtin_num = parser_token(parser)->constval.i;
5584 parseerror(parser, "builtin number must be a compile time constant");
5588 if (var->hasvalue) {
5589 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
5590 "builtin `%s` has already been defined\n"
5591 " -> previous declaration here: %s:%i",
5592 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
5596 func = ast_function_new(ast_ctx(var), var->name, var);
5598 parseerror(parser, "failed to allocate function for `%s`", var->name);
5601 vec_push(parser->functions, func);
5603 func->builtin = -builtin_num-1;
5606 if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)
5607 ? (parser->tok != ',' && parser->tok != ';')
5608 : (!parser_next(parser)))
5610 parseerror(parser, "expected comma or semicolon");
5612 ast_function_delete(func);
5613 var->constval.vfunc = NULL;
5617 else if (var->expression.vtype == TYPE_ARRAY && parser->tok == '{')
5620 /* Note that fteqcc and most others don't even *have*
5621 * local arrays, so this is not a high priority.
5623 parseerror(parser, "TODO: initializers for local arrays");
5627 var->hasvalue = true;
5628 if (!parse_array(parser, var))
5631 else if (var->expression.vtype == TYPE_FUNCTION && (parser->tok == '{' || parser->tok == '['))
5634 parseerror(parser, "cannot declare functions within functions");
5639 ast_ctx(proto) = parser_ctx(parser);
5641 if (!parse_function_body(parser, var))
5643 ast_delete(basetype);
5644 for (i = 0; i < vec_size(parser->gotos); ++i)
5645 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
5646 vec_free(parser->gotos);
5647 vec_free(parser->labels);
5650 ast_expression *cexp;
5653 cexp = parse_expression_leave(parser, true, false, false);
5658 cval = (ast_value*)cexp;
5659 if (cval != parser->nil &&
5660 (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
5663 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
5667 if (!OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
5668 qualifier != CV_VAR)
5670 var->cvq = CV_CONST;
5672 if (cval == parser->nil)
5673 var->expression.flags |= AST_FLAG_INITIALIZED;
5676 var->hasvalue = true;
5677 if (cval->expression.vtype == TYPE_STRING)
5678 var->constval.vstring = parser_strdup(cval->constval.vstring);
5679 else if (cval->expression.vtype == TYPE_FIELD)
5680 var->constval.vfield = cval;
5682 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
5688 shunt sy = { NULL, NULL, NULL, NULL };
5691 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
5692 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
5693 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
5694 if (!parser_sy_apply_operator(parser, &sy))
5697 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
5698 parseerror(parser, "internal error: leaked operands");
5699 if (!ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out))
5710 if (parser->tok == ',') {
5711 if (!parser_next(parser)) {
5712 parseerror(parser, "expected another variable");
5716 if (parser->tok != TOKEN_IDENT) {
5717 parseerror(parser, "expected another variable");
5720 var = ast_value_copy(basetype);
5722 ast_value_set_name(var, parser_tokval(parser));
5723 if (!parser_next(parser)) {
5724 parseerror(parser, "error parsing variable declaration");
5730 if (parser->tok != ';') {
5731 parseerror(parser, "missing semicolon after variables");
5735 if (!parser_next(parser)) {
5736 parseerror(parser, "parse error after variable declaration");
5740 ast_delete(basetype);
5744 if (cleanvar && var)
5746 ast_delete(basetype);
5750 ast_delete(basetype);
5751 if (cleanvar && var)
5753 if (me[0]) ast_member_delete(me[0]);
5754 if (me[1]) ast_member_delete(me[1]);
5755 if (me[2]) ast_member_delete(me[2]);
5759 static bool parser_global_statement(parser_t *parser)
5763 bool is_static = false;
5764 uint32_t qflags = 0;
5765 ast_value *istype = NULL;
5766 char *vstring = NULL;
5768 if (parser->tok == TOKEN_IDENT)
5769 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
5771 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
5773 return parse_variable(parser, NULL, false, CV_NONE, istype, false, false, 0, NULL);
5775 else if (parse_qualifiers(parser, false, &cvq, &noref, &is_static, &qflags, &vstring))
5777 if (cvq == CV_WRONG)
5779 return parse_variable(parser, NULL, true, cvq, NULL, noref, is_static, qflags, vstring);
5781 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "enum"))
5783 return parse_enum(parser);
5785 else if (parser->tok == TOKEN_KEYWORD)
5787 if (!strcmp(parser_tokval(parser), "typedef")) {
5788 if (!parser_next(parser)) {
5789 parseerror(parser, "expected type definition after 'typedef'");
5792 return parse_typedef(parser);
5794 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
5797 else if (parser->tok == '#')
5799 return parse_pragma(parser);
5801 else if (parser->tok == '$')
5803 if (!parser_next(parser)) {
5804 parseerror(parser, "parse error");
5810 parseerror(parser, "unexpected token: `%s`", parser->lex->tok.value);
5816 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
5818 return util_crc16(old, str, strlen(str));
5821 static void progdefs_crc_file(const char *str)
5823 /* write to progdefs.h here */
5827 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
5829 old = progdefs_crc_sum(old, str);
5830 progdefs_crc_file(str);
5834 static void generate_checksum(parser_t *parser, ir_builder *ir)
5836 uint16_t crc = 0xFFFF;
5840 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
5841 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
5843 progdefs_crc_file("\tint\tpad;\n");
5844 progdefs_crc_file("\tint\tofs_return[3];\n");
5845 progdefs_crc_file("\tint\tofs_parm0[3];\n");
5846 progdefs_crc_file("\tint\tofs_parm1[3];\n");
5847 progdefs_crc_file("\tint\tofs_parm2[3];\n");
5848 progdefs_crc_file("\tint\tofs_parm3[3];\n");
5849 progdefs_crc_file("\tint\tofs_parm4[3];\n");
5850 progdefs_crc_file("\tint\tofs_parm5[3];\n");
5851 progdefs_crc_file("\tint\tofs_parm6[3];\n");
5852 progdefs_crc_file("\tint\tofs_parm7[3];\n");
5854 for (i = 0; i < parser->crc_globals; ++i) {
5855 if (!ast_istype(parser->globals[i], ast_value))
5857 value = (ast_value*)(parser->globals[i]);
5858 switch (value->expression.vtype) {
5859 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5860 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5861 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5862 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5864 crc = progdefs_crc_both(crc, "\tint\t");
5867 crc = progdefs_crc_both(crc, value->name);
5868 crc = progdefs_crc_both(crc, ";\n");
5870 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
5871 for (i = 0; i < parser->crc_fields; ++i) {
5872 if (!ast_istype(parser->fields[i], ast_value))
5874 value = (ast_value*)(parser->fields[i]);
5875 switch (value->expression.next->vtype) {
5876 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5877 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5878 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5879 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5881 crc = progdefs_crc_both(crc, "\tint\t");
5884 crc = progdefs_crc_both(crc, value->name);
5885 crc = progdefs_crc_both(crc, ";\n");
5887 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
5888 ir->code->crc = crc;
5891 parser_t *parser_create()
5894 lex_ctx_t empty_ctx;
5897 parser = (parser_t*)mem_a(sizeof(parser_t));
5901 memset(parser, 0, sizeof(*parser));
5903 for (i = 0; i < operator_count; ++i) {
5904 if (operators[i].id == opid1('=')) {
5905 parser->assign_op = operators+i;
5909 if (!parser->assign_op) {
5910 printf("internal error: initializing parser: failed to find assign operator\n");
5915 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
5916 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
5917 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
5918 vec_push(parser->_blocktypedefs, 0);
5920 parser->aliases = util_htnew(PARSER_HT_SIZE);
5923 vec_push(parser->correct_variables, correct_trie_new());
5924 vec_push(parser->correct_variables_score, NULL);
5926 empty_ctx.file = "<internal>";
5928 empty_ctx.column = 0;
5929 parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
5930 parser->nil->cvq = CV_CONST;
5931 if (OPTS_FLAG(UNTYPED_NIL))
5932 util_htset(parser->htglobals, "nil", (void*)parser->nil);
5934 parser->max_param_count = 1;
5936 parser->const_vec[0] = ast_value_new(empty_ctx, "<vector.x>", TYPE_NOEXPR);
5937 parser->const_vec[1] = ast_value_new(empty_ctx, "<vector.y>", TYPE_NOEXPR);
5938 parser->const_vec[2] = ast_value_new(empty_ctx, "<vector.z>", TYPE_NOEXPR);
5940 if (OPTS_OPTION_BOOL(OPTION_ADD_INFO)) {
5941 parser->reserved_version = ast_value_new(empty_ctx, "reserved:version", TYPE_STRING);
5942 parser->reserved_version->cvq = CV_CONST;
5943 parser->reserved_version->hasvalue = true;
5944 parser->reserved_version->expression.flags |= AST_FLAG_INCLUDE_DEF;
5945 parser->reserved_version->constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
5947 parser->reserved_version = NULL;
5950 parser->fold = fold_init (parser);
5951 parser->intrin = intrin_init(parser);
5955 static bool parser_compile(parser_t *parser)
5957 /* initial lexer/parser state */
5958 parser->lex->flags.noops = true;
5960 if (parser_next(parser))
5962 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
5964 if (!parser_global_statement(parser)) {
5965 if (parser->tok == TOKEN_EOF)
5966 parseerror(parser, "unexpected end of file");
5967 else if (compile_errors)
5968 parseerror(parser, "there have been errors, bailing out");
5969 lex_close(parser->lex);
5975 parseerror(parser, "parse error");
5976 lex_close(parser->lex);
5981 lex_close(parser->lex);
5984 return !compile_errors;
5987 bool parser_compile_file(parser_t *parser, const char *filename)
5989 parser->lex = lex_open(filename);
5991 con_err("failed to open file \"%s\"\n", filename);
5994 return parser_compile(parser);
5997 bool parser_compile_string(parser_t *parser, const char *name, const char *str, size_t len)
5999 parser->lex = lex_open_string(str, len, name);
6001 con_err("failed to create lexer for string \"%s\"\n", name);
6004 return parser_compile(parser);
6007 static void parser_remove_ast(parser_t *parser)
6010 if (parser->ast_cleaned)
6012 parser->ast_cleaned = true;
6013 for (i = 0; i < vec_size(parser->accessors); ++i) {
6014 ast_delete(parser->accessors[i]->constval.vfunc);
6015 parser->accessors[i]->constval.vfunc = NULL;
6016 ast_delete(parser->accessors[i]);
6018 for (i = 0; i < vec_size(parser->functions); ++i) {
6019 ast_delete(parser->functions[i]);
6021 for (i = 0; i < vec_size(parser->fields); ++i) {
6022 ast_delete(parser->fields[i]);
6024 for (i = 0; i < vec_size(parser->globals); ++i) {
6025 ast_delete(parser->globals[i]);
6027 vec_free(parser->accessors);
6028 vec_free(parser->functions);
6029 vec_free(parser->globals);
6030 vec_free(parser->fields);
6032 for (i = 0; i < vec_size(parser->variables); ++i)
6033 util_htdel(parser->variables[i]);
6034 vec_free(parser->variables);
6035 vec_free(parser->_blocklocals);
6036 vec_free(parser->_locals);
6039 for (i = 0; i < vec_size(parser->correct_variables); ++i) {
6040 correct_del(parser->correct_variables[i], parser->correct_variables_score[i]);
6042 vec_free(parser->correct_variables);
6043 vec_free(parser->correct_variables_score);
6045 for (i = 0; i < vec_size(parser->_typedefs); ++i)
6046 ast_delete(parser->_typedefs[i]);
6047 vec_free(parser->_typedefs);
6048 for (i = 0; i < vec_size(parser->typedefs); ++i)
6049 util_htdel(parser->typedefs[i]);
6050 vec_free(parser->typedefs);
6051 vec_free(parser->_blocktypedefs);
6053 vec_free(parser->_block_ctx);
6055 vec_free(parser->labels);
6056 vec_free(parser->gotos);
6057 vec_free(parser->breaks);
6058 vec_free(parser->continues);
6060 ast_value_delete(parser->nil);
6062 ast_value_delete(parser->const_vec[0]);
6063 ast_value_delete(parser->const_vec[1]);
6064 ast_value_delete(parser->const_vec[2]);
6066 if (parser->reserved_version)
6067 ast_value_delete(parser->reserved_version);
6069 util_htdel(parser->aliases);
6070 fold_cleanup(parser->fold);
6071 intrin_cleanup(parser->intrin);
6074 void parser_cleanup(parser_t *parser)
6076 parser_remove_ast(parser);
6080 bool parser_finish(parser_t *parser, const char *output)
6086 if (compile_errors) {
6087 con_out("*** there were compile errors\n");
6091 ir = ir_builder_new("gmqcc_out");
6093 con_out("failed to allocate builder\n");
6097 for (i = 0; i < vec_size(parser->fields); ++i) {
6100 if (!ast_istype(parser->fields[i], ast_value))
6102 field = (ast_value*)parser->fields[i];
6103 hasvalue = field->hasvalue;
6104 field->hasvalue = false;
6105 if (!ast_global_codegen((ast_value*)field, ir, true)) {
6106 con_out("failed to generate field %s\n", field->name);
6107 ir_builder_delete(ir);
6112 ast_expression *subtype;
6113 field->hasvalue = true;
6114 subtype = field->expression.next;
6115 ifld = ir_builder_create_field(ir, field->name, subtype->vtype);
6116 if (subtype->vtype == TYPE_FIELD)
6117 ifld->fieldtype = subtype->next->vtype;
6118 else if (subtype->vtype == TYPE_FUNCTION)
6119 ifld->outtype = subtype->next->vtype;
6120 (void)!ir_value_set_field(field->ir_v, ifld);
6123 for (i = 0; i < vec_size(parser->globals); ++i) {
6125 if (!ast_istype(parser->globals[i], ast_value))
6127 asvalue = (ast_value*)(parser->globals[i]);
6128 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
6129 retval = retval && !compile_warning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
6130 "unused global: `%s`", asvalue->name);
6132 if (!ast_global_codegen(asvalue, ir, false)) {
6133 con_out("failed to generate global %s\n", asvalue->name);
6134 ir_builder_delete(ir);
6138 /* Build function vararg accessor ast tree now before generating
6139 * immediates, because the accessors may add new immediates
6141 for (i = 0; i < vec_size(parser->functions); ++i) {
6142 ast_function *f = parser->functions[i];
6144 if (parser->max_param_count > vec_size(f->vtype->expression.params)) {
6145 f->varargs->expression.count = parser->max_param_count - vec_size(f->vtype->expression.params);
6146 if (!parser_create_array_setter_impl(parser, f->varargs)) {
6147 con_out("failed to generate vararg setter for %s\n", f->name);
6148 ir_builder_delete(ir);
6151 if (!parser_create_array_getter_impl(parser, f->varargs)) {
6152 con_out("failed to generate vararg getter for %s\n", f->name);
6153 ir_builder_delete(ir);
6157 ast_delete(f->varargs);
6162 /* Now we can generate immediates */
6163 if (!fold_generate(parser->fold, ir))
6166 for (i = 0; i < vec_size(parser->globals); ++i) {
6168 if (!ast_istype(parser->globals[i], ast_value))
6170 asvalue = (ast_value*)(parser->globals[i]);
6171 if (!(asvalue->expression.flags & AST_FLAG_INITIALIZED))
6173 if (asvalue->cvq == CV_CONST && !asvalue->hasvalue)
6174 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_CONSTANT,
6175 "uninitialized constant: `%s`",
6177 else if ((asvalue->cvq == CV_NONE || asvalue->cvq == CV_CONST) && !asvalue->hasvalue)
6178 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_GLOBAL,
6179 "uninitialized global: `%s`",
6182 if (!ast_generate_accessors(asvalue, ir)) {
6183 ir_builder_delete(ir);
6187 for (i = 0; i < vec_size(parser->fields); ++i) {
6189 asvalue = (ast_value*)(parser->fields[i]->next);
6191 if (!ast_istype((ast_expression*)asvalue, ast_value))
6193 if (asvalue->expression.vtype != TYPE_ARRAY)
6195 if (!ast_generate_accessors(asvalue, ir)) {
6196 ir_builder_delete(ir);
6200 if (parser->reserved_version &&
6201 !ast_global_codegen(parser->reserved_version, ir, false))
6203 con_out("failed to generate reserved::version");
6204 ir_builder_delete(ir);
6207 for (i = 0; i < vec_size(parser->functions); ++i) {
6208 ast_function *f = parser->functions[i];
6209 if (!ast_function_codegen(f, ir)) {
6210 con_out("failed to generate function %s\n", f->name);
6211 ir_builder_delete(ir);
6216 generate_checksum(parser, ir);
6218 if (OPTS_OPTION_BOOL(OPTION_DUMP))
6219 ir_builder_dump(ir, con_out);
6220 for (i = 0; i < vec_size(parser->functions); ++i) {
6221 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
6222 con_out("failed to finalize function %s\n", parser->functions[i]->name);
6223 ir_builder_delete(ir);
6227 parser_remove_ast(parser);
6229 if (compile_Werrors) {
6230 con_out("*** there were warnings treated as errors\n");
6231 compile_show_werrors();
6236 if (OPTS_OPTION_BOOL(OPTION_DUMPFIN))
6237 ir_builder_dump(ir, con_out);
6239 if (!ir_builder_generate(ir, output)) {
6240 con_out("*** failed to generate output file\n");
6241 ir_builder_delete(ir);
6245 ir_builder_delete(ir);