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
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 128
35 #define TYPEDEF_HT_SIZE 16
41 ast_expression **globals;
42 ast_expression **fields;
43 ast_function **functions;
44 ast_value **imm_float;
45 ast_value **imm_string;
46 ast_value **imm_vector;
49 /* must be deleted first, they reference immediates and values */
50 ast_value **accessors;
52 ast_value *imm_float_zero;
53 ast_value *imm_float_one;
54 ast_value *imm_float_neg_one;
56 ast_value *imm_vector_zero;
59 ast_value *reserved_version;
64 ast_function *function;
66 /* All the labels the function defined...
67 * Should they be in ast_function instead?
72 const char **continues;
74 /* A list of hashtables for each scope */
80 /* same as above but for the spelling corrector */
81 correct_trie_t **correct_variables;
82 size_t ***correct_variables_score; /* vector of vector of size_t* */
84 /* not to be used directly, we use the hash table */
85 ast_expression **_locals;
87 ast_value **_typedefs;
88 size_t *_blocktypedefs;
91 /* we store the '=' operator info */
92 const oper_info *assign_op;
95 ast_value *const_vec[3];
100 /* collected information */
101 size_t max_param_count;
104 static ast_expression * const intrinsic_debug_typestring = (ast_expression*)0x1;
106 static void parser_enterblock(parser_t *parser);
107 static bool parser_leaveblock(parser_t *parser);
108 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
109 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
110 static bool parse_typedef(parser_t *parser);
111 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);
112 static ast_block* parse_block(parser_t *parser);
113 static bool parse_block_into(parser_t *parser, ast_block *block);
114 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
115 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
116 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels);
117 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels);
118 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
119 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
120 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
122 static void parseerror(parser_t *parser, const char *fmt, ...)
126 vcompile_error(parser->lex->tok.ctx, fmt, ap);
130 /* returns true if it counts as an error */
131 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
136 r = vcompile_warning(parser->lex->tok.ctx, warntype, fmt, ap);
141 /**********************************************************************
142 * some maths used for constant folding
145 vector vec3_add(vector a, vector b)
154 vector vec3_sub(vector a, vector b)
163 qcfloat vec3_mulvv(vector a, vector b)
165 return (a.x * b.x + a.y * b.y + a.z * b.z);
168 vector vec3_mulvf(vector a, float b)
177 /**********************************************************************
181 bool parser_next(parser_t *parser)
183 /* lex_do kills the previous token */
184 parser->tok = lex_do(parser->lex);
185 if (parser->tok == TOKEN_EOF)
187 if (parser->tok >= TOKEN_ERROR) {
188 parseerror(parser, "lex error");
194 #define parser_tokval(p) ((p)->lex->tok.value)
195 #define parser_token(p) (&((p)->lex->tok))
196 #define parser_ctx(p) ((p)->lex->tok.ctx)
198 static ast_value* parser_const_float(parser_t *parser, double d)
203 for (i = 0; i < vec_size(parser->imm_float); ++i) {
204 const double compare = parser->imm_float[i]->constval.vfloat;
205 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
206 return parser->imm_float[i];
209 ctx = parser_ctx(parser);
211 memset(&ctx, 0, sizeof(ctx));
213 out = ast_value_new(ctx, "#IMMEDIATE", TYPE_FLOAT);
215 out->hasvalue = true;
216 out->constval.vfloat = d;
217 vec_push(parser->imm_float, out);
221 static ast_value* parser_const_float_0(parser_t *parser)
223 if (!parser->imm_float_zero)
224 parser->imm_float_zero = parser_const_float(parser, 0);
225 return parser->imm_float_zero;
228 static ast_value* parser_const_float_neg1(parser_t *parser) {
229 if (!parser->imm_float_neg_one)
230 parser->imm_float_neg_one = parser_const_float(parser, -1);
231 return parser->imm_float_neg_one;
234 static ast_value* parser_const_float_1(parser_t *parser)
236 if (!parser->imm_float_one)
237 parser->imm_float_one = parser_const_float(parser, 1);
238 return parser->imm_float_one;
241 static char *parser_strdup(const char *str)
244 /* actually dup empty strings */
245 char *out = (char*)mem_a(1);
249 return util_strdup(str);
252 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
256 for (i = 0; i < vec_size(parser->imm_string); ++i) {
257 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
258 return parser->imm_string[i];
262 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
263 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
265 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
267 out->hasvalue = true;
268 out->constval.vstring = parser_strdup(str);
269 vec_push(parser->imm_string, out);
273 static ast_value* parser_const_vector(parser_t *parser, vector v)
277 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
278 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
279 return parser->imm_vector[i];
281 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
283 out->hasvalue = true;
284 out->constval.vvec = v;
285 vec_push(parser->imm_vector, out);
289 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
295 return parser_const_vector(parser, v);
298 static ast_value* parser_const_vector_0(parser_t *parser)
300 if (!parser->imm_vector_zero)
301 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
302 return parser->imm_vector_zero;
305 static ast_expression* parser_find_field(parser_t *parser, const char *name)
307 return ( ast_expression*)util_htget(parser->htfields, name);
310 static ast_expression* parser_find_label(parser_t *parser, const char *name)
313 for(i = 0; i < vec_size(parser->labels); i++)
314 if (!strcmp(parser->labels[i]->name, name))
315 return (ast_expression*)parser->labels[i];
319 static ast_expression* parser_find_global(parser_t *parser, const char *name)
321 return (ast_expression*)util_htget(parser->htglobals, name);
324 static ast_expression* parser_find_param(parser_t *parser, const char *name)
328 if (!parser->function)
330 fun = parser->function->vtype;
331 for (i = 0; i < vec_size(fun->expression.params); ++i) {
332 if (!strcmp(fun->expression.params[i]->name, name))
333 return (ast_expression*)(fun->expression.params[i]);
338 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
343 hash = util_hthash(parser->htglobals, name);
346 for (i = vec_size(parser->variables); i > upto;) {
348 if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
352 return parser_find_param(parser, name);
355 static ast_expression* parser_find_var(parser_t *parser, const char *name)
359 v = parser_find_local(parser, name, 0, &dummy);
360 if (!v) v = parser_find_global(parser, name);
364 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
368 hash = util_hthash(parser->typedefs[0], name);
370 for (i = vec_size(parser->typedefs); i > upto;) {
372 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
380 size_t etype; /* 0 = expression, others are operators */
384 ast_block *block; /* for commas and function calls */
403 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
414 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
418 e.out = (ast_expression*)v;
425 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
427 e.etype = 1 + (op - operators);
436 static sy_elem syparen(lex_ctx ctx, size_t off) {
447 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
448 * so we need to rotate it to become ent.(foo[n]).
450 static bool rotate_entfield_array_index_nodes(ast_expression **out)
452 ast_array_index *index, *oldindex;
453 ast_entfield *entfield;
457 ast_expression *entity;
459 lex_ctx ctx = ast_ctx(*out);
461 if (!ast_istype(*out, ast_array_index))
463 index = (ast_array_index*)*out;
465 if (!ast_istype(index->array, ast_entfield))
467 entfield = (ast_entfield*)index->array;
469 if (!ast_istype(entfield->field, ast_value))
471 field = (ast_value*)entfield->field;
474 entity = entfield->entity;
478 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
479 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
480 *out = (ast_expression*)entfield;
482 oldindex->array = NULL;
483 oldindex->index = NULL;
484 ast_delete(oldindex);
489 static bool immediate_is_true(lex_ctx ctx, ast_value *v)
491 switch (v->expression.vtype) {
493 return !!v->constval.vfloat;
495 return !!v->constval.vint;
497 if (OPTS_FLAG(CORRECT_LOGIC))
498 return v->constval.vvec.x &&
499 v->constval.vvec.y &&
502 return !!(v->constval.vvec.x);
504 if (!v->constval.vstring)
506 if (v->constval.vstring && OPTS_FLAG(TRUE_EMPTY_STRINGS))
508 return !!v->constval.vstring[0];
510 compile_error(ctx, "internal error: immediate_is_true on invalid type");
511 return !!v->constval.vfunc;
515 static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
519 ast_expression *out = NULL;
520 ast_expression *exprs[3];
521 ast_block *blocks[3];
522 ast_value *asvalue[3];
523 ast_binstore *asbinstore;
524 size_t i, assignop, addop, subop;
525 qcint generated_op = 0;
530 if (!vec_size(sy->ops)) {
531 parseerror(parser, "internal error: missing operator");
535 if (vec_last(sy->ops).isparen) {
536 parseerror(parser, "unmatched parenthesis");
540 op = &operators[vec_last(sy->ops).etype - 1];
541 ctx = vec_last(sy->ops).ctx;
543 if (vec_size(sy->out) < op->operands) {
544 compile_error(ctx, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
545 op->op, (int)op->id);
549 vec_shrinkby(sy->ops, 1);
551 /* op(:?) has no input and no output */
555 vec_shrinkby(sy->out, op->operands);
556 for (i = 0; i < op->operands; ++i) {
557 exprs[i] = sy->out[vec_size(sy->out)+i].out;
558 blocks[i] = sy->out[vec_size(sy->out)+i].block;
559 asvalue[i] = (ast_value*)exprs[i];
561 if (exprs[i]->expression.vtype == TYPE_NOEXPR &&
562 !(i != 0 && op->id == opid2('?',':')) &&
563 !(i == 1 && op->id == opid1('.')))
565 if (ast_istype(exprs[i], ast_label))
566 compile_error(ast_ctx(exprs[i]), "expected expression, got an unknown identifier");
568 compile_error(ast_ctx(exprs[i]), "not an expression");
569 (void)!compile_warning(ast_ctx(exprs[i]), WARN_DEBUG, "expression %u\n", (unsigned int)i);
573 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
574 compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
578 #define NotSameType(T) \
579 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
580 exprs[0]->expression.vtype != T)
581 #define CanConstFold1(A) \
582 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && (((ast_value*)(A))->cvq == CV_CONST) &&\
583 (A)->expression.vtype != TYPE_FUNCTION)
584 #define CanConstFold(A, B) \
585 (CanConstFold1(A) && CanConstFold1(B))
586 #define ConstV(i) (asvalue[(i)]->constval.vvec)
587 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
588 #define ConstS(i) (asvalue[(i)]->constval.vstring)
592 compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
596 if (exprs[0]->expression.vtype == TYPE_VECTOR &&
597 exprs[1]->expression.vtype == TYPE_NOEXPR)
599 if (exprs[1] == (ast_expression*)parser->const_vec[0])
600 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
601 else if (exprs[1] == (ast_expression*)parser->const_vec[1])
602 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
603 else if (exprs[1] == (ast_expression*)parser->const_vec[2])
604 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
606 compile_error(ctx, "access to invalid vector component");
610 else if (exprs[0]->expression.vtype == TYPE_ENTITY) {
611 if (exprs[1]->expression.vtype != TYPE_FIELD) {
612 compile_error(ast_ctx(exprs[1]), "type error: right hand of member-operand should be an entity-field");
615 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
617 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
618 compile_error(ast_ctx(exprs[1]), "vectors cannot be accessed this way");
622 compile_error(ast_ctx(exprs[1]), "type error: member-of operator on something that is not an entity or vector");
628 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
629 !(exprs[0]->expression.vtype == TYPE_FIELD &&
630 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
632 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
633 compile_error(ast_ctx(exprs[0]), "cannot index value of type %s", ty1);
636 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
637 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
638 compile_error(ast_ctx(exprs[1]), "index must be of type float, not %s", ty1);
641 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
642 if (rotate_entfield_array_index_nodes(&out))
645 /* This is not broken in fteqcc anymore */
646 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
647 /* this error doesn't need to make us bail out */
648 (void)!parsewarning(parser, WARN_EXTENSIONS,
649 "accessing array-field members of an entity without parenthesis\n"
650 " -> this is an extension from -std=gmqcc");
657 if (vec_size(sy->paren) && vec_last(sy->paren) == PAREN_FUNC) {
658 vec_push(sy->out, syexp(ctx, exprs[0]));
659 vec_push(sy->out, syexp(ctx, exprs[1]));
660 vec_last(sy->argc)++;
664 if (!ast_block_add_expr(blocks[0], exprs[1]))
667 blocks[0] = ast_block_new(ctx);
668 if (!ast_block_add_expr(blocks[0], exprs[0]) ||
669 !ast_block_add_expr(blocks[0], exprs[1]))
674 if (!ast_block_set_type(blocks[0], exprs[1]))
677 vec_push(sy->out, syblock(ctx, blocks[0]));
684 switch (exprs[0]->expression.vtype) {
686 if (CanConstFold1(exprs[0]))
687 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
689 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
690 (ast_expression*)parser_const_float_0(parser),
694 if (CanConstFold1(exprs[0]))
695 out = (ast_expression*)parser_const_vector_f(parser,
696 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
698 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
699 (ast_expression*)parser_const_vector_0(parser),
703 compile_error(ctx, "invalid types used in expression: cannot negate type %s",
704 type_name[exprs[0]->expression.vtype]);
710 switch (exprs[0]->expression.vtype) {
712 if (CanConstFold1(exprs[0]))
713 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
715 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
718 if (CanConstFold1(exprs[0]))
719 out = (ast_expression*)parser_const_float(parser,
720 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
722 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
725 if (CanConstFold1(exprs[0])) {
726 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
727 out = (ast_expression*)parser_const_float(parser, !ConstS(0));
729 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
731 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
732 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
734 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
737 /* we don't constant-fold NOT for these types */
739 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
742 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
745 compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
746 type_name[exprs[0]->expression.vtype]);
752 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
753 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
755 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
756 type_name[exprs[0]->expression.vtype],
757 type_name[exprs[1]->expression.vtype]);
760 switch (exprs[0]->expression.vtype) {
762 if (CanConstFold(exprs[0], exprs[1]))
764 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
767 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
770 if (CanConstFold(exprs[0], exprs[1]))
771 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
773 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
776 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
777 type_name[exprs[0]->expression.vtype],
778 type_name[exprs[1]->expression.vtype]);
783 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
784 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
786 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
787 type_name[exprs[1]->expression.vtype],
788 type_name[exprs[0]->expression.vtype]);
791 switch (exprs[0]->expression.vtype) {
793 if (CanConstFold(exprs[0], exprs[1]))
794 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
796 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
799 if (CanConstFold(exprs[0], exprs[1]))
800 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
802 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
805 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
806 type_name[exprs[1]->expression.vtype],
807 type_name[exprs[0]->expression.vtype]);
812 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
813 !(exprs[0]->expression.vtype == TYPE_VECTOR &&
814 exprs[1]->expression.vtype == TYPE_FLOAT) &&
815 !(exprs[1]->expression.vtype == TYPE_VECTOR &&
816 exprs[0]->expression.vtype == TYPE_FLOAT)
819 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
820 type_name[exprs[1]->expression.vtype],
821 type_name[exprs[0]->expression.vtype]);
824 switch (exprs[0]->expression.vtype) {
826 if (exprs[1]->expression.vtype == TYPE_VECTOR)
828 if (CanConstFold(exprs[0], exprs[1]))
829 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
831 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
835 if (CanConstFold(exprs[0], exprs[1]))
836 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
838 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
842 if (exprs[1]->expression.vtype == TYPE_FLOAT)
844 if (CanConstFold(exprs[0], exprs[1]))
845 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
847 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
851 if (CanConstFold(exprs[0], exprs[1]))
852 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
853 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[0])) {
854 vector vec = ConstV(0);
855 if (!vec.y && !vec.z) { /* 'n 0 0' * v */
856 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
857 out = (ast_expression*)ast_member_new(ctx, exprs[1], 0, NULL);
858 out->expression.node.keep = false;
859 ((ast_member*)out)->rvalue = true;
861 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.x), out);
863 else if (!vec.x && !vec.z) { /* '0 n 0' * v */
864 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
865 out = (ast_expression*)ast_member_new(ctx, exprs[1], 1, NULL);
866 out->expression.node.keep = false;
867 ((ast_member*)out)->rvalue = true;
869 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.y), out);
871 else if (!vec.x && !vec.y) { /* '0 n 0' * v */
872 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
873 out = (ast_expression*)ast_member_new(ctx, exprs[1], 2, NULL);
874 out->expression.node.keep = false;
875 ((ast_member*)out)->rvalue = true;
877 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.z), out);
880 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
882 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[1])) {
883 vector vec = ConstV(1);
884 if (!vec.y && !vec.z) { /* v * 'n 0 0' */
885 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
886 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
887 out->expression.node.keep = false;
888 ((ast_member*)out)->rvalue = true;
890 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.x));
892 else if (!vec.x && !vec.z) { /* v * '0 n 0' */
893 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
894 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
895 out->expression.node.keep = false;
896 ((ast_member*)out)->rvalue = true;
898 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.y));
900 else if (!vec.x && !vec.y) { /* v * '0 n 0' */
901 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
902 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
903 out->expression.node.keep = false;
904 ((ast_member*)out)->rvalue = true;
906 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.z));
909 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
912 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
916 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
917 type_name[exprs[1]->expression.vtype],
918 type_name[exprs[0]->expression.vtype]);
923 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
924 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
925 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
926 compile_error(ctx, "invalid types used in expression: cannot divide tyeps %s and %s", ty1, ty2);
929 if (exprs[0]->expression.vtype == TYPE_FLOAT) {
930 if (CanConstFold(exprs[0], exprs[1]))
931 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
933 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
935 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
936 if (CanConstFold(exprs[0], exprs[1]))
937 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), 1.0/ConstF(1)));
939 if (CanConstFold1(exprs[1])) {
940 out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1));
942 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
943 (ast_expression*)parser_const_float_1(parser),
947 compile_error(ctx, "internal error: failed to generate division");
950 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], out);
955 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
956 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
957 compile_error(ctx, "invalid types used in expression: cannot divide tyeps %s and %s", ty1, ty2);
963 compile_error(ctx, "qc does not have a modulo operator");
967 if (NotSameType(TYPE_FLOAT)) {
968 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
969 type_name[exprs[0]->expression.vtype],
970 type_name[exprs[1]->expression.vtype]);
973 if (CanConstFold(exprs[0], exprs[1]))
974 out = (ast_expression*)parser_const_float(parser,
975 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
976 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
978 out = (ast_expression*)ast_binary_new(ctx,
979 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
983 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor via ^");
988 if (CanConstFold(exprs[0], exprs[1]) && ! NotSameType(TYPE_FLOAT)) {
989 if (op->id == opid2('<','<'))
990 out = (ast_expression*)parser_const_float(parser, (double)((int)(ConstF(0)) << (int)(ConstF(1))));
992 out = (ast_expression*)parser_const_float(parser, (double)((int)(ConstF(0)) >> (int)(ConstF(1))));
995 case opid3('<','<','='):
996 case opid3('>','>','='):
997 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-shifts");
1000 case opid2('|','|'):
1001 generated_op += 1; /* INSTR_OR */
1002 case opid2('&','&'):
1003 generated_op += INSTR_AND;
1004 if (CanConstFold(exprs[0], exprs[1]))
1006 if (OPTS_FLAG(PERL_LOGIC)) {
1007 if (immediate_is_true(ctx, asvalue[0]))
1011 out = (ast_expression*)parser_const_float(parser,
1012 ( (generated_op == INSTR_OR)
1013 ? (immediate_is_true(ctx, asvalue[0]) || immediate_is_true(ctx, asvalue[1]))
1014 : (immediate_is_true(ctx, asvalue[0]) && immediate_is_true(ctx, asvalue[1])) )
1019 if (OPTS_FLAG(PERL_LOGIC) && !ast_compare_type(exprs[0], exprs[1])) {
1020 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1021 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1022 compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
1025 for (i = 0; i < 2; ++i) {
1026 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->expression.vtype == TYPE_VECTOR) {
1027 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[i]);
1029 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
1031 exprs[i] = out; out = NULL;
1032 if (OPTS_FLAG(PERL_LOGIC)) {
1033 /* here we want to keep the right expressions' type */
1037 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->expression.vtype == TYPE_STRING) {
1038 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[i]);
1040 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
1042 exprs[i] = out; out = NULL;
1043 if (OPTS_FLAG(PERL_LOGIC)) {
1044 /* here we want to keep the right expressions' type */
1049 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
1053 case opid2('?',':'):
1054 if (vec_last(sy->paren) != PAREN_TERNARY2) {
1055 compile_error(ctx, "mismatched parenthesis/ternary");
1059 if (!ast_compare_type(exprs[1], exprs[2])) {
1060 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
1061 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
1062 compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
1065 if (CanConstFold1(exprs[0]))
1066 out = (immediate_is_true(ctx, asvalue[0]) ? exprs[1] : exprs[2]);
1068 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
1072 generated_op += 1; /* INSTR_GT */
1074 generated_op += 1; /* INSTR_LT */
1075 case opid2('>', '='):
1076 generated_op += 1; /* INSTR_GE */
1077 case opid2('<', '='):
1078 generated_op += INSTR_LE;
1079 if (NotSameType(TYPE_FLOAT)) {
1080 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1081 type_name[exprs[0]->expression.vtype],
1082 type_name[exprs[1]->expression.vtype]);
1085 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
1087 case opid2('!', '='):
1088 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
1089 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1090 type_name[exprs[0]->expression.vtype],
1091 type_name[exprs[1]->expression.vtype]);
1094 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
1096 case opid2('=', '='):
1097 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
1098 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1099 type_name[exprs[0]->expression.vtype],
1100 type_name[exprs[1]->expression.vtype]);
1103 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
1107 if (ast_istype(exprs[0], ast_entfield)) {
1108 ast_expression *field = ((ast_entfield*)exprs[0])->field;
1109 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1110 exprs[0]->expression.vtype == TYPE_FIELD &&
1111 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
1113 assignop = type_storep_instr[TYPE_VECTOR];
1116 assignop = type_storep_instr[exprs[0]->expression.vtype];
1117 if (assignop == VINSTR_END || !ast_compare_type(field->expression.next, exprs[1]))
1119 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
1120 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1121 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1122 field->expression.next->expression.vtype == TYPE_FUNCTION &&
1123 exprs[1]->expression.vtype == TYPE_FUNCTION)
1125 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
1126 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1129 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1134 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1135 exprs[0]->expression.vtype == TYPE_FIELD &&
1136 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
1138 assignop = type_store_instr[TYPE_VECTOR];
1141 assignop = type_store_instr[exprs[0]->expression.vtype];
1144 if (assignop == VINSTR_END) {
1145 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1146 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1147 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1149 else if (!ast_compare_type(exprs[0], exprs[1]))
1151 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1152 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1153 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1154 exprs[0]->expression.vtype == TYPE_FUNCTION &&
1155 exprs[1]->expression.vtype == TYPE_FUNCTION)
1157 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
1158 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1161 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1164 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1165 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1167 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
1169 case opid3('+','+','P'):
1170 case opid3('-','-','P'):
1172 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
1173 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1174 compile_error(ast_ctx(exprs[0]), "invalid type for prefix increment: %s", ty1);
1177 if (op->id == opid3('+','+','P'))
1178 addop = INSTR_ADD_F;
1180 addop = INSTR_SUB_F;
1181 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1182 compile_error(ast_ctx(exprs[0]), "assignment to constant `%s`", asvalue[0]->name);
1184 if (ast_istype(exprs[0], ast_entfield)) {
1185 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1187 (ast_expression*)parser_const_float_1(parser));
1189 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1191 (ast_expression*)parser_const_float_1(parser));
1194 case opid3('S','+','+'):
1195 case opid3('S','-','-'):
1197 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
1198 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1199 compile_error(ast_ctx(exprs[0]), "invalid type for suffix increment: %s", ty1);
1202 if (op->id == opid3('S','+','+')) {
1203 addop = INSTR_ADD_F;
1204 subop = INSTR_SUB_F;
1206 addop = INSTR_SUB_F;
1207 subop = INSTR_ADD_F;
1209 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1210 compile_error(ast_ctx(exprs[0]), "assignment to constant `%s`", asvalue[0]->name);
1212 if (ast_istype(exprs[0], ast_entfield)) {
1213 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1215 (ast_expression*)parser_const_float_1(parser));
1217 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1219 (ast_expression*)parser_const_float_1(parser));
1223 out = (ast_expression*)ast_binary_new(ctx, subop,
1225 (ast_expression*)parser_const_float_1(parser));
1227 case opid2('+','='):
1228 case opid2('-','='):
1229 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1230 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1232 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1233 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1234 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1238 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1239 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1241 if (ast_istype(exprs[0], ast_entfield))
1242 assignop = type_storep_instr[exprs[0]->expression.vtype];
1244 assignop = type_store_instr[exprs[0]->expression.vtype];
1245 switch (exprs[0]->expression.vtype) {
1247 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1248 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1249 exprs[0], exprs[1]);
1252 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1253 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1254 exprs[0], exprs[1]);
1257 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1258 type_name[exprs[0]->expression.vtype],
1259 type_name[exprs[1]->expression.vtype]);
1263 case opid2('*','='):
1264 case opid2('/','='):
1265 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1266 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1267 exprs[0]->expression.vtype == TYPE_VECTOR))
1269 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1270 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1271 compile_error(ctx, "invalid types used in expression: %s and %s",
1275 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1276 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1278 if (ast_istype(exprs[0], ast_entfield))
1279 assignop = type_storep_instr[exprs[0]->expression.vtype];
1281 assignop = type_store_instr[exprs[0]->expression.vtype];
1282 switch (exprs[0]->expression.vtype) {
1284 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1285 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1286 exprs[0], exprs[1]);
1289 if (op->id == opid2('*','=')) {
1290 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1291 exprs[0], exprs[1]);
1293 /* there's no DIV_VF */
1294 if (CanConstFold1(exprs[1])) {
1295 out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1));
1297 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1298 (ast_expression*)parser_const_float_1(parser),
1302 compile_error(ctx, "internal error: failed to generate division");
1305 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1310 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1311 type_name[exprs[0]->expression.vtype],
1312 type_name[exprs[1]->expression.vtype]);
1316 case opid2('&','='):
1317 case opid2('|','='):
1318 if (NotSameType(TYPE_FLOAT)) {
1319 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1320 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1321 compile_error(ctx, "invalid types used in expression: %s and %s",
1325 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1326 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1328 if (ast_istype(exprs[0], ast_entfield))
1329 assignop = type_storep_instr[exprs[0]->expression.vtype];
1331 assignop = type_store_instr[exprs[0]->expression.vtype];
1332 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1333 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1334 exprs[0], exprs[1]);
1336 case opid3('&','~','='):
1337 /* This is like: a &= ~(b);
1338 * But QC has no bitwise-not, so we implement it as
1341 if (NotSameType(TYPE_FLOAT)) {
1342 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1343 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1344 compile_error(ctx, "invalid types used in expression: %s and %s",
1348 if (ast_istype(exprs[0], ast_entfield))
1349 assignop = type_storep_instr[exprs[0]->expression.vtype];
1351 assignop = type_store_instr[exprs[0]->expression.vtype];
1352 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1355 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1356 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1358 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1359 asbinstore->keep_dest = true;
1360 out = (ast_expression*)asbinstore;
1363 case opid2('~', 'P'):
1364 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
1365 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1366 compile_error(ast_ctx(exprs[0]), "invalid type for bit not: %s", ty1);
1370 if(CanConstFold1(exprs[0]))
1371 out = (ast_expression*)parser_const_float(parser, ~(qcint)ConstF(0));
1373 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]);
1380 compile_error(ctx, "failed to apply operator %s", op->op);
1384 vec_push(sy->out, syexp(ctx, out));
1388 static bool parser_close_call(parser_t *parser, shunt *sy)
1390 /* was a function call */
1391 ast_expression *fun;
1392 ast_value *funval = NULL;
1396 size_t paramcount, i;
1398 fid = vec_last(sy->ops).off;
1399 vec_shrinkby(sy->ops, 1);
1401 /* out[fid] is the function
1402 * everything above is parameters...
1404 if (!vec_size(sy->argc)) {
1405 parseerror(parser, "internal error: no argument counter available");
1409 paramcount = vec_last(sy->argc);
1412 if (vec_size(sy->out) < fid) {
1413 parseerror(parser, "internal error: broken function call%lu < %lu+%lu\n",
1414 (unsigned long)vec_size(sy->out),
1416 (unsigned long)paramcount);
1420 fun = sy->out[fid].out;
1422 if (fun == intrinsic_debug_typestring) {
1424 if (fid+2 != vec_size(sy->out) ||
1425 vec_last(sy->out).block)
1427 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1430 ast_type_to_string(vec_last(sy->out).out, ty, sizeof(ty));
1431 ast_unref(vec_last(sy->out).out);
1432 sy->out[fid] = syexp(ast_ctx(vec_last(sy->out).out),
1433 (ast_expression*)parser_const_string(parser, ty, false));
1434 vec_shrinkby(sy->out, 1);
1438 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1442 if (fid+1 < vec_size(sy->out))
1445 if (fid+1 + paramcount != vec_size(sy->out)) {
1446 parseerror(parser, "internal error: parameter count mismatch: (%lu+1+%lu), %lu",
1447 (unsigned long)fid, (unsigned long)paramcount, (unsigned long)vec_size(sy->out));
1451 for (i = 0; i < paramcount; ++i)
1452 vec_push(call->params, sy->out[fid+1 + i].out);
1453 vec_shrinkby(sy->out, paramcount);
1454 (void)!ast_call_check_types(call);
1455 if (parser->max_param_count < paramcount)
1456 parser->max_param_count = paramcount;
1458 if (ast_istype(fun, ast_value)) {
1459 funval = (ast_value*)fun;
1460 if ((fun->expression.flags & AST_FLAG_VARIADIC) &&
1461 !(/*funval->cvq == CV_CONST && */ funval->hasvalue && funval->constval.vfunc->builtin))
1463 call->va_count = (ast_expression*)parser_const_float(parser, (double)paramcount);
1467 /* overwrite fid, the function, with a call */
1468 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1470 if (fun->expression.vtype != TYPE_FUNCTION) {
1471 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1475 if (!fun->expression.next) {
1476 parseerror(parser, "could not determine function return type");
1479 ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1481 if (fun->expression.flags & AST_FLAG_DEPRECATED) {
1483 return !parsewarning(parser, WARN_DEPRECATED,
1484 "call to function (which is marked deprecated)\n",
1485 "-> it has been declared here: %s:%i",
1486 ast_ctx(fun).file, ast_ctx(fun).line);
1489 return !parsewarning(parser, WARN_DEPRECATED,
1490 "call to `%s` (which is marked deprecated)\n"
1491 "-> `%s` declared here: %s:%i",
1492 fval->name, fval->name, ast_ctx(fun).file, ast_ctx(fun).line);
1494 return !parsewarning(parser, WARN_DEPRECATED,
1495 "call to `%s` (deprecated: %s)\n"
1496 "-> `%s` declared here: %s:%i",
1497 fval->name, fval->desc, fval->name, ast_ctx(fun).file,
1501 if (vec_size(fun->expression.params) != paramcount &&
1502 !((fun->expression.flags & AST_FLAG_VARIADIC) &&
1503 vec_size(fun->expression.params) < paramcount))
1505 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1507 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1508 "too %s parameters for call to %s: expected %i, got %i\n"
1509 " -> `%s` has been declared here: %s:%i",
1510 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1511 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1513 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1514 "too %s parameters for function call: expected %i, got %i\n"
1515 " -> it has been declared here: %s:%i",
1516 fewmany, (int)vec_size(fun->expression.params), (int)paramcount,
1517 ast_ctx(fun).file, (int)ast_ctx(fun).line);
1524 static bool parser_close_paren(parser_t *parser, shunt *sy)
1526 if (!vec_size(sy->ops)) {
1527 parseerror(parser, "unmatched closing paren");
1531 while (vec_size(sy->ops)) {
1532 if (vec_last(sy->ops).isparen) {
1533 if (vec_last(sy->paren) == PAREN_FUNC) {
1535 if (!parser_close_call(parser, sy))
1539 if (vec_last(sy->paren) == PAREN_EXPR) {
1541 if (!vec_size(sy->out)) {
1542 compile_error(vec_last(sy->ops).ctx, "empty paren expression");
1543 vec_shrinkby(sy->ops, 1);
1546 vec_shrinkby(sy->ops, 1);
1549 if (vec_last(sy->paren) == PAREN_INDEX) {
1551 /* pop off the parenthesis */
1552 vec_shrinkby(sy->ops, 1);
1553 /* then apply the index operator */
1554 if (!parser_sy_apply_operator(parser, sy))
1558 if (vec_last(sy->paren) == PAREN_TERNARY1) {
1559 vec_last(sy->paren) = PAREN_TERNARY2;
1560 /* pop off the parenthesis */
1561 vec_shrinkby(sy->ops, 1);
1564 compile_error(vec_last(sy->ops).ctx, "invalid parenthesis");
1567 if (!parser_sy_apply_operator(parser, sy))
1573 static void parser_reclassify_token(parser_t *parser)
1576 for (i = 0; i < operator_count; ++i) {
1577 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1578 parser->tok = TOKEN_OPERATOR;
1584 static ast_expression* parse_vararg_do(parser_t *parser)
1586 ast_expression *idx, *out;
1588 ast_value *funtype = parser->function->vtype;
1590 lex_ctx ctx = parser_ctx(parser);
1592 if (!parser_next(parser) || parser->tok != '(') {
1593 parseerror(parser, "expected parameter index and type in parenthesis");
1596 if (!parser_next(parser)) {
1597 parseerror(parser, "error parsing parameter index");
1601 idx = parse_expression_leave(parser, true, false, false);
1605 if (parser->tok != ',') {
1607 parseerror(parser, "expected comma after parameter index");
1611 if (!parser_next(parser) || (parser->tok != TOKEN_IDENT && parser->tok != TOKEN_TYPENAME)) {
1613 parseerror(parser, "expected typename for vararg");
1617 typevar = parse_typename(parser, NULL, NULL);
1623 if (parser->tok != ')') {
1625 ast_delete(typevar);
1626 parseerror(parser, "expected closing paren");
1631 if (!parser_next(parser)) {
1633 ast_delete(typevar);
1634 parseerror(parser, "parse error after vararg");
1639 if (!parser->function->varargs) {
1641 ast_delete(typevar);
1642 parseerror(parser, "function has no variable argument list");
1646 if (funtype->expression.varparam &&
1647 !ast_compare_type((ast_expression*)typevar, (ast_expression*)funtype->expression.varparam))
1651 ast_type_to_string((ast_expression*)typevar, ty1, sizeof(ty1));
1652 ast_type_to_string((ast_expression*)funtype->expression.varparam, ty2, sizeof(ty2));
1653 compile_error(ast_ctx(typevar),
1654 "function was declared to take varargs of type `%s`, requested type is: %s",
1658 out = (ast_expression*)ast_array_index_new(ctx, (ast_expression*)(parser->function->varargs), idx);
1659 ast_type_adopt(out, typevar);
1660 ast_delete(typevar);
1664 static ast_expression* parse_vararg(parser_t *parser)
1666 bool old_noops = parser->lex->flags.noops;
1668 ast_expression *out;
1670 parser->lex->flags.noops = true;
1671 out = parse_vararg_do(parser);
1673 parser->lex->flags.noops = old_noops;
1677 static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
1679 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1680 parser->tok == TOKEN_IDENT &&
1681 !strcmp(parser_tokval(parser), "_"))
1683 /* a translatable string */
1686 parser->lex->flags.noops = true;
1687 if (!parser_next(parser) || parser->tok != '(') {
1688 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1691 parser->lex->flags.noops = false;
1692 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1693 parseerror(parser, "expected a constant string in translatable-string extension");
1696 val = parser_const_string(parser, parser_tokval(parser), true);
1699 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1701 if (!parser_next(parser) || parser->tok != ')') {
1702 parseerror(parser, "expected closing paren after translatable string");
1707 else if (parser->tok == TOKEN_DOTS)
1710 if (!OPTS_FLAG(VARIADIC_ARGS)) {
1711 parseerror(parser, "cannot access varargs (try -fvariadic-args)");
1714 va = parse_vararg(parser);
1717 vec_push(sy->out, syexp(parser_ctx(parser), va));
1720 else if (parser->tok == TOKEN_FLOATCONST) {
1722 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1725 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1728 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1730 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1733 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1736 else if (parser->tok == TOKEN_STRINGCONST) {
1738 val = parser_const_string(parser, parser_tokval(parser), false);
1741 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1744 else if (parser->tok == TOKEN_VECTORCONST) {
1746 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1749 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1752 else if (parser->tok == TOKEN_IDENT)
1754 const char *ctoken = parser_tokval(parser);
1755 ast_expression *prev = vec_size(sy->out) ? vec_last(sy->out).out : NULL;
1756 ast_expression *var;
1757 /* a_vector.{x,y,z} */
1758 if (!vec_size(sy->ops) ||
1759 !vec_last(sy->ops).etype ||
1760 operators[vec_last(sy->ops).etype-1].id != opid1('.') ||
1761 (prev >= intrinsic_debug_typestring &&
1762 prev <= intrinsic_debug_typestring))
1764 /* When adding more intrinsics, fix the above condition */
1767 if (prev && prev->expression.vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
1769 var = (ast_expression*)parser->const_vec[ctoken[0]-'x'];
1771 var = parser_find_var(parser, parser_tokval(parser));
1773 var = parser_find_field(parser, parser_tokval(parser));
1775 if (!var && with_labels) {
1776 var = (ast_expression*)parser_find_label(parser, parser_tokval(parser));
1778 ast_label *lbl = ast_label_new(parser_ctx(parser), parser_tokval(parser), true);
1779 var = (ast_expression*)lbl;
1780 vec_push(parser->labels, lbl);
1785 if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
1786 var = (ast_expression*)intrinsic_debug_typestring;
1790 char *correct = NULL;
1794 * sometimes people use preprocessing predefs without enabling them
1795 * i've done this thousands of times already myself. Lets check for
1796 * it in the predef table. And diagnose it better :)
1798 if (!OPTS_FLAG(FTEPP_PREDEFS)) {
1799 for (i = 0; i < sizeof(ftepp_predefs)/sizeof(*ftepp_predefs); i++) {
1800 if (!strcmp(ftepp_predefs[i].name, parser_tokval(parser))) {
1801 parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
1808 * TODO: determine the best score for the identifier: be it
1809 * a variable, a field.
1811 * We should also consider adding correction tables for
1812 * other things as well.
1814 if (OPTS_OPTION_BOOL(OPTION_CORRECTION)) {
1816 correct_init(&corr);
1818 for (i = 0; i < vec_size(parser->correct_variables); i++) {
1819 correct = correct_str(&corr, parser->correct_variables[i], parser_tokval(parser));
1820 if (strcmp(correct, parser_tokval(parser))) {
1822 } else if (correct) {
1827 correct_free(&corr);
1830 parseerror(parser, "unexpected ident: %s (did you mean %s?)", parser_tokval(parser), correct);
1835 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1841 if (ast_istype(var, ast_value)) {
1842 ((ast_value*)var)->uses++;
1844 else if (ast_istype(var, ast_member)) {
1845 ast_member *mem = (ast_member*)var;
1846 if (ast_istype(mem->owner, ast_value))
1847 ((ast_value*)(mem->owner))->uses++;
1850 vec_push(sy->out, syexp(parser_ctx(parser), var));
1853 parseerror(parser, "unexpected token `%s`", parser_tokval(parser));
1857 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels)
1859 ast_expression *expr = NULL;
1862 bool wantop = false;
1863 /* only warn once about an assignment in a truth value because the current code
1864 * would trigger twice on: if(a = b && ...), once for the if-truth-value, once for the && part
1866 bool warn_truthvalue = true;
1868 /* count the parens because an if starts with one, so the
1869 * end of a condition is an unmatched closing paren
1873 memset(&sy, 0, sizeof(sy));
1875 parser->lex->flags.noops = false;
1877 parser_reclassify_token(parser);
1881 if (parser->tok == TOKEN_TYPENAME) {
1882 parseerror(parser, "unexpected typename");
1886 if (parser->tok == TOKEN_OPERATOR)
1888 /* classify the operator */
1889 const oper_info *op;
1890 const oper_info *olast = NULL;
1892 for (o = 0; o < operator_count; ++o) {
1893 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
1894 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1895 !strcmp(parser_tokval(parser), operators[o].op))
1900 if (o == operator_count) {
1901 compile_error(parser_ctx(parser), "unknown operator: %s", parser_tokval(parser));
1904 /* Are there any expressions which actually end with an operator? */
1905 /* no operator found... must be the end of the statement */
1909 /* found an operator */
1912 /* when declaring variables, a comma starts a new variable */
1913 if (op->id == opid1(',') && !vec_size(sy.paren) && stopatcomma) {
1914 /* fixup the token */
1919 /* a colon without a pervious question mark cannot be a ternary */
1920 if (!ternaries && op->id == opid2(':','?')) {
1925 if (op->id == opid1(',')) {
1926 if (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
1927 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
1931 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
1932 olast = &operators[vec_last(sy.ops).etype-1];
1934 #define IsAssignOp(x) (\
1935 (x) == opid1('=') || \
1936 (x) == opid2('+','=') || \
1937 (x) == opid2('-','=') || \
1938 (x) == opid2('*','=') || \
1939 (x) == opid2('/','=') || \
1940 (x) == opid2('%','=') || \
1941 (x) == opid2('&','=') || \
1942 (x) == opid2('|','=') || \
1943 (x) == opid3('&','~','=') \
1945 if (warn_truthvalue) {
1946 if ( (olast && IsAssignOp(olast->id) && (op->id == opid2('&','&') || op->id == opid2('|','|'))) ||
1947 (olast && IsAssignOp(op->id) && (olast->id == opid2('&','&') || olast->id == opid2('|','|'))) ||
1948 (truthvalue && !vec_size(sy.paren) && IsAssignOp(op->id))
1951 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around assignment used as truth value");
1952 warn_truthvalue = false;
1957 (op->prec < olast->prec) ||
1958 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1960 if (!parser_sy_apply_operator(parser, &sy))
1962 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
1963 olast = &operators[vec_last(sy.ops).etype-1];
1968 if (op->id == opid1('(')) {
1970 size_t sycount = vec_size(sy.out);
1971 /* we expected an operator, this is the function-call operator */
1972 vec_push(sy.paren, PAREN_FUNC);
1973 vec_push(sy.ops, syparen(parser_ctx(parser), sycount-1));
1974 vec_push(sy.argc, 0);
1976 vec_push(sy.paren, PAREN_EXPR);
1977 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1980 } else if (op->id == opid1('[')) {
1982 parseerror(parser, "unexpected array subscript");
1985 vec_push(sy.paren, PAREN_INDEX);
1986 /* push both the operator and the paren, this makes life easier */
1987 vec_push(sy.ops, syop(parser_ctx(parser), op));
1988 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1990 } else if (op->id == opid2('?',':')) {
1991 vec_push(sy.ops, syop(parser_ctx(parser), op));
1992 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
1995 vec_push(sy.paren, PAREN_TERNARY1);
1996 } else if (op->id == opid2(':','?')) {
1997 if (!vec_size(sy.paren)) {
1998 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
2001 if (vec_last(sy.paren) != PAREN_TERNARY1) {
2002 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
2005 if (!parser_close_paren(parser, &sy))
2007 vec_push(sy.ops, syop(parser_ctx(parser), op));
2011 vec_push(sy.ops, syop(parser_ctx(parser), op));
2012 wantop = !!(op->flags & OP_SUFFIX);
2015 else if (parser->tok == ')') {
2016 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
2017 if (!parser_sy_apply_operator(parser, &sy))
2020 if (!vec_size(sy.paren))
2023 if (vec_last(sy.paren) == PAREN_TERNARY1) {
2024 parseerror(parser, "mismatched parentheses (closing paren in ternary expression?)");
2027 if (!parser_close_paren(parser, &sy))
2030 /* must be a function call without parameters */
2031 if (vec_last(sy.paren) != PAREN_FUNC) {
2032 parseerror(parser, "closing paren in invalid position");
2035 if (!parser_close_paren(parser, &sy))
2040 else if (parser->tok == '(') {
2041 parseerror(parser, "internal error: '(' should be classified as operator");
2044 else if (parser->tok == '[') {
2045 parseerror(parser, "internal error: '[' should be classified as operator");
2048 else if (parser->tok == ']') {
2049 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
2050 if (!parser_sy_apply_operator(parser, &sy))
2053 if (!vec_size(sy.paren))
2055 if (vec_last(sy.paren) != PAREN_INDEX) {
2056 parseerror(parser, "mismatched parentheses, unexpected ']'");
2059 if (!parser_close_paren(parser, &sy))
2064 if (!parse_sya_operand(parser, &sy, with_labels))
2067 if (vec_size(sy.paren) && vec_last(sy.ops).isparen && vec_last(sy.paren) == PAREN_FUNC)
2068 vec_last(sy.argc)++;
2073 parseerror(parser, "expected operator or end of statement");
2077 if (!parser_next(parser)) {
2080 if (parser->tok == ';' ||
2081 ((!vec_size(sy.paren) || (vec_size(sy.paren) == 1 && vec_last(sy.paren) == PAREN_TERNARY2)) &&
2082 (parser->tok == ']' || parser->tok == ')' || parser->tok == '}')))
2088 while (vec_size(sy.ops)) {
2089 if (!parser_sy_apply_operator(parser, &sy))
2093 parser->lex->flags.noops = true;
2094 if (!vec_size(sy.out)) {
2095 parseerror(parser, "empty expression");
2098 expr = sy.out[0].out;
2101 if (vec_size(sy.paren)) {
2102 parseerror(parser, "internal error: vec_size(sy.paren) = %lu", (unsigned long)vec_size(sy.paren));
2110 parser->lex->flags.noops = true;
2111 for (i = 0; i < vec_size(sy.out); ++i) {
2113 ast_unref(sy.out[i].out);
2122 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels)
2124 ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
2127 if (parser->tok != ';') {
2128 parseerror(parser, "semicolon expected after expression");
2132 if (!parser_next(parser)) {
2139 static void parser_enterblock(parser_t *parser)
2141 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
2142 vec_push(parser->_blocklocals, vec_size(parser->_locals));
2143 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
2144 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
2145 vec_push(parser->_block_ctx, parser_ctx(parser));
2148 vec_push(parser->correct_variables, correct_trie_new());
2149 vec_push(parser->correct_variables_score, NULL);
2152 static bool parser_leaveblock(parser_t *parser)
2155 size_t locals, typedefs;
2157 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
2158 parseerror(parser, "internal error: parser_leaveblock with no block");
2162 util_htdel(vec_last(parser->variables));
2163 correct_del(vec_last(parser->correct_variables), vec_last(parser->correct_variables_score));
2165 vec_pop(parser->variables);
2166 vec_pop(parser->correct_variables);
2167 vec_pop(parser->correct_variables_score);
2168 if (!vec_size(parser->_blocklocals)) {
2169 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
2173 locals = vec_last(parser->_blocklocals);
2174 vec_pop(parser->_blocklocals);
2175 while (vec_size(parser->_locals) != locals) {
2176 ast_expression *e = vec_last(parser->_locals);
2177 ast_value *v = (ast_value*)e;
2178 vec_pop(parser->_locals);
2179 if (ast_istype(e, ast_value) && !v->uses) {
2180 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
2185 typedefs = vec_last(parser->_blocktypedefs);
2186 while (vec_size(parser->_typedefs) != typedefs) {
2187 ast_delete(vec_last(parser->_typedefs));
2188 vec_pop(parser->_typedefs);
2190 util_htdel(vec_last(parser->typedefs));
2191 vec_pop(parser->typedefs);
2193 vec_pop(parser->_block_ctx);
2198 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
2200 vec_push(parser->_locals, e);
2201 util_htset(vec_last(parser->variables), name, (void*)e);
2205 vec_last(parser->correct_variables),
2206 &vec_last(parser->correct_variables_score),
2211 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
2213 vec_push(parser->globals, e);
2214 util_htset(parser->htglobals, name, e);
2218 parser->correct_variables[0],
2219 &parser->correct_variables_score[0],
2224 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
2228 ast_expression *prev;
2230 if (cond->expression.vtype == TYPE_VOID || cond->expression.vtype >= TYPE_VARIANT) {
2232 ast_type_to_string(cond, ty, sizeof(ty));
2233 compile_error(ast_ctx(cond), "invalid type for if() condition: %s", ty);
2236 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->expression.vtype == TYPE_STRING)
2239 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_S, cond);
2242 parseerror(parser, "internal error: failed to process condition");
2247 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->expression.vtype == TYPE_VECTOR)
2249 /* vector types need to be cast to true booleans */
2250 ast_binary *bin = (ast_binary*)cond;
2251 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->op == INSTR_AND || bin->op == INSTR_OR))
2253 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
2255 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_V, cond);
2258 parseerror(parser, "internal error: failed to process condition");
2265 unary = (ast_unary*)cond;
2266 while (ast_istype(cond, ast_unary) && unary->op == INSTR_NOT_F)
2268 cond = unary->operand;
2269 unary->operand = NULL;
2272 unary = (ast_unary*)cond;
2276 parseerror(parser, "internal error: failed to process condition");
2278 if (ifnot) *_ifnot = !*_ifnot;
2282 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
2285 ast_expression *cond, *ontrue = NULL, *onfalse = NULL;
2288 lex_ctx ctx = parser_ctx(parser);
2290 (void)block; /* not touching */
2292 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2293 if (!parser_next(parser)) {
2294 parseerror(parser, "expected condition or 'not'");
2297 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
2299 if (!parser_next(parser)) {
2300 parseerror(parser, "expected condition in parenthesis");
2304 if (parser->tok != '(') {
2305 parseerror(parser, "expected 'if' condition in parenthesis");
2308 /* parse into the expression */
2309 if (!parser_next(parser)) {
2310 parseerror(parser, "expected 'if' condition after opening paren");
2313 /* parse the condition */
2314 cond = parse_expression_leave(parser, false, true, false);
2318 if (parser->tok != ')') {
2319 parseerror(parser, "expected closing paren after 'if' condition");
2323 /* parse into the 'then' branch */
2324 if (!parser_next(parser)) {
2325 parseerror(parser, "expected statement for on-true branch of 'if'");
2329 if (!parse_statement_or_block(parser, &ontrue)) {
2334 ontrue = (ast_expression*)ast_block_new(parser_ctx(parser));
2335 /* check for an else */
2336 if (!strcmp(parser_tokval(parser), "else")) {
2337 /* parse into the 'else' branch */
2338 if (!parser_next(parser)) {
2339 parseerror(parser, "expected on-false branch after 'else'");
2344 if (!parse_statement_or_block(parser, &onfalse)) {
2351 cond = process_condition(parser, cond, &ifnot);
2353 if (ontrue) ast_delete(ontrue);
2354 if (onfalse) ast_delete(onfalse);
2359 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
2361 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
2362 *out = (ast_expression*)ifthen;
2366 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out);
2367 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
2372 /* skip the 'while' and get the body */
2373 if (!parser_next(parser)) {
2374 if (OPTS_FLAG(LOOP_LABELS))
2375 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2377 parseerror(parser, "expected 'while' condition in parenthesis");
2381 if (parser->tok == ':') {
2382 if (!OPTS_FLAG(LOOP_LABELS))
2383 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2384 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2385 parseerror(parser, "expected loop label");
2388 label = util_strdup(parser_tokval(parser));
2389 if (!parser_next(parser)) {
2391 parseerror(parser, "expected 'while' condition in parenthesis");
2396 if (parser->tok != '(') {
2397 parseerror(parser, "expected 'while' condition in parenthesis");
2401 vec_push(parser->breaks, label);
2402 vec_push(parser->continues, label);
2404 rv = parse_while_go(parser, block, out);
2407 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2408 parseerror(parser, "internal error: label stack corrupted");
2414 vec_pop(parser->breaks);
2415 vec_pop(parser->continues);
2420 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out)
2423 ast_expression *cond, *ontrue;
2427 lex_ctx ctx = parser_ctx(parser);
2429 (void)block; /* not touching */
2431 /* parse into the expression */
2432 if (!parser_next(parser)) {
2433 parseerror(parser, "expected 'while' condition after opening paren");
2436 /* parse the condition */
2437 cond = parse_expression_leave(parser, false, true, false);
2441 if (parser->tok != ')') {
2442 parseerror(parser, "expected closing paren after 'while' condition");
2446 /* parse into the 'then' branch */
2447 if (!parser_next(parser)) {
2448 parseerror(parser, "expected while-loop body");
2452 if (!parse_statement_or_block(parser, &ontrue)) {
2457 cond = process_condition(parser, cond, &ifnot);
2462 aloop = ast_loop_new(ctx, NULL, cond, ifnot, NULL, false, NULL, ontrue);
2463 *out = (ast_expression*)aloop;
2467 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out);
2468 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
2473 /* skip the 'do' and get the body */
2474 if (!parser_next(parser)) {
2475 if (OPTS_FLAG(LOOP_LABELS))
2476 parseerror(parser, "expected loop label or body");
2478 parseerror(parser, "expected loop body");
2482 if (parser->tok == ':') {
2483 if (!OPTS_FLAG(LOOP_LABELS))
2484 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2485 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2486 parseerror(parser, "expected loop label");
2489 label = util_strdup(parser_tokval(parser));
2490 if (!parser_next(parser)) {
2492 parseerror(parser, "expected loop body");
2497 vec_push(parser->breaks, label);
2498 vec_push(parser->continues, label);
2500 rv = parse_dowhile_go(parser, block, out);
2503 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2504 parseerror(parser, "internal error: label stack corrupted");
2510 vec_pop(parser->breaks);
2511 vec_pop(parser->continues);
2516 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out)
2519 ast_expression *cond, *ontrue;
2523 lex_ctx ctx = parser_ctx(parser);
2525 (void)block; /* not touching */
2527 if (!parse_statement_or_block(parser, &ontrue))
2530 /* expect the "while" */
2531 if (parser->tok != TOKEN_KEYWORD ||
2532 strcmp(parser_tokval(parser), "while"))
2534 parseerror(parser, "expected 'while' and condition");
2539 /* skip the 'while' and check for opening paren */
2540 if (!parser_next(parser) || parser->tok != '(') {
2541 parseerror(parser, "expected 'while' condition in parenthesis");
2545 /* parse into the expression */
2546 if (!parser_next(parser)) {
2547 parseerror(parser, "expected 'while' condition after opening paren");
2551 /* parse the condition */
2552 cond = parse_expression_leave(parser, false, true, false);
2556 if (parser->tok != ')') {
2557 parseerror(parser, "expected closing paren after 'while' condition");
2563 if (!parser_next(parser) || parser->tok != ';') {
2564 parseerror(parser, "expected semicolon after condition");
2570 if (!parser_next(parser)) {
2571 parseerror(parser, "parse error");
2577 cond = process_condition(parser, cond, &ifnot);
2582 aloop = ast_loop_new(ctx, NULL, NULL, false, cond, ifnot, NULL, ontrue);
2583 *out = (ast_expression*)aloop;
2587 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
2588 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
2593 /* skip the 'for' and check for opening paren */
2594 if (!parser_next(parser)) {
2595 if (OPTS_FLAG(LOOP_LABELS))
2596 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2598 parseerror(parser, "expected 'for' expressions in parenthesis");
2602 if (parser->tok == ':') {
2603 if (!OPTS_FLAG(LOOP_LABELS))
2604 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2605 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2606 parseerror(parser, "expected loop label");
2609 label = util_strdup(parser_tokval(parser));
2610 if (!parser_next(parser)) {
2612 parseerror(parser, "expected 'for' expressions in parenthesis");
2617 if (parser->tok != '(') {
2618 parseerror(parser, "expected 'for' expressions in parenthesis");
2622 vec_push(parser->breaks, label);
2623 vec_push(parser->continues, label);
2625 rv = parse_for_go(parser, block, out);
2628 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2629 parseerror(parser, "internal error: label stack corrupted");
2635 vec_pop(parser->breaks);
2636 vec_pop(parser->continues);
2640 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
2643 ast_expression *initexpr, *cond, *increment, *ontrue;
2649 lex_ctx ctx = parser_ctx(parser);
2651 parser_enterblock(parser);
2658 /* parse into the expression */
2659 if (!parser_next(parser)) {
2660 parseerror(parser, "expected 'for' initializer after opening paren");
2665 if (parser->tok == TOKEN_IDENT)
2666 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2668 if (typevar || parser->tok == TOKEN_TYPENAME) {
2670 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
2671 if (parsewarning(parser, WARN_EXTENSIONS,
2672 "current standard does not allow variable declarations in for-loop initializers"))
2676 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, NULL))
2679 else if (parser->tok != ';')
2681 initexpr = parse_expression_leave(parser, false, false, false);
2686 /* move on to condition */
2687 if (parser->tok != ';') {
2688 parseerror(parser, "expected semicolon after for-loop initializer");
2691 if (!parser_next(parser)) {
2692 parseerror(parser, "expected for-loop condition");
2696 /* parse the condition */
2697 if (parser->tok != ';') {
2698 cond = parse_expression_leave(parser, false, true, false);
2703 /* move on to incrementor */
2704 if (parser->tok != ';') {
2705 parseerror(parser, "expected semicolon after for-loop initializer");
2708 if (!parser_next(parser)) {
2709 parseerror(parser, "expected for-loop condition");
2713 /* parse the incrementor */
2714 if (parser->tok != ')') {
2715 lex_ctx condctx = parser_ctx(parser);
2716 increment = parse_expression_leave(parser, false, false, false);
2719 if (!ast_side_effects(increment)) {
2720 if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2726 if (parser->tok != ')') {
2727 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2730 /* parse into the 'then' branch */
2731 if (!parser_next(parser)) {
2732 parseerror(parser, "expected for-loop body");
2735 if (!parse_statement_or_block(parser, &ontrue))
2739 cond = process_condition(parser, cond, &ifnot);
2743 aloop = ast_loop_new(ctx, initexpr, cond, ifnot, NULL, false, increment, ontrue);
2744 *out = (ast_expression*)aloop;
2746 if (!parser_leaveblock(parser))
2750 if (initexpr) ast_delete(initexpr);
2751 if (cond) ast_delete(cond);
2752 if (increment) ast_delete(increment);
2753 (void)!parser_leaveblock(parser);
2757 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2759 ast_expression *exp = NULL;
2760 ast_return *ret = NULL;
2761 ast_value *expected = parser->function->vtype;
2763 lex_ctx ctx = parser_ctx(parser);
2765 (void)block; /* not touching */
2767 if (!parser_next(parser)) {
2768 parseerror(parser, "expected return expression");
2772 if (parser->tok != ';') {
2773 exp = parse_expression(parser, false, false);
2777 if (exp->expression.vtype != TYPE_NIL &&
2778 exp->expression.vtype != expected->expression.next->expression.vtype)
2780 parseerror(parser, "return with invalid expression");
2783 ret = ast_return_new(ctx, exp);
2789 if (!parser_next(parser))
2790 parseerror(parser, "parse error");
2791 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2792 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2794 ret = ast_return_new(ctx, NULL);
2796 *out = (ast_expression*)ret;
2800 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2803 unsigned int levels = 0;
2804 lex_ctx ctx = parser_ctx(parser);
2805 const char **loops = (is_continue ? parser->continues : parser->breaks);
2807 (void)block; /* not touching */
2808 if (!parser_next(parser)) {
2809 parseerror(parser, "expected semicolon or loop label");
2813 if (!vec_size(loops)) {
2815 parseerror(parser, "`continue` can only be used inside loops");
2817 parseerror(parser, "`break` can only be used inside loops or switches");
2820 if (parser->tok == TOKEN_IDENT) {
2821 if (!OPTS_FLAG(LOOP_LABELS))
2822 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2823 i = vec_size(loops);
2825 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
2828 parseerror(parser, "no such loop to %s: `%s`",
2829 (is_continue ? "continue" : "break out of"),
2830 parser_tokval(parser));
2835 if (!parser_next(parser)) {
2836 parseerror(parser, "expected semicolon");
2841 if (parser->tok != ';') {
2842 parseerror(parser, "expected semicolon");
2846 if (!parser_next(parser))
2847 parseerror(parser, "parse error");
2849 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
2853 /* returns true when it was a variable qualifier, false otherwise!
2854 * on error, cvq is set to CV_WRONG
2856 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
2858 bool had_const = false;
2859 bool had_var = false;
2860 bool had_noref = false;
2861 bool had_attrib = false;
2862 bool had_static = false;
2867 if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
2869 /* parse an attribute */
2870 if (!parser_next(parser)) {
2871 parseerror(parser, "expected attribute after `[[`");
2875 if (!strcmp(parser_tokval(parser), "noreturn")) {
2876 flags |= AST_FLAG_NORETURN;
2877 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2878 parseerror(parser, "`noreturn` attribute has no parameters, expected `]]`");
2883 else if (!strcmp(parser_tokval(parser), "noref")) {
2885 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2886 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2891 else if (!strcmp(parser_tokval(parser), "inline")) {
2892 flags |= AST_FLAG_INLINE;
2893 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2894 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2901 else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
2902 flags |= AST_FLAG_DEPRECATED;
2905 if (!parser_next(parser)) {
2906 parseerror(parser, "parse error in attribute");
2910 if (parser->tok == '(') {
2911 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
2912 parseerror(parser, "`deprecated` attribute missing parameter");
2916 *message = util_strdup(parser_tokval(parser));
2918 if (!parser_next(parser)) {
2919 parseerror(parser, "parse error in attribute");
2923 if(parser->tok != ')') {
2924 parseerror(parser, "`deprecated` attribute expected `)` after parameter");
2928 if (!parser_next(parser)) {
2929 parseerror(parser, "parse error in attribute");
2934 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2935 parseerror(parser, "`deprecated` attribute expected `]]`");
2938 if (*message) mem_d(*message);
2946 /* Skip tokens until we hit a ]] */
2947 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
2948 while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2949 if (!parser_next(parser)) {
2950 parseerror(parser, "error inside attribute");
2957 else if (with_local && !strcmp(parser_tokval(parser), "static"))
2959 else if (!strcmp(parser_tokval(parser), "const"))
2961 else if (!strcmp(parser_tokval(parser), "var"))
2963 else if (with_local && !strcmp(parser_tokval(parser), "local"))
2965 else if (!strcmp(parser_tokval(parser), "noref"))
2967 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
2972 if (!parser_next(parser))
2982 *is_static = had_static;
2986 parseerror(parser, "parse error after variable qualifier");
2991 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
2992 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2997 /* skip the 'while' and get the body */
2998 if (!parser_next(parser)) {
2999 if (OPTS_FLAG(LOOP_LABELS))
3000 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
3002 parseerror(parser, "expected 'switch' operand in parenthesis");
3006 if (parser->tok == ':') {
3007 if (!OPTS_FLAG(LOOP_LABELS))
3008 parseerror(parser, "labeled loops not activated, try using -floop-labels");
3009 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
3010 parseerror(parser, "expected loop label");
3013 label = util_strdup(parser_tokval(parser));
3014 if (!parser_next(parser)) {
3016 parseerror(parser, "expected 'switch' operand in parenthesis");
3021 if (parser->tok != '(') {
3022 parseerror(parser, "expected 'switch' operand in parenthesis");
3026 vec_push(parser->breaks, label);
3028 rv = parse_switch_go(parser, block, out);
3031 if (vec_last(parser->breaks) != label) {
3032 parseerror(parser, "internal error: label stack corrupted");
3038 vec_pop(parser->breaks);
3043 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
3045 ast_expression *operand;
3048 ast_switch *switchnode;
3049 ast_switch_case swcase;
3052 bool noref, is_static;
3053 uint32_t qflags = 0;
3055 lex_ctx ctx = parser_ctx(parser);
3057 (void)block; /* not touching */
3060 /* parse into the expression */
3061 if (!parser_next(parser)) {
3062 parseerror(parser, "expected switch operand");
3065 /* parse the operand */
3066 operand = parse_expression_leave(parser, false, false, false);
3070 switchnode = ast_switch_new(ctx, operand);
3073 if (parser->tok != ')') {
3074 ast_delete(switchnode);
3075 parseerror(parser, "expected closing paren after 'switch' operand");
3079 /* parse over the opening paren */
3080 if (!parser_next(parser) || parser->tok != '{') {
3081 ast_delete(switchnode);
3082 parseerror(parser, "expected list of cases");
3086 if (!parser_next(parser)) {
3087 ast_delete(switchnode);
3088 parseerror(parser, "expected 'case' or 'default'");
3092 /* new block; allow some variables to be declared here */
3093 parser_enterblock(parser);
3096 if (parser->tok == TOKEN_IDENT)
3097 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3098 if (typevar || parser->tok == TOKEN_TYPENAME) {
3099 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, NULL)) {
3100 ast_delete(switchnode);
3105 if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, NULL))
3107 if (cvq == CV_WRONG) {
3108 ast_delete(switchnode);
3111 if (!parse_variable(parser, block, false, cvq, NULL, noref, is_static, qflags, NULL)) {
3112 ast_delete(switchnode);
3121 while (parser->tok != '}') {
3122 ast_block *caseblock;
3124 if (!strcmp(parser_tokval(parser), "case")) {
3125 if (!parser_next(parser)) {
3126 ast_delete(switchnode);
3127 parseerror(parser, "expected expression for case");
3130 swcase.value = parse_expression_leave(parser, false, false, false);
3131 if (!swcase.value) {
3132 ast_delete(switchnode);
3133 parseerror(parser, "expected expression for case");
3136 if (!OPTS_FLAG(RELAXED_SWITCH)) {
3137 if (!ast_istype(swcase.value, ast_value)) { /* || ((ast_value*)swcase.value)->cvq != CV_CONST) { */
3138 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
3144 else if (!strcmp(parser_tokval(parser), "default")) {
3145 swcase.value = NULL;
3146 if (!parser_next(parser)) {
3147 ast_delete(switchnode);
3148 parseerror(parser, "expected colon");
3153 ast_delete(switchnode);
3154 parseerror(parser, "expected 'case' or 'default'");
3158 /* Now the colon and body */
3159 if (parser->tok != ':') {
3160 if (swcase.value) ast_unref(swcase.value);
3161 ast_delete(switchnode);
3162 parseerror(parser, "expected colon");
3166 if (!parser_next(parser)) {
3167 if (swcase.value) ast_unref(swcase.value);
3168 ast_delete(switchnode);
3169 parseerror(parser, "expected statements or case");
3172 caseblock = ast_block_new(parser_ctx(parser));
3174 if (swcase.value) ast_unref(swcase.value);
3175 ast_delete(switchnode);
3178 swcase.code = (ast_expression*)caseblock;
3179 vec_push(switchnode->cases, swcase);
3181 ast_expression *expr;
3182 if (parser->tok == '}')
3184 if (parser->tok == TOKEN_KEYWORD) {
3185 if (!strcmp(parser_tokval(parser), "case") ||
3186 !strcmp(parser_tokval(parser), "default"))
3191 if (!parse_statement(parser, caseblock, &expr, true)) {
3192 ast_delete(switchnode);
3197 if (!ast_block_add_expr(caseblock, expr)) {
3198 ast_delete(switchnode);
3204 parser_leaveblock(parser);
3207 if (parser->tok != '}') {
3208 ast_delete(switchnode);
3209 parseerror(parser, "expected closing paren of case list");
3212 if (!parser_next(parser)) {
3213 ast_delete(switchnode);
3214 parseerror(parser, "parse error after switch");
3217 *out = (ast_expression*)switchnode;
3221 /* parse computed goto sides */
3222 static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
3223 ast_expression *on_true;
3224 ast_expression *on_false;
3225 ast_expression *cond;
3230 if (ast_istype(*side, ast_ternary)) {
3231 ast_ternary *tern = (ast_ternary*)*side;
3232 on_true = parse_goto_computed(parser, &tern->on_true);
3233 on_false = parse_goto_computed(parser, &tern->on_false);
3235 if (!on_true || !on_false) {
3236 parseerror(parser, "expected label or expression in ternary");
3237 if (on_true) ast_unref(on_true);
3238 if (on_false) ast_unref(on_false);
3246 return (ast_expression*)ast_ifthen_new(parser_ctx(parser), cond, on_true, on_false);
3247 } else if (ast_istype(*side, ast_label)) {
3248 ast_goto *gt = ast_goto_new(parser_ctx(parser), ((ast_label*)*side)->name);
3249 ast_goto_set_label(gt, ((ast_label*)*side));
3251 return (ast_expression*)gt;
3256 static bool parse_goto(parser_t *parser, ast_expression **out)
3258 ast_goto *gt = NULL;
3259 ast_expression *lbl;
3261 if (!parser_next(parser))
3264 if (parser->tok != TOKEN_IDENT) {
3265 ast_expression *expression;
3267 /* could be an expression i.e computed goto :-) */
3268 if (parser->tok != '(') {
3269 parseerror(parser, "expected label name after `goto`");
3273 /* failed to parse expression for goto */
3274 if (!(expression = parse_expression(parser, false, true)) ||
3275 !(*out = parse_goto_computed(parser, &expression))) {
3276 parseerror(parser, "invalid goto expression");
3277 ast_unref(expression);
3284 /* not computed goto */
3285 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
3286 lbl = parser_find_label(parser, gt->name);
3288 if (!ast_istype(lbl, ast_label)) {
3289 parseerror(parser, "internal error: label is not an ast_label");
3293 ast_goto_set_label(gt, (ast_label*)lbl);
3296 vec_push(parser->gotos, gt);
3298 if (!parser_next(parser) || parser->tok != ';') {
3299 parseerror(parser, "semicolon expected after goto label");
3302 if (!parser_next(parser)) {
3303 parseerror(parser, "parse error after goto");
3307 *out = (ast_expression*)gt;
3311 static bool parse_skipwhite(parser_t *parser)
3314 if (!parser_next(parser))
3316 } while (parser->tok == TOKEN_WHITE && parser->tok < TOKEN_ERROR);
3317 return parser->tok < TOKEN_ERROR;
3320 static bool parse_eol(parser_t *parser)
3322 if (!parse_skipwhite(parser))
3324 return parser->tok == TOKEN_EOL;
3327 static bool parse_pragma_do(parser_t *parser)
3329 if (!parser_next(parser) ||
3330 parser->tok != TOKEN_IDENT ||
3331 strcmp(parser_tokval(parser), "pragma"))
3333 parseerror(parser, "expected `pragma` keyword after `#`, got `%s`", parser_tokval(parser));
3336 if (!parse_skipwhite(parser) || parser->tok != TOKEN_IDENT) {
3337 parseerror(parser, "expected pragma, got `%s`", parser_tokval(parser));
3341 if (!strcmp(parser_tokval(parser), "noref")) {
3342 if (!parse_skipwhite(parser) || parser->tok != TOKEN_INTCONST) {
3343 parseerror(parser, "`noref` pragma requires an argument: 0 or 1");
3346 parser->noref = !!parser_token(parser)->constval.i;
3347 if (!parse_eol(parser)) {
3348 parseerror(parser, "parse error after `noref` pragma");
3354 (void)!parsewarning(parser, WARN_UNKNOWN_PRAGMAS, "ignoring #pragma %s", parser_tokval(parser));
3361 static bool parse_pragma(parser_t *parser)
3364 parser->lex->flags.preprocessing = true;
3365 parser->lex->flags.mergelines = true;
3366 rv = parse_pragma_do(parser);
3367 if (parser->tok != TOKEN_EOL) {
3368 parseerror(parser, "junk after pragma");
3371 parser->lex->flags.preprocessing = false;
3372 parser->lex->flags.mergelines = false;
3373 if (!parser_next(parser)) {
3374 parseerror(parser, "parse error after pragma");
3380 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
3382 bool noref, is_static;
3384 uint32_t qflags = 0;
3385 ast_value *typevar = NULL;
3386 char *vstring = NULL;
3390 if (parser->tok == TOKEN_IDENT)
3391 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3393 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3395 /* local variable */
3397 parseerror(parser, "cannot declare a variable from here");
3400 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3401 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
3404 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, NULL))
3408 else if (parse_qualifiers(parser, !!block, &cvq, &noref, &is_static, &qflags, &vstring))
3410 if (cvq == CV_WRONG)
3412 return parse_variable(parser, block, true, cvq, NULL, noref, is_static, qflags, vstring);
3414 else if (parser->tok == TOKEN_KEYWORD)
3416 if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
3421 if (!parser_next(parser)) {
3422 parseerror(parser, "parse error after __builtin_debug_printtype");
3426 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
3428 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
3429 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
3430 if (!parser_next(parser)) {
3431 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
3437 if (!parse_statement(parser, block, out, allow_cases))
3440 con_out("__builtin_debug_printtype: got no output node\n");
3443 ast_type_to_string(*out, ty, sizeof(ty));
3444 con_out("__builtin_debug_printtype: `%s`\n", ty);
3449 else if (!strcmp(parser_tokval(parser), "return"))
3451 return parse_return(parser, block, out);
3453 else if (!strcmp(parser_tokval(parser), "if"))
3455 return parse_if(parser, block, out);
3457 else if (!strcmp(parser_tokval(parser), "while"))
3459 return parse_while(parser, block, out);
3461 else if (!strcmp(parser_tokval(parser), "do"))
3463 return parse_dowhile(parser, block, out);
3465 else if (!strcmp(parser_tokval(parser), "for"))
3467 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3468 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
3471 return parse_for(parser, block, out);
3473 else if (!strcmp(parser_tokval(parser), "break"))
3475 return parse_break_continue(parser, block, out, false);
3477 else if (!strcmp(parser_tokval(parser), "continue"))
3479 return parse_break_continue(parser, block, out, true);
3481 else if (!strcmp(parser_tokval(parser), "switch"))
3483 return parse_switch(parser, block, out);
3485 else if (!strcmp(parser_tokval(parser), "case") ||
3486 !strcmp(parser_tokval(parser), "default"))
3489 parseerror(parser, "unexpected 'case' label");
3494 else if (!strcmp(parser_tokval(parser), "goto"))
3496 return parse_goto(parser, out);
3498 else if (!strcmp(parser_tokval(parser), "typedef"))
3500 if (!parser_next(parser)) {
3501 parseerror(parser, "expected type definition after 'typedef'");
3504 return parse_typedef(parser);
3506 parseerror(parser, "Unexpected keyword");
3509 else if (parser->tok == '{')
3512 inner = parse_block(parser);
3515 *out = (ast_expression*)inner;
3518 else if (parser->tok == ':')
3522 if (!parser_next(parser)) {
3523 parseerror(parser, "expected label name");
3526 if (parser->tok != TOKEN_IDENT) {
3527 parseerror(parser, "label must be an identifier");
3530 label = (ast_label*)parser_find_label(parser, parser_tokval(parser));
3532 if (!label->undefined) {
3533 parseerror(parser, "label `%s` already defined", label->name);
3536 label->undefined = false;
3539 label = ast_label_new(parser_ctx(parser), parser_tokval(parser), false);
3540 vec_push(parser->labels, label);
3542 *out = (ast_expression*)label;
3543 if (!parser_next(parser)) {
3544 parseerror(parser, "parse error after label");
3547 for (i = 0; i < vec_size(parser->gotos); ++i) {
3548 if (!strcmp(parser->gotos[i]->name, label->name)) {
3549 ast_goto_set_label(parser->gotos[i], label);
3550 vec_remove(parser->gotos, i, 1);
3556 else if (parser->tok == ';')
3558 if (!parser_next(parser)) {
3559 parseerror(parser, "parse error after empty statement");
3566 lex_ctx ctx = parser_ctx(parser);
3567 ast_expression *exp = parse_expression(parser, false, false);
3571 if (!ast_side_effects(exp)) {
3572 if (compile_warning(ctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
3579 static bool parse_enum(parser_t *parser)
3582 ast_value **values = NULL;
3583 ast_value *var = NULL;
3586 ast_expression *old;
3588 if (!parser_next(parser) || parser->tok != '{') {
3589 parseerror(parser, "expected `{` after `enum` keyword");
3594 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
3595 if (parser->tok == '}') {
3596 /* allow an empty enum */
3599 parseerror(parser, "expected identifier or `}`");
3603 old = parser_find_field(parser, parser_tokval(parser));
3605 old = parser_find_global(parser, parser_tokval(parser));
3607 parseerror(parser, "value `%s` has already been declared here: %s:%i",
3608 parser_tokval(parser), ast_ctx(old).file, ast_ctx(old).line);
3612 var = ast_value_new(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
3613 vec_push(values, var);
3614 var->cvq = CV_CONST;
3615 var->hasvalue = true;
3616 var->constval.vfloat = num++;
3618 parser_addglobal(parser, var->name, (ast_expression*)var);
3620 if (!parser_next(parser)) {
3621 parseerror(parser, "expected `=`, `}` or comma after identifier");
3625 if (parser->tok == ',')
3627 if (parser->tok == '}')
3629 if (parser->tok != '=') {
3630 parseerror(parser, "expected `=`, `}` or comma after identifier");
3634 if (!parser_next(parser)) {
3635 parseerror(parser, "expected expression after `=`");
3639 /* We got a value! */
3640 old = parse_expression_leave(parser, true, false, false);
3641 asvalue = (ast_value*)old;
3642 if (!ast_istype(old, ast_value) || asvalue->cvq != CV_CONST || !asvalue->hasvalue) {
3643 compile_error(ast_ctx(var), "constant value or expression expected");
3646 num = (var->constval.vfloat = asvalue->constval.vfloat) + 1;
3648 if (parser->tok == '}')
3650 if (parser->tok != ',') {
3651 parseerror(parser, "expected `}` or comma after expression");
3656 if (parser->tok != '}') {
3657 parseerror(parser, "internal error: breaking without `}`");
3661 if (!parser_next(parser) || parser->tok != ';') {
3662 parseerror(parser, "expected semicolon after enumeration");
3666 if (!parser_next(parser)) {
3667 parseerror(parser, "parse error after enumeration");
3679 static bool parse_block_into(parser_t *parser, ast_block *block)
3683 parser_enterblock(parser);
3685 if (!parser_next(parser)) { /* skip the '{' */
3686 parseerror(parser, "expected function body");
3690 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3692 ast_expression *expr = NULL;
3693 if (parser->tok == '}')
3696 if (!parse_statement(parser, block, &expr, false)) {
3697 /* parseerror(parser, "parse error"); */
3703 if (!ast_block_add_expr(block, expr)) {
3710 if (parser->tok != '}') {
3713 (void)parser_next(parser);
3717 if (!parser_leaveblock(parser))
3719 return retval && !!block;
3722 static ast_block* parse_block(parser_t *parser)
3725 block = ast_block_new(parser_ctx(parser));
3728 if (!parse_block_into(parser, block)) {
3729 ast_block_delete(block);
3735 static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
3737 if (parser->tok == '{') {
3738 *out = (ast_expression*)parse_block(parser);
3741 return parse_statement(parser, NULL, out, false);
3744 static bool create_vector_members(ast_value *var, ast_member **me)
3747 size_t len = strlen(var->name);
3749 for (i = 0; i < 3; ++i) {
3750 char *name = (char*)mem_a(len+3);
3751 memcpy(name, var->name, len);
3753 name[len+1] = 'x'+i;
3755 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
3764 do { ast_member_delete(me[--i]); } while(i);
3768 static bool parse_function_body(parser_t *parser, ast_value *var)
3770 ast_block *block = NULL;
3775 ast_expression *framenum = NULL;
3776 ast_expression *nextthink = NULL;
3777 /* None of the following have to be deleted */
3778 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
3779 ast_expression *gbl_time = NULL, *gbl_self = NULL;
3780 bool has_frame_think;
3784 has_frame_think = false;
3785 old = parser->function;
3787 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
3788 parseerror(parser, "gotos/labels leaking");
3792 if (!OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC) {
3793 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
3794 "variadic function with implementation will not be able to access additional parameters (try -fvariadic-args)"))
3800 if (parser->tok == '[') {
3801 /* got a frame definition: [ framenum, nextthink ]
3802 * this translates to:
3803 * self.frame = framenum;
3804 * self.nextthink = time + 0.1;
3805 * self.think = nextthink;
3809 fld_think = parser_find_field(parser, "think");
3810 fld_nextthink = parser_find_field(parser, "nextthink");
3811 fld_frame = parser_find_field(parser, "frame");
3812 if (!fld_think || !fld_nextthink || !fld_frame) {
3813 parseerror(parser, "cannot use [frame,think] notation without the required fields");
3814 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
3817 gbl_time = parser_find_global(parser, "time");
3818 gbl_self = parser_find_global(parser, "self");
3819 if (!gbl_time || !gbl_self) {
3820 parseerror(parser, "cannot use [frame,think] notation without the required globals");
3821 parseerror(parser, "please declare the following globals: `time`, `self`");
3825 if (!parser_next(parser))
3828 framenum = parse_expression_leave(parser, true, false, false);
3830 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
3833 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
3834 ast_unref(framenum);
3835 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
3839 if (parser->tok != ',') {
3840 ast_unref(framenum);
3841 parseerror(parser, "expected comma after frame number in [frame,think] notation");
3842 parseerror(parser, "Got a %i\n", parser->tok);
3846 if (!parser_next(parser)) {
3847 ast_unref(framenum);
3851 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
3853 /* qc allows the use of not-yet-declared functions here
3854 * - this automatically creates a prototype */
3855 ast_value *thinkfunc;
3856 ast_expression *functype = fld_think->expression.next;
3858 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
3859 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
3860 ast_unref(framenum);
3861 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
3865 if (!parser_next(parser)) {
3866 ast_unref(framenum);
3867 ast_delete(thinkfunc);
3871 parser_addglobal(parser, thinkfunc->name, (ast_expression*)thinkfunc);
3873 nextthink = (ast_expression*)thinkfunc;
3876 nextthink = parse_expression_leave(parser, true, false, false);
3878 ast_unref(framenum);
3879 parseerror(parser, "expected a think-function in [frame,think] notation");
3884 if (!ast_istype(nextthink, ast_value)) {
3885 parseerror(parser, "think-function in [frame,think] notation must be a constant");
3889 if (retval && parser->tok != ']') {
3890 parseerror(parser, "expected closing `]` for [frame,think] notation");
3894 if (retval && !parser_next(parser)) {
3898 if (retval && parser->tok != '{') {
3899 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
3904 ast_unref(nextthink);
3905 ast_unref(framenum);
3909 has_frame_think = true;
3912 block = ast_block_new(parser_ctx(parser));
3914 parseerror(parser, "failed to allocate block");
3915 if (has_frame_think) {
3916 ast_unref(nextthink);
3917 ast_unref(framenum);
3922 if (has_frame_think) {
3924 ast_expression *self_frame;
3925 ast_expression *self_nextthink;
3926 ast_expression *self_think;
3927 ast_expression *time_plus_1;
3928 ast_store *store_frame;
3929 ast_store *store_nextthink;
3930 ast_store *store_think;
3932 ctx = parser_ctx(parser);
3933 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
3934 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
3935 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
3937 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
3938 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
3940 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
3941 if (self_frame) ast_delete(self_frame);
3942 if (self_nextthink) ast_delete(self_nextthink);
3943 if (self_think) ast_delete(self_think);
3944 if (time_plus_1) ast_delete(time_plus_1);
3950 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
3951 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
3952 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
3955 ast_delete(self_frame);
3958 if (!store_nextthink) {
3959 ast_delete(self_nextthink);
3963 ast_delete(self_think);
3967 if (store_frame) ast_delete(store_frame);
3968 if (store_nextthink) ast_delete(store_nextthink);
3969 if (store_think) ast_delete(store_think);
3972 if (!ast_block_add_expr(block, (ast_expression*)store_frame) ||
3973 !ast_block_add_expr(block, (ast_expression*)store_nextthink) ||
3974 !ast_block_add_expr(block, (ast_expression*)store_think))
3981 parseerror(parser, "failed to generate code for [frame,think]");
3982 ast_unref(nextthink);
3983 ast_unref(framenum);
3989 if (var->hasvalue) {
3990 parseerror(parser, "function `%s` declared with multiple bodies", var->name);
3991 ast_block_delete(block);
3995 func = ast_function_new(ast_ctx(var), var->name, var);
3997 parseerror(parser, "failed to allocate function for `%s`", var->name);
3998 ast_block_delete(block);
4001 vec_push(parser->functions, func);
4003 parser_enterblock(parser);
4005 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
4007 ast_value *param = var->expression.params[parami];
4010 if (param->expression.vtype != TYPE_VECTOR &&
4011 (param->expression.vtype != TYPE_FIELD ||
4012 param->expression.next->expression.vtype != TYPE_VECTOR))
4017 if (!create_vector_members(param, me)) {
4018 ast_block_delete(block);
4022 for (e = 0; e < 3; ++e) {
4023 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
4024 ast_block_collect(block, (ast_expression*)me[e]);
4028 if (var->argcounter) {
4029 ast_value *argc = ast_value_new(ast_ctx(var), var->argcounter, TYPE_FLOAT);
4030 parser_addlocal(parser, argc->name, (ast_expression*)argc);
4034 if (OPTS_FLAG(VARIADIC_ARGS) && var->expression.flags & AST_FLAG_VARIADIC) {
4036 ast_value *varargs = ast_value_new(ast_ctx(var), "reserved:va_args", TYPE_ARRAY);
4037 varargs->expression.flags |= AST_FLAG_IS_VARARG;
4038 varargs->expression.next = (ast_expression*)ast_value_new(ast_ctx(var), NULL, TYPE_VECTOR);
4039 varargs->expression.count = 0;
4040 snprintf(name, sizeof(name), "%s##va##SET", var->name);
4041 if (!parser_create_array_setter_proto(parser, varargs, name)) {
4042 ast_delete(varargs);
4043 ast_block_delete(block);
4046 snprintf(name, sizeof(name), "%s##va##GET", var->name);
4047 if (!parser_create_array_getter_proto(parser, varargs, varargs->expression.next, name)) {
4048 ast_delete(varargs);
4049 ast_block_delete(block);
4052 func->varargs = varargs;
4054 func->fixedparams = parser_const_float(parser, vec_size(var->expression.params));
4057 parser->function = func;
4058 if (!parse_block_into(parser, block)) {
4059 ast_block_delete(block);
4063 vec_push(func->blocks, block);
4065 parser->function = old;
4066 if (!parser_leaveblock(parser))
4068 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
4069 parseerror(parser, "internal error: local scopes left");
4073 if (parser->tok == ';')
4074 return parser_next(parser);
4075 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4076 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
4080 (void)!parser_leaveblock(parser);
4081 vec_pop(parser->functions);
4082 ast_function_delete(func);
4083 var->constval.vfunc = NULL;
4086 parser->function = old;
4090 static ast_expression *array_accessor_split(
4095 ast_expression *left,
4096 ast_expression *right
4102 lex_ctx ctx = ast_ctx(array);
4104 if (!left || !right) {
4105 if (left) ast_delete(left);
4106 if (right) ast_delete(right);
4110 cmp = ast_binary_new(ctx, INSTR_LT,
4111 (ast_expression*)index,
4112 (ast_expression*)parser_const_float(parser, middle));
4116 parseerror(parser, "internal error: failed to create comparison for array setter");
4120 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
4122 ast_delete(cmp); /* will delete left and right */
4123 parseerror(parser, "internal error: failed to create conditional jump for array setter");
4127 return (ast_expression*)ifthen;
4130 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
4132 lex_ctx ctx = ast_ctx(array);
4134 if (from+1 == afterend) {
4135 /* set this value */
4138 ast_array_index *subscript;
4140 int assignop = type_store_instr[value->expression.vtype];
4142 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
4143 assignop = INSTR_STORE_V;
4145 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
4149 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
4151 ast_delete(subscript);
4155 block = ast_block_new(ctx);
4161 if (!ast_block_add_expr(block, (ast_expression*)st)) {
4166 ret = ast_return_new(ctx, NULL);
4172 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
4177 return (ast_expression*)block;
4179 ast_expression *left, *right;
4180 size_t diff = afterend - from;
4181 size_t middle = from + diff/2;
4182 left = array_setter_node(parser, array, index, value, from, middle);
4183 right = array_setter_node(parser, array, index, value, middle, afterend);
4184 return array_accessor_split(parser, array, index, middle, left, right);
4188 static ast_expression *array_field_setter_node(
4197 lex_ctx ctx = ast_ctx(array);
4199 if (from+1 == afterend) {
4200 /* set this value */
4203 ast_entfield *entfield;
4204 ast_array_index *subscript;
4206 int assignop = type_storep_instr[value->expression.vtype];
4208 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
4209 assignop = INSTR_STOREP_V;
4211 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
4215 subscript->expression.next = ast_type_copy(ast_ctx(subscript), (ast_expression*)subscript);
4216 subscript->expression.vtype = TYPE_FIELD;
4218 entfield = ast_entfield_new_force(ctx,
4219 (ast_expression*)entity,
4220 (ast_expression*)subscript,
4221 (ast_expression*)subscript);
4223 ast_delete(subscript);
4227 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
4229 ast_delete(entfield);
4233 block = ast_block_new(ctx);
4239 if (!ast_block_add_expr(block, (ast_expression*)st)) {
4244 ret = ast_return_new(ctx, NULL);
4250 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
4255 return (ast_expression*)block;
4257 ast_expression *left, *right;
4258 size_t diff = afterend - from;
4259 size_t middle = from + diff/2;
4260 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
4261 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
4262 return array_accessor_split(parser, array, index, middle, left, right);
4266 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
4268 lex_ctx ctx = ast_ctx(array);
4270 if (from+1 == afterend) {
4272 ast_array_index *subscript;
4274 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
4278 ret = ast_return_new(ctx, (ast_expression*)subscript);
4280 ast_delete(subscript);
4284 return (ast_expression*)ret;
4286 ast_expression *left, *right;
4287 size_t diff = afterend - from;
4288 size_t middle = from + diff/2;
4289 left = array_getter_node(parser, array, index, from, middle);
4290 right = array_getter_node(parser, array, index, middle, afterend);
4291 return array_accessor_split(parser, array, index, middle, left, right);
4295 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
4297 ast_function *func = NULL;
4298 ast_value *fval = NULL;
4299 ast_block *body = NULL;
4301 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
4303 parseerror(parser, "failed to create accessor function value");
4307 func = ast_function_new(ast_ctx(array), funcname, fval);
4310 parseerror(parser, "failed to create accessor function node");
4314 body = ast_block_new(ast_ctx(array));
4316 parseerror(parser, "failed to create block for array accessor");
4322 vec_push(func->blocks, body);
4325 vec_push(parser->accessors, fval);
4330 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname)
4332 ast_value *index = NULL;
4333 ast_value *value = NULL;
4337 if (!ast_istype(array->expression.next, ast_value)) {
4338 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4342 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4344 func = fval->constval.vfunc;
4345 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
4347 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4348 value = ast_value_copy((ast_value*)array->expression.next);
4350 if (!index || !value) {
4351 parseerror(parser, "failed to create locals for array accessor");
4354 (void)!ast_value_set_name(value, "value"); /* not important */
4355 vec_push(fval->expression.params, index);
4356 vec_push(fval->expression.params, value);
4358 array->setter = fval;
4361 if (index) ast_delete(index);
4362 if (value) ast_delete(value);
4368 static bool parser_create_array_setter_impl(parser_t *parser, ast_value *array)
4370 ast_expression *root = NULL;
4371 root = array_setter_node(parser, array,
4372 array->setter->expression.params[0],
4373 array->setter->expression.params[1],
4374 0, array->expression.count);
4376 parseerror(parser, "failed to build accessor search tree");
4379 if (!ast_block_add_expr(array->setter->constval.vfunc->blocks[0], root)) {
4386 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
4388 if (!parser_create_array_setter_proto(parser, array, funcname))
4390 return parser_create_array_setter_impl(parser, array);
4393 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
4395 ast_expression *root = NULL;
4396 ast_value *entity = NULL;
4397 ast_value *index = NULL;
4398 ast_value *value = NULL;
4402 if (!ast_istype(array->expression.next, ast_value)) {
4403 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4407 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4409 func = fval->constval.vfunc;
4410 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
4412 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
4413 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4414 value = ast_value_copy((ast_value*)array->expression.next);
4415 if (!entity || !index || !value) {
4416 parseerror(parser, "failed to create locals for array accessor");
4419 (void)!ast_value_set_name(value, "value"); /* not important */
4420 vec_push(fval->expression.params, entity);
4421 vec_push(fval->expression.params, index);
4422 vec_push(fval->expression.params, value);
4424 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
4426 parseerror(parser, "failed to build accessor search tree");
4430 array->setter = fval;
4431 return ast_block_add_expr(func->blocks[0], root);
4433 if (entity) ast_delete(entity);
4434 if (index) ast_delete(index);
4435 if (value) ast_delete(value);
4436 if (root) ast_delete(root);
4442 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4444 ast_value *index = NULL;
4448 /* NOTE: checking array->expression.next rather than elemtype since
4449 * for fields elemtype is a temporary fieldtype.
4451 if (!ast_istype(array->expression.next, ast_value)) {
4452 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4456 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4458 func = fval->constval.vfunc;
4459 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
4461 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
4464 parseerror(parser, "failed to create locals for array accessor");
4467 vec_push(fval->expression.params, index);
4469 array->getter = fval;
4472 if (index) ast_delete(index);
4478 static bool parser_create_array_getter_impl(parser_t *parser, ast_value *array)
4480 ast_expression *root = NULL;
4482 root = array_getter_node(parser, array, array->getter->expression.params[0], 0, array->expression.count);
4484 parseerror(parser, "failed to build accessor search tree");
4487 if (!ast_block_add_expr(array->getter->constval.vfunc->blocks[0], root)) {
4494 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4496 if (!parser_create_array_getter_proto(parser, array, elemtype, funcname))
4498 return parser_create_array_getter_impl(parser, array);
4501 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
4502 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
4510 bool variadic = false;
4511 ast_value *varparam = NULL;
4512 char *argcounter = NULL;
4514 ctx = parser_ctx(parser);
4516 /* for the sake of less code we parse-in in this function */
4517 if (!parser_next(parser)) {
4518 parseerror(parser, "expected parameter list");
4524 /* parse variables until we hit a closing paren */
4525 while (parser->tok != ')') {
4527 /* there must be commas between them */
4528 if (parser->tok != ',') {
4529 parseerror(parser, "expected comma or end of parameter list");
4532 if (!parser_next(parser)) {
4533 parseerror(parser, "expected parameter");
4539 if (parser->tok == TOKEN_DOTS) {
4540 /* '...' indicates a varargs function */
4542 if (!parser_next(parser) || (parser->tok != ')' && parser->tok != TOKEN_IDENT)) {
4543 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4546 if (parser->tok == TOKEN_IDENT) {
4547 argcounter = util_strdup(parser_tokval(parser));
4548 if (!parser_next(parser) || parser->tok != ')') {
4549 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4556 /* for anything else just parse a typename */
4557 param = parse_typename(parser, NULL, NULL);
4560 vec_push(params, param);
4561 if (param->expression.vtype >= TYPE_VARIANT) {
4562 char tname[1024]; /* typename is reserved in C++ */
4563 ast_type_to_string((ast_expression*)param, tname, sizeof(tname));
4564 parseerror(parser, "type not supported as part of a parameter list: %s", tname);
4567 /* type-restricted varargs */
4568 if (parser->tok == TOKEN_DOTS) {
4570 varparam = vec_last(params);
4572 if (!parser_next(parser) || (parser->tok != ')' && parser->tok != TOKEN_IDENT)) {
4573 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4576 if (parser->tok == TOKEN_IDENT) {
4577 argcounter = util_strdup(parser_tokval(parser));
4578 if (!parser_next(parser) || parser->tok != ')') {
4579 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4587 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
4591 if (vec_size(params) > 8 && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4592 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
4595 if (!parser_next(parser)) {
4596 parseerror(parser, "parse error after typename");
4600 /* now turn 'var' into a function type */
4601 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
4602 fval->expression.next = (ast_expression*)var;
4604 fval->expression.flags |= AST_FLAG_VARIADIC;
4607 var->expression.params = params;
4608 var->expression.varparam = (ast_expression*)varparam;
4609 var->argcounter = argcounter;
4618 for (i = 0; i < vec_size(params); ++i)
4619 ast_delete(params[i]);
4624 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
4626 ast_expression *cexp;
4627 ast_value *cval, *tmp;
4630 ctx = parser_ctx(parser);
4632 if (!parser_next(parser)) {
4634 parseerror(parser, "expected array-size");
4638 cexp = parse_expression_leave(parser, true, false, false);
4640 if (!cexp || !ast_istype(cexp, ast_value)) {
4644 parseerror(parser, "expected array-size as constant positive integer");
4647 cval = (ast_value*)cexp;
4649 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
4650 tmp->expression.next = (ast_expression*)var;
4653 if (cval->expression.vtype == TYPE_INTEGER)
4654 tmp->expression.count = cval->constval.vint;
4655 else if (cval->expression.vtype == TYPE_FLOAT)
4656 tmp->expression.count = cval->constval.vfloat;
4660 parseerror(parser, "array-size must be a positive integer constant");
4665 if (parser->tok != ']') {
4667 parseerror(parser, "expected ']' after array-size");
4670 if (!parser_next(parser)) {
4672 parseerror(parser, "error after parsing array size");
4678 /* Parse a complete typename.
4679 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
4680 * but when parsing variables separated by comma
4681 * 'storebase' should point to where the base-type should be kept.
4682 * The base type makes up every bit of type information which comes *before* the
4685 * The following will be parsed in its entirety:
4687 * The 'basetype' in this case is 'void()'
4688 * and if there's a comma after it, say:
4690 * then the type-information 'void()' can be stored in 'storebase'
4692 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
4694 ast_value *var, *tmp;
4697 const char *name = NULL;
4698 bool isfield = false;
4699 bool wasarray = false;
4700 size_t morefields = 0;
4702 ctx = parser_ctx(parser);
4704 /* types may start with a dot */
4705 if (parser->tok == '.') {
4707 /* if we parsed a dot we need a typename now */
4708 if (!parser_next(parser)) {
4709 parseerror(parser, "expected typename for field definition");
4713 /* Further dots are handled seperately because they won't be part of the
4716 while (parser->tok == '.') {
4718 if (!parser_next(parser)) {
4719 parseerror(parser, "expected typename for field definition");
4724 if (parser->tok == TOKEN_IDENT)
4725 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
4726 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
4727 parseerror(parser, "expected typename");
4731 /* generate the basic type value */
4732 if (cached_typedef) {
4733 var = ast_value_copy(cached_typedef);
4734 ast_value_set_name(var, "<type(from_def)>");
4736 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
4738 for (; morefields; --morefields) {
4739 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
4740 tmp->expression.next = (ast_expression*)var;
4744 /* do not yet turn into a field - remember:
4745 * .void() foo; is a field too
4746 * .void()() foo; is a function
4750 if (!parser_next(parser)) {
4752 parseerror(parser, "parse error after typename");
4756 /* an opening paren now starts the parameter-list of a function
4757 * this is where original-QC has parameter lists.
4758 * We allow a single parameter list here.
4759 * Much like fteqcc we don't allow `float()() x`
4761 if (parser->tok == '(') {
4762 var = parse_parameter_list(parser, var);
4767 /* store the base if requested */
4769 *storebase = ast_value_copy(var);
4771 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4772 tmp->expression.next = (ast_expression*)*storebase;
4777 /* there may be a name now */
4778 if (parser->tok == TOKEN_IDENT) {
4779 name = util_strdup(parser_tokval(parser));
4781 if (!parser_next(parser)) {
4783 parseerror(parser, "error after variable or field declaration");
4788 /* now this may be an array */
4789 if (parser->tok == '[') {
4791 var = parse_arraysize(parser, var);
4796 /* This is the point where we can turn it into a field */
4798 /* turn it into a field if desired */
4799 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4800 tmp->expression.next = (ast_expression*)var;
4804 /* now there may be function parens again */
4805 if (parser->tok == '(' && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4806 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4807 if (parser->tok == '(' && wasarray)
4808 parseerror(parser, "arrays as part of a return type is not supported");
4809 while (parser->tok == '(') {
4810 var = parse_parameter_list(parser, var);
4818 /* finally name it */
4820 if (!ast_value_set_name(var, name)) {
4822 parseerror(parser, "internal error: failed to set name");
4825 /* free the name, ast_value_set_name duplicates */
4832 static bool parse_typedef(parser_t *parser)
4834 ast_value *typevar, *oldtype;
4835 ast_expression *old;
4837 typevar = parse_typename(parser, NULL, NULL);
4842 if ( (old = parser_find_var(parser, typevar->name)) ) {
4843 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
4844 " -> `%s` has been declared here: %s:%i",
4845 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
4846 ast_delete(typevar);
4850 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
4851 parseerror(parser, "type `%s` has already been declared here: %s:%i",
4852 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
4853 ast_delete(typevar);
4857 vec_push(parser->_typedefs, typevar);
4858 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
4860 if (parser->tok != ';') {
4861 parseerror(parser, "expected semicolon after typedef");
4864 if (!parser_next(parser)) {
4865 parseerror(parser, "parse error after typedef");
4872 static const char *cvq_to_str(int cvq) {
4874 case CV_NONE: return "none";
4875 case CV_VAR: return "`var`";
4876 case CV_CONST: return "`const`";
4877 default: return "<INVALID>";
4881 static bool parser_check_qualifiers(parser_t *parser, const ast_value *var, const ast_value *proto)
4884 if (proto->cvq != var->cvq) {
4885 if (!(proto->cvq == CV_CONST && var->cvq == CV_NONE &&
4886 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4887 parser->tok == '='))
4889 return !parsewarning(parser, WARN_DIFFERENT_QUALIFIERS,
4890 "`%s` declared with different qualifiers: %s\n"
4891 " -> previous declaration here: %s:%i uses %s",
4892 var->name, cvq_to_str(var->cvq),
4893 ast_ctx(proto).file, ast_ctx(proto).line,
4894 cvq_to_str(proto->cvq));
4897 av = (var ->expression.flags & AST_FLAG_NORETURN);
4898 ao = (proto->expression.flags & AST_FLAG_NORETURN);
4900 return !parsewarning(parser, WARN_DIFFERENT_ATTRIBUTES,
4901 "`%s` declared with different attributes%s\n"
4902 " -> previous declaration here: %s:%i",
4903 var->name, (av ? ": noreturn" : ""),
4904 ast_ctx(proto).file, ast_ctx(proto).line,
4905 (ao ? ": noreturn" : ""));
4910 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)
4914 ast_expression *old;
4918 ast_value *basetype = NULL;
4920 bool isparam = false;
4921 bool isvector = false;
4922 bool cleanvar = true;
4923 bool wasarray = false;
4927 if (!localblock && is_static)
4928 parseerror(parser, "`static` qualifier is not supported in global scope");
4930 /* get the first complete variable */
4931 var = parse_typename(parser, &basetype, cached_typedef);
4934 ast_delete(basetype);
4942 /* Part 0: finish the type */
4943 if (parser->tok == '(') {
4944 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4945 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4946 var = parse_parameter_list(parser, var);
4952 /* we only allow 1-dimensional arrays */
4953 if (parser->tok == '[') {
4955 var = parse_arraysize(parser, var);
4961 if (parser->tok == '(' && wasarray) {
4962 parseerror(parser, "arrays as part of a return type is not supported");
4963 /* we'll still parse the type completely for now */
4965 /* for functions returning functions */
4966 while (parser->tok == '(') {
4967 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4968 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4969 var = parse_parameter_list(parser, var);
4976 var->cvq = qualifier;
4977 var->expression.flags |= qflags;
4978 if (var->expression.flags & AST_FLAG_DEPRECATED)
4979 var->desc = vstring;
4982 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
4983 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
4984 * is then filled with the previous definition and the parameter-names replaced.
4986 if (!strcmp(var->name, "nil")) {
4987 if (OPTS_FLAG(UNTYPED_NIL)) {
4988 if (!localblock || !OPTS_FLAG(PERMISSIVE))
4989 parseerror(parser, "name `nil` not allowed (try -fpermissive)");
4991 (void)!parsewarning(parser, WARN_RESERVED_NAMES, "variable name `nil` is reserved");
4994 /* Deal with end_sys_ vars */
4996 if (!strcmp(var->name, "end_sys_globals")) {
4998 parser->crc_globals = vec_size(parser->globals);
5001 else if (!strcmp(var->name, "end_sys_fields")) {
5003 parser->crc_fields = vec_size(parser->fields);
5006 if (was_end && var->expression.vtype == TYPE_FIELD) {
5007 if (parsewarning(parser, WARN_END_SYS_FIELDS,
5008 "global '%s' hint should not be a field",
5009 parser_tokval(parser)))
5016 if (!nofields && var->expression.vtype == TYPE_FIELD)
5018 /* deal with field declarations */
5019 old = parser_find_field(parser, var->name);
5021 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
5022 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
5031 parseerror(parser, "field `%s` already declared here: %s:%i",
5032 var->name, ast_ctx(old).file, ast_ctx(old).line);
5037 if ((OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC || OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) &&
5038 (old = parser_find_global(parser, var->name)))
5040 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5041 parseerror(parser, "field `%s` already declared here: %s:%i",
5042 var->name, ast_ctx(old).file, ast_ctx(old).line);
5049 /* deal with other globals */
5050 old = parser_find_global(parser, var->name);
5051 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
5053 /* This is a function which had a prototype */
5054 if (!ast_istype(old, ast_value)) {
5055 parseerror(parser, "internal error: prototype is not an ast_value");
5059 proto = (ast_value*)old;
5060 proto->desc = var->desc;
5061 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
5062 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
5064 ast_ctx(proto).file, ast_ctx(proto).line);
5068 /* we need the new parameter-names */
5069 for (i = 0; i < vec_size(proto->expression.params); ++i)
5070 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
5071 if (!parser_check_qualifiers(parser, var, proto)) {
5078 proto->expression.flags |= var->expression.flags;
5086 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
5087 "global `%s` already declared here: %s:%i",
5088 var->name, ast_ctx(old).file, ast_ctx(old).line))
5093 proto = (ast_value*)old;
5094 if (!ast_istype(old, ast_value)) {
5095 parseerror(parser, "internal error: not an ast_value");
5100 if (!parser_check_qualifiers(parser, var, proto)) {
5105 proto->expression.flags |= var->expression.flags;
5109 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC &&
5110 (old = parser_find_field(parser, var->name)))
5112 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5113 parseerror(parser, "global `%s` already declared here: %s:%i",
5114 var->name, ast_ctx(old).file, ast_ctx(old).line);
5121 else /* it's not a global */
5123 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
5124 if (old && !isparam) {
5125 parseerror(parser, "local `%s` already declared here: %s:%i",
5126 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
5130 old = parser_find_local(parser, var->name, 0, &isparam);
5131 if (old && isparam) {
5132 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
5133 "local `%s` is shadowing a parameter", var->name))
5135 parseerror(parser, "local `%s` already declared here: %s:%i",
5136 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
5140 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
5148 /* in a noref section we simply bump the usecount */
5149 if (noref || parser->noref)
5153 * Create the global/local, and deal with vector types.
5156 if (var->expression.vtype == TYPE_VECTOR)
5158 else if (var->expression.vtype == TYPE_FIELD &&
5159 var->expression.next->expression.vtype == TYPE_VECTOR)
5163 if (!create_vector_members(var, me)) {
5170 /* deal with global variables, fields, functions */
5171 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
5172 var->isfield = true;
5173 vec_push(parser->fields, (ast_expression*)var);
5174 util_htset(parser->htfields, var->name, var);
5176 for (i = 0; i < 3; ++i) {
5177 vec_push(parser->fields, (ast_expression*)me[i]);
5178 util_htset(parser->htfields, me[i]->name, me[i]);
5183 parser_addglobal(parser, var->name, (ast_expression*)var);
5185 for (i = 0; i < 3; ++i) {
5186 parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
5192 /* a static adds itself to be generated like any other global
5193 * but is added to the local namespace instead
5195 char *defname = NULL;
5196 size_t prefix_len, ln;
5198 ln = strlen(parser->function->name);
5199 vec_append(defname, ln, parser->function->name);
5201 vec_append(defname, 2, "::");
5202 /* remember the length up to here */
5203 prefix_len = vec_size(defname);
5205 /* Add it to the local scope */
5206 util_htset(vec_last(parser->variables), var->name, (void*)var);
5210 vec_last(parser->correct_variables),
5211 &vec_last(parser->correct_variables_score),
5215 /* now rename the global */
5216 ln = strlen(var->name);
5217 vec_append(defname, ln, var->name);
5218 ast_value_set_name(var, defname);
5220 /* push it to the to-be-generated globals */
5221 vec_push(parser->globals, (ast_expression*)var);
5223 /* same game for the vector members */
5225 for (i = 0; i < 3; ++i) {
5226 util_htset(vec_last(parser->variables), me[i]->name, (void*)(me[i]));
5230 vec_last(parser->correct_variables),
5231 &vec_last(parser->correct_variables_score),
5235 vec_shrinkto(defname, prefix_len);
5236 ln = strlen(me[i]->name);
5237 vec_append(defname, ln, me[i]->name);
5238 ast_member_set_name(me[i], defname);
5240 vec_push(parser->globals, (ast_expression*)me[i]);
5245 vec_push(localblock->locals, var);
5246 parser_addlocal(parser, var->name, (ast_expression*)var);
5248 for (i = 0; i < 3; ++i) {
5249 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
5250 ast_block_collect(localblock, (ast_expression*)me[i]);
5256 me[0] = me[1] = me[2] = NULL;
5261 if (var->expression.vtype == TYPE_ARRAY) {
5263 snprintf(name, sizeof(name), "%s##SET", var->name);
5264 if (!parser_create_array_setter(parser, var, name))
5266 snprintf(name, sizeof(name), "%s##GET", var->name);
5267 if (!parser_create_array_getter(parser, var, var->expression.next, name))
5270 else if (!localblock && !nofields &&
5271 var->expression.vtype == TYPE_FIELD &&
5272 var->expression.next->expression.vtype == TYPE_ARRAY)
5275 ast_expression *telem;
5277 ast_value *array = (ast_value*)var->expression.next;
5279 if (!ast_istype(var->expression.next, ast_value)) {
5280 parseerror(parser, "internal error: field element type must be an ast_value");
5284 snprintf(name, sizeof(name), "%s##SETF", var->name);
5285 if (!parser_create_array_field_setter(parser, array, name))
5288 telem = ast_type_copy(ast_ctx(var), array->expression.next);
5289 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
5290 tfield->expression.next = telem;
5291 snprintf(name, sizeof(name), "%s##GETFP", var->name);
5292 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
5300 if (parser->tok == ';') {
5301 ast_delete(basetype);
5302 if (!parser_next(parser)) {
5303 parseerror(parser, "error after variable declaration");
5309 if (parser->tok == ',')
5313 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
5316 parseerror(parser, "missing comma or semicolon while parsing variables");
5320 if (localblock && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5321 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
5322 "initializing expression turns variable `%s` into a constant in this standard",
5329 if (parser->tok != '{') {
5330 if (parser->tok != '=') {
5331 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
5335 if (!parser_next(parser)) {
5336 parseerror(parser, "error parsing initializer");
5340 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5341 parseerror(parser, "expected '=' before function body in this standard");
5344 if (parser->tok == '#') {
5345 ast_function *func = NULL;
5348 parseerror(parser, "cannot declare builtins within functions");
5351 if (var->expression.vtype != TYPE_FUNCTION) {
5352 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
5355 if (!parser_next(parser)) {
5356 parseerror(parser, "expected builtin number");
5359 if (parser->tok != TOKEN_INTCONST) {
5360 parseerror(parser, "builtin number must be an integer constant");
5363 if (parser_token(parser)->constval.i < 0) {
5364 parseerror(parser, "builtin number must be an integer greater than zero");
5368 if (var->hasvalue) {
5369 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
5370 "builtin `%s` has already been defined\n"
5371 " -> previous declaration here: %s:%i",
5372 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
5376 func = ast_function_new(ast_ctx(var), var->name, var);
5378 parseerror(parser, "failed to allocate function for `%s`", var->name);
5381 vec_push(parser->functions, func);
5383 func->builtin = -parser_token(parser)->constval.i-1;
5386 if (!parser_next(parser)) {
5387 parseerror(parser, "expected comma or semicolon");
5389 ast_function_delete(func);
5390 var->constval.vfunc = NULL;
5394 else if (parser->tok == '{' || parser->tok == '[')
5397 parseerror(parser, "cannot declare functions within functions");
5402 ast_ctx(proto) = parser_ctx(parser);
5404 if (!parse_function_body(parser, var))
5406 ast_delete(basetype);
5407 for (i = 0; i < vec_size(parser->gotos); ++i)
5408 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
5409 vec_free(parser->gotos);
5410 vec_free(parser->labels);
5413 ast_expression *cexp;
5416 cexp = parse_expression_leave(parser, true, false, false);
5421 cval = (ast_value*)cexp;
5422 if (cval != parser->nil &&
5423 (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
5426 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
5430 if (!OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
5431 qualifier != CV_VAR)
5433 var->cvq = CV_CONST;
5435 if (cval == parser->nil)
5436 var->expression.flags |= AST_FLAG_INITIALIZED;
5439 var->hasvalue = true;
5440 if (cval->expression.vtype == TYPE_STRING)
5441 var->constval.vstring = parser_strdup(cval->constval.vstring);
5442 else if (cval->expression.vtype == TYPE_FIELD)
5443 var->constval.vfield = cval;
5445 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
5451 shunt sy = { NULL, NULL, NULL, NULL };
5454 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
5455 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
5456 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
5457 if (!parser_sy_apply_operator(parser, &sy))
5460 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
5461 parseerror(parser, "internal error: leaked operands");
5462 if (!ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out))
5472 if (parser->tok == ',') {
5473 if (!parser_next(parser)) {
5474 parseerror(parser, "expected another variable");
5478 if (parser->tok != TOKEN_IDENT) {
5479 parseerror(parser, "expected another variable");
5482 var = ast_value_copy(basetype);
5484 ast_value_set_name(var, parser_tokval(parser));
5485 if (!parser_next(parser)) {
5486 parseerror(parser, "error parsing variable declaration");
5492 if (parser->tok != ';') {
5493 parseerror(parser, "missing semicolon after variables");
5497 if (!parser_next(parser)) {
5498 parseerror(parser, "parse error after variable declaration");
5502 ast_delete(basetype);
5506 if (cleanvar && var)
5508 ast_delete(basetype);
5512 ast_delete(basetype);
5513 if (cleanvar && var)
5515 if (me[0]) ast_member_delete(me[0]);
5516 if (me[1]) ast_member_delete(me[1]);
5517 if (me[2]) ast_member_delete(me[2]);
5521 static bool parser_global_statement(parser_t *parser)
5525 bool is_static = false;
5526 uint32_t qflags = 0;
5527 ast_value *istype = NULL;
5528 char *vstring = NULL;
5530 if (parser->tok == TOKEN_IDENT)
5531 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
5533 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
5535 return parse_variable(parser, NULL, false, CV_NONE, istype, false, false, 0, NULL);
5537 else if (parse_qualifiers(parser, false, &cvq, &noref, &is_static, &qflags, &vstring))
5539 if (cvq == CV_WRONG)
5541 return parse_variable(parser, NULL, true, cvq, NULL, noref, is_static, qflags, vstring);
5543 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "enum"))
5545 return parse_enum(parser);
5547 else if (parser->tok == TOKEN_KEYWORD)
5549 if (!strcmp(parser_tokval(parser), "typedef")) {
5550 if (!parser_next(parser)) {
5551 parseerror(parser, "expected type definition after 'typedef'");
5554 return parse_typedef(parser);
5556 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
5559 else if (parser->tok == '#')
5561 return parse_pragma(parser);
5563 else if (parser->tok == '$')
5565 if (!parser_next(parser)) {
5566 parseerror(parser, "parse error");
5572 parseerror(parser, "unexpected token: `%s`", parser->lex->tok.value);
5578 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
5580 return util_crc16(old, str, strlen(str));
5583 static void progdefs_crc_file(const char *str)
5585 /* write to progdefs.h here */
5589 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
5591 old = progdefs_crc_sum(old, str);
5592 progdefs_crc_file(str);
5596 static void generate_checksum(parser_t *parser)
5598 uint16_t crc = 0xFFFF;
5602 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
5603 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
5605 progdefs_crc_file("\tint\tpad;\n");
5606 progdefs_crc_file("\tint\tofs_return[3];\n");
5607 progdefs_crc_file("\tint\tofs_parm0[3];\n");
5608 progdefs_crc_file("\tint\tofs_parm1[3];\n");
5609 progdefs_crc_file("\tint\tofs_parm2[3];\n");
5610 progdefs_crc_file("\tint\tofs_parm3[3];\n");
5611 progdefs_crc_file("\tint\tofs_parm4[3];\n");
5612 progdefs_crc_file("\tint\tofs_parm5[3];\n");
5613 progdefs_crc_file("\tint\tofs_parm6[3];\n");
5614 progdefs_crc_file("\tint\tofs_parm7[3];\n");
5616 for (i = 0; i < parser->crc_globals; ++i) {
5617 if (!ast_istype(parser->globals[i], ast_value))
5619 value = (ast_value*)(parser->globals[i]);
5620 switch (value->expression.vtype) {
5621 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5622 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5623 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5624 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5626 crc = progdefs_crc_both(crc, "\tint\t");
5629 crc = progdefs_crc_both(crc, value->name);
5630 crc = progdefs_crc_both(crc, ";\n");
5632 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
5633 for (i = 0; i < parser->crc_fields; ++i) {
5634 if (!ast_istype(parser->fields[i], ast_value))
5636 value = (ast_value*)(parser->fields[i]);
5637 switch (value->expression.next->expression.vtype) {
5638 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5639 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5640 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5641 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5643 crc = progdefs_crc_both(crc, "\tint\t");
5646 crc = progdefs_crc_both(crc, value->name);
5647 crc = progdefs_crc_both(crc, ";\n");
5649 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
5654 static parser_t *parser;
5661 parser = (parser_t*)mem_a(sizeof(parser_t));
5665 memset(parser, 0, sizeof(*parser));
5667 for (i = 0; i < operator_count; ++i) {
5668 if (operators[i].id == opid1('=')) {
5669 parser->assign_op = operators+i;
5673 if (!parser->assign_op) {
5674 printf("internal error: initializing parser: failed to find assign operator\n");
5679 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
5680 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
5681 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
5682 vec_push(parser->_blocktypedefs, 0);
5685 vec_push(parser->correct_variables, correct_trie_new());
5686 vec_push(parser->correct_variables_score, NULL);
5688 empty_ctx.file = "<internal>";
5690 parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
5691 parser->nil->cvq = CV_CONST;
5692 if (OPTS_FLAG(UNTYPED_NIL))
5693 util_htset(parser->htglobals, "nil", (void*)parser->nil);
5695 parser->max_param_count = 1;
5697 parser->const_vec[0] = ast_value_new(empty_ctx, "<vector.x>", TYPE_NOEXPR);
5698 parser->const_vec[1] = ast_value_new(empty_ctx, "<vector.y>", TYPE_NOEXPR);
5699 parser->const_vec[2] = ast_value_new(empty_ctx, "<vector.z>", TYPE_NOEXPR);
5701 if (OPTS_OPTION_BOOL(OPTION_ADD_INFO)) {
5702 parser->reserved_version = ast_value_new(empty_ctx, "reserved:version", TYPE_STRING);
5703 parser->reserved_version->cvq = CV_CONST;
5704 parser->reserved_version->hasvalue = true;
5705 parser->reserved_version->expression.flags |= AST_FLAG_INCLUDE_DEF;
5706 parser->reserved_version->constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
5708 parser->reserved_version = NULL;
5713 bool parser_compile()
5715 /* initial lexer/parser state */
5716 parser->lex->flags.noops = true;
5718 if (parser_next(parser))
5720 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
5722 if (!parser_global_statement(parser)) {
5723 if (parser->tok == TOKEN_EOF)
5724 parseerror(parser, "unexpected eof");
5725 else if (compile_errors)
5726 parseerror(parser, "there have been errors, bailing out");
5727 lex_close(parser->lex);
5733 parseerror(parser, "parse error");
5734 lex_close(parser->lex);
5739 lex_close(parser->lex);
5742 return !compile_errors;
5745 bool parser_compile_file(const char *filename)
5747 parser->lex = lex_open(filename);
5749 con_err("failed to open file \"%s\"\n", filename);
5752 return parser_compile();
5755 bool parser_compile_string(const char *name, const char *str, size_t len)
5757 parser->lex = lex_open_string(str, len, name);
5759 con_err("failed to create lexer for string \"%s\"\n", name);
5762 return parser_compile();
5765 void parser_cleanup()
5768 for (i = 0; i < vec_size(parser->accessors); ++i) {
5769 ast_delete(parser->accessors[i]->constval.vfunc);
5770 parser->accessors[i]->constval.vfunc = NULL;
5771 ast_delete(parser->accessors[i]);
5773 for (i = 0; i < vec_size(parser->functions); ++i) {
5774 ast_delete(parser->functions[i]);
5776 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
5777 ast_delete(parser->imm_vector[i]);
5779 for (i = 0; i < vec_size(parser->imm_string); ++i) {
5780 ast_delete(parser->imm_string[i]);
5782 for (i = 0; i < vec_size(parser->imm_float); ++i) {
5783 ast_delete(parser->imm_float[i]);
5785 for (i = 0; i < vec_size(parser->fields); ++i) {
5786 ast_delete(parser->fields[i]);
5788 for (i = 0; i < vec_size(parser->globals); ++i) {
5789 ast_delete(parser->globals[i]);
5791 vec_free(parser->accessors);
5792 vec_free(parser->functions);
5793 vec_free(parser->imm_vector);
5794 vec_free(parser->imm_string);
5795 vec_free(parser->imm_float);
5796 vec_free(parser->globals);
5797 vec_free(parser->fields);
5799 for (i = 0; i < vec_size(parser->variables); ++i)
5800 util_htdel(parser->variables[i]);
5801 vec_free(parser->variables);
5802 vec_free(parser->_blocklocals);
5803 vec_free(parser->_locals);
5806 for (i = 0; i < vec_size(parser->correct_variables); ++i) {
5807 correct_del(parser->correct_variables[i], parser->correct_variables_score[i]);
5809 vec_free(parser->correct_variables);
5810 vec_free(parser->correct_variables_score);
5813 for (i = 0; i < vec_size(parser->_typedefs); ++i)
5814 ast_delete(parser->_typedefs[i]);
5815 vec_free(parser->_typedefs);
5816 for (i = 0; i < vec_size(parser->typedefs); ++i)
5817 util_htdel(parser->typedefs[i]);
5818 vec_free(parser->typedefs);
5819 vec_free(parser->_blocktypedefs);
5821 vec_free(parser->_block_ctx);
5823 vec_free(parser->labels);
5824 vec_free(parser->gotos);
5825 vec_free(parser->breaks);
5826 vec_free(parser->continues);
5828 ast_value_delete(parser->nil);
5830 ast_value_delete(parser->const_vec[0]);
5831 ast_value_delete(parser->const_vec[1]);
5832 ast_value_delete(parser->const_vec[2]);
5837 bool parser_finish(const char *output)
5843 if (compile_errors) {
5844 con_out("*** there were compile errors\n");
5848 ir = ir_builder_new("gmqcc_out");
5850 con_out("failed to allocate builder\n");
5854 for (i = 0; i < vec_size(parser->fields); ++i) {
5857 if (!ast_istype(parser->fields[i], ast_value))
5859 field = (ast_value*)parser->fields[i];
5860 hasvalue = field->hasvalue;
5861 field->hasvalue = false;
5862 if (!ast_global_codegen((ast_value*)field, ir, true)) {
5863 con_out("failed to generate field %s\n", field->name);
5864 ir_builder_delete(ir);
5869 ast_expression *subtype;
5870 field->hasvalue = true;
5871 subtype = field->expression.next;
5872 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
5873 if (subtype->expression.vtype == TYPE_FIELD)
5874 ifld->fieldtype = subtype->expression.next->expression.vtype;
5875 else if (subtype->expression.vtype == TYPE_FUNCTION)
5876 ifld->outtype = subtype->expression.next->expression.vtype;
5877 (void)!ir_value_set_field(field->ir_v, ifld);
5880 for (i = 0; i < vec_size(parser->globals); ++i) {
5882 if (!ast_istype(parser->globals[i], ast_value))
5884 asvalue = (ast_value*)(parser->globals[i]);
5885 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
5886 retval = retval && !compile_warning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
5887 "unused global: `%s`", asvalue->name);
5889 if (!ast_global_codegen(asvalue, ir, false)) {
5890 con_out("failed to generate global %s\n", asvalue->name);
5891 ir_builder_delete(ir);
5895 /* Build function vararg accessor ast tree now before generating
5896 * immediates, because the accessors may add new immediates
5898 for (i = 0; i < vec_size(parser->functions); ++i) {
5899 ast_function *f = parser->functions[i];
5901 if (parser->max_param_count > vec_size(f->vtype->expression.params)) {
5902 f->varargs->expression.count = parser->max_param_count - vec_size(f->vtype->expression.params);
5903 if (!parser_create_array_setter_impl(parser, f->varargs)) {
5904 con_out("failed to generate vararg setter for %s\n", f->name);
5905 ir_builder_delete(ir);
5908 if (!parser_create_array_getter_impl(parser, f->varargs)) {
5909 con_out("failed to generate vararg getter for %s\n", f->name);
5910 ir_builder_delete(ir);
5914 ast_delete(f->varargs);
5919 /* Now we can generate immediates */
5920 for (i = 0; i < vec_size(parser->imm_float); ++i) {
5921 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
5922 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
5923 ir_builder_delete(ir);
5927 for (i = 0; i < vec_size(parser->imm_string); ++i) {
5928 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
5929 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
5930 ir_builder_delete(ir);
5934 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
5935 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
5936 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
5937 ir_builder_delete(ir);
5941 for (i = 0; i < vec_size(parser->globals); ++i) {
5943 if (!ast_istype(parser->globals[i], ast_value))
5945 asvalue = (ast_value*)(parser->globals[i]);
5946 if (!(asvalue->expression.flags & AST_FLAG_INITIALIZED))
5948 if (asvalue->cvq == CV_CONST && !asvalue->hasvalue)
5949 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_CONSTANT,
5950 "uninitialized constant: `%s`",
5952 else if ((asvalue->cvq == CV_NONE || asvalue->cvq == CV_CONST) && !asvalue->hasvalue)
5953 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_GLOBAL,
5954 "uninitialized global: `%s`",
5957 if (!ast_generate_accessors(asvalue, ir)) {
5958 ir_builder_delete(ir);
5962 for (i = 0; i < vec_size(parser->fields); ++i) {
5964 asvalue = (ast_value*)(parser->fields[i]->expression.next);
5966 if (!ast_istype((ast_expression*)asvalue, ast_value))
5968 if (asvalue->expression.vtype != TYPE_ARRAY)
5970 if (!ast_generate_accessors(asvalue, ir)) {
5971 ir_builder_delete(ir);
5975 if (parser->reserved_version &&
5976 !ast_global_codegen(parser->reserved_version, ir, false))
5978 con_out("failed to generate reserved::version");
5979 ir_builder_delete(ir);
5982 for (i = 0; i < vec_size(parser->functions); ++i) {
5983 ast_function *f = parser->functions[i];
5984 if (!ast_function_codegen(f, ir)) {
5985 con_out("failed to generate function %s\n", f->name);
5986 ir_builder_delete(ir);
5990 if (OPTS_OPTION_BOOL(OPTION_DUMP))
5991 ir_builder_dump(ir, con_out);
5992 for (i = 0; i < vec_size(parser->functions); ++i) {
5993 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
5994 con_out("failed to finalize function %s\n", parser->functions[i]->name);
5995 ir_builder_delete(ir);
6000 if (compile_Werrors) {
6001 con_out("*** there were warnings treated as errors\n");
6002 compile_show_werrors();
6007 if (OPTS_OPTION_BOOL(OPTION_DUMPFIN))
6008 ir_builder_dump(ir, con_out);
6010 generate_checksum(parser);
6012 if (!ir_builder_generate(ir, output)) {
6013 con_out("*** failed to generate output file\n");
6014 ir_builder_delete(ir);
6019 ir_builder_delete(ir);