Automatic date update in version.in
[platform/upstream/binutils.git] / gdb / python / py-type.c
1 /* Python interface to types.
2
3    Copyright (C) 2008-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "value.h"
22 #include "python-internal.h"
23 #include "charset.h"
24 #include "gdbtypes.h"
25 #include "cp-support.h"
26 #include "demangle.h"
27 #include "objfiles.h"
28 #include "language.h"
29 #include "vec.h"
30 #include "typeprint.h"
31
32 typedef struct pyty_type_object
33 {
34   PyObject_HEAD
35   struct type *type;
36
37   /* If a Type object is associated with an objfile, it is kept on a
38      doubly-linked list, rooted in the objfile.  This lets us copy the
39      underlying struct type when the objfile is deleted.  */
40   struct pyty_type_object *prev;
41   struct pyty_type_object *next;
42 } type_object;
43
44 static PyTypeObject type_object_type
45     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");
46
47 /* A Field object.  */
48 typedef struct pyty_field_object
49 {
50   PyObject_HEAD
51
52   /* Dictionary holding our attributes.  */
53   PyObject *dict;
54 } field_object;
55
56 static PyTypeObject field_object_type
57     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");
58
59 /* A type iterator object.  */
60 typedef struct {
61   PyObject_HEAD
62   /* The current field index.  */
63   int field;
64   /* What to return.  */
65   enum gdbpy_iter_kind kind;
66   /* Pointer back to the original source type object.  */
67   struct pyty_type_object *source;
68 } typy_iterator_object;
69
70 static PyTypeObject type_iterator_object_type
71     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
72
73 /* This is used to initialize various gdb.TYPE_ constants.  */
74 struct pyty_code
75 {
76   /* The code.  */
77   enum type_code code;
78   /* The name.  */
79   const char *name;
80 };
81
82 /* Forward declarations.  */
83 static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind);
84
85 #define ENTRY(X) { X, #X }
86
87 static struct pyty_code pyty_codes[] =
88 {
89   ENTRY (TYPE_CODE_BITSTRING),
90   ENTRY (TYPE_CODE_PTR),
91   ENTRY (TYPE_CODE_ARRAY),
92   ENTRY (TYPE_CODE_STRUCT),
93   ENTRY (TYPE_CODE_UNION),
94   ENTRY (TYPE_CODE_ENUM),
95   ENTRY (TYPE_CODE_FLAGS),
96   ENTRY (TYPE_CODE_FUNC),
97   ENTRY (TYPE_CODE_INT),
98   ENTRY (TYPE_CODE_FLT),
99   ENTRY (TYPE_CODE_VOID),
100   ENTRY (TYPE_CODE_SET),
101   ENTRY (TYPE_CODE_RANGE),
102   ENTRY (TYPE_CODE_STRING),
103   ENTRY (TYPE_CODE_ERROR),
104   ENTRY (TYPE_CODE_METHOD),
105   ENTRY (TYPE_CODE_METHODPTR),
106   ENTRY (TYPE_CODE_MEMBERPTR),
107   ENTRY (TYPE_CODE_REF),
108   ENTRY (TYPE_CODE_CHAR),
109   ENTRY (TYPE_CODE_BOOL),
110   ENTRY (TYPE_CODE_COMPLEX),
111   ENTRY (TYPE_CODE_TYPEDEF),
112   ENTRY (TYPE_CODE_NAMESPACE),
113   ENTRY (TYPE_CODE_DECFLOAT),
114   ENTRY (TYPE_CODE_INTERNAL_FUNCTION),
115   { TYPE_CODE_UNDEF, NULL }
116 };
117
118 \f
119
120 static void
121 field_dealloc (PyObject *obj)
122 {
123   field_object *f = (field_object *) obj;
124
125   Py_XDECREF (f->dict);
126   Py_TYPE (obj)->tp_free (obj);
127 }
128
129 static PyObject *
130 field_new (void)
131 {
132   field_object *result = PyObject_New (field_object, &field_object_type);
133
134   if (result)
135     {
136       result->dict = PyDict_New ();
137       if (!result->dict)
138         {
139           Py_DECREF (result);
140           result = NULL;
141         }
142     }
143   return (PyObject *) result;
144 }
145
146 \f
147
148 /* Return true if OBJ is of type gdb.Field, false otherwise.  */
149
150 int
151 gdbpy_is_field (PyObject *obj)
152 {
153   return PyObject_TypeCheck (obj, &field_object_type);
154 }
155
156 /* Return the code for this type.  */
157 static PyObject *
158 typy_get_code (PyObject *self, void *closure)
159 {
160   struct type *type = ((type_object *) self)->type;
161
162   return PyInt_FromLong (TYPE_CODE (type));
163 }
164
165 /* Helper function for typy_fields which converts a single field to a
166    gdb.Field object.  Returns NULL on error.  */
167
168 static PyObject *
169 convert_field (struct type *type, int field)
170 {
171   PyObject *result = field_new ();
172   PyObject *arg;
173
174   if (!result)
175     return NULL;
176
177   arg = type_to_type_object (type);
178   if (arg == NULL)
179     goto fail;
180   if (PyObject_SetAttrString (result, "parent_type", arg) < 0)
181     goto failarg;
182   Py_DECREF (arg);
183
184   if (!field_is_static (&TYPE_FIELD (type, field)))
185     {
186       const char *attrstring;
187
188       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
189         {
190           arg = gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type, field));
191           attrstring = "enumval";
192         }
193       else
194         {
195           arg = gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type, field));
196           attrstring = "bitpos";
197         }
198
199       if (!arg)
200         goto fail;
201
202       /* At least python-2.4 had the second parameter non-const.  */
203       if (PyObject_SetAttrString (result, (char *) attrstring, arg) < 0)
204         goto failarg;
205       Py_DECREF (arg);
206     }
207
208   arg = NULL;
209   if (TYPE_FIELD_NAME (type, field))
210     {
211       const char *field_name = TYPE_FIELD_NAME (type, field);
212
213       if (field_name[0] != '\0')
214         {
215           arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
216           if (arg == NULL)
217             goto fail;
218         }
219     }
220   if (arg == NULL)
221     {
222       arg = Py_None;
223       Py_INCREF (arg);
224     }
225   if (PyObject_SetAttrString (result, "name", arg) < 0)
226     goto failarg;
227   Py_DECREF (arg);
228
229   arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
230   Py_INCREF (arg);
231   if (PyObject_SetAttrString (result, "artificial", arg) < 0)
232     goto failarg;
233   Py_DECREF (arg);
234
235   if (TYPE_CODE (type) == TYPE_CODE_CLASS)
236     arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False;
237   else
238     arg = Py_False;
239   Py_INCREF (arg);
240   if (PyObject_SetAttrString (result, "is_base_class", arg) < 0)
241     goto failarg;
242   Py_DECREF (arg);
243
244   arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
245   if (!arg)
246     goto fail;
247   if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
248     goto failarg;
249   Py_DECREF (arg);
250
251   /* A field can have a NULL type in some situations.  */
252   if (TYPE_FIELD_TYPE (type, field) == NULL)
253     {
254       arg = Py_None;
255       Py_INCREF (arg);
256     }
257   else
258     arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
259   if (!arg)
260     goto fail;
261   if (PyObject_SetAttrString (result, "type", arg) < 0)
262     goto failarg;
263   Py_DECREF (arg);
264
265   return result;
266
267  failarg:
268   Py_DECREF (arg);
269  fail:
270   Py_DECREF (result);
271   return NULL;
272 }
273
274 /* Helper function to return the name of a field, as a gdb.Field object.
275    If the field doesn't have a name, None is returned.  */
276
277 static PyObject *
278 field_name (struct type *type, int field)
279 {
280   PyObject *result;
281
282   if (TYPE_FIELD_NAME (type, field))
283     result = PyString_FromString (TYPE_FIELD_NAME (type, field));
284   else
285     {
286       result = Py_None;
287       Py_INCREF (result);
288     }
289   return result;
290 }
291
292 /* Helper function for Type standard mapping methods.  Returns a
293    Python object for field i of the type.  "kind" specifies what to
294    return: the name of the field, a gdb.Field object corresponding to
295    the field, or a tuple consisting of field name and gdb.Field
296    object.  */
297
298 static PyObject *
299 make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
300 {
301   PyObject *item = NULL, *key = NULL, *value = NULL;
302
303   switch (kind)
304     {
305     case iter_items:
306       key = field_name (type, i);
307       if (key == NULL)
308         goto fail;
309       value = convert_field (type, i);
310       if (value == NULL)
311         goto fail;
312       item = PyTuple_New (2);
313       if (item == NULL)
314         goto fail;
315       PyTuple_SET_ITEM (item, 0, key);
316       PyTuple_SET_ITEM (item, 1, value);
317       break;
318     case iter_keys:
319       item = field_name (type, i);
320       break;
321     case iter_values:
322       item =  convert_field (type, i);
323       break;
324     default:
325       gdb_assert_not_reached ("invalid gdbpy_iter_kind");
326     }
327   return item;
328
329  fail:
330   Py_XDECREF (key);
331   Py_XDECREF (value);
332   Py_XDECREF (item);
333   return NULL;
334 }
335
336 /* Return a sequence of all field names, fields, or (name, field) pairs.
337    Each field is a gdb.Field object.  */
338
339 static PyObject *
340 typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
341 {
342   PyObject *py_type = self;
343   PyObject *result = NULL, *iter = NULL;
344   volatile struct gdb_exception except;
345   struct type *type = ((type_object *) py_type)->type;
346   struct type *checked_type = type;
347
348   TRY_CATCH (except, RETURN_MASK_ALL)
349     {
350       CHECK_TYPEDEF (checked_type);
351     }
352   GDB_PY_HANDLE_EXCEPTION (except);
353
354   if (checked_type != type)
355     py_type = type_to_type_object (checked_type);
356   iter = typy_make_iter (py_type, kind);
357   if (checked_type != type)
358     {
359       /* Need to wrap this in braces because Py_DECREF isn't wrapped
360          in a do{}while(0).  */
361       Py_DECREF (py_type);
362     }
363   if (iter != NULL)
364     {
365       result = PySequence_List (iter);
366       Py_DECREF (iter);
367     }
368
369   return result;
370 }
371
372 /* Return a sequence of all fields.  Each field is a gdb.Field object.  */
373
374 static PyObject *
375 typy_values (PyObject *self, PyObject *args)
376 {
377   return typy_fields_items (self, iter_values);
378 }
379
380 /* Return a sequence of all fields.  Each field is a gdb.Field object.
381    This method is similar to typy_values, except where the supplied
382    gdb.Type is an array, in which case it returns a list of one entry
383    which is a gdb.Field object for a range (the array bounds).  */
384
385 static PyObject *
386 typy_fields (PyObject *self, PyObject *args)
387 {
388   struct type *type = ((type_object *) self)->type;
389   PyObject *r, *rl;
390
391   if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
392     return typy_fields_items (self, iter_values);
393
394   /* Array type.  Handle this as a special case because the common
395      machinery wants struct or union or enum types.  Build a list of
396      one entry which is the range for the array.  */
397   r = convert_field (type, 0);
398   if (r == NULL)
399     return NULL;
400
401   rl = Py_BuildValue ("[O]", r);
402   Py_DECREF (r);
403
404   return rl;
405 }
406
407 /* Return a sequence of all field names.  Each field is a gdb.Field object.  */
408
409 static PyObject *
410 typy_field_names (PyObject *self, PyObject *args)
411 {
412   return typy_fields_items (self, iter_keys);
413 }
414
415 /* Return a sequence of all (name, fields) pairs.  Each field is a
416    gdb.Field object.  */
417
418 static PyObject *
419 typy_items (PyObject *self, PyObject *args)
420 {
421   return typy_fields_items (self, iter_items);
422 }
423
424 /* Return the type's name, or None.  */
425
426 static PyObject *
427 typy_get_name (PyObject *self, void *closure)
428 {
429   struct type *type = ((type_object *) self)->type;
430
431   if (TYPE_NAME (type) == NULL)
432     Py_RETURN_NONE;
433   return PyString_FromString (TYPE_NAME (type));
434 }
435
436 /* Return the type's tag, or None.  */
437 static PyObject *
438 typy_get_tag (PyObject *self, void *closure)
439 {
440   struct type *type = ((type_object *) self)->type;
441
442   if (!TYPE_TAG_NAME (type))
443     Py_RETURN_NONE;
444   return PyString_FromString (TYPE_TAG_NAME (type));
445 }
446
447 /* Return the type, stripped of typedefs. */
448 static PyObject *
449 typy_strip_typedefs (PyObject *self, PyObject *args)
450 {
451   struct type *type = ((type_object *) self)->type;
452   volatile struct gdb_exception except;
453
454   TRY_CATCH (except, RETURN_MASK_ALL)
455     {
456       type = check_typedef (type);
457     }
458   GDB_PY_HANDLE_EXCEPTION (except);
459
460   return type_to_type_object (type);
461 }
462
463 /* Strip typedefs and pointers/reference from a type.  Then check that
464    it is a struct, union, or enum type.  If not, raise TypeError.  */
465
466 static struct type *
467 typy_get_composite (struct type *type)
468 {
469   volatile struct gdb_exception except;
470
471   for (;;)
472     {
473       TRY_CATCH (except, RETURN_MASK_ALL)
474         {
475           CHECK_TYPEDEF (type);
476         }
477       GDB_PY_HANDLE_EXCEPTION (except);
478
479       if (TYPE_CODE (type) != TYPE_CODE_PTR
480           && TYPE_CODE (type) != TYPE_CODE_REF)
481         break;
482       type = TYPE_TARGET_TYPE (type);
483     }
484
485   /* If this is not a struct, union, or enum type, raise TypeError
486      exception.  */
487   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
488       && TYPE_CODE (type) != TYPE_CODE_UNION
489       && TYPE_CODE (type) != TYPE_CODE_ENUM)
490     {
491       PyErr_SetString (PyExc_TypeError,
492                        "Type is not a structure, union, or enum type.");
493       return NULL;
494     }
495
496   return type;
497 }
498
499 /* Helper for typy_array and typy_vector.  */
500
501 static PyObject *
502 typy_array_1 (PyObject *self, PyObject *args, int is_vector)
503 {
504   long n1, n2;
505   PyObject *n2_obj = NULL;
506   struct type *array = NULL;
507   struct type *type = ((type_object *) self)->type;
508   volatile struct gdb_exception except;
509
510   if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
511     return NULL;
512
513   if (n2_obj)
514     {
515       if (!PyInt_Check (n2_obj))
516         {
517           PyErr_SetString (PyExc_RuntimeError,
518                            _("Array bound must be an integer"));
519           return NULL;
520         }
521
522       if (! gdb_py_int_as_long (n2_obj, &n2))
523         return NULL;
524     }
525   else
526     {
527       n2 = n1;
528       n1 = 0;
529     }
530
531   if (n2 < n1)
532     {
533       PyErr_SetString (PyExc_ValueError,
534                        _("Array length must not be negative"));
535       return NULL;
536     }
537
538   TRY_CATCH (except, RETURN_MASK_ALL)
539     {
540       array = lookup_array_range_type (type, n1, n2);
541       if (is_vector)
542         make_vector_type (array);
543     }
544   GDB_PY_HANDLE_EXCEPTION (except);
545
546   return type_to_type_object (array);
547 }
548
549 /* Return an array type.  */
550
551 static PyObject *
552 typy_array (PyObject *self, PyObject *args)
553 {
554   return typy_array_1 (self, args, 0);
555 }
556
557 /* Return a vector type.  */
558
559 static PyObject *
560 typy_vector (PyObject *self, PyObject *args)
561 {
562   return typy_array_1 (self, args, 1);
563 }
564
565 /* Return a Type object which represents a pointer to SELF.  */
566 static PyObject *
567 typy_pointer (PyObject *self, PyObject *args)
568 {
569   struct type *type = ((type_object *) self)->type;
570   volatile struct gdb_exception except;
571
572   TRY_CATCH (except, RETURN_MASK_ALL)
573     {
574       type = lookup_pointer_type (type);
575     }
576   GDB_PY_HANDLE_EXCEPTION (except);
577
578   return type_to_type_object (type);
579 }
580
581 /* Return the range of a type represented by SELF.  The return type is
582    a tuple.  The first element of the tuple contains the low bound,
583    while the second element of the tuple contains the high bound.  */
584 static PyObject *
585 typy_range (PyObject *self, PyObject *args)
586 {
587   struct type *type = ((type_object *) self)->type;
588   PyObject *result;
589   PyObject *low_bound = NULL, *high_bound = NULL;
590   /* Initialize these to appease GCC warnings.  */
591   LONGEST low = 0, high = 0;
592
593   if (TYPE_CODE (type) != TYPE_CODE_ARRAY
594       && TYPE_CODE (type) != TYPE_CODE_STRING
595       && TYPE_CODE (type) != TYPE_CODE_RANGE)
596     {
597       PyErr_SetString (PyExc_RuntimeError,
598                        _("This type does not have a range."));
599       return NULL;
600     }
601
602   switch (TYPE_CODE (type))
603     {
604     case TYPE_CODE_ARRAY:
605     case TYPE_CODE_STRING:
606       low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
607       high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
608       break;
609     case TYPE_CODE_RANGE:
610       low = TYPE_LOW_BOUND (type);
611       high = TYPE_HIGH_BOUND (type);
612       break;
613     }
614
615   low_bound = PyLong_FromLong (low);
616   if (!low_bound)
617     goto failarg;
618
619   high_bound = PyLong_FromLong (high);
620   if (!high_bound)
621     goto failarg;
622
623   result = PyTuple_New (2);
624   if (!result)
625     goto failarg;
626
627   if (PyTuple_SetItem (result, 0, low_bound) != 0)
628     {
629       Py_DECREF (result);
630       goto failarg;
631     }
632   if (PyTuple_SetItem (result, 1, high_bound) != 0)
633     {
634       Py_DECREF (high_bound);
635       Py_DECREF (result);
636       return NULL;
637     }
638   return result;
639
640  failarg:
641   Py_XDECREF (high_bound);
642   Py_XDECREF (low_bound);
643   return NULL;
644 }
645
646 /* Return a Type object which represents a reference to SELF.  */
647 static PyObject *
648 typy_reference (PyObject *self, PyObject *args)
649 {
650   struct type *type = ((type_object *) self)->type;
651   volatile struct gdb_exception except;
652
653   TRY_CATCH (except, RETURN_MASK_ALL)
654     {
655       type = lookup_reference_type (type);
656     }
657   GDB_PY_HANDLE_EXCEPTION (except);
658
659   return type_to_type_object (type);
660 }
661
662 /* Return a Type object which represents the target type of SELF.  */
663 static PyObject *
664 typy_target (PyObject *self, PyObject *args)
665 {
666   struct type *type = ((type_object *) self)->type;
667
668   if (!TYPE_TARGET_TYPE (type))
669     {
670       PyErr_SetString (PyExc_RuntimeError,
671                        _("Type does not have a target."));
672       return NULL;
673     }
674
675   return type_to_type_object (TYPE_TARGET_TYPE (type));
676 }
677
678 /* Return a const-qualified type variant.  */
679 static PyObject *
680 typy_const (PyObject *self, PyObject *args)
681 {
682   struct type *type = ((type_object *) self)->type;
683   volatile struct gdb_exception except;
684
685   TRY_CATCH (except, RETURN_MASK_ALL)
686     {
687       type = make_cv_type (1, 0, type, NULL);
688     }
689   GDB_PY_HANDLE_EXCEPTION (except);
690
691   return type_to_type_object (type);
692 }
693
694 /* Return a volatile-qualified type variant.  */
695 static PyObject *
696 typy_volatile (PyObject *self, PyObject *args)
697 {
698   struct type *type = ((type_object *) self)->type;
699   volatile struct gdb_exception except;
700
701   TRY_CATCH (except, RETURN_MASK_ALL)
702     {
703       type = make_cv_type (0, 1, type, NULL);
704     }
705   GDB_PY_HANDLE_EXCEPTION (except);
706
707   return type_to_type_object (type);
708 }
709
710 /* Return an unqualified type variant.  */
711 static PyObject *
712 typy_unqualified (PyObject *self, PyObject *args)
713 {
714   struct type *type = ((type_object *) self)->type;
715   volatile struct gdb_exception except;
716
717   TRY_CATCH (except, RETURN_MASK_ALL)
718     {
719       type = make_cv_type (0, 0, type, NULL);
720     }
721   GDB_PY_HANDLE_EXCEPTION (except);
722
723   return type_to_type_object (type);
724 }
725
726 /* Return the size of the type represented by SELF, in bytes.  */
727 static PyObject *
728 typy_get_sizeof (PyObject *self, void *closure)
729 {
730   struct type *type = ((type_object *) self)->type;
731   volatile struct gdb_exception except;
732
733   TRY_CATCH (except, RETURN_MASK_ALL)
734     {
735       check_typedef (type);
736     }
737   /* Ignore exceptions.  */
738
739   return gdb_py_long_from_longest (TYPE_LENGTH (type));
740 }
741
742 static struct type *
743 typy_lookup_typename (const char *type_name, const struct block *block)
744 {
745   struct type *type = NULL;
746   volatile struct gdb_exception except;
747
748   TRY_CATCH (except, RETURN_MASK_ALL)
749     {
750       if (!strncmp (type_name, "struct ", 7))
751         type = lookup_struct (type_name + 7, NULL);
752       else if (!strncmp (type_name, "union ", 6))
753         type = lookup_union (type_name + 6, NULL);
754       else if (!strncmp (type_name, "enum ", 5))
755         type = lookup_enum (type_name + 5, NULL);
756       else
757         type = lookup_typename (python_language, python_gdbarch,
758                                 type_name, block, 0);
759     }
760   GDB_PY_HANDLE_EXCEPTION (except);
761
762   return type;
763 }
764
765 static struct type *
766 typy_lookup_type (struct demangle_component *demangled,
767                   const struct block *block)
768 {
769   struct type *type, *rtype = NULL;
770   char *type_name = NULL;
771   enum demangle_component_type demangled_type;
772   volatile struct gdb_exception except;
773
774   /* Save the type: typy_lookup_type() may (indirectly) overwrite
775      memory pointed by demangled.  */
776   demangled_type = demangled->type;
777
778   if (demangled_type == DEMANGLE_COMPONENT_POINTER
779       || demangled_type == DEMANGLE_COMPONENT_REFERENCE
780       || demangled_type == DEMANGLE_COMPONENT_CONST
781       || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
782     {
783       type = typy_lookup_type (demangled->u.s_binary.left, block);
784       if (! type)
785         return NULL;
786
787       TRY_CATCH (except, RETURN_MASK_ALL)
788         {
789           /* If the demangled_type matches with one of the types
790              below, run the corresponding function and save the type
791              to return later.  We cannot just return here as we are in
792              an exception handler.  */
793           switch (demangled_type)
794             {
795             case DEMANGLE_COMPONENT_REFERENCE:
796               rtype =  lookup_reference_type (type);
797               break;
798             case DEMANGLE_COMPONENT_POINTER:
799               rtype = lookup_pointer_type (type);
800               break;
801             case DEMANGLE_COMPONENT_CONST:
802               rtype = make_cv_type (1, 0, type, NULL);
803               break;
804             case DEMANGLE_COMPONENT_VOLATILE:
805               rtype = make_cv_type (0, 1, type, NULL);
806               break;
807             }
808         }
809       GDB_PY_HANDLE_EXCEPTION (except);
810     }
811
812   /* If we have a type from the switch statement above, just return
813      that.  */
814   if (rtype)
815     return rtype;
816
817   /* We don't have a type, so lookup the type.  */
818   type_name = cp_comp_to_string (demangled, 10);
819   type = typy_lookup_typename (type_name, block);
820   xfree (type_name);
821
822   return type;
823 }
824
825 /* This is a helper function for typy_template_argument that is used
826    when the type does not have template symbols attached.  It works by
827    parsing the type name.  This happens with compilers, like older
828    versions of GCC, that do not emit DW_TAG_template_*.  */
829
830 static PyObject *
831 typy_legacy_template_argument (struct type *type, const struct block *block,
832                                int argno)
833 {
834   int i;
835   struct demangle_component *demangled;
836   struct demangle_parse_info *info = NULL;
837   const char *err;
838   struct type *argtype;
839   struct cleanup *cleanup;
840   volatile struct gdb_exception except;
841
842   if (TYPE_NAME (type) == NULL)
843     {
844       PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
845       return NULL;
846     }
847
848   TRY_CATCH (except, RETURN_MASK_ALL)
849     {
850       /* Note -- this is not thread-safe.  */
851       info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
852     }
853   GDB_PY_HANDLE_EXCEPTION (except);
854
855   if (! info)
856     {
857       PyErr_SetString (PyExc_RuntimeError, err);
858       return NULL;
859     }
860   demangled = info->tree;
861   cleanup = make_cleanup_cp_demangled_name_parse_free (info);
862
863   /* Strip off component names.  */
864   while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
865          || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
866     demangled = demangled->u.s_binary.right;
867
868   if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
869     {
870       do_cleanups (cleanup);
871       PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
872       return NULL;
873     }
874
875   /* Skip from the template to the arguments.  */
876   demangled = demangled->u.s_binary.right;
877
878   for (i = 0; demangled && i < argno; ++i)
879     demangled = demangled->u.s_binary.right;
880
881   if (! demangled)
882     {
883       do_cleanups (cleanup);
884       PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
885                     argno);
886       return NULL;
887     }
888
889   argtype = typy_lookup_type (demangled->u.s_binary.left, block);
890   do_cleanups (cleanup);
891   if (! argtype)
892     return NULL;
893
894   return type_to_type_object (argtype);
895 }
896
897 static PyObject *
898 typy_template_argument (PyObject *self, PyObject *args)
899 {
900   int argno;
901   struct type *type = ((type_object *) self)->type;
902   const struct block *block = NULL;
903   PyObject *block_obj = NULL;
904   struct symbol *sym;
905   struct value *val = NULL;
906   volatile struct gdb_exception except;
907
908   if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
909     return NULL;
910
911   if (block_obj)
912     {
913       block = block_object_to_block (block_obj);
914       if (! block)
915         {
916           PyErr_SetString (PyExc_RuntimeError,
917                            _("Second argument must be block."));
918           return NULL;
919         }
920     }
921
922   TRY_CATCH (except, RETURN_MASK_ALL)
923     {
924       type = check_typedef (type);
925       if (TYPE_CODE (type) == TYPE_CODE_REF)
926         type = check_typedef (TYPE_TARGET_TYPE (type));
927     }
928   GDB_PY_HANDLE_EXCEPTION (except);
929
930   /* We might not have DW_TAG_template_*, so try to parse the type's
931      name.  This is inefficient if we do not have a template type --
932      but that is going to wind up as an error anyhow.  */
933   if (! TYPE_N_TEMPLATE_ARGUMENTS (type))
934     return typy_legacy_template_argument (type, block, argno);
935
936   if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
937     {
938       PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
939                     argno);
940       return NULL;
941     }
942
943   sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
944   if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
945     return type_to_type_object (SYMBOL_TYPE (sym));
946   else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT)
947     {
948       PyErr_Format (PyExc_RuntimeError,
949                     _("Template argument is optimized out"));
950       return NULL;
951     }
952
953   TRY_CATCH (except, RETURN_MASK_ALL)
954     {
955       val = value_of_variable (sym, block);
956     }
957   GDB_PY_HANDLE_EXCEPTION (except);
958
959   return value_to_value_object (val);
960 }
961
962 static PyObject *
963 typy_str (PyObject *self)
964 {
965   volatile struct gdb_exception except;
966   char *thetype = NULL;
967   long length = 0;
968   PyObject *result;
969
970   TRY_CATCH (except, RETURN_MASK_ALL)
971     {
972       struct cleanup *old_chain;
973       struct ui_file *stb;
974
975       stb = mem_fileopen ();
976       old_chain = make_cleanup_ui_file_delete (stb);
977
978       LA_PRINT_TYPE (type_object_to_type (self), "", stb, -1, 0,
979                      &type_print_raw_options);
980
981       thetype = ui_file_xstrdup (stb, &length);
982       do_cleanups (old_chain);
983     }
984   if (except.reason < 0)
985     {
986       xfree (thetype);
987       GDB_PY_HANDLE_EXCEPTION (except);
988     }
989
990   result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
991   xfree (thetype);
992
993   return result;
994 }
995
996 /* Implement the richcompare method.  */
997
998 static PyObject *
999 typy_richcompare (PyObject *self, PyObject *other, int op)
1000 {
1001   int result = Py_NE;
1002   struct type *type1 = type_object_to_type (self);
1003   struct type *type2 = type_object_to_type (other);
1004   volatile struct gdb_exception except;
1005
1006   /* We can only compare ourselves to another Type object, and only
1007      for equality or inequality.  */
1008   if (type2 == NULL || (op != Py_EQ && op != Py_NE))
1009     {
1010       Py_INCREF (Py_NotImplemented);
1011       return Py_NotImplemented;
1012     }
1013
1014   if (type1 == type2)
1015     result = Py_EQ;
1016   else
1017     {
1018       TRY_CATCH (except, RETURN_MASK_ALL)
1019         {
1020           result = types_deeply_equal (type1, type2);
1021         }
1022       /* If there is a GDB exception, a comparison is not capable
1023          (or trusted), so exit.  */
1024       GDB_PY_HANDLE_EXCEPTION (except);
1025     }
1026
1027   if (op == (result ? Py_EQ : Py_NE))
1028     Py_RETURN_TRUE;
1029   Py_RETURN_FALSE;
1030 }
1031
1032 \f
1033
1034 static const struct objfile_data *typy_objfile_data_key;
1035
1036 static void
1037 save_objfile_types (struct objfile *objfile, void *datum)
1038 {
1039   type_object *obj = datum;
1040   htab_t copied_types;
1041   struct cleanup *cleanup;
1042
1043   if (!gdb_python_initialized)
1044     return;
1045
1046   /* This prevents another thread from freeing the objects we're
1047      operating on.  */
1048   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
1049
1050   copied_types = create_copied_types_hash (objfile);
1051
1052   while (obj)
1053     {
1054       type_object *next = obj->next;
1055
1056       htab_empty (copied_types);
1057
1058       obj->type = copy_type_recursive (objfile, obj->type, copied_types);
1059
1060       obj->next = NULL;
1061       obj->prev = NULL;
1062
1063       obj = next;
1064     }
1065
1066   htab_delete (copied_types);
1067
1068   do_cleanups (cleanup);
1069 }
1070
1071 static void
1072 set_type (type_object *obj, struct type *type)
1073 {
1074   obj->type = type;
1075   obj->prev = NULL;
1076   if (type && TYPE_OBJFILE (type))
1077     {
1078       struct objfile *objfile = TYPE_OBJFILE (type);
1079
1080       obj->next = objfile_data (objfile, typy_objfile_data_key);
1081       if (obj->next)
1082         obj->next->prev = obj;
1083       set_objfile_data (objfile, typy_objfile_data_key, obj);
1084     }
1085   else
1086     obj->next = NULL;
1087 }
1088
1089 static void
1090 typy_dealloc (PyObject *obj)
1091 {
1092   type_object *type = (type_object *) obj;
1093
1094   if (type->prev)
1095     type->prev->next = type->next;
1096   else if (type->type && TYPE_OBJFILE (type->type))
1097     {
1098       /* Must reset head of list.  */
1099       struct objfile *objfile = TYPE_OBJFILE (type->type);
1100
1101       if (objfile)
1102         set_objfile_data (objfile, typy_objfile_data_key, type->next);
1103     }
1104   if (type->next)
1105     type->next->prev = type->prev;
1106
1107   Py_TYPE (type)->tp_free (type);
1108 }
1109
1110 /* Return number of fields ("length" of the field dictionary).  */
1111
1112 static Py_ssize_t
1113 typy_length (PyObject *self)
1114 {
1115   struct type *type = ((type_object *) self)->type;
1116
1117   type = typy_get_composite (type);
1118   if (type == NULL)
1119     return -1;
1120
1121   return TYPE_NFIELDS (type);
1122 }
1123
1124 /* Implements boolean evaluation of gdb.Type.  Handle this like other
1125    Python objects that don't have a meaningful truth value -- all
1126    values are true.  */
1127
1128 static int
1129 typy_nonzero (PyObject *self)
1130 {
1131   return 1;
1132 }
1133
1134 /* Return a gdb.Field object for the field named by the argument.  */
1135
1136 static PyObject *
1137 typy_getitem (PyObject *self, PyObject *key)
1138 {
1139   struct type *type = ((type_object *) self)->type;
1140   char *field;
1141   int i;
1142
1143   field = python_string_to_host_string (key);
1144   if (field == NULL)
1145     return NULL;
1146
1147   /* We want just fields of this type, not of base types, so instead of
1148      using lookup_struct_elt_type, portions of that function are
1149      copied here.  */
1150
1151   type = typy_get_composite (type);
1152   if (type == NULL)
1153     return NULL;
1154
1155   for (i = 0; i < TYPE_NFIELDS (type); i++)
1156     {
1157       const char *t_field_name = TYPE_FIELD_NAME (type, i);
1158
1159       if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1160         {
1161           return convert_field (type, i);
1162         }
1163     }
1164   PyErr_SetObject (PyExc_KeyError, key);
1165   return NULL;
1166 }
1167
1168 /* Implement the "get" method on the type object.  This is the
1169    same as getitem if the key is present, but returns the supplied
1170    default value or None if the key is not found.  */
1171
1172 static PyObject *
1173 typy_get (PyObject *self, PyObject *args)
1174 {
1175   PyObject *key, *defval = Py_None, *result;
1176
1177   if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
1178     return NULL;
1179
1180   result = typy_getitem (self, key);
1181   if (result != NULL)
1182     return result;
1183
1184   /* typy_getitem returned error status.  If the exception is
1185      KeyError, clear the exception status and return the defval
1186      instead.  Otherwise return the exception unchanged.  */
1187   if (!PyErr_ExceptionMatches (PyExc_KeyError))
1188     return NULL;
1189
1190   PyErr_Clear ();
1191   Py_INCREF (defval);
1192   return defval;
1193 }
1194
1195 /* Implement the "has_key" method on the type object.  */
1196
1197 static PyObject *
1198 typy_has_key (PyObject *self, PyObject *args)
1199 {
1200   struct type *type = ((type_object *) self)->type;
1201   const char *field;
1202   int i;
1203
1204   if (!PyArg_ParseTuple (args, "s", &field))
1205     return NULL;
1206
1207   /* We want just fields of this type, not of base types, so instead of
1208      using lookup_struct_elt_type, portions of that function are
1209      copied here.  */
1210
1211   type = typy_get_composite (type);
1212   if (type == NULL)
1213     return NULL;
1214
1215   for (i = 0; i < TYPE_NFIELDS (type); i++)
1216     {
1217       const char *t_field_name = TYPE_FIELD_NAME (type, i);
1218
1219       if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1220         Py_RETURN_TRUE;
1221     }
1222   Py_RETURN_FALSE;
1223 }
1224
1225 /* Make an iterator object to iterate over keys, values, or items.  */
1226
1227 static PyObject *
1228 typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind)
1229 {
1230   typy_iterator_object *typy_iter_obj;
1231
1232   /* Check that "self" is a structure or union type.  */
1233   if (typy_get_composite (((type_object *) self)->type) == NULL)
1234     return NULL;
1235
1236   typy_iter_obj = PyObject_New (typy_iterator_object,
1237                                 &type_iterator_object_type);
1238   if (typy_iter_obj == NULL)
1239       return NULL;
1240
1241   typy_iter_obj->field = 0;
1242   typy_iter_obj->kind = kind;
1243   Py_INCREF (self);
1244   typy_iter_obj->source = (type_object *) self;
1245
1246   return (PyObject *) typy_iter_obj;
1247 }
1248
1249 /* iteritems() method.  */
1250
1251 static PyObject *
1252 typy_iteritems (PyObject *self, PyObject *args)
1253 {
1254   return typy_make_iter (self, iter_items);
1255 }
1256
1257 /* iterkeys() method.  */
1258
1259 static PyObject *
1260 typy_iterkeys (PyObject *self, PyObject *args)
1261 {
1262   return typy_make_iter (self, iter_keys);
1263 }
1264
1265 /* Iterating over the class, same as iterkeys except for the function
1266    signature.  */
1267
1268 static PyObject *
1269 typy_iter (PyObject *self)
1270 {
1271   return typy_make_iter (self, iter_keys);
1272 }
1273
1274 /* itervalues() method.  */
1275
1276 static PyObject *
1277 typy_itervalues (PyObject *self, PyObject *args)
1278 {
1279   return typy_make_iter (self, iter_values);
1280 }
1281
1282 /* Return a reference to the type iterator.  */
1283
1284 static PyObject *
1285 typy_iterator_iter (PyObject *self)
1286 {
1287   Py_INCREF (self);
1288   return self;
1289 }
1290
1291 /* Return the next field in the iteration through the list of fields
1292    of the type.  */
1293
1294 static PyObject *
1295 typy_iterator_iternext (PyObject *self)
1296 {
1297   typy_iterator_object *iter_obj = (typy_iterator_object *) self;
1298   struct type *type = iter_obj->source->type;
1299   PyObject *result;
1300
1301   if (iter_obj->field < TYPE_NFIELDS (type))
1302     {
1303       result = make_fielditem (type, iter_obj->field, iter_obj->kind);
1304       if (result != NULL)
1305         iter_obj->field++;
1306       return result;
1307     }
1308
1309   return NULL;
1310 }
1311
1312 static void
1313 typy_iterator_dealloc (PyObject *obj)
1314 {
1315   typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
1316
1317   Py_DECREF (iter_obj->source);
1318 }
1319
1320 /* Create a new Type referring to TYPE.  */
1321 PyObject *
1322 type_to_type_object (struct type *type)
1323 {
1324   type_object *type_obj;
1325
1326   type_obj = PyObject_New (type_object, &type_object_type);
1327   if (type_obj)
1328     set_type (type_obj, type);
1329
1330   return (PyObject *) type_obj;
1331 }
1332
1333 struct type *
1334 type_object_to_type (PyObject *obj)
1335 {
1336   if (! PyObject_TypeCheck (obj, &type_object_type))
1337     return NULL;
1338   return ((type_object *) obj)->type;
1339 }
1340
1341 \f
1342
1343 /* Implementation of gdb.lookup_type.  */
1344 PyObject *
1345 gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
1346 {
1347   static char *keywords[] = { "name", "block", NULL };
1348   const char *type_name = NULL;
1349   struct type *type = NULL;
1350   PyObject *block_obj = NULL;
1351   const struct block *block = NULL;
1352
1353   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1354                                      &type_name, &block_obj))
1355     return NULL;
1356
1357   if (block_obj)
1358     {
1359       block = block_object_to_block (block_obj);
1360       if (! block)
1361         {
1362           PyErr_SetString (PyExc_RuntimeError,
1363                            _("'block' argument must be a Block."));
1364           return NULL;
1365         }
1366     }
1367
1368   type = typy_lookup_typename (type_name, block);
1369   if (! type)
1370     return NULL;
1371
1372   return (PyObject *) type_to_type_object (type);
1373 }
1374
1375 int
1376 gdbpy_initialize_types (void)
1377 {
1378   int i;
1379
1380   typy_objfile_data_key
1381     = register_objfile_data_with_cleanup (save_objfile_types, NULL);
1382
1383   if (PyType_Ready (&type_object_type) < 0)
1384     return -1;
1385   if (PyType_Ready (&field_object_type) < 0)
1386     return -1;
1387   if (PyType_Ready (&type_iterator_object_type) < 0)
1388     return -1;
1389
1390   for (i = 0; pyty_codes[i].name; ++i)
1391     {
1392       if (PyModule_AddIntConstant (gdb_module,
1393                                    /* Cast needed for Python 2.4.  */
1394                                    (char *) pyty_codes[i].name,
1395                                    pyty_codes[i].code) < 0)
1396         return -1;
1397     }
1398
1399   if (gdb_pymodule_addobject (gdb_module, "Type",
1400                               (PyObject *) &type_object_type) < 0)
1401     return -1;
1402
1403   if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
1404                               (PyObject *) &type_iterator_object_type) < 0)
1405     return -1;
1406
1407   return gdb_pymodule_addobject (gdb_module, "Field",
1408                                  (PyObject *) &field_object_type);
1409 }
1410
1411 \f
1412
1413 static PyGetSetDef type_object_getset[] =
1414 {
1415   { "code", typy_get_code, NULL,
1416     "The code for this type.", NULL },
1417   { "name", typy_get_name, NULL,
1418     "The name for this type, or None.", NULL },
1419   { "sizeof", typy_get_sizeof, NULL,
1420     "The size of this type, in bytes.", NULL },
1421   { "tag", typy_get_tag, NULL,
1422     "The tag name for this type, or None.", NULL },
1423   { NULL }
1424 };
1425
1426 static PyMethodDef type_object_methods[] =
1427 {
1428   { "array", typy_array, METH_VARARGS,
1429     "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1430 Return a type which represents an array of objects of this type.\n\
1431 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1432 If LOW_BOUND is omitted, a value of zero is used." },
1433   { "vector", typy_vector, METH_VARARGS,
1434     "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1435 Return a type which represents a vector of objects of this type.\n\
1436 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1437 If LOW_BOUND is omitted, a value of zero is used.\n\
1438 Vectors differ from arrays in that if the current language has C-style\n\
1439 arrays, vectors don't decay to a pointer to the first element.\n\
1440 They are first class values." },
1441    { "__contains__", typy_has_key, METH_VARARGS,
1442      "T.__contains__(k) -> True if T has a field named k, else False" },
1443   { "const", typy_const, METH_NOARGS,
1444     "const () -> Type\n\
1445 Return a const variant of this type." },
1446   { "fields", typy_fields, METH_NOARGS,
1447     "fields () -> list\n\
1448 Return a list holding all the fields of this type.\n\
1449 Each field is a gdb.Field object." },
1450   { "get", typy_get, METH_VARARGS,
1451     "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1452 otherwise returns default, if supplied, or None if not." },
1453   { "has_key", typy_has_key, METH_VARARGS,
1454     "T.has_key(k) -> True if T has a field named k, else False" },
1455   { "items", typy_items, METH_NOARGS,
1456     "items () -> list\n\
1457 Return a list of (name, field) pairs of this type.\n\
1458 Each field is a gdb.Field object." },
1459   { "iteritems", typy_iteritems, METH_NOARGS,
1460     "iteritems () -> an iterator over the (name, field)\n\
1461 pairs of this type.  Each field is a gdb.Field object." },
1462   { "iterkeys", typy_iterkeys, METH_NOARGS,
1463     "iterkeys () -> an iterator over the field names of this type." },
1464   { "itervalues", typy_itervalues, METH_NOARGS,
1465     "itervalues () -> an iterator over the fields of this type.\n\
1466 Each field is a gdb.Field object." },
1467   { "keys", typy_field_names, METH_NOARGS,
1468     "keys () -> list\n\
1469 Return a list holding all the fields names of this type." },
1470   { "pointer", typy_pointer, METH_NOARGS,
1471     "pointer () -> Type\n\
1472 Return a type of pointer to this type." },
1473   { "range", typy_range, METH_NOARGS,
1474     "range () -> tuple\n\
1475 Return a tuple containing the lower and upper range for this type."},
1476   { "reference", typy_reference, METH_NOARGS,
1477     "reference () -> Type\n\
1478 Return a type of reference to this type." },
1479   { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1480     "strip_typedefs () -> Type\n\
1481 Return a type formed by stripping this type of all typedefs."},
1482   { "target", typy_target, METH_NOARGS,
1483     "target () -> Type\n\
1484 Return the target type of this type." },
1485   { "template_argument", typy_template_argument, METH_VARARGS,
1486     "template_argument (arg, [block]) -> Type\n\
1487 Return the type of a template argument." },
1488   { "unqualified", typy_unqualified, METH_NOARGS,
1489     "unqualified () -> Type\n\
1490 Return a variant of this type without const or volatile attributes." },
1491   { "values", typy_values, METH_NOARGS,
1492     "values () -> list\n\
1493 Return a list holding all the fields of this type.\n\
1494 Each field is a gdb.Field object." },
1495   { "volatile", typy_volatile, METH_NOARGS,
1496     "volatile () -> Type\n\
1497 Return a volatile variant of this type" },
1498   { NULL }
1499 };
1500
1501 static PyNumberMethods type_object_as_number = {
1502   NULL,                       /* nb_add */
1503   NULL,                       /* nb_subtract */
1504   NULL,                       /* nb_multiply */
1505 #ifndef IS_PY3K
1506   NULL,                       /* nb_divide */
1507 #endif
1508   NULL,                       /* nb_remainder */
1509   NULL,                       /* nb_divmod */
1510   NULL,                       /* nb_power */
1511   NULL,                       /* nb_negative */
1512   NULL,                       /* nb_positive */
1513   NULL,                       /* nb_absolute */
1514   typy_nonzero,               /* nb_nonzero */
1515   NULL,                       /* nb_invert */
1516   NULL,                       /* nb_lshift */
1517   NULL,                       /* nb_rshift */
1518   NULL,                       /* nb_and */
1519   NULL,                       /* nb_xor */
1520   NULL,                       /* nb_or */
1521 #ifdef IS_PY3K
1522   NULL,                       /* nb_int */
1523   NULL,                       /* reserved */
1524 #else
1525   NULL,                       /* nb_coerce */
1526   NULL,                       /* nb_int */
1527   NULL,                       /* nb_long */
1528 #endif
1529   NULL,                       /* nb_float */
1530 #ifndef IS_PY3K
1531   NULL,                       /* nb_oct */
1532   NULL                        /* nb_hex */
1533 #endif
1534 };
1535
1536 static PyMappingMethods typy_mapping = {
1537   typy_length,
1538   typy_getitem,
1539   NULL                            /* no "set" method */
1540 };
1541
1542 static PyTypeObject type_object_type =
1543 {
1544   PyVarObject_HEAD_INIT (NULL, 0)
1545   "gdb.Type",                     /*tp_name*/
1546   sizeof (type_object),           /*tp_basicsize*/
1547   0,                              /*tp_itemsize*/
1548   typy_dealloc,                   /*tp_dealloc*/
1549   0,                              /*tp_print*/
1550   0,                              /*tp_getattr*/
1551   0,                              /*tp_setattr*/
1552   0,                              /*tp_compare*/
1553   0,                              /*tp_repr*/
1554   &type_object_as_number,         /*tp_as_number*/
1555   0,                              /*tp_as_sequence*/
1556   &typy_mapping,                  /*tp_as_mapping*/
1557   0,                              /*tp_hash */
1558   0,                              /*tp_call*/
1559   typy_str,                       /*tp_str*/
1560   0,                              /*tp_getattro*/
1561   0,                              /*tp_setattro*/
1562   0,                              /*tp_as_buffer*/
1563   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /*tp_flags*/
1564   "GDB type object",              /* tp_doc */
1565   0,                              /* tp_traverse */
1566   0,                              /* tp_clear */
1567   typy_richcompare,               /* tp_richcompare */
1568   0,                              /* tp_weaklistoffset */
1569   typy_iter,                      /* tp_iter */
1570   0,                              /* tp_iternext */
1571   type_object_methods,            /* tp_methods */
1572   0,                              /* tp_members */
1573   type_object_getset,             /* tp_getset */
1574   0,                              /* tp_base */
1575   0,                              /* tp_dict */
1576   0,                              /* tp_descr_get */
1577   0,                              /* tp_descr_set */
1578   0,                              /* tp_dictoffset */
1579   0,                              /* tp_init */
1580   0,                              /* tp_alloc */
1581   0,                              /* tp_new */
1582 };
1583
1584 static PyGetSetDef field_object_getset[] =
1585 {
1586   { "__dict__", gdb_py_generic_dict, NULL,
1587     "The __dict__ for this field.", &field_object_type },
1588   { NULL }
1589 };
1590
1591 static PyTypeObject field_object_type =
1592 {
1593   PyVarObject_HEAD_INIT (NULL, 0)
1594   "gdb.Field",                    /*tp_name*/
1595   sizeof (field_object),          /*tp_basicsize*/
1596   0,                              /*tp_itemsize*/
1597   field_dealloc,                  /*tp_dealloc*/
1598   0,                              /*tp_print*/
1599   0,                              /*tp_getattr*/
1600   0,                              /*tp_setattr*/
1601   0,                              /*tp_compare*/
1602   0,                              /*tp_repr*/
1603   0,                              /*tp_as_number*/
1604   0,                              /*tp_as_sequence*/
1605   0,                              /*tp_as_mapping*/
1606   0,                              /*tp_hash */
1607   0,                              /*tp_call*/
1608   0,                              /*tp_str*/
1609   0,                              /*tp_getattro*/
1610   0,                              /*tp_setattro*/
1611   0,                              /*tp_as_buffer*/
1612   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /*tp_flags*/
1613   "GDB field object",             /* tp_doc */
1614   0,                              /* tp_traverse */
1615   0,                              /* tp_clear */
1616   0,                              /* tp_richcompare */
1617   0,                              /* tp_weaklistoffset */
1618   0,                              /* tp_iter */
1619   0,                              /* tp_iternext */
1620   0,                              /* tp_methods */
1621   0,                              /* tp_members */
1622   field_object_getset,            /* tp_getset */
1623   0,                              /* tp_base */
1624   0,                              /* tp_dict */
1625   0,                              /* tp_descr_get */
1626   0,                              /* tp_descr_set */
1627   offsetof (field_object, dict),  /* tp_dictoffset */
1628   0,                              /* tp_init */
1629   0,                              /* tp_alloc */
1630   0,                              /* tp_new */
1631 };
1632
1633 static PyTypeObject type_iterator_object_type = {
1634   PyVarObject_HEAD_INIT (NULL, 0)
1635   "gdb.TypeIterator",             /*tp_name*/
1636   sizeof (typy_iterator_object),  /*tp_basicsize*/
1637   0,                              /*tp_itemsize*/
1638   typy_iterator_dealloc,          /*tp_dealloc*/
1639   0,                              /*tp_print*/
1640   0,                              /*tp_getattr*/
1641   0,                              /*tp_setattr*/
1642   0,                              /*tp_compare*/
1643   0,                              /*tp_repr*/
1644   0,                              /*tp_as_number*/
1645   0,                              /*tp_as_sequence*/
1646   0,                              /*tp_as_mapping*/
1647   0,                              /*tp_hash */
1648   0,                              /*tp_call*/
1649   0,                              /*tp_str*/
1650   0,                              /*tp_getattro*/
1651   0,                              /*tp_setattro*/
1652   0,                              /*tp_as_buffer*/
1653   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /*tp_flags*/
1654   "GDB type iterator object",     /*tp_doc */
1655   0,                              /*tp_traverse */
1656   0,                              /*tp_clear */
1657   0,                              /*tp_richcompare */
1658   0,                              /*tp_weaklistoffset */
1659   typy_iterator_iter,             /*tp_iter */
1660   typy_iterator_iternext,         /*tp_iternext */
1661   0                               /*tp_methods */
1662 };