Sort includes for files gdb/[a-f]*.[chyl].
[external/binutils.git] / gdb / d-valprint.c
1 /* Support for printing D values for GDB, the GNU debugger.
2
3    Copyright (C) 2008-2019 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
22 /* Local non-gdb includes.  */
23 #include "c-lang.h"
24 #include "d-lang.h"
25 #include "gdbcore.h"
26 #include "gdbtypes.h"
27
28 /* Assuming that TYPE is a TYPE_CODE_STRUCT, verify that TYPE is a
29    dynamic array, and then print its value to STREAM.  Return zero if
30    TYPE is a dynamic array, non-zero otherwise.  */
31
32 static int
33 dynamic_array_type (struct type *type,
34                     LONGEST embedded_offset, CORE_ADDR address,
35                     struct ui_file *stream, int recurse,
36                     struct value *val,
37                     const struct value_print_options *options)
38 {
39   if (TYPE_NFIELDS (type) == 2
40       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_INT
41       && strcmp (TYPE_FIELD_NAME (type, 0), "length") == 0
42       && strcmp (TYPE_FIELD_NAME (type, 1), "ptr") == 0
43       && !value_bits_any_optimized_out (val,
44                                         TARGET_CHAR_BIT * embedded_offset,
45                                         TARGET_CHAR_BIT * TYPE_LENGTH (type)))
46     {
47       CORE_ADDR addr;
48       struct type *elttype;
49       struct type *true_type;
50       struct type *ptr_type;
51       struct value *ival;
52       int length;
53       const gdb_byte *valaddr = value_contents_for_printing (val);
54
55       length = unpack_field_as_long (type, valaddr + embedded_offset, 0);
56
57       ptr_type = TYPE_FIELD_TYPE (type, 1);
58       elttype = check_typedef (TYPE_TARGET_TYPE (ptr_type));
59       addr = unpack_pointer (ptr_type,
60                              valaddr + TYPE_FIELD_BITPOS (type, 1) / 8
61                              + embedded_offset);
62       true_type = check_typedef (elttype);
63
64       true_type = lookup_array_range_type (true_type, 0, length - 1);
65       ival = value_at (true_type, addr);
66       true_type = value_type (ival);
67
68       d_val_print (true_type,
69                    value_embedded_offset (ival), addr,
70                    stream, recurse + 1, ival, options);
71       return 0;
72     }
73   return 1;
74 }
75
76 /* Implements the la_val_print routine for language D.  */
77 void
78 d_val_print (struct type *type, int embedded_offset,
79              CORE_ADDR address, struct ui_file *stream, int recurse,
80              struct value *val,
81              const struct value_print_options *options)
82 {
83   int ret;
84
85   type = check_typedef (type);
86   switch (TYPE_CODE (type))
87     {
88       case TYPE_CODE_STRUCT:
89         ret = dynamic_array_type (type, embedded_offset, address,
90                                   stream, recurse, val, options);
91         if (ret == 0)
92           break;
93         /* Fall through.  */
94       default:
95         c_val_print (type, embedded_offset, address, stream,
96                      recurse, val, options);
97     }
98 }