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