5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define PARSER_HT_FIELDS 0
30 #define PARSER_HT_GLOBALS 1
31 /* beginning of locals */
32 #define PARSER_HT_LOCALS 2
34 #define PARSER_HT_SIZE 1024
35 #define TYPEDEF_HT_SIZE 16
41 ast_expression **globals;
42 ast_expression **fields;
43 ast_function **functions;
44 ast_value **imm_float;
45 ast_value **imm_string;
46 ast_value **imm_vector;
49 /* must be deleted first, they reference immediates and values */
50 ast_value **accessors;
52 ast_value *imm_float_zero;
53 ast_value *imm_float_one;
54 ast_value *imm_vector_zero;
59 ast_function *function;
61 /* All the labels the function defined...
62 * Should they be in ast_function instead?
67 /* A list of hashtables for each scope */
73 /* not to be used directly, we use the hash table */
74 ast_expression **_locals;
76 ast_value **_typedefs;
77 size_t *_blocktypedefs;
82 /* we store the '=' operator info */
83 const oper_info *assign_op;
85 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
86 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
87 * anything else: type error
92 static void parser_enterblock(parser_t *parser);
93 static bool parser_leaveblock(parser_t *parser);
94 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
95 static bool parse_typedef(parser_t *parser);
96 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef);
97 static ast_block* parse_block(parser_t *parser);
98 static bool parse_block_into(parser_t *parser, ast_block *block);
99 static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
100 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
101 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
102 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
104 static void parseerror(parser_t *parser, const char *fmt, ...)
111 con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
115 /* returns true if it counts as an error */
116 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
119 int lvl = LVL_WARNING;
121 if (!OPTS_WARN(warntype))
130 con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
136 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
139 int lvl = LVL_WARNING;
141 if (!OPTS_WARN(warntype))
148 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
154 /**********************************************************************
155 * some maths used for constant folding
158 vector vec3_add(vector a, vector b)
167 vector vec3_sub(vector a, vector b)
176 qcfloat vec3_mulvv(vector a, vector b)
178 return (a.x * b.x + a.y * b.y + a.z * b.z);
181 vector vec3_mulvf(vector a, float b)
190 /**********************************************************************
194 bool parser_next(parser_t *parser)
196 /* lex_do kills the previous token */
197 parser->tok = lex_do(parser->lex);
198 if (parser->tok == TOKEN_EOF)
200 if (parser->tok >= TOKEN_ERROR) {
201 parseerror(parser, "lex error");
207 #define parser_tokval(p) ((p)->lex->tok.value)
208 #define parser_token(p) (&((p)->lex->tok))
209 #define parser_ctx(p) ((p)->lex->tok.ctx)
211 static ast_value* parser_const_float(parser_t *parser, double d)
215 for (i = 0; i < vec_size(parser->imm_float); ++i) {
216 const double compare = parser->imm_float[i]->constval.vfloat;
217 if (memcmp((const void*)&compare, (const void *)&d, sizeof(double)) == 0)
218 return parser->imm_float[i];
220 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
222 out->hasvalue = true;
223 out->constval.vfloat = d;
224 vec_push(parser->imm_float, out);
228 static ast_value* parser_const_float_0(parser_t *parser)
230 if (!parser->imm_float_zero)
231 parser->imm_float_zero = parser_const_float(parser, 0);
232 return parser->imm_float_zero;
235 static ast_value* parser_const_float_1(parser_t *parser)
237 if (!parser->imm_float_one)
238 parser->imm_float_one = parser_const_float(parser, 1);
239 return parser->imm_float_one;
242 static char *parser_strdup(const char *str)
245 /* actually dup empty strings */
246 char *out = mem_a(1);
250 return util_strdup(str);
253 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
257 for (i = 0; i < vec_size(parser->imm_string); ++i) {
258 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
259 return parser->imm_string[i];
263 snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
264 out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
266 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
268 out->hasvalue = true;
269 out->constval.vstring = parser_strdup(str);
270 vec_push(parser->imm_string, out);
274 static ast_value* parser_const_vector(parser_t *parser, vector v)
278 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
279 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
280 return parser->imm_vector[i];
282 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
284 out->hasvalue = true;
285 out->constval.vvec = v;
286 vec_push(parser->imm_vector, out);
290 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
296 return parser_const_vector(parser, v);
299 static ast_value* parser_const_vector_0(parser_t *parser)
301 if (!parser->imm_vector_zero)
302 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
303 return parser->imm_vector_zero;
306 static ast_expression* parser_find_field(parser_t *parser, const char *name)
308 return util_htget(parser->htfields, name);
311 static ast_expression* parser_find_global(parser_t *parser, const char *name)
313 return util_htget(parser->htglobals, name);
316 static ast_expression* parser_find_param(parser_t *parser, const char *name)
320 if (!parser->function)
322 fun = parser->function->vtype;
323 for (i = 0; i < vec_size(fun->expression.params); ++i) {
324 if (!strcmp(fun->expression.params[i]->name, name))
325 return (ast_expression*)(fun->expression.params[i]);
330 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
335 hash = util_hthash(parser->htglobals, name);
338 for (i = vec_size(parser->variables); i > upto;) {
340 if ( (e = util_htgeth(parser->variables[i], name, hash)) )
344 return parser_find_param(parser, name);
347 static ast_expression* parser_find_var(parser_t *parser, const char *name)
351 v = parser_find_local(parser, name, 0, &dummy);
352 if (!v) v = parser_find_global(parser, name);
356 static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
360 hash = util_hthash(parser->typedefs[0], name);
362 for (i = vec_size(parser->typedefs); i > upto;) {
364 if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
372 size_t etype; /* 0 = expression, others are operators */
376 ast_block *block; /* for commas and function calls */
385 #define SY_PAREN_EXPR '('
386 #define SY_PAREN_FUNC 'f'
387 #define SY_PAREN_INDEX '['
388 #define SY_PAREN_TERNARY '?'
390 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
401 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
405 e.out = (ast_expression*)v;
412 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
414 e.etype = 1 + (op - operators);
423 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
435 # define DEBUGSHUNTDO(x) x
437 # define DEBUGSHUNTDO(x)
440 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
441 * so we need to rotate it to become ent.(foo[n]).
443 static bool rotate_entfield_array_index_nodes(ast_expression **out)
445 ast_array_index *index;
446 ast_entfield *entfield;
450 ast_expression *entity;
452 lex_ctx ctx = ast_ctx(*out);
454 if (!ast_istype(*out, ast_array_index))
456 index = (ast_array_index*)*out;
458 if (!ast_istype(index->array, ast_entfield))
460 entfield = (ast_entfield*)index->array;
462 if (!ast_istype(entfield->field, ast_value))
464 field = (ast_value*)entfield->field;
467 entity = entfield->entity;
471 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
472 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
473 *out = (ast_expression*)entfield;
478 static bool parser_sy_pop(parser_t *parser, shunt *sy)
482 ast_expression *out = NULL;
483 ast_expression *exprs[3];
484 ast_block *blocks[3];
485 ast_value *asvalue[3];
486 ast_binstore *asbinstore;
487 size_t i, assignop, addop, subop;
488 qcint generated_op = 0;
493 if (!vec_size(sy->ops)) {
494 parseerror(parser, "internal error: missing operator");
498 if (vec_last(sy->ops).paren) {
499 parseerror(parser, "unmatched parenthesis");
503 op = &operators[vec_last(sy->ops).etype - 1];
504 ctx = vec_last(sy->ops).ctx;
506 DEBUGSHUNTDO(con_out("apply %s\n", op->op));
508 if (vec_size(sy->out) < op->operands) {
509 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", vec_size(sy->out),
510 op->op, (int)op->id);
514 vec_shrinkby(sy->ops, 1);
516 /* op(:?) has no input and no output */
520 vec_shrinkby(sy->out, op->operands);
521 for (i = 0; i < op->operands; ++i) {
522 exprs[i] = sy->out[vec_size(sy->out)+i].out;
523 blocks[i] = sy->out[vec_size(sy->out)+i].block;
524 asvalue[i] = (ast_value*)exprs[i];
527 if (blocks[0] && !vec_size(blocks[0]->exprs) && op->id != opid1(',')) {
528 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
532 #define NotSameType(T) \
533 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
534 exprs[0]->expression.vtype != T)
535 #define CanConstFold1(A) \
536 (ast_istype((A), ast_value) && ((ast_value*)(A))->hasvalue && (((ast_value*)(A))->cvq == CV_CONST))
537 #define CanConstFold(A, B) \
538 (CanConstFold1(A) && CanConstFold1(B))
539 #define ConstV(i) (asvalue[(i)]->constval.vvec)
540 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
541 #define ConstS(i) (asvalue[(i)]->constval.vstring)
545 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
549 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
550 if (exprs[1]->expression.vtype != TYPE_FIELD) {
551 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
554 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
556 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
557 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
561 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
567 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
568 !(exprs[0]->expression.vtype == TYPE_FIELD &&
569 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
571 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
572 parseerror(parser, "cannot index value of type %s", ty1);
575 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
576 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
577 parseerror(parser, "index must be of type float, not %s", ty1);
580 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
581 if (rotate_entfield_array_index_nodes(&out))
584 /* This is not broken in fteqcc anymore */
585 if (opts_standard != COMPILER_GMQCC) {
586 /* this error doesn't need to make us bail out */
587 (void)!parsewarning(parser, WARN_EXTENSIONS,
588 "accessing array-field members of an entity without parenthesis\n"
589 " -> this is an extension from -std=gmqcc");
597 ast_block_add_expr(blocks[0], exprs[1]);
599 blocks[0] = ast_block_new(ctx);
600 ast_block_add_expr(blocks[0], exprs[0]);
601 ast_block_add_expr(blocks[0], exprs[1]);
603 if (!ast_block_set_type(blocks[0], exprs[1]))
606 vec_push(sy->out, syblock(ctx, blocks[0]));
613 switch (exprs[0]->expression.vtype) {
615 if (CanConstFold1(exprs[0]))
616 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
618 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
619 (ast_expression*)parser_const_float_0(parser),
623 if (CanConstFold1(exprs[0]))
624 out = (ast_expression*)parser_const_vector_f(parser,
625 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
627 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
628 (ast_expression*)parser_const_vector_0(parser),
632 parseerror(parser, "invalid types used in expression: cannot negate type %s",
633 type_name[exprs[0]->expression.vtype]);
639 switch (exprs[0]->expression.vtype) {
641 if (CanConstFold1(exprs[0]))
642 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
644 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
647 if (CanConstFold1(exprs[0]))
648 out = (ast_expression*)parser_const_float(parser,
649 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
651 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
654 if (CanConstFold1(exprs[0]))
655 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
657 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
659 /* we don't constant-fold NOT for these types */
661 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
664 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
667 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
668 type_name[exprs[0]->expression.vtype]);
674 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
675 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
677 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
678 type_name[exprs[0]->expression.vtype],
679 type_name[exprs[1]->expression.vtype]);
682 switch (exprs[0]->expression.vtype) {
684 if (CanConstFold(exprs[0], exprs[1]))
686 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
689 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
692 if (CanConstFold(exprs[0], exprs[1]))
693 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
695 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
698 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
699 type_name[exprs[0]->expression.vtype],
700 type_name[exprs[1]->expression.vtype]);
705 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
706 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
708 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
709 type_name[exprs[1]->expression.vtype],
710 type_name[exprs[0]->expression.vtype]);
713 switch (exprs[0]->expression.vtype) {
715 if (CanConstFold(exprs[0], exprs[1]))
716 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
718 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
721 if (CanConstFold(exprs[0], exprs[1]))
722 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
724 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
727 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
728 type_name[exprs[1]->expression.vtype],
729 type_name[exprs[0]->expression.vtype]);
734 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
735 exprs[0]->expression.vtype != TYPE_VECTOR &&
736 exprs[0]->expression.vtype != TYPE_FLOAT &&
737 exprs[1]->expression.vtype != TYPE_VECTOR &&
738 exprs[1]->expression.vtype != TYPE_FLOAT)
740 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
741 type_name[exprs[1]->expression.vtype],
742 type_name[exprs[0]->expression.vtype]);
745 switch (exprs[0]->expression.vtype) {
747 if (exprs[1]->expression.vtype == TYPE_VECTOR)
749 if (CanConstFold(exprs[0], exprs[1]))
750 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
752 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
756 if (CanConstFold(exprs[0], exprs[1]))
757 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
759 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
763 if (exprs[1]->expression.vtype == TYPE_FLOAT)
765 if (CanConstFold(exprs[0], exprs[1]))
766 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
768 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
772 if (CanConstFold(exprs[0], exprs[1]))
773 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
775 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
779 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
780 type_name[exprs[1]->expression.vtype],
781 type_name[exprs[0]->expression.vtype]);
786 if (NotSameType(TYPE_FLOAT)) {
787 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
788 type_name[exprs[0]->expression.vtype],
789 type_name[exprs[1]->expression.vtype]);
792 if (CanConstFold(exprs[0], exprs[1]))
793 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
795 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
799 parseerror(parser, "qc does not have a modulo operator");
803 if (NotSameType(TYPE_FLOAT)) {
804 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
805 type_name[exprs[0]->expression.vtype],
806 type_name[exprs[1]->expression.vtype]);
809 if (CanConstFold(exprs[0], exprs[1]))
810 out = (ast_expression*)parser_const_float(parser,
811 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
812 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
814 out = (ast_expression*)ast_binary_new(ctx,
815 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
819 parseerror(parser, "TODO: bitxor");
824 case opid3('<','<','='):
825 case opid3('>','>','='):
826 parseerror(parser, "TODO: shifts");
830 generated_op += 1; /* INSTR_OR */
832 generated_op += INSTR_AND;
834 if (NotSameType(TYPE_FLOAT)) {
835 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
836 type_name[exprs[0]->expression.vtype],
837 type_name[exprs[1]->expression.vtype]);
838 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
839 parseerror(parser, "TODO: optional early out");
843 if (opts_standard == COMPILER_GMQCC)
844 con_out("TODO: early out logic\n");
845 if (CanConstFold(exprs[0], exprs[1]))
846 out = (ast_expression*)parser_const_float(parser,
847 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
849 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
853 if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
854 ast_type_to_string(exprs[1], ty1, sizeof(ty1));
855 ast_type_to_string(exprs[2], ty2, sizeof(ty2));
856 parseerror(parser, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
859 if (CanConstFold1(exprs[0]))
860 out = (ConstF(0) ? exprs[1] : exprs[2]);
862 out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
866 generated_op += 1; /* INSTR_GT */
868 generated_op += 1; /* INSTR_LT */
869 case opid2('>', '='):
870 generated_op += 1; /* INSTR_GE */
871 case opid2('<', '='):
872 generated_op += INSTR_LE;
873 if (NotSameType(TYPE_FLOAT)) {
874 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
875 type_name[exprs[0]->expression.vtype],
876 type_name[exprs[1]->expression.vtype]);
879 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
881 case opid2('!', '='):
882 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
883 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
884 type_name[exprs[0]->expression.vtype],
885 type_name[exprs[1]->expression.vtype]);
888 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
890 case opid2('=', '='):
891 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
892 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
893 type_name[exprs[0]->expression.vtype],
894 type_name[exprs[1]->expression.vtype]);
897 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
901 if (ast_istype(exprs[0], ast_entfield)) {
902 ast_expression *field = ((ast_entfield*)exprs[0])->field;
903 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
904 exprs[0]->expression.vtype == TYPE_FIELD &&
905 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
907 assignop = type_storep_instr[TYPE_VECTOR];
910 assignop = type_storep_instr[exprs[0]->expression.vtype];
911 if (!ast_compare_type(field->expression.next, exprs[1])) {
912 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
913 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
914 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
915 field->expression.next->expression.vtype == TYPE_FUNCTION &&
916 exprs[1]->expression.vtype == TYPE_FUNCTION)
918 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
919 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
925 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
930 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
931 exprs[0]->expression.vtype == TYPE_FIELD &&
932 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
934 assignop = type_store_instr[TYPE_VECTOR];
937 assignop = type_store_instr[exprs[0]->expression.vtype];
940 if (assignop == AINSTR_END) {
941 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
942 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
943 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
945 else if (!ast_compare_type(exprs[0], exprs[1])) {
946 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
947 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
948 if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
949 exprs[0]->expression.vtype == TYPE_FUNCTION &&
950 exprs[1]->expression.vtype == TYPE_FUNCTION)
952 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
953 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
959 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
962 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
963 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
965 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
967 case opid3('+','+','P'):
968 case opid3('-','-','P'):
970 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
971 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
972 parseerror(parser, "invalid type for prefix increment: %s", ty1);
975 if (op->id == opid3('+','+','P'))
979 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
980 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
982 if (ast_istype(exprs[0], ast_entfield)) {
983 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
985 (ast_expression*)parser_const_float_1(parser));
987 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
989 (ast_expression*)parser_const_float_1(parser));
992 case opid3('S','+','+'):
993 case opid3('S','-','-'):
995 if (exprs[0]->expression.vtype != TYPE_FLOAT) {
996 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
997 parseerror(parser, "invalid type for suffix increment: %s", ty1);
1000 if (op->id == opid3('S','+','+')) {
1001 addop = INSTR_ADD_F;
1002 subop = INSTR_SUB_F;
1004 addop = INSTR_SUB_F;
1005 subop = INSTR_ADD_F;
1007 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1008 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1010 if (ast_istype(exprs[0], ast_entfield)) {
1011 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STOREP_F, addop,
1013 (ast_expression*)parser_const_float_1(parser));
1015 out = (ast_expression*)ast_binstore_new(ctx, INSTR_STORE_F, addop,
1017 (ast_expression*)parser_const_float_1(parser));
1021 out = (ast_expression*)ast_binary_new(ctx, subop,
1023 (ast_expression*)parser_const_float_1(parser));
1025 case opid2('+','='):
1026 case opid2('-','='):
1027 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
1028 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
1030 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1031 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1032 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1036 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1037 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1039 if (ast_istype(exprs[0], ast_entfield))
1040 assignop = type_storep_instr[exprs[0]->expression.vtype];
1042 assignop = type_store_instr[exprs[0]->expression.vtype];
1043 switch (exprs[0]->expression.vtype) {
1045 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1046 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
1047 exprs[0], exprs[1]);
1050 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1051 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
1052 exprs[0], exprs[1]);
1055 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1056 type_name[exprs[0]->expression.vtype],
1057 type_name[exprs[1]->expression.vtype]);
1061 case opid2('*','='):
1062 case opid2('/','='):
1063 if (exprs[1]->expression.vtype != TYPE_FLOAT ||
1064 !(exprs[0]->expression.vtype == TYPE_FLOAT ||
1065 exprs[0]->expression.vtype == TYPE_VECTOR))
1067 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1068 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1069 parseerror(parser, "invalid types used in expression: %s and %s",
1073 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1074 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1076 if (ast_istype(exprs[0], ast_entfield))
1077 assignop = type_storep_instr[exprs[0]->expression.vtype];
1079 assignop = type_store_instr[exprs[0]->expression.vtype];
1080 switch (exprs[0]->expression.vtype) {
1082 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1083 (op->id == opid2('*','=') ? INSTR_MUL_F : INSTR_DIV_F),
1084 exprs[0], exprs[1]);
1087 if (op->id == opid2('*','=')) {
1088 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1089 exprs[0], exprs[1]);
1091 /* there's no DIV_VF */
1092 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F,
1093 (ast_expression*)parser_const_float_1(parser),
1097 out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF,
1102 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
1103 type_name[exprs[0]->expression.vtype],
1104 type_name[exprs[1]->expression.vtype]);
1108 case opid2('&','='):
1109 case opid2('|','='):
1110 if (NotSameType(TYPE_FLOAT)) {
1111 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1112 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1113 parseerror(parser, "invalid types used in expression: %s and %s",
1117 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1118 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1120 if (ast_istype(exprs[0], ast_entfield))
1121 assignop = type_storep_instr[exprs[0]->expression.vtype];
1123 assignop = type_store_instr[exprs[0]->expression.vtype];
1124 out = (ast_expression*)ast_binstore_new(ctx, assignop,
1125 (op->id == opid2('&','=') ? INSTR_BITAND : INSTR_BITOR),
1126 exprs[0], exprs[1]);
1128 case opid3('&','~','='):
1129 /* This is like: a &= ~(b);
1130 * But QC has no bitwise-not, so we implement it as
1133 if (NotSameType(TYPE_FLOAT)) {
1134 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
1135 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
1136 parseerror(parser, "invalid types used in expression: %s and %s",
1140 if (ast_istype(exprs[0], ast_entfield))
1141 assignop = type_storep_instr[exprs[0]->expression.vtype];
1143 assignop = type_store_instr[exprs[0]->expression.vtype];
1144 out = (ast_expression*)ast_binary_new(ctx, INSTR_BITAND, exprs[0], exprs[1]);
1147 if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) {
1148 parseerror(parser, "assignment to constant `%s`", asvalue[0]->name);
1150 asbinstore = ast_binstore_new(ctx, assignop, INSTR_SUB_F, exprs[0], out);
1151 asbinstore->keep_dest = true;
1152 out = (ast_expression*)asbinstore;
1158 parseerror(parser, "failed to apply operand %s", op->op);
1162 DEBUGSHUNTDO(con_out("applied %s\n", op->op));
1163 vec_push(sy->out, syexp(ctx, out));
1167 static bool parser_close_call(parser_t *parser, shunt *sy)
1169 /* was a function call */
1170 ast_expression *fun;
1176 vec_shrinkby(sy->ops, 1);
1177 fid = sy->ops[vec_size(sy->ops)].off;
1179 /* out[fid] is the function
1180 * everything above is parameters...
1181 * 0 params = nothing
1182 * 1 params = ast_expression
1186 if (vec_size(sy->out) < 1 || vec_size(sy->out) <= fid) {
1187 parseerror(parser, "internal error: function call needs function and parameter list...");
1191 fun = sy->out[fid].out;
1193 call = ast_call_new(sy->ops[vec_size(sy->ops)].ctx, fun);
1195 parseerror(parser, "out of memory");
1199 if (fid+1 == vec_size(sy->out)) {
1202 } else if (fid+2 == vec_size(sy->out)) {
1204 vec_shrinkby(sy->out, 1);
1205 params = sy->out[vec_size(sy->out)].block;
1209 vec_push(call->params, sy->out[vec_size(sy->out)].out);
1211 paramcount = vec_size(params->exprs);
1212 call->params = params->exprs;
1213 params->exprs = NULL;
1216 if (!ast_call_check_types(call))
1219 parseerror(parser, "invalid function call");
1223 /* overwrite fid, the function, with a call */
1224 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
1226 if (fun->expression.vtype != TYPE_FUNCTION) {
1227 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
1231 if (!fun->expression.next) {
1232 parseerror(parser, "could not determine function return type");
1235 if (vec_size(fun->expression.params) != paramcount &&
1236 !(fun->expression.variadic &&
1237 vec_size(fun->expression.params) < paramcount))
1240 const char *fewmany = (vec_size(fun->expression.params) > paramcount) ? "few" : "many";
1242 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
1243 if (opts_standard == COMPILER_GMQCC)
1246 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1247 " -> `%s` has been declared here: %s:%i",
1248 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1249 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1251 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1252 " -> `%s` has been declared here: %s:%i",
1253 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1254 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1260 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1261 "too %s parameters for call to %s: expected %i, got %i\n"
1262 " -> `%s` has been declared here: %s:%i",
1263 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1264 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1266 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1267 "too %s parameters for function call: expected %i, got %i\n"
1268 " -> `%s` has been declared here: %s:%i",
1269 fewmany, fval->name, (int)vec_size(fun->expression.params), (int)paramcount,
1270 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1278 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1280 if (!vec_size(sy->ops)) {
1281 parseerror(parser, "unmatched closing paren");
1284 /* this would for bit a + (x) because there are no operators inside (x)
1285 if (sy->ops[vec_size(sy->ops)-1].paren == 1) {
1286 parseerror(parser, "empty parenthesis expression");
1290 while (vec_size(sy->ops)) {
1291 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_FUNC) {
1292 if (!parser_close_call(parser, sy))
1296 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_EXPR) {
1297 vec_shrinkby(sy->ops, 1);
1298 return !functions_only;
1300 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_INDEX) {
1303 /* pop off the parenthesis */
1304 vec_shrinkby(sy->ops, 1);
1305 /* then apply the index operator */
1306 if (!parser_sy_pop(parser, sy))
1310 if (sy->ops[vec_size(sy->ops)-1].paren == SY_PAREN_TERNARY) {
1313 /* pop off the parenthesis */
1314 vec_shrinkby(sy->ops, 1);
1317 if (!parser_sy_pop(parser, sy))
1323 static void parser_reclassify_token(parser_t *parser)
1326 for (i = 0; i < operator_count; ++i) {
1327 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1328 parser->tok = TOKEN_OPERATOR;
1334 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1336 ast_expression *expr = NULL;
1338 bool wantop = false;
1339 bool gotmemberof = false;
1341 /* count the parens because an if starts with one, so the
1342 * end of a condition is an unmatched closing paren
1350 parser->lex->flags.noops = false;
1352 parser_reclassify_token(parser);
1357 gotmemberof = false;
1359 parser->memberof = 0;
1361 if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
1362 parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "_"))
1364 /* a translatable string */
1368 parseerror(parser, "expected operator or end of statement, got constant");
1372 parser->lex->flags.noops = true;
1373 if (!parser_next(parser) || parser->tok != '(') {
1374 parseerror(parser, "use _(\"string\") to create a translatable string constant");
1377 parser->lex->flags.noops = false;
1378 if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
1379 parseerror(parser, "expected a constant string in translatable-string extension");
1382 val = parser_const_string(parser, parser_tokval(parser), true);
1386 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1387 DEBUGSHUNTDO(con_out("push string\n"));
1389 if (!parser_next(parser) || parser->tok != ')') {
1390 parseerror(parser, "expected closing paren after translatable string");
1394 else if (parser->tok == TOKEN_IDENT)
1396 ast_expression *var;
1398 parseerror(parser, "expected operator or end of statement");
1403 if (opts_standard == COMPILER_GMQCC)
1405 if (parser->memberof == TYPE_ENTITY) {
1406 /* still get vars first since there could be a fieldpointer */
1407 var = parser_find_var(parser, parser_tokval(parser));
1409 var = parser_find_field(parser, parser_tokval(parser));
1411 else if (parser->memberof == TYPE_VECTOR)
1413 parseerror(parser, "TODO: implement effective vector member access");
1416 else if (parser->memberof) {
1417 parseerror(parser, "namespace for member not found");
1421 var = parser_find_var(parser, parser_tokval(parser));
1423 var = parser_find_var(parser, parser_tokval(parser));
1425 var = parser_find_field(parser, parser_tokval(parser));
1428 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1431 if (ast_istype(var, ast_value)) {
1432 ((ast_value*)var)->uses++;
1434 else if (ast_istype(var, ast_member)) {
1435 ast_member *mem = (ast_member*)var;
1436 if (ast_istype(mem->owner, ast_value))
1437 ((ast_value*)(mem->owner))->uses++;
1439 vec_push(sy.out, syexp(parser_ctx(parser), var));
1440 DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser)));
1442 else if (parser->tok == TOKEN_FLOATCONST) {
1445 parseerror(parser, "expected operator or end of statement, got constant");
1449 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1452 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1453 DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
1455 else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
1458 parseerror(parser, "expected operator or end of statement, got constant");
1462 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1465 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1466 DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i));
1468 else if (parser->tok == TOKEN_STRINGCONST) {
1471 parseerror(parser, "expected operator or end of statement, got constant");
1475 val = parser_const_string(parser, parser_tokval(parser), false);
1478 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1479 DEBUGSHUNTDO(con_out("push string\n"));
1481 else if (parser->tok == TOKEN_VECTORCONST) {
1484 parseerror(parser, "expected operator or end of statement, got constant");
1488 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1491 vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
1492 DEBUGSHUNTDO(con_out("push '%g %g %g'\n",
1493 parser_token(parser)->constval.v.x,
1494 parser_token(parser)->constval.v.y,
1495 parser_token(parser)->constval.v.z));
1497 else if (parser->tok == '(') {
1498 parseerror(parser, "internal error: '(' should be classified as operator");
1501 else if (parser->tok == '[') {
1502 parseerror(parser, "internal error: '[' should be classified as operator");
1505 else if (parser->tok == ')') {
1507 DEBUGSHUNTDO(con_out("do[op] )\n"));
1511 /* we do expect an operator next */
1512 /* closing an opening paren */
1513 if (!parser_close_paren(parser, &sy, false))
1516 DEBUGSHUNTDO(con_out("do[nop] )\n"));
1520 /* allowed for function calls */
1521 if (!parser_close_paren(parser, &sy, true))
1526 else if (parser->tok == ']') {
1528 parseerror(parser, "operand expected");
1532 if (!parser_close_paren(parser, &sy, false))
1536 else if (parser->tok != TOKEN_OPERATOR) {
1538 parseerror(parser, "expected operator or end of statement");
1545 /* classify the operator */
1546 const oper_info *op;
1547 const oper_info *olast = NULL;
1549 for (o = 0; o < operator_count; ++o) {
1550 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1551 /* !(operators[o].flags & OP_SUFFIX) && / * remove this */
1552 !strcmp(parser_tokval(parser), operators[o].op))
1557 if (o == operator_count) {
1558 /* no operator found... must be the end of the statement */
1561 /* found an operator */
1564 /* when declaring variables, a comma starts a new variable */
1565 if (op->id == opid1(',') && !parens && stopatcomma) {
1566 /* fixup the token */
1571 /* a colon without a pervious question mark cannot be a ternary */
1572 if (!ternaries && op->id == opid2(':','?')) {
1577 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1578 olast = &operators[vec_last(sy.ops).etype-1];
1581 (op->prec < olast->prec) ||
1582 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1584 if (!parser_sy_pop(parser, &sy))
1586 if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
1587 olast = &operators[vec_last(sy.ops).etype-1];
1592 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1593 /* for gmqcc standard: open up the namespace of the previous type */
1594 ast_expression *prevex = vec_last(sy.out).out;
1596 parseerror(parser, "unexpected member operator");
1599 if (prevex->expression.vtype == TYPE_ENTITY)
1600 parser->memberof = TYPE_ENTITY;
1601 else if (prevex->expression.vtype == TYPE_VECTOR)
1602 parser->memberof = TYPE_VECTOR;
1604 parseerror(parser, "type error: type has no members");
1610 if (op->id == opid1('(')) {
1612 size_t sycount = vec_size(sy.out);
1613 DEBUGSHUNTDO(con_out("push [op] (\n"));
1615 /* we expected an operator, this is the function-call operator */
1616 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_FUNC, sycount-1));
1619 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0));
1620 DEBUGSHUNTDO(con_out("push [nop] (\n"));
1623 } else if (op->id == opid1('[')) {
1625 parseerror(parser, "unexpected array subscript");
1629 /* push both the operator and the paren, this makes life easier */
1630 vec_push(sy.ops, syop(parser_ctx(parser), op));
1631 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
1633 } else if (op->id == opid2('?',':')) {
1635 vec_push(sy.ops, syop(parser_ctx(parser), op));
1636 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_TERNARY, 0));
1639 } else if (op->id == opid2(':','?')) {
1640 if (!parser_close_paren(parser, &sy, false))
1642 vec_push(sy.ops, syop(parser_ctx(parser), op));
1646 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
1647 vec_push(sy.ops, syop(parser_ctx(parser), op));
1648 wantop = !!(op->flags & OP_SUFFIX);
1651 if (!parser_next(parser)) {
1654 if (parser->tok == ';' ||
1655 (!parens && parser->tok == ']'))
1661 while (vec_size(sy.ops)) {
1662 if (!parser_sy_pop(parser, &sy))
1666 parser->lex->flags.noops = true;
1667 if (!vec_size(sy.out)) {
1668 parseerror(parser, "empty expression");
1671 expr = sy.out[0].out;
1674 DEBUGSHUNTDO(con_out("shunt done\n"));
1678 parser->lex->flags.noops = true;
1684 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1686 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1689 if (!parser_next(parser)) {
1696 static void parser_enterblock(parser_t *parser)
1698 vec_push(parser->variables, util_htnew(PARSER_HT_SIZE));
1699 vec_push(parser->_blocklocals, vec_size(parser->_locals));
1700 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
1701 vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
1702 vec_push(parser->_block_ctx, parser_ctx(parser));
1705 static bool parser_leaveblock(parser_t *parser)
1708 size_t locals, typedefs;
1710 if (vec_size(parser->variables) <= PARSER_HT_LOCALS) {
1711 parseerror(parser, "internal error: parser_leaveblock with no block");
1715 util_htdel(vec_last(parser->variables));
1716 vec_pop(parser->variables);
1717 if (!vec_size(parser->_blocklocals)) {
1718 parseerror(parser, "internal error: parser_leaveblock with no block (2)");
1722 locals = vec_last(parser->_blocklocals);
1723 vec_pop(parser->_blocklocals);
1724 while (vec_size(parser->_locals) != locals) {
1725 ast_expression *e = vec_last(parser->_locals);
1726 ast_value *v = (ast_value*)e;
1727 vec_pop(parser->_locals);
1728 if (ast_istype(e, ast_value) && !v->uses) {
1729 if (compile_warning(ast_ctx(v), WARN_UNUSED_VARIABLE, "unused variable: `%s`", v->name)) {
1736 typedefs = vec_last(parser->_blocktypedefs);
1737 while (vec_size(parser->_typedefs) != typedefs) {
1738 ast_delete(vec_last(parser->_typedefs));
1739 vec_pop(parser->_typedefs);
1741 util_htdel(vec_last(parser->typedefs));
1742 vec_pop(parser->typedefs);
1744 vec_pop(parser->_block_ctx);
1748 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e)
1750 vec_push(parser->_locals, e);
1751 util_htset(vec_last(parser->variables), name, (void*)e);
1754 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1757 ast_expression *cond, *ontrue, *onfalse = NULL;
1760 lex_ctx ctx = parser_ctx(parser);
1762 (void)block; /* not touching */
1764 /* skip the 'if', parse an optional 'not' and check for an opening paren */
1765 if (!parser_next(parser)) {
1766 parseerror(parser, "expected condition or 'not'");
1769 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "not")) {
1771 if (!parser_next(parser)) {
1772 parseerror(parser, "expected condition in parenthesis");
1776 if (parser->tok != '(') {
1777 parseerror(parser, "expected 'if' condition in parenthesis");
1780 /* parse into the expression */
1781 if (!parser_next(parser)) {
1782 parseerror(parser, "expected 'if' condition after opening paren");
1785 /* parse the condition */
1786 cond = parse_expression_leave(parser, false);
1790 if (parser->tok != ')') {
1791 parseerror(parser, "expected closing paren after 'if' condition");
1795 /* parse into the 'then' branch */
1796 if (!parser_next(parser)) {
1797 parseerror(parser, "expected statement for on-true branch of 'if'");
1801 if (!parse_statement_or_block(parser, &ontrue)) {
1805 /* check for an else */
1806 if (!strcmp(parser_tokval(parser), "else")) {
1807 /* parse into the 'else' branch */
1808 if (!parser_next(parser)) {
1809 parseerror(parser, "expected on-false branch after 'else'");
1814 if (!parse_statement_or_block(parser, &onfalse)) {
1822 ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue);
1824 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1825 *out = (ast_expression*)ifthen;
1829 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1832 ast_expression *cond, *ontrue;
1834 lex_ctx ctx = parser_ctx(parser);
1836 (void)block; /* not touching */
1838 /* skip the 'while' and check for opening paren */
1839 if (!parser_next(parser) || parser->tok != '(') {
1840 parseerror(parser, "expected 'while' condition in parenthesis");
1843 /* parse into the expression */
1844 if (!parser_next(parser)) {
1845 parseerror(parser, "expected 'while' condition after opening paren");
1848 /* parse the condition */
1849 cond = parse_expression_leave(parser, false);
1853 if (parser->tok != ')') {
1854 parseerror(parser, "expected closing paren after 'while' condition");
1858 /* parse into the 'then' branch */
1859 if (!parser_next(parser)) {
1860 parseerror(parser, "expected while-loop body");
1864 if (!parse_statement_or_block(parser, &ontrue)) {
1869 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1870 *out = (ast_expression*)aloop;
1874 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1877 ast_expression *cond, *ontrue;
1879 lex_ctx ctx = parser_ctx(parser);
1881 (void)block; /* not touching */
1883 /* skip the 'do' and get the body */
1884 if (!parser_next(parser)) {
1885 parseerror(parser, "expected loop body");
1888 if (!parse_statement_or_block(parser, &ontrue))
1891 /* expect the "while" */
1892 if (parser->tok != TOKEN_KEYWORD ||
1893 strcmp(parser_tokval(parser), "while"))
1895 parseerror(parser, "expected 'while' and condition");
1900 /* skip the 'while' and check for opening paren */
1901 if (!parser_next(parser) || parser->tok != '(') {
1902 parseerror(parser, "expected 'while' condition in parenthesis");
1906 /* parse into the expression */
1907 if (!parser_next(parser)) {
1908 parseerror(parser, "expected 'while' condition after opening paren");
1912 /* parse the condition */
1913 cond = parse_expression_leave(parser, false);
1917 if (parser->tok != ')') {
1918 parseerror(parser, "expected closing paren after 'while' condition");
1924 if (!parser_next(parser) || parser->tok != ';') {
1925 parseerror(parser, "expected semicolon after condition");
1931 if (!parser_next(parser)) {
1932 parseerror(parser, "parse error");
1938 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1939 *out = (ast_expression*)aloop;
1943 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1946 ast_expression *initexpr, *cond, *increment, *ontrue;
1950 lex_ctx ctx = parser_ctx(parser);
1952 parser_enterblock(parser);
1959 /* skip the 'while' and check for opening paren */
1960 if (!parser_next(parser) || parser->tok != '(') {
1961 parseerror(parser, "expected 'for' expressions in parenthesis");
1964 /* parse into the expression */
1965 if (!parser_next(parser)) {
1966 parseerror(parser, "expected 'for' initializer after opening paren");
1971 if (parser->tok == TOKEN_IDENT)
1972 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
1974 if (typevar || parser->tok == TOKEN_TYPENAME) {
1975 if (opts_standard != COMPILER_GMQCC) {
1976 if (parsewarning(parser, WARN_EXTENSIONS,
1977 "current standard does not allow variable declarations in for-loop initializers"))
1980 if (!parse_variable(parser, block, true, CV_VAR, typevar))
1983 else if (parser->tok != ';')
1985 initexpr = parse_expression_leave(parser, false);
1990 /* move on to condition */
1991 if (parser->tok != ';') {
1992 parseerror(parser, "expected semicolon after for-loop initializer");
1995 if (!parser_next(parser)) {
1996 parseerror(parser, "expected for-loop condition");
2000 /* parse the condition */
2001 if (parser->tok != ';') {
2002 cond = parse_expression_leave(parser, false);
2007 /* move on to incrementor */
2008 if (parser->tok != ';') {
2009 parseerror(parser, "expected semicolon after for-loop initializer");
2012 if (!parser_next(parser)) {
2013 parseerror(parser, "expected for-loop condition");
2017 /* parse the incrementor */
2018 if (parser->tok != ')') {
2019 increment = parse_expression_leave(parser, false);
2022 if (!ast_side_effects(increment)) {
2023 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2029 if (parser->tok != ')') {
2030 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
2033 /* parse into the 'then' branch */
2034 if (!parser_next(parser)) {
2035 parseerror(parser, "expected for-loop body");
2038 if (!parse_statement_or_block(parser, &ontrue))
2041 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
2042 *out = (ast_expression*)aloop;
2044 if (!parser_leaveblock(parser))
2048 if (initexpr) ast_delete(initexpr);
2049 if (cond) ast_delete(cond);
2050 if (increment) ast_delete(increment);
2051 (void)!parser_leaveblock(parser);
2055 static bool parse_return(parser_t *parser, ast_block *block, ast_expression **out)
2057 ast_expression *exp = NULL;
2058 ast_return *ret = NULL;
2059 ast_value *expected = parser->function->vtype;
2061 lex_ctx ctx = parser_ctx(parser);
2063 (void)block; /* not touching */
2065 if (!parser_next(parser)) {
2066 parseerror(parser, "expected return expression");
2070 if (parser->tok != ';') {
2071 exp = parse_expression(parser, false);
2075 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
2076 parseerror(parser, "return with invalid expression");
2079 ret = ast_return_new(exp->expression.node.context, exp);
2085 if (!parser_next(parser))
2086 parseerror(parser, "parse error");
2087 if (expected->expression.next->expression.vtype != TYPE_VOID) {
2088 if (opts_standard != COMPILER_GMQCC)
2089 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
2091 parseerror(parser, "return without value");
2093 ret = ast_return_new(ctx, NULL);
2095 *out = (ast_expression*)ret;
2099 static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
2101 lex_ctx ctx = parser_ctx(parser);
2103 (void)block; /* not touching */
2105 if (!parser_next(parser) || parser->tok != ';') {
2106 parseerror(parser, "expected semicolon");
2110 if (!parser_next(parser))
2111 parseerror(parser, "parse error");
2113 *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
2117 static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
2119 ast_expression *operand;
2122 ast_switch *switchnode;
2123 ast_switch_case swcase;
2125 lex_ctx ctx = parser_ctx(parser);
2127 (void)block; /* not touching */
2130 /* parse over the opening paren */
2131 if (!parser_next(parser) || parser->tok != '(') {
2132 parseerror(parser, "expected switch operand in parenthesis");
2136 /* parse into the expression */
2137 if (!parser_next(parser)) {
2138 parseerror(parser, "expected switch operand");
2141 /* parse the operand */
2142 operand = parse_expression_leave(parser, false);
2146 switchnode = ast_switch_new(ctx, operand);
2149 if (parser->tok != ')') {
2150 ast_delete(switchnode);
2151 parseerror(parser, "expected closing paren after 'switch' operand");
2155 /* parse over the opening paren */
2156 if (!parser_next(parser) || parser->tok != '{') {
2157 ast_delete(switchnode);
2158 parseerror(parser, "expected list of cases");
2162 if (!parser_next(parser)) {
2163 ast_delete(switchnode);
2164 parseerror(parser, "expected 'case' or 'default'");
2168 /* new block; allow some variables to be declared here */
2169 parser_enterblock(parser);
2172 if (parser->tok == TOKEN_IDENT)
2173 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2174 if (typevar || parser->tok == TOKEN_TYPENAME) {
2175 if (!parse_variable(parser, block, false, CV_NONE, typevar)) {
2176 ast_delete(switchnode);
2181 if (!strcmp(parser_tokval(parser), "var") ||
2182 !strcmp(parser_tokval(parser), "local"))
2184 if (!parser_next(parser)) {
2185 parseerror(parser, "expected variable declaration");
2186 ast_delete(switchnode);
2189 if (!parse_variable(parser, block, false, CV_VAR, NULL)) {
2190 ast_delete(switchnode);
2195 if (!strcmp(parser_tokval(parser), "const")) {
2196 if (!parser_next(parser)) {
2197 parseerror(parser, "expected variable declaration");
2198 ast_delete(switchnode);
2201 if (!strcmp(parser_tokval(parser), "var")) {
2202 if (!parser_next(parser)) {
2203 parseerror(parser, "expected variable declaration");
2204 ast_delete(switchnode);
2208 if (!parse_variable(parser, block, false, CV_CONST, NULL)) {
2209 ast_delete(switchnode);
2218 while (parser->tok != '}') {
2219 ast_block *caseblock;
2221 if (parser->tok != TOKEN_KEYWORD) {
2222 ast_delete(switchnode);
2223 parseerror(parser, "expected 'case' or 'default'");
2226 if (!strcmp(parser_tokval(parser), "case")) {
2227 if (!parser_next(parser)) {
2228 ast_delete(switchnode);
2229 parseerror(parser, "expected expression for case");
2232 swcase.value = parse_expression_leave(parser, false);
2233 if (!swcase.value) {
2234 ast_delete(switchnode);
2235 parseerror(parser, "expected expression for case");
2238 if (!OPTS_FLAG(RELAXED_SWITCH)) {
2239 opval = (ast_value*)swcase.value;
2240 if (!ast_istype(swcase.value, ast_value)) { /* || opval->cvq != CV_CONST) { */
2241 parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
2247 else if (!strcmp(parser_tokval(parser), "default")) {
2248 swcase.value = NULL;
2249 if (!parser_next(parser)) {
2250 ast_delete(switchnode);
2251 parseerror(parser, "expected colon");
2256 /* Now the colon and body */
2257 if (parser->tok != ':') {
2258 if (swcase.value) ast_unref(swcase.value);
2259 ast_delete(switchnode);
2260 parseerror(parser, "expected colon");
2264 if (!parser_next(parser)) {
2265 if (swcase.value) ast_unref(swcase.value);
2266 ast_delete(switchnode);
2267 parseerror(parser, "expected statements or case");
2270 caseblock = ast_block_new(parser_ctx(parser));
2272 if (swcase.value) ast_unref(swcase.value);
2273 ast_delete(switchnode);
2276 swcase.code = (ast_expression*)caseblock;
2277 vec_push(switchnode->cases, swcase);
2279 ast_expression *expr;
2280 if (parser->tok == '}')
2282 if (parser->tok == TOKEN_KEYWORD) {
2283 if (!strcmp(parser_tokval(parser), "case") ||
2284 !strcmp(parser_tokval(parser), "default"))
2289 if (!parse_statement(parser, caseblock, &expr, true)) {
2290 ast_delete(switchnode);
2295 ast_block_add_expr(caseblock, expr);
2299 parser_leaveblock(parser);
2302 if (parser->tok != '}') {
2303 ast_delete(switchnode);
2304 parseerror(parser, "expected closing paren of case list");
2307 if (!parser_next(parser)) {
2308 ast_delete(switchnode);
2309 parseerror(parser, "parse error after switch");
2312 *out = (ast_expression*)switchnode;
2316 static bool parse_goto(parser_t *parser, ast_expression **out)
2321 if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
2322 parseerror(parser, "expected label name after `goto`");
2326 gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
2328 for (i = 0; i < vec_size(parser->labels); ++i) {
2329 if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
2330 ast_goto_set_label(gt, parser->labels[i]);
2334 if (i == vec_size(parser->labels))
2335 vec_push(parser->gotos, gt);
2337 if (!parser_next(parser) || parser->tok != ';') {
2338 parseerror(parser, "semicolon expected after goto label");
2341 if (!parser_next(parser)) {
2342 parseerror(parser, "parse error after goto");
2346 *out = (ast_expression*)gt;
2350 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
2353 ast_value *typevar = NULL;
2356 if (parser->tok == TOKEN_IDENT)
2357 typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
2359 if (typevar || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
2361 /* local variable */
2363 parseerror(parser, "cannot declare a variable from here");
2366 if (opts_standard == COMPILER_QCC) {
2367 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
2370 if (!parse_variable(parser, block, false, CV_NONE, typevar))
2375 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
2379 else if (parser->tok == TOKEN_KEYWORD)
2381 if (!strcmp(parser_tokval(parser), "local") ||
2382 !strcmp(parser_tokval(parser), "const") ||
2383 !strcmp(parser_tokval(parser), "var"))
2386 if (parser_tokval(parser)[0] == 'c')
2388 else if (parser_tokval(parser)[0] == 'v')
2394 parseerror(parser, "cannot declare a local variable here");
2397 if (!parser_next(parser)) {
2398 parseerror(parser, "expected variable declaration");
2401 if (!parse_variable(parser, block, true, cvq, NULL))
2406 else if (!strcmp(parser_tokval(parser), "__builtin_debug_printtype"))
2411 if (!parser_next(parser)) {
2412 parseerror(parser, "parse error after __builtin_debug_printtype");
2416 if (parser->tok == TOKEN_IDENT && (tdef = parser_find_typedef(parser, parser_tokval(parser), 0)))
2418 ast_type_to_string((ast_expression*)tdef, ty, sizeof(ty));
2419 con_out("__builtin_debug_printtype: `%s`=`%s`\n", tdef->name, ty);
2420 if (!parser_next(parser)) {
2421 parseerror(parser, "parse error after __builtin_debug_printtype typename argument");
2427 if (!parse_statement(parser, block, out, allow_cases))
2430 con_out("__builtin_debug_printtype: got no output node\n");
2433 ast_type_to_string(*out, ty, sizeof(ty));
2434 con_out("__builtin_debug_printtype: `%s`\n", ty);
2439 else if (!strcmp(parser_tokval(parser), "return"))
2441 return parse_return(parser, block, out);
2443 else if (!strcmp(parser_tokval(parser), "if"))
2445 return parse_if(parser, block, out);
2447 else if (!strcmp(parser_tokval(parser), "while"))
2449 return parse_while(parser, block, out);
2451 else if (!strcmp(parser_tokval(parser), "do"))
2453 return parse_dowhile(parser, block, out);
2455 else if (!strcmp(parser_tokval(parser), "for"))
2457 if (opts_standard == COMPILER_QCC) {
2458 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
2461 return parse_for(parser, block, out);
2463 else if (!strcmp(parser_tokval(parser), "break"))
2465 return parse_break_continue(parser, block, out, false);
2467 else if (!strcmp(parser_tokval(parser), "continue"))
2469 return parse_break_continue(parser, block, out, true);
2471 else if (!strcmp(parser_tokval(parser), "switch"))
2473 return parse_switch(parser, block, out);
2475 else if (!strcmp(parser_tokval(parser), "case") ||
2476 !strcmp(parser_tokval(parser), "default"))
2479 parseerror(parser, "unexpected 'case' label");
2484 else if (!strcmp(parser_tokval(parser), "goto"))
2486 return parse_goto(parser, out);
2488 else if (!strcmp(parser_tokval(parser), "typedef"))
2490 if (!parser_next(parser)) {
2491 parseerror(parser, "expected type definition after 'typedef'");
2494 return parse_typedef(parser);
2496 parseerror(parser, "Unexpected keyword");
2499 else if (parser->tok == '{')
2502 inner = parse_block(parser);
2505 *out = (ast_expression*)inner;
2508 else if (parser->tok == ':')
2512 if (!parser_next(parser)) {
2513 parseerror(parser, "expected label name");
2516 if (parser->tok != TOKEN_IDENT) {
2517 parseerror(parser, "label must be an identifier");
2520 label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
2523 vec_push(parser->labels, label);
2524 *out = (ast_expression*)label;
2525 if (!parser_next(parser)) {
2526 parseerror(parser, "parse error after label");
2529 for (i = 0; i < vec_size(parser->gotos); ++i) {
2530 if (!strcmp(parser->gotos[i]->name, label->name)) {
2531 ast_goto_set_label(parser->gotos[i], label);
2532 vec_remove(parser->gotos, i, 1);
2538 else if (parser->tok == ';')
2540 if (!parser_next(parser)) {
2541 parseerror(parser, "parse error after empty statement");
2548 ast_expression *exp = parse_expression(parser, false);
2552 if (!ast_side_effects(exp)) {
2553 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
2560 static bool parse_block_into(parser_t *parser, ast_block *block)
2564 parser_enterblock(parser);
2566 if (!parser_next(parser)) { /* skip the '{' */
2567 parseerror(parser, "expected function body");
2571 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2573 ast_expression *expr = NULL;
2574 if (parser->tok == '}')
2577 if (!parse_statement(parser, block, &expr, false)) {
2578 /* parseerror(parser, "parse error"); */
2584 ast_block_add_expr(block, expr);
2587 if (parser->tok != '}') {
2590 (void)parser_next(parser);
2594 if (!parser_leaveblock(parser))
2596 return retval && !!block;
2599 static ast_block* parse_block(parser_t *parser)
2602 block = ast_block_new(parser_ctx(parser));
2605 if (!parse_block_into(parser, block)) {
2606 ast_block_delete(block);
2612 static bool parse_statement_or_block(parser_t *parser, ast_expression **out)
2614 if (parser->tok == '{') {
2615 *out = (ast_expression*)parse_block(parser);
2618 return parse_statement(parser, NULL, out, false);
2621 static bool create_vector_members(ast_value *var, ast_member **me)
2624 size_t len = strlen(var->name);
2626 for (i = 0; i < 3; ++i) {
2627 char *name = mem_a(len+3);
2628 memcpy(name, var->name, len);
2630 name[len+1] = 'x'+i;
2632 me[i] = ast_member_new(ast_ctx(var), (ast_expression*)var, i, name);
2641 do { ast_member_delete(me[--i]); } while(i);
2645 static bool parse_function_body(parser_t *parser, ast_value *var)
2647 ast_block *block = NULL;
2652 ast_expression *framenum = NULL;
2653 ast_expression *nextthink = NULL;
2654 /* None of the following have to be deleted */
2655 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
2656 ast_expression *gbl_time = NULL, *gbl_self = NULL;
2657 bool has_frame_think;
2661 has_frame_think = false;
2662 old = parser->function;
2664 if (vec_size(parser->gotos) || vec_size(parser->labels)) {
2665 parseerror(parser, "gotos/labels leaking");
2669 if (var->expression.variadic) {
2670 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
2671 "variadic function with implementation will not be able to access additional parameters"))
2677 if (parser->tok == '[') {
2678 /* got a frame definition: [ framenum, nextthink ]
2679 * this translates to:
2680 * self.frame = framenum;
2681 * self.nextthink = time + 0.1;
2682 * self.think = nextthink;
2686 fld_think = parser_find_field(parser, "think");
2687 fld_nextthink = parser_find_field(parser, "nextthink");
2688 fld_frame = parser_find_field(parser, "frame");
2689 if (!fld_think || !fld_nextthink || !fld_frame) {
2690 parseerror(parser, "cannot use [frame,think] notation without the required fields");
2691 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
2694 gbl_time = parser_find_global(parser, "time");
2695 gbl_self = parser_find_global(parser, "self");
2696 if (!gbl_time || !gbl_self) {
2697 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2698 parseerror(parser, "please declare the following globals: `time`, `self`");
2702 if (!parser_next(parser))
2705 framenum = parse_expression_leave(parser, true);
2707 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2710 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->hasvalue) {
2711 ast_unref(framenum);
2712 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2716 if (parser->tok != ',') {
2717 ast_unref(framenum);
2718 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2719 parseerror(parser, "Got a %i\n", parser->tok);
2723 if (!parser_next(parser)) {
2724 ast_unref(framenum);
2728 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2730 /* qc allows the use of not-yet-declared functions here
2731 * - this automatically creates a prototype */
2732 ast_value *thinkfunc;
2733 ast_expression *functype = fld_think->expression.next;
2735 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2736 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2737 ast_unref(framenum);
2738 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2742 if (!parser_next(parser)) {
2743 ast_unref(framenum);
2744 ast_delete(thinkfunc);
2748 vec_push(parser->globals, (ast_expression*)thinkfunc);
2749 util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
2750 nextthink = (ast_expression*)thinkfunc;
2753 nextthink = parse_expression_leave(parser, true);
2755 ast_unref(framenum);
2756 parseerror(parser, "expected a think-function in [frame,think] notation");
2761 if (!ast_istype(nextthink, ast_value)) {
2762 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2766 if (retval && parser->tok != ']') {
2767 parseerror(parser, "expected closing `]` for [frame,think] notation");
2771 if (retval && !parser_next(parser)) {
2775 if (retval && parser->tok != '{') {
2776 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2781 ast_unref(nextthink);
2782 ast_unref(framenum);
2786 has_frame_think = true;
2789 block = ast_block_new(parser_ctx(parser));
2791 parseerror(parser, "failed to allocate block");
2792 if (has_frame_think) {
2793 ast_unref(nextthink);
2794 ast_unref(framenum);
2799 if (has_frame_think) {
2801 ast_expression *self_frame;
2802 ast_expression *self_nextthink;
2803 ast_expression *self_think;
2804 ast_expression *time_plus_1;
2805 ast_store *store_frame;
2806 ast_store *store_nextthink;
2807 ast_store *store_think;
2809 ctx = parser_ctx(parser);
2810 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2811 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2812 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2814 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2815 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2817 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2818 if (self_frame) ast_delete(self_frame);
2819 if (self_nextthink) ast_delete(self_nextthink);
2820 if (self_think) ast_delete(self_think);
2821 if (time_plus_1) ast_delete(time_plus_1);
2827 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2828 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2829 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2832 ast_delete(self_frame);
2835 if (!store_nextthink) {
2836 ast_delete(self_nextthink);
2840 ast_delete(self_think);
2844 if (store_frame) ast_delete(store_frame);
2845 if (store_nextthink) ast_delete(store_nextthink);
2846 if (store_think) ast_delete(store_think);
2849 ast_block_add_expr(block, (ast_expression*)store_frame);
2850 ast_block_add_expr(block, (ast_expression*)store_nextthink);
2851 ast_block_add_expr(block, (ast_expression*)store_think);
2855 parseerror(parser, "failed to generate code for [frame,think]");
2856 ast_unref(nextthink);
2857 ast_unref(framenum);
2863 parser_enterblock(parser);
2865 for (parami = 0; parami < vec_size(var->expression.params); ++parami) {
2867 ast_value *param = var->expression.params[parami];
2870 if (param->expression.vtype != TYPE_VECTOR &&
2871 (param->expression.vtype != TYPE_FIELD ||
2872 param->expression.next->expression.vtype != TYPE_VECTOR))
2877 if (!create_vector_members(param, me)) {
2878 ast_block_delete(block);
2882 for (e = 0; e < 3; ++e) {
2883 parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
2884 ast_block_collect(block, (ast_expression*)me[e]);
2888 func = ast_function_new(ast_ctx(var), var->name, var);
2890 parseerror(parser, "failed to allocate function for `%s`", var->name);
2891 ast_block_delete(block);
2894 vec_push(parser->functions, func);
2896 parser->function = func;
2897 if (!parse_block_into(parser, block)) {
2898 ast_block_delete(block);
2902 vec_push(func->blocks, block);
2904 parser->function = old;
2905 if (!parser_leaveblock(parser))
2907 if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
2908 parseerror(parser, "internal error: local scopes left");
2912 if (parser->tok == ';')
2913 return parser_next(parser);
2914 else if (opts_standard == COMPILER_QCC)
2915 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2919 vec_pop(parser->functions);
2920 ast_function_delete(func);
2921 var->constval.vfunc = NULL;
2924 (void)!parser_leaveblock(parser);
2925 parser->function = old;
2929 static ast_expression *array_accessor_split(
2934 ast_expression *left,
2935 ast_expression *right
2941 lex_ctx ctx = ast_ctx(array);
2943 if (!left || !right) {
2944 if (left) ast_delete(left);
2945 if (right) ast_delete(right);
2949 cmp = ast_binary_new(ctx, INSTR_LT,
2950 (ast_expression*)index,
2951 (ast_expression*)parser_const_float(parser, middle));
2955 parseerror(parser, "internal error: failed to create comparison for array setter");
2959 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2961 ast_delete(cmp); /* will delete left and right */
2962 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2966 return (ast_expression*)ifthen;
2969 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2971 lex_ctx ctx = ast_ctx(array);
2973 if (from+1 == afterend) {
2974 /* set this value */
2977 ast_array_index *subscript;
2979 int assignop = type_store_instr[value->expression.vtype];
2981 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2982 assignop = INSTR_STORE_V;
2984 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2988 st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2990 ast_delete(subscript);
2994 block = ast_block_new(ctx);
3000 ast_block_add_expr(block, (ast_expression*)st);
3002 ret = ast_return_new(ctx, NULL);
3008 ast_block_add_expr(block, (ast_expression*)ret);
3010 return (ast_expression*)block;
3012 ast_expression *left, *right;
3013 size_t diff = afterend - from;
3014 size_t middle = from + diff/2;
3015 left = array_setter_node(parser, array, index, value, from, middle);
3016 right = array_setter_node(parser, array, index, value, middle, afterend);
3017 return array_accessor_split(parser, array, index, middle, left, right);
3021 static ast_expression *array_field_setter_node(
3030 lex_ctx ctx = ast_ctx(array);
3032 if (from+1 == afterend) {
3033 /* set this value */
3036 ast_entfield *entfield;
3037 ast_array_index *subscript;
3039 int assignop = type_storep_instr[value->expression.vtype];
3041 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
3042 assignop = INSTR_STOREP_V;
3044 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3048 entfield = ast_entfield_new_force(ctx,
3049 (ast_expression*)entity,
3050 (ast_expression*)subscript,
3051 (ast_expression*)subscript);
3053 ast_delete(subscript);
3057 st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
3059 ast_delete(entfield);
3063 block = ast_block_new(ctx);
3069 ast_block_add_expr(block, (ast_expression*)st);
3071 ret = ast_return_new(ctx, NULL);
3077 ast_block_add_expr(block, (ast_expression*)ret);
3079 return (ast_expression*)block;
3081 ast_expression *left, *right;
3082 size_t diff = afterend - from;
3083 size_t middle = from + diff/2;
3084 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
3085 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
3086 return array_accessor_split(parser, array, index, middle, left, right);
3090 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
3092 lex_ctx ctx = ast_ctx(array);
3094 if (from+1 == afterend) {
3096 ast_array_index *subscript;
3098 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
3102 ret = ast_return_new(ctx, (ast_expression*)subscript);
3104 ast_delete(subscript);
3108 return (ast_expression*)ret;
3110 ast_expression *left, *right;
3111 size_t diff = afterend - from;
3112 size_t middle = from + diff/2;
3113 left = array_getter_node(parser, array, index, from, middle);
3114 right = array_getter_node(parser, array, index, middle, afterend);
3115 return array_accessor_split(parser, array, index, middle, left, right);
3119 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
3121 ast_function *func = NULL;
3122 ast_value *fval = NULL;
3123 ast_block *body = NULL;
3125 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
3127 parseerror(parser, "failed to create accessor function value");
3131 func = ast_function_new(ast_ctx(array), funcname, fval);
3134 parseerror(parser, "failed to create accessor function node");
3138 body = ast_block_new(ast_ctx(array));
3140 parseerror(parser, "failed to create block for array accessor");
3146 vec_push(func->blocks, body);
3149 vec_push(parser->accessors, fval);
3154 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
3156 ast_expression *root = NULL;
3157 ast_value *index = NULL;
3158 ast_value *value = NULL;
3162 if (!ast_istype(array->expression.next, ast_value)) {
3163 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3167 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3169 func = fval->constval.vfunc;
3170 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3172 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3173 value = ast_value_copy((ast_value*)array->expression.next);
3175 if (!index || !value) {
3176 parseerror(parser, "failed to create locals for array accessor");
3179 (void)!ast_value_set_name(value, "value"); /* not important */
3180 vec_push(fval->expression.params, index);
3181 vec_push(fval->expression.params, value);
3183 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
3185 parseerror(parser, "failed to build accessor search tree");
3189 ast_block_add_expr(func->blocks[0], root);
3190 array->setter = fval;
3193 if (index) ast_delete(index);
3194 if (value) ast_delete(value);
3195 if (root) ast_delete(root);
3201 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
3203 ast_expression *root = NULL;
3204 ast_value *entity = NULL;
3205 ast_value *index = NULL;
3206 ast_value *value = NULL;
3210 if (!ast_istype(array->expression.next, ast_value)) {
3211 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3215 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3217 func = fval->constval.vfunc;
3218 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
3220 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
3221 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3222 value = ast_value_copy((ast_value*)array->expression.next);
3223 if (!entity || !index || !value) {
3224 parseerror(parser, "failed to create locals for array accessor");
3227 (void)!ast_value_set_name(value, "value"); /* not important */
3228 vec_push(fval->expression.params, entity);
3229 vec_push(fval->expression.params, index);
3230 vec_push(fval->expression.params, value);
3232 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
3234 parseerror(parser, "failed to build accessor search tree");
3238 ast_block_add_expr(func->blocks[0], root);
3239 array->setter = fval;
3242 if (entity) ast_delete(entity);
3243 if (index) ast_delete(index);
3244 if (value) ast_delete(value);
3245 if (root) ast_delete(root);
3251 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
3253 ast_expression *root = NULL;
3254 ast_value *index = NULL;
3258 /* NOTE: checking array->expression.next rather than elemtype since
3259 * for fields elemtype is a temporary fieldtype.
3261 if (!ast_istype(array->expression.next, ast_value)) {
3262 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
3266 if (!parser_create_array_accessor(parser, array, funcname, &fval))
3268 func = fval->constval.vfunc;
3269 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
3271 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
3274 parseerror(parser, "failed to create locals for array accessor");
3277 vec_push(fval->expression.params, index);
3279 root = array_getter_node(parser, array, index, 0, array->expression.count);
3281 parseerror(parser, "failed to build accessor search tree");
3285 ast_block_add_expr(func->blocks[0], root);
3286 array->getter = fval;
3289 if (index) ast_delete(index);
3290 if (root) ast_delete(root);
3296 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
3297 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
3305 bool variadic = false;
3307 ctx = parser_ctx(parser);
3309 /* for the sake of less code we parse-in in this function */
3310 if (!parser_next(parser)) {
3311 parseerror(parser, "expected parameter list");
3317 /* parse variables until we hit a closing paren */
3318 while (parser->tok != ')') {
3320 /* there must be commas between them */
3321 if (parser->tok != ',') {
3322 parseerror(parser, "expected comma or end of parameter list");
3325 if (!parser_next(parser)) {
3326 parseerror(parser, "expected parameter");
3332 if (parser->tok == TOKEN_DOTS) {
3333 /* '...' indicates a varargs function */
3335 if (!parser_next(parser)) {
3336 parseerror(parser, "expected parameter");
3339 if (parser->tok != ')') {
3340 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
3346 /* for anything else just parse a typename */
3347 param = parse_typename(parser, NULL, NULL);
3350 vec_push(params, param);
3351 if (param->expression.vtype >= TYPE_VARIANT) {
3352 char typename[1024];
3353 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
3354 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
3360 if (vec_size(params) == 1 && params[0]->expression.vtype == TYPE_VOID)
3364 if (vec_size(params) > 8 && opts_standard == COMPILER_QCC)
3365 (void)!parsewarning(parser, WARN_EXTENSIONS, "more than 8 parameters are not supported by this standard");
3368 if (!parser_next(parser)) {
3369 parseerror(parser, "parse error after typename");
3373 /* now turn 'var' into a function type */
3374 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
3375 fval->expression.next = (ast_expression*)var;
3376 fval->expression.variadic = variadic;
3379 var->expression.params = params;
3386 for (i = 0; i < vec_size(params); ++i)
3387 ast_delete(params[i]);
3392 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
3394 ast_expression *cexp;
3395 ast_value *cval, *tmp;
3398 ctx = parser_ctx(parser);
3400 if (!parser_next(parser)) {
3402 parseerror(parser, "expected array-size");
3406 cexp = parse_expression_leave(parser, true);
3408 if (!cexp || !ast_istype(cexp, ast_value)) {
3412 parseerror(parser, "expected array-size as constant positive integer");
3415 cval = (ast_value*)cexp;
3417 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
3418 tmp->expression.next = (ast_expression*)var;
3421 if (cval->expression.vtype == TYPE_INTEGER)
3422 tmp->expression.count = cval->constval.vint;
3423 else if (cval->expression.vtype == TYPE_FLOAT)
3424 tmp->expression.count = cval->constval.vfloat;
3428 parseerror(parser, "array-size must be a positive integer constant");
3433 if (parser->tok != ']') {
3435 parseerror(parser, "expected ']' after array-size");
3438 if (!parser_next(parser)) {
3440 parseerror(parser, "error after parsing array size");
3446 /* Parse a complete typename.
3447 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
3448 * but when parsing variables separated by comma
3449 * 'storebase' should point to where the base-type should be kept.
3450 * The base type makes up every bit of type information which comes *before* the
3453 * The following will be parsed in its entirety:
3455 * The 'basetype' in this case is 'void()'
3456 * and if there's a comma after it, say:
3458 * then the type-information 'void()' can be stored in 'storebase'
3460 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef)
3462 ast_value *var, *tmp;
3465 const char *name = NULL;
3466 bool isfield = false;
3467 bool wasarray = false;
3468 size_t morefields = 0;
3470 ctx = parser_ctx(parser);
3472 /* types may start with a dot */
3473 if (parser->tok == '.') {
3475 /* if we parsed a dot we need a typename now */
3476 if (!parser_next(parser)) {
3477 parseerror(parser, "expected typename for field definition");
3481 /* Further dots are handled seperately because they won't be part of the
3484 while (parser->tok == '.') {
3486 if (!parser_next(parser)) {
3487 parseerror(parser, "expected typename for field definition");
3492 if (parser->tok == TOKEN_IDENT)
3493 cached_typedef = parser_find_typedef(parser, parser_tokval(parser), 0);
3494 if (!cached_typedef && parser->tok != TOKEN_TYPENAME) {
3495 parseerror(parser, "expected typename");
3499 /* generate the basic type value */
3500 if (cached_typedef) {
3501 var = ast_value_copy(cached_typedef);
3502 ast_value_set_name(var, "<type(from_def)>");
3504 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
3506 for (; morefields; --morefields) {
3507 tmp = ast_value_new(ctx, "<.type>", TYPE_FIELD);
3508 tmp->expression.next = (ast_expression*)var;
3512 /* do not yet turn into a field - remember:
3513 * .void() foo; is a field too
3514 * .void()() foo; is a function
3518 if (!parser_next(parser)) {
3520 parseerror(parser, "parse error after typename");
3524 /* an opening paren now starts the parameter-list of a function
3525 * this is where original-QC has parameter lists.
3526 * We allow a single parameter list here.
3527 * Much like fteqcc we don't allow `float()() x`
3529 if (parser->tok == '(') {
3530 var = parse_parameter_list(parser, var);
3535 /* store the base if requested */
3537 *storebase = ast_value_copy(var);
3539 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3540 tmp->expression.next = (ast_expression*)*storebase;
3545 /* there may be a name now */
3546 if (parser->tok == TOKEN_IDENT) {
3547 name = util_strdup(parser_tokval(parser));
3549 if (!parser_next(parser)) {
3551 parseerror(parser, "error after variable or field declaration");
3556 /* now this may be an array */
3557 if (parser->tok == '[') {
3559 var = parse_arraysize(parser, var);
3564 /* This is the point where we can turn it into a field */
3566 /* turn it into a field if desired */
3567 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
3568 tmp->expression.next = (ast_expression*)var;
3572 /* now there may be function parens again */
3573 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
3574 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3575 if (parser->tok == '(' && wasarray)
3576 parseerror(parser, "arrays as part of a return type is not supported");
3577 while (parser->tok == '(') {
3578 var = parse_parameter_list(parser, var);
3587 /* finally name it */
3589 if (!ast_value_set_name(var, name)) {
3591 parseerror(parser, "internal error: failed to set name");
3594 /* free the name, ast_value_set_name duplicates */
3601 static bool parse_typedef(parser_t *parser)
3603 ast_value *typevar, *oldtype;
3604 ast_expression *old;
3606 typevar = parse_typename(parser, NULL, NULL);
3611 if ( (old = parser_find_var(parser, typevar->name)) ) {
3612 parseerror(parser, "cannot define a type with the same name as a variable: %s\n"
3613 " -> `%s` has been declared here: %s:%i",
3614 typevar->name, ast_ctx(old).file, ast_ctx(old).line);
3615 ast_delete(typevar);
3619 if ( (oldtype = parser_find_typedef(parser, typevar->name, vec_last(parser->_blocktypedefs))) ) {
3620 parseerror(parser, "type `%s` has already been declared here: %s:%i",
3621 typevar->name, ast_ctx(oldtype).file, ast_ctx(oldtype).line);
3622 ast_delete(typevar);
3626 vec_push(parser->_typedefs, typevar);
3627 util_htset(vec_last(parser->typedefs), typevar->name, typevar);
3629 if (parser->tok != ';') {
3630 parseerror(parser, "expected semicolon after typedef");
3633 if (!parser_next(parser)) {
3634 parseerror(parser, "parse error after typedef");
3641 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef)
3645 ast_expression *old;
3649 ast_value *basetype = NULL;
3651 bool isparam = false;
3652 bool isvector = false;
3653 bool cleanvar = true;
3654 bool wasarray = false;
3658 /* get the first complete variable */
3659 var = parse_typename(parser, &basetype, cached_typedef);
3662 ast_delete(basetype);
3670 /* Part 0: finish the type */
3671 if (parser->tok == '(') {
3672 if (opts_standard == COMPILER_QCC)
3673 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3674 var = parse_parameter_list(parser, var);
3680 /* we only allow 1-dimensional arrays */
3681 if (parser->tok == '[') {
3683 var = parse_arraysize(parser, var);
3689 if (parser->tok == '(' && wasarray) {
3690 parseerror(parser, "arrays as part of a return type is not supported");
3691 /* we'll still parse the type completely for now */
3693 /* for functions returning functions */
3694 while (parser->tok == '(') {
3695 if (opts_standard == COMPILER_QCC)
3696 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3697 var = parse_parameter_list(parser, var);
3704 var->cvq = qualifier;
3707 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3708 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3709 * is then filled with the previous definition and the parameter-names replaced.
3712 /* Deal with end_sys_ vars */
3714 if (!strcmp(var->name, "end_sys_globals")) {
3715 parser->crc_globals = vec_size(parser->globals);
3718 else if (!strcmp(var->name, "end_sys_fields")) {
3719 parser->crc_fields = vec_size(parser->fields);
3722 if (was_end && var->expression.vtype == TYPE_FIELD) {
3723 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3724 "global '%s' hint should not be a field",
3725 parser_tokval(parser)))
3732 if (!nofields && var->expression.vtype == TYPE_FIELD)
3734 /* deal with field declarations */
3735 old = parser_find_field(parser, var->name);
3737 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3738 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3747 parseerror(parser, "field `%s` already declared here: %s:%i",
3748 var->name, ast_ctx(old).file, ast_ctx(old).line);
3753 if (opts_standard == COMPILER_QCC &&
3754 (old = parser_find_global(parser, var->name)))
3756 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3757 parseerror(parser, "field `%s` already declared here: %s:%i",
3758 var->name, ast_ctx(old).file, ast_ctx(old).line);
3765 /* deal with other globals */
3766 old = parser_find_global(parser, var->name);
3767 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3769 /* This is a function which had a prototype */
3770 if (!ast_istype(old, ast_value)) {
3771 parseerror(parser, "internal error: prototype is not an ast_value");
3775 proto = (ast_value*)old;
3776 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3777 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3779 ast_ctx(proto).file, ast_ctx(proto).line);
3783 /* we need the new parameter-names */
3784 for (i = 0; i < vec_size(proto->expression.params); ++i)
3785 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3793 if (opts_standard == COMPILER_GMQCC) {
3794 parseerror(parser, "global `%s` already declared here: %s:%i",
3795 var->name, ast_ctx(old).file, ast_ctx(old).line);
3799 if (parsewarning(parser, WARN_DOUBLE_DECLARATION,
3800 "global `%s` already declared here: %s:%i",
3801 var->name, ast_ctx(old).file, ast_ctx(old).line))
3806 proto = (ast_value*)old;
3807 if (!ast_istype(old, ast_value)) {
3808 parseerror(parser, "internal error: not an ast_value");
3817 if (opts_standard == COMPILER_QCC &&
3818 (old = parser_find_field(parser, var->name)))
3820 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3821 parseerror(parser, "global `%s` already declared here: %s:%i",
3822 var->name, ast_ctx(old).file, ast_ctx(old).line);
3829 else /* it's not a global */
3831 old = parser_find_local(parser, var->name, vec_size(parser->variables)-1, &isparam);
3832 if (old && !isparam) {
3833 parseerror(parser, "local `%s` already declared here: %s:%i",
3834 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3838 old = parser_find_local(parser, var->name, 0, &isparam);
3839 if (old && isparam) {
3840 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3841 "local `%s` is shadowing a parameter", var->name))
3843 parseerror(parser, "local `%s` already declared here: %s:%i",
3844 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3848 if (opts_standard != COMPILER_GMQCC) {
3857 * Create the global/local, and deal with vector types.
3860 if (var->expression.vtype == TYPE_VECTOR)
3862 else if (var->expression.vtype == TYPE_FIELD &&
3863 var->expression.next->expression.vtype == TYPE_VECTOR)
3867 if (!create_vector_members(var, me)) {
3874 /* deal with global variables, fields, functions */
3875 if (!nofields && var->expression.vtype == TYPE_FIELD && parser->tok != '=') {
3876 var->isfield = true;
3877 vec_push(parser->fields, (ast_expression*)var);
3878 util_htset(parser->htfields, var->name, var);
3880 for (i = 0; i < 3; ++i) {
3881 vec_push(parser->fields, (ast_expression*)me[i]);
3882 util_htset(parser->htfields, me[i]->name, me[i]);
3887 vec_push(parser->globals, (ast_expression*)var);
3888 util_htset(parser->htglobals, var->name, var);
3890 for (i = 0; i < 3; ++i) {
3891 vec_push(parser->globals, (ast_expression*)me[i]);
3892 util_htset(parser->htglobals, me[i]->name, me[i]);
3897 vec_push(localblock->locals, var);
3898 parser_addlocal(parser, var->name, (ast_expression*)var);
3900 for (i = 0; i < 3; ++i) {
3901 parser_addlocal(parser, me[i]->name, (ast_expression*)me[i]);
3902 ast_block_collect(localblock, (ast_expression*)me[i]);
3908 me[0] = me[1] = me[2] = NULL;
3913 if (var->expression.vtype == TYPE_ARRAY) {
3915 snprintf(name, sizeof(name), "%s##SET", var->name);
3916 if (!parser_create_array_setter(parser, var, name))
3918 snprintf(name, sizeof(name), "%s##GET", var->name);
3919 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3922 else if (!localblock && !nofields &&
3923 var->expression.vtype == TYPE_FIELD &&
3924 var->expression.next->expression.vtype == TYPE_ARRAY)
3927 ast_expression *telem;
3929 ast_value *array = (ast_value*)var->expression.next;
3931 if (!ast_istype(var->expression.next, ast_value)) {
3932 parseerror(parser, "internal error: field element type must be an ast_value");
3936 snprintf(name, sizeof(name), "%s##SETF", var->name);
3937 if (!parser_create_array_field_setter(parser, array, name))
3940 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3941 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3942 tfield->expression.next = telem;
3943 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3944 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3952 if (parser->tok == ';') {
3953 ast_delete(basetype);
3954 if (!parser_next(parser)) {
3955 parseerror(parser, "error after variable declaration");
3961 if (parser->tok == ',')
3965 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3968 parseerror(parser, "missing comma or semicolon while parsing variables");
3972 if (localblock && opts_standard == COMPILER_QCC) {
3973 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3974 "initializing expression turns variable `%s` into a constant in this standard",
3981 if (parser->tok != '{') {
3982 if (parser->tok != '=') {
3983 parseerror(parser, "missing semicolon or initializer, got: `%s`", parser_tokval(parser));
3987 if (!parser_next(parser)) {
3988 parseerror(parser, "error parsing initializer");
3992 else if (opts_standard == COMPILER_QCC) {
3993 parseerror(parser, "expected '=' before function body in this standard");
3996 if (parser->tok == '#') {
3997 ast_function *func = NULL;
4000 parseerror(parser, "cannot declare builtins within functions");
4003 if (var->expression.vtype != TYPE_FUNCTION) {
4004 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
4007 if (!parser_next(parser)) {
4008 parseerror(parser, "expected builtin number");
4011 if (parser->tok != TOKEN_INTCONST) {
4012 parseerror(parser, "builtin number must be an integer constant");
4015 if (parser_token(parser)->constval.i < 0) {
4016 parseerror(parser, "builtin number must be an integer greater than zero");
4020 if (var->hasvalue) {
4021 (void)!parsewarning(parser, WARN_DOUBLE_DECLARATION,
4022 "builtin `%s` has already been defined\n"
4023 " -> previous declaration here: %s:%i",
4024 var->name, ast_ctx(var).file, (int)ast_ctx(var).line);
4028 func = ast_function_new(ast_ctx(var), var->name, var);
4030 parseerror(parser, "failed to allocate function for `%s`", var->name);
4033 vec_push(parser->functions, func);
4035 func->builtin = -parser_token(parser)->constval.i-1;
4038 if (!parser_next(parser)) {
4039 parseerror(parser, "expected comma or semicolon");
4041 ast_function_delete(func);
4042 var->constval.vfunc = NULL;
4046 else if (parser->tok == '{' || parser->tok == '[')
4050 parseerror(parser, "cannot declare functions within functions");
4055 ast_ctx(proto) = parser_ctx(parser);
4057 if (!parse_function_body(parser, var))
4059 ast_delete(basetype);
4060 for (i = 0; i < vec_size(parser->gotos); ++i)
4061 parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
4062 vec_free(parser->gotos);
4063 vec_free(parser->labels);
4066 ast_expression *cexp;
4069 cexp = parse_expression_leave(parser, true);
4074 cval = (ast_value*)cexp;
4075 if (!ast_istype(cval, ast_value) || ((!cval->hasvalue || cval->cvq != CV_CONST) && !cval->isfield))
4076 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
4079 if (opts_standard != COMPILER_GMQCC &&
4080 !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
4081 qualifier != CV_VAR)
4083 var->cvq = CV_CONST;
4085 var->hasvalue = true;
4086 if (cval->expression.vtype == TYPE_STRING)
4087 var->constval.vstring = parser_strdup(cval->constval.vstring);
4088 else if (cval->expression.vtype == TYPE_FIELD)
4089 var->constval.vfield = cval;
4091 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
4096 shunt sy = { NULL, NULL };
4099 vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
4100 vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
4101 vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
4102 if (!parser_sy_pop(parser, &sy))
4105 if (vec_size(sy.out) != 1 && vec_size(sy.ops) != 0)
4106 parseerror(parser, "internal error: leaked operands");
4107 ast_block_add_expr(localblock, (ast_expression*)sy.out[0].out);
4116 if (parser->tok == ',') {
4117 if (!parser_next(parser)) {
4118 parseerror(parser, "expected another variable");
4122 if (parser->tok != TOKEN_IDENT) {
4123 parseerror(parser, "expected another variable");
4126 var = ast_value_copy(basetype);
4128 ast_value_set_name(var, parser_tokval(parser));
4129 if (!parser_next(parser)) {
4130 parseerror(parser, "error parsing variable declaration");
4136 if (parser->tok != ';') {
4137 parseerror(parser, "missing semicolon after variables");
4141 if (!parser_next(parser)) {
4142 parseerror(parser, "parse error after variable declaration");
4146 ast_delete(basetype);
4150 if (cleanvar && var)
4152 ast_delete(basetype);
4156 ast_delete(basetype);
4157 if (cleanvar && var)
4159 if (me[0]) ast_member_delete(me[0]);
4160 if (me[1]) ast_member_delete(me[1]);
4161 if (me[2]) ast_member_delete(me[2]);
4165 static bool parser_global_statement(parser_t *parser)
4167 ast_value *istype = NULL;
4168 if (parser->tok == TOKEN_IDENT)
4169 istype = parser_find_typedef(parser, parser_tokval(parser), 0);
4171 if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
4173 return parse_variable(parser, NULL, false, CV_NONE, istype);
4175 else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
4177 if (!strcmp(parser_tokval(parser), "var")) {
4178 if (!parser_next(parser)) {
4179 parseerror(parser, "expected variable declaration after 'var'");
4182 if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "const")) {
4183 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `const` after 'var' qualifier");
4184 if (!parser_next(parser)) {
4185 parseerror(parser, "expected variable declaration after 'const var'");
4189 return parse_variable(parser, NULL, true, CV_VAR, NULL);
4192 else if (parser->tok == TOKEN_KEYWORD)
4194 if (!strcmp(parser_tokval(parser), "const")) {
4195 if (!parser_next(parser)) {
4196 parseerror(parser, "expected variable declaration after 'const'");
4199 if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var")) {
4200 (void)!parsewarning(parser, WARN_CONST_VAR, "ignoring `var` after const qualifier");
4201 if (!parser_next(parser)) {
4202 parseerror(parser, "expected variable declaration after 'const var'");
4206 return parse_variable(parser, NULL, true, CV_CONST, NULL);
4208 else if (!strcmp(parser_tokval(parser), "typedef")) {
4209 if (!parser_next(parser)) {
4210 parseerror(parser, "expected type definition after 'typedef'");
4213 return parse_typedef(parser);
4215 parseerror(parser, "unrecognized keyword `%s`", parser_tokval(parser));
4218 else if (parser->tok == '$')
4220 if (!parser_next(parser)) {
4221 parseerror(parser, "parse error");
4227 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
4233 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
4235 return util_crc16(old, str, strlen(str));
4238 static void progdefs_crc_file(const char *str)
4240 /* write to progdefs.h here */
4244 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
4246 old = progdefs_crc_sum(old, str);
4247 progdefs_crc_file(str);
4251 static void generate_checksum(parser_t *parser)
4253 uint16_t crc = 0xFFFF;
4257 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
4258 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
4260 progdefs_crc_file("\tint\tpad;\n");
4261 progdefs_crc_file("\tint\tofs_return[3];\n");
4262 progdefs_crc_file("\tint\tofs_parm0[3];\n");
4263 progdefs_crc_file("\tint\tofs_parm1[3];\n");
4264 progdefs_crc_file("\tint\tofs_parm2[3];\n");
4265 progdefs_crc_file("\tint\tofs_parm3[3];\n");
4266 progdefs_crc_file("\tint\tofs_parm4[3];\n");
4267 progdefs_crc_file("\tint\tofs_parm5[3];\n");
4268 progdefs_crc_file("\tint\tofs_parm6[3];\n");
4269 progdefs_crc_file("\tint\tofs_parm7[3];\n");
4271 for (i = 0; i < parser->crc_globals; ++i) {
4272 if (!ast_istype(parser->globals[i], ast_value))
4274 value = (ast_value*)(parser->globals[i]);
4275 switch (value->expression.vtype) {
4276 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4277 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4278 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4279 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4281 crc = progdefs_crc_both(crc, "\tint\t");
4284 crc = progdefs_crc_both(crc, value->name);
4285 crc = progdefs_crc_both(crc, ";\n");
4287 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
4288 for (i = 0; i < parser->crc_fields; ++i) {
4289 if (!ast_istype(parser->fields[i], ast_value))
4291 value = (ast_value*)(parser->fields[i]);
4292 switch (value->expression.next->expression.vtype) {
4293 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
4294 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
4295 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
4296 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
4298 crc = progdefs_crc_both(crc, "\tint\t");
4301 crc = progdefs_crc_both(crc, value->name);
4302 crc = progdefs_crc_both(crc, ";\n");
4304 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
4309 static parser_t *parser;
4315 parser = (parser_t*)mem_a(sizeof(parser_t));
4319 memset(parser, 0, sizeof(*parser));
4321 for (i = 0; i < operator_count; ++i) {
4322 if (operators[i].id == opid1('=')) {
4323 parser->assign_op = operators+i;
4327 if (!parser->assign_op) {
4328 printf("internal error: initializing parser: failed to find assign operator\n");
4333 vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
4334 vec_push(parser->variables, parser->htglobals = util_htnew(PARSER_HT_SIZE));
4335 vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
4336 vec_push(parser->_blocktypedefs, 0);
4340 bool parser_compile()
4342 /* initial lexer/parser state */
4343 parser->lex->flags.noops = true;
4345 if (parser_next(parser))
4347 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
4349 if (!parser_global_statement(parser)) {
4350 if (parser->tok == TOKEN_EOF)
4351 parseerror(parser, "unexpected eof");
4352 else if (!parser->errors)
4353 parseerror(parser, "there have been errors, bailing out");
4354 lex_close(parser->lex);
4360 parseerror(parser, "parse error");
4361 lex_close(parser->lex);
4366 lex_close(parser->lex);
4369 return !parser->errors;
4372 bool parser_compile_file(const char *filename)
4374 parser->lex = lex_open(filename);
4376 con_err("failed to open file \"%s\"\n", filename);
4379 return parser_compile();
4382 bool parser_compile_string_len(const char *name, const char *str, size_t len)
4384 parser->lex = lex_open_string(str, len, name);
4386 con_err("failed to create lexer for string \"%s\"\n", name);
4389 return parser_compile();
4392 bool parser_compile_string(const char *name, const char *str)
4394 parser->lex = lex_open_string(str, strlen(str), name);
4396 con_err("failed to create lexer for string \"%s\"\n", name);
4399 return parser_compile();
4402 void parser_cleanup()
4405 for (i = 0; i < vec_size(parser->accessors); ++i) {
4406 ast_delete(parser->accessors[i]->constval.vfunc);
4407 parser->accessors[i]->constval.vfunc = NULL;
4408 ast_delete(parser->accessors[i]);
4410 for (i = 0; i < vec_size(parser->functions); ++i) {
4411 ast_delete(parser->functions[i]);
4413 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4414 ast_delete(parser->imm_vector[i]);
4416 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4417 ast_delete(parser->imm_string[i]);
4419 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4420 ast_delete(parser->imm_float[i]);
4422 for (i = 0; i < vec_size(parser->fields); ++i) {
4423 ast_delete(parser->fields[i]);
4425 for (i = 0; i < vec_size(parser->globals); ++i) {
4426 ast_delete(parser->globals[i]);
4428 vec_free(parser->accessors);
4429 vec_free(parser->functions);
4430 vec_free(parser->imm_vector);
4431 vec_free(parser->imm_string);
4432 vec_free(parser->imm_float);
4433 vec_free(parser->globals);
4434 vec_free(parser->fields);
4436 for (i = 0; i < vec_size(parser->variables); ++i)
4437 util_htdel(parser->variables[i]);
4438 vec_free(parser->variables);
4439 vec_free(parser->_blocklocals);
4440 vec_free(parser->_locals);
4442 for (i = 0; i < vec_size(parser->_typedefs); ++i)
4443 ast_delete(parser->_typedefs[i]);
4444 vec_free(parser->_typedefs);
4445 for (i = 0; i < vec_size(parser->typedefs); ++i)
4446 util_htdel(parser->typedefs[i]);
4447 vec_free(parser->typedefs);
4448 vec_free(parser->_blocktypedefs);
4450 vec_free(parser->_block_ctx);
4452 vec_free(parser->labels);
4453 vec_free(parser->gotos);
4458 bool parser_finish(const char *output)
4464 if (!parser->errors)
4466 ir = ir_builder_new("gmqcc_out");
4468 con_out("failed to allocate builder\n");
4472 for (i = 0; i < vec_size(parser->fields); ++i) {
4475 if (!ast_istype(parser->fields[i], ast_value))
4477 field = (ast_value*)parser->fields[i];
4478 hasvalue = field->hasvalue;
4479 field->hasvalue = false;
4480 if (!ast_global_codegen((ast_value*)field, ir, true)) {
4481 con_out("failed to generate field %s\n", field->name);
4482 ir_builder_delete(ir);
4487 ast_expression *subtype;
4488 field->hasvalue = true;
4489 subtype = field->expression.next;
4490 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
4491 if (subtype->expression.vtype == TYPE_FIELD)
4492 ifld->fieldtype = subtype->expression.next->expression.vtype;
4493 else if (subtype->expression.vtype == TYPE_FUNCTION)
4494 ifld->outtype = subtype->expression.next->expression.vtype;
4495 (void)!ir_value_set_field(field->ir_v, ifld);
4498 for (i = 0; i < vec_size(parser->globals); ++i) {
4500 if (!ast_istype(parser->globals[i], ast_value))
4502 asvalue = (ast_value*)(parser->globals[i]);
4503 if (!asvalue->uses && !asvalue->hasvalue && asvalue->expression.vtype != TYPE_FUNCTION) {
4504 if (strcmp(asvalue->name, "end_sys_globals") &&
4505 strcmp(asvalue->name, "end_sys_fields"))
4507 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
4508 "unused global: `%s`", asvalue->name);
4511 if (!ast_global_codegen(asvalue, ir, false)) {
4512 con_out("failed to generate global %s\n", asvalue->name);
4513 ir_builder_delete(ir);
4517 for (i = 0; i < vec_size(parser->imm_float); ++i) {
4518 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
4519 con_out("failed to generate global %s\n", parser->imm_float[i]->name);
4520 ir_builder_delete(ir);
4524 for (i = 0; i < vec_size(parser->imm_string); ++i) {
4525 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
4526 con_out("failed to generate global %s\n", parser->imm_string[i]->name);
4527 ir_builder_delete(ir);
4531 for (i = 0; i < vec_size(parser->imm_vector); ++i) {
4532 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
4533 con_out("failed to generate global %s\n", parser->imm_vector[i]->name);
4534 ir_builder_delete(ir);
4538 for (i = 0; i < vec_size(parser->globals); ++i) {
4540 if (!ast_istype(parser->globals[i], ast_value))
4542 asvalue = (ast_value*)(parser->globals[i]);
4543 if (asvalue->setter) {
4544 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4545 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4546 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4548 printf("failed to generate setter for %s\n", asvalue->name);
4549 ir_builder_delete(ir);
4553 if (asvalue->getter) {
4554 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4555 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4556 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4558 printf("failed to generate getter for %s\n", asvalue->name);
4559 ir_builder_delete(ir);
4564 for (i = 0; i < vec_size(parser->fields); ++i) {
4566 asvalue = (ast_value*)(parser->fields[i]->expression.next);
4568 if (!ast_istype((ast_expression*)asvalue, ast_value))
4570 if (asvalue->expression.vtype != TYPE_ARRAY)
4572 if (asvalue->setter) {
4573 if (!ast_global_codegen(asvalue->setter, ir, false) ||
4574 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
4575 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
4577 printf("failed to generate setter for %s\n", asvalue->name);
4578 ir_builder_delete(ir);
4582 if (asvalue->getter) {
4583 if (!ast_global_codegen(asvalue->getter, ir, false) ||
4584 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
4585 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
4587 printf("failed to generate getter for %s\n", asvalue->name);
4588 ir_builder_delete(ir);
4593 for (i = 0; i < vec_size(parser->functions); ++i) {
4594 if (!ast_function_codegen(parser->functions[i], ir)) {
4595 con_out("failed to generate function %s\n", parser->functions[i]->name);
4596 ir_builder_delete(ir);
4601 ir_builder_dump(ir, con_out);
4602 for (i = 0; i < vec_size(parser->functions); ++i) {
4603 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
4604 con_out("failed to finalize function %s\n", parser->functions[i]->name);
4605 ir_builder_delete(ir);
4612 ir_builder_dump(ir, con_out);
4614 generate_checksum(parser);
4616 if (!ir_builder_generate(ir, output)) {
4617 con_out("*** failed to generate output file\n");
4618 ir_builder_delete(ir);
4623 ir_builder_delete(ir);
4627 con_out("*** there were compile errors\n");