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