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