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