[gdb/symtab] Workaround PR gas/31115
[binutils-gdb.git] / gdb / eval.c
blob2759b46a5dff15d070d88656193c6845c92c63a6
1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "language.h"
29 #include "cp-abi.h"
30 #include "infcall.h"
31 #include "objc-lang.h"
32 #include "block.h"
33 #include "parser-defs.h"
34 #include "cp-support.h"
35 #include "ui-out.h"
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "valprint.h"
39 #include "gdbsupport/gdb_obstack.h"
40 #include "objfiles.h"
41 #include "typeprint.h"
42 #include <ctype.h>
43 #include "expop.h"
44 #include "c-exp.h"
45 #include "inferior.h"
48 /* Parse the string EXP as a C expression, evaluate it,
49 and return the result as a number. */
51 CORE_ADDR
52 parse_and_eval_address (const char *exp)
54 expression_up expr = parse_expression (exp);
56 return value_as_address (expr->evaluate ());
59 /* Like parse_and_eval_address, but treats the value of the expression
60 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
61 LONGEST
62 parse_and_eval_long (const char *exp)
64 expression_up expr = parse_expression (exp);
66 return value_as_long (expr->evaluate ());
69 struct value *
70 parse_and_eval (const char *exp, parser_flags flags)
72 expression_up expr = parse_expression (exp, nullptr, flags);
74 return expr->evaluate ();
77 /* Parse up to a comma (or to a closeparen)
78 in the string EXPP as an expression, evaluate it, and return the value.
79 EXPP is advanced to point to the comma. */
81 struct value *
82 parse_to_comma_and_eval (const char **expp)
84 expression_up expr = parse_exp_1 (expp, 0, nullptr,
85 PARSER_COMMA_TERMINATES);
87 return expr->evaluate ();
91 /* See expression.h. */
93 bool
94 expression::uses_objfile (struct objfile *objfile) const
96 gdb_assert (objfile->separate_debug_objfile_backlink == nullptr);
97 return op->uses_objfile (objfile);
100 /* See expression.h. */
102 struct value *
103 expression::evaluate (struct type *expect_type, enum noside noside)
105 std::optional<enable_thread_stack_temporaries> stack_temporaries;
106 if (target_has_execution () && inferior_ptid != null_ptid
107 && language_defn->la_language == language_cplus
108 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
109 stack_temporaries.emplace (inferior_thread ());
111 struct value *retval = op->evaluate (expect_type, this, noside);
113 if (stack_temporaries.has_value ()
114 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
115 retval = retval->non_lval ();
117 return retval;
120 /* Find the current value of a watchpoint on EXP. Return the value in
121 *VALP and *RESULTP and the chain of intermediate and final values
122 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
123 not need them.
125 If PRESERVE_ERRORS is true, then exceptions are passed through.
126 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
127 occurs while evaluating the expression, *RESULTP will be set to
128 NULL. *RESULTP may be a lazy value, if the result could not be
129 read from memory. It is used to determine whether a value is
130 user-specified (we should watch the whole value) or intermediate
131 (we should watch only the bit used to locate the final value).
133 If the final value, or any intermediate value, could not be read
134 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
135 set to any referenced values. *VALP will never be a lazy value.
136 This is the value which we store in struct breakpoint.
138 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
139 released from the value chain. If VAL_CHAIN is NULL, all generated
140 values will be left on the value chain. */
142 void
143 fetch_subexp_value (struct expression *exp,
144 expr::operation *op,
145 struct value **valp, struct value **resultp,
146 std::vector<value_ref_ptr> *val_chain,
147 bool preserve_errors)
149 struct value *mark, *new_mark, *result;
151 *valp = NULL;
152 if (resultp)
153 *resultp = NULL;
154 if (val_chain)
155 val_chain->clear ();
157 /* Evaluate the expression. */
158 mark = value_mark ();
159 result = NULL;
163 result = op->evaluate (nullptr, exp, EVAL_NORMAL);
165 catch (const gdb_exception &ex)
167 /* Ignore memory errors if we want watchpoints pointing at
168 inaccessible memory to still be created; otherwise, throw the
169 error to some higher catcher. */
170 switch (ex.error)
172 case MEMORY_ERROR:
173 if (!preserve_errors)
174 break;
175 [[fallthrough]];
176 default:
177 throw;
178 break;
182 new_mark = value_mark ();
183 if (mark == new_mark)
184 return;
185 if (resultp)
186 *resultp = result;
188 /* Make sure it's not lazy, so that after the target stops again we
189 have a non-lazy previous value to compare with. */
190 if (result != NULL)
192 if (!result->lazy ())
193 *valp = result;
194 else
199 result->fetch_lazy ();
200 *valp = result;
202 catch (const gdb_exception_error &except)
208 if (val_chain)
210 /* Return the chain of intermediate values. We use this to
211 decide which addresses to watch. */
212 *val_chain = value_release_to_mark (mark);
216 /* Promote value ARG1 as appropriate before performing a unary operation
217 on this argument.
218 If the result is not appropriate for any particular language then it
219 needs to patch this function. */
221 void
222 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
223 struct value **arg1)
225 struct type *type1;
227 *arg1 = coerce_ref (*arg1);
228 type1 = check_typedef ((*arg1)->type ());
230 if (is_integral_type (type1))
232 switch (language->la_language)
234 default:
235 /* Perform integral promotion for ANSI C/C++.
236 If not appropriate for any particular language
237 it needs to modify this function. */
239 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
241 if (type1->length () < builtin_int->length ())
242 *arg1 = value_cast (builtin_int, *arg1);
244 break;
249 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
250 operation on those two operands.
251 If the result is not appropriate for any particular language then it
252 needs to patch this function. */
254 void
255 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
256 struct value **arg1, struct value **arg2)
258 struct type *promoted_type = NULL;
259 struct type *type1;
260 struct type *type2;
262 *arg1 = coerce_ref (*arg1);
263 *arg2 = coerce_ref (*arg2);
265 type1 = check_typedef ((*arg1)->type ());
266 type2 = check_typedef ((*arg2)->type ());
268 if ((type1->code () != TYPE_CODE_FLT
269 && type1->code () != TYPE_CODE_DECFLOAT
270 && !is_integral_type (type1))
271 || (type2->code () != TYPE_CODE_FLT
272 && type2->code () != TYPE_CODE_DECFLOAT
273 && !is_integral_type (type2)))
274 return;
276 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
277 return;
279 if (type1->code () == TYPE_CODE_DECFLOAT
280 || type2->code () == TYPE_CODE_DECFLOAT)
282 /* No promotion required. */
284 else if (type1->code () == TYPE_CODE_FLT
285 || type2->code () == TYPE_CODE_FLT)
287 switch (language->la_language)
289 case language_c:
290 case language_cplus:
291 case language_asm:
292 case language_objc:
293 case language_opencl:
294 /* No promotion required. */
295 break;
297 default:
298 /* For other languages the result type is unchanged from gdb
299 version 6.7 for backward compatibility.
300 If either arg was long double, make sure that value is also long
301 double. Otherwise use double. */
302 if (type1->length () * 8 > gdbarch_double_bit (gdbarch)
303 || type2->length () * 8 > gdbarch_double_bit (gdbarch))
304 promoted_type = builtin_type (gdbarch)->builtin_long_double;
305 else
306 promoted_type = builtin_type (gdbarch)->builtin_double;
307 break;
310 else if (type1->code () == TYPE_CODE_BOOL
311 && type2->code () == TYPE_CODE_BOOL)
313 /* No promotion required. */
315 else
316 /* Integral operations here. */
317 /* FIXME: Also mixed integral/booleans, with result an integer. */
319 const struct builtin_type *builtin = builtin_type (gdbarch);
320 unsigned int promoted_len1 = type1->length ();
321 unsigned int promoted_len2 = type2->length ();
322 int is_unsigned1 = type1->is_unsigned ();
323 int is_unsigned2 = type2->is_unsigned ();
324 unsigned int result_len;
325 int unsigned_operation;
327 /* Determine type length and signedness after promotion for
328 both operands. */
329 if (promoted_len1 < builtin->builtin_int->length ())
331 is_unsigned1 = 0;
332 promoted_len1 = builtin->builtin_int->length ();
334 if (promoted_len2 < builtin->builtin_int->length ())
336 is_unsigned2 = 0;
337 promoted_len2 = builtin->builtin_int->length ();
340 if (promoted_len1 > promoted_len2)
342 unsigned_operation = is_unsigned1;
343 result_len = promoted_len1;
345 else if (promoted_len2 > promoted_len1)
347 unsigned_operation = is_unsigned2;
348 result_len = promoted_len2;
350 else
352 unsigned_operation = is_unsigned1 || is_unsigned2;
353 result_len = promoted_len1;
356 switch (language->la_language)
358 case language_opencl:
359 if (result_len
360 <= lookup_signed_typename (language, "int")->length())
362 promoted_type =
363 (unsigned_operation
364 ? lookup_unsigned_typename (language, "int")
365 : lookup_signed_typename (language, "int"));
367 else if (result_len
368 <= lookup_signed_typename (language, "long")->length())
370 promoted_type =
371 (unsigned_operation
372 ? lookup_unsigned_typename (language, "long")
373 : lookup_signed_typename (language,"long"));
375 break;
376 default:
377 if (result_len <= builtin->builtin_int->length ())
379 promoted_type = (unsigned_operation
380 ? builtin->builtin_unsigned_int
381 : builtin->builtin_int);
383 else if (result_len <= builtin->builtin_long->length ())
385 promoted_type = (unsigned_operation
386 ? builtin->builtin_unsigned_long
387 : builtin->builtin_long);
389 else if (result_len <= builtin->builtin_long_long->length ())
391 promoted_type = (unsigned_operation
392 ? builtin->builtin_unsigned_long_long
393 : builtin->builtin_long_long);
395 else
397 promoted_type = (unsigned_operation
398 ? builtin->builtin_uint128
399 : builtin->builtin_int128);
401 break;
405 if (promoted_type)
407 /* Promote both operands to common type. */
408 *arg1 = value_cast (promoted_type, *arg1);
409 *arg2 = value_cast (promoted_type, *arg2);
413 static int
414 ptrmath_type_p (const struct language_defn *lang, struct type *type)
416 type = check_typedef (type);
417 if (TYPE_IS_REFERENCE (type))
418 type = type->target_type ();
420 switch (type->code ())
422 case TYPE_CODE_PTR:
423 case TYPE_CODE_FUNC:
424 return 1;
426 case TYPE_CODE_ARRAY:
427 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
429 default:
430 return 0;
434 /* Represents a fake method with the given parameter types. This is
435 used by the parser to construct a temporary "expected" type for
436 method overload resolution. FLAGS is used as instance flags of the
437 new type, in order to be able to make the new type represent a
438 const/volatile overload. */
440 class fake_method
442 public:
443 fake_method (type_instance_flags flags,
444 int num_types, struct type **param_types);
445 ~fake_method ();
447 /* The constructed type. */
448 struct type *type () { return &m_type; }
450 private:
451 struct type m_type {};
452 main_type m_main_type {};
455 fake_method::fake_method (type_instance_flags flags,
456 int num_types, struct type **param_types)
458 struct type *type = &m_type;
460 TYPE_MAIN_TYPE (type) = &m_main_type;
461 type->set_length (1);
462 type->set_code (TYPE_CODE_METHOD);
463 TYPE_CHAIN (type) = type;
464 type->set_instance_flags (flags);
465 if (num_types > 0)
467 if (param_types[num_types - 1] == NULL)
469 --num_types;
470 type->set_has_varargs (true);
472 else if (check_typedef (param_types[num_types - 1])->code ()
473 == TYPE_CODE_VOID)
475 --num_types;
476 /* Caller should have ensured this. */
477 gdb_assert (num_types == 0);
478 type->set_is_prototyped (true);
482 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
483 neither an objfile nor a gdbarch. As a result we must manually
484 allocate memory for auxiliary fields, and free the memory ourselves
485 when we are done with it. */
486 type->set_num_fields (num_types);
487 type->set_fields
488 ((struct field *) xzalloc (sizeof (struct field) * num_types));
490 while (num_types-- > 0)
491 type->field (num_types).set_type (param_types[num_types]);
494 fake_method::~fake_method ()
496 xfree (m_type.fields ());
499 namespace expr
502 value *
503 type_instance_operation::evaluate (struct type *expect_type,
504 struct expression *exp,
505 enum noside noside)
507 type_instance_flags flags = std::get<0> (m_storage);
508 std::vector<type *> &types = std::get<1> (m_storage);
510 fake_method fake_expect_type (flags, types.size (), types.data ());
511 return std::get<2> (m_storage)->evaluate (fake_expect_type.type (),
512 exp, noside);
517 /* Helper for evaluating an OP_VAR_VALUE. */
519 value *
520 evaluate_var_value (enum noside noside, const block *blk, symbol *var)
522 /* JYG: We used to just return value::zero of the symbol type if
523 we're asked to avoid side effects. Otherwise we return
524 value_of_variable (...). However I'm not sure if
525 value_of_variable () has any side effect. We need a full value
526 object returned here for whatis_exp () to call evaluate_type ()
527 and then pass the full value to value_rtti_target_type () if we
528 are dealing with a pointer or reference to a base class and print
529 object is on. */
531 struct value *ret = NULL;
535 ret = value_of_variable (var, blk);
538 catch (const gdb_exception_error &except)
540 if (noside != EVAL_AVOID_SIDE_EFFECTS)
541 throw;
543 ret = value::zero (var->type (), not_lval);
546 return ret;
549 namespace expr
553 value *
554 var_value_operation::evaluate (struct type *expect_type,
555 struct expression *exp,
556 enum noside noside)
558 symbol *var = std::get<0> (m_storage).symbol;
559 if (var->type ()->code () == TYPE_CODE_ERROR)
560 error_unknown_type (var->print_name ());
561 return evaluate_var_value (noside, std::get<0> (m_storage).block, var);
564 } /* namespace expr */
566 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
568 value *
569 evaluate_var_msym_value (enum noside noside,
570 struct objfile *objfile, minimal_symbol *msymbol)
572 CORE_ADDR address;
573 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
575 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ())
576 return value::zero (the_type, not_lval);
577 else
578 return value_at_lazy (the_type, address);
581 /* See expression.h. */
583 value *
584 evaluate_subexp_do_call (expression *exp, enum noside noside,
585 value *callee,
586 gdb::array_view<value *> argvec,
587 const char *function_name,
588 type *default_return_type)
590 if (callee == NULL)
591 error (_("Cannot evaluate function -- may be inlined"));
592 if (noside == EVAL_AVOID_SIDE_EFFECTS)
594 /* If the return type doesn't look like a function type,
595 call an error. This can happen if somebody tries to turn
596 a variable into a function call. */
598 type *ftype = callee->type ();
600 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
602 /* We don't know anything about what the internal
603 function might return, but we have to return
604 something. */
605 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
606 not_lval);
608 else if (ftype->code () == TYPE_CODE_XMETHOD)
610 type *return_type = callee->result_type_of_xmethod (argvec);
612 if (return_type == NULL)
613 error (_("Xmethod is missing return type."));
614 return value::zero (return_type, not_lval);
616 else if (ftype->code () == TYPE_CODE_FUNC
617 || ftype->code () == TYPE_CODE_METHOD)
619 if (ftype->is_gnu_ifunc ())
621 CORE_ADDR address = callee->address ();
622 type *resolved_type = find_gnu_ifunc_target_type (address);
624 if (resolved_type != NULL)
625 ftype = resolved_type;
628 type *return_type = ftype->target_type ();
630 if (return_type == NULL)
631 return_type = default_return_type;
633 if (return_type == NULL)
634 error_call_unknown_return_type (function_name);
636 return value::allocate (return_type);
638 else
639 error (_("Expression of type other than "
640 "\"Function returning ...\" used as function"));
642 switch (callee->type ()->code ())
644 case TYPE_CODE_INTERNAL_FUNCTION:
645 return call_internal_function (exp->gdbarch, exp->language_defn,
646 callee, argvec.size (), argvec.data ());
647 case TYPE_CODE_XMETHOD:
648 return callee->call_xmethod (argvec);
649 default:
650 return call_function_by_hand (callee, default_return_type, argvec);
654 namespace expr
657 value *
658 operation::evaluate_funcall (struct type *expect_type,
659 struct expression *exp,
660 enum noside noside,
661 const char *function_name,
662 const std::vector<operation_up> &args)
664 std::vector<value *> vals (args.size ());
666 value *callee = evaluate_with_coercion (exp, noside);
667 struct type *type = callee->type ();
668 if (type->code () == TYPE_CODE_PTR)
669 type = type->target_type ();
670 for (int i = 0; i < args.size (); ++i)
672 if (i < type->num_fields ())
673 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
674 else
675 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
678 return evaluate_subexp_do_call (exp, noside, callee, vals,
679 function_name, expect_type);
682 value *
683 var_value_operation::evaluate_funcall (struct type *expect_type,
684 struct expression *exp,
685 enum noside noside,
686 const std::vector<operation_up> &args)
688 if (!overload_resolution
689 || exp->language_defn->la_language != language_cplus)
690 return operation::evaluate_funcall (expect_type, exp, noside, args);
692 std::vector<value *> argvec (args.size ());
693 for (int i = 0; i < args.size (); ++i)
694 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
696 struct symbol *symp;
697 find_overload_match (argvec, NULL, NON_METHOD,
698 NULL, std::get<0> (m_storage).symbol,
699 NULL, &symp, NULL, 0, noside);
701 if (symp->type ()->code () == TYPE_CODE_ERROR)
702 error_unknown_type (symp->print_name ());
703 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
704 symp);
706 return evaluate_subexp_do_call (exp, noside, callee, argvec,
707 nullptr, expect_type);
710 value *
711 scope_operation::evaluate_funcall (struct type *expect_type,
712 struct expression *exp,
713 enum noside noside,
714 const std::vector<operation_up> &args)
716 if (!overload_resolution
717 || exp->language_defn->la_language != language_cplus)
718 return operation::evaluate_funcall (expect_type, exp, noside, args);
720 /* Unpack it locally so we can properly handle overload
721 resolution. */
722 const std::string &name = std::get<1> (m_storage);
723 struct type *type = std::get<0> (m_storage);
725 symbol *function = NULL;
726 const char *function_name = NULL;
727 std::vector<value *> argvec (1 + args.size ());
728 if (type->code () == TYPE_CODE_NAMESPACE)
730 function = cp_lookup_symbol_namespace (type->name (),
731 name.c_str (),
732 get_selected_block (0),
733 SEARCH_FUNCTION_DOMAIN).symbol;
734 if (function == NULL)
735 error (_("No symbol \"%s\" in namespace \"%s\"."),
736 name.c_str (), type->name ());
738 else
740 gdb_assert (type->code () == TYPE_CODE_STRUCT
741 || type->code () == TYPE_CODE_UNION);
742 function_name = name.c_str ();
744 /* We need a properly typed value for method lookup. */
745 argvec[0] = value::zero (type, lval_memory);
748 for (int i = 0; i < args.size (); ++i)
749 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
750 gdb::array_view<value *> arg_view = argvec;
752 value *callee = nullptr;
753 if (function_name != nullptr)
755 int static_memfuncp;
757 find_overload_match (arg_view, function_name, METHOD,
758 &argvec[0], nullptr, &callee, nullptr,
759 &static_memfuncp, 0, noside);
760 if (!static_memfuncp)
762 /* For the time being, we don't handle this. */
763 error (_("Call to overloaded function %s requires "
764 "`this' pointer"),
765 function_name);
768 arg_view = arg_view.slice (1);
770 else
772 symbol *symp;
773 arg_view = arg_view.slice (1);
774 find_overload_match (arg_view, nullptr,
775 NON_METHOD, nullptr, function,
776 nullptr, &symp, nullptr, 1, noside);
777 callee = value_of_variable (symp, get_selected_block (0));
780 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
781 nullptr, expect_type);
784 value *
785 structop_member_base::evaluate_funcall (struct type *expect_type,
786 struct expression *exp,
787 enum noside noside,
788 const std::vector<operation_up> &args)
790 /* First, evaluate the structure into lhs. */
791 value *lhs;
792 if (opcode () == STRUCTOP_MEMBER)
793 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
794 else
795 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
797 std::vector<value *> vals (args.size () + 1);
798 gdb::array_view<value *> val_view = vals;
799 /* If the function is a virtual function, then the aggregate
800 value (providing the structure) plays its part by providing
801 the vtable. Otherwise, it is just along for the ride: call
802 the function directly. */
803 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
804 value *callee;
806 type *a1_type = check_typedef (rhs->type ());
807 if (a1_type->code () == TYPE_CODE_METHODPTR)
809 if (noside == EVAL_AVOID_SIDE_EFFECTS)
810 callee = value::zero (a1_type->target_type (), not_lval);
811 else
812 callee = cplus_method_ptr_to_value (&lhs, rhs);
814 vals[0] = lhs;
816 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
818 struct type *type_ptr
819 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
820 struct type *target_type_ptr
821 = lookup_pointer_type (a1_type->target_type ());
823 /* Now, convert this value to an address. */
824 lhs = value_cast (type_ptr, lhs);
826 long mem_offset = value_as_long (rhs);
828 callee = value_from_pointer (target_type_ptr,
829 value_as_long (lhs) + mem_offset);
830 callee = value_ind (callee);
832 val_view = val_view.slice (1);
834 else
835 error (_("Non-pointer-to-member value used in pointer-to-member "
836 "construct"));
838 for (int i = 0; i < args.size (); ++i)
839 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
841 return evaluate_subexp_do_call (exp, noside, callee, val_view,
842 nullptr, expect_type);
846 value *
847 structop_base_operation::evaluate_funcall
848 (struct type *expect_type, struct expression *exp, enum noside noside,
849 const std::vector<operation_up> &args)
851 /* Allocate space for the function call arguments, Including space for a
852 `this' pointer at the start. */
853 std::vector<value *> vals (args.size () + 1);
854 /* First, evaluate the structure into vals[0]. */
855 enum exp_opcode op = opcode ();
856 if (op == STRUCTOP_STRUCT)
858 /* If v is a variable in a register, and the user types
859 v.method (), this will produce an error, because v has no
860 address.
862 A possible way around this would be to allocate a copy of
863 the variable on the stack, copy in the contents, call the
864 function, and copy out the contents. I.e. convert this
865 from call by reference to call by copy-return (or
866 whatever it's called). However, this does not work
867 because it is not the same: the method being called could
868 stash a copy of the address, and then future uses through
869 that address (after the method returns) would be expected
870 to use the variable itself, not some copy of it. */
871 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
873 else
875 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
876 /* Check to see if the operator '->' has been overloaded.
877 If the operator has been overloaded replace vals[0] with the
878 value returned by the custom operator and continue
879 evaluation. */
880 while (unop_user_defined_p (op, vals[0]))
882 struct value *value = nullptr;
885 value = value_x_unop (vals[0], op, noside);
887 catch (const gdb_exception_error &except)
889 if (except.error == NOT_FOUND_ERROR)
890 break;
891 else
892 throw;
895 vals[0] = value;
899 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
900 pointer we placed into vals[0]. */
901 for (int i = 0; i < args.size (); ++i)
902 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
904 /* The array view includes the `this' pointer. */
905 gdb::array_view<value *> arg_view (vals);
907 int static_memfuncp;
908 value *callee;
909 const char *tstr = std::get<1> (m_storage).c_str ();
910 if (overload_resolution
911 && exp->language_defn->la_language == language_cplus)
913 /* Language is C++, do some overload resolution before
914 evaluation. */
915 value *val0 = vals[0];
916 find_overload_match (arg_view, tstr, METHOD,
917 &val0, nullptr, &callee, nullptr,
918 &static_memfuncp, 0, noside);
919 vals[0] = val0;
921 else
922 /* Non-C++ case -- or no overload resolution. */
924 struct value *temp = vals[0];
926 callee = value_struct_elt (&temp, arg_view, tstr,
927 &static_memfuncp,
928 op == STRUCTOP_STRUCT
929 ? "structure" : "structure pointer");
930 /* value_struct_elt updates temp with the correct value of the
931 ``this'' pointer if necessary, so modify it to reflect any
932 ``this'' changes. */
933 vals[0] = value_from_longest (lookup_pointer_type (temp->type ()),
934 temp->address ()
935 + temp->embedded_offset ());
938 /* Take out `this' if needed. */
939 if (static_memfuncp)
940 arg_view = arg_view.slice (1);
942 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
943 nullptr, expect_type);
946 /* Helper for structop_base_operation::complete which recursively adds
947 field and method names from TYPE, a struct or union type, to the
948 OUTPUT list. PREFIX is prepended to each result. */
950 static void
951 add_struct_fields (struct type *type, completion_list &output,
952 const char *fieldname, int namelen, const char *prefix)
954 int i;
955 int computed_type_name = 0;
956 const char *type_name = NULL;
958 type = check_typedef (type);
959 for (i = 0; i < type->num_fields (); ++i)
961 if (i < TYPE_N_BASECLASSES (type))
962 add_struct_fields (TYPE_BASECLASS (type, i),
963 output, fieldname, namelen, prefix);
964 else if (type->field (i).name ())
966 if (type->field (i).name ()[0] != '\0')
968 if (! strncmp (type->field (i).name (),
969 fieldname, namelen))
970 output.emplace_back (concat (prefix, type->field (i).name (),
971 nullptr));
973 else if (type->field (i).type ()->code () == TYPE_CODE_UNION)
975 /* Recurse into anonymous unions. */
976 add_struct_fields (type->field (i).type (),
977 output, fieldname, namelen, prefix);
982 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
984 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
986 if (name && ! strncmp (name, fieldname, namelen))
988 if (!computed_type_name)
990 type_name = type->name ();
991 computed_type_name = 1;
993 /* Omit constructors from the completion list. */
994 if (!type_name || strcmp (type_name, name))
995 output.emplace_back (concat (prefix, name, nullptr));
1000 /* See expop.h. */
1002 bool
1003 structop_base_operation::complete (struct expression *exp,
1004 completion_tracker &tracker,
1005 const char *prefix)
1007 const std::string &fieldname = std::get<1> (m_storage);
1009 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp,
1010 EVAL_AVOID_SIDE_EFFECTS);
1011 struct type *type = lhs->type ();
1012 for (;;)
1014 type = check_typedef (type);
1015 if (!type->is_pointer_or_reference ())
1016 break;
1017 type = type->target_type ();
1020 if (type->code () == TYPE_CODE_UNION
1021 || type->code () == TYPE_CODE_STRUCT)
1023 completion_list result;
1025 add_struct_fields (type, result, fieldname.c_str (),
1026 fieldname.length (), prefix);
1027 tracker.add_completions (std::move (result));
1028 return true;
1031 return false;
1034 } /* namespace expr */
1036 /* Return true if type is integral or reference to integral */
1038 static bool
1039 is_integral_or_integral_reference (struct type *type)
1041 if (is_integral_type (type))
1042 return true;
1044 type = check_typedef (type);
1045 return (type != nullptr
1046 && TYPE_IS_REFERENCE (type)
1047 && is_integral_type (type->target_type ()));
1050 /* Helper function that implements the body of OP_SCOPE. */
1052 struct value *
1053 eval_op_scope (struct type *expect_type, struct expression *exp,
1054 enum noside noside,
1055 struct type *type, const char *string)
1057 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
1058 0, noside);
1059 if (arg1 == NULL)
1060 error (_("There is no field named %s"), string);
1061 return arg1;
1064 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1066 struct value *
1067 eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1068 enum noside noside, symbol *sym)
1070 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1071 return value::zero (sym->type (), not_lval);
1073 const symbol_computed_ops *computed_ops = sym->computed_ops ();
1074 if (computed_ops == nullptr
1075 || computed_ops->read_variable_at_entry == nullptr)
1076 error (_("Symbol \"%s\" does not have any specific entry value"),
1077 sym->print_name ());
1079 frame_info_ptr frame = get_selected_frame (NULL);
1080 return computed_ops->read_variable_at_entry (sym, frame);
1083 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1085 struct value *
1086 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1087 enum noside noside, bool outermost_p,
1088 bound_minimal_symbol msymbol)
1090 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1091 msymbol.minsym);
1093 struct type *type = val->type ();
1094 if (type->code () == TYPE_CODE_ERROR
1095 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1096 error_unknown_type (msymbol.minsym->print_name ());
1097 return val;
1100 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1102 struct value *
1103 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1104 enum noside noside,
1105 value *func, const char *var)
1107 CORE_ADDR addr = func->address ();
1108 const block *blk = block_for_pc (addr);
1109 struct block_symbol sym = lookup_symbol (var, blk, SEARCH_VAR_DOMAIN,
1110 nullptr);
1111 if (sym.symbol == NULL)
1112 error (_("No symbol \"%s\" in specified context."), var);
1113 return evaluate_var_value (noside, sym.block, sym.symbol);
1116 /* Helper function that implements the body of OP_REGISTER. */
1118 struct value *
1119 eval_op_register (struct type *expect_type, struct expression *exp,
1120 enum noside noside, const char *name)
1122 int regno;
1123 struct value *val;
1125 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1126 name, strlen (name));
1127 if (regno == -1)
1128 error (_("Register $%s not available."), name);
1130 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1131 a value with the appropriate register type. Unfortunately,
1132 we don't have easy access to the type of user registers.
1133 So for these registers, we fetch the register value regardless
1134 of the evaluation mode. */
1135 if (noside == EVAL_AVOID_SIDE_EFFECTS
1136 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1137 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
1138 else
1139 val = value_of_register
1140 (regno, get_next_frame_sentinel_okay (get_selected_frame ()));
1141 if (val == NULL)
1142 error (_("Value of register %s not available."), name);
1143 else
1144 return val;
1147 namespace expr
1150 value *
1151 string_operation::evaluate (struct type *expect_type,
1152 struct expression *exp,
1153 enum noside noside)
1155 const std::string &str = std::get<0> (m_storage);
1156 struct type *type = language_string_char_type (exp->language_defn,
1157 exp->gdbarch);
1158 return value_string (str.c_str (), str.size (), type);
1161 struct value *
1162 ternop_slice_operation::evaluate (struct type *expect_type,
1163 struct expression *exp,
1164 enum noside noside)
1166 struct value *array
1167 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1168 struct value *low
1169 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1170 struct value *upper
1171 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1173 int lowbound = value_as_long (low);
1174 int upperbound = value_as_long (upper);
1175 return value_slice (array, lowbound, upperbound - lowbound + 1);
1178 } /* namespace expr */
1180 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1182 struct value *
1183 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1184 enum noside noside,
1185 const char *sel)
1187 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1188 return value_from_longest (selector_type,
1189 lookup_child_selector (exp->gdbarch, sel));
1192 /* A helper function for STRUCTOP_STRUCT. */
1194 struct value *
1195 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1196 enum noside noside,
1197 struct value *arg1, const char *string)
1199 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1200 NULL, "structure");
1201 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1202 arg3 = value::zero (arg3->type (), arg3->lval ());
1203 return arg3;
1206 /* A helper function for STRUCTOP_PTR. */
1208 struct value *
1209 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1210 enum noside noside,
1211 struct value *arg1, const char *string)
1213 /* Check to see if operator '->' has been overloaded. If so replace
1214 arg1 with the value returned by evaluating operator->(). */
1215 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1217 struct value *value = NULL;
1220 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1223 catch (const gdb_exception_error &except)
1225 if (except.error == NOT_FOUND_ERROR)
1226 break;
1227 else
1228 throw;
1231 arg1 = value;
1234 /* JYG: if print object is on we need to replace the base type
1235 with rtti type in order to continue on with successful
1236 lookup of member / method only available in the rtti type. */
1238 struct type *arg_type = arg1->type ();
1239 struct type *real_type;
1240 int full, using_enc;
1241 LONGEST top;
1242 struct value_print_options opts;
1244 get_user_print_options (&opts);
1245 if (opts.objectprint && arg_type->target_type ()
1246 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
1248 real_type = value_rtti_indirect_type (arg1, &full, &top,
1249 &using_enc);
1250 if (real_type)
1251 arg1 = value_cast (real_type, arg1);
1255 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1256 NULL, "structure pointer");
1257 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1258 arg3 = value::zero (arg3->type (), arg3->lval ());
1259 return arg3;
1262 /* A helper function for STRUCTOP_MEMBER. */
1264 struct value *
1265 eval_op_member (struct type *expect_type, struct expression *exp,
1266 enum noside noside,
1267 struct value *arg1, struct value *arg2)
1269 long mem_offset;
1271 struct value *arg3;
1272 struct type *type = check_typedef (arg2->type ());
1273 switch (type->code ())
1275 case TYPE_CODE_METHODPTR:
1276 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1277 return value::zero (type->target_type (), not_lval);
1278 else
1280 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1281 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
1282 return value_ind (arg2);
1285 case TYPE_CODE_MEMBERPTR:
1286 /* Now, convert these values to an address. */
1287 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
1288 arg1 = value_addr (arg1);
1289 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1290 arg1, 1);
1292 mem_offset = value_as_long (arg2);
1294 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
1295 value_as_long (arg1) + mem_offset);
1296 return value_ind (arg3);
1298 default:
1299 error (_("non-pointer-to-member value used "
1300 "in pointer-to-member construct"));
1304 /* A helper function for BINOP_ADD. */
1306 struct value *
1307 eval_op_add (struct type *expect_type, struct expression *exp,
1308 enum noside noside,
1309 struct value *arg1, struct value *arg2)
1311 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1312 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1313 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1314 && is_integral_or_integral_reference (arg2->type ()))
1315 return value_ptradd (arg1, value_as_long (arg2));
1316 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1317 && is_integral_or_integral_reference (arg1->type ()))
1318 return value_ptradd (arg2, value_as_long (arg1));
1319 else
1321 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1322 return value_binop (arg1, arg2, BINOP_ADD);
1326 /* A helper function for BINOP_SUB. */
1328 struct value *
1329 eval_op_sub (struct type *expect_type, struct expression *exp,
1330 enum noside noside,
1331 struct value *arg1, struct value *arg2)
1333 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1334 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1335 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1336 && ptrmath_type_p (exp->language_defn, arg2->type ()))
1338 /* FIXME -- should be ptrdiff_t */
1339 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1340 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1342 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1343 && is_integral_or_integral_reference (arg2->type ()))
1344 return value_ptradd (arg1, - value_as_long (arg2));
1345 else
1347 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1348 return value_binop (arg1, arg2, BINOP_SUB);
1352 /* Helper function for several different binary operations. */
1354 struct value *
1355 eval_op_binary (struct type *expect_type, struct expression *exp,
1356 enum noside noside, enum exp_opcode op,
1357 struct value *arg1, struct value *arg2)
1359 if (binop_user_defined_p (op, arg1, arg2))
1360 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1361 else
1363 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1364 fudge arg2 to avoid division-by-zero, the caller is
1365 (theoretically) only looking for the type of the result. */
1366 if (noside == EVAL_AVOID_SIDE_EFFECTS
1367 /* ??? Do we really want to test for BINOP_MOD here?
1368 The implementation of value_binop gives it a well-defined
1369 value. */
1370 && (op == BINOP_DIV
1371 || op == BINOP_INTDIV
1372 || op == BINOP_REM
1373 || op == BINOP_MOD)
1374 && value_logical_not (arg2))
1376 struct value *v_one;
1378 v_one = value_one (arg2->type ());
1379 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1380 return value_binop (arg1, v_one, op);
1382 else
1384 /* For shift and integer exponentiation operations,
1385 only promote the first argument. */
1386 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1387 && is_integral_type (arg2->type ()))
1388 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1389 else
1390 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1392 return value_binop (arg1, arg2, op);
1397 /* A helper function for BINOP_SUBSCRIPT. */
1399 struct value *
1400 eval_op_subscript (struct type *expect_type, struct expression *exp,
1401 enum noside noside, enum exp_opcode op,
1402 struct value *arg1, struct value *arg2)
1404 if (binop_user_defined_p (op, arg1, arg2))
1405 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1406 else
1408 /* If the user attempts to subscript something that is not an
1409 array or pointer type (like a plain int variable for example),
1410 then report this as an error. */
1412 arg1 = coerce_ref (arg1);
1413 struct type *type = check_typedef (arg1->type ());
1414 if (type->code () != TYPE_CODE_ARRAY
1415 && type->code () != TYPE_CODE_PTR)
1417 if (type->name ())
1418 error (_("cannot subscript something of type `%s'"),
1419 type->name ());
1420 else
1421 error (_("cannot subscript requested type"));
1424 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1425 return value::zero (type->target_type (), arg1->lval ());
1426 else
1427 return value_subscript (arg1, value_as_long (arg2));
1431 /* A helper function for BINOP_EQUAL. */
1433 struct value *
1434 eval_op_equal (struct type *expect_type, struct expression *exp,
1435 enum noside noside, enum exp_opcode op,
1436 struct value *arg1, struct value *arg2)
1438 if (binop_user_defined_p (op, arg1, arg2))
1440 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1442 else
1444 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1445 int tem = value_equal (arg1, arg2);
1446 struct type *type = language_bool_type (exp->language_defn,
1447 exp->gdbarch);
1448 return value_from_longest (type, (LONGEST) tem);
1452 /* A helper function for BINOP_NOTEQUAL. */
1454 struct value *
1455 eval_op_notequal (struct type *expect_type, struct expression *exp,
1456 enum noside noside, enum exp_opcode op,
1457 struct value *arg1, struct value *arg2)
1459 if (binop_user_defined_p (op, arg1, arg2))
1461 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1463 else
1465 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1466 int tem = value_equal (arg1, arg2);
1467 struct type *type = language_bool_type (exp->language_defn,
1468 exp->gdbarch);
1469 return value_from_longest (type, (LONGEST) ! tem);
1473 /* A helper function for BINOP_LESS. */
1475 struct value *
1476 eval_op_less (struct type *expect_type, struct expression *exp,
1477 enum noside noside, enum exp_opcode op,
1478 struct value *arg1, struct value *arg2)
1480 if (binop_user_defined_p (op, arg1, arg2))
1482 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1484 else
1486 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1487 int tem = value_less (arg1, arg2);
1488 struct type *type = language_bool_type (exp->language_defn,
1489 exp->gdbarch);
1490 return value_from_longest (type, (LONGEST) tem);
1494 /* A helper function for BINOP_GTR. */
1496 struct value *
1497 eval_op_gtr (struct type *expect_type, struct expression *exp,
1498 enum noside noside, enum exp_opcode op,
1499 struct value *arg1, struct value *arg2)
1501 if (binop_user_defined_p (op, arg1, arg2))
1503 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1505 else
1507 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1508 int tem = value_less (arg2, arg1);
1509 struct type *type = language_bool_type (exp->language_defn,
1510 exp->gdbarch);
1511 return value_from_longest (type, (LONGEST) tem);
1515 /* A helper function for BINOP_GEQ. */
1517 struct value *
1518 eval_op_geq (struct type *expect_type, struct expression *exp,
1519 enum noside noside, enum exp_opcode op,
1520 struct value *arg1, struct value *arg2)
1522 if (binop_user_defined_p (op, arg1, arg2))
1524 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1526 else
1528 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1529 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1530 struct type *type = language_bool_type (exp->language_defn,
1531 exp->gdbarch);
1532 return value_from_longest (type, (LONGEST) tem);
1536 /* A helper function for BINOP_LEQ. */
1538 struct value *
1539 eval_op_leq (struct type *expect_type, struct expression *exp,
1540 enum noside noside, enum exp_opcode op,
1541 struct value *arg1, struct value *arg2)
1543 if (binop_user_defined_p (op, arg1, arg2))
1545 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1547 else
1549 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1550 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1551 struct type *type = language_bool_type (exp->language_defn,
1552 exp->gdbarch);
1553 return value_from_longest (type, (LONGEST) tem);
1557 /* A helper function for BINOP_REPEAT. */
1559 struct value *
1560 eval_op_repeat (struct type *expect_type, struct expression *exp,
1561 enum noside noside, enum exp_opcode op,
1562 struct value *arg1, struct value *arg2)
1564 struct type *type = check_typedef (arg2->type ());
1565 if (type->code () != TYPE_CODE_INT
1566 && type->code () != TYPE_CODE_ENUM)
1567 error (_("Non-integral right operand for \"@\" operator."));
1568 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1570 return allocate_repeat_value (arg1->type (),
1571 longest_to_int (value_as_long (arg2)));
1573 else
1574 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1577 /* A helper function for UNOP_PLUS. */
1579 struct value *
1580 eval_op_plus (struct type *expect_type, struct expression *exp,
1581 enum noside noside, enum exp_opcode op,
1582 struct value *arg1)
1584 if (unop_user_defined_p (op, arg1))
1585 return value_x_unop (arg1, op, noside);
1586 else
1588 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1589 return value_pos (arg1);
1593 /* A helper function for UNOP_NEG. */
1595 struct value *
1596 eval_op_neg (struct type *expect_type, struct expression *exp,
1597 enum noside noside, enum exp_opcode op,
1598 struct value *arg1)
1600 if (unop_user_defined_p (op, arg1))
1601 return value_x_unop (arg1, op, noside);
1602 else
1604 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1605 return value_neg (arg1);
1609 /* A helper function for UNOP_COMPLEMENT. */
1611 struct value *
1612 eval_op_complement (struct type *expect_type, struct expression *exp,
1613 enum noside noside, enum exp_opcode op,
1614 struct value *arg1)
1616 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1617 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1618 else
1620 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1621 return value_complement (arg1);
1625 /* A helper function for UNOP_LOGICAL_NOT. */
1627 struct value *
1628 eval_op_lognot (struct type *expect_type, struct expression *exp,
1629 enum noside noside, enum exp_opcode op,
1630 struct value *arg1)
1632 if (unop_user_defined_p (op, arg1))
1633 return value_x_unop (arg1, op, noside);
1634 else
1636 struct type *type = language_bool_type (exp->language_defn,
1637 exp->gdbarch);
1638 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1642 /* A helper function for UNOP_IND. */
1644 struct value *
1645 eval_op_ind (struct type *expect_type, struct expression *exp,
1646 enum noside noside,
1647 struct value *arg1)
1649 struct type *type = check_typedef (arg1->type ());
1650 if (type->code () == TYPE_CODE_METHODPTR
1651 || type->code () == TYPE_CODE_MEMBERPTR)
1652 error (_("Attempt to dereference pointer "
1653 "to member without an object"));
1654 if (unop_user_defined_p (UNOP_IND, arg1))
1655 return value_x_unop (arg1, UNOP_IND, noside);
1656 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1658 type = check_typedef (arg1->type ());
1660 /* If the type pointed to is dynamic then in order to resolve the
1661 dynamic properties we must actually dereference the pointer.
1662 There is a risk that this dereference will have side-effects
1663 in the inferior, but being able to print accurate type
1664 information seems worth the risk. */
1665 if (!type->is_pointer_or_reference ()
1666 || !is_dynamic_type (type->target_type ()))
1668 if (type->is_pointer_or_reference ()
1669 /* In C you can dereference an array to get the 1st elt. */
1670 || type->code () == TYPE_CODE_ARRAY)
1671 return value::zero (type->target_type (),
1672 lval_memory);
1673 else if (type->code () == TYPE_CODE_INT)
1674 /* GDB allows dereferencing an int. */
1675 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
1676 lval_memory);
1677 else
1678 error (_("Attempt to take contents of a non-pointer value."));
1682 /* Allow * on an integer so we can cast it to whatever we want.
1683 This returns an int, which seems like the most C-like thing to
1684 do. "long long" variables are rare enough that
1685 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1686 if (type->code () == TYPE_CODE_INT)
1687 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1688 value_as_address (arg1));
1689 return value_ind (arg1);
1692 /* A helper function for UNOP_ALIGNOF. */
1694 struct value *
1695 eval_op_alignof (struct type *expect_type, struct expression *exp,
1696 enum noside noside,
1697 struct value *arg1)
1699 struct type *type = arg1->type ();
1700 /* FIXME: This should be size_t. */
1701 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1702 ULONGEST align = type_align (type);
1703 if (align == 0)
1704 error (_("could not determine alignment of type"));
1705 return value_from_longest (size_type, align);
1708 /* A helper function for UNOP_MEMVAL. */
1710 struct value *
1711 eval_op_memval (struct type *expect_type, struct expression *exp,
1712 enum noside noside,
1713 struct value *arg1, struct type *type)
1715 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1716 return value::zero (type, lval_memory);
1717 else
1718 return value_at_lazy (type, value_as_address (arg1));
1721 /* A helper function for UNOP_PREINCREMENT. */
1723 struct value *
1724 eval_op_preinc (struct type *expect_type, struct expression *exp,
1725 enum noside noside, enum exp_opcode op,
1726 struct value *arg1)
1728 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1729 return arg1;
1730 else if (unop_user_defined_p (op, arg1))
1732 return value_x_unop (arg1, op, noside);
1734 else
1736 struct value *arg2;
1737 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1738 arg2 = value_ptradd (arg1, 1);
1739 else
1741 struct value *tmp = arg1;
1743 arg2 = value_one (arg1->type ());
1744 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1745 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1748 return value_assign (arg1, arg2);
1752 /* A helper function for UNOP_PREDECREMENT. */
1754 struct value *
1755 eval_op_predec (struct type *expect_type, struct expression *exp,
1756 enum noside noside, enum exp_opcode op,
1757 struct value *arg1)
1759 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1760 return arg1;
1761 else if (unop_user_defined_p (op, arg1))
1763 return value_x_unop (arg1, op, noside);
1765 else
1767 struct value *arg2;
1768 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1769 arg2 = value_ptradd (arg1, -1);
1770 else
1772 struct value *tmp = arg1;
1774 arg2 = value_one (arg1->type ());
1775 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1776 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1779 return value_assign (arg1, arg2);
1783 /* A helper function for UNOP_POSTINCREMENT. */
1785 struct value *
1786 eval_op_postinc (struct type *expect_type, struct expression *exp,
1787 enum noside noside, enum exp_opcode op,
1788 struct value *arg1)
1790 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1791 return arg1;
1792 else if (unop_user_defined_p (op, arg1))
1794 return value_x_unop (arg1, op, noside);
1796 else
1798 struct value *arg3 = arg1->non_lval ();
1799 struct value *arg2;
1801 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1802 arg2 = value_ptradd (arg1, 1);
1803 else
1805 struct value *tmp = arg1;
1807 arg2 = value_one (arg1->type ());
1808 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1809 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1812 value_assign (arg1, arg2);
1813 return arg3;
1817 /* A helper function for UNOP_POSTDECREMENT. */
1819 struct value *
1820 eval_op_postdec (struct type *expect_type, struct expression *exp,
1821 enum noside noside, enum exp_opcode op,
1822 struct value *arg1)
1824 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1825 return arg1;
1826 else if (unop_user_defined_p (op, arg1))
1828 return value_x_unop (arg1, op, noside);
1830 else
1832 struct value *arg3 = arg1->non_lval ();
1833 struct value *arg2;
1835 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1836 arg2 = value_ptradd (arg1, -1);
1837 else
1839 struct value *tmp = arg1;
1841 arg2 = value_one (arg1->type ());
1842 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1843 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1846 value_assign (arg1, arg2);
1847 return arg3;
1851 /* A helper function for OP_TYPE. */
1853 struct value *
1854 eval_op_type (struct type *expect_type, struct expression *exp,
1855 enum noside noside, struct type *type)
1857 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1858 return value::allocate (type);
1859 else
1860 error (_("Attempt to use a type name as an expression"));
1863 /* A helper function for BINOP_ASSIGN_MODIFY. */
1865 struct value *
1866 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1867 enum noside noside, enum exp_opcode op,
1868 struct value *arg1, struct value *arg2)
1870 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1871 return arg1;
1872 if (binop_user_defined_p (op, arg1, arg2))
1873 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1874 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1875 arg1->type ())
1876 && is_integral_type (arg2->type ()))
1877 arg2 = value_ptradd (arg1, value_as_long (arg2));
1878 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1879 arg1->type ())
1880 && is_integral_type (arg2->type ()))
1881 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1882 else
1884 struct value *tmp = arg1;
1886 /* For shift and integer exponentiation operations,
1887 only promote the first argument. */
1888 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1889 && is_integral_type (arg2->type ()))
1890 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1891 else
1892 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1894 arg2 = value_binop (tmp, arg2, op);
1896 return value_assign (arg1, arg2);
1899 /* Note that ARGS needs 2 empty slots up front and must end with a
1900 null pointer. */
1901 static struct value *
1902 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1903 enum noside noside, CORE_ADDR selector,
1904 value *target, gdb::array_view<value *> args)
1906 CORE_ADDR responds_selector = 0;
1907 CORE_ADDR method_selector = 0;
1909 int struct_return = 0;
1911 struct value *msg_send = NULL;
1912 struct value *msg_send_stret = NULL;
1913 int gnu_runtime = 0;
1915 struct value *method = NULL;
1916 struct value *called_method = NULL;
1918 struct type *selector_type = NULL;
1919 struct type *long_type;
1920 struct type *type;
1922 struct value *ret = NULL;
1923 CORE_ADDR addr = 0;
1925 value *argvec[5];
1927 long_type = builtin_type (exp->gdbarch)->builtin_long;
1928 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1930 if (value_as_long (target) == 0)
1931 return value_from_longest (long_type, 0);
1933 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1934 gnu_runtime = 1;
1936 /* Find the method dispatch (Apple runtime) or method lookup
1937 (GNU runtime) function for Objective-C. These will be used
1938 to lookup the symbol information for the method. If we
1939 can't find any symbol information, then we'll use these to
1940 call the method, otherwise we can call the method
1941 directly. The msg_send_stret function is used in the special
1942 case of a method that returns a structure (Apple runtime
1943 only). */
1944 if (gnu_runtime)
1946 type = selector_type;
1948 type = lookup_function_type (type);
1949 type = lookup_pointer_type (type);
1950 type = lookup_function_type (type);
1951 type = lookup_pointer_type (type);
1953 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1954 msg_send_stret
1955 = find_function_in_inferior ("objc_msg_lookup", NULL);
1957 msg_send = value_from_pointer (type, value_as_address (msg_send));
1958 msg_send_stret = value_from_pointer (type,
1959 value_as_address (msg_send_stret));
1961 else
1963 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1964 /* Special dispatcher for methods returning structs. */
1965 msg_send_stret
1966 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1969 /* Verify the target object responds to this method. The
1970 standard top-level 'Object' class uses a different name for
1971 the verification method than the non-standard, but more
1972 often used, 'NSObject' class. Make sure we check for both. */
1974 responds_selector
1975 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1976 if (responds_selector == 0)
1977 responds_selector
1978 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1980 if (responds_selector == 0)
1981 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1983 method_selector
1984 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1985 if (method_selector == 0)
1986 method_selector
1987 = lookup_child_selector (exp->gdbarch, "methodFor:");
1989 if (method_selector == 0)
1990 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1992 /* Call the verification method, to make sure that the target
1993 class implements the desired method. */
1995 argvec[0] = msg_send;
1996 argvec[1] = target;
1997 argvec[2] = value_from_longest (long_type, responds_selector);
1998 argvec[3] = value_from_longest (long_type, selector);
1999 argvec[4] = 0;
2001 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2002 if (gnu_runtime)
2004 /* Function objc_msg_lookup returns a pointer. */
2005 argvec[0] = ret;
2006 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2008 if (value_as_long (ret) == 0)
2009 error (_("Target does not respond to this message selector."));
2011 /* Call "methodForSelector:" method, to get the address of a
2012 function method that implements this selector for this
2013 class. If we can find a symbol at that address, then we
2014 know the return type, parameter types etc. (that's a good
2015 thing). */
2017 argvec[0] = msg_send;
2018 argvec[1] = target;
2019 argvec[2] = value_from_longest (long_type, method_selector);
2020 argvec[3] = value_from_longest (long_type, selector);
2021 argvec[4] = 0;
2023 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2024 if (gnu_runtime)
2026 argvec[0] = ret;
2027 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2030 /* ret should now be the selector. */
2032 addr = value_as_long (ret);
2033 if (addr)
2035 struct symbol *sym = NULL;
2037 /* The address might point to a function descriptor;
2038 resolve it to the actual code address instead. */
2039 addr = gdbarch_convert_from_func_ptr_addr
2040 (exp->gdbarch, addr, current_inferior ()->top_target ());
2042 /* Is it a high_level symbol? */
2043 sym = find_pc_function (addr);
2044 if (sym != NULL)
2045 method = value_of_variable (sym, 0);
2048 /* If we found a method with symbol information, check to see
2049 if it returns a struct. Otherwise assume it doesn't. */
2051 if (method)
2053 CORE_ADDR funaddr;
2054 struct type *val_type;
2056 funaddr = find_function_addr (method, &val_type);
2058 block_for_pc (funaddr);
2060 val_type = check_typedef (val_type);
2062 if ((val_type == NULL)
2063 || (val_type->code () == TYPE_CODE_ERROR))
2065 if (expect_type != NULL)
2066 val_type = expect_type;
2069 struct_return = using_struct_return (exp->gdbarch, method,
2070 val_type);
2072 else if (expect_type != NULL)
2074 struct_return = using_struct_return (exp->gdbarch, NULL,
2075 check_typedef (expect_type));
2078 /* Found a function symbol. Now we will substitute its
2079 value in place of the message dispatcher (obj_msgSend),
2080 so that we call the method directly instead of thru
2081 the dispatcher. The main reason for doing this is that
2082 we can now evaluate the return value and parameter values
2083 according to their known data types, in case we need to
2084 do things like promotion, dereferencing, special handling
2085 of structs and doubles, etc.
2087 We want to use the type signature of 'method', but still
2088 jump to objc_msgSend() or objc_msgSend_stret() to better
2089 mimic the behavior of the runtime. */
2091 if (method)
2093 if (method->type ()->code () != TYPE_CODE_FUNC)
2094 error (_("method address has symbol information "
2095 "with non-function type; skipping"));
2097 /* Create a function pointer of the appropriate type, and
2098 replace its value with the value of msg_send or
2099 msg_send_stret. We must use a pointer here, as
2100 msg_send and msg_send_stret are of pointer type, and
2101 the representation may be different on systems that use
2102 function descriptors. */
2103 if (struct_return)
2104 called_method
2105 = value_from_pointer (lookup_pointer_type (method->type ()),
2106 value_as_address (msg_send_stret));
2107 else
2108 called_method
2109 = value_from_pointer (lookup_pointer_type (method->type ()),
2110 value_as_address (msg_send));
2112 else
2114 if (struct_return)
2115 called_method = msg_send_stret;
2116 else
2117 called_method = msg_send;
2121 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2123 /* If the return type doesn't look like a function type,
2124 call an error. This can happen if somebody tries to
2125 turn a variable into a function call. This is here
2126 because people often want to call, eg, strcmp, which
2127 gdb doesn't know is a function. If gdb isn't asked for
2128 it's opinion (ie. through "whatis"), it won't offer
2129 it. */
2131 struct type *callee_type = called_method->type ();
2133 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2134 callee_type = callee_type->target_type ();
2135 callee_type = callee_type->target_type ();
2137 if (callee_type)
2139 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2140 return value::allocate (expect_type);
2141 else
2142 return value::allocate (callee_type);
2144 else
2145 error (_("Expression of type other than "
2146 "\"method returning ...\" used as a method"));
2149 /* Now depending on whether we found a symbol for the method,
2150 we will either call the runtime dispatcher or the method
2151 directly. */
2153 args[0] = target;
2154 args[1] = value_from_longest (long_type, selector);
2156 if (gnu_runtime && (method != NULL))
2158 /* Function objc_msg_lookup returns a pointer. */
2159 struct type *tem_type = called_method->type ();
2160 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2161 called_method->deprecated_set_type (tem_type);
2162 called_method = call_function_by_hand (called_method, NULL, args);
2165 return call_function_by_hand (called_method, NULL, args);
2168 /* Helper function for MULTI_SUBSCRIPT. */
2170 static struct value *
2171 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2172 enum noside noside, value *arg1,
2173 gdb::array_view<value *> args)
2175 for (value *arg2 : args)
2177 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2179 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2181 else
2183 arg1 = coerce_ref (arg1);
2184 struct type *type = check_typedef (arg1->type ());
2186 switch (type->code ())
2188 case TYPE_CODE_PTR:
2189 case TYPE_CODE_ARRAY:
2190 case TYPE_CODE_STRING:
2191 arg1 = value_subscript (arg1, value_as_long (arg2));
2192 break;
2194 default:
2195 if (type->name ())
2196 error (_("cannot subscript something of type `%s'"),
2197 type->name ());
2198 else
2199 error (_("cannot subscript requested type"));
2203 return (arg1);
2206 namespace expr
2209 value *
2210 objc_msgcall_operation::evaluate (struct type *expect_type,
2211 struct expression *exp,
2212 enum noside noside)
2214 enum noside sub_no_side = EVAL_NORMAL;
2215 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2217 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2218 sub_no_side = EVAL_NORMAL;
2219 else
2220 sub_no_side = noside;
2221 value *target
2222 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2224 if (value_as_long (target) == 0)
2225 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2226 else
2227 sub_no_side = noside;
2228 std::vector<operation_up> &args = std::get<2> (m_storage);
2229 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2230 argvec[0] = nullptr;
2231 argvec[1] = nullptr;
2232 for (int i = 0; i < args.size (); ++i)
2233 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2234 argvec[args.size () + 2] = nullptr;
2236 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2237 get<0> (m_storage), target,
2238 gdb::make_array_view (argvec,
2239 args.size () + 3));
2242 value *
2243 multi_subscript_operation::evaluate (struct type *expect_type,
2244 struct expression *exp,
2245 enum noside noside)
2247 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2248 std::vector<operation_up> &values = std::get<1> (m_storage);
2249 value **argvec = XALLOCAVEC (struct value *, values.size ());
2250 for (int ix = 0; ix < values.size (); ++ix)
2251 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2252 return eval_multi_subscript (expect_type, exp, noside, arg1,
2253 gdb::make_array_view (argvec, values.size ()));
2256 value *
2257 logical_and_operation::evaluate (struct type *expect_type,
2258 struct expression *exp,
2259 enum noside noside)
2261 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2263 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2264 EVAL_AVOID_SIDE_EFFECTS);
2266 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2268 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2269 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2271 else
2273 bool tem = value_logical_not (arg1);
2274 if (!tem)
2276 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2277 tem = value_logical_not (arg2);
2279 struct type *type = language_bool_type (exp->language_defn,
2280 exp->gdbarch);
2281 return value_from_longest (type, !tem);
2285 value *
2286 logical_or_operation::evaluate (struct type *expect_type,
2287 struct expression *exp,
2288 enum noside noside)
2290 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2292 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2293 EVAL_AVOID_SIDE_EFFECTS);
2295 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2297 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2298 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2300 else
2302 bool tem = value_logical_not (arg1);
2303 if (tem)
2305 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2306 tem = value_logical_not (arg2);
2309 struct type *type = language_bool_type (exp->language_defn,
2310 exp->gdbarch);
2311 return value_from_longest (type, !tem);
2315 value *
2316 adl_func_operation::evaluate (struct type *expect_type,
2317 struct expression *exp,
2318 enum noside noside)
2320 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2321 std::vector<value *> args (arg_ops.size ());
2322 for (int i = 0; i < arg_ops.size (); ++i)
2323 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2325 struct symbol *symp;
2326 find_overload_match (args, std::get<0> (m_storage).c_str (),
2327 NON_METHOD,
2328 nullptr, nullptr,
2329 nullptr, &symp, nullptr, 0, noside);
2330 if (symp->type ()->code () == TYPE_CODE_ERROR)
2331 error_unknown_type (symp->print_name ());
2332 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2333 return evaluate_subexp_do_call (exp, noside, callee, args,
2334 nullptr, expect_type);
2338 /* This function evaluates brace-initializers (in C/C++) for
2339 structure types. */
2341 struct value *
2342 array_operation::evaluate_struct_tuple (struct value *struct_val,
2343 struct expression *exp,
2344 enum noside noside, int nargs)
2346 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2347 struct type *struct_type = check_typedef (struct_val->type ());
2348 struct type *field_type;
2349 int fieldno = -1;
2351 int idx = 0;
2352 while (--nargs >= 0)
2354 struct value *val = NULL;
2355 int bitpos, bitsize;
2356 bfd_byte *addr;
2358 fieldno++;
2359 /* Skip static fields. */
2360 while (fieldno < struct_type->num_fields ()
2361 && struct_type->field (fieldno).is_static ())
2362 fieldno++;
2363 if (fieldno >= struct_type->num_fields ())
2364 error (_("too many initializers"));
2365 field_type = struct_type->field (fieldno).type ();
2366 if (field_type->code () == TYPE_CODE_UNION
2367 && struct_type->field (fieldno).name ()[0] == '0')
2368 error (_("don't know which variant you want to set"));
2370 /* Here, struct_type is the type of the inner struct,
2371 while substruct_type is the type of the inner struct.
2372 These are the same for normal structures, but a variant struct
2373 contains anonymous union fields that contain substruct fields.
2374 The value fieldno is the index of the top-level (normal or
2375 anonymous union) field in struct_field, while the value
2376 subfieldno is the index of the actual real (named inner) field
2377 in substruct_type. */
2379 field_type = struct_type->field (fieldno).type ();
2380 if (val == 0)
2381 val = in_args[idx++]->evaluate (field_type, exp, noside);
2383 /* Now actually set the field in struct_val. */
2385 /* Assign val to field fieldno. */
2386 if (val->type () != field_type)
2387 val = value_cast (field_type, val);
2389 bitsize = struct_type->field (fieldno).bitsize ();
2390 bitpos = struct_type->field (fieldno).loc_bitpos ();
2391 addr = struct_val->contents_writeable ().data () + bitpos / 8;
2392 if (bitsize)
2393 modify_field (struct_type, addr,
2394 value_as_long (val), bitpos % 8, bitsize);
2395 else
2396 memcpy (addr, val->contents ().data (),
2397 val->type ()->length ());
2400 return struct_val;
2403 value *
2404 array_operation::evaluate (struct type *expect_type,
2405 struct expression *exp,
2406 enum noside noside)
2408 const int provided_low_bound = std::get<0> (m_storage);
2409 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2410 const int nargs = std::get<1> (m_storage) - provided_low_bound + 1;
2411 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2413 if (expect_type != nullptr
2414 && type->code () == TYPE_CODE_STRUCT)
2416 struct value *rec = value::allocate (expect_type);
2418 memset (rec->contents_raw ().data (), '\0', type->length ());
2419 return evaluate_struct_tuple (rec, exp, noside, nargs);
2422 if (expect_type != nullptr
2423 && type->code () == TYPE_CODE_ARRAY)
2425 struct type *range_type = type->index_type ();
2426 struct type *element_type = type->target_type ();
2427 struct value *array = value::allocate (expect_type);
2428 int element_size = check_typedef (element_type)->length ();
2429 LONGEST low_bound, high_bound;
2431 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2433 low_bound = 0;
2434 high_bound = (type->length () / element_size) - 1;
2436 if (low_bound + nargs - 1 > high_bound)
2437 error (_("Too many array elements"));
2438 memset (array->contents_raw ().data (), 0, expect_type->length ());
2439 for (int idx = 0; idx < nargs; ++idx)
2441 struct value *element;
2443 element = in_args[idx]->evaluate (element_type, exp, noside);
2444 if (element->type () != element_type)
2445 element = value_cast (element_type, element);
2446 memcpy (array->contents_raw ().data () + idx * element_size,
2447 element->contents ().data (),
2448 element_size);
2450 return array;
2453 if (expect_type != nullptr
2454 && type->code () == TYPE_CODE_SET)
2456 struct value *set = value::allocate (expect_type);
2457 gdb_byte *valaddr = set->contents_raw ().data ();
2458 struct type *element_type = type->index_type ();
2459 struct type *check_type = element_type;
2460 LONGEST low_bound, high_bound;
2462 /* Get targettype of elementtype. */
2463 while (check_type->code () == TYPE_CODE_RANGE
2464 || check_type->code () == TYPE_CODE_TYPEDEF)
2465 check_type = check_type->target_type ();
2467 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2468 error (_("(power)set type with unknown size"));
2469 memset (valaddr, '\0', type->length ());
2470 for (int idx = 0; idx < nargs; idx++)
2472 LONGEST range_low, range_high;
2473 struct type *range_low_type, *range_high_type;
2474 struct value *elem_val;
2476 elem_val = in_args[idx]->evaluate (element_type, exp, noside);
2477 range_low_type = range_high_type = elem_val->type ();
2478 range_low = range_high = value_as_long (elem_val);
2480 /* Check types of elements to avoid mixture of elements from
2481 different types. Also check if type of element is "compatible"
2482 with element type of powerset. */
2483 if (range_low_type->code () == TYPE_CODE_RANGE)
2484 range_low_type = range_low_type->target_type ();
2485 if (range_high_type->code () == TYPE_CODE_RANGE)
2486 range_high_type = range_high_type->target_type ();
2487 if ((range_low_type->code () != range_high_type->code ())
2488 || (range_low_type->code () == TYPE_CODE_ENUM
2489 && (range_low_type != range_high_type)))
2490 /* different element modes. */
2491 error (_("POWERSET tuple elements of different mode"));
2492 if ((check_type->code () != range_low_type->code ())
2493 || (check_type->code () == TYPE_CODE_ENUM
2494 && range_low_type != check_type))
2495 error (_("incompatible POWERSET tuple elements"));
2496 if (range_low > range_high)
2498 warning (_("empty POWERSET tuple range"));
2499 continue;
2501 if (range_low < low_bound || range_high > high_bound)
2502 error (_("POWERSET tuple element out of range"));
2503 range_low -= low_bound;
2504 range_high -= low_bound;
2505 for (; range_low <= range_high; range_low++)
2507 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2509 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2510 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2511 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2512 |= 1 << bit_index;
2515 return set;
2518 std::vector<value *> argvec (nargs);
2519 for (int tem = 0; tem < nargs; tem++)
2521 /* Ensure that array expressions are coerced into pointer
2522 objects. */
2523 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2525 return value_array (provided_low_bound, argvec);
2528 value *
2529 unop_extract_operation::evaluate (struct type *expect_type,
2530 struct expression *exp,
2531 enum noside noside)
2533 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2534 struct type *type = get_type ();
2536 if (type->length () > old_value->type ()->length ())
2537 error (_("length type is larger than the value type"));
2539 struct value *result = value::allocate (type);
2540 old_value->contents_copy (result, 0, 0, type->length ());
2541 return result;
2547 /* Helper for evaluate_subexp_for_address. */
2549 static value *
2550 evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
2551 value *x)
2553 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2555 struct type *type = check_typedef (x->type ());
2557 if (TYPE_IS_REFERENCE (type))
2558 return value::zero (lookup_pointer_type (type->target_type ()),
2559 not_lval);
2560 else if (x->lval () == lval_memory || value_must_coerce_to_target (x))
2561 return value::zero (lookup_pointer_type (x->type ()),
2562 not_lval);
2563 else
2564 error (_("Attempt to take address of "
2565 "value not located in memory."));
2567 return value_addr (x);
2570 namespace expr
2573 value *
2574 operation::evaluate_for_cast (struct type *expect_type,
2575 struct expression *exp,
2576 enum noside noside)
2578 value *val = evaluate (expect_type, exp, noside);
2579 return value_cast (expect_type, val);
2582 value *
2583 operation::evaluate_for_address (struct expression *exp, enum noside noside)
2585 value *val = evaluate (nullptr, exp, noside);
2586 return evaluate_subexp_for_address_base (exp, noside, val);
2589 value *
2590 scope_operation::evaluate_for_address (struct expression *exp,
2591 enum noside noside)
2593 value *x = value_aggregate_elt (std::get<0> (m_storage),
2594 std::get<1> (m_storage).c_str (),
2595 NULL, 1, noside);
2596 if (x == NULL)
2597 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
2598 return x;
2601 value *
2602 unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2603 enum noside noside)
2605 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2607 /* We can't optimize out "&*" if there's a user-defined operator*. */
2608 if (unop_user_defined_p (UNOP_IND, x))
2610 x = value_x_unop (x, UNOP_IND, noside);
2611 return evaluate_subexp_for_address_base (exp, noside, x);
2614 return coerce_array (x);
2617 value *
2618 var_msym_value_operation::evaluate_for_address (struct expression *exp,
2619 enum noside noside)
2621 const bound_minimal_symbol &b = std::get<0> (m_storage);
2622 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2623 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2625 struct type *type = lookup_pointer_type (val->type ());
2626 return value::zero (type, not_lval);
2628 else
2629 return value_addr (val);
2632 value *
2633 unop_memval_operation::evaluate_for_address (struct expression *exp,
2634 enum noside noside)
2636 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2637 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2640 value *
2641 unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2642 enum noside noside)
2644 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2645 EVAL_AVOID_SIDE_EFFECTS);
2646 struct type *type = typeval->type ();
2647 return value_cast (lookup_pointer_type (type),
2648 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2651 value *
2652 var_value_operation::evaluate_for_address (struct expression *exp,
2653 enum noside noside)
2655 symbol *var = std::get<0> (m_storage).symbol;
2657 /* C++: The "address" of a reference should yield the address
2658 * of the object pointed to. Let value_addr() deal with it. */
2659 if (TYPE_IS_REFERENCE (var->type ()))
2660 return operation::evaluate_for_address (exp, noside);
2662 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2664 struct type *type = lookup_pointer_type (var->type ());
2665 enum address_class sym_class = var->aclass ();
2667 if (sym_class == LOC_CONST
2668 || sym_class == LOC_CONST_BYTES
2669 || sym_class == LOC_REGISTER)
2670 error (_("Attempt to take address of register or constant."));
2672 return value::zero (type, not_lval);
2674 else
2675 return address_of_variable (var, std::get<0> (m_storage).block);
2678 value *
2679 var_value_operation::evaluate_with_coercion (struct expression *exp,
2680 enum noside noside)
2682 struct symbol *var = std::get<0> (m_storage).symbol;
2683 struct type *type = check_typedef (var->type ());
2684 if (type->code () == TYPE_CODE_ARRAY
2685 && !type->is_vector ()
2686 && CAST_IS_CONVERSION (exp->language_defn))
2688 struct value *val = address_of_variable (var,
2689 std::get<0> (m_storage).block);
2690 return value_cast (lookup_pointer_type (type->target_type ()), val);
2692 return evaluate (nullptr, exp, noside);
2697 /* Helper function for evaluating the size of a type. */
2699 static value *
2700 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2702 /* FIXME: This should be size_t. */
2703 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2704 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2705 "When applied to a reference or a reference type, the result is
2706 the size of the referenced type." */
2707 type = check_typedef (type);
2708 if (exp->language_defn->la_language == language_cplus
2709 && (TYPE_IS_REFERENCE (type)))
2710 type = check_typedef (type->target_type ());
2711 else if (exp->language_defn->la_language == language_fortran
2712 && type->code () == TYPE_CODE_PTR)
2714 /* Dereference Fortran pointer types to allow them for the Fortran
2715 sizeof intrinsic. */
2716 type = check_typedef (type->target_type ());
2718 return value_from_longest (size_type, (LONGEST) type->length ());
2721 namespace expr
2724 value *
2725 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2727 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2728 return evaluate_subexp_for_sizeof_base (exp, val->type ());
2731 value *
2732 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2733 enum noside noside)
2736 const bound_minimal_symbol &b = std::get<0> (m_storage);
2737 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2739 struct type *type = mval->type ();
2740 if (type->code () == TYPE_CODE_ERROR)
2741 error_unknown_type (b.minsym->print_name ());
2743 /* FIXME: This should be size_t. */
2744 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2745 return value_from_longest (size_type, type->length ());
2748 value *
2749 subscript_operation::evaluate_for_sizeof (struct expression *exp,
2750 enum noside noside)
2752 if (noside == EVAL_NORMAL)
2754 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2755 EVAL_AVOID_SIDE_EFFECTS);
2756 struct type *type = check_typedef (val->type ());
2757 if (type->code () == TYPE_CODE_ARRAY)
2759 type = check_typedef (type->target_type ());
2760 if (type->code () == TYPE_CODE_ARRAY)
2762 type = type->index_type ();
2763 /* Only re-evaluate the right hand side if the resulting type
2764 is a variable length type. */
2765 if (type->bounds ()->flag_bound_evaluated)
2767 val = evaluate (nullptr, exp, EVAL_NORMAL);
2768 /* FIXME: This should be size_t. */
2769 struct type *size_type
2770 = builtin_type (exp->gdbarch)->builtin_int;
2771 return value_from_longest
2772 (size_type, (LONGEST) val->type ()->length ());
2778 return operation::evaluate_for_sizeof (exp, noside);
2781 value *
2782 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2783 enum noside noside)
2785 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2786 EVAL_AVOID_SIDE_EFFECTS);
2787 struct type *type = check_typedef (val->type ());
2788 if (!type->is_pointer_or_reference ()
2789 && type->code () != TYPE_CODE_ARRAY)
2790 error (_("Attempt to take contents of a non-pointer value."));
2791 type = type->target_type ();
2792 if (is_dynamic_type (type))
2793 type = value_ind (val)->type ();
2794 /* FIXME: This should be size_t. */
2795 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2796 return value_from_longest (size_type, (LONGEST) type->length ());
2799 value *
2800 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2801 enum noside noside)
2803 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2806 value *
2807 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2808 enum noside noside)
2810 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2811 EVAL_AVOID_SIDE_EFFECTS);
2812 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
2815 value *
2816 var_value_operation::evaluate_for_sizeof (struct expression *exp,
2817 enum noside noside)
2819 struct type *type = std::get<0> (m_storage).symbol->type ();
2820 if (is_dynamic_type (type))
2822 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2823 type = val->type ();
2824 if (type->code () == TYPE_CODE_ARRAY)
2826 /* FIXME: This should be size_t. */
2827 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2828 if (type_not_allocated (type) || type_not_associated (type))
2829 return value::zero (size_type, not_lval);
2830 else if (is_dynamic_type (type->index_type ())
2831 && !type->bounds ()->high.is_available ())
2832 return value::allocate_optimized_out (size_type);
2835 return evaluate_subexp_for_sizeof_base (exp, type);
2838 value *
2839 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2840 struct expression *exp,
2841 enum noside noside)
2843 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2844 return value::zero (to_type, not_lval);
2846 const bound_minimal_symbol &b = std::get<0> (m_storage);
2847 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2849 val = value_cast (to_type, val);
2851 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2852 if (val->lval () == lval_memory)
2854 if (val->lazy ())
2855 val->fetch_lazy ();
2856 val->set_lval (not_lval);
2858 return val;
2861 value *
2862 var_value_operation::evaluate_for_cast (struct type *to_type,
2863 struct expression *exp,
2864 enum noside noside)
2866 value *val = evaluate_var_value (noside,
2867 std::get<0> (m_storage).block,
2868 std::get<0> (m_storage).symbol);
2870 val = value_cast (to_type, val);
2872 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2873 if (val->lval () == lval_memory)
2875 if (val->lazy ())
2876 val->fetch_lazy ();
2877 val->set_lval (not_lval);
2879 return val;
2884 /* Parse a type expression in the string [P..P+LENGTH). */
2886 struct type *
2887 parse_and_eval_type (const char *p, int length)
2889 char *tmp = (char *) alloca (length + 4);
2891 tmp[0] = '(';
2892 memcpy (tmp + 1, p, length);
2893 tmp[length + 1] = ')';
2894 tmp[length + 2] = '0';
2895 tmp[length + 3] = '\0';
2896 expression_up expr = parse_expression (tmp);
2897 expr::unop_cast_operation *op
2898 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2899 if (op == nullptr)
2900 error (_("Internal error in eval_type."));
2901 return op->get_type ();