* gdb-add-index.sh: Really remove.
[deliverable/binutils-gdb.git] / gdb / python / py-type.c
CommitLineData
2c74e833
TT
1/* Python interface to types.
2
4c38e0a4 3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
2c74e833
TT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "value.h"
22#include "exceptions.h"
23#include "python-internal.h"
24#include "charset.h"
25#include "gdbtypes.h"
26#include "cp-support.h"
27#include "demangle.h"
28#include "objfiles.h"
e6c014f2 29#include "language.h"
d839c8a4
TT
30#include "vec.h"
31#include "bcache.h"
2c74e833
TT
32
33typedef struct pyty_type_object
34{
35 PyObject_HEAD
36 struct type *type;
37
38 /* If a Type object is associated with an objfile, it is kept on a
39 doubly-linked list, rooted in the objfile. This lets us copy the
40 underlying struct type when the objfile is deleted. */
41 struct pyty_type_object *prev;
42 struct pyty_type_object *next;
43} type_object;
44
45static PyTypeObject type_object_type;
46
47/* A Field object. */
48typedef struct pyty_field_object
49{
50 PyObject_HEAD
51
52 /* Dictionary holding our attributes. */
53 PyObject *dict;
54} field_object;
55
56static PyTypeObject field_object_type;
57
58/* This is used to initialize various gdb.TYPE_ constants. */
59struct pyty_code
60{
61 /* The code. */
62 enum type_code code;
63 /* The name. */
64 const char *name;
65};
66
67#define ENTRY(X) { X, #X }
68
69static struct pyty_code pyty_codes[] =
70{
71 ENTRY (TYPE_CODE_PTR),
72 ENTRY (TYPE_CODE_ARRAY),
73 ENTRY (TYPE_CODE_STRUCT),
74 ENTRY (TYPE_CODE_UNION),
75 ENTRY (TYPE_CODE_ENUM),
76 ENTRY (TYPE_CODE_FLAGS),
77 ENTRY (TYPE_CODE_FUNC),
78 ENTRY (TYPE_CODE_INT),
79 ENTRY (TYPE_CODE_FLT),
80 ENTRY (TYPE_CODE_VOID),
81 ENTRY (TYPE_CODE_SET),
82 ENTRY (TYPE_CODE_RANGE),
83 ENTRY (TYPE_CODE_STRING),
84 ENTRY (TYPE_CODE_BITSTRING),
85 ENTRY (TYPE_CODE_ERROR),
86 ENTRY (TYPE_CODE_METHOD),
87 ENTRY (TYPE_CODE_METHODPTR),
88 ENTRY (TYPE_CODE_MEMBERPTR),
89 ENTRY (TYPE_CODE_REF),
90 ENTRY (TYPE_CODE_CHAR),
91 ENTRY (TYPE_CODE_BOOL),
92 ENTRY (TYPE_CODE_COMPLEX),
93 ENTRY (TYPE_CODE_TYPEDEF),
94 ENTRY (TYPE_CODE_NAMESPACE),
95 ENTRY (TYPE_CODE_DECFLOAT),
96 ENTRY (TYPE_CODE_INTERNAL_FUNCTION),
97 { TYPE_CODE_UNDEF, NULL }
98};
99
100\f
101
102static void
103field_dealloc (PyObject *obj)
104{
105 field_object *f = (field_object *) obj;
d59b6f6c 106
2c74e833
TT
107 Py_XDECREF (f->dict);
108 f->ob_type->tp_free (obj);
109}
110
111static PyObject *
112field_new (void)
113{
114 field_object *result = PyObject_New (field_object, &field_object_type);
d59b6f6c 115
2c74e833
TT
116 if (result)
117 {
118 result->dict = PyDict_New ();
119 if (!result->dict)
120 {
121 Py_DECREF (result);
122 result = NULL;
123 }
124 }
125 return (PyObject *) result;
126}
127
128\f
129
130/* Return the code for this type. */
131static PyObject *
132typy_get_code (PyObject *self, void *closure)
133{
134 struct type *type = ((type_object *) self)->type;
d59b6f6c 135
2c74e833
TT
136 return PyInt_FromLong (TYPE_CODE (type));
137}
138
139/* Helper function for typy_fields which converts a single field to a
140 dictionary. Returns NULL on error. */
141static PyObject *
142convert_field (struct type *type, int field)
143{
144 PyObject *result = field_new ();
145 PyObject *arg;
146
147 if (!result)
148 return NULL;
149
150 if (!field_is_static (&TYPE_FIELD (type, field)))
151 {
152 arg = PyLong_FromLong (TYPE_FIELD_BITPOS (type, field));
153 if (!arg)
154 goto fail;
155
156 if (PyObject_SetAttrString (result, "bitpos", arg) < 0)
157 goto failarg;
158 }
159
160 if (TYPE_FIELD_NAME (type, field))
161 arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
162 else
163 {
164 arg = Py_None;
165 Py_INCREF (arg);
166 }
167 if (!arg)
168 goto fail;
169 if (PyObject_SetAttrString (result, "name", arg) < 0)
170 goto failarg;
171
172 arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
173 Py_INCREF (arg);
174 if (PyObject_SetAttrString (result, "artificial", arg) < 0)
175 goto failarg;
176
bfd31e71
PM
177 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
178 arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False;
179 else
180 arg = Py_False;
181 Py_INCREF (arg);
182 if (PyObject_SetAttrString (result, "is_base_class", arg) < 0)
183 goto failarg;
184
2c74e833
TT
185 arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
186 if (!arg)
187 goto fail;
188 if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
189 goto failarg;
190
191 /* A field can have a NULL type in some situations. */
192 if (TYPE_FIELD_TYPE (type, field) == NULL)
193 {
194 arg = Py_None;
195 Py_INCREF (arg);
196 }
197 else
198 arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
199 if (!arg)
200 goto fail;
201 if (PyObject_SetAttrString (result, "type", arg) < 0)
202 goto failarg;
203
204 return result;
205
206 failarg:
207 Py_DECREF (arg);
208 fail:
209 Py_DECREF (result);
210 return NULL;
211}
212
213/* Return a sequence of all fields. Each field is a dictionary with
214 some pre-defined keys. */
215static PyObject *
216typy_fields (PyObject *self, PyObject *args)
217{
218 PyObject *result;
219 int i;
220 struct type *type = ((type_object *) self)->type;
05d0e1e7
TT
221 volatile struct gdb_exception except;
222
223 TRY_CATCH (except, RETURN_MASK_ALL)
224 {
225 CHECK_TYPEDEF (type);
226 }
227 GDB_PY_HANDLE_EXCEPTION (except);
2c74e833
TT
228
229 /* We would like to make a tuple here, make fields immutable, and
230 then memoize the result (and perhaps make Field.type() lazy).
231 However, that can lead to cycles. */
232 result = PyList_New (0);
233
234 for (i = 0; i < TYPE_NFIELDS (type); ++i)
235 {
236 PyObject *dict = convert_field (type, i);
d59b6f6c 237
2c74e833
TT
238 if (!dict)
239 {
240 Py_DECREF (result);
241 return NULL;
242 }
243 if (PyList_Append (result, dict))
244 {
245 Py_DECREF (dict);
246 Py_DECREF (result);
247 return NULL;
248 }
249 }
250
251 return result;
252}
253
254/* Return the type's tag, or None. */
255static PyObject *
256typy_get_tag (PyObject *self, void *closure)
257{
258 struct type *type = ((type_object *) self)->type;
d59b6f6c 259
2c74e833
TT
260 if (!TYPE_TAG_NAME (type))
261 Py_RETURN_NONE;
262 return PyString_FromString (TYPE_TAG_NAME (type));
263}
264
265/* Return the type, stripped of typedefs. */
266static PyObject *
267typy_strip_typedefs (PyObject *self, PyObject *args)
268{
269 struct type *type = ((type_object *) self)->type;
270
271 return type_to_type_object (check_typedef (type));
272}
273
702c2711
TT
274/* Return an array type. */
275
276static PyObject *
277typy_array (PyObject *self, PyObject *args)
278{
279 int n1, n2;
280 PyObject *n2_obj = NULL;
281 struct type *array = NULL;
282 struct type *type = ((type_object *) self)->type;
283 volatile struct gdb_exception except;
284
285 if (! PyArg_ParseTuple (args, "i|O", &n1, &n2_obj))
286 return NULL;
287
288 if (n2_obj)
289 {
290 if (!PyInt_Check (n2_obj))
291 {
292 PyErr_SetString (PyExc_RuntimeError,
293 _("Array bound must be an integer"));
294 return NULL;
295 }
296 n2 = (int) PyInt_AsLong (n2_obj);
297 if (PyErr_Occurred ())
298 return NULL;
299 }
300 else
301 {
302 n2 = n1;
303 n1 = 0;
304 }
305
306 if (n2 < n1)
307 {
308 PyErr_SetString (PyExc_ValueError,
309 _("Array length must not be negative"));
310 return NULL;
311 }
312
313 TRY_CATCH (except, RETURN_MASK_ALL)
314 {
315 array = lookup_array_range_type (type, n1, n2);
316 }
317 GDB_PY_HANDLE_EXCEPTION (except);
318
319 return type_to_type_object (array);
320}
321
2c74e833
TT
322/* Return a Type object which represents a pointer to SELF. */
323static PyObject *
324typy_pointer (PyObject *self, PyObject *args)
325{
326 struct type *type = ((type_object *) self)->type;
327 volatile struct gdb_exception except;
328
329 TRY_CATCH (except, RETURN_MASK_ALL)
330 {
331 type = lookup_pointer_type (type);
332 }
333 GDB_PY_HANDLE_EXCEPTION (except);
334
335 return type_to_type_object (type);
336}
337
361ae042
PM
338/* Return the range of a type represented by SELF. The return type is
339 a tuple. The first element of the tuple contains the low bound,
340 while the second element of the tuple contains the high bound. */
341static PyObject *
342typy_range (PyObject *self, PyObject *args)
343{
344 struct type *type = ((type_object *) self)->type;
345 PyObject *result;
346 PyObject *low_bound = NULL, *high_bound = NULL;
8d099ae9
PM
347 /* Initialize these to appease GCC warnings. */
348 LONGEST low = 0, high = 0;
361ae042
PM
349
350 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
351 && TYPE_CODE (type) != TYPE_CODE_STRING
352 && TYPE_CODE (type) != TYPE_CODE_RANGE)
353 {
354 PyErr_SetString (PyExc_RuntimeError,
044c0f87 355 _("This type does not have a range."));
361ae042
PM
356 return NULL;
357 }
358
359 switch (TYPE_CODE (type))
360 {
361 case TYPE_CODE_ARRAY:
362 case TYPE_CODE_STRING:
363 low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
364 high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
365 break;
366 case TYPE_CODE_RANGE:
367 low = TYPE_LOW_BOUND (type);
368 high = TYPE_HIGH_BOUND (type);
369 break;
370 }
371
372 low_bound = PyLong_FromLong (low);
373 if (!low_bound)
374 goto failarg;
375
376 high_bound = PyLong_FromLong (high);
377 if (!high_bound)
378 goto failarg;
379
380 result = PyTuple_New (2);
381 if (!result)
382 goto failarg;
383
384 if (PyTuple_SetItem (result, 0, low_bound) != 0)
385 {
386 Py_DECREF (result);
387 goto failarg;
388 }
389 if (PyTuple_SetItem (result, 1, high_bound) != 0)
390 {
391 Py_DECREF (high_bound);
392 Py_DECREF (result);
393 return NULL;
394 }
395 return result;
396
397 failarg:
398 Py_XDECREF (high_bound);
399 Py_XDECREF (low_bound);
400 return NULL;
401}
402
2c74e833
TT
403/* Return a Type object which represents a reference to SELF. */
404static PyObject *
405typy_reference (PyObject *self, PyObject *args)
406{
407 struct type *type = ((type_object *) self)->type;
408 volatile struct gdb_exception except;
409
410 TRY_CATCH (except, RETURN_MASK_ALL)
411 {
412 type = lookup_reference_type (type);
413 }
414 GDB_PY_HANDLE_EXCEPTION (except);
415
416 return type_to_type_object (type);
417}
418
419/* Return a Type object which represents the target type of SELF. */
420static PyObject *
421typy_target (PyObject *self, PyObject *args)
422{
423 struct type *type = ((type_object *) self)->type;
424
425 if (!TYPE_TARGET_TYPE (type))
426 {
044c0f87
PM
427 PyErr_SetString (PyExc_RuntimeError,
428 _("Type does not have a target."));
2c74e833
TT
429 return NULL;
430 }
431
432 return type_to_type_object (TYPE_TARGET_TYPE (type));
433}
434
435/* Return a const-qualified type variant. */
436static PyObject *
437typy_const (PyObject *self, PyObject *args)
438{
439 struct type *type = ((type_object *) self)->type;
440 volatile struct gdb_exception except;
441
442 TRY_CATCH (except, RETURN_MASK_ALL)
443 {
444 type = make_cv_type (1, 0, type, NULL);
445 }
446 GDB_PY_HANDLE_EXCEPTION (except);
447
448 return type_to_type_object (type);
449}
450
451/* Return a volatile-qualified type variant. */
452static PyObject *
453typy_volatile (PyObject *self, PyObject *args)
454{
455 struct type *type = ((type_object *) self)->type;
456 volatile struct gdb_exception except;
457
458 TRY_CATCH (except, RETURN_MASK_ALL)
459 {
460 type = make_cv_type (0, 1, type, NULL);
461 }
462 GDB_PY_HANDLE_EXCEPTION (except);
463
464 return type_to_type_object (type);
465}
466
467/* Return an unqualified type variant. */
468static PyObject *
469typy_unqualified (PyObject *self, PyObject *args)
470{
471 struct type *type = ((type_object *) self)->type;
472 volatile struct gdb_exception except;
473
474 TRY_CATCH (except, RETURN_MASK_ALL)
475 {
476 type = make_cv_type (0, 0, type, NULL);
477 }
478 GDB_PY_HANDLE_EXCEPTION (except);
479
480 return type_to_type_object (type);
481}
482
483/* Return the size of the type represented by SELF, in bytes. */
484static PyObject *
485typy_get_sizeof (PyObject *self, void *closure)
486{
487 struct type *type = ((type_object *) self)->type;
488 volatile struct gdb_exception except;
489
490 TRY_CATCH (except, RETURN_MASK_ALL)
491 {
492 check_typedef (type);
493 }
494 /* Ignore exceptions. */
495
496 return PyLong_FromLong (TYPE_LENGTH (type));
497}
498
499static struct type *
5107b149 500typy_lookup_typename (char *type_name, struct block *block)
2c74e833
TT
501{
502 struct type *type = NULL;
503 volatile struct gdb_exception except;
d59b6f6c 504
2c74e833
TT
505 TRY_CATCH (except, RETURN_MASK_ALL)
506 {
507 if (!strncmp (type_name, "struct ", 7))
508 type = lookup_struct (type_name + 7, NULL);
509 else if (!strncmp (type_name, "union ", 6))
510 type = lookup_union (type_name + 6, NULL);
511 else if (!strncmp (type_name, "enum ", 5))
512 type = lookup_enum (type_name + 5, NULL);
513 else
d452c4bc 514 type = lookup_typename (python_language, python_gdbarch,
5107b149 515 type_name, block, 0);
2c74e833
TT
516 }
517 if (except.reason < 0)
518 {
519 PyErr_Format (except.reason == RETURN_QUIT
520 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
521 "%s", except.message);
522 return NULL;
523 }
524
525 return type;
526}
527
528static struct type *
5107b149
PM
529typy_lookup_type (struct demangle_component *demangled,
530 struct block *block)
2c74e833
TT
531{
532 struct type *type;
533 char *type_name;
534 enum demangle_component_type demangled_type;
535
536 /* Save the type: typy_lookup_type() may (indirectly) overwrite
537 memory pointed by demangled. */
538 demangled_type = demangled->type;
539
540 if (demangled_type == DEMANGLE_COMPONENT_POINTER
541 || demangled_type == DEMANGLE_COMPONENT_REFERENCE
542 || demangled_type == DEMANGLE_COMPONENT_CONST
543 || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
544 {
5107b149 545 type = typy_lookup_type (demangled->u.s_binary.left, block);
2c74e833
TT
546 if (! type)
547 return NULL;
548
549 switch (demangled_type)
550 {
551 case DEMANGLE_COMPONENT_REFERENCE:
552 return lookup_reference_type (type);
553 case DEMANGLE_COMPONENT_POINTER:
554 return lookup_pointer_type (type);
555 case DEMANGLE_COMPONENT_CONST:
556 return make_cv_type (1, 0, type, NULL);
557 case DEMANGLE_COMPONENT_VOLATILE:
558 return make_cv_type (0, 1, type, NULL);
559 }
560 }
561
562 type_name = cp_comp_to_string (demangled, 10);
5107b149 563 type = typy_lookup_typename (type_name, block);
2c74e833
TT
564 xfree (type_name);
565
566 return type;
567}
568
326fd672
TT
569/* This is a helper function for typy_template_argument that is used
570 when the type does not have template symbols attached. It works by
571 parsing the type name. This happens with compilers, like older
572 versions of GCC, that do not emit DW_TAG_template_*. */
573
2c74e833 574static PyObject *
326fd672
TT
575typy_legacy_template_argument (struct type *type, struct block *block,
576 int argno)
2c74e833 577{
326fd672 578 int i;
2c74e833
TT
579 struct demangle_component *demangled;
580 const char *err;
581 struct type *argtype;
2c74e833
TT
582
583 if (TYPE_NAME (type) == NULL)
584 {
5107b149 585 PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
2c74e833
TT
586 return NULL;
587 }
588
589 /* Note -- this is not thread-safe. */
590 demangled = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
591 if (! demangled)
592 {
593 PyErr_SetString (PyExc_RuntimeError, err);
594 return NULL;
595 }
596
597 /* Strip off component names. */
598 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
599 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
600 demangled = demangled->u.s_binary.right;
601
602 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
603 {
5107b149 604 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
2c74e833
TT
605 return NULL;
606 }
607
608 /* Skip from the template to the arguments. */
609 demangled = demangled->u.s_binary.right;
610
611 for (i = 0; demangled && i < argno; ++i)
612 demangled = demangled->u.s_binary.right;
613
614 if (! demangled)
615 {
5107b149 616 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
2c74e833
TT
617 argno);
618 return NULL;
619 }
620
5107b149 621 argtype = typy_lookup_type (demangled->u.s_binary.left, block);
2c74e833
TT
622 if (! argtype)
623 return NULL;
624
625 return type_to_type_object (argtype);
326fd672
TT
626}
627
628static PyObject *
629typy_template_argument (PyObject *self, PyObject *args)
630{
631 int argno;
632 struct type *type = ((type_object *) self)->type;
633 struct block *block = NULL;
634 PyObject *block_obj = NULL;
635 struct symbol *sym;
636 struct value *val = NULL;
637 volatile struct gdb_exception except;
638
639 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
640 return NULL;
641
642 if (block_obj)
643 {
644 block = block_object_to_block (block_obj);
645 if (! block)
646 {
647 PyErr_SetString (PyExc_RuntimeError,
648 _("Second argument must be block."));
649 return NULL;
650 }
651 }
652
05d0e1e7
TT
653 TRY_CATCH (except, RETURN_MASK_ALL)
654 {
655 type = check_typedef (type);
656 if (TYPE_CODE (type) == TYPE_CODE_REF)
657 type = check_typedef (TYPE_TARGET_TYPE (type));
658 }
659 GDB_PY_HANDLE_EXCEPTION (except);
326fd672
TT
660
661 /* We might not have DW_TAG_template_*, so try to parse the type's
662 name. This is inefficient if we do not have a template type --
663 but that is going to wind up as an error anyhow. */
664 if (! TYPE_N_TEMPLATE_ARGUMENTS (type))
665 return typy_legacy_template_argument (type, block, argno);
666
667 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
668 {
669 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
670 argno);
671 return NULL;
672 }
673
674 sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
675 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
676 return type_to_type_object (SYMBOL_TYPE (sym));
677 else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT)
678 {
679 PyErr_Format (PyExc_RuntimeError,
680 _("Template argument is optimized out"));
681 return NULL;
682 }
683
684 TRY_CATCH (except, RETURN_MASK_ALL)
685 {
686 val = value_of_variable (sym, block);
687 }
688 GDB_PY_HANDLE_EXCEPTION (except);
689
690 return value_to_value_object (val);
2c74e833
TT
691}
692
693static PyObject *
694typy_str (PyObject *self)
695{
696 volatile struct gdb_exception except;
697 char *thetype = NULL;
759ef836 698 long length = 0;
2c74e833
TT
699 PyObject *result;
700
701 TRY_CATCH (except, RETURN_MASK_ALL)
702 {
703 struct cleanup *old_chain;
704 struct ui_file *stb;
2c74e833
TT
705
706 stb = mem_fileopen ();
707 old_chain = make_cleanup_ui_file_delete (stb);
708
709 type_print (type_object_to_type (self), "", stb, -1);
710
711 thetype = ui_file_xstrdup (stb, &length);
712 do_cleanups (old_chain);
713 }
714 if (except.reason < 0)
715 {
716 xfree (thetype);
717 GDB_PY_HANDLE_EXCEPTION (except);
718 }
719
759ef836 720 result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
2c74e833
TT
721 xfree (thetype);
722
723 return result;
724}
725
d839c8a4
TT
726/* An entry in the type-equality bcache. */
727
728typedef struct type_equality_entry
729{
730 struct type *type1, *type2;
731} type_equality_entry_d;
732
733DEF_VEC_O (type_equality_entry_d);
734
735/* A helper function to compare two strings. Returns 1 if they are
736 the same, 0 otherwise. Handles NULLs properly. */
737
738static int
739compare_strings (const char *s, const char *t)
740{
741 if (s == NULL && t != NULL)
742 return 0;
743 else if (s != NULL && t == NULL)
744 return 0;
745 else if (s == NULL && t== NULL)
746 return 1;
747 return strcmp (s, t) == 0;
748}
749
750/* A helper function for typy_richcompare that checks two types for
751 "deep" equality. Returns Py_EQ if the types are considered the
752 same, Py_NE otherwise. */
753
754static int
755check_types_equal (struct type *type1, struct type *type2,
756 VEC (type_equality_entry_d) **worklist)
757{
758 CHECK_TYPEDEF (type1);
759 CHECK_TYPEDEF (type2);
760
761 if (type1 == type2)
762 return Py_EQ;
763
764 if (TYPE_CODE (type1) != TYPE_CODE (type2)
765 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
766 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
767 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
768 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
769 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
770 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
771 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
772 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
773 return Py_NE;
774
775 if (!compare_strings (TYPE_TAG_NAME (type1), TYPE_TAG_NAME (type2)))
776 return Py_NE;
777 if (!compare_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
778 return Py_NE;
779
780 if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
781 {
782 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
783 sizeof (*TYPE_RANGE_DATA (type1))) != 0)
784 return Py_NE;
785 }
786 else
787 {
788 int i;
789
790 for (i = 0; i < TYPE_NFIELDS (type1); ++i)
791 {
792 const struct field *field1 = &TYPE_FIELD (type1, i);
793 const struct field *field2 = &TYPE_FIELD (type2, i);
794 struct type_equality_entry entry;
795
796 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
797 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
798 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
799 return Py_NE;
800 if (!compare_strings (FIELD_NAME (*field1), FIELD_NAME (*field2)))
801 return Py_NE;
802 switch (FIELD_LOC_KIND (*field1))
803 {
804 case FIELD_LOC_KIND_BITPOS:
805 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
806 return Py_NE;
807 break;
808 case FIELD_LOC_KIND_PHYSADDR:
809 if (FIELD_STATIC_PHYSADDR (*field1)
810 != FIELD_STATIC_PHYSADDR (*field2))
811 return Py_NE;
812 break;
813 case FIELD_LOC_KIND_PHYSNAME:
814 if (!compare_strings (FIELD_STATIC_PHYSNAME (*field1),
815 FIELD_STATIC_PHYSNAME (*field2)))
816 return Py_NE;
817 break;
818 }
819
820 entry.type1 = FIELD_TYPE (*field1);
821 entry.type2 = FIELD_TYPE (*field2);
822 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
823 }
824 }
825
826 if (TYPE_TARGET_TYPE (type1) != NULL)
827 {
828 struct type_equality_entry entry;
829 int added;
830
831 if (TYPE_TARGET_TYPE (type2) == NULL)
832 return Py_NE;
833
834 entry.type1 = TYPE_TARGET_TYPE (type1);
835 entry.type2 = TYPE_TARGET_TYPE (type2);
836 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
837 }
838 else if (TYPE_TARGET_TYPE (type2) != NULL)
839 return Py_NE;
840
841 return Py_EQ;
842}
843
844/* Check types on a worklist for equality. Returns Py_NE if any pair
845 is not equal, Py_EQ if they are all considered equal. */
846
847static int
848check_types_worklist (VEC (type_equality_entry_d) **worklist,
849 struct bcache *cache)
850{
851 while (!VEC_empty (type_equality_entry_d, *worklist))
852 {
853 struct type_equality_entry entry;
854 int added;
855
856 entry = *VEC_last (type_equality_entry_d, *worklist);
857 VEC_pop (type_equality_entry_d, *worklist);
858
859 /* If the type pair has already been visited, we know it is
860 ok. */
861 bcache_full (&entry, sizeof (entry), cache, &added);
862 if (!added)
863 continue;
864
865 if (check_types_equal (entry.type1, entry.type2, worklist) == Py_NE)
866 return Py_NE;
867 }
868
869 return Py_EQ;
870}
871
872/* Implement the richcompare method. */
873
874static PyObject *
875typy_richcompare (PyObject *self, PyObject *other, int op)
876{
877 int result = Py_NE;
878 struct type *type1 = type_object_to_type (self);
879 struct type *type2 = type_object_to_type (other);
880 volatile struct gdb_exception except;
881
882 /* We can only compare ourselves to another Type object, and only
883 for equality or inequality. */
884 if (type2 == NULL || (op != Py_EQ && op != Py_NE))
885 {
886 Py_INCREF (Py_NotImplemented);
887 return Py_NotImplemented;
888 }
889
890 if (type1 == type2)
891 result = Py_EQ;
892 else
893 {
894 struct bcache *cache;
e8fd65ef 895 VEC (type_equality_entry_d) *worklist = NULL;
d839c8a4
TT
896 struct type_equality_entry entry;
897
cbd70537 898 cache = bcache_xmalloc (NULL, NULL);
d839c8a4
TT
899
900 entry.type1 = type1;
901 entry.type2 = type2;
902 VEC_safe_push (type_equality_entry_d, worklist, &entry);
903
904 TRY_CATCH (except, RETURN_MASK_ALL)
905 {
906 result = check_types_worklist (&worklist, cache);
907 }
908 if (except.reason < 0)
909 result = Py_NE;
910
911 bcache_xfree (cache);
912 VEC_free (type_equality_entry_d, worklist);
913 }
914
915 if (op == result)
916 Py_RETURN_TRUE;
917 Py_RETURN_FALSE;
918}
919
2c74e833
TT
920\f
921
922static const struct objfile_data *typy_objfile_data_key;
923
924static void
c1bd65d0 925save_objfile_types (struct objfile *objfile, void *datum)
2c74e833
TT
926{
927 type_object *obj = datum;
928 htab_t copied_types;
929 struct cleanup *cleanup;
2c74e833
TT
930
931 /* This prevents another thread from freeing the objects we're
932 operating on. */
d452c4bc 933 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
2c74e833
TT
934
935 copied_types = create_copied_types_hash (objfile);
936
937 while (obj)
938 {
939 type_object *next = obj->next;
940
941 htab_empty (copied_types);
942
943 obj->type = copy_type_recursive (objfile, obj->type, copied_types);
944
945 obj->next = NULL;
946 obj->prev = NULL;
947
948 obj = next;
949 }
950
951 htab_delete (copied_types);
952
953 do_cleanups (cleanup);
954}
955
956static void
957set_type (type_object *obj, struct type *type)
958{
959 obj->type = type;
960 obj->prev = NULL;
961 if (type && TYPE_OBJFILE (type))
962 {
963 struct objfile *objfile = TYPE_OBJFILE (type);
964
965 obj->next = objfile_data (objfile, typy_objfile_data_key);
966 if (obj->next)
967 obj->next->prev = obj;
968 set_objfile_data (objfile, typy_objfile_data_key, obj);
969 }
970 else
971 obj->next = NULL;
972}
973
974static void
975typy_dealloc (PyObject *obj)
976{
977 type_object *type = (type_object *) obj;
978
979 if (type->prev)
980 type->prev->next = type->next;
981 else if (type->type && TYPE_OBJFILE (type->type))
982 {
983 /* Must reset head of list. */
984 struct objfile *objfile = TYPE_OBJFILE (type->type);
d59b6f6c 985
2c74e833
TT
986 if (objfile)
987 set_objfile_data (objfile, typy_objfile_data_key, type->next);
988 }
989 if (type->next)
990 type->next->prev = type->prev;
991
992 type->ob_type->tp_free (type);
993}
994
995/* Create a new Type referring to TYPE. */
996PyObject *
997type_to_type_object (struct type *type)
998{
999 type_object *type_obj;
1000
1001 type_obj = PyObject_New (type_object, &type_object_type);
1002 if (type_obj)
1003 set_type (type_obj, type);
1004
1005 return (PyObject *) type_obj;
1006}
1007
1008struct type *
1009type_object_to_type (PyObject *obj)
1010{
1011 if (! PyObject_TypeCheck (obj, &type_object_type))
1012 return NULL;
1013 return ((type_object *) obj)->type;
1014}
1015
1016\f
1017
1018/* Implementation of gdb.lookup_type. */
1019PyObject *
1020gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
1021{
5107b149 1022 static char *keywords[] = { "name", "block", NULL };
2c74e833
TT
1023 char *type_name = NULL;
1024 struct type *type = NULL;
5107b149
PM
1025 PyObject *block_obj = NULL;
1026 struct block *block = NULL;
2c74e833 1027
5107b149
PM
1028 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1029 &type_name, &block_obj))
2c74e833
TT
1030 return NULL;
1031
5107b149
PM
1032 if (block_obj)
1033 {
1034 block = block_object_to_block (block_obj);
1035 if (! block)
1036 {
1037 PyErr_SetString (PyExc_RuntimeError,
1038 _("'block' argument must be a Block."));
1039 return NULL;
1040 }
1041 }
1042
1043 type = typy_lookup_typename (type_name, block);
2c74e833
TT
1044 if (! type)
1045 return NULL;
1046
1047 return (PyObject *) type_to_type_object (type);
1048}
1049
1050void
1051gdbpy_initialize_types (void)
1052{
1053 int i;
1054
1055 typy_objfile_data_key
c1bd65d0 1056 = register_objfile_data_with_cleanup (save_objfile_types, NULL);
2c74e833
TT
1057
1058 if (PyType_Ready (&type_object_type) < 0)
1059 return;
1060 if (PyType_Ready (&field_object_type) < 0)
1061 return;
1062
1063 for (i = 0; pyty_codes[i].name; ++i)
1064 {
1065 if (PyModule_AddIntConstant (gdb_module,
1066 /* Cast needed for Python 2.4. */
1067 (char *) pyty_codes[i].name,
1068 pyty_codes[i].code) < 0)
1069 return;
1070 }
1071
1072 Py_INCREF (&type_object_type);
1073 PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type);
1074
1075 Py_INCREF (&field_object_type);
1076 PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type);
1077}
1078
1079\f
1080
1081static PyGetSetDef type_object_getset[] =
1082{
1083 { "code", typy_get_code, NULL,
1084 "The code for this type.", NULL },
1085 { "sizeof", typy_get_sizeof, NULL,
1086 "The size of this type, in bytes.", NULL },
1087 { "tag", typy_get_tag, NULL,
1088 "The tag name for this type, or None.", NULL },
1089 { NULL }
1090};
1091
1092static PyMethodDef type_object_methods[] =
1093{
702c2711
TT
1094 { "array", typy_array, METH_VARARGS,
1095 "array (N) -> Type\n\
1096Return a type which represents an array of N objects of this type." },
2c74e833
TT
1097 { "const", typy_const, METH_NOARGS,
1098 "const () -> Type\n\
1099Return a const variant of this type." },
1100 { "fields", typy_fields, METH_NOARGS,
1101 "field () -> list\n\
1102Return a sequence holding all the fields of this type.\n\
1103Each field is a dictionary." },
1104 { "pointer", typy_pointer, METH_NOARGS,
1105 "pointer () -> Type\n\
1106Return a type of pointer to this type." },
361ae042
PM
1107 { "range", typy_range, METH_NOARGS,
1108 "range () -> tuple\n\
1109Return a tuple containing the lower and upper range for this type."},
2c74e833
TT
1110 { "reference", typy_reference, METH_NOARGS,
1111 "reference () -> Type\n\
1112Return a type of reference to this type." },
1113 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1114 "strip_typedefs () -> Type\n\
1115Return a type formed by stripping this type of all typedefs."},
1116 { "target", typy_target, METH_NOARGS,
1117 "target () -> Type\n\
1118Return the target type of this type." },
1119 { "template_argument", typy_template_argument, METH_VARARGS,
5107b149 1120 "template_argument (arg, [block]) -> Type\n\
2c74e833
TT
1121Return the type of a template argument." },
1122 { "unqualified", typy_unqualified, METH_NOARGS,
1123 "unqualified () -> Type\n\
1124Return a variant of this type without const or volatile attributes." },
1125 { "volatile", typy_volatile, METH_NOARGS,
1126 "volatile () -> Type\n\
1127Return a volatile variant of this type" },
1128 { NULL }
1129};
1130
1131static PyTypeObject type_object_type =
1132{
1133 PyObject_HEAD_INIT (NULL)
1134 0, /*ob_size*/
1135 "gdb.Type", /*tp_name*/
1136 sizeof (type_object), /*tp_basicsize*/
1137 0, /*tp_itemsize*/
1138 typy_dealloc, /*tp_dealloc*/
1139 0, /*tp_print*/
1140 0, /*tp_getattr*/
1141 0, /*tp_setattr*/
1142 0, /*tp_compare*/
1143 0, /*tp_repr*/
1144 0, /*tp_as_number*/
1145 0, /*tp_as_sequence*/
1146 0, /*tp_as_mapping*/
1147 0, /*tp_hash */
1148 0, /*tp_call*/
1149 typy_str, /*tp_str*/
1150 0, /*tp_getattro*/
1151 0, /*tp_setattro*/
1152 0, /*tp_as_buffer*/
1153 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1154 "GDB type object", /* tp_doc */
1155 0, /* tp_traverse */
1156 0, /* tp_clear */
d839c8a4 1157 typy_richcompare, /* tp_richcompare */
2c74e833
TT
1158 0, /* tp_weaklistoffset */
1159 0, /* tp_iter */
1160 0, /* tp_iternext */
1161 type_object_methods, /* tp_methods */
1162 0, /* tp_members */
1163 type_object_getset, /* tp_getset */
1164 0, /* tp_base */
1165 0, /* tp_dict */
1166 0, /* tp_descr_get */
1167 0, /* tp_descr_set */
1168 0, /* tp_dictoffset */
1169 0, /* tp_init */
1170 0, /* tp_alloc */
1171 0, /* tp_new */
1172};
1173
1174static PyTypeObject field_object_type =
1175{
1176 PyObject_HEAD_INIT (NULL)
1177 0, /*ob_size*/
1178 "gdb.Field", /*tp_name*/
1179 sizeof (field_object), /*tp_basicsize*/
1180 0, /*tp_itemsize*/
1181 field_dealloc, /*tp_dealloc*/
1182 0, /*tp_print*/
1183 0, /*tp_getattr*/
1184 0, /*tp_setattr*/
1185 0, /*tp_compare*/
1186 0, /*tp_repr*/
1187 0, /*tp_as_number*/
1188 0, /*tp_as_sequence*/
1189 0, /*tp_as_mapping*/
1190 0, /*tp_hash */
1191 0, /*tp_call*/
1192 0, /*tp_str*/
1193 0, /*tp_getattro*/
1194 0, /*tp_setattro*/
1195 0, /*tp_as_buffer*/
1196 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1197 "GDB field object", /* tp_doc */
1198 0, /* tp_traverse */
1199 0, /* tp_clear */
1200 0, /* tp_richcompare */
1201 0, /* tp_weaklistoffset */
1202 0, /* tp_iter */
1203 0, /* tp_iternext */
1204 0, /* tp_methods */
1205 0, /* tp_members */
1206 0, /* tp_getset */
1207 0, /* tp_base */
1208 0, /* tp_dict */
1209 0, /* tp_descr_get */
1210 0, /* tp_descr_set */
1211 offsetof (field_object, dict), /* tp_dictoffset */
1212 0, /* tp_init */
1213 0, /* tp_alloc */
1214 0, /* tp_new */
1215};
This page took 0.224771 seconds and 4 git commands to generate.