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.
5 This file is part of GDB.
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.
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.
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. */
26 #include "expression.h"
30 #include "c-lang.h" /* For c_val_print */
31 #include "typeprint.h"
36 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
37 enum val_prettyprint, struct type **));
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. */
47 chill_print_type_scalar (type, val, stream)
52 switch (TYPE_CODE (type))
55 if (TYPE_TARGET_TYPE (type))
57 chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
64 case TYPE_CODE_STRUCT:
72 case TYPE_CODE_STRING:
73 case TYPE_CODE_BITSTRING:
75 case TYPE_CODE_MEMBER:
76 case TYPE_CODE_METHOD:
80 case TYPE_CODE_COMPLEX:
81 case TYPE_CODE_TYPEDEF:
85 print_type_scalar (type, val, stream);
88 /* Print the elements of an array.
89 Similar to val_print_array_elements, but prints
90 element indexes (in Chill syntax). */
93 chill_val_print_array_elements (type, valaddr, address, stream,
94 format, deref_ref, recurse, pretty)
102 enum val_prettyprint pretty;
105 unsigned int things_printed = 0;
107 struct type *elttype;
108 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
109 struct type *index_type = TYPE_TARGET_TYPE (range_type);
111 /* Position of the array element we are examining to see
112 whether it is repeated. */
114 /* Number of repetitions we have detected so far. */
116 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
118 elttype = check_typedef (TYPE_TARGET_TYPE (type));
119 eltlen = TYPE_LENGTH (elttype);
120 len = TYPE_LENGTH (type) / eltlen;
122 annotate_array_section_begin (i, elttype);
124 for (; i < len && things_printed < print_max; i++)
128 if (prettyprint_arrays)
130 fprintf_filtered (stream, ",\n");
131 print_spaces_filtered (2 + 2 * recurse, stream);
135 fprintf_filtered (stream, ", ");
138 wrap_here (n_spaces (2 + 2 * recurse));
142 while ((rep1 < len) &&
143 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
149 fputs_filtered ("(", stream);
150 chill_print_type_scalar (index_type, low_bound + i, stream);
153 fputs_filtered (":", stream);
154 chill_print_type_scalar (index_type, low_bound + i + reps - 1,
156 fputs_filtered ("): ", stream);
157 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
158 deref_ref, recurse + 1, pretty);
165 fputs_filtered ("): ", stream);
166 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
167 deref_ref, recurse + 1, pretty);
172 annotate_array_section_end ();
175 fprintf_filtered (stream, "...");
179 /* Print data of type TYPE located at VALADDR (within GDB), which came from
180 the inferior at address ADDRESS, onto stdio stream STREAM according to
181 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
184 If the data are a string pointer, returns the number of string characters
187 If DEREF_REF is nonzero, then dereference references, otherwise just print
190 The PRETTY parameter controls prettyprinting. */
193 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
202 enum val_prettyprint pretty;
205 unsigned int i = 0; /* Number of characters printed. */
206 struct type *elttype;
209 CHECK_TYPEDEF (type);
211 switch (TYPE_CODE (type))
213 case TYPE_CODE_ARRAY:
214 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
216 if (prettyprint_arrays)
218 print_spaces_filtered (2 + 2 * recurse, stream);
220 fprintf_filtered (stream, "[");
221 chill_val_print_array_elements (type, valaddr, address, stream,
222 format, deref_ref, recurse, pretty);
223 fprintf_filtered (stream, "]");
227 error ("unimplemented in chill_val_print; unspecified array length");
232 format = format ? format : output_format;
235 print_scalar_formatted (valaddr, type, format, 0, stream);
239 val_print_type_code_int (type, valaddr, stream);
244 format = format ? format : output_format;
247 print_scalar_formatted (valaddr, type, format, 0, stream);
251 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
259 print_scalar_formatted (valaddr, type, format, 0, stream);
263 print_floating (valaddr, type, stream);
268 format = format ? format : output_format;
271 print_scalar_formatted (valaddr, type, format, 0, stream);
275 /* FIXME: Why is this using builtin_type_chill_bool not type? */
276 val = unpack_long (builtin_type_chill_bool, valaddr);
277 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
281 case TYPE_CODE_UNDEF:
282 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
283 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
284 and no complete type for struct foo in that file. */
285 fprintf_filtered (stream, "<incomplete type>");
289 if (format && format != 's')
291 print_scalar_formatted (valaddr, type, format, 0, stream);
294 addr = unpack_pointer (type, valaddr);
295 elttype = check_typedef (TYPE_TARGET_TYPE (type));
297 /* We assume a NULL pointer is all zeros ... */
300 fputs_filtered ("NULL", stream);
304 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
306 /* Try to print what function it points to. */
307 print_address_demangle (addr, stream, demangle);
308 /* Return value is irrelevant except for string pointers. */
311 if (addressprint && format != 's')
313 print_address_numeric (addr, 1, stream);
316 /* For a pointer to char or unsigned char, also print the string
317 pointed to, unless pointer is null. */
318 if (TYPE_LENGTH (elttype) == 1
319 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
320 && (format == 0 || format == 's')
322 && /* If print_max is UINT_MAX, the alloca below will fail.
323 In that case don't try to print the string. */
324 print_max < UINT_MAX)
326 i = val_print_string (addr, 0, stream);
328 /* Return number of characters printed, plus one for the
329 terminating null if we have "reached the end". */
330 return (i + (print_max && i != print_max));
333 case TYPE_CODE_STRING:
334 i = TYPE_LENGTH (type);
335 LA_PRINT_STRING (stream, valaddr, i, 0);
336 /* Return number of characters printed, plus one for the terminating
337 null if we have "reached the end". */
338 return (i + (print_max && i != print_max));
341 case TYPE_CODE_BITSTRING:
343 elttype = TYPE_INDEX_TYPE (type);
344 CHECK_TYPEDEF (elttype);
345 if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
347 fprintf_filtered (stream, "<incomplete type>");
352 struct type *range = elttype;
353 LONGEST low_bound, high_bound;
355 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
359 fputs_filtered ("B'", stream);
361 fputs_filtered ("[", stream);
363 i = get_discrete_bounds (range, &low_bound, &high_bound);
367 fputs_filtered ("<error value>", stream);
371 for (i = low_bound; i <= high_bound; i++)
373 int element = value_bit_index (type, valaddr, i);
377 goto maybe_bad_bstring;
380 fprintf_filtered (stream, "%d", element);
384 fputs_filtered (", ", stream);
385 chill_print_type_scalar (range, i, stream);
388 /* Look for a continuous range of true elements. */
389 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
391 int j = i; /* j is the upper bound so far of the range */
392 fputs_filtered (":", stream);
393 while (i+1 <= high_bound
394 && value_bit_index (type, valaddr, ++i))
396 chill_print_type_scalar (range, j, stream);
402 fputs_filtered ("'", stream);
404 fputs_filtered ("]", stream);
408 case TYPE_CODE_STRUCT:
409 if (chill_varying_type (type))
411 struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
412 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
413 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
415 switch (TYPE_CODE (inner))
417 case TYPE_CODE_STRING:
418 if (length > TYPE_LENGTH (type))
420 fprintf_filtered (stream,
421 "<dynamic length %ld > static length %d>",
422 length, TYPE_LENGTH (type));
424 LA_PRINT_STRING (stream, data_addr, length, 0);
430 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
437 fprintf_filtered (stream, "LOC(");
438 print_address_numeric
439 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
442 fprintf_filtered (stream, ")");
444 fputs_filtered (": ", stream);
446 /* De-reference the reference. */
449 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
451 value_ptr deref_val =
453 (TYPE_TARGET_TYPE (type),
454 unpack_pointer (lookup_pointer_type (builtin_type_void),
456 val_print (VALUE_TYPE (deref_val),
457 VALUE_CONTENTS (deref_val),
458 VALUE_ADDRESS (deref_val), stream, format,
459 deref_ref, recurse + 1, pretty);
462 fputs_filtered ("???", stream);
467 c_val_print (type, valaddr, address, stream, format,
468 deref_ref, recurse, pretty);
471 case TYPE_CODE_RANGE:
472 if (TYPE_TARGET_TYPE (type))
473 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
474 format, deref_ref, recurse, pretty);
477 case TYPE_CODE_MEMBER:
478 case TYPE_CODE_UNION:
481 case TYPE_CODE_ERROR:
483 /* Let's defer printing to the C printer, rather than
484 print an error message. FIXME! */
485 c_val_print (type, valaddr, address, stream, format,
486 deref_ref, recurse, pretty);
492 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
493 print out a structure's fields: cp_print_value_fields and cplus_print_value.
495 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
496 same meanings as in cplus_print_value and c_val_print.
498 DONT_PRINT is an array of baseclass types that we
499 should not print, or zero if called from top level. */
502 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
509 enum val_prettyprint pretty;
510 struct type **dont_print;
515 CHECK_TYPEDEF (type);
517 fprintf_filtered (stream, "[");
518 len = TYPE_NFIELDS (type);
521 fprintf_filtered (stream, "<No data fields>");
525 for (i = 0; i < len; i++)
529 fprintf_filtered (stream, ", ");
534 fprintf_filtered (stream, "\n");
535 print_spaces_filtered (2 + 2 * recurse, stream);
539 wrap_here (n_spaces (2 + 2 * recurse));
541 fputs_filtered (".", stream);
542 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
543 language_chill, DMGL_NO_OPTS);
544 fputs_filtered (": ", stream);
545 if (TYPE_FIELD_PACKED (type, i))
549 /* Bitfields require special handling, especially due to byte
551 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
552 unpack_field_as_long (type, valaddr, i));
554 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
555 stream, format, 0, recurse + 1, pretty);
559 chill_val_print (TYPE_FIELD_TYPE (type, i),
560 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
561 0, stream, format, 0, recurse + 1, pretty);
566 fprintf_filtered (stream, "\n");
567 print_spaces_filtered (2 * recurse, stream);
570 fprintf_filtered (stream, "]");
574 chill_value_print (val, stream, format, pretty)
578 enum val_prettyprint pretty;
580 struct type *type = VALUE_TYPE (val);
581 struct type *real_type = check_typedef (type);
583 /* If it is a pointer, indicate what it points to.
585 Print type also if it is a reference. */
587 if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
588 TYPE_CODE (real_type) == TYPE_CODE_REF)
590 char *valaddr = VALUE_CONTENTS (val);
591 CORE_ADDR addr = unpack_pointer (type, valaddr);
592 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
595 char *name = TYPE_NAME (type);
597 fputs_filtered (name, stream);
598 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
599 fputs_filtered ("PTR", stream);
602 fprintf_filtered (stream, "(");
603 type_print (type, "", stream, -1);
604 fprintf_filtered (stream, ")");
606 fprintf_filtered (stream, "(");
607 i = val_print (type, valaddr, VALUE_ADDRESS (val),
608 stream, format, 1, 0, pretty);
609 fprintf_filtered (stream, ")");
613 return (val_print (type, VALUE_CONTENTS (val),
614 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));