Change pointer_type to a method of struct type

I noticed that pointer_type is declared in language.h and defined in
language.c.  However, it really has to do with types, so it should
have been in gdbtypes.h all along.

This patch changes it to be a method on struct type.  And, I went
through uses of TYPE_IS_REFERENCE and updated many spots to use the
new method as well.  (I didn't update ones that were in arch-specific
code, as I couldn't readily test that.)
This commit is contained in:
Tom Tromey 2021-09-11 13:58:04 -06:00
parent 0086a91cee
commit 809f3be12c
17 changed files with 44 additions and 60 deletions

View file

@ -961,7 +961,7 @@ static void
gen_ptradd (struct agent_expr *ax, struct axs_value *value,
struct axs_value *value1, struct axs_value *value2)
{
gdb_assert (pointer_type (value1->type));
gdb_assert (value1->type->is_pointer_or_reference ());
gdb_assert (value2->type->code () == TYPE_CODE_INT);
gen_scale (ax, aop_mul, value1->type);
@ -977,7 +977,7 @@ static void
gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
struct axs_value *value1, struct axs_value *value2)
{
gdb_assert (pointer_type (value1->type));
gdb_assert (value1->type->is_pointer_or_reference ());
gdb_assert (value2->type->code () == TYPE_CODE_INT);
gen_scale (ax, aop_mul, value1->type);
@ -994,8 +994,8 @@ gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
struct axs_value *value1, struct axs_value *value2,
struct type *result_type)
{
gdb_assert (pointer_type (value1->type));
gdb_assert (pointer_type (value2->type));
gdb_assert (value1->type->is_pointer_or_reference ());
gdb_assert (value2->type->is_pointer_or_reference ());
if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
!= TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
@ -1014,7 +1014,7 @@ gen_equal (struct agent_expr *ax, struct axs_value *value,
struct axs_value *value1, struct axs_value *value2,
struct type *result_type)
{
if (pointer_type (value1->type) || pointer_type (value2->type))
if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
ax_simple (ax, aop_equal);
else
gen_binop (ax, value, value1, value2,
@ -1028,7 +1028,7 @@ gen_less (struct agent_expr *ax, struct axs_value *value,
struct axs_value *value1, struct axs_value *value2,
struct type *result_type)
{
if (pointer_type (value1->type) || pointer_type (value2->type))
if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
ax_simple (ax, aop_less_unsigned);
else
gen_binop (ax, value, value1, value2,
@ -1095,7 +1095,7 @@ gen_deref (struct axs_value *value)
{
/* The caller should check the type, because several operators use
this, and we don't know what error message to generate. */
if (!pointer_type (value->type))
if (!value->type->is_pointer_or_reference ())
internal_error (__FILE__, __LINE__,
_("gen_deref: expected a pointer"));
@ -1401,7 +1401,7 @@ gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
/* Follow pointers until we reach a non-pointer. These aren't the C
semantics, but they're what the normal GDB evaluator does, so we
should at least be consistent. */
while (pointer_type (value->type))
while (value->type->is_pointer_or_reference ())
{
require_rvalue (ax, value);
gen_deref (value);
@ -2070,13 +2070,13 @@ gen_expr_binop_rest (struct expression *exp,
{
case BINOP_ADD:
if (value1->type->code () == TYPE_CODE_INT
&& pointer_type (value2->type))
&& value2->type->is_pointer_or_reference ())
{
/* Swap the values and proceed normally. */
ax_simple (ax, aop_swap);
gen_ptradd (ax, value, value2, value1);
}
else if (pointer_type (value1->type)
else if (value1->type->is_pointer_or_reference ()
&& value2->type->code () == TYPE_CODE_INT)
gen_ptradd (ax, value, value1, value2);
else
@ -2084,11 +2084,11 @@ gen_expr_binop_rest (struct expression *exp,
aop_add, aop_add, 1, "addition");
break;
case BINOP_SUB:
if (pointer_type (value1->type)
if (value1->type->is_pointer_or_reference ()
&& value2->type->code () == TYPE_CODE_INT)
gen_ptrsub (ax,value, value1, value2);
else if (pointer_type (value1->type)
&& pointer_type (value2->type))
else if (value1->type->is_pointer_or_reference ()
&& value2->type->is_pointer_or_reference ())
/* FIXME --- result type should be ptrdiff_t */
gen_ptrdiff (ax, value, value1, value2,
builtin_type (ax->gdbarch)->builtin_long);
@ -2285,7 +2285,7 @@ gen_expr_unop (struct expression *exp,
case UNOP_IND:
lhs->generate_ax (exp, ax, value);
gen_usual_unary (ax, value);
if (!pointer_type (value->type))
if (!value->type->is_pointer_or_reference ())
error (_("Argument of unary `*' is not a pointer."));
gen_deref (value);
break;

View file

@ -486,7 +486,7 @@ c_value_print (struct value *val, struct ui_file *stream,
type = check_typedef (value_type (val));
if (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
if (type->is_pointer_or_reference ())
{
struct type *original_type = value_type (val);

View file

@ -574,8 +574,7 @@ cplus_number_of_children (const struct varobj *var)
if (opts.objectprint)
{
value = var->value.get ();
lookup_actual_type = (TYPE_IS_REFERENCE (var->type)
|| var->type->code () == TYPE_CODE_PTR);
lookup_actual_type = var->type->is_pointer_or_reference ();
}
adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
@ -611,8 +610,7 @@ cplus_number_of_children (const struct varobj *var)
const struct varobj *parent = var->parent;
value = parent->value.get ();
lookup_actual_type = (TYPE_IS_REFERENCE (parent->type)
|| parent->type->code () == TYPE_CODE_PTR);
lookup_actual_type = parent->type->is_pointer_or_reference ();
}
adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
@ -716,8 +714,7 @@ cplus_describe_child (const struct varobj *parent, int index,
var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
if (opts.objectprint)
lookup_actual_type = (TYPE_IS_REFERENCE (var->type)
|| var->type->code () == TYPE_CODE_PTR);
lookup_actual_type = var->type->is_pointer_or_reference ();
value = var->value.get ();
type = varobj_get_value_type (var);
if (cfull_expression)

View file

@ -1134,7 +1134,7 @@ complete_expression (completion_tracker &tracker,
for (;;)
{
type = check_typedef (type);
if (type->code () != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
if (!type->is_pointer_or_reference ())
break;
type = TYPE_TARGET_TYPE (type);
}

View file

@ -1335,8 +1335,7 @@ add_symbol_overload_list_adl_namespace (struct type *type,
const char *type_name;
int i, prefix_len;
while (type->code () == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (type)
while (type->is_pointer_or_reference ()
|| type->code () == TYPE_CODE_ARRAY
|| type->code () == TYPE_CODE_TYPEDEF)
{

View file

@ -1596,12 +1596,10 @@ eval_op_ind (struct type *expect_type, struct expression *exp,
There is a risk that this dereference will have side-effects
in the inferior, but being able to print accurate type
information seems worth the risk. */
if ((type->code () != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type))
if (!type->is_pointer_or_reference ()
|| !is_dynamic_type (TYPE_TARGET_TYPE (type)))
{
if (type->code () == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (type)
if (type->is_pointer_or_reference ()
/* In C you can dereference an array to get the 1st elt. */
|| type->code () == TYPE_CODE_ARRAY)
return value_zero (TYPE_TARGET_TYPE (type),
@ -2706,8 +2704,7 @@ unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
EVAL_AVOID_SIDE_EFFECTS);
struct type *type = check_typedef (value_type (val));
if (type->code () != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type)
if (!type->is_pointer_or_reference ()
&& type->code () != TYPE_CODE_ARRAY)
error (_("Attempt to take contents of a non-pointer value."));
type = TYPE_TARGET_TYPE (type);

View file

@ -153,7 +153,7 @@ extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
CORE_ADDR
extract_typed_address (const gdb_byte *buf, struct type *type)
{
if (type->code () != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
if (!type->is_pointer_or_reference ())
internal_error (__FILE__, __LINE__,
_("extract_typed_address: "
"type is not a pointer or reference"));
@ -206,7 +206,7 @@ template void store_integer (gdb_byte *addr, int len,
void
store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
{
if (type->code () != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
if (!type->is_pointer_or_reference ())
internal_error (__FILE__, __LINE__,
_("store_typed_address: "
"type is not a pointer or reference"));

View file

@ -4774,7 +4774,7 @@ rank_one_type (struct type *parm, struct type *arg, struct value *value)
struct type *t2 = arg;
/* For pointers and references, compare target type. */
if (parm->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (parm))
if (parm->is_pointer_or_reference ())
{
t1 = TYPE_TARGET_TYPE (parm);
t2 = TYPE_TARGET_TYPE (arg);

View file

@ -1355,6 +1355,12 @@ struct type
return main_type->type_specific.int_stuff.bit_offset;
}
/* Return true if this is a pointer or reference type. */
bool is_pointer_or_reference () const
{
return this->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (this);
}
/* * Type that is a pointer to this type.
NULL if no such pointer-to type is known yet.
The debugger may add the address of such a type

View file

@ -388,14 +388,6 @@ language_info ()
}
/* Returns non-zero if the value is a pointer type. */
int
pointer_type (struct type *type)
{
return type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
}
/* This page contains functions that return info about
(struct value) values used in GDB. */

View file

@ -774,10 +774,6 @@ extern enum language set_language (enum language);
&& ((c) < 0x7F || (c) >= 0xA0) \
&& (!sevenbit_strings || (c) < 0x80))
/* Type predicates */
extern int pointer_type (struct type *);
/* Error messages */
extern void range_error (const char *, ...) ATTRIBUTE_PRINTF (1, 2);

View file

@ -470,7 +470,7 @@ typy_get_composite (struct type *type)
GDB_PY_HANDLE_EXCEPTION (except);
}
if (type->code () != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
if (!type->is_pointer_or_reference ())
break;
type = TYPE_TARGET_TYPE (type);
}

View file

@ -400,7 +400,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure)
type = value_type (val);
type = check_typedef (type);
if (((type->code () == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type))
if (type->is_pointer_or_reference ()
&& (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT))
{
struct value *target;
@ -851,7 +851,7 @@ value_has_field (struct value *v, PyObject *field)
{
val_type = value_type (v);
val_type = check_typedef (val_type);
if (TYPE_IS_REFERENCE (val_type) || val_type->code () == TYPE_CODE_PTR)
if (val_type->is_pointer_or_reference ())
val_type = check_typedef (TYPE_TARGET_TYPE (val_type));
type_code = val_type->code ();

View file

@ -2068,7 +2068,7 @@ lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
/* I'm not really sure that type of this can ever
be typedefed; just be safe. */
t = check_typedef (t);
if (t->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
if (t->is_pointer_or_reference ())
t = TYPE_TARGET_TYPE (t);
if (t->code () != TYPE_CODE_STRUCT

View file

@ -544,7 +544,7 @@ whatis_exp (const char *exp, int show)
get_user_print_options (&opts);
if (val != NULL && opts.objectprint)
{
if (((type->code () == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type))
if (type->is_pointer_or_reference ()
&& (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT))
real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
else if (type->code () == TYPE_CODE_STRUCT)

View file

@ -2335,7 +2335,7 @@ value_struct_elt (struct value **argp,
/* Follow pointers until we get to a non-pointer. */
while (t->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
while (t->is_pointer_or_reference ())
{
*argp = value_ind (*argp);
/* Don't coerce fn pointer to fn and then back again! */
@ -2422,7 +2422,7 @@ value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
t = check_typedef (value_type (*argp));
while (t->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
while (t->is_pointer_or_reference ())
{
*argp = value_ind (*argp);
if (check_typedef (value_type (*argp))->code () != TYPE_CODE_FUNC)
@ -2575,7 +2575,7 @@ value_find_oload_method_list (struct value **argp, const char *method,
t = check_typedef (value_type (*argp));
/* Code snarfed from value_struct_elt. */
while (t->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
while (t->is_pointer_or_reference ())
{
*argp = value_ind (*argp);
/* Don't coerce fn pointer to fn and then back again! */
@ -2969,8 +2969,7 @@ find_overload_match (gdb::array_view<value *> args,
struct type *objtype = check_typedef (obj_type);
if (temp_type->code () != TYPE_CODE_PTR
&& (objtype->code () == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (objtype)))
&& objtype->is_pointer_or_reference ())
{
temp = value_addr (temp);
}

View file

@ -1182,7 +1182,7 @@ value_actual_type (struct value *value, int resolve_simple_types,
{
/* If result's target type is TYPE_CODE_STRUCT, proceed to
fetch its rtti type. */
if ((result->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (result))
if (result->is_pointer_or_reference ()
&& (check_typedef (TYPE_TARGET_TYPE (result))->code ()
== TYPE_CODE_STRUCT)
&& !value_optimized_out (value))
@ -2778,8 +2778,7 @@ value_as_address (struct value *val)
converted to pointers; usually, the ABI doesn't either, but
ABI-specific code is a more reasonable place to handle it. */
if (value_type (val)->code () != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (value_type (val))
if (!value_type (val)->is_pointer_or_reference ()
&& gdbarch_integer_to_address_p (gdbarch))
return gdbarch_integer_to_address (gdbarch, value_type (val),
value_contents (val));
@ -3726,8 +3725,7 @@ readjust_indirect_value_type (struct value *value, struct type *enc_type,
struct value *original_value,
CORE_ADDR original_value_address)
{
gdb_assert (original_type->code () == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (original_type));
gdb_assert (original_type->is_pointer_or_reference ());
struct type *original_target_type = TYPE_TARGET_TYPE (original_type);
gdb::array_view<const gdb_byte> view;