arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / eval.c
blob457a4362289dac383be28d352b3a714587344029
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 "symtab.h"
21 #include "gdbtypes.h"
22 #include "value.h"
23 #include "expression.h"
24 #include "target.h"
25 #include "frame.h"
26 #include "gdbthread.h"
27 #include "language.h"
28 #include "cp-abi.h"
29 #include "infcall.h"
30 #include "objc-lang.h"
31 #include "block.h"
32 #include "parser-defs.h"
33 #include "cp-support.h"
34 #include "ui-out.h"
35 #include "regcache.h"
36 #include "user-regs.h"
37 #include "valprint.h"
38 #include "gdbsupport/gdb_obstack.h"
39 #include "objfiles.h"
40 #include "typeprint.h"
41 #include <ctype.h>
42 #include "expop.h"
43 #include "c-exp.h"
44 #include "inferior.h"
47 /* Parse the string EXP as a C expression, evaluate it,
48 and return the result as a number. */
50 CORE_ADDR
51 parse_and_eval_address (const char *exp)
53 expression_up expr = parse_expression (exp);
55 return value_as_address (expr->evaluate ());
58 /* Like parse_and_eval_address, but treats the value of the expression
59 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
60 LONGEST
61 parse_and_eval_long (const char *exp)
63 expression_up expr = parse_expression (exp);
65 return value_as_long (expr->evaluate ());
68 struct value *
69 parse_and_eval (const char *exp, parser_flags flags)
71 expression_up expr = parse_expression (exp, nullptr, flags);
73 return expr->evaluate ();
76 /* Parse up to a comma (or to a closeparen)
77 in the string EXPP as an expression, evaluate it, and return the value.
78 EXPP is advanced to point to the comma. */
80 struct value *
81 parse_to_comma_and_eval (const char **expp)
83 expression_up expr = parse_exp_1 (expp, 0, nullptr,
84 PARSER_COMMA_TERMINATES);
86 return expr->evaluate ();
90 /* See expression.h. */
92 bool
93 expression::uses_objfile (struct objfile *objfile) const
95 gdb_assert (objfile->separate_debug_objfile_backlink == nullptr);
96 return op->uses_objfile (objfile);
99 /* See expression.h. */
101 struct value *
102 expression::evaluate (struct type *expect_type, enum noside noside)
104 std::optional<enable_thread_stack_temporaries> stack_temporaries;
105 if (target_has_execution () && inferior_ptid != null_ptid
106 && language_defn->la_language == language_cplus
107 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
108 stack_temporaries.emplace (inferior_thread ());
110 struct value *retval = op->evaluate (expect_type, this, noside);
112 if (stack_temporaries.has_value ()
113 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
114 retval = retval->non_lval ();
116 return retval;
119 /* Find the current value of a watchpoint on EXP. Return the value in
120 *VALP and *RESULTP and the chain of intermediate and final values
121 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
122 not need them.
124 If PRESERVE_ERRORS is true, then exceptions are passed through.
125 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
126 occurs while evaluating the expression, *RESULTP will be set to
127 NULL. *RESULTP may be a lazy value, if the result could not be
128 read from memory. It is used to determine whether a value is
129 user-specified (we should watch the whole value) or intermediate
130 (we should watch only the bit used to locate the final value).
132 If the final value, or any intermediate value, could not be read
133 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
134 set to any referenced values. *VALP will never be a lazy value.
135 This is the value which we store in struct breakpoint.
137 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
138 released from the value chain. If VAL_CHAIN is NULL, all generated
139 values will be left on the value chain. */
141 void
142 fetch_subexp_value (struct expression *exp,
143 expr::operation *op,
144 struct value **valp, struct value **resultp,
145 std::vector<value_ref_ptr> *val_chain,
146 bool preserve_errors)
148 struct value *mark, *new_mark, *result;
150 *valp = NULL;
151 if (resultp)
152 *resultp = NULL;
153 if (val_chain)
154 val_chain->clear ();
156 /* Evaluate the expression. */
157 mark = value_mark ();
158 result = NULL;
162 result = op->evaluate (nullptr, exp, EVAL_NORMAL);
164 catch (const gdb_exception &ex)
166 /* Ignore memory errors if we want watchpoints pointing at
167 inaccessible memory to still be created; otherwise, throw the
168 error to some higher catcher. */
169 switch (ex.error)
171 case MEMORY_ERROR:
172 if (!preserve_errors)
173 break;
174 [[fallthrough]];
175 default:
176 throw;
177 break;
181 new_mark = value_mark ();
182 if (mark == new_mark)
183 return;
184 if (resultp)
185 *resultp = result;
187 /* Make sure it's not lazy, so that after the target stops again we
188 have a non-lazy previous value to compare with. */
189 if (result != NULL)
191 if (!result->lazy ())
192 *valp = result;
193 else
198 result->fetch_lazy ();
199 *valp = result;
201 catch (const gdb_exception_error &except)
207 if (val_chain)
209 /* Return the chain of intermediate values. We use this to
210 decide which addresses to watch. */
211 *val_chain = value_release_to_mark (mark);
215 /* Promote value ARG1 as appropriate before performing a unary operation
216 on this argument.
217 If the result is not appropriate for any particular language then it
218 needs to patch this function. */
220 void
221 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
222 struct value **arg1)
224 struct type *type1;
226 *arg1 = coerce_ref (*arg1);
227 type1 = check_typedef ((*arg1)->type ());
229 if (is_integral_type (type1))
231 switch (language->la_language)
233 default:
234 /* Perform integral promotion for ANSI C/C++.
235 If not appropriate for any particular language
236 it needs to modify this function. */
238 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
240 if (type1->length () < builtin_int->length ())
241 *arg1 = value_cast (builtin_int, *arg1);
243 break;
248 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
249 operation on those two operands.
250 If the result is not appropriate for any particular language then it
251 needs to patch this function. */
253 void
254 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
255 struct value **arg1, struct value **arg2)
257 struct type *promoted_type = NULL;
258 struct type *type1;
259 struct type *type2;
261 *arg1 = coerce_ref (*arg1);
262 *arg2 = coerce_ref (*arg2);
264 type1 = check_typedef ((*arg1)->type ());
265 type2 = check_typedef ((*arg2)->type ());
267 if ((type1->code () != TYPE_CODE_FLT
268 && type1->code () != TYPE_CODE_DECFLOAT
269 && !is_integral_type (type1))
270 || (type2->code () != TYPE_CODE_FLT
271 && type2->code () != TYPE_CODE_DECFLOAT
272 && !is_integral_type (type2)))
273 return;
275 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
276 return;
278 if (type1->code () == TYPE_CODE_DECFLOAT
279 || type2->code () == TYPE_CODE_DECFLOAT)
281 /* No promotion required. */
283 else if (type1->code () == TYPE_CODE_FLT
284 || type2->code () == TYPE_CODE_FLT)
286 switch (language->la_language)
288 case language_c:
289 case language_cplus:
290 case language_asm:
291 case language_objc:
292 case language_opencl:
293 /* No promotion required. */
294 break;
296 default:
297 /* For other languages the result type is unchanged from gdb
298 version 6.7 for backward compatibility.
299 If either arg was long double, make sure that value is also long
300 double. Otherwise use double. */
301 if (type1->length () * 8 > gdbarch_double_bit (gdbarch)
302 || type2->length () * 8 > gdbarch_double_bit (gdbarch))
303 promoted_type = builtin_type (gdbarch)->builtin_long_double;
304 else
305 promoted_type = builtin_type (gdbarch)->builtin_double;
306 break;
309 else if (type1->code () == TYPE_CODE_BOOL
310 && type2->code () == TYPE_CODE_BOOL)
312 /* No promotion required. */
314 else
315 /* Integral operations here. */
316 /* FIXME: Also mixed integral/booleans, with result an integer. */
318 const struct builtin_type *builtin = builtin_type (gdbarch);
319 unsigned int promoted_len1 = type1->length ();
320 unsigned int promoted_len2 = type2->length ();
321 int is_unsigned1 = type1->is_unsigned ();
322 int is_unsigned2 = type2->is_unsigned ();
323 unsigned int result_len;
324 int unsigned_operation;
326 /* Determine type length and signedness after promotion for
327 both operands. */
328 if (promoted_len1 < builtin->builtin_int->length ())
330 is_unsigned1 = 0;
331 promoted_len1 = builtin->builtin_int->length ();
333 if (promoted_len2 < builtin->builtin_int->length ())
335 is_unsigned2 = 0;
336 promoted_len2 = builtin->builtin_int->length ();
339 if (promoted_len1 > promoted_len2)
341 unsigned_operation = is_unsigned1;
342 result_len = promoted_len1;
344 else if (promoted_len2 > promoted_len1)
346 unsigned_operation = is_unsigned2;
347 result_len = promoted_len2;
349 else
351 unsigned_operation = is_unsigned1 || is_unsigned2;
352 result_len = promoted_len1;
355 switch (language->la_language)
357 case language_opencl:
358 if (result_len
359 <= lookup_signed_typename (language, "int")->length())
361 promoted_type =
362 (unsigned_operation
363 ? lookup_unsigned_typename (language, "int")
364 : lookup_signed_typename (language, "int"));
366 else if (result_len
367 <= lookup_signed_typename (language, "long")->length())
369 promoted_type =
370 (unsigned_operation
371 ? lookup_unsigned_typename (language, "long")
372 : lookup_signed_typename (language,"long"));
374 break;
375 default:
376 if (result_len <= builtin->builtin_int->length ())
378 promoted_type = (unsigned_operation
379 ? builtin->builtin_unsigned_int
380 : builtin->builtin_int);
382 else if (result_len <= builtin->builtin_long->length ())
384 promoted_type = (unsigned_operation
385 ? builtin->builtin_unsigned_long
386 : builtin->builtin_long);
388 else if (result_len <= builtin->builtin_long_long->length ())
390 promoted_type = (unsigned_operation
391 ? builtin->builtin_unsigned_long_long
392 : builtin->builtin_long_long);
394 else
396 promoted_type = (unsigned_operation
397 ? builtin->builtin_uint128
398 : builtin->builtin_int128);
400 break;
404 if (promoted_type)
406 /* Promote both operands to common type. */
407 *arg1 = value_cast (promoted_type, *arg1);
408 *arg2 = value_cast (promoted_type, *arg2);
412 static int
413 ptrmath_type_p (const struct language_defn *lang, struct type *type)
415 type = check_typedef (type);
416 if (TYPE_IS_REFERENCE (type))
417 type = type->target_type ();
419 switch (type->code ())
421 case TYPE_CODE_PTR:
422 case TYPE_CODE_FUNC:
423 return 1;
425 case TYPE_CODE_ARRAY:
426 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
428 default:
429 return 0;
433 /* Represents a fake method with the given parameter types. This is
434 used by the parser to construct a temporary "expected" type for
435 method overload resolution. FLAGS is used as instance flags of the
436 new type, in order to be able to make the new type represent a
437 const/volatile overload. */
439 class fake_method
441 public:
442 fake_method (type_instance_flags flags,
443 int num_types, struct type **param_types);
444 ~fake_method ();
446 /* The constructed type. */
447 struct type *type () { return &m_type; }
449 private:
450 struct type m_type {};
451 main_type m_main_type {};
454 fake_method::fake_method (type_instance_flags flags,
455 int num_types, struct type **param_types)
457 struct type *type = &m_type;
459 TYPE_MAIN_TYPE (type) = &m_main_type;
460 type->set_length (1);
461 type->set_code (TYPE_CODE_METHOD);
462 TYPE_CHAIN (type) = type;
463 type->set_instance_flags (flags);
464 if (num_types > 0)
466 if (param_types[num_types - 1] == NULL)
468 --num_types;
469 type->set_has_varargs (true);
471 else if (check_typedef (param_types[num_types - 1])->code ()
472 == TYPE_CODE_VOID)
474 --num_types;
475 /* Caller should have ensured this. */
476 gdb_assert (num_types == 0);
477 type->set_is_prototyped (true);
481 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
482 neither an objfile nor a gdbarch. As a result we must manually
483 allocate memory for auxiliary fields, and free the memory ourselves
484 when we are done with it. */
485 type->set_num_fields (num_types);
486 type->set_fields
487 ((struct field *) xzalloc (sizeof (struct field) * num_types));
489 while (num_types-- > 0)
490 type->field (num_types).set_type (param_types[num_types]);
493 fake_method::~fake_method ()
495 xfree (m_type.fields ());
498 namespace expr
501 value *
502 type_instance_operation::evaluate (struct type *expect_type,
503 struct expression *exp,
504 enum noside noside)
506 type_instance_flags flags = std::get<0> (m_storage);
507 std::vector<type *> &types = std::get<1> (m_storage);
509 fake_method fake_expect_type (flags, types.size (), types.data ());
510 return std::get<2> (m_storage)->evaluate (fake_expect_type.type (),
511 exp, noside);
516 /* Helper for evaluating an OP_VAR_VALUE. */
518 value *
519 evaluate_var_value (enum noside noside, const block *blk, symbol *var)
521 /* JYG: We used to just return value::zero of the symbol type if
522 we're asked to avoid side effects. Otherwise we return
523 value_of_variable (...). However I'm not sure if
524 value_of_variable () has any side effect. We need a full value
525 object returned here for whatis_exp () to call evaluate_type ()
526 and then pass the full value to value_rtti_target_type () if we
527 are dealing with a pointer or reference to a base class and print
528 object is on. */
530 struct value *ret = NULL;
534 ret = value_of_variable (var, blk);
537 catch (const gdb_exception_error &except)
539 if (noside != EVAL_AVOID_SIDE_EFFECTS)
540 throw;
542 ret = value::zero (var->type (), not_lval);
545 return ret;
548 namespace expr
552 value *
553 var_value_operation::evaluate (struct type *expect_type,
554 struct expression *exp,
555 enum noside noside)
557 symbol *var = std::get<0> (m_storage).symbol;
558 if (var->type ()->code () == TYPE_CODE_ERROR)
559 error_unknown_type (var->print_name ());
560 return evaluate_var_value (noside, std::get<0> (m_storage).block, var);
563 } /* namespace expr */
565 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
567 value *
568 evaluate_var_msym_value (enum noside noside,
569 struct objfile *objfile, minimal_symbol *msymbol)
571 CORE_ADDR address;
572 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
574 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ())
575 return value::zero (the_type, not_lval);
576 else
577 return value_at_lazy (the_type, address);
580 /* See expression.h. */
582 value *
583 evaluate_subexp_do_call (expression *exp, enum noside noside,
584 value *callee,
585 gdb::array_view<value *> argvec,
586 const char *function_name,
587 type *default_return_type)
589 if (callee == NULL)
590 error (_("Cannot evaluate function -- may be inlined"));
592 type *ftype = callee->type ();
594 /* If the callee is a struct, there might be a user-defined function call
595 operator that should be used instead. */
596 std::vector<value *> vals;
597 if (overload_resolution
598 && exp->language_defn->la_language == language_cplus
599 && check_typedef (ftype)->code () == TYPE_CODE_STRUCT)
601 /* Include space for the `this' pointer at the start. */
602 vals.resize (argvec.size () + 1);
604 vals[0] = value_addr (callee);
605 for (int i = 0; i < argvec.size (); ++i)
606 vals[i + 1] = argvec[i];
608 int static_memfuncp;
609 find_overload_match (vals, "operator()", METHOD, &vals[0], nullptr,
610 &callee, nullptr, &static_memfuncp, 0, noside);
611 if (!static_memfuncp)
612 argvec = vals;
614 ftype = callee->type ();
617 if (noside == EVAL_AVOID_SIDE_EFFECTS)
619 /* If the return type doesn't look like a function type,
620 call an error. This can happen if somebody tries to turn
621 a variable into a function call. */
623 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
625 /* The call to call_internal_function below handles noside. */
627 else if (ftype->code () == TYPE_CODE_XMETHOD)
629 type *return_type = callee->result_type_of_xmethod (argvec);
631 if (return_type == NULL)
632 error (_("Xmethod is missing return type."));
633 return value::zero (return_type, not_lval);
635 else if (ftype->code () == TYPE_CODE_FUNC
636 || ftype->code () == TYPE_CODE_METHOD)
638 if (ftype->is_gnu_ifunc ())
640 CORE_ADDR address = callee->address ();
641 type *resolved_type = find_gnu_ifunc_target_type (address);
643 if (resolved_type != NULL)
644 ftype = resolved_type;
647 type *return_type = ftype->target_type ();
649 if (return_type == NULL)
650 return_type = default_return_type;
652 if (return_type == NULL)
653 error_call_unknown_return_type (function_name);
655 return value::allocate (return_type);
657 else
658 error (_("Expression of type other than "
659 "\"Function returning ...\" used as function"));
661 switch (callee->type ()->code ())
663 case TYPE_CODE_INTERNAL_FUNCTION:
664 return call_internal_function (exp->gdbarch, exp->language_defn,
665 callee, argvec.size (), argvec.data (),
666 noside);
667 case TYPE_CODE_XMETHOD:
668 return callee->call_xmethod (argvec);
669 default:
670 return call_function_by_hand (callee, default_return_type, argvec);
674 namespace expr
677 value *
678 operation::evaluate_funcall (struct type *expect_type,
679 struct expression *exp,
680 enum noside noside,
681 const char *function_name,
682 const std::vector<operation_up> &args)
684 std::vector<value *> vals (args.size ());
686 value *callee = evaluate_with_coercion (exp, noside);
687 struct type *type = callee->type ();
688 if (type->code () == TYPE_CODE_PTR)
689 type = type->target_type ();
690 /* If type is a struct, num_fields would refer to the number of
691 members in the type, not the number of arguments. */
692 bool type_has_arguments
693 = type->code () == TYPE_CODE_FUNC || type->code () == TYPE_CODE_METHOD;
694 for (int i = 0; i < args.size (); ++i)
696 if (type_has_arguments && i < type->num_fields ())
697 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
698 else
699 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
702 return evaluate_subexp_do_call (exp, noside, callee, vals,
703 function_name, expect_type);
706 value *
707 var_value_operation::evaluate_funcall (struct type *expect_type,
708 struct expression *exp,
709 enum noside noside,
710 const std::vector<operation_up> &args)
712 if (!overload_resolution
713 || exp->language_defn->la_language != language_cplus)
714 return operation::evaluate_funcall (expect_type, exp, noside, args);
716 std::vector<value *> argvec (args.size ());
717 for (int i = 0; i < args.size (); ++i)
718 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
720 struct symbol *symp;
721 find_overload_match (argvec, NULL, NON_METHOD,
722 NULL, std::get<0> (m_storage).symbol,
723 NULL, &symp, NULL, 0, noside);
725 if (symp->type ()->code () == TYPE_CODE_ERROR)
726 error_unknown_type (symp->print_name ());
727 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
728 symp);
730 return evaluate_subexp_do_call (exp, noside, callee, argvec,
731 nullptr, expect_type);
734 value *
735 scope_operation::evaluate_funcall (struct type *expect_type,
736 struct expression *exp,
737 enum noside noside,
738 const std::vector<operation_up> &args)
740 if (!overload_resolution
741 || exp->language_defn->la_language != language_cplus)
742 return operation::evaluate_funcall (expect_type, exp, noside, args);
744 /* Unpack it locally so we can properly handle overload
745 resolution. */
746 const std::string &name = std::get<1> (m_storage);
747 struct type *type = std::get<0> (m_storage);
749 symbol *function = NULL;
750 const char *function_name = NULL;
751 std::vector<value *> argvec (1 + args.size ());
752 if (type->code () == TYPE_CODE_NAMESPACE)
754 function = cp_lookup_symbol_namespace (type->name (),
755 name.c_str (),
756 get_selected_block (0),
757 SEARCH_FUNCTION_DOMAIN).symbol;
758 if (function == NULL)
759 error (_("No symbol \"%s\" in namespace \"%s\"."),
760 name.c_str (), type->name ());
762 else
764 gdb_assert (type->code () == TYPE_CODE_STRUCT
765 || type->code () == TYPE_CODE_UNION);
766 function_name = name.c_str ();
768 /* We need a properly typed value for method lookup. */
769 argvec[0] = value::zero (type, lval_memory);
772 for (int i = 0; i < args.size (); ++i)
773 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
774 gdb::array_view<value *> arg_view = argvec;
776 value *callee = nullptr;
777 if (function_name != nullptr)
779 int static_memfuncp;
781 find_overload_match (arg_view, function_name, METHOD,
782 &argvec[0], nullptr, &callee, nullptr,
783 &static_memfuncp, 0, noside);
784 if (!static_memfuncp)
786 /* For the time being, we don't handle this. */
787 error (_("Call to overloaded function %s requires "
788 "`this' pointer"),
789 function_name);
792 arg_view = arg_view.slice (1);
794 else
796 symbol *symp;
797 arg_view = arg_view.slice (1);
798 find_overload_match (arg_view, nullptr,
799 NON_METHOD, nullptr, function,
800 nullptr, &symp, nullptr, 1, noside);
801 callee = value_of_variable (symp, get_selected_block (0));
804 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
805 nullptr, expect_type);
808 value *
809 structop_member_base::evaluate_funcall (struct type *expect_type,
810 struct expression *exp,
811 enum noside noside,
812 const std::vector<operation_up> &args)
814 /* First, evaluate the structure into lhs. */
815 value *lhs;
816 if (opcode () == STRUCTOP_MEMBER)
817 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
818 else
819 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
821 std::vector<value *> vals (args.size () + 1);
822 gdb::array_view<value *> val_view = vals;
823 /* If the function is a virtual function, then the aggregate
824 value (providing the structure) plays its part by providing
825 the vtable. Otherwise, it is just along for the ride: call
826 the function directly. */
827 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
828 value *callee;
830 type *a1_type = check_typedef (rhs->type ());
831 if (a1_type->code () == TYPE_CODE_METHODPTR)
833 if (noside == EVAL_AVOID_SIDE_EFFECTS)
834 callee = value::zero (a1_type->target_type (), not_lval);
835 else
836 callee = cplus_method_ptr_to_value (&lhs, rhs);
838 vals[0] = lhs;
840 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
842 struct type *type_ptr
843 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
844 struct type *target_type_ptr
845 = lookup_pointer_type (a1_type->target_type ());
847 /* Now, convert this value to an address. */
848 lhs = value_cast (type_ptr, lhs);
850 long mem_offset = value_as_long (rhs);
852 callee = value_from_pointer (target_type_ptr,
853 value_as_long (lhs) + mem_offset);
854 callee = value_ind (callee);
856 val_view = val_view.slice (1);
858 else
859 error (_("Non-pointer-to-member value used in pointer-to-member "
860 "construct"));
862 for (int i = 0; i < args.size (); ++i)
863 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
865 return evaluate_subexp_do_call (exp, noside, callee, val_view,
866 nullptr, expect_type);
870 value *
871 structop_base_operation::evaluate_funcall
872 (struct type *expect_type, struct expression *exp, enum noside noside,
873 const std::vector<operation_up> &args)
875 /* Allocate space for the function call arguments, Including space for a
876 `this' pointer at the start. */
877 std::vector<value *> vals (args.size () + 1);
878 /* First, evaluate the structure into vals[0]. */
879 enum exp_opcode op = opcode ();
880 if (op == STRUCTOP_STRUCT)
882 /* If v is a variable in a register, and the user types
883 v.method (), this will produce an error, because v has no
884 address.
886 A possible way around this would be to allocate a copy of
887 the variable on the stack, copy in the contents, call the
888 function, and copy out the contents. I.e. convert this
889 from call by reference to call by copy-return (or
890 whatever it's called). However, this does not work
891 because it is not the same: the method being called could
892 stash a copy of the address, and then future uses through
893 that address (after the method returns) would be expected
894 to use the variable itself, not some copy of it. */
895 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
897 else
899 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
900 /* Check to see if the operator '->' has been overloaded.
901 If the operator has been overloaded replace vals[0] with the
902 value returned by the custom operator and continue
903 evaluation. */
904 while (unop_user_defined_p (op, vals[0]))
906 struct value *value = nullptr;
909 value = value_x_unop (vals[0], op, noside);
911 catch (const gdb_exception_error &except)
913 if (except.error == NOT_FOUND_ERROR)
914 break;
915 else
916 throw;
919 vals[0] = value;
923 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
924 pointer we placed into vals[0]. */
925 for (int i = 0; i < args.size (); ++i)
926 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
928 /* The array view includes the `this' pointer. */
929 gdb::array_view<value *> arg_view (vals);
931 int static_memfuncp;
932 value *callee;
933 const char *tstr = std::get<1> (m_storage).c_str ();
934 if (overload_resolution
935 && exp->language_defn->la_language == language_cplus)
937 /* Language is C++, do some overload resolution before
938 evaluation. */
939 value *val0 = vals[0];
940 find_overload_match (arg_view, tstr, METHOD,
941 &val0, nullptr, &callee, nullptr,
942 &static_memfuncp, 0, noside);
943 vals[0] = val0;
945 else
946 /* Non-C++ case -- or no overload resolution. */
948 struct value *temp = vals[0];
950 callee = value_struct_elt (&temp, arg_view, tstr,
951 &static_memfuncp,
952 op == STRUCTOP_STRUCT
953 ? "structure" : "structure pointer");
954 /* value_struct_elt updates temp with the correct value of the
955 ``this'' pointer if necessary, so modify it to reflect any
956 ``this'' changes. */
957 vals[0] = value_from_longest (lookup_pointer_type (temp->type ()),
958 temp->address ()
959 + temp->embedded_offset ());
962 /* Take out `this' if needed. */
963 if (static_memfuncp)
964 arg_view = arg_view.slice (1);
966 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
967 nullptr, expect_type);
970 /* Helper for structop_base_operation::complete which recursively adds
971 field and method names from TYPE, a struct or union type, to the
972 OUTPUT list. PREFIX is prepended to each result. */
974 static void
975 add_struct_fields (struct type *type, completion_list &output,
976 const char *fieldname, int namelen, const char *prefix)
978 int i;
979 int computed_type_name = 0;
980 const char *type_name = NULL;
982 type = check_typedef (type);
983 for (i = 0; i < type->num_fields (); ++i)
985 if (i < TYPE_N_BASECLASSES (type))
986 add_struct_fields (TYPE_BASECLASS (type, i),
987 output, fieldname, namelen, prefix);
988 else if (type->field (i).name ())
990 if (type->field (i).name ()[0] != '\0')
992 if (! strncmp (type->field (i).name (),
993 fieldname, namelen))
994 output.emplace_back (concat (prefix, type->field (i).name (),
995 nullptr));
997 else if (type->field (i).type ()->code () == TYPE_CODE_UNION)
999 /* Recurse into anonymous unions. */
1000 add_struct_fields (type->field (i).type (),
1001 output, fieldname, namelen, prefix);
1006 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1008 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
1010 if (name && ! strncmp (name, fieldname, namelen))
1012 if (!computed_type_name)
1014 type_name = type->name ();
1015 computed_type_name = 1;
1017 /* Omit constructors from the completion list. */
1018 if (!type_name || strcmp (type_name, name))
1019 output.emplace_back (concat (prefix, name, nullptr));
1024 /* See expop.h. */
1026 bool
1027 structop_base_operation::complete (struct expression *exp,
1028 completion_tracker &tracker,
1029 const char *prefix)
1031 const std::string &fieldname = std::get<1> (m_storage);
1033 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp,
1034 EVAL_AVOID_SIDE_EFFECTS);
1035 struct type *type = lhs->type ();
1036 for (;;)
1038 type = check_typedef (type);
1039 if (!type->is_pointer_or_reference ())
1040 break;
1041 type = type->target_type ();
1044 if (type->code () == TYPE_CODE_UNION
1045 || type->code () == TYPE_CODE_STRUCT)
1047 completion_list result;
1049 add_struct_fields (type, result, fieldname.c_str (),
1050 fieldname.length (), prefix);
1051 tracker.add_completions (std::move (result));
1052 return true;
1055 return false;
1058 } /* namespace expr */
1060 /* Return true if type is integral or reference to integral */
1062 static bool
1063 is_integral_or_integral_reference (struct type *type)
1065 if (is_integral_type (type))
1066 return true;
1068 type = check_typedef (type);
1069 return (type != nullptr
1070 && TYPE_IS_REFERENCE (type)
1071 && is_integral_type (type->target_type ()));
1074 /* Helper function that implements the body of OP_SCOPE. */
1076 struct value *
1077 eval_op_scope (struct type *expect_type, struct expression *exp,
1078 enum noside noside,
1079 struct type *type, const char *string)
1081 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
1082 0, noside);
1083 if (arg1 == NULL)
1084 error (_("There is no field named %s"), string);
1085 return arg1;
1088 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1090 struct value *
1091 eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1092 enum noside noside, symbol *sym)
1094 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1095 return value::zero (sym->type (), not_lval);
1097 const symbol_computed_ops *computed_ops = sym->computed_ops ();
1098 if (computed_ops == nullptr
1099 || computed_ops->read_variable_at_entry == nullptr)
1100 error (_("Symbol \"%s\" does not have any specific entry value"),
1101 sym->print_name ());
1103 frame_info_ptr frame = get_selected_frame (NULL);
1104 return computed_ops->read_variable_at_entry (sym, frame);
1107 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1109 struct value *
1110 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1111 enum noside noside, bool outermost_p,
1112 bound_minimal_symbol msymbol)
1114 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1115 msymbol.minsym);
1117 struct type *type = val->type ();
1118 if (type->code () == TYPE_CODE_ERROR
1119 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1120 error_unknown_type (msymbol.minsym->print_name ());
1121 return val;
1124 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1126 struct value *
1127 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1128 enum noside noside,
1129 value *func, const char *var)
1131 CORE_ADDR addr = func->address ();
1132 const block *blk = block_for_pc (addr);
1133 struct block_symbol sym = lookup_symbol (var, blk, SEARCH_VAR_DOMAIN,
1134 nullptr);
1135 if (sym.symbol == NULL)
1136 error (_("No symbol \"%s\" in specified context."), var);
1137 return evaluate_var_value (noside, sym.block, sym.symbol);
1140 /* Helper function that implements the body of OP_REGISTER. */
1142 struct value *
1143 eval_op_register (struct type *expect_type, struct expression *exp,
1144 enum noside noside, const char *name)
1146 int regno;
1147 struct value *val;
1149 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1150 name, strlen (name));
1151 if (regno == -1)
1152 error (_("Register $%s not available."), name);
1154 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1155 a value with the appropriate register type. Unfortunately,
1156 we don't have easy access to the type of user registers.
1157 So for these registers, we fetch the register value regardless
1158 of the evaluation mode. */
1159 if (noside == EVAL_AVOID_SIDE_EFFECTS
1160 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1161 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
1162 else
1163 val = value_of_register
1164 (regno, get_next_frame_sentinel_okay (get_selected_frame ()));
1165 if (val == NULL)
1166 error (_("Value of register %s not available."), name);
1167 else
1168 return val;
1171 namespace expr
1174 value *
1175 string_operation::evaluate (struct type *expect_type,
1176 struct expression *exp,
1177 enum noside noside)
1179 const std::string &str = std::get<0> (m_storage);
1180 struct type *type = language_string_char_type (exp->language_defn,
1181 exp->gdbarch);
1182 return value_string (str.c_str (), str.size (), type);
1185 struct value *
1186 ternop_slice_operation::evaluate (struct type *expect_type,
1187 struct expression *exp,
1188 enum noside noside)
1190 struct value *array
1191 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1192 struct value *low
1193 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1194 struct value *upper
1195 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1197 int lowbound = value_as_long (low);
1198 int upperbound = value_as_long (upper);
1199 return value_slice (array, lowbound, upperbound - lowbound + 1);
1202 } /* namespace expr */
1204 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1206 struct value *
1207 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1208 enum noside noside,
1209 const char *sel)
1211 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1212 return value_from_longest (selector_type,
1213 lookup_child_selector (exp->gdbarch, sel));
1216 /* A helper function for STRUCTOP_STRUCT. */
1218 struct value *
1219 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1220 enum noside noside,
1221 struct value *arg1, const char *string)
1223 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1224 NULL, "structure");
1225 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1226 arg3 = value::zero (arg3->type (), arg3->lval ());
1227 return arg3;
1230 /* A helper function for STRUCTOP_PTR. */
1232 struct value *
1233 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1234 enum noside noside,
1235 struct value *arg1, const char *string)
1237 /* Check to see if operator '->' has been overloaded. If so replace
1238 arg1 with the value returned by evaluating operator->(). */
1239 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1241 struct value *value = NULL;
1244 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1247 catch (const gdb_exception_error &except)
1249 if (except.error == NOT_FOUND_ERROR)
1250 break;
1251 else
1252 throw;
1255 arg1 = value;
1258 /* JYG: if print object is on we need to replace the base type
1259 with rtti type in order to continue on with successful
1260 lookup of member / method only available in the rtti type. */
1262 struct type *arg_type = arg1->type ();
1263 struct type *real_type;
1264 int full, using_enc;
1265 LONGEST top;
1266 struct value_print_options opts;
1268 get_user_print_options (&opts);
1269 if (opts.objectprint && arg_type->target_type ()
1270 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
1272 real_type = value_rtti_indirect_type (arg1, &full, &top,
1273 &using_enc);
1274 if (real_type)
1275 arg1 = value_cast (real_type, arg1);
1279 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1280 NULL, "structure pointer");
1281 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1282 arg3 = value::zero (arg3->type (), arg3->lval ());
1283 return arg3;
1286 /* A helper function for STRUCTOP_MEMBER. */
1288 struct value *
1289 eval_op_member (struct type *expect_type, struct expression *exp,
1290 enum noside noside,
1291 struct value *arg1, struct value *arg2)
1293 long mem_offset;
1295 struct value *arg3;
1296 struct type *type = check_typedef (arg2->type ());
1297 switch (type->code ())
1299 case TYPE_CODE_METHODPTR:
1300 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1301 return value::zero (type->target_type (), not_lval);
1302 else
1304 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1305 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
1306 return value_ind (arg2);
1309 case TYPE_CODE_MEMBERPTR:
1310 /* Now, convert these values to an address. */
1311 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
1312 arg1 = value_addr (arg1);
1313 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1314 arg1, 1);
1316 mem_offset = value_as_long (arg2);
1318 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
1319 value_as_long (arg1) + mem_offset);
1320 return value_ind (arg3);
1322 default:
1323 error (_("non-pointer-to-member value used "
1324 "in pointer-to-member construct"));
1328 /* A helper function for BINOP_ADD. */
1330 struct value *
1331 eval_op_add (struct type *expect_type, struct expression *exp,
1332 enum noside noside,
1333 struct value *arg1, struct value *arg2)
1335 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1336 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1337 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1338 && is_integral_or_integral_reference (arg2->type ()))
1339 return value_ptradd (arg1, value_as_long (arg2));
1340 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1341 && is_integral_or_integral_reference (arg1->type ()))
1342 return value_ptradd (arg2, value_as_long (arg1));
1343 else
1345 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1346 return value_binop (arg1, arg2, BINOP_ADD);
1350 /* A helper function for BINOP_SUB. */
1352 struct value *
1353 eval_op_sub (struct type *expect_type, struct expression *exp,
1354 enum noside noside,
1355 struct value *arg1, struct value *arg2)
1357 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1358 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1359 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1360 && ptrmath_type_p (exp->language_defn, arg2->type ()))
1362 /* FIXME -- should be ptrdiff_t */
1363 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1364 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1366 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1367 && is_integral_or_integral_reference (arg2->type ()))
1368 return value_ptradd (arg1, - value_as_long (arg2));
1369 else
1371 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1372 return value_binop (arg1, arg2, BINOP_SUB);
1376 /* Helper function for several different binary operations. */
1378 struct value *
1379 eval_op_binary (struct type *expect_type, struct expression *exp,
1380 enum noside noside, enum exp_opcode op,
1381 struct value *arg1, struct value *arg2)
1383 if (binop_user_defined_p (op, arg1, arg2))
1384 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1385 else
1387 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1388 fudge arg2 to avoid division-by-zero, the caller is
1389 (theoretically) only looking for the type of the result. */
1390 if (noside == EVAL_AVOID_SIDE_EFFECTS
1391 /* ??? Do we really want to test for BINOP_MOD here?
1392 The implementation of value_binop gives it a well-defined
1393 value. */
1394 && (op == BINOP_DIV
1395 || op == BINOP_INTDIV
1396 || op == BINOP_REM
1397 || op == BINOP_MOD)
1398 && value_logical_not (arg2))
1400 struct value *v_one;
1402 v_one = value_one (arg2->type ());
1403 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1404 return value_binop (arg1, v_one, op);
1406 else
1408 /* For shift and integer exponentiation operations,
1409 only promote the first argument. */
1410 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1411 && is_integral_type (arg2->type ()))
1412 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1413 else
1414 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1416 return value_binop (arg1, arg2, op);
1421 /* A helper function for BINOP_SUBSCRIPT. */
1423 struct value *
1424 eval_op_subscript (struct type *expect_type, struct expression *exp,
1425 enum noside noside, enum exp_opcode op,
1426 struct value *arg1, struct value *arg2)
1428 if (binop_user_defined_p (op, arg1, arg2))
1429 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1430 else
1432 /* If the user attempts to subscript something that is not an
1433 array or pointer type (like a plain int variable for example),
1434 then report this as an error. */
1436 arg1 = coerce_ref (arg1);
1437 struct type *type = check_typedef (arg1->type ());
1438 if (type->code () != TYPE_CODE_ARRAY
1439 && type->code () != TYPE_CODE_PTR)
1441 if (type->name ())
1442 error (_("cannot subscript something of type `%s'"),
1443 type->name ());
1444 else
1445 error (_("cannot subscript requested type"));
1448 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1449 return value::zero (type->target_type (), arg1->lval ());
1450 else
1451 return value_subscript (arg1, value_as_long (arg2));
1455 /* A helper function for BINOP_EQUAL. */
1457 struct value *
1458 eval_op_equal (struct type *expect_type, struct expression *exp,
1459 enum noside noside, enum exp_opcode op,
1460 struct value *arg1, struct value *arg2)
1462 if (binop_user_defined_p (op, arg1, arg2))
1464 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1466 else
1468 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1469 int tem = value_equal (arg1, arg2);
1470 struct type *type = language_bool_type (exp->language_defn,
1471 exp->gdbarch);
1472 return value_from_longest (type, (LONGEST) tem);
1476 /* A helper function for BINOP_NOTEQUAL. */
1478 struct value *
1479 eval_op_notequal (struct type *expect_type, struct expression *exp,
1480 enum noside noside, enum exp_opcode op,
1481 struct value *arg1, struct value *arg2)
1483 if (binop_user_defined_p (op, arg1, arg2))
1485 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1487 else
1489 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1490 int tem = value_equal (arg1, arg2);
1491 struct type *type = language_bool_type (exp->language_defn,
1492 exp->gdbarch);
1493 return value_from_longest (type, (LONGEST) ! tem);
1497 /* A helper function for BINOP_LESS. */
1499 struct value *
1500 eval_op_less (struct type *expect_type, struct expression *exp,
1501 enum noside noside, enum exp_opcode op,
1502 struct value *arg1, struct value *arg2)
1504 if (binop_user_defined_p (op, arg1, arg2))
1506 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1508 else
1510 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1511 int tem = value_less (arg1, arg2);
1512 struct type *type = language_bool_type (exp->language_defn,
1513 exp->gdbarch);
1514 return value_from_longest (type, (LONGEST) tem);
1518 /* A helper function for BINOP_GTR. */
1520 struct value *
1521 eval_op_gtr (struct type *expect_type, struct expression *exp,
1522 enum noside noside, enum exp_opcode op,
1523 struct value *arg1, struct value *arg2)
1525 if (binop_user_defined_p (op, arg1, arg2))
1527 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1529 else
1531 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1532 int tem = value_less (arg2, arg1);
1533 struct type *type = language_bool_type (exp->language_defn,
1534 exp->gdbarch);
1535 return value_from_longest (type, (LONGEST) tem);
1539 /* A helper function for BINOP_GEQ. */
1541 struct value *
1542 eval_op_geq (struct type *expect_type, struct expression *exp,
1543 enum noside noside, enum exp_opcode op,
1544 struct value *arg1, struct value *arg2)
1546 if (binop_user_defined_p (op, arg1, arg2))
1548 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1550 else
1552 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1553 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1554 struct type *type = language_bool_type (exp->language_defn,
1555 exp->gdbarch);
1556 return value_from_longest (type, (LONGEST) tem);
1560 /* A helper function for BINOP_LEQ. */
1562 struct value *
1563 eval_op_leq (struct type *expect_type, struct expression *exp,
1564 enum noside noside, enum exp_opcode op,
1565 struct value *arg1, struct value *arg2)
1567 if (binop_user_defined_p (op, arg1, arg2))
1569 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1571 else
1573 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1574 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1575 struct type *type = language_bool_type (exp->language_defn,
1576 exp->gdbarch);
1577 return value_from_longest (type, (LONGEST) tem);
1581 /* A helper function for BINOP_REPEAT. */
1583 struct value *
1584 eval_op_repeat (struct type *expect_type, struct expression *exp,
1585 enum noside noside, enum exp_opcode op,
1586 struct value *arg1, struct value *arg2)
1588 struct type *type = check_typedef (arg2->type ());
1589 if (type->code () != TYPE_CODE_INT
1590 && type->code () != TYPE_CODE_ENUM)
1591 error (_("Non-integral right operand for \"@\" operator."));
1592 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1594 return allocate_repeat_value (arg1->type (),
1595 longest_to_int (value_as_long (arg2)));
1597 else
1598 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1601 /* A helper function for UNOP_PLUS. */
1603 struct value *
1604 eval_op_plus (struct type *expect_type, struct expression *exp,
1605 enum noside noside, enum exp_opcode op,
1606 struct value *arg1)
1608 if (unop_user_defined_p (op, arg1))
1609 return value_x_unop (arg1, op, noside);
1610 else
1612 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1613 return value_pos (arg1);
1617 /* A helper function for UNOP_NEG. */
1619 struct value *
1620 eval_op_neg (struct type *expect_type, struct expression *exp,
1621 enum noside noside, enum exp_opcode op,
1622 struct value *arg1)
1624 if (unop_user_defined_p (op, arg1))
1625 return value_x_unop (arg1, op, noside);
1626 else
1628 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1629 return value_neg (arg1);
1633 /* A helper function for UNOP_COMPLEMENT. */
1635 struct value *
1636 eval_op_complement (struct type *expect_type, struct expression *exp,
1637 enum noside noside, enum exp_opcode op,
1638 struct value *arg1)
1640 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1641 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1642 else
1644 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1645 return value_complement (arg1);
1649 /* A helper function for UNOP_LOGICAL_NOT. */
1651 struct value *
1652 eval_op_lognot (struct type *expect_type, struct expression *exp,
1653 enum noside noside, enum exp_opcode op,
1654 struct value *arg1)
1656 if (unop_user_defined_p (op, arg1))
1657 return value_x_unop (arg1, op, noside);
1658 else
1660 struct type *type = language_bool_type (exp->language_defn,
1661 exp->gdbarch);
1662 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1666 /* A helper function for UNOP_IND. */
1668 struct value *
1669 eval_op_ind (struct type *expect_type, struct expression *exp,
1670 enum noside noside,
1671 struct value *arg1)
1673 struct type *type = check_typedef (arg1->type ());
1674 if (type->code () == TYPE_CODE_METHODPTR
1675 || type->code () == TYPE_CODE_MEMBERPTR)
1676 error (_("Attempt to dereference pointer "
1677 "to member without an object"));
1678 if (unop_user_defined_p (UNOP_IND, arg1))
1679 return value_x_unop (arg1, UNOP_IND, noside);
1680 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1682 type = check_typedef (arg1->type ());
1684 /* If the type pointed to is dynamic then in order to resolve the
1685 dynamic properties we must actually dereference the pointer.
1686 There is a risk that this dereference will have side-effects
1687 in the inferior, but being able to print accurate type
1688 information seems worth the risk. */
1689 if (!type->is_pointer_or_reference ()
1690 || !is_dynamic_type (type->target_type ()))
1692 if (type->is_pointer_or_reference ()
1693 /* In C you can dereference an array to get the 1st elt. */
1694 || type->code () == TYPE_CODE_ARRAY)
1695 return value::zero (type->target_type (),
1696 lval_memory);
1697 else if (type->code () == TYPE_CODE_INT)
1698 /* GDB allows dereferencing an int. */
1699 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
1700 lval_memory);
1701 else
1702 error (_("Attempt to take contents of a non-pointer value."));
1706 /* Allow * on an integer so we can cast it to whatever we want.
1707 This returns an int, which seems like the most C-like thing to
1708 do. "long long" variables are rare enough that
1709 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1710 if (type->code () == TYPE_CODE_INT)
1711 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1712 value_as_address (arg1));
1713 return value_ind (arg1);
1716 /* A helper function for UNOP_ALIGNOF. */
1718 struct value *
1719 eval_op_alignof (struct type *expect_type, struct expression *exp,
1720 enum noside noside,
1721 struct value *arg1)
1723 struct type *type = arg1->type ();
1724 /* FIXME: This should be size_t. */
1725 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1726 ULONGEST align = type_align (type);
1727 if (align == 0)
1728 error (_("could not determine alignment of type"));
1729 return value_from_longest (size_type, align);
1732 /* A helper function for UNOP_MEMVAL. */
1734 struct value *
1735 eval_op_memval (struct type *expect_type, struct expression *exp,
1736 enum noside noside,
1737 struct value *arg1, struct type *type)
1739 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1740 return value::zero (type, lval_memory);
1741 else
1742 return value_at_lazy (type, value_as_address (arg1));
1745 /* A helper function for UNOP_PREINCREMENT. */
1747 struct value *
1748 eval_op_preinc (struct type *expect_type, struct expression *exp,
1749 enum noside noside, enum exp_opcode op,
1750 struct value *arg1)
1752 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1753 return arg1;
1754 else if (unop_user_defined_p (op, arg1))
1756 return value_x_unop (arg1, op, noside);
1758 else
1760 struct value *arg2;
1761 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1762 arg2 = value_ptradd (arg1, 1);
1763 else
1765 struct value *tmp = arg1;
1767 arg2 = value_one (arg1->type ());
1768 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1769 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1772 return value_assign (arg1, arg2);
1776 /* A helper function for UNOP_PREDECREMENT. */
1778 struct value *
1779 eval_op_predec (struct type *expect_type, struct expression *exp,
1780 enum noside noside, enum exp_opcode op,
1781 struct value *arg1)
1783 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1784 return arg1;
1785 else if (unop_user_defined_p (op, arg1))
1787 return value_x_unop (arg1, op, noside);
1789 else
1791 struct value *arg2;
1792 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1793 arg2 = value_ptradd (arg1, -1);
1794 else
1796 struct value *tmp = arg1;
1798 arg2 = value_one (arg1->type ());
1799 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1800 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1803 return value_assign (arg1, arg2);
1807 /* A helper function for UNOP_POSTINCREMENT. */
1809 struct value *
1810 eval_op_postinc (struct type *expect_type, struct expression *exp,
1811 enum noside noside, enum exp_opcode op,
1812 struct value *arg1)
1814 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1815 return arg1;
1816 else if (unop_user_defined_p (op, arg1))
1818 return value_x_unop (arg1, op, noside);
1820 else
1822 struct value *arg3 = arg1->non_lval ();
1823 struct value *arg2;
1825 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1826 arg2 = value_ptradd (arg1, 1);
1827 else
1829 struct value *tmp = arg1;
1831 arg2 = value_one (arg1->type ());
1832 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1833 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1836 value_assign (arg1, arg2);
1837 return arg3;
1841 /* A helper function for UNOP_POSTDECREMENT. */
1843 struct value *
1844 eval_op_postdec (struct type *expect_type, struct expression *exp,
1845 enum noside noside, enum exp_opcode op,
1846 struct value *arg1)
1848 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1849 return arg1;
1850 else if (unop_user_defined_p (op, arg1))
1852 return value_x_unop (arg1, op, noside);
1854 else
1856 struct value *arg3 = arg1->non_lval ();
1857 struct value *arg2;
1859 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1860 arg2 = value_ptradd (arg1, -1);
1861 else
1863 struct value *tmp = arg1;
1865 arg2 = value_one (arg1->type ());
1866 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1867 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1870 value_assign (arg1, arg2);
1871 return arg3;
1875 namespace expr
1878 struct value *
1879 type_operation::evaluate (struct type *expect_type, struct expression *exp,
1880 enum noside noside)
1882 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1883 return value::allocate (std::get<0> (m_storage));
1884 else
1885 error (_("Attempt to use a type name as an expression"));
1890 /* A helper function for BINOP_ASSIGN_MODIFY. */
1892 struct value *
1893 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1894 enum noside noside, enum exp_opcode op,
1895 struct value *arg1, struct value *arg2)
1897 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1898 return arg1;
1899 if (binop_user_defined_p (op, arg1, arg2))
1900 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1901 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1902 arg1->type ())
1903 && is_integral_type (arg2->type ()))
1904 arg2 = value_ptradd (arg1, value_as_long (arg2));
1905 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1906 arg1->type ())
1907 && is_integral_type (arg2->type ()))
1908 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1909 else
1911 struct value *tmp = arg1;
1913 /* For shift and integer exponentiation operations,
1914 only promote the first argument. */
1915 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1916 && is_integral_type (arg2->type ()))
1917 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1918 else
1919 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1921 arg2 = value_binop (tmp, arg2, op);
1923 return value_assign (arg1, arg2);
1926 /* Note that ARGS needs 2 empty slots up front and must end with a
1927 null pointer. */
1928 static struct value *
1929 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1930 enum noside noside, CORE_ADDR selector,
1931 value *target, gdb::array_view<value *> args)
1933 CORE_ADDR responds_selector = 0;
1934 CORE_ADDR method_selector = 0;
1936 int struct_return = 0;
1938 struct value *msg_send = NULL;
1939 struct value *msg_send_stret = NULL;
1940 int gnu_runtime = 0;
1942 struct value *method = NULL;
1943 struct value *called_method = NULL;
1945 struct type *selector_type = NULL;
1946 struct type *long_type;
1947 struct type *type;
1949 struct value *ret = NULL;
1950 CORE_ADDR addr = 0;
1952 value *argvec[5];
1954 long_type = builtin_type (exp->gdbarch)->builtin_long;
1955 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1957 if (value_as_long (target) == 0)
1958 return value_from_longest (long_type, 0);
1960 if (lookup_minimal_symbol (current_program_space, "objc_msg_lookup").minsym
1961 != nullptr)
1962 gnu_runtime = 1;
1964 /* Find the method dispatch (Apple runtime) or method lookup
1965 (GNU runtime) function for Objective-C. These will be used
1966 to lookup the symbol information for the method. If we
1967 can't find any symbol information, then we'll use these to
1968 call the method, otherwise we can call the method
1969 directly. The msg_send_stret function is used in the special
1970 case of a method that returns a structure (Apple runtime
1971 only). */
1972 if (gnu_runtime)
1974 type = selector_type;
1976 type = lookup_function_type (type);
1977 type = lookup_pointer_type (type);
1978 type = lookup_function_type (type);
1979 type = lookup_pointer_type (type);
1981 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1982 msg_send_stret
1983 = find_function_in_inferior ("objc_msg_lookup", NULL);
1985 msg_send = value_from_pointer (type, value_as_address (msg_send));
1986 msg_send_stret = value_from_pointer (type,
1987 value_as_address (msg_send_stret));
1989 else
1991 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1992 /* Special dispatcher for methods returning structs. */
1993 msg_send_stret
1994 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1997 /* Verify the target object responds to this method. The
1998 standard top-level 'Object' class uses a different name for
1999 the verification method than the non-standard, but more
2000 often used, 'NSObject' class. Make sure we check for both. */
2002 responds_selector
2003 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
2004 if (responds_selector == 0)
2005 responds_selector
2006 = lookup_child_selector (exp->gdbarch, "respondsTo:");
2008 if (responds_selector == 0)
2009 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
2011 method_selector
2012 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
2013 if (method_selector == 0)
2014 method_selector
2015 = lookup_child_selector (exp->gdbarch, "methodFor:");
2017 if (method_selector == 0)
2018 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2020 /* Call the verification method, to make sure that the target
2021 class implements the desired method. */
2023 argvec[0] = msg_send;
2024 argvec[1] = target;
2025 argvec[2] = value_from_longest (long_type, responds_selector);
2026 argvec[3] = value_from_longest (long_type, selector);
2027 argvec[4] = 0;
2029 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2030 if (gnu_runtime)
2032 /* Function objc_msg_lookup returns a pointer. */
2033 argvec[0] = ret;
2034 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2036 if (value_as_long (ret) == 0)
2037 error (_("Target does not respond to this message selector."));
2039 /* Call "methodForSelector:" method, to get the address of a
2040 function method that implements this selector for this
2041 class. If we can find a symbol at that address, then we
2042 know the return type, parameter types etc. (that's a good
2043 thing). */
2045 argvec[0] = msg_send;
2046 argvec[1] = target;
2047 argvec[2] = value_from_longest (long_type, method_selector);
2048 argvec[3] = value_from_longest (long_type, selector);
2049 argvec[4] = 0;
2051 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2052 if (gnu_runtime)
2054 argvec[0] = ret;
2055 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2058 /* ret should now be the selector. */
2060 addr = value_as_long (ret);
2061 if (addr)
2063 struct symbol *sym = NULL;
2065 /* The address might point to a function descriptor;
2066 resolve it to the actual code address instead. */
2067 addr = gdbarch_convert_from_func_ptr_addr
2068 (exp->gdbarch, addr, current_inferior ()->top_target ());
2070 /* Is it a high_level symbol? */
2071 sym = find_pc_function (addr);
2072 if (sym != NULL)
2073 method = value_of_variable (sym, 0);
2076 /* If we found a method with symbol information, check to see
2077 if it returns a struct. Otherwise assume it doesn't. */
2079 if (method)
2081 CORE_ADDR funaddr;
2082 struct type *val_type;
2084 funaddr = find_function_addr (method, &val_type);
2086 block_for_pc (funaddr);
2088 val_type = check_typedef (val_type);
2090 if ((val_type == NULL)
2091 || (val_type->code () == TYPE_CODE_ERROR))
2093 if (expect_type != NULL)
2094 val_type = expect_type;
2097 struct_return = using_struct_return (exp->gdbarch, method,
2098 val_type);
2100 else if (expect_type != NULL)
2102 struct_return = using_struct_return (exp->gdbarch, NULL,
2103 check_typedef (expect_type));
2106 /* Found a function symbol. Now we will substitute its
2107 value in place of the message dispatcher (obj_msgSend),
2108 so that we call the method directly instead of through
2109 the dispatcher. The main reason for doing this is that
2110 we can now evaluate the return value and parameter values
2111 according to their known data types, in case we need to
2112 do things like promotion, dereferencing, special handling
2113 of structs and doubles, etc.
2115 We want to use the type signature of 'method', but still
2116 jump to objc_msgSend() or objc_msgSend_stret() to better
2117 mimic the behavior of the runtime. */
2119 if (method)
2121 if (method->type ()->code () != TYPE_CODE_FUNC)
2122 error (_("method address has symbol information "
2123 "with non-function type; skipping"));
2125 /* Create a function pointer of the appropriate type, and
2126 replace its value with the value of msg_send or
2127 msg_send_stret. We must use a pointer here, as
2128 msg_send and msg_send_stret are of pointer type, and
2129 the representation may be different on systems that use
2130 function descriptors. */
2131 if (struct_return)
2132 called_method
2133 = value_from_pointer (lookup_pointer_type (method->type ()),
2134 value_as_address (msg_send_stret));
2135 else
2136 called_method
2137 = value_from_pointer (lookup_pointer_type (method->type ()),
2138 value_as_address (msg_send));
2140 else
2142 if (struct_return)
2143 called_method = msg_send_stret;
2144 else
2145 called_method = msg_send;
2149 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2151 /* If the return type doesn't look like a function type,
2152 call an error. This can happen if somebody tries to
2153 turn a variable into a function call. This is here
2154 because people often want to call, eg, strcmp, which
2155 gdb doesn't know is a function. If gdb isn't asked for
2156 it's opinion (ie. through "whatis"), it won't offer
2157 it. */
2159 struct type *callee_type = called_method->type ();
2161 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2162 callee_type = callee_type->target_type ();
2163 callee_type = callee_type->target_type ();
2165 if (callee_type)
2167 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2168 return value::allocate (expect_type);
2169 else
2170 return value::allocate (callee_type);
2172 else
2173 error (_("Expression of type other than "
2174 "\"method returning ...\" used as a method"));
2177 /* Now depending on whether we found a symbol for the method,
2178 we will either call the runtime dispatcher or the method
2179 directly. */
2181 args[0] = target;
2182 args[1] = value_from_longest (long_type, selector);
2184 if (gnu_runtime && (method != NULL))
2186 /* Function objc_msg_lookup returns a pointer. */
2187 struct type *tem_type = called_method->type ();
2188 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2189 called_method->deprecated_set_type (tem_type);
2190 called_method = call_function_by_hand (called_method, NULL, args);
2193 return call_function_by_hand (called_method, NULL, args);
2196 /* Helper function for MULTI_SUBSCRIPT. */
2198 static struct value *
2199 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2200 enum noside noside, value *arg1,
2201 gdb::array_view<value *> args)
2203 for (value *arg2 : args)
2205 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2207 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2209 else
2211 arg1 = coerce_ref (arg1);
2212 struct type *type = check_typedef (arg1->type ());
2214 switch (type->code ())
2216 case TYPE_CODE_PTR:
2217 case TYPE_CODE_ARRAY:
2218 case TYPE_CODE_STRING:
2219 arg1 = value_subscript (arg1, value_as_long (arg2));
2220 break;
2222 default:
2223 if (type->name ())
2224 error (_("cannot subscript something of type `%s'"),
2225 type->name ());
2226 else
2227 error (_("cannot subscript requested type"));
2231 return (arg1);
2234 namespace expr
2237 value *
2238 objc_msgcall_operation::evaluate (struct type *expect_type,
2239 struct expression *exp,
2240 enum noside noside)
2242 enum noside sub_no_side = EVAL_NORMAL;
2243 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2245 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2246 sub_no_side = EVAL_NORMAL;
2247 else
2248 sub_no_side = noside;
2249 value *target
2250 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2252 if (value_as_long (target) == 0)
2253 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2254 else
2255 sub_no_side = noside;
2256 std::vector<operation_up> &args = std::get<2> (m_storage);
2257 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2258 argvec[0] = nullptr;
2259 argvec[1] = nullptr;
2260 for (int i = 0; i < args.size (); ++i)
2261 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2262 argvec[args.size () + 2] = nullptr;
2264 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2265 get<0> (m_storage), target,
2266 gdb::make_array_view (argvec,
2267 args.size () + 3));
2270 value *
2271 multi_subscript_operation::evaluate (struct type *expect_type,
2272 struct expression *exp,
2273 enum noside noside)
2275 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2276 std::vector<operation_up> &values = std::get<1> (m_storage);
2277 value **argvec = XALLOCAVEC (struct value *, values.size ());
2278 for (int ix = 0; ix < values.size (); ++ix)
2279 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2280 return eval_multi_subscript (expect_type, exp, noside, arg1,
2281 gdb::make_array_view (argvec, values.size ()));
2284 value *
2285 logical_and_operation::evaluate (struct type *expect_type,
2286 struct expression *exp,
2287 enum noside noside)
2289 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2291 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2292 EVAL_AVOID_SIDE_EFFECTS);
2294 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2296 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2297 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2299 else
2301 bool tem = value_logical_not (arg1);
2302 if (!tem)
2304 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2305 tem = value_logical_not (arg2);
2307 struct type *type = language_bool_type (exp->language_defn,
2308 exp->gdbarch);
2309 return value_from_longest (type, !tem);
2313 value *
2314 logical_or_operation::evaluate (struct type *expect_type,
2315 struct expression *exp,
2316 enum noside noside)
2318 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2320 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2321 EVAL_AVOID_SIDE_EFFECTS);
2323 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2325 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2326 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2328 else
2330 bool tem = value_logical_not (arg1);
2331 if (tem)
2333 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2334 tem = value_logical_not (arg2);
2337 struct type *type = language_bool_type (exp->language_defn,
2338 exp->gdbarch);
2339 return value_from_longest (type, !tem);
2343 value *
2344 adl_func_operation::evaluate (struct type *expect_type,
2345 struct expression *exp,
2346 enum noside noside)
2348 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2349 std::vector<value *> args (arg_ops.size ());
2350 for (int i = 0; i < arg_ops.size (); ++i)
2351 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2353 struct symbol *symp;
2354 find_overload_match (args, std::get<0> (m_storage).c_str (),
2355 NON_METHOD,
2356 nullptr, nullptr,
2357 nullptr, &symp, nullptr, 0, noside);
2358 if (symp->type ()->code () == TYPE_CODE_ERROR)
2359 error_unknown_type (symp->print_name ());
2360 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2361 return evaluate_subexp_do_call (exp, noside, callee, args,
2362 nullptr, expect_type);
2366 /* This function evaluates brace-initializers (in C/C++) for
2367 structure types. */
2369 struct value *
2370 array_operation::evaluate_struct_tuple (struct value *struct_val,
2371 struct expression *exp,
2372 enum noside noside, int nargs)
2374 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2375 struct type *struct_type = check_typedef (struct_val->type ());
2376 struct type *field_type;
2377 int fieldno = -1;
2379 int idx = 0;
2380 while (--nargs >= 0)
2382 struct value *val = NULL;
2383 int bitpos, bitsize;
2384 bfd_byte *addr;
2386 fieldno++;
2387 /* Skip static fields. */
2388 while (fieldno < struct_type->num_fields ()
2389 && struct_type->field (fieldno).is_static ())
2390 fieldno++;
2391 if (fieldno >= struct_type->num_fields ())
2392 error (_("too many initializers"));
2393 field_type = struct_type->field (fieldno).type ();
2394 if (field_type->code () == TYPE_CODE_UNION
2395 && struct_type->field (fieldno).name ()[0] == '0')
2396 error (_("don't know which variant you want to set"));
2398 /* Here, struct_type is the type of the inner struct,
2399 while substruct_type is the type of the inner struct.
2400 These are the same for normal structures, but a variant struct
2401 contains anonymous union fields that contain substruct fields.
2402 The value fieldno is the index of the top-level (normal or
2403 anonymous union) field in struct_field, while the value
2404 subfieldno is the index of the actual real (named inner) field
2405 in substruct_type. */
2407 field_type = struct_type->field (fieldno).type ();
2408 if (val == 0)
2409 val = in_args[idx++]->evaluate (field_type, exp, noside);
2411 /* Now actually set the field in struct_val. */
2413 /* Assign val to field fieldno. */
2414 if (val->type () != field_type)
2415 val = value_cast (field_type, val);
2417 bitsize = struct_type->field (fieldno).bitsize ();
2418 bitpos = struct_type->field (fieldno).loc_bitpos ();
2419 addr = struct_val->contents_writeable ().data () + bitpos / 8;
2420 if (bitsize)
2421 modify_field (struct_type, addr,
2422 value_as_long (val), bitpos % 8, bitsize);
2423 else
2424 memcpy (addr, val->contents ().data (),
2425 val->type ()->length ());
2428 return struct_val;
2431 value *
2432 array_operation::evaluate (struct type *expect_type,
2433 struct expression *exp,
2434 enum noside noside)
2436 const int provided_low_bound = std::get<0> (m_storage);
2437 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2438 const int nargs = std::get<1> (m_storage) - provided_low_bound + 1;
2439 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2441 if (expect_type != nullptr
2442 && type->code () == TYPE_CODE_STRUCT)
2444 struct value *rec = value::allocate (expect_type);
2446 memset (rec->contents_raw ().data (), '\0', type->length ());
2447 return evaluate_struct_tuple (rec, exp, noside, nargs);
2450 if (expect_type != nullptr
2451 && type->code () == TYPE_CODE_ARRAY)
2453 struct type *range_type = type->index_type ();
2454 struct type *element_type = type->target_type ();
2455 struct value *array = value::allocate (expect_type);
2456 int element_size = check_typedef (element_type)->length ();
2457 LONGEST low_bound, high_bound;
2459 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2461 low_bound = 0;
2462 high_bound = (type->length () / element_size) - 1;
2464 if (low_bound + nargs - 1 > high_bound)
2465 error (_("Too many array elements"));
2466 memset (array->contents_raw ().data (), 0, expect_type->length ());
2467 for (int idx = 0; idx < nargs; ++idx)
2469 struct value *element;
2471 element = in_args[idx]->evaluate (element_type, exp, noside);
2472 if (element->type () != element_type)
2473 element = value_cast (element_type, element);
2474 memcpy (array->contents_raw ().data () + idx * element_size,
2475 element->contents ().data (),
2476 element_size);
2478 return array;
2481 if (expect_type != nullptr
2482 && type->code () == TYPE_CODE_SET)
2484 struct value *set = value::allocate (expect_type);
2485 gdb_byte *valaddr = set->contents_raw ().data ();
2486 struct type *element_type = type->index_type ();
2487 struct type *check_type = element_type;
2488 LONGEST low_bound, high_bound;
2490 /* Get targettype of elementtype. */
2491 while (check_type->code () == TYPE_CODE_RANGE
2492 || check_type->code () == TYPE_CODE_TYPEDEF)
2493 check_type = check_type->target_type ();
2495 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2496 error (_("(power)set type with unknown size"));
2497 memset (valaddr, '\0', type->length ());
2498 for (int idx = 0; idx < nargs; idx++)
2500 LONGEST range_low, range_high;
2501 struct type *range_low_type, *range_high_type;
2502 struct value *elem_val;
2504 elem_val = in_args[idx]->evaluate (element_type, exp, noside);
2505 range_low_type = range_high_type = elem_val->type ();
2506 range_low = range_high = value_as_long (elem_val);
2508 /* Check types of elements to avoid mixture of elements from
2509 different types. Also check if type of element is "compatible"
2510 with element type of powerset. */
2511 if (range_low_type->code () == TYPE_CODE_RANGE)
2512 range_low_type = range_low_type->target_type ();
2513 if (range_high_type->code () == TYPE_CODE_RANGE)
2514 range_high_type = range_high_type->target_type ();
2515 if ((range_low_type->code () != range_high_type->code ())
2516 || (range_low_type->code () == TYPE_CODE_ENUM
2517 && (range_low_type != range_high_type)))
2518 /* different element modes. */
2519 error (_("POWERSET tuple elements of different mode"));
2520 if ((check_type->code () != range_low_type->code ())
2521 || (check_type->code () == TYPE_CODE_ENUM
2522 && range_low_type != check_type))
2523 error (_("incompatible POWERSET tuple elements"));
2524 if (range_low > range_high)
2526 warning (_("empty POWERSET tuple range"));
2527 continue;
2529 if (range_low < low_bound || range_high > high_bound)
2530 error (_("POWERSET tuple element out of range"));
2531 range_low -= low_bound;
2532 range_high -= low_bound;
2533 for (; range_low <= range_high; range_low++)
2535 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2537 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2538 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2539 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2540 |= 1 << bit_index;
2543 return set;
2546 std::vector<value *> argvec (nargs);
2547 for (int tem = 0; tem < nargs; tem++)
2549 /* Ensure that array expressions are coerced into pointer
2550 objects. */
2551 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2553 return value_array (provided_low_bound, argvec);
2556 value *
2557 unop_extract_operation::evaluate (struct type *expect_type,
2558 struct expression *exp,
2559 enum noside noside)
2561 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2562 struct type *type = get_type ();
2564 if (type->length () > old_value->type ()->length ())
2565 error (_("length type is larger than the value type"));
2567 struct value *result = value::allocate (type);
2568 old_value->contents_copy (result, 0, 0, type->length ());
2569 return result;
2575 /* Helper for evaluate_subexp_for_address. */
2577 static value *
2578 evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
2579 value *x)
2581 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2583 struct type *type = check_typedef (x->type ());
2585 if (TYPE_IS_REFERENCE (type))
2586 return value::zero (lookup_pointer_type (type->target_type ()),
2587 not_lval);
2588 else if (x->lval () == lval_memory || value_must_coerce_to_target (x))
2589 return value::zero (lookup_pointer_type (x->type ()),
2590 not_lval);
2591 else
2592 error (_("Attempt to take address of "
2593 "value not located in memory."));
2595 return value_addr (x);
2598 namespace expr
2601 value *
2602 operation::evaluate_for_cast (struct type *expect_type,
2603 struct expression *exp,
2604 enum noside noside)
2606 value *val = evaluate (expect_type, exp, noside);
2607 return value_cast (expect_type, val);
2610 value *
2611 operation::evaluate_for_address (struct expression *exp, enum noside noside)
2613 value *val = evaluate (nullptr, exp, noside);
2614 return evaluate_subexp_for_address_base (exp, noside, val);
2617 value *
2618 scope_operation::evaluate_for_address (struct expression *exp,
2619 enum noside noside)
2621 value *x = value_aggregate_elt (std::get<0> (m_storage),
2622 std::get<1> (m_storage).c_str (),
2623 NULL, 1, noside);
2624 if (x == NULL)
2625 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
2626 return x;
2629 value *
2630 unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2631 enum noside noside)
2633 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2635 /* We can't optimize out "&*" if there's a user-defined operator*. */
2636 if (unop_user_defined_p (UNOP_IND, x))
2638 x = value_x_unop (x, UNOP_IND, noside);
2639 return evaluate_subexp_for_address_base (exp, noside, x);
2642 return coerce_array (x);
2645 value *
2646 var_msym_value_operation::evaluate_for_address (struct expression *exp,
2647 enum noside noside)
2649 const bound_minimal_symbol &b = std::get<0> (m_storage);
2650 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2651 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2653 struct type *type = lookup_pointer_type (val->type ());
2654 return value::zero (type, not_lval);
2656 else
2657 return value_addr (val);
2660 value *
2661 unop_memval_operation::evaluate_for_address (struct expression *exp,
2662 enum noside noside)
2664 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2665 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2668 value *
2669 unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2670 enum noside noside)
2672 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2673 EVAL_AVOID_SIDE_EFFECTS);
2674 struct type *type = typeval->type ();
2675 return value_cast (lookup_pointer_type (type),
2676 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2679 value *
2680 var_value_operation::evaluate_for_address (struct expression *exp,
2681 enum noside noside)
2683 symbol *var = std::get<0> (m_storage).symbol;
2685 /* C++: The "address" of a reference should yield the address
2686 * of the object pointed to. Let value_addr() deal with it. */
2687 if (TYPE_IS_REFERENCE (var->type ()))
2688 return operation::evaluate_for_address (exp, noside);
2690 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2692 struct type *type = lookup_pointer_type (var->type ());
2693 enum address_class sym_class = var->aclass ();
2695 if (sym_class == LOC_CONST
2696 || sym_class == LOC_CONST_BYTES
2697 || sym_class == LOC_REGISTER)
2698 error (_("Attempt to take address of register or constant."));
2700 return value::zero (type, not_lval);
2702 else
2703 return address_of_variable (var, std::get<0> (m_storage).block);
2706 value *
2707 var_value_operation::evaluate_with_coercion (struct expression *exp,
2708 enum noside noside)
2710 struct symbol *var = std::get<0> (m_storage).symbol;
2711 struct type *type = check_typedef (var->type ());
2712 if (type->code () == TYPE_CODE_ARRAY
2713 && !type->is_vector ()
2714 && CAST_IS_CONVERSION (exp->language_defn))
2716 struct value *val = address_of_variable (var,
2717 std::get<0> (m_storage).block);
2718 return value_cast (lookup_pointer_type (type->target_type ()), val);
2720 return evaluate (nullptr, exp, noside);
2725 /* Helper function for evaluating the size of a type. */
2727 static value *
2728 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2730 /* FIXME: This should be size_t. */
2731 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2732 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2733 "When applied to a reference or a reference type, the result is
2734 the size of the referenced type." */
2735 type = check_typedef (type);
2736 if (exp->language_defn->la_language == language_cplus
2737 && (TYPE_IS_REFERENCE (type)))
2738 type = check_typedef (type->target_type ());
2739 else if (exp->language_defn->la_language == language_fortran
2740 && type->code () == TYPE_CODE_PTR)
2742 /* Dereference Fortran pointer types to allow them for the Fortran
2743 sizeof intrinsic. */
2744 type = check_typedef (type->target_type ());
2746 return value_from_longest (size_type, (LONGEST) type->length ());
2749 namespace expr
2752 value *
2753 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2755 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2756 return evaluate_subexp_for_sizeof_base (exp, val->type ());
2759 value *
2760 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2761 enum noside noside)
2764 const bound_minimal_symbol &b = std::get<0> (m_storage);
2765 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2767 struct type *type = mval->type ();
2768 if (type->code () == TYPE_CODE_ERROR)
2769 error_unknown_type (b.minsym->print_name ());
2771 /* FIXME: This should be size_t. */
2772 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2773 return value_from_longest (size_type, type->length ());
2776 value *
2777 subscript_operation::evaluate_for_sizeof (struct expression *exp,
2778 enum noside noside)
2780 if (noside == EVAL_NORMAL)
2782 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2783 EVAL_AVOID_SIDE_EFFECTS);
2784 struct type *type = check_typedef (val->type ());
2785 if (type->code () == TYPE_CODE_ARRAY)
2787 type = check_typedef (type->target_type ());
2788 if (type->code () == TYPE_CODE_ARRAY)
2790 type = type->index_type ();
2791 /* Only re-evaluate the right hand side if the resulting type
2792 is a variable length type. */
2793 if (type->bounds ()->flag_bound_evaluated)
2795 val = evaluate (nullptr, exp, EVAL_NORMAL);
2796 /* FIXME: This should be size_t. */
2797 struct type *size_type
2798 = builtin_type (exp->gdbarch)->builtin_int;
2799 return value_from_longest
2800 (size_type, (LONGEST) val->type ()->length ());
2806 return operation::evaluate_for_sizeof (exp, noside);
2809 value *
2810 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2811 enum noside noside)
2813 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2814 EVAL_AVOID_SIDE_EFFECTS);
2815 struct type *type = check_typedef (val->type ());
2816 if (!type->is_pointer_or_reference ()
2817 && type->code () != TYPE_CODE_ARRAY)
2818 error (_("Attempt to take contents of a non-pointer value."));
2819 type = type->target_type ();
2820 if (is_dynamic_type (type))
2821 type = value_ind (val)->type ();
2822 /* FIXME: This should be size_t. */
2823 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2824 return value_from_longest (size_type, (LONGEST) type->length ());
2827 value *
2828 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2829 enum noside noside)
2831 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2834 value *
2835 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2836 enum noside noside)
2838 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2839 EVAL_AVOID_SIDE_EFFECTS);
2840 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
2843 value *
2844 var_value_operation::evaluate_for_sizeof (struct expression *exp,
2845 enum noside noside)
2847 struct type *type = std::get<0> (m_storage).symbol->type ();
2848 if (is_dynamic_type (type))
2850 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2851 type = val->type ();
2852 if (type->code () == TYPE_CODE_ARRAY)
2854 /* FIXME: This should be size_t. */
2855 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2856 if (type_not_allocated (type) || type_not_associated (type))
2857 return value::zero (size_type, not_lval);
2858 else if (is_dynamic_type (type->index_type ())
2859 && !type->bounds ()->high.is_available ())
2860 return value::allocate_optimized_out (size_type);
2863 return evaluate_subexp_for_sizeof_base (exp, type);
2866 value *
2867 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2868 struct expression *exp,
2869 enum noside noside)
2871 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2872 return value::zero (to_type, not_lval);
2874 const bound_minimal_symbol &b = std::get<0> (m_storage);
2875 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2877 val = value_cast (to_type, val);
2879 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2880 if (val->lval () == lval_memory)
2882 if (val->lazy ())
2883 val->fetch_lazy ();
2884 val->set_lval (not_lval);
2886 return val;
2889 value *
2890 var_value_operation::evaluate_for_cast (struct type *to_type,
2891 struct expression *exp,
2892 enum noside noside)
2894 value *val = evaluate_var_value (noside,
2895 std::get<0> (m_storage).block,
2896 std::get<0> (m_storage).symbol);
2898 val = value_cast (to_type, val);
2900 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2901 if (val->lval () == lval_memory)
2903 if (val->lazy ())
2904 val->fetch_lazy ();
2905 val->set_lval (not_lval);
2907 return val;
2912 /* Parse a type expression in the string [P..P+LENGTH). */
2914 struct type *
2915 parse_and_eval_type (const char *p, int length)
2917 char *tmp = (char *) alloca (length + 4);
2919 tmp[0] = '(';
2920 memcpy (tmp + 1, p, length);
2921 tmp[length + 1] = ')';
2922 tmp[length + 2] = '0';
2923 tmp[length + 3] = '\0';
2924 expression_up expr = parse_expression (tmp);
2925 expr::unop_cast_operation *op
2926 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2927 if (op == nullptr)
2928 error (_("Internal error in eval_type."));
2929 return op->get_type ();