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

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