gdb/python/py-type.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Python interface to types.

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "value.h"
  16. #include "python-internal.h"
  17. #include "charset.h"
  18. #include "gdbtypes.h"
  19. #include "cp-support.h"
  20. #include "demangle.h"
  21. #include "objfiles.h"
  22. #include "language.h"
  23. #include "vec.h"
  24. #include "typeprint.h"

  25. typedef struct pyty_type_object
  26. {
  27.   PyObject_HEAD
  28.   struct type *type;

  29.   /* If a Type object is associated with an objfile, it is kept on a
  30.      doubly-linked list, rooted in the objfile.  This lets us copy the
  31.      underlying struct type when the objfile is deleted.  */
  32.   struct pyty_type_object *prev;
  33.   struct pyty_type_object *next;
  34. } type_object;

  35. static PyTypeObject type_object_type
  36.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");

  37. /* A Field object.  */
  38. typedef struct pyty_field_object
  39. {
  40.   PyObject_HEAD

  41.   /* Dictionary holding our attributes.  */
  42.   PyObject *dict;
  43. } field_object;

  44. static PyTypeObject field_object_type
  45.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");

  46. /* A type iterator object.  */
  47. typedef struct {
  48.   PyObject_HEAD
  49.   /* The current field index.  */
  50.   int field;
  51.   /* What to return.  */
  52.   enum gdbpy_iter_kind kind;
  53.   /* Pointer back to the original source type object.  */
  54.   struct pyty_type_object *source;
  55. } typy_iterator_object;

  56. static PyTypeObject type_iterator_object_type
  57.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");

  58. /* This is used to initialize various gdb.TYPE_ constants.  */
  59. struct pyty_code
  60. {
  61.   /* The code.  */
  62.   enum type_code code;
  63.   /* The name.  */
  64.   const char *name;
  65. };

  66. /* Forward declarations.  */
  67. static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind);

  68. #define ENTRY(X) { X, #X }

  69. static struct pyty_code pyty_codes[] =
  70. {
  71.   ENTRY (TYPE_CODE_BITSTRING),
  72.   ENTRY (TYPE_CODE_PTR),
  73.   ENTRY (TYPE_CODE_ARRAY),
  74.   ENTRY (TYPE_CODE_STRUCT),
  75.   ENTRY (TYPE_CODE_UNION),
  76.   ENTRY (TYPE_CODE_ENUM),
  77.   ENTRY (TYPE_CODE_FLAGS),
  78.   ENTRY (TYPE_CODE_FUNC),
  79.   ENTRY (TYPE_CODE_INT),
  80.   ENTRY (TYPE_CODE_FLT),
  81.   ENTRY (TYPE_CODE_VOID),
  82.   ENTRY (TYPE_CODE_SET),
  83.   ENTRY (TYPE_CODE_RANGE),
  84.   ENTRY (TYPE_CODE_STRING),
  85.   ENTRY (TYPE_CODE_ERROR),
  86.   ENTRY (TYPE_CODE_METHOD),
  87.   ENTRY (TYPE_CODE_METHODPTR),
  88.   ENTRY (TYPE_CODE_MEMBERPTR),
  89.   ENTRY (TYPE_CODE_REF),
  90.   ENTRY (TYPE_CODE_CHAR),
  91.   ENTRY (TYPE_CODE_BOOL),
  92.   ENTRY (TYPE_CODE_COMPLEX),
  93.   ENTRY (TYPE_CODE_TYPEDEF),
  94.   ENTRY (TYPE_CODE_NAMESPACE),
  95.   ENTRY (TYPE_CODE_DECFLOAT),
  96.   ENTRY (TYPE_CODE_INTERNAL_FUNCTION),
  97.   { TYPE_CODE_UNDEF, NULL }
  98. };



  99. static void
  100. field_dealloc (PyObject *obj)
  101. {
  102.   field_object *f = (field_object *) obj;

  103.   Py_XDECREF (f->dict);
  104.   Py_TYPE (obj)->tp_free (obj);
  105. }

  106. static PyObject *
  107. field_new (void)
  108. {
  109.   field_object *result = PyObject_New (field_object, &field_object_type);

  110.   if (result)
  111.     {
  112.       result->dict = PyDict_New ();
  113.       if (!result->dict)
  114.         {
  115.           Py_DECREF (result);
  116.           result = NULL;
  117.         }
  118.     }
  119.   return (PyObject *) result;
  120. }



  121. /* Return true if OBJ is of type gdb.Field, false otherwise.  */

  122. int
  123. gdbpy_is_field (PyObject *obj)
  124. {
  125.   return PyObject_TypeCheck (obj, &field_object_type);
  126. }

  127. /* Return the code for this type.  */
  128. static PyObject *
  129. typy_get_code (PyObject *self, void *closure)
  130. {
  131.   struct type *type = ((type_object *) self)->type;

  132.   return PyInt_FromLong (TYPE_CODE (type));
  133. }

  134. /* Helper function for typy_fields which converts a single field to a
  135.    gdb.Field object.  Returns NULL on error.  */

  136. static PyObject *
  137. convert_field (struct type *type, int field)
  138. {
  139.   PyObject *result = field_new ();
  140.   PyObject *arg;

  141.   if (!result)
  142.     return NULL;

  143.   arg = type_to_type_object (type);
  144.   if (arg == NULL)
  145.     goto fail;
  146.   if (PyObject_SetAttrString (result, "parent_type", arg) < 0)
  147.     goto failarg;
  148.   Py_DECREF (arg);

  149.   if (!field_is_static (&TYPE_FIELD (type, field)))
  150.     {
  151.       const char *attrstring;

  152.       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
  153.         {
  154.           arg = gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type, field));
  155.           attrstring = "enumval";
  156.         }
  157.       else
  158.         {
  159.           arg = gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type, field));
  160.           attrstring = "bitpos";
  161.         }

  162.       if (!arg)
  163.         goto fail;

  164.       /* At least python-2.4 had the second parameter non-const.  */
  165.       if (PyObject_SetAttrString (result, (char *) attrstring, arg) < 0)
  166.         goto failarg;
  167.       Py_DECREF (arg);
  168.     }

  169.   arg = NULL;
  170.   if (TYPE_FIELD_NAME (type, field))
  171.     {
  172.       const char *field_name = TYPE_FIELD_NAME (type, field);

  173.       if (field_name[0] != '\0')
  174.         {
  175.           arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
  176.           if (arg == NULL)
  177.             goto fail;
  178.         }
  179.     }
  180.   if (arg == NULL)
  181.     {
  182.       arg = Py_None;
  183.       Py_INCREF (arg);
  184.     }
  185.   if (PyObject_SetAttrString (result, "name", arg) < 0)
  186.     goto failarg;
  187.   Py_DECREF (arg);

  188.   arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
  189.   Py_INCREF (arg);
  190.   if (PyObject_SetAttrString (result, "artificial", arg) < 0)
  191.     goto failarg;
  192.   Py_DECREF (arg);

  193.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  194.     arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False;
  195.   else
  196.     arg = Py_False;
  197.   Py_INCREF (arg);
  198.   if (PyObject_SetAttrString (result, "is_base_class", arg) < 0)
  199.     goto failarg;
  200.   Py_DECREF (arg);

  201.   arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
  202.   if (!arg)
  203.     goto fail;
  204.   if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
  205.     goto failarg;
  206.   Py_DECREF (arg);

  207.   /* A field can have a NULL type in some situations.  */
  208.   if (TYPE_FIELD_TYPE (type, field) == NULL)
  209.     {
  210.       arg = Py_None;
  211.       Py_INCREF (arg);
  212.     }
  213.   else
  214.     arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
  215.   if (!arg)
  216.     goto fail;
  217.   if (PyObject_SetAttrString (result, "type", arg) < 0)
  218.     goto failarg;
  219.   Py_DECREF (arg);

  220.   return result;

  221. failarg:
  222.   Py_DECREF (arg);
  223. fail:
  224.   Py_DECREF (result);
  225.   return NULL;
  226. }

  227. /* Helper function to return the name of a field, as a gdb.Field object.
  228.    If the field doesn't have a name, None is returned.  */

  229. static PyObject *
  230. field_name (struct type *type, int field)
  231. {
  232.   PyObject *result;

  233.   if (TYPE_FIELD_NAME (type, field))
  234.     result = PyString_FromString (TYPE_FIELD_NAME (type, field));
  235.   else
  236.     {
  237.       result = Py_None;
  238.       Py_INCREF (result);
  239.     }
  240.   return result;
  241. }

  242. /* Helper function for Type standard mapping methods.  Returns a
  243.    Python object for field i of the type.  "kind" specifies what to
  244.    return: the name of the field, a gdb.Field object corresponding to
  245.    the field, or a tuple consisting of field name and gdb.Field
  246.    object.  */

  247. static PyObject *
  248. make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
  249. {
  250.   PyObject *item = NULL, *key = NULL, *value = NULL;

  251.   switch (kind)
  252.     {
  253.     case iter_items:
  254.       key = field_name (type, i);
  255.       if (key == NULL)
  256.         goto fail;
  257.       value = convert_field (type, i);
  258.       if (value == NULL)
  259.         goto fail;
  260.       item = PyTuple_New (2);
  261.       if (item == NULL)
  262.         goto fail;
  263.       PyTuple_SET_ITEM (item, 0, key);
  264.       PyTuple_SET_ITEM (item, 1, value);
  265.       break;
  266.     case iter_keys:
  267.       item = field_name (type, i);
  268.       break;
  269.     case iter_values:
  270.       item =  convert_field (type, i);
  271.       break;
  272.     default:
  273.       gdb_assert_not_reached ("invalid gdbpy_iter_kind");
  274.     }
  275.   return item;

  276. fail:
  277.   Py_XDECREF (key);
  278.   Py_XDECREF (value);
  279.   Py_XDECREF (item);
  280.   return NULL;
  281. }

  282. /* Return a sequence of all field names, fields, or (name, field) pairs.
  283.    Each field is a gdb.Field object.  */

  284. static PyObject *
  285. typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
  286. {
  287.   PyObject *py_type = self;
  288.   PyObject *result = NULL, *iter = NULL;
  289.   volatile struct gdb_exception except;
  290.   struct type *type = ((type_object *) py_type)->type;
  291.   struct type *checked_type = type;

  292.   TRY_CATCH (except, RETURN_MASK_ALL)
  293.     {
  294.       CHECK_TYPEDEF (checked_type);
  295.     }
  296.   GDB_PY_HANDLE_EXCEPTION (except);

  297.   if (checked_type != type)
  298.     py_type = type_to_type_object (checked_type);
  299.   iter = typy_make_iter (py_type, kind);
  300.   if (checked_type != type)
  301.     {
  302.       /* Need to wrap this in braces because Py_DECREF isn't wrapped
  303.          in a do{}while(0).  */
  304.       Py_DECREF (py_type);
  305.     }
  306.   if (iter != NULL)
  307.     {
  308.       result = PySequence_List (iter);
  309.       Py_DECREF (iter);
  310.     }

  311.   return result;
  312. }

  313. /* Return a sequence of all fields.  Each field is a gdb.Field object.  */

  314. static PyObject *
  315. typy_values (PyObject *self, PyObject *args)
  316. {
  317.   return typy_fields_items (self, iter_values);
  318. }

  319. /* Return a sequence of all fields.  Each field is a gdb.Field object.
  320.    This method is similar to typy_values, except where the supplied
  321.    gdb.Type is an array, in which case it returns a list of one entry
  322.    which is a gdb.Field object for a range (the array bounds).  */

  323. static PyObject *
  324. typy_fields (PyObject *self, PyObject *args)
  325. {
  326.   struct type *type = ((type_object *) self)->type;
  327.   PyObject *r, *rl;

  328.   if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
  329.     return typy_fields_items (self, iter_values);

  330.   /* Array type.  Handle this as a special case because the common
  331.      machinery wants struct or union or enum types.  Build a list of
  332.      one entry which is the range for the array.  */
  333.   r = convert_field (type, 0);
  334.   if (r == NULL)
  335.     return NULL;

  336.   rl = Py_BuildValue ("[O]", r);
  337.   Py_DECREF (r);

  338.   return rl;
  339. }

  340. /* Return a sequence of all field names.  Each field is a gdb.Field object.  */

  341. static PyObject *
  342. typy_field_names (PyObject *self, PyObject *args)
  343. {
  344.   return typy_fields_items (self, iter_keys);
  345. }

  346. /* Return a sequence of all (name, fields) pairs.  Each field is a
  347.    gdb.Field object.  */

  348. static PyObject *
  349. typy_items (PyObject *self, PyObject *args)
  350. {
  351.   return typy_fields_items (self, iter_items);
  352. }

  353. /* Return the type's name, or None.  */

  354. static PyObject *
  355. typy_get_name (PyObject *self, void *closure)
  356. {
  357.   struct type *type = ((type_object *) self)->type;

  358.   if (TYPE_NAME (type) == NULL)
  359.     Py_RETURN_NONE;
  360.   return PyString_FromString (TYPE_NAME (type));
  361. }

  362. /* Return the type's tag, or None.  */
  363. static PyObject *
  364. typy_get_tag (PyObject *self, void *closure)
  365. {
  366.   struct type *type = ((type_object *) self)->type;

  367.   if (!TYPE_TAG_NAME (type))
  368.     Py_RETURN_NONE;
  369.   return PyString_FromString (TYPE_TAG_NAME (type));
  370. }

  371. /* Return the type, stripped of typedefs. */
  372. static PyObject *
  373. typy_strip_typedefs (PyObject *self, PyObject *args)
  374. {
  375.   struct type *type = ((type_object *) self)->type;
  376.   volatile struct gdb_exception except;

  377.   TRY_CATCH (except, RETURN_MASK_ALL)
  378.     {
  379.       type = check_typedef (type);
  380.     }
  381.   GDB_PY_HANDLE_EXCEPTION (except);

  382.   return type_to_type_object (type);
  383. }

  384. /* Strip typedefs and pointers/reference from a type.  Then check that
  385.    it is a struct, union, or enum type.  If not, raise TypeError.  */

  386. static struct type *
  387. typy_get_composite (struct type *type)
  388. {
  389.   volatile struct gdb_exception except;

  390.   for (;;)
  391.     {
  392.       TRY_CATCH (except, RETURN_MASK_ALL)
  393.         {
  394.           CHECK_TYPEDEF (type);
  395.         }
  396.       GDB_PY_HANDLE_EXCEPTION (except);

  397.       if (TYPE_CODE (type) != TYPE_CODE_PTR
  398.           && TYPE_CODE (type) != TYPE_CODE_REF)
  399.         break;
  400.       type = TYPE_TARGET_TYPE (type);
  401.     }

  402.   /* If this is not a struct, union, or enum type, raise TypeError
  403.      exception.  */
  404.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  405.       && TYPE_CODE (type) != TYPE_CODE_UNION
  406.       && TYPE_CODE (type) != TYPE_CODE_ENUM)
  407.     {
  408.       PyErr_SetString (PyExc_TypeError,
  409.                        "Type is not a structure, union, or enum type.");
  410.       return NULL;
  411.     }

  412.   return type;
  413. }

  414. /* Helper for typy_array and typy_vector.  */

  415. static PyObject *
  416. typy_array_1 (PyObject *self, PyObject *args, int is_vector)
  417. {
  418.   long n1, n2;
  419.   PyObject *n2_obj = NULL;
  420.   struct type *array = NULL;
  421.   struct type *type = ((type_object *) self)->type;
  422.   volatile struct gdb_exception except;

  423.   if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
  424.     return NULL;

  425.   if (n2_obj)
  426.     {
  427.       if (!PyInt_Check (n2_obj))
  428.         {
  429.           PyErr_SetString (PyExc_RuntimeError,
  430.                            _("Array bound must be an integer"));
  431.           return NULL;
  432.         }

  433.       if (! gdb_py_int_as_long (n2_obj, &n2))
  434.         return NULL;
  435.     }
  436.   else
  437.     {
  438.       n2 = n1;
  439.       n1 = 0;
  440.     }

  441.   if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1.  */
  442.     {
  443.       PyErr_SetString (PyExc_ValueError,
  444.                        _("Array length must not be negative"));
  445.       return NULL;
  446.     }

  447.   TRY_CATCH (except, RETURN_MASK_ALL)
  448.     {
  449.       array = lookup_array_range_type (type, n1, n2);
  450.       if (is_vector)
  451.         make_vector_type (array);
  452.     }
  453.   GDB_PY_HANDLE_EXCEPTION (except);

  454.   return type_to_type_object (array);
  455. }

  456. /* Return an array type.  */

  457. static PyObject *
  458. typy_array (PyObject *self, PyObject *args)
  459. {
  460.   return typy_array_1 (self, args, 0);
  461. }

  462. /* Return a vector type.  */

  463. static PyObject *
  464. typy_vector (PyObject *self, PyObject *args)
  465. {
  466.   return typy_array_1 (self, args, 1);
  467. }

  468. /* Return a Type object which represents a pointer to SELF.  */
  469. static PyObject *
  470. typy_pointer (PyObject *self, PyObject *args)
  471. {
  472.   struct type *type = ((type_object *) self)->type;
  473.   volatile struct gdb_exception except;

  474.   TRY_CATCH (except, RETURN_MASK_ALL)
  475.     {
  476.       type = lookup_pointer_type (type);
  477.     }
  478.   GDB_PY_HANDLE_EXCEPTION (except);

  479.   return type_to_type_object (type);
  480. }

  481. /* Return the range of a type represented by SELF.  The return type is
  482.    a tuple.  The first element of the tuple contains the low bound,
  483.    while the second element of the tuple contains the high bound.  */
  484. static PyObject *
  485. typy_range (PyObject *self, PyObject *args)
  486. {
  487.   struct type *type = ((type_object *) self)->type;
  488.   PyObject *result;
  489.   PyObject *low_bound = NULL, *high_bound = NULL;
  490.   /* Initialize these to appease GCC warnings.  */
  491.   LONGEST low = 0, high = 0;

  492.   if (TYPE_CODE (type) != TYPE_CODE_ARRAY
  493.       && TYPE_CODE (type) != TYPE_CODE_STRING
  494.       && TYPE_CODE (type) != TYPE_CODE_RANGE)
  495.     {
  496.       PyErr_SetString (PyExc_RuntimeError,
  497.                        _("This type does not have a range."));
  498.       return NULL;
  499.     }

  500.   switch (TYPE_CODE (type))
  501.     {
  502.     case TYPE_CODE_ARRAY:
  503.     case TYPE_CODE_STRING:
  504.       low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
  505.       high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
  506.       break;
  507.     case TYPE_CODE_RANGE:
  508.       low = TYPE_LOW_BOUND (type);
  509.       high = TYPE_HIGH_BOUND (type);
  510.       break;
  511.     }

  512.   low_bound = PyLong_FromLong (low);
  513.   if (!low_bound)
  514.     goto failarg;

  515.   high_bound = PyLong_FromLong (high);
  516.   if (!high_bound)
  517.     goto failarg;

  518.   result = PyTuple_New (2);
  519.   if (!result)
  520.     goto failarg;

  521.   if (PyTuple_SetItem (result, 0, low_bound) != 0)
  522.     {
  523.       Py_DECREF (result);
  524.       goto failarg;
  525.     }
  526.   if (PyTuple_SetItem (result, 1, high_bound) != 0)
  527.     {
  528.       Py_DECREF (high_bound);
  529.       Py_DECREF (result);
  530.       return NULL;
  531.     }
  532.   return result;

  533. failarg:
  534.   Py_XDECREF (high_bound);
  535.   Py_XDECREF (low_bound);
  536.   return NULL;
  537. }

  538. /* Return a Type object which represents a reference to SELF.  */
  539. static PyObject *
  540. typy_reference (PyObject *self, PyObject *args)
  541. {
  542.   struct type *type = ((type_object *) self)->type;
  543.   volatile struct gdb_exception except;

  544.   TRY_CATCH (except, RETURN_MASK_ALL)
  545.     {
  546.       type = lookup_reference_type (type);
  547.     }
  548.   GDB_PY_HANDLE_EXCEPTION (except);

  549.   return type_to_type_object (type);
  550. }

  551. /* Return a Type object which represents the target type of SELF.  */
  552. static PyObject *
  553. typy_target (PyObject *self, PyObject *args)
  554. {
  555.   struct type *type = ((type_object *) self)->type;

  556.   if (!TYPE_TARGET_TYPE (type))
  557.     {
  558.       PyErr_SetString (PyExc_RuntimeError,
  559.                        _("Type does not have a target."));
  560.       return NULL;
  561.     }

  562.   return type_to_type_object (TYPE_TARGET_TYPE (type));
  563. }

  564. /* Return a const-qualified type variant.  */
  565. static PyObject *
  566. typy_const (PyObject *self, PyObject *args)
  567. {
  568.   struct type *type = ((type_object *) self)->type;
  569.   volatile struct gdb_exception except;

  570.   TRY_CATCH (except, RETURN_MASK_ALL)
  571.     {
  572.       type = make_cv_type (1, 0, type, NULL);
  573.     }
  574.   GDB_PY_HANDLE_EXCEPTION (except);

  575.   return type_to_type_object (type);
  576. }

  577. /* Return a volatile-qualified type variant.  */
  578. static PyObject *
  579. typy_volatile (PyObject *self, PyObject *args)
  580. {
  581.   struct type *type = ((type_object *) self)->type;
  582.   volatile struct gdb_exception except;

  583.   TRY_CATCH (except, RETURN_MASK_ALL)
  584.     {
  585.       type = make_cv_type (0, 1, type, NULL);
  586.     }
  587.   GDB_PY_HANDLE_EXCEPTION (except);

  588.   return type_to_type_object (type);
  589. }

  590. /* Return an unqualified type variant.  */
  591. static PyObject *
  592. typy_unqualified (PyObject *self, PyObject *args)
  593. {
  594.   struct type *type = ((type_object *) self)->type;
  595.   volatile struct gdb_exception except;

  596.   TRY_CATCH (except, RETURN_MASK_ALL)
  597.     {
  598.       type = make_cv_type (0, 0, type, NULL);
  599.     }
  600.   GDB_PY_HANDLE_EXCEPTION (except);

  601.   return type_to_type_object (type);
  602. }

  603. /* Return the size of the type represented by SELF, in bytes.  */
  604. static PyObject *
  605. typy_get_sizeof (PyObject *self, void *closure)
  606. {
  607.   struct type *type = ((type_object *) self)->type;
  608.   volatile struct gdb_exception except;

  609.   TRY_CATCH (except, RETURN_MASK_ALL)
  610.     {
  611.       check_typedef (type);
  612.     }
  613.   /* Ignore exceptions.  */

  614.   return gdb_py_long_from_longest (TYPE_LENGTH (type));
  615. }

  616. static struct type *
  617. typy_lookup_typename (const char *type_name, const struct block *block)
  618. {
  619.   struct type *type = NULL;
  620.   volatile struct gdb_exception except;

  621.   TRY_CATCH (except, RETURN_MASK_ALL)
  622.     {
  623.       if (!strncmp (type_name, "struct ", 7))
  624.         type = lookup_struct (type_name + 7, NULL);
  625.       else if (!strncmp (type_name, "union ", 6))
  626.         type = lookup_union (type_name + 6, NULL);
  627.       else if (!strncmp (type_name, "enum ", 5))
  628.         type = lookup_enum (type_name + 5, NULL);
  629.       else
  630.         type = lookup_typename (python_language, python_gdbarch,
  631.                                 type_name, block, 0);
  632.     }
  633.   GDB_PY_HANDLE_EXCEPTION (except);

  634.   return type;
  635. }

  636. static struct type *
  637. typy_lookup_type (struct demangle_component *demangled,
  638.                   const struct block *block)
  639. {
  640.   struct type *type, *rtype = NULL;
  641.   char *type_name = NULL;
  642.   enum demangle_component_type demangled_type;
  643.   volatile struct gdb_exception except;

  644.   /* Save the type: typy_lookup_type() may (indirectly) overwrite
  645.      memory pointed by demangled.  */
  646.   demangled_type = demangled->type;

  647.   if (demangled_type == DEMANGLE_COMPONENT_POINTER
  648.       || demangled_type == DEMANGLE_COMPONENT_REFERENCE
  649.       || demangled_type == DEMANGLE_COMPONENT_CONST
  650.       || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
  651.     {
  652.       type = typy_lookup_type (demangled->u.s_binary.left, block);
  653.       if (! type)
  654.         return NULL;

  655.       TRY_CATCH (except, RETURN_MASK_ALL)
  656.         {
  657.           /* If the demangled_type matches with one of the types
  658.              below, run the corresponding function and save the type
  659.              to return later.  We cannot just return here as we are in
  660.              an exception handler.  */
  661.           switch (demangled_type)
  662.             {
  663.             case DEMANGLE_COMPONENT_REFERENCE:
  664.               rtype =  lookup_reference_type (type);
  665.               break;
  666.             case DEMANGLE_COMPONENT_POINTER:
  667.               rtype = lookup_pointer_type (type);
  668.               break;
  669.             case DEMANGLE_COMPONENT_CONST:
  670.               rtype = make_cv_type (1, 0, type, NULL);
  671.               break;
  672.             case DEMANGLE_COMPONENT_VOLATILE:
  673.               rtype = make_cv_type (0, 1, type, NULL);
  674.               break;
  675.             }
  676.         }
  677.       GDB_PY_HANDLE_EXCEPTION (except);
  678.     }

  679.   /* If we have a type from the switch statement above, just return
  680.      that.  */
  681.   if (rtype)
  682.     return rtype;

  683.   /* We don't have a type, so lookup the type.  */
  684.   type_name = cp_comp_to_string (demangled, 10);
  685.   type = typy_lookup_typename (type_name, block);
  686.   xfree (type_name);

  687.   return type;
  688. }

  689. /* This is a helper function for typy_template_argument that is used
  690.    when the type does not have template symbols attached.  It works by
  691.    parsing the type name.  This happens with compilers, like older
  692.    versions of GCC, that do not emit DW_TAG_template_*.  */

  693. static PyObject *
  694. typy_legacy_template_argument (struct type *type, const struct block *block,
  695.                                int argno)
  696. {
  697.   int i;
  698.   struct demangle_component *demangled;
  699.   struct demangle_parse_info *info = NULL;
  700.   const char *err;
  701.   struct type *argtype;
  702.   struct cleanup *cleanup;
  703.   volatile struct gdb_exception except;

  704.   if (TYPE_NAME (type) == NULL)
  705.     {
  706.       PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
  707.       return NULL;
  708.     }

  709.   TRY_CATCH (except, RETURN_MASK_ALL)
  710.     {
  711.       /* Note -- this is not thread-safe.  */
  712.       info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
  713.     }
  714.   GDB_PY_HANDLE_EXCEPTION (except);

  715.   if (! info)
  716.     {
  717.       PyErr_SetString (PyExc_RuntimeError, err);
  718.       return NULL;
  719.     }
  720.   demangled = info->tree;
  721.   cleanup = make_cleanup_cp_demangled_name_parse_free (info);

  722.   /* Strip off component names.  */
  723.   while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
  724.          || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
  725.     demangled = demangled->u.s_binary.right;

  726.   if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
  727.     {
  728.       do_cleanups (cleanup);
  729.       PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
  730.       return NULL;
  731.     }

  732.   /* Skip from the template to the arguments.  */
  733.   demangled = demangled->u.s_binary.right;

  734.   for (i = 0; demangled && i < argno; ++i)
  735.     demangled = demangled->u.s_binary.right;

  736.   if (! demangled)
  737.     {
  738.       do_cleanups (cleanup);
  739.       PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
  740.                     argno);
  741.       return NULL;
  742.     }

  743.   argtype = typy_lookup_type (demangled->u.s_binary.left, block);
  744.   do_cleanups (cleanup);
  745.   if (! argtype)
  746.     return NULL;

  747.   return type_to_type_object (argtype);
  748. }

  749. static PyObject *
  750. typy_template_argument (PyObject *self, PyObject *args)
  751. {
  752.   int argno;
  753.   struct type *type = ((type_object *) self)->type;
  754.   const struct block *block = NULL;
  755.   PyObject *block_obj = NULL;
  756.   struct symbol *sym;
  757.   struct value *val = NULL;
  758.   volatile struct gdb_exception except;

  759.   if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
  760.     return NULL;

  761.   if (block_obj)
  762.     {
  763.       block = block_object_to_block (block_obj);
  764.       if (! block)
  765.         {
  766.           PyErr_SetString (PyExc_RuntimeError,
  767.                            _("Second argument must be block."));
  768.           return NULL;
  769.         }
  770.     }

  771.   TRY_CATCH (except, RETURN_MASK_ALL)
  772.     {
  773.       type = check_typedef (type);
  774.       if (TYPE_CODE (type) == TYPE_CODE_REF)
  775.         type = check_typedef (TYPE_TARGET_TYPE (type));
  776.     }
  777.   GDB_PY_HANDLE_EXCEPTION (except);

  778.   /* We might not have DW_TAG_template_*, so try to parse the type's
  779.      name.  This is inefficient if we do not have a template type --
  780.      but that is going to wind up as an error anyhow.  */
  781.   if (! TYPE_N_TEMPLATE_ARGUMENTS (type))
  782.     return typy_legacy_template_argument (type, block, argno);

  783.   if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
  784.     {
  785.       PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
  786.                     argno);
  787.       return NULL;
  788.     }

  789.   sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
  790.   if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  791.     return type_to_type_object (SYMBOL_TYPE (sym));
  792.   else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT)
  793.     {
  794.       PyErr_Format (PyExc_RuntimeError,
  795.                     _("Template argument is optimized out"));
  796.       return NULL;
  797.     }

  798.   TRY_CATCH (except, RETURN_MASK_ALL)
  799.     {
  800.       val = value_of_variable (sym, block);
  801.     }
  802.   GDB_PY_HANDLE_EXCEPTION (except);

  803.   return value_to_value_object (val);
  804. }

  805. static PyObject *
  806. typy_str (PyObject *self)
  807. {
  808.   volatile struct gdb_exception except;
  809.   char *thetype = NULL;
  810.   long length = 0;
  811.   PyObject *result;

  812.   TRY_CATCH (except, RETURN_MASK_ALL)
  813.     {
  814.       struct cleanup *old_chain;
  815.       struct ui_file *stb;

  816.       stb = mem_fileopen ();
  817.       old_chain = make_cleanup_ui_file_delete (stb);

  818.       LA_PRINT_TYPE (type_object_to_type (self), "", stb, -1, 0,
  819.                      &type_print_raw_options);

  820.       thetype = ui_file_xstrdup (stb, &length);
  821.       do_cleanups (old_chain);
  822.     }
  823.   if (except.reason < 0)
  824.     {
  825.       xfree (thetype);
  826.       GDB_PY_HANDLE_EXCEPTION (except);
  827.     }

  828.   result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
  829.   xfree (thetype);

  830.   return result;
  831. }

  832. /* Implement the richcompare method.  */

  833. static PyObject *
  834. typy_richcompare (PyObject *self, PyObject *other, int op)
  835. {
  836.   int result = Py_NE;
  837.   struct type *type1 = type_object_to_type (self);
  838.   struct type *type2 = type_object_to_type (other);
  839.   volatile struct gdb_exception except;

  840.   /* We can only compare ourselves to another Type object, and only
  841.      for equality or inequality.  */
  842.   if (type2 == NULL || (op != Py_EQ && op != Py_NE))
  843.     {
  844.       Py_INCREF (Py_NotImplemented);
  845.       return Py_NotImplemented;
  846.     }

  847.   if (type1 == type2)
  848.     result = Py_EQ;
  849.   else
  850.     {
  851.       TRY_CATCH (except, RETURN_MASK_ALL)
  852.         {
  853.           result = types_deeply_equal (type1, type2);
  854.         }
  855.       /* If there is a GDB exception, a comparison is not capable
  856.          (or trusted), so exit.  */
  857.       GDB_PY_HANDLE_EXCEPTION (except);
  858.     }

  859.   if (op == (result ? Py_EQ : Py_NE))
  860.     Py_RETURN_TRUE;
  861.   Py_RETURN_FALSE;
  862. }



  863. static const struct objfile_data *typy_objfile_data_key;

  864. static void
  865. save_objfile_types (struct objfile *objfile, void *datum)
  866. {
  867.   type_object *obj = datum;
  868.   htab_t copied_types;
  869.   struct cleanup *cleanup;

  870.   if (!gdb_python_initialized)
  871.     return;

  872.   /* This prevents another thread from freeing the objects we're
  873.      operating on.  */
  874.   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);

  875.   copied_types = create_copied_types_hash (objfile);

  876.   while (obj)
  877.     {
  878.       type_object *next = obj->next;

  879.       htab_empty (copied_types);

  880.       obj->type = copy_type_recursive (objfile, obj->type, copied_types);

  881.       obj->next = NULL;
  882.       obj->prev = NULL;

  883.       obj = next;
  884.     }

  885.   htab_delete (copied_types);

  886.   do_cleanups (cleanup);
  887. }

  888. static void
  889. set_type (type_object *obj, struct type *type)
  890. {
  891.   obj->type = type;
  892.   obj->prev = NULL;
  893.   if (type && TYPE_OBJFILE (type))
  894.     {
  895.       struct objfile *objfile = TYPE_OBJFILE (type);

  896.       obj->next = objfile_data (objfile, typy_objfile_data_key);
  897.       if (obj->next)
  898.         obj->next->prev = obj;
  899.       set_objfile_data (objfile, typy_objfile_data_key, obj);
  900.     }
  901.   else
  902.     obj->next = NULL;
  903. }

  904. static void
  905. typy_dealloc (PyObject *obj)
  906. {
  907.   type_object *type = (type_object *) obj;

  908.   if (type->prev)
  909.     type->prev->next = type->next;
  910.   else if (type->type && TYPE_OBJFILE (type->type))
  911.     {
  912.       /* Must reset head of list.  */
  913.       struct objfile *objfile = TYPE_OBJFILE (type->type);

  914.       if (objfile)
  915.         set_objfile_data (objfile, typy_objfile_data_key, type->next);
  916.     }
  917.   if (type->next)
  918.     type->next->prev = type->prev;

  919.   Py_TYPE (type)->tp_free (type);
  920. }

  921. /* Return number of fields ("length" of the field dictionary).  */

  922. static Py_ssize_t
  923. typy_length (PyObject *self)
  924. {
  925.   struct type *type = ((type_object *) self)->type;

  926.   type = typy_get_composite (type);
  927.   if (type == NULL)
  928.     return -1;

  929.   return TYPE_NFIELDS (type);
  930. }

  931. /* Implements boolean evaluation of gdb.Type.  Handle this like other
  932.    Python objects that don't have a meaningful truth value -- all
  933.    values are true.  */

  934. static int
  935. typy_nonzero (PyObject *self)
  936. {
  937.   return 1;
  938. }

  939. /* Return a gdb.Field object for the field named by the argument.  */

  940. static PyObject *
  941. typy_getitem (PyObject *self, PyObject *key)
  942. {
  943.   struct type *type = ((type_object *) self)->type;
  944.   char *field;
  945.   int i;

  946.   field = python_string_to_host_string (key);
  947.   if (field == NULL)
  948.     return NULL;

  949.   /* We want just fields of this type, not of base types, so instead of
  950.      using lookup_struct_elt_type, portions of that function are
  951.      copied here.  */

  952.   type = typy_get_composite (type);
  953.   if (type == NULL)
  954.     return NULL;

  955.   for (i = 0; i < TYPE_NFIELDS (type); i++)
  956.     {
  957.       const char *t_field_name = TYPE_FIELD_NAME (type, i);

  958.       if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
  959.         {
  960.           return convert_field (type, i);
  961.         }
  962.     }
  963.   PyErr_SetObject (PyExc_KeyError, key);
  964.   return NULL;
  965. }

  966. /* Implement the "get" method on the type object.  This is the
  967.    same as getitem if the key is present, but returns the supplied
  968.    default value or None if the key is not found.  */

  969. static PyObject *
  970. typy_get (PyObject *self, PyObject *args)
  971. {
  972.   PyObject *key, *defval = Py_None, *result;

  973.   if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
  974.     return NULL;

  975.   result = typy_getitem (self, key);
  976.   if (result != NULL)
  977.     return result;

  978.   /* typy_getitem returned error status.  If the exception is
  979.      KeyError, clear the exception status and return the defval
  980.      instead.  Otherwise return the exception unchanged.  */
  981.   if (!PyErr_ExceptionMatches (PyExc_KeyError))
  982.     return NULL;

  983.   PyErr_Clear ();
  984.   Py_INCREF (defval);
  985.   return defval;
  986. }

  987. /* Implement the "has_key" method on the type object.  */

  988. static PyObject *
  989. typy_has_key (PyObject *self, PyObject *args)
  990. {
  991.   struct type *type = ((type_object *) self)->type;
  992.   const char *field;
  993.   int i;

  994.   if (!PyArg_ParseTuple (args, "s", &field))
  995.     return NULL;

  996.   /* We want just fields of this type, not of base types, so instead of
  997.      using lookup_struct_elt_type, portions of that function are
  998.      copied here.  */

  999.   type = typy_get_composite (type);
  1000.   if (type == NULL)
  1001.     return NULL;

  1002.   for (i = 0; i < TYPE_NFIELDS (type); i++)
  1003.     {
  1004.       const char *t_field_name = TYPE_FIELD_NAME (type, i);

  1005.       if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
  1006.         Py_RETURN_TRUE;
  1007.     }
  1008.   Py_RETURN_FALSE;
  1009. }

  1010. /* Make an iterator object to iterate over keys, values, or items.  */

  1011. static PyObject *
  1012. typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind)
  1013. {
  1014.   typy_iterator_object *typy_iter_obj;

  1015.   /* Check that "self" is a structure or union type.  */
  1016.   if (typy_get_composite (((type_object *) self)->type) == NULL)
  1017.     return NULL;

  1018.   typy_iter_obj = PyObject_New (typy_iterator_object,
  1019.                                 &type_iterator_object_type);
  1020.   if (typy_iter_obj == NULL)
  1021.       return NULL;

  1022.   typy_iter_obj->field = 0;
  1023.   typy_iter_obj->kind = kind;
  1024.   Py_INCREF (self);
  1025.   typy_iter_obj->source = (type_object *) self;

  1026.   return (PyObject *) typy_iter_obj;
  1027. }

  1028. /* iteritems() method.  */

  1029. static PyObject *
  1030. typy_iteritems (PyObject *self, PyObject *args)
  1031. {
  1032.   return typy_make_iter (self, iter_items);
  1033. }

  1034. /* iterkeys() method.  */

  1035. static PyObject *
  1036. typy_iterkeys (PyObject *self, PyObject *args)
  1037. {
  1038.   return typy_make_iter (self, iter_keys);
  1039. }

  1040. /* Iterating over the class, same as iterkeys except for the function
  1041.    signature.  */

  1042. static PyObject *
  1043. typy_iter (PyObject *self)
  1044. {
  1045.   return typy_make_iter (self, iter_keys);
  1046. }

  1047. /* itervalues() method.  */

  1048. static PyObject *
  1049. typy_itervalues (PyObject *self, PyObject *args)
  1050. {
  1051.   return typy_make_iter (self, iter_values);
  1052. }

  1053. /* Return a reference to the type iterator.  */

  1054. static PyObject *
  1055. typy_iterator_iter (PyObject *self)
  1056. {
  1057.   Py_INCREF (self);
  1058.   return self;
  1059. }

  1060. /* Return the next field in the iteration through the list of fields
  1061.    of the type.  */

  1062. static PyObject *
  1063. typy_iterator_iternext (PyObject *self)
  1064. {
  1065.   typy_iterator_object *iter_obj = (typy_iterator_object *) self;
  1066.   struct type *type = iter_obj->source->type;
  1067.   PyObject *result;

  1068.   if (iter_obj->field < TYPE_NFIELDS (type))
  1069.     {
  1070.       result = make_fielditem (type, iter_obj->field, iter_obj->kind);
  1071.       if (result != NULL)
  1072.         iter_obj->field++;
  1073.       return result;
  1074.     }

  1075.   return NULL;
  1076. }

  1077. static void
  1078. typy_iterator_dealloc (PyObject *obj)
  1079. {
  1080.   typy_iterator_object *iter_obj = (typy_iterator_object *) obj;

  1081.   Py_DECREF (iter_obj->source);
  1082. }

  1083. /* Create a new Type referring to TYPE.  */
  1084. PyObject *
  1085. type_to_type_object (struct type *type)
  1086. {
  1087.   type_object *type_obj;

  1088.   type_obj = PyObject_New (type_object, &type_object_type);
  1089.   if (type_obj)
  1090.     set_type (type_obj, type);

  1091.   return (PyObject *) type_obj;
  1092. }

  1093. struct type *
  1094. type_object_to_type (PyObject *obj)
  1095. {
  1096.   if (! PyObject_TypeCheck (obj, &type_object_type))
  1097.     return NULL;
  1098.   return ((type_object *) obj)->type;
  1099. }



  1100. /* Implementation of gdb.lookup_type.  */
  1101. PyObject *
  1102. gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
  1103. {
  1104.   static char *keywords[] = { "name", "block", NULL };
  1105.   const char *type_name = NULL;
  1106.   struct type *type = NULL;
  1107.   PyObject *block_obj = NULL;
  1108.   const struct block *block = NULL;

  1109.   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
  1110.                                      &type_name, &block_obj))
  1111.     return NULL;

  1112.   if (block_obj)
  1113.     {
  1114.       block = block_object_to_block (block_obj);
  1115.       if (! block)
  1116.         {
  1117.           PyErr_SetString (PyExc_RuntimeError,
  1118.                            _("'block' argument must be a Block."));
  1119.           return NULL;
  1120.         }
  1121.     }

  1122.   type = typy_lookup_typename (type_name, block);
  1123.   if (! type)
  1124.     return NULL;

  1125.   return (PyObject *) type_to_type_object (type);
  1126. }

  1127. int
  1128. gdbpy_initialize_types (void)
  1129. {
  1130.   int i;

  1131.   typy_objfile_data_key
  1132.     = register_objfile_data_with_cleanup (save_objfile_types, NULL);

  1133.   if (PyType_Ready (&type_object_type) < 0)
  1134.     return -1;
  1135.   if (PyType_Ready (&field_object_type) < 0)
  1136.     return -1;
  1137.   if (PyType_Ready (&type_iterator_object_type) < 0)
  1138.     return -1;

  1139.   for (i = 0; pyty_codes[i].name; ++i)
  1140.     {
  1141.       if (PyModule_AddIntConstant (gdb_module,
  1142.                                    /* Cast needed for Python 2.4.  */
  1143.                                    (char *) pyty_codes[i].name,
  1144.                                    pyty_codes[i].code) < 0)
  1145.         return -1;
  1146.     }

  1147.   if (gdb_pymodule_addobject (gdb_module, "Type",
  1148.                               (PyObject *) &type_object_type) < 0)
  1149.     return -1;

  1150.   if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
  1151.                               (PyObject *) &type_iterator_object_type) < 0)
  1152.     return -1;

  1153.   return gdb_pymodule_addobject (gdb_module, "Field",
  1154.                                  (PyObject *) &field_object_type);
  1155. }



  1156. static PyGetSetDef type_object_getset[] =
  1157. {
  1158.   { "code", typy_get_code, NULL,
  1159.     "The code for this type.", NULL },
  1160.   { "name", typy_get_name, NULL,
  1161.     "The name for this type, or None.", NULL },
  1162.   { "sizeof", typy_get_sizeof, NULL,
  1163.     "The size of this type, in bytes.", NULL },
  1164.   { "tag", typy_get_tag, NULL,
  1165.     "The tag name for this type, or None.", NULL },
  1166.   { NULL }
  1167. };

  1168. static PyMethodDef type_object_methods[] =
  1169. {
  1170.   { "array", typy_array, METH_VARARGS,
  1171.     "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
  1172. Return a type which represents an array of objects of this type.\n\
  1173. The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
  1174. If LOW_BOUND is omitted, a value of zero is used." },
  1175.   { "vector", typy_vector, METH_VARARGS,
  1176.     "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
  1177. Return a type which represents a vector of objects of this type.\n\
  1178. The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
  1179. If LOW_BOUND is omitted, a value of zero is used.\n\
  1180. Vectors differ from arrays in that if the current language has C-style\n\
  1181. arrays, vectors don't decay to a pointer to the first element.\n\
  1182. They are first class values." },
  1183.    { "__contains__", typy_has_key, METH_VARARGS,
  1184.      "T.__contains__(k) -> True if T has a field named k, else False" },
  1185.   { "const", typy_const, METH_NOARGS,
  1186.     "const () -> Type\n\
  1187. Return a const variant of this type." },
  1188.   { "fields", typy_fields, METH_NOARGS,
  1189.     "fields () -> list\n\
  1190. Return a list holding all the fields of this type.\n\
  1191. Each field is a gdb.Field object." },
  1192.   { "get", typy_get, METH_VARARGS,
  1193.     "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
  1194. otherwise returns default, if supplied, or None if not." },
  1195.   { "has_key", typy_has_key, METH_VARARGS,
  1196.     "T.has_key(k) -> True if T has a field named k, else False" },
  1197.   { "items", typy_items, METH_NOARGS,
  1198.     "items () -> list\n\
  1199. Return a list of (name, field) pairs of this type.\n\
  1200. Each field is a gdb.Field object." },
  1201.   { "iteritems", typy_iteritems, METH_NOARGS,
  1202.     "iteritems () -> an iterator over the (name, field)\n\
  1203. pairs of this type.  Each field is a gdb.Field object." },
  1204.   { "iterkeys", typy_iterkeys, METH_NOARGS,
  1205.     "iterkeys () -> an iterator over the field names of this type." },
  1206.   { "itervalues", typy_itervalues, METH_NOARGS,
  1207.     "itervalues () -> an iterator over the fields of this type.\n\
  1208. Each field is a gdb.Field object." },
  1209.   { "keys", typy_field_names, METH_NOARGS,
  1210.     "keys () -> list\n\
  1211. Return a list holding all the fields names of this type." },
  1212.   { "pointer", typy_pointer, METH_NOARGS,
  1213.     "pointer () -> Type\n\
  1214. Return a type of pointer to this type." },
  1215.   { "range", typy_range, METH_NOARGS,
  1216.     "range () -> tuple\n\
  1217. Return a tuple containing the lower and upper range for this type."},
  1218.   { "reference", typy_reference, METH_NOARGS,
  1219.     "reference () -> Type\n\
  1220. Return a type of reference to this type." },
  1221.   { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
  1222.     "strip_typedefs () -> Type\n\
  1223. Return a type formed by stripping this type of all typedefs."},
  1224.   { "target", typy_target, METH_NOARGS,
  1225.     "target () -> Type\n\
  1226. Return the target type of this type." },
  1227.   { "template_argument", typy_template_argument, METH_VARARGS,
  1228.     "template_argument (arg, [block]) -> Type\n\
  1229. Return the type of a template argument." },
  1230.   { "unqualified", typy_unqualified, METH_NOARGS,
  1231.     "unqualified () -> Type\n\
  1232. Return a variant of this type without const or volatile attributes." },
  1233.   { "values", typy_values, METH_NOARGS,
  1234.     "values () -> list\n\
  1235. Return a list holding all the fields of this type.\n\
  1236. Each field is a gdb.Field object." },
  1237.   { "volatile", typy_volatile, METH_NOARGS,
  1238.     "volatile () -> Type\n\
  1239. Return a volatile variant of this type" },
  1240.   { NULL }
  1241. };

  1242. static PyNumberMethods type_object_as_number = {
  1243.   NULL,                              /* nb_add */
  1244.   NULL,                              /* nb_subtract */
  1245.   NULL,                              /* nb_multiply */
  1246. #ifndef IS_PY3K
  1247.   NULL,                              /* nb_divide */
  1248. #endif
  1249.   NULL,                              /* nb_remainder */
  1250.   NULL,                              /* nb_divmod */
  1251.   NULL,                              /* nb_power */
  1252.   NULL,                              /* nb_negative */
  1253.   NULL,                              /* nb_positive */
  1254.   NULL,                              /* nb_absolute */
  1255.   typy_nonzero,                      /* nb_nonzero */
  1256.   NULL,                              /* nb_invert */
  1257.   NULL,                              /* nb_lshift */
  1258.   NULL,                              /* nb_rshift */
  1259.   NULL,                              /* nb_and */
  1260.   NULL,                              /* nb_xor */
  1261.   NULL,                              /* nb_or */
  1262. #ifdef IS_PY3K
  1263.   NULL,                              /* nb_int */
  1264.   NULL,                              /* reserved */
  1265. #else
  1266.   NULL,                              /* nb_coerce */
  1267.   NULL,                              /* nb_int */
  1268.   NULL,                              /* nb_long */
  1269. #endif
  1270.   NULL,                              /* nb_float */
  1271. #ifndef IS_PY3K
  1272.   NULL,                              /* nb_oct */
  1273.   NULL                              /* nb_hex */
  1274. #endif
  1275. };

  1276. static PyMappingMethods typy_mapping = {
  1277.   typy_length,
  1278.   typy_getitem,
  1279.   NULL                                  /* no "set" method */
  1280. };

  1281. static PyTypeObject type_object_type =
  1282. {
  1283.   PyVarObject_HEAD_INIT (NULL, 0)
  1284.   "gdb.Type",                          /*tp_name*/
  1285.   sizeof (type_object),                  /*tp_basicsize*/
  1286.   0,                                  /*tp_itemsize*/
  1287.   typy_dealloc,                          /*tp_dealloc*/
  1288.   0,                                  /*tp_print*/
  1289.   0,                                  /*tp_getattr*/
  1290.   0,                                  /*tp_setattr*/
  1291.   0,                                  /*tp_compare*/
  1292.   0,                                  /*tp_repr*/
  1293.   &type_object_as_number,          /*tp_as_number*/
  1294.   0,                                  /*tp_as_sequence*/
  1295.   &typy_mapping,                  /*tp_as_mapping*/
  1296.   0,                                  /*tp_hash */
  1297.   0,                                  /*tp_call*/
  1298.   typy_str,                          /*tp_str*/
  1299.   0,                                  /*tp_getattro*/
  1300.   0,                                  /*tp_setattro*/
  1301.   0,                                  /*tp_as_buffer*/
  1302.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER/*tp_flags*/
  1303.   "GDB type object",                  /* tp_doc */
  1304.   0,                                  /* tp_traverse */
  1305.   0,                                  /* tp_clear */
  1306.   typy_richcompare,                  /* tp_richcompare */
  1307.   0,                                  /* tp_weaklistoffset */
  1308.   typy_iter,                          /* tp_iter */
  1309.   0,                                  /* tp_iternext */
  1310.   type_object_methods,                  /* tp_methods */
  1311.   0,                                  /* tp_members */
  1312.   type_object_getset,                  /* tp_getset */
  1313.   0,                                  /* tp_base */
  1314.   0,                                  /* tp_dict */
  1315.   0,                                  /* tp_descr_get */
  1316.   0,                                  /* tp_descr_set */
  1317.   0,                                  /* tp_dictoffset */
  1318.   0,                                  /* tp_init */
  1319.   0,                                  /* tp_alloc */
  1320.   0,                                  /* tp_new */
  1321. };

  1322. static PyGetSetDef field_object_getset[] =
  1323. {
  1324.   { "__dict__", gdb_py_generic_dict, NULL,
  1325.     "The __dict__ for this field.", &field_object_type },
  1326.   { NULL }
  1327. };

  1328. static PyTypeObject field_object_type =
  1329. {
  1330.   PyVarObject_HEAD_INIT (NULL, 0)
  1331.   "gdb.Field",                          /*tp_name*/
  1332.   sizeof (field_object),          /*tp_basicsize*/
  1333.   0,                                  /*tp_itemsize*/
  1334.   field_dealloc,                  /*tp_dealloc*/
  1335.   0,                                  /*tp_print*/
  1336.   0,                                  /*tp_getattr*/
  1337.   0,                                  /*tp_setattr*/
  1338.   0,                                  /*tp_compare*/
  1339.   0,                                  /*tp_repr*/
  1340.   0,                                  /*tp_as_number*/
  1341.   0,                                  /*tp_as_sequence*/
  1342.   0,                                  /*tp_as_mapping*/
  1343.   0,                                  /*tp_hash */
  1344.   0,                                  /*tp_call*/
  1345.   0,                                  /*tp_str*/
  1346.   0,                                  /*tp_getattro*/
  1347.   0,                                  /*tp_setattro*/
  1348.   0,                                  /*tp_as_buffer*/
  1349.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER/*tp_flags*/
  1350.   "GDB field object",                  /* tp_doc */
  1351.   0,                                  /* tp_traverse */
  1352.   0,                                  /* tp_clear */
  1353.   0,                                  /* tp_richcompare */
  1354.   0,                                  /* tp_weaklistoffset */
  1355.   0,                                  /* tp_iter */
  1356.   0,                                  /* tp_iternext */
  1357.   0,                                  /* tp_methods */
  1358.   0,                                  /* tp_members */
  1359.   field_object_getset,                  /* tp_getset */
  1360.   0,                                  /* tp_base */
  1361.   0,                                  /* tp_dict */
  1362.   0,                                  /* tp_descr_get */
  1363.   0,                                  /* tp_descr_set */
  1364.   offsetof (field_object, dict),  /* tp_dictoffset */
  1365.   0,                                  /* tp_init */
  1366.   0,                                  /* tp_alloc */
  1367.   0,                                  /* tp_new */
  1368. };

  1369. static PyTypeObject type_iterator_object_type = {
  1370.   PyVarObject_HEAD_INIT (NULL, 0)
  1371.   "gdb.TypeIterator",                  /*tp_name*/
  1372.   sizeof (typy_iterator_object),  /*tp_basicsize*/
  1373.   0,                                  /*tp_itemsize*/
  1374.   typy_iterator_dealloc,          /*tp_dealloc*/
  1375.   0,                                  /*tp_print*/
  1376.   0,                                  /*tp_getattr*/
  1377.   0,                                  /*tp_setattr*/
  1378.   0,                                  /*tp_compare*/
  1379.   0,                                  /*tp_repr*/
  1380.   0,                                  /*tp_as_number*/
  1381.   0,                                  /*tp_as_sequence*/
  1382.   0,                                  /*tp_as_mapping*/
  1383.   0,                                  /*tp_hash */
  1384.   0,                                  /*tp_call*/
  1385.   0,                                  /*tp_str*/
  1386.   0,                                  /*tp_getattro*/
  1387.   0,                                  /*tp_setattro*/
  1388.   0,                                  /*tp_as_buffer*/
  1389.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER/*tp_flags*/
  1390.   "GDB type iterator object",          /*tp_doc */
  1391.   0,                                  /*tp_traverse */
  1392.   0,                                  /*tp_clear */
  1393.   0,                                  /*tp_richcompare */
  1394.   0,                                  /*tp_weaklistoffset */
  1395.   typy_iterator_iter,             /*tp_iter */
  1396.   typy_iterator_iternext,          /*tp_iternext */
  1397.   0                                  /*tp_methods */
  1398. };