[3/6] Rename gdb_mpz::val and make contents private

Message ID 20230223-submit-gmp-hiding-v1-3-ea6ff9e32716@adacore.com
State New
Headers
Series Make GMP data members private |

Commit Message

Tom Tromey Feb. 23, 2023, 7:48 p.m. UTC
  This changes gdb_mpz to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
---
 gdb/gmp-utils.c | 46 ++++++++++++++++++-------------------
 gdb/gmp-utils.h | 70 +++++++++++++++++++++++++++++++--------------------------
 2 files changed, 61 insertions(+), 55 deletions(-)
  

Patch

diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index 8150fa5dcdf..aaa075e236e 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -46,7 +46,7 @@  void
 gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
 	       bool unsigned_p)
 {
-  mpz_import (val, 1 /* count */, -1 /* order */, buf.size () /* size */,
+  mpz_import (m_val, 1 /* count */, -1 /* order */, buf.size () /* size */,
 	      byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
 	      0 /* nails */, buf.data () /* op */);
 
@@ -57,9 +57,9 @@  gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
 	 was in fact negative, we need to adjust VAL accordingly.  */
       gdb_mpz max;
 
-      mpz_ui_pow_ui (max.val, 2, buf.size () * HOST_CHAR_BIT - 1);
-      if (mpz_cmp (val, max.val) >= 0)
-	mpz_submul_ui (val, max.val, 2);
+      mpz_ui_pow_ui (max.m_val, 2, buf.size () * HOST_CHAR_BIT - 1);
+      if (mpz_cmp (m_val, max.m_val) >= 0)
+	mpz_submul_ui (m_val, max.m_val, 2);
     }
 }
 
@@ -81,7 +81,7 @@  gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
 {
   gdb_assert (buf.size () > 0);
 
-  if (mpz_sgn (val) == 0)
+  if (mpz_sgn (m_val) == 0)
     {
       /* Our value is zero, so no need to call mpz_export to do the work,
 	 especially since mpz_export's documentation explicitly says
@@ -100,19 +100,19 @@  gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
     {
       lo = 0;
 
-      mpz_ui_pow_ui (hi.val, 2, max_usable_bits);
-      mpz_sub_ui (hi.val, hi.val, 1);
+      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
+      mpz_sub_ui (hi.m_val, hi.m_val, 1);
     }
   else
     {
-      mpz_ui_pow_ui (lo.val, 2, max_usable_bits - 1);
-      mpz_neg (lo.val, lo.val);
+      mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
+      mpz_neg (lo.m_val, lo.m_val);
 
-      mpz_ui_pow_ui (hi.val, 2, max_usable_bits - 1);
-      mpz_sub_ui (hi.val, hi.val, 1);
+      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
+      mpz_sub_ui (hi.m_val, hi.m_val, 1);
     }
 
-  if (mpz_cmp (val, lo.val) < 0 || mpz_cmp (val, hi.val) > 0)
+  if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
     error (_("Cannot export value %s as %zu-bits %s integer"
 	     " (must be between %s and %s)"),
 	   this->str ().c_str (),
@@ -121,17 +121,17 @@  gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
 	   lo.str ().c_str (),
 	   hi.str ().c_str ());
 
-  gdb_mpz exported_val (val);
+  gdb_mpz exported_val (m_val);
 
-  if (mpz_cmp_ui (exported_val.val, 0) < 0)
+  if (mpz_cmp_ui (exported_val.m_val, 0) < 0)
     {
       /* mpz_export does not handle signed values, so create a positive
 	 value whose bit representation as an unsigned of the same length
 	 would be the same as our negative value.  */
       gdb_mpz neg_offset;
 
-      mpz_ui_pow_ui (neg_offset.val, 2, buf.size () * HOST_CHAR_BIT);
-      mpz_add (exported_val.val, exported_val.val, neg_offset.val);
+      mpz_ui_pow_ui (neg_offset.m_val, 2, buf.size () * HOST_CHAR_BIT);
+      mpz_add (exported_val.m_val, exported_val.m_val, neg_offset.m_val);
     }
 
   /* Do the export into a buffer allocated by GMP itself; that way,
@@ -147,7 +147,7 @@  gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
   size_t word_countp;
   gdb::unique_xmalloc_ptr<void> exported
     (mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */,
-		 endian, 0 /* nails */, exported_val.val));
+		 endian, 0 /* nails */, exported_val.m_val));
 
   gdb_assert (word_countp == 1);
 
@@ -170,19 +170,19 @@  gdb_mpq::get_rounded () const
      towards zero.  */
 
   gdb_mpz quotient, remainder;
-  mpz_fdiv_qr (quotient.val, remainder.val,
+  mpz_fdiv_qr (quotient.m_val, remainder.m_val,
 	       mpq_numref (abs_val.val), mpq_denref (abs_val.val));
 
   /* Multiply the remainder by 2, and see if it is greater or equal
      to abs_val's denominator.  If yes, round to the next integer.  */
 
-  mpz_mul_ui (remainder.val, remainder.val, 2);
-  if (mpz_cmp (remainder.val, mpq_denref (abs_val.val)) >= 0)
-    mpz_add_ui (quotient.val, quotient.val, 1);
+  mpz_mul_ui (remainder.m_val, remainder.m_val, 2);
+  if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0)
+    mpz_add_ui (quotient.m_val, quotient.m_val, 1);
 
   /* Re-apply the sign if needed.  */
   if (mpq_sgn (val) < 0)
-    mpz_neg (quotient.val, quotient.val);
+    mpz_neg (quotient.m_val, quotient.m_val);
 
   return quotient;
 }
@@ -197,7 +197,7 @@  gdb_mpq::read_fixed_point (gdb::array_view<const gdb_byte> buf,
   gdb_mpz vz;
   vz.read (buf, byte_order, unsigned_p);
 
-  mpq_set_z (val, vz.val);
+  mpq_set_z (val, vz.m_val);
   mpq_mul (val, val, scaling_factor.val);
 }
 
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index dcb998a1656..601d9a6a1ec 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -31,25 +31,26 @@ 
 
 std::string gmp_string_printf (const char *fmt, ...);
 
+struct gdb_mpq;
+struct gdb_mpf;
+
 /* A class to make it easier to use GMP's mpz_t values within GDB.  */
 
 struct gdb_mpz
 {
-  mpz_t val;
-
   /* Constructors.  */
-  gdb_mpz () { mpz_init (val); }
+  gdb_mpz () { mpz_init (m_val); }
 
   explicit gdb_mpz (const mpz_t &from_val)
   {
-    mpz_init (val);
-    mpz_set (val, from_val);
+    mpz_init (m_val);
+    mpz_set (m_val, from_val);
   }
 
   gdb_mpz (const gdb_mpz &from)
   {
-    mpz_init (val);
-    mpz_set (val, from.val);
+    mpz_init (m_val);
+    mpz_set (m_val, from.m_val);
   }
 
   /* Initialize using the given integral value.
@@ -59,26 +60,26 @@  struct gdb_mpz
   template<typename T, typename = gdb::Requires<std::is_integral<T>>>
   explicit gdb_mpz (T src)
   {
-    mpz_init (val);
+    mpz_init (m_val);
     set (src);
   }
 
   explicit gdb_mpz (gdb_mpz &&from)
   {
-    mpz_init (val);
-    mpz_swap (val, from.val);
+    mpz_init (m_val);
+    mpz_swap (m_val, from.m_val);
   }
 
   
   gdb_mpz &operator= (const gdb_mpz &from)
   {
-    mpz_set (val, from.val);
+    mpz_set (m_val, from.m_val);
     return *this;
   }
 
   gdb_mpz &operator= (gdb_mpz &&other)
   {
-    mpz_swap (val, other.val);
+    mpz_swap (m_val, other.m_val);
     return *this;
   }
 
@@ -93,14 +94,14 @@  struct gdb_mpz
      the string was parsed successfully, false otherwise.  */
   bool set (const char *str, int base)
   {
-    return mpz_set_str (val, str, base) != -1;
+    return mpz_set_str (m_val, str, base) != -1;
   }
 
   /* Return a new value that is BASE**EXP.  */
   static gdb_mpz pow (unsigned long base, unsigned long exp)
   {
     gdb_mpz result;
-    mpz_ui_pow_ui (result.val, base, exp);
+    mpz_ui_pow_ui (result.m_val, base, exp);
     return result;
   }
 
@@ -125,59 +126,59 @@  struct gdb_mpz
 	      bool unsigned_p) const;
 
   /* Return a string containing VAL.  */
-  std::string str () const { return gmp_string_printf ("%Zd", val); }
+  std::string str () const { return gmp_string_printf ("%Zd", m_val); }
 
   /* The destructor.  */
-  ~gdb_mpz () { mpz_clear (val); }
+  ~gdb_mpz () { mpz_clear (m_val); }
 
   /* Negate this value in place.  */
   void negate ()
   {
-    mpz_neg (val, val);
+    mpz_neg (m_val, m_val);
   }
 
   gdb_mpz &operator*= (long other)
   {
-    mpz_mul_si (val, val, other);
+    mpz_mul_si (m_val, m_val, other);
     return *this;
   }
 
   gdb_mpz &operator+= (unsigned long other)
   {
-    mpz_add_ui (val, val, other);
+    mpz_add_ui (m_val, m_val, other);
     return *this;
   }
 
   gdb_mpz &operator-= (unsigned long other)
   {
-    mpz_sub_ui (val, val, other);
+    mpz_sub_ui (m_val, m_val, other);
     return *this;
   }
 
   gdb_mpz &operator<<= (unsigned long nbits)
   {
-    mpz_mul_2exp (val, val, nbits);
+    mpz_mul_2exp (m_val, m_val, nbits);
     return *this;
   }
 
   bool operator> (const gdb_mpz &other) const
   {
-    return mpz_cmp (val, other.val) > 0;
+    return mpz_cmp (m_val, other.m_val) > 0;
   }
 
   bool operator< (int other) const
   {
-    return mpz_cmp_si (val, other) < 0;
+    return mpz_cmp_si (m_val, other) < 0;
   }
 
   bool operator== (int other) const
   {
-    return mpz_cmp_si (val, other) == 0;
+    return mpz_cmp_si (m_val, other) == 0;
   }
 
   bool operator== (const gdb_mpz &other) const
   {
-    return mpz_cmp (val, other.val) == 0;
+    return mpz_cmp (m_val, other.m_val) == 0;
   }
 
 private:
@@ -202,6 +203,11 @@  struct gdb_mpz
     being exported.  */
   void safe_export (gdb::array_view<gdb_byte> buf,
 		    int endian, bool unsigned_p) const;
+
+  friend struct gdb_mpq;
+  friend struct gdb_mpf;
+
+  mpz_t m_val;
 };
 
 /* A class to make it easier to use GMP's mpq_t values within GDB.  */
@@ -234,8 +240,8 @@  struct gdb_mpq
   gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
   {
     mpq_init (val);
-    mpz_set (mpq_numref (val), num.val);
-    mpz_set (mpq_denref (val), denom.val);
+    mpz_set (mpq_numref (val), num.m_val);
+    mpz_set (mpq_denref (val), denom.m_val);
     mpq_canonicalize (val);
   }
 
@@ -254,7 +260,7 @@  struct gdb_mpq
 
   gdb_mpq &operator= (const gdb_mpz &from)
   {
-    mpq_set_z (val, from.val);
+    mpq_set_z (val, from.m_val);
     return *this;
   }
 
@@ -268,7 +274,7 @@  struct gdb_mpq
   gdb_mpz as_integer () const
   {
     gdb_mpz result;
-    mpz_tdiv_q (result.val, mpq_numref (val), mpq_denref (val));
+    mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val));
     return result;
   }
 
@@ -340,7 +346,7 @@  template<typename T>
 void
 gdb_mpz::set (T src)
 {
-  mpz_import (val, 1 /* count */, -1 /* order */,
+  mpz_import (m_val, 1 /* count */, -1 /* order */,
 	      sizeof (T) /* size */, 0 /* endian (0 = native) */,
 	      0 /* nails */, &src /* op */);
   if (std::is_signed<T>::value && src < 0)
@@ -350,8 +356,8 @@  gdb_mpz::set (T src)
 	 the correct negative value.  */
       gdb_mpz neg_offset;
 
-      mpz_ui_pow_ui (neg_offset.val, 2, sizeof (T) * HOST_CHAR_BIT);
-      mpz_sub (val, val, neg_offset.val);
+      mpz_ui_pow_ui (neg_offset.m_val, 2, sizeof (T) * HOST_CHAR_BIT);
+      mpz_sub (m_val, m_val, neg_offset.m_val);
     }
 }