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