gdb/fortran: support ALLOCATED builtin
[deliverable/binutils-gdb.git] / gdb / f-lang.c
1 /* Fortran language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1993-2021 Free Software Foundation, Inc.
4
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
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 "parser-defs.h"
28 #include "language.h"
29 #include "varobj.h"
30 #include "gdbcore.h"
31 #include "f-lang.h"
32 #include "valprint.h"
33 #include "value.h"
34 #include "cp-support.h"
35 #include "charset.h"
36 #include "c-lang.h"
37 #include "target-float.h"
38 #include "gdbarch.h"
39 #include "gdbcmd.h"
40 #include "f-array-walker.h"
41
42 #include <math.h>
43
44 /* Whether GDB should repack array slices created by the user. */
45 static bool repack_array_slices = false;
46
47 /* Implement 'show fortran repack-array-slices'. */
48 static void
49 show_repack_array_slices (struct ui_file *file, int from_tty,
50 struct cmd_list_element *c, const char *value)
51 {
52 fprintf_filtered (file, _("Repacking of Fortran array slices is %s.\n"),
53 value);
54 }
55
56 /* Debugging of Fortran's array slicing. */
57 static bool fortran_array_slicing_debug = false;
58
59 /* Implement 'show debug fortran-array-slicing'. */
60 static void
61 show_fortran_array_slicing_debug (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c,
63 const char *value)
64 {
65 fprintf_filtered (file, _("Debugging of Fortran array slicing is %s.\n"),
66 value);
67 }
68
69 /* Local functions */
70
71 static struct value *fortran_argument_convert (struct value *value,
72 bool is_artificial);
73
74 /* Return the encoding that should be used for the character type
75 TYPE. */
76
77 const char *
78 f_language::get_encoding (struct type *type)
79 {
80 const char *encoding;
81
82 switch (TYPE_LENGTH (type))
83 {
84 case 1:
85 encoding = target_charset (type->arch ());
86 break;
87 case 4:
88 if (type_byte_order (type) == BFD_ENDIAN_BIG)
89 encoding = "UTF-32BE";
90 else
91 encoding = "UTF-32LE";
92 break;
93
94 default:
95 error (_("unrecognized character type"));
96 }
97
98 return encoding;
99 }
100
101 \f
102
103 /* Table of operators and their precedences for printing expressions. */
104
105 const struct op_print f_language::op_print_tab[] =
106 {
107 {"+", BINOP_ADD, PREC_ADD, 0},
108 {"+", UNOP_PLUS, PREC_PREFIX, 0},
109 {"-", BINOP_SUB, PREC_ADD, 0},
110 {"-", UNOP_NEG, PREC_PREFIX, 0},
111 {"*", BINOP_MUL, PREC_MUL, 0},
112 {"/", BINOP_DIV, PREC_MUL, 0},
113 {"DIV", BINOP_INTDIV, PREC_MUL, 0},
114 {"MOD", BINOP_REM, PREC_MUL, 0},
115 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
116 {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
117 {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
118 {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
119 {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
120 {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
121 {".LE.", BINOP_LEQ, PREC_ORDER, 0},
122 {".GE.", BINOP_GEQ, PREC_ORDER, 0},
123 {".GT.", BINOP_GTR, PREC_ORDER, 0},
124 {".LT.", BINOP_LESS, PREC_ORDER, 0},
125 {"**", UNOP_IND, PREC_PREFIX, 0},
126 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
127 {NULL, OP_NULL, PREC_REPEAT, 0}
128 };
129 \f
130
131 /* Create an array containing the lower bounds (when LBOUND_P is true) or
132 the upper bounds (when LBOUND_P is false) of ARRAY (which must be of
133 array type). GDBARCH is the current architecture. */
134
135 static struct value *
136 fortran_bounds_all_dims (bool lbound_p,
137 struct gdbarch *gdbarch,
138 struct value *array)
139 {
140 type *array_type = check_typedef (value_type (array));
141 int ndimensions = calc_f77_array_dims (array_type);
142
143 /* Allocate a result value of the correct type. */
144 struct type *range
145 = create_static_range_type (nullptr,
146 builtin_type (gdbarch)->builtin_int,
147 1, ndimensions);
148 struct type *elm_type = builtin_type (gdbarch)->builtin_long_long;
149 struct type *result_type = create_array_type (nullptr, elm_type, range);
150 struct value *result = allocate_value (result_type);
151
152 /* Walk the array dimensions backwards due to the way the array will be
153 laid out in memory, the first dimension will be the most inner. */
154 LONGEST elm_len = TYPE_LENGTH (elm_type);
155 for (LONGEST dst_offset = elm_len * (ndimensions - 1);
156 dst_offset >= 0;
157 dst_offset -= elm_len)
158 {
159 LONGEST b;
160
161 /* Grab the required bound. */
162 if (lbound_p)
163 b = f77_get_lowerbound (array_type);
164 else
165 b = f77_get_upperbound (array_type);
166
167 /* And copy the value into the result value. */
168 struct value *v = value_from_longest (elm_type, b);
169 gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
170 <= TYPE_LENGTH (value_type (result)));
171 gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
172 value_contents_copy (result, dst_offset, v, 0, elm_len);
173
174 /* Peel another dimension of the array. */
175 array_type = TYPE_TARGET_TYPE (array_type);
176 }
177
178 return result;
179 }
180
181 /* Return the lower bound (when LBOUND_P is true) or the upper bound (when
182 LBOUND_P is false) for dimension DIM_VAL (which must be an integer) of
183 ARRAY (which must be an array). GDBARCH is the current architecture. */
184
185 static struct value *
186 fortran_bounds_for_dimension (bool lbound_p,
187 struct gdbarch *gdbarch,
188 struct value *array,
189 struct value *dim_val)
190 {
191 /* Check the requested dimension is valid for this array. */
192 type *array_type = check_typedef (value_type (array));
193 int ndimensions = calc_f77_array_dims (array_type);
194 long dim = value_as_long (dim_val);
195 if (dim < 1 || dim > ndimensions)
196 {
197 if (lbound_p)
198 error (_("LBOUND dimension must be from 1 to %d"), ndimensions);
199 else
200 error (_("UBOUND dimension must be from 1 to %d"), ndimensions);
201 }
202
203 /* The type for the result. */
204 struct type *bound_type = builtin_type (gdbarch)->builtin_long_long;
205
206 /* Walk the dimensions backwards, due to the ordering in which arrays are
207 laid out the first dimension is the most inner. */
208 for (int i = ndimensions - 1; i >= 0; --i)
209 {
210 /* If this is the requested dimension then we're done. Grab the
211 bounds and return. */
212 if (i == dim - 1)
213 {
214 LONGEST b;
215
216 if (lbound_p)
217 b = f77_get_lowerbound (array_type);
218 else
219 b = f77_get_upperbound (array_type);
220
221 return value_from_longest (bound_type, b);
222 }
223
224 /* Peel off another dimension of the array. */
225 array_type = TYPE_TARGET_TYPE (array_type);
226 }
227
228 gdb_assert_not_reached ("failed to find matching dimension");
229 }
230 \f
231
232 /* Return the number of dimensions for a Fortran array or string. */
233
234 int
235 calc_f77_array_dims (struct type *array_type)
236 {
237 int ndimen = 1;
238 struct type *tmp_type;
239
240 if ((array_type->code () == TYPE_CODE_STRING))
241 return 1;
242
243 if ((array_type->code () != TYPE_CODE_ARRAY))
244 error (_("Can't get dimensions for a non-array type"));
245
246 tmp_type = array_type;
247
248 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
249 {
250 if (tmp_type->code () == TYPE_CODE_ARRAY)
251 ++ndimen;
252 }
253 return ndimen;
254 }
255
256 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
257 slices. This is a base class for two alternative repacking mechanisms,
258 one for when repacking from a lazy value, and one for repacking from a
259 non-lazy (already loaded) value. */
260 class fortran_array_repacker_base_impl
261 : public fortran_array_walker_base_impl
262 {
263 public:
264 /* Constructor, DEST is the value we are repacking into. */
265 fortran_array_repacker_base_impl (struct value *dest)
266 : m_dest (dest),
267 m_dest_offset (0)
268 { /* Nothing. */ }
269
270 /* When we start processing the inner most dimension, this is where we
271 will be creating values for each element as we load them and then copy
272 them into the M_DEST value. Set a value mark so we can free these
273 temporary values. */
274 void start_dimension (bool inner_p)
275 {
276 if (inner_p)
277 {
278 gdb_assert (m_mark == nullptr);
279 m_mark = value_mark ();
280 }
281 }
282
283 /* When we finish processing the inner most dimension free all temporary
284 value that were created. */
285 void finish_dimension (bool inner_p, bool last_p)
286 {
287 if (inner_p)
288 {
289 gdb_assert (m_mark != nullptr);
290 value_free_to_mark (m_mark);
291 m_mark = nullptr;
292 }
293 }
294
295 protected:
296 /* Copy the contents of array element ELT into M_DEST at the next
297 available offset. */
298 void copy_element_to_dest (struct value *elt)
299 {
300 value_contents_copy (m_dest, m_dest_offset, elt, 0,
301 TYPE_LENGTH (value_type (elt)));
302 m_dest_offset += TYPE_LENGTH (value_type (elt));
303 }
304
305 /* The value being written to. */
306 struct value *m_dest;
307
308 /* The byte offset in M_DEST at which the next element should be
309 written. */
310 LONGEST m_dest_offset;
311
312 /* Set with a call to VALUE_MARK, and then reset after calling
313 VALUE_FREE_TO_MARK. */
314 struct value *m_mark = nullptr;
315 };
316
317 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
318 slices. This class is specialised for repacking an array slice from a
319 lazy array value, as such it does not require the parent array value to
320 be loaded into GDB's memory; the parent value could be huge, while the
321 slice could be tiny. */
322 class fortran_lazy_array_repacker_impl
323 : public fortran_array_repacker_base_impl
324 {
325 public:
326 /* Constructor. TYPE is the type of the slice being loaded from the
327 parent value, so this type will correctly reflect the strides required
328 to find all of the elements from the parent value. ADDRESS is the
329 address in target memory of value matching TYPE, and DEST is the value
330 we are repacking into. */
331 explicit fortran_lazy_array_repacker_impl (struct type *type,
332 CORE_ADDR address,
333 struct value *dest)
334 : fortran_array_repacker_base_impl (dest),
335 m_addr (address)
336 { /* Nothing. */ }
337
338 /* Create a lazy value in target memory representing a single element,
339 then load the element into GDB's memory and copy the contents into the
340 destination value. */
341 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
342 {
343 copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
344 }
345
346 private:
347 /* The address in target memory where the parent value starts. */
348 CORE_ADDR m_addr;
349 };
350
351 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
352 slices. This class is specialised for repacking an array slice from a
353 previously loaded (non-lazy) array value, as such it fetches the
354 element values from the contents of the parent value. */
355 class fortran_array_repacker_impl
356 : public fortran_array_repacker_base_impl
357 {
358 public:
359 /* Constructor. TYPE is the type for the array slice within the parent
360 value, as such it has stride values as required to find the elements
361 within the original parent value. ADDRESS is the address in target
362 memory of the value matching TYPE. BASE_OFFSET is the offset from
363 the start of VAL's content buffer to the start of the object of TYPE,
364 VAL is the parent object from which we are loading the value, and
365 DEST is the value into which we are repacking. */
366 explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
367 LONGEST base_offset,
368 struct value *val, struct value *dest)
369 : fortran_array_repacker_base_impl (dest),
370 m_base_offset (base_offset),
371 m_val (val)
372 {
373 gdb_assert (!value_lazy (val));
374 }
375
376 /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
377 from the content buffer of M_VAL then copy this extracted value into
378 the repacked destination value. */
379 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
380 {
381 struct value *elt
382 = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
383 copy_element_to_dest (elt);
384 }
385
386 private:
387 /* The offset into the content buffer of M_VAL to the start of the slice
388 being extracted. */
389 LONGEST m_base_offset;
390
391 /* The parent value from which we are extracting a slice. */
392 struct value *m_val;
393 };
394
395 /* Called from evaluate_subexp_standard to perform array indexing, and
396 sub-range extraction, for Fortran. As well as arrays this function
397 also handles strings as they can be treated like arrays of characters.
398 ARRAY is the array or string being accessed. EXP, POS, and NOSIDE are
399 as for evaluate_subexp_standard, and NARGS is the number of arguments
400 in this access (e.g. 'array (1,2,3)' would be NARGS 3). */
401
402 static struct value *
403 fortran_value_subarray (struct value *array, struct expression *exp,
404 int *pos, int nargs, enum noside noside)
405 {
406 type *original_array_type = check_typedef (value_type (array));
407 bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
408
409 /* Perform checks for ARRAY not being available. The somewhat overly
410 complex logic here is just to keep backward compatibility with the
411 errors that we used to get before FORTRAN_VALUE_SUBARRAY was
412 rewritten. Maybe a future task would streamline the error messages we
413 get here, and update all the expected test results. */
414 if (exp->elts[*pos].opcode != OP_RANGE)
415 {
416 if (type_not_associated (original_array_type))
417 error (_("no such vector element (vector not associated)"));
418 else if (type_not_allocated (original_array_type))
419 error (_("no such vector element (vector not allocated)"));
420 }
421 else
422 {
423 if (type_not_associated (original_array_type))
424 error (_("array not associated"));
425 else if (type_not_allocated (original_array_type))
426 error (_("array not allocated"));
427 }
428
429 /* First check that the number of dimensions in the type we are slicing
430 matches the number of arguments we were passed. */
431 int ndimensions = calc_f77_array_dims (original_array_type);
432 if (nargs != ndimensions)
433 error (_("Wrong number of subscripts"));
434
435 /* This will be initialised below with the type of the elements held in
436 ARRAY. */
437 struct type *inner_element_type;
438
439 /* Extract the types of each array dimension from the original array
440 type. We need these available so we can fill in the default upper and
441 lower bounds if the user requested slice doesn't provide that
442 information. Additionally unpacking the dimensions like this gives us
443 the inner element type. */
444 std::vector<struct type *> dim_types;
445 {
446 dim_types.reserve (ndimensions);
447 struct type *type = original_array_type;
448 for (int i = 0; i < ndimensions; ++i)
449 {
450 dim_types.push_back (type);
451 type = TYPE_TARGET_TYPE (type);
452 }
453 /* TYPE is now the inner element type of the array, we start the new
454 array slice off as this type, then as we process the requested slice
455 (from the user) we wrap new types around this to build up the final
456 slice type. */
457 inner_element_type = type;
458 }
459
460 /* As we analyse the new slice type we need to understand if the data
461 being referenced is contiguous. Do decide this we must track the size
462 of an element at each dimension of the new slice array. Initially the
463 elements of the inner most dimension of the array are the same inner
464 most elements as the original ARRAY. */
465 LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
466
467 /* Start off assuming all data is contiguous, this will be set to false
468 if access to any dimension results in non-contiguous data. */
469 bool is_all_contiguous = true;
470
471 /* The TOTAL_OFFSET is the distance in bytes from the start of the
472 original ARRAY to the start of the new slice. This is calculated as
473 we process the information from the user. */
474 LONGEST total_offset = 0;
475
476 /* A structure representing information about each dimension of the
477 resulting slice. */
478 struct slice_dim
479 {
480 /* Constructor. */
481 slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
482 : low (l),
483 high (h),
484 stride (s),
485 index (idx)
486 { /* Nothing. */ }
487
488 /* The low bound for this dimension of the slice. */
489 LONGEST low;
490
491 /* The high bound for this dimension of the slice. */
492 LONGEST high;
493
494 /* The byte stride for this dimension of the slice. */
495 LONGEST stride;
496
497 struct type *index;
498 };
499
500 /* The dimensions of the resulting slice. */
501 std::vector<slice_dim> slice_dims;
502
503 /* Process the incoming arguments. These arguments are in the reverse
504 order to the array dimensions, that is the first argument refers to
505 the last array dimension. */
506 if (fortran_array_slicing_debug)
507 debug_printf ("Processing array access:\n");
508 for (int i = 0; i < nargs; ++i)
509 {
510 /* For each dimension of the array the user will have either provided
511 a ranged access with optional lower bound, upper bound, and
512 stride, or the user will have supplied a single index. */
513 struct type *dim_type = dim_types[ndimensions - (i + 1)];
514 if (exp->elts[*pos].opcode == OP_RANGE)
515 {
516 int pc = (*pos) + 1;
517 enum range_flag range_flag = (enum range_flag) exp->elts[pc].longconst;
518 *pos += 3;
519
520 LONGEST low, high, stride;
521 low = high = stride = 0;
522
523 if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
524 low = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
525 else
526 low = f77_get_lowerbound (dim_type);
527 if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
528 high = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
529 else
530 high = f77_get_upperbound (dim_type);
531 if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
532 stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
533 else
534 stride = 1;
535
536 if (stride == 0)
537 error (_("stride must not be 0"));
538
539 /* Get information about this dimension in the original ARRAY. */
540 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
541 struct type *index_type = dim_type->index_type ();
542 LONGEST lb = f77_get_lowerbound (dim_type);
543 LONGEST ub = f77_get_upperbound (dim_type);
544 LONGEST sd = index_type->bit_stride ();
545 if (sd == 0)
546 sd = TYPE_LENGTH (target_type) * 8;
547
548 if (fortran_array_slicing_debug)
549 {
550 debug_printf ("|-> Range access\n");
551 std::string str = type_to_string (dim_type);
552 debug_printf ("| |-> Type: %s\n", str.c_str ());
553 debug_printf ("| |-> Array:\n");
554 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
555 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
556 debug_printf ("| | |-> Bit stride: %s\n", plongest (sd));
557 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8));
558 debug_printf ("| | |-> Type size: %s\n",
559 pulongest (TYPE_LENGTH (dim_type)));
560 debug_printf ("| | '-> Target type size: %s\n",
561 pulongest (TYPE_LENGTH (target_type)));
562 debug_printf ("| |-> Accessing:\n");
563 debug_printf ("| | |-> Low bound: %s\n",
564 plongest (low));
565 debug_printf ("| | |-> High bound: %s\n",
566 plongest (high));
567 debug_printf ("| | '-> Element stride: %s\n",
568 plongest (stride));
569 }
570
571 /* Check the user hasn't asked for something invalid. */
572 if (high > ub || low < lb)
573 error (_("array subscript out of bounds"));
574
575 /* Calculate what this dimension of the new slice array will look
576 like. OFFSET is the byte offset from the start of the
577 previous (more outer) dimension to the start of this
578 dimension. E_COUNT is the number of elements in this
579 dimension. REMAINDER is the number of elements remaining
580 between the last included element and the upper bound. For
581 example an access '1:6:2' will include elements 1, 3, 5 and
582 have a remainder of 1 (element #6). */
583 LONGEST lowest = std::min (low, high);
584 LONGEST offset = (sd / 8) * (lowest - lb);
585 LONGEST e_count = std::abs (high - low) + 1;
586 e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
587 LONGEST new_low = 1;
588 LONGEST new_high = new_low + e_count - 1;
589 LONGEST new_stride = (sd * stride) / 8;
590 LONGEST last_elem = low + ((e_count - 1) * stride);
591 LONGEST remainder = high - last_elem;
592 if (low > high)
593 {
594 offset += std::abs (remainder) * TYPE_LENGTH (target_type);
595 if (stride > 0)
596 error (_("incorrect stride and boundary combination"));
597 }
598 else if (stride < 0)
599 error (_("incorrect stride and boundary combination"));
600
601 /* Is the data within this dimension contiguous? It is if the
602 newly computed stride is the same size as a single element of
603 this dimension. */
604 bool is_dim_contiguous = (new_stride == slice_element_size);
605 is_all_contiguous &= is_dim_contiguous;
606
607 if (fortran_array_slicing_debug)
608 {
609 debug_printf ("| '-> Results:\n");
610 debug_printf ("| |-> Offset = %s\n", plongest (offset));
611 debug_printf ("| |-> Elements = %s\n", plongest (e_count));
612 debug_printf ("| |-> Low bound = %s\n", plongest (new_low));
613 debug_printf ("| |-> High bound = %s\n",
614 plongest (new_high));
615 debug_printf ("| |-> Byte stride = %s\n",
616 plongest (new_stride));
617 debug_printf ("| |-> Last element = %s\n",
618 plongest (last_elem));
619 debug_printf ("| |-> Remainder = %s\n",
620 plongest (remainder));
621 debug_printf ("| '-> Contiguous = %s\n",
622 (is_dim_contiguous ? "Yes" : "No"));
623 }
624
625 /* Figure out how big (in bytes) an element of this dimension of
626 the new array slice will be. */
627 slice_element_size = std::abs (new_stride * e_count);
628
629 slice_dims.emplace_back (new_low, new_high, new_stride,
630 index_type);
631
632 /* Update the total offset. */
633 total_offset += offset;
634 }
635 else
636 {
637 /* There is a single index for this dimension. */
638 LONGEST index
639 = value_as_long (evaluate_subexp_with_coercion (exp, pos, noside));
640
641 /* Get information about this dimension in the original ARRAY. */
642 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
643 struct type *index_type = dim_type->index_type ();
644 LONGEST lb = f77_get_lowerbound (dim_type);
645 LONGEST ub = f77_get_upperbound (dim_type);
646 LONGEST sd = index_type->bit_stride () / 8;
647 if (sd == 0)
648 sd = TYPE_LENGTH (target_type);
649
650 if (fortran_array_slicing_debug)
651 {
652 debug_printf ("|-> Index access\n");
653 std::string str = type_to_string (dim_type);
654 debug_printf ("| |-> Type: %s\n", str.c_str ());
655 debug_printf ("| |-> Array:\n");
656 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
657 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
658 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd));
659 debug_printf ("| | |-> Type size: %s\n",
660 pulongest (TYPE_LENGTH (dim_type)));
661 debug_printf ("| | '-> Target type size: %s\n",
662 pulongest (TYPE_LENGTH (target_type)));
663 debug_printf ("| '-> Accessing:\n");
664 debug_printf ("| '-> Index: %s\n",
665 plongest (index));
666 }
667
668 /* If the array has actual content then check the index is in
669 bounds. An array without content (an unbound array) doesn't
670 have a known upper bound, so don't error check in that
671 situation. */
672 if (index < lb
673 || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
674 && index > ub)
675 || (VALUE_LVAL (array) != lval_memory
676 && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
677 {
678 if (type_not_associated (dim_type))
679 error (_("no such vector element (vector not associated)"));
680 else if (type_not_allocated (dim_type))
681 error (_("no such vector element (vector not allocated)"));
682 else
683 error (_("no such vector element"));
684 }
685
686 /* Calculate using the type stride, not the target type size. */
687 LONGEST offset = sd * (index - lb);
688 total_offset += offset;
689 }
690 }
691
692 if (noside == EVAL_SKIP)
693 return array;
694
695 /* Build a type that represents the new array slice in the target memory
696 of the original ARRAY, this type makes use of strides to correctly
697 find only those elements that are part of the new slice. */
698 struct type *array_slice_type = inner_element_type;
699 for (const auto &d : slice_dims)
700 {
701 /* Create the range. */
702 dynamic_prop p_low, p_high, p_stride;
703
704 p_low.set_const_val (d.low);
705 p_high.set_const_val (d.high);
706 p_stride.set_const_val (d.stride);
707
708 struct type *new_range
709 = create_range_type_with_stride ((struct type *) NULL,
710 TYPE_TARGET_TYPE (d.index),
711 &p_low, &p_high, 0, &p_stride,
712 true);
713 array_slice_type
714 = create_array_type (nullptr, array_slice_type, new_range);
715 }
716
717 if (fortran_array_slicing_debug)
718 {
719 debug_printf ("'-> Final result:\n");
720 debug_printf (" |-> Type: %s\n",
721 type_to_string (array_slice_type).c_str ());
722 debug_printf (" |-> Total offset: %s\n",
723 plongest (total_offset));
724 debug_printf (" |-> Base address: %s\n",
725 core_addr_to_string (value_address (array)));
726 debug_printf (" '-> Contiguous = %s\n",
727 (is_all_contiguous ? "Yes" : "No"));
728 }
729
730 /* Should we repack this array slice? */
731 if (!is_all_contiguous && (repack_array_slices || is_string_p))
732 {
733 /* Build a type for the repacked slice. */
734 struct type *repacked_array_type = inner_element_type;
735 for (const auto &d : slice_dims)
736 {
737 /* Create the range. */
738 dynamic_prop p_low, p_high, p_stride;
739
740 p_low.set_const_val (d.low);
741 p_high.set_const_val (d.high);
742 p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
743
744 struct type *new_range
745 = create_range_type_with_stride ((struct type *) NULL,
746 TYPE_TARGET_TYPE (d.index),
747 &p_low, &p_high, 0, &p_stride,
748 true);
749 repacked_array_type
750 = create_array_type (nullptr, repacked_array_type, new_range);
751 }
752
753 /* Now copy the elements from the original ARRAY into the packed
754 array value DEST. */
755 struct value *dest = allocate_value (repacked_array_type);
756 if (value_lazy (array)
757 || (total_offset + TYPE_LENGTH (array_slice_type)
758 > TYPE_LENGTH (check_typedef (value_type (array)))))
759 {
760 fortran_array_walker<fortran_lazy_array_repacker_impl> p
761 (array_slice_type, value_address (array) + total_offset, dest);
762 p.walk ();
763 }
764 else
765 {
766 fortran_array_walker<fortran_array_repacker_impl> p
767 (array_slice_type, value_address (array) + total_offset,
768 total_offset, array, dest);
769 p.walk ();
770 }
771 array = dest;
772 }
773 else
774 {
775 if (VALUE_LVAL (array) == lval_memory)
776 {
777 /* If the value we're taking a slice from is not yet loaded, or
778 the requested slice is outside the values content range then
779 just create a new lazy value pointing at the memory where the
780 contents we're looking for exist. */
781 if (value_lazy (array)
782 || (total_offset + TYPE_LENGTH (array_slice_type)
783 > TYPE_LENGTH (check_typedef (value_type (array)))))
784 array = value_at_lazy (array_slice_type,
785 value_address (array) + total_offset);
786 else
787 array = value_from_contents_and_address (array_slice_type,
788 (value_contents (array)
789 + total_offset),
790 (value_address (array)
791 + total_offset));
792 }
793 else if (!value_lazy (array))
794 array = value_from_component (array, array_slice_type, total_offset);
795 else
796 error (_("cannot subscript arrays that are not in memory"));
797 }
798
799 return array;
800 }
801
802 /* Special expression evaluation cases for Fortran. */
803
804 static struct value *
805 evaluate_subexp_f (struct type *expect_type, struct expression *exp,
806 int *pos, enum noside noside)
807 {
808 struct value *arg1 = NULL, *arg2 = NULL;
809 enum exp_opcode op;
810 int pc;
811 struct type *type;
812
813 pc = *pos;
814 *pos += 1;
815 op = exp->elts[pc].opcode;
816
817 switch (op)
818 {
819 default:
820 *pos -= 1;
821 return evaluate_subexp_standard (expect_type, exp, pos, noside);
822
823 case UNOP_ABS:
824 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
825 if (noside == EVAL_SKIP)
826 return eval_skip_value (exp);
827 type = value_type (arg1);
828 switch (type->code ())
829 {
830 case TYPE_CODE_FLT:
831 {
832 double d
833 = fabs (target_float_to_host_double (value_contents (arg1),
834 value_type (arg1)));
835 return value_from_host_double (type, d);
836 }
837 case TYPE_CODE_INT:
838 {
839 LONGEST l = value_as_long (arg1);
840 l = llabs (l);
841 return value_from_longest (type, l);
842 }
843 }
844 error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
845
846 case BINOP_MOD:
847 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
848 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
849 if (noside == EVAL_SKIP)
850 return eval_skip_value (exp);
851 type = value_type (arg1);
852 if (type->code () != value_type (arg2)->code ())
853 error (_("non-matching types for parameters to MOD ()"));
854 switch (type->code ())
855 {
856 case TYPE_CODE_FLT:
857 {
858 double d1
859 = target_float_to_host_double (value_contents (arg1),
860 value_type (arg1));
861 double d2
862 = target_float_to_host_double (value_contents (arg2),
863 value_type (arg2));
864 double d3 = fmod (d1, d2);
865 return value_from_host_double (type, d3);
866 }
867 case TYPE_CODE_INT:
868 {
869 LONGEST v1 = value_as_long (arg1);
870 LONGEST v2 = value_as_long (arg2);
871 if (v2 == 0)
872 error (_("calling MOD (N, 0) is undefined"));
873 LONGEST v3 = v1 - (v1 / v2) * v2;
874 return value_from_longest (value_type (arg1), v3);
875 }
876 }
877 error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
878
879 case UNOP_FORTRAN_CEILING:
880 {
881 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
882 if (noside == EVAL_SKIP)
883 return eval_skip_value (exp);
884 type = value_type (arg1);
885 if (type->code () != TYPE_CODE_FLT)
886 error (_("argument to CEILING must be of type float"));
887 double val
888 = target_float_to_host_double (value_contents (arg1),
889 value_type (arg1));
890 val = ceil (val);
891 return value_from_host_double (type, val);
892 }
893
894 case UNOP_FORTRAN_FLOOR:
895 {
896 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
897 if (noside == EVAL_SKIP)
898 return eval_skip_value (exp);
899 type = value_type (arg1);
900 if (type->code () != TYPE_CODE_FLT)
901 error (_("argument to FLOOR must be of type float"));
902 double val
903 = target_float_to_host_double (value_contents (arg1),
904 value_type (arg1));
905 val = floor (val);
906 return value_from_host_double (type, val);
907 }
908
909 case UNOP_FORTRAN_ALLOCATED:
910 {
911 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
912 if (noside == EVAL_SKIP)
913 return eval_skip_value (exp);
914 type = check_typedef (value_type (arg1));
915 if (type->code () != TYPE_CODE_ARRAY)
916 error (_("ALLOCATED can only be applied to arrays"));
917 struct type *result_type
918 = builtin_f_type (exp->gdbarch)->builtin_logical;
919 LONGEST result_value = type_not_allocated (type) ? 0 : 1;
920 return value_from_longest (result_type, result_value);
921 }
922
923 case BINOP_FORTRAN_MODULO:
924 {
925 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
926 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
927 if (noside == EVAL_SKIP)
928 return eval_skip_value (exp);
929 type = value_type (arg1);
930 if (type->code () != value_type (arg2)->code ())
931 error (_("non-matching types for parameters to MODULO ()"));
932 /* MODULO(A, P) = A - FLOOR (A / P) * P */
933 switch (type->code ())
934 {
935 case TYPE_CODE_INT:
936 {
937 LONGEST a = value_as_long (arg1);
938 LONGEST p = value_as_long (arg2);
939 LONGEST result = a - (a / p) * p;
940 if (result != 0 && (a < 0) != (p < 0))
941 result += p;
942 return value_from_longest (value_type (arg1), result);
943 }
944 case TYPE_CODE_FLT:
945 {
946 double a
947 = target_float_to_host_double (value_contents (arg1),
948 value_type (arg1));
949 double p
950 = target_float_to_host_double (value_contents (arg2),
951 value_type (arg2));
952 double result = fmod (a, p);
953 if (result != 0 && (a < 0.0) != (p < 0.0))
954 result += p;
955 return value_from_host_double (type, result);
956 }
957 }
958 error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
959 }
960
961 case FORTRAN_LBOUND:
962 case FORTRAN_UBOUND:
963 {
964 int nargs = longest_to_int (exp->elts[pc + 1].longconst);
965 (*pos) += 2;
966
967 /* This assertion should be enforced by the expression parser. */
968 gdb_assert (nargs == 1 || nargs == 2);
969
970 bool lbound_p = op == FORTRAN_LBOUND;
971
972 /* Check that the first argument is array like. */
973 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
974 type = check_typedef (value_type (arg1));
975 if (type->code () != TYPE_CODE_ARRAY)
976 {
977 if (lbound_p)
978 error (_("LBOUND can only be applied to arrays"));
979 else
980 error (_("UBOUND can only be applied to arrays"));
981 }
982
983 if (nargs == 1)
984 return fortran_bounds_all_dims (lbound_p, exp->gdbarch, arg1);
985
986 /* User asked for the bounds of a specific dimension of the array. */
987 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
988 type = check_typedef (value_type (arg2));
989 if (type->code () != TYPE_CODE_INT)
990 {
991 if (lbound_p)
992 error (_("LBOUND second argument should be an integer"));
993 else
994 error (_("UBOUND second argument should be an integer"));
995 }
996
997 return fortran_bounds_for_dimension (lbound_p, exp->gdbarch, arg1,
998 arg2);
999 }
1000 break;
1001
1002 case BINOP_FORTRAN_CMPLX:
1003 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1004 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1005 if (noside == EVAL_SKIP)
1006 return eval_skip_value (exp);
1007 type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
1008 return value_literal_complex (arg1, arg2, type);
1009
1010 case UNOP_FORTRAN_KIND:
1011 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1012 type = value_type (arg1);
1013
1014 switch (type->code ())
1015 {
1016 case TYPE_CODE_STRUCT:
1017 case TYPE_CODE_UNION:
1018 case TYPE_CODE_MODULE:
1019 case TYPE_CODE_FUNC:
1020 error (_("argument to kind must be an intrinsic type"));
1021 }
1022
1023 if (!TYPE_TARGET_TYPE (type))
1024 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1025 TYPE_LENGTH (type));
1026 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1027 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
1028
1029
1030 case OP_F77_UNDETERMINED_ARGLIST:
1031 /* Remember that in F77, functions, substring ops and array subscript
1032 operations cannot be disambiguated at parse time. We have made
1033 all array subscript operations, substring operations as well as
1034 function calls come here and we now have to discover what the heck
1035 this thing actually was. If it is a function, we process just as
1036 if we got an OP_FUNCALL. */
1037 int nargs = longest_to_int (exp->elts[pc + 1].longconst);
1038 (*pos) += 2;
1039
1040 /* First determine the type code we are dealing with. */
1041 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1042 type = check_typedef (value_type (arg1));
1043 enum type_code code = type->code ();
1044
1045 if (code == TYPE_CODE_PTR)
1046 {
1047 /* Fortran always passes variable to subroutines as pointer.
1048 So we need to look into its target type to see if it is
1049 array, string or function. If it is, we need to switch
1050 to the target value the original one points to. */
1051 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1052
1053 if (target_type->code () == TYPE_CODE_ARRAY
1054 || target_type->code () == TYPE_CODE_STRING
1055 || target_type->code () == TYPE_CODE_FUNC)
1056 {
1057 arg1 = value_ind (arg1);
1058 type = check_typedef (value_type (arg1));
1059 code = type->code ();
1060 }
1061 }
1062
1063 switch (code)
1064 {
1065 case TYPE_CODE_ARRAY:
1066 case TYPE_CODE_STRING:
1067 return fortran_value_subarray (arg1, exp, pos, nargs, noside);
1068
1069 case TYPE_CODE_PTR:
1070 case TYPE_CODE_FUNC:
1071 case TYPE_CODE_INTERNAL_FUNCTION:
1072 {
1073 /* It's a function call. Allocate arg vector, including
1074 space for the function to be called in argvec[0] and a
1075 termination NULL. */
1076 struct value **argvec = (struct value **)
1077 alloca (sizeof (struct value *) * (nargs + 2));
1078 argvec[0] = arg1;
1079 int tem = 1;
1080 for (; tem <= nargs; tem++)
1081 {
1082 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1083 /* Arguments in Fortran are passed by address. Coerce the
1084 arguments here rather than in value_arg_coerce as
1085 otherwise the call to malloc to place the non-lvalue
1086 parameters in target memory is hit by this Fortran
1087 specific logic. This results in malloc being called
1088 with a pointer to an integer followed by an attempt to
1089 malloc the arguments to malloc in target memory.
1090 Infinite recursion ensues. */
1091 if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
1092 {
1093 bool is_artificial
1094 = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
1095 argvec[tem] = fortran_argument_convert (argvec[tem],
1096 is_artificial);
1097 }
1098 }
1099 argvec[tem] = 0; /* signal end of arglist */
1100 if (noside == EVAL_SKIP)
1101 return eval_skip_value (exp);
1102 return evaluate_subexp_do_call (exp, noside, argvec[0],
1103 gdb::make_array_view (argvec + 1,
1104 nargs),
1105 NULL, expect_type);
1106 }
1107
1108 default:
1109 error (_("Cannot perform substring on this type"));
1110 }
1111 }
1112
1113 /* Should be unreachable. */
1114 return nullptr;
1115 }
1116
1117 /* Special expression lengths for Fortran. */
1118
1119 static void
1120 operator_length_f (const struct expression *exp, int pc, int *oplenp,
1121 int *argsp)
1122 {
1123 int oplen = 1;
1124 int args = 0;
1125
1126 switch (exp->elts[pc - 1].opcode)
1127 {
1128 default:
1129 operator_length_standard (exp, pc, oplenp, argsp);
1130 return;
1131
1132 case UNOP_FORTRAN_KIND:
1133 case UNOP_FORTRAN_FLOOR:
1134 case UNOP_FORTRAN_CEILING:
1135 case UNOP_FORTRAN_ALLOCATED:
1136 oplen = 1;
1137 args = 1;
1138 break;
1139
1140 case BINOP_FORTRAN_CMPLX:
1141 case BINOP_FORTRAN_MODULO:
1142 oplen = 1;
1143 args = 2;
1144 break;
1145
1146 case FORTRAN_LBOUND:
1147 case FORTRAN_UBOUND:
1148 oplen = 3;
1149 args = longest_to_int (exp->elts[pc - 2].longconst);
1150 break;
1151
1152 case OP_F77_UNDETERMINED_ARGLIST:
1153 oplen = 3;
1154 args = 1 + longest_to_int (exp->elts[pc - 2].longconst);
1155 break;
1156 }
1157
1158 *oplenp = oplen;
1159 *argsp = args;
1160 }
1161
1162 /* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
1163 the extra argument NAME which is the text that should be printed as the
1164 name of this operation. */
1165
1166 static void
1167 print_unop_subexp_f (struct expression *exp, int *pos,
1168 struct ui_file *stream, enum precedence prec,
1169 const char *name)
1170 {
1171 (*pos)++;
1172 fprintf_filtered (stream, "%s(", name);
1173 print_subexp (exp, pos, stream, PREC_SUFFIX);
1174 fputs_filtered (")", stream);
1175 }
1176
1177 /* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
1178 the extra argument NAME which is the text that should be printed as the
1179 name of this operation. */
1180
1181 static void
1182 print_binop_subexp_f (struct expression *exp, int *pos,
1183 struct ui_file *stream, enum precedence prec,
1184 const char *name)
1185 {
1186 (*pos)++;
1187 fprintf_filtered (stream, "%s(", name);
1188 print_subexp (exp, pos, stream, PREC_SUFFIX);
1189 fputs_filtered (",", stream);
1190 print_subexp (exp, pos, stream, PREC_SUFFIX);
1191 fputs_filtered (")", stream);
1192 }
1193
1194 /* Special expression printing for Fortran. */
1195
1196 static void
1197 print_subexp_f (struct expression *exp, int *pos,
1198 struct ui_file *stream, enum precedence prec)
1199 {
1200 int pc = *pos;
1201 enum exp_opcode op = exp->elts[pc].opcode;
1202
1203 switch (op)
1204 {
1205 default:
1206 print_subexp_standard (exp, pos, stream, prec);
1207 return;
1208
1209 case UNOP_FORTRAN_KIND:
1210 print_unop_subexp_f (exp, pos, stream, prec, "KIND");
1211 return;
1212
1213 case UNOP_FORTRAN_FLOOR:
1214 print_unop_subexp_f (exp, pos, stream, prec, "FLOOR");
1215 return;
1216
1217 case UNOP_FORTRAN_CEILING:
1218 print_unop_subexp_f (exp, pos, stream, prec, "CEILING");
1219 return;
1220
1221 case UNOP_FORTRAN_ALLOCATED:
1222 print_unop_subexp_f (exp, pos, stream, prec, "ALLOCATED");
1223 return;
1224
1225 case BINOP_FORTRAN_CMPLX:
1226 print_binop_subexp_f (exp, pos, stream, prec, "CMPLX");
1227 return;
1228
1229 case BINOP_FORTRAN_MODULO:
1230 print_binop_subexp_f (exp, pos, stream, prec, "MODULO");
1231 return;
1232
1233 case FORTRAN_LBOUND:
1234 case FORTRAN_UBOUND:
1235 {
1236 unsigned nargs = longest_to_int (exp->elts[*pos + 1].longconst);
1237 (*pos) += 3;
1238 fprintf_filtered (stream, "%s (",
1239 ((op == FORTRAN_LBOUND) ? "LBOUND" : "UBOUND"));
1240 for (unsigned tem = 0; tem < nargs; tem++)
1241 {
1242 if (tem != 0)
1243 fputs_filtered (", ", stream);
1244 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
1245 }
1246 fputs_filtered (")", stream);
1247 return;
1248 }
1249
1250 case OP_F77_UNDETERMINED_ARGLIST:
1251 (*pos)++;
1252 print_subexp_funcall (exp, pos, stream);
1253 return;
1254 }
1255 }
1256
1257 /* Special expression dumping for Fortran. */
1258
1259 static int
1260 dump_subexp_body_f (struct expression *exp,
1261 struct ui_file *stream, int elt)
1262 {
1263 int opcode = exp->elts[elt].opcode;
1264 int oplen, nargs, i;
1265
1266 switch (opcode)
1267 {
1268 default:
1269 return dump_subexp_body_standard (exp, stream, elt);
1270
1271 case UNOP_FORTRAN_KIND:
1272 case UNOP_FORTRAN_FLOOR:
1273 case UNOP_FORTRAN_CEILING:
1274 case UNOP_FORTRAN_ALLOCATED:
1275 case BINOP_FORTRAN_CMPLX:
1276 case BINOP_FORTRAN_MODULO:
1277 operator_length_f (exp, (elt + 1), &oplen, &nargs);
1278 break;
1279
1280 case FORTRAN_LBOUND:
1281 case FORTRAN_UBOUND:
1282 operator_length_f (exp, (elt + 3), &oplen, &nargs);
1283 break;
1284
1285 case OP_F77_UNDETERMINED_ARGLIST:
1286 return dump_subexp_body_funcall (exp, stream, elt + 1);
1287 }
1288
1289 elt += oplen;
1290 for (i = 0; i < nargs; i += 1)
1291 elt = dump_subexp (exp, stream, elt);
1292
1293 return elt;
1294 }
1295
1296 /* Special expression checking for Fortran. */
1297
1298 static int
1299 operator_check_f (struct expression *exp, int pos,
1300 int (*objfile_func) (struct objfile *objfile,
1301 void *data),
1302 void *data)
1303 {
1304 const union exp_element *const elts = exp->elts;
1305
1306 switch (elts[pos].opcode)
1307 {
1308 case UNOP_FORTRAN_KIND:
1309 case UNOP_FORTRAN_FLOOR:
1310 case UNOP_FORTRAN_CEILING:
1311 case UNOP_FORTRAN_ALLOCATED:
1312 case BINOP_FORTRAN_CMPLX:
1313 case BINOP_FORTRAN_MODULO:
1314 case FORTRAN_LBOUND:
1315 case FORTRAN_UBOUND:
1316 /* Any references to objfiles are held in the arguments to this
1317 expression, not within the expression itself, so no additional
1318 checking is required here, the outer expression iteration code
1319 will take care of checking each argument. */
1320 break;
1321
1322 default:
1323 return operator_check_standard (exp, pos, objfile_func, data);
1324 }
1325
1326 return 0;
1327 }
1328
1329 /* Expression processing for Fortran. */
1330 const struct exp_descriptor f_language::exp_descriptor_tab =
1331 {
1332 print_subexp_f,
1333 operator_length_f,
1334 operator_check_f,
1335 dump_subexp_body_f,
1336 evaluate_subexp_f
1337 };
1338
1339 /* See language.h. */
1340
1341 void
1342 f_language::language_arch_info (struct gdbarch *gdbarch,
1343 struct language_arch_info *lai) const
1344 {
1345 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
1346
1347 /* Helper function to allow shorter lines below. */
1348 auto add = [&] (struct type * t)
1349 {
1350 lai->add_primitive_type (t);
1351 };
1352
1353 add (builtin->builtin_character);
1354 add (builtin->builtin_logical);
1355 add (builtin->builtin_logical_s1);
1356 add (builtin->builtin_logical_s2);
1357 add (builtin->builtin_logical_s8);
1358 add (builtin->builtin_real);
1359 add (builtin->builtin_real_s8);
1360 add (builtin->builtin_real_s16);
1361 add (builtin->builtin_complex_s8);
1362 add (builtin->builtin_complex_s16);
1363 add (builtin->builtin_void);
1364
1365 lai->set_string_char_type (builtin->builtin_character);
1366 lai->set_bool_type (builtin->builtin_logical_s2, "logical");
1367 }
1368
1369 /* See language.h. */
1370
1371 unsigned int
1372 f_language::search_name_hash (const char *name) const
1373 {
1374 return cp_search_name_hash (name);
1375 }
1376
1377 /* See language.h. */
1378
1379 struct block_symbol
1380 f_language::lookup_symbol_nonlocal (const char *name,
1381 const struct block *block,
1382 const domain_enum domain) const
1383 {
1384 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1385 }
1386
1387 /* See language.h. */
1388
1389 symbol_name_matcher_ftype *
1390 f_language::get_symbol_name_matcher_inner
1391 (const lookup_name_info &lookup_name) const
1392 {
1393 return cp_get_symbol_name_matcher (lookup_name);
1394 }
1395
1396 /* Single instance of the Fortran language class. */
1397
1398 static f_language f_language_defn;
1399
1400 static void *
1401 build_fortran_types (struct gdbarch *gdbarch)
1402 {
1403 struct builtin_f_type *builtin_f_type
1404 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
1405
1406 builtin_f_type->builtin_void
1407 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
1408
1409 builtin_f_type->builtin_character
1410 = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
1411
1412 builtin_f_type->builtin_logical_s1
1413 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
1414
1415 builtin_f_type->builtin_integer_s2
1416 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
1417 "integer*2");
1418
1419 builtin_f_type->builtin_integer_s8
1420 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
1421 "integer*8");
1422
1423 builtin_f_type->builtin_logical_s2
1424 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
1425 "logical*2");
1426
1427 builtin_f_type->builtin_logical_s8
1428 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
1429 "logical*8");
1430
1431 builtin_f_type->builtin_integer
1432 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
1433 "integer");
1434
1435 builtin_f_type->builtin_logical
1436 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
1437 "logical*4");
1438
1439 builtin_f_type->builtin_real
1440 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
1441 "real", gdbarch_float_format (gdbarch));
1442 builtin_f_type->builtin_real_s8
1443 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
1444 "real*8", gdbarch_double_format (gdbarch));
1445 auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
1446 if (fmt != nullptr)
1447 builtin_f_type->builtin_real_s16
1448 = arch_float_type (gdbarch, 128, "real*16", fmt);
1449 else if (gdbarch_long_double_bit (gdbarch) == 128)
1450 builtin_f_type->builtin_real_s16
1451 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
1452 "real*16", gdbarch_long_double_format (gdbarch));
1453 else
1454 builtin_f_type->builtin_real_s16
1455 = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
1456
1457 builtin_f_type->builtin_complex_s8
1458 = init_complex_type ("complex*8", builtin_f_type->builtin_real);
1459 builtin_f_type->builtin_complex_s16
1460 = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
1461
1462 if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
1463 builtin_f_type->builtin_complex_s32
1464 = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
1465 else
1466 builtin_f_type->builtin_complex_s32
1467 = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
1468
1469 return builtin_f_type;
1470 }
1471
1472 static struct gdbarch_data *f_type_data;
1473
1474 const struct builtin_f_type *
1475 builtin_f_type (struct gdbarch *gdbarch)
1476 {
1477 return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
1478 }
1479
1480 /* Command-list for the "set/show fortran" prefix command. */
1481 static struct cmd_list_element *set_fortran_list;
1482 static struct cmd_list_element *show_fortran_list;
1483
1484 void _initialize_f_language ();
1485 void
1486 _initialize_f_language ()
1487 {
1488 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
1489
1490 add_basic_prefix_cmd ("fortran", no_class,
1491 _("Prefix command for changing Fortran-specific settings."),
1492 &set_fortran_list, "set fortran ", 0, &setlist);
1493
1494 add_show_prefix_cmd ("fortran", no_class,
1495 _("Generic command for showing Fortran-specific settings."),
1496 &show_fortran_list, "show fortran ", 0, &showlist);
1497
1498 add_setshow_boolean_cmd ("repack-array-slices", class_vars,
1499 &repack_array_slices, _("\
1500 Enable or disable repacking of non-contiguous array slices."), _("\
1501 Show whether non-contiguous array slices are repacked."), _("\
1502 When the user requests a slice of a Fortran array then we can either return\n\
1503 a descriptor that describes the array in place (using the original array data\n\
1504 in its existing location) or the original data can be repacked (copied) to a\n\
1505 new location.\n\
1506 \n\
1507 When the content of the array slice is contiguous within the original array\n\
1508 then the result will never be repacked, but when the data for the new array\n\
1509 is non-contiguous within the original array repacking will only be performed\n\
1510 when this setting is on."),
1511 NULL,
1512 show_repack_array_slices,
1513 &set_fortran_list, &show_fortran_list);
1514
1515 /* Debug Fortran's array slicing logic. */
1516 add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
1517 &fortran_array_slicing_debug, _("\
1518 Set debugging of Fortran array slicing."), _("\
1519 Show debugging of Fortran array slicing."), _("\
1520 When on, debugging of Fortran array slicing is enabled."),
1521 NULL,
1522 show_fortran_array_slicing_debug,
1523 &setdebuglist, &showdebuglist);
1524 }
1525
1526 /* Ensures that function argument VALUE is in the appropriate form to
1527 pass to a Fortran function. Returns a possibly new value that should
1528 be used instead of VALUE.
1529
1530 When IS_ARTIFICIAL is true this indicates an artificial argument,
1531 e.g. hidden string lengths which the GNU Fortran argument passing
1532 convention specifies as being passed by value.
1533
1534 When IS_ARTIFICIAL is false, the argument is passed by pointer. If the
1535 value is already in target memory then return a value that is a pointer
1536 to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate
1537 space in the target, copy VALUE in, and return a pointer to the in
1538 memory copy. */
1539
1540 static struct value *
1541 fortran_argument_convert (struct value *value, bool is_artificial)
1542 {
1543 if (!is_artificial)
1544 {
1545 /* If the value is not in the inferior e.g. registers values,
1546 convenience variables and user input. */
1547 if (VALUE_LVAL (value) != lval_memory)
1548 {
1549 struct type *type = value_type (value);
1550 const int length = TYPE_LENGTH (type);
1551 const CORE_ADDR addr
1552 = value_as_long (value_allocate_space_in_inferior (length));
1553 write_memory (addr, value_contents (value), length);
1554 struct value *val
1555 = value_from_contents_and_address (type, value_contents (value),
1556 addr);
1557 return value_addr (val);
1558 }
1559 else
1560 return value_addr (value); /* Program variables, e.g. arrays. */
1561 }
1562 return value;
1563 }
1564
1565 /* See f-lang.h. */
1566
1567 struct type *
1568 fortran_preserve_arg_pointer (struct value *arg, struct type *type)
1569 {
1570 if (value_type (arg)->code () == TYPE_CODE_PTR)
1571 return value_type (arg);
1572 return type;
1573 }
1574
1575 /* See f-lang.h. */
1576
1577 CORE_ADDR
1578 fortran_adjust_dynamic_array_base_address_hack (struct type *type,
1579 CORE_ADDR address)
1580 {
1581 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1582
1583 /* We can't adjust the base address for arrays that have no content. */
1584 if (type_not_allocated (type) || type_not_associated (type))
1585 return address;
1586
1587 int ndimensions = calc_f77_array_dims (type);
1588 LONGEST total_offset = 0;
1589
1590 /* Walk through each of the dimensions of this array type and figure out
1591 if any of the dimensions are "backwards", that is the base address
1592 for this dimension points to the element at the highest memory
1593 address and the stride is negative. */
1594 struct type *tmp_type = type;
1595 for (int i = 0 ; i < ndimensions; ++i)
1596 {
1597 /* Grab the range for this dimension and extract the lower and upper
1598 bounds. */
1599 tmp_type = check_typedef (tmp_type);
1600 struct type *range_type = tmp_type->index_type ();
1601 LONGEST lowerbound, upperbound, stride;
1602 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
1603 error ("failed to get range bounds");
1604
1605 /* Figure out the stride for this dimension. */
1606 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
1607 stride = tmp_type->index_type ()->bounds ()->bit_stride ();
1608 if (stride == 0)
1609 stride = type_length_units (elt_type);
1610 else
1611 {
1612 int unit_size
1613 = gdbarch_addressable_memory_unit_size (elt_type->arch ());
1614 stride /= (unit_size * 8);
1615 }
1616
1617 /* If this dimension is "backward" then figure out the offset
1618 adjustment required to point to the element at the lowest memory
1619 address, and add this to the total offset. */
1620 LONGEST offset = 0;
1621 if (stride < 0 && lowerbound < upperbound)
1622 offset = (upperbound - lowerbound) * stride;
1623 total_offset += offset;
1624 tmp_type = TYPE_TARGET_TYPE (tmp_type);
1625 }
1626
1627 /* Adjust the address of this object and return it. */
1628 address += total_offset;
1629 return address;
1630 }
This page took 0.096495 seconds and 5 git commands to generate.