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"
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 const char *attrstring
;
172 if (TYPE_CODE (type
) == TYPE_CODE_ENUM
)
174 arg
= gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type
, field
));
175 attrstring
= "enumval";
179 arg
= gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type
, field
));
180 attrstring
= "bitpos";
186 /* At least python-2.4 had the second parameter non-const. */
187 if (PyObject_SetAttrString (result
, (char *) attrstring
, arg
) < 0)
191 if (TYPE_FIELD_NAME (type
, field
))
192 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
200 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
203 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
205 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
208 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
209 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
213 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
216 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
219 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
222 /* A field can have a NULL type in some situations. */
223 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
229 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
232 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
244 /* Helper function to return the name of a field, as a gdb.Field object.
245 If the field doesn't have a name, None is returned. */
248 field_name (struct type
*type
, int field
)
252 if (TYPE_FIELD_NAME (type
, field
))
253 result
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
262 /* Helper function for Type standard mapping methods. Returns a
263 Python object for field i of the type. "kind" specifies what to
264 return: the name of the field, a gdb.Field object corresponding to
265 the field, or a tuple consisting of field name and gdb.Field
269 make_fielditem (struct type
*type
, int i
, enum gdbpy_iter_kind kind
)
271 PyObject
*item
= NULL
, *key
= NULL
, *value
= NULL
;
276 key
= field_name (type
, i
);
279 value
= convert_field (type
, i
);
282 item
= PyTuple_New (2);
285 PyTuple_SET_ITEM (item
, 0, key
);
286 PyTuple_SET_ITEM (item
, 1, value
);
289 item
= field_name (type
, i
);
292 item
= convert_field (type
, i
);
304 /* Return a sequence of all field names, fields, or (name, field) pairs.
305 Each field is a gdb.Field object. */
308 typy_fields_items (PyObject
*self
, enum gdbpy_iter_kind kind
)
310 PyObject
*py_type
= self
;
311 PyObject
*result
= NULL
, *iter
= NULL
;
312 volatile struct gdb_exception except
;
313 struct type
*type
= ((type_object
*) py_type
)->type
;
314 struct type
*checked_type
= type
;
316 TRY_CATCH (except
, RETURN_MASK_ALL
)
318 CHECK_TYPEDEF (checked_type
);
320 GDB_PY_HANDLE_EXCEPTION (except
);
322 if (checked_type
!= type
)
323 py_type
= type_to_type_object (checked_type
);
324 iter
= typy_make_iter (py_type
, kind
);
325 if (checked_type
!= type
)
327 /* Need to wrap this in braces because Py_DECREF isn't wrapped
328 in a do{}while(0). */
333 result
= PySequence_List (iter
);
340 /* Return a sequence of all fields. Each field is a gdb.Field object. */
343 typy_values (PyObject
*self
, PyObject
*args
)
345 return typy_fields_items (self
, iter_values
);
348 /* Return a sequence of all fields. Each field is a gdb.Field object.
349 This method is similar to typy_values, except where the supplied
350 gdb.Type is an array, in which case it returns a list of one entry
351 which is a gdb.Field object for a range (the array bounds). */
354 typy_fields (PyObject
*self
, PyObject
*args
)
356 struct type
*type
= ((type_object
*) self
)->type
;
359 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
360 return typy_fields_items (self
, iter_values
);
362 /* Array type. Handle this as a special case because the common
363 machinery wants struct or union or enum types. Build a list of
364 one entry which is the range for the array. */
365 r
= convert_field (type
, 0);
369 rl
= Py_BuildValue ("[O]", r
);
378 /* Return a sequence of all field names. Each field is a gdb.Field object. */
381 typy_field_names (PyObject
*self
, PyObject
*args
)
383 return typy_fields_items (self
, iter_keys
);
386 /* Return a sequence of all (name, fields) pairs. Each field is a
390 typy_items (PyObject
*self
, PyObject
*args
)
392 return typy_fields_items (self
, iter_items
);
395 /* Return the type's tag, or None. */
397 typy_get_tag (PyObject
*self
, void *closure
)
399 struct type
*type
= ((type_object
*) self
)->type
;
401 if (!TYPE_TAG_NAME (type
))
403 return PyString_FromString (TYPE_TAG_NAME (type
));
406 /* Return the type, stripped of typedefs. */
408 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
410 struct type
*type
= ((type_object
*) self
)->type
;
411 volatile struct gdb_exception except
;
413 TRY_CATCH (except
, RETURN_MASK_ALL
)
415 type
= check_typedef (type
);
417 GDB_PY_HANDLE_EXCEPTION (except
);
419 return type_to_type_object (check_typedef (type
));
422 /* Strip typedefs and pointers/reference from a type. Then check that
423 it is a struct, union, or enum type. If not, raise TypeError. */
426 typy_get_composite (struct type
*type
)
428 volatile struct gdb_exception except
;
432 TRY_CATCH (except
, RETURN_MASK_ALL
)
434 CHECK_TYPEDEF (type
);
436 /* Don't use GDB_PY_HANDLE_EXCEPTION here because that returns
437 a (NULL) pointer of the wrong type. */
438 if (except
.reason
< 0)
440 gdbpy_convert_exception (except
);
444 if (TYPE_CODE (type
) != TYPE_CODE_PTR
445 && TYPE_CODE (type
) != TYPE_CODE_REF
)
447 type
= TYPE_TARGET_TYPE (type
);
450 /* If this is not a struct, union, or enum type, raise TypeError
452 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
453 && TYPE_CODE (type
) != TYPE_CODE_UNION
454 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
456 PyErr_SetString (PyExc_TypeError
,
457 "Type is not a structure, union, or enum type.");
464 /* Return an array type. */
467 typy_array (PyObject
*self
, PyObject
*args
)
470 PyObject
*n2_obj
= NULL
;
471 struct type
*array
= NULL
;
472 struct type
*type
= ((type_object
*) self
)->type
;
473 volatile struct gdb_exception except
;
475 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
480 if (!PyInt_Check (n2_obj
))
482 PyErr_SetString (PyExc_RuntimeError
,
483 _("Array bound must be an integer"));
487 if (! gdb_py_int_as_long (n2_obj
, &n2
))
498 PyErr_SetString (PyExc_ValueError
,
499 _("Array length must not be negative"));
503 TRY_CATCH (except
, RETURN_MASK_ALL
)
505 array
= lookup_array_range_type (type
, n1
, n2
);
507 GDB_PY_HANDLE_EXCEPTION (except
);
509 return type_to_type_object (array
);
512 /* Return a Type object which represents a pointer to SELF. */
514 typy_pointer (PyObject
*self
, PyObject
*args
)
516 struct type
*type
= ((type_object
*) self
)->type
;
517 volatile struct gdb_exception except
;
519 TRY_CATCH (except
, RETURN_MASK_ALL
)
521 type
= lookup_pointer_type (type
);
523 GDB_PY_HANDLE_EXCEPTION (except
);
525 return type_to_type_object (type
);
528 /* Return the range of a type represented by SELF. The return type is
529 a tuple. The first element of the tuple contains the low bound,
530 while the second element of the tuple contains the high bound. */
532 typy_range (PyObject
*self
, PyObject
*args
)
534 struct type
*type
= ((type_object
*) self
)->type
;
536 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
537 /* Initialize these to appease GCC warnings. */
538 LONGEST low
= 0, high
= 0;
540 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
541 && TYPE_CODE (type
) != TYPE_CODE_STRING
542 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
544 PyErr_SetString (PyExc_RuntimeError
,
545 _("This type does not have a range."));
549 switch (TYPE_CODE (type
))
551 case TYPE_CODE_ARRAY
:
552 case TYPE_CODE_STRING
:
553 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
554 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
556 case TYPE_CODE_RANGE
:
557 low
= TYPE_LOW_BOUND (type
);
558 high
= TYPE_HIGH_BOUND (type
);
562 low_bound
= PyLong_FromLong (low
);
566 high_bound
= PyLong_FromLong (high
);
570 result
= PyTuple_New (2);
574 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
579 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
581 Py_DECREF (high_bound
);
588 Py_XDECREF (high_bound
);
589 Py_XDECREF (low_bound
);
593 /* Return a Type object which represents a reference to SELF. */
595 typy_reference (PyObject
*self
, PyObject
*args
)
597 struct type
*type
= ((type_object
*) self
)->type
;
598 volatile struct gdb_exception except
;
600 TRY_CATCH (except
, RETURN_MASK_ALL
)
602 type
= lookup_reference_type (type
);
604 GDB_PY_HANDLE_EXCEPTION (except
);
606 return type_to_type_object (type
);
609 /* Return a Type object which represents the target type of SELF. */
611 typy_target (PyObject
*self
, PyObject
*args
)
613 struct type
*type
= ((type_object
*) self
)->type
;
615 if (!TYPE_TARGET_TYPE (type
))
617 PyErr_SetString (PyExc_RuntimeError
,
618 _("Type does not have a target."));
622 return type_to_type_object (TYPE_TARGET_TYPE (type
));
625 /* Return a const-qualified type variant. */
627 typy_const (PyObject
*self
, PyObject
*args
)
629 struct type
*type
= ((type_object
*) self
)->type
;
630 volatile struct gdb_exception except
;
632 TRY_CATCH (except
, RETURN_MASK_ALL
)
634 type
= make_cv_type (1, 0, type
, NULL
);
636 GDB_PY_HANDLE_EXCEPTION (except
);
638 return type_to_type_object (type
);
641 /* Return a volatile-qualified type variant. */
643 typy_volatile (PyObject
*self
, PyObject
*args
)
645 struct type
*type
= ((type_object
*) self
)->type
;
646 volatile struct gdb_exception except
;
648 TRY_CATCH (except
, RETURN_MASK_ALL
)
650 type
= make_cv_type (0, 1, type
, NULL
);
652 GDB_PY_HANDLE_EXCEPTION (except
);
654 return type_to_type_object (type
);
657 /* Return an unqualified type variant. */
659 typy_unqualified (PyObject
*self
, PyObject
*args
)
661 struct type
*type
= ((type_object
*) self
)->type
;
662 volatile struct gdb_exception except
;
664 TRY_CATCH (except
, RETURN_MASK_ALL
)
666 type
= make_cv_type (0, 0, type
, NULL
);
668 GDB_PY_HANDLE_EXCEPTION (except
);
670 return type_to_type_object (type
);
673 /* Return the size of the type represented by SELF, in bytes. */
675 typy_get_sizeof (PyObject
*self
, void *closure
)
677 struct type
*type
= ((type_object
*) self
)->type
;
678 volatile struct gdb_exception except
;
680 TRY_CATCH (except
, RETURN_MASK_ALL
)
682 check_typedef (type
);
684 /* Ignore exceptions. */
686 return gdb_py_long_from_longest (TYPE_LENGTH (type
));
690 typy_lookup_typename (const char *type_name
, const struct block
*block
)
692 struct type
*type
= NULL
;
693 volatile struct gdb_exception except
;
695 TRY_CATCH (except
, RETURN_MASK_ALL
)
697 if (!strncmp (type_name
, "struct ", 7))
698 type
= lookup_struct (type_name
+ 7, NULL
);
699 else if (!strncmp (type_name
, "union ", 6))
700 type
= lookup_union (type_name
+ 6, NULL
);
701 else if (!strncmp (type_name
, "enum ", 5))
702 type
= lookup_enum (type_name
+ 5, NULL
);
704 type
= lookup_typename (python_language
, python_gdbarch
,
705 type_name
, block
, 0);
707 if (except
.reason
< 0)
709 gdbpy_convert_exception (except
);
717 typy_lookup_type (struct demangle_component
*demangled
,
718 const struct block
*block
)
720 struct type
*type
, *rtype
= NULL
;
721 char *type_name
= NULL
;
722 enum demangle_component_type demangled_type
;
723 volatile struct gdb_exception except
;
725 /* Save the type: typy_lookup_type() may (indirectly) overwrite
726 memory pointed by demangled. */
727 demangled_type
= demangled
->type
;
729 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
730 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
731 || demangled_type
== DEMANGLE_COMPONENT_CONST
732 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
734 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
738 TRY_CATCH (except
, RETURN_MASK_ALL
)
740 /* If the demangled_type matches with one of the types
741 below, run the corresponding function and save the type
742 to return later. We cannot just return here as we are in
743 an exception handler. */
744 switch (demangled_type
)
746 case DEMANGLE_COMPONENT_REFERENCE
:
747 rtype
= lookup_reference_type (type
);
749 case DEMANGLE_COMPONENT_POINTER
:
750 rtype
= lookup_pointer_type (type
);
752 case DEMANGLE_COMPONENT_CONST
:
753 rtype
= make_cv_type (1, 0, type
, NULL
);
755 case DEMANGLE_COMPONENT_VOLATILE
:
756 rtype
= make_cv_type (0, 1, type
, NULL
);
760 if (except
.reason
< 0)
762 gdbpy_convert_exception (except
);
767 /* If we have a type from the switch statement above, just return
772 /* We don't have a type, so lookup the type. */
773 type_name
= cp_comp_to_string (demangled
, 10);
774 type
= typy_lookup_typename (type_name
, block
);
780 /* This is a helper function for typy_template_argument that is used
781 when the type does not have template symbols attached. It works by
782 parsing the type name. This happens with compilers, like older
783 versions of GCC, that do not emit DW_TAG_template_*. */
786 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
790 struct demangle_component
*demangled
;
791 struct demangle_parse_info
*info
= NULL
;
793 struct type
*argtype
;
794 struct cleanup
*cleanup
;
795 volatile struct gdb_exception except
;
797 if (TYPE_NAME (type
) == NULL
)
799 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
803 TRY_CATCH (except
, RETURN_MASK_ALL
)
805 /* Note -- this is not thread-safe. */
806 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
808 GDB_PY_HANDLE_EXCEPTION (except
);
812 PyErr_SetString (PyExc_RuntimeError
, err
);
815 demangled
= info
->tree
;
816 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
818 /* Strip off component names. */
819 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
820 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
821 demangled
= demangled
->u
.s_binary
.right
;
823 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
825 do_cleanups (cleanup
);
826 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
830 /* Skip from the template to the arguments. */
831 demangled
= demangled
->u
.s_binary
.right
;
833 for (i
= 0; demangled
&& i
< argno
; ++i
)
834 demangled
= demangled
->u
.s_binary
.right
;
838 do_cleanups (cleanup
);
839 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
844 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
845 do_cleanups (cleanup
);
849 return type_to_type_object (argtype
);
853 typy_template_argument (PyObject
*self
, PyObject
*args
)
856 struct type
*type
= ((type_object
*) self
)->type
;
857 const struct block
*block
= NULL
;
858 PyObject
*block_obj
= NULL
;
860 struct value
*val
= NULL
;
861 volatile struct gdb_exception except
;
863 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
868 block
= block_object_to_block (block_obj
);
871 PyErr_SetString (PyExc_RuntimeError
,
872 _("Second argument must be block."));
877 TRY_CATCH (except
, RETURN_MASK_ALL
)
879 type
= check_typedef (type
);
880 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
881 type
= check_typedef (TYPE_TARGET_TYPE (type
));
883 GDB_PY_HANDLE_EXCEPTION (except
);
885 /* We might not have DW_TAG_template_*, so try to parse the type's
886 name. This is inefficient if we do not have a template type --
887 but that is going to wind up as an error anyhow. */
888 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
889 return typy_legacy_template_argument (type
, block
, argno
);
891 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
893 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
898 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
899 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
900 return type_to_type_object (SYMBOL_TYPE (sym
));
901 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
903 PyErr_Format (PyExc_RuntimeError
,
904 _("Template argument is optimized out"));
908 TRY_CATCH (except
, RETURN_MASK_ALL
)
910 val
= value_of_variable (sym
, block
);
912 GDB_PY_HANDLE_EXCEPTION (except
);
914 return value_to_value_object (val
);
918 typy_str (PyObject
*self
)
920 volatile struct gdb_exception except
;
921 char *thetype
= NULL
;
925 TRY_CATCH (except
, RETURN_MASK_ALL
)
927 struct cleanup
*old_chain
;
930 stb
= mem_fileopen ();
931 old_chain
= make_cleanup_ui_file_delete (stb
);
933 type_print (type_object_to_type (self
), "", stb
, -1);
935 thetype
= ui_file_xstrdup (stb
, &length
);
936 do_cleanups (old_chain
);
938 if (except
.reason
< 0)
941 GDB_PY_HANDLE_EXCEPTION (except
);
944 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
950 /* An entry in the type-equality bcache. */
952 typedef struct type_equality_entry
954 struct type
*type1
, *type2
;
955 } type_equality_entry_d
;
957 DEF_VEC_O (type_equality_entry_d
);
959 /* A helper function to compare two strings. Returns 1 if they are
960 the same, 0 otherwise. Handles NULLs properly. */
963 compare_maybe_null_strings (const char *s
, const char *t
)
965 if (s
== NULL
&& t
!= NULL
)
967 else if (s
!= NULL
&& t
== NULL
)
969 else if (s
== NULL
&& t
== NULL
)
971 return strcmp (s
, t
) == 0;
974 /* A helper function for typy_richcompare that checks two types for
975 "deep" equality. Returns Py_EQ if the types are considered the
976 same, Py_NE otherwise. */
979 check_types_equal (struct type
*type1
, struct type
*type2
,
980 VEC (type_equality_entry_d
) **worklist
)
982 CHECK_TYPEDEF (type1
);
983 CHECK_TYPEDEF (type2
);
988 if (TYPE_CODE (type1
) != TYPE_CODE (type2
)
989 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
990 || TYPE_UNSIGNED (type1
) != TYPE_UNSIGNED (type2
)
991 || TYPE_NOSIGN (type1
) != TYPE_NOSIGN (type2
)
992 || TYPE_VARARGS (type1
) != TYPE_VARARGS (type2
)
993 || TYPE_VECTOR (type1
) != TYPE_VECTOR (type2
)
994 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
995 || TYPE_INSTANCE_FLAGS (type1
) != TYPE_INSTANCE_FLAGS (type2
)
996 || TYPE_NFIELDS (type1
) != TYPE_NFIELDS (type2
))
999 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1
),
1000 TYPE_TAG_NAME (type2
)))
1002 if (!compare_maybe_null_strings (TYPE_NAME (type1
), TYPE_NAME (type2
)))
1005 if (TYPE_CODE (type1
) == TYPE_CODE_RANGE
)
1007 if (memcmp (TYPE_RANGE_DATA (type1
), TYPE_RANGE_DATA (type2
),
1008 sizeof (*TYPE_RANGE_DATA (type1
))) != 0)
1015 for (i
= 0; i
< TYPE_NFIELDS (type1
); ++i
)
1017 const struct field
*field1
= &TYPE_FIELD (type1
, i
);
1018 const struct field
*field2
= &TYPE_FIELD (type2
, i
);
1019 struct type_equality_entry entry
;
1021 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
1022 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
1023 || FIELD_LOC_KIND (*field1
) != FIELD_LOC_KIND (*field2
))
1025 if (!compare_maybe_null_strings (FIELD_NAME (*field1
),
1026 FIELD_NAME (*field2
)))
1028 switch (FIELD_LOC_KIND (*field1
))
1030 case FIELD_LOC_KIND_BITPOS
:
1031 if (FIELD_BITPOS (*field1
) != FIELD_BITPOS (*field2
))
1034 case FIELD_LOC_KIND_ENUMVAL
:
1035 if (FIELD_ENUMVAL (*field1
) != FIELD_ENUMVAL (*field2
))
1038 case FIELD_LOC_KIND_PHYSADDR
:
1039 if (FIELD_STATIC_PHYSADDR (*field1
)
1040 != FIELD_STATIC_PHYSADDR (*field2
))
1043 case FIELD_LOC_KIND_PHYSNAME
:
1044 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1
),
1045 FIELD_STATIC_PHYSNAME (*field2
)))
1048 case FIELD_LOC_KIND_DWARF_BLOCK
:
1050 struct dwarf2_locexpr_baton
*block1
, *block2
;
1052 block1
= FIELD_DWARF_BLOCK (*field1
);
1053 block2
= FIELD_DWARF_BLOCK (*field2
);
1054 if (block1
->per_cu
!= block2
->per_cu
1055 || block1
->size
!= block2
->size
1056 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
1061 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
1062 "%d by check_types_equal"),
1063 FIELD_LOC_KIND (*field1
));
1066 entry
.type1
= FIELD_TYPE (*field1
);
1067 entry
.type2
= FIELD_TYPE (*field2
);
1068 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
1072 if (TYPE_TARGET_TYPE (type1
) != NULL
)
1074 struct type_equality_entry entry
;
1076 if (TYPE_TARGET_TYPE (type2
) == NULL
)
1079 entry
.type1
= TYPE_TARGET_TYPE (type1
);
1080 entry
.type2
= TYPE_TARGET_TYPE (type2
);
1081 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
1083 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
1089 /* Check types on a worklist for equality. Returns Py_NE if any pair
1090 is not equal, Py_EQ if they are all considered equal. */
1093 check_types_worklist (VEC (type_equality_entry_d
) **worklist
,
1094 struct bcache
*cache
)
1096 while (!VEC_empty (type_equality_entry_d
, *worklist
))
1098 struct type_equality_entry entry
;
1101 entry
= *VEC_last (type_equality_entry_d
, *worklist
);
1102 VEC_pop (type_equality_entry_d
, *worklist
);
1104 /* If the type pair has already been visited, we know it is
1106 bcache_full (&entry
, sizeof (entry
), cache
, &added
);
1110 if (check_types_equal (entry
.type1
, entry
.type2
, worklist
) == Py_NE
)
1117 /* Implement the richcompare method. */
1120 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1123 struct type
*type1
= type_object_to_type (self
);
1124 struct type
*type2
= type_object_to_type (other
);
1125 volatile struct gdb_exception except
;
1127 /* We can only compare ourselves to another Type object, and only
1128 for equality or inequality. */
1129 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
1131 Py_INCREF (Py_NotImplemented
);
1132 return Py_NotImplemented
;
1139 struct bcache
*cache
;
1140 VEC (type_equality_entry_d
) *worklist
= NULL
;
1141 struct type_equality_entry entry
;
1143 cache
= bcache_xmalloc (NULL
, NULL
);
1145 entry
.type1
= type1
;
1146 entry
.type2
= type2
;
1147 VEC_safe_push (type_equality_entry_d
, worklist
, &entry
);
1149 TRY_CATCH (except
, RETURN_MASK_ALL
)
1151 result
= check_types_worklist (&worklist
, cache
);
1153 /* check_types_worklist calls several nested Python helper
1154 functions, some of which can raise a GDB Exception, so we
1155 just check and convert here. If there is a GDB exception, a
1156 comparison is not capable (or trusted), so exit. */
1157 bcache_xfree (cache
);
1158 VEC_free (type_equality_entry_d
, worklist
);
1159 GDB_PY_HANDLE_EXCEPTION (except
);
1169 static const struct objfile_data
*typy_objfile_data_key
;
1172 save_objfile_types (struct objfile
*objfile
, void *datum
)
1174 type_object
*obj
= datum
;
1175 htab_t copied_types
;
1176 struct cleanup
*cleanup
;
1178 /* This prevents another thread from freeing the objects we're
1180 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1182 copied_types
= create_copied_types_hash (objfile
);
1186 type_object
*next
= obj
->next
;
1188 htab_empty (copied_types
);
1190 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1198 htab_delete (copied_types
);
1200 do_cleanups (cleanup
);
1204 set_type (type_object
*obj
, struct type
*type
)
1208 if (type
&& TYPE_OBJFILE (type
))
1210 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1212 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
1214 obj
->next
->prev
= obj
;
1215 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1222 typy_dealloc (PyObject
*obj
)
1224 type_object
*type
= (type_object
*) obj
;
1227 type
->prev
->next
= type
->next
;
1228 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1230 /* Must reset head of list. */
1231 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1234 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1237 type
->next
->prev
= type
->prev
;
1239 type
->ob_type
->tp_free (type
);
1242 /* Return number of fields ("length" of the field dictionary). */
1245 typy_length (PyObject
*self
)
1247 struct type
*type
= ((type_object
*) self
)->type
;
1249 type
= typy_get_composite (type
);
1253 return TYPE_NFIELDS (type
);
1256 /* Implements boolean evaluation of gdb.Type. Handle this like other
1257 Python objects that don't have a meaningful truth value -- all
1261 typy_nonzero (PyObject
*self
)
1266 /* Return a gdb.Field object for the field named by the argument. */
1269 typy_getitem (PyObject
*self
, PyObject
*key
)
1271 struct type
*type
= ((type_object
*) self
)->type
;
1275 field
= python_string_to_host_string (key
);
1279 /* We want just fields of this type, not of base types, so instead of
1280 using lookup_struct_elt_type, portions of that function are
1283 type
= typy_get_composite (type
);
1287 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1289 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1291 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1293 return convert_field (type
, i
);
1296 PyErr_SetObject (PyExc_KeyError
, key
);
1300 /* Implement the "get" method on the type object. This is the
1301 same as getitem if the key is present, but returns the supplied
1302 default value or None if the key is not found. */
1305 typy_get (PyObject
*self
, PyObject
*args
)
1307 PyObject
*key
, *defval
= Py_None
, *result
;
1309 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1312 result
= typy_getitem (self
, key
);
1316 /* typy_getitem returned error status. If the exception is
1317 KeyError, clear the exception status and return the defval
1318 instead. Otherwise return the exception unchanged. */
1319 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1327 /* Implement the "has_key" method on the type object. */
1330 typy_has_key (PyObject
*self
, PyObject
*args
)
1332 struct type
*type
= ((type_object
*) self
)->type
;
1336 if (!PyArg_ParseTuple (args
, "s", &field
))
1339 /* We want just fields of this type, not of base types, so instead of
1340 using lookup_struct_elt_type, portions of that function are
1343 type
= typy_get_composite (type
);
1347 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1349 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1351 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1357 /* Make an iterator object to iterate over keys, values, or items. */
1360 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1362 typy_iterator_object
*typy_iter_obj
;
1364 /* Check that "self" is a structure or union type. */
1365 if (typy_get_composite (((type_object
*) self
)->type
) == NULL
)
1368 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1369 &type_iterator_object_type
);
1370 if (typy_iter_obj
== NULL
)
1373 typy_iter_obj
->field
= 0;
1374 typy_iter_obj
->kind
= kind
;
1376 typy_iter_obj
->source
= (type_object
*) self
;
1378 return (PyObject
*) typy_iter_obj
;
1381 /* iteritems() method. */
1384 typy_iteritems (PyObject
*self
, PyObject
*args
)
1386 return typy_make_iter (self
, iter_items
);
1389 /* iterkeys() method. */
1392 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1394 return typy_make_iter (self
, iter_keys
);
1397 /* Iterating over the class, same as iterkeys except for the function
1401 typy_iter (PyObject
*self
)
1403 return typy_make_iter (self
, iter_keys
);
1406 /* itervalues() method. */
1409 typy_itervalues (PyObject
*self
, PyObject
*args
)
1411 return typy_make_iter (self
, iter_values
);
1414 /* Return a reference to the type iterator. */
1417 typy_iterator_iter (PyObject
*self
)
1423 /* Return the next field in the iteration through the list of fields
1427 typy_iterator_iternext (PyObject
*self
)
1429 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1430 struct type
*type
= iter_obj
->source
->type
;
1433 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1435 result
= make_fielditem (type
, iter_obj
->field
, iter_obj
->kind
);
1445 typy_iterator_dealloc (PyObject
*obj
)
1447 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1449 Py_DECREF (iter_obj
->source
);
1452 /* Create a new Type referring to TYPE. */
1454 type_to_type_object (struct type
*type
)
1456 type_object
*type_obj
;
1458 type_obj
= PyObject_New (type_object
, &type_object_type
);
1460 set_type (type_obj
, type
);
1462 return (PyObject
*) type_obj
;
1466 type_object_to_type (PyObject
*obj
)
1468 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1470 return ((type_object
*) obj
)->type
;
1475 /* Implementation of gdb.lookup_type. */
1477 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1479 static char *keywords
[] = { "name", "block", NULL
};
1480 const char *type_name
= NULL
;
1481 struct type
*type
= NULL
;
1482 PyObject
*block_obj
= NULL
;
1483 const struct block
*block
= NULL
;
1485 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1486 &type_name
, &block_obj
))
1491 block
= block_object_to_block (block_obj
);
1494 PyErr_SetString (PyExc_RuntimeError
,
1495 _("'block' argument must be a Block."));
1500 type
= typy_lookup_typename (type_name
, block
);
1504 return (PyObject
*) type_to_type_object (type
);
1508 gdbpy_initialize_types (void)
1512 typy_objfile_data_key
1513 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1515 if (PyType_Ready (&type_object_type
) < 0)
1517 if (PyType_Ready (&field_object_type
) < 0)
1519 if (PyType_Ready (&type_iterator_object_type
) < 0)
1522 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1524 if (PyModule_AddIntConstant (gdb_module
,
1525 /* Cast needed for Python 2.4. */
1526 (char *) pyty_codes
[i
].name
,
1527 pyty_codes
[i
].code
) < 0)
1531 Py_INCREF (&type_object_type
);
1532 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
1534 Py_INCREF (&type_iterator_object_type
);
1535 PyModule_AddObject (gdb_module
, "TypeIterator",
1536 (PyObject
*) &type_iterator_object_type
);
1538 Py_INCREF (&field_object_type
);
1539 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
1544 static PyGetSetDef type_object_getset
[] =
1546 { "code", typy_get_code
, NULL
,
1547 "The code for this type.", NULL
},
1548 { "sizeof", typy_get_sizeof
, NULL
,
1549 "The size of this type, in bytes.", NULL
},
1550 { "tag", typy_get_tag
, NULL
,
1551 "The tag name for this type, or None.", NULL
},
1555 static PyMethodDef type_object_methods
[] =
1557 { "array", typy_array
, METH_VARARGS
,
1558 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1559 Return a type which represents an array of objects of this type.\n\
1560 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1561 If LOW_BOUND is omitted, a value of zero is used." },
1562 { "__contains__", typy_has_key
, METH_VARARGS
,
1563 "T.__contains__(k) -> True if T has a field named k, else False" },
1564 { "const", typy_const
, METH_NOARGS
,
1565 "const () -> Type\n\
1566 Return a const variant of this type." },
1567 { "fields", typy_fields
, METH_NOARGS
,
1568 "fields () -> list\n\
1569 Return a list holding all the fields of this type.\n\
1570 Each field is a gdb.Field object." },
1571 { "get", typy_get
, METH_VARARGS
,
1572 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1573 otherwise returns default, if supplied, or None if not." },
1574 { "has_key", typy_has_key
, METH_VARARGS
,
1575 "T.has_key(k) -> True if T has a field named k, else False" },
1576 { "items", typy_items
, METH_NOARGS
,
1577 "items () -> list\n\
1578 Return a list of (name, field) pairs of this type.\n\
1579 Each field is a gdb.Field object." },
1580 { "iteritems", typy_iteritems
, METH_NOARGS
,
1581 "iteritems () -> an iterator over the (name, field)\n\
1582 pairs of this type. Each field is a gdb.Field object." },
1583 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1584 "iterkeys () -> an iterator over the field names of this type." },
1585 { "itervalues", typy_itervalues
, METH_NOARGS
,
1586 "itervalues () -> an iterator over the fields of this type.\n\
1587 Each field is a gdb.Field object." },
1588 { "keys", typy_field_names
, METH_NOARGS
,
1590 Return a list holding all the fields names of this type." },
1591 { "pointer", typy_pointer
, METH_NOARGS
,
1592 "pointer () -> Type\n\
1593 Return a type of pointer to this type." },
1594 { "range", typy_range
, METH_NOARGS
,
1595 "range () -> tuple\n\
1596 Return a tuple containing the lower and upper range for this type."},
1597 { "reference", typy_reference
, METH_NOARGS
,
1598 "reference () -> Type\n\
1599 Return a type of reference to this type." },
1600 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1601 "strip_typedefs () -> Type\n\
1602 Return a type formed by stripping this type of all typedefs."},
1603 { "target", typy_target
, METH_NOARGS
,
1604 "target () -> Type\n\
1605 Return the target type of this type." },
1606 { "template_argument", typy_template_argument
, METH_VARARGS
,
1607 "template_argument (arg, [block]) -> Type\n\
1608 Return the type of a template argument." },
1609 { "unqualified", typy_unqualified
, METH_NOARGS
,
1610 "unqualified () -> Type\n\
1611 Return a variant of this type without const or volatile attributes." },
1612 { "values", typy_values
, METH_NOARGS
,
1613 "values () -> list\n\
1614 Return a list holding all the fields of this type.\n\
1615 Each field is a gdb.Field object." },
1616 { "volatile", typy_volatile
, METH_NOARGS
,
1617 "volatile () -> Type\n\
1618 Return a volatile variant of this type" },
1622 static PyNumberMethods type_object_as_number
= {
1624 NULL
, /* nb_subtract */
1625 NULL
, /* nb_multiply */
1626 NULL
, /* nb_divide */
1627 NULL
, /* nb_remainder */
1628 NULL
, /* nb_divmod */
1629 NULL
, /* nb_power */
1630 NULL
, /* nb_negative */
1631 NULL
, /* nb_positive */
1632 NULL
, /* nb_absolute */
1633 typy_nonzero
, /* nb_nonzero */
1634 NULL
, /* nb_invert */
1635 NULL
, /* nb_lshift */
1636 NULL
, /* nb_rshift */
1640 NULL
, /* nb_coerce */
1643 NULL
, /* nb_float */
1648 static PyMappingMethods typy_mapping
= {
1651 NULL
/* no "set" method */
1654 static PyTypeObject type_object_type
=
1656 PyObject_HEAD_INIT (NULL
)
1658 "gdb.Type", /*tp_name*/
1659 sizeof (type_object
), /*tp_basicsize*/
1661 typy_dealloc
, /*tp_dealloc*/
1667 &type_object_as_number
, /*tp_as_number*/
1668 0, /*tp_as_sequence*/
1669 &typy_mapping
, /*tp_as_mapping*/
1672 typy_str
, /*tp_str*/
1676 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1677 "GDB type object", /* tp_doc */
1678 0, /* tp_traverse */
1680 typy_richcompare
, /* tp_richcompare */
1681 0, /* tp_weaklistoffset */
1682 typy_iter
, /* tp_iter */
1683 0, /* tp_iternext */
1684 type_object_methods
, /* tp_methods */
1686 type_object_getset
, /* tp_getset */
1689 0, /* tp_descr_get */
1690 0, /* tp_descr_set */
1691 0, /* tp_dictoffset */
1697 static PyGetSetDef field_object_getset
[] =
1699 { "__dict__", gdb_py_generic_dict
, NULL
,
1700 "The __dict__ for this field.", &field_object_type
},
1704 static PyTypeObject field_object_type
=
1706 PyObject_HEAD_INIT (NULL
)
1708 "gdb.Field", /*tp_name*/
1709 sizeof (field_object
), /*tp_basicsize*/
1711 field_dealloc
, /*tp_dealloc*/
1718 0, /*tp_as_sequence*/
1719 0, /*tp_as_mapping*/
1726 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1727 "GDB field object", /* tp_doc */
1728 0, /* tp_traverse */
1730 0, /* tp_richcompare */
1731 0, /* tp_weaklistoffset */
1733 0, /* tp_iternext */
1736 field_object_getset
, /* tp_getset */
1739 0, /* tp_descr_get */
1740 0, /* tp_descr_set */
1741 offsetof (field_object
, dict
), /* tp_dictoffset */
1747 static PyTypeObject type_iterator_object_type
= {
1748 PyObject_HEAD_INIT (NULL
)
1750 "gdb.TypeIterator", /*tp_name*/
1751 sizeof (typy_iterator_object
), /*tp_basicsize*/
1753 typy_iterator_dealloc
, /*tp_dealloc*/
1760 0, /*tp_as_sequence*/
1761 0, /*tp_as_mapping*/
1768 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1769 "GDB type iterator object", /*tp_doc */
1772 0, /*tp_richcompare */
1773 0, /*tp_weaklistoffset */
1774 typy_iterator_iter
, /*tp_iter */
1775 typy_iterator_iternext
, /*tp_iternext */