GDB (API)
|
00001 /* Python interface to types. 00002 00003 Copyright (C) 2008-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "value.h" 00022 #include "exceptions.h" 00023 #include "python-internal.h" 00024 #include "charset.h" 00025 #include "gdbtypes.h" 00026 #include "cp-support.h" 00027 #include "demangle.h" 00028 #include "objfiles.h" 00029 #include "language.h" 00030 #include "vec.h" 00031 #include "bcache.h" 00032 #include "dwarf2loc.h" 00033 #include "typeprint.h" 00034 00035 typedef struct pyty_type_object 00036 { 00037 PyObject_HEAD 00038 struct type *type; 00039 00040 /* If a Type object is associated with an objfile, it is kept on a 00041 doubly-linked list, rooted in the objfile. This lets us copy the 00042 underlying struct type when the objfile is deleted. */ 00043 struct pyty_type_object *prev; 00044 struct pyty_type_object *next; 00045 } type_object; 00046 00047 static PyTypeObject type_object_type 00048 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object"); 00049 00050 /* A Field object. */ 00051 typedef struct pyty_field_object 00052 { 00053 PyObject_HEAD 00054 00055 /* Dictionary holding our attributes. */ 00056 PyObject *dict; 00057 } field_object; 00058 00059 static PyTypeObject field_object_type 00060 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object"); 00061 00062 /* A type iterator object. */ 00063 typedef struct { 00064 PyObject_HEAD 00065 /* The current field index. */ 00066 int field; 00067 /* What to return. */ 00068 enum gdbpy_iter_kind kind; 00069 /* Pointer back to the original source type object. */ 00070 struct pyty_type_object *source; 00071 } typy_iterator_object; 00072 00073 static PyTypeObject type_iterator_object_type 00074 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object"); 00075 00076 /* This is used to initialize various gdb.TYPE_ constants. */ 00077 struct pyty_code 00078 { 00079 /* The code. */ 00080 enum type_code code; 00081 /* The name. */ 00082 const char *name; 00083 }; 00084 00085 /* Forward declarations. */ 00086 static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind); 00087 00088 #define ENTRY(X) { X, #X } 00089 00090 static struct pyty_code pyty_codes[] = 00091 { 00092 ENTRY (TYPE_CODE_BITSTRING), 00093 ENTRY (TYPE_CODE_PTR), 00094 ENTRY (TYPE_CODE_ARRAY), 00095 ENTRY (TYPE_CODE_STRUCT), 00096 ENTRY (TYPE_CODE_UNION), 00097 ENTRY (TYPE_CODE_ENUM), 00098 ENTRY (TYPE_CODE_FLAGS), 00099 ENTRY (TYPE_CODE_FUNC), 00100 ENTRY (TYPE_CODE_INT), 00101 ENTRY (TYPE_CODE_FLT), 00102 ENTRY (TYPE_CODE_VOID), 00103 ENTRY (TYPE_CODE_SET), 00104 ENTRY (TYPE_CODE_RANGE), 00105 ENTRY (TYPE_CODE_STRING), 00106 ENTRY (TYPE_CODE_ERROR), 00107 ENTRY (TYPE_CODE_METHOD), 00108 ENTRY (TYPE_CODE_METHODPTR), 00109 ENTRY (TYPE_CODE_MEMBERPTR), 00110 ENTRY (TYPE_CODE_REF), 00111 ENTRY (TYPE_CODE_CHAR), 00112 ENTRY (TYPE_CODE_BOOL), 00113 ENTRY (TYPE_CODE_COMPLEX), 00114 ENTRY (TYPE_CODE_TYPEDEF), 00115 ENTRY (TYPE_CODE_NAMESPACE), 00116 ENTRY (TYPE_CODE_DECFLOAT), 00117 ENTRY (TYPE_CODE_INTERNAL_FUNCTION), 00118 { TYPE_CODE_UNDEF, NULL } 00119 }; 00120 00121 00122 00123 static void 00124 field_dealloc (PyObject *obj) 00125 { 00126 field_object *f = (field_object *) obj; 00127 00128 Py_XDECREF (f->dict); 00129 Py_TYPE (obj)->tp_free (obj); 00130 } 00131 00132 static PyObject * 00133 field_new (void) 00134 { 00135 field_object *result = PyObject_New (field_object, &field_object_type); 00136 00137 if (result) 00138 { 00139 result->dict = PyDict_New (); 00140 if (!result->dict) 00141 { 00142 Py_DECREF (result); 00143 result = NULL; 00144 } 00145 } 00146 return (PyObject *) result; 00147 } 00148 00149 00150 00151 /* Return the code for this type. */ 00152 static PyObject * 00153 typy_get_code (PyObject *self, void *closure) 00154 { 00155 struct type *type = ((type_object *) self)->type; 00156 00157 return PyInt_FromLong (TYPE_CODE (type)); 00158 } 00159 00160 /* Helper function for typy_fields which converts a single field to a 00161 gdb.Field object. Returns NULL on error. */ 00162 00163 static PyObject * 00164 convert_field (struct type *type, int field) 00165 { 00166 PyObject *result = field_new (); 00167 PyObject *arg; 00168 00169 if (!result) 00170 return NULL; 00171 00172 if (!field_is_static (&TYPE_FIELD (type, field))) 00173 { 00174 const char *attrstring; 00175 00176 if (TYPE_CODE (type) == TYPE_CODE_ENUM) 00177 { 00178 arg = gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type, field)); 00179 attrstring = "enumval"; 00180 } 00181 else 00182 { 00183 arg = gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type, field)); 00184 attrstring = "bitpos"; 00185 } 00186 00187 if (!arg) 00188 goto fail; 00189 00190 /* At least python-2.4 had the second parameter non-const. */ 00191 if (PyObject_SetAttrString (result, (char *) attrstring, arg) < 0) 00192 goto failarg; 00193 Py_DECREF (arg); 00194 } 00195 00196 if (TYPE_FIELD_NAME (type, field)) 00197 arg = PyString_FromString (TYPE_FIELD_NAME (type, field)); 00198 else 00199 { 00200 arg = Py_None; 00201 Py_INCREF (arg); 00202 } 00203 if (!arg) 00204 goto fail; 00205 if (PyObject_SetAttrString (result, "name", arg) < 0) 00206 goto failarg; 00207 Py_DECREF (arg); 00208 00209 arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False; 00210 Py_INCREF (arg); 00211 if (PyObject_SetAttrString (result, "artificial", arg) < 0) 00212 goto failarg; 00213 Py_DECREF (arg); 00214 00215 if (TYPE_CODE (type) == TYPE_CODE_CLASS) 00216 arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False; 00217 else 00218 arg = Py_False; 00219 Py_INCREF (arg); 00220 if (PyObject_SetAttrString (result, "is_base_class", arg) < 0) 00221 goto failarg; 00222 Py_DECREF (arg); 00223 00224 arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field)); 00225 if (!arg) 00226 goto fail; 00227 if (PyObject_SetAttrString (result, "bitsize", arg) < 0) 00228 goto failarg; 00229 Py_DECREF (arg); 00230 00231 /* A field can have a NULL type in some situations. */ 00232 if (TYPE_FIELD_TYPE (type, field) == NULL) 00233 { 00234 arg = Py_None; 00235 Py_INCREF (arg); 00236 } 00237 else 00238 arg = type_to_type_object (TYPE_FIELD_TYPE (type, field)); 00239 if (!arg) 00240 goto fail; 00241 if (PyObject_SetAttrString (result, "type", arg) < 0) 00242 goto failarg; 00243 Py_DECREF (arg); 00244 00245 return result; 00246 00247 failarg: 00248 Py_DECREF (arg); 00249 fail: 00250 Py_DECREF (result); 00251 return NULL; 00252 } 00253 00254 /* Helper function to return the name of a field, as a gdb.Field object. 00255 If the field doesn't have a name, None is returned. */ 00256 00257 static PyObject * 00258 field_name (struct type *type, int field) 00259 { 00260 PyObject *result; 00261 00262 if (TYPE_FIELD_NAME (type, field)) 00263 result = PyString_FromString (TYPE_FIELD_NAME (type, field)); 00264 else 00265 { 00266 result = Py_None; 00267 Py_INCREF (result); 00268 } 00269 return result; 00270 } 00271 00272 /* Helper function for Type standard mapping methods. Returns a 00273 Python object for field i of the type. "kind" specifies what to 00274 return: the name of the field, a gdb.Field object corresponding to 00275 the field, or a tuple consisting of field name and gdb.Field 00276 object. */ 00277 00278 static PyObject * 00279 make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind) 00280 { 00281 PyObject *item = NULL, *key = NULL, *value = NULL; 00282 00283 switch (kind) 00284 { 00285 case iter_items: 00286 key = field_name (type, i); 00287 if (key == NULL) 00288 goto fail; 00289 value = convert_field (type, i); 00290 if (value == NULL) 00291 goto fail; 00292 item = PyTuple_New (2); 00293 if (item == NULL) 00294 goto fail; 00295 PyTuple_SET_ITEM (item, 0, key); 00296 PyTuple_SET_ITEM (item, 1, value); 00297 break; 00298 case iter_keys: 00299 item = field_name (type, i); 00300 break; 00301 case iter_values: 00302 item = convert_field (type, i); 00303 break; 00304 default: 00305 gdb_assert_not_reached ("invalid gdbpy_iter_kind"); 00306 } 00307 return item; 00308 00309 fail: 00310 Py_XDECREF (key); 00311 Py_XDECREF (value); 00312 Py_XDECREF (item); 00313 return NULL; 00314 } 00315 00316 /* Return a sequence of all field names, fields, or (name, field) pairs. 00317 Each field is a gdb.Field object. */ 00318 00319 static PyObject * 00320 typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind) 00321 { 00322 PyObject *py_type = self; 00323 PyObject *result = NULL, *iter = NULL; 00324 volatile struct gdb_exception except; 00325 struct type *type = ((type_object *) py_type)->type; 00326 struct type *checked_type = type; 00327 00328 TRY_CATCH (except, RETURN_MASK_ALL) 00329 { 00330 CHECK_TYPEDEF (checked_type); 00331 } 00332 GDB_PY_HANDLE_EXCEPTION (except); 00333 00334 if (checked_type != type) 00335 py_type = type_to_type_object (checked_type); 00336 iter = typy_make_iter (py_type, kind); 00337 if (checked_type != type) 00338 { 00339 /* Need to wrap this in braces because Py_DECREF isn't wrapped 00340 in a do{}while(0). */ 00341 Py_DECREF (py_type); 00342 } 00343 if (iter != NULL) 00344 { 00345 result = PySequence_List (iter); 00346 Py_DECREF (iter); 00347 } 00348 00349 return result; 00350 } 00351 00352 /* Return a sequence of all fields. Each field is a gdb.Field object. */ 00353 00354 static PyObject * 00355 typy_values (PyObject *self, PyObject *args) 00356 { 00357 return typy_fields_items (self, iter_values); 00358 } 00359 00360 /* Return a sequence of all fields. Each field is a gdb.Field object. 00361 This method is similar to typy_values, except where the supplied 00362 gdb.Type is an array, in which case it returns a list of one entry 00363 which is a gdb.Field object for a range (the array bounds). */ 00364 00365 static PyObject * 00366 typy_fields (PyObject *self, PyObject *args) 00367 { 00368 struct type *type = ((type_object *) self)->type; 00369 PyObject *r, *rl; 00370 00371 if (TYPE_CODE (type) != TYPE_CODE_ARRAY) 00372 return typy_fields_items (self, iter_values); 00373 00374 /* Array type. Handle this as a special case because the common 00375 machinery wants struct or union or enum types. Build a list of 00376 one entry which is the range for the array. */ 00377 r = convert_field (type, 0); 00378 if (r == NULL) 00379 return NULL; 00380 00381 rl = Py_BuildValue ("[O]", r); 00382 Py_DECREF (r); 00383 00384 return rl; 00385 } 00386 00387 /* Return a sequence of all field names. Each field is a gdb.Field object. */ 00388 00389 static PyObject * 00390 typy_field_names (PyObject *self, PyObject *args) 00391 { 00392 return typy_fields_items (self, iter_keys); 00393 } 00394 00395 /* Return a sequence of all (name, fields) pairs. Each field is a 00396 gdb.Field object. */ 00397 00398 static PyObject * 00399 typy_items (PyObject *self, PyObject *args) 00400 { 00401 return typy_fields_items (self, iter_items); 00402 } 00403 00404 /* Return the type's tag, or None. */ 00405 static PyObject * 00406 typy_get_tag (PyObject *self, void *closure) 00407 { 00408 struct type *type = ((type_object *) self)->type; 00409 00410 if (!TYPE_TAG_NAME (type)) 00411 Py_RETURN_NONE; 00412 return PyString_FromString (TYPE_TAG_NAME (type)); 00413 } 00414 00415 /* Return the type, stripped of typedefs. */ 00416 static PyObject * 00417 typy_strip_typedefs (PyObject *self, PyObject *args) 00418 { 00419 struct type *type = ((type_object *) self)->type; 00420 volatile struct gdb_exception except; 00421 00422 TRY_CATCH (except, RETURN_MASK_ALL) 00423 { 00424 type = check_typedef (type); 00425 } 00426 GDB_PY_HANDLE_EXCEPTION (except); 00427 00428 return type_to_type_object (type); 00429 } 00430 00431 /* Strip typedefs and pointers/reference from a type. Then check that 00432 it is a struct, union, or enum type. If not, raise TypeError. */ 00433 00434 static struct type * 00435 typy_get_composite (struct type *type) 00436 { 00437 volatile struct gdb_exception except; 00438 00439 for (;;) 00440 { 00441 TRY_CATCH (except, RETURN_MASK_ALL) 00442 { 00443 CHECK_TYPEDEF (type); 00444 } 00445 GDB_PY_HANDLE_EXCEPTION (except); 00446 00447 if (TYPE_CODE (type) != TYPE_CODE_PTR 00448 && TYPE_CODE (type) != TYPE_CODE_REF) 00449 break; 00450 type = TYPE_TARGET_TYPE (type); 00451 } 00452 00453 /* If this is not a struct, union, or enum type, raise TypeError 00454 exception. */ 00455 if (TYPE_CODE (type) != TYPE_CODE_STRUCT 00456 && TYPE_CODE (type) != TYPE_CODE_UNION 00457 && TYPE_CODE (type) != TYPE_CODE_ENUM) 00458 { 00459 PyErr_SetString (PyExc_TypeError, 00460 "Type is not a structure, union, or enum type."); 00461 return NULL; 00462 } 00463 00464 return type; 00465 } 00466 00467 /* Helper for typy_array and typy_vector. */ 00468 00469 static PyObject * 00470 typy_array_1 (PyObject *self, PyObject *args, int is_vector) 00471 { 00472 long n1, n2; 00473 PyObject *n2_obj = NULL; 00474 struct type *array = NULL; 00475 struct type *type = ((type_object *) self)->type; 00476 volatile struct gdb_exception except; 00477 00478 if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj)) 00479 return NULL; 00480 00481 if (n2_obj) 00482 { 00483 if (!PyInt_Check (n2_obj)) 00484 { 00485 PyErr_SetString (PyExc_RuntimeError, 00486 _("Array bound must be an integer")); 00487 return NULL; 00488 } 00489 00490 if (! gdb_py_int_as_long (n2_obj, &n2)) 00491 return NULL; 00492 } 00493 else 00494 { 00495 n2 = n1; 00496 n1 = 0; 00497 } 00498 00499 if (n2 < n1) 00500 { 00501 PyErr_SetString (PyExc_ValueError, 00502 _("Array length must not be negative")); 00503 return NULL; 00504 } 00505 00506 TRY_CATCH (except, RETURN_MASK_ALL) 00507 { 00508 array = lookup_array_range_type (type, n1, n2); 00509 if (is_vector) 00510 make_vector_type (array); 00511 } 00512 GDB_PY_HANDLE_EXCEPTION (except); 00513 00514 return type_to_type_object (array); 00515 } 00516 00517 /* Return an array type. */ 00518 00519 static PyObject * 00520 typy_array (PyObject *self, PyObject *args) 00521 { 00522 return typy_array_1 (self, args, 0); 00523 } 00524 00525 /* Return a vector type. */ 00526 00527 static PyObject * 00528 typy_vector (PyObject *self, PyObject *args) 00529 { 00530 return typy_array_1 (self, args, 1); 00531 } 00532 00533 /* Return a Type object which represents a pointer to SELF. */ 00534 static PyObject * 00535 typy_pointer (PyObject *self, PyObject *args) 00536 { 00537 struct type *type = ((type_object *) self)->type; 00538 volatile struct gdb_exception except; 00539 00540 TRY_CATCH (except, RETURN_MASK_ALL) 00541 { 00542 type = lookup_pointer_type (type); 00543 } 00544 GDB_PY_HANDLE_EXCEPTION (except); 00545 00546 return type_to_type_object (type); 00547 } 00548 00549 /* Return the range of a type represented by SELF. The return type is 00550 a tuple. The first element of the tuple contains the low bound, 00551 while the second element of the tuple contains the high bound. */ 00552 static PyObject * 00553 typy_range (PyObject *self, PyObject *args) 00554 { 00555 struct type *type = ((type_object *) self)->type; 00556 PyObject *result; 00557 PyObject *low_bound = NULL, *high_bound = NULL; 00558 /* Initialize these to appease GCC warnings. */ 00559 LONGEST low = 0, high = 0; 00560 00561 if (TYPE_CODE (type) != TYPE_CODE_ARRAY 00562 && TYPE_CODE (type) != TYPE_CODE_STRING 00563 && TYPE_CODE (type) != TYPE_CODE_RANGE) 00564 { 00565 PyErr_SetString (PyExc_RuntimeError, 00566 _("This type does not have a range.")); 00567 return NULL; 00568 } 00569 00570 switch (TYPE_CODE (type)) 00571 { 00572 case TYPE_CODE_ARRAY: 00573 case TYPE_CODE_STRING: 00574 low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); 00575 high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)); 00576 break; 00577 case TYPE_CODE_RANGE: 00578 low = TYPE_LOW_BOUND (type); 00579 high = TYPE_HIGH_BOUND (type); 00580 break; 00581 } 00582 00583 low_bound = PyLong_FromLong (low); 00584 if (!low_bound) 00585 goto failarg; 00586 00587 high_bound = PyLong_FromLong (high); 00588 if (!high_bound) 00589 goto failarg; 00590 00591 result = PyTuple_New (2); 00592 if (!result) 00593 goto failarg; 00594 00595 if (PyTuple_SetItem (result, 0, low_bound) != 0) 00596 { 00597 Py_DECREF (result); 00598 goto failarg; 00599 } 00600 if (PyTuple_SetItem (result, 1, high_bound) != 0) 00601 { 00602 Py_DECREF (high_bound); 00603 Py_DECREF (result); 00604 return NULL; 00605 } 00606 return result; 00607 00608 failarg: 00609 Py_XDECREF (high_bound); 00610 Py_XDECREF (low_bound); 00611 return NULL; 00612 } 00613 00614 /* Return a Type object which represents a reference to SELF. */ 00615 static PyObject * 00616 typy_reference (PyObject *self, PyObject *args) 00617 { 00618 struct type *type = ((type_object *) self)->type; 00619 volatile struct gdb_exception except; 00620 00621 TRY_CATCH (except, RETURN_MASK_ALL) 00622 { 00623 type = lookup_reference_type (type); 00624 } 00625 GDB_PY_HANDLE_EXCEPTION (except); 00626 00627 return type_to_type_object (type); 00628 } 00629 00630 /* Return a Type object which represents the target type of SELF. */ 00631 static PyObject * 00632 typy_target (PyObject *self, PyObject *args) 00633 { 00634 struct type *type = ((type_object *) self)->type; 00635 00636 if (!TYPE_TARGET_TYPE (type)) 00637 { 00638 PyErr_SetString (PyExc_RuntimeError, 00639 _("Type does not have a target.")); 00640 return NULL; 00641 } 00642 00643 return type_to_type_object (TYPE_TARGET_TYPE (type)); 00644 } 00645 00646 /* Return a const-qualified type variant. */ 00647 static PyObject * 00648 typy_const (PyObject *self, PyObject *args) 00649 { 00650 struct type *type = ((type_object *) self)->type; 00651 volatile struct gdb_exception except; 00652 00653 TRY_CATCH (except, RETURN_MASK_ALL) 00654 { 00655 type = make_cv_type (1, 0, type, NULL); 00656 } 00657 GDB_PY_HANDLE_EXCEPTION (except); 00658 00659 return type_to_type_object (type); 00660 } 00661 00662 /* Return a volatile-qualified type variant. */ 00663 static PyObject * 00664 typy_volatile (PyObject *self, PyObject *args) 00665 { 00666 struct type *type = ((type_object *) self)->type; 00667 volatile struct gdb_exception except; 00668 00669 TRY_CATCH (except, RETURN_MASK_ALL) 00670 { 00671 type = make_cv_type (0, 1, type, NULL); 00672 } 00673 GDB_PY_HANDLE_EXCEPTION (except); 00674 00675 return type_to_type_object (type); 00676 } 00677 00678 /* Return an unqualified type variant. */ 00679 static PyObject * 00680 typy_unqualified (PyObject *self, PyObject *args) 00681 { 00682 struct type *type = ((type_object *) self)->type; 00683 volatile struct gdb_exception except; 00684 00685 TRY_CATCH (except, RETURN_MASK_ALL) 00686 { 00687 type = make_cv_type (0, 0, type, NULL); 00688 } 00689 GDB_PY_HANDLE_EXCEPTION (except); 00690 00691 return type_to_type_object (type); 00692 } 00693 00694 /* Return the size of the type represented by SELF, in bytes. */ 00695 static PyObject * 00696 typy_get_sizeof (PyObject *self, void *closure) 00697 { 00698 struct type *type = ((type_object *) self)->type; 00699 volatile struct gdb_exception except; 00700 00701 TRY_CATCH (except, RETURN_MASK_ALL) 00702 { 00703 check_typedef (type); 00704 } 00705 /* Ignore exceptions. */ 00706 00707 return gdb_py_long_from_longest (TYPE_LENGTH (type)); 00708 } 00709 00710 static struct type * 00711 typy_lookup_typename (const char *type_name, const struct block *block) 00712 { 00713 struct type *type = NULL; 00714 volatile struct gdb_exception except; 00715 00716 TRY_CATCH (except, RETURN_MASK_ALL) 00717 { 00718 if (!strncmp (type_name, "struct ", 7)) 00719 type = lookup_struct (type_name + 7, NULL); 00720 else if (!strncmp (type_name, "union ", 6)) 00721 type = lookup_union (type_name + 6, NULL); 00722 else if (!strncmp (type_name, "enum ", 5)) 00723 type = lookup_enum (type_name + 5, NULL); 00724 else 00725 type = lookup_typename (python_language, python_gdbarch, 00726 type_name, block, 0); 00727 } 00728 GDB_PY_HANDLE_EXCEPTION (except); 00729 00730 return type; 00731 } 00732 00733 static struct type * 00734 typy_lookup_type (struct demangle_component *demangled, 00735 const struct block *block) 00736 { 00737 struct type *type, *rtype = NULL; 00738 char *type_name = NULL; 00739 enum demangle_component_type demangled_type; 00740 volatile struct gdb_exception except; 00741 00742 /* Save the type: typy_lookup_type() may (indirectly) overwrite 00743 memory pointed by demangled. */ 00744 demangled_type = demangled->type; 00745 00746 if (demangled_type == DEMANGLE_COMPONENT_POINTER 00747 || demangled_type == DEMANGLE_COMPONENT_REFERENCE 00748 || demangled_type == DEMANGLE_COMPONENT_CONST 00749 || demangled_type == DEMANGLE_COMPONENT_VOLATILE) 00750 { 00751 type = typy_lookup_type (demangled->u.s_binary.left, block); 00752 if (! type) 00753 return NULL; 00754 00755 TRY_CATCH (except, RETURN_MASK_ALL) 00756 { 00757 /* If the demangled_type matches with one of the types 00758 below, run the corresponding function and save the type 00759 to return later. We cannot just return here as we are in 00760 an exception handler. */ 00761 switch (demangled_type) 00762 { 00763 case DEMANGLE_COMPONENT_REFERENCE: 00764 rtype = lookup_reference_type (type); 00765 break; 00766 case DEMANGLE_COMPONENT_POINTER: 00767 rtype = lookup_pointer_type (type); 00768 break; 00769 case DEMANGLE_COMPONENT_CONST: 00770 rtype = make_cv_type (1, 0, type, NULL); 00771 break; 00772 case DEMANGLE_COMPONENT_VOLATILE: 00773 rtype = make_cv_type (0, 1, type, NULL); 00774 break; 00775 } 00776 } 00777 GDB_PY_HANDLE_EXCEPTION (except); 00778 } 00779 00780 /* If we have a type from the switch statement above, just return 00781 that. */ 00782 if (rtype) 00783 return rtype; 00784 00785 /* We don't have a type, so lookup the type. */ 00786 type_name = cp_comp_to_string (demangled, 10); 00787 type = typy_lookup_typename (type_name, block); 00788 xfree (type_name); 00789 00790 return type; 00791 } 00792 00793 /* This is a helper function for typy_template_argument that is used 00794 when the type does not have template symbols attached. It works by 00795 parsing the type name. This happens with compilers, like older 00796 versions of GCC, that do not emit DW_TAG_template_*. */ 00797 00798 static PyObject * 00799 typy_legacy_template_argument (struct type *type, const struct block *block, 00800 int argno) 00801 { 00802 int i; 00803 struct demangle_component *demangled; 00804 struct demangle_parse_info *info = NULL; 00805 const char *err; 00806 struct type *argtype; 00807 struct cleanup *cleanup; 00808 volatile struct gdb_exception except; 00809 00810 if (TYPE_NAME (type) == NULL) 00811 { 00812 PyErr_SetString (PyExc_RuntimeError, _("Null type name.")); 00813 return NULL; 00814 } 00815 00816 TRY_CATCH (except, RETURN_MASK_ALL) 00817 { 00818 /* Note -- this is not thread-safe. */ 00819 info = cp_demangled_name_to_comp (TYPE_NAME (type), &err); 00820 } 00821 GDB_PY_HANDLE_EXCEPTION (except); 00822 00823 if (! info) 00824 { 00825 PyErr_SetString (PyExc_RuntimeError, err); 00826 return NULL; 00827 } 00828 demangled = info->tree; 00829 cleanup = make_cleanup_cp_demangled_name_parse_free (info); 00830 00831 /* Strip off component names. */ 00832 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME 00833 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) 00834 demangled = demangled->u.s_binary.right; 00835 00836 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) 00837 { 00838 do_cleanups (cleanup); 00839 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template.")); 00840 return NULL; 00841 } 00842 00843 /* Skip from the template to the arguments. */ 00844 demangled = demangled->u.s_binary.right; 00845 00846 for (i = 0; demangled && i < argno; ++i) 00847 demangled = demangled->u.s_binary.right; 00848 00849 if (! demangled) 00850 { 00851 do_cleanups (cleanup); 00852 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), 00853 argno); 00854 return NULL; 00855 } 00856 00857 argtype = typy_lookup_type (demangled->u.s_binary.left, block); 00858 do_cleanups (cleanup); 00859 if (! argtype) 00860 return NULL; 00861 00862 return type_to_type_object (argtype); 00863 } 00864 00865 static PyObject * 00866 typy_template_argument (PyObject *self, PyObject *args) 00867 { 00868 int argno; 00869 struct type *type = ((type_object *) self)->type; 00870 const struct block *block = NULL; 00871 PyObject *block_obj = NULL; 00872 struct symbol *sym; 00873 struct value *val = NULL; 00874 volatile struct gdb_exception except; 00875 00876 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj)) 00877 return NULL; 00878 00879 if (block_obj) 00880 { 00881 block = block_object_to_block (block_obj); 00882 if (! block) 00883 { 00884 PyErr_SetString (PyExc_RuntimeError, 00885 _("Second argument must be block.")); 00886 return NULL; 00887 } 00888 } 00889 00890 TRY_CATCH (except, RETURN_MASK_ALL) 00891 { 00892 type = check_typedef (type); 00893 if (TYPE_CODE (type) == TYPE_CODE_REF) 00894 type = check_typedef (TYPE_TARGET_TYPE (type)); 00895 } 00896 GDB_PY_HANDLE_EXCEPTION (except); 00897 00898 /* We might not have DW_TAG_template_*, so try to parse the type's 00899 name. This is inefficient if we do not have a template type -- 00900 but that is going to wind up as an error anyhow. */ 00901 if (! TYPE_N_TEMPLATE_ARGUMENTS (type)) 00902 return typy_legacy_template_argument (type, block, argno); 00903 00904 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type)) 00905 { 00906 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), 00907 argno); 00908 return NULL; 00909 } 00910 00911 sym = TYPE_TEMPLATE_ARGUMENT (type, argno); 00912 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) 00913 return type_to_type_object (SYMBOL_TYPE (sym)); 00914 else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT) 00915 { 00916 PyErr_Format (PyExc_RuntimeError, 00917 _("Template argument is optimized out")); 00918 return NULL; 00919 } 00920 00921 TRY_CATCH (except, RETURN_MASK_ALL) 00922 { 00923 val = value_of_variable (sym, block); 00924 } 00925 GDB_PY_HANDLE_EXCEPTION (except); 00926 00927 return value_to_value_object (val); 00928 } 00929 00930 static PyObject * 00931 typy_str (PyObject *self) 00932 { 00933 volatile struct gdb_exception except; 00934 char *thetype = NULL; 00935 long length = 0; 00936 PyObject *result; 00937 00938 TRY_CATCH (except, RETURN_MASK_ALL) 00939 { 00940 struct cleanup *old_chain; 00941 struct ui_file *stb; 00942 00943 stb = mem_fileopen (); 00944 old_chain = make_cleanup_ui_file_delete (stb); 00945 00946 LA_PRINT_TYPE (type_object_to_type (self), "", stb, -1, 0, 00947 &type_print_raw_options); 00948 00949 thetype = ui_file_xstrdup (stb, &length); 00950 do_cleanups (old_chain); 00951 } 00952 if (except.reason < 0) 00953 { 00954 xfree (thetype); 00955 GDB_PY_HANDLE_EXCEPTION (except); 00956 } 00957 00958 result = PyUnicode_Decode (thetype, length, host_charset (), NULL); 00959 xfree (thetype); 00960 00961 return result; 00962 } 00963 00964 /* An entry in the type-equality bcache. */ 00965 00966 typedef struct type_equality_entry 00967 { 00968 struct type *type1, *type2; 00969 } type_equality_entry_d; 00970 00971 DEF_VEC_O (type_equality_entry_d); 00972 00973 /* A helper function to compare two strings. Returns 1 if they are 00974 the same, 0 otherwise. Handles NULLs properly. */ 00975 00976 static int 00977 compare_maybe_null_strings (const char *s, const char *t) 00978 { 00979 if (s == NULL && t != NULL) 00980 return 0; 00981 else if (s != NULL && t == NULL) 00982 return 0; 00983 else if (s == NULL && t== NULL) 00984 return 1; 00985 return strcmp (s, t) == 0; 00986 } 00987 00988 /* A helper function for typy_richcompare that checks two types for 00989 "deep" equality. Returns Py_EQ if the types are considered the 00990 same, Py_NE otherwise. */ 00991 00992 static int 00993 check_types_equal (struct type *type1, struct type *type2, 00994 VEC (type_equality_entry_d) **worklist) 00995 { 00996 CHECK_TYPEDEF (type1); 00997 CHECK_TYPEDEF (type2); 00998 00999 if (type1 == type2) 01000 return Py_EQ; 01001 01002 if (TYPE_CODE (type1) != TYPE_CODE (type2) 01003 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2) 01004 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2) 01005 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2) 01006 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2) 01007 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2) 01008 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2) 01009 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2) 01010 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2)) 01011 return Py_NE; 01012 01013 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1), 01014 TYPE_TAG_NAME (type2))) 01015 return Py_NE; 01016 if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2))) 01017 return Py_NE; 01018 01019 if (TYPE_CODE (type1) == TYPE_CODE_RANGE) 01020 { 01021 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2), 01022 sizeof (*TYPE_RANGE_DATA (type1))) != 0) 01023 return Py_NE; 01024 } 01025 else 01026 { 01027 int i; 01028 01029 for (i = 0; i < TYPE_NFIELDS (type1); ++i) 01030 { 01031 const struct field *field1 = &TYPE_FIELD (type1, i); 01032 const struct field *field2 = &TYPE_FIELD (type2, i); 01033 struct type_equality_entry entry; 01034 01035 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2) 01036 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2) 01037 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2)) 01038 return Py_NE; 01039 if (!compare_maybe_null_strings (FIELD_NAME (*field1), 01040 FIELD_NAME (*field2))) 01041 return Py_NE; 01042 switch (FIELD_LOC_KIND (*field1)) 01043 { 01044 case FIELD_LOC_KIND_BITPOS: 01045 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2)) 01046 return Py_NE; 01047 break; 01048 case FIELD_LOC_KIND_ENUMVAL: 01049 if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2)) 01050 return Py_NE; 01051 break; 01052 case FIELD_LOC_KIND_PHYSADDR: 01053 if (FIELD_STATIC_PHYSADDR (*field1) 01054 != FIELD_STATIC_PHYSADDR (*field2)) 01055 return Py_NE; 01056 break; 01057 case FIELD_LOC_KIND_PHYSNAME: 01058 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1), 01059 FIELD_STATIC_PHYSNAME (*field2))) 01060 return Py_NE; 01061 break; 01062 case FIELD_LOC_KIND_DWARF_BLOCK: 01063 { 01064 struct dwarf2_locexpr_baton *block1, *block2; 01065 01066 block1 = FIELD_DWARF_BLOCK (*field1); 01067 block2 = FIELD_DWARF_BLOCK (*field2); 01068 if (block1->per_cu != block2->per_cu 01069 || block1->size != block2->size 01070 || memcmp (block1->data, block2->data, block1->size) != 0) 01071 return Py_NE; 01072 } 01073 break; 01074 default: 01075 internal_error (__FILE__, __LINE__, _("Unsupported field kind " 01076 "%d by check_types_equal"), 01077 FIELD_LOC_KIND (*field1)); 01078 } 01079 01080 entry.type1 = FIELD_TYPE (*field1); 01081 entry.type2 = FIELD_TYPE (*field2); 01082 VEC_safe_push (type_equality_entry_d, *worklist, &entry); 01083 } 01084 } 01085 01086 if (TYPE_TARGET_TYPE (type1) != NULL) 01087 { 01088 struct type_equality_entry entry; 01089 01090 if (TYPE_TARGET_TYPE (type2) == NULL) 01091 return Py_NE; 01092 01093 entry.type1 = TYPE_TARGET_TYPE (type1); 01094 entry.type2 = TYPE_TARGET_TYPE (type2); 01095 VEC_safe_push (type_equality_entry_d, *worklist, &entry); 01096 } 01097 else if (TYPE_TARGET_TYPE (type2) != NULL) 01098 return Py_NE; 01099 01100 return Py_EQ; 01101 } 01102 01103 /* Check types on a worklist for equality. Returns Py_NE if any pair 01104 is not equal, Py_EQ if they are all considered equal. */ 01105 01106 static int 01107 check_types_worklist (VEC (type_equality_entry_d) **worklist, 01108 struct bcache *cache) 01109 { 01110 while (!VEC_empty (type_equality_entry_d, *worklist)) 01111 { 01112 struct type_equality_entry entry; 01113 int added; 01114 01115 entry = *VEC_last (type_equality_entry_d, *worklist); 01116 VEC_pop (type_equality_entry_d, *worklist); 01117 01118 /* If the type pair has already been visited, we know it is 01119 ok. */ 01120 bcache_full (&entry, sizeof (entry), cache, &added); 01121 if (!added) 01122 continue; 01123 01124 if (check_types_equal (entry.type1, entry.type2, worklist) == Py_NE) 01125 return Py_NE; 01126 } 01127 01128 return Py_EQ; 01129 } 01130 01131 /* Implement the richcompare method. */ 01132 01133 static PyObject * 01134 typy_richcompare (PyObject *self, PyObject *other, int op) 01135 { 01136 int result = Py_NE; 01137 struct type *type1 = type_object_to_type (self); 01138 struct type *type2 = type_object_to_type (other); 01139 volatile struct gdb_exception except; 01140 01141 /* We can only compare ourselves to another Type object, and only 01142 for equality or inequality. */ 01143 if (type2 == NULL || (op != Py_EQ && op != Py_NE)) 01144 { 01145 Py_INCREF (Py_NotImplemented); 01146 return Py_NotImplemented; 01147 } 01148 01149 if (type1 == type2) 01150 result = Py_EQ; 01151 else 01152 { 01153 struct bcache *cache; 01154 VEC (type_equality_entry_d) *worklist = NULL; 01155 struct type_equality_entry entry; 01156 01157 cache = bcache_xmalloc (NULL, NULL); 01158 01159 entry.type1 = type1; 01160 entry.type2 = type2; 01161 VEC_safe_push (type_equality_entry_d, worklist, &entry); 01162 01163 TRY_CATCH (except, RETURN_MASK_ALL) 01164 { 01165 result = check_types_worklist (&worklist, cache); 01166 } 01167 /* check_types_worklist calls several nested Python helper 01168 functions, some of which can raise a GDB Exception, so we 01169 just check and convert here. If there is a GDB exception, a 01170 comparison is not capable (or trusted), so exit. */ 01171 bcache_xfree (cache); 01172 VEC_free (type_equality_entry_d, worklist); 01173 GDB_PY_HANDLE_EXCEPTION (except); 01174 } 01175 01176 if (op == result) 01177 Py_RETURN_TRUE; 01178 Py_RETURN_FALSE; 01179 } 01180 01181 01182 01183 static const struct objfile_data *typy_objfile_data_key; 01184 01185 static void 01186 save_objfile_types (struct objfile *objfile, void *datum) 01187 { 01188 type_object *obj = datum; 01189 htab_t copied_types; 01190 struct cleanup *cleanup; 01191 01192 if (!gdb_python_initialized) 01193 return; 01194 01195 /* This prevents another thread from freeing the objects we're 01196 operating on. */ 01197 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); 01198 01199 copied_types = create_copied_types_hash (objfile); 01200 01201 while (obj) 01202 { 01203 type_object *next = obj->next; 01204 01205 htab_empty (copied_types); 01206 01207 obj->type = copy_type_recursive (objfile, obj->type, copied_types); 01208 01209 obj->next = NULL; 01210 obj->prev = NULL; 01211 01212 obj = next; 01213 } 01214 01215 htab_delete (copied_types); 01216 01217 do_cleanups (cleanup); 01218 } 01219 01220 static void 01221 set_type (type_object *obj, struct type *type) 01222 { 01223 obj->type = type; 01224 obj->prev = NULL; 01225 if (type && TYPE_OBJFILE (type)) 01226 { 01227 struct objfile *objfile = TYPE_OBJFILE (type); 01228 01229 obj->next = objfile_data (objfile, typy_objfile_data_key); 01230 if (obj->next) 01231 obj->next->prev = obj; 01232 set_objfile_data (objfile, typy_objfile_data_key, obj); 01233 } 01234 else 01235 obj->next = NULL; 01236 } 01237 01238 static void 01239 typy_dealloc (PyObject *obj) 01240 { 01241 type_object *type = (type_object *) obj; 01242 01243 if (type->prev) 01244 type->prev->next = type->next; 01245 else if (type->type && TYPE_OBJFILE (type->type)) 01246 { 01247 /* Must reset head of list. */ 01248 struct objfile *objfile = TYPE_OBJFILE (type->type); 01249 01250 if (objfile) 01251 set_objfile_data (objfile, typy_objfile_data_key, type->next); 01252 } 01253 if (type->next) 01254 type->next->prev = type->prev; 01255 01256 Py_TYPE (type)->tp_free (type); 01257 } 01258 01259 /* Return number of fields ("length" of the field dictionary). */ 01260 01261 static Py_ssize_t 01262 typy_length (PyObject *self) 01263 { 01264 struct type *type = ((type_object *) self)->type; 01265 01266 type = typy_get_composite (type); 01267 if (type == NULL) 01268 return -1; 01269 01270 return TYPE_NFIELDS (type); 01271 } 01272 01273 /* Implements boolean evaluation of gdb.Type. Handle this like other 01274 Python objects that don't have a meaningful truth value -- all 01275 values are true. */ 01276 01277 static int 01278 typy_nonzero (PyObject *self) 01279 { 01280 return 1; 01281 } 01282 01283 /* Return a gdb.Field object for the field named by the argument. */ 01284 01285 static PyObject * 01286 typy_getitem (PyObject *self, PyObject *key) 01287 { 01288 struct type *type = ((type_object *) self)->type; 01289 char *field; 01290 int i; 01291 01292 field = python_string_to_host_string (key); 01293 if (field == NULL) 01294 return NULL; 01295 01296 /* We want just fields of this type, not of base types, so instead of 01297 using lookup_struct_elt_type, portions of that function are 01298 copied here. */ 01299 01300 type = typy_get_composite (type); 01301 if (type == NULL) 01302 return NULL; 01303 01304 for (i = 0; i < TYPE_NFIELDS (type); i++) 01305 { 01306 const char *t_field_name = TYPE_FIELD_NAME (type, i); 01307 01308 if (t_field_name && (strcmp_iw (t_field_name, field) == 0)) 01309 { 01310 return convert_field (type, i); 01311 } 01312 } 01313 PyErr_SetObject (PyExc_KeyError, key); 01314 return NULL; 01315 } 01316 01317 /* Implement the "get" method on the type object. This is the 01318 same as getitem if the key is present, but returns the supplied 01319 default value or None if the key is not found. */ 01320 01321 static PyObject * 01322 typy_get (PyObject *self, PyObject *args) 01323 { 01324 PyObject *key, *defval = Py_None, *result; 01325 01326 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval)) 01327 return NULL; 01328 01329 result = typy_getitem (self, key); 01330 if (result != NULL) 01331 return result; 01332 01333 /* typy_getitem returned error status. If the exception is 01334 KeyError, clear the exception status and return the defval 01335 instead. Otherwise return the exception unchanged. */ 01336 if (!PyErr_ExceptionMatches (PyExc_KeyError)) 01337 return NULL; 01338 01339 PyErr_Clear (); 01340 Py_INCREF (defval); 01341 return defval; 01342 } 01343 01344 /* Implement the "has_key" method on the type object. */ 01345 01346 static PyObject * 01347 typy_has_key (PyObject *self, PyObject *args) 01348 { 01349 struct type *type = ((type_object *) self)->type; 01350 const char *field; 01351 int i; 01352 01353 if (!PyArg_ParseTuple (args, "s", &field)) 01354 return NULL; 01355 01356 /* We want just fields of this type, not of base types, so instead of 01357 using lookup_struct_elt_type, portions of that function are 01358 copied here. */ 01359 01360 type = typy_get_composite (type); 01361 if (type == NULL) 01362 return NULL; 01363 01364 for (i = 0; i < TYPE_NFIELDS (type); i++) 01365 { 01366 const char *t_field_name = TYPE_FIELD_NAME (type, i); 01367 01368 if (t_field_name && (strcmp_iw (t_field_name, field) == 0)) 01369 Py_RETURN_TRUE; 01370 } 01371 Py_RETURN_FALSE; 01372 } 01373 01374 /* Make an iterator object to iterate over keys, values, or items. */ 01375 01376 static PyObject * 01377 typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind) 01378 { 01379 typy_iterator_object *typy_iter_obj; 01380 01381 /* Check that "self" is a structure or union type. */ 01382 if (typy_get_composite (((type_object *) self)->type) == NULL) 01383 return NULL; 01384 01385 typy_iter_obj = PyObject_New (typy_iterator_object, 01386 &type_iterator_object_type); 01387 if (typy_iter_obj == NULL) 01388 return NULL; 01389 01390 typy_iter_obj->field = 0; 01391 typy_iter_obj->kind = kind; 01392 Py_INCREF (self); 01393 typy_iter_obj->source = (type_object *) self; 01394 01395 return (PyObject *) typy_iter_obj; 01396 } 01397 01398 /* iteritems() method. */ 01399 01400 static PyObject * 01401 typy_iteritems (PyObject *self, PyObject *args) 01402 { 01403 return typy_make_iter (self, iter_items); 01404 } 01405 01406 /* iterkeys() method. */ 01407 01408 static PyObject * 01409 typy_iterkeys (PyObject *self, PyObject *args) 01410 { 01411 return typy_make_iter (self, iter_keys); 01412 } 01413 01414 /* Iterating over the class, same as iterkeys except for the function 01415 signature. */ 01416 01417 static PyObject * 01418 typy_iter (PyObject *self) 01419 { 01420 return typy_make_iter (self, iter_keys); 01421 } 01422 01423 /* itervalues() method. */ 01424 01425 static PyObject * 01426 typy_itervalues (PyObject *self, PyObject *args) 01427 { 01428 return typy_make_iter (self, iter_values); 01429 } 01430 01431 /* Return a reference to the type iterator. */ 01432 01433 static PyObject * 01434 typy_iterator_iter (PyObject *self) 01435 { 01436 Py_INCREF (self); 01437 return self; 01438 } 01439 01440 /* Return the next field in the iteration through the list of fields 01441 of the type. */ 01442 01443 static PyObject * 01444 typy_iterator_iternext (PyObject *self) 01445 { 01446 typy_iterator_object *iter_obj = (typy_iterator_object *) self; 01447 struct type *type = iter_obj->source->type; 01448 PyObject *result; 01449 01450 if (iter_obj->field < TYPE_NFIELDS (type)) 01451 { 01452 result = make_fielditem (type, iter_obj->field, iter_obj->kind); 01453 if (result != NULL) 01454 iter_obj->field++; 01455 return result; 01456 } 01457 01458 return NULL; 01459 } 01460 01461 static void 01462 typy_iterator_dealloc (PyObject *obj) 01463 { 01464 typy_iterator_object *iter_obj = (typy_iterator_object *) obj; 01465 01466 Py_DECREF (iter_obj->source); 01467 } 01468 01469 /* Create a new Type referring to TYPE. */ 01470 PyObject * 01471 type_to_type_object (struct type *type) 01472 { 01473 type_object *type_obj; 01474 01475 type_obj = PyObject_New (type_object, &type_object_type); 01476 if (type_obj) 01477 set_type (type_obj, type); 01478 01479 return (PyObject *) type_obj; 01480 } 01481 01482 struct type * 01483 type_object_to_type (PyObject *obj) 01484 { 01485 if (! PyObject_TypeCheck (obj, &type_object_type)) 01486 return NULL; 01487 return ((type_object *) obj)->type; 01488 } 01489 01490 01491 01492 /* Implementation of gdb.lookup_type. */ 01493 PyObject * 01494 gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) 01495 { 01496 static char *keywords[] = { "name", "block", NULL }; 01497 const char *type_name = NULL; 01498 struct type *type = NULL; 01499 PyObject *block_obj = NULL; 01500 const struct block *block = NULL; 01501 01502 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords, 01503 &type_name, &block_obj)) 01504 return NULL; 01505 01506 if (block_obj) 01507 { 01508 block = block_object_to_block (block_obj); 01509 if (! block) 01510 { 01511 PyErr_SetString (PyExc_RuntimeError, 01512 _("'block' argument must be a Block.")); 01513 return NULL; 01514 } 01515 } 01516 01517 type = typy_lookup_typename (type_name, block); 01518 if (! type) 01519 return NULL; 01520 01521 return (PyObject *) type_to_type_object (type); 01522 } 01523 01524 int 01525 gdbpy_initialize_types (void) 01526 { 01527 int i; 01528 01529 typy_objfile_data_key 01530 = register_objfile_data_with_cleanup (save_objfile_types, NULL); 01531 01532 if (PyType_Ready (&type_object_type) < 0) 01533 return -1; 01534 if (PyType_Ready (&field_object_type) < 0) 01535 return -1; 01536 if (PyType_Ready (&type_iterator_object_type) < 0) 01537 return -1; 01538 01539 for (i = 0; pyty_codes[i].name; ++i) 01540 { 01541 if (PyModule_AddIntConstant (gdb_module, 01542 /* Cast needed for Python 2.4. */ 01543 (char *) pyty_codes[i].name, 01544 pyty_codes[i].code) < 0) 01545 return -1; 01546 } 01547 01548 if (gdb_pymodule_addobject (gdb_module, "Type", 01549 (PyObject *) &type_object_type) < 0) 01550 return -1; 01551 01552 if (gdb_pymodule_addobject (gdb_module, "TypeIterator", 01553 (PyObject *) &type_iterator_object_type) < 0) 01554 return -1; 01555 01556 return gdb_pymodule_addobject (gdb_module, "Field", 01557 (PyObject *) &field_object_type); 01558 } 01559 01560 01561 01562 static PyGetSetDef type_object_getset[] = 01563 { 01564 { "code", typy_get_code, NULL, 01565 "The code for this type.", NULL }, 01566 { "sizeof", typy_get_sizeof, NULL, 01567 "The size of this type, in bytes.", NULL }, 01568 { "tag", typy_get_tag, NULL, 01569 "The tag name for this type, or None.", NULL }, 01570 { NULL } 01571 }; 01572 01573 static PyMethodDef type_object_methods[] = 01574 { 01575 { "array", typy_array, METH_VARARGS, 01576 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ 01577 Return a type which represents an array of objects of this type.\n\ 01578 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ 01579 If LOW_BOUND is omitted, a value of zero is used." }, 01580 { "vector", typy_vector, METH_VARARGS, 01581 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ 01582 Return a type which represents a vector of objects of this type.\n\ 01583 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ 01584 If LOW_BOUND is omitted, a value of zero is used.\n\ 01585 Vectors differ from arrays in that if the current language has C-style\n\ 01586 arrays, vectors don't decay to a pointer to the first element.\n\ 01587 They are first class values." }, 01588 { "__contains__", typy_has_key, METH_VARARGS, 01589 "T.__contains__(k) -> True if T has a field named k, else False" }, 01590 { "const", typy_const, METH_NOARGS, 01591 "const () -> Type\n\ 01592 Return a const variant of this type." }, 01593 { "fields", typy_fields, METH_NOARGS, 01594 "fields () -> list\n\ 01595 Return a list holding all the fields of this type.\n\ 01596 Each field is a gdb.Field object." }, 01597 { "get", typy_get, METH_VARARGS, 01598 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\ 01599 otherwise returns default, if supplied, or None if not." }, 01600 { "has_key", typy_has_key, METH_VARARGS, 01601 "T.has_key(k) -> True if T has a field named k, else False" }, 01602 { "items", typy_items, METH_NOARGS, 01603 "items () -> list\n\ 01604 Return a list of (name, field) pairs of this type.\n\ 01605 Each field is a gdb.Field object." }, 01606 { "iteritems", typy_iteritems, METH_NOARGS, 01607 "iteritems () -> an iterator over the (name, field)\n\ 01608 pairs of this type. Each field is a gdb.Field object." }, 01609 { "iterkeys", typy_iterkeys, METH_NOARGS, 01610 "iterkeys () -> an iterator over the field names of this type." }, 01611 { "itervalues", typy_itervalues, METH_NOARGS, 01612 "itervalues () -> an iterator over the fields of this type.\n\ 01613 Each field is a gdb.Field object." }, 01614 { "keys", typy_field_names, METH_NOARGS, 01615 "keys () -> list\n\ 01616 Return a list holding all the fields names of this type." }, 01617 { "pointer", typy_pointer, METH_NOARGS, 01618 "pointer () -> Type\n\ 01619 Return a type of pointer to this type." }, 01620 { "range", typy_range, METH_NOARGS, 01621 "range () -> tuple\n\ 01622 Return a tuple containing the lower and upper range for this type."}, 01623 { "reference", typy_reference, METH_NOARGS, 01624 "reference () -> Type\n\ 01625 Return a type of reference to this type." }, 01626 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, 01627 "strip_typedefs () -> Type\n\ 01628 Return a type formed by stripping this type of all typedefs."}, 01629 { "target", typy_target, METH_NOARGS, 01630 "target () -> Type\n\ 01631 Return the target type of this type." }, 01632 { "template_argument", typy_template_argument, METH_VARARGS, 01633 "template_argument (arg, [block]) -> Type\n\ 01634 Return the type of a template argument." }, 01635 { "unqualified", typy_unqualified, METH_NOARGS, 01636 "unqualified () -> Type\n\ 01637 Return a variant of this type without const or volatile attributes." }, 01638 { "values", typy_values, METH_NOARGS, 01639 "values () -> list\n\ 01640 Return a list holding all the fields of this type.\n\ 01641 Each field is a gdb.Field object." }, 01642 { "volatile", typy_volatile, METH_NOARGS, 01643 "volatile () -> Type\n\ 01644 Return a volatile variant of this type" }, 01645 { NULL } 01646 }; 01647 01648 static PyNumberMethods type_object_as_number = { 01649 NULL, /* nb_add */ 01650 NULL, /* nb_subtract */ 01651 NULL, /* nb_multiply */ 01652 #ifndef IS_PY3K 01653 NULL, /* nb_divide */ 01654 #endif 01655 NULL, /* nb_remainder */ 01656 NULL, /* nb_divmod */ 01657 NULL, /* nb_power */ 01658 NULL, /* nb_negative */ 01659 NULL, /* nb_positive */ 01660 NULL, /* nb_absolute */ 01661 typy_nonzero, /* nb_nonzero */ 01662 NULL, /* nb_invert */ 01663 NULL, /* nb_lshift */ 01664 NULL, /* nb_rshift */ 01665 NULL, /* nb_and */ 01666 NULL, /* nb_xor */ 01667 NULL, /* nb_or */ 01668 #ifdef IS_PY3K 01669 NULL, /* nb_int */ 01670 NULL, /* reserved */ 01671 #else 01672 NULL, /* nb_coerce */ 01673 NULL, /* nb_int */ 01674 NULL, /* nb_long */ 01675 #endif 01676 NULL, /* nb_float */ 01677 #ifndef IS_PY3K 01678 NULL, /* nb_oct */ 01679 NULL /* nb_hex */ 01680 #endif 01681 }; 01682 01683 static PyMappingMethods typy_mapping = { 01684 typy_length, 01685 typy_getitem, 01686 NULL /* no "set" method */ 01687 }; 01688 01689 static PyTypeObject type_object_type = 01690 { 01691 PyVarObject_HEAD_INIT (NULL, 0) 01692 "gdb.Type", /*tp_name*/ 01693 sizeof (type_object), /*tp_basicsize*/ 01694 0, /*tp_itemsize*/ 01695 typy_dealloc, /*tp_dealloc*/ 01696 0, /*tp_print*/ 01697 0, /*tp_getattr*/ 01698 0, /*tp_setattr*/ 01699 0, /*tp_compare*/ 01700 0, /*tp_repr*/ 01701 &type_object_as_number, /*tp_as_number*/ 01702 0, /*tp_as_sequence*/ 01703 &typy_mapping, /*tp_as_mapping*/ 01704 0, /*tp_hash */ 01705 0, /*tp_call*/ 01706 typy_str, /*tp_str*/ 01707 0, /*tp_getattro*/ 01708 0, /*tp_setattro*/ 01709 0, /*tp_as_buffer*/ 01710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 01711 "GDB type object", /* tp_doc */ 01712 0, /* tp_traverse */ 01713 0, /* tp_clear */ 01714 typy_richcompare, /* tp_richcompare */ 01715 0, /* tp_weaklistoffset */ 01716 typy_iter, /* tp_iter */ 01717 0, /* tp_iternext */ 01718 type_object_methods, /* tp_methods */ 01719 0, /* tp_members */ 01720 type_object_getset, /* tp_getset */ 01721 0, /* tp_base */ 01722 0, /* tp_dict */ 01723 0, /* tp_descr_get */ 01724 0, /* tp_descr_set */ 01725 0, /* tp_dictoffset */ 01726 0, /* tp_init */ 01727 0, /* tp_alloc */ 01728 0, /* tp_new */ 01729 }; 01730 01731 static PyGetSetDef field_object_getset[] = 01732 { 01733 { "__dict__", gdb_py_generic_dict, NULL, 01734 "The __dict__ for this field.", &field_object_type }, 01735 { NULL } 01736 }; 01737 01738 static PyTypeObject field_object_type = 01739 { 01740 PyVarObject_HEAD_INIT (NULL, 0) 01741 "gdb.Field", /*tp_name*/ 01742 sizeof (field_object), /*tp_basicsize*/ 01743 0, /*tp_itemsize*/ 01744 field_dealloc, /*tp_dealloc*/ 01745 0, /*tp_print*/ 01746 0, /*tp_getattr*/ 01747 0, /*tp_setattr*/ 01748 0, /*tp_compare*/ 01749 0, /*tp_repr*/ 01750 0, /*tp_as_number*/ 01751 0, /*tp_as_sequence*/ 01752 0, /*tp_as_mapping*/ 01753 0, /*tp_hash */ 01754 0, /*tp_call*/ 01755 0, /*tp_str*/ 01756 0, /*tp_getattro*/ 01757 0, /*tp_setattro*/ 01758 0, /*tp_as_buffer*/ 01759 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 01760 "GDB field object", /* tp_doc */ 01761 0, /* tp_traverse */ 01762 0, /* tp_clear */ 01763 0, /* tp_richcompare */ 01764 0, /* tp_weaklistoffset */ 01765 0, /* tp_iter */ 01766 0, /* tp_iternext */ 01767 0, /* tp_methods */ 01768 0, /* tp_members */ 01769 field_object_getset, /* tp_getset */ 01770 0, /* tp_base */ 01771 0, /* tp_dict */ 01772 0, /* tp_descr_get */ 01773 0, /* tp_descr_set */ 01774 offsetof (field_object, dict), /* tp_dictoffset */ 01775 0, /* tp_init */ 01776 0, /* tp_alloc */ 01777 0, /* tp_new */ 01778 }; 01779 01780 static PyTypeObject type_iterator_object_type = { 01781 PyVarObject_HEAD_INIT (NULL, 0) 01782 "gdb.TypeIterator", /*tp_name*/ 01783 sizeof (typy_iterator_object), /*tp_basicsize*/ 01784 0, /*tp_itemsize*/ 01785 typy_iterator_dealloc, /*tp_dealloc*/ 01786 0, /*tp_print*/ 01787 0, /*tp_getattr*/ 01788 0, /*tp_setattr*/ 01789 0, /*tp_compare*/ 01790 0, /*tp_repr*/ 01791 0, /*tp_as_number*/ 01792 0, /*tp_as_sequence*/ 01793 0, /*tp_as_mapping*/ 01794 0, /*tp_hash */ 01795 0, /*tp_call*/ 01796 0, /*tp_str*/ 01797 0, /*tp_getattro*/ 01798 0, /*tp_setattro*/ 01799 0, /*tp_as_buffer*/ 01800 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 01801 "GDB type iterator object", /*tp_doc */ 01802 0, /*tp_traverse */ 01803 0, /*tp_clear */ 01804 0, /*tp_richcompare */ 01805 0, /*tp_weaklistoffset */ 01806 typy_iterator_iter, /*tp_iter */ 01807 typy_iterator_iternext, /*tp_iternext */ 01808 0 /*tp_methods */ 01809 };