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