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