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