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; | |
104 | Py_XDECREF (f->dict); | |
105 | f->ob_type->tp_free (obj); | |
106 | } | |
107 | ||
108 | static PyObject * | |
109 | field_new (void) | |
110 | { | |
111 | field_object *result = PyObject_New (field_object, &field_object_type); | |
112 | if (result) | |
113 | { | |
114 | result->dict = PyDict_New (); | |
115 | if (!result->dict) | |
116 | { | |
117 | Py_DECREF (result); | |
118 | result = NULL; | |
119 | } | |
120 | } | |
121 | return (PyObject *) result; | |
122 | } | |
123 | ||
124 | \f | |
125 | ||
126 | /* Return the code for this type. */ | |
127 | static PyObject * | |
128 | typy_get_code (PyObject *self, void *closure) | |
129 | { | |
130 | struct type *type = ((type_object *) self)->type; | |
131 | return PyInt_FromLong (TYPE_CODE (type)); | |
132 | } | |
133 | ||
134 | /* Helper function for typy_fields which converts a single field to a | |
135 | dictionary. Returns NULL on error. */ | |
136 | static PyObject * | |
137 | convert_field (struct type *type, int field) | |
138 | { | |
139 | PyObject *result = field_new (); | |
140 | PyObject *arg; | |
141 | ||
142 | if (!result) | |
143 | return NULL; | |
144 | ||
145 | if (!field_is_static (&TYPE_FIELD (type, field))) | |
146 | { | |
147 | arg = PyLong_FromLong (TYPE_FIELD_BITPOS (type, field)); | |
148 | if (!arg) | |
149 | goto fail; | |
150 | ||
151 | if (PyObject_SetAttrString (result, "bitpos", arg) < 0) | |
152 | goto failarg; | |
153 | } | |
154 | ||
155 | if (TYPE_FIELD_NAME (type, field)) | |
156 | arg = PyString_FromString (TYPE_FIELD_NAME (type, field)); | |
157 | else | |
158 | { | |
159 | arg = Py_None; | |
160 | Py_INCREF (arg); | |
161 | } | |
162 | if (!arg) | |
163 | goto fail; | |
164 | if (PyObject_SetAttrString (result, "name", arg) < 0) | |
165 | goto failarg; | |
166 | ||
167 | arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False; | |
168 | Py_INCREF (arg); | |
169 | if (PyObject_SetAttrString (result, "artificial", arg) < 0) | |
170 | goto failarg; | |
171 | ||
bfd31e71 PM |
172 | if (TYPE_CODE (type) == TYPE_CODE_CLASS) |
173 | arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False; | |
174 | else | |
175 | arg = Py_False; | |
176 | Py_INCREF (arg); | |
177 | if (PyObject_SetAttrString (result, "is_base_class", arg) < 0) | |
178 | goto failarg; | |
179 | ||
2c74e833 TT |
180 | arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field)); |
181 | if (!arg) | |
182 | goto fail; | |
183 | if (PyObject_SetAttrString (result, "bitsize", arg) < 0) | |
184 | goto failarg; | |
185 | ||
186 | /* A field can have a NULL type in some situations. */ | |
187 | if (TYPE_FIELD_TYPE (type, field) == NULL) | |
188 | { | |
189 | arg = Py_None; | |
190 | Py_INCREF (arg); | |
191 | } | |
192 | else | |
193 | arg = type_to_type_object (TYPE_FIELD_TYPE (type, field)); | |
194 | if (!arg) | |
195 | goto fail; | |
196 | if (PyObject_SetAttrString (result, "type", arg) < 0) | |
197 | goto failarg; | |
198 | ||
199 | return result; | |
200 | ||
201 | failarg: | |
202 | Py_DECREF (arg); | |
203 | fail: | |
204 | Py_DECREF (result); | |
205 | return NULL; | |
206 | } | |
207 | ||
208 | /* Return a sequence of all fields. Each field is a dictionary with | |
209 | some pre-defined keys. */ | |
210 | static PyObject * | |
211 | typy_fields (PyObject *self, PyObject *args) | |
212 | { | |
213 | PyObject *result; | |
214 | int i; | |
215 | struct type *type = ((type_object *) self)->type; | |
216 | ||
217 | /* We would like to make a tuple here, make fields immutable, and | |
218 | then memoize the result (and perhaps make Field.type() lazy). | |
219 | However, that can lead to cycles. */ | |
220 | result = PyList_New (0); | |
221 | ||
222 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
223 | { | |
224 | PyObject *dict = convert_field (type, i); | |
225 | if (!dict) | |
226 | { | |
227 | Py_DECREF (result); | |
228 | return NULL; | |
229 | } | |
230 | if (PyList_Append (result, dict)) | |
231 | { | |
232 | Py_DECREF (dict); | |
233 | Py_DECREF (result); | |
234 | return NULL; | |
235 | } | |
236 | } | |
237 | ||
238 | return result; | |
239 | } | |
240 | ||
241 | /* Return the type's tag, or None. */ | |
242 | static PyObject * | |
243 | typy_get_tag (PyObject *self, void *closure) | |
244 | { | |
245 | struct type *type = ((type_object *) self)->type; | |
246 | if (!TYPE_TAG_NAME (type)) | |
247 | Py_RETURN_NONE; | |
248 | return PyString_FromString (TYPE_TAG_NAME (type)); | |
249 | } | |
250 | ||
251 | /* Return the type, stripped of typedefs. */ | |
252 | static PyObject * | |
253 | typy_strip_typedefs (PyObject *self, PyObject *args) | |
254 | { | |
255 | struct type *type = ((type_object *) self)->type; | |
256 | ||
257 | return type_to_type_object (check_typedef (type)); | |
258 | } | |
259 | ||
260 | /* Return a Type object which represents a pointer to SELF. */ | |
261 | static PyObject * | |
262 | typy_pointer (PyObject *self, PyObject *args) | |
263 | { | |
264 | struct type *type = ((type_object *) self)->type; | |
265 | volatile struct gdb_exception except; | |
266 | ||
267 | TRY_CATCH (except, RETURN_MASK_ALL) | |
268 | { | |
269 | type = lookup_pointer_type (type); | |
270 | } | |
271 | GDB_PY_HANDLE_EXCEPTION (except); | |
272 | ||
273 | return type_to_type_object (type); | |
274 | } | |
275 | ||
361ae042 PM |
276 | /* Return the range of a type represented by SELF. The return type is |
277 | a tuple. The first element of the tuple contains the low bound, | |
278 | while the second element of the tuple contains the high bound. */ | |
279 | static PyObject * | |
280 | typy_range (PyObject *self, PyObject *args) | |
281 | { | |
282 | struct type *type = ((type_object *) self)->type; | |
283 | PyObject *result; | |
284 | PyObject *low_bound = NULL, *high_bound = NULL; | |
8d099ae9 PM |
285 | /* Initialize these to appease GCC warnings. */ |
286 | LONGEST low = 0, high = 0; | |
361ae042 PM |
287 | |
288 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
289 | && TYPE_CODE (type) != TYPE_CODE_STRING | |
290 | && TYPE_CODE (type) != TYPE_CODE_RANGE) | |
291 | { | |
292 | PyErr_SetString (PyExc_RuntimeError, | |
293 | "This type does not have a range."); | |
294 | return NULL; | |
295 | } | |
296 | ||
297 | switch (TYPE_CODE (type)) | |
298 | { | |
299 | case TYPE_CODE_ARRAY: | |
300 | case TYPE_CODE_STRING: | |
301 | low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); | |
302 | high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)); | |
303 | break; | |
304 | case TYPE_CODE_RANGE: | |
305 | low = TYPE_LOW_BOUND (type); | |
306 | high = TYPE_HIGH_BOUND (type); | |
307 | break; | |
308 | } | |
309 | ||
310 | low_bound = PyLong_FromLong (low); | |
311 | if (!low_bound) | |
312 | goto failarg; | |
313 | ||
314 | high_bound = PyLong_FromLong (high); | |
315 | if (!high_bound) | |
316 | goto failarg; | |
317 | ||
318 | result = PyTuple_New (2); | |
319 | if (!result) | |
320 | goto failarg; | |
321 | ||
322 | if (PyTuple_SetItem (result, 0, low_bound) != 0) | |
323 | { | |
324 | Py_DECREF (result); | |
325 | goto failarg; | |
326 | } | |
327 | if (PyTuple_SetItem (result, 1, high_bound) != 0) | |
328 | { | |
329 | Py_DECREF (high_bound); | |
330 | Py_DECREF (result); | |
331 | return NULL; | |
332 | } | |
333 | return result; | |
334 | ||
335 | failarg: | |
336 | Py_XDECREF (high_bound); | |
337 | Py_XDECREF (low_bound); | |
338 | return NULL; | |
339 | } | |
340 | ||
2c74e833 TT |
341 | /* Return a Type object which represents a reference to SELF. */ |
342 | static PyObject * | |
343 | typy_reference (PyObject *self, PyObject *args) | |
344 | { | |
345 | struct type *type = ((type_object *) self)->type; | |
346 | volatile struct gdb_exception except; | |
347 | ||
348 | TRY_CATCH (except, RETURN_MASK_ALL) | |
349 | { | |
350 | type = lookup_reference_type (type); | |
351 | } | |
352 | GDB_PY_HANDLE_EXCEPTION (except); | |
353 | ||
354 | return type_to_type_object (type); | |
355 | } | |
356 | ||
357 | /* Return a Type object which represents the target type of SELF. */ | |
358 | static PyObject * | |
359 | typy_target (PyObject *self, PyObject *args) | |
360 | { | |
361 | struct type *type = ((type_object *) self)->type; | |
362 | ||
363 | if (!TYPE_TARGET_TYPE (type)) | |
364 | { | |
365 | PyErr_SetString (PyExc_RuntimeError, "type does not have a target"); | |
366 | return NULL; | |
367 | } | |
368 | ||
369 | return type_to_type_object (TYPE_TARGET_TYPE (type)); | |
370 | } | |
371 | ||
372 | /* Return a const-qualified type variant. */ | |
373 | static PyObject * | |
374 | typy_const (PyObject *self, PyObject *args) | |
375 | { | |
376 | struct type *type = ((type_object *) self)->type; | |
377 | volatile struct gdb_exception except; | |
378 | ||
379 | TRY_CATCH (except, RETURN_MASK_ALL) | |
380 | { | |
381 | type = make_cv_type (1, 0, type, NULL); | |
382 | } | |
383 | GDB_PY_HANDLE_EXCEPTION (except); | |
384 | ||
385 | return type_to_type_object (type); | |
386 | } | |
387 | ||
388 | /* Return a volatile-qualified type variant. */ | |
389 | static PyObject * | |
390 | typy_volatile (PyObject *self, PyObject *args) | |
391 | { | |
392 | struct type *type = ((type_object *) self)->type; | |
393 | volatile struct gdb_exception except; | |
394 | ||
395 | TRY_CATCH (except, RETURN_MASK_ALL) | |
396 | { | |
397 | type = make_cv_type (0, 1, type, NULL); | |
398 | } | |
399 | GDB_PY_HANDLE_EXCEPTION (except); | |
400 | ||
401 | return type_to_type_object (type); | |
402 | } | |
403 | ||
404 | /* Return an unqualified type variant. */ | |
405 | static PyObject * | |
406 | typy_unqualified (PyObject *self, PyObject *args) | |
407 | { | |
408 | struct type *type = ((type_object *) self)->type; | |
409 | volatile struct gdb_exception except; | |
410 | ||
411 | TRY_CATCH (except, RETURN_MASK_ALL) | |
412 | { | |
413 | type = make_cv_type (0, 0, type, NULL); | |
414 | } | |
415 | GDB_PY_HANDLE_EXCEPTION (except); | |
416 | ||
417 | return type_to_type_object (type); | |
418 | } | |
419 | ||
420 | /* Return the size of the type represented by SELF, in bytes. */ | |
421 | static PyObject * | |
422 | typy_get_sizeof (PyObject *self, void *closure) | |
423 | { | |
424 | struct type *type = ((type_object *) self)->type; | |
425 | volatile struct gdb_exception except; | |
426 | ||
427 | TRY_CATCH (except, RETURN_MASK_ALL) | |
428 | { | |
429 | check_typedef (type); | |
430 | } | |
431 | /* Ignore exceptions. */ | |
432 | ||
433 | return PyLong_FromLong (TYPE_LENGTH (type)); | |
434 | } | |
435 | ||
436 | static struct type * | |
437 | typy_lookup_typename (char *type_name) | |
438 | { | |
439 | struct type *type = NULL; | |
440 | volatile struct gdb_exception except; | |
441 | TRY_CATCH (except, RETURN_MASK_ALL) | |
442 | { | |
443 | if (!strncmp (type_name, "struct ", 7)) | |
444 | type = lookup_struct (type_name + 7, NULL); | |
445 | else if (!strncmp (type_name, "union ", 6)) | |
446 | type = lookup_union (type_name + 6, NULL); | |
447 | else if (!strncmp (type_name, "enum ", 5)) | |
448 | type = lookup_enum (type_name + 5, NULL); | |
449 | else | |
d452c4bc | 450 | type = lookup_typename (python_language, python_gdbarch, |
e6c014f2 | 451 | type_name, NULL, 0); |
2c74e833 TT |
452 | } |
453 | if (except.reason < 0) | |
454 | { | |
455 | PyErr_Format (except.reason == RETURN_QUIT | |
456 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
457 | "%s", except.message); | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | return type; | |
462 | } | |
463 | ||
464 | static struct type * | |
465 | typy_lookup_type (struct demangle_component *demangled) | |
466 | { | |
467 | struct type *type; | |
468 | char *type_name; | |
469 | enum demangle_component_type demangled_type; | |
470 | ||
471 | /* Save the type: typy_lookup_type() may (indirectly) overwrite | |
472 | memory pointed by demangled. */ | |
473 | demangled_type = demangled->type; | |
474 | ||
475 | if (demangled_type == DEMANGLE_COMPONENT_POINTER | |
476 | || demangled_type == DEMANGLE_COMPONENT_REFERENCE | |
477 | || demangled_type == DEMANGLE_COMPONENT_CONST | |
478 | || demangled_type == DEMANGLE_COMPONENT_VOLATILE) | |
479 | { | |
480 | type = typy_lookup_type (demangled->u.s_binary.left); | |
481 | if (! type) | |
482 | return NULL; | |
483 | ||
484 | switch (demangled_type) | |
485 | { | |
486 | case DEMANGLE_COMPONENT_REFERENCE: | |
487 | return lookup_reference_type (type); | |
488 | case DEMANGLE_COMPONENT_POINTER: | |
489 | return lookup_pointer_type (type); | |
490 | case DEMANGLE_COMPONENT_CONST: | |
491 | return make_cv_type (1, 0, type, NULL); | |
492 | case DEMANGLE_COMPONENT_VOLATILE: | |
493 | return make_cv_type (0, 1, type, NULL); | |
494 | } | |
495 | } | |
496 | ||
497 | type_name = cp_comp_to_string (demangled, 10); | |
498 | type = typy_lookup_typename (type_name); | |
499 | xfree (type_name); | |
500 | ||
501 | return type; | |
502 | } | |
503 | ||
504 | static PyObject * | |
505 | typy_template_argument (PyObject *self, PyObject *args) | |
506 | { | |
507 | int i, argno, n_pointers; | |
508 | struct type *type = ((type_object *) self)->type; | |
509 | struct demangle_component *demangled; | |
510 | const char *err; | |
511 | struct type *argtype; | |
512 | ||
513 | if (! PyArg_ParseTuple (args, "i", &argno)) | |
514 | return NULL; | |
515 | ||
516 | type = check_typedef (type); | |
517 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
518 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
519 | ||
520 | if (TYPE_NAME (type) == NULL) | |
521 | { | |
522 | PyErr_SetString (PyExc_RuntimeError, "null type name"); | |
523 | return NULL; | |
524 | } | |
525 | ||
526 | /* Note -- this is not thread-safe. */ | |
527 | demangled = cp_demangled_name_to_comp (TYPE_NAME (type), &err); | |
528 | if (! demangled) | |
529 | { | |
530 | PyErr_SetString (PyExc_RuntimeError, err); | |
531 | return NULL; | |
532 | } | |
533 | ||
534 | /* Strip off component names. */ | |
535 | while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME | |
536 | || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
537 | demangled = demangled->u.s_binary.right; | |
538 | ||
539 | if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) | |
540 | { | |
541 | PyErr_SetString (PyExc_RuntimeError, "type is not a template"); | |
542 | return NULL; | |
543 | } | |
544 | ||
545 | /* Skip from the template to the arguments. */ | |
546 | demangled = demangled->u.s_binary.right; | |
547 | ||
548 | for (i = 0; demangled && i < argno; ++i) | |
549 | demangled = demangled->u.s_binary.right; | |
550 | ||
551 | if (! demangled) | |
552 | { | |
553 | PyErr_Format (PyExc_RuntimeError, "no argument %d in template", | |
554 | argno); | |
555 | return NULL; | |
556 | } | |
557 | ||
558 | argtype = typy_lookup_type (demangled->u.s_binary.left); | |
559 | if (! argtype) | |
560 | return NULL; | |
561 | ||
562 | return type_to_type_object (argtype); | |
563 | } | |
564 | ||
565 | static PyObject * | |
566 | typy_str (PyObject *self) | |
567 | { | |
568 | volatile struct gdb_exception except; | |
569 | char *thetype = NULL; | |
759ef836 | 570 | long length = 0; |
2c74e833 TT |
571 | PyObject *result; |
572 | ||
573 | TRY_CATCH (except, RETURN_MASK_ALL) | |
574 | { | |
575 | struct cleanup *old_chain; | |
576 | struct ui_file *stb; | |
2c74e833 TT |
577 | |
578 | stb = mem_fileopen (); | |
579 | old_chain = make_cleanup_ui_file_delete (stb); | |
580 | ||
581 | type_print (type_object_to_type (self), "", stb, -1); | |
582 | ||
583 | thetype = ui_file_xstrdup (stb, &length); | |
584 | do_cleanups (old_chain); | |
585 | } | |
586 | if (except.reason < 0) | |
587 | { | |
588 | xfree (thetype); | |
589 | GDB_PY_HANDLE_EXCEPTION (except); | |
590 | } | |
591 | ||
759ef836 | 592 | result = PyUnicode_Decode (thetype, length, host_charset (), NULL); |
2c74e833 TT |
593 | xfree (thetype); |
594 | ||
595 | return result; | |
596 | } | |
597 | ||
598 | \f | |
599 | ||
600 | static const struct objfile_data *typy_objfile_data_key; | |
601 | ||
602 | static void | |
c1bd65d0 | 603 | save_objfile_types (struct objfile *objfile, void *datum) |
2c74e833 TT |
604 | { |
605 | type_object *obj = datum; | |
606 | htab_t copied_types; | |
607 | struct cleanup *cleanup; | |
2c74e833 TT |
608 | |
609 | /* This prevents another thread from freeing the objects we're | |
610 | operating on. */ | |
d452c4bc | 611 | cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); |
2c74e833 TT |
612 | |
613 | copied_types = create_copied_types_hash (objfile); | |
614 | ||
615 | while (obj) | |
616 | { | |
617 | type_object *next = obj->next; | |
618 | ||
619 | htab_empty (copied_types); | |
620 | ||
621 | obj->type = copy_type_recursive (objfile, obj->type, copied_types); | |
622 | ||
623 | obj->next = NULL; | |
624 | obj->prev = NULL; | |
625 | ||
626 | obj = next; | |
627 | } | |
628 | ||
629 | htab_delete (copied_types); | |
630 | ||
631 | do_cleanups (cleanup); | |
632 | } | |
633 | ||
634 | static void | |
635 | set_type (type_object *obj, struct type *type) | |
636 | { | |
637 | obj->type = type; | |
638 | obj->prev = NULL; | |
639 | if (type && TYPE_OBJFILE (type)) | |
640 | { | |
641 | struct objfile *objfile = TYPE_OBJFILE (type); | |
642 | ||
643 | obj->next = objfile_data (objfile, typy_objfile_data_key); | |
644 | if (obj->next) | |
645 | obj->next->prev = obj; | |
646 | set_objfile_data (objfile, typy_objfile_data_key, obj); | |
647 | } | |
648 | else | |
649 | obj->next = NULL; | |
650 | } | |
651 | ||
652 | static void | |
653 | typy_dealloc (PyObject *obj) | |
654 | { | |
655 | type_object *type = (type_object *) obj; | |
656 | ||
657 | if (type->prev) | |
658 | type->prev->next = type->next; | |
659 | else if (type->type && TYPE_OBJFILE (type->type)) | |
660 | { | |
661 | /* Must reset head of list. */ | |
662 | struct objfile *objfile = TYPE_OBJFILE (type->type); | |
663 | if (objfile) | |
664 | set_objfile_data (objfile, typy_objfile_data_key, type->next); | |
665 | } | |
666 | if (type->next) | |
667 | type->next->prev = type->prev; | |
668 | ||
669 | type->ob_type->tp_free (type); | |
670 | } | |
671 | ||
672 | /* Create a new Type referring to TYPE. */ | |
673 | PyObject * | |
674 | type_to_type_object (struct type *type) | |
675 | { | |
676 | type_object *type_obj; | |
677 | ||
678 | type_obj = PyObject_New (type_object, &type_object_type); | |
679 | if (type_obj) | |
680 | set_type (type_obj, type); | |
681 | ||
682 | return (PyObject *) type_obj; | |
683 | } | |
684 | ||
685 | struct type * | |
686 | type_object_to_type (PyObject *obj) | |
687 | { | |
688 | if (! PyObject_TypeCheck (obj, &type_object_type)) | |
689 | return NULL; | |
690 | return ((type_object *) obj)->type; | |
691 | } | |
692 | ||
693 | \f | |
694 | ||
695 | /* Implementation of gdb.lookup_type. */ | |
696 | PyObject * | |
697 | gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) | |
698 | { | |
699 | static char *keywords[] = { "name", NULL }; | |
700 | char *type_name = NULL; | |
701 | struct type *type = NULL; | |
702 | ||
703 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &type_name)) | |
704 | return NULL; | |
705 | ||
706 | type = typy_lookup_typename (type_name); | |
707 | if (! type) | |
708 | return NULL; | |
709 | ||
710 | return (PyObject *) type_to_type_object (type); | |
711 | } | |
712 | ||
713 | void | |
714 | gdbpy_initialize_types (void) | |
715 | { | |
716 | int i; | |
717 | ||
718 | typy_objfile_data_key | |
c1bd65d0 | 719 | = register_objfile_data_with_cleanup (save_objfile_types, NULL); |
2c74e833 TT |
720 | |
721 | if (PyType_Ready (&type_object_type) < 0) | |
722 | return; | |
723 | if (PyType_Ready (&field_object_type) < 0) | |
724 | return; | |
725 | ||
726 | for (i = 0; pyty_codes[i].name; ++i) | |
727 | { | |
728 | if (PyModule_AddIntConstant (gdb_module, | |
729 | /* Cast needed for Python 2.4. */ | |
730 | (char *) pyty_codes[i].name, | |
731 | pyty_codes[i].code) < 0) | |
732 | return; | |
733 | } | |
734 | ||
735 | Py_INCREF (&type_object_type); | |
736 | PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type); | |
737 | ||
738 | Py_INCREF (&field_object_type); | |
739 | PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type); | |
740 | } | |
741 | ||
742 | \f | |
743 | ||
744 | static PyGetSetDef type_object_getset[] = | |
745 | { | |
746 | { "code", typy_get_code, NULL, | |
747 | "The code for this type.", NULL }, | |
748 | { "sizeof", typy_get_sizeof, NULL, | |
749 | "The size of this type, in bytes.", NULL }, | |
750 | { "tag", typy_get_tag, NULL, | |
751 | "The tag name for this type, or None.", NULL }, | |
752 | { NULL } | |
753 | }; | |
754 | ||
755 | static PyMethodDef type_object_methods[] = | |
756 | { | |
757 | { "const", typy_const, METH_NOARGS, | |
758 | "const () -> Type\n\ | |
759 | Return a const variant of this type." }, | |
760 | { "fields", typy_fields, METH_NOARGS, | |
761 | "field () -> list\n\ | |
762 | Return a sequence holding all the fields of this type.\n\ | |
763 | Each field is a dictionary." }, | |
764 | { "pointer", typy_pointer, METH_NOARGS, | |
765 | "pointer () -> Type\n\ | |
766 | Return a type of pointer to this type." }, | |
361ae042 PM |
767 | { "range", typy_range, METH_NOARGS, |
768 | "range () -> tuple\n\ | |
769 | Return a tuple containing the lower and upper range for this type."}, | |
2c74e833 TT |
770 | { "reference", typy_reference, METH_NOARGS, |
771 | "reference () -> Type\n\ | |
772 | Return a type of reference to this type." }, | |
773 | { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, | |
774 | "strip_typedefs () -> Type\n\ | |
775 | Return a type formed by stripping this type of all typedefs."}, | |
776 | { "target", typy_target, METH_NOARGS, | |
777 | "target () -> Type\n\ | |
778 | Return the target type of this type." }, | |
779 | { "template_argument", typy_template_argument, METH_VARARGS, | |
780 | "template_argument (arg) -> Type\n\ | |
781 | Return the type of a template argument." }, | |
782 | { "unqualified", typy_unqualified, METH_NOARGS, | |
783 | "unqualified () -> Type\n\ | |
784 | Return a variant of this type without const or volatile attributes." }, | |
785 | { "volatile", typy_volatile, METH_NOARGS, | |
786 | "volatile () -> Type\n\ | |
787 | Return a volatile variant of this type" }, | |
788 | { NULL } | |
789 | }; | |
790 | ||
791 | static PyTypeObject type_object_type = | |
792 | { | |
793 | PyObject_HEAD_INIT (NULL) | |
794 | 0, /*ob_size*/ | |
795 | "gdb.Type", /*tp_name*/ | |
796 | sizeof (type_object), /*tp_basicsize*/ | |
797 | 0, /*tp_itemsize*/ | |
798 | typy_dealloc, /*tp_dealloc*/ | |
799 | 0, /*tp_print*/ | |
800 | 0, /*tp_getattr*/ | |
801 | 0, /*tp_setattr*/ | |
802 | 0, /*tp_compare*/ | |
803 | 0, /*tp_repr*/ | |
804 | 0, /*tp_as_number*/ | |
805 | 0, /*tp_as_sequence*/ | |
806 | 0, /*tp_as_mapping*/ | |
807 | 0, /*tp_hash */ | |
808 | 0, /*tp_call*/ | |
809 | typy_str, /*tp_str*/ | |
810 | 0, /*tp_getattro*/ | |
811 | 0, /*tp_setattro*/ | |
812 | 0, /*tp_as_buffer*/ | |
813 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
814 | "GDB type object", /* tp_doc */ | |
815 | 0, /* tp_traverse */ | |
816 | 0, /* tp_clear */ | |
817 | 0, /* tp_richcompare */ | |
818 | 0, /* tp_weaklistoffset */ | |
819 | 0, /* tp_iter */ | |
820 | 0, /* tp_iternext */ | |
821 | type_object_methods, /* tp_methods */ | |
822 | 0, /* tp_members */ | |
823 | type_object_getset, /* tp_getset */ | |
824 | 0, /* tp_base */ | |
825 | 0, /* tp_dict */ | |
826 | 0, /* tp_descr_get */ | |
827 | 0, /* tp_descr_set */ | |
828 | 0, /* tp_dictoffset */ | |
829 | 0, /* tp_init */ | |
830 | 0, /* tp_alloc */ | |
831 | 0, /* tp_new */ | |
832 | }; | |
833 | ||
834 | static PyTypeObject field_object_type = | |
835 | { | |
836 | PyObject_HEAD_INIT (NULL) | |
837 | 0, /*ob_size*/ | |
838 | "gdb.Field", /*tp_name*/ | |
839 | sizeof (field_object), /*tp_basicsize*/ | |
840 | 0, /*tp_itemsize*/ | |
841 | field_dealloc, /*tp_dealloc*/ | |
842 | 0, /*tp_print*/ | |
843 | 0, /*tp_getattr*/ | |
844 | 0, /*tp_setattr*/ | |
845 | 0, /*tp_compare*/ | |
846 | 0, /*tp_repr*/ | |
847 | 0, /*tp_as_number*/ | |
848 | 0, /*tp_as_sequence*/ | |
849 | 0, /*tp_as_mapping*/ | |
850 | 0, /*tp_hash */ | |
851 | 0, /*tp_call*/ | |
852 | 0, /*tp_str*/ | |
853 | 0, /*tp_getattro*/ | |
854 | 0, /*tp_setattro*/ | |
855 | 0, /*tp_as_buffer*/ | |
856 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ | |
857 | "GDB field object", /* tp_doc */ | |
858 | 0, /* tp_traverse */ | |
859 | 0, /* tp_clear */ | |
860 | 0, /* tp_richcompare */ | |
861 | 0, /* tp_weaklistoffset */ | |
862 | 0, /* tp_iter */ | |
863 | 0, /* tp_iternext */ | |
864 | 0, /* tp_methods */ | |
865 | 0, /* tp_members */ | |
866 | 0, /* tp_getset */ | |
867 | 0, /* tp_base */ | |
868 | 0, /* tp_dict */ | |
869 | 0, /* tp_descr_get */ | |
870 | 0, /* tp_descr_set */ | |
871 | offsetof (field_object, dict), /* tp_dictoffset */ | |
872 | 0, /* tp_init */ | |
873 | 0, /* tp_alloc */ | |
874 | 0, /* tp_new */ | |
875 | }; |