[122/203] Introduce var_value_operation

Message ID 20210101214723.1784144-123-tom@tromey.com
State Superseded
Headers show
Series
  • Refactor expressions
Related show

Commit Message

Tom Tromey Jan. 1, 2021, 9:46 p.m.
This adds class var_value_operation, which implements OP_VAR_VALUE.

gdb/ChangeLog
2021-01-01  Tom Tromey  <tom@tromey.com>

	* expop.h (class var_value_operation): New.
	* eval.c (var_value_operation::evaluate)
	(var_value_operation::evaluate_for_address)
	(var_value_operation::evaluate_with_coercion)
	(var_value_operation::evaluate_for_sizeof)
	(var_value_operation::evaluate_for_cast): New methods.
	* ax-gdb.c (var_value_operation::do_generate_ax): New method.
---
 gdb/ChangeLog |  10 +++++
 gdb/ax-gdb.c  |  20 +++++++++
 gdb/eval.c    | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/expop.h   |  45 +++++++++++++++++++++
 4 files changed, 185 insertions(+)

-- 
2.26.2

Patch

diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index e0970e87d68..bdf4a6dc39a 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -2673,6 +2673,26 @@  unop_cast_type_operation::do_generate_ax (struct expression *exp,
   std::get<1> (m_storage)->generate_ax (exp, ax, value, value_type (val));
 }
 
+void
+var_value_operation::do_generate_ax (struct expression *exp,
+				     struct agent_expr *ax,
+				     struct axs_value *value,
+				     struct type *cast_type)
+{
+  gen_var_ref (ax, value, std::get<0> (m_storage));
+
+  if (value->optimized_out)
+    error (_("`%s' has been optimized out, cannot use"),
+	   std::get<0> (m_storage)->print_name ());
+
+  if (value->type->code () == TYPE_CODE_ERROR)
+    {
+      if (cast_type == nullptr)
+	error_unknown_type (std::get<0> (m_storage)->print_name ());
+      value->type = cast_type;
+    }
+}
+
 }
 
 /* This handles the middle-to-right-side of code generation for binary
diff --git a/gdb/eval.c b/gdb/eval.c
index 51a737ac5a5..4b12875f00c 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -683,6 +683,23 @@  evaluate_var_value (enum noside noside, const block *blk, symbol *var)
   return ret;
 }
 
+namespace expr
+
+{
+
+value *
+var_value_operation::evaluate (struct type *expect_type,
+			       struct expression *exp,
+			       enum noside noside)
+{
+  symbol *var = std::get<0> (m_storage);
+  if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR)
+    error_unknown_type (var->print_name ());
+  return evaluate_var_value (noside, std::get<1> (m_storage), var);
+}
+
+} /* namespace expr */
+
 /* Helper for evaluating an OP_VAR_MSYM_VALUE.  */
 
 value *
@@ -3392,6 +3409,54 @@  evaluate_subexp_with_coercion (struct expression *exp,
     }
 }
 
+namespace expr
+{
+
+value *
+var_value_operation::evaluate_for_address (struct expression *exp,
+					   enum noside noside)
+{
+  symbol *var = std::get<0> (m_storage);
+
+  /* C++: The "address" of a reference should yield the address
+   * of the object pointed to.  Let value_addr() deal with it.  */
+  if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
+    return operation::evaluate_for_address (exp, noside);
+
+  if (noside == EVAL_AVOID_SIDE_EFFECTS)
+    {
+      struct type *type = lookup_pointer_type (SYMBOL_TYPE (var));
+      enum address_class sym_class = SYMBOL_CLASS (var);
+
+      if (sym_class == LOC_CONST
+	  || sym_class == LOC_CONST_BYTES
+	  || sym_class == LOC_REGISTER)
+	error (_("Attempt to take address of register or constant."));
+
+      return value_zero (type, not_lval);
+    }
+  else
+    return address_of_variable (var, std::get<1> (m_storage));
+}
+
+value *
+var_value_operation::evaluate_with_coercion (struct expression *exp,
+					     enum noside noside)
+{
+  struct symbol *var = std::get<0> (m_storage);
+  struct type *type = check_typedef (SYMBOL_TYPE (var));
+  if (type->code () == TYPE_CODE_ARRAY
+      && !type->is_vector ()
+      && CAST_IS_CONVERSION (exp->language_defn))
+    {
+      struct value *val = address_of_variable (var, std::get<1> (m_storage));
+      return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), val);
+    }
+  return evaluate (nullptr, exp, noside);
+}
+
+}
+
 /* Helper function for evaluating the size of a type.  */
 
 static value *
@@ -3632,6 +3697,27 @@  unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
   return evaluate_subexp_for_sizeof_base (exp, value_type (typeval));
 }
 
+value *
+var_value_operation::evaluate_for_sizeof (struct expression *exp,
+					  enum noside noside)
+{
+  struct type *type = SYMBOL_TYPE (std::get<0> (m_storage));
+  if (is_dynamic_type (type))
+    {
+      value *val = evaluate (nullptr, exp, EVAL_NORMAL);
+      type = value_type (val);
+      if (type->code () == TYPE_CODE_ARRAY
+	  && is_dynamic_type (type->index_type ())
+	  && type->bounds ()->high.kind () == PROP_UNDEFINED)
+	{
+	  /* FIXME: This should be size_t.  */
+	  struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
+	  return allocate_optimized_out_value (size_type);
+	}
+    }
+  return evaluate_subexp_for_sizeof_base (exp, type);
+}
+
 }
 
 /* Evaluate a subexpression of EXP, at index *POS, and return a value
@@ -3719,6 +3805,30 @@  var_msym_value_operation::evaluate_for_cast (struct type *to_type,
   return val;
 }
 
+value *
+var_value_operation::evaluate_for_cast (struct type *to_type,
+					struct expression *exp,
+					enum noside noside)
+{
+  value *val = evaluate_var_value (noside,
+				   std::get<1> (m_storage),
+				   std::get<0> (m_storage));
+
+  if (noside == EVAL_SKIP)
+    return eval_skip_value (exp);
+
+  val = value_cast (to_type, val);
+
+  /* Don't allow e.g. '&(int)var_with_no_debug_info'.  */
+  if (VALUE_LVAL (val) == lval_memory)
+    {
+      if (value_lazy (val))
+	value_fetch_lazy (val);
+      VALUE_LVAL (val) = not_lval;
+    }
+  return val;
+}
+
 }
 
 /* Parse a type expression in the string [P..P+LENGTH).  */
diff --git a/gdb/expop.h b/gdb/expop.h
index 53ff93c4b83..0c489c77450 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -606,6 +606,51 @@  class scope_operation
     override;
 };
 
+/* Compute the value of a variable.  */
+class var_value_operation
+  : public tuple_holding_operation<symbol *, const block *>
+{
+public:
+
+  using tuple_holding_operation::tuple_holding_operation;
+
+  value *evaluate (struct type *expect_type,
+		   struct expression *exp,
+		   enum noside noside) override;
+
+  value *evaluate_with_coercion (struct expression *exp,
+				 enum noside noside) override;
+
+  value *evaluate_for_sizeof (struct expression *exp, enum noside noside)
+    override;
+
+  value *evaluate_for_cast (struct type *expect_type,
+			    struct expression *exp,
+			    enum noside noside) override;
+
+  value *evaluate_for_address (struct expression *exp, enum noside noside)
+    override;
+
+  enum exp_opcode opcode () const override
+  { return OP_VAR_VALUE; }
+
+  bool constant_p () const override
+  {
+    symbol *sym = std::get<0> (m_storage);
+    return (SYMBOL_CLASS (sym) == LOC_CONST
+	    || SYMBOL_CLASS (sym) == LOC_CONST_BYTES
+	    || SYMBOL_CLASS (sym) == LOC_LABEL);
+  }
+
+protected:
+
+  void do_generate_ax (struct expression *exp,
+		       struct agent_expr *ax,
+		       struct axs_value *value,
+		       struct type *cast_type)
+    override;
+};
+
 class long_const_operation
   : public tuple_holding_operation<struct type *, LONGEST>
 {