gdb: Add maint set ignore-prologue-end-flag
[binutils-gdb.git] / gdb / f-lang.c
1 /* Fortran language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1993-2022 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 #include "f-exp.h"
42
43 #include <math.h>
44
45 /* Whether GDB should repack array slices created by the user. */
46 static bool repack_array_slices = false;
47
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)
52 {
53 gdb_printf (file, _("Repacking of Fortran array slices is %s.\n"),
54 value);
55 }
56
57 /* Debugging of Fortran's array slicing. */
58 static bool fortran_array_slicing_debug = false;
59
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)
65 {
66 gdb_printf (file, _("Debugging of Fortran array slicing is %s.\n"),
67 value);
68 }
69
70 /* Local functions */
71
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);
76
77 /* Return the encoding that should be used for the character type
78 TYPE. */
79
80 const char *
81 f_language::get_encoding (struct type *type)
82 {
83 const char *encoding;
84
85 switch (TYPE_LENGTH (type))
86 {
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;
96
97 default:
98 error (_("unrecognized character type"));
99 }
100
101 return encoding;
102 }
103
104 \f
105
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. */
109
110 static void
111 fortran_require_array (struct type *type, bool lbound_p)
112 {
113 type = check_typedef (type);
114 if (type->code () != TYPE_CODE_ARRAY)
115 {
116 if (lbound_p)
117 error (_("LBOUND can only be applied to arrays"));
118 else
119 error (_("UBOUND can only be applied to arrays"));
120 }
121 }
122
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. */
126
127 static struct value *
128 fortran_bounds_all_dims (bool lbound_p,
129 struct gdbarch *gdbarch,
130 struct value *array)
131 {
132 type *array_type = check_typedef (value_type (array));
133 int ndimensions = calc_f77_array_dims (array_type);
134
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);
143
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)
150 {
151 LONGEST b;
152
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);
158
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);
165
166 /* Peel another dimension of the array. */
167 array_type = TYPE_TARGET_TYPE (array_type);
168 }
169
170 return result;
171 }
172
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. */
176
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)
182 {
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)
188 {
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);
193 }
194
195 /* The type for the result. */
196 struct type *bound_type = builtin_type (gdbarch)->builtin_long_long;
197
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)
201 {
202 /* If this is the requested dimension then we're done. Grab the
203 bounds and return. */
204 if (i == dim - 1)
205 {
206 LONGEST b;
207
208 if (lbound_p)
209 b = f77_get_lowerbound (array_type);
210 else
211 b = f77_get_upperbound (array_type);
212
213 return value_from_longest (bound_type, b);
214 }
215
216 /* Peel off another dimension of the array. */
217 array_type = TYPE_TARGET_TYPE (array_type);
218 }
219
220 gdb_assert_not_reached ("failed to find matching dimension");
221 }
222 \f
223
224 /* Return the number of dimensions for a Fortran array or string. */
225
226 int
227 calc_f77_array_dims (struct type *array_type)
228 {
229 int ndimen = 1;
230 struct type *tmp_type;
231
232 if ((array_type->code () == TYPE_CODE_STRING))
233 return 1;
234
235 if ((array_type->code () != TYPE_CODE_ARRAY))
236 error (_("Can't get dimensions for a non-array type"));
237
238 tmp_type = array_type;
239
240 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
241 {
242 if (tmp_type->code () == TYPE_CODE_ARRAY)
243 ++ndimen;
244 }
245 return ndimen;
246 }
247
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
254 {
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. */ }
261
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 (struct type *index_type, LONGEST nelts, bool inner_p)
267 {
268 if (inner_p)
269 {
270 gdb_assert (m_mark == nullptr);
271 m_mark = value_mark ();
272 }
273 }
274
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)
278 {
279 if (inner_p)
280 {
281 gdb_assert (m_mark != nullptr);
282 value_free_to_mark (m_mark);
283 m_mark = nullptr;
284 }
285 }
286
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)
291 {
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));
295 }
296
297 /* The value being written to. */
298 struct value *m_dest;
299
300 /* The byte offset in M_DEST at which the next element should be
301 written. */
302 LONGEST m_dest_offset;
303
304 /* Set with a call to VALUE_MARK, and then reset after calling
305 VALUE_FREE_TO_MARK. */
306 struct value *m_mark = nullptr;
307 };
308
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
316 {
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. */ }
329
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,
334 LONGEST index, bool last_p)
335 {
336 copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
337 }
338
339 private:
340 /* The address in target memory where the parent value starts. */
341 CORE_ADDR m_addr;
342 };
343
344 /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
345 slices. This class is specialised for repacking an array slice from a
346 previously loaded (non-lazy) array value, as such it fetches the
347 element values from the contents of the parent value. */
348 class fortran_array_repacker_impl
349 : public fortran_array_repacker_base_impl
350 {
351 public:
352 /* Constructor. TYPE is the type for the array slice within the parent
353 value, as such it has stride values as required to find the elements
354 within the original parent value. ADDRESS is the address in target
355 memory of the value matching TYPE. BASE_OFFSET is the offset from
356 the start of VAL's content buffer to the start of the object of TYPE,
357 VAL is the parent object from which we are loading the value, and
358 DEST is the value into which we are repacking. */
359 explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
360 LONGEST base_offset,
361 struct value *val, struct value *dest)
362 : fortran_array_repacker_base_impl (dest),
363 m_base_offset (base_offset),
364 m_val (val)
365 {
366 gdb_assert (!value_lazy (val));
367 }
368
369 /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
370 from the content buffer of M_VAL then copy this extracted value into
371 the repacked destination value. */
372 void process_element (struct type *elt_type, LONGEST elt_off,
373 LONGEST index, bool last_p)
374 {
375 struct value *elt
376 = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
377 copy_element_to_dest (elt);
378 }
379
380 private:
381 /* The offset into the content buffer of M_VAL to the start of the slice
382 being extracted. */
383 LONGEST m_base_offset;
384
385 /* The parent value from which we are extracting a slice. */
386 struct value *m_val;
387 };
388
389
390 /* Evaluate FORTRAN_ASSOCIATED expressions. Both GDBARCH and LANG are
391 extracted from the expression being evaluated. POINTER is the required
392 first argument to the 'associated' keyword, and TARGET is the optional
393 second argument, this will be nullptr if the user only passed one
394 argument to their use of 'associated'. */
395
396 static struct value *
397 fortran_associated (struct gdbarch *gdbarch, const language_defn *lang,
398 struct value *pointer, struct value *target = nullptr)
399 {
400 struct type *result_type = language_bool_type (lang, gdbarch);
401
402 /* All Fortran pointers should have the associated property, this is
403 how we know the pointer is pointing at something or not. */
404 struct type *pointer_type = check_typedef (value_type (pointer));
405 if (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
406 && pointer_type->code () != TYPE_CODE_PTR)
407 error (_("ASSOCIATED can only be applied to pointers"));
408
409 /* Get an address from POINTER. Fortran (or at least gfortran) models
410 array pointers as arrays with a dynamic data address, so we need to
411 use two approaches here, for real pointers we take the contents of the
412 pointer as an address. For non-pointers we take the address of the
413 content. */
414 CORE_ADDR pointer_addr;
415 if (pointer_type->code () == TYPE_CODE_PTR)
416 pointer_addr = value_as_address (pointer);
417 else
418 pointer_addr = value_address (pointer);
419
420 /* The single argument case, is POINTER associated with anything? */
421 if (target == nullptr)
422 {
423 bool is_associated = false;
424
425 /* If POINTER is an actual pointer and doesn't have an associated
426 property then we need to figure out whether this pointer is
427 associated by looking at the value of the pointer itself. We make
428 the assumption that a non-associated pointer will be set to 0.
429 This is probably true for most targets, but might not be true for
430 everyone. */
431 if (pointer_type->code () == TYPE_CODE_PTR
432 && TYPE_ASSOCIATED_PROP (pointer_type) == nullptr)
433 is_associated = (pointer_addr != 0);
434 else
435 is_associated = !type_not_associated (pointer_type);
436 return value_from_longest (result_type, is_associated ? 1 : 0);
437 }
438
439 /* The two argument case, is POINTER associated with TARGET? */
440
441 struct type *target_type = check_typedef (value_type (target));
442
443 struct type *pointer_target_type;
444 if (pointer_type->code () == TYPE_CODE_PTR)
445 pointer_target_type = TYPE_TARGET_TYPE (pointer_type);
446 else
447 pointer_target_type = pointer_type;
448
449 struct type *target_target_type;
450 if (target_type->code () == TYPE_CODE_PTR)
451 target_target_type = TYPE_TARGET_TYPE (target_type);
452 else
453 target_target_type = target_type;
454
455 if (pointer_target_type->code () != target_target_type->code ()
456 || (pointer_target_type->code () != TYPE_CODE_ARRAY
457 && (TYPE_LENGTH (pointer_target_type)
458 != TYPE_LENGTH (target_target_type))))
459 error (_("arguments to associated must be of same type and kind"));
460
461 /* If TARGET is not in memory, or the original pointer is specifically
462 known to be not associated with anything, then the answer is obviously
463 false. Alternatively, if POINTER is an actual pointer and has no
464 associated property, then we have to check if its associated by
465 looking the value of the pointer itself. We make the assumption that
466 a non-associated pointer will be set to 0. This is probably true for
467 most targets, but might not be true for everyone. */
468 if (value_lval_const (target) != lval_memory
469 || type_not_associated (pointer_type)
470 || (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
471 && pointer_type->code () == TYPE_CODE_PTR
472 && pointer_addr == 0))
473 return value_from_longest (result_type, 0);
474
475 /* See the comment for POINTER_ADDR above. */
476 CORE_ADDR target_addr;
477 if (target_type->code () == TYPE_CODE_PTR)
478 target_addr = value_as_address (target);
479 else
480 target_addr = value_address (target);
481
482 /* Wrap the following checks inside a do { ... } while (false) loop so
483 that we can use `break' to jump out of the loop. */
484 bool is_associated = false;
485 do
486 {
487 /* If the addresses are different then POINTER is definitely not
488 pointing at TARGET. */
489 if (pointer_addr != target_addr)
490 break;
491
492 /* If POINTER is a real pointer (i.e. not an array pointer, which are
493 implemented as arrays with a dynamic content address), then this
494 is all the checking that is needed. */
495 if (pointer_type->code () == TYPE_CODE_PTR)
496 {
497 is_associated = true;
498 break;
499 }
500
501 /* We have an array pointer. Check the number of dimensions. */
502 int pointer_dims = calc_f77_array_dims (pointer_type);
503 int target_dims = calc_f77_array_dims (target_type);
504 if (pointer_dims != target_dims)
505 break;
506
507 /* Now check that every dimension has the same upper bound, lower
508 bound, and stride value. */
509 int dim = 0;
510 while (dim < pointer_dims)
511 {
512 LONGEST pointer_lowerbound, pointer_upperbound, pointer_stride;
513 LONGEST target_lowerbound, target_upperbound, target_stride;
514
515 pointer_type = check_typedef (pointer_type);
516 target_type = check_typedef (target_type);
517
518 struct type *pointer_range = pointer_type->index_type ();
519 struct type *target_range = target_type->index_type ();
520
521 if (!get_discrete_bounds (pointer_range, &pointer_lowerbound,
522 &pointer_upperbound))
523 break;
524
525 if (!get_discrete_bounds (target_range, &target_lowerbound,
526 &target_upperbound))
527 break;
528
529 if (pointer_lowerbound != target_lowerbound
530 || pointer_upperbound != target_upperbound)
531 break;
532
533 /* Figure out the stride (in bits) for both pointer and target.
534 If either doesn't have a stride then we take the element size,
535 but we need to convert to bits (hence the * 8). */
536 pointer_stride = pointer_range->bounds ()->bit_stride ();
537 if (pointer_stride == 0)
538 pointer_stride
539 = type_length_units (check_typedef
540 (TYPE_TARGET_TYPE (pointer_type))) * 8;
541 target_stride = target_range->bounds ()->bit_stride ();
542 if (target_stride == 0)
543 target_stride
544 = type_length_units (check_typedef
545 (TYPE_TARGET_TYPE (target_type))) * 8;
546 if (pointer_stride != target_stride)
547 break;
548
549 ++dim;
550 }
551
552 if (dim < pointer_dims)
553 break;
554
555 is_associated = true;
556 }
557 while (false);
558
559 return value_from_longest (result_type, is_associated ? 1 : 0);
560 }
561
562 struct value *
563 eval_op_f_associated (struct type *expect_type,
564 struct expression *exp,
565 enum noside noside,
566 enum exp_opcode opcode,
567 struct value *arg1)
568 {
569 return fortran_associated (exp->gdbarch, exp->language_defn, arg1);
570 }
571
572 struct value *
573 eval_op_f_associated (struct type *expect_type,
574 struct expression *exp,
575 enum noside noside,
576 enum exp_opcode opcode,
577 struct value *arg1,
578 struct value *arg2)
579 {
580 return fortran_associated (exp->gdbarch, exp->language_defn, arg1, arg2);
581 }
582
583 /* Implement FORTRAN_ARRAY_SIZE expression, this corresponds to the 'SIZE'
584 keyword. Both GDBARCH and LANG are extracted from the expression being
585 evaluated. ARRAY is the value that should be an array, though this will
586 not have been checked before calling this function. DIM is optional, if
587 present then it should be an integer identifying a dimension of the
588 array to ask about. As with ARRAY the validity of DIM is not checked
589 before calling this function.
590
591 Return either the total number of elements in ARRAY (when DIM is
592 nullptr), or the number of elements in dimension DIM. */
593
594 static struct value *
595 fortran_array_size (struct gdbarch *gdbarch, const language_defn *lang,
596 struct value *array, struct value *dim_val = nullptr)
597 {
598 /* Check that ARRAY is the correct type. */
599 struct type *array_type = check_typedef (value_type (array));
600 if (array_type->code () != TYPE_CODE_ARRAY)
601 error (_("SIZE can only be applied to arrays"));
602 if (type_not_allocated (array_type) || type_not_associated (array_type))
603 error (_("SIZE can only be used on allocated/associated arrays"));
604
605 int ndimensions = calc_f77_array_dims (array_type);
606 int dim = -1;
607 LONGEST result = 0;
608
609 if (dim_val != nullptr)
610 {
611 if (check_typedef (value_type (dim_val))->code () != TYPE_CODE_INT)
612 error (_("DIM argument to SIZE must be an integer"));
613 dim = (int) value_as_long (dim_val);
614
615 if (dim < 1 || dim > ndimensions)
616 error (_("DIM argument to SIZE must be between 1 and %d"),
617 ndimensions);
618 }
619
620 /* Now walk over all the dimensions of the array totalling up the
621 elements in each dimension. */
622 for (int i = ndimensions - 1; i >= 0; --i)
623 {
624 /* If this is the requested dimension then we're done. Grab the
625 bounds and return. */
626 if (i == dim - 1 || dim == -1)
627 {
628 LONGEST lbound, ubound;
629 struct type *range = array_type->index_type ();
630
631 if (!get_discrete_bounds (range, &lbound, &ubound))
632 error (_("failed to find array bounds"));
633
634 LONGEST dim_size = (ubound - lbound + 1);
635 if (result == 0)
636 result = dim_size;
637 else
638 result *= dim_size;
639
640 if (dim != -1)
641 break;
642 }
643
644 /* Peel off another dimension of the array. */
645 array_type = TYPE_TARGET_TYPE (array_type);
646 }
647
648 struct type *result_type
649 = builtin_f_type (gdbarch)->builtin_integer;
650 return value_from_longest (result_type, result);
651 }
652
653 /* See f-exp.h. */
654
655 struct value *
656 eval_op_f_array_size (struct type *expect_type,
657 struct expression *exp,
658 enum noside noside,
659 enum exp_opcode opcode,
660 struct value *arg1)
661 {
662 gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
663 return fortran_array_size (exp->gdbarch, exp->language_defn, arg1);
664 }
665
666 /* See f-exp.h. */
667
668 struct value *
669 eval_op_f_array_size (struct type *expect_type,
670 struct expression *exp,
671 enum noside noside,
672 enum exp_opcode opcode,
673 struct value *arg1,
674 struct value *arg2)
675 {
676 gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
677 return fortran_array_size (exp->gdbarch, exp->language_defn, arg1, arg2);
678 }
679
680 /* Implement UNOP_FORTRAN_SHAPE expression. Both GDBARCH and LANG are
681 extracted from the expression being evaluated. VAL is the value on
682 which 'shape' was used, this can be any type.
683
684 Return an array of integers. If VAL is not an array then the returned
685 array should have zero elements. If VAL is an array then the returned
686 array should have one element per dimension, with the element
687 containing the extent of that dimension from VAL. */
688
689 static struct value *
690 fortran_array_shape (struct gdbarch *gdbarch, const language_defn *lang,
691 struct value *val)
692 {
693 struct type *val_type = check_typedef (value_type (val));
694
695 /* If we are passed an array that is either not allocated, or not
696 associated, then this is explicitly not allowed according to the
697 Fortran specification. */
698 if (val_type->code () == TYPE_CODE_ARRAY
699 && (type_not_associated (val_type) || type_not_allocated (val_type)))
700 error (_("The array passed to SHAPE must be allocated or associated"));
701
702 /* The Fortran specification allows non-array types to be passed to this
703 function, in which case we get back an empty array.
704
705 Calculate the number of dimensions for the resulting array. */
706 int ndimensions = 0;
707 if (val_type->code () == TYPE_CODE_ARRAY)
708 ndimensions = calc_f77_array_dims (val_type);
709
710 /* Allocate a result value of the correct type. */
711 struct type *range
712 = create_static_range_type (nullptr,
713 builtin_type (gdbarch)->builtin_int,
714 1, ndimensions);
715 struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
716 struct type *result_type = create_array_type (nullptr, elm_type, range);
717 struct value *result = allocate_value (result_type);
718 LONGEST elm_len = TYPE_LENGTH (elm_type);
719
720 /* Walk the array dimensions backwards due to the way the array will be
721 laid out in memory, the first dimension will be the most inner.
722
723 If VAL was not an array then ndimensions will be 0, in which case we
724 will never go around this loop. */
725 for (LONGEST dst_offset = elm_len * (ndimensions - 1);
726 dst_offset >= 0;
727 dst_offset -= elm_len)
728 {
729 LONGEST lbound, ubound;
730
731 if (!get_discrete_bounds (val_type->index_type (), &lbound, &ubound))
732 error (_("failed to find array bounds"));
733
734 LONGEST dim_size = (ubound - lbound + 1);
735
736 /* And copy the value into the result value. */
737 struct value *v = value_from_longest (elm_type, dim_size);
738 gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
739 <= TYPE_LENGTH (value_type (result)));
740 gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
741 value_contents_copy (result, dst_offset, v, 0, elm_len);
742
743 /* Peel another dimension of the array. */
744 val_type = TYPE_TARGET_TYPE (val_type);
745 }
746
747 return result;
748 }
749
750 /* See f-exp.h. */
751
752 struct value *
753 eval_op_f_array_shape (struct type *expect_type, struct expression *exp,
754 enum noside noside, enum exp_opcode opcode,
755 struct value *arg1)
756 {
757 gdb_assert (opcode == UNOP_FORTRAN_SHAPE);
758 return fortran_array_shape (exp->gdbarch, exp->language_defn, arg1);
759 }
760
761 /* A helper function for UNOP_ABS. */
762
763 struct value *
764 eval_op_f_abs (struct type *expect_type, struct expression *exp,
765 enum noside noside,
766 enum exp_opcode opcode,
767 struct value *arg1)
768 {
769 struct type *type = value_type (arg1);
770 switch (type->code ())
771 {
772 case TYPE_CODE_FLT:
773 {
774 double d
775 = fabs (target_float_to_host_double (value_contents (arg1).data (),
776 value_type (arg1)));
777 return value_from_host_double (type, d);
778 }
779 case TYPE_CODE_INT:
780 {
781 LONGEST l = value_as_long (arg1);
782 l = llabs (l);
783 return value_from_longest (type, l);
784 }
785 }
786 error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
787 }
788
789 /* A helper function for BINOP_MOD. */
790
791 struct value *
792 eval_op_f_mod (struct type *expect_type, struct expression *exp,
793 enum noside noside,
794 enum exp_opcode opcode,
795 struct value *arg1, struct value *arg2)
796 {
797 struct type *type = value_type (arg1);
798 if (type->code () != value_type (arg2)->code ())
799 error (_("non-matching types for parameters to MOD ()"));
800 switch (type->code ())
801 {
802 case TYPE_CODE_FLT:
803 {
804 double d1
805 = target_float_to_host_double (value_contents (arg1).data (),
806 value_type (arg1));
807 double d2
808 = target_float_to_host_double (value_contents (arg2).data (),
809 value_type (arg2));
810 double d3 = fmod (d1, d2);
811 return value_from_host_double (type, d3);
812 }
813 case TYPE_CODE_INT:
814 {
815 LONGEST v1 = value_as_long (arg1);
816 LONGEST v2 = value_as_long (arg2);
817 if (v2 == 0)
818 error (_("calling MOD (N, 0) is undefined"));
819 LONGEST v3 = v1 - (v1 / v2) * v2;
820 return value_from_longest (value_type (arg1), v3);
821 }
822 }
823 error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
824 }
825
826 /* A helper function for UNOP_FORTRAN_CEILING. */
827
828 struct value *
829 eval_op_f_ceil (struct type *expect_type, struct expression *exp,
830 enum noside noside,
831 enum exp_opcode opcode,
832 struct value *arg1)
833 {
834 struct type *type = value_type (arg1);
835 if (type->code () != TYPE_CODE_FLT)
836 error (_("argument to CEILING must be of type float"));
837 double val
838 = target_float_to_host_double (value_contents (arg1).data (),
839 value_type (arg1));
840 val = ceil (val);
841 return value_from_host_double (type, val);
842 }
843
844 /* A helper function for UNOP_FORTRAN_FLOOR. */
845
846 struct value *
847 eval_op_f_floor (struct type *expect_type, struct expression *exp,
848 enum noside noside,
849 enum exp_opcode opcode,
850 struct value *arg1)
851 {
852 struct type *type = value_type (arg1);
853 if (type->code () != TYPE_CODE_FLT)
854 error (_("argument to FLOOR must be of type float"));
855 double val
856 = target_float_to_host_double (value_contents (arg1).data (),
857 value_type (arg1));
858 val = floor (val);
859 return value_from_host_double (type, val);
860 }
861
862 /* A helper function for BINOP_FORTRAN_MODULO. */
863
864 struct value *
865 eval_op_f_modulo (struct type *expect_type, struct expression *exp,
866 enum noside noside,
867 enum exp_opcode opcode,
868 struct value *arg1, struct value *arg2)
869 {
870 struct type *type = value_type (arg1);
871 if (type->code () != value_type (arg2)->code ())
872 error (_("non-matching types for parameters to MODULO ()"));
873 /* MODULO(A, P) = A - FLOOR (A / P) * P */
874 switch (type->code ())
875 {
876 case TYPE_CODE_INT:
877 {
878 LONGEST a = value_as_long (arg1);
879 LONGEST p = value_as_long (arg2);
880 LONGEST result = a - (a / p) * p;
881 if (result != 0 && (a < 0) != (p < 0))
882 result += p;
883 return value_from_longest (value_type (arg1), result);
884 }
885 case TYPE_CODE_FLT:
886 {
887 double a
888 = target_float_to_host_double (value_contents (arg1).data (),
889 value_type (arg1));
890 double p
891 = target_float_to_host_double (value_contents (arg2).data (),
892 value_type (arg2));
893 double result = fmod (a, p);
894 if (result != 0 && (a < 0.0) != (p < 0.0))
895 result += p;
896 return value_from_host_double (type, result);
897 }
898 }
899 error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
900 }
901
902 /* A helper function for BINOP_FORTRAN_CMPLX. */
903
904 struct value *
905 eval_op_f_cmplx (struct type *expect_type, struct expression *exp,
906 enum noside noside,
907 enum exp_opcode opcode,
908 struct value *arg1, struct value *arg2)
909 {
910 struct type *type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
911 return value_literal_complex (arg1, arg2, type);
912 }
913
914 /* A helper function for UNOP_FORTRAN_KIND. */
915
916 struct value *
917 eval_op_f_kind (struct type *expect_type, struct expression *exp,
918 enum noside noside,
919 enum exp_opcode opcode,
920 struct value *arg1)
921 {
922 struct type *type = value_type (arg1);
923
924 switch (type->code ())
925 {
926 case TYPE_CODE_STRUCT:
927 case TYPE_CODE_UNION:
928 case TYPE_CODE_MODULE:
929 case TYPE_CODE_FUNC:
930 error (_("argument to kind must be an intrinsic type"));
931 }
932
933 if (!TYPE_TARGET_TYPE (type))
934 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
935 TYPE_LENGTH (type));
936 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
937 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
938 }
939
940 /* A helper function for UNOP_FORTRAN_ALLOCATED. */
941
942 struct value *
943 eval_op_f_allocated (struct type *expect_type, struct expression *exp,
944 enum noside noside, enum exp_opcode op,
945 struct value *arg1)
946 {
947 struct type *type = check_typedef (value_type (arg1));
948 if (type->code () != TYPE_CODE_ARRAY)
949 error (_("ALLOCATED can only be applied to arrays"));
950 struct type *result_type
951 = builtin_f_type (exp->gdbarch)->builtin_logical;
952 LONGEST result_value = type_not_allocated (type) ? 0 : 1;
953 return value_from_longest (result_type, result_value);
954 }
955
956 /* See f-exp.h. */
957
958 struct value *
959 eval_op_f_rank (struct type *expect_type,
960 struct expression *exp,
961 enum noside noside,
962 enum exp_opcode op,
963 struct value *arg1)
964 {
965 gdb_assert (op == UNOP_FORTRAN_RANK);
966
967 struct type *result_type
968 = builtin_f_type (exp->gdbarch)->builtin_integer;
969 struct type *type = check_typedef (value_type (arg1));
970 if (type->code () != TYPE_CODE_ARRAY)
971 return value_from_longest (result_type, 0);
972 LONGEST ndim = calc_f77_array_dims (type);
973 return value_from_longest (result_type, ndim);
974 }
975
976 /* A helper function for UNOP_FORTRAN_LOC. */
977
978 struct value *
979 eval_op_f_loc (struct type *expect_type, struct expression *exp,
980 enum noside noside, enum exp_opcode op,
981 struct value *arg1)
982 {
983 struct type *result_type;
984 if (gdbarch_ptr_bit (exp->gdbarch) == 16)
985 result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s2;
986 else if (gdbarch_ptr_bit (exp->gdbarch) == 32)
987 result_type = builtin_f_type (exp->gdbarch)->builtin_integer;
988 else
989 result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s8;
990
991 LONGEST result_value = value_address (arg1);
992 return value_from_longest (result_type, result_value);
993 }
994
995 namespace expr
996 {
997
998 /* Called from evaluate to perform array indexing, and sub-range
999 extraction, for Fortran. As well as arrays this function also
1000 handles strings as they can be treated like arrays of characters.
1001 ARRAY is the array or string being accessed. EXP and NOSIDE are as
1002 for evaluate. */
1003
1004 value *
1005 fortran_undetermined::value_subarray (value *array,
1006 struct expression *exp,
1007 enum noside noside)
1008 {
1009 type *original_array_type = check_typedef (value_type (array));
1010 bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
1011 const std::vector<operation_up> &ops = std::get<1> (m_storage);
1012 int nargs = ops.size ();
1013
1014 /* Perform checks for ARRAY not being available. The somewhat overly
1015 complex logic here is just to keep backward compatibility with the
1016 errors that we used to get before FORTRAN_VALUE_SUBARRAY was
1017 rewritten. Maybe a future task would streamline the error messages we
1018 get here, and update all the expected test results. */
1019 if (ops[0]->opcode () != OP_RANGE)
1020 {
1021 if (type_not_associated (original_array_type))
1022 error (_("no such vector element (vector not associated)"));
1023 else if (type_not_allocated (original_array_type))
1024 error (_("no such vector element (vector not allocated)"));
1025 }
1026 else
1027 {
1028 if (type_not_associated (original_array_type))
1029 error (_("array not associated"));
1030 else if (type_not_allocated (original_array_type))
1031 error (_("array not allocated"));
1032 }
1033
1034 /* First check that the number of dimensions in the type we are slicing
1035 matches the number of arguments we were passed. */
1036 int ndimensions = calc_f77_array_dims (original_array_type);
1037 if (nargs != ndimensions)
1038 error (_("Wrong number of subscripts"));
1039
1040 /* This will be initialised below with the type of the elements held in
1041 ARRAY. */
1042 struct type *inner_element_type;
1043
1044 /* Extract the types of each array dimension from the original array
1045 type. We need these available so we can fill in the default upper and
1046 lower bounds if the user requested slice doesn't provide that
1047 information. Additionally unpacking the dimensions like this gives us
1048 the inner element type. */
1049 std::vector<struct type *> dim_types;
1050 {
1051 dim_types.reserve (ndimensions);
1052 struct type *type = original_array_type;
1053 for (int i = 0; i < ndimensions; ++i)
1054 {
1055 dim_types.push_back (type);
1056 type = TYPE_TARGET_TYPE (type);
1057 }
1058 /* TYPE is now the inner element type of the array, we start the new
1059 array slice off as this type, then as we process the requested slice
1060 (from the user) we wrap new types around this to build up the final
1061 slice type. */
1062 inner_element_type = type;
1063 }
1064
1065 /* As we analyse the new slice type we need to understand if the data
1066 being referenced is contiguous. Do decide this we must track the size
1067 of an element at each dimension of the new slice array. Initially the
1068 elements of the inner most dimension of the array are the same inner
1069 most elements as the original ARRAY. */
1070 LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
1071
1072 /* Start off assuming all data is contiguous, this will be set to false
1073 if access to any dimension results in non-contiguous data. */
1074 bool is_all_contiguous = true;
1075
1076 /* The TOTAL_OFFSET is the distance in bytes from the start of the
1077 original ARRAY to the start of the new slice. This is calculated as
1078 we process the information from the user. */
1079 LONGEST total_offset = 0;
1080
1081 /* A structure representing information about each dimension of the
1082 resulting slice. */
1083 struct slice_dim
1084 {
1085 /* Constructor. */
1086 slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
1087 : low (l),
1088 high (h),
1089 stride (s),
1090 index (idx)
1091 { /* Nothing. */ }
1092
1093 /* The low bound for this dimension of the slice. */
1094 LONGEST low;
1095
1096 /* The high bound for this dimension of the slice. */
1097 LONGEST high;
1098
1099 /* The byte stride for this dimension of the slice. */
1100 LONGEST stride;
1101
1102 struct type *index;
1103 };
1104
1105 /* The dimensions of the resulting slice. */
1106 std::vector<slice_dim> slice_dims;
1107
1108 /* Process the incoming arguments. These arguments are in the reverse
1109 order to the array dimensions, that is the first argument refers to
1110 the last array dimension. */
1111 if (fortran_array_slicing_debug)
1112 debug_printf ("Processing array access:\n");
1113 for (int i = 0; i < nargs; ++i)
1114 {
1115 /* For each dimension of the array the user will have either provided
1116 a ranged access with optional lower bound, upper bound, and
1117 stride, or the user will have supplied a single index. */
1118 struct type *dim_type = dim_types[ndimensions - (i + 1)];
1119 fortran_range_operation *range_op
1120 = dynamic_cast<fortran_range_operation *> (ops[i].get ());
1121 if (range_op != nullptr)
1122 {
1123 enum range_flag range_flag = range_op->get_flags ();
1124
1125 LONGEST low, high, stride;
1126 low = high = stride = 0;
1127
1128 if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
1129 low = value_as_long (range_op->evaluate0 (exp, noside));
1130 else
1131 low = f77_get_lowerbound (dim_type);
1132 if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
1133 high = value_as_long (range_op->evaluate1 (exp, noside));
1134 else
1135 high = f77_get_upperbound (dim_type);
1136 if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
1137 stride = value_as_long (range_op->evaluate2 (exp, noside));
1138 else
1139 stride = 1;
1140
1141 if (stride == 0)
1142 error (_("stride must not be 0"));
1143
1144 /* Get information about this dimension in the original ARRAY. */
1145 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
1146 struct type *index_type = dim_type->index_type ();
1147 LONGEST lb = f77_get_lowerbound (dim_type);
1148 LONGEST ub = f77_get_upperbound (dim_type);
1149 LONGEST sd = index_type->bit_stride ();
1150 if (sd == 0)
1151 sd = TYPE_LENGTH (target_type) * 8;
1152
1153 if (fortran_array_slicing_debug)
1154 {
1155 debug_printf ("|-> Range access\n");
1156 std::string str = type_to_string (dim_type);
1157 debug_printf ("| |-> Type: %s\n", str.c_str ());
1158 debug_printf ("| |-> Array:\n");
1159 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
1160 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
1161 debug_printf ("| | |-> Bit stride: %s\n", plongest (sd));
1162 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8));
1163 debug_printf ("| | |-> Type size: %s\n",
1164 pulongest (TYPE_LENGTH (dim_type)));
1165 debug_printf ("| | '-> Target type size: %s\n",
1166 pulongest (TYPE_LENGTH (target_type)));
1167 debug_printf ("| |-> Accessing:\n");
1168 debug_printf ("| | |-> Low bound: %s\n",
1169 plongest (low));
1170 debug_printf ("| | |-> High bound: %s\n",
1171 plongest (high));
1172 debug_printf ("| | '-> Element stride: %s\n",
1173 plongest (stride));
1174 }
1175
1176 /* Check the user hasn't asked for something invalid. */
1177 if (high > ub || low < lb)
1178 error (_("array subscript out of bounds"));
1179
1180 /* Calculate what this dimension of the new slice array will look
1181 like. OFFSET is the byte offset from the start of the
1182 previous (more outer) dimension to the start of this
1183 dimension. E_COUNT is the number of elements in this
1184 dimension. REMAINDER is the number of elements remaining
1185 between the last included element and the upper bound. For
1186 example an access '1:6:2' will include elements 1, 3, 5 and
1187 have a remainder of 1 (element #6). */
1188 LONGEST lowest = std::min (low, high);
1189 LONGEST offset = (sd / 8) * (lowest - lb);
1190 LONGEST e_count = std::abs (high - low) + 1;
1191 e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
1192 LONGEST new_low = 1;
1193 LONGEST new_high = new_low + e_count - 1;
1194 LONGEST new_stride = (sd * stride) / 8;
1195 LONGEST last_elem = low + ((e_count - 1) * stride);
1196 LONGEST remainder = high - last_elem;
1197 if (low > high)
1198 {
1199 offset += std::abs (remainder) * TYPE_LENGTH (target_type);
1200 if (stride > 0)
1201 error (_("incorrect stride and boundary combination"));
1202 }
1203 else if (stride < 0)
1204 error (_("incorrect stride and boundary combination"));
1205
1206 /* Is the data within this dimension contiguous? It is if the
1207 newly computed stride is the same size as a single element of
1208 this dimension. */
1209 bool is_dim_contiguous = (new_stride == slice_element_size);
1210 is_all_contiguous &= is_dim_contiguous;
1211
1212 if (fortran_array_slicing_debug)
1213 {
1214 debug_printf ("| '-> Results:\n");
1215 debug_printf ("| |-> Offset = %s\n", plongest (offset));
1216 debug_printf ("| |-> Elements = %s\n", plongest (e_count));
1217 debug_printf ("| |-> Low bound = %s\n", plongest (new_low));
1218 debug_printf ("| |-> High bound = %s\n",
1219 plongest (new_high));
1220 debug_printf ("| |-> Byte stride = %s\n",
1221 plongest (new_stride));
1222 debug_printf ("| |-> Last element = %s\n",
1223 plongest (last_elem));
1224 debug_printf ("| |-> Remainder = %s\n",
1225 plongest (remainder));
1226 debug_printf ("| '-> Contiguous = %s\n",
1227 (is_dim_contiguous ? "Yes" : "No"));
1228 }
1229
1230 /* Figure out how big (in bytes) an element of this dimension of
1231 the new array slice will be. */
1232 slice_element_size = std::abs (new_stride * e_count);
1233
1234 slice_dims.emplace_back (new_low, new_high, new_stride,
1235 index_type);
1236
1237 /* Update the total offset. */
1238 total_offset += offset;
1239 }
1240 else
1241 {
1242 /* There is a single index for this dimension. */
1243 LONGEST index
1244 = value_as_long (ops[i]->evaluate_with_coercion (exp, noside));
1245
1246 /* Get information about this dimension in the original ARRAY. */
1247 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
1248 struct type *index_type = dim_type->index_type ();
1249 LONGEST lb = f77_get_lowerbound (dim_type);
1250 LONGEST ub = f77_get_upperbound (dim_type);
1251 LONGEST sd = index_type->bit_stride () / 8;
1252 if (sd == 0)
1253 sd = TYPE_LENGTH (target_type);
1254
1255 if (fortran_array_slicing_debug)
1256 {
1257 debug_printf ("|-> Index access\n");
1258 std::string str = type_to_string (dim_type);
1259 debug_printf ("| |-> Type: %s\n", str.c_str ());
1260 debug_printf ("| |-> Array:\n");
1261 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
1262 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
1263 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd));
1264 debug_printf ("| | |-> Type size: %s\n",
1265 pulongest (TYPE_LENGTH (dim_type)));
1266 debug_printf ("| | '-> Target type size: %s\n",
1267 pulongest (TYPE_LENGTH (target_type)));
1268 debug_printf ("| '-> Accessing:\n");
1269 debug_printf ("| '-> Index: %s\n",
1270 plongest (index));
1271 }
1272
1273 /* If the array has actual content then check the index is in
1274 bounds. An array without content (an unbound array) doesn't
1275 have a known upper bound, so don't error check in that
1276 situation. */
1277 if (index < lb
1278 || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
1279 && index > ub)
1280 || (VALUE_LVAL (array) != lval_memory
1281 && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
1282 {
1283 if (type_not_associated (dim_type))
1284 error (_("no such vector element (vector not associated)"));
1285 else if (type_not_allocated (dim_type))
1286 error (_("no such vector element (vector not allocated)"));
1287 else
1288 error (_("no such vector element"));
1289 }
1290
1291 /* Calculate using the type stride, not the target type size. */
1292 LONGEST offset = sd * (index - lb);
1293 total_offset += offset;
1294 }
1295 }
1296
1297 /* Build a type that represents the new array slice in the target memory
1298 of the original ARRAY, this type makes use of strides to correctly
1299 find only those elements that are part of the new slice. */
1300 struct type *array_slice_type = inner_element_type;
1301 for (const auto &d : slice_dims)
1302 {
1303 /* Create the range. */
1304 dynamic_prop p_low, p_high, p_stride;
1305
1306 p_low.set_const_val (d.low);
1307 p_high.set_const_val (d.high);
1308 p_stride.set_const_val (d.stride);
1309
1310 struct type *new_range
1311 = create_range_type_with_stride ((struct type *) NULL,
1312 TYPE_TARGET_TYPE (d.index),
1313 &p_low, &p_high, 0, &p_stride,
1314 true);
1315 array_slice_type
1316 = create_array_type (nullptr, array_slice_type, new_range);
1317 }
1318
1319 if (fortran_array_slicing_debug)
1320 {
1321 debug_printf ("'-> Final result:\n");
1322 debug_printf (" |-> Type: %s\n",
1323 type_to_string (array_slice_type).c_str ());
1324 debug_printf (" |-> Total offset: %s\n",
1325 plongest (total_offset));
1326 debug_printf (" |-> Base address: %s\n",
1327 core_addr_to_string (value_address (array)));
1328 debug_printf (" '-> Contiguous = %s\n",
1329 (is_all_contiguous ? "Yes" : "No"));
1330 }
1331
1332 /* Should we repack this array slice? */
1333 if (!is_all_contiguous && (repack_array_slices || is_string_p))
1334 {
1335 /* Build a type for the repacked slice. */
1336 struct type *repacked_array_type = inner_element_type;
1337 for (const auto &d : slice_dims)
1338 {
1339 /* Create the range. */
1340 dynamic_prop p_low, p_high, p_stride;
1341
1342 p_low.set_const_val (d.low);
1343 p_high.set_const_val (d.high);
1344 p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
1345
1346 struct type *new_range
1347 = create_range_type_with_stride ((struct type *) NULL,
1348 TYPE_TARGET_TYPE (d.index),
1349 &p_low, &p_high, 0, &p_stride,
1350 true);
1351 repacked_array_type
1352 = create_array_type (nullptr, repacked_array_type, new_range);
1353 }
1354
1355 /* Now copy the elements from the original ARRAY into the packed
1356 array value DEST. */
1357 struct value *dest = allocate_value (repacked_array_type);
1358 if (value_lazy (array)
1359 || (total_offset + TYPE_LENGTH (array_slice_type)
1360 > TYPE_LENGTH (check_typedef (value_type (array)))))
1361 {
1362 fortran_array_walker<fortran_lazy_array_repacker_impl> p
1363 (array_slice_type, value_address (array) + total_offset, dest);
1364 p.walk ();
1365 }
1366 else
1367 {
1368 fortran_array_walker<fortran_array_repacker_impl> p
1369 (array_slice_type, value_address (array) + total_offset,
1370 total_offset, array, dest);
1371 p.walk ();
1372 }
1373 array = dest;
1374 }
1375 else
1376 {
1377 if (VALUE_LVAL (array) == lval_memory)
1378 {
1379 /* If the value we're taking a slice from is not yet loaded, or
1380 the requested slice is outside the values content range then
1381 just create a new lazy value pointing at the memory where the
1382 contents we're looking for exist. */
1383 if (value_lazy (array)
1384 || (total_offset + TYPE_LENGTH (array_slice_type)
1385 > TYPE_LENGTH (check_typedef (value_type (array)))))
1386 array = value_at_lazy (array_slice_type,
1387 value_address (array) + total_offset);
1388 else
1389 array = value_from_contents_and_address
1390 (array_slice_type, value_contents (array).data () + total_offset,
1391 value_address (array) + total_offset);
1392 }
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"));
1397 }
1398
1399 return array;
1400 }
1401
1402 value *
1403 fortran_undetermined::evaluate (struct type *expect_type,
1404 struct expression *exp,
1405 enum noside noside)
1406 {
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 ();
1413
1414 if (code == TYPE_CODE_PTR)
1415 {
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));
1421
1422 if (target_type->code () == TYPE_CODE_ARRAY
1423 || target_type->code () == TYPE_CODE_STRING
1424 || target_type->code () == TYPE_CODE_FUNC)
1425 {
1426 callee = value_ind (callee);
1427 type = check_typedef (value_type (callee));
1428 code = type->code ();
1429 }
1430 }
1431
1432 switch (code)
1433 {
1434 case TYPE_CODE_ARRAY:
1435 case TYPE_CODE_STRING:
1436 return value_subarray (callee, exp, noside);
1437
1438 case TYPE_CODE_PTR:
1439 case TYPE_CODE_FUNC:
1440 case TYPE_CODE_INTERNAL_FUNCTION:
1441 {
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);
1455 }
1456
1457 default:
1458 error (_("Cannot perform substring on this type"));
1459 }
1460 }
1461
1462 value *
1463 fortran_bound_1arg::evaluate (struct type *expect_type,
1464 struct expression *exp,
1465 enum noside noside)
1466 {
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);
1471 }
1472
1473 value *
1474 fortran_bound_2arg::evaluate (struct type *expect_type,
1475 struct expression *exp,
1476 enum noside noside)
1477 {
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);
1481
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)
1486 {
1487 if (lbound_p)
1488 error (_("LBOUND second argument should be an integer"));
1489 else
1490 error (_("UBOUND second argument should be an integer"));
1491 }
1492
1493 return fortran_bounds_for_dimension (lbound_p, exp->gdbarch, arg1, arg2);
1494 }
1495
1496 /* Implement STRUCTOP_STRUCT for Fortran. See operation::evaluate in
1497 expression.h for argument descriptions. */
1498
1499 value *
1500 fortran_structop_operation::evaluate (struct type *expect_type,
1501 struct expression *exp,
1502 enum noside noside)
1503 {
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)
1507 {
1508 struct type *type = lookup_struct_elt_type (value_type (arg1), str, 1);
1509
1510 if (type != nullptr && is_dynamic_type (type))
1511 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, EVAL_NORMAL);
1512 }
1513
1514 value *elt = value_struct_elt (&arg1, {}, str, NULL, "structure");
1515
1516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1517 {
1518 struct type *elt_type = value_type (elt);
1519 if (is_dynamic_type (elt_type))
1520 {
1521 const gdb_byte *valaddr = value_contents_for_printing (elt).data ();
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);
1526 }
1527 elt = value_zero (elt_type, VALUE_LVAL (elt));
1528 }
1529
1530 return elt;
1531 }
1532
1533 } /* namespace expr */
1534
1535 /* See language.h. */
1536
1537 void
1538 f_language::print_array_index (struct type *index_type, LONGEST index,
1539 struct ui_file *stream,
1540 const value_print_options *options) const
1541 {
1542 struct value *index_value = value_from_longest (index_type, index);
1543
1544 gdb_printf (stream, "(");
1545 value_print (index_value, stream, options);
1546 gdb_printf (stream, ") = ");
1547 }
1548
1549 /* See language.h. */
1550
1551 void
1552 f_language::language_arch_info (struct gdbarch *gdbarch,
1553 struct language_arch_info *lai) const
1554 {
1555 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
1556
1557 /* Helper function to allow shorter lines below. */
1558 auto add = [&] (struct type * t)
1559 {
1560 lai->add_primitive_type (t);
1561 };
1562
1563 add (builtin->builtin_character);
1564 add (builtin->builtin_logical);
1565 add (builtin->builtin_logical_s1);
1566 add (builtin->builtin_logical_s2);
1567 add (builtin->builtin_logical_s8);
1568 add (builtin->builtin_real);
1569 add (builtin->builtin_real_s8);
1570 add (builtin->builtin_real_s16);
1571 add (builtin->builtin_complex_s8);
1572 add (builtin->builtin_complex_s16);
1573 add (builtin->builtin_void);
1574
1575 lai->set_string_char_type (builtin->builtin_character);
1576 lai->set_bool_type (builtin->builtin_logical_s2, "logical");
1577 }
1578
1579 /* See language.h. */
1580
1581 unsigned int
1582 f_language::search_name_hash (const char *name) const
1583 {
1584 return cp_search_name_hash (name);
1585 }
1586
1587 /* See language.h. */
1588
1589 struct block_symbol
1590 f_language::lookup_symbol_nonlocal (const char *name,
1591 const struct block *block,
1592 const domain_enum domain) const
1593 {
1594 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1595 }
1596
1597 /* See language.h. */
1598
1599 symbol_name_matcher_ftype *
1600 f_language::get_symbol_name_matcher_inner
1601 (const lookup_name_info &lookup_name) const
1602 {
1603 return cp_get_symbol_name_matcher (lookup_name);
1604 }
1605
1606 /* Single instance of the Fortran language class. */
1607
1608 static f_language f_language_defn;
1609
1610 static void *
1611 build_fortran_types (struct gdbarch *gdbarch)
1612 {
1613 struct builtin_f_type *builtin_f_type
1614 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
1615
1616 builtin_f_type->builtin_void
1617 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
1618
1619 builtin_f_type->builtin_character
1620 = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
1621
1622 builtin_f_type->builtin_logical_s1
1623 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
1624
1625 builtin_f_type->builtin_integer_s2
1626 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
1627 "integer*2");
1628
1629 builtin_f_type->builtin_integer_s8
1630 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
1631 "integer*8");
1632
1633 builtin_f_type->builtin_logical_s2
1634 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
1635 "logical*2");
1636
1637 builtin_f_type->builtin_logical_s8
1638 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
1639 "logical*8");
1640
1641 builtin_f_type->builtin_integer
1642 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
1643 "integer");
1644
1645 builtin_f_type->builtin_logical
1646 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
1647 "logical*4");
1648
1649 builtin_f_type->builtin_real
1650 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
1651 "real", gdbarch_float_format (gdbarch));
1652 builtin_f_type->builtin_real_s8
1653 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
1654 "real*8", gdbarch_double_format (gdbarch));
1655 auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
1656 if (fmt != nullptr)
1657 builtin_f_type->builtin_real_s16
1658 = arch_float_type (gdbarch, 128, "real*16", fmt);
1659 else if (gdbarch_long_double_bit (gdbarch) == 128)
1660 builtin_f_type->builtin_real_s16
1661 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
1662 "real*16", gdbarch_long_double_format (gdbarch));
1663 else
1664 builtin_f_type->builtin_real_s16
1665 = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
1666
1667 builtin_f_type->builtin_complex_s8
1668 = init_complex_type ("complex*8", builtin_f_type->builtin_real);
1669 builtin_f_type->builtin_complex_s16
1670 = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
1671
1672 if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
1673 builtin_f_type->builtin_complex_s32
1674 = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
1675 else
1676 builtin_f_type->builtin_complex_s32
1677 = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
1678
1679 return builtin_f_type;
1680 }
1681
1682 static struct gdbarch_data *f_type_data;
1683
1684 const struct builtin_f_type *
1685 builtin_f_type (struct gdbarch *gdbarch)
1686 {
1687 return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
1688 }
1689
1690 /* Command-list for the "set/show fortran" prefix command. */
1691 static struct cmd_list_element *set_fortran_list;
1692 static struct cmd_list_element *show_fortran_list;
1693
1694 void _initialize_f_language ();
1695 void
1696 _initialize_f_language ()
1697 {
1698 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
1699
1700 add_setshow_prefix_cmd
1701 ("fortran", no_class,
1702 _("Prefix command for changing Fortran-specific settings."),
1703 _("Generic command for showing Fortran-specific settings."),
1704 &set_fortran_list, &show_fortran_list,
1705 &setlist, &showlist);
1706
1707 add_setshow_boolean_cmd ("repack-array-slices", class_vars,
1708 &repack_array_slices, _("\
1709 Enable or disable repacking of non-contiguous array slices."), _("\
1710 Show whether non-contiguous array slices are repacked."), _("\
1711 When the user requests a slice of a Fortran array then we can either return\n\
1712 a descriptor that describes the array in place (using the original array data\n\
1713 in its existing location) or the original data can be repacked (copied) to a\n\
1714 new location.\n\
1715 \n\
1716 When the content of the array slice is contiguous within the original array\n\
1717 then the result will never be repacked, but when the data for the new array\n\
1718 is non-contiguous within the original array repacking will only be performed\n\
1719 when this setting is on."),
1720 NULL,
1721 show_repack_array_slices,
1722 &set_fortran_list, &show_fortran_list);
1723
1724 /* Debug Fortran's array slicing logic. */
1725 add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
1726 &fortran_array_slicing_debug, _("\
1727 Set debugging of Fortran array slicing."), _("\
1728 Show debugging of Fortran array slicing."), _("\
1729 When on, debugging of Fortran array slicing is enabled."),
1730 NULL,
1731 show_fortran_array_slicing_debug,
1732 &setdebuglist, &showdebuglist);
1733 }
1734
1735 /* Ensures that function argument VALUE is in the appropriate form to
1736 pass to a Fortran function. Returns a possibly new value that should
1737 be used instead of VALUE.
1738
1739 When IS_ARTIFICIAL is true this indicates an artificial argument,
1740 e.g. hidden string lengths which the GNU Fortran argument passing
1741 convention specifies as being passed by value.
1742
1743 When IS_ARTIFICIAL is false, the argument is passed by pointer. If the
1744 value is already in target memory then return a value that is a pointer
1745 to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate
1746 space in the target, copy VALUE in, and return a pointer to the in
1747 memory copy. */
1748
1749 static struct value *
1750 fortran_argument_convert (struct value *value, bool is_artificial)
1751 {
1752 if (!is_artificial)
1753 {
1754 /* If the value is not in the inferior e.g. registers values,
1755 convenience variables and user input. */
1756 if (VALUE_LVAL (value) != lval_memory)
1757 {
1758 struct type *type = value_type (value);
1759 const int length = TYPE_LENGTH (type);
1760 const CORE_ADDR addr
1761 = value_as_long (value_allocate_space_in_inferior (length));
1762 write_memory (addr, value_contents (value).data (), length);
1763 struct value *val = value_from_contents_and_address
1764 (type, value_contents (value).data (), addr);
1765 return value_addr (val);
1766 }
1767 else
1768 return value_addr (value); /* Program variables, e.g. arrays. */
1769 }
1770 return value;
1771 }
1772
1773 /* Prepare (and return) an argument value ready for an inferior function
1774 call to a Fortran function. EXP and POS are the expressions describing
1775 the argument to prepare. ARG_NUM is the argument number being
1776 prepared, with 0 being the first argument and so on. FUNC_TYPE is the
1777 type of the function being called.
1778
1779 IS_INTERNAL_CALL_P is true if this is a call to a function of type
1780 TYPE_CODE_INTERNAL_FUNCTION, otherwise this parameter is false.
1781
1782 NOSIDE has its usual meaning for expression parsing (see eval.c).
1783
1784 Arguments in Fortran are normally passed by address, we coerce the
1785 arguments here rather than in value_arg_coerce as otherwise the call to
1786 malloc (to place the non-lvalue parameters in target memory) is hit by
1787 this Fortran specific logic. This results in malloc being called with a
1788 pointer to an integer followed by an attempt to malloc the arguments to
1789 malloc in target memory. Infinite recursion ensues. */
1790
1791 static value *
1792 fortran_prepare_argument (struct expression *exp,
1793 expr::operation *subexp,
1794 int arg_num, bool is_internal_call_p,
1795 struct type *func_type, enum noside noside)
1796 {
1797 if (is_internal_call_p)
1798 return subexp->evaluate_with_coercion (exp, noside);
1799
1800 bool is_artificial = ((arg_num >= func_type->num_fields ())
1801 ? true
1802 : TYPE_FIELD_ARTIFICIAL (func_type, arg_num));
1803
1804 /* If this is an artificial argument, then either, this is an argument
1805 beyond the end of the known arguments, or possibly, there are no known
1806 arguments (maybe missing debug info).
1807
1808 For these artificial arguments, if the user has prefixed it with '&'
1809 (for address-of), then lets always allow this to succeed, even if the
1810 argument is not actually in inferior memory. This will allow the user
1811 to pass arguments to a Fortran function even when there's no debug
1812 information.
1813
1814 As we already pass the address of non-artificial arguments, all we
1815 need to do if skip the UNOP_ADDR operator in the expression and mark
1816 the argument as non-artificial. */
1817 if (is_artificial)
1818 {
1819 expr::unop_addr_operation *addrop
1820 = dynamic_cast<expr::unop_addr_operation *> (subexp);
1821 if (addrop != nullptr)
1822 {
1823 subexp = addrop->get_expression ().get ();
1824 is_artificial = false;
1825 }
1826 }
1827
1828 struct value *arg_val = subexp->evaluate_with_coercion (exp, noside);
1829 return fortran_argument_convert (arg_val, is_artificial);
1830 }
1831
1832 /* See f-lang.h. */
1833
1834 struct type *
1835 fortran_preserve_arg_pointer (struct value *arg, struct type *type)
1836 {
1837 if (value_type (arg)->code () == TYPE_CODE_PTR)
1838 return value_type (arg);
1839 return type;
1840 }
1841
1842 /* See f-lang.h. */
1843
1844 CORE_ADDR
1845 fortran_adjust_dynamic_array_base_address_hack (struct type *type,
1846 CORE_ADDR address)
1847 {
1848 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1849
1850 /* We can't adjust the base address for arrays that have no content. */
1851 if (type_not_allocated (type) || type_not_associated (type))
1852 return address;
1853
1854 int ndimensions = calc_f77_array_dims (type);
1855 LONGEST total_offset = 0;
1856
1857 /* Walk through each of the dimensions of this array type and figure out
1858 if any of the dimensions are "backwards", that is the base address
1859 for this dimension points to the element at the highest memory
1860 address and the stride is negative. */
1861 struct type *tmp_type = type;
1862 for (int i = 0 ; i < ndimensions; ++i)
1863 {
1864 /* Grab the range for this dimension and extract the lower and upper
1865 bounds. */
1866 tmp_type = check_typedef (tmp_type);
1867 struct type *range_type = tmp_type->index_type ();
1868 LONGEST lowerbound, upperbound, stride;
1869 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
1870 error ("failed to get range bounds");
1871
1872 /* Figure out the stride for this dimension. */
1873 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
1874 stride = tmp_type->index_type ()->bounds ()->bit_stride ();
1875 if (stride == 0)
1876 stride = type_length_units (elt_type);
1877 else
1878 {
1879 int unit_size
1880 = gdbarch_addressable_memory_unit_size (elt_type->arch ());
1881 stride /= (unit_size * 8);
1882 }
1883
1884 /* If this dimension is "backward" then figure out the offset
1885 adjustment required to point to the element at the lowest memory
1886 address, and add this to the total offset. */
1887 LONGEST offset = 0;
1888 if (stride < 0 && lowerbound < upperbound)
1889 offset = (upperbound - lowerbound) * stride;
1890 total_offset += offset;
1891 tmp_type = TYPE_TARGET_TYPE (tmp_type);
1892 }
1893
1894 /* Adjust the address of this object and return it. */
1895 address += total_offset;
1896 return address;
1897 }