1 /* Python interface to types.
3 Copyright (C) 2008-2016 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 "python-internal.h"
25 #include "cp-support.h"
30 #include "typeprint.h"
32 typedef struct pyty_type_object
37 /* If a Type object is associated with an objfile, it is kept on a
38 doubly-linked list, rooted in the objfile. This lets us copy the
39 underlying struct type when the objfile is deleted. */
40 struct pyty_type_object
*prev
;
41 struct pyty_type_object
*next
;
44 extern PyTypeObject type_object_type
45 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");
48 typedef struct pyty_field_object
52 /* Dictionary holding our attributes. */
56 extern PyTypeObject field_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");
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 extern PyTypeObject type_iterator_object_type
71 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
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 Py_TYPE (obj
)->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 true if OBJ is of type gdb.Field, false otherwise. */
151 gdbpy_is_field (PyObject
*obj
)
153 return PyObject_TypeCheck (obj
, &field_object_type
);
156 /* Return the code for this type. */
158 typy_get_code (PyObject
*self
, void *closure
)
160 struct type
*type
= ((type_object
*) self
)->type
;
162 return PyInt_FromLong (TYPE_CODE (type
));
165 /* Helper function for typy_fields which converts a single field to a
166 gdb.Field object. Returns NULL on error. */
169 convert_field (struct type
*type
, int field
)
171 PyObject
*result
= field_new ();
177 arg
= type_to_type_object (type
);
180 if (PyObject_SetAttrString (result
, "parent_type", arg
) < 0)
184 if (!field_is_static (&TYPE_FIELD (type
, field
)))
186 const char *attrstring
;
188 if (TYPE_CODE (type
) == TYPE_CODE_ENUM
)
190 arg
= gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type
, field
));
191 attrstring
= "enumval";
195 arg
= gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type
, field
));
196 attrstring
= "bitpos";
202 /* At least python-2.4 had the second parameter non-const. */
203 if (PyObject_SetAttrString (result
, (char *) attrstring
, arg
) < 0)
209 if (TYPE_FIELD_NAME (type
, field
))
211 const char *field_name
= TYPE_FIELD_NAME (type
, field
);
213 if (field_name
[0] != '\0')
215 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
225 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
229 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
231 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
235 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
236 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
240 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
244 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
247 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
251 /* A field can have a NULL type in some situations. */
252 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
258 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
261 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
274 /* Helper function to return the name of a field, as a gdb.Field object.
275 If the field doesn't have a name, None is returned. */
278 field_name (struct type
*type
, int field
)
282 if (TYPE_FIELD_NAME (type
, field
))
283 result
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
292 /* Helper function for Type standard mapping methods. Returns a
293 Python object for field i of the type. "kind" specifies what to
294 return: the name of the field, a gdb.Field object corresponding to
295 the field, or a tuple consisting of field name and gdb.Field
299 make_fielditem (struct type
*type
, int i
, enum gdbpy_iter_kind kind
)
301 PyObject
*item
= NULL
, *key
= NULL
, *value
= NULL
;
306 key
= field_name (type
, i
);
309 value
= convert_field (type
, i
);
312 item
= PyTuple_New (2);
315 PyTuple_SET_ITEM (item
, 0, key
);
316 PyTuple_SET_ITEM (item
, 1, value
);
319 item
= field_name (type
, i
);
322 item
= convert_field (type
, i
);
325 gdb_assert_not_reached ("invalid gdbpy_iter_kind");
336 /* Return a sequence of all field names, fields, or (name, field) pairs.
337 Each field is a gdb.Field object. */
340 typy_fields_items (PyObject
*self
, enum gdbpy_iter_kind kind
)
342 PyObject
*py_type
= self
;
343 PyObject
*result
= NULL
, *iter
= NULL
;
344 struct type
*type
= ((type_object
*) py_type
)->type
;
345 struct type
*checked_type
= type
;
349 checked_type
= check_typedef (checked_type
);
351 CATCH (except
, RETURN_MASK_ALL
)
353 GDB_PY_HANDLE_EXCEPTION (except
);
357 if (checked_type
!= type
)
358 py_type
= type_to_type_object (checked_type
);
359 iter
= typy_make_iter (py_type
, kind
);
360 if (checked_type
!= type
)
362 /* Need to wrap this in braces because Py_DECREF isn't wrapped
363 in a do{}while(0). */
368 result
= PySequence_List (iter
);
375 /* Return a sequence of all fields. Each field is a gdb.Field object. */
378 typy_values (PyObject
*self
, PyObject
*args
)
380 return typy_fields_items (self
, iter_values
);
383 /* Return a sequence of all fields. Each field is a gdb.Field object.
384 This method is similar to typy_values, except where the supplied
385 gdb.Type is an array, in which case it returns a list of one entry
386 which is a gdb.Field object for a range (the array bounds). */
389 typy_fields (PyObject
*self
, PyObject
*args
)
391 struct type
*type
= ((type_object
*) self
)->type
;
394 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
395 return typy_fields_items (self
, iter_values
);
397 /* Array type. Handle this as a special case because the common
398 machinery wants struct or union or enum types. Build a list of
399 one entry which is the range for the array. */
400 r
= convert_field (type
, 0);
404 rl
= Py_BuildValue ("[O]", r
);
410 /* Return a sequence of all field names. Each field is a gdb.Field object. */
413 typy_field_names (PyObject
*self
, PyObject
*args
)
415 return typy_fields_items (self
, iter_keys
);
418 /* Return a sequence of all (name, fields) pairs. Each field is a
422 typy_items (PyObject
*self
, PyObject
*args
)
424 return typy_fields_items (self
, iter_items
);
427 /* Return the type's name, or None. */
430 typy_get_name (PyObject
*self
, void *closure
)
432 struct type
*type
= ((type_object
*) self
)->type
;
434 if (TYPE_NAME (type
) == NULL
)
436 return PyString_FromString (TYPE_NAME (type
));
439 /* Return the type's tag, or None. */
441 typy_get_tag (PyObject
*self
, void *closure
)
443 struct type
*type
= ((type_object
*) self
)->type
;
445 if (!TYPE_TAG_NAME (type
))
447 return PyString_FromString (TYPE_TAG_NAME (type
));
450 /* Return the type, stripped of typedefs. */
452 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
454 struct type
*type
= ((type_object
*) self
)->type
;
458 type
= check_typedef (type
);
460 CATCH (except
, RETURN_MASK_ALL
)
462 GDB_PY_HANDLE_EXCEPTION (except
);
466 return type_to_type_object (type
);
469 /* Strip typedefs and pointers/reference from a type. Then check that
470 it is a struct, union, or enum type. If not, raise TypeError. */
473 typy_get_composite (struct type
*type
)
480 type
= check_typedef (type
);
482 CATCH (except
, RETURN_MASK_ALL
)
484 GDB_PY_HANDLE_EXCEPTION (except
);
488 if (TYPE_CODE (type
) != TYPE_CODE_PTR
489 && TYPE_CODE (type
) != TYPE_CODE_REF
)
491 type
= TYPE_TARGET_TYPE (type
);
494 /* If this is not a struct, union, or enum type, raise TypeError
496 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
497 && TYPE_CODE (type
) != TYPE_CODE_UNION
498 && TYPE_CODE (type
) != TYPE_CODE_ENUM
499 && TYPE_CODE (type
) != TYPE_CODE_FUNC
)
501 PyErr_SetString (PyExc_TypeError
,
502 "Type is not a structure, union, enum, or function type.");
509 /* Helper for typy_array and typy_vector. */
512 typy_array_1 (PyObject
*self
, PyObject
*args
, int is_vector
)
515 PyObject
*n2_obj
= NULL
;
516 struct type
*array
= NULL
;
517 struct type
*type
= ((type_object
*) self
)->type
;
519 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
524 if (!PyInt_Check (n2_obj
))
526 PyErr_SetString (PyExc_RuntimeError
,
527 _("Array bound must be an integer"));
531 if (! gdb_py_int_as_long (n2_obj
, &n2
))
540 if (n2
< n1
- 1) /* Note: An empty array has n2 == n1 - 1. */
542 PyErr_SetString (PyExc_ValueError
,
543 _("Array length must not be negative"));
549 array
= lookup_array_range_type (type
, n1
, n2
);
551 make_vector_type (array
);
553 CATCH (except
, RETURN_MASK_ALL
)
555 GDB_PY_HANDLE_EXCEPTION (except
);
559 return type_to_type_object (array
);
562 /* Return an array type. */
565 typy_array (PyObject
*self
, PyObject
*args
)
567 return typy_array_1 (self
, args
, 0);
570 /* Return a vector type. */
573 typy_vector (PyObject
*self
, PyObject
*args
)
575 return typy_array_1 (self
, args
, 1);
578 /* Return a Type object which represents a pointer to SELF. */
580 typy_pointer (PyObject
*self
, PyObject
*args
)
582 struct type
*type
= ((type_object
*) self
)->type
;
586 type
= lookup_pointer_type (type
);
588 CATCH (except
, RETURN_MASK_ALL
)
590 GDB_PY_HANDLE_EXCEPTION (except
);
594 return type_to_type_object (type
);
597 /* Return the range of a type represented by SELF. The return type is
598 a tuple. The first element of the tuple contains the low bound,
599 while the second element of the tuple contains the high bound. */
601 typy_range (PyObject
*self
, PyObject
*args
)
603 struct type
*type
= ((type_object
*) self
)->type
;
605 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
606 /* Initialize these to appease GCC warnings. */
607 LONGEST low
= 0, high
= 0;
609 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
610 && TYPE_CODE (type
) != TYPE_CODE_STRING
611 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
613 PyErr_SetString (PyExc_RuntimeError
,
614 _("This type does not have a range."));
618 switch (TYPE_CODE (type
))
620 case TYPE_CODE_ARRAY
:
621 case TYPE_CODE_STRING
:
622 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
623 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
625 case TYPE_CODE_RANGE
:
626 low
= TYPE_LOW_BOUND (type
);
627 high
= TYPE_HIGH_BOUND (type
);
631 low_bound
= PyLong_FromLong (low
);
635 high_bound
= PyLong_FromLong (high
);
639 result
= PyTuple_New (2);
643 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
648 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
650 Py_DECREF (high_bound
);
657 Py_XDECREF (high_bound
);
658 Py_XDECREF (low_bound
);
662 /* Return a Type object which represents a reference to SELF. */
664 typy_reference (PyObject
*self
, PyObject
*args
)
666 struct type
*type
= ((type_object
*) self
)->type
;
670 type
= lookup_reference_type (type
);
672 CATCH (except
, RETURN_MASK_ALL
)
674 GDB_PY_HANDLE_EXCEPTION (except
);
678 return type_to_type_object (type
);
681 /* Return a Type object which represents the target type of SELF. */
683 typy_target (PyObject
*self
, PyObject
*args
)
685 struct type
*type
= ((type_object
*) self
)->type
;
687 if (!TYPE_TARGET_TYPE (type
))
689 PyErr_SetString (PyExc_RuntimeError
,
690 _("Type does not have a target."));
694 return type_to_type_object (TYPE_TARGET_TYPE (type
));
697 /* Return a const-qualified type variant. */
699 typy_const (PyObject
*self
, PyObject
*args
)
701 struct type
*type
= ((type_object
*) self
)->type
;
705 type
= make_cv_type (1, 0, type
, NULL
);
707 CATCH (except
, RETURN_MASK_ALL
)
709 GDB_PY_HANDLE_EXCEPTION (except
);
713 return type_to_type_object (type
);
716 /* Return a volatile-qualified type variant. */
718 typy_volatile (PyObject
*self
, PyObject
*args
)
720 struct type
*type
= ((type_object
*) self
)->type
;
724 type
= make_cv_type (0, 1, type
, NULL
);
726 CATCH (except
, RETURN_MASK_ALL
)
728 GDB_PY_HANDLE_EXCEPTION (except
);
732 return type_to_type_object (type
);
735 /* Return an unqualified type variant. */
737 typy_unqualified (PyObject
*self
, PyObject
*args
)
739 struct type
*type
= ((type_object
*) self
)->type
;
743 type
= make_cv_type (0, 0, type
, NULL
);
745 CATCH (except
, RETURN_MASK_ALL
)
747 GDB_PY_HANDLE_EXCEPTION (except
);
751 return type_to_type_object (type
);
754 /* Return the size of the type represented by SELF, in bytes. */
756 typy_get_sizeof (PyObject
*self
, void *closure
)
758 struct type
*type
= ((type_object
*) self
)->type
;
762 check_typedef (type
);
764 CATCH (except
, RETURN_MASK_ALL
)
769 /* Ignore exceptions. */
771 return gdb_py_long_from_longest (TYPE_LENGTH (type
));
775 typy_lookup_typename (const char *type_name
, const struct block
*block
)
777 struct type
*type
= NULL
;
781 if (startswith (type_name
, "struct "))
782 type
= lookup_struct (type_name
+ 7, NULL
);
783 else if (startswith (type_name
, "union "))
784 type
= lookup_union (type_name
+ 6, NULL
);
785 else if (startswith (type_name
, "enum "))
786 type
= lookup_enum (type_name
+ 5, NULL
);
788 type
= lookup_typename (python_language
, python_gdbarch
,
789 type_name
, block
, 0);
791 CATCH (except
, RETURN_MASK_ALL
)
793 GDB_PY_HANDLE_EXCEPTION (except
);
801 typy_lookup_type (struct demangle_component
*demangled
,
802 const struct block
*block
)
804 struct type
*type
, *rtype
= NULL
;
805 char *type_name
= NULL
;
806 enum demangle_component_type demangled_type
;
808 /* Save the type: typy_lookup_type() may (indirectly) overwrite
809 memory pointed by demangled. */
810 demangled_type
= demangled
->type
;
812 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
813 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
814 || demangled_type
== DEMANGLE_COMPONENT_CONST
815 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
817 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
823 /* If the demangled_type matches with one of the types
824 below, run the corresponding function and save the type
825 to return later. We cannot just return here as we are in
826 an exception handler. */
827 switch (demangled_type
)
829 case DEMANGLE_COMPONENT_REFERENCE
:
830 rtype
= lookup_reference_type (type
);
832 case DEMANGLE_COMPONENT_POINTER
:
833 rtype
= lookup_pointer_type (type
);
835 case DEMANGLE_COMPONENT_CONST
:
836 rtype
= make_cv_type (1, 0, type
, NULL
);
838 case DEMANGLE_COMPONENT_VOLATILE
:
839 rtype
= make_cv_type (0, 1, type
, NULL
);
843 CATCH (except
, RETURN_MASK_ALL
)
845 GDB_PY_HANDLE_EXCEPTION (except
);
850 /* If we have a type from the switch statement above, just return
855 /* We don't have a type, so lookup the type. */
856 type_name
= cp_comp_to_string (demangled
, 10);
857 type
= typy_lookup_typename (type_name
, block
);
863 /* This is a helper function for typy_template_argument that is used
864 when the type does not have template symbols attached. It works by
865 parsing the type name. This happens with compilers, like older
866 versions of GCC, that do not emit DW_TAG_template_*. */
869 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
873 struct demangle_component
*demangled
;
874 struct demangle_parse_info
*info
= NULL
;
876 struct type
*argtype
;
877 struct cleanup
*cleanup
;
879 if (TYPE_NAME (type
) == NULL
)
881 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
887 /* Note -- this is not thread-safe. */
888 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
890 CATCH (except
, RETURN_MASK_ALL
)
892 GDB_PY_HANDLE_EXCEPTION (except
);
898 PyErr_SetString (PyExc_RuntimeError
, err
);
901 demangled
= info
->tree
;
902 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
904 /* Strip off component names. */
905 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
906 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
907 demangled
= demangled
->u
.s_binary
.right
;
909 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
911 do_cleanups (cleanup
);
912 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
916 /* Skip from the template to the arguments. */
917 demangled
= demangled
->u
.s_binary
.right
;
919 for (i
= 0; demangled
&& i
< argno
; ++i
)
920 demangled
= demangled
->u
.s_binary
.right
;
924 do_cleanups (cleanup
);
925 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
930 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
931 do_cleanups (cleanup
);
935 return type_to_type_object (argtype
);
939 typy_template_argument (PyObject
*self
, PyObject
*args
)
942 struct type
*type
= ((type_object
*) self
)->type
;
943 const struct block
*block
= NULL
;
944 PyObject
*block_obj
= NULL
;
946 struct value
*val
= NULL
;
948 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
953 block
= block_object_to_block (block_obj
);
956 PyErr_SetString (PyExc_RuntimeError
,
957 _("Second argument must be block."));
964 type
= check_typedef (type
);
965 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
966 type
= check_typedef (TYPE_TARGET_TYPE (type
));
968 CATCH (except
, RETURN_MASK_ALL
)
970 GDB_PY_HANDLE_EXCEPTION (except
);
974 /* We might not have DW_TAG_template_*, so try to parse the type's
975 name. This is inefficient if we do not have a template type --
976 but that is going to wind up as an error anyhow. */
977 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
978 return typy_legacy_template_argument (type
, block
, argno
);
980 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
982 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
987 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
988 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
989 return type_to_type_object (SYMBOL_TYPE (sym
));
990 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
992 PyErr_Format (PyExc_RuntimeError
,
993 _("Template argument is optimized out"));
999 val
= value_of_variable (sym
, block
);
1001 CATCH (except
, RETURN_MASK_ALL
)
1003 GDB_PY_HANDLE_EXCEPTION (except
);
1007 return value_to_value_object (val
);
1011 typy_str (PyObject
*self
)
1013 std::string thetype
;
1018 struct cleanup
*old_chain
;
1019 struct ui_file
*stb
;
1021 stb
= mem_fileopen ();
1022 old_chain
= make_cleanup_ui_file_delete (stb
);
1024 LA_PRINT_TYPE (type_object_to_type (self
), "", stb
, -1, 0,
1025 &type_print_raw_options
);
1027 thetype
= ui_file_as_string (stb
);
1028 do_cleanups (old_chain
);
1030 CATCH (except
, RETURN_MASK_ALL
)
1032 GDB_PY_HANDLE_EXCEPTION (except
);
1036 result
= PyUnicode_Decode (thetype
.c_str (), thetype
.length (),
1037 host_charset (), NULL
);
1042 /* Implement the richcompare method. */
1045 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1048 struct type
*type1
= type_object_to_type (self
);
1049 struct type
*type2
= type_object_to_type (other
);
1051 /* We can only compare ourselves to another Type object, and only
1052 for equality or inequality. */
1053 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
1055 Py_INCREF (Py_NotImplemented
);
1056 return Py_NotImplemented
;
1065 result
= types_deeply_equal (type1
, type2
);
1067 CATCH (except
, RETURN_MASK_ALL
)
1069 /* If there is a GDB exception, a comparison is not capable
1070 (or trusted), so exit. */
1071 GDB_PY_HANDLE_EXCEPTION (except
);
1076 if (op
== (result
? Py_EQ
: Py_NE
))
1083 static const struct objfile_data
*typy_objfile_data_key
;
1086 save_objfile_types (struct objfile
*objfile
, void *datum
)
1088 type_object
*obj
= (type_object
*) datum
;
1089 htab_t copied_types
;
1090 struct cleanup
*cleanup
;
1092 if (!gdb_python_initialized
)
1095 /* This prevents another thread from freeing the objects we're
1097 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1099 copied_types
= create_copied_types_hash (objfile
);
1103 type_object
*next
= obj
->next
;
1105 htab_empty (copied_types
);
1107 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1115 htab_delete (copied_types
);
1117 do_cleanups (cleanup
);
1121 set_type (type_object
*obj
, struct type
*type
)
1125 if (type
&& TYPE_OBJFILE (type
))
1127 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1129 obj
->next
= ((struct pyty_type_object
*)
1130 objfile_data (objfile
, typy_objfile_data_key
));
1132 obj
->next
->prev
= obj
;
1133 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1140 typy_dealloc (PyObject
*obj
)
1142 type_object
*type
= (type_object
*) obj
;
1145 type
->prev
->next
= type
->next
;
1146 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1148 /* Must reset head of list. */
1149 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1152 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1155 type
->next
->prev
= type
->prev
;
1157 Py_TYPE (type
)->tp_free (type
);
1160 /* Return number of fields ("length" of the field dictionary). */
1163 typy_length (PyObject
*self
)
1165 struct type
*type
= ((type_object
*) self
)->type
;
1167 type
= typy_get_composite (type
);
1171 return TYPE_NFIELDS (type
);
1174 /* Implements boolean evaluation of gdb.Type. Handle this like other
1175 Python objects that don't have a meaningful truth value -- all
1179 typy_nonzero (PyObject
*self
)
1184 /* Return optimized out value of this type. */
1187 typy_optimized_out (PyObject
*self
, PyObject
*args
)
1189 struct type
*type
= ((type_object
*) self
)->type
;
1191 return value_to_value_object (allocate_optimized_out_value (type
));
1194 /* Return a gdb.Field object for the field named by the argument. */
1197 typy_getitem (PyObject
*self
, PyObject
*key
)
1199 struct type
*type
= ((type_object
*) self
)->type
;
1203 field
= python_string_to_host_string (key
);
1207 /* We want just fields of this type, not of base types, so instead of
1208 using lookup_struct_elt_type, portions of that function are
1211 type
= typy_get_composite (type
);
1215 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1217 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1219 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1221 return convert_field (type
, i
);
1224 PyErr_SetObject (PyExc_KeyError
, key
);
1228 /* Implement the "get" method on the type object. This is the
1229 same as getitem if the key is present, but returns the supplied
1230 default value or None if the key is not found. */
1233 typy_get (PyObject
*self
, PyObject
*args
)
1235 PyObject
*key
, *defval
= Py_None
, *result
;
1237 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1240 result
= typy_getitem (self
, key
);
1244 /* typy_getitem returned error status. If the exception is
1245 KeyError, clear the exception status and return the defval
1246 instead. Otherwise return the exception unchanged. */
1247 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1255 /* Implement the "has_key" method on the type object. */
1258 typy_has_key (PyObject
*self
, PyObject
*args
)
1260 struct type
*type
= ((type_object
*) self
)->type
;
1264 if (!PyArg_ParseTuple (args
, "s", &field
))
1267 /* We want just fields of this type, not of base types, so instead of
1268 using lookup_struct_elt_type, portions of that function are
1271 type
= typy_get_composite (type
);
1275 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1277 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1279 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1285 /* Make an iterator object to iterate over keys, values, or items. */
1288 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1290 typy_iterator_object
*typy_iter_obj
;
1292 /* Check that "self" is a structure or union type. */
1293 if (typy_get_composite (((type_object
*) self
)->type
) == NULL
)
1296 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1297 &type_iterator_object_type
);
1298 if (typy_iter_obj
== NULL
)
1301 typy_iter_obj
->field
= 0;
1302 typy_iter_obj
->kind
= kind
;
1304 typy_iter_obj
->source
= (type_object
*) self
;
1306 return (PyObject
*) typy_iter_obj
;
1309 /* iteritems() method. */
1312 typy_iteritems (PyObject
*self
, PyObject
*args
)
1314 return typy_make_iter (self
, iter_items
);
1317 /* iterkeys() method. */
1320 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1322 return typy_make_iter (self
, iter_keys
);
1325 /* Iterating over the class, same as iterkeys except for the function
1329 typy_iter (PyObject
*self
)
1331 return typy_make_iter (self
, iter_keys
);
1334 /* itervalues() method. */
1337 typy_itervalues (PyObject
*self
, PyObject
*args
)
1339 return typy_make_iter (self
, iter_values
);
1342 /* Return a reference to the type iterator. */
1345 typy_iterator_iter (PyObject
*self
)
1351 /* Return the next field in the iteration through the list of fields
1355 typy_iterator_iternext (PyObject
*self
)
1357 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1358 struct type
*type
= iter_obj
->source
->type
;
1361 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1363 result
= make_fielditem (type
, iter_obj
->field
, iter_obj
->kind
);
1373 typy_iterator_dealloc (PyObject
*obj
)
1375 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1377 Py_DECREF (iter_obj
->source
);
1380 /* Create a new Type referring to TYPE. */
1382 type_to_type_object (struct type
*type
)
1384 type_object
*type_obj
;
1386 type_obj
= PyObject_New (type_object
, &type_object_type
);
1388 set_type (type_obj
, type
);
1390 return (PyObject
*) type_obj
;
1394 type_object_to_type (PyObject
*obj
)
1396 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1398 return ((type_object
*) obj
)->type
;
1403 /* Implementation of gdb.lookup_type. */
1405 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1407 static char *keywords
[] = { "name", "block", NULL
};
1408 const char *type_name
= NULL
;
1409 struct type
*type
= NULL
;
1410 PyObject
*block_obj
= NULL
;
1411 const struct block
*block
= NULL
;
1413 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1414 &type_name
, &block_obj
))
1419 block
= block_object_to_block (block_obj
);
1422 PyErr_SetString (PyExc_RuntimeError
,
1423 _("'block' argument must be a Block."));
1428 type
= typy_lookup_typename (type_name
, block
);
1432 return (PyObject
*) type_to_type_object (type
);
1436 gdbpy_initialize_types (void)
1440 typy_objfile_data_key
1441 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1443 if (PyType_Ready (&type_object_type
) < 0)
1445 if (PyType_Ready (&field_object_type
) < 0)
1447 if (PyType_Ready (&type_iterator_object_type
) < 0)
1450 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1452 if (PyModule_AddIntConstant (gdb_module
,
1453 /* Cast needed for Python 2.4. */
1454 (char *) pyty_codes
[i
].name
,
1455 pyty_codes
[i
].code
) < 0)
1459 if (gdb_pymodule_addobject (gdb_module
, "Type",
1460 (PyObject
*) &type_object_type
) < 0)
1463 if (gdb_pymodule_addobject (gdb_module
, "TypeIterator",
1464 (PyObject
*) &type_iterator_object_type
) < 0)
1467 return gdb_pymodule_addobject (gdb_module
, "Field",
1468 (PyObject
*) &field_object_type
);
1473 static PyGetSetDef type_object_getset
[] =
1475 { "code", typy_get_code
, NULL
,
1476 "The code for this type.", NULL
},
1477 { "name", typy_get_name
, NULL
,
1478 "The name for this type, or None.", NULL
},
1479 { "sizeof", typy_get_sizeof
, NULL
,
1480 "The size of this type, in bytes.", NULL
},
1481 { "tag", typy_get_tag
, NULL
,
1482 "The tag name for this type, or None.", NULL
},
1486 static PyMethodDef type_object_methods
[] =
1488 { "array", typy_array
, METH_VARARGS
,
1489 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1490 Return a type which represents an array of objects of this type.\n\
1491 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1492 If LOW_BOUND is omitted, a value of zero is used." },
1493 { "vector", typy_vector
, METH_VARARGS
,
1494 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1495 Return a type which represents a vector of objects of this type.\n\
1496 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1497 If LOW_BOUND is omitted, a value of zero is used.\n\
1498 Vectors differ from arrays in that if the current language has C-style\n\
1499 arrays, vectors don't decay to a pointer to the first element.\n\
1500 They are first class values." },
1501 { "__contains__", typy_has_key
, METH_VARARGS
,
1502 "T.__contains__(k) -> True if T has a field named k, else False" },
1503 { "const", typy_const
, METH_NOARGS
,
1504 "const () -> Type\n\
1505 Return a const variant of this type." },
1506 { "optimized_out", typy_optimized_out
, METH_NOARGS
,
1507 "optimized_out() -> Value\n\
1508 Return optimized out value of this type." },
1509 { "fields", typy_fields
, METH_NOARGS
,
1510 "fields () -> list\n\
1511 Return a list holding all the fields of this type.\n\
1512 Each field is a gdb.Field object." },
1513 { "get", typy_get
, METH_VARARGS
,
1514 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1515 otherwise returns default, if supplied, or None if not." },
1516 { "has_key", typy_has_key
, METH_VARARGS
,
1517 "T.has_key(k) -> True if T has a field named k, else False" },
1518 { "items", typy_items
, METH_NOARGS
,
1519 "items () -> list\n\
1520 Return a list of (name, field) pairs of this type.\n\
1521 Each field is a gdb.Field object." },
1522 { "iteritems", typy_iteritems
, METH_NOARGS
,
1523 "iteritems () -> an iterator over the (name, field)\n\
1524 pairs of this type. Each field is a gdb.Field object." },
1525 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1526 "iterkeys () -> an iterator over the field names of this type." },
1527 { "itervalues", typy_itervalues
, METH_NOARGS
,
1528 "itervalues () -> an iterator over the fields of this type.\n\
1529 Each field is a gdb.Field object." },
1530 { "keys", typy_field_names
, METH_NOARGS
,
1532 Return a list holding all the fields names of this type." },
1533 { "pointer", typy_pointer
, METH_NOARGS
,
1534 "pointer () -> Type\n\
1535 Return a type of pointer to this type." },
1536 { "range", typy_range
, METH_NOARGS
,
1537 "range () -> tuple\n\
1538 Return a tuple containing the lower and upper range for this type."},
1539 { "reference", typy_reference
, METH_NOARGS
,
1540 "reference () -> Type\n\
1541 Return a type of reference to this type." },
1542 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1543 "strip_typedefs () -> Type\n\
1544 Return a type formed by stripping this type of all typedefs."},
1545 { "target", typy_target
, METH_NOARGS
,
1546 "target () -> Type\n\
1547 Return the target type of this type." },
1548 { "template_argument", typy_template_argument
, METH_VARARGS
,
1549 "template_argument (arg, [block]) -> Type\n\
1550 Return the type of a template argument." },
1551 { "unqualified", typy_unqualified
, METH_NOARGS
,
1552 "unqualified () -> Type\n\
1553 Return a variant of this type without const or volatile attributes." },
1554 { "values", typy_values
, METH_NOARGS
,
1555 "values () -> list\n\
1556 Return a list holding all the fields of this type.\n\
1557 Each field is a gdb.Field object." },
1558 { "volatile", typy_volatile
, METH_NOARGS
,
1559 "volatile () -> Type\n\
1560 Return a volatile variant of this type" },
1564 static PyNumberMethods type_object_as_number
= {
1566 NULL
, /* nb_subtract */
1567 NULL
, /* nb_multiply */
1569 NULL
, /* nb_divide */
1571 NULL
, /* nb_remainder */
1572 NULL
, /* nb_divmod */
1573 NULL
, /* nb_power */
1574 NULL
, /* nb_negative */
1575 NULL
, /* nb_positive */
1576 NULL
, /* nb_absolute */
1577 typy_nonzero
, /* nb_nonzero */
1578 NULL
, /* nb_invert */
1579 NULL
, /* nb_lshift */
1580 NULL
, /* nb_rshift */
1586 NULL
, /* reserved */
1588 NULL
, /* nb_coerce */
1592 NULL
, /* nb_float */
1599 static PyMappingMethods typy_mapping
= {
1602 NULL
/* no "set" method */
1605 PyTypeObject type_object_type
=
1607 PyVarObject_HEAD_INIT (NULL
, 0)
1608 "gdb.Type", /*tp_name*/
1609 sizeof (type_object
), /*tp_basicsize*/
1611 typy_dealloc
, /*tp_dealloc*/
1617 &type_object_as_number
, /*tp_as_number*/
1618 0, /*tp_as_sequence*/
1619 &typy_mapping
, /*tp_as_mapping*/
1622 typy_str
, /*tp_str*/
1626 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1627 "GDB type object", /* tp_doc */
1628 0, /* tp_traverse */
1630 typy_richcompare
, /* tp_richcompare */
1631 0, /* tp_weaklistoffset */
1632 typy_iter
, /* tp_iter */
1633 0, /* tp_iternext */
1634 type_object_methods
, /* tp_methods */
1636 type_object_getset
, /* tp_getset */
1639 0, /* tp_descr_get */
1640 0, /* tp_descr_set */
1641 0, /* tp_dictoffset */
1647 static PyGetSetDef field_object_getset
[] =
1649 { "__dict__", gdb_py_generic_dict
, NULL
,
1650 "The __dict__ for this field.", &field_object_type
},
1654 PyTypeObject field_object_type
=
1656 PyVarObject_HEAD_INIT (NULL
, 0)
1657 "gdb.Field", /*tp_name*/
1658 sizeof (field_object
), /*tp_basicsize*/
1660 field_dealloc
, /*tp_dealloc*/
1667 0, /*tp_as_sequence*/
1668 0, /*tp_as_mapping*/
1675 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1676 "GDB field object", /* tp_doc */
1677 0, /* tp_traverse */
1679 0, /* tp_richcompare */
1680 0, /* tp_weaklistoffset */
1682 0, /* tp_iternext */
1685 field_object_getset
, /* tp_getset */
1688 0, /* tp_descr_get */
1689 0, /* tp_descr_set */
1690 offsetof (field_object
, dict
), /* tp_dictoffset */
1696 PyTypeObject type_iterator_object_type
= {
1697 PyVarObject_HEAD_INIT (NULL
, 0)
1698 "gdb.TypeIterator", /*tp_name*/
1699 sizeof (typy_iterator_object
), /*tp_basicsize*/
1701 typy_iterator_dealloc
, /*tp_dealloc*/
1708 0, /*tp_as_sequence*/
1709 0, /*tp_as_mapping*/
1716 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1717 "GDB type iterator object", /*tp_doc */
1720 0, /*tp_richcompare */
1721 0, /*tp_weaklistoffset */
1722 typy_iterator_iter
, /*tp_iter */
1723 typy_iterator_iternext
, /*tp_iternext */