1 /* Python interface to types.
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "exceptions.h"
23 #include "python-internal.h"
26 #include "cp-support.h"
32 #include "dwarf2loc.h"
34 typedef struct pyty_type_object
39 /* If a Type object is associated with an objfile, it is kept on a
40 doubly-linked list, rooted in the objfile. This lets us copy the
41 underlying struct type when the objfile is deleted. */
42 struct pyty_type_object
*prev
;
43 struct pyty_type_object
*next
;
46 static PyTypeObject type_object_type
;
49 typedef struct pyty_field_object
53 /* Dictionary holding our attributes. */
57 static PyTypeObject field_object_type
;
59 /* A type iterator object. */
62 /* The current field index. */
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
;
70 static PyTypeObject type_iterator_object_type
;
72 /* This is used to initialize various gdb.TYPE_ constants. */
81 /* Forward declarations. */
82 static PyObject
*typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
);
84 #define ENTRY(X) { X, #X }
86 static struct pyty_code pyty_codes
[] =
88 ENTRY (TYPE_CODE_PTR
),
89 ENTRY (TYPE_CODE_ARRAY
),
90 ENTRY (TYPE_CODE_STRUCT
),
91 ENTRY (TYPE_CODE_UNION
),
92 ENTRY (TYPE_CODE_ENUM
),
93 ENTRY (TYPE_CODE_FLAGS
),
94 ENTRY (TYPE_CODE_FUNC
),
95 ENTRY (TYPE_CODE_INT
),
96 ENTRY (TYPE_CODE_FLT
),
97 ENTRY (TYPE_CODE_VOID
),
98 ENTRY (TYPE_CODE_SET
),
99 ENTRY (TYPE_CODE_RANGE
),
100 ENTRY (TYPE_CODE_STRING
),
101 ENTRY (TYPE_CODE_BITSTRING
),
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_CHAR
),
108 ENTRY (TYPE_CODE_BOOL
),
109 ENTRY (TYPE_CODE_COMPLEX
),
110 ENTRY (TYPE_CODE_TYPEDEF
),
111 ENTRY (TYPE_CODE_NAMESPACE
),
112 ENTRY (TYPE_CODE_DECFLOAT
),
113 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
114 { TYPE_CODE_UNDEF
, NULL
}
120 field_dealloc (PyObject
*obj
)
122 field_object
*f
= (field_object
*) obj
;
124 Py_XDECREF (f
->dict
);
125 f
->ob_type
->tp_free (obj
);
131 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
135 result
->dict
= PyDict_New ();
142 return (PyObject
*) result
;
147 /* Return the code for this type. */
149 typy_get_code (PyObject
*self
, void *closure
)
151 struct type
*type
= ((type_object
*) self
)->type
;
153 return PyInt_FromLong (TYPE_CODE (type
));
156 /* Helper function for typy_fields which converts a single field to a
157 gdb.Field object. Returns NULL on error. */
160 convert_field (struct type
*type
, int field
)
162 PyObject
*result
= field_new ();
168 if (!field_is_static (&TYPE_FIELD (type
, field
)))
170 arg
= PyLong_FromLong (TYPE_FIELD_BITPOS (type
, field
));
174 if (PyObject_SetAttrString (result
, "bitpos", arg
) < 0)
178 if (TYPE_FIELD_NAME (type
, field
))
179 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
187 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
190 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
192 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
195 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
196 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
200 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
203 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
206 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
209 /* A field can have a NULL type in some situations. */
210 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
216 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
219 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
231 /* Helper function to return the name of a field, as a gdb.Field object.
232 If the field doesn't have a name, None is returned. */
235 field_name (struct type
*type
, int field
)
239 if (TYPE_FIELD_NAME (type
, field
))
240 result
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
249 /* Helper function for Type standard mapping methods. Returns a
250 Python object for field i of the type. "kind" specifies what to
251 return: the name of the field, a gdb.Field object corresponding to
252 the field, or a tuple consisting of field name and gdb.Field
256 make_fielditem (struct type
*type
, int i
, enum gdbpy_iter_kind kind
)
258 PyObject
*item
= NULL
, *key
= NULL
, *value
= NULL
;
263 key
= field_name (type
, i
);
266 value
= convert_field (type
, i
);
269 item
= PyTuple_New (2);
272 PyTuple_SET_ITEM (item
, 0, key
);
273 PyTuple_SET_ITEM (item
, 1, value
);
276 item
= field_name (type
, i
);
279 item
= convert_field (type
, i
);
291 /* Return a sequence of all field names, fields, or (name, field) pairs.
292 Each field is a gdb.Field object. */
295 typy_fields_items (PyObject
*self
, enum gdbpy_iter_kind kind
)
297 PyObject
*result
= NULL
, *iter
= NULL
;
299 iter
= typy_make_iter (self
, kind
);
303 result
= PySequence_List (iter
);
308 /* Return a sequence of all fields. Each field is a gdb.Field object. */
311 typy_fields (PyObject
*self
, PyObject
*args
)
313 return typy_fields_items (self
, iter_values
);
316 /* Return a sequence of all field names. Each field is a gdb.Field object. */
319 typy_field_names (PyObject
*self
, PyObject
*args
)
321 return typy_fields_items (self
, iter_keys
);
324 /* Return a sequence of all (name, fields) pairs. Each field is a
328 typy_items (PyObject
*self
, PyObject
*args
)
330 return typy_fields_items (self
, iter_items
);
333 /* Return the type's tag, or None. */
335 typy_get_tag (PyObject
*self
, void *closure
)
337 struct type
*type
= ((type_object
*) self
)->type
;
339 if (!TYPE_TAG_NAME (type
))
341 return PyString_FromString (TYPE_TAG_NAME (type
));
344 /* Return the type, stripped of typedefs. */
346 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
348 struct type
*type
= ((type_object
*) self
)->type
;
350 return type_to_type_object (check_typedef (type
));
353 /* Return an array type. */
356 typy_array (PyObject
*self
, PyObject
*args
)
359 PyObject
*n2_obj
= NULL
;
360 struct type
*array
= NULL
;
361 struct type
*type
= ((type_object
*) self
)->type
;
362 volatile struct gdb_exception except
;
364 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
369 if (!PyInt_Check (n2_obj
))
371 PyErr_SetString (PyExc_RuntimeError
,
372 _("Array bound must be an integer"));
376 if (! gdb_py_int_as_long (n2_obj
, &n2
))
387 PyErr_SetString (PyExc_ValueError
,
388 _("Array length must not be negative"));
392 TRY_CATCH (except
, RETURN_MASK_ALL
)
394 array
= lookup_array_range_type (type
, n1
, n2
);
396 GDB_PY_HANDLE_EXCEPTION (except
);
398 return type_to_type_object (array
);
401 /* Return a Type object which represents a pointer to SELF. */
403 typy_pointer (PyObject
*self
, PyObject
*args
)
405 struct type
*type
= ((type_object
*) self
)->type
;
406 volatile struct gdb_exception except
;
408 TRY_CATCH (except
, RETURN_MASK_ALL
)
410 type
= lookup_pointer_type (type
);
412 GDB_PY_HANDLE_EXCEPTION (except
);
414 return type_to_type_object (type
);
417 /* Return the range of a type represented by SELF. The return type is
418 a tuple. The first element of the tuple contains the low bound,
419 while the second element of the tuple contains the high bound. */
421 typy_range (PyObject
*self
, PyObject
*args
)
423 struct type
*type
= ((type_object
*) self
)->type
;
425 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
426 /* Initialize these to appease GCC warnings. */
427 LONGEST low
= 0, high
= 0;
429 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
430 && TYPE_CODE (type
) != TYPE_CODE_STRING
431 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
433 PyErr_SetString (PyExc_RuntimeError
,
434 _("This type does not have a range."));
438 switch (TYPE_CODE (type
))
440 case TYPE_CODE_ARRAY
:
441 case TYPE_CODE_STRING
:
442 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
443 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
445 case TYPE_CODE_RANGE
:
446 low
= TYPE_LOW_BOUND (type
);
447 high
= TYPE_HIGH_BOUND (type
);
451 low_bound
= PyLong_FromLong (low
);
455 high_bound
= PyLong_FromLong (high
);
459 result
= PyTuple_New (2);
463 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
468 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
470 Py_DECREF (high_bound
);
477 Py_XDECREF (high_bound
);
478 Py_XDECREF (low_bound
);
482 /* Return a Type object which represents a reference to SELF. */
484 typy_reference (PyObject
*self
, PyObject
*args
)
486 struct type
*type
= ((type_object
*) self
)->type
;
487 volatile struct gdb_exception except
;
489 TRY_CATCH (except
, RETURN_MASK_ALL
)
491 type
= lookup_reference_type (type
);
493 GDB_PY_HANDLE_EXCEPTION (except
);
495 return type_to_type_object (type
);
498 /* Return a Type object which represents the target type of SELF. */
500 typy_target (PyObject
*self
, PyObject
*args
)
502 struct type
*type
= ((type_object
*) self
)->type
;
504 if (!TYPE_TARGET_TYPE (type
))
506 PyErr_SetString (PyExc_RuntimeError
,
507 _("Type does not have a target."));
511 return type_to_type_object (TYPE_TARGET_TYPE (type
));
514 /* Return a const-qualified type variant. */
516 typy_const (PyObject
*self
, PyObject
*args
)
518 struct type
*type
= ((type_object
*) self
)->type
;
519 volatile struct gdb_exception except
;
521 TRY_CATCH (except
, RETURN_MASK_ALL
)
523 type
= make_cv_type (1, 0, type
, NULL
);
525 GDB_PY_HANDLE_EXCEPTION (except
);
527 return type_to_type_object (type
);
530 /* Return a volatile-qualified type variant. */
532 typy_volatile (PyObject
*self
, PyObject
*args
)
534 struct type
*type
= ((type_object
*) self
)->type
;
535 volatile struct gdb_exception except
;
537 TRY_CATCH (except
, RETURN_MASK_ALL
)
539 type
= make_cv_type (0, 1, type
, NULL
);
541 GDB_PY_HANDLE_EXCEPTION (except
);
543 return type_to_type_object (type
);
546 /* Return an unqualified type variant. */
548 typy_unqualified (PyObject
*self
, PyObject
*args
)
550 struct type
*type
= ((type_object
*) self
)->type
;
551 volatile struct gdb_exception except
;
553 TRY_CATCH (except
, RETURN_MASK_ALL
)
555 type
= make_cv_type (0, 0, type
, NULL
);
557 GDB_PY_HANDLE_EXCEPTION (except
);
559 return type_to_type_object (type
);
562 /* Return the size of the type represented by SELF, in bytes. */
564 typy_get_sizeof (PyObject
*self
, void *closure
)
566 struct type
*type
= ((type_object
*) self
)->type
;
567 volatile struct gdb_exception except
;
569 TRY_CATCH (except
, RETURN_MASK_ALL
)
571 check_typedef (type
);
573 /* Ignore exceptions. */
575 return PyLong_FromLong (TYPE_LENGTH (type
));
579 typy_lookup_typename (const char *type_name
, const struct block
*block
)
581 struct type
*type
= NULL
;
582 volatile struct gdb_exception except
;
584 TRY_CATCH (except
, RETURN_MASK_ALL
)
586 if (!strncmp (type_name
, "struct ", 7))
587 type
= lookup_struct (type_name
+ 7, NULL
);
588 else if (!strncmp (type_name
, "union ", 6))
589 type
= lookup_union (type_name
+ 6, NULL
);
590 else if (!strncmp (type_name
, "enum ", 5))
591 type
= lookup_enum (type_name
+ 5, NULL
);
593 type
= lookup_typename (python_language
, python_gdbarch
,
594 type_name
, block
, 0);
596 if (except
.reason
< 0)
598 gdbpy_convert_exception (except
);
606 typy_lookup_type (struct demangle_component
*demangled
,
607 const struct block
*block
)
610 char *type_name
= NULL
;
611 enum demangle_component_type demangled_type
;
612 volatile struct gdb_exception except
;
614 /* Save the type: typy_lookup_type() may (indirectly) overwrite
615 memory pointed by demangled. */
616 demangled_type
= demangled
->type
;
618 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
619 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
620 || demangled_type
== DEMANGLE_COMPONENT_CONST
621 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
623 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
627 TRY_CATCH (except
, RETURN_MASK_ALL
)
629 switch (demangled_type
)
631 case DEMANGLE_COMPONENT_REFERENCE
:
632 return lookup_reference_type (type
);
633 case DEMANGLE_COMPONENT_POINTER
:
634 return lookup_pointer_type (type
);
635 case DEMANGLE_COMPONENT_CONST
:
636 return make_cv_type (1, 0, type
, NULL
);
637 case DEMANGLE_COMPONENT_VOLATILE
:
638 return make_cv_type (0, 1, type
, NULL
);
641 type_name
= cp_comp_to_string (demangled
, 10);
643 if (except
.reason
< 0)
645 gdbpy_convert_exception (except
);
650 type
= typy_lookup_typename (type_name
, block
);
656 /* This is a helper function for typy_template_argument that is used
657 when the type does not have template symbols attached. It works by
658 parsing the type name. This happens with compilers, like older
659 versions of GCC, that do not emit DW_TAG_template_*. */
662 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
666 struct demangle_component
*demangled
;
667 struct demangle_parse_info
*info
;
669 struct type
*argtype
;
670 struct cleanup
*cleanup
;
672 if (TYPE_NAME (type
) == NULL
)
674 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
678 /* Note -- this is not thread-safe. */
679 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
682 PyErr_SetString (PyExc_RuntimeError
, err
);
685 demangled
= info
->tree
;
686 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
688 /* Strip off component names. */
689 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
690 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
691 demangled
= demangled
->u
.s_binary
.right
;
693 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
695 do_cleanups (cleanup
);
696 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
700 /* Skip from the template to the arguments. */
701 demangled
= demangled
->u
.s_binary
.right
;
703 for (i
= 0; demangled
&& i
< argno
; ++i
)
704 demangled
= demangled
->u
.s_binary
.right
;
708 do_cleanups (cleanup
);
709 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
714 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
715 do_cleanups (cleanup
);
719 return type_to_type_object (argtype
);
723 typy_template_argument (PyObject
*self
, PyObject
*args
)
726 struct type
*type
= ((type_object
*) self
)->type
;
727 const struct block
*block
= NULL
;
728 PyObject
*block_obj
= NULL
;
730 struct value
*val
= NULL
;
731 volatile struct gdb_exception except
;
733 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
738 block
= block_object_to_block (block_obj
);
741 PyErr_SetString (PyExc_RuntimeError
,
742 _("Second argument must be block."));
747 TRY_CATCH (except
, RETURN_MASK_ALL
)
749 type
= check_typedef (type
);
750 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
751 type
= check_typedef (TYPE_TARGET_TYPE (type
));
753 GDB_PY_HANDLE_EXCEPTION (except
);
755 /* We might not have DW_TAG_template_*, so try to parse the type's
756 name. This is inefficient if we do not have a template type --
757 but that is going to wind up as an error anyhow. */
758 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
759 return typy_legacy_template_argument (type
, block
, argno
);
761 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
763 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
768 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
769 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
770 return type_to_type_object (SYMBOL_TYPE (sym
));
771 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
773 PyErr_Format (PyExc_RuntimeError
,
774 _("Template argument is optimized out"));
778 TRY_CATCH (except
, RETURN_MASK_ALL
)
780 val
= value_of_variable (sym
, block
);
782 GDB_PY_HANDLE_EXCEPTION (except
);
784 return value_to_value_object (val
);
788 typy_str (PyObject
*self
)
790 volatile struct gdb_exception except
;
791 char *thetype
= NULL
;
795 TRY_CATCH (except
, RETURN_MASK_ALL
)
797 struct cleanup
*old_chain
;
800 stb
= mem_fileopen ();
801 old_chain
= make_cleanup_ui_file_delete (stb
);
803 type_print (type_object_to_type (self
), "", stb
, -1);
805 thetype
= ui_file_xstrdup (stb
, &length
);
806 do_cleanups (old_chain
);
808 if (except
.reason
< 0)
811 GDB_PY_HANDLE_EXCEPTION (except
);
814 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
820 /* An entry in the type-equality bcache. */
822 typedef struct type_equality_entry
824 struct type
*type1
, *type2
;
825 } type_equality_entry_d
;
827 DEF_VEC_O (type_equality_entry_d
);
829 /* A helper function to compare two strings. Returns 1 if they are
830 the same, 0 otherwise. Handles NULLs properly. */
833 compare_strings (const char *s
, const char *t
)
835 if (s
== NULL
&& t
!= NULL
)
837 else if (s
!= NULL
&& t
== NULL
)
839 else if (s
== NULL
&& t
== NULL
)
841 return strcmp (s
, t
) == 0;
844 /* A helper function for typy_richcompare that checks two types for
845 "deep" equality. Returns Py_EQ if the types are considered the
846 same, Py_NE otherwise. */
849 check_types_equal (struct type
*type1
, struct type
*type2
,
850 VEC (type_equality_entry_d
) **worklist
)
852 CHECK_TYPEDEF (type1
);
853 CHECK_TYPEDEF (type2
);
858 if (TYPE_CODE (type1
) != TYPE_CODE (type2
)
859 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
860 || TYPE_UNSIGNED (type1
) != TYPE_UNSIGNED (type2
)
861 || TYPE_NOSIGN (type1
) != TYPE_NOSIGN (type2
)
862 || TYPE_VARARGS (type1
) != TYPE_VARARGS (type2
)
863 || TYPE_VECTOR (type1
) != TYPE_VECTOR (type2
)
864 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
865 || TYPE_INSTANCE_FLAGS (type1
) != TYPE_INSTANCE_FLAGS (type2
)
866 || TYPE_NFIELDS (type1
) != TYPE_NFIELDS (type2
))
869 if (!compare_strings (TYPE_TAG_NAME (type1
), TYPE_TAG_NAME (type2
)))
871 if (!compare_strings (TYPE_NAME (type1
), TYPE_NAME (type2
)))
874 if (TYPE_CODE (type1
) == TYPE_CODE_RANGE
)
876 if (memcmp (TYPE_RANGE_DATA (type1
), TYPE_RANGE_DATA (type2
),
877 sizeof (*TYPE_RANGE_DATA (type1
))) != 0)
884 for (i
= 0; i
< TYPE_NFIELDS (type1
); ++i
)
886 const struct field
*field1
= &TYPE_FIELD (type1
, i
);
887 const struct field
*field2
= &TYPE_FIELD (type2
, i
);
888 struct type_equality_entry entry
;
890 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
891 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
892 || FIELD_LOC_KIND (*field1
) != FIELD_LOC_KIND (*field2
))
894 if (!compare_strings (FIELD_NAME (*field1
), FIELD_NAME (*field2
)))
896 switch (FIELD_LOC_KIND (*field1
))
898 case FIELD_LOC_KIND_BITPOS
:
899 if (FIELD_BITPOS (*field1
) != FIELD_BITPOS (*field2
))
902 case FIELD_LOC_KIND_PHYSADDR
:
903 if (FIELD_STATIC_PHYSADDR (*field1
)
904 != FIELD_STATIC_PHYSADDR (*field2
))
907 case FIELD_LOC_KIND_PHYSNAME
:
908 if (!compare_strings (FIELD_STATIC_PHYSNAME (*field1
),
909 FIELD_STATIC_PHYSNAME (*field2
)))
912 case FIELD_LOC_KIND_DWARF_BLOCK
:
914 struct dwarf2_locexpr_baton
*block1
, *block2
;
916 block1
= FIELD_DWARF_BLOCK (*field1
);
917 block2
= FIELD_DWARF_BLOCK (*field2
);
918 if (block1
->per_cu
!= block2
->per_cu
919 || block1
->size
!= block2
->size
920 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
925 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
926 "%d by check_types_equal"),
927 FIELD_LOC_KIND (*field1
));
930 entry
.type1
= FIELD_TYPE (*field1
);
931 entry
.type2
= FIELD_TYPE (*field2
);
932 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
936 if (TYPE_TARGET_TYPE (type1
) != NULL
)
938 struct type_equality_entry entry
;
941 if (TYPE_TARGET_TYPE (type2
) == NULL
)
944 entry
.type1
= TYPE_TARGET_TYPE (type1
);
945 entry
.type2
= TYPE_TARGET_TYPE (type2
);
946 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
948 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
954 /* Check types on a worklist for equality. Returns Py_NE if any pair
955 is not equal, Py_EQ if they are all considered equal. */
958 check_types_worklist (VEC (type_equality_entry_d
) **worklist
,
959 struct bcache
*cache
)
961 while (!VEC_empty (type_equality_entry_d
, *worklist
))
963 struct type_equality_entry entry
;
966 entry
= *VEC_last (type_equality_entry_d
, *worklist
);
967 VEC_pop (type_equality_entry_d
, *worklist
);
969 /* If the type pair has already been visited, we know it is
971 bcache_full (&entry
, sizeof (entry
), cache
, &added
);
975 if (check_types_equal (entry
.type1
, entry
.type2
, worklist
) == Py_NE
)
982 /* Implement the richcompare method. */
985 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
988 struct type
*type1
= type_object_to_type (self
);
989 struct type
*type2
= type_object_to_type (other
);
990 volatile struct gdb_exception except
;
992 /* We can only compare ourselves to another Type object, and only
993 for equality or inequality. */
994 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
996 Py_INCREF (Py_NotImplemented
);
997 return Py_NotImplemented
;
1004 struct bcache
*cache
;
1005 VEC (type_equality_entry_d
) *worklist
= NULL
;
1006 struct type_equality_entry entry
;
1008 cache
= bcache_xmalloc (NULL
, NULL
);
1010 entry
.type1
= type1
;
1011 entry
.type2
= type2
;
1012 VEC_safe_push (type_equality_entry_d
, worklist
, &entry
);
1014 TRY_CATCH (except
, RETURN_MASK_ALL
)
1016 result
= check_types_worklist (&worklist
, cache
);
1018 /* check_types_worklist calls several nested Python helper
1019 functions, some of which can raise a GDB Exception, so we
1020 just check and convert here. If there is a GDB exception, a
1021 comparison is not capable (or trusted), so exit. */
1022 bcache_xfree (cache
);
1023 VEC_free (type_equality_entry_d
, worklist
);
1024 GDB_PY_HANDLE_EXCEPTION (except
);
1034 static const struct objfile_data
*typy_objfile_data_key
;
1037 save_objfile_types (struct objfile
*objfile
, void *datum
)
1039 type_object
*obj
= datum
;
1040 htab_t copied_types
;
1041 struct cleanup
*cleanup
;
1043 /* This prevents another thread from freeing the objects we're
1045 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1047 copied_types
= create_copied_types_hash (objfile
);
1051 type_object
*next
= obj
->next
;
1053 htab_empty (copied_types
);
1055 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1063 htab_delete (copied_types
);
1065 do_cleanups (cleanup
);
1069 set_type (type_object
*obj
, struct type
*type
)
1073 if (type
&& TYPE_OBJFILE (type
))
1075 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1077 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
1079 obj
->next
->prev
= obj
;
1080 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1087 typy_dealloc (PyObject
*obj
)
1089 type_object
*type
= (type_object
*) obj
;
1092 type
->prev
->next
= type
->next
;
1093 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1095 /* Must reset head of list. */
1096 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1099 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1102 type
->next
->prev
= type
->prev
;
1104 type
->ob_type
->tp_free (type
);
1107 /* Return number of fields ("length" of the field dictionary). */
1110 typy_length (PyObject
*self
)
1112 struct type
*type
= ((type_object
*) self
)->type
;
1114 return TYPE_NFIELDS (type
);
1117 /* Return a gdb.Field object for the field named by the argument. */
1120 typy_getitem (PyObject
*self
, PyObject
*key
)
1122 struct type
*type
= ((type_object
*) self
)->type
;
1125 volatile struct gdb_exception except
;
1127 field
= python_string_to_host_string (key
);
1131 /* We want just fields of this type, not of base types, so instead of
1132 using lookup_struct_elt_type, portions of that function are
1137 TRY_CATCH (except
, RETURN_MASK_ALL
)
1139 CHECK_TYPEDEF (type
);
1141 GDB_PY_HANDLE_EXCEPTION (except
);
1143 if (TYPE_CODE (type
) != TYPE_CODE_PTR
1144 && TYPE_CODE (type
) != TYPE_CODE_REF
)
1146 type
= TYPE_TARGET_TYPE (type
);
1149 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1151 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1153 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1155 return convert_field (type
, i
);
1158 PyErr_SetObject (PyExc_KeyError
, key
);
1162 /* Implement the "get" method on the type object. This is the
1163 same as getitem if the key is present, but returns the supplied
1164 default value or None if the key is not found. */
1167 typy_get (PyObject
*self
, PyObject
*args
)
1169 PyObject
*key
, *defval
= Py_None
, *result
;
1171 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1174 result
= typy_getitem (self
, key
);
1178 /* typy_getitem returned error status. If the exception is
1179 KeyError, clear the exception status and return the defval
1180 instead. Otherwise return the exception unchanged. */
1181 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1189 /* Implement the "has_key" method on the type object. */
1192 typy_has_key (PyObject
*self
, PyObject
*args
)
1194 struct type
*type
= ((type_object
*) self
)->type
;
1197 volatile struct gdb_exception except
;
1199 if (!PyArg_ParseTuple (args
, "s", &field
))
1202 /* We want just fields of this type, not of base types, so instead of
1203 using lookup_struct_elt_type, portions of that function are
1208 TRY_CATCH (except
, RETURN_MASK_ALL
)
1210 CHECK_TYPEDEF (type
);
1212 GDB_PY_HANDLE_EXCEPTION (except
);
1213 if (TYPE_CODE (type
) != TYPE_CODE_PTR
1214 && TYPE_CODE (type
) != TYPE_CODE_REF
)
1216 type
= TYPE_TARGET_TYPE (type
);
1219 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1221 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1223 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1229 /* Make an iterator object to iterate over keys, values, or items. */
1232 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1234 typy_iterator_object
*typy_iter_obj
;
1236 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1237 &type_iterator_object_type
);
1238 if (typy_iter_obj
== NULL
)
1241 typy_iter_obj
->field
= 0;
1242 typy_iter_obj
->kind
= kind
;
1244 typy_iter_obj
->source
= (type_object
*) self
;
1246 return (PyObject
*) typy_iter_obj
;
1249 /* iteritems() method. */
1252 typy_iteritems (PyObject
*self
, PyObject
*args
)
1254 return typy_make_iter (self
, iter_items
);
1257 /* iterkeys() method. */
1260 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1262 return typy_make_iter (self
, iter_keys
);
1265 /* Iterating over the class, same as iterkeys except for the function
1269 typy_iter (PyObject
*self
)
1271 return typy_make_iter (self
, iter_keys
);
1274 /* itervalues() method. */
1277 typy_itervalues (PyObject
*self
, PyObject
*args
)
1279 return typy_make_iter (self
, iter_values
);
1282 /* Return a reference to the type iterator. */
1285 typy_iterator_iter (PyObject
*self
)
1291 /* Return the next field in the iteration through the list of fields
1295 typy_iterator_iternext (PyObject
*self
)
1297 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1298 struct type
*type
= iter_obj
->source
->type
;
1302 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1304 result
= make_fielditem (type
, iter_obj
->field
, iter_obj
->kind
);
1314 typy_iterator_dealloc (PyObject
*obj
)
1316 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1318 Py_DECREF (iter_obj
->source
);
1321 /* Create a new Type referring to TYPE. */
1323 type_to_type_object (struct type
*type
)
1325 type_object
*type_obj
;
1327 type_obj
= PyObject_New (type_object
, &type_object_type
);
1329 set_type (type_obj
, type
);
1331 return (PyObject
*) type_obj
;
1335 type_object_to_type (PyObject
*obj
)
1337 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1339 return ((type_object
*) obj
)->type
;
1344 /* Implementation of gdb.lookup_type. */
1346 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1348 static char *keywords
[] = { "name", "block", NULL
};
1349 const char *type_name
= NULL
;
1350 struct type
*type
= NULL
;
1351 PyObject
*block_obj
= NULL
;
1352 const struct block
*block
= NULL
;
1354 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1355 &type_name
, &block_obj
))
1360 block
= block_object_to_block (block_obj
);
1363 PyErr_SetString (PyExc_RuntimeError
,
1364 _("'block' argument must be a Block."));
1369 type
= typy_lookup_typename (type_name
, block
);
1373 return (PyObject
*) type_to_type_object (type
);
1377 gdbpy_initialize_types (void)
1381 typy_objfile_data_key
1382 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1384 if (PyType_Ready (&type_object_type
) < 0)
1386 if (PyType_Ready (&field_object_type
) < 0)
1388 if (PyType_Ready (&type_iterator_object_type
) < 0)
1391 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1393 if (PyModule_AddIntConstant (gdb_module
,
1394 /* Cast needed for Python 2.4. */
1395 (char *) pyty_codes
[i
].name
,
1396 pyty_codes
[i
].code
) < 0)
1400 Py_INCREF (&type_object_type
);
1401 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
1403 Py_INCREF (&type_iterator_object_type
);
1404 PyModule_AddObject (gdb_module
, "TypeIterator",
1405 (PyObject
*) &type_iterator_object_type
);
1407 Py_INCREF (&field_object_type
);
1408 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
1413 static PyGetSetDef type_object_getset
[] =
1415 { "code", typy_get_code
, NULL
,
1416 "The code for this type.", NULL
},
1417 { "sizeof", typy_get_sizeof
, NULL
,
1418 "The size of this type, in bytes.", NULL
},
1419 { "tag", typy_get_tag
, NULL
,
1420 "The tag name for this type, or None.", NULL
},
1424 static PyMethodDef type_object_methods
[] =
1426 { "array", typy_array
, METH_VARARGS
,
1427 "array (N) -> Type\n\
1428 Return a type which represents an array of N objects of this type." },
1429 { "__contains__", typy_has_key
, METH_VARARGS
,
1430 "T.__contains__(k) -> True if T has a field named k, else False" },
1431 { "const", typy_const
, METH_NOARGS
,
1432 "const () -> Type\n\
1433 Return a const variant of this type." },
1434 { "fields", typy_fields
, METH_NOARGS
,
1435 "fields () -> list\n\
1436 Return a list holding all the fields of this type.\n\
1437 Each field is a gdb.Field object." },
1438 { "get", typy_get
, METH_VARARGS
,
1439 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1440 otherwise returns default, if supplied, or None if not." },
1441 { "has_key", typy_has_key
, METH_VARARGS
,
1442 "T.has_key(k) -> True if T has a field named k, else False" },
1443 { "items", typy_items
, METH_NOARGS
,
1444 "items () -> list\n\
1445 Return a list of (name, field) pairs of this type.\n\
1446 Each field is a gdb.Field object." },
1447 { "iteritems", typy_iteritems
, METH_NOARGS
,
1448 "iteritems () -> an iterator over the (name, field)\n\
1449 pairs of this type. Each field is a gdb.Field object." },
1450 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1451 "iterkeys () -> an iterator over the field names of this type." },
1452 { "itervalues", typy_itervalues
, METH_NOARGS
,
1453 "itervalues () -> an iterator over the fields of this type.\n\
1454 Each field is a gdb.Field object." },
1455 { "keys", typy_field_names
, METH_NOARGS
,
1457 Return a list holding all the fields names of this type." },
1458 { "pointer", typy_pointer
, METH_NOARGS
,
1459 "pointer () -> Type\n\
1460 Return a type of pointer to this type." },
1461 { "range", typy_range
, METH_NOARGS
,
1462 "range () -> tuple\n\
1463 Return a tuple containing the lower and upper range for this type."},
1464 { "reference", typy_reference
, METH_NOARGS
,
1465 "reference () -> Type\n\
1466 Return a type of reference to this type." },
1467 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1468 "strip_typedefs () -> Type\n\
1469 Return a type formed by stripping this type of all typedefs."},
1470 { "target", typy_target
, METH_NOARGS
,
1471 "target () -> Type\n\
1472 Return the target type of this type." },
1473 { "template_argument", typy_template_argument
, METH_VARARGS
,
1474 "template_argument (arg, [block]) -> Type\n\
1475 Return the type of a template argument." },
1476 { "unqualified", typy_unqualified
, METH_NOARGS
,
1477 "unqualified () -> Type\n\
1478 Return a variant of this type without const or volatile attributes." },
1479 { "values", typy_fields
, METH_NOARGS
,
1480 "values () -> list\n\
1481 Return a list holding all the fields of this type.\n\
1482 Each field is a gdb.Field object." },
1483 { "volatile", typy_volatile
, METH_NOARGS
,
1484 "volatile () -> Type\n\
1485 Return a volatile variant of this type" },
1489 static PyMappingMethods typy_mapping
= {
1492 NULL
/* no "set" method */
1495 static PyTypeObject type_object_type
=
1497 PyObject_HEAD_INIT (NULL
)
1499 "gdb.Type", /*tp_name*/
1500 sizeof (type_object
), /*tp_basicsize*/
1502 typy_dealloc
, /*tp_dealloc*/
1509 0, /*tp_as_sequence*/
1510 &typy_mapping
, /*tp_as_mapping*/
1513 typy_str
, /*tp_str*/
1517 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1518 "GDB type object", /* tp_doc */
1519 0, /* tp_traverse */
1521 typy_richcompare
, /* tp_richcompare */
1522 0, /* tp_weaklistoffset */
1523 typy_iter
, /* tp_iter */
1524 0, /* tp_iternext */
1525 type_object_methods
, /* tp_methods */
1527 type_object_getset
, /* tp_getset */
1530 0, /* tp_descr_get */
1531 0, /* tp_descr_set */
1532 0, /* tp_dictoffset */
1538 static PyTypeObject field_object_type
=
1540 PyObject_HEAD_INIT (NULL
)
1542 "gdb.Field", /*tp_name*/
1543 sizeof (field_object
), /*tp_basicsize*/
1545 field_dealloc
, /*tp_dealloc*/
1552 0, /*tp_as_sequence*/
1553 0, /*tp_as_mapping*/
1560 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1561 "GDB field object", /* tp_doc */
1562 0, /* tp_traverse */
1564 0, /* tp_richcompare */
1565 0, /* tp_weaklistoffset */
1567 0, /* tp_iternext */
1573 0, /* tp_descr_get */
1574 0, /* tp_descr_set */
1575 offsetof (field_object
, dict
), /* tp_dictoffset */
1581 static PyTypeObject type_iterator_object_type
= {
1582 PyObject_HEAD_INIT (NULL
)
1584 "gdb.TypeIterator", /*tp_name*/
1585 sizeof (typy_iterator_object
), /*tp_basicsize*/
1587 typy_iterator_dealloc
, /*tp_dealloc*/
1594 0, /*tp_as_sequence*/
1595 0, /*tp_as_mapping*/
1602 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1603 "GDB type iterator object", /*tp_doc */
1606 0, /*tp_richcompare */
1607 0, /*tp_weaklistoffset */
1608 typy_iterator_iter
, /*tp_iter */
1609 typy_iterator_iternext
, /*tp_iternext */