1 /* Support for printing D values for GDB, the GNU debugger.
2
3 Copyright (C) 2008-2024 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 "gdbtypes.h"
21 #include "gdbcore.h"
22 #include "d-lang.h"
23 #include "c-lang.h"
24
25 /* Assuming that TYPE is a TYPE_CODE_STRUCT, verify that TYPE is a
26 dynamic array, and then print its value to STREAM. Return zero if
27 TYPE is a dynamic array, non-zero otherwise. */
28
29 static int
dynamic_array_type(struct type * type,LONGEST embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,struct value * val,const struct value_print_options * options)30 dynamic_array_type (struct type *type,
31 LONGEST embedded_offset, CORE_ADDR address,
32 struct ui_file *stream, int recurse,
33 struct value *val,
34 const struct value_print_options *options)
35 {
36 if (type->num_fields () == 2
37 && type->field (0).type ()->code () == TYPE_CODE_INT
38 && strcmp (type->field (0).name (), "length") == 0
39 && strcmp (type->field (1).name (), "ptr") == 0
40 && !val->bits_any_optimized_out (TARGET_CHAR_BIT * embedded_offset,
41 TARGET_CHAR_BIT * type->length ()))
42 {
43 CORE_ADDR addr;
44 struct type *elttype;
45 struct type *true_type;
46 struct type *ptr_type;
47 struct value *ival;
48 int length;
49 const gdb_byte *valaddr = val->contents_for_printing ().data ();
50
51 length = unpack_field_as_long (type, valaddr + embedded_offset, 0);
52
53 ptr_type = type->field (1).type ();
54 elttype = check_typedef (ptr_type->target_type ());
55 addr = unpack_pointer (ptr_type,
56 valaddr + type->field (1).loc_bitpos () / 8
57 + embedded_offset);
58 true_type = check_typedef (elttype);
59
60 true_type = lookup_array_range_type (true_type, 0, length - 1);
61 ival = value_at (true_type, addr);
62 true_type = ival->type ();
63
64 d_value_print_inner (ival, stream, recurse + 1, options);
65 return 0;
66 }
67 return 1;
68 }
69
70 /* See d-lang.h. */
71
72 void
d_value_print_inner(struct value * val,struct ui_file * stream,int recurse,const struct value_print_options * options)73 d_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
74 const struct value_print_options *options)
75 {
76 int ret;
77
78 struct type *type = check_typedef (val->type ());
79 switch (type->code ())
80 {
81 case TYPE_CODE_STRUCT:
82 ret = dynamic_array_type (type, val->embedded_offset (),
83 val->address (),
84 stream, recurse, val, options);
85 if (ret == 0)
86 break;
87 [[fallthrough]];
88 default:
89 c_value_print_inner (val, stream, recurse, options);
90 break;
91 }
92 }
93