Blame SOURCES/gdb-fortran-stride-intel-1of6.patch

be09dc
http://sourceware.org/ml/gdb-patches/2016-02/msg00843.html
be09dc
Subject: [PATCH v2 1/6] fortran: allow multi-dimensional subarrays
be09dc
be09dc
From: Christoph Weinmann <christoph.t.weinmann@intel.com>
be09dc
be09dc
Add an argument count for subrange expressions in Fortran.
be09dc
Based on the counted value calculate a new array with the
be09dc
elements specified by the user.  First parse the user input,
be09dc
secondly copy the desired array values into the return
be09dc
array, thirdly re-create the necessary ranges and bounds.
be09dc
be09dc
1|  program prog
be09dc
2|    integer :: ary(10,5) = (/ (i,i=1,10) (j, j=1,5) /)
be09dc
3|  end program prog
be09dc
be09dc
(gdb) print ary(2:4,1:3)
be09dc
old> Syntax error in expression near ':3'
be09dc
new> $3 = ( ( 21, 31, 41) ( 22, 32, 42) ( 23, 33, 43) )
be09dc
be09dc
2013-11-25  Christoph Weinmann  <christoph.t.weinmann@intel.com>
be09dc
be09dc
	* eval.c (multi_f77_subscript): Remove function.
be09dc
	* eval.c (evaluate_subrange_expr): When evaluating
be09dc
	an array or string expression, call
be09dc
	value_f90_subarray.
be09dc
	* eval.c (value_f90_subarray): Add argument parsing
be09dc
	and compute result array based on user input.
be09dc
	* f-exp.y: Increment argument counter for every subrange
be09dc
	expression entered by the user.
be09dc
	* valops.c (value_slice): Call value_slice_1 with
be09dc
	additional default argument.
be09dc
	* valops.c (value_slice_1): Add functionality to
be09dc
	copy and return result values based on input.
be09dc
	* value.h: Add function definition.
be09dc
be09dc
be09dc
Signed-off-by: Christoph Weinmann <christoph.t.weinmann@intel.com>
be09dc
---
be09dc
 gdb/eval.c   | 309 ++++++++++++++++++++++++++++++++++++++++++++++-------------
be09dc
 gdb/f-exp.y  |   2 +
be09dc
 gdb/valops.c | 157 ++++++++++++++++++++++++------
be09dc
 gdb/value.h  |   2 +
be09dc
 4 files changed, 375 insertions(+), 95 deletions(-)
be09dc
be09dc
diff --git a/gdb/eval.c b/gdb/eval.c
be09dc
index 78ad946..c9f325f 100644
be09dc
--- a/gdb/eval.c
be09dc
+++ b/gdb/eval.c
be09dc
@@ -399,29 +399,253 @@ init_array_element (struct value *array, struct value *element,
be09dc
   return index;
be09dc
 }
be09dc
 
be09dc
+/* Evaluates any operation on Fortran arrays or strings with at least
be09dc
+   one user provided parameter.  Expects the input ARRAY to be either
be09dc
+   an array, or a string.  Evaluates EXP by incrementing POS, and
be09dc
+   writes the content from the elt stack into a local struct.  NARGS
be09dc
+   specifies number of literal or range arguments the user provided.
be09dc
+   NARGS must be the same number as ARRAY has dimensions.  */
be09dc
+
be09dc
 static struct value *
be09dc
-value_f90_subarray (struct value *array,
be09dc
-		    struct expression *exp, int *pos, enum noside noside)
be09dc
+value_f90_subarray (struct value *array, struct expression *exp,
be09dc
+		    int *pos, int nargs, enum noside noside)
be09dc
 {
be09dc
-  int pc = (*pos) + 1;
be09dc
+  int i, dim_count = 0;
be09dc
   LONGEST low_bound, high_bound;
be09dc
   struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array)));
be09dc
-  enum f90_range_type range_type
be09dc
-    = (enum f90_range_type) longest_to_int (exp->elts[pc].longconst);
be09dc
- 
be09dc
-  *pos += 3;
be09dc
+  struct value *new_array = array;
be09dc
+  struct type *array_type = check_typedef (value_type (new_array));
be09dc
+  struct type *temp_type;
be09dc
+
be09dc
+  /* Local struct to hold user data for Fortran subarray dimensions.  */
be09dc
+  struct subscript_store
be09dc
+  {
be09dc
+    /* For every dimension, we are either working on a range or an index
be09dc
+       expression, so we store this info separately for later.  */
be09dc
+    enum
be09dc
+    {
be09dc
+      SUBSCRIPT_RANGE,    /* e.g. "(lowbound:highbound)"  */
be09dc
+      SUBSCRIPT_INDEX    /* e.g. "(literal)"  */
be09dc
+    } kind;
be09dc
+
be09dc
+    /* We also store either the lower and upper bound info, or the index
be09dc
+       number.  Before evaluation of the input values, we do not know if we are
be09dc
+       actually working on a range of ranges, or an index in a range.  So as a
be09dc
+       first step we store all input in a union.  The array calculation itself
be09dc
+       deals with this later on.  */
be09dc
+    union
be09dc
+    {
be09dc
+      struct subscript_range
be09dc
+      {
be09dc
+        enum f90_range_type f90_range_type;
be09dc
+        LONGEST low, high;
be09dc
+      }
be09dc
+      range;
be09dc
+      LONGEST number;
be09dc
+    };
be09dc
+  } *subscript_array;
be09dc
+
be09dc
+  /* Check if the number of arguments provided by the user matches
be09dc
+     the number of dimension of the array.  A string has only one
be09dc
+     dimension.  */
be09dc
+  if (nargs != calc_f77_array_dims (value_type (new_array)))
be09dc
+    error (_("Wrong number of subscripts"));
be09dc
+
be09dc
+  subscript_array = alloca (sizeof (*subscript_array) * nargs);
be09dc
+
be09dc
+  /* Parse the user input into the SUBSCRIPT_ARRAY to store it.  We need
be09dc
+     to evaluate it first, as the input is from left-to-right.  The
be09dc
+     array is stored from right-to-left.  So we have to use the user
be09dc
+     input in reverse order.  Later on, we need the input information to
be09dc
+     re-calculate the output array.  For multi-dimensional arrays, we
be09dc
+     can be dealing with any possible combination of ranges and indices
be09dc
+     for every dimension.  */
be09dc
+  for (i = 0; i < nargs; i++)
be09dc
+    {
be09dc
+      struct subscript_store *index = &subscript_array[i];
be09dc
 
be09dc
-  if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
be09dc
-    low_bound = TYPE_LOW_BOUND (range);
be09dc
-  else
be09dc
-    low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
be09dc
+      /* The user input is a range, with or without lower and upper bound.
be09dc
+	 E.g.: "p arry(2:5)", "p arry( :5)", "p arry( : )", etc.  */
be09dc
+      if (exp->elts[*pos].opcode == OP_F90_RANGE)
be09dc
+	{
be09dc
+	  int pc = (*pos) + 1;
be09dc
+	  struct subscript_range *range;
be09dc
+
be09dc
+	  index->kind = SUBSCRIPT_RANGE;
be09dc
+	  range = &index->range;
be09dc
+
be09dc
+	  *pos += 3;
be09dc
+	  range->f90_range_type = longest_to_int (exp->elts[pc].longconst);
be09dc
+
be09dc
+	  /* If a lower bound was provided by the user, the bit has been
be09dc
+	     set and we can assign the value from the elt stack.  Same for
be09dc
+	     upper bound.  */
be09dc
+	  if ((range->f90_range_type == HIGH_BOUND_DEFAULT)
be09dc
+	      || range->f90_range_type == NONE_BOUND_DEFAULT)
be09dc
+	    range->low = value_as_long (evaluate_subexp (NULL_TYPE, exp,
be09dc
+							 pos, noside));
be09dc
+	  if ((range->f90_range_type == LOW_BOUND_DEFAULT)
be09dc
+	      || range->f90_range_type == NONE_BOUND_DEFAULT)
be09dc
+	    range->high = value_as_long (evaluate_subexp (NULL_TYPE, exp,
be09dc
+							  pos, noside));
be09dc
+	}
be09dc
+      /* User input is an index.  E.g.: "p arry(5)".  */
be09dc
+      else
be09dc
+	{
be09dc
+	  struct value *val;
be09dc
 
be09dc
-  if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
be09dc
-    high_bound = TYPE_HIGH_BOUND (range);
be09dc
-  else
be09dc
-    high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
be09dc
+	  index->kind = SUBSCRIPT_INDEX;
be09dc
+
be09dc
+	  /* Evaluate each subscript; it must be a legal integer in F77.  This
be09dc
+	     ensures the validity of the provided index.  */
be09dc
+	  val = evaluate_subexp_with_coercion (exp, pos, noside);
be09dc
+	  index->number = value_as_long (val);
be09dc
+	}
be09dc
+
be09dc
+    }
be09dc
+
be09dc
+  /* Traverse the array from right to left and evaluate each corresponding
be09dc
+     user input.  VALUE_SUBSCRIPT is called for every index, until a range
be09dc
+     expression is evaluated.  After a range expression has been evaluated,
be09dc
+     every subsequent expression is also treated as a range.  */
be09dc
+  for (i = nargs - 1; i >= 0; i--)
be09dc
+    {
be09dc
+      struct subscript_store *index = &subscript_array[i];
be09dc
+      struct type *index_type = TYPE_INDEX_TYPE (array_type);
be09dc
+
be09dc
+      switch (index->kind)
be09dc
+	{
be09dc
+	case SUBSCRIPT_RANGE:
be09dc
+	  {
be09dc
+
be09dc
+	    /* When we hit the first range specified by the user, we must
be09dc
+	       treat any subsequent user entry as a range.  We simply
be09dc
+	       increment DIM_COUNT which tells us how many times we are
be09dc
+	       calling VALUE_SLICE_1.  */
be09dc
+	    struct subscript_range *range = &index->range;
be09dc
+
be09dc
+	    /* If no lower bound was provided by the user, we take the
be09dc
+	       default boundary.  Same for the high bound.  */
be09dc
+	    if ((range->f90_range_type == LOW_BOUND_DEFAULT)
be09dc
+		|| (range->f90_range_type == BOTH_BOUND_DEFAULT))
be09dc
+	      range->low = TYPE_LOW_BOUND (index_type);
be09dc
+
be09dc
+	    if ((range->f90_range_type == HIGH_BOUND_DEFAULT)
be09dc
+		|| (range->f90_range_type == BOTH_BOUND_DEFAULT))
be09dc
+	      range->high = TYPE_HIGH_BOUND (index_type);
be09dc
+
be09dc
+	    /* Both user provided low and high bound have to be inside the
be09dc
+	       array bounds.  Throw an error if not.  */
be09dc
+	    if (range->low < TYPE_LOW_BOUND (index_type)
be09dc
+		|| range->low > TYPE_HIGH_BOUND (index_type)
be09dc
+		|| range->high < TYPE_LOW_BOUND (index_type)
be09dc
+		|| range->high > TYPE_HIGH_BOUND (index_type))
be09dc
+	      error (_("provided bound(s) outside array bound(s)"));
be09dc
+
be09dc
+	    /* DIM_COUNT counts every user argument that is treated as a range.
be09dc
+	       This is necessary for expressions like 'print array(7, 8:9).
be09dc
+	       Here the first argument is a literal, but must be treated as a
be09dc
+	       range argument to allow the correct output representation.  */
be09dc
+	    dim_count++;
be09dc
+
be09dc
+	    new_array
be09dc
+	      = value_slice_1 (new_array,
be09dc
+			       longest_to_int (range->low),
be09dc
+			       longest_to_int (range->high - range->low + 1),
be09dc
+			       dim_count);
be09dc
+	  }
be09dc
+	  break;
be09dc
+
be09dc
+	case SUBSCRIPT_INDEX:
be09dc
+	  {
be09dc
+	    /* DIM_COUNT only stays '0' when no range argument was processed
be09dc
+	       before, starting from the last dimension.  This way we can
be09dc
+	       reduce the number of dimensions from the result array.
be09dc
+	       However, if a range has been processed before an index, we
be09dc
+	       treat the index like a range with equal low- and high bounds
be09dc
+	       to get the value offset right.  */
be09dc
+	    if (dim_count == 0)
be09dc
+	      new_array
be09dc
+	        = value_subscripted_rvalue (new_array, index->number,
be09dc
+					    f77_get_lowerbound (value_type
be09dc
+								  (new_array)));
be09dc
+	    else
be09dc
+	      {
be09dc
+		/* Check for valid index input.  */
be09dc
+		if (index->number < TYPE_LOW_BOUND (index_type)
be09dc
+		    || index->number > TYPE_HIGH_BOUND (index_type))
be09dc
+		  error (_("error no such vector element"));
be09dc
+
be09dc
+		dim_count++;
be09dc
+		new_array = value_slice_1 (new_array,
be09dc
+					   longest_to_int (index->number),
be09dc
+					   1, /* length is '1' element  */
be09dc
+					   dim_count);
be09dc
+	      }
be09dc
+
be09dc
+	  }
be09dc
+	  break;
be09dc
+	}
be09dc
+    }
be09dc
+
be09dc
+  /* With DIM_COUNT > 1 we currently have a one dimensional array, but expect
be09dc
+     an array of arrays, depending on how many ranges have been provided by
be09dc
+     the user.  So we need to rebuild the array dimensions for printing it
be09dc
+     correctly.
be09dc
+     Starting from right to left in the user input, after we hit the first
be09dc
+     range argument every subsequent argument is also treated as a range.
be09dc
+     E.g.:
be09dc
+     "p ary(3, 7, 2:15)" in Fortran has only 1 dimension, but we calculated 3
be09dc
+     ranges.
be09dc
+     "p ary(3, 7:12, 4)" in Fortran has only 1 dimension, but we calculated 2
be09dc
+     ranges.
be09dc
+     "p ary(2:4, 5, 7)" in Fortran has only 1 dimension, and we calculated 1
be09dc
+     range.  */
be09dc
+  if (dim_count > 1)
be09dc
+    {
be09dc
+      struct value *v = NULL;
be09dc
 
be09dc
-  return value_slice (array, low_bound, high_bound - low_bound + 1);
be09dc
+      temp_type = TYPE_TARGET_TYPE (value_type (new_array));
be09dc
+
be09dc
+      /* Every SUBSCRIPT_RANGE in the user input signifies an actual range in
be09dc
+	 the output array.  So we traverse the SUBSCRIPT_ARRAY again, looking
be09dc
+	 for a range entry.  When we find one, we use the range info to create
be09dc
+	 an additional range_type to set the correct bounds and dimensions for
be09dc
+	 the output array.  */
be09dc
+      for (i = 0; i < nargs; i++)
be09dc
+	{
be09dc
+	  struct subscript_store *index = &subscript_array[i];
be09dc
+
be09dc
+	  if (index->kind == SUBSCRIPT_RANGE)
be09dc
+	    {
be09dc
+	      struct type *range_type, *interim_array_type;
be09dc
+
be09dc
+	      range_type
be09dc
+		= create_static_range_type (NULL,
be09dc
+				     temp_type,
be09dc
+				     1,
be09dc
+				     index->range.high - index->range.low + 1);
be09dc
+
be09dc
+	      interim_array_type = create_array_type (NULL,
be09dc
+						      temp_type,
be09dc
+						      range_type);
be09dc
+
be09dc
+	      /* For some reason the type code of the contents is missing, so
be09dc
+		 reset it from the original array.  */
be09dc
+	      TYPE_CODE (interim_array_type)
be09dc
+		= TYPE_CODE (value_type (new_array));
be09dc
+
be09dc
+	      v = allocate_value (interim_array_type);
be09dc
+
be09dc
+	      temp_type = value_type (v);
be09dc
+	    }
be09dc
+
be09dc
+	}
be09dc
+      value_contents_copy (v, 0, new_array, 0, TYPE_LENGTH (temp_type));
be09dc
+      return v;
be09dc
+    }
be09dc
+
be09dc
+  return new_array;
be09dc
 }
be09dc
 
be09dc
 
be09dc
@@ -1810,14 +2034,11 @@ evaluate_subexp_standard (struct type *expect_type,
be09dc
       switch (code)
be09dc
 	{
be09dc
 	case TYPE_CODE_ARRAY:
be09dc
-	  if (exp->elts[*pos].opcode == OP_F90_RANGE)
be09dc
-	    return value_f90_subarray (arg1, exp, pos, noside);
be09dc
-	  else
be09dc
-	    goto multi_f77_subscript;
be09dc
+	  return value_f90_subarray (arg1, exp, pos, nargs, noside);
be09dc
 
be09dc
 	case TYPE_CODE_STRING:
be09dc
 	  if (exp->elts[*pos].opcode == OP_F90_RANGE)
be09dc
-	    return value_f90_subarray (arg1, exp, pos, noside);
be09dc
+	    return value_f90_subarray (arg1, exp, pos, 1, noside);
be09dc
 	  else
be09dc
 	    {
be09dc
 	      arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
be09dc
@@ -2222,49 +2443,6 @@ evaluate_subexp_standard (struct type *expect_type,
be09dc
 	}
be09dc
       return (arg1);
be09dc
 
be09dc
-    multi_f77_subscript:
be09dc
-      {
be09dc
-	LONGEST subscript_array[MAX_FORTRAN_DIMS];
be09dc
-	int ndimensions = 1, i;
be09dc
-	struct value *array = arg1;
be09dc
-
be09dc
-	if (nargs > MAX_FORTRAN_DIMS)
be09dc
-	  error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
be09dc
-
be09dc
-	ndimensions = calc_f77_array_dims (type);
be09dc
-
be09dc
-	if (nargs != ndimensions)
be09dc
-	  error (_("Wrong number of subscripts"));
be09dc
-
be09dc
-	gdb_assert (nargs > 0);
be09dc
-
be09dc
-	/* Now that we know we have a legal array subscript expression 
be09dc
-	   let us actually find out where this element exists in the array.  */
be09dc
-
be09dc
-	/* Take array indices left to right.  */
be09dc
-	for (i = 0; i < nargs; i++)
be09dc
-	  {
be09dc
-	    /* Evaluate each subscript; it must be a legal integer in F77.  */
be09dc
-	    arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
be09dc
-
be09dc
-	    /* Fill in the subscript array.  */
be09dc
-
be09dc
-	    subscript_array[i] = value_as_long (arg2);
be09dc
-	  }
be09dc
-
be09dc
-	/* Internal type of array is arranged right to left.  */
be09dc
-	for (i = nargs; i > 0; i--)
be09dc
-	  {
be09dc
-	    struct type *array_type = check_typedef (value_type (array));
be09dc
-	    LONGEST index = subscript_array[i - 1];
be09dc
-
be09dc
-	    array = value_subscripted_rvalue (array, index,
be09dc
-					      f77_get_lowerbound (array_type));
be09dc
-	  }
be09dc
-
be09dc
-	return array;
be09dc
-      }
be09dc
-
be09dc
     case BINOP_LOGICAL_AND:
be09dc
       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
be09dc
       if (noside == EVAL_SKIP)
be09dc
@@ -3121,6 +3299,9 @@ calc_f77_array_dims (struct type *array_type)
be09dc
   int ndimen = 1;
be09dc
   struct type *tmp_type;
be09dc
 
be09dc
+  if (TYPE_CODE (array_type) == TYPE_CODE_STRING)
be09dc
+    return 1;
be09dc
+
be09dc
   if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
be09dc
     error (_("Can't get dimensions for a non-array type"));
be09dc
 
be09dc
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
be09dc
index 4faac32..9343abb 100644
be09dc
--- a/gdb/f-exp.y
be09dc
+++ b/gdb/f-exp.y
be09dc
@@ -308,6 +308,8 @@ arglist :	subrange
be09dc
    
be09dc
 arglist	:	arglist ',' exp   %prec ABOVE_COMMA
be09dc
 			{ arglist_len++; }
be09dc
+	|	arglist ',' subrange	%prec ABOVE_COMMA
be09dc
+			{ arglist_len++; }
be09dc
 	;
be09dc
 
be09dc
 /* There are four sorts of subrange types in F90.  */
be09dc
diff --git a/gdb/valops.c b/gdb/valops.c
be09dc
index 5a244a9..09ea877 100644
be09dc
--- a/gdb/valops.c
be09dc
+++ b/gdb/valops.c
be09dc
@@ -3759,56 +3759,151 @@ value_of_this_silent (const struct language_defn *lang)
be09dc
 struct value *
be09dc
 value_slice (struct value *array, int lowbound, int length)
be09dc
 {
be09dc
+  /* Pass unaltered arguments to VALUE_SLICE_1, plus a CALL_COUNT of '1' as we
be09dc
+     are only considering the highest dimension, or we are working on a one
be09dc
+     dimensional array.  So we call VALUE_SLICE_1 exactly once.  */
be09dc
+  return value_slice_1 (array, lowbound, length, 1);
be09dc
+}
be09dc
+
be09dc
+/* CALL_COUNT is used to determine if we are calling the function once, e.g.
be09dc
+   we are working on the current dimension of ARRAY, or if we are calling
be09dc
+   the function repeatedly.  In the later case we need to take elements
be09dc
+   from the TARGET_TYPE of ARRAY.
be09dc
+   With a CALL_COUNT greater than 1 we calculate the offsets for every element
be09dc
+   that should be in the result array.  Then we fetch the contents and then
be09dc
+   copy them into the result array.  The result array will have one dimension
be09dc
+   less than the input array, so later on we need to recreate the indices and
be09dc
+   ranges in the calling function.  */
be09dc
+
be09dc
+struct value *
be09dc
+value_slice_1 (struct value *array, int lowbound, int length, int call_count)
be09dc
+{
be09dc
   struct type *slice_range_type, *slice_type, *range_type;
be09dc
-  LONGEST lowerbound, upperbound;
be09dc
-  struct value *slice;
be09dc
-  struct type *array_type;
be09dc
+  struct type *array_type = check_typedef (value_type (array));
be09dc
+  struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
be09dc
+  unsigned int elt_size, elt_offs;
be09dc
+  LONGEST elt_stride, ary_high_bound, ary_low_bound;
be09dc
+  struct value *v;
be09dc
+  int slice_range_size, i = 0, row_count = 1, elem_count = 1;
be09dc
 
be09dc
-  array_type = check_typedef (value_type (array));
be09dc
+  /* Check for legacy code if we are actually dealing with an array or
be09dc
+     string.  */
be09dc
   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
be09dc
       && TYPE_CODE (array_type) != TYPE_CODE_STRING)
be09dc
     error (_("cannot take slice of non-array"));
be09dc
 
be09dc
-  range_type = TYPE_INDEX_TYPE (array_type);
be09dc
-  if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
be09dc
-    error (_("slice from bad array or bitstring"));
be09dc
+  ary_low_bound = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (array_type));
be09dc
+  ary_high_bound = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (array_type));
be09dc
+
be09dc
+  /* When we are working on a multi-dimensional array, we need to get the
be09dc
+     attributes of the underlying type.  */
be09dc
+  if (call_count > 1)
be09dc
+    {
be09dc
+      elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
be09dc
+      row_count = TYPE_LENGTH (array_type)
be09dc
+		    / TYPE_LENGTH (TYPE_TARGET_TYPE (array_type));
be09dc
+    }
be09dc
+
be09dc
+  elem_count = length;
be09dc
+  elt_size = TYPE_LENGTH (elt_type);
be09dc
+  elt_offs = longest_to_int (lowbound - ary_low_bound);
be09dc
+  elt_stride = TYPE_LENGTH (TYPE_INDEX_TYPE (array_type));
be09dc
+
be09dc
+  elt_offs *= elt_size;
be09dc
+
be09dc
+  /* Check for valid user input.  In case of Fortran this was already done
be09dc
+     in the calling function.  */
be09dc
+  if (call_count == 1
be09dc
+	&& (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
be09dc
+	      && elt_offs >= TYPE_LENGTH (array_type)))
be09dc
+    error (_("no such vector element"));
be09dc
 
be09dc
-  if (lowbound < lowerbound || length < 0
be09dc
-      || lowbound + length - 1 > upperbound)
be09dc
-    error (_("slice out of range"));
be09dc
+  /* CALL_COUNT is 1 when we are dealing either with the highest dimension
be09dc
+     of the array, or a one dimensional array.  Set RANGE_TYPE accordingly.
be09dc
+     In both cases we calculate how many rows/elements will be in the output
be09dc
+     array by setting slice_range_size.  */
be09dc
+  if (call_count == 1)
be09dc
+    {
be09dc
+      range_type = TYPE_INDEX_TYPE (array_type);
be09dc
+      slice_range_size = elem_count;
be09dc
+
be09dc
+      /* Check if the array bounds are valid.  */
be09dc
+      if (get_discrete_bounds (range_type, &ary_low_bound, &ary_high_bound) < 0)
be09dc
+	error (_("slice from bad array or bitstring"));
be09dc
+    }
be09dc
+  /* When CALL_COUNT is greater than 1, we are dealing with an array of arrays.
be09dc
+     So we need to get the type below the current one and set the RANGE_TYPE
be09dc
+     accordingly.  */
be09dc
+  else
be09dc
+    {
be09dc
+      range_type = TYPE_INDEX_TYPE (TYPE_TARGET_TYPE (array_type));
be09dc
+      slice_range_size = (ary_low_bound + row_count - 1) * (elem_count);
be09dc
+      ary_low_bound = TYPE_LOW_BOUND (range_type);
be09dc
+    }
be09dc
 
be09dc
   /* FIXME-type-allocation: need a way to free this type when we are
be09dc
-     done with it.  */
be09dc
-  slice_range_type = create_static_range_type ((struct type *) NULL,
be09dc
-					       TYPE_TARGET_TYPE (range_type),
be09dc
-					       lowbound,
be09dc
-					       lowbound + length - 1);
be09dc
+      done with it.  */
be09dc
 
be09dc
+  slice_range_type = create_static_range_type (NULL, TYPE_TARGET_TYPE (range_type),
be09dc
+					ary_low_bound, slice_range_size);
be09dc
   {
be09dc
-    struct type *element_type = TYPE_TARGET_TYPE (array_type);
be09dc
-    LONGEST offset
be09dc
-      = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
be09dc
+    struct type *element_type;
be09dc
+
be09dc
+    /* When CALL_COUNT equals 1 we can use the legacy code for subarrays.  */
be09dc
+    if (call_count == 1)
be09dc
+      {
be09dc
+	element_type = TYPE_TARGET_TYPE (array_type);
be09dc
 
be09dc
-    slice_type = create_array_type ((struct type *) NULL,
be09dc
-				    element_type,
be09dc
-				    slice_range_type);
be09dc
-    TYPE_CODE (slice_type) = TYPE_CODE (array_type);
be09dc
+	slice_type = create_array_type (NULL, element_type, slice_range_type);
be09dc
+
be09dc
+	TYPE_CODE (slice_type) = TYPE_CODE (array_type);
be09dc
+
be09dc
+	if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
be09dc
+	  v = allocate_value_lazy (slice_type);
be09dc
+	else
be09dc
+	  {
be09dc
+	    v = allocate_value (slice_type);
be09dc
+	    value_contents_copy (v,
be09dc
+				 value_embedded_offset (v),
be09dc
+				 array,
be09dc
+				 value_embedded_offset (array) + elt_offs,
be09dc
+				 elt_size * longest_to_int (length));
be09dc
+	  }
be09dc
 
be09dc
-    if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
be09dc
-      slice = allocate_value_lazy (slice_type);
be09dc
+      }
be09dc
+    /* When CALL_COUNT is larger than 1 we are working on a range of ranges.
be09dc
+       So we copy the relevant elements into the new array we return.  */
be09dc
     else
be09dc
       {
be09dc
-	slice = allocate_value (slice_type);
be09dc
-	value_contents_copy (slice, 0, array, offset,
be09dc
-			     type_length_units (slice_type));
be09dc
+	LONGEST dst_offset = 0;
be09dc
+	LONGEST src_row_length = TYPE_LENGTH (TYPE_TARGET_TYPE (array_type));
be09dc
+
be09dc
+	element_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (array_type));
be09dc
+	slice_type = create_array_type (NULL, element_type, slice_range_type);
be09dc
+
be09dc
+	TYPE_CODE (slice_type) = TYPE_CODE (TYPE_TARGET_TYPE (array_type));
be09dc
+
be09dc
+	v = allocate_value (slice_type);
be09dc
+	for (i = 0; i < longest_to_int (row_count); i++)
be09dc
+	  {
be09dc
+	    /* Fetches the contents of ARRAY and copies them into V.  */
be09dc
+	    value_contents_copy (v,
be09dc
+				 dst_offset,
be09dc
+				 array,
be09dc
+				 elt_offs,
be09dc
+				 elt_size * elem_count);
be09dc
+	    elt_offs += src_row_length;
be09dc
+	    dst_offset += elt_size * elem_count;
be09dc
+	  }
be09dc
       }
be09dc
 
be09dc
-    set_value_component_location (slice, array);
be09dc
-    VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
be09dc
-    set_value_offset (slice, value_offset (array) + offset);
be09dc
+    set_value_component_location (v, array);
be09dc
+    VALUE_REGNUM (v) = VALUE_REGNUM (array);
be09dc
+    VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
be09dc
+    set_value_offset (v, value_offset (array) + elt_offs);
be09dc
   }
be09dc
 
be09dc
-  return slice;
be09dc
+  return v;
be09dc
 }
be09dc
 
be09dc
 /* Create a value for a FORTRAN complex number.  Currently most of the
be09dc
diff --git a/gdb/value.h b/gdb/value.h
be09dc
index 2eac5ef..3400460 100644
be09dc
--- a/gdb/value.h
be09dc
+++ b/gdb/value.h
be09dc
@@ -1056,6 +1056,8 @@ extern struct value *varying_to_slice (struct value *);
be09dc
 
be09dc
 extern struct value *value_slice (struct value *, int, int);
be09dc
 
be09dc
+extern struct value *value_slice_1 (struct value *, int, int, int);
be09dc
+
be09dc
 extern struct value *value_literal_complex (struct value *, struct value *,
be09dc
 					    struct type *);
be09dc
 
be09dc
-- 
be09dc
2.5.0
be09dc