]>
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; | |
219 | ||
220 | /* We would like to make a tuple here, make fields immutable, and | |
221 | then memoize the result (and perhaps make Field.type() lazy). | |
222 | However, that can lead to cycles. */ | |
223 | result = PyList_New (0); | |
224 | ||
225 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
226 | { | |
227 | PyObject *dict = convert_field (type, i); | |
d59b6f6c | 228 | |
2c74e833 TT |
229 | if (!dict) |
230 | { | |
231 | Py_DECREF (result); | |
232 | return NULL; | |
233 | } | |
234 | if (PyList_Append (result, dict)) | |
235 | { | |
236 | Py_DECREF (dict); | |
237 | Py_DECREF (result); | |
238 | return NULL; | |
239 | } | |
240 | } | |
241 | ||
242 | return result; | |
243 | } | |
244 | ||
245 | /* Return the type's tag, or None. */ | |
246 | static PyObject * | |
247 | typy_get_tag (PyObject *self, void *closure) | |
248 | { | |
249 | struct type *type = ((type_object *) self)->type; | |
d59b6f6c | 250 | |
2c74e833 TT |
251 | if (!TYPE_TAG_NAME (type)) |
252 | Py_RETURN_NONE; | |
253 | return PyString_FromString (TYPE_TAG_NAME (type)); | |
254 | } | |
255 | ||
256 | /* Return the type, stripped of typedefs. */ | |
257 | static PyObject * | |
258 | typy_strip_typedefs (PyObject *self, PyObject *args) | |
259 | { | |
260 | struct type *type = ((type_object *) self)->type; | |
261 | ||
262 | return type_to_type_object (check_typedef (type)); | |
263 | } | |
264 | ||
265 | /* Return a Type object which represents a pointer to SELF. */ | |
266 | static PyObject * | |
267 | typy_pointer (PyObject *self, PyObject *args) | |
268 | { | |
269 | struct type *type = ((type_object *) self)->type; | |
270 | volatile struct gdb_exception except; | |
271 | ||
272 | TRY_CATCH (except, RETURN_MASK_ALL) | |
273 | { | |
274 | type = lookup_pointer_type (type); | |
275 | } | |
276 | GDB_PY_HANDLE_EXCEPTION (except); | |
277 | ||
278 | return type_to_type_object (type); | |
279 | } | |
280 | ||
361ae042 PM |
281 | /* Return the range of a type represented by SELF. The return type is |
282 | a tuple. The first element of the tuple contains the low bound, | |
283 | while the second element of the tuple contains the high bound. */ | |
284 | static PyObject * | |
285 | typy_range (PyObject *self, PyObject *args) | |
286 | { | |
287 | struct type *type = ((type_object *) self)->type; | |
288 | PyObject *result; | |
289 | PyObject *low_bound = NULL, *high_bound = NULL; | |
8d099ae9 PM |
290 | /* Initialize these to appease GCC warnings. */ |
291 | LONGEST low = 0, high = 0; | |
361ae042 PM |
292 | |
293 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
294 | && TYPE_CODE (type) != TYPE_CODE_STRING | |
295 | && TYPE_CODE (type) != TYPE_CODE_RANGE) | |
296 | { | |
297 | PyErr_SetString (PyExc_RuntimeError, | |
044c0f87 | 298 | _("This type does not have a range.")); |
361ae042 PM |
299 | return NULL; |
300 | } | |
301 | ||
302 | switch (TYPE_CODE (type)) | |
303 | { | |
304 | case TYPE_CODE_ARRAY: | |
305 | case TYPE_CODE_STRING: | |
306 | low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); | |
307 | high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)); | |
308 | break; | |
309 | case TYPE_CODE_RANGE: | |
310 | low = TYPE_LOW_BOUND (type); | |
311 | high = TYPE_HIGH_BOUND (type); | |
312 | break; | |
313 | } | |
314 | ||
315 | low_bound = PyLong_FromLong (low); | |
316 | if (!low_bound) | |
317 | goto failarg; | |
318 | ||
319 | high_bound = PyLong_FromLong (high); | |
320 | if (!high_bound) | |
321 | goto failarg; | |
322 | ||
323 | result = PyTuple_New (2); | |
324 | if (!result) | |
325 | goto failarg; | |
326 | ||
327 | if (PyTuple_SetItem (result, 0, low_bound) != 0) | |
328 | { | |
329 | Py_DECREF (result); | |
330 | goto failarg; | |
331 | } | |
332 | if (PyTuple_SetItem (result, 1, high_bound) != 0) | |
333 | { | |
334 | Py_DECREF (high_bound); | |
335 | Py_DECREF (result); | |
336 | return NULL; | |
337 | } | |
338 | return result; | |
339 | ||
340 | failarg: | |
341 | Py_XDECREF (high_bound); | |
342 | Py_XDECREF (low_bound); | |
343 | return NULL; | |
344 | } | |
345 | ||
2c74e833 TT |
346 | /* Return a Type object which represents a reference to SELF. */ |
347 | static PyObject * | |
348 | typy_reference (PyObject *self, PyObject *args) | |
349 | { | |
350 | struct type *type = ((type_object *) self)->type; | |
351 | volatile struct gdb_exception except; | |
352 | ||
353 | TRY_CATCH (except, RETURN_MASK_ALL) | |
354 | { | |
355 | type = lookup_reference_type (type); | |
356 | } | |
357 | GDB_PY_HANDLE_EXCEPTION (except); | |
358 | ||
359 | return type_to_type_object (type); | |
360 | } | |
361 | ||
362 | /* Return a Type object which represents the target type of SELF. */ | |
363 | static PyObject * | |
364 | typy_target (PyObject *self, PyObject *args) | |
365 | { | |
366 | struct type *type = ((type_object *) self)->type; | |
367 | ||
368 | if (!TYPE_TARGET_TYPE (type)) | |
369 | { | |
044c0f87 PM |
370 | PyErr_SetString (PyExc_RuntimeError, |
371 | _("Type does not have a target.")); | |
2c74e833 TT |
372 | return NULL; |
373 | } | |
374 | ||
375 | return type_to_type_object (TYPE_TARGET_TYPE (type)); | |
376 | } | |
377 | ||
378 | /* Return a const-qualified type variant. */ | |
379 | static PyObject * | |
380 | typy_const (PyObject *self, PyObject *args) | |
381 | { | |
382 | struct type *type = ((type_object *) self)->type; | |
383 | volatile struct gdb_exception except; | |
384 | ||
385 | TRY_CATCH (except, RETURN_MASK_ALL) | |
386 | { | |
387 | type = make_cv_type (1, 0, type, NULL); | |
388 | } | |
389 | GDB_PY_HANDLE_EXCEPTION (except); | |
390 | ||
391 | return type_to_type_object (type); | |
392 | } | |
393 | ||
394 | /* Return a volatile-qualified type variant. */ | |
395 | static PyObject * | |
396 | typy_volatile (PyObject *self, PyObject *args) | |
397 | { | |
398 | struct type *type = ((type_object *) self)->type; | |
399 | volatile struct gdb_exception except; | |
400 | ||
401 | TRY_CATCH (except, RETURN_MASK_ALL) | |
402 | { | |
403 | type = make_cv_type (0, 1, type, NULL); | |
404 | } | |
405 | GDB_PY_HANDLE_EXCEPTION (except); | |
406 | ||
407 | return type_to_type_object (type); | |
408 | } | |
409 | ||
410 | /* Return an unqualified type variant. */ | |
411 | static PyObject * | |
412 | typy_unqualified (PyObject *self, PyObject *args) | |
413 | { | |
414 | struct type *type = ((type_object *) self)->type; | |
415 | volatile struct gdb_exception except; | |
416 | ||
417 | TRY_CATCH (except, RETURN_MASK_ALL) | |
418 | { | |
419 | type = make_cv_type (0, 0, type, NULL); | |
420 | } | |
421 | GDB_PY_HANDLE_EXCEPTION (except); | |
422 | ||
423 | return type_to_type_object (type); | |
424 | } | |
425 | ||
426 | /* Return the size of the type represented by SELF, in bytes. */ | |
427 | static PyObject * | |
428 | typy_get_sizeof (PyObject *self, void *closure) | |
429 | { | |
430 | struct type *type = ((type_object *) self)->type; | |
431 | volatile struct gdb_exception except; | |
432 | ||
433 | TRY_CATCH (except, RETURN_MASK_ALL) | |
434 | { | |
435 | check_typedef (type); | |
436 | } | |
437 | /* Ignore exceptions. */ | |
438 | ||
439 | return PyLong_FromLong (TYPE_LENGTH (type)); | |
440 | } | |
441 | ||
442 | static struct type * | |
5107b149 | 443 | typy_lookup_typename (char *type_name, struct block *block) |
2c74e833 TT |
444 | { |
445 | struct type *type = NULL; | |
446 | volatile struct gdb_exception except; | |
d59b6f6c | 447 | |
2c74e833 TT |
448 | TRY_CATCH (except, RETURN_MASK_ALL) |
449 | { | |
450 | if (!strncmp (type_name, "struct ", 7)) | |
451 | type = lookup_struct (type_name + 7, NULL); | |
452 | else if (!strncmp (type_name, "union ", 6)) | |
453 | type = lookup_union (type_name + 6, NULL); | |
454 | else if (!strncmp (type_name, "enum ", 5)) | |
455 | type = lookup_enum (type_name + 5, NULL); | |
456 | else | |
d452c4bc | 457 | type = lookup_typename (python_language, python_gdbarch, |
5107b149 | 458 | type_name, block, 0); |
2c74e833 TT |
459 | } |
460 | if (except.reason < 0) | |
461 | { | |
462 | PyErr_Format (except.reason == RETURN_QUIT | |
463 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
464 | "%s", except.message); | |
465 | return NULL; | |
466 | } | |
467 | ||
468 | return type; | |
469 | } | |
470 | ||
471 | static struct type * | |
5107b149 PM |
472 | typy_lookup_type (struct demangle_component *demangled, |
473 | struct block *block) | |
2c74e833 TT |
474 | { |
475 | struct type *type; | |
476 | char *type_name; | |
477 | enum demangle_component_type demangled_type; | |
478 | ||
479 | /* Save the type: typy_lookup_type() may (indirectly) overwrite | |
480 | memory pointed by demangled. */ | |
481 | demangled_type = demangled->type; | |
482 | ||
483 | if (demangled_type == DEMANGLE_COMPONENT_POINTER | |
484 | || demangled_type == DEMANGLE_COMPONENT_REFERENCE | |
485 | || demangled_type == DEMANGLE_COMPONENT_CONST | |
486 | || demangled_type == DEMANGLE_COMPONENT_VOLATILE) | |
487 | { | |
5107b149 | 488 | type = typy_lookup_type (demangled->u.s_binary.left, block); |
2c74e833 TT |
489 | if (! type) |
490 | return NULL; | |
491 | ||
492 | switch (demangled_type) | |
493 | { | |
494 | case DEMANGLE_COMPONENT_REFERENCE: | |
495 | return lookup_reference_type (type); | |
496 | case DEMANGLE_COMPONENT_POINTER: | |
497 | return lookup_pointer_type (type); | |
498 | case DEMANGLE_COMPONENT_CONST: | |
499 | return make_cv_type (1, 0, type, NULL); | |
500 | case DEMANGLE_COMPONENT_VOLATILE: | |
501 | return make_cv_type (0, 1, type, NULL); | |
502 | } | |
503 | } | |
504 | ||
505 | type_name = cp_comp_to_string (demangled, 10); | |
5107b149 | 506 | type = typy_lookup_typename (type_name, block); |
2c74e833 TT |
507 | xfree (type_name); |
508 | ||
509 | return type; | |
510 | } | |
511 | ||
326fd672 TT |
512 | /* This is a helper function for typy_template_argument that is used |
513 | when the type does not have template symbols attached. It works by | |
514 | parsing the type name. This happens with compilers, like older | |
515 | versions of GCC, that do not emit DW_TAG_template_*. */ | |
516 | ||
2c74e833 | 517 | static PyObject * |
326fd672 TT |
518 | typy_legacy_template_argument (struct type *type, struct block *block, |
519 | int argno) | |
2c74e833 | 520 | { |
326fd672 | 521 | int i; |
2c74e833 TT |
522 | struct demangle_component *demangled; |
523 | const char *err; | |
524 | struct type *argtype; | |
2c74e833 TT |
525 | |
526 | if (TYPE_NAME (type) == NULL) | |
527 | { | |
5107b149 | 528 | PyErr_SetString (PyExc_RuntimeError, _("Null type name.")); |
2c74e833 TT |
529 | return NULL; |
530 | } | |
531 | ||
532 | /* Note -- this is not thread-safe. */ | |
533 | demangled = cp_demangled_name_to_comp (TYPE_NAME (type), &err); | |
534 | if (! demangled) | |
535 | { | |
536 | PyErr_SetString (PyExc_RuntimeError, err); | |
537 | return NULL; | |
538 | } | |
539 | ||
540 | /* Strip off component names. */ | |
541 | while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME | |
542 | || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
543 | demangled = demangled->u.s_binary.right; | |
544 | ||
545 | if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) | |
546 | { | |
5107b149 | 547 | PyErr_SetString (PyExc_RuntimeError, _("Type is not a template.")); |
2c74e833 TT |
548 | return NULL; |
549 | } | |
550 | ||
551 | /* Skip from the template to the arguments. */ | |
552 | demangled = demangled->u.s_binary.right; | |
553 | ||
554 | for (i = 0; demangled && i < argno; ++i) | |
555 | demangled = demangled->u.s_binary.right; | |
556 | ||
557 | if (! demangled) | |
558 | { | |
5107b149 | 559 | PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), |
2c74e833 TT |
560 | argno); |
561 | return NULL; | |
562 | } | |
563 | ||
5107b149 | 564 | argtype = typy_lookup_type (demangled->u.s_binary.left, block); |
2c74e833 TT |
565 | if (! argtype) |
566 | return NULL; | |
567 | ||
568 | return type_to_type_object (argtype); | |
326fd672 TT |
569 | } |
570 | ||
571 | static PyObject * | |
572 | typy_template_argument (PyObject *self, PyObject *args) | |
573 | { | |
574 | int argno; | |
575 | struct type *type = ((type_object *) self)->type; | |
576 | struct block *block = NULL; | |
577 | PyObject *block_obj = NULL; | |
578 | struct symbol *sym; | |
579 | struct value *val = NULL; | |
580 | volatile struct gdb_exception except; | |
581 | ||
582 | if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj)) | |
583 | return NULL; | |
584 | ||
585 | if (block_obj) | |
586 | { | |
587 | block = block_object_to_block (block_obj); | |
588 | if (! block) | |
589 | { | |
590 | PyErr_SetString (PyExc_RuntimeError, | |
591 | _("Second argument must be block.")); | |
592 | return NULL; | |
593 | } | |
594 | } | |
595 | ||
596 | type = check_typedef (type); | |
597 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
598 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
599 | ||
600 | /* We might not have DW_TAG_template_*, so try to parse the type's | |
601 | name. This is inefficient if we do not have a template type -- | |
602 | but that is going to wind up as an error anyhow. */ | |
603 | if (! TYPE_N_TEMPLATE_ARGUMENTS (type)) | |
604 | return typy_legacy_template_argument (type, block, argno); | |
605 | ||
606 | if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type)) | |
607 | { | |
608 | PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), | |
609 | argno); | |
610 | return NULL; | |
611 | } | |
612 | ||
613 | sym = TYPE_TEMPLATE_ARGUMENT (type, argno); | |
614 | if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
615 | return type_to_type_object (SYMBOL_TYPE (sym)); | |
616 | else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT) | |
617 | { | |
618 | PyErr_Format (PyExc_RuntimeError, | |
619 | _("Template argument is optimized out")); | |
620 | return NULL; | |
621 | } | |
622 | ||
623 | TRY_CATCH (except, RETURN_MASK_ALL) | |
624 | { | |
625 | val = value_of_variable (sym, block); | |
626 | } | |
627 | GDB_PY_HANDLE_EXCEPTION (except); | |
628 | ||
629 | return value_to_value_object (val); | |
2c74e833 TT |
630 | } |
631 | ||
632 | static PyObject * | |
633 | typy_str (PyObject *self) | |
634 | { | |
635 | volatile struct gdb_exception except; | |
636 | char *thetype = NULL; | |
759ef836 | 637 | long length = 0; |
2c74e833 TT |
638 | PyObject *result; |
639 | ||
640 | TRY_CATCH (except, RETURN_MASK_ALL) | |
641 | { | |
642 | struct cleanup *old_chain; | |
643 | struct ui_file *stb; | |
2c74e833 TT |
644 | |
645 | stb = mem_fileopen (); | |
646 | old_chain = make_cleanup_ui_file_delete (stb); | |
647 | ||
648 | type_print (type_object_to_type (self), "", stb, -1); | |
649 | ||
650 | thetype = ui_file_xstrdup (stb, &length); | |
651 | do_cleanups (old_chain); | |
652 | } | |
653 | if (except.reason < 0) | |
654 | { | |
655 | xfree (thetype); | |
656 | GDB_PY_HANDLE_EXCEPTION (except); | |
657 | } | |
658 | ||
759ef836 | 659 | result = PyUnicode_Decode (thetype, length, host_charset (), NULL); |
2c74e833 TT |
660 | xfree (thetype); |
661 | ||
662 | return result; | |
663 | } | |
664 | ||
665 | \f | |
666 | ||
667 | static const struct objfile_data *typy_objfile_data_key; | |
668 | ||
669 | static void | |
c1bd65d0 | 670 | save_objfile_types (struct objfile *objfile, void *datum) |
2c74e833 TT |
671 | { |
672 | type_object *obj = datum; | |
673 | htab_t copied_types; | |
674 | struct cleanup *cleanup; | |
2c74e833 TT |
675 | |
676 | /* This prevents another thread from freeing the objects we're | |
677 | operating on. */ | |
d452c4bc | 678 | cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); |
2c74e833 TT |
679 | |
680 | copied_types = create_copied_types_hash (objfile); | |
681 | ||
682 | while (obj) | |
683 | { | |
684 | type_object *next = obj->next; | |
685 | ||
686 | htab_empty (copied_types); | |
687 | ||
688 | obj->type = copy_type_recursive (objfile, obj->type, copied_types); | |
689 | ||
690 | obj->next = NULL; | |
691 | obj->prev = NULL; | |
692 | ||
693 | obj = next; | |
694 | } | |
695 | ||
696 | htab_delete (copied_types); | |
697 | ||
698 | do_cleanups (cleanup); | |
699 | } | |
700 | ||
701 | static void | |
702 | set_type (type_object *obj, struct type *type) | |
703 | { | |
704 | obj->type = type; | |
705 | obj->prev = NULL; | |
706 | if (type && TYPE_OBJFILE (type)) | |
707 | { | |
708 | struct objfile *objfile = TYPE_OBJFILE (type); | |
709 | ||
710 | obj->next = objfile_data (objfile, typy_objfile_data_key); | |
711 | if (obj->next) | |
712 | obj->next->prev = obj; | |
713 | set_objfile_data (objfile, typy_objfile_data_key, obj); | |
714 | } | |
715 | else | |
716 | obj->next = NULL; | |
717 | } | |
718 | ||
719 | static void | |
720 | typy_dealloc (PyObject *obj) | |
721 | { | |
722 | type_object *type = (type_object *) obj; | |
723 | ||
724 | if (type->prev) | |
725 | type->prev->next = type->next; | |
726 | else if (type->type && TYPE_OBJFILE (type->type)) | |
727 | { | |
728 | /* Must reset head of list. */ | |
729 | struct objfile *objfile = TYPE_OBJFILE (type->type); | |
d59b6f6c | 730 | |
2c74e833 TT |
731 | if (objfile) |
732 | set_objfile_data (objfile, typy_objfile_data_key, type->next); | |
733 | } | |
734 | if (type->next) | |
735 | type->next->prev = type->prev; | |
736 | ||
737 | type->ob_type->tp_free (type); | |
738 | } | |
739 | ||
740 | /* Create a new Type referring to TYPE. */ | |
741 | PyObject * | |
742 | type_to_type_object (struct type *type) | |
743 | { | |
744 | type_object *type_obj; | |
745 | ||
746 | type_obj = PyObject_New (type_object, &type_object_type); | |
747 | if (type_obj) | |
748 | set_type (type_obj, type); | |
749 | ||
750 | return (PyObject *) type_obj; | |
751 | } | |
752 | ||
753 | struct type * | |
754 | type_object_to_type (PyObject *obj) | |
755 | { | |
756 | if (! PyObject_TypeCheck (obj, &type_object_type)) | |
757 | return NULL; | |
758 | return ((type_object *) obj)->type; | |
759 | } | |
760 | ||
761 | \f | |
762 | ||
763 | /* Implementation of gdb.lookup_type. */ | |
764 | PyObject * | |
765 | gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) | |
766 | { | |
5107b149 | 767 | static char *keywords[] = { "name", "block", NULL }; |
2c74e833 TT |
768 | char *type_name = NULL; |
769 | struct type *type = NULL; | |
5107b149 PM |
770 | PyObject *block_obj = NULL; |
771 | struct block *block = NULL; | |
2c74e833 | 772 | |
5107b149 PM |
773 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords, |
774 | &type_name, &block_obj)) | |
2c74e833 TT |
775 | return NULL; |
776 | ||
5107b149 PM |
777 | if (block_obj) |
778 | { | |
779 | block = block_object_to_block (block_obj); | |
780 | if (! block) | |
781 | { | |
782 | PyErr_SetString (PyExc_RuntimeError, | |
783 | _("'block' argument must be a Block.")); | |
784 | return NULL; | |
785 | } | |
786 | } | |
787 | ||
788 | type = typy_lookup_typename (type_name, block); | |
2c74e833 TT |
789 | if (! type) |
790 | return NULL; | |
791 | ||
792 | return (PyObject *) type_to_type_object (type); | |
793 | } | |
794 | ||
795 | void | |
796 | gdbpy_initialize_types (void) | |
797 | { | |
798 | int i; | |
799 | ||
800 | typy_objfile_data_key | |
c1bd65d0 | 801 | = register_objfile_data_with_cleanup (save_objfile_types, NULL); |
2c74e833 TT |
802 | |
803 | if (PyType_Ready (&type_object_type) < 0) | |
804 | return; | |
805 | if (PyType_Ready (&field_object_type) < 0) | |
806 | return; | |
807 | ||
808 | for (i = 0; pyty_codes[i].name; ++i) | |
809 | { | |
810 | if (PyModule_AddIntConstant (gdb_module, | |
811 | /* Cast needed for Python 2.4. */ | |
812 | (char *) pyty_codes[i].name, | |
813 | pyty_codes[i].code) < 0) | |
814 | return; | |
815 | } | |
816 | ||
817 | Py_INCREF (&type_object_type); | |
818 | PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type); | |
819 | ||
820 | Py_INCREF (&field_object_type); | |
821 | PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type); | |
822 | } | |
823 | ||
824 | \f | |
825 | ||
826 | static PyGetSetDef type_object_getset[] = | |
827 | { | |
828 | { "code", typy_get_code, NULL, | |
829 | "The code for this type.", NULL }, | |
830 | { "sizeof", typy_get_sizeof, NULL, | |
831 | "The size of this type, in bytes.", NULL }, | |
832 | { "tag", typy_get_tag, NULL, | |
833 | "The tag name for this type, or None.", NULL }, | |
834 | { NULL } | |
835 | }; | |
836 | ||
837 | static PyMethodDef type_object_methods[] = | |
838 | { | |
839 | { "const", typy_const, METH_NOARGS, | |
840 | "const () -> Type\n\ | |
841 | Return a const variant of this type." }, | |
842 | { "fields", typy_fields, METH_NOARGS, | |
843 | "field () -> list\n\ | |
844 | Return a sequence holding all the fields of this type.\n\ | |
845 | Each field is a dictionary." }, | |
846 | { "pointer", typy_pointer, METH_NOARGS, | |
847 | "pointer () -> Type\n\ | |
848 | Return a type of pointer to this type." }, | |
361ae042 PM |
849 | { "range", typy_range, METH_NOARGS, |
850 | "range () -> tuple\n\ | |
851 | Return a tuple containing the lower and upper range for this type."}, | |
2c74e833 TT |
852 | { "reference", typy_reference, METH_NOARGS, |
853 | "reference () -> Type\n\ | |
854 | Return a type of reference to this type." }, | |
855 | { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, | |
856 | "strip_typedefs () -> Type\n\ | |
857 | Return a type formed by stripping this type of all typedefs."}, | |
858 | { "target", typy_target, METH_NOARGS, | |
859 | "target () -> Type\n\ | |
860 | Return the target type of this type." }, | |
861 | { "template_argument", typy_template_argument, METH_VARARGS, | |
5107b149 | 862 | "template_argument (arg, [block]) -> Type\n\ |
2c74e833 TT |
863 | Return the type of a template argument." }, |
864 | { "unqualified", typy_unqualified, METH_NOARGS, | |
865 | "unqualified () -> Type\n\ | |
866 | Return a variant of this type without const or volatile attributes." }, | |
867 | { "volatile", typy_volatile, METH_NOARGS, | |
868 | "volatile () -> Type\n\ | |
869 | Return a volatile variant of this type" }, | |
870 | { NULL } | |
871 | }; | |
872 | ||
873 | static PyTypeObject type_object_type = | |
874 | { | |
875 | PyObject_HEAD_INIT (NULL) | |
876 | 0, /*ob_size*/ | |
877 | "gdb.Type", /*tp_name*/ | |
878 | sizeof (type_object), /*tp_basicsize*/ | |
879 | 0, /*tp_itemsize*/ | |
880 | typy_dealloc, /*tp_dealloc*/ | |
881 | 0, /*tp_print*/ | |
882 | 0, /*tp_getattr*/ | |
883 | 0, /*tp_setattr*/ | |
884 | 0, /*tp_compare*/ | |
885 | 0, /*tp_repr*/ | |
886 | 0, /*tp_as_number*/ | |
887 | 0, /*tp_as_sequence*/ | |
888 | 0, /*tp_as_mapping*/ | |
889 | 0, /*tp_hash */ | |
890 | 0, /*tp_call*/ | |
891 | typy_str, /*tp_str*/ | |
892 | 0, /*tp_getattro*/ | |
893 | 0, /*tp_setattro*/ | |
894 | 0, /*tp_as_buffer*/ | |
895 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
896 | "GDB type object", /* tp_doc */ | |
897 | 0, /* tp_traverse */ | |
898 | 0, /* tp_clear */ | |
899 | 0, /* tp_richcompare */ | |
900 | 0, /* tp_weaklistoffset */ | |
901 | 0, /* tp_iter */ | |
902 | 0, /* tp_iternext */ | |
903 | type_object_methods, /* tp_methods */ | |
904 | 0, /* tp_members */ | |
905 | type_object_getset, /* tp_getset */ | |
906 | 0, /* tp_base */ | |
907 | 0, /* tp_dict */ | |
908 | 0, /* tp_descr_get */ | |
909 | 0, /* tp_descr_set */ | |
910 | 0, /* tp_dictoffset */ | |
911 | 0, /* tp_init */ | |
912 | 0, /* tp_alloc */ | |
913 | 0, /* tp_new */ | |
914 | }; | |
915 | ||
916 | static PyTypeObject field_object_type = | |
917 | { | |
918 | PyObject_HEAD_INIT (NULL) | |
919 | 0, /*ob_size*/ | |
920 | "gdb.Field", /*tp_name*/ | |
921 | sizeof (field_object), /*tp_basicsize*/ | |
922 | 0, /*tp_itemsize*/ | |
923 | field_dealloc, /*tp_dealloc*/ | |
924 | 0, /*tp_print*/ | |
925 | 0, /*tp_getattr*/ | |
926 | 0, /*tp_setattr*/ | |
927 | 0, /*tp_compare*/ | |
928 | 0, /*tp_repr*/ | |
929 | 0, /*tp_as_number*/ | |
930 | 0, /*tp_as_sequence*/ | |
931 | 0, /*tp_as_mapping*/ | |
932 | 0, /*tp_hash */ | |
933 | 0, /*tp_call*/ | |
934 | 0, /*tp_str*/ | |
935 | 0, /*tp_getattro*/ | |
936 | 0, /*tp_setattro*/ | |
937 | 0, /*tp_as_buffer*/ | |
938 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
939 | "GDB field object", /* tp_doc */ | |
940 | 0, /* tp_traverse */ | |
941 | 0, /* tp_clear */ | |
942 | 0, /* tp_richcompare */ | |
943 | 0, /* tp_weaklistoffset */ | |
944 | 0, /* tp_iter */ | |
945 | 0, /* tp_iternext */ | |
946 | 0, /* tp_methods */ | |
947 | 0, /* tp_members */ | |
948 | 0, /* tp_getset */ | |
949 | 0, /* tp_base */ | |
950 | 0, /* tp_dict */ | |
951 | 0, /* tp_descr_get */ | |
952 | 0, /* tp_descr_set */ | |
953 | offsetof (field_object, dict), /* tp_dictoffset */ | |
954 | 0, /* tp_init */ | |
955 | 0, /* tp_alloc */ | |
956 | 0, /* tp_new */ | |
957 | }; |