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