gdb/python: add Type.is_signed property

Add a new read-only property, Type.is_signed, which is True for signed
types, and False otherwise.

This property should only be read on types for which Type.is_scalar is
true, attempting to read this property for non-scalar types will raise
a ValueError.

I chose 'is_signed' rather than 'is_unsigned' in order to match the
existing Architecture.integer_type method, which takes a 'signed'
parameter.  As far as I could find, that was the only existing
signed/unsigned selector in the Python API, so it seemed reasonable to
stay consistent.
This commit is contained in:
Andrew Burgess 2021-11-30 14:35:44 +00:00
parent ee6a3d9e94
commit 551b380fbd
5 changed files with 80 additions and 1 deletions

View file

@ -446,6 +446,27 @@ typy_is_scalar (PyObject *self, void *closure)
Py_RETURN_FALSE;
}
/* Return true if this type is signed. Raises a ValueError if this type
is not a scalar type. */
static PyObject *
typy_is_signed (PyObject *self, void *closure)
{
struct type *type = ((type_object *) self)->type;
if (!is_scalar_type (type))
{
PyErr_SetString (PyExc_ValueError,
_("Type must be a scalar type"));
return nullptr;
}
if (type->is_unsigned ())
Py_RETURN_FALSE;
else
Py_RETURN_TRUE;
}
/* Return the type, stripped of typedefs. */
static PyObject *
typy_strip_typedefs (PyObject *self, PyObject *args)
@ -1502,6 +1523,8 @@ static gdb_PyGetSetDef type_object_getset[] =
"The objfile this type was defined in, or None.", NULL },
{ "is_scalar", typy_is_scalar, nullptr,
"Is this a scalar type?", nullptr },
{ "is_signed", typy_is_signed, nullptr,
"Is this an signed type?", nullptr },
{ NULL }
};