* valops.c (value_assign): Set `type' after coercing toval.
[deliverable/binutils-gdb.git] / gdb / ch-valprint.c
1 /* Support for printing Chill values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
3 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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "obstack.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "valprint.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "language.h"
29 #include "demangle.h"
30 #include "c-lang.h" /* For c_val_print */
31
32 static void
33 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
34 enum val_prettyprint, struct type **));
35
36 \f
37 /* Print data of type TYPE located at VALADDR (within GDB), which came from
38 the inferior at address ADDRESS, onto stdio stream STREAM according to
39 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
40 target byte order.
41
42 If the data are a string pointer, returns the number of string characters
43 printed.
44
45 If DEREF_REF is nonzero, then dereference references, otherwise just print
46 them like pointers.
47
48 The PRETTY parameter controls prettyprinting. */
49
50 int
51 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
52 pretty)
53 struct type *type;
54 char *valaddr;
55 CORE_ADDR address;
56 GDB_FILE *stream;
57 int format;
58 int deref_ref;
59 int recurse;
60 enum val_prettyprint pretty;
61 {
62 LONGEST val;
63 unsigned int i = 0; /* Number of characters printed. */
64 struct type *elttype;
65 CORE_ADDR addr;
66
67 switch (TYPE_CODE (type))
68 {
69 case TYPE_CODE_ARRAY:
70 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
71 {
72 if (prettyprint_arrays)
73 {
74 print_spaces_filtered (2 + 2 * recurse, stream);
75 }
76 fprintf_filtered (stream, "[");
77 val_print_array_elements (type, valaddr, address, stream, format,
78 deref_ref, recurse, pretty, 0);
79 fprintf_filtered (stream, "]");
80 }
81 else
82 {
83 error ("unimplemented in chill_val_print; unspecified array length");
84 }
85 break;
86
87 case TYPE_CODE_INT:
88 format = format ? format : output_format;
89 if (format)
90 {
91 print_scalar_formatted (valaddr, type, format, 0, stream);
92 }
93 else
94 {
95 val_print_type_code_int (type, valaddr, stream);
96 }
97 break;
98
99 case TYPE_CODE_CHAR:
100 format = format ? format : output_format;
101 if (format)
102 {
103 print_scalar_formatted (valaddr, type, format, 0, stream);
104 }
105 else
106 {
107 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
108 stream);
109 }
110 break;
111
112 case TYPE_CODE_FLT:
113 if (format)
114 {
115 print_scalar_formatted (valaddr, type, format, 0, stream);
116 }
117 else
118 {
119 print_floating (valaddr, type, stream);
120 }
121 break;
122
123 case TYPE_CODE_BOOL:
124 format = format ? format : output_format;
125 if (format)
126 {
127 print_scalar_formatted (valaddr, type, format, 0, stream);
128 }
129 else
130 {
131 val = unpack_long (builtin_type_chill_bool, valaddr);
132 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
133 }
134 break;
135
136 case TYPE_CODE_UNDEF:
137 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
138 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
139 and no complete type for struct foo in that file. */
140 fprintf_filtered (stream, "<incomplete type>");
141 break;
142
143 case TYPE_CODE_PTR:
144 if (format && format != 's')
145 {
146 print_scalar_formatted (valaddr, type, format, 0, stream);
147 break;
148 }
149 addr = unpack_pointer (type, valaddr);
150 elttype = TYPE_TARGET_TYPE (type);
151
152 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
153 {
154 /* Try to print what function it points to. */
155 print_address_demangle (addr, stream, demangle);
156 /* Return value is irrelevant except for string pointers. */
157 return (0);
158 }
159 if (addressprint && format != 's')
160 {
161 fprintf_filtered (stream, "H'%lx", (unsigned long) addr);
162 }
163
164 /* For a pointer to char or unsigned char, also print the string
165 pointed to, unless pointer is null. */
166 if (TYPE_LENGTH (elttype) == 1
167 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
168 && (format == 0 || format == 's')
169 && addr != 0
170 && /* If print_max is UINT_MAX, the alloca below will fail.
171 In that case don't try to print the string. */
172 print_max < UINT_MAX)
173 {
174 i = val_print_string (addr, 0, stream);
175 }
176 /* Return number of characters printed, plus one for the
177 terminating null if we have "reached the end". */
178 return (i + (print_max && i != print_max));
179 break;
180
181 case TYPE_CODE_STRING:
182 if (format && format != 's')
183 {
184 print_scalar_formatted (valaddr, type, format, 0, stream);
185 break;
186 }
187 i = TYPE_LENGTH (type);
188 LA_PRINT_STRING (stream, valaddr, i, 0);
189 /* Return number of characters printed, plus one for the terminating
190 null if we have "reached the end". */
191 return (i + (print_max && i != print_max));
192 break;
193
194 case TYPE_CODE_BITSTRING:
195 case TYPE_CODE_SET:
196 {
197 struct type *range = TYPE_FIELD_TYPE (type, 0);
198 int low_bound = TYPE_LOW_BOUND (range);
199 int high_bound = TYPE_HIGH_BOUND (range);
200 int i;
201 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
202 int need_comma = 0;
203 int in_range = 0;
204
205 if (is_bitstring)
206 fputs_filtered ("B'", stream);
207 else
208 fputs_filtered ("[", stream);
209 for (i = low_bound; i <= high_bound; i++)
210 {
211 int element = value_bit_index (type, valaddr, i);
212 if (is_bitstring)
213 fprintf_filtered (stream, "%d", element);
214 else if (element)
215 {
216 if (need_comma)
217 fputs_filtered (", ", stream);
218 print_type_scalar (TYPE_TARGET_TYPE (range), i, stream);
219 need_comma = 1;
220
221 /* Look for a continuous range of true elements. */
222 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
223 {
224 int j = i; /* j is the upper bound so far of the range */
225 fputs_filtered (":", stream);
226 while (i+1 <= high_bound
227 && value_bit_index (type, valaddr, ++i))
228 j = i;
229 print_type_scalar (TYPE_TARGET_TYPE (range), j, stream);
230 }
231 }
232 }
233 if (is_bitstring)
234 fputs_filtered ("'", stream);
235 else
236 fputs_filtered ("]", stream);
237 }
238 break;
239
240 case TYPE_CODE_STRUCT:
241 if (chill_is_varying_struct (type))
242 {
243 struct type *inner = TYPE_FIELD_TYPE (type, 1);
244 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
245 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
246
247 switch (TYPE_CODE (inner))
248 {
249 case TYPE_CODE_STRING:
250 if (length > TYPE_LENGTH (type))
251 {
252 fprintf_filtered (stream,
253 "<dynamic length %d > static length %d>",
254 length, TYPE_LENGTH (type));
255 length > TYPE_LENGTH (type);
256 }
257 LA_PRINT_STRING (stream, data_addr, length, 0);
258 return length;
259 }
260 }
261 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
262 0);
263 break;
264
265 case TYPE_CODE_REF:
266 if (addressprint)
267 {
268 fprintf_filtered (stream, "LOC(H'%lx)",
269 extract_unsigned_integer (valaddr,
270 TARGET_PTR_BIT / HOST_CHAR_BIT));
271 if (deref_ref)
272 fputs_filtered (": ", stream);
273 }
274 /* De-reference the reference. */
275 if (deref_ref)
276 {
277 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
278 {
279 value deref_val =
280 value_at
281 (TYPE_TARGET_TYPE (type),
282 unpack_pointer (lookup_pointer_type (builtin_type_void),
283 valaddr));
284 val_print (VALUE_TYPE (deref_val),
285 VALUE_CONTENTS (deref_val),
286 VALUE_ADDRESS (deref_val), stream, format,
287 deref_ref, recurse + 1, pretty);
288 }
289 else
290 fputs_filtered ("???", stream);
291 }
292 break;
293
294 case TYPE_CODE_ENUM:
295 c_val_print (type, valaddr, address, stream, format,
296 deref_ref, recurse, pretty);
297 break;
298
299 case TYPE_CODE_RANGE:
300 if (TYPE_TARGET_TYPE (type))
301 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
302 format, deref_ref, recurse, pretty);
303 break;
304
305 case TYPE_CODE_MEMBER:
306 case TYPE_CODE_UNION:
307 case TYPE_CODE_FUNC:
308 case TYPE_CODE_VOID:
309 case TYPE_CODE_ERROR:
310 default:
311 /* Let's defer printing to the C printer, rather than
312 print an error message. FIXME! */
313 c_val_print (type, valaddr, address, stream, format,
314 deref_ref, recurse, pretty);
315 }
316 gdb_flush (stream);
317 return (0);
318 }
319
320 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
321 print out a structure's fields: cp_print_value_fields and cplus_print_value.
322
323 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
324 same meanings as in cplus_print_value and c_val_print.
325
326 DONT_PRINT is an array of baseclass types that we
327 should not print, or zero if called from top level. */
328
329 static void
330 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
331 dont_print)
332 struct type *type;
333 char *valaddr;
334 GDB_FILE *stream;
335 int format;
336 int recurse;
337 enum val_prettyprint pretty;
338 struct type **dont_print;
339 {
340 int i, len;
341 int fields_seen = 0;
342
343 check_stub_type (type);
344
345 fprintf_filtered (stream, "[");
346 len = TYPE_NFIELDS (type);
347 if (len == 0)
348 {
349 fprintf_filtered (stream, "<No data fields>");
350 }
351 else
352 {
353 for (i = 0; i < len; i++)
354 {
355 if (fields_seen)
356 {
357 fprintf_filtered (stream, ", ");
358 }
359 fields_seen = 1;
360 if (pretty)
361 {
362 fprintf_filtered (stream, "\n");
363 print_spaces_filtered (2 + 2 * recurse, stream);
364 }
365 else
366 {
367 wrap_here (n_spaces (2 + 2 * recurse));
368 }
369 fputs_filtered (".", stream);
370 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
371 language_chill, DMGL_NO_OPTS);
372 fputs_filtered (": ", stream);
373 if (TYPE_FIELD_PACKED (type, i))
374 {
375 value v;
376
377 /* Bitfields require special handling, especially due to byte
378 order problems. */
379 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
380 unpack_field_as_long (type, valaddr, i));
381
382 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
383 stream, format, 0, recurse + 1, pretty);
384 }
385 else
386 {
387 chill_val_print (TYPE_FIELD_TYPE (type, i),
388 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
389 0, stream, format, 0, recurse + 1, pretty);
390 }
391 }
392 if (pretty)
393 {
394 fprintf_filtered (stream, "\n");
395 print_spaces_filtered (2 * recurse, stream);
396 }
397 }
398 fprintf_filtered (stream, "]");
399 }
This page took 0.041664 seconds and 5 git commands to generate.