1 /* Python interface to types.
3 Copyright (C) 2008, 2009, 2010 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 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 static PyTypeObject type_object_type
;
46 typedef struct pyty_field_object
50 /* Dictionary holding our attributes. */
54 static PyTypeObject field_object_type
;
56 /* This is used to initialize various gdb.TYPE_ constants. */
65 #define ENTRY(X) { X, #X }
67 static struct pyty_code pyty_codes
[] =
69 ENTRY (TYPE_CODE_PTR
),
70 ENTRY (TYPE_CODE_ARRAY
),
71 ENTRY (TYPE_CODE_STRUCT
),
72 ENTRY (TYPE_CODE_UNION
),
73 ENTRY (TYPE_CODE_ENUM
),
74 ENTRY (TYPE_CODE_FLAGS
),
75 ENTRY (TYPE_CODE_FUNC
),
76 ENTRY (TYPE_CODE_INT
),
77 ENTRY (TYPE_CODE_FLT
),
78 ENTRY (TYPE_CODE_VOID
),
79 ENTRY (TYPE_CODE_SET
),
80 ENTRY (TYPE_CODE_RANGE
),
81 ENTRY (TYPE_CODE_STRING
),
82 ENTRY (TYPE_CODE_BITSTRING
),
83 ENTRY (TYPE_CODE_ERROR
),
84 ENTRY (TYPE_CODE_METHOD
),
85 ENTRY (TYPE_CODE_METHODPTR
),
86 ENTRY (TYPE_CODE_MEMBERPTR
),
87 ENTRY (TYPE_CODE_REF
),
88 ENTRY (TYPE_CODE_CHAR
),
89 ENTRY (TYPE_CODE_BOOL
),
90 ENTRY (TYPE_CODE_COMPLEX
),
91 ENTRY (TYPE_CODE_TYPEDEF
),
92 ENTRY (TYPE_CODE_NAMESPACE
),
93 ENTRY (TYPE_CODE_DECFLOAT
),
94 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
95 { TYPE_CODE_UNDEF
, NULL
}
101 field_dealloc (PyObject
*obj
)
103 field_object
*f
= (field_object
*) obj
;
104 Py_XDECREF (f
->dict
);
105 f
->ob_type
->tp_free (obj
);
111 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
114 result
->dict
= PyDict_New ();
121 return (PyObject
*) result
;
126 /* Return the code for this type. */
128 typy_get_code (PyObject
*self
, void *closure
)
130 struct type
*type
= ((type_object
*) self
)->type
;
131 return PyInt_FromLong (TYPE_CODE (type
));
134 /* Helper function for typy_fields which converts a single field to a
135 dictionary. Returns NULL on error. */
137 convert_field (struct type
*type
, int field
)
139 PyObject
*result
= field_new ();
145 if (!field_is_static (&TYPE_FIELD (type
, field
)))
147 arg
= PyLong_FromLong (TYPE_FIELD_BITPOS (type
, field
));
151 if (PyObject_SetAttrString (result
, "bitpos", arg
) < 0)
155 if (TYPE_FIELD_NAME (type
, field
))
156 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
164 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
167 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
169 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
172 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
173 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
177 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
180 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
183 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
186 /* A field can have a NULL type in some situations. */
187 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
193 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
196 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
208 /* Return a sequence of all fields. Each field is a dictionary with
209 some pre-defined keys. */
211 typy_fields (PyObject
*self
, PyObject
*args
)
215 struct type
*type
= ((type_object
*) self
)->type
;
217 /* We would like to make a tuple here, make fields immutable, and
218 then memoize the result (and perhaps make Field.type() lazy).
219 However, that can lead to cycles. */
220 result
= PyList_New (0);
222 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
224 PyObject
*dict
= convert_field (type
, i
);
230 if (PyList_Append (result
, dict
))
241 /* Return the type's tag, or None. */
243 typy_get_tag (PyObject
*self
, void *closure
)
245 struct type
*type
= ((type_object
*) self
)->type
;
246 if (!TYPE_TAG_NAME (type
))
248 return PyString_FromString (TYPE_TAG_NAME (type
));
251 /* Return the type, stripped of typedefs. */
253 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
255 struct type
*type
= ((type_object
*) self
)->type
;
257 return type_to_type_object (check_typedef (type
));
260 /* Return a Type object which represents a pointer to SELF. */
262 typy_pointer (PyObject
*self
, PyObject
*args
)
264 struct type
*type
= ((type_object
*) self
)->type
;
265 volatile struct gdb_exception except
;
267 TRY_CATCH (except
, RETURN_MASK_ALL
)
269 type
= lookup_pointer_type (type
);
271 GDB_PY_HANDLE_EXCEPTION (except
);
273 return type_to_type_object (type
);
276 /* Return the range of a type represented by SELF. The return type is
277 a tuple. The first element of the tuple contains the low bound,
278 while the second element of the tuple contains the high bound. */
280 typy_range (PyObject
*self
, PyObject
*args
)
282 struct type
*type
= ((type_object
*) self
)->type
;
284 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
285 /* Initialize these to appease GCC warnings. */
286 LONGEST low
= 0, high
= 0;
288 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
289 && TYPE_CODE (type
) != TYPE_CODE_STRING
290 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
292 PyErr_SetString (PyExc_RuntimeError
,
293 "This type does not have a range.");
297 switch (TYPE_CODE (type
))
299 case TYPE_CODE_ARRAY
:
300 case TYPE_CODE_STRING
:
301 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
302 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
304 case TYPE_CODE_RANGE
:
305 low
= TYPE_LOW_BOUND (type
);
306 high
= TYPE_HIGH_BOUND (type
);
310 low_bound
= PyLong_FromLong (low
);
314 high_bound
= PyLong_FromLong (high
);
318 result
= PyTuple_New (2);
322 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
327 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
329 Py_DECREF (high_bound
);
336 Py_XDECREF (high_bound
);
337 Py_XDECREF (low_bound
);
341 /* Return a Type object which represents a reference to SELF. */
343 typy_reference (PyObject
*self
, PyObject
*args
)
345 struct type
*type
= ((type_object
*) self
)->type
;
346 volatile struct gdb_exception except
;
348 TRY_CATCH (except
, RETURN_MASK_ALL
)
350 type
= lookup_reference_type (type
);
352 GDB_PY_HANDLE_EXCEPTION (except
);
354 return type_to_type_object (type
);
357 /* Return a Type object which represents the target type of SELF. */
359 typy_target (PyObject
*self
, PyObject
*args
)
361 struct type
*type
= ((type_object
*) self
)->type
;
363 if (!TYPE_TARGET_TYPE (type
))
365 PyErr_SetString (PyExc_RuntimeError
, "type does not have a target");
369 return type_to_type_object (TYPE_TARGET_TYPE (type
));
372 /* Return a const-qualified type variant. */
374 typy_const (PyObject
*self
, PyObject
*args
)
376 struct type
*type
= ((type_object
*) self
)->type
;
377 volatile struct gdb_exception except
;
379 TRY_CATCH (except
, RETURN_MASK_ALL
)
381 type
= make_cv_type (1, 0, type
, NULL
);
383 GDB_PY_HANDLE_EXCEPTION (except
);
385 return type_to_type_object (type
);
388 /* Return a volatile-qualified type variant. */
390 typy_volatile (PyObject
*self
, PyObject
*args
)
392 struct type
*type
= ((type_object
*) self
)->type
;
393 volatile struct gdb_exception except
;
395 TRY_CATCH (except
, RETURN_MASK_ALL
)
397 type
= make_cv_type (0, 1, type
, NULL
);
399 GDB_PY_HANDLE_EXCEPTION (except
);
401 return type_to_type_object (type
);
404 /* Return an unqualified type variant. */
406 typy_unqualified (PyObject
*self
, PyObject
*args
)
408 struct type
*type
= ((type_object
*) self
)->type
;
409 volatile struct gdb_exception except
;
411 TRY_CATCH (except
, RETURN_MASK_ALL
)
413 type
= make_cv_type (0, 0, type
, NULL
);
415 GDB_PY_HANDLE_EXCEPTION (except
);
417 return type_to_type_object (type
);
420 /* Return the size of the type represented by SELF, in bytes. */
422 typy_get_sizeof (PyObject
*self
, void *closure
)
424 struct type
*type
= ((type_object
*) self
)->type
;
425 volatile struct gdb_exception except
;
427 TRY_CATCH (except
, RETURN_MASK_ALL
)
429 check_typedef (type
);
431 /* Ignore exceptions. */
433 return PyLong_FromLong (TYPE_LENGTH (type
));
437 typy_lookup_typename (char *type_name
, struct block
*block
)
439 struct type
*type
= NULL
;
440 volatile struct gdb_exception except
;
441 TRY_CATCH (except
, RETURN_MASK_ALL
)
443 if (!strncmp (type_name
, "struct ", 7))
444 type
= lookup_struct (type_name
+ 7, NULL
);
445 else if (!strncmp (type_name
, "union ", 6))
446 type
= lookup_union (type_name
+ 6, NULL
);
447 else if (!strncmp (type_name
, "enum ", 5))
448 type
= lookup_enum (type_name
+ 5, NULL
);
450 type
= lookup_typename (python_language
, python_gdbarch
,
451 type_name
, block
, 0);
453 if (except
.reason
< 0)
455 PyErr_Format (except
.reason
== RETURN_QUIT
456 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
457 "%s", except
.message
);
465 typy_lookup_type (struct demangle_component
*demangled
,
470 enum demangle_component_type demangled_type
;
472 /* Save the type: typy_lookup_type() may (indirectly) overwrite
473 memory pointed by demangled. */
474 demangled_type
= demangled
->type
;
476 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
477 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
478 || demangled_type
== DEMANGLE_COMPONENT_CONST
479 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
481 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
485 switch (demangled_type
)
487 case DEMANGLE_COMPONENT_REFERENCE
:
488 return lookup_reference_type (type
);
489 case DEMANGLE_COMPONENT_POINTER
:
490 return lookup_pointer_type (type
);
491 case DEMANGLE_COMPONENT_CONST
:
492 return make_cv_type (1, 0, type
, NULL
);
493 case DEMANGLE_COMPONENT_VOLATILE
:
494 return make_cv_type (0, 1, type
, NULL
);
498 type_name
= cp_comp_to_string (demangled
, 10);
499 type
= typy_lookup_typename (type_name
, block
);
506 typy_template_argument (PyObject
*self
, PyObject
*args
)
508 int i
, argno
, n_pointers
;
509 struct type
*type
= ((type_object
*) self
)->type
;
510 struct demangle_component
*demangled
;
512 struct type
*argtype
;
513 struct block
*block
= NULL
;
514 PyObject
*block_obj
= NULL
;
516 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
521 block
= block_object_to_block (block_obj
);
524 PyErr_SetString (PyExc_RuntimeError
,
525 _("Second argument must be block."));
530 type
= check_typedef (type
);
531 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
532 type
= check_typedef (TYPE_TARGET_TYPE (type
));
534 if (TYPE_NAME (type
) == NULL
)
536 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
540 /* Note -- this is not thread-safe. */
541 demangled
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
544 PyErr_SetString (PyExc_RuntimeError
, err
);
548 /* Strip off component names. */
549 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
550 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
551 demangled
= demangled
->u
.s_binary
.right
;
553 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
555 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
559 /* Skip from the template to the arguments. */
560 demangled
= demangled
->u
.s_binary
.right
;
562 for (i
= 0; demangled
&& i
< argno
; ++i
)
563 demangled
= demangled
->u
.s_binary
.right
;
567 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
572 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
576 return type_to_type_object (argtype
);
580 typy_str (PyObject
*self
)
582 volatile struct gdb_exception except
;
583 char *thetype
= NULL
;
587 TRY_CATCH (except
, RETURN_MASK_ALL
)
589 struct cleanup
*old_chain
;
592 stb
= mem_fileopen ();
593 old_chain
= make_cleanup_ui_file_delete (stb
);
595 type_print (type_object_to_type (self
), "", stb
, -1);
597 thetype
= ui_file_xstrdup (stb
, &length
);
598 do_cleanups (old_chain
);
600 if (except
.reason
< 0)
603 GDB_PY_HANDLE_EXCEPTION (except
);
606 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
614 static const struct objfile_data
*typy_objfile_data_key
;
617 save_objfile_types (struct objfile
*objfile
, void *datum
)
619 type_object
*obj
= datum
;
621 struct cleanup
*cleanup
;
623 /* This prevents another thread from freeing the objects we're
625 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
627 copied_types
= create_copied_types_hash (objfile
);
631 type_object
*next
= obj
->next
;
633 htab_empty (copied_types
);
635 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
643 htab_delete (copied_types
);
645 do_cleanups (cleanup
);
649 set_type (type_object
*obj
, struct type
*type
)
653 if (type
&& TYPE_OBJFILE (type
))
655 struct objfile
*objfile
= TYPE_OBJFILE (type
);
657 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
659 obj
->next
->prev
= obj
;
660 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
667 typy_dealloc (PyObject
*obj
)
669 type_object
*type
= (type_object
*) obj
;
672 type
->prev
->next
= type
->next
;
673 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
675 /* Must reset head of list. */
676 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
678 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
681 type
->next
->prev
= type
->prev
;
683 type
->ob_type
->tp_free (type
);
686 /* Create a new Type referring to TYPE. */
688 type_to_type_object (struct type
*type
)
690 type_object
*type_obj
;
692 type_obj
= PyObject_New (type_object
, &type_object_type
);
694 set_type (type_obj
, type
);
696 return (PyObject
*) type_obj
;
700 type_object_to_type (PyObject
*obj
)
702 if (! PyObject_TypeCheck (obj
, &type_object_type
))
704 return ((type_object
*) obj
)->type
;
709 /* Implementation of gdb.lookup_type. */
711 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
713 static char *keywords
[] = { "name", "block", NULL
};
714 char *type_name
= NULL
;
715 struct type
*type
= NULL
;
716 PyObject
*block_obj
= NULL
;
717 struct block
*block
= NULL
;
719 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
720 &type_name
, &block_obj
))
725 block
= block_object_to_block (block_obj
);
728 PyErr_SetString (PyExc_RuntimeError
,
729 _("'block' argument must be a Block."));
734 type
= typy_lookup_typename (type_name
, block
);
738 return (PyObject
*) type_to_type_object (type
);
742 gdbpy_initialize_types (void)
746 typy_objfile_data_key
747 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
749 if (PyType_Ready (&type_object_type
) < 0)
751 if (PyType_Ready (&field_object_type
) < 0)
754 for (i
= 0; pyty_codes
[i
].name
; ++i
)
756 if (PyModule_AddIntConstant (gdb_module
,
757 /* Cast needed for Python 2.4. */
758 (char *) pyty_codes
[i
].name
,
759 pyty_codes
[i
].code
) < 0)
763 Py_INCREF (&type_object_type
);
764 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
766 Py_INCREF (&field_object_type
);
767 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
772 static PyGetSetDef type_object_getset
[] =
774 { "code", typy_get_code
, NULL
,
775 "The code for this type.", NULL
},
776 { "sizeof", typy_get_sizeof
, NULL
,
777 "The size of this type, in bytes.", NULL
},
778 { "tag", typy_get_tag
, NULL
,
779 "The tag name for this type, or None.", NULL
},
783 static PyMethodDef type_object_methods
[] =
785 { "const", typy_const
, METH_NOARGS
,
787 Return a const variant of this type." },
788 { "fields", typy_fields
, METH_NOARGS
,
790 Return a sequence holding all the fields of this type.\n\
791 Each field is a dictionary." },
792 { "pointer", typy_pointer
, METH_NOARGS
,
793 "pointer () -> Type\n\
794 Return a type of pointer to this type." },
795 { "range", typy_range
, METH_NOARGS
,
796 "range () -> tuple\n\
797 Return a tuple containing the lower and upper range for this type."},
798 { "reference", typy_reference
, METH_NOARGS
,
799 "reference () -> Type\n\
800 Return a type of reference to this type." },
801 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
802 "strip_typedefs () -> Type\n\
803 Return a type formed by stripping this type of all typedefs."},
804 { "target", typy_target
, METH_NOARGS
,
805 "target () -> Type\n\
806 Return the target type of this type." },
807 { "template_argument", typy_template_argument
, METH_VARARGS
,
808 "template_argument (arg, [block]) -> Type\n\
809 Return the type of a template argument." },
810 { "unqualified", typy_unqualified
, METH_NOARGS
,
811 "unqualified () -> Type\n\
812 Return a variant of this type without const or volatile attributes." },
813 { "volatile", typy_volatile
, METH_NOARGS
,
814 "volatile () -> Type\n\
815 Return a volatile variant of this type" },
819 static PyTypeObject type_object_type
=
821 PyObject_HEAD_INIT (NULL
)
823 "gdb.Type", /*tp_name*/
824 sizeof (type_object
), /*tp_basicsize*/
826 typy_dealloc
, /*tp_dealloc*/
833 0, /*tp_as_sequence*/
841 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
842 "GDB type object", /* tp_doc */
845 0, /* tp_richcompare */
846 0, /* tp_weaklistoffset */
849 type_object_methods
, /* tp_methods */
851 type_object_getset
, /* tp_getset */
854 0, /* tp_descr_get */
855 0, /* tp_descr_set */
856 0, /* tp_dictoffset */
862 static PyTypeObject field_object_type
=
864 PyObject_HEAD_INIT (NULL
)
866 "gdb.Field", /*tp_name*/
867 sizeof (field_object
), /*tp_basicsize*/
869 field_dealloc
, /*tp_dealloc*/
876 0, /*tp_as_sequence*/
884 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
885 "GDB field object", /* tp_doc */
888 0, /* tp_richcompare */
889 0, /* tp_weaklistoffset */
897 0, /* tp_descr_get */
898 0, /* tp_descr_set */
899 offsetof (field_object
, dict
), /* tp_dictoffset */