Remove trailing whitespace.
[deliverable/binutils-gdb.git] / gdb / python / py-type.c
CommitLineData
2c74e833
TT
1/* Python interface to types.
2
28e7fd62 3 Copyright (C) 2008-2013 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 30#include "vec.h"
53342f27 31#include "typeprint.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
62eec1a5
TT
45static PyTypeObject type_object_type
46 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object");
2c74e833
TT
47
48/* A Field object. */
49typedef struct pyty_field_object
50{
51 PyObject_HEAD
52
53 /* Dictionary holding our attributes. */
54 PyObject *dict;
55} field_object;
56
62eec1a5
TT
57static PyTypeObject field_object_type
58 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object");
2c74e833 59
a73bb892
PK
60/* A type iterator object. */
61typedef struct {
62 PyObject_HEAD
63 /* The current field index. */
64 int field;
65 /* What to return. */
66 enum gdbpy_iter_kind kind;
67 /* Pointer back to the original source type object. */
68 struct pyty_type_object *source;
69} typy_iterator_object;
70
62eec1a5
TT
71static PyTypeObject type_iterator_object_type
72 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object");
a73bb892 73
2c74e833
TT
74/* This is used to initialize various gdb.TYPE_ constants. */
75struct pyty_code
76{
77 /* The code. */
78 enum type_code code;
79 /* The name. */
80 const char *name;
81};
82
0dab82e9
PK
83/* Forward declarations. */
84static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind);
85
2c74e833
TT
86#define ENTRY(X) { X, #X }
87
88static struct pyty_code pyty_codes[] =
89{
6b1755ce 90 ENTRY (TYPE_CODE_BITSTRING),
2c74e833
TT
91 ENTRY (TYPE_CODE_PTR),
92 ENTRY (TYPE_CODE_ARRAY),
93 ENTRY (TYPE_CODE_STRUCT),
94 ENTRY (TYPE_CODE_UNION),
95 ENTRY (TYPE_CODE_ENUM),
96 ENTRY (TYPE_CODE_FLAGS),
97 ENTRY (TYPE_CODE_FUNC),
98 ENTRY (TYPE_CODE_INT),
99 ENTRY (TYPE_CODE_FLT),
100 ENTRY (TYPE_CODE_VOID),
101 ENTRY (TYPE_CODE_SET),
102 ENTRY (TYPE_CODE_RANGE),
103 ENTRY (TYPE_CODE_STRING),
2c74e833
TT
104 ENTRY (TYPE_CODE_ERROR),
105 ENTRY (TYPE_CODE_METHOD),
106 ENTRY (TYPE_CODE_METHODPTR),
107 ENTRY (TYPE_CODE_MEMBERPTR),
108 ENTRY (TYPE_CODE_REF),
109 ENTRY (TYPE_CODE_CHAR),
110 ENTRY (TYPE_CODE_BOOL),
111 ENTRY (TYPE_CODE_COMPLEX),
112 ENTRY (TYPE_CODE_TYPEDEF),
113 ENTRY (TYPE_CODE_NAMESPACE),
114 ENTRY (TYPE_CODE_DECFLOAT),
115 ENTRY (TYPE_CODE_INTERNAL_FUNCTION),
116 { TYPE_CODE_UNDEF, NULL }
117};
118
119\f
120
121static void
122field_dealloc (PyObject *obj)
123{
124 field_object *f = (field_object *) obj;
d59b6f6c 125
2c74e833 126 Py_XDECREF (f->dict);
9a27f2c6 127 Py_TYPE (obj)->tp_free (obj);
2c74e833
TT
128}
129
130static PyObject *
131field_new (void)
132{
133 field_object *result = PyObject_New (field_object, &field_object_type);
d59b6f6c 134
2c74e833
TT
135 if (result)
136 {
137 result->dict = PyDict_New ();
138 if (!result->dict)
139 {
140 Py_DECREF (result);
141 result = NULL;
142 }
143 }
144 return (PyObject *) result;
145}
146
147\f
148
149/* Return the code for this type. */
150static PyObject *
151typy_get_code (PyObject *self, void *closure)
152{
153 struct type *type = ((type_object *) self)->type;
d59b6f6c 154
2c74e833
TT
155 return PyInt_FromLong (TYPE_CODE (type));
156}
157
158/* Helper function for typy_fields which converts a single field to a
a73bb892
PK
159 gdb.Field object. Returns NULL on error. */
160
2c74e833
TT
161static PyObject *
162convert_field (struct type *type, int field)
163{
164 PyObject *result = field_new ();
165 PyObject *arg;
166
167 if (!result)
168 return NULL;
169
170 if (!field_is_static (&TYPE_FIELD (type, field)))
171 {
14e75d8e
JK
172 const char *attrstring;
173
174 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
175 {
176 arg = gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type, field));
177 attrstring = "enumval";
178 }
179 else
180 {
200bc880 181 arg = gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type, field));
14e75d8e
JK
182 attrstring = "bitpos";
183 }
184
2c74e833
TT
185 if (!arg)
186 goto fail;
187
4fb2c64a
JK
188 /* At least python-2.4 had the second parameter non-const. */
189 if (PyObject_SetAttrString (result, (char *) attrstring, arg) < 0)
2c74e833 190 goto failarg;
db6573d6 191 Py_DECREF (arg);
2c74e833
TT
192 }
193
194 if (TYPE_FIELD_NAME (type, field))
195 arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
196 else
197 {
198 arg = Py_None;
199 Py_INCREF (arg);
200 }
201 if (!arg)
202 goto fail;
203 if (PyObject_SetAttrString (result, "name", arg) < 0)
204 goto failarg;
db6573d6 205 Py_DECREF (arg);
2c74e833
TT
206
207 arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
208 Py_INCREF (arg);
209 if (PyObject_SetAttrString (result, "artificial", arg) < 0)
210 goto failarg;
db6573d6 211 Py_DECREF (arg);
2c74e833 212
bfd31e71
PM
213 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
214 arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False;
215 else
216 arg = Py_False;
217 Py_INCREF (arg);
218 if (PyObject_SetAttrString (result, "is_base_class", arg) < 0)
219 goto failarg;
db6573d6 220 Py_DECREF (arg);
bfd31e71 221
2c74e833
TT
222 arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
223 if (!arg)
224 goto fail;
225 if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
226 goto failarg;
db6573d6 227 Py_DECREF (arg);
2c74e833
TT
228
229 /* A field can have a NULL type in some situations. */
230 if (TYPE_FIELD_TYPE (type, field) == NULL)
231 {
232 arg = Py_None;
233 Py_INCREF (arg);
234 }
235 else
236 arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
237 if (!arg)
238 goto fail;
239 if (PyObject_SetAttrString (result, "type", arg) < 0)
240 goto failarg;
db6573d6 241 Py_DECREF (arg);
2c74e833
TT
242
243 return result;
244
245 failarg:
246 Py_DECREF (arg);
247 fail:
248 Py_DECREF (result);
249 return NULL;
250}
251
a73bb892
PK
252/* Helper function to return the name of a field, as a gdb.Field object.
253 If the field doesn't have a name, None is returned. */
254
2c74e833 255static PyObject *
a73bb892 256field_name (struct type *type, int field)
2c74e833
TT
257{
258 PyObject *result;
a73bb892
PK
259
260 if (TYPE_FIELD_NAME (type, field))
261 result = PyString_FromString (TYPE_FIELD_NAME (type, field));
262 else
263 {
264 result = Py_None;
265 Py_INCREF (result);
266 }
267 return result;
268}
269
270/* Helper function for Type standard mapping methods. Returns a
271 Python object for field i of the type. "kind" specifies what to
272 return: the name of the field, a gdb.Field object corresponding to
273 the field, or a tuple consisting of field name and gdb.Field
274 object. */
275
276static PyObject *
277make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
278{
279 PyObject *item = NULL, *key = NULL, *value = NULL;
280
281 switch (kind)
282 {
283 case iter_items:
284 key = field_name (type, i);
285 if (key == NULL)
286 goto fail;
287 value = convert_field (type, i);
288 if (value == NULL)
289 goto fail;
290 item = PyTuple_New (2);
291 if (item == NULL)
292 goto fail;
293 PyTuple_SET_ITEM (item, 0, key);
294 PyTuple_SET_ITEM (item, 1, value);
295 break;
296 case iter_keys:
297 item = field_name (type, i);
298 break;
299 case iter_values:
300 item = convert_field (type, i);
301 break;
0d3a2e8a
TT
302 default:
303 gdb_assert_not_reached ("invalid gdbpy_iter_kind");
a73bb892
PK
304 }
305 return item;
256458bc 306
a73bb892
PK
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
317static PyObject *
318typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
319{
f6b47be4 320 PyObject *py_type = self;
0dab82e9 321 PyObject *result = NULL, *iter = NULL;
f6b47be4
DE
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
2c74e833 347 return result;
a73bb892
PK
348}
349
350/* Return a sequence of all fields. Each field is a gdb.Field object. */
351
352static PyObject *
9cc10fd1 353typy_values (PyObject *self, PyObject *args)
a73bb892
PK
354{
355 return typy_fields_items (self, iter_values);
356}
357
9cc10fd1 358/* Return a sequence of all fields. Each field is a gdb.Field object.
256458bc 359 This method is similar to typy_values, except where the supplied
9cc10fd1
PK
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
363static PyObject *
364typy_fields (PyObject *self, PyObject *args)
365{
366 struct type *type = ((type_object *) self)->type;
367 PyObject *r, *rl;
256458bc 368
9cc10fd1
PK
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;
256458bc 378
9cc10fd1 379 rl = Py_BuildValue ("[O]", r);
53e66479 380 Py_DECREF (r);
9cc10fd1
PK
381
382 return rl;
383}
384
a73bb892
PK
385/* Return a sequence of all field names. Each field is a gdb.Field object. */
386
387static PyObject *
388typy_field_names (PyObject *self, PyObject *args)
389{
390 return typy_fields_items (self, iter_keys);
391}
392
256458bc 393/* Return a sequence of all (name, fields) pairs. Each field is a
a73bb892
PK
394 gdb.Field object. */
395
396static PyObject *
397typy_items (PyObject *self, PyObject *args)
398{
399 return typy_fields_items (self, iter_items);
2c74e833
TT
400}
401
402/* Return the type's tag, or None. */
403static PyObject *
404typy_get_tag (PyObject *self, void *closure)
405{
406 struct type *type = ((type_object *) self)->type;
d59b6f6c 407
2c74e833
TT
408 if (!TYPE_TAG_NAME (type))
409 Py_RETURN_NONE;
410 return PyString_FromString (TYPE_TAG_NAME (type));
411}
412
413/* Return the type, stripped of typedefs. */
414static PyObject *
415typy_strip_typedefs (PyObject *self, PyObject *args)
416{
417 struct type *type = ((type_object *) self)->type;
5d9c5995
PM
418 volatile struct gdb_exception except;
419
420 TRY_CATCH (except, RETURN_MASK_ALL)
421 {
422 type = check_typedef (type);
423 }
424 GDB_PY_HANDLE_EXCEPTION (except);
2c74e833 425
bc9abe4a 426 return type_to_type_object (type);
2c74e833
TT
427}
428
9cc10fd1
PK
429/* Strip typedefs and pointers/reference from a type. Then check that
430 it is a struct, union, or enum type. If not, raise TypeError. */
431
432static struct type *
433typy_get_composite (struct type *type)
434{
435 volatile struct gdb_exception except;
436
437 for (;;)
438 {
439 TRY_CATCH (except, RETURN_MASK_ALL)
440 {
441 CHECK_TYPEDEF (type);
442 }
56cc411c 443 GDB_PY_HANDLE_EXCEPTION (except);
9cc10fd1
PK
444
445 if (TYPE_CODE (type) != TYPE_CODE_PTR
446 && TYPE_CODE (type) != TYPE_CODE_REF)
447 break;
448 type = TYPE_TARGET_TYPE (type);
449 }
450
451 /* If this is not a struct, union, or enum type, raise TypeError
452 exception. */
256458bc 453 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
9cc10fd1
PK
454 && TYPE_CODE (type) != TYPE_CODE_UNION
455 && TYPE_CODE (type) != TYPE_CODE_ENUM)
456 {
457 PyErr_SetString (PyExc_TypeError,
458 "Type is not a structure, union, or enum type.");
459 return NULL;
460 }
256458bc 461
9cc10fd1
PK
462 return type;
463}
464
a72c3253 465/* Helper for typy_array and typy_vector. */
702c2711
TT
466
467static PyObject *
a72c3253 468typy_array_1 (PyObject *self, PyObject *args, int is_vector)
702c2711 469{
74aedc46 470 long n1, n2;
702c2711
TT
471 PyObject *n2_obj = NULL;
472 struct type *array = NULL;
473 struct type *type = ((type_object *) self)->type;
474 volatile struct gdb_exception except;
475
74aedc46 476 if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
702c2711
TT
477 return NULL;
478
479 if (n2_obj)
480 {
481 if (!PyInt_Check (n2_obj))
482 {
483 PyErr_SetString (PyExc_RuntimeError,
484 _("Array bound must be an integer"));
485 return NULL;
486 }
74aedc46
TT
487
488 if (! gdb_py_int_as_long (n2_obj, &n2))
702c2711
TT
489 return NULL;
490 }
491 else
492 {
493 n2 = n1;
494 n1 = 0;
495 }
496
497 if (n2 < n1)
498 {
499 PyErr_SetString (PyExc_ValueError,
500 _("Array length must not be negative"));
501 return NULL;
502 }
503
504 TRY_CATCH (except, RETURN_MASK_ALL)
505 {
506 array = lookup_array_range_type (type, n1, n2);
a72c3253
DE
507 if (is_vector)
508 make_vector_type (array);
702c2711
TT
509 }
510 GDB_PY_HANDLE_EXCEPTION (except);
511
512 return type_to_type_object (array);
513}
514
a72c3253
DE
515/* Return an array type. */
516
517static PyObject *
518typy_array (PyObject *self, PyObject *args)
519{
520 return typy_array_1 (self, args, 0);
521}
522
523/* Return a vector type. */
524
525static PyObject *
526typy_vector (PyObject *self, PyObject *args)
527{
528 return typy_array_1 (self, args, 1);
529}
530
2c74e833
TT
531/* Return a Type object which represents a pointer to SELF. */
532static PyObject *
533typy_pointer (PyObject *self, PyObject *args)
534{
535 struct type *type = ((type_object *) self)->type;
536 volatile struct gdb_exception except;
537
538 TRY_CATCH (except, RETURN_MASK_ALL)
539 {
540 type = lookup_pointer_type (type);
541 }
542 GDB_PY_HANDLE_EXCEPTION (except);
543
544 return type_to_type_object (type);
545}
546
361ae042
PM
547/* Return the range of a type represented by SELF. The return type is
548 a tuple. The first element of the tuple contains the low bound,
549 while the second element of the tuple contains the high bound. */
550static PyObject *
551typy_range (PyObject *self, PyObject *args)
552{
553 struct type *type = ((type_object *) self)->type;
554 PyObject *result;
555 PyObject *low_bound = NULL, *high_bound = NULL;
8d099ae9
PM
556 /* Initialize these to appease GCC warnings. */
557 LONGEST low = 0, high = 0;
361ae042
PM
558
559 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
560 && TYPE_CODE (type) != TYPE_CODE_STRING
561 && TYPE_CODE (type) != TYPE_CODE_RANGE)
562 {
563 PyErr_SetString (PyExc_RuntimeError,
044c0f87 564 _("This type does not have a range."));
361ae042
PM
565 return NULL;
566 }
567
568 switch (TYPE_CODE (type))
569 {
570 case TYPE_CODE_ARRAY:
571 case TYPE_CODE_STRING:
572 low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
573 high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
574 break;
575 case TYPE_CODE_RANGE:
576 low = TYPE_LOW_BOUND (type);
577 high = TYPE_HIGH_BOUND (type);
578 break;
579 }
580
581 low_bound = PyLong_FromLong (low);
582 if (!low_bound)
583 goto failarg;
584
585 high_bound = PyLong_FromLong (high);
586 if (!high_bound)
587 goto failarg;
588
589 result = PyTuple_New (2);
590 if (!result)
591 goto failarg;
592
593 if (PyTuple_SetItem (result, 0, low_bound) != 0)
594 {
595 Py_DECREF (result);
596 goto failarg;
597 }
598 if (PyTuple_SetItem (result, 1, high_bound) != 0)
599 {
600 Py_DECREF (high_bound);
601 Py_DECREF (result);
602 return NULL;
603 }
604 return result;
256458bc 605
361ae042
PM
606 failarg:
607 Py_XDECREF (high_bound);
608 Py_XDECREF (low_bound);
609 return NULL;
610}
611
2c74e833
TT
612/* Return a Type object which represents a reference to SELF. */
613static PyObject *
614typy_reference (PyObject *self, PyObject *args)
615{
616 struct type *type = ((type_object *) self)->type;
617 volatile struct gdb_exception except;
618
619 TRY_CATCH (except, RETURN_MASK_ALL)
620 {
621 type = lookup_reference_type (type);
622 }
623 GDB_PY_HANDLE_EXCEPTION (except);
624
625 return type_to_type_object (type);
626}
627
628/* Return a Type object which represents the target type of SELF. */
629static PyObject *
630typy_target (PyObject *self, PyObject *args)
631{
632 struct type *type = ((type_object *) self)->type;
633
634 if (!TYPE_TARGET_TYPE (type))
635 {
256458bc 636 PyErr_SetString (PyExc_RuntimeError,
044c0f87 637 _("Type does not have a target."));
2c74e833
TT
638 return NULL;
639 }
640
641 return type_to_type_object (TYPE_TARGET_TYPE (type));
642}
643
644/* Return a const-qualified type variant. */
645static PyObject *
646typy_const (PyObject *self, PyObject *args)
647{
648 struct type *type = ((type_object *) self)->type;
649 volatile struct gdb_exception except;
650
651 TRY_CATCH (except, RETURN_MASK_ALL)
652 {
653 type = make_cv_type (1, 0, type, NULL);
654 }
655 GDB_PY_HANDLE_EXCEPTION (except);
656
657 return type_to_type_object (type);
658}
659
660/* Return a volatile-qualified type variant. */
661static PyObject *
662typy_volatile (PyObject *self, PyObject *args)
663{
664 struct type *type = ((type_object *) self)->type;
665 volatile struct gdb_exception except;
666
667 TRY_CATCH (except, RETURN_MASK_ALL)
668 {
669 type = make_cv_type (0, 1, type, NULL);
670 }
671 GDB_PY_HANDLE_EXCEPTION (except);
672
673 return type_to_type_object (type);
674}
675
676/* Return an unqualified type variant. */
677static PyObject *
678typy_unqualified (PyObject *self, PyObject *args)
679{
680 struct type *type = ((type_object *) self)->type;
681 volatile struct gdb_exception except;
682
683 TRY_CATCH (except, RETURN_MASK_ALL)
684 {
685 type = make_cv_type (0, 0, type, NULL);
686 }
687 GDB_PY_HANDLE_EXCEPTION (except);
688
689 return type_to_type_object (type);
690}
691
692/* Return the size of the type represented by SELF, in bytes. */
693static PyObject *
694typy_get_sizeof (PyObject *self, void *closure)
695{
696 struct type *type = ((type_object *) self)->type;
697 volatile struct gdb_exception except;
698
699 TRY_CATCH (except, RETURN_MASK_ALL)
700 {
701 check_typedef (type);
702 }
703 /* Ignore exceptions. */
704
200bc880 705 return gdb_py_long_from_longest (TYPE_LENGTH (type));
2c74e833
TT
706}
707
708static struct type *
9df2fbc4 709typy_lookup_typename (const char *type_name, const struct block *block)
2c74e833
TT
710{
711 struct type *type = NULL;
712 volatile struct gdb_exception except;
d59b6f6c 713
2c74e833
TT
714 TRY_CATCH (except, RETURN_MASK_ALL)
715 {
716 if (!strncmp (type_name, "struct ", 7))
717 type = lookup_struct (type_name + 7, NULL);
718 else if (!strncmp (type_name, "union ", 6))
719 type = lookup_union (type_name + 6, NULL);
720 else if (!strncmp (type_name, "enum ", 5))
721 type = lookup_enum (type_name + 5, NULL);
722 else
d452c4bc 723 type = lookup_typename (python_language, python_gdbarch,
5107b149 724 type_name, block, 0);
2c74e833 725 }
56cc411c 726 GDB_PY_HANDLE_EXCEPTION (except);
2c74e833
TT
727
728 return type;
729}
730
731static struct type *
5107b149 732typy_lookup_type (struct demangle_component *demangled,
9df2fbc4 733 const struct block *block)
2c74e833 734{
cd829959 735 struct type *type, *rtype = NULL;
76dce0be 736 char *type_name = NULL;
2c74e833 737 enum demangle_component_type demangled_type;
76dce0be 738 volatile struct gdb_exception except;
2c74e833
TT
739
740 /* Save the type: typy_lookup_type() may (indirectly) overwrite
741 memory pointed by demangled. */
742 demangled_type = demangled->type;
743
744 if (demangled_type == DEMANGLE_COMPONENT_POINTER
745 || demangled_type == DEMANGLE_COMPONENT_REFERENCE
746 || demangled_type == DEMANGLE_COMPONENT_CONST
747 || demangled_type == DEMANGLE_COMPONENT_VOLATILE)
748 {
5107b149 749 type = typy_lookup_type (demangled->u.s_binary.left, block);
2c74e833
TT
750 if (! type)
751 return NULL;
752
76dce0be
PM
753 TRY_CATCH (except, RETURN_MASK_ALL)
754 {
cd829959
PM
755 /* If the demangled_type matches with one of the types
756 below, run the corresponding function and save the type
757 to return later. We cannot just return here as we are in
758 an exception handler. */
76dce0be
PM
759 switch (demangled_type)
760 {
761 case DEMANGLE_COMPONENT_REFERENCE:
cd829959
PM
762 rtype = lookup_reference_type (type);
763 break;
76dce0be 764 case DEMANGLE_COMPONENT_POINTER:
cd829959
PM
765 rtype = lookup_pointer_type (type);
766 break;
76dce0be 767 case DEMANGLE_COMPONENT_CONST:
cd829959
PM
768 rtype = make_cv_type (1, 0, type, NULL);
769 break;
76dce0be 770 case DEMANGLE_COMPONENT_VOLATILE:
cd829959
PM
771 rtype = make_cv_type (0, 1, type, NULL);
772 break;
76dce0be 773 }
76dce0be 774 }
56cc411c 775 GDB_PY_HANDLE_EXCEPTION (except);
2c74e833 776 }
256458bc 777
cd829959
PM
778 /* If we have a type from the switch statement above, just return
779 that. */
780 if (rtype)
781 return rtype;
256458bc 782
cd829959
PM
783 /* We don't have a type, so lookup the type. */
784 type_name = cp_comp_to_string (demangled, 10);
5107b149 785 type = typy_lookup_typename (type_name, block);
2c74e833
TT
786 xfree (type_name);
787
788 return type;
789}
790
326fd672
TT
791/* This is a helper function for typy_template_argument that is used
792 when the type does not have template symbols attached. It works by
793 parsing the type name. This happens with compilers, like older
794 versions of GCC, that do not emit DW_TAG_template_*. */
795
2c74e833 796static PyObject *
9df2fbc4 797typy_legacy_template_argument (struct type *type, const struct block *block,
326fd672 798 int argno)
2c74e833 799{
326fd672 800 int i;
2c74e833 801 struct demangle_component *demangled;
5d9c5995 802 struct demangle_parse_info *info = NULL;
2c74e833
TT
803 const char *err;
804 struct type *argtype;
3a93a0c2 805 struct cleanup *cleanup;
5d9c5995 806 volatile struct gdb_exception except;
2c74e833
TT
807
808 if (TYPE_NAME (type) == NULL)
809 {
5107b149 810 PyErr_SetString (PyExc_RuntimeError, _("Null type name."));
2c74e833
TT
811 return NULL;
812 }
813
5d9c5995
PM
814 TRY_CATCH (except, RETURN_MASK_ALL)
815 {
816 /* Note -- this is not thread-safe. */
817 info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
818 }
819 GDB_PY_HANDLE_EXCEPTION (except);
820
3a93a0c2 821 if (! info)
2c74e833
TT
822 {
823 PyErr_SetString (PyExc_RuntimeError, err);
824 return NULL;
825 }
3a93a0c2
KS
826 demangled = info->tree;
827 cleanup = make_cleanup_cp_demangled_name_parse_free (info);
2c74e833
TT
828
829 /* Strip off component names. */
830 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME
831 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME)
832 demangled = demangled->u.s_binary.right;
833
834 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE)
835 {
3a93a0c2 836 do_cleanups (cleanup);
5107b149 837 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template."));
2c74e833
TT
838 return NULL;
839 }
840
841 /* Skip from the template to the arguments. */
842 demangled = demangled->u.s_binary.right;
843
844 for (i = 0; demangled && i < argno; ++i)
845 demangled = demangled->u.s_binary.right;
846
847 if (! demangled)
848 {
3a93a0c2 849 do_cleanups (cleanup);
5107b149 850 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
2c74e833
TT
851 argno);
852 return NULL;
853 }
854
5107b149 855 argtype = typy_lookup_type (demangled->u.s_binary.left, block);
3a93a0c2 856 do_cleanups (cleanup);
2c74e833
TT
857 if (! argtype)
858 return NULL;
859
860 return type_to_type_object (argtype);
326fd672
TT
861}
862
863static PyObject *
864typy_template_argument (PyObject *self, PyObject *args)
865{
866 int argno;
867 struct type *type = ((type_object *) self)->type;
9df2fbc4 868 const struct block *block = NULL;
326fd672
TT
869 PyObject *block_obj = NULL;
870 struct symbol *sym;
871 struct value *val = NULL;
872 volatile struct gdb_exception except;
873
874 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
875 return NULL;
876
877 if (block_obj)
878 {
879 block = block_object_to_block (block_obj);
880 if (! block)
881 {
882 PyErr_SetString (PyExc_RuntimeError,
883 _("Second argument must be block."));
884 return NULL;
885 }
886 }
887
05d0e1e7
TT
888 TRY_CATCH (except, RETURN_MASK_ALL)
889 {
890 type = check_typedef (type);
891 if (TYPE_CODE (type) == TYPE_CODE_REF)
892 type = check_typedef (TYPE_TARGET_TYPE (type));
893 }
894 GDB_PY_HANDLE_EXCEPTION (except);
326fd672
TT
895
896 /* We might not have DW_TAG_template_*, so try to parse the type's
897 name. This is inefficient if we do not have a template type --
898 but that is going to wind up as an error anyhow. */
899 if (! TYPE_N_TEMPLATE_ARGUMENTS (type))
900 return typy_legacy_template_argument (type, block, argno);
901
902 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type))
903 {
904 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."),
905 argno);
906 return NULL;
907 }
908
909 sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
910 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
911 return type_to_type_object (SYMBOL_TYPE (sym));
912 else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT)
913 {
914 PyErr_Format (PyExc_RuntimeError,
915 _("Template argument is optimized out"));
916 return NULL;
917 }
918
919 TRY_CATCH (except, RETURN_MASK_ALL)
920 {
921 val = value_of_variable (sym, block);
922 }
923 GDB_PY_HANDLE_EXCEPTION (except);
924
925 return value_to_value_object (val);
2c74e833
TT
926}
927
928static PyObject *
929typy_str (PyObject *self)
930{
931 volatile struct gdb_exception except;
932 char *thetype = NULL;
759ef836 933 long length = 0;
2c74e833
TT
934 PyObject *result;
935
936 TRY_CATCH (except, RETURN_MASK_ALL)
937 {
938 struct cleanup *old_chain;
939 struct ui_file *stb;
2c74e833
TT
940
941 stb = mem_fileopen ();
942 old_chain = make_cleanup_ui_file_delete (stb);
943
53342f27
TT
944 LA_PRINT_TYPE (type_object_to_type (self), "", stb, -1, 0,
945 &type_print_raw_options);
2c74e833
TT
946
947 thetype = ui_file_xstrdup (stb, &length);
948 do_cleanups (old_chain);
949 }
950 if (except.reason < 0)
951 {
952 xfree (thetype);
953 GDB_PY_HANDLE_EXCEPTION (except);
954 }
955
759ef836 956 result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
2c74e833
TT
957 xfree (thetype);
958
959 return result;
960}
961
d839c8a4
TT
962/* Implement the richcompare method. */
963
964static PyObject *
965typy_richcompare (PyObject *self, PyObject *other, int op)
966{
967 int result = Py_NE;
968 struct type *type1 = type_object_to_type (self);
969 struct type *type2 = type_object_to_type (other);
970 volatile struct gdb_exception except;
971
972 /* We can only compare ourselves to another Type object, and only
973 for equality or inequality. */
974 if (type2 == NULL || (op != Py_EQ && op != Py_NE))
975 {
976 Py_INCREF (Py_NotImplemented);
977 return Py_NotImplemented;
978 }
979
980 if (type1 == type2)
981 result = Py_EQ;
982 else
983 {
d839c8a4
TT
984 TRY_CATCH (except, RETURN_MASK_ALL)
985 {
ca092b61 986 result = types_deeply_equal (type1, type2);
d839c8a4 987 }
ca092b61
DE
988 /* If there is a GDB exception, a comparison is not capable
989 (or trusted), so exit. */
76dce0be 990 GDB_PY_HANDLE_EXCEPTION (except);
d839c8a4
TT
991 }
992
ca092b61 993 if (op == (result ? Py_EQ : Py_NE))
d839c8a4
TT
994 Py_RETURN_TRUE;
995 Py_RETURN_FALSE;
996}
997
2c74e833
TT
998\f
999
1000static const struct objfile_data *typy_objfile_data_key;
1001
1002static void
c1bd65d0 1003save_objfile_types (struct objfile *objfile, void *datum)
2c74e833
TT
1004{
1005 type_object *obj = datum;
1006 htab_t copied_types;
1007 struct cleanup *cleanup;
2c74e833 1008
0646da15
TT
1009 if (!gdb_python_initialized)
1010 return;
1011
2c74e833
TT
1012 /* This prevents another thread from freeing the objects we're
1013 operating on. */
d452c4bc 1014 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
2c74e833
TT
1015
1016 copied_types = create_copied_types_hash (objfile);
1017
1018 while (obj)
1019 {
1020 type_object *next = obj->next;
1021
1022 htab_empty (copied_types);
1023
1024 obj->type = copy_type_recursive (objfile, obj->type, copied_types);
1025
1026 obj->next = NULL;
1027 obj->prev = NULL;
1028
1029 obj = next;
1030 }
1031
1032 htab_delete (copied_types);
1033
1034 do_cleanups (cleanup);
1035}
1036
1037static void
1038set_type (type_object *obj, struct type *type)
1039{
1040 obj->type = type;
1041 obj->prev = NULL;
1042 if (type && TYPE_OBJFILE (type))
1043 {
1044 struct objfile *objfile = TYPE_OBJFILE (type);
1045
1046 obj->next = objfile_data (objfile, typy_objfile_data_key);
1047 if (obj->next)
1048 obj->next->prev = obj;
1049 set_objfile_data (objfile, typy_objfile_data_key, obj);
1050 }
1051 else
1052 obj->next = NULL;
1053}
1054
1055static void
1056typy_dealloc (PyObject *obj)
1057{
1058 type_object *type = (type_object *) obj;
1059
1060 if (type->prev)
1061 type->prev->next = type->next;
1062 else if (type->type && TYPE_OBJFILE (type->type))
1063 {
1064 /* Must reset head of list. */
1065 struct objfile *objfile = TYPE_OBJFILE (type->type);
d59b6f6c 1066
2c74e833
TT
1067 if (objfile)
1068 set_objfile_data (objfile, typy_objfile_data_key, type->next);
1069 }
1070 if (type->next)
1071 type->next->prev = type->prev;
1072
9a27f2c6 1073 Py_TYPE (type)->tp_free (type);
2c74e833
TT
1074}
1075
a73bb892
PK
1076/* Return number of fields ("length" of the field dictionary). */
1077
1078static Py_ssize_t
1079typy_length (PyObject *self)
1080{
1081 struct type *type = ((type_object *) self)->type;
1082
9cc10fd1
PK
1083 type = typy_get_composite (type);
1084 if (type == NULL)
1085 return -1;
1086
a73bb892
PK
1087 return TYPE_NFIELDS (type);
1088}
1089
9cc10fd1 1090/* Implements boolean evaluation of gdb.Type. Handle this like other
256458bc 1091 Python objects that don't have a meaningful truth value -- all
9cc10fd1
PK
1092 values are true. */
1093
1094static int
1095typy_nonzero (PyObject *self)
1096{
1097 return 1;
1098}
1099
a73bb892
PK
1100/* Return a gdb.Field object for the field named by the argument. */
1101
1102static PyObject *
1103typy_getitem (PyObject *self, PyObject *key)
1104{
1105 struct type *type = ((type_object *) self)->type;
1106 char *field;
1107 int i;
76dce0be 1108
a73bb892
PK
1109 field = python_string_to_host_string (key);
1110 if (field == NULL)
1111 return NULL;
1112
256458bc 1113 /* We want just fields of this type, not of base types, so instead of
a73bb892
PK
1114 using lookup_struct_elt_type, portions of that function are
1115 copied here. */
1116
9cc10fd1
PK
1117 type = typy_get_composite (type);
1118 if (type == NULL)
1119 return NULL;
256458bc 1120
a73bb892
PK
1121 for (i = 0; i < TYPE_NFIELDS (type); i++)
1122 {
0d5cff50 1123 const char *t_field_name = TYPE_FIELD_NAME (type, i);
a73bb892
PK
1124
1125 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1126 {
1127 return convert_field (type, i);
1128 }
1129 }
1130 PyErr_SetObject (PyExc_KeyError, key);
1131 return NULL;
1132}
1133
256458bc 1134/* Implement the "get" method on the type object. This is the
a73bb892
PK
1135 same as getitem if the key is present, but returns the supplied
1136 default value or None if the key is not found. */
1137
1138static PyObject *
1139typy_get (PyObject *self, PyObject *args)
1140{
1141 PyObject *key, *defval = Py_None, *result;
256458bc 1142
a73bb892
PK
1143 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval))
1144 return NULL;
256458bc 1145
a73bb892
PK
1146 result = typy_getitem (self, key);
1147 if (result != NULL)
1148 return result;
256458bc 1149
a73bb892
PK
1150 /* typy_getitem returned error status. If the exception is
1151 KeyError, clear the exception status and return the defval
1152 instead. Otherwise return the exception unchanged. */
1153 if (!PyErr_ExceptionMatches (PyExc_KeyError))
1154 return NULL;
256458bc 1155
a73bb892
PK
1156 PyErr_Clear ();
1157 Py_INCREF (defval);
1158 return defval;
1159}
1160
1161/* Implement the "has_key" method on the type object. */
1162
1163static PyObject *
1164typy_has_key (PyObject *self, PyObject *args)
1165{
1166 struct type *type = ((type_object *) self)->type;
2ff6b080 1167 const char *field;
a73bb892 1168 int i;
76dce0be 1169
a73bb892
PK
1170 if (!PyArg_ParseTuple (args, "s", &field))
1171 return NULL;
1172
256458bc 1173 /* We want just fields of this type, not of base types, so instead of
a73bb892
PK
1174 using lookup_struct_elt_type, portions of that function are
1175 copied here. */
1176
9cc10fd1
PK
1177 type = typy_get_composite (type);
1178 if (type == NULL)
1179 return NULL;
a73bb892
PK
1180
1181 for (i = 0; i < TYPE_NFIELDS (type); i++)
1182 {
0d5cff50 1183 const char *t_field_name = TYPE_FIELD_NAME (type, i);
a73bb892
PK
1184
1185 if (t_field_name && (strcmp_iw (t_field_name, field) == 0))
1186 Py_RETURN_TRUE;
1187 }
1188 Py_RETURN_FALSE;
1189}
1190
1191/* Make an iterator object to iterate over keys, values, or items. */
1192
1193static PyObject *
1194typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind)
1195{
1196 typy_iterator_object *typy_iter_obj;
1197
9cc10fd1
PK
1198 /* Check that "self" is a structure or union type. */
1199 if (typy_get_composite (((type_object *) self)->type) == NULL)
1200 return NULL;
256458bc 1201
a73bb892
PK
1202 typy_iter_obj = PyObject_New (typy_iterator_object,
1203 &type_iterator_object_type);
1204 if (typy_iter_obj == NULL)
1205 return NULL;
1206
1207 typy_iter_obj->field = 0;
1208 typy_iter_obj->kind = kind;
1209 Py_INCREF (self);
1210 typy_iter_obj->source = (type_object *) self;
1211
1212 return (PyObject *) typy_iter_obj;
1213}
1214
1215/* iteritems() method. */
1216
1217static PyObject *
1218typy_iteritems (PyObject *self, PyObject *args)
1219{
1220 return typy_make_iter (self, iter_items);
1221}
1222
1223/* iterkeys() method. */
1224
1225static PyObject *
1226typy_iterkeys (PyObject *self, PyObject *args)
1227{
1228 return typy_make_iter (self, iter_keys);
1229}
1230
1231/* Iterating over the class, same as iterkeys except for the function
1232 signature. */
1233
1234static PyObject *
1235typy_iter (PyObject *self)
1236{
1237 return typy_make_iter (self, iter_keys);
1238}
1239
1240/* itervalues() method. */
1241
1242static PyObject *
1243typy_itervalues (PyObject *self, PyObject *args)
1244{
1245 return typy_make_iter (self, iter_values);
1246}
1247
1248/* Return a reference to the type iterator. */
1249
1250static PyObject *
1251typy_iterator_iter (PyObject *self)
1252{
1253 Py_INCREF (self);
1254 return self;
1255}
1256
1257/* Return the next field in the iteration through the list of fields
1258 of the type. */
1259
1260static PyObject *
1261typy_iterator_iternext (PyObject *self)
1262{
1263 typy_iterator_object *iter_obj = (typy_iterator_object *) self;
1264 struct type *type = iter_obj->source->type;
a73bb892 1265 PyObject *result;
256458bc 1266
a73bb892
PK
1267 if (iter_obj->field < TYPE_NFIELDS (type))
1268 {
1269 result = make_fielditem (type, iter_obj->field, iter_obj->kind);
1270 if (result != NULL)
1271 iter_obj->field++;
1272 return result;
1273 }
1274
1275 return NULL;
1276}
1277
1278static void
1279typy_iterator_dealloc (PyObject *obj)
1280{
1281 typy_iterator_object *iter_obj = (typy_iterator_object *) obj;
1282
1283 Py_DECREF (iter_obj->source);
1284}
1285
2c74e833
TT
1286/* Create a new Type referring to TYPE. */
1287PyObject *
1288type_to_type_object (struct type *type)
1289{
1290 type_object *type_obj;
1291
1292 type_obj = PyObject_New (type_object, &type_object_type);
1293 if (type_obj)
1294 set_type (type_obj, type);
1295
1296 return (PyObject *) type_obj;
1297}
1298
1299struct type *
1300type_object_to_type (PyObject *obj)
1301{
1302 if (! PyObject_TypeCheck (obj, &type_object_type))
1303 return NULL;
1304 return ((type_object *) obj)->type;
1305}
1306
1307\f
1308
1309/* Implementation of gdb.lookup_type. */
1310PyObject *
1311gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
1312{
5107b149 1313 static char *keywords[] = { "name", "block", NULL };
ddd49eee 1314 const char *type_name = NULL;
2c74e833 1315 struct type *type = NULL;
5107b149 1316 PyObject *block_obj = NULL;
9df2fbc4 1317 const struct block *block = NULL;
2c74e833 1318
5107b149
PM
1319 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords,
1320 &type_name, &block_obj))
2c74e833
TT
1321 return NULL;
1322
5107b149
PM
1323 if (block_obj)
1324 {
1325 block = block_object_to_block (block_obj);
1326 if (! block)
1327 {
1328 PyErr_SetString (PyExc_RuntimeError,
1329 _("'block' argument must be a Block."));
1330 return NULL;
1331 }
1332 }
1333
1334 type = typy_lookup_typename (type_name, block);
2c74e833
TT
1335 if (! type)
1336 return NULL;
1337
1338 return (PyObject *) type_to_type_object (type);
1339}
1340
999633ed 1341int
2c74e833
TT
1342gdbpy_initialize_types (void)
1343{
1344 int i;
1345
1346 typy_objfile_data_key
c1bd65d0 1347 = register_objfile_data_with_cleanup (save_objfile_types, NULL);
2c74e833
TT
1348
1349 if (PyType_Ready (&type_object_type) < 0)
999633ed 1350 return -1;
2c74e833 1351 if (PyType_Ready (&field_object_type) < 0)
999633ed 1352 return -1;
a73bb892 1353 if (PyType_Ready (&type_iterator_object_type) < 0)
999633ed 1354 return -1;
2c74e833
TT
1355
1356 for (i = 0; pyty_codes[i].name; ++i)
1357 {
1358 if (PyModule_AddIntConstant (gdb_module,
1359 /* Cast needed for Python 2.4. */
1360 (char *) pyty_codes[i].name,
1361 pyty_codes[i].code) < 0)
999633ed 1362 return -1;
2c74e833
TT
1363 }
1364
aa36459a
TT
1365 if (gdb_pymodule_addobject (gdb_module, "Type",
1366 (PyObject *) &type_object_type) < 0)
999633ed 1367 return -1;
2c74e833 1368
aa36459a
TT
1369 if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
1370 (PyObject *) &type_iterator_object_type) < 0)
999633ed 1371 return -1;
a73bb892 1372
aa36459a
TT
1373 return gdb_pymodule_addobject (gdb_module, "Field",
1374 (PyObject *) &field_object_type);
2c74e833
TT
1375}
1376
1377\f
1378
1379static PyGetSetDef type_object_getset[] =
1380{
1381 { "code", typy_get_code, NULL,
1382 "The code for this type.", NULL },
1383 { "sizeof", typy_get_sizeof, NULL,
1384 "The size of this type, in bytes.", NULL },
1385 { "tag", typy_get_tag, NULL,
1386 "The tag name for this type, or None.", NULL },
1387 { NULL }
1388};
1389
1390static PyMethodDef type_object_methods[] =
1391{
702c2711 1392 { "array", typy_array, METH_VARARGS,
f28c316a
DE
1393 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1394Return a type which represents an array of objects of this type.\n\
1395The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1396If LOW_BOUND is omitted, a value of zero is used." },
a72c3253
DE
1397 { "vector", typy_vector, METH_VARARGS,
1398 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
1399Return a type which represents a vector of objects of this type.\n\
1400The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
1401If LOW_BOUND is omitted, a value of zero is used.\n\
1402Vectors differ from arrays in that if the current language has C-style\n\
1403arrays, vectors don't decay to a pointer to the first element.\n\
1404They are first class values." },
a73bb892
PK
1405 { "__contains__", typy_has_key, METH_VARARGS,
1406 "T.__contains__(k) -> True if T has a field named k, else False" },
2c74e833
TT
1407 { "const", typy_const, METH_NOARGS,
1408 "const () -> Type\n\
1409Return a const variant of this type." },
1410 { "fields", typy_fields, METH_NOARGS,
a73bb892
PK
1411 "fields () -> list\n\
1412Return a list holding all the fields of this type.\n\
1413Each field is a gdb.Field object." },
1414 { "get", typy_get, METH_VARARGS,
1415 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\
1416otherwise returns default, if supplied, or None if not." },
1417 { "has_key", typy_has_key, METH_VARARGS,
1418 "T.has_key(k) -> True if T has a field named k, else False" },
1419 { "items", typy_items, METH_NOARGS,
1420 "items () -> list\n\
1421Return a list of (name, field) pairs of this type.\n\
1422Each field is a gdb.Field object." },
1423 { "iteritems", typy_iteritems, METH_NOARGS,
1424 "iteritems () -> an iterator over the (name, field)\n\
1425pairs of this type. Each field is a gdb.Field object." },
1426 { "iterkeys", typy_iterkeys, METH_NOARGS,
1427 "iterkeys () -> an iterator over the field names of this type." },
1428 { "itervalues", typy_itervalues, METH_NOARGS,
1429 "itervalues () -> an iterator over the fields of this type.\n\
1430Each field is a gdb.Field object." },
1431 { "keys", typy_field_names, METH_NOARGS,
1432 "keys () -> list\n\
1433Return a list holding all the fields names of this type." },
2c74e833
TT
1434 { "pointer", typy_pointer, METH_NOARGS,
1435 "pointer () -> Type\n\
1436Return a type of pointer to this type." },
361ae042
PM
1437 { "range", typy_range, METH_NOARGS,
1438 "range () -> tuple\n\
1439Return a tuple containing the lower and upper range for this type."},
2c74e833
TT
1440 { "reference", typy_reference, METH_NOARGS,
1441 "reference () -> Type\n\
1442Return a type of reference to this type." },
1443 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS,
1444 "strip_typedefs () -> Type\n\
1445Return a type formed by stripping this type of all typedefs."},
1446 { "target", typy_target, METH_NOARGS,
1447 "target () -> Type\n\
1448Return the target type of this type." },
1449 { "template_argument", typy_template_argument, METH_VARARGS,
5107b149 1450 "template_argument (arg, [block]) -> Type\n\
2c74e833
TT
1451Return the type of a template argument." },
1452 { "unqualified", typy_unqualified, METH_NOARGS,
1453 "unqualified () -> Type\n\
1454Return a variant of this type without const or volatile attributes." },
9cc10fd1 1455 { "values", typy_values, METH_NOARGS,
a73bb892
PK
1456 "values () -> list\n\
1457Return a list holding all the fields of this type.\n\
1458Each field is a gdb.Field object." },
2c74e833
TT
1459 { "volatile", typy_volatile, METH_NOARGS,
1460 "volatile () -> Type\n\
1461Return a volatile variant of this type" },
1462 { NULL }
1463};
1464
9cc10fd1
PK
1465static PyNumberMethods type_object_as_number = {
1466 NULL, /* nb_add */
1467 NULL, /* nb_subtract */
1468 NULL, /* nb_multiply */
9a27f2c6 1469#ifndef IS_PY3K
9cc10fd1 1470 NULL, /* nb_divide */
9a27f2c6 1471#endif
9cc10fd1
PK
1472 NULL, /* nb_remainder */
1473 NULL, /* nb_divmod */
1474 NULL, /* nb_power */
1475 NULL, /* nb_negative */
1476 NULL, /* nb_positive */
1477 NULL, /* nb_absolute */
1478 typy_nonzero, /* nb_nonzero */
1479 NULL, /* nb_invert */
1480 NULL, /* nb_lshift */
1481 NULL, /* nb_rshift */
1482 NULL, /* nb_and */
1483 NULL, /* nb_xor */
1484 NULL, /* nb_or */
9a27f2c6
PK
1485#ifdef IS_PY3K
1486 NULL, /* nb_int */
1487 NULL, /* reserved */
1488#else
9cc10fd1
PK
1489 NULL, /* nb_coerce */
1490 NULL, /* nb_int */
1491 NULL, /* nb_long */
9a27f2c6 1492#endif
9cc10fd1 1493 NULL, /* nb_float */
9a27f2c6 1494#ifndef IS_PY3K
9cc10fd1
PK
1495 NULL, /* nb_oct */
1496 NULL /* nb_hex */
9a27f2c6 1497#endif
9cc10fd1
PK
1498};
1499
a73bb892
PK
1500static PyMappingMethods typy_mapping = {
1501 typy_length,
1502 typy_getitem,
1503 NULL /* no "set" method */
1504};
1505
2c74e833
TT
1506static PyTypeObject type_object_type =
1507{
9a27f2c6 1508 PyVarObject_HEAD_INIT (NULL, 0)
2c74e833
TT
1509 "gdb.Type", /*tp_name*/
1510 sizeof (type_object), /*tp_basicsize*/
1511 0, /*tp_itemsize*/
1512 typy_dealloc, /*tp_dealloc*/
1513 0, /*tp_print*/
1514 0, /*tp_getattr*/
1515 0, /*tp_setattr*/
1516 0, /*tp_compare*/
1517 0, /*tp_repr*/
9cc10fd1 1518 &type_object_as_number, /*tp_as_number*/
2c74e833 1519 0, /*tp_as_sequence*/
a73bb892 1520 &typy_mapping, /*tp_as_mapping*/
2c74e833
TT
1521 0, /*tp_hash */
1522 0, /*tp_call*/
1523 typy_str, /*tp_str*/
1524 0, /*tp_getattro*/
1525 0, /*tp_setattro*/
1526 0, /*tp_as_buffer*/
1527 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1528 "GDB type object", /* tp_doc */
1529 0, /* tp_traverse */
1530 0, /* tp_clear */
d839c8a4 1531 typy_richcompare, /* tp_richcompare */
2c74e833 1532 0, /* tp_weaklistoffset */
a73bb892 1533 typy_iter, /* tp_iter */
2c74e833
TT
1534 0, /* tp_iternext */
1535 type_object_methods, /* tp_methods */
1536 0, /* tp_members */
1537 type_object_getset, /* tp_getset */
1538 0, /* tp_base */
1539 0, /* tp_dict */
1540 0, /* tp_descr_get */
1541 0, /* tp_descr_set */
1542 0, /* tp_dictoffset */
1543 0, /* tp_init */
1544 0, /* tp_alloc */
1545 0, /* tp_new */
1546};
1547
2e8265fd
TT
1548static PyGetSetDef field_object_getset[] =
1549{
1550 { "__dict__", gdb_py_generic_dict, NULL,
1551 "The __dict__ for this field.", &field_object_type },
1552 { NULL }
1553};
1554
2c74e833
TT
1555static PyTypeObject field_object_type =
1556{
9a27f2c6 1557 PyVarObject_HEAD_INIT (NULL, 0)
2c74e833
TT
1558 "gdb.Field", /*tp_name*/
1559 sizeof (field_object), /*tp_basicsize*/
1560 0, /*tp_itemsize*/
1561 field_dealloc, /*tp_dealloc*/
1562 0, /*tp_print*/
1563 0, /*tp_getattr*/
1564 0, /*tp_setattr*/
1565 0, /*tp_compare*/
1566 0, /*tp_repr*/
1567 0, /*tp_as_number*/
1568 0, /*tp_as_sequence*/
1569 0, /*tp_as_mapping*/
1570 0, /*tp_hash */
1571 0, /*tp_call*/
1572 0, /*tp_str*/
1573 0, /*tp_getattro*/
1574 0, /*tp_setattro*/
1575 0, /*tp_as_buffer*/
1576 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1577 "GDB field object", /* tp_doc */
1578 0, /* tp_traverse */
1579 0, /* tp_clear */
1580 0, /* tp_richcompare */
1581 0, /* tp_weaklistoffset */
1582 0, /* tp_iter */
1583 0, /* tp_iternext */
1584 0, /* tp_methods */
1585 0, /* tp_members */
2e8265fd 1586 field_object_getset, /* tp_getset */
2c74e833
TT
1587 0, /* tp_base */
1588 0, /* tp_dict */
1589 0, /* tp_descr_get */
1590 0, /* tp_descr_set */
1591 offsetof (field_object, dict), /* tp_dictoffset */
1592 0, /* tp_init */
1593 0, /* tp_alloc */
1594 0, /* tp_new */
1595};
a73bb892
PK
1596
1597static PyTypeObject type_iterator_object_type = {
9a27f2c6 1598 PyVarObject_HEAD_INIT (NULL, 0)
a73bb892
PK
1599 "gdb.TypeIterator", /*tp_name*/
1600 sizeof (typy_iterator_object), /*tp_basicsize*/
1601 0, /*tp_itemsize*/
1602 typy_iterator_dealloc, /*tp_dealloc*/
1603 0, /*tp_print*/
1604 0, /*tp_getattr*/
1605 0, /*tp_setattr*/
1606 0, /*tp_compare*/
1607 0, /*tp_repr*/
1608 0, /*tp_as_number*/
1609 0, /*tp_as_sequence*/
1610 0, /*tp_as_mapping*/
1611 0, /*tp_hash */
1612 0, /*tp_call*/
1613 0, /*tp_str*/
1614 0, /*tp_getattro*/
1615 0, /*tp_setattro*/
1616 0, /*tp_as_buffer*/
1617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
1618 "GDB type iterator object", /*tp_doc */
1619 0, /*tp_traverse */
1620 0, /*tp_clear */
1621 0, /*tp_richcompare */
1622 0, /*tp_weaklistoffset */
1623 typy_iterator_iter, /*tp_iter */
1624 typy_iterator_iternext, /*tp_iternext */
1625 0 /*tp_methods */
1626};
This page took 0.55077 seconds and 4 git commands to generate.