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

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