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