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