d-valprint.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Support for printing D values for GDB, the GNU debugger.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbtypes.h"
  16. #include "gdbcore.h"
  17. #include "d-lang.h"
  18. #include "c-lang.h"
  19. /* Assuming that TYPE is a TYPE_CODE_STRUCT, verify that TYPE is a
  20. dynamic array, and then print its value to STREAM. Return zero if
  21. TYPE is a dynamic array, non-zero otherwise. */
  22. static int
  23. dynamic_array_type (struct type *type,
  24. LONGEST embedded_offset, CORE_ADDR address,
  25. struct ui_file *stream, int recurse,
  26. struct value *val,
  27. const struct value_print_options *options)
  28. {
  29. if (type->num_fields () == 2
  30. && type->field (0).type ()->code () == TYPE_CODE_INT
  31. && strcmp (type->field (0).name (), "length") == 0
  32. && strcmp (type->field (1).name (), "ptr") == 0
  33. && !value_bits_any_optimized_out (val,
  34. TARGET_CHAR_BIT * embedded_offset,
  35. TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  36. {
  37. CORE_ADDR addr;
  38. struct type *elttype;
  39. struct type *true_type;
  40. struct type *ptr_type;
  41. struct value *ival;
  42. int length;
  43. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  44. length = unpack_field_as_long (type, valaddr + embedded_offset, 0);
  45. ptr_type = type->field (1).type ();
  46. elttype = check_typedef (TYPE_TARGET_TYPE (ptr_type));
  47. addr = unpack_pointer (ptr_type,
  48. valaddr + type->field (1).loc_bitpos () / 8
  49. + embedded_offset);
  50. true_type = check_typedef (elttype);
  51. true_type = lookup_array_range_type (true_type, 0, length - 1);
  52. ival = value_at (true_type, addr);
  53. true_type = value_type (ival);
  54. d_value_print_inner (ival, stream, recurse + 1, options);
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. /* See d-lang.h. */
  60. void
  61. d_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
  62. const struct value_print_options *options)
  63. {
  64. int ret;
  65. struct type *type = check_typedef (value_type (val));
  66. switch (type->code ())
  67. {
  68. case TYPE_CODE_STRUCT:
  69. ret = dynamic_array_type (type, value_embedded_offset (val),
  70. value_address (val),
  71. stream, recurse, val, options);
  72. if (ret == 0)
  73. break;
  74. /* Fall through. */
  75. default:
  76. c_value_print_inner (val, stream, recurse, options);
  77. break;
  78. }
  79. }