1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "expression.h"
27 #include "typeprint.h"
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
);
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
,
45 /* get_long_set_bounds - assigns the bounds of the long set to low and
49 get_long_set_bounds (struct type
*type
, LONGEST
*low
, LONGEST
*high
)
53 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
55 len
= TYPE_NFIELDS (type
);
56 i
= TYPE_N_BASECLASSES (type
);
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
,
64 error (_("expecting long_set"));
69 m2_print_long_set (struct type
*type
, const gdb_byte
*valaddr
,
70 int embedded_offset
, CORE_ADDR address
,
71 struct ui_file
*stream
)
75 LONGEST previous_low
= 0;
76 LONGEST previous_high
= 0;
77 LONGEST i
, low_bound
, high_bound
;
78 LONGEST field_low
, field_high
;
86 fprintf_filtered (stream
, "{");
87 len
= TYPE_NFIELDS (type
);
88 if (get_long_set_bounds (type
, &low_bound
, &high_bound
))
90 field
= TYPE_N_BASECLASSES (type
);
91 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, field
));
95 fprintf_filtered (stream
, " %s }", _("<unknown bounds of set>"));
99 target
= TYPE_TARGET_TYPE (range
);
101 if (get_discrete_bounds (range
, &field_low
, &field_high
) >= 0)
103 for (i
= low_bound
; i
<= high_bound
; i
++)
105 bitval
= value_bit_index (TYPE_FIELD_TYPE (type
, field
),
106 (TYPE_FIELD_BITPOS (type
, field
) / 8) +
107 valaddr
+ embedded_offset
, i
);
109 error (_("bit test is out of range"));
116 fprintf_filtered (stream
, ", ");
117 print_type_scalar (target
, i
, stream
);
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
);
140 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, field
));
141 if (get_discrete_bounds (range
, &field_low
, &field_high
) < 0)
143 target
= TYPE_TARGET_TYPE (range
);
148 if (previous_low
+1 < previous_high
)
150 fprintf_filtered (stream
, "..");
151 print_type_scalar (target
, previous_high
, stream
);
155 fprintf_filtered (stream
, "}");
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
)
165 struct type
*content_type
;
170 CHECK_TYPEDEF (type
);
171 content_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0));
173 addr
= unpack_pointer (TYPE_FIELD_TYPE (type
, 0),
174 (TYPE_FIELD_BITPOS (type
, 0) / 8) +
175 valaddr
+ embedded_offset
);
177 val
= value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0)),
179 len
= unpack_field_as_long (type
, valaddr
+ embedded_offset
, 1);
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
);
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
)
195 struct gdbarch
*gdbarch
= get_type_arch (type
);
196 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
199 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
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. */
207 if (options
->addressprint
&& options
->format
!= 's')
209 fputs_filtered (paddress (gdbarch
, address
), stream
);
213 /* For a pointer to char or unsigned char, also print the string
214 pointed to, unless pointer is null. */
216 if (TYPE_LENGTH (elttype
) == 1
217 && TYPE_CODE (elttype
) == TYPE_CODE_INT
218 && (options
->format
== 0 || options
->format
== 's')
222 fputs_filtered (" ", stream
);
223 return val_print_string (TYPE_TARGET_TYPE (type
), NULL
, addr
, -1,
231 print_variable_at_address (struct type
*type
,
232 const gdb_byte
*valaddr
,
233 struct ui_file
*stream
,
235 const struct value_print_options
*options
)
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
));
241 fprintf_filtered (stream
, "[");
242 fputs_filtered (paddress (gdbarch
, addr
), stream
);
243 fprintf_filtered (stream
, "] : ");
245 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
247 struct value
*deref_val
=
248 value_at (TYPE_TARGET_TYPE (type
), unpack_pointer (type
, valaddr
));
250 common_val_print (deref_val
, stream
, recurse
, options
, current_language
);
253 fputs_filtered ("???", stream
);
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
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
,
271 CHECK_TYPEDEF (type
);
273 if (TYPE_LENGTH (type
) > 0)
275 if (options
->prettyprint_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
);
286 fprintf_filtered (stream
, "{");
287 val_print_array_elements (type
, valaddr
, embedded_offset
,
288 address
, stream
, recurse
, val
,
290 fprintf_filtered (stream
, "}");
295 /* Decorations for Modula 2. */
297 static const struct generic_val_print_decorations m2_decorations
=
307 /* See val_print for a description of the various parameters of this
308 function; they are identical. */
311 m2_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
312 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
313 const struct value
*original_value
,
314 const struct value_print_options
*options
)
316 struct gdbarch
*gdbarch
= get_type_arch (type
);
317 unsigned int i
= 0; /* Number of characters printed. */
319 struct type
*elttype
;
322 CHECK_TYPEDEF (type
);
323 switch (TYPE_CODE (type
))
325 case TYPE_CODE_ARRAY
:
326 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
328 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
329 len
= TYPE_LENGTH (type
) / TYPE_LENGTH (elttype
);
330 if (options
->prettyprint_arrays
)
331 print_spaces_filtered (2 + 2 * recurse
, stream
);
332 /* For an array of chars, print with string syntax. */
333 if (TYPE_LENGTH (elttype
) == 1 &&
334 ((TYPE_CODE (elttype
) == TYPE_CODE_INT
)
335 || ((current_language
->la_language
== language_m2
)
336 && (TYPE_CODE (elttype
) == TYPE_CODE_CHAR
)))
337 && (options
->format
== 0 || options
->format
== 's'))
339 /* If requested, look for the first null char and only print
340 elements up to it. */
341 if (options
->stop_print_at_null
)
343 unsigned int temp_len
;
345 /* Look for a NULL char. */
347 (valaddr
+ embedded_offset
)[temp_len
]
348 && temp_len
< len
&& temp_len
< options
->print_max
;
353 LA_PRINT_STRING (stream
, TYPE_TARGET_TYPE (type
),
354 valaddr
+ embedded_offset
, len
, NULL
,
360 fprintf_filtered (stream
, "{");
361 val_print_array_elements (type
, valaddr
, embedded_offset
,
363 recurse
, original_value
,
365 fprintf_filtered (stream
, "}");
369 /* Array of unspecified length: treat like pointer to first elt. */
370 print_unpacked_pointer (type
, address
, address
, options
, stream
);
374 if (TYPE_CONST (type
))
375 print_variable_at_address (type
, valaddr
+ embedded_offset
,
376 stream
, recurse
, options
);
377 else if (options
->format
&& options
->format
!= 's')
378 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
379 original_value
, options
, 0, stream
);
382 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
383 print_unpacked_pointer (type
, addr
, address
, options
, stream
);
387 case TYPE_CODE_UNION
:
388 if (recurse
&& !options
->unionprint
)
390 fprintf_filtered (stream
, "{...}");
394 case TYPE_CODE_STRUCT
:
395 if (m2_is_long_set (type
))
396 m2_print_long_set (type
, valaddr
, embedded_offset
, address
,
398 else if (m2_is_unbounded_array (type
))
399 m2_print_unbounded_array (type
, valaddr
, embedded_offset
,
400 address
, stream
, recurse
, options
);
402 cp_print_value_fields (type
, type
, valaddr
, embedded_offset
,
403 address
, stream
, recurse
, original_value
,
408 elttype
= TYPE_INDEX_TYPE (type
);
409 CHECK_TYPEDEF (elttype
);
410 if (TYPE_STUB (elttype
))
412 fprintf_filtered (stream
, _("<incomplete type>"));
418 struct type
*range
= elttype
;
419 LONGEST low_bound
, high_bound
;
423 fputs_filtered ("{", stream
);
425 i
= get_discrete_bounds (range
, &low_bound
, &high_bound
);
429 fputs_filtered (_("<error value>"), stream
);
433 for (i
= low_bound
; i
<= high_bound
; i
++)
435 int element
= value_bit_index (type
, valaddr
+ embedded_offset
,
441 goto maybe_bad_bstring
;
446 fputs_filtered (", ", stream
);
447 print_type_scalar (range
, i
, stream
);
450 if (i
+ 1 <= high_bound
451 && value_bit_index (type
, valaddr
+ embedded_offset
,
456 fputs_filtered ("..", stream
);
457 while (i
+ 1 <= high_bound
458 && value_bit_index (type
,
459 valaddr
+ embedded_offset
,
462 print_type_scalar (range
, j
, stream
);
467 fputs_filtered ("}", stream
);
471 case TYPE_CODE_RANGE
:
472 if (TYPE_LENGTH (type
) == TYPE_LENGTH (TYPE_TARGET_TYPE (type
)))
474 m2_val_print (TYPE_TARGET_TYPE (type
), valaddr
, embedded_offset
,
475 address
, stream
, recurse
, original_value
, options
);
478 /* FIXME: create_range_type does not set the unsigned bit in a
479 range type (I think it probably should copy it from the target
480 type), so we won't print values which are too large to
481 fit in a signed integer correctly. */
482 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
483 print with the target type, though, because the size of our type
484 and the target type might differ). */
492 case TYPE_CODE_METHOD
:
494 case TYPE_CODE_ERROR
:
495 case TYPE_CODE_UNDEF
:
499 generic_val_print (type
, valaddr
, embedded_offset
, address
,
500 stream
, recurse
, original_value
, options
,