1 /* Python interface to types.
3 Copyright (C) 2008, 2009, 2010, 2011 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"
33 typedef struct pyty_type_object
38 /* If a Type object is associated with an objfile, it is kept on a
39 doubly-linked list, rooted in the objfile. This lets us copy the
40 underlying struct type when the objfile is deleted. */
41 struct pyty_type_object
*prev
;
42 struct pyty_type_object
*next
;
45 static PyTypeObject type_object_type
;
48 typedef struct pyty_field_object
52 /* Dictionary holding our attributes. */
56 static PyTypeObject field_object_type
;
58 /* This is used to initialize various gdb.TYPE_ constants. */
67 #define ENTRY(X) { X, #X }
69 static struct pyty_code pyty_codes
[] =
71 ENTRY (TYPE_CODE_PTR
),
72 ENTRY (TYPE_CODE_ARRAY
),
73 ENTRY (TYPE_CODE_STRUCT
),
74 ENTRY (TYPE_CODE_UNION
),
75 ENTRY (TYPE_CODE_ENUM
),
76 ENTRY (TYPE_CODE_FLAGS
),
77 ENTRY (TYPE_CODE_FUNC
),
78 ENTRY (TYPE_CODE_INT
),
79 ENTRY (TYPE_CODE_FLT
),
80 ENTRY (TYPE_CODE_VOID
),
81 ENTRY (TYPE_CODE_SET
),
82 ENTRY (TYPE_CODE_RANGE
),
83 ENTRY (TYPE_CODE_STRING
),
84 ENTRY (TYPE_CODE_BITSTRING
),
85 ENTRY (TYPE_CODE_ERROR
),
86 ENTRY (TYPE_CODE_METHOD
),
87 ENTRY (TYPE_CODE_METHODPTR
),
88 ENTRY (TYPE_CODE_MEMBERPTR
),
89 ENTRY (TYPE_CODE_REF
),
90 ENTRY (TYPE_CODE_CHAR
),
91 ENTRY (TYPE_CODE_BOOL
),
92 ENTRY (TYPE_CODE_COMPLEX
),
93 ENTRY (TYPE_CODE_TYPEDEF
),
94 ENTRY (TYPE_CODE_NAMESPACE
),
95 ENTRY (TYPE_CODE_DECFLOAT
),
96 ENTRY (TYPE_CODE_INTERNAL_FUNCTION
),
97 { TYPE_CODE_UNDEF
, NULL
}
103 field_dealloc (PyObject
*obj
)
105 field_object
*f
= (field_object
*) obj
;
107 Py_XDECREF (f
->dict
);
108 f
->ob_type
->tp_free (obj
);
114 field_object
*result
= PyObject_New (field_object
, &field_object_type
);
118 result
->dict
= PyDict_New ();
125 return (PyObject
*) result
;
130 /* Return the code for this type. */
132 typy_get_code (PyObject
*self
, void *closure
)
134 struct type
*type
= ((type_object
*) self
)->type
;
136 return PyInt_FromLong (TYPE_CODE (type
));
139 /* Helper function for typy_fields which converts a single field to a
140 dictionary. Returns NULL on error. */
142 convert_field (struct type
*type
, int field
)
144 PyObject
*result
= field_new ();
150 if (!field_is_static (&TYPE_FIELD (type
, field
)))
152 arg
= PyLong_FromLong (TYPE_FIELD_BITPOS (type
, field
));
156 if (PyObject_SetAttrString (result
, "bitpos", arg
) < 0)
160 if (TYPE_FIELD_NAME (type
, field
))
161 arg
= PyString_FromString (TYPE_FIELD_NAME (type
, field
));
169 if (PyObject_SetAttrString (result
, "name", arg
) < 0)
172 arg
= TYPE_FIELD_ARTIFICIAL (type
, field
) ? Py_True
: Py_False
;
174 if (PyObject_SetAttrString (result
, "artificial", arg
) < 0)
177 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
178 arg
= field
< TYPE_N_BASECLASSES (type
) ? Py_True
: Py_False
;
182 if (PyObject_SetAttrString (result
, "is_base_class", arg
) < 0)
185 arg
= PyLong_FromLong (TYPE_FIELD_BITSIZE (type
, field
));
188 if (PyObject_SetAttrString (result
, "bitsize", arg
) < 0)
191 /* A field can have a NULL type in some situations. */
192 if (TYPE_FIELD_TYPE (type
, field
) == NULL
)
198 arg
= type_to_type_object (TYPE_FIELD_TYPE (type
, field
));
201 if (PyObject_SetAttrString (result
, "type", arg
) < 0)
213 /* Return a sequence of all fields. Each field is a dictionary with
214 some pre-defined keys. */
216 typy_fields (PyObject
*self
, PyObject
*args
)
220 struct type
*type
= ((type_object
*) self
)->type
;
221 volatile struct gdb_exception except
;
223 TRY_CATCH (except
, RETURN_MASK_ALL
)
225 CHECK_TYPEDEF (type
);
227 GDB_PY_HANDLE_EXCEPTION (except
);
229 /* We would like to make a tuple here, make fields immutable, and
230 then memoize the result (and perhaps make Field.type() lazy).
231 However, that can lead to cycles. */
232 result
= PyList_New (0);
234 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
236 PyObject
*dict
= convert_field (type
, i
);
243 if (PyList_Append (result
, dict
))
254 /* Return the type's tag, or None. */
256 typy_get_tag (PyObject
*self
, void *closure
)
258 struct type
*type
= ((type_object
*) self
)->type
;
260 if (!TYPE_TAG_NAME (type
))
262 return PyString_FromString (TYPE_TAG_NAME (type
));
265 /* Return the type, stripped of typedefs. */
267 typy_strip_typedefs (PyObject
*self
, PyObject
*args
)
269 struct type
*type
= ((type_object
*) self
)->type
;
271 return type_to_type_object (check_typedef (type
));
274 /* Return an array type. */
277 typy_array (PyObject
*self
, PyObject
*args
)
280 PyObject
*n2_obj
= NULL
;
281 struct type
*array
= NULL
;
282 struct type
*type
= ((type_object
*) self
)->type
;
283 volatile struct gdb_exception except
;
285 if (! PyArg_ParseTuple (args
, "l|O", &n1
, &n2_obj
))
290 if (!PyInt_Check (n2_obj
))
292 PyErr_SetString (PyExc_RuntimeError
,
293 _("Array bound must be an integer"));
297 if (! gdb_py_int_as_long (n2_obj
, &n2
))
308 PyErr_SetString (PyExc_ValueError
,
309 _("Array length must not be negative"));
313 TRY_CATCH (except
, RETURN_MASK_ALL
)
315 array
= lookup_array_range_type (type
, n1
, n2
);
317 GDB_PY_HANDLE_EXCEPTION (except
);
319 return type_to_type_object (array
);
322 /* Return a Type object which represents a pointer to SELF. */
324 typy_pointer (PyObject
*self
, PyObject
*args
)
326 struct type
*type
= ((type_object
*) self
)->type
;
327 volatile struct gdb_exception except
;
329 TRY_CATCH (except
, RETURN_MASK_ALL
)
331 type
= lookup_pointer_type (type
);
333 GDB_PY_HANDLE_EXCEPTION (except
);
335 return type_to_type_object (type
);
338 /* Return the range of a type represented by SELF. The return type is
339 a tuple. The first element of the tuple contains the low bound,
340 while the second element of the tuple contains the high bound. */
342 typy_range (PyObject
*self
, PyObject
*args
)
344 struct type
*type
= ((type_object
*) self
)->type
;
346 PyObject
*low_bound
= NULL
, *high_bound
= NULL
;
347 /* Initialize these to appease GCC warnings. */
348 LONGEST low
= 0, high
= 0;
350 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
351 && TYPE_CODE (type
) != TYPE_CODE_STRING
352 && TYPE_CODE (type
) != TYPE_CODE_RANGE
)
354 PyErr_SetString (PyExc_RuntimeError
,
355 _("This type does not have a range."));
359 switch (TYPE_CODE (type
))
361 case TYPE_CODE_ARRAY
:
362 case TYPE_CODE_STRING
:
363 low
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
364 high
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type
));
366 case TYPE_CODE_RANGE
:
367 low
= TYPE_LOW_BOUND (type
);
368 high
= TYPE_HIGH_BOUND (type
);
372 low_bound
= PyLong_FromLong (low
);
376 high_bound
= PyLong_FromLong (high
);
380 result
= PyTuple_New (2);
384 if (PyTuple_SetItem (result
, 0, low_bound
) != 0)
389 if (PyTuple_SetItem (result
, 1, high_bound
) != 0)
391 Py_DECREF (high_bound
);
398 Py_XDECREF (high_bound
);
399 Py_XDECREF (low_bound
);
403 /* Return a Type object which represents a reference to SELF. */
405 typy_reference (PyObject
*self
, PyObject
*args
)
407 struct type
*type
= ((type_object
*) self
)->type
;
408 volatile struct gdb_exception except
;
410 TRY_CATCH (except
, RETURN_MASK_ALL
)
412 type
= lookup_reference_type (type
);
414 GDB_PY_HANDLE_EXCEPTION (except
);
416 return type_to_type_object (type
);
419 /* Return a Type object which represents the target type of SELF. */
421 typy_target (PyObject
*self
, PyObject
*args
)
423 struct type
*type
= ((type_object
*) self
)->type
;
425 if (!TYPE_TARGET_TYPE (type
))
427 PyErr_SetString (PyExc_RuntimeError
,
428 _("Type does not have a target."));
432 return type_to_type_object (TYPE_TARGET_TYPE (type
));
435 /* Return a const-qualified type variant. */
437 typy_const (PyObject
*self
, PyObject
*args
)
439 struct type
*type
= ((type_object
*) self
)->type
;
440 volatile struct gdb_exception except
;
442 TRY_CATCH (except
, RETURN_MASK_ALL
)
444 type
= make_cv_type (1, 0, type
, NULL
);
446 GDB_PY_HANDLE_EXCEPTION (except
);
448 return type_to_type_object (type
);
451 /* Return a volatile-qualified type variant. */
453 typy_volatile (PyObject
*self
, PyObject
*args
)
455 struct type
*type
= ((type_object
*) self
)->type
;
456 volatile struct gdb_exception except
;
458 TRY_CATCH (except
, RETURN_MASK_ALL
)
460 type
= make_cv_type (0, 1, type
, NULL
);
462 GDB_PY_HANDLE_EXCEPTION (except
);
464 return type_to_type_object (type
);
467 /* Return an unqualified type variant. */
469 typy_unqualified (PyObject
*self
, PyObject
*args
)
471 struct type
*type
= ((type_object
*) self
)->type
;
472 volatile struct gdb_exception except
;
474 TRY_CATCH (except
, RETURN_MASK_ALL
)
476 type
= make_cv_type (0, 0, type
, NULL
);
478 GDB_PY_HANDLE_EXCEPTION (except
);
480 return type_to_type_object (type
);
483 /* Return the size of the type represented by SELF, in bytes. */
485 typy_get_sizeof (PyObject
*self
, void *closure
)
487 struct type
*type
= ((type_object
*) self
)->type
;
488 volatile struct gdb_exception except
;
490 TRY_CATCH (except
, RETURN_MASK_ALL
)
492 check_typedef (type
);
494 /* Ignore exceptions. */
496 return PyLong_FromLong (TYPE_LENGTH (type
));
500 typy_lookup_typename (const char *type_name
, struct block
*block
)
502 struct type
*type
= NULL
;
503 volatile struct gdb_exception except
;
505 TRY_CATCH (except
, RETURN_MASK_ALL
)
507 if (!strncmp (type_name
, "struct ", 7))
508 type
= lookup_struct (type_name
+ 7, NULL
);
509 else if (!strncmp (type_name
, "union ", 6))
510 type
= lookup_union (type_name
+ 6, NULL
);
511 else if (!strncmp (type_name
, "enum ", 5))
512 type
= lookup_enum (type_name
+ 5, NULL
);
514 type
= lookup_typename (python_language
, python_gdbarch
,
515 type_name
, block
, 0);
517 if (except
.reason
< 0)
519 PyErr_Format (except
.reason
== RETURN_QUIT
520 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
521 "%s", except
.message
);
529 typy_lookup_type (struct demangle_component
*demangled
,
534 enum demangle_component_type demangled_type
;
536 /* Save the type: typy_lookup_type() may (indirectly) overwrite
537 memory pointed by demangled. */
538 demangled_type
= demangled
->type
;
540 if (demangled_type
== DEMANGLE_COMPONENT_POINTER
541 || demangled_type
== DEMANGLE_COMPONENT_REFERENCE
542 || demangled_type
== DEMANGLE_COMPONENT_CONST
543 || demangled_type
== DEMANGLE_COMPONENT_VOLATILE
)
545 type
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
549 switch (demangled_type
)
551 case DEMANGLE_COMPONENT_REFERENCE
:
552 return lookup_reference_type (type
);
553 case DEMANGLE_COMPONENT_POINTER
:
554 return lookup_pointer_type (type
);
555 case DEMANGLE_COMPONENT_CONST
:
556 return make_cv_type (1, 0, type
, NULL
);
557 case DEMANGLE_COMPONENT_VOLATILE
:
558 return make_cv_type (0, 1, type
, NULL
);
562 type_name
= cp_comp_to_string (demangled
, 10);
563 type
= typy_lookup_typename (type_name
, block
);
569 /* This is a helper function for typy_template_argument that is used
570 when the type does not have template symbols attached. It works by
571 parsing the type name. This happens with compilers, like older
572 versions of GCC, that do not emit DW_TAG_template_*. */
575 typy_legacy_template_argument (struct type
*type
, struct block
*block
,
579 struct demangle_component
*demangled
;
580 struct demangle_parse_info
*info
;
582 struct type
*argtype
;
583 struct cleanup
*cleanup
;
585 if (TYPE_NAME (type
) == NULL
)
587 PyErr_SetString (PyExc_RuntimeError
, _("Null type name."));
591 /* Note -- this is not thread-safe. */
592 info
= cp_demangled_name_to_comp (TYPE_NAME (type
), &err
);
595 PyErr_SetString (PyExc_RuntimeError
, err
);
598 demangled
= info
->tree
;
599 cleanup
= make_cleanup_cp_demangled_name_parse_free (info
);
601 /* Strip off component names. */
602 while (demangled
->type
== DEMANGLE_COMPONENT_QUAL_NAME
603 || demangled
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
604 demangled
= demangled
->u
.s_binary
.right
;
606 if (demangled
->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
608 do_cleanups (cleanup
);
609 PyErr_SetString (PyExc_RuntimeError
, _("Type is not a template."));
613 /* Skip from the template to the arguments. */
614 demangled
= demangled
->u
.s_binary
.right
;
616 for (i
= 0; demangled
&& i
< argno
; ++i
)
617 demangled
= demangled
->u
.s_binary
.right
;
621 do_cleanups (cleanup
);
622 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
627 argtype
= typy_lookup_type (demangled
->u
.s_binary
.left
, block
);
628 do_cleanups (cleanup
);
632 return type_to_type_object (argtype
);
636 typy_template_argument (PyObject
*self
, PyObject
*args
)
639 struct type
*type
= ((type_object
*) self
)->type
;
640 struct block
*block
= NULL
;
641 PyObject
*block_obj
= NULL
;
643 struct value
*val
= NULL
;
644 volatile struct gdb_exception except
;
646 if (! PyArg_ParseTuple (args
, "i|O", &argno
, &block_obj
))
651 block
= block_object_to_block (block_obj
);
654 PyErr_SetString (PyExc_RuntimeError
,
655 _("Second argument must be block."));
660 TRY_CATCH (except
, RETURN_MASK_ALL
)
662 type
= check_typedef (type
);
663 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
664 type
= check_typedef (TYPE_TARGET_TYPE (type
));
666 GDB_PY_HANDLE_EXCEPTION (except
);
668 /* We might not have DW_TAG_template_*, so try to parse the type's
669 name. This is inefficient if we do not have a template type --
670 but that is going to wind up as an error anyhow. */
671 if (! TYPE_N_TEMPLATE_ARGUMENTS (type
))
672 return typy_legacy_template_argument (type
, block
, argno
);
674 if (argno
>= TYPE_N_TEMPLATE_ARGUMENTS (type
))
676 PyErr_Format (PyExc_RuntimeError
, _("No argument %d in template."),
681 sym
= TYPE_TEMPLATE_ARGUMENT (type
, argno
);
682 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
683 return type_to_type_object (SYMBOL_TYPE (sym
));
684 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
686 PyErr_Format (PyExc_RuntimeError
,
687 _("Template argument is optimized out"));
691 TRY_CATCH (except
, RETURN_MASK_ALL
)
693 val
= value_of_variable (sym
, block
);
695 GDB_PY_HANDLE_EXCEPTION (except
);
697 return value_to_value_object (val
);
701 typy_str (PyObject
*self
)
703 volatile struct gdb_exception except
;
704 char *thetype
= NULL
;
708 TRY_CATCH (except
, RETURN_MASK_ALL
)
710 struct cleanup
*old_chain
;
713 stb
= mem_fileopen ();
714 old_chain
= make_cleanup_ui_file_delete (stb
);
716 type_print (type_object_to_type (self
), "", stb
, -1);
718 thetype
= ui_file_xstrdup (stb
, &length
);
719 do_cleanups (old_chain
);
721 if (except
.reason
< 0)
724 GDB_PY_HANDLE_EXCEPTION (except
);
727 result
= PyUnicode_Decode (thetype
, length
, host_charset (), NULL
);
733 /* An entry in the type-equality bcache. */
735 typedef struct type_equality_entry
737 struct type
*type1
, *type2
;
738 } type_equality_entry_d
;
740 DEF_VEC_O (type_equality_entry_d
);
742 /* A helper function to compare two strings. Returns 1 if they are
743 the same, 0 otherwise. Handles NULLs properly. */
746 compare_strings (const char *s
, const char *t
)
748 if (s
== NULL
&& t
!= NULL
)
750 else if (s
!= NULL
&& t
== NULL
)
752 else if (s
== NULL
&& t
== NULL
)
754 return strcmp (s
, t
) == 0;
757 /* A helper function for typy_richcompare that checks two types for
758 "deep" equality. Returns Py_EQ if the types are considered the
759 same, Py_NE otherwise. */
762 check_types_equal (struct type
*type1
, struct type
*type2
,
763 VEC (type_equality_entry_d
) **worklist
)
765 CHECK_TYPEDEF (type1
);
766 CHECK_TYPEDEF (type2
);
771 if (TYPE_CODE (type1
) != TYPE_CODE (type2
)
772 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
773 || TYPE_UNSIGNED (type1
) != TYPE_UNSIGNED (type2
)
774 || TYPE_NOSIGN (type1
) != TYPE_NOSIGN (type2
)
775 || TYPE_VARARGS (type1
) != TYPE_VARARGS (type2
)
776 || TYPE_VECTOR (type1
) != TYPE_VECTOR (type2
)
777 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
778 || TYPE_INSTANCE_FLAGS (type1
) != TYPE_INSTANCE_FLAGS (type2
)
779 || TYPE_NFIELDS (type1
) != TYPE_NFIELDS (type2
))
782 if (!compare_strings (TYPE_TAG_NAME (type1
), TYPE_TAG_NAME (type2
)))
784 if (!compare_strings (TYPE_NAME (type1
), TYPE_NAME (type2
)))
787 if (TYPE_CODE (type1
) == TYPE_CODE_RANGE
)
789 if (memcmp (TYPE_RANGE_DATA (type1
), TYPE_RANGE_DATA (type2
),
790 sizeof (*TYPE_RANGE_DATA (type1
))) != 0)
797 for (i
= 0; i
< TYPE_NFIELDS (type1
); ++i
)
799 const struct field
*field1
= &TYPE_FIELD (type1
, i
);
800 const struct field
*field2
= &TYPE_FIELD (type2
, i
);
801 struct type_equality_entry entry
;
803 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
804 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
805 || FIELD_LOC_KIND (*field1
) != FIELD_LOC_KIND (*field2
))
807 if (!compare_strings (FIELD_NAME (*field1
), FIELD_NAME (*field2
)))
809 switch (FIELD_LOC_KIND (*field1
))
811 case FIELD_LOC_KIND_BITPOS
:
812 if (FIELD_BITPOS (*field1
) != FIELD_BITPOS (*field2
))
815 case FIELD_LOC_KIND_PHYSADDR
:
816 if (FIELD_STATIC_PHYSADDR (*field1
)
817 != FIELD_STATIC_PHYSADDR (*field2
))
820 case FIELD_LOC_KIND_PHYSNAME
:
821 if (!compare_strings (FIELD_STATIC_PHYSNAME (*field1
),
822 FIELD_STATIC_PHYSNAME (*field2
)))
827 entry
.type1
= FIELD_TYPE (*field1
);
828 entry
.type2
= FIELD_TYPE (*field2
);
829 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
833 if (TYPE_TARGET_TYPE (type1
) != NULL
)
835 struct type_equality_entry entry
;
838 if (TYPE_TARGET_TYPE (type2
) == NULL
)
841 entry
.type1
= TYPE_TARGET_TYPE (type1
);
842 entry
.type2
= TYPE_TARGET_TYPE (type2
);
843 VEC_safe_push (type_equality_entry_d
, *worklist
, &entry
);
845 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
851 /* Check types on a worklist for equality. Returns Py_NE if any pair
852 is not equal, Py_EQ if they are all considered equal. */
855 check_types_worklist (VEC (type_equality_entry_d
) **worklist
,
856 struct bcache
*cache
)
858 while (!VEC_empty (type_equality_entry_d
, *worklist
))
860 struct type_equality_entry entry
;
863 entry
= *VEC_last (type_equality_entry_d
, *worklist
);
864 VEC_pop (type_equality_entry_d
, *worklist
);
866 /* If the type pair has already been visited, we know it is
868 bcache_full (&entry
, sizeof (entry
), cache
, &added
);
872 if (check_types_equal (entry
.type1
, entry
.type2
, worklist
) == Py_NE
)
879 /* Implement the richcompare method. */
882 typy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
885 struct type
*type1
= type_object_to_type (self
);
886 struct type
*type2
= type_object_to_type (other
);
887 volatile struct gdb_exception except
;
889 /* We can only compare ourselves to another Type object, and only
890 for equality or inequality. */
891 if (type2
== NULL
|| (op
!= Py_EQ
&& op
!= Py_NE
))
893 Py_INCREF (Py_NotImplemented
);
894 return Py_NotImplemented
;
901 struct bcache
*cache
;
902 VEC (type_equality_entry_d
) *worklist
= NULL
;
903 struct type_equality_entry entry
;
905 cache
= bcache_xmalloc (NULL
, NULL
);
909 VEC_safe_push (type_equality_entry_d
, worklist
, &entry
);
911 TRY_CATCH (except
, RETURN_MASK_ALL
)
913 result
= check_types_worklist (&worklist
, cache
);
915 if (except
.reason
< 0)
918 bcache_xfree (cache
);
919 VEC_free (type_equality_entry_d
, worklist
);
929 static const struct objfile_data
*typy_objfile_data_key
;
932 save_objfile_types (struct objfile
*objfile
, void *datum
)
934 type_object
*obj
= datum
;
936 struct cleanup
*cleanup
;
938 /* This prevents another thread from freeing the objects we're
940 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
942 copied_types
= create_copied_types_hash (objfile
);
946 type_object
*next
= obj
->next
;
948 htab_empty (copied_types
);
950 obj
->type
= copy_type_recursive (objfile
, obj
->type
, copied_types
);
958 htab_delete (copied_types
);
960 do_cleanups (cleanup
);
964 set_type (type_object
*obj
, struct type
*type
)
968 if (type
&& TYPE_OBJFILE (type
))
970 struct objfile
*objfile
= TYPE_OBJFILE (type
);
972 obj
->next
= objfile_data (objfile
, typy_objfile_data_key
);
974 obj
->next
->prev
= obj
;
975 set_objfile_data (objfile
, typy_objfile_data_key
, obj
);
982 typy_dealloc (PyObject
*obj
)
984 type_object
*type
= (type_object
*) obj
;
987 type
->prev
->next
= type
->next
;
988 else if (type
->type
&& TYPE_OBJFILE (type
->type
))
990 /* Must reset head of list. */
991 struct objfile
*objfile
= TYPE_OBJFILE (type
->type
);
994 set_objfile_data (objfile
, typy_objfile_data_key
, type
->next
);
997 type
->next
->prev
= type
->prev
;
999 type
->ob_type
->tp_free (type
);
1002 /* Create a new Type referring to TYPE. */
1004 type_to_type_object (struct type
*type
)
1006 type_object
*type_obj
;
1008 type_obj
= PyObject_New (type_object
, &type_object_type
);
1010 set_type (type_obj
, type
);
1012 return (PyObject
*) type_obj
;
1016 type_object_to_type (PyObject
*obj
)
1018 if (! PyObject_TypeCheck (obj
, &type_object_type
))
1020 return ((type_object
*) obj
)->type
;
1025 /* Implementation of gdb.lookup_type. */
1027 gdbpy_lookup_type (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1029 static char *keywords
[] = { "name", "block", NULL
};
1030 const char *type_name
= NULL
;
1031 struct type
*type
= NULL
;
1032 PyObject
*block_obj
= NULL
;
1033 struct block
*block
= NULL
;
1035 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O", keywords
,
1036 &type_name
, &block_obj
))
1041 block
= block_object_to_block (block_obj
);
1044 PyErr_SetString (PyExc_RuntimeError
,
1045 _("'block' argument must be a Block."));
1050 type
= typy_lookup_typename (type_name
, block
);
1054 return (PyObject
*) type_to_type_object (type
);
1058 gdbpy_initialize_types (void)
1062 typy_objfile_data_key
1063 = register_objfile_data_with_cleanup (save_objfile_types
, NULL
);
1065 if (PyType_Ready (&type_object_type
) < 0)
1067 if (PyType_Ready (&field_object_type
) < 0)
1070 for (i
= 0; pyty_codes
[i
].name
; ++i
)
1072 if (PyModule_AddIntConstant (gdb_module
,
1073 /* Cast needed for Python 2.4. */
1074 (char *) pyty_codes
[i
].name
,
1075 pyty_codes
[i
].code
) < 0)
1079 Py_INCREF (&type_object_type
);
1080 PyModule_AddObject (gdb_module
, "Type", (PyObject
*) &type_object_type
);
1082 Py_INCREF (&field_object_type
);
1083 PyModule_AddObject (gdb_module
, "Field", (PyObject
*) &field_object_type
);
1088 static PyGetSetDef type_object_getset
[] =
1090 { "code", typy_get_code
, NULL
,
1091 "The code for this type.", NULL
},
1092 { "sizeof", typy_get_sizeof
, NULL
,
1093 "The size of this type, in bytes.", NULL
},
1094 { "tag", typy_get_tag
, NULL
,
1095 "The tag name for this type, or None.", NULL
},
1099 static PyMethodDef type_object_methods
[] =
1101 { "array", typy_array
, METH_VARARGS
,
1102 "array (N) -> Type\n\
1103 Return a type which represents an array of N objects of this type." },
1104 { "const", typy_const
, METH_NOARGS
,
1105 "const () -> Type\n\
1106 Return a const variant of this type." },
1107 { "fields", typy_fields
, METH_NOARGS
,
1108 "field () -> list\n\
1109 Return a sequence holding all the fields of this type.\n\
1110 Each field is a dictionary." },
1111 { "pointer", typy_pointer
, METH_NOARGS
,
1112 "pointer () -> Type\n\
1113 Return a type of pointer to this type." },
1114 { "range", typy_range
, METH_NOARGS
,
1115 "range () -> tuple\n\
1116 Return a tuple containing the lower and upper range for this type."},
1117 { "reference", typy_reference
, METH_NOARGS
,
1118 "reference () -> Type\n\
1119 Return a type of reference to this type." },
1120 { "strip_typedefs", typy_strip_typedefs
, METH_NOARGS
,
1121 "strip_typedefs () -> Type\n\
1122 Return a type formed by stripping this type of all typedefs."},
1123 { "target", typy_target
, METH_NOARGS
,
1124 "target () -> Type\n\
1125 Return the target type of this type." },
1126 { "template_argument", typy_template_argument
, METH_VARARGS
,
1127 "template_argument (arg, [block]) -> Type\n\
1128 Return the type of a template argument." },
1129 { "unqualified", typy_unqualified
, METH_NOARGS
,
1130 "unqualified () -> Type\n\
1131 Return a variant of this type without const or volatile attributes." },
1132 { "volatile", typy_volatile
, METH_NOARGS
,
1133 "volatile () -> Type\n\
1134 Return a volatile variant of this type" },
1138 static PyTypeObject type_object_type
=
1140 PyObject_HEAD_INIT (NULL
)
1142 "gdb.Type", /*tp_name*/
1143 sizeof (type_object
), /*tp_basicsize*/
1145 typy_dealloc
, /*tp_dealloc*/
1152 0, /*tp_as_sequence*/
1153 0, /*tp_as_mapping*/
1156 typy_str
, /*tp_str*/
1160 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1161 "GDB type object", /* tp_doc */
1162 0, /* tp_traverse */
1164 typy_richcompare
, /* tp_richcompare */
1165 0, /* tp_weaklistoffset */
1167 0, /* tp_iternext */
1168 type_object_methods
, /* tp_methods */
1170 type_object_getset
, /* tp_getset */
1173 0, /* tp_descr_get */
1174 0, /* tp_descr_set */
1175 0, /* tp_dictoffset */
1181 static PyTypeObject field_object_type
=
1183 PyObject_HEAD_INIT (NULL
)
1185 "gdb.Field", /*tp_name*/
1186 sizeof (field_object
), /*tp_basicsize*/
1188 field_dealloc
, /*tp_dealloc*/
1195 0, /*tp_as_sequence*/
1196 0, /*tp_as_mapping*/
1203 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
1204 "GDB field object", /* tp_doc */
1205 0, /* tp_traverse */
1207 0, /* tp_richcompare */
1208 0, /* tp_weaklistoffset */
1210 0, /* tp_iternext */
1216 0, /* tp_descr_get */
1217 0, /* tp_descr_set */
1218 offsetof (field_object
, dict
), /* tp_dictoffset */