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