Updated Swedish translation for the opcodes directory
[binutils-gdb.git] / gdb / eval.c
blob9d5ca0b47de80a8beb79849944fd8bf8a8e1e226
1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2023 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" /* For CAST_IS_CONVERSION. */
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)
72 expression_up expr = parse_expression (exp);
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, 1);
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 gdb::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 /* Fall through. */
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"));
591 if (noside == EVAL_AVOID_SIDE_EFFECTS)
593 /* If the return type doesn't look like a function type,
594 call an error. This can happen if somebody tries to turn
595 a variable into a function call. */
597 type *ftype = callee->type ();
599 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
601 /* We don't know anything about what the internal
602 function might return, but we have to return
603 something. */
604 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
605 not_lval);
607 else if (ftype->code () == TYPE_CODE_XMETHOD)
609 type *return_type = callee->result_type_of_xmethod (argvec);
611 if (return_type == NULL)
612 error (_("Xmethod is missing return type."));
613 return value::zero (return_type, not_lval);
615 else if (ftype->code () == TYPE_CODE_FUNC
616 || ftype->code () == TYPE_CODE_METHOD)
618 if (ftype->is_gnu_ifunc ())
620 CORE_ADDR address = callee->address ();
621 type *resolved_type = find_gnu_ifunc_target_type (address);
623 if (resolved_type != NULL)
624 ftype = resolved_type;
627 type *return_type = ftype->target_type ();
629 if (return_type == NULL)
630 return_type = default_return_type;
632 if (return_type == NULL)
633 error_call_unknown_return_type (function_name);
635 return value::allocate (return_type);
637 else
638 error (_("Expression of type other than "
639 "\"Function returning ...\" used as function"));
641 switch (callee->type ()->code ())
643 case TYPE_CODE_INTERNAL_FUNCTION:
644 return call_internal_function (exp->gdbarch, exp->language_defn,
645 callee, argvec.size (), argvec.data ());
646 case TYPE_CODE_XMETHOD:
647 return callee->call_xmethod (argvec);
648 default:
649 return call_function_by_hand (callee, default_return_type, argvec);
653 namespace expr
656 value *
657 operation::evaluate_funcall (struct type *expect_type,
658 struct expression *exp,
659 enum noside noside,
660 const char *function_name,
661 const std::vector<operation_up> &args)
663 std::vector<value *> vals (args.size ());
665 value *callee = evaluate_with_coercion (exp, noside);
666 struct type *type = callee->type ();
667 if (type->code () == TYPE_CODE_PTR)
668 type = type->target_type ();
669 for (int i = 0; i < args.size (); ++i)
671 if (i < type->num_fields ())
672 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
673 else
674 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
677 return evaluate_subexp_do_call (exp, noside, callee, vals,
678 function_name, expect_type);
681 value *
682 var_value_operation::evaluate_funcall (struct type *expect_type,
683 struct expression *exp,
684 enum noside noside,
685 const std::vector<operation_up> &args)
687 if (!overload_resolution
688 || exp->language_defn->la_language != language_cplus)
689 return operation::evaluate_funcall (expect_type, exp, noside, args);
691 std::vector<value *> argvec (args.size ());
692 for (int i = 0; i < args.size (); ++i)
693 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
695 struct symbol *symp;
696 find_overload_match (argvec, NULL, NON_METHOD,
697 NULL, std::get<0> (m_storage).symbol,
698 NULL, &symp, NULL, 0, noside);
700 if (symp->type ()->code () == TYPE_CODE_ERROR)
701 error_unknown_type (symp->print_name ());
702 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
703 symp);
705 return evaluate_subexp_do_call (exp, noside, callee, argvec,
706 nullptr, expect_type);
709 value *
710 scope_operation::evaluate_funcall (struct type *expect_type,
711 struct expression *exp,
712 enum noside noside,
713 const std::vector<operation_up> &args)
715 if (!overload_resolution
716 || exp->language_defn->la_language != language_cplus)
717 return operation::evaluate_funcall (expect_type, exp, noside, args);
719 /* Unpack it locally so we can properly handle overload
720 resolution. */
721 const std::string &name = std::get<1> (m_storage);
722 struct type *type = std::get<0> (m_storage);
724 symbol *function = NULL;
725 const char *function_name = NULL;
726 std::vector<value *> argvec (1 + args.size ());
727 if (type->code () == TYPE_CODE_NAMESPACE)
729 function = cp_lookup_symbol_namespace (type->name (),
730 name.c_str (),
731 get_selected_block (0),
732 VAR_DOMAIN).symbol;
733 if (function == NULL)
734 error (_("No symbol \"%s\" in namespace \"%s\"."),
735 name.c_str (), type->name ());
737 else
739 gdb_assert (type->code () == TYPE_CODE_STRUCT
740 || type->code () == TYPE_CODE_UNION);
741 function_name = name.c_str ();
743 /* We need a properly typed value for method lookup. */
744 argvec[0] = value::zero (type, lval_memory);
747 for (int i = 0; i < args.size (); ++i)
748 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
749 gdb::array_view<value *> arg_view = argvec;
751 value *callee = nullptr;
752 if (function_name != nullptr)
754 int static_memfuncp;
756 find_overload_match (arg_view, function_name, METHOD,
757 &argvec[0], nullptr, &callee, nullptr,
758 &static_memfuncp, 0, noside);
759 if (!static_memfuncp)
761 /* For the time being, we don't handle this. */
762 error (_("Call to overloaded function %s requires "
763 "`this' pointer"),
764 function_name);
767 arg_view = arg_view.slice (1);
769 else
771 symbol *symp;
772 arg_view = arg_view.slice (1);
773 find_overload_match (arg_view, nullptr,
774 NON_METHOD, nullptr, function,
775 nullptr, &symp, nullptr, 1, noside);
776 callee = value_of_variable (symp, get_selected_block (0));
779 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
780 nullptr, expect_type);
783 value *
784 structop_member_base::evaluate_funcall (struct type *expect_type,
785 struct expression *exp,
786 enum noside noside,
787 const std::vector<operation_up> &args)
789 /* First, evaluate the structure into lhs. */
790 value *lhs;
791 if (opcode () == STRUCTOP_MEMBER)
792 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
793 else
794 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
796 std::vector<value *> vals (args.size () + 1);
797 gdb::array_view<value *> val_view = vals;
798 /* If the function is a virtual function, then the aggregate
799 value (providing the structure) plays its part by providing
800 the vtable. Otherwise, it is just along for the ride: call
801 the function directly. */
802 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
803 value *callee;
805 type *a1_type = check_typedef (rhs->type ());
806 if (a1_type->code () == TYPE_CODE_METHODPTR)
808 if (noside == EVAL_AVOID_SIDE_EFFECTS)
809 callee = value::zero (a1_type->target_type (), not_lval);
810 else
811 callee = cplus_method_ptr_to_value (&lhs, rhs);
813 vals[0] = lhs;
815 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
817 struct type *type_ptr
818 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
819 struct type *target_type_ptr
820 = lookup_pointer_type (a1_type->target_type ());
822 /* Now, convert this value to an address. */
823 lhs = value_cast (type_ptr, lhs);
825 long mem_offset = value_as_long (rhs);
827 callee = value_from_pointer (target_type_ptr,
828 value_as_long (lhs) + mem_offset);
829 callee = value_ind (callee);
831 val_view = val_view.slice (1);
833 else
834 error (_("Non-pointer-to-member value used in pointer-to-member "
835 "construct"));
837 for (int i = 0; i < args.size (); ++i)
838 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
840 return evaluate_subexp_do_call (exp, noside, callee, val_view,
841 nullptr, expect_type);
845 value *
846 structop_base_operation::evaluate_funcall
847 (struct type *expect_type, struct expression *exp, enum noside noside,
848 const std::vector<operation_up> &args)
850 /* Allocate space for the function call arguments, Including space for a
851 `this' pointer at the start. */
852 std::vector<value *> vals (args.size () + 1);
853 /* First, evaluate the structure into vals[0]. */
854 enum exp_opcode op = opcode ();
855 if (op == STRUCTOP_STRUCT)
857 /* If v is a variable in a register, and the user types
858 v.method (), this will produce an error, because v has no
859 address.
861 A possible way around this would be to allocate a copy of
862 the variable on the stack, copy in the contents, call the
863 function, and copy out the contents. I.e. convert this
864 from call by reference to call by copy-return (or
865 whatever it's called). However, this does not work
866 because it is not the same: the method being called could
867 stash a copy of the address, and then future uses through
868 that address (after the method returns) would be expected
869 to use the variable itself, not some copy of it. */
870 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
872 else
874 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
875 /* Check to see if the operator '->' has been overloaded.
876 If the operator has been overloaded replace vals[0] with the
877 value returned by the custom operator and continue
878 evaluation. */
879 while (unop_user_defined_p (op, vals[0]))
881 struct value *value = nullptr;
884 value = value_x_unop (vals[0], op, noside);
886 catch (const gdb_exception_error &except)
888 if (except.error == NOT_FOUND_ERROR)
889 break;
890 else
891 throw;
894 vals[0] = value;
898 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
899 pointer we placed into vals[0]. */
900 for (int i = 0; i < args.size (); ++i)
901 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
903 /* The array view includes the `this' pointer. */
904 gdb::array_view<value *> arg_view (vals);
906 int static_memfuncp;
907 value *callee;
908 const char *tstr = std::get<1> (m_storage).c_str ();
909 if (overload_resolution
910 && exp->language_defn->la_language == language_cplus)
912 /* Language is C++, do some overload resolution before
913 evaluation. */
914 value *val0 = vals[0];
915 find_overload_match (arg_view, tstr, METHOD,
916 &val0, nullptr, &callee, nullptr,
917 &static_memfuncp, 0, noside);
918 vals[0] = val0;
920 else
921 /* Non-C++ case -- or no overload resolution. */
923 struct value *temp = vals[0];
925 callee = value_struct_elt (&temp, arg_view, tstr,
926 &static_memfuncp,
927 op == STRUCTOP_STRUCT
928 ? "structure" : "structure pointer");
929 /* value_struct_elt updates temp with the correct value of the
930 ``this'' pointer if necessary, so modify it to reflect any
931 ``this'' changes. */
932 vals[0] = value_from_longest (lookup_pointer_type (temp->type ()),
933 temp->address ()
934 + temp->embedded_offset ());
937 /* Take out `this' if needed. */
938 if (static_memfuncp)
939 arg_view = arg_view.slice (1);
941 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
942 nullptr, expect_type);
945 /* Helper for structop_base_operation::complete which recursively adds
946 field and method names from TYPE, a struct or union type, to the
947 OUTPUT list. PREFIX is prepended to each result. */
949 static void
950 add_struct_fields (struct type *type, completion_list &output,
951 const char *fieldname, int namelen, const char *prefix)
953 int i;
954 int computed_type_name = 0;
955 const char *type_name = NULL;
957 type = check_typedef (type);
958 for (i = 0; i < type->num_fields (); ++i)
960 if (i < TYPE_N_BASECLASSES (type))
961 add_struct_fields (TYPE_BASECLASS (type, i),
962 output, fieldname, namelen, prefix);
963 else if (type->field (i).name ())
965 if (type->field (i).name ()[0] != '\0')
967 if (! strncmp (type->field (i).name (),
968 fieldname, namelen))
969 output.emplace_back (concat (prefix, type->field (i).name (),
970 nullptr));
972 else if (type->field (i).type ()->code () == TYPE_CODE_UNION)
974 /* Recurse into anonymous unions. */
975 add_struct_fields (type->field (i).type (),
976 output, fieldname, namelen, prefix);
981 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
983 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
985 if (name && ! strncmp (name, fieldname, namelen))
987 if (!computed_type_name)
989 type_name = type->name ();
990 computed_type_name = 1;
992 /* Omit constructors from the completion list. */
993 if (!type_name || strcmp (type_name, name))
994 output.emplace_back (concat (prefix, name, nullptr));
999 /* See expop.h. */
1001 bool
1002 structop_base_operation::complete (struct expression *exp,
1003 completion_tracker &tracker,
1004 const char *prefix)
1006 const std::string &fieldname = std::get<1> (m_storage);
1008 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp,
1009 EVAL_AVOID_SIDE_EFFECTS);
1010 struct type *type = lhs->type ();
1011 for (;;)
1013 type = check_typedef (type);
1014 if (!type->is_pointer_or_reference ())
1015 break;
1016 type = type->target_type ();
1019 if (type->code () == TYPE_CODE_UNION
1020 || type->code () == TYPE_CODE_STRUCT)
1022 completion_list result;
1024 add_struct_fields (type, result, fieldname.c_str (),
1025 fieldname.length (), prefix);
1026 tracker.add_completions (std::move (result));
1027 return true;
1030 return false;
1033 } /* namespace expr */
1035 /* Return true if type is integral or reference to integral */
1037 static bool
1038 is_integral_or_integral_reference (struct type *type)
1040 if (is_integral_type (type))
1041 return true;
1043 type = check_typedef (type);
1044 return (type != nullptr
1045 && TYPE_IS_REFERENCE (type)
1046 && is_integral_type (type->target_type ()));
1049 /* Helper function that implements the body of OP_SCOPE. */
1051 struct value *
1052 eval_op_scope (struct type *expect_type, struct expression *exp,
1053 enum noside noside,
1054 struct type *type, const char *string)
1056 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
1057 0, noside);
1058 if (arg1 == NULL)
1059 error (_("There is no field named %s"), string);
1060 return arg1;
1063 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1065 struct value *
1066 eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1067 enum noside noside, symbol *sym)
1069 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1070 return value::zero (sym->type (), not_lval);
1072 if (SYMBOL_COMPUTED_OPS (sym) == NULL
1073 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
1074 error (_("Symbol \"%s\" does not have any specific entry value"),
1075 sym->print_name ());
1077 frame_info_ptr frame = get_selected_frame (NULL);
1078 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1081 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1083 struct value *
1084 eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1085 enum noside noside, bool outermost_p,
1086 bound_minimal_symbol msymbol)
1088 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1089 msymbol.minsym);
1091 struct type *type = val->type ();
1092 if (type->code () == TYPE_CODE_ERROR
1093 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1094 error_unknown_type (msymbol.minsym->print_name ());
1095 return val;
1098 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1100 struct value *
1101 eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1102 enum noside noside,
1103 value *func, const char *var)
1105 CORE_ADDR addr = func->address ();
1106 const block *blk = block_for_pc (addr);
1107 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1108 if (sym.symbol == NULL)
1109 error (_("No symbol \"%s\" in specified context."), var);
1110 return evaluate_var_value (noside, sym.block, sym.symbol);
1113 /* Helper function that implements the body of OP_REGISTER. */
1115 struct value *
1116 eval_op_register (struct type *expect_type, struct expression *exp,
1117 enum noside noside, const char *name)
1119 int regno;
1120 struct value *val;
1122 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1123 name, strlen (name));
1124 if (regno == -1)
1125 error (_("Register $%s not available."), name);
1127 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1128 a value with the appropriate register type. Unfortunately,
1129 we don't have easy access to the type of user registers.
1130 So for these registers, we fetch the register value regardless
1131 of the evaluation mode. */
1132 if (noside == EVAL_AVOID_SIDE_EFFECTS
1133 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1134 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
1135 else
1136 val = value_of_register (regno, get_selected_frame (NULL));
1137 if (val == NULL)
1138 error (_("Value of register %s not available."), name);
1139 else
1140 return val;
1143 namespace expr
1146 value *
1147 string_operation::evaluate (struct type *expect_type,
1148 struct expression *exp,
1149 enum noside noside)
1151 const std::string &str = std::get<0> (m_storage);
1152 struct type *type = language_string_char_type (exp->language_defn,
1153 exp->gdbarch);
1154 return value_string (str.c_str (), str.size (), type);
1157 } /* namespace expr */
1159 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1161 struct value *
1162 eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1163 enum noside noside,
1164 const char *sel)
1166 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1167 return value_from_longest (selector_type,
1168 lookup_child_selector (exp->gdbarch, sel));
1171 /* A helper function for TERNOP_SLICE. */
1173 struct value *
1174 eval_op_ternop (struct type *expect_type, struct expression *exp,
1175 enum noside noside,
1176 struct value *array, struct value *low, struct value *upper)
1178 int lowbound = value_as_long (low);
1179 int upperbound = value_as_long (upper);
1180 return value_slice (array, lowbound, upperbound - lowbound + 1);
1183 /* A helper function for STRUCTOP_STRUCT. */
1185 struct value *
1186 eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1187 enum noside noside,
1188 struct value *arg1, const char *string)
1190 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1191 NULL, "structure");
1192 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1193 arg3 = value::zero (arg3->type (), arg3->lval ());
1194 return arg3;
1197 /* A helper function for STRUCTOP_PTR. */
1199 struct value *
1200 eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
1201 enum noside noside,
1202 struct value *arg1, const char *string)
1204 /* Check to see if operator '->' has been overloaded. If so replace
1205 arg1 with the value returned by evaluating operator->(). */
1206 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
1208 struct value *value = NULL;
1211 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
1214 catch (const gdb_exception_error &except)
1216 if (except.error == NOT_FOUND_ERROR)
1217 break;
1218 else
1219 throw;
1222 arg1 = value;
1225 /* JYG: if print object is on we need to replace the base type
1226 with rtti type in order to continue on with successful
1227 lookup of member / method only available in the rtti type. */
1229 struct type *arg_type = arg1->type ();
1230 struct type *real_type;
1231 int full, using_enc;
1232 LONGEST top;
1233 struct value_print_options opts;
1235 get_user_print_options (&opts);
1236 if (opts.objectprint && arg_type->target_type ()
1237 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
1239 real_type = value_rtti_indirect_type (arg1, &full, &top,
1240 &using_enc);
1241 if (real_type)
1242 arg1 = value_cast (real_type, arg1);
1246 struct value *arg3 = value_struct_elt (&arg1, {}, string,
1247 NULL, "structure pointer");
1248 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1249 arg3 = value::zero (arg3->type (), arg3->lval ());
1250 return arg3;
1253 /* A helper function for STRUCTOP_MEMBER. */
1255 struct value *
1256 eval_op_member (struct type *expect_type, struct expression *exp,
1257 enum noside noside,
1258 struct value *arg1, struct value *arg2)
1260 long mem_offset;
1262 struct value *arg3;
1263 struct type *type = check_typedef (arg2->type ());
1264 switch (type->code ())
1266 case TYPE_CODE_METHODPTR:
1267 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1268 return value::zero (type->target_type (), not_lval);
1269 else
1271 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1272 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
1273 return value_ind (arg2);
1276 case TYPE_CODE_MEMBERPTR:
1277 /* Now, convert these values to an address. */
1278 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
1279 arg1 = value_addr (arg1);
1280 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1281 arg1, 1);
1283 mem_offset = value_as_long (arg2);
1285 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
1286 value_as_long (arg1) + mem_offset);
1287 return value_ind (arg3);
1289 default:
1290 error (_("non-pointer-to-member value used "
1291 "in pointer-to-member construct"));
1295 /* A helper function for BINOP_ADD. */
1297 struct value *
1298 eval_op_add (struct type *expect_type, struct expression *exp,
1299 enum noside noside,
1300 struct value *arg1, struct value *arg2)
1302 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1303 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
1304 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1305 && is_integral_or_integral_reference (arg2->type ()))
1306 return value_ptradd (arg1, value_as_long (arg2));
1307 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1308 && is_integral_or_integral_reference (arg1->type ()))
1309 return value_ptradd (arg2, value_as_long (arg1));
1310 else
1312 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1313 return value_binop (arg1, arg2, BINOP_ADD);
1317 /* A helper function for BINOP_SUB. */
1319 struct value *
1320 eval_op_sub (struct type *expect_type, struct expression *exp,
1321 enum noside noside,
1322 struct value *arg1, struct value *arg2)
1324 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1325 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
1326 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1327 && ptrmath_type_p (exp->language_defn, arg2->type ()))
1329 /* FIXME -- should be ptrdiff_t */
1330 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1331 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1333 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1334 && is_integral_or_integral_reference (arg2->type ()))
1335 return value_ptradd (arg1, - value_as_long (arg2));
1336 else
1338 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1339 return value_binop (arg1, arg2, BINOP_SUB);
1343 /* Helper function for several different binary operations. */
1345 struct value *
1346 eval_op_binary (struct type *expect_type, struct expression *exp,
1347 enum noside noside, enum exp_opcode op,
1348 struct value *arg1, struct value *arg2)
1350 if (binop_user_defined_p (op, arg1, arg2))
1351 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1352 else
1354 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1355 fudge arg2 to avoid division-by-zero, the caller is
1356 (theoretically) only looking for the type of the result. */
1357 if (noside == EVAL_AVOID_SIDE_EFFECTS
1358 /* ??? Do we really want to test for BINOP_MOD here?
1359 The implementation of value_binop gives it a well-defined
1360 value. */
1361 && (op == BINOP_DIV
1362 || op == BINOP_INTDIV
1363 || op == BINOP_REM
1364 || op == BINOP_MOD)
1365 && value_logical_not (arg2))
1367 struct value *v_one;
1369 v_one = value_one (arg2->type ());
1370 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1371 return value_binop (arg1, v_one, op);
1373 else
1375 /* For shift and integer exponentiation operations,
1376 only promote the first argument. */
1377 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1378 && is_integral_type (arg2->type ()))
1379 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1380 else
1381 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1383 return value_binop (arg1, arg2, op);
1388 /* A helper function for BINOP_SUBSCRIPT. */
1390 struct value *
1391 eval_op_subscript (struct type *expect_type, struct expression *exp,
1392 enum noside noside, enum exp_opcode op,
1393 struct value *arg1, struct value *arg2)
1395 if (binop_user_defined_p (op, arg1, arg2))
1396 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1397 else
1399 /* If the user attempts to subscript something that is not an
1400 array or pointer type (like a plain int variable for example),
1401 then report this as an error. */
1403 arg1 = coerce_ref (arg1);
1404 struct type *type = check_typedef (arg1->type ());
1405 if (type->code () != TYPE_CODE_ARRAY
1406 && type->code () != TYPE_CODE_PTR)
1408 if (type->name ())
1409 error (_("cannot subscript something of type `%s'"),
1410 type->name ());
1411 else
1412 error (_("cannot subscript requested type"));
1415 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1416 return value::zero (type->target_type (), arg1->lval ());
1417 else
1418 return value_subscript (arg1, value_as_long (arg2));
1422 /* A helper function for BINOP_EQUAL. */
1424 struct value *
1425 eval_op_equal (struct type *expect_type, struct expression *exp,
1426 enum noside noside, enum exp_opcode op,
1427 struct value *arg1, struct value *arg2)
1429 if (binop_user_defined_p (op, arg1, arg2))
1431 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1433 else
1435 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1436 int tem = value_equal (arg1, arg2);
1437 struct type *type = language_bool_type (exp->language_defn,
1438 exp->gdbarch);
1439 return value_from_longest (type, (LONGEST) tem);
1443 /* A helper function for BINOP_NOTEQUAL. */
1445 struct value *
1446 eval_op_notequal (struct type *expect_type, struct expression *exp,
1447 enum noside noside, enum exp_opcode op,
1448 struct value *arg1, struct value *arg2)
1450 if (binop_user_defined_p (op, arg1, arg2))
1452 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1454 else
1456 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1457 int tem = value_equal (arg1, arg2);
1458 struct type *type = language_bool_type (exp->language_defn,
1459 exp->gdbarch);
1460 return value_from_longest (type, (LONGEST) ! tem);
1464 /* A helper function for BINOP_LESS. */
1466 struct value *
1467 eval_op_less (struct type *expect_type, struct expression *exp,
1468 enum noside noside, enum exp_opcode op,
1469 struct value *arg1, struct value *arg2)
1471 if (binop_user_defined_p (op, arg1, arg2))
1473 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1475 else
1477 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1478 int tem = value_less (arg1, arg2);
1479 struct type *type = language_bool_type (exp->language_defn,
1480 exp->gdbarch);
1481 return value_from_longest (type, (LONGEST) tem);
1485 /* A helper function for BINOP_GTR. */
1487 struct value *
1488 eval_op_gtr (struct type *expect_type, struct expression *exp,
1489 enum noside noside, enum exp_opcode op,
1490 struct value *arg1, struct value *arg2)
1492 if (binop_user_defined_p (op, arg1, arg2))
1494 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1496 else
1498 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1499 int tem = value_less (arg2, arg1);
1500 struct type *type = language_bool_type (exp->language_defn,
1501 exp->gdbarch);
1502 return value_from_longest (type, (LONGEST) tem);
1506 /* A helper function for BINOP_GEQ. */
1508 struct value *
1509 eval_op_geq (struct type *expect_type, struct expression *exp,
1510 enum noside noside, enum exp_opcode op,
1511 struct value *arg1, struct value *arg2)
1513 if (binop_user_defined_p (op, arg1, arg2))
1515 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1517 else
1519 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1520 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1521 struct type *type = language_bool_type (exp->language_defn,
1522 exp->gdbarch);
1523 return value_from_longest (type, (LONGEST) tem);
1527 /* A helper function for BINOP_LEQ. */
1529 struct value *
1530 eval_op_leq (struct type *expect_type, struct expression *exp,
1531 enum noside noside, enum exp_opcode op,
1532 struct value *arg1, struct value *arg2)
1534 if (binop_user_defined_p (op, arg1, arg2))
1536 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1538 else
1540 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1541 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1542 struct type *type = language_bool_type (exp->language_defn,
1543 exp->gdbarch);
1544 return value_from_longest (type, (LONGEST) tem);
1548 /* A helper function for BINOP_REPEAT. */
1550 struct value *
1551 eval_op_repeat (struct type *expect_type, struct expression *exp,
1552 enum noside noside, enum exp_opcode op,
1553 struct value *arg1, struct value *arg2)
1555 struct type *type = check_typedef (arg2->type ());
1556 if (type->code () != TYPE_CODE_INT
1557 && type->code () != TYPE_CODE_ENUM)
1558 error (_("Non-integral right operand for \"@\" operator."));
1559 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1561 return allocate_repeat_value (arg1->type (),
1562 longest_to_int (value_as_long (arg2)));
1564 else
1565 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1568 /* A helper function for UNOP_PLUS. */
1570 struct value *
1571 eval_op_plus (struct type *expect_type, struct expression *exp,
1572 enum noside noside, enum exp_opcode op,
1573 struct value *arg1)
1575 if (unop_user_defined_p (op, arg1))
1576 return value_x_unop (arg1, op, noside);
1577 else
1579 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1580 return value_pos (arg1);
1584 /* A helper function for UNOP_NEG. */
1586 struct value *
1587 eval_op_neg (struct type *expect_type, struct expression *exp,
1588 enum noside noside, enum exp_opcode op,
1589 struct value *arg1)
1591 if (unop_user_defined_p (op, arg1))
1592 return value_x_unop (arg1, op, noside);
1593 else
1595 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1596 return value_neg (arg1);
1600 /* A helper function for UNOP_COMPLEMENT. */
1602 struct value *
1603 eval_op_complement (struct type *expect_type, struct expression *exp,
1604 enum noside noside, enum exp_opcode op,
1605 struct value *arg1)
1607 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1608 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1609 else
1611 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1612 return value_complement (arg1);
1616 /* A helper function for UNOP_LOGICAL_NOT. */
1618 struct value *
1619 eval_op_lognot (struct type *expect_type, struct expression *exp,
1620 enum noside noside, enum exp_opcode op,
1621 struct value *arg1)
1623 if (unop_user_defined_p (op, arg1))
1624 return value_x_unop (arg1, op, noside);
1625 else
1627 struct type *type = language_bool_type (exp->language_defn,
1628 exp->gdbarch);
1629 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1633 /* A helper function for UNOP_IND. */
1635 struct value *
1636 eval_op_ind (struct type *expect_type, struct expression *exp,
1637 enum noside noside,
1638 struct value *arg1)
1640 struct type *type = check_typedef (arg1->type ());
1641 if (type->code () == TYPE_CODE_METHODPTR
1642 || type->code () == TYPE_CODE_MEMBERPTR)
1643 error (_("Attempt to dereference pointer "
1644 "to member without an object"));
1645 if (unop_user_defined_p (UNOP_IND, arg1))
1646 return value_x_unop (arg1, UNOP_IND, noside);
1647 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1649 type = check_typedef (arg1->type ());
1651 /* If the type pointed to is dynamic then in order to resolve the
1652 dynamic properties we must actually dereference the pointer.
1653 There is a risk that this dereference will have side-effects
1654 in the inferior, but being able to print accurate type
1655 information seems worth the risk. */
1656 if (!type->is_pointer_or_reference ()
1657 || !is_dynamic_type (type->target_type ()))
1659 if (type->is_pointer_or_reference ()
1660 /* In C you can dereference an array to get the 1st elt. */
1661 || type->code () == TYPE_CODE_ARRAY)
1662 return value::zero (type->target_type (),
1663 lval_memory);
1664 else if (type->code () == TYPE_CODE_INT)
1665 /* GDB allows dereferencing an int. */
1666 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
1667 lval_memory);
1668 else
1669 error (_("Attempt to take contents of a non-pointer value."));
1673 /* Allow * on an integer so we can cast it to whatever we want.
1674 This returns an int, which seems like the most C-like thing to
1675 do. "long long" variables are rare enough that
1676 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1677 if (type->code () == TYPE_CODE_INT)
1678 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1679 (CORE_ADDR) value_as_address (arg1));
1680 return value_ind (arg1);
1683 /* A helper function for UNOP_ALIGNOF. */
1685 struct value *
1686 eval_op_alignof (struct type *expect_type, struct expression *exp,
1687 enum noside noside,
1688 struct value *arg1)
1690 struct type *type = arg1->type ();
1691 /* FIXME: This should be size_t. */
1692 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1693 ULONGEST align = type_align (type);
1694 if (align == 0)
1695 error (_("could not determine alignment of type"));
1696 return value_from_longest (size_type, align);
1699 /* A helper function for UNOP_MEMVAL. */
1701 struct value *
1702 eval_op_memval (struct type *expect_type, struct expression *exp,
1703 enum noside noside,
1704 struct value *arg1, struct type *type)
1706 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1707 return value::zero (type, lval_memory);
1708 else
1709 return value_at_lazy (type, value_as_address (arg1));
1712 /* A helper function for UNOP_PREINCREMENT. */
1714 struct value *
1715 eval_op_preinc (struct type *expect_type, struct expression *exp,
1716 enum noside noside, enum exp_opcode op,
1717 struct value *arg1)
1719 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1720 return arg1;
1721 else if (unop_user_defined_p (op, arg1))
1723 return value_x_unop (arg1, op, noside);
1725 else
1727 struct value *arg2;
1728 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1729 arg2 = value_ptradd (arg1, 1);
1730 else
1732 struct value *tmp = arg1;
1734 arg2 = value_one (arg1->type ());
1735 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1736 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1739 return value_assign (arg1, arg2);
1743 /* A helper function for UNOP_PREDECREMENT. */
1745 struct value *
1746 eval_op_predec (struct type *expect_type, struct expression *exp,
1747 enum noside noside, enum exp_opcode op,
1748 struct value *arg1)
1750 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1751 return arg1;
1752 else if (unop_user_defined_p (op, arg1))
1754 return value_x_unop (arg1, op, noside);
1756 else
1758 struct value *arg2;
1759 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1760 arg2 = value_ptradd (arg1, -1);
1761 else
1763 struct value *tmp = arg1;
1765 arg2 = value_one (arg1->type ());
1766 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1767 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1770 return value_assign (arg1, arg2);
1774 /* A helper function for UNOP_POSTINCREMENT. */
1776 struct value *
1777 eval_op_postinc (struct type *expect_type, struct expression *exp,
1778 enum noside noside, enum exp_opcode op,
1779 struct value *arg1)
1781 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1782 return arg1;
1783 else if (unop_user_defined_p (op, arg1))
1785 return value_x_unop (arg1, op, noside);
1787 else
1789 struct value *arg3 = arg1->non_lval ();
1790 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_ADD);
1803 value_assign (arg1, arg2);
1804 return arg3;
1808 /* A helper function for UNOP_POSTDECREMENT. */
1810 struct value *
1811 eval_op_postdec (struct type *expect_type, struct expression *exp,
1812 enum noside noside, enum exp_opcode op,
1813 struct value *arg1)
1815 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1816 return arg1;
1817 else if (unop_user_defined_p (op, arg1))
1819 return value_x_unop (arg1, op, noside);
1821 else
1823 struct value *arg3 = arg1->non_lval ();
1824 struct value *arg2;
1826 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
1827 arg2 = value_ptradd (arg1, -1);
1828 else
1830 struct value *tmp = arg1;
1832 arg2 = value_one (arg1->type ());
1833 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1834 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1837 value_assign (arg1, arg2);
1838 return arg3;
1842 /* A helper function for OP_TYPE. */
1844 struct value *
1845 eval_op_type (struct type *expect_type, struct expression *exp,
1846 enum noside noside, struct type *type)
1848 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1849 return value::allocate (type);
1850 else
1851 error (_("Attempt to use a type name as an expression"));
1854 /* A helper function for BINOP_ASSIGN_MODIFY. */
1856 struct value *
1857 eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1858 enum noside noside, enum exp_opcode op,
1859 struct value *arg1, struct value *arg2)
1861 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1862 return arg1;
1863 if (binop_user_defined_p (op, arg1, arg2))
1864 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1865 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1866 arg1->type ())
1867 && is_integral_type (arg2->type ()))
1868 arg2 = value_ptradd (arg1, value_as_long (arg2));
1869 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1870 arg1->type ())
1871 && is_integral_type (arg2->type ()))
1872 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1873 else
1875 struct value *tmp = arg1;
1877 /* For shift and integer exponentiation operations,
1878 only promote the first argument. */
1879 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1880 && is_integral_type (arg2->type ()))
1881 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1882 else
1883 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1885 arg2 = value_binop (tmp, arg2, op);
1887 return value_assign (arg1, arg2);
1890 /* Note that ARGS needs 2 empty slots up front and must end with a
1891 null pointer. */
1892 static struct value *
1893 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1894 enum noside noside, CORE_ADDR selector,
1895 value *target, gdb::array_view<value *> args)
1897 CORE_ADDR responds_selector = 0;
1898 CORE_ADDR method_selector = 0;
1900 int struct_return = 0;
1902 struct value *msg_send = NULL;
1903 struct value *msg_send_stret = NULL;
1904 int gnu_runtime = 0;
1906 struct value *method = NULL;
1907 struct value *called_method = NULL;
1909 struct type *selector_type = NULL;
1910 struct type *long_type;
1911 struct type *type;
1913 struct value *ret = NULL;
1914 CORE_ADDR addr = 0;
1916 value *argvec[5];
1918 long_type = builtin_type (exp->gdbarch)->builtin_long;
1919 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1921 if (value_as_long (target) == 0)
1922 return value_from_longest (long_type, 0);
1924 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1925 gnu_runtime = 1;
1927 /* Find the method dispatch (Apple runtime) or method lookup
1928 (GNU runtime) function for Objective-C. These will be used
1929 to lookup the symbol information for the method. If we
1930 can't find any symbol information, then we'll use these to
1931 call the method, otherwise we can call the method
1932 directly. The msg_send_stret function is used in the special
1933 case of a method that returns a structure (Apple runtime
1934 only). */
1935 if (gnu_runtime)
1937 type = selector_type;
1939 type = lookup_function_type (type);
1940 type = lookup_pointer_type (type);
1941 type = lookup_function_type (type);
1942 type = lookup_pointer_type (type);
1944 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1945 msg_send_stret
1946 = find_function_in_inferior ("objc_msg_lookup", NULL);
1948 msg_send = value_from_pointer (type, value_as_address (msg_send));
1949 msg_send_stret = value_from_pointer (type,
1950 value_as_address (msg_send_stret));
1952 else
1954 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1955 /* Special dispatcher for methods returning structs. */
1956 msg_send_stret
1957 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1960 /* Verify the target object responds to this method. The
1961 standard top-level 'Object' class uses a different name for
1962 the verification method than the non-standard, but more
1963 often used, 'NSObject' class. Make sure we check for both. */
1965 responds_selector
1966 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1967 if (responds_selector == 0)
1968 responds_selector
1969 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1971 if (responds_selector == 0)
1972 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1974 method_selector
1975 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1976 if (method_selector == 0)
1977 method_selector
1978 = lookup_child_selector (exp->gdbarch, "methodFor:");
1980 if (method_selector == 0)
1981 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1983 /* Call the verification method, to make sure that the target
1984 class implements the desired method. */
1986 argvec[0] = msg_send;
1987 argvec[1] = target;
1988 argvec[2] = value_from_longest (long_type, responds_selector);
1989 argvec[3] = value_from_longest (long_type, selector);
1990 argvec[4] = 0;
1992 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1993 if (gnu_runtime)
1995 /* Function objc_msg_lookup returns a pointer. */
1996 argvec[0] = ret;
1997 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1999 if (value_as_long (ret) == 0)
2000 error (_("Target does not respond to this message selector."));
2002 /* Call "methodForSelector:" method, to get the address of a
2003 function method that implements this selector for this
2004 class. If we can find a symbol at that address, then we
2005 know the return type, parameter types etc. (that's a good
2006 thing). */
2008 argvec[0] = msg_send;
2009 argvec[1] = target;
2010 argvec[2] = value_from_longest (long_type, method_selector);
2011 argvec[3] = value_from_longest (long_type, selector);
2012 argvec[4] = 0;
2014 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2015 if (gnu_runtime)
2017 argvec[0] = ret;
2018 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2021 /* ret should now be the selector. */
2023 addr = value_as_long (ret);
2024 if (addr)
2026 struct symbol *sym = NULL;
2028 /* The address might point to a function descriptor;
2029 resolve it to the actual code address instead. */
2030 addr = gdbarch_convert_from_func_ptr_addr
2031 (exp->gdbarch, addr, current_inferior ()->top_target ());
2033 /* Is it a high_level symbol? */
2034 sym = find_pc_function (addr);
2035 if (sym != NULL)
2036 method = value_of_variable (sym, 0);
2039 /* If we found a method with symbol information, check to see
2040 if it returns a struct. Otherwise assume it doesn't. */
2042 if (method)
2044 CORE_ADDR funaddr;
2045 struct type *val_type;
2047 funaddr = find_function_addr (method, &val_type);
2049 block_for_pc (funaddr);
2051 val_type = check_typedef (val_type);
2053 if ((val_type == NULL)
2054 || (val_type->code () == TYPE_CODE_ERROR))
2056 if (expect_type != NULL)
2057 val_type = expect_type;
2060 struct_return = using_struct_return (exp->gdbarch, method,
2061 val_type);
2063 else if (expect_type != NULL)
2065 struct_return = using_struct_return (exp->gdbarch, NULL,
2066 check_typedef (expect_type));
2069 /* Found a function symbol. Now we will substitute its
2070 value in place of the message dispatcher (obj_msgSend),
2071 so that we call the method directly instead of thru
2072 the dispatcher. The main reason for doing this is that
2073 we can now evaluate the return value and parameter values
2074 according to their known data types, in case we need to
2075 do things like promotion, dereferencing, special handling
2076 of structs and doubles, etc.
2078 We want to use the type signature of 'method', but still
2079 jump to objc_msgSend() or objc_msgSend_stret() to better
2080 mimic the behavior of the runtime. */
2082 if (method)
2084 if (method->type ()->code () != TYPE_CODE_FUNC)
2085 error (_("method address has symbol information "
2086 "with non-function type; skipping"));
2088 /* Create a function pointer of the appropriate type, and
2089 replace its value with the value of msg_send or
2090 msg_send_stret. We must use a pointer here, as
2091 msg_send and msg_send_stret are of pointer type, and
2092 the representation may be different on systems that use
2093 function descriptors. */
2094 if (struct_return)
2095 called_method
2096 = value_from_pointer (lookup_pointer_type (method->type ()),
2097 value_as_address (msg_send_stret));
2098 else
2099 called_method
2100 = value_from_pointer (lookup_pointer_type (method->type ()),
2101 value_as_address (msg_send));
2103 else
2105 if (struct_return)
2106 called_method = msg_send_stret;
2107 else
2108 called_method = msg_send;
2112 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2114 /* If the return type doesn't look like a function type,
2115 call an error. This can happen if somebody tries to
2116 turn a variable into a function call. This is here
2117 because people often want to call, eg, strcmp, which
2118 gdb doesn't know is a function. If gdb isn't asked for
2119 it's opinion (ie. through "whatis"), it won't offer
2120 it. */
2122 struct type *callee_type = called_method->type ();
2124 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2125 callee_type = callee_type->target_type ();
2126 callee_type = callee_type->target_type ();
2128 if (callee_type)
2130 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2131 return value::allocate (expect_type);
2132 else
2133 return value::allocate (callee_type);
2135 else
2136 error (_("Expression of type other than "
2137 "\"method returning ...\" used as a method"));
2140 /* Now depending on whether we found a symbol for the method,
2141 we will either call the runtime dispatcher or the method
2142 directly. */
2144 args[0] = target;
2145 args[1] = value_from_longest (long_type, selector);
2147 if (gnu_runtime && (method != NULL))
2149 /* Function objc_msg_lookup returns a pointer. */
2150 struct type *tem_type = called_method->type ();
2151 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2152 called_method->deprecated_set_type (tem_type);
2153 called_method = call_function_by_hand (called_method, NULL, args);
2156 return call_function_by_hand (called_method, NULL, args);
2159 /* Helper function for MULTI_SUBSCRIPT. */
2161 static struct value *
2162 eval_multi_subscript (struct type *expect_type, struct expression *exp,
2163 enum noside noside, value *arg1,
2164 gdb::array_view<value *> args)
2166 for (value *arg2 : args)
2168 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2170 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2172 else
2174 arg1 = coerce_ref (arg1);
2175 struct type *type = check_typedef (arg1->type ());
2177 switch (type->code ())
2179 case TYPE_CODE_PTR:
2180 case TYPE_CODE_ARRAY:
2181 case TYPE_CODE_STRING:
2182 arg1 = value_subscript (arg1, value_as_long (arg2));
2183 break;
2185 default:
2186 if (type->name ())
2187 error (_("cannot subscript something of type `%s'"),
2188 type->name ());
2189 else
2190 error (_("cannot subscript requested type"));
2194 return (arg1);
2197 namespace expr
2200 value *
2201 objc_msgcall_operation::evaluate (struct type *expect_type,
2202 struct expression *exp,
2203 enum noside noside)
2205 enum noside sub_no_side = EVAL_NORMAL;
2206 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2208 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2209 sub_no_side = EVAL_NORMAL;
2210 else
2211 sub_no_side = noside;
2212 value *target
2213 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2215 if (value_as_long (target) == 0)
2216 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2217 else
2218 sub_no_side = noside;
2219 std::vector<operation_up> &args = std::get<2> (m_storage);
2220 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2221 argvec[0] = nullptr;
2222 argvec[1] = nullptr;
2223 for (int i = 0; i < args.size (); ++i)
2224 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2225 argvec[args.size () + 2] = nullptr;
2227 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2228 get<0> (m_storage), target,
2229 gdb::make_array_view (argvec,
2230 args.size () + 3));
2233 value *
2234 multi_subscript_operation::evaluate (struct type *expect_type,
2235 struct expression *exp,
2236 enum noside noside)
2238 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2239 std::vector<operation_up> &values = std::get<1> (m_storage);
2240 value **argvec = XALLOCAVEC (struct value *, values.size ());
2241 for (int ix = 0; ix < values.size (); ++ix)
2242 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2243 return eval_multi_subscript (expect_type, exp, noside, arg1,
2244 gdb::make_array_view (argvec, values.size ()));
2247 value *
2248 logical_and_operation::evaluate (struct type *expect_type,
2249 struct expression *exp,
2250 enum noside noside)
2252 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2254 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2255 EVAL_AVOID_SIDE_EFFECTS);
2257 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2259 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2260 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2262 else
2264 bool tem = value_logical_not (arg1);
2265 if (!tem)
2267 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2268 tem = value_logical_not (arg2);
2270 struct type *type = language_bool_type (exp->language_defn,
2271 exp->gdbarch);
2272 return value_from_longest (type, !tem);
2276 value *
2277 logical_or_operation::evaluate (struct type *expect_type,
2278 struct expression *exp,
2279 enum noside noside)
2281 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2283 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2284 EVAL_AVOID_SIDE_EFFECTS);
2286 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2288 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2289 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2291 else
2293 bool tem = value_logical_not (arg1);
2294 if (tem)
2296 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2297 tem = value_logical_not (arg2);
2300 struct type *type = language_bool_type (exp->language_defn,
2301 exp->gdbarch);
2302 return value_from_longest (type, !tem);
2306 value *
2307 adl_func_operation::evaluate (struct type *expect_type,
2308 struct expression *exp,
2309 enum noside noside)
2311 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2312 std::vector<value *> args (arg_ops.size ());
2313 for (int i = 0; i < arg_ops.size (); ++i)
2314 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2316 struct symbol *symp;
2317 find_overload_match (args, std::get<0> (m_storage).c_str (),
2318 NON_METHOD,
2319 nullptr, nullptr,
2320 nullptr, &symp, nullptr, 0, noside);
2321 if (symp->type ()->code () == TYPE_CODE_ERROR)
2322 error_unknown_type (symp->print_name ());
2323 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2324 return evaluate_subexp_do_call (exp, noside, callee, args,
2325 nullptr, expect_type);
2329 /* This function evaluates brace-initializers (in C/C++) for
2330 structure types. */
2332 struct value *
2333 array_operation::evaluate_struct_tuple (struct value *struct_val,
2334 struct expression *exp,
2335 enum noside noside, int nargs)
2337 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2338 struct type *struct_type = check_typedef (struct_val->type ());
2339 struct type *field_type;
2340 int fieldno = -1;
2342 int idx = 0;
2343 while (--nargs >= 0)
2345 struct value *val = NULL;
2346 int bitpos, bitsize;
2347 bfd_byte *addr;
2349 fieldno++;
2350 /* Skip static fields. */
2351 while (fieldno < struct_type->num_fields ()
2352 && struct_type->field (fieldno).is_static ())
2353 fieldno++;
2354 if (fieldno >= struct_type->num_fields ())
2355 error (_("too many initializers"));
2356 field_type = struct_type->field (fieldno).type ();
2357 if (field_type->code () == TYPE_CODE_UNION
2358 && struct_type->field (fieldno).name ()[0] == '0')
2359 error (_("don't know which variant you want to set"));
2361 /* Here, struct_type is the type of the inner struct,
2362 while substruct_type is the type of the inner struct.
2363 These are the same for normal structures, but a variant struct
2364 contains anonymous union fields that contain substruct fields.
2365 The value fieldno is the index of the top-level (normal or
2366 anonymous union) field in struct_field, while the value
2367 subfieldno is the index of the actual real (named inner) field
2368 in substruct_type. */
2370 field_type = struct_type->field (fieldno).type ();
2371 if (val == 0)
2372 val = in_args[idx++]->evaluate (field_type, exp, noside);
2374 /* Now actually set the field in struct_val. */
2376 /* Assign val to field fieldno. */
2377 if (val->type () != field_type)
2378 val = value_cast (field_type, val);
2380 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
2381 bitpos = struct_type->field (fieldno).loc_bitpos ();
2382 addr = struct_val->contents_writeable ().data () + bitpos / 8;
2383 if (bitsize)
2384 modify_field (struct_type, addr,
2385 value_as_long (val), bitpos % 8, bitsize);
2386 else
2387 memcpy (addr, val->contents ().data (),
2388 val->type ()->length ());
2391 return struct_val;
2394 value *
2395 array_operation::evaluate (struct type *expect_type,
2396 struct expression *exp,
2397 enum noside noside)
2399 int tem;
2400 int tem2 = std::get<0> (m_storage);
2401 int tem3 = std::get<1> (m_storage);
2402 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2403 int nargs = tem3 - tem2 + 1;
2404 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2406 if (expect_type != nullptr
2407 && type->code () == TYPE_CODE_STRUCT)
2409 struct value *rec = value::allocate (expect_type);
2411 memset (rec->contents_raw ().data (), '\0', type->length ());
2412 return evaluate_struct_tuple (rec, exp, noside, nargs);
2415 if (expect_type != nullptr
2416 && type->code () == TYPE_CODE_ARRAY)
2418 struct type *range_type = type->index_type ();
2419 struct type *element_type = type->target_type ();
2420 struct value *array = value::allocate (expect_type);
2421 int element_size = check_typedef (element_type)->length ();
2422 LONGEST low_bound, high_bound, index;
2424 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2426 low_bound = 0;
2427 high_bound = (type->length () / element_size) - 1;
2429 index = low_bound;
2430 memset (array->contents_raw ().data (), 0, expect_type->length ());
2431 for (tem = nargs; --nargs >= 0;)
2433 struct value *element;
2435 element = in_args[index - low_bound]->evaluate (element_type,
2436 exp, noside);
2437 if (element->type () != element_type)
2438 element = value_cast (element_type, element);
2439 if (index > high_bound)
2440 /* To avoid memory corruption. */
2441 error (_("Too many array elements"));
2442 memcpy (array->contents_raw ().data ()
2443 + (index - low_bound) * element_size,
2444 element->contents ().data (),
2445 element_size);
2446 index++;
2448 return array;
2451 if (expect_type != nullptr
2452 && type->code () == TYPE_CODE_SET)
2454 struct value *set = value::allocate (expect_type);
2455 gdb_byte *valaddr = set->contents_raw ().data ();
2456 struct type *element_type = type->index_type ();
2457 struct type *check_type = element_type;
2458 LONGEST low_bound, high_bound;
2460 /* Get targettype of elementtype. */
2461 while (check_type->code () == TYPE_CODE_RANGE
2462 || check_type->code () == TYPE_CODE_TYPEDEF)
2463 check_type = check_type->target_type ();
2465 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2466 error (_("(power)set type with unknown size"));
2467 memset (valaddr, '\0', type->length ());
2468 int idx = 0;
2469 for (tem = 0; tem < nargs; tem++)
2471 LONGEST range_low, range_high;
2472 struct type *range_low_type, *range_high_type;
2473 struct value *elem_val;
2475 elem_val = in_args[idx++]->evaluate (element_type, exp, noside);
2476 range_low_type = range_high_type = elem_val->type ();
2477 range_low = range_high = value_as_long (elem_val);
2479 /* Check types of elements to avoid mixture of elements from
2480 different types. Also check if type of element is "compatible"
2481 with element type of powerset. */
2482 if (range_low_type->code () == TYPE_CODE_RANGE)
2483 range_low_type = range_low_type->target_type ();
2484 if (range_high_type->code () == TYPE_CODE_RANGE)
2485 range_high_type = range_high_type->target_type ();
2486 if ((range_low_type->code () != range_high_type->code ())
2487 || (range_low_type->code () == TYPE_CODE_ENUM
2488 && (range_low_type != range_high_type)))
2489 /* different element modes. */
2490 error (_("POWERSET tuple elements of different mode"));
2491 if ((check_type->code () != range_low_type->code ())
2492 || (check_type->code () == TYPE_CODE_ENUM
2493 && range_low_type != check_type))
2494 error (_("incompatible POWERSET tuple elements"));
2495 if (range_low > range_high)
2497 warning (_("empty POWERSET tuple range"));
2498 continue;
2500 if (range_low < low_bound || range_high > high_bound)
2501 error (_("POWERSET tuple element out of range"));
2502 range_low -= low_bound;
2503 range_high -= low_bound;
2504 for (; range_low <= range_high; range_low++)
2506 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2508 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2509 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2510 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2511 |= 1 << bit_index;
2514 return set;
2517 value **argvec = XALLOCAVEC (struct value *, nargs);
2518 for (tem = 0; tem < nargs; tem++)
2520 /* Ensure that array expressions are coerced into pointer
2521 objects. */
2522 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2524 return value_array (tem2, tem3, argvec);
2527 value *
2528 unop_extract_operation::evaluate (struct type *expect_type,
2529 struct expression *exp,
2530 enum noside noside)
2532 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2533 struct type *type = get_type ();
2535 if (type->length () > old_value->type ()->length ())
2536 error (_("length type is larger than the value type"));
2538 struct value *result = value::allocate (type);
2539 old_value->contents_copy (result, 0, 0, type->length ());
2540 return result;
2546 /* Helper for evaluate_subexp_for_address. */
2548 static value *
2549 evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
2550 value *x)
2552 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2554 struct type *type = check_typedef (x->type ());
2556 if (TYPE_IS_REFERENCE (type))
2557 return value::zero (lookup_pointer_type (type->target_type ()),
2558 not_lval);
2559 else if (x->lval () == lval_memory || value_must_coerce_to_target (x))
2560 return value::zero (lookup_pointer_type (x->type ()),
2561 not_lval);
2562 else
2563 error (_("Attempt to take address of "
2564 "value not located in memory."));
2566 return value_addr (x);
2569 namespace expr
2572 value *
2573 operation::evaluate_for_cast (struct type *expect_type,
2574 struct expression *exp,
2575 enum noside noside)
2577 value *val = evaluate (expect_type, exp, noside);
2578 return value_cast (expect_type, val);
2581 value *
2582 operation::evaluate_for_address (struct expression *exp, enum noside noside)
2584 value *val = evaluate (nullptr, exp, noside);
2585 return evaluate_subexp_for_address_base (exp, noside, val);
2588 value *
2589 scope_operation::evaluate_for_address (struct expression *exp,
2590 enum noside noside)
2592 value *x = value_aggregate_elt (std::get<0> (m_storage),
2593 std::get<1> (m_storage).c_str (),
2594 NULL, 1, noside);
2595 if (x == NULL)
2596 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
2597 return x;
2600 value *
2601 unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2602 enum noside noside)
2604 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2606 /* We can't optimize out "&*" if there's a user-defined operator*. */
2607 if (unop_user_defined_p (UNOP_IND, x))
2609 x = value_x_unop (x, UNOP_IND, noside);
2610 return evaluate_subexp_for_address_base (exp, noside, x);
2613 return coerce_array (x);
2616 value *
2617 var_msym_value_operation::evaluate_for_address (struct expression *exp,
2618 enum noside noside)
2620 const bound_minimal_symbol &b = std::get<0> (m_storage);
2621 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2622 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2624 struct type *type = lookup_pointer_type (val->type ());
2625 return value::zero (type, not_lval);
2627 else
2628 return value_addr (val);
2631 value *
2632 unop_memval_operation::evaluate_for_address (struct expression *exp,
2633 enum noside noside)
2635 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2636 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2639 value *
2640 unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2641 enum noside noside)
2643 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2644 EVAL_AVOID_SIDE_EFFECTS);
2645 struct type *type = typeval->type ();
2646 return value_cast (lookup_pointer_type (type),
2647 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2650 value *
2651 var_value_operation::evaluate_for_address (struct expression *exp,
2652 enum noside noside)
2654 symbol *var = std::get<0> (m_storage).symbol;
2656 /* C++: The "address" of a reference should yield the address
2657 * of the object pointed to. Let value_addr() deal with it. */
2658 if (TYPE_IS_REFERENCE (var->type ()))
2659 return operation::evaluate_for_address (exp, noside);
2661 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2663 struct type *type = lookup_pointer_type (var->type ());
2664 enum address_class sym_class = var->aclass ();
2666 if (sym_class == LOC_CONST
2667 || sym_class == LOC_CONST_BYTES
2668 || sym_class == LOC_REGISTER)
2669 error (_("Attempt to take address of register or constant."));
2671 return value::zero (type, not_lval);
2673 else
2674 return address_of_variable (var, std::get<0> (m_storage).block);
2677 value *
2678 var_value_operation::evaluate_with_coercion (struct expression *exp,
2679 enum noside noside)
2681 struct symbol *var = std::get<0> (m_storage).symbol;
2682 struct type *type = check_typedef (var->type ());
2683 if (type->code () == TYPE_CODE_ARRAY
2684 && !type->is_vector ()
2685 && CAST_IS_CONVERSION (exp->language_defn))
2687 struct value *val = address_of_variable (var,
2688 std::get<0> (m_storage).block);
2689 return value_cast (lookup_pointer_type (type->target_type ()), val);
2691 return evaluate (nullptr, exp, noside);
2696 /* Helper function for evaluating the size of a type. */
2698 static value *
2699 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2701 /* FIXME: This should be size_t. */
2702 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2703 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2704 "When applied to a reference or a reference type, the result is
2705 the size of the referenced type." */
2706 type = check_typedef (type);
2707 if (exp->language_defn->la_language == language_cplus
2708 && (TYPE_IS_REFERENCE (type)))
2709 type = check_typedef (type->target_type ());
2710 return value_from_longest (size_type, (LONGEST) type->length ());
2713 namespace expr
2716 value *
2717 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2719 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2720 return evaluate_subexp_for_sizeof_base (exp, val->type ());
2723 value *
2724 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2725 enum noside noside)
2728 const bound_minimal_symbol &b = std::get<0> (m_storage);
2729 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2731 struct type *type = mval->type ();
2732 if (type->code () == TYPE_CODE_ERROR)
2733 error_unknown_type (b.minsym->print_name ());
2735 /* FIXME: This should be size_t. */
2736 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2737 return value_from_longest (size_type, type->length ());
2740 value *
2741 subscript_operation::evaluate_for_sizeof (struct expression *exp,
2742 enum noside noside)
2744 if (noside == EVAL_NORMAL)
2746 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2747 EVAL_AVOID_SIDE_EFFECTS);
2748 struct type *type = check_typedef (val->type ());
2749 if (type->code () == TYPE_CODE_ARRAY)
2751 type = check_typedef (type->target_type ());
2752 if (type->code () == TYPE_CODE_ARRAY)
2754 type = type->index_type ();
2755 /* Only re-evaluate the right hand side if the resulting type
2756 is a variable length type. */
2757 if (type->bounds ()->flag_bound_evaluated)
2759 val = evaluate (nullptr, exp, EVAL_NORMAL);
2760 /* FIXME: This should be size_t. */
2761 struct type *size_type
2762 = builtin_type (exp->gdbarch)->builtin_int;
2763 return value_from_longest
2764 (size_type, (LONGEST) val->type ()->length ());
2770 return operation::evaluate_for_sizeof (exp, noside);
2773 value *
2774 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2775 enum noside noside)
2777 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2778 EVAL_AVOID_SIDE_EFFECTS);
2779 struct type *type = check_typedef (val->type ());
2780 if (!type->is_pointer_or_reference ()
2781 && type->code () != TYPE_CODE_ARRAY)
2782 error (_("Attempt to take contents of a non-pointer value."));
2783 type = type->target_type ();
2784 if (is_dynamic_type (type))
2785 type = value_ind (val)->type ();
2786 /* FIXME: This should be size_t. */
2787 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2788 return value_from_longest (size_type, (LONGEST) type->length ());
2791 value *
2792 unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2793 enum noside noside)
2795 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2798 value *
2799 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2800 enum noside noside)
2802 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2803 EVAL_AVOID_SIDE_EFFECTS);
2804 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
2807 value *
2808 var_value_operation::evaluate_for_sizeof (struct expression *exp,
2809 enum noside noside)
2811 struct type *type = std::get<0> (m_storage).symbol->type ();
2812 if (is_dynamic_type (type))
2814 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2815 type = val->type ();
2816 if (type->code () == TYPE_CODE_ARRAY)
2818 /* FIXME: This should be size_t. */
2819 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2820 if (type_not_allocated (type) || type_not_associated (type))
2821 return value::zero (size_type, not_lval);
2822 else if (is_dynamic_type (type->index_type ())
2823 && type->bounds ()->high.kind () == PROP_UNDEFINED)
2824 return value::allocate_optimized_out (size_type);
2827 return evaluate_subexp_for_sizeof_base (exp, type);
2830 value *
2831 var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2832 struct expression *exp,
2833 enum noside noside)
2835 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2836 return value::zero (to_type, not_lval);
2838 const bound_minimal_symbol &b = std::get<0> (m_storage);
2839 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
2841 val = value_cast (to_type, val);
2843 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2844 if (val->lval () == lval_memory)
2846 if (val->lazy ())
2847 val->fetch_lazy ();
2848 val->set_lval (not_lval);
2850 return val;
2853 value *
2854 var_value_operation::evaluate_for_cast (struct type *to_type,
2855 struct expression *exp,
2856 enum noside noside)
2858 value *val = evaluate_var_value (noside,
2859 std::get<0> (m_storage).block,
2860 std::get<0> (m_storage).symbol);
2862 val = value_cast (to_type, val);
2864 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2865 if (val->lval () == lval_memory)
2867 if (val->lazy ())
2868 val->fetch_lazy ();
2869 val->set_lval (not_lval);
2871 return val;
2876 /* Parse a type expression in the string [P..P+LENGTH). */
2878 struct type *
2879 parse_and_eval_type (const char *p, int length)
2881 char *tmp = (char *) alloca (length + 4);
2883 tmp[0] = '(';
2884 memcpy (tmp + 1, p, length);
2885 tmp[length + 1] = ')';
2886 tmp[length + 2] = '0';
2887 tmp[length + 3] = '\0';
2888 expression_up expr = parse_expression (tmp);
2889 expr::unop_cast_operation *op
2890 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2891 if (op == nullptr)
2892 error (_("Internal error in eval_type."));
2893 return op->get_type ();