[2/7] Add accessors for members of complex numbers

Message ID 20200307152246.32630-3-tom@tromey.com
State New
Headers show
Series
  • Update complex number support
Related show

Commit Message

Tom Tromey March 7, 2020, 3:22 p.m.
This introduces two new functions that make it simpler to access the
components of a complex number.

gdb/ChangeLog
2020-03-07  Tom Tromey  <tom@tromey.com>

	* value.h (value_real_part, value_imaginary_part): Declare.
	* valops.c (value_real_part, value_imaginary_part): New
	functions.
	* value.c (creal_internal_fn, cimag_internal_fn): Use accessors.
---
 gdb/ChangeLog |  7 +++++++
 gdb/valops.c  | 25 +++++++++++++++++++++++++
 gdb/value.c   |  5 ++---
 gdb/value.h   |  8 ++++++++
 4 files changed, 42 insertions(+), 3 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/valops.c b/gdb/valops.c
index d48474665c3..83fd2584b59 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3877,6 +3877,31 @@  value_literal_complex (struct value *arg1,
   return val;
 }
 
+/* See value.h.  */
+
+struct value *
+value_real_part (struct value *value)
+{
+  struct type *type = check_typedef (value_type (value));
+  struct type *ttype = TYPE_TARGET_TYPE (type);
+
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_COMPLEX);
+  return value_from_component (value, ttype, 0);
+}
+
+/* See value.h.  */
+
+struct value *
+value_imaginary_part (struct value *value)
+{
+  struct type *type = check_typedef (value_type (value));
+  struct type *ttype = TYPE_TARGET_TYPE (type);
+
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_COMPLEX);
+  return value_from_component (value, ttype,
+			       TYPE_LENGTH (check_typedef (ttype)));
+}
+
 /* Cast a value into the appropriate complex data type.  */
 
 static struct value *
diff --git a/gdb/value.c b/gdb/value.c
index ceaeb835fa7..f722c272d8b 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -3962,7 +3962,7 @@  creal_internal_fn (struct gdbarch *gdbarch,
   type *ctype = check_typedef (value_type (cval));
   if (TYPE_CODE (ctype) != TYPE_CODE_COMPLEX)
     error (_("expected a complex number"));
-  return value_from_component (cval, TYPE_TARGET_TYPE (ctype), 0);
+  return value_real_part (cval);
 }
 
 /* Implementation of the convenience function $_cimag.  Extracts the
@@ -3981,8 +3981,7 @@  cimag_internal_fn (struct gdbarch *gdbarch,
   type *ctype = check_typedef (value_type (cval));
   if (TYPE_CODE (ctype) != TYPE_CODE_COMPLEX)
     error (_("expected a complex number"));
-  return value_from_component (cval, TYPE_TARGET_TYPE (ctype),
-			       TYPE_LENGTH (TYPE_TARGET_TYPE (ctype)));
+  return value_imaginary_part (cval);
 }
 
 #if GDB_SELF_TEST
diff --git a/gdb/value.h b/gdb/value.h
index df6d80c2a70..27869989fbc 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1148,6 +1148,14 @@  extern struct value *value_slice (struct value *, int, int);
 extern struct value *value_literal_complex (struct value *, struct value *,
 					    struct type *);
 
+/* Return the real part of a complex value.  */
+
+extern struct value *value_real_part (struct value *value);
+
+/* Return the imaginary part of a complex value.  */
+
+extern struct value *value_imaginary_part (struct value *value);
+
 extern struct value *find_function_in_inferior (const char *,
 						struct objfile **);