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