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_EOF)
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 char *parser_strdup(const char *str)
83 /* actually dup empty strings */
84 char *out = (char*)mem_a(1);
88 return util_strdup(str);
91 static ast_expression* parser_find_field(parser_t *parser, const char *name) {
92 return (ast_expression*)util_htget(parser->htfields, name);
94 static ast_expression* parser_find_field(parser_t *parser, const std::string &name) {
95 return parser_find_field(parser, name.c_str());
98 static ast_expression* parser_find_label(parser_t *parser, const char *name)
100 for (auto &it : parser->labels)
101 if (it->m_name == name)
105 static inline ast_expression* parser_find_label(parser_t *parser, const std::string &name) {
106 return parser_find_label(parser, name.c_str());
109 ast_expression* parser_find_global(parser_t *parser, const char *name)
111 ast_expression *var = (ast_expression*)util_htget(parser->aliases, parser_tokval(parser));
114 return (ast_expression*)util_htget(parser->htglobals, name);
117 ast_expression* parser_find_global(parser_t *parser, const std::string &name) {
118 return parser_find_global(parser, name.c_str());
121 static ast_expression* parser_find_param(parser_t *parser, const char *name)
124 if (!parser->function)
126 fun = parser->function->m_function_type;
127 for (auto &it : fun->m_type_params) {
128 if (it->m_name == name)
134 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
139 hash = util_hthash(parser->htglobals, name);
142 for (i = parser->variables.size(); i > upto;) {
144 if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
148 return parser_find_param(parser, name);
151 static ast_expression* parser_find_local(parser_t *parser, const std::string &name, size_t upto, bool *isparam) {
152 return parser_find_local(parser, name.c_str(), upto, isparam);
155 static ast_expression* parser_find_var(parser_t *parser, const char *name)
159 v = parser_find_local(parser, name, 0, &dummy);
160 if (!v) v = parser_find_global(parser, name);
164 static inline ast_expression* parser_find_var(parser_t *parser, const std::string &name) {
165 return parser_find_var(parser, name.c_str());
168 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
172 hash = util_hthash(parser->typedefs[0], name);
174 for (i = parser->typedefs.size(); i > upto;) {
176 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
182 static ast_value* parser_find_typedef(parser_t *parser, const std::string &name, size_t upto) {
183 return parser_find_typedef(parser, name.c_str(), upto);
187 size_t etype; /* 0 = expression, others are operators */
191 ast_block *block; /* for commas and function calls */
204 std::vector<sy_elem> out;
205 std::vector<sy_elem> ops;
206 std::vector<size_t> argc;
207 std::vector<unsigned int> paren;
210 static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
221 static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
232 static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
234 e.etype = 1 + (op - operators);
243 static sy_elem syparen(lex_ctx_t ctx, size_t off) {
254 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
255 * so we need to rotate it to become ent.(foo[n]).
257 static bool rotate_entfield_array_index_nodes(ast_expression **out)
259 ast_array_index *index, *oldindex;
260 ast_entfield *entfield;
264 ast_expression *entity;
266 lex_ctx_t ctx = (*out)->m_context;
268 if (!ast_istype(*out, ast_array_index))
270 index = (ast_array_index*)*out;
272 if (!ast_istype(index->m_array, ast_entfield))
274 entfield = (ast_entfield*)index->m_array;
276 if (!ast_istype(entfield->m_field, ast_value))
278 field = (ast_value*)entfield->m_field;
280 sub = index->m_index;
281 entity = entfield->m_entity;
285 index = ast_array_index::make(ctx, field, sub);
286 entfield = new ast_entfield(ctx, entity, index);
289 oldindex->m_array = nullptr;
290 oldindex->m_index = nullptr;
296 static int store_op_for(ast_expression* expr)
298 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) && expr->m_vtype == TYPE_FIELD && expr->m_next->m_vtype == TYPE_VECTOR) {
299 return type_storep_instr[TYPE_VECTOR];
302 if (ast_istype(expr, ast_member) && ast_istype(((ast_member*)expr)->m_owner, ast_entfield)) {
303 return type_storep_instr[expr->m_vtype];
306 if (ast_istype(expr, ast_entfield)) {
307 return type_storep_instr[expr->m_vtype];
310 return type_store_instr[expr->m_vtype];
313 static bool check_write_to(lex_ctx_t ctx, ast_expression *expr)
315 if (ast_istype(expr, ast_value)) {
316 ast_value *val = (ast_value*)expr;
317 if (val->m_cvq == CV_CONST) {
318 if (val->m_name[0] == '#') {
319 compile_error(ctx, "invalid assignment to a literal constant");
323 * To work around quakeworld we must elide the error and make it
326 if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC)
327 compile_error(ctx, "assignment to constant `%s`", val->m_name);
329 (void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->m_name);
336 static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
340 ast_expression *out = nullptr;
341 ast_expression *exprs[3];
342 ast_block *blocks[3];
343 ast_binstore *asbinstore;
344 size_t i, assignop, addop, subop;
345 qcint_t generated_op = 0;
350 if (sy->ops.empty()) {
351 parseerror(parser, "internal error: missing operator");
355 if (sy->ops.back().isparen) {
356 parseerror(parser, "unmatched parenthesis");
360 op = &operators[sy->ops.back().etype - 1];
361 ctx = sy->ops.back().ctx;
363 if (sy->out.size() < op->operands) {
364 if (op->flags & OP_PREFIX)
365 compile_error(ctx, "expected expression after unary operator `%s`", op->op, (int)op->id);
366 else /* this should have errored previously already */
367 compile_error(ctx, "expected expression after operator `%s`", op->op, (int)op->id);
373 /* op(:?) has no input and no output */
377 sy->out.erase(sy->out.end() - op->operands, sy->out.end());
378 for (i = 0; i < op->operands; ++i) {
379 exprs[i] = sy->out[sy->out.size()+i].out;
380 blocks[i] = sy->out[sy->out.size()+i].block;
382 if (exprs[i]->m_vtype == TYPE_NOEXPR &&
383 !(i != 0 && op->id == opid2('?',':')) &&
384 !(i == 1 && op->id == opid1('.')))
386 if (ast_istype(exprs[i], ast_label))
387 compile_error(exprs[i]->m_context, "expected expression, got an unknown identifier");
389 compile_error(exprs[i]->m_context, "not an expression");
390 (void)!compile_warning(exprs[i]->m_context, WARN_DEBUG, "expression %u\n", (unsigned int)i);
394 if (blocks[0] && blocks[0]->m_exprs.empty() && op->id != opid1(',')) {
395 compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
399 #define NotSameType(T) \
400 (exprs[0]->m_vtype != exprs[1]->m_vtype || \
401 exprs[0]->m_vtype != T)
406 compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
410 if (exprs[0]->m_vtype == TYPE_VECTOR &&
411 exprs[1]->m_vtype == TYPE_NOEXPR)
413 if (exprs[1] == parser->const_vec[0])
414 out = ast_member::make(ctx, exprs[0], 0, "");
415 else if (exprs[1] == parser->const_vec[1])
416 out = ast_member::make(ctx, exprs[0], 1, "");
417 else if (exprs[1] == parser->const_vec[2])
418 out = ast_member::make(ctx, exprs[0], 2, "");
420 compile_error(ctx, "access to invalid vector component");
424 else if (exprs[0]->m_vtype == TYPE_ENTITY) {
425 if (exprs[1]->m_vtype != TYPE_FIELD) {
426 compile_error(exprs[1]->m_context, "type error: right hand of member-operand should be an entity-field");
429 out = new ast_entfield(ctx, exprs[0], exprs[1]);
431 else if (exprs[0]->m_vtype == TYPE_VECTOR) {
432 compile_error(exprs[1]->m_context, "vectors cannot be accessed this way");
436 compile_error(exprs[1]->m_context, "type error: member-of operator on something that is not an entity or vector");
442 if (exprs[0]->m_vtype != TYPE_ARRAY &&
443 !(exprs[0]->m_vtype == TYPE_FIELD &&
444 exprs[0]->m_next->m_vtype == TYPE_ARRAY))
446 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
447 compile_error(exprs[0]->m_context, "cannot index value of type %s", ty1);
450 if (exprs[1]->m_vtype != TYPE_FLOAT) {
451 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
452 compile_error(exprs[1]->m_context, "index must be of type float, not %s", ty1);
455 out = ast_array_index::make(ctx, exprs[0], exprs[1]);
456 rotate_entfield_array_index_nodes(&out);
460 if (sy->paren.size() && sy->paren.back() == PAREN_FUNC) {
461 sy->out.push_back(syexp(ctx, exprs[0]));
462 sy->out.push_back(syexp(ctx, exprs[1]));
467 if (!blocks[0]->addExpr(exprs[1]))
470 blocks[0] = new ast_block(ctx);
471 if (!blocks[0]->addExpr(exprs[0]) ||
472 !blocks[0]->addExpr(exprs[1]))
477 blocks[0]->setType(*exprs[1]);
479 sy->out.push_back(syblock(ctx, blocks[0]));
486 if ((out = parser->m_fold.op(op, exprs)))
489 if (exprs[0]->m_vtype != TYPE_FLOAT &&
490 exprs[0]->m_vtype != TYPE_VECTOR) {
491 compile_error(ctx, "invalid types used in unary expression: cannot negate type %s",
492 type_name[exprs[0]->m_vtype]);
495 if (exprs[0]->m_vtype == TYPE_FLOAT)
496 out = ast_unary::make(ctx, VINSTR_NEG_F, exprs[0]);
498 out = ast_unary::make(ctx, VINSTR_NEG_V, exprs[0]);
502 if (!(out = parser->m_fold.op(op, exprs))) {
503 switch (exprs[0]->m_vtype) {
505 out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
508 out = ast_unary::make(ctx, INSTR_NOT_V, exprs[0]);
511 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
512 out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
514 out = ast_unary::make(ctx, INSTR_NOT_S, exprs[0]);
516 /* we don't constant-fold NOT for these types */
518 out = ast_unary::make(ctx, INSTR_NOT_ENT, exprs[0]);
521 out = ast_unary::make(ctx, INSTR_NOT_FNC, exprs[0]);
524 compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
525 type_name[exprs[0]->m_vtype]);
532 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
533 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
535 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
536 type_name[exprs[0]->m_vtype],
537 type_name[exprs[1]->m_vtype]);
540 if (!(out = parser->m_fold.op(op, exprs))) {
541 switch (exprs[0]->m_vtype) {
543 out = fold::binary(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
546 out = fold::binary(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
549 compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
550 type_name[exprs[0]->m_vtype],
551 type_name[exprs[1]->m_vtype]);
557 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
558 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT))
560 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
561 type_name[exprs[1]->m_vtype],
562 type_name[exprs[0]->m_vtype]);
565 if (!(out = parser->m_fold.op(op, exprs))) {
566 switch (exprs[0]->m_vtype) {
568 out = fold::binary(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
571 out = fold::binary(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
574 compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
575 type_name[exprs[1]->m_vtype],
576 type_name[exprs[0]->m_vtype]);
582 if (exprs[0]->m_vtype != exprs[1]->m_vtype &&
583 !(exprs[0]->m_vtype == TYPE_VECTOR &&
584 exprs[1]->m_vtype == TYPE_FLOAT) &&
585 !(exprs[1]->m_vtype == TYPE_VECTOR &&
586 exprs[0]->m_vtype == TYPE_FLOAT)
589 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
590 type_name[exprs[1]->m_vtype],
591 type_name[exprs[0]->m_vtype]);
594 if (!(out = parser->m_fold.op(op, exprs))) {
595 switch (exprs[0]->m_vtype) {
597 if (exprs[1]->m_vtype == TYPE_VECTOR)
598 out = fold::binary(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
600 out = fold::binary(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
603 if (exprs[1]->m_vtype == TYPE_FLOAT)
604 out = fold::binary(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
606 out = fold::binary(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
609 compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
610 type_name[exprs[1]->m_vtype],
611 type_name[exprs[0]->m_vtype]);
618 if (exprs[1]->m_vtype != TYPE_FLOAT) {
619 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
620 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
621 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
624 if (!(out = parser->m_fold.op(op, exprs))) {
625 if (exprs[0]->m_vtype == TYPE_FLOAT)
626 out = fold::binary(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
628 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
629 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
630 compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
637 if (NotSameType(TYPE_FLOAT)) {
638 compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
639 type_name[exprs[0]->m_vtype],
640 type_name[exprs[1]->m_vtype]);
642 } else if (!(out = parser->m_fold.op(op, exprs))) {
643 /* generate a call to __builtin_mod */
644 ast_expression *mod = parser->m_intrin.func("mod");
645 ast_call *call = nullptr;
646 if (!mod) return false; /* can return null for missing floor */
648 call = ast_call::make(parser_ctx(parser), mod);
649 call->m_params.push_back(exprs[0]);
650 call->m_params.push_back(exprs[1]);
657 compile_error(ctx, "%= is unimplemented");
663 if ( !(exprs[0]->m_vtype == TYPE_FLOAT && exprs[1]->m_vtype == TYPE_FLOAT) &&
664 !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_FLOAT) &&
665 !(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_VECTOR))
667 compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
668 type_name[exprs[0]->m_vtype],
669 type_name[exprs[1]->m_vtype]);
673 if (!(out = parser->m_fold.op(op, exprs))) {
675 * IF the first expression is float, the following will be too
676 * since scalar ^ vector is not allowed.
678 if (exprs[0]->m_vtype == TYPE_FLOAT) {
679 out = fold::binary(ctx,
680 (op->id == opid1('^') ? VINSTR_BITXOR : op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
684 * The first is a vector: vector is allowed to bitop with vector and
685 * with scalar, branch here for the second operand.
687 if (exprs[1]->m_vtype == TYPE_VECTOR) {
689 * Bitop all the values of the vector components against the
690 * vectors components in question.
692 out = fold::binary(ctx,
693 (op->id == opid1('^') ? VINSTR_BITXOR_V : op->id == opid1('|') ? VINSTR_BITOR_V : VINSTR_BITAND_V),
696 out = fold::binary(ctx,
697 (op->id == opid1('^') ? VINSTR_BITXOR_VF : op->id == opid1('|') ? VINSTR_BITOR_VF : VINSTR_BITAND_VF),
706 if (NotSameType(TYPE_FLOAT)) {
707 compile_error(ctx, "invalid types used in expression: cannot perform shift between types %s and %s",
708 type_name[exprs[0]->m_vtype],
709 type_name[exprs[1]->m_vtype]);
713 if (!(out = parser->m_fold.op(op, exprs))) {
714 ast_expression *shift = parser->m_intrin.func((op->id == opid2('<','<')) ? "__builtin_lshift" : "__builtin_rshift");
715 ast_call *call = ast_call::make(parser_ctx(parser), shift);
716 call->m_params.push_back(exprs[0]);
717 call->m_params.push_back(exprs[1]);
722 case opid3('<','<','='):
723 case opid3('>','>','='):
724 if (NotSameType(TYPE_FLOAT)) {
725 compile_error(ctx, "invalid types used in expression: cannot perform shift operation between types %s and %s",
726 type_name[exprs[0]->m_vtype],
727 type_name[exprs[1]->m_vtype]);
731 if(!(out = parser->m_fold.op(op, exprs))) {
732 ast_expression *shift = parser->m_intrin.func((op->id == opid3('<','<','=')) ? "__builtin_lshift" : "__builtin_rshift");
733 ast_call *call = ast_call::make(parser_ctx(parser), shift);
734 call->m_params.push_back(exprs[0]);
735 call->m_params.push_back(exprs[1]);
747 generated_op += 1; /* INSTR_OR */
750 generated_op += INSTR_AND;
751 if (!(out = parser->m_fold.op(op, exprs))) {
752 if (OPTS_FLAG(PERL_LOGIC) && !exprs[0]->compareType(*exprs[1])) {
753 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
754 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
755 compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
758 for (i = 0; i < 2; ++i) {
759 if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->m_vtype == TYPE_VECTOR) {
760 out = ast_unary::make(ctx, INSTR_NOT_V, exprs[i]);
762 out = ast_unary::make(ctx, INSTR_NOT_F, out);
764 exprs[i] = out; out = nullptr;
765 if (OPTS_FLAG(PERL_LOGIC)) {
766 /* here we want to keep the right expressions' type */
770 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->m_vtype == TYPE_STRING) {
771 out = ast_unary::make(ctx, INSTR_NOT_S, exprs[i]);
773 out = ast_unary::make(ctx, INSTR_NOT_F, out);
775 exprs[i] = out; out = nullptr;
776 if (OPTS_FLAG(PERL_LOGIC)) {
777 /* here we want to keep the right expressions' type */
782 out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
787 if (sy->paren.back() != PAREN_TERNARY2) {
788 compile_error(ctx, "mismatched parenthesis/ternary");
791 sy->paren.pop_back();
792 if (!exprs[1]->compareType(*exprs[2])) {
793 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
794 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
795 compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
798 if (!(out = parser->m_fold.op(op, exprs)))
799 out = new ast_ternary(ctx, exprs[0], exprs[1], exprs[2]);
802 case opid2('*', '*'):
803 if (NotSameType(TYPE_FLOAT)) {
804 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
805 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
806 compile_error(ctx, "invalid types used in exponentiation: %s and %s",
811 if (!(out = parser->m_fold.op(op, exprs))) {
812 ast_call *gencall = ast_call::make(parser_ctx(parser), parser->m_intrin.func("pow"));
813 gencall->m_params.push_back(exprs[0]);
814 gencall->m_params.push_back(exprs[1]);
819 case opid2('>', '<'):
820 if (NotSameType(TYPE_VECTOR)) {
821 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
822 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
823 compile_error(ctx, "invalid types used in cross product: %s and %s",
828 if (!(out = parser->m_fold.op(op, exprs))) {
839 case opid3('<','=','>'): /* -1, 0, or 1 */
840 if (NotSameType(TYPE_FLOAT)) {
841 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
842 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
843 compile_error(ctx, "invalid types used in comparision: %s and %s",
849 if (!(out = parser->m_fold.op(op, exprs))) {
850 /* This whole block is NOT fold_binary safe */
851 ast_binary *eq = new ast_binary(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
853 eq->m_refs = AST_REF_NONE;
856 out = new ast_ternary(ctx,
857 new ast_binary(ctx, INSTR_LT, exprs[0], exprs[1]),
859 parser->m_fold.imm_float(2),
862 new ast_ternary(ctx, eq,
864 parser->m_fold.imm_float(0),
867 parser->m_fold.imm_float(1)
877 generated_op += 1; /* INSTR_GT */
880 generated_op += 1; /* INSTR_LT */
882 case opid2('>', '='):
883 generated_op += 1; /* INSTR_GE */
885 case opid2('<', '='):
886 generated_op += INSTR_LE;
887 if (NotSameType(TYPE_FLOAT)) {
888 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
889 type_name[exprs[0]->m_vtype],
890 type_name[exprs[1]->m_vtype]);
893 if (!(out = parser->m_fold.op(op, exprs)))
894 out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
896 case opid2('!', '='):
897 if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
898 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
899 type_name[exprs[0]->m_vtype],
900 type_name[exprs[1]->m_vtype]);
903 if (!(out = parser->m_fold.op(op, exprs)))
904 out = fold::binary(ctx, type_ne_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
906 case opid2('=', '='):
907 if (exprs[0]->m_vtype != exprs[1]->m_vtype) {
908 compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
909 type_name[exprs[0]->m_vtype],
910 type_name[exprs[1]->m_vtype]);
913 if (!(out = parser->m_fold.op(op, exprs)))
914 out = fold::binary(ctx, type_eq_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
918 if (ast_istype(exprs[0], ast_entfield)) {
919 ast_expression *field = ((ast_entfield*)exprs[0])->m_field;
920 assignop = store_op_for(exprs[0]);
921 if (assignop == VINSTR_END || !field->m_next->compareType(*exprs[1]))
923 ast_type_to_string(field->m_next, ty1, sizeof(ty1));
924 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
925 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
926 field->m_next->m_vtype == TYPE_FUNCTION &&
927 exprs[1]->m_vtype == TYPE_FUNCTION)
929 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
930 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
933 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
938 assignop = store_op_for(exprs[0]);
940 if (assignop == VINSTR_END) {
941 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
942 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
943 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
945 else if (!exprs[0]->compareType(*exprs[1]))
947 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
948 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
949 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
950 exprs[0]->m_vtype == TYPE_FUNCTION &&
951 exprs[1]->m_vtype == TYPE_FUNCTION)
953 (void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
954 "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
957 compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
960 (void)check_write_to(ctx, exprs[0]);
961 /* When we're a vector of part of an entity field we use STOREP */
962 if (ast_istype(exprs[0], ast_member) && ast_istype(((ast_member*)exprs[0])->m_owner, ast_entfield))
963 assignop = INSTR_STOREP_F;
964 out = new ast_store(ctx, assignop, exprs[0], exprs[1]);
966 case opid3('+','+','P'):
967 case opid3('-','-','P'):
969 if (exprs[0]->m_vtype != TYPE_FLOAT) {
970 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
971 compile_error(exprs[0]->m_context, "invalid type for prefix increment: %s", ty1);
974 if (op->id == opid3('+','+','P'))
978 (void)check_write_to(exprs[0]->m_context, exprs[0]);
979 if (ast_istype(exprs[0], ast_entfield)) {
980 out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
982 parser->m_fold.imm_float(1));
984 out = new ast_binstore(ctx, INSTR_STORE_F, addop,
986 parser->m_fold.imm_float(1));
989 case opid3('S','+','+'):
990 case opid3('S','-','-'):
992 if (exprs[0]->m_vtype != TYPE_FLOAT) {
993 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
994 compile_error(exprs[0]->m_context, "invalid type for suffix increment: %s", ty1);
997 if (op->id == opid3('S','+','+')) {
1001 addop = INSTR_SUB_F;
1002 subop = INSTR_ADD_F;
1004 (void)check_write_to(exprs[0]->m_context, exprs[0]);
1005 if (ast_istype(exprs[0], ast_entfield)) {
1006 out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
1008 parser->m_fold.imm_float(1));
1010 out = new ast_binstore(ctx, INSTR_STORE_F, addop,
1012 parser->m_fold.imm_float(1));
1016 out = fold::binary(ctx, subop,
1018 parser->m_fold.imm_float(1));
1021 case opid2('+','='):
1022 case opid2('-','='):
1023 if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
1024 (exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
1026 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1027 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1028 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1032 (void)check_write_to(ctx, exprs[0]);
1033 assignop = store_op_for(exprs[0]);
1034 switch (exprs[0]->m_vtype) {
1036 out = new ast_binstore(ctx, assignop,
1037 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1038 exprs[0], exprs[1]);
1041 out = new ast_binstore(ctx, assignop,
1042 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1043 exprs[0], exprs[1]);
1046 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1047 type_name[exprs[0]->m_vtype],
1048 type_name[exprs[1]->m_vtype]);
1052 case opid2('*','='):
1053 case opid2('/','='):
1054 if (exprs[1]->m_vtype != TYPE_FLOAT ||
1055 !(exprs[0]->m_vtype == TYPE_FLOAT ||
1056 exprs[0]->m_vtype == TYPE_VECTOR))
1058 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1059 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1060 compile_error(ctx, "invalid types used in expression: %s and %s",
1064 (void)check_write_to(ctx, exprs[0]);
1065 assignop = store_op_for(exprs[0]);
1066 switch (exprs[0]->m_vtype) {
1068 out = new ast_binstore(ctx, assignop,
1069 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1070 exprs[0], exprs[1]);
1073 if (op->id == opid2('*','=')) {
1074 out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
1075 exprs[0], exprs[1]);
1077 out = fold::binary(ctx, INSTR_DIV_F,
1078 parser->m_fold.imm_float(1),
1081 compile_error(ctx, "internal error: failed to generate division");
1084 out = new ast_binstore(ctx, assignop, INSTR_MUL_VF,
1089 compile_error(ctx, "invalid types used in expression: cannot add or subtract type %s and %s",
1090 type_name[exprs[0]->m_vtype],
1091 type_name[exprs[1]->m_vtype]);
1095 case opid2('&','='):
1096 case opid2('|','='):
1097 case opid2('^','='):
1098 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1099 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1100 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1101 compile_error(ctx, "invalid types used in expression: %s and %s",
1105 (void)check_write_to(ctx, exprs[0]);
1106 assignop = store_op_for(exprs[0]);
1107 if (exprs[0]->m_vtype == TYPE_FLOAT)
1108 out = new ast_binstore(ctx, assignop,
1109 (op->id == opid2('^','=') ? VINSTR_BITXOR : op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1110 exprs[0], exprs[1]);
1112 out = new ast_binstore(ctx, assignop,
1113 (op->id == opid2('^','=') ? VINSTR_BITXOR_V : op->id == opid2('&','=') ? VINSTR_BITAND_V : VINSTR_BITOR_V),
1114 exprs[0], exprs[1]);
1116 case opid3('&','~','='):
1117 /* This is like: a &= ~(b);
1118 * But QC has no bitwise-not, so we implement it as
1121 if (NotSameType(TYPE_FLOAT) && NotSameType(TYPE_VECTOR)) {
1122 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1123 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1124 compile_error(ctx, "invalid types used in expression: %s and %s",
1128 assignop = store_op_for(exprs[0]);
1129 if (exprs[0]->m_vtype == TYPE_FLOAT)
1130 out = fold::binary(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1132 out = fold::binary(ctx, VINSTR_BITAND_V, exprs[0], exprs[1]);
1135 (void)check_write_to(ctx, exprs[0]);
1136 if (exprs[0]->m_vtype == TYPE_FLOAT)
1137 asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1139 asbinstore = new ast_binstore(ctx, assignop, INSTR_SUB_V, exprs[0], out);
1140 asbinstore->m_keep_dest = true;
1144 case opid3('l', 'e', 'n'):
1145 if (exprs[0]->m_vtype != TYPE_STRING && exprs[0]->m_vtype != TYPE_ARRAY) {
1146 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1147 compile_error(exprs[0]->m_context, "invalid type for length operator: %s", ty1);
1150 /* strings must be const, arrays are statically sized */
1151 if (exprs[0]->m_vtype == TYPE_STRING &&
1152 !(((ast_value*)exprs[0])->m_hasvalue && ((ast_value*)exprs[0])->m_cvq == CV_CONST))
1154 compile_error(exprs[0]->m_context, "operand of length operator not a valid constant expression");
1157 out = parser->m_fold.op(op, exprs);
1160 case opid2('~', 'P'):
1161 if (exprs[0]->m_vtype != TYPE_FLOAT && exprs[0]->m_vtype != TYPE_VECTOR) {
1162 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1163 compile_error(exprs[0]->m_context, "invalid type for bit not: %s", ty1);
1166 if (!(out = parser->m_fold.op(op, exprs))) {
1167 if (exprs[0]->m_vtype == TYPE_FLOAT) {
1168 out = fold::binary(ctx, INSTR_SUB_F, parser->m_fold.imm_float(2), exprs[0]);
1170 out = fold::binary(ctx, INSTR_SUB_V, parser->m_fold.imm_vector(1), exprs[0]);
1177 compile_error(ctx, "failed to apply operator %s", op->op);
1181 sy->out.push_back(syexp(ctx, out));
1185 static bool parser_close_call(parser_t *parser, shunt *sy)
1187 /* was a function call */
1188 ast_expression *fun;
1189 ast_value *funval = nullptr;
1193 size_t paramcount, i;
1196 fid = sy->ops.back().off;
1199 /* out[fid] is the function
1200 * everything above is parameters...
1202 if (sy->argc.empty()) {
1203 parseerror(parser, "internal error: no argument counter available");
1207 paramcount = sy->argc.back();
1208 sy->argc.pop_back();
1210 if (sy->out.size() < fid) {
1211 parseerror(parser, "internal error: broken function call %zu < %zu+%zu\n",
1219 * TODO handle this at the intrinsic level with an ast_intrinsic
1222 if ((fun = sy->out[fid].out) == parser->m_intrin.debug_typestring()) {
1224 if (fid+2 != sy->out.size() || sy->out.back().block) {
1225 parseerror(parser, "intrinsic __builtin_debug_typestring requires exactly 1 parameter");
1228 ast_type_to_string(sy->out.back().out, ty, sizeof(ty));
1229 ast_unref(sy->out.back().out);
1230 sy->out[fid] = syexp(sy->out.back().out->m_context,
1231 parser->m_fold.constgen_string(ty, false));
1237 * Now we need to determine if the function that is being called is
1238 * an intrinsic so we can evaluate if the arguments to it are constant
1239 * and than fruitfully fold them.
1241 #define fold_can_1(X) \
1242 (ast_istype(((X)), ast_value) && (X)->m_hasvalue && ((X)->m_cvq == CV_CONST) && \
1243 ((X))->m_vtype != TYPE_FUNCTION)
1245 if (fid + 1 < sy->out.size())
1248 for (i = 0; i < paramcount; ++i) {
1249 if (!fold_can_1((ast_value*)sy->out[fid + 1 + i].out)) {
1256 * All is well which ends well, if we make it into here we can ignore the
1257 * intrinsic call and just evaluate it i.e constant fold it.
1259 if (fold && ast_istype(fun, ast_value) && ((ast_value*)fun)->m_intrinsic) {
1260 std::vector<ast_expression*> exprs;
1261 ast_expression *foldval = nullptr;
1263 exprs.reserve(paramcount);
1264 for (i = 0; i < paramcount; i++)
1265 exprs.push_back(sy->out[fid+1 + i].out);
1267 if (!(foldval = parser->m_intrin.do_fold((ast_value*)fun, exprs.data()))) {
1272 * Blub: what sorts of unreffing and resizing of
1273 * sy->out should I be doing here?
1275 sy->out[fid] = syexp(foldval->m_context, foldval);
1276 sy->out.erase(sy->out.end() - paramcount, sy->out.end());
1282 call = ast_call::make(sy->ops[sy->ops.size()].ctx, fun);
1287 if (fid+1 + paramcount != sy->out.size()) {
1288 parseerror(parser, "internal error: parameter count mismatch: (%zu+1+%zu), %zu",
1295 for (i = 0; i < paramcount; ++i)
1296 call->m_params.push_back(sy->out[fid+1 + i].out);
1297 sy->out.erase(sy->out.end() - paramcount, sy->out.end());
1298 (void)!call->checkTypes(parser->function->m_function_type->m_varparam);
1299 if (parser->max_param_count < paramcount)
1300 parser->max_param_count = paramcount;
1302 if (ast_istype(fun, ast_value)) {
1303 funval = (ast_value*)fun;
1304 if ((fun->m_flags & AST_FLAG_VARIADIC) &&
1305 !(/*funval->m_cvq == CV_CONST && */ funval->m_hasvalue && funval->m_constval.vfunc->m_builtin))
1307 call->m_va_count = parser->m_fold.constgen_float((qcfloat_t)paramcount, false);
1311 /* overwrite fid, the function, with a call */
1312 sy->out[fid] = syexp(call->m_context, call);
1314 if (fun->m_vtype != TYPE_FUNCTION) {
1315 parseerror(parser, "not a function (%s)", type_name[fun->m_vtype]);
1320 parseerror(parser, "could not determine function return type");
1323 ast_value *fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : nullptr);
1325 if (fun->m_flags & AST_FLAG_DEPRECATED) {
1327 return !parsewarning(parser, WARN_DEPRECATED,
1328 "call to function (which is marked deprecated)\n",
1329 "-> it has been declared here: %s:%i",
1330 fun->m_context.file, fun->m_context.line);
1332 if (!fval->m_desc.length()) {
1333 return !parsewarning(parser, WARN_DEPRECATED,
1334 "call to `%s` (which is marked deprecated)\n"
1335 "-> `%s` declared here: %s:%i",
1336 fval->m_name, fval->m_name, fun->m_context.file, fun->m_context.line);
1338 return !parsewarning(parser, WARN_DEPRECATED,
1339 "call to `%s` (deprecated: %s)\n"
1340 "-> `%s` declared here: %s:%i",
1341 fval->m_name, fval->m_desc, fval->m_name, fun->m_context.file,
1342 fun->m_context.line);
1345 if (fun->m_type_params.size() != paramcount &&
1346 !((fun->m_flags & AST_FLAG_VARIADIC) &&
1347 fun->m_type_params.size() < paramcount))
1349 const char *fewmany = (fun->m_type_params.size() > paramcount) ? "few" : "many";
1351 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1352 "too %s parameters for call to %s: expected %i, got %i\n"
1353 " -> `%s` has been declared here: %s:%i",
1354 fewmany, fval->m_name, (int)fun->m_type_params.size(), (int)paramcount,
1355 fval->m_name, fun->m_context.file, (int)fun->m_context.line);
1357 return !parsewarning(parser, WARN_INVALID_PARAMETER_COUNT,
1358 "too %s parameters for function call: expected %i, got %i\n"
1359 " -> it has been declared here: %s:%i",
1360 fewmany, (int)fun->m_type_params.size(), (int)paramcount,
1361 fun->m_context.file, (int)fun->m_context.line);
1368 static bool parser_close_paren(parser_t *parser, shunt *sy)
1370 if (sy->ops.empty()) {
1371 parseerror(parser, "unmatched closing paren");
1375 while (sy->ops.size()) {
1376 if (sy->ops.back().isparen) {
1377 if (sy->paren.back() == PAREN_FUNC) {
1378 sy->paren.pop_back();
1379 if (!parser_close_call(parser, sy))
1383 if (sy->paren.back() == PAREN_EXPR) {
1384 sy->paren.pop_back();
1385 if (sy->out.empty()) {
1386 compile_error(sy->ops.back().ctx, "empty paren expression");
1393 if (sy->paren.back() == PAREN_INDEX) {
1394 sy->paren.pop_back();
1395 // pop off the parenthesis
1397 /* then apply the index operator */
1398 if (!parser_sy_apply_operator(parser, sy))
1402 if (sy->paren.back() == PAREN_TERNARY1) {
1403 sy->paren.back() = PAREN_TERNARY2;
1404 // pop off the parenthesis
1408 compile_error(sy->ops.back().ctx, "invalid parenthesis");
1411 if (!parser_sy_apply_operator(parser, sy))
1417 static void parser_reclassify_token(parser_t *parser)
1420 if (parser->tok >= TOKEN_START)
1422 for (i = 0; i < operator_count; ++i) {
1423 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1424 parser->tok = TOKEN_OPERATOR;
1430 static ast_expression* parse_vararg_do(parser_t *parser)
1432 ast_expression *idx, *out;
1434 ast_value *funtype = parser->function->m_function_type;
1435 lex_ctx_t ctx = parser_ctx(parser);
1437 if (!parser->function->m_varargs) {
1438 parseerror(parser, "function has no variable argument list");
1442 if (!parser_next(parser) || parser->tok != '(') {
1443 parseerror(parser, "expected parameter index and type in parenthesis");
1446 if (!parser_next(parser)) {
1447 parseerror(parser, "error parsing parameter index");
1451 idx = parse_expression_leave(parser, true, false, false);
1455 if (parser->tok != ',') {
1456 if (parser->tok != ')') {
1458 parseerror(parser, "expected comma after parameter index");
1461 // vararg piping: ...(start)
1462 out = new ast_argpipe(ctx, idx);
1466 if (!parser_next(parser) || (parser->tok != TOKEN_IDENT && parser->tok != TOKEN_TYPENAME)) {
1468 parseerror(parser, "expected typename for vararg");
1472 typevar = parse_typename(parser, nullptr, nullptr, nullptr);
1478 if (parser->tok != ')') {
1481 parseerror(parser, "expected closing paren");
1485 if (funtype->m_varparam &&
1486 !typevar->compareType(*funtype->m_varparam))
1490 ast_type_to_string(typevar, ty1, sizeof(ty1));
1491 ast_type_to_string(funtype->m_varparam, ty2, sizeof(ty2));
1492 compile_error(typevar->m_context,
1493 "function was declared to take varargs of type `%s`, requested type is: %s",
1497 out = ast_array_index::make(ctx, parser->function->m_varargs.get(), idx);
1498 out->adoptType(*typevar);
1503 static ast_expression* parse_vararg(parser_t *parser)
1505 bool old_noops = parser->lex->flags.noops;
1507 ast_expression *out;
1509 parser->lex->flags.noops = true;
1510 out = parse_vararg_do(parser);
1512 parser->lex->flags.noops = old_noops;
1516 /* not to be exposed */
1517 bool ftepp_predef_exists(const char *name);
1518 static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
1520 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1521 parser->tok == TOKEN_IDENT &&
1522 !strcmp(parser_tokval(parser), "_"))
1524 /* a translatable string */
1527 parser->lex->flags.noops = true;
1528 if (!parser_next(parser) || parser->tok != '(') {
1529 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1532 parser->lex->flags.noops = false;
1533 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1534 parseerror(parser, "expected a constant string in translatable-string extension");
1537 val = (ast_value*)parser->m_fold.constgen_string(parser_tokval(parser), true);
1540 sy->out.push_back(syexp(parser_ctx(parser), val));
1542 if (!parser_next(parser) || parser->tok != ')') {
1543 parseerror(parser, "expected closing paren after translatable string");
1548 else if (parser->tok == TOKEN_DOTS)
1551 if (!OPTS_FLAG(VARIADIC_ARGS)) {
1552 parseerror(parser, "cannot access varargs (try -fvariadic-args)");
1555 va = parse_vararg(parser);
1558 sy->out.push_back(syexp(parser_ctx(parser), va));
1561 else if (parser->tok == TOKEN_FLOATCONST) {
1562 ast_expression *val = parser->m_fold.constgen_float((parser_token(parser)->constval.f), false);
1565 sy->out.push_back(syexp(parser_ctx(parser), val));
1568 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1569 ast_expression *val = parser->m_fold.constgen_float((qcfloat_t)(parser_token(parser)->constval.i), false);
1572 sy->out.push_back(syexp(parser_ctx(parser), val));
1575 else if (parser->tok == TOKEN_STRINGCONST) {
1576 ast_expression *val = parser->m_fold.constgen_string(parser_tokval(parser), false);
1579 sy->out.push_back(syexp(parser_ctx(parser), val));
1582 else if (parser->tok == TOKEN_VECTORCONST) {
1583 ast_expression *val = parser->m_fold.constgen_vector(parser_token(parser)->constval.v);
1586 sy->out.push_back(syexp(parser_ctx(parser), val));
1589 else if (parser->tok == TOKEN_IDENT)
1591 const char *ctoken = parser_tokval(parser);
1592 ast_expression *prev = sy->out.size() ? sy->out.back().out : nullptr;
1593 ast_expression *var;
1594 /* a_vector.{x,y,z} */
1595 if (sy->ops.empty() ||
1596 !sy->ops.back().etype ||
1597 operators[sy->ops.back().etype-1].id != opid1('.'))
1599 /* When adding more intrinsics, fix the above condition */
1602 if (prev && prev->m_vtype == TYPE_VECTOR && ctoken[0] >= 'x' && ctoken[0] <= 'z' && !ctoken[1])
1604 var = parser->const_vec[ctoken[0]-'x'];
1606 var = parser_find_var(parser, parser_tokval(parser));
1608 var = parser_find_field(parser, parser_tokval(parser));
1610 if (!var && with_labels) {
1611 var = parser_find_label(parser, parser_tokval(parser));
1613 ast_label *lbl = new ast_label(parser_ctx(parser), parser_tokval(parser), true);
1615 parser->labels.push_back(lbl);
1618 if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
1619 var = parser->m_fold.constgen_string(parser->function->m_name, false);
1622 * now we try for the real intrinsic hashtable. If the string
1623 * begins with __builtin, we simply skip past it, otherwise we
1624 * use the identifier as is.
1626 if (!strncmp(parser_tokval(parser), "__builtin_", 10)) {
1627 var = parser->m_intrin.func(parser_tokval(parser));
1631 * Try it again, intrin_func deals with the alias method as well
1632 * the first one masks for __builtin though, we emit warning here.
1635 if ((var = parser->m_intrin.func(parser_tokval(parser)))) {
1636 (void)!compile_warning(
1639 "using implicitly defined builtin `__builtin_%s' for `%s'",
1640 parser_tokval(parser),
1641 parser_tokval(parser)
1649 * sometimes people use preprocessing predefs without enabling them
1650 * i've done this thousands of times already myself. Lets check for
1651 * it in the predef table. And diagnose it better :)
1653 if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
1654 parseerror(parser, "unexpected identifier: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
1658 parseerror(parser, "unexpected identifier: %s", parser_tokval(parser));
1664 // promote these to norefs
1665 if (ast_istype(var, ast_value))
1667 ((ast_value *)var)->m_flags |= AST_FLAG_NOREF;
1669 else if (ast_istype(var, ast_member))
1671 ast_member *mem = (ast_member *)var;
1672 if (ast_istype(mem->m_owner, ast_value))
1673 ((ast_value *)mem->m_owner)->m_flags |= AST_FLAG_NOREF;
1676 sy->out.push_back(syexp(parser_ctx(parser), var));
1679 parseerror(parser, "unexpected token `%s`", parser_tokval(parser));
1683 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels)
1685 ast_expression *expr = nullptr;
1687 bool wantop = false;
1688 /* only warn once about an assignment in a truth value because the current code
1689 * would trigger twice on: if(a = b && ...), once for the if-truth-value, once for the && part
1691 bool warn_parenthesis = true;
1693 /* count the parens because an if starts with one, so the
1694 * end of a condition is an unmatched closing paren
1698 memset(&sy, 0, sizeof(sy));
1700 parser->lex->flags.noops = false;
1702 parser_reclassify_token(parser);
1706 if (parser->tok == TOKEN_TYPENAME) {
1707 parseerror(parser, "unexpected typename `%s`", parser_tokval(parser));
1711 if (parser->tok == TOKEN_OPERATOR)
1713 /* classify the operator */
1714 const oper_info *op;
1715 const oper_info *olast = nullptr;
1717 for (o = 0; o < operator_count; ++o) {
1718 if (((!(operators[o].flags & OP_PREFIX) == !!wantop)) &&
1719 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1720 !strcmp(parser_tokval(parser), operators[o].op))
1725 if (o == operator_count) {
1726 compile_error(parser_ctx(parser), "unexpected operator: %s", parser_tokval(parser));
1729 /* found an operator */
1732 /* when declaring variables, a comma starts a new variable */
1733 if (op->id == opid1(',') && sy.paren.empty() && stopatcomma) {
1734 /* fixup the token */
1739 /* a colon without a pervious question mark cannot be a ternary */
1740 if (!ternaries && op->id == opid2(':','?')) {
1745 if (op->id == opid1(',')) {
1746 if (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1747 (void)!parsewarning(parser, WARN_TERNARY_PRECEDENCE, "suggesting parenthesis around ternary expression");
1751 if (sy.ops.size() && !sy.ops.back().isparen)
1752 olast = &operators[sy.ops.back().etype-1];
1754 /* first only apply higher precedences, assoc_left+equal comes after we warn about precedence rules */
1755 while (olast && op->prec < olast->prec)
1757 if (!parser_sy_apply_operator(parser, &sy))
1759 if (sy.ops.size() && !sy.ops.back().isparen)
1760 olast = &operators[sy.ops.back().etype-1];
1765 #define IsAssignOp(x) (\
1766 (x) == opid1('=') || \
1767 (x) == opid2('+','=') || \
1768 (x) == opid2('-','=') || \
1769 (x) == opid2('*','=') || \
1770 (x) == opid2('/','=') || \
1771 (x) == opid2('%','=') || \
1772 (x) == opid2('&','=') || \
1773 (x) == opid2('|','=') || \
1774 (x) == opid3('&','~','=') \
1776 if (warn_parenthesis) {
1777 if ( (olast && IsAssignOp(olast->id) && (op->id == opid2('&','&') || op->id == opid2('|','|'))) ||
1778 (olast && IsAssignOp(op->id) && (olast->id == opid2('&','&') || olast->id == opid2('|','|'))) ||
1779 (truthvalue && sy.paren.empty() && IsAssignOp(op->id))
1782 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around assignment used as truth value");
1783 warn_parenthesis = false;
1786 if (olast && olast->id != op->id) {
1787 if ((op->id == opid1('&') || op->id == opid1('|') || op->id == opid1('^')) &&
1788 (olast->id == opid1('&') || olast->id == opid1('|') || olast->id == opid1('^')))
1790 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around bitwise operations");
1791 warn_parenthesis = false;
1793 else if ((op->id == opid2('&','&') || op->id == opid2('|','|')) &&
1794 (olast->id == opid2('&','&') || olast->id == opid2('|','|')))
1796 (void)!parsewarning(parser, WARN_PARENTHESIS, "suggesting parenthesis around logical operations");
1797 warn_parenthesis = false;
1803 (op->prec < olast->prec) ||
1804 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1806 if (!parser_sy_apply_operator(parser, &sy))
1808 if (sy.ops.size() && !sy.ops.back().isparen)
1809 olast = &operators[sy.ops.back().etype-1];
1814 if (op->id == opid1('(')) {
1816 size_t sycount = sy.out.size();
1817 /* we expected an operator, this is the function-call operator */
1818 sy.paren.push_back(PAREN_FUNC);
1819 sy.ops.push_back(syparen(parser_ctx(parser), sycount-1));
1820 sy.argc.push_back(0);
1822 sy.paren.push_back(PAREN_EXPR);
1823 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1826 } else if (op->id == opid1('[')) {
1828 parseerror(parser, "unexpected array subscript");
1831 sy.paren.push_back(PAREN_INDEX);
1832 /* push both the operator and the paren, this makes life easier */
1833 sy.ops.push_back(syop(parser_ctx(parser), op));
1834 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1836 } else if (op->id == opid2('?',':')) {
1837 sy.ops.push_back(syop(parser_ctx(parser), op));
1838 sy.ops.push_back(syparen(parser_ctx(parser), 0));
1841 sy.paren.push_back(PAREN_TERNARY1);
1842 } else if (op->id == opid2(':','?')) {
1843 if (sy.paren.empty()) {
1844 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1847 if (sy.paren.back() != PAREN_TERNARY1) {
1848 parseerror(parser, "unexpected colon outside ternary expression (missing parenthesis?)");
1851 if (!parser_close_paren(parser, &sy))
1853 sy.ops.push_back(syop(parser_ctx(parser), op));
1857 sy.ops.push_back(syop(parser_ctx(parser), op));
1858 wantop = !!(op->flags & OP_SUFFIX);
1861 else if (parser->tok == ')') {
1862 while (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1863 if (!parser_sy_apply_operator(parser, &sy))
1866 if (sy.paren.empty())
1869 if (sy.paren.back() == PAREN_TERNARY1) {
1870 parseerror(parser, "mismatched parentheses (closing paren in ternary expression?)");
1873 if (!parser_close_paren(parser, &sy))
1876 /* must be a function call without parameters */
1877 if (sy.paren.back() != PAREN_FUNC) {
1878 parseerror(parser, "closing paren in invalid position");
1881 if (!parser_close_paren(parser, &sy))
1886 else if (parser->tok == '(') {
1887 parseerror(parser, "internal error: '(' should be classified as operator");
1890 else if (parser->tok == '[') {
1891 parseerror(parser, "internal error: '[' should be classified as operator");
1894 else if (parser->tok == ']') {
1895 while (sy.paren.size() && sy.paren.back() == PAREN_TERNARY2) {
1896 if (!parser_sy_apply_operator(parser, &sy))
1899 if (sy.paren.empty())
1901 if (sy.paren.back() != PAREN_INDEX) {
1902 parseerror(parser, "mismatched parentheses, unexpected ']'");
1905 if (!parser_close_paren(parser, &sy))
1910 if (!parse_sya_operand(parser, &sy, with_labels))
1915 /* in this case we might want to allow constant string concatenation */
1916 bool concatenated = false;
1917 if (parser->tok == TOKEN_STRINGCONST && sy.out.size()) {
1918 ast_expression *lexpr = sy.out.back().out;
1919 if (ast_istype(lexpr, ast_value)) {
1920 ast_value *last = (ast_value*)lexpr;
1921 if (last->m_isimm == true && last->m_cvq == CV_CONST &&
1922 last->m_hasvalue && last->m_vtype == TYPE_STRING)
1924 char *newstr = nullptr;
1925 util_asprintf(&newstr, "%s%s", last->m_constval.vstring, parser_tokval(parser));
1926 sy.out.back().out = parser->m_fold.constgen_string(newstr, false);
1928 concatenated = true;
1932 if (!concatenated) {
1933 parseerror(parser, "expected operator or end of statement");
1938 if (!parser_next(parser)) {
1941 if (parser->tok == ';' ||
1942 ((sy.paren.empty() || (sy.paren.size() == 1 && sy.paren.back() == PAREN_TERNARY2)) &&
1943 (parser->tok == ']' || parser->tok == ')' || parser->tok == '}')))
1949 while (sy.ops.size()) {
1950 if (!parser_sy_apply_operator(parser, &sy))
1954 parser->lex->flags.noops = true;
1955 if (sy.out.size() != 1) {
1956 parseerror(parser, "expression expected");
1959 expr = sy.out[0].out;
1960 if (sy.paren.size()) {
1961 parseerror(parser, "internal error: sy.paren.size() = %zu", sy.paren.size());
1967 parser->lex->flags.noops = true;
1968 for (auto &it : sy.out)
1969 if (it.out) ast_unref(it.out);
1973 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels)
1975 ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
1978 if (parser->tok != ';') {
1979 parseerror(parser, "semicolon expected after expression");
1983 if (!parser_next(parser)) {
1990 static void parser_enterblock(parser_t *parser)
1992 parser->variables.push_back(util_htnew(PARSER_HT_SIZE));
1993 parser->_blocklocals.push_back(parser->_locals.size());
1994 parser->typedefs.push_back(util_htnew(TYPEDEF_HT_SIZE));
1995 parser->_blocktypedefs.push_back(parser->_typedefs.size());
1996 parser->_block_ctx.push_back(parser_ctx(parser));
1999 static bool parser_leaveblock(parser_t *parser)
2002 size_t locals, typedefs;
2004 if (parser->variables.size() <= PARSER_HT_LOCALS) {
2005 parseerror(parser, "internal error: parser_leaveblock with no block");
2009 util_htdel(parser->variables.back());
2011 parser->variables.pop_back();
2012 if (!parser->_blocklocals.size()) {
2013 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
2017 locals = parser->_blocklocals.back();
2018 parser->_blocklocals.pop_back();
2019 parser->_locals.resize(locals);
2021 typedefs = parser->_blocktypedefs.back();
2022 parser->_typedefs.resize(typedefs);
2023 util_htdel(parser->typedefs.back());
2024 parser->typedefs.pop_back();
2026 parser->_block_ctx.pop_back();
2031 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
2033 parser->_locals.push_back(e);
2034 util_htset(parser->variables.back(), name, (void*)e);
2036 static void parser_addlocal(parser_t *parser, const std::string &name, ast_expression *e) {
2037 return parser_addlocal(parser, name.c_str(), e);
2040 static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
2042 parser->globals.push_back(e);
2043 util_htset(parser->htglobals, name, e);
2045 static void parser_addglobal(parser_t *parser, const std::string &name, ast_expression *e) {
2046 return parser_addglobal(parser, name.c_str(), e);
2049 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
2053 ast_expression *prev;
2055 if (cond->m_vtype == TYPE_VOID || cond->m_vtype >= TYPE_VARIANT) {
2057 ast_type_to_string(cond, ty, sizeof(ty));
2058 compile_error(cond->m_context, "invalid type for if() condition: %s", ty);
2061 if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && cond->m_vtype == TYPE_STRING)
2064 cond = ast_unary::make(cond->m_context, INSTR_NOT_S, cond);
2067 parseerror(parser, "internal error: failed to process condition");
2072 else if (OPTS_FLAG(CORRECT_LOGIC) && cond->m_vtype == TYPE_VECTOR)
2074 /* vector types need to be cast to true booleans */
2075 ast_binary *bin = (ast_binary*)cond;
2076 if (!OPTS_FLAG(PERL_LOGIC) || !ast_istype(cond, ast_binary) || !(bin->m_op == INSTR_AND || bin->m_op == INSTR_OR))
2078 /* in perl-logic, AND and OR take care of the -fcorrect-logic */
2080 cond = ast_unary::make(cond->m_context, INSTR_NOT_V, cond);
2083 parseerror(parser, "internal error: failed to process condition");
2090 unary = (ast_unary*)cond;
2091 /* ast_istype dereferences cond, should test here for safety */
2092 while (cond && ast_istype(cond, ast_unary) && unary->m_op == INSTR_NOT_F)
2094 cond = unary->m_operand;
2095 unary->m_operand = nullptr;
2098 unary = (ast_unary*)cond;
2102 parseerror(parser, "internal error: failed to process condition");
2104 if (ifnot) *_ifnot = !*_ifnot;
2108 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
2111 ast_expression *cond, *ontrue = nullptr, *onfalse = nullptr;
2114 lex_ctx_t ctx = parser_ctx(parser);
2116 (void)block; /* not touching */
2118 /* skip the 'if', parse an optional 'not' and check for an opening paren */
2119 if (!parser_next(parser)) {
2120 parseerror(parser, "expected condition or 'not'");
2123 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
2125 if (!parser_next(parser)) {
2126 parseerror(parser, "expected condition in parenthesis");
2130 if (parser->tok != '(') {
2131 parseerror(parser, "expected 'if' condition in parenthesis");
2134 /* parse into the expression */
2135 if (!parser_next(parser)) {
2136 parseerror(parser, "expected 'if' condition after opening paren");
2139 /* parse the condition */
2140 cond = parse_expression_leave(parser, false, true, false);
2144 if (parser->tok != ')') {
2145 parseerror(parser, "expected closing paren after 'if' condition");
2149 /* parse into the 'then' branch */
2150 if (!parser_next(parser)) {
2151 parseerror(parser, "expected statement for on-true branch of 'if'");
2155 if (!parse_statement_or_block(parser, &ontrue)) {
2160 ontrue = new ast_block(parser_ctx(parser));
2161 /* check for an else */
2162 if (!strcmp(parser_tokval(parser), "else")) {
2163 /* parse into the 'else' branch */
2164 if (!parser_next(parser)) {
2165 parseerror(parser, "expected on-false branch after 'else'");
2170 if (!parse_statement_or_block(parser, &onfalse)) {
2177 cond = process_condition(parser, cond, &ifnot);
2179 if (ontrue) delete ontrue;
2180 if (onfalse) delete onfalse;
2185 ifthen = new ast_ifthen(ctx, cond, onfalse, ontrue);
2187 ifthen = new ast_ifthen(ctx, cond, ontrue, onfalse);
2192 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out);
2193 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
2196 char *label = nullptr;
2198 /* skip the 'while' and get the body */
2199 if (!parser_next(parser)) {
2200 if (OPTS_FLAG(LOOP_LABELS))
2201 parseerror(parser, "expected loop label or 'while' condition in parenthesis");
2203 parseerror(parser, "expected 'while' condition in parenthesis");
2207 if (parser->tok == ':') {
2208 if (!OPTS_FLAG(LOOP_LABELS))
2209 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2210 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2211 parseerror(parser, "expected loop label");
2214 label = util_strdup(parser_tokval(parser));
2215 if (!parser_next(parser)) {
2217 parseerror(parser, "expected 'while' condition in parenthesis");
2222 if (parser->tok != '(') {
2223 parseerror(parser, "expected 'while' condition in parenthesis");
2227 parser->breaks.push_back(label);
2228 parser->continues.push_back(label);
2230 rv = parse_while_go(parser, block, out);
2233 if (parser->breaks.back() != label || parser->continues.back() != label) {
2234 parseerror(parser, "internal error: label stack corrupted");
2240 parser->breaks.pop_back();
2241 parser->continues.pop_back();
2246 static bool parse_while_go(parser_t *parser, ast_block *block, ast_expression **out)
2249 ast_expression *cond, *ontrue;
2253 lex_ctx_t ctx = parser_ctx(parser);
2255 (void)block; /* not touching */
2257 /* parse into the expression */
2258 if (!parser_next(parser)) {
2259 parseerror(parser, "expected 'while' condition after opening paren");
2262 /* parse the condition */
2263 cond = parse_expression_leave(parser, false, true, false);
2267 if (parser->tok != ')') {
2268 parseerror(parser, "expected closing paren after 'while' condition");
2272 /* parse into the 'then' branch */
2273 if (!parser_next(parser)) {
2274 parseerror(parser, "expected while-loop body");
2278 if (!parse_statement_or_block(parser, &ontrue)) {
2283 cond = process_condition(parser, cond, &ifnot);
2288 aloop = new ast_loop(ctx, nullptr, cond, ifnot, nullptr, false, nullptr, ontrue);
2293 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out);
2294 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
2297 char *label = nullptr;
2299 /* skip the 'do' and get the body */
2300 if (!parser_next(parser)) {
2301 if (OPTS_FLAG(LOOP_LABELS))
2302 parseerror(parser, "expected loop label or body");
2304 parseerror(parser, "expected loop body");
2308 if (parser->tok == ':') {
2309 if (!OPTS_FLAG(LOOP_LABELS))
2310 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2311 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2312 parseerror(parser, "expected loop label");
2315 label = util_strdup(parser_tokval(parser));
2316 if (!parser_next(parser)) {
2318 parseerror(parser, "expected loop body");
2323 parser->breaks.push_back(label);
2324 parser->continues.push_back(label);
2326 rv = parse_dowhile_go(parser, block, out);
2329 if (parser->breaks.back() != label || parser->continues.back() != label) {
2330 parseerror(parser, "internal error: label stack corrupted");
2336 parser->breaks.pop_back();
2337 parser->continues.pop_back();
2342 static bool parse_dowhile_go(parser_t *parser, ast_block *block, ast_expression **out)
2345 ast_expression *cond, *ontrue;
2349 lex_ctx_t ctx = parser_ctx(parser);
2351 (void)block; /* not touching */
2353 if (!parse_statement_or_block(parser, &ontrue))
2356 /* expect the "while" */
2357 if (parser->tok != TOKEN_KEYWORD ||
2358 strcmp(parser_tokval(parser), "while"))
2360 parseerror(parser, "expected 'while' and condition");
2365 /* skip the 'while' and check for opening paren */
2366 if (!parser_next(parser) || parser->tok != '(') {
2367 parseerror(parser, "expected 'while' condition in parenthesis");
2371 /* parse into the expression */
2372 if (!parser_next(parser)) {
2373 parseerror(parser, "expected 'while' condition after opening paren");
2377 /* parse the condition */
2378 cond = parse_expression_leave(parser, false, true, false);
2382 if (parser->tok != ')') {
2383 parseerror(parser, "expected closing paren after 'while' condition");
2389 if (!parser_next(parser) || parser->tok != ';') {
2390 parseerror(parser, "expected semicolon after condition");
2396 if (!parser_next(parser)) {
2397 parseerror(parser, "parse error");
2403 cond = process_condition(parser, cond, &ifnot);
2408 aloop = new ast_loop(ctx, nullptr, nullptr, false, cond, ifnot, nullptr, ontrue);
2413 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out);
2414 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
2417 char *label = nullptr;
2419 /* skip the 'for' and check for opening paren */
2420 if (!parser_next(parser)) {
2421 if (OPTS_FLAG(LOOP_LABELS))
2422 parseerror(parser, "expected loop label or 'for' expressions in parenthesis");
2424 parseerror(parser, "expected 'for' expressions in parenthesis");
2428 if (parser->tok == ':') {
2429 if (!OPTS_FLAG(LOOP_LABELS))
2430 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2431 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2432 parseerror(parser, "expected loop label");
2435 label = util_strdup(parser_tokval(parser));
2436 if (!parser_next(parser)) {
2438 parseerror(parser, "expected 'for' expressions in parenthesis");
2443 if (parser->tok != '(') {
2444 parseerror(parser, "expected 'for' expressions in parenthesis");
2448 parser->breaks.push_back(label);
2449 parser->continues.push_back(label);
2451 rv = parse_for_go(parser, block, out);
2454 if (parser->breaks.back() != label || parser->continues.back() != label) {
2455 parseerror(parser, "internal error: label stack corrupted");
2461 parser->breaks.pop_back();
2462 parser->continues.pop_back();
2466 static bool parse_for_go(parser_t *parser, ast_block *block, ast_expression **out)
2469 ast_expression *initexpr, *cond, *increment, *ontrue;
2474 lex_ctx_t ctx = parser_ctx(parser);
2476 parser_enterblock(parser);
2480 increment = nullptr;
2483 /* parse into the expression */
2484 if (!parser_next(parser)) {
2485 parseerror(parser, "expected 'for' initializer after opening paren");
2490 if (parser->tok == TOKEN_IDENT)
2491 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2493 if (typevar || parser->tok == TOKEN_TYPENAME) {
2494 if (!parse_variable(parser, block, true, CV_VAR, typevar, false, false, 0, nullptr))
2497 else if (parser->tok != ';')
2499 initexpr = parse_expression_leave(parser, false, false, false);
2502 /* move on to condition */
2503 if (parser->tok != ';') {
2504 parseerror(parser, "expected semicolon after for-loop initializer");
2507 if (!parser_next(parser)) {
2508 parseerror(parser, "expected for-loop condition");
2511 } else if (!parser_next(parser)) {
2512 parseerror(parser, "expected for-loop condition");
2516 /* parse the condition */
2517 if (parser->tok != ';') {
2518 cond = parse_expression_leave(parser, false, true, false);
2522 /* move on to incrementor */
2523 if (parser->tok != ';') {
2524 parseerror(parser, "expected semicolon after for-loop initializer");
2527 if (!parser_next(parser)) {
2528 parseerror(parser, "expected for-loop condition");
2532 /* parse the incrementor */
2533 if (parser->tok != ')') {
2534 lex_ctx_t condctx = parser_ctx(parser);
2535 increment = parse_expression_leave(parser, false, false, false);
2538 if (!increment->m_side_effects) {
2539 if (compile_warning(condctx, WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2545 if (parser->tok != ')') {
2546 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2549 /* parse into the 'then' branch */
2550 if (!parser_next(parser)) {
2551 parseerror(parser, "expected for-loop body");
2554 if (!parse_statement_or_block(parser, &ontrue))
2558 cond = process_condition(parser, cond, &ifnot);
2562 aloop = new ast_loop(ctx, initexpr, cond, ifnot, nullptr, false, increment, ontrue);
2565 if (!parser_leaveblock(parser)) {
2571 if (initexpr) ast_unref(initexpr);
2572 if (cond) ast_unref(cond);
2573 if (increment) ast_unref(increment);
2574 (void)!parser_leaveblock(parser);
2578 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2580 ast_expression *exp = nullptr;
2581 ast_expression *var = nullptr;
2582 ast_return *ret = nullptr;
2583 ast_value *retval = parser->function->m_return_value;
2584 ast_value *expected = parser->function->m_function_type;
2586 lex_ctx_t ctx = parser_ctx(parser);
2588 (void)block; /* not touching */
2590 if (!parser_next(parser)) {
2591 parseerror(parser, "expected return expression");
2595 /* return assignments */
2596 if (parser->tok == '=') {
2597 if (!OPTS_FLAG(RETURN_ASSIGNMENTS)) {
2598 parseerror(parser, "return assignments not activated, try using -freturn-assigments");
2602 if (type_store_instr[expected->m_next->m_vtype] == VINSTR_END) {
2604 ast_type_to_string(expected->m_next, ty1, sizeof(ty1));
2605 parseerror(parser, "invalid return type: `%s'", ty1);
2609 if (!parser_next(parser)) {
2610 parseerror(parser, "expected return assignment expression");
2614 if (!(exp = parse_expression_leave(parser, false, false, false)))
2617 /* prepare the return value */
2619 retval = new ast_value(ctx, "#LOCAL_RETURN", TYPE_VOID);
2620 retval->adoptType(*expected->m_next);
2621 parser->function->m_return_value = retval;
2622 parser->function->m_return_value->m_flags |= AST_FLAG_NOREF;
2625 if (!exp->compareType(*retval)) {
2626 char ty1[1024], ty2[1024];
2627 ast_type_to_string(exp, ty1, sizeof(ty1));
2628 ast_type_to_string(retval, ty2, sizeof(ty2));
2629 parseerror(parser, "invalid type for return value: `%s', expected `%s'", ty1, ty2);
2632 /* store to 'return' local variable */
2633 var = new ast_store(
2635 type_store_instr[expected->m_next->m_vtype],
2643 if (parser->tok != ';')
2644 parseerror(parser, "missing semicolon after return assignment");
2645 else if (!parser_next(parser))
2646 parseerror(parser, "parse error after return assignment");
2652 if (parser->tok != ';') {
2653 exp = parse_expression(parser, false, false);
2657 if (exp->m_vtype != TYPE_NIL &&
2658 exp->m_vtype != (expected)->m_next->m_vtype)
2660 parseerror(parser, "return with invalid expression");
2663 ret = new ast_return(ctx, exp);
2669 if (!parser_next(parser))
2670 parseerror(parser, "parse error");
2672 if (!retval && expected->m_next->m_vtype != TYPE_VOID)
2674 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2676 ret = new ast_return(ctx, retval);
2682 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2685 unsigned int levels = 0;
2686 lex_ctx_t ctx = parser_ctx(parser);
2687 auto &loops = (is_continue ? parser->continues : parser->breaks);
2689 (void)block; /* not touching */
2690 if (!parser_next(parser)) {
2691 parseerror(parser, "expected semicolon or loop label");
2695 if (loops.empty()) {
2697 parseerror(parser, "`continue` can only be used inside loops");
2699 parseerror(parser, "`break` can only be used inside loops or switches");
2702 if (parser->tok == TOKEN_IDENT) {
2703 if (!OPTS_FLAG(LOOP_LABELS))
2704 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2707 if (loops[i] && !strcmp(loops[i], parser_tokval(parser)))
2710 parseerror(parser, "no such loop to %s: `%s`",
2711 (is_continue ? "continue" : "break out of"),
2712 parser_tokval(parser));
2717 if (!parser_next(parser)) {
2718 parseerror(parser, "expected semicolon");
2723 if (parser->tok != ';') {
2724 parseerror(parser, "expected semicolon");
2728 if (!parser_next(parser))
2729 parseerror(parser, "parse error");
2731 *out = new ast_breakcont(ctx, is_continue, levels);
2735 /* returns true when it was a variable qualifier, false otherwise!
2736 * on error, cvq is set to CV_WRONG
2738 struct attribute_t {
2743 static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *noref, bool *is_static, uint32_t *_flags, char **message)
2745 bool had_const = false;
2746 bool had_var = false;
2747 bool had_noref = false;
2748 bool had_attrib = false;
2749 bool had_static = false;
2752 static attribute_t attributes[] = {
2753 { "noreturn", AST_FLAG_NORETURN },
2754 { "inline", AST_FLAG_INLINE },
2755 { "eraseable", AST_FLAG_ERASEABLE },
2756 { "accumulate", AST_FLAG_ACCUMULATE },
2757 { "last", AST_FLAG_FINAL_DECL }
2764 if (parser->tok == TOKEN_ATTRIBUTE_OPEN) {
2766 /* parse an attribute */
2767 if (!parser_next(parser)) {
2768 parseerror(parser, "expected attribute after `[[`");
2773 for (i = 0; i < GMQCC_ARRAY_COUNT(attributes); i++) {
2774 if (!strcmp(parser_tokval(parser), attributes[i].name)) {
2775 flags |= attributes[i].flag;
2776 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2777 parseerror(parser, "`%s` attribute has no parameters, expected `]]`",
2778 attributes[i].name);
2786 if (i != GMQCC_ARRAY_COUNT(attributes))
2790 if (!strcmp(parser_tokval(parser), "noref")) {
2792 if (!parser_next(parser) || parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2793 parseerror(parser, "`noref` attribute has no parameters, expected `]]`");
2798 else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
2799 flags |= AST_FLAG_ALIAS;
2802 if (!parser_next(parser)) {
2803 parseerror(parser, "parse error in attribute");
2807 if (parser->tok == '(') {
2808 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
2809 parseerror(parser, "`alias` attribute missing parameter");
2813 *message = util_strdup(parser_tokval(parser));
2815 if (!parser_next(parser)) {
2816 parseerror(parser, "parse error in attribute");
2820 if (parser->tok != ')') {
2821 parseerror(parser, "`alias` attribute expected `)` after parameter");
2825 if (!parser_next(parser)) {
2826 parseerror(parser, "parse error in attribute");
2831 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2832 parseerror(parser, "`alias` attribute expected `]]`");
2836 else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
2837 flags |= AST_FLAG_DEPRECATED;
2840 if (!parser_next(parser)) {
2841 parseerror(parser, "parse error in attribute");
2845 if (parser->tok == '(') {
2846 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
2847 parseerror(parser, "`deprecated` attribute missing parameter");
2851 *message = util_strdup(parser_tokval(parser));
2853 if (!parser_next(parser)) {
2854 parseerror(parser, "parse error in attribute");
2858 if(parser->tok != ')') {
2859 parseerror(parser, "`deprecated` attribute expected `)` after parameter");
2863 if (!parser_next(parser)) {
2864 parseerror(parser, "parse error in attribute");
2869 if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2870 parseerror(parser, "`deprecated` attribute expected `]]`");
2873 if (*message) mem_d(*message);
2879 else if (!strcmp(parser_tokval(parser), "coverage") && !(flags & AST_FLAG_COVERAGE)) {
2880 flags |= AST_FLAG_COVERAGE;
2881 if (!parser_next(parser)) {
2883 parseerror(parser, "parse error in coverage attribute");
2887 if (parser->tok == '(') {
2888 if (!parser_next(parser)) {
2890 parseerror(parser, "invalid parameter for coverage() attribute\n"
2891 "valid are: block");
2895 if (parser->tok != ')') {
2897 if (parser->tok != TOKEN_IDENT)
2898 goto bad_coverage_arg;
2899 if (!strcmp(parser_tokval(parser), "block"))
2900 flags |= AST_FLAG_BLOCK_COVERAGE;
2901 else if (!strcmp(parser_tokval(parser), "none"))
2902 flags &= ~(AST_FLAG_COVERAGE_MASK);
2904 goto bad_coverage_arg;
2905 if (!parser_next(parser))
2906 goto error_in_coverage;
2907 if (parser->tok == ',') {
2908 if (!parser_next(parser))
2909 goto error_in_coverage;
2911 } while (parser->tok != ')');
2913 if (parser->tok != ')' || !parser_next(parser))
2914 goto error_in_coverage;
2916 /* without parameter [[coverage]] equals [[coverage(block)]] */
2917 flags |= AST_FLAG_BLOCK_COVERAGE;
2922 /* Skip tokens until we hit a ]] */
2923 (void)!parsewarning(parser, WARN_UNKNOWN_ATTRIBUTE, "unknown attribute starting with `%s`", parser_tokval(parser));
2924 while (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
2925 if (!parser_next(parser)) {
2926 parseerror(parser, "error inside attribute");
2933 else if (with_local && !strcmp(parser_tokval(parser), "static"))
2935 else if (!strcmp(parser_tokval(parser), "const"))
2937 else if (!strcmp(parser_tokval(parser), "var"))
2939 else if (with_local && !strcmp(parser_tokval(parser), "local"))
2941 else if (!strcmp(parser_tokval(parser), "noref"))
2943 else if (!had_const && !had_var && !had_noref && !had_attrib && !had_static && !flags) {
2950 if (!parser_next(parser))
2960 *is_static = had_static;
2964 parseerror(parser, "parse error after variable qualifier");
2969 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
2970 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2973 char *label = nullptr;
2975 /* skip the 'while' and get the body */
2976 if (!parser_next(parser)) {
2977 if (OPTS_FLAG(LOOP_LABELS))
2978 parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
2980 parseerror(parser, "expected 'switch' operand in parenthesis");
2984 if (parser->tok == ':') {
2985 if (!OPTS_FLAG(LOOP_LABELS))
2986 parseerror(parser, "labeled loops not activated, try using -floop-labels");
2987 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2988 parseerror(parser, "expected loop label");
2991 label = util_strdup(parser_tokval(parser));
2992 if (!parser_next(parser)) {
2994 parseerror(parser, "expected 'switch' operand in parenthesis");
2999 if (parser->tok != '(') {
3000 parseerror(parser, "expected 'switch' operand in parenthesis");
3004 parser->breaks.push_back(label);
3006 rv = parse_switch_go(parser, block, out);
3009 if (parser->breaks.back() != label) {
3010 parseerror(parser, "internal error: label stack corrupted");
3016 parser->breaks.pop_back();
3021 static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
3023 ast_expression *operand;
3026 ast_switch *switchnode;
3027 ast_switch_case swcase;
3030 bool noref, is_static;
3031 uint32_t qflags = 0;
3033 lex_ctx_t ctx = parser_ctx(parser);
3035 (void)block; /* not touching */
3038 /* parse into the expression */
3039 if (!parser_next(parser)) {
3040 parseerror(parser, "expected switch operand");
3043 /* parse the operand */
3044 operand = parse_expression_leave(parser, false, false, false);
3048 switchnode = new ast_switch(ctx, operand);
3051 if (parser->tok != ')') {
3053 parseerror(parser, "expected closing paren after 'switch' operand");
3057 /* parse over the opening paren */
3058 if (!parser_next(parser) || parser->tok != '{') {
3060 parseerror(parser, "expected list of cases");
3064 if (!parser_next(parser)) {
3066 parseerror(parser, "expected 'case' or 'default'");
3070 /* new block; allow some variables to be declared here */
3071 parser_enterblock(parser);
3074 if (parser->tok == TOKEN_IDENT)
3075 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3076 if (typevar || parser->tok == TOKEN_TYPENAME) {
3077 if (!parse_variable(parser, block, true, CV_NONE, typevar, false, false, 0, nullptr)) {
3083 if (parse_qualifiers(parser, true, &cvq, &noref, &is_static, &qflags, nullptr))
3085 if (cvq == CV_WRONG) {
3089 if (!parse_variable(parser, block, true, cvq, nullptr, noref, is_static, qflags, nullptr)) {
3099 while (parser->tok != '}') {
3100 ast_block *caseblock;
3102 if (!strcmp(parser_tokval(parser), "case")) {
3103 if (!parser_next(parser)) {
3105 parseerror(parser, "expected expression for case");
3108 swcase.m_value = parse_expression_leave(parser, false, false, false);
3110 if (!operand->compareType(*swcase.m_value)) {
3114 ast_type_to_string(swcase.m_value, ty1, sizeof ty1);
3115 ast_type_to_string(operand, ty2, sizeof ty2);
3117 auto fnLiteral = [](ast_expression *expression) -> char* {
3118 if (!ast_istype(expression, ast_value))
3120 ast_value *value = (ast_value *)expression;
3121 if (!value->m_hasvalue)
3123 char *string = nullptr;
3124 basic_value_t *constval = &value->m_constval;
3125 switch (value->m_vtype)
3128 util_asprintf(&string, "%.2f", constval->vfloat);
3131 util_asprintf(&string, "'%.2f %.2f %.2f'",
3137 util_asprintf(&string, "\"%s\"", constval->vstring);
3145 char *literal = fnLiteral(swcase.m_value);
3147 compile_error(parser_ctx(parser), "incompatible type `%s` for switch case `%s` expected `%s`", ty1, literal, ty2);
3149 compile_error(parser_ctx(parser), "incompatible type `%s` for switch case expected `%s`", ty1, ty2);
3155 if (!swcase.m_value) {
3157 parseerror(parser, "expected expression for case");
3160 if (!OPTS_FLAG(RELAXED_SWITCH)) {
3161 if (!ast_istype(swcase.m_value, ast_value)) { /* || ((ast_value*)swcase.m_value)->m_cvq != CV_CONST) { */
3163 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
3169 else if (!strcmp(parser_tokval(parser), "default")) {
3170 swcase.m_value = nullptr;
3171 if (!parser_next(parser)) {
3173 parseerror(parser, "expected colon");
3179 parseerror(parser, "expected 'case' or 'default'");
3183 /* Now the colon and body */
3184 if (parser->tok != ':') {
3185 if (swcase.m_value) ast_unref(swcase.m_value);
3187 parseerror(parser, "expected colon");
3191 if (!parser_next(parser)) {
3192 if (swcase.m_value) ast_unref(swcase.m_value);
3194 parseerror(parser, "expected statements or case");
3197 caseblock = new ast_block(parser_ctx(parser));
3199 if (swcase.m_value) ast_unref(swcase.m_value);
3203 swcase.m_code = caseblock;
3204 switchnode->m_cases.push_back(swcase);
3206 ast_expression *expr;
3207 if (parser->tok == '}')
3209 if (parser->tok == TOKEN_KEYWORD) {
3210 if (!strcmp(parser_tokval(parser), "case") ||
3211 !strcmp(parser_tokval(parser), "default"))
3216 if (!parse_statement(parser, caseblock, &expr, true)) {
3222 if (!caseblock->addExpr(expr)) {
3229 parser_leaveblock(parser);
3232 if (parser->tok != '}') {
3234 parseerror(parser, "expected closing paren of case list");
3237 if (!parser_next(parser)) {
3239 parseerror(parser, "parse error after switch");
3246 /* parse computed goto sides */
3247 static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
3248 ast_expression *on_true;
3249 ast_expression *on_false;
3250 ast_expression *cond;
3255 if (ast_istype(*side, ast_ternary)) {
3256 ast_ternary *tern = (ast_ternary*)*side;
3257 on_true = parse_goto_computed(parser, &tern->m_on_true);
3258 on_false = parse_goto_computed(parser, &tern->m_on_false);
3260 if (!on_true || !on_false) {
3261 parseerror(parser, "expected label or expression in ternary");
3262 if (on_true) ast_unref(on_true);
3263 if (on_false) ast_unref(on_false);
3267 cond = tern->m_cond;
3268 tern->m_cond = nullptr;
3271 return new ast_ifthen(parser_ctx(parser), cond, on_true, on_false);
3272 } else if (ast_istype(*side, ast_label)) {
3273 ast_goto *gt = new ast_goto(parser_ctx(parser), ((ast_label*)*side)->m_name);
3274 gt->setLabel(reinterpret_cast<ast_label*>(*side));
3281 static bool parse_goto(parser_t *parser, ast_expression **out)
3283 ast_goto *gt = nullptr;
3284 ast_expression *lbl;
3286 if (!parser_next(parser))
3289 if (parser->tok != TOKEN_IDENT) {
3290 ast_expression *expression;
3292 /* could be an expression i.e computed goto :-) */
3293 if (parser->tok != '(') {
3294 parseerror(parser, "expected label name after `goto`");
3298 /* failed to parse expression for goto */
3299 if (!(expression = parse_expression(parser, false, true)) ||
3300 !(*out = parse_goto_computed(parser, &expression))) {
3301 parseerror(parser, "invalid goto expression");
3303 ast_unref(expression);
3310 /* not computed goto */
3311 gt = new ast_goto(parser_ctx(parser), parser_tokval(parser));
3312 lbl = parser_find_label(parser, gt->m_name);
3314 if (!ast_istype(lbl, ast_label)) {
3315 parseerror(parser, "internal error: label is not an ast_label");
3319 gt->setLabel(reinterpret_cast<ast_label*>(lbl));
3322 parser->gotos.push_back(gt);
3324 if (!parser_next(parser) || parser->tok != ';') {
3325 parseerror(parser, "semicolon expected after goto label");
3328 if (!parser_next(parser)) {
3329 parseerror(parser, "parse error after goto");
3337 static bool parse_skipwhite(parser_t *parser)
3340 if (!parser_next(parser))
3342 } while (parser->tok == TOKEN_WHITE && parser->tok < TOKEN_ERROR);
3343 return parser->tok < TOKEN_ERROR;
3346 static bool parse_eol(parser_t *parser)
3348 if (!parse_skipwhite(parser))
3350 return parser->tok == TOKEN_EOL;
3353 static bool parse_pragma_do(parser_t *parser)
3355 if (!parser_next(parser) ||
3356 parser->tok != TOKEN_IDENT ||
3357 strcmp(parser_tokval(parser), "pragma"))
3359 parseerror(parser, "expected `pragma` keyword after `#`, got `%s`", parser_tokval(parser));
3362 if (!parse_skipwhite(parser) || parser->tok != TOKEN_IDENT) {
3363 parseerror(parser, "expected pragma, got `%s`", parser_tokval(parser));
3367 if (!strcmp(parser_tokval(parser), "noref")) {
3368 if (!parse_skipwhite(parser) || parser->tok != TOKEN_INTCONST) {
3369 parseerror(parser, "`noref` pragma requires an argument: 0 or 1");
3372 parser->noref = !!parser_token(parser)->constval.i;
3373 if (!parse_eol(parser)) {
3374 parseerror(parser, "parse error after `noref` pragma");
3380 (void)!parsewarning(parser, WARN_UNKNOWN_PRAGMAS, "ignoring #pragma %s", parser_tokval(parser));
3383 while (!parse_eol(parser)) {
3384 parser_next(parser);
3393 static bool parse_pragma(parser_t *parser)
3396 parser->lex->flags.preprocessing = true;
3397 parser->lex->flags.mergelines = true;
3398 rv = parse_pragma_do(parser);
3399 if (parser->tok != TOKEN_EOL) {
3400 parseerror(parser, "junk after pragma");
3403 parser->lex->flags.preprocessing = false;
3404 parser->lex->flags.mergelines = false;
3405 if (!parser_next(parser)) {
3406 parseerror(parser, "parse error after pragma");
3412 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
3414 bool noref, is_static;
3416 uint32_t qflags = 0;
3417 ast_value *typevar = nullptr;
3418 char *vstring = nullptr;
3422 if (parser->tok == TOKEN_IDENT)
3423 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
3425 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.' || parser->tok == TOKEN_DOTS)
3427 /* local variable */
3429 parseerror(parser, "cannot declare a variable from here");
3432 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
3433 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
3436 if (!parse_variable(parser, block, false, CV_NONE, typevar, false, false, 0, nullptr))
3440 else if (parse_qualifiers(parser, !!block, &cvq, &noref, &is_static, &qflags, &vstring))
3442 if (cvq == CV_WRONG)
3444 return parse_variable(parser, block, false, cvq, nullptr, noref, is_static, qflags, vstring);
3446 else if (parser->tok == TOKEN_KEYWORD)
3448 if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
3453 if (!parser_next(parser)) {
3454 parseerror(parser, "parse error after __builtin_debug_printtype");
3458 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
3460 ast_type_to_string(tdef, ty, sizeof(ty));
3461 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->m_name.c_str(), ty);
3462 if (!parser_next(parser)) {
3463 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
3469 if (!parse_statement(parser, block, out, allow_cases))
3472 con_out("__builtin_debug_printtype: got no output node\n");