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