* ada-valprint.c (ada_val_print_1): When implicitly dereferencing
[deliverable/binutils-gdb.git] / gdb / m2-valprint.c
1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1996, 1998, 2000, 2005, 2006,
4 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "valprint.h"
27 #include "language.h"
28 #include "typeprint.h"
29 #include "c-lang.h"
30 #include "m2-lang.h"
31 #include "target.h"
32
33 int print_unpacked_pointer (struct type *type,
34 CORE_ADDR address, CORE_ADDR addr,
35 int format, struct ui_file *stream);
36 static void
37 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
38 int embedded_offset, CORE_ADDR address,
39 struct ui_file *stream, int format,
40 enum val_prettyprint pretty,
41 int deref_ref, int recurse, int len);
42
43
44 /* Print function pointer with inferior address ADDRESS onto stdio
45 stream STREAM. */
46
47 static void
48 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
49 {
50 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
51 address,
52 &current_target);
53
54 /* If the function pointer is represented by a description, print the
55 address of the description. */
56 if (addressprint && func_addr != address)
57 {
58 fputs_filtered ("@", stream);
59 fputs_filtered (paddress (address), stream);
60 fputs_filtered (": ", stream);
61 }
62 print_address_demangle (func_addr, stream, demangle);
63 }
64
65 /* get_long_set_bounds - assigns the bounds of the long set to low and
66 high. */
67
68 int
69 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
70 {
71 int len, i;
72
73 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
74 {
75 len = TYPE_NFIELDS (type);
76 i = TYPE_N_BASECLASSES (type);
77 if (len == 0)
78 return 0;
79 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
80 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
81 len-1)));
82 return 1;
83 }
84 error (_("expecting long_set"));
85 return 0;
86 }
87
88 static void
89 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
90 int embedded_offset, CORE_ADDR address,
91 struct ui_file *stream, int format,
92 enum val_prettyprint pretty)
93 {
94 int empty_set = 1;
95 int element_seen = 0;
96 LONGEST previous_low = 0;
97 LONGEST previous_high= 0;
98 LONGEST i, low_bound, high_bound;
99 LONGEST field_low, field_high;
100 struct type *range;
101 int len, field;
102 struct type *target;
103 int bitval;
104
105 CHECK_TYPEDEF (type);
106
107 fprintf_filtered (stream, "{");
108 len = TYPE_NFIELDS (type);
109 if (get_long_set_bounds (type, &low_bound, &high_bound))
110 {
111 field = TYPE_N_BASECLASSES (type);
112 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
113 }
114 else
115 {
116 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
117 return;
118 }
119
120 target = TYPE_TARGET_TYPE (range);
121 if (target == NULL)
122 target = builtin_type_int;
123
124 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
125 {
126 for (i = low_bound; i <= high_bound; i++)
127 {
128 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
129 (TYPE_FIELD_BITPOS (type, field) / 8) +
130 valaddr + embedded_offset, i);
131 if (bitval < 0)
132 error (_("bit test is out of range"));
133 else if (bitval > 0)
134 {
135 previous_high = i;
136 if (! element_seen)
137 {
138 if (! empty_set)
139 fprintf_filtered (stream, ", ");
140 print_type_scalar (target, i, stream);
141 empty_set = 0;
142 element_seen = 1;
143 previous_low = i;
144 }
145 }
146 else
147 {
148 /* bit is not set */
149 if (element_seen)
150 {
151 if (previous_low+1 < previous_high)
152 fprintf_filtered (stream, "..");
153 if (previous_low+1 < previous_high)
154 print_type_scalar (target, previous_high, stream);
155 element_seen = 0;
156 }
157 }
158 if (i == field_high)
159 {
160 field++;
161 if (field == len)
162 break;
163 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
164 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
165 break;
166 target = TYPE_TARGET_TYPE (range);
167 if (target == NULL)
168 target = builtin_type_int;
169 }
170 }
171 if (element_seen)
172 {
173 if (previous_low+1 < previous_high)
174 {
175 fprintf_filtered (stream, "..");
176 print_type_scalar (target, previous_high, stream);
177 }
178 element_seen = 0;
179 }
180 fprintf_filtered (stream, "}");
181 }
182 }
183
184 static void
185 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
186 int embedded_offset, CORE_ADDR address,
187 struct ui_file *stream, int format,
188 int deref_ref, enum val_prettyprint pretty,
189 int recurse)
190 {
191 struct type *content_type;
192 CORE_ADDR addr;
193 LONGEST len;
194 struct value *val;
195
196 CHECK_TYPEDEF (type);
197 content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
198
199 addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
200 (TYPE_FIELD_BITPOS (type, 0) / 8) +
201 valaddr + embedded_offset);
202
203 val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
204 addr);
205 len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
206
207 fprintf_filtered (stream, "{");
208 m2_print_array_contents (value_type (val), value_contents(val),
209 value_embedded_offset (val), addr, stream,
210 format, deref_ref, pretty, recurse, len);
211 fprintf_filtered (stream, ", HIGH = %d}", (int) len);
212 }
213
214 int
215 print_unpacked_pointer (struct type *type,
216 CORE_ADDR address, CORE_ADDR addr,
217 int format, struct ui_file *stream)
218 {
219 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
220
221 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
222 {
223 /* Try to print what function it points to. */
224 print_function_pointer_address (addr, stream);
225 /* Return value is irrelevant except for string pointers. */
226 return 0;
227 }
228
229 if (addressprint && format != 's')
230 fputs_filtered (paddress (address), stream);
231
232 /* For a pointer to char or unsigned char, also print the string
233 pointed to, unless pointer is null. */
234
235 if (TYPE_LENGTH (elttype) == 1
236 && TYPE_CODE (elttype) == TYPE_CODE_INT
237 && (format == 0 || format == 's')
238 && addr != 0)
239 return val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
240
241 return 0;
242 }
243
244 static void
245 print_variable_at_address (struct type *type,
246 const gdb_byte *valaddr,
247 struct ui_file *stream, int format,
248 int deref_ref, int recurse,
249 enum val_prettyprint pretty)
250 {
251 CORE_ADDR addr = unpack_pointer (type, valaddr);
252 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
253
254 fprintf_filtered (stream, "[");
255 fputs_filtered (paddress (addr), stream);
256 fprintf_filtered (stream, "] : ");
257
258 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
259 {
260 struct value *deref_val =
261 value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
262 common_val_print (deref_val, stream, format, deref_ref,
263 recurse, pretty, current_language);
264 }
265 else
266 fputs_filtered ("???", stream);
267 }
268
269
270 /* m2_print_array_contents - prints out the contents of an
271 array up to a max_print values.
272 It prints arrays of char as a string
273 and all other data types as comma
274 separated values. */
275
276 static void
277 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
278 int embedded_offset, CORE_ADDR address,
279 struct ui_file *stream, int format,
280 enum val_prettyprint pretty,
281 int deref_ref, int recurse, int len)
282 {
283 int eltlen;
284 CHECK_TYPEDEF (type);
285
286 if (TYPE_LENGTH (type) > 0)
287 {
288 eltlen = TYPE_LENGTH (type);
289 if (prettyprint_arrays)
290 print_spaces_filtered (2 + 2 * recurse, stream);
291 /* For an array of chars, print with string syntax. */
292 if (eltlen == 1 &&
293 ((TYPE_CODE (type) == TYPE_CODE_INT)
294 || ((current_language->la_language == language_m2)
295 && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
296 && (format == 0 || format == 's'))
297 val_print_string (address, len+1, eltlen, stream);
298 else
299 {
300 fprintf_filtered (stream, "{");
301 val_print_array_elements (type, valaddr + embedded_offset,
302 address, stream, format,
303 deref_ref, recurse, pretty, 0);
304 fprintf_filtered (stream, "}");
305 }
306 }
307 }
308
309
310 /* Print data of type TYPE located at VALADDR (within GDB), which came from
311 the inferior at address ADDRESS, onto stdio stream STREAM according to
312 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
313 target byte order.
314
315 If the data are a string pointer, returns the number of string characters
316 printed.
317
318 If DEREF_REF is nonzero, then dereference references, otherwise just print
319 them like pointers.
320
321 The PRETTY parameter controls prettyprinting. */
322
323 int
324 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
325 CORE_ADDR address, struct ui_file *stream, int format,
326 int deref_ref, int recurse, enum val_prettyprint pretty)
327 {
328 unsigned int i = 0; /* Number of characters printed */
329 unsigned len;
330 struct type *elttype;
331 unsigned eltlen;
332 int length_pos, length_size, string_pos;
333 int char_size;
334 LONGEST val;
335 CORE_ADDR addr;
336
337 CHECK_TYPEDEF (type);
338 switch (TYPE_CODE (type))
339 {
340 case TYPE_CODE_ARRAY:
341 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
342 {
343 elttype = check_typedef (TYPE_TARGET_TYPE (type));
344 eltlen = TYPE_LENGTH (elttype);
345 len = TYPE_LENGTH (type) / eltlen;
346 if (prettyprint_arrays)
347 print_spaces_filtered (2 + 2 * recurse, stream);
348 /* For an array of chars, print with string syntax. */
349 if (eltlen == 1 &&
350 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
351 || ((current_language->la_language == language_m2)
352 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
353 && (format == 0 || format == 's'))
354 {
355 /* If requested, look for the first null char and only print
356 elements up to it. */
357 if (stop_print_at_null)
358 {
359 unsigned int temp_len;
360
361 /* Look for a NULL char. */
362 for (temp_len = 0;
363 (valaddr + embedded_offset)[temp_len]
364 && temp_len < len && temp_len < print_max;
365 temp_len++);
366 len = temp_len;
367 }
368
369 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, 1, 0);
370 i = len;
371 }
372 else
373 {
374 fprintf_filtered (stream, "{");
375 val_print_array_elements (type, valaddr + embedded_offset,
376 address, stream, format, deref_ref,
377 recurse, pretty, 0);
378 fprintf_filtered (stream, "}");
379 }
380 break;
381 }
382 /* Array of unspecified length: treat like pointer to first elt. */
383 print_unpacked_pointer (type, address, address, format, stream);
384 break;
385
386 case TYPE_CODE_PTR:
387 if (TYPE_CONST (type))
388 print_variable_at_address (type, valaddr + embedded_offset,
389 stream, format, deref_ref, recurse,
390 pretty);
391 else if (format && format != 's')
392 print_scalar_formatted (valaddr + embedded_offset, type, format,
393 0, stream);
394 else
395 {
396 addr = unpack_pointer (type, valaddr + embedded_offset);
397 print_unpacked_pointer (type, addr, address, format, stream);
398 }
399 break;
400
401 case TYPE_CODE_REF:
402 elttype = check_typedef (TYPE_TARGET_TYPE (type));
403 if (addressprint)
404 {
405 CORE_ADDR addr
406 = extract_typed_address (valaddr + embedded_offset, type);
407 fprintf_filtered (stream, "@");
408 fputs_filtered (paddress (addr), stream);
409 if (deref_ref)
410 fputs_filtered (": ", stream);
411 }
412 /* De-reference the reference. */
413 if (deref_ref)
414 {
415 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
416 {
417 struct value *deref_val =
418 value_at
419 (TYPE_TARGET_TYPE (type),
420 unpack_pointer (type, valaddr + embedded_offset));
421 common_val_print (deref_val, stream, format, deref_ref,
422 recurse, pretty, current_language);
423 }
424 else
425 fputs_filtered ("???", stream);
426 }
427 break;
428
429 case TYPE_CODE_UNION:
430 if (recurse && !unionprint)
431 {
432 fprintf_filtered (stream, "{...}");
433 break;
434 }
435 /* Fall through. */
436 case TYPE_CODE_STRUCT:
437 if (m2_is_long_set (type))
438 m2_print_long_set (type, valaddr, embedded_offset, address,
439 stream, format, pretty);
440 else if (m2_is_unbounded_array (type))
441 m2_print_unbounded_array (type, valaddr, embedded_offset,
442 address, stream, format, deref_ref,
443 pretty, recurse);
444 else
445 cp_print_value_fields (type, type, valaddr, embedded_offset,
446 address, stream, format,
447 recurse, pretty, NULL, 0);
448 break;
449
450 case TYPE_CODE_ENUM:
451 if (format)
452 {
453 print_scalar_formatted (valaddr + embedded_offset, type,
454 format, 0, stream);
455 break;
456 }
457 len = TYPE_NFIELDS (type);
458 val = unpack_long (type, valaddr + embedded_offset);
459 for (i = 0; i < len; i++)
460 {
461 QUIT;
462 if (val == TYPE_FIELD_BITPOS (type, i))
463 {
464 break;
465 }
466 }
467 if (i < len)
468 {
469 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
470 }
471 else
472 {
473 print_longest (stream, 'd', 0, val);
474 }
475 break;
476
477 case TYPE_CODE_FUNC:
478 if (format)
479 {
480 print_scalar_formatted (valaddr + embedded_offset, type,
481 format, 0, stream);
482 break;
483 }
484 /* FIXME, we should consider, at least for ANSI C language, eliminating
485 the distinction made between FUNCs and POINTERs to FUNCs. */
486 fprintf_filtered (stream, "{");
487 type_print (type, "", stream, -1);
488 fprintf_filtered (stream, "} ");
489 /* Try to print what function it points to, and its address. */
490 print_address_demangle (address, stream, demangle);
491 break;
492
493 case TYPE_CODE_BOOL:
494 format = format ? format : output_format;
495 if (format)
496 print_scalar_formatted (valaddr + embedded_offset, type,
497 format, 0, stream);
498 else
499 {
500 val = unpack_long (type, valaddr + embedded_offset);
501 if (val == 0)
502 fputs_filtered ("FALSE", stream);
503 else if (val == 1)
504 fputs_filtered ("TRUE", stream);
505 else
506 fprintf_filtered (stream, "%ld)", (long int) val);
507 }
508 break;
509
510 case TYPE_CODE_RANGE:
511 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
512 {
513 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
514 address, stream, format, deref_ref, recurse, pretty);
515 break;
516 }
517 /* FIXME: create_range_type does not set the unsigned bit in a
518 range type (I think it probably should copy it from the target
519 type), so we won't print values which are too large to
520 fit in a signed integer correctly. */
521 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
522 print with the target type, though, because the size of our type
523 and the target type might differ). */
524 /* FALLTHROUGH */
525
526 case TYPE_CODE_INT:
527 format = format ? format : output_format;
528 if (format)
529 print_scalar_formatted (valaddr + embedded_offset, type, format,
530 0, stream);
531 else
532 val_print_type_code_int (type, valaddr + embedded_offset, stream);
533 break;
534
535 case TYPE_CODE_CHAR:
536 format = format ? format : output_format;
537 if (format)
538 print_scalar_formatted (valaddr + embedded_offset, type,
539 format, 0, stream);
540 else
541 {
542 val = unpack_long (type, valaddr + embedded_offset);
543 if (TYPE_UNSIGNED (type))
544 fprintf_filtered (stream, "%u", (unsigned int) val);
545 else
546 fprintf_filtered (stream, "%d", (int) val);
547 fputs_filtered (" ", stream);
548 LA_PRINT_CHAR ((unsigned char) val, stream);
549 }
550 break;
551
552 case TYPE_CODE_FLT:
553 if (format)
554 print_scalar_formatted (valaddr + embedded_offset, type,
555 format, 0, stream);
556 else
557 print_floating (valaddr + embedded_offset, type, stream);
558 break;
559
560 case TYPE_CODE_METHOD:
561 break;
562
563 case TYPE_CODE_BITSTRING:
564 case TYPE_CODE_SET:
565 elttype = TYPE_INDEX_TYPE (type);
566 CHECK_TYPEDEF (elttype);
567 if (TYPE_STUB (elttype))
568 {
569 fprintf_filtered (stream, _("<incomplete type>"));
570 gdb_flush (stream);
571 break;
572 }
573 else
574 {
575 struct type *range = elttype;
576 LONGEST low_bound, high_bound;
577 int i;
578 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
579 int need_comma = 0;
580
581 if (is_bitstring)
582 fputs_filtered ("B'", stream);
583 else
584 fputs_filtered ("{", stream);
585
586 i = get_discrete_bounds (range, &low_bound, &high_bound);
587 maybe_bad_bstring:
588 if (i < 0)
589 {
590 fputs_filtered (_("<error value>"), stream);
591 goto done;
592 }
593
594 for (i = low_bound; i <= high_bound; i++)
595 {
596 int element = value_bit_index (type, valaddr + embedded_offset,
597 i);
598 if (element < 0)
599 {
600 i = element;
601 goto maybe_bad_bstring;
602 }
603 if (is_bitstring)
604 fprintf_filtered (stream, "%d", element);
605 else if (element)
606 {
607 if (need_comma)
608 fputs_filtered (", ", stream);
609 print_type_scalar (range, i, stream);
610 need_comma = 1;
611
612 if (i + 1 <= high_bound
613 && value_bit_index (type, valaddr + embedded_offset,
614 ++i))
615 {
616 int j = i;
617 fputs_filtered ("..", stream);
618 while (i + 1 <= high_bound
619 && value_bit_index (type,
620 valaddr + embedded_offset,
621 ++i))
622 j = i;
623 print_type_scalar (range, j, stream);
624 }
625 }
626 }
627 done:
628 if (is_bitstring)
629 fputs_filtered ("'", stream);
630 else
631 fputs_filtered ("}", stream);
632 }
633 break;
634
635 case TYPE_CODE_VOID:
636 fprintf_filtered (stream, "void");
637 break;
638
639 case TYPE_CODE_ERROR:
640 fprintf_filtered (stream, _("<error type>"));
641 break;
642
643 case TYPE_CODE_UNDEF:
644 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
645 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
646 and no complete type for struct foo in that file. */
647 fprintf_filtered (stream, _("<incomplete type>"));
648 break;
649
650 default:
651 error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));
652 }
653 gdb_flush (stream);
654 return (0);
655 }
This page took 0.077606 seconds and 5 git commands to generate.