Simplify MULTI_SUBSCRIPT implementation
[binutils-gdb.git] / gdb / f-valprint.c
1 /* Support for printing Fortran values for GDB, the GNU debugger.
2
3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
4
5 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
6 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "f-lang.h"
31 #include "frame.h"
32 #include "gdbcore.h"
33 #include "command.h"
34 #include "block.h"
35 #include "dictionary.h"
36 #include "cli/cli-style.h"
37 #include "gdbarch.h"
38 #include "f-array-walker.h"
39
40 static void f77_get_dynamic_length_of_aggregate (struct type *);
41
42 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
43
44 /* Array which holds offsets to be applied to get a row's elements
45 for a given array. Array also holds the size of each subarray. */
46
47 LONGEST
48 f77_get_lowerbound (struct type *type)
49 {
50 if (type->bounds ()->low.kind () == PROP_UNDEFINED)
51 error (_("Lower bound may not be '*' in F77"));
52
53 return type->bounds ()->low.const_val ();
54 }
55
56 LONGEST
57 f77_get_upperbound (struct type *type)
58 {
59 if (type->bounds ()->high.kind () == PROP_UNDEFINED)
60 {
61 /* We have an assumed size array on our hands. Assume that
62 upper_bound == lower_bound so that we show at least 1 element.
63 If the user wants to see more elements, let him manually ask for 'em
64 and we'll subscript the array and show him. */
65
66 return f77_get_lowerbound (type);
67 }
68
69 return type->bounds ()->high.const_val ();
70 }
71
72 /* Obtain F77 adjustable array dimensions. */
73
74 static void
75 f77_get_dynamic_length_of_aggregate (struct type *type)
76 {
77 int upper_bound = -1;
78 int lower_bound = 1;
79
80 /* Recursively go all the way down into a possibly multi-dimensional
81 F77 array and get the bounds. For simple arrays, this is pretty
82 easy but when the bounds are dynamic, we must be very careful
83 to add up all the lengths correctly. Not doing this right
84 will lead to horrendous-looking arrays in parameter lists.
85
86 This function also works for strings which behave very
87 similarly to arrays. */
88
89 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
90 || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRING)
91 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
92
93 /* Recursion ends here, start setting up lengths. */
94 lower_bound = f77_get_lowerbound (type);
95 upper_bound = f77_get_upperbound (type);
96
97 /* Patch in a valid length value. */
98
99 TYPE_LENGTH (type) =
100 (upper_bound - lower_bound + 1)
101 * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
102 }
103
104 /* A class used by FORTRAN_PRINT_ARRAY as a specialisation of the array
105 walking template. This specialisation prints Fortran arrays. */
106
107 class fortran_array_printer_impl : public fortran_array_walker_base_impl
108 {
109 public:
110 /* Constructor. TYPE is the array type being printed, ADDRESS is the
111 address in target memory for the object of TYPE being printed. VAL is
112 the GDB value (of TYPE) being printed. STREAM is where to print to,
113 RECOURSE is passed through (and prevents infinite recursion), and
114 OPTIONS are the printing control options. */
115 explicit fortran_array_printer_impl (struct type *type,
116 CORE_ADDR address,
117 struct value *val,
118 struct ui_file *stream,
119 int recurse,
120 const struct value_print_options *options)
121 : m_elts (0),
122 m_val (val),
123 m_stream (stream),
124 m_recurse (recurse),
125 m_options (options)
126 { /* Nothing. */ }
127
128 /* Called while iterating over the array bounds. When SHOULD_CONTINUE is
129 false then we must return false, as we have reached the end of the
130 array bounds for this dimension. However, we also return false if we
131 have printed too many elements (after printing '...'). In all other
132 cases, return true. */
133 bool continue_walking (bool should_continue)
134 {
135 bool cont = should_continue && (m_elts < m_options->print_max);
136 if (!cont && should_continue)
137 fputs_filtered ("...", m_stream);
138 return cont;
139 }
140
141 /* Called when we start iterating over a dimension. If it's not the
142 inner most dimension then print an opening '(' character. */
143 void start_dimension (bool inner_p)
144 {
145 fputs_filtered ("(", m_stream);
146 }
147
148 /* Called when we finish processing a batch of items within a dimension
149 of the array. Depending on whether this is the inner most dimension
150 or not we print different things, but this is all about adding
151 separators between elements, and dimensions of the array. */
152 void finish_dimension (bool inner_p, bool last_p)
153 {
154 fputs_filtered (")", m_stream);
155 if (!last_p)
156 fputs_filtered (" ", m_stream);
157 }
158
159 /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
160 start of the parent object. */
161 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
162 {
163 /* Extract the element value from the parent value. */
164 struct value *e_val
165 = value_from_component (m_val, elt_type, elt_off);
166 common_val_print (e_val, m_stream, m_recurse, m_options, current_language);
167 if (!last_p)
168 fputs_filtered (", ", m_stream);
169 ++m_elts;
170 }
171
172 private:
173 /* The number of elements printed so far. */
174 int m_elts;
175
176 /* The value from which we are printing elements. */
177 struct value *m_val;
178
179 /* The stream we should print too. */
180 struct ui_file *m_stream;
181
182 /* The recursion counter, passed through when we print each element. */
183 int m_recurse;
184
185 /* The print control options. Gives us the maximum number of elements to
186 print, and is passed through to each element that we print. */
187 const struct value_print_options *m_options = nullptr;
188 };
189
190 /* This function gets called to print a Fortran array. */
191
192 static void
193 fortran_print_array (struct type *type, CORE_ADDR address,
194 struct ui_file *stream, int recurse,
195 const struct value *val,
196 const struct value_print_options *options)
197 {
198 fortran_array_walker<fortran_array_printer_impl> p
199 (type, address, (struct value *) val, stream, recurse, options);
200 p.walk ();
201 }
202 \f
203
204 /* Decorations for Fortran. */
205
206 static const struct generic_val_print_decorations f_decorations =
207 {
208 "(",
209 ",",
210 ")",
211 ".TRUE.",
212 ".FALSE.",
213 "void",
214 "{",
215 "}"
216 };
217
218 /* See f-lang.h. */
219
220 void
221 f_language::value_print_inner (struct value *val, struct ui_file *stream,
222 int recurse,
223 const struct value_print_options *options) const
224 {
225 struct type *type = check_typedef (value_type (val));
226 struct gdbarch *gdbarch = get_type_arch (type);
227 int printed_field = 0; /* Number of fields printed. */
228 struct type *elttype;
229 CORE_ADDR addr;
230 int index;
231 const gdb_byte *valaddr = value_contents_for_printing (val);
232 const CORE_ADDR address = value_address (val);
233
234 switch (type->code ())
235 {
236 case TYPE_CODE_STRING:
237 f77_get_dynamic_length_of_aggregate (type);
238 printstr (stream, builtin_type (gdbarch)->builtin_char, valaddr,
239 TYPE_LENGTH (type), NULL, 0, options);
240 break;
241
242 case TYPE_CODE_ARRAY:
243 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_CHAR)
244 fortran_print_array (type, address, stream, recurse, val, options);
245 else
246 {
247 struct type *ch_type = TYPE_TARGET_TYPE (type);
248
249 f77_get_dynamic_length_of_aggregate (type);
250 printstr (stream, ch_type, valaddr,
251 TYPE_LENGTH (type) / TYPE_LENGTH (ch_type), NULL, 0,
252 options);
253 }
254 break;
255
256 case TYPE_CODE_PTR:
257 if (options->format && options->format != 's')
258 {
259 value_print_scalar_formatted (val, options, 0, stream);
260 break;
261 }
262 else
263 {
264 int want_space = 0;
265
266 addr = unpack_pointer (type, valaddr);
267 elttype = check_typedef (TYPE_TARGET_TYPE (type));
268
269 if (elttype->code () == TYPE_CODE_FUNC)
270 {
271 /* Try to print what function it points to. */
272 print_function_pointer_address (options, gdbarch, addr, stream);
273 return;
274 }
275
276 if (options->symbol_print)
277 want_space = print_address_demangle (options, gdbarch, addr,
278 stream, demangle);
279 else if (options->addressprint && options->format != 's')
280 {
281 fputs_filtered (paddress (gdbarch, addr), stream);
282 want_space = 1;
283 }
284
285 /* For a pointer to char or unsigned char, also print the string
286 pointed to, unless pointer is null. */
287 if (TYPE_LENGTH (elttype) == 1
288 && elttype->code () == TYPE_CODE_INT
289 && (options->format == 0 || options->format == 's')
290 && addr != 0)
291 {
292 if (want_space)
293 fputs_filtered (" ", stream);
294 val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
295 stream, options);
296 }
297 return;
298 }
299 break;
300
301 case TYPE_CODE_STRUCT:
302 case TYPE_CODE_UNION:
303 /* Starting from the Fortran 90 standard, Fortran supports derived
304 types. */
305 fprintf_filtered (stream, "( ");
306 for (index = 0; index < type->num_fields (); index++)
307 {
308 struct value *field = value_field (val, index);
309
310 struct type *field_type = check_typedef (type->field (index).type ());
311
312
313 if (field_type->code () != TYPE_CODE_FUNC)
314 {
315 const char *field_name;
316
317 if (printed_field > 0)
318 fputs_filtered (", ", stream);
319
320 field_name = TYPE_FIELD_NAME (type, index);
321 if (field_name != NULL)
322 {
323 fputs_styled (field_name, variable_name_style.style (),
324 stream);
325 fputs_filtered (" = ", stream);
326 }
327
328 common_val_print (field, stream, recurse + 1,
329 options, current_language);
330
331 ++printed_field;
332 }
333 }
334 fprintf_filtered (stream, " )");
335 break;
336
337 case TYPE_CODE_BOOL:
338 if (options->format || options->output_format)
339 {
340 struct value_print_options opts = *options;
341 opts.format = (options->format ? options->format
342 : options->output_format);
343 value_print_scalar_formatted (val, &opts, 0, stream);
344 }
345 else
346 {
347 LONGEST longval = value_as_long (val);
348 /* The Fortran standard doesn't specify how logical types are
349 represented. Different compilers use different non zero
350 values to represent logical true. */
351 if (longval == 0)
352 fputs_filtered (f_decorations.false_name, stream);
353 else
354 fputs_filtered (f_decorations.true_name, stream);
355 }
356 break;
357
358 case TYPE_CODE_INT:
359 case TYPE_CODE_REF:
360 case TYPE_CODE_FUNC:
361 case TYPE_CODE_FLAGS:
362 case TYPE_CODE_FLT:
363 case TYPE_CODE_VOID:
364 case TYPE_CODE_ERROR:
365 case TYPE_CODE_RANGE:
366 case TYPE_CODE_UNDEF:
367 case TYPE_CODE_COMPLEX:
368 case TYPE_CODE_CHAR:
369 default:
370 generic_value_print (val, stream, recurse, options, &f_decorations);
371 break;
372 }
373 }
374
375 static void
376 info_common_command_for_block (const struct block *block, const char *comname,
377 int *any_printed)
378 {
379 struct block_iterator iter;
380 struct symbol *sym;
381 struct value_print_options opts;
382
383 get_user_print_options (&opts);
384
385 ALL_BLOCK_SYMBOLS (block, iter, sym)
386 if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
387 {
388 const struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
389 size_t index;
390
391 gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);
392
393 if (comname && (!sym->linkage_name ()
394 || strcmp (comname, sym->linkage_name ()) != 0))
395 continue;
396
397 if (*any_printed)
398 putchar_filtered ('\n');
399 else
400 *any_printed = 1;
401 if (sym->print_name ())
402 printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
403 sym->print_name ());
404 else
405 printf_filtered (_("Contents of blank COMMON block:\n"));
406
407 for (index = 0; index < common->n_entries; index++)
408 {
409 struct value *val = NULL;
410
411 printf_filtered ("%s = ",
412 common->contents[index]->print_name ());
413
414 try
415 {
416 val = value_of_variable (common->contents[index], block);
417 value_print (val, gdb_stdout, &opts);
418 }
419
420 catch (const gdb_exception_error &except)
421 {
422 fprintf_styled (gdb_stdout, metadata_style.style (),
423 "<error reading variable: %s>",
424 except.what ());
425 }
426
427 putchar_filtered ('\n');
428 }
429 }
430 }
431
432 /* This function is used to print out the values in a given COMMON
433 block. It will always use the most local common block of the
434 given name. */
435
436 static void
437 info_common_command (const char *comname, int from_tty)
438 {
439 struct frame_info *fi;
440 const struct block *block;
441 int values_printed = 0;
442
443 /* We have been told to display the contents of F77 COMMON
444 block supposedly visible in this function. Let us
445 first make sure that it is visible and if so, let
446 us display its contents. */
447
448 fi = get_selected_frame (_("No frame selected"));
449
450 /* The following is generally ripped off from stack.c's routine
451 print_frame_info(). */
452
453 block = get_frame_block (fi, 0);
454 if (block == NULL)
455 {
456 printf_filtered (_("No symbol table info available.\n"));
457 return;
458 }
459
460 while (block)
461 {
462 info_common_command_for_block (block, comname, &values_printed);
463 /* After handling the function's top-level block, stop. Don't
464 continue to its superblock, the block of per-file symbols. */
465 if (BLOCK_FUNCTION (block))
466 break;
467 block = BLOCK_SUPERBLOCK (block);
468 }
469
470 if (!values_printed)
471 {
472 if (comname)
473 printf_filtered (_("No common block '%s'.\n"), comname);
474 else
475 printf_filtered (_("No common blocks.\n"));
476 }
477 }
478
479 void _initialize_f_valprint ();
480 void
481 _initialize_f_valprint ()
482 {
483 add_info ("common", info_common_command,
484 _("Print out the values contained in a Fortran COMMON block."));
485 }