1 /* Support for printing Chill values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #include "expression.h"
32 #include "c-lang.h" /* For c_val_print */
33 #include "typeprint.h"
37 static void chill_print_value_fields (struct type *, char *,
38 struct ui_file *, int, int,
39 enum val_prettyprint, struct type **);
41 static void chill_print_type_scalar (struct type *, LONGEST,
44 static void chill_val_print_array_elements (struct type *, char *,
45 CORE_ADDR, struct ui_file *,
47 enum val_prettyprint);
50 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
51 Used to print data from type structures in a specified type. For example,
52 array bounds may be characters or booleans in some languages, and this
53 allows the ranges to be printed in their "natural" form rather than as
54 decimal integer values. */
57 chill_print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
59 switch (TYPE_CODE (type))
62 if (TYPE_TARGET_TYPE (type))
64 chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
71 case TYPE_CODE_STRUCT:
79 case TYPE_CODE_STRING:
80 case TYPE_CODE_BITSTRING:
82 case TYPE_CODE_MEMBER:
83 case TYPE_CODE_METHOD:
87 case TYPE_CODE_COMPLEX:
88 case TYPE_CODE_TYPEDEF:
92 print_type_scalar (type, val, stream);
95 /* Print the elements of an array.
96 Similar to val_print_array_elements, but prints
97 element indexes (in Chill syntax). */
100 chill_val_print_array_elements (struct type *type, char *valaddr,
101 CORE_ADDR address, struct ui_file *stream,
102 int format, int deref_ref, int recurse,
103 enum val_prettyprint pretty)
106 unsigned int things_printed = 0;
108 struct type *elttype;
109 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
110 struct type *index_type = TYPE_TARGET_TYPE (range_type);
112 /* Position of the array element we are examining to see
113 whether it is repeated. */
115 /* Number of repetitions we have detected so far. */
117 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
119 elttype = check_typedef (TYPE_TARGET_TYPE (type));
120 eltlen = TYPE_LENGTH (elttype);
121 len = TYPE_LENGTH (type) / eltlen;
123 annotate_array_section_begin (i, elttype);
125 for (; i < len && things_printed < print_max; i++)
129 if (prettyprint_arrays)
131 fprintf_filtered (stream, ",\n");
132 print_spaces_filtered (2 + 2 * recurse, stream);
136 fprintf_filtered (stream, ", ");
139 wrap_here (n_spaces (2 + 2 * recurse));
143 while ((rep1 < len) &&
144 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
150 fputs_filtered ("(", stream);
151 chill_print_type_scalar (index_type, low_bound + i, stream);
154 fputs_filtered (":", stream);
155 chill_print_type_scalar (index_type, low_bound + i + reps - 1,
157 fputs_filtered ("): ", stream);
158 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
159 deref_ref, recurse + 1, pretty);
166 fputs_filtered ("): ", stream);
167 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
168 deref_ref, recurse + 1, pretty);
173 annotate_array_section_end ();
176 fprintf_filtered (stream, "...");
180 /* Print data of type TYPE located at VALADDR (within GDB), which came from
181 the inferior at address ADDRESS, onto stdio stream STREAM according to
182 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
185 If the data are a string pointer, returns the number of string characters
188 If DEREF_REF is nonzero, then dereference references, otherwise just print
191 The PRETTY parameter controls prettyprinting. */
194 chill_val_print (struct type *type, char *valaddr, int embedded_offset,
195 CORE_ADDR address, struct ui_file *stream, int format,
196 int deref_ref, int recurse, enum val_prettyprint pretty)
199 unsigned int i = 0; /* Number of characters printed. */
200 struct type *elttype;
203 CHECK_TYPEDEF (type);
205 switch (TYPE_CODE (type))
207 case TYPE_CODE_ARRAY:
208 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
210 if (prettyprint_arrays)
212 print_spaces_filtered (2 + 2 * recurse, stream);
214 fprintf_filtered (stream, "[");
215 chill_val_print_array_elements (type, valaddr, address, stream,
216 format, deref_ref, recurse, pretty);
217 fprintf_filtered (stream, "]");
221 error ("unimplemented in chill_val_print; unspecified array length");
226 format = format ? format : output_format;
229 print_scalar_formatted (valaddr, type, format, 0, stream);
233 val_print_type_code_int (type, valaddr, stream);
238 format = format ? format : output_format;
241 print_scalar_formatted (valaddr, type, format, 0, stream);
245 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
253 print_scalar_formatted (valaddr, type, format, 0, stream);
257 print_floating (valaddr, type, stream);
262 format = format ? format : output_format;
265 print_scalar_formatted (valaddr, type, format, 0, stream);
269 /* FIXME: Why is this using builtin_type_chill_bool not type? */
270 val = unpack_long (builtin_type_chill_bool, valaddr);
271 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
275 case TYPE_CODE_UNDEF:
276 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
277 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
278 and no complete type for struct foo in that file. */
279 fprintf_filtered (stream, "<incomplete type>");
283 if (format && format != 's')
285 print_scalar_formatted (valaddr, type, format, 0, stream);
288 addr = unpack_pointer (type, valaddr);
289 elttype = check_typedef (TYPE_TARGET_TYPE (type));
291 /* We assume a NULL pointer is all zeros ... */
294 fputs_filtered ("NULL", stream);
298 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
300 /* Try to print what function it points to. */
301 print_address_demangle (addr, stream, demangle);
302 /* Return value is irrelevant except for string pointers. */
305 if (addressprint && format != 's')
307 print_address_numeric (addr, 1, stream);
310 /* For a pointer to char or unsigned char, also print the string
311 pointed to, unless pointer is null. */
312 if (TYPE_LENGTH (elttype) == 1
313 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
314 && (format == 0 || format == 's')
316 && /* If print_max is UINT_MAX, the alloca below will fail.
317 In that case don't try to print the string. */
318 print_max < UINT_MAX)
319 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
321 /* Return number of characters printed, plus one for the
322 terminating null if we have "reached the end". */
323 return (i + (print_max && i != print_max));
326 case TYPE_CODE_STRING:
327 i = TYPE_LENGTH (type);
328 LA_PRINT_STRING (stream, valaddr, i, 1, 0);
329 /* Return number of characters printed, plus one for the terminating
330 null if we have "reached the end". */
331 return (i + (print_max && i != print_max));
334 case TYPE_CODE_BITSTRING:
336 elttype = TYPE_INDEX_TYPE (type);
337 CHECK_TYPEDEF (elttype);
338 if (TYPE_STUB (elttype))
340 fprintf_filtered (stream, "<incomplete type>");
345 struct type *range = elttype;
346 LONGEST low_bound, high_bound;
348 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
352 fputs_filtered ("B'", stream);
354 fputs_filtered ("[", stream);
356 i = get_discrete_bounds (range, &low_bound, &high_bound);
360 fputs_filtered ("<error value>", stream);
364 for (i = low_bound; i <= high_bound; i++)
366 int element = value_bit_index (type, valaddr, i);
370 goto maybe_bad_bstring;
373 fprintf_filtered (stream, "%d", element);
377 fputs_filtered (", ", stream);
378 chill_print_type_scalar (range, (LONGEST) i, stream);
381 /* Look for a continuous range of true elements. */
382 if (i + 1 <= high_bound && value_bit_index (type, valaddr, ++i))
384 int j = i; /* j is the upper bound so far of the range */
385 fputs_filtered (":", stream);
386 while (i + 1 <= high_bound
387 && value_bit_index (type, valaddr, ++i))
389 chill_print_type_scalar (range, (LONGEST) j, stream);
395 fputs_filtered ("'", stream);
397 fputs_filtered ("]", stream);
401 case TYPE_CODE_STRUCT:
402 if (chill_varying_type (type))
404 struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
405 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
406 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
408 switch (TYPE_CODE (inner))
410 case TYPE_CODE_STRING:
411 if (length > TYPE_LENGTH (type) - 2)
413 fprintf_filtered (stream,
414 "<dynamic length %ld > static length %d> *invalid*",
415 length, TYPE_LENGTH (type));
417 /* Don't print the string; doing so might produce a
421 LA_PRINT_STRING (stream, data_addr, length, 1, 0);
427 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
434 fprintf_filtered (stream, "LOC(");
435 print_address_numeric
436 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
439 fprintf_filtered (stream, ")");
441 fputs_filtered (": ", stream);
443 /* De-reference the reference. */
446 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
448 struct value *deref_val =
450 (TYPE_TARGET_TYPE (type),
451 unpack_pointer (lookup_pointer_type (builtin_type_void),
454 val_print (VALUE_TYPE (deref_val),
455 VALUE_CONTENTS (deref_val),
457 VALUE_ADDRESS (deref_val), stream, format,
458 deref_ref, recurse + 1, pretty);
461 fputs_filtered ("???", stream);
466 c_val_print (type, valaddr, 0, address, stream, format,
467 deref_ref, recurse, pretty);
470 case TYPE_CODE_RANGE:
471 if (TYPE_TARGET_TYPE (type))
472 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, 0, address, stream,
473 format, deref_ref, recurse, pretty);
476 case TYPE_CODE_MEMBER:
477 case TYPE_CODE_UNION:
480 case TYPE_CODE_ERROR:
482 /* Let's defer printing to the C printer, rather than
483 print an error message. FIXME! */
484 c_val_print (type, valaddr, 0, address, stream, format,
485 deref_ref, recurse, pretty);
491 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
492 print out a structure's fields: cp_print_value_fields and cplus_print_value.
494 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
495 same meanings as in cplus_print_value and c_val_print.
497 DONT_PRINT is an array of baseclass types that we
498 should not print, or zero if called from top level. */
501 chill_print_value_fields (struct type *type, char *valaddr,
502 struct ui_file *stream, int format, int recurse,
503 enum val_prettyprint pretty, struct type **dont_print)
508 CHECK_TYPEDEF (type);
510 fprintf_filtered (stream, "[");
511 len = TYPE_NFIELDS (type);
514 fprintf_filtered (stream, "<No data fields>");
518 for (i = 0; i < len; i++)
522 fprintf_filtered (stream, ", ");
527 fprintf_filtered (stream, "\n");
528 print_spaces_filtered (2 + 2 * recurse, stream);
532 wrap_here (n_spaces (2 + 2 * recurse));
534 fputs_filtered (".", stream);
535 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
536 language_chill, DMGL_NO_OPTS);
537 fputs_filtered (": ", stream);
538 if (TYPE_FIELD_PACKED (type, i))
542 /* Bitfields require special handling, especially due to byte
544 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
545 unpack_field_as_long (type, valaddr, i));
547 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
548 stream, format, 0, recurse + 1, pretty);
552 chill_val_print (TYPE_FIELD_TYPE (type, i),
553 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
554 0, stream, format, 0, recurse + 1, pretty);
559 fprintf_filtered (stream, "\n");
560 print_spaces_filtered (2 * recurse, stream);
563 fprintf_filtered (stream, "]");
567 chill_value_print (struct value *val, struct ui_file *stream, int format,
568 enum val_prettyprint pretty)
570 struct type *type = VALUE_TYPE (val);
571 struct type *real_type = check_typedef (type);
573 /* If it is a pointer, indicate what it points to.
575 Print type also if it is a reference. */
577 if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
578 TYPE_CODE (real_type) == TYPE_CODE_REF)
580 char *valaddr = VALUE_CONTENTS (val);
581 CORE_ADDR addr = unpack_pointer (type, valaddr);
582 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
585 char *name = TYPE_NAME (type);
587 fputs_filtered (name, stream);
588 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
589 fputs_filtered ("PTR", stream);
592 fprintf_filtered (stream, "(");
593 type_print (type, "", stream, -1);
594 fprintf_filtered (stream, ")");
596 fprintf_filtered (stream, "(");
597 i = val_print (type, valaddr, 0, VALUE_ADDRESS (val),
598 stream, format, 1, 0, pretty);
599 fprintf_filtered (stream, ")");
603 return (val_print (type, VALUE_CONTENTS (val), 0,
604 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));