Return unique_ptr from language_defn::get_compile_context
[deliverable/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"
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{
52 fprintf_filtered (file, _("Repacking of Fortran array slices is %s.\n"),
53 value);
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{
65 fprintf_filtered (file, _("Debugging of Fortran array slicing is %s.\n"),
66 value);
67}
68
c906108c
SS
69/* Local functions */
70
5a7cf527
AB
71static struct value *fortran_argument_convert (struct value *value,
72 bool is_artificial);
73
3b2b8fea
TT
74/* Return the encoding that should be used for the character type
75 TYPE. */
76
1a0ea399
AB
77const char *
78f_language::get_encoding (struct type *type)
3b2b8fea
TT
79{
80 const char *encoding;
81
82 switch (TYPE_LENGTH (type))
83 {
84 case 1:
8ee511af 85 encoding = target_charset (type->arch ());
3b2b8fea
TT
86 break;
87 case 4:
34877895 88 if (type_byte_order (type) == BFD_ENDIAN_BIG)
3b2b8fea
TT
89 encoding = "UTF-32BE";
90 else
91 encoding = "UTF-32LE";
92 break;
93
94 default:
95 error (_("unrecognized character type"));
96 }
97
98 return encoding;
99}
100
c906108c 101\f
c5aa993b 102
c906108c
SS
103/* Table of operators and their precedences for printing expressions. */
104
1a0ea399 105const struct op_print f_language::op_print_tab[] =
c5aa993b
JM
106{
107 {"+", BINOP_ADD, PREC_ADD, 0},
108 {"+", UNOP_PLUS, PREC_PREFIX, 0},
109 {"-", BINOP_SUB, PREC_ADD, 0},
110 {"-", UNOP_NEG, PREC_PREFIX, 0},
111 {"*", BINOP_MUL, PREC_MUL, 0},
112 {"/", BINOP_DIV, PREC_MUL, 0},
113 {"DIV", BINOP_INTDIV, PREC_MUL, 0},
114 {"MOD", BINOP_REM, PREC_MUL, 0},
115 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
116 {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
117 {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
118 {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
119 {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
120 {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
121 {".LE.", BINOP_LEQ, PREC_ORDER, 0},
122 {".GE.", BINOP_GEQ, PREC_ORDER, 0},
123 {".GT.", BINOP_GTR, PREC_ORDER, 0},
124 {".LT.", BINOP_LESS, PREC_ORDER, 0},
125 {"**", UNOP_IND, PREC_PREFIX, 0},
126 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
f486487f 127 {NULL, OP_NULL, PREC_REPEAT, 0}
c906108c
SS
128};
129\f
c906108c 130
6d816919
AB
131/* Return the number of dimensions for a Fortran array or string. */
132
133int
134calc_f77_array_dims (struct type *array_type)
135{
136 int ndimen = 1;
137 struct type *tmp_type;
138
139 if ((array_type->code () == TYPE_CODE_STRING))
140 return 1;
141
142 if ((array_type->code () != TYPE_CODE_ARRAY))
143 error (_("Can't get dimensions for a non-array type"));
144
145 tmp_type = array_type;
146
147 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
148 {
149 if (tmp_type->code () == TYPE_CODE_ARRAY)
150 ++ndimen;
151 }
152 return ndimen;
153}
154
a5c641b5
AB
155/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
156 slices. This is a base class for two alternative repacking mechanisms,
157 one for when repacking from a lazy value, and one for repacking from a
158 non-lazy (already loaded) value. */
159class fortran_array_repacker_base_impl
160 : public fortran_array_walker_base_impl
161{
162public:
163 /* Constructor, DEST is the value we are repacking into. */
164 fortran_array_repacker_base_impl (struct value *dest)
165 : m_dest (dest),
166 m_dest_offset (0)
167 { /* Nothing. */ }
168
169 /* When we start processing the inner most dimension, this is where we
170 will be creating values for each element as we load them and then copy
171 them into the M_DEST value. Set a value mark so we can free these
172 temporary values. */
173 void start_dimension (bool inner_p)
174 {
175 if (inner_p)
176 {
177 gdb_assert (m_mark == nullptr);
178 m_mark = value_mark ();
179 }
180 }
181
182 /* When we finish processing the inner most dimension free all temporary
183 value that were created. */
184 void finish_dimension (bool inner_p, bool last_p)
185 {
186 if (inner_p)
187 {
188 gdb_assert (m_mark != nullptr);
189 value_free_to_mark (m_mark);
190 m_mark = nullptr;
191 }
192 }
193
194protected:
195 /* Copy the contents of array element ELT into M_DEST at the next
196 available offset. */
197 void copy_element_to_dest (struct value *elt)
198 {
199 value_contents_copy (m_dest, m_dest_offset, elt, 0,
200 TYPE_LENGTH (value_type (elt)));
201 m_dest_offset += TYPE_LENGTH (value_type (elt));
202 }
203
204 /* The value being written to. */
205 struct value *m_dest;
206
207 /* The byte offset in M_DEST at which the next element should be
208 written. */
209 LONGEST m_dest_offset;
210
211 /* Set with a call to VALUE_MARK, and then reset after calling
212 VALUE_FREE_TO_MARK. */
213 struct value *m_mark = nullptr;
214};
215
216/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
217 slices. This class is specialised for repacking an array slice from a
218 lazy array value, as such it does not require the parent array value to
219 be loaded into GDB's memory; the parent value could be huge, while the
220 slice could be tiny. */
221class fortran_lazy_array_repacker_impl
222 : public fortran_array_repacker_base_impl
223{
224public:
225 /* Constructor. TYPE is the type of the slice being loaded from the
226 parent value, so this type will correctly reflect the strides required
227 to find all of the elements from the parent value. ADDRESS is the
228 address in target memory of value matching TYPE, and DEST is the value
229 we are repacking into. */
230 explicit fortran_lazy_array_repacker_impl (struct type *type,
231 CORE_ADDR address,
232 struct value *dest)
233 : fortran_array_repacker_base_impl (dest),
234 m_addr (address)
235 { /* Nothing. */ }
236
237 /* Create a lazy value in target memory representing a single element,
238 then load the element into GDB's memory and copy the contents into the
239 destination value. */
240 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
241 {
242 copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
243 }
244
245private:
246 /* The address in target memory where the parent value starts. */
247 CORE_ADDR m_addr;
248};
249
250/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
251 slices. This class is specialised for repacking an array slice from a
252 previously loaded (non-lazy) array value, as such it fetches the
253 element values from the contents of the parent value. */
254class fortran_array_repacker_impl
255 : public fortran_array_repacker_base_impl
256{
257public:
258 /* Constructor. TYPE is the type for the array slice within the parent
259 value, as such it has stride values as required to find the elements
260 within the original parent value. ADDRESS is the address in target
261 memory of the value matching TYPE. BASE_OFFSET is the offset from
262 the start of VAL's content buffer to the start of the object of TYPE,
263 VAL is the parent object from which we are loading the value, and
264 DEST is the value into which we are repacking. */
265 explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
266 LONGEST base_offset,
267 struct value *val, struct value *dest)
268 : fortran_array_repacker_base_impl (dest),
269 m_base_offset (base_offset),
270 m_val (val)
271 {
272 gdb_assert (!value_lazy (val));
273 }
274
275 /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
276 from the content buffer of M_VAL then copy this extracted value into
277 the repacked destination value. */
278 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
279 {
280 struct value *elt
281 = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
282 copy_element_to_dest (elt);
283 }
284
285private:
286 /* The offset into the content buffer of M_VAL to the start of the slice
287 being extracted. */
288 LONGEST m_base_offset;
289
290 /* The parent value from which we are extracting a slice. */
291 struct value *m_val;
292};
293
6d816919
AB
294/* Called from evaluate_subexp_standard to perform array indexing, and
295 sub-range extraction, for Fortran. As well as arrays this function
296 also handles strings as they can be treated like arrays of characters.
297 ARRAY is the array or string being accessed. EXP, POS, and NOSIDE are
298 as for evaluate_subexp_standard, and NARGS is the number of arguments
299 in this access (e.g. 'array (1,2,3)' would be NARGS 3). */
300
301static struct value *
302fortran_value_subarray (struct value *array, struct expression *exp,
303 int *pos, int nargs, enum noside noside)
304{
a5c641b5
AB
305 type *original_array_type = check_typedef (value_type (array));
306 bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
307
308 /* Perform checks for ARRAY not being available. The somewhat overly
309 complex logic here is just to keep backward compatibility with the
310 errors that we used to get before FORTRAN_VALUE_SUBARRAY was
311 rewritten. Maybe a future task would streamline the error messages we
312 get here, and update all the expected test results. */
313 if (exp->elts[*pos].opcode != OP_RANGE)
314 {
315 if (type_not_associated (original_array_type))
316 error (_("no such vector element (vector not associated)"));
317 else if (type_not_allocated (original_array_type))
318 error (_("no such vector element (vector not allocated)"));
319 }
320 else
6d816919 321 {
a5c641b5
AB
322 if (type_not_associated (original_array_type))
323 error (_("array not associated"));
324 else if (type_not_allocated (original_array_type))
325 error (_("array not allocated"));
6d816919
AB
326 }
327
a5c641b5
AB
328 /* First check that the number of dimensions in the type we are slicing
329 matches the number of arguments we were passed. */
330 int ndimensions = calc_f77_array_dims (original_array_type);
331 if (nargs != ndimensions)
332 error (_("Wrong number of subscripts"));
333
334 /* This will be initialised below with the type of the elements held in
335 ARRAY. */
336 struct type *inner_element_type;
337
338 /* Extract the types of each array dimension from the original array
339 type. We need these available so we can fill in the default upper and
340 lower bounds if the user requested slice doesn't provide that
341 information. Additionally unpacking the dimensions like this gives us
342 the inner element type. */
343 std::vector<struct type *> dim_types;
344 {
345 dim_types.reserve (ndimensions);
346 struct type *type = original_array_type;
347 for (int i = 0; i < ndimensions; ++i)
348 {
349 dim_types.push_back (type);
350 type = TYPE_TARGET_TYPE (type);
351 }
352 /* TYPE is now the inner element type of the array, we start the new
353 array slice off as this type, then as we process the requested slice
354 (from the user) we wrap new types around this to build up the final
355 slice type. */
356 inner_element_type = type;
357 }
358
359 /* As we analyse the new slice type we need to understand if the data
360 being referenced is contiguous. Do decide this we must track the size
361 of an element at each dimension of the new slice array. Initially the
362 elements of the inner most dimension of the array are the same inner
363 most elements as the original ARRAY. */
364 LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
365
366 /* Start off assuming all data is contiguous, this will be set to false
367 if access to any dimension results in non-contiguous data. */
368 bool is_all_contiguous = true;
369
370 /* The TOTAL_OFFSET is the distance in bytes from the start of the
371 original ARRAY to the start of the new slice. This is calculated as
372 we process the information from the user. */
373 LONGEST total_offset = 0;
374
375 /* A structure representing information about each dimension of the
376 resulting slice. */
377 struct slice_dim
378 {
379 /* Constructor. */
380 slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
381 : low (l),
382 high (h),
383 stride (s),
384 index (idx)
385 { /* Nothing. */ }
386
387 /* The low bound for this dimension of the slice. */
388 LONGEST low;
6d816919 389
a5c641b5
AB
390 /* The high bound for this dimension of the slice. */
391 LONGEST high;
6d816919 392
a5c641b5
AB
393 /* The byte stride for this dimension of the slice. */
394 LONGEST stride;
6d816919 395
a5c641b5
AB
396 struct type *index;
397 };
398
399 /* The dimensions of the resulting slice. */
400 std::vector<slice_dim> slice_dims;
401
402 /* Process the incoming arguments. These arguments are in the reverse
403 order to the array dimensions, that is the first argument refers to
404 the last array dimension. */
405 if (fortran_array_slicing_debug)
406 debug_printf ("Processing array access:\n");
407 for (int i = 0; i < nargs; ++i)
408 {
409 /* For each dimension of the array the user will have either provided
410 a ranged access with optional lower bound, upper bound, and
411 stride, or the user will have supplied a single index. */
412 struct type *dim_type = dim_types[ndimensions - (i + 1)];
413 if (exp->elts[*pos].opcode == OP_RANGE)
414 {
415 int pc = (*pos) + 1;
416 enum range_flag range_flag = (enum range_flag) exp->elts[pc].longconst;
417 *pos += 3;
418
419 LONGEST low, high, stride;
420 low = high = stride = 0;
421
422 if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
423 low = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
424 else
425 low = f77_get_lowerbound (dim_type);
426 if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
427 high = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
428 else
429 high = f77_get_upperbound (dim_type);
430 if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
431 stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
432 else
433 stride = 1;
434
435 if (stride == 0)
436 error (_("stride must not be 0"));
437
438 /* Get information about this dimension in the original ARRAY. */
439 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
440 struct type *index_type = dim_type->index_type ();
441 LONGEST lb = f77_get_lowerbound (dim_type);
442 LONGEST ub = f77_get_upperbound (dim_type);
443 LONGEST sd = index_type->bit_stride ();
444 if (sd == 0)
445 sd = TYPE_LENGTH (target_type) * 8;
446
447 if (fortran_array_slicing_debug)
448 {
449 debug_printf ("|-> Range access\n");
450 std::string str = type_to_string (dim_type);
451 debug_printf ("| |-> Type: %s\n", str.c_str ());
452 debug_printf ("| |-> Array:\n");
a5adb8f3
SM
453 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
454 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
455 debug_printf ("| | |-> Bit stride: %s\n", plongest (sd));
456 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8));
457 debug_printf ("| | |-> Type size: %s\n",
458 pulongest (TYPE_LENGTH (dim_type)));
459 debug_printf ("| | '-> Target type size: %s\n",
460 pulongest (TYPE_LENGTH (target_type)));
a5c641b5 461 debug_printf ("| |-> Accessing:\n");
a5adb8f3
SM
462 debug_printf ("| | |-> Low bound: %s\n",
463 plongest (low));
464 debug_printf ("| | |-> High bound: %s\n",
465 plongest (high));
466 debug_printf ("| | '-> Element stride: %s\n",
467 plongest (stride));
a5c641b5
AB
468 }
469
470 /* Check the user hasn't asked for something invalid. */
471 if (high > ub || low < lb)
472 error (_("array subscript out of bounds"));
473
474 /* Calculate what this dimension of the new slice array will look
475 like. OFFSET is the byte offset from the start of the
476 previous (more outer) dimension to the start of this
477 dimension. E_COUNT is the number of elements in this
478 dimension. REMAINDER is the number of elements remaining
479 between the last included element and the upper bound. For
480 example an access '1:6:2' will include elements 1, 3, 5 and
481 have a remainder of 1 (element #6). */
482 LONGEST lowest = std::min (low, high);
483 LONGEST offset = (sd / 8) * (lowest - lb);
484 LONGEST e_count = std::abs (high - low) + 1;
485 e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
486 LONGEST new_low = 1;
487 LONGEST new_high = new_low + e_count - 1;
488 LONGEST new_stride = (sd * stride) / 8;
489 LONGEST last_elem = low + ((e_count - 1) * stride);
490 LONGEST remainder = high - last_elem;
491 if (low > high)
492 {
493 offset += std::abs (remainder) * TYPE_LENGTH (target_type);
494 if (stride > 0)
495 error (_("incorrect stride and boundary combination"));
496 }
497 else if (stride < 0)
498 error (_("incorrect stride and boundary combination"));
499
500 /* Is the data within this dimension contiguous? It is if the
501 newly computed stride is the same size as a single element of
502 this dimension. */
503 bool is_dim_contiguous = (new_stride == slice_element_size);
504 is_all_contiguous &= is_dim_contiguous;
6d816919 505
a5c641b5
AB
506 if (fortran_array_slicing_debug)
507 {
508 debug_printf ("| '-> Results:\n");
a5adb8f3
SM
509 debug_printf ("| |-> Offset = %s\n", plongest (offset));
510 debug_printf ("| |-> Elements = %s\n", plongest (e_count));
511 debug_printf ("| |-> Low bound = %s\n", plongest (new_low));
512 debug_printf ("| |-> High bound = %s\n",
513 plongest (new_high));
514 debug_printf ("| |-> Byte stride = %s\n",
515 plongest (new_stride));
516 debug_printf ("| |-> Last element = %s\n",
517 plongest (last_elem));
518 debug_printf ("| |-> Remainder = %s\n",
519 plongest (remainder));
a5c641b5
AB
520 debug_printf ("| '-> Contiguous = %s\n",
521 (is_dim_contiguous ? "Yes" : "No"));
522 }
523
524 /* Figure out how big (in bytes) an element of this dimension of
525 the new array slice will be. */
526 slice_element_size = std::abs (new_stride * e_count);
6d816919 527
a5c641b5
AB
528 slice_dims.emplace_back (new_low, new_high, new_stride,
529 index_type);
530
531 /* Update the total offset. */
532 total_offset += offset;
533 }
534 else
535 {
536 /* There is a single index for this dimension. */
537 LONGEST index
538 = value_as_long (evaluate_subexp_with_coercion (exp, pos, noside));
539
540 /* Get information about this dimension in the original ARRAY. */
541 struct type *target_type = TYPE_TARGET_TYPE (dim_type);
542 struct type *index_type = dim_type->index_type ();
543 LONGEST lb = f77_get_lowerbound (dim_type);
544 LONGEST ub = f77_get_upperbound (dim_type);
545 LONGEST sd = index_type->bit_stride () / 8;
546 if (sd == 0)
547 sd = TYPE_LENGTH (target_type);
548
549 if (fortran_array_slicing_debug)
550 {
551 debug_printf ("|-> Index access\n");
552 std::string str = type_to_string (dim_type);
553 debug_printf ("| |-> Type: %s\n", str.c_str ());
554 debug_printf ("| |-> Array:\n");
a5adb8f3
SM
555 debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
556 debug_printf ("| | |-> High bound: %s\n", plongest (ub));
557 debug_printf ("| | |-> Byte stride: %s\n", plongest (sd));
558 debug_printf ("| | |-> Type size: %s\n",
559 pulongest (TYPE_LENGTH (dim_type)));
560 debug_printf ("| | '-> Target type size: %s\n",
561 pulongest (TYPE_LENGTH (target_type)));
a5c641b5 562 debug_printf ("| '-> Accessing:\n");
a5adb8f3
SM
563 debug_printf ("| '-> Index: %s\n",
564 plongest (index));
a5c641b5
AB
565 }
566
567 /* If the array has actual content then check the index is in
568 bounds. An array without content (an unbound array) doesn't
569 have a known upper bound, so don't error check in that
570 situation. */
571 if (index < lb
572 || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
573 && index > ub)
574 || (VALUE_LVAL (array) != lval_memory
575 && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
576 {
577 if (type_not_associated (dim_type))
578 error (_("no such vector element (vector not associated)"));
579 else if (type_not_allocated (dim_type))
580 error (_("no such vector element (vector not allocated)"));
581 else
582 error (_("no such vector element"));
583 }
584
585 /* Calculate using the type stride, not the target type size. */
586 LONGEST offset = sd * (index - lb);
587 total_offset += offset;
588 }
589 }
590
591 if (noside == EVAL_SKIP)
592 return array;
6d816919 593
a5c641b5
AB
594 /* Build a type that represents the new array slice in the target memory
595 of the original ARRAY, this type makes use of strides to correctly
596 find only those elements that are part of the new slice. */
597 struct type *array_slice_type = inner_element_type;
598 for (const auto &d : slice_dims)
6d816919 599 {
a5c641b5
AB
600 /* Create the range. */
601 dynamic_prop p_low, p_high, p_stride;
602
603 p_low.set_const_val (d.low);
604 p_high.set_const_val (d.high);
605 p_stride.set_const_val (d.stride);
606
607 struct type *new_range
608 = create_range_type_with_stride ((struct type *) NULL,
609 TYPE_TARGET_TYPE (d.index),
610 &p_low, &p_high, 0, &p_stride,
611 true);
612 array_slice_type
613 = create_array_type (nullptr, array_slice_type, new_range);
614 }
6d816919 615
a5c641b5
AB
616 if (fortran_array_slicing_debug)
617 {
618 debug_printf ("'-> Final result:\n");
619 debug_printf (" |-> Type: %s\n",
620 type_to_string (array_slice_type).c_str ());
a5adb8f3
SM
621 debug_printf (" |-> Total offset: %s\n",
622 plongest (total_offset));
a5c641b5
AB
623 debug_printf (" |-> Base address: %s\n",
624 core_addr_to_string (value_address (array)));
625 debug_printf (" '-> Contiguous = %s\n",
626 (is_all_contiguous ? "Yes" : "No"));
6d816919
AB
627 }
628
a5c641b5
AB
629 /* Should we repack this array slice? */
630 if (!is_all_contiguous && (repack_array_slices || is_string_p))
6d816919 631 {
a5c641b5
AB
632 /* Build a type for the repacked slice. */
633 struct type *repacked_array_type = inner_element_type;
634 for (const auto &d : slice_dims)
635 {
636 /* Create the range. */
637 dynamic_prop p_low, p_high, p_stride;
638
639 p_low.set_const_val (d.low);
640 p_high.set_const_val (d.high);
641 p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
642
643 struct type *new_range
644 = create_range_type_with_stride ((struct type *) NULL,
645 TYPE_TARGET_TYPE (d.index),
646 &p_low, &p_high, 0, &p_stride,
647 true);
648 repacked_array_type
649 = create_array_type (nullptr, repacked_array_type, new_range);
650 }
6d816919 651
a5c641b5
AB
652 /* Now copy the elements from the original ARRAY into the packed
653 array value DEST. */
654 struct value *dest = allocate_value (repacked_array_type);
655 if (value_lazy (array)
656 || (total_offset + TYPE_LENGTH (array_slice_type)
657 > TYPE_LENGTH (check_typedef (value_type (array)))))
658 {
659 fortran_array_walker<fortran_lazy_array_repacker_impl> p
660 (array_slice_type, value_address (array) + total_offset, dest);
661 p.walk ();
662 }
663 else
664 {
665 fortran_array_walker<fortran_array_repacker_impl> p
666 (array_slice_type, value_address (array) + total_offset,
667 total_offset, array, dest);
668 p.walk ();
669 }
670 array = dest;
671 }
672 else
673 {
674 if (VALUE_LVAL (array) == lval_memory)
675 {
676 /* If the value we're taking a slice from is not yet loaded, or
677 the requested slice is outside the values content range then
678 just create a new lazy value pointing at the memory where the
679 contents we're looking for exist. */
680 if (value_lazy (array)
681 || (total_offset + TYPE_LENGTH (array_slice_type)
682 > TYPE_LENGTH (check_typedef (value_type (array)))))
683 array = value_at_lazy (array_slice_type,
684 value_address (array) + total_offset);
685 else
686 array = value_from_contents_and_address (array_slice_type,
687 (value_contents (array)
688 + total_offset),
689 (value_address (array)
690 + total_offset));
691 }
692 else if (!value_lazy (array))
e3436813 693 array = value_from_component (array, array_slice_type, total_offset);
a5c641b5
AB
694 else
695 error (_("cannot subscript arrays that are not in memory"));
6d816919
AB
696 }
697
698 return array;
699}
700
9dad4a58 701/* Special expression evaluation cases for Fortran. */
cb8c24b6
SM
702
703static struct value *
9dad4a58
AB
704evaluate_subexp_f (struct type *expect_type, struct expression *exp,
705 int *pos, enum noside noside)
706{
b6d03bb2 707 struct value *arg1 = NULL, *arg2 = NULL;
4d00f5d8
AB
708 enum exp_opcode op;
709 int pc;
710 struct type *type;
711
712 pc = *pos;
713 *pos += 1;
714 op = exp->elts[pc].opcode;
715
716 switch (op)
717 {
718 default:
719 *pos -= 1;
720 return evaluate_subexp_standard (expect_type, exp, pos, noside);
721
0841c79a 722 case UNOP_ABS:
fe1fe7ea 723 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
0841c79a
AB
724 if (noside == EVAL_SKIP)
725 return eval_skip_value (exp);
726 type = value_type (arg1);
78134374 727 switch (type->code ())
0841c79a
AB
728 {
729 case TYPE_CODE_FLT:
730 {
731 double d
732 = fabs (target_float_to_host_double (value_contents (arg1),
733 value_type (arg1)));
734 return value_from_host_double (type, d);
735 }
736 case TYPE_CODE_INT:
737 {
738 LONGEST l = value_as_long (arg1);
739 l = llabs (l);
740 return value_from_longest (type, l);
741 }
742 }
743 error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
744
b6d03bb2 745 case BINOP_MOD:
fe1fe7ea 746 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
747 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
748 if (noside == EVAL_SKIP)
749 return eval_skip_value (exp);
750 type = value_type (arg1);
78134374 751 if (type->code () != value_type (arg2)->code ())
b6d03bb2 752 error (_("non-matching types for parameters to MOD ()"));
78134374 753 switch (type->code ())
b6d03bb2
AB
754 {
755 case TYPE_CODE_FLT:
756 {
757 double d1
758 = target_float_to_host_double (value_contents (arg1),
759 value_type (arg1));
760 double d2
761 = target_float_to_host_double (value_contents (arg2),
762 value_type (arg2));
763 double d3 = fmod (d1, d2);
764 return value_from_host_double (type, d3);
765 }
766 case TYPE_CODE_INT:
767 {
768 LONGEST v1 = value_as_long (arg1);
769 LONGEST v2 = value_as_long (arg2);
770 if (v2 == 0)
771 error (_("calling MOD (N, 0) is undefined"));
772 LONGEST v3 = v1 - (v1 / v2) * v2;
773 return value_from_longest (value_type (arg1), v3);
774 }
775 }
776 error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
777
778 case UNOP_FORTRAN_CEILING:
779 {
fe1fe7ea 780 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
781 if (noside == EVAL_SKIP)
782 return eval_skip_value (exp);
783 type = value_type (arg1);
78134374 784 if (type->code () != TYPE_CODE_FLT)
b6d03bb2
AB
785 error (_("argument to CEILING must be of type float"));
786 double val
787 = target_float_to_host_double (value_contents (arg1),
788 value_type (arg1));
789 val = ceil (val);
790 return value_from_host_double (type, val);
791 }
792
793 case UNOP_FORTRAN_FLOOR:
794 {
fe1fe7ea 795 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
796 if (noside == EVAL_SKIP)
797 return eval_skip_value (exp);
798 type = value_type (arg1);
78134374 799 if (type->code () != TYPE_CODE_FLT)
b6d03bb2
AB
800 error (_("argument to FLOOR must be of type float"));
801 double val
802 = target_float_to_host_double (value_contents (arg1),
803 value_type (arg1));
804 val = floor (val);
805 return value_from_host_double (type, val);
806 }
807
808 case BINOP_FORTRAN_MODULO:
809 {
fe1fe7ea 810 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
811 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
812 if (noside == EVAL_SKIP)
813 return eval_skip_value (exp);
814 type = value_type (arg1);
78134374 815 if (type->code () != value_type (arg2)->code ())
b6d03bb2 816 error (_("non-matching types for parameters to MODULO ()"));
dda83cd7 817 /* MODULO(A, P) = A - FLOOR (A / P) * P */
78134374 818 switch (type->code ())
b6d03bb2
AB
819 {
820 case TYPE_CODE_INT:
821 {
822 LONGEST a = value_as_long (arg1);
823 LONGEST p = value_as_long (arg2);
824 LONGEST result = a - (a / p) * p;
825 if (result != 0 && (a < 0) != (p < 0))
826 result += p;
827 return value_from_longest (value_type (arg1), result);
828 }
829 case TYPE_CODE_FLT:
830 {
831 double a
832 = target_float_to_host_double (value_contents (arg1),
833 value_type (arg1));
834 double p
835 = target_float_to_host_double (value_contents (arg2),
836 value_type (arg2));
837 double result = fmod (a, p);
838 if (result != 0 && (a < 0.0) != (p < 0.0))
839 result += p;
840 return value_from_host_double (type, result);
841 }
842 }
843 error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
844 }
845
846 case BINOP_FORTRAN_CMPLX:
fe1fe7ea 847 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
848 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
849 if (noside == EVAL_SKIP)
850 return eval_skip_value (exp);
851 type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
852 return value_literal_complex (arg1, arg2, type);
853
83228e93 854 case UNOP_FORTRAN_KIND:
4d00f5d8
AB
855 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
856 type = value_type (arg1);
857
78134374 858 switch (type->code ())
dda83cd7
SM
859 {
860 case TYPE_CODE_STRUCT:
861 case TYPE_CODE_UNION:
862 case TYPE_CODE_MODULE:
863 case TYPE_CODE_FUNC:
864 error (_("argument to kind must be an intrinsic type"));
865 }
4d00f5d8
AB
866
867 if (!TYPE_TARGET_TYPE (type))
dda83cd7 868 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
4d00f5d8
AB
869 TYPE_LENGTH (type));
870 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
78134374 871 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
6d816919
AB
872
873
874 case OP_F77_UNDETERMINED_ARGLIST:
875 /* Remember that in F77, functions, substring ops and array subscript
dda83cd7
SM
876 operations cannot be disambiguated at parse time. We have made
877 all array subscript operations, substring operations as well as
878 function calls come here and we now have to discover what the heck
879 this thing actually was. If it is a function, we process just as
880 if we got an OP_FUNCALL. */
6d816919
AB
881 int nargs = longest_to_int (exp->elts[pc + 1].longconst);
882 (*pos) += 2;
883
884 /* First determine the type code we are dealing with. */
885 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
886 type = check_typedef (value_type (arg1));
887 enum type_code code = type->code ();
888
889 if (code == TYPE_CODE_PTR)
890 {
891 /* Fortran always passes variable to subroutines as pointer.
892 So we need to look into its target type to see if it is
893 array, string or function. If it is, we need to switch
894 to the target value the original one points to. */
895 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
896
897 if (target_type->code () == TYPE_CODE_ARRAY
898 || target_type->code () == TYPE_CODE_STRING
899 || target_type->code () == TYPE_CODE_FUNC)
900 {
901 arg1 = value_ind (arg1);
902 type = check_typedef (value_type (arg1));
903 code = type->code ();
904 }
905 }
906
907 switch (code)
908 {
909 case TYPE_CODE_ARRAY:
910 case TYPE_CODE_STRING:
911 return fortran_value_subarray (arg1, exp, pos, nargs, noside);
912
913 case TYPE_CODE_PTR:
914 case TYPE_CODE_FUNC:
915 case TYPE_CODE_INTERNAL_FUNCTION:
916 {
917 /* It's a function call. Allocate arg vector, including
918 space for the function to be called in argvec[0] and a
919 termination NULL. */
920 struct value **argvec = (struct value **)
921 alloca (sizeof (struct value *) * (nargs + 2));
922 argvec[0] = arg1;
923 int tem = 1;
924 for (; tem <= nargs; tem++)
925 {
926 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
927 /* Arguments in Fortran are passed by address. Coerce the
928 arguments here rather than in value_arg_coerce as
929 otherwise the call to malloc to place the non-lvalue
930 parameters in target memory is hit by this Fortran
931 specific logic. This results in malloc being called
932 with a pointer to an integer followed by an attempt to
933 malloc the arguments to malloc in target memory.
934 Infinite recursion ensues. */
935 if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
936 {
937 bool is_artificial
938 = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
939 argvec[tem] = fortran_argument_convert (argvec[tem],
940 is_artificial);
941 }
942 }
943 argvec[tem] = 0; /* signal end of arglist */
944 if (noside == EVAL_SKIP)
945 return eval_skip_value (exp);
1ab8280d
TT
946 return evaluate_subexp_do_call (exp, noside, argvec[0],
947 gdb::make_array_view (argvec + 1,
948 nargs),
949 NULL, expect_type);
6d816919
AB
950 }
951
952 default:
953 error (_("Cannot perform substring on this type"));
954 }
4d00f5d8
AB
955 }
956
957 /* Should be unreachable. */
958 return nullptr;
9dad4a58
AB
959}
960
83228e93
AB
961/* Special expression lengths for Fortran. */
962
963static void
964operator_length_f (const struct expression *exp, int pc, int *oplenp,
965 int *argsp)
966{
967 int oplen = 1;
968 int args = 0;
969
970 switch (exp->elts[pc - 1].opcode)
971 {
972 default:
973 operator_length_standard (exp, pc, oplenp, argsp);
974 return;
975
976 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
977 case UNOP_FORTRAN_FLOOR:
978 case UNOP_FORTRAN_CEILING:
83228e93
AB
979 oplen = 1;
980 args = 1;
981 break;
b6d03bb2
AB
982
983 case BINOP_FORTRAN_CMPLX:
984 case BINOP_FORTRAN_MODULO:
985 oplen = 1;
986 args = 2;
987 break;
6d816919
AB
988
989 case OP_F77_UNDETERMINED_ARGLIST:
990 oplen = 3;
991 args = 1 + longest_to_int (exp->elts[pc - 2].longconst);
992 break;
83228e93
AB
993 }
994
995 *oplenp = oplen;
996 *argsp = args;
997}
998
b6d03bb2
AB
999/* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
1000 the extra argument NAME which is the text that should be printed as the
1001 name of this operation. */
1002
1003static void
1004print_unop_subexp_f (struct expression *exp, int *pos,
1005 struct ui_file *stream, enum precedence prec,
1006 const char *name)
1007{
1008 (*pos)++;
1009 fprintf_filtered (stream, "%s(", name);
1010 print_subexp (exp, pos, stream, PREC_SUFFIX);
1011 fputs_filtered (")", stream);
1012}
1013
1014/* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
1015 the extra argument NAME which is the text that should be printed as the
1016 name of this operation. */
1017
1018static void
1019print_binop_subexp_f (struct expression *exp, int *pos,
1020 struct ui_file *stream, enum precedence prec,
1021 const char *name)
1022{
1023 (*pos)++;
1024 fprintf_filtered (stream, "%s(", name);
1025 print_subexp (exp, pos, stream, PREC_SUFFIX);
1026 fputs_filtered (",", stream);
1027 print_subexp (exp, pos, stream, PREC_SUFFIX);
1028 fputs_filtered (")", stream);
1029}
1030
83228e93
AB
1031/* Special expression printing for Fortran. */
1032
1033static void
1034print_subexp_f (struct expression *exp, int *pos,
1035 struct ui_file *stream, enum precedence prec)
1036{
1037 int pc = *pos;
1038 enum exp_opcode op = exp->elts[pc].opcode;
1039
1040 switch (op)
1041 {
1042 default:
1043 print_subexp_standard (exp, pos, stream, prec);
1044 return;
1045
1046 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
1047 print_unop_subexp_f (exp, pos, stream, prec, "KIND");
1048 return;
1049
1050 case UNOP_FORTRAN_FLOOR:
1051 print_unop_subexp_f (exp, pos, stream, prec, "FLOOR");
1052 return;
1053
1054 case UNOP_FORTRAN_CEILING:
1055 print_unop_subexp_f (exp, pos, stream, prec, "CEILING");
1056 return;
1057
1058 case BINOP_FORTRAN_CMPLX:
1059 print_binop_subexp_f (exp, pos, stream, prec, "CMPLX");
1060 return;
1061
1062 case BINOP_FORTRAN_MODULO:
1063 print_binop_subexp_f (exp, pos, stream, prec, "MODULO");
83228e93 1064 return;
6d816919
AB
1065
1066 case OP_F77_UNDETERMINED_ARGLIST:
86775fab 1067 (*pos)++;
6d816919
AB
1068 print_subexp_funcall (exp, pos, stream);
1069 return;
83228e93
AB
1070 }
1071}
1072
83228e93
AB
1073/* Special expression dumping for Fortran. */
1074
1075static int
1076dump_subexp_body_f (struct expression *exp,
1077 struct ui_file *stream, int elt)
1078{
1079 int opcode = exp->elts[elt].opcode;
1080 int oplen, nargs, i;
1081
1082 switch (opcode)
1083 {
1084 default:
1085 return dump_subexp_body_standard (exp, stream, elt);
1086
1087 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
1088 case UNOP_FORTRAN_FLOOR:
1089 case UNOP_FORTRAN_CEILING:
1090 case BINOP_FORTRAN_CMPLX:
1091 case BINOP_FORTRAN_MODULO:
83228e93
AB
1092 operator_length_f (exp, (elt + 1), &oplen, &nargs);
1093 break;
6d816919
AB
1094
1095 case OP_F77_UNDETERMINED_ARGLIST:
86775fab 1096 return dump_subexp_body_funcall (exp, stream, elt + 1);
83228e93
AB
1097 }
1098
1099 elt += oplen;
1100 for (i = 0; i < nargs; i += 1)
1101 elt = dump_subexp (exp, stream, elt);
1102
1103 return elt;
1104}
1105
1106/* Special expression checking for Fortran. */
1107
1108static int
1109operator_check_f (struct expression *exp, int pos,
1110 int (*objfile_func) (struct objfile *objfile,
1111 void *data),
1112 void *data)
1113{
1114 const union exp_element *const elts = exp->elts;
1115
1116 switch (elts[pos].opcode)
1117 {
1118 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
1119 case UNOP_FORTRAN_FLOOR:
1120 case UNOP_FORTRAN_CEILING:
1121 case BINOP_FORTRAN_CMPLX:
1122 case BINOP_FORTRAN_MODULO:
83228e93
AB
1123 /* Any references to objfiles are held in the arguments to this
1124 expression, not within the expression itself, so no additional
1125 checking is required here, the outer expression iteration code
1126 will take care of checking each argument. */
1127 break;
1128
1129 default:
1130 return operator_check_standard (exp, pos, objfile_func, data);
1131 }
1132
1133 return 0;
1134}
1135
9dad4a58 1136/* Expression processing for Fortran. */
1a0ea399 1137const struct exp_descriptor f_language::exp_descriptor_tab =
9dad4a58 1138{
83228e93
AB
1139 print_subexp_f,
1140 operator_length_f,
1141 operator_check_f,
83228e93 1142 dump_subexp_body_f,
9dad4a58
AB
1143 evaluate_subexp_f
1144};
1145
1a0ea399 1146/* See language.h. */
0874fd07 1147
1a0ea399
AB
1148void
1149f_language::language_arch_info (struct gdbarch *gdbarch,
1150 struct language_arch_info *lai) const
0874fd07 1151{
1a0ea399
AB
1152 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
1153
7bea47f0
AB
1154 /* Helper function to allow shorter lines below. */
1155 auto add = [&] (struct type * t)
1156 {
1157 lai->add_primitive_type (t);
1158 };
1159
1160 add (builtin->builtin_character);
1161 add (builtin->builtin_logical);
1162 add (builtin->builtin_logical_s1);
1163 add (builtin->builtin_logical_s2);
1164 add (builtin->builtin_logical_s8);
1165 add (builtin->builtin_real);
1166 add (builtin->builtin_real_s8);
1167 add (builtin->builtin_real_s16);
1168 add (builtin->builtin_complex_s8);
1169 add (builtin->builtin_complex_s16);
1170 add (builtin->builtin_void);
1171
1172 lai->set_string_char_type (builtin->builtin_character);
1173 lai->set_bool_type (builtin->builtin_logical_s2, "logical");
1a0ea399 1174}
5aba6ebe 1175
1a0ea399 1176/* See language.h. */
5aba6ebe 1177
1a0ea399
AB
1178unsigned int
1179f_language::search_name_hash (const char *name) const
1180{
1181 return cp_search_name_hash (name);
1182}
b7c6e27d 1183
1a0ea399 1184/* See language.h. */
b7c6e27d 1185
1a0ea399
AB
1186struct block_symbol
1187f_language::lookup_symbol_nonlocal (const char *name,
1188 const struct block *block,
1189 const domain_enum domain) const
1190{
1191 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1192}
c9debfb9 1193
1a0ea399 1194/* See language.h. */
c9debfb9 1195
1a0ea399
AB
1196symbol_name_matcher_ftype *
1197f_language::get_symbol_name_matcher_inner
1198 (const lookup_name_info &lookup_name) const
1199{
1200 return cp_get_symbol_name_matcher (lookup_name);
1201}
0874fd07
AB
1202
1203/* Single instance of the Fortran language class. */
1204
1205static f_language f_language_defn;
1206
54ef06c7
UW
1207static void *
1208build_fortran_types (struct gdbarch *gdbarch)
c906108c 1209{
54ef06c7
UW
1210 struct builtin_f_type *builtin_f_type
1211 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
1212
e9bb382b 1213 builtin_f_type->builtin_void
bbe75b9d 1214 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
e9bb382b
UW
1215
1216 builtin_f_type->builtin_character
4a270568 1217 = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
e9bb382b
UW
1218
1219 builtin_f_type->builtin_logical_s1
1220 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
1221
1222 builtin_f_type->builtin_integer_s2
1223 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
1224 "integer*2");
1225
067630bd
AB
1226 builtin_f_type->builtin_integer_s8
1227 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
1228 "integer*8");
1229
e9bb382b
UW
1230 builtin_f_type->builtin_logical_s2
1231 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
1232 "logical*2");
1233
ce4b0682
SDJ
1234 builtin_f_type->builtin_logical_s8
1235 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
1236 "logical*8");
1237
e9bb382b
UW
1238 builtin_f_type->builtin_integer
1239 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
1240 "integer");
1241
1242 builtin_f_type->builtin_logical
1243 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
1244 "logical*4");
1245
1246 builtin_f_type->builtin_real
1247 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
49f190bc 1248 "real", gdbarch_float_format (gdbarch));
e9bb382b
UW
1249 builtin_f_type->builtin_real_s8
1250 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
49f190bc 1251 "real*8", gdbarch_double_format (gdbarch));
34d11c68 1252 auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
dc42e902
AB
1253 if (fmt != nullptr)
1254 builtin_f_type->builtin_real_s16
1255 = arch_float_type (gdbarch, 128, "real*16", fmt);
1256 else if (gdbarch_long_double_bit (gdbarch) == 128)
1257 builtin_f_type->builtin_real_s16
1258 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
1259 "real*16", gdbarch_long_double_format (gdbarch));
1260 else
1261 builtin_f_type->builtin_real_s16
1262 = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
e9bb382b
UW
1263
1264 builtin_f_type->builtin_complex_s8
5b930b45 1265 = init_complex_type ("complex*8", builtin_f_type->builtin_real);
e9bb382b 1266 builtin_f_type->builtin_complex_s16
5b930b45 1267 = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
0830d301 1268
78134374 1269 if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
0830d301
TT
1270 builtin_f_type->builtin_complex_s32
1271 = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
1272 else
1273 builtin_f_type->builtin_complex_s32
1274 = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
54ef06c7
UW
1275
1276 return builtin_f_type;
1277}
1278
1279static struct gdbarch_data *f_type_data;
1280
1281const struct builtin_f_type *
1282builtin_f_type (struct gdbarch *gdbarch)
1283{
9a3c8263 1284 return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
4e845cd3
MS
1285}
1286
a5c641b5
AB
1287/* Command-list for the "set/show fortran" prefix command. */
1288static struct cmd_list_element *set_fortran_list;
1289static struct cmd_list_element *show_fortran_list;
1290
6c265988 1291void _initialize_f_language ();
4e845cd3 1292void
6c265988 1293_initialize_f_language ()
4e845cd3 1294{
54ef06c7 1295 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
a5c641b5
AB
1296
1297 add_basic_prefix_cmd ("fortran", no_class,
1298 _("Prefix command for changing Fortran-specific settings."),
1299 &set_fortran_list, "set fortran ", 0, &setlist);
1300
1301 add_show_prefix_cmd ("fortran", no_class,
1302 _("Generic command for showing Fortran-specific settings."),
1303 &show_fortran_list, "show fortran ", 0, &showlist);
1304
1305 add_setshow_boolean_cmd ("repack-array-slices", class_vars,
1306 &repack_array_slices, _("\
1307Enable or disable repacking of non-contiguous array slices."), _("\
1308Show whether non-contiguous array slices are repacked."), _("\
1309When the user requests a slice of a Fortran array then we can either return\n\
1310a descriptor that describes the array in place (using the original array data\n\
1311in its existing location) or the original data can be repacked (copied) to a\n\
1312new location.\n\
1313\n\
1314When the content of the array slice is contiguous within the original array\n\
1315then the result will never be repacked, but when the data for the new array\n\
1316is non-contiguous within the original array repacking will only be performed\n\
1317when this setting is on."),
1318 NULL,
1319 show_repack_array_slices,
1320 &set_fortran_list, &show_fortran_list);
1321
1322 /* Debug Fortran's array slicing logic. */
1323 add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
1324 &fortran_array_slicing_debug, _("\
1325Set debugging of Fortran array slicing."), _("\
1326Show debugging of Fortran array slicing."), _("\
1327When on, debugging of Fortran array slicing is enabled."),
1328 NULL,
1329 show_fortran_array_slicing_debug,
1330 &setdebuglist, &showdebuglist);
c906108c 1331}
aa3cfbda 1332
5a7cf527
AB
1333/* Ensures that function argument VALUE is in the appropriate form to
1334 pass to a Fortran function. Returns a possibly new value that should
1335 be used instead of VALUE.
1336
1337 When IS_ARTIFICIAL is true this indicates an artificial argument,
1338 e.g. hidden string lengths which the GNU Fortran argument passing
1339 convention specifies as being passed by value.
aa3cfbda 1340
5a7cf527
AB
1341 When IS_ARTIFICIAL is false, the argument is passed by pointer. If the
1342 value is already in target memory then return a value that is a pointer
1343 to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate
1344 space in the target, copy VALUE in, and return a pointer to the in
1345 memory copy. */
1346
1347static struct value *
aa3cfbda
RB
1348fortran_argument_convert (struct value *value, bool is_artificial)
1349{
1350 if (!is_artificial)
1351 {
1352 /* If the value is not in the inferior e.g. registers values,
1353 convenience variables and user input. */
1354 if (VALUE_LVAL (value) != lval_memory)
1355 {
1356 struct type *type = value_type (value);
1357 const int length = TYPE_LENGTH (type);
1358 const CORE_ADDR addr
1359 = value_as_long (value_allocate_space_in_inferior (length));
1360 write_memory (addr, value_contents (value), length);
1361 struct value *val
1362 = value_from_contents_and_address (type, value_contents (value),
1363 addr);
1364 return value_addr (val);
1365 }
1366 else
1367 return value_addr (value); /* Program variables, e.g. arrays. */
1368 }
1369 return value;
1370}
1371
1372/* See f-lang.h. */
1373
1374struct type *
1375fortran_preserve_arg_pointer (struct value *arg, struct type *type)
1376{
78134374 1377 if (value_type (arg)->code () == TYPE_CODE_PTR)
aa3cfbda
RB
1378 return value_type (arg);
1379 return type;
1380}
a5c641b5
AB
1381
1382/* See f-lang.h. */
1383
1384CORE_ADDR
1385fortran_adjust_dynamic_array_base_address_hack (struct type *type,
1386 CORE_ADDR address)
1387{
1388 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1389
b7874836
AB
1390 /* We can't adjust the base address for arrays that have no content. */
1391 if (type_not_allocated (type) || type_not_associated (type))
1392 return address;
1393
a5c641b5
AB
1394 int ndimensions = calc_f77_array_dims (type);
1395 LONGEST total_offset = 0;
1396
1397 /* Walk through each of the dimensions of this array type and figure out
1398 if any of the dimensions are "backwards", that is the base address
1399 for this dimension points to the element at the highest memory
1400 address and the stride is negative. */
1401 struct type *tmp_type = type;
1402 for (int i = 0 ; i < ndimensions; ++i)
1403 {
1404 /* Grab the range for this dimension and extract the lower and upper
1405 bounds. */
1406 tmp_type = check_typedef (tmp_type);
1407 struct type *range_type = tmp_type->index_type ();
1408 LONGEST lowerbound, upperbound, stride;
1f8d2881 1409 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
a5c641b5
AB
1410 error ("failed to get range bounds");
1411
1412 /* Figure out the stride for this dimension. */
1413 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
1414 stride = tmp_type->index_type ()->bounds ()->bit_stride ();
1415 if (stride == 0)
1416 stride = type_length_units (elt_type);
1417 else
1418 {
8ee511af
SM
1419 int unit_size
1420 = gdbarch_addressable_memory_unit_size (elt_type->arch ());
a5c641b5
AB
1421 stride /= (unit_size * 8);
1422 }
1423
1424 /* If this dimension is "backward" then figure out the offset
1425 adjustment required to point to the element at the lowest memory
1426 address, and add this to the total offset. */
1427 LONGEST offset = 0;
1428 if (stride < 0 && lowerbound < upperbound)
1429 offset = (upperbound - lowerbound) * stride;
1430 total_offset += offset;
1431 tmp_type = TYPE_TARGET_TYPE (tmp_type);
1432 }
1433
1434 /* Adjust the address of this object and return it. */
1435 address += total_offset;
1436 return address;
1437}
This page took 1.867132 seconds and 4 git commands to generate.