Blame SOURCES/gdb-rhbz1964167-move-fortran-expr-handling.patch

405ea9
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
405ea9
From: Kevin Buettner <kevinb@redhat.com>
405ea9
Date: Mon, 24 May 2021 17:00:17 -0700
405ea9
Subject: gdb-rhbz1964167-move-fortran-expr-handling.patch
405ea9
405ea9
;; [fortran] Backport Andrew Burgess's commit which moves Fortran
405ea9
;; expression handling to f-lang.c.
405ea9
405ea9
gdb/fortran: Move Fortran expression handling into f-lang.c
405ea9
405ea9
The Fortran specific OP_F77_UNDETERMINED_ARGLIST is currently handled
405ea9
in the generic expression handling code.  There's no reason why this
405ea9
should be the case, so this commit moves handling of this into Fortran
405ea9
specific files.
405ea9
405ea9
There should be no user visible changes after this commit.
405ea9
405ea9
gdb/ChangeLog:
405ea9
405ea9
	* eval.c: Remove 'f-lang.h' include.
405ea9
	(value_f90_subarray): Moved to f-lang.c.
405ea9
	(eval_call): Renamed to...
405ea9
	(evaluate_subexp_do_call): ...this, is no longer static, header
405ea9
	comment moved into header file.
405ea9
	(evaluate_funcall): Update call to eval_call.
405ea9
	(skip_undetermined_arglist): Moved to f-lang.c.
405ea9
	(fortran_value_subarray): Likewise.
405ea9
	(evaluate_subexp_standard): OP_F77_UNDETERMINED_ARGLIST handling
405ea9
	moved to evaluate_subexp_f.
405ea9
	(calc_f77_array_dims): Moved to f-lang.c
405ea9
	* expprint.c (print_subexp_funcall): New function.
405ea9
	(print_subexp_standard): OP_F77_UNDETERMINED_ARGLIST handling
405ea9
	moved to print_subexp_f, OP_FUNCALL uses new function.
405ea9
	(dump_subexp_body_funcall): New function.
405ea9
	(dump_subexp_body_standard): OP_F77_UNDETERMINED_ARGLIST handling
405ea9
	moved to dump_subexp_f, OP_FUNCALL uses new function.
405ea9
	* expression.h (evaluate_subexp_do_call): Declare.
405ea9
	* f-lang.c (value_f90_subarray): Moved from eval.c.
405ea9
	(skip_undetermined_arglist): Likewise.
405ea9
	(calc_f77_array_dims): Likewise.
405ea9
	(fortran_value_subarray): Likewise.
405ea9
	(evaluate_subexp_f): Add OP_F77_UNDETERMINED_ARGLIST support.
405ea9
	(operator_length_f): Likewise.
405ea9
	(print_subexp_f): Likewise.
405ea9
	(dump_subexp_body_f): Likewise.
405ea9
	* fortran-operator.def (OP_F77_UNDETERMINED_ARGLIST): Move
405ea9
	declaration of this operation to here.
405ea9
	* parse.c (operator_length_standard): OP_F77_UNDETERMINED_ARGLIST
405ea9
	support moved to operator_length_f.
405ea9
	* parser-defs.h (dump_subexp_body_funcall): Declare.
405ea9
	(print_subexp_funcall): Declare.
405ea9
	* std-operator.def (OP_F77_UNDETERMINED_ARGLIST): Moved to
405ea9
	fortran-operator.def.
405ea9
405ea9
diff --git a/gdb/eval.c b/gdb/eval.c
405ea9
--- a/gdb/eval.c
405ea9
+++ b/gdb/eval.c
405ea9
@@ -26,7 +26,6 @@
405ea9
 #include "frame.h"
405ea9
 #include "gdbthread.h"
405ea9
 #include "language.h"		/* For CAST_IS_CONVERSION.  */
405ea9
-#include "f-lang.h"		/* For array bound stuff.  */
405ea9
 #include "cp-abi.h"
405ea9
 #include "infcall.h"
405ea9
 #include "objc-lang.h"
405ea9
@@ -371,32 +370,6 @@ init_array_element (struct value *array, struct value *element,
405ea9
   return index;
405ea9
 }
405ea9
 
405ea9
-static struct value *
405ea9
-value_f90_subarray (struct value *array,
405ea9
-		    struct expression *exp, int *pos, enum noside noside)
405ea9
-{
405ea9
-  int pc = (*pos) + 1;
405ea9
-  LONGEST low_bound, high_bound;
405ea9
-  struct type *range = check_typedef (value_type (array)->index_type ());
405ea9
-  enum range_type range_type
405ea9
-    = (enum range_type) longest_to_int (exp->elts[pc].longconst);
405ea9
- 
405ea9
-  *pos += 3;
405ea9
-
405ea9
-  if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
405ea9
-    low_bound = range->bounds ()->low.const_val ();
405ea9
-  else
405ea9
-    low_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
405ea9
-
405ea9
-  if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
405ea9
-    high_bound = range->bounds ()->high.const_val ();
405ea9
-  else
405ea9
-    high_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
405ea9
-
405ea9
-  return value_slice (array, low_bound, high_bound - low_bound + 1);
405ea9
-}
405ea9
-
405ea9
-
405ea9
 /* Promote value ARG1 as appropriate before performing a unary operation
405ea9
    on this argument.
405ea9
    If the result is not appropriate for any particular language then it
405ea9
@@ -749,17 +722,13 @@ eval_skip_value (expression *exp)
405ea9
   return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
405ea9
 }
405ea9
 
405ea9
-/* Evaluate a function call.  The function to be called is in
405ea9
-   ARGVEC[0] and the arguments passed to the function are in
405ea9
-   ARGVEC[1..NARGS].  FUNCTION_NAME is the name of the function, if
405ea9
-   known.  DEFAULT_RETURN_TYPE is used as the function's return type
405ea9
-   if the return type is unknown.  */
405ea9
+/* See expression.h.  */
405ea9
 
405ea9
-static value *
405ea9
-eval_call (expression *exp, enum noside noside,
405ea9
-	   int nargs, value **argvec,
405ea9
-	   const char *function_name,
405ea9
-	   type *default_return_type)
405ea9
+value *
405ea9
+evaluate_subexp_do_call (expression *exp, enum noside noside,
405ea9
+			 int nargs, value **argvec,
405ea9
+			 const char *function_name,
405ea9
+			 type *default_return_type)
405ea9
 {
405ea9
   if (argvec[0] == NULL)
405ea9
     error (_("Cannot evaluate function -- may be inlined"));
405ea9
@@ -1230,20 +1199,8 @@ evaluate_funcall (type *expect_type, expression *exp, int *pos,
405ea9
       /* Nothing to be done; argvec already correctly set up.  */
405ea9
     }
405ea9
 
405ea9
-  return eval_call (exp, noside, nargs, argvec, var_func_name, expect_type);
405ea9
-}
405ea9
-
405ea9
-/* Helper for skipping all the arguments in an undetermined argument list.
405ea9
-   This function was designed for use in the OP_F77_UNDETERMINED_ARGLIST
405ea9
-   case of evaluate_subexp_standard as multiple, but not all, code paths
405ea9
-   require a generic skip.  */
405ea9
-
405ea9
-static void
405ea9
-skip_undetermined_arglist (int nargs, struct expression *exp, int *pos,
405ea9
-			   enum noside noside)
405ea9
-{
405ea9
-  for (int i = 0; i < nargs; ++i)
405ea9
-    evaluate_subexp (nullptr, exp, pos, noside);
405ea9
+  return evaluate_subexp_do_call (exp, noside, nargs, argvec,
405ea9
+				  var_func_name, expect_type);
405ea9
 }
405ea9
 
405ea9
 /* Return true if type is integral or reference to integral */
405ea9
@@ -1260,67 +1217,6 @@ is_integral_or_integral_reference (struct type *type)
405ea9
 	  && is_integral_type (TYPE_TARGET_TYPE (type)));
405ea9
 }
405ea9
 
405ea9
-/* Called from evaluate_subexp_standard to perform array indexing, and
405ea9
-   sub-range extraction, for Fortran.  As well as arrays this function
405ea9
-   also handles strings as they can be treated like arrays of characters.
405ea9
-   ARRAY is the array or string being accessed.  EXP, POS, and NOSIDE are
405ea9
-   as for evaluate_subexp_standard, and NARGS is the number of arguments
405ea9
-   in this access (e.g. 'array (1,2,3)' would be NARGS 3).  */
405ea9
-
405ea9
-static struct value *
405ea9
-fortran_value_subarray (struct value *array, struct expression *exp,
405ea9
-			int *pos, int nargs, enum noside noside)
405ea9
-{
405ea9
-  if (exp->elts[*pos].opcode == OP_RANGE)
405ea9
-    return value_f90_subarray (array, exp, pos, noside);
405ea9
-
405ea9
-  if (noside == EVAL_SKIP)
405ea9
-    {
405ea9
-      skip_undetermined_arglist (nargs, exp, pos, noside);
405ea9
-      /* Return the dummy value with the correct type.  */
405ea9
-      return array;
405ea9
-    }
405ea9
-
405ea9
-  LONGEST subscript_array[MAX_FORTRAN_DIMS];
405ea9
-  int ndimensions = 1;
405ea9
-  struct type *type = check_typedef (value_type (array));
405ea9
-
405ea9
-  if (nargs > MAX_FORTRAN_DIMS)
405ea9
-    error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
405ea9
-
405ea9
-  ndimensions = calc_f77_array_dims (type);
405ea9
-
405ea9
-  if (nargs != ndimensions)
405ea9
-    error (_("Wrong number of subscripts"));
405ea9
-
405ea9
-  gdb_assert (nargs > 0);
405ea9
-
405ea9
-  /* Now that we know we have a legal array subscript expression let us
405ea9
-     actually find out where this element exists in the array.  */
405ea9
-
405ea9
-  /* Take array indices left to right.  */
405ea9
-  for (int i = 0; i < nargs; i++)
405ea9
-    {
405ea9
-      /* Evaluate each subscript; it must be a legal integer in F77.  */
405ea9
-      value *arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
405ea9
-
405ea9
-      /* Fill in the subscript array.  */
405ea9
-      subscript_array[i] = value_as_long (arg2);
405ea9
-    }
405ea9
-
405ea9
-  /* Internal type of array is arranged right to left.  */
405ea9
-  for (int i = nargs; i > 0; i--)
405ea9
-    {
405ea9
-      struct type *array_type = check_typedef (value_type (array));
405ea9
-      LONGEST index = subscript_array[i - 1];
405ea9
-
405ea9
-      array = value_subscripted_rvalue (array, index,
405ea9
-					f77_get_lowerbound (array_type));
405ea9
-    }
405ea9
-
405ea9
-  return array;
405ea9
-}
405ea9
-
405ea9
 struct value *
405ea9
 evaluate_subexp_standard (struct type *expect_type,
405ea9
 			  struct expression *exp, int *pos,
405ea9
@@ -1335,7 +1231,6 @@ evaluate_subexp_standard (struct type *expect_type,
405ea9
   struct type *type;
405ea9
   int nargs;
405ea9
   struct value **argvec;
405ea9
-  int code;
405ea9
   int ix;
405ea9
   long mem_offset;
405ea9
   struct type **arg_types;
405ea9
@@ -1976,84 +1871,6 @@ evaluate_subexp_standard (struct type *expect_type,
405ea9
     case OP_FUNCALL:
405ea9
       return evaluate_funcall (expect_type, exp, pos, noside);
405ea9
 
405ea9
-    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
-
405ea9
-      /* Remember that in F77, functions, substring ops and 
405ea9
-         array subscript operations cannot be disambiguated 
405ea9
-         at parse time.  We have made all array subscript operations, 
405ea9
-         substring operations as well as function calls  come here 
405ea9
-         and we now have to discover what the heck this thing actually was.
405ea9
-         If it is a function, we process just as if we got an OP_FUNCALL.  */
405ea9
-
405ea9
-      nargs = longest_to_int (exp->elts[pc + 1].longconst);
405ea9
-      (*pos) += 2;
405ea9
-
405ea9
-      /* First determine the type code we are dealing with.  */
405ea9
-      arg1 = evaluate_subexp (nullptr, exp, pos, noside);
405ea9
-      type = check_typedef (value_type (arg1));
405ea9
-      code = type->code ();
405ea9
-
405ea9
-      if (code == TYPE_CODE_PTR)
405ea9
-	{
405ea9
-	  /* Fortran always passes variable to subroutines as pointer.
405ea9
-	     So we need to look into its target type to see if it is
405ea9
-	     array, string or function.  If it is, we need to switch
405ea9
-	     to the target value the original one points to.  */ 
405ea9
-	  struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
405ea9
-
405ea9
-	  if (target_type->code () == TYPE_CODE_ARRAY
405ea9
-	      || target_type->code () == TYPE_CODE_STRING
405ea9
-	      || target_type->code () == TYPE_CODE_FUNC)
405ea9
-	    {
405ea9
-	      arg1 = value_ind (arg1);
405ea9
-	      type = check_typedef (value_type (arg1));
405ea9
-	      code = type->code ();
405ea9
-	    }
405ea9
-	} 
405ea9
-
405ea9
-      switch (code)
405ea9
-	{
405ea9
-	case TYPE_CODE_ARRAY:
405ea9
-	case TYPE_CODE_STRING:
405ea9
-	  return fortran_value_subarray (arg1, exp, pos, nargs, noside);
405ea9
-
405ea9
-	case TYPE_CODE_PTR:
405ea9
-	case TYPE_CODE_FUNC:
405ea9
-	case TYPE_CODE_INTERNAL_FUNCTION:
405ea9
-	  /* It's a function call.  */
405ea9
-	  /* Allocate arg vector, including space for the function to be
405ea9
-	     called in argvec[0] and a terminating NULL.  */
405ea9
-	  argvec = (struct value **)
405ea9
-	    alloca (sizeof (struct value *) * (nargs + 2));
405ea9
-	  argvec[0] = arg1;
405ea9
-	  tem = 1;
405ea9
-	  for (; tem <= nargs; tem++)
405ea9
-	    {
405ea9
-	      argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
405ea9
-	      /* Arguments in Fortran are passed by address.  Coerce the
405ea9
-		 arguments here rather than in value_arg_coerce as otherwise
405ea9
-		 the call to malloc to place the non-lvalue parameters in
405ea9
-		 target memory is hit by this Fortran specific logic.  This
405ea9
-		 results in malloc being called with a pointer to an integer
405ea9
-		 followed by an attempt to malloc the arguments to malloc in
405ea9
-		 target memory.  Infinite recursion ensues.  */
405ea9
-	      if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
405ea9
-		{
405ea9
-		  bool is_artificial
405ea9
-		    = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
405ea9
-		  argvec[tem] = fortran_argument_convert (argvec[tem],
405ea9
-							  is_artificial);
405ea9
-		}
405ea9
-	    }
405ea9
-	  argvec[tem] = 0;	/* signal end of arglist */
405ea9
-	  if (noside == EVAL_SKIP)
405ea9
-	    return eval_skip_value (exp);
405ea9
-	  return eval_call (exp, noside, nargs, argvec, NULL, expect_type);
405ea9
-
405ea9
-	default:
405ea9
-	  error (_("Cannot perform substring on this type"));
405ea9
-	}
405ea9
-
405ea9
     case OP_COMPLEX:
405ea9
       /* We have a complex number, There should be 2 floating 
405ea9
          point numbers that compose it.  */
405ea9
@@ -3346,27 +3163,3 @@ parse_and_eval_type (char *p, int length)
405ea9
     error (_("Internal error in eval_type."));
405ea9
   return expr->elts[1].type;
405ea9
 }
405ea9
-
405ea9
-/* Return the number of dimensions for a Fortran array or string.  */
405ea9
-
405ea9
-int
405ea9
-calc_f77_array_dims (struct type *array_type)
405ea9
-{
405ea9
-  int ndimen = 1;
405ea9
-  struct type *tmp_type;
405ea9
-
405ea9
-  if ((array_type->code () == TYPE_CODE_STRING))
405ea9
-    return 1;
405ea9
-
405ea9
-  if ((array_type->code () != TYPE_CODE_ARRAY))
405ea9
-    error (_("Can't get dimensions for a non-array type"));
405ea9
-
405ea9
-  tmp_type = array_type;
405ea9
-
405ea9
-  while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
405ea9
-    {
405ea9
-      if (tmp_type->code () == TYPE_CODE_ARRAY)
405ea9
-	++ndimen;
405ea9
-    }
405ea9
-  return ndimen;
405ea9
-}
405ea9
diff --git a/gdb/expprint.c b/gdb/expprint.c
405ea9
--- a/gdb/expprint.c
405ea9
+++ b/gdb/expprint.c
405ea9
@@ -53,6 +53,25 @@ print_subexp (struct expression *exp, int *pos,
405ea9
   exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
405ea9
 }
405ea9
 
405ea9
+/* See parser-defs.h.  */
405ea9
+
405ea9
+void
405ea9
+print_subexp_funcall (struct expression *exp, int *pos,
405ea9
+		      struct ui_file *stream)
405ea9
+{
405ea9
+  (*pos) += 2;
405ea9
+  unsigned nargs = longest_to_int (exp->elts[*pos].longconst);
405ea9
+  print_subexp (exp, pos, stream, PREC_SUFFIX);
405ea9
+  fputs_filtered (" (", stream);
405ea9
+  for (unsigned tem = 0; tem < nargs; tem++)
405ea9
+    {
405ea9
+      if (tem != 0)
405ea9
+	fputs_filtered (", ", stream);
405ea9
+      print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
405ea9
+    }
405ea9
+  fputs_filtered (")", stream);
405ea9
+}
405ea9
+
405ea9
 /* Standard implementation of print_subexp for use in language_defn
405ea9
    vectors.  */
405ea9
 void
405ea9
@@ -187,18 +206,7 @@ print_subexp_standard (struct expression *exp, int *pos,
405ea9
       return;
405ea9
 
405ea9
     case OP_FUNCALL:
405ea9
-    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
-      (*pos) += 2;
405ea9
-      nargs = longest_to_int (exp->elts[pc + 1].longconst);
405ea9
-      print_subexp (exp, pos, stream, PREC_SUFFIX);
405ea9
-      fputs_filtered (" (", stream);
405ea9
-      for (tem = 0; tem < nargs; tem++)
405ea9
-	{
405ea9
-	  if (tem != 0)
405ea9
-	    fputs_filtered (", ", stream);
405ea9
-	  print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
405ea9
-	}
405ea9
-      fputs_filtered (")", stream);
405ea9
+      print_subexp_funcall (exp, pos, stream);
405ea9
       return;
405ea9
 
405ea9
     case OP_NAME:
405ea9
@@ -796,6 +804,22 @@ dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
405ea9
   return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
405ea9
 }
405ea9
 
405ea9
+/* See parser-defs.h.  */
405ea9
+
405ea9
+int
405ea9
+dump_subexp_body_funcall (struct expression *exp,
405ea9
+			  struct ui_file *stream, int elt)
405ea9
+{
405ea9
+  int nargs = longest_to_int (exp->elts[elt].longconst);
405ea9
+  fprintf_filtered (stream, "Number of args: %d", nargs);
405ea9
+  elt += 2;
405ea9
+
405ea9
+  for (int i = 1; i <= nargs + 1; i++)
405ea9
+    elt = dump_subexp (exp, stream, elt);
405ea9
+
405ea9
+  return elt;
405ea9
+}
405ea9
+
405ea9
 /* Default value for subexp_body in exp_descriptor vector.  */
405ea9
 
405ea9
 int
405ea9
@@ -931,18 +955,7 @@ dump_subexp_body_standard (struct expression *exp,
405ea9
       elt += 2;
405ea9
       break;
405ea9
     case OP_FUNCALL:
405ea9
-    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
-      {
405ea9
-	int i, nargs;
405ea9
-
405ea9
-	nargs = longest_to_int (exp->elts[elt].longconst);
405ea9
-
405ea9
-	fprintf_filtered (stream, "Number of args: %d", nargs);
405ea9
-	elt += 2;
405ea9
-
405ea9
-	for (i = 1; i <= nargs + 1; i++)
405ea9
-	  elt = dump_subexp (exp, stream, elt);
405ea9
-      }
405ea9
+      elt = dump_subexp_body_funcall (exp, stream, elt);
405ea9
       break;
405ea9
     case OP_ARRAY:
405ea9
       {
405ea9
diff --git a/gdb/expression.h b/gdb/expression.h
405ea9
--- a/gdb/expression.h
405ea9
+++ b/gdb/expression.h
405ea9
@@ -155,6 +155,18 @@ enum noside
405ea9
 extern struct value *evaluate_subexp_standard
405ea9
   (struct type *, struct expression *, int *, enum noside);
405ea9
 
405ea9
+/* Evaluate a function call.  The function to be called is in ARGVEC[0] and
405ea9
+   the arguments passed to the function are in ARGVEC[1..NARGS].
405ea9
+   FUNCTION_NAME is the name of the function, if known.
405ea9
+   DEFAULT_RETURN_TYPE is used as the function's return type if the return
405ea9
+   type is unknown.  */
405ea9
+
405ea9
+extern struct value *evaluate_subexp_do_call (expression *exp,
405ea9
+					      enum noside noside,
405ea9
+					      int nargs, value **argvec,
405ea9
+					      const char *function_name,
405ea9
+					      type *default_return_type);
405ea9
+
405ea9
 /* From expprint.c */
405ea9
 
405ea9
 extern void print_expression (struct expression *, struct ui_file *);
405ea9
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
405ea9
--- a/gdb/f-lang.c
405ea9
+++ b/gdb/f-lang.c
405ea9
@@ -114,6 +114,134 @@ enum f_primitive_types {
405ea9
   nr_f_primitive_types
405ea9
 };
405ea9
 
405ea9
+/* Called from fortran_value_subarray to take a slice of an array or a
405ea9
+   string.  ARRAY is the array or string to be accessed.  EXP, POS, and
405ea9
+   NOSIDE are as for evaluate_subexp_standard.  Return a value that is a
405ea9
+   slice of the array.  */
405ea9
+
405ea9
+static struct value *
405ea9
+value_f90_subarray (struct value *array,
405ea9
+		    struct expression *exp, int *pos, enum noside noside)
405ea9
+{
405ea9
+  int pc = (*pos) + 1;
405ea9
+  LONGEST low_bound, high_bound;
405ea9
+  struct type *range = check_typedef (value_type (array)->index_type ());
405ea9
+  enum range_type range_type
405ea9
+    = (enum range_type) longest_to_int (exp->elts[pc].longconst);
405ea9
+
405ea9
+  *pos += 3;
405ea9
+
405ea9
+  if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
405ea9
+    low_bound = range->bounds ()->low.const_val ();
405ea9
+  else
405ea9
+    low_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
405ea9
+
405ea9
+  if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
405ea9
+    high_bound = range->bounds ()->high.const_val ();
405ea9
+  else
405ea9
+    high_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
405ea9
+
405ea9
+  return value_slice (array, low_bound, high_bound - low_bound + 1);
405ea9
+}
405ea9
+
405ea9
+/* Helper for skipping all the arguments in an undetermined argument list.
405ea9
+   This function was designed for use in the OP_F77_UNDETERMINED_ARGLIST
405ea9
+   case of evaluate_subexp_standard as multiple, but not all, code paths
405ea9
+   require a generic skip.  */
405ea9
+
405ea9
+static void
405ea9
+skip_undetermined_arglist (int nargs, struct expression *exp, int *pos,
405ea9
+			   enum noside noside)
405ea9
+{
405ea9
+  for (int i = 0; i < nargs; ++i)
405ea9
+    evaluate_subexp (nullptr, exp, pos, noside);
405ea9
+}
405ea9
+
405ea9
+/* Return the number of dimensions for a Fortran array or string.  */
405ea9
+
405ea9
+int
405ea9
+calc_f77_array_dims (struct type *array_type)
405ea9
+{
405ea9
+  int ndimen = 1;
405ea9
+  struct type *tmp_type;
405ea9
+
405ea9
+  if ((array_type->code () == TYPE_CODE_STRING))
405ea9
+    return 1;
405ea9
+
405ea9
+  if ((array_type->code () != TYPE_CODE_ARRAY))
405ea9
+    error (_("Can't get dimensions for a non-array type"));
405ea9
+
405ea9
+  tmp_type = array_type;
405ea9
+
405ea9
+  while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
405ea9
+    {
405ea9
+      if (tmp_type->code () == TYPE_CODE_ARRAY)
405ea9
+	++ndimen;
405ea9
+    }
405ea9
+  return ndimen;
405ea9
+}
405ea9
+
405ea9
+/* Called from evaluate_subexp_standard to perform array indexing, and
405ea9
+   sub-range extraction, for Fortran.  As well as arrays this function
405ea9
+   also handles strings as they can be treated like arrays of characters.
405ea9
+   ARRAY is the array or string being accessed.  EXP, POS, and NOSIDE are
405ea9
+   as for evaluate_subexp_standard, and NARGS is the number of arguments
405ea9
+   in this access (e.g. 'array (1,2,3)' would be NARGS 3).  */
405ea9
+
405ea9
+static struct value *
405ea9
+fortran_value_subarray (struct value *array, struct expression *exp,
405ea9
+			int *pos, int nargs, enum noside noside)
405ea9
+{
405ea9
+  if (exp->elts[*pos].opcode == OP_RANGE)
405ea9
+    return value_f90_subarray (array, exp, pos, noside);
405ea9
+
405ea9
+  if (noside == EVAL_SKIP)
405ea9
+    {
405ea9
+      skip_undetermined_arglist (nargs, exp, pos, noside);
405ea9
+      /* Return the dummy value with the correct type.  */
405ea9
+      return array;
405ea9
+    }
405ea9
+
405ea9
+  LONGEST subscript_array[MAX_FORTRAN_DIMS];
405ea9
+  int ndimensions = 1;
405ea9
+  struct type *type = check_typedef (value_type (array));
405ea9
+
405ea9
+  if (nargs > MAX_FORTRAN_DIMS)
405ea9
+    error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
405ea9
+
405ea9
+  ndimensions = calc_f77_array_dims (type);
405ea9
+
405ea9
+  if (nargs != ndimensions)
405ea9
+    error (_("Wrong number of subscripts"));
405ea9
+
405ea9
+  gdb_assert (nargs > 0);
405ea9
+
405ea9
+  /* Now that we know we have a legal array subscript expression let us
405ea9
+     actually find out where this element exists in the array.  */
405ea9
+
405ea9
+  /* Take array indices left to right.  */
405ea9
+  for (int i = 0; i < nargs; i++)
405ea9
+    {
405ea9
+      /* Evaluate each subscript; it must be a legal integer in F77.  */
405ea9
+      value *arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
405ea9
+
405ea9
+      /* Fill in the subscript array.  */
405ea9
+      subscript_array[i] = value_as_long (arg2);
405ea9
+    }
405ea9
+
405ea9
+  /* Internal type of array is arranged right to left.  */
405ea9
+  for (int i = nargs; i > 0; i--)
405ea9
+    {
405ea9
+      struct type *array_type = check_typedef (value_type (array));
405ea9
+      LONGEST index = subscript_array[i - 1];
405ea9
+
405ea9
+      array = value_subscripted_rvalue (array, index,
405ea9
+					f77_get_lowerbound (array_type));
405ea9
+    }
405ea9
+
405ea9
+  return array;
405ea9
+}
405ea9
+
405ea9
 /* Special expression evaluation cases for Fortran.  */
405ea9
 
405ea9
 static struct value *
405ea9
@@ -285,6 +413,87 @@ evaluate_subexp_f (struct type *expect_type, struct expression *exp,
405ea9
 				   TYPE_LENGTH (type));
405ea9
       return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
405ea9
 				 TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
405ea9
+
405ea9
+
405ea9
+    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
+      /* Remember that in F77, functions, substring ops and array subscript
405ea9
+         operations cannot be disambiguated at parse time.  We have made
405ea9
+         all array subscript operations, substring operations as well as
405ea9
+         function calls come here and we now have to discover what the heck
405ea9
+         this thing actually was.  If it is a function, we process just as
405ea9
+         if we got an OP_FUNCALL.  */
405ea9
+      int nargs = longest_to_int (exp->elts[pc + 1].longconst);
405ea9
+      (*pos) += 2;
405ea9
+
405ea9
+      /* First determine the type code we are dealing with.  */
405ea9
+      arg1 = evaluate_subexp (nullptr, exp, pos, noside);
405ea9
+      type = check_typedef (value_type (arg1));
405ea9
+      enum type_code code = type->code ();
405ea9
+
405ea9
+      if (code == TYPE_CODE_PTR)
405ea9
+	{
405ea9
+	  /* Fortran always passes variable to subroutines as pointer.
405ea9
+	     So we need to look into its target type to see if it is
405ea9
+	     array, string or function.  If it is, we need to switch
405ea9
+	     to the target value the original one points to.  */
405ea9
+	  struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
405ea9
+
405ea9
+	  if (target_type->code () == TYPE_CODE_ARRAY
405ea9
+	      || target_type->code () == TYPE_CODE_STRING
405ea9
+	      || target_type->code () == TYPE_CODE_FUNC)
405ea9
+	    {
405ea9
+	      arg1 = value_ind (arg1);
405ea9
+	      type = check_typedef (value_type (arg1));
405ea9
+	      code = type->code ();
405ea9
+	    }
405ea9
+	}
405ea9
+
405ea9
+      switch (code)
405ea9
+	{
405ea9
+	case TYPE_CODE_ARRAY:
405ea9
+	case TYPE_CODE_STRING:
405ea9
+	  return fortran_value_subarray (arg1, exp, pos, nargs, noside);
405ea9
+
405ea9
+	case TYPE_CODE_PTR:
405ea9
+	case TYPE_CODE_FUNC:
405ea9
+	case TYPE_CODE_INTERNAL_FUNCTION:
405ea9
+	  {
405ea9
+	    /* It's a function call.  Allocate arg vector, including
405ea9
+	    space for the function to be called in argvec[0] and a
405ea9
+	    termination NULL.  */
405ea9
+	    struct value **argvec = (struct value **)
405ea9
+	      alloca (sizeof (struct value *) * (nargs + 2));
405ea9
+	    argvec[0] = arg1;
405ea9
+	    int tem = 1;
405ea9
+	    for (; tem <= nargs; tem++)
405ea9
+	      {
405ea9
+		argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
405ea9
+		/* Arguments in Fortran are passed by address.  Coerce the
405ea9
+		   arguments here rather than in value_arg_coerce as
405ea9
+		   otherwise the call to malloc to place the non-lvalue
405ea9
+		   parameters in target memory is hit by this Fortran
405ea9
+		   specific logic.  This results in malloc being called
405ea9
+		   with a pointer to an integer followed by an attempt to
405ea9
+		   malloc the arguments to malloc in target memory.
405ea9
+		   Infinite recursion ensues.  */
405ea9
+		if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
405ea9
+		  {
405ea9
+		    bool is_artificial
405ea9
+		      = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
405ea9
+		    argvec[tem] = fortran_argument_convert (argvec[tem],
405ea9
+							    is_artificial);
405ea9
+		  }
405ea9
+	      }
405ea9
+	    argvec[tem] = 0;	/* signal end of arglist */
405ea9
+	    if (noside == EVAL_SKIP)
405ea9
+	      return eval_skip_value (exp);
405ea9
+	    return evaluate_subexp_do_call (exp, noside, nargs, argvec, NULL,
405ea9
+					    expect_type);
405ea9
+	  }
405ea9
+
405ea9
+	default:
405ea9
+	  error (_("Cannot perform substring on this type"));
405ea9
+	}
405ea9
     }
405ea9
 
405ea9
   /* Should be unreachable.  */
405ea9
@@ -318,6 +527,11 @@ operator_length_f (const struct expression *exp, int pc, int *oplenp,
405ea9
       oplen = 1;
405ea9
       args = 2;
405ea9
       break;
405ea9
+
405ea9
+    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
+      oplen = 3;
405ea9
+      args = 1 + longest_to_int (exp->elts[pc - 2].longconst);
405ea9
+      break;
405ea9
     }
405ea9
 
405ea9
   *oplenp = oplen;
405ea9
@@ -390,6 +604,10 @@ print_subexp_f (struct expression *exp, int *pos,
405ea9
     case BINOP_FORTRAN_MODULO:
405ea9
       print_binop_subexp_f (exp, pos, stream, prec, "MODULO");
405ea9
       return;
405ea9
+
405ea9
+    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
+      print_subexp_funcall (exp, pos, stream);
405ea9
+      return;
405ea9
     }
405ea9
 }
405ea9
 
405ea9
@@ -432,6 +650,9 @@ dump_subexp_body_f (struct expression *exp,
405ea9
     case BINOP_FORTRAN_MODULO:
405ea9
       operator_length_f (exp, (elt + 1), &oplen, &nargs);
405ea9
       break;
405ea9
+
405ea9
+    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
+      return dump_subexp_body_funcall (exp, stream, elt);
405ea9
     }
405ea9
 
405ea9
   elt += oplen;
405ea9
diff --git a/gdb/fortran-operator.def b/gdb/fortran-operator.def
405ea9
--- a/gdb/fortran-operator.def
405ea9
+++ b/gdb/fortran-operator.def
405ea9
@@ -17,6 +17,14 @@
405ea9
    You should have received a copy of the GNU General Public License
405ea9
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
405ea9
 
405ea9
+/* This is EXACTLY like OP_FUNCALL but is semantically different.
405ea9
+   In F77, array subscript expressions, substring expressions and
405ea9
+   function calls are all exactly the same syntactically.  They
405ea9
+   may only be disambiguated at runtime.  Thus this operator,
405ea9
+   which indicates that we have found something of the form
405ea9
+   <name> ( <stuff> ).  */
405ea9
+OP (OP_F77_UNDETERMINED_ARGLIST)
405ea9
+
405ea9
 /* Single operand builtins.  */
405ea9
 OP (UNOP_FORTRAN_KIND)
405ea9
 OP (UNOP_FORTRAN_FLOOR)
405ea9
diff --git a/gdb/parse.c b/gdb/parse.c
405ea9
--- a/gdb/parse.c
405ea9
+++ b/gdb/parse.c
405ea9
@@ -817,7 +817,6 @@ operator_length_standard (const struct expression *expr, int endpos,
405ea9
       break;
405ea9
 
405ea9
     case OP_FUNCALL:
405ea9
-    case OP_F77_UNDETERMINED_ARGLIST:
405ea9
       oplen = 3;
405ea9
       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
405ea9
       break;
405ea9
diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h
405ea9
--- a/gdb/parser-defs.h
405ea9
+++ b/gdb/parser-defs.h
405ea9
@@ -338,6 +338,13 @@ extern int dump_subexp (struct expression *, struct ui_file *, int);
405ea9
 extern int dump_subexp_body_standard (struct expression *, 
405ea9
 				      struct ui_file *, int);
405ea9
 
405ea9
+/* Dump (to STREAM) a function call like expression at position ELT in the
405ea9
+   expression array EXP.  Return a new value for ELT just after the
405ea9
+   function call expression.  */
405ea9
+
405ea9
+extern int dump_subexp_body_funcall (struct expression *exp,
405ea9
+				     struct ui_file *stream, int elt);
405ea9
+
405ea9
 extern void operator_length (const struct expression *, int, int *, int *);
405ea9
 
405ea9
 extern void operator_length_standard (const struct expression *, int, int *,
405ea9
@@ -440,6 +447,15 @@ extern void print_subexp (struct expression *, int *, struct ui_file *,
405ea9
 extern void print_subexp_standard (struct expression *, int *, 
405ea9
 				   struct ui_file *, enum precedence);
405ea9
 
405ea9
+/* Print a function call like expression to STREAM.  This is called as a
405ea9
+   helper function by which point the expression node identifying this as a
405ea9
+   function call has already been stripped off and POS should point to the
405ea9
+   number of function call arguments.  EXP is the object containing the
405ea9
+   list of expression elements.  */
405ea9
+
405ea9
+extern void print_subexp_funcall (struct expression *exp, int *pos,
405ea9
+				  struct ui_file *stream);
405ea9
+
405ea9
 /* Function used to avoid direct calls to fprintf
405ea9
    in the code generated by the bison parser.  */
405ea9
 
405ea9
diff --git a/gdb/std-operator.def b/gdb/std-operator.def
405ea9
--- a/gdb/std-operator.def
405ea9
+++ b/gdb/std-operator.def
405ea9
@@ -168,14 +168,6 @@ OP (OP_FUNCALL)
405ea9
    pointer.  This is an Objective C message.  */
405ea9
 OP (OP_OBJC_MSGCALL)
405ea9
 
405ea9
-/* This is EXACTLY like OP_FUNCALL but is semantically different.
405ea9
-   In F77, array subscript expressions, substring expressions and
405ea9
-   function calls are all exactly the same syntactically.  They
405ea9
-   may only be disambiguated at runtime.  Thus this operator,
405ea9
-   which indicates that we have found something of the form
405ea9
-   <name> ( <stuff> ).  */
405ea9
-OP (OP_F77_UNDETERMINED_ARGLIST)
405ea9
-
405ea9
 /* OP_COMPLEX takes a type in the following element, followed by another
405ea9
    OP_COMPLEX, making three exp_elements.  It is followed by two double
405ea9
    args, and converts them into a complex number of the given type.  */