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