public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Change pointer_type to a method of struct type
@ 2021-09-11 20:01 Tom Tromey
  2021-09-23 19:19 ` Pedro Alves
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2021-09-11 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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.)
---
 gdb/ax-gdb.c          | 28 ++++++++++++++--------------
 gdb/c-valprint.c      |  2 +-
 gdb/c-varobj.c        |  9 +++------
 gdb/completer.c       |  2 +-
 gdb/cp-support.c      |  3 +--
 gdb/eval.c            |  9 +++------
 gdb/findvar.c         |  4 ++--
 gdb/gdbtypes.c        |  2 +-
 gdb/gdbtypes.h        |  6 ++++++
 gdb/language.c        |  8 --------
 gdb/language.h        |  4 ----
 gdb/python/py-type.c  |  2 +-
 gdb/python/py-value.c |  4 ++--
 gdb/symtab.c          |  2 +-
 gdb/typeprint.c       |  2 +-
 gdb/valops.c          |  9 ++++-----
 gdb/value.c           |  8 +++-----
 17 files changed, 44 insertions(+), 60 deletions(-)

diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index 009c128c83c..2eea12cfacd 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -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;
diff --git a/gdb/c-valprint.c b/gdb/c-valprint.c
index 9c82869525f..d3071d1f5b9 100644
--- a/gdb/c-valprint.c
+++ b/gdb/c-valprint.c
@@ -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);
 
diff --git a/gdb/c-varobj.c b/gdb/c-varobj.c
index f6db3a5d1ba..8056c1c9ec1 100644
--- a/gdb/c-varobj.c
+++ b/gdb/c-varobj.c
@@ -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)
diff --git a/gdb/completer.c b/gdb/completer.c
index 40b976c8001..9444995575b 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -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);
 	}
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 5bd8fd4e940..fb4c8364ae0 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -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)
     {
diff --git a/gdb/eval.c b/gdb/eval.c
index 5c348c34e66..1c5c8cf38af 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -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);
diff --git a/gdb/findvar.c b/gdb/findvar.c
index fcd97191c14..56edbdba578 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -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"));
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 8fbc5d3a80b..67593dac055 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -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);
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index bfb7f2965a6..2a641122aec 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -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
diff --git a/gdb/language.c b/gdb/language.c
index 45ce2ebcc92..0354fb81865 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -388,14 +388,6 @@ language_info ()
 }
 \f
 
-/* 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);
-}
-
-\f
 /* This page contains functions that return info about
    (struct value) values used in GDB.  */
 
diff --git a/gdb/language.h b/gdb/language.h
index 21ed47b3580..1526b817962 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -766,10 +766,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);
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index d82bdf84957..aa1553baf9e 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -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);
     }
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 26d5940f842..d45df5fd113 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -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 ();
diff --git a/gdb/symtab.c b/gdb/symtab.c
index fa3f42207ec..a30d900cf8d 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -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
diff --git a/gdb/typeprint.c b/gdb/typeprint.c
index 0776f634215..1f43ab32e11 100644
--- a/gdb/typeprint.c
+++ b/gdb/typeprint.c
@@ -538,7 +538,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)
diff --git a/gdb/valops.c b/gdb/valops.c
index 50874a5f55d..67298604dd3 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -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);
 	}
diff --git a/gdb/value.c b/gdb/value.c
index 2cbaadc3641..3a2bc13985e 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -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;
-- 
2.31.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Change pointer_type to a method of struct type
  2021-09-11 20:01 [PATCH] Change pointer_type to a method of struct type Tom Tromey
@ 2021-09-23 19:19 ` Pedro Alves
  0 siblings, 0 replies; 2+ messages in thread
From: Pedro Alves @ 2021-09-23 19:19 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 2021-09-11 9:01 p.m., Tom Tromey wrote:
> 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.)

LGTM.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-23 19:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-11 20:01 [PATCH] Change pointer_type to a method of struct type Tom Tromey
2021-09-23 19:19 ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).