]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/m2-valprint.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / m2-valprint.c
CommitLineData
c906108c 1/* Support for printing Modula 2 values for GDB, the GNU debugger.
a8d6eb4a 2
1d506c26 3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
c906108c
SS
21#include "symtab.h"
22#include "gdbtypes.h"
72019c9c
GM
23#include "expression.h"
24#include "value.h"
25#include "valprint.h"
26#include "language.h"
27#include "typeprint.h"
a8d6eb4a 28#include "c-lang.h"
72019c9c
GM
29#include "m2-lang.h"
30#include "target.h"
7f6aba03 31#include "cli/cli-style.h"
72019c9c 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 37static void
59fcdac6 38m2_print_array_contents (struct value *val,
79a45b7d
TT
39 struct ui_file *stream, int recurse,
40 const struct value_print_options *options,
41 int len);
72019c9c
GM
42
43
844781a1 44/* get_long_set_bounds - assigns the bounds of the long set to low and
dda83cd7 45 high. */
72019c9c
GM
46
47int
48get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
49{
50 int len, i;
51
78134374 52 if (type->code () == TYPE_CODE_STRUCT)
72019c9c 53 {
1f704f76 54 len = type->num_fields ();
72019c9c
GM
55 i = TYPE_N_BASECLASSES (type);
56 if (len == 0)
57 return 0;
cf88be68
SM
58 *low = type->field (i).type ()->bounds ()->low.const_val ();
59 *high = type->field (len - 1).type ()->bounds ()->high.const_val ();
72019c9c
GM
60 return 1;
61 }
62 error (_("expecting long_set"));
63 return 0;
64}
65
66static void
67m2_print_long_set (struct type *type, const gdb_byte *valaddr,
68 int embedded_offset, CORE_ADDR address,
79a45b7d 69 struct ui_file *stream)
72019c9c
GM
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
f168693b 82 type = check_typedef (type);
72019c9c 83
6cb06a8c 84 gdb_printf (stream, "{");
1f704f76 85 len = type->num_fields ();
72019c9c
GM
86 if (get_long_set_bounds (type, &low_bound, &high_bound))
87 {
88 field = TYPE_N_BASECLASSES (type);
940da03e 89 range = type->field (field).type ()->index_type ();
72019c9c
GM
90 }
91 else
92 {
7f6aba03
TT
93 fprintf_styled (stream, metadata_style.style (),
94 " %s }", _("<unknown bounds of set>"));
72019c9c
GM
95 return;
96 }
97
27710edb 98 target = range->target_type ();
72019c9c 99
1f8d2881 100 if (get_discrete_bounds (range, &field_low, &field_high))
72019c9c
GM
101 {
102 for (i = low_bound; i <= high_bound; i++)
103 {
940da03e 104 bitval = value_bit_index (type->field (field).type (),
b610c045 105 (type->field (field).loc_bitpos () / 8) +
72019c9c
GM
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)
6cb06a8c 115 gdb_printf (stream, ", ");
72019c9c
GM
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)
6cb06a8c 128 gdb_printf (stream, "..");
72019c9c
GM
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;
940da03e 139 range = type->field (field).type ()->index_type ();
1f8d2881 140 if (!get_discrete_bounds (range, &field_low, &field_high))
72019c9c 141 break;
27710edb 142 target = range->target_type ();
72019c9c
GM
143 }
144 }
145 if (element_seen)
146 {
147 if (previous_low+1 < previous_high)
148 {
6cb06a8c 149 gdb_printf (stream, "..");
72019c9c
GM
150 print_type_scalar (target, previous_high, stream);
151 }
152 element_seen = 0;
153 }
6cb06a8c 154 gdb_printf (stream, "}");
72019c9c
GM
155 }
156}
157
59fcdac6
TT
158static void
159m2_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
d0c97917 167 struct type *type = check_typedef (value->type ());
efaf1ae0 168 const gdb_byte *valaddr = value->contents_for_printing ().data ();
59fcdac6 169
940da03e 170 addr = unpack_pointer (type->field (0).type (),
b610c045 171 (type->field (0).loc_bitpos () / 8) +
59fcdac6
TT
172 valaddr);
173
27710edb 174 val = value_at_lazy (type->field (0).type ()->target_type (),
59fcdac6
TT
175 addr);
176 len = unpack_field_as_long (type, valaddr, 1);
177
6cb06a8c 178 gdb_printf (stream, "{");
59fcdac6 179 m2_print_array_contents (val, stream, recurse, options, len);
6cb06a8c 180 gdb_printf (stream, ", HIGH = %d}", (int) len);
59fcdac6
TT
181}
182
79a45b7d 183static int
72019c9c
GM
184print_unpacked_pointer (struct type *type,
185 CORE_ADDR address, CORE_ADDR addr,
79a45b7d
TT
186 const struct value_print_options *options,
187 struct ui_file *stream)
72019c9c 188{
8ee511af 189 struct gdbarch *gdbarch = type->arch ();
27710edb 190 struct type *elttype = check_typedef (type->target_type ());
b012acdd 191 int want_space = 0;
72019c9c 192
78134374 193 if (elttype->code () == TYPE_CODE_FUNC)
72019c9c
GM
194 {
195 /* Try to print what function it points to. */
edf0c1b7 196 print_function_pointer_address (options, gdbarch, addr, stream);
72019c9c
GM
197 /* Return value is irrelevant except for string pointers. */
198 return 0;
199 }
200
79a45b7d 201 if (options->addressprint && options->format != 's')
b012acdd 202 {
0426ad51 203 gdb_puts (paddress (gdbarch, address), stream);
b012acdd
TT
204 want_space = 1;
205 }
72019c9c
GM
206
207 /* For a pointer to char or unsigned char, also print the string
208 pointed to, unless pointer is null. */
209
df86565b 210 if (elttype->length () == 1
78134374 211 && elttype->code () == TYPE_CODE_INT
79a45b7d 212 && (options->format == 0 || options->format == 's')
72019c9c 213 && addr != 0)
b012acdd
TT
214 {
215 if (want_space)
0426ad51 216 gdb_puts (" ", stream);
27710edb 217 return val_print_string (type->target_type (), NULL, addr, -1,
b012acdd
TT
218 stream, options);
219 }
72019c9c
GM
220
221 return 0;
222}
223
224static void
844781a1
GM
225print_variable_at_address (struct type *type,
226 const gdb_byte *valaddr,
79a45b7d
TT
227 struct ui_file *stream,
228 int recurse,
229 const struct value_print_options *options)
72019c9c 230{
8ee511af 231 struct gdbarch *gdbarch = type->arch ();
72019c9c 232 CORE_ADDR addr = unpack_pointer (type, valaddr);
27710edb 233 struct type *elttype = check_typedef (type->target_type ());
72019c9c 234
6cb06a8c 235 gdb_printf (stream, "[");
0426ad51 236 gdb_puts (paddress (gdbarch, addr), stream);
6cb06a8c 237 gdb_printf (stream, "] : ");
72019c9c 238
78134374 239 if (elttype->code () != TYPE_CODE_UNDEF)
72019c9c
GM
240 {
241 struct value *deref_val =
27710edb 242 value_at (type->target_type (), unpack_pointer (type, valaddr));
b8d56208 243
79a45b7d 244 common_val_print (deref_val, stream, recurse, options, current_language);
72019c9c
GM
245 }
246 else
0426ad51 247 gdb_puts ("???", stream);
72019c9c
GM
248}
249
844781a1
GM
250
251/* m2_print_array_contents - prints out the contents of an
dda83cd7
SM
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. */
844781a1
GM
256
257static void
59fcdac6 258m2_print_array_contents (struct value *val,
79a45b7d
TT
259 struct ui_file *stream, int recurse,
260 const struct value_print_options *options,
261 int len)
844781a1 262{
d0c97917 263 struct type *type = check_typedef (val->type ());
844781a1 264
df86565b 265 if (type->length () > 0)
844781a1 266 {
844781a1 267 /* For an array of chars, print with string syntax. */
df86565b 268 if (type->length () == 1 &&
78134374 269 ((type->code () == TYPE_CODE_INT)
844781a1 270 || ((current_language->la_language == language_m2)
78134374 271 && (type->code () == TYPE_CODE_CHAR)))
79a45b7d 272 && (options->format == 0 || options->format == 's'))
9feb2d07 273 val_print_string (type, NULL, val->address (), len+1, stream,
59fcdac6 274 options);
844781a1
GM
275 else
276 {
6cb06a8c 277 gdb_printf (stream, "{");
59fcdac6 278 value_print_array_elements (val, stream, recurse, options, 0);
6cb06a8c 279 gdb_printf (stream, "}");
844781a1
GM
280 }
281 }
282}
283
e88acd96
TT
284/* Decorations for Modula 2. */
285
286static const struct generic_val_print_decorations m2_decorations =
287{
288 "",
289 " + ",
290 " * I",
291 "TRUE",
292 "FALSE",
00272ec4
TT
293 "void",
294 "{",
295 "}"
e88acd96 296};
844781a1 297
62c4663d
TT
298/* See m2-lang.h. */
299
300void
790e2a12
AB
301m2_language::value_print_inner (struct value *val, struct ui_file *stream,
302 int recurse,
303 const struct value_print_options *options) const
62c4663d 304{
59fcdac6
TT
305 unsigned len;
306 struct type *elttype;
307 CORE_ADDR addr;
efaf1ae0 308 const gdb_byte *valaddr = val->contents_for_printing ().data ();
9feb2d07 309 const CORE_ADDR address = val->address ();
59fcdac6 310
d0c97917 311 struct type *type = check_typedef (val->type ());
78134374 312 switch (type->code ())
59fcdac6
TT
313 {
314 case TYPE_CODE_ARRAY:
df86565b 315 if (type->length () > 0 && type->target_type ()->length () > 0)
59fcdac6 316 {
27710edb 317 elttype = check_typedef (type->target_type ());
df86565b 318 len = type->length () / elttype->length ();
59fcdac6 319 /* For an array of chars, print with string syntax. */
df86565b 320 if (elttype->length () == 1 &&
78134374 321 ((elttype->code () == TYPE_CODE_INT)
59fcdac6 322 || ((current_language->la_language == language_m2)
78134374 323 && (elttype->code () == TYPE_CODE_CHAR)))
59fcdac6
TT
324 && (options->format == 0 || options->format == 's'))
325 {
326 /* If requested, look for the first null char and only print
dda83cd7 327 elements up to it. */
59fcdac6
TT
328 if (options->stop_print_at_null)
329 {
76b58849 330 unsigned int print_max_chars = get_print_max_chars (options);
59fcdac6
TT
331 unsigned int temp_len;
332
333 /* Look for a NULL char. */
334 for (temp_len = 0;
335 (valaddr[temp_len]
76b58849
AB
336 && temp_len < len
337 && temp_len < print_max_chars);
59fcdac6
TT
338 temp_len++);
339 len = temp_len;
340 }
341
27710edb 342 printstr (stream, type->target_type (), valaddr, len,
790e2a12 343 NULL, 0, options);
59fcdac6
TT
344 }
345 else
346 {
6cb06a8c 347 gdb_printf (stream, "{");
59fcdac6
TT
348 value_print_array_elements (val, stream, recurse,
349 options, 0);
6cb06a8c 350 gdb_printf (stream, "}");
59fcdac6
TT
351 }
352 break;
353 }
354 /* Array of unspecified length: treat like pointer to first elt. */
355 print_unpacked_pointer (type, address, address, options, stream);
356 break;
357
358 case TYPE_CODE_PTR:
359 if (TYPE_CONST (type))
360 print_variable_at_address (type, valaddr, stream, recurse, options);
361 else if (options->format && options->format != 's')
362 value_print_scalar_formatted (val, options, 0, stream);
363 else
364 {
365 addr = unpack_pointer (type, valaddr);
366 print_unpacked_pointer (type, addr, address, options, stream);
367 }
368 break;
369
370 case TYPE_CODE_UNION:
371 if (recurse && !options->unionprint)
372 {
6cb06a8c 373 gdb_printf (stream, "{...}");
59fcdac6
TT
374 break;
375 }
d182e398 376 [[fallthrough]];
59fcdac6
TT
377 case TYPE_CODE_STRUCT:
378 if (m2_is_long_set (type))
379 m2_print_long_set (type, valaddr, 0, address, stream);
380 else if (m2_is_unbounded_array (type))
381 m2_print_unbounded_array (val, stream, recurse, options);
382 else
64b653ca 383 cp_print_value_fields (val, stream, recurse, options, NULL, 0);
59fcdac6
TT
384 break;
385
386 case TYPE_CODE_SET:
3d967001 387 elttype = type->index_type ();
59fcdac6 388 elttype = check_typedef (elttype);
e46d3488 389 if (elttype->is_stub ())
59fcdac6
TT
390 {
391 fprintf_styled (stream, metadata_style.style (),
392 _("<incomplete type>"));
393 break;
394 }
395 else
396 {
397 struct type *range = elttype;
398 LONGEST low_bound, high_bound;
399 int i;
400 int need_comma = 0;
401
0426ad51 402 gdb_puts ("{", stream);
59fcdac6 403
1f8d2881 404 i = get_discrete_bounds (range, &low_bound, &high_bound) ? 0 : -1;
59fcdac6
TT
405 maybe_bad_bstring:
406 if (i < 0)
407 {
408 fputs_styled (_("<error value>"), metadata_style.style (),
409 stream);
410 goto done;
411 }
412
413 for (i = low_bound; i <= high_bound; i++)
414 {
415 int element = value_bit_index (type, valaddr, i);
416
417 if (element < 0)
418 {
419 i = element;
420 goto maybe_bad_bstring;
421 }
422 if (element)
423 {
424 if (need_comma)
0426ad51 425 gdb_puts (", ", stream);
59fcdac6
TT
426 print_type_scalar (range, i, stream);
427 need_comma = 1;
428
429 if (i + 1 <= high_bound
430 && value_bit_index (type, valaddr, ++i))
431 {
432 int j = i;
433
0426ad51 434 gdb_puts ("..", stream);
59fcdac6
TT
435 while (i + 1 <= high_bound
436 && value_bit_index (type, valaddr, ++i))
437 j = i;
438 print_type_scalar (range, j, stream);
439 }
440 }
441 }
442 done:
0426ad51 443 gdb_puts ("}", stream);
59fcdac6
TT
444 }
445 break;
446
447 case TYPE_CODE_RANGE:
df86565b 448 if (type->length () == type->target_type ()->length ())
59fcdac6 449 {
27710edb 450 struct value *v = value_cast (type->target_type (), val);
790e2a12 451 value_print_inner (v, stream, recurse, options);
59fcdac6
TT
452 break;
453 }
d182e398 454 [[fallthrough]];
59fcdac6
TT
455
456 case TYPE_CODE_REF:
457 case TYPE_CODE_ENUM:
458 case TYPE_CODE_FUNC:
459 case TYPE_CODE_INT:
460 case TYPE_CODE_FLT:
461 case TYPE_CODE_METHOD:
462 case TYPE_CODE_VOID:
463 case TYPE_CODE_ERROR:
464 case TYPE_CODE_UNDEF:
465 case TYPE_CODE_BOOL:
466 case TYPE_CODE_CHAR:
467 default:
468 generic_value_print (val, stream, recurse, options, &m2_decorations);
469 break;
470 }
62c4663d 471}