[qfcc] Indicate type width in type strings

Makes for much more informative error messages for type mismatches
(confusing when both sides look the same).
This commit is contained in:
Bill Currie 2022-04-26 19:57:43 +09:00
parent af2dfde37a
commit 8912c65029
4 changed files with 30 additions and 6 deletions

View file

@ -166,6 +166,7 @@ type_t *alias_type (type_t *type, type_t *alias_chain, const char *name);
const type_t *unalias_type (const type_t *type) __attribute__((pure));
const type_t *dereference_type (const type_t *type) __attribute__((pure));
void print_type_str (struct dstring_s *str, const type_t *type);
const char *get_type_string (const type_t *type);
void print_type (const type_t *type);
void dump_dot_type (void *t, const char *filename);
const char *encode_params (const type_t *type);

View file

@ -596,7 +596,8 @@ initialize_def (symbol_t *sym, expr_t *init, defspace_t *space,
}
init_type = get_type (init);
if (!type_assignable (sym->type, init_type)) {
error (init, "type mismatch in initializer");
error (init, "type mismatch in initializer: %s = %s",
get_type_string (sym->type), get_type_string (init_type));
return;
}
if (storage == sc_local && local_expr) {

View file

@ -828,11 +828,12 @@ entity_compare (int op, expr_t *e1, expr_t *e2)
static expr_t *
invalid_binary_expr (int op, expr_t *e1, expr_t *e2)
{
etype_t t1, t2;
t1 = extract_type (e1);
t2 = extract_type (e2);
type_t *t1, *t2;
t1 = get_type (e1);
t2 = get_type (e2);
return error (e1, "invalid binary expression: %s %s %s",
pr_type_name[t1], get_op_string (op), pr_type_name[t2]);
get_type_string (t1), get_op_string (op),
get_type_string (t2));
}
static expr_t *

View file

@ -711,7 +711,9 @@ print_type_str (dstring_t *str, const type_t *type)
case ev_short:
case ev_ushort:
case ev_double:
dasprintf (str, " %s", pr_type_name[type->type]);
dasprintf (str, " %s%s", pr_type_name[type->type],
type->width > 1 ? va (0, "{%d}", type->width)
: "");
return;
case ev_invalid:
case ev_type_count:
@ -722,6 +724,25 @@ print_type_str (dstring_t *str, const type_t *type)
internal_error (0, "bad type meta:type %d:%d", type->meta, type->type);
}
const char *
get_type_string (const type_t *type)
{
static dstring_t *type_str[8];
static int str_index;
if (!type_str[str_index]) {
type_str[str_index] = dstring_newstr ();
}
dstring_clearstr (type_str[str_index]);
print_type_str (type_str[str_index], type);
const char *str = type_str[str_index++]->str;
str_index %= sizeof (type_str) / sizeof (type_str[0]);
while (*str == ' ') {
str++;
}
return str;
}
void
print_type (const type_t *type)
{