Commit Graph

471 Commits

Author SHA1 Message Date
Andrew Burgess
158cc4feb7 gdb: use gdb::optional instead of passing a pointer to gdb::array_view
Following on from the previous commit, this commit changes the API of
value_struct_elt to take gdb::optional<gdb::array_view<value *>>
instead of a pointer to the gdb::array_view.

This makes the optional nature of the array_view parameter explicit.

This commit is purely a refactoring commit, there should be no user
visible change after this commit.

I have deliberately kept this refactor separate from the previous two
commits as this is a more extensive change, and I'm not 100% sure that
using gdb::optional for the parameter type, instead of a pointer, is
going to be to everyone's taste.  If there's push back on this patch
then this one can be dropped from the series.

gdb/ChangeLog:

	* ada-lang.c (desc_bounds): Use '{}' instead of NULL to indicate
	an empty gdb::optional when calling value_struct_elt.
	(desc_data): Likewise.
	(desc_one_bound): Likewise.
	* eval.c (structop_base_operation::evaluate_funcall): Pass
	gdb::array_view, not a gdb::array_view* to value_struct_elt.
	(eval_op_structop_struct): Use '{}' instead of NULL to indicate
	an empty gdb::optional when calling value_struct_elt.
	(eval_op_structop_ptr): Likewise.
	* f-lang.c (fortran_structop_operation::evaluate): Likewise.
	* guile/scm-value.c (gdbscm_value_field): Likewise.
	* m2-lang.c (eval_op_m2_high): Likewise.
	(eval_op_m2_subscript): Likewise.
	* opencl-lang.c (opencl_structop_operation::evaluate): Likewise.
	* python/py-value.c (valpy_getitem): Likewise.
	* rust-lang.c (rust_val_print_str): Likewise.
	(rust_range): Likewise.
	(rust_subscript): Likewise.
	(eval_op_rust_structop): Likewise.
	(rust_aggregate_operation::evaluate): Likewise.
	* valarith.c (value_user_defined_op): Likewise.
	* valops.c (search_struct_method): Change parameter type, update
	function body accordingly, and update header comment.
	(value_struct_elt): Change parameter type, update function body
	accordingly.
	* value.h (value_struct_elt): Update declaration.
2021-06-25 20:43:06 +01:00
Andrew Burgess
13221aec0d gdb: replace NULL terminated array with array_view
After the previous commit, this commit updates the value_struct_elt
function to take an array_view rather than a NULL terminated array of
values.

The requirement for a NULL terminated array of values actually stems
from typecmp, so the change from an array to array_view needs to be
propagated through to this function.

While making this change I noticed that this fixes another bug, in
value_x_binop and value_x_unop GDB creates an array of values which
doesn't have a NULL at the end.  An array_view of this array is passed
to value_user_defined_op, which then unpacks the array_view and passed
the raw array to value_struct_elt, but only if the language is not
C++.

As value_x_binop and value_x_unop can only request member functions
with the names of C++ operators, then most of the time, assuming the
inferior is not a C++ program, then GDB will not find a matching
member function with the call to value_struct_elt, and so typecmp will
never be called, and so, GDB will avoid undefined behaviour.

However, it is worth remembering that, when GDB's language is set to
"auto", the current language is selected based on the language of the
current compilation unit.  As C++ programs usually link against libc,
which is written in C, then, if the inferior is stopped in libc GDB
will set the language to C.  And so, it is possible that we will end
up using value_struct_elt to try and lookup, and match, a C++
operator.  If this occurs then GDB will experience undefined
behaviour.

I have extended the test added in the previous commit to also cover
this case.

Finally, this commit changes the API from passing around a pointer to
an array to passing around a pointer to an array_view.  The reason for
this is that we need to be able to distinguish between the cases where
we call value_struct_elt with no arguments, i.e. we are looking up a
struct member, but we either don't have the arguments we want to pass
yet, or we don't expect there to be any need for GDB to use the
argument types to resolve any overloading; and the second case where
we call value_struct_elt looking for a function that takes no
arguments, that is, the argument list is empty.

NOTE: While writing this I realise that if we pass an array_view at
all then it will always have at least one item in it, the `this'
pointer for the object we are planning to call the method on.  So we
could, I guess, pass an empty array_view to indicate the case where we
don't know anything about the arguments, and when the array_view is
length 1 or more, it means we do have the arguments.  However, though
we could do this, I don't think this would be better, the length 0 vs
length 1 difference seems a little too subtle, I think that there's a
better solution...

I think a better solution would be to wrap the array_view in a
gdb::optional, this would make the whole, do we have an array view or
not question explicit.

I haven't done this as part of this commit as making that change is
much more extensive, every user of value_struct_elt will need to be
updated, and as this commit already contains a bug fix, I wanted to
keep the large refactoring in a separate commit, so, check out the
next commit for the use of gdb::optional.

gdb/ChangeLog:

	PR gdb/27994
	* eval.c (structop_base_operation::evaluate_funcall): Pass
	array_view instead of array to value_struct_elt.
	* valarith.c (value_user_defined_op): Likewise.
	* valops.c (typecmp): Change parameter type from array pointer to
	array_view.  Update header comment, and update body accordingly.
	(search_struct_method): Likewise.
	(value_struct_elt): Likewise.
	* value.h (value_struct_elt): Update declaration.

gdb/testsuite/ChangeLog:

	PR gdb/27994
	* gdb.cp/method-call-in-c.cc (struct foo_type): Add operator+=,
	change initial value of var member variable.
	(main): Make use of foo_type's operator+=.
	* gdb.cp/method-call-in-c.exp: Test use of operator+=.
2021-06-25 20:43:06 +01:00
Andrew Burgess
79bd4d34f0 gdb: fix regression in evaluate_funcall for non C++ like cases
This regression, as it is exposed by the test added in this commit,
first became noticable with this commit:

  commit d182f27979
  Date:   Mon Mar 8 07:27:57 2021 -0700

      Convert c-exp.y to use operations

But, this commit only added converted the C expression parser to make
use of code that was added in this commit:

  commit a00b7254fb
  Date:   Mon Mar 8 07:27:57 2021 -0700

      Implement function call operations

And it was this second commit that actually introduced the bugs (there
are two).

In structop_base_operation::evaluate_funcall we build up an argument
list in the vector vals.  Later in this function the argument list
might be passed to value_struct_elt.

Prior to commit a00b7254fb the vals vector (or argvec as it used to
be called) stored the value for the function callee in the argvec at
index 0.  This 'callee' value is what ends up being passed to
evaluate_subexp_do_call, and represents the function to be called, the
value contents are the address of the function, and the value type is
the function signature.  The remaining items held in the argvec were
the values to pass to the function.  For a non-static member function
the `this' pointer would be at index 1 in the array.

After commit a00b7254fb this callee value is now held in a separate
variable, not the vals array.  So, for non-static member functions,
the `this' pointer is now at index 0, with any other arguments after
that.

What this means is that previous, when we called value_struct_elt we
would pass the address of argvec[1] as this was the first argument.
But now we should be passing the address of vals[0].  Unfortunately,
we are still passing vals[1], effectively skipping the first
argument.

The second issue is that, prior to commit a00b7254fb, the argvec
array was NULL terminated.  This is required as value_struct_elt
calls search_struct_method, which calls typecmp, and typecmp requires
that the array have a NULL at the end.

After commit a00b7254fb this NULL has been lost, and we are
therefore violating the API requirements of typecmp.

This commit fixes both of these regressions.  I also extended the
header comments on search_struct_method and value_struct_elt to make
it clearer that the array required a NULL marker at the end.

You will notice in the test attached to this commit that I test
calling a non-static member function, but not calling a static member
function.  The reason for this is that calling static member functions
is currently broken due to a different bug.  That will be fixed in a
later patch in this series, at which time I'll add a test for calling
a static member function.

gdb/ChangeLog:

	PR gdb/27994
	* eval.c (structop_base_operation::evaluate_funcall): Add a
	nullptr to the end of the args array, which should not be included
	in the argument array_view.  Pass all the arguments through to
	value_struct_elt.
	* valops.c (search_struct_method): Update header comment.
	(value_struct_elt): Likewise.

gdb/testsuite/ChangeLog:

	PR gdb/27994
	* gdb.cp/method-call-in-c.cc: New file.
	* gdb.cp/method-call-in-c.exp: New file.
2021-06-25 20:43:05 +01:00
Magne Hov
df5bc734f2 gdb: fix eval.c assert during inferior exit event
Evaluating expressions from within an inferior exit event handler can
cause a crash:

    echo "int main() { return 0; }" > repro.c
    gcc -g repro.c -o repro
    ./gdb -q --ex "set language c++" --ex "python gdb.events.exited.connect(lambda _: gdb.execute('set \$_a=0'))" --ex "run" repro

    Reading symbols from repro...
    Starting program: /home/mhov/repos/binutils-gdb-master/install-bad/bin/repro
    [Inferior 1 (process 1974779) exited normally]
    ../../gdb/thread.c:72: internal-error: thread_info* inferior_thread(): Assertion `current_thread_ != nullptr' failed.
    A problem internal to GDB has been detected,
    further debugging may prove unreliable.
    Quit this debugging session? (y or n) [answered Y; input not from terminal]

    This is a bug, please report it.  For instructions, see:
    <https://www.gnu.org/software/gdb/bugs/>.

Backtrace
    0  in internal_error of ../../gdbsupport/errors.cc:51
    1  in inferior_thread of ../../gdb/thread.c:72
    2  in expression::evaluate of ../../gdb/eval.c:98
    3  in evaluate_expression of ../../gdb/eval.c:115
    4  in set_command of ../../gdb/printcmd.c:1502
    5  in do_const_cfunc of ../../gdb/cli/cli-decode.c:101
    6  in cmd_func of ../../gdb/cli/cli-decode.c:2181
    7  in execute_command of ../../gdb/top.c:670
    ...
    22 in python_inferior_exit of ../../gdb/python/py-inferior.c:182

In `expression::evaluate (...)' there is a call to `inferior_thread
()' that is guarded by `target_has_execution ()':

    struct value *
    expression::evaluate (struct type *expect_type, enum noside noside)
    {
      gdb::optional<enable_thread_stack_temporaries> stack_temporaries;
      if (target_has_execution ()
          && language_defn->la_language == language_cplus
          && !thread_stack_temporaries_enabled_p (inferior_thread ()))
        stack_temporaries.emplace (inferior_thread ());

The `target_has_execution ()' guard maps onto `inf->pid' and the
`inferior_thread ()' call assumes that `current_thread_' is set to
something meaningful:

    struct thread_info*
    inferior_thread (void)
    {
      gdb_assert (current_thread_ != nullptr);
      return current_thread_;
    }

In other words, it is assumed that if `inf->pid' is set then
`current_thread_' must also be set. This does not hold at the point
where inferior exit observers are notified:
- `generic_mourn_inferior (...)'
  - `switch_to_no_thread ()'
    - `current_thread_ = nullptr;'
  - `exit_inferior (...)'
    - `gdb::observers::inferior_exit.notify (...)'
    - `inf->pid = 0'

The inferior exit notification means that a Python handler can get a
chance to run while `current_thread' has been cleared and the
`inf->pid' has not been cleared. Since the Python handler can call any
GDB command with `gdb.execute(...)' (in my case `gdb.execute("set
$_a=0")' we can end up evaluating expressions and asserting in
`evaluate_subexp (...)'.

This patch adds a test in `evaluate_subexp (...)' to check the global
`inferior_ptid' which is reset at the same time as `current_thread_'.
Checking `inferior_ptid' at the same time as `target_has_execution ()'
seems to be a common pattern:

    $ git grep -n -e inferior_ptid --and -e target_has_execution
    gdb/breakpoint.c:2998:    && (inferior_ptid == null_ptid || !target_has_execution ()))
    gdb/breakpoint.c:3054:    && (inferior_ptid == null_ptid || !target_has_execution ()))
    gdb/breakpoint.c:4587:  if (inferior_ptid == null_ptid || !target_has_execution ())
    gdb/infcmd.c:360:  if (inferior_ptid != null_ptid && target_has_execution ())
    gdb/infcmd.c:2380:  /* FIXME:  This should not really be inferior_ptid (or target_has_execution).
    gdb/infrun.c:3438:  if (!target_has_execution () || inferior_ptid == null_ptid)
    gdb/remote.c:11961:  if (!target_has_execution () || inferior_ptid == null_ptid)
    gdb/solib.c:725:  if (target_has_execution () && inferior_ptid != null_ptid)

The testsuite has been run on 5.4.0-59-generic x86_64 GNU/Linux:
- Ubuntu 20.04.1 LTS
- gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
- DejaGnu version 1.6.2
  - Expect version 5.45.4
  - Tcl version 8.6
- Native configuration: x86_64-pc-linux-gnu
- Target: unix

Results show a few XFAIL in
gdb.threads/attach-many-short-lived-threads.exp. The existing
py-events.exp tests are skipped for native-gdbserver and fail for
native-extended-gdbserver, but the new tests pass with
native-extended-gdbserver when run without the existing tests.

gdb/ChangeLog:

2021-06-03  Magne Hov  <mhov@undo.io>

	PR python/27841
	* eval.c (expression::evaluate): Check inferior_ptid.

gdb/testsuite/ChangeLog:

2021-06-03  Magne Hov  <mhov@undo.io>

	PR python/27841
	* gdb.python/py-events.exp: Extend inferior exit tests.
	* gdb.python/py-events.py: Print inferior exit PID.
2021-06-03 21:22:08 +01:00
Tom Tromey
9e5e03df52 Use block_symbol in var_value_operation
I noticed that var_value_operation takes a block and a symbol, and
most callers destructure a block_symbol to pass in.  It seems better
for this class to simply hold a block_symbol instead.

Tested on x86-64 Fedora 32.

gdb/ChangeLog
2021-04-15  Tom Tromey  <tromey@adacore.com>

	* rust-exp.y (rust_parser::convert_ast_to_expression): Update.
	* parse.c (parser_state::push_symbol, parser_state::push_dollar):
	Update.
	* p-exp.y (variable): Update.
	* m2-exp.y (variable): Update.
	* go-exp.y (variable): Update.
	* expprint.c (dump_for_expression): New overload.
	* expop.h (check_objfile): New overload.
	(check_constant): New overload.
	(class var_value_operation): Use block_symbol.
	<get_symbol>: Rewrite.
	* eval.c (var_value_operation::evaluate)
	(var_value_operation::evaluate_funcall)
	(var_value_operation::evaluate_for_address)
	(var_value_operation::evaluate_for_address)
	(var_value_operation::evaluate_with_coercion)
	(var_value_operation::evaluate_for_sizeof)
	(var_value_operation::evaluate_for_cast): Update.
	* d-exp.y (PrimaryExpression): Update.
	* c-exp.y (variable): Update.
	* ax-gdb.c (var_value_operation::do_generate_ax): Update.
	* ada-lang.c (ada_var_value_operation::evaluate_for_cast)
	(ada_var_value_operation::evaluate)
	(ada_var_value_operation::resolve)
	(ada_funcall_operation::resolve): Update.
	* ada-exp.y (write_var_from_sym, write_object_renaming)
	(write_ambiguous_var, write_var_or_type, write_name_assoc)
	(maybe_overload): Update.
	* ada-exp.h (class ada_var_value_operation) <get_block>: Rewrite.
2021-04-15 10:05:00 -06:00
Simon Marchi
328d42d87e gdb: remove current_top_target function
The current_top_target function is a hidden dependency on the current
inferior.  Since I'd like to slowly move towards reducing our dependency
on the global current state, remove this function and make callers use

  current_inferior ()->top_target ()

There is no expected change in behavior, but this one step towards
making those callers use the inferior from their context, rather than
refer to the global current inferior.

gdb/ChangeLog:

	* target.h (current_top_target): Remove, make callers use the
	current inferior instead.
	* target.c (current_top_target): Remove.

Change-Id: Iccd457036f84466cdaa3865aa3f9339a24ea001d
2021-03-24 18:08:24 -04:00
Tom Tromey
ccdc02ed07 Fix function call regression in new evaluator
The internal AdaCore test suite revealed a bug in the new evaluator.
A hunk of evaluate_funcall was not correctly transcribed.  This was
not caught in my original testing because the feature in question was
apparently not tested in gdb.

This patch fixes the oversight.  The idea here is that ordinary
function calls should use the function's formal parameter types as the
expected types of subexpressions.

Regression tested on x86-64 Fedora 32.

gdb/ChangeLog
2021-03-09  Tom Tromey  <tromey@adacore.com>

	* eval.c (operation::evaluate_funcall): Use function formal
	parameter types when evaluating.

gdb/testsuite/ChangeLog
2021-03-09  Tom Tromey  <tromey@adacore.com>

	* gdb.base/cast-call.exp: New file.
	* gdb.base/cast-call.c: New file.
2021-03-09 07:36:26 -07:00
Tom Tromey
9c79936b3d Use bound_minimal_symbol in var_msym_value_operation
This changes var_msym_value_operation to use a bound_minimal_symbol
rather than separate minsym and objfile parameters.  The main benefit
of this is removing the possibly-confusing check_objfile overload for
a plain minimal symbol.

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

	* parse.c (parser_state::push_symbol, parser_state::push_dollar):
	Update.
	* p-exp.y (variable): Update.
	* go-exp.y (variable): Update.
	* expprint.c (dump_for_expression): Use bound_minimal_symbol.
	Remove overload for objfile.
	* expop.h (eval_op_var_msym_value): Use bound_minimal_symbol
	parameter.
	(check_objfile): Likewise.
	(dump_for_expression): Likewise.  Remove overload for objfile.
	(class var_msym_value_operation): Use bound_minimal_symbol.
	* eval.c (eval_op_var_msym_value): Use bound_minimal_symbol
	parameter.
	(var_msym_value_operation::evaluate_for_address)
	(var_msym_value_operation::evaluate_for_sizeof)
	(var_msym_value_operation::evaluate_for_cast): Update.
	* d-exp.y (PrimaryExpression): Update.
	* c-exp.y (variable): Update.
	* ax-gdb.c (var_msym_value_operation::do_generate_ax): Update.
	* ada-lang.c (ada_var_msym_value_operation::evaluate_for_cast):
	Update.
	* ada-exp.y (write_var_or_type): Update.
2021-03-08 07:28:44 -07:00
Tom Tromey
0b2b0b8220 Remove EVAL_SKIP
EVAL_SKIP was needed in the old expression implementation due to its
linearized tree structure.  This is not needed in the new
implementation, because it is trivial to not evaluate a subexpression.
This patch removes the last vestiges of EVAL_SKIP.

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

	* value.h (eval_skip_value): Don't declare.
	* opencl-lang.c (eval_opencl_assign): Update.
	* m2-lang.c (eval_op_m2_high, eval_op_m2_subscript): Update.
	* f-lang.c (eval_op_f_abs, eval_op_f_mod, eval_op_f_ceil)
	(eval_op_f_floor, eval_op_f_modulo, eval_op_f_cmplx): Remove.
	* expression.h (enum noside) <EVAL_SKIP>: Remove.
	* expop.h (typeof_operation::evaluate)
	(decltype_operation::evaluate, unop_addr_operation::evaluate)
	(unop_sizeof_operation::evaluate, assign_operation::evaluate)
	(cxx_cast_operation::evaluate): Update.
	* eval.c (eval_skip_value): Remove.
	(eval_op_scope, eval_op_var_entry_value)
	(eval_op_func_static_var, eval_op_string, eval_op_objc_selector)
	(eval_op_concat, eval_op_ternop, eval_op_structop_struct)
	(eval_op_structop_ptr, eval_op_member, eval_op_add, eval_op_sub)
	(eval_op_binary, eval_op_subscript, eval_op_equal)
	(eval_op_notequal, eval_op_less, eval_op_gtr, eval_op_geq)
	(eval_op_leq, eval_op_repeat, eval_op_plus, eval_op_neg)
	(eval_op_complement, eval_op_lognot, eval_op_ind)
	(eval_op_memval, eval_op_preinc, eval_op_predec)
	(eval_op_postinc, eval_op_postdec, eval_op_type)
	(eval_binop_assign_modify, eval_op_objc_msgcall)
	(eval_multi_subscript, logical_and_operation::evaluate)
	(logical_or_operation::evaluate, array_operation::evaluate)
	(operation::evaluate_for_cast)
	(var_msym_value_operation::evaluate_for_cast)
	(var_value_operation::evaluate_for_cast): Update.
	* c-lang.c (c_string_operation::evaluate): Update.
	* c-exp.h (objc_nsstring_operation::evaluate)
	(objc_selector_operation::evaluate): Update.
	* ada-lang.c (ada_assign_operation::evaluate)
	(eval_ternop_in_range, ada_unop_neg, ada_unop_in_range)
	(ada_atr_size): Update.
2021-03-08 07:28:43 -07:00
Tom Tromey
96db551d06 Merge namespace scopes in eval.c
The big deletion patch left some identical namespace scopes sitting
next to one another.  This patch removes the redundant "namespace
expr"s.

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

	* eval.c: Merge "namespace" scopes.
2021-03-08 07:28:43 -07:00
Tom Tromey
1eaebe02cf Remove union exp_element
This removes union exp_element functions that either create such
elements or walk them.  struct expression no longer holds
exp_elements.  A couple of language_defn methods are also removed, as
they are obsolete.

Note that this patch also removes the print_expression code.  The only
in-tree caller of this was from dump_prefix_expression, which is only
called when expression debugging is enabled.  Implementing this would
involve a fair amount of code, and it seems to me that prefix dumping
is preferable anyway, as it is unambiguous.  So, I have not
reimplemented this feature.

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

	* value.h (evaluate_subexp_with_coercion): Don't declare.
	* parse.c (exp_descriptor_standard): Remove.
	(expr_builder::expr_builder, expr_builder::release): Update.
	(expression::expression): Remove size_t parameter.
	(expression::~expression): Simplify.
	(expression::resize): Remove.
	(write_exp_elt, write_exp_elt_opcode, write_exp_elt_sym)
	(write_exp_elt_msym, write_exp_elt_block, write_exp_elt_objfile)
	(write_exp_elt_longcst, write_exp_elt_floatcst)
	(write_exp_elt_type, write_exp_elt_intern, write_exp_string)
	(write_exp_string_vector, write_exp_bitstring): Remove.
	* p-lang.h (class pascal_language) <opcode_print_table,
	op_print_tab>: Remove.
	* p-lang.c (pascal_language::op_print_tab): Remove.
	* opencl-lang.c (class opencl_language) <opcode_print_table>:
	Remove.
	* objc-lang.c (objc_op_print_tab): Remove.
	(class objc_language) <opcode_print_table>: Remove.
	* m2-lang.h (class m2_language) <opcode_print_table,
	op_print_tab>: Remove.
	* m2-lang.c (m2_language::op_print_tab): Remove.
	* language.h (struct language_defn) <post_parser, expression_ops,
	opcode_print_table>: Remove.
	* language.c (language_defn::expression_ops)
	(auto_or_unknown_language::opcode_print_table): Remove.
	* go-lang.h (class go_language) <opcode_print_table,
	op_print_tab>: Remove.
	* go-lang.c (go_language::op_print_tab): Remove.
	* f-lang.h (class f_language) <opcode_print_table>: Remove
	<op_print_tab>: Remove.
	* f-lang.c (f_language::op_print_tab): Remove.
	* expression.h (union exp_element): Remove.
	(struct expression): Remove size_t parameter from constructor.
	<resize>: Remove.
	<first_opcode>: Update.
	<nelts, elts>: Remove.
	(EXP_ELEM_TO_BYTES, BYTES_TO_EXP_ELEM): Remove.
	(evaluate_subexp_standard, print_expression, op_string)
	(dump_raw_expression): Don't declare.
	* expprint.c (print_expression, print_subexp)
	(print_subexp_funcall, print_subexp_standard, op_string)
	(dump_raw_expression, dump_subexp, dump_subexp_body)
	(dump_subexp_body_funcall, dump_subexp_body_standard): Remove.
	(dump_prefix_expression): Update.
	* eval.c (evaluate_subexp): Remove.
	(evaluate_expression, evaluate_type): Update.
	(evaluate_subexpression_type): Remove.
	(fetch_subexp_value): Remove "pc" parameter.  Update.
	(extract_field_op, evaluate_struct_tuple, evaluate_funcall)
	(evaluate_subexp_standard, evaluate_subexp_for_address)
	(evaluate_subexp_with_coercion, evaluate_subexp_for_sizeof)
	(evaluate_subexp_for_cast): Remove.
	(parse_and_eval_type): Update.
	* dtrace-probe.c (dtrace_probe::compile_to_ax): Update.
	* d-lang.c (d_op_print_tab): Remove.
	(class d_language) <opcode_print_table>: Remove.
	* c-lang.h (c_op_print_tab): Don't declare.
	* c-lang.c (c_op_print_tab): Remove.
	(class c_language, class cplus_language, class asm_language, class
	minimal_language) <opcode_print_table>: Remove.
	* breakpoint.c (update_watchpoint, watchpoint_check)
	(watchpoint_exp_is_const, watch_command_1): Update.
	* ax-gdb.h (union exp_element): Don't declare.
	* ax-gdb.c (const_var_ref, const_expr, maybe_const_expr)
	(gen_repeat, gen_sizeof, gen_expr_for_cast, gen_expr)
	(gen_expr_binop_rest): Remove.
	(gen_trace_for_expr, gen_eval_for_expr, gen_printf): Update.
	* ada-lang.c (ada_op_print_tab): Remove.
	(class ada_language) <post_parser, opcode_print_table>: Remove.
2021-03-08 07:28:41 -07:00
Tom Tromey
413403fc34 Add an expr::operation_up to struct expression
This adds an expr::operation_up to struct expression, and then
modifies various parts of GDB to use this member when it is non-null.
The list of such spots was a bit surprising to me, and found only
after writing most of the code and then noticing what no longer
compiled.

In a few spots, new accessor methods are added to operation
subclasses, so that code that dissects an expression will work with
the new scheme.

After this change, code that constructs an expression can be switched
to the new form without breaking.

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

	* ada-exp.h (class ada_var_value_operation) <get_symbol>: Remove;
	now in superclass.
	* value.h (fetch_subexp_value): Add "op" parameter.
	* value.c (init_if_undefined_command): Update.
	* tracepoint.c (validate_actionline, encode_actions_1): Update.
	* stap-probe.c (stap_probe::compile_to_ax): Update.
	* printcmd.c (set_command): Update.
	* ppc-linux-nat.c (ppc_linux_nat_target::check_condition):
	Update.
	* parser-defs.h (struct expr_builder) <set_operation>: New
	method.
	* parse.c (parse_exp_in_context, exp_uses_objfile): Update.
	* expression.h (struct expression) <first_opcode>: Update.
	<op>: New member.
	* expprint.c (dump_raw_expression, dump_prefix_expression):
	Update.
	* expop.h (class var_value_operation) <get_symbol>: New method.
	(class register_operation) <get_name>: New method.
	(class equal_operation): No longer a typedef, now a subclass.
	(class unop_memval_operation) <get_type>: New method.
	(class assign_operation) <get_lhs>: New method.
	(class unop_cast_operation) <get_type>: New method.
	* eval.c (evaluate_expression, evaluate_type)
	(evaluate_subexpression_type): Update.
	(fetch_subexp_value): Add "op" parameter.
	(parse_and_eval_type): Update.
	* dtrace-probe.c (dtrace_probe::compile_to_ax): Update.
	* breakpoint.c (update_watchpoint, watchpoint_check)
	(watchpoint_exp_is_const, watch_command_1): Update.
	* ax-gdb.c (gen_trace_for_expr, gen_eval_for_expr, gen_printf):
	Update.
2021-03-08 07:28:37 -07:00
Tom Tromey
a00b7254fb Implement function call operations
This implement function call operations.

The current function call code relies on some very lengthy code
(evaluate_funcall is 398 lines...) to distinguish between the
different opcodes that might appear in the callee position.

Rather than try to replicate this, and have a function that tried to
dissect many different kinds of operation subclass, this patch instead
puts the work into the callee.  A new operation::evaluate_funcall
method is added, and then this is overridden in the classes that
require special treatment.

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

	* expression.h (class operation) <evaluate_funcall>: New methods.
	* expop.h (class scope_operation) <evaluate_funcall>: New method.
	(class var_value_operation) <evaluate_funcall>: New method.
	(class structop_base_operation) <evaluate_funcall>: New method.
	(class var_msym_value_operation) <evaluate_funcall>: New method.
	(class structop_member_base): New class.
	(class structop_member_operation): Derive from
	structop_member_base.
	(class structop_mptr_operation): Derive from
	structop_member_base.
	(class funcall_operation): New class.
	* eval.c (operation::evaluate_funcall)
	(var_value_operation::evaluate_funcall)
	(scope_operation::evaluate_funcall)
	(structop_member_base::evaluate_funcall)
	(structop_base_operation::evaluate_funcall): New methods.
2021-03-08 07:28:29 -07:00
Tom Tromey
1c02eb3035 Introduce array_operation
This adds class array_operation, which implements OP_ARRAY.

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

	* expop.h (class array_operation): New.
	* eval.c (array_operation::evaluate_struct_tuple)
	(array_operation::evaluate): New methods.
2021-03-08 07:28:29 -07:00
Tom Tromey
e447908052 Introduce class adl_func_operation
This adds class adl_func_operation, which implements
argument-dependent lookup function calls.

Other function calls will be handled in a different way.  However,
because ADL calls were created in a single spot in the C++ parser, and
because they had different semantics from the other cases, it was
convenient to treat them specially.

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

	* expop.h (class adl_func_operation): New.
	* eval.c (adl_func_operation::evaluate): New method.
2021-03-08 07:28:29 -07:00
Tom Tromey
5019124b1d Implement the "&&" and "||" operators
This implements the "&&" and "||" operators.

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

	* expop.h (class logical_and_operation)
	(class logical_or_operation): New.
	* eval.c (logical_and_operation::evaluate)
	(logical_or_operation::evaluate): New methods.
	* ax-gdb.c (logical_and_operation::do_generate_ax)
	(logical_or_operation::do_generate_ax): New methods.
2021-03-08 07:28:28 -07:00
Tom Tromey
821e72d775 Introduce multi_subscript_operation
This adds class multi_subscript_operation, which implements
MULTI_SUBSCRIPT.

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

	* expop.h (class multi_subscript_operation): New.
	* eval.c (multi_subscript_operation::evaluate): New method.
2021-03-08 07:28:25 -07:00
Tom Tromey
085734dd95 Introduce objc_msgcall_operation
This adds class objc_msgcall_operation, which implements
OP_OBJC_MSGCALL.

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

	* eval.c (objc_msgcall_operation::evaluate): New method.
	* c-exp.h (class objc_msgcall_operation): New.
2021-03-08 07:28:25 -07:00
Tom Tromey
e82a5afced Introduce var_value_operation
This adds class var_value_operation, which implements OP_VAR_VALUE.

gdb/ChangeLog
2021-03-08  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.
2021-03-08 07:28:24 -07:00
Tom Tromey
e5946e1604 Introduce assign_modify_operation
This adds class assign_modify_operation, which implements
BINOP_ASSIGN_MODIFY.

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

	* expop.h (class assign_modify_operation): New.
	* eval.c (eval_binop_assign_modify): No longer static.
	* ax-gdb.c (assign_modify_operation::do_generate_ax): New method.
2021-03-08 07:28:24 -07:00
Tom Tromey
44b675c89b Introduce type_instance_operation
This adds class type_instance_operation, which implements
TYPE_INSTANCE.

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

	* expop.h (class type_instance_operation): New.
	* eval.c (type_instance_operation::evaluate): New method.
2021-03-08 07:28:23 -07:00
Tom Tromey
cbc18219d3 Implement UNOP_MEMVAL and UNOP_MEMVAL_TYPE
This adds class unop_memval_operation and unop_memval_type_operation,
which implement UNOP_MEMVAL and UNOP_MEMVAL_TYPE.

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

	* expop.h (class unop_memval_operation)
	(class unop_memval_type_operation): New.
	* eval.c (eval_op_memval): No longer static.
	(unop_memval_operation::evaluate_for_address)
	(unop_memval_type_operation::evaluate_for_address)
	(unop_memval_operation::evaluate_for_sizeof)
	(unop_memval_type_operation::evaluate_for_sizeof): New methods.
	* ax-gdb.c (unop_memval_operation::do_generate_ax)
	(unop_memval_type_operation::do_generate_ax): New methods.
2021-03-08 07:28:23 -07:00
Tom Tromey
ae4bb61e19 Introduce unop_alignof_operation
This adds class unop_alignof_operation, which implements UNOP_ALIGNOF.

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

	* expop.h (class unop_alignof_operation): New.
	* eval.c (eval_op_alignof): No longer static.
2021-03-08 07:28:22 -07:00
Tom Tromey
5b5f5140e1 Introduce type_operation
This adds class type_operation, which implements OP_TYPE.

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

	* expop.h (class type_operation): New.
	* eval.c (eval_op_type): No longer static.
2021-03-08 07:28:21 -07:00
Tom Tromey
876469ffa1 Introduce unop_ind_operation
This adds class unop_ind_operation, which implements UNOP_IND.

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

	* expop.h (class unop_ind_base_operation)
	(class unop_ind_operation): New.
	* eval.c (eval_op_ind): No longer static.  Remove "op" parameter.
	(unop_ind_base_operation::evaluate_for_address)
	(unop_ind_base_operation::evaluate_for_sizeof): New method.
	* ax-gdb.c (gen_expr_unop) <case UNOP_IND>: New.
2021-03-08 07:28:21 -07:00
Tom Tromey
6d89e2962a Implement unary increment and decrement operations
This implements the unary increment and decrement operations, "++" and
"--".

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

	* expop.h (unop_incr_operation): New template.
	(preinc_operation, predec_operation, postinc_operation)
	(postdec_operation): New typedefs.
	* eval.c (eval_op_preinc, eval_op_predec, eval_op_postinc)
	(eval_op_postdec): No longer static.
2021-03-08 07:28:21 -07:00
Tom Tromey
9307d17b7a Implement some unary operations
This introduces a couple of new template classes and then uses them to
implement some simple unary operations.

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

	* expop.h (unary_ftype): New typedef.
	(unop_operation, usual_ax_binop_operation): New templates.
	(unary_plus_operation, unary_neg_operation)
	(unary_complement_operation, unary_logical_not_operation): New
	typedefs.
	* eval.c (eval_op_plus, eval_op_neg, eval_op_complement)
	(eval_op_lognot): No longer static.
	* ax-gdb.c (gen_expr_unop): New function.
2021-03-08 07:28:20 -07:00
Tom Tromey
d4eff4c122 Introduce repeat_operation
This adds class repeat_operation, which implements BINOP_REPEAT.

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

	* expop.h (class repeat_operation): New.
	* eval.c (eval_op_repeat): No longer static.  Remove "op"
	parameter.
	(evaluate_subexp_standard): Update.
	* ax-gdb.c (repeat_operation::do_generate_ax): New method.
2021-03-08 07:28:20 -07:00
Tom Tromey
46916f2bcb Implement binary comparison operations
This implements the binary comparison operations via a template class.

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

	* expop.h (class comparison_operation): New.
	(equal_operation, notequal_operation, less_operation)
	(gtr_operation, geq_operation, leq_operation): New typedefs.
	* eval.c (eval_op_equal, eval_op_notequal, eval_op_less)
	(eval_op_gtr, eval_op_geq, eval_op_leq): No longer static.
2021-03-08 07:28:20 -07:00
Tom Tromey
224d6424ba Introduce subscript_operation
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.
2021-03-08 07:28:19 -07:00
Tom Tromey
373907ffb2 Introduce binop_operation
This adds two new template classes, binop_operation and
usual_ax_binop_operation, and then uses these to implement a number of
binary operations that follow similar patterns.

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

	* expop.h (class binop_operation, class usual_ax_binop_operation):
	New.
	(exp_operation, intdiv_operation, mod_operation, mul_operation)
	(div_operation, rem_operation, lsh_operation, rsh_operation)
	(bitwise_and_operation, bitwise_ior_operation)
	(bitwise_xor_operation): New typedefs.
	* eval.c (eval_op_binary): No longer static.
2021-03-08 07:28:19 -07:00
Tom Tromey
5133d78b7b Introduce sub_operation
This adds class sub_operation, which implements BINOP_SUB.

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

	* expop.h (class sub_operation): New.
	* eval.c (eval_op_sub): No longer static.  Remove "op" parameter.
	(evaluate_subexp_standard): Update.
2021-03-08 07:28:19 -07:00
Tom Tromey
a94323b607 Introduce add_operation
This adds class add_operation, which implements BINOP_ADD.

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

	* expop.h (class add_operation): New.
	* eval.c (eval_op_add): No longer static.  Remove "op" parameter.
	(evaluate_subexp_standard): Update.
2021-03-08 07:28:19 -07:00
Tom Tromey
e51e26a090 Introduce concat_operation
This adds class concat_operation, which implements BINOP_CONCAT.

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

	* expop.h (class concat_operation): New.
	* eval.c (eval_op_concat): No longer static.  Remove "op"
	parameter.
	(evaluate_subexp_standard): Update.
2021-03-08 07:28:19 -07:00
Tom Tromey
07f724a8c6 Introduce structop_member_operation and structop_mptr_operation
This adds class structop_member_operation and structop_mptr_operation,
which implement STRUCTOP_MEMBER and STRUCTOP_MPTR.

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

	* expop.h (class structop_member_operation)
	(class structop_mptr_operation): New.
	* eval.c (eval_op_member): No longer static.
2021-03-08 07:28:18 -07:00
Tom Tromey
ab0609be83 Introduce structop_ptr_operation
This adds class structop_ptr_operation, which implements STRUCTOP_PTR.

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

	* expop.h (class structop_ptr_operation): New.
	* eval.c (eval_op_structop_ptr): No longer static.  Remove "op"
	parameter.
2021-03-08 07:28:18 -07:00
Tom Tromey
808b22cfd7 Introduce structop_operation
This adds class structop_base_operation and structop_operation, which
implement STRUCTOP_STRUCT.  The base class exists to unify the
completion code between STRUCTOP_STRUCT and STRUCTOP_PTR.

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

	* expop.h (class structop_base_operation)
	(class structop_operation): New.
	* eval.c (eval_op_structop_struct): No longer static.
2021-03-08 07:28:18 -07:00
Tom Tromey
09db370026 Introduce objc_selector_operation
This adds class objc_selector_operation, which implements
OP_OBJC_SELECTOR.

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

	* eval.c (eval_op_objc_selector): No longer static.
	* c-exp.h (class objc_selector_operation): New.
2021-03-08 07:28:17 -07:00
Tom Tromey
06dc61b9df Introduce objc_nsstring_operation
This adds class objc_nsstring_operation, which implements
OP_OBJC_NSSTRING.

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

	* eval.c: Include c-exp.h.
	* c-exp.h (class objc_nsstring_operation): New.
2021-03-08 07:28:17 -07:00
Tom Tromey
1594e0bb3d Introduce ternop_slice_operation
This adds class ternop_slice_operation, which implements TERNOP_SLICE.

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

	* expop.h (class ternop_slice_operation): New.
	* eval.c (eval_op_ternop): No longer static.
2021-03-08 07:28:17 -07:00
Tom Tromey
b50db09ff9 Introduce string_operation
This adds string_operation, which implements OP_STRING for most
languages (C has its own variant).

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

	* expop.h (class string_operation): New.
	* eval.c (eval_op_string): No longer static.
2021-03-08 07:28:16 -07:00
Tom Tromey
55bdbff857 Introduce register_operation
This adds class register_operation, which implements OP_REGISTER.

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

	* expop.h (class register_operation): New.
	* eval.c (eval_op_register): No longer static.
	* ax-gdb.c (register_operation::do_generate_ax): New method.
2021-03-08 07:28:16 -07:00
Tom Tromey
176793957a Introduce func_static_var_operation
This adds class func_static_var_operation, which implements
OP_FUNC_STATIC_VAR.

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

	* expop.h (class func_static_var_operation): New.
	* eval.c (eval_op_func_static_var): No longer static.
2021-03-08 07:28:15 -07:00
Tom Tromey
b5cc3923de Introduce var_entry_value_operation
This adds class var_entry_value_operation, which implements
OP_VAR_ENTRY_VALUE.

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

	* expop.h (class var_entry_value_operation): New.
	* eval.c (eval_op_var_entry_value): No longer static.
2021-03-08 07:28:15 -07:00
Tom Tromey
0c8effa32e Introduce var_msym_value_operation
This adds class var_msym_value_operation, which implements
OP_VAR_MSYM_VALUE.  A new method is added to class operation in order
to support a special case in minsym evaluation.

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

	* expression.h (class operation) <set_outermost>: New method.
	* expop.h (class var_msym_value_operation): New.
	* eval.c (eval_op_var_msym_value): No longer static.
	(var_msym_value_operation::evaluate_for_address)
	(var_msym_value_operation::evaluate_for_sizeof)
	(var_msym_value_operation::evaluate_for_cast): New methods.
	* ax-gdb.c (var_msym_value_operation::do_generate_ax): New
	method.
2021-03-08 07:28:15 -07:00
Tom Tromey
d5ab122c48 Introduce scope_operation
This adds class scope_operation, an implementation of OP_SCOPE.

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

	* expop.h (class scope_operation): New.
	* eval.c (eval_op_scope): No longer static.
	(scope_operation::evaluate_for_address): New method.
	* ax-gdb.c (scope_operation::do_generate_ax): New method.
2021-03-08 07:28:14 -07:00
Tom Tromey
e2803273a0 Introduce class operation
This patch introduces class operation, the new base class for all
expression operations.

In the new approach, an operation is simply a class that presents a
certain interface.  Operations own their operands, and management is
done via unique_ptr.

The operation interface is largely ad hoc, based on the evolution of
expression handling in GDB.  Parts (for example,
evaluate_with_coercion) are probably redundant; however I took this
approach to try to avoid mixing different kinds of refactorings.

In some specific situations, rather than add a generic method across
the entire operation class hierarchy, I chose instead to use
dynamic_cast and specialized methods on certain concrete subclasses.
This will appear in some subsequent patches.

One goal of this work is to avoid the kinds of easy-to-make errors
that affected the old implementation.  To this end, some helper
subclasses are also added here.  These helpers automate the
implementation of the 'dump', 'uses_objfile', and 'constant_p'
methods.  Nearly every concrete operation that is subsequently added
will use these facilities.  (Note that the 'dump' implementation is
only outlined here, the body appears in the next patch.)

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

	* expression.h (expr::operation): New class.
	(expr::make_operation): New function.
	(expr::operation_up): New typedef.
	* expop.h: New file.
	* eval.c (operation::evaluate_for_cast)
	(operation::evaluate_for_address, operation::evaluate_for_sizeof):
	New methods.
	* ax-gdb.c (operation::generate_ax): New method.
2021-03-08 07:28:13 -07:00
Tom Tromey
c0d7ed8ca8 Split out eval_multi_subscript
This splits MULTI_SUBSCRIPT into a new function for future use.

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

	* eval.c (eval_multi_subscript): New function.
	(evaluate_subexp_standard): Use it.
2021-03-08 07:28:13 -07:00
Tom Tromey
5e80600ed0 Split out eval_op_objc_msgcall
This splits OP_OBJC_MSGCALL into a new function for future use.

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

	* eval.c (eval_op_objc_msgcall): New function.
	(evaluate_subexp_standard): Use it.
2021-03-08 07:28:09 -07:00
Tom Tromey
fb5ba2ab26 Split out eval_binop_assign_modify
This splits BINOP_ASSIGN_MODIFY into a new function for future use.

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

	* eval.c (eval_binop_assign_modify): New function.
	(evaluate_subexp_standard): Use it.
2021-03-08 07:28:09 -07:00