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"
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 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
175 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
178 /* A field can have a NULL type in some situations. */
179 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
185 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
188 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
200 /* Return a sequence of all fields. Each field is a dictionary with
201 some pre-defined keys. */
203 typy_fields (PyObject
*self
, PyObject
*args
)
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
);
222 if (PyList_Append (result
, dict
))
233 /* Return the type's tag, or None. */
235 typy_get_tag (PyObject
*self
, void *closure
)
237 struct type
*type
= ((type_object
*) self
)->type
;
238 if (!TYPE_TAG_NAME (type
))
240 return PyString_FromString (TYPE_TAG_NAME (type
));
243 /* Return the type, stripped of typedefs. */
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. */
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. */
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. */
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");
296 return type_to_type_object (TYPE_TARGET_TYPE (type
));
299 /* Return a const-qualified type variant. */
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. */
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. */
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. */
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
));
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
);
377 type
= lookup_typename (python_language
, python_gdbarch
,
380 if (except
.reason
< 0)
382 PyErr_Format (except
.reason
== RETURN_QUIT
383 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
384 "%s", except
.message
);
392 typy_lookup_type (struct demangle_component
*demangled
)
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
);
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
);
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
;
438 struct type
*argtype
;
440 if (! PyArg_ParseTuple (args
, "i", &argno
))
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");
453 /* Note -- this is not thread-safe. */
454 demangled
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
457 PyErr_SetString (PyExc_RuntimeError
, err
);
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");
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
;
480 PyErr_Format (PyExc_RuntimeError
, "no argument %d in template",
485 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
);
489 return type_to_type_object (argtype
);
493 typy_str (PyObject
*self
)
495 volatile struct gdb_exception except
;
496 char *thetype
= NULL
;
500 TRY_CATCH (except
, RETURN_MASK_ALL
)
502 struct cleanup
*old_chain
;
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)
516 GDB_PY_HANDLE_EXCEPTION (except
);
519 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
527 static const struct objfile_data
*typy_objfile_data_key
;
530 save_objfile_types (struct objfile
*objfile
, void *datum
)
532 type_object
*obj
= datum
;
534 struct cleanup
*cleanup
;
536 /* This prevents another thread from freeing the objects we're
538 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
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 (save_objfile_types
, NULL
);
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 */