]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/f-lang.c
gas, arm: PR26858 Fix availability of single precision vmul/vmla in arm mode
[thirdparty/binutils-gdb.git] / gdb / f-lang.c
CommitLineData
c906108c 1/* Fortran language support routines for GDB, the GNU debugger.
ce27fb25 2
b811d2c2 3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
ce27fb25 4
c906108c
SS
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
4de283e4 24#include "symtab.h"
d55e5aa6 25#include "gdbtypes.h"
4de283e4 26#include "expression.h"
d55e5aa6 27#include "parser-defs.h"
4de283e4
TT
28#include "language.h"
29#include "varobj.h"
30#include "gdbcore.h"
31#include "f-lang.h"
745b8ca0 32#include "valprint.h"
5f9a71c3 33#include "value.h"
4de283e4
TT
34#include "cp-support.h"
35#include "charset.h"
36#include "c-lang.h"
37#include "target-float.h"
0d12e84c 38#include "gdbarch.h"
4de283e4
TT
39
40#include <math.h>
c906108c 41
c906108c
SS
42/* Local functions */
43
3b2b8fea
TT
44/* Return the encoding that should be used for the character type
45 TYPE. */
46
1a0ea399
AB
47const char *
48f_language::get_encoding (struct type *type)
3b2b8fea
TT
49{
50 const char *encoding;
51
52 switch (TYPE_LENGTH (type))
53 {
54 case 1:
55 encoding = target_charset (get_type_arch (type));
56 break;
57 case 4:
34877895 58 if (type_byte_order (type) == BFD_ENDIAN_BIG)
3b2b8fea
TT
59 encoding = "UTF-32BE";
60 else
61 encoding = "UTF-32LE";
62 break;
63
64 default:
65 error (_("unrecognized character type"));
66 }
67
68 return encoding;
69}
70
c906108c 71\f
c5aa993b 72
c906108c
SS
73/* Table of operators and their precedences for printing expressions. */
74
1a0ea399 75const struct op_print f_language::op_print_tab[] =
c5aa993b
JM
76{
77 {"+", BINOP_ADD, PREC_ADD, 0},
78 {"+", UNOP_PLUS, PREC_PREFIX, 0},
79 {"-", BINOP_SUB, PREC_ADD, 0},
80 {"-", UNOP_NEG, PREC_PREFIX, 0},
81 {"*", BINOP_MUL, PREC_MUL, 0},
82 {"/", BINOP_DIV, PREC_MUL, 0},
83 {"DIV", BINOP_INTDIV, PREC_MUL, 0},
84 {"MOD", BINOP_REM, PREC_MUL, 0},
85 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
86 {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
87 {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
88 {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
89 {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
90 {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
91 {".LE.", BINOP_LEQ, PREC_ORDER, 0},
92 {".GE.", BINOP_GEQ, PREC_ORDER, 0},
93 {".GT.", BINOP_GTR, PREC_ORDER, 0},
94 {".LT.", BINOP_LESS, PREC_ORDER, 0},
95 {"**", UNOP_IND, PREC_PREFIX, 0},
96 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
f486487f 97 {NULL, OP_NULL, PREC_REPEAT, 0}
c906108c
SS
98};
99\f
c906108c 100
6d816919
AB
101/* Called from fortran_value_subarray to take a slice of an array or a
102 string. ARRAY is the array or string to be accessed. EXP, POS, and
103 NOSIDE are as for evaluate_subexp_standard. Return a value that is a
104 slice of the array. */
105
106static struct value *
107value_f90_subarray (struct value *array,
108 struct expression *exp, int *pos, enum noside noside)
109{
110 int pc = (*pos) + 1;
6b4c676c 111 LONGEST low_bound, high_bound, stride;
6d816919 112 struct type *range = check_typedef (value_type (array)->index_type ());
f2d8e4c5
AB
113 enum range_flag range_flag
114 = (enum range_flag) longest_to_int (exp->elts[pc].longconst);
6d816919
AB
115
116 *pos += 3;
117
f2d8e4c5 118 if (range_flag & RANGE_LOW_BOUND_DEFAULT)
6d816919
AB
119 low_bound = range->bounds ()->low.const_val ();
120 else
121 low_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
122
f2d8e4c5 123 if (range_flag & RANGE_HIGH_BOUND_DEFAULT)
6d816919
AB
124 high_bound = range->bounds ()->high.const_val ();
125 else
126 high_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
127
6b4c676c
AB
128 if (range_flag & RANGE_HAS_STRIDE)
129 stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
130 else
131 stride = 1;
132
133 if (stride != 1)
134 error (_("Fortran array strides are not currently supported"));
135
6d816919
AB
136 return value_slice (array, low_bound, high_bound - low_bound + 1);
137}
138
139/* Helper for skipping all the arguments in an undetermined argument list.
140 This function was designed for use in the OP_F77_UNDETERMINED_ARGLIST
141 case of evaluate_subexp_standard as multiple, but not all, code paths
142 require a generic skip. */
143
144static void
145skip_undetermined_arglist (int nargs, struct expression *exp, int *pos,
146 enum noside noside)
147{
148 for (int i = 0; i < nargs; ++i)
149 evaluate_subexp (nullptr, exp, pos, noside);
150}
151
152/* Return the number of dimensions for a Fortran array or string. */
153
154int
155calc_f77_array_dims (struct type *array_type)
156{
157 int ndimen = 1;
158 struct type *tmp_type;
159
160 if ((array_type->code () == TYPE_CODE_STRING))
161 return 1;
162
163 if ((array_type->code () != TYPE_CODE_ARRAY))
164 error (_("Can't get dimensions for a non-array type"));
165
166 tmp_type = array_type;
167
168 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
169 {
170 if (tmp_type->code () == TYPE_CODE_ARRAY)
171 ++ndimen;
172 }
173 return ndimen;
174}
175
176/* Called from evaluate_subexp_standard to perform array indexing, and
177 sub-range extraction, for Fortran. As well as arrays this function
178 also handles strings as they can be treated like arrays of characters.
179 ARRAY is the array or string being accessed. EXP, POS, and NOSIDE are
180 as for evaluate_subexp_standard, and NARGS is the number of arguments
181 in this access (e.g. 'array (1,2,3)' would be NARGS 3). */
182
183static struct value *
184fortran_value_subarray (struct value *array, struct expression *exp,
185 int *pos, int nargs, enum noside noside)
186{
187 if (exp->elts[*pos].opcode == OP_RANGE)
188 return value_f90_subarray (array, exp, pos, noside);
189
190 if (noside == EVAL_SKIP)
191 {
192 skip_undetermined_arglist (nargs, exp, pos, noside);
193 /* Return the dummy value with the correct type. */
194 return array;
195 }
196
197 LONGEST subscript_array[MAX_FORTRAN_DIMS];
198 int ndimensions = 1;
199 struct type *type = check_typedef (value_type (array));
200
201 if (nargs > MAX_FORTRAN_DIMS)
202 error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
203
204 ndimensions = calc_f77_array_dims (type);
205
206 if (nargs != ndimensions)
207 error (_("Wrong number of subscripts"));
208
209 gdb_assert (nargs > 0);
210
211 /* Now that we know we have a legal array subscript expression let us
212 actually find out where this element exists in the array. */
213
214 /* Take array indices left to right. */
215 for (int i = 0; i < nargs; i++)
216 {
217 /* Evaluate each subscript; it must be a legal integer in F77. */
218 value *arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
219
220 /* Fill in the subscript array. */
221 subscript_array[i] = value_as_long (arg2);
222 }
223
224 /* Internal type of array is arranged right to left. */
225 for (int i = nargs; i > 0; i--)
226 {
227 struct type *array_type = check_typedef (value_type (array));
228 LONGEST index = subscript_array[i - 1];
229
230 array = value_subscripted_rvalue (array, index,
231 f77_get_lowerbound (array_type));
232 }
233
234 return array;
235}
236
9dad4a58 237/* Special expression evaluation cases for Fortran. */
cb8c24b6
SM
238
239static struct value *
9dad4a58
AB
240evaluate_subexp_f (struct type *expect_type, struct expression *exp,
241 int *pos, enum noside noside)
242{
b6d03bb2 243 struct value *arg1 = NULL, *arg2 = NULL;
4d00f5d8
AB
244 enum exp_opcode op;
245 int pc;
246 struct type *type;
247
248 pc = *pos;
249 *pos += 1;
250 op = exp->elts[pc].opcode;
251
252 switch (op)
253 {
254 default:
255 *pos -= 1;
256 return evaluate_subexp_standard (expect_type, exp, pos, noside);
257
0841c79a 258 case UNOP_ABS:
fe1fe7ea 259 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
0841c79a
AB
260 if (noside == EVAL_SKIP)
261 return eval_skip_value (exp);
262 type = value_type (arg1);
78134374 263 switch (type->code ())
0841c79a
AB
264 {
265 case TYPE_CODE_FLT:
266 {
267 double d
268 = fabs (target_float_to_host_double (value_contents (arg1),
269 value_type (arg1)));
270 return value_from_host_double (type, d);
271 }
272 case TYPE_CODE_INT:
273 {
274 LONGEST l = value_as_long (arg1);
275 l = llabs (l);
276 return value_from_longest (type, l);
277 }
278 }
279 error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
280
b6d03bb2 281 case BINOP_MOD:
fe1fe7ea 282 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
283 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
284 if (noside == EVAL_SKIP)
285 return eval_skip_value (exp);
286 type = value_type (arg1);
78134374 287 if (type->code () != value_type (arg2)->code ())
b6d03bb2 288 error (_("non-matching types for parameters to MOD ()"));
78134374 289 switch (type->code ())
b6d03bb2
AB
290 {
291 case TYPE_CODE_FLT:
292 {
293 double d1
294 = target_float_to_host_double (value_contents (arg1),
295 value_type (arg1));
296 double d2
297 = target_float_to_host_double (value_contents (arg2),
298 value_type (arg2));
299 double d3 = fmod (d1, d2);
300 return value_from_host_double (type, d3);
301 }
302 case TYPE_CODE_INT:
303 {
304 LONGEST v1 = value_as_long (arg1);
305 LONGEST v2 = value_as_long (arg2);
306 if (v2 == 0)
307 error (_("calling MOD (N, 0) is undefined"));
308 LONGEST v3 = v1 - (v1 / v2) * v2;
309 return value_from_longest (value_type (arg1), v3);
310 }
311 }
312 error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
313
314 case UNOP_FORTRAN_CEILING:
315 {
fe1fe7ea 316 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
317 if (noside == EVAL_SKIP)
318 return eval_skip_value (exp);
319 type = value_type (arg1);
78134374 320 if (type->code () != TYPE_CODE_FLT)
b6d03bb2
AB
321 error (_("argument to CEILING must be of type float"));
322 double val
323 = target_float_to_host_double (value_contents (arg1),
324 value_type (arg1));
325 val = ceil (val);
326 return value_from_host_double (type, val);
327 }
328
329 case UNOP_FORTRAN_FLOOR:
330 {
fe1fe7ea 331 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
332 if (noside == EVAL_SKIP)
333 return eval_skip_value (exp);
334 type = value_type (arg1);
78134374 335 if (type->code () != TYPE_CODE_FLT)
b6d03bb2
AB
336 error (_("argument to FLOOR must be of type float"));
337 double val
338 = target_float_to_host_double (value_contents (arg1),
339 value_type (arg1));
340 val = floor (val);
341 return value_from_host_double (type, val);
342 }
343
344 case BINOP_FORTRAN_MODULO:
345 {
fe1fe7ea 346 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
347 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
348 if (noside == EVAL_SKIP)
349 return eval_skip_value (exp);
350 type = value_type (arg1);
78134374 351 if (type->code () != value_type (arg2)->code ())
b6d03bb2 352 error (_("non-matching types for parameters to MODULO ()"));
dda83cd7 353 /* MODULO(A, P) = A - FLOOR (A / P) * P */
78134374 354 switch (type->code ())
b6d03bb2
AB
355 {
356 case TYPE_CODE_INT:
357 {
358 LONGEST a = value_as_long (arg1);
359 LONGEST p = value_as_long (arg2);
360 LONGEST result = a - (a / p) * p;
361 if (result != 0 && (a < 0) != (p < 0))
362 result += p;
363 return value_from_longest (value_type (arg1), result);
364 }
365 case TYPE_CODE_FLT:
366 {
367 double a
368 = target_float_to_host_double (value_contents (arg1),
369 value_type (arg1));
370 double p
371 = target_float_to_host_double (value_contents (arg2),
372 value_type (arg2));
373 double result = fmod (a, p);
374 if (result != 0 && (a < 0.0) != (p < 0.0))
375 result += p;
376 return value_from_host_double (type, result);
377 }
378 }
379 error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
380 }
381
382 case BINOP_FORTRAN_CMPLX:
fe1fe7ea 383 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
b6d03bb2
AB
384 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
385 if (noside == EVAL_SKIP)
386 return eval_skip_value (exp);
387 type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
388 return value_literal_complex (arg1, arg2, type);
389
83228e93 390 case UNOP_FORTRAN_KIND:
4d00f5d8
AB
391 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
392 type = value_type (arg1);
393
78134374 394 switch (type->code ())
dda83cd7
SM
395 {
396 case TYPE_CODE_STRUCT:
397 case TYPE_CODE_UNION:
398 case TYPE_CODE_MODULE:
399 case TYPE_CODE_FUNC:
400 error (_("argument to kind must be an intrinsic type"));
401 }
4d00f5d8
AB
402
403 if (!TYPE_TARGET_TYPE (type))
dda83cd7 404 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
4d00f5d8
AB
405 TYPE_LENGTH (type));
406 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
78134374 407 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
6d816919
AB
408
409
410 case OP_F77_UNDETERMINED_ARGLIST:
411 /* Remember that in F77, functions, substring ops and array subscript
dda83cd7
SM
412 operations cannot be disambiguated at parse time. We have made
413 all array subscript operations, substring operations as well as
414 function calls come here and we now have to discover what the heck
415 this thing actually was. If it is a function, we process just as
416 if we got an OP_FUNCALL. */
6d816919
AB
417 int nargs = longest_to_int (exp->elts[pc + 1].longconst);
418 (*pos) += 2;
419
420 /* First determine the type code we are dealing with. */
421 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
422 type = check_typedef (value_type (arg1));
423 enum type_code code = type->code ();
424
425 if (code == TYPE_CODE_PTR)
426 {
427 /* Fortran always passes variable to subroutines as pointer.
428 So we need to look into its target type to see if it is
429 array, string or function. If it is, we need to switch
430 to the target value the original one points to. */
431 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
432
433 if (target_type->code () == TYPE_CODE_ARRAY
434 || target_type->code () == TYPE_CODE_STRING
435 || target_type->code () == TYPE_CODE_FUNC)
436 {
437 arg1 = value_ind (arg1);
438 type = check_typedef (value_type (arg1));
439 code = type->code ();
440 }
441 }
442
443 switch (code)
444 {
445 case TYPE_CODE_ARRAY:
446 case TYPE_CODE_STRING:
447 return fortran_value_subarray (arg1, exp, pos, nargs, noside);
448
449 case TYPE_CODE_PTR:
450 case TYPE_CODE_FUNC:
451 case TYPE_CODE_INTERNAL_FUNCTION:
452 {
453 /* It's a function call. Allocate arg vector, including
454 space for the function to be called in argvec[0] and a
455 termination NULL. */
456 struct value **argvec = (struct value **)
457 alloca (sizeof (struct value *) * (nargs + 2));
458 argvec[0] = arg1;
459 int tem = 1;
460 for (; tem <= nargs; tem++)
461 {
462 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
463 /* Arguments in Fortran are passed by address. Coerce the
464 arguments here rather than in value_arg_coerce as
465 otherwise the call to malloc to place the non-lvalue
466 parameters in target memory is hit by this Fortran
467 specific logic. This results in malloc being called
468 with a pointer to an integer followed by an attempt to
469 malloc the arguments to malloc in target memory.
470 Infinite recursion ensues. */
471 if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
472 {
473 bool is_artificial
474 = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
475 argvec[tem] = fortran_argument_convert (argvec[tem],
476 is_artificial);
477 }
478 }
479 argvec[tem] = 0; /* signal end of arglist */
480 if (noside == EVAL_SKIP)
481 return eval_skip_value (exp);
482 return evaluate_subexp_do_call (exp, noside, nargs, argvec, NULL,
483 expect_type);
484 }
485
486 default:
487 error (_("Cannot perform substring on this type"));
488 }
4d00f5d8
AB
489 }
490
491 /* Should be unreachable. */
492 return nullptr;
9dad4a58
AB
493}
494
83228e93
AB
495/* Special expression lengths for Fortran. */
496
497static void
498operator_length_f (const struct expression *exp, int pc, int *oplenp,
499 int *argsp)
500{
501 int oplen = 1;
502 int args = 0;
503
504 switch (exp->elts[pc - 1].opcode)
505 {
506 default:
507 operator_length_standard (exp, pc, oplenp, argsp);
508 return;
509
510 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
511 case UNOP_FORTRAN_FLOOR:
512 case UNOP_FORTRAN_CEILING:
83228e93
AB
513 oplen = 1;
514 args = 1;
515 break;
b6d03bb2
AB
516
517 case BINOP_FORTRAN_CMPLX:
518 case BINOP_FORTRAN_MODULO:
519 oplen = 1;
520 args = 2;
521 break;
6d816919
AB
522
523 case OP_F77_UNDETERMINED_ARGLIST:
524 oplen = 3;
525 args = 1 + longest_to_int (exp->elts[pc - 2].longconst);
526 break;
83228e93
AB
527 }
528
529 *oplenp = oplen;
530 *argsp = args;
531}
532
b6d03bb2
AB
533/* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
534 the extra argument NAME which is the text that should be printed as the
535 name of this operation. */
536
537static void
538print_unop_subexp_f (struct expression *exp, int *pos,
539 struct ui_file *stream, enum precedence prec,
540 const char *name)
541{
542 (*pos)++;
543 fprintf_filtered (stream, "%s(", name);
544 print_subexp (exp, pos, stream, PREC_SUFFIX);
545 fputs_filtered (")", stream);
546}
547
548/* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except
549 the extra argument NAME which is the text that should be printed as the
550 name of this operation. */
551
552static void
553print_binop_subexp_f (struct expression *exp, int *pos,
554 struct ui_file *stream, enum precedence prec,
555 const char *name)
556{
557 (*pos)++;
558 fprintf_filtered (stream, "%s(", name);
559 print_subexp (exp, pos, stream, PREC_SUFFIX);
560 fputs_filtered (",", stream);
561 print_subexp (exp, pos, stream, PREC_SUFFIX);
562 fputs_filtered (")", stream);
563}
564
83228e93
AB
565/* Special expression printing for Fortran. */
566
567static void
568print_subexp_f (struct expression *exp, int *pos,
569 struct ui_file *stream, enum precedence prec)
570{
571 int pc = *pos;
572 enum exp_opcode op = exp->elts[pc].opcode;
573
574 switch (op)
575 {
576 default:
577 print_subexp_standard (exp, pos, stream, prec);
578 return;
579
580 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
581 print_unop_subexp_f (exp, pos, stream, prec, "KIND");
582 return;
583
584 case UNOP_FORTRAN_FLOOR:
585 print_unop_subexp_f (exp, pos, stream, prec, "FLOOR");
586 return;
587
588 case UNOP_FORTRAN_CEILING:
589 print_unop_subexp_f (exp, pos, stream, prec, "CEILING");
590 return;
591
592 case BINOP_FORTRAN_CMPLX:
593 print_binop_subexp_f (exp, pos, stream, prec, "CMPLX");
594 return;
595
596 case BINOP_FORTRAN_MODULO:
597 print_binop_subexp_f (exp, pos, stream, prec, "MODULO");
83228e93 598 return;
6d816919
AB
599
600 case OP_F77_UNDETERMINED_ARGLIST:
86775fab 601 (*pos)++;
6d816919
AB
602 print_subexp_funcall (exp, pos, stream);
603 return;
83228e93
AB
604 }
605}
606
607/* Special expression names for Fortran. */
608
609static const char *
610op_name_f (enum exp_opcode opcode)
611{
612 switch (opcode)
613 {
614 default:
615 return op_name_standard (opcode);
616
617#define OP(name) \
618 case name: \
619 return #name ;
620#include "fortran-operator.def"
621#undef OP
622 }
623}
624
625/* Special expression dumping for Fortran. */
626
627static int
628dump_subexp_body_f (struct expression *exp,
629 struct ui_file *stream, int elt)
630{
631 int opcode = exp->elts[elt].opcode;
632 int oplen, nargs, i;
633
634 switch (opcode)
635 {
636 default:
637 return dump_subexp_body_standard (exp, stream, elt);
638
639 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
640 case UNOP_FORTRAN_FLOOR:
641 case UNOP_FORTRAN_CEILING:
642 case BINOP_FORTRAN_CMPLX:
643 case BINOP_FORTRAN_MODULO:
83228e93
AB
644 operator_length_f (exp, (elt + 1), &oplen, &nargs);
645 break;
6d816919
AB
646
647 case OP_F77_UNDETERMINED_ARGLIST:
86775fab 648 return dump_subexp_body_funcall (exp, stream, elt + 1);
83228e93
AB
649 }
650
651 elt += oplen;
652 for (i = 0; i < nargs; i += 1)
653 elt = dump_subexp (exp, stream, elt);
654
655 return elt;
656}
657
658/* Special expression checking for Fortran. */
659
660static int
661operator_check_f (struct expression *exp, int pos,
662 int (*objfile_func) (struct objfile *objfile,
663 void *data),
664 void *data)
665{
666 const union exp_element *const elts = exp->elts;
667
668 switch (elts[pos].opcode)
669 {
670 case UNOP_FORTRAN_KIND:
b6d03bb2
AB
671 case UNOP_FORTRAN_FLOOR:
672 case UNOP_FORTRAN_CEILING:
673 case BINOP_FORTRAN_CMPLX:
674 case BINOP_FORTRAN_MODULO:
83228e93
AB
675 /* Any references to objfiles are held in the arguments to this
676 expression, not within the expression itself, so no additional
677 checking is required here, the outer expression iteration code
678 will take care of checking each argument. */
679 break;
680
681 default:
682 return operator_check_standard (exp, pos, objfile_func, data);
683 }
684
685 return 0;
686}
687
9dad4a58 688/* Expression processing for Fortran. */
1a0ea399 689const struct exp_descriptor f_language::exp_descriptor_tab =
9dad4a58 690{
83228e93
AB
691 print_subexp_f,
692 operator_length_f,
693 operator_check_f,
694 op_name_f,
695 dump_subexp_body_f,
9dad4a58
AB
696 evaluate_subexp_f
697};
698
1a0ea399 699/* See language.h. */
0874fd07 700
1a0ea399
AB
701void
702f_language::language_arch_info (struct gdbarch *gdbarch,
703 struct language_arch_info *lai) const
0874fd07 704{
1a0ea399
AB
705 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
706
7bea47f0
AB
707 /* Helper function to allow shorter lines below. */
708 auto add = [&] (struct type * t)
709 {
710 lai->add_primitive_type (t);
711 };
712
713 add (builtin->builtin_character);
714 add (builtin->builtin_logical);
715 add (builtin->builtin_logical_s1);
716 add (builtin->builtin_logical_s2);
717 add (builtin->builtin_logical_s8);
718 add (builtin->builtin_real);
719 add (builtin->builtin_real_s8);
720 add (builtin->builtin_real_s16);
721 add (builtin->builtin_complex_s8);
722 add (builtin->builtin_complex_s16);
723 add (builtin->builtin_void);
724
725 lai->set_string_char_type (builtin->builtin_character);
726 lai->set_bool_type (builtin->builtin_logical_s2, "logical");
1a0ea399 727}
5aba6ebe 728
1a0ea399 729/* See language.h. */
5aba6ebe 730
1a0ea399
AB
731unsigned int
732f_language::search_name_hash (const char *name) const
733{
734 return cp_search_name_hash (name);
735}
b7c6e27d 736
1a0ea399 737/* See language.h. */
b7c6e27d 738
1a0ea399
AB
739struct block_symbol
740f_language::lookup_symbol_nonlocal (const char *name,
741 const struct block *block,
742 const domain_enum domain) const
743{
744 return cp_lookup_symbol_nonlocal (this, name, block, domain);
745}
c9debfb9 746
1a0ea399 747/* See language.h. */
c9debfb9 748
1a0ea399
AB
749symbol_name_matcher_ftype *
750f_language::get_symbol_name_matcher_inner
751 (const lookup_name_info &lookup_name) const
752{
753 return cp_get_symbol_name_matcher (lookup_name);
754}
0874fd07
AB
755
756/* Single instance of the Fortran language class. */
757
758static f_language f_language_defn;
759
54ef06c7
UW
760static void *
761build_fortran_types (struct gdbarch *gdbarch)
c906108c 762{
54ef06c7
UW
763 struct builtin_f_type *builtin_f_type
764 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
765
e9bb382b 766 builtin_f_type->builtin_void
bbe75b9d 767 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
e9bb382b
UW
768
769 builtin_f_type->builtin_character
4a270568 770 = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
e9bb382b
UW
771
772 builtin_f_type->builtin_logical_s1
773 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
774
775 builtin_f_type->builtin_integer_s2
776 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
777 "integer*2");
778
067630bd
AB
779 builtin_f_type->builtin_integer_s8
780 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
781 "integer*8");
782
e9bb382b
UW
783 builtin_f_type->builtin_logical_s2
784 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
785 "logical*2");
786
ce4b0682
SDJ
787 builtin_f_type->builtin_logical_s8
788 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
789 "logical*8");
790
e9bb382b
UW
791 builtin_f_type->builtin_integer
792 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
793 "integer");
794
795 builtin_f_type->builtin_logical
796 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
797 "logical*4");
798
799 builtin_f_type->builtin_real
800 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
49f190bc 801 "real", gdbarch_float_format (gdbarch));
e9bb382b
UW
802 builtin_f_type->builtin_real_s8
803 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
49f190bc 804 "real*8", gdbarch_double_format (gdbarch));
34d11c68 805 auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
dc42e902
AB
806 if (fmt != nullptr)
807 builtin_f_type->builtin_real_s16
808 = arch_float_type (gdbarch, 128, "real*16", fmt);
809 else if (gdbarch_long_double_bit (gdbarch) == 128)
810 builtin_f_type->builtin_real_s16
811 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
812 "real*16", gdbarch_long_double_format (gdbarch));
813 else
814 builtin_f_type->builtin_real_s16
815 = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
e9bb382b
UW
816
817 builtin_f_type->builtin_complex_s8
5b930b45 818 = init_complex_type ("complex*8", builtin_f_type->builtin_real);
e9bb382b 819 builtin_f_type->builtin_complex_s16
5b930b45 820 = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
0830d301 821
78134374 822 if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
0830d301
TT
823 builtin_f_type->builtin_complex_s32
824 = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
825 else
826 builtin_f_type->builtin_complex_s32
827 = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
54ef06c7
UW
828
829 return builtin_f_type;
830}
831
832static struct gdbarch_data *f_type_data;
833
834const struct builtin_f_type *
835builtin_f_type (struct gdbarch *gdbarch)
836{
9a3c8263 837 return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
4e845cd3
MS
838}
839
6c265988 840void _initialize_f_language ();
4e845cd3 841void
6c265988 842_initialize_f_language ()
4e845cd3 843{
54ef06c7 844 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
c906108c 845}
aa3cfbda
RB
846
847/* See f-lang.h. */
848
849struct value *
850fortran_argument_convert (struct value *value, bool is_artificial)
851{
852 if (!is_artificial)
853 {
854 /* If the value is not in the inferior e.g. registers values,
855 convenience variables and user input. */
856 if (VALUE_LVAL (value) != lval_memory)
857 {
858 struct type *type = value_type (value);
859 const int length = TYPE_LENGTH (type);
860 const CORE_ADDR addr
861 = value_as_long (value_allocate_space_in_inferior (length));
862 write_memory (addr, value_contents (value), length);
863 struct value *val
864 = value_from_contents_and_address (type, value_contents (value),
865 addr);
866 return value_addr (val);
867 }
868 else
869 return value_addr (value); /* Program variables, e.g. arrays. */
870 }
871 return value;
872}
873
874/* See f-lang.h. */
875
876struct type *
877fortran_preserve_arg_pointer (struct value *arg, struct type *type)
878{
78134374 879 if (value_type (arg)->code () == TYPE_CODE_PTR)
aa3cfbda
RB
880 return value_type (arg);
881 return type;
882}