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