Rename gdb_mpq::val and make contents private

This changes gdb_mpq to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
This commit is contained in:
Tom Tromey
2023-02-23 10:37:31 -07:00
parent 7607de9431
commit 8176838655
2 changed files with 47 additions and 43 deletions

View File

@ -162,8 +162,8 @@ gdb_mpq::get_rounded () const
/* Work with a positive number so as to make the "floor" rounding /* Work with a positive number so as to make the "floor" rounding
always round towards zero. */ always round towards zero. */
gdb_mpq abs_val (val); gdb_mpq abs_val (m_val);
mpq_abs (abs_val.val, abs_val.val); mpq_abs (abs_val.m_val, abs_val.m_val);
/* Convert our rational number into a quotient and remainder, /* Convert our rational number into a quotient and remainder,
with "floor" rounding, which in our case means rounding with "floor" rounding, which in our case means rounding
@ -171,17 +171,17 @@ gdb_mpq::get_rounded () const
gdb_mpz quotient, remainder; gdb_mpz quotient, remainder;
mpz_fdiv_qr (quotient.m_val, remainder.m_val, mpz_fdiv_qr (quotient.m_val, remainder.m_val,
mpq_numref (abs_val.val), mpq_denref (abs_val.val)); mpq_numref (abs_val.m_val), mpq_denref (abs_val.m_val));
/* Multiply the remainder by 2, and see if it is greater or equal /* 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. */ to abs_val's denominator. If yes, round to the next integer. */
mpz_mul_ui (remainder.m_val, remainder.m_val, 2); mpz_mul_ui (remainder.m_val, remainder.m_val, 2);
if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0) if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.m_val)) >= 0)
mpz_add_ui (quotient.m_val, quotient.m_val, 1); mpz_add_ui (quotient.m_val, quotient.m_val, 1);
/* Re-apply the sign if needed. */ /* Re-apply the sign if needed. */
if (mpq_sgn (val) < 0) if (mpq_sgn (m_val) < 0)
mpz_neg (quotient.m_val, quotient.m_val); mpz_neg (quotient.m_val, quotient.m_val);
return quotient; return quotient;
@ -197,8 +197,8 @@ gdb_mpq::read_fixed_point (gdb::array_view<const gdb_byte> buf,
gdb_mpz vz; gdb_mpz vz;
vz.read (buf, byte_order, unsigned_p); vz.read (buf, byte_order, unsigned_p);
mpq_set_z (val, vz.m_val); mpq_set_z (m_val, vz.m_val);
mpq_mul (val, val, scaling_factor.val); mpq_mul (m_val, m_val, scaling_factor.m_val);
} }
/* See gmp-utils.h. */ /* See gmp-utils.h. */
@ -208,9 +208,9 @@ gdb_mpq::write_fixed_point (gdb::array_view<gdb_byte> buf,
enum bfd_endian byte_order, bool unsigned_p, enum bfd_endian byte_order, bool unsigned_p,
const gdb_mpq &scaling_factor) const const gdb_mpq &scaling_factor) const
{ {
gdb_mpq unscaled (val); gdb_mpq unscaled (m_val);
mpq_div (unscaled.val, unscaled.val, scaling_factor.val); mpq_div (unscaled.m_val, unscaled.m_val, scaling_factor.m_val);
gdb_mpz unscaled_z = unscaled.get_rounded (); gdb_mpz unscaled_z = unscaled.get_rounded ();
unscaled_z.write (buf, byte_order, unsigned_p); unscaled_z.write (buf, byte_order, unsigned_p);

View File

@ -214,126 +214,124 @@ private:
struct gdb_mpq struct gdb_mpq
{ {
mpq_t val;
/* Constructors. */ /* Constructors. */
gdb_mpq () { mpq_init (val); } gdb_mpq () { mpq_init (m_val); }
explicit gdb_mpq (const mpq_t &from_val) explicit gdb_mpq (const mpq_t &from_val)
{ {
mpq_init (val); mpq_init (m_val);
mpq_set (val, from_val); mpq_set (m_val, from_val);
} }
gdb_mpq (const gdb_mpq &from) gdb_mpq (const gdb_mpq &from)
{ {
mpq_init (val); mpq_init (m_val);
mpq_set (val, from.val); mpq_set (m_val, from.m_val);
} }
explicit gdb_mpq (gdb_mpq &&from) explicit gdb_mpq (gdb_mpq &&from)
{ {
mpq_init (val); mpq_init (m_val);
mpq_swap (val, from.val); mpq_swap (m_val, from.m_val);
} }
gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom) gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
{ {
mpq_init (val); mpq_init (m_val);
mpz_set (mpq_numref (val), num.m_val); mpz_set (mpq_numref (m_val), num.m_val);
mpz_set (mpq_denref (val), denom.m_val); mpz_set (mpq_denref (m_val), denom.m_val);
mpq_canonicalize (val); mpq_canonicalize (m_val);
} }
gdb_mpq (long num, long denom) gdb_mpq (long num, long denom)
{ {
mpq_init (val); mpq_init (m_val);
mpq_set_si (val, num, denom); mpq_set_si (m_val, num, denom);
mpq_canonicalize (val); mpq_canonicalize (m_val);
} }
/* Copy assignment operator. */ /* Copy assignment operator. */
gdb_mpq &operator= (const gdb_mpq &from) gdb_mpq &operator= (const gdb_mpq &from)
{ {
mpq_set (val, from.val); mpq_set (m_val, from.m_val);
return *this; return *this;
} }
gdb_mpq &operator= (gdb_mpq &&from) gdb_mpq &operator= (gdb_mpq &&from)
{ {
mpq_swap (val, from.val); mpq_swap (m_val, from.m_val);
return *this; return *this;
} }
gdb_mpq &operator= (const gdb_mpz &from) gdb_mpq &operator= (const gdb_mpz &from)
{ {
mpq_set_z (val, from.m_val); mpq_set_z (m_val, from.m_val);
return *this; return *this;
} }
gdb_mpq &operator= (double d) gdb_mpq &operator= (double d)
{ {
mpq_set_d (val, d); mpq_set_d (m_val, d);
return *this; return *this;
} }
/* Return the sign of this value. This returns -1 for a negative /* Return the sign of this value. This returns -1 for a negative
value, 0 if the value is 0, and 1 for a positive value. */ value, 0 if the value is 0, and 1 for a positive value. */
int sgn () const int sgn () const
{ return mpq_sgn (val); } { return mpq_sgn (m_val); }
gdb_mpq operator+ (const gdb_mpq &other) const gdb_mpq operator+ (const gdb_mpq &other) const
{ {
gdb_mpq result; gdb_mpq result;
mpq_add (result.val, val, other.val); mpq_add (result.m_val, m_val, other.m_val);
return result; return result;
} }
gdb_mpq operator- (const gdb_mpq &other) const gdb_mpq operator- (const gdb_mpq &other) const
{ {
gdb_mpq result; gdb_mpq result;
mpq_sub (result.val, val, other.val); mpq_sub (result.m_val, m_val, other.m_val);
return result; return result;
} }
gdb_mpq operator* (const gdb_mpq &other) const gdb_mpq operator* (const gdb_mpq &other) const
{ {
gdb_mpq result; gdb_mpq result;
mpq_mul (result.val, val, other.val); mpq_mul (result.m_val, m_val, other.m_val);
return result; return result;
} }
gdb_mpq operator/ (const gdb_mpq &other) const gdb_mpq operator/ (const gdb_mpq &other) const
{ {
gdb_mpq result; gdb_mpq result;
mpq_div (result.val, val, other.val); mpq_div (result.m_val, m_val, other.m_val);
return result; return result;
} }
gdb_mpq &operator*= (const gdb_mpq &other) gdb_mpq &operator*= (const gdb_mpq &other)
{ {
mpq_mul (val, val, other.val); mpq_mul (m_val, m_val, other.m_val);
return *this; return *this;
} }
gdb_mpq &operator/= (const gdb_mpq &other) gdb_mpq &operator/= (const gdb_mpq &other)
{ {
mpq_div (val, val, other.val); mpq_div (m_val, m_val, other.m_val);
return *this; return *this;
} }
bool operator== (const gdb_mpq &other) const bool operator== (const gdb_mpq &other) const
{ {
return mpq_cmp (val, other.val) == 0; return mpq_cmp (m_val, other.m_val) == 0;
} }
bool operator< (const gdb_mpq &other) const bool operator< (const gdb_mpq &other) const
{ {
return mpq_cmp (val, other.val) < 0; return mpq_cmp (m_val, other.m_val) < 0;
} }
/* Return a string representing VAL as "<numerator> / <denominator>". */ /* Return a string representing VAL as "<numerator> / <denominator>". */
std::string str () const { return gmp_string_printf ("%Qd", val); } std::string str () const { return gmp_string_printf ("%Qd", m_val); }
/* Return VAL rounded to the nearest integer. */ /* Return VAL rounded to the nearest integer. */
gdb_mpz get_rounded () const; gdb_mpz get_rounded () const;
@ -342,13 +340,13 @@ struct gdb_mpq
gdb_mpz as_integer () const gdb_mpz as_integer () const
{ {
gdb_mpz result; gdb_mpz result;
mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val)); mpz_tdiv_q (result.m_val, mpq_numref (m_val), mpq_denref (m_val));
return result; return result;
} }
/* Return this value converted to a host double. */ /* Return this value converted to a host double. */
double as_double () const double as_double () const
{ return mpq_get_d (val); } { return mpq_get_d (m_val); }
/* Set VAL from the contents of the given byte array (BUF), which /* Set VAL from the contents of the given byte array (BUF), which
contains the unscaled value of a fixed point type object. contains the unscaled value of a fixed point type object.
@ -374,7 +372,13 @@ struct gdb_mpq
const gdb_mpq &scaling_factor) const; const gdb_mpq &scaling_factor) const;
/* The destructor. */ /* The destructor. */
~gdb_mpq () { mpq_clear (val); } ~gdb_mpq () { mpq_clear (m_val); }
private:
friend struct gdb_mpf;
mpq_t m_val;
}; };
/* A class to make it easier to use GMP's mpf_t values within GDB. /* A class to make it easier to use GMP's mpf_t values within GDB.
@ -405,7 +409,7 @@ struct gdb_mpf
gdb_mpq tmp_q; gdb_mpq tmp_q;
tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor); tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor);
mpf_set_q (val, tmp_q.val); mpf_set_q (val, tmp_q.m_val);
} }
/* The destructor. */ /* The destructor. */