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