aboutsummaryrefslogtreecommitdiff
path: root/gdb/parse.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2023-03-27 13:42:38 -0600
committerTom Tromey <tromey@adacore.com>2023-04-17 10:43:06 -0600
commit64f33c6938af83b3365c118a049fdc444de95b12 (patch)
treef6354db92e9a7c1991357f810655c4ae55821528 /gdb/parse.c
parentd760ae22b964995234e14e090ba179311382b90d (diff)
downloadgdb-64f33c6938af83b3365c118a049fdc444de95b12.zip
gdb-64f33c6938af83b3365c118a049fdc444de95b12.tar.gz
gdb-64f33c6938af83b3365c118a049fdc444de95b12.tar.bz2
Add overload of fits_in_type
This adds an overload of fits_in_type that accepts a gdb_mpz. A subsequent patch will use this.
Diffstat (limited to 'gdb/parse.c')
-rw-r--r--gdb/parse.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/gdb/parse.c b/gdb/parse.c
index c0c9fa0..0588c06 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -648,6 +648,32 @@ fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
else
gdb_assert_not_reached ("");
}
+
+/* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and
+ TYPE_SIGNED_P. N_SIGNED is either 1 or -1. */
+
+bool
+fits_in_type (int n_sign, const gdb_mpz &n, int type_bits, bool type_signed_p)
+{
+ /* N must be nonnegative. */
+ gdb_assert (n.sgn () >= 0);
+
+ /* Zero always fits. */
+ /* Normalize -0. */
+ if (n.sgn () == 0)
+ return true;
+
+ if (n_sign == -1 && !type_signed_p)
+ /* Can't fit a negative number in an unsigned type. */
+ return false;
+
+ gdb_mpz max = gdb_mpz::pow (2, (type_signed_p
+ ? type_bits - 1
+ : type_bits));
+ if (n_sign == -1)
+ return n <= max;
+ return n < max;
+}
/* This function avoids direct calls to fprintf
in the parser generated debug code. */