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