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