/* Python interface to types.
- Copyright (C) 2008-2019 Free Software Foundation, Inc.
+ Copyright (C) 2008-2020 Free Software Foundation, Inc.
This file is part of GDB.
#include "demangle.h"
#include "objfiles.h"
#include "language.h"
-#include "common/vec.h"
#include "typeprint.h"
typedef struct pyty_type_object
{
struct type *type = ((type_object *) self)->type;
- return PyInt_FromLong (TYPE_CODE (type));
+ return PyInt_FromLong (type->code ());
}
/* Helper function for typy_fields which converts a single field to a
if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
return NULL;
- if (!field_is_static (&TYPE_FIELD (type, field)))
+ if (!field_is_static (&type->field (field)))
{
const char *attrstring;
- if (TYPE_CODE (type) == TYPE_CODE_ENUM)
+ if (type->code () == TYPE_CODE_ENUM)
{
arg.reset (gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type,
field)));
}
else
{
- arg.reset (gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type,
- field)));
+ if (TYPE_FIELD_LOC_KIND (type, field) == FIELD_LOC_KIND_DWARF_BLOCK)
+ arg = gdbpy_ref<>::new_reference (Py_None);
+ else
+ arg.reset (gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type,
+ field)));
attrstring = "bitpos";
}
if (PyObject_SetAttrString (result.get (), "artificial", arg.get ()) < 0)
return NULL;
- if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
+ if (type->code () == TYPE_CODE_STRUCT)
arg = gdbpy_ref<>::new_reference (field < TYPE_N_BASECLASSES (type)
? Py_True : Py_False);
else
return NULL;
/* A field can have a NULL type in some situations. */
- if (TYPE_FIELD_TYPE (type, field) == NULL)
+ if (type->field (field).type () == NULL)
arg = gdbpy_ref<>::new_reference (Py_None);
else
- arg.reset (type_to_type_object (TYPE_FIELD_TYPE (type, field)));
+ arg.reset (type_to_type_object (type->field (field).type ()));
if (arg == NULL)
return NULL;
if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0)
{
checked_type = check_typedef (checked_type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
struct type *type = ((type_object *) self)->type;
- if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
+ if (type->code () != TYPE_CODE_ARRAY)
return typy_fields_items (self, iter_values);
/* Array type. Handle this as a special case because the common
{
struct type *type = ((type_object *) self)->type;
- if (TYPE_NAME (type) == NULL)
+ if (type->name () == NULL)
Py_RETURN_NONE;
- return PyString_FromString (TYPE_NAME (type));
+ return PyString_FromString (type->name ());
}
/* Return the type's tag, or None. */
struct type *type = ((type_object *) self)->type;
const char *tagname = nullptr;
- if (TYPE_CODE (type) == TYPE_CODE_STRUCT
- || TYPE_CODE (type) == TYPE_CODE_UNION
- || TYPE_CODE (type) == TYPE_CODE_ENUM)
- tagname = TYPE_NAME (type);
+ if (type->code () == TYPE_CODE_STRUCT
+ || type->code () == TYPE_CODE_UNION
+ || type->code () == TYPE_CODE_ENUM)
+ tagname = type->name ();
if (tagname == nullptr)
Py_RETURN_NONE;
return PyString_FromString (tagname);
}
+/* Return the type's objfile, or None. */
+static PyObject *
+typy_get_objfile (PyObject *self, void *closure)
+{
+ struct type *type = ((type_object *) self)->type;
+ struct objfile *objfile = TYPE_OBJFILE (type);
+
+ if (objfile == nullptr)
+ Py_RETURN_NONE;
+ return objfile_to_objfile_object (objfile).release ();
+}
+
/* Return the type, stripped of typedefs. */
static PyObject *
typy_strip_typedefs (PyObject *self, PyObject *args)
{
type = check_typedef (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
type = check_typedef (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
- if (TYPE_CODE (type) != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
+ if (type->code () != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
break;
type = TYPE_TARGET_TYPE (type);
}
/* If this is not a struct, union, or enum type, raise TypeError
exception. */
- if (TYPE_CODE (type) != TYPE_CODE_STRUCT
- && TYPE_CODE (type) != TYPE_CODE_UNION
- && TYPE_CODE (type) != TYPE_CODE_ENUM
- && TYPE_CODE (type) != TYPE_CODE_FUNC)
+ if (type->code () != TYPE_CODE_STRUCT
+ && type->code () != TYPE_CODE_UNION
+ && type->code () != TYPE_CODE_ENUM
+ && type->code () != TYPE_CODE_FUNC)
{
PyErr_SetString (PyExc_TypeError,
"Type is not a structure, union, enum, or function type.");
if (is_vector)
make_vector_type (array);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
type = lookup_pointer_type (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
/* Initialize these to appease GCC warnings. */
LONGEST low = 0, high = 0;
- if (TYPE_CODE (type) != TYPE_CODE_ARRAY
- && TYPE_CODE (type) != TYPE_CODE_STRING
- && TYPE_CODE (type) != TYPE_CODE_RANGE)
+ if (type->code () != TYPE_CODE_ARRAY
+ && type->code () != TYPE_CODE_STRING
+ && type->code () != TYPE_CODE_RANGE)
{
PyErr_SetString (PyExc_RuntimeError,
_("This type does not have a range."));
return NULL;
}
- switch (TYPE_CODE (type))
+ switch (type->code ())
{
case TYPE_CODE_ARRAY:
case TYPE_CODE_STRING:
- low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
- high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
+ low = TYPE_LOW_BOUND (type->index_type ());
+ high = TYPE_HIGH_BOUND (type->index_type ());
break;
case TYPE_CODE_RANGE:
low = TYPE_LOW_BOUND (type);
{
type = lookup_lvalue_reference_type (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
type = make_cv_type (1, 0, type, NULL);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
type = make_cv_type (0, 1, type, NULL);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
type = make_cv_type (0, 0, type, NULL);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
struct type *type = ((type_object *) self)->type;
+ bool size_varies = false;
try
{
check_typedef (type);
+
+ size_varies = TYPE_HAS_DYNAMIC_LENGTH (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
}
/* Ignore exceptions. */
+ if (size_varies)
+ Py_RETURN_NONE;
return gdb_py_long_from_longest (TYPE_LENGTH (type));
}
{
align = type_align (type);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
align = 0;
}
return gdb_py_object_from_ulongest (align).release ();
}
+/* Return whether or not the type is dynamic. */
+static PyObject *
+typy_get_dynamic (PyObject *self, void *closure)
+{
+ struct type *type = ((type_object *) self)->type;
+
+ bool result = false;
+ try
+ {
+ result = is_dynamic_type (type);
+ }
+ catch (const gdb_exception &except)
+ {
+ /* Ignore exceptions. */
+ }
+
+ if (result)
+ Py_RETURN_TRUE;
+ Py_RETURN_FALSE;
+}
+
static struct type *
typy_lookup_typename (const char *type_name, const struct block *block)
{
else if (startswith (type_name, "enum "))
type = lookup_enum (type_name + 5, NULL);
else
- type = lookup_typename (python_language, python_gdbarch,
+ type = lookup_typename (python_language,
type_name, block, 0);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
break;
}
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
std::string err;
struct type *argtype;
- if (TYPE_NAME (type) == NULL)
+ if (type->name () == NULL)
{
PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
return NULL;
try
{
/* Note -- this is not thread-safe. */
- info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
+ info = cp_demangled_name_to_comp (type->name (), &err);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
if (TYPE_IS_REFERENCE (type))
type = check_typedef (TYPE_TARGET_TYPE (type));
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
val = value_of_variable (sym, block);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
LA_PRINT_TYPE (type_object_to_type (self), "", &thetype, -1, 0,
&type_print_raw_options);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
{
result = types_deeply_equal (type1, type2);
}
- catch (const gdb_exception_RETURN_MASK_ALL &except)
+ catch (const gdb_exception &except)
{
/* If there is a GDB exception, a comparison is not capable
(or trusted), so exit. */
/* This prevents another thread from freeing the objects we're
operating on. */
- gdbpy_enter enter_py (get_objfile_arch (objfile), current_language);
+ gdbpy_enter enter_py (objfile->arch (), current_language);
copied_types = create_copied_types_hash (objfile);
if (type == NULL)
return -1;
- return TYPE_NFIELDS (type);
+ return type->num_fields ();
}
/* Implements boolean evaluation of gdb.Type. Handle this like other
if (type == NULL)
return NULL;
- for (i = 0; i < TYPE_NFIELDS (type); i++)
+ for (i = 0; i < type->num_fields (); i++)
{
const char *t_field_name = TYPE_FIELD_NAME (type, i);
if (type == NULL)
return NULL;
- for (i = 0; i < TYPE_NFIELDS (type); i++)
+ for (i = 0; i < type->num_fields (); i++)
{
const char *t_field_name = TYPE_FIELD_NAME (type, i);
typy_iterator_object *iter_obj = (typy_iterator_object *) self;
struct type *type = iter_obj->source->type;
- if (iter_obj->field < TYPE_NFIELDS (type))
+ if (iter_obj->field < type->num_fields ())
{
gdbpy_ref<> result = make_fielditem (type, iter_obj->field,
iter_obj->kind);
typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
Py_DECREF (iter_obj->source);
+ Py_TYPE (obj)->tp_free (obj);
}
/* Create a new Type referring to TYPE. */
{
type_object *type_obj;
+ try
+ {
+ /* Try not to let stub types leak out to Python. */
+ if (TYPE_STUB (type))
+ type = check_typedef (type);
+ }
+ catch (...)
+ {
+ /* Just ignore failures in check_typedef. */
+ }
+
type_obj = PyObject_New (type_object, &type_object_type);
if (type_obj)
set_type (type_obj, type);
"The alignment of this type, in bytes.", NULL },
{ "code", typy_get_code, NULL,
"The code for this type.", NULL },
+ { "dynamic", typy_get_dynamic, NULL,
+ "Whether this type is dynamic.", NULL },
{ "name", typy_get_name, NULL,
"The name for this type, or None.", NULL },
{ "sizeof", typy_get_sizeof, NULL,
"The size of this type, in bytes.", NULL },
{ "tag", typy_get_tag, NULL,
"The tag name for this type, or None.", NULL },
+ { "objfile", typy_get_objfile, NULL,
+ "The objfile this type was defined in, or None.", NULL },
{ NULL }
};