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