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