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