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