]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ch-valprint.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / ch-valprint.c
CommitLineData
c906108c
SS
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
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "obstack.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "valprint.h"
27#include "expression.h"
28#include "value.h"
29#include "language.h"
30#include "demangle.h"
c5aa993b 31#include "c-lang.h" /* For c_val_print */
c906108c
SS
32#include "typeprint.h"
33#include "ch-lang.h"
34#include "annotate.h"
35
36static void
37chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
38 enum val_prettyprint, struct type **));
39
40static void
41chill_print_type_scalar PARAMS ((struct type *, LONGEST, GDB_FILE *));
42
43static void
44chill_val_print_array_elements PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
c5aa993b 45 int, int, int, enum val_prettyprint));
c906108c 46\f
c5aa993b 47
c906108c
SS
48/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
49 Used to print data from type structures in a specified type. For example,
50 array bounds may be characters or booleans in some languages, and this
51 allows the ranges to be printed in their "natural" form rather than as
52 decimal integer values. */
53
54static void
55chill_print_type_scalar (type, val, stream)
56 struct type *type;
57 LONGEST val;
58 GDB_FILE *stream;
59{
60 switch (TYPE_CODE (type))
61 {
62 case TYPE_CODE_RANGE:
63 if (TYPE_TARGET_TYPE (type))
64 {
65 chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
66 return;
67 }
68 break;
69 case TYPE_CODE_UNDEF:
70 case TYPE_CODE_PTR:
71 case TYPE_CODE_ARRAY:
72 case TYPE_CODE_STRUCT:
73 case TYPE_CODE_UNION:
74 case TYPE_CODE_ENUM:
75 case TYPE_CODE_FUNC:
76 case TYPE_CODE_INT:
77 case TYPE_CODE_FLT:
78 case TYPE_CODE_VOID:
79 case TYPE_CODE_SET:
80 case TYPE_CODE_STRING:
81 case TYPE_CODE_BITSTRING:
82 case TYPE_CODE_ERROR:
83 case TYPE_CODE_MEMBER:
84 case TYPE_CODE_METHOD:
85 case TYPE_CODE_REF:
86 case TYPE_CODE_CHAR:
87 case TYPE_CODE_BOOL:
88 case TYPE_CODE_COMPLEX:
89 case TYPE_CODE_TYPEDEF:
90 default:
91 break;
92 }
93 print_type_scalar (type, val, stream);
94}
95\f
96/* Print the elements of an array.
97 Similar to val_print_array_elements, but prints
98 element indexes (in Chill syntax). */
99
100static void
101chill_val_print_array_elements (type, valaddr, address, stream,
102 format, deref_ref, recurse, pretty)
103 struct type *type;
104 char *valaddr;
105 CORE_ADDR address;
106 GDB_FILE *stream;
107 int format;
108 int deref_ref;
109 int recurse;
110 enum val_prettyprint pretty;
111{
112 unsigned int i = 0;
113 unsigned int things_printed = 0;
114 unsigned len;
115 struct type *elttype;
116 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
117 struct type *index_type = TYPE_TARGET_TYPE (range_type);
118 unsigned eltlen;
119 /* Position of the array element we are examining to see
120 whether it is repeated. */
121 unsigned int rep1;
122 /* Number of repetitions we have detected so far. */
123 unsigned int reps;
c5aa993b
JM
124 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
125
c906108c
SS
126 elttype = check_typedef (TYPE_TARGET_TYPE (type));
127 eltlen = TYPE_LENGTH (elttype);
128 len = TYPE_LENGTH (type) / eltlen;
129
130 annotate_array_section_begin (i, elttype);
131
132 for (; i < len && things_printed < print_max; i++)
133 {
134 if (i != 0)
135 {
136 if (prettyprint_arrays)
137 {
138 fprintf_filtered (stream, ",\n");
139 print_spaces_filtered (2 + 2 * recurse, stream);
140 }
141 else
142 {
143 fprintf_filtered (stream, ", ");
144 }
145 }
146 wrap_here (n_spaces (2 + 2 * recurse));
147
148 rep1 = i + 1;
149 reps = 1;
c5aa993b 150 while ((rep1 < len) &&
c906108c
SS
151 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
152 {
153 ++reps;
154 ++rep1;
155 }
156
157 fputs_filtered ("(", stream);
158 chill_print_type_scalar (index_type, low_bound + i, stream);
159 if (reps > 1)
160 {
161 fputs_filtered (":", stream);
162 chill_print_type_scalar (index_type, low_bound + i + reps - 1,
163 stream);
164 fputs_filtered ("): ", stream);
165 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
166 deref_ref, recurse + 1, pretty);
167
168 i = rep1 - 1;
169 things_printed += 1;
170 }
171 else
172 {
173 fputs_filtered ("): ", stream);
174 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
175 deref_ref, recurse + 1, pretty);
176 annotate_elt ();
177 things_printed++;
178 }
179 }
180 annotate_array_section_end ();
181 if (i < len)
182 {
183 fprintf_filtered (stream, "...");
184 }
185}
186
187/* Print data of type TYPE located at VALADDR (within GDB), which came from
188 the inferior at address ADDRESS, onto stdio stream STREAM according to
189 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
190 target byte order.
191
192 If the data are a string pointer, returns the number of string characters
193 printed.
194
195 If DEREF_REF is nonzero, then dereference references, otherwise just print
196 them like pointers.
197
198 The PRETTY parameter controls prettyprinting. */
199
200int
201chill_val_print (type, valaddr, embedded_offset, address,
c5aa993b 202 stream, format, deref_ref, recurse, pretty)
c906108c
SS
203 struct type *type;
204 char *valaddr;
205 int embedded_offset;
206 CORE_ADDR address;
207 GDB_FILE *stream;
208 int format;
209 int deref_ref;
210 int recurse;
211 enum val_prettyprint pretty;
212{
213 LONGEST val;
214 unsigned int i = 0; /* Number of characters printed. */
215 struct type *elttype;
216 CORE_ADDR addr;
217
218 CHECK_TYPEDEF (type);
219
220 switch (TYPE_CODE (type))
221 {
222 case TYPE_CODE_ARRAY:
223 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
224 {
225 if (prettyprint_arrays)
226 {
227 print_spaces_filtered (2 + 2 * recurse, stream);
228 }
229 fprintf_filtered (stream, "[");
230 chill_val_print_array_elements (type, valaddr, address, stream,
c5aa993b 231 format, deref_ref, recurse, pretty);
c906108c
SS
232 fprintf_filtered (stream, "]");
233 }
234 else
235 {
236 error ("unimplemented in chill_val_print; unspecified array length");
237 }
238 break;
239
240 case TYPE_CODE_INT:
241 format = format ? format : output_format;
242 if (format)
243 {
244 print_scalar_formatted (valaddr, type, format, 0, stream);
245 }
246 else
247 {
248 val_print_type_code_int (type, valaddr, stream);
249 }
250 break;
251
252 case TYPE_CODE_CHAR:
253 format = format ? format : output_format;
254 if (format)
255 {
256 print_scalar_formatted (valaddr, type, format, 0, stream);
257 }
258 else
259 {
260 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
261 stream);
262 }
263 break;
264
265 case TYPE_CODE_FLT:
266 if (format)
267 {
268 print_scalar_formatted (valaddr, type, format, 0, stream);
269 }
270 else
271 {
272 print_floating (valaddr, type, stream);
273 }
274 break;
275
276 case TYPE_CODE_BOOL:
277 format = format ? format : output_format;
278 if (format)
279 {
280 print_scalar_formatted (valaddr, type, format, 0, stream);
281 }
282 else
283 {
284 /* FIXME: Why is this using builtin_type_chill_bool not type? */
285 val = unpack_long (builtin_type_chill_bool, valaddr);
286 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
287 }
288 break;
289
290 case TYPE_CODE_UNDEF:
291 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
c5aa993b
JM
292 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
293 and no complete type for struct foo in that file. */
c906108c
SS
294 fprintf_filtered (stream, "<incomplete type>");
295 break;
296
297 case TYPE_CODE_PTR:
298 if (format && format != 's')
299 {
300 print_scalar_formatted (valaddr, type, format, 0, stream);
301 break;
302 }
303 addr = unpack_pointer (type, valaddr);
304 elttype = check_typedef (TYPE_TARGET_TYPE (type));
305
306 /* We assume a NULL pointer is all zeros ... */
307 if (addr == 0)
308 {
309 fputs_filtered ("NULL", stream);
310 return 0;
311 }
c5aa993b 312
c906108c
SS
313 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
314 {
315 /* Try to print what function it points to. */
316 print_address_demangle (addr, stream, demangle);
317 /* Return value is irrelevant except for string pointers. */
318 return (0);
319 }
320 if (addressprint && format != 's')
321 {
322 print_address_numeric (addr, 1, stream);
323 }
c5aa993b 324
c906108c 325 /* For a pointer to char or unsigned char, also print the string
c5aa993b 326 pointed to, unless pointer is null. */
c906108c
SS
327 if (TYPE_LENGTH (elttype) == 1
328 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
329 && (format == 0 || format == 's')
330 && addr != 0
c5aa993b
JM
331 && /* If print_max is UINT_MAX, the alloca below will fail.
332 In that case don't try to print the string. */
c906108c
SS
333 print_max < UINT_MAX)
334 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
335
336 /* Return number of characters printed, plus one for the
c5aa993b 337 terminating null if we have "reached the end". */
c906108c
SS
338 return (i + (print_max && i != print_max));
339 break;
340
341 case TYPE_CODE_STRING:
342 i = TYPE_LENGTH (type);
343 LA_PRINT_STRING (stream, valaddr, i, 1, 0);
344 /* Return number of characters printed, plus one for the terminating
c5aa993b 345 null if we have "reached the end". */
c906108c
SS
346 return (i + (print_max && i != print_max));
347 break;
348
349 case TYPE_CODE_BITSTRING:
350 case TYPE_CODE_SET:
351 elttype = TYPE_INDEX_TYPE (type);
352 CHECK_TYPEDEF (elttype);
353 if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
354 {
355 fprintf_filtered (stream, "<incomplete type>");
356 gdb_flush (stream);
357 break;
358 }
359 {
360 struct type *range = elttype;
361 LONGEST low_bound, high_bound;
362 int i;
363 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
364 int need_comma = 0;
365
366 if (is_bitstring)
367 fputs_filtered ("B'", stream);
368 else
369 fputs_filtered ("[", stream);
370
371 i = get_discrete_bounds (range, &low_bound, &high_bound);
372 maybe_bad_bstring:
373 if (i < 0)
374 {
375 fputs_filtered ("<error value>", stream);
376 goto done;
377 }
378
379 for (i = low_bound; i <= high_bound; i++)
380 {
381 int element = value_bit_index (type, valaddr, i);
382 if (element < 0)
383 {
384 i = element;
385 goto maybe_bad_bstring;
386 }
387 if (is_bitstring)
388 fprintf_filtered (stream, "%d", element);
389 else if (element)
390 {
391 if (need_comma)
392 fputs_filtered (", ", stream);
393 chill_print_type_scalar (range, (LONGEST) i, stream);
394 need_comma = 1;
395
396 /* Look for a continuous range of true elements. */
c5aa993b 397 if (i + 1 <= high_bound && value_bit_index (type, valaddr, ++i))
c906108c 398 {
c5aa993b 399 int j = i; /* j is the upper bound so far of the range */
c906108c 400 fputs_filtered (":", stream);
c5aa993b 401 while (i + 1 <= high_bound
c906108c
SS
402 && value_bit_index (type, valaddr, ++i))
403 j = i;
404 chill_print_type_scalar (range, (LONGEST) j, stream);
405 }
406 }
407 }
408 done:
409 if (is_bitstring)
410 fputs_filtered ("'", stream);
411 else
412 fputs_filtered ("]", stream);
413 }
414 break;
415
416 case TYPE_CODE_STRUCT:
417 if (chill_varying_type (type))
418 {
419 struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
420 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
421 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
c5aa993b 422
c906108c
SS
423 switch (TYPE_CODE (inner))
424 {
425 case TYPE_CODE_STRING:
426 if (length > TYPE_LENGTH (type) - 2)
427 {
428 fprintf_filtered (stream,
c5aa993b 429 "<dynamic length %ld > static length %d> *invalid*",
c906108c
SS
430 length, TYPE_LENGTH (type));
431
432 /* Don't print the string; doing so might produce a
433 segfault. */
434 return length;
435 }
436 LA_PRINT_STRING (stream, data_addr, length, 1, 0);
437 return length;
438 default:
439 break;
440 }
441 }
442 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
443 0);
444 break;
445
446 case TYPE_CODE_REF:
447 if (addressprint)
c5aa993b 448 {
c906108c
SS
449 fprintf_filtered (stream, "LOC(");
450 print_address_numeric
451 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
452 1,
453 stream);
454 fprintf_filtered (stream, ")");
455 if (deref_ref)
456 fputs_filtered (": ", stream);
c5aa993b 457 }
c906108c
SS
458 /* De-reference the reference. */
459 if (deref_ref)
460 {
461 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
462 {
463 value_ptr deref_val =
c5aa993b
JM
464 value_at
465 (TYPE_TARGET_TYPE (type),
466 unpack_pointer (lookup_pointer_type (builtin_type_void),
467 valaddr),
468 NULL);
c906108c
SS
469 val_print (VALUE_TYPE (deref_val),
470 VALUE_CONTENTS (deref_val),
c5aa993b 471 0,
c906108c
SS
472 VALUE_ADDRESS (deref_val), stream, format,
473 deref_ref, recurse + 1, pretty);
474 }
475 else
476 fputs_filtered ("???", stream);
477 }
478 break;
479
480 case TYPE_CODE_ENUM:
481 c_val_print (type, valaddr, 0, address, stream, format,
482 deref_ref, recurse, pretty);
483 break;
484
485 case TYPE_CODE_RANGE:
486 if (TYPE_TARGET_TYPE (type))
487 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, 0, address, stream,
488 format, deref_ref, recurse, pretty);
489 break;
490
491 case TYPE_CODE_MEMBER:
492 case TYPE_CODE_UNION:
493 case TYPE_CODE_FUNC:
494 case TYPE_CODE_VOID:
495 case TYPE_CODE_ERROR:
496 default:
497 /* Let's defer printing to the C printer, rather than
c5aa993b 498 print an error message. FIXME! */
c906108c
SS
499 c_val_print (type, valaddr, 0, address, stream, format,
500 deref_ref, recurse, pretty);
501 }
502 gdb_flush (stream);
503 return (0);
504}
505
506/* Mutually recursive subroutines of cplus_print_value and c_val_print to
507 print out a structure's fields: cp_print_value_fields and cplus_print_value.
508
509 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
510 same meanings as in cplus_print_value and c_val_print.
511
512 DONT_PRINT is an array of baseclass types that we
513 should not print, or zero if called from top level. */
514
515static void
516chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
517 dont_print)
518 struct type *type;
519 char *valaddr;
520 GDB_FILE *stream;
521 int format;
522 int recurse;
523 enum val_prettyprint pretty;
524 struct type **dont_print;
525{
526 int i, len;
527 int fields_seen = 0;
528
529 CHECK_TYPEDEF (type);
530
531 fprintf_filtered (stream, "[");
532 len = TYPE_NFIELDS (type);
533 if (len == 0)
534 {
535 fprintf_filtered (stream, "<No data fields>");
536 }
537 else
538 {
539 for (i = 0; i < len; i++)
540 {
541 if (fields_seen)
542 {
543 fprintf_filtered (stream, ", ");
544 }
545 fields_seen = 1;
546 if (pretty)
547 {
548 fprintf_filtered (stream, "\n");
549 print_spaces_filtered (2 + 2 * recurse, stream);
550 }
c5aa993b 551 else
c906108c
SS
552 {
553 wrap_here (n_spaces (2 + 2 * recurse));
554 }
555 fputs_filtered (".", stream);
556 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
557 language_chill, DMGL_NO_OPTS);
558 fputs_filtered (": ", stream);
559 if (TYPE_FIELD_PACKED (type, i))
560 {
561 value_ptr v;
562
563 /* Bitfields require special handling, especially due to byte
c5aa993b 564 order problems. */
c906108c 565 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
c5aa993b 566 unpack_field_as_long (type, valaddr, i));
c906108c
SS
567
568 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
569 stream, format, 0, recurse + 1, pretty);
570 }
571 else
572 {
c5aa993b 573 chill_val_print (TYPE_FIELD_TYPE (type, i),
c906108c
SS
574 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
575 0, stream, format, 0, recurse + 1, pretty);
576 }
577 }
578 if (pretty)
579 {
580 fprintf_filtered (stream, "\n");
581 print_spaces_filtered (2 * recurse, stream);
582 }
583 }
584 fprintf_filtered (stream, "]");
585}
586\f
587int
588chill_value_print (val, stream, format, pretty)
589 value_ptr val;
590 GDB_FILE *stream;
591 int format;
592 enum val_prettyprint pretty;
593{
594 struct type *type = VALUE_TYPE (val);
c5aa993b 595 struct type *real_type = check_typedef (type);
c906108c
SS
596
597 /* If it is a pointer, indicate what it points to.
598
599 Print type also if it is a reference. */
600
601 if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
602 TYPE_CODE (real_type) == TYPE_CODE_REF)
603 {
604 char *valaddr = VALUE_CONTENTS (val);
605 CORE_ADDR addr = unpack_pointer (type, valaddr);
606 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
607 {
608 int i;
609 char *name = TYPE_NAME (type);
610 if (name)
611 fputs_filtered (name, stream);
612 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
613 fputs_filtered ("PTR", stream);
614 else
615 {
616 fprintf_filtered (stream, "(");
617 type_print (type, "", stream, -1);
618 fprintf_filtered (stream, ")");
619 }
620 fprintf_filtered (stream, "(");
621 i = val_print (type, valaddr, 0, VALUE_ADDRESS (val),
622 stream, format, 1, 0, pretty);
623 fprintf_filtered (stream, ")");
624 return i;
625 }
626 }
627 return (val_print (type, VALUE_CONTENTS (val), 0,
628 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
629}