diff options
author | Tom Tromey <tromey@adacore.com> | 2023-04-17 12:59:57 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-04-26 08:24:15 -0600 |
commit | 3ad6c165ca098a0fa4491893a75b4b1581698484 (patch) | |
tree | 982830f1243692e0c643d0a48649e5115a7a68a9 | |
parent | 19e012c813f00e047355e8b45b2c8c85825d37e0 (diff) | |
download | gdb-3ad6c165ca098a0fa4491893a75b4b1581698484.zip gdb-3ad6c165ca098a0fa4491893a75b4b1581698484.tar.gz gdb-3ad6c165ca098a0fa4491893a75b4b1581698484.tar.bz2 |
Rewrite gdb_mpz::operator==
Simon pointed out that the recent changes to gdb_mpz caused a build
failure on amd64 macOS. It turns out to be somewhat difficult to
overload a method in a way that will work "naturally" for all integer
types; especially in a case like gdb_mpz::operator==, where it's
desirable to special case all integer types that are no wider than
'long'.
After a false start, I came up with this patch, which seems to work.
It applies the desirable GMP special cases directly in the body,
rather than via overloads.
Approved-By: Simon Marchi <simon.marchi@efficios.com>
-rw-r--r-- | gdb/gmp-utils.h | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index d05c11e..a5c27fe 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -323,31 +323,25 @@ struct gdb_mpz return mpz_cmp_si (m_val, other) < 0; } - bool operator== (int other) const - { - return mpz_cmp_si (m_val, other) == 0; - } - - bool operator== (long other) const - { - return mpz_cmp_si (m_val, other) == 0; - } - - bool operator== (unsigned long other) const - { - return mpz_cmp_ui (m_val, other) == 0; - } - - template<typename T, - typename = gdb::Requires< - gdb::And<std::is_integral<T>, - std::integral_constant<bool, - (sizeof (T) > sizeof (long))>> - > - > - bool operator== (T src) - { - return *this == gdb_mpz (src); + /* We want an operator== that can handle all integer types. For + types that are 'long' or narrower, we can use a GMP function and + avoid boxing the RHS. But, because overloading based on integer + type is a pain in C++, we accept all such types here and check + the size in the body. */ + template<typename T, typename = gdb::Requires<std::is_integral<T>>> + bool operator== (T other) const + { + if (std::is_signed<T>::value) + { + if (sizeof (T) <= sizeof (long)) + return mpz_cmp_si (m_val, other) == 0; + } + else + { + if (sizeof (T) <= sizeof (unsigned long)) + return mpz_cmp_ui (m_val, other) == 0; + } + return *this == gdb_mpz (other); } bool operator== (const gdb_mpz &other) const |