]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-type.c
Add dynamic_prop::is_constant
[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
c86acd3f 200 arg.reset (PyBool_FromLong (TYPE_FIELD_ARTIFICIAL (type, field)));
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
062534d4 211 arg = gdb_py_object_from_longest (TYPE_FIELD_BITSIZE (type, field));
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
1031static PyObject *
1032typy_str (PyObject *self)
1033{
d7e74731 1034 string_file thetype;
2c74e833 1035
a70b8144 1036 try
2c74e833 1037 {
13eb081a
TT
1038 current_language->print_type (type_object_to_type (self), "",
1039 &thetype, -1, 0,
1040 &type_print_raw_options);
2c74e833 1041 }
230d2906 1042 catch (const gdb_exception &except)
2c74e833 1043 {
2c74e833
TT
1044 GDB_PY_HANDLE_EXCEPTION (except);
1045 }
1046
d7e74731
PA
1047 return PyUnicode_Decode (thetype.c_str (), thetype.size (),
1048 host_charset (), NULL);
2c74e833
TT
1049}
1050
d839c8a4
TT
1051/* Implement the richcompare method. */
1052
1053static PyObject *
1054typy_richcompare (PyObject *self, PyObject *other, int op)
1055{
894882e3 1056 bool result = false;
d839c8a4
TT
1057 struct type *type1 = type_object_to_type (self);
1058 struct type *type2 = type_object_to_type (other);
d839c8a4
TT
1059
1060 /* We can only compare ourselves to another Type object, and only
1061 for equality or inequality. */
1062 if (type2 == NULL || (op != Py_EQ && op != Py_NE))
1063 {
1064 Py_INCREF (Py_NotImplemented);
1065 return Py_NotImplemented;
1066 }
1067
1068 if (type1 == type2)
894882e3 1069 result = true;
d839c8a4
TT
1070 else
1071 {
a70b8144 1072 try
d839c8a4 1073 {
ca092b61 1074 result = types_deeply_equal (type1, type2);
d839c8a4 1075 }
230d2906 1076 catch (const gdb_exception &except)
492d29ea
PA
1077 {
1078 /* If there is a GDB exception, a comparison is not capable
1079 (or trusted), so exit. */
1080 GDB_PY_HANDLE_EXCEPTION (except);
1081 }
d839c8a4
TT
1082 }
1083
ca092b61 1084 if (op == (result ? Py_EQ : Py_NE))
d839c8a4
TT
1085 Py_RETURN_TRUE;
1086 Py_RETURN_FALSE;
1087}
1088
2c74e833
TT
1089\f
1090
08b8a139
TT
1091/* Deleter that saves types when an objfile is being destroyed. */
1092struct typy_deleter
2c74e833 1093{
08b8a139
TT
1094 void operator() (type_object *obj)
1095 {
1096 if (!gdb_python_initialized)
1097 return;
2c74e833 1098
08b8a139
TT
1099 /* This prevents another thread from freeing the objects we're
1100 operating on. */
1101 gdbpy_enter enter_py;
0646da15 1102
08b8a139 1103 htab_up copied_types = create_copied_types_hash ();
2c74e833 1104
08b8a139
TT
1105 while (obj)
1106 {
1107 type_object *next = obj->next;
2c74e833 1108
08b8a139 1109 htab_empty (copied_types.get ());
2c74e833 1110
08b8a139 1111 obj->type = copy_type_recursive (obj->type, copied_types.get ());
2c74e833 1112
08b8a139
TT
1113 obj->next = NULL;
1114 obj->prev = NULL;
2c74e833 1115
08b8a139
TT
1116 obj = next;
1117 }
1118 }
1119};
2c74e833 1120
08b8a139
TT
1121static const registry<objfile>::key<type_object, typy_deleter>
1122 typy_objfile_data_key;
2c74e833
TT
1123
1124static void
1125set_type (type_object *obj, struct type *type)
1126{
1127 obj->type = type;
1128 obj->prev = NULL;
6ac37371 1129 if (type != nullptr && type->objfile_owner () != nullptr)
2c74e833 1130 {
6ac37371 1131 struct objfile *objfile = type->objfile_owner ();
2c74e833 1132
08b8a139 1133 obj->next = typy_objfile_data_key.get (objfile);
2c74e833
TT
1134 if (obj->next)
1135 obj->next->prev = obj;
08b8a139 1136 typy_objfile_data_key.set (objfile, obj);
2c74e833
TT
1137 }
1138 else
1139 obj->next = NULL;
1140}
1141
1142static void
1143typy_dealloc (PyObject *obj)
1144{
1145 type_object *type = (type_object *) obj;
1146
1147 if (type->prev)
1148 type->prev->next = type->next;
6ac37371 1149 else if (type->type != nullptr && type->type->objfile_owner () != nullptr)
2c74e833
TT
1150 {
1151 /* Must reset head of list. */
6ac37371 1152 struct objfile *objfile = type->type->objfile_owner ();
d59b6f6c 1153
2c74e833 1154 if (objfile)
08b8a139 1155 typy_objfile_data_key.set (objfile, type->next);
2c74e833
TT
1156 }
1157 if (type->next)
1158 type->next->prev = type->prev;
1159
9a27f2c6 1160 Py_TYPE (type)->tp_free (type);
2c74e833
TT
1161}
1162
a73bb892
PK
1163/* Return number of fields ("length" of the field dictionary). */
1164
1165static Py_ssize_t
1166typy_length (PyObject *self)
1167{
1168 struct type *type = ((type_object *) self)->type;
1169
9cc10fd1
PK
1170 type = typy_get_composite (type);
1171 if (type == NULL)
1172 return -1;
1173
1f704f76 1174 return type->num_fields ();
a73bb892
PK
1175}
1176
9cc10fd1 1177/* Implements boolean evaluation of gdb.Type. Handle this like other
256458bc 1178 Python objects that don't have a meaningful truth value -- all
9cc10fd1
PK
1179 values are true. */
1180
1181static int
1182typy_nonzero (PyObject *self)
1183{
1184 return 1;
1185}
1186
59fb7612
SS
1187/* Return optimized out value of this type. */
1188
1189static PyObject *
1190typy_optimized_out (PyObject *self, PyObject *args)
1191{
1192 struct type *type = ((type_object *) self)->type;
1193
f3d3bbbc 1194 scoped_value_mark free_values;
b27556e3 1195 return value_to_value_object (value::allocate_optimized_out (type));
59fb7612
SS
1196}
1197
a73bb892
PK
1198/* Return a gdb.Field object for the field named by the argument. */
1199
1200static PyObject *
1201typy_getitem (PyObject *self, PyObject *key)
1202{
1203 struct type *type = ((type_object *) self)->type;
a73bb892 1204 int i;
76dce0be 1205
9b972014 1206 gdb::unique_xmalloc_ptr<char> field = python_string_to_host_string (key);
a73bb892
PK
1207 if (field == NULL)
1208 return NULL;
1209
256458bc 1210 /* We want just fields of this type, not of base types, so instead of
a73bb892
PK
1211 using lookup_struct_elt_type, portions of that function are
1212 copied here. */
1213
9cc10fd1
PK
1214 type = typy_get_composite (type);
1215 if (type == NULL)
1216 return NULL;
256458bc 1217
1f704f76 1218 for (i = 0; i < type->num_fields (); i++)
a73bb892 1219 {
33d16dd9 1220 const char *t_field_name = type->field (i).name ();
a73bb892 1221
9b972014 1222 if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
1b20edf0 1223 return convert_field (type, i).release ();
a73bb892
PK
1224 }
1225 PyErr_SetObject (PyExc_KeyError, key);
1226 return NULL;
1227}
1228
256458bc 1229/* Implement the "get" method on the type object. This is the
a73bb892
PK
1230 same as getitem if the key is present, but returns the supplied
1231 default value or None if the key is not found. */
1232
1233static PyObject *
1234typy_get (PyObject *self, PyObject *args)
1235{
1236 PyObject *key, *defval = Py_None, *result;
256458bc 1237
a73bb892
PK
1238 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
1239 return NULL;
256458bc 1240
a73bb892
PK
1241 result = typy_getitem (self, key);
1242 if (result != NULL)
1243 return result;
256458bc 1244
a73bb892
PK
1245 /* typy_getitem returned error status. If the exception is
1246 KeyError, clear the exception status and return the defval
1247 instead. Otherwise return the exception unchanged. */
1248 if (!PyErr_ExceptionMatches (PyExc_KeyError))
1249 return NULL;
256458bc 1250
a73bb892
PK
1251 PyErr_Clear ();
1252 Py_INCREF (defval);
1253 return defval;
1254}
1255
1256/* Implement the "has_key" method on the type object. */
1257
1258static PyObject *
1259typy_has_key (PyObject *self, PyObject *args)
1260{
1261 struct type *type = ((type_object *) self)->type;
2ff6b080 1262 const char *field;
a73bb892 1263 int i;
76dce0be 1264
a73bb892
PK
1265 if (!PyArg_ParseTuple (args, "s", &field))
1266 return NULL;
1267
256458bc 1268 /* We want just fields of this type, not of base types, so instead of
a73bb892
PK
1269 using lookup_struct_elt_type, portions of that function are
1270 copied here. */
1271
9cc10fd1
PK
1272 type = typy_get_composite (type);
1273 if (type == NULL)
1274 return NULL;
a73bb892 1275
1f704f76 1276 for (i = 0; i < type->num_fields (); i++)
a73bb892 1277 {
33d16dd9 1278 const char *t_field_name = type->field (i).name ();
a73bb892
PK
1279
1280 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1281 Py_RETURN_TRUE;
1282 }
1283 Py_RETURN_FALSE;
1284}
1285
1286/* Make an iterator object to iterate over keys, values, or items. */
1287
1288static PyObject *
1289typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind)
1290{
1291 typy_iterator_object *typy_iter_obj;
1292
9cc10fd1
PK
1293 /* Check that "self" is a structure or union type. */
1294 if (typy_get_composite (((type_object *) self)->type) == NULL)
1295 return NULL;
256458bc 1296
a73bb892
PK
1297 typy_iter_obj = PyObject_New (typy_iterator_object,
1298 &type_iterator_object_type);
1299 if (typy_iter_obj == NULL)
1300 return NULL;
1301
1302 typy_iter_obj->field = 0;
1303 typy_iter_obj->kind = kind;
1304 Py_INCREF (self);
1305 typy_iter_obj->source = (type_object *) self;
1306
1307 return (PyObject *) typy_iter_obj;
1308}
1309
1310/* iteritems() method. */
1311
1312static PyObject *
1313typy_iteritems (PyObject *self, PyObject *args)
1314{
1315 return typy_make_iter (self, iter_items);
1316}
1317
1318/* iterkeys() method. */
1319
1320static PyObject *
1321typy_iterkeys (PyObject *self, PyObject *args)
1322{
1323 return typy_make_iter (self, iter_keys);
1324}
1325
1326/* Iterating over the class, same as iterkeys except for the function
1327 signature. */
1328
1329static PyObject *
1330typy_iter (PyObject *self)
1331{
1332 return typy_make_iter (self, iter_keys);
1333}
1334
1335/* itervalues() method. */
1336
1337static PyObject *
1338typy_itervalues (PyObject *self, PyObject *args)
1339{
1340 return typy_make_iter (self, iter_values);
1341}
1342
1343/* Return a reference to the type iterator. */
1344
1345static PyObject *
1346typy_iterator_iter (PyObject *self)
1347{
1348 Py_INCREF (self);
1349 return self;
1350}
1351
1352/* Return the next field in the iteration through the list of fields
1353 of the type. */
1354
1355static PyObject *
1356typy_iterator_iternext (PyObject *self)
1357{
1358 typy_iterator_object *iter_obj = (typy_iterator_object *) self;
1359 struct type *type = iter_obj->source->type;
256458bc 1360
1f704f76 1361 if (iter_obj->field < type->num_fields ())
a73bb892 1362 {
1b20edf0
TT
1363 gdbpy_ref<> result = make_fielditem (type, iter_obj->field,
1364 iter_obj->kind);
a73bb892
PK
1365 if (result != NULL)
1366 iter_obj->field++;
1b20edf0 1367 return result.release ();
a73bb892
PK
1368 }
1369
1370 return NULL;
1371}
1372
1373static void
1374typy_iterator_dealloc (PyObject *obj)
1375{
1376 typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
1377
1378 Py_DECREF (iter_obj->source);
2e953aca 1379 Py_TYPE (obj)->tp_free (obj);
a73bb892
PK
1380}
1381
2c74e833
TT
1382/* Create a new Type referring to TYPE. */
1383PyObject *
1384type_to_type_object (struct type *type)
1385{
1386 type_object *type_obj;
1387
5d63b30a
TT
1388 try
1389 {
1390 /* Try not to let stub types leak out to Python. */
e46d3488 1391 if (type->is_stub ())
5d63b30a
TT
1392 type = check_typedef (type);
1393 }
1394 catch (...)
1395 {
1396 /* Just ignore failures in check_typedef. */
1397 }
1398
2c74e833
TT
1399 type_obj = PyObject_New (type_object, &type_object_type);
1400 if (type_obj)
1401 set_type (type_obj, type);
1402
1403 return (PyObject *) type_obj;
1404}
1405
1406struct type *
1407type_object_to_type (PyObject *obj)
1408{
1409 if (! PyObject_TypeCheck (obj, &type_object_type))
1410 return NULL;
1411 return ((type_object *) obj)->type;
1412}
1413
1414\f
1415
1416/* Implementation of gdb.lookup_type. */
1417PyObject *
1418gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
1419{
2adadf51 1420 static const char *keywords[] = { "name", "block", NULL };
ddd49eee 1421 const char *type_name = NULL;
2c74e833 1422 struct type *type = NULL;
5107b149 1423 PyObject *block_obj = NULL;
9df2fbc4 1424 const struct block *block = NULL;
2c74e833 1425
2adadf51
PA
1426 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1427 &type_name, &block_obj))
2c74e833
TT
1428 return NULL;
1429
5107b149
PM
1430 if (block_obj)
1431 {
1432 block = block_object_to_block (block_obj);
1433 if (! block)
1434 {
1435 PyErr_SetString (PyExc_RuntimeError,
1436 _("'block' argument must be a Block."));
1437 return NULL;
1438 }
1439 }
1440
1441 type = typy_lookup_typename (type_name, block);
2c74e833
TT
1442 if (! type)
1443 return NULL;
1444
8833fbf0 1445 return type_to_type_object (type);
2c74e833
TT
1446}
1447
3965bff5 1448static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
2c74e833
TT
1449gdbpy_initialize_types (void)
1450{
2c74e833 1451 if (PyType_Ready (&type_object_type) < 0)
999633ed 1452 return -1;
2c74e833 1453 if (PyType_Ready (&field_object_type) < 0)
999633ed 1454 return -1;
a73bb892 1455 if (PyType_Ready (&type_iterator_object_type) < 0)
999633ed 1456 return -1;
2c74e833 1457
dd1ae8ea 1458 for (const auto &item : pyty_codes)
2c74e833 1459 {
dd1ae8ea 1460 if (PyModule_AddIntConstant (gdb_module, item.name, item.code) < 0)
999633ed 1461 return -1;
2c74e833
TT
1462 }
1463
aa36459a
TT
1464 if (gdb_pymodule_addobject (gdb_module, "Type",
1465 (PyObject *) &type_object_type) < 0)
999633ed 1466 return -1;
2c74e833 1467
aa36459a
TT
1468 if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
1469 (PyObject *) &type_iterator_object_type) < 0)
999633ed 1470 return -1;
a73bb892 1471
aa36459a
TT
1472 return gdb_pymodule_addobject (gdb_module, "Field",
1473 (PyObject *) &field_object_type);
2c74e833
TT
1474}
1475
3965bff5
AB
1476GDBPY_INITIALIZE_FILE (gdbpy_initialize_types);
1477
2c74e833
TT
1478\f
1479
0d1f4ceb 1480static gdb_PyGetSetDef type_object_getset[] =
2c74e833 1481{
6d7bb824
TT
1482 { "alignof", typy_get_alignof, NULL,
1483 "The alignment of this type, in bytes.", NULL },
2c74e833
TT
1484 { "code", typy_get_code, NULL,
1485 "The code for this type.", NULL },
1acda803
TT
1486 { "dynamic", typy_get_dynamic, NULL,
1487 "Whether this type is dynamic.", NULL },
c0d48811
JB
1488 { "name", typy_get_name, NULL,
1489 "The name for this type, or None.", NULL },
2c74e833
TT
1490 { "sizeof", typy_get_sizeof, NULL,
1491 "The size of this type, in bytes.", NULL },
1492 { "tag", typy_get_tag, NULL,
1493 "The tag name for this type, or None.", NULL },
e1f2e1a2
CB
1494 { "objfile", typy_get_objfile, NULL,
1495 "The objfile this type was defined in, or None.", NULL },
ee6a3d9e
AB
1496 { "is_scalar", typy_is_scalar, nullptr,
1497 "Is this a scalar type?", nullptr },
551b380f
AB
1498 { "is_signed", typy_is_signed, nullptr,
1499 "Is this an signed type?", nullptr },
2c74e833
TT
1500 { NULL }
1501};
1502
1503static PyMethodDef type_object_methods[] =
1504{
702c2711 1505 { "array", typy_array, METH_VARARGS,
f28c316a
DE
1506 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1507Return a type which represents an array of objects of this type.\n\
1508The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1509If LOW_BOUND is omitted, a value of zero is used." },
a72c3253
DE
1510 { "vector", typy_vector, METH_VARARGS,
1511 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1512Return a type which represents a vector of objects of this type.\n\
1513The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1514If LOW_BOUND is omitted, a value of zero is used.\n\
1515Vectors differ from arrays in that if the current language has C-style\n\
1516arrays, vectors don't decay to a pointer to the first element.\n\
1517They are first class values." },
a73bb892
PK
1518 { "__contains__", typy_has_key, METH_VARARGS,
1519 "T.__contains__(k) -> True if T has a field named k, else False" },
2c74e833
TT
1520 { "const", typy_const, METH_NOARGS,
1521 "const () -> Type\n\
1522Return a const variant of this type." },
59fb7612
SS
1523 { "optimized_out", typy_optimized_out, METH_NOARGS,
1524 "optimized_out() -> Value\n\
1525Return optimized out value of this type." },
2c74e833 1526 { "fields", typy_fields, METH_NOARGS,
a73bb892
PK
1527 "fields () -> list\n\
1528Return a list holding all the fields of this type.\n\
1529Each field is a gdb.Field object." },
1530 { "get", typy_get, METH_VARARGS,
1531 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1532otherwise returns default, if supplied, or None if not." },
1533 { "has_key", typy_has_key, METH_VARARGS,
1534 "T.has_key(k) -> True if T has a field named k, else False" },
1535 { "items", typy_items, METH_NOARGS,
1536 "items () -> list\n\
1537Return a list of (name, field) pairs of this type.\n\
1538Each field is a gdb.Field object." },
1539 { "iteritems", typy_iteritems, METH_NOARGS,
1540 "iteritems () -> an iterator over the (name, field)\n\
1541pairs of this type. Each field is a gdb.Field object." },
1542 { "iterkeys", typy_iterkeys, METH_NOARGS,
1543 "iterkeys () -> an iterator over the field names of this type." },
1544 { "itervalues", typy_itervalues, METH_NOARGS,
1545 "itervalues () -> an iterator over the fields of this type.\n\
1546Each field is a gdb.Field object." },
1547 { "keys", typy_field_names, METH_NOARGS,
1548 "keys () -> list\n\
1549Return a list holding all the fields names of this type." },
2c74e833
TT
1550 { "pointer", typy_pointer, METH_NOARGS,
1551 "pointer () -> Type\n\
1552Return a type of pointer to this type." },
361ae042
PM
1553 { "range", typy_range, METH_NOARGS,
1554 "range () -> tuple\n\
1555Return a tuple containing the lower and upper range for this type."},
2c74e833
TT
1556 { "reference", typy_reference, METH_NOARGS,
1557 "reference () -> Type\n\
1558Return a type of reference to this type." },
1559 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1560 "strip_typedefs () -> Type\n\
1561Return a type formed by stripping this type of all typedefs."},
1562 { "target", typy_target, METH_NOARGS,
1563 "target () -> Type\n\
1564Return the target type of this type." },
1565 { "template_argument", typy_template_argument, METH_VARARGS,
5107b149 1566 "template_argument (arg, [block]) -> Type\n\
2c74e833
TT
1567Return the type of a template argument." },
1568 { "unqualified", typy_unqualified, METH_NOARGS,
1569 "unqualified () -> Type\n\
1570Return a variant of this type without const or volatile attributes." },
9cc10fd1 1571 { "values", typy_values, METH_NOARGS,
a73bb892
PK
1572 "values () -> list\n\
1573Return a list holding all the fields of this type.\n\
1574Each field is a gdb.Field object." },
2c74e833
TT
1575 { "volatile", typy_volatile, METH_NOARGS,
1576 "volatile () -> Type\n\
1577Return a volatile variant of this type" },
1578 { NULL }
1579};
1580
9cc10fd1
PK
1581static PyNumberMethods type_object_as_number = {
1582 NULL, /* nb_add */
1583 NULL, /* nb_subtract */
1584 NULL, /* nb_multiply */
9cc10fd1
PK
1585 NULL, /* nb_remainder */
1586 NULL, /* nb_divmod */
1587 NULL, /* nb_power */
1588 NULL, /* nb_negative */
1589 NULL, /* nb_positive */
1590 NULL, /* nb_absolute */
1591 typy_nonzero, /* nb_nonzero */
1592 NULL, /* nb_invert */
1593 NULL, /* nb_lshift */
1594 NULL, /* nb_rshift */
1595 NULL, /* nb_and */
1596 NULL, /* nb_xor */
1597 NULL, /* nb_or */
9a27f2c6
PK
1598 NULL, /* nb_int */
1599 NULL, /* reserved */
9cc10fd1 1600 NULL, /* nb_float */
9cc10fd1
PK
1601};
1602
a73bb892
PK
1603static PyMappingMethods typy_mapping = {
1604 typy_length,
1605 typy_getitem,
1606 NULL /* no "set" method */
1607};
1608
e36122e9 1609PyTypeObject type_object_type =
2c74e833 1610{
9a27f2c6 1611 PyVarObject_HEAD_INIT (NULL, 0)
2c74e833
TT
1612 "gdb.Type", /*tp_name*/
1613 sizeof (type_object), /*tp_basicsize*/
1614 0, /*tp_itemsize*/
1615 typy_dealloc, /*tp_dealloc*/
1616 0, /*tp_print*/
1617 0, /*tp_getattr*/
1618 0, /*tp_setattr*/
1619 0, /*tp_compare*/
1620 0, /*tp_repr*/
9cc10fd1 1621 &type_object_as_number, /*tp_as_number*/
2c74e833 1622 0, /*tp_as_sequence*/
a73bb892 1623 &typy_mapping, /*tp_as_mapping*/
2c74e833
TT
1624 0, /*tp_hash */
1625 0, /*tp_call*/
1626 typy_str, /*tp_str*/
1627 0, /*tp_getattro*/
1628 0, /*tp_setattro*/
1629 0, /*tp_as_buffer*/
0b233e34 1630 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2c74e833
TT
1631 "GDB type object", /* tp_doc */
1632 0, /* tp_traverse */
1633 0, /* tp_clear */
d839c8a4 1634 typy_richcompare, /* tp_richcompare */
2c74e833 1635 0, /* tp_weaklistoffset */
a73bb892 1636 typy_iter, /* tp_iter */
2c74e833
TT
1637 0, /* tp_iternext */
1638 type_object_methods, /* tp_methods */
1639 0, /* tp_members */
1640 type_object_getset, /* tp_getset */
1641 0, /* tp_base */
1642 0, /* tp_dict */
1643 0, /* tp_descr_get */
1644 0, /* tp_descr_set */
1645 0, /* tp_dictoffset */
1646 0, /* tp_init */
1647 0, /* tp_alloc */
1648 0, /* tp_new */
1649};
1650
0d1f4ceb 1651static gdb_PyGetSetDef field_object_getset[] =
2e8265fd
TT
1652{
1653 { "__dict__", gdb_py_generic_dict, NULL,
1654 "The __dict__ for this field.", &field_object_type },
1655 { NULL }
1656};
1657
e36122e9 1658PyTypeObject field_object_type =
2c74e833 1659{
9a27f2c6 1660 PyVarObject_HEAD_INIT (NULL, 0)
2c74e833
TT
1661 "gdb.Field", /*tp_name*/
1662 sizeof (field_object), /*tp_basicsize*/
1663 0, /*tp_itemsize*/
1664 field_dealloc, /*tp_dealloc*/
1665 0, /*tp_print*/
1666 0, /*tp_getattr*/
1667 0, /*tp_setattr*/
1668 0, /*tp_compare*/
1669 0, /*tp_repr*/
1670 0, /*tp_as_number*/
1671 0, /*tp_as_sequence*/
1672 0, /*tp_as_mapping*/
1673 0, /*tp_hash */
1674 0, /*tp_call*/
1675 0, /*tp_str*/
1676 0, /*tp_getattro*/
1677 0, /*tp_setattro*/
1678 0, /*tp_as_buffer*/
0b233e34 1679 Py_TPFLAGS_DEFAULT, /*tp_flags*/
2c74e833
TT
1680 "GDB field object", /* tp_doc */
1681 0, /* tp_traverse */
1682 0, /* tp_clear */
1683 0, /* tp_richcompare */
1684 0, /* tp_weaklistoffset */
1685 0, /* tp_iter */
1686 0, /* tp_iternext */
1687 0, /* tp_methods */
1688 0, /* tp_members */
2e8265fd 1689 field_object_getset, /* tp_getset */
2c74e833
TT
1690 0, /* tp_base */
1691 0, /* tp_dict */
1692 0, /* tp_descr_get */
1693 0, /* tp_descr_set */
1694 offsetof (field_object, dict), /* tp_dictoffset */
1695 0, /* tp_init */
1696 0, /* tp_alloc */
1697 0, /* tp_new */
1698};
a73bb892 1699
e36122e9 1700PyTypeObject type_iterator_object_type = {
9a27f2c6 1701 PyVarObject_HEAD_INIT (NULL, 0)
a73bb892
PK
1702 "gdb.TypeIterator", /*tp_name*/
1703 sizeof (typy_iterator_object), /*tp_basicsize*/
1704 0, /*tp_itemsize*/
1705 typy_iterator_dealloc, /*tp_dealloc*/
1706 0, /*tp_print*/
1707 0, /*tp_getattr*/
1708 0, /*tp_setattr*/
1709 0, /*tp_compare*/
1710 0, /*tp_repr*/
1711 0, /*tp_as_number*/
1712 0, /*tp_as_sequence*/
1713 0, /*tp_as_mapping*/
1714 0, /*tp_hash */
1715 0, /*tp_call*/
1716 0, /*tp_str*/
1717 0, /*tp_getattro*/
1718 0, /*tp_setattro*/
1719 0, /*tp_as_buffer*/
0b233e34 1720 Py_TPFLAGS_DEFAULT, /*tp_flags*/
a73bb892
PK
1721 "GDB type iterator object", /*tp_doc */
1722 0, /*tp_traverse */
1723 0, /*tp_clear */
1724 0, /*tp_richcompare */
1725 0, /*tp_weaklistoffset */
1726 typy_iterator_iter, /*tp_iter */
1727 typy_iterator_iternext, /*tp_iternext */
1728 0 /*tp_methods */
1729};