1 /* Python interface to types.
3 Copyright (C) 2008-2013 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"
31 #include "typeprint.h"
33 typedef struct pyty_type_object
38 /* If a Type object is associated with an objfile, it is kept on a
39 doubly-linked list, rooted in the objfile. This lets us copy the
40 underlying struct type when the objfile is deleted. */
41 struct pyty_type_object
*prev
;
42 struct pyty_type_object
*next
;
45 static PyTypeObject type_object_type
46 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");
49 typedef struct pyty_field_object
53 /* Dictionary holding our attributes. */
57 static PyTypeObject field_object_type
58 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");
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
72 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
74 /* This is used to initialize various gdb.TYPE_ constants. */
83 /* Forward declarations. */
84 static PyObject
*typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
);
86 #define ENTRY(X) { X, #X }
88 static struct pyty_code pyty_codes
[] =
90 ENTRY (TYPE_CODE_BITSTRING
),
91 ENTRY (TYPE_CODE_PTR
),
92 ENTRY (TYPE_CODE_ARRAY
),
93 ENTRY (TYPE_CODE_STRUCT
),
94 ENTRY (TYPE_CODE_UNION
),
95 ENTRY (TYPE_CODE_ENUM
),
96 ENTRY (TYPE_CODE_FLAGS
),
97 ENTRY (TYPE_CODE_FUNC
),
98 ENTRY (TYPE_CODE_INT
),
99 ENTRY (TYPE_CODE_FLT
),
100 ENTRY (TYPE_CODE_VOID
),
101 ENTRY (TYPE_CODE_SET
),
102 ENTRY (TYPE_CODE_RANGE
),
103 ENTRY (TYPE_CODE_STRING
),
104 ENTRY (TYPE_CODE_ERROR
),
105 ENTRY (TYPE_CODE_METHOD
),
106 ENTRY (TYPE_CODE_METHODPTR
),
107 ENTRY (TYPE_CODE_MEMBERPTR
),
108 ENTRY (TYPE_CODE_REF
),
109 ENTRY (TYPE_CODE_CHAR
),
110 ENTRY (TYPE_CODE_BOOL
),
111 ENTRY (TYPE_CODE_COMPLEX
),
112 ENTRY (TYPE_CODE_TYPEDEF
),
113 ENTRY (TYPE_CODE_NAMESPACE
),
114 ENTRY (TYPE_CODE_DECFLOAT
),
115 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
116 { TYPE_CODE_UNDEF
, NULL
}
122 field_dealloc (PyObject
*obj
)
124 field_object
*f
= (field_object
*) obj
;
126 Py_XDECREF (f
->dict
);
127 Py_TYPE (obj
)->tp_free (obj
);
133 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
137 result
->dict
= PyDict_New ();
144 return (PyObject
*) result
;
149 /* Return the code for this type. */
151 typy_get_code (PyObject
*self
, void *closure
)
153 struct type
*type
= ((type_object
*) self
)->type
;
155 return PyInt_FromLong (TYPE_CODE (type
));
158 /* Helper function for typy_fields which converts a single field to a
159 gdb.Field object. Returns NULL on error. */
162 convert_field (struct type
*type
, int field
)
164 PyObject
*result
= field_new ();
170 if (!field_is_static (&TYPE_FIELD (type
, field
)))
172 const char *attrstring
;
174 if (TYPE_CODE (type
) == TYPE_CODE_ENUM
)
176 arg
= gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type
, field
));
177 attrstring
= "enumval";
181 arg
= gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type
, field
));
182 attrstring
= "bitpos";
188 /* At least python-2.4 had the second parameter non-const. */
189 if (PyObject_SetAttrString (result
, (char *) attrstring
, arg
) < 0)
194 if (TYPE_FIELD_NAME (type
, field
))
195 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
203 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
207 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
209 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
213 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
214 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
218 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
222 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
225 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
229 /* A field can have a NULL type in some situations. */
230 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
236 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
239 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
252 /* Helper function to return the name of a field, as a gdb.Field object.
253 If the field doesn't have a name, None is returned. */
256 field_name (struct type
*type
, int field
)
260 if (TYPE_FIELD_NAME (type
, field
))
261 result
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
270 /* Helper function for Type standard mapping methods. Returns a
271 Python object for field i of the type. "kind" specifies what to
272 return: the name of the field, a gdb.Field object corresponding to
273 the field, or a tuple consisting of field name and gdb.Field
277 make_fielditem (struct type
*type
, int i
, enum gdbpy_iter_kind kind
)
279 PyObject
*item
= NULL
, *key
= NULL
, *value
= NULL
;
284 key
= field_name (type
, i
);
287 value
= convert_field (type
, i
);
290 item
= PyTuple_New (2);
293 PyTuple_SET_ITEM (item
, 0, key
);
294 PyTuple_SET_ITEM (item
, 1, value
);
297 item
= field_name (type
, i
);
300 item
= convert_field (type
, i
);
303 gdb_assert_not_reached ("invalid gdbpy_iter_kind");
314 /* Return a sequence of all field names, fields, or (name, field) pairs.
315 Each field is a gdb.Field object. */
318 typy_fields_items (PyObject
*self
, enum gdbpy_iter_kind kind
)
320 PyObject
*py_type
= self
;
321 PyObject
*result
= NULL
, *iter
= NULL
;
322 volatile struct gdb_exception except
;
323 struct type
*type
= ((type_object
*) py_type
)->type
;
324 struct type
*checked_type
= type
;
326 TRY_CATCH (except
, RETURN_MASK_ALL
)
328 CHECK_TYPEDEF (checked_type
);
330 GDB_PY_HANDLE_EXCEPTION (except
);
332 if (checked_type
!= type
)
333 py_type
= type_to_type_object (checked_type
);
334 iter
= typy_make_iter (py_type
, kind
);
335 if (checked_type
!= type
)
337 /* Need to wrap this in braces because Py_DECREF isn't wrapped
338 in a do{}while(0). */
343 result
= PySequence_List (iter
);
350 /* Return a sequence of all fields. Each field is a gdb.Field object. */
353 typy_values (PyObject
*self
, PyObject
*args
)
355 return typy_fields_items (self
, iter_values
);
358 /* Return a sequence of all fields. Each field is a gdb.Field object.
359 This method is similar to typy_values, except where the supplied
360 gdb.Type is an array, in which case it returns a list of one entry
361 which is a gdb.Field object for a range (the array bounds). */
364 typy_fields (PyObject
*self
, PyObject
*args
)
366 struct type
*type
= ((type_object
*) self
)->type
;
369 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
370 return typy_fields_items (self
, iter_values
);
372 /* Array type. Handle this as a special case because the common
373 machinery wants struct or union or enum types. Build a list of
374 one entry which is the range for the array. */
375 r
= convert_field (type
, 0);
379 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 (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 GDB_PY_HANDLE_EXCEPTION (except
);
445 if (TYPE_CODE (type
) != TYPE_CODE_PTR
446 && TYPE_CODE (type
) != TYPE_CODE_REF
)
448 type
= TYPE_TARGET_TYPE (type
);
451 /* If this is not a struct, union, or enum type, raise TypeError
453 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
454 && TYPE_CODE (type
) != TYPE_CODE_UNION
455 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
457 PyErr_SetString (PyExc_TypeError
,
458 "Type is not a structure, union, or enum type.");
465 /* Helper for typy_array and typy_vector. */
468 typy_array_1 (PyObject
*self
, PyObject
*args
, int is_vector
)
471 PyObject
*n2_obj
= NULL
;
472 struct type
*array
= NULL
;
473 struct type
*type
= ((type_object
*) self
)->type
;
474 volatile struct gdb_exception except
;
476 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
481 if (!PyInt_Check (n2_obj
))
483 PyErr_SetString (PyExc_RuntimeError
,
484 _("Array bound must be an integer"));
488 if (! gdb_py_int_as_long (n2_obj
, &n2
))
499 PyErr_SetString (PyExc_ValueError
,
500 _("Array length must not be negative"));
504 TRY_CATCH (except
, RETURN_MASK_ALL
)
506 array
= lookup_array_range_type (type
, n1
, n2
);
508 make_vector_type (array
);
510 GDB_PY_HANDLE_EXCEPTION (except
);
512 return type_to_type_object (array
);
515 /* Return an array type. */
518 typy_array (PyObject
*self
, PyObject
*args
)
520 return typy_array_1 (self
, args
, 0);
523 /* Return a vector type. */
526 typy_vector (PyObject
*self
, PyObject
*args
)
528 return typy_array_1 (self
, args
, 1);
531 /* Return a Type object which represents a pointer to SELF. */
533 typy_pointer (PyObject
*self
, PyObject
*args
)
535 struct type
*type
= ((type_object
*) self
)->type
;
536 volatile struct gdb_exception except
;
538 TRY_CATCH (except
, RETURN_MASK_ALL
)
540 type
= lookup_pointer_type (type
);
542 GDB_PY_HANDLE_EXCEPTION (except
);
544 return type_to_type_object (type
);
547 /* Return the range of a type represented by SELF. The return type is
548 a tuple. The first element of the tuple contains the low bound,
549 while the second element of the tuple contains the high bound. */
551 typy_range (PyObject
*self
, PyObject
*args
)
553 struct type
*type
= ((type_object
*) self
)->type
;
555 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
556 /* Initialize these to appease GCC warnings. */
557 LONGEST low
= 0, high
= 0;
559 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
560 && TYPE_CODE (type
) != TYPE_CODE_STRING
561 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
563 PyErr_SetString (PyExc_RuntimeError
,
564 _("This type does not have a range."));
568 switch (TYPE_CODE (type
))
570 case TYPE_CODE_ARRAY
:
571 case TYPE_CODE_STRING
:
572 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
573 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
575 case TYPE_CODE_RANGE
:
576 low
= TYPE_LOW_BOUND (type
);
577 high
= TYPE_HIGH_BOUND (type
);
581 low_bound
= PyLong_FromLong (low
);
585 high_bound
= PyLong_FromLong (high
);
589 result
= PyTuple_New (2);
593 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
598 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
600 Py_DECREF (high_bound
);
607 Py_XDECREF (high_bound
);
608 Py_XDECREF (low_bound
);
612 /* Return a Type object which represents a reference to SELF. */
614 typy_reference (PyObject
*self
, PyObject
*args
)
616 struct type
*type
= ((type_object
*) self
)->type
;
617 volatile struct gdb_exception except
;
619 TRY_CATCH (except
, RETURN_MASK_ALL
)
621 type
= lookup_reference_type (type
);
623 GDB_PY_HANDLE_EXCEPTION (except
);
625 return type_to_type_object (type
);
628 /* Return a Type object which represents the target type of SELF. */
630 typy_target (PyObject
*self
, PyObject
*args
)
632 struct type
*type
= ((type_object
*) self
)->type
;
634 if (!TYPE_TARGET_TYPE (type
))
636 PyErr_SetString (PyExc_RuntimeError
,
637 _("Type does not have a target."));
641 return type_to_type_object (TYPE_TARGET_TYPE (type
));
644 /* Return a const-qualified type variant. */
646 typy_const (PyObject
*self
, PyObject
*args
)
648 struct type
*type
= ((type_object
*) self
)->type
;
649 volatile struct gdb_exception except
;
651 TRY_CATCH (except
, RETURN_MASK_ALL
)
653 type
= make_cv_type (1, 0, type
, NULL
);
655 GDB_PY_HANDLE_EXCEPTION (except
);
657 return type_to_type_object (type
);
660 /* Return a volatile-qualified type variant. */
662 typy_volatile (PyObject
*self
, PyObject
*args
)
664 struct type
*type
= ((type_object
*) self
)->type
;
665 volatile struct gdb_exception except
;
667 TRY_CATCH (except
, RETURN_MASK_ALL
)
669 type
= make_cv_type (0, 1, type
, NULL
);
671 GDB_PY_HANDLE_EXCEPTION (except
);
673 return type_to_type_object (type
);
676 /* Return an unqualified type variant. */
678 typy_unqualified (PyObject
*self
, PyObject
*args
)
680 struct type
*type
= ((type_object
*) self
)->type
;
681 volatile struct gdb_exception except
;
683 TRY_CATCH (except
, RETURN_MASK_ALL
)
685 type
= make_cv_type (0, 0, type
, NULL
);
687 GDB_PY_HANDLE_EXCEPTION (except
);
689 return type_to_type_object (type
);
692 /* Return the size of the type represented by SELF, in bytes. */
694 typy_get_sizeof (PyObject
*self
, void *closure
)
696 struct type
*type
= ((type_object
*) self
)->type
;
697 volatile struct gdb_exception except
;
699 TRY_CATCH (except
, RETURN_MASK_ALL
)
701 check_typedef (type
);
703 /* Ignore exceptions. */
705 return gdb_py_long_from_longest (TYPE_LENGTH (type
));
709 typy_lookup_typename (const char *type_name
, const struct block
*block
)
711 struct type
*type
= NULL
;
712 volatile struct gdb_exception except
;
714 TRY_CATCH (except
, RETURN_MASK_ALL
)
716 if (!strncmp (type_name
, "struct ", 7))
717 type
= lookup_struct (type_name
+ 7, NULL
);
718 else if (!strncmp (type_name
, "union ", 6))
719 type
= lookup_union (type_name
+ 6, NULL
);
720 else if (!strncmp (type_name
, "enum ", 5))
721 type
= lookup_enum (type_name
+ 5, NULL
);
723 type
= lookup_typename (python_language
, python_gdbarch
,
724 type_name
, block
, 0);
726 GDB_PY_HANDLE_EXCEPTION (except
);
732 typy_lookup_type (struct demangle_component
*demangled
,
733 const struct block
*block
)
735 struct type
*type
, *rtype
= NULL
;
736 char *type_name
= NULL
;
737 enum demangle_component_type demangled_type
;
738 volatile struct gdb_exception except
;
740 /* Save the type: typy_lookup_type() may (indirectly) overwrite
741 memory pointed by demangled. */
742 demangled_type
= demangled
->type
;
744 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
745 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
746 || demangled_type
== DEMANGLE_COMPONENT_CONST
747 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
749 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
753 TRY_CATCH (except
, RETURN_MASK_ALL
)
755 /* If the demangled_type matches with one of the types
756 below, run the corresponding function and save the type
757 to return later. We cannot just return here as we are in
758 an exception handler. */
759 switch (demangled_type
)
761 case DEMANGLE_COMPONENT_REFERENCE
:
762 rtype
= lookup_reference_type (type
);
764 case DEMANGLE_COMPONENT_POINTER
:
765 rtype
= lookup_pointer_type (type
);
767 case DEMANGLE_COMPONENT_CONST
:
768 rtype
= make_cv_type (1, 0, type
, NULL
);
770 case DEMANGLE_COMPONENT_VOLATILE
:
771 rtype
= make_cv_type (0, 1, type
, NULL
);
775 GDB_PY_HANDLE_EXCEPTION (except
);
778 /* If we have a type from the switch statement above, just return
783 /* We don't have a type, so lookup the type. */
784 type_name
= cp_comp_to_string (demangled
, 10);
785 type
= typy_lookup_typename (type_name
, block
);
791 /* This is a helper function for typy_template_argument that is used
792 when the type does not have template symbols attached. It works by
793 parsing the type name. This happens with compilers, like older
794 versions of GCC, that do not emit DW_TAG_template_*. */
797 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
801 struct demangle_component
*demangled
;
802 struct demangle_parse_info
*info
= NULL
;
804 struct type
*argtype
;
805 struct cleanup
*cleanup
;
806 volatile struct gdb_exception except
;
808 if (TYPE_NAME (type
) == NULL
)
810 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
814 TRY_CATCH (except
, RETURN_MASK_ALL
)
816 /* Note -- this is not thread-safe. */
817 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
819 GDB_PY_HANDLE_EXCEPTION (except
);
823 PyErr_SetString (PyExc_RuntimeError
, err
);
826 demangled
= info
->tree
;
827 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
829 /* Strip off component names. */
830 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
831 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
832 demangled
= demangled
->u
.s_binary
.right
;
834 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
836 do_cleanups (cleanup
);
837 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
841 /* Skip from the template to the arguments. */
842 demangled
= demangled
->u
.s_binary
.right
;
844 for (i
= 0; demangled
&& i
< argno
; ++i
)
845 demangled
= demangled
->u
.s_binary
.right
;
849 do_cleanups (cleanup
);
850 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
855 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
856 do_cleanups (cleanup
);
860 return type_to_type_object (argtype
);
864 typy_template_argument (PyObject
*self
, PyObject
*args
)
867 struct type
*type
= ((type_object
*) self
)->type
;
868 const struct block
*block
= NULL
;
869 PyObject
*block_obj
= NULL
;
871 struct value
*val
= NULL
;
872 volatile struct gdb_exception except
;
874 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
879 block
= block_object_to_block (block_obj
);
882 PyErr_SetString (PyExc_RuntimeError
,
883 _("Second argument must be block."));
888 TRY_CATCH (except
, RETURN_MASK_ALL
)
890 type
= check_typedef (type
);
891 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
892 type
= check_typedef (TYPE_TARGET_TYPE (type
));
894 GDB_PY_HANDLE_EXCEPTION (except
);
896 /* We might not have DW_TAG_template_*, so try to parse the type's
897 name. This is inefficient if we do not have a template type --
898 but that is going to wind up as an error anyhow. */
899 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
900 return typy_legacy_template_argument (type
, block
, argno
);
902 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
904 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
909 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
910 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
911 return type_to_type_object (SYMBOL_TYPE (sym
));
912 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
914 PyErr_Format (PyExc_RuntimeError
,
915 _("Template argument is optimized out"));
919 TRY_CATCH (except
, RETURN_MASK_ALL
)
921 val
= value_of_variable (sym
, block
);
923 GDB_PY_HANDLE_EXCEPTION (except
);
925 return value_to_value_object (val
);
929 typy_str (PyObject
*self
)
931 volatile struct gdb_exception except
;
932 char *thetype
= NULL
;
936 TRY_CATCH (except
, RETURN_MASK_ALL
)
938 struct cleanup
*old_chain
;
941 stb
= mem_fileopen ();
942 old_chain
= make_cleanup_ui_file_delete (stb
);
944 LA_PRINT_TYPE (type_object_to_type (self
), "", stb
, -1, 0,
945 &type_print_raw_options
);
947 thetype
= ui_file_xstrdup (stb
, &length
);
948 do_cleanups (old_chain
);
950 if (except
.reason
< 0)
953 GDB_PY_HANDLE_EXCEPTION (except
);
956 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
962 /* Implement the richcompare method. */
965 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
968 struct type
*type1
= type_object_to_type (self
);
969 struct type
*type2
= type_object_to_type (other
);
970 volatile struct gdb_exception except
;
972 /* We can only compare ourselves to another Type object, and only
973 for equality or inequality. */
974 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
976 Py_INCREF (Py_NotImplemented
);
977 return Py_NotImplemented
;
984 TRY_CATCH (except
, RETURN_MASK_ALL
)
986 result
= types_deeply_equal (type1
, type2
);
988 /* If there is a GDB exception, a comparison is not capable
989 (or trusted), so exit. */
990 GDB_PY_HANDLE_EXCEPTION (except
);
993 if (op
== (result
? Py_EQ
: Py_NE
))
1000 static const struct objfile_data
*typy_objfile_data_key
;
1003 save_objfile_types (struct objfile
*objfile
, void *datum
)
1005 type_object
*obj
= datum
;
1006 htab_t copied_types
;
1007 struct cleanup
*cleanup
;
1009 if (!gdb_python_initialized
)
1012 /* This prevents another thread from freeing the objects we're
1014 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1016 copied_types
= create_copied_types_hash (objfile
);
1020 type_object
*next
= obj
->next
;
1022 htab_empty (copied_types
);
1024 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1032 htab_delete (copied_types
);
1034 do_cleanups (cleanup
);
1038 set_type (type_object
*obj
, struct type
*type
)
1042 if (type
&& TYPE_OBJFILE (type
))
1044 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1046 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
1048 obj
->next
->prev
= obj
;
1049 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1056 typy_dealloc (PyObject
*obj
)
1058 type_object
*type
= (type_object
*) obj
;
1061 type
->prev
->next
= type
->next
;
1062 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1064 /* Must reset head of list. */
1065 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1068 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1071 type
->next
->prev
= type
->prev
;
1073 Py_TYPE (type
)->tp_free (type
);
1076 /* Return number of fields ("length" of the field dictionary). */
1079 typy_length (PyObject
*self
)
1081 struct type
*type
= ((type_object
*) self
)->type
;
1083 type
= typy_get_composite (type
);
1087 return TYPE_NFIELDS (type
);
1090 /* Implements boolean evaluation of gdb.Type. Handle this like other
1091 Python objects that don't have a meaningful truth value -- all
1095 typy_nonzero (PyObject
*self
)
1100 /* Return a gdb.Field object for the field named by the argument. */
1103 typy_getitem (PyObject
*self
, PyObject
*key
)
1105 struct type
*type
= ((type_object
*) self
)->type
;
1109 field
= python_string_to_host_string (key
);
1113 /* We want just fields of this type, not of base types, so instead of
1114 using lookup_struct_elt_type, portions of that function are
1117 type
= typy_get_composite (type
);
1121 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1123 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1125 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1127 return convert_field (type
, i
);
1130 PyErr_SetObject (PyExc_KeyError
, key
);
1134 /* Implement the "get" method on the type object. This is the
1135 same as getitem if the key is present, but returns the supplied
1136 default value or None if the key is not found. */
1139 typy_get (PyObject
*self
, PyObject
*args
)
1141 PyObject
*key
, *defval
= Py_None
, *result
;
1143 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1146 result
= typy_getitem (self
, key
);
1150 /* typy_getitem returned error status. If the exception is
1151 KeyError, clear the exception status and return the defval
1152 instead. Otherwise return the exception unchanged. */
1153 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1161 /* Implement the "has_key" method on the type object. */
1164 typy_has_key (PyObject
*self
, PyObject
*args
)
1166 struct type
*type
= ((type_object
*) self
)->type
;
1170 if (!PyArg_ParseTuple (args
, "s", &field
))
1173 /* We want just fields of this type, not of base types, so instead of
1174 using lookup_struct_elt_type, portions of that function are
1177 type
= typy_get_composite (type
);
1181 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1183 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1185 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1191 /* Make an iterator object to iterate over keys, values, or items. */
1194 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1196 typy_iterator_object
*typy_iter_obj
;
1198 /* Check that "self" is a structure or union type. */
1199 if (typy_get_composite (((type_object
*) self
)->type
) == NULL
)
1202 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1203 &type_iterator_object_type
);
1204 if (typy_iter_obj
== NULL
)
1207 typy_iter_obj
->field
= 0;
1208 typy_iter_obj
->kind
= kind
;
1210 typy_iter_obj
->source
= (type_object
*) self
;
1212 return (PyObject
*) typy_iter_obj
;
1215 /* iteritems() method. */
1218 typy_iteritems (PyObject
*self
, PyObject
*args
)
1220 return typy_make_iter (self
, iter_items
);
1223 /* iterkeys() method. */
1226 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1228 return typy_make_iter (self
, iter_keys
);
1231 /* Iterating over the class, same as iterkeys except for the function
1235 typy_iter (PyObject
*self
)
1237 return typy_make_iter (self
, iter_keys
);
1240 /* itervalues() method. */
1243 typy_itervalues (PyObject
*self
, PyObject
*args
)
1245 return typy_make_iter (self
, iter_values
);
1248 /* Return a reference to the type iterator. */
1251 typy_iterator_iter (PyObject
*self
)
1257 /* Return the next field in the iteration through the list of fields
1261 typy_iterator_iternext (PyObject
*self
)
1263 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1264 struct type
*type
= iter_obj
->source
->type
;
1267 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1269 result
= make_fielditem (type
, iter_obj
->field
, iter_obj
->kind
);
1279 typy_iterator_dealloc (PyObject
*obj
)
1281 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1283 Py_DECREF (iter_obj
->source
);
1286 /* Create a new Type referring to TYPE. */
1288 type_to_type_object (struct type
*type
)
1290 type_object
*type_obj
;
1292 type_obj
= PyObject_New (type_object
, &type_object_type
);
1294 set_type (type_obj
, type
);
1296 return (PyObject
*) type_obj
;
1300 type_object_to_type (PyObject
*obj
)
1302 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1304 return ((type_object
*) obj
)->type
;
1309 /* Implementation of gdb.lookup_type. */
1311 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1313 static char *keywords
[] = { "name", "block", NULL
};
1314 const char *type_name
= NULL
;
1315 struct type
*type
= NULL
;
1316 PyObject
*block_obj
= NULL
;
1317 const struct block
*block
= NULL
;
1319 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1320 &type_name
, &block_obj
))
1325 block
= block_object_to_block (block_obj
);
1328 PyErr_SetString (PyExc_RuntimeError
,
1329 _("'block' argument must be a Block."));
1334 type
= typy_lookup_typename (type_name
, block
);
1338 return (PyObject
*) type_to_type_object (type
);
1342 gdbpy_initialize_types (void)
1346 typy_objfile_data_key
1347 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1349 if (PyType_Ready (&type_object_type
) < 0)
1351 if (PyType_Ready (&field_object_type
) < 0)
1353 if (PyType_Ready (&type_iterator_object_type
) < 0)
1356 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1358 if (PyModule_AddIntConstant (gdb_module
,
1359 /* Cast needed for Python 2.4. */
1360 (char *) pyty_codes
[i
].name
,
1361 pyty_codes
[i
].code
) < 0)
1365 if (gdb_pymodule_addobject (gdb_module
, "Type",
1366 (PyObject
*) &type_object_type
) < 0)
1369 if (gdb_pymodule_addobject (gdb_module
, "TypeIterator",
1370 (PyObject
*) &type_iterator_object_type
) < 0)
1373 return gdb_pymodule_addobject (gdb_module
, "Field",
1374 (PyObject
*) &field_object_type
);
1379 static PyGetSetDef type_object_getset
[] =
1381 { "code", typy_get_code
, NULL
,
1382 "The code for this type.", NULL
},
1383 { "sizeof", typy_get_sizeof
, NULL
,
1384 "The size of this type, in bytes.", NULL
},
1385 { "tag", typy_get_tag
, NULL
,
1386 "The tag name for this type, or None.", NULL
},
1390 static PyMethodDef type_object_methods
[] =
1392 { "array", typy_array
, METH_VARARGS
,
1393 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1394 Return a type which represents an array of objects of this type.\n\
1395 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1396 If LOW_BOUND is omitted, a value of zero is used." },
1397 { "vector", typy_vector
, METH_VARARGS
,
1398 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1399 Return a type which represents a vector of objects of this type.\n\
1400 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1401 If LOW_BOUND is omitted, a value of zero is used.\n\
1402 Vectors differ from arrays in that if the current language has C-style\n\
1403 arrays, vectors don't decay to a pointer to the first element.\n\
1404 They are first class values." },
1405 { "__contains__", typy_has_key
, METH_VARARGS
,
1406 "T.__contains__(k) -> True if T has a field named k, else False" },
1407 { "const", typy_const
, METH_NOARGS
,
1408 "const () -> Type\n\
1409 Return a const variant of this type." },
1410 { "fields", typy_fields
, METH_NOARGS
,
1411 "fields () -> list\n\
1412 Return a list holding all the fields of this type.\n\
1413 Each field is a gdb.Field object." },
1414 { "get", typy_get
, METH_VARARGS
,
1415 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1416 otherwise returns default, if supplied, or None if not." },
1417 { "has_key", typy_has_key
, METH_VARARGS
,
1418 "T.has_key(k) -> True if T has a field named k, else False" },
1419 { "items", typy_items
, METH_NOARGS
,
1420 "items () -> list\n\
1421 Return a list of (name, field) pairs of this type.\n\
1422 Each field is a gdb.Field object." },
1423 { "iteritems", typy_iteritems
, METH_NOARGS
,
1424 "iteritems () -> an iterator over the (name, field)\n\
1425 pairs of this type. Each field is a gdb.Field object." },
1426 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1427 "iterkeys () -> an iterator over the field names of this type." },
1428 { "itervalues", typy_itervalues
, METH_NOARGS
,
1429 "itervalues () -> an iterator over the fields of this type.\n\
1430 Each field is a gdb.Field object." },
1431 { "keys", typy_field_names
, METH_NOARGS
,
1433 Return a list holding all the fields names of this type." },
1434 { "pointer", typy_pointer
, METH_NOARGS
,
1435 "pointer () -> Type\n\
1436 Return a type of pointer to this type." },
1437 { "range", typy_range
, METH_NOARGS
,
1438 "range () -> tuple\n\
1439 Return a tuple containing the lower and upper range for this type."},
1440 { "reference", typy_reference
, METH_NOARGS
,
1441 "reference () -> Type\n\
1442 Return a type of reference to this type." },
1443 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1444 "strip_typedefs () -> Type\n\
1445 Return a type formed by stripping this type of all typedefs."},
1446 { "target", typy_target
, METH_NOARGS
,
1447 "target () -> Type\n\
1448 Return the target type of this type." },
1449 { "template_argument", typy_template_argument
, METH_VARARGS
,
1450 "template_argument (arg, [block]) -> Type\n\
1451 Return the type of a template argument." },
1452 { "unqualified", typy_unqualified
, METH_NOARGS
,
1453 "unqualified () -> Type\n\
1454 Return a variant of this type without const or volatile attributes." },
1455 { "values", typy_values
, METH_NOARGS
,
1456 "values () -> list\n\
1457 Return a list holding all the fields of this type.\n\
1458 Each field is a gdb.Field object." },
1459 { "volatile", typy_volatile
, METH_NOARGS
,
1460 "volatile () -> Type\n\
1461 Return a volatile variant of this type" },
1465 static PyNumberMethods type_object_as_number
= {
1467 NULL
, /* nb_subtract */
1468 NULL
, /* nb_multiply */
1470 NULL
, /* nb_divide */
1472 NULL
, /* nb_remainder */
1473 NULL
, /* nb_divmod */
1474 NULL
, /* nb_power */
1475 NULL
, /* nb_negative */
1476 NULL
, /* nb_positive */
1477 NULL
, /* nb_absolute */
1478 typy_nonzero
, /* nb_nonzero */
1479 NULL
, /* nb_invert */
1480 NULL
, /* nb_lshift */
1481 NULL
, /* nb_rshift */
1487 NULL
, /* reserved */
1489 NULL
, /* nb_coerce */
1493 NULL
, /* nb_float */
1500 static PyMappingMethods typy_mapping
= {
1503 NULL
/* no "set" method */
1506 static PyTypeObject type_object_type
=
1508 PyVarObject_HEAD_INIT (NULL
, 0)
1509 "gdb.Type", /*tp_name*/
1510 sizeof (type_object
), /*tp_basicsize*/
1512 typy_dealloc
, /*tp_dealloc*/
1518 &type_object_as_number
, /*tp_as_number*/
1519 0, /*tp_as_sequence*/
1520 &typy_mapping
, /*tp_as_mapping*/
1523 typy_str
, /*tp_str*/
1527 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1528 "GDB type object", /* tp_doc */
1529 0, /* tp_traverse */
1531 typy_richcompare
, /* tp_richcompare */
1532 0, /* tp_weaklistoffset */
1533 typy_iter
, /* tp_iter */
1534 0, /* tp_iternext */
1535 type_object_methods
, /* tp_methods */
1537 type_object_getset
, /* tp_getset */
1540 0, /* tp_descr_get */
1541 0, /* tp_descr_set */
1542 0, /* tp_dictoffset */
1548 static PyGetSetDef field_object_getset
[] =
1550 { "__dict__", gdb_py_generic_dict
, NULL
,
1551 "The __dict__ for this field.", &field_object_type
},
1555 static PyTypeObject field_object_type
=
1557 PyVarObject_HEAD_INIT (NULL
, 0)
1558 "gdb.Field", /*tp_name*/
1559 sizeof (field_object
), /*tp_basicsize*/
1561 field_dealloc
, /*tp_dealloc*/
1568 0, /*tp_as_sequence*/
1569 0, /*tp_as_mapping*/
1576 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1577 "GDB field object", /* tp_doc */
1578 0, /* tp_traverse */
1580 0, /* tp_richcompare */
1581 0, /* tp_weaklistoffset */
1583 0, /* tp_iternext */
1586 field_object_getset
, /* tp_getset */
1589 0, /* tp_descr_get */
1590 0, /* tp_descr_set */
1591 offsetof (field_object
, dict
), /* tp_dictoffset */
1597 static PyTypeObject type_iterator_object_type
= {
1598 PyVarObject_HEAD_INIT (NULL
, 0)
1599 "gdb.TypeIterator", /*tp_name*/
1600 sizeof (typy_iterator_object
), /*tp_basicsize*/
1602 typy_iterator_dealloc
, /*tp_dealloc*/
1609 0, /*tp_as_sequence*/
1610 0, /*tp_as_mapping*/
1617 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1618 "GDB type iterator object", /*tp_doc */
1621 0, /*tp_richcompare */
1622 0, /*tp_weaklistoffset */
1623 typy_iterator_iter
, /*tp_iter */
1624 typy_iterator_iternext
, /*tp_iternext */