* printcmd.c (print_address_demangle): Remove special case for 0.
[deliverable/binutils-gdb.git] / gdb / m2-valprint.c
CommitLineData
c906108c 1/* Support for printing Modula 2 values for GDB, the GNU debugger.
a8d6eb4a 2
0b302171
JB
3 Copyright (C) 1986, 1988-1989, 1991-1992, 1996, 1998, 2000, 2005-2012
4 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 40 struct ui_file *stream, int recurse,
0e03807e 41 const struct value *val,
79a45b7d
TT
42 const struct value_print_options *options,
43 int len);
72019c9c
GM
44
45
844781a1
GM
46/* get_long_set_bounds - assigns the bounds of the long set to low and
47 high. */
72019c9c
GM
48
49int
50get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
51{
52 int len, i;
53
54 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
55 {
56 len = TYPE_NFIELDS (type);
57 i = TYPE_N_BASECLASSES (type);
58 if (len == 0)
59 return 0;
60 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
61 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
62 len-1)));
63 return 1;
64 }
65 error (_("expecting long_set"));
66 return 0;
67}
68
69static void
70m2_print_long_set (struct type *type, const gdb_byte *valaddr,
71 int embedded_offset, CORE_ADDR address,
79a45b7d 72 struct ui_file *stream)
72019c9c
GM
73{
74 int empty_set = 1;
75 int element_seen = 0;
76 LONGEST previous_low = 0;
77 LONGEST previous_high= 0;
78 LONGEST i, low_bound, high_bound;
79 LONGEST field_low, field_high;
80 struct type *range;
81 int len, field;
82 struct type *target;
83 int bitval;
84
85 CHECK_TYPEDEF (type);
86
87 fprintf_filtered (stream, "{");
88 len = TYPE_NFIELDS (type);
89 if (get_long_set_bounds (type, &low_bound, &high_bound))
90 {
91 field = TYPE_N_BASECLASSES (type);
92 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
93 }
94 else
95 {
96 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
97 return;
98 }
99
100 target = TYPE_TARGET_TYPE (range);
72019c9c
GM
101
102 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
103 {
104 for (i = low_bound; i <= high_bound; i++)
105 {
106 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
107 (TYPE_FIELD_BITPOS (type, field) / 8) +
108 valaddr + embedded_offset, i);
109 if (bitval < 0)
110 error (_("bit test is out of range"));
111 else if (bitval > 0)
112 {
113 previous_high = i;
114 if (! element_seen)
115 {
116 if (! empty_set)
117 fprintf_filtered (stream, ", ");
118 print_type_scalar (target, i, stream);
119 empty_set = 0;
120 element_seen = 1;
121 previous_low = i;
122 }
123 }
124 else
125 {
126 /* bit is not set */
127 if (element_seen)
128 {
129 if (previous_low+1 < previous_high)
130 fprintf_filtered (stream, "..");
131 if (previous_low+1 < previous_high)
132 print_type_scalar (target, previous_high, stream);
133 element_seen = 0;
134 }
135 }
136 if (i == field_high)
137 {
138 field++;
139 if (field == len)
140 break;
141 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
142 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
143 break;
144 target = TYPE_TARGET_TYPE (range);
72019c9c
GM
145 }
146 }
147 if (element_seen)
148 {
149 if (previous_low+1 < previous_high)
150 {
151 fprintf_filtered (stream, "..");
152 print_type_scalar (target, previous_high, stream);
153 }
154 element_seen = 0;
155 }
156 fprintf_filtered (stream, "}");
157 }
158}
159
844781a1
GM
160static void
161m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
162 int embedded_offset, CORE_ADDR address,
79a45b7d
TT
163 struct ui_file *stream, int recurse,
164 const struct value_print_options *options)
844781a1
GM
165{
166 struct type *content_type;
167 CORE_ADDR addr;
168 LONGEST len;
169 struct value *val;
170
171 CHECK_TYPEDEF (type);
172 content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
173
174 addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
175 (TYPE_FIELD_BITPOS (type, 0) / 8) +
176 valaddr + embedded_offset);
177
178 val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
179 addr);
180 len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
181
182 fprintf_filtered (stream, "{");
66d61a4c
PA
183 m2_print_array_contents (value_type (val),
184 value_contents_for_printing (val),
844781a1 185 value_embedded_offset (val), addr, stream,
66d61a4c 186 recurse, val, options, len);
844781a1
GM
187 fprintf_filtered (stream, ", HIGH = %d}", (int) len);
188}
189
79a45b7d 190static int
72019c9c
GM
191print_unpacked_pointer (struct type *type,
192 CORE_ADDR address, CORE_ADDR addr,
79a45b7d
TT
193 const struct value_print_options *options,
194 struct ui_file *stream)
72019c9c 195{
50810684 196 struct gdbarch *gdbarch = get_type_arch (type);
72019c9c
GM
197 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
198
199 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
200 {
201 /* Try to print what function it points to. */
edf0c1b7 202 print_function_pointer_address (options, gdbarch, addr, stream);
72019c9c
GM
203 /* Return value is irrelevant except for string pointers. */
204 return 0;
205 }
206
79a45b7d 207 if (options->addressprint && options->format != 's')
5af949e3 208 fputs_filtered (paddress (gdbarch, address), stream);
72019c9c
GM
209
210 /* For a pointer to char or unsigned char, also print the string
211 pointed to, unless pointer is null. */
212
213 if (TYPE_LENGTH (elttype) == 1
214 && TYPE_CODE (elttype) == TYPE_CODE_INT
79a45b7d 215 && (options->format == 0 || options->format == 's')
72019c9c 216 && addr != 0)
09ca9e2e 217 return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
6c7a06a3 218 stream, options);
72019c9c
GM
219
220 return 0;
221}
222
223static void
844781a1
GM
224print_variable_at_address (struct type *type,
225 const gdb_byte *valaddr,
79a45b7d
TT
226 struct ui_file *stream,
227 int recurse,
228 const struct value_print_options *options)
72019c9c 229{
5af949e3 230 struct gdbarch *gdbarch = get_type_arch (type);
72019c9c
GM
231 CORE_ADDR addr = unpack_pointer (type, valaddr);
232 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
233
234 fprintf_filtered (stream, "[");
5af949e3 235 fputs_filtered (paddress (gdbarch, addr), stream);
72019c9c
GM
236 fprintf_filtered (stream, "] : ");
237
238 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
239 {
240 struct value *deref_val =
d8631d21 241 value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
b8d56208 242
79a45b7d 243 common_val_print (deref_val, stream, recurse, options, current_language);
72019c9c
GM
244 }
245 else
246 fputs_filtered ("???", stream);
247}
248
844781a1
GM
249
250/* m2_print_array_contents - prints out the contents of an
251 array up to a max_print values.
252 It prints arrays of char as a string
253 and all other data types as comma
254 separated values. */
255
256static void
257m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
258 int embedded_offset, CORE_ADDR address,
79a45b7d 259 struct ui_file *stream, int recurse,
0e03807e 260 const struct value *val,
79a45b7d
TT
261 const struct value_print_options *options,
262 int len)
844781a1
GM
263{
264 int eltlen;
265 CHECK_TYPEDEF (type);
266
267 if (TYPE_LENGTH (type) > 0)
268 {
269 eltlen = TYPE_LENGTH (type);
79a45b7d 270 if (options->prettyprint_arrays)
844781a1
GM
271 print_spaces_filtered (2 + 2 * recurse, stream);
272 /* For an array of chars, print with string syntax. */
273 if (eltlen == 1 &&
274 ((TYPE_CODE (type) == TYPE_CODE_INT)
275 || ((current_language->la_language == language_m2)
276 && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
79a45b7d 277 && (options->format == 0 || options->format == 's'))
09ca9e2e 278 val_print_string (type, NULL, address, len+1, stream, options);
844781a1
GM
279 else
280 {
281 fprintf_filtered (stream, "{");
490f124f 282 val_print_array_elements (type, valaddr, embedded_offset,
0e03807e
TT
283 address, stream, recurse, val,
284 options, 0);
844781a1
GM
285 fprintf_filtered (stream, "}");
286 }
287 }
288}
289
e88acd96
TT
290/* Decorations for Modula 2. */
291
292static const struct generic_val_print_decorations m2_decorations =
293{
294 "",
295 " + ",
296 " * I",
297 "TRUE",
298 "FALSE",
299 "void"
300};
844781a1 301
32b72a42 302/* See val_print for a description of the various parameters of this
d3eab38a 303 function; they are identical. */
c906108c 304
d3eab38a 305void
fc1a4b47 306m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
79a45b7d 307 CORE_ADDR address, struct ui_file *stream, int recurse,
0e03807e 308 const struct value *original_value,
79a45b7d 309 const struct value_print_options *options)
c906108c 310{
5af949e3 311 struct gdbarch *gdbarch = get_type_arch (type);
025bb325 312 unsigned int i = 0; /* Number of characters printed. */
72019c9c
GM
313 unsigned len;
314 struct type *elttype;
315 unsigned eltlen;
72019c9c
GM
316 LONGEST val;
317 CORE_ADDR addr;
318
319 CHECK_TYPEDEF (type);
320 switch (TYPE_CODE (type))
321 {
322 case TYPE_CODE_ARRAY:
323 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
324 {
325 elttype = check_typedef (TYPE_TARGET_TYPE (type));
326 eltlen = TYPE_LENGTH (elttype);
327 len = TYPE_LENGTH (type) / eltlen;
79a45b7d 328 if (options->prettyprint_arrays)
72019c9c
GM
329 print_spaces_filtered (2 + 2 * recurse, stream);
330 /* For an array of chars, print with string syntax. */
331 if (eltlen == 1 &&
332 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
333 || ((current_language->la_language == language_m2)
334 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
79a45b7d 335 && (options->format == 0 || options->format == 's'))
72019c9c
GM
336 {
337 /* If requested, look for the first null char and only print
338 elements up to it. */
79a45b7d 339 if (options->stop_print_at_null)
72019c9c
GM
340 {
341 unsigned int temp_len;
342
025bb325 343 /* Look for a NULL char. */
72019c9c
GM
344 for (temp_len = 0;
345 (valaddr + embedded_offset)[temp_len]
79a45b7d 346 && temp_len < len && temp_len < options->print_max;
72019c9c
GM
347 temp_len++);
348 len = temp_len;
349 }
350
6c7a06a3 351 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
be759fcf
PM
352 valaddr + embedded_offset, len, NULL,
353 0, options);
72019c9c
GM
354 i = len;
355 }
356 else
357 {
358 fprintf_filtered (stream, "{");
490f124f 359 val_print_array_elements (type, valaddr, embedded_offset,
025bb325
MS
360 address, stream,
361 recurse, original_value,
0e03807e 362 options, 0);
72019c9c
GM
363 fprintf_filtered (stream, "}");
364 }
365 break;
366 }
367 /* Array of unspecified length: treat like pointer to first elt. */
79a45b7d 368 print_unpacked_pointer (type, address, address, options, stream);
72019c9c
GM
369 break;
370
371 case TYPE_CODE_PTR:
372 if (TYPE_CONST (type))
373 print_variable_at_address (type, valaddr + embedded_offset,
79a45b7d
TT
374 stream, recurse, options);
375 else if (options->format && options->format != 's')
ab2188aa
PA
376 val_print_scalar_formatted (type, valaddr, embedded_offset,
377 original_value, options, 0, stream);
72019c9c
GM
378 else
379 {
380 addr = unpack_pointer (type, valaddr + embedded_offset);
79a45b7d 381 print_unpacked_pointer (type, addr, address, options, stream);
72019c9c
GM
382 }
383 break;
384
72019c9c 385 case TYPE_CODE_UNION:
79a45b7d 386 if (recurse && !options->unionprint)
72019c9c
GM
387 {
388 fprintf_filtered (stream, "{...}");
389 break;
390 }
391 /* Fall through. */
392 case TYPE_CODE_STRUCT:
393 if (m2_is_long_set (type))
394 m2_print_long_set (type, valaddr, embedded_offset, address,
79a45b7d 395 stream);
844781a1
GM
396 else if (m2_is_unbounded_array (type))
397 m2_print_unbounded_array (type, valaddr, embedded_offset,
79a45b7d 398 address, stream, recurse, options);
72019c9c
GM
399 else
400 cp_print_value_fields (type, type, valaddr, embedded_offset,
0e03807e
TT
401 address, stream, recurse, original_value,
402 options, NULL, 0);
72019c9c
GM
403 break;
404
72019c9c
GM
405 case TYPE_CODE_BITSTRING:
406 case TYPE_CODE_SET:
407 elttype = TYPE_INDEX_TYPE (type);
408 CHECK_TYPEDEF (elttype);
409 if (TYPE_STUB (elttype))
410 {
411 fprintf_filtered (stream, _("<incomplete type>"));
412 gdb_flush (stream);
413 break;
414 }
415 else
416 {
417 struct type *range = elttype;
418 LONGEST low_bound, high_bound;
419 int i;
420 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
421 int need_comma = 0;
422
423 if (is_bitstring)
424 fputs_filtered ("B'", stream);
425 else
426 fputs_filtered ("{", stream);
427
428 i = get_discrete_bounds (range, &low_bound, &high_bound);
429 maybe_bad_bstring:
430 if (i < 0)
431 {
432 fputs_filtered (_("<error value>"), stream);
433 goto done;
434 }
435
436 for (i = low_bound; i <= high_bound; i++)
437 {
438 int element = value_bit_index (type, valaddr + embedded_offset,
439 i);
b8d56208 440
72019c9c
GM
441 if (element < 0)
442 {
443 i = element;
444 goto maybe_bad_bstring;
445 }
446 if (is_bitstring)
447 fprintf_filtered (stream, "%d", element);
448 else if (element)
449 {
450 if (need_comma)
451 fputs_filtered (", ", stream);
452 print_type_scalar (range, i, stream);
453 need_comma = 1;
454
455 if (i + 1 <= high_bound
456 && value_bit_index (type, valaddr + embedded_offset,
457 ++i))
458 {
459 int j = i;
b8d56208 460
72019c9c
GM
461 fputs_filtered ("..", stream);
462 while (i + 1 <= high_bound
463 && value_bit_index (type,
464 valaddr + embedded_offset,
465 ++i))
466 j = i;
467 print_type_scalar (range, j, stream);
468 }
469 }
470 }
471 done:
472 if (is_bitstring)
473 fputs_filtered ("'", stream);
474 else
475 fputs_filtered ("}", stream);
476 }
477 break;
478
e88acd96
TT
479 case TYPE_CODE_RANGE:
480 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
481 {
482 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
483 address, stream, recurse, original_value, options);
484 break;
485 }
486 /* FIXME: create_range_type does not set the unsigned bit in a
487 range type (I think it probably should copy it from the target
488 type), so we won't print values which are too large to
489 fit in a signed integer correctly. */
490 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
491 print with the target type, though, because the size of our type
492 and the target type might differ). */
493 /* FALLTHROUGH */
72019c9c 494
e88acd96
TT
495 case TYPE_CODE_REF:
496 case TYPE_CODE_ENUM:
497 case TYPE_CODE_FUNC:
498 case TYPE_CODE_INT:
499 case TYPE_CODE_FLT:
500 case TYPE_CODE_METHOD:
501 case TYPE_CODE_VOID:
72019c9c 502 case TYPE_CODE_ERROR:
72019c9c 503 case TYPE_CODE_UNDEF:
e88acd96
TT
504 case TYPE_CODE_BOOL:
505 case TYPE_CODE_CHAR:
72019c9c 506 default:
e88acd96
TT
507 generic_val_print (type, valaddr, embedded_offset, address,
508 stream, recurse, original_value, options,
509 &m2_decorations);
510 break;
72019c9c
GM
511 }
512 gdb_flush (stream);
c906108c 513}
This page took 0.985675 seconds and 4 git commands to generate.