{
ULONGEST n = 0;
ULONGEST prevn = 0;
- ULONGEST un;
int i = 0;
int c;
/* We have found a "L" or "U" (or "i") suffix. */
int found_suffix = 0;
- ULONGEST high_bit;
- struct type *signed_type;
- struct type *unsigned_type;
char *p;
p = (char *) alloca (len);
if (i >= base)
return ERROR; /* Invalid digit in this base */
- /* Portably test for overflow (only works for nonzero values, so make
- a second check for zero). FIXME: Can't we just make n and prevn
- unsigned and avoid this? */
- if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
- unsigned_p = 1; /* Try something unsigned */
-
- /* Portably test for unsigned overflow.
- FIXME: This check is wrong; for example it doesn't find overflow
- on 0x123456789 when LONGEST is 32 bits. */
- if (c != 'l' && c != 'u' && c != 'i' && n != 0)
- {
- if (unsigned_p && prevn >= n)
+ if (c != 'l' && c != 'u' && c != 'i')
+ {
+ /* Test for overflow. */
+ if (prevn == 0 && n == 0)
+ ;
+ else if (prevn >= n)
error (_("Numeric constant too large."));
}
prevn = n;
or gdbarch_long_bit will be that big, sometimes not. To deal with
the case where it is we just always shift the value more than
once, with fewer bits each time. */
-
- un = n >> 2;
- if (long_p == 0
- && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
- {
- high_bit
- = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
-
- /* A large decimal (not hex or octal) constant (between INT_MAX
- and UINT_MAX) is a long or unsigned long, according to ANSI,
- never an unsigned int, but this code treats it as unsigned
- int. This probably should be fixed. GCC gives a warning on
- such constants. */
-
- unsigned_type = parse_type (par_state)->builtin_unsigned_int;
- signed_type = parse_type (par_state)->builtin_int;
- }
- else if (long_p <= 1
- && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
- {
- high_bit
- = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
- unsigned_type = parse_type (par_state)->builtin_unsigned_long;
- signed_type = parse_type (par_state)->builtin_long;
- }
+ int int_bits = gdbarch_int_bit (par_state->gdbarch ());
+ int long_bits = gdbarch_long_bit (par_state->gdbarch ());
+ int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
+ bool have_signed
+ /* No 'u' suffix. */
+ = !unsigned_p;
+ bool have_unsigned
+ = ((/* 'u' suffix. */
+ unsigned_p)
+ || (/* Not a decimal. */
+ base != 10)
+ || (/* Allowed as a convenience, in case decimal doesn't fit in largest
+ signed type. */
+ !fits_in_type (1, n, long_long_bits, true)));
+ bool have_int
+ /* No 'l' or 'll' suffix. */
+ = long_p == 0;
+ bool have_long
+ /* No 'll' suffix. */
+ = long_p <= 1;
+ if (have_int && have_signed && fits_in_type (1, n, int_bits, true))
+ putithere->typed_val_int.type = parse_type (par_state)->builtin_int;
+ else if (have_int && have_unsigned && fits_in_type (1, n, int_bits, false))
+ putithere->typed_val_int.type
+ = parse_type (par_state)->builtin_unsigned_int;
+ else if (have_long && have_signed && fits_in_type (1, n, long_bits, true))
+ putithere->typed_val_int.type = parse_type (par_state)->builtin_long;
+ else if (have_long && have_unsigned && fits_in_type (1, n, long_bits, false))
+ putithere->typed_val_int.type
+ = parse_type (par_state)->builtin_unsigned_long;
+ else if (have_signed && fits_in_type (1, n, long_long_bits, true))
+ putithere->typed_val_int.type
+ = parse_type (par_state)->builtin_long_long;
+ else if (have_unsigned && fits_in_type (1, n, long_long_bits, false))
+ putithere->typed_val_int.type
+ = parse_type (par_state)->builtin_unsigned_long_long;
else
- {
- int shift;
- if (sizeof (ULONGEST) * HOST_CHAR_BIT
- < gdbarch_long_long_bit (par_state->gdbarch ()))
- /* A long long does not fit in a LONGEST. */
- shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
- else
- shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
- high_bit = (ULONGEST) 1 << shift;
- unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
- signed_type = parse_type (par_state)->builtin_long_long;
- }
-
- putithere->typed_val_int.val = n;
-
- /* If the high bit of the worked out type is set then this number
- has to be unsigned. */
-
- if (unsigned_p || (n & high_bit))
- {
- putithere->typed_val_int.type = unsigned_type;
- }
- else
- {
- putithere->typed_val_int.type = signed_type;
- }
+ error (_("Numeric constant too large."));
+ putithere->typed_val_int.val = n;
if (imaginary_p)
putithere->typed_val_int.type
{
return target_float_from_string (data, type, std::string (p, len));
}
+
+/* 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, ULONGEST n, int type_bits, bool type_signed_p)
+{
+ /* Normalize -0. */
+ if (n == 0 && n_sign == -1)
+ n_sign = 1;
+
+ if (n_sign == -1 && !type_signed_p)
+ /* Can't fit a negative number in an unsigned type. */
+ return false;
+
+ if (type_bits > sizeof (ULONGEST) * 8)
+ return true;
+
+ ULONGEST smax = (ULONGEST)1 << (type_bits - 1);
+ if (n_sign == -1)
+ {
+ /* Negative number, signed type. */
+ return (n <= smax);
+ }
+ else if (n_sign == 1 && type_signed_p)
+ {
+ /* Positive number, signed type. */
+ return (n < smax);
+ }
+ else if (n_sign == 1 && !type_signed_p)
+ {
+ /* Positive number, unsigned type. */
+ return ((n >> 1) >> (type_bits - 1)) == 0;
+ }
+ else
+ gdb_assert_not_reached ("");
+}
\f
/* This function avoids direct calls to fprintf
in the parser generated debug code. */
}
}
+# Return 1 if LANG is a c-like language, in the sense that it uses the same
+# parser.
+
+proc c_like { lang } {
+ set res 0
+ switch $lang {
+ c
+ - c++
+ - asm
+ - objective-c
+ - opencl
+ - minimal {set res 1}
+ }
+ return $res
+}
+
# Parse number N for LANG, and return a list of expected type and value.
proc parse_number { lang n } {
($any|$re_overflow)]
}
} else {
- # This is wrong for c-like languages. For the decimal case, we
- # shouldn't use unsigned.
- # See PR 16377.
+ if { [c_like $lang] } {
+ if { $hex_p } {
+ # C Hex.
+ set have_unsigned 1
+ } else {
+ # C Decimal. Unsigned not allowed according.
+ if { [fits_in_type $n $long_long_bits s] } {
+ # Fits in largest signed type.
+ set have_unsigned 0
+ } else {
+ # Doesn't fit in largest signed type, so ill-formed, but
+ # allow unsigned as a convenience, as compilers do (though
+ # with a warning).
+ set have_unsigned 1
+ }
+ }
+ } else {
+ # Non-C.
+ set have_unsigned 1
+ }
+
if { [fits_in_type $n $int_bits s] } {
return [list int $n]
- } elseif { [fits_in_type $n $int_bits u] } {
+ } elseif { $have_unsigned && [fits_in_type $n $int_bits u] } {
return [list "unsigned int" $n]
} elseif { [fits_in_type $n $long_bits s] } {
return [list long $n]
- } elseif { [fits_in_type $n $long_bits u] } {
+ } elseif { $have_unsigned && [fits_in_type $n $long_bits u] } {
return [list "unsigned long" $n]
} elseif { [fits_in_type $n $long_long_bits s] } {
return [list "long long" $n]
- } elseif { [fits_in_type $n $long_long_bits u] } {
+ } elseif { $have_unsigned && [fits_in_type $n $long_long_bits u] } {
return [list "unsigned long long" $n]
} else {
# Overflow.
- # Some truncated value or re_overflow, should be re_overflow.
- return [list "((unsigned )?(int|long)|$re_overflow)" \
- ($any|$re_overflow)]
+ if { [c_like $lang] } {
+ return [list $re_overflow $re_overflow]
+ } else {
+ # Some truncated value or re_overflow, should be re_overflow.
+ return [list "((unsigned )?(int|long)|$re_overflow)" \
+ ($any|$re_overflow)]
+ }
}
}