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