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