* python/py-type.c (typy_get_composite): New function.
[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_values (PyObject *self, PyObject *args)
331 {
332 return typy_fields_items (self, iter_values);
333 }
334
335 /* Return a sequence of all fields. Each field is a gdb.Field object.
336 This method is similar to typy_values, except where the supplied
337 gdb.Type is an array, in which case it returns a list of one entry
338 which is a gdb.Field object for a range (the array bounds). */
339
340 static PyObject *
341 typy_fields (PyObject *self, PyObject *args)
342 {
343 struct type *type = ((type_object *) self)->type;
344 PyObject *r, *rl;
345
346 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
347 return typy_fields_items (self, iter_values);
348
349 /* Array type. Handle this as a special case because the common
350 machinery wants struct or union or enum types. Build a list of
351 one entry which is the range for the array. */
352 r = convert_field (type, 0);
353 if (r == NULL)
354 return NULL;
355
356 rl = Py_BuildValue ("[O]", r);
357 if (rl == NULL)
358 {
359 Py_DECREF (r);
360 }
361
362 return rl;
363 }
364
365 /* Return a sequence of all field names. Each field is a gdb.Field object. */
366
367 static PyObject *
368 typy_field_names (PyObject *self, PyObject *args)
369 {
370 return typy_fields_items (self, iter_keys);
371 }
372
373 /* Return a sequence of all (name, fields) pairs. Each field is a
374 gdb.Field object. */
375
376 static PyObject *
377 typy_items (PyObject *self, PyObject *args)
378 {
379 return typy_fields_items (self, iter_items);
380 }
381
382 /* Return the type's tag, or None. */
383 static PyObject *
384 typy_get_tag (PyObject *self, void *closure)
385 {
386 struct type *type = ((type_object *) self)->type;
387
388 if (!TYPE_TAG_NAME (type))
389 Py_RETURN_NONE;
390 return PyString_FromString (TYPE_TAG_NAME (type));
391 }
392
393 /* Return the type, stripped of typedefs. */
394 static PyObject *
395 typy_strip_typedefs (PyObject *self, PyObject *args)
396 {
397 struct type *type = ((type_object *) self)->type;
398
399 return type_to_type_object (check_typedef (type));
400 }
401
402 /* Strip typedefs and pointers/reference from a type. Then check that
403 it is a struct, union, or enum type. If not, raise TypeError. */
404
405 static struct type *
406 typy_get_composite (struct type *type)
407 {
408 volatile struct gdb_exception except;
409
410 for (;;)
411 {
412 TRY_CATCH (except, RETURN_MASK_ALL)
413 {
414 CHECK_TYPEDEF (type);
415 }
416 /* Don't use GDB_PY_HANDLE_EXCEPTION here because that returns
417 a (NULL) pointer of the wrong type. */
418 if (except.reason < 0)
419 {
420 gdbpy_convert_exception (except);
421 return NULL;
422 }
423
424 if (TYPE_CODE (type) != TYPE_CODE_PTR
425 && TYPE_CODE (type) != TYPE_CODE_REF)
426 break;
427 type = TYPE_TARGET_TYPE (type);
428 }
429
430 /* If this is not a struct, union, or enum type, raise TypeError
431 exception. */
432 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
433 && TYPE_CODE (type) != TYPE_CODE_UNION
434 && TYPE_CODE (type) != TYPE_CODE_ENUM)
435 {
436 PyErr_SetString (PyExc_TypeError,
437 "Type is not a structure, union, or enum type.");
438 return NULL;
439 }
440
441 return type;
442 }
443
444 /* Return an array type. */
445
446 static PyObject *
447 typy_array (PyObject *self, PyObject *args)
448 {
449 long n1, n2;
450 PyObject *n2_obj = NULL;
451 struct type *array = NULL;
452 struct type *type = ((type_object *) self)->type;
453 volatile struct gdb_exception except;
454
455 if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
456 return NULL;
457
458 if (n2_obj)
459 {
460 if (!PyInt_Check (n2_obj))
461 {
462 PyErr_SetString (PyExc_RuntimeError,
463 _("Array bound must be an integer"));
464 return NULL;
465 }
466
467 if (! gdb_py_int_as_long (n2_obj, &n2))
468 return NULL;
469 }
470 else
471 {
472 n2 = n1;
473 n1 = 0;
474 }
475
476 if (n2 < n1)
477 {
478 PyErr_SetString (PyExc_ValueError,
479 _("Array length must not be negative"));
480 return NULL;
481 }
482
483 TRY_CATCH (except, RETURN_MASK_ALL)
484 {
485 array = lookup_array_range_type (type, n1, n2);
486 }
487 GDB_PY_HANDLE_EXCEPTION (except);
488
489 return type_to_type_object (array);
490 }
491
492 /* Return a Type object which represents a pointer to SELF. */
493 static PyObject *
494 typy_pointer (PyObject *self, PyObject *args)
495 {
496 struct type *type = ((type_object *) self)->type;
497 volatile struct gdb_exception except;
498
499 TRY_CATCH (except, RETURN_MASK_ALL)
500 {
501 type = lookup_pointer_type (type);
502 }
503 GDB_PY_HANDLE_EXCEPTION (except);
504
505 return type_to_type_object (type);
506 }
507
508 /* Return the range of a type represented by SELF. The return type is
509 a tuple. The first element of the tuple contains the low bound,
510 while the second element of the tuple contains the high bound. */
511 static PyObject *
512 typy_range (PyObject *self, PyObject *args)
513 {
514 struct type *type = ((type_object *) self)->type;
515 PyObject *result;
516 PyObject *low_bound = NULL, *high_bound = NULL;
517 /* Initialize these to appease GCC warnings. */
518 LONGEST low = 0, high = 0;
519
520 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
521 && TYPE_CODE (type) != TYPE_CODE_STRING
522 && TYPE_CODE (type) != TYPE_CODE_RANGE)
523 {
524 PyErr_SetString (PyExc_RuntimeError,
525 _("This type does not have a range."));
526 return NULL;
527 }
528
529 switch (TYPE_CODE (type))
530 {
531 case TYPE_CODE_ARRAY:
532 case TYPE_CODE_STRING:
533 low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
534 high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
535 break;
536 case TYPE_CODE_RANGE:
537 low = TYPE_LOW_BOUND (type);
538 high = TYPE_HIGH_BOUND (type);
539 break;
540 }
541
542 low_bound = PyLong_FromLong (low);
543 if (!low_bound)
544 goto failarg;
545
546 high_bound = PyLong_FromLong (high);
547 if (!high_bound)
548 goto failarg;
549
550 result = PyTuple_New (2);
551 if (!result)
552 goto failarg;
553
554 if (PyTuple_SetItem (result, 0, low_bound) != 0)
555 {
556 Py_DECREF (result);
557 goto failarg;
558 }
559 if (PyTuple_SetItem (result, 1, high_bound) != 0)
560 {
561 Py_DECREF (high_bound);
562 Py_DECREF (result);
563 return NULL;
564 }
565 return result;
566
567 failarg:
568 Py_XDECREF (high_bound);
569 Py_XDECREF (low_bound);
570 return NULL;
571 }
572
573 /* Return a Type object which represents a reference to SELF. */
574 static PyObject *
575 typy_reference (PyObject *self, PyObject *args)
576 {
577 struct type *type = ((type_object *) self)->type;
578 volatile struct gdb_exception except;
579
580 TRY_CATCH (except, RETURN_MASK_ALL)
581 {
582 type = lookup_reference_type (type);
583 }
584 GDB_PY_HANDLE_EXCEPTION (except);
585
586 return type_to_type_object (type);
587 }
588
589 /* Return a Type object which represents the target type of SELF. */
590 static PyObject *
591 typy_target (PyObject *self, PyObject *args)
592 {
593 struct type *type = ((type_object *) self)->type;
594
595 if (!TYPE_TARGET_TYPE (type))
596 {
597 PyErr_SetString (PyExc_RuntimeError,
598 _("Type does not have a target."));
599 return NULL;
600 }
601
602 return type_to_type_object (TYPE_TARGET_TYPE (type));
603 }
604
605 /* Return a const-qualified type variant. */
606 static PyObject *
607 typy_const (PyObject *self, PyObject *args)
608 {
609 struct type *type = ((type_object *) self)->type;
610 volatile struct gdb_exception except;
611
612 TRY_CATCH (except, RETURN_MASK_ALL)
613 {
614 type = make_cv_type (1, 0, type, NULL);
615 }
616 GDB_PY_HANDLE_EXCEPTION (except);
617
618 return type_to_type_object (type);
619 }
620
621 /* Return a volatile-qualified type variant. */
622 static PyObject *
623 typy_volatile (PyObject *self, PyObject *args)
624 {
625 struct type *type = ((type_object *) self)->type;
626 volatile struct gdb_exception except;
627
628 TRY_CATCH (except, RETURN_MASK_ALL)
629 {
630 type = make_cv_type (0, 1, type, NULL);
631 }
632 GDB_PY_HANDLE_EXCEPTION (except);
633
634 return type_to_type_object (type);
635 }
636
637 /* Return an unqualified type variant. */
638 static PyObject *
639 typy_unqualified (PyObject *self, PyObject *args)
640 {
641 struct type *type = ((type_object *) self)->type;
642 volatile struct gdb_exception except;
643
644 TRY_CATCH (except, RETURN_MASK_ALL)
645 {
646 type = make_cv_type (0, 0, type, NULL);
647 }
648 GDB_PY_HANDLE_EXCEPTION (except);
649
650 return type_to_type_object (type);
651 }
652
653 /* Return the size of the type represented by SELF, in bytes. */
654 static PyObject *
655 typy_get_sizeof (PyObject *self, void *closure)
656 {
657 struct type *type = ((type_object *) self)->type;
658 volatile struct gdb_exception except;
659
660 TRY_CATCH (except, RETURN_MASK_ALL)
661 {
662 check_typedef (type);
663 }
664 /* Ignore exceptions. */
665
666 return PyLong_FromLong (TYPE_LENGTH (type));
667 }
668
669 static struct type *
670 typy_lookup_typename (const char *type_name, const struct block *block)
671 {
672 struct type *type = NULL;
673 volatile struct gdb_exception except;
674
675 TRY_CATCH (except, RETURN_MASK_ALL)
676 {
677 if (!strncmp (type_name, "struct ", 7))
678 type = lookup_struct (type_name + 7, NULL);
679 else if (!strncmp (type_name, "union ", 6))
680 type = lookup_union (type_name + 6, NULL);
681 else if (!strncmp (type_name, "enum ", 5))
682 type = lookup_enum (type_name + 5, NULL);
683 else
684 type = lookup_typename (python_language, python_gdbarch,
685 type_name, block, 0);
686 }
687 if (except.reason < 0)
688 {
689 gdbpy_convert_exception (except);
690 return NULL;
691 }
692
693 return type;
694 }
695
696 static struct type *
697 typy_lookup_type (struct demangle_component *demangled,
698 const struct block *block)
699 {
700 struct type *type, *rtype = NULL;
701 char *type_name = NULL;
702 enum demangle_component_type demangled_type;
703 volatile struct gdb_exception except;
704
705 /* Save the type: typy_lookup_type() may (indirectly) overwrite
706 memory pointed by demangled. */
707 demangled_type = demangled->type;
708
709 if (demangled_type == DEMANGLE_COMPONENT_POINTER
710 || demangled_type == DEMANGLE_COMPONENT_REFERENCE
711 || demangled_type == DEMANGLE_COMPONENT_CONST
712 || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
713 {
714 type = typy_lookup_type (demangled->u.s_binary.left, block);
715 if (! type)
716 return NULL;
717
718 TRY_CATCH (except, RETURN_MASK_ALL)
719 {
720 /* If the demangled_type matches with one of the types
721 below, run the corresponding function and save the type
722 to return later. We cannot just return here as we are in
723 an exception handler. */
724 switch (demangled_type)
725 {
726 case DEMANGLE_COMPONENT_REFERENCE:
727 rtype = lookup_reference_type (type);
728 break;
729 case DEMANGLE_COMPONENT_POINTER:
730 rtype = lookup_pointer_type (type);
731 break;
732 case DEMANGLE_COMPONENT_CONST:
733 rtype = make_cv_type (1, 0, type, NULL);
734 break;
735 case DEMANGLE_COMPONENT_VOLATILE:
736 rtype = make_cv_type (0, 1, type, NULL);
737 break;
738 }
739 }
740 if (except.reason < 0)
741 {
742 gdbpy_convert_exception (except);
743 return NULL;
744 }
745 }
746
747 /* If we have a type from the switch statement above, just return
748 that. */
749 if (rtype)
750 return rtype;
751
752 /* We don't have a type, so lookup the type. */
753 type_name = cp_comp_to_string (demangled, 10);
754 type = typy_lookup_typename (type_name, block);
755 xfree (type_name);
756
757 return type;
758 }
759
760 /* This is a helper function for typy_template_argument that is used
761 when the type does not have template symbols attached. It works by
762 parsing the type name. This happens with compilers, like older
763 versions of GCC, that do not emit DW_TAG_template_*. */
764
765 static PyObject *
766 typy_legacy_template_argument (struct type *type, const struct block *block,
767 int argno)
768 {
769 int i;
770 struct demangle_component *demangled;
771 struct demangle_parse_info *info;
772 const char *err;
773 struct type *argtype;
774 struct cleanup *cleanup;
775
776 if (TYPE_NAME (type) == NULL)
777 {
778 PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
779 return NULL;
780 }
781
782 /* Note -- this is not thread-safe. */
783 info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
784 if (! info)
785 {
786 PyErr_SetString (PyExc_RuntimeError, err);
787 return NULL;
788 }
789 demangled = info->tree;
790 cleanup = make_cleanup_cp_demangled_name_parse_free (info);
791
792 /* Strip off component names. */
793 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
794 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
795 demangled = demangled->u.s_binary.right;
796
797 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
798 {
799 do_cleanups (cleanup);
800 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
801 return NULL;
802 }
803
804 /* Skip from the template to the arguments. */
805 demangled = demangled->u.s_binary.right;
806
807 for (i = 0; demangled && i < argno; ++i)
808 demangled = demangled->u.s_binary.right;
809
810 if (! demangled)
811 {
812 do_cleanups (cleanup);
813 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
814 argno);
815 return NULL;
816 }
817
818 argtype = typy_lookup_type (demangled->u.s_binary.left, block);
819 do_cleanups (cleanup);
820 if (! argtype)
821 return NULL;
822
823 return type_to_type_object (argtype);
824 }
825
826 static PyObject *
827 typy_template_argument (PyObject *self, PyObject *args)
828 {
829 int argno;
830 struct type *type = ((type_object *) self)->type;
831 const struct block *block = NULL;
832 PyObject *block_obj = NULL;
833 struct symbol *sym;
834 struct value *val = NULL;
835 volatile struct gdb_exception except;
836
837 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
838 return NULL;
839
840 if (block_obj)
841 {
842 block = block_object_to_block (block_obj);
843 if (! block)
844 {
845 PyErr_SetString (PyExc_RuntimeError,
846 _("Second argument must be block."));
847 return NULL;
848 }
849 }
850
851 TRY_CATCH (except, RETURN_MASK_ALL)
852 {
853 type = check_typedef (type);
854 if (TYPE_CODE (type) == TYPE_CODE_REF)
855 type = check_typedef (TYPE_TARGET_TYPE (type));
856 }
857 GDB_PY_HANDLE_EXCEPTION (except);
858
859 /* We might not have DW_TAG_template_*, so try to parse the type's
860 name. This is inefficient if we do not have a template type --
861 but that is going to wind up as an error anyhow. */
862 if (! TYPE_N_TEMPLATE_ARGUMENTS (type))
863 return typy_legacy_template_argument (type, block, argno);
864
865 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
866 {
867 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
868 argno);
869 return NULL;
870 }
871
872 sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
873 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
874 return type_to_type_object (SYMBOL_TYPE (sym));
875 else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT)
876 {
877 PyErr_Format (PyExc_RuntimeError,
878 _("Template argument is optimized out"));
879 return NULL;
880 }
881
882 TRY_CATCH (except, RETURN_MASK_ALL)
883 {
884 val = value_of_variable (sym, block);
885 }
886 GDB_PY_HANDLE_EXCEPTION (except);
887
888 return value_to_value_object (val);
889 }
890
891 static PyObject *
892 typy_str (PyObject *self)
893 {
894 volatile struct gdb_exception except;
895 char *thetype = NULL;
896 long length = 0;
897 PyObject *result;
898
899 TRY_CATCH (except, RETURN_MASK_ALL)
900 {
901 struct cleanup *old_chain;
902 struct ui_file *stb;
903
904 stb = mem_fileopen ();
905 old_chain = make_cleanup_ui_file_delete (stb);
906
907 type_print (type_object_to_type (self), "", stb, -1);
908
909 thetype = ui_file_xstrdup (stb, &length);
910 do_cleanups (old_chain);
911 }
912 if (except.reason < 0)
913 {
914 xfree (thetype);
915 GDB_PY_HANDLE_EXCEPTION (except);
916 }
917
918 result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
919 xfree (thetype);
920
921 return result;
922 }
923
924 /* An entry in the type-equality bcache. */
925
926 typedef struct type_equality_entry
927 {
928 struct type *type1, *type2;
929 } type_equality_entry_d;
930
931 DEF_VEC_O (type_equality_entry_d);
932
933 /* A helper function to compare two strings. Returns 1 if they are
934 the same, 0 otherwise. Handles NULLs properly. */
935
936 static int
937 compare_strings (const char *s, const char *t)
938 {
939 if (s == NULL && t != NULL)
940 return 0;
941 else if (s != NULL && t == NULL)
942 return 0;
943 else if (s == NULL && t== NULL)
944 return 1;
945 return strcmp (s, t) == 0;
946 }
947
948 /* A helper function for typy_richcompare that checks two types for
949 "deep" equality. Returns Py_EQ if the types are considered the
950 same, Py_NE otherwise. */
951
952 static int
953 check_types_equal (struct type *type1, struct type *type2,
954 VEC (type_equality_entry_d) **worklist)
955 {
956 CHECK_TYPEDEF (type1);
957 CHECK_TYPEDEF (type2);
958
959 if (type1 == type2)
960 return Py_EQ;
961
962 if (TYPE_CODE (type1) != TYPE_CODE (type2)
963 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
964 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
965 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
966 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
967 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
968 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
969 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
970 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
971 return Py_NE;
972
973 if (!compare_strings (TYPE_TAG_NAME (type1), TYPE_TAG_NAME (type2)))
974 return Py_NE;
975 if (!compare_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
976 return Py_NE;
977
978 if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
979 {
980 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
981 sizeof (*TYPE_RANGE_DATA (type1))) != 0)
982 return Py_NE;
983 }
984 else
985 {
986 int i;
987
988 for (i = 0; i < TYPE_NFIELDS (type1); ++i)
989 {
990 const struct field *field1 = &TYPE_FIELD (type1, i);
991 const struct field *field2 = &TYPE_FIELD (type2, i);
992 struct type_equality_entry entry;
993
994 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
995 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
996 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
997 return Py_NE;
998 if (!compare_strings (FIELD_NAME (*field1), FIELD_NAME (*field2)))
999 return Py_NE;
1000 switch (FIELD_LOC_KIND (*field1))
1001 {
1002 case FIELD_LOC_KIND_BITPOS:
1003 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
1004 return Py_NE;
1005 break;
1006 case FIELD_LOC_KIND_PHYSADDR:
1007 if (FIELD_STATIC_PHYSADDR (*field1)
1008 != FIELD_STATIC_PHYSADDR (*field2))
1009 return Py_NE;
1010 break;
1011 case FIELD_LOC_KIND_PHYSNAME:
1012 if (!compare_strings (FIELD_STATIC_PHYSNAME (*field1),
1013 FIELD_STATIC_PHYSNAME (*field2)))
1014 return Py_NE;
1015 break;
1016 case FIELD_LOC_KIND_DWARF_BLOCK:
1017 {
1018 struct dwarf2_locexpr_baton *block1, *block2;
1019
1020 block1 = FIELD_DWARF_BLOCK (*field1);
1021 block2 = FIELD_DWARF_BLOCK (*field2);
1022 if (block1->per_cu != block2->per_cu
1023 || block1->size != block2->size
1024 || memcmp (block1->data, block2->data, block1->size) != 0)
1025 return Py_NE;
1026 }
1027 break;
1028 default:
1029 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
1030 "%d by check_types_equal"),
1031 FIELD_LOC_KIND (*field1));
1032 }
1033
1034 entry.type1 = FIELD_TYPE (*field1);
1035 entry.type2 = FIELD_TYPE (*field2);
1036 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
1037 }
1038 }
1039
1040 if (TYPE_TARGET_TYPE (type1) != NULL)
1041 {
1042 struct type_equality_entry entry;
1043 int added;
1044
1045 if (TYPE_TARGET_TYPE (type2) == NULL)
1046 return Py_NE;
1047
1048 entry.type1 = TYPE_TARGET_TYPE (type1);
1049 entry.type2 = TYPE_TARGET_TYPE (type2);
1050 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
1051 }
1052 else if (TYPE_TARGET_TYPE (type2) != NULL)
1053 return Py_NE;
1054
1055 return Py_EQ;
1056 }
1057
1058 /* Check types on a worklist for equality. Returns Py_NE if any pair
1059 is not equal, Py_EQ if they are all considered equal. */
1060
1061 static int
1062 check_types_worklist (VEC (type_equality_entry_d) **worklist,
1063 struct bcache *cache)
1064 {
1065 while (!VEC_empty (type_equality_entry_d, *worklist))
1066 {
1067 struct type_equality_entry entry;
1068 int added;
1069
1070 entry = *VEC_last (type_equality_entry_d, *worklist);
1071 VEC_pop (type_equality_entry_d, *worklist);
1072
1073 /* If the type pair has already been visited, we know it is
1074 ok. */
1075 bcache_full (&entry, sizeof (entry), cache, &added);
1076 if (!added)
1077 continue;
1078
1079 if (check_types_equal (entry.type1, entry.type2, worklist) == Py_NE)
1080 return Py_NE;
1081 }
1082
1083 return Py_EQ;
1084 }
1085
1086 /* Implement the richcompare method. */
1087
1088 static PyObject *
1089 typy_richcompare (PyObject *self, PyObject *other, int op)
1090 {
1091 int result = Py_NE;
1092 struct type *type1 = type_object_to_type (self);
1093 struct type *type2 = type_object_to_type (other);
1094 volatile struct gdb_exception except;
1095
1096 /* We can only compare ourselves to another Type object, and only
1097 for equality or inequality. */
1098 if (type2 == NULL || (op != Py_EQ && op != Py_NE))
1099 {
1100 Py_INCREF (Py_NotImplemented);
1101 return Py_NotImplemented;
1102 }
1103
1104 if (type1 == type2)
1105 result = Py_EQ;
1106 else
1107 {
1108 struct bcache *cache;
1109 VEC (type_equality_entry_d) *worklist = NULL;
1110 struct type_equality_entry entry;
1111
1112 cache = bcache_xmalloc (NULL, NULL);
1113
1114 entry.type1 = type1;
1115 entry.type2 = type2;
1116 VEC_safe_push (type_equality_entry_d, worklist, &entry);
1117
1118 TRY_CATCH (except, RETURN_MASK_ALL)
1119 {
1120 result = check_types_worklist (&worklist, cache);
1121 }
1122 /* check_types_worklist calls several nested Python helper
1123 functions, some of which can raise a GDB Exception, so we
1124 just check and convert here. If there is a GDB exception, a
1125 comparison is not capable (or trusted), so exit. */
1126 bcache_xfree (cache);
1127 VEC_free (type_equality_entry_d, worklist);
1128 GDB_PY_HANDLE_EXCEPTION (except);
1129 }
1130
1131 if (op == result)
1132 Py_RETURN_TRUE;
1133 Py_RETURN_FALSE;
1134 }
1135
1136 \f
1137
1138 static const struct objfile_data *typy_objfile_data_key;
1139
1140 static void
1141 save_objfile_types (struct objfile *objfile, void *datum)
1142 {
1143 type_object *obj = datum;
1144 htab_t copied_types;
1145 struct cleanup *cleanup;
1146
1147 /* This prevents another thread from freeing the objects we're
1148 operating on. */
1149 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
1150
1151 copied_types = create_copied_types_hash (objfile);
1152
1153 while (obj)
1154 {
1155 type_object *next = obj->next;
1156
1157 htab_empty (copied_types);
1158
1159 obj->type = copy_type_recursive (objfile, obj->type, copied_types);
1160
1161 obj->next = NULL;
1162 obj->prev = NULL;
1163
1164 obj = next;
1165 }
1166
1167 htab_delete (copied_types);
1168
1169 do_cleanups (cleanup);
1170 }
1171
1172 static void
1173 set_type (type_object *obj, struct type *type)
1174 {
1175 obj->type = type;
1176 obj->prev = NULL;
1177 if (type && TYPE_OBJFILE (type))
1178 {
1179 struct objfile *objfile = TYPE_OBJFILE (type);
1180
1181 obj->next = objfile_data (objfile, typy_objfile_data_key);
1182 if (obj->next)
1183 obj->next->prev = obj;
1184 set_objfile_data (objfile, typy_objfile_data_key, obj);
1185 }
1186 else
1187 obj->next = NULL;
1188 }
1189
1190 static void
1191 typy_dealloc (PyObject *obj)
1192 {
1193 type_object *type = (type_object *) obj;
1194
1195 if (type->prev)
1196 type->prev->next = type->next;
1197 else if (type->type && TYPE_OBJFILE (type->type))
1198 {
1199 /* Must reset head of list. */
1200 struct objfile *objfile = TYPE_OBJFILE (type->type);
1201
1202 if (objfile)
1203 set_objfile_data (objfile, typy_objfile_data_key, type->next);
1204 }
1205 if (type->next)
1206 type->next->prev = type->prev;
1207
1208 type->ob_type->tp_free (type);
1209 }
1210
1211 /* Return number of fields ("length" of the field dictionary). */
1212
1213 static Py_ssize_t
1214 typy_length (PyObject *self)
1215 {
1216 struct type *type = ((type_object *) self)->type;
1217
1218 type = typy_get_composite (type);
1219 if (type == NULL)
1220 return -1;
1221
1222 return TYPE_NFIELDS (type);
1223 }
1224
1225 /* Implements boolean evaluation of gdb.Type. Handle this like other
1226 Python objects that don't have a meaningful truth value -- all
1227 values are true. */
1228
1229 static int
1230 typy_nonzero (PyObject *self)
1231 {
1232 return 1;
1233 }
1234
1235 /* Return a gdb.Field object for the field named by the argument. */
1236
1237 static PyObject *
1238 typy_getitem (PyObject *self, PyObject *key)
1239 {
1240 struct type *type = ((type_object *) self)->type;
1241 char *field;
1242 int i;
1243 volatile struct gdb_exception except;
1244
1245 field = python_string_to_host_string (key);
1246 if (field == NULL)
1247 return NULL;
1248
1249 /* We want just fields of this type, not of base types, so instead of
1250 using lookup_struct_elt_type, portions of that function are
1251 copied here. */
1252
1253 type = typy_get_composite (type);
1254 if (type == NULL)
1255 return NULL;
1256
1257 for (i = 0; i < TYPE_NFIELDS (type); i++)
1258 {
1259 char *t_field_name = TYPE_FIELD_NAME (type, i);
1260
1261 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1262 {
1263 return convert_field (type, i);
1264 }
1265 }
1266 PyErr_SetObject (PyExc_KeyError, key);
1267 return NULL;
1268 }
1269
1270 /* Implement the "get" method on the type object. This is the
1271 same as getitem if the key is present, but returns the supplied
1272 default value or None if the key is not found. */
1273
1274 static PyObject *
1275 typy_get (PyObject *self, PyObject *args)
1276 {
1277 PyObject *key, *defval = Py_None, *result;
1278
1279 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
1280 return NULL;
1281
1282 result = typy_getitem (self, key);
1283 if (result != NULL)
1284 return result;
1285
1286 /* typy_getitem returned error status. If the exception is
1287 KeyError, clear the exception status and return the defval
1288 instead. Otherwise return the exception unchanged. */
1289 if (!PyErr_ExceptionMatches (PyExc_KeyError))
1290 return NULL;
1291
1292 PyErr_Clear ();
1293 Py_INCREF (defval);
1294 return defval;
1295 }
1296
1297 /* Implement the "has_key" method on the type object. */
1298
1299 static PyObject *
1300 typy_has_key (PyObject *self, PyObject *args)
1301 {
1302 struct type *type = ((type_object *) self)->type;
1303 const char *field;
1304 int i;
1305 volatile struct gdb_exception except;
1306
1307 if (!PyArg_ParseTuple (args, "s", &field))
1308 return NULL;
1309
1310 /* We want just fields of this type, not of base types, so instead of
1311 using lookup_struct_elt_type, portions of that function are
1312 copied here. */
1313
1314 type = typy_get_composite (type);
1315 if (type == NULL)
1316 return NULL;
1317
1318 for (i = 0; i < TYPE_NFIELDS (type); i++)
1319 {
1320 char *t_field_name = TYPE_FIELD_NAME (type, i);
1321
1322 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1323 Py_RETURN_TRUE;
1324 }
1325 Py_RETURN_FALSE;
1326 }
1327
1328 /* Make an iterator object to iterate over keys, values, or items. */
1329
1330 static PyObject *
1331 typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind)
1332 {
1333 typy_iterator_object *typy_iter_obj;
1334
1335 /* Check that "self" is a structure or union type. */
1336 if (typy_get_composite (((type_object *) self)->type) == NULL)
1337 return NULL;
1338
1339 typy_iter_obj = PyObject_New (typy_iterator_object,
1340 &type_iterator_object_type);
1341 if (typy_iter_obj == NULL)
1342 return NULL;
1343
1344 typy_iter_obj->field = 0;
1345 typy_iter_obj->kind = kind;
1346 Py_INCREF (self);
1347 typy_iter_obj->source = (type_object *) self;
1348
1349 return (PyObject *) typy_iter_obj;
1350 }
1351
1352 /* iteritems() method. */
1353
1354 static PyObject *
1355 typy_iteritems (PyObject *self, PyObject *args)
1356 {
1357 return typy_make_iter (self, iter_items);
1358 }
1359
1360 /* iterkeys() method. */
1361
1362 static PyObject *
1363 typy_iterkeys (PyObject *self, PyObject *args)
1364 {
1365 return typy_make_iter (self, iter_keys);
1366 }
1367
1368 /* Iterating over the class, same as iterkeys except for the function
1369 signature. */
1370
1371 static PyObject *
1372 typy_iter (PyObject *self)
1373 {
1374 return typy_make_iter (self, iter_keys);
1375 }
1376
1377 /* itervalues() method. */
1378
1379 static PyObject *
1380 typy_itervalues (PyObject *self, PyObject *args)
1381 {
1382 return typy_make_iter (self, iter_values);
1383 }
1384
1385 /* Return a reference to the type iterator. */
1386
1387 static PyObject *
1388 typy_iterator_iter (PyObject *self)
1389 {
1390 Py_INCREF (self);
1391 return self;
1392 }
1393
1394 /* Return the next field in the iteration through the list of fields
1395 of the type. */
1396
1397 static PyObject *
1398 typy_iterator_iternext (PyObject *self)
1399 {
1400 typy_iterator_object *iter_obj = (typy_iterator_object *) self;
1401 struct type *type = iter_obj->source->type;
1402 int i;
1403 PyObject *result;
1404
1405 if (iter_obj->field < TYPE_NFIELDS (type))
1406 {
1407 result = make_fielditem (type, iter_obj->field, iter_obj->kind);
1408 if (result != NULL)
1409 iter_obj->field++;
1410 return result;
1411 }
1412
1413 return NULL;
1414 }
1415
1416 static void
1417 typy_iterator_dealloc (PyObject *obj)
1418 {
1419 typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
1420
1421 Py_DECREF (iter_obj->source);
1422 }
1423
1424 /* Create a new Type referring to TYPE. */
1425 PyObject *
1426 type_to_type_object (struct type *type)
1427 {
1428 type_object *type_obj;
1429
1430 type_obj = PyObject_New (type_object, &type_object_type);
1431 if (type_obj)
1432 set_type (type_obj, type);
1433
1434 return (PyObject *) type_obj;
1435 }
1436
1437 struct type *
1438 type_object_to_type (PyObject *obj)
1439 {
1440 if (! PyObject_TypeCheck (obj, &type_object_type))
1441 return NULL;
1442 return ((type_object *) obj)->type;
1443 }
1444
1445 \f
1446
1447 /* Implementation of gdb.lookup_type. */
1448 PyObject *
1449 gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
1450 {
1451 static char *keywords[] = { "name", "block", NULL };
1452 const char *type_name = NULL;
1453 struct type *type = NULL;
1454 PyObject *block_obj = NULL;
1455 const struct block *block = NULL;
1456
1457 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1458 &type_name, &block_obj))
1459 return NULL;
1460
1461 if (block_obj)
1462 {
1463 block = block_object_to_block (block_obj);
1464 if (! block)
1465 {
1466 PyErr_SetString (PyExc_RuntimeError,
1467 _("'block' argument must be a Block."));
1468 return NULL;
1469 }
1470 }
1471
1472 type = typy_lookup_typename (type_name, block);
1473 if (! type)
1474 return NULL;
1475
1476 return (PyObject *) type_to_type_object (type);
1477 }
1478
1479 void
1480 gdbpy_initialize_types (void)
1481 {
1482 int i;
1483
1484 typy_objfile_data_key
1485 = register_objfile_data_with_cleanup (save_objfile_types, NULL);
1486
1487 if (PyType_Ready (&type_object_type) < 0)
1488 return;
1489 if (PyType_Ready (&field_object_type) < 0)
1490 return;
1491 if (PyType_Ready (&type_iterator_object_type) < 0)
1492 return;
1493
1494 for (i = 0; pyty_codes[i].name; ++i)
1495 {
1496 if (PyModule_AddIntConstant (gdb_module,
1497 /* Cast needed for Python 2.4. */
1498 (char *) pyty_codes[i].name,
1499 pyty_codes[i].code) < 0)
1500 return;
1501 }
1502
1503 Py_INCREF (&type_object_type);
1504 PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type);
1505
1506 Py_INCREF (&type_iterator_object_type);
1507 PyModule_AddObject (gdb_module, "TypeIterator",
1508 (PyObject *) &type_iterator_object_type);
1509
1510 Py_INCREF (&field_object_type);
1511 PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type);
1512 }
1513
1514 \f
1515
1516 static PyGetSetDef type_object_getset[] =
1517 {
1518 { "code", typy_get_code, NULL,
1519 "The code for this type.", NULL },
1520 { "sizeof", typy_get_sizeof, NULL,
1521 "The size of this type, in bytes.", NULL },
1522 { "tag", typy_get_tag, NULL,
1523 "The tag name for this type, or None.", NULL },
1524 { NULL }
1525 };
1526
1527 static PyMethodDef type_object_methods[] =
1528 {
1529 { "array", typy_array, METH_VARARGS,
1530 "array (N) -> Type\n\
1531 Return a type which represents an array of N objects of this type." },
1532 { "__contains__", typy_has_key, METH_VARARGS,
1533 "T.__contains__(k) -> True if T has a field named k, else False" },
1534 { "const", typy_const, METH_NOARGS,
1535 "const () -> Type\n\
1536 Return a const variant of this type." },
1537 { "fields", typy_fields, METH_NOARGS,
1538 "fields () -> list\n\
1539 Return a list holding all the fields of this type.\n\
1540 Each field is a gdb.Field object." },
1541 { "get", typy_get, METH_VARARGS,
1542 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1543 otherwise returns default, if supplied, or None if not." },
1544 { "has_key", typy_has_key, METH_VARARGS,
1545 "T.has_key(k) -> True if T has a field named k, else False" },
1546 { "items", typy_items, METH_NOARGS,
1547 "items () -> list\n\
1548 Return a list of (name, field) pairs of this type.\n\
1549 Each field is a gdb.Field object." },
1550 { "iteritems", typy_iteritems, METH_NOARGS,
1551 "iteritems () -> an iterator over the (name, field)\n\
1552 pairs of this type. Each field is a gdb.Field object." },
1553 { "iterkeys", typy_iterkeys, METH_NOARGS,
1554 "iterkeys () -> an iterator over the field names of this type." },
1555 { "itervalues", typy_itervalues, METH_NOARGS,
1556 "itervalues () -> an iterator over the fields of this type.\n\
1557 Each field is a gdb.Field object." },
1558 { "keys", typy_field_names, METH_NOARGS,
1559 "keys () -> list\n\
1560 Return a list holding all the fields names of this type." },
1561 { "pointer", typy_pointer, METH_NOARGS,
1562 "pointer () -> Type\n\
1563 Return a type of pointer to this type." },
1564 { "range", typy_range, METH_NOARGS,
1565 "range () -> tuple\n\
1566 Return a tuple containing the lower and upper range for this type."},
1567 { "reference", typy_reference, METH_NOARGS,
1568 "reference () -> Type\n\
1569 Return a type of reference to this type." },
1570 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1571 "strip_typedefs () -> Type\n\
1572 Return a type formed by stripping this type of all typedefs."},
1573 { "target", typy_target, METH_NOARGS,
1574 "target () -> Type\n\
1575 Return the target type of this type." },
1576 { "template_argument", typy_template_argument, METH_VARARGS,
1577 "template_argument (arg, [block]) -> Type\n\
1578 Return the type of a template argument." },
1579 { "unqualified", typy_unqualified, METH_NOARGS,
1580 "unqualified () -> Type\n\
1581 Return a variant of this type without const or volatile attributes." },
1582 { "values", typy_values, METH_NOARGS,
1583 "values () -> list\n\
1584 Return a list holding all the fields of this type.\n\
1585 Each field is a gdb.Field object." },
1586 { "volatile", typy_volatile, METH_NOARGS,
1587 "volatile () -> Type\n\
1588 Return a volatile variant of this type" },
1589 { NULL }
1590 };
1591
1592 static PyNumberMethods type_object_as_number = {
1593 NULL, /* nb_add */
1594 NULL, /* nb_subtract */
1595 NULL, /* nb_multiply */
1596 NULL, /* nb_divide */
1597 NULL, /* nb_remainder */
1598 NULL, /* nb_divmod */
1599 NULL, /* nb_power */
1600 NULL, /* nb_negative */
1601 NULL, /* nb_positive */
1602 NULL, /* nb_absolute */
1603 typy_nonzero, /* nb_nonzero */
1604 NULL, /* nb_invert */
1605 NULL, /* nb_lshift */
1606 NULL, /* nb_rshift */
1607 NULL, /* nb_and */
1608 NULL, /* nb_xor */
1609 NULL, /* nb_or */
1610 NULL, /* nb_coerce */
1611 NULL, /* nb_int */
1612 NULL, /* nb_long */
1613 NULL, /* nb_float */
1614 NULL, /* nb_oct */
1615 NULL /* nb_hex */
1616 };
1617
1618 static PyMappingMethods typy_mapping = {
1619 typy_length,
1620 typy_getitem,
1621 NULL /* no "set" method */
1622 };
1623
1624 static PyTypeObject type_object_type =
1625 {
1626 PyObject_HEAD_INIT (NULL)
1627 0, /*ob_size*/
1628 "gdb.Type", /*tp_name*/
1629 sizeof (type_object), /*tp_basicsize*/
1630 0, /*tp_itemsize*/
1631 typy_dealloc, /*tp_dealloc*/
1632 0, /*tp_print*/
1633 0, /*tp_getattr*/
1634 0, /*tp_setattr*/
1635 0, /*tp_compare*/
1636 0, /*tp_repr*/
1637 &type_object_as_number, /*tp_as_number*/
1638 0, /*tp_as_sequence*/
1639 &typy_mapping, /*tp_as_mapping*/
1640 0, /*tp_hash */
1641 0, /*tp_call*/
1642 typy_str, /*tp_str*/
1643 0, /*tp_getattro*/
1644 0, /*tp_setattro*/
1645 0, /*tp_as_buffer*/
1646 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1647 "GDB type object", /* tp_doc */
1648 0, /* tp_traverse */
1649 0, /* tp_clear */
1650 typy_richcompare, /* tp_richcompare */
1651 0, /* tp_weaklistoffset */
1652 typy_iter, /* tp_iter */
1653 0, /* tp_iternext */
1654 type_object_methods, /* tp_methods */
1655 0, /* tp_members */
1656 type_object_getset, /* tp_getset */
1657 0, /* tp_base */
1658 0, /* tp_dict */
1659 0, /* tp_descr_get */
1660 0, /* tp_descr_set */
1661 0, /* tp_dictoffset */
1662 0, /* tp_init */
1663 0, /* tp_alloc */
1664 0, /* tp_new */
1665 };
1666
1667 static PyTypeObject field_object_type =
1668 {
1669 PyObject_HEAD_INIT (NULL)
1670 0, /*ob_size*/
1671 "gdb.Field", /*tp_name*/
1672 sizeof (field_object), /*tp_basicsize*/
1673 0, /*tp_itemsize*/
1674 field_dealloc, /*tp_dealloc*/
1675 0, /*tp_print*/
1676 0, /*tp_getattr*/
1677 0, /*tp_setattr*/
1678 0, /*tp_compare*/
1679 0, /*tp_repr*/
1680 0, /*tp_as_number*/
1681 0, /*tp_as_sequence*/
1682 0, /*tp_as_mapping*/
1683 0, /*tp_hash */
1684 0, /*tp_call*/
1685 0, /*tp_str*/
1686 0, /*tp_getattro*/
1687 0, /*tp_setattro*/
1688 0, /*tp_as_buffer*/
1689 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1690 "GDB field object", /* tp_doc */
1691 0, /* tp_traverse */
1692 0, /* tp_clear */
1693 0, /* tp_richcompare */
1694 0, /* tp_weaklistoffset */
1695 0, /* tp_iter */
1696 0, /* tp_iternext */
1697 0, /* tp_methods */
1698 0, /* tp_members */
1699 0, /* tp_getset */
1700 0, /* tp_base */
1701 0, /* tp_dict */
1702 0, /* tp_descr_get */
1703 0, /* tp_descr_set */
1704 offsetof (field_object, dict), /* tp_dictoffset */
1705 0, /* tp_init */
1706 0, /* tp_alloc */
1707 0, /* tp_new */
1708 };
1709
1710 static PyTypeObject type_iterator_object_type = {
1711 PyObject_HEAD_INIT (NULL)
1712 0, /*ob_size*/
1713 "gdb.TypeIterator", /*tp_name*/
1714 sizeof (typy_iterator_object), /*tp_basicsize*/
1715 0, /*tp_itemsize*/
1716 typy_iterator_dealloc, /*tp_dealloc*/
1717 0, /*tp_print*/
1718 0, /*tp_getattr*/
1719 0, /*tp_setattr*/
1720 0, /*tp_compare*/
1721 0, /*tp_repr*/
1722 0, /*tp_as_number*/
1723 0, /*tp_as_sequence*/
1724 0, /*tp_as_mapping*/
1725 0, /*tp_hash */
1726 0, /*tp_call*/
1727 0, /*tp_str*/
1728 0, /*tp_getattro*/
1729 0, /*tp_setattro*/
1730 0, /*tp_as_buffer*/
1731 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1732 "GDB type iterator object", /*tp_doc */
1733 0, /*tp_traverse */
1734 0, /*tp_clear */
1735 0, /*tp_richcompare */
1736 0, /*tp_weaklistoffset */
1737 typy_iterator_iter, /*tp_iter */
1738 typy_iterator_iternext, /*tp_iternext */
1739 0 /*tp_methods */
1740 };