Add accessors for members of complex numbers
authorTom Tromey <tom@tromey.com>
Wed, 1 Apr 2020 20:09:52 +0000 (14:09 -0600)
committerTom Tromey <tromey@adacore.com>
Wed, 1 Apr 2020 20:09:52 +0000 (14:09 -0600)
This introduces two new functions that make it simpler to access the
components of a complex number.

gdb/ChangeLog
2020-04-01  Tom Tromey  <tom@tromey.com>

* valprint.c (generic_value_print_complex): Use accessors.
* 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
gdb/valops.c
gdb/valprint.c
gdb/value.c
gdb/value.h

index 7a883e81c2fcf58d1c6dfb045565e3d39cb43dc3..d1f408f1a47971d0fdaa13c62d5b8e1e55d21921 100644 (file)
@@ -1,3 +1,11 @@
+2020-04-01  Tom Tromey  <tom@tromey.com>
+
+       * valprint.c (generic_value_print_complex): Use accessors.
+       * 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.
+
 2020-04-01  Tom Tromey  <tom@tromey.com>
 
        * stabsread.c (rs6000_builtin_type, read_sun_floating_type)
index d48474665c3dfa2823b49142cb55c2f2cad6e1c9..83fd2584b597475bc8a24646cbc6656f3c7d8d8b 100644 (file)
@@ -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 *
index 108a21b684964eda724e34d141cd1c5c8910447e..80b7514b7e3b8fdef98f1acc91a381a0bbb92a37 100644 (file)
@@ -811,16 +811,11 @@ generic_value_print_complex (struct value *val, struct ui_file *stream,
 {
   fprintf_filtered (stream, "%s", decorations->complex_prefix);
 
-  struct type *type = check_typedef (value_type (val));
-  struct value *real_part
-    = value_from_component (val, TYPE_TARGET_TYPE (type), 0);
+  struct value *real_part = value_real_part (val);
   value_print_scalar_formatted (real_part, options, 0, stream);
   fprintf_filtered (stream, "%s", decorations->complex_infix);
 
-  struct value *imag_part
-    = value_from_component (val, TYPE_TARGET_TYPE (type),
-                           TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
-
+  struct value *imag_part = value_imaginary_part (val);
   value_print_scalar_formatted (imag_part, options, 0, stream);
   fprintf_filtered (stream, "%s", decorations->complex_suffix);
 }
index ceaeb835fa7ffbe581d52da7c1e72820ec836c51..f722c272d8b0a8dc6ea27d6ead7e5bf12dc999a2 100644 (file)
@@ -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
index e4fd258aa8f43025904f9fbcb0ad39af4f2f7cf9..85fe6c297f50d3f39b43cd92be99d900d3604c71 100644 (file)
@@ -1141,6 +1141,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 **);
 
This page took 0.032287 seconds and 4 git commands to generate.