Implement 'Enum_Val and 'Enum_Rep

This patch implements the Ada 2022 attributes 'Enum_Val and 'Enum_Rep.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
This commit is contained in:
Tom Tromey
2023-06-23 09:59:38 -06:00
parent 1e5ae3d17f
commit 22f6f7979c
6 changed files with 99 additions and 16 deletions

View File

@@ -8815,7 +8815,8 @@ val_atr (struct type *type, LONGEST val)
}
struct value *
ada_val_atr (enum noside noside, struct type *type, struct value *arg)
ada_val_atr (struct expression *exp, enum noside noside, struct type *type,
struct value *arg)
{
if (noside == EVAL_AVOID_SIDE_EFFECTS)
return value::zero (type, not_lval);
@@ -8827,6 +8828,52 @@ ada_val_atr (enum noside noside, struct type *type, struct value *arg)
return val_atr (type, value_as_long (arg));
}
/* Implementation of the enum_rep attribute. */
struct value *
ada_atr_enum_rep (struct expression *exp, enum noside noside, struct type *type,
struct value *arg)
{
struct type *inttype = builtin_type (exp->gdbarch)->builtin_int;
if (noside == EVAL_AVOID_SIDE_EFFECTS)
return value::zero (inttype, not_lval);
if (type->code () == TYPE_CODE_RANGE)
type = type->target_type ();
if (type->code () != TYPE_CODE_ENUM)
error (_("'Enum_Rep only defined on enum types"));
if (!types_equal (type, arg->type ()))
error (_("'Enum_Rep requires argument to have same type as enum"));
return value_cast (inttype, arg);
}
/* Implementation of the enum_val attribute. */
struct value *
ada_atr_enum_val (struct expression *exp, enum noside noside, struct type *type,
struct value *arg)
{
struct type *original_type = type;
if (noside == EVAL_AVOID_SIDE_EFFECTS)
return value::zero (original_type, not_lval);
if (type->code () == TYPE_CODE_RANGE)
type = type->target_type ();
if (type->code () != TYPE_CODE_ENUM)
error (_("'Enum_Val only defined on enum types"));
if (!integer_type_p (arg->type ()))
error (_("'Enum_Val requires integral argument"));
LONGEST value = value_as_long (arg);
for (int i = 0; i < type->num_fields (); ++i)
{
if (type->field (i).loc_enumval () == value)
return value_from_longest (original_type, value);
}
error (_("value %s not found in enum"), plongest (value));
}
/* Evaluation */
@@ -10919,15 +10966,6 @@ ada_var_value_operation::do_generate_ax (struct expression *exp,
var_value_operation::do_generate_ax (exp, ax, value, cast_type);
}
value *
ada_atr_val_operation::evaluate (struct type *expect_type,
struct expression *exp,
enum noside noside)
{
value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
return ada_val_atr (noside, std::get<0> (m_storage), arg);
}
value *
ada_unop_ind_operation::evaluate (struct type *expect_type,
struct expression *exp,