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