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