1 /* Python interface to types.
3 Copyright (C) 2008-2012 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"
33 #include "typeprint.h"
35 typedef struct pyty_type_object
40 /* If a Type object is associated with an objfile, it is kept on a
41 doubly-linked list, rooted in the objfile. This lets us copy the
42 underlying struct type when the objfile is deleted. */
43 struct pyty_type_object
*prev
;
44 struct pyty_type_object
*next
;
47 static PyTypeObject type_object_type
;
50 typedef struct pyty_field_object
54 /* Dictionary holding our attributes. */
58 static PyTypeObject field_object_type
;
60 /* A type iterator object. */
63 /* The current field index. */
66 enum gdbpy_iter_kind kind
;
67 /* Pointer back to the original source type object. */
68 struct pyty_type_object
*source
;
69 } typy_iterator_object
;
71 static PyTypeObject type_iterator_object_type
;
73 /* This is used to initialize various gdb.TYPE_ constants. */
82 /* Forward declarations. */
83 static PyObject
*typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
);
85 #define ENTRY(X) { X, #X }
87 static struct pyty_code pyty_codes
[] =
89 ENTRY (TYPE_CODE_BITSTRING
),
90 ENTRY (TYPE_CODE_PTR
),
91 ENTRY (TYPE_CODE_ARRAY
),
92 ENTRY (TYPE_CODE_STRUCT
),
93 ENTRY (TYPE_CODE_UNION
),
94 ENTRY (TYPE_CODE_ENUM
),
95 ENTRY (TYPE_CODE_FLAGS
),
96 ENTRY (TYPE_CODE_FUNC
),
97 ENTRY (TYPE_CODE_INT
),
98 ENTRY (TYPE_CODE_FLT
),
99 ENTRY (TYPE_CODE_VOID
),
100 ENTRY (TYPE_CODE_SET
),
101 ENTRY (TYPE_CODE_RANGE
),
102 ENTRY (TYPE_CODE_STRING
),
103 ENTRY (TYPE_CODE_ERROR
),
104 ENTRY (TYPE_CODE_METHOD
),
105 ENTRY (TYPE_CODE_METHODPTR
),
106 ENTRY (TYPE_CODE_MEMBERPTR
),
107 ENTRY (TYPE_CODE_REF
),
108 ENTRY (TYPE_CODE_CHAR
),
109 ENTRY (TYPE_CODE_BOOL
),
110 ENTRY (TYPE_CODE_COMPLEX
),
111 ENTRY (TYPE_CODE_TYPEDEF
),
112 ENTRY (TYPE_CODE_NAMESPACE
),
113 ENTRY (TYPE_CODE_DECFLOAT
),
114 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
115 { TYPE_CODE_UNDEF
, NULL
}
121 field_dealloc (PyObject
*obj
)
123 field_object
*f
= (field_object
*) obj
;
125 Py_XDECREF (f
->dict
);
126 f
->ob_type
->tp_free (obj
);
132 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
136 result
->dict
= PyDict_New ();
143 return (PyObject
*) result
;
148 /* Return the code for this type. */
150 typy_get_code (PyObject
*self
, void *closure
)
152 struct type
*type
= ((type_object
*) self
)->type
;
154 return PyInt_FromLong (TYPE_CODE (type
));
157 /* Helper function for typy_fields which converts a single field to a
158 gdb.Field object. Returns NULL on error. */
161 convert_field (struct type
*type
, int field
)
163 PyObject
*result
= field_new ();
169 if (!field_is_static (&TYPE_FIELD (type
, field
)))
171 const char *attrstring
;
173 if (TYPE_CODE (type
) == TYPE_CODE_ENUM
)
175 arg
= gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type
, field
));
176 attrstring
= "enumval";
180 arg
= gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type
, field
));
181 attrstring
= "bitpos";
187 /* At least python-2.4 had the second parameter non-const. */
188 if (PyObject_SetAttrString (result
, (char *) attrstring
, arg
) < 0)
193 if (TYPE_FIELD_NAME (type
, field
))
194 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
202 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
206 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
208 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
212 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
213 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
217 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
221 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
224 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
228 /* A field can have a NULL type in some situations. */
229 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
235 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
238 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
251 /* Helper function to return the name of a field, as a gdb.Field object.
252 If the field doesn't have a name, None is returned. */
255 field_name (struct type
*type
, int field
)
259 if (TYPE_FIELD_NAME (type
, field
))
260 result
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
269 /* Helper function for Type standard mapping methods. Returns a
270 Python object for field i of the type. "kind" specifies what to
271 return: the name of the field, a gdb.Field object corresponding to
272 the field, or a tuple consisting of field name and gdb.Field
276 make_fielditem (struct type
*type
, int i
, enum gdbpy_iter_kind kind
)
278 PyObject
*item
= NULL
, *key
= NULL
, *value
= NULL
;
283 key
= field_name (type
, i
);
286 value
= convert_field (type
, i
);
289 item
= PyTuple_New (2);
292 PyTuple_SET_ITEM (item
, 0, key
);
293 PyTuple_SET_ITEM (item
, 1, value
);
296 item
= field_name (type
, i
);
299 item
= convert_field (type
, i
);
311 /* Return a sequence of all field names, fields, or (name, field) pairs.
312 Each field is a gdb.Field object. */
315 typy_fields_items (PyObject
*self
, enum gdbpy_iter_kind kind
)
317 PyObject
*py_type
= self
;
318 PyObject
*result
= NULL
, *iter
= NULL
;
319 volatile struct gdb_exception except
;
320 struct type
*type
= ((type_object
*) py_type
)->type
;
321 struct type
*checked_type
= type
;
323 TRY_CATCH (except
, RETURN_MASK_ALL
)
325 CHECK_TYPEDEF (checked_type
);
327 GDB_PY_HANDLE_EXCEPTION (except
);
329 if (checked_type
!= type
)
330 py_type
= type_to_type_object (checked_type
);
331 iter
= typy_make_iter (py_type
, kind
);
332 if (checked_type
!= type
)
334 /* Need to wrap this in braces because Py_DECREF isn't wrapped
335 in a do{}while(0). */
340 result
= PySequence_List (iter
);
347 /* Return a sequence of all fields. Each field is a gdb.Field object. */
350 typy_values (PyObject
*self
, PyObject
*args
)
352 return typy_fields_items (self
, iter_values
);
355 /* Return a sequence of all fields. Each field is a gdb.Field object.
356 This method is similar to typy_values, except where the supplied
357 gdb.Type is an array, in which case it returns a list of one entry
358 which is a gdb.Field object for a range (the array bounds). */
361 typy_fields (PyObject
*self
, PyObject
*args
)
363 struct type
*type
= ((type_object
*) self
)->type
;
366 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
367 return typy_fields_items (self
, iter_values
);
369 /* Array type. Handle this as a special case because the common
370 machinery wants struct or union or enum types. Build a list of
371 one entry which is the range for the array. */
372 r
= convert_field (type
, 0);
376 rl
= Py_BuildValue ("[O]", r
);
385 /* Return a sequence of all field names. Each field is a gdb.Field object. */
388 typy_field_names (PyObject
*self
, PyObject
*args
)
390 return typy_fields_items (self
, iter_keys
);
393 /* Return a sequence of all (name, fields) pairs. Each field is a
397 typy_items (PyObject
*self
, PyObject
*args
)
399 return typy_fields_items (self
, iter_items
);
402 /* Return the type's tag, or None. */
404 typy_get_tag (PyObject
*self
, void *closure
)
406 struct type
*type
= ((type_object
*) self
)->type
;
408 if (!TYPE_TAG_NAME (type
))
410 return PyString_FromString (TYPE_TAG_NAME (type
));
413 /* Return the type, stripped of typedefs. */
415 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
417 struct type
*type
= ((type_object
*) self
)->type
;
418 volatile struct gdb_exception except
;
420 TRY_CATCH (except
, RETURN_MASK_ALL
)
422 type
= check_typedef (type
);
424 GDB_PY_HANDLE_EXCEPTION (except
);
426 return type_to_type_object (check_typedef (type
));
429 /* Strip typedefs and pointers/reference from a type. Then check that
430 it is a struct, union, or enum type. If not, raise TypeError. */
433 typy_get_composite (struct type
*type
)
435 volatile struct gdb_exception except
;
439 TRY_CATCH (except
, RETURN_MASK_ALL
)
441 CHECK_TYPEDEF (type
);
443 /* Don't use GDB_PY_HANDLE_EXCEPTION here because that returns
444 a (NULL) pointer of the wrong type. */
445 if (except
.reason
< 0)
447 gdbpy_convert_exception (except
);
451 if (TYPE_CODE (type
) != TYPE_CODE_PTR
452 && TYPE_CODE (type
) != TYPE_CODE_REF
)
454 type
= TYPE_TARGET_TYPE (type
);
457 /* If this is not a struct, union, or enum type, raise TypeError
459 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
460 && TYPE_CODE (type
) != TYPE_CODE_UNION
461 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
463 PyErr_SetString (PyExc_TypeError
,
464 "Type is not a structure, union, or enum type.");
471 /* Helper for typy_array and typy_vector. */
474 typy_array_1 (PyObject
*self
, PyObject
*args
, int is_vector
)
477 PyObject
*n2_obj
= NULL
;
478 struct type
*array
= NULL
;
479 struct type
*type
= ((type_object
*) self
)->type
;
480 volatile struct gdb_exception except
;
482 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
487 if (!PyInt_Check (n2_obj
))
489 PyErr_SetString (PyExc_RuntimeError
,
490 _("Array bound must be an integer"));
494 if (! gdb_py_int_as_long (n2_obj
, &n2
))
505 PyErr_SetString (PyExc_ValueError
,
506 _("Array length must not be negative"));
510 TRY_CATCH (except
, RETURN_MASK_ALL
)
512 array
= lookup_array_range_type (type
, n1
, n2
);
514 make_vector_type (array
);
516 GDB_PY_HANDLE_EXCEPTION (except
);
518 return type_to_type_object (array
);
521 /* Return an array type. */
524 typy_array (PyObject
*self
, PyObject
*args
)
526 return typy_array_1 (self
, args
, 0);
529 /* Return a vector type. */
532 typy_vector (PyObject
*self
, PyObject
*args
)
534 return typy_array_1 (self
, args
, 1);
537 /* Return a Type object which represents a pointer to SELF. */
539 typy_pointer (PyObject
*self
, PyObject
*args
)
541 struct type
*type
= ((type_object
*) self
)->type
;
542 volatile struct gdb_exception except
;
544 TRY_CATCH (except
, RETURN_MASK_ALL
)
546 type
= lookup_pointer_type (type
);
548 GDB_PY_HANDLE_EXCEPTION (except
);
550 return type_to_type_object (type
);
553 /* Return the range of a type represented by SELF. The return type is
554 a tuple. The first element of the tuple contains the low bound,
555 while the second element of the tuple contains the high bound. */
557 typy_range (PyObject
*self
, PyObject
*args
)
559 struct type
*type
= ((type_object
*) self
)->type
;
561 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
562 /* Initialize these to appease GCC warnings. */
563 LONGEST low
= 0, high
= 0;
565 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
566 && TYPE_CODE (type
) != TYPE_CODE_STRING
567 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
569 PyErr_SetString (PyExc_RuntimeError
,
570 _("This type does not have a range."));
574 switch (TYPE_CODE (type
))
576 case TYPE_CODE_ARRAY
:
577 case TYPE_CODE_STRING
:
578 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
579 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
581 case TYPE_CODE_RANGE
:
582 low
= TYPE_LOW_BOUND (type
);
583 high
= TYPE_HIGH_BOUND (type
);
587 low_bound
= PyLong_FromLong (low
);
591 high_bound
= PyLong_FromLong (high
);
595 result
= PyTuple_New (2);
599 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
604 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
606 Py_DECREF (high_bound
);
613 Py_XDECREF (high_bound
);
614 Py_XDECREF (low_bound
);
618 /* Return a Type object which represents a reference to SELF. */
620 typy_reference (PyObject
*self
, PyObject
*args
)
622 struct type
*type
= ((type_object
*) self
)->type
;
623 volatile struct gdb_exception except
;
625 TRY_CATCH (except
, RETURN_MASK_ALL
)
627 type
= lookup_reference_type (type
);
629 GDB_PY_HANDLE_EXCEPTION (except
);
631 return type_to_type_object (type
);
634 /* Return a Type object which represents the target type of SELF. */
636 typy_target (PyObject
*self
, PyObject
*args
)
638 struct type
*type
= ((type_object
*) self
)->type
;
640 if (!TYPE_TARGET_TYPE (type
))
642 PyErr_SetString (PyExc_RuntimeError
,
643 _("Type does not have a target."));
647 return type_to_type_object (TYPE_TARGET_TYPE (type
));
650 /* Return a const-qualified type variant. */
652 typy_const (PyObject
*self
, PyObject
*args
)
654 struct type
*type
= ((type_object
*) self
)->type
;
655 volatile struct gdb_exception except
;
657 TRY_CATCH (except
, RETURN_MASK_ALL
)
659 type
= make_cv_type (1, 0, type
, NULL
);
661 GDB_PY_HANDLE_EXCEPTION (except
);
663 return type_to_type_object (type
);
666 /* Return a volatile-qualified type variant. */
668 typy_volatile (PyObject
*self
, PyObject
*args
)
670 struct type
*type
= ((type_object
*) self
)->type
;
671 volatile struct gdb_exception except
;
673 TRY_CATCH (except
, RETURN_MASK_ALL
)
675 type
= make_cv_type (0, 1, type
, NULL
);
677 GDB_PY_HANDLE_EXCEPTION (except
);
679 return type_to_type_object (type
);
682 /* Return an unqualified type variant. */
684 typy_unqualified (PyObject
*self
, PyObject
*args
)
686 struct type
*type
= ((type_object
*) self
)->type
;
687 volatile struct gdb_exception except
;
689 TRY_CATCH (except
, RETURN_MASK_ALL
)
691 type
= make_cv_type (0, 0, type
, NULL
);
693 GDB_PY_HANDLE_EXCEPTION (except
);
695 return type_to_type_object (type
);
698 /* Return the size of the type represented by SELF, in bytes. */
700 typy_get_sizeof (PyObject
*self
, void *closure
)
702 struct type
*type
= ((type_object
*) self
)->type
;
703 volatile struct gdb_exception except
;
705 TRY_CATCH (except
, RETURN_MASK_ALL
)
707 check_typedef (type
);
709 /* Ignore exceptions. */
711 return gdb_py_long_from_longest (TYPE_LENGTH (type
));
715 typy_lookup_typename (const char *type_name
, const struct block
*block
)
717 struct type
*type
= NULL
;
718 volatile struct gdb_exception except
;
720 TRY_CATCH (except
, RETURN_MASK_ALL
)
722 if (!strncmp (type_name
, "struct ", 7))
723 type
= lookup_struct (type_name
+ 7, NULL
);
724 else if (!strncmp (type_name
, "union ", 6))
725 type
= lookup_union (type_name
+ 6, NULL
);
726 else if (!strncmp (type_name
, "enum ", 5))
727 type
= lookup_enum (type_name
+ 5, NULL
);
729 type
= lookup_typename (python_language
, python_gdbarch
,
730 type_name
, block
, 0);
732 if (except
.reason
< 0)
734 gdbpy_convert_exception (except
);
742 typy_lookup_type (struct demangle_component
*demangled
,
743 const struct block
*block
)
745 struct type
*type
, *rtype
= NULL
;
746 char *type_name
= NULL
;
747 enum demangle_component_type demangled_type
;
748 volatile struct gdb_exception except
;
750 /* Save the type: typy_lookup_type() may (indirectly) overwrite
751 memory pointed by demangled. */
752 demangled_type
= demangled
->type
;
754 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
755 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
756 || demangled_type
== DEMANGLE_COMPONENT_CONST
757 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
759 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
763 TRY_CATCH (except
, RETURN_MASK_ALL
)
765 /* If the demangled_type matches with one of the types
766 below, run the corresponding function and save the type
767 to return later. We cannot just return here as we are in
768 an exception handler. */
769 switch (demangled_type
)
771 case DEMANGLE_COMPONENT_REFERENCE
:
772 rtype
= lookup_reference_type (type
);
774 case DEMANGLE_COMPONENT_POINTER
:
775 rtype
= lookup_pointer_type (type
);
777 case DEMANGLE_COMPONENT_CONST
:
778 rtype
= make_cv_type (1, 0, type
, NULL
);
780 case DEMANGLE_COMPONENT_VOLATILE
:
781 rtype
= make_cv_type (0, 1, type
, NULL
);
785 if (except
.reason
< 0)
787 gdbpy_convert_exception (except
);
792 /* If we have a type from the switch statement above, just return
797 /* We don't have a type, so lookup the type. */
798 type_name
= cp_comp_to_string (demangled
, 10);
799 type
= typy_lookup_typename (type_name
, block
);
805 /* This is a helper function for typy_template_argument that is used
806 when the type does not have template symbols attached. It works by
807 parsing the type name. This happens with compilers, like older
808 versions of GCC, that do not emit DW_TAG_template_*. */
811 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
815 struct demangle_component
*demangled
;
816 struct demangle_parse_info
*info
= NULL
;
818 struct type
*argtype
;
819 struct cleanup
*cleanup
;
820 volatile struct gdb_exception except
;
822 if (TYPE_NAME (type
) == NULL
)
824 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
828 TRY_CATCH (except
, RETURN_MASK_ALL
)
830 /* Note -- this is not thread-safe. */
831 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
833 GDB_PY_HANDLE_EXCEPTION (except
);
837 PyErr_SetString (PyExc_RuntimeError
, err
);
840 demangled
= info
->tree
;
841 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
843 /* Strip off component names. */
844 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
845 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
846 demangled
= demangled
->u
.s_binary
.right
;
848 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
850 do_cleanups (cleanup
);
851 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
855 /* Skip from the template to the arguments. */
856 demangled
= demangled
->u
.s_binary
.right
;
858 for (i
= 0; demangled
&& i
< argno
; ++i
)
859 demangled
= demangled
->u
.s_binary
.right
;
863 do_cleanups (cleanup
);
864 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
869 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
870 do_cleanups (cleanup
);
874 return type_to_type_object (argtype
);
878 typy_template_argument (PyObject
*self
, PyObject
*args
)
881 struct type
*type
= ((type_object
*) self
)->type
;
882 const struct block
*block
= NULL
;
883 PyObject
*block_obj
= NULL
;
885 struct value
*val
= NULL
;
886 volatile struct gdb_exception except
;
888 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
893 block
= block_object_to_block (block_obj
);
896 PyErr_SetString (PyExc_RuntimeError
,
897 _("Second argument must be block."));
902 TRY_CATCH (except
, RETURN_MASK_ALL
)
904 type
= check_typedef (type
);
905 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
906 type
= check_typedef (TYPE_TARGET_TYPE (type
));
908 GDB_PY_HANDLE_EXCEPTION (except
);
910 /* We might not have DW_TAG_template_*, so try to parse the type's
911 name. This is inefficient if we do not have a template type --
912 but that is going to wind up as an error anyhow. */
913 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
914 return typy_legacy_template_argument (type
, block
, argno
);
916 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
918 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
923 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
924 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
925 return type_to_type_object (SYMBOL_TYPE (sym
));
926 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
928 PyErr_Format (PyExc_RuntimeError
,
929 _("Template argument is optimized out"));
933 TRY_CATCH (except
, RETURN_MASK_ALL
)
935 val
= value_of_variable (sym
, block
);
937 GDB_PY_HANDLE_EXCEPTION (except
);
939 return value_to_value_object (val
);
943 typy_str (PyObject
*self
)
945 volatile struct gdb_exception except
;
946 char *thetype
= NULL
;
950 TRY_CATCH (except
, RETURN_MASK_ALL
)
952 struct cleanup
*old_chain
;
955 stb
= mem_fileopen ();
956 old_chain
= make_cleanup_ui_file_delete (stb
);
958 LA_PRINT_TYPE (type_object_to_type (self
), "", stb
, -1, 0,
959 &type_print_raw_options
);
961 thetype
= ui_file_xstrdup (stb
, &length
);
962 do_cleanups (old_chain
);
964 if (except
.reason
< 0)
967 GDB_PY_HANDLE_EXCEPTION (except
);
970 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
976 /* An entry in the type-equality bcache. */
978 typedef struct type_equality_entry
980 struct type
*type1
, *type2
;
981 } type_equality_entry_d
;
983 DEF_VEC_O (type_equality_entry_d
);
985 /* A helper function to compare two strings. Returns 1 if they are
986 the same, 0 otherwise. Handles NULLs properly. */
989 compare_maybe_null_strings (const char *s
, const char *t
)
991 if (s
== NULL
&& t
!= NULL
)
993 else if (s
!= NULL
&& t
== NULL
)
995 else if (s
== NULL
&& t
== NULL
)
997 return strcmp (s
, t
) == 0;
1000 /* A helper function for typy_richcompare that checks two types for
1001 "deep" equality. Returns Py_EQ if the types are considered the
1002 same, Py_NE otherwise. */
1005 check_types_equal (struct type
*type1
, struct type
*type2
,
1006 VEC (type_equality_entry_d
) **worklist
)
1008 CHECK_TYPEDEF (type1
);
1009 CHECK_TYPEDEF (type2
);
1014 if (TYPE_CODE (type1
) != TYPE_CODE (type2
)
1015 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
1016 || TYPE_UNSIGNED (type1
) != TYPE_UNSIGNED (type2
)
1017 || TYPE_NOSIGN (type1
) != TYPE_NOSIGN (type2
)
1018 || TYPE_VARARGS (type1
) != TYPE_VARARGS (type2
)
1019 || TYPE_VECTOR (type1
) != TYPE_VECTOR (type2
)
1020 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
1021 || TYPE_INSTANCE_FLAGS (type1
) != TYPE_INSTANCE_FLAGS (type2
)
1022 || TYPE_NFIELDS (type1
) != TYPE_NFIELDS (type2
))
1025 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1
),
1026 TYPE_TAG_NAME (type2
)))
1028 if (!compare_maybe_null_strings (TYPE_NAME (type1
), TYPE_NAME (type2
)))
1031 if (TYPE_CODE (type1
) == TYPE_CODE_RANGE
)
1033 if (memcmp (TYPE_RANGE_DATA (type1
), TYPE_RANGE_DATA (type2
),
1034 sizeof (*TYPE_RANGE_DATA (type1
))) != 0)
1041 for (i
= 0; i
< TYPE_NFIELDS (type1
); ++i
)
1043 const struct field
*field1
= &TYPE_FIELD (type1
, i
);
1044 const struct field
*field2
= &TYPE_FIELD (type2
, i
);
1045 struct type_equality_entry entry
;
1047 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
1048 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
1049 || FIELD_LOC_KIND (*field1
) != FIELD_LOC_KIND (*field2
))
1051 if (!compare_maybe_null_strings (FIELD_NAME (*field1
),
1052 FIELD_NAME (*field2
)))
1054 switch (FIELD_LOC_KIND (*field1
))
1056 case FIELD_LOC_KIND_BITPOS
:
1057 if (FIELD_BITPOS (*field1
) != FIELD_BITPOS (*field2
))
1060 case FIELD_LOC_KIND_ENUMVAL
:
1061 if (FIELD_ENUMVAL (*field1
) != FIELD_ENUMVAL (*field2
))
1064 case FIELD_LOC_KIND_PHYSADDR
:
1065 if (FIELD_STATIC_PHYSADDR (*field1
)
1066 != FIELD_STATIC_PHYSADDR (*field2
))
1069 case FIELD_LOC_KIND_PHYSNAME
:
1070 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1
),
1071 FIELD_STATIC_PHYSNAME (*field2
)))
1074 case FIELD_LOC_KIND_DWARF_BLOCK
:
1076 struct dwarf2_locexpr_baton
*block1
, *block2
;
1078 block1
= FIELD_DWARF_BLOCK (*field1
);
1079 block2
= FIELD_DWARF_BLOCK (*field2
);
1080 if (block1
->per_cu
!= block2
->per_cu
1081 || block1
->size
!= block2
->size
1082 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
1087 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
1088 "%d by check_types_equal"),
1089 FIELD_LOC_KIND (*field1
));
1092 entry
.type1
= FIELD_TYPE (*field1
);
1093 entry
.type2
= FIELD_TYPE (*field2
);
1094 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
1098 if (TYPE_TARGET_TYPE (type1
) != NULL
)
1100 struct type_equality_entry entry
;
1102 if (TYPE_TARGET_TYPE (type2
) == NULL
)
1105 entry
.type1
= TYPE_TARGET_TYPE (type1
);
1106 entry
.type2
= TYPE_TARGET_TYPE (type2
);
1107 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
1109 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
1115 /* Check types on a worklist for equality. Returns Py_NE if any pair
1116 is not equal, Py_EQ if they are all considered equal. */
1119 check_types_worklist (VEC (type_equality_entry_d
) **worklist
,
1120 struct bcache
*cache
)
1122 while (!VEC_empty (type_equality_entry_d
, *worklist
))
1124 struct type_equality_entry entry
;
1127 entry
= *VEC_last (type_equality_entry_d
, *worklist
);
1128 VEC_pop (type_equality_entry_d
, *worklist
);
1130 /* If the type pair has already been visited, we know it is
1132 bcache_full (&entry
, sizeof (entry
), cache
, &added
);
1136 if (check_types_equal (entry
.type1
, entry
.type2
, worklist
) == Py_NE
)
1143 /* Implement the richcompare method. */
1146 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1149 struct type
*type1
= type_object_to_type (self
);
1150 struct type
*type2
= type_object_to_type (other
);
1151 volatile struct gdb_exception except
;
1153 /* We can only compare ourselves to another Type object, and only
1154 for equality or inequality. */
1155 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
1157 Py_INCREF (Py_NotImplemented
);
1158 return Py_NotImplemented
;
1165 struct bcache
*cache
;
1166 VEC (type_equality_entry_d
) *worklist
= NULL
;
1167 struct type_equality_entry entry
;
1169 cache
= bcache_xmalloc (NULL
, NULL
);
1171 entry
.type1
= type1
;
1172 entry
.type2
= type2
;
1173 VEC_safe_push (type_equality_entry_d
, worklist
, &entry
);
1175 TRY_CATCH (except
, RETURN_MASK_ALL
)
1177 result
= check_types_worklist (&worklist
, cache
);
1179 /* check_types_worklist calls several nested Python helper
1180 functions, some of which can raise a GDB Exception, so we
1181 just check and convert here. If there is a GDB exception, a
1182 comparison is not capable (or trusted), so exit. */
1183 bcache_xfree (cache
);
1184 VEC_free (type_equality_entry_d
, worklist
);
1185 GDB_PY_HANDLE_EXCEPTION (except
);
1195 static const struct objfile_data
*typy_objfile_data_key
;
1198 save_objfile_types (struct objfile
*objfile
, void *datum
)
1200 type_object
*obj
= datum
;
1201 htab_t copied_types
;
1202 struct cleanup
*cleanup
;
1204 /* This prevents another thread from freeing the objects we're
1206 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1208 copied_types
= create_copied_types_hash (objfile
);
1212 type_object
*next
= obj
->next
;
1214 htab_empty (copied_types
);
1216 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1224 htab_delete (copied_types
);
1226 do_cleanups (cleanup
);
1230 set_type (type_object
*obj
, struct type
*type
)
1234 if (type
&& TYPE_OBJFILE (type
))
1236 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1238 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
1240 obj
->next
->prev
= obj
;
1241 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1248 typy_dealloc (PyObject
*obj
)
1250 type_object
*type
= (type_object
*) obj
;
1253 type
->prev
->next
= type
->next
;
1254 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1256 /* Must reset head of list. */
1257 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1260 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1263 type
->next
->prev
= type
->prev
;
1265 type
->ob_type
->tp_free (type
);
1268 /* Return number of fields ("length" of the field dictionary). */
1271 typy_length (PyObject
*self
)
1273 struct type
*type
= ((type_object
*) self
)->type
;
1275 type
= typy_get_composite (type
);
1279 return TYPE_NFIELDS (type
);
1282 /* Implements boolean evaluation of gdb.Type. Handle this like other
1283 Python objects that don't have a meaningful truth value -- all
1287 typy_nonzero (PyObject
*self
)
1292 /* Return a gdb.Field object for the field named by the argument. */
1295 typy_getitem (PyObject
*self
, PyObject
*key
)
1297 struct type
*type
= ((type_object
*) self
)->type
;
1301 field
= python_string_to_host_string (key
);
1305 /* We want just fields of this type, not of base types, so instead of
1306 using lookup_struct_elt_type, portions of that function are
1309 type
= typy_get_composite (type
);
1313 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1315 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1317 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1319 return convert_field (type
, i
);
1322 PyErr_SetObject (PyExc_KeyError
, key
);
1326 /* Implement the "get" method on the type object. This is the
1327 same as getitem if the key is present, but returns the supplied
1328 default value or None if the key is not found. */
1331 typy_get (PyObject
*self
, PyObject
*args
)
1333 PyObject
*key
, *defval
= Py_None
, *result
;
1335 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1338 result
= typy_getitem (self
, key
);
1342 /* typy_getitem returned error status. If the exception is
1343 KeyError, clear the exception status and return the defval
1344 instead. Otherwise return the exception unchanged. */
1345 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1353 /* Implement the "has_key" method on the type object. */
1356 typy_has_key (PyObject
*self
, PyObject
*args
)
1358 struct type
*type
= ((type_object
*) self
)->type
;
1362 if (!PyArg_ParseTuple (args
, "s", &field
))
1365 /* We want just fields of this type, not of base types, so instead of
1366 using lookup_struct_elt_type, portions of that function are
1369 type
= typy_get_composite (type
);
1373 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1375 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1377 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1383 /* Make an iterator object to iterate over keys, values, or items. */
1386 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1388 typy_iterator_object
*typy_iter_obj
;
1390 /* Check that "self" is a structure or union type. */
1391 if (typy_get_composite (((type_object
*) self
)->type
) == NULL
)
1394 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1395 &type_iterator_object_type
);
1396 if (typy_iter_obj
== NULL
)
1399 typy_iter_obj
->field
= 0;
1400 typy_iter_obj
->kind
= kind
;
1402 typy_iter_obj
->source
= (type_object
*) self
;
1404 return (PyObject
*) typy_iter_obj
;
1407 /* iteritems() method. */
1410 typy_iteritems (PyObject
*self
, PyObject
*args
)
1412 return typy_make_iter (self
, iter_items
);
1415 /* iterkeys() method. */
1418 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1420 return typy_make_iter (self
, iter_keys
);
1423 /* Iterating over the class, same as iterkeys except for the function
1427 typy_iter (PyObject
*self
)
1429 return typy_make_iter (self
, iter_keys
);
1432 /* itervalues() method. */
1435 typy_itervalues (PyObject
*self
, PyObject
*args
)
1437 return typy_make_iter (self
, iter_values
);
1440 /* Return a reference to the type iterator. */
1443 typy_iterator_iter (PyObject
*self
)
1449 /* Return the next field in the iteration through the list of fields
1453 typy_iterator_iternext (PyObject
*self
)
1455 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1456 struct type
*type
= iter_obj
->source
->type
;
1459 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1461 result
= make_fielditem (type
, iter_obj
->field
, iter_obj
->kind
);
1471 typy_iterator_dealloc (PyObject
*obj
)
1473 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1475 Py_DECREF (iter_obj
->source
);
1478 /* Create a new Type referring to TYPE. */
1480 type_to_type_object (struct type
*type
)
1482 type_object
*type_obj
;
1484 type_obj
= PyObject_New (type_object
, &type_object_type
);
1486 set_type (type_obj
, type
);
1488 return (PyObject
*) type_obj
;
1492 type_object_to_type (PyObject
*obj
)
1494 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1496 return ((type_object
*) obj
)->type
;
1501 /* Implementation of gdb.lookup_type. */
1503 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1505 static char *keywords
[] = { "name", "block", NULL
};
1506 const char *type_name
= NULL
;
1507 struct type
*type
= NULL
;
1508 PyObject
*block_obj
= NULL
;
1509 const struct block
*block
= NULL
;
1511 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1512 &type_name
, &block_obj
))
1517 block
= block_object_to_block (block_obj
);
1520 PyErr_SetString (PyExc_RuntimeError
,
1521 _("'block' argument must be a Block."));
1526 type
= typy_lookup_typename (type_name
, block
);
1530 return (PyObject
*) type_to_type_object (type
);
1534 gdbpy_initialize_types (void)
1538 typy_objfile_data_key
1539 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1541 if (PyType_Ready (&type_object_type
) < 0)
1543 if (PyType_Ready (&field_object_type
) < 0)
1545 if (PyType_Ready (&type_iterator_object_type
) < 0)
1548 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1550 if (PyModule_AddIntConstant (gdb_module
,
1551 /* Cast needed for Python 2.4. */
1552 (char *) pyty_codes
[i
].name
,
1553 pyty_codes
[i
].code
) < 0)
1557 Py_INCREF (&type_object_type
);
1558 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
1560 Py_INCREF (&type_iterator_object_type
);
1561 PyModule_AddObject (gdb_module
, "TypeIterator",
1562 (PyObject
*) &type_iterator_object_type
);
1564 Py_INCREF (&field_object_type
);
1565 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
1570 static PyGetSetDef type_object_getset
[] =
1572 { "code", typy_get_code
, NULL
,
1573 "The code for this type.", NULL
},
1574 { "sizeof", typy_get_sizeof
, NULL
,
1575 "The size of this type, in bytes.", NULL
},
1576 { "tag", typy_get_tag
, NULL
,
1577 "The tag name for this type, or None.", NULL
},
1581 static PyMethodDef type_object_methods
[] =
1583 { "array", typy_array
, METH_VARARGS
,
1584 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1585 Return a type which represents an array of objects of this type.\n\
1586 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1587 If LOW_BOUND is omitted, a value of zero is used." },
1588 { "vector", typy_vector
, METH_VARARGS
,
1589 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1590 Return a type which represents a vector of objects of this type.\n\
1591 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1592 If LOW_BOUND is omitted, a value of zero is used.\n\
1593 Vectors differ from arrays in that if the current language has C-style\n\
1594 arrays, vectors don't decay to a pointer to the first element.\n\
1595 They are first class values." },
1596 { "__contains__", typy_has_key
, METH_VARARGS
,
1597 "T.__contains__(k) -> True if T has a field named k, else False" },
1598 { "const", typy_const
, METH_NOARGS
,
1599 "const () -> Type\n\
1600 Return a const variant of this type." },
1601 { "fields", typy_fields
, METH_NOARGS
,
1602 "fields () -> list\n\
1603 Return a list holding all the fields of this type.\n\
1604 Each field is a gdb.Field object." },
1605 { "get", typy_get
, METH_VARARGS
,
1606 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1607 otherwise returns default, if supplied, or None if not." },
1608 { "has_key", typy_has_key
, METH_VARARGS
,
1609 "T.has_key(k) -> True if T has a field named k, else False" },
1610 { "items", typy_items
, METH_NOARGS
,
1611 "items () -> list\n\
1612 Return a list of (name, field) pairs of this type.\n\
1613 Each field is a gdb.Field object." },
1614 { "iteritems", typy_iteritems
, METH_NOARGS
,
1615 "iteritems () -> an iterator over the (name, field)\n\
1616 pairs of this type. Each field is a gdb.Field object." },
1617 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1618 "iterkeys () -> an iterator over the field names of this type." },
1619 { "itervalues", typy_itervalues
, METH_NOARGS
,
1620 "itervalues () -> an iterator over the fields of this type.\n\
1621 Each field is a gdb.Field object." },
1622 { "keys", typy_field_names
, METH_NOARGS
,
1624 Return a list holding all the fields names of this type." },
1625 { "pointer", typy_pointer
, METH_NOARGS
,
1626 "pointer () -> Type\n\
1627 Return a type of pointer to this type." },
1628 { "range", typy_range
, METH_NOARGS
,
1629 "range () -> tuple\n\
1630 Return a tuple containing the lower and upper range for this type."},
1631 { "reference", typy_reference
, METH_NOARGS
,
1632 "reference () -> Type\n\
1633 Return a type of reference to this type." },
1634 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1635 "strip_typedefs () -> Type\n\
1636 Return a type formed by stripping this type of all typedefs."},
1637 { "target", typy_target
, METH_NOARGS
,
1638 "target () -> Type\n\
1639 Return the target type of this type." },
1640 { "template_argument", typy_template_argument
, METH_VARARGS
,
1641 "template_argument (arg, [block]) -> Type\n\
1642 Return the type of a template argument." },
1643 { "unqualified", typy_unqualified
, METH_NOARGS
,
1644 "unqualified () -> Type\n\
1645 Return a variant of this type without const or volatile attributes." },
1646 { "values", typy_values
, METH_NOARGS
,
1647 "values () -> list\n\
1648 Return a list holding all the fields of this type.\n\
1649 Each field is a gdb.Field object." },
1650 { "volatile", typy_volatile
, METH_NOARGS
,
1651 "volatile () -> Type\n\
1652 Return a volatile variant of this type" },
1656 static PyNumberMethods type_object_as_number
= {
1658 NULL
, /* nb_subtract */
1659 NULL
, /* nb_multiply */
1660 NULL
, /* nb_divide */
1661 NULL
, /* nb_remainder */
1662 NULL
, /* nb_divmod */
1663 NULL
, /* nb_power */
1664 NULL
, /* nb_negative */
1665 NULL
, /* nb_positive */
1666 NULL
, /* nb_absolute */
1667 typy_nonzero
, /* nb_nonzero */
1668 NULL
, /* nb_invert */
1669 NULL
, /* nb_lshift */
1670 NULL
, /* nb_rshift */
1674 NULL
, /* nb_coerce */
1677 NULL
, /* nb_float */
1682 static PyMappingMethods typy_mapping
= {
1685 NULL
/* no "set" method */
1688 static PyTypeObject type_object_type
=
1690 PyObject_HEAD_INIT (NULL
)
1692 "gdb.Type", /*tp_name*/
1693 sizeof (type_object
), /*tp_basicsize*/
1695 typy_dealloc
, /*tp_dealloc*/
1701 &type_object_as_number
, /*tp_as_number*/
1702 0, /*tp_as_sequence*/
1703 &typy_mapping
, /*tp_as_mapping*/
1706 typy_str
, /*tp_str*/
1710 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1711 "GDB type object", /* tp_doc */
1712 0, /* tp_traverse */
1714 typy_richcompare
, /* tp_richcompare */
1715 0, /* tp_weaklistoffset */
1716 typy_iter
, /* tp_iter */
1717 0, /* tp_iternext */
1718 type_object_methods
, /* tp_methods */
1720 type_object_getset
, /* tp_getset */
1723 0, /* tp_descr_get */
1724 0, /* tp_descr_set */
1725 0, /* tp_dictoffset */
1731 static PyGetSetDef field_object_getset
[] =
1733 { "__dict__", gdb_py_generic_dict
, NULL
,
1734 "The __dict__ for this field.", &field_object_type
},
1738 static PyTypeObject field_object_type
=
1740 PyObject_HEAD_INIT (NULL
)
1742 "gdb.Field", /*tp_name*/
1743 sizeof (field_object
), /*tp_basicsize*/
1745 field_dealloc
, /*tp_dealloc*/
1752 0, /*tp_as_sequence*/
1753 0, /*tp_as_mapping*/
1760 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1761 "GDB field object", /* tp_doc */
1762 0, /* tp_traverse */
1764 0, /* tp_richcompare */
1765 0, /* tp_weaklistoffset */
1767 0, /* tp_iternext */
1770 field_object_getset
, /* tp_getset */
1773 0, /* tp_descr_get */
1774 0, /* tp_descr_set */
1775 offsetof (field_object
, dict
), /* tp_dictoffset */
1781 static PyTypeObject type_iterator_object_type
= {
1782 PyObject_HEAD_INIT (NULL
)
1784 "gdb.TypeIterator", /*tp_name*/
1785 sizeof (typy_iterator_object
), /*tp_basicsize*/
1787 typy_iterator_dealloc
, /*tp_dealloc*/
1794 0, /*tp_as_sequence*/
1795 0, /*tp_as_mapping*/
1802 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1803 "GDB type iterator object", /*tp_doc */
1806 0, /*tp_richcompare */
1807 0, /*tp_weaklistoffset */
1808 typy_iterator_iter
, /*tp_iter */
1809 typy_iterator_iternext
, /*tp_iternext */