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