* NEWS: Mention non-stop mode.
[binutils-gdb.git] / gdb / python / py-type.c
blob590d90a9083f7e18210ce4dc940efcac38cb5c7d
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/>. */
20 #include "defs.h"
21 #include "value.h"
22 #include "exceptions.h"
23 #include "python-internal.h"
24 #include "charset.h"
25 #include "gdbtypes.h"
26 #include "cp-support.h"
27 #include "demangle.h"
28 #include "objfiles.h"
29 #include "language.h"
31 typedef struct pyty_type_object
33 PyObject_HEAD
34 struct type *type;
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;
41 } type_object;
43 static PyTypeObject type_object_type;
45 /* A Field object. */
46 typedef struct pyty_field_object
48 PyObject_HEAD
50 /* Dictionary holding our attributes. */
51 PyObject *dict;
52 } field_object;
54 static PyTypeObject field_object_type;
56 /* This is used to initialize various gdb.TYPE_ constants. */
57 struct pyty_code
59 /* The code. */
60 enum type_code code;
61 /* The name. */
62 const char *name;
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 }
100 static void
101 field_dealloc (PyObject *obj)
103 field_object *f = (field_object *) obj;
104 Py_XDECREF (f->dict);
105 f->ob_type->tp_free (obj);
108 static PyObject *
109 field_new (void)
111 field_object *result = PyObject_New (field_object, &field_object_type);
112 if (result)
114 result->dict = PyDict_New ();
115 if (!result->dict)
117 Py_DECREF (result);
118 result = NULL;
121 return (PyObject *) result;
126 /* Return the code for this type. */
127 static PyObject *
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. */
136 static PyObject *
137 convert_field (struct type *type, int field)
139 PyObject *result = field_new ();
140 PyObject *arg;
142 if (!result)
143 return NULL;
145 if (!field_is_static (&TYPE_FIELD (type, field)))
147 arg = PyLong_FromLong (TYPE_FIELD_BITPOS (type, field));
148 if (!arg)
149 goto fail;
151 if (PyObject_SetAttrString (result, "bitpos", arg) < 0)
152 goto failarg;
155 if (TYPE_FIELD_NAME (type, field))
156 arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
157 else
159 arg = Py_None;
160 Py_INCREF (arg);
162 if (!arg)
163 goto fail;
164 if (PyObject_SetAttrString (result, "name", arg) < 0)
165 goto failarg;
167 arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
168 Py_INCREF (arg);
169 if (PyObject_SetAttrString (result, "artificial", arg) < 0)
170 goto failarg;
172 arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
173 if (!arg)
174 goto fail;
175 if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
176 goto failarg;
178 /* A field can have a NULL type in some situations. */
179 if (TYPE_FIELD_TYPE (type, field) == NULL)
181 arg = Py_None;
182 Py_INCREF (arg);
184 else
185 arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
186 if (!arg)
187 goto fail;
188 if (PyObject_SetAttrString (result, "type", arg) < 0)
189 goto failarg;
191 return result;
193 failarg:
194 Py_DECREF (arg);
195 fail:
196 Py_DECREF (result);
197 return NULL;
200 /* Return a sequence of all fields. Each field is a dictionary with
201 some pre-defined keys. */
202 static PyObject *
203 typy_fields (PyObject *self, PyObject *args)
205 PyObject *result;
206 int i;
207 struct type *type = ((type_object *) self)->type;
209 /* We would like to make a tuple here, make fields immutable, and
210 then memoize the result (and perhaps make Field.type() lazy).
211 However, that can lead to cycles. */
212 result = PyList_New (0);
214 for (i = 0; i < TYPE_NFIELDS (type); ++i)
216 PyObject *dict = convert_field (type, i);
217 if (!dict)
219 Py_DECREF (result);
220 return NULL;
222 if (PyList_Append (result, dict))
224 Py_DECREF (dict);
225 Py_DECREF (result);
226 return NULL;
230 return result;
233 /* Return the type's tag, or None. */
234 static PyObject *
235 typy_get_tag (PyObject *self, void *closure)
237 struct type *type = ((type_object *) self)->type;
238 if (!TYPE_TAG_NAME (type))
239 Py_RETURN_NONE;
240 return PyString_FromString (TYPE_TAG_NAME (type));
243 /* Return the type, stripped of typedefs. */
244 static PyObject *
245 typy_strip_typedefs (PyObject *self, PyObject *args)
247 struct type *type = ((type_object *) self)->type;
249 return type_to_type_object (check_typedef (type));
252 /* Return a Type object which represents a pointer to SELF. */
253 static PyObject *
254 typy_pointer (PyObject *self, PyObject *args)
256 struct type *type = ((type_object *) self)->type;
257 volatile struct gdb_exception except;
259 TRY_CATCH (except, RETURN_MASK_ALL)
261 type = lookup_pointer_type (type);
263 GDB_PY_HANDLE_EXCEPTION (except);
265 return type_to_type_object (type);
268 /* Return a Type object which represents a reference to SELF. */
269 static PyObject *
270 typy_reference (PyObject *self, PyObject *args)
272 struct type *type = ((type_object *) self)->type;
273 volatile struct gdb_exception except;
275 TRY_CATCH (except, RETURN_MASK_ALL)
277 type = lookup_reference_type (type);
279 GDB_PY_HANDLE_EXCEPTION (except);
281 return type_to_type_object (type);
284 /* Return a Type object which represents the target type of SELF. */
285 static PyObject *
286 typy_target (PyObject *self, PyObject *args)
288 struct type *type = ((type_object *) self)->type;
290 if (!TYPE_TARGET_TYPE (type))
292 PyErr_SetString (PyExc_RuntimeError, "type does not have a target");
293 return NULL;
296 return type_to_type_object (TYPE_TARGET_TYPE (type));
299 /* Return a const-qualified type variant. */
300 static PyObject *
301 typy_const (PyObject *self, PyObject *args)
303 struct type *type = ((type_object *) self)->type;
304 volatile struct gdb_exception except;
306 TRY_CATCH (except, RETURN_MASK_ALL)
308 type = make_cv_type (1, 0, type, NULL);
310 GDB_PY_HANDLE_EXCEPTION (except);
312 return type_to_type_object (type);
315 /* Return a volatile-qualified type variant. */
316 static PyObject *
317 typy_volatile (PyObject *self, PyObject *args)
319 struct type *type = ((type_object *) self)->type;
320 volatile struct gdb_exception except;
322 TRY_CATCH (except, RETURN_MASK_ALL)
324 type = make_cv_type (0, 1, type, NULL);
326 GDB_PY_HANDLE_EXCEPTION (except);
328 return type_to_type_object (type);
331 /* Return an unqualified type variant. */
332 static PyObject *
333 typy_unqualified (PyObject *self, PyObject *args)
335 struct type *type = ((type_object *) self)->type;
336 volatile struct gdb_exception except;
338 TRY_CATCH (except, RETURN_MASK_ALL)
340 type = make_cv_type (0, 0, type, NULL);
342 GDB_PY_HANDLE_EXCEPTION (except);
344 return type_to_type_object (type);
347 /* Return the size of the type represented by SELF, in bytes. */
348 static PyObject *
349 typy_get_sizeof (PyObject *self, void *closure)
351 struct type *type = ((type_object *) self)->type;
352 volatile struct gdb_exception except;
354 TRY_CATCH (except, RETURN_MASK_ALL)
356 check_typedef (type);
358 /* Ignore exceptions. */
360 return PyLong_FromLong (TYPE_LENGTH (type));
363 static struct type *
364 typy_lookup_typename (char *type_name)
366 struct type *type = NULL;
367 volatile struct gdb_exception except;
368 TRY_CATCH (except, RETURN_MASK_ALL)
370 if (!strncmp (type_name, "struct ", 7))
371 type = lookup_struct (type_name + 7, NULL);
372 else if (!strncmp (type_name, "union ", 6))
373 type = lookup_union (type_name + 6, NULL);
374 else if (!strncmp (type_name, "enum ", 5))
375 type = lookup_enum (type_name + 5, NULL);
376 else
377 type = lookup_typename (python_language, python_gdbarch,
378 type_name, NULL, 0);
380 if (except.reason < 0)
382 PyErr_Format (except.reason == RETURN_QUIT
383 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
384 "%s", except.message);
385 return NULL;
388 return type;
391 static struct type *
392 typy_lookup_type (struct demangle_component *demangled)
394 struct type *type;
395 char *type_name;
396 enum demangle_component_type demangled_type;
398 /* Save the type: typy_lookup_type() may (indirectly) overwrite
399 memory pointed by demangled. */
400 demangled_type = demangled->type;
402 if (demangled_type == DEMANGLE_COMPONENT_POINTER
403 || demangled_type == DEMANGLE_COMPONENT_REFERENCE
404 || demangled_type == DEMANGLE_COMPONENT_CONST
405 || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
407 type = typy_lookup_type (demangled->u.s_binary.left);
408 if (! type)
409 return NULL;
411 switch (demangled_type)
413 case DEMANGLE_COMPONENT_REFERENCE:
414 return lookup_reference_type (type);
415 case DEMANGLE_COMPONENT_POINTER:
416 return lookup_pointer_type (type);
417 case DEMANGLE_COMPONENT_CONST:
418 return make_cv_type (1, 0, type, NULL);
419 case DEMANGLE_COMPONENT_VOLATILE:
420 return make_cv_type (0, 1, type, NULL);
424 type_name = cp_comp_to_string (demangled, 10);
425 type = typy_lookup_typename (type_name);
426 xfree (type_name);
428 return type;
431 static PyObject *
432 typy_template_argument (PyObject *self, PyObject *args)
434 int i, argno, n_pointers;
435 struct type *type = ((type_object *) self)->type;
436 struct demangle_component *demangled;
437 const char *err;
438 struct type *argtype;
440 if (! PyArg_ParseTuple (args, "i", &argno))
441 return NULL;
443 type = check_typedef (type);
444 if (TYPE_CODE (type) == TYPE_CODE_REF)
445 type = check_typedef (TYPE_TARGET_TYPE (type));
447 if (TYPE_NAME (type) == NULL)
449 PyErr_SetString (PyExc_RuntimeError, "null type name");
450 return NULL;
453 /* Note -- this is not thread-safe. */
454 demangled = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
455 if (! demangled)
457 PyErr_SetString (PyExc_RuntimeError, err);
458 return NULL;
461 /* Strip off component names. */
462 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
463 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
464 demangled = demangled->u.s_binary.right;
466 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
468 PyErr_SetString (PyExc_RuntimeError, "type is not a template");
469 return NULL;
472 /* Skip from the template to the arguments. */
473 demangled = demangled->u.s_binary.right;
475 for (i = 0; demangled && i < argno; ++i)
476 demangled = demangled->u.s_binary.right;
478 if (! demangled)
480 PyErr_Format (PyExc_RuntimeError, "no argument %d in template",
481 argno);
482 return NULL;
485 argtype = typy_lookup_type (demangled->u.s_binary.left);
486 if (! argtype)
487 return NULL;
489 return type_to_type_object (argtype);
492 static PyObject *
493 typy_str (PyObject *self)
495 volatile struct gdb_exception except;
496 char *thetype = NULL;
497 long length = 0;
498 PyObject *result;
500 TRY_CATCH (except, RETURN_MASK_ALL)
502 struct cleanup *old_chain;
503 struct ui_file *stb;
505 stb = mem_fileopen ();
506 old_chain = make_cleanup_ui_file_delete (stb);
508 type_print (type_object_to_type (self), "", stb, -1);
510 thetype = ui_file_xstrdup (stb, &length);
511 do_cleanups (old_chain);
513 if (except.reason < 0)
515 xfree (thetype);
516 GDB_PY_HANDLE_EXCEPTION (except);
519 result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
520 xfree (thetype);
522 return result;
527 static const struct objfile_data *typy_objfile_data_key;
529 static void
530 save_objfile_types (struct objfile *objfile, void *datum)
532 type_object *obj = datum;
533 htab_t copied_types;
534 struct cleanup *cleanup;
536 /* This prevents another thread from freeing the objects we're
537 operating on. */
538 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
540 copied_types = create_copied_types_hash (objfile);
542 while (obj)
544 type_object *next = obj->next;
546 htab_empty (copied_types);
548 obj->type = copy_type_recursive (objfile, obj->type, copied_types);
550 obj->next = NULL;
551 obj->prev = NULL;
553 obj = next;
556 htab_delete (copied_types);
558 do_cleanups (cleanup);
561 static void
562 set_type (type_object *obj, struct type *type)
564 obj->type = type;
565 obj->prev = NULL;
566 if (type && TYPE_OBJFILE (type))
568 struct objfile *objfile = TYPE_OBJFILE (type);
570 obj->next = objfile_data (objfile, typy_objfile_data_key);
571 if (obj->next)
572 obj->next->prev = obj;
573 set_objfile_data (objfile, typy_objfile_data_key, obj);
575 else
576 obj->next = NULL;
579 static void
580 typy_dealloc (PyObject *obj)
582 type_object *type = (type_object *) obj;
584 if (type->prev)
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);
590 if (objfile)
591 set_objfile_data (objfile, typy_objfile_data_key, type->next);
593 if (type->next)
594 type->next->prev = type->prev;
596 type->ob_type->tp_free (type);
599 /* Create a new Type referring to TYPE. */
600 PyObject *
601 type_to_type_object (struct type *type)
603 type_object *type_obj;
605 type_obj = PyObject_New (type_object, &type_object_type);
606 if (type_obj)
607 set_type (type_obj, type);
609 return (PyObject *) type_obj;
612 struct type *
613 type_object_to_type (PyObject *obj)
615 if (! PyObject_TypeCheck (obj, &type_object_type))
616 return NULL;
617 return ((type_object *) obj)->type;
622 /* Implementation of gdb.lookup_type. */
623 PyObject *
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))
631 return NULL;
633 type = typy_lookup_typename (type_name);
634 if (! type)
635 return NULL;
637 return (PyObject *) type_to_type_object (type);
640 void
641 gdbpy_initialize_types (void)
643 int i;
645 typy_objfile_data_key
646 = register_objfile_data_with_cleanup (save_objfile_types, NULL);
648 if (PyType_Ready (&type_object_type) < 0)
649 return;
650 if (PyType_Ready (&field_object_type) < 0)
651 return;
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)
659 return;
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 },
679 { NULL }
682 static PyMethodDef type_object_methods[] =
684 { "const", typy_const, METH_NOARGS,
685 "const () -> Type\n\
686 Return a const variant of this type." },
687 { "fields", typy_fields, METH_NOARGS,
688 "field () -> list\n\
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" },
712 { NULL }
715 static PyTypeObject type_object_type =
717 PyObject_HEAD_INIT (NULL)
718 0, /*ob_size*/
719 "gdb.Type", /*tp_name*/
720 sizeof (type_object), /*tp_basicsize*/
721 0, /*tp_itemsize*/
722 typy_dealloc, /*tp_dealloc*/
723 0, /*tp_print*/
724 0, /*tp_getattr*/
725 0, /*tp_setattr*/
726 0, /*tp_compare*/
727 0, /*tp_repr*/
728 0, /*tp_as_number*/
729 0, /*tp_as_sequence*/
730 0, /*tp_as_mapping*/
731 0, /*tp_hash */
732 0, /*tp_call*/
733 typy_str, /*tp_str*/
734 0, /*tp_getattro*/
735 0, /*tp_setattro*/
736 0, /*tp_as_buffer*/
737 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
738 "GDB type object", /* tp_doc */
739 0, /* tp_traverse */
740 0, /* tp_clear */
741 0, /* tp_richcompare */
742 0, /* tp_weaklistoffset */
743 0, /* tp_iter */
744 0, /* tp_iternext */
745 type_object_methods, /* tp_methods */
746 0, /* tp_members */
747 type_object_getset, /* tp_getset */
748 0, /* tp_base */
749 0, /* tp_dict */
750 0, /* tp_descr_get */
751 0, /* tp_descr_set */
752 0, /* tp_dictoffset */
753 0, /* tp_init */
754 0, /* tp_alloc */
755 0, /* tp_new */
758 static PyTypeObject field_object_type =
760 PyObject_HEAD_INIT (NULL)
761 0, /*ob_size*/
762 "gdb.Field", /*tp_name*/
763 sizeof (field_object), /*tp_basicsize*/
764 0, /*tp_itemsize*/
765 field_dealloc, /*tp_dealloc*/
766 0, /*tp_print*/
767 0, /*tp_getattr*/
768 0, /*tp_setattr*/
769 0, /*tp_compare*/
770 0, /*tp_repr*/
771 0, /*tp_as_number*/
772 0, /*tp_as_sequence*/
773 0, /*tp_as_mapping*/
774 0, /*tp_hash */
775 0, /*tp_call*/
776 0, /*tp_str*/
777 0, /*tp_getattro*/
778 0, /*tp_setattro*/
779 0, /*tp_as_buffer*/
780 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
781 "GDB field object", /* tp_doc */
782 0, /* tp_traverse */
783 0, /* tp_clear */
784 0, /* tp_richcompare */
785 0, /* tp_weaklistoffset */
786 0, /* tp_iter */
787 0, /* tp_iternext */
788 0, /* tp_methods */
789 0, /* tp_members */
790 0, /* tp_getset */
791 0, /* tp_base */
792 0, /* tp_dict */
793 0, /* tp_descr_get */
794 0, /* tp_descr_set */
795 offsetof (field_object, dict), /* tp_dictoffset */
796 0, /* tp_init */
797 0, /* tp_alloc */
798 0, /* tp_new */