1 /* Python interface to types.
3 Copyright (C) 2008-2019 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"
29 #include "typeprint.h"
31 typedef struct pyty_type_object
36 /* If a Type object is associated with an objfile, it is kept on a
37 doubly-linked list, rooted in the objfile. This lets us copy the
38 underlying struct type when the objfile is deleted. */
39 struct pyty_type_object
*prev
;
40 struct pyty_type_object
*next
;
43 extern PyTypeObject type_object_type
44 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");
47 typedef struct pyty_field_object
51 /* Dictionary holding our attributes. */
55 extern PyTypeObject field_object_type
56 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");
58 /* A type iterator object. */
61 /* The current field index. */
64 enum gdbpy_iter_kind kind
;
65 /* Pointer back to the original source type object. */
66 struct pyty_type_object
*source
;
67 } typy_iterator_object
;
69 extern PyTypeObject type_iterator_object_type
70 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
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_BITSTRING
),
89 ENTRY (TYPE_CODE_PTR
),
90 ENTRY (TYPE_CODE_ARRAY
),
91 ENTRY (TYPE_CODE_STRUCT
),
92 ENTRY (TYPE_CODE_UNION
),
93 ENTRY (TYPE_CODE_ENUM
),
94 ENTRY (TYPE_CODE_FLAGS
),
95 ENTRY (TYPE_CODE_FUNC
),
96 ENTRY (TYPE_CODE_INT
),
97 ENTRY (TYPE_CODE_FLT
),
98 ENTRY (TYPE_CODE_VOID
),
99 ENTRY (TYPE_CODE_SET
),
100 ENTRY (TYPE_CODE_RANGE
),
101 ENTRY (TYPE_CODE_STRING
),
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_RVALUE_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 gdbpy_ref
<field_object
> result (PyObject_New (field_object
,
133 &field_object_type
));
137 result
->dict
= PyDict_New ();
141 return (PyObject
*) result
.release ();
146 /* Return true if OBJ is of type gdb.Field, false otherwise. */
149 gdbpy_is_field (PyObject
*obj
)
151 return PyObject_TypeCheck (obj
, &field_object_type
);
154 /* Return the code for this type. */
156 typy_get_code (PyObject
*self
, void *closure
)
158 struct type
*type
= ((type_object
*) self
)->type
;
160 return PyInt_FromLong (TYPE_CODE (type
));
163 /* Helper function for typy_fields which converts a single field to a
164 gdb.Field object. Returns NULL on error. */
167 convert_field (struct type
*type
, int field
)
169 gdbpy_ref
<> result (field_new ());
174 gdbpy_ref
<> arg (type_to_type_object (type
));
177 if (PyObject_SetAttrString (result
.get (), "parent_type", arg
.get ()) < 0)
180 if (!field_is_static (&TYPE_FIELD (type
, field
)))
182 const char *attrstring
;
184 if (TYPE_CODE (type
) == TYPE_CODE_ENUM
)
186 arg
.reset (gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type
,
188 attrstring
= "enumval";
192 arg
.reset (gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type
,
194 attrstring
= "bitpos";
200 if (PyObject_SetAttrString (result
.get (), attrstring
, arg
.get ()) < 0)
205 if (TYPE_FIELD_NAME (type
, field
))
207 const char *field_name
= TYPE_FIELD_NAME (type
, field
);
209 if (field_name
[0] != '\0')
211 arg
.reset (PyString_FromString (TYPE_FIELD_NAME (type
, field
)));
217 arg
= gdbpy_ref
<>::new_reference (Py_None
);
219 if (PyObject_SetAttrString (result
.get (), "name", arg
.get ()) < 0)
222 arg
= gdbpy_ref
<>::new_reference (TYPE_FIELD_ARTIFICIAL (type
, field
)
223 ? Py_True
: Py_False
);
224 if (PyObject_SetAttrString (result
.get (), "artificial", arg
.get ()) < 0)
227 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
228 arg
= gdbpy_ref
<>::new_reference (field
< TYPE_N_BASECLASSES (type
)
229 ? Py_True
: Py_False
);
231 arg
= gdbpy_ref
<>::new_reference (Py_False
);
232 if (PyObject_SetAttrString (result
.get (), "is_base_class", arg
.get ()) < 0)
235 arg
.reset (PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
)));
238 if (PyObject_SetAttrString (result
.get (), "bitsize", arg
.get ()) < 0)
241 /* A field can have a NULL type in some situations. */
242 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
243 arg
= gdbpy_ref
<>::new_reference (Py_None
);
245 arg
.reset (type_to_type_object (TYPE_FIELD_TYPE (type
, field
)));
248 if (PyObject_SetAttrString (result
.get (), "type", arg
.get ()) < 0)
254 /* Helper function to return the name of a field, as a gdb.Field object.
255 If the field doesn't have a name, None is returned. */
258 field_name (struct type
*type
, int field
)
262 if (TYPE_FIELD_NAME (type
, field
))
263 result
.reset (PyString_FromString (TYPE_FIELD_NAME (type
, field
)));
265 result
= gdbpy_ref
<>::new_reference (Py_None
);
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
)
283 gdbpy_ref
<> key (field_name (type
, i
));
286 gdbpy_ref
<> value
= convert_field (type
, i
);
289 gdbpy_ref
<> item (PyTuple_New (2));
292 PyTuple_SET_ITEM (item
.get (), 0, key
.release ());
293 PyTuple_SET_ITEM (item
.get (), 1, value
.release ());
297 return field_name (type
, i
);
299 return convert_field (type
, i
);
301 gdb_assert_not_reached ("invalid gdbpy_iter_kind");
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 struct type
*type
= ((type_object
*) py_type
)->type
;
312 struct type
*checked_type
= type
;
316 checked_type
= check_typedef (checked_type
);
318 catch (const gdb_exception
&except
)
320 GDB_PY_HANDLE_EXCEPTION (except
);
323 gdbpy_ref
<> type_holder
;
324 if (checked_type
!= type
)
326 type_holder
.reset (type_to_type_object (checked_type
));
327 if (type_holder
== nullptr)
329 py_type
= type_holder
.get ();
331 gdbpy_ref
<> iter (typy_make_iter (py_type
, kind
));
335 return PySequence_List (iter
.get ());
338 /* Return a sequence of all fields. Each field is a gdb.Field object. */
341 typy_values (PyObject
*self
, PyObject
*args
)
343 return typy_fields_items (self
, iter_values
);
346 /* Return a sequence of all fields. Each field is a gdb.Field object.
347 This method is similar to typy_values, except where the supplied
348 gdb.Type is an array, in which case it returns a list of one entry
349 which is a gdb.Field object for a range (the array bounds). */
352 typy_fields (PyObject
*self
, PyObject
*args
)
354 struct type
*type
= ((type_object
*) self
)->type
;
356 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
357 return typy_fields_items (self
, iter_values
);
359 /* Array type. Handle this as a special case because the common
360 machinery wants struct or union or enum types. Build a list of
361 one entry which is the range for the array. */
362 gdbpy_ref
<> r
= convert_field (type
, 0);
366 return Py_BuildValue ("[O]", r
.get ());
369 /* Return a sequence of all field names. Each field is a gdb.Field object. */
372 typy_field_names (PyObject
*self
, PyObject
*args
)
374 return typy_fields_items (self
, iter_keys
);
377 /* Return a sequence of all (name, fields) pairs. Each field is a
381 typy_items (PyObject
*self
, PyObject
*args
)
383 return typy_fields_items (self
, iter_items
);
386 /* Return the type's name, or None. */
389 typy_get_name (PyObject
*self
, void *closure
)
391 struct type
*type
= ((type_object
*) self
)->type
;
393 if (TYPE_NAME (type
) == NULL
)
395 return PyString_FromString (TYPE_NAME (type
));
398 /* Return the type's tag, or None. */
400 typy_get_tag (PyObject
*self
, void *closure
)
402 struct type
*type
= ((type_object
*) self
)->type
;
403 const char *tagname
= nullptr;
405 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
406 || TYPE_CODE (type
) == TYPE_CODE_UNION
407 || TYPE_CODE (type
) == TYPE_CODE_ENUM
)
408 tagname
= TYPE_NAME (type
);
410 if (tagname
== nullptr)
412 return PyString_FromString (tagname
);
415 /* Return the type's objfile, or None. */
417 typy_get_objfile (PyObject
*self
, void *closure
)
419 struct type
*type
= ((type_object
*) self
)->type
;
420 struct objfile
*objfile
= TYPE_OBJFILE (type
);
422 if (objfile
== nullptr)
424 return objfile_to_objfile_object (objfile
).release ();
427 /* Return the type, stripped of typedefs. */
429 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
431 struct type
*type
= ((type_object
*) self
)->type
;
435 type
= check_typedef (type
);
437 catch (const gdb_exception
&except
)
439 GDB_PY_HANDLE_EXCEPTION (except
);
442 return type_to_type_object (type
);
445 /* Strip typedefs and pointers/reference from a type. Then check that
446 it is a struct, union, or enum type. If not, raise TypeError. */
449 typy_get_composite (struct type
*type
)
456 type
= check_typedef (type
);
458 catch (const gdb_exception
&except
)
460 GDB_PY_HANDLE_EXCEPTION (except
);
463 if (TYPE_CODE (type
) != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type
))
465 type
= TYPE_TARGET_TYPE (type
);
468 /* If this is not a struct, union, or enum type, raise TypeError
470 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
471 && TYPE_CODE (type
) != TYPE_CODE_UNION
472 && TYPE_CODE (type
) != TYPE_CODE_ENUM
473 && TYPE_CODE (type
) != TYPE_CODE_FUNC
)
475 PyErr_SetString (PyExc_TypeError
,
476 "Type is not a structure, union, enum, or function type.");
483 /* Helper for typy_array and typy_vector. */
486 typy_array_1 (PyObject
*self
, PyObject
*args
, int is_vector
)
489 PyObject
*n2_obj
= NULL
;
490 struct type
*array
= NULL
;
491 struct type
*type
= ((type_object
*) self
)->type
;
493 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
498 if (!PyInt_Check (n2_obj
))
500 PyErr_SetString (PyExc_RuntimeError
,
501 _("Array bound must be an integer"));
505 if (! gdb_py_int_as_long (n2_obj
, &n2
))
514 if (n2
< n1
- 1) /* Note: An empty array has n2 == n1 - 1. */
516 PyErr_SetString (PyExc_ValueError
,
517 _("Array length must not be negative"));
523 array
= lookup_array_range_type (type
, n1
, n2
);
525 make_vector_type (array
);
527 catch (const gdb_exception
&except
)
529 GDB_PY_HANDLE_EXCEPTION (except
);
532 return type_to_type_object (array
);
535 /* Return an array type. */
538 typy_array (PyObject
*self
, PyObject
*args
)
540 return typy_array_1 (self
, args
, 0);
543 /* Return a vector type. */
546 typy_vector (PyObject
*self
, PyObject
*args
)
548 return typy_array_1 (self
, args
, 1);
551 /* Return a Type object which represents a pointer to SELF. */
553 typy_pointer (PyObject
*self
, PyObject
*args
)
555 struct type
*type
= ((type_object
*) self
)->type
;
559 type
= lookup_pointer_type (type
);
561 catch (const gdb_exception
&except
)
563 GDB_PY_HANDLE_EXCEPTION (except
);
566 return type_to_type_object (type
);
569 /* Return the range of a type represented by SELF. The return type is
570 a tuple. The first element of the tuple contains the low bound,
571 while the second element of the tuple contains the high bound. */
573 typy_range (PyObject
*self
, PyObject
*args
)
575 struct type
*type
= ((type_object
*) self
)->type
;
576 /* Initialize these to appease GCC warnings. */
577 LONGEST low
= 0, high
= 0;
579 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
580 && TYPE_CODE (type
) != TYPE_CODE_STRING
581 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
583 PyErr_SetString (PyExc_RuntimeError
,
584 _("This type does not have a range."));
588 switch (TYPE_CODE (type
))
590 case TYPE_CODE_ARRAY
:
591 case TYPE_CODE_STRING
:
592 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
593 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
595 case TYPE_CODE_RANGE
:
596 low
= TYPE_LOW_BOUND (type
);
597 high
= TYPE_HIGH_BOUND (type
);
601 gdbpy_ref
<> low_bound (PyLong_FromLong (low
));
602 if (low_bound
== NULL
)
605 gdbpy_ref
<> high_bound (PyLong_FromLong (high
));
606 if (high_bound
== NULL
)
609 gdbpy_ref
<> result (PyTuple_New (2));
613 if (PyTuple_SetItem (result
.get (), 0, low_bound
.release ()) != 0
614 || PyTuple_SetItem (result
.get (), 1, high_bound
.release ()) != 0)
616 return result
.release ();
619 /* Return a Type object which represents a reference to SELF. */
621 typy_reference (PyObject
*self
, PyObject
*args
)
623 struct type
*type
= ((type_object
*) self
)->type
;
627 type
= lookup_lvalue_reference_type (type
);
629 catch (const gdb_exception
&except
)
631 GDB_PY_HANDLE_EXCEPTION (except
);
634 return type_to_type_object (type
);
637 /* Return a Type object which represents the target type of SELF. */
639 typy_target (PyObject
*self
, PyObject
*args
)
641 struct type
*type
= ((type_object
*) self
)->type
;
643 if (!TYPE_TARGET_TYPE (type
))
645 PyErr_SetString (PyExc_RuntimeError
,
646 _("Type does not have a target."));
650 return type_to_type_object (TYPE_TARGET_TYPE (type
));
653 /* Return a const-qualified type variant. */
655 typy_const (PyObject
*self
, PyObject
*args
)
657 struct type
*type
= ((type_object
*) self
)->type
;
661 type
= make_cv_type (1, 0, type
, NULL
);
663 catch (const gdb_exception
&except
)
665 GDB_PY_HANDLE_EXCEPTION (except
);
668 return type_to_type_object (type
);
671 /* Return a volatile-qualified type variant. */
673 typy_volatile (PyObject
*self
, PyObject
*args
)
675 struct type
*type
= ((type_object
*) self
)->type
;
679 type
= make_cv_type (0, 1, type
, NULL
);
681 catch (const gdb_exception
&except
)
683 GDB_PY_HANDLE_EXCEPTION (except
);
686 return type_to_type_object (type
);
689 /* Return an unqualified type variant. */
691 typy_unqualified (PyObject
*self
, PyObject
*args
)
693 struct type
*type
= ((type_object
*) self
)->type
;
697 type
= make_cv_type (0, 0, type
, NULL
);
699 catch (const gdb_exception
&except
)
701 GDB_PY_HANDLE_EXCEPTION (except
);
704 return type_to_type_object (type
);
707 /* Return the size of the type represented by SELF, in bytes. */
709 typy_get_sizeof (PyObject
*self
, void *closure
)
711 struct type
*type
= ((type_object
*) self
)->type
;
715 check_typedef (type
);
717 catch (const gdb_exception
&except
)
721 /* Ignore exceptions. */
723 return gdb_py_long_from_longest (TYPE_LENGTH (type
));
726 /* Return the alignment of the type represented by SELF, in bytes. */
728 typy_get_alignof (PyObject
*self
, void *closure
)
730 struct type
*type
= ((type_object
*) self
)->type
;
735 align
= type_align (type
);
737 catch (const gdb_exception
&except
)
742 /* Ignore exceptions. */
744 return gdb_py_object_from_ulongest (align
).release ();
748 typy_lookup_typename (const char *type_name
, const struct block
*block
)
750 struct type
*type
= NULL
;
754 if (startswith (type_name
, "struct "))
755 type
= lookup_struct (type_name
+ 7, NULL
);
756 else if (startswith (type_name
, "union "))
757 type
= lookup_union (type_name
+ 6, NULL
);
758 else if (startswith (type_name
, "enum "))
759 type
= lookup_enum (type_name
+ 5, NULL
);
761 type
= lookup_typename (python_language
,
762 type_name
, block
, 0);
764 catch (const gdb_exception
&except
)
766 GDB_PY_HANDLE_EXCEPTION (except
);
773 typy_lookup_type (struct demangle_component
*demangled
,
774 const struct block
*block
)
776 struct type
*type
, *rtype
= NULL
;
777 enum demangle_component_type demangled_type
;
779 /* Save the type: typy_lookup_type() may (indirectly) overwrite
780 memory pointed by demangled. */
781 demangled_type
= demangled
->type
;
783 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
784 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
785 || demangled_type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
786 || demangled_type
== DEMANGLE_COMPONENT_CONST
787 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
789 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
795 /* If the demangled_type matches with one of the types
796 below, run the corresponding function and save the type
797 to return later. We cannot just return here as we are in
798 an exception handler. */
799 switch (demangled_type
)
801 case DEMANGLE_COMPONENT_REFERENCE
:
802 rtype
= lookup_lvalue_reference_type (type
);
804 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
805 rtype
= lookup_rvalue_reference_type (type
);
807 case DEMANGLE_COMPONENT_POINTER
:
808 rtype
= lookup_pointer_type (type
);
810 case DEMANGLE_COMPONENT_CONST
:
811 rtype
= make_cv_type (1, 0, type
, NULL
);
813 case DEMANGLE_COMPONENT_VOLATILE
:
814 rtype
= make_cv_type (0, 1, type
, NULL
);
818 catch (const gdb_exception
&except
)
820 GDB_PY_HANDLE_EXCEPTION (except
);
824 /* If we have a type from the switch statement above, just return
829 /* We don't have a type, so lookup the type. */
830 gdb::unique_xmalloc_ptr
<char> type_name
= cp_comp_to_string (demangled
, 10);
831 return typy_lookup_typename (type_name
.get (), block
);
834 /* This is a helper function for typy_template_argument that is used
835 when the type does not have template symbols attached. It works by
836 parsing the type name. This happens with compilers, like older
837 versions of GCC, that do not emit DW_TAG_template_*. */
840 typy_legacy_template_argument (struct type
*type
, const struct block
*block
,
844 struct demangle_component
*demangled
;
845 std::unique_ptr
<demangle_parse_info
> info
;
847 struct type
*argtype
;
849 if (TYPE_NAME (type
) == NULL
)
851 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
857 /* Note -- this is not thread-safe. */
858 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
860 catch (const gdb_exception
&except
)
862 GDB_PY_HANDLE_EXCEPTION (except
);
867 PyErr_SetString (PyExc_RuntimeError
, err
.c_str ());
870 demangled
= info
->tree
;
872 /* Strip off component names. */
873 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
874 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
875 demangled
= demangled
->u
.s_binary
.right
;
877 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
879 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
883 /* Skip from the template to the arguments. */
884 demangled
= demangled
->u
.s_binary
.right
;
886 for (i
= 0; demangled
&& i
< argno
; ++i
)
887 demangled
= demangled
->u
.s_binary
.right
;
891 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
896 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
900 return type_to_type_object (argtype
);
904 typy_template_argument (PyObject
*self
, PyObject
*args
)
907 struct type
*type
= ((type_object
*) self
)->type
;
908 const struct block
*block
= NULL
;
909 PyObject
*block_obj
= NULL
;
911 struct value
*val
= NULL
;
913 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
918 PyErr_SetString (PyExc_RuntimeError
,
919 _("Template argument number must be non-negative"));
925 block
= block_object_to_block (block_obj
);
928 PyErr_SetString (PyExc_RuntimeError
,
929 _("Second argument must be block."));
936 type
= check_typedef (type
);
937 if (TYPE_IS_REFERENCE (type
))
938 type
= check_typedef (TYPE_TARGET_TYPE (type
));
940 catch (const gdb_exception
&except
)
942 GDB_PY_HANDLE_EXCEPTION (except
);
945 /* We might not have DW_TAG_template_*, so try to parse the type's
946 name. This is inefficient if we do not have a template type --
947 but that is going to wind up as an error anyhow. */
948 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
949 return typy_legacy_template_argument (type
, block
, argno
);
951 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
953 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
958 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
959 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
960 return type_to_type_object (SYMBOL_TYPE (sym
));
961 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
963 PyErr_Format (PyExc_RuntimeError
,
964 _("Template argument is optimized out"));
970 val
= value_of_variable (sym
, block
);
972 catch (const gdb_exception
&except
)
974 GDB_PY_HANDLE_EXCEPTION (except
);
977 return value_to_value_object (val
);
981 typy_str (PyObject
*self
)
987 LA_PRINT_TYPE (type_object_to_type (self
), "", &thetype
, -1, 0,
988 &type_print_raw_options
);
990 catch (const gdb_exception
&except
)
992 GDB_PY_HANDLE_EXCEPTION (except
);
995 return PyUnicode_Decode (thetype
.c_str (), thetype
.size (),
996 host_charset (), NULL
);
999 /* Implement the richcompare method. */
1002 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1004 bool result
= false;
1005 struct type
*type1
= type_object_to_type (self
);
1006 struct type
*type2
= type_object_to_type (other
);
1008 /* We can only compare ourselves to another Type object, and only
1009 for equality or inequality. */
1010 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
1012 Py_INCREF (Py_NotImplemented
);
1013 return Py_NotImplemented
;
1022 result
= types_deeply_equal (type1
, type2
);
1024 catch (const gdb_exception
&except
)
1026 /* If there is a GDB exception, a comparison is not capable
1027 (or trusted), so exit. */
1028 GDB_PY_HANDLE_EXCEPTION (except
);
1032 if (op
== (result
? Py_EQ
: Py_NE
))
1039 static const struct objfile_data
*typy_objfile_data_key
;
1042 save_objfile_types (struct objfile
*objfile
, void *datum
)
1044 type_object
*obj
= (type_object
*) datum
;
1045 htab_t copied_types
;
1047 if (!gdb_python_initialized
)
1050 /* This prevents another thread from freeing the objects we're
1052 gdbpy_enter
enter_py (get_objfile_arch (objfile
), current_language
);
1054 copied_types
= create_copied_types_hash (objfile
);
1058 type_object
*next
= obj
->next
;
1060 htab_empty (copied_types
);
1062 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
1070 htab_delete (copied_types
);
1074 set_type (type_object
*obj
, struct type
*type
)
1078 if (type
&& TYPE_OBJFILE (type
))
1080 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1082 obj
->next
= ((struct pyty_type_object
*)
1083 objfile_data (objfile
, typy_objfile_data_key
));
1085 obj
->next
->prev
= obj
;
1086 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
1093 typy_dealloc (PyObject
*obj
)
1095 type_object
*type
= (type_object
*) obj
;
1098 type
->prev
->next
= type
->next
;
1099 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
1101 /* Must reset head of list. */
1102 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
1105 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
1108 type
->next
->prev
= type
->prev
;
1110 Py_TYPE (type
)->tp_free (type
);
1113 /* Return number of fields ("length" of the field dictionary). */
1116 typy_length (PyObject
*self
)
1118 struct type
*type
= ((type_object
*) self
)->type
;
1120 type
= typy_get_composite (type
);
1124 return TYPE_NFIELDS (type
);
1127 /* Implements boolean evaluation of gdb.Type. Handle this like other
1128 Python objects that don't have a meaningful truth value -- all
1132 typy_nonzero (PyObject
*self
)
1137 /* Return optimized out value of this type. */
1140 typy_optimized_out (PyObject
*self
, PyObject
*args
)
1142 struct type
*type
= ((type_object
*) self
)->type
;
1144 return value_to_value_object (allocate_optimized_out_value (type
));
1147 /* Return a gdb.Field object for the field named by the argument. */
1150 typy_getitem (PyObject
*self
, PyObject
*key
)
1152 struct type
*type
= ((type_object
*) self
)->type
;
1155 gdb::unique_xmalloc_ptr
<char> field
= python_string_to_host_string (key
);
1159 /* We want just fields of this type, not of base types, so instead of
1160 using lookup_struct_elt_type, portions of that function are
1163 type
= typy_get_composite (type
);
1167 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1169 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1171 if (t_field_name
&& (strcmp_iw (t_field_name
, field
.get ()) == 0))
1172 return convert_field (type
, i
).release ();
1174 PyErr_SetObject (PyExc_KeyError
, key
);
1178 /* Implement the "get" method on the type object. This is the
1179 same as getitem if the key is present, but returns the supplied
1180 default value or None if the key is not found. */
1183 typy_get (PyObject
*self
, PyObject
*args
)
1185 PyObject
*key
, *defval
= Py_None
, *result
;
1187 if (!PyArg_UnpackTuple (args
, "get", 1, 2, &key
, &defval
))
1190 result
= typy_getitem (self
, key
);
1194 /* typy_getitem returned error status. If the exception is
1195 KeyError, clear the exception status and return the defval
1196 instead. Otherwise return the exception unchanged. */
1197 if (!PyErr_ExceptionMatches (PyExc_KeyError
))
1205 /* Implement the "has_key" method on the type object. */
1208 typy_has_key (PyObject
*self
, PyObject
*args
)
1210 struct type
*type
= ((type_object
*) self
)->type
;
1214 if (!PyArg_ParseTuple (args
, "s", &field
))
1217 /* We want just fields of this type, not of base types, so instead of
1218 using lookup_struct_elt_type, portions of that function are
1221 type
= typy_get_composite (type
);
1225 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1227 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1229 if (t_field_name
&& (strcmp_iw (t_field_name
, field
) == 0))
1235 /* Make an iterator object to iterate over keys, values, or items. */
1238 typy_make_iter (PyObject
*self
, enum gdbpy_iter_kind kind
)
1240 typy_iterator_object
*typy_iter_obj
;
1242 /* Check that "self" is a structure or union type. */
1243 if (typy_get_composite (((type_object
*) self
)->type
) == NULL
)
1246 typy_iter_obj
= PyObject_New (typy_iterator_object
,
1247 &type_iterator_object_type
);
1248 if (typy_iter_obj
== NULL
)
1251 typy_iter_obj
->field
= 0;
1252 typy_iter_obj
->kind
= kind
;
1254 typy_iter_obj
->source
= (type_object
*) self
;
1256 return (PyObject
*) typy_iter_obj
;
1259 /* iteritems() method. */
1262 typy_iteritems (PyObject
*self
, PyObject
*args
)
1264 return typy_make_iter (self
, iter_items
);
1267 /* iterkeys() method. */
1270 typy_iterkeys (PyObject
*self
, PyObject
*args
)
1272 return typy_make_iter (self
, iter_keys
);
1275 /* Iterating over the class, same as iterkeys except for the function
1279 typy_iter (PyObject
*self
)
1281 return typy_make_iter (self
, iter_keys
);
1284 /* itervalues() method. */
1287 typy_itervalues (PyObject
*self
, PyObject
*args
)
1289 return typy_make_iter (self
, iter_values
);
1292 /* Return a reference to the type iterator. */
1295 typy_iterator_iter (PyObject
*self
)
1301 /* Return the next field in the iteration through the list of fields
1305 typy_iterator_iternext (PyObject
*self
)
1307 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) self
;
1308 struct type
*type
= iter_obj
->source
->type
;
1310 if (iter_obj
->field
< TYPE_NFIELDS (type
))
1312 gdbpy_ref
<> result
= make_fielditem (type
, iter_obj
->field
,
1316 return result
.release ();
1323 typy_iterator_dealloc (PyObject
*obj
)
1325 typy_iterator_object
*iter_obj
= (typy_iterator_object
*) obj
;
1327 Py_DECREF (iter_obj
->source
);
1328 Py_TYPE (obj
)->tp_free (obj
);
1331 /* Create a new Type referring to TYPE. */
1333 type_to_type_object (struct type
*type
)
1335 type_object
*type_obj
;
1339 /* Try not to let stub types leak out to Python. */
1340 if (TYPE_STUB (type
))
1341 type
= check_typedef (type
);
1345 /* Just ignore failures in check_typedef. */
1348 type_obj
= PyObject_New (type_object
, &type_object_type
);
1350 set_type (type_obj
, type
);
1352 return (PyObject
*) type_obj
;
1356 type_object_to_type (PyObject
*obj
)
1358 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1360 return ((type_object
*) obj
)->type
;
1365 /* Implementation of gdb.lookup_type. */
1367 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1369 static const char *keywords
[] = { "name", "block", NULL
};
1370 const char *type_name
= NULL
;
1371 struct type
*type
= NULL
;
1372 PyObject
*block_obj
= NULL
;
1373 const struct block
*block
= NULL
;
1375 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1376 &type_name
, &block_obj
))
1381 block
= block_object_to_block (block_obj
);
1384 PyErr_SetString (PyExc_RuntimeError
,
1385 _("'block' argument must be a Block."));
1390 type
= typy_lookup_typename (type_name
, block
);
1394 return type_to_type_object (type
);
1398 gdbpy_initialize_types (void)
1402 typy_objfile_data_key
1403 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1405 if (PyType_Ready (&type_object_type
) < 0)
1407 if (PyType_Ready (&field_object_type
) < 0)
1409 if (PyType_Ready (&type_iterator_object_type
) < 0)
1412 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1414 if (PyModule_AddIntConstant (gdb_module
, pyty_codes
[i
].name
,
1415 pyty_codes
[i
].code
) < 0)
1419 if (gdb_pymodule_addobject (gdb_module
, "Type",
1420 (PyObject
*) &type_object_type
) < 0)
1423 if (gdb_pymodule_addobject (gdb_module
, "TypeIterator",
1424 (PyObject
*) &type_iterator_object_type
) < 0)
1427 return gdb_pymodule_addobject (gdb_module
, "Field",
1428 (PyObject
*) &field_object_type
);
1433 static gdb_PyGetSetDef type_object_getset
[] =
1435 { "alignof", typy_get_alignof
, NULL
,
1436 "The alignment of this type, in bytes.", NULL
},
1437 { "code", typy_get_code
, NULL
,
1438 "The code for this type.", NULL
},
1439 { "name", typy_get_name
, NULL
,
1440 "The name for this type, or None.", NULL
},
1441 { "sizeof", typy_get_sizeof
, NULL
,
1442 "The size of this type, in bytes.", NULL
},
1443 { "tag", typy_get_tag
, NULL
,
1444 "The tag name for this type, or None.", NULL
},
1445 { "objfile", typy_get_objfile
, NULL
,
1446 "The objfile this type was defined in, or None.", NULL
},
1450 static PyMethodDef type_object_methods
[] =
1452 { "array", typy_array
, METH_VARARGS
,
1453 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1454 Return a type which represents an array of objects of this type.\n\
1455 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1456 If LOW_BOUND is omitted, a value of zero is used." },
1457 { "vector", typy_vector
, METH_VARARGS
,
1458 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1459 Return a type which represents a vector of objects of this type.\n\
1460 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1461 If LOW_BOUND is omitted, a value of zero is used.\n\
1462 Vectors differ from arrays in that if the current language has C-style\n\
1463 arrays, vectors don't decay to a pointer to the first element.\n\
1464 They are first class values." },
1465 { "__contains__", typy_has_key
, METH_VARARGS
,
1466 "T.__contains__(k) -> True if T has a field named k, else False" },
1467 { "const", typy_const
, METH_NOARGS
,
1468 "const () -> Type\n\
1469 Return a const variant of this type." },
1470 { "optimized_out", typy_optimized_out
, METH_NOARGS
,
1471 "optimized_out() -> Value\n\
1472 Return optimized out value of this type." },
1473 { "fields", typy_fields
, METH_NOARGS
,
1474 "fields () -> list\n\
1475 Return a list holding all the fields of this type.\n\
1476 Each field is a gdb.Field object." },
1477 { "get", typy_get
, METH_VARARGS
,
1478 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1479 otherwise returns default, if supplied, or None if not." },
1480 { "has_key", typy_has_key
, METH_VARARGS
,
1481 "T.has_key(k) -> True if T has a field named k, else False" },
1482 { "items", typy_items
, METH_NOARGS
,
1483 "items () -> list\n\
1484 Return a list of (name, field) pairs of this type.\n\
1485 Each field is a gdb.Field object." },
1486 { "iteritems", typy_iteritems
, METH_NOARGS
,
1487 "iteritems () -> an iterator over the (name, field)\n\
1488 pairs of this type. Each field is a gdb.Field object." },
1489 { "iterkeys", typy_iterkeys
, METH_NOARGS
,
1490 "iterkeys () -> an iterator over the field names of this type." },
1491 { "itervalues", typy_itervalues
, METH_NOARGS
,
1492 "itervalues () -> an iterator over the fields of this type.\n\
1493 Each field is a gdb.Field object." },
1494 { "keys", typy_field_names
, METH_NOARGS
,
1496 Return a list holding all the fields names of this type." },
1497 { "pointer", typy_pointer
, METH_NOARGS
,
1498 "pointer () -> Type\n\
1499 Return a type of pointer to this type." },
1500 { "range", typy_range
, METH_NOARGS
,
1501 "range () -> tuple\n\
1502 Return a tuple containing the lower and upper range for this type."},
1503 { "reference", typy_reference
, METH_NOARGS
,
1504 "reference () -> Type\n\
1505 Return a type of reference to this type." },
1506 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1507 "strip_typedefs () -> Type\n\
1508 Return a type formed by stripping this type of all typedefs."},
1509 { "target", typy_target
, METH_NOARGS
,
1510 "target () -> Type\n\
1511 Return the target type of this type." },
1512 { "template_argument", typy_template_argument
, METH_VARARGS
,
1513 "template_argument (arg, [block]) -> Type\n\
1514 Return the type of a template argument." },
1515 { "unqualified", typy_unqualified
, METH_NOARGS
,
1516 "unqualified () -> Type\n\
1517 Return a variant of this type without const or volatile attributes." },
1518 { "values", typy_values
, METH_NOARGS
,
1519 "values () -> list\n\
1520 Return a list holding all the fields of this type.\n\
1521 Each field is a gdb.Field object." },
1522 { "volatile", typy_volatile
, METH_NOARGS
,
1523 "volatile () -> Type\n\
1524 Return a volatile variant of this type" },
1528 static PyNumberMethods type_object_as_number
= {
1530 NULL
, /* nb_subtract */
1531 NULL
, /* nb_multiply */
1533 NULL
, /* nb_divide */
1535 NULL
, /* nb_remainder */
1536 NULL
, /* nb_divmod */
1537 NULL
, /* nb_power */
1538 NULL
, /* nb_negative */
1539 NULL
, /* nb_positive */
1540 NULL
, /* nb_absolute */
1541 typy_nonzero
, /* nb_nonzero */
1542 NULL
, /* nb_invert */
1543 NULL
, /* nb_lshift */
1544 NULL
, /* nb_rshift */
1550 NULL
, /* reserved */
1552 NULL
, /* nb_coerce */
1556 NULL
, /* nb_float */
1563 static PyMappingMethods typy_mapping
= {
1566 NULL
/* no "set" method */
1569 PyTypeObject type_object_type
=
1571 PyVarObject_HEAD_INIT (NULL
, 0)
1572 "gdb.Type", /*tp_name*/
1573 sizeof (type_object
), /*tp_basicsize*/
1575 typy_dealloc
, /*tp_dealloc*/
1581 &type_object_as_number
, /*tp_as_number*/
1582 0, /*tp_as_sequence*/
1583 &typy_mapping
, /*tp_as_mapping*/
1586 typy_str
, /*tp_str*/
1590 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1591 "GDB type object", /* tp_doc */
1592 0, /* tp_traverse */
1594 typy_richcompare
, /* tp_richcompare */
1595 0, /* tp_weaklistoffset */
1596 typy_iter
, /* tp_iter */
1597 0, /* tp_iternext */
1598 type_object_methods
, /* tp_methods */
1600 type_object_getset
, /* tp_getset */
1603 0, /* tp_descr_get */
1604 0, /* tp_descr_set */
1605 0, /* tp_dictoffset */
1611 static gdb_PyGetSetDef field_object_getset
[] =
1613 { "__dict__", gdb_py_generic_dict
, NULL
,
1614 "The __dict__ for this field.", &field_object_type
},
1618 PyTypeObject field_object_type
=
1620 PyVarObject_HEAD_INIT (NULL
, 0)
1621 "gdb.Field", /*tp_name*/
1622 sizeof (field_object
), /*tp_basicsize*/
1624 field_dealloc
, /*tp_dealloc*/
1631 0, /*tp_as_sequence*/
1632 0, /*tp_as_mapping*/
1639 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1640 "GDB field object", /* tp_doc */
1641 0, /* tp_traverse */
1643 0, /* tp_richcompare */
1644 0, /* tp_weaklistoffset */
1646 0, /* tp_iternext */
1649 field_object_getset
, /* tp_getset */
1652 0, /* tp_descr_get */
1653 0, /* tp_descr_set */
1654 offsetof (field_object
, dict
), /* tp_dictoffset */
1660 PyTypeObject type_iterator_object_type
= {
1661 PyVarObject_HEAD_INIT (NULL
, 0)
1662 "gdb.TypeIterator", /*tp_name*/
1663 sizeof (typy_iterator_object
), /*tp_basicsize*/
1665 typy_iterator_dealloc
, /*tp_dealloc*/
1672 0, /*tp_as_sequence*/
1673 0, /*tp_as_mapping*/
1680 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1681 "GDB type iterator object", /*tp_doc */
1684 0, /*tp_richcompare */
1685 0, /*tp_weaklistoffset */
1686 typy_iterator_iter
, /*tp_iter */
1687 typy_iterator_iternext
, /*tp_iternext */