Home | History | Annotate | Line # | Download | only in gdb
f-lang.c revision 1.9
      1  1.1  christos /* Fortran language support routines for GDB, the GNU debugger.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 1993-2020 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    Contributed by Motorola.  Adapted from the C parser by Farooq Butt
      6  1.1  christos    (fmbutt (at) engage.sps.mot.com).
      7  1.1  christos 
      8  1.1  christos    This file is part of GDB.
      9  1.1  christos 
     10  1.1  christos    This program is free software; you can redistribute it and/or modify
     11  1.1  christos    it under the terms of the GNU General Public License as published by
     12  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     13  1.1  christos    (at your option) any later version.
     14  1.1  christos 
     15  1.1  christos    This program is distributed in the hope that it will be useful,
     16  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  1.1  christos    GNU General Public License for more details.
     19  1.1  christos 
     20  1.1  christos    You should have received a copy of the GNU General Public License
     21  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     22  1.1  christos 
     23  1.1  christos #include "defs.h"
     24  1.1  christos #include "symtab.h"
     25  1.1  christos #include "gdbtypes.h"
     26  1.1  christos #include "expression.h"
     27  1.1  christos #include "parser-defs.h"
     28  1.1  christos #include "language.h"
     29  1.1  christos #include "varobj.h"
     30  1.9  christos #include "gdbcore.h"
     31  1.1  christos #include "f-lang.h"
     32  1.1  christos #include "valprint.h"
     33  1.1  christos #include "value.h"
     34  1.1  christos #include "cp-support.h"
     35  1.1  christos #include "charset.h"
     36  1.1  christos #include "c-lang.h"
     37  1.9  christos #include "target-float.h"
     38  1.9  christos #include "gdbarch.h"
     39  1.1  christos 
     40  1.9  christos #include <math.h>
     41  1.1  christos 
     42  1.1  christos /* Local functions */
     43  1.1  christos 
     44  1.1  christos /* Return the encoding that should be used for the character type
     45  1.1  christos    TYPE.  */
     46  1.1  christos 
     47  1.1  christos static const char *
     48  1.1  christos f_get_encoding (struct type *type)
     49  1.1  christos {
     50  1.1  christos   const char *encoding;
     51  1.1  christos 
     52  1.1  christos   switch (TYPE_LENGTH (type))
     53  1.1  christos     {
     54  1.1  christos     case 1:
     55  1.1  christos       encoding = target_charset (get_type_arch (type));
     56  1.1  christos       break;
     57  1.1  christos     case 4:
     58  1.9  christos       if (type_byte_order (type) == BFD_ENDIAN_BIG)
     59  1.1  christos 	encoding = "UTF-32BE";
     60  1.1  christos       else
     61  1.1  christos 	encoding = "UTF-32LE";
     62  1.1  christos       break;
     63  1.1  christos 
     64  1.1  christos     default:
     65  1.1  christos       error (_("unrecognized character type"));
     66  1.1  christos     }
     67  1.1  christos 
     68  1.1  christos   return encoding;
     69  1.1  christos }
     70  1.1  christos 
     71  1.1  christos 
     72  1.1  christos 
     74  1.1  christos /* Table of operators and their precedences for printing expressions.  */
     75  1.1  christos 
     76  1.1  christos static const struct op_print f_op_print_tab[] =
     77  1.1  christos {
     78  1.1  christos   {"+", BINOP_ADD, PREC_ADD, 0},
     79  1.1  christos   {"+", UNOP_PLUS, PREC_PREFIX, 0},
     80  1.1  christos   {"-", BINOP_SUB, PREC_ADD, 0},
     81  1.1  christos   {"-", UNOP_NEG, PREC_PREFIX, 0},
     82  1.1  christos   {"*", BINOP_MUL, PREC_MUL, 0},
     83  1.1  christos   {"/", BINOP_DIV, PREC_MUL, 0},
     84  1.1  christos   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
     85  1.1  christos   {"MOD", BINOP_REM, PREC_MUL, 0},
     86  1.1  christos   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
     87  1.1  christos   {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
     88  1.1  christos   {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
     89  1.1  christos   {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
     90  1.1  christos   {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
     91  1.1  christos   {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
     92  1.1  christos   {".LE.", BINOP_LEQ, PREC_ORDER, 0},
     93  1.1  christos   {".GE.", BINOP_GEQ, PREC_ORDER, 0},
     94  1.1  christos   {".GT.", BINOP_GTR, PREC_ORDER, 0},
     95  1.1  christos   {".LT.", BINOP_LESS, PREC_ORDER, 0},
     96  1.1  christos   {"**", UNOP_IND, PREC_PREFIX, 0},
     97  1.6  christos   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
     98  1.1  christos   {NULL, OP_NULL, PREC_REPEAT, 0}
     99  1.1  christos };
    100  1.1  christos 
    101  1.1  christos enum f_primitive_types {
    103  1.1  christos   f_primitive_type_character,
    104  1.1  christos   f_primitive_type_logical,
    105  1.1  christos   f_primitive_type_logical_s1,
    106  1.1  christos   f_primitive_type_logical_s2,
    107  1.1  christos   f_primitive_type_logical_s8,
    108  1.1  christos   f_primitive_type_integer,
    109  1.1  christos   f_primitive_type_integer_s2,
    110  1.1  christos   f_primitive_type_real,
    111  1.1  christos   f_primitive_type_real_s8,
    112  1.1  christos   f_primitive_type_real_s16,
    113  1.1  christos   f_primitive_type_complex_s8,
    114  1.1  christos   f_primitive_type_complex_s16,
    115  1.1  christos   f_primitive_type_void,
    116  1.1  christos   nr_f_primitive_types
    117  1.9  christos };
    118  1.9  christos 
    119  1.9  christos /* Special expression evaluation cases for Fortran.  */
    120  1.9  christos 
    121  1.9  christos static struct value *
    122  1.9  christos evaluate_subexp_f (struct type *expect_type, struct expression *exp,
    123  1.9  christos 		   int *pos, enum noside noside)
    124  1.9  christos {
    125  1.9  christos   struct value *arg1 = NULL, *arg2 = NULL;
    126  1.9  christos   enum exp_opcode op;
    127  1.9  christos   int pc;
    128  1.9  christos   struct type *type;
    129  1.9  christos 
    130  1.9  christos   pc = *pos;
    131  1.9  christos   *pos += 1;
    132  1.9  christos   op = exp->elts[pc].opcode;
    133  1.9  christos 
    134  1.9  christos   switch (op)
    135  1.9  christos     {
    136  1.9  christos     default:
    137  1.9  christos       *pos -= 1;
    138  1.9  christos       return evaluate_subexp_standard (expect_type, exp, pos, noside);
    139  1.9  christos 
    140  1.9  christos     case UNOP_ABS:
    141  1.9  christos       arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    142  1.9  christos       if (noside == EVAL_SKIP)
    143  1.9  christos 	return eval_skip_value (exp);
    144  1.9  christos       type = value_type (arg1);
    145  1.9  christos       switch (type->code ())
    146  1.9  christos 	{
    147  1.9  christos 	case TYPE_CODE_FLT:
    148  1.9  christos 	  {
    149  1.9  christos 	    double d
    150  1.9  christos 	      = fabs (target_float_to_host_double (value_contents (arg1),
    151  1.9  christos 						   value_type (arg1)));
    152  1.9  christos 	    return value_from_host_double (type, d);
    153  1.9  christos 	  }
    154  1.9  christos 	case TYPE_CODE_INT:
    155  1.9  christos 	  {
    156  1.9  christos 	    LONGEST l = value_as_long (arg1);
    157  1.9  christos 	    l = llabs (l);
    158  1.9  christos 	    return value_from_longest (type, l);
    159  1.9  christos 	  }
    160  1.9  christos 	}
    161  1.9  christos       error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
    162  1.9  christos 
    163  1.9  christos     case BINOP_MOD:
    164  1.9  christos       arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    165  1.9  christos       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
    166  1.9  christos       if (noside == EVAL_SKIP)
    167  1.9  christos 	return eval_skip_value (exp);
    168  1.9  christos       type = value_type (arg1);
    169  1.9  christos       if (type->code () != value_type (arg2)->code ())
    170  1.9  christos 	error (_("non-matching types for parameters to MOD ()"));
    171  1.9  christos       switch (type->code ())
    172  1.9  christos 	{
    173  1.9  christos 	case TYPE_CODE_FLT:
    174  1.9  christos 	  {
    175  1.9  christos 	    double d1
    176  1.9  christos 	      = target_float_to_host_double (value_contents (arg1),
    177  1.9  christos 					     value_type (arg1));
    178  1.9  christos 	    double d2
    179  1.9  christos 	      = target_float_to_host_double (value_contents (arg2),
    180  1.9  christos 					     value_type (arg2));
    181  1.9  christos 	    double d3 = fmod (d1, d2);
    182  1.9  christos 	    return value_from_host_double (type, d3);
    183  1.9  christos 	  }
    184  1.9  christos 	case TYPE_CODE_INT:
    185  1.9  christos 	  {
    186  1.9  christos 	    LONGEST v1 = value_as_long (arg1);
    187  1.9  christos 	    LONGEST v2 = value_as_long (arg2);
    188  1.9  christos 	    if (v2 == 0)
    189  1.9  christos 	      error (_("calling MOD (N, 0) is undefined"));
    190  1.9  christos 	    LONGEST v3 = v1 - (v1 / v2) * v2;
    191  1.9  christos 	    return value_from_longest (value_type (arg1), v3);
    192  1.9  christos 	  }
    193  1.9  christos 	}
    194  1.9  christos       error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
    195  1.9  christos 
    196  1.9  christos     case UNOP_FORTRAN_CEILING:
    197  1.9  christos       {
    198  1.9  christos 	arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    199  1.9  christos 	if (noside == EVAL_SKIP)
    200  1.9  christos 	  return eval_skip_value (exp);
    201  1.9  christos 	type = value_type (arg1);
    202  1.9  christos 	if (type->code () != TYPE_CODE_FLT)
    203  1.9  christos 	  error (_("argument to CEILING must be of type float"));
    204  1.9  christos 	double val
    205  1.9  christos 	  = target_float_to_host_double (value_contents (arg1),
    206  1.9  christos 					 value_type (arg1));
    207  1.9  christos 	val = ceil (val);
    208  1.9  christos 	return value_from_host_double (type, val);
    209  1.9  christos       }
    210  1.9  christos 
    211  1.9  christos     case UNOP_FORTRAN_FLOOR:
    212  1.9  christos       {
    213  1.9  christos 	arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    214  1.9  christos 	if (noside == EVAL_SKIP)
    215  1.9  christos 	  return eval_skip_value (exp);
    216  1.9  christos 	type = value_type (arg1);
    217  1.9  christos 	if (type->code () != TYPE_CODE_FLT)
    218  1.9  christos 	  error (_("argument to FLOOR must be of type float"));
    219  1.9  christos 	double val
    220  1.9  christos 	  = target_float_to_host_double (value_contents (arg1),
    221  1.9  christos 					 value_type (arg1));
    222  1.9  christos 	val = floor (val);
    223  1.9  christos 	return value_from_host_double (type, val);
    224  1.9  christos       }
    225  1.9  christos 
    226  1.9  christos     case BINOP_FORTRAN_MODULO:
    227  1.9  christos       {
    228  1.9  christos 	arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    229  1.9  christos 	arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
    230  1.9  christos 	if (noside == EVAL_SKIP)
    231  1.9  christos 	  return eval_skip_value (exp);
    232  1.9  christos 	type = value_type (arg1);
    233  1.9  christos 	if (type->code () != value_type (arg2)->code ())
    234  1.9  christos 	  error (_("non-matching types for parameters to MODULO ()"));
    235  1.9  christos         /* MODULO(A, P) = A - FLOOR (A / P) * P */
    236  1.9  christos 	switch (type->code ())
    237  1.9  christos 	  {
    238  1.9  christos 	  case TYPE_CODE_INT:
    239  1.9  christos 	    {
    240  1.9  christos 	      LONGEST a = value_as_long (arg1);
    241  1.9  christos 	      LONGEST p = value_as_long (arg2);
    242  1.9  christos 	      LONGEST result = a - (a / p) * p;
    243  1.9  christos 	      if (result != 0 && (a < 0) != (p < 0))
    244  1.9  christos 		result += p;
    245  1.9  christos 	      return value_from_longest (value_type (arg1), result);
    246  1.9  christos 	    }
    247  1.9  christos 	  case TYPE_CODE_FLT:
    248  1.9  christos 	    {
    249  1.9  christos 	      double a
    250  1.9  christos 		= target_float_to_host_double (value_contents (arg1),
    251  1.9  christos 					       value_type (arg1));
    252  1.9  christos 	      double p
    253  1.9  christos 		= target_float_to_host_double (value_contents (arg2),
    254  1.9  christos 					       value_type (arg2));
    255  1.9  christos 	      double result = fmod (a, p);
    256  1.9  christos 	      if (result != 0 && (a < 0.0) != (p < 0.0))
    257  1.9  christos 		result += p;
    258  1.9  christos 	      return value_from_host_double (type, result);
    259  1.9  christos 	    }
    260  1.9  christos 	  }
    261  1.9  christos 	error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
    262  1.9  christos       }
    263  1.9  christos 
    264  1.9  christos     case BINOP_FORTRAN_CMPLX:
    265  1.9  christos       arg1 = evaluate_subexp (nullptr, exp, pos, noside);
    266  1.9  christos       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
    267  1.9  christos       if (noside == EVAL_SKIP)
    268  1.9  christos 	return eval_skip_value (exp);
    269  1.9  christos       type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
    270  1.9  christos       return value_literal_complex (arg1, arg2, type);
    271  1.9  christos 
    272  1.9  christos     case UNOP_FORTRAN_KIND:
    273  1.9  christos       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
    274  1.9  christos       type = value_type (arg1);
    275  1.9  christos 
    276  1.9  christos       switch (type->code ())
    277  1.9  christos         {
    278  1.9  christos           case TYPE_CODE_STRUCT:
    279  1.9  christos           case TYPE_CODE_UNION:
    280  1.9  christos           case TYPE_CODE_MODULE:
    281  1.9  christos           case TYPE_CODE_FUNC:
    282  1.9  christos             error (_("argument to kind must be an intrinsic type"));
    283  1.9  christos         }
    284  1.9  christos 
    285  1.9  christos       if (!TYPE_TARGET_TYPE (type))
    286  1.9  christos         return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
    287  1.9  christos 				   TYPE_LENGTH (type));
    288  1.9  christos       return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
    289  1.9  christos 				 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
    290  1.9  christos     }
    291  1.9  christos 
    292  1.9  christos   /* Should be unreachable.  */
    293  1.9  christos   return nullptr;
    294  1.9  christos }
    295  1.9  christos 
    296  1.9  christos /* Special expression lengths for Fortran.  */
    297  1.9  christos 
    298  1.9  christos static void
    299  1.9  christos operator_length_f (const struct expression *exp, int pc, int *oplenp,
    300  1.9  christos 		   int *argsp)
    301  1.9  christos {
    302  1.9  christos   int oplen = 1;
    303  1.9  christos   int args = 0;
    304  1.9  christos 
    305  1.9  christos   switch (exp->elts[pc - 1].opcode)
    306  1.9  christos     {
    307  1.9  christos     default:
    308  1.9  christos       operator_length_standard (exp, pc, oplenp, argsp);
    309  1.9  christos       return;
    310  1.9  christos 
    311  1.9  christos     case UNOP_FORTRAN_KIND:
    312  1.9  christos     case UNOP_FORTRAN_FLOOR:
    313  1.9  christos     case UNOP_FORTRAN_CEILING:
    314  1.9  christos       oplen = 1;
    315  1.9  christos       args = 1;
    316  1.9  christos       break;
    317  1.9  christos 
    318  1.9  christos     case BINOP_FORTRAN_CMPLX:
    319  1.9  christos     case BINOP_FORTRAN_MODULO:
    320  1.9  christos       oplen = 1;
    321  1.9  christos       args = 2;
    322  1.9  christos       break;
    323  1.9  christos     }
    324  1.9  christos 
    325  1.9  christos   *oplenp = oplen;
    326  1.9  christos   *argsp = args;
    327  1.9  christos }
    328  1.9  christos 
    329  1.9  christos /* Helper for PRINT_SUBEXP_F.  Arguments are as for PRINT_SUBEXP_F, except
    330  1.9  christos    the extra argument NAME which is the text that should be printed as the
    331  1.9  christos    name of this operation.  */
    332  1.9  christos 
    333  1.9  christos static void
    334  1.9  christos print_unop_subexp_f (struct expression *exp, int *pos,
    335  1.9  christos 		     struct ui_file *stream, enum precedence prec,
    336  1.9  christos 		     const char *name)
    337  1.9  christos {
    338  1.9  christos   (*pos)++;
    339  1.9  christos   fprintf_filtered (stream, "%s(", name);
    340  1.9  christos   print_subexp (exp, pos, stream, PREC_SUFFIX);
    341  1.9  christos   fputs_filtered (")", stream);
    342  1.9  christos }
    343  1.9  christos 
    344  1.9  christos /* Helper for PRINT_SUBEXP_F.  Arguments are as for PRINT_SUBEXP_F, except
    345  1.9  christos    the extra argument NAME which is the text that should be printed as the
    346  1.9  christos    name of this operation.  */
    347  1.9  christos 
    348  1.9  christos static void
    349  1.9  christos print_binop_subexp_f (struct expression *exp, int *pos,
    350  1.9  christos 		      struct ui_file *stream, enum precedence prec,
    351  1.9  christos 		      const char *name)
    352  1.9  christos {
    353  1.9  christos   (*pos)++;
    354  1.9  christos   fprintf_filtered (stream, "%s(", name);
    355  1.9  christos   print_subexp (exp, pos, stream, PREC_SUFFIX);
    356  1.9  christos   fputs_filtered (",", stream);
    357  1.9  christos   print_subexp (exp, pos, stream, PREC_SUFFIX);
    358  1.9  christos   fputs_filtered (")", stream);
    359  1.9  christos }
    360  1.9  christos 
    361  1.1  christos /* Special expression printing for Fortran.  */
    362  1.9  christos 
    363  1.9  christos static void
    364  1.1  christos print_subexp_f (struct expression *exp, int *pos,
    365  1.9  christos 		struct ui_file *stream, enum precedence prec)
    366  1.9  christos {
    367  1.1  christos   int pc = *pos;
    368  1.9  christos   enum exp_opcode op = exp->elts[pc].opcode;
    369  1.9  christos 
    370  1.9  christos   switch (op)
    371  1.9  christos     {
    372  1.9  christos     default:
    373  1.1  christos       print_subexp_standard (exp, pos, stream, prec);
    374  1.9  christos       return;
    375  1.9  christos 
    376  1.9  christos     case UNOP_FORTRAN_KIND:
    377  1.9  christos       print_unop_subexp_f (exp, pos, stream, prec, "KIND");
    378  1.9  christos       return;
    379  1.9  christos 
    380  1.9  christos     case UNOP_FORTRAN_FLOOR:
    381  1.9  christos       print_unop_subexp_f (exp, pos, stream, prec, "FLOOR");
    382  1.9  christos       return;
    383  1.9  christos 
    384  1.9  christos     case UNOP_FORTRAN_CEILING:
    385  1.9  christos       print_unop_subexp_f (exp, pos, stream, prec, "CEILING");
    386  1.9  christos       return;
    387  1.9  christos 
    388  1.9  christos     case BINOP_FORTRAN_CMPLX:
    389  1.9  christos       print_binop_subexp_f (exp, pos, stream, prec, "CMPLX");
    390  1.9  christos       return;
    391  1.9  christos 
    392  1.9  christos     case BINOP_FORTRAN_MODULO:
    393  1.9  christos       print_binop_subexp_f (exp, pos, stream, prec, "MODULO");
    394  1.1  christos       return;
    395  1.1  christos     }
    396  1.9  christos }
    397  1.1  christos 
    398  1.7  christos /* Special expression names for Fortran.  */
    399  1.9  christos 
    400  1.9  christos static const char *
    401  1.9  christos op_name_f (enum exp_opcode opcode)
    402  1.9  christos {
    403  1.9  christos   switch (opcode)
    404  1.9  christos     {
    405  1.9  christos     default:
    406  1.9  christos       return op_name_standard (opcode);
    407  1.9  christos 
    408  1.9  christos #define OP(name)	\
    409  1.9  christos     case name:		\
    410  1.9  christos       return #name ;
    411  1.9  christos #include "fortran-operator.def"
    412  1.9  christos #undef OP
    413  1.9  christos     }
    414  1.9  christos }
    415  1.9  christos 
    416  1.9  christos /* Special expression dumping for Fortran.  */
    417  1.9  christos 
    418  1.9  christos static int
    419  1.1  christos dump_subexp_body_f (struct expression *exp,
    420  1.9  christos 		    struct ui_file *stream, int elt)
    421  1.9  christos {
    422  1.1  christos   int opcode = exp->elts[elt].opcode;
    423  1.9  christos   int oplen, nargs, i;
    424  1.1  christos 
    425  1.9  christos   switch (opcode)
    426  1.9  christos     {
    427  1.1  christos     default:
    428  1.9  christos       return dump_subexp_body_standard (exp, stream, elt);
    429  1.9  christos 
    430  1.9  christos     case UNOP_FORTRAN_KIND:
    431  1.9  christos     case UNOP_FORTRAN_FLOOR:
    432  1.9  christos     case UNOP_FORTRAN_CEILING:
    433  1.9  christos     case BINOP_FORTRAN_CMPLX:
    434  1.9  christos     case BINOP_FORTRAN_MODULO:
    435  1.9  christos       operator_length_f (exp, (elt + 1), &oplen, &nargs);
    436  1.9  christos       break;
    437  1.9  christos     }
    438  1.9  christos 
    439  1.9  christos   elt += oplen;
    440  1.1  christos   for (i = 0; i < nargs; i += 1)
    441  1.9  christos     elt = dump_subexp (exp, stream, elt);
    442  1.1  christos 
    443  1.1  christos   return elt;
    444  1.9  christos }
    445  1.9  christos 
    446  1.9  christos /* Special expression checking for Fortran.  */
    447  1.9  christos 
    448  1.9  christos static int
    449  1.9  christos operator_check_f (struct expression *exp, int pos,
    450  1.9  christos 		  int (*objfile_func) (struct objfile *objfile,
    451  1.9  christos 				       void *data),
    452  1.9  christos 		  void *data)
    453  1.9  christos {
    454  1.9  christos   const union exp_element *const elts = exp->elts;
    455  1.9  christos 
    456  1.9  christos   switch (elts[pos].opcode)
    457  1.9  christos     {
    458  1.9  christos     case UNOP_FORTRAN_KIND:
    459  1.9  christos     case UNOP_FORTRAN_FLOOR:
    460  1.9  christos     case UNOP_FORTRAN_CEILING:
    461  1.9  christos     case BINOP_FORTRAN_CMPLX:
    462  1.9  christos     case BINOP_FORTRAN_MODULO:
    463  1.9  christos       /* Any references to objfiles are held in the arguments to this
    464  1.9  christos 	 expression, not within the expression itself, so no additional
    465  1.9  christos 	 checking is required here, the outer expression iteration code
    466  1.9  christos 	 will take care of checking each argument.  */
    467  1.9  christos       break;
    468  1.9  christos 
    469  1.9  christos     default:
    470  1.1  christos       return operator_check_standard (exp, pos, objfile_func, data);
    471  1.9  christos     }
    472  1.1  christos 
    473  1.1  christos   return 0;
    474  1.6  christos }
    475  1.6  christos 
    476  1.6  christos static const char *f_extensions[] =
    477  1.6  christos {
    478  1.6  christos   ".f", ".F", ".for", ".FOR", ".ftn", ".FTN", ".fpp", ".FPP",
    479  1.6  christos   ".f90", ".F90", ".f95", ".F95", ".f03", ".F03", ".f08", ".F08",
    480  1.6  christos   NULL
    481  1.9  christos };
    482  1.9  christos 
    483  1.9  christos /* Expression processing for Fortran.  */
    484  1.9  christos static const struct exp_descriptor exp_descriptor_f =
    485  1.9  christos {
    486  1.9  christos   print_subexp_f,
    487  1.9  christos   operator_length_f,
    488  1.9  christos   operator_check_f,
    489  1.9  christos   op_name_f,
    490  1.9  christos   dump_subexp_body_f,
    491  1.9  christos   evaluate_subexp_f
    492  1.9  christos };
    493  1.9  christos 
    494  1.9  christos /* Constant data that describes the Fortran language.  */
    495  1.1  christos 
    496  1.1  christos extern const struct language_data f_language_data =
    497  1.1  christos {
    498  1.1  christos   "fortran",
    499  1.1  christos   "Fortran",
    500  1.1  christos   language_fortran,
    501  1.1  christos   range_check_on,
    502  1.1  christos   case_sensitive_off,
    503  1.6  christos   array_column_major,
    504  1.9  christos   macro_expansion_no,
    505  1.1  christos   f_extensions,
    506  1.8  christos   &exp_descriptor_f,
    507  1.1  christos   NULL,                    	/* name_of_this */
    508  1.1  christos   false,			/* la_store_sym_names_in_linkage_form_p */
    509  1.1  christos   f_op_print_tab,		/* expression operators for printing */
    510  1.1  christos   0,				/* arrays are first-class (not c-style) */
    511  1.9  christos   1,				/* String lower bound */
    512  1.9  christos   &default_varobj_ops,
    513  1.9  christos   "(...)"			/* la_struct_too_deep_ellipsis */
    514  1.9  christos };
    515  1.9  christos 
    516  1.9  christos /* Class representing the Fortran language.  */
    517  1.9  christos 
    518  1.9  christos class f_language : public language_defn
    519  1.9  christos {
    520  1.9  christos public:
    521  1.9  christos   f_language ()
    522  1.9  christos     : language_defn (language_fortran, f_language_data)
    523  1.9  christos   { /* Nothing.  */ }
    524  1.9  christos 
    525  1.9  christos   /* See language.h.  */
    526  1.9  christos   void language_arch_info (struct gdbarch *gdbarch,
    527  1.9  christos 			   struct language_arch_info *lai) const override
    528  1.9  christos   {
    529  1.9  christos     const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
    530  1.9  christos 
    531  1.9  christos     lai->string_char_type = builtin->builtin_character;
    532  1.9  christos     lai->primitive_type_vector
    533  1.9  christos       = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_f_primitive_types + 1,
    534  1.9  christos 				struct type *);
    535  1.9  christos 
    536  1.9  christos     lai->primitive_type_vector [f_primitive_type_character]
    537  1.9  christos       = builtin->builtin_character;
    538  1.9  christos     lai->primitive_type_vector [f_primitive_type_logical]
    539  1.9  christos       = builtin->builtin_logical;
    540  1.9  christos     lai->primitive_type_vector [f_primitive_type_logical_s1]
    541  1.9  christos       = builtin->builtin_logical_s1;
    542  1.9  christos     lai->primitive_type_vector [f_primitive_type_logical_s2]
    543  1.9  christos       = builtin->builtin_logical_s2;
    544  1.9  christos     lai->primitive_type_vector [f_primitive_type_logical_s8]
    545  1.9  christos       = builtin->builtin_logical_s8;
    546  1.9  christos     lai->primitive_type_vector [f_primitive_type_real]
    547  1.9  christos       = builtin->builtin_real;
    548  1.9  christos     lai->primitive_type_vector [f_primitive_type_real_s8]
    549  1.9  christos       = builtin->builtin_real_s8;
    550  1.9  christos     lai->primitive_type_vector [f_primitive_type_real_s16]
    551  1.9  christos       = builtin->builtin_real_s16;
    552  1.9  christos     lai->primitive_type_vector [f_primitive_type_complex_s8]
    553  1.9  christos       = builtin->builtin_complex_s8;
    554  1.9  christos     lai->primitive_type_vector [f_primitive_type_complex_s16]
    555  1.9  christos       = builtin->builtin_complex_s16;
    556  1.9  christos     lai->primitive_type_vector [f_primitive_type_void]
    557  1.9  christos       = builtin->builtin_void;
    558  1.9  christos 
    559  1.9  christos     lai->bool_type_symbol = "logical";
    560  1.9  christos     lai->bool_type_default = builtin->builtin_logical_s2;
    561  1.9  christos   }
    562  1.9  christos 
    563  1.9  christos   /* See language.h.  */
    564  1.9  christos   unsigned int search_name_hash (const char *name) const override
    565  1.9  christos   {
    566  1.9  christos     return cp_search_name_hash (name);
    567  1.9  christos   }
    568  1.9  christos 
    569  1.9  christos   /* See language.h.  */
    570  1.9  christos 
    571  1.9  christos   char *demangle (const char *mangled, int options) const override
    572  1.9  christos   {
    573  1.9  christos       /* We could support demangling here to provide module namespaces
    574  1.9  christos 	 also for inferiors with only minimal symbol table (ELF symbols).
    575  1.9  christos 	 Just the mangling standard is not standardized across compilers
    576  1.9  christos 	 and there is no DW_AT_producer available for inferiors with only
    577  1.9  christos 	 the ELF symbols to check the mangling kind.  */
    578  1.9  christos     return nullptr;
    579  1.9  christos   }
    580  1.9  christos 
    581  1.9  christos   /* See language.h.  */
    582  1.9  christos 
    583  1.9  christos   void print_type (struct type *type, const char *varstring,
    584  1.9  christos 		   struct ui_file *stream, int show, int level,
    585  1.9  christos 		   const struct type_print_options *flags) const override
    586  1.9  christos   {
    587  1.9  christos     f_print_type (type, varstring, stream, show, level, flags);
    588  1.9  christos   }
    589  1.9  christos 
    590  1.9  christos   /* See language.h.  This just returns default set of word break
    591  1.9  christos      characters but with the modules separator `::' removed.  */
    592  1.9  christos 
    593  1.9  christos   const char *word_break_characters (void) const override
    594  1.9  christos   {
    595  1.9  christos     static char *retval;
    596  1.9  christos 
    597  1.9  christos     if (!retval)
    598  1.9  christos       {
    599  1.9  christos 	char *s;
    600  1.9  christos 
    601  1.9  christos 	retval = xstrdup (language_defn::word_break_characters ());
    602  1.9  christos 	s = strchr (retval, ':');
    603  1.9  christos 	if (s)
    604  1.9  christos 	  {
    605  1.9  christos 	    char *last_char = &s[strlen (s) - 1];
    606  1.9  christos 
    607  1.9  christos 	    *s = *last_char;
    608  1.9  christos 	    *last_char = 0;
    609  1.9  christos 	  }
    610  1.9  christos       }
    611  1.9  christos     return retval;
    612  1.9  christos   }
    613  1.9  christos 
    614  1.9  christos 
    615  1.9  christos   /* See language.h.  */
    616  1.9  christos 
    617  1.9  christos   void collect_symbol_completion_matches (completion_tracker &tracker,
    618  1.9  christos 					  complete_symbol_mode mode,
    619  1.9  christos 					  symbol_name_match_type name_match_type,
    620  1.9  christos 					  const char *text, const char *word,
    621  1.9  christos 					  enum type_code code) const override
    622  1.9  christos   {
    623  1.9  christos     /* Consider the modules separator :: as a valid symbol name character
    624  1.9  christos        class.  */
    625  1.9  christos     default_collect_symbol_completion_matches_break_on (tracker, mode,
    626  1.9  christos 							name_match_type,
    627  1.9  christos 							text, word, ":",
    628  1.9  christos 							code);
    629  1.9  christos   }
    630  1.9  christos 
    631  1.9  christos   /* See language.h.  */
    632  1.9  christos 
    633  1.9  christos   void value_print_inner
    634  1.9  christos 	(struct value *val, struct ui_file *stream, int recurse,
    635  1.9  christos 	 const struct value_print_options *options) const override
    636  1.9  christos   {
    637  1.9  christos     return f_value_print_inner (val, stream, recurse, options);
    638  1.9  christos   }
    639  1.9  christos 
    640  1.9  christos   /* See language.h.  */
    641  1.9  christos 
    642  1.9  christos   struct block_symbol lookup_symbol_nonlocal
    643  1.9  christos 	(const char *name, const struct block *block,
    644  1.9  christos 	 const domain_enum domain) const override
    645  1.9  christos   {
    646  1.9  christos     return cp_lookup_symbol_nonlocal (this, name, block, domain);
    647  1.9  christos   }
    648  1.9  christos 
    649  1.9  christos   /* See language.h.  */
    650  1.9  christos 
    651  1.9  christos   int parser (struct parser_state *ps) const override
    652  1.9  christos   {
    653  1.9  christos     return f_parse (ps);
    654  1.9  christos   }
    655  1.9  christos 
    656  1.9  christos   /* See language.h.  */
    657  1.9  christos 
    658  1.9  christos   void emitchar (int ch, struct type *chtype,
    659  1.9  christos 		 struct ui_file *stream, int quoter) const override
    660  1.9  christos   {
    661  1.9  christos     const char *encoding = f_get_encoding (chtype);
    662  1.9  christos     generic_emit_char (ch, chtype, stream, quoter, encoding);
    663  1.9  christos   }
    664  1.9  christos 
    665  1.9  christos   /* See language.h.  */
    666  1.9  christos 
    667  1.9  christos   void printchar (int ch, struct type *chtype,
    668  1.9  christos 		  struct ui_file *stream) const override
    669  1.9  christos   {
    670  1.9  christos     fputs_filtered ("'", stream);
    671  1.9  christos     LA_EMIT_CHAR (ch, chtype, stream, '\'');
    672  1.9  christos     fputs_filtered ("'", stream);
    673  1.9  christos   }
    674  1.9  christos 
    675  1.9  christos   /* See language.h.  */
    676  1.9  christos 
    677  1.9  christos   void printstr (struct ui_file *stream, struct type *elttype,
    678  1.9  christos 		 const gdb_byte *string, unsigned int length,
    679  1.9  christos 		 const char *encoding, int force_ellipses,
    680  1.9  christos 		 const struct value_print_options *options) const override
    681  1.9  christos   {
    682  1.9  christos     const char *type_encoding = f_get_encoding (elttype);
    683  1.9  christos 
    684  1.9  christos     if (TYPE_LENGTH (elttype) == 4)
    685  1.9  christos       fputs_filtered ("4_", stream);
    686  1.9  christos 
    687  1.9  christos     if (!encoding || !*encoding)
    688  1.9  christos       encoding = type_encoding;
    689  1.9  christos 
    690  1.9  christos     generic_printstr (stream, elttype, string, length, encoding,
    691  1.9  christos 		      force_ellipses, '\'', 0, options);
    692  1.9  christos   }
    693  1.9  christos 
    694  1.9  christos   /* See language.h.  */
    695  1.9  christos 
    696  1.9  christos   void print_typedef (struct type *type, struct symbol *new_symbol,
    697  1.9  christos 		      struct ui_file *stream) const override
    698  1.9  christos   {
    699  1.9  christos     f_print_typedef (type, new_symbol, stream);
    700  1.9  christos   }
    701  1.9  christos 
    702  1.9  christos   /* See language.h.  */
    703  1.9  christos 
    704  1.9  christos   bool is_string_type_p (struct type *type) const override
    705  1.9  christos   {
    706  1.9  christos     type = check_typedef (type);
    707  1.9  christos     return (type->code () == TYPE_CODE_STRING
    708  1.9  christos 	    || (type->code () == TYPE_CODE_ARRAY
    709  1.9  christos 		&& TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_CHAR));
    710  1.9  christos   }
    711  1.9  christos 
    712  1.9  christos protected:
    713  1.9  christos 
    714  1.9  christos   /* See language.h.  */
    715  1.9  christos 
    716  1.9  christos   symbol_name_matcher_ftype *get_symbol_name_matcher_inner
    717  1.9  christos 	(const lookup_name_info &lookup_name) const override
    718  1.9  christos   {
    719  1.1  christos     return cp_get_symbol_name_matcher (lookup_name);
    720  1.1  christos   }
    721  1.9  christos };
    722  1.9  christos 
    723  1.9  christos /* Single instance of the Fortran language class.  */
    724  1.9  christos 
    725  1.1  christos static f_language f_language_defn;
    726  1.1  christos 
    727  1.1  christos static void *
    728  1.1  christos build_fortran_types (struct gdbarch *gdbarch)
    729  1.1  christos {
    730  1.1  christos   struct builtin_f_type *builtin_f_type
    731  1.1  christos     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
    732  1.9  christos 
    733  1.1  christos   builtin_f_type->builtin_void
    734  1.1  christos     = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
    735  1.9  christos 
    736  1.1  christos   builtin_f_type->builtin_character
    737  1.1  christos     = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
    738  1.1  christos 
    739  1.1  christos   builtin_f_type->builtin_logical_s1
    740  1.1  christos     = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
    741  1.1  christos 
    742  1.1  christos   builtin_f_type->builtin_integer_s2
    743  1.1  christos     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
    744  1.9  christos 			 "integer*2");
    745  1.9  christos 
    746  1.9  christos   builtin_f_type->builtin_integer_s8
    747  1.9  christos     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
    748  1.1  christos 			 "integer*8");
    749  1.1  christos 
    750  1.1  christos   builtin_f_type->builtin_logical_s2
    751  1.1  christos     = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
    752  1.1  christos 			 "logical*2");
    753  1.1  christos 
    754  1.1  christos   builtin_f_type->builtin_logical_s8
    755  1.1  christos     = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
    756  1.1  christos 			 "logical*8");
    757  1.1  christos 
    758  1.1  christos   builtin_f_type->builtin_integer
    759  1.1  christos     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
    760  1.1  christos 			 "integer");
    761  1.1  christos 
    762  1.1  christos   builtin_f_type->builtin_logical
    763  1.1  christos     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
    764  1.1  christos 			 "logical*4");
    765  1.1  christos 
    766  1.7  christos   builtin_f_type->builtin_real
    767  1.1  christos     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
    768  1.1  christos 		       "real", gdbarch_float_format (gdbarch));
    769  1.7  christos   builtin_f_type->builtin_real_s8
    770  1.9  christos     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
    771  1.9  christos 		       "real*8", gdbarch_double_format (gdbarch));
    772  1.9  christos   auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
    773  1.9  christos   if (fmt != nullptr)
    774  1.9  christos     builtin_f_type->builtin_real_s16
    775  1.9  christos       = arch_float_type (gdbarch, 128, "real*16", fmt);
    776  1.9  christos   else if (gdbarch_long_double_bit (gdbarch) == 128)
    777  1.9  christos     builtin_f_type->builtin_real_s16
    778  1.9  christos       = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
    779  1.9  christos 			 "real*16", gdbarch_long_double_format (gdbarch));
    780  1.9  christos   else
    781  1.1  christos     builtin_f_type->builtin_real_s16
    782  1.1  christos       = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
    783  1.9  christos 
    784  1.1  christos   builtin_f_type->builtin_complex_s8
    785  1.9  christos     = init_complex_type ("complex*8", builtin_f_type->builtin_real);
    786  1.9  christos   builtin_f_type->builtin_complex_s16
    787  1.9  christos     = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
    788  1.9  christos 
    789  1.9  christos   if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
    790  1.9  christos     builtin_f_type->builtin_complex_s32
    791  1.9  christos       = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
    792  1.9  christos   else
    793  1.1  christos     builtin_f_type->builtin_complex_s32
    794  1.1  christos       = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
    795  1.1  christos 
    796  1.1  christos   return builtin_f_type;
    797  1.1  christos }
    798  1.1  christos 
    799  1.1  christos static struct gdbarch_data *f_type_data;
    800  1.1  christos 
    801  1.1  christos const struct builtin_f_type *
    802  1.6  christos builtin_f_type (struct gdbarch *gdbarch)
    803  1.1  christos {
    804  1.1  christos   return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
    805  1.9  christos }
    806  1.1  christos 
    807  1.9  christos void _initialize_f_language ();
    808  1.1  christos void
    809  1.1  christos _initialize_f_language ()
    810  1.1  christos {
    811  1.9  christos   f_type_data = gdbarch_data_register_post_init (build_fortran_types);
    812  1.9  christos }
    813  1.9  christos 
    814  1.9  christos /* See f-lang.h.  */
    815  1.9  christos 
    816  1.9  christos struct value *
    817  1.9  christos fortran_argument_convert (struct value *value, bool is_artificial)
    818  1.9  christos {
    819  1.9  christos   if (!is_artificial)
    820  1.9  christos     {
    821  1.9  christos       /* If the value is not in the inferior e.g. registers values,
    822  1.9  christos 	 convenience variables and user input.  */
    823  1.9  christos       if (VALUE_LVAL (value) != lval_memory)
    824  1.9  christos 	{
    825  1.9  christos 	  struct type *type = value_type (value);
    826  1.9  christos 	  const int length = TYPE_LENGTH (type);
    827  1.9  christos 	  const CORE_ADDR addr
    828  1.9  christos 	    = value_as_long (value_allocate_space_in_inferior (length));
    829  1.9  christos 	  write_memory (addr, value_contents (value), length);
    830  1.9  christos 	  struct value *val
    831  1.9  christos 	    = value_from_contents_and_address (type, value_contents (value),
    832  1.9  christos 					       addr);
    833  1.9  christos 	  return value_addr (val);
    834  1.9  christos 	}
    835  1.9  christos       else
    836  1.9  christos 	return value_addr (value); /* Program variables, e.g. arrays.  */
    837  1.9  christos     }
    838  1.9  christos     return value;
    839  1.9  christos }
    840  1.9  christos 
    841  1.9  christos /* See f-lang.h.  */
    842  1.9  christos 
    843  1.9  christos struct type *
    844  1.9  christos fortran_preserve_arg_pointer (struct value *arg, struct type *type)
    845  1.9  christos {
    846  1.9  christos   if (value_type (arg)->code () == TYPE_CODE_PTR)
    847  1.9  christos     return value_type (arg);
    848                  return type;
    849                }
    850