]>
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 | ||
512 | static PyObject * | |
513 | typy_template_argument (PyObject *self, PyObject *args) | |
514 | { | |
f92adf3c | 515 | int i, argno; |
2c74e833 TT |
516 | struct type *type = ((type_object *) self)->type; |
517 | struct demangle_component *demangled; | |
518 | const char *err; | |
519 | struct type *argtype; | |
5107b149 PM |
520 | struct block *block = NULL; |
521 | PyObject *block_obj = NULL; | |
2c74e833 | 522 | |
5107b149 | 523 | if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj)) |
2c74e833 TT |
524 | return NULL; |
525 | ||
5107b149 PM |
526 | if (block_obj) |
527 | { | |
528 | block = block_object_to_block (block_obj); | |
529 | if (! block) | |
530 | { | |
531 | PyErr_SetString (PyExc_RuntimeError, | |
532 | _("Second argument must be block.")); | |
533 | return NULL; | |
534 | } | |
535 | } | |
536 | ||
2c74e833 TT |
537 | type = check_typedef (type); |
538 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
539 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
540 | ||
541 | if (TYPE_NAME (type) == NULL) | |
542 | { | |
5107b149 | 543 | PyErr_SetString (PyExc_RuntimeError, _("Null type name.")); |
2c74e833 TT |
544 | return NULL; |
545 | } | |
546 | ||
547 | /* Note -- this is not thread-safe. */ | |
548 | demangled = cp_demangled_name_to_comp (TYPE_NAME (type), &err); | |
549 | if (! demangled) | |
550 | { | |
551 | PyErr_SetString (PyExc_RuntimeError, err); | |
552 | return NULL; | |
553 | } | |
554 | ||
555 | /* Strip off component names. */ | |
556 | while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME | |
557 | || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
558 | demangled = demangled->u.s_binary.right; | |
559 | ||
560 | if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) | |
561 | { | |
5107b149 | 562 | PyErr_SetString (PyExc_RuntimeError, _("Type is not a template.")); |
2c74e833 TT |
563 | return NULL; |
564 | } | |
565 | ||
566 | /* Skip from the template to the arguments. */ | |
567 | demangled = demangled->u.s_binary.right; | |
568 | ||
569 | for (i = 0; demangled && i < argno; ++i) | |
570 | demangled = demangled->u.s_binary.right; | |
571 | ||
572 | if (! demangled) | |
573 | { | |
5107b149 | 574 | PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), |
2c74e833 TT |
575 | argno); |
576 | return NULL; | |
577 | } | |
578 | ||
5107b149 | 579 | argtype = typy_lookup_type (demangled->u.s_binary.left, block); |
2c74e833 TT |
580 | if (! argtype) |
581 | return NULL; | |
582 | ||
583 | return type_to_type_object (argtype); | |
584 | } | |
585 | ||
586 | static PyObject * | |
587 | typy_str (PyObject *self) | |
588 | { | |
589 | volatile struct gdb_exception except; | |
590 | char *thetype = NULL; | |
759ef836 | 591 | long length = 0; |
2c74e833 TT |
592 | PyObject *result; |
593 | ||
594 | TRY_CATCH (except, RETURN_MASK_ALL) | |
595 | { | |
596 | struct cleanup *old_chain; | |
597 | struct ui_file *stb; | |
2c74e833 TT |
598 | |
599 | stb = mem_fileopen (); | |
600 | old_chain = make_cleanup_ui_file_delete (stb); | |
601 | ||
602 | type_print (type_object_to_type (self), "", stb, -1); | |
603 | ||
604 | thetype = ui_file_xstrdup (stb, &length); | |
605 | do_cleanups (old_chain); | |
606 | } | |
607 | if (except.reason < 0) | |
608 | { | |
609 | xfree (thetype); | |
610 | GDB_PY_HANDLE_EXCEPTION (except); | |
611 | } | |
612 | ||
759ef836 | 613 | result = PyUnicode_Decode (thetype, length, host_charset (), NULL); |
2c74e833 TT |
614 | xfree (thetype); |
615 | ||
616 | return result; | |
617 | } | |
618 | ||
619 | \f | |
620 | ||
621 | static const struct objfile_data *typy_objfile_data_key; | |
622 | ||
623 | static void | |
c1bd65d0 | 624 | save_objfile_types (struct objfile *objfile, void *datum) |
2c74e833 TT |
625 | { |
626 | type_object *obj = datum; | |
627 | htab_t copied_types; | |
628 | struct cleanup *cleanup; | |
2c74e833 TT |
629 | |
630 | /* This prevents another thread from freeing the objects we're | |
631 | operating on. */ | |
d452c4bc | 632 | cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); |
2c74e833 TT |
633 | |
634 | copied_types = create_copied_types_hash (objfile); | |
635 | ||
636 | while (obj) | |
637 | { | |
638 | type_object *next = obj->next; | |
639 | ||
640 | htab_empty (copied_types); | |
641 | ||
642 | obj->type = copy_type_recursive (objfile, obj->type, copied_types); | |
643 | ||
644 | obj->next = NULL; | |
645 | obj->prev = NULL; | |
646 | ||
647 | obj = next; | |
648 | } | |
649 | ||
650 | htab_delete (copied_types); | |
651 | ||
652 | do_cleanups (cleanup); | |
653 | } | |
654 | ||
655 | static void | |
656 | set_type (type_object *obj, struct type *type) | |
657 | { | |
658 | obj->type = type; | |
659 | obj->prev = NULL; | |
660 | if (type && TYPE_OBJFILE (type)) | |
661 | { | |
662 | struct objfile *objfile = TYPE_OBJFILE (type); | |
663 | ||
664 | obj->next = objfile_data (objfile, typy_objfile_data_key); | |
665 | if (obj->next) | |
666 | obj->next->prev = obj; | |
667 | set_objfile_data (objfile, typy_objfile_data_key, obj); | |
668 | } | |
669 | else | |
670 | obj->next = NULL; | |
671 | } | |
672 | ||
673 | static void | |
674 | typy_dealloc (PyObject *obj) | |
675 | { | |
676 | type_object *type = (type_object *) obj; | |
677 | ||
678 | if (type->prev) | |
679 | type->prev->next = type->next; | |
680 | else if (type->type && TYPE_OBJFILE (type->type)) | |
681 | { | |
682 | /* Must reset head of list. */ | |
683 | struct objfile *objfile = TYPE_OBJFILE (type->type); | |
d59b6f6c | 684 | |
2c74e833 TT |
685 | if (objfile) |
686 | set_objfile_data (objfile, typy_objfile_data_key, type->next); | |
687 | } | |
688 | if (type->next) | |
689 | type->next->prev = type->prev; | |
690 | ||
691 | type->ob_type->tp_free (type); | |
692 | } | |
693 | ||
694 | /* Create a new Type referring to TYPE. */ | |
695 | PyObject * | |
696 | type_to_type_object (struct type *type) | |
697 | { | |
698 | type_object *type_obj; | |
699 | ||
700 | type_obj = PyObject_New (type_object, &type_object_type); | |
701 | if (type_obj) | |
702 | set_type (type_obj, type); | |
703 | ||
704 | return (PyObject *) type_obj; | |
705 | } | |
706 | ||
707 | struct type * | |
708 | type_object_to_type (PyObject *obj) | |
709 | { | |
710 | if (! PyObject_TypeCheck (obj, &type_object_type)) | |
711 | return NULL; | |
712 | return ((type_object *) obj)->type; | |
713 | } | |
714 | ||
715 | \f | |
716 | ||
717 | /* Implementation of gdb.lookup_type. */ | |
718 | PyObject * | |
719 | gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) | |
720 | { | |
5107b149 | 721 | static char *keywords[] = { "name", "block", NULL }; |
2c74e833 TT |
722 | char *type_name = NULL; |
723 | struct type *type = NULL; | |
5107b149 PM |
724 | PyObject *block_obj = NULL; |
725 | struct block *block = NULL; | |
2c74e833 | 726 | |
5107b149 PM |
727 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords, |
728 | &type_name, &block_obj)) | |
2c74e833 TT |
729 | return NULL; |
730 | ||
5107b149 PM |
731 | if (block_obj) |
732 | { | |
733 | block = block_object_to_block (block_obj); | |
734 | if (! block) | |
735 | { | |
736 | PyErr_SetString (PyExc_RuntimeError, | |
737 | _("'block' argument must be a Block.")); | |
738 | return NULL; | |
739 | } | |
740 | } | |
741 | ||
742 | type = typy_lookup_typename (type_name, block); | |
2c74e833 TT |
743 | if (! type) |
744 | return NULL; | |
745 | ||
746 | return (PyObject *) type_to_type_object (type); | |
747 | } | |
748 | ||
749 | void | |
750 | gdbpy_initialize_types (void) | |
751 | { | |
752 | int i; | |
753 | ||
754 | typy_objfile_data_key | |
c1bd65d0 | 755 | = register_objfile_data_with_cleanup (save_objfile_types, NULL); |
2c74e833 TT |
756 | |
757 | if (PyType_Ready (&type_object_type) < 0) | |
758 | return; | |
759 | if (PyType_Ready (&field_object_type) < 0) | |
760 | return; | |
761 | ||
762 | for (i = 0; pyty_codes[i].name; ++i) | |
763 | { | |
764 | if (PyModule_AddIntConstant (gdb_module, | |
765 | /* Cast needed for Python 2.4. */ | |
766 | (char *) pyty_codes[i].name, | |
767 | pyty_codes[i].code) < 0) | |
768 | return; | |
769 | } | |
770 | ||
771 | Py_INCREF (&type_object_type); | |
772 | PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type); | |
773 | ||
774 | Py_INCREF (&field_object_type); | |
775 | PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type); | |
776 | } | |
777 | ||
778 | \f | |
779 | ||
780 | static PyGetSetDef type_object_getset[] = | |
781 | { | |
782 | { "code", typy_get_code, NULL, | |
783 | "The code for this type.", NULL }, | |
784 | { "sizeof", typy_get_sizeof, NULL, | |
785 | "The size of this type, in bytes.", NULL }, | |
786 | { "tag", typy_get_tag, NULL, | |
787 | "The tag name for this type, or None.", NULL }, | |
788 | { NULL } | |
789 | }; | |
790 | ||
791 | static PyMethodDef type_object_methods[] = | |
792 | { | |
793 | { "const", typy_const, METH_NOARGS, | |
794 | "const () -> Type\n\ | |
795 | Return a const variant of this type." }, | |
796 | { "fields", typy_fields, METH_NOARGS, | |
797 | "field () -> list\n\ | |
798 | Return a sequence holding all the fields of this type.\n\ | |
799 | Each field is a dictionary." }, | |
800 | { "pointer", typy_pointer, METH_NOARGS, | |
801 | "pointer () -> Type\n\ | |
802 | Return a type of pointer to this type." }, | |
361ae042 PM |
803 | { "range", typy_range, METH_NOARGS, |
804 | "range () -> tuple\n\ | |
805 | Return a tuple containing the lower and upper range for this type."}, | |
2c74e833 TT |
806 | { "reference", typy_reference, METH_NOARGS, |
807 | "reference () -> Type\n\ | |
808 | Return a type of reference to this type." }, | |
809 | { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, | |
810 | "strip_typedefs () -> Type\n\ | |
811 | Return a type formed by stripping this type of all typedefs."}, | |
812 | { "target", typy_target, METH_NOARGS, | |
813 | "target () -> Type\n\ | |
814 | Return the target type of this type." }, | |
815 | { "template_argument", typy_template_argument, METH_VARARGS, | |
5107b149 | 816 | "template_argument (arg, [block]) -> Type\n\ |
2c74e833 TT |
817 | Return the type of a template argument." }, |
818 | { "unqualified", typy_unqualified, METH_NOARGS, | |
819 | "unqualified () -> Type\n\ | |
820 | Return a variant of this type without const or volatile attributes." }, | |
821 | { "volatile", typy_volatile, METH_NOARGS, | |
822 | "volatile () -> Type\n\ | |
823 | Return a volatile variant of this type" }, | |
824 | { NULL } | |
825 | }; | |
826 | ||
827 | static PyTypeObject type_object_type = | |
828 | { | |
829 | PyObject_HEAD_INIT (NULL) | |
830 | 0, /*ob_size*/ | |
831 | "gdb.Type", /*tp_name*/ | |
832 | sizeof (type_object), /*tp_basicsize*/ | |
833 | 0, /*tp_itemsize*/ | |
834 | typy_dealloc, /*tp_dealloc*/ | |
835 | 0, /*tp_print*/ | |
836 | 0, /*tp_getattr*/ | |
837 | 0, /*tp_setattr*/ | |
838 | 0, /*tp_compare*/ | |
839 | 0, /*tp_repr*/ | |
840 | 0, /*tp_as_number*/ | |
841 | 0, /*tp_as_sequence*/ | |
842 | 0, /*tp_as_mapping*/ | |
843 | 0, /*tp_hash */ | |
844 | 0, /*tp_call*/ | |
845 | typy_str, /*tp_str*/ | |
846 | 0, /*tp_getattro*/ | |
847 | 0, /*tp_setattro*/ | |
848 | 0, /*tp_as_buffer*/ | |
849 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
850 | "GDB type object", /* tp_doc */ | |
851 | 0, /* tp_traverse */ | |
852 | 0, /* tp_clear */ | |
853 | 0, /* tp_richcompare */ | |
854 | 0, /* tp_weaklistoffset */ | |
855 | 0, /* tp_iter */ | |
856 | 0, /* tp_iternext */ | |
857 | type_object_methods, /* tp_methods */ | |
858 | 0, /* tp_members */ | |
859 | type_object_getset, /* tp_getset */ | |
860 | 0, /* tp_base */ | |
861 | 0, /* tp_dict */ | |
862 | 0, /* tp_descr_get */ | |
863 | 0, /* tp_descr_set */ | |
864 | 0, /* tp_dictoffset */ | |
865 | 0, /* tp_init */ | |
866 | 0, /* tp_alloc */ | |
867 | 0, /* tp_new */ | |
868 | }; | |
869 | ||
870 | static PyTypeObject field_object_type = | |
871 | { | |
872 | PyObject_HEAD_INIT (NULL) | |
873 | 0, /*ob_size*/ | |
874 | "gdb.Field", /*tp_name*/ | |
875 | sizeof (field_object), /*tp_basicsize*/ | |
876 | 0, /*tp_itemsize*/ | |
877 | field_dealloc, /*tp_dealloc*/ | |
878 | 0, /*tp_print*/ | |
879 | 0, /*tp_getattr*/ | |
880 | 0, /*tp_setattr*/ | |
881 | 0, /*tp_compare*/ | |
882 | 0, /*tp_repr*/ | |
883 | 0, /*tp_as_number*/ | |
884 | 0, /*tp_as_sequence*/ | |
885 | 0, /*tp_as_mapping*/ | |
886 | 0, /*tp_hash */ | |
887 | 0, /*tp_call*/ | |
888 | 0, /*tp_str*/ | |
889 | 0, /*tp_getattro*/ | |
890 | 0, /*tp_setattro*/ | |
891 | 0, /*tp_as_buffer*/ | |
892 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
893 | "GDB field object", /* tp_doc */ | |
894 | 0, /* tp_traverse */ | |
895 | 0, /* tp_clear */ | |
896 | 0, /* tp_richcompare */ | |
897 | 0, /* tp_weaklistoffset */ | |
898 | 0, /* tp_iter */ | |
899 | 0, /* tp_iternext */ | |
900 | 0, /* tp_methods */ | |
901 | 0, /* tp_members */ | |
902 | 0, /* tp_getset */ | |
903 | 0, /* tp_base */ | |
904 | 0, /* tp_dict */ | |
905 | 0, /* tp_descr_get */ | |
906 | 0, /* tp_descr_set */ | |
907 | offsetof (field_object, dict), /* tp_dictoffset */ | |
908 | 0, /* tp_init */ | |
909 | 0, /* tp_alloc */ | |
910 | 0, /* tp_new */ | |
911 | }; |