16 MEM_VECTOR_MAKE(varentry_t, globals);
17 MEM_VECTOR_MAKE(varentry_t, fields);
18 MEM_VECTOR_MAKE(ast_function*, functions);
19 MEM_VECTOR_MAKE(ast_value*, imm_float);
20 MEM_VECTOR_MAKE(ast_value*, imm_string);
21 MEM_VECTOR_MAKE(ast_value*, imm_vector);
23 ast_value *imm_float_zero;
24 ast_value *imm_vector_zero;
29 ast_function *function;
30 MEM_VECTOR_MAKE(varentry_t, locals);
35 /* we store the '=' operator info */
36 const oper_info *assign_op;
38 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
39 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
40 * anything else: type error
45 MEM_VEC_FUNCTIONS(parser_t, varentry_t, globals)
46 MEM_VEC_FUNCTIONS(parser_t, varentry_t, fields)
47 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_float)
48 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_string)
49 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
50 MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
51 MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
53 static bool GMQCC_WARN parser_pop_local(parser_t *parser);
54 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields);
55 static ast_block* parse_block(parser_t *parser, bool warnreturn);
56 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
57 static ast_expression* parse_statement_or_block(parser_t *parser);
58 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
59 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
61 static void parseerror(parser_t *parser, const char *fmt, ...)
68 vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap);
72 /* returns true if it counts as an error */
73 static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
76 int lvl = LVL_WARNING;
78 if (!OPTS_WARN(warntype))
87 vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap);
93 static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
96 int lvl = LVL_WARNING;
98 if (!OPTS_WARN(warntype))
105 vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
111 /**********************************************************************
112 * some maths used for constant folding
115 vector vec3_add(vector a, vector b)
124 vector vec3_sub(vector a, vector b)
133 qcfloat vec3_mulvv(vector a, vector b)
135 return (a.x * b.x + a.y * b.y + a.z * b.z);
138 vector vec3_mulvf(vector a, float b)
147 /**********************************************************************
151 bool parser_next(parser_t *parser)
153 /* lex_do kills the previous token */
154 parser->tok = lex_do(parser->lex);
155 if (parser->tok == TOKEN_EOF)
157 if (parser->tok >= TOKEN_ERROR) {
158 parseerror(parser, "lex error");
164 #define parser_tokval(p) ((p)->lex->tok.value)
165 #define parser_token(p) (&((p)->lex->tok))
166 #define parser_ctx(p) ((p)->lex->tok.ctx)
168 static ast_value* parser_const_float(parser_t *parser, double d)
172 for (i = 0; i < parser->imm_float_count; ++i) {
173 if (parser->imm_float[i]->constval.vfloat == d)
174 return parser->imm_float[i];
176 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
178 out->constval.vfloat = d;
179 if (!parser_t_imm_float_add(parser, out)) {
180 ast_value_delete(out);
186 static ast_value* parser_const_float_0(parser_t *parser)
188 if (!parser->imm_float_zero)
189 parser->imm_float_zero = parser_const_float(parser, 0);
190 return parser->imm_float_zero;
193 static char *parser_strdup(const char *str)
196 /* actually dup empty strings */
197 char *out = mem_a(1);
201 return util_strdup(str);
204 static ast_value* parser_const_string(parser_t *parser, const char *str)
208 for (i = 0; i < parser->imm_string_count; ++i) {
209 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
210 return parser->imm_string[i];
212 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
214 out->constval.vstring = parser_strdup(str);
215 if (!parser_t_imm_string_add(parser, out)) {
216 ast_value_delete(out);
222 static ast_value* parser_const_vector(parser_t *parser, vector v)
226 for (i = 0; i < parser->imm_vector_count; ++i) {
227 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
228 return parser->imm_vector[i];
230 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
232 out->constval.vvec = v;
233 if (!parser_t_imm_vector_add(parser, out)) {
234 ast_value_delete(out);
240 static ast_value* parser_const_vector_f(parser_t *parser, float x, float y, float z)
246 return parser_const_vector(parser, v);
249 static ast_value* parser_const_vector_0(parser_t *parser)
251 if (!parser->imm_vector_zero)
252 parser->imm_vector_zero = parser_const_vector_f(parser, 0, 0, 0);
253 return parser->imm_vector_zero;
256 static ast_expression* parser_find_field(parser_t *parser, const char *name)
259 for (i = 0; i < parser->fields_count; ++i) {
260 if (!strcmp(parser->fields[i].name, name))
261 return parser->fields[i].var;
266 static ast_expression* parser_find_global(parser_t *parser, const char *name)
269 for (i = 0; i < parser->globals_count; ++i) {
270 if (!strcmp(parser->globals[i].name, name))
271 return parser->globals[i].var;
276 static ast_expression* parser_find_param(parser_t *parser, const char *name)
280 if (!parser->function)
282 fun = parser->function->vtype;
283 for (i = 0; i < fun->expression.params_count; ++i) {
284 if (!strcmp(fun->expression.params[i]->name, name))
285 return (ast_expression*)(fun->expression.params[i]);
290 static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
294 for (i = parser->locals_count; i > upto;) {
296 if (!strcmp(parser->locals[i].name, name))
297 return parser->locals[i].var;
300 return parser_find_param(parser, name);
303 static ast_expression* parser_find_var(parser_t *parser, const char *name)
307 v = parser_find_local(parser, name, 0, &dummy);
308 if (!v) v = parser_find_global(parser, name);
314 size_t etype; /* 0 = expression, others are operators */
318 ast_block *block; /* for commas and function calls */
323 MEM_VECTOR_MAKE(sy_elem, out);
324 MEM_VECTOR_MAKE(sy_elem, ops);
326 MEM_VEC_FUNCTIONS(shunt, sy_elem, out)
327 MEM_VEC_FUNCTIONS(shunt, sy_elem, ops)
329 #define SY_PAREN_EXPR '('
330 #define SY_PAREN_FUNC 'f'
331 #define SY_PAREN_INDEX '['
333 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
344 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
348 e.out = (ast_expression*)v;
355 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
357 e.etype = 1 + (op - operators);
366 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
378 # define DEBUGSHUNTDO(x) x
380 # define DEBUGSHUNTDO(x)
383 /* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
384 * so we need to rotate it to become ent.(foo[n]).
386 static void rotate_entfield_array_index_nodes(ast_expression **out)
388 ast_array_index *index;
389 ast_entfield *entfield;
393 ast_expression *entity;
395 lex_ctx ctx = ast_ctx(*out);
397 if (!ast_istype(*out, ast_array_index))
399 index = (ast_array_index*)*out;
401 if (!ast_istype(index->array, ast_entfield))
403 entfield = (ast_entfield*)index->array;
405 if (!ast_istype(entfield->field, ast_value))
407 field = (ast_value*)entfield->field;
410 entity = entfield->entity;
414 index = ast_array_index_new(ctx, (ast_expression*)field, sub);
415 entfield = ast_entfield_new(ctx, entity, (ast_expression*)index);
416 *out = (ast_expression*)entfield;
419 static bool parser_sy_pop(parser_t *parser, shunt *sy)
423 ast_expression *out = NULL;
424 ast_expression *exprs[3];
425 ast_block *blocks[3];
426 ast_value *asvalue[3];
428 qcint generated_op = 0;
433 if (!sy->ops_count) {
434 parseerror(parser, "internal error: missing operator");
438 if (sy->ops[sy->ops_count-1].paren) {
439 parseerror(parser, "unmatched parenthesis");
443 op = &operators[sy->ops[sy->ops_count-1].etype - 1];
444 ctx = sy->ops[sy->ops_count-1].ctx;
446 DEBUGSHUNTDO(printf("apply %s\n", op->op));
448 if (sy->out_count < op->operands) {
449 parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", sy->out_count,
450 op->op, (int)op->id);
456 sy->out_count -= op->operands;
457 for (i = 0; i < op->operands; ++i) {
458 exprs[i] = sy->out[sy->out_count+i].out;
459 blocks[i] = sy->out[sy->out_count+i].block;
460 asvalue[i] = (ast_value*)exprs[i];
463 if (blocks[0] && !blocks[0]->exprs_count && op->id != opid1(',')) {
464 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
468 #define NotSameType(T) \
469 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
470 exprs[0]->expression.vtype != T)
471 #define CanConstFold1(A) \
472 (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
473 #define CanConstFold(A, B) \
474 (CanConstFold1(A) && CanConstFold1(B))
475 #define ConstV(i) (asvalue[(i)]->constval.vvec)
476 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
477 #define ConstS(i) (asvalue[(i)]->constval.vstring)
481 parseerror(parser, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
485 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
486 if (exprs[1]->expression.vtype != TYPE_FIELD) {
487 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
490 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
492 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
493 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
497 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
503 if (exprs[0]->expression.vtype != TYPE_ARRAY &&
504 !(exprs[0]->expression.vtype == TYPE_FIELD &&
505 exprs[0]->expression.next->expression.vtype == TYPE_ARRAY))
507 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
508 parseerror(parser, "cannot index value of type %s", ty1);
511 if (exprs[1]->expression.vtype != TYPE_FLOAT) {
512 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
513 parseerror(parser, "index must be of type float, not %s", ty1);
516 out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]);
517 rotate_entfield_array_index_nodes(&out);
522 if (!ast_block_exprs_add(blocks[0], exprs[1]))
525 blocks[0] = ast_block_new(ctx);
526 if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
527 !ast_block_exprs_add(blocks[0], exprs[1]))
532 if (!ast_block_set_type(blocks[0], exprs[1]))
535 sy->out[sy->out_count++] = syblock(ctx, blocks[0]);
539 switch (exprs[0]->expression.vtype) {
541 if (CanConstFold1(exprs[0]))
542 out = (ast_expression*)parser_const_float(parser, -ConstF(0));
544 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F,
545 (ast_expression*)parser_const_float_0(parser),
549 if (CanConstFold1(exprs[0]))
550 out = (ast_expression*)parser_const_vector_f(parser,
551 -ConstV(0).x, -ConstV(0).y, -ConstV(0).z);
553 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V,
554 (ast_expression*)parser_const_vector_0(parser),
558 parseerror(parser, "invalid types used in expression: cannot negate type %s",
559 type_name[exprs[0]->expression.vtype]);
565 switch (exprs[0]->expression.vtype) {
567 if (CanConstFold1(exprs[0]))
568 out = (ast_expression*)parser_const_float(parser, !ConstF(0));
570 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_F, exprs[0]);
573 if (CanConstFold1(exprs[0]))
574 out = (ast_expression*)parser_const_float(parser,
575 (!ConstV(0).x && !ConstV(0).y && !ConstV(0).z));
577 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_V, exprs[0]);
580 if (CanConstFold1(exprs[0]))
581 out = (ast_expression*)parser_const_float(parser, !ConstS(0) || !*ConstS(0));
583 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_S, exprs[0]);
585 /* we don't constant-fold NOT for these types */
587 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_ENT, exprs[0]);
590 out = (ast_expression*)ast_unary_new(ctx, INSTR_NOT_FNC, exprs[0]);
593 parseerror(parser, "invalid types used in expression: cannot logically negate type %s",
594 type_name[exprs[0]->expression.vtype]);
600 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
601 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
603 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
604 type_name[exprs[0]->expression.vtype],
605 type_name[exprs[1]->expression.vtype]);
608 switch (exprs[0]->expression.vtype) {
610 if (CanConstFold(exprs[0], exprs[1]))
612 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
615 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
618 if (CanConstFold(exprs[0], exprs[1]))
619 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
621 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
624 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
625 type_name[exprs[0]->expression.vtype],
626 type_name[exprs[1]->expression.vtype]);
631 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
632 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
634 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
635 type_name[exprs[1]->expression.vtype],
636 type_name[exprs[0]->expression.vtype]);
639 switch (exprs[0]->expression.vtype) {
641 if (CanConstFold(exprs[0], exprs[1]))
642 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
644 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
647 if (CanConstFold(exprs[0], exprs[1]))
648 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
650 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
653 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
654 type_name[exprs[1]->expression.vtype],
655 type_name[exprs[0]->expression.vtype]);
660 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
661 exprs[0]->expression.vtype != TYPE_VECTOR &&
662 exprs[0]->expression.vtype != TYPE_FLOAT &&
663 exprs[1]->expression.vtype != TYPE_VECTOR &&
664 exprs[1]->expression.vtype != TYPE_FLOAT)
666 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
667 type_name[exprs[1]->expression.vtype],
668 type_name[exprs[0]->expression.vtype]);
671 switch (exprs[0]->expression.vtype) {
673 if (exprs[1]->expression.vtype == TYPE_VECTOR)
675 if (CanConstFold(exprs[0], exprs[1]))
676 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
678 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
682 if (CanConstFold(exprs[0], exprs[1]))
683 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
685 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
689 if (exprs[1]->expression.vtype == TYPE_FLOAT)
691 if (CanConstFold(exprs[0], exprs[1]))
692 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
694 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
698 if (CanConstFold(exprs[0], exprs[1]))
699 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
701 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
705 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
706 type_name[exprs[1]->expression.vtype],
707 type_name[exprs[0]->expression.vtype]);
712 if (NotSameType(TYPE_FLOAT)) {
713 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
714 type_name[exprs[0]->expression.vtype],
715 type_name[exprs[1]->expression.vtype]);
718 if (CanConstFold(exprs[0], exprs[1]))
719 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
721 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
725 parseerror(parser, "qc does not have a modulo operator");
729 if (NotSameType(TYPE_FLOAT)) {
730 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
731 type_name[exprs[0]->expression.vtype],
732 type_name[exprs[1]->expression.vtype]);
735 if (CanConstFold(exprs[0], exprs[1]))
736 out = (ast_expression*)parser_const_float(parser,
737 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
738 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
740 out = (ast_expression*)ast_binary_new(ctx,
741 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
745 parseerror(parser, "TODO: bitxor");
750 case opid3('<','<','='):
751 case opid3('>','>','='):
752 parseerror(parser, "TODO: shifts");
756 generated_op += 1; /* INSTR_OR */
758 generated_op += INSTR_AND;
759 if (NotSameType(TYPE_FLOAT)) {
760 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
761 type_name[exprs[0]->expression.vtype],
762 type_name[exprs[1]->expression.vtype]);
763 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
764 parseerror(parser, "TODO: optional early out");
767 if (opts_standard == COMPILER_GMQCC)
768 printf("TODO: early out logic\n");
769 if (CanConstFold(exprs[0], exprs[1]))
770 out = (ast_expression*)parser_const_float(parser,
771 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
773 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
777 generated_op += 1; /* INSTR_GT */
779 generated_op += 1; /* INSTR_LT */
780 case opid2('>', '='):
781 generated_op += 1; /* INSTR_GE */
782 case opid2('<', '='):
783 generated_op += INSTR_LE;
784 if (NotSameType(TYPE_FLOAT)) {
785 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
786 type_name[exprs[0]->expression.vtype],
787 type_name[exprs[1]->expression.vtype]);
790 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
792 case opid2('!', '='):
793 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
794 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
795 type_name[exprs[0]->expression.vtype],
796 type_name[exprs[1]->expression.vtype]);
799 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
801 case opid2('=', '='):
802 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
803 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
804 type_name[exprs[0]->expression.vtype],
805 type_name[exprs[1]->expression.vtype]);
808 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
812 if (ast_istype(exprs[0], ast_entfield)) {
813 ast_expression *field = ((ast_entfield*)exprs[0])->field;
814 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
815 exprs[0]->expression.vtype == TYPE_FIELD &&
816 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
818 assignop = type_storep_instr[TYPE_VECTOR];
821 assignop = type_storep_instr[exprs[0]->expression.vtype];
822 if (!ast_compare_type(field->expression.next, exprs[1])) {
823 ast_type_to_string(field->expression.next, ty1, sizeof(ty1));
824 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
825 if (opts_standard == COMPILER_QCC &&
826 field->expression.next->expression.vtype == TYPE_FUNCTION &&
827 exprs[1]->expression.vtype == TYPE_FUNCTION)
829 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
830 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
836 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
841 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) &&
842 exprs[0]->expression.vtype == TYPE_FIELD &&
843 exprs[0]->expression.next->expression.vtype == TYPE_VECTOR)
845 assignop = type_store_instr[TYPE_VECTOR];
848 assignop = type_store_instr[exprs[0]->expression.vtype];
851 if (assignop == AINSTR_END) {
852 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
853 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
854 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
856 else if (!ast_compare_type(exprs[0], exprs[1])) {
857 ast_type_to_string(exprs[0], ty1, sizeof(ty1));
858 ast_type_to_string(exprs[1], ty2, sizeof(ty2));
859 if (opts_standard == COMPILER_QCC &&
860 exprs[0]->expression.vtype == TYPE_FUNCTION &&
861 exprs[1]->expression.vtype == TYPE_FUNCTION)
863 if (parsewarning(parser, WARN_ASSIGN_FUNCTION_TYPES,
864 "invalid types in assignment: cannot assign %s to %s", ty2, ty1))
870 parseerror(parser, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
873 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
877 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
878 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
880 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
881 type_name[exprs[0]->expression.vtype],
882 type_name[exprs[1]->expression.vtype]);
885 if (ast_istype(exprs[0], ast_entfield))
886 assignop = type_storep_instr[exprs[0]->expression.vtype];
888 assignop = type_store_instr[exprs[0]->expression.vtype];
889 switch (exprs[0]->expression.vtype) {
891 out = (ast_expression*)ast_binstore_new(ctx, assignop,
892 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
896 out = (ast_expression*)ast_binstore_new(ctx, assignop,
897 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
901 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
902 type_name[exprs[0]->expression.vtype],
903 type_name[exprs[1]->expression.vtype]);
911 parseerror(parser, "failed to apply operand %s", op->op);
915 DEBUGSHUNTDO(printf("applied %s\n", op->op));
916 sy->out[sy->out_count++] = syexp(ctx, out);
920 static bool parser_close_call(parser_t *parser, shunt *sy)
922 /* was a function call */
930 fid = sy->ops[sy->ops_count].off;
932 /* out[fid] is the function
933 * everything above is parameters...
935 * 1 params = ast_expression
939 if (sy->out_count < 1 || sy->out_count <= fid) {
940 parseerror(parser, "internal error: function call needs function and parameter list...");
944 fun = sy->out[fid].out;
946 call = ast_call_new(sy->ops[sy->ops_count].ctx, fun);
948 parseerror(parser, "out of memory");
952 if (fid+1 == sy->out_count) {
955 } else if (fid+2 == sy->out_count) {
958 params = sy->out[sy->out_count].block;
962 if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
963 ast_delete(sy->out[sy->out_count].out);
964 parseerror(parser, "out of memory");
968 paramcount = params->exprs_count;
969 MEM_VECTOR_MOVE(params, exprs, call, params);
972 if (!ast_call_check_types(call))
975 parseerror(parser, "invalid function call");
979 /* overwrite fid, the function, with a call */
980 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
982 if (fun->expression.vtype != TYPE_FUNCTION) {
983 parseerror(parser, "not a function (%s)", type_name[fun->expression.vtype]);
987 if (!fun->expression.next) {
988 parseerror(parser, "could not determine function return type");
991 if (fun->expression.params_count != paramcount &&
992 !(fun->expression.variadic &&
993 fun->expression.params_count < paramcount))
996 const char *fewmany = (fun->expression.params_count > paramcount) ? "few" : "many";
998 fval = (ast_istype(fun, ast_value) ? ((ast_value*)fun) : NULL);
999 if (opts_standard == COMPILER_GMQCC)
1002 parseerror(parser, "too %s parameters for call to %s: expected %i, got %i\n"
1003 " -> `%s` has been declared here: %s:%i",
1004 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
1005 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1007 parseerror(parser, "too %s parameters for function call: expected %i, got %i\n"
1008 " -> `%s` has been declared here: %s:%i",
1009 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
1010 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1016 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1017 "too %s parameters for call to %s: expected %i, got %i\n"
1018 " -> `%s` has been declared here: %s:%i",
1019 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
1020 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1022 return !parsewarning(parser, WARN_TOO_FEW_PARAMETERS,
1023 "too %s parameters for function call: expected %i, got %i\n"
1024 " -> `%s` has been declared here: %s:%i",
1025 fewmany, fval->name, (int)fun->expression.params_count, (int)paramcount,
1026 fval->name, ast_ctx(fun).file, (int)ast_ctx(fun).line);
1034 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
1036 if (!sy->ops_count) {
1037 parseerror(parser, "unmatched closing paren");
1040 /* this would for bit a + (x) because there are no operators inside (x)
1041 if (sy->ops[sy->ops_count-1].paren == 1) {
1042 parseerror(parser, "empty parenthesis expression");
1046 while (sy->ops_count) {
1047 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_FUNC) {
1048 if (!parser_close_call(parser, sy))
1052 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_EXPR) {
1054 return !functions_only;
1056 if (sy->ops[sy->ops_count-1].paren == SY_PAREN_INDEX) {
1059 /* pop off the parenthesis */
1061 /* then apply the index operator */
1062 if (!parser_sy_pop(parser, sy))
1066 if (!parser_sy_pop(parser, sy))
1072 static void parser_reclassify_token(parser_t *parser)
1075 for (i = 0; i < operator_count; ++i) {
1076 if (!strcmp(parser_tokval(parser), operators[i].op)) {
1077 parser->tok = TOKEN_OPERATOR;
1083 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma)
1085 ast_expression *expr = NULL;
1087 bool wantop = false;
1088 bool gotmemberof = false;
1090 /* count the parens because an if starts with one, so the
1091 * end of a condition is an unmatched closing paren
1095 MEM_VECTOR_INIT(&sy, out);
1096 MEM_VECTOR_INIT(&sy, ops);
1098 parser->lex->flags.noops = false;
1100 parser_reclassify_token(parser);
1105 gotmemberof = false;
1107 parser->memberof = 0;
1109 if (parser->tok == TOKEN_IDENT)
1111 ast_expression *var;
1113 parseerror(parser, "expected operator or end of statement");
1118 if (opts_standard == COMPILER_GMQCC)
1120 if (parser->memberof == TYPE_ENTITY) {
1121 /* still get vars first since there could be a fieldpointer */
1122 var = parser_find_var(parser, parser_tokval(parser));
1124 var = parser_find_field(parser, parser_tokval(parser));
1126 else if (parser->memberof == TYPE_VECTOR)
1128 parseerror(parser, "TODO: implement effective vector member access");
1131 else if (parser->memberof) {
1132 parseerror(parser, "namespace for member not found");
1136 var = parser_find_var(parser, parser_tokval(parser));
1138 var = parser_find_var(parser, parser_tokval(parser));
1140 var = parser_find_field(parser, parser_tokval(parser));
1143 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
1146 if (ast_istype(var, ast_value))
1147 ((ast_value*)var)->uses++;
1148 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), var))) {
1149 parseerror(parser, "out of memory");
1152 DEBUGSHUNTDO(printf("push %s\n", parser_tokval(parser)));
1154 else if (parser->tok == TOKEN_FLOATCONST) {
1157 parseerror(parser, "expected operator or end of statement, got constant");
1161 val = parser_const_float(parser, (parser_token(parser)->constval.f));
1164 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1165 parseerror(parser, "out of memory");
1168 DEBUGSHUNTDO(printf("push %g\n", parser_token(parser)->constval.f));
1170 else if (parser->tok == TOKEN_INTCONST) {
1173 parseerror(parser, "expected operator or end of statement, got constant");
1177 val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
1180 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1181 parseerror(parser, "out of memory");
1184 DEBUGSHUNTDO(printf("push %i\n", parser_token(parser)->constval.i));
1186 else if (parser->tok == TOKEN_STRINGCONST) {
1189 parseerror(parser, "expected operator or end of statement, got constant");
1193 val = parser_const_string(parser, parser_tokval(parser));
1196 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1197 parseerror(parser, "out of memory");
1200 DEBUGSHUNTDO(printf("push string\n"));
1202 else if (parser->tok == TOKEN_VECTORCONST) {
1205 parseerror(parser, "expected operator or end of statement, got constant");
1209 val = parser_const_vector(parser, parser_token(parser)->constval.v);
1212 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
1213 parseerror(parser, "out of memory");
1216 DEBUGSHUNTDO(printf("push '%g %g %g'\n",
1217 parser_token(parser)->constval.v.x,
1218 parser_token(parser)->constval.v.y,
1219 parser_token(parser)->constval.v.z));
1221 else if (parser->tok == '(') {
1222 parseerror(parser, "internal error: '(' should be classified as operator");
1225 else if (parser->tok == '[') {
1226 parseerror(parser, "internal error: '[' should be classified as operator");
1229 else if (parser->tok == ')') {
1231 DEBUGSHUNTDO(printf("do[op] )\n"));
1235 /* we do expect an operator next */
1236 /* closing an opening paren */
1237 if (!parser_close_paren(parser, &sy, false))
1240 DEBUGSHUNTDO(printf("do[nop] )\n"));
1244 /* allowed for function calls */
1245 if (!parser_close_paren(parser, &sy, true))
1250 else if (parser->tok == ']') {
1252 parseerror(parser, "operand expected");
1256 if (!parser_close_paren(parser, &sy, false))
1260 else if (parser->tok != TOKEN_OPERATOR) {
1262 parseerror(parser, "expected operator or end of statement");
1269 /* classify the operator */
1270 /* TODO: suffix operators */
1271 const oper_info *op;
1272 const oper_info *olast = NULL;
1274 for (o = 0; o < operator_count; ++o) {
1275 if ((!(operators[o].flags & OP_PREFIX) == wantop) &&
1276 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1277 !strcmp(parser_tokval(parser), operators[o].op))
1282 if (o == operator_count) {
1283 /* no operator found... must be the end of the statement */
1286 /* found an operator */
1289 /* when declaring variables, a comma starts a new variable */
1290 if (op->id == opid1(',') && !parens && stopatcomma) {
1291 /* fixup the token */
1296 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1297 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1300 (op->prec < olast->prec) ||
1301 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1303 if (!parser_sy_pop(parser, &sy))
1305 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1306 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1311 if (op->id == opid1('.') && opts_standard == COMPILER_GMQCC) {
1312 /* for gmqcc standard: open up the namespace of the previous type */
1313 ast_expression *prevex = sy.out[sy.out_count-1].out;
1315 parseerror(parser, "unexpected member operator");
1318 if (prevex->expression.vtype == TYPE_ENTITY)
1319 parser->memberof = TYPE_ENTITY;
1320 else if (prevex->expression.vtype == TYPE_VECTOR)
1321 parser->memberof = TYPE_VECTOR;
1323 parseerror(parser, "type error: type has no members");
1329 if (op->id == opid1('(')) {
1331 DEBUGSHUNTDO(printf("push [op] (\n"));
1333 /* we expected an operator, this is the function-call operator */
1334 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_FUNC, sy.out_count-1))) {
1335 parseerror(parser, "out of memory");
1340 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_EXPR, 0))) {
1341 parseerror(parser, "out of memory");
1344 DEBUGSHUNTDO(printf("push [nop] (\n"));
1347 } else if (op->id == opid1('[')) {
1349 parseerror(parser, "unexpected array subscript");
1353 /* push both the operator and the paren, this makes life easier */
1354 if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
1356 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0)))
1360 DEBUGSHUNTDO(printf("push operator %s\n", op->op));
1361 if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
1366 if (!parser_next(parser)) {
1369 if (parser->tok == ';' || (!parens && parser->tok == ']')) {
1374 while (sy.ops_count) {
1375 if (!parser_sy_pop(parser, &sy))
1379 parser->lex->flags.noops = true;
1380 if (!sy.out_count) {
1381 parseerror(parser, "empty expression");
1384 expr = sy.out[0].out;
1385 MEM_VECTOR_CLEAR(&sy, out);
1386 MEM_VECTOR_CLEAR(&sy, ops);
1387 DEBUGSHUNTDO(printf("shunt done\n"));
1391 parser->lex->flags.noops = true;
1392 MEM_VECTOR_CLEAR(&sy, out);
1393 MEM_VECTOR_CLEAR(&sy, ops);
1397 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma)
1399 ast_expression *e = parse_expression_leave(parser, stopatcomma);
1402 if (!parser_next(parser)) {
1409 static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1412 ast_expression *cond, *ontrue, *onfalse = NULL;
1414 lex_ctx ctx = parser_ctx(parser);
1416 /* skip the 'if' and check for opening paren */
1417 if (!parser_next(parser) || parser->tok != '(') {
1418 parseerror(parser, "expected 'if' condition in parenthesis");
1421 /* parse into the expression */
1422 if (!parser_next(parser)) {
1423 parseerror(parser, "expected 'if' condition after opening paren");
1426 /* parse the condition */
1427 cond = parse_expression_leave(parser, false);
1431 if (parser->tok != ')') {
1432 parseerror(parser, "expected closing paren after 'if' condition");
1436 /* parse into the 'then' branch */
1437 if (!parser_next(parser)) {
1438 parseerror(parser, "expected statement for on-true branch of 'if'");
1442 ontrue = parse_statement_or_block(parser);
1447 /* check for an else */
1448 if (!strcmp(parser_tokval(parser), "else")) {
1449 /* parse into the 'else' branch */
1450 if (!parser_next(parser)) {
1451 parseerror(parser, "expected on-false branch after 'else'");
1456 onfalse = parse_statement_or_block(parser);
1464 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1465 *out = (ast_expression*)ifthen;
1469 static bool parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1472 ast_expression *cond, *ontrue;
1474 lex_ctx ctx = parser_ctx(parser);
1476 /* skip the 'while' and check for opening paren */
1477 if (!parser_next(parser) || parser->tok != '(') {
1478 parseerror(parser, "expected 'while' condition in parenthesis");
1481 /* parse into the expression */
1482 if (!parser_next(parser)) {
1483 parseerror(parser, "expected 'while' condition after opening paren");
1486 /* parse the condition */
1487 cond = parse_expression_leave(parser, false);
1491 if (parser->tok != ')') {
1492 parseerror(parser, "expected closing paren after 'while' condition");
1496 /* parse into the 'then' branch */
1497 if (!parser_next(parser)) {
1498 parseerror(parser, "expected while-loop body");
1502 ontrue = parse_statement_or_block(parser);
1508 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1509 *out = (ast_expression*)aloop;
1513 static bool parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1516 ast_expression *cond, *ontrue;
1518 lex_ctx ctx = parser_ctx(parser);
1520 /* skip the 'do' and get the body */
1521 if (!parser_next(parser)) {
1522 parseerror(parser, "expected loop body");
1525 ontrue = parse_statement_or_block(parser);
1529 /* expect the "while" */
1530 if (parser->tok != TOKEN_KEYWORD ||
1531 strcmp(parser_tokval(parser), "while"))
1533 parseerror(parser, "expected 'while' and condition");
1538 /* skip the 'while' and check for opening paren */
1539 if (!parser_next(parser) || parser->tok != '(') {
1540 parseerror(parser, "expected 'while' condition in parenthesis");
1544 /* parse into the expression */
1545 if (!parser_next(parser)) {
1546 parseerror(parser, "expected 'while' condition after opening paren");
1550 /* parse the condition */
1551 cond = parse_expression_leave(parser, false);
1555 if (parser->tok != ')') {
1556 parseerror(parser, "expected closing paren after 'while' condition");
1562 if (!parser_next(parser) || parser->tok != ';') {
1563 parseerror(parser, "expected semicolon after condition");
1569 if (!parser_next(parser)) {
1570 parseerror(parser, "parse error");
1576 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1577 *out = (ast_expression*)aloop;
1581 static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1584 ast_expression *initexpr, *cond, *increment, *ontrue;
1585 size_t oldblocklocal;
1588 lex_ctx ctx = parser_ctx(parser);
1590 oldblocklocal = parser->blocklocal;
1591 parser->blocklocal = parser->locals_count;
1598 /* skip the 'while' and check for opening paren */
1599 if (!parser_next(parser) || parser->tok != '(') {
1600 parseerror(parser, "expected 'for' expressions in parenthesis");
1603 /* parse into the expression */
1604 if (!parser_next(parser)) {
1605 parseerror(parser, "expected 'for' initializer after opening paren");
1609 if (parser->tok == TOKEN_TYPENAME) {
1610 if (opts_standard != COMPILER_GMQCC) {
1611 if (parsewarning(parser, WARN_EXTENSIONS,
1612 "current standard does not allow variable declarations in for-loop initializers"))
1616 parseerror(parser, "TODO: assignment of new variables to be non-const");
1618 if (!parse_variable(parser, block, true))
1621 else if (parser->tok != ';')
1623 initexpr = parse_expression_leave(parser, false);
1628 /* move on to condition */
1629 if (parser->tok != ';') {
1630 parseerror(parser, "expected semicolon after for-loop initializer");
1633 if (!parser_next(parser)) {
1634 parseerror(parser, "expected for-loop condition");
1638 /* parse the condition */
1639 if (parser->tok != ';') {
1640 cond = parse_expression_leave(parser, false);
1645 /* move on to incrementor */
1646 if (parser->tok != ';') {
1647 parseerror(parser, "expected semicolon after for-loop initializer");
1650 if (!parser_next(parser)) {
1651 parseerror(parser, "expected for-loop condition");
1655 /* parse the incrementor */
1656 if (parser->tok != ')') {
1657 increment = parse_expression_leave(parser, false);
1660 if (!ast_istype(increment, ast_store) &&
1661 !ast_istype(increment, ast_call) &&
1662 !ast_istype(increment, ast_binstore))
1664 if (genwarning(ast_ctx(increment), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1670 if (parser->tok != ')') {
1671 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1674 /* parse into the 'then' branch */
1675 if (!parser_next(parser)) {
1676 parseerror(parser, "expected for-loop body");
1679 ontrue = parse_statement_or_block(parser);
1684 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1685 *out = (ast_expression*)aloop;
1687 while (parser->locals_count > parser->blocklocal)
1688 retval = retval && parser_pop_local(parser);
1689 parser->blocklocal = oldblocklocal;
1692 if (initexpr) ast_delete(initexpr);
1693 if (cond) ast_delete(cond);
1694 if (increment) ast_delete(increment);
1695 while (parser->locals_count > parser->blocklocal)
1696 (void)!parser_pop_local(parser);
1697 parser->blocklocal = oldblocklocal;
1701 static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
1703 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
1705 /* local variable */
1707 parseerror(parser, "cannot declare a variable from here");
1710 if (opts_standard == COMPILER_QCC) {
1711 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1714 if (!parse_variable(parser, block, false))
1719 else if (parser->tok == TOKEN_KEYWORD)
1721 if (!strcmp(parser_tokval(parser), "local"))
1724 parseerror(parser, "cannot declare a local variable here");
1727 if (!parser_next(parser)) {
1728 parseerror(parser, "expected variable declaration");
1731 if (!parse_variable(parser, block, true))
1736 else if (!strcmp(parser_tokval(parser), "return"))
1738 ast_expression *exp = NULL;
1739 ast_return *ret = NULL;
1740 ast_value *expected = parser->function->vtype;
1742 if (!parser_next(parser)) {
1743 parseerror(parser, "expected return expression");
1747 if (parser->tok != ';') {
1748 exp = parse_expression(parser, false);
1752 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1753 parseerror(parser, "return with invalid expression");
1756 ret = ast_return_new(exp->expression.node.context, exp);
1762 if (!parser_next(parser))
1763 parseerror(parser, "parse error");
1764 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1765 if (opts_standard != COMPILER_GMQCC)
1766 (void)!parsewarning(parser, WARN_MISSING_RETURN_VALUES, "return without value");
1768 parseerror(parser, "return without value");
1770 ret = ast_return_new(parser_ctx(parser), NULL);
1772 *out = (ast_expression*)ret;
1775 else if (!strcmp(parser_tokval(parser), "if"))
1777 return parse_if(parser, block, out);
1779 else if (!strcmp(parser_tokval(parser), "while"))
1781 return parse_while(parser, block, out);
1783 else if (!strcmp(parser_tokval(parser), "do"))
1785 return parse_dowhile(parser, block, out);
1787 else if (!strcmp(parser_tokval(parser), "for"))
1789 if (opts_standard == COMPILER_QCC) {
1790 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1793 return parse_for(parser, block, out);
1795 parseerror(parser, "Unexpected keyword");
1798 else if (parser->tok == '{')
1801 inner = parse_block(parser, false);
1804 *out = (ast_expression*)inner;
1809 ast_expression *exp = parse_expression(parser, false);
1813 if (!ast_istype(exp, ast_store) &&
1814 !ast_istype(exp, ast_call) &&
1815 !ast_istype(exp, ast_binstore))
1817 if (genwarning(ast_ctx(exp), WARN_EFFECTLESS_STATEMENT, "statement has no effect"))
1824 static bool GMQCC_WARN parser_pop_local(parser_t *parser)
1827 parser->locals_count--;
1829 ve = &parser->locals[parser->locals_count];
1830 if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
1831 if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
1834 mem_d(parser->locals[parser->locals_count].name);
1838 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn)
1840 size_t oldblocklocal;
1843 oldblocklocal = parser->blocklocal;
1844 parser->blocklocal = parser->locals_count;
1846 if (!parser_next(parser)) { /* skip the '{' */
1847 parseerror(parser, "expected function body");
1851 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
1853 ast_expression *expr;
1854 if (parser->tok == '}')
1857 if (!parse_statement(parser, block, &expr)) {
1858 /* parseerror(parser, "parse error"); */
1864 if (!ast_block_exprs_add(block, expr)) {
1871 if (parser->tok != '}') {
1874 if (warnreturn && parser->function->vtype->expression.next->expression.vtype != TYPE_VOID)
1876 if (!block->exprs_count ||
1877 !ast_istype(block->exprs[block->exprs_count-1], ast_return))
1879 if (parsewarning(parser, WARN_MISSING_RETURN_VALUES, "control reaches end of non-void function")) {
1885 (void)parser_next(parser);
1889 while (parser->locals_count > parser->blocklocal)
1890 retval = retval && parser_pop_local(parser);
1891 parser->blocklocal = oldblocklocal;
1895 static ast_block* parse_block(parser_t *parser, bool warnreturn)
1898 block = ast_block_new(parser_ctx(parser));
1901 if (!parse_block_into(parser, block, warnreturn)) {
1902 ast_block_delete(block);
1908 static ast_expression* parse_statement_or_block(parser_t *parser)
1910 ast_expression *expr = NULL;
1911 if (parser->tok == '{')
1912 return (ast_expression*)parse_block(parser, false);
1913 if (!parse_statement(parser, NULL, &expr))
1919 static bool create_vector_members(parser_t *parser, ast_value *var, varentry_t *ve)
1922 size_t len = strlen(var->name);
1924 for (i = 0; i < 3; ++i) {
1925 ve[i].var = (ast_expression*)ast_member_new(ast_ctx(var), (ast_expression*)var, i);
1929 ve[i].name = (char*)mem_a(len+3);
1931 ast_delete(ve[i].var);
1935 memcpy(ve[i].name, var->name, len);
1936 ve[i].name[len] = '_';
1937 ve[i].name[len+1] = 'x'+i;
1938 ve[i].name[len+2] = 0;
1947 ast_delete(ve[i].var);
1954 static bool parse_function_body(parser_t *parser, ast_value *var)
1956 ast_block *block = NULL;
1961 ast_expression *framenum = NULL;
1962 ast_expression *nextthink = NULL;
1963 /* None of the following have to be deleted */
1964 ast_expression *fld_think = NULL, *fld_nextthink = NULL, *fld_frame = NULL;
1965 ast_expression *gbl_time = NULL, *gbl_self = NULL;
1966 bool has_frame_think;
1970 has_frame_think = false;
1971 old = parser->function;
1973 if (var->expression.variadic) {
1974 if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
1975 "variadic function with implementation will not be able to access additional parameters"))
1981 if (parser->tok == '[') {
1982 /* got a frame definition: [ framenum, nextthink ]
1983 * this translates to:
1984 * self.frame = framenum;
1985 * self.nextthink = time + 0.1;
1986 * self.think = nextthink;
1990 fld_think = parser_find_field(parser, "think");
1991 fld_nextthink = parser_find_field(parser, "nextthink");
1992 fld_frame = parser_find_field(parser, "frame");
1993 if (!fld_think || !fld_nextthink || !fld_frame) {
1994 parseerror(parser, "cannot use [frame,think] notation without the required fields");
1995 parseerror(parser, "please declare the following entityfields: `frame`, `think`, `nextthink`");
1998 gbl_time = parser_find_global(parser, "time");
1999 gbl_self = parser_find_global(parser, "self");
2000 if (!gbl_time || !gbl_self) {
2001 parseerror(parser, "cannot use [frame,think] notation without the required globals");
2002 parseerror(parser, "please declare the following globals: `time`, `self`");
2006 if (!parser_next(parser))
2009 framenum = parse_expression_leave(parser, true);
2011 parseerror(parser, "expected a framenumber constant in[frame,think] notation");
2014 if (!ast_istype(framenum, ast_value) || !( (ast_value*)framenum )->isconst) {
2015 ast_unref(framenum);
2016 parseerror(parser, "framenumber in [frame,think] notation must be a constant");
2020 if (parser->tok != ',') {
2021 ast_unref(framenum);
2022 parseerror(parser, "expected comma after frame number in [frame,think] notation");
2023 parseerror(parser, "Got a %i\n", parser->tok);
2027 if (!parser_next(parser)) {
2028 ast_unref(framenum);
2032 if (parser->tok == TOKEN_IDENT && !parser_find_var(parser, parser_tokval(parser)))
2034 /* qc allows the use of not-yet-declared functions here
2035 * - this automatically creates a prototype */
2037 ast_value *thinkfunc;
2038 ast_expression *functype = fld_think->expression.next;
2040 thinkfunc = ast_value_new(parser_ctx(parser), parser_tokval(parser), functype->expression.vtype);
2041 if (!thinkfunc || !ast_type_adopt(thinkfunc, functype)) {
2042 ast_unref(framenum);
2043 parseerror(parser, "failed to create implicit prototype for `%s`", parser_tokval(parser));
2047 if (!parser_next(parser)) {
2048 ast_unref(framenum);
2049 ast_delete(thinkfunc);
2053 varent.var = (ast_expression*)thinkfunc;
2054 varent.name = util_strdup(thinkfunc->name);
2055 if (!parser_t_globals_add(parser, varent)) {
2056 ast_unref(framenum);
2057 ast_delete(thinkfunc);
2060 nextthink = (ast_expression*)thinkfunc;
2063 nextthink = parse_expression_leave(parser, true);
2065 ast_unref(framenum);
2066 parseerror(parser, "expected a think-function in [frame,think] notation");
2071 if (!ast_istype(nextthink, ast_value)) {
2072 parseerror(parser, "think-function in [frame,think] notation must be a constant");
2076 if (retval && parser->tok != ']') {
2077 parseerror(parser, "expected closing `]` for [frame,think] notation");
2081 if (retval && !parser_next(parser)) {
2085 if (retval && parser->tok != '{') {
2086 parseerror(parser, "a function body has to be declared after a [frame,think] declaration");
2091 ast_unref(nextthink);
2092 ast_unref(framenum);
2096 has_frame_think = true;
2099 block = ast_block_new(parser_ctx(parser));
2101 parseerror(parser, "failed to allocate block");
2102 if (has_frame_think) {
2103 ast_unref(nextthink);
2104 ast_unref(framenum);
2109 if (has_frame_think) {
2111 ast_expression *self_frame;
2112 ast_expression *self_nextthink;
2113 ast_expression *self_think;
2114 ast_expression *time_plus_1;
2115 ast_store *store_frame;
2116 ast_store *store_nextthink;
2117 ast_store *store_think;
2119 ctx = parser_ctx(parser);
2120 self_frame = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_frame);
2121 self_nextthink = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_nextthink);
2122 self_think = (ast_expression*)ast_entfield_new(ctx, gbl_self, fld_think);
2124 time_plus_1 = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F,
2125 gbl_time, (ast_expression*)parser_const_float(parser, 0.1));
2127 if (!self_frame || !self_nextthink || !self_think || !time_plus_1) {
2128 if (self_frame) ast_delete(self_frame);
2129 if (self_nextthink) ast_delete(self_nextthink);
2130 if (self_think) ast_delete(self_think);
2131 if (time_plus_1) ast_delete(time_plus_1);
2137 store_frame = ast_store_new(ctx, INSTR_STOREP_F, self_frame, framenum);
2138 store_nextthink = ast_store_new(ctx, INSTR_STOREP_F, self_nextthink, time_plus_1);
2139 store_think = ast_store_new(ctx, INSTR_STOREP_FNC, self_think, nextthink);
2142 ast_delete(self_frame);
2145 if (!store_nextthink) {
2146 ast_delete(self_nextthink);
2150 ast_delete(self_think);
2154 if (store_frame) ast_delete(store_frame);
2155 if (store_nextthink) ast_delete(store_nextthink);
2156 if (store_think) ast_delete(store_think);
2159 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_frame)) {
2160 ast_delete(store_frame);
2161 ast_delete(store_nextthink);
2162 ast_delete(store_think);
2166 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_nextthink)) {
2167 ast_delete(store_nextthink);
2168 ast_delete(store_think);
2172 if (retval && !ast_block_exprs_add(block, (ast_expression*)store_think) )
2174 ast_delete(store_think);
2180 parseerror(parser, "failed to generate code for [frame,think]");
2181 ast_unref(nextthink);
2182 ast_unref(framenum);
2188 for (parami = 0; parami < var->expression.params_count; ++parami) {
2191 ast_value *param = var->expression.params[parami];
2193 if (param->expression.vtype != TYPE_VECTOR &&
2194 (param->expression.vtype != TYPE_FIELD ||
2195 param->expression.next->expression.vtype != TYPE_VECTOR))
2200 if (!create_vector_members(parser, param, ve)) {
2201 ast_block_delete(block);
2205 for (e = 0; e < 3; ++e) {
2206 if (!parser_t_locals_add(parser, ve[e]))
2208 if (!ast_block_collect(block, ve[e].var)) {
2209 parser->locals_count--;
2212 ve[e].var = NULL; /* collected */
2215 parser->locals -= e;
2220 ast_block_delete(block);
2225 func = ast_function_new(ast_ctx(var), var->name, var);
2227 parseerror(parser, "failed to allocate function for `%s`", var->name);
2228 ast_block_delete(block);
2231 if (!parser_t_functions_add(parser, func)) {
2232 parseerror(parser, "failed to allocate slot for function `%s`", var->name);
2233 ast_block_delete(block);
2237 parser->function = func;
2238 if (!parse_block_into(parser, block, true)) {
2239 ast_block_delete(block);
2243 if (!ast_function_blocks_add(func, block)) {
2244 ast_block_delete(block);
2248 parser->function = old;
2249 while (parser->locals_count)
2250 retval = retval && parser_pop_local(parser);
2252 if (parser->tok == ';')
2253 return parser_next(parser);
2254 else if (opts_standard == COMPILER_QCC)
2255 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
2259 parser->functions_count--;
2261 ast_function_delete(func);
2262 var->constval.vfunc = NULL;
2265 while (parser->locals_count) {
2266 parser->locals_count--;
2267 mem_d(parser->locals[parser->locals_count].name);
2269 parser->function = old;
2273 static ast_expression *array_accessor_split(
2278 ast_expression *left,
2279 ast_expression *right
2285 lex_ctx ctx = ast_ctx(array);
2287 if (!left || !right) {
2288 if (left) ast_delete(left);
2289 if (right) ast_delete(right);
2293 cmp = ast_binary_new(ctx, INSTR_LT,
2294 (ast_expression*)index,
2295 (ast_expression*)parser_const_float(parser, middle));
2299 parseerror(parser, "internal error: failed to create comparison for array setter");
2303 ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
2305 ast_delete(cmp); /* will delete left and right */
2306 parseerror(parser, "internal error: failed to create conditional jump for array setter");
2310 return (ast_expression*)ifthen;
2313 static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
2315 lex_ctx ctx = ast_ctx(array);
2317 if (from+1 == afterend) {
2321 ast_array_index *subscript;
2322 int assignop = type_store_instr[value->expression.vtype];
2324 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2325 assignop = INSTR_STORE_V;
2327 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2331 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
2333 ast_delete(subscript);
2337 block = ast_block_new(ctx);
2343 if (!ast_block_exprs_add(block, (ast_expression*)st)) {
2348 ret = ast_return_new(ctx, NULL);
2354 if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
2359 return (ast_expression*)block;
2361 ast_expression *left, *right;
2362 size_t diff = afterend - from;
2363 size_t middle = from + diff/2;
2364 left = array_setter_node(parser, array, index, value, from, middle);
2365 right = array_setter_node(parser, array, index, value, middle, afterend);
2366 return array_accessor_split(parser, array, index, middle, left, right);
2370 static ast_expression *array_field_setter_node(
2379 lex_ctx ctx = ast_ctx(array);
2381 if (from+1 == afterend) {
2385 ast_entfield *entfield;
2386 ast_array_index *subscript;
2387 int assignop = type_storep_instr[value->expression.vtype];
2389 if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
2390 assignop = INSTR_STOREP_V;
2392 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2396 entfield = ast_entfield_new_force(ctx,
2397 (ast_expression*)entity,
2398 (ast_expression*)subscript,
2399 (ast_expression*)subscript);
2401 ast_delete(subscript);
2405 ast_store *st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
2407 ast_delete(entfield);
2411 block = ast_block_new(ctx);
2417 if (!ast_block_exprs_add(block, (ast_expression*)st)) {
2422 ret = ast_return_new(ctx, NULL);
2428 if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
2433 return (ast_expression*)block;
2435 ast_expression *left, *right;
2436 size_t diff = afterend - from;
2437 size_t middle = from + diff/2;
2438 left = array_field_setter_node(parser, array, entity, index, value, from, middle);
2439 right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
2440 return array_accessor_split(parser, array, index, middle, left, right);
2444 static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
2446 lex_ctx ctx = ast_ctx(array);
2448 if (from+1 == afterend) {
2450 ast_array_index *subscript;
2452 subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
2456 ret = ast_return_new(ctx, (ast_expression*)subscript);
2458 ast_delete(subscript);
2462 return (ast_expression*)ret;
2464 ast_expression *left, *right;
2465 size_t diff = afterend - from;
2466 size_t middle = from + diff/2;
2467 left = array_getter_node(parser, array, index, from, middle);
2468 right = array_getter_node(parser, array, index, middle, afterend);
2469 return array_accessor_split(parser, array, index, middle, left, right);
2473 static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
2475 ast_function *func = NULL;
2476 ast_value *fval = NULL;
2478 fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
2480 parseerror(parser, "failed to create accessor function value");
2484 func = ast_function_new(ast_ctx(array), funcname, fval);
2487 parseerror(parser, "failed to create accessor function node");
2496 static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
2498 ast_expression *root = NULL;
2499 ast_block *body = NULL;
2500 ast_value *index = NULL;
2501 ast_value *value = NULL;
2505 if (!ast_istype(array->expression.next, ast_value)) {
2506 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2510 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2512 func = fval->constval.vfunc;
2513 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2515 body = ast_block_new(ast_ctx(array));
2517 parseerror(parser, "failed to create block for array accessor");
2521 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2522 value = ast_value_copy((ast_value*)array->expression.next);
2524 if (!index || !value) {
2525 parseerror(parser, "failed to create locals for array accessor");
2528 (void)!ast_value_set_name(value, "value"); /* not important */
2529 (void)!ast_expression_common_params_add(&fval->expression, index);
2530 (void)!ast_expression_common_params_add(&fval->expression, value);
2532 root = array_setter_node(parser, array, index, value, 0, array->expression.count);
2534 parseerror(parser, "failed to build accessor search tree");
2538 (void)!ast_block_exprs_add(body, root);
2539 (void)!ast_function_blocks_add(func, body);
2540 array->setter = fval;
2543 if (body) ast_delete(body);
2544 if (index) ast_delete(index);
2545 if (value) ast_delete(value);
2546 if (root) ast_delete(root);
2552 static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
2554 ast_expression *root = NULL;
2555 ast_block *body = NULL;
2556 ast_value *entity = NULL;
2557 ast_value *index = NULL;
2558 ast_value *value = NULL;
2562 if (!ast_istype(array->expression.next, ast_value)) {
2563 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2567 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2569 func = fval->constval.vfunc;
2570 fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
2572 body = ast_block_new(ast_ctx(array));
2574 parseerror(parser, "failed to create block for array accessor");
2578 entity = ast_value_new(ast_ctx(array), "entity", TYPE_ENTITY);
2579 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2580 value = ast_value_copy((ast_value*)array->expression.next);
2581 if (!entity || !index || !value) {
2582 parseerror(parser, "failed to create locals for array accessor");
2585 (void)!ast_value_set_name(value, "value"); /* not important */
2586 (void)!ast_expression_common_params_add(&fval->expression, entity);
2587 (void)!ast_expression_common_params_add(&fval->expression, index);
2588 (void)!ast_expression_common_params_add(&fval->expression, value);
2590 root = array_field_setter_node(parser, array, entity, index, value, 0, array->expression.count);
2592 parseerror(parser, "failed to build accessor search tree");
2596 (void)!ast_block_exprs_add(body, root);
2597 (void)!ast_function_blocks_add(func, body);
2598 array->setter = fval;
2601 if (body) ast_delete(body);
2602 if (entity) ast_delete(entity);
2603 if (index) ast_delete(index);
2604 if (value) ast_delete(value);
2605 if (root) ast_delete(root);
2611 static bool parser_create_array_getter(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname)
2613 ast_expression *root = NULL;
2614 ast_block *body = NULL;
2615 ast_value *index = NULL;
2619 /* NOTE: checking array->expression.next rather than elemtype since
2620 * for fields elemtype is a temporary fieldtype.
2622 if (!ast_istype(array->expression.next, ast_value)) {
2623 parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
2627 if (!parser_create_array_accessor(parser, array, funcname, &fval))
2629 func = fval->constval.vfunc;
2630 fval->expression.next = ast_type_copy(ast_ctx(array), elemtype);
2632 body = ast_block_new(ast_ctx(array));
2634 parseerror(parser, "failed to create block for array accessor");
2638 index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
2641 parseerror(parser, "failed to create locals for array accessor");
2644 (void)!ast_expression_common_params_add(&fval->expression, index);
2646 root = array_getter_node(parser, array, index, 0, array->expression.count);
2648 parseerror(parser, "failed to build accessor search tree");
2652 (void)!ast_block_exprs_add(body, root);
2653 (void)!ast_function_blocks_add(func, body);
2654 array->getter = fval;
2657 if (body) ast_delete(body);
2658 if (index) ast_delete(index);
2659 if (root) ast_delete(root);
2666 MEM_VECTOR_MAKE(ast_value*, p);
2668 MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
2670 static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
2671 static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
2679 bool variadic = false;
2681 ctx = parser_ctx(parser);
2683 /* for the sake of less code we parse-in in this function */
2684 if (!parser_next(parser)) {
2685 parseerror(parser, "expected parameter list");
2689 MEM_VECTOR_INIT(¶ms, p);
2691 /* parse variables until we hit a closing paren */
2692 while (parser->tok != ')') {
2694 /* there must be commas between them */
2695 if (parser->tok != ',') {
2696 parseerror(parser, "expected comma or end of parameter list");
2699 if (!parser_next(parser)) {
2700 parseerror(parser, "expected parameter");
2706 if (parser->tok == TOKEN_DOTS) {
2707 /* '...' indicates a varargs function */
2709 if (!parser_next(parser)) {
2710 parseerror(parser, "expected parameter");
2713 if (parser->tok != ')') {
2714 parseerror(parser, "`...` must be the last parameter of a variadic function declaration");
2720 /* for anything else just parse a typename */
2721 param = parse_typename(parser, NULL);
2724 if (!paramlist_t_p_add(¶ms, param)) {
2728 if (param->expression.vtype >= TYPE_VARIANT) {
2729 char typename[1024];
2730 ast_type_to_string((ast_expression*)param, typename, sizeof(typename));
2731 parseerror(parser, "type not supported as part of a parameter list: %s", typename);
2738 if (params.p_count > 8)
2739 parseerror(parser, "more than 8 parameters are currently not supported");
2742 if (!parser_next(parser)) {
2743 parseerror(parser, "parse error after typename");
2747 /* now turn 'var' into a function type */
2748 fval = ast_value_new(ctx, "<type()>", TYPE_FUNCTION);
2749 fval->expression.next = (ast_expression*)var;
2750 fval->expression.variadic = variadic;
2753 MEM_VECTOR_MOVE(¶ms, p, &var->expression, params);
2759 for (i = 0; i < params.p_count; ++i)
2760 ast_delete(params.p[i]);
2761 MEM_VECTOR_CLEAR(¶ms, p);
2765 static ast_value *parse_arraysize(parser_t *parser, ast_value *var)
2767 ast_expression *cexp;
2768 ast_value *cval, *tmp;
2771 ctx = parser_ctx(parser);
2773 if (!parser_next(parser)) {
2775 parseerror(parser, "expected array-size");
2779 cexp = parse_expression_leave(parser, true);
2781 if (!cexp || !ast_istype(cexp, ast_value)) {
2785 parseerror(parser, "expected array-size as constant positive integer");
2788 cval = (ast_value*)cexp;
2790 tmp = ast_value_new(ctx, "<type[]>", TYPE_ARRAY);
2791 tmp->expression.next = (ast_expression*)var;
2794 if (cval->expression.vtype == TYPE_INTEGER)
2795 tmp->expression.count = cval->constval.vint;
2796 else if (cval->expression.vtype == TYPE_FLOAT)
2797 tmp->expression.count = cval->constval.vfloat;
2801 parseerror(parser, "array-size must be a positive integer constant");
2806 if (parser->tok != ']') {
2808 parseerror(parser, "expected ']' after array-size");
2811 if (!parser_next(parser)) {
2813 parseerror(parser, "error after parsing array size");
2819 /* Parse a complete typename.
2820 * for single-variables (ie. function parameters or typedefs) storebase should be NULL
2821 * but when parsing variables separated by comma
2822 * 'storebase' should point to where the base-type should be kept.
2823 * The base type makes up every bit of type information which comes *before* the
2826 * The following will be parsed in its entirety:
2828 * The 'basetype' in this case is 'void()'
2829 * and if there's a comma after it, say:
2831 * then the type-information 'void()' can be stored in 'storebase'
2833 static ast_value *parse_typename(parser_t *parser, ast_value **storebase)
2835 ast_value *var, *tmp;
2838 const char *name = NULL;
2839 bool isfield = false;
2840 bool wasarray = false;
2842 ctx = parser_ctx(parser);
2844 /* types may start with a dot */
2845 if (parser->tok == '.') {
2847 /* if we parsed a dot we need a typename now */
2848 if (!parser_next(parser)) {
2849 parseerror(parser, "expected typename for field definition");
2852 if (parser->tok != TOKEN_TYPENAME) {
2853 parseerror(parser, "expected typename");
2858 /* generate the basic type value */
2859 var = ast_value_new(ctx, "<type>", parser_token(parser)->constval.t);
2860 /* do not yet turn into a field - remember:
2861 * .void() foo; is a field too
2862 * .void()() foo; is a function
2866 if (!parser_next(parser)) {
2868 parseerror(parser, "parse error after typename");
2872 /* an opening paren now starts the parameter-list of a function
2873 * this is where original-QC has parameter lists.
2874 * We allow a single parameter list here.
2875 * Much like fteqcc we don't allow `float()() x`
2877 if (parser->tok == '(') {
2878 var = parse_parameter_list(parser, var);
2883 /* store the base if requested */
2885 *storebase = ast_value_copy(var);
2887 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2888 tmp->expression.next = (ast_expression*)*storebase;
2893 /* there may be a name now */
2894 if (parser->tok == TOKEN_IDENT) {
2895 name = util_strdup(parser_tokval(parser));
2897 if (!parser_next(parser)) {
2899 parseerror(parser, "error after variable or field declaration");
2904 /* now this may be an array */
2905 if (parser->tok == '[') {
2907 var = parse_arraysize(parser, var);
2912 /* This is the point where we can turn it into a field */
2914 /* turn it into a field if desired */
2915 tmp = ast_value_new(ctx, "<type:f>", TYPE_FIELD);
2916 tmp->expression.next = (ast_expression*)var;
2920 /* now there may be function parens again */
2921 if (parser->tok == '(' && opts_standard == COMPILER_QCC)
2922 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2923 if (parser->tok == '(' && wasarray)
2924 parseerror(parser, "arrays as part of a return type is not supported");
2925 while (parser->tok == '(') {
2926 var = parse_parameter_list(parser, var);
2935 /* finally name it */
2937 if (!ast_value_set_name(var, name)) {
2939 parseerror(parser, "internal error: failed to set name");
2942 /* free the name, ast_value_set_name duplicates */
2949 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields)
2953 ast_expression *old;
2957 ast_value *basetype = NULL;
2959 bool isparam = false;
2960 bool isvector = false;
2961 bool cleanvar = true;
2962 bool wasarray = false;
2964 varentry_t varent, ve[3];
2966 /* get the first complete variable */
2967 var = parse_typename(parser, &basetype);
2970 ast_delete(basetype);
2974 memset(&varent, 0, sizeof(varent));
2975 memset(&ve, 0, sizeof(ve));
2981 /* Part 0: finish the type */
2982 if (parser->tok == '(') {
2983 if (opts_standard == COMPILER_QCC)
2984 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
2985 var = parse_parameter_list(parser, var);
2991 /* we only allow 1-dimensional arrays */
2992 if (parser->tok == '[') {
2994 var = parse_arraysize(parser, var);
3000 if (parser->tok == '(' && wasarray) {
3001 parseerror(parser, "arrays as part of a return type is not supported");
3002 /* we'll still parse the type completely for now */
3004 /* for functions returning functions */
3005 while (parser->tok == '(') {
3006 if (opts_standard == COMPILER_QCC)
3007 parseerror(parser, "C-style function syntax is not allowed in -std=qcc");
3008 var = parse_parameter_list(parser, var);
3016 * check for validity: (end_sys_..., multiple-definitions, prototypes, ...)
3017 * Also: if there was a prototype, `var` will be deleted and set to `proto` which
3018 * is then filled with the previous definition and the parameter-names replaced.
3021 /* Deal with end_sys_ vars */
3023 if (!strcmp(var->name, "end_sys_globals")) {
3024 parser->crc_globals = parser->globals_count;
3027 else if (!strcmp(var->name, "end_sys_fields")) {
3028 parser->crc_fields = parser->fields_count;
3031 if (was_end && var->expression.vtype == TYPE_FIELD) {
3032 if (parsewarning(parser, WARN_END_SYS_FIELDS,
3033 "global '%s' hint should not be a field",
3034 parser_tokval(parser)))
3041 if (!nofields && var->expression.vtype == TYPE_FIELD)
3043 /* deal with field declarations */
3044 old = parser_find_field(parser, var->name);
3046 if (parsewarning(parser, WARN_FIELD_REDECLARED, "field `%s` already declared here: %s:%i",
3047 var->name, ast_ctx(old).file, (int)ast_ctx(old).line))
3056 parseerror(parser, "field `%s` already declared here: %s:%i",
3057 var->name, ast_ctx(old).file, ast_ctx(old).line);
3062 if (opts_standard == COMPILER_QCC &&
3063 (old = parser_find_global(parser, var->name)))
3065 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3066 parseerror(parser, "field `%s` already declared here: %s:%i",
3067 var->name, ast_ctx(old).file, ast_ctx(old).line);
3074 /* deal with other globals */
3075 old = parser_find_global(parser, var->name);
3076 if (old && var->expression.vtype == TYPE_FUNCTION && old->expression.vtype == TYPE_FUNCTION)
3078 /* This is a function which had a prototype */
3079 if (!ast_istype(old, ast_value)) {
3080 parseerror(parser, "internal error: prototype is not an ast_value");
3084 proto = (ast_value*)old;
3085 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)var)) {
3086 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
3088 ast_ctx(proto).file, ast_ctx(proto).line);
3092 /* we need the new parameter-names */
3093 for (i = 0; i < proto->expression.params_count; ++i)
3094 ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
3102 parseerror(parser, "global `%s` already declared here: %s:%i",
3103 var->name, ast_ctx(old).file, ast_ctx(old).line);
3107 if (opts_standard == COMPILER_QCC &&
3108 (old = parser_find_field(parser, var->name)))
3110 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
3111 parseerror(parser, "global `%s` already declared here: %s:%i",
3112 var->name, ast_ctx(old).file, ast_ctx(old).line);
3119 else /* it's not a global */
3121 old = parser_find_local(parser, var->name, parser->blocklocal, &isparam);
3122 if (old && !isparam) {
3123 parseerror(parser, "local `%s` already declared here: %s:%i",
3124 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3128 old = parser_find_local(parser, var->name, 0, &isparam);
3129 if (old && isparam) {
3130 if (parsewarning(parser, WARN_LOCAL_SHADOWS,
3131 "local `%s` is shadowing a parameter", var->name))
3133 parseerror(parser, "local `%s` already declared here: %s:%i",
3134 var->name, ast_ctx(old).file, (int)ast_ctx(old).line);
3138 if (opts_standard != COMPILER_GMQCC) {
3147 * Create the global/local, and deal with vector types.
3150 if (var->expression.vtype == TYPE_VECTOR)
3152 else if (var->expression.vtype == TYPE_FIELD &&
3153 var->expression.next->expression.vtype == TYPE_VECTOR)
3157 if (!create_vector_members(parser, var, ve)) {
3163 varent.name = util_strdup(var->name);
3164 varent.var = (ast_expression*)var;
3167 /* deal with global variables, fields, functions */
3168 if (!nofields && var->expression.vtype == TYPE_FIELD) {
3169 if (!(retval = parser_t_fields_add(parser, varent)))
3172 for (i = 0; i < 3; ++i) {
3173 if (!(retval = parser_t_fields_add(parser, ve[i])))
3177 parser->fields_count -= i+1;
3183 if (!(retval = parser_t_globals_add(parser, varent)))
3186 for (i = 0; i < 3; ++i) {
3187 if (!(retval = parser_t_globals_add(parser, ve[i])))
3191 parser->globals_count -= i+1;
3197 if (!(retval = parser_t_locals_add(parser, varent)))
3199 if (!(retval = ast_block_locals_add(localblock, var))) {
3200 parser->locals_count--;
3204 for (i = 0; i < 3; ++i) {
3205 if (!(retval = parser_t_locals_add(parser, ve[i])))
3207 if (!(retval = ast_block_collect(localblock, ve[i].var)))
3209 ve[i].var = NULL; /* from here it's being collected in the block */
3212 parser->locals_count -= i+1;
3213 localblock->locals_count--;
3220 ve[0].name = ve[1].name = ve[2].name = NULL;
3221 ve[0].var = ve[1].var = ve[2].var = NULL;
3227 if (var->expression.vtype == TYPE_ARRAY) {
3229 snprintf(name, sizeof(name), "%s##SET", var->name);
3230 if (!parser_create_array_setter(parser, var, name))
3232 snprintf(name, sizeof(name), "%s##GET", var->name);
3233 if (!parser_create_array_getter(parser, var, var->expression.next, name))
3236 else if (!localblock && !nofields &&
3237 var->expression.vtype == TYPE_FIELD &&
3238 var->expression.next->expression.vtype == TYPE_ARRAY)
3241 ast_expression *telem;
3243 ast_value *array = (ast_value*)var->expression.next;
3245 if (!ast_istype(var->expression.next, ast_value)) {
3246 parseerror(parser, "internal error: field element type must be an ast_value");
3250 snprintf(name, sizeof(name), "%s##SETF", var->name);
3251 if (!parser_create_array_field_setter(parser, array, name))
3254 telem = ast_type_copy(ast_ctx(var), array->expression.next);
3255 tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
3256 tfield->expression.next = telem;
3257 snprintf(name, sizeof(name), "%s##GETFP", var->name);
3258 if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
3266 if (parser->tok == ';') {
3267 ast_delete(basetype);
3268 if (!parser_next(parser)) {
3269 parseerror(parser, "error after variable declaration");
3275 if (parser->tok == ',')
3278 if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) {
3279 parseerror(parser, "missing comma or semicolon while parsing variables");
3283 if (localblock && opts_standard == COMPILER_QCC) {
3284 if (parsewarning(parser, WARN_LOCAL_CONSTANTS,
3285 "initializing expression turns variable `%s` into a constant in this standard",
3292 if (parser->tok != '{') {
3293 if (parser->tok != '=') {
3294 parseerror(parser, "missing semicolon or initializer");
3298 if (!parser_next(parser)) {
3299 parseerror(parser, "error parsing initializer");
3303 else if (opts_standard == COMPILER_QCC) {
3304 parseerror(parser, "expected '=' before function body in this standard");
3307 if (parser->tok == '#') {
3311 parseerror(parser, "cannot declare builtins within functions");
3314 if (var->expression.vtype != TYPE_FUNCTION) {
3315 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
3318 if (!parser_next(parser)) {
3319 parseerror(parser, "expected builtin number");
3322 if (parser->tok != TOKEN_INTCONST) {
3323 parseerror(parser, "builtin number must be an integer constant");
3326 if (parser_token(parser)->constval.i <= 0) {
3327 parseerror(parser, "builtin number must be an integer greater than zero");
3331 func = ast_function_new(ast_ctx(var), var->name, var);
3333 parseerror(parser, "failed to allocate function for `%s`", var->name);
3336 if (!parser_t_functions_add(parser, func)) {
3337 parseerror(parser, "failed to allocate slot for function `%s`", var->name);
3338 ast_function_delete(func);
3339 var->constval.vfunc = NULL;
3343 func->builtin = -parser_token(parser)->constval.i;
3345 if (!parser_next(parser)) {
3346 parseerror(parser, "expected comma or semicolon");
3347 ast_function_delete(func);
3348 var->constval.vfunc = NULL;
3352 else if (parser->tok == '{' || parser->tok == '[')
3355 parseerror(parser, "cannot declare functions within functions");
3359 if (!parse_function_body(parser, var))
3361 ast_delete(basetype);
3364 ast_expression *cexp;
3367 cexp = parse_expression_leave(parser, true);
3372 cval = (ast_value*)cexp;
3373 if (!ast_istype(cval, ast_value) || !cval->isconst)
3374 parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
3377 var->isconst = true;
3378 if (cval->expression.vtype == TYPE_STRING)
3379 var->constval.vstring = parser_strdup(cval->constval.vstring);
3381 memcpy(&var->constval, &cval->constval, sizeof(var->constval));
3386 MEM_VECTOR_INIT(&sy, out);
3387 MEM_VECTOR_INIT(&sy, ops);
3388 if (!shunt_out_add(&sy, syexp(ast_ctx(var), (ast_expression*)var)) ||
3389 !shunt_out_add(&sy, syexp(ast_ctx(cexp), (ast_expression*)cexp)) ||
3390 !shunt_ops_add(&sy, syop(ast_ctx(var), parser->assign_op)))
3392 parseerror(parser, "internal error: failed to prepare initializer");
3395 else if (!parser_sy_pop(parser, &sy))
3398 if (sy.out_count != 1 && sy.ops_count != 0)
3399 parseerror(parser, "internal error: leaked operands");
3400 else if (!ast_block_exprs_add(localblock, (ast_expression*)sy.out[0].out)) {
3401 parseerror(parser, "failed to create intializing expression");
3402 ast_unref(sy.out[0].out);
3406 MEM_VECTOR_CLEAR(&sy, out);
3407 MEM_VECTOR_CLEAR(&sy, ops);
3412 if (parser->tok == ',') {
3413 if (!parser_next(parser)) {
3414 parseerror(parser, "expected another variable");
3418 if (parser->tok != TOKEN_IDENT) {
3419 parseerror(parser, "expected another variable");
3422 var = ast_value_copy(basetype);
3424 ast_value_set_name(var, parser_tokval(parser));
3425 if (!parser_next(parser)) {
3426 parseerror(parser, "error parsing variable declaration");
3432 if (parser->tok != ';') {
3433 parseerror(parser, "missing semicolon after variables");
3437 if (!parser_next(parser)) {
3438 parseerror(parser, "parse error after variable declaration");
3442 ast_delete(basetype);
3446 if (cleanvar && var)
3448 ast_delete(basetype);
3452 ast_delete(basetype);
3453 if (cleanvar && var)
3455 if (varent.name) mem_d(varent.name);
3456 if (ve[0].name) mem_d(ve[0].name);
3457 if (ve[1].name) mem_d(ve[1].name);
3458 if (ve[2].name) mem_d(ve[2].name);
3459 if (ve[0].var) mem_d(ve[0].var);
3460 if (ve[1].var) mem_d(ve[1].var);
3461 if (ve[2].var) mem_d(ve[2].var);
3465 static bool parser_global_statement(parser_t *parser)
3467 if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
3469 return parse_variable(parser, NULL, false);
3471 else if (parser->tok == TOKEN_KEYWORD)
3473 /* handle 'var' and 'const' */
3474 if (!strcmp(parser_tokval(parser), "var")) {
3475 if (!parser_next(parser)) {
3476 parseerror(parser, "expected variable declaration after 'var'");
3479 return parse_variable(parser, NULL, true);
3483 else if (parser->tok == '$')
3485 if (!parser_next(parser)) {
3486 parseerror(parser, "parse error");
3492 parseerror(parser, "unexpected token: %s", parser->lex->tok.value);
3498 static parser_t *parser;
3503 parser = (parser_t*)mem_a(sizeof(parser_t));
3507 memset(parser, 0, sizeof(*parser));
3509 for (i = 0; i < operator_count; ++i) {
3510 if (operators[i].id == opid1('=')) {
3511 parser->assign_op = operators+i;
3515 if (!parser->assign_op) {
3516 printf("internal error: initializing parser: failed to find assign operator\n");
3523 bool parser_compile(const char *filename)
3525 parser->lex = lex_open(filename);
3527 printf("failed to open file \"%s\"\n", filename);
3531 /* initial lexer/parser state */
3532 parser->lex->flags.noops = true;
3534 if (parser_next(parser))
3536 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
3538 if (!parser_global_statement(parser)) {
3539 if (parser->tok == TOKEN_EOF)
3540 parseerror(parser, "unexpected eof");
3541 else if (!parser->errors)
3542 parseerror(parser, "there have been errors, bailing out");
3543 lex_close(parser->lex);
3549 parseerror(parser, "parse error");
3550 lex_close(parser->lex);
3555 lex_close(parser->lex);
3558 return !parser->errors;
3561 void parser_cleanup()
3564 for (i = 0; i < parser->functions_count; ++i) {
3565 ast_delete(parser->functions[i]);
3567 for (i = 0; i < parser->imm_vector_count; ++i) {
3568 ast_delete(parser->imm_vector[i]);
3570 for (i = 0; i < parser->imm_string_count; ++i) {
3571 ast_delete(parser->imm_string[i]);
3573 for (i = 0; i < parser->imm_float_count; ++i) {
3574 ast_delete(parser->imm_float[i]);
3576 for (i = 0; i < parser->fields_count; ++i) {
3577 ast_delete(parser->fields[i].var);
3578 mem_d(parser->fields[i].name);
3580 for (i = 0; i < parser->globals_count; ++i) {
3581 ast_delete(parser->globals[i].var);
3582 mem_d(parser->globals[i].name);
3584 MEM_VECTOR_CLEAR(parser, functions);
3585 MEM_VECTOR_CLEAR(parser, imm_vector);
3586 MEM_VECTOR_CLEAR(parser, imm_string);
3587 MEM_VECTOR_CLEAR(parser, imm_float);
3588 MEM_VECTOR_CLEAR(parser, globals);
3589 MEM_VECTOR_CLEAR(parser, fields);
3590 MEM_VECTOR_CLEAR(parser, locals);
3595 static uint16_t progdefs_crc_sum(uint16_t old, const char *str)
3597 return util_crc16(old, str, strlen(str));
3600 static void progdefs_crc_file(const char *str)
3602 /* write to progdefs.h here */
3605 static uint16_t progdefs_crc_both(uint16_t old, const char *str)
3607 old = progdefs_crc_sum(old, str);
3608 progdefs_crc_file(str);
3612 static void generate_checksum(parser_t *parser)
3614 uint16_t crc = 0xFFFF;
3617 crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{");
3618 crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n");
3620 progdefs_crc_file("\tint\tpad;\n");
3621 progdefs_crc_file("\tint\tofs_return[3];\n");
3622 progdefs_crc_file("\tint\tofs_parm0[3];\n");
3623 progdefs_crc_file("\tint\tofs_parm1[3];\n");
3624 progdefs_crc_file("\tint\tofs_parm2[3];\n");
3625 progdefs_crc_file("\tint\tofs_parm3[3];\n");
3626 progdefs_crc_file("\tint\tofs_parm4[3];\n");
3627 progdefs_crc_file("\tint\tofs_parm5[3];\n");
3628 progdefs_crc_file("\tint\tofs_parm6[3];\n");
3629 progdefs_crc_file("\tint\tofs_parm7[3];\n");
3631 for (i = 0; i < parser->crc_globals; ++i) {
3632 if (!ast_istype(parser->globals[i].var, ast_value))
3634 switch (parser->globals[i].var->expression.vtype) {
3635 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3636 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3637 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3638 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3640 crc = progdefs_crc_both(crc, "\tint\t");
3643 crc = progdefs_crc_both(crc, parser->globals[i].name);
3644 crc = progdefs_crc_both(crc, ";\n");
3646 crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n");
3647 for (i = 0; i < parser->crc_fields; ++i) {
3648 if (!ast_istype(parser->fields[i].var, ast_value))
3650 switch (parser->fields[i].var->expression.next->expression.vtype) {
3651 case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break;
3652 case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break;
3653 case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break;
3654 case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break;
3656 crc = progdefs_crc_both(crc, "\tint\t");
3659 crc = progdefs_crc_both(crc, parser->fields[i].name);
3660 crc = progdefs_crc_both(crc, ";\n");
3662 crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
3667 bool parser_finish(const char *output)
3673 if (!parser->errors)
3675 ir = ir_builder_new("gmqcc_out");
3677 printf("failed to allocate builder\n");
3681 for (i = 0; i < parser->fields_count; ++i) {
3684 if (!ast_istype(parser->fields[i].var, ast_value))
3686 field = (ast_value*)parser->fields[i].var;
3687 isconst = field->isconst;
3688 field->isconst = false;
3689 if (!ast_global_codegen((ast_value*)field, ir, true)) {
3690 printf("failed to generate field %s\n", field->name);
3691 ir_builder_delete(ir);
3696 ast_expression *subtype;
3697 field->isconst = true;
3698 subtype = field->expression.next;
3699 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
3700 if (subtype->expression.vtype == TYPE_FIELD)
3701 ifld->fieldtype = subtype->expression.next->expression.vtype;
3702 else if (subtype->expression.vtype == TYPE_FUNCTION)
3703 ifld->outtype = subtype->expression.next->expression.vtype;
3704 (void)!ir_value_set_field(field->ir_v, ifld);
3707 for (i = 0; i < parser->globals_count; ++i) {
3709 if (!ast_istype(parser->globals[i].var, ast_value))
3711 asvalue = (ast_value*)(parser->globals[i].var);
3712 if (!asvalue->uses && !asvalue->isconst && asvalue->expression.vtype != TYPE_FUNCTION) {
3713 if (strcmp(asvalue->name, "end_sys_globals") &&
3714 strcmp(asvalue->name, "end_sys_fields"))
3716 retval = retval && !genwarning(ast_ctx(asvalue), WARN_UNUSED_VARIABLE,
3717 "unused global: `%s`", asvalue->name);
3720 if (!ast_global_codegen(asvalue, ir, false)) {
3721 printf("failed to generate global %s\n", parser->globals[i].name);
3722 ir_builder_delete(ir);
3726 for (i = 0; i < parser->imm_float_count; ++i) {
3727 if (!ast_global_codegen(parser->imm_float[i], ir, false)) {
3728 printf("failed to generate global %s\n", parser->imm_float[i]->name);
3729 ir_builder_delete(ir);
3733 for (i = 0; i < parser->imm_string_count; ++i) {
3734 if (!ast_global_codegen(parser->imm_string[i], ir, false)) {
3735 printf("failed to generate global %s\n", parser->imm_string[i]->name);
3736 ir_builder_delete(ir);
3740 for (i = 0; i < parser->imm_vector_count; ++i) {
3741 if (!ast_global_codegen(parser->imm_vector[i], ir, false)) {
3742 printf("failed to generate global %s\n", parser->imm_vector[i]->name);
3743 ir_builder_delete(ir);
3747 for (i = 0; i < parser->globals_count; ++i) {
3749 if (!ast_istype(parser->globals[i].var, ast_value))
3751 asvalue = (ast_value*)(parser->globals[i].var);
3752 if (asvalue->setter) {
3753 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3754 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3755 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3757 printf("failed to generate setter for %s\n", parser->globals[i].name);
3758 ir_builder_delete(ir);
3762 if (asvalue->getter) {
3763 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3764 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3765 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3767 printf("failed to generate getter for %s\n", parser->globals[i].name);
3768 ir_builder_delete(ir);
3773 for (i = 0; i < parser->fields_count; ++i) {
3775 asvalue = (ast_value*)(parser->fields[i].var->expression.next);
3777 if (!ast_istype((ast_expression*)asvalue, ast_value))
3779 if (asvalue->expression.vtype != TYPE_ARRAY)
3781 if (asvalue->setter) {
3782 if (!ast_global_codegen(asvalue->setter, ir, false) ||
3783 !ast_function_codegen(asvalue->setter->constval.vfunc, ir) ||
3784 !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func))
3786 printf("failed to generate setter for %s\n", parser->fields[i].name);
3787 ir_builder_delete(ir);
3791 if (asvalue->getter) {
3792 if (!ast_global_codegen(asvalue->getter, ir, false) ||
3793 !ast_function_codegen(asvalue->getter->constval.vfunc, ir) ||
3794 !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func))
3796 printf("failed to generate getter for %s\n", parser->fields[i].name);
3797 ir_builder_delete(ir);
3802 for (i = 0; i < parser->functions_count; ++i) {
3803 if (!ast_function_codegen(parser->functions[i], ir)) {
3804 printf("failed to generate function %s\n", parser->functions[i]->name);
3805 ir_builder_delete(ir);
3808 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
3809 printf("failed to finalize function %s\n", parser->functions[i]->name);
3810 ir_builder_delete(ir);
3817 ir_builder_dump(ir, printf);
3819 generate_checksum(parser);
3821 if (!ir_builder_generate(ir, output)) {
3822 printf("*** failed to generate output file\n");
3823 ir_builder_delete(ir);
3828 ir_builder_delete(ir);
3832 printf("*** there were compile errors\n");