5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 /* beginning of locals */
30 #define PARSER_HT_LOCALS 2
32 #define PARSER_HT_SIZE 1024
33 #define TYPEDEF_HT_SIZE 16
35 enum parser_pot { POT_PAREN, POT_TERNARY1, POT_TERNARY2 };
40 ast_expression **globals;
41 ast_expression **fields;
42 ast_function **functions;
43 ast_value **imm_float;
44 ast_value **imm_string;
45 ast_value **imm_vector;
48 /* must be deleted first, they reference immediates and values */
49 ast_value **accessors;
51 ast_value *imm_float_zero;
52 ast_value *imm_float_one;
53 ast_value *imm_vector_zero;
59 ast_function *function;
61 /* All the labels the function defined...
62 * Should they be in ast_function instead?
67 const char **continues;
69 /* A list of hashtables for each scope */
75 /* not to be used directly, we use the hash table */
76 ast_expression **_locals;
78 ast_value **_typedefs;
79 size_t *_blocktypedefs;
82 /* we store the '=' operator info */
83 const oper_info *assign_op;
85 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
86 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
87 * anything else: type error
91 /* Keep track of our ternary vs parenthesis nesting state.
92 * If we reach a 'comma' operator in a ternary without a paren,
93 * we shall trigger -Wternary-precedence.
101 static const ast_expression *intrinsic_debug_typestring = (ast_expression*)0x10;
103 static void parser_enterblock(parser_t *parser);
104 static bool parser_leaveblock(parser_t *parser);
105 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
106 static bool parse_typedef(parser_t *parser);
107 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);
108 static ast_block* parse_block(parser_t *parser);
109 static bool parse_block_into(parser_t *parser, ast_block *block);
110 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
111 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
112 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
113 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
115 static void parseerror(parser_t *parser, const char *fmt, ...)
119 vcompile_error(parser->lex->tok.ctx, fmt, ap);
123 /* returns true if it counts as an error */
124 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
129 r = vcompile_warning(parser->lex->tok.ctx, warntype, fmt, ap);
134 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
139 r = vcompile_warning(ctx, warntype, fmt, ap);
144 /**********************************************************************
145 * some maths used for constant folding
148 vector vec3_add(vector a, vector b)
157 vector vec3_sub(vector a, vector b)
166 qcfloat vec3_mulvv(vector a, vector b)
168 return (a.x * b.x + a.y * b.y + a.z * b.z);
171 vector vec3_mulvf(vector a, float b)
180 /**********************************************************************
184 bool parser_next(parser_t *parser)
186 /* lex_do kills the previous token */
187 parser->tok = lex_do(parser->lex);
188 if (parser->tok == TOKEN_EOF)
190 if (parser->tok >= TOKEN_ERROR) {
191 parseerror(parser, "lex error");
197 #define parser_tokval(p) ((p)->lex->tok.value)
198 #define parser_token(p) (&((p)->lex->tok))
199 #define parser_ctx(p) ((p)->lex->tok.ctx)
201 static ast_value* parser_const_float(parser_t *parser, double d)
205 for (i = 0; i < vec_size(parser->imm_float); ++i) {
206 const double compare = parser->imm_float[i]->constval.vfloat;
207 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
208 return parser->imm_float[i];
210 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
212 out->hasvalue = true;
213 out->constval.vfloat = d;
214 vec_push(parser->imm_float, out);
218 static ast_value* parser_const_float_0(parser_t *parser)
220 if (!parser->imm_float_zero)
221 parser->imm_float_zero = parser_const_float(parser, 0);
222 return parser->imm_float_zero;
225 static ast_value* parser_const_float_1(parser_t *parser)
227 if (!parser->imm_float_one)
228 parser->imm_float_one = parser_const_float(parser, 1);
229 return parser->imm_float_one;
232 static char *parser_strdup(const char *str)
235 /* actually dup empty strings */
236 char *out = (char*)mem_a(1);
240 return util_strdup(str);
243 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
247 for (i = 0; i < vec_size(parser->imm_string); ++i) {
248 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
249 return parser->imm_string[i];
253 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
254 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
256 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
258 out->hasvalue = true;
259 out->constval.vstring = parser_strdup(str);
260 vec_push(parser->imm_string, out);
264 static ast_value* parser_const_vector(parser_t *parser, vector v)
268 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
269 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
270 return parser->imm_vector[i];
272 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
274 out->hasvalue = true;
275 out->constval.vvec = v;
276 vec_push(parser->imm_vector, out);
280 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
286 return parser_const_vector(parser, v);
289 static ast_value* parser_const_vector_0(parser_t *parser)
291 if (!parser->imm_vector_zero)
292 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
293 return parser->imm_vector_zero;
296 static ast_expression* parser_find_field(parser_t *parser, const char *name)
298 return ( ast_expression*)util_htget(parser->htfields, name);
301 static ast_expression* parser_find_global(parser_t *parser, const char *name)
303 return (ast_expression*)util_htget(parser->htglobals, name);
306 static ast_expression* parser_find_param(parser_t *parser, const char *name)
310 if (!parser->function)
312 fun = parser->function->vtype;
313 for (i = 0; i < vec_size(fun->expression.params); ++i) {
314 if (!strcmp(fun->expression.params[i]->name, name))
315 return (ast_expression*)(fun->expression.params[i]);
320 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
325 hash = util_hthash(parser->htglobals, name);
328 for (i = vec_size(parser->variables); i > upto;) {
330 if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
334 return parser_find_param(parser, name);
337 static ast_expression* parser_find_var(parser_t *parser, const char *name)
341 v = parser_find_local(parser, name, 0, &dummy);
342 if (!v) v = parser_find_global(parser, name);
346 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
350 hash = util_hthash(parser->typedefs[0], name);
352 for (i = vec_size(parser->typedefs); i > upto;) {
354 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
362 size_t etype; /* 0 = expression, others are operators */
366 ast_block *block; /* for commas and function calls */
375 #define SY_PAREN_EXPR '('
376 #define SY_PAREN_FUNC 'f'
377 #define SY_PAREN_INDEX '['
378 #define SY_PAREN_TERNARY '?'
380 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
391 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
395 e.out = (ast_expression*)v;
402 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
404 e.etype = 1 + (op - operators);
413 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
425 # define DEBUGSHUNTDO(x) x
427 # define DEBUGSHUNTDO(x)
430 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
431 * so we need to rotate it to become ent.(foo[n]).
433 static bool rotate_entfield_array_index_nodes(ast_expression **out)
435 ast_array_index *index;
436 ast_entfield *entfield;
440 ast_expression *entity;
442 lex_ctx ctx = ast_ctx(*out);
444 if (!ast_istype(*out, ast_array_index))
446 index = (ast_array_index*)*out;
448 if (!ast_istype(index->array, ast_entfield))
450 entfield = (ast_entfield*)index->array;
452 if (!ast_istype(entfield->field, ast_value))
454 field = (ast_value*)entfield->field;
457 entity = entfield->entity;
461 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
462 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
463 *out = (ast_expression*)entfield;
468 static bool immediate_is_true(lex_ctx ctx, ast_value *v)
470 switch (v->expression.vtype) {
472 return !!v->constval.vfloat;
474 return !!v->constval.vint;
476 if (OPTS_FLAG(CORRECT_LOGIC))
477 return v->constval.vvec.x &&
478 v->constval.vvec.y &&
481 return !!(v->constval.vvec.x);
483 if (!v->constval.vstring)
485 if (v->constval.vstring && OPTS_FLAG(TRUE_EMPTY_STRINGS))
487 return !!v->constval.vstring[0];
489 compile_error(ctx, "internal error: immediate_is_true on invalid type");
490 return !!v->constval.vfunc;
494 static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
498 ast_expression *out = NULL;
499 ast_expression *exprs[3];
500 ast_block *blocks[3];
501 ast_value *asvalue[3];
502 ast_binstore *asbinstore;
503 size_t i, assignop, addop, subop;
504 qcint generated_op = 0;
509 if (!vec_size(sy->ops)) {
510 parseerror(parser, "internal error: missing operator");
514 if (vec_last(sy->ops).paren) {
515 parseerror(parser, "unmatched parenthesis");
519 op = &operators[vec_last(sy->ops).etype - 1];
520 ctx = vec_last(sy->ops).ctx;
522 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
524 if (vec_size(sy->out) < op->operands) {
525 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
526 op->op, (int)op->id);
530 vec_shrinkby(sy->ops, 1);
532 /* op(:?) has no input and no output */
536 vec_shrinkby(sy->out, op->operands);
537 for (i = 0; i < op->operands; ++i) {
538 exprs[i] = sy->out[vec_size(sy->out)+i].out;
539 blocks[i] = sy->out[vec_size(sy->out)+i].block;
540 asvalue[i] = (ast_value*)exprs[i];
543 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
544 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
548 #define NotSameType(T) \
549 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
550 exprs[0]->expression.vtype != T)
551 #define CanConstFold1(A) \
552 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && (((ast_value*)(A))->cvq == CV_CONST) &&\
553 (A)->expression.vtype != TYPE_FUNCTION)
554 #define CanConstFold(A, B) \
555 (CanConstFold1(A) && CanConstFold1(B))
556 #define ConstV(i) (asvalue[(i)]->constval.vvec)
557 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
558 #define ConstS(i) (asvalue[(i)]->constval.vstring)
562 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
566 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
567 if (exprs[1]->expression.vtype != TYPE_FIELD) {
568 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
571 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
573 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
574 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
578 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
584 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
585 !(exprs[0]->expression.vtype == TYPE_FIELD &&
586 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
588 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
589 parseerror(parser, "cannot index value of type %s", ty1);
592 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
593 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
594 parseerror(parser, "index must be of type float, not %s", ty1);
597 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
598 if (rotate_entfield_array_index_nodes(&out))
601 /* This is not broken in fteqcc anymore */
602 if (opts.standard != COMPILER_GMQCC) {
603 /* this error doesn't need to make us bail out */
604 (void)!parsewarning(parser, WARN_EXTENSIONS,
605 "accessing array-field members of an entity without parenthesis\n"
606 " -> this is an extension from -std=gmqcc");
614 if (!ast_block_add_expr(blocks[0], exprs[1]))
617 blocks[0] = ast_block_new(ctx);
618 if (!ast_block_add_expr(blocks[0], exprs[0]) ||
619 !ast_block_add_expr(blocks[0], exprs[1]))
624 if (!ast_block_set_type(blocks[0], exprs[1]))
627 vec_push(sy->out, syblock(ctx, blocks[0]));
634 switch (exprs[0]->expression.vtype) {
636 if (CanConstFold1(exprs[0]))
637 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
639 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
640 (ast_expression*)parser_const_float_0(parser),
644 if (CanConstFold1(exprs[0]))
645 out = (ast_expression*)parser_const_vector_f(parser,
646 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
648 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
649 (ast_expression*)parser_const_vector_0(parser),
653 parseerror(parser, "invalid types used in expression: cannot negate type %s",
654 type_name[exprs[0]->expression.vtype]);
660 switch (exprs[0]->expression.vtype) {
662 if (CanConstFold1(exprs[0]))
663 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
665 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
668 if (CanConstFold1(exprs[0]))
669 out = (ast_expression*)parser_const_float(parser,
670 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
672 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
675 if (CanConstFold1(exprs[0])) {
676 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
677 out = (ast_expression*)parser_const_float(parser, !ConstS(0));
679 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
681 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
682 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
684 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
687 /* we don't constant-fold NOT for these types */
689 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
692 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
695 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
696 type_name[exprs[0]->expression.vtype]);
702 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
703 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
705 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
706 type_name[exprs[0]->expression.vtype],
707 type_name[exprs[1]->expression.vtype]);
710 switch (exprs[0]->expression.vtype) {
712 if (CanConstFold(exprs[0], exprs[1]))
714 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
717 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
720 if (CanConstFold(exprs[0], exprs[1]))
721 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
723 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
726 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
727 type_name[exprs[0]->expression.vtype],
728 type_name[exprs[1]->expression.vtype]);
733 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
734 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
736 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
737 type_name[exprs[1]->expression.vtype],
738 type_name[exprs[0]->expression.vtype]);
741 switch (exprs[0]->expression.vtype) {
743 if (CanConstFold(exprs[0], exprs[1]))
744 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
746 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
749 if (CanConstFold(exprs[0], exprs[1]))
750 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
752 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
755 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
756 type_name[exprs[1]->expression.vtype],
757 type_name[exprs[0]->expression.vtype]);
762 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
763 !(exprs[0]->expression.vtype == TYPE_VECTOR &&
764 exprs[1]->expression.vtype == TYPE_FLOAT) &&
765 !(exprs[1]->expression.vtype == TYPE_VECTOR &&
766 exprs[0]->expression.vtype == TYPE_FLOAT)
769 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
770 type_name[exprs[1]->expression.vtype],
771 type_name[exprs[0]->expression.vtype]);
774 switch (exprs[0]->expression.vtype) {
776 if (exprs[1]->expression.vtype == TYPE_VECTOR)
778 if (CanConstFold(exprs[0], exprs[1]))
779 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
781 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
785 if (CanConstFold(exprs[0], exprs[1]))
786 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
788 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
792 if (exprs[1]->expression.vtype == TYPE_FLOAT)
794 if (CanConstFold(exprs[0], exprs[1]))
795 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
797 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
801 if (CanConstFold(exprs[0], exprs[1]))
802 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
803 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[0])) {
804 vector vec = ConstV(0);
805 if (!vec.y && !vec.z) { /* 'n 0 0' * v */
806 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
807 out = (ast_expression*)ast_member_new(ctx, exprs[1], 0, NULL);
808 out->expression.node.keep = false;
809 ((ast_member*)out)->rvalue = true;
811 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.x), out);
813 else if (!vec.x && !vec.z) { /* '0 n 0' * v */
814 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
815 out = (ast_expression*)ast_member_new(ctx, exprs[1], 1, NULL);
816 out->expression.node.keep = false;
817 ((ast_member*)out)->rvalue = true;
819 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.y), out);
821 else if (!vec.x && !vec.y) { /* '0 n 0' * v */
822 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
823 out = (ast_expression*)ast_member_new(ctx, exprs[1], 2, NULL);
824 out->expression.node.keep = false;
825 ((ast_member*)out)->rvalue = true;
827 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.z), out);
830 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
832 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[1])) {
833 vector vec = ConstV(1);
834 if (!vec.y && !vec.z) { /* v * 'n 0 0' */
835 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
836 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
837 out->expression.node.keep = false;
838 ((ast_member*)out)->rvalue = true;
840 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.x));
842 else if (!vec.x && !vec.z) { /* v * '0 n 0' */
843 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
844 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
845 out->expression.node.keep = false;
846 ((ast_member*)out)->rvalue = true;
848 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.y));
850 else if (!vec.x && !vec.y) { /* v * '0 n 0' */
851 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
852 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
853 out->expression.node.keep = false;
854 ((ast_member*)out)->rvalue = true;
856 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.z));
859 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
862 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
866 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
867 type_name[exprs[1]->expression.vtype],
868 type_name[exprs[0]->expression.vtype]);
873 if (NotSameType(TYPE_FLOAT)) {
874 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
875 type_name[exprs[0]->expression.vtype],
876 type_name[exprs[1]->expression.vtype]);
879 if (CanConstFold(exprs[0], exprs[1]))
880 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
882 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
886 parseerror(parser, "qc does not have a modulo operator");
890 if (NotSameType(TYPE_FLOAT)) {
891 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
892 type_name[exprs[0]->expression.vtype],
893 type_name[exprs[1]->expression.vtype]);
896 if (CanConstFold(exprs[0], exprs[1]))
897 out = (ast_expression*)parser_const_float(parser,
898 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
899 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
901 out = (ast_expression*)ast_binary_new(ctx,
902 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
906 parseerror(parser, "TODO: bitxor");
911 case opid3('<','<','='):
912 case opid3('>','>','='):
913 parseerror(parser, "TODO: shifts");
917 generated_op += 1; /* INSTR_OR */
919 generated_op += INSTR_AND;
921 if (NotSameType(TYPE_FLOAT)) {
922 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
923 type_name[exprs[0]->expression.vtype],
924 type_name[exprs[1]->expression.vtype]);
925 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
926 parseerror(parser, "TODO: optional early out");
930 if (CanConstFold(exprs[0], exprs[1]))
932 if (OPTS_FLAG(PERL_LOGIC)) {
933 if (immediate_is_true(ctx, asvalue[0]))
937 out = (ast_expression*)parser_const_float(parser,
938 ( (generated_op == INSTR_OR)
939 ? (immediate_is_true(ctx, asvalue[0]) || immediate_is_true(ctx, asvalue[1]))
940 : (immediate_is_true(ctx, asvalue[0]) && immediate_is_true(ctx, asvalue[1])) )
945 if (OPTS_FLAG(PERL_LOGIC) && !ast_compare_type(exprs[0], exprs[1])) {
946 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
947 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
948 parseerror(parser, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
951 for (i = 0; i < 2; ++i) {
952 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->expression.vtype == TYPE_VECTOR) {
953 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[i]);
955 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
957 exprs[i] = out; out = NULL;
958 if (OPTS_FLAG(PERL_LOGIC)) {
959 /* here we want to keep the right expressions' type */
963 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->expression.vtype == TYPE_STRING) {
964 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[i]);
966 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
968 exprs[i] = out; out = NULL;
969 if (OPTS_FLAG(PERL_LOGIC)) {
970 /* here we want to keep the right expressions' type */
975 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
980 if (vec_last(parser->pot) != POT_TERNARY2) {
981 parseerror(parser, "mismatched parenthesis/ternary");
984 vec_pop(parser->pot);
985 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
986 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
987 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
988 parseerror(parser, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
991 if (CanConstFold1(exprs[0]))
992 out = (ConstF(0) ? exprs[1] : exprs[2]);
994 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
998 generated_op += 1; /* INSTR_GT */
1000 generated_op += 1; /* INSTR_LT */
1001 case opid2('>', '='):
1002 generated_op += 1; /* INSTR_GE */
1003 case opid2('<', '='):
1004 generated_op += INSTR_LE;
1005 if (NotSameType(TYPE_FLOAT)) {
1006 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
1007 type_name[exprs[0]->expression.vtype],
1008 type_name[exprs[1]->expression.vtype]);
1011 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
1013 case opid2('!', '='):
1014 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
1015 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
1016 type_name[exprs[0]->expression.vtype],
1017 type_name[exprs[1]->expression.vtype]);
1020 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
1022 case opid2('=', '='):
1023 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
1024 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
1025 type_name[exprs[0]->expression.vtype],
1026 type_name[exprs[1]->expression.vtype]);
1029 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
1033 if (ast_istype(exprs[0], ast_entfield)) {
1034 ast_expression *field = ((ast_entfield*)exprs[0])->field;
1035 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1036 exprs[0]->expression.vtype == TYPE_FIELD &&
1037 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
1039 assignop = type_storep_instr[TYPE_VECTOR];
1042 assignop = type_storep_instr[exprs[0]->expression.vtype];
1043 if (assignop == AINSTR_END ||
1044 !ast_compare_type(field->expression.next, exprs[1]))
1046 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
1047 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1048 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1049 field->expression.next->expression.vtype == TYPE_FUNCTION &&
1050 exprs[1]->expression.vtype == TYPE_FUNCTION)
1052 (void)!parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
1053 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1056 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1061 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1062 exprs[0]->expression.vtype == TYPE_FIELD &&
1063 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
1065 assignop = type_store_instr[TYPE_VECTOR];
1068 assignop = type_store_instr[exprs[0]->expression.vtype];
1071 if (assignop == AINSTR_END) {
1072 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1073 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1074 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1076 else if (exprs[1]->expression.vtype != TYPE_NIL &&
1077 !ast_compare_type(exprs[0], exprs[1]))
1079 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1080 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1081 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1082 exprs[0]->expression.vtype == TYPE_FUNCTION &&
1083 exprs[1]->expression.vtype == TYPE_FUNCTION)
1085 (void)!parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
1086 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1089 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1092 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1093 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1095 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
1097 case opid3('+','+','P'):
1098 case opid3('-','-','P'):
1100 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
1101 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1102 parseerror(parser, "invalid type for prefix increment: %s", ty1);
1105 if (op->id == opid3('+','+','P'))
1106 addop = INSTR_ADD_F;
1108 addop = INSTR_SUB_F;
1109 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1110 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1112 if (ast_istype(exprs[0], ast_entfield)) {
1113 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1115 (ast_expression*)parser_const_float_1(parser));
1117 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1119 (ast_expression*)parser_const_float_1(parser));
1122 case opid3('S','+','+'):
1123 case opid3('S','-','-'):
1125 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
1126 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1127 parseerror(parser, "invalid type for suffix increment: %s", ty1);
1130 if (op->id == opid3('S','+','+')) {
1131 addop = INSTR_ADD_F;
1132 subop = INSTR_SUB_F;
1134 addop = INSTR_SUB_F;
1135 subop = INSTR_ADD_F;
1137 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1138 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1140 if (ast_istype(exprs[0], ast_entfield)) {
1141 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1143 (ast_expression*)parser_const_float_1(parser));
1145 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1147 (ast_expression*)parser_const_float_1(parser));
1151 out = (ast_expression*)ast_binary_new(ctx, subop,
1153 (ast_expression*)parser_const_float_1(parser));
1155 case opid2('+','='):
1156 case opid2('-','='):
1157 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1158 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1160 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1161 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1162 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1166 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1167 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1169 if (ast_istype(exprs[0], ast_entfield))
1170 assignop = type_storep_instr[exprs[0]->expression.vtype];
1172 assignop = type_store_instr[exprs[0]->expression.vtype];
1173 switch (exprs[0]->expression.vtype) {
1175 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1176 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1177 exprs[0], exprs[1]);
1180 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1181 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1182 exprs[0], exprs[1]);
1185 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1186 type_name[exprs[0]->expression.vtype],
1187 type_name[exprs[1]->expression.vtype]);
1191 case opid2('*','='):
1192 case opid2('/','='):
1193 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1194 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1195 exprs[0]->expression.vtype == TYPE_VECTOR))
1197 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1198 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1199 parseerror(parser, "invalid types used in expression: %s and %s",
1203 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1204 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1206 if (ast_istype(exprs[0], ast_entfield))
1207 assignop = type_storep_instr[exprs[0]->expression.vtype];
1209 assignop = type_store_instr[exprs[0]->expression.vtype];
1210 switch (exprs[0]->expression.vtype) {
1212 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1213 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1214 exprs[0], exprs[1]);
1217 if (op->id == opid2('*','=')) {
1218 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1219 exprs[0], exprs[1]);
1221 /* there's no DIV_VF */
1222 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1223 (ast_expression*)parser_const_float_1(parser),
1227 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1232 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1233 type_name[exprs[0]->expression.vtype],
1234 type_name[exprs[1]->expression.vtype]);
1238 case opid2('&','='):
1239 case opid2('|','='):
1240 if (NotSameType(TYPE_FLOAT)) {
1241 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1242 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1243 parseerror(parser, "invalid types used in expression: %s and %s",
1247 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1248 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1250 if (ast_istype(exprs[0], ast_entfield))
1251 assignop = type_storep_instr[exprs[0]->expression.vtype];
1253 assignop = type_store_instr[exprs[0]->expression.vtype];
1254 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1255 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1256 exprs[0], exprs[1]);
1258 case opid3('&','~','='):
1259 /* This is like: a &= ~(b);
1260 * But QC has no bitwise-not, so we implement it as
1263 if (NotSameType(TYPE_FLOAT)) {
1264 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1265 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1266 parseerror(parser, "invalid types used in expression: %s and %s",
1270 if (ast_istype(exprs[0], ast_entfield))
1271 assignop = type_storep_instr[exprs[0]->expression.vtype];
1273 assignop = type_store_instr[exprs[0]->expression.vtype];
1274 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1277 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1278 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1280 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1281 asbinstore->keep_dest = true;
1282 out = (ast_expression*)asbinstore;
1288 parseerror(parser, "failed to apply operand %s", op->op);
1292 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1293 vec_push(sy->out, syexp(ctx, out));
1297 static bool parser_close_call(parser_t *parser, shunt *sy)
1299 /* was a function call */
1300 ast_expression *fun;
1306 vec_shrinkby(sy->ops, 1);
1307 fid = sy->ops[vec_size(sy->ops)].off;
1309 /* out[fid] is the function
1310 * everything above is parameters...
1311 * 0 params = nothing
1312 * 1 params = ast_expression
1316 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1317 parseerror(parser, "internal error: function call needs function and parameter list...");
1321 fun = sy->out[fid].out;
1323 if (fun == intrinsic_debug_typestring) {
1325 if (fid+2 != vec_size(sy->out) ||
1326 vec_last(sy->out).block)
1328 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1331 ast_type_to_string(vec_last(sy->out).out, ty, sizeof(ty));
1332 ast_unref(vec_last(sy->out).out);
1333 sy->out[fid] = syexp(ast_ctx(vec_last(sy->out).out),
1334 (ast_expression*)parser_const_string(parser, ty, false));
1335 vec_shrinkby(sy->out, 1);
1339 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1341 parseerror(parser, "internal error: failed to create ast_call node");
1345 if (fid+1 == vec_size(sy->out)) {
1348 } else if (fid+2 == vec_size(sy->out)) {
1350 vec_shrinkby(sy->out, 1);
1351 params = sy->out[vec_size(sy->out)].block;
1355 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1357 paramcount = vec_size(params->exprs);
1358 call->params = params->exprs;
1359 params->exprs = NULL;
1362 (void)!ast_call_check_types(call);
1364 parseerror(parser, "invalid function call");
1368 /* overwrite fid, the function, with a call */
1369 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1371 if (fun->expression.vtype != TYPE_FUNCTION) {
1372 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1376 if (!fun->expression.next) {
1377 parseerror(parser, "could not determine function return type");
1380 if (vec_size(fun->expression.params) != paramcount &&
1381 !((fun->expression.flags & AST_FLAG_VARIADIC) &&
1382 vec_size(fun->expression.params) < paramcount))
1385 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1387 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1388 if (opts.standard == COMPILER_GMQCC)
1391 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1392 " -> `%s` has been declared here: %s:%i",
1393 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1394 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1396 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1397 " -> it has been declared here: %s:%i",
1398 fewmany, (int)vec_size(fun->expression.params), (int)paramcount,
1399 ast_ctx(fun).file, (int)ast_ctx(fun).line);
1405 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1406 "too %s parameters for call to %s: expected %i, got %i\n"
1407 " -> `%s` has been declared here: %s:%i",
1408 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1409 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1411 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1412 "too %s parameters for function call: expected %i, got %i\n"
1413 " -> it has been declared here: %s:%i",
1414 fewmany, (int)vec_size(fun->expression.params), (int)paramcount,
1415 ast_ctx(fun).file, (int)ast_ctx(fun).line);
1423 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1425 if (!vec_size(sy->ops)) {
1426 parseerror(parser, "unmatched closing paren");
1429 /* this would for bit a + (x) because there are no operators inside (x)
1430 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1431 parseerror(parser, "empty parenthesis expression");
1435 while (vec_size(sy->ops)) {
1436 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1437 if (!parser_close_call(parser, sy))
1441 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1442 vec_shrinkby(sy->ops, 1);
1443 return !functions_only;
1445 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1448 /* pop off the parenthesis */
1449 vec_shrinkby(sy->ops, 1);
1450 /* then apply the index operator */
1451 if (!parser_sy_apply_operator(parser, sy))
1455 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_TERNARY) {
1458 if (vec_last(parser->pot) != POT_TERNARY1) {
1459 parseerror(parser, "mismatched colon in ternary expression (missing closing paren?)");
1462 vec_last(parser->pot) = POT_TERNARY2;
1463 /* pop off the parenthesis */
1464 vec_shrinkby(sy->ops, 1);
1467 if (!parser_sy_apply_operator(parser, sy))
1473 static void parser_reclassify_token(parser_t *parser)
1476 for (i = 0; i < operator_count; ++i) {
1477 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1478 parser->tok = TOKEN_OPERATOR;
1484 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1486 ast_expression *expr = NULL;
1488 bool wantop = false;
1489 bool gotmemberof = false;
1491 /* count the parens because an if starts with one, so the
1492 * end of a condition is an unmatched closing paren
1500 parser->lex->flags.noops = false;
1502 parser_reclassify_token(parser);
1507 gotmemberof = false;
1509 parser->memberof = 0;
1511 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1512 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1514 /* a translatable string */
1518 parseerror(parser, "expected operator or end of statement, got constant");
1522 parser->lex->flags.noops = true;
1523 if (!parser_next(parser) || parser->tok != '(') {
1524 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1527 parser->lex->flags.noops = false;
1528 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1529 parseerror(parser, "expected a constant string in translatable-string extension");
1532 val = parser_const_string(parser, parser_tokval(parser), true);
1536 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1537 DEBUGSHUNTDO(con_out("push string\n"));
1539 if (!parser_next(parser) || parser->tok != ')') {
1540 parseerror(parser, "expected closing paren after translatable string");
1544 else if (parser->tok == TOKEN_IDENT)
1546 ast_expression *var;
1548 parseerror(parser, "expected operator or end of statement");
1553 if (opts.standard == COMPILER_GMQCC)
1555 if (parser->memberof == TYPE_ENTITY) {
1556 /* still get vars first since there could be a fieldpointer */
1557 var = parser_find_var(parser, parser_tokval(parser));
1559 var = parser_find_field(parser, parser_tokval(parser));
1561 else if (parser->memberof == TYPE_VECTOR)
1563 parseerror(parser, "TODO: implement effective vector member access");
1566 else if (parser->memberof) {
1567 parseerror(parser, "namespace for member not found");
1571 var = parser_find_var(parser, parser_tokval(parser));
1573 var = parser_find_var(parser, parser_tokval(parser));
1575 var = parser_find_field(parser, parser_tokval(parser));
1579 if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
1580 var = (ast_expression*)intrinsic_debug_typestring;
1585 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1591 if (ast_istype(var, ast_value)) {
1592 ((ast_value*)var)->uses++;
1594 else if (ast_istype(var, ast_member)) {
1595 ast_member *mem = (ast_member*)var;
1596 if (ast_istype(mem->owner, ast_value))
1597 ((ast_value*)(mem->owner))->uses++;
1600 vec_push(sy.out, syexp(parser_ctx(parser), var));
1601 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1603 else if (parser->tok == TOKEN_FLOATCONST) {
1606 parseerror(parser, "expected operator or end of statement, got constant");
1610 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1613 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1614 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1616 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1619 parseerror(parser, "expected operator or end of statement, got constant");
1623 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1626 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1627 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1629 else if (parser->tok == TOKEN_STRINGCONST) {
1632 parseerror(parser, "expected operator or end of statement, got constant");
1636 val = parser_const_string(parser, parser_tokval(parser), false);
1639 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1640 DEBUGSHUNTDO(con_out("push string\n"));
1642 else if (parser->tok == TOKEN_VECTORCONST) {
1645 parseerror(parser, "expected operator or end of statement, got constant");
1649 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1652 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1653 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1654 parser_token(parser)->constval.v.x,
1655 parser_token(parser)->constval.v.y,
1656 parser_token(parser)->constval.v.z));
1658 else if (parser->tok == '(') {
1659 parseerror(parser, "internal error: '(' should be classified as operator");
1662 else if (parser->tok == '[') {
1663 parseerror(parser, "internal error: '[' should be classified as operator");
1666 else if (parser->tok == ')') {
1668 DEBUGSHUNTDO(con_out("do[op] )\n"));
1672 /* we do expect an operator next */
1673 /* closing an opening paren */
1674 if (!parser_close_paren(parser, &sy, false))
1676 if (vec_last(parser->pot) != POT_PAREN) {
1677 parseerror(parser, "mismatched parentheses (closing paren during ternary expression?)");
1680 vec_pop(parser->pot);
1682 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1686 /* allowed for function calls */
1687 if (!parser_close_paren(parser, &sy, true))
1689 if (vec_last(parser->pot) != POT_PAREN) {
1690 parseerror(parser, "mismatched parentheses (closing paren during ternary expression?)");
1693 vec_pop(parser->pot);
1697 else if (parser->tok == ']') {
1699 parseerror(parser, "operand expected");
1703 if (!parser_close_paren(parser, &sy, false))
1705 if (vec_last(parser->pot) != POT_PAREN) {
1706 parseerror(parser, "mismatched parentheses (closing paren during ternary expression?)");
1709 vec_pop(parser->pot);
1712 else if (parser->tok == TOKEN_TYPENAME) {
1713 parseerror(parser, "unexpected typename");
1716 else if (parser->tok != TOKEN_OPERATOR) {
1718 parseerror(parser, "expected operator or end of statement");
1725 /* classify the operator */
1726 const oper_info *op;
1727 const oper_info *olast = NULL;
1729 for (o = 0; o < operator_count; ++o) {
1730 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
1731 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1732 !strcmp(parser_tokval(parser), operators[o].op))
1737 if (o == operator_count) {
1738 /* no operator found... must be the end of the statement */
1741 /* found an operator */
1744 /* when declaring variables, a comma starts a new variable */
1745 if (op->id == opid1(',') && !parens && stopatcomma) {
1746 /* fixup the token */
1751 /* a colon without a pervious question mark cannot be a ternary */
1752 if (!ternaries && op->id == opid2(':','?')) {
1757 if (op->id == opid1(',')) {
1758 if (vec_size(parser->pot) && vec_last(parser->pot) == POT_TERNARY2) {
1759 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
1763 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1764 olast = &operators[vec_last(sy.ops).etype-1];
1767 (op->prec < olast->prec) ||
1768 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1770 if (!parser_sy_apply_operator(parser, &sy))
1772 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1773 olast = &operators[vec_last(sy.ops).etype-1];
1778 if (op->id == opid1('.') && opts.standard == COMPILER_GMQCC) {
1779 /* for gmqcc standard: open up the namespace of the previous type */
1780 ast_expression *prevex = vec_last(sy.out).out;
1782 parseerror(parser, "unexpected member operator");
1785 if (prevex->expression.vtype == TYPE_ENTITY)
1786 parser->memberof = TYPE_ENTITY;
1787 else if (prevex->expression.vtype == TYPE_VECTOR)
1788 parser->memberof = TYPE_VECTOR;
1790 parseerror(parser, "type error: type has no members");
1796 if (op->id == opid1('(')) {
1798 size_t sycount = vec_size(sy.out);
1799 DEBUGSHUNTDO(con_out("push [op] (\n"));
1800 ++parens; vec_push(parser->pot, POT_PAREN);
1801 /* we expected an operator, this is the function-call operator */
1802 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1804 ++parens; vec_push(parser->pot, POT_PAREN);
1805 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1806 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1809 } else if (op->id == opid1('[')) {
1811 parseerror(parser, "unexpected array subscript");
1814 ++parens; vec_push(parser->pot, POT_PAREN);
1815 /* push both the operator and the paren, this makes life easier */
1816 vec_push(sy.ops, syop(parser_ctx(parser), op));
1817 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1819 } else if (op->id == opid2('?',':')) {
1820 vec_push(sy.ops, syop(parser_ctx(parser), op));
1821 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_TERNARY, 0));
1824 vec_push(parser->pot, POT_TERNARY1);
1825 } else if (op->id == opid2(':','?')) {
1826 if (!vec_size(parser->pot)) {
1827 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1830 if (vec_last(parser->pot) != POT_TERNARY1) {
1831 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1834 if (!parser_close_paren(parser, &sy, false))
1836 vec_push(sy.ops, syop(parser_ctx(parser), op));
1840 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1841 vec_push(sy.ops, syop(parser_ctx(parser), op));
1842 wantop = !!(op->flags & OP_SUFFIX);
1845 if (!parser_next(parser)) {
1848 if (parser->tok == ';' ||
1849 (!parens && parser->tok == ']'))
1855 while (vec_size(sy.ops)) {
1856 if (!parser_sy_apply_operator(parser, &sy))
1860 parser->lex->flags.noops = true;
1861 if (!vec_size(sy.out)) {
1862 parseerror(parser, "empty expression");
1865 expr = sy.out[0].out;
1868 DEBUGSHUNTDO(con_out("shunt done\n"));
1869 if (vec_size(parser->pot)) {
1870 parseerror(parser, "internal error: vec_size(parser->pot) = %lu", (unsigned long)vec_size(parser->pot));
1873 vec_free(parser->pot);
1877 parser->lex->flags.noops = true;
1883 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1885 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1888 if (!parser_next(parser)) {
1895 static void parser_enterblock(parser_t *parser)
1897 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1898 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1899 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1900 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1901 vec_push(parser->_block_ctx, parser_ctx(parser));
1904 static bool parser_leaveblock(parser_t *parser)
1907 size_t locals, typedefs;
1909 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1910 parseerror(parser, "internal error: parser_leaveblock with no block");
1914 util_htdel(vec_last(parser->variables));
1915 vec_pop(parser->variables);
1916 if (!vec_size(parser->_blocklocals)) {
1917 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1921 locals = vec_last(parser->_blocklocals);
1922 vec_pop(parser->_blocklocals);
1923 while (vec_size(parser->_locals) != locals) {
1924 ast_expression *e = vec_last(parser->_locals);
1925 ast_value *v = (ast_value*)e;
1926 vec_pop(parser->_locals);
1927 if (ast_istype(e, ast_value) && !v->uses) {
1928 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
1933 typedefs = vec_last(parser->_blocktypedefs);
1934 while (vec_size(parser->_typedefs) != typedefs) {
1935 ast_delete(vec_last(parser->_typedefs));
1936 vec_pop(parser->_typedefs);
1938 util_htdel(vec_last(parser->typedefs));
1939 vec_pop(parser->typedefs);
1941 vec_pop(parser->_block_ctx);
1945 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1947 vec_push(parser->_locals, e);
1948 util_htset(vec_last(parser->variables), name, (void*)e);
1951 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
1955 ast_expression *prev;
1957 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->expression.vtype == TYPE_STRING)
1960 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_S, cond);
1963 parseerror(parser, "internal error: failed to process condition");
1968 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->expression.vtype == TYPE_VECTOR)
1970 /* vector types need to be cast to true booleans */
1971 ast_binary *bin = (ast_binary*)cond;
1972 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->op == INSTR_AND || bin->op == INSTR_OR))
1974 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
1976 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_V, cond);
1979 parseerror(parser, "internal error: failed to process condition");
1986 unary = (ast_unary*)cond;
1987 while (ast_istype(cond, ast_unary) && unary->op == INSTR_NOT_F)
1989 cond = unary->operand;
1990 unary->operand = NULL;
1993 unary = (ast_unary*)cond;
1997 parseerror(parser, "internal error: failed to process condition");
1999 if (ifnot) *_ifnot = !*_ifnot;
2003 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
2006 ast_expression *cond, *ontrue = NULL, *onfalse = NULL;
2009 lex_ctx ctx = parser_ctx(parser);
2011 (void)block; /* not touching */
2013 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2014 if (!parser_next(parser)) {
2015 parseerror(parser, "expected condition or 'not'");
2018 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
2020 if (!parser_next(parser)) {
2021 parseerror(parser, "expected condition in parenthesis");
2025 if (parser->tok != '(') {
2026 parseerror(parser, "expected 'if' condition in parenthesis");
2029 /* parse into the expression */
2030 if (!parser_next(parser)) {
2031 parseerror(parser, "expected 'if' condition after opening paren");
2034 /* parse the condition */
2035 cond = parse_expression_leave(parser, false);
2039 if (parser->tok != ')') {
2040 parseerror(parser, "expected closing paren after 'if' condition");
2044 /* parse into the 'then' branch */
2045 if (!parser_next(parser)) {
2046 parseerror(parser, "expected statement for on-true branch of 'if'");
2050 if (!parse_statement_or_block(parser, &ontrue)) {
2054 /* check for an else */
2055 if (!strcmp(parser_tokval(parser), "else")) {
2056 /* parse into the 'else' branch */
2057 if (!parser_next(parser)) {
2058 parseerror(parser, "expected on-false branch after 'else'");
2063 if (!parse_statement_or_block(parser, &onfalse)) {
2070 cond = process_condition(parser, cond, &ifnot);
2072 if (ontrue) ast_delete(ontrue);
2073 if (onfalse) ast_delete(onfalse);
2078 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
2080 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
2081 *out = (ast_expression*)ifthen;
2085 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out);
2086 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
2091 /* skip the 'while' and get the body */
2092 if (!parser_next(parser)) {
2093 if (OPTS_FLAG(LOOP_LABELS))
2094 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2096 parseerror(parser, "expected 'while' condition in parenthesis");
2100 if (parser->tok == ':') {
2101 if (!OPTS_FLAG(LOOP_LABELS))
2102 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2103 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2104 parseerror(parser, "expected loop label");
2107 label = util_strdup(parser_tokval(parser));
2108 if (!parser_next(parser)) {
2110 parseerror(parser, "expected 'while' condition in parenthesis");
2115 if (parser->tok != '(') {
2116 parseerror(parser, "expected 'while' condition in parenthesis");
2120 vec_push(parser->breaks, label);
2121 vec_push(parser->continues, label);
2123 rv = parse_while_go(parser, block, out);
2126 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2127 parseerror(parser, "internal error: label stack corrupted");
2133 vec_pop(parser->breaks);
2134 vec_pop(parser->continues);
2139 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out)
2142 ast_expression *cond, *ontrue;
2146 lex_ctx ctx = parser_ctx(parser);
2148 (void)block; /* not touching */
2150 /* parse into the expression */
2151 if (!parser_next(parser)) {
2152 parseerror(parser, "expected 'while' condition after opening paren");
2155 /* parse the condition */
2156 cond = parse_expression_leave(parser, false);
2160 if (parser->tok != ')') {
2161 parseerror(parser, "expected closing paren after 'while' condition");
2165 /* parse into the 'then' branch */
2166 if (!parser_next(parser)) {
2167 parseerror(parser, "expected while-loop body");
2171 if (!parse_statement_or_block(parser, &ontrue)) {
2176 cond = process_condition(parser, cond, &ifnot);
2181 aloop = ast_loop_new(ctx, NULL, cond, ifnot, NULL, false, NULL, ontrue);
2182 *out = (ast_expression*)aloop;
2186 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out);
2187 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
2192 /* skip the 'do' and get the body */
2193 if (!parser_next(parser)) {
2194 if (OPTS_FLAG(LOOP_LABELS))
2195 parseerror(parser, "expected loop label or body");
2197 parseerror(parser, "expected loop body");
2201 if (parser->tok == ':') {
2202 if (!OPTS_FLAG(LOOP_LABELS))
2203 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2204 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2205 parseerror(parser, "expected loop label");
2208 label = util_strdup(parser_tokval(parser));
2209 if (!parser_next(parser)) {
2211 parseerror(parser, "expected loop body");
2216 vec_push(parser->breaks, label);
2217 vec_push(parser->continues, label);
2219 rv = parse_dowhile_go(parser, block, out);
2222 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2223 parseerror(parser, "internal error: label stack corrupted");
2229 vec_pop(parser->breaks);
2230 vec_pop(parser->continues);
2235 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out)
2238 ast_expression *cond, *ontrue;
2242 lex_ctx ctx = parser_ctx(parser);
2244 (void)block; /* not touching */
2246 if (!parse_statement_or_block(parser, &ontrue))
2249 /* expect the "while" */
2250 if (parser->tok != TOKEN_KEYWORD ||
2251 strcmp(parser_tokval(parser), "while"))
2253 parseerror(parser, "expected 'while' and condition");
2258 /* skip the 'while' and check for opening paren */
2259 if (!parser_next(parser) || parser->tok != '(') {
2260 parseerror(parser, "expected 'while' condition in parenthesis");
2264 /* parse into the expression */
2265 if (!parser_next(parser)) {
2266 parseerror(parser, "expected 'while' condition after opening paren");
2270 /* parse the condition */
2271 cond = parse_expression_leave(parser, false);
2275 if (parser->tok != ')') {
2276 parseerror(parser, "expected closing paren after 'while' condition");
2282 if (!parser_next(parser) || parser->tok != ';') {
2283 parseerror(parser, "expected semicolon after condition");
2289 if (!parser_next(parser)) {
2290 parseerror(parser, "parse error");
2296 cond = process_condition(parser, cond, &ifnot);
2301 aloop = ast_loop_new(ctx, NULL, NULL, false, cond, ifnot, NULL, ontrue);
2302 *out = (ast_expression*)aloop;
2306 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
2307 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
2312 /* skip the 'for' and check for opening paren */
2313 if (!parser_next(parser)) {
2314 if (OPTS_FLAG(LOOP_LABELS))
2315 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2317 parseerror(parser, "expected 'for' expressions in parenthesis");
2321 if (parser->tok == ':') {
2322 if (!OPTS_FLAG(LOOP_LABELS))
2323 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2324 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2325 parseerror(parser, "expected loop label");
2328 label = util_strdup(parser_tokval(parser));
2329 if (!parser_next(parser)) {
2331 parseerror(parser, "expected 'for' expressions in parenthesis");
2336 if (parser->tok != '(') {
2337 parseerror(parser, "expected 'for' expressions in parenthesis");
2341 vec_push(parser->breaks, label);
2342 vec_push(parser->continues, label);
2344 rv = parse_for_go(parser, block, out);
2347 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2348 parseerror(parser, "internal error: label stack corrupted");
2354 vec_pop(parser->breaks);
2355 vec_pop(parser->continues);
2359 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
2362 ast_expression *initexpr, *cond, *increment, *ontrue;
2368 lex_ctx ctx = parser_ctx(parser);
2370 parser_enterblock(parser);
2377 /* parse into the expression */
2378 if (!parser_next(parser)) {
2379 parseerror(parser, "expected 'for' initializer after opening paren");
2384 if (parser->tok == TOKEN_IDENT)
2385 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2387 if (typevar || parser->tok == TOKEN_TYPENAME) {
2388 if (opts.standard != COMPILER_GMQCC) {
2389 if (parsewarning(parser, WARN_EXTENSIONS,
2390 "current standard does not allow variable declarations in for-loop initializers"))
2393 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0))
2396 else if (parser->tok != ';')
2398 initexpr = parse_expression_leave(parser, false);
2403 /* move on to condition */
2404 if (parser->tok != ';') {
2405 parseerror(parser, "expected semicolon after for-loop initializer");
2408 if (!parser_next(parser)) {
2409 parseerror(parser, "expected for-loop condition");
2413 /* parse the condition */
2414 if (parser->tok != ';') {
2415 cond = parse_expression_leave(parser, false);
2420 /* move on to incrementor */
2421 if (parser->tok != ';') {
2422 parseerror(parser, "expected semicolon after for-loop initializer");
2425 if (!parser_next(parser)) {
2426 parseerror(parser, "expected for-loop condition");
2430 /* parse the incrementor */
2431 if (parser->tok != ')') {
2432 increment = parse_expression_leave(parser, false);
2435 if (!ast_side_effects(increment)) {
2436 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2442 if (parser->tok != ')') {
2443 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2446 /* parse into the 'then' branch */
2447 if (!parser_next(parser)) {
2448 parseerror(parser, "expected for-loop body");
2451 if (!parse_statement_or_block(parser, &ontrue))
2455 cond = process_condition(parser, cond, &ifnot);
2459 aloop = ast_loop_new(ctx, initexpr, cond, ifnot, NULL, false, increment, ontrue);
2460 *out = (ast_expression*)aloop;
2462 if (!parser_leaveblock(parser))
2466 if (initexpr) ast_delete(initexpr);
2467 if (cond) ast_delete(cond);
2468 if (increment) ast_delete(increment);
2469 (void)!parser_leaveblock(parser);
2473 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2475 ast_expression *exp = NULL;
2476 ast_return *ret = NULL;
2477 ast_value *expected = parser->function->vtype;
2479 lex_ctx ctx = parser_ctx(parser);
2481 (void)block; /* not touching */
2483 if (!parser_next(parser)) {
2484 parseerror(parser, "expected return expression");
2488 if (parser->tok != ';') {
2489 exp = parse_expression(parser, false);
2493 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2494 parseerror(parser, "return with invalid expression");
2497 ret = ast_return_new(ctx, exp);
2503 if (!parser_next(parser))
2504 parseerror(parser, "parse error");
2505 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2506 if (opts.standard != COMPILER_GMQCC)
2507 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2509 parseerror(parser, "return without value");
2511 ret = ast_return_new(ctx, NULL);
2513 *out = (ast_expression*)ret;
2517 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2520 unsigned int levels = 0;
2521 lex_ctx ctx = parser_ctx(parser);
2522 const char **loops = (is_continue ? parser->continues : parser->breaks);
2524 (void)block; /* not touching */
2525 if (!parser_next(parser)) {
2526 parseerror(parser, "expected semicolon or loop label");
2530 if (parser->tok == TOKEN_IDENT) {
2531 if (!OPTS_FLAG(LOOP_LABELS))
2532 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2533 i = vec_size(loops);
2535 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
2538 parseerror(parser, "no such loop to %s: `%s`",
2539 (is_continue ? "continue" : "break out of"),
2540 parser_tokval(parser));
2545 if (!parser_next(parser)) {
2546 parseerror(parser, "expected semicolon");
2551 if (parser->tok != ';') {
2552 parseerror(parser, "expected semicolon");
2556 if (!parser_next(parser))
2557 parseerror(parser, "parse error");
2559 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
2563 /* returns true when it was a variable qualifier, false otherwise!
2564 * on error, cvq is set to CV_WRONG
2566 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags)
2568 bool had_const = false;
2569 bool had_var = false;
2570 bool had_noref = false;
2571 bool had_attrib = false;
2572 bool had_static = false;
2577 if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
2579 /* parse an attribute */
2580 if (!parser_next(parser)) {
2581 parseerror(parser, "expected attribute after `[[`");
2585 if (!strcmp(parser_tokval(parser), "noreturn")) {
2586 flags |= AST_FLAG_NORETURN;
2587 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2588 parseerror(parser, "`noreturn` attribute has no parameters, expected `]]`");
2593 else if (!strcmp(parser_tokval(parser), "noref")) {
2595 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2596 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2601 else if (!strcmp(parser_tokval(parser), "inline")) {
2602 flags |= AST_FLAG_INLINE;
2603 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2604 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2611 /* Skip tokens until we hit a ]] */
2612 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
2613 while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2614 if (!parser_next(parser)) {
2615 parseerror(parser, "error inside attribute");
2622 else if (!strcmp(parser_tokval(parser), "static"))
2624 else if (!strcmp(parser_tokval(parser), "const"))
2626 else if (!strcmp(parser_tokval(parser), "var"))
2628 else if (with_local && !strcmp(parser_tokval(parser), "local"))
2630 else if (!strcmp(parser_tokval(parser), "noref"))
2632 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
2637 if (!parser_next(parser))
2647 *is_static = had_static;
2651 parseerror(parser, "parse error after variable qualifier");
2656 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
2657 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2662 /* skip the 'while' and get the body */
2663 if (!parser_next(parser)) {
2664 if (OPTS_FLAG(LOOP_LABELS))
2665 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
2667 parseerror(parser, "expected 'switch' operand in parenthesis");
2671 if (parser->tok == ':') {
2672 if (!OPTS_FLAG(LOOP_LABELS))
2673 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2674 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2675 parseerror(parser, "expected loop label");
2678 label = util_strdup(parser_tokval(parser));
2679 if (!parser_next(parser)) {
2681 parseerror(parser, "expected 'switch' operand in parenthesis");
2686 if (parser->tok != '(') {
2687 parseerror(parser, "expected 'switch' operand in parenthesis");
2691 vec_push(parser->breaks, label);
2693 rv = parse_switch_go(parser, block, out);
2696 if (vec_last(parser->breaks) != label) {
2697 parseerror(parser, "internal error: label stack corrupted");
2703 vec_pop(parser->breaks);
2708 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
2710 ast_expression *operand;
2713 ast_switch *switchnode;
2714 ast_switch_case swcase;
2717 bool noref, is_static;
2718 uint32_t qflags = 0;
2720 lex_ctx ctx = parser_ctx(parser);
2722 (void)block; /* not touching */
2725 /* parse into the expression */
2726 if (!parser_next(parser)) {
2727 parseerror(parser, "expected switch operand");
2730 /* parse the operand */
2731 operand = parse_expression_leave(parser, false);
2735 switchnode = ast_switch_new(ctx, operand);
2738 if (parser->tok != ')') {
2739 ast_delete(switchnode);
2740 parseerror(parser, "expected closing paren after 'switch' operand");
2744 /* parse over the opening paren */
2745 if (!parser_next(parser) || parser->tok != '{') {
2746 ast_delete(switchnode);
2747 parseerror(parser, "expected list of cases");
2751 if (!parser_next(parser)) {
2752 ast_delete(switchnode);
2753 parseerror(parser, "expected 'case' or 'default'");
2757 /* new block; allow some variables to be declared here */
2758 parser_enterblock(parser);
2761 if (parser->tok == TOKEN_IDENT)
2762 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2763 if (typevar || parser->tok == TOKEN_TYPENAME) {
2764 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0)) {
2765 ast_delete(switchnode);
2770 if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags))
2772 if (cvq == CV_WRONG) {
2773 ast_delete(switchnode);
2776 if (!parse_variable(parser, block, false, cvq, NULL, noref, is_static, qflags)) {
2777 ast_delete(switchnode);
2786 while (parser->tok != '}') {
2787 ast_block *caseblock;
2789 if (!strcmp(parser_tokval(parser), "case")) {
2790 if (!parser_next(parser)) {
2791 ast_delete(switchnode);
2792 parseerror(parser, "expected expression for case");
2795 swcase.value = parse_expression_leave(parser, false);
2796 if (!swcase.value) {
2797 ast_delete(switchnode);
2798 parseerror(parser, "expected expression for case");
2801 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2802 if (!ast_istype(swcase.value, ast_value)) { /* || ((ast_value*)swcase.value)->cvq != CV_CONST) { */
2803 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2809 else if (!strcmp(parser_tokval(parser), "default")) {
2810 swcase.value = NULL;
2811 if (!parser_next(parser)) {
2812 ast_delete(switchnode);
2813 parseerror(parser, "expected colon");
2818 ast_delete(switchnode);
2819 parseerror(parser, "expected 'case' or 'default'");
2823 /* Now the colon and body */
2824 if (parser->tok != ':') {
2825 if (swcase.value) ast_unref(swcase.value);
2826 ast_delete(switchnode);
2827 parseerror(parser, "expected colon");
2831 if (!parser_next(parser)) {
2832 if (swcase.value) ast_unref(swcase.value);
2833 ast_delete(switchnode);
2834 parseerror(parser, "expected statements or case");
2837 caseblock = ast_block_new(parser_ctx(parser));
2839 if (swcase.value) ast_unref(swcase.value);
2840 ast_delete(switchnode);
2843 swcase.code = (ast_expression*)caseblock;
2844 vec_push(switchnode->cases, swcase);
2846 ast_expression *expr;
2847 if (parser->tok == '}')
2849 if (parser->tok == TOKEN_KEYWORD) {
2850 if (!strcmp(parser_tokval(parser), "case") ||
2851 !strcmp(parser_tokval(parser), "default"))
2856 if (!parse_statement(parser, caseblock, &expr, true)) {
2857 ast_delete(switchnode);
2862 if (!ast_block_add_expr(caseblock, expr)) {
2863 ast_delete(switchnode);
2869 parser_leaveblock(parser);
2872 if (parser->tok != '}') {
2873 ast_delete(switchnode);
2874 parseerror(parser, "expected closing paren of case list");
2877 if (!parser_next(parser)) {
2878 ast_delete(switchnode);
2879 parseerror(parser, "parse error after switch");
2882 *out = (ast_expression*)switchnode;
2886 static bool parse_goto(parser_t *parser, ast_expression **out)
2891 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2892 parseerror(parser, "expected label name after `goto`");
2896 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2898 for (i = 0; i < vec_size(parser->labels); ++i) {
2899 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2900 ast_goto_set_label(gt, parser->labels[i]);
2904 if (i == vec_size(parser->labels))
2905 vec_push(parser->gotos, gt);
2907 if (!parser_next(parser) || parser->tok != ';') {
2908 parseerror(parser, "semicolon expected after goto label");
2911 if (!parser_next(parser)) {
2912 parseerror(parser, "parse error after goto");
2916 *out = (ast_expression*)gt;
2920 static bool parse_skipwhite(parser_t *parser)
2923 if (!parser_next(parser))
2925 } while (parser->tok == TOKEN_WHITE && parser->tok < TOKEN_ERROR);
2926 return parser->tok < TOKEN_ERROR;
2929 static bool parse_eol(parser_t *parser)
2931 if (!parse_skipwhite(parser))
2933 return parser->tok == TOKEN_EOL;
2936 static bool parse_pragma_do(parser_t *parser)
2938 if (!parser_next(parser) ||
2939 parser->tok != TOKEN_IDENT ||
2940 strcmp(parser_tokval(parser), "pragma"))
2942 parseerror(parser, "expected `pragma` keyword after `#`, got `%s`", parser_tokval(parser));
2945 if (!parse_skipwhite(parser) || parser->tok != TOKEN_IDENT) {
2946 parseerror(parser, "expected pragma, got `%s`", parser_tokval(parser));
2950 if (!strcmp(parser_tokval(parser), "noref")) {
2951 if (!parse_skipwhite(parser) || parser->tok != TOKEN_INTCONST) {
2952 parseerror(parser, "`noref` pragma requires an argument: 0 or 1");
2955 parser->noref = !!parser_token(parser)->constval.i;
2956 if (!parse_eol(parser)) {
2957 parseerror(parser, "parse error after `noref` pragma");
2963 (void)!parsewarning(parser, WARN_UNKNOWN_PRAGMAS, "ignoring #pragma %s", parser_tokval(parser));
2970 static bool parse_pragma(parser_t *parser)
2973 parser->lex->flags.preprocessing = true;
2974 parser->lex->flags.mergelines = true;
2975 rv = parse_pragma_do(parser);
2976 if (parser->tok != TOKEN_EOL) {
2977 parseerror(parser, "junk after pragma");
2980 parser->lex->flags.preprocessing = false;
2981 parser->lex->flags.mergelines = false;
2982 if (!parser_next(parser)) {
2983 parseerror(parser, "parse error after pragma");
2989 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2991 bool noref, is_static;
2993 uint32_t qflags = 0;
2994 ast_value *typevar = NULL;
2998 if (parser->tok == TOKEN_IDENT)
2999 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3001 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3003 /* local variable */
3005 parseerror(parser, "cannot declare a variable from here");
3008 if (opts.standard == COMPILER_QCC) {
3009 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
3012 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0))
3016 else if (parse_qualifiers(parser, !!block, &cvq, &noref, &is_static, &qflags))
3018 if (cvq == CV_WRONG)
3020 return parse_variable(parser, block, true, cvq, NULL, noref, is_static, qflags);
3022 else if (parser->tok == TOKEN_KEYWORD)
3024 if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
3029 if (!parser_next(parser)) {
3030 parseerror(parser, "parse error after __builtin_debug_printtype");
3034 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
3036 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
3037 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
3038 if (!parser_next(parser)) {
3039 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
3045 if (!parse_statement(parser, block, out, allow_cases))
3048 con_out("__builtin_debug_printtype: got no output node\n");
3051 ast_type_to_string(*out, ty, sizeof(ty));
3052 con_out("__builtin_debug_printtype: `%s`\n", ty);
3057 else if (!strcmp(parser_tokval(parser), "return"))
3059 return parse_return(parser, block, out);
3061 else if (!strcmp(parser_tokval(parser), "if"))
3063 return parse_if(parser, block, out);
3065 else if (!strcmp(parser_tokval(parser), "while"))
3067 return parse_while(parser, block, out);
3069 else if (!strcmp(parser_tokval(parser), "do"))
3071 return parse_dowhile(parser, block, out);
3073 else if (!strcmp(parser_tokval(parser), "for"))
3075 if (opts.standard == COMPILER_QCC) {
3076 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
3079 return parse_for(parser, block, out);
3081 else if (!strcmp(parser_tokval(parser), "break"))
3083 return parse_break_continue(parser, block, out, false);
3085 else if (!strcmp(parser_tokval(parser), "continue"))
3087 return parse_break_continue(parser, block, out, true);
3089 else if (!strcmp(parser_tokval(parser), "switch"))
3091 return parse_switch(parser, block, out);
3093 else if (!strcmp(parser_tokval(parser), "case") ||
3094 !strcmp(parser_tokval(parser), "default"))
3097 parseerror(parser, "unexpected 'case' label");
3102 else if (!strcmp(parser_tokval(parser), "goto"))
3104 return parse_goto(parser, out);
3106 else if (!strcmp(parser_tokval(parser), "typedef"))
3108 if (!parser_next(parser)) {
3109 parseerror(parser, "expected type definition after 'typedef'");
3112 return parse_typedef(parser);
3114 parseerror(parser, "Unexpected keyword");
3117 else if (parser->tok == '{')
3120 inner = parse_block(parser);
3123 *out = (ast_expression*)inner;
3126 else if (parser->tok == ':')
3130 if (!parser_next(parser)) {
3131 parseerror(parser, "expected label name");
3134 if (parser->tok != TOKEN_IDENT) {
3135 parseerror(parser, "label must be an identifier");
3138 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
3141 vec_push(parser->labels, label);
3142 *out = (ast_expression*)label;
3143 if (!parser_next(parser)) {
3144 parseerror(parser, "parse error after label");
3147 for (i = 0; i < vec_size(parser->gotos); ++i) {
3148 if (!strcmp(parser->gotos[i]->name, label->name)) {
3149 ast_goto_set_label(parser->gotos[i], label);
3150 vec_remove(parser->gotos, i, 1);
3156 else if (parser->tok == ';')
3158 if (!parser_next(parser)) {
3159 parseerror(parser, "parse error after empty statement");
3166 ast_expression *exp = parse_expression(parser, false);
3170 if (!ast_side_effects(exp)) {
3171 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
3178 static bool parse_block_into(parser_t *parser, ast_block *block)
3182 parser_enterblock(parser);
3184 if (!parser_next(parser)) { /* skip the '{' */
3185 parseerror(parser, "expected function body");
3189 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3191 ast_expression *expr = NULL;
3192 if (parser->tok == '}')
3195 if (!parse_statement(parser, block, &expr, false)) {
3196 /* parseerror(parser, "parse error"); */
3202 if (!ast_block_add_expr(block, expr)) {
3209 if (parser->tok != '}') {
3212 (void)parser_next(parser);
3216 if (!parser_leaveblock(parser))
3218 return retval && !!block;
3221 static ast_block* parse_block(parser_t *parser)
3224 block = ast_block_new(parser_ctx(parser));
3227 if (!parse_block_into(parser, block)) {
3228 ast_block_delete(block);
3234 static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
3236 if (parser->tok == '{') {
3237 *out = (ast_expression*)parse_block(parser);
3240 return parse_statement(parser, NULL, out, false);
3243 static bool create_vector_members(ast_value *var, ast_member **me)
3246 size_t len = strlen(var->name);
3248 for (i = 0; i < 3; ++i) {
3249 char *name = (char*)mem_a(len+3);
3250 memcpy(name, var->name, len);
3252 name[len+1] = 'x'+i;
3254 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
3263 do { ast_member_delete(me[--i]); } while(i);
3267 static bool parse_function_body(parser_t *parser, ast_value *var)
3269 ast_block *block = NULL;
3274 ast_expression *framenum = NULL;
3275 ast_expression *nextthink = NULL;
3276 /* None of the following have to be deleted */
3277 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
3278 ast_expression *gbl_time = NULL, *gbl_self = NULL;
3279 bool has_frame_think;
3283 has_frame_think = false;
3284 old = parser->function;
3286 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
3287 parseerror(parser, "gotos/labels leaking");
3291 if (var->expression.flags & AST_FLAG_VARIADIC) {
3292 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
3293 "variadic function with implementation will not be able to access additional parameters"))
3299 if (parser->tok == '[') {
3300 /* got a frame definition: [ framenum, nextthink ]
3301 * this translates to:
3302 * self.frame = framenum;
3303 * self.nextthink = time + 0.1;
3304 * self.think = nextthink;
3308 fld_think = parser_find_field(parser, "think");
3309 fld_nextthink = parser_find_field(parser, "nextthink");
3310 fld_frame = parser_find_field(parser, "frame");
3311 if (!fld_think || !fld_nextthink || !fld_frame) {
3312 parseerror(parser, "cannot use [frame,think] notation without the required fields");
3313 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
3316 gbl_time = parser_find_global(parser, "time");
3317 gbl_self = parser_find_global(parser, "self");
3318 if (!gbl_time || !gbl_self) {
3319 parseerror(parser, "cannot use [frame,think] notation without the required globals");
3320 parseerror(parser, "please declare the following globals: `time`, `self`");
3324 if (!parser_next(parser))
3327 framenum = parse_expression_leave(parser, true);
3329 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
3332 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
3333 ast_unref(framenum);
3334 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
3338 if (parser->tok != ',') {
3339 ast_unref(framenum);
3340 parseerror(parser, "expected comma after frame number in [frame,think] notation");
3341 parseerror(parser, "Got a %i\n", parser->tok);
3345 if (!parser_next(parser)) {
3346 ast_unref(framenum);
3350 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
3352 /* qc allows the use of not-yet-declared functions here
3353 * - this automatically creates a prototype */
3354 ast_value *thinkfunc;
3355 ast_expression *functype = fld_think->expression.next;
3357 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
3358 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
3359 ast_unref(framenum);
3360 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
3364 if (!parser_next(parser)) {
3365 ast_unref(framenum);
3366 ast_delete(thinkfunc);
3370 vec_push(parser->globals, (ast_expression*)thinkfunc);
3371 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
3372 nextthink = (ast_expression*)thinkfunc;
3375 nextthink = parse_expression_leave(parser, true);
3377 ast_unref(framenum);
3378 parseerror(parser, "expected a think-function in [frame,think] notation");
3383 if (!ast_istype(nextthink, ast_value)) {
3384 parseerror(parser, "think-function in [frame,think] notation must be a constant");
3388 if (retval && parser->tok != ']') {
3389 parseerror(parser, "expected closing `]` for [frame,think] notation");
3393 if (retval && !parser_next(parser)) {
3397 if (retval && parser->tok != '{') {
3398 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
3403 ast_unref(nextthink);
3404 ast_unref(framenum);
3408 has_frame_think = true;
3411 block = ast_block_new(parser_ctx(parser));
3413 parseerror(parser, "failed to allocate block");
3414 if (has_frame_think) {
3415 ast_unref(nextthink);
3416 ast_unref(framenum);
3421 if (has_frame_think) {
3423 ast_expression *self_frame;
3424 ast_expression *self_nextthink;
3425 ast_expression *self_think;
3426 ast_expression *time_plus_1;
3427 ast_store *store_frame;
3428 ast_store *store_nextthink;
3429 ast_store *store_think;
3431 ctx = parser_ctx(parser);
3432 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
3433 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
3434 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
3436 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
3437 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
3439 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
3440 if (self_frame) ast_delete(self_frame);
3441 if (self_nextthink) ast_delete(self_nextthink);
3442 if (self_think) ast_delete(self_think);
3443 if (time_plus_1) ast_delete(time_plus_1);
3449 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
3450 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
3451 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
3454 ast_delete(self_frame);
3457 if (!store_nextthink) {
3458 ast_delete(self_nextthink);
3462 ast_delete(self_think);
3466 if (store_frame) ast_delete(store_frame);
3467 if (store_nextthink) ast_delete(store_nextthink);
3468 if (store_think) ast_delete(store_think);
3471 if (!ast_block_add_expr(block, (ast_expression*)store_frame) ||
3472 !ast_block_add_expr(block, (ast_expression*)store_nextthink) ||
3473 !ast_block_add_expr(block, (ast_expression*)store_think))
3480 parseerror(parser, "failed to generate code for [frame,think]");
3481 ast_unref(nextthink);
3482 ast_unref(framenum);
3488 parser_enterblock(parser);
3490 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
3492 ast_value *param = var->expression.params[parami];
3495 if (param->expression.vtype != TYPE_VECTOR &&
3496 (param->expression.vtype != TYPE_FIELD ||
3497 param->expression.next->expression.vtype != TYPE_VECTOR))
3502 if (!create_vector_members(param, me)) {
3503 ast_block_delete(block);
3507 for (e = 0; e < 3; ++e) {
3508 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
3509 ast_block_collect(block, (ast_expression*)me[e]);
3513 func = ast_function_new(ast_ctx(var), var->name, var);
3515 parseerror(parser, "failed to allocate function for `%s`", var->name);
3516 ast_block_delete(block);
3519 vec_push(parser->functions, func);
3521 parser->function = func;
3522 if (!parse_block_into(parser, block)) {
3523 ast_block_delete(block);
3527 vec_push(func->blocks, block);
3529 parser->function = old;
3530 if (!parser_leaveblock(parser))
3532 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
3533 parseerror(parser, "internal error: local scopes left");
3537 if (parser->tok == ';')
3538 return parser_next(parser);
3539 else if (opts.standard == COMPILER_QCC)
3540 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
3544 vec_pop(parser->functions);
3545 ast_function_delete(func);
3546 var->constval.vfunc = NULL;
3549 (void)!parser_leaveblock(parser);
3550 parser->function = old;
3554 static ast_expression *array_accessor_split(
3559 ast_expression *left,
3560 ast_expression *right
3566 lex_ctx ctx = ast_ctx(array);
3568 if (!left || !right) {
3569 if (left) ast_delete(left);
3570 if (right) ast_delete(right);
3574 cmp = ast_binary_new(ctx, INSTR_LT,
3575 (ast_expression*)index,
3576 (ast_expression*)parser_const_float(parser, middle));
3580 parseerror(parser, "internal error: failed to create comparison for array setter");
3584 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
3586 ast_delete(cmp); /* will delete left and right */
3587 parseerror(parser, "internal error: failed to create conditional jump for array setter");
3591 return (ast_expression*)ifthen;
3594 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
3596 lex_ctx ctx = ast_ctx(array);
3598 if (from+1 == afterend) {
3599 /* set this value */
3602 ast_array_index *subscript;
3604 int assignop = type_store_instr[value->expression.vtype];
3606 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
3607 assignop = INSTR_STORE_V;
3609 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3613 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
3615 ast_delete(subscript);
3619 block = ast_block_new(ctx);
3625 if (!ast_block_add_expr(block, (ast_expression*)st)) {
3630 ret = ast_return_new(ctx, NULL);
3636 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
3641 return (ast_expression*)block;
3643 ast_expression *left, *right;
3644 size_t diff = afterend - from;
3645 size_t middle = from + diff/2;
3646 left = array_setter_node(parser, array, index, value, from, middle);
3647 right = array_setter_node(parser, array, index, value, middle, afterend);
3648 return array_accessor_split(parser, array, index, middle, left, right);
3652 static ast_expression *array_field_setter_node(
3661 lex_ctx ctx = ast_ctx(array);
3663 if (from+1 == afterend) {
3664 /* set this value */
3667 ast_entfield *entfield;
3668 ast_array_index *subscript;
3670 int assignop = type_storep_instr[value->expression.vtype];
3672 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
3673 assignop = INSTR_STOREP_V;
3675 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3679 entfield = ast_entfield_new_force(ctx,
3680 (ast_expression*)entity,
3681 (ast_expression*)subscript,
3682 (ast_expression*)subscript);
3684 ast_delete(subscript);
3688 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
3690 ast_delete(entfield);
3694 block = ast_block_new(ctx);
3700 if (!ast_block_add_expr(block, (ast_expression*)st)) {
3705 ret = ast_return_new(ctx, NULL);
3711 if (!ast_block_add_expr(block, (ast_expression*)ret)) {
3716 return (ast_expression*)block;
3718 ast_expression *left, *right;
3719 size_t diff = afterend - from;
3720 size_t middle = from + diff/2;
3721 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
3722 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
3723 return array_accessor_split(parser, array, index, middle, left, right);
3727 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
3729 lex_ctx ctx = ast_ctx(array);
3731 if (from+1 == afterend) {
3733 ast_array_index *subscript;
3735 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3739 ret = ast_return_new(ctx, (ast_expression*)subscript);
3741 ast_delete(subscript);
3745 return (ast_expression*)ret;
3747 ast_expression *left, *right;
3748 size_t diff = afterend - from;
3749 size_t middle = from + diff/2;
3750 left = array_getter_node(parser, array, index, from, middle);
3751 right = array_getter_node(parser, array, index, middle, afterend);
3752 return array_accessor_split(parser, array, index, middle, left, right);
3756 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3758 ast_function *func = NULL;
3759 ast_value *fval = NULL;
3760 ast_block *body = NULL;
3762 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3764 parseerror(parser, "failed to create accessor function value");
3768 func = ast_function_new(ast_ctx(array), funcname, fval);
3771 parseerror(parser, "failed to create accessor function node");
3775 body = ast_block_new(ast_ctx(array));
3777 parseerror(parser, "failed to create block for array accessor");
3783 vec_push(func->blocks, body);
3786 vec_push(parser->accessors, fval);
3791 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3793 ast_expression *root = NULL;
3794 ast_value *index = NULL;
3795 ast_value *value = NULL;
3799 if (!ast_istype(array->expression.next, ast_value)) {
3800 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3804 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3806 func = fval->constval.vfunc;
3807 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3809 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3810 value = ast_value_copy((ast_value*)array->expression.next);
3812 if (!index || !value) {
3813 parseerror(parser, "failed to create locals for array accessor");
3816 (void)!ast_value_set_name(value, "value"); /* not important */
3817 vec_push(fval->expression.params, index);
3818 vec_push(fval->expression.params, value);
3820 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3822 parseerror(parser, "failed to build accessor search tree");
3826 array->setter = fval;
3827 return ast_block_add_expr(func->blocks[0], root);
3829 if (index) ast_delete(index);
3830 if (value) ast_delete(value);
3831 if (root) ast_delete(root);
3837 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3839 ast_expression *root = NULL;
3840 ast_value *entity = NULL;
3841 ast_value *index = NULL;
3842 ast_value *value = NULL;
3846 if (!ast_istype(array->expression.next, ast_value)) {
3847 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3851 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3853 func = fval->constval.vfunc;
3854 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3856 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3857 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3858 value = ast_value_copy((ast_value*)array->expression.next);
3859 if (!entity || !index || !value) {
3860 parseerror(parser, "failed to create locals for array accessor");
3863 (void)!ast_value_set_name(value, "value"); /* not important */
3864 vec_push(fval->expression.params, entity);
3865 vec_push(fval->expression.params, index);
3866 vec_push(fval->expression.params, value);
3868 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3870 parseerror(parser, "failed to build accessor search tree");
3874 array->setter = fval;
3875 return ast_block_add_expr(func->blocks[0], root);
3877 if (entity) ast_delete(entity);
3878 if (index) ast_delete(index);
3879 if (value) ast_delete(value);
3880 if (root) ast_delete(root);
3886 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3888 ast_expression *root = NULL;
3889 ast_value *index = NULL;
3893 /* NOTE: checking array->expression.next rather than elemtype since
3894 * for fields elemtype is a temporary fieldtype.
3896 if (!ast_istype(array->expression.next, ast_value)) {
3897 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3901 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3903 func = fval->constval.vfunc;
3904 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3906 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3909 parseerror(parser, "failed to create locals for array accessor");
3912 vec_push(fval->expression.params, index);
3914 root = array_getter_node(parser, array, index, 0, array->expression.count);
3916 parseerror(parser, "failed to build accessor search tree");
3920 array->getter = fval;
3921 return ast_block_add_expr(func->blocks[0], root);
3923 if (index) ast_delete(index);
3924 if (root) ast_delete(root);
3930 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3931 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3939 bool variadic = false;
3941 ctx = parser_ctx(parser);
3943 /* for the sake of less code we parse-in in this function */
3944 if (!parser_next(parser)) {
3945 parseerror(parser, "expected parameter list");
3951 /* parse variables until we hit a closing paren */
3952 while (parser->tok != ')') {
3954 /* there must be commas between them */
3955 if (parser->tok != ',') {
3956 parseerror(parser, "expected comma or end of parameter list");
3959 if (!parser_next(parser)) {
3960 parseerror(parser, "expected parameter");
3966 if (parser->tok == TOKEN_DOTS) {
3967 /* '...' indicates a varargs function */
3969 if (!parser_next(parser)) {
3970 parseerror(parser, "expected parameter");
3973 if (parser->tok != ')') {
3974 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3980 /* for anything else just parse a typename */
3981 param = parse_typename(parser, NULL, NULL);
3984 vec_push(params, param);
3985 if (param->expression.vtype >= TYPE_VARIANT) {
3986 char tname[1024]; /* typename is reserved in C++ */
3987 ast_type_to_string((ast_expression*)param, tname, sizeof(tname));
3988 parseerror(parser, "type not supported as part of a parameter list: %s", tname);
3994 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3998 if (vec_size(params) > 8 && opts.standard == COMPILER_QCC)
3999 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
4002 if (!parser_next(parser)) {
4003 parseerror(parser, "parse error after typename");
4007 /* now turn 'var' into a function type */
4008 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
4009 fval->expression.next = (ast_expression*)var;
4011 fval->expression.flags |= AST_FLAG_VARIADIC;
4014 var->expression.params = params;
4021 for (i = 0; i < vec_size(params); ++i)
4022 ast_delete(params[i]);
4027 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
4029 ast_expression *cexp;
4030 ast_value *cval, *tmp;
4033 ctx = parser_ctx(parser);
4035 if (!parser_next(parser)) {
4037 parseerror(parser, "expected array-size");
4041 cexp = parse_expression_leave(parser, true);
4043 if (!cexp || !ast_istype(cexp, ast_value)) {
4047 parseerror(parser, "expected array-size as constant positive integer");
4050 cval = (ast_value*)cexp;
4052 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
4053 tmp->expression.next = (ast_expression*)var;
4056 if (cval->expression.vtype == TYPE_INTEGER)
4057 tmp->expression.count = cval->constval.vint;
4058 else if (cval->expression.vtype == TYPE_FLOAT)
4059 tmp->expression.count = cval->constval.vfloat;
4063 parseerror(parser, "array-size must be a positive integer constant");
4068 if (parser->tok != ']') {
4070 parseerror(parser, "expected ']' after array-size");
4073 if (!parser_next(parser)) {
4075 parseerror(parser, "error after parsing array size");
4081 /* Parse a complete typename.
4082 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
4083 * but when parsing variables separated by comma
4084 * 'storebase' should point to where the base-type should be kept.
4085 * The base type makes up every bit of type information which comes *before* the
4088 * The following will be parsed in its entirety:
4090 * The 'basetype' in this case is 'void()'
4091 * and if there's a comma after it, say:
4093 * then the type-information 'void()' can be stored in 'storebase'
4095 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
4097 ast_value *var, *tmp;
4100 const char *name = NULL;
4101 bool isfield = false;
4102 bool wasarray = false;
4103 size_t morefields = 0;
4105 ctx = parser_ctx(parser);
4107 /* types may start with a dot */
4108 if (parser->tok == '.') {
4110 /* if we parsed a dot we need a typename now */
4111 if (!parser_next(parser)) {
4112 parseerror(parser, "expected typename for field definition");
4116 /* Further dots are handled seperately because they won't be part of the
4119 while (parser->tok == '.') {
4121 if (!parser_next(parser)) {
4122 parseerror(parser, "expected typename for field definition");
4127 if (parser->tok == TOKEN_IDENT)
4128 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
4129 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
4130 parseerror(parser, "expected typename");
4134 /* generate the basic type value */
4135 if (cached_typedef) {
4136 var = ast_value_copy(cached_typedef);
4137 ast_value_set_name(var, "<type(from_def)>");
4139 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
4141 for (; morefields; --morefields) {
4142 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
4143 tmp->expression.next = (ast_expression*)var;
4147 /* do not yet turn into a field - remember:
4148 * .void() foo; is a field too
4149 * .void()() foo; is a function
4153 if (!parser_next(parser)) {
4155 parseerror(parser, "parse error after typename");
4159 /* an opening paren now starts the parameter-list of a function
4160 * this is where original-QC has parameter lists.
4161 * We allow a single parameter list here.
4162 * Much like fteqcc we don't allow `float()() x`
4164 if (parser->tok == '(') {
4165 var = parse_parameter_list(parser, var);
4170 /* store the base if requested */
4172 *storebase = ast_value_copy(var);
4174 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4175 tmp->expression.next = (ast_expression*)*storebase;
4180 /* there may be a name now */
4181 if (parser->tok == TOKEN_IDENT) {
4182 name = util_strdup(parser_tokval(parser));
4184 if (!parser_next(parser)) {
4186 parseerror(parser, "error after variable or field declaration");
4191 /* now this may be an array */
4192 if (parser->tok == '[') {
4194 var = parse_arraysize(parser, var);
4199 /* This is the point where we can turn it into a field */
4201 /* turn it into a field if desired */
4202 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
4203 tmp->expression.next = (ast_expression*)var;
4207 /* now there may be function parens again */
4208 if (parser->tok == '(' && opts.standard == COMPILER_QCC)
4209 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4210 if (parser->tok == '(' && wasarray)
4211 parseerror(parser, "arrays as part of a return type is not supported");
4212 while (parser->tok == '(') {
4213 var = parse_parameter_list(parser, var);
4222 /* finally name it */
4224 if (!ast_value_set_name(var, name)) {
4226 parseerror(parser, "internal error: failed to set name");
4229 /* free the name, ast_value_set_name duplicates */
4236 static bool parse_typedef(parser_t *parser)
4238 ast_value *typevar, *oldtype;
4239 ast_expression *old;
4241 typevar = parse_typename(parser, NULL, NULL);
4246 if ( (old = parser_find_var(parser, typevar->name)) ) {
4247 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
4248 " -> `%s` has been declared here: %s:%i",
4249 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
4250 ast_delete(typevar);
4254 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
4255 parseerror(parser, "type `%s` has already been declared here: %s:%i",
4256 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
4257 ast_delete(typevar);
4261 vec_push(parser->_typedefs, typevar);
4262 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
4264 if (parser->tok != ';') {
4265 parseerror(parser, "expected semicolon after typedef");
4268 if (!parser_next(parser)) {
4269 parseerror(parser, "parse error after typedef");
4276 static const char *cvq_to_str(int cvq) {
4278 case CV_NONE: return "none";
4279 case CV_VAR: return "`var`";
4280 case CV_CONST: return "`const`";
4281 default: return "<INVALID>";
4285 static bool parser_check_qualifiers(parser_t *parser, const ast_value *var, const ast_value *proto)
4288 if (proto->cvq != var->cvq) {
4289 if (!(proto->cvq == CV_CONST && var->cvq == CV_NONE &&
4290 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4291 parser->tok == '='))
4293 return !parsewarning(parser, WARN_DIFFERENT_QUALIFIERS,
4294 "`%s` declared with different qualifiers: %s\n"
4295 " -> previous declaration here: %s:%i uses %s",
4296 var->name, cvq_to_str(var->cvq),
4297 ast_ctx(proto).file, ast_ctx(proto).line,
4298 cvq_to_str(proto->cvq));
4301 av = (var ->expression.flags & AST_FLAG_NORETURN);
4302 ao = (proto->expression.flags & AST_FLAG_NORETURN);
4304 return !parsewarning(parser, WARN_DIFFERENT_ATTRIBUTES,
4305 "`%s` declared with different attributes%s\n"
4306 " -> previous declaration here: %s:%i",
4307 var->name, (av ? ": noreturn" : ""),
4308 ast_ctx(proto).file, ast_ctx(proto).line,
4309 (ao ? ": noreturn" : ""));
4314 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)
4318 ast_expression *old;
4322 ast_value *basetype = NULL;
4324 bool isparam = false;
4325 bool isvector = false;
4326 bool cleanvar = true;
4327 bool wasarray = false;
4331 if (!localblock && is_static)
4332 parseerror(parser, "`static` qualifier is not supported in global scope");
4334 /* get the first complete variable */
4335 var = parse_typename(parser, &basetype, cached_typedef);
4338 ast_delete(basetype);
4346 /* Part 0: finish the type */
4347 if (parser->tok == '(') {
4348 if (opts.standard == COMPILER_QCC)
4349 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4350 var = parse_parameter_list(parser, var);
4356 /* we only allow 1-dimensional arrays */
4357 if (parser->tok == '[') {
4359 var = parse_arraysize(parser, var);
4365 if (parser->tok == '(' && wasarray) {
4366 parseerror(parser, "arrays as part of a return type is not supported");
4367 /* we'll still parse the type completely for now */
4369 /* for functions returning functions */
4370 while (parser->tok == '(') {
4371 if (opts.standard == COMPILER_QCC)
4372 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4373 var = parse_parameter_list(parser, var);
4380 var->cvq = qualifier;
4381 var->expression.flags |= qflags;
4384 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
4385 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
4386 * is then filled with the previous definition and the parameter-names replaced.
4388 if (!strcmp(var->name, "nil")) {
4389 if (OPTS_FLAG(UNTYPED_NIL)) {
4390 if (!localblock || !OPTS_FLAG(PERMISSIVE))
4391 parseerror(parser, "name `nil` not allowed (try -fpermissive)");
4393 (void)!parsewarning(parser, WARN_RESERVED_NAMES, "variable name `nil` is reserved");
4396 /* Deal with end_sys_ vars */
4398 if (!strcmp(var->name, "end_sys_globals")) {
4400 parser->crc_globals = vec_size(parser->globals);
4403 else if (!strcmp(var->name, "end_sys_fields")) {
4405 parser->crc_fields = vec_size(parser->fields);
4408 if (was_end && var->expression.vtype == TYPE_FIELD) {
4409 if (parsewarning(parser, WARN_END_SYS_FIELDS,
4410 "global '%s' hint should not be a field",
4411 parser_tokval(parser)))
4418 if (!nofields && var->expression.vtype == TYPE_FIELD)
4420 /* deal with field declarations */
4421 old = parser_find_field(parser, var->name);
4423 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
4424 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
4433 parseerror(parser, "field `%s` already declared here: %s:%i",
4434 var->name, ast_ctx(old).file, ast_ctx(old).line);
4439 if (opts.standard == COMPILER_QCC &&
4440 (old = parser_find_global(parser, var->name)))
4442 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
4443 parseerror(parser, "field `%s` already declared here: %s:%i",
4444 var->name, ast_ctx(old).file, ast_ctx(old).line);
4451 /* deal with other globals */
4452 old = parser_find_global(parser, var->name);
4453 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
4455 /* This is a function which had a prototype */
4456 if (!ast_istype(old, ast_value)) {
4457 parseerror(parser, "internal error: prototype is not an ast_value");
4461 proto = (ast_value*)old;
4462 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
4463 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
4465 ast_ctx(proto).file, ast_ctx(proto).line);
4469 /* we need the new parameter-names */
4470 for (i = 0; i < vec_size(proto->expression.params); ++i)
4471 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
4472 if (!parser_check_qualifiers(parser, var, proto)) {
4477 proto->expression.flags |= var->expression.flags;
4485 if (opts.standard == COMPILER_GMQCC) {
4486 parseerror(parser, "global `%s` already declared here: %s:%i",
4487 var->name, ast_ctx(old).file, ast_ctx(old).line);
4491 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
4492 "global `%s` already declared here: %s:%i",
4493 var->name, ast_ctx(old).file, ast_ctx(old).line))
4498 proto = (ast_value*)old;
4499 if (!ast_istype(old, ast_value)) {
4500 parseerror(parser, "internal error: not an ast_value");
4505 if (!parser_check_qualifiers(parser, var, proto)) {
4510 proto->expression.flags |= var->expression.flags;
4515 if (opts.standard == COMPILER_QCC &&
4516 (old = parser_find_field(parser, var->name)))
4518 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
4519 parseerror(parser, "global `%s` already declared here: %s:%i",
4520 var->name, ast_ctx(old).file, ast_ctx(old).line);
4527 else /* it's not a global */
4529 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
4530 if (old && !isparam) {
4531 parseerror(parser, "local `%s` already declared here: %s:%i",
4532 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
4536 old = parser_find_local(parser, var->name, 0, &isparam);
4537 if (old && isparam) {
4538 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
4539 "local `%s` is shadowing a parameter", var->name))
4541 parseerror(parser, "local `%s` already declared here: %s:%i",
4542 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
4546 if (opts.standard != COMPILER_GMQCC) {
4554 /* in a noref section we simply bump the usecount */
4555 if (noref || parser->noref)
4559 * Create the global/local, and deal with vector types.
4562 if (var->expression.vtype == TYPE_VECTOR)
4564 else if (var->expression.vtype == TYPE_FIELD &&
4565 var->expression.next->expression.vtype == TYPE_VECTOR)
4569 if (!create_vector_members(var, me)) {
4576 /* deal with global variables, fields, functions */
4577 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
4578 var->isfield = true;
4579 vec_push(parser->fields, (ast_expression*)var);
4580 util_htset(parser->htfields, var->name, var);
4582 for (i = 0; i < 3; ++i) {
4583 vec_push(parser->fields, (ast_expression*)me[i]);
4584 util_htset(parser->htfields, me[i]->name, me[i]);
4589 vec_push(parser->globals, (ast_expression*)var);
4590 util_htset(parser->htglobals, var->name, var);
4592 for (i = 0; i < 3; ++i) {
4593 vec_push(parser->globals, (ast_expression*)me[i]);
4594 util_htset(parser->htglobals, me[i]->name, me[i]);
4600 /* a static adds itself to be generated like any other global
4601 * but is added to the local namespace instead
4603 char *defname = NULL;
4604 size_t prefix_len, ln;
4606 ln = strlen(parser->function->name);
4607 vec_append(defname, ln, parser->function->name);
4609 vec_append(defname, 2, "::");
4610 /* remember the length up to here */
4611 prefix_len = vec_size(defname);
4613 /* Add it to the local scope */
4614 util_htset(vec_last(parser->variables), var->name, (void*)var);
4615 /* now rename the global */
4616 ln = strlen(var->name);
4617 vec_append(defname, ln, var->name);
4618 ast_value_set_name(var, defname);
4620 /* push it to the to-be-generated globals */
4621 vec_push(parser->globals, (ast_expression*)var);
4623 /* same game for the vector members */
4625 for (i = 0; i < 3; ++i) {
4626 util_htset(vec_last(parser->variables), me[i]->name, (void*)(me[i]));
4628 vec_shrinkto(defname, prefix_len);
4629 ln = strlen(me[i]->name);
4630 vec_append(defname, ln, me[i]->name);
4631 ast_member_set_name(me[i], defname);
4633 vec_push(parser->globals, (ast_expression*)me[i]);
4638 vec_push(localblock->locals, var);
4639 parser_addlocal(parser, var->name, (ast_expression*)var);
4641 for (i = 0; i < 3; ++i) {
4642 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
4643 ast_block_collect(localblock, (ast_expression*)me[i]);
4649 me[0] = me[1] = me[2] = NULL;
4654 if (var->expression.vtype == TYPE_ARRAY) {
4656 snprintf(name, sizeof(name), "%s##SET", var->name);
4657 if (!parser_create_array_setter(parser, var, name))
4659 snprintf(name, sizeof(name), "%s##GET", var->name);
4660 if (!parser_create_array_getter(parser, var, var->expression.next, name))
4663 else if (!localblock && !nofields &&
4664 var->expression.vtype == TYPE_FIELD &&
4665 var->expression.next->expression.vtype == TYPE_ARRAY)
4668 ast_expression *telem;
4670 ast_value *array = (ast_value*)var->expression.next;
4672 if (!ast_istype(var->expression.next, ast_value)) {
4673 parseerror(parser, "internal error: field element type must be an ast_value");
4677 snprintf(name, sizeof(name), "%s##SETF", var->name);
4678 if (!parser_create_array_field_setter(parser, array, name))
4681 telem = ast_type_copy(ast_ctx(var), array->expression.next);
4682 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
4683 tfield->expression.next = telem;
4684 snprintf(name, sizeof(name), "%s##GETFP", var->name);
4685 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
4693 if (parser->tok == ';') {
4694 ast_delete(basetype);
4695 if (!parser_next(parser)) {
4696 parseerror(parser, "error after variable declaration");
4702 if (parser->tok == ',')
4706 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
4709 parseerror(parser, "missing comma or semicolon while parsing variables");
4713 if (localblock && opts.standard == COMPILER_QCC) {
4714 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
4715 "initializing expression turns variable `%s` into a constant in this standard",
4722 if (parser->tok != '{') {
4723 if (parser->tok != '=') {
4724 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
4728 if (!parser_next(parser)) {
4729 parseerror(parser, "error parsing initializer");
4733 else if (opts.standard == COMPILER_QCC) {
4734 parseerror(parser, "expected '=' before function body in this standard");
4737 if (parser->tok == '#') {
4738 ast_function *func = NULL;
4741 parseerror(parser, "cannot declare builtins within functions");
4744 if (var->expression.vtype != TYPE_FUNCTION) {
4745 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
4748 if (!parser_next(parser)) {
4749 parseerror(parser, "expected builtin number");
4752 if (parser->tok != TOKEN_INTCONST) {
4753 parseerror(parser, "builtin number must be an integer constant");
4756 if (parser_token(parser)->constval.i < 0) {
4757 parseerror(parser, "builtin number must be an integer greater than zero");
4761 if (var->hasvalue) {
4762 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
4763 "builtin `%s` has already been defined\n"
4764 " -> previous declaration here: %s:%i",
4765 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
4769 func = ast_function_new(ast_ctx(var), var->name, var);
4771 parseerror(parser, "failed to allocate function for `%s`", var->name);
4774 vec_push(parser->functions, func);
4776 func->builtin = -parser_token(parser)->constval.i-1;
4779 if (!parser_next(parser)) {
4780 parseerror(parser, "expected comma or semicolon");
4782 ast_function_delete(func);
4783 var->constval.vfunc = NULL;
4787 else if (parser->tok == '{' || parser->tok == '[')
4790 parseerror(parser, "cannot declare functions within functions");
4795 ast_ctx(proto) = parser_ctx(parser);
4797 if (!parse_function_body(parser, var))
4799 ast_delete(basetype);
4800 for (i = 0; i < vec_size(parser->gotos); ++i)
4801 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
4802 vec_free(parser->gotos);
4803 vec_free(parser->labels);
4806 ast_expression *cexp;
4809 cexp = parse_expression_leave(parser, true);
4814 cval = (ast_value*)cexp;
4815 if (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
4816 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
4819 if (opts.standard != COMPILER_GMQCC &&
4820 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4821 qualifier != CV_VAR)
4823 var->cvq = CV_CONST;
4825 var->hasvalue = true;
4826 if (cval->expression.vtype == TYPE_STRING)
4827 var->constval.vstring = parser_strdup(cval->constval.vstring);
4828 else if (cval->expression.vtype == TYPE_FIELD)
4829 var->constval.vfield = cval;
4831 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
4836 shunt sy = { NULL, NULL };
4839 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
4840 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
4841 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
4842 if (!parser_sy_apply_operator(parser, &sy))
4845 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
4846 parseerror(parser, "internal error: leaked operands");
4847 if (!ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out))
4857 if (parser->tok == ',') {
4858 if (!parser_next(parser)) {
4859 parseerror(parser, "expected another variable");
4863 if (parser->tok != TOKEN_IDENT) {
4864 parseerror(parser, "expected another variable");
4867 var = ast_value_copy(basetype);
4869 ast_value_set_name(var, parser_tokval(parser));
4870 if (!parser_next(parser)) {
4871 parseerror(parser, "error parsing variable declaration");
4877 if (parser->tok != ';') {
4878 parseerror(parser, "missing semicolon after variables");
4882 if (!parser_next(parser)) {
4883 parseerror(parser, "parse error after variable declaration");
4887 ast_delete(basetype);
4891 if (cleanvar && var)
4893 ast_delete(basetype);
4897 ast_delete(basetype);
4898 if (cleanvar && var)
4900 if (me[0]) ast_member_delete(me[0]);
4901 if (me[1]) ast_member_delete(me[1]);
4902 if (me[2]) ast_member_delete(me[2]);
4906 static bool parser_global_statement(parser_t *parser)
4910 bool is_static = false;
4911 uint32_t qflags = 0;
4912 ast_value *istype = NULL;
4914 if (parser->tok == TOKEN_IDENT)
4915 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4917 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4919 return parse_variable(parser, NULL, false, CV_NONE, istype, false, false, 0);
4921 else if (parse_qualifiers(parser, false, &cvq, &noref, &is_static, &qflags))
4923 if (cvq == CV_WRONG)
4925 return parse_variable(parser, NULL, true, cvq, NULL, noref, is_static, qflags);
4927 else if (parser->tok == TOKEN_KEYWORD)
4929 if (!strcmp(parser_tokval(parser), "typedef")) {
4930 if (!parser_next(parser)) {
4931 parseerror(parser, "expected type definition after 'typedef'");
4934 return parse_typedef(parser);
4936 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4939 else if (parser->tok == '#')
4941 return parse_pragma(parser);
4943 else if (parser->tok == '$')
4945 if (!parser_next(parser)) {
4946 parseerror(parser, "parse error");
4952 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4958 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4960 return util_crc16(old, str, strlen(str));
4963 static void progdefs_crc_file(const char *str)
4965 /* write to progdefs.h here */
4969 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4971 old = progdefs_crc_sum(old, str);
4972 progdefs_crc_file(str);
4976 static void generate_checksum(parser_t *parser)
4978 uint16_t crc = 0xFFFF;
4982 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4983 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4985 progdefs_crc_file("\tint\tpad;\n");
4986 progdefs_crc_file("\tint\tofs_return[3];\n");
4987 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4988 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4989 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4990 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4991 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4992 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4993 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4994 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4996 for (i = 0; i < parser->crc_globals; ++i) {
4997 if (!ast_istype(parser->globals[i], ast_value))
4999 value = (ast_value*)(parser->globals[i]);
5000 switch (value->expression.vtype) {
5001 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5002 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5003 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5004 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5006 crc = progdefs_crc_both(crc, "\tint\t");
5009 crc = progdefs_crc_both(crc, value->name);
5010 crc = progdefs_crc_both(crc, ";\n");
5012 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
5013 for (i = 0; i < parser->crc_fields; ++i) {
5014 if (!ast_istype(parser->fields[i], ast_value))
5016 value = (ast_value*)(parser->fields[i]);
5017 switch (value->expression.next->expression.vtype) {
5018 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5019 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5020 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5021 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5023 crc = progdefs_crc_both(crc, "\tint\t");
5026 crc = progdefs_crc_both(crc, value->name);
5027 crc = progdefs_crc_both(crc, ";\n");
5029 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
5034 static parser_t *parser;
5041 parser = (parser_t*)mem_a(sizeof(parser_t));
5045 memset(parser, 0, sizeof(*parser));
5047 for (i = 0; i < operator_count; ++i) {
5048 if (operators[i].id == opid1('=')) {
5049 parser->assign_op = operators+i;
5053 if (!parser->assign_op) {
5054 printf("internal error: initializing parser: failed to find assign operator\n");
5059 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
5060 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
5061 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
5062 vec_push(parser->_blocktypedefs, 0);
5064 empty_ctx.file = "<internal>";
5066 parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
5067 if (OPTS_FLAG(UNTYPED_NIL))
5068 util_htset(parser->htglobals, "nil", (void*)parser->nil);
5072 bool parser_compile()
5074 /* initial lexer/parser state */
5075 parser->lex->flags.noops = true;
5077 if (parser_next(parser))
5079 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
5081 if (!parser_global_statement(parser)) {
5082 if (parser->tok == TOKEN_EOF)
5083 parseerror(parser, "unexpected eof");
5084 else if (compile_errors)
5085 parseerror(parser, "there have been errors, bailing out");
5086 lex_close(parser->lex);
5092 parseerror(parser, "parse error");
5093 lex_close(parser->lex);
5098 lex_close(parser->lex);
5101 return !compile_errors;
5104 bool parser_compile_file(const char *filename)
5106 parser->lex = lex_open(filename);
5108 con_err("failed to open file \"%s\"\n", filename);
5111 return parser_compile();
5114 bool parser_compile_string(const char *name, const char *str, size_t len)
5116 parser->lex = lex_open_string(str, len, name);
5118 con_err("failed to create lexer for string \"%s\"\n", name);
5121 return parser_compile();
5124 void parser_cleanup()
5127 for (i = 0; i < vec_size(parser->accessors); ++i) {
5128 ast_delete(parser->accessors[i]->constval.vfunc);
5129 parser->accessors[i]->constval.vfunc = NULL;
5130 ast_delete(parser->accessors[i]);
5132 for (i = 0; i < vec_size(parser->functions); ++i) {
5133 ast_delete(parser->functions[i]);
5135 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
5136 ast_delete(parser->imm_vector[i]);
5138 for (i = 0; i < vec_size(parser->imm_string); ++i) {
5139 ast_delete(parser->imm_string[i]);
5141 for (i = 0; i < vec_size(parser->imm_float); ++i) {
5142 ast_delete(parser->imm_float[i]);
5144 for (i = 0; i < vec_size(parser->fields); ++i) {
5145 ast_delete(parser->fields[i]);
5147 for (i = 0; i < vec_size(parser->globals); ++i) {
5148 ast_delete(parser->globals[i]);
5150 vec_free(parser->accessors);
5151 vec_free(parser->functions);
5152 vec_free(parser->imm_vector);
5153 vec_free(parser->imm_string);
5154 vec_free(parser->imm_float);
5155 vec_free(parser->globals);
5156 vec_free(parser->fields);
5158 for (i = 0; i < vec_size(parser->variables); ++i)
5159 util_htdel(parser->variables[i]);
5160 vec_free(parser->variables);
5161 vec_free(parser->_blocklocals);
5162 vec_free(parser->_locals);
5164 for (i = 0; i < vec_size(parser->_typedefs); ++i)
5165 ast_delete(parser->_typedefs[i]);
5166 vec_free(parser->_typedefs);
5167 for (i = 0; i < vec_size(parser->typedefs); ++i)
5168 util_htdel(parser->typedefs[i]);
5169 vec_free(parser->typedefs);
5170 vec_free(parser->_blocktypedefs);
5172 vec_free(parser->_block_ctx);
5174 vec_free(parser->labels);
5175 vec_free(parser->gotos);
5176 vec_free(parser->breaks);
5177 vec_free(parser->continues);
5182 bool parser_finish(const char *output)
5188 if (compile_errors) {
5189 con_out("*** there were compile errors\n");
5193 ir = ir_builder_new("gmqcc_out");
5195 con_out("failed to allocate builder\n");
5199 for (i = 0; i < vec_size(parser->fields); ++i) {
5202 if (!ast_istype(parser->fields[i], ast_value))
5204 field = (ast_value*)parser->fields[i];
5205 hasvalue = field->hasvalue;
5206 field->hasvalue = false;
5207 if (!ast_global_codegen((ast_value*)field, ir, true)) {
5208 con_out("failed to generate field %s\n", field->name);
5209 ir_builder_delete(ir);
5214 ast_expression *subtype;
5215 field->hasvalue = true;
5216 subtype = field->expression.next;
5217 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
5218 if (subtype->expression.vtype == TYPE_FIELD)
5219 ifld->fieldtype = subtype->expression.next->expression.vtype;
5220 else if (subtype->expression.vtype == TYPE_FUNCTION)
5221 ifld->outtype = subtype->expression.next->expression.vtype;
5222 (void)!ir_value_set_field(field->ir_v, ifld);
5225 for (i = 0; i < vec_size(parser->globals); ++i) {
5227 if (!ast_istype(parser->globals[i], ast_value))
5229 asvalue = (ast_value*)(parser->globals[i]);
5230 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
5231 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
5232 "unused global: `%s`", asvalue->name);
5234 if (!ast_global_codegen(asvalue, ir, false)) {
5235 con_out("failed to generate global %s\n", asvalue->name);
5236 ir_builder_delete(ir);
5240 for (i = 0; i < vec_size(parser->imm_float); ++i) {
5241 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
5242 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
5243 ir_builder_delete(ir);
5247 for (i = 0; i < vec_size(parser->imm_string); ++i) {
5248 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
5249 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
5250 ir_builder_delete(ir);
5254 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
5255 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
5256 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
5257 ir_builder_delete(ir);
5261 for (i = 0; i < vec_size(parser->globals); ++i) {
5263 if (!ast_istype(parser->globals[i], ast_value))
5265 asvalue = (ast_value*)(parser->globals[i]);
5266 if (asvalue->cvq == CV_CONST && !asvalue->hasvalue)
5267 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_CONSTANT,
5268 "uninitialized constant: `%s`",
5270 else if ((asvalue->cvq == CV_NONE || asvalue->cvq == CV_CONST) && !asvalue->hasvalue)
5271 (void)!compile_warning(ast_ctx(asvalue), WARN_UNINITIALIZED_GLOBAL,
5272 "uninitialized global: `%s`",
5274 if (!ast_generate_accessors(asvalue, ir)) {
5275 ir_builder_delete(ir);
5279 for (i = 0; i < vec_size(parser->fields); ++i) {
5281 asvalue = (ast_value*)(parser->fields[i]->expression.next);
5283 if (!ast_istype((ast_expression*)asvalue, ast_value))
5285 if (asvalue->expression.vtype != TYPE_ARRAY)
5287 if (!ast_generate_accessors(asvalue, ir)) {
5288 ir_builder_delete(ir);
5292 for (i = 0; i < vec_size(parser->functions); ++i) {
5293 if (!ast_function_codegen(parser->functions[i], ir)) {
5294 con_out("failed to generate function %s\n", parser->functions[i]->name);
5295 ir_builder_delete(ir);
5300 ir_builder_dump(ir, con_out);
5301 for (i = 0; i < vec_size(parser->functions); ++i) {
5302 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
5303 con_out("failed to finalize function %s\n", parser->functions[i]->name);
5304 ir_builder_delete(ir);
5309 if (compile_Werrors) {
5310 con_out("*** there were warnings treated as errors\n");
5311 compile_show_werrors();
5317 ir_builder_dump(ir, con_out);
5319 generate_checksum(parser);
5321 if (!ir_builder_generate(ir, output)) {
5322 con_out("*** failed to generate output file\n");
5323 ir_builder_delete(ir);
5328 ir_builder_delete(ir);