9 #define PARSER_HT_LOCALS 2
10 #define PARSER_HT_SIZE 512
11 #define TYPEDEF_HT_SIZE 512
13 static void parser_enterblock(parser_t &parser);
14 static bool parser_leaveblock(parser_t &parser);
15 static void parser_addlocal(parser_t &parser, const char *name, ast_expression *e);
16 static void parser_addlocal(parser_t &parser, const std::string &name, ast_expression *e);
17 static void parser_addglobal(parser_t &parser, const char *name, ast_expression *e);
18 static void parser_addglobal(parser_t &parser, const std::string &name, ast_expression *e);
19 static bool parse_typedef(parser_t &parser);
20 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);
21 static ast_block* parse_block(parser_t &parser);
22 static bool parse_block_into(parser_t &parser, ast_block *block);
23 static bool parse_statement_or_block(parser_t &parser, ast_expression **out);
24 static bool parse_statement(parser_t &parser, ast_block *block, ast_expression **out, bool allow_cases);
25 static ast_expression* parse_expression_leave(parser_t &parser, bool stopatcomma, bool truthvalue, bool with_labels);
26 static ast_expression* parse_expression(parser_t &parser, bool stopatcomma, bool with_labels);
27 static ast_value* parser_create_array_setter_proto(parser_t &parser, ast_value *array, const char *funcname);
28 static ast_value* parser_create_array_getter_proto(parser_t &parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
29 static ast_value *parse_typename(parser_t &parser, ast_value **storebase, ast_value *cached_typedef, bool *is_vararg);
31 static void parseerror_(parser_t &parser, const char *fmt, ...)
35 vcompile_error(parser.lex->tok.ctx, fmt, ap);
39 template<typename... Ts>
40 static inline void parseerror(parser_t &parser, const char *fmt, const Ts&... ts) {
41 return parseerror_(parser, fmt, formatNormalize(ts)...);
44 // returns true if it counts as an error
45 static bool GMQCC_WARN parsewarning_(parser_t &parser, int warntype, const char *fmt, ...)
50 r = vcompile_warning(parser.lex->tok.ctx, warntype, fmt, ap);
55 template<typename... Ts>
56 static inline bool GMQCC_WARN parsewarning(parser_t &parser, int warntype, const char *fmt, const Ts&... ts) {
57 return parsewarning_(parser, warntype, fmt, formatNormalize(ts)...);
60 /**********************************************************************
64 static bool parser_next(parser_t &parser)
66 /* lex_do kills the previous token */
67 parser.tok = lex_do(parser.lex);
68 if (parser.tok == Token::END)
70 if (parser.tok >= Token::ERROR) {
71 parseerror(parser, "lex error");
77 #define parser_tokval(p) ((p).lex->tok.value)
78 #define parser_token(p) (&((p).lex->tok))
80 static ast_expression* parser_find_field(parser_t &parser, const char *name) {
81 return (ast_expression*)util_htget(parser.htfields, name);
83 static ast_expression* parser_find_field(parser_t &parser, const std::string &name) {
84 return parser_find_field(parser, name.c_str());
87 static ast_expression* parser_find_label(parser_t &parser, const char *name)
89 for (auto &it : parser.labels)
90 if (it->m_name == name)
94 static inline ast_expression* parser_find_label(parser_t &parser, const std::string &name) {
95 return parser_find_label(parser, name.c_str());
98 ast_expression* parser_find_global(parser_t &parser, const char *name)
100 ast_expression *var = (ast_expression*)util_htget(parser.aliases, parser_tokval(parser));
103 return (ast_expression*)util_htget(parser.htglobals, name);
106 ast_expression* parser_find_global(parser_t &parser, const std::string &name) {
107 return parser_find_global(parser, name.c_str());
110 static ast_expression* parser_find_param(parser_t &parser, const char *name)
113 if (!parser.function)
115 fun = parser.function->m_function_type;
116 for (auto &it : fun->m_type_params) {
117 if (it->m_name == name)
123 static ast_expression* parser_find_local(parser_t &parser, const char *name, size_t upto, bool *isparam)
128 hash = util_hthash(parser.htglobals, name);
131 for (i = parser.variables.size(); i > upto;) {
133 if ( (e = (ast_expression*)util_htgeth(parser.variables[i], name, hash)) )
137 return parser_find_param(parser, name);
140 static ast_expression* parser_find_local(parser_t &parser, const std::string &name, size_t upto, bool *isparam) {
141 return parser_find_local(parser, name.c_str(), upto, isparam);
144 static ast_expression* parser_find_var(parser_t &parser, const char *name)
148 v = parser_find_local(parser, name, 0, &dummy);
149 if (!v) v = parser_find_global(parser, name);
153 static inline ast_expression* parser_find_var(parser_t &parser, const std::string &name) {
154 return parser_find_var(parser, name.c_str());
157 static ast_value* parser_find_typedef(parser_t &parser, const char *name, size_t upto)
161 hash = util_hthash(parser.typedefs[0], name);
163 for (i = parser.typedefs.size(); i > upto;) {
165 if ( (e = (ast_value*)util_htgeth(parser.typedefs[i], name, hash)) )
171 static ast_value* parser_find_typedef(parser_t &parser, const std::string &name, size_t upto) {
172 return parser_find_typedef(parser, name.c_str(), upto);
176 size_t etype; /* 0 = expression, others are operators */
180 ast_block *block; /* for commas and function calls */
193 std::vector<sy_elem> out;
194 std::vector<sy_elem> ops;
195 std::vector<size_t> argc;
196 std::vector<unsigned int> paren;
199 static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
210 static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
221 static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
223 e.etype = 1 + (op - operators);
232 static sy_elem syparen(lex_ctx_t ctx, size_t off) {
243 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
244 * so we need to rotate it to become ent.(foo[n]).
246 static bool rotate_entfield_array_index_nodes(ast_expression **out)
248 ast_array_index *index, *oldindex;
249 ast_entfield *entfield;
253 ast_expression *entity;
255 lex_ctx_t ctx = (*out)->m_context;
257 if (!ast_istype(*out, ast_array_index))
259 index = (ast_array_index*)*out;
261 if (!ast_istype(index->m_array, ast_entfield))
263 entfield = (ast_entfield*)index->m_array;
265 if (!ast_istype(entfield->m_field, ast_value))
267 field = (ast_value*)entfield->m_field;
269 sub = index->m_index;
270 entity = entfield->m_entity;
274 index = ast_array_index::make(ctx, field, sub);
275 entfield = new ast_entfield(ctx, entity, index);
278 oldindex->m_array = nullptr;
279 oldindex->m_index = nullptr;
285 static bool check_write_to(lex_ctx_t ctx, ast_expression *expr)
287 if (ast_istype(expr, ast_value)) {
288 ast_value *val = (ast_value*)expr;
289 if (val->m_cvq == CV_CONST) {
290 if (val->m_name[0] == '#') {
291 compile_error(ctx, "invalid assignment to a literal constant");
295 * To work around quakeworld we must elide the error and make it
298 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC)
299 compile_error(ctx, "assignment to constant `%s`", val->m_name);
301 (void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->m_name);
308 static bool parser_sy_apply_operator(parser_t &parser, shunt *sy)
312 ast_expression *out = nullptr;
313 ast_expression *exprs[3];
314 ast_block *blocks[3];
315 ast_binstore *asbinstore;
316 size_t i, assignop, addop, subop;
317 qcint_t generated_op = 0;
322 if (sy->ops.empty()) {
323 parseerror(parser, "internal error: missing operator");
327 if (sy->ops.back().isparen) {
328 parseerror(parser, "unmatched parenthesis");
332 op = &operators[sy->ops.back().etype - 1];
333 ctx = sy->ops.back().ctx;
335 if (sy->out.size() < op->operands) {
336 if (op->flags & OP_PREFIX)
337 compile_error(ctx, "expected expression after unary operator `%s`", op->op, (int)op->id);
338 else /* this should have errored previously already */
339 compile_error(ctx, "expected expression after operator `%s`", op->op, (int)op->id);
345 /* op(:?) has no input and no output */
349 sy->out.erase(sy->out.end() - op->operands, sy->out.end());
350 for (i = 0; i < op->operands; ++i) {
351 exprs[i] = sy->out[sy->out.size()+i].out;
352 blocks[i] = sy->out[sy->out.size()+i].block;
354 if (exprs[i]->m_vtype == TYPE_NOEXPR &&
355 !(i != 0 && op->id == opid2('?',':')) &&
356 !(i == 1 && op->id == opid1('.')))
358 if (ast_istype(exprs[i], ast_label))
359 compile_error(exprs[i]->m_context, "expected expression, got an unknown identifier");
361 compile_error(exprs[i]->m_context, "not an expression");
362 (void)!compile_warning(exprs[i]->m_context, WARN_DEBUG, "expression %u\n", (unsigned int)i);
366 if (blocks[0] && blocks[0]->m_exprs.empty() && op->id != opid1(',')) {
367 compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
371 #define NotSameType(T) \
372 (exprs[0]->m_vtype != exprs[1]->m_vtype || \
373 exprs[0]->m_vtype != T)
378 compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
382 if (exprs[0]->m_vtype == TYPE_VECTOR &&
383 exprs[1]->m_vtype == TYPE_NOEXPR)
385 if (exprs[1] == parser.const_vec[0])
386 out = ast_member::make(ctx, exprs[0], 0, "");
387 else if (exprs[1] == parser.const_vec[1])
388 out = ast_member::make(ctx, exprs[0], 1, "");
389 else if (exprs[1] == parser.const_vec[2])
390 out = ast_member::make(ctx, exprs[0], 2, "");
392 compile_error(ctx, "access to invalid vector component");
396 else if (exprs[0]->m_vtype == TYPE_ENTITY) {
397 if (exprs[1]->m_vtype != TYPE_FIELD) {
398 compile_error(exprs[1]->m_context, "type error: right hand of member-operand should be an entity-field");
401 out = new ast_entfield(ctx, exprs[0], exprs[1]);
403 else if (exprs[0]->m_vtype == TYPE_VECTOR) {
404 compile_error(exprs[1]->m_context, "vectors cannot be accessed this way");
408 compile_error(exprs[1]->m_context, "type error: member-of operator on something that is not an entity or vector");
414 if (exprs[0]->m_vtype != TYPE_ARRAY &&
415 !(exprs[0]->m_vtype == TYPE_FIELD &&
416 exprs[0]->m_next->m_vtype == TYPE_ARRAY))
418 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
419 compile_error(exprs[0]->m_context, "cannot index value of type %s", ty1);
422 if (exprs[1]->m_vtype != TYPE_FLOAT) {
423 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
424 compile_error(exprs[1]->m_context, "index must be of type float, not %s", ty1);
427 out = ast_array_index::make(ctx, exprs[0], exprs[1]);
428 rotate_entfield_array_index_nodes(&out);
432 if (sy->paren.size() && sy->paren.back() == PAREN_FUNC) {
433 sy->out.push_back(syexp(ctx, exprs[0]));
434 sy->out.push_back(syexp(ctx, exprs[1]));
439 if (!blocks[0]->addExpr(exprs[1]))
442 blocks[0] = new ast_block(ctx);
443 if (!blocks[0]->addExpr(exprs[0]) ||
444 !blocks[0]->addExpr(exprs[1]))
449 blocks[0]->setType(*exprs[1]);
451 sy->out.push_back(syblock(ctx, blocks[0]));
458 if ((out = parser.m_fold.op(op, exprs)))
461 if (exprs[0]->m_vtype != TYPE_FLOAT &&
462 exprs[0]->m_vtype != TYPE_VECTOR) {
463 compile_error(ctx, "invalid types used in unary expression: cannot negate type %s",
464 type_name[exprs[0]->m_vtype]);
467 if (exprs[0]->m_vtype == TYPE_FLOAT)
468 out = ast_unary::make(ctx, VINSTR_NEG_F, exprs[0]);
470 out = ast_unary::make(ctx, VINSTR_NEG_V, exprs[0]);
474 if (!(out = parser.m_fold.op(op, exprs))) {
475 switch (exprs[0]->m_vtype) {
477 out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
480 out = ast_unary::make(ctx, INSTR_NOT_V, exprs[0]);
483 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
484 out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
486 out = ast_unary::make(ctx, INSTR_NOT_S, exprs[0]);
488 /* we don't constant-fold NOT for these types */
490 out = ast_unary::make(ctx, INSTR_NOT_ENT, exprs[0]);
493 out = ast_unary::make(ctx, INSTR_NOT_FNC, exprs[0]);
496 compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
497 type_name[exprs[0]->m_vtype]);
504 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
505 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
507 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
508 type_name[exprs[0]->m_vtype],
509 type_name[exprs[1]->m_vtype]);
512 if (!(out = parser.m_fold.op(op, exprs))) {
513 switch (exprs[0]->m_vtype) {
515 out = fold::binary(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
518 out = fold::binary(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
521 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
522 type_name[exprs[0]->m_vtype],
523 type_name[exprs[1]->m_vtype]);
529 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
530 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT))
532 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
533 type_name[exprs[1]->m_vtype],
534 type_name[exprs[0]->m_vtype]);
537 if (!(out = parser.m_fold.op(op, exprs))) {
538 switch (exprs[0]->m_vtype) {
540 out = fold::binary(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
543 out = fold::binary(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
546 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
547 type_name[exprs[1]->m_vtype],
548 type_name[exprs[0]->m_vtype]);
554 if (exprs[0]->m_vtype != exprs[1]->m_vtype &&
555 !(exprs[0]->m_vtype == TYPE_VECTOR &&
556 exprs[1]->m_vtype == TYPE_FLOAT) &&
557 !(exprs[1]->m_vtype == TYPE_VECTOR &&
558 exprs[0]->m_vtype == TYPE_FLOAT)
561 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
562 type_name[exprs[1]->m_vtype],
563 type_name[exprs[0]->m_vtype]);
566 if (!(out = parser.m_fold.op(op, exprs))) {
567 switch (exprs[0]->m_vtype) {
569 if (exprs[1]->m_vtype == TYPE_VECTOR)
570 out = fold::binary(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
572 out = fold::binary(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
575 if (exprs[1]->m_vtype == TYPE_FLOAT)
576 out = fold::binary(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
578 out = fold::binary(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
581 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
582 type_name[exprs[1]->m_vtype],
583 type_name[exprs[0]->m_vtype]);
590 if (exprs[1]->m_vtype != TYPE_FLOAT) {
591 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
592 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
593 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
596 if (!(out = parser.m_fold.op(op, exprs))) {
597 if (exprs[0]->m_vtype == TYPE_FLOAT)
598 out = fold::binary(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
600 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
601 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
602 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
609 if (NotSameType(TYPE_FLOAT)) {
610 compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
611 type_name[exprs[0]->m_vtype],
612 type_name[exprs[1]->m_vtype]);
614 } else if (!(out = parser.m_fold.op(op, exprs))) {
615 /* generate a call to __builtin_mod */
616 ast_expression *mod = parser.m_intrin.func("mod");
617 ast_call *call = nullptr;
618 if (!mod) return false; /* can return null for missing floor */
620 call = ast_call::make(parser_ctx(parser), mod);
621 call->m_params.push_back(exprs[0]);
622 call->m_params.push_back(exprs[1]);
629 compile_error(ctx, "%= is unimplemented");
635 if ( !(exprs[0]->m_vtype == TYPE_FLOAT && exprs[1]->m_vtype == TYPE_FLOAT) &&
636 !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_FLOAT) &&
637 !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_VECTOR))
639 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
640 type_name[exprs[0]->m_vtype],
641 type_name[exprs[1]->m_vtype]);
645 if (!(out = parser.m_fold.op(op, exprs))) {
647 * IF the first expression is float, the following will be too
648 * since scalar ^ vector is not allowed.
650 if (exprs[0]->m_vtype == TYPE_FLOAT) {
651 out = fold::binary(ctx,
652 (op->id == opid1('^') ? VINSTR_BITXOR : op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
656 * The first is a vector: vector is allowed to bitop with vector and
657 * with scalar, branch here for the second operand.
659 if (exprs[1]->m_vtype == TYPE_VECTOR) {
661 * Bitop all the values of the vector components against the
662 * vectors components in question.
664 out = fold::binary(ctx,
665 (op->id == opid1('^') ? VINSTR_BITXOR_V : op->id == opid1('|') ? VINSTR_BITOR_V : VINSTR_BITAND_V),
668 out = fold::binary(ctx,
669 (op->id == opid1('^') ? VINSTR_BITXOR_VF : op->id == opid1('|') ? VINSTR_BITOR_VF : VINSTR_BITAND_VF),
678 if (NotSameType(TYPE_FLOAT)) {
679 compile_error(ctx, "invalid types used in expression: cannot perform shift between types %s and %s",
680 type_name[exprs[0]->m_vtype],
681 type_name[exprs[1]->m_vtype]);
685 if (!(out = parser.m_fold.op(op, exprs))) {
686 ast_expression *shift = parser.m_intrin.func((op->id == opid2('<','<')) ? "__builtin_lshift" : "__builtin_rshift");
687 ast_call *call = ast_call::make(parser_ctx(parser), shift);
688 call->m_params.push_back(exprs[0]);
689 call->m_params.push_back(exprs[1]);
694 case opid3('<','<','='):
695 case opid3('>','>','='):
696 if (NotSameType(TYPE_FLOAT)) {
697 compile_error(ctx, "invalid types used in expression: cannot perform shift operation between types %s and %s",
698 type_name[exprs[0]->m_vtype],
699 type_name[exprs[1]->m_vtype]);
703 if(!(out = parser.m_fold.op(op, exprs))) {
704 ast_expression *shift = parser.m_intrin.func((op->id == opid3('<','<','=')) ? "__builtin_lshift" : "__builtin_rshift");
705 ast_call *call = ast_call::make(parser_ctx(parser), shift);
706 call->m_params.push_back(exprs[0]);
707 call->m_params.push_back(exprs[1]);
719 generated_op += 1; /* INSTR_OR */
722 generated_op += INSTR_AND;
723 if (!(out = parser.m_fold.op(op, exprs))) {
724 if (OPTS_FLAG(PERL_LOGIC) && !exprs[0]->compareType(*exprs[1])) {
725 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
726 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
727 compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
730 for (i = 0; i < 2; ++i) {
731 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->m_vtype == TYPE_VECTOR) {
732 out = ast_unary::make(ctx, INSTR_NOT_V, exprs[i]);
734 out = ast_unary::make(ctx, INSTR_NOT_F, out);
736 exprs[i] = out; out = nullptr;
737 if (OPTS_FLAG(PERL_LOGIC)) {
738 /* here we want to keep the right expressions' type */
742 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->m_vtype == TYPE_STRING) {
743 out = ast_unary::make(ctx, INSTR_NOT_S, exprs[i]);
745 out = ast_unary::make(ctx, INSTR_NOT_F, out);
747 exprs[i] = out; out = nullptr;
748 if (OPTS_FLAG(PERL_LOGIC)) {
749 /* here we want to keep the right expressions' type */
754 out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
759 if (sy->paren.back() != PAREN_TERNARY2) {
760 compile_error(ctx, "mismatched parenthesis/ternary");
763 sy->paren.pop_back();
764 if (!exprs[1]->compareType(*exprs[2])) {
765 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
766 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
767 compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
770 if (!(out = parser.m_fold.op(op, exprs)))
771 out = new ast_ternary(ctx, exprs[0], exprs[1], exprs[2]);
774 case opid2('*', '*'):
775 if (NotSameType(TYPE_FLOAT)) {
776 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
777 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
778 compile_error(ctx, "invalid types used in exponentiation: %s and %s",
783 if (!(out = parser.m_fold.op(op, exprs))) {
784 ast_call *gencall = ast_call::make(parser_ctx(parser), parser.m_intrin.func("pow"));
785 gencall->m_params.push_back(exprs[0]);
786 gencall->m_params.push_back(exprs[1]);
791 case opid2('>', '<'):
792 if (NotSameType(TYPE_VECTOR)) {
793 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
794 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
795 compile_error(ctx, "invalid types used in cross product: %s and %s",
800 if (!(out = parser.m_fold.op(op, exprs))) {
811 case opid3('<','=','>'): /* -1, 0, or 1 */
812 if (NotSameType(TYPE_FLOAT)) {
813 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
814 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
815 compile_error(ctx, "invalid types used in comparision: %s and %s",
821 if (!(out = parser.m_fold.op(op, exprs))) {
822 /* This whole block is NOT fold_binary safe */
823 ast_binary *eq = new ast_binary(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
825 eq->m_refs = AST_REF_NONE;
828 out = new ast_ternary(ctx,
829 new ast_binary(ctx, INSTR_LT, exprs[0], exprs[1]),
831 parser.m_fold.imm_float(2),
834 new ast_ternary(ctx, eq,
836 parser.m_fold.imm_float(0),
839 parser.m_fold.imm_float(1)
849 generated_op += 1; /* INSTR_GT */
852 generated_op += 1; /* INSTR_LT */
854 case opid2('>', '='):
855 generated_op += 1; /* INSTR_GE */
857 case opid2('<', '='):
858 generated_op += INSTR_LE;
859 if (NotSameType(TYPE_FLOAT)) {
860 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
861 type_name[exprs[0]->m_vtype],
862 type_name[exprs[1]->m_vtype]);
865 if (!(out = parser.m_fold.op(op, exprs)))
866 out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
868 case opid2('!', '='):
869 if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
870 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
871 type_name[exprs[0]->m_vtype],
872 type_name[exprs[1]->m_vtype]);
875 if (!(out = parser.m_fold.op(op, exprs)))
876 out = fold::binary(ctx, type_ne_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
878 case opid2('=', '='):
879 if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
880 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
881 type_name[exprs[0]->m_vtype],
882 type_name[exprs[1]->m_vtype]);
885 if (!(out = parser.m_fold.op(op, exprs)))
886 out = fold::binary(ctx, type_eq_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
890 if (ast_istype(exprs[0], ast_entfield)) {
891 ast_expression *field = ((ast_entfield*)exprs[0])->m_field;
892 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
893 exprs[0]->m_vtype == TYPE_FIELD &&
894 exprs[0]->m_next->m_vtype == TYPE_VECTOR)
896 assignop = type_storep_instr[TYPE_VECTOR];
899 assignop = type_storep_instr[exprs[0]->m_vtype];
900 if (assignop == VINSTR_END || !field->m_next->compareType(*exprs[1]))
902 ast_type_to_string(field->m_next, ty1, sizeof(ty1));
903 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
904 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
905 field->m_next->m_vtype == TYPE_FUNCTION &&
906 exprs[1]->m_vtype == TYPE_FUNCTION)
908 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
909 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
912 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
917 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
918 exprs[0]->m_vtype == TYPE_FIELD &&
919 exprs[0]->m_next->m_vtype == TYPE_VECTOR)
921 assignop = type_store_instr[TYPE_VECTOR];
924 assignop = type_store_instr[exprs[0]->m_vtype];
927 if (assignop == VINSTR_END) {
928 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
929 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
930 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
932 else if (!exprs[0]->compareType(*exprs[1]))
934 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
935 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
936 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
937 exprs[0]->m_vtype == TYPE_FUNCTION &&
938 exprs[1]->m_vtype == TYPE_FUNCTION)
940 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
941 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
944 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
947 (void)check_write_to(ctx, exprs[0]);
948 /* When we're a vector of part of an entity field we use STOREP */
949 if (ast_istype(exprs[0], ast_member) && ast_istype(((ast_member*)exprs[0])->m_owner, ast_entfield))
950 assignop = INSTR_STOREP_F;
951 out = new ast_store(ctx, assignop, exprs[0], exprs[1]);
953 case opid3('+','+','P'):
954 case opid3('-','-','P'):
956 if (exprs[0]->m_vtype != TYPE_FLOAT) {
957 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
958 compile_error(exprs[0]->m_context, "invalid type for prefix increment: %s", ty1);
961 if (op->id == opid3('+','+','P'))
965 (void)check_write_to(exprs[0]->m_context, exprs[0]);
966 if (ast_istype(exprs[0], ast_entfield)) {
967 out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
969 parser.m_fold.imm_float(1));
971 out = new ast_binstore(ctx, INSTR_STORE_F, addop,
973 parser.m_fold.imm_float(1));
976 case opid3('S','+','+'):
977 case opid3('S','-','-'):
979 if (exprs[0]->m_vtype != TYPE_FLOAT) {
980 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
981 compile_error(exprs[0]->m_context, "invalid type for suffix increment: %s", ty1);
984 if (op->id == opid3('S','+','+')) {
991 (void)check_write_to(exprs[0]->m_context, exprs[0]);
992 if (ast_istype(exprs[0], ast_entfield)) {
993 out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
995 parser.m_fold.imm_float(1));
997 out = new ast_binstore(ctx, INSTR_STORE_F, addop,
999 parser.m_fold.imm_float(1));
1003 out = fold::binary(ctx, subop,
1005 parser.m_fold.imm_float(1));
1008 case opid2('+','='):
1009 case opid2('-','='):
1010 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
1011 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
1013 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1014 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1015 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1019 (void)check_write_to(ctx, exprs[0]);
1020 if (ast_istype(exprs[0], ast_entfield))
1021 assignop = type_storep_instr[exprs[0]->m_vtype];
1023 assignop = type_store_instr[exprs[0]->m_vtype];
1024 switch (exprs[0]->m_vtype) {
1026 out = new ast_binstore(ctx, assignop,
1027 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1028 exprs[0], exprs[1]);
1031 out = new ast_binstore(ctx, assignop,
1032 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1033 exprs[0], exprs[1]);
1036 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1037 type_name[exprs[0]->m_vtype],
1038 type_name[exprs[1]->m_vtype]);
1042 case opid2('*','='):
1043 case opid2('/','='):
1044 if (exprs[1]->m_vtype != TYPE_FLOAT ||
1045 !(exprs[0]->m_vtype == TYPE_FLOAT ||
1046 exprs[0]->m_vtype == TYPE_VECTOR))
1048 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1049 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1050 compile_error(ctx, "invalid types used in expression: %s and %s",
1054 (void)check_write_to(ctx, exprs[0]);
1055 if (ast_istype(exprs[0], ast_entfield))
1056 assignop = type_storep_instr[exprs[0]->m_vtype];
1058 assignop = type_store_instr[exprs[0]->m_vtype];
1059 switch (exprs[0]->m_vtype) {
1061 out = new ast_binstore(ctx, assignop,
1062 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1063 exprs[0], exprs[1]);
1066 if (op->id == opid2('*','=')) {
1067 out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
1068 exprs[0], exprs[1]);
1070 out = fold::binary(ctx, INSTR_DIV_F,
1071 parser.m_fold.imm_float(1),
1074 compile_error(ctx, "internal error: failed to generate division");
1077 out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
1082 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1083 type_name[exprs[0]->m_vtype],
1084 type_name[exprs[1]->m_vtype]);
1088 case opid2('&','='):
1089 case opid2('|','='):
1090 case opid2('^','='):
1091 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1092 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1093 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1094 compile_error(ctx, "invalid types used in expression: %s and %s",
1098 (void)check_write_to(ctx, exprs[0]);
1099 if (ast_istype(exprs[0], ast_entfield))
1100 assignop = type_storep_instr[exprs[0]->m_vtype];
1102 assignop = type_store_instr[exprs[0]->m_vtype];
1103 if (exprs[0]->m_vtype == TYPE_FLOAT)
1104 out = new ast_binstore(ctx, assignop,
1105 (op->id == opid2('^','=') ? VINSTR_BITXOR : op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1106 exprs[0], exprs[1]);
1108 out = new ast_binstore(ctx, assignop,
1109 (op->id == opid2('^','=') ? VINSTR_BITXOR_V : op->id == opid2('&','=') ? VINSTR_BITAND_V : VINSTR_BITOR_V),
1110 exprs[0], exprs[1]);
1112 case opid3('&','~','='):
1113 /* This is like: a &= ~(b);
1114 * But QC has no bitwise-not, so we implement it as
1117 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1118 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1119 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1120 compile_error(ctx, "invalid types used in expression: %s and %s",
1124 if (ast_istype(exprs[0], ast_entfield))
1125 assignop = type_storep_instr[exprs[0]->m_vtype];
1127 assignop = type_store_instr[exprs[0]->m_vtype];
1128 if (exprs[0]->m_vtype == TYPE_FLOAT)
1129 out = fold::binary(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1131 out = fold::binary(ctx, VINSTR_BITAND_V, exprs[0], exprs[1]);
1134 (void)check_write_to(ctx, exprs[0]);
1135 if (exprs[0]->m_vtype == TYPE_FLOAT)
1136 asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1138 asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_V, exprs[0], out);
1139 asbinstore->m_keep_dest = true;
1143 case opid3('l', 'e', 'n'):
1144 if (exprs[0]->m_vtype != TYPE_STRING && exprs[0]->m_vtype != TYPE_ARRAY) {
1145 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1146 compile_error(exprs[0]->m_context, "invalid type for length operator: %s", ty1);
1149 /* strings must be const, arrays are statically sized */
1150 if (exprs[0]->m_vtype == TYPE_STRING &&
1151 !(((ast_value*)exprs[0])->m_hasvalue && ((ast_value*)exprs[0])->m_cvq == CV_CONST))
1153 compile_error(exprs[0]->m_context, "operand of length operator not a valid constant expression");
1156 out = parser.m_fold.op(op, exprs);
1159 case opid2('~', 'P'):
1160 if (exprs[0]->m_vtype != TYPE_FLOAT && exprs[0]->m_vtype != TYPE_VECTOR) {
1161 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1162 compile_error(exprs[0]->m_context, "invalid type for bit not: %s", ty1);
1165 if (!(out = parser.m_fold.op(op, exprs))) {
1166 if (exprs[0]->m_vtype == TYPE_FLOAT) {
1167 out = fold::binary(ctx, INSTR_SUB_F, parser.m_fold.imm_float(2), exprs[0]);
1169 out = fold::binary(ctx, INSTR_SUB_V, parser.m_fold.imm_vector(1), exprs[0]);
1176 compile_error(ctx, "failed to apply operator %s", op->op);
1180 sy->out.push_back(syexp(ctx, out));
1184 static bool parser_close_call(parser_t &parser, shunt *sy)
1186 /* was a function call */
1187 ast_expression *fun;
1188 ast_value *funval = nullptr;
1192 size_t paramcount, i;
1195 fid = sy->ops.back().off;
1198 /* out[fid] is the function
1199 * everything above is parameters...
1201 if (sy->argc.empty()) {
1202 parseerror(parser, "internal error: no argument counter available");
1206 paramcount = sy->argc.back();
1207 sy->argc.pop_back();
1209 if (sy->out.size() < fid) {
1210 parseerror(parser, "internal error: broken function call %zu < %zu+%zu\n",
1218 * TODO handle this at the intrinsic level with an ast_intrinsic
1221 if ((fun = sy->out[fid].out) == parser.m_intrin.debug_typestring()) {
1223 if (fid+2 != sy->out.size() || sy->out.back().block) {
1224 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1227 ast_type_to_string(sy->out.back().out, ty, sizeof(ty));
1228 ast_unref(sy->out.back().out);
1229 sy->out[fid] = syexp(sy->out.back().out->m_context,
1230 parser.m_fold.constgen_string(ty, false));
1236 * Now we need to determine if the function that is being called is
1237 * an intrinsic so we can evaluate if the arguments to it are constant
1238 * and than fruitfully fold them.
1240 #define fold_can_1(X) \
1241 (ast_istype(((X)), ast_value) && (X)->m_hasvalue && ((X)->m_cvq == CV_CONST) && \
1242 ((X))->m_vtype != TYPE_FUNCTION)
1244 if (fid + 1 < sy->out.size())
1247 for (i = 0; i < paramcount; ++i) {
1248 if (!fold_can_1((ast_value*)sy->out[fid + 1 + i].out)) {
1255 * All is well which ends well, if we make it into here we can ignore the
1256 * intrinsic call and just evaluate it i.e constant fold it.
1258 if (fold && ast_istype(fun, ast_value) && ((ast_value*)fun)->m_intrinsic) {
1259 std::vector<ast_expression*> exprs;
1260 ast_expression *foldval = nullptr;
1262 exprs.reserve(paramcount);
1263 for (i = 0; i < paramcount; i++)
1264 exprs.push_back(sy->out[fid+1 + i].out);
1266 if (!(foldval = parser.m_intrin.do_fold((ast_value*)fun, exprs.data()))) {
1271 * Blub: what sorts of unreffing and resizing of
1272 * sy->out should I be doing here?
1274 sy->out[fid] = syexp(foldval->m_context, foldval);
1275 sy->out.erase(sy->out.end() - paramcount, sy->out.end());
1281 call = ast_call::make(sy->ops[sy->ops.size()].ctx, fun);
1286 if (fid+1 + paramcount != sy->out.size()) {
1287 parseerror(parser, "internal error: parameter count mismatch: (%zu+1+%zu), %zu",
1294 for (i = 0; i < paramcount; ++i)
1295 call->m_params.push_back(sy->out[fid+1 + i].out);
1296 sy->out.erase(sy->out.end() - paramcount, sy->out.end());
1297 (void)!call->checkTypes(parser.function->m_function_type->m_varparam);
1298 if (parser.max_param_count < paramcount)
1299 parser.max_param_count = paramcount;
1301 if (ast_istype(fun, ast_value)) {
1302 funval = (ast_value*)fun;
1303 if ((fun->m_flags & AST_FLAG_VARIADIC) &&
1304 !(/*funval->m_cvq == CV_CONST && */ funval->m_hasvalue && funval->m_constval.vfunc->m_builtin))
1306 call->m_va_count = parser.m_fold.constgen_float((qcfloat_t)paramcount, false);
1310 /* overwrite fid, the function, with a call */
1311 sy->out[fid] = syexp(call->m_context, call);
1313 if (fun->m_vtype != TYPE_FUNCTION) {
1314 parseerror(parser, "not a function (%s)", type_name[fun->m_vtype]);
1319 parseerror(parser, "could not determine function return type");
1322 ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : nullptr);
1324 if (fun->m_flags & AST_FLAG_DEPRECATED) {
1326 return !parsewarning(parser, WARN_DEPRECATED,
1327 "call to function (which is marked deprecated)\n",
1328 "-> it has been declared here: %s:%i",
1329 fun->m_context.file, fun->m_context.line);
1331 if (!fval->m_desc.length()) {
1332 return !parsewarning(parser, WARN_DEPRECATED,
1333 "call to `%s` (which is marked deprecated)\n"
1334 "-> `%s` declared here: %s:%i",
1335 fval->m_name, fval->m_name, fun->m_context.file, fun->m_context.line);
1337 return !parsewarning(parser, WARN_DEPRECATED,
1338 "call to `%s` (deprecated: %s)\n"
1339 "-> `%s` declared here: %s:%i",
1340 fval->m_name, fval->m_desc, fval->m_name, fun->m_context.file,
1341 fun->m_context.line);
1344 if (fun->m_type_params.size() != paramcount &&
1345 !((fun->m_flags & AST_FLAG_VARIADIC) &&
1346 fun->m_type_params.size() < paramcount))
1348 const char *fewmany = (fun->m_type_params.size() > paramcount) ? "few" : "many";
1350 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1351 "too %s parameters for call to %s: expected %i, got %i\n"
1352 " -> `%s` has been declared here: %s:%i",
1353 fewmany, fval->m_name, (int)fun->m_type_params.size(), (int)paramcount,
1354 fval->m_name, fun->m_context.file, (int)fun->m_context.line);
1356 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1357 "too %s parameters for function call: expected %i, got %i\n"
1358 " -> it has been declared here: %s:%i",
1359 fewmany, (int)fun->m_type_params.size(), (int)paramcount,
1360 fun->m_context.file, (int)fun->m_context.line);
1367 static bool parser_close_paren(parser_t &parser, shunt *sy)
1369 if (sy->ops.empty()) {
1370 parseerror(parser, "unmatched closing paren");
1374 while (sy->ops.size()) {
1375 if (sy->ops.back().isparen) {
1376 if (sy->paren.back() == PAREN_FUNC) {
1377 sy->paren.pop_back();
1378 if (!parser_close_call(parser, sy))
1382 if (sy->paren.back() == PAREN_EXPR) {
1383 sy->paren.pop_back();
1384 if (sy->out.empty()) {
1385 compile_error(sy->ops.back().ctx, "empty paren expression");
1392 if (sy->paren.back() == PAREN_INDEX) {
1393 sy->paren.pop_back();
1394 // pop off the parenthesis
1396 /* then apply the index operator */
1397 if (!parser_sy_apply_operator(parser, sy))
1401 if (sy->paren.back() == PAREN_TERNARY1) {
1402 sy->paren.back() = PAREN_TERNARY2;
1403 // pop off the parenthesis
1407 compile_error(sy->ops.back().ctx, "invalid parenthesis");
1410 if (!parser_sy_apply_operator(parser, sy))
1416 static void parser_reclassify_token(parser_t &parser)
1419 if (parser.tok >= Token::START)
1421 for (i = 0; i < operator_count; ++i) {
1422 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1423 parser.tok = Token::OPERATOR;
1429 static ast_expression* parse_vararg_do(parser_t &parser)
1431 ast_expression *idx, *out;
1433 ast_value *funtype = parser.function->m_function_type;
1434 lex_ctx_t ctx = parser_ctx(parser);
1436 if (!parser.function->m_varargs) {
1437 parseerror(parser, "function has no variable argument list");
1441 if (!parser_next(parser) || parser.tok != '(') {
1442 parseerror(parser, "expected parameter index and type in parenthesis");
1445 if (!parser_next(parser)) {
1446 parseerror(parser, "error parsing parameter index");
1450 idx = parse_expression_leave(parser, true, false, false);
1454 if (parser.tok != ',') {
1455 if (parser.tok != ')') {
1457 parseerror(parser, "expected comma after parameter index");
1460 // vararg piping: ...(start)
1461 out = new ast_argpipe(ctx, idx);
1465 if (!parser_next(parser) || (parser.tok != Token::IDENT && parser.tok != Token::TYPENAME)) {
1467 parseerror(parser, "expected typename for vararg");
1471 typevar = parse_typename(parser, nullptr, nullptr, nullptr);
1477 if (parser.tok != ')') {
1480 parseerror(parser, "expected closing paren");
1484 if (funtype->m_varparam &&
1485 !typevar->compareType(*funtype->m_varparam))
1489 ast_type_to_string(typevar, ty1, sizeof(ty1));
1490 ast_type_to_string(funtype->m_varparam, ty2, sizeof(ty2));
1491 compile_error(typevar->m_context,
1492 "function was declared to take varargs of type `%s`, requested type is: %s",
1496 out = ast_array_index::make(ctx, parser.function->m_varargs.get(), idx);
1497 out->adoptType(*typevar);
1502 static ast_expression* parse_vararg(parser_t &parser)
1504 bool old_noops = parser.lex->flags.noops;
1506 ast_expression *out;
1508 parser.lex->flags.noops = true;
1509 out = parse_vararg_do(parser);
1511 parser.lex->flags.noops = old_noops;
1515 /* not to be exposed */
1516 bool ftepp_predef_exists(const char *name);
1517 static bool parse_sya_operand(parser_t &parser, shunt *sy, bool with_labels)
1519 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1520 parser.tok == Token::IDENT &&
1521 !strcmp(parser_tokval(parser), "_"))
1523 /* a translatable string */
1526 parser.lex->flags.noops = true;
1527 if (!parser_next(parser) || parser.tok != '(') {
1528 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1531 parser.lex->flags.noops = false;
1532 if (!parser_next(parser) || parser.tok != Token::STRINGCONST) {
1533 parseerror(parser, "expected a constant string in translatable-string extension");
1536 val = (ast_value*)parser.m_fold.constgen_string(parser_tokval(parser), true);
1539 sy->out.push_back(syexp(parser_ctx(parser), val));
1541 if (!parser_next(parser) || parser.tok != ')') {
1542 parseerror(parser, "expected closing paren after translatable string");
1547 else if (parser.tok == Token::DOTS)
1550 if (!OPTS_FLAG(VARIADIC_ARGS)) {
1551 parseerror(parser, "cannot access varargs (try -fvariadic-args)");
1554 va = parse_vararg(parser);
1557 sy->out.push_back(syexp(parser_ctx(parser), va));
1560 else if (parser.tok == Token::FLOATCONST) {
1561 ast_expression *val = parser.m_fold.constgen_float((parser_token(parser)->constval.f), false);
1564 sy->out.push_back(syexp(parser_ctx(parser), val));
1567 else if (parser.tok == Token::INTCONST || parser.tok == Token::CHARCONST) {
1568 ast_expression *val = parser.m_fold.constgen_float((qcfloat_t)(parser_token(parser)->constval.i), false);
1571 sy->out.push_back(syexp(parser_ctx(parser), val));
1574 else if (parser.tok == Token::STRINGCONST) {
1575 ast_expression *val = parser.m_fold.constgen_string(parser_tokval(parser), false);
1578 sy->out.push_back(syexp(parser_ctx(parser), val));
1581 else if (parser.tok == Token::VECTORCONST) {
1582 ast_expression *val = parser.m_fold.constgen_vector(parser_token(parser)->constval.v);
1585 sy->out.push_back(syexp(parser_ctx(parser), val));
1588 else if (parser.tok == Token::IDENT)
1590 const char *ctoken = parser_tokval(parser);
1591 ast_expression *prev = sy->out.size() ? sy->out.back().out : nullptr;
1592 ast_expression *var;
1593 /* a_vector.{x,y,z} */
1594 if (sy->ops.empty() ||
1595 !sy->ops.back().etype ||
1596 operators[sy->ops.back().etype-1].id != opid1('.'))
1598 /* When adding more intrinsics, fix the above condition */
1601 if (prev && prev->m_vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
1603 var = parser.const_vec[ctoken[0]-'x'];
1605 var = parser_find_var(parser, parser_tokval(parser));
1607 var = parser_find_field(parser, parser_tokval(parser));
1609 if (!var && with_labels) {
1610 var = parser_find_label(parser, parser_tokval(parser));
1612 ast_label *lbl = new ast_label(parser_ctx(parser), parser_tokval(parser), true);
1614 parser.labels.push_back(lbl);
1617 if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
1618 var = parser.m_fold.constgen_string(parser.function->m_name, false);
1621 * now we try for the real intrinsic hashtable. If the string
1622 * begins with __builtin, we simply skip past it, otherwise we
1623 * use the identifier as is.
1625 if (!strncmp(parser_tokval(parser), "__builtin_", 10)) {
1626 var = parser.m_intrin.func(parser_tokval(parser));
1630 * Try it again, intrin_func deals with the alias method as well
1631 * the first one masks for __builtin though, we emit warning here.
1634 if ((var = parser.m_intrin.func(parser_tokval(parser)))) {
1635 (void)!compile_warning(
1638 "using implicitly defined builtin `__builtin_%s' for `%s'",
1639 parser_tokval(parser),
1640 parser_tokval(parser)
1648 * sometimes people use preprocessing predefs without enabling them
1649 * i've done this thousands of times already myself. Lets check for
1650 * it in the predef table. And diagnose it better :)
1652 if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
1653 parseerror(parser, "unexpected identifier: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
1657 parseerror(parser, "unexpected identifier: %s", parser_tokval(parser));
1663 // promote these to norefs
1664 if (ast_istype(var, ast_value))
1666 ((ast_value *)var)->m_flags |= AST_FLAG_NOREF;
1668 else if (ast_istype(var, ast_member))
1670 ast_member *mem = (ast_member *)var;
1671 if (ast_istype(mem->m_owner, ast_value))
1672 ((ast_value *)mem->m_owner)->m_flags |= AST_FLAG_NOREF;
1675 sy->out.push_back(syexp(parser_ctx(parser), var));
1678 parseerror(parser, "unexpected token `%s`", parser_tokval(parser));
1682 static ast_expression* parse_expression_leave(parser_t &parser, bool stopatcomma, bool truthvalue, bool with_labels)
1684 ast_expression *expr = nullptr;
1686 bool wantop = false;
1687 /* only warn once about an assignment in a truth value because the current code
1688 * would trigger twice on: if(a = b && ...), once for the if-truth-value, once for the && part
1690 bool warn_parenthesis = true;
1692 /* count the parens because an if starts with one, so the
1693 * end of a condition is an unmatched closing paren
1697 memset(&sy, 0, sizeof(sy));
1699 parser.lex->flags.noops = false;
1701 parser_reclassify_token(parser);
1705 if (parser.tok == Token::TYPENAME) {
1706 parseerror(parser, "unexpected typename `%s`", parser_tokval(parser));
1710 if (parser.tok == Token::OPERATOR)
1712 /* classify the operator */
1713 const oper_info *op;
1714 const oper_info *olast = nullptr;
1716 for (o = 0; o < operator_count; ++o) {
1717 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
1718 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1719 !strcmp(parser_tokval(parser), operators[o].op))
1724 if (o == operator_count) {
1725 compile_error(parser_ctx(parser), "unexpected operator: %s", parser_tokval(parser));
1728 /* found an operator */
1731 /* when declaring variables, a comma starts a new variable */
1732 if (op->id == opid1(',') && sy.paren.empty() && stopatcomma) {
1733 /* fixup the token */
1734 parser.tok = Token::COMMA;
1738 /* a colon without a pervious question mark cannot be a ternary */
1739 if (!ternaries && op->id == opid2(':','?')) {
1740 parser.tok = Token::COLON;
1744 if (op->id == opid1(',')) {
1745 if (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1746 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
1750 if (sy.ops.size() && !sy.ops.back().isparen)
1751 olast = &operators[sy.ops.back().etype-1];
1753 /* first only apply higher precedences, assoc_left+equal comes after we warn about precedence rules */
1754 while (olast && op->prec < olast->prec)
1756 if (!parser_sy_apply_operator(parser, &sy))
1758 if (sy.ops.size() && !sy.ops.back().isparen)
1759 olast = &operators[sy.ops.back().etype-1];
1764 #define IsAssignOp(x) (\
1765 (x) == opid1('=') || \
1766 (x) == opid2('+','=') || \
1767 (x) == opid2('-','=') || \
1768 (x) == opid2('*','=') || \
1769 (x) == opid2('/','=') || \
1770 (x) == opid2('%','=') || \
1771 (x) == opid2('&','=') || \
1772 (x) == opid2('|','=') || \
1773 (x) == opid3('&','~','=') \
1775 if (warn_parenthesis) {
1776 if ( (olast && IsAssignOp(olast->id) && (op->id == opid2('&','&') || op->id == opid2('|','|'))) ||
1777 (olast && IsAssignOp(op->id) && (olast->id == opid2('&','&') || olast->id == opid2('|','|'))) ||
1778 (truthvalue && sy.paren.empty() && IsAssignOp(op->id))
1781 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around assignment used as truth value");
1782 warn_parenthesis = false;
1785 if (olast && olast->id != op->id) {
1786 if ((op->id == opid1('&') || op->id == opid1('|') || op->id == opid1('^')) &&
1787 (olast->id == opid1('&') || olast->id == opid1('|') || olast->id == opid1('^')))
1789 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around bitwise operations");
1790 warn_parenthesis = false;
1792 else if ((op->id == opid2('&','&') || op->id == opid2('|','|')) &&
1793 (olast->id == opid2('&','&') || olast->id == opid2('|','|')))
1795 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around logical operations");
1796 warn_parenthesis = false;
1802 (op->prec < olast->prec) ||
1803 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1805 if (!parser_sy_apply_operator(parser, &sy))
1807 if (sy.ops.size() && !sy.ops.back().isparen)
1808 olast = &operators[sy.ops.back().etype-1];
1813 if (op->id == opid1('(')) {
1815 size_t sycount = sy.out.size();
1816 /* we expected an operator, this is the function-call operator */
1817 sy.paren.push_back(PAREN_FUNC);
1818 sy.ops.push_back(syparen(parser_ctx(parser), sycount-1));
1819 sy.argc.push_back(0);
1821 sy.paren.push_back(PAREN_EXPR);
1822 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1825 } else if (op->id == opid1('[')) {
1827 parseerror(parser, "unexpected array subscript");
1830 sy.paren.push_back(PAREN_INDEX);
1831 /* push both the operator and the paren, this makes life easier */
1832 sy.ops.push_back(syop(parser_ctx(parser), op));
1833 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1835 } else if (op->id == opid2('?',':')) {
1836 sy.ops.push_back(syop(parser_ctx(parser), op));
1837 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1840 sy.paren.push_back(PAREN_TERNARY1);
1841 } else if (op->id == opid2(':','?')) {
1842 if (sy.paren.empty()) {
1843 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1846 if (sy.paren.back() != PAREN_TERNARY1) {
1847 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1850 if (!parser_close_paren(parser, &sy))
1852 sy.ops.push_back(syop(parser_ctx(parser), op));
1856 sy.ops.push_back(syop(parser_ctx(parser), op));
1857 wantop = !!(op->flags & OP_SUFFIX);
1860 else if (parser.tok == ')') {
1861 while (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1862 if (!parser_sy_apply_operator(parser, &sy))
1865 if (sy.paren.empty())
1868 if (sy.paren.back() == PAREN_TERNARY1) {
1869 parseerror(parser, "mismatched parentheses (closing paren in ternary expression?)");
1872 if (!parser_close_paren(parser, &sy))
1875 /* must be a function call without parameters */
1876 if (sy.paren.back() != PAREN_FUNC) {
1877 parseerror(parser, "closing paren in invalid position");
1880 if (!parser_close_paren(parser, &sy))
1885 else if (parser.tok == '(') {
1886 parseerror(parser, "internal error: '(' should be classified as operator");
1889 else if (parser.tok == '[') {
1890 parseerror(parser, "internal error: '[' should be classified as operator");
1893 else if (parser.tok == ']') {
1894 while (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1895 if (!parser_sy_apply_operator(parser, &sy))
1898 if (sy.paren.empty())
1900 if (sy.paren.back() != PAREN_INDEX) {
1901 parseerror(parser, "mismatched parentheses, unexpected ']'");
1904 if (!parser_close_paren(parser, &sy))
1909 if (!parse_sya_operand(parser, &sy, with_labels))
1914 /* in this case we might want to allow constant string concatenation */
1915 bool concatenated = false;
1916 if (parser.tok == Token::STRINGCONST && sy.out.size()) {
1917 ast_expression *lexpr = sy.out.back().out;
1918 if (ast_istype(lexpr, ast_value)) {
1919 ast_value *last = (ast_value*)lexpr;
1920 if (last->m_isimm == true && last->m_cvq == CV_CONST &&
1921 last->m_hasvalue && last->m_vtype == TYPE_STRING)
1923 char *newstr = nullptr;
1924 util_asprintf(&newstr, "%s%s", last->m_constval.vstring, parser_tokval(parser));
1925 sy.out.back().out = parser.m_fold.constgen_string(newstr, false);
1927 concatenated = true;
1931 if (!concatenated) {
1932 parseerror(parser, "expected operator or end of statement");
1937 if (!parser_next(parser)) {
1940 if (parser.tok == ';' ||
1941 ((sy.paren.empty() || (sy.paren.size() == 1 && sy.paren.back() == PAREN_TERNARY2)) &&
1942 (parser.tok == ']' || parser.tok == ')' || parser.tok == '}')))
1948 while (sy.ops.size()) {
1949 if (!parser_sy_apply_operator(parser, &sy))
1953 parser.lex->flags.noops = true;
1954 if (sy.out.size() != 1) {
1955 parseerror(parser, "expression expected");
1958 expr = sy.out[0].out;
1959 if (sy.paren.size()) {
1960 parseerror(parser, "internal error: sy.paren.size() = %zu", sy.paren.size());
1966 parser.lex->flags.noops = true;
1967 for (auto &it : sy.out)
1968 if (it.out) ast_unref(it.out);
1972 static ast_expression* parse_expression(parser_t &parser, bool stopatcomma, bool with_labels)
1974 ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
1977 if (parser.tok != ';') {
1978 parseerror(parser, "semicolon expected after expression");
1982 if (!parser_next(parser)) {
1989 static void parser_enterblock(parser_t &parser)
1991 parser.variables.push_back(util_htnew(PARSER_HT_SIZE));
1992 parser._blocklocals.push_back(parser._locals.size());
1993 parser.typedefs.push_back(util_htnew(TYPEDEF_HT_SIZE));
1994 parser._blocktypedefs.push_back(parser._typedefs.size());
1995 parser._block_ctx.push_back(parser_ctx(parser));
1998 static bool parser_leaveblock(parser_t &parser)
2001 size_t locals, typedefs;
2003 if (parser.variables.size() <= PARSER_HT_LOCALS) {
2004 parseerror(parser, "internal error: parser_leaveblock with no block");
2008 util_htdel(parser.variables.back());
2010 parser.variables.pop_back();
2011 if (!parser._blocklocals.size()) {
2012 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
2016 locals = parser._blocklocals.back();
2017 parser._blocklocals.pop_back();
2018 parser._locals.resize(locals);
2020 typedefs = parser._blocktypedefs.back();
2021 parser._typedefs.resize(typedefs);
2022 util_htdel(parser.typedefs.back());
2023 parser.typedefs.pop_back();
2025 parser._block_ctx.pop_back();
2030 static void parser_addlocal(parser_t &parser, const char *name, ast_expression *e)
2032 parser._locals.push_back(e);
2033 util_htset(parser.variables.back(), name, (void*)e);
2035 static void parser_addlocal(parser_t &parser, const std::string &name, ast_expression *e) {
2036 return parser_addlocal(parser, name.c_str(), e);
2039 static void parser_addglobal(parser_t &parser, const char *name, ast_expression *e)
2041 parser.globals.push_back(e);
2042 util_htset(parser.htglobals, name, e);
2044 static void parser_addglobal(parser_t &parser, const std::string &name, ast_expression *e) {
2045 return parser_addglobal(parser, name.c_str(), e);
2048 static ast_expression* process_condition(parser_t &parser, ast_expression *cond, bool *_ifnot)
2052 ast_expression *prev;
2054 if (cond->m_vtype == TYPE_VOID || cond->m_vtype >= TYPE_VARIANT) {
2056 ast_type_to_string(cond, ty, sizeof(ty));
2057 compile_error(cond->m_context, "invalid type for if() condition: %s", ty);
2060 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->m_vtype == TYPE_STRING)
2063 cond = ast_unary::make(cond->m_context, INSTR_NOT_S, cond);
2066 parseerror(parser, "internal error: failed to process condition");
2071 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->m_vtype == TYPE_VECTOR)
2073 /* vector types need to be cast to true booleans */
2074 ast_binary *bin = (ast_binary*)cond;
2075 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->m_op == INSTR_AND || bin->m_op == INSTR_OR))
2077 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
2079 cond = ast_unary::make(cond->m_context, INSTR_NOT_V, cond);
2082 parseerror(parser, "internal error: failed to process condition");
2089 unary = (ast_unary*)cond;
2090 /* ast_istype dereferences cond, should test here for safety */
2091 while (cond && ast_istype(cond, ast_unary) && unary->m_op == INSTR_NOT_F)
2093 cond = unary->m_operand;
2094 unary->m_operand = nullptr;
2097 unary = (ast_unary*)cond;
2101 parseerror(parser, "internal error: failed to process condition");
2103 if (ifnot) *_ifnot = !*_ifnot;
2107 static bool parse_if(parser_t &parser, ast_block *block, ast_expression **out)
2110 ast_expression *cond, *ontrue = nullptr, *onfalse = nullptr;
2113 lex_ctx_t ctx = parser_ctx(parser);
2115 (void)block; /* not touching */
2117 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2118 if (!parser_next(parser)) {
2119 parseerror(parser, "expected condition or 'not'");
2122 if (parser.tok == Token::IDENT && !strcmp(parser_tokval(parser), "not")) {
2124 if (!parser_next(parser)) {
2125 parseerror(parser, "expected condition in parenthesis");
2129 if (parser.tok != '(') {
2130 parseerror(parser, "expected 'if' condition in parenthesis");
2133 /* parse into the expression */
2134 if (!parser_next(parser)) {
2135 parseerror(parser, "expected 'if' condition after opening paren");
2138 /* parse the condition */
2139 cond = parse_expression_leave(parser, false, true, false);
2143 if (parser.tok != ')') {
2144 parseerror(parser, "expected closing paren after 'if' condition");
2148 /* parse into the 'then' branch */
2149 if (!parser_next(parser)) {
2150 parseerror(parser, "expected statement for on-true branch of 'if'");
2154 if (!parse_statement_or_block(parser, &ontrue)) {
2159 ontrue = new ast_block(parser_ctx(parser));
2160 /* check for an else */
2161 if (!strcmp(parser_tokval(parser), "else")) {
2162 /* parse into the 'else' branch */
2163 if (!parser_next(parser)) {
2164 parseerror(parser, "expected on-false branch after 'else'");
2169 if (!parse_statement_or_block(parser, &onfalse)) {
2176 cond = process_condition(parser, cond, &ifnot);
2178 if (ontrue) delete ontrue;
2179 if (onfalse) delete onfalse;
2184 ifthen = new ast_ifthen(ctx, cond, onfalse, ontrue);
2186 ifthen = new ast_ifthen(ctx, cond, ontrue, onfalse);
2191 static bool parse_while_go(parser_t &parser, ast_block *block, ast_expression **out);
2192 static bool parse_while(parser_t &parser, ast_block *block, ast_expression **out)
2195 char *label = nullptr;
2197 /* skip the 'while' and get the body */
2198 if (!parser_next(parser)) {
2199 if (OPTS_FLAG(LOOP_LABELS))
2200 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2202 parseerror(parser, "expected 'while' condition in parenthesis");
2206 if (parser.tok == ':') {
2207 if (!OPTS_FLAG(LOOP_LABELS))
2208 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2209 if (!parser_next(parser) || parser.tok != Token::IDENT) {
2210 parseerror(parser, "expected loop label");
2213 label = util_strdup(parser_tokval(parser));
2214 if (!parser_next(parser)) {
2216 parseerror(parser, "expected 'while' condition in parenthesis");
2221 if (parser.tok != '(') {
2222 parseerror(parser, "expected 'while' condition in parenthesis");
2226 parser.breaks.push_back(label);
2227 parser.continues.push_back(label);
2229 rv = parse_while_go(parser, block, out);
2232 if (parser.breaks.back() != label || parser.continues.back() != label) {
2233 parseerror(parser, "internal error: label stack corrupted");
2239 parser.breaks.pop_back();
2240 parser.continues.pop_back();
2245 static bool parse_while_go(parser_t &parser, ast_block *block, ast_expression **out)
2248 ast_expression *cond, *ontrue;
2252 lex_ctx_t ctx = parser_ctx(parser);
2254 (void)block; /* not touching */
2256 /* parse into the expression */
2257 if (!parser_next(parser)) {
2258 parseerror(parser, "expected 'while' condition after opening paren");
2261 /* parse the condition */
2262 cond = parse_expression_leave(parser, false, true, false);
2266 if (parser.tok != ')') {
2267 parseerror(parser, "expected closing paren after 'while' condition");
2271 /* parse into the 'then' branch */
2272 if (!parser_next(parser)) {
2273 parseerror(parser, "expected while-loop body");
2277 if (!parse_statement_or_block(parser, &ontrue)) {
2282 cond = process_condition(parser, cond, &ifnot);
2287 aloop = new ast_loop(ctx, nullptr, cond, ifnot, nullptr, false, nullptr, ontrue);
2292 static bool parse_dowhile_go(parser_t &parser, ast_block *block, ast_expression **out);
2293 static bool parse_dowhile(parser_t &parser, ast_block *block, ast_expression **out)
2296 char *label = nullptr;
2298 /* skip the 'do' and get the body */
2299 if (!parser_next(parser)) {
2300 if (OPTS_FLAG(LOOP_LABELS))
2301 parseerror(parser, "expected loop label or body");
2303 parseerror(parser, "expected loop body");
2307 if (parser.tok == ':') {
2308 if (!OPTS_FLAG(LOOP_LABELS))
2309 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2310 if (!parser_next(parser) || parser.tok != Token::IDENT) {
2311 parseerror(parser, "expected loop label");
2314 label = util_strdup(parser_tokval(parser));
2315 if (!parser_next(parser)) {
2317 parseerror(parser, "expected loop body");
2322 parser.breaks.push_back(label);
2323 parser.continues.push_back(label);
2325 rv = parse_dowhile_go(parser, block, out);
2328 if (parser.breaks.back() != label || parser.continues.back() != label) {
2329 parseerror(parser, "internal error: label stack corrupted");
2335 parser.breaks.pop_back();
2336 parser.continues.pop_back();
2341 static bool parse_dowhile_go(parser_t &parser, ast_block *block, ast_expression **out)
2344 ast_expression *cond, *ontrue;
2348 lex_ctx_t ctx = parser_ctx(parser);
2350 (void)block; /* not touching */
2352 if (!parse_statement_or_block(parser, &ontrue))
2355 /* expect the "while" */
2356 if (parser.tok != Token::KEYWORD ||
2357 strcmp(parser_tokval(parser), "while"))
2359 parseerror(parser, "expected 'while' and condition");
2364 /* skip the 'while' and check for opening paren */
2365 if (!parser_next(parser) || parser.tok != '(') {
2366 parseerror(parser, "expected 'while' condition in parenthesis");
2370 /* parse into the expression */
2371 if (!parser_next(parser)) {
2372 parseerror(parser, "expected 'while' condition after opening paren");
2376 /* parse the condition */
2377 cond = parse_expression_leave(parser, false, true, false);
2381 if (parser.tok != ')') {
2382 parseerror(parser, "expected closing paren after 'while' condition");
2388 if (!parser_next(parser) || parser.tok != ';') {
2389 parseerror(parser, "expected semicolon after condition");
2395 if (!parser_next(parser)) {
2396 parseerror(parser, "parse error");
2402 cond = process_condition(parser, cond, &ifnot);
2407 aloop = new ast_loop(ctx, nullptr, nullptr, false, cond, ifnot, nullptr, ontrue);
2412 static bool parse_for_go(parser_t &parser, ast_block *block, ast_expression **out);
2413 static bool parse_for(parser_t &parser, ast_block *block, ast_expression **out)
2416 char *label = nullptr;
2418 /* skip the 'for' and check for opening paren */
2419 if (!parser_next(parser)) {
2420 if (OPTS_FLAG(LOOP_LABELS))
2421 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2423 parseerror(parser, "expected 'for' expressions in parenthesis");
2427 if (parser.tok == ':') {
2428 if (!OPTS_FLAG(LOOP_LABELS))
2429 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2430 if (!parser_next(parser) || parser.tok != Token::IDENT) {
2431 parseerror(parser, "expected loop label");
2434 label = util_strdup(parser_tokval(parser));
2435 if (!parser_next(parser)) {
2437 parseerror(parser, "expected 'for' expressions in parenthesis");
2442 if (parser.tok != '(') {
2443 parseerror(parser, "expected 'for' expressions in parenthesis");
2447 parser.breaks.push_back(label);
2448 parser.continues.push_back(label);
2450 rv = parse_for_go(parser, block, out);
2453 if (parser.breaks.back() != label || parser.continues.back() != label) {
2454 parseerror(parser, "internal error: label stack corrupted");
2460 parser.breaks.pop_back();
2461 parser.continues.pop_back();
2465 static bool parse_for_go(parser_t &parser, ast_block *block, ast_expression **out)
2468 ast_expression *initexpr, *cond, *increment, *ontrue;
2473 lex_ctx_t ctx = parser_ctx(parser);
2475 parser_enterblock(parser);
2479 increment = nullptr;
2482 /* parse into the expression */
2483 if (!parser_next(parser)) {
2484 parseerror(parser, "expected 'for' initializer after opening paren");
2489 if (parser.tok == Token::IDENT)
2490 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2492 if (typevar || parser.tok == Token::TYPENAME) {
2493 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, nullptr))
2496 else if (parser.tok != ';')
2498 initexpr = parse_expression_leave(parser, false, false, false);
2501 /* move on to condition */
2502 if (parser.tok != ';') {
2503 parseerror(parser, "expected semicolon after for-loop initializer");
2506 if (!parser_next(parser)) {
2507 parseerror(parser, "expected for-loop condition");
2510 } else if (!parser_next(parser)) {
2511 parseerror(parser, "expected for-loop condition");
2515 /* parse the condition */
2516 if (parser.tok != ';') {
2517 cond = parse_expression_leave(parser, false, true, false);
2521 /* move on to incrementor */
2522 if (parser.tok != ';') {
2523 parseerror(parser, "expected semicolon after for-loop initializer");
2526 if (!parser_next(parser)) {
2527 parseerror(parser, "expected for-loop condition");
2531 /* parse the incrementor */
2532 if (parser.tok != ')') {
2533 lex_ctx_t condctx = parser_ctx(parser);
2534 increment = parse_expression_leave(parser, false, false, false);
2537 if (!increment->m_side_effects) {
2538 if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2544 if (parser.tok != ')') {
2545 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2548 /* parse into the 'then' branch */
2549 if (!parser_next(parser)) {
2550 parseerror(parser, "expected for-loop body");
2553 if (!parse_statement_or_block(parser, &ontrue))
2557 cond = process_condition(parser, cond, &ifnot);
2561 aloop = new ast_loop(ctx, initexpr, cond, ifnot, nullptr, false, increment, ontrue);
2564 if (!parser_leaveblock(parser)) {
2570 if (initexpr) ast_unref(initexpr);
2571 if (cond) ast_unref(cond);
2572 if (increment) ast_unref(increment);
2573 (void)!parser_leaveblock(parser);
2577 static bool parse_return(parser_t &parser, ast_block *block, ast_expression **out)
2579 ast_expression *exp = nullptr;
2580 ast_expression *var = nullptr;
2581 ast_return *ret = nullptr;
2582 ast_value *retval = parser.function->m_return_value;
2583 ast_value *expected = parser.function->m_function_type;
2585 lex_ctx_t ctx = parser_ctx(parser);
2587 (void)block; /* not touching */
2589 if (!parser_next(parser)) {
2590 parseerror(parser, "expected return expression");
2594 /* return assignments */
2595 if (parser.tok == '=') {
2596 if (!OPTS_FLAG(RETURN_ASSIGNMENTS)) {
2597 parseerror(parser, "return assignments not activated, try using -freturn-assigments");
2601 if (type_store_instr[expected->m_next->m_vtype] == VINSTR_END) {
2603 ast_type_to_string(expected->m_next, ty1, sizeof(ty1));
2604 parseerror(parser, "invalid return type: `%s'", ty1);
2608 if (!parser_next(parser)) {
2609 parseerror(parser, "expected return assignment expression");
2613 if (!(exp = parse_expression_leave(parser, false, false, false)))
2616 /* prepare the return value */
2618 retval = new ast_value(ctx, "#LOCAL_RETURN", TYPE_VOID);
2619 retval->adoptType(*expected->m_next);
2620 parser.function->m_return_value = retval;
2621 parser.function->m_return_value->m_flags |= AST_FLAG_NOREF;
2624 if (!exp->compareType(*retval)) {
2625 char ty1[1024], ty2[1024];
2626 ast_type_to_string(exp, ty1, sizeof(ty1));
2627 ast_type_to_string(retval, ty2, sizeof(ty2));
2628 parseerror(parser, "invalid type for return value: `%s', expected `%s'", ty1, ty2);
2631 /* store to 'return' local variable */
2632 var = new ast_store(
2634 type_store_instr[expected->m_next->m_vtype],
2642 if (parser.tok != ';')
2643 parseerror(parser, "missing semicolon after return assignment");
2644 else if (!parser_next(parser))
2645 parseerror(parser, "parse error after return assignment");
2651 if (parser.tok != ';') {
2652 exp = parse_expression(parser, false, false);
2656 if (exp->m_vtype != TYPE_NIL &&
2657 exp->m_vtype != (expected)->m_next->m_vtype)
2659 parseerror(parser, "return with invalid expression");
2662 ret = new ast_return(ctx, exp);
2668 if (!parser_next(parser))
2669 parseerror(parser, "parse error");
2671 if (!retval && expected->m_next->m_vtype != TYPE_VOID)
2673 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2675 ret = new ast_return(ctx, retval);
2681 static bool parse_break_continue(parser_t &parser, ast_block *block, ast_expression **out, bool is_continue)
2684 unsigned int levels = 0;
2685 lex_ctx_t ctx = parser_ctx(parser);
2686 auto &loops = (is_continue ? parser.continues : parser.breaks);
2688 (void)block; /* not touching */
2689 if (!parser_next(parser)) {
2690 parseerror(parser, "expected semicolon or loop label");
2694 if (loops.empty()) {
2696 parseerror(parser, "`continue` can only be used inside loops");
2698 parseerror(parser, "`break` can only be used inside loops or switches");
2701 if (parser.tok == Token::IDENT) {
2702 if (!OPTS_FLAG(LOOP_LABELS))
2703 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2706 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
2709 parseerror(parser, "no such loop to %s: `%s`",
2710 (is_continue ? "continue" : "break out of"),
2711 parser_tokval(parser));
2716 if (!parser_next(parser)) {
2717 parseerror(parser, "expected semicolon");
2722 if (parser.tok != ';') {
2723 parseerror(parser, "expected semicolon");
2727 if (!parser_next(parser))
2728 parseerror(parser, "parse error");
2730 *out = new ast_breakcont(ctx, is_continue, levels);
2734 /* returns true when it was a variable qualifier, false otherwise!
2735 * on error, cvq is set to CV_WRONG
2737 struct attribute_t {
2742 static bool parse_qualifiers(parser_t &parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
2744 bool had_const = false;
2745 bool had_var = false;
2746 bool had_noref = false;
2747 bool had_attrib = false;
2748 bool had_static = false;
2751 static attribute_t attributes[] = {
2752 { "noreturn", AST_FLAG_NORETURN },
2753 { "inline", AST_FLAG_INLINE },
2754 { "eraseable", AST_FLAG_ERASEABLE },
2755 { "accumulate", AST_FLAG_ACCUMULATE },
2756 { "last", AST_FLAG_FINAL_DECL }
2763 if (parser.tok == Token::ATTRIBUTE_OPEN) {
2765 /* parse an attribute */
2766 if (!parser_next(parser)) {
2767 parseerror(parser, "expected attribute after `[[`");
2772 for (i = 0; i < GMQCC_ARRAY_COUNT(attributes); i++) {
2773 if (!strcmp(parser_tokval(parser), attributes[i].name)) {
2774 flags |= attributes[i].flag;
2775 if (!parser_next(parser) || parser.tok != Token::ATTRIBUTE_CLOSE) {
2776 parseerror(parser, "`%s` attribute has no parameters, expected `]]`",
2777 attributes[i].name);
2785 if (i != GMQCC_ARRAY_COUNT(attributes))
2789 if (!strcmp(parser_tokval(parser), "noref")) {
2791 if (!parser_next(parser) || parser.tok != Token::ATTRIBUTE_CLOSE) {
2792 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2797 else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
2798 flags |= AST_FLAG_ALIAS;
2801 if (!parser_next(parser)) {
2802 parseerror(parser, "parse error in attribute");
2806 if (parser.tok == '(') {
2807 if (!parser_next(parser) || parser.tok != Token::STRINGCONST) {
2808 parseerror(parser, "`alias` attribute missing parameter");
2812 *message = util_strdup(parser_tokval(parser));
2814 if (!parser_next(parser)) {
2815 parseerror(parser, "parse error in attribute");
2819 if (parser.tok != ')') {
2820 parseerror(parser, "`alias` attribute expected `)` after parameter");
2824 if (!parser_next(parser)) {
2825 parseerror(parser, "parse error in attribute");
2830 if (parser.tok != Token::ATTRIBUTE_CLOSE) {
2831 parseerror(parser, "`alias` attribute expected `]]`");
2835 else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
2836 flags |= AST_FLAG_DEPRECATED;
2839 if (!parser_next(parser)) {
2840 parseerror(parser, "parse error in attribute");
2844 if (parser.tok == '(') {
2845 if (!parser_next(parser) || parser.tok != Token::STRINGCONST) {
2846 parseerror(parser, "`deprecated` attribute missing parameter");
2850 *message = util_strdup(parser_tokval(parser));
2852 if (!parser_next(parser)) {
2853 parseerror(parser, "parse error in attribute");
2857 if(parser.tok != ')') {
2858 parseerror(parser, "`deprecated` attribute expected `)` after parameter");
2862 if (!parser_next(parser)) {
2863 parseerror(parser, "parse error in attribute");
2868 if (parser.tok != Token::ATTRIBUTE_CLOSE) {
2869 parseerror(parser, "`deprecated` attribute expected `]]`");
2872 if (*message) mem_d(*message);
2878 else if (!strcmp(parser_tokval(parser), "coverage") && !(flags & AST_FLAG_COVERAGE)) {
2879 flags |= AST_FLAG_COVERAGE;
2880 if (!parser_next(parser)) {
2882 parseerror(parser, "parse error in coverage attribute");
2886 if (parser.tok == '(') {
2887 if (!parser_next(parser)) {
2889 parseerror(parser, "invalid parameter for coverage() attribute\n"
2890 "valid are: block");
2894 if (parser.tok != ')') {
2896 if (parser.tok != Token::IDENT)
2897 goto bad_coverage_arg;
2898 if (!strcmp(parser_tokval(parser), "block"))
2899 flags |= AST_FLAG_BLOCK_COVERAGE;
2900 else if (!strcmp(parser_tokval(parser), "none"))
2901 flags &= ~(AST_FLAG_COVERAGE_MASK);
2903 goto bad_coverage_arg;
2904 if (!parser_next(parser))
2905 goto error_in_coverage;
2906 if (parser.tok == ',') {
2907 if (!parser_next(parser))
2908 goto error_in_coverage;
2910 } while (parser.tok != ')');
2912 if (parser.tok != ')' || !parser_next(parser))
2913 goto error_in_coverage;
2915 /* without parameter [[coverage]] equals [[coverage(block)]] */
2916 flags |= AST_FLAG_BLOCK_COVERAGE;
2921 /* Skip tokens until we hit a ]] */
2922 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
2923 while (parser.tok != Token::ATTRIBUTE_CLOSE) {
2924 if (!parser_next(parser)) {
2925 parseerror(parser, "error inside attribute");
2932 else if (with_local && !strcmp(parser_tokval(parser), "static"))
2934 else if (!strcmp(parser_tokval(parser), "const"))
2936 else if (!strcmp(parser_tokval(parser), "var"))
2938 else if (with_local && !strcmp(parser_tokval(parser), "local"))
2940 else if (!strcmp(parser_tokval(parser), "noref"))
2942 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
2949 if (!parser_next(parser))
2959 *is_static = had_static;
2963 parseerror(parser, "parse error after variable qualifier");
2968 static bool parse_switch_go(parser_t &parser, ast_block *block, ast_expression **out);
2969 static bool parse_switch(parser_t &parser, ast_block *block, ast_expression **out)
2972 char *label = nullptr;
2974 /* skip the 'while' and get the body */
2975 if (!parser_next(parser)) {
2976 if (OPTS_FLAG(LOOP_LABELS))
2977 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
2979 parseerror(parser, "expected 'switch' operand in parenthesis");
2983 if (parser.tok == ':') {
2984 if (!OPTS_FLAG(LOOP_LABELS))
2985 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2986 if (!parser_next(parser) || parser.tok != Token::IDENT) {
2987 parseerror(parser, "expected loop label");
2990 label = util_strdup(parser_tokval(parser));
2991 if (!parser_next(parser)) {
2993 parseerror(parser, "expected 'switch' operand in parenthesis");
2998 if (parser.tok != '(') {
2999 parseerror(parser, "expected 'switch' operand in parenthesis");
3003 parser.breaks.push_back(label);
3005 rv = parse_switch_go(parser, block, out);
3008 if (parser.breaks.back() != label) {
3009 parseerror(parser, "internal error: label stack corrupted");
3015 parser.breaks.pop_back();
3020 static bool parse_switch_go(parser_t &parser, ast_block *block, ast_expression **out)
3022 ast_expression *operand;
3025 ast_switch *switchnode;
3026 ast_switch_case swcase;
3029 bool noref, is_static;
3030 uint32_t qflags = 0;
3032 lex_ctx_t ctx = parser_ctx(parser);
3034 (void)block; /* not touching */
3037 /* parse into the expression */
3038 if (!parser_next(parser)) {
3039 parseerror(parser, "expected switch operand");
3042 /* parse the operand */
3043 operand = parse_expression_leave(parser, false, false, false);
3047 switchnode = new ast_switch(ctx, operand);
3050 if (parser.tok != ')') {
3052 parseerror(parser, "expected closing paren after 'switch' operand");
3056 /* parse over the opening paren */
3057 if (!parser_next(parser) || parser.tok != '{') {
3059 parseerror(parser, "expected list of cases");
3063 if (!parser_next(parser)) {
3065 parseerror(parser, "expected 'case' or 'default'");
3069 /* new block; allow some variables to be declared here */
3070 parser_enterblock(parser);
3073 if (parser.tok == Token::IDENT)
3074 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3075 if (typevar || parser.tok == Token::TYPENAME) {
3076 if (!parse_variable(parser, block, true, CV_NONE, typevar, false, false, 0, nullptr)) {
3082 if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, nullptr))
3084 if (cvq == CV_WRONG) {
3088 if (!parse_variable(parser, block, true, cvq, nullptr, noref, is_static, qflags, nullptr)) {
3098 while (parser.tok != '}') {
3099 ast_block *caseblock;
3101 if (!strcmp(parser_tokval(parser), "case")) {
3102 if (!parser_next(parser)) {
3104 parseerror(parser, "expected expression for case");
3107 swcase.m_value = parse_expression_leave(parser, false, false, false);
3109 if (!operand->compareType(*swcase.m_value)) {
3113 ast_type_to_string(swcase.m_value, ty1, sizeof ty1);
3114 ast_type_to_string(operand, ty2, sizeof ty2);
3116 auto fnLiteral = [](ast_expression *expression) -> char* {
3117 if (!ast_istype(expression, ast_value))
3119 ast_value *value = (ast_value *)expression;
3120 if (!value->m_hasvalue)
3122 char *string = nullptr;
3123 basic_value_t *constval = &value->m_constval;
3124 switch (value->m_vtype)
3127 util_asprintf(&string, "%.2f", constval->vfloat);
3130 util_asprintf(&string, "'%.2f %.2f %.2f'",
3136 util_asprintf(&string, "\"%s\"", constval->vstring);
3144 char *literal = fnLiteral(swcase.m_value);
3146 compile_error(parser_ctx(parser), "incompatible type `%s` for switch case `%s` expected `%s`", ty1, literal, ty2);
3148 compile_error(parser_ctx(parser), "incompatible type `%s` for switch case expected `%s`", ty1, ty2);
3154 if (!swcase.m_value) {
3156 parseerror(parser, "expected expression for case");
3159 if (!OPTS_FLAG(RELAXED_SWITCH)) {
3160 if (!ast_istype(swcase.m_value, ast_value)) { /* || ((ast_value*)swcase.m_value)->m_cvq != CV_CONST) { */
3161 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
3167 else if (!strcmp(parser_tokval(parser), "default")) {
3168 swcase.m_value = nullptr;
3169 if (!parser_next(parser)) {
3171 parseerror(parser, "expected colon");
3177 parseerror(parser, "expected 'case' or 'default'");
3181 /* Now the colon and body */
3182 if (parser.tok != ':') {
3183 if (swcase.m_value) ast_unref(swcase.m_value);
3185 parseerror(parser, "expected colon");
3189 if (!parser_next(parser)) {
3190 if (swcase.m_value) ast_unref(swcase.m_value);
3192 parseerror(parser, "expected statements or case");
3195 caseblock = new ast_block(parser_ctx(parser));
3197 if (swcase.m_value) ast_unref(swcase.m_value);
3201 swcase.m_code = caseblock;
3202 switchnode->m_cases.push_back(swcase);
3204 ast_expression *expr;
3205 if (parser.tok == '}')
3207 if (parser.tok == Token::KEYWORD) {
3208 if (!strcmp(parser_tokval(parser), "case") ||
3209 !strcmp(parser_tokval(parser), "default"))
3214 if (!parse_statement(parser, caseblock, &expr, true)) {
3220 if (!caseblock->addExpr(expr)) {
3227 parser_leaveblock(parser);
3230 if (parser.tok != '}') {
3232 parseerror(parser, "expected closing paren of case list");
3235 if (!parser_next(parser)) {
3237 parseerror(parser, "parse error after switch");
3244 /* parse computed goto sides */
3245 static ast_expression *parse_goto_computed(parser_t &parser, ast_expression **side) {
3246 ast_expression *on_true;
3247 ast_expression *on_false;
3248 ast_expression *cond;
3253 if (ast_istype(*side, ast_ternary)) {
3254 ast_ternary *tern = (ast_ternary*)*side;
3255 on_true = parse_goto_computed(parser, &tern->m_on_true);
3256 on_false = parse_goto_computed(parser, &tern->m_on_false);
3258 if (!on_true || !on_false) {
3259 parseerror(parser, "expected label or expression in ternary");
3260 if (on_true) ast_unref(on_true);
3261 if (on_false) ast_unref(on_false);
3265 cond = tern->m_cond;
3266 tern->m_cond = nullptr;
3269 return new ast_ifthen(parser_ctx(parser), cond, on_true, on_false);
3270 } else if (ast_istype(*side, ast_label)) {
3271 ast_goto *gt = new ast_goto(parser_ctx(parser), ((ast_label*)*side)->m_name);
3272 gt->setLabel(reinterpret_cast<ast_label*>(*side));
3279 static bool parse_goto(parser_t &parser, ast_expression **out)
3281 ast_goto *gt = nullptr;
3282 ast_expression *lbl;
3284 if (!parser_next(parser))
3287 if (parser.tok != Token::IDENT) {
3288 ast_expression *expression;
3290 /* could be an expression i.e computed goto :-) */
3291 if (parser.tok != '(') {
3292 parseerror(parser, "expected label name after `goto`");
3296 /* failed to parse expression for goto */
3297 if (!(expression = parse_expression(parser, false, true)) ||
3298 !(*out = parse_goto_computed(parser, &expression))) {
3299 parseerror(parser, "invalid goto expression");
3301 ast_unref(expression);
3308 /* not computed goto */
3309 gt = new ast_goto(parser_ctx(parser), parser_tokval(parser));
3310 lbl = parser_find_label(parser, gt->m_name);
3312 if (!ast_istype(lbl, ast_label)) {
3313 parseerror(parser, "internal error: label is not an ast_label");
3317 gt->setLabel(reinterpret_cast<ast_label*>(lbl));
3320 parser.gotos.push_back(gt);
3322 if (!parser_next(parser) || parser.tok != ';') {
3323 parseerror(parser, "semicolon expected after goto label");
3326 if (!parser_next(parser)) {
3327 parseerror(parser, "parse error after goto");
3335 static bool parse_skipwhite(parser_t &parser)
3338 if (!parser_next(parser))
3340 } while (parser.tok == Token::WHITE && parser.tok < Token::ERROR);
3341 return parser.tok < Token::ERROR;
3344 static bool parse_eol(parser_t &parser)
3346 if (!parse_skipwhite(parser))
3348 return parser.tok == Token::EOL;
3351 static bool parse_pragma_do(parser_t &parser)
3353 if (!parser_next(parser) ||
3354 parser.tok != Token::IDENT ||
3355 strcmp(parser_tokval(parser), "pragma"))
3357 parseerror(parser, "expected `pragma` keyword after `#`, got `%s`", parser_tokval(parser));
3360 if (!parse_skipwhite(parser) || parser.tok != Token::IDENT) {
3361 parseerror(parser, "expected pragma, got `%s`", parser_tokval(parser));
3365 if (!strcmp(parser_tokval(parser), "noref")) {
3366 if (!parse_skipwhite(parser) || parser.tok != Token::INTCONST) {
3367 parseerror(parser, "`noref` pragma requires an argument: 0 or 1");
3370 parser.noref = !!parser_token(parser)->constval.i;
3371 if (!parse_eol(parser)) {
3372 parseerror(parser, "parse error after `noref` pragma");
3378 (void)!parsewarning(parser, WARN_UNKNOWN_PRAGMAS, "ignoring #pragma %s", parser_tokval(parser));
3381 while (!parse_eol(parser)) {
3382 parser_next(parser);
3391 static bool parse_pragma(parser_t &parser)
3393 parser.lex->flags.preprocessing = true;
3394 parser.lex->flags.mergelines = true;
3395 auto ret = parse_pragma_do(parser);
3396 if (parser.tok != Token::EOL) {
3397 parseerror(parser, "junk after pragma");
3400 parser.lex->flags.preprocessing = false;
3401 parser.lex->flags.mergelines = false;
3402 if (!parser_next(parser)) {
3403 parseerror(parser, "parse error after pragma");
3409 static bool parse_statement(parser_t &parser, ast_block *block, ast_expression **out, bool allow_cases)
3411 bool noref, is_static;
3413 uint32_t qflags = 0;
3414 ast_value *typevar = nullptr;
3415 char *vstring = nullptr;
3419 if (parser.tok == Token::IDENT)
3420 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3422 if (typevar || parser.tok == Token::TYPENAME || parser.tok == '.' || parser.tok == Token::DOTS)
3424 /* local variable */
3426 parseerror(parser, "cannot declare a variable from here");
3429 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3430 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
3433 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, nullptr))
3437 else if (parse_qualifiers(parser, !!block, &cvq, &noref, &is_static, &qflags, &vstring))
3439 if (cvq == CV_WRONG)
3441 return parse_variable(parser, block, false, cvq, nullptr, noref, is_static, qflags, vstring);
3443 else if (parser.tok == Token::KEYWORD)
3445 if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
3450 if (!parser_next(parser)) {
3451 parseerror(parser, "parse error after __builtin_debug_printtype");
3455 if (parser.tok == Token::IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
3457 ast_type_to_string(tdef, ty, sizeof(ty));
3458 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->m_name.c_str(), ty);
3459 if (!parser_next(parser)) {
3460 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
3466 if (!parse_statement(parser, block, out, allow_cases))
3469 con_out("__builtin_debug_printtype: got no output node\n");
3472 ast_type_to_string(*out, ty, sizeof(ty));
3473 con_out("__builtin_debug_printtype: `%s`\n", ty);
3478 else if (!strcmp(parser_tokval(parser), "return"))
3480 return parse_return(parser, block, out);
3482 else if (!strcmp(parser_tokval(parser), "if"))
3484 return parse_if(parser, block, out);
3486 else if (!strcmp(parser_tokval(parser), "while"))
3488 return parse_while(parser, block, out);
3490 else if (!strcmp(parser_tokval(parser), "do"))
3492 return parse_dowhile(parser, block, out);
3494 else if (!strcmp(parser_tokval(parser), "for"))
3496 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3497 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
3500 return parse_for(parser, block, out);
3502 else if (!strcmp(parser_tokval(parser), "break"))
3504 return parse_break_continue(parser, block, out, false);
3506 else if (!strcmp(parser_tokval(parser), "continue"))
3508 return parse_break_continue(parser, block, out, true);
3510 else if (!strcmp(parser_tokval(parser), "switch"))
3512 return parse_switch(parser, block, out);
3514 else if (!strcmp(parser_tokval(parser), "case") ||
3515 !strcmp(parser_tokval(parser), "default"))
3518 parseerror(parser, "unexpected 'case' label");
3523 else if (!strcmp(parser_tokval(parser), "goto"))
3525 return parse_goto(parser, out);
3527 else if (!strcmp(parser_tokval(parser), "typedef"))
3529 if (!parser_next(parser)) {
3530 parseerror(parser, "expected type definition after 'typedef'");
3533 return parse_typedef(parser);
3535 parseerror(parser, "Unexpected keyword: `%s'", parser_tokval(parser));
3538 else if (parser.tok == '{')
3541 inner = parse_block(parser);
3547 else if (parser.tok == ':')
3551 if (!parser_next(parser)) {
3552 parseerror(parser, "expected label name");
3555 if (parser.tok != Token::IDENT) {
3556 parseerror(parser, "label must be an identifier");
3559 label = (ast_label*)parser_find_label(parser, parser_tokval(parser));
3561 if (!label->m_undefined) {
3562 parseerror(parser, "label `%s` already defined", label->m_name);
3565 label->m_undefined = false;
3568 label = new ast_label(parser_ctx(parser), parser_tokval(parser), false);
3569 parser.labels.push_back(label);
3572 if (!parser_next(parser)) {
3573 parseerror(parser, "parse error after label");
3576 for (i = 0; i < parser.gotos.size(); ++i) {
3577 if (parser.gotos[i]->m_name == label->m_name) {
3578 parser.gotos[i]->setLabel(label);
3579 parser.gotos.erase(parser.gotos.begin() + i);
3585 else if (parser.tok == ';')
3587 if (!parser_next(parser)) {
3588 parseerror(parser, "parse error after empty statement");
3595 lex_ctx_t ctx = parser_ctx(parser);
3596 ast_expression *exp = parse_expression(parser, false, false);
3600 if (!exp->m_side_effects) {
3601 if (compile_warning(ctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
3608 static bool parse_enum(parser_t &parser)
3611 bool reverse = false;
3613 ast_value *var = nullptr;
3615 std::vector<ast_value*> values;
3617 ast_expression *old;
3619 if (!parser_next(parser) || (parser.tok != '{' && parser.tok != ':')) {
3620 parseerror(parser, "expected `{` or `:` after `enum` keyword");
3624 /* enumeration attributes (can add more later) */
3625 if (parser.tok == ':') {
3626 if (!parser_next(parser) || parser.tok != Token::IDENT){
3627 parseerror(parser, "expected `flag` or `reverse` for enumeration attribute");
3632 if (!strcmp(parser_tokval(parser), "flag")) {
3636 else if (!strcmp(parser_tokval(parser), "reverse")) {
3640 parseerror(parser, "invalid attribute `%s` for enumeration", parser_tokval(parser));
3644 if (!parser_next(parser) || parser.tok != '{') {
3645 parseerror(parser, "expected `{` after enum attribute ");
3651 if (!parser_next(parser) || parser.tok != Token::IDENT) {
3652 if (parser.tok == '}') {
3653 /* allow an empty enum */
3656 parseerror(parser, "expected identifier or `}`");
3660 old = parser_find_field(parser, parser_tokval(parser));
3662 old = parser_find_global(parser, parser_tokval(parser));
3664 parseerror(parser, "value `%s` has already been declared here: %s:%i",
3665 parser_tokval(parser), old->m_context.file, old->m_context.line);
3669 var = new ast_value(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
3670 values.push_back(var);
3671 var->m_cvq = CV_CONST;
3672 var->m_hasvalue = true;
3674 /* for flagged enumerations increment in POTs of TWO */
3675 var->m_constval.vfloat = (flag) ? (num *= 2) : (num ++);
3676 parser_addglobal(parser, var->m_name, var);
3678 if (!parser_next(parser)) {
3679 parseerror(parser, "expected `=`, `}` or comma after identifier");
3683 if (parser.tok == ',')
3685 if (parser.tok == '}')
3687 if (parser.tok != '=') {
3688 parseerror(parser, "expected `=`, `}` or comma after identifier");
3692 if (!parser_next(parser)) {
3693 parseerror(parser, "expected expression after `=`");
3697 /* We got a value! */
3698 old = parse_expression_leave(parser, true, false, false);
3699 asvalue = (ast_value*)old;
3700 if (!ast_istype(old, ast_value) || asvalue->m_cvq != CV_CONST || !asvalue->m_hasvalue) {
3701 compile_error(var->m_context, "constant value or expression expected");
3704 num = (var->m_constval.vfloat = asvalue->m_constval.vfloat) + 1;
3706 if (parser.tok == '}')
3708 if (parser.tok != ',') {
3709 parseerror(parser, "expected `}` or comma after expression");
3714 /* patch them all (for reversed attribute) */
3717 for (i = 0; i < values.size(); i++)
3718 values[i]->m_constval.vfloat = values.size() - i - 1;
3721 if (parser.tok != '}') {
3722 parseerror(parser, "internal error: breaking without `}`");
3726 if (!parser_next(parser) || parser.tok != ';') {
3727 parseerror(parser, "expected semicolon after enumeration");
3731 if (!parser_next(parser)) {
3732 parseerror(parser, "parse error after enumeration");
3739 static bool parse_block_into(parser_t &parser, ast_block *block)
3743 parser_enterblock(parser);
3745 if (!parser_next(parser)) { /* skip the '{' */
3746 parseerror(parser, "expected function body");
3750 while (parser.tok != Token::END && parser.tok < Token::ERROR)
3752 ast_expression *expr = nullptr;
3753 if (parser.tok == '}')
3756 if (!parse_statement(parser, block, &expr, false)) {
3757 /* parseerror(parser, "parse error"); */
3763 if (!block->addExpr(expr)) {
3770 if (parser.tok != '}') {
3773 (void)parser_next(parser);
3777 if (!parser_leaveblock(parser))
3779 return retval && !!block;
3782 static ast_block* parse_block(parser_t &parser)
3785 block = new ast_block(parser_ctx(parser));
3788 if (!parse_block_into(parser, block)) {
3795 static bool parse_statement_or_block(parser_t &parser, ast_expression **out)
3797 if (parser.tok == '{') {
3798 *out = parse_block(parser);
3801 return parse_statement(parser, nullptr, out, false);
3804 static bool create_vector_members(ast_value *var, ast_member **me)
3807 size_t len = var->m_name.length();
3809 for (i = 0; i < 3; ++i) {
3810 char *name = (char*)mem_a(len+3);
3811 memcpy(name, var->m_name.c_str(), len);
3813 name[len+1] = 'x'+i;
3815 me[i] = ast_member::make(var->m_context, var, i, name);
3824 do { delete me[--i]; } while(i);
3828 static bool parse_function_body(parser_t &parser, ast_value *var)
3830 ast_block *block = nullptr;
3834 ast_expression *framenum = nullptr;
3835 ast_expression *nextthink = nullptr;
3836 /* None of the following have to be deleted */
3837 ast_expression *fld_think = nullptr, *fld_nextthink = nullptr, *fld_frame = nullptr;
3838 ast_expression *gbl_time = nullptr, *gbl_self = nullptr;
3839 bool has_frame_think;
3843 has_frame_think = false;
3844 old = parser.function;
3846 if (var->m_flags & AST_FLAG_ALIAS) {
3847 parseerror(parser, "function aliases cannot have bodies");
3851 if (parser.gotos.size() || parser.labels.size()) {
3852 parseerror(parser, "gotos/labels leaking");
3856 if (!OPTS_FLAG(VARIADIC_ARGS) && var->m_flags & AST_FLAG_VARIADIC) {
3857 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
3858 "variadic function with implementation will not be able to access additional parameters (try -fvariadic-args)"))
3864 if (parser.tok == '[') {
3865 /* got a frame definition: [ framenum, nextthink ]
3866 * this translates to:
3867 * self.frame = framenum;
3868 * self.nextthink = time + 0.1;
3869 * self.think = nextthink;
3871 nextthink = nullptr;
3873 fld_think = parser_find_field(parser, "think");
3874 fld_nextthink = parser_find_field(parser, "nextthink");
3875 fld_frame = parser_find_field(parser, "frame");
3876 if (!fld_think || !fld_nextthink || !fld_frame) {
3877 parseerror(parser, "cannot use [frame,think] notation without the required fields");
3878 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
3881 gbl_time = parser_find_global(parser, "time");
3882 gbl_self = parser_find_global(parser, "self");
3883 if (!gbl_time || !gbl_self) {
3884 parseerror(parser, "cannot use [frame,think] notation without the required globals");
3885 parseerror(parser, "please declare the following globals: `time`, `self`");
3889 if (!parser_next(parser))
3892 framenum = parse_expression_leave(parser, true, false, false);
3894 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
3897 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->m_hasvalue) {
3898 ast_unref(framenum);
3899 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
3903 if (parser.tok != ',') {
3904 ast_unref(framenum);
3905 parseerror(parser, "expected comma after frame number in [frame,think] notation");
3906 parseerror(parser, "Got a %i\n", parser.tok);
3910 if (!parser_next(parser)) {
3911 ast_unref(framenum);
3915 if (parser.tok == Token::IDENT && !parser_find_var(parser, parser_tokval(parser)))
3917 /* qc allows the use of not-yet-declared functions here
3918 * - this automatically creates a prototype */
3919 ast_value *thinkfunc;
3920 ast_expression *functype = fld_think->m_next;
3922 thinkfunc = new ast_value(parser_ctx(parser), parser_tokval(parser), functype->m_vtype);
3923 if (!thinkfunc) { /* || !thinkfunc->adoptType(*functype)*/
3924 ast_unref(framenum);
3925 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
3928 thinkfunc->adoptType(*functype);
3930 if (!parser_next(parser)) {
3931 ast_unref(framenum);
3936 parser_addglobal(parser, thinkfunc->m_name, thinkfunc);
3938 nextthink = thinkfunc;
3941 nextthink = parse_expression_leave(parser, true, false, false);
3943 ast_unref(framenum);
3944 parseerror(parser, "expected a think-function in [frame,think] notation");
3949 if (!ast_istype(nextthink, ast_value)) {
3950 parseerror(parser, "think-function in [frame,think] notation must be a constant");
3954 if (retval && parser.tok != ']') {
3955 parseerror(parser, "expected closing `]` for [frame,think] notation");
3959 if (retval && !parser_next(parser)) {
3963 if (retval && parser.tok != '{') {
3964 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
3969 ast_unref(nextthink);
3970 ast_unref(framenum);
3974 has_frame_think = true;
3977 block = new ast_block(parser_ctx(parser));
3979 parseerror(parser, "failed to allocate block");
3980 if (has_frame_think) {
3981 ast_unref(nextthink);
3982 ast_unref(framenum);
3987 if (has_frame_think) {
3988 if (!OPTS_FLAG(EMULATE_STATE)) {
3989 ast_state *state_op = new ast_state(parser_ctx(parser), framenum, nextthink);
3990 if (!block->addExpr(state_op)) {
3991 parseerror(parser, "failed to generate state op for [frame,think]");
3992 ast_unref(nextthink);
3993 ast_unref(framenum);
3998 /* emulate OP_STATE in code: */
4000 ast_expression *self_frame;
4001 ast_expression *self_nextthink;
4002 ast_expression *self_think;
4003 ast_expression *time_plus_1;
4004 ast_store *store_frame;
4005 ast_store *store_nextthink;
4006 ast_store *store_think;
4008 float frame_delta = 1.0f / (float)OPTS_OPTION_U32(OPTION_STATE_FPS);
4010 ctx = parser_ctx(parser);
4011 self_frame = new ast_entfield(ctx, gbl_self, fld_frame);
4012 self_nextthink = new ast_entfield(ctx, gbl_self, fld_nextthink);
4013 self_think = new ast_entfield(ctx, gbl_self, fld_think);
4015 time_plus_1 = new ast_binary(ctx, INSTR_ADD_F,
4016 gbl_time, parser.m_fold.constgen_float(frame_delta, false));
4018 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
4019 if (self_frame) delete self_frame;
4020 if (self_nextthink) delete self_nextthink;
4021 if (self_think) delete self_think;
4022 if (time_plus_1) delete time_plus_1;
4028 store_frame = new ast_store(ctx, INSTR_STOREP_F, self_frame, framenum);
4029 store_nextthink = new ast_store(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
4030 store_think = new ast_store(ctx, INSTR_STOREP_FNC, self_think, nextthink);
4036 if (!store_nextthink) {
4037 delete self_nextthink;
4045 if (store_frame) delete store_frame;
4046 if (store_nextthink) delete store_nextthink;
4047 if (store_think) delete store_think;
4050 if (!block->addExpr(store_frame) ||
4051 !block->addExpr(store_nextthink) ||
4052 !block->addExpr(store_think))
4059 parseerror(parser, "failed to generate code for [frame,think]");
4060 ast_unref(nextthink);
4061 ast_unref(framenum);
4068 if (var->m_hasvalue) {
4069 if (!(var->m_flags & AST_FLAG_ACCUMULATE)) {
4070 parseerror(parser, "function `%s` declared with multiple bodies", var->m_name);
4074 func = var->m_constval.vfunc;
4077 parseerror(parser, "internal error: nullptr function: `%s`", var->m_name);
4082 func = ast_function::make(var->m_context, var->m_name, var);
4085 parseerror(parser, "failed to allocate function for `%s`", var->m_name);
4089 parser.functions.push_back(func);
4092 parser_enterblock(parser);
4094 for (auto &it : var->m_type_params) {
4098 if (it->m_vtype != TYPE_VECTOR &&
4099 (it->m_vtype != TYPE_FIELD ||
4100 it->m_next->m_vtype != TYPE_VECTOR))
4105 if (!create_vector_members(it.get(), me)) {
4110 for (e = 0; e < 3; ++e) {
4111 parser_addlocal(parser, me[e]->m_name, me[e]);
4112 block->collect(me[e]);
4116 if (var->m_argcounter && !func->m_argc) {
4117 ast_value *argc = new ast_value(var->m_context, var->m_argcounter, TYPE_FLOAT);
4118 parser_addlocal(parser, argc->m_name, argc);
4119 func->m_argc.reset(argc);
4122 if (OPTS_FLAG(VARIADIC_ARGS) && var->m_flags & AST_FLAG_VARIADIC && !func->m_varargs) {
4124 ast_value *varargs = new ast_value(var->m_context, "reserved:va_args", TYPE_ARRAY);
4125 varargs->m_flags |= AST_FLAG_IS_VARARG;
4126 varargs->m_next = new ast_value(var->m_context, "", TYPE_VECTOR);
4127 varargs->m_count = 0;
4128 util_snprintf(name, sizeof(name), "%s##va##SET", var->m_name.c_str());
4129 if (!parser_create_array_setter_proto(parser, varargs, name)) {
4134 util_snprintf(name, sizeof(name), "%s##va##GET", var->m_name.c_str());
4135 if (!parser_create_array_getter_proto(parser, varargs, varargs->m_next, name)) {
4140 func->m_varargs.reset(varargs);
4141 func->m_fixedparams = (ast_value*)parser.m_fold.constgen_float(var->m_type_params.size(), false);
4144 parser.function = func;
4145 if (!parse_block_into(parser, block)) {
4150 func->m_blocks.emplace_back(block);
4152 parser.function = old;
4153 if (!parser_leaveblock(parser))
4155 if (parser.variables.size() != PARSER_HT_LOCALS) {
4156 parseerror(parser, "internal error: local scopes left");
4160 if (parser.tok == ';')
4161 return parser_next(parser);
4162 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4163 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
4167 (void)!parser_leaveblock(parser);
4168 parser.functions.pop_back();
4170 var->m_constval.vfunc = nullptr;
4173 parser.function = old;
4177 static ast_expression *array_accessor_split(
4182 ast_expression *left,
4183 ast_expression *right
4189 lex_ctx_t ctx = array->m_context;
4191 if (!left || !right) {
4192 if (left) delete left;
4193 if (right) delete right;
4197 cmp = new ast_binary(ctx, INSTR_LT,
4199 parser.m_fold.constgen_float(middle, false));
4203 parseerror(parser, "internal error: failed to create comparison for array setter");
4207 ifthen = new ast_ifthen(ctx, cmp, left, right);
4209 delete cmp; /* will delete left and right */
4210 parseerror(parser, "internal error: failed to create conditional jump for array setter");
4217 static ast_expression *array_setter_node(parser_t &parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
4219 lex_ctx_t ctx = array->m_context;
4221 if (from+1 == afterend) {
4222 /* set this value */
4225 ast_array_index *subscript;
4227 int assignop = type_store_instr[value->m_vtype];
4229 if (value->m_vtype == TYPE_FIELD && value->m_next->m_vtype == TYPE_VECTOR)
4230 assignop = INSTR_STORE_V;
4232 subscript = ast_array_index::make(ctx, array, parser.m_fold.constgen_float(from, false));
4236 st = new ast_store(ctx, assignop, subscript, value);
4242 block = new ast_block(ctx);
4248 if (!block->addExpr(st)) {
4253 ret = new ast_return(ctx, nullptr);
4259 if (!block->addExpr(ret)) {
4266 ast_expression *left, *right;
4267 size_t diff = afterend - from;
4268 size_t middle = from + diff/2;
4269 left = array_setter_node(parser, array, index, value, from, middle);
4270 right = array_setter_node(parser, array, index, value, middle, afterend);
4271 return array_accessor_split(parser, array, index, middle, left, right);
4275 static ast_expression *array_field_setter_node(
4284 lex_ctx_t ctx = array->m_context;
4286 if (from+1 == afterend) {
4287 /* set this value */
4290 ast_entfield *entfield;
4291 ast_array_index *subscript;
4293 int assignop = type_storep_instr[value->m_vtype];
4295 if (value->m_vtype == TYPE_FIELD && value->m_next->m_vtype == TYPE_VECTOR)
4296 assignop = INSTR_STOREP_V;
4298 subscript = ast_array_index::make(ctx, array, parser.m_fold.constgen_float(from, false));
4302 subscript->m_next = new ast_expression(ast_copy_type, subscript->m_context, *subscript);
4303 subscript->m_vtype = TYPE_FIELD;
4305 entfield = new ast_entfield(ctx, entity, subscript, subscript);
4311 st = new ast_store(ctx, assignop, entfield, value);
4317 block = new ast_block(ctx);
4323 if (!block->addExpr(st)) {
4328 ret = new ast_return(ctx, nullptr);
4334 if (!block->addExpr(ret)) {
4341 ast_expression *left, *right;
4342 size_t diff = afterend - from;
4343 size_t middle = from + diff/2;
4344 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
4345 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
4346 return array_accessor_split(parser, array, index, middle, left, right);
4350 static ast_expression *array_getter_node(parser_t &parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
4352 lex_ctx_t ctx = array->m_context;
4354 if (from+1 == afterend) {
4356 ast_array_index *subscript;
4358 subscript = ast_array_index::make(ctx, array, parser.m_fold.constgen_float(from, false));
4362 ret = new ast_return(ctx, subscript);
4370 ast_expression *left, *right;
4371 size_t diff = afterend - from;
4372 size_t middle = from + diff/2;
4373 left = array_getter_node(parser, array, index, from, middle);
4374 right = array_getter_node(parser, array, index, middle, afterend);
4375 return array_accessor_split(parser, array, index, middle, left, right);
4379 static bool parser_create_array_accessor(parser_t &parser, ast_value *array, const char *funcname, ast_value **out)
4381 ast_function *func = nullptr;
4382 ast_value *fval = nullptr;
4383 ast_block *body = nullptr;
4385 fval = new ast_value(array->m_context, funcname, TYPE_FUNCTION);
4387 parseerror(parser, "failed to create accessor function value");
4390 fval->m_flags &= ~(AST_FLAG_COVERAGE_MASK);
4392 func = ast_function::make(array->m_context, funcname, fval);
4395 parseerror(parser, "failed to create accessor function node");
4399 body = new ast_block(array->m_context);
4401 parseerror(parser, "failed to create block for array accessor");
4407 func->m_blocks.emplace_back(body);
4410 parser.accessors.push_back(fval);
4415 static ast_value* parser_create_array_setter_proto(parser_t &parser, ast_value *array, const char *funcname)
4417 ast_value *index = nullptr;
4418 ast_value *value = nullptr;
4422 if (!ast_istype(array->m_next, ast_value)) {
4423 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4427 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4429 func = fval->m_constval.vfunc;
4430 fval->m_next = new ast_value(array->m_context, "<void>", TYPE_VOID);
4432 index = new ast_value(array->m_context, "index", TYPE_FLOAT);
4433 value = new ast_value(ast_copy_type, *(ast_value*)array->m_next);
4435 if (!index || !value) {
4436 parseerror(parser, "failed to create locals for array accessor");
4439 value->m_name = "value"; // not important
4440 fval->m_type_params.emplace_back(index);
4441 fval->m_type_params.emplace_back(value);
4443 array->m_setter = fval;
4446 if (index) delete index;
4447 if (value) delete value;
4453 static bool parser_create_array_setter_impl(parser_t &parser, ast_value *array)
4455 ast_expression *root = nullptr;
4456 root = array_setter_node(parser, array,
4457 array->m_setter->m_type_params[0].get(),
4458 array->m_setter->m_type_params[1].get(),
4461 parseerror(parser, "failed to build accessor search tree");
4464 if (!array->m_setter->m_constval.vfunc->m_blocks[0].get()->addExpr(root)) {
4471 static bool parser_create_array_setter(parser_t &parser, ast_value *array, const char *funcname)
4473 if (!parser_create_array_setter_proto(parser, array, funcname))
4475 return parser_create_array_setter_impl(parser, array);
4478 static bool parser_create_array_field_setter(parser_t &parser, ast_value *array, const char *funcname)
4480 ast_expression *root = nullptr;
4481 ast_value *entity = nullptr;
4482 ast_value *index = nullptr;
4483 ast_value *value = nullptr;
4487 if (!ast_istype(array->m_next, ast_value)) {
4488 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4492 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4494 func = fval->m_constval.vfunc;
4495 fval->m_next = new ast_value(array->m_context, "<void>", TYPE_VOID);
4497 entity = new ast_value(array->m_context, "entity", TYPE_ENTITY);
4498 index = new ast_value(array->m_context, "index", TYPE_FLOAT);
4499 value = new ast_value(ast_copy_type, *(ast_value*)array->m_next);
4500 if (!entity || !index || !value) {
4501 parseerror(parser, "failed to create locals for array accessor");
4504 value->m_name = "value"; // not important
4505 fval->m_type_params.emplace_back(entity);
4506 fval->m_type_params.emplace_back(index);
4507 fval->m_type_params.emplace_back(value);
4509 root = array_field_setter_node(parser, array, entity, index, value, 0, array->m_count);
4511 parseerror(parser, "failed to build accessor search tree");
4515 array->m_setter = fval;
4516 return func->m_blocks[0].get()->addExpr(root);
4518 if (entity) delete entity;
4519 if (index) delete index;
4520 if (value) delete value;
4521 if (root) delete root;
4527 static ast_value* parser_create_array_getter_proto(parser_t &parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4529 ast_value *index = nullptr;
4533 /* NOTE: checking array->m_next rather than elemtype since
4534 * for fields elemtype is a temporary fieldtype.
4536 if (!ast_istype(array->m_next, ast_value)) {
4537 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
4541 if (!parser_create_array_accessor(parser, array, funcname, &fval))
4543 func = fval->m_constval.vfunc;
4544 fval->m_next = new ast_expression(ast_copy_type, array->m_context, *elemtype);
4546 index = new ast_value(array->m_context, "index", TYPE_FLOAT);
4549 parseerror(parser, "failed to create locals for array accessor");
4552 fval->m_type_params.emplace_back(index);
4554 array->m_getter = fval;
4557 if (index) delete index;
4563 static bool parser_create_array_getter_impl(parser_t &parser, ast_value *array)
4565 ast_expression *root = nullptr;
4567 root = array_getter_node(parser, array, array->m_getter->m_type_params[0].get(), 0, array->m_count);
4569 parseerror(parser, "failed to build accessor search tree");
4572 if (!array->m_getter->m_constval.vfunc->m_blocks[0].get()->addExpr(root)) {
4579 static bool parser_create_array_getter(parser_t &parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
4581 if (!parser_create_array_getter_proto(parser, array, elemtype, funcname))
4583 return parser_create_array_getter_impl(parser, array);
4586 static ast_value *parse_parameter_list(parser_t &parser, ast_value *var)
4588 lex_ctx_t ctx = parser_ctx(parser);
4589 std::vector<std::unique_ptr<ast_value>> params;
4592 bool variadic = false;
4593 ast_value *varparam = nullptr;
4594 char *argcounter = nullptr;
4596 /* for the sake of less code we parse-in in this function */
4597 if (!parser_next(parser)) {
4599 parseerror(parser, "expected parameter list");
4603 /* parse variables until we hit a closing paren */
4604 while (parser.tok != ')') {
4605 bool is_varargs = false;
4608 /* there must be commas between them */
4609 if (parser.tok != ',') {
4610 parseerror(parser, "expected comma or end of parameter list");
4613 if (!parser_next(parser)) {
4614 parseerror(parser, "expected parameter");
4620 ast_value *param = parse_typename(parser, nullptr, nullptr, &is_varargs);
4621 if (!param && !is_varargs)
4624 /* '...' indicates a varargs function */
4626 if (parser.tok != ')' && parser.tok != Token::IDENT) {
4627 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4630 if (parser.tok == Token::IDENT) {
4631 argcounter = util_strdup(parser_tokval(parser));
4632 if (!parser_next(parser) || parser.tok != ')') {
4633 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4638 params.emplace_back(param);
4639 if (param->m_vtype >= TYPE_VARIANT) {
4640 char tname[1024]; /* typename is reserved in C++ */
4641 ast_type_to_string(param, tname, sizeof(tname));
4642 parseerror(parser, "type not supported as part of a parameter list: %s", tname);
4645 /* type-restricted varargs */
4646 if (parser.tok == Token::DOTS) {
4648 varparam = params.back().release();
4650 if (!parser_next(parser) || (parser.tok != ')' && parser.tok != Token::IDENT)) {
4651 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4654 if (parser.tok == Token::IDENT) {
4655 argcounter = util_strdup(parser_tokval(parser));
4656 param->m_name = argcounter;
4657 if (!parser_next(parser) || parser.tok != ')') {
4658 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
4663 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC && param->m_name[0] == '<') {
4664 parseerror(parser, "parameter name omitted");
4670 if (params.size() == 1 && params[0]->m_vtype == TYPE_VOID)
4674 if (params.size() > 8 && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4675 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
4678 if (!parser_next(parser)) {
4679 parseerror(parser, "parse error after typename");
4683 /* now turn 'var' into a function type */
4684 fval = new ast_value(ctx, "<type()>", TYPE_FUNCTION);
4687 fval->m_flags |= AST_FLAG_VARIADIC;
4690 var->m_type_params = move(params);
4691 var->m_varparam = varparam;
4692 var->m_argcounter = argcounter;
4705 static ast_value *parse_arraysize(parser_t &parser, ast_value *var)
4707 ast_expression *cexp;
4708 ast_value *cval, *tmp;
4711 ctx = parser_ctx(parser);
4713 if (!parser_next(parser)) {
4715 parseerror(parser, "expected array-size");
4719 if (parser.tok != ']') {
4720 cexp = parse_expression_leave(parser, true, false, false);
4722 if (!cexp || !ast_istype(cexp, ast_value)) {
4726 parseerror(parser, "expected array-size as constant positive integer");
4729 cval = (ast_value*)cexp;
4736 tmp = new ast_value(ctx, "<type[]>", TYPE_ARRAY);
4741 if (cval->m_vtype == TYPE_INTEGER)
4742 tmp->m_count = cval->m_constval.vint;
4743 else if (cval->m_vtype == TYPE_FLOAT)
4744 tmp->m_count = cval->m_constval.vfloat;
4748 parseerror(parser, "array-size must be a positive integer constant");
4755 var->m_flags |= AST_FLAG_ARRAY_INIT;
4758 if (parser.tok != ']') {
4760 parseerror(parser, "expected ']' after array-size");
4763 if (!parser_next(parser)) {
4765 parseerror(parser, "error after parsing array size");
4771 /* Parse a complete typename.
4772 * for single-variables (ie. function parameters or typedefs) storebase should be nullptr
4773 * but when parsing variables separated by comma
4774 * 'storebase' should point to where the base-type should be kept.
4775 * The base type makes up every bit of type information which comes *before* the
4778 * NOTE: The value must either be named, have a nullptr name, or a name starting
4779 * with '<'. In the first case, this will be the actual variable or type
4780 * name, in the other cases it is assumed that the name will appear
4781 * later, and an error is generated otherwise.
4783 * The following will be parsed in its entirety:
4785 * The 'basetype' in this case is 'void()'
4786 * and if there's a comma after it, say:
4788 * then the type-information 'void()' can be stored in 'storebase'
4790 static ast_value *parse_typename(parser_t &parser, ast_value **storebase, ast_value *cached_typedef, bool *is_vararg)
4792 ast_value *var, *tmp;
4795 const char *name = nullptr;
4796 bool isfield = false;
4797 bool wasarray = false;
4798 size_t morefields = 0;
4800 bool vararg = (parser.tok == Token::DOTS);
4802 ctx = parser_ctx(parser);
4804 /* types may start with a dot */
4805 if (parser.tok == '.' || parser.tok == Token::DOTS) {
4807 if (parser.tok == Token::DOTS)
4809 /* if we parsed a dot we need a typename now */
4810 if (!parser_next(parser)) {
4811 parseerror(parser, "expected typename for field definition");
4815 /* Further dots are handled seperately because they won't be part of the
4819 if (parser.tok == '.')
4821 else if (parser.tok == Token::DOTS)
4826 if (!parser_next(parser)) {
4827 parseerror(parser, "expected typename for field definition");
4832 if (parser.tok == Token::IDENT)
4833 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
4834 if (!cached_typedef && parser.tok != Token::TYPENAME) {
4835 if (vararg && is_vararg) {
4839 parseerror(parser, "expected typename");
4843 /* generate the basic type value */
4844 if (cached_typedef) {
4845 var = new ast_value(ast_copy_type, *cached_typedef);
4846 var->m_name = "<type(from_def)>";
4848 var = new ast_value(ctx, "<type>", parser_token(parser)->constval.t);
4850 for (; morefields; --morefields) {
4851 tmp = new ast_value(ctx, "<.type>", TYPE_FIELD);
4856 /* do not yet turn into a field - remember:
4857 * .void() foo; is a field too
4858 * .void()() foo; is a function
4862 if (!parser_next(parser)) {
4864 parseerror(parser, "parse error after typename");
4868 /* an opening paren now starts the parameter-list of a function
4869 * this is where original-QC has parameter lists.
4870 * We allow a single parameter list here.
4871 * Much like fteqcc we don't allow `float()() x`
4873 if (parser.tok == '(') {
4874 var = parse_parameter_list(parser, var);
4879 /* store the base if requested */
4881 *storebase = new ast_value(ast_copy_type, *var);
4883 tmp = new ast_value(ctx, "<type:f>", TYPE_FIELD);
4884 tmp->m_next = *storebase;
4889 /* there may be a name now */
4890 if (parser.tok == Token::IDENT || parser.tok == Token::KEYWORD) {
4891 if (!strcmp(parser_tokval(parser), "break"))
4892 (void)!parsewarning(parser, WARN_BREAKDEF, "break definition ignored (suggest removing it)");
4893 else if (parser.tok == Token::KEYWORD)
4896 name = util_strdup(parser_tokval(parser));
4899 if (!parser_next(parser)) {
4902 parseerror(parser, "error after variable or field declaration");
4908 /* now this may be an array */
4909 if (parser.tok == '[') {
4911 var = parse_arraysize(parser, var);
4913 if (name) mem_d(name);
4918 /* This is the point where we can turn it into a field */
4920 /* turn it into a field if desired */
4921 tmp = new ast_value(ctx, "<type:f>", TYPE_FIELD);
4926 /* now there may be function parens again */
4927 if (parser.tok == '(' && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC)
4928 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
4929 if (parser.tok == '(' && wasarray)
4930 parseerror(parser, "arrays as part of a return type is not supported");
4931 while (parser.tok == '(') {
4932 var = parse_parameter_list(parser, var);
4934 if (name) mem_d(name);
4939 /* finally name it */
4942 // free the name, ast_value_set_name duplicates
4949 static bool parse_typedef(parser_t &parser)
4951 ast_value *typevar, *oldtype;
4952 ast_expression *old;
4954 typevar = parse_typename(parser, nullptr, nullptr, nullptr);
4959 // while parsing types, the ast_value's get named '<something>'
4960 if (!typevar->m_name.length() || typevar->m_name[0] == '<') {
4961 parseerror(parser, "missing name in typedef");
4966 if ( (old = parser_find_var(parser, typevar->m_name)) ) {
4967 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
4968 " -> `%s` has been declared here: %s:%i",
4969 typevar->m_name, old->m_context.file, old->m_context.line);
4974 if ( (oldtype = parser_find_typedef(parser, typevar->m_name, parser._blocktypedefs.back())) ) {
4975 parseerror(parser, "type `%s` has already been declared here: %s:%i",
4976 typevar->m_name, oldtype->m_context.file, oldtype->m_context.line);
4981 parser._typedefs.emplace_back(typevar);
4982 util_htset(parser.typedefs.back(), typevar->m_name.c_str(), typevar);
4984 if (parser.tok != ';') {
4985 parseerror(parser, "expected semicolon after typedef");
4988 if (!parser_next(parser)) {
4989 parseerror(parser, "parse error after typedef");
4996 static const char *cvq_to_str(int cvq) {
4998 case CV_NONE: return "none";
4999 case CV_VAR: return "`var`";
5000 case CV_CONST: return "`const`";
5001 default: return "<INVALID>";
5005 static bool parser_check_qualifiers(parser_t &parser, const ast_value *var, const ast_value *proto)
5008 if (proto->m_cvq != var->m_cvq) {
5009 if (!(proto->m_cvq == CV_CONST && var->m_cvq == CV_NONE &&
5010 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
5013 return !parsewarning(parser, WARN_DIFFERENT_QUALIFIERS,
5014 "`%s` declared with different qualifiers: %s\n"
5015 " -> previous declaration here: %s:%i uses %s",
5016 var->m_name, cvq_to_str(var->m_cvq),
5017 proto->m_context.file, proto->m_context.line,
5018 cvq_to_str(proto->m_cvq));
5021 av = (var ->m_flags & AST_FLAG_NORETURN);
5022 ao = (proto->m_flags & AST_FLAG_NORETURN);
5024 return !parsewarning(parser, WARN_DIFFERENT_ATTRIBUTES,
5025 "`%s` declared with different attributes%s\n"
5026 " -> previous declaration here: %s:%i",
5027 var->m_name, (av ? ": noreturn" : ""),
5028 proto->m_context.file, proto->m_context.line,
5029 (ao ? ": noreturn" : ""));
5034 static bool create_array_accessors(parser_t &parser, ast_value *var)
5037 util_snprintf(name, sizeof(name), "%s##SET", var->m_name.c_str());
5038 if (!parser_create_array_setter(parser, var, name))
5040 util_snprintf(name, sizeof(name), "%s##GET", var->m_name.c_str());
5041 if (!parser_create_array_getter(parser, var, var->m_next, name))
5046 static bool parse_array(parser_t &parser, ast_value *array)
5049 if (array->m_initlist.size()) {
5050 parseerror(parser, "array already initialized elsewhere");
5053 if (!parser_next(parser)) {
5054 parseerror(parser, "parse error in array initializer");
5058 while (parser.tok != '}') {
5059 ast_value *v = (ast_value*)parse_expression_leave(parser, true, false, false);
5062 if (!ast_istype(v, ast_value) || !v->m_hasvalue || v->m_cvq != CV_CONST) {
5064 parseerror(parser, "initializing element must be a compile time constant");
5067 array->m_initlist.push_back(v->m_constval);
5068 if (v->m_vtype == TYPE_STRING) {
5069 array->m_initlist[i].vstring = util_strdupe(array->m_initlist[i].vstring);
5073 if (parser.tok == '}')
5075 if (parser.tok != ',' || !parser_next(parser)) {
5076 parseerror(parser, "expected comma or '}' in element list");
5080 if (!parser_next(parser) || parser.tok != ';') {
5081 parseerror(parser, "expected semicolon after initializer, got %s");
5085 if (!parser_next(parser)) {
5086 parseerror(parser, "parse error after initializer");
5091 if (array->m_flags & AST_FLAG_ARRAY_INIT) {
5092 if (array->m_count != (size_t)-1) {
5093 parseerror(parser, "array `%s' has already been initialized with %u elements",
5094 array->m_name, (unsigned)array->m_count);
5096 array->m_count = array->m_initlist.size();
5097 if (!create_array_accessors(parser, array))
5103 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)
5105 if (!localblock && is_static) {
5106 parseerror(parser, "`static` qualifier is not supported in global scope");
5109 // get the first complete variable
5110 ast_value *basetype = nullptr;
5111 auto var = parse_typename(parser, &basetype, cached_typedef, nullptr);
5119 // while parsing types, the ast_value's get named '<something>'
5120 if (!var->m_name.length() || var->m_name[0] == '<') {
5121 parseerror(parser, "declaration does not declare anything");
5131 bool isparam = false;
5132 bool isvector = false;
5133 bool cleanvar = true;
5135 ast_member *me[3] = { nullptr, nullptr, nullptr };
5136 ast_member *last_me[3] = { nullptr, nullptr, nullptr };
5140 ast_value *proto = nullptr;
5141 bool wasarray = false;
5143 // Part 0: finish the type
5144 if (parser.tok == '(') {
5145 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5146 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
5148 var = parse_parameter_list(parser, var);
5154 // we only allow 1-dimensional arrays
5155 if (parser.tok == '[') {
5157 var = parse_arraysize(parser, var);
5163 if (parser.tok == '(' && wasarray) {
5164 parseerror(parser, "arrays as part of a return type is not supported");
5165 // we'll still parse the type completely for now
5167 // for functions returning functions
5168 while (parser.tok == '(') {
5169 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5170 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
5172 var = parse_parameter_list(parser, var);
5179 var->m_cvq = qualifier;
5180 if (qflags & AST_FLAG_COVERAGE) {
5181 // specified in QC, drop our default
5182 var->m_flags &= ~(AST_FLAG_COVERAGE_MASK);
5184 var->m_flags |= qflags;
5187 * store the vstring back to var for alias and
5188 * deprecation messages.
5190 if (var->m_flags & AST_FLAG_DEPRECATED
5191 || var->m_flags & AST_FLAG_ALIAS) {
5192 var->m_desc = vstring;
5195 if (parser_find_global(parser, var->m_name) && var->m_flags & AST_FLAG_ALIAS) {
5196 parseerror(parser, "function aliases cannot be forward declared");
5203 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
5204 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
5205 * is then filled with the previous definition and the parameter-names replaced.
5207 if (var->m_name == "nil") {
5208 if (OPTS_FLAG(UNTYPED_NIL)) {
5209 if (!localblock || !OPTS_FLAG(PERMISSIVE)) {
5210 parseerror(parser, "name `nil` not allowed (try -fpermissive)");
5213 (void) parsewarning(parser, WARN_RESERVED_NAMES, "variable name `nil` is reserved");
5217 /* Deal with end_sys_ vars */
5218 bool was_end = false;
5219 if (var->m_name == "end_sys_globals") {
5220 var->m_flags |= AST_FLAG_NOREF;
5221 parser.crc_globals = parser.globals.size();
5224 else if (var->m_name == "end_sys_fields") {
5225 var->m_flags |= AST_FLAG_NOREF;
5226 parser.crc_fields = parser.fields.size();
5229 if (was_end && var->m_vtype == TYPE_FIELD) {
5230 if (parsewarning(parser, WARN_END_SYS_FIELDS,
5231 "global '%s' hint should not be a field",
5232 parser_tokval(parser))) {
5238 if (!nofields && var->m_vtype == TYPE_FIELD) {
5239 /* deal with field declarations */
5240 auto old = parser_find_field(parser, var->m_name);
5242 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
5243 var->m_name, old->m_context.file, (int)old->m_context.line)) {
5251 parseerror(parser, "field `%s` already declared here: %s:%i",
5252 var->m_name, old->m_context.file, old->m_context.line);
5257 if ((OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC || OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC)
5258 && (old = parser_find_global(parser, var->m_name)))
5260 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5261 parseerror(parser, "field `%s` already declared here: %s:%i",
5262 var->m_name, old->m_context.file, old->m_context.line);
5269 /* deal with other globals */
5270 auto old = parser_find_global(parser, var->m_name);
5271 if (old && var->m_vtype == TYPE_FUNCTION && old->m_vtype == TYPE_FUNCTION)
5273 /* This is a function which had a prototype */
5274 if (!ast_istype(old, ast_value)) {
5275 parseerror(parser, "internal error: prototype is not an ast_value");
5279 proto = (ast_value*)old;
5280 proto->m_desc = var->m_desc;
5281 if (!proto->compareType(*var)) {
5282 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
5284 proto->m_context.file, proto->m_context.line);
5288 /* we need the new parameter-names */
5289 for (i = 0; i < proto->m_type_params.size(); ++i)
5290 proto->m_type_params[i]->m_name = var->m_type_params[i]->m_name;
5291 if (!parser_check_qualifiers(parser, var, proto)) {
5296 proto->m_flags |= var->m_flags;
5304 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
5305 "global `%s` already declared here: %s:%i",
5306 var->m_name, old->m_context.file, old->m_context.line))
5311 if (old->m_flags & AST_FLAG_FINAL_DECL) {
5312 parseerror(parser, "cannot redeclare variable `%s`, declared final here: %s:%i",
5313 var->m_name, old->m_context.file, old->m_context.line);
5317 proto = (ast_value*)old;
5318 if (!ast_istype(old, ast_value)) {
5319 parseerror(parser, "internal error: not an ast_value");
5324 if (!parser_check_qualifiers(parser, var, proto)) {
5329 proto->m_flags |= var->m_flags;
5330 /* copy the context for finals,
5331 * so the error can show where it was actually made 'final'
5333 if (proto->m_flags & AST_FLAG_FINAL_DECL)
5334 old->m_context = var->m_context;
5338 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC &&
5339 (old = parser_find_field(parser, var->m_name)))
5341 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
5342 parseerror(parser, "global `%s` already declared here: %s:%i",
5343 var->m_name, old->m_context.file, old->m_context.line);
5350 else { // it's not a global
5351 if (auto old = parser_find_local(parser, var->m_name, parser.variables.size() - 1, &isparam)) {
5353 parseerror(parser, "local `%s` already declared here: %s:%i",
5354 var->m_name, old->m_context.file, (int) old->m_context.line);
5359 // doing this here as the above is just for a single scope
5360 if (auto old = parser_find_local(parser, var->m_name, 0, &isparam)) {
5362 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
5363 "local `%s` is shadowing a parameter", var->m_name)) {
5364 parseerror(parser, "local `%s` already declared here: %s:%i",
5365 var->m_name, old->m_context.file, (int) old->m_context.line);
5369 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_GMQCC) {
5371 if (ast_istype(old, ast_value))
5372 var = proto = (ast_value *) old;
5382 if (noref || parser.noref) {
5383 var->m_flags |= AST_FLAG_NOREF;
5387 * Create the global/local, and deal with vector types.
5390 if (var->m_vtype == TYPE_VECTOR) {
5392 } else if (var->m_vtype == TYPE_FIELD
5393 && var->m_next->m_vtype == TYPE_VECTOR) {
5397 if (isvector && !create_vector_members(var, me)) {
5403 // deal with global variables, fields, functions
5404 if (!nofields && var->m_vtype == TYPE_FIELD && parser.tok != '=') {
5405 var->m_isfield = true;
5406 parser.fields.push_back(var);
5407 util_htset(parser.htfields, var->m_name.c_str(), var);
5409 for (i = 0; i < 3; ++i) {
5410 parser.fields.push_back(me[i]);
5411 util_htset(parser.htfields, me[i]->m_name.c_str(), me[i]);
5414 } else if (!(var->m_flags & AST_FLAG_ALIAS)) {
5415 parser_addglobal(parser, var->m_name, var);
5417 for (i = 0; i < 3; ++i) {
5418 parser_addglobal(parser, me[i]->m_name.c_str(), me[i]);
5422 ast_expression *find = parser_find_global(parser, var->m_desc);
5425 compile_error(parser_ctx(parser), "undeclared variable `%s` for alias `%s`", var->m_desc, var->m_name);
5429 if (!var->compareType(*find)) {
5433 ast_type_to_string(find, ty1, sizeof(ty1));
5434 ast_type_to_string(var, ty2, sizeof(ty2));
5436 compile_error(parser_ctx(parser), "incompatible types `%s` and `%s` for alias `%s`",
5437 ty1, ty2, var->m_name
5442 util_htset(parser.aliases, var->m_name.c_str(), find);
5444 /* generate aliases for vector components */
5448 util_asprintf(&buffer[0], "%s_x", var->m_desc.c_str());
5449 util_asprintf(&buffer[1], "%s_y", var->m_desc.c_str());
5450 util_asprintf(&buffer[2], "%s_z", var->m_desc.c_str());
5452 util_htset(parser.aliases, me[0]->m_name.c_str(), parser_find_global(parser, buffer[0]));
5453 util_htset(parser.aliases, me[1]->m_name.c_str(), parser_find_global(parser, buffer[1]));
5454 util_htset(parser.aliases, me[2]->m_name.c_str(), parser_find_global(parser, buffer[2]));
5463 // a static adds itself to be generated like any other global
5464 // but is added to the local namespace instead
5465 std::string defname;
5469 defname = parser.function->m_name;
5470 defname.append(2, ':');
5472 // remember the length up to here
5473 prefix_len = defname.length();
5475 // Add it to the local scope
5476 util_htset(parser.variables.back(), var->m_name.c_str(), (void*)var);
5478 // now rename the global
5479 defname.append(var->m_name);
5480 // if a variable of that name already existed, add the
5482 // The counter is incremented either way.
5483 sn_size = parser.function->m_static_names.size();
5484 for (sn = 0; sn != sn_size; ++sn) {
5485 if (parser.function->m_static_names[sn] == var->m_name.c_str())
5488 if (sn != sn_size) {
5489 char *num = nullptr;
5490 int len = util_asprintf(&num, "#%u", parser.function->m_static_count);
5491 defname.append(num, 0, len);
5495 parser.function->m_static_names.emplace_back(var->m_name);
5496 parser.function->m_static_count++;
5497 var->m_name = defname;
5499 // push it to the to-be-generated globals
5500 parser.globals.push_back(var);
5502 // same game for the vector members
5504 defname.erase(prefix_len);
5505 for (i = 0; i < 3; ++i) {
5506 util_htset(parser.variables.back(), me[i]->m_name.c_str(), (void*)(me[i]));
5507 me[i]->m_name = defname + me[i]->m_name;
5508 parser.globals.push_back(me[i]);
5512 localblock->m_locals.push_back(var);
5513 parser_addlocal(parser, var->m_name, var);
5515 for (i = 0; i < 3; ++i) {
5516 parser_addlocal(parser, me[i]->m_name, me[i]);
5517 localblock->collect(me[i]);
5523 memcpy(last_me, me, sizeof(me));
5524 me[0] = me[1] = me[2] = nullptr;
5529 if (var->m_vtype == TYPE_ARRAY) {
5530 if (var->m_count != (size_t)-1) {
5531 if (!create_array_accessors(parser, var))
5534 } else if (!localblock && !nofields &&
5535 var->m_vtype == TYPE_FIELD &&
5536 var->m_next->m_vtype == TYPE_ARRAY) {
5538 ast_expression *telem;
5540 ast_value *array = (ast_value*)var->m_next;
5542 if (!ast_istype(var->m_next, ast_value)) {
5543 parseerror(parser, "internal error: field element type must be an ast_value");
5547 util_snprintf(name, sizeof(name), "%s##SETF", var->m_name.c_str());
5548 if (!parser_create_array_field_setter(parser, array, name))
5551 telem = new ast_expression(ast_copy_type, var->m_context, *array->m_next);
5552 tfield = new ast_value(var->m_context, "<.type>", TYPE_FIELD);
5553 tfield->m_next = telem;
5554 util_snprintf(name, sizeof(name), "%s##GETFP", var->m_name.c_str());
5555 if (!parser_create_array_getter(parser, array, tfield, name)) {
5563 if (parser.tok == ';') {
5565 if (!parser_next(parser)) {
5566 parseerror(parser, "error after variable declaration");
5572 if (parser.tok == ',')
5576 if (!var || (!localblock && !nofields && basetype->m_vtype == TYPE_FIELD)) {
5579 parseerror(parser, "missing comma or semicolon while parsing variables");
5583 if (localblock && OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5584 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
5585 "initializing expression turns variable `%s` into a constant in this standard",
5591 if (parser.tok != '{' || var->m_vtype != TYPE_FUNCTION) {
5592 if (parser.tok != '=') {
5593 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
5597 if (!parser_next(parser)) {
5598 parseerror(parser, "error parsing initializer");
5602 else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
5603 parseerror(parser, "expected '=' before function body in this standard");
5606 if (parser.tok == '#') {
5607 ast_function *func = nullptr;
5608 ast_value *number = nullptr;
5611 parseerror(parser, "cannot declare builtins within functions");
5614 if (var->m_vtype != TYPE_FUNCTION) {
5615 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->m_name);
5618 if (!parser_next(parser)) {
5619 parseerror(parser, "expected builtin number");
5624 if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)) {
5625 number = (ast_value*)parse_expression_leave(parser, true, false, false);
5627 parseerror(parser, "builtin number expected");
5630 if (!ast_istype(number, ast_value) || !number->m_hasvalue || number->m_cvq != CV_CONST)
5633 parseerror(parser, "builtin number must be a compile time constant");
5636 if (number->m_vtype == TYPE_INTEGER)
5637 builtin_num = number->m_constval.vint;
5638 else if (number->m_vtype == TYPE_FLOAT)
5639 builtin_num = number->m_constval.vfloat;
5642 parseerror(parser, "builtin number must be an integer constant");
5648 float fractional = modff(builtin_num, &integral);
5649 if (builtin_num < 0 || fractional != 0) {
5650 parseerror(parser, "builtin number must be an integer greater than zero");
5654 /* we only want the integral part anyways */
5655 builtin_num = integral;
5656 } else if (parser.tok == Token::INTCONST) {
5657 builtin_num = parser_token(parser)->constval.i;
5659 parseerror(parser, "builtin number must be a compile time constant");
5663 if (var->m_hasvalue) {
5664 (void) parsewarning(parser, WARN_DOUBLE_DECLARATION,
5665 "builtin `%s` has already been defined\n"
5666 " -> previous declaration here: %s:%i",
5667 var->m_name, var->m_context.file, (int)var->m_context.line);
5671 func = ast_function::make(var->m_context, var->m_name, var);
5673 parseerror(parser, "failed to allocate function for `%s`", var->m_name);
5676 parser.functions.push_back(func);
5678 func->m_builtin = -builtin_num-1;
5681 if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)
5682 ? (parser.tok != ',' && parser.tok != ';')
5683 : (!parser_next(parser)))
5685 parseerror(parser, "expected comma or semicolon");
5687 var->m_constval.vfunc = nullptr;
5691 else if (var->m_vtype == TYPE_ARRAY && parser.tok == '{')
5694 /* Note that fteqcc and most others don't even *have*
5695 * local arrays, so this is not a high priority.
5697 parseerror(parser, "TODO: initializers for local arrays");
5701 var->m_hasvalue = true;
5702 if (!parse_array(parser, var))
5705 else if (var->m_vtype == TYPE_FUNCTION && (parser.tok == '{' || parser.tok == '['))
5708 parseerror(parser, "cannot declare functions within functions");
5713 proto->m_context = parser_ctx(parser);
5715 if (!parse_function_body(parser, var))
5718 for (auto &it : parser.gotos)
5719 parseerror(parser, "undefined label: `%s`", it->m_name);
5720 parser.gotos.clear();
5721 parser.labels.clear();
5724 bool folded_const = false;
5726 auto cexp = parse_expression_leave(parser, true, false, false);
5729 auto cval = ast_istype(cexp, ast_value) ? reinterpret_cast<ast_value*>(cexp) : nullptr;
5731 /* deal with foldable constants: */
5733 var->m_cvq == CV_CONST && cval && cval->m_hasvalue && cval->m_cvq == CV_CONST && !cval->m_isfield)
5735 /* remove it from the current locals */
5737 for (i = 0; i < 3; ++i) {
5738 parser._locals.pop_back();
5739 localblock->m_collect.pop_back();
5742 /* do sanity checking, this function really needs refactoring */
5743 if (parser._locals.back() != var)
5744 parseerror(parser, "internal error: unexpected change in local variable handling");
5746 parser._locals.pop_back();
5747 if (localblock->m_locals.back() != var)
5748 parseerror(parser, "internal error: unexpected change in local variable handling (2)");
5750 localblock->m_locals.pop_back();
5751 /* push it to the to-be-generated globals */
5752 parser.globals.push_back(var);
5754 for (i = 0; i < 3; ++i)
5755 parser.globals.push_back(last_me[i]);
5756 folded_const = true;
5759 if (folded_const || !localblock || is_static) {
5760 if (cval != parser.nil &&
5761 (!cval || ((!cval->m_hasvalue || cval->m_cvq != CV_CONST) && !cval->m_isfield))
5764 parseerror(parser, "initializer is non constant");
5769 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
5770 qualifier != CV_VAR)
5772 var->m_cvq = CV_CONST;
5774 if (cval == parser.nil)
5776 var->m_flags |= AST_FLAG_INITIALIZED;
5777 var->m_flags |= AST_FLAG_NOREF;
5781 var->m_hasvalue = true;
5782 if (cval->m_vtype == TYPE_STRING)
5783 var->m_constval.vstring = parser_strdup(cval->m_constval.vstring);
5784 else if (cval->m_vtype == TYPE_FIELD)
5785 var->m_constval.vfield = cval;
5787 memcpy(&var->m_constval, &cval->m_constval, sizeof(var->m_constval));
5793 int cvq = var->m_cvq;
5794 var->m_cvq = CV_NONE;
5795 sy.out.push_back(syexp(var->m_context, var));
5796 sy.out.push_back(syexp(cexp->m_context, cexp));
5797 sy.ops.push_back(syop(var->m_context, parser.assign_op));
5798 if (!parser_sy_apply_operator(parser, &sy))
5801 if (sy.out.size() != 1 && sy.ops.size() != 0)
5802 parseerror(parser, "internal error: leaked operands");
5803 if (!localblock->addExpr(sy.out[0].out))
5808 /* a constant initialized to an inexact value should be marked inexact:
5809 * const float x = <inexact>; should propagate the inexact flag
5811 if (var->m_cvq == CV_CONST && var->m_vtype == TYPE_FLOAT) {
5812 if (cval && cval->m_hasvalue && cval->m_cvq == CV_CONST)
5813 var->m_inexact = cval->m_inexact;
5818 if (parser.tok == ',') {
5819 if (!parser_next(parser)) {
5820 parseerror(parser, "expected another variable");
5824 if (parser.tok != Token::IDENT) {
5825 parseerror(parser, "expected another variable");
5828 var = new ast_value(ast_copy_type, *basetype);
5830 var->m_name = parser_tokval(parser);
5831 if (!parser_next(parser)) {
5832 parseerror(parser, "error parsing variable declaration");
5838 if (parser.tok != ';') {
5839 parseerror(parser, "missing semicolon after variables");
5843 if (!parser_next(parser)) {
5844 parseerror(parser, "parse error after variable declaration");
5852 if (cleanvar && var)
5859 if (cleanvar && var)
5867 static bool parser_global_statement(parser_t &parser)
5869 ast_value *istype = nullptr;
5870 if ((parser.tok == Token::IDENT && (istype = parser_find_typedef(parser, parser_tokval(parser), 0)) != nullptr)
5871 || parser.tok == Token::TYPENAME
5872 || parser.tok == '.' || parser.tok == Token::DOTS) {
5873 return parse_variable(parser, nullptr, false, CV_NONE, istype, false, false, 0, nullptr);
5878 bool is_static = false;
5879 uint32_t qflags = 0;
5880 char *vstring = nullptr;
5881 if (parse_qualifiers(parser, false, &cvq, &noref, &is_static, &qflags, &vstring)) {
5882 if (cvq == CV_WRONG)
5884 return parse_variable(parser, nullptr, false, cvq, nullptr, noref, is_static, qflags, vstring);
5887 if (parser.tok == Token::IDENT && strcmp(parser_tokval(parser), "enum") == 0) {
5888 return parse_enum(parser);
5891 if (parser.tok == Token::KEYWORD) {
5892 if (strcmp(parser_tokval(parser), "typedef") == 0) {
5893 if (!parser_next(parser)) {
5894 parseerror(parser, "expected type definition after 'typedef'");
5897 return parse_typedef(parser);
5899 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
5903 if (parser.tok == '#') {
5904 return parse_pragma(parser);
5907 if (parser.tok == '$') {
5908 if (!parser_next(parser)) {
5909 parseerror(parser, "parse error");
5915 parseerror(parser, "unexpected token: `%s`", parser.lex->tok.value);
5919 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
5921 return util_crc16(old, str, strlen(str));
5924 static void progdefs_crc_file(const char *str)
5926 /* write to progdefs.h here */
5930 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
5932 old = progdefs_crc_sum(old, str);
5933 progdefs_crc_file(str);
5937 static void generate_checksum(parser_t &parser, ir_builder &ir)
5939 uint16_t crc = 0xFFFF;
5943 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
5944 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
5946 progdefs_crc_file("\tint\tpad;\n");
5947 progdefs_crc_file("\tint\tofs_return[3];\n");
5948 progdefs_crc_file("\tint\tofs_parm0[3];\n");
5949 progdefs_crc_file("\tint\tofs_parm1[3];\n");
5950 progdefs_crc_file("\tint\tofs_parm2[3];\n");
5951 progdefs_crc_file("\tint\tofs_parm3[3];\n");
5952 progdefs_crc_file("\tint\tofs_parm4[3];\n");
5953 progdefs_crc_file("\tint\tofs_parm5[3];\n");
5954 progdefs_crc_file("\tint\tofs_parm6[3];\n");
5955 progdefs_crc_file("\tint\tofs_parm7[3];\n");
5957 for (i = 0; i < parser.crc_globals; ++i) {
5958 if (!ast_istype(parser.globals[i], ast_value))
5960 value = (ast_value*)(parser.globals[i]);
5961 switch (value->m_vtype) {
5962 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5963 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5964 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5965 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5967 crc = progdefs_crc_both(crc, "\tint\t");
5970 crc = progdefs_crc_both(crc, value->m_name.c_str());
5971 crc = progdefs_crc_both(crc, ";\n");
5973 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
5974 for (i = 0; i < parser.crc_fields; ++i) {
5975 if (!ast_istype(parser.fields[i], ast_value))
5977 value = (ast_value*)(parser.fields[i]);
5978 switch (value->m_next->m_vtype) {
5979 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
5980 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
5981 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
5982 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
5984 crc = progdefs_crc_both(crc, "\tint\t");
5987 crc = progdefs_crc_both(crc, value->m_name.c_str());
5988 crc = progdefs_crc_both(crc, ";\n");
5990 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
5991 ir.m_code->crc = crc;
5994 parser_t::parser_t()
5997 , ast_cleaned(false)
6002 , aliases(util_htnew(PARSER_HT_SIZE))
6003 , htfields(util_htnew(PARSER_HT_SIZE))
6004 , htglobals(util_htnew(PARSER_HT_SIZE))
6005 , assign_op(nullptr)
6007 , max_param_count(1)
6011 variables.push_back(htfields);
6012 variables.push_back(htglobals);
6013 typedefs.push_back(util_htnew(TYPEDEF_HT_SIZE));
6014 _blocktypedefs.push_back(0);
6016 lex_ctx_t empty_ctx;
6017 empty_ctx.file = "<internal>";
6019 empty_ctx.column = 0;
6020 nil = new ast_value(empty_ctx, "nil", TYPE_NIL);
6021 nil->m_cvq = CV_CONST;
6022 if (OPTS_FLAG(UNTYPED_NIL))
6023 util_htset(htglobals, "nil", (void*)nil);
6025 const_vec[0] = new ast_value(empty_ctx, "<vector.x>", TYPE_NOEXPR);
6026 const_vec[1] = new ast_value(empty_ctx, "<vector.y>", TYPE_NOEXPR);
6027 const_vec[2] = new ast_value(empty_ctx, "<vector.z>", TYPE_NOEXPR);
6029 if (OPTS_OPTION_BOOL(OPTION_ADD_INFO)) {
6030 reserved_version = new ast_value(empty_ctx, "reserved:version", TYPE_STRING);
6031 reserved_version->m_cvq = CV_CONST;
6032 reserved_version->m_hasvalue = true;
6033 reserved_version->m_flags |= AST_FLAG_INCLUDE_DEF;
6034 reserved_version->m_flags |= AST_FLAG_NOREF;
6035 reserved_version->m_constval.vstring = util_strdup(GMQCC_FULL_VERSION_STRING);
6037 reserved_version = nullptr;
6041 parser_t::~parser_t()
6046 parser_t *parser_create()
6048 auto parser = new parser_t;
6049 for (size_t i = 0; i < operator_count; ++i) {
6050 if (operators[i].id == opid1('=')) {
6051 parser->assign_op = &operators[i];
6055 if (!parser->assign_op) {
6056 con_err("internal error: initializing parser: failed to find assign operator\n");
6064 /** parser entrypoint */
6065 static bool parser_compile(parser_t &parser)
6067 /* initial lexer/parser state */
6068 parser.lex->flags.noops = true;
6070 if (!parser_next(parser)) {
6071 parseerror(parser, "parse error");
6072 compile_errors = true;
6075 while (parser.tok != Token::END && parser.tok < Token::ERROR) {
6076 if (parser_global_statement(parser)) continue;
6077 if (parser.tok == Token::END) {
6078 parseerror(parser, "unexpected end of file");
6079 } else if (compile_errors) {
6080 parseerror(parser, "there have been errors, bailing out");
6082 compile_errors = true;
6087 lex_close(parser.lex);
6088 parser.lex = nullptr;
6089 return !compile_errors;
6092 bool parser_compile_file(parser_t &parser, const char *filename)
6094 parser.lex = lex_open(filename);
6096 con_err("failed to open file \"%s\"\n", filename);
6099 return parser_compile(parser);
6102 bool parser_compile_string(parser_t &parser, const char *name, const char *str, size_t len)
6104 parser.lex = lex_open_string(str, len, name);
6106 con_err("failed to create lexer for string \"%s\"\n", name);
6109 return parser_compile(parser);
6112 void parser_t::remove_ast()
6117 for (auto &it : accessors) {
6118 delete it->m_constval.vfunc;
6119 it->m_constval.vfunc = nullptr;
6122 for (auto &it : functions) delete it;
6123 for (auto &it : globals) delete it;
6124 for (auto &it : fields) delete it;
6126 for (auto &it : variables) util_htdel(it);
6128 _blocklocals.clear();
6132 for (auto &it : typedefs) util_htdel(it);
6134 _blocktypedefs.clear();
6140 delete const_vec[0];
6141 delete const_vec[1];
6142 delete const_vec[2];
6144 if (reserved_version)
6145 delete reserved_version;
6147 util_htdel(aliases);
6150 static bool parser_set_coverage_func(parser_t &parser, ir_builder &ir) {
6151 ast_expression *expr;
6155 if (!OPTS_OPTION_BOOL(OPTION_COVERAGE))
6159 for (auto &it : parser.functions) {
6160 if (it->m_name == "coverage") {
6166 if (OPTS_OPTION_BOOL(OPTION_COVERAGE)) {
6167 con_out("coverage support requested but no coverage() builtin declared\n");
6173 cov = func->m_function_type;
6176 if (expr->m_vtype != TYPE_FUNCTION || expr->m_type_params.size()) {
6178 ast_type_to_string(expr, ty, sizeof(ty));
6179 con_out("invalid type for coverage(): %s\n", ty);
6183 ir.m_coverage_func = func->m_ir_func->m_value;
6187 bool parser_finish(parser_t &parser, const char *output)
6191 if (compile_errors) {
6192 con_out("*** there were compile errors\n");
6196 ir_builder ir("gmqcc_out");
6198 for (auto &it : parser.fields) {
6199 if (!ast_istype(it, ast_value))
6201 auto field = reinterpret_cast<ast_value*>(it);
6202 auto hasvalue = field->m_hasvalue;
6203 field->m_hasvalue = false;
6204 if (!field->generateGlobal(ir, true)) {
6205 con_out("failed to generate field %s\n", field->m_name.c_str());
6208 field->m_hasvalue = hasvalue;
6210 auto subtype = field->m_next;
6211 auto ifld = ir.createField(field->m_name, subtype->m_vtype);
6212 if (subtype->m_vtype == TYPE_FIELD)
6213 ifld->m_fieldtype = subtype->m_next->m_vtype;
6214 else if (subtype->m_vtype == TYPE_FUNCTION)
6215 ifld->m_outtype = subtype->m_next->m_vtype;
6216 (void)!field->m_ir_v->setField(ifld);
6219 for (auto &it : parser.globals) {
6220 if (!ast_istype(it, ast_value))
6222 auto asvalue = reinterpret_cast<ast_value*>(it);
6223 if (!(asvalue->m_flags & AST_FLAG_NOREF) && asvalue->m_cvq != CV_CONST && asvalue->m_vtype != TYPE_FUNCTION) {
6224 retval = retval && !compile_warning(asvalue->m_context, WARN_UNUSED_VARIABLE,
6225 "unused global: `%s`", asvalue->m_name);
6227 if (!asvalue->generateGlobal(ir, false)) {
6228 con_out("failed to generate global %s\n", asvalue->m_name.c_str());
6232 /* Build function vararg accessor ast tree now before generating
6233 * immediates, because the accessors may add new immediates
6235 for (auto &f : parser.functions) {
6237 if (parser.max_param_count > f->m_function_type->m_type_params.size()) {
6238 f->m_varargs->m_count = parser.max_param_count - f->m_function_type->m_type_params.size();
6239 if (!parser_create_array_setter_impl(parser, f->m_varargs.get())) {
6240 con_out("failed to generate vararg setter for %s\n", f->m_name.c_str());
6243 if (!parser_create_array_getter_impl(parser, f->m_varargs.get())) {
6244 con_out("failed to generate vararg getter for %s\n", f->m_name.c_str());
6248 f->m_varargs = nullptr;
6252 /* Now we can generate immediates */
6253 if (!parser.m_fold.generate(ir))
6256 /* before generating any functions we need to set the coverage_func */
6257 if (!parser_set_coverage_func(parser, ir))
6259 for (auto &it : parser.globals) {
6260 if (!ast_istype(it, ast_value))
6262 auto asvalue = reinterpret_cast<ast_value*>(it);
6263 if (!(asvalue->m_flags & AST_FLAG_INITIALIZED))
6265 if (asvalue->m_cvq == CV_CONST && !asvalue->m_hasvalue)
6266 (void)!compile_warning(asvalue->m_context, WARN_UNINITIALIZED_CONSTANT,
6267 "uninitialized constant: `%s`",
6269 else if ((asvalue->m_cvq == CV_NONE || asvalue->m_cvq == CV_CONST) && !asvalue->m_hasvalue)
6270 (void)!compile_warning(asvalue->m_context, WARN_UNINITIALIZED_GLOBAL,
6271 "uninitialized global: `%s`",
6274 if (!asvalue->generateAccessors(ir)) {
6278 for (auto &it : parser.fields) {
6279 auto asvalue = reinterpret_cast<ast_value*>(it->m_next);
6280 if (!ast_istype(asvalue, ast_value))
6282 if (asvalue->m_vtype != TYPE_ARRAY)
6284 if (!asvalue->generateAccessors(ir)) {
6288 if (parser.reserved_version &&
6289 !parser.reserved_version->generateGlobal(ir, false))
6291 con_out("failed to generate reserved::version");
6294 for (auto &f : parser.functions) {
6295 if (!f->generateFunction(ir)) {
6296 con_out("failed to generate function %s\n", f->m_name.c_str());
6301 generate_checksum(parser, ir);
6303 if (OPTS_OPTION_BOOL(OPTION_DUMP))
6305 for (auto &it : parser.functions) {
6306 if (!ir_function_finalize(it->m_ir_func)) {
6307 con_out("failed to finalize function %s\n", it->m_name.c_str());
6311 parser.remove_ast();
6313 auto fnCheckWErrors = [&retval]() {
6314 if (compile_Werrors) {
6315 con_out("*** there were warnings treated as errors\n");
6316 compile_show_werrors();
6324 if (OPTS_OPTION_BOOL(OPTION_DUMPFIN))
6327 if (!ir.generate(output)) {
6328 con_out("*** failed to generate output file\n");
6332 // ir->generate can generate compiler warnings