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