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