Introduce subscript_operation
authorTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:27:57 +0000 (07:27 -0700)
committerTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:28:19 +0000 (07:28 -0700)
This adds class subscript_operation, which implements BINOP_SUBSCRIPT.

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

* expop.h (class subscript_operation): New.
* eval.c (eval_op_subscript): No longer static.

gdb/ChangeLog
gdb/eval.c
gdb/expop.h

index 0ed474f176cae6ca459949c4865661a84d69ff5a..b84ebfb1ec71420019c6576a631c1520ecb1d2e1 100644 (file)
@@ -1,3 +1,8 @@
+2021-03-08  Tom Tromey  <tom@tromey.com>
+
+       * expop.h (class subscript_operation): New.
+       * eval.c (eval_op_subscript): No longer static.
+
 2021-03-08  Tom Tromey  <tom@tromey.com>
 
        * expop.h (class binop_operation, class usual_ax_binop_operation):
index 6fde03d3522b66bf5980aac9b97a8bf2abbce03e..056daa051602e581c9fe886989852855f6a558ab 100644 (file)
@@ -1558,7 +1558,7 @@ eval_op_binary (struct type *expect_type, struct expression *exp,
 
 /* A helper function for BINOP_SUBSCRIPT.  */
 
-static struct value *
+struct value *
 eval_op_subscript (struct type *expect_type, struct expression *exp,
                   enum noside noside, enum exp_opcode op,
                   struct value *arg1, struct value *arg2)
@@ -3536,6 +3536,39 @@ var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
   return value_from_longest (size_type, TYPE_LENGTH (type));
 }
 
+value *
+subscript_operation::evaluate_for_sizeof (struct expression *exp,
+                                         enum noside noside)
+{
+  if (noside == EVAL_NORMAL)
+    {
+      value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
+                                                     EVAL_AVOID_SIDE_EFFECTS);
+      struct type *type = check_typedef (value_type (val));
+      if (type->code () == TYPE_CODE_ARRAY)
+       {
+         type = check_typedef (TYPE_TARGET_TYPE (type));
+         if (type->code () == TYPE_CODE_ARRAY)
+           {
+             type = type->index_type ();
+             /* Only re-evaluate the right hand side if the resulting type
+                is a variable length type.  */
+             if (type->bounds ()->flag_bound_evaluated)
+               {
+                 val = evaluate (nullptr, exp, EVAL_NORMAL);
+                 /* FIXME: This should be size_t.  */
+                 struct type *size_type
+                   = builtin_type (exp->gdbarch)->builtin_int;
+                 return value_from_longest
+                   (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
+               }
+           }
+       }
+    }
+
+  return operation::evaluate_for_sizeof (exp, noside);
+}
+
 }
 
 /* Evaluate a subexpression of EXP, at index *POS, and return a value
index 4624c2fdefeac1feea38f6e624e29bdf9682ed9d..5c3b0afee551b611c5021821058662dd341ed33e 100644 (file)
@@ -100,6 +100,11 @@ extern struct value *eval_op_binary (struct type *expect_type,
                                     struct expression *exp,
                                     enum noside noside, enum exp_opcode op,
                                     struct value *arg1, struct value *arg2);
+extern struct value *eval_op_subscript (struct type *expect_type,
+                                       struct expression *exp,
+                                       enum noside noside, enum exp_opcode op,
+                                       struct value *arg1,
+                                       struct value *arg2);
 
 namespace expr
 {
@@ -1112,6 +1117,17 @@ using bitwise_ior_operation
 using bitwise_xor_operation
      = usual_ax_binop_operation<BINOP_BITWISE_XOR, eval_op_binary>;
 
+class subscript_operation
+  : public usual_ax_binop_operation<BINOP_SUBSCRIPT, eval_op_subscript>
+{
+public:
+  using usual_ax_binop_operation<BINOP_SUBSCRIPT,
+                                eval_op_subscript>::usual_ax_binop_operation;
+
+  value *evaluate_for_sizeof (struct expression *exp,
+                             enum noside noside) override;
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */