2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 512
35 #define TYPEDEF_HT_SIZE 512
37 typedef struct parser_s {
43 ast_expression **globals;
44 ast_expression **fields;
45 ast_function **functions;
46 ast_value **imm_float;
47 ast_value **imm_string;
48 ast_value **imm_vector;
52 ht ht_imm_string_dotranslate;
54 /* must be deleted first, they reference immediates and values */
55 ast_value **accessors;
57 ast_value *imm_float_zero;
58 ast_value *imm_float_one;
59 ast_value *imm_float_neg_one;
61 ast_value *imm_vector_zero;
64 ast_value *reserved_version;
69 ast_function *function;
72 /* All the labels the function defined...
73 * Should they be in ast_function instead?
78 const char **continues;
80 /* A list of hashtables for each scope */
86 /* same as above but for the spelling corrector */
87 correct_trie_t **correct_variables;
88 size_t ***correct_variables_score; /* vector of vector of size_t* */
90 /* not to be used directly, we use the hash table */
91 ast_expression **_locals;
93 ast_value **_typedefs;
94 size_t *_blocktypedefs;
95 lex_ctx_t *_block_ctx;
97 /* we store the '=' operator info */
98 const oper_info *assign_op;
101 ast_value *const_vec[3];
106 /* collected information */
107 size_t max_param_count;
110 static ast_expression * const intrinsic_debug_typestring = (ast_expression*)0x1;
112 static void parser_enterblock(parser_t *parser);
113 static bool parser_leaveblock(parser_t *parser);
114 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
115 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
116 static bool parse_typedef(parser_t *parser);
117 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring);
118 static ast_block* parse_block(parser_t *parser);
119 static bool parse_block_into(parser_t *parser, ast_block *block);
120 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
121 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
122 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels);
123 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels);
124 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
125 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
126 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
128 static void parseerror(parser_t *parser, const char *fmt, ...)
132 vcompile_error(parser->lex->tok.ctx, fmt, ap);
136 /* returns true if it counts as an error */
137 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
142 r = vcompile_warning(parser->lex->tok.ctx, warntype, fmt, ap);
147 /**********************************************************************
148 * some maths used for constant folding
151 vec3_t vec3_add(vec3_t a, vec3_t b)
160 vec3_t vec3_sub(vec3_t a, vec3_t b)
169 qcfloat_t vec3_mulvv(vec3_t a, vec3_t b)
171 return (a.x * b.x + a.y * b.y + a.z * b.z);
174 vec3_t vec3_mulvf(vec3_t a, float b)
183 /**********************************************************************
187 static bool parser_next(parser_t *parser)
189 /* lex_do kills the previous token */
190 parser->tok = lex_do(parser->lex);
191 if (parser->tok == TOKEN_EOF)
193 if (parser->tok >= TOKEN_ERROR) {
194 parseerror(parser, "lex error");
200 #define parser_tokval(p) ((p)->lex->tok.value)
201 #define parser_token(p) (&((p)->lex->tok))
202 #define parser_ctx(p) ((p)->lex->tok.ctx)
204 static ast_value* parser_const_float(parser_t *parser, double d)
209 for (i = 0; i < vec_size(parser->imm_float); ++i) {
210 const double compare = parser->imm_float[i]->constval.vfloat;
211 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
212 return parser->imm_float[i];
215 ctx = parser_ctx(parser);
217 memset(&ctx, 0, sizeof(ctx));
219 out = ast_value_new(ctx, "#IMMEDIATE", TYPE_FLOAT);
221 out->hasvalue = true;
223 out->constval.vfloat = d;
224 vec_push(parser->imm_float, out);
228 static ast_value* parser_const_float_0(parser_t *parser)
230 if (!parser->imm_float_zero)
231 parser->imm_float_zero = parser_const_float(parser, 0);
232 return parser->imm_float_zero;
235 static ast_value* parser_const_float_neg1(parser_t *parser) {
236 if (!parser->imm_float_neg_one)
237 parser->imm_float_neg_one = parser_const_float(parser, -1);
238 return parser->imm_float_neg_one;
241 static ast_value* parser_const_float_1(parser_t *parser)
243 if (!parser->imm_float_one)
244 parser->imm_float_one = parser_const_float(parser, 1);
245 return parser->imm_float_one;
248 static char *parser_strdup(const char *str)
251 /* actually dup empty strings */
252 char *out = (char*)mem_a(1);
256 return util_strdup(str);
259 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
261 ht ht_string = (dotranslate)
262 ? parser->ht_imm_string_dotranslate
263 : parser->ht_imm_string;
266 size_t hash = util_hthash(ht_string, str);
268 if ((out = (ast_value*)util_htgeth(ht_string, str, hash)))
271 for (i = 0; i < vec_size(parser->imm_string); ++i) {
272 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
273 return parser->imm_string[i];
278 util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
279 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
280 out->expression.flags |= AST_FLAG_INCLUDE_DEF;
282 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
285 out->hasvalue = true;
287 out->constval.vstring = parser_strdup(str);
288 vec_push(parser->imm_string, out);
289 util_htseth(ht_string, str, hash, out);
294 static ast_value* parser_const_vector(parser_t *parser, vec3_t v)
298 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
299 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
300 return parser->imm_vector[i];
302 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
304 out->hasvalue = true;
306 out->constval.vvec = v;
307 vec_push(parser->imm_vector, out);
311 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
317 return parser_const_vector(parser, v);
320 static ast_value* parser_const_vector_0(parser_t *parser)
322 if (!parser->imm_vector_zero)
323 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
324 return parser->imm_vector_zero;
327 static ast_expression* parser_find_field(parser_t *parser, const char *name)
329 return ( ast_expression*)util_htget(parser->htfields, name);
332 static ast_expression* parser_find_label(parser_t *parser, const char *name)
335 for(i = 0; i < vec_size(parser->labels); i++)
336 if (!strcmp(parser->labels[i]->name, name))
337 return (ast_expression*)parser->labels[i];
341 static ast_expression* parser_find_global(parser_t *parser, const char *name)
343 ast_expression *var = (ast_expression*)util_htget(parser->aliases, parser_tokval(parser));
346 return (ast_expression*)util_htget(parser->htglobals, name);
349 static ast_expression* parser_find_param(parser_t *parser, const char *name)
353 if (!parser->function)
355 fun = parser->function->vtype;
356 for (i = 0; i < vec_size(fun->expression.params); ++i) {
357 if (!strcmp(fun->expression.params[i]->name, name))
358 return (ast_expression*)(fun->expression.params[i]);
363 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
368 hash = util_hthash(parser->htglobals, name);
371 for (i = vec_size(parser->variables); i > upto;) {
373 if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
377 return parser_find_param(parser, name);
380 static ast_expression* parser_find_var(parser_t *parser, const char *name)
384 v = parser_find_local(parser, name, 0, &dummy);
385 if (!v) v = parser_find_global(parser, name);
389 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
393 hash = util_hthash(parser->typedefs[0], name);
395 for (i = vec_size(parser->typedefs); i > upto;) {
397 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
403 /* include intrinsics */
408 size_t etype; /* 0 = expression, others are operators */
412 ast_block *block; /* for commas and function calls */
431 static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
442 static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
446 e.out = (ast_expression*)v;
453 static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
455 e.etype = 1 + (op - operators);
464 static sy_elem syparen(lex_ctx_t ctx, size_t off) {
475 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
476 * so we need to rotate it to become ent.(foo[n]).
478 static bool rotate_entfield_array_index_nodes(ast_expression **out)
480 ast_array_index *index, *oldindex;
481 ast_entfield *entfield;
485 ast_expression *entity;
487 lex_ctx_t ctx = ast_ctx(*out);
489 if (!ast_istype(*out, ast_array_index))
491 index = (ast_array_index*)*out;
493 if (!ast_istype(index->array, ast_entfield))
495 entfield = (ast_entfield*)index->array;
497 if (!ast_istype(entfield->field, ast_value))
499 field = (ast_value*)entfield->field;
502 entity = entfield->entity;
506 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
507 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
508 *out = (ast_expression*)entfield;
510 oldindex->array = NULL;
511 oldindex->index = NULL;
512 ast_delete(oldindex);
517 static bool immediate_is_true(lex_ctx_t ctx, ast_value *v)
519 switch (v->expression.vtype) {
521 return !!v->constval.vfloat;
523 return !!v->constval.vint;
525 if (OPTS_FLAG(CORRECT_LOGIC))
526 return v->constval.vvec.x &&
527 v->constval.vvec.y &&
530 return !!(v->constval.vvec.x);
532 if (!v->constval.vstring)
534 if (v->constval.vstring && OPTS_FLAG(TRUE_EMPTY_STRINGS))
536 return !!v->constval.vstring[0];
538 compile_error(ctx, "internal error: immediate_is_true on invalid type");
539 return !!v->constval.vfunc;
543 static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
547 ast_expression *out = NULL;
548 ast_expression *exprs[3];
549 ast_block *blocks[3];
550 ast_value *asvalue[3];
551 ast_binstore *asbinstore;
552 size_t i, assignop, addop, subop;
553 qcint_t generated_op = 0;
558 if (!vec_size(sy->ops)) {
559 parseerror(parser, "internal error: missing operator");
563 if (vec_last(sy->ops).isparen) {
564 parseerror(parser, "unmatched parenthesis");
568 op = &operators[vec_last(sy->ops).etype - 1];
569 ctx = vec_last(sy->ops).ctx;
571 if (vec_size(sy->out) < op->operands) {
572 compile_error(ctx, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
573 op->op, (int)op->id);
577 vec_shrinkby(sy->ops, 1);
579 /* op(:?) has no input and no output */
583 vec_shrinkby(sy->out, op->operands);
584 for (i = 0; i < op->operands; ++i) {
585 exprs[i] = sy->out[vec_size(sy->out)+i].out;
586 blocks[i] = sy->out[vec_size(sy->out)+i].block;
587 asvalue[i] = (ast_value*)exprs[i];
589 if (exprs[i]->vtype == TYPE_NOEXPR &&
590 !(i != 0 && op->id == opid2('?',':')) &&
591 !(i == 1 && op->id == opid1('.')))
593 if (ast_istype(exprs[i], ast_label))
594 compile_error(ast_ctx(exprs[i]), "expected expression, got an unknown identifier");
596 compile_error(ast_ctx(exprs[i]), "not an expression");
597 (void)!compile_warning(ast_ctx(exprs[i]), WARN_DEBUG, "expression %u\n", (unsigned int)i);
601 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
602 compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
606 #define NotSameType(T) \
607 (exprs[0]->vtype != exprs[1]->vtype || \
608 exprs[0]->vtype != T)
609 #define CanConstFold1(A) \
610 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && (((ast_value*)(A))->cvq == CV_CONST) &&\
611 (A)->vtype != TYPE_FUNCTION)
612 #define CanConstFold(A, B) \
613 (CanConstFold1(A) && CanConstFold1(B))
614 #define ConstV(i) (asvalue[(i)]->constval.vvec)
615 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
616 #define ConstS(i) (asvalue[(i)]->constval.vstring)
620 compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
624 if (exprs[0]->vtype == TYPE_VECTOR &&
625 exprs[1]->vtype == TYPE_NOEXPR)
627 if (exprs[1] == (ast_expression*)parser->const_vec[0])
628 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
629 else if (exprs[1] == (ast_expression*)parser->const_vec[1])
630 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
631 else if (exprs[1] == (ast_expression*)parser->const_vec[2])
632 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
634 compile_error(ctx, "access to invalid vector component");
638 else if (exprs[0]->vtype == TYPE_ENTITY) {
639 if (exprs[1]->vtype != TYPE_FIELD) {
640 compile_error(ast_ctx(exprs[1]), "type error: right hand of member-operand should be an entity-field");
643 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
645 else if (exprs[0]->vtype == TYPE_VECTOR) {
646 compile_error(ast_ctx(exprs[1]), "vectors cannot be accessed this way");
650 compile_error(ast_ctx(exprs[1]), "type error: member-of operator on something that is not an entity or vector");
656 if (exprs[0]->vtype != TYPE_ARRAY &&
657 !(exprs[0]->vtype == TYPE_FIELD &&
658 exprs[0]->next->vtype == TYPE_ARRAY))
660 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
661 compile_error(ast_ctx(exprs[0]), "cannot index value of type %s", ty1);
664 if (exprs[1]->vtype != TYPE_FLOAT) {
665 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
666 compile_error(ast_ctx(exprs[1]), "index must be of type float, not %s", ty1);
669 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
670 if (rotate_entfield_array_index_nodes(&out))
673 /* This is not broken in fteqcc anymore */
674 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
675 /* this error doesn't need to make us bail out */
676 (void)!parsewarning(parser, WARN_EXTENSIONS,
677 "accessing array-field members of an entity without parenthesis\n"
678 " -> this is an extension from -std=gmqcc");
685 if (vec_size(sy->paren) && vec_last(sy->paren) == PAREN_FUNC) {
686 vec_push(sy->out, syexp(ctx, exprs[0]));
687 vec_push(sy->out, syexp(ctx, exprs[1]));
688 vec_last(sy->argc)++;
692 if (!ast_block_add_expr(blocks[0], exprs[1]))
695 blocks[0] = ast_block_new(ctx);
696 if (!ast_block_add_expr(blocks[0], exprs[0]) ||
697 !ast_block_add_expr(blocks[0], exprs[1]))
702 ast_block_set_type(blocks[0], exprs[1]);
704 vec_push(sy->out, syblock(ctx, blocks[0]));
711 switch (exprs[0]->vtype) {
713 if (CanConstFold1(exprs[0]))
714 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
716 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
717 (ast_expression*)parser_const_float_0(parser),
721 if (CanConstFold1(exprs[0]))
722 out = (ast_expression*)parser_const_vector_f(parser,
723 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
725 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
726 (ast_expression*)parser_const_vector_0(parser),
730 compile_error(ctx, "invalid types used in expression: cannot negate type %s",
731 type_name[exprs[0]->vtype]);
737 switch (exprs[0]->vtype) {
739 if (CanConstFold1(exprs[0]))
740 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
742 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
745 if (CanConstFold1(exprs[0]))
746 out = (ast_expression*)parser_const_float(parser,
747 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
749 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
752 if (CanConstFold1(exprs[0])) {
753 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
754 out = (ast_expression*)parser_const_float(parser, !ConstS(0));
756 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
758 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
759 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
761 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
764 /* we don't constant-fold NOT for these types */
766 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
769 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
772 compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
773 type_name[exprs[0]->vtype]);
779 if (exprs[0]->vtype != exprs[1]->vtype ||
780 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
782 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
783 type_name[exprs[0]->vtype],
784 type_name[exprs[1]->vtype]);
787 switch (exprs[0]->vtype) {
789 if (CanConstFold(exprs[0], exprs[1]))
791 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
794 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
797 if (CanConstFold(exprs[0], exprs[1]))
798 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
800 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
803 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
804 type_name[exprs[0]->vtype],
805 type_name[exprs[1]->vtype]);
810 if (exprs[0]->vtype != exprs[1]->vtype ||
811 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
813 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
814 type_name[exprs[1]->vtype],
815 type_name[exprs[0]->vtype]);
818 switch (exprs[0]->vtype) {
820 if (CanConstFold(exprs[0], exprs[1]))
821 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
823 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
826 if (CanConstFold(exprs[0], exprs[1]))
827 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
829 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
832 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
833 type_name[exprs[1]->vtype],
834 type_name[exprs[0]->vtype]);
839 if (exprs[0]->vtype != exprs[1]->vtype &&
840 !(exprs[0]->vtype == TYPE_VECTOR &&
841 exprs[1]->vtype == TYPE_FLOAT) &&
842 !(exprs[1]->vtype == TYPE_VECTOR &&
843 exprs[0]->vtype == TYPE_FLOAT)
846 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
847 type_name[exprs[1]->vtype],
848 type_name[exprs[0]->vtype]);
851 switch (exprs[0]->vtype) {
853 if (exprs[1]->vtype == TYPE_VECTOR)
855 if (CanConstFold(exprs[0], exprs[1]))
856 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
858 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
862 if (CanConstFold(exprs[0], exprs[1]))
863 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
865 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
869 if (exprs[1]->vtype == TYPE_FLOAT)
871 if (CanConstFold(exprs[0], exprs[1]))
872 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
874 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
878 if (CanConstFold(exprs[0], exprs[1]))
879 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
880 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[0])) {
881 vec3_t vec = ConstV(0);
882 if (!vec.y && !vec.z) { /* 'n 0 0' * v */
883 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
884 out = (ast_expression*)ast_member_new(ctx, exprs[1], 0, NULL);
885 out->node.keep = false;
886 ((ast_member*)out)->rvalue = true;
888 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.x), out);
890 else if (!vec.x && !vec.z) { /* '0 n 0' * v */
891 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
892 out = (ast_expression*)ast_member_new(ctx, exprs[1], 1, NULL);
893 out->node.keep = false;
894 ((ast_member*)out)->rvalue = true;
896 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.y), out);
898 else if (!vec.x && !vec.y) { /* '0 n 0' * v */
899 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
900 out = (ast_expression*)ast_member_new(ctx, exprs[1], 2, NULL);
901 out->node.keep = false;
902 ((ast_member*)out)->rvalue = true;
904 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)parser_const_float(parser, vec.z), out);
907 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
909 else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && CanConstFold1(exprs[1])) {
910 vec3_t vec = ConstV(1);
911 if (!vec.y && !vec.z) { /* v * 'n 0 0' */
912 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
913 out = (ast_expression*)ast_member_new(ctx, exprs[0], 0, NULL);
914 out->node.keep = false;
915 ((ast_member*)out)->rvalue = true;
917 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.x));
919 else if (!vec.x && !vec.z) { /* v * '0 n 0' */
920 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
921 out = (ast_expression*)ast_member_new(ctx, exprs[0], 1, NULL);
922 out->node.keep = false;
923 ((ast_member*)out)->rvalue = true;
925 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.y));
927 else if (!vec.x && !vec.y) { /* v * '0 n 0' */
928 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
929 out = (ast_expression*)ast_member_new(ctx, exprs[0], 2, NULL);
930 out->node.keep = false;
931 ((ast_member*)out)->rvalue = true;
933 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, out, (ast_expression*)parser_const_float(parser, vec.z));
936 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
939 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
943 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
944 type_name[exprs[1]->vtype],
945 type_name[exprs[0]->vtype]);
950 if (exprs[1]->vtype != TYPE_FLOAT) {
951 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
952 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
953 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
956 if (exprs[0]->vtype == TYPE_FLOAT) {
957 if (CanConstFold(exprs[0], exprs[1]))
958 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
960 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
962 else if (exprs[0]->vtype == TYPE_VECTOR) {
963 if (CanConstFold(exprs[0], exprs[1]))
964 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), 1.0/ConstF(1)));
966 if (CanConstFold1(exprs[1])) {
967 out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1));
969 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
970 (ast_expression*)parser_const_float_1(parser),
974 compile_error(ctx, "internal error: failed to generate division");
977 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], out);
982 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
983 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
984 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
990 if (NotSameType(TYPE_FLOAT)) {
991 compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
992 type_name[exprs[0]->vtype],
993 type_name[exprs[1]->vtype]);
996 if (CanConstFold(exprs[0], exprs[1])) {
997 out = (ast_expression*)parser_const_float(parser,
998 (float)(((qcint_t)ConstF(0)) % ((qcint_t)ConstF(1))));
1000 /* generate a call to __builtin_mod */
1001 ast_expression *mod = intrin_func(parser, "mod");
1002 ast_call *call = NULL;
1003 if (!mod) return false; /* can return null for missing floor */
1005 call = ast_call_new(parser_ctx(parser), mod);
1006 vec_push(call->params, exprs[0]);
1007 vec_push(call->params, exprs[1]);
1009 out = (ast_expression*)call;
1013 case opid2('%','='):
1014 compile_error(ctx, "%= is unimplemented");
1019 if (NotSameType(TYPE_FLOAT)) {
1020 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
1021 type_name[exprs[0]->vtype],
1022 type_name[exprs[1]->vtype]);
1025 if (CanConstFold(exprs[0], exprs[1]))
1026 out = (ast_expression*)parser_const_float(parser,
1027 (op->id == opid1('|') ? (float)( ((qcint_t)ConstF(0)) | ((qcint_t)ConstF(1)) ) :
1028 (float)( ((qcint_t)ConstF(0)) & ((qcint_t)ConstF(1)) ) ));
1030 out = (ast_expression*)ast_binary_new(ctx,
1031 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
1032 exprs[0], exprs[1]);
1036 * Okay lets designate what the hell is an acceptable use
1037 * of the ^ operator. In many vector processing units, XOR
1038 * is allowed to be used on vectors, but only if the first
1039 * operand is a vector, the second operand can be a float
1040 * or vector. It's never legal for the first operand to be
1041 * a float, and then the following operand to be a vector.
1042 * Further more, the only time it is legal to do XOR otherwise
1043 * is when both operand are floats. This nicely crafted if
1044 * statement catches them all.
1046 * In the event that the first operand is a vector, two
1047 * possible situations can arise, thus, each element of
1048 * vector A (operand A) is exclusive-ORed with the corresponding
1049 * element of vector B (operand B), If B is scalar, the
1050 * scalar value is first replicated for each element.
1052 * The QCVM itself lacks a BITXOR instruction. Thus emulating
1053 * the mathematics of it is required. The following equation
1054 * is used: (LHS | RHS) & ~(LHS & RHS). However, due to the
1055 * QCVM also lacking a BITNEG instruction, we need to emulate
1056 * ~FOO with -1 - FOO, the whole process becoming this nicely
1057 * crafted expression: (LHS | RHS) & (-1 - (LHS & RHS)).
1059 * When A is not scalar, this process is repeated for all
1060 * components of vector A with the value in operand B,
1061 * only if operand B is scalar. When A is not scalar, and B
1062 * is also not scalar, this process is repeated for all
1063 * components of the vector A with the components of vector B.
1064 * Finally when A is scalar and B is scalar, this process is
1065 * simply used once for A and B being LHS and RHS respectfully.
1067 * Yes the semantics are a bit strange (no pun intended).
1068 * But then again BITXOR is strange itself, consdering it's
1069 * commutative, assocative, and elements of the BITXOR operation
1070 * are their own inverse.
1072 if ( !(exprs[0]->vtype == TYPE_FLOAT && exprs[1]->vtype == TYPE_FLOAT) &&
1073 !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_FLOAT) &&
1074 !(exprs[0]->vtype == TYPE_VECTOR && exprs[1]->vtype == TYPE_VECTOR))
1076 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
1077 type_name[exprs[0]->vtype],
1078 type_name[exprs[1]->vtype]);
1083 * IF the first expression is float, the following will be too
1084 * since scalar ^ vector is not allowed.
1086 if (exprs[0]->vtype == TYPE_FLOAT) {
1087 if(CanConstFold(exprs[0], exprs[1])) {
1088 out = (ast_expression*)parser_const_float(parser, (float)((qcint_t)(ConstF(0)) ^ ((qcint_t)(ConstF(1)))));
1090 ast_binary *expr = ast_binary_new(
1093 (ast_expression*)parser_const_float_neg1(parser),
1094 (ast_expression*)ast_binary_new(
1101 expr->refs = AST_REF_NONE;
1103 out = (ast_expression*)
1107 (ast_expression*)ast_binary_new(
1113 (ast_expression*)expr
1118 * The first is a vector: vector is allowed to xor with vector and
1119 * with scalar, branch here for the second operand.
1121 if (exprs[1]->vtype == TYPE_VECTOR) {
1123 * Xor all the values of the vector components against the
1124 * vectors components in question.
1126 if (CanConstFold(exprs[0], exprs[1])) {
1127 out = (ast_expression*)parser_const_vector_f(
1129 (float)(((qcint_t)(ConstV(0).x)) ^ ((qcint_t)(ConstV(1).x))),
1130 (float)(((qcint_t)(ConstV(0).y)) ^ ((qcint_t)(ConstV(1).y))),
1131 (float)(((qcint_t)(ConstV(0).z)) ^ ((qcint_t)(ConstV(1).z)))
1134 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against vector");
1139 * Xor all the values of the vector components against the
1140 * scalar in question.
1142 if (CanConstFold(exprs[0], exprs[1])) {
1143 out = (ast_expression*)parser_const_vector_f(
1145 (float)(((qcint_t)(ConstV(0).x)) ^ ((qcint_t)(ConstF(1)))),
1146 (float)(((qcint_t)(ConstV(0).y)) ^ ((qcint_t)(ConstF(1)))),
1147 (float)(((qcint_t)(ConstV(0).z)) ^ ((qcint_t)(ConstF(1))))
1150 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor for vector against float");
1158 case opid2('<','<'):
1159 case opid2('>','>'):
1160 if (CanConstFold(exprs[0], exprs[1]) && ! NotSameType(TYPE_FLOAT)) {
1161 if (op->id == opid2('<','<'))
1162 out = (ast_expression*)parser_const_float(parser, (double)((unsigned int)(ConstF(0)) << (unsigned int)(ConstF(1))));
1164 out = (ast_expression*)parser_const_float(parser, (double)((unsigned int)(ConstF(0)) >> (unsigned int)(ConstF(1))));
1167 case opid3('<','<','='):
1168 case opid3('>','>','='):
1169 compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-shifts");
1172 case opid2('|','|'):
1173 generated_op += 1; /* INSTR_OR */
1174 case opid2('&','&'):
1175 generated_op += INSTR_AND;
1176 if (CanConstFold(exprs[0], exprs[1]))
1178 if (OPTS_FLAG(PERL_LOGIC)) {
1179 if (immediate_is_true(ctx, asvalue[0]))
1183 out = (ast_expression*)parser_const_float(parser,
1184 ( (generated_op == INSTR_OR)
1185 ? (immediate_is_true(ctx, asvalue[0]) || immediate_is_true(ctx, asvalue[1]))
1186 : (immediate_is_true(ctx, asvalue[0]) && immediate_is_true(ctx, asvalue[1])) )
1191 if (OPTS_FLAG(PERL_LOGIC) && !ast_compare_type(exprs[0], exprs[1])) {
1192 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1193 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1194 compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
1197 for (i = 0; i < 2; ++i) {
1198 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->vtype == TYPE_VECTOR) {
1199 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[i]);
1201 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
1203 exprs[i] = out; out = NULL;
1204 if (OPTS_FLAG(PERL_LOGIC)) {
1205 /* here we want to keep the right expressions' type */
1209 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->vtype == TYPE_STRING) {
1210 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[i]);
1212 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, out);
1214 exprs[i] = out; out = NULL;
1215 if (OPTS_FLAG(PERL_LOGIC)) {
1216 /* here we want to keep the right expressions' type */
1221 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
1225 case opid2('?',':'):
1226 if (vec_last(sy->paren) != PAREN_TERNARY2) {
1227 compile_error(ctx, "mismatched parenthesis/ternary");
1231 if (!ast_compare_type(exprs[1], exprs[2])) {
1232 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
1233 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
1234 compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
1237 if (CanConstFold1(exprs[0]))
1238 out = (immediate_is_true(ctx, asvalue[0]) ? exprs[1] : exprs[2]);
1240 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
1243 case opid2('*', '*'):
1244 if (NotSameType(TYPE_FLOAT)) {
1245 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1246 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1247 compile_error(ctx, "invalid types used in exponentiation: %s and %s",
1253 if (CanConstFold(exprs[0], exprs[1])) {
1254 out = (ast_expression*)parser_const_float(parser, powf(ConstF(0), ConstF(1)));
1256 ast_call *gencall = ast_call_new(parser_ctx(parser), intrin_func(parser, "pow"));
1257 vec_push(gencall->params, exprs[0]);
1258 vec_push(gencall->params, exprs[1]);
1259 out = (ast_expression*)gencall;
1263 case opid3('<','=','>'): /* -1, 0, or 1 */
1264 if (NotSameType(TYPE_FLOAT)) {
1265 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1266 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1267 compile_error(ctx, "invalid types used in comparision: %s and %s",
1273 if (CanConstFold(exprs[0], exprs[1])) {
1274 if (ConstF(0) < ConstF(1))
1275 out = (ast_expression*)parser_const_float_neg1(parser);
1276 else if (ConstF(0) == ConstF(1))
1277 out = (ast_expression*)parser_const_float_0(parser);
1278 else if (ConstF(0) > ConstF(1))
1279 out = (ast_expression*)parser_const_float_1(parser);
1281 ast_binary *eq = ast_binary_new(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
1283 eq->refs = AST_REF_NONE;
1286 out = (ast_expression*)ast_ternary_new(ctx,
1287 (ast_expression*)ast_binary_new(ctx, INSTR_LT, exprs[0], exprs[1]),
1289 (ast_expression*)parser_const_float_neg1(parser),
1292 (ast_expression*)ast_ternary_new(ctx, (ast_expression*)eq,
1294 (ast_expression*)parser_const_float_0(parser),
1297 (ast_expression*)parser_const_float_1(parser)
1307 generated_op += 1; /* INSTR_GT */
1309 generated_op += 1; /* INSTR_LT */
1310 case opid2('>', '='):
1311 generated_op += 1; /* INSTR_GE */
1312 case opid2('<', '='):
1313 generated_op += INSTR_LE;
1314 if (NotSameType(TYPE_FLOAT)) {
1315 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1316 type_name[exprs[0]->vtype],
1317 type_name[exprs[1]->vtype]);
1320 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
1322 case opid2('!', '='):
1323 if (exprs[0]->vtype != exprs[1]->vtype) {
1324 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1325 type_name[exprs[0]->vtype],
1326 type_name[exprs[1]->vtype]);
1329 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->vtype], exprs[0], exprs[1]);
1331 case opid2('=', '='):
1332 if (exprs[0]->vtype != exprs[1]->vtype) {
1333 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
1334 type_name[exprs[0]->vtype],
1335 type_name[exprs[1]->vtype]);
1338 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->vtype], exprs[0], exprs[1]);
1342 if (ast_istype(exprs[0], ast_entfield)) {
1343 ast_expression *field = ((ast_entfield*)exprs[0])->field;
1344 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1345 exprs[0]->vtype == TYPE_FIELD &&
1346 exprs[0]->next->vtype == TYPE_VECTOR)
1348 assignop = type_storep_instr[TYPE_VECTOR];
1351 assignop = type_storep_instr[exprs[0]->vtype];
1352 if (assignop == VINSTR_END || !ast_compare_type(field->next, exprs[1]))
1354 ast_type_to_string(field->next, ty1, sizeof(ty1));
1355 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1356 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1357 field->next->vtype == TYPE_FUNCTION &&
1358 exprs[1]->vtype == TYPE_FUNCTION)
1360 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
1361 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1364 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1369 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
1370 exprs[0]->vtype == TYPE_FIELD &&
1371 exprs[0]->next->vtype == TYPE_VECTOR)
1373 assignop = type_store_instr[TYPE_VECTOR];
1376 assignop = type_store_instr[exprs[0]->vtype];
1379 if (assignop == VINSTR_END) {
1380 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1381 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1382 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1384 else if (!ast_compare_type(exprs[0], exprs[1]))
1386 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1387 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1388 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
1389 exprs[0]->vtype == TYPE_FUNCTION &&
1390 exprs[1]->vtype == TYPE_FUNCTION)
1392 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
1393 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1396 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
1399 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1400 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1402 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
1404 case opid3('+','+','P'):
1405 case opid3('-','-','P'):
1407 if (exprs[0]->vtype != TYPE_FLOAT) {
1408 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1409 compile_error(ast_ctx(exprs[0]), "invalid type for prefix increment: %s", ty1);
1412 if (op->id == opid3('+','+','P'))
1413 addop = INSTR_ADD_F;
1415 addop = INSTR_SUB_F;
1416 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1417 compile_error(ast_ctx(exprs[0]), "assignment to constant `%s`", asvalue[0]->name);
1419 if (ast_istype(exprs[0], ast_entfield)) {
1420 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1422 (ast_expression*)parser_const_float_1(parser));
1424 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1426 (ast_expression*)parser_const_float_1(parser));
1429 case opid3('S','+','+'):
1430 case opid3('S','-','-'):
1432 if (exprs[0]->vtype != TYPE_FLOAT) {
1433 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1434 compile_error(ast_ctx(exprs[0]), "invalid type for suffix increment: %s", ty1);
1437 if (op->id == opid3('S','+','+')) {
1438 addop = INSTR_ADD_F;
1439 subop = INSTR_SUB_F;
1441 addop = INSTR_SUB_F;
1442 subop = INSTR_ADD_F;
1444 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1445 compile_error(ast_ctx(exprs[0]), "assignment to constant `%s`", asvalue[0]->name);
1447 if (ast_istype(exprs[0], ast_entfield)) {
1448 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1450 (ast_expression*)parser_const_float_1(parser));
1452 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1454 (ast_expression*)parser_const_float_1(parser));
1458 out = (ast_expression*)ast_binary_new(ctx, subop,
1460 (ast_expression*)parser_const_float_1(parser));
1462 case opid2('+','='):
1463 case opid2('-','='):
1464 if (exprs[0]->vtype != exprs[1]->vtype ||
1465 (exprs[0]->vtype != TYPE_VECTOR && exprs[0]->vtype != TYPE_FLOAT) )
1467 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1468 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1469 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1473 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1474 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1476 if (ast_istype(exprs[0], ast_entfield))
1477 assignop = type_storep_instr[exprs[0]->vtype];
1479 assignop = type_store_instr[exprs[0]->vtype];
1480 switch (exprs[0]->vtype) {
1482 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1483 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1484 exprs[0], exprs[1]);
1487 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1488 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1489 exprs[0], exprs[1]);
1492 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1493 type_name[exprs[0]->vtype],
1494 type_name[exprs[1]->vtype]);
1498 case opid2('*','='):
1499 case opid2('/','='):
1500 if (exprs[1]->vtype != TYPE_FLOAT ||
1501 !(exprs[0]->vtype == TYPE_FLOAT ||
1502 exprs[0]->vtype == TYPE_VECTOR))
1504 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1505 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1506 compile_error(ctx, "invalid types used in expression: %s and %s",
1510 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1511 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1513 if (ast_istype(exprs[0], ast_entfield))
1514 assignop = type_storep_instr[exprs[0]->vtype];
1516 assignop = type_store_instr[exprs[0]->vtype];
1517 switch (exprs[0]->vtype) {
1519 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1520 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1521 exprs[0], exprs[1]);
1524 if (op->id == opid2('*','=')) {
1525 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1526 exprs[0], exprs[1]);
1528 /* there's no DIV_VF */
1529 if (CanConstFold1(exprs[1])) {
1530 out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1));
1532 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1533 (ast_expression*)parser_const_float_1(parser),
1537 compile_error(ctx, "internal error: failed to generate division");
1540 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1545 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1546 type_name[exprs[0]->vtype],
1547 type_name[exprs[1]->vtype]);
1551 case opid2('&','='):
1552 case opid2('|','='):
1553 if (NotSameType(TYPE_FLOAT)) {
1554 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1555 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1556 compile_error(ctx, "invalid types used in expression: %s and %s",
1560 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1561 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1563 if (ast_istype(exprs[0], ast_entfield))
1564 assignop = type_storep_instr[exprs[0]->vtype];
1566 assignop = type_store_instr[exprs[0]->vtype];
1567 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1568 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1569 exprs[0], exprs[1]);
1571 case opid3('&','~','='):
1572 /* This is like: a &= ~(b);
1573 * But QC has no bitwise-not, so we implement it as
1576 if (NotSameType(TYPE_FLOAT)) {
1577 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1578 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1579 compile_error(ctx, "invalid types used in expression: %s and %s",
1583 if (ast_istype(exprs[0], ast_entfield))
1584 assignop = type_storep_instr[exprs[0]->vtype];
1586 assignop = type_store_instr[exprs[0]->vtype];
1587 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1590 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1591 compile_error(ctx, "assignment to constant `%s`", asvalue[0]->name);
1593 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1594 asbinstore->keep_dest = true;
1595 out = (ast_expression*)asbinstore;
1598 case opid2('~', 'P'):
1599 if (exprs[0]->vtype != TYPE_FLOAT) {
1600 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1601 compile_error(ast_ctx(exprs[0]), "invalid type for bit not: %s", ty1);
1605 if(CanConstFold1(exprs[0]))
1606 out = (ast_expression*)parser_const_float(parser, ~(qcint_t)ConstF(0));
1608 out = (ast_expression*)
1609 ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]);
1615 compile_error(ctx, "failed to apply operator %s", op->op);
1619 vec_push(sy->out, syexp(ctx, out));
1623 static bool parser_close_call(parser_t *parser, shunt *sy)
1625 /* was a function call */
1626 ast_expression *fun;
1627 ast_value *funval = NULL;
1631 size_t paramcount, i;
1633 fid = vec_last(sy->ops).off;
1634 vec_shrinkby(sy->ops, 1);
1636 /* out[fid] is the function
1637 * everything above is parameters...
1639 if (!vec_size(sy->argc)) {
1640 parseerror(parser, "internal error: no argument counter available");
1644 paramcount = vec_last(sy->argc);
1647 if (vec_size(sy->out) < fid) {
1648 parseerror(parser, "internal error: broken function call%lu < %lu+%lu\n",
1649 (unsigned long)vec_size(sy->out),
1651 (unsigned long)paramcount);
1655 fun = sy->out[fid].out;
1657 if (fun == intrinsic_debug_typestring) {
1659 if (fid+2 != vec_size(sy->out) ||
1660 vec_last(sy->out).block)
1662 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1665 ast_type_to_string(vec_last(sy->out).out, ty, sizeof(ty));
1666 ast_unref(vec_last(sy->out).out);
1667 sy->out[fid] = syexp(ast_ctx(vec_last(sy->out).out),
1668 (ast_expression*)parser_const_string(parser, ty, false));
1669 vec_shrinkby(sy->out, 1);
1673 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1677 if (fid+1 < vec_size(sy->out))
1680 if (fid+1 + paramcount != vec_size(sy->out)) {
1681 parseerror(parser, "internal error: parameter count mismatch: (%lu+1+%lu), %lu",
1682 (unsigned long)fid, (unsigned long)paramcount, (unsigned long)vec_size(sy->out));
1686 for (i = 0; i < paramcount; ++i)
1687 vec_push(call->params, sy->out[fid+1 + i].out);
1688 vec_shrinkby(sy->out, paramcount);
1689 (void)!ast_call_check_types(call, parser->function->vtype->expression.varparam);
1690 if (parser->max_param_count < paramcount)
1691 parser->max_param_count = paramcount;
1693 if (ast_istype(fun, ast_value)) {
1694 funval = (ast_value*)fun;
1695 if ((fun->flags & AST_FLAG_VARIADIC) &&
1696 !(/*funval->cvq == CV_CONST && */ funval->hasvalue && funval->constval.vfunc->builtin))
1698 call->va_count = (ast_expression*)parser_const_float(parser, (double)paramcount);
1702 /* overwrite fid, the function, with a call */
1703 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1705 if (fun->vtype != TYPE_FUNCTION) {
1706 parseerror(parser, "not a function (%s)", type_name[fun->vtype]);
1711 parseerror(parser, "could not determine function return type");
1714 ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1716 if (fun->flags & AST_FLAG_DEPRECATED) {
1718 return !parsewarning(parser, WARN_DEPRECATED,
1719 "call to function (which is marked deprecated)\n",
1720 "-> it has been declared here: %s:%i",
1721 ast_ctx(fun).file, ast_ctx(fun).line);
1724 return !parsewarning(parser, WARN_DEPRECATED,
1725 "call to `%s` (which is marked deprecated)\n"
1726 "-> `%s` declared here: %s:%i",
1727 fval->name, fval->name, ast_ctx(fun).file, ast_ctx(fun).line);
1729 return !parsewarning(parser, WARN_DEPRECATED,
1730 "call to `%s` (deprecated: %s)\n"
1731 "-> `%s` declared here: %s:%i",
1732 fval->name, fval->desc, fval->name, ast_ctx(fun).file,
1736 if (vec_size(fun->params) != paramcount &&
1737 !((fun->flags & AST_FLAG_VARIADIC) &&
1738 vec_size(fun->params) < paramcount))
1740 const char *fewmany = (vec_size(fun->params) > paramcount) ? "few" : "many";
1742 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1743 "too %s parameters for call to %s: expected %i, got %i\n"
1744 " -> `%s` has been declared here: %s:%i",
1745 fewmany, fval->name, (int)vec_size(fun->params), (int)paramcount,
1746 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1748 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1749 "too %s parameters for function call: expected %i, got %i\n"
1750 " -> it has been declared here: %s:%i",
1751 fewmany, (int)vec_size(fun->params), (int)paramcount,
1752 ast_ctx(fun).file, (int)ast_ctx(fun).line);
1759 static bool parser_close_paren(parser_t *parser, shunt *sy)
1761 if (!vec_size(sy->ops)) {
1762 parseerror(parser, "unmatched closing paren");
1766 while (vec_size(sy->ops)) {
1767 if (vec_last(sy->ops).isparen) {
1768 if (vec_last(sy->paren) == PAREN_FUNC) {
1770 if (!parser_close_call(parser, sy))
1774 if (vec_last(sy->paren) == PAREN_EXPR) {
1776 if (!vec_size(sy->out)) {
1777 compile_error(vec_last(sy->ops).ctx, "empty paren expression");
1778 vec_shrinkby(sy->ops, 1);
1781 vec_shrinkby(sy->ops, 1);
1784 if (vec_last(sy->paren) == PAREN_INDEX) {
1786 /* pop off the parenthesis */
1787 vec_shrinkby(sy->ops, 1);
1788 /* then apply the index operator */
1789 if (!parser_sy_apply_operator(parser, sy))
1793 if (vec_last(sy->paren) == PAREN_TERNARY1) {
1794 vec_last(sy->paren) = PAREN_TERNARY2;
1795 /* pop off the parenthesis */
1796 vec_shrinkby(sy->ops, 1);
1799 compile_error(vec_last(sy->ops).ctx, "invalid parenthesis");
1802 if (!parser_sy_apply_operator(parser, sy))
1808 static void parser_reclassify_token(parser_t *parser)
1811 if (parser->tok >= TOKEN_START)
1813 for (i = 0; i < operator_count; ++i) {
1814 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1815 parser->tok = TOKEN_OPERATOR;
1821 static ast_expression* parse_vararg_do(parser_t *parser)
1823 ast_expression *idx, *out;
1825 ast_value *funtype = parser->function->vtype;
1826 lex_ctx_t ctx = parser_ctx(parser);
1828 if (!parser->function->varargs) {
1829 parseerror(parser, "function has no variable argument list");
1833 if (!parser_next(parser) || parser->tok != '(') {
1834 parseerror(parser, "expected parameter index and type in parenthesis");
1837 if (!parser_next(parser)) {
1838 parseerror(parser, "error parsing parameter index");
1842 idx = parse_expression_leave(parser, true, false, false);
1846 if (parser->tok != ',') {
1847 if (parser->tok != ')') {
1849 parseerror(parser, "expected comma after parameter index");
1852 /* vararg piping: ...(start) */
1853 out = (ast_expression*)ast_argpipe_new(ctx, idx);
1857 if (!parser_next(parser) || (parser->tok != TOKEN_IDENT && parser->tok != TOKEN_TYPENAME)) {
1859 parseerror(parser, "expected typename for vararg");
1863 typevar = parse_typename(parser, NULL, NULL);
1869 if (parser->tok != ')') {
1871 ast_delete(typevar);
1872 parseerror(parser, "expected closing paren");
1876 if (funtype->expression.varparam &&
1877 !ast_compare_type((ast_expression*)typevar, (ast_expression*)funtype->expression.varparam))
1881 ast_type_to_string((ast_expression*)typevar, ty1, sizeof(ty1));
1882 ast_type_to_string((ast_expression*)funtype->expression.varparam, ty2, sizeof(ty2));
1883 compile_error(ast_ctx(typevar),
1884 "function was declared to take varargs of type `%s`, requested type is: %s",
1888 out = (ast_expression*)ast_array_index_new(ctx, (ast_expression*)(parser->function->varargs), idx);
1889 ast_type_adopt(out, typevar);
1890 ast_delete(typevar);
1894 static ast_expression* parse_vararg(parser_t *parser)
1896 bool old_noops = parser->lex->flags.noops;
1898 ast_expression *out;
1900 parser->lex->flags.noops = true;
1901 out = parse_vararg_do(parser);
1903 parser->lex->flags.noops = old_noops;
1907 /* not to be exposed */
1908 extern bool ftepp_predef_exists(const char *name);
1910 static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
1912 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1913 parser->tok == TOKEN_IDENT &&
1914 !strcmp(parser_tokval(parser), "_"))
1916 /* a translatable string */
1919 parser->lex->flags.noops = true;
1920 if (!parser_next(parser) || parser->tok != '(') {
1921 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1924 parser->lex->flags.noops = false;
1925 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1926 parseerror(parser, "expected a constant string in translatable-string extension");
1929 val = parser_const_string(parser, parser_tokval(parser), true);
1932 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1934 if (!parser_next(parser) || parser->tok != ')') {
1935 parseerror(parser, "expected closing paren after translatable string");
1940 else if (parser->tok == TOKEN_DOTS)
1943 if (!OPTS_FLAG(VARIADIC_ARGS)) {
1944 parseerror(parser, "cannot access varargs (try -fvariadic-args)");
1947 va = parse_vararg(parser);
1950 vec_push(sy->out, syexp(parser_ctx(parser), va));
1953 else if (parser->tok == TOKEN_FLOATCONST) {
1955 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1958 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1961 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1963 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1966 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1969 else if (parser->tok == TOKEN_STRINGCONST) {
1971 val = parser_const_string(parser, parser_tokval(parser), false);
1974 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1977 else if (parser->tok == TOKEN_VECTORCONST) {
1979 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1982 vec_push(sy->out, syexp(parser_ctx(parser), (ast_expression*)val));
1985 else if (parser->tok == TOKEN_IDENT)
1987 const char *ctoken = parser_tokval(parser);
1988 ast_expression *prev = vec_size(sy->out) ? vec_last(sy->out).out : NULL;
1989 ast_expression *var;
1990 /* a_vector.{x,y,z} */
1991 if (!vec_size(sy->ops) ||
1992 !vec_last(sy->ops).etype ||
1993 operators[vec_last(sy->ops).etype-1].id != opid1('.') ||
1994 (prev >= intrinsic_debug_typestring &&
1995 prev <= intrinsic_debug_typestring))
1997 /* When adding more intrinsics, fix the above condition */
2000 if (prev && prev->vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
2002 var = (ast_expression*)parser->const_vec[ctoken[0]-'x'];
2004 var = parser_find_var(parser, parser_tokval(parser));
2006 var = parser_find_field(parser, parser_tokval(parser));
2008 if (!var && with_labels) {
2009 var = (ast_expression*)parser_find_label(parser, parser_tokval(parser));
2011 ast_label *lbl = ast_label_new(parser_ctx(parser), parser_tokval(parser), true);
2012 var = (ast_expression*)lbl;
2013 vec_push(parser->labels, lbl);
2016 if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
2017 var = (ast_expression*)parser_const_string(parser, parser->function->name, false);
2020 if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
2021 var = (ast_expression*)intrinsic_debug_typestring;
2023 /* now we try for the real intrinsic hashtable. If the string
2024 * begins with __builtin, we simply skip past it, otherwise we
2025 * use the identifier as is.
2027 else if (!strncmp(parser_tokval(parser), "__builtin_", 10)) {
2028 var = intrin_func(parser, parser_tokval(parser) + 10 /* skip __builtin */);
2032 char *correct = NULL;
2036 * sometimes people use preprocessing predefs without enabling them
2037 * i've done this thousands of times already myself. Lets check for
2038 * it in the predef table. And diagnose it better :)
2040 if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
2041 parseerror(parser, "unexpected identifier: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
2046 * TODO: determine the best score for the identifier: be it
2047 * a variable, a field.
2049 * We should also consider adding correction tables for
2050 * other things as well.
2052 if (OPTS_OPTION_BOOL(OPTION_CORRECTION) && strlen(parser_tokval(parser)) <= 16) {
2054 correct_init(&corr);
2056 for (i = 0; i < vec_size(parser->correct_variables); i++) {
2057 correct = correct_str(&corr, parser->correct_variables[i], parser_tokval(parser));
2058 if (strcmp(correct, parser_tokval(parser))) {
2065 correct_free(&corr);
2068 parseerror(parser, "unexpected identifier: %s (did you mean %s?)", parser_tokval(parser), correct);
2073 parseerror(parser, "unexpected identifier: %s", parser_tokval(parser));
2079 if (ast_istype(var, ast_value)) {
2080 ((ast_value*)var)->uses++;
2082 else if (ast_istype(var, ast_member)) {
2083 ast_member *mem = (ast_member*)var;
2084 if (ast_istype(mem->owner, ast_value))
2085 ((ast_value*)(mem->owner))->uses++;
2088 vec_push(sy->out, syexp(parser_ctx(parser), var));
2091 parseerror(parser, "unexpected token `%s`", parser_tokval(parser));
2095 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels)
2097 ast_expression *expr = NULL;
2100 bool wantop = false;
2101 /* only warn once about an assignment in a truth value because the current code
2102 * would trigger twice on: if(a = b && ...), once for the if-truth-value, once for the && part
2104 bool warn_truthvalue = true;
2106 /* count the parens because an if starts with one, so the
2107 * end of a condition is an unmatched closing paren
2111 memset(&sy, 0, sizeof(sy));
2113 parser->lex->flags.noops = false;
2115 parser_reclassify_token(parser);
2119 if (parser->tok == TOKEN_TYPENAME) {
2120 parseerror(parser, "unexpected typename `%s`", parser_tokval(parser));
2124 if (parser->tok == TOKEN_OPERATOR)
2126 /* classify the operator */
2127 const oper_info *op;
2128 const oper_info *olast = NULL;
2130 for (o = 0; o < operator_count; ++o) {
2131 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
2132 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
2133 !strcmp(parser_tokval(parser), operators[o].op))
2138 if (o == operator_count) {
2139 compile_error(parser_ctx(parser), "unknown operator: %s", parser_tokval(parser));
2142 /* found an operator */
2145 /* when declaring variables, a comma starts a new variable */
2146 if (op->id == opid1(',') && !vec_size(sy.paren) && stopatcomma) {
2147 /* fixup the token */
2152 /* a colon without a pervious question mark cannot be a ternary */
2153 if (!ternaries && op->id == opid2(':','?')) {
2158 if (op->id == opid1(',')) {
2159 if (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
2160 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
2164 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
2165 olast = &operators[vec_last(sy.ops).etype-1];
2167 #define IsAssignOp(x) (\
2168 (x) == opid1('=') || \
2169 (x) == opid2('+','=') || \
2170 (x) == opid2('-','=') || \
2171 (x) == opid2('*','=') || \
2172 (x) == opid2('/','=') || \
2173 (x) == opid2('%','=') || \
2174 (x) == opid2('&','=') || \
2175 (x) == opid2('|','=') || \
2176 (x) == opid3('&','~','=') \
2178 if (warn_truthvalue) {
2179 if ( (olast && IsAssignOp(olast->id) && (op->id == opid2('&','&') || op->id == opid2('|','|'))) ||
2180 (olast && IsAssignOp(op->id) && (olast->id == opid2('&','&') || olast->id == opid2('|','|'))) ||
2181 (truthvalue && !vec_size(sy.paren) && IsAssignOp(op->id))
2184 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around assignment used as truth value");
2185 warn_truthvalue = false;
2190 (op->prec < olast->prec) ||
2191 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
2193 if (!parser_sy_apply_operator(parser, &sy))
2195 if (vec_size(sy.ops) && !vec_last(sy.ops).isparen)
2196 olast = &operators[vec_last(sy.ops).etype-1];
2201 if (op->id == opid1('(')) {
2203 size_t sycount = vec_size(sy.out);
2204 /* we expected an operator, this is the function-call operator */
2205 vec_push(sy.paren, PAREN_FUNC);
2206 vec_push(sy.ops, syparen(parser_ctx(parser), sycount-1));
2207 vec_push(sy.argc, 0);
2209 vec_push(sy.paren, PAREN_EXPR);
2210 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
2213 } else if (op->id == opid1('[')) {
2215 parseerror(parser, "unexpected array subscript");
2218 vec_push(sy.paren, PAREN_INDEX);
2219 /* push both the operator and the paren, this makes life easier */
2220 vec_push(sy.ops, syop(parser_ctx(parser), op));
2221 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
2223 } else if (op->id == opid2('?',':')) {
2224 vec_push(sy.ops, syop(parser_ctx(parser), op));
2225 vec_push(sy.ops, syparen(parser_ctx(parser), 0));
2228 vec_push(sy.paren, PAREN_TERNARY1);
2229 } else if (op->id == opid2(':','?')) {
2230 if (!vec_size(sy.paren)) {
2231 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
2234 if (vec_last(sy.paren) != PAREN_TERNARY1) {
2235 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
2238 if (!parser_close_paren(parser, &sy))
2240 vec_push(sy.ops, syop(parser_ctx(parser), op));
2244 vec_push(sy.ops, syop(parser_ctx(parser), op));
2245 wantop = !!(op->flags & OP_SUFFIX);
2248 else if (parser->tok == ')') {
2249 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
2250 if (!parser_sy_apply_operator(parser, &sy))
2253 if (!vec_size(sy.paren))
2256 if (vec_last(sy.paren) == PAREN_TERNARY1) {
2257 parseerror(parser, "mismatched parentheses (closing paren in ternary expression?)");
2260 if (!parser_close_paren(parser, &sy))
2263 /* must be a function call without parameters */
2264 if (vec_last(sy.paren) != PAREN_FUNC) {
2265 parseerror(parser, "closing paren in invalid position");
2268 if (!parser_close_paren(parser, &sy))
2273 else if (parser->tok == '(') {
2274 parseerror(parser, "internal error: '(' should be classified as operator");
2277 else if (parser->tok == '[') {
2278 parseerror(parser, "internal error: '[' should be classified as operator");
2281 else if (parser->tok == ']') {
2282 while (vec_size(sy.paren) && vec_last(sy.paren) == PAREN_TERNARY2) {
2283 if (!parser_sy_apply_operator(parser, &sy))
2286 if (!vec_size(sy.paren))
2288 if (vec_last(sy.paren) != PAREN_INDEX) {
2289 parseerror(parser, "mismatched parentheses, unexpected ']'");
2292 if (!parser_close_paren(parser, &sy))
2297 if (!parse_sya_operand(parser, &sy, with_labels))
2300 if (vec_size(sy.paren) && vec_last(sy.ops).isparen && vec_last(sy.paren) == PAREN_FUNC)
2301 vec_last(sy.argc)++;
2306 /* in this case we might want to allow constant string concatenation */
2307 bool concatenated = false;
2308 if (parser->tok == TOKEN_STRINGCONST && vec_size(sy.out)) {
2309 ast_expression *lexpr = vec_last(sy.out).out;
2310 if (ast_istype(lexpr, ast_value)) {
2311 ast_value *last = (ast_value*)lexpr;
2312 if (last->isimm == true && last->cvq == CV_CONST &&
2313 last->hasvalue && last->expression.vtype == TYPE_STRING)
2315 char *newstr = NULL;
2316 util_asprintf(&newstr, "%s%s", last->constval.vstring, parser_tokval(parser));
2317 vec_last(sy.out).out = (ast_expression*)parser_const_string(parser, newstr, false);
2319 concatenated = true;
2323 if (!concatenated) {
2324 parseerror(parser, "expected operator or end of statement");
2329 if (!parser_next(parser)) {
2332 if (parser->tok == ';' ||
2333 ((!vec_size(sy.paren) || (vec_size(sy.paren) == 1 && vec_last(sy.paren) == PAREN_TERNARY2)) &&
2334 (parser->tok == ']' || parser->tok == ')' || parser->tok == '}')))
2340 while (vec_size(sy.ops)) {
2341 if (!parser_sy_apply_operator(parser, &sy))
2345 parser->lex->flags.noops = true;
2346 if (vec_size(sy.out) != 1) {
2347 parseerror(parser, "expression with not 1 but %lu output values...", (unsigned long) vec_size(sy.out));
2350 expr = sy.out[0].out;
2353 if (vec_size(sy.paren)) {
2354 parseerror(parser, "internal error: vec_size(sy.paren) = %lu", (unsigned long)vec_size(sy.paren));
2362 parser->lex->flags.noops = true;
2363 for (i = 0; i < vec_size(sy.out); ++i) {
2365 ast_unref(sy.out[i].out);
2374 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels)
2376 ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
2379 if (parser->tok != ';') {
2380 parseerror(parser, "semicolon expected after expression");
2384 if (!parser_next(parser)) {
2391 static void parser_enterblock(parser_t *parser)
2393 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
2394 vec_push(parser->_blocklocals, vec_size(parser->_locals));
2395 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
2396 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
2397 vec_push(parser->_block_ctx, parser_ctx(parser));
2400 vec_push(parser->correct_variables, correct_trie_new());
2401 vec_push(parser->correct_variables_score, NULL);
2404 static bool parser_leaveblock(parser_t *parser)
2407 size_t locals, typedefs;
2409 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
2410 parseerror(parser, "internal error: parser_leaveblock with no block");
2414 util_htdel(vec_last(parser->variables));
2415 correct_del(vec_last(parser->correct_variables), vec_last(parser->correct_variables_score));
2417 vec_pop(parser->variables);
2418 vec_pop(parser->correct_variables);
2419 vec_pop(parser->correct_variables_score);
2420 if (!vec_size(parser->_blocklocals)) {
2421 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
2425 locals = vec_last(parser->_blocklocals);
2426 vec_pop(parser->_blocklocals);
2427 while (vec_size(parser->_locals) != locals) {
2428 ast_expression *e = vec_last(parser->_locals);
2429 ast_value *v = (ast_value*)e;
2430 vec_pop(parser->_locals);
2431 if (ast_istype(e, ast_value) && !v->uses) {
2432 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name))
2437 typedefs = vec_last(parser->_blocktypedefs);
2438 while (vec_size(parser->_typedefs) != typedefs) {
2439 ast_delete(vec_last(parser->_typedefs));
2440 vec_pop(parser->_typedefs);
2442 util_htdel(vec_last(parser->typedefs));
2443 vec_pop(parser->typedefs);
2445 vec_pop(parser->_block_ctx);
2450 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
2452 vec_push(parser->_locals, e);
2453 util_htset(vec_last(parser->variables), name, (void*)e);
2457 vec_last(parser->correct_variables),
2458 &vec_last(parser->correct_variables_score),
2463 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
2465 vec_push(parser->globals, e);
2466 util_htset(parser->htglobals, name, e);
2470 parser->correct_variables[0],
2471 &parser->correct_variables_score[0],
2476 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
2480 ast_expression *prev;
2482 if (cond->vtype == TYPE_VOID || cond->vtype >= TYPE_VARIANT) {
2484 ast_type_to_string(cond, ty, sizeof(ty));
2485 compile_error(ast_ctx(cond), "invalid type for if() condition: %s", ty);
2488 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->vtype == TYPE_STRING)
2491 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_S, cond);
2494 parseerror(parser, "internal error: failed to process condition");
2499 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->vtype == TYPE_VECTOR)
2501 /* vector types need to be cast to true booleans */
2502 ast_binary *bin = (ast_binary*)cond;
2503 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->op == INSTR_AND || bin->op == INSTR_OR))
2505 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
2507 cond = (ast_expression*)ast_unary_new(ast_ctx(cond), INSTR_NOT_V, cond);
2510 parseerror(parser, "internal error: failed to process condition");
2517 unary = (ast_unary*)cond;
2518 /* ast_istype dereferences cond, should test here for safety */
2519 while (cond && ast_istype(cond, ast_unary) && unary->op == INSTR_NOT_F)
2521 cond = unary->operand;
2522 unary->operand = NULL;
2525 unary = (ast_unary*)cond;
2529 parseerror(parser, "internal error: failed to process condition");
2531 if (ifnot) *_ifnot = !*_ifnot;
2535 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
2538 ast_expression *cond, *ontrue = NULL, *onfalse = NULL;
2541 lex_ctx_t ctx = parser_ctx(parser);
2543 (void)block; /* not touching */
2545 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2546 if (!parser_next(parser)) {
2547 parseerror(parser, "expected condition or 'not'");
2550 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
2552 if (!parser_next(parser)) {
2553 parseerror(parser, "expected condition in parenthesis");
2557 if (parser->tok != '(') {
2558 parseerror(parser, "expected 'if' condition in parenthesis");
2561 /* parse into the expression */
2562 if (!parser_next(parser)) {
2563 parseerror(parser, "expected 'if' condition after opening paren");
2566 /* parse the condition */
2567 cond = parse_expression_leave(parser, false, true, false);
2571 if (parser->tok != ')') {
2572 parseerror(parser, "expected closing paren after 'if' condition");
2576 /* parse into the 'then' branch */
2577 if (!parser_next(parser)) {
2578 parseerror(parser, "expected statement for on-true branch of 'if'");
2582 if (!parse_statement_or_block(parser, &ontrue)) {
2587 ontrue = (ast_expression*)ast_block_new(parser_ctx(parser));
2588 /* check for an else */
2589 if (!strcmp(parser_tokval(parser), "else")) {
2590 /* parse into the 'else' branch */
2591 if (!parser_next(parser)) {
2592 parseerror(parser, "expected on-false branch after 'else'");
2597 if (!parse_statement_or_block(parser, &onfalse)) {
2604 cond = process_condition(parser, cond, &ifnot);
2606 if (ontrue) ast_delete(ontrue);
2607 if (onfalse) ast_delete(onfalse);
2612 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
2614 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
2615 *out = (ast_expression*)ifthen;
2619 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out);
2620 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
2625 /* skip the 'while' and get the body */
2626 if (!parser_next(parser)) {
2627 if (OPTS_FLAG(LOOP_LABELS))
2628 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2630 parseerror(parser, "expected 'while' condition in parenthesis");
2634 if (parser->tok == ':') {
2635 if (!OPTS_FLAG(LOOP_LABELS))
2636 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2637 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2638 parseerror(parser, "expected loop label");
2641 label = util_strdup(parser_tokval(parser));
2642 if (!parser_next(parser)) {
2644 parseerror(parser, "expected 'while' condition in parenthesis");
2649 if (parser->tok != '(') {
2650 parseerror(parser, "expected 'while' condition in parenthesis");
2654 vec_push(parser->breaks, label);
2655 vec_push(parser->continues, label);
2657 rv = parse_while_go(parser, block, out);
2660 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2661 parseerror(parser, "internal error: label stack corrupted");
2667 vec_pop(parser->breaks);
2668 vec_pop(parser->continues);
2673 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out)
2676 ast_expression *cond, *ontrue;
2680 lex_ctx_t ctx = parser_ctx(parser);
2682 (void)block; /* not touching */
2684 /* parse into the expression */
2685 if (!parser_next(parser)) {
2686 parseerror(parser, "expected 'while' condition after opening paren");
2689 /* parse the condition */
2690 cond = parse_expression_leave(parser, false, true, false);
2694 if (parser->tok != ')') {
2695 parseerror(parser, "expected closing paren after 'while' condition");
2699 /* parse into the 'then' branch */
2700 if (!parser_next(parser)) {
2701 parseerror(parser, "expected while-loop body");
2705 if (!parse_statement_or_block(parser, &ontrue)) {
2710 cond = process_condition(parser, cond, &ifnot);
2715 aloop = ast_loop_new(ctx, NULL, cond, ifnot, NULL, false, NULL, ontrue);
2716 *out = (ast_expression*)aloop;
2720 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out);
2721 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
2726 /* skip the 'do' and get the body */
2727 if (!parser_next(parser)) {
2728 if (OPTS_FLAG(LOOP_LABELS))
2729 parseerror(parser, "expected loop label or body");
2731 parseerror(parser, "expected loop body");
2735 if (parser->tok == ':') {
2736 if (!OPTS_FLAG(LOOP_LABELS))
2737 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2738 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2739 parseerror(parser, "expected loop label");
2742 label = util_strdup(parser_tokval(parser));
2743 if (!parser_next(parser)) {
2745 parseerror(parser, "expected loop body");
2750 vec_push(parser->breaks, label);
2751 vec_push(parser->continues, label);
2753 rv = parse_dowhile_go(parser, block, out);
2756 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2757 parseerror(parser, "internal error: label stack corrupted");
2760 * Test for NULL otherwise ast_delete dereferences null pointer
2768 vec_pop(parser->breaks);
2769 vec_pop(parser->continues);
2774 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out)
2777 ast_expression *cond, *ontrue;
2781 lex_ctx_t ctx = parser_ctx(parser);
2783 (void)block; /* not touching */
2785 if (!parse_statement_or_block(parser, &ontrue))
2788 /* expect the "while" */
2789 if (parser->tok != TOKEN_KEYWORD ||
2790 strcmp(parser_tokval(parser), "while"))
2792 parseerror(parser, "expected 'while' and condition");
2797 /* skip the 'while' and check for opening paren */
2798 if (!parser_next(parser) || parser->tok != '(') {
2799 parseerror(parser, "expected 'while' condition in parenthesis");
2803 /* parse into the expression */
2804 if (!parser_next(parser)) {
2805 parseerror(parser, "expected 'while' condition after opening paren");
2809 /* parse the condition */
2810 cond = parse_expression_leave(parser, false, true, false);
2814 if (parser->tok != ')') {
2815 parseerror(parser, "expected closing paren after 'while' condition");
2821 if (!parser_next(parser) || parser->tok != ';') {
2822 parseerror(parser, "expected semicolon after condition");
2828 if (!parser_next(parser)) {
2829 parseerror(parser, "parse error");
2835 cond = process_condition(parser, cond, &ifnot);
2840 aloop = ast_loop_new(ctx, NULL, NULL, false, cond, ifnot, NULL, ontrue);
2841 *out = (ast_expression*)aloop;
2845 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
2846 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
2851 /* skip the 'for' and check for opening paren */
2852 if (!parser_next(parser)) {
2853 if (OPTS_FLAG(LOOP_LABELS))
2854 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2856 parseerror(parser, "expected 'for' expressions in parenthesis");
2860 if (parser->tok == ':') {
2861 if (!OPTS_FLAG(LOOP_LABELS))
2862 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2863 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2864 parseerror(parser, "expected loop label");
2867 label = util_strdup(parser_tokval(parser));
2868 if (!parser_next(parser)) {
2870 parseerror(parser, "expected 'for' expressions in parenthesis");
2875 if (parser->tok != '(') {
2876 parseerror(parser, "expected 'for' expressions in parenthesis");
2880 vec_push(parser->breaks, label);
2881 vec_push(parser->continues, label);
2883 rv = parse_for_go(parser, block, out);
2886 if (vec_last(parser->breaks) != label || vec_last(parser->continues) != label) {
2887 parseerror(parser, "internal error: label stack corrupted");
2893 vec_pop(parser->breaks);
2894 vec_pop(parser->continues);
2898 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
2901 ast_expression *initexpr, *cond, *increment, *ontrue;
2906 lex_ctx_t ctx = parser_ctx(parser);
2908 parser_enterblock(parser);
2915 /* parse into the expression */
2916 if (!parser_next(parser)) {
2917 parseerror(parser, "expected 'for' initializer after opening paren");
2922 if (parser->tok == TOKEN_IDENT)
2923 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2925 if (typevar || parser->tok == TOKEN_TYPENAME) {
2927 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
2928 if (parsewarning(parser, WARN_EXTENSIONS,
2929 "current standard does not allow variable declarations in for-loop initializers"))
2933 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, NULL))
2936 else if (parser->tok != ';')
2938 initexpr = parse_expression_leave(parser, false, false, false);
2943 /* move on to condition */
2944 if (parser->tok != ';') {
2945 parseerror(parser, "expected semicolon after for-loop initializer");
2948 if (!parser_next(parser)) {
2949 parseerror(parser, "expected for-loop condition");
2953 /* parse the condition */
2954 if (parser->tok != ';') {
2955 cond = parse_expression_leave(parser, false, true, false);
2960 /* move on to incrementor */
2961 if (parser->tok != ';') {
2962 parseerror(parser, "expected semicolon after for-loop initializer");
2965 if (!parser_next(parser)) {
2966 parseerror(parser, "expected for-loop condition");
2970 /* parse the incrementor */
2971 if (parser->tok != ')') {
2972 lex_ctx_t condctx = parser_ctx(parser);
2973 increment = parse_expression_leave(parser, false, false, false);
2976 if (!ast_side_effects(increment)) {
2977 if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2983 if (parser->tok != ')') {
2984 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2987 /* parse into the 'then' branch */
2988 if (!parser_next(parser)) {
2989 parseerror(parser, "expected for-loop body");
2992 if (!parse_statement_or_block(parser, &ontrue))
2996 cond = process_condition(parser, cond, &ifnot);
3000 aloop = ast_loop_new(ctx, initexpr, cond, ifnot, NULL, false, increment, ontrue);
3001 *out = (ast_expression*)aloop;
3003 if (!parser_leaveblock(parser)) {
3009 if (initexpr) ast_unref(initexpr);
3010 if (cond) ast_unref(cond);
3011 if (increment) ast_unref(increment);
3012 (void)!parser_leaveblock(parser);
3016 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
3018 ast_expression *exp = NULL;
3019 ast_expression *var = NULL;
3020 ast_return *ret = NULL;
3021 ast_value *retval = parser->function->return_value;
3022 ast_value *expected = parser->function->vtype;
3024 lex_ctx_t ctx = parser_ctx(parser);
3026 (void)block; /* not touching */
3028 if (!parser_next(parser)) {
3029 parseerror(parser, "expected return expression");
3033 /* return assignments */
3034 if (parser->tok == '=') {
3035 if (!OPTS_FLAG(RETURN_ASSIGNMENTS)) {
3036 parseerror(parser, "return assignments not activated, try using -freturn-assigments");
3040 if (type_store_instr[expected->expression.next->vtype] == VINSTR_END) {
3042 ast_type_to_string(expected->expression.next, ty1, sizeof(ty1));
3043 parseerror(parser, "invalid return type: `%s'", ty1);
3047 if (!parser_next(parser)) {
3048 parseerror(parser, "expected return assignment expression");
3052 if (!(exp = parse_expression_leave(parser, false, false, false)))
3055 /* prepare the return value */
3057 retval = ast_value_new(ctx, "#LOCAL_RETURN", TYPE_VOID);
3058 ast_type_adopt(retval, expected->expression.next);
3059 parser->function->return_value = retval;
3062 if (!ast_compare_type(exp, (ast_expression*)retval)) {
3063 char ty1[1024], ty2[1024];
3064 ast_type_to_string(exp, ty1, sizeof(ty1));
3065 ast_type_to_string(&retval->expression, ty2, sizeof(ty2));
3066 parseerror(parser, "invalid type for return value: `%s', expected `%s'", ty1, ty2);
3069 /* store to 'return' local variable */
3070 var = (ast_expression*)ast_store_new(
3072 type_store_instr[expected->expression.next->vtype],
3073 (ast_expression*)retval, exp);
3080 if (parser->tok != ';')
3081 parseerror(parser, "missing semicolon after return assignment");
3082 else if (!parser_next(parser))
3083 parseerror(parser, "parse error after return assignment");
3089 if (parser->tok != ';') {
3090 exp = parse_expression(parser, false, false);
3094 if (exp->vtype != TYPE_NIL &&
3095 exp->vtype != ((ast_expression*)expected)->next->vtype)
3097 parseerror(parser, "return with invalid expression");
3100 ret = ast_return_new(ctx, exp);
3106 if (!parser_next(parser))
3107 parseerror(parser, "parse error");
3109 if (!retval && expected->expression.next->vtype != TYPE_VOID)
3111 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
3113 ret = ast_return_new(ctx, (ast_expression*)retval);
3115 *out = (ast_expression*)ret;
3119 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
3122 unsigned int levels = 0;
3123 lex_ctx_t ctx = parser_ctx(parser);
3124 const char **loops = (is_continue ? parser->continues : parser->breaks);
3126 (void)block; /* not touching */
3127 if (!parser_next(parser)) {
3128 parseerror(parser, "expected semicolon or loop label");
3132 if (!vec_size(loops)) {
3134 parseerror(parser, "`continue` can only be used inside loops");
3136 parseerror(parser, "`break` can only be used inside loops or switches");
3139 if (parser->tok == TOKEN_IDENT) {
3140 if (!OPTS_FLAG(LOOP_LABELS))
3141 parseerror(parser, "labeled loops not activated, try using -floop-labels");
3142 i = vec_size(loops);
3144 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
3147 parseerror(parser, "no such loop to %s: `%s`",
3148 (is_continue ? "continue" : "break out of"),
3149 parser_tokval(parser));
3154 if (!parser_next(parser)) {
3155 parseerror(parser, "expected semicolon");
3160 if (parser->tok != ';') {
3161 parseerror(parser, "expected semicolon");
3165 if (!parser_next(parser))
3166 parseerror(parser, "parse error");
3168 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue, levels);
3172 /* returns true when it was a variable qualifier, false otherwise!
3173 * on error, cvq is set to CV_WRONG
3175 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
3177 bool had_const = false;
3178 bool had_var = false;
3179 bool had_noref = false;
3180 bool had_attrib = false;
3181 bool had_static = false;
3186 if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
3188 /* parse an attribute */
3189 if (!parser_next(parser)) {
3190 parseerror(parser, "expected attribute after `[[`");
3194 if (!strcmp(parser_tokval(parser), "noreturn")) {
3195 flags |= AST_FLAG_NORETURN;
3196 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3197 parseerror(parser, "`noreturn` attribute has no parameters, expected `]]`");
3202 else if (!strcmp(parser_tokval(parser), "noref")) {
3204 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3205 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
3210 else if (!strcmp(parser_tokval(parser), "inline")) {
3211 flags |= AST_FLAG_INLINE;
3212 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3213 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
3218 else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
3219 flags |= AST_FLAG_ALIAS;
3222 if (!parser_next(parser)) {
3223 parseerror(parser, "parse error in attribute");
3227 if (parser->tok == '(') {
3228 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
3229 parseerror(parser, "`alias` attribute missing parameter");
3233 *message = util_strdup(parser_tokval(parser));
3235 if (!parser_next(parser)) {
3236 parseerror(parser, "parse error in attribute");
3240 if (parser->tok != ')') {
3241 parseerror(parser, "`alias` attribute expected `)` after parameter");
3245 if (!parser_next(parser)) {
3246 parseerror(parser, "parse error in attribute");
3251 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3252 parseerror(parser, "`alias` attribute expected `]]`");
3256 else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
3257 flags |= AST_FLAG_DEPRECATED;
3260 if (!parser_next(parser)) {
3261 parseerror(parser, "parse error in attribute");
3265 if (parser->tok == '(') {
3266 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
3267 parseerror(parser, "`deprecated` attribute missing parameter");
3271 *message = util_strdup(parser_tokval(parser));
3273 if (!parser_next(parser)) {
3274 parseerror(parser, "parse error in attribute");
3278 if(parser->tok != ')') {
3279 parseerror(parser, "`deprecated` attribute expected `)` after parameter");
3283 if (!parser_next(parser)) {
3284 parseerror(parser, "parse error in attribute");
3289 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3290 parseerror(parser, "`deprecated` attribute expected `]]`");
3293 if (*message) mem_d(*message);
3301 /* Skip tokens until we hit a ]] */
3302 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
3303 while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
3304 if (!parser_next(parser)) {
3305 parseerror(parser, "error inside attribute");
3312 else if (with_local && !strcmp(parser_tokval(parser), "static"))
3314 else if (!strcmp(parser_tokval(parser), "const"))
3316 else if (!strcmp(parser_tokval(parser), "var"))
3318 else if (with_local && !strcmp(parser_tokval(parser), "local"))
3320 else if (!strcmp(parser_tokval(parser), "noref"))
3322 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
3327 if (!parser_next(parser))
3337 *is_static = had_static;
3341 parseerror(parser, "parse error after variable qualifier");
3346 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
3347 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
3352 /* skip the 'while' and get the body */
3353 if (!parser_next(parser)) {
3354 if (OPTS_FLAG(LOOP_LABELS))
3355 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
3357 parseerror(parser, "expected 'switch' operand in parenthesis");
3361 if (parser->tok == ':') {
3362 if (!OPTS_FLAG(LOOP_LABELS))
3363 parseerror(parser, "labeled loops not activated, try using -floop-labels");
3364 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
3365 parseerror(parser, "expected loop label");
3368 label = util_strdup(parser_tokval(parser));
3369 if (!parser_next(parser)) {
3371 parseerror(parser, "expected 'switch' operand in parenthesis");
3376 if (parser->tok != '(') {
3377 parseerror(parser, "expected 'switch' operand in parenthesis");
3381 vec_push(parser->breaks, label);
3383 rv = parse_switch_go(parser, block, out);
3386 if (vec_last(parser->breaks) != label) {
3387 parseerror(parser, "internal error: label stack corrupted");
3393 vec_pop(parser->breaks);
3398 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
3400 ast_expression *operand;
3403 ast_switch *switchnode;
3404 ast_switch_case swcase;
3407 bool noref, is_static;
3408 uint32_t qflags = 0;
3410 lex_ctx_t ctx = parser_ctx(parser);
3412 (void)block; /* not touching */
3415 /* parse into the expression */
3416 if (!parser_next(parser)) {
3417 parseerror(parser, "expected switch operand");
3420 /* parse the operand */
3421 operand = parse_expression_leave(parser, false, false, false);
3425 switchnode = ast_switch_new(ctx, operand);
3428 if (parser->tok != ')') {
3429 ast_delete(switchnode);
3430 parseerror(parser, "expected closing paren after 'switch' operand");
3434 /* parse over the opening paren */
3435 if (!parser_next(parser) || parser->tok != '{') {
3436 ast_delete(switchnode);
3437 parseerror(parser, "expected list of cases");