Re: [PATCH v2] infcall: Add support for integer literals as reference function paramters

Tom Tromey
2025-10-21T20:19:54Z

>>>>> "Keith" == Keith Seitz <keiths@redhat.com> writes:

Keith> This patch attempts to mitigate the shortcomings of passing literals
Keith> to inferior function calls requiring references. The specific use case here
Keith> is std::map's operator[]:

Thanks for doing this.

Keith> struct value *
Keith> -value_coerce_to_target (struct value *val)
Keith> +value_coerce_to_target (struct value *val, struct type *param_type)
Keith> {

I wasn't totally sold on doing the work here and not in value_arg_coerce
but I guess it makes sense.

Keith> + if (param_type != nullptr && param_type->language () == language_cplus
Keith> + && TYPE_IS_REFERENCE (param_type))
Keith> + {

Can we get here with a value that has an address, and so we don't need
to make a copy? Like if there's a local variable 'x', and you do 'print
map[x]'?

If the argument is already in memory, then just casting its address to a
reference would be a lot better, especially because memcpy()ing the data
into the inferior is only really correct for trivially-copyable types.

I don't know if that property (trivially-copyable) is all that easy to
detect in gdb, but if it is, I guess it would make sense to detect it
and throw an exception. Since in that case we'd be looking at something
like a callee that wants memory but where the object has been SROA'd or
something along those lines.

Tom