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