Make convert_value create a new value.

It operating in-place proved to be troublesome.
This commit is contained in:
Bill Currie 2012-12-05 22:16:08 +09:00
parent a099847026
commit 7071a46936
3 changed files with 13 additions and 16 deletions

View file

@ -54,7 +54,8 @@ struct ex_value_s *new_uinteger_val (int uinteger_val);
struct ex_value_s *new_short_val (short short_val); struct ex_value_s *new_short_val (short short_val);
struct ex_value_s *new_nil_val (struct type_s *type); struct ex_value_s *new_nil_val (struct type_s *type);
void convert_value (struct ex_value_s *value, struct type_s *type); struct ex_value_s * convert_value (struct ex_value_s *value,
struct type_s *type);
struct def_s *emit_value (struct ex_value_s *value, struct def_s *def); struct def_s *emit_value (struct ex_value_s *value, struct def_s *def);
int ReuseString (const char *str); int ReuseString (const char *str);

View file

@ -569,14 +569,14 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
if (init->e.value->v.pointer.def) if (init->e.value->v.pointer.def)
reloc_def_field (init->e.value->v.pointer.def, sym->s.def); reloc_def_field (init->e.value->v.pointer.def, sym->s.def);
} else { } else {
ex_value_t v = *init->e.value; ex_value_t *v = init->e.value;
if (is_scalar (sym->type)) if (is_scalar (sym->type))
convert_value (&v, sym->type); v = convert_value (v, sym->type);
if (v.type == ev_string) { if (v->type == ev_string) {
EMIT_STRING (sym->s.def->space, D_STRING (sym->s.def), EMIT_STRING (sym->s.def->space, D_STRING (sym->s.def),
v.v.string_val); v->v.string_val);
} else { } else {
memcpy (D_POINTER (void, sym->s.def), &v.v, memcpy (D_POINTER (void, sym->s.def), &v->v,
type_size (type) * sizeof (pr_type_t)); type_size (type) * sizeof (pr_type_t));
} }
} }

View file

@ -373,30 +373,26 @@ value_as_uint (ex_value_t *value)
return 0; return 0;
} }
void ex_value_t *
convert_value (ex_value_t *value, type_t *type) convert_value (ex_value_t *value, type_t *type)
{ {
if (!is_scalar (type) || !is_scalar (ev_types[value->type])) { if (!is_scalar (type) || !is_scalar (ev_types[value->type])) {
error (0, "unable to convert non-scalar value"); error (0, "unable to convert non-scalar value");
return; return value;
} }
if (is_float (type)) { if (is_float (type)) {
float val = value_as_float (value); float val = value_as_float (value);
value->type = ev_float; return new_float_val (val);
value->v.float_val = val;
} else if (type->type == ev_short) { } else if (type->type == ev_short) {
int val = value_as_int (value); int val = value_as_int (value);
value->type = ev_short; return new_short_val (val);
value->v.short_val = val;
} else if (type->type == ev_uinteger) { } else if (type->type == ev_uinteger) {
unsigned val = value_as_uint (value); unsigned val = value_as_uint (value);
value->type = ev_uinteger; return new_uinteger_val (val);
value->v.uinteger_val = val;
} else { } else {
//FIXME handle enums separately? //FIXME handle enums separately?
int val = value_as_int (value); int val = value_as_int (value);
value->type = ev_integer; return new_integer_val (val);
value->v.integer_val = val;
} }
} }