Bump version to 12.0.50.DATE-git.
[binutils-gdb.git] / gdb / f-lang.c
blob16ec9e04044b899b59560e9cdc72a15441f98372
1 /* Fortran language support routines for GDB, the GNU debugger.
3 Copyright (C) 1993-2021 Free Software Foundation, Inc.
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
8 This file is part of GDB.
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.
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.
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/>. */
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 #include "f-exp.h"
43 #include <math.h>
45 /* Whether GDB should repack array slices created by the user. */
46 static bool repack_array_slices = false;
48 /* Implement 'show fortran repack-array-slices'. */
49 static void
50 show_repack_array_slices (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
53 fprintf_filtered (file, _("Repacking of Fortran array slices is %s.\n"),
54 value);
57 /* Debugging of Fortran's array slicing. */
58 static bool fortran_array_slicing_debug = false;
60 /* Implement 'show debug fortran-array-slicing'. */
61 static void
62 show_fortran_array_slicing_debug (struct ui_file *file, int from_tty,
63 struct cmd_list_element *c,
64 const char *value)
66 fprintf_filtered (file, _("Debugging of Fortran array slicing is %s.\n"),
67 value);
70 /* Local functions */
72 static value *fortran_prepare_argument (struct expression *exp,
73 expr::operation *subexp,
74 int arg_num, bool is_internal_call_p,
75 struct type *func_type, enum noside noside);
77 /* Return the encoding that should be used for the character type
78 TYPE. */
80 const char *
81 f_language::get_encoding (struct type *type)
83 const char *encoding;
85 switch (TYPE_LENGTH (type))
87 case 1:
88 encoding = target_charset (type->arch ());
89 break;
90 case 4:
91 if (type_byte_order (type) == BFD_ENDIAN_BIG)
92 encoding = "UTF-32BE";
93 else
94 encoding = "UTF-32LE";
95 break;
97 default:
98 error (_("unrecognized character type"));
101 return encoding;
106 /* A helper function for the "bound" intrinsics that checks that TYPE
107 is an array. LBOUND_P is true for lower bound; this is used for
108 the error message, if any. */
110 static void
111 fortran_require_array (struct type *type, bool lbound_p)
113 type = check_typedef (type);
114 if (type->code () != TYPE_CODE_ARRAY)
116 if (lbound_p)
117 error (_("LBOUND can only be applied to arrays"));
118 else
119 error (_("UBOUND can only be applied to arrays"));
123 /* Create an array containing the lower bounds (when LBOUND_P is true) or
124 the upper bounds (when LBOUND_P is false) of ARRAY (which must be of
125 array type). GDBARCH is the current architecture. */
127 static struct value *
128 fortran_bounds_all_dims (bool lbound_p,
129 struct gdbarch *gdbarch,
130 struct value *array)
132 type *array_type = check_typedef (value_type (array));
133 int ndimensions = calc_f77_array_dims (array_type);
135 /* Allocate a result value of the correct type. */
136 struct type *range
137 = create_static_range_type (nullptr,
138 builtin_type (gdbarch)->builtin_int,
139 1, ndimensions);
140 struct type *elm_type = builtin_type (gdbarch)->builtin_long_long;
141 struct type *result_type = create_array_type (nullptr, elm_type, range);
142 struct value *result = allocate_value (result_type);
144 /* Walk the array dimensions backwards due to the way the array will be
145 laid out in memory, the first dimension will be the most inner. */
146 LONGEST elm_len = TYPE_LENGTH (elm_type);
147 for (LONGEST dst_offset = elm_len * (ndimensions - 1);
148 dst_offset >= 0;
149 dst_offset -= elm_len)
151 LONGEST b;
153 /* Grab the required bound. */
154 if (lbound_p)
155 b = f77_get_lowerbound (array_type);
156 else
157 b = f77_get_upperbound (array_type);
159 /* And copy the value into the result value. */
160 struct value *v = value_from_longest (elm_type, b);
161 gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
162 <= TYPE_LENGTH (value_type (result)));
163 gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
164 value_contents_copy (result, dst_offset, v, 0, elm_len);
166 /* Peel another dimension of the array. */
167 array_type = TYPE_TARGET_TYPE (array_type);
170 return result;
173 /* Return the lower bound (when LBOUND_P is true) or the upper bound (when
174 LBOUND_P is false) for dimension DIM_VAL (which must be an integer) of
175 ARRAY (which must be an array). GDBARCH is the current architecture. */
177 static struct value *
178 fortran_bounds_for_dimension (bool lbound_p,
179 struct gdbarch *gdbarch,
180 struct value *array,
181 struct value *dim_val)
183 /* Check the requested dimension is valid for this array. */
184 type *array_type = check_typedef (value_type (array));
185 int ndimensions = calc_f77_array_dims (array_type);
186 long dim = value_as_long (dim_val);
187 if (dim < 1 || dim > ndimensions)
189 if (lbound_p)
190 error (_("LBOUND dimension must be from 1 to %d"), ndimensions);
191 else
192 error (_("UBOUND dimension must be from 1 to %d"), ndimensions);
195 /* The type for the result. */
196 struct type *bound_type = builtin_type (gdbarch)->builtin_long_long;
198 /* Walk the dimensions backwards, due to the ordering in which arrays are
199 laid out the first dimension is the most inner. */
200 for (int i = ndimensions - 1; i >= 0; --i)
202 /* If this is the requested dimension then we're done. Grab the
203 bounds and return. */
204 if (i == dim - 1)
206 LONGEST b;
208 if (lbound_p)
209 b = f77_get_lowerbound (array_type);
210 else
211 b = f77_get_upperbound (array_type);
213 return value_from_longest (bound_type, b);
216 /* Peel off another dimension of the array. */
217 array_type = TYPE_TARGET_TYPE (array_type);
220 gdb_assert_not_reached ("failed to find matching dimension");
224 /* Return the number of dimensions for a Fortran array or string. */
227 calc_f77_array_dims (struct type *array_type)
229 int ndimen = 1;
230 struct type *tmp_type;
232 if ((array_type->code () == TYPE_CODE_STRING))
233 return 1;
235 if ((array_type->code () != TYPE_CODE_ARRAY))
236 error (_("Can't get dimensions for a non-array type"));
238 tmp_type = array_type;
240 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
242 if (tmp_type->code () == TYPE_CODE_ARRAY)
243 ++ndimen;
245 return ndimen;
248 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
249 slices. This is a base class for two alternative repacking mechanisms,
250 one for when repacking from a lazy value, and one for repacking from a
251 non-lazy (already loaded) value. */
252 class fortran_array_repacker_base_impl
253 : public fortran_array_walker_base_impl
255 public:
256 /* Constructor, DEST is the value we are repacking into. */
257 fortran_array_repacker_base_impl (struct value *dest)
258 : m_dest (dest),
259 m_dest_offset (0)
260 { /* Nothing. */ }
262 /* When we start processing the inner most dimension, this is where we
263 will be creating values for each element as we load them and then copy
264 them into the M_DEST value. Set a value mark so we can free these
265 temporary values. */
266 void start_dimension (bool inner_p)
268 if (inner_p)
270 gdb_assert (m_mark == nullptr);
271 m_mark = value_mark ();
275 /* When we finish processing the inner most dimension free all temporary
276 value that were created. */
277 void finish_dimension (bool inner_p, bool last_p)
279 if (inner_p)
281 gdb_assert (m_mark != nullptr);
282 value_free_to_mark (m_mark);
283 m_mark = nullptr;
287 protected:
288 /* Copy the contents of array element ELT into M_DEST at the next
289 available offset. */
290 void copy_element_to_dest (struct value *elt)
292 value_contents_copy (m_dest, m_dest_offset, elt, 0,
293 TYPE_LENGTH (value_type (elt)));
294 m_dest_offset += TYPE_LENGTH (value_type (elt));
297 /* The value being written to. */
298 struct value *m_dest;
300 /* The byte offset in M_DEST at which the next element should be
301 written. */
302 LONGEST m_dest_offset;
304 /* Set with a call to VALUE_MARK, and then reset after calling
305 VALUE_FREE_TO_MARK. */
306 struct value *m_mark = nullptr;
309 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
310 slices. This class is specialised for repacking an array slice from a
311 lazy array value, as such it does not require the parent array value to
312 be loaded into GDB's memory; the parent value could be huge, while the
313 slice could be tiny. */
314 class fortran_lazy_array_repacker_impl
315 : public fortran_array_repacker_base_impl
317 public:
318 /* Constructor. TYPE is the type of the slice being loaded from the
319 parent value, so this type will correctly reflect the strides required
320 to find all of the elements from the parent value. ADDRESS is the
321 address in target memory of value matching TYPE, and DEST is the value
322 we are repacking into. */
323 explicit fortran_lazy_array_repacker_impl (struct type *type,
324 CORE_ADDR address,
325 struct value *dest)
326 : fortran_array_repacker_base_impl (dest),
327 m_addr (address)
328 { /* Nothing. */ }
330 /* Create a lazy value in target memory representing a single element,
331 then load the element into GDB's memory and copy the contents into the
332 destination value. */
333 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
335 copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
338 private:
339 /* The address in target memory where the parent value starts. */
340 CORE_ADDR m_addr;
343 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
344 slices. This class is specialised for repacking an array slice from a
345 previously loaded (non-lazy) array value, as such it fetches the
346 element values from the contents of the parent value. */
347 class fortran_array_repacker_impl
348 : public fortran_array_repacker_base_impl
350 public:
351 /* Constructor. TYPE is the type for the array slice within the parent
352 value, as such it has stride values as required to find the elements
353 within the original parent value. ADDRESS is the address in target
354 memory of the value matching TYPE. BASE_OFFSET is the offset from
355 the start of VAL's content buffer to the start of the object of TYPE,
356 VAL is the parent object from which we are loading the value, and
357 DEST is the value into which we are repacking. */
358 explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
359 LONGEST base_offset,
360 struct value *val, struct value *dest)
361 : fortran_array_repacker_base_impl (dest),
362 m_base_offset (base_offset),
363 m_val (val)
365 gdb_assert (!value_lazy (val));
368 /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
369 from the content buffer of M_VAL then copy this extracted value into
370 the repacked destination value. */
371 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
373 struct value *elt
374 = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
375 copy_element_to_dest (elt);
378 private:
379 /* The offset into the content buffer of M_VAL to the start of the slice
380 being extracted. */
381 LONGEST m_base_offset;
383 /* The parent value from which we are extracting a slice. */
384 struct value *m_val;
388 /* Evaluate FORTRAN_ASSOCIATED expressions. Both GDBARCH and LANG are
389 extracted from the expression being evaluated. POINTER is the required
390 first argument to the 'associated' keyword, and TARGET is the optional
391 second argument, this will be nullptr if the user only passed one
392 argument to their use of 'associated'. */
394 static struct value *
395 fortran_associated (struct gdbarch *gdbarch, const language_defn *lang,
396 struct value *pointer, struct value *target = nullptr)
398 struct type *result_type = language_bool_type (lang, gdbarch);
400 /* All Fortran pointers should have the associated property, this is
401 how we know the pointer is pointing at something or not. */
402 struct type *pointer_type = check_typedef (value_type (pointer));
403 if (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
404 && pointer_type->code () != TYPE_CODE_PTR)
405 error (_("ASSOCIATED can only be applied to pointers"));
407 /* Get an address from POINTER. Fortran (or at least gfortran) models
408 array pointers as arrays with a dynamic data address, so we need to
409 use two approaches here, for real pointers we take the contents of the
410 pointer as an address. For non-pointers we take the address of the
411 content. */
412 CORE_ADDR pointer_addr;
413 if (pointer_type->code () == TYPE_CODE_PTR)
414 pointer_addr = value_as_address (pointer);
415 else
416 pointer_addr = value_address (pointer);
418 /* The single argument case, is POINTER associated with anything? */
419 if (target == nullptr)
421 bool is_associated = false;
423 /* If POINTER is an actual pointer and doesn't have an associated
424 property then we need to figure out whether this pointer is
425 associated by looking at the value of the pointer itself. We make
426 the assumption that a non-associated pointer will be set to 0.
427 This is probably true for most targets, but might not be true for
428 everyone. */
429 if (pointer_type->code () == TYPE_CODE_PTR
430 && TYPE_ASSOCIATED_PROP (pointer_type) == nullptr)
431 is_associated = (pointer_addr != 0);
432 else
433 is_associated = !type_not_associated (pointer_type);
434 return value_from_longest (result_type, is_associated ? 1 : 0);
437 /* The two argument case, is POINTER associated with TARGET? */
439 struct type *target_type = check_typedef (value_type (target));
441 struct type *pointer_target_type;
442 if (pointer_type->code () == TYPE_CODE_PTR)
443 pointer_target_type = TYPE_TARGET_TYPE (pointer_type);
444 else
445 pointer_target_type = pointer_type;
447 struct type *target_target_type;
448 if (target_type->code () == TYPE_CODE_PTR)
449 target_target_type = TYPE_TARGET_TYPE (target_type);
450 else
451 target_target_type = target_type;
453 if (pointer_target_type->code () != target_target_type->code ()
454 || (pointer_target_type->code () != TYPE_CODE_ARRAY
455 && (TYPE_LENGTH (pointer_target_type)
456 != TYPE_LENGTH (target_target_type))))
457 error (_("arguments to associated must be of same type and kind"));
459 /* If TARGET is not in memory, or the original pointer is specifically
460 known to be not associated with anything, then the answer is obviously
461 false. Alternatively, if POINTER is an actual pointer and has no
462 associated property, then we have to check if its associated by
463 looking the value of the pointer itself. We make the assumption that
464 a non-associated pointer will be set to 0. This is probably true for
465 most targets, but might not be true for everyone. */
466 if (value_lval_const (target) != lval_memory
467 || type_not_associated (pointer_type)
468 || (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
469 && pointer_type->code () == TYPE_CODE_PTR
470 && pointer_addr == 0))
471 return value_from_longest (result_type, 0);
473 /* See the comment for POINTER_ADDR above. */
474 CORE_ADDR target_addr;
475 if (target_type->code () == TYPE_CODE_PTR)
476 target_addr = value_as_address (target);
477 else
478 target_addr = value_address (target);
480 /* Wrap the following checks inside a do { ... } while (false) loop so
481 that we can use `break' to jump out of the loop. */
482 bool is_associated = false;
485 /* If the addresses are different then POINTER is definitely not
486 pointing at TARGET. */
487 if (pointer_addr != target_addr)
488 break;
490 /* If POINTER is a real pointer (i.e. not an array pointer, which are
491 implemented as arrays with a dynamic content address), then this
492 is all the checking that is needed. */
493 if (pointer_type->code () == TYPE_CODE_PTR)
495 is_associated = true;
496 break;
499 /* We have an array pointer. Check the number of dimensions. */
500 int pointer_dims = calc_f77_array_dims (pointer_type);
501 int target_dims = calc_f77_array_dims (target_type);
502 if (pointer_dims != target_dims)
503 break;
505 /* Now check that every dimension has the same upper bound, lower
506 bound, and stride value. */
507 int dim = 0;
508 while (dim < pointer_dims)
510 LONGEST pointer_lowerbound, pointer_upperbound, pointer_stride;
511 LONGEST target_lowerbound, target_upperbound, target_stride;
513 pointer_type = check_typedef (pointer_type);
514 target_type = check_typedef (target_type);
516 struct type *pointer_range = pointer_type->index_type ();
517 struct type *target_range = target_type->index_type ();
519 if (!get_discrete_bounds (pointer_range, &pointer_lowerbound,
520 &pointer_upperbound))
521 break;
523 if (!get_discrete_bounds (target_range, &target_lowerbound,
524 &target_upperbound))
525 break;
527 if (pointer_lowerbound != target_lowerbound
528 || pointer_upperbound != target_upperbound)
529 break;
531 /* Figure out the stride (in bits) for both pointer and target.
532 If either doesn't have a stride then we take the element size,
533 but we need to convert to bits (hence the * 8). */
534 pointer_stride = pointer_range->bounds ()->bit_stride ();
535 if (pointer_stride == 0)
536 pointer_stride
537 = type_length_units (check_typedef
538 (TYPE_TARGET_TYPE (pointer_type))) * 8;
539 target_stride = target_range->bounds ()->bit_stride ();
540 if (target_stride == 0)
541 target_stride
542 = type_length_units (check_typedef
543 (TYPE_TARGET_TYPE (target_type))) * 8;
544 if (pointer_stride != target_stride)
545 break;
547 ++dim;
550 if (dim < pointer_dims)
551 break;
553 is_associated = true;
555 while (false);
557 return value_from_longest (result_type, is_associated ? 1 : 0);
560 struct value *
561 eval_op_f_associated (struct type *expect_type,
562 struct expression *exp,
563 enum noside noside,
564 enum exp_opcode opcode,
565 struct value *arg1)
567 return fortran_associated (exp->gdbarch, exp->language_defn, arg1);
570 struct value *
571 eval_op_f_associated (struct type *expect_type,
572 struct expression *exp,
573 enum noside noside,
574 enum exp_opcode opcode,
575 struct value *arg1,
576 struct value *arg2)
578 return fortran_associated (exp->gdbarch, exp->language_defn, arg1, arg2);
581 /* Implement FORTRAN_ARRAY_SIZE expression, this corresponds to the 'SIZE'
582 keyword. Both GDBARCH and LANG are extracted from the expression being
583 evaluated. ARRAY is the value that should be an array, though this will
584 not have been checked before calling this function. DIM is optional, if
585 present then it should be an integer identifying a dimension of the
586 array to ask about. As with ARRAY the validity of DIM is not checked
587 before calling this function.
589 Return either the total number of elements in ARRAY (when DIM is
590 nullptr), or the number of elements in dimension DIM. */
592 static struct value *
593 fortran_array_size (struct gdbarch *gdbarch, const language_defn *lang,
594 struct value *array, struct value *dim_val = nullptr)
596 /* Check that ARRAY is the correct type. */
597 struct type *array_type = check_typedef (value_type (array));
598 if (array_type->code () != TYPE_CODE_ARRAY)
599 error (_("SIZE can only be applied to arrays"));
600 if (type_not_allocated (array_type) || type_not_associated (array_type))
601 error (_("SIZE can only be used on allocated/associated arrays"));
603 int ndimensions = calc_f77_array_dims (array_type);
604 int dim = -1;
605 LONGEST result = 0;
607 if (dim_val != nullptr)
609 if (check_typedef (value_type (dim_val))->code () != TYPE_CODE_INT)
610 error (_("DIM argument to SIZE must be an integer"));
611 dim = (int) value_as_long (dim_val);
613 if (dim < 1 || dim > ndimensions)
614 error (_("DIM argument to SIZE must be between 1 and %d"),
615 ndimensions);
618 /* Now walk over all the dimensions of the array totalling up the
619 elements in each dimension. */
620 for (int i = ndimensions - 1; i >= 0; --i)
622 /* If this is the requested dimension then we're done. Grab the
623 bounds and return. */
624 if (i == dim - 1 || dim == -1)
626 LONGEST lbound, ubound;
627 struct type *range = array_type->index_type ();
629 if (!get_discrete_bounds (range, &lbound, &ubound))
630 error (_("failed to find array bounds"));
632 LONGEST dim_size = (ubound - lbound + 1);
633 if (result == 0)
634 result = dim_size;
635 else
636 result *= dim_size;
638 if (dim != -1)
639 break;
642 /* Peel off another dimension of the array. */
643 array_type = TYPE_TARGET_TYPE (array_type);
646 struct type *result_type
647 = builtin_f_type (gdbarch)->builtin_integer;
648 return value_from_longest (result_type, result);
651 /* See f-exp.h. */
653 struct value *
654 eval_op_f_array_size (struct type *expect_type,
655 struct expression *exp,
656 enum noside noside,
657 enum exp_opcode opcode,
658 struct value *arg1)
660 gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
661 return fortran_array_size (exp->gdbarch, exp->language_defn, arg1);
664 /* See f-exp.h. */
666 struct value *
667 eval_op_f_array_size (struct type *expect_type,
668 struct expression *exp,
669 enum noside noside,
670 enum exp_opcode opcode,
671 struct value *arg1,
672 struct value *arg2)
674 gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
675 return fortran_array_size (exp->gdbarch, exp->language_defn, arg1, arg2);
678 /* Implement UNOP_FORTRAN_SHAPE expression. Both GDBARCH and LANG are
679 extracted from the expression being evaluated. VAL is the value on
680 which 'shape' was used, this can be any type.
682 Return an array of integers. If VAL is not an array then the returned
683 array should have zero elements. If VAL is an array then the returned
684 array should have one element per dimension, with the element
685 containing the extent of that dimension from VAL. */
687 static struct value *
688 fortran_array_shape (struct gdbarch *gdbarch, const language_defn *lang,
689 struct value *val)
691 struct type *val_type = check_typedef (value_type (val));
693 /* If we are passed an array that is either not allocated, or not
694 associated, then this is explicitly not allowed according to the
695 Fortran specification. */
696 if (val_type->code () == TYPE_CODE_ARRAY
697 && (type_not_associated (val_type) || type_not_allocated (val_type)))
698 error (_("The array passed to SHAPE must be allocated or associated"));
700 /* The Fortran specification allows non-array types to be passed to this
701 function, in which case we get back an empty array.
703 Calculate the number of dimensions for the resulting array. */
704 int ndimensions = 0;
705 if (val_type->code () == TYPE_CODE_ARRAY)
706 ndimensions = calc_f77_array_dims (val_type);
708 /* Allocate a result value of the correct type. */
709 struct type *range
710 = create_static_range_type (nullptr,
711 builtin_type (gdbarch)->builtin_int,
712 1, ndimensions);
713 struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
714 struct type *result_type = create_array_type (nullptr, elm_type, range);
715 struct value *result = allocate_value (result_type);
716 LONGEST elm_len = TYPE_LENGTH (elm_type);
718 /* Walk the array dimensions backwards due to the way the array will be
719 laid out in memory, the first dimension will be the most inner.
721 If VAL was not an array then ndimensions will be 0, in which case we
722 will never go around this loop. */
723 for (LONGEST dst_offset = elm_len * (ndimensions - 1);
724 dst_offset >= 0;
725 dst_offset -= elm_len)
727 LONGEST lbound, ubound;
729 if (!get_discrete_bounds (val_type->index_type (), &lbound, &ubound))
730 error (_("failed to find array bounds"));
732 LONGEST dim_size = (ubound - lbound + 1);
734 /* And copy the value into the result value. */
735 struct value *v = value_from_longest (elm_type, dim_size);
736 gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
737 <= TYPE_LENGTH (value_type (result)));
738 gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
739 value_contents_copy (result, dst_offset, v, 0, elm_len);
741 /* Peel another dimension of the array. */
742 val_type = TYPE_TARGET_TYPE (val_type);
745 return result;
748 /* See f-exp.h. */
750 struct value *
751 eval_op_f_array_shape (struct type *expect_type, struct expression *exp,
752 enum noside noside, enum exp_opcode opcode,
753 struct value *arg1)
755 gdb_assert (opcode == UNOP_FORTRAN_SHAPE);
756 return fortran_array_shape (exp->gdbarch, exp->language_defn, arg1);
759 /* A helper function for UNOP_ABS. */
761 struct value *
762 eval_op_f_abs (struct type *expect_type, struct expression *exp,
763 enum noside noside,
764 enum exp_opcode opcode,
765 struct value *arg1)
767 struct type *type = value_type (arg1);
768 switch (type->code ())
770 case TYPE_CODE_FLT:
772 double d
773 = fabs (target_float_to_host_double (value_contents (arg1),
774 value_type (arg1)));
775 return value_from_host_double (type, d);
777 case TYPE_CODE_INT:
779 LONGEST l = value_as_long (arg1);
780 l = llabs (l);
781 return value_from_longest (type, l);
784 error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
787 /* A helper function for BINOP_MOD. */
789 struct value *
790 eval_op_f_mod (struct type *expect_type, struct expression *exp,
791 enum noside noside,
792 enum exp_opcode opcode,
793 struct value *arg1, struct value *arg2)
795 struct type *type = value_type (arg1);
796 if (type->code () != value_type (arg2)->code ())
797 error (_("non-matching types for parameters to MOD ()"));
798 switch (type->code ())
800 case TYPE_CODE_FLT:
802 double d1
803 = target_float_to_host_double (value_contents (arg1),
804 value_type (arg1));
805 double d2
806 = target_float_to_host_double (value_contents (arg2),
807 value_type (arg2));
808 double d3 = fmod (d1, d2);
809 return value_from_host_double (type, d3);
811 case TYPE_CODE_INT:
813 LONGEST v1 = value_as_long (arg1);
814 LONGEST v2 = value_as_long (arg2);
815 if (v2 == 0)
816 error (_("calling MOD (N, 0) is undefined"));
817 LONGEST v3 = v1 - (v1 / v2) * v2;
818 return value_from_longest (value_type (arg1), v3);
821 error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
824 /* A helper function for UNOP_FORTRAN_CEILING. */
826 struct value *
827 eval_op_f_ceil (struct type *expect_type, struct expression *exp,
828 enum noside noside,
829 enum exp_opcode opcode,
830 struct value *arg1)
832 struct type *type = value_type (arg1);
833 if (type->code () != TYPE_CODE_FLT)
834 error (_("argument to CEILING must be of type float"));
835 double val
836 = target_float_to_host_double (value_contents (arg1),
837 value_type (arg1));
838 val = ceil (val);
839 return value_from_host_double (type, val);
842 /* A helper function for UNOP_FORTRAN_FLOOR. */
844 struct value *
845 eval_op_f_floor (struct type *expect_type, struct expression *exp,
846 enum noside noside,
847 enum exp_opcode opcode,
848 struct value *arg1)
850 struct type *type = value_type (arg1);
851 if (type->code () != TYPE_CODE_FLT)
852 error (_("argument to FLOOR must be of type float"));
853 double val
854 = target_float_to_host_double (value_contents (arg1),
855 value_type (arg1));
856 val = floor (val);
857 return value_from_host_double (type, val);
860 /* A helper function for BINOP_FORTRAN_MODULO. */
862 struct value *
863 eval_op_f_modulo (struct type *expect_type, struct expression *exp,
864 enum noside noside,
865 enum exp_opcode opcode,
866 struct value *arg1, struct value *arg2)
868 struct type *type = value_type (arg1);
869 if (type->code () != value_type (arg2)->code ())
870 error (_("non-matching types for parameters to MODULO ()"));
871 /* MODULO(A, P) = A - FLOOR (A / P) * P */
872 switch (type->code ())
874 case TYPE_CODE_INT:
876 LONGEST a = value_as_long (arg1);
877 LONGEST p = value_as_long (arg2);
878 LONGEST result = a - (a / p) * p;
879 if (result != 0 && (a < 0) != (p < 0))
880 result += p;
881 return value_from_longest (value_type (arg1), result);
883 case TYPE_CODE_FLT:
885 double a
886 = target_float_to_host_double (value_contents (arg1),
887 value_type (arg1));
888 double p
889 = target_float_to_host_double (value_contents (arg2),
890 value_type (arg2));
891 double result = fmod (a, p);
892 if (result != 0 && (a < 0.0) != (p < 0.0))
893 result += p;
894 return value_from_host_double (type, result);
897 error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
900 /* A helper function for BINOP_FORTRAN_CMPLX. */
902 struct value *
903 eval_op_f_cmplx (struct type *expect_type, struct expression *exp,
904 enum noside noside,
905 enum exp_opcode opcode,
906 struct value *arg1, struct value *arg2)
908 struct type *type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
909 return value_literal_complex (arg1, arg2, type);
912 /* A helper function for UNOP_FORTRAN_KIND. */
914 struct value *
915 eval_op_f_kind (struct type *expect_type, struct expression *exp,
916 enum noside noside,
917 enum exp_opcode opcode,
918 struct value *arg1)
920 struct type *type = value_type (arg1);
922 switch (type->code ())
924 case TYPE_CODE_STRUCT:
925 case TYPE_CODE_UNION:
926 case TYPE_CODE_MODULE:
927 case TYPE_CODE_FUNC:
928 error (_("argument to kind must be an intrinsic type"));
931 if (!TYPE_TARGET_TYPE (type))
932 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
933 TYPE_LENGTH (type));
934 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
935 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
938 /* A helper function for UNOP_FORTRAN_ALLOCATED. */
940 struct value *
941 eval_op_f_allocated (struct type *expect_type, struct expression *exp,
942 enum noside noside, enum exp_opcode op,
943 struct value *arg1)
945 struct type *type = check_typedef (value_type (arg1));
946 if (type->code () != TYPE_CODE_ARRAY)
947 error (_("ALLOCATED can only be applied to arrays"));
948 struct type *result_type
949 = builtin_f_type (exp->gdbarch)->builtin_logical;
950 LONGEST result_value = type_not_allocated (type) ? 0 : 1;
951 return value_from_longest (result_type, result_value);
954 /* See f-exp.h. */
956 struct value *
957 eval_op_f_rank (struct type *expect_type,
958 struct expression *exp,
959 enum noside noside,
960 enum exp_opcode op,
961 struct value *arg1)
963 gdb_assert (op == UNOP_FORTRAN_RANK);
965 struct type *result_type
966 = builtin_f_type (exp->gdbarch)->builtin_integer;
967 struct type *type = check_typedef (value_type (arg1));
968 if (type->code () != TYPE_CODE_ARRAY)
969 return value_from_longest (result_type, 0);
970 LONGEST ndim = calc_f77_array_dims (type);
971 return value_from_longest (result_type, ndim);
974 /* A helper function for UNOP_FORTRAN_LOC. */
976 struct value *
977 eval_op_f_loc (struct type *expect_type, struct expression *exp,
978 enum noside noside, enum exp_opcode op,
979 struct value *arg1)
981 struct type *result_type;
982 if (gdbarch_ptr_bit (exp->gdbarch) == 16)
983 result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s2;
984 else if (gdbarch_ptr_bit (exp->gdbarch) == 32)
985 result_type = builtin_f_type (exp->gdbarch)->builtin_integer;
986 else
987 result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s8;
989 LONGEST result_value = value_address (arg1);
990 return value_from_longest (result_type, result_value);
993 namespace expr
996 /* Called from evaluate to perform array indexing, and sub-range
997 extraction, for Fortran. As well as arrays this function also
998 handles strings as they can be treated like arrays of characters.
999 ARRAY is the array or string being accessed. EXP and NOSIDE are as
1000 for evaluate. */
1002 value *
1003 fortran_undetermined::value_subarray (value *array,
1004 struct expression *exp,
1005 enum noside noside)
1007 type *original_array_type = check_typedef (value_type (array));
1008 bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
1009 const std::vector<operation_up> &ops = std::get<1> (m_storage);
1010 int nargs = ops.size ();
1012 /* Perform checks for ARRAY not being available. The somewhat overly
1013 complex logic here is just to keep backward compatibility with the
1014 errors that we used to get before FORTRAN_VALUE_SUBARRAY was
1015 rewritten. Maybe a future task would streamline the error messages we
1016 get here, and update all the expected test results. */
1017 if (ops[0]->opcode () != OP_RANGE)
1019 if (type_not_associated (original_array_type))
1020 error (_("no such vector element (vector not associated)"));
1021 else if (type_not_allocated (original_array_type))
1022 error (_("no such vector element (vector not allocated)"));
1024 else
1026 if (type_not_associated (original_array_type))
1027 error (_("array not associated"));
1028 else if (type_not_allocated (original_array_type))
1029 error (_("array not allocated"));
1032 /* First check that the number of dimensions in the type we are slicing
1033 matches the number of arguments we were passed. */
1034 int ndimensions = calc_f77_array_dims (original_array_type);
1035 if (nargs != ndimensions)
1036 error (_("Wrong number of subscripts"));
1038 /* This will be initialised below with the type of the elements held in
1039 ARRAY. */
1040 struct type *inner_element_type;
1042 /* Extract the types of each array dimension from the original array
1043 type. We need these available so we can fill in the default upper and
1044 lower bounds if the user requested slice doesn't provide that
1045 information. Additionally unpacking the dimensions like this gives us
1046 the inner element type. */
1047 std::vector<struct type *> dim_types;
1049 dim_types.reserve (ndimensions);
1050 struct type *type = original_array_type;
1051 for (int i = 0; i < ndimensions; ++i)
1053 dim_types.push_back (type);
1054 type = TYPE_TARGET_TYPE (type);
1056 /* TYPE is now the inner element type of the array, we start the new
1057 array slice off as this type, then as we process the requested slice
1058 (from the user) we wrap new types around this to build up the final
1059 slice type. */
1060 inner_element_type = type;
1063 /* As we analyse the new slice type we need to understand if the data
1064 being referenced is contiguous. Do decide this we must track the size
1065 of an element at each dimension of the new slice array. Initially the
1066 elements of the inner most dimension of the array are the same inner
1067 most elements as the original ARRAY. */
1068 LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
1070 /* Start off assuming all data is contiguous, this will be set to false
1071 if access to any dimension results in non-contiguous data. */
1072 bool is_all_contiguous = true;
1074 /* The TOTAL_OFFSET is the distance in bytes from the start of the
1075 original ARRAY to the start of the new slice. This is calculated as
1076 we process the information from the user. */
1077 LONGEST total_offset = 0;
1079 /* A structure representing information about each dimension of the
1080 resulting slice. */
1081 struct slice_dim
1083 /* Constructor. */
1084 slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
1085 : low (l),
1086 high (h),
1087 stride (s),
1088 index (idx)
1089 { /* Nothing. */ }
1091 /* The low bound for this dimension of the slice. */
1092 LONGEST low;
1094 /* The high bound for this dimension of the slice. */
1095 LONGEST high;
1097 /* The byte stride for this dimension of the slice. */
1098 LONGEST stride;
1100 struct type *index;
1103 /* The dimensions of the resulting slice. */
1104 std::vector<slice_dim> slice_dims;
1106 /* Process the incoming arguments. These arguments are in the reverse
1107 order to the array dimensions, that is the first argument refers to
1108 the last array dimension. */
1109 if (fortran_array_slicing_debug)
1110 debug_printf ("Processing array access:\n");
1111 for (int i = 0; i < nargs; ++i)
1113 /* For each dimension of the array the user will have either provided
1114 a ranged access with optional lower bound, upper bound, and
1115 stride, or the user will have supplied a single index. */
1116 struct type *dim_type = dim_types[ndimensions - (i + 1)];
1117 fortran_range_operation *range_op
1118 = dynamic_cast<fortran_range_operation *> (ops[i].get ());
1119 if (range_op != nullptr)
1121 enum range_flag range_flag = range_op->get_flags ();
1123 LONGEST low, high, stride;
1124 low = high = stride = 0;
1126 if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
1127 low = value_as_long (range_op->evaluate0 (exp, noside));
1128 else
1129 low = f77_get_lowerbound (dim_type);
1130 if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
1131 high = value_as_long (range_op->evaluate1 (exp, noside));
1132 else
1133 high = f77_get_upperbound (dim_type);
1134 if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
1135 stride = value_as_long (range_op->evaluate2 (exp, noside));
1136 else
1137 stride = 1;
1139 if (stride == 0)
1140 error (_("stride must not be 0"));
1142 /* Get information about this dimension in the original ARRAY. */
1143 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
1144 struct type *index_type = dim_type->index_type ();
1145 LONGEST lb = f77_get_lowerbound (dim_type);
1146 LONGEST ub = f77_get_upperbound (dim_type);
1147 LONGEST sd = index_type->bit_stride ();
1148 if (sd == 0)
1149 sd = TYPE_LENGTH (target_type) * 8;
1151 if (fortran_array_slicing_debug)
1153 debug_printf ("|-> Range access\n");
1154 std::string str = type_to_string (dim_type);
1155 debug_printf ("| |-> Type: %s\n", str.c_str ());
1156 debug_printf ("| |-> Array:\n");
1157 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
1158 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
1159 debug_printf ("| | |-> Bit stride: %s\n", plongest (sd));
1160 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8));
1161 debug_printf ("| | |-> Type size: %s\n",
1162 pulongest (TYPE_LENGTH (dim_type)));
1163 debug_printf ("| | '-> Target type size: %s\n",
1164 pulongest (TYPE_LENGTH (target_type)));
1165 debug_printf ("| |-> Accessing:\n");
1166 debug_printf ("| | |-> Low bound: %s\n",
1167 plongest (low));
1168 debug_printf ("| | |-> High bound: %s\n",
1169 plongest (high));
1170 debug_printf ("| | '-> Element stride: %s\n",
1171 plongest (stride));
1174 /* Check the user hasn't asked for something invalid. */
1175 if (high > ub || low < lb)
1176 error (_("array subscript out of bounds"));
1178 /* Calculate what this dimension of the new slice array will look
1179 like. OFFSET is the byte offset from the start of the
1180 previous (more outer) dimension to the start of this
1181 dimension. E_COUNT is the number of elements in this
1182 dimension. REMAINDER is the number of elements remaining
1183 between the last included element and the upper bound. For
1184 example an access '1:6:2' will include elements 1, 3, 5 and
1185 have a remainder of 1 (element #6). */
1186 LONGEST lowest = std::min (low, high);
1187 LONGEST offset = (sd / 8) * (lowest - lb);
1188 LONGEST e_count = std::abs (high - low) + 1;
1189 e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
1190 LONGEST new_low = 1;
1191 LONGEST new_high = new_low + e_count - 1;
1192 LONGEST new_stride = (sd * stride) / 8;
1193 LONGEST last_elem = low + ((e_count - 1) * stride);
1194 LONGEST remainder = high - last_elem;
1195 if (low > high)
1197 offset += std::abs (remainder) * TYPE_LENGTH (target_type);
1198 if (stride > 0)
1199 error (_("incorrect stride and boundary combination"));
1201 else if (stride < 0)
1202 error (_("incorrect stride and boundary combination"));
1204 /* Is the data within this dimension contiguous? It is if the
1205 newly computed stride is the same size as a single element of
1206 this dimension. */
1207 bool is_dim_contiguous = (new_stride == slice_element_size);
1208 is_all_contiguous &= is_dim_contiguous;
1210 if (fortran_array_slicing_debug)
1212 debug_printf ("| '-> Results:\n");
1213 debug_printf ("| |-> Offset = %s\n", plongest (offset));
1214 debug_printf ("| |-> Elements = %s\n", plongest (e_count));
1215 debug_printf ("| |-> Low bound = %s\n", plongest (new_low));
1216 debug_printf ("| |-> High bound = %s\n",
1217 plongest (new_high));
1218 debug_printf ("| |-> Byte stride = %s\n",
1219 plongest (new_stride));
1220 debug_printf ("| |-> Last element = %s\n",
1221 plongest (last_elem));
1222 debug_printf ("| |-> Remainder = %s\n",
1223 plongest (remainder));
1224 debug_printf ("| '-> Contiguous = %s\n",
1225 (is_dim_contiguous ? "Yes" : "No"));
1228 /* Figure out how big (in bytes) an element of this dimension of
1229 the new array slice will be. */
1230 slice_element_size = std::abs (new_stride * e_count);
1232 slice_dims.emplace_back (new_low, new_high, new_stride,
1233 index_type);
1235 /* Update the total offset. */
1236 total_offset += offset;
1238 else
1240 /* There is a single index for this dimension. */
1241 LONGEST index
1242 = value_as_long (ops[i]->evaluate_with_coercion (exp, noside));
1244 /* Get information about this dimension in the original ARRAY. */
1245 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
1246 struct type *index_type = dim_type->index_type ();
1247 LONGEST lb = f77_get_lowerbound (dim_type);
1248 LONGEST ub = f77_get_upperbound (dim_type);
1249 LONGEST sd = index_type->bit_stride () / 8;
1250 if (sd == 0)
1251 sd = TYPE_LENGTH (target_type);
1253 if (fortran_array_slicing_debug)
1255 debug_printf ("|-> Index access\n");
1256 std::string str = type_to_string (dim_type);
1257 debug_printf ("| |-> Type: %s\n", str.c_str ());
1258 debug_printf ("| |-> Array:\n");
1259 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
1260 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
1261 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd));
1262 debug_printf ("| | |-> Type size: %s\n",
1263 pulongest (TYPE_LENGTH (dim_type)));
1264 debug_printf ("| | '-> Target type size: %s\n",
1265 pulongest (TYPE_LENGTH (target_type)));
1266 debug_printf ("| '-> Accessing:\n");
1267 debug_printf ("| '-> Index: %s\n",
1268 plongest (index));
1271 /* If the array has actual content then check the index is in
1272 bounds. An array without content (an unbound array) doesn't
1273 have a known upper bound, so don't error check in that
1274 situation. */
1275 if (index < lb
1276 || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
1277 && index > ub)
1278 || (VALUE_LVAL (array) != lval_memory
1279 && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
1281 if (type_not_associated (dim_type))
1282 error (_("no such vector element (vector not associated)"));
1283 else if (type_not_allocated (dim_type))
1284 error (_("no such vector element (vector not allocated)"));
1285 else
1286 error (_("no such vector element"));
1289 /* Calculate using the type stride, not the target type size. */
1290 LONGEST offset = sd * (index - lb);
1291 total_offset += offset;
1295 /* Build a type that represents the new array slice in the target memory
1296 of the original ARRAY, this type makes use of strides to correctly
1297 find only those elements that are part of the new slice. */
1298 struct type *array_slice_type = inner_element_type;
1299 for (const auto &d : slice_dims)
1301 /* Create the range. */
1302 dynamic_prop p_low, p_high, p_stride;
1304 p_low.set_const_val (d.low);
1305 p_high.set_const_val (d.high);
1306 p_stride.set_const_val (d.stride);
1308 struct type *new_range
1309 = create_range_type_with_stride ((struct type *) NULL,
1310 TYPE_TARGET_TYPE (d.index),
1311 &p_low, &p_high, 0, &p_stride,
1312 true);
1313 array_slice_type
1314 = create_array_type (nullptr, array_slice_type, new_range);
1317 if (fortran_array_slicing_debug)
1319 debug_printf ("'-> Final result:\n");
1320 debug_printf (" |-> Type: %s\n",
1321 type_to_string (array_slice_type).c_str ());
1322 debug_printf (" |-> Total offset: %s\n",
1323 plongest (total_offset));
1324 debug_printf (" |-> Base address: %s\n",
1325 core_addr_to_string (value_address (array)));
1326 debug_printf (" '-> Contiguous = %s\n",
1327 (is_all_contiguous ? "Yes" : "No"));
1330 /* Should we repack this array slice? */
1331 if (!is_all_contiguous && (repack_array_slices || is_string_p))
1333 /* Build a type for the repacked slice. */
1334 struct type *repacked_array_type = inner_element_type;
1335 for (const auto &d : slice_dims)
1337 /* Create the range. */
1338 dynamic_prop p_low, p_high, p_stride;
1340 p_low.set_const_val (d.low);
1341 p_high.set_const_val (d.high);
1342 p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
1344 struct type *new_range
1345 = create_range_type_with_stride ((struct type *) NULL,
1346 TYPE_TARGET_TYPE (d.index),
1347 &p_low, &p_high, 0, &p_stride,
1348 true);
1349 repacked_array_type
1350 = create_array_type (nullptr, repacked_array_type, new_range);
1353 /* Now copy the elements from the original ARRAY into the packed
1354 array value DEST. */
1355 struct value *dest = allocate_value (repacked_array_type);
1356 if (value_lazy (array)
1357 || (total_offset + TYPE_LENGTH (array_slice_type)
1358 > TYPE_LENGTH (check_typedef (value_type (array)))))
1360 fortran_array_walker<fortran_lazy_array_repacker_impl> p
1361 (array_slice_type, value_address (array) + total_offset, dest);
1362 p.walk ();
1364 else
1366 fortran_array_walker<fortran_array_repacker_impl> p
1367 (array_slice_type, value_address (array) + total_offset,
1368 total_offset, array, dest);
1369 p.walk ();
1371 array = dest;
1373 else
1375 if (VALUE_LVAL (array) == lval_memory)
1377 /* If the value we're taking a slice from is not yet loaded, or
1378 the requested slice is outside the values content range then
1379 just create a new lazy value pointing at the memory where the
1380 contents we're looking for exist. */
1381 if (value_lazy (array)
1382 || (total_offset + TYPE_LENGTH (array_slice_type)
1383 > TYPE_LENGTH (check_typedef (value_type (array)))))
1384 array = value_at_lazy (array_slice_type,
1385 value_address (array) + total_offset);
1386 else
1387 array = value_from_contents_and_address (array_slice_type,
1388 (value_contents (array)
1389 + total_offset),
1390 (value_address (array)
1391 + total_offset));
1393 else if (!value_lazy (array))
1394 array = value_from_component (array, array_slice_type, total_offset);
1395 else
1396 error (_("cannot subscript arrays that are not in memory"));
1399 return array;
1402 value *
1403 fortran_undetermined::evaluate (struct type *expect_type,
1404 struct expression *exp,
1405 enum noside noside)
1407 value *callee = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1408 if (noside == EVAL_AVOID_SIDE_EFFECTS
1409 && is_dynamic_type (value_type (callee)))
1410 callee = std::get<0> (m_storage)->evaluate (nullptr, exp, EVAL_NORMAL);
1411 struct type *type = check_typedef (value_type (callee));
1412 enum type_code code = type->code ();
1414 if (code == TYPE_CODE_PTR)
1416 /* Fortran always passes variable to subroutines as pointer.
1417 So we need to look into its target type to see if it is
1418 array, string or function. If it is, we need to switch
1419 to the target value the original one points to. */
1420 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1422 if (target_type->code () == TYPE_CODE_ARRAY
1423 || target_type->code () == TYPE_CODE_STRING
1424 || target_type->code () == TYPE_CODE_FUNC)
1426 callee = value_ind (callee);
1427 type = check_typedef (value_type (callee));
1428 code = type->code ();
1432 switch (code)
1434 case TYPE_CODE_ARRAY:
1435 case TYPE_CODE_STRING:
1436 return value_subarray (callee, exp, noside);
1438 case TYPE_CODE_PTR:
1439 case TYPE_CODE_FUNC:
1440 case TYPE_CODE_INTERNAL_FUNCTION:
1442 /* It's a function call. Allocate arg vector, including
1443 space for the function to be called in argvec[0] and a
1444 termination NULL. */
1445 const std::vector<operation_up> &actual (std::get<1> (m_storage));
1446 std::vector<value *> argvec (actual.size ());
1447 bool is_internal_func = (code == TYPE_CODE_INTERNAL_FUNCTION);
1448 for (int tem = 0; tem < argvec.size (); tem++)
1449 argvec[tem] = fortran_prepare_argument (exp, actual[tem].get (),
1450 tem, is_internal_func,
1451 value_type (callee),
1452 noside);
1453 return evaluate_subexp_do_call (exp, noside, callee, argvec,
1454 nullptr, expect_type);
1457 default:
1458 error (_("Cannot perform substring on this type"));
1462 value *
1463 fortran_bound_1arg::evaluate (struct type *expect_type,
1464 struct expression *exp,
1465 enum noside noside)
1467 bool lbound_p = std::get<0> (m_storage) == FORTRAN_LBOUND;
1468 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1469 fortran_require_array (value_type (arg1), lbound_p);
1470 return fortran_bounds_all_dims (lbound_p, exp->gdbarch, arg1);
1473 value *
1474 fortran_bound_2arg::evaluate (struct type *expect_type,
1475 struct expression *exp,
1476 enum noside noside)
1478 bool lbound_p = std::get<0> (m_storage) == FORTRAN_LBOUND;
1479 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1480 fortran_require_array (value_type (arg1), lbound_p);
1482 /* User asked for the bounds of a specific dimension of the array. */
1483 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1484 struct type *type = check_typedef (value_type (arg2));
1485 if (type->code () != TYPE_CODE_INT)
1487 if (lbound_p)
1488 error (_("LBOUND second argument should be an integer"));
1489 else
1490 error (_("UBOUND second argument should be an integer"));
1493 return fortran_bounds_for_dimension (lbound_p, exp->gdbarch, arg1, arg2);
1496 /* Implement STRUCTOP_STRUCT for Fortran. See operation::evaluate in
1497 expression.h for argument descriptions. */
1499 value *
1500 fortran_structop_operation::evaluate (struct type *expect_type,
1501 struct expression *exp,
1502 enum noside noside)
1504 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1505 const char *str = std::get<1> (m_storage).c_str ();
1506 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1508 struct type *type = lookup_struct_elt_type (value_type (arg1), str, 1);
1510 if (type != nullptr && is_dynamic_type (type))
1511 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, EVAL_NORMAL);
1514 value *elt = value_struct_elt (&arg1, {}, str, NULL, "structure");
1516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1518 struct type *elt_type = value_type (elt);
1519 if (is_dynamic_type (elt_type))
1521 const gdb_byte *valaddr = value_contents_for_printing (elt);
1522 CORE_ADDR address = value_address (elt);
1523 gdb::array_view<const gdb_byte> view
1524 = gdb::make_array_view (valaddr, TYPE_LENGTH (elt_type));
1525 elt_type = resolve_dynamic_type (elt_type, view, address);
1527 elt = value_zero (elt_type, VALUE_LVAL (elt));
1530 return elt;
1533 } /* namespace expr */
1535 /* See language.h. */
1537 void
1538 f_language::language_arch_info (struct gdbarch *gdbarch,
1539 struct language_arch_info *lai) const
1541 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
1543 /* Helper function to allow shorter lines below. */
1544 auto add = [&] (struct type * t)
1546 lai->add_primitive_type (t);
1549 add (builtin->builtin_character);
1550 add (builtin->builtin_logical);
1551 add (builtin->builtin_logical_s1);
1552 add (builtin->builtin_logical_s2);
1553 add (builtin->builtin_logical_s8);
1554 add (builtin->builtin_real);
1555 add (builtin->builtin_real_s8);
1556 add (builtin->builtin_real_s16);
1557 add (builtin->builtin_complex_s8);
1558 add (builtin->builtin_complex_s16);
1559 add (builtin->builtin_void);
1561 lai->set_string_char_type (builtin->builtin_character);
1562 lai->set_bool_type (builtin->builtin_logical_s2, "logical");
1565 /* See language.h. */
1567 unsigned int
1568 f_language::search_name_hash (const char *name) const
1570 return cp_search_name_hash (name);
1573 /* See language.h. */
1575 struct block_symbol
1576 f_language::lookup_symbol_nonlocal (const char *name,
1577 const struct block *block,
1578 const domain_enum domain) const
1580 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1583 /* See language.h. */
1585 symbol_name_matcher_ftype *
1586 f_language::get_symbol_name_matcher_inner
1587 (const lookup_name_info &lookup_name) const
1589 return cp_get_symbol_name_matcher (lookup_name);
1592 /* Single instance of the Fortran language class. */
1594 static f_language f_language_defn;
1596 static void *
1597 build_fortran_types (struct gdbarch *gdbarch)
1599 struct builtin_f_type *builtin_f_type
1600 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
1602 builtin_f_type->builtin_void
1603 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
1605 builtin_f_type->builtin_character
1606 = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
1608 builtin_f_type->builtin_logical_s1
1609 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
1611 builtin_f_type->builtin_integer_s2
1612 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
1613 "integer*2");
1615 builtin_f_type->builtin_integer_s8
1616 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
1617 "integer*8");
1619 builtin_f_type->builtin_logical_s2
1620 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
1621 "logical*2");
1623 builtin_f_type->builtin_logical_s8
1624 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
1625 "logical*8");
1627 builtin_f_type->builtin_integer
1628 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
1629 "integer");
1631 builtin_f_type->builtin_logical
1632 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
1633 "logical*4");
1635 builtin_f_type->builtin_real
1636 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
1637 "real", gdbarch_float_format (gdbarch));
1638 builtin_f_type->builtin_real_s8
1639 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
1640 "real*8", gdbarch_double_format (gdbarch));
1641 auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
1642 if (fmt != nullptr)
1643 builtin_f_type->builtin_real_s16
1644 = arch_float_type (gdbarch, 128, "real*16", fmt);
1645 else if (gdbarch_long_double_bit (gdbarch) == 128)
1646 builtin_f_type->builtin_real_s16
1647 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
1648 "real*16", gdbarch_long_double_format (gdbarch));
1649 else
1650 builtin_f_type->builtin_real_s16
1651 = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
1653 builtin_f_type->builtin_complex_s8
1654 = init_complex_type ("complex*8", builtin_f_type->builtin_real);
1655 builtin_f_type->builtin_complex_s16
1656 = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
1658 if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
1659 builtin_f_type->builtin_complex_s32
1660 = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
1661 else
1662 builtin_f_type->builtin_complex_s32
1663 = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
1665 return builtin_f_type;
1668 static struct gdbarch_data *f_type_data;
1670 const struct builtin_f_type *
1671 builtin_f_type (struct gdbarch *gdbarch)
1673 return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
1676 /* Command-list for the "set/show fortran" prefix command. */
1677 static struct cmd_list_element *set_fortran_list;
1678 static struct cmd_list_element *show_fortran_list;
1680 void _initialize_f_language ();
1681 void
1682 _initialize_f_language ()
1684 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
1686 add_basic_prefix_cmd ("fortran", no_class,
1687 _("Prefix command for changing Fortran-specific settings."),
1688 &set_fortran_list, 0, &setlist);
1690 add_show_prefix_cmd ("fortran", no_class,
1691 _("Generic command for showing Fortran-specific settings."),
1692 &show_fortran_list, 0, &showlist);
1694 add_setshow_boolean_cmd ("repack-array-slices", class_vars,
1695 &repack_array_slices, _("\
1696 Enable or disable repacking of non-contiguous array slices."), _("\
1697 Show whether non-contiguous array slices are repacked."), _("\
1698 When the user requests a slice of a Fortran array then we can either return\n\
1699 a descriptor that describes the array in place (using the original array data\n\
1700 in its existing location) or the original data can be repacked (copied) to a\n\
1701 new location.\n\
1703 When the content of the array slice is contiguous within the original array\n\
1704 then the result will never be repacked, but when the data for the new array\n\
1705 is non-contiguous within the original array repacking will only be performed\n\
1706 when this setting is on."),
1707 NULL,
1708 show_repack_array_slices,
1709 &set_fortran_list, &show_fortran_list);
1711 /* Debug Fortran's array slicing logic. */
1712 add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
1713 &fortran_array_slicing_debug, _("\
1714 Set debugging of Fortran array slicing."), _("\
1715 Show debugging of Fortran array slicing."), _("\
1716 When on, debugging of Fortran array slicing is enabled."),
1717 NULL,
1718 show_fortran_array_slicing_debug,
1719 &setdebuglist, &showdebuglist);
1722 /* Ensures that function argument VALUE is in the appropriate form to
1723 pass to a Fortran function. Returns a possibly new value that should
1724 be used instead of VALUE.
1726 When IS_ARTIFICIAL is true this indicates an artificial argument,
1727 e.g. hidden string lengths which the GNU Fortran argument passing
1728 convention specifies as being passed by value.
1730 When IS_ARTIFICIAL is false, the argument is passed by pointer. If the
1731 value is already in target memory then return a value that is a pointer
1732 to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate
1733 space in the target, copy VALUE in, and return a pointer to the in
1734 memory copy. */
1736 static struct value *
1737 fortran_argument_convert (struct value *value, bool is_artificial)
1739 if (!is_artificial)
1741 /* If the value is not in the inferior e.g. registers values,
1742 convenience variables and user input. */
1743 if (VALUE_LVAL (value) != lval_memory)
1745 struct type *type = value_type (value);
1746 const int length = TYPE_LENGTH (type);
1747 const CORE_ADDR addr
1748 = value_as_long (value_allocate_space_in_inferior (length));
1749 write_memory (addr, value_contents (value), length);
1750 struct value *val
1751 = value_from_contents_and_address (type, value_contents (value),
1752 addr);
1753 return value_addr (val);
1755 else
1756 return value_addr (value); /* Program variables, e.g. arrays. */
1758 return value;
1761 /* Prepare (and return) an argument value ready for an inferior function
1762 call to a Fortran function. EXP and POS are the expressions describing
1763 the argument to prepare. ARG_NUM is the argument number being
1764 prepared, with 0 being the first argument and so on. FUNC_TYPE is the
1765 type of the function being called.
1767 IS_INTERNAL_CALL_P is true if this is a call to a function of type
1768 TYPE_CODE_INTERNAL_FUNCTION, otherwise this parameter is false.
1770 NOSIDE has its usual meaning for expression parsing (see eval.c).
1772 Arguments in Fortran are normally passed by address, we coerce the
1773 arguments here rather than in value_arg_coerce as otherwise the call to
1774 malloc (to place the non-lvalue parameters in target memory) is hit by
1775 this Fortran specific logic. This results in malloc being called with a
1776 pointer to an integer followed by an attempt to malloc the arguments to
1777 malloc in target memory. Infinite recursion ensues. */
1779 static value *
1780 fortran_prepare_argument (struct expression *exp,
1781 expr::operation *subexp,
1782 int arg_num, bool is_internal_call_p,
1783 struct type *func_type, enum noside noside)
1785 if (is_internal_call_p)
1786 return subexp->evaluate_with_coercion (exp, noside);
1788 bool is_artificial = ((arg_num >= func_type->num_fields ())
1789 ? true
1790 : TYPE_FIELD_ARTIFICIAL (func_type, arg_num));
1792 /* If this is an artificial argument, then either, this is an argument
1793 beyond the end of the known arguments, or possibly, there are no known
1794 arguments (maybe missing debug info).
1796 For these artificial arguments, if the user has prefixed it with '&'
1797 (for address-of), then lets always allow this to succeed, even if the
1798 argument is not actually in inferior memory. This will allow the user
1799 to pass arguments to a Fortran function even when there's no debug
1800 information.
1802 As we already pass the address of non-artificial arguments, all we
1803 need to do if skip the UNOP_ADDR operator in the expression and mark
1804 the argument as non-artificial. */
1805 if (is_artificial)
1807 expr::unop_addr_operation *addrop
1808 = dynamic_cast<expr::unop_addr_operation *> (subexp);
1809 if (addrop != nullptr)
1811 subexp = addrop->get_expression ().get ();
1812 is_artificial = false;
1816 struct value *arg_val = subexp->evaluate_with_coercion (exp, noside);
1817 return fortran_argument_convert (arg_val, is_artificial);
1820 /* See f-lang.h. */
1822 struct type *
1823 fortran_preserve_arg_pointer (struct value *arg, struct type *type)
1825 if (value_type (arg)->code () == TYPE_CODE_PTR)
1826 return value_type (arg);
1827 return type;
1830 /* See f-lang.h. */
1832 CORE_ADDR
1833 fortran_adjust_dynamic_array_base_address_hack (struct type *type,
1834 CORE_ADDR address)
1836 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1838 /* We can't adjust the base address for arrays that have no content. */
1839 if (type_not_allocated (type) || type_not_associated (type))
1840 return address;
1842 int ndimensions = calc_f77_array_dims (type);
1843 LONGEST total_offset = 0;
1845 /* Walk through each of the dimensions of this array type and figure out
1846 if any of the dimensions are "backwards", that is the base address
1847 for this dimension points to the element at the highest memory
1848 address and the stride is negative. */
1849 struct type *tmp_type = type;
1850 for (int i = 0 ; i < ndimensions; ++i)
1852 /* Grab the range for this dimension and extract the lower and upper
1853 bounds. */
1854 tmp_type = check_typedef (tmp_type);
1855 struct type *range_type = tmp_type->index_type ();
1856 LONGEST lowerbound, upperbound, stride;
1857 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
1858 error ("failed to get range bounds");
1860 /* Figure out the stride for this dimension. */
1861 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
1862 stride = tmp_type->index_type ()->bounds ()->bit_stride ();
1863 if (stride == 0)
1864 stride = type_length_units (elt_type);
1865 else
1867 int unit_size
1868 = gdbarch_addressable_memory_unit_size (elt_type->arch ());
1869 stride /= (unit_size * 8);
1872 /* If this dimension is "backward" then figure out the offset
1873 adjustment required to point to the element at the lowest memory
1874 address, and add this to the total offset. */
1875 LONGEST offset = 0;
1876 if (stride < 0 && lowerbound < upperbound)
1877 offset = (upperbound - lowerbound) * stride;
1878 total_offset += offset;
1879 tmp_type = TYPE_TARGET_TYPE (tmp_type);
1882 /* Adjust the address of this object and return it. */
1883 address += total_offset;
1884 return address;