Blame SOURCES/gdb-rhbz1964167-convert-enum-range_type.patch

132741
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
132741
From: Kevin Buettner <kevinb@redhat.com>
132741
Date: Mon, 24 May 2021 17:10:28 -0700
132741
Subject: gdb-rhbz1964167-convert-enum-range_type.patch
132741
132741
;; [fortran] Backport Andrew Burgess's commit which changes enum
132741
;; range_type into a bit field enum.
132741
132741
gdb: Convert enum range_type to a bit field enum
132741
132741
The expression range_type enum represents the following ideas:
132741
132741
  - Lower bound is set to default,
132741
  - Upper bound is set to default,
132741
  - Upper bound is exclusive.
132741
132741
There are currently 6 entries in the enum to represent the combination
132741
of all those ideas.
132741
132741
In a future commit I'd like to add stride information to the range,
132741
this could in theory appear with any of the existing enum entries, so
132741
this would take us to 12 enum entries.
132741
132741
This feels like its getting a little out of hand, so in this commit I
132741
switch the range_type enum over to being a flags style enum.  There's
132741
one entry to represent no flags being set, then 3 flags to represent
132741
the 3 ideas above.  Adding stride information will require adding only
132741
one more enum flag.
132741
132741
I've then gone through and updated the code to handle this change.
132741
132741
There should be no user visible changes after this commit.
132741
132741
gdb/ChangeLog:
132741
132741
	* expprint.c (print_subexp_standard): Update to reflect changes to
132741
	enum range_type.
132741
	(dump_subexp_body_standard): Likewise.
132741
	* expression.h (enum range_type): Convert to a bit field enum, and
132741
	make the enum unsigned.
132741
	* f-exp.y (subrange): Update to reflect changes to enum
132741
	range_type.
132741
	* f-lang.c (value_f90_subarray): Likewise.
132741
	* parse.c (operator_length_standard): Likewise.
132741
	* rust-exp.y (rust_parser::convert_ast_to_expression): Likewise.
132741
	* rust-lang.c (rust_range): Likewise.
132741
	(rust_compute_range): Likewise.
132741
	(rust_subscript): Likewise.
132741
132741
diff --git a/gdb/expprint.c b/gdb/expprint.c
132741
--- a/gdb/expprint.c
132741
+++ b/gdb/expprint.c
132741
@@ -584,17 +584,13 @@ print_subexp_standard (struct expression *exp, int *pos,
132741
 	  longest_to_int (exp->elts[pc + 1].longconst);
132741
 	*pos += 2;
132741
 
132741
-	if (range_type == NONE_BOUND_DEFAULT_EXCLUSIVE
132741
-	    || range_type == LOW_BOUND_DEFAULT_EXCLUSIVE)
132741
+	if (range_type & RANGE_HIGH_BOUND_EXCLUSIVE)
132741
 	  fputs_filtered ("EXCLUSIVE_", stream);
132741
 	fputs_filtered ("RANGE(", stream);
132741
-	if (range_type == HIGH_BOUND_DEFAULT
132741
-	    || range_type == NONE_BOUND_DEFAULT
132741
-	    || range_type == NONE_BOUND_DEFAULT_EXCLUSIVE)
132741
+	if (!(range_type & RANGE_LOW_BOUND_DEFAULT))
132741
 	  print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
132741
 	fputs_filtered ("..", stream);
132741
-	if (range_type == LOW_BOUND_DEFAULT
132741
-	    || range_type == NONE_BOUND_DEFAULT)
132741
+	if (!(range_type & RANGE_HIGH_BOUND_DEFAULT))
132741
 	  print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
132741
 	fputs_filtered (")", stream);
132741
 	return;
132741
@@ -1114,36 +1110,19 @@ dump_subexp_body_standard (struct expression *exp,
132741
 	  longest_to_int (exp->elts[elt].longconst);
132741
 	elt += 2;
132741
 
132741
-	switch (range_type)
132741
-	  {
132741
-	  case BOTH_BOUND_DEFAULT:
132741
-	    fputs_filtered ("Range '..'", stream);
132741
-	    break;
132741
-	  case LOW_BOUND_DEFAULT:
132741
-	    fputs_filtered ("Range '..EXP'", stream);
132741
-	    break;
132741
-	  case LOW_BOUND_DEFAULT_EXCLUSIVE:
132741
-	    fputs_filtered ("ExclusiveRange '..EXP'", stream);
132741
-	    break;
132741
-	  case HIGH_BOUND_DEFAULT:
132741
-	    fputs_filtered ("Range 'EXP..'", stream);
132741
-	    break;
132741
-	  case NONE_BOUND_DEFAULT:
132741
-	    fputs_filtered ("Range 'EXP..EXP'", stream);
132741
-	    break;
132741
-	  case NONE_BOUND_DEFAULT_EXCLUSIVE:
132741
-	    fputs_filtered ("ExclusiveRange 'EXP..EXP'", stream);
132741
-	    break;
132741
-	  default:
132741
-	    fputs_filtered ("Invalid Range!", stream);
132741
-	    break;
132741
-	  }
132741
+	if (range_type & RANGE_HIGH_BOUND_EXCLUSIVE)
132741
+	  fputs_filtered ("Exclusive", stream);
132741
+	fputs_filtered ("Range '", stream);
132741
+	if (!(range_type & RANGE_LOW_BOUND_DEFAULT))
132741
+	  fputs_filtered ("EXP", stream);
132741
+	fputs_filtered ("..", stream);
132741
+	if (!(range_type & RANGE_HIGH_BOUND_DEFAULT))
132741
+	  fputs_filtered ("EXP", stream);
132741
+	fputs_filtered ("'", stream);
132741
 
132741
-	if (range_type == HIGH_BOUND_DEFAULT
132741
-	    || range_type == NONE_BOUND_DEFAULT)
132741
+	if (!(range_type & RANGE_LOW_BOUND_DEFAULT))
132741
 	  elt = dump_subexp (exp, stream, elt);
132741
-	if (range_type == LOW_BOUND_DEFAULT
132741
-	    || range_type == NONE_BOUND_DEFAULT)
132741
+	if (!(range_type & RANGE_HIGH_BOUND_DEFAULT))
132741
 	  elt = dump_subexp (exp, stream, elt);
132741
       }
132741
       break;
132741
diff --git a/gdb/expression.h b/gdb/expression.h
132741
--- a/gdb/expression.h
132741
+++ b/gdb/expression.h
132741
@@ -185,22 +185,22 @@ extern void dump_prefix_expression (struct expression *, struct ui_file *);
132741
    or inclusive.  So we have six sorts of subrange.  This enumeration
132741
    type is to identify this.  */
132741
 
132741
-enum range_type
132741
+enum range_type : unsigned
132741
 {
132741
-  /* Neither the low nor the high bound was given -- so this refers to
132741
-     the entire available range.  */
132741
-  BOTH_BOUND_DEFAULT,
132741
-  /* The low bound was not given and the high bound is inclusive.  */
132741
-  LOW_BOUND_DEFAULT,
132741
-  /* The high bound was not given and the low bound in inclusive.  */
132741
-  HIGH_BOUND_DEFAULT,
132741
-  /* Both bounds were given and both are inclusive.  */
132741
-  NONE_BOUND_DEFAULT,
132741
-  /* The low bound was not given and the high bound is exclusive.  */
132741
-  NONE_BOUND_DEFAULT_EXCLUSIVE,
132741
-  /* Both bounds were given.  The low bound is inclusive and the high
132741
-     bound is exclusive.  */
132741
-  LOW_BOUND_DEFAULT_EXCLUSIVE,
132741
+  /* This is a standard range.  Both the lower and upper bounds are
132741
+     defined, and the bounds are inclusive.  */
132741
+  RANGE_STANDARD = 0,
132741
+
132741
+  /* The low bound was not given.  */
132741
+  RANGE_LOW_BOUND_DEFAULT = 1 << 0,
132741
+
132741
+  /* The high bound was not given.  */
132741
+  RANGE_HIGH_BOUND_DEFAULT = 1 << 1,
132741
+
132741
+  /* The high bound of this range is exclusive.  */
132741
+  RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2,
132741
 };
132741
 
132741
+DEF_ENUM_FLAGS_TYPE (enum range_type, range_types);
132741
+
132741
 #endif /* !defined (EXPRESSION_H) */
132741
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
132741
--- a/gdb/f-exp.y
132741
+++ b/gdb/f-exp.y
132741
@@ -287,26 +287,30 @@ arglist	:	arglist ',' exp   %prec ABOVE_COMMA
132741
 /* There are four sorts of subrange types in F90.  */
132741
 
132741
 subrange:	exp ':' exp	%prec ABOVE_COMMA
132741
-			{ write_exp_elt_opcode (pstate, OP_RANGE); 
132741
-			  write_exp_elt_longcst (pstate, NONE_BOUND_DEFAULT);
132741
+			{ write_exp_elt_opcode (pstate, OP_RANGE);
132741
+			  write_exp_elt_longcst (pstate, RANGE_STANDARD);
132741
 			  write_exp_elt_opcode (pstate, OP_RANGE); }
132741
 	;
132741
 
132741
 subrange:	exp ':'	%prec ABOVE_COMMA
132741
 			{ write_exp_elt_opcode (pstate, OP_RANGE);
132741
-			  write_exp_elt_longcst (pstate, HIGH_BOUND_DEFAULT);
132741
+			  write_exp_elt_longcst (pstate,
132741
+						 RANGE_HIGH_BOUND_DEFAULT);
132741
 			  write_exp_elt_opcode (pstate, OP_RANGE); }
132741
 	;
132741
 
132741
 subrange:	':' exp	%prec ABOVE_COMMA
132741
 			{ write_exp_elt_opcode (pstate, OP_RANGE);
132741
-			  write_exp_elt_longcst (pstate, LOW_BOUND_DEFAULT);
132741
+			  write_exp_elt_longcst (pstate,
132741
+						 RANGE_LOW_BOUND_DEFAULT);
132741
 			  write_exp_elt_opcode (pstate, OP_RANGE); }
132741
 	;
132741
 
132741
 subrange:	':'	%prec ABOVE_COMMA
132741
 			{ write_exp_elt_opcode (pstate, OP_RANGE);
132741
-			  write_exp_elt_longcst (pstate, BOTH_BOUND_DEFAULT);
132741
+			  write_exp_elt_longcst (pstate,
132741
+						 (RANGE_LOW_BOUND_DEFAULT
132741
+						  | RANGE_HIGH_BOUND_DEFAULT));
132741
 			  write_exp_elt_opcode (pstate, OP_RANGE); }
132741
 	;
132741
 
132741
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
132741
--- a/gdb/f-lang.c
132741
+++ b/gdb/f-lang.c
132741
@@ -131,12 +131,12 @@ value_f90_subarray (struct value *array,
132741
 
132741
   *pos += 3;
132741
 
132741
-  if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
132741
+  if (range_type & RANGE_LOW_BOUND_DEFAULT)
132741
     low_bound = range->bounds ()->low.const_val ();
132741
   else
132741
     low_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
132741
 
132741
-  if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
132741
+  if (range_type & RANGE_HIGH_BOUND_DEFAULT)
132741
     high_bound = range->bounds ()->high.const_val ();
132741
   else
132741
     high_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
132741
diff --git a/gdb/parse.c b/gdb/parse.c
132741
--- a/gdb/parse.c
132741
+++ b/gdb/parse.c
132741
@@ -921,21 +921,13 @@ operator_length_standard (const struct expression *expr, int endpos,
132741
       range_type = (enum range_type)
132741
 	longest_to_int (expr->elts[endpos - 2].longconst);
132741
 
132741
-      switch (range_type)
132741
-	{
132741
-	case LOW_BOUND_DEFAULT:
132741
-	case LOW_BOUND_DEFAULT_EXCLUSIVE:
132741
-	case HIGH_BOUND_DEFAULT:
132741
-	  args = 1;
132741
-	  break;
132741
-	case BOTH_BOUND_DEFAULT:
132741
-	  args = 0;
132741
-	  break;
132741
-	case NONE_BOUND_DEFAULT:
132741
-	case NONE_BOUND_DEFAULT_EXCLUSIVE:
132741
-	  args = 2;
132741
-	  break;
132741
-	}
132741
+      /* Assume the range has 2 arguments (low bound and high bound), then
132741
+	 reduce the argument count if any bounds are set to default.  */
132741
+      args = 2;
132741
+      if (range_type & RANGE_LOW_BOUND_DEFAULT)
132741
+	--args;
132741
+      if (range_type & RANGE_HIGH_BOUND_DEFAULT)
132741
+	--args;
132741
 
132741
       break;
132741
 
132741
diff --git a/gdb/rust-exp.y b/gdb/rust-exp.y
132741
--- a/gdb/rust-exp.y
132741
+++ b/gdb/rust-exp.y
132741
@@ -2492,24 +2492,29 @@ rust_parser::convert_ast_to_expression (const struct rust_op *operation,
132741
 
132741
     case OP_RANGE:
132741
       {
132741
-	enum range_type kind = BOTH_BOUND_DEFAULT;
132741
+	enum range_type kind = (RANGE_HIGH_BOUND_DEFAULT
132741
+				| RANGE_LOW_BOUND_DEFAULT);
132741
 
132741
 	if (operation->left.op != NULL)
132741
 	  {
132741
 	    convert_ast_to_expression (operation->left.op, top);
132741
-	    kind = HIGH_BOUND_DEFAULT;
132741
+	    kind &= ~RANGE_LOW_BOUND_DEFAULT;
132741
 	  }
132741
 	if (operation->right.op != NULL)
132741
 	  {
132741
 	    convert_ast_to_expression (operation->right.op, top);
132741
-	    if (kind == BOTH_BOUND_DEFAULT)
132741
-	      kind = (operation->inclusive
132741
-		      ? LOW_BOUND_DEFAULT : LOW_BOUND_DEFAULT_EXCLUSIVE);
132741
+	    if (kind == (RANGE_HIGH_BOUND_DEFAULT | RANGE_LOW_BOUND_DEFAULT))
132741
+	      {
132741
+		kind = RANGE_LOW_BOUND_DEFAULT;
132741
+		if (!operation->inclusive)
132741
+		  kind |= RANGE_HIGH_BOUND_EXCLUSIVE;
132741
+	      }
132741
 	    else
132741
 	      {
132741
-		gdb_assert (kind == HIGH_BOUND_DEFAULT);
132741
-		kind = (operation->inclusive
132741
-			? NONE_BOUND_DEFAULT : NONE_BOUND_DEFAULT_EXCLUSIVE);
132741
+		gdb_assert (kind == RANGE_HIGH_BOUND_DEFAULT);
132741
+		kind = RANGE_STANDARD;
132741
+		if (!operation->inclusive)
132741
+		  kind |= RANGE_HIGH_BOUND_EXCLUSIVE;
132741
 	      }
132741
 	  }
132741
 	else
132741
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
132741
--- a/gdb/rust-lang.c
132741
+++ b/gdb/rust-lang.c
132741
@@ -1082,13 +1082,11 @@ rust_range (struct expression *exp, int *pos, enum noside noside)
132741
   kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
132741
   *pos += 3;
132741
 
132741
-  if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
132741
-      || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
132741
+  if (!(kind & RANGE_LOW_BOUND_DEFAULT))
132741
     low = evaluate_subexp (nullptr, exp, pos, noside);
132741
-  if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
132741
-      || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
132741
+  if (!(kind & RANGE_HIGH_BOUND_DEFAULT))
132741
     high = evaluate_subexp (nullptr, exp, pos, noside);
132741
-  bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
132741
+  bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
132741
 
132741
   if (noside == EVAL_SKIP)
132741
     return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
132741
@@ -1171,13 +1169,13 @@ rust_range (struct expression *exp, int *pos, enum noside noside)
132741
 static void
132741
 rust_compute_range (struct type *type, struct value *range,
132741
 		    LONGEST *low, LONGEST *high,
132741
-		    enum range_type *kind)
132741
+		    range_types *kind)
132741
 {
132741
   int i;
132741
 
132741
   *low = 0;
132741
   *high = 0;
132741
-  *kind = BOTH_BOUND_DEFAULT;
132741
+  *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
132741
 
132741
   if (type->num_fields () == 0)
132741
     return;
132741
@@ -1185,15 +1183,15 @@ rust_compute_range (struct type *type, struct value *range,
132741
   i = 0;
132741
   if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
132741
     {
132741
-      *kind = HIGH_BOUND_DEFAULT;
132741
+      *kind = RANGE_HIGH_BOUND_DEFAULT;
132741
       *low = value_as_long (value_field (range, 0));
132741
       ++i;
132741
     }
132741
   if (type->num_fields () > i
132741
       && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
132741
     {
132741
-      *kind = (*kind == BOTH_BOUND_DEFAULT
132741
-	       ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
132741
+      *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
132741
+	       ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
132741
       *high = value_as_long (value_field (range, i));
132741
 
132741
       if (rust_inclusive_range_type_p (type))
132741
@@ -1211,7 +1209,7 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside,
132741
   struct type *rhstype;
132741
   LONGEST low, high_bound;
132741
   /* Initialized to appease the compiler.  */
132741
-  enum range_type kind = BOTH_BOUND_DEFAULT;
132741
+  range_types kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
132741
   LONGEST high = 0;
132741
   int want_slice = 0;
132741
 
132741
@@ -1308,8 +1306,7 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside,
132741
       else
132741
 	error (_("Cannot subscript non-array type"));
132741
 
132741
-      if (want_slice
132741
-	  && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
132741
+      if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
132741
 	low = low_bound;
132741
       if (low < 0)
132741
 	error (_("Index less than zero"));
132741
@@ -1327,7 +1324,7 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside,
132741
 	  CORE_ADDR addr;
132741
 	  struct value *addrval, *tem;
132741
 
132741
-	  if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
132741
+	  if (kind & RANGE_HIGH_BOUND_DEFAULT)
132741
 	    high = high_bound;
132741
 	  if (high < 0)
132741
 	    error (_("High index less than zero"));