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