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