1 /* Python interface to types.
3 Copyright (C) 2008, 2009 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"
30 typedef struct pyty_type_object
35 /* If a Type object is associated with an objfile, it is kept on a
36 doubly-linked list, rooted in the objfile. This lets us copy the
37 underlying struct type when the objfile is deleted. */
38 struct pyty_type_object
*prev
;
39 struct pyty_type_object
*next
;
42 static PyTypeObject type_object_type
;
45 typedef struct pyty_field_object
49 /* Dictionary holding our attributes. */
53 static PyTypeObject field_object_type
;
55 /* This is used to initialize various gdb.TYPE_ constants. */
64 #define ENTRY(X) { X, #X }
66 static struct pyty_code pyty_codes
[] =
68 ENTRY (TYPE_CODE_PTR
),
69 ENTRY (TYPE_CODE_ARRAY
),
70 ENTRY (TYPE_CODE_STRUCT
),
71 ENTRY (TYPE_CODE_UNION
),
72 ENTRY (TYPE_CODE_ENUM
),
73 ENTRY (TYPE_CODE_FLAGS
),
74 ENTRY (TYPE_CODE_FUNC
),
75 ENTRY (TYPE_CODE_INT
),
76 ENTRY (TYPE_CODE_FLT
),
77 ENTRY (TYPE_CODE_VOID
),
78 ENTRY (TYPE_CODE_SET
),
79 ENTRY (TYPE_CODE_RANGE
),
80 ENTRY (TYPE_CODE_STRING
),
81 ENTRY (TYPE_CODE_BITSTRING
),
82 ENTRY (TYPE_CODE_ERROR
),
83 ENTRY (TYPE_CODE_METHOD
),
84 ENTRY (TYPE_CODE_METHODPTR
),
85 ENTRY (TYPE_CODE_MEMBERPTR
),
86 ENTRY (TYPE_CODE_REF
),
87 ENTRY (TYPE_CODE_CHAR
),
88 ENTRY (TYPE_CODE_BOOL
),
89 ENTRY (TYPE_CODE_COMPLEX
),
90 ENTRY (TYPE_CODE_TYPEDEF
),
91 ENTRY (TYPE_CODE_NAMESPACE
),
92 ENTRY (TYPE_CODE_DECFLOAT
),
93 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
94 { TYPE_CODE_UNDEF
, NULL
}
100 field_dealloc (PyObject
*obj
)
102 field_object
*f
= (field_object
*) obj
;
103 Py_XDECREF (f
->dict
);
104 f
->ob_type
->tp_free (obj
);
110 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
113 result
->dict
= PyDict_New ();
120 return (PyObject
*) result
;
125 /* Return the code for this type. */
127 typy_get_code (PyObject
*self
, void *closure
)
129 struct type
*type
= ((type_object
*) self
)->type
;
130 return PyInt_FromLong (TYPE_CODE (type
));
133 /* Helper function for typy_fields which converts a single field to a
134 dictionary. Returns NULL on error. */
136 convert_field (struct type
*type
, int field
)
138 PyObject
*result
= field_new ();
144 if (!field_is_static (&TYPE_FIELD (type
, field
)))
146 arg
= PyLong_FromLong (TYPE_FIELD_BITPOS (type
, field
));
150 if (PyObject_SetAttrString (result
, "bitpos", arg
) < 0)
154 if (TYPE_FIELD_NAME (type
, field
))
155 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
163 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
166 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
168 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
171 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
174 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
177 /* A field can have a NULL type in some situations. */
178 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
184 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
187 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
199 /* Return a sequence of all fields. Each field is a dictionary with
200 some pre-defined keys. */
202 typy_fields (PyObject
*self
, PyObject
*args
)
206 struct type
*type
= ((type_object
*) self
)->type
;
208 /* We would like to make a tuple here, make fields immutable, and
209 then memoize the result (and perhaps make Field.type() lazy).
210 However, that can lead to cycles. */
211 result
= PyList_New (0);
213 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
215 PyObject
*dict
= convert_field (type
, i
);
221 if (PyList_Append (result
, dict
))
232 /* Return the type's tag, or None. */
234 typy_get_tag (PyObject
*self
, void *closure
)
236 struct type
*type
= ((type_object
*) self
)->type
;
237 if (!TYPE_TAG_NAME (type
))
239 return PyString_FromString (TYPE_TAG_NAME (type
));
242 /* Return the type, stripped of typedefs. */
244 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
246 struct type
*type
= ((type_object
*) self
)->type
;
248 return type_to_type_object (check_typedef (type
));
251 /* Return a Type object which represents a pointer to SELF. */
253 typy_pointer (PyObject
*self
, PyObject
*args
)
255 struct type
*type
= ((type_object
*) self
)->type
;
256 volatile struct gdb_exception except
;
258 TRY_CATCH (except
, RETURN_MASK_ALL
)
260 type
= lookup_pointer_type (type
);
262 GDB_PY_HANDLE_EXCEPTION (except
);
264 return type_to_type_object (type
);
267 /* Return a Type object which represents a reference to SELF. */
269 typy_reference (PyObject
*self
, PyObject
*args
)
271 struct type
*type
= ((type_object
*) self
)->type
;
272 volatile struct gdb_exception except
;
274 TRY_CATCH (except
, RETURN_MASK_ALL
)
276 type
= lookup_reference_type (type
);
278 GDB_PY_HANDLE_EXCEPTION (except
);
280 return type_to_type_object (type
);
283 /* Return a Type object which represents the target type of SELF. */
285 typy_target (PyObject
*self
, PyObject
*args
)
287 struct type
*type
= ((type_object
*) self
)->type
;
289 if (!TYPE_TARGET_TYPE (type
))
291 PyErr_SetString (PyExc_RuntimeError
, "type does not have a target");
295 return type_to_type_object (TYPE_TARGET_TYPE (type
));
298 /* Return a const-qualified type variant. */
300 typy_const (PyObject
*self
, PyObject
*args
)
302 struct type
*type
= ((type_object
*) self
)->type
;
303 volatile struct gdb_exception except
;
305 TRY_CATCH (except
, RETURN_MASK_ALL
)
307 type
= make_cv_type (1, 0, type
, NULL
);
309 GDB_PY_HANDLE_EXCEPTION (except
);
311 return type_to_type_object (type
);
314 /* Return a volatile-qualified type variant. */
316 typy_volatile (PyObject
*self
, PyObject
*args
)
318 struct type
*type
= ((type_object
*) self
)->type
;
319 volatile struct gdb_exception except
;
321 TRY_CATCH (except
, RETURN_MASK_ALL
)
323 type
= make_cv_type (0, 1, type
, NULL
);
325 GDB_PY_HANDLE_EXCEPTION (except
);
327 return type_to_type_object (type
);
330 /* Return an unqualified type variant. */
332 typy_unqualified (PyObject
*self
, PyObject
*args
)
334 struct type
*type
= ((type_object
*) self
)->type
;
335 volatile struct gdb_exception except
;
337 TRY_CATCH (except
, RETURN_MASK_ALL
)
339 type
= make_cv_type (0, 0, type
, NULL
);
341 GDB_PY_HANDLE_EXCEPTION (except
);
343 return type_to_type_object (type
);
346 /* Return the size of the type represented by SELF, in bytes. */
348 typy_get_sizeof (PyObject
*self
, void *closure
)
350 struct type
*type
= ((type_object
*) self
)->type
;
351 volatile struct gdb_exception except
;
353 TRY_CATCH (except
, RETURN_MASK_ALL
)
355 check_typedef (type
);
357 /* Ignore exceptions. */
359 return PyLong_FromLong (TYPE_LENGTH (type
));
363 typy_lookup_typename (char *type_name
)
365 struct type
*type
= NULL
;
366 volatile struct gdb_exception except
;
367 TRY_CATCH (except
, RETURN_MASK_ALL
)
369 if (!strncmp (type_name
, "struct ", 7))
370 type
= lookup_struct (type_name
+ 7, NULL
);
371 else if (!strncmp (type_name
, "union ", 6))
372 type
= lookup_union (type_name
+ 6, NULL
);
373 else if (!strncmp (type_name
, "enum ", 5))
374 type
= lookup_enum (type_name
+ 5, NULL
);
376 type
= lookup_typename (type_name
, NULL
, 0);
378 if (except
.reason
< 0)
380 PyErr_Format (except
.reason
== RETURN_QUIT
381 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
382 "%s", except
.message
);
390 typy_lookup_type (struct demangle_component
*demangled
)
394 enum demangle_component_type demangled_type
;
396 /* Save the type: typy_lookup_type() may (indirectly) overwrite
397 memory pointed by demangled. */
398 demangled_type
= demangled
->type
;
400 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
401 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
402 || demangled_type
== DEMANGLE_COMPONENT_CONST
403 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
405 type
= typy_lookup_type (demangled
->u
.s_binary
.left
);
409 switch (demangled_type
)
411 case DEMANGLE_COMPONENT_REFERENCE
:
412 return lookup_reference_type (type
);
413 case DEMANGLE_COMPONENT_POINTER
:
414 return lookup_pointer_type (type
);
415 case DEMANGLE_COMPONENT_CONST
:
416 return make_cv_type (1, 0, type
, NULL
);
417 case DEMANGLE_COMPONENT_VOLATILE
:
418 return make_cv_type (0, 1, type
, NULL
);
422 type_name
= cp_comp_to_string (demangled
, 10);
423 type
= typy_lookup_typename (type_name
);
430 typy_template_argument (PyObject
*self
, PyObject
*args
)
432 int i
, argno
, n_pointers
;
433 struct type
*type
= ((type_object
*) self
)->type
;
434 struct demangle_component
*demangled
;
436 struct type
*argtype
;
438 if (! PyArg_ParseTuple (args
, "i", &argno
))
441 type
= check_typedef (type
);
442 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
443 type
= check_typedef (TYPE_TARGET_TYPE (type
));
445 if (TYPE_NAME (type
) == NULL
)
447 PyErr_SetString (PyExc_RuntimeError
, "null type name");
451 /* Note -- this is not thread-safe. */
452 demangled
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
455 PyErr_SetString (PyExc_RuntimeError
, err
);
459 /* Strip off component names. */
460 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
461 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
462 demangled
= demangled
->u
.s_binary
.right
;
464 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
466 PyErr_SetString (PyExc_RuntimeError
, "type is not a template");
470 /* Skip from the template to the arguments. */
471 demangled
= demangled
->u
.s_binary
.right
;
473 for (i
= 0; demangled
&& i
< argno
; ++i
)
474 demangled
= demangled
->u
.s_binary
.right
;
478 PyErr_Format (PyExc_RuntimeError
, "no argument %d in template",
483 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
);
487 return type_to_type_object (argtype
);
491 typy_str (PyObject
*self
)
493 volatile struct gdb_exception except
;
494 char *thetype
= NULL
;
497 TRY_CATCH (except
, RETURN_MASK_ALL
)
499 struct cleanup
*old_chain
;
503 stb
= mem_fileopen ();
504 old_chain
= make_cleanup_ui_file_delete (stb
);
506 type_print (type_object_to_type (self
), "", stb
, -1);
508 thetype
= ui_file_xstrdup (stb
, &length
);
509 do_cleanups (old_chain
);
511 if (except
.reason
< 0)
514 GDB_PY_HANDLE_EXCEPTION (except
);
517 result
= PyUnicode_Decode (thetype
, strlen (thetype
), host_charset (), NULL
);
525 static const struct objfile_data
*typy_objfile_data_key
;
528 clean_up_objfile_types (struct objfile
*objfile
, void *datum
)
530 type_object
*obj
= datum
;
532 struct cleanup
*cleanup
;
533 PyGILState_STATE state
;
535 /* This prevents another thread from freeing the objects we're
537 state
= PyGILState_Ensure ();
538 cleanup
= make_cleanup_py_restore_gil (&state
);
540 copied_types
= create_copied_types_hash (objfile
);
544 type_object
*next
= obj
->next
;
546 htab_empty (copied_types
);
548 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
556 htab_delete (copied_types
);
558 do_cleanups (cleanup
);
562 set_type (type_object
*obj
, struct type
*type
)
566 if (type
&& TYPE_OBJFILE (type
))
568 struct objfile
*objfile
= TYPE_OBJFILE (type
);
570 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
572 obj
->next
->prev
= obj
;
573 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
580 typy_dealloc (PyObject
*obj
)
582 type_object
*type
= (type_object
*) obj
;
585 type
->prev
->next
= type
->next
;
586 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
588 /* Must reset head of list. */
589 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
591 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
594 type
->next
->prev
= type
->prev
;
596 type
->ob_type
->tp_free (type
);
599 /* Create a new Type referring to TYPE. */
601 type_to_type_object (struct type
*type
)
603 type_object
*type_obj
;
605 type_obj
= PyObject_New (type_object
, &type_object_type
);
607 set_type (type_obj
, type
);
609 return (PyObject
*) type_obj
;
613 type_object_to_type (PyObject
*obj
)
615 if (! PyObject_TypeCheck (obj
, &type_object_type
))
617 return ((type_object
*) obj
)->type
;
622 /* Implementation of gdb.lookup_type. */
624 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
626 static char *keywords
[] = { "name", NULL
};
627 char *type_name
= NULL
;
628 struct type
*type
= NULL
;
630 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &type_name
))
633 type
= typy_lookup_typename (type_name
);
637 return (PyObject
*) type_to_type_object (type
);
641 gdbpy_initialize_types (void)
645 typy_objfile_data_key
646 = register_objfile_data_with_cleanup (clean_up_objfile_types
);
648 if (PyType_Ready (&type_object_type
) < 0)
650 if (PyType_Ready (&field_object_type
) < 0)
653 for (i
= 0; pyty_codes
[i
].name
; ++i
)
655 if (PyModule_AddIntConstant (gdb_module
,
656 /* Cast needed for Python 2.4. */
657 (char *) pyty_codes
[i
].name
,
658 pyty_codes
[i
].code
) < 0)
662 Py_INCREF (&type_object_type
);
663 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
665 Py_INCREF (&field_object_type
);
666 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
671 static PyGetSetDef type_object_getset
[] =
673 { "code", typy_get_code
, NULL
,
674 "The code for this type.", NULL
},
675 { "sizeof", typy_get_sizeof
, NULL
,
676 "The size of this type, in bytes.", NULL
},
677 { "tag", typy_get_tag
, NULL
,
678 "The tag name for this type, or None.", NULL
},
682 static PyMethodDef type_object_methods
[] =
684 { "const", typy_const
, METH_NOARGS
,
686 Return a const variant of this type." },
687 { "fields", typy_fields
, METH_NOARGS
,
689 Return a sequence holding all the fields of this type.\n\
690 Each field is a dictionary." },
691 { "pointer", typy_pointer
, METH_NOARGS
,
692 "pointer () -> Type\n\
693 Return a type of pointer to this type." },
694 { "reference", typy_reference
, METH_NOARGS
,
695 "reference () -> Type\n\
696 Return a type of reference to this type." },
697 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
698 "strip_typedefs () -> Type\n\
699 Return a type formed by stripping this type of all typedefs."},
700 { "target", typy_target
, METH_NOARGS
,
701 "target () -> Type\n\
702 Return the target type of this type." },
703 { "template_argument", typy_template_argument
, METH_VARARGS
,
704 "template_argument (arg) -> Type\n\
705 Return the type of a template argument." },
706 { "unqualified", typy_unqualified
, METH_NOARGS
,
707 "unqualified () -> Type\n\
708 Return a variant of this type without const or volatile attributes." },
709 { "volatile", typy_volatile
, METH_NOARGS
,
710 "volatile () -> Type\n\
711 Return a volatile variant of this type" },
715 static PyTypeObject type_object_type
=
717 PyObject_HEAD_INIT (NULL
)
719 "gdb.Type", /*tp_name*/
720 sizeof (type_object
), /*tp_basicsize*/
722 typy_dealloc
, /*tp_dealloc*/
729 0, /*tp_as_sequence*/
737 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
738 "GDB type object", /* tp_doc */
741 0, /* tp_richcompare */
742 0, /* tp_weaklistoffset */
745 type_object_methods
, /* tp_methods */
747 type_object_getset
, /* tp_getset */
750 0, /* tp_descr_get */
751 0, /* tp_descr_set */
752 0, /* tp_dictoffset */
758 static PyTypeObject field_object_type
=
760 PyObject_HEAD_INIT (NULL
)
762 "gdb.Field", /*tp_name*/
763 sizeof (field_object
), /*tp_basicsize*/
765 field_dealloc
, /*tp_dealloc*/
772 0, /*tp_as_sequence*/
780 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
781 "GDB field object", /* tp_doc */
784 0, /* tp_richcompare */
785 0, /* tp_weaklistoffset */
793 0, /* tp_descr_get */
794 0, /* tp_descr_set */
795 offsetof (field_object
, dict
), /* tp_dictoffset */