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