GDB (xrefs)
Loading...
Searching...
No Matches
py-type.c
Go to the documentation of this file.
1/* Python interface to types.
2
3 Copyright (C) 2008-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "value.h"
22#include "python-internal.h"
23#include "charset.h"
24#include "gdbtypes.h"
25#include "cp-support.h"
26#include "demangle.h"
27#include "objfiles.h"
28#include "language.h"
29#include "typeprint.h"
30#include "ada-lang.h"
31
33{
34 PyObject_HEAD
35 struct type *type;
36
37 /* If a Type object is associated with an objfile, it is kept on a
38 doubly-linked list, rooted in the objfile. This lets us copy the
39 underlying struct type when the objfile is deleted. */
42};
43
44extern PyTypeObject type_object_type
46
47/* A Field object. */
49{
50 PyObject_HEAD
51
52 /* Dictionary holding our attributes. */
54};
55
56extern PyTypeObject field_object_type
58
59/* A type iterator object. */
61 PyObject_HEAD
62 /* The current field index. */
63 int field;
64 /* What to return. */
66 /* Pointer back to the original source type object. */
68};
69
70extern PyTypeObject type_iterator_object_type
71 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
72
73/* This is used to initialize various gdb.TYPE_ constants. */
75{
76 /* The code. */
77 int code;
78 /* The name. */
79 const char *name;
80};
81
82/* Forward declarations. */
83static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind);
84
85static struct pyty_code pyty_codes[] =
86{
87 /* This is kept for backward compatibility. */
88 { -1, "TYPE_CODE_BITSTRING" },
89
90#define OP(X) { X, #X },
91#include "type-codes.def"
92#undef OP
93};
94
95
96
97static void
99{
100 field_object *f = (field_object *) obj;
101
102 Py_XDECREF (f->dict);
103 Py_TYPE (obj)->tp_free (obj);
104}
105
106static PyObject *
108{
109 gdbpy_ref<field_object> result (PyObject_New (field_object,
111
112 if (result != NULL)
113 {
114 result->dict = PyDict_New ();
115 if (!result->dict)
116 return NULL;
117 }
118 return (PyObject *) result.release ();
119}
120
121
122
123/* Return true if OBJ is of type gdb.Field, false otherwise. */
124
125int
127{
128 return PyObject_TypeCheck (obj, &field_object_type);
129}
130
131/* Return the code for this type. */
132static PyObject *
133typy_get_code (PyObject *self, void *closure)
134{
135 struct type *type = ((type_object *) self)->type;
136
137 return gdb_py_object_from_longest (type->code ()).release ();
138}
139
140/* Helper function for typy_fields which converts a single field to a
141 gdb.Field object. Returns NULL on error. */
142
143static gdbpy_ref<>
145{
146 gdbpy_ref<> result (field_new ());
147
148 if (result == NULL)
149 return NULL;
150
152 if (arg == NULL)
153 return NULL;
154 if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
155 return NULL;
156
157 if (!type->field (field).is_static ())
158 {
159 const char *attrstring;
160
161 if (type->code () == TYPE_CODE_ENUM)
162 {
164 attrstring = "enumval";
165 }
166 else
167 {
169 arg = gdbpy_ref<>::new_reference (Py_None);
170 else
172 attrstring = "bitpos";
173 }
174
175 if (arg == NULL)
176 return NULL;
177
178 if (PyObject_SetAttrString (result.get (), attrstring, arg.get ()) < 0)
179 return NULL;
180 }
181
182 arg.reset (NULL);
183 if (type->field (field).name ())
184 {
185 const char *field_name = type->field (field).name ();
186
187 if (field_name[0] != '\0')
188 {
189 arg.reset (PyUnicode_FromString (type->field (field).name ()));
190 if (arg == NULL)
191 return NULL;
192 }
193 }
194 if (arg == NULL)
195 arg = gdbpy_ref<>::new_reference (Py_None);
196
197 if (PyObject_SetAttrString (result.get (), "name", arg.get ()) < 0)
198 return NULL;
199
200 arg.reset (PyBool_FromLong (type->field (field).is_artificial ()));
201 if (PyObject_SetAttrString (result.get (), "artificial", arg.get ()) < 0)
202 return NULL;
203
204 if (type->code () == TYPE_CODE_STRUCT)
205 arg.reset (PyBool_FromLong (field < TYPE_N_BASECLASSES (type)));
206 else
207 arg = gdbpy_ref<>::new_reference (Py_False);
208 if (PyObject_SetAttrString (result.get (), "is_base_class", arg.get ()) < 0)
209 return NULL;
210
212 if (arg == NULL)
213 return NULL;
214 if (PyObject_SetAttrString (result.get (), "bitsize", arg.get ()) < 0)
215 return NULL;
216
217 /* A field can have a NULL type in some situations. */
218 if (type->field (field).type () == NULL)
219 arg = gdbpy_ref<>::new_reference (Py_None);
220 else
221 arg.reset (type_to_type_object (type->field (field).type ()));
222 if (arg == NULL)
223 return NULL;
224 if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0)
225 return NULL;
226
227 return result;
228}
229
230/* Helper function to return the name of a field, as a gdb.Field object.
231 If the field doesn't have a name, None is returned. */
232
233static gdbpy_ref<>
234field_name (struct type *type, int field)
235{
236 gdbpy_ref<> result;
237
238 if (type->field (field).name ())
239 result.reset (PyUnicode_FromString (type->field (field).name ()));
240 else
241 result = gdbpy_ref<>::new_reference (Py_None);
242
243 return result;
244}
245
246/* Helper function for Type standard mapping methods. Returns a
247 Python object for field i of the type. "kind" specifies what to
248 return: the name of the field, a gdb.Field object corresponding to
249 the field, or a tuple consisting of field name and gdb.Field
250 object. */
251
252static gdbpy_ref<>
253make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
254{
255 switch (kind)
256 {
257 case iter_items:
258 {
259 gdbpy_ref<> key (field_name (type, i));
260 if (key == NULL)
261 return NULL;
263 if (value == NULL)
264 return NULL;
265 gdbpy_ref<> item (PyTuple_New (2));
266 if (item == NULL)
267 return NULL;
268 PyTuple_SET_ITEM (item.get (), 0, key.release ());
269 PyTuple_SET_ITEM (item.get (), 1, value.release ());
270 return item;
271 }
272 case iter_keys:
273 return field_name (type, i);
274 case iter_values:
275 return convert_field (type, i);
276 }
277 gdb_assert_not_reached ("invalid gdbpy_iter_kind");
278}
279
280/* Return a sequence of all field names, fields, or (name, field) pairs.
281 Each field is a gdb.Field object. */
282
283static PyObject *
285{
286 PyObject *py_type = self;
287 struct type *type = ((type_object *) py_type)->type;
288 struct type *checked_type = type;
289
290 try
291 {
292 checked_type = check_typedef (checked_type);
293 }
294 catch (const gdb_exception &except)
295 {
297 }
298
299 gdbpy_ref<> type_holder;
300 if (checked_type != type)
301 {
302 type_holder.reset (type_to_type_object (checked_type));
303 if (type_holder == nullptr)
304 return nullptr;
305 py_type = type_holder.get ();
306 }
307 gdbpy_ref<> iter (typy_make_iter (py_type, kind));
308 if (iter == nullptr)
309 return nullptr;
310
311 return PySequence_List (iter.get ());
312}
313
314/* Return a sequence of all fields. Each field is a gdb.Field object. */
315
316static PyObject *
318{
319 return typy_fields_items (self, iter_values);
320}
321
322/* Return a sequence of all fields. Each field is a gdb.Field object.
323 This method is similar to typy_values, except where the supplied
324 gdb.Type is an array, in which case it returns a list of one entry
325 which is a gdb.Field object for a range (the array bounds). */
326
327static PyObject *
329{
330 struct type *type = ((type_object *) self)->type;
331
332 if (type->code () != TYPE_CODE_ARRAY)
333 return typy_fields_items (self, iter_values);
334
335 /* Array type. Handle this as a special case because the common
336 machinery wants struct or union or enum types. Build a list of
337 one entry which is the range for the array. */
339 if (r == NULL)
340 return NULL;
341
342 return Py_BuildValue ("[O]", r.get ());
343}
344
345/* Return a sequence of all field names. Each field is a gdb.Field object. */
346
347static PyObject *
349{
350 return typy_fields_items (self, iter_keys);
351}
352
353/* Return a sequence of all (name, fields) pairs. Each field is a
354 gdb.Field object. */
355
356static PyObject *
358{
359 return typy_fields_items (self, iter_items);
360}
361
362/* Return the type's name, or None. */
363
364static PyObject *
365typy_get_name (PyObject *self, void *closure)
366{
367 struct type *type = ((type_object *) self)->type;
368
369 if (type->name () == NULL)
370 Py_RETURN_NONE;
371 /* Ada type names are encoded, but it is better for users to see the
372 decoded form. */
373 if (ADA_TYPE_P (type))
374 {
375 std::string name = ada_decode (type->name (), false);
376 if (!name.empty ())
377 return PyUnicode_FromString (name.c_str ());
378 }
379 return PyUnicode_FromString (type->name ());
380}
381
382/* Return the type's tag, or None. */
383static PyObject *
384typy_get_tag (PyObject *self, void *closure)
385{
386 struct type *type = ((type_object *) self)->type;
387 const char *tagname = nullptr;
388
389 if (type->code () == TYPE_CODE_STRUCT
390 || type->code () == TYPE_CODE_UNION
391 || type->code () == TYPE_CODE_ENUM)
392 tagname = type->name ();
393
394 if (tagname == nullptr)
395 Py_RETURN_NONE;
396 return PyUnicode_FromString (tagname);
397}
398
399/* Return the type's objfile, or None. */
400static PyObject *
401typy_get_objfile (PyObject *self, void *closure)
402{
403 struct type *type = ((type_object *) self)->type;
404 struct objfile *objfile = type->objfile_owner ();
405
406 if (objfile == nullptr)
407 Py_RETURN_NONE;
408 return objfile_to_objfile_object (objfile).release ();
409}
410
411/* Return true if this is a scalar type, otherwise, returns false. */
412
413static PyObject *
414typy_is_scalar (PyObject *self, void *closure)
415{
416 struct type *type = ((type_object *) self)->type;
417
418 if (is_scalar_type (type))
419 Py_RETURN_TRUE;
420 else
421 Py_RETURN_FALSE;
422}
423
424/* Return true if this type is signed. Raises a ValueError if this type
425 is not a scalar type. */
426
427static PyObject *
428typy_is_signed (PyObject *self, void *closure)
429{
430 struct type *type = ((type_object *) self)->type;
431
432 if (!is_scalar_type (type))
433 {
434 PyErr_SetString (PyExc_ValueError,
435 _("Type must be a scalar type"));
436 return nullptr;
437 }
438
439 if (type->is_unsigned ())
440 Py_RETURN_FALSE;
441 else
442 Py_RETURN_TRUE;
443}
444
445/* Return true if this type is array-like. */
446
447static PyObject *
448typy_is_array_like (PyObject *self, void *closure)
449{
450 struct type *type = ((type_object *) self)->type;
451 bool result = false;
452
453 try
454 {
456 result = type->is_array_like ();
457 }
458 catch (const gdb_exception &except)
459 {
461 }
462
463 if (result)
464 Py_RETURN_TRUE;
465 else
466 Py_RETURN_FALSE;
467}
468
469/* Return true if this type is string-like. */
470
471static PyObject *
472typy_is_string_like (PyObject *self, void *closure)
473{
474 struct type *type = ((type_object *) self)->type;
475 bool result = false;
476
477 try
478 {
480 result = type->is_string_like ();
481 }
482 catch (const gdb_exception &except)
483 {
485 }
486
487 if (result)
488 Py_RETURN_TRUE;
489 else
490 Py_RETURN_FALSE;
491}
492
493/* Return the type, stripped of typedefs. */
494static PyObject *
496{
497 struct type *type = ((type_object *) self)->type;
498
499 try
500 {
502 }
503 catch (const gdb_exception &except)
504 {
506 }
507
508 return type_to_type_object (type);
509}
510
511/* Strip typedefs and pointers/reference from a type. Then check that
512 it is a struct, union, or enum type. If not, raise TypeError. */
513
514static struct type *
516{
517
518 for (;;)
519 {
520 try
521 {
523 }
524 catch (const gdb_exception &except)
525 {
527 }
528
530 break;
531 type = type->target_type ();
532 }
533
534 /* If this is not a struct, union, or enum type, raise TypeError
535 exception. */
536 if (type->code () != TYPE_CODE_STRUCT
537 && type->code () != TYPE_CODE_UNION
538 && type->code () != TYPE_CODE_ENUM
539 && type->code () != TYPE_CODE_METHOD
540 && type->code () != TYPE_CODE_FUNC)
541 {
542 PyErr_SetString (PyExc_TypeError,
543 "Type is not a structure, union, enum, or function type.");
544 return NULL;
545 }
546
547 return type;
548}
549
550/* Helper for typy_array and typy_vector. */
551
552static PyObject *
554{
555 long n1, n2;
556 PyObject *n2_obj = NULL;
557 struct type *array = NULL;
558 struct type *type = ((type_object *) self)->type;
559
560 if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
561 return NULL;
562
563 if (n2_obj)
564 {
565 if (!PyLong_Check (n2_obj))
566 {
567 PyErr_SetString (PyExc_RuntimeError,
568 _("Array bound must be an integer"));
569 return NULL;
570 }
571
572 if (! gdb_py_int_as_long (n2_obj, &n2))
573 return NULL;
574 }
575 else
576 {
577 n2 = n1;
578 n1 = 0;
579 }
580
581 if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */
582 {
583 PyErr_SetString (PyExc_ValueError,
584 _("Array length must not be negative"));
585 return NULL;
586 }
587
588 try
589 {
590 array = lookup_array_range_type (type, n1, n2);
591 if (is_vector)
592 make_vector_type (array);
593 }
594 catch (const gdb_exception &except)
595 {
597 }
598
599 return type_to_type_object (array);
600}
601
602/* Return an array type. */
603
604static PyObject *
606{
607 return typy_array_1 (self, args, 0);
608}
609
610/* Return a vector type. */
611
612static PyObject *
614{
615 return typy_array_1 (self, args, 1);
616}
617
618/* Return a Type object which represents a pointer to SELF. */
619static PyObject *
621{
622 struct type *type = ((type_object *) self)->type;
623
624 try
625 {
627 }
628 catch (const gdb_exception &except)
629 {
631 }
632
633 return type_to_type_object (type);
634}
635
636/* Return the range of a type represented by SELF. The return type is
637 a tuple. The first element of the tuple contains the low bound,
638 while the second element of the tuple contains the high bound. */
639static PyObject *
641{
642 struct type *type = ((type_object *) self)->type;
643 /* Initialize these to appease GCC warnings. */
644 LONGEST low = 0, high = 0;
645
646 if (type->code () != TYPE_CODE_ARRAY
647 && type->code () != TYPE_CODE_STRING
648 && type->code () != TYPE_CODE_RANGE)
649 {
650 PyErr_SetString (PyExc_RuntimeError,
651 _("This type does not have a range."));
652 return NULL;
653 }
654
655 switch (type->code ())
656 {
657 case TYPE_CODE_ARRAY:
658 case TYPE_CODE_STRING:
659 case TYPE_CODE_RANGE:
660 if (type->bounds ()->low.is_constant ())
661 low = type->bounds ()->low.const_val ();
662 else
663 low = 0;
664
665 if (type->bounds ()->high.is_constant ())
666 high = type->bounds ()->high.const_val ();
667 else
668 high = 0;
669 break;
670 }
671
672 gdbpy_ref<> low_bound = gdb_py_object_from_longest (low);
673 if (low_bound == NULL)
674 return NULL;
675
676 gdbpy_ref<> high_bound = gdb_py_object_from_longest (high);
677 if (high_bound == NULL)
678 return NULL;
679
680 gdbpy_ref<> result (PyTuple_New (2));
681 if (result == NULL)
682 return NULL;
683
684 if (PyTuple_SetItem (result.get (), 0, low_bound.release ()) != 0
685 || PyTuple_SetItem (result.get (), 1, high_bound.release ()) != 0)
686 return NULL;
687 return result.release ();
688}
689
690/* Return a Type object which represents a reference to SELF. */
691static PyObject *
693{
694 struct type *type = ((type_object *) self)->type;
695
696 try
697 {
699 }
700 catch (const gdb_exception &except)
701 {
703 }
704
705 return type_to_type_object (type);
706}
707
708/* Return a Type object which represents the target type of SELF. */
709static PyObject *
711{
712 struct type *type = ((type_object *) self)->type;
713
714 if (!type->target_type ())
715 {
716 PyErr_SetString (PyExc_RuntimeError,
717 _("Type does not have a target."));
718 return NULL;
719 }
720
722}
723
724/* Return a const-qualified type variant. */
725static PyObject *
727{
728 struct type *type = ((type_object *) self)->type;
729
730 try
731 {
732 type = make_cv_type (1, 0, type, NULL);
733 }
734 catch (const gdb_exception &except)
735 {
737 }
738
739 return type_to_type_object (type);
740}
741
742/* Return a volatile-qualified type variant. */
743static PyObject *
745{
746 struct type *type = ((type_object *) self)->type;
747
748 try
749 {
750 type = make_cv_type (0, 1, type, NULL);
751 }
752 catch (const gdb_exception &except)
753 {
755 }
756
757 return type_to_type_object (type);
758}
759
760/* Return an unqualified type variant. */
761static PyObject *
763{
764 struct type *type = ((type_object *) self)->type;
765
766 try
767 {
768 type = make_cv_type (0, 0, type, NULL);
769 }
770 catch (const gdb_exception &except)
771 {
773 }
774
775 return type_to_type_object (type);
776}
777
778/* Return the size of the type represented by SELF, in bytes. */
779static PyObject *
780typy_get_sizeof (PyObject *self, void *closure)
781{
782 struct type *type = ((type_object *) self)->type;
783
784 bool size_varies = false;
785 try
786 {
788
789 size_varies = TYPE_HAS_DYNAMIC_LENGTH (type);
790 }
791 catch (const gdb_exception &except)
792 {
793 }
794
795 /* Ignore exceptions. */
796
797 if (size_varies)
798 Py_RETURN_NONE;
799 return gdb_py_object_from_longest (type->length ()).release ();
800}
801
802/* Return the alignment of the type represented by SELF, in bytes. */
803static PyObject *
804typy_get_alignof (PyObject *self, void *closure)
805{
806 struct type *type = ((type_object *) self)->type;
807
808 ULONGEST align = 0;
809 try
810 {
811 align = type_align (type);
812 }
813 catch (const gdb_exception &except)
814 {
815 align = 0;
816 }
817
818 /* Ignore exceptions. */
819
820 return gdb_py_object_from_ulongest (align).release ();
821}
822
823/* Return whether or not the type is dynamic. */
824static PyObject *
825typy_get_dynamic (PyObject *self, void *closure)
826{
827 struct type *type = ((type_object *) self)->type;
828
829 bool result = false;
830 try
831 {
832 result = is_dynamic_type (type);
833 }
834 catch (const gdb_exception &except)
835 {
836 /* Ignore exceptions. */
837 }
838
839 if (result)
840 Py_RETURN_TRUE;
841 Py_RETURN_FALSE;
842}
843
844static struct type *
845typy_lookup_typename (const char *type_name, const struct block *block)
846{
847 struct type *type = NULL;
848
849 try
850 {
851 if (startswith (type_name, "struct "))
852 type = lookup_struct (type_name + 7, NULL);
853 else if (startswith (type_name, "union "))
854 type = lookup_union (type_name + 6, NULL);
855 else if (startswith (type_name, "enum "))
856 type = lookup_enum (type_name + 5, NULL);
857 else
859 type_name, block, 0);
860 }
861 catch (const gdb_exception &except)
862 {
864 }
865
866 return type;
867}
868
869static struct type *
870typy_lookup_type (struct demangle_component *demangled,
871 const struct block *block)
872{
873 struct type *type, *rtype = NULL;
874 enum demangle_component_type demangled_type;
875
876 /* Save the type: typy_lookup_type() may (indirectly) overwrite
877 memory pointed by demangled. */
878 demangled_type = demangled->type;
879
880 if (demangled_type == DEMANGLE_COMPONENT_POINTER
881 || demangled_type == DEMANGLE_COMPONENT_REFERENCE
882 || demangled_type == DEMANGLE_COMPONENT_RVALUE_REFERENCE
883 || demangled_type == DEMANGLE_COMPONENT_CONST
884 || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
885 {
886 type = typy_lookup_type (demangled->u.s_binary.left, block);
887 if (! type)
888 return NULL;
889
890 try
891 {
892 /* If the demangled_type matches with one of the types
893 below, run the corresponding function and save the type
894 to return later. We cannot just return here as we are in
895 an exception handler. */
896 switch (demangled_type)
897 {
898 case DEMANGLE_COMPONENT_REFERENCE:
900 break;
901 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
903 break;
904 case DEMANGLE_COMPONENT_POINTER:
905 rtype = lookup_pointer_type (type);
906 break;
907 case DEMANGLE_COMPONENT_CONST:
908 rtype = make_cv_type (1, 0, type, NULL);
909 break;
910 case DEMANGLE_COMPONENT_VOLATILE:
911 rtype = make_cv_type (0, 1, type, NULL);
912 break;
913 }
914 }
915 catch (const gdb_exception &except)
916 {
918 }
919 }
920
921 /* If we have a type from the switch statement above, just return
922 that. */
923 if (rtype)
924 return rtype;
925
926 /* We don't have a type, so lookup the type. */
927 gdb::unique_xmalloc_ptr<char> type_name = cp_comp_to_string (demangled, 10);
928 return typy_lookup_typename (type_name.get (), block);
929}
930
931/* This is a helper function for typy_template_argument that is used
932 when the type does not have template symbols attached. It works by
933 parsing the type name. This happens with compilers, like older
934 versions of GCC, that do not emit DW_TAG_template_*. */
935
936static PyObject *
938 int argno)
939{
940 int i;
941 struct demangle_component *demangled;
942 std::unique_ptr<demangle_parse_info> info;
943 std::string err;
944 struct type *argtype;
945
946 if (type->name () == NULL)
947 {
948 PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
949 return NULL;
950 }
951
952 try
953 {
954 /* Note -- this is not thread-safe. */
955 info = cp_demangled_name_to_comp (type->name (), &err);
956 }
957 catch (const gdb_exception &except)
958 {
960 }
961
962 if (! info)
963 {
964 PyErr_SetString (PyExc_RuntimeError, err.c_str ());
965 return NULL;
966 }
967 demangled = info->tree;
968
969 /* Strip off component names. */
970 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
971 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
972 demangled = demangled->u.s_binary.right;
973
974 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
975 {
976 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
977 return NULL;
978 }
979
980 /* Skip from the template to the arguments. */
981 demangled = demangled->u.s_binary.right;
982
983 for (i = 0; demangled && i < argno; ++i)
984 demangled = demangled->u.s_binary.right;
985
986 if (! demangled)
987 {
988 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
989 argno);
990 return NULL;
991 }
992
993 argtype = typy_lookup_type (demangled->u.s_binary.left, block);
994 if (! argtype)
995 return NULL;
996
997 return type_to_type_object (argtype);
998}
999
1000static PyObject *
1002{
1003 int argno;
1004 struct type *type = ((type_object *) self)->type;
1005 const struct block *block = NULL;
1006 PyObject *block_obj = NULL;
1007 struct symbol *sym;
1008
1009 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
1010 return NULL;
1011
1012 if (argno < 0)
1013 {
1014 PyErr_SetString (PyExc_RuntimeError,
1015 _("Template argument number must be non-negative"));
1016 return NULL;
1017 }
1018
1019 if (block_obj)
1020 {
1021 block = block_object_to_block (block_obj);
1022 if (! block)
1023 {
1024 PyErr_SetString (PyExc_RuntimeError,
1025 _("Second argument must be block."));
1026 return NULL;
1027 }
1028 }
1029
1030 try
1031 {
1033 if (TYPE_IS_REFERENCE (type))
1035 }
1036 catch (const gdb_exception &except)
1037 {
1038 GDB_PY_HANDLE_EXCEPTION (except);
1039 }
1040
1041 /* We might not have DW_TAG_template_*, so try to parse the type's
1042 name. This is inefficient if we do not have a template type --
1043 but that is going to wind up as an error anyhow. */
1045 return typy_legacy_template_argument (type, block, argno);
1046
1047 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
1048 {
1049 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
1050 argno);
1051 return NULL;
1052 }
1053
1054 sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
1055 if (sym->aclass () == LOC_TYPEDEF)
1056 return type_to_type_object (sym->type ());
1057 else if (sym->aclass () == LOC_OPTIMIZED_OUT)
1058 {
1059 PyErr_Format (PyExc_RuntimeError,
1060 _("Template argument is optimized out"));
1061 return NULL;
1062 }
1063
1064 PyObject *result = nullptr;
1065 try
1066 {
1067 scoped_value_mark free_values;
1068 struct value *val = value_of_variable (sym, block);
1069 result = value_to_value_object (val);
1070 }
1071 catch (const gdb_exception &except)
1072 {
1073 GDB_PY_HANDLE_EXCEPTION (except);
1074 }
1075
1076 return result;
1077}
1078
1079/* __repr__ implementation for gdb.Type. */
1080
1081static PyObject *
1083{
1084 const auto type = type_object_to_type (self);
1085 if (type == nullptr)
1086 return PyUnicode_FromFormat ("<%s (invalid)>",
1087 Py_TYPE (self)->tp_name);
1088
1089 const char *code = pyty_codes[type->code ()].name;
1090 string_file type_name;
1091 try
1092 {
1093 current_language->print_type (type, "", &type_name, -1, 0,
1095 }
1096 catch (const gdb_exception &except)
1097 {
1098 GDB_PY_HANDLE_EXCEPTION (except);
1099 }
1100 auto py_typename = PyUnicode_Decode (type_name.c_str (), type_name.size (),
1101 host_charset (), NULL);
1102
1103 return PyUnicode_FromFormat ("<%s code=%s name=%U>", Py_TYPE (self)->tp_name,
1104 code, py_typename);
1105}
1106
1107static PyObject *
1109{
1110 string_file thetype;
1111
1112 try
1113 {
1115 &thetype, -1, 0,
1117 }
1118 catch (const gdb_exception &except)
1119 {
1120 GDB_PY_HANDLE_EXCEPTION (except);
1121 }
1122
1123 return PyUnicode_Decode (thetype.c_str (), thetype.size (),
1124 host_charset (), NULL);
1125}
1126
1127/* Implement the richcompare method. */
1128
1129static PyObject *
1130typy_richcompare (PyObject *self, PyObject *other, int op)
1131{
1132 bool result = false;
1133 struct type *type1 = type_object_to_type (self);
1134 struct type *type2 = type_object_to_type (other);
1135
1136 /* We can only compare ourselves to another Type object, and only
1137 for equality or inequality. */
1138 if (type2 == NULL || (op != Py_EQ && op != Py_NE))
1139 {
1140 Py_INCREF (Py_NotImplemented);
1141 return Py_NotImplemented;
1142 }
1143
1144 if (type1 == type2)
1145 result = true;
1146 else
1147 {
1148 try
1149 {
1150 result = types_deeply_equal (type1, type2);
1151 }
1152 catch (const gdb_exception &except)
1153 {
1154 /* If there is a GDB exception, a comparison is not capable
1155 (or trusted), so exit. */
1156 GDB_PY_HANDLE_EXCEPTION (except);
1157 }
1158 }
1159
1160 if (op == (result ? Py_EQ : Py_NE))
1161 Py_RETURN_TRUE;
1162 Py_RETURN_FALSE;
1163}
1164
1165
1166
1167/* Deleter that saves types when an objfile is being destroyed. */
1169{
1171 {
1173 return;
1174
1175 /* This prevents another thread from freeing the objects we're
1176 operating on. */
1177 gdbpy_enter enter_py;
1178
1179 htab_up copied_types = create_copied_types_hash ();
1180
1181 while (obj)
1182 {
1183 type_object *next = obj->next;
1184
1185 htab_empty (copied_types.get ());
1186
1187 obj->type = copy_type_recursive (obj->type, copied_types.get ());
1188
1189 obj->next = NULL;
1190 obj->prev = NULL;
1191
1192 obj = next;
1193 }
1194 }
1195};
1196
1199
1200static void
1202{
1203 obj->type = type;
1204 obj->prev = NULL;
1205 if (type != nullptr && type->objfile_owner () != nullptr)
1206 {
1207 struct objfile *objfile = type->objfile_owner ();
1208
1210 if (obj->next)
1211 obj->next->prev = obj;
1213 }
1214 else
1215 obj->next = NULL;
1216}
1217
1218static void
1220{
1221 type_object *type = (type_object *) obj;
1222
1223 if (type->prev)
1224 type->prev->next = type->next;
1225 else if (type->type != nullptr && type->type->objfile_owner () != nullptr)
1226 {
1227 /* Must reset head of list. */
1228 struct objfile *objfile = type->type->objfile_owner ();
1229
1230 if (objfile)
1232 }
1233 if (type->next)
1234 type->next->prev = type->prev;
1235
1236 Py_TYPE (type)->tp_free (type);
1237}
1238
1239/* Return number of fields ("length" of the field dictionary). */
1240
1241static Py_ssize_t
1243{
1244 struct type *type = ((type_object *) self)->type;
1245
1247 if (type == NULL)
1248 return -1;
1249
1250 return type->num_fields ();
1251}
1252
1253/* Implements boolean evaluation of gdb.Type. Handle this like other
1254 Python objects that don't have a meaningful truth value -- all
1255 values are true. */
1256
1257static int
1259{
1260 return 1;
1261}
1262
1263/* Return optimized out value of this type. */
1264
1265static PyObject *
1267{
1268 struct type *type = ((type_object *) self)->type;
1269
1270 scoped_value_mark free_values;
1272}
1273
1274/* Return a gdb.Field object for the field named by the argument. */
1275
1276static PyObject *
1278{
1279 struct type *type = ((type_object *) self)->type;
1280 int i;
1281
1282 gdb::unique_xmalloc_ptr<char> field = python_string_to_host_string (key);
1283 if (field == NULL)
1284 return NULL;
1285
1286 /* We want just fields of this type, not of base types, so instead of
1287 using lookup_struct_elt_type, portions of that function are
1288 copied here. */
1289
1291 if (type == NULL)
1292 return NULL;
1293
1294 for (i = 0; i < type->num_fields (); i++)
1295 {
1296 const char *t_field_name = type->field (i).name ();
1297
1298 if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
1299 return convert_field (type, i).release ();
1300 }
1301 PyErr_SetObject (PyExc_KeyError, key);
1302 return NULL;
1303}
1304
1305/* Implement the "get" method on the type object. This is the
1306 same as getitem if the key is present, but returns the supplied
1307 default value or None if the key is not found. */
1308
1309static PyObject *
1311{
1312 PyObject *key, *defval = Py_None, *result;
1313
1314 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
1315 return NULL;
1316
1317 result = typy_getitem (self, key);
1318 if (result != NULL)
1319 return result;
1320
1321 /* typy_getitem returned error status. If the exception is
1322 KeyError, clear the exception status and return the defval
1323 instead. Otherwise return the exception unchanged. */
1324 if (!PyErr_ExceptionMatches (PyExc_KeyError))
1325 return NULL;
1326
1327 PyErr_Clear ();
1328 Py_INCREF (defval);
1329 return defval;
1330}
1331
1332/* Implement the "has_key" method on the type object. */
1333
1334static PyObject *
1336{
1337 struct type *type = ((type_object *) self)->type;
1338 const char *field;
1339 int i;
1340
1341 if (!PyArg_ParseTuple (args, "s", &field))
1342 return NULL;
1343
1344 /* We want just fields of this type, not of base types, so instead of
1345 using lookup_struct_elt_type, portions of that function are
1346 copied here. */
1347
1349 if (type == NULL)
1350 return NULL;
1351
1352 for (i = 0; i < type->num_fields (); i++)
1353 {
1354 const char *t_field_name = type->field (i).name ();
1355
1356 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1357 Py_RETURN_TRUE;
1358 }
1359 Py_RETURN_FALSE;
1360}
1361
1362/* Make an iterator object to iterate over keys, values, or items. */
1363
1364static PyObject *
1366{
1367 typy_iterator_object *typy_iter_obj;
1368
1369 /* Check that "self" is a structure or union type. */
1370 if (typy_get_composite (((type_object *) self)->type) == NULL)
1371 return NULL;
1372
1373 typy_iter_obj = PyObject_New (typy_iterator_object,
1375 if (typy_iter_obj == NULL)
1376 return NULL;
1377
1378 typy_iter_obj->field = 0;
1379 typy_iter_obj->kind = kind;
1380 Py_INCREF (self);
1381 typy_iter_obj->source = (type_object *) self;
1382
1383 return (PyObject *) typy_iter_obj;
1384}
1385
1386/* iteritems() method. */
1387
1388static PyObject *
1390{
1391 return typy_make_iter (self, iter_items);
1392}
1393
1394/* iterkeys() method. */
1395
1396static PyObject *
1398{
1399 return typy_make_iter (self, iter_keys);
1400}
1401
1402/* Iterating over the class, same as iterkeys except for the function
1403 signature. */
1404
1405static PyObject *
1407{
1408 return typy_make_iter (self, iter_keys);
1409}
1410
1411/* itervalues() method. */
1412
1413static PyObject *
1415{
1416 return typy_make_iter (self, iter_values);
1417}
1418
1419/* Return a reference to the type iterator. */
1420
1421static PyObject *
1423{
1424 Py_INCREF (self);
1425 return self;
1426}
1427
1428/* Return the next field in the iteration through the list of fields
1429 of the type. */
1430
1431static PyObject *
1433{
1434 typy_iterator_object *iter_obj = (typy_iterator_object *) self;
1435 struct type *type = iter_obj->source->type;
1436
1437 if (iter_obj->field < type->num_fields ())
1438 {
1439 gdbpy_ref<> result = make_fielditem (type, iter_obj->field,
1440 iter_obj->kind);
1441 if (result != NULL)
1442 iter_obj->field++;
1443 return result.release ();
1444 }
1445
1446 return NULL;
1447}
1448
1449static void
1451{
1452 typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
1453
1454 Py_DECREF (iter_obj->source);
1455 Py_TYPE (obj)->tp_free (obj);
1456}
1457
1458/* Create a new Type referring to TYPE. */
1459PyObject *
1461{
1462 type_object *type_obj;
1463
1464 try
1465 {
1466 /* Try not to let stub types leak out to Python. */
1467 if (type->is_stub ())
1469 }
1470 catch (...)
1471 {
1472 /* Just ignore failures in check_typedef. */
1473 }
1474
1475 type_obj = PyObject_New (type_object, &type_object_type);
1476 if (type_obj)
1477 set_type (type_obj, type);
1478
1479 return (PyObject *) type_obj;
1480}
1481
1482struct type *
1484{
1485 if (! PyObject_TypeCheck (obj, &type_object_type))
1486 return NULL;
1487 return ((type_object *) obj)->type;
1488}
1489
1490
1491
1492/* Implementation of gdb.lookup_type. */
1493PyObject *
1495{
1496 static const char *keywords[] = { "name", "block", NULL };
1497 const char *type_name = NULL;
1498 struct type *type = NULL;
1499 PyObject *block_obj = NULL;
1500 const struct block *block = NULL;
1501
1502 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1503 &type_name, &block_obj))
1504 return NULL;
1505
1506 if (block_obj)
1507 {
1508 block = block_object_to_block (block_obj);
1509 if (! block)
1510 {
1511 PyErr_SetString (PyExc_RuntimeError,
1512 _("'block' argument must be a Block."));
1513 return NULL;
1514 }
1515 }
1516
1517 type = typy_lookup_typename (type_name, block);
1518 if (! type)
1519 return NULL;
1520
1521 return type_to_type_object (type);
1522}
1523
1526{
1527 if (PyType_Ready (&type_object_type) < 0)
1528 return -1;
1529 if (PyType_Ready (&field_object_type) < 0)
1530 return -1;
1531 if (PyType_Ready (&type_iterator_object_type) < 0)
1532 return -1;
1533
1534 for (const auto &item : pyty_codes)
1535 {
1536 if (PyModule_AddIntConstant (gdb_module, item.name, item.code) < 0)
1537 return -1;
1538 }
1539
1541 (PyObject *) &type_object_type) < 0)
1542 return -1;
1543
1544 if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
1546 return -1;
1547
1548 return gdb_pymodule_addobject (gdb_module, "Field",
1550}
1551
1553
1554
1555
1557{
1558 { "alignof", typy_get_alignof, NULL,
1559 "The alignment of this type, in bytes.", NULL },
1560 { "code", typy_get_code, NULL,
1561 "The code for this type.", NULL },
1562 { "dynamic", typy_get_dynamic, NULL,
1563 "Whether this type is dynamic.", NULL },
1564 { "name", typy_get_name, NULL,
1565 "The name for this type, or None.", NULL },
1566 { "sizeof", typy_get_sizeof, NULL,
1567 "The size of this type, in bytes.", NULL },
1568 { "tag", typy_get_tag, NULL,
1569 "The tag name for this type, or None.", NULL },
1570 { "objfile", typy_get_objfile, NULL,
1571 "The objfile this type was defined in, or None.", NULL },
1572 { "is_scalar", typy_is_scalar, nullptr,
1573 "Is this a scalar type?", nullptr },
1574 { "is_signed", typy_is_signed, nullptr,
1575 "Is this a signed type?", nullptr },
1576 { "is_array_like", typy_is_array_like, nullptr,
1577 "Is this an array-like type?", nullptr },
1578 { "is_string_like", typy_is_string_like, nullptr,
1579 "Is this a string-like type?", nullptr },
1580 { NULL }
1581};
1582
1583static PyMethodDef type_object_methods[] =
1584{
1585 { "array", typy_array, METH_VARARGS,
1586 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1587Return a type which represents an array of objects of this type.\n\
1588The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1589If LOW_BOUND is omitted, a value of zero is used." },
1590 { "vector", typy_vector, METH_VARARGS,
1591 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1592Return a type which represents a vector of objects of this type.\n\
1593The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1594If LOW_BOUND is omitted, a value of zero is used.\n\
1595Vectors differ from arrays in that if the current language has C-style\n\
1596arrays, vectors don't decay to a pointer to the first element.\n\
1597They are first class values." },
1598 { "__contains__", typy_has_key, METH_VARARGS,
1599 "T.__contains__(k) -> True if T has a field named k, else False" },
1600 { "const", typy_const, METH_NOARGS,
1601 "const () -> Type\n\
1602Return a const variant of this type." },
1603 { "optimized_out", typy_optimized_out, METH_NOARGS,
1604 "optimized_out() -> Value\n\
1605Return optimized out value of this type." },
1606 { "fields", typy_fields, METH_NOARGS,
1607 "fields () -> list\n\
1608Return a list holding all the fields of this type.\n\
1609Each field is a gdb.Field object." },
1610 { "get", typy_get, METH_VARARGS,
1611 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1612otherwise returns default, if supplied, or None if not." },
1613 { "has_key", typy_has_key, METH_VARARGS,
1614 "T.has_key(k) -> True if T has a field named k, else False" },
1615 { "items", typy_items, METH_NOARGS,
1616 "items () -> list\n\
1617Return a list of (name, field) pairs of this type.\n\
1618Each field is a gdb.Field object." },
1619 { "iteritems", typy_iteritems, METH_NOARGS,
1620 "iteritems () -> an iterator over the (name, field)\n\
1621pairs of this type. Each field is a gdb.Field object." },
1622 { "iterkeys", typy_iterkeys, METH_NOARGS,
1623 "iterkeys () -> an iterator over the field names of this type." },
1624 { "itervalues", typy_itervalues, METH_NOARGS,
1625 "itervalues () -> an iterator over the fields of this type.\n\
1626Each field is a gdb.Field object." },
1627 { "keys", typy_field_names, METH_NOARGS,
1628 "keys () -> list\n\
1629Return a list holding all the fields names of this type." },
1630 { "pointer", typy_pointer, METH_NOARGS,
1631 "pointer () -> Type\n\
1632Return a type of pointer to this type." },
1633 { "range", typy_range, METH_NOARGS,
1634 "range () -> tuple\n\
1635Return a tuple containing the lower and upper range for this type."},
1636 { "reference", typy_reference, METH_NOARGS,
1637 "reference () -> Type\n\
1638Return a type of reference to this type." },
1639 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1640 "strip_typedefs () -> Type\n\
1641Return a type formed by stripping this type of all typedefs."},
1642 { "target", typy_target, METH_NOARGS,
1643 "target () -> Type\n\
1644Return the target type of this type." },
1645 { "template_argument", typy_template_argument, METH_VARARGS,
1646 "template_argument (arg, [block]) -> Type\n\
1647Return the type of a template argument." },
1648 { "unqualified", typy_unqualified, METH_NOARGS,
1649 "unqualified () -> Type\n\
1650Return a variant of this type without const or volatile attributes." },
1651 { "values", typy_values, METH_NOARGS,
1652 "values () -> list\n\
1653Return a list holding all the fields of this type.\n\
1654Each field is a gdb.Field object." },
1655 { "volatile", typy_volatile, METH_NOARGS,
1656 "volatile () -> Type\n\
1657Return a volatile variant of this type" },
1658 { NULL }
1659};
1660
1661static PyNumberMethods type_object_as_number = {
1662 NULL, /* nb_add */
1663 NULL, /* nb_subtract */
1664 NULL, /* nb_multiply */
1665 NULL, /* nb_remainder */
1666 NULL, /* nb_divmod */
1667 NULL, /* nb_power */
1668 NULL, /* nb_negative */
1669 NULL, /* nb_positive */
1670 NULL, /* nb_absolute */
1671 typy_nonzero, /* nb_nonzero */
1672 NULL, /* nb_invert */
1673 NULL, /* nb_lshift */
1674 NULL, /* nb_rshift */
1675 NULL, /* nb_and */
1676 NULL, /* nb_xor */
1677 NULL, /* nb_or */
1678 NULL, /* nb_int */
1679 NULL, /* reserved */
1680 NULL, /* nb_float */
1681};
1682
1683static PyMappingMethods typy_mapping = {
1686 NULL /* no "set" method */
1687};
1688
1689PyTypeObject type_object_type =
1690{
1691 PyVarObject_HEAD_INIT (NULL, 0)
1692 "gdb.Type", /*tp_name*/
1693 sizeof (type_object), /*tp_basicsize*/
1694 0, /*tp_itemsize*/
1695 typy_dealloc, /*tp_dealloc*/
1696 0, /*tp_print*/
1697 0, /*tp_getattr*/
1698 0, /*tp_setattr*/
1699 0, /*tp_compare*/
1700 typy_repr, /*tp_repr*/
1701 &type_object_as_number, /*tp_as_number*/
1702 0, /*tp_as_sequence*/
1703 &typy_mapping, /*tp_as_mapping*/
1704 0, /*tp_hash */
1705 0, /*tp_call*/
1706 typy_str, /*tp_str*/
1707 0, /*tp_getattro*/
1708 0, /*tp_setattro*/
1709 0, /*tp_as_buffer*/
1710 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1711 "GDB type object", /* tp_doc */
1712 0, /* tp_traverse */
1713 0, /* tp_clear */
1714 typy_richcompare, /* tp_richcompare */
1715 0, /* tp_weaklistoffset */
1716 typy_iter, /* tp_iter */
1717 0, /* tp_iternext */
1718 type_object_methods, /* tp_methods */
1719 0, /* tp_members */
1720 type_object_getset, /* tp_getset */
1721 0, /* tp_base */
1722 0, /* tp_dict */
1723 0, /* tp_descr_get */
1724 0, /* tp_descr_set */
1725 0, /* tp_dictoffset */
1726 0, /* tp_init */
1727 0, /* tp_alloc */
1728 0, /* tp_new */
1729};
1730
1732{
1733 { "__dict__", gdb_py_generic_dict, NULL,
1734 "The __dict__ for this field.", &field_object_type },
1735 { NULL }
1736};
1737
1738PyTypeObject field_object_type =
1739{
1740 PyVarObject_HEAD_INIT (NULL, 0)
1741 "gdb.Field", /*tp_name*/
1742 sizeof (field_object), /*tp_basicsize*/
1743 0, /*tp_itemsize*/
1744 field_dealloc, /*tp_dealloc*/
1745 0, /*tp_print*/
1746 0, /*tp_getattr*/
1747 0, /*tp_setattr*/
1748 0, /*tp_compare*/
1749 0, /*tp_repr*/
1750 0, /*tp_as_number*/
1751 0, /*tp_as_sequence*/
1752 0, /*tp_as_mapping*/
1753 0, /*tp_hash */
1754 0, /*tp_call*/
1755 0, /*tp_str*/
1756 0, /*tp_getattro*/
1757 0, /*tp_setattro*/
1758 0, /*tp_as_buffer*/
1759 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1760 "GDB field object", /* tp_doc */
1761 0, /* tp_traverse */
1762 0, /* tp_clear */
1763 0, /* tp_richcompare */
1764 0, /* tp_weaklistoffset */
1765 0, /* tp_iter */
1766 0, /* tp_iternext */
1767 0, /* tp_methods */
1768 0, /* tp_members */
1769 field_object_getset, /* tp_getset */
1770 0, /* tp_base */
1771 0, /* tp_dict */
1772 0, /* tp_descr_get */
1773 0, /* tp_descr_set */
1774 offsetof (field_object, dict), /* tp_dictoffset */
1775 0, /* tp_init */
1776 0, /* tp_alloc */
1777 0, /* tp_new */
1778};
1779
1781 PyVarObject_HEAD_INIT (NULL, 0)
1782 "gdb.TypeIterator", /*tp_name*/
1783 sizeof (typy_iterator_object), /*tp_basicsize*/
1784 0, /*tp_itemsize*/
1785 typy_iterator_dealloc, /*tp_dealloc*/
1786 0, /*tp_print*/
1787 0, /*tp_getattr*/
1788 0, /*tp_setattr*/
1789 0, /*tp_compare*/
1790 0, /*tp_repr*/
1791 0, /*tp_as_number*/
1792 0, /*tp_as_sequence*/
1793 0, /*tp_as_mapping*/
1794 0, /*tp_hash */
1795 0, /*tp_call*/
1796 0, /*tp_str*/
1797 0, /*tp_getattro*/
1798 0, /*tp_setattro*/
1799 0, /*tp_as_buffer*/
1800 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1801 "GDB type iterator object", /*tp_doc */
1802 0, /*tp_traverse */
1803 0, /*tp_clear */
1804 0, /*tp_richcompare */
1805 0, /*tp_weaklistoffset */
1806 typy_iterator_iter, /*tp_iter */
1807 typy_iterator_iternext, /*tp_iternext */
1808 0 /*tp_methods */
1809};
const char *const name
std::string ada_decode(const char *encoded, bool wrap, bool operators)
Definition ada-lang.c:1311
int code
Definition ada-lex.l:670
constexpr int n1
Definition 2.cc:29
constexpr int n2
Definition 2.cc:30
void f()
Definition 1.cc:36
const char * host_charset(void)
Definition charset.c:416
void set(unsigned key, void *datum)
Definition registry.h:204
void * get(unsigned key)
Definition registry.h:211
size_t size() const
Definition ui-file.h:223
const char * c_str() const
Definition ui-file.h:222
struct std::unique_ptr< demangle_parse_info > cp_demangled_name_to_comp(const char *demangled_name, std::string *errmsg)
gdb::unique_xmalloc_ptr< char > cp_comp_to_string(struct demangle_component *result, int estimated_len)
struct type * copy_type_recursive(struct type *type, htab_t copied_types)
Definition gdbtypes.c:5502
struct type * lookup_pointer_type(struct type *type)
Definition gdbtypes.c:430
struct type * lookup_enum(const char *name, const struct block *block)
Definition gdbtypes.c:1743
struct type * lookup_lvalue_reference_type(struct type *type)
Definition gdbtypes.c:518
struct type * lookup_typename(const struct language_defn *language, const char *name, const struct block *block, int noerr)
Definition gdbtypes.c:1651
htab_up create_copied_types_hash()
Definition gdbtypes.c:5464
int is_scalar_type(struct type *type)
Definition gdbtypes.c:3681
int is_dynamic_type(struct type *type)
Definition gdbtypes.c:2140
struct type * lookup_array_range_type(struct type *element_type, LONGEST low_bound, LONGEST high_bound)
Definition gdbtypes.c:1397
struct type * make_cv_type(int cnst, int voltl, struct type *type, struct type **typeptr)
Definition gdbtypes.c:740
struct type * lookup_rvalue_reference_type(struct type *type)
Definition gdbtypes.c:526
unsigned type_align(struct type *type)
Definition gdbtypes.c:3527
bool types_deeply_equal(struct type *type1, struct type *type2)
Definition gdbtypes.c:4342
struct type * lookup_struct(const char *name, const struct block *block)
Definition gdbtypes.c:1697
struct type * lookup_union(const char *name, const struct block *block)
Definition gdbtypes.c:1719
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
void make_vector_type(struct type *array_type)
Definition gdbtypes.c:1468
#define TYPE_IS_REFERENCE(t)
Definition gdbtypes.h:139
@ FIELD_LOC_KIND_DWARF_BLOCK
Definition gdbtypes.h:485
#define ADA_TYPE_P(type)
Definition gdbtypes.h:1848
#define TYPE_HAS_DYNAMIC_LENGTH(t)
Definition gdbtypes.h:151
#define TYPE_N_TEMPLATE_ARGUMENTS(thistype)
Definition gdbtypes.h:1993
#define TYPE_TEMPLATE_ARGUMENT(thistype, n)
Definition gdbtypes.h:1997
#define TYPE_N_BASECLASSES(thistype)
Definition gdbtypes.h:1947
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
const struct language_defn * current_language
Definition language.c:82
const struct block * block_object_to_block(PyObject *obj)
Definition py-block.c:336
gdbpy_ref objfile_to_objfile_object(struct objfile *objfile)
Definition py-objfile.c:686
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
static PyObject * typy_getitem(PyObject *self, PyObject *key)
Definition py-type.c:1277
static PyObject * typy_pointer(PyObject *self, PyObject *args)
Definition py-type.c:620
PyObject * type_to_type_object(struct type *type)
Definition py-type.c:1460
static void typy_dealloc(PyObject *obj)
Definition py-type.c:1219
static PyMappingMethods typy_mapping
Definition py-type.c:1683
static PyObject * typy_iteritems(PyObject *self, PyObject *args)
Definition py-type.c:1389
static PyObject * typy_get_code(PyObject *self, void *closure)
Definition py-type.c:133
static struct pyty_code pyty_codes[]
Definition py-type.c:85
static PyObject * typy_make_iter(PyObject *self, enum gdbpy_iter_kind kind)
Definition py-type.c:1365
static PyObject * field_new(void)
Definition py-type.c:107
static PyObject * typy_template_argument(PyObject *self, PyObject *args)
Definition py-type.c:1001
static PyObject * typy_target(PyObject *self, PyObject *args)
Definition py-type.c:710
static void typy_iterator_dealloc(PyObject *obj)
Definition py-type.c:1450
static int typy_nonzero(PyObject *self)
Definition py-type.c:1258
static struct type * typy_get_composite(struct type *type)
Definition py-type.c:515
static PyObject * typy_items(PyObject *self, PyObject *args)
Definition py-type.c:357
static PyObject * typy_optimized_out(PyObject *self, PyObject *args)
Definition py-type.c:1266
static gdb_PyGetSetDef type_object_getset[]
Definition py-type.c:1556
static PyObject * typy_get_tag(PyObject *self, void *closure)
Definition py-type.c:384
PyTypeObject field_object_type
Definition py-type.c:1738
static PyObject * typy_get_sizeof(PyObject *self, void *closure)
Definition py-type.c:780
static PyObject * typy_vector(PyObject *self, PyObject *args)
Definition py-type.c:613
static PyObject * typy_is_array_like(PyObject *self, void *closure)
Definition py-type.c:448
static PyObject * typy_iterator_iter(PyObject *self)
Definition py-type.c:1422
static PyObject * typy_reference(PyObject *self, PyObject *args)
Definition py-type.c:692
static PyObject * typy_unqualified(PyObject *self, PyObject *args)
Definition py-type.c:762
static PyObject * typy_get_name(PyObject *self, void *closure)
Definition py-type.c:365
static gdbpy_ref field_name(struct type *type, int field)
Definition py-type.c:234
static PyObject * typy_iterkeys(PyObject *self, PyObject *args)
Definition py-type.c:1397
static PyObject * typy_volatile(PyObject *self, PyObject *args)
Definition py-type.c:744
static Py_ssize_t typy_length(PyObject *self)
Definition py-type.c:1242
static PyObject * typy_array_1(PyObject *self, PyObject *args, int is_vector)
Definition py-type.c:553
static PyObject * typy_get_objfile(PyObject *self, void *closure)
Definition py-type.c:401
static PyObject * typy_get_dynamic(PyObject *self, void *closure)
Definition py-type.c:825
static gdbpy_ref make_fielditem(struct type *type, int i, enum gdbpy_iter_kind kind)
Definition py-type.c:253
static PyObject * typy_iter(PyObject *self)
Definition py-type.c:1406
static PyObject * typy_richcompare(PyObject *self, PyObject *other, int op)
Definition py-type.c:1130
static PyObject * typy_fields(PyObject *self, PyObject *args)
Definition py-type.c:328
static PyObject * typy_str(PyObject *self)
Definition py-type.c:1108
static struct type * typy_lookup_type(struct demangle_component *demangled, const struct block *block)
Definition py-type.c:870
static void set_type(type_object *obj, struct type *type)
Definition py-type.c:1201
static PyObject * typy_is_signed(PyObject *self, void *closure)
Definition py-type.c:428
static PyObject * typy_get(PyObject *self, PyObject *args)
Definition py-type.c:1310
static PyObject * typy_iterator_iternext(PyObject *self)
Definition py-type.c:1432
static PyObject * typy_legacy_template_argument(struct type *type, const struct block *block, int argno)
Definition py-type.c:937
int gdbpy_is_field(PyObject *obj)
Definition py-type.c:126
static PyObject * typy_is_scalar(PyObject *self, void *closure)
Definition py-type.c:414
static PyObject * typy_is_string_like(PyObject *self, void *closure)
Definition py-type.c:472
PyTypeObject type_object_type
Definition py-type.c:1689
static PyObject * typy_range(PyObject *self, PyObject *args)
Definition py-type.c:640
static PyObject * typy_const(PyObject *self, PyObject *args)
Definition py-type.c:726
static PyObject * typy_strip_typedefs(PyObject *self, PyObject *args)
Definition py-type.c:495
PyTypeObject type_iterator_object_type
Definition py-type.c:1780
static struct type * typy_lookup_typename(const char *type_name, const struct block *block)
Definition py-type.c:845
static PyObject * typy_fields_items(PyObject *self, enum gdbpy_iter_kind kind)
Definition py-type.c:284
static void field_dealloc(PyObject *obj)
Definition py-type.c:98
static PyObject * typy_values(PyObject *self, PyObject *args)
Definition py-type.c:317
static PyObject * typy_itervalues(PyObject *self, PyObject *args)
Definition py-type.c:1414
static PyObject * typy_repr(PyObject *self)
Definition py-type.c:1082
static PyObject * typy_get_alignof(PyObject *self, void *closure)
Definition py-type.c:804
static PyMethodDef type_object_methods[]
Definition py-type.c:1583
static const registry< objfile >::key< type_object, typy_deleter > typy_objfile_data_key
Definition py-type.c:1198
static gdb_PyGetSetDef field_object_getset[]
Definition py-type.c:1731
struct type * type_object_to_type(PyObject *obj)
Definition py-type.c:1483
static gdbpy_ref convert_field(struct type *type, int field)
Definition py-type.c:144
static PyObject * typy_array(PyObject *self, PyObject *args)
Definition py-type.c:605
PyObject * gdbpy_lookup_type(PyObject *self, PyObject *args, PyObject *kw)
Definition py-type.c:1494
static PyObject * typy_field_names(PyObject *self, PyObject *args)
Definition py-type.c:348
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_types(void)
Definition py-type.c:1525
static PyObject * typy_has_key(PyObject *self, PyObject *args)
Definition py-type.c:1335
static PyNumberMethods type_object_as_number
Definition py-type.c:1661
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
Definition py-utils.c:282
int gdb_py_int_as_long(PyObject *obj, long *result)
Definition py-utils.c:304
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition py-utils.c:334
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
Definition py-utils.c:142
PyObject * gdb_py_generic_dict(PyObject *self, void *closure)
Definition py-utils.c:317
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
Definition py-utils.c:293
PyObject * value_to_value_object(struct value *val)
Definition py-value.c:1854
PyObject * gdb_module
int gdb_python_initialized
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
gdbpy_iter_kind
@ iter_values
@ iter_items
@ iter_keys
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#define GDBPY_INITIALIZE_FILE(INIT,...)
#define GDB_PY_HANDLE_EXCEPTION(Exception)
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
enum var_types type
Definition scm-param.c:142
Definition block.h:109
LONGEST const_val() const
Definition gdbtypes.h:330
bool is_constant() const
Definition gdbtypes.h:345
PyObject_HEAD PyObject * dict
Definition py-type.c:53
LONGEST loc_bitpos() const
Definition gdbtypes.h:611
bool is_artificial() const
Definition gdbtypes.h:567
field_loc_kind loc_kind() const
Definition gdbtypes.h:606
LONGEST loc_enumval() const
Definition gdbtypes.h:623
const char * name() const
Definition gdbtypes.h:557
unsigned int bitsize() const
Definition gdbtypes.h:577
struct type * type() const
Definition gdbtypes.h:547
bool is_static() const
Definition gdbtypes.h:593
virtual void print_type(struct type *type, const char *varstring, struct ui_file *stream, int show, int level, const struct type_print_options *flags) const =0
const char * name
Definition py-type.c:79
int code
Definition py-type.c:77
struct dynamic_prop high
Definition gdbtypes.h:721
struct dynamic_prop low
Definition gdbtypes.h:717
address_class aclass() const
Definition symtab.h:1274
struct type * type() const
Definition symtab.h:1331
PyObject_HEAD struct type * type
Definition py-type.c:35
struct type_object * prev
Definition py-type.c:40
struct type_object * next
Definition py-type.c:41
struct type * target_type() const
Definition gdbtypes.h:1037
type_code code() const
Definition gdbtypes.h:956
ULONGEST length() const
Definition gdbtypes.h:983
struct field & field(int idx) const
Definition gdbtypes.h:1012
bool is_unsigned() const
Definition gdbtypes.h:1100
bool is_vector() const
Definition gdbtypes.h:1186
bool is_stub() const
Definition gdbtypes.h:1128
struct objfile * objfile_owner() const
Definition gdbtypes.h:1379
unsigned int num_fields() const
Definition gdbtypes.h:994
bool is_string_like()
Definition gdbtypes.c:5948
bool is_pointer_or_reference() const
Definition gdbtypes.h:1431
range_bounds * bounds() const
Definition gdbtypes.h:1065
const char * name() const
Definition gdbtypes.h:968
bool is_array_like()
Definition gdbtypes.c:5957
void operator()(type_object *obj)
Definition py-type.c:1170
PyObject_HEAD int field
Definition py-type.c:63
enum gdbpy_iter_kind kind
Definition py-type.c:65
type_object * source
Definition py-type.c:67
Definition value.h:130
static struct value * allocate_optimized_out(struct type *type)
Definition value.c:997
@ LOC_OPTIMIZED_OUT
Definition symtab.h:1062
@ LOC_TYPEDEF
Definition symtab.h:1018
const struct type_print_options type_print_raw_options
Definition typeprint.c:41
int strcmp_iw(const char *string1, const char *string2)
Definition utils.c:3033
struct value * value_of_variable(struct symbol *var, const struct block *b)
Definition valops.c:1386
int PyObject
Definition varobj.c:41