1 /* Python interface to types. 2 3 Copyright (C) 2008-2013 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" 29 #include "language.h" 30 #include "vec.h" 31 #include "bcache.h" 32 #include "dwarf2loc.h" 33 #include "typeprint.h" 34 35 typedef struct pyty_type_object 36 { 37 PyObject_HEAD 38 struct type *type; 39 40 /* If a Type object is associated with an objfile, it is kept on a 41 doubly-linked list, rooted in the objfile. This lets us copy the 42 underlying struct type when the objfile is deleted. */ 43 struct pyty_type_object *prev; 44 struct pyty_type_object *next; 45 } type_object; 46 47 static PyTypeObject type_object_type; 48 49 /* A Field object. */ 50 typedef struct pyty_field_object 51 { 52 PyObject_HEAD 53 54 /* Dictionary holding our attributes. */ 55 PyObject *dict; 56 } field_object; 57 58 static PyTypeObject field_object_type; 59 60 /* A type iterator object. */ 61 typedef struct { 62 PyObject_HEAD 63 /* The current field index. */ 64 int field; 65 /* What to return. */ 66 enum gdbpy_iter_kind kind; 67 /* Pointer back to the original source type object. */ 68 struct pyty_type_object *source; 69 } typy_iterator_object; 70 71 static PyTypeObject type_iterator_object_type; 72 73 /* This is used to initialize various gdb.TYPE_ constants. */ 74 struct pyty_code 75 { 76 /* The code. */ 77 enum type_code code; 78 /* The name. */ 79 const char *name; 80 }; 81 82 /* Forward declarations. */ 83 static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind); 84 85 #define ENTRY(X) { X, #X } 86 87 static struct pyty_code pyty_codes[] = 88 { 89 ENTRY (TYPE_CODE_BITSTRING), 90 ENTRY (TYPE_CODE_PTR), 91 ENTRY (TYPE_CODE_ARRAY), 92 ENTRY (TYPE_CODE_STRUCT), 93 ENTRY (TYPE_CODE_UNION), 94 ENTRY (TYPE_CODE_ENUM), 95 ENTRY (TYPE_CODE_FLAGS), 96 ENTRY (TYPE_CODE_FUNC), 97 ENTRY (TYPE_CODE_INT), 98 ENTRY (TYPE_CODE_FLT), 99 ENTRY (TYPE_CODE_VOID), 100 ENTRY (TYPE_CODE_SET), 101 ENTRY (TYPE_CODE_RANGE), 102 ENTRY (TYPE_CODE_STRING), 103 ENTRY (TYPE_CODE_ERROR), 104 ENTRY (TYPE_CODE_METHOD), 105 ENTRY (TYPE_CODE_METHODPTR), 106 ENTRY (TYPE_CODE_MEMBERPTR), 107 ENTRY (TYPE_CODE_REF), 108 ENTRY (TYPE_CODE_CHAR), 109 ENTRY (TYPE_CODE_BOOL), 110 ENTRY (TYPE_CODE_COMPLEX), 111 ENTRY (TYPE_CODE_TYPEDEF), 112 ENTRY (TYPE_CODE_NAMESPACE), 113 ENTRY (TYPE_CODE_DECFLOAT), 114 ENTRY (TYPE_CODE_INTERNAL_FUNCTION), 115 { TYPE_CODE_UNDEF, NULL } 116 }; 117 118 119 120 static void 121 field_dealloc (PyObject *obj) 122 { 123 field_object *f = (field_object *) obj; 124 125 Py_XDECREF (f->dict); 126 Py_TYPE (obj)->tp_free (obj); 127 } 128 129 static PyObject * 130 field_new (void) 131 { 132 field_object *result = PyObject_New (field_object, &field_object_type); 133 134 if (result) 135 { 136 result->dict = PyDict_New (); 137 if (!result->dict) 138 { 139 Py_DECREF (result); 140 result = NULL; 141 } 142 } 143 return (PyObject *) result; 144 } 145 146 147 148 /* Return the code for this type. */ 149 static PyObject * 150 typy_get_code (PyObject *self, void *closure) 151 { 152 struct type *type = ((type_object *) self)->type; 153 154 return PyInt_FromLong (TYPE_CODE (type)); 155 } 156 157 /* Helper function for typy_fields which converts a single field to a 158 gdb.Field object. Returns NULL on error. */ 159 160 static PyObject * 161 convert_field (struct type *type, int field) 162 { 163 PyObject *result = field_new (); 164 PyObject *arg; 165 166 if (!result) 167 return NULL; 168 169 if (!field_is_static (&TYPE_FIELD (type, field))) 170 { 171 const char *attrstring; 172 173 if (TYPE_CODE (type) == TYPE_CODE_ENUM) 174 { 175 arg = gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type, field)); 176 attrstring = "enumval"; 177 } 178 else 179 { 180 arg = gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type, field)); 181 attrstring = "bitpos"; 182 } 183 184 if (!arg) 185 goto fail; 186 187 /* At least python-2.4 had the second parameter non-const. */ 188 if (PyObject_SetAttrString (result, (char *) attrstring, arg) < 0) 189 goto failarg; 190 Py_DECREF (arg); 191 } 192 193 if (TYPE_FIELD_NAME (type, field)) 194 arg = PyString_FromString (TYPE_FIELD_NAME (type, field)); 195 else 196 { 197 arg = Py_None; 198 Py_INCREF (arg); 199 } 200 if (!arg) 201 goto fail; 202 if (PyObject_SetAttrString (result, "name", arg) < 0) 203 goto failarg; 204 Py_DECREF (arg); 205 206 arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False; 207 Py_INCREF (arg); 208 if (PyObject_SetAttrString (result, "artificial", arg) < 0) 209 goto failarg; 210 Py_DECREF (arg); 211 212 if (TYPE_CODE (type) == TYPE_CODE_CLASS) 213 arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False; 214 else 215 arg = Py_False; 216 Py_INCREF (arg); 217 if (PyObject_SetAttrString (result, "is_base_class", arg) < 0) 218 goto failarg; 219 Py_DECREF (arg); 220 221 arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field)); 222 if (!arg) 223 goto fail; 224 if (PyObject_SetAttrString (result, "bitsize", arg) < 0) 225 goto failarg; 226 Py_DECREF (arg); 227 228 /* A field can have a NULL type in some situations. */ 229 if (TYPE_FIELD_TYPE (type, field) == NULL) 230 { 231 arg = Py_None; 232 Py_INCREF (arg); 233 } 234 else 235 arg = type_to_type_object (TYPE_FIELD_TYPE (type, field)); 236 if (!arg) 237 goto fail; 238 if (PyObject_SetAttrString (result, "type", arg) < 0) 239 goto failarg; 240 Py_DECREF (arg); 241 242 return result; 243 244 failarg: 245 Py_DECREF (arg); 246 fail: 247 Py_DECREF (result); 248 return NULL; 249 } 250 251 /* Helper function to return the name of a field, as a gdb.Field object. 252 If the field doesn't have a name, None is returned. */ 253 254 static PyObject * 255 field_name (struct type *type, int field) 256 { 257 PyObject *result; 258 259 if (TYPE_FIELD_NAME (type, field)) 260 result = PyString_FromString (TYPE_FIELD_NAME (type, field)); 261 else 262 { 263 result = Py_None; 264 Py_INCREF (result); 265 } 266 return result; 267 } 268 269 /* Helper function for Type standard mapping methods. Returns a 270 Python object for field i of the type. "kind" specifies what to 271 return: the name of the field, a gdb.Field object corresponding to 272 the field, or a tuple consisting of field name and gdb.Field 273 object. */ 274 275 static PyObject * 276 make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind) 277 { 278 PyObject *item = NULL, *key = NULL, *value = NULL; 279 280 switch (kind) 281 { 282 case iter_items: 283 key = field_name (type, i); 284 if (key == NULL) 285 goto fail; 286 value = convert_field (type, i); 287 if (value == NULL) 288 goto fail; 289 item = PyTuple_New (2); 290 if (item == NULL) 291 goto fail; 292 PyTuple_SET_ITEM (item, 0, key); 293 PyTuple_SET_ITEM (item, 1, value); 294 break; 295 case iter_keys: 296 item = field_name (type, i); 297 break; 298 case iter_values: 299 item = convert_field (type, i); 300 break; 301 } 302 return item; 303 304 fail: 305 Py_XDECREF (key); 306 Py_XDECREF (value); 307 Py_XDECREF (item); 308 return NULL; 309 } 310 311 /* Return a sequence of all field names, fields, or (name, field) pairs. 312 Each field is a gdb.Field object. */ 313 314 static PyObject * 315 typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind) 316 { 317 PyObject *py_type = self; 318 PyObject *result = NULL, *iter = NULL; 319 volatile struct gdb_exception except; 320 struct type *type = ((type_object *) py_type)->type; 321 struct type *checked_type = type; 322 323 TRY_CATCH (except, RETURN_MASK_ALL) 324 { 325 CHECK_TYPEDEF (checked_type); 326 } 327 GDB_PY_HANDLE_EXCEPTION (except); 328 329 if (checked_type != type) 330 py_type = type_to_type_object (checked_type); 331 iter = typy_make_iter (py_type, kind); 332 if (checked_type != type) 333 { 334 /* Need to wrap this in braces because Py_DECREF isn't wrapped 335 in a do{}while(0). */ 336 Py_DECREF (py_type); 337 } 338 if (iter != NULL) 339 { 340 result = PySequence_List (iter); 341 Py_DECREF (iter); 342 } 343 344 return result; 345 } 346 347 /* Return a sequence of all fields. Each field is a gdb.Field object. */ 348 349 static PyObject * 350 typy_values (PyObject *self, PyObject *args) 351 { 352 return typy_fields_items (self, iter_values); 353 } 354 355 /* Return a sequence of all fields. Each field is a gdb.Field object. 356 This method is similar to typy_values, except where the supplied 357 gdb.Type is an array, in which case it returns a list of one entry 358 which is a gdb.Field object for a range (the array bounds). */ 359 360 static PyObject * 361 typy_fields (PyObject *self, PyObject *args) 362 { 363 struct type *type = ((type_object *) self)->type; 364 PyObject *r, *rl; 365 366 if (TYPE_CODE (type) != TYPE_CODE_ARRAY) 367 return typy_fields_items (self, iter_values); 368 369 /* Array type. Handle this as a special case because the common 370 machinery wants struct or union or enum types. Build a list of 371 one entry which is the range for the array. */ 372 r = convert_field (type, 0); 373 if (r == NULL) 374 return NULL; 375 376 rl = Py_BuildValue ("[O]", r); 377 if (rl == NULL) 378 { 379 Py_DECREF (r); 380 } 381 382 return rl; 383 } 384 385 /* Return a sequence of all field names. Each field is a gdb.Field object. */ 386 387 static PyObject * 388 typy_field_names (PyObject *self, PyObject *args) 389 { 390 return typy_fields_items (self, iter_keys); 391 } 392 393 /* Return a sequence of all (name, fields) pairs. Each field is a 394 gdb.Field object. */ 395 396 static PyObject * 397 typy_items (PyObject *self, PyObject *args) 398 { 399 return typy_fields_items (self, iter_items); 400 } 401 402 /* Return the type's tag, or None. */ 403 static PyObject * 404 typy_get_tag (PyObject *self, void *closure) 405 { 406 struct type *type = ((type_object *) self)->type; 407 408 if (!TYPE_TAG_NAME (type)) 409 Py_RETURN_NONE; 410 return PyString_FromString (TYPE_TAG_NAME (type)); 411 } 412 413 /* Return the type, stripped of typedefs. */ 414 static PyObject * 415 typy_strip_typedefs (PyObject *self, PyObject *args) 416 { 417 struct type *type = ((type_object *) self)->type; 418 volatile struct gdb_exception except; 419 420 TRY_CATCH (except, RETURN_MASK_ALL) 421 { 422 type = check_typedef (type); 423 } 424 GDB_PY_HANDLE_EXCEPTION (except); 425 426 return type_to_type_object (type); 427 } 428 429 /* Strip typedefs and pointers/reference from a type. Then check that 430 it is a struct, union, or enum type. If not, raise TypeError. */ 431 432 static struct type * 433 typy_get_composite (struct type *type) 434 { 435 volatile struct gdb_exception except; 436 437 for (;;) 438 { 439 TRY_CATCH (except, RETURN_MASK_ALL) 440 { 441 CHECK_TYPEDEF (type); 442 } 443 /* Don't use GDB_PY_HANDLE_EXCEPTION here because that returns 444 a (NULL) pointer of the wrong type. */ 445 if (except.reason < 0) 446 { 447 gdbpy_convert_exception (except); 448 return NULL; 449 } 450 451 if (TYPE_CODE (type) != TYPE_CODE_PTR 452 && TYPE_CODE (type) != TYPE_CODE_REF) 453 break; 454 type = TYPE_TARGET_TYPE (type); 455 } 456 457 /* If this is not a struct, union, or enum type, raise TypeError 458 exception. */ 459 if (TYPE_CODE (type) != TYPE_CODE_STRUCT 460 && TYPE_CODE (type) != TYPE_CODE_UNION 461 && TYPE_CODE (type) != TYPE_CODE_ENUM) 462 { 463 PyErr_SetString (PyExc_TypeError, 464 "Type is not a structure, union, or enum type."); 465 return NULL; 466 } 467 468 return type; 469 } 470 471 /* Helper for typy_array and typy_vector. */ 472 473 static PyObject * 474 typy_array_1 (PyObject *self, PyObject *args, int is_vector) 475 { 476 long n1, n2; 477 PyObject *n2_obj = NULL; 478 struct type *array = NULL; 479 struct type *type = ((type_object *) self)->type; 480 volatile struct gdb_exception except; 481 482 if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj)) 483 return NULL; 484 485 if (n2_obj) 486 { 487 if (!PyInt_Check (n2_obj)) 488 { 489 PyErr_SetString (PyExc_RuntimeError, 490 _("Array bound must be an integer")); 491 return NULL; 492 } 493 494 if (! gdb_py_int_as_long (n2_obj, &n2)) 495 return NULL; 496 } 497 else 498 { 499 n2 = n1; 500 n1 = 0; 501 } 502 503 if (n2 < n1) 504 { 505 PyErr_SetString (PyExc_ValueError, 506 _("Array length must not be negative")); 507 return NULL; 508 } 509 510 TRY_CATCH (except, RETURN_MASK_ALL) 511 { 512 array = lookup_array_range_type (type, n1, n2); 513 if (is_vector) 514 make_vector_type (array); 515 } 516 GDB_PY_HANDLE_EXCEPTION (except); 517 518 return type_to_type_object (array); 519 } 520 521 /* Return an array type. */ 522 523 static PyObject * 524 typy_array (PyObject *self, PyObject *args) 525 { 526 return typy_array_1 (self, args, 0); 527 } 528 529 /* Return a vector type. */ 530 531 static PyObject * 532 typy_vector (PyObject *self, PyObject *args) 533 { 534 return typy_array_1 (self, args, 1); 535 } 536 537 /* Return a Type object which represents a pointer to SELF. */ 538 static PyObject * 539 typy_pointer (PyObject *self, PyObject *args) 540 { 541 struct type *type = ((type_object *) self)->type; 542 volatile struct gdb_exception except; 543 544 TRY_CATCH (except, RETURN_MASK_ALL) 545 { 546 type = lookup_pointer_type (type); 547 } 548 GDB_PY_HANDLE_EXCEPTION (except); 549 550 return type_to_type_object (type); 551 } 552 553 /* Return the range of a type represented by SELF. The return type is 554 a tuple. The first element of the tuple contains the low bound, 555 while the second element of the tuple contains the high bound. */ 556 static PyObject * 557 typy_range (PyObject *self, PyObject *args) 558 { 559 struct type *type = ((type_object *) self)->type; 560 PyObject *result; 561 PyObject *low_bound = NULL, *high_bound = NULL; 562 /* Initialize these to appease GCC warnings. */ 563 LONGEST low = 0, high = 0; 564 565 if (TYPE_CODE (type) != TYPE_CODE_ARRAY 566 && TYPE_CODE (type) != TYPE_CODE_STRING 567 && TYPE_CODE (type) != TYPE_CODE_RANGE) 568 { 569 PyErr_SetString (PyExc_RuntimeError, 570 _("This type does not have a range.")); 571 return NULL; 572 } 573 574 switch (TYPE_CODE (type)) 575 { 576 case TYPE_CODE_ARRAY: 577 case TYPE_CODE_STRING: 578 low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); 579 high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)); 580 break; 581 case TYPE_CODE_RANGE: 582 low = TYPE_LOW_BOUND (type); 583 high = TYPE_HIGH_BOUND (type); 584 break; 585 } 586 587 low_bound = PyLong_FromLong (low); 588 if (!low_bound) 589 goto failarg; 590 591 high_bound = PyLong_FromLong (high); 592 if (!high_bound) 593 goto failarg; 594 595 result = PyTuple_New (2); 596 if (!result) 597 goto failarg; 598 599 if (PyTuple_SetItem (result, 0, low_bound) != 0) 600 { 601 Py_DECREF (result); 602 goto failarg; 603 } 604 if (PyTuple_SetItem (result, 1, high_bound) != 0) 605 { 606 Py_DECREF (high_bound); 607 Py_DECREF (result); 608 return NULL; 609 } 610 return result; 611 612 failarg: 613 Py_XDECREF (high_bound); 614 Py_XDECREF (low_bound); 615 return NULL; 616 } 617 618 /* Return a Type object which represents a reference to SELF. */ 619 static PyObject * 620 typy_reference (PyObject *self, PyObject *args) 621 { 622 struct type *type = ((type_object *) self)->type; 623 volatile struct gdb_exception except; 624 625 TRY_CATCH (except, RETURN_MASK_ALL) 626 { 627 type = lookup_reference_type (type); 628 } 629 GDB_PY_HANDLE_EXCEPTION (except); 630 631 return type_to_type_object (type); 632 } 633 634 /* Return a Type object which represents the target type of SELF. */ 635 static PyObject * 636 typy_target (PyObject *self, PyObject *args) 637 { 638 struct type *type = ((type_object *) self)->type; 639 640 if (!TYPE_TARGET_TYPE (type)) 641 { 642 PyErr_SetString (PyExc_RuntimeError, 643 _("Type does not have a target.")); 644 return NULL; 645 } 646 647 return type_to_type_object (TYPE_TARGET_TYPE (type)); 648 } 649 650 /* Return a const-qualified type variant. */ 651 static PyObject * 652 typy_const (PyObject *self, PyObject *args) 653 { 654 struct type *type = ((type_object *) self)->type; 655 volatile struct gdb_exception except; 656 657 TRY_CATCH (except, RETURN_MASK_ALL) 658 { 659 type = make_cv_type (1, 0, type, NULL); 660 } 661 GDB_PY_HANDLE_EXCEPTION (except); 662 663 return type_to_type_object (type); 664 } 665 666 /* Return a volatile-qualified type variant. */ 667 static PyObject * 668 typy_volatile (PyObject *self, PyObject *args) 669 { 670 struct type *type = ((type_object *) self)->type; 671 volatile struct gdb_exception except; 672 673 TRY_CATCH (except, RETURN_MASK_ALL) 674 { 675 type = make_cv_type (0, 1, type, NULL); 676 } 677 GDB_PY_HANDLE_EXCEPTION (except); 678 679 return type_to_type_object (type); 680 } 681 682 /* Return an unqualified type variant. */ 683 static PyObject * 684 typy_unqualified (PyObject *self, PyObject *args) 685 { 686 struct type *type = ((type_object *) self)->type; 687 volatile struct gdb_exception except; 688 689 TRY_CATCH (except, RETURN_MASK_ALL) 690 { 691 type = make_cv_type (0, 0, type, NULL); 692 } 693 GDB_PY_HANDLE_EXCEPTION (except); 694 695 return type_to_type_object (type); 696 } 697 698 /* Return the size of the type represented by SELF, in bytes. */ 699 static PyObject * 700 typy_get_sizeof (PyObject *self, void *closure) 701 { 702 struct type *type = ((type_object *) self)->type; 703 volatile struct gdb_exception except; 704 705 TRY_CATCH (except, RETURN_MASK_ALL) 706 { 707 check_typedef (type); 708 } 709 /* Ignore exceptions. */ 710 711 return gdb_py_long_from_longest (TYPE_LENGTH (type)); 712 } 713 714 static struct type * 715 typy_lookup_typename (const char *type_name, const struct block *block) 716 { 717 struct type *type = NULL; 718 volatile struct gdb_exception except; 719 720 TRY_CATCH (except, RETURN_MASK_ALL) 721 { 722 if (!strncmp (type_name, "struct ", 7)) 723 type = lookup_struct (type_name + 7, NULL); 724 else if (!strncmp (type_name, "union ", 6)) 725 type = lookup_union (type_name + 6, NULL); 726 else if (!strncmp (type_name, "enum ", 5)) 727 type = lookup_enum (type_name + 5, NULL); 728 else 729 type = lookup_typename (python_language, python_gdbarch, 730 type_name, block, 0); 731 } 732 if (except.reason < 0) 733 { 734 gdbpy_convert_exception (except); 735 return NULL; 736 } 737 738 return type; 739 } 740 741 static struct type * 742 typy_lookup_type (struct demangle_component *demangled, 743 const struct block *block) 744 { 745 struct type *type, *rtype = NULL; 746 char *type_name = NULL; 747 enum demangle_component_type demangled_type; 748 volatile struct gdb_exception except; 749 750 /* Save the type: typy_lookup_type() may (indirectly) overwrite 751 memory pointed by demangled. */ 752 demangled_type = demangled->type; 753 754 if (demangled_type == DEMANGLE_COMPONENT_POINTER 755 || demangled_type == DEMANGLE_COMPONENT_REFERENCE 756 || demangled_type == DEMANGLE_COMPONENT_CONST 757 || demangled_type == DEMANGLE_COMPONENT_VOLATILE) 758 { 759 type = typy_lookup_type (demangled->u.s_binary.left, block); 760 if (! type) 761 return NULL; 762 763 TRY_CATCH (except, RETURN_MASK_ALL) 764 { 765 /* If the demangled_type matches with one of the types 766 below, run the corresponding function and save the type 767 to return later. We cannot just return here as we are in 768 an exception handler. */ 769 switch (demangled_type) 770 { 771 case DEMANGLE_COMPONENT_REFERENCE: 772 rtype = lookup_reference_type (type); 773 break; 774 case DEMANGLE_COMPONENT_POINTER: 775 rtype = lookup_pointer_type (type); 776 break; 777 case DEMANGLE_COMPONENT_CONST: 778 rtype = make_cv_type (1, 0, type, NULL); 779 break; 780 case DEMANGLE_COMPONENT_VOLATILE: 781 rtype = make_cv_type (0, 1, type, NULL); 782 break; 783 } 784 } 785 if (except.reason < 0) 786 { 787 gdbpy_convert_exception (except); 788 return NULL; 789 } 790 } 791 792 /* If we have a type from the switch statement above, just return 793 that. */ 794 if (rtype) 795 return rtype; 796 797 /* We don't have a type, so lookup the type. */ 798 type_name = cp_comp_to_string (demangled, 10); 799 type = typy_lookup_typename (type_name, block); 800 xfree (type_name); 801 802 return type; 803 } 804 805 /* This is a helper function for typy_template_argument that is used 806 when the type does not have template symbols attached. It works by 807 parsing the type name. This happens with compilers, like older 808 versions of GCC, that do not emit DW_TAG_template_*. */ 809 810 static PyObject * 811 typy_legacy_template_argument (struct type *type, const struct block *block, 812 int argno) 813 { 814 int i; 815 struct demangle_component *demangled; 816 struct demangle_parse_info *info = NULL; 817 const char *err; 818 struct type *argtype; 819 struct cleanup *cleanup; 820 volatile struct gdb_exception except; 821 822 if (TYPE_NAME (type) == NULL) 823 { 824 PyErr_SetString (PyExc_RuntimeError, _("Null type name.")); 825 return NULL; 826 } 827 828 TRY_CATCH (except, RETURN_MASK_ALL) 829 { 830 /* Note -- this is not thread-safe. */ 831 info = cp_demangled_name_to_comp (TYPE_NAME (type), &err); 832 } 833 GDB_PY_HANDLE_EXCEPTION (except); 834 835 if (! info) 836 { 837 PyErr_SetString (PyExc_RuntimeError, err); 838 return NULL; 839 } 840 demangled = info->tree; 841 cleanup = make_cleanup_cp_demangled_name_parse_free (info); 842 843 /* Strip off component names. */ 844 while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME 845 || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) 846 demangled = demangled->u.s_binary.right; 847 848 if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) 849 { 850 do_cleanups (cleanup); 851 PyErr_SetString (PyExc_RuntimeError, _("Type is not a template.")); 852 return NULL; 853 } 854 855 /* Skip from the template to the arguments. */ 856 demangled = demangled->u.s_binary.right; 857 858 for (i = 0; demangled && i < argno; ++i) 859 demangled = demangled->u.s_binary.right; 860 861 if (! demangled) 862 { 863 do_cleanups (cleanup); 864 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), 865 argno); 866 return NULL; 867 } 868 869 argtype = typy_lookup_type (demangled->u.s_binary.left, block); 870 do_cleanups (cleanup); 871 if (! argtype) 872 return NULL; 873 874 return type_to_type_object (argtype); 875 } 876 877 static PyObject * 878 typy_template_argument (PyObject *self, PyObject *args) 879 { 880 int argno; 881 struct type *type = ((type_object *) self)->type; 882 const struct block *block = NULL; 883 PyObject *block_obj = NULL; 884 struct symbol *sym; 885 struct value *val = NULL; 886 volatile struct gdb_exception except; 887 888 if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj)) 889 return NULL; 890 891 if (block_obj) 892 { 893 block = block_object_to_block (block_obj); 894 if (! block) 895 { 896 PyErr_SetString (PyExc_RuntimeError, 897 _("Second argument must be block.")); 898 return NULL; 899 } 900 } 901 902 TRY_CATCH (except, RETURN_MASK_ALL) 903 { 904 type = check_typedef (type); 905 if (TYPE_CODE (type) == TYPE_CODE_REF) 906 type = check_typedef (TYPE_TARGET_TYPE (type)); 907 } 908 GDB_PY_HANDLE_EXCEPTION (except); 909 910 /* We might not have DW_TAG_template_*, so try to parse the type's 911 name. This is inefficient if we do not have a template type -- 912 but that is going to wind up as an error anyhow. */ 913 if (! TYPE_N_TEMPLATE_ARGUMENTS (type)) 914 return typy_legacy_template_argument (type, block, argno); 915 916 if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type)) 917 { 918 PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), 919 argno); 920 return NULL; 921 } 922 923 sym = TYPE_TEMPLATE_ARGUMENT (type, argno); 924 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) 925 return type_to_type_object (SYMBOL_TYPE (sym)); 926 else if (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT) 927 { 928 PyErr_Format (PyExc_RuntimeError, 929 _("Template argument is optimized out")); 930 return NULL; 931 } 932 933 TRY_CATCH (except, RETURN_MASK_ALL) 934 { 935 val = value_of_variable (sym, block); 936 } 937 GDB_PY_HANDLE_EXCEPTION (except); 938 939 return value_to_value_object (val); 940 } 941 942 static PyObject * 943 typy_str (PyObject *self) 944 { 945 volatile struct gdb_exception except; 946 char *thetype = NULL; 947 long length = 0; 948 PyObject *result; 949 950 TRY_CATCH (except, RETURN_MASK_ALL) 951 { 952 struct cleanup *old_chain; 953 struct ui_file *stb; 954 955 stb = mem_fileopen (); 956 old_chain = make_cleanup_ui_file_delete (stb); 957 958 LA_PRINT_TYPE (type_object_to_type (self), "", stb, -1, 0, 959 &type_print_raw_options); 960 961 thetype = ui_file_xstrdup (stb, &length); 962 do_cleanups (old_chain); 963 } 964 if (except.reason < 0) 965 { 966 xfree (thetype); 967 GDB_PY_HANDLE_EXCEPTION (except); 968 } 969 970 result = PyUnicode_Decode (thetype, length, host_charset (), NULL); 971 xfree (thetype); 972 973 return result; 974 } 975 976 /* An entry in the type-equality bcache. */ 977 978 typedef struct type_equality_entry 979 { 980 struct type *type1, *type2; 981 } type_equality_entry_d; 982 983 DEF_VEC_O (type_equality_entry_d); 984 985 /* A helper function to compare two strings. Returns 1 if they are 986 the same, 0 otherwise. Handles NULLs properly. */ 987 988 static int 989 compare_maybe_null_strings (const char *s, const char *t) 990 { 991 if (s == NULL && t != NULL) 992 return 0; 993 else if (s != NULL && t == NULL) 994 return 0; 995 else if (s == NULL && t== NULL) 996 return 1; 997 return strcmp (s, t) == 0; 998 } 999 1000 /* A helper function for typy_richcompare that checks two types for 1001 "deep" equality. Returns Py_EQ if the types are considered the 1002 same, Py_NE otherwise. */ 1003 1004 static int 1005 check_types_equal (struct type *type1, struct type *type2, 1006 VEC (type_equality_entry_d) **worklist) 1007 { 1008 CHECK_TYPEDEF (type1); 1009 CHECK_TYPEDEF (type2); 1010 1011 if (type1 == type2) 1012 return Py_EQ; 1013 1014 if (TYPE_CODE (type1) != TYPE_CODE (type2) 1015 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2) 1016 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2) 1017 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2) 1018 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2) 1019 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2) 1020 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2) 1021 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2) 1022 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2)) 1023 return Py_NE; 1024 1025 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1), 1026 TYPE_TAG_NAME (type2))) 1027 return Py_NE; 1028 if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2))) 1029 return Py_NE; 1030 1031 if (TYPE_CODE (type1) == TYPE_CODE_RANGE) 1032 { 1033 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2), 1034 sizeof (*TYPE_RANGE_DATA (type1))) != 0) 1035 return Py_NE; 1036 } 1037 else 1038 { 1039 int i; 1040 1041 for (i = 0; i < TYPE_NFIELDS (type1); ++i) 1042 { 1043 const struct field *field1 = &TYPE_FIELD (type1, i); 1044 const struct field *field2 = &TYPE_FIELD (type2, i); 1045 struct type_equality_entry entry; 1046 1047 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2) 1048 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2) 1049 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2)) 1050 return Py_NE; 1051 if (!compare_maybe_null_strings (FIELD_NAME (*field1), 1052 FIELD_NAME (*field2))) 1053 return Py_NE; 1054 switch (FIELD_LOC_KIND (*field1)) 1055 { 1056 case FIELD_LOC_KIND_BITPOS: 1057 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2)) 1058 return Py_NE; 1059 break; 1060 case FIELD_LOC_KIND_ENUMVAL: 1061 if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2)) 1062 return Py_NE; 1063 break; 1064 case FIELD_LOC_KIND_PHYSADDR: 1065 if (FIELD_STATIC_PHYSADDR (*field1) 1066 != FIELD_STATIC_PHYSADDR (*field2)) 1067 return Py_NE; 1068 break; 1069 case FIELD_LOC_KIND_PHYSNAME: 1070 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1), 1071 FIELD_STATIC_PHYSNAME (*field2))) 1072 return Py_NE; 1073 break; 1074 case FIELD_LOC_KIND_DWARF_BLOCK: 1075 { 1076 struct dwarf2_locexpr_baton *block1, *block2; 1077 1078 block1 = FIELD_DWARF_BLOCK (*field1); 1079 block2 = FIELD_DWARF_BLOCK (*field2); 1080 if (block1->per_cu != block2->per_cu 1081 || block1->size != block2->size 1082 || memcmp (block1->data, block2->data, block1->size) != 0) 1083 return Py_NE; 1084 } 1085 break; 1086 default: 1087 internal_error (__FILE__, __LINE__, _("Unsupported field kind " 1088 "%d by check_types_equal"), 1089 FIELD_LOC_KIND (*field1)); 1090 } 1091 1092 entry.type1 = FIELD_TYPE (*field1); 1093 entry.type2 = FIELD_TYPE (*field2); 1094 VEC_safe_push (type_equality_entry_d, *worklist, &entry); 1095 } 1096 } 1097 1098 if (TYPE_TARGET_TYPE (type1) != NULL) 1099 { 1100 struct type_equality_entry entry; 1101 1102 if (TYPE_TARGET_TYPE (type2) == NULL) 1103 return Py_NE; 1104 1105 entry.type1 = TYPE_TARGET_TYPE (type1); 1106 entry.type2 = TYPE_TARGET_TYPE (type2); 1107 VEC_safe_push (type_equality_entry_d, *worklist, &entry); 1108 } 1109 else if (TYPE_TARGET_TYPE (type2) != NULL) 1110 return Py_NE; 1111 1112 return Py_EQ; 1113 } 1114 1115 /* Check types on a worklist for equality. Returns Py_NE if any pair 1116 is not equal, Py_EQ if they are all considered equal. */ 1117 1118 static int 1119 check_types_worklist (VEC (type_equality_entry_d) **worklist, 1120 struct bcache *cache) 1121 { 1122 while (!VEC_empty (type_equality_entry_d, *worklist)) 1123 { 1124 struct type_equality_entry entry; 1125 int added; 1126 1127 entry = *VEC_last (type_equality_entry_d, *worklist); 1128 VEC_pop (type_equality_entry_d, *worklist); 1129 1130 /* If the type pair has already been visited, we know it is 1131 ok. */ 1132 bcache_full (&entry, sizeof (entry), cache, &added); 1133 if (!added) 1134 continue; 1135 1136 if (check_types_equal (entry.type1, entry.type2, worklist) == Py_NE) 1137 return Py_NE; 1138 } 1139 1140 return Py_EQ; 1141 } 1142 1143 /* Implement the richcompare method. */ 1144 1145 static PyObject * 1146 typy_richcompare (PyObject *self, PyObject *other, int op) 1147 { 1148 int result = Py_NE; 1149 struct type *type1 = type_object_to_type (self); 1150 struct type *type2 = type_object_to_type (other); 1151 volatile struct gdb_exception except; 1152 1153 /* We can only compare ourselves to another Type object, and only 1154 for equality or inequality. */ 1155 if (type2 == NULL || (op != Py_EQ && op != Py_NE)) 1156 { 1157 Py_INCREF (Py_NotImplemented); 1158 return Py_NotImplemented; 1159 } 1160 1161 if (type1 == type2) 1162 result = Py_EQ; 1163 else 1164 { 1165 struct bcache *cache; 1166 VEC (type_equality_entry_d) *worklist = NULL; 1167 struct type_equality_entry entry; 1168 1169 cache = bcache_xmalloc (NULL, NULL); 1170 1171 entry.type1 = type1; 1172 entry.type2 = type2; 1173 VEC_safe_push (type_equality_entry_d, worklist, &entry); 1174 1175 TRY_CATCH (except, RETURN_MASK_ALL) 1176 { 1177 result = check_types_worklist (&worklist, cache); 1178 } 1179 /* check_types_worklist calls several nested Python helper 1180 functions, some of which can raise a GDB Exception, so we 1181 just check and convert here. If there is a GDB exception, a 1182 comparison is not capable (or trusted), so exit. */ 1183 bcache_xfree (cache); 1184 VEC_free (type_equality_entry_d, worklist); 1185 GDB_PY_HANDLE_EXCEPTION (except); 1186 } 1187 1188 if (op == result) 1189 Py_RETURN_TRUE; 1190 Py_RETURN_FALSE; 1191 } 1192 1193 1194 1195 static const struct objfile_data *typy_objfile_data_key; 1196 1197 static void 1198 save_objfile_types (struct objfile *objfile, void *datum) 1199 { 1200 type_object *obj = datum; 1201 htab_t copied_types; 1202 struct cleanup *cleanup; 1203 1204 /* This prevents another thread from freeing the objects we're 1205 operating on. */ 1206 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); 1207 1208 copied_types = create_copied_types_hash (objfile); 1209 1210 while (obj) 1211 { 1212 type_object *next = obj->next; 1213 1214 htab_empty (copied_types); 1215 1216 obj->type = copy_type_recursive (objfile, obj->type, copied_types); 1217 1218 obj->next = NULL; 1219 obj->prev = NULL; 1220 1221 obj = next; 1222 } 1223 1224 htab_delete (copied_types); 1225 1226 do_cleanups (cleanup); 1227 } 1228 1229 static void 1230 set_type (type_object *obj, struct type *type) 1231 { 1232 obj->type = type; 1233 obj->prev = NULL; 1234 if (type && TYPE_OBJFILE (type)) 1235 { 1236 struct objfile *objfile = TYPE_OBJFILE (type); 1237 1238 obj->next = objfile_data (objfile, typy_objfile_data_key); 1239 if (obj->next) 1240 obj->next->prev = obj; 1241 set_objfile_data (objfile, typy_objfile_data_key, obj); 1242 } 1243 else 1244 obj->next = NULL; 1245 } 1246 1247 static void 1248 typy_dealloc (PyObject *obj) 1249 { 1250 type_object *type = (type_object *) obj; 1251 1252 if (type->prev) 1253 type->prev->next = type->next; 1254 else if (type->type && TYPE_OBJFILE (type->type)) 1255 { 1256 /* Must reset head of list. */ 1257 struct objfile *objfile = TYPE_OBJFILE (type->type); 1258 1259 if (objfile) 1260 set_objfile_data (objfile, typy_objfile_data_key, type->next); 1261 } 1262 if (type->next) 1263 type->next->prev = type->prev; 1264 1265 Py_TYPE (type)->tp_free (type); 1266 } 1267 1268 /* Return number of fields ("length" of the field dictionary). */ 1269 1270 static Py_ssize_t 1271 typy_length (PyObject *self) 1272 { 1273 struct type *type = ((type_object *) self)->type; 1274 1275 type = typy_get_composite (type); 1276 if (type == NULL) 1277 return -1; 1278 1279 return TYPE_NFIELDS (type); 1280 } 1281 1282 /* Implements boolean evaluation of gdb.Type. Handle this like other 1283 Python objects that don't have a meaningful truth value -- all 1284 values are true. */ 1285 1286 static int 1287 typy_nonzero (PyObject *self) 1288 { 1289 return 1; 1290 } 1291 1292 /* Return a gdb.Field object for the field named by the argument. */ 1293 1294 static PyObject * 1295 typy_getitem (PyObject *self, PyObject *key) 1296 { 1297 struct type *type = ((type_object *) self)->type; 1298 char *field; 1299 int i; 1300 1301 field = python_string_to_host_string (key); 1302 if (field == NULL) 1303 return NULL; 1304 1305 /* We want just fields of this type, not of base types, so instead of 1306 using lookup_struct_elt_type, portions of that function are 1307 copied here. */ 1308 1309 type = typy_get_composite (type); 1310 if (type == NULL) 1311 return NULL; 1312 1313 for (i = 0; i < TYPE_NFIELDS (type); i++) 1314 { 1315 const char *t_field_name = TYPE_FIELD_NAME (type, i); 1316 1317 if (t_field_name && (strcmp_iw (t_field_name, field) == 0)) 1318 { 1319 return convert_field (type, i); 1320 } 1321 } 1322 PyErr_SetObject (PyExc_KeyError, key); 1323 return NULL; 1324 } 1325 1326 /* Implement the "get" method on the type object. This is the 1327 same as getitem if the key is present, but returns the supplied 1328 default value or None if the key is not found. */ 1329 1330 static PyObject * 1331 typy_get (PyObject *self, PyObject *args) 1332 { 1333 PyObject *key, *defval = Py_None, *result; 1334 1335 if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval)) 1336 return NULL; 1337 1338 result = typy_getitem (self, key); 1339 if (result != NULL) 1340 return result; 1341 1342 /* typy_getitem returned error status. If the exception is 1343 KeyError, clear the exception status and return the defval 1344 instead. Otherwise return the exception unchanged. */ 1345 if (!PyErr_ExceptionMatches (PyExc_KeyError)) 1346 return NULL; 1347 1348 PyErr_Clear (); 1349 Py_INCREF (defval); 1350 return defval; 1351 } 1352 1353 /* Implement the "has_key" method on the type object. */ 1354 1355 static PyObject * 1356 typy_has_key (PyObject *self, PyObject *args) 1357 { 1358 struct type *type = ((type_object *) self)->type; 1359 const char *field; 1360 int i; 1361 1362 if (!PyArg_ParseTuple (args, "s", &field)) 1363 return NULL; 1364 1365 /* We want just fields of this type, not of base types, so instead of 1366 using lookup_struct_elt_type, portions of that function are 1367 copied here. */ 1368 1369 type = typy_get_composite (type); 1370 if (type == NULL) 1371 return NULL; 1372 1373 for (i = 0; i < TYPE_NFIELDS (type); i++) 1374 { 1375 const char *t_field_name = TYPE_FIELD_NAME (type, i); 1376 1377 if (t_field_name && (strcmp_iw (t_field_name, field) == 0)) 1378 Py_RETURN_TRUE; 1379 } 1380 Py_RETURN_FALSE; 1381 } 1382 1383 /* Make an iterator object to iterate over keys, values, or items. */ 1384 1385 static PyObject * 1386 typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind) 1387 { 1388 typy_iterator_object *typy_iter_obj; 1389 1390 /* Check that "self" is a structure or union type. */ 1391 if (typy_get_composite (((type_object *) self)->type) == NULL) 1392 return NULL; 1393 1394 typy_iter_obj = PyObject_New (typy_iterator_object, 1395 &type_iterator_object_type); 1396 if (typy_iter_obj == NULL) 1397 return NULL; 1398 1399 typy_iter_obj->field = 0; 1400 typy_iter_obj->kind = kind; 1401 Py_INCREF (self); 1402 typy_iter_obj->source = (type_object *) self; 1403 1404 return (PyObject *) typy_iter_obj; 1405 } 1406 1407 /* iteritems() method. */ 1408 1409 static PyObject * 1410 typy_iteritems (PyObject *self, PyObject *args) 1411 { 1412 return typy_make_iter (self, iter_items); 1413 } 1414 1415 /* iterkeys() method. */ 1416 1417 static PyObject * 1418 typy_iterkeys (PyObject *self, PyObject *args) 1419 { 1420 return typy_make_iter (self, iter_keys); 1421 } 1422 1423 /* Iterating over the class, same as iterkeys except for the function 1424 signature. */ 1425 1426 static PyObject * 1427 typy_iter (PyObject *self) 1428 { 1429 return typy_make_iter (self, iter_keys); 1430 } 1431 1432 /* itervalues() method. */ 1433 1434 static PyObject * 1435 typy_itervalues (PyObject *self, PyObject *args) 1436 { 1437 return typy_make_iter (self, iter_values); 1438 } 1439 1440 /* Return a reference to the type iterator. */ 1441 1442 static PyObject * 1443 typy_iterator_iter (PyObject *self) 1444 { 1445 Py_INCREF (self); 1446 return self; 1447 } 1448 1449 /* Return the next field in the iteration through the list of fields 1450 of the type. */ 1451 1452 static PyObject * 1453 typy_iterator_iternext (PyObject *self) 1454 { 1455 typy_iterator_object *iter_obj = (typy_iterator_object *) self; 1456 struct type *type = iter_obj->source->type; 1457 PyObject *result; 1458 1459 if (iter_obj->field < TYPE_NFIELDS (type)) 1460 { 1461 result = make_fielditem (type, iter_obj->field, iter_obj->kind); 1462 if (result != NULL) 1463 iter_obj->field++; 1464 return result; 1465 } 1466 1467 return NULL; 1468 } 1469 1470 static void 1471 typy_iterator_dealloc (PyObject *obj) 1472 { 1473 typy_iterator_object *iter_obj = (typy_iterator_object *) obj; 1474 1475 Py_DECREF (iter_obj->source); 1476 } 1477 1478 /* Create a new Type referring to TYPE. */ 1479 PyObject * 1480 type_to_type_object (struct type *type) 1481 { 1482 type_object *type_obj; 1483 1484 type_obj = PyObject_New (type_object, &type_object_type); 1485 if (type_obj) 1486 set_type (type_obj, type); 1487 1488 return (PyObject *) type_obj; 1489 } 1490 1491 struct type * 1492 type_object_to_type (PyObject *obj) 1493 { 1494 if (! PyObject_TypeCheck (obj, &type_object_type)) 1495 return NULL; 1496 return ((type_object *) obj)->type; 1497 } 1498 1499 1500 1501 /* Implementation of gdb.lookup_type. */ 1502 PyObject * 1503 gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) 1504 { 1505 static char *keywords[] = { "name", "block", NULL }; 1506 const char *type_name = NULL; 1507 struct type *type = NULL; 1508 PyObject *block_obj = NULL; 1509 const struct block *block = NULL; 1510 1511 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords, 1512 &type_name, &block_obj)) 1513 return NULL; 1514 1515 if (block_obj) 1516 { 1517 block = block_object_to_block (block_obj); 1518 if (! block) 1519 { 1520 PyErr_SetString (PyExc_RuntimeError, 1521 _("'block' argument must be a Block.")); 1522 return NULL; 1523 } 1524 } 1525 1526 type = typy_lookup_typename (type_name, block); 1527 if (! type) 1528 return NULL; 1529 1530 return (PyObject *) type_to_type_object (type); 1531 } 1532 1533 void 1534 gdbpy_initialize_types (void) 1535 { 1536 int i; 1537 1538 typy_objfile_data_key 1539 = register_objfile_data_with_cleanup (save_objfile_types, NULL); 1540 1541 if (PyType_Ready (&type_object_type) < 0) 1542 return; 1543 if (PyType_Ready (&field_object_type) < 0) 1544 return; 1545 if (PyType_Ready (&type_iterator_object_type) < 0) 1546 return; 1547 1548 for (i = 0; pyty_codes[i].name; ++i) 1549 { 1550 if (PyModule_AddIntConstant (gdb_module, 1551 /* Cast needed for Python 2.4. */ 1552 (char *) pyty_codes[i].name, 1553 pyty_codes[i].code) < 0) 1554 return; 1555 } 1556 1557 Py_INCREF (&type_object_type); 1558 PyModule_AddObject (gdb_module, "Type", (PyObject *) &type_object_type); 1559 1560 Py_INCREF (&type_iterator_object_type); 1561 PyModule_AddObject (gdb_module, "TypeIterator", 1562 (PyObject *) &type_iterator_object_type); 1563 1564 Py_INCREF (&field_object_type); 1565 PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type); 1566 } 1567 1568 1569 1570 static PyGetSetDef type_object_getset[] = 1571 { 1572 { "code", typy_get_code, NULL, 1573 "The code for this type.", NULL }, 1574 { "sizeof", typy_get_sizeof, NULL, 1575 "The size of this type, in bytes.", NULL }, 1576 { "tag", typy_get_tag, NULL, 1577 "The tag name for this type, or None.", NULL }, 1578 { NULL } 1579 }; 1580 1581 static PyMethodDef type_object_methods[] = 1582 { 1583 { "array", typy_array, METH_VARARGS, 1584 "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ 1585 Return a type which represents an array of objects of this type.\n\ 1586 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ 1587 If LOW_BOUND is omitted, a value of zero is used." }, 1588 { "vector", typy_vector, METH_VARARGS, 1589 "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ 1590 Return a type which represents a vector of objects of this type.\n\ 1591 The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ 1592 If LOW_BOUND is omitted, a value of zero is used.\n\ 1593 Vectors differ from arrays in that if the current language has C-style\n\ 1594 arrays, vectors don't decay to a pointer to the first element.\n\ 1595 They are first class values." }, 1596 { "__contains__", typy_has_key, METH_VARARGS, 1597 "T.__contains__(k) -> True if T has a field named k, else False" }, 1598 { "const", typy_const, METH_NOARGS, 1599 "const () -> Type\n\ 1600 Return a const variant of this type." }, 1601 { "fields", typy_fields, METH_NOARGS, 1602 "fields () -> list\n\ 1603 Return a list holding all the fields of this type.\n\ 1604 Each field is a gdb.Field object." }, 1605 { "get", typy_get, METH_VARARGS, 1606 "T.get(k[,default]) -> returns field named k in T, if it exists;\n\ 1607 otherwise returns default, if supplied, or None if not." }, 1608 { "has_key", typy_has_key, METH_VARARGS, 1609 "T.has_key(k) -> True if T has a field named k, else False" }, 1610 { "items", typy_items, METH_NOARGS, 1611 "items () -> list\n\ 1612 Return a list of (name, field) pairs of this type.\n\ 1613 Each field is a gdb.Field object." }, 1614 { "iteritems", typy_iteritems, METH_NOARGS, 1615 "iteritems () -> an iterator over the (name, field)\n\ 1616 pairs of this type. Each field is a gdb.Field object." }, 1617 { "iterkeys", typy_iterkeys, METH_NOARGS, 1618 "iterkeys () -> an iterator over the field names of this type." }, 1619 { "itervalues", typy_itervalues, METH_NOARGS, 1620 "itervalues () -> an iterator over the fields of this type.\n\ 1621 Each field is a gdb.Field object." }, 1622 { "keys", typy_field_names, METH_NOARGS, 1623 "keys () -> list\n\ 1624 Return a list holding all the fields names of this type." }, 1625 { "pointer", typy_pointer, METH_NOARGS, 1626 "pointer () -> Type\n\ 1627 Return a type of pointer to this type." }, 1628 { "range", typy_range, METH_NOARGS, 1629 "range () -> tuple\n\ 1630 Return a tuple containing the lower and upper range for this type."}, 1631 { "reference", typy_reference, METH_NOARGS, 1632 "reference () -> Type\n\ 1633 Return a type of reference to this type." }, 1634 { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, 1635 "strip_typedefs () -> Type\n\ 1636 Return a type formed by stripping this type of all typedefs."}, 1637 { "target", typy_target, METH_NOARGS, 1638 "target () -> Type\n\ 1639 Return the target type of this type." }, 1640 { "template_argument", typy_template_argument, METH_VARARGS, 1641 "template_argument (arg, [block]) -> Type\n\ 1642 Return the type of a template argument." }, 1643 { "unqualified", typy_unqualified, METH_NOARGS, 1644 "unqualified () -> Type\n\ 1645 Return a variant of this type without const or volatile attributes." }, 1646 { "values", typy_values, METH_NOARGS, 1647 "values () -> list\n\ 1648 Return a list holding all the fields of this type.\n\ 1649 Each field is a gdb.Field object." }, 1650 { "volatile", typy_volatile, METH_NOARGS, 1651 "volatile () -> Type\n\ 1652 Return a volatile variant of this type" }, 1653 { NULL } 1654 }; 1655 1656 static PyNumberMethods type_object_as_number = { 1657 NULL, /* nb_add */ 1658 NULL, /* nb_subtract */ 1659 NULL, /* nb_multiply */ 1660 #ifndef IS_PY3K 1661 NULL, /* nb_divide */ 1662 #endif 1663 NULL, /* nb_remainder */ 1664 NULL, /* nb_divmod */ 1665 NULL, /* nb_power */ 1666 NULL, /* nb_negative */ 1667 NULL, /* nb_positive */ 1668 NULL, /* nb_absolute */ 1669 typy_nonzero, /* nb_nonzero */ 1670 NULL, /* nb_invert */ 1671 NULL, /* nb_lshift */ 1672 NULL, /* nb_rshift */ 1673 NULL, /* nb_and */ 1674 NULL, /* nb_xor */ 1675 NULL, /* nb_or */ 1676 #ifdef IS_PY3K 1677 NULL, /* nb_int */ 1678 NULL, /* reserved */ 1679 #else 1680 NULL, /* nb_coerce */ 1681 NULL, /* nb_int */ 1682 NULL, /* nb_long */ 1683 #endif 1684 NULL, /* nb_float */ 1685 #ifndef IS_PY3K 1686 NULL, /* nb_oct */ 1687 NULL /* nb_hex */ 1688 #endif 1689 }; 1690 1691 static PyMappingMethods typy_mapping = { 1692 typy_length, 1693 typy_getitem, 1694 NULL /* no "set" method */ 1695 }; 1696 1697 static PyTypeObject type_object_type = 1698 { 1699 PyVarObject_HEAD_INIT (NULL, 0) 1700 "gdb.Type", /*tp_name*/ 1701 sizeof (type_object), /*tp_basicsize*/ 1702 0, /*tp_itemsize*/ 1703 typy_dealloc, /*tp_dealloc*/ 1704 0, /*tp_print*/ 1705 0, /*tp_getattr*/ 1706 0, /*tp_setattr*/ 1707 0, /*tp_compare*/ 1708 0, /*tp_repr*/ 1709 &type_object_as_number, /*tp_as_number*/ 1710 0, /*tp_as_sequence*/ 1711 &typy_mapping, /*tp_as_mapping*/ 1712 0, /*tp_hash */ 1713 0, /*tp_call*/ 1714 typy_str, /*tp_str*/ 1715 0, /*tp_getattro*/ 1716 0, /*tp_setattro*/ 1717 0, /*tp_as_buffer*/ 1718 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 1719 "GDB type object", /* tp_doc */ 1720 0, /* tp_traverse */ 1721 0, /* tp_clear */ 1722 typy_richcompare, /* tp_richcompare */ 1723 0, /* tp_weaklistoffset */ 1724 typy_iter, /* tp_iter */ 1725 0, /* tp_iternext */ 1726 type_object_methods, /* tp_methods */ 1727 0, /* tp_members */ 1728 type_object_getset, /* tp_getset */ 1729 0, /* tp_base */ 1730 0, /* tp_dict */ 1731 0, /* tp_descr_get */ 1732 0, /* tp_descr_set */ 1733 0, /* tp_dictoffset */ 1734 0, /* tp_init */ 1735 0, /* tp_alloc */ 1736 0, /* tp_new */ 1737 }; 1738 1739 static PyGetSetDef field_object_getset[] = 1740 { 1741 { "__dict__", gdb_py_generic_dict, NULL, 1742 "The __dict__ for this field.", &field_object_type }, 1743 { NULL } 1744 }; 1745 1746 static PyTypeObject field_object_type = 1747 { 1748 PyVarObject_HEAD_INIT (NULL, 0) 1749 "gdb.Field", /*tp_name*/ 1750 sizeof (field_object), /*tp_basicsize*/ 1751 0, /*tp_itemsize*/ 1752 field_dealloc, /*tp_dealloc*/ 1753 0, /*tp_print*/ 1754 0, /*tp_getattr*/ 1755 0, /*tp_setattr*/ 1756 0, /*tp_compare*/ 1757 0, /*tp_repr*/ 1758 0, /*tp_as_number*/ 1759 0, /*tp_as_sequence*/ 1760 0, /*tp_as_mapping*/ 1761 0, /*tp_hash */ 1762 0, /*tp_call*/ 1763 0, /*tp_str*/ 1764 0, /*tp_getattro*/ 1765 0, /*tp_setattro*/ 1766 0, /*tp_as_buffer*/ 1767 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 1768 "GDB field object", /* tp_doc */ 1769 0, /* tp_traverse */ 1770 0, /* tp_clear */ 1771 0, /* tp_richcompare */ 1772 0, /* tp_weaklistoffset */ 1773 0, /* tp_iter */ 1774 0, /* tp_iternext */ 1775 0, /* tp_methods */ 1776 0, /* tp_members */ 1777 field_object_getset, /* tp_getset */ 1778 0, /* tp_base */ 1779 0, /* tp_dict */ 1780 0, /* tp_descr_get */ 1781 0, /* tp_descr_set */ 1782 offsetof (field_object, dict), /* tp_dictoffset */ 1783 0, /* tp_init */ 1784 0, /* tp_alloc */ 1785 0, /* tp_new */ 1786 }; 1787 1788 static PyTypeObject type_iterator_object_type = { 1789 PyVarObject_HEAD_INIT (NULL, 0) 1790 "gdb.TypeIterator", /*tp_name*/ 1791 sizeof (typy_iterator_object), /*tp_basicsize*/ 1792 0, /*tp_itemsize*/ 1793 typy_iterator_dealloc, /*tp_dealloc*/ 1794 0, /*tp_print*/ 1795 0, /*tp_getattr*/ 1796 0, /*tp_setattr*/ 1797 0, /*tp_compare*/ 1798 0, /*tp_repr*/ 1799 0, /*tp_as_number*/ 1800 0, /*tp_as_sequence*/ 1801 0, /*tp_as_mapping*/ 1802 0, /*tp_hash */ 1803 0, /*tp_call*/ 1804 0, /*tp_str*/ 1805 0, /*tp_getattro*/ 1806 0, /*tp_setattro*/ 1807 0, /*tp_as_buffer*/ 1808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/ 1809 "GDB type iterator object", /*tp_doc */ 1810 0, /*tp_traverse */ 1811 0, /*tp_clear */ 1812 0, /*tp_richcompare */ 1813 0, /*tp_weaklistoffset */ 1814 typy_iterator_iter, /*tp_iter */ 1815 typy_iterator_iternext, /*tp_iternext */ 1816 0 /*tp_methods */ 1817 }; 1818