include: Sync plugin-api.h with GCC
[binutils-gdb.git] / gdb / python / py-type.c
index d918fc0d68ecc8b9d8716ef69866724229b6e39b..b19cad098a46410262080cd2b1aaa8f8f1729ea6 100644 (file)
@@ -1,6 +1,6 @@
 /* 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.
 
@@ -26,7 +26,6 @@
 #include "demangle.h"
 #include "objfiles.h"
 #include "language.h"
-#include "common/vec.h"
 #include "typeprint.h"
 
 typedef struct pyty_type_object
@@ -316,7 +315,7 @@ typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
     {
       checked_type = check_typedef (checked_type);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       GDB_PY_HANDLE_EXCEPTION (except);
     }
@@ -413,6 +412,18 @@ typy_get_tag (PyObject *self, void *closure)
   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)
@@ -423,7 +434,7 @@ 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);
     }
@@ -444,7 +455,7 @@ typy_get_composite (struct type *type)
        {
          type = check_typedef (type);
        }
-      catch (const gdb_exception_RETURN_MASK_ALL &except)
+      catch (const gdb_exception &except)
        {
          GDB_PY_HANDLE_EXCEPTION (except);
        }
@@ -513,7 +524,7 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
       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);
     }
@@ -547,7 +558,7 @@ typy_pointer (PyObject *self, PyObject *args)
     {
       type = lookup_pointer_type (type);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       GDB_PY_HANDLE_EXCEPTION (except);
     }
@@ -615,7 +626,7 @@ typy_reference (PyObject *self, PyObject *args)
     {
       type = lookup_lvalue_reference_type (type);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       GDB_PY_HANDLE_EXCEPTION (except);
     }
@@ -649,7 +660,7 @@ typy_const (PyObject *self, PyObject *args)
     {
       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);
     }
@@ -667,7 +678,7 @@ typy_volatile (PyObject *self, PyObject *args)
     {
       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);
     }
@@ -685,7 +696,7 @@ typy_unqualified (PyObject *self, PyObject *args)
     {
       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);
     }
@@ -703,7 +714,7 @@ typy_get_sizeof (PyObject *self, void *closure)
     {
       check_typedef (type);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
     }
 
@@ -723,7 +734,7 @@ typy_get_alignof (PyObject *self, void *closure)
     {
       align = type_align (type);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       align = 0;
     }
@@ -747,10 +758,10 @@ 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);
     }
@@ -804,7 +815,7 @@ typy_lookup_type (struct demangle_component *demangled,
              break;
            }
        }
-      catch (const gdb_exception_RETURN_MASK_ALL &except)
+      catch (const gdb_exception &except)
        {
          GDB_PY_HANDLE_EXCEPTION (except);
        }
@@ -846,7 +857,7 @@ typy_legacy_template_argument (struct type *type, const struct block *block,
       /* Note -- this is not thread-safe.  */
       info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       GDB_PY_HANDLE_EXCEPTION (except);
     }
@@ -926,7 +937,7 @@ typy_template_argument (PyObject *self, PyObject *args)
       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);
     }
@@ -958,7 +969,7 @@ typy_template_argument (PyObject *self, PyObject *args)
     {
       val = value_of_variable (sym, block);
     }
-  catch (const gdb_exception_RETURN_MASK_ALL &except)
+  catch (const gdb_exception &except)
     {
       GDB_PY_HANDLE_EXCEPTION (except);
     }
@@ -976,7 +987,7 @@ typy_str (PyObject *self)
       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);
     }
@@ -1010,7 +1021,7 @@ typy_richcompare (PyObject *self, PyObject *other, int op)
        {
          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.  */
@@ -1314,6 +1325,7 @@ typy_iterator_dealloc (PyObject *obj)
   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.  */
@@ -1322,6 +1334,17 @@ type_to_type_object (struct type *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);
@@ -1419,6 +1442,8 @@ static gdb_PyGetSetDef type_object_getset[] =
     "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 }
 };