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