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